# D3D10 - How to set constant buffers for effects?

## Recommended Posts

Cycles    187
Hi, I'm having a bit of trouble getting constant buffers to work. I have grouped the variables together in my shader in a constant buffer, like so:
cbuffer Camera
{
matrix view, projection, world;
};

And I am using them like this:
VS_OUTPUT VS(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT) 0;

output.pos = mul(input.pos, world);
output.pos = mul(output.pos, view);
output.pos = mul(output.pos, projection);

return output;
}

This code works fine if I set variables by hand. By this, I mean retrieving each variable with ID3DEffect::GetVariableByName(...)::AsMatrix. Here's an example, to clarify:
world->SetMatrix(static_cast<float*>(geometry->GetTransformation().GetWorldMatrix()));
view->SetMatrix(static_cast<float*>(scene->GetCamera()->GetView()));
projection->SetMatrix(static_cast<float*>(scene->GetCamera()->GetProjection()));

However, when I try and use a constant buffer to set these variables, I get no output - so presumably the matrices are not being passed.
ID3D10EffectConstantBuffer *cameraCBuffer, *worldCBuffer;
cameraCBuffer = effect->GetConstantBufferByName("Camera");
bool valid = cameraCBuffer->IsValid(); // Is showing as true.

D3D10_BUFFER_DESC cameraBufferDesc;
cameraBufferDesc.ByteWidth = sizeof(CameraConstants);
cameraBufferDesc.Usage = D3D10_USAGE_DYNAMIC;
cameraBufferDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
cameraBufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
cameraBufferDesc.MiscFlags = 0;

CameraConstants cameraConstants;
cameraConstants.projection = scene->GetCamera()->GetProjection();
cameraConstants.view = scene->GetCamera()->GetView();
cameraConstants.world= geometry->GetTransformation().GetWorldMatrix();

ID3D10Buffer *cameraBuffer;
HRESULT hr = display->GetDevice()->CreateBuffer(&cameraBufferDesc, &cameraData, &cameraBuffer); // Returns S_OK
hr = cameraCBuffer->SetConstantBuffer(cameraBuffer); // Returns S_OK


I've check my hr's where they're returned and they are all S_OK. Also, there is nothing in the debug output. Any ideas?

##### Share on other sites
Cycles    187
While I haven't found a solution to make this work, I don't think it's actually necessary. I made a shader with 2 constant buffers, and updated variables in both buffers. Profiling in PIX automatically handles 2 VSSetConstantBuffers(...) calls, so presumably setting "just" by variable in ID3D10Effect handles this for you.

Brilliant! :)