Unity的
Dispatch
和DispatchIndirect
有什么区别?
我多次尝试了解这一点,但都失败了。
我认为
DispatchIndirect
会导致着色器同时运行多次。
但事实并非如此。我现在真的不知道。
ps。下面的代码现在无法运行。
#pragma kernel CSMain
struct IndirectData
{
uint numGroupX;
uint numGroupY;
uint numGroupZ;
uint texIndex;
};
RWTexture2D<float4> renderTexture;
StructuredBuffer<Texture2D> texBuffer;
StructuredBuffer<IndirectData> dispatchData;
[numthreads(8, 8, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// some code...
IndirectData data = dispatchData[**ShaderIndex**]; //I wanted this index value...
renderTexture[id.xy] = texBuffer[data.texIndex];
// some code...
}
C# 代码
// some code...
struct IndirectData
{
public uint numGroupX;
public uint numGroupY;
public uint numGroupZ;
public uint texIndex;
// some code...
};
// some code...
IndirectData[] data = new IndirectData[orderCount];
void SomeFunc(out data);
// some code...
ComputeBuffer buffer = new ComputeBuffer(orderCount, Marshal.SizeOf(typeof(IndirectData)), ComputeBufferType.IndirectArguments);
buffer.SetData(data);
shader.SetBuffer(kernelID, rectID, buffer);
shader.DispatchIndirect(kernelID, buffer, 0);
// some code...
回答标题中的问题,根据我的经验,以下
ID3D11DeviceContext::Dispatch
电话,
context->Dispatch(x, y, z);
相当于使用以
DispatchIndirect
为内容的缓冲区调用 {x,y,z}
:
D3D11_BUFFER_DESC desc{};
desc.ByteWidth = sizeof(unsigned) * 3;
desc.MiscFlags = D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS;
ID3D11Buffer* argbuf;
unsigned dim[] = { x, y, z };
D3D11_SUBRESOURCE_DATA argdata{};
argdata.pSysMem = dim;
device->CreateBuffer(&desc, &argdata, &argbuf);
context->DispatchIndirect(argbuf, 0);