I'm having some problems when passing multiple light structures to a shader stage. I'm using an array of point lights that is updated each frame.
This is my code for creating the buffer
D3D11_BUFFER_DESC lbDesc;
lbDesc.ByteWidth = sizeof( PointLightData ) * NUM_LIGHTS;
lbDesc.Usage = D3D11_USAGE_DYNAMIC;
lbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
lbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
lbDesc.MiscFlags = 0;
lbDesc.StructureByteStride = 0;
hr = mDevice->CreateBuffer( &lbDesc, nullptr, &mLightBuffer );
And this is how I update it
D3D11_MAPPED_SUBRESOURCE mappedResource;
hr = mDeviceContext->Map( mLightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
if( SUCCEEDED( hr ) )
{
memcpy( mappedResource.pData, &mPointLightData[0], sizeof(PointLightData) * NUM_LIGHTS );
mDeviceContext->Unmap( mLightBuffer, 0 );
}
mPointLightData
is declared like this
mPointLightData = new PointLightData[NUM_LIGHTS];
In my shader I declare the light
struct PointLight
{
float4 positionAndRadius;
float4 ambient;
float4 diffuse;
float4 specular;
float3 attenuation;
};
cbuffer CB_LIGHT : register(b1)
{
PointLight pointLight[2];
};
I can only access the first light object in the buffer successfully. Rendering the second light results in a pitch black scene.
However, if I change the starting address at the mapping stage from &mPointLightData[0]
to &mPointLightData[1]
the second light works but obviously not the first.
So my guess is that I'm doing simething wrong at either the mapping stage or at the creation of the buffer..
Any suggestions? Thank you!