[DX11] Constant buffer layout

Started by
4 comments, last by Aqua Costa 12 years, 7 months ago
I use Shader reflection to get the layout of each constant buffer used by each shader and the variable types/descs contained in it. So when I load a shader I create an array of constant buffer layouts (each layout containing an array of variables info). Then when I want to apply a certain material to an object I have to iterate through the array of variables types/descs and the parameters contained in the material object and find matches in order to update the constant buffers... Like this:
[source]
struct ShaderVariable
{
D3D11_SHADER_VARIABLE_DESC desc;
D3D11_SHADER_TYPE_DESC typeDesc;
};

struct ConstantBufferLayout
{
D3D11_SHADER_BUFFER_DESC desc;
ShaderVariable* variables;
};

//[...]

UINT m_NumConstantBuffers;
ConstantBufferLayout* m_ConstantBufferLayouts;

//[...]

for(int i = 0; i < m_NumConstantBuffers; i++)
{
for(int j = 0; j < m_ConstantBufferLayouts.desc.Variables; i++)
{
for(int k = 0; k < material.m_NumParamenters; k++)
{
if(m_ConstantBufferLayouts.variables[j].Name == material.m_Paramenters[k].Name)
{
material.addParameterToCBuffer(i, material.m_Paramenters[k];
}
}
}
}
[/source]

However searching for matches like this isn't as fast as I would like, and I also have to find matches between material shader resources and the shader resources needed by each shader. So does anyone has an idea how to fast is faster? Maybe a way to handle the parameters and the constant buffers layouts without have to find matches in the names each frame...
Advertisement
I just have 16 unnamed vectors for each shader and let the shaders use macro definitions to name them. Arg[0..15].xyzw
Some properties like color and transformation should just be static and named to make most shaders easy to use.
Then when I want to apply a certain material to an object I have to iterate through the array of variables types/descs and the parameters contained in the material object and find matches in order to update the constant buffers...
Instead of storing two copies of the data, and you could just store the one copy -- i.e. instead of storing values in a 'material' and copying them into a cbuffer, just store them in the cbuffer to begin with.

Can you explain what your "material" type is?

[quote name='TiagoCosta' timestamp='1315997663' post='4861490']Then when I want to apply a certain material to an object I have to iterate through the array of variables types/descs and the parameters contained in the material object and find matches in order to update the constant buffers...

Instead of storing two copies of the data, and you could just store the one copy -- i.e. instead of storing values in a 'material' and copying them into a cbuffer, just store them in the cbuffer to begin with.

Can you explain what your "material" type is?
[/quote]

The thing is that some values might change between frames, like world/view/projection matrix, camera position, and even some material-related values like diffuse color.

My "material" type is just a class containing an array of parameters (each parameter has a type (constant, texture), a name, and a value) and the shader ID used by the material
Keep those all separate. Stuff like view matrix or projection matrices only change once per frame, so have those in a constant buffer with other per-frame constants. Make 1 HLSL definition for that constant buffer and #include it in your shaders, and then you won't have to reflect every shader to get the memory layout. If you want, you can do similar stuff with stuff that changed per-draw (like a world matrix). Material parameters that never change should be in one constant buffer that you make when you create the material, so that you only have to set the values once when you load it. Dynamic material parameters can go in another constant buffer, or perhaps in a buffer with other per-draw constants.

Keep those all separate. Stuff like view matrix or projection matrices only change once per frame, so have those in a constant buffer with other per-frame constants. Make 1 HLSL definition for that constant buffer and #include it in your shaders, and then you won't have to reflect every shader to get the memory layout. If you want, you can do similar stuff with stuff that changed per-draw (like a world matrix). Material parameters that never change should be in one constant buffer that you make when you create the material, so that you only have to set the values once when you load it. Dynamic material parameters can go in another constant buffer, or perhaps in a buffer with other per-draw constants.


I have the constants sorted by update frequency in different constant buffers like you suggest.

However some shaders need view / projection / view-projection matrices and others might only need view matrix so I never know which matrices a shader might need.
Or could I register a constant buffer slot to hold a camera related data constant buffer and put all camera related data in it so every shader has access to it. :)

This topic is closed to new replies.

Advertisement