4

I interested in both a code design and performance aspect if having a separated buffers when sending data to the GPU in HLSL, or another high-level shader language, is better.

This is where a particular shader needs to have a lot of variable data which changes during runtime and as such needs information to be passed by buffers.

I give an very basic example:

cbuffer SomeLargeBuffer : register(cb0)
{
    float3 data;
    float someData;
    float4 largeArray[2500];
    float moreData;
    ...
    ...
    ... 
    ...
    ...
}

or to have

cbuffer SamllerBuffer: register(cb0)
{
    float3 data;
    float someRelatedData;

}

cbuffer SecondSmallerBuffer : register(cb1)
{

    float4 largeArray[2500];
    float moreData;

}

cbuffer ThirdBuffer: register(cb2)
{
    ...
    ...
    ...
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
A. Yeats
  • 89
  • 2
  • 7

1 Answers1

5

In terms of efficiency, the documentation on shader constants in HLSL gives the following advice:

The best way to efficiently use constant buffers is to organize shader variables into constant buffers based on their frequency of update. This allows an application to minimize the bandwidth required for updating shader constants. For example, a shader might declare two constant buffers and organize the data in each based on their frequency of update: data that needs to be updated on a per-object basis (like a world matrix) is grouped into a constant buffer which could be updated for each object. This is separate from data that characterizes a scene and is therefore likely to be updated much less often (when the scene changes).

So, if you data updates at different rates, it would be best to group all data that are updated at the same frequency in the same constant buffers. Generally, data is either updated a) every frame, b) sporadically or c) never (once at startup). Reducing the number of total constant buffers also is helpful for performance, because it will reduce the number of binding calls, and required resource tracking.

In terms of code design, it's difficult to say, although usually it fits naturally the frequency-of-update pattern.

MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • 2
    The original Direct3D 10 design of constant buffers was that you'd update the entire CB each time you changed any part of it, so it's best to organize them by frequency of update to avoid uploading the same data over and over again. With DirectX 11.1 on Windows 8 or better with the optional feature supported, you can do partial constant buffer updates more efficiently for effects systems that work better with larger 'global style' CBs. – Chuck Walbourn Mar 06 '17 at 06:21