Hello gamedev,
I'm trying to implement and learn how to do deferred rendering, but having some problems along the way.
In my first pass I render the scene and save the color, normal, and depth to 3 different rendertargets. This seems to work fine according to GPU PerfStudio 2, as it shows me these 3 images:
However the second stage is causing me problems. From what I understand I'm supposed to draw a fullscreen quad so that every pixel of my rendertargets are used in the lighting calculations. What do i need to change between drawing the scene above to drawing the fullscreen quad in terms of settings?
At the moment I set up my vertices as this:
Imports SharpDX
Public Structure Vertex2DTextured
Public Position As Vector3
Public TextureCoords As Vector2
Public Sub New(Position As Vector3, texcoord As Vector2)
Me.Position = Position
Me.TextureCoords = texcoord
End Sub
End Structure
Dim quad2d(5) As Vertex2DTextured
quad2d(0) = New Vertex2DTextured(New Vector3(-1, 1, 0), New Vector2(0, 0))
quad2d(1) = New Vertex2DTextured(New Vector3(1, 1, 0), New Vector2(1, 0))
quad2d(2) = New Vertex2DTextured(New Vector3(-1, -1, 0), New Vector2(0, 1))
quad2d(3) = New Vertex2DTextured(New Vector3(1, 1, 0), New Vector2(1, 0))
quad2d(4) = New Vertex2DTextured(New Vector3(1, -1, 0), New Vector2(1, 1))
quad2d(5) = New Vertex2DTextured(New Vector3(-1, -1, 0), New Vector2(0, 1))
Dim quad2dvb As Buffer = Buffer.Create(device, BindFlags.VertexBuffer, quad2d)
And my renderloop looks like this:
context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0F, 0)
'First stage, works fine
context.InputAssembler.InputLayout = layout3d 'Set layout to POS (float4), NOR, COL, TEX
context.OutputMerger.SetTargets(rtvs) 'Set the rendertargets
context.ClearRenderTargetView(rtvs(0), Color4.Black)
context.ClearRenderTargetView(rtvs(1), Color.Gray)
context.ClearRenderTargetView(rtvs(2), Color4.White)
context.VertexShader.Set(vertexShader)
context.PixelShader.Set(pixelShader)
Manager.Render()
'Second stage, does not work fine :(
'context.OutputMerger.SetTargets(renderview)
context.OutputMerger.SetTargets(depthView, renderview)
context.VertexShader.Set(vertexShader2)
context.PixelShader.Set(pixelShader2)
context.InputAssembler.InputLayout = layout2d 'Set layout to Pos(float3), TEX
context.ClearRenderTargetView(renderview, Color.Black)
context.PixelShader.SetSampler(0, SampleState)
context.PixelShader.SetShaderResources(0, rendertv) 'My 3 ShaderRescourceViews from stage 1
context.InputAssembler.SetVertexBuffers(0, New VertexBufferBinding(quad2dvb, Utilities.SizeOf(Of Quad2DTextured)(), 0))
context.Draw(6, 0)
swapchain.Present(0, PresentFlags.None)
I just wanted to see if the fullscreen quad was drawn in the second stage to vertex- and pixelshader2 corresponds to this:
SamplerState colorSampler;
Texture2D colorMap;
Texture2D NormalMap;
Texture2D DepthMap;
struct VertexShaderInput
{
float3 Position : POSITION;
float2 TexCoord : TEXCOORD;
};
struct VertexShaderOutput
{
float3 Position : POSITION;
float2 TexCoord : TEXCOORD;
};
VertexShaderOutput VS(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = input.Position;
output.TexCoord = input.TexCoord;
return output;
}
float4 PS(VertexShaderOutput input) : SV_TARGET
{
return colorMap.Sample(colorSampler, input.TexCoord);
}
However, nothing is drawn. When i look in PerfStudio I only see 1 incoming texture:
which is none of my rendertargets... :(. I was hoping my first rendertarget would be drawn.
My 2D and 3D layouts:
Dim layout2d As InputLayout = New InputLayout(device, signature2, {New InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), New InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0)})
Dim layout3d As InputLayout = New InputLayout(device, signature, {New InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), New InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0), New InputElement("NORMAL", 0, Format.R32G32B32_Float, 32, 0), New InputElement("TEXCOORD", 0, Format.R32G32_Float, 44, 0)})
If anyone could help me get this quad rendered it would be greatly appreciated.
/ Björn