Step 1. Define
CB_BANDWIDTH* pcbBandwidth = NULL; //CB_BANDWIDTH is a structure for transmitting.
ID3D11Buffer* bandwidthBuffer = NULL;
ID3D11ShaderResourceView* bandwidthBuffer_RV = NULL;
Step 2. Set buffer and its shader resource view
D3D11_BUFFER_DESC Desc2;
Desc2.Usage = D3D11_USAGE_DYNAMIC;
Desc2.BindFlags = D3D11_BIND_SHADER_RESOURCE;
Desc2.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
Desc2.MiscFlags = 0;
Desc2.ByteWidth = sizeof(CB_BANDWIDTH);
V_RETURN(pd3dDevice->CreateBuffer(&Desc2, NULL, &bandwidthBuffer));
D3D11_SHADER_RESOURCE_VIEW_DESC rd; //never replace this thing into NULL.
ZeroMemory(&rd, sizeof(rd));
rd.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
rd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
rd.Buffer.FirstElement = 0;
rd.Buffer.NumElements = 7;
pd3dDevice->CreateShaderResourceView(bandwidthBuffer, &rd, &bandwidthBuffer_RV);
Step 3. Fill the data
//Set constant buffer
D3D11_MAPPED_SUBRESOURCE MappedResource;
pd3dImmediateContext->Map(bandwidthBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
pcbBandwidth = (CB_BANDWIDTH*)MappedResource.pData; //CB_BANDWIDTH is a structure for transmitting.
pcbBandwidth.data = somedata;
Step 4. Set shader resource and unmap
pd3dImmediateContext->PSSetShaderResources(77, 1, &bandwidthBuffer_RV); // 77 is a register number. you can set in the shader.
// In this example, tbuffer is set to only for pixel shader,
pd3dImmediateContext->Unmap(bandwidthBuffer, 0);
Step 5. Define the CB_BANDWIDTH in the hlsl shader code with 77 register.
tbuffer CB_BANDWIDTH : register(t77)
{
Bandwidth bandwidth; //must be synchronized with definition in the cpp code.
};
Note that, the detailed description of CB_BANDWIDTH and Bandwidth structure is omitted. They can be replaced by simply float4.