Sign in to follow this  
Cycles

D3D10 - How to set constant buffers for effects?

Recommended Posts

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();

D3D10_SUBRESOURCE_DATA cameraData;
cameraData.pSysMem = &cameraConstants;

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 this post


Link to post
Share on other sites
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! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this