1
\$\begingroup\$

In hlsl, we always see this

cbuffer cbPerFrame 
{
    float2 gRasterSize;
    float4x4 gView;
    int gVoxelDim;
    float3 gVoxelSize;
};

cbuffer cbPerObject 
{
    float gObjectID;
    float4x4 gWorld;
};

I know that the group cbPerFrame is to update variables every frame.
The other group cbPerObject is to update variables per object per frame, for example 2 times per frame in this case.
But how can hlsl know that? cbPerFrame and cbPerObject are just names but not semantics.
When we define different groups, does hlsl really know how many times this group should update every frame?

\$\endgroup\$
1
  • \$\begingroup\$ You update it on the CPU and then pass it to the GPU. At the start of the frame you compute your cameraCB then just pass it to each shader that need that CB then for each object you draw you update the ObjectCB and pass that to shaders that need it. Because only the objectCB changes mid frame there is no need to update the cameraCB every time you pass it to a shader. \$\endgroup\$ Feb 22, 2017 at 1:46

1 Answer 1

1
\$\begingroup\$

HLSL does not know about this, the naming is just for the programmer. But the separation in multiple buffers achieves an important thing:

It saves memory bandwidth

If you put all constant data into a single constant buffer you have to transfer the complete data every time you want to update a part of the data. In this case this means you'd have to send your per-Frame data again everytime you update your per-object data. This is a waste of memory bandwidth, which is a very limited ressource.

As you are updating the constant buffers manually via Direct3D (ID3D11DeviceContext::Map, ID3D11DeviceContext::Unmap) you have complete control over when (and why) constant buffers are updated. HLSL has now way to know when constant buffers are updated, nor does it need to. Your should use your control over constant buffers to optimize your memory bandwidth usage as much as possible.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .