In a book I'm reading I learned that we use constant buffers in directX to minise the number of API calls needed to do state changes, which leads to better performance.
However, then I see the book using it like this:
The C++ application code typically needs to communicate with the effect; in particular, the C++ application usually needs to update variables in the constant buffers. For example, suppose that we had the following constant buffer defined in an effect file:
Through the ID3D10Effect interface, we can obtain pointers to the variables in the constant buffer:
fxWVPVar = mFX->GetVariableByName("gWVP")->AsMatrix();
fxColorVar = mFX->GetVariableByName("gColor")->AsVector();
fxSizeVar = mFX->GetVariableByName("gSize")->AsScalar();
fxIndexVar = mFX->GetVariableByName("gIndex")->AsScalar();
fxOptionOnVar = mFX->GetVariableByName("gOptionOn")->AsScalar();
The ID3D10Effect::GetVariableByName method returns a pointer of type ID3D10EffectVariable. This is a generic effect variable type; to obtain a pointer to the specialized type (e.g., matrix, vector, scalar), you must use the appropriate As***** method (e.g., AsMatrix, AsVector, AsScalar).
Once we have pointers to the variables, we can update them through the C++ interface. Here are some examples:
fxWVPVar->SetMatrix( (float*)&M ); // assume M is of type D3DXMATRIX
fxColorVar->SetFloatVector( (float*)&v ); // assume v is of type
fxSizeVar->>SetFloat( 5.0f );
fxIndexVar->SetInt( 77 );
fxOptionOnVar->SetBool( true );
But now they make separate calls, is this not beating the purpose of constant buffers?
If it isn't, then why not?
If it is, then how do I set it correctly? (I assume this has something to do with "ID3D10EffectConstantBuffer::SetConstantBuffer()" but it seems to be poorly documented)