[DX10] Can't get the constant buffer to update

Started by
4 comments, last by kauna 12 years, 1 month ago
Hey all, to be brief, I can't get the constant buffer in the shader to update.
Heres the code:


//decl
ID3D10Buffer* g_pConstantBuffer;

//definition
struct _SHADER_CONST_BUFFER
{
float fDelta;
};

//creation
D3D10_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = sizeof( _SHADER_CONST_BUFFER );
cbDesc.Usage = D3D10_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
HRESULT hr = pD3DDevice->CreateBuffer( &cbDesc, &InitData, &g_pConstantBuffer );
pD3DDevice->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer );

//updating
_SHADER_CONST_BUFFER* pConstData;
g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, ( void** )&pConstData );
pConstData->fDelta = gTimeDelta;
g_pConstantBuffer->Unmap();
pD3DDevice->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer );


This is the way it looks in the .fx file


cbuffer cb0
{
float fDelta;
};


I am given no errors during compilation or runtime. I don't show it here, but I tested all the functions' HRESULTs - no failures.I have the D3D error reporting enabled, but I'm not getting messages from there either.
It appears to have written the values, but when I read them from the shader they all show 0.0000, also a little animation in the texture is not playing (because this is the time delta I'm trying to set)

I have gone trough the directx samples and copied the code verbatim, so I don't know what the problem could be.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement
Just to be clear on intent, as someone made this mistake on here recently, you are aware that you are only binding the vertex shader CB, yes? That is where you expect to access the value and not any other shader stage, correct?
Not relevant here, but NULL isn't valid for the MapFlags arg - it should be 0.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Just to be clear on intent, as someone made this mistake on here recently, you are aware that you are only binding the vertex shader CB, yes? That is where you expect to access the value and not any other shader stage, correct?


I think this might be the cause. I'm referencing the timeDelta only in my pixel shader. What is strange is that not one of the sources told me anything about the buffer being limited to vs. Thanks, phantom. Does that mean I have to specify a second buffer to be used in the ps, or is just a limitation?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Hi!


Does that mean I have to specify a second buffer to be used in the ps, or is just a limitation?

You can bind the constant buffer to every shader stage you want, even at the same time. So there is no need to create a second buffer storing the same values.

This will bind the constant buffer to the VS and PS:
pD3DDevice->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer );
pD3DDevice->PSSetConstantBuffers( 0, 1, &g_pConstantBuffer );


Cheers!
Also, stated by the MSDN

[color=#2A2A2A]"If the bind flag is D3D11_BIND_CONSTANT_BUFFER then the ByteWidth[color=#2A2A2A] value must be in multiples of 16, and less than or equal to D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT."

[color=#2A2A2A]one float is only 4 bytes

[color=#2A2A2A]Cheers!

This topic is closed to new replies.

Advertisement