3

I would like to use the Render Target as the input the Compute Shader in DirectX 11. I only need to read the Render Target in the Compute Shader, and in my case I do not have to render the Compute Shader output (although I do have to consume this in the CPU later, and may need to process it in additional Compute Shader passes.)

I have seen comments that this is possible, but I cannot seem to find any sample code that shows how to consume the render target (a Texture2D) as a Compute Shader Buffer in a Shader Resource View. I have tried a number of options, but have not been able to create the Shader Resource View that the Computer Shader needs as input.

A pointer to a fragment of sample code would be most appreciated.

2 Answers 2

2

Your resource needs to indeed have Shader resource usage mode to be readable via compute shader: -For a standard render target : D3D11_BIND_SHADER_RESOURCE -For a swap chain : DXGI_USAGE_SHADER_INPUT in swap chain description.

Then you just create a Shader Resource View as usual.

It is also possible (in some extents), to have your buffer also writeable directly by the compute shader. Flags is D3D11_BIND_UNORDERED_ACCESS or DXGI_USAGE_UNORDERED_ACCESS in that case (please note this doesn't work for multisampled textures tho).

Here is a very simple compute shader that reads samples from a texture from UV coords:

RWStructuredBuffer<float4> rwbuffer;

//Render target previously rendered
Texture2D tex;

//Buffer containing uvs for sampling
 StructuredBuffer<float2> uv <string uiname="UV Buffer";>;

 SamplerState mySampler : IMMUTABLE
 {
     Filter = MIN_MAG_MIP_LINEAR;
     AddressU = Clamp;
     AddressV = Clamp;
 };

 [numthreads(1, 1, 1)]
 void CS( uint3 i : SV_DispatchThreadID)
 { 
//Read color and write to buffer
rwbuffer[i.x] = tex.SampleLevel(mySampler,uv[i.x],0);
 }

 technique11 Process
 {
pass P0
{
    SetComputeShader( CompileShader( cs_5_0, CS() ) );
}
 }

Hope that helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks so much. Appreciate the sample code for the shader as well.
I must apologize, but I continue to struggle with this. What I am trying to accomplish may not have been clear from my original description: process the output from the Output-Merger stage. I need to see the results of the application of the Depth Stencil. I have tried to add DXGI_USAGE_SHADER_INPUT to the Swap Chain Descriptor as suggested: swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT;
I can find my misconception if I could just see a simple C++ code sample that creates a Shader Resource View of the output of the Output-Merger stage that I can set as the input to the Compute Shader. I still cannot find any code that does this.
I see a workaround: render to a texture instead of the Back Buffer. This will work fine in my case, and there is lots of sample code out there that shows how to do this. However I would still like to see some code that can do this with the swap chain.
@PerfWise Hi, can you let me know when you see this message. I'd interested in your posts on the Intel forum.
0

the first requirement to create a shader resource view from a texture is to create the texture with the good flags. The flag is D3D11_BIND_SHADER_RESOURCE. It has to be set in the decription binding member before calling CreateTexture2D.

If the render target is not create by you, but from the swap chain, you cannot bind it as an SRV. You will need an intermediate frame buffer and copy it to the back buffer at the end of the frame.

2 Comments

In your response, did you actually mean to say "...copy the back buffer to it..."? What I need is to have the compute shader read the back buffer after it has been rendered by the pixel shader. It is a big penalty to have to copy the back buffer, but of course I will if I have to.
Forget what i said, just add the DXGI_USAGE_SHADER_INPUT to the buffer usage member of the swap chain description and you will be able to create a shader resource view of the backbuffer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.