Program Listing for File d3d12-descriptorheap.h

Return to documentation for file (include\d3d12-state-tracking\d3d12-descriptorheap.h)

/******************************************************************************
© 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 "d3d12-object.h"
#include <d3d12.h>
#include <vector>

namespace gpa {
namespace d3d12_state_tracker {

enum DX12DescriptorType {
    D3D12_DESCRIPTOR_TYPE_EMPTY = 0,
    D3D12_DESCRIPTOR_TYPE_CONSTANT_BUFFER_VIEW,
    D3D12_DESCRIPTOR_TYPE_DEPTH_STENCIL_VIEW,
    D3D12_DESCRIPTOR_TYPE_RENDER_TARGET_VIEW,
    D3D12_DESCRIPTOR_TYPE_SAMPLER,
    D3D12_DESCRIPTOR_TYPE_SHADER_RESOURCE_VIEW,
    D3D12_DESCRIPTOR_TYPE_UNORDERED_ACCESS_VIEW,
    D3D12_DESCRIPTOR_TYPE_SAMPLER_FEEDBACK_UNORDERED_ACCESS_VIEW,
};

struct DX12NullDescriptor
{
    uint64_t timestamp = 0ui64;
    DX12DescriptorType type = D3D12_DESCRIPTOR_TYPE_EMPTY;
    bool hasDesc = false;
    union
    {
        D3D12_CONSTANT_BUFFER_VIEW_DESC cbv;
        D3D12_DEPTH_STENCIL_VIEW_DESC dsv;
        D3D12_RENDER_TARGET_VIEW_DESC rtv;
        D3D12_SAMPLER_DESC sampler;
        D3D12_SHADER_RESOURCE_VIEW_DESC srv;
        D3D12_UNORDERED_ACCESS_VIEW_DESC uav;
    };
};

struct DX12Descriptor : DX12NullDescriptor
{
    bool hasResource = false;
    WeakPtr<ID3D12Resource> resource;
    WeakPtr<ID3D12Resource> secondaryResource;

    ID3D12Resource* UAVCounterResource() const
    {
        return secondaryResource;
    }

    ID3D12Resource* SFUAVFeedbackResource() const
    {
        return resource;
    }

    ID3D12Resource* SFUAVTargetedResource() const
    {
        return secondaryResource;
    }
};

class D3D12DescriptorHeapState : public D3D12ObjectState
{
public:
    static constexpr GUID sGUID = {0x6f61b7b, 0xba75, 0x4782, {0xa7, 0x1e, 0xb6, 0x24, 0xd3, 0x76, 0xad, 0x71}};

private:
    D3D12_DESCRIPTOR_HEAP_DESC mDesc;

    uint32_t mUniqueSequentialID;
    UINT mIncrementSize = 0;
    D3D12_CPU_DESCRIPTOR_HANDLE mCPUHandle = {};
    bool mCPUHandleObtained = false;
    D3D12_GPU_DESCRIPTOR_HANDLE mGPUHandle = {};
    bool mGPUHandleObtained = false;
    std::vector<DX12Descriptor> mDescriptors;

protected:
    DX12Descriptor* GetDescriptors();

public:
    D3D12DescriptorHeapState(ID3D12DescriptorHeap* pHeap, const D3D12_DESCRIPTOR_HEAP_DESC* pDescriptorHeapDesc, uint32_t uniqueSequentialID);

    GUID GetGUID() override;

    D3D12_DESCRIPTOR_HEAP_TYPE GetType() const;

    uint32_t GetUniqueID() const;

    UINT GetIncrementSize() const;

    D3D12_CPU_DESCRIPTOR_HANDLE GetCPUHandle() const;

    bool CPUHandleObtained() const;
    void CPUHandleObtained(bool val);

    D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle() const;

    bool GPUHandleObtained() const;
    void GPUHandleObtained(bool val);

    uint64_t GetCPUHandle(DX12Descriptor const& descriptor) const;

    std::vector<DX12Descriptor>::iterator GetDescriptor(uint64_t cpuHandle);

    size_t GetDescriptorCount() const;

    std::vector<DX12Descriptor>::iterator begin();
    std::vector<DX12Descriptor>::iterator end();

    struct CopyCollection
    {
        uint32_t mNumDescriptorRanges;
        D3D12_CPU_DESCRIPTOR_HANDLE const* mpDescriptorRangeStarts;
        uint32_t const* mpDescriptorRangeSizes;
        D3D12_DESCRIPTOR_HEAP_TYPE mType;

        uint32_t mCurrDescriptorRangeIndex = 0;
        uint32_t mCurrDescriptorIndexInCurrRange = 0;

        uint32_t mCurrentRangeHeapIndex = UINT32_MAX;
        uint32_t mCurrentRangeBaseDescriptorIndex = UINT32_MAX;
        gpa::d3d12_state_tracker::DX12Descriptor* mCurrentRangeDescriptorHeapContents = nullptr;
        uint32_t mCurrentHeapTypeIncrementSize = UINT32_MAX;

        CopyCollection(UINT NumDescriptorRanges, const D3D12_CPU_DESCRIPTOR_HANDLE* pDescriptorRangeStarts, const UINT* pDescriptorRangeSizes, D3D12_DESCRIPTOR_HEAP_TYPE type);
        void OnNewRange(D3D12_CPU_DESCRIPTOR_HANDLE rangeStart);
        gpa::d3d12_state_tracker::DX12Descriptor& Current();
        bool Proceed();
    };
};

}  // namespace d3d12_state_tracker
}  // namespace gpa