[SharpDX][VB.net] Problems drawing a fullscreenquad.

Started by
0 comments, last by Gavin Williams 11 years, 1 month ago

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:

post-209015-0-75355900-1362936990_thumb.post-209015-0-74717500-1362936991_thumb.post-209015-0-79513800-1362936992_thumb.

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:

post-209015-0-74868500-1362938436_thumb.

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

Advertisement

Hi Bjorn,

Just to follow the steps through, here's how i do it ...

* render to gbuffer - Color, Normal, Position / Depth

* render shaped lights to light textures (require Normal & Position maps) - skip this for now, but I include it for completeness

* render fullscreen light to fullscreen texture (requires normal texture) - start with this, it's much easier

* render final light map // composes all light maps into one - skip this when you just have a fullscreen lightmap

* render final composition to the default or output depth stencil view - additive blend the final light map with your color (albedo) map

So my first comment is that i find it strange that your input looks different as though it were a different frame, or the camera has been moved. So you might have to check that everything is happening in proper sequence.

I'm not sure about this line

context.PixelShader.SetShaderResources(0, rendertv)

That should be a Shader Resource View, not a Render Target View. The way i do it is by having both SRV & RTV over the same resource, and you use whichever is appropriate for usage. Like this ...

            // create light map texture
            resLightMap = new Texture2D(device, resDescription);
            srvLightMap = new ShaderResourceView(device, resLightMap);
            rtvLightMap = new RenderTargetView(device, resLightMap);

Let me know how it's going, if you've made progress or if you need more info.

This topic is closed to new replies.

Advertisement