2
\$\begingroup\$

I've been learning the "dragon book" written by Frank D. Luna and trying to implement the part of the demo on my own. As some of you can see, the demo of the book uses effects interfaces to update the shader constant, but I'm not gonna do that. Because the effects may be deprecated in the future. So I try to find a convenient way to handle this shader constant stuff.

For example, I defined two shader constant structures in my C++ application:

struct PerObjConst : register(b0)
{
    XMMATRIX world;
    XMMATRIX worldViewProj;
    //...other vars
};

struct PerFrameConst : register(b1)
{
    float3 EyePos;
    //...other vars
};

Should I encapsulate every element's updating method for each? like:

SetShaderParameters(ID3D11DeviceContext *deviceContext, XMMATRIX &worldViewProj);
SetShaderParameters(ID3D11DeviceContext *deviceContext, XMMATRIX &world);
//...and so on

That seems to be cumbersome, isn't it? Instead, how about passing a whole structure's reference into the function? But I think that would be unnecessary to update whole structure when actually there is only a few of the elements are updated.

\$\endgroup\$
  • 1
    \$\begingroup\$ Just wanted to note that "The Dragon Book" refers almost universally to a book about compiler design. \$\endgroup\$ – SurvivalMachine Jul 9 '16 at 21:29
1
\$\begingroup\$

You should group your shader constants in buffers based on how often you are updating them, so it won't be inefficient to update the whole buffer.
For instance if you want to update the view matrix which is often done once per frame, you are most likely also updating the eye position of the camera, so they should be in the same buffer, so when you update it, you can update the whole thing.

\$\endgroup\$
  • 2
    \$\begingroup\$ The way CBs are designed in Direct3D 10 or later, you are intended to update the whole CB every time. There is a notion of 'partial updates and offsets' with DirectX 11.1 or later on Windows 8.x or Windows 10, but generally speaking you update the entire CB if you touch any part of it. \$\endgroup\$ – Chuck Walbourn Nov 2 '15 at 1:15

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.