简体   繁体   English

着色器中的DirectX着色器资源视图

[英]DirectX Shader Resource View in Shaders

I'm a bit confused right now and just want to ask you all to help me get a few ideas clarfied. 我现在有点困惑,只想请大家帮我澄清一些想法。

In a HLSL shader (compute shader for example) I can declare a StructuredBuffer sb, right? 在HLSL着色器中(例如,计算着色器),我可以声明一个StructuredBuffer sb,对吗? Do I HAVE to bind it to a register, such as : register(t0)? 我是否必须将其绑定到寄存器,例如:register(t0)?

From the application side, I can call CSSetShaderResources(...). 从应用程序方面,我可以调用CSSetShaderResources(...)。 The first argument (the StartSlot), does it have anything to do with the little number behind the "t" in the register declaration? 第一个参数(StartSlot),与寄存器声明中“ t”后面的小数字有关系吗?

If I set the StartSlot as 0 (for example), and I set the second argument as 2. Am I telling the API that I'll binding two Shader Resource Views, one in register(t0) and another in register(t1)? 如果我将StartSlot设置为0(例如),并且将第二个参数设置为2,我是否告诉API我将绑定两个Shader Resource View,一个在register(t0)中,另一个在register(t1)中?

If I declare Texture2D tex[10] : register(t0) I can set it by calling CSSetShaderResources(0, 10, ...). 如果我声明Texture2D tex [10]:register(t0),则可以通过调用CSSetShaderResources(0,10,...)进行设置。 Does this mean that registers(t0~t9) are all used up? 这是否意味着寄存器(t0〜t9)全部用完了?

Sorry for such a "rapid fire" of questions, but I'm just really confused and some of my tests seem to give confliting results... 对这样的“快速反应”感到抱歉,但是我真的很困惑,我的一些测试似乎给出了令人困惑的结果...

Any help would be appreciated. 任何帮助,将不胜感激。

So let's reply in order: 因此,让我们按顺序回复:

Yes you can of course declare a StructuredBuffer in a compute shader (actually you can declare it for any type of shader). 是的,您当然可以在计算着色器中声明一个StructuredBuffer(实际上您可以为任何类型的着色器声明它)。

If you don't use effect framework (techniques), you need to declare a register, so the shader will know where to read from your data (using effect framework it just does it under the hood, but you still can explicitely declare). 如果您不使用效果框架(技术),则需要声明一个寄存器,以便着色器将知道从数据中读取的位置(使用效果框架,它只是在后台进行,但是您仍然可以显式声明)。

CSSetShaderResources tells to bind N resources from a start slot, so your description of using 0,2 is correct. CSSetShaderResources告诉您从开始位置绑定N个资源,因此您对使用0,2的描述是正确的。

For array of textures, I had to run PIX to check it out, but it's indeed the way you said. 对于纹理阵列,我必须运行PIX进行检查,但这确实是您所说的。

Texture2D tex[10] : register(t0); 

Will mean that each texture index will be allocated a slot starting from the register you specified, so you need to call CSSetShaderResources(0,10,srvarray) to set them. 这意味着将从您指定的寄存器开始为每个纹理索引分配一个插槽,因此您需要调用CSSetShaderResources(0,10,srvarray)进行设置。

Very cool explanation! 很酷的解释! I also got confused, and after your questions and explanations it's clear for me! 我也很困惑,在您提出问题和解释之后,这对我来说很清楚!

But I found a good example for this post, which I want to share. 但是我发现了一个很好的例子,我想分享。 It seems it starts the counter of the slot for every SetShaderResources Type. 似乎它为每个SetShaderResources类型启动了插槽的计数器。 All shaders (VS, HS, DS, PS) seems to have theirs own counter. 所有着色器(VS,HS,DS,PS)似乎都有自己的计数器。 Here the code from a NVidia example: 这是NVidia示例中的代码:

The Shaderclass code: Shaderclass代码:

pd3dDeviceContext->HSSetShaderResources( 0, 2, Resources ); 
pd3dDeviceContext->HSSetShaderResources( 8, 1, &m_pRegularWatertightTexSRV );
pd3dDeviceContext->HSSetShaderResources( 9, 1, &m_pQuadWatertightTexSRV );
pd3dDeviceContext->HSSetShaderResources( 2, 1, &pHeightMapTextureSRV );
pd3dDeviceContext->DSSetShaderResources( 2, 1, &pHeightMapTextureSRV );
pd3dDeviceContext->PSSetShaderResources( 2, 1, &pHeightMapTextureSRV );
pd3dDeviceContext->PSSetShaderResources( 10, 1, &pNormalMapTextureSRV );
pd3dDeviceContext->PSSetShaderResources( 3, 1, &pTexRenderRV11 );

The first is holding two resources, so the next slot (line 4) has to add 2 for the starting slot (0+2=2). 第一个保留两个资源,因此下一个插槽(第4行)必须为起始插槽添加2(0 + 2 = 2)。 Every SetShaderResources has to start with 0, but you can do that on different places in your code, therefore there is no 0 slot for DS and PS here. 每个SetShaderResources必须以0开头,但是您可以在代码的不同位置执行此操作,因此,此处的DS和PS没有0插槽。 Some times if you remove a line it still works but the data is postponed. 有时,如果删除一行,它仍然可以工作,但是数据被推迟。 Now you see the first four in HLSL at line t0, t1, t8, and t9 the other register were bound somewhere else. 现在,您在HLSL的t0,t1,t8和t9行看到了前四个,另一个寄存器被绑定到其他位置。

The HLSL code: HLSL代码:

Texture2D<float> GregoryStencil               : register( t0 ); 
Texture2D<uint>  Index                        : register( t1 );    
Texture2D<float>  g_txHeight                  : register( t2 );       
Texture2D<float> g_depth                      : register( t3 ); 
Texture2D g_FloorTexture                      : register( t4 );  
Texture2D<float3>  regularPatchControlPoints  : register( t5 ); 
Texture2D<float3>  gregoryPatchControlPoints  : register( t6 ); 
Texture2D<float4>  g_floorHeight              : register( t7 );   
Texture2D<float2>  RegularWatertightUVs       : register( t8 );  
Texture2D<float2>  QuadWatertightUVs          : register( t9 );  
Texture2D<float3>  g_txNormal                 : register( t10 );

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

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