• Advertisement
Sign in to follow this  

[SharpDX][.Net] Rendertargets dont display correctly when used as textures

This topic is 1770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello gamedev.
I'm having some troubles getting my rendertargets to display correctly when used as textures. I generate all the rendertargets in an earlier stage and pass them on to the drawing stage, according to GPU PerfStudio they look like this when passed to the drawing stage:


This is what i expect them to look like, so they seem correct. However when trying to draw them as fullscreen textures all I see is this: 



This is how i create my rendertargets and shaderresourceviews:

Dim rtdesc As RenderTargetViewDescription
'rtdesc.Format = Format.R32G32B32A32_Float
rtdesc.Format = Format.R8G8B8A8_UNorm
rtdesc.Dimension = RenderTargetViewDimension.Texture2D
rtdesc.Texture2D.MipSlice = 0

Dim srvdesc As ShaderResourceViewDescription
srvdesc.Dimension = ShaderResourceViewDimension.Texture2D
'srvdesc.Format = Format.R32G32B32A32_Float
srvdesc.Format = Format.R8G8B8A8_UNorm
srvdesc.Texture2D.MostDetailedMip = 0
srvdesc.Texture2D.MipLevels = 1

Dim renderTargetColor As Texture2D = New Texture2D(device, rendertargetdesc)
Dim renderTargetNormal As Texture2D = New Texture2D(device, rendertargetdesc)
Dim renderTargetDepth As Texture2D = New Texture2D(device, rendertargetdesc)

Dim rttColorRTV = New RenderTargetView(device, renderTargetColor, rtdesc)
Dim rttColorSRV = New ShaderResourceView(device, renderTargetColor, srvdesc)
Dim rttNormalRTV = New RenderTargetView(device, renderTargetNormal, rtdesc)
Dim rttNormalSRV = New ShaderResourceView(device, renderTargetNormal, srvdesc)
Dim rttDepthRTV = New RenderTargetView(device, renderTargetDepth, rtdesc)
Dim rttDepthSRV = New ShaderResourceView(device, renderTargetDepth, srvdesc)


My drawing stage:

context.InputAssembler.InputLayout = layout2d
context.PixelShader.SetShaderResource(0, rttColorSRV)
context.PixelShader.SetShaderResource(1, rttNormalSRV)
context.PixelShader.SetShaderResource(2, rttDepthSRV)
context.InputAssembler.SetVertexBuffers(0, New VertexBufferBinding(quad2dvb, Utilities.SizeOf(Of Vertex2DTextured)(), 0))
context.Draw(6, 0)
swapchain.Present(0, PresentFlags.None)


My shader:

SamplerState colorSampler;
Texture2D colorMap;
Texture2D NormalMap;
Texture2D DepthMap;

struct VertexShaderInput
    float3 Position : POSITION;
    float2 TexCoord : TEXCOORD0;

struct VertexShaderOutput
    float4 Position : SV_POSITION;
    float2 TexCoord : TEXCOORD0;

VertexShaderOutput VS(VertexShaderInput input)
    VertexShaderOutput output;
    output.Position.w = 1.0f;
    output.Position.xyz = input.Position;
    output.TexCoord = input.TexCoord;
    return output;

float4 PS(VertexShaderOutput input) : SV_TARGET
    return colorMap.Sample(colorSampler, input.TexCoord);
    //return NormalMap.Sample(colorSampler, input.TexCoord);
    //return DepthMap.Sample(colorSampler, input.TexCoord);


At first i thought the fullscreen quad i use was wrong, but I loaded a texture from a bmp file, and it renders correctly as a textured fullscreen quad. Is there something special I have to do with my ShaderResourceViews before using them? I release the RenderTargetViews before using the ShaderResourceViews, Switching which texture to use in the shader gives the exact same result except it's a different color.

Edited by bjornp

Share this post

Link to post
Share on other sites

It looks like you still have the render target textures bound as outputs when you bind those textures as inputs, which means that the runtime will set them to NULL in order to avoid a read/write conflict. The runtime will warn you about these kinds of things if you pass the DEBUG flag when creating your device. However the messages will go to the native debugging output, so you need to either enable managed debugging for your project (which you can only do with paid versions of Visual Studio) or you can use a separate program like DebugView.

Edited by MJP

Share this post

Link to post
Share on other sites
Hi, MJP and thank you for your answer. The first thing i do in my drawing code is
So I don't think that is the case, if i don't do this the shaderresourceviews don't show up at all in perfstudio.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement