microsoft / DirectX-Graphics-Samples

This repo contains the DirectX Graphics samples that demonstrate how to build graphics intensive applications on Windows.
MIT License
5.89k stars 2k forks source link

D3D12 ray tracing sample missing UAV barrier #789

Closed jeremyong closed 1 year ago

jeremyong commented 1 year ago

https://github.com/microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingMiniEngineSample/ModelViewer.cpp#L848

The code surrounding the link above looks like this:

    auto uavBarrier = CD3DX12_RESOURCE_BARRIER::UAV(nullptr);
    for (UINT i = 0; i < bottomLevelAccelerationStructureDescs.size(); i++)
    {
        pRaytracingCommandList->BuildRaytracingAccelerationStructure(&bottomLevelAccelerationStructureDescs[i], 0, nullptr);
    }
    pCommandList->ResourceBarrier(1, &uavBarrier);

    pRaytracingCommandList->BuildRaytracingAccelerationStructure(&topLevelAccelerationStructureDesc, 0, nullptr);

Note the lack of a UAV barrier within the loop body. The bottomLevelAccelerationStructureDescs array contains descriptions for BLAS builds defined further up. Note that in line 811, the scratch memory assigned to each description in the array is the same, implying that these BLAS builds may not overlap on the GPU timeline or they will surely invoke a memory hazard.

The reason this code "works" currently is because even further up in line 727, the number of BLAS descriptions in this loop is hardcoded to 1. While the code works, it clearly wasn't intended to function this way, and its suggested to either remove the loop altogether, insert UAV barriers as needed, or allocate additional scratch storage so the BLAS builds operate in independent regions of a single scratch buffer.

stanard commented 1 year ago

Thank you for bringing this to our attention. I'll push a fix soon.

GameDreamByHuo commented 1 year ago

这是来自QQ邮箱的假期自动回复邮件。   你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

stanard commented 1 year ago

Fixed with https://github.com/microsoft/DirectX-Graphics-Samples/pull/790

jeremyong commented 1 year ago

Nice work, thanks!