Skip to content

Commit

Permalink
Add a RenderTest which writes custom values via gl_FragDepth.
Browse files Browse the repository at this point in the history
  • Loading branch information
mellinoe committed Mar 27, 2020
1 parent 92ed125 commit 6853a63
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Veldrid.Tests/RenderTests.cs
Expand Up @@ -1180,6 +1180,67 @@ public void RenderToCubemapFace(uint layerCount, uint targetLayer, uint targetFa
}
GD.Unmap(staging, (targetLayer * 6) + targetFace);
}

[Fact]
public void WriteFragmentDepth()
{
Texture depthTarget = RF.CreateTexture(
TextureDescription.Texture2D(64, 64, 1, 1, PixelFormat.R32_Float, TextureUsage.DepthStencil | TextureUsage.Sampled));
Framebuffer framebuffer = RF.CreateFramebuffer(new FramebufferDescription(depthTarget));

string setName = "FullScreenWriteDepth";
ShaderSetDescription shaderSet = new ShaderSetDescription(
Array.Empty<VertexLayoutDescription>(),
TestShaders.LoadVertexFragment(RF, setName));

ResourceLayout layout = RF.CreateResourceLayout(new ResourceLayoutDescription(
new ResourceLayoutElementDescription("FramebufferInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

DeviceBuffer ub = RF.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer));
GD.UpdateBuffer(ub, 0, new Vector4(depthTarget.Width, depthTarget.Height, 0, 0));
ResourceSet rs = RF.CreateResourceSet(new ResourceSetDescription(layout, ub));

GraphicsPipelineDescription gpd = new GraphicsPipelineDescription(
BlendStateDescription.SingleOverrideBlend,
new DepthStencilStateDescription(true, true, ComparisonKind.Always),
RasterizerStateDescription.CullNone,
PrimitiveTopology.TriangleList,
shaderSet,
layout,
framebuffer.OutputDescription);

Pipeline pipeline = RF.CreateGraphicsPipeline(ref gpd);

CommandList cl = RF.CreateCommandList();

cl.Begin();
cl.SetFramebuffer(framebuffer);
cl.SetFullViewports();
cl.SetFullScissorRects();
cl.ClearDepthStencil(0f);
cl.SetPipeline(pipeline);
cl.SetGraphicsResourceSet(0, rs);
cl.Draw(3);
cl.End();
GD.SubmitCommands(cl);
GD.WaitForIdle();

Texture readback = GetReadback(depthTarget);

MappedResourceView<float> readView = GD.Map<float>(readback, MapMode.Read);
for (uint y = 0; y < readback.Height; y++)
{
for (uint x = 0; x < readback.Width; x++)
{
float xComp = x;
float yComp = y * readback.Width;
float val = (yComp + xComp) / (readback.Width * readback.Height);

Assert.Equal(val, readView[x, y], 2);
}
}
GD.Unmap(readback);
}
}

#if TEST_OPENGL
Expand Down
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions src/Veldrid.Tests/Shaders/FullScreenWriteDepth.frag
@@ -0,0 +1,17 @@
#version 450

layout(location = 0) in vec2 fsin_UV;

layout (set = 0, binding = 0) uniform FramebufferInfo
{
vec4 OutputSize;
};

void main()
{
vec2 uv = gl_FragCoord.xy - vec2(0.5, 0.5);
float xComp = uv.x;
float yComp = uv.y * OutputSize.x;
float val = (yComp + xComp) / (OutputSize.x * OutputSize.y);
gl_FragDepth = val;
}
Binary file not shown.
12 changes: 12 additions & 0 deletions src/Veldrid.Tests/Shaders/FullScreenWriteDepth.vert
@@ -0,0 +1,12 @@
#version 450

layout(location = 0) out vec2 fsin_UV;

void main()
{
float x = -1.0 + float((gl_VertexIndex & 1) << 2);
float y = -1.0 + float((gl_VertexIndex & 2) << 1);
fsin_UV.x = (x + 1.0) * 0.5;
fsin_UV.y = (y + 1.0) * 0.5;
gl_Position = vec4(x, y, 0, 1);
}
Binary file not shown.
1 change: 1 addition & 0 deletions src/Veldrid.Tests/TestUtils.cs
Expand Up @@ -174,6 +174,7 @@ public GraphicsDeviceTestBase()
if (Environment.GetEnvironmentVariable("VELDRID_TESTS_ENABLE_RENDERDOC") == "1"
&& RenderDoc.Load(out _renderDoc))
{
_renderDoc.APIValidation = true;
_renderDoc.DebugOutputMute = false;
}
Activator.CreateInstance<T>().CreateGraphicsDevice(out _window, out _gd);
Expand Down
2 changes: 2 additions & 0 deletions src/Veldrid.Tests/Veldrid.Tests.csproj
Expand Up @@ -46,6 +46,8 @@
<None Remove="Shaders\FullScreenTriSampleTexture2D.frag" />
<None Remove="Shaders\FullScreenTriSampleTexture2D.vert" />
<None Remove="Shaders\FullScreenTriSampleTextureArray.vert" />
<None Remove="Shaders\FullScreenWriteDepth.frag" />
<None Remove="Shaders\FullScreenWriteDepth.vert" />
<None Remove="Shaders\VertexLayoutTestShader.frag" />
<None Remove="Shaders\VertexLayoutTestShader.vert" />
</ItemGroup>
Expand Down

0 comments on commit 6853a63

Please sign in to comment.