The code to create d3d11 structured buffer :
D3D11_BUFFER_DESC desc;
desc.ByteWidth = _count * _structSize;
if (_type == StructType::Struct)
{
desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
}
else
{
desc.MiscFlags = 0;
}
desc.StructureByteStride = _structSize;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
if (_dynamic)
{
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
}
else
{
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.CPUAccessFlags = 0;
}
if (FAILED(getDevice()->CreateBuffer(&desc, NULL, &_object)))
{
return false;
}
D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
memset(&resourceViewDesc, 0, sizeof(resourceViewDesc));
if(_type == StructType::Float)
resourceViewDesc.Format = DXGI_FORMAT_R32_FLOAT;
else if (_type == StructType::Float2)
resourceViewDesc.Format = DXGI_FORMAT_R32G32_FLOAT;
else if (_type == StructType::Float3)
resourceViewDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT;
else if (_type == StructType::Float4)
resourceViewDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
else
resourceViewDesc.Format = DXGI_FORMAT_UNKNOWN;
resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
resourceViewDesc.Buffer.ElementOffset = 0;
resourceViewDesc.Buffer.NumElements = _count;
ID3D11Resource* viewObject = _object;
auto hr = getDevice()->CreateShaderResourceView(viewObject, &resourceViewDesc,
&_shaderResourceView);
if (FAILED(hr))
{
return false;
}
I've created a float type structured buffer.
The source data is a float array, I update the buffer from array[startIndex] to array[endIndex - 1].
The code to update buffer :
bool setData(int startIndex, int endIndex, const void* data)
{
if (!data)
return false;
D3D11_BOX destBox;
destBox.left = startIndex * _structSize;
destBox.right = endIndex * _structSize;
destBox.top = 0;
destBox.bottom = 1;
destBox.front = 0;
destBox.back = 1;
getContext()->UpdateSubresource(_object, 0, &destBox,
data,
_count * _structSize,
0);
}
The final result is that the data is not smooth, then I change the code of setData
destBox.left = startIndex ;
destBox.right = endIndex ;
Then, the result looks smooth, but with some data missed !!!!
Don't know why..