summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineD3D12/src/TopLevelASD3D12Impl.cpp
blob: c4a0547af4fd662a321eae9323917bd9dbe233c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  Copyright 2019-2021 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

#include "pch.h"

#include "TopLevelASD3D12Impl.hpp"

#include "RenderDeviceD3D12Impl.hpp"
#include "D3D12TypeConversions.hpp"
#include "GraphicsAccessories.hpp"
#include "DXGITypeConversions.hpp"
#include "StringTools.hpp"

namespace Diligent
{

TopLevelASD3D12Impl::TopLevelASD3D12Impl(IReferenceCounters*    pRefCounters,
                                         RenderDeviceD3D12Impl* pDeviceD3D12,
                                         const TopLevelASDesc&  Desc) :
    TTopLevelASBase{pRefCounters, pDeviceD3D12, Desc}
{
    auto* const pd3d12Device = pDeviceD3D12->GetD3D12Device5();
    const auto& Limits       = pDeviceD3D12->GetProperties();

    UINT64 ResultDataMaxSizeInBytes = 0;
    if (m_Desc.CompactedSize > 0)
    {
        ResultDataMaxSizeInBytes = m_Desc.CompactedSize;
    }
    else
    {
        D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO d3d12TopLevelPrebuildInfo = {};
        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS  d3d12TopLevelInputs       = {};

        d3d12TopLevelInputs.Type        = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
        d3d12TopLevelInputs.Flags       = BuildASFlagsToD3D12ASBuildFlags(m_Desc.Flags);
        d3d12TopLevelInputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
        d3d12TopLevelInputs.NumDescs    = m_Desc.MaxInstanceCount;

        DEV_CHECK_ERR(m_Desc.MaxInstanceCount <= Limits.MaxInstancesPerTLAS,
                      "Max instance count (", m_Desc.MaxInstanceCount, ") exceeds device limit (", Limits.MaxInstancesPerTLAS, ").");

        pd3d12Device->GetRaytracingAccelerationStructurePrebuildInfo(&d3d12TopLevelInputs, &d3d12TopLevelPrebuildInfo);
        if (d3d12TopLevelPrebuildInfo.ResultDataMaxSizeInBytes == 0)
            LOG_ERROR_AND_THROW("Failed to get ray tracing acceleration structure prebuild info.");

        ResultDataMaxSizeInBytes = d3d12TopLevelPrebuildInfo.ResultDataMaxSizeInBytes;

        m_ScratchSize.Build  = static_cast<Uint32>(d3d12TopLevelPrebuildInfo.ScratchDataSizeInBytes);
        m_ScratchSize.Update = static_cast<Uint32>(d3d12TopLevelPrebuildInfo.UpdateScratchDataSizeInBytes);
    }

    D3D12_HEAP_PROPERTIES HeapProps{};
    HeapProps.Type                 = D3D12_HEAP_TYPE_DEFAULT;
    HeapProps.CPUPageProperty      = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
    HeapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
    HeapProps.CreationNodeMask     = 1;
    HeapProps.VisibleNodeMask      = 1;

    D3D12_RESOURCE_DESC d3d12ASDesc{};
    d3d12ASDesc.Dimension          = D3D12_RESOURCE_DIMENSION_BUFFER;
    d3d12ASDesc.Alignment          = 0;
    d3d12ASDesc.Width              = ResultDataMaxSizeInBytes;
    d3d12ASDesc.Height             = 1;
    d3d12ASDesc.DepthOrArraySize   = 1;
    d3d12ASDesc.MipLevels          = 1;
    d3d12ASDesc.Format             = DXGI_FORMAT_UNKNOWN;
    d3d12ASDesc.SampleDesc.Count   = 1;
    d3d12ASDesc.SampleDesc.Quality = 0;
    d3d12ASDesc.Layout             = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
    d3d12ASDesc.Flags              = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;

    auto hr = pd3d12Device->CreateCommittedResource(&HeapProps, D3D12_HEAP_FLAG_NONE,
                                                    &d3d12ASDesc, D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, nullptr,
                                                    __uuidof(m_pd3d12Resource),
                                                    reinterpret_cast<void**>(static_cast<ID3D12Resource**>(&m_pd3d12Resource)));
    if (FAILED(hr))
        LOG_ERROR_AND_THROW("Failed to create D3D12 Top-level acceleration structure");

    if (*m_Desc.Name != 0)
        m_pd3d12Resource->SetName(WidenString(m_Desc.Name).c_str());

    m_DescriptorHandle = pDeviceD3D12->AllocateDescriptors(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    D3D12_SHADER_RESOURCE_VIEW_DESC d3d12SRVDesc{};
    d3d12SRVDesc.ViewDimension                            = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
    d3d12SRVDesc.Shader4ComponentMapping                  = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
    d3d12SRVDesc.Format                                   = DXGI_FORMAT_UNKNOWN;
    d3d12SRVDesc.RaytracingAccelerationStructure.Location = GetGPUAddress();
    pd3d12Device->CreateShaderResourceView(nullptr, &d3d12SRVDesc, m_DescriptorHandle.GetCpuHandle());

    DEV_CHECK_ERR(GetGPUAddress() % D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT == 0, "GPU virtual address is expected to be at least 256-byte aligned");

    SetState(RESOURCE_STATE_BUILD_AS_READ);
}

TopLevelASD3D12Impl::TopLevelASD3D12Impl(IReferenceCounters*          pRefCounters,
                                         class RenderDeviceD3D12Impl* pDeviceD3D12,
                                         const TopLevelASDesc&        Desc,
                                         RESOURCE_STATE               InitialState,
                                         ID3D12Resource*              pd3d12TLAS) :
    TTopLevelASBase{pRefCounters, pDeviceD3D12, Desc}
{
    m_pd3d12Resource = pd3d12TLAS;
    SetState(InitialState);
}

TopLevelASD3D12Impl::~TopLevelASD3D12Impl()
{
    // D3D12 object can only be destroyed when it is no longer used by the GPU
    GetDevice()->SafeReleaseDeviceObject(std::move(m_pd3d12Resource), m_Desc.CommandQueueMask);
}

} // namespace Diligent