简体   繁体   English

使用Compute Shader处理渲染目标

[英]Use Compute Shader to Process the Render Target

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.) 我想将渲染目标用作DirectX 11中的Compute Shader的输入。我只需要读取Compute Shader中的Render Target,就我而言,我不必渲染Compute Shader输出(尽管我确实有以便稍后在CPU中使用它,并且可能需要在其他Compute Shader传递中对其进行处理。)

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. 我已经看到有评论认为这样做是可行的,但是我似乎找不到任何示例代码来显示如何在“着色器资源视图”中将渲染目标(Texture2D)用作计算着色器缓冲区。 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. 最好指向示例代码片段的指针。

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. 您的资源确实需要具有可通过计算着色器读取的着色器资源使用模式:-对于标准渲染目标:D3D11_BIND_SHADER_RESOURCE-对于交换链:交换链描述中的DXGI_USAGE_SHADER_INPUT。

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). 在这种情况下,标记为D3D11_BIND_UNORDERED_ACCESS或DXGI_USAGE_UNORDERED_ACCESS(请注意,这不适用于多采样纹理。)

Here is a very simple compute shader that reads samples from a texture from UV coords: 这是一个非常简单的计算着色器,可从UV坐标读取纹理中的样本:

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. 希望能有所帮助。

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 . 标志是D3D11_BIND_SHADER_RESOURCE It has to be set in the decription binding member before calling CreateTexture2D. 在调用CreateTexture2D之前,必须在解密绑定成员中设置它。

If the render target is not create by you, but from the swap chain, you cannot bind it as an SRV. 如果渲染目标不是您创建的,而是由交换链创建的,则您不能将其绑定为SRV。 You will need an intermediate frame buffer and copy it to the back buffer at the end of the frame. 您将需要一个中间帧缓冲区,并将其复制到帧末尾的后台缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM