1
\$\begingroup\$

I have some shaders, every shader has the same constant buffer,

//-----------------------
//constant buffer
//---------------------
cbuffer cbPerFrame : register(b0)
{
    float3 gEyePosW;
    float4x4 gView;
    float4x4 gProj;
    float gDim;
    float3 gVoxelOffset;
    float gVoxelSize;
};

I think it's a waste to send these same constant buffer several times, so I want to pack them in one Constant Buffer, and share them to other vertex shaders.

I have tried send constant buffer to compute shader, but they don't work in vertex shader.

Do I need to pack these data in one structured buffer? Is it faster than send constant buffer several times?

Is deferred shading relevant to this problem? Which solution do we usually use to solve this problem?

\$\endgroup\$
1
\$\begingroup\$

The whole purpose of the Direct3D 10/11 'constant buffer' model is to let you share them with many shaders. You only pay to transfer once per CB per change of the data, so reusing a CB between shaders is actually a win if you don't have to change the data.

Generally, the recommendation is to organize your data into CBs by "frequency of update" since the real cost is updating any element in a CB resends the whole CB.

cbuffer cbNeverChanges : register( b0 )
{
    matrix View;
};

cbuffer cbChangeOnResize : register( b1 )
{
    matrix Projection;
};

cbuffer cbChangesEveryFrame : register( b2 )
{
    matrix World;
    float4 vMeshColor;
};

Note with Windows 8 or later, drivers can opt to support Direct3D 11.1's partial CB update and offset CB update features which let's you implement alternative CB update strategies.

DirectCompute shaders can only output to UAVs. I think you can create a ID3D11Buffer with both BIND_UAV and BIND_CB properties, but unless you are processing a lot of data to generate the output it probably doesn't make any sense to do it that way.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.