[D3D12] Array of cbuffers?

Started by
3 comments, last by Hodgman 8 years, 7 months ago

I'm trying to use an array of cbuffers for instanced rendering. In the shader I set up an array of two structs, like so:


struct MVPBuffer
{
   float4x4 MVP;
};
ConstantBuffer<MVPBuffer> Matrices[2] : register(b0);

For creating the root signature, I do the following:


cbufferRange = CD3DX12_DESCRIPTOR_RANGE(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 2, 0, 0);
rootParams[0].InitAsDescriptorTable(1, &cbufferRange, D3D12_SHADER_VISIBILITY_ALL);

And when I create the constant buffer view, I tell it to use (sizeof(MVPBuffer) * 2). But when I render my mesh with two instances, and try to index the Matrices array with a variable assigned to SV_InstanceID, I only see the first mesh.

Advertisement

Are you sure you want an array of two constant buffers and not just a single constant buffer with an array of two matrices?

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

Eventually I want to declare it as an unbounded array. I didn't think I could do that inside the struct.

You can't, but you should really stick to a StructuredBuffer for what you're trying to do.

A wave/warp of threads is almost certainly going to access your array in a divergent manner and that's not a good thing to do with a constant buffer on some GPUs. Trying to access an array of constant buffers using a feature of Shader Model 5.1 is taking divergence to a whole new level and will not run well at all.

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

And when I create the constant buffer view, I tell it to use (sizeof(MVPBuffer) * 2).

That's what you would do if you had a cbuffer that contained an array.

If you want an array of cbuffer like in your shader code, you'll need an array of constant buffer views with size=sizeof(MVPBuffer)

This topic is closed to new replies.

Advertisement