Hi, I want to implement a StructuredBuffer in my shader so I can pass in an undefined number of lights. From my research I have decided that the best way to do this is to use a StructuredBuffer. However, I cannot find any good tutorials that document how to use it. Can anyone point me in the right direction? Currently, I am having trouble initializing the buffer in my C++ shader code; I am unsure about some of the parameters such as the StructureByteStride. Here is my current code which gives an error when calling CreateBuffer:
D3D11_BUFFER_DESC sbDesc;
sbDesc.ByteWidth = sizeof(CLight);
sbDesc.Usage = D3D11_USAGE_DYNAMIC;
sbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
sbDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
sbDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
sbDesc.StructureByteStride = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
D3D11_SUBRESOURCE_DATA subResourceData2;
subResourceData2.pSysMem = (void*)_lights.data();
subResourceData2.SysMemPitch = 0;
subResourceData2.SysMemSlicePitch = 0;
result = device->CreateBuffer(&sbDesc, &subResourceData2, &m_lightBuffer2);
if(FAILED(result)){
cout << "error" << endl;
return false;
}
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = DXGI_FORMAT_UNKNOWN;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
srvDesc.Buffer.FirstElement = 0;
srvDesc.Buffer.NumElements = _lights.size();
result = device->CreateShaderResourceView(m_lightBuffer2.Get(), &srvDesc, &m_pSRV);
if (FAILED(result)) {
cout << "error" << endl;
return false;
}
And later to set the resource:
D3D11_MAPPED_SUBRESOURCE mappedResource2;
result = deviceContext->Map(m_lightBuffer2.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource2);
if (FAILED(result))
{
cout << "error" << endl;
return false;
}
size_t sizeInBytes = _lights.size();
memcpy_s(mappedResource2.pData, sizeInBytes, _lights.data(), sizeInBytes);
deviceContext->Unmap(m_lightBuffer2.Get(), 0);
deviceContext->PSSetShaderResources(7, 1, &m_pSRV);
I am using ComPtr's for the buffer and the shader resource view:
Microsoft::WRL::ComPtr<ID3D11Buffer> m_lightBuffer2;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pSRV;