Jump to content
  • Advertisement
Sign in to follow this  
poigwym

DX11 d3d11 Buffer data not correct

Recommended Posts

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..

Share this post


Link to post
Share on other sites
Advertisement

For UpdateSubresource the "left" and "right" members of the box should be offsets in bytes when updating a buffer resource, so the code that you posted for your setData function looks correct. What exactly do you mean when you say that "the data is not smooth"?

Share this post


Link to post
Share on other sites
5 hours ago, MJP said:

For UpdateSubresource the "left" and "right" members of the box should be offsets in bytes when updating a buffer resource, so the code that you posted for your setData function looks correct. What exactly do you mean when you say that "the data is not smooth"?

solved, there's nothing missed, thank you !!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!