Debian Codegraph @

Source/Core/VideoBackends/D3D12/DescriptorHeapManager.cpp

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/D3D12/DescriptorHeapManager.h"
5
6 #include "Common/Assert.h"
7
8 #include "VideoBackends/D3D12/DX12Context.h"
9 #include "VideoCommon/VideoConfig.h"
10
11 namespace DX12
12 {
13 DescriptorHeapManager::DescriptorHeapManager() = default;
14 DescriptorHeapManager::~DescriptorHeapManager() = default;
15
16 bool DescriptorHeapManager::Create(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE type,
17 u32 num_descriptors)
18 {
19 D3D12_DESCRIPTOR_HEAP_DESC desc = {type, static_cast<UINT>(num_descriptors),
20 D3D12_DESCRIPTOR_HEAP_FLAG_NONE};
21
22 HRESULT hr = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&m_descriptor_heap));
23 ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create descriptor heap: {}", DX12HRWrap(hr));
24 if (FAILED(hr))
25 return false;
26
27 m_heap_base_cpu = m_descriptor_heap->GetCPUDescriptorHandleForHeapStart();
28 m_num_descriptors = num_descriptors;
29 m_descriptor_increment_size = device->GetDescriptorHandleIncrementSize(type);
30
31 // Set all slots to unallocated (1)
32 const u32 bitset_count =
33 num_descriptors / BITSET_SIZE + (((num_descriptors % BITSET_SIZE) != 0) ? 1 : 0);
34 m_free_slots.resize(bitset_count);
35 for (BitSetType& bs : m_free_slots)
36 bs.flip();
37
38 return true;
39 }
40
41 bool DescriptorHeapManager::Allocate(DescriptorHandle* handle)
42 {
43 // Start past the temporary slots, no point in searching those.
44 for (u32 group = 0; group < m_free_slots.size(); group++)
45 {
46 BitSetType& bs = m_free_slots[group];
47 if (bs.none())
48 continue;
49
50 u32 bit = 0;
51 for (; bit < BITSET_SIZE; bit++)
52 {
53 if (bs[bit])
54 break;
55 }
56
57 u32 index = group * BITSET_SIZE + bit;
58 bs[bit] = false;
59
60 handle->index = index;
61 handle->cpu_handle.ptr = m_heap_base_cpu.ptr + index * m_descriptor_increment_size;
62 handle->gpu_handle.ptr = 0;
63 return true;
64 }
65
66 PanicAlertFmt("Out of fixed descriptors");
67 return false;
68 }
69
70 void DescriptorHeapManager::Free(u32 index)
71 {
72 ASSERT(index < m_num_descriptors);
73
74 u32 group = index / BITSET_SIZE;
75 u32 bit = index % BITSET_SIZE;
76 m_free_slots[group][bit] = true;
77 }
78
79 void DescriptorHeapManager::Free(const DescriptorHandle& handle)
80 {
81 Free(handle.index);
82 }
83
84 SamplerHeapManager::SamplerHeapManager() = default;
85 SamplerHeapManager::~SamplerHeapManager() = default;
86
87 static void GetD3DSamplerDesc(D3D12_SAMPLER_DESC* desc, const SamplerState& state)
88 {
89 if (state.tm0.mipmap_filter == FilterMode::Linear)
90 {
91 if (state.tm0.min_filter == FilterMode::Linear)
92 {
93 desc->Filter = (state.tm0.mag_filter == FilterMode::Linear) ?
94 D3D12_FILTER_MIN_MAG_MIP_LINEAR :
95 D3D12_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
96 }
97 else
98 {
99 desc->Filter = (state.tm0.mag_filter == FilterMode::Linear) ?
100 D3D12_FILTER_MIN_POINT_MAG_MIP_LINEAR :
101 D3D12_FILTER_MIN_MAG_POINT_MIP_LINEAR;
102 }
103 }
104 else
105 {
106 if (state.tm0.min_filter == FilterMode::Linear)
107 {
108 desc->Filter = (state.tm0.mag_filter == FilterMode::Linear) ?
109 D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT :
110 D3D12_FILTER_MIN_LINEAR_MAG_MIP_POINT;
111 }
112 else
113 {
114 desc->Filter = (state.tm0.mag_filter == FilterMode::Linear) ?
115 D3D12_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT :
116 D3D12_FILTER_MIN_MAG_MIP_POINT;
117 }
118 }
119
120 static constexpr std::array<D3D12_TEXTURE_ADDRESS_MODE, 3> address_modes = {
121 {D3D12_TEXTURE_ADDRESS_MODE_CLAMP, D3D12_TEXTURE_ADDRESS_MODE_WRAP,
122 D3D12_TEXTURE_ADDRESS_MODE_MIRROR}};
123 desc->AddressU = address_modes[static_cast<u32>(state.tm0.wrap_u.Value())];
124 desc->AddressV = address_modes[static_cast<u32>(state.tm0.wrap_v.Value())];
125 desc->AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
126 desc->MaxLOD = state.tm1.max_lod / 16.f;
127 desc->MinLOD = state.tm1.min_lod / 16.f;
128 desc->MipLODBias = static_cast<s32>(state.tm0.lod_bias) / 256.f;
129 desc->ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
130
131 if (state.tm0.anisotropic_filtering != 0)
132 {
133 desc->Filter = D3D12_FILTER_ANISOTROPIC;
134 desc->MaxAnisotropy = 1u << state.tm0.anisotropic_filtering;
135 }
136 }
137
138 bool SamplerHeapManager::Lookup(const SamplerState& ss, D3D12_CPU_DESCRIPTOR_HANDLE* handle)
139 {
140 const auto it = m_sampler_map.find(ss);
141 if (it != m_sampler_map.end())
142 {
143 *handle = it->second;
144 return true;
145 }
146
147 if (m_current_offset == m_num_descriptors)
148 {
149 // We can clear at any time because the descriptors are copied prior to execution.
150 // It's still not free, since we have to recreate all our samplers again.
151 WARN_LOG_FMT(VIDEO, "Out of samplers, resetting CPU heap");
152 Clear();
153 }
154
155 D3D12_SAMPLER_DESC desc = {};
156 GetD3DSamplerDesc(&desc, ss);
157
158 const D3D12_CPU_DESCRIPTOR_HANDLE new_handle = {m_heap_base_cpu.ptr +
159 m_current_offset * m_descriptor_increment_size};
160 g_dx_context->GetDevice()->CreateSampler(&desc, new_handle);
161
162 m_sampler_map.emplace(ss, new_handle);
163 m_current_offset++;
164 *handle = new_handle;
165 return true;
166 }
167
168 void SamplerHeapManager::Clear()
169 {
170 m_sampler_map.clear();
171 m_current_offset = 0;
172 }
173
174 bool SamplerHeapManager::Create(ID3D12Device* device, u32 num_descriptors)
175 {
176 const D3D12_DESCRIPTOR_HEAP_DESC desc = {D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, num_descriptors};
177 HRESULT hr = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&m_descriptor_heap));
178 ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create sampler descriptor heap: {}", DX12HRWrap(hr));
179 if (FAILED(hr))
180 return false;
181
182 m_num_descriptors = num_descriptors;
183 m_descriptor_increment_size =
184 device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
185 m_heap_base_cpu = m_descriptor_heap->GetCPUDescriptorHandleForHeapStart();
186 return true;
187 }
188 } // namespace DX12