Wednesday, August 17, 2022
HomeGame Developmentdirectx - DXR / DX12 - When I attempt to entry Acceleration...

directx – DXR / DX12 – When I attempt to entry Acceleration Construction in RayGeneratioin Shader, it makes Gadget Take away


I believe TLAS is currupted or initialized flawed, however cannot discover any errors.

BuildRaytracingAccelerationStructure perform does not throw any error in debug layer, it simply throw Gadget Take away after first body.

After I take away TraceRay from the shader, every other issues are works properly however with out accessing RaytracingAccelerationStructure.

here is how I initialize my Acceleration constructions.
(and github deal with for full code : https://github.com/kcjsend2/Chulsu/tree/most important/Chulsu)

first, load single triangle mesh.

void AssetManager::LoadTestTriangleInstance(ID3D12Device5* machine, ID3D12GraphicsCommandList4* cmdList, ComPtr<D3D12MA::Allocator> alloc, ResourceStateTracker& tracker)
{
    Vertex v1, v2, v3;
    v1.place = { 0, 1, 0 };
    v2.place = { 0.866f, -0.5f, 0 };
    v3.place = { -0.866f, -0.5f, 0 };

    const Vertex vertices[] = { v1, v2, v3 };

    shared_ptr<Occasion> occasion = make_shared<Occasion>();

    SubMesh subMesh;
    subMesh.InitializeBuffers(machine, cmdList, alloc, tracker, *this, sizeof(Vertex), NULL, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, vertices, 3, NULL, 0);
    
    vector<SubMesh> subMeshes;
    subMeshes.push_back(subMesh);

    auto mesh = make_shared<Mesh>(subMeshes);
    mMeshMap["Triangle"] = mesh;

    instance->SetMesh(mesh);

    instance->Replace();
    mInstances.push_back(occasion);
}

And Construct BLAS with triangle.

void AssetManager::BuildBLAS(ID3D12Device5* machine, ID3D12GraphicsCommandList4* cmdList, ComPtr<D3D12MA::Allocator> alloc, ResourceStateTracker tracker)
{
    std::vector<D3D12_RAYTRACING_GEOMETRY_DESC> geomDescs;
    geomDescs.reserve(mMeshMap.dimension());

    for (auto i = mMeshMap.start(); i != mMeshMap.finish(); ++i)
    {
        auto mesh = i->second;
        auto subMeshes = mesh->GetSubMeshes();
        for (auto j = subMeshes.start(); j != subMeshes.finish(); ++j)
        {
            D3D12_RAYTRACING_GEOMETRY_DESC geomDesc = {};
            geomDesc.Kind = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
            geomDesc.Triangles.VertexBuffer.StartAddress = (*j).GetVertexBufferAlloc()->GetResource()->GetGPUVirtualAddress();
            geomDesc.Triangles.VertexBuffer.StrideInBytes = sizeof(Vertex);
            geomDesc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
            geomDesc.Triangles.VertexCount = (*j).GetVertexCount();

            if ((*j).GetIndexCount() > 0)
            {
                geomDesc.Triangles.IndexBuffer = (*j).GetIndexBufferAlloc()->GetResource()->GetGPUVirtualAddress();
                geomDesc.Triangles.IndexFormat = DXGI_FORMAT_R32_UINT;
                geomDesc.Triangles.IndexCount = (*j).GetIndexCount();
            }
            geomDesc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;

            geomDescs.push_back(geomDesc);
        }

        // Get the scale necessities for the scratch and AS buffers
        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
        inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
        inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
        inputs.NumDescs = geomDescs.dimension();
        inputs.pGeometryDescs = geomDescs.information();
        inputs.Kind = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;

        D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO information = {};
        device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &information);

        // Create the buffers. They should assist UAV, and since we're going to instantly use them, we create them with an unordered-access state
        AccelerationStructureBuffers buffers;
        buffers.mScratch = CreateResource(machine, cmdList, alloc, tracker, NULL, information.ScratchDataSizeInBytes, 1,
            D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
        buffers.mResult = CreateResource(machine, cmdList, alloc, tracker, NULL, information.ResultDataMaxSizeInBytes, 1,
            D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);

        // Create the bottom-level AS
        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC asDesc = {};
        asDesc.Inputs = inputs;
        asDesc.DestAccelerationStructureData = buffers.mResult->GetResource()->GetGPUVirtualAddress();
        asDesc.ScratchAccelerationStructureData = buffers.mScratch->GetResource()->GetGPUVirtualAddress();

        cmdList->BuildRaytracingAccelerationStructure(&asDesc, 0, nullptr);

        // We have to insert a UAV barrier earlier than utilizing the acceleration constructions in a raytracing operation
        D3D12_RESOURCE_BARRIER uavBarrier = {};
        uavBarrier.Kind = D3D12_RESOURCE_BARRIER_TYPE_UAV;
        uavBarrier.UAV.pResource = buffers.mResult->GetResource();
        cmdList->ResourceBarrier(1, &uavBarrier);

        mesh->SetBLAS(buffers);
    }
}

now for TLAS…

    void AssetManager::BuildTLAS(ID3D12Device5* machine, ID3D12GraphicsCommandList4* cmdList, ComPtr<D3D12MA::Allocator> alloc, ResourceStateTracker tracker, UINT& tlasSize)
    {
        // First, get the scale of the TLAS buffers and create them
        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
        inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
        inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
        inputs.NumDescs = mInstances.dimension();
        inputs.Kind = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
    
        D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO information;
        device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &information);
    
        // Create the buffers
        AccelerationStructureBuffers buffers;
        buffers.mScratch = CreateResource(machine, cmdList, alloc, tracker, NULL, information.ScratchDataSizeInBytes, 1,
            D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
        buffers.mResult = CreateResource(machine, cmdList, alloc, tracker, NULL, information.ResultDataMaxSizeInBytes, 1,
            D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
        tlasSize = information.ResultDataMaxSizeInBytes;
    
        // The occasion desc ought to be inside a buffer, create and map the buffer
        buffers.mInstanceDesc = CreateResource(machine, cmdList, alloc, tracker, NULL, sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * mInstances.dimension(), 1,
            D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE, D3D12_HEAP_TYPE_UPLOAD);
    
        D3D12_RAYTRACING_INSTANCE_DESC* instanceDescs;
        buffers.mInstanceDesc->GetResource()->Map(0, nullptr, (void**)&instanceDescs);
        ZeroMemory(instanceDescs, sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * mInstances.dimension());
    
        // The transformation matrices for the cases
        XMFLOAT4X4 remodel;
        remodel = Matrix4x4::Identity4x4(); // Id
    
        for (uint32_t i = 0; i < mInstances.dimension(); i++)
        {
            instanceDescs[i].InstanceID = i; // This worth shall be uncovered to the shader through InstanceID()
            instanceDescs[i].InstanceContributionToHitGroupIndex = i;  // That is the offset contained in the shader-table. Since we've got distinctive constant-buffer for every occasion, we'd like a unique offset
            instanceDescs[i].Flags = D3D12_RAYTRACING_INSTANCE_FLAG_NONE;
            XMFLOAT4X4 m = Matrix4x4::Transpose(remodel);
            memcpy(instanceDescs[i].Remodel, &m, sizeof(instanceDescs[i].Remodel));
            instanceDescs[i].AccelerationStructure = mInstances[i]->GetMesh()->GetBLAS().mResult->GetResource()->GetGPUVirtualAddress();
            instanceDescs[i].InstanceMask = 0xFF;
        }
    
        // Unmap
        buffers.mInstanceDesc->GetResource()->Unmap(0, nullptr);
    
        // Create the TLAS
        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC asDesc = {};
        asDesc.Inputs = inputs;
        asDesc.Inputs.InstanceDescs = buffers.mInstanceDesc->GetResource()->GetGPUVirtualAddress();
        asDesc.DestAccelerationStructureData = buffers.mResult->GetResource()->GetGPUVirtualAddress();
        asDesc.ScratchAccelerationStructureData = buffers.mScratch->GetResource()->GetGPUVirtualAddress();
    
        cmdList->BuildRaytracingAccelerationStructure(&asDesc, 0, nullptr);
    
        // We have to insert a UAV barrier earlier than utilizing the acceleration constructions in a raytracing operation
        D3D12_RESOURCE_BARRIER uavBarrier = {};
        uavBarrier.Kind = D3D12_RESOURCE_BARRIER_TYPE_UAV;
        uavBarrier.UAV.pResource = buffers.mResult->GetResource();
        cmdList->ResourceBarrier(1, &uavBarrier);
    
        mTLAS = buffers;
    
        D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
        srvDesc.ViewDimension = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
        srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
        srvDesc.RaytracingAccelerationStructure.Location = mTLAS.mResult->GetResource()->GetGPUVirtualAddress();
        //At present simply do that...
        device->CreateShaderResourceView(nullptr, &srvDesc, GetIndexedCPUHandle(mHeapCurrentIndex));
        mHeapCurrentIndex++;
    }

I attempt to debug with PIX, but it surely simply says AS is properly uploaded on Descriptor Heap, and there is nothing I can get about what’s flawed with AS’s inside.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments