🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Basic D3D12 question in context of Unity

Started by
1 comment, last by evelyn4you 2 years, 5 months ago

All this is taking place in context of a Unity Native Plugin but the question is general.

Is it possible to copy the contents of native mesh buffers on GPU.

Unity exposes native buffer pointers and I have no problem with using them in D3D11 however 12 refuses to cooperate.

Not only I can't copy the contents of the native mesh buffers, I can't seem to even copy a freshly created ComputeBuffer.

I do realize Unity 2021.2 exposes such functionality on the managed side unfortunately I need this in earlier versions.

Code goes as follows:

ID3D12Resource *pSrcBufferHandle = (ID3D12Resource*) srcBufferHandle;
ID3D12Resource *pDstComputeBufferHandle = (ID3D12Resource*) dstComputeBufferHandle;

pGraphicsCommandList->CopyResource(pDstComputeBufferHandle, pSrcBufferHandle);
pGraphicsCommandList->Close();

// states are supposed to be optional. In any case I'm not sure what 'expected' state 
// the buffers are in before executing the list.
fenceValue = iD3D12->ExecuteCommandList(pGraphicsCommandList, 0, NULL);

pCommandAllocator->Reset();
pGraphicsCommandList->Reset(pCommandAllocator, nullptr);

// If I try to wait for the fenceValue it simply hangs
// ID3D12Fence *fence = iD3D12->GetFrameFence();
// pFence->SetEventOnCompletion(fenceValue, sD3D12Event);

// WaitForSingleObject(sD3D12Event, INFINITE);









I was also trying to use ResourceBarrier() to change resource states before and after CopyResource() but because I don't know the ‘StateBefore’ of the buffers this had little chance of working.

Is there a way to peek the resource state?

I also tried to create my own fence, queue, and signal that queue in which case executing the list seems to set the expected value on the fence but the dest buffer is still full of zeros. No trace of the data src buffer was initialized with in Unity.

Advertisement

hello gregee123

i have worked with unity but not too much so i cant speak for the special implementation "things" of the unity API especially the “Native Plugin” side.

A. first of all, it is NOT needed to close, execute, reset the commandlist to apply your intention.
you simply add your commandlist commands and in the GPU timeline, which is totally different from the CPU timeline the copy commands will be executed.

B. to test your scenario, i assume that yout take care, that the ressource destination it NOT written in any other portion of your code.

C. you have to be carefull about using GraphicsCommandList or ComputeCommandList.

sometimes i had hours to search my bug yust because i copied a portion of a code that using GraphicsCommandList instead of Compute which was not configured correctly
at that portion of my code.

D. how do you exactly debug the copy command. I would propose
- use a “upload heap” buffer, fill it with well defined values, and test whether the upload map/unmap of your data is correct
- then second step readback your data from upload heap to CPU array and compare if reading is OK
- you can define the state of your source buffer to "default read" so you have a defined state
- then try to copy the values to the "upload heap" destination ressource and read back values and compare

E. imho it is most important to set the state of your destination ressource as copy destination, then copy, and after the copy set it back to e.g. “default read state”

hope this helps

This topic is closed to new replies.

Advertisement