I've tried everything I can think of, but I cannot get a constant buffer to update a variable in my shader in DirectX 11. I followed the examples on msdn. I read and re-read the articles on constant buffers. I even followed the nearly identical thread on this site. But for some reason I simply cannot get this to work. Can someone point me in the right direction?

Here's the relevant code:

//my data variable to keep everything together
    D3DXMATRIX mWorldViewProj;                              
    D3DXVECTOR4 vSomeVectorThatMayBeNeededByASpecificShader;
    float fSomeFloatThatMayBeNeededByASpecificShader;
    float fTime;                                            
    float fSomeFloatThatMayBeNeededByASpecificShader2;
    float fSomeFloatThatMayBeNeededByASpecificShader3;
    //I initialize my data variable
    D3DXMATRIX identity;
D3DXMatrixIdentity( &identity );
m_ConstantBufferContents.mWorldViewProj = identity;
m_ConstantBufferContents.vSomeVectorThatMayBeNeededByASpecificShader = D3DXVECTOR4(1,2,3,4);
m_ConstantBufferContents.fSomeFloatThatMayBeNeededByASpecificShader = 3.0f;
m_ConstantBufferContents.fTime = 1.0f;
m_ConstantBufferContents.fSomeFloatThatMayBeNeededByASpecificShader2 = 2.0f;
m_ConstantBufferContents.fSomeFloatThatMayBeNeededByASpecificShader3 = 4.0f;

// Fill in a buffer description.
cbDesc.ByteWidth = sizeof( VS_CONSTANT_BUFFER );
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.StructureByteStride = 0;

// Fill in the subresource data.
InitData.pSysMem = &m_ConstantBufferContents;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;

// Create the buffer.
HRESULT hr = pDevice->CreateBuffer( &cbDesc, &InitData, &m_ConstantBuffer );

    //from my update function, I update the contents of my buffer like so

m_ConstantBufferContents.mWorldViewProj = view;
// Set the buffer.
pDeviceContext->UpdateSubresource( m_ConstantBuffer, 0, 0, &m_ConstantBufferContents, 0, 0 );
pDeviceContext->VSSetConstantBuffers( 0, 1, &m_ConstantBuffer );

    //And this is how I attempt to use it in my shader
    // Constant Buffers
    cbuffer cbPerObject : register( b0 )
       matrix  g_mWorldViewProjection  : packoffset( c0 );
       matrix  g_mWorld                : packoffset( c4 );
       float4  g_MaterialAmbientColor  : packoffset( c8 );
       float4  g_MaterialDiffuseColor  : packoffset( c9 );

I'm sure it's something simple and stupid that I've overlooked, but I cannot seem to find the problem myself. Any ideas?

  • Goishin
  • \$\begingroup\$ If you have found an answer that helps you (as it sounds like Shaktal's has) you should mark it as the accepted answer to this question :) \$\endgroup\$ – user1430 Jul 30 '11 at 2:21

Why have you defined your cbuffer as reading in from register b0? That might be a potential cause of your problem (the device context is writing to a different register than the one your buffer is reading in from).

Also, before writing to your buffer, you should use the ID3D11DeviceContext::Map() function, as you need to prevent the GPU reading from it before you are ready to pass it. Try the following:

Instead of this:

m_ConstantBufferContents.mWorldViewProj = view;


D3D11_MAPPED_SUBRESOURCE mappedResource;
pDeviceContext->Map( &m_ConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource )

VS_CONSTANT_BUFFER* dataPtr = (VS_CONSTANT_BUFFER*)mappedResource.pData;

dataPtr->mWorldViewProj = view;

pDeviceContext->Unmap( m_ConstantBuffer, 0 );

I think that's the correct syntax (been away from DirectX for a while, gradually easing myself back into it :P), but the concept is to ensure that it doesn't read the information before you've written the new information to it. That also might be causing your issue. I also don't believe that you need to tell the shader the offsets of the elements within the buffer, that should be figured out when you bring in your shader for compilation.

Other than that I don't see any issues that could be causing your problem. But then, I'm currently an "untrained eye", so to speak, so if you still can't get it to work, then try looking at the tutorials from this guy's website, they seem to be fairly good, and provide a good starting ground to build off of (I find once I have a working concept down, building on top of it is much easier, and enjoyable).

Hope this helps! :)

| improve this answer | |
  • \$\begingroup\$ Hey, that worked! Things are moving on the screen!. So it's clear I'm getting data up to the shader now. This is great. Thanks a bunch, Shaktal! \$\endgroup\$ – Goishin Jul 29 '11 at 23:48

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.