Files

86 lines
4.5 KiB
C#

using System.Numerics;
using GamePort.Assets;
using GamePort.Rendering.Vulkan;
// These tests read back actual Vulkan color attachments. No window or original
// executable is needed. The source-linked project avoids rebuilding GamePort.
var white=PngImage.White;
var count=0;
using var renderer=new VulkanRenderer(Scene(new Layer(Vector3.UnitZ,.8f)),white,64,64,validation:true);
Console.WriteLine($"Blend test GPU: {renderer.DeviceName}");
Check("opaque default",Scene(new Layer(Vector3.UnitX,.4f)),255,0,0);
Check("alpha half opacity",Scene(new(Vector3.UnitX,.4f,MeshBlend.Alpha,.5f),new(Vector3.UnitZ,.8f)),128,0,128);
Check("additive half opacity",Scene(new(Vector3.UnitX,.4f,MeshBlend.Additive,.5f),new(Vector3.UnitZ,.8f)),128,0,255);
Check("opaque depth occludes blend",Scene(new(Vector3.UnitX,.4f,MeshBlend.Alpha,.5f),new(Vector3.UnitZ,.2f)),0,0,255);
Check("blend does not write depth",Scene(new(Vector3.UnitX,.2f,MeshBlend.Alpha,.5f),new(Vector3.UnitY,.4f,MeshBlend.Alpha,.5f),new(Vector3.UnitZ,.8f)),64,128,64);
Check("UI follows blended world",Scene(new(Vector3.UnitY,.9f,Screen:true),new(Vector3.UnitX,.4f,MeshBlend.Additive,.5f),new(Vector3.UnitZ,.8f)),0,255,0);
Check("UI alpha ignores depth",Scene(new(Vector3.UnitX,.9f,MeshBlend.Alpha,.5f,true),new(Vector3.UnitZ,.2f)),128,0,128);
Check("UI additive ignores depth",Scene(new(Vector3.UnitX,.9f,MeshBlend.Additive,.5f,true),new(Vector3.UnitZ,.2f)),128,0,255);
Check("zero opacity",Scene(new(Vector3.UnitX,.4f,MeshBlend.Alpha,0),new(Vector3.UnitZ,.8f)),0,0,255);
// Texture coverage is tested before material opacity: a solid texture at .5
// opacity must draw, while a cutout remains absent even at full opacity.
var cutout=Scene(new(Vector3.UnitX,.4f,MeshBlend.Alpha,.5f),new(Vector3.UnitZ,.8f));
cutout.Draws[0]=cutout.Draws[0] with {TextureId=7};
cutout.Textures[7]=new PngImage(1,1,[255,255,255,0]);
Check("texture cutout unchanged",cutout,0,0,255);
var invalid=Scene(new Layer(Vector3.UnitX,.4f));
renderer.ReplaceScene(invalid,white);
foreach(var opacity in new[]{float.NaN,float.PositiveInfinity,-.01f,1.01f})
{
invalid.Draws[0].Opacity=opacity;
Throws("invalid mutable opacity",()=>renderer.Render(Matrix4x4.Identity));
}
invalid.Draws[0].Opacity=1;
invalid.Draws[0].Blend=(MeshBlend)99;
invalid.Draws[0].Visible=false;
Throws("invalid hidden blend",()=>renderer.Render(Matrix4x4.Identity));
Throws("replacement rejects invalid blend",()=>renderer.ReplaceScene(invalid,white));
invalid.Draws[0].Blend=MeshBlend.Opaque;
invalid.Draws[0].Visible=true;
renderer.Render(Matrix4x4.Identity);
AssertPixel("renderer usable after rejected state",255,0,0);
if(renderer.ValidationErrors!=0)throw new Exception($"Vulkan reported {renderer.ValidationErrors} validation errors.");
if(args.Length>0)renderer.SavePpm(args[0]);
Console.WriteLine($"PASS: {count} Vulkan material/readback checks; validation errors: {renderer.ValidationErrors}.");
void Check(string name,Mesh scene,int r,int g,int b)
{
renderer.ReplaceScene(scene,white);
renderer.Render(Matrix4x4.Identity);
AssertPixel(name,r,g,b);
}
void AssertPixel(string name,int r,int g,int b)
{
var pixels=renderer.ReadPixels();
var offset=(32*64+32)*4;
var expected=new[]{r,g,b};
for(var channel=0;channel<3;channel++)
if(Math.Abs(pixels[offset+channel]-expected[channel])>2)
throw new Exception($"{name}: expected ({r},{g},{b}), got ({pixels[offset]},{pixels[offset+1]},{pixels[offset+2]}).");
count++;
Console.WriteLine($"PASS: {name}");
}
void Throws(string name,Action action)
{
try {action();}
catch(ArgumentException){count++;Console.WriteLine($"PASS: {name}");return;}
throw new Exception($"{name}: expected ArgumentException.");
}
static Mesh Scene(params Layer[] layers)
{
var vertices=new List<MeshVertex>();var indices=new List<uint>();var draws=new List<MeshDraw>();
foreach(var layer in layers)
{
uint start=(uint)vertices.Count,first=(uint)indices.Count;
foreach(var xy in new[]{new Vector2(-1,-1),new Vector2(1,-1),new Vector2(1,1),new Vector2(-1,1)})
vertices.Add(new MeshVertex(new Vector3(xy,layer.Z),new Vector2(.5f),layer.Color));
indices.AddRange(new[]{start,start+1,start+2,start+2,start+3,start});
draws.Add(new MeshDraw(first,6,-1){Blend=layer.Blend,Opacity=layer.Opacity,ScreenSpace=layer.Screen});
}
return new Mesh(vertices.ToArray(),indices.ToArray()){HasExplicitDraws=true,Draws=draws.ToArray()};
}
readonly record struct Layer(Vector3 Color,float Z,MeshBlend Blend=MeshBlend.Opaque,float Opacity=1,bool Screen=false);