hi i am learning directx11 recently. and i have problem in using constant buffer.
so what i am doing is, i create the constant buffer for directional light and update the value, so that i can use this in my shader.
constant buffer structure in application side
struct SHADER_DIRECTIONAL_LIGHT {
XMFLOAT4 ambient;
XMFLOAT4 diffuse;
XMFLOAT4 specular;
XMFLOAT4 dir;
XMFLOAT4 enabled;
XMFLOAT4 intensity;
};
class DirectionalLight
{
private:
//1. member variable
SHADER_DIRECTIONAL_LIGHT m_data;
//2. static variable
//static SHADER_DIRECTIONAL_LIGHT m_data;
//3. allocated variable
//SHADER_DIRECTIONAL_LIGHT* m_data;
}
constant buffer structure in shader side
cbuffer DIRECTIONAL_LIGHT : register(b0)
{
float4 d_Ambient;
float4 d_Diffuse;
float4 d_Specular;
float4 d_Dir;
float4 d_Enabled;
float4 d_intensity;
};
how i update constant buffer
//of course, edit `m_data` before using map
D3D11_MAPPED_SUBRESOURCE mappedData;
ZeroMemory(&mappedData, sizeof(D3D11_MAPPED_SUBRESOURCE));
HRESULT hr = dContext->Map(m_cb, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);
r_assert(hr);
CopyMemory(mappedData.pData, &m_data, sizeof(SHADER_DIRECTIONAL_LIGHT));
dContext->Unmap(m_cb, 0);
dContext->PSSetConstantBuffers(SHADER_REG_CB_DIRECTIONAL_LIGHT, 1, &m_cb);
and the problem is that
when creating m_data
like SHADER_DIRECTIONAL_LIGHT m_data;
or static SHADER_DIRECTIONAL_LIGHT m_data;
,
it works fine and the value i updated into the constant buffer using map
properly applied into shader side too.
But when i create m_data
like SHADER_DIRECTIONAL_LIGHT* m_data
, the value i update doesn't really work. the value in shader side is just uninitialized random value.
by the debug, i am just guessing the problem is from the different memory space of variable that i use to update the constant buffer. if i use variable in stack, i successfully update the constant bufferm, and when using variable from heap, it doesn't.
hope somebody to clarify what actually problem here. thanks