DiligentGraphics / DiligentEngine

A modern cross-platform low-level graphics library and rendering framework
http://diligentgraphics.com/diligent-engine/
Apache License 2.0
3.37k stars 318 forks source link

Incorrect index buffer view in D3D12 backend when using dynamic usage buffers #236

Closed chelvisgit closed 1 year ago

chelvisgit commented 1 year ago

With D3D12, mapping and unmapping a vertex & index buffer conveys to a memory overwrite: Checking the Index buffer content with RenderDoc, buffer appears to have some floats instead the valid indexes (uint32) on certain parts. It does not happens with D3D11 backend (same code).

Digging into the Diligent code we have seen that there is a huge memory array allocated to do the mapping cache. The problem is that while the returned pointer by the MapBuffer() points to the correct place, using that same buffer with setIndexBuffer() shows in RenderDoc the contents of the dynamic page instead of the section allocated to the index buffer.

Diagram: Clipboard - March 23, 2023 8_12 AM

This is at least in theory what I think is going on. Could you provide me with any tips to help investigate the problem further?

System: Windows 10 Pro 22H2 Visual Studio 2019 Release x64 NVIDIA GTX 1080 Driver 531.14 AMD Ryzen 9 3900X 12 - 3.80GHz

TheMostDiligent commented 1 year ago

The contents of the dynamic buffer is valid during a single frame only - it is not preserved between frames. If you run in Debug mode, the engine will print the errors if you try to use a buffer that was not mapped in the frame.

Please take a look at Tutorial 11 - it describes different resource usage/update strategies and tradeoffs.

chelvisgit commented 1 year ago

The contents of the dynamic buffer is valid during a single frame only - it is not preserved between frames. If you run in Debug mode, the engine will print the errors if you try to use a buffer that was not mapped in the frame.

Please take a look at Tutorial 11 - it describes different resource usage/update strategies and tradeoffs.

Thanks, we've found a solution by using the UpdateBuffer() function instead mapping/unmaping the buffers. Now it's working properly in a similar way than the tutorial 11.

Besides, looks like that texture creation with USAGE_DEFAULT rather than USAGE_DYNAMIC makes the difference.

TheMostDiligent commented 1 year ago

Dynamic textures is not really a thing. If you need to upload textures a lot, you should likely create staging texture, update it and then copy to USAGE_DEFAULT texture

chelvisgit commented 1 year ago

Dynamic textures is not really a thing. If you need to upload textures a lot, you should likely create staging texture, update it and then copy to USAGE_DEFAULT texture

*buffer creation with USAGE_DEFAULT I wanted to say btw, thanks for the texture tip. Probably I'll need it in the future.

TheMostDiligent commented 1 year ago

Sounds good. Feel free to close the issue if it is resolved. Note that there is also a discord channel for discussions.