Program Listing for File d3d12-acceleration-structure-placement-manager.h

Return to documentation for file (include\cross-gpu\d3d12-acceleration-structure-placement-manager.h)

/******************************************************************************
(C) Intel Corporation.

This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.


This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.

******************************************************************************/
#pragma once

#include "cross-gpu/d3d12-cross-gpu-support.h"
#include "reflection/struct-data-manager.h"
#include "utility/noncopyable.h"

#include <map>
#include <memory>
#include <unordered_map>
#include <unordered_set>

namespace gpa {
namespace cross_gpu {
namespace playback {

class D3D12ResourcePlacementManager;

// Manages acceleration structures that will be built within one or more resource placements.
class D3D12AccelerationStructurePlacementManager : public NonCopyable
{
public:
    D3D12AccelerationStructurePlacementManager() = default;
    ~D3D12AccelerationStructurePlacementManager() = default;

    // Called to load AS placements from captured stream before playback begins.
    //
    void OnCommandListBuildAccelerationStructure(const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& desc);
    void OnCommandListCopyAccelerationStructure(D3D12_GPU_VIRTUAL_ADDRESS DestAccelerationStructureData, D3D12_GPU_VIRTUAL_ADDRESS SourceAccelerationStructureData);
    void OnResourceGetGPUVirtualAddress(TObjectKey resourceKey, D3D12_GPU_VIRTUAL_ADDRESS resourceStartAddress);
    void OnDeviceGetRaytracingAccelerationStructurePrebuildInfo(TObjectKey deviceKey, const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& desc, const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& pInfo);

    // Called during playback to patch resource sizes at creation to fit containing acceleration structures.
    void CreateAccelerationStructurePlacements(ID3D12Device* pDevice, TObjectKey deviceKey, D3D12ResourcePlacementManager* pManager);

private:
    TObjectKey GetResourceKey(D3D12_GPU_VIRTUAL_ADDRESS destAccelerationStructureData) const;
    uint64_t GetResourceOffset(D3D12_GPU_VIRTUAL_ADDRESS destAccelerationStructureData) const;
    D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO GetAccelerationStructurePreBuildInfo(TObjectKey deviceKey, const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& inputs) const;

    std::unordered_map<D3D12_GPU_VIRTUAL_ADDRESS, std::unique_ptr<serialization::StructDataManager>> mBuildAccelerationStructureDescPerDest;
    std::map<D3D12_GPU_VIRTUAL_ADDRESS, TObjectKey> mResourcesByStart;

    class AccelerationStructurePreBuildInfoEntry : public NonCopyable
    {
    public:
        AccelerationStructurePreBuildInfoEntry(const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& inputs);  // for lookup
        AccelerationStructurePreBuildInfoEntry(const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& inputs, const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& info);

        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS GetInputs() const;
        D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO GetInfo() const;

        struct HashFunc
        {
            size_t operator()(const AccelerationStructurePreBuildInfoEntry& entry) const;
        };

        struct EqualFunc
        {
            bool operator()(const AccelerationStructurePreBuildInfoEntry& lhs, const AccelerationStructurePreBuildInfoEntry& rhs) const;
        };

    private:
        serialization::StructDataManager mInputsStruct;
        D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO mInfo = {};
    };

    // Tracks {Inputs, PREBUILD_INFO} per device.
    using AccelerationStructurePreBuildInfoPerDesc = std::unordered_set<AccelerationStructurePreBuildInfoEntry, AccelerationStructurePreBuildInfoEntry::HashFunc, AccelerationStructurePreBuildInfoEntry::EqualFunc>;
    std::unordered_map<TObjectKey, AccelerationStructurePreBuildInfoPerDesc> mAccelerationStructurePrebuildInfosPerDevice;
};

}  // namespace playback
}  // namespace cross_gpu
}  // namespace gpa