对缓冲区资源使用无绑定描述符有意义吗



我正在directx12项目中尝试无绑定hlsl资源绑定,我可以理解为什么无绑定对绑定纹理有用,因为您可以执行以下操作:

DescriptorTable(
SRV(t0, space=0, numDescriptors=unbounded, offset=0),
SRV(t0, space=1, numDescriptors=unbounded, offset=),
SRV(t0, space=2, numDescriptors=unbounded, offset=0)
visibility=SHADER_VISIBLITY_ALL)
Texture2D textures2d[8192] : register(t0, space0);
Texture3D textures3d[8192] : register(t0, space1);
TextureCube texturesCube[8192] : register(t0, space2);

但是绑定缓冲区资源(如StructuredBuffers(又如何呢?由于它们需要有一个关联的类型,例如StructuredBuffer<some_struct>,您将如何在HLSL中声明它们?例如,假设您有许多不同类型的StructuredBuffer,您是否必须将它们分别绑定到一个单独的空间?

struct point_light
{
float3 position_ws;
float falloff_start;
float3 color;
float falloff_end;
float3 strenght;
uint id;
};
RWStructuredBuffer<point_light> sb_point_lights : register(u0, space0);
struct spot_light
{
float3 position_ws;
float falloff_start;
float3 color;
float falloff_end;
float3 strenght;
float spot_power;
float3 direction;
uint id;
};
StructuredBuffer<spot_light> sb_spot_lights : register(u0, space1);

或者有没有类似的方法(不起作用(:

struct point_light
{
float3 position_ws;
float falloff_start;
float3 color;
float falloff_end;
float3 strenght;
uint id;
};
RWStructuredBuffer<point_light> sb_point_lights : register(u0, space0);
struct spot_light
{
float3 position_ws;
float falloff_start;
float3 color;
float falloff_end;
float3 strenght;
float spot_power;
float3 direction;
uint id;
};
StructuredBuffer<spot_light> sb_spot_lights : register(u0 + offset_to_spotlights, space0);

如果您需要在相同类型的缓冲区之间切换,动态索引确实很有用,但正如您所提到的,您需要声明相同类型的资源(也就是说,它与纹理相同,例如,我有类似Texture2d<uint>的常见用例,因此它不限于StructuredBuffer(。

所以,是的,您将不得不为每个类型化缓冲区声明一个不同的数组,这不是非常方便。

如果您想对资源进行完全动态访问(无论类型如何(,Shader Model 6.6引入了对堆的直接访问(代码名:终极无绑定;(

因此,您有一个名为ResourceDescriptorHeap的全局变量(表示当前绑定的资源堆(

和SamplerDescriptorHeap(相同,但用于采样器(。

现在你可以做(例如(:

int textureLocationInHeap =5;
int bufferLocationInHeap = 6;

Texture2D myTexture = ResourceDescriptorHeap[textureLocationInHeap];
StructuredBuffer<float> myBuffer = ResourceDescriptorHeap[bufferLocationInHeap ];

然后,您可以使用根常量或常量缓冲区(根据您的喜好(传递textureLocationInHeap和bufferLocationInHeap。

您不再需要传递描述符表(当然,您仍然可以使用表/"直接访问"的混合

当然,请注意,如果您索引的资源类型不正确(或者您有一个空堆槽(,您将面临未定义的结果或驱动程序崩溃(第二个更可能(。

有关动态资源绑定的更多信息,请点击此处:

https://devblogs.microsoft.com/directx/in-the-works-hlsl-shader-model-6-6/

最新更新