0

I am exporing Nvidia's Flex but I am very new to D3D. I am trying to copy between two ID3D11Buffer which are created using different D3D11_BUFFER_DESC. I wanted to use ID3D11DeviceContext::CopyResource() since it's GPU-GPU copy but the code is throwing exception.

Two buffers are created as below:

//First Buffer:
D3D11_BUFFER_DESC bufDesc;
bufDesc.ByteWidth = numParticles*sizeof(Vec4);
bufDesc.Usage = D3D11_USAGE_DYNAMIC;
bufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bufDesc.MiscFlags = 0;
bufDesc.StructureByteStride = 0;
m_device->CreateBuffer(&bufDesc, NULL, buff0);

//Second Buffer
D3D11_BUFFER_DESC bufDescTemp;
bufDescTemp.ByteWidth = numParticles * sizeof(Vec4);
bufDescTemp.Usage =  D3D11_USAGE_DEFAULT;
bufDescTemp.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufDescTemp.CPUAccessFlags = 
D3D11_CPU_ACCESS_READ;//D3D11_CPU_ACCESS_READ;// 
bufDescTemp.MiscFlags = 0;
bufDescTemp.StructureByteStride = 0;
m_device->CreateBuffer(&bufDescTemp, NULL, buff1);

Then at each frame I get an updated buff0 from Flex's NvFlexGet. Then I try to copy buff0 to buff1:

context->CopyResource(buff1,buff0);

But this line throws exception. It works fine if both buffers are created using D3D11_USAGE_DYNAMIC. But not when buff1 is D3D11_USAGE_DEFAULT.

Does ID3D11DeviceContext::CopyResource supports copy between buffers with different D3D11_USAGE? Or did I miss something?

Amir
  • 10,600
  • 9
  • 48
  • 75
  • What type is ``buff0`` and ``buff1``? the problem is likely that you have invalid buffers created here. Try ``m_device->CreateBuffer(&bufDesc, NULL, &buff0);`` and ``m_device->CreateBuffer(&bufDescTemp, NULL, &buff1);``. Also, you need to check the return values of all functions that return an ``HRESULT`` for failure/success. Consider using [ThrowIfFailed](https://github.com/Microsoft/DirectXTK/wiki/ThrowIfFailed). – Chuck Walbourn Dec 12 '17 at 19:48
  • If you had enabled the [debug device](https://msdn.microsoft.com/en-us/library/windows/desktop/ff476881.aspx) you'd have gotten a nice explanation of the problem in the output window as well. – Chuck Walbourn Dec 12 '17 at 19:54
  • Thanks a lot! Turns out there was error creating the second buffer. D3D11_CPU_ACCESS_READ cannot be used with D3D11_USAGE_DEFAULT. – user9085913 Dec 13 '17 at 15:20

1 Answers1

0

D3D11_USAGE_DYNAMIC means a resource that is accessible by both the GPU (read only) and the CPU (write only). That's why it cannot be used in CopyResource.

VuVirt
  • 1,887
  • 11
  • 13
  • Thanks for answering! _CopyResource_ works when both buffer are _D3D11_USAGE_DYNAMIC_. I am confused about that too since it is GPU read only. Also, if I use _CopyResource_ to copy from _D3D11_USAGE_DYNAMIC_ to _D3D11_USAGE_DEFAULT_, does it mean that GPU read from _DYNAMIC_ and write to _DEFAUT_? If that's the case, then why would there a problem using CopyResource? – user9085913 Dec 12 '17 at 15:30
  • Actually, the CopyResource documentation says that it cannot use Immutable resource as destination. So it seems that Dynamic is valid. However, I suppose that since CopyResource is an asynchronous call, then it cannot copy from Dynamic to Default. If you use DX Debug Device you might be able to get details about the exception thrown. – VuVirt Dec 12 '17 at 15:35
  • Thanks a lot! Turns out there was error creating the second buffer. D3D11_CPU_ACCESS_READ cannot be used with D3D11_USAGE_DEFAULT. – user9085913 Dec 13 '17 at 15:21