| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D12/DescriptorAllocator.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | |
| 8 | #include "VideoBackends/D3D12/DX12Context.h" |
| 9 | |
| 10 | namespace DX12 |
| 11 | { |
| 12 | DescriptorAllocator::DescriptorAllocator() = default; |
| 13 | DescriptorAllocator::~DescriptorAllocator() = default; |
| 14 | |
| 15 | bool DescriptorAllocator::Create(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE type, |
| 16 | u32 num_descriptors) |
| 17 | { |
| 18 | const D3D12_DESCRIPTOR_HEAP_DESC desc = {type, static_cast<UINT>(num_descriptors), |
| 19 | D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE}; |
| 20 | HRESULT hr = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&m_descriptor_heap)); |
| 21 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating descriptor heap for linear allocator failed: {}", |
| 22 | DX12HRWrap(hr)); |
| 23 | if (FAILED(hr)) |
| 24 | return false; |
| 25 | |
| 26 | m_num_descriptors = num_descriptors; |
| 27 | m_descriptor_increment_size = device->GetDescriptorHandleIncrementSize(type); |
| 28 | m_heap_base_cpu = m_descriptor_heap->GetCPUDescriptorHandleForHeapStart(); |
| 29 | m_heap_base_gpu = m_descriptor_heap->GetGPUDescriptorHandleForHeapStart(); |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | bool DescriptorAllocator::Allocate(u32 num_handles, DescriptorHandle* out_base_handle) |
| 34 | { |
| 35 | if ((m_current_offset + num_handles) > m_num_descriptors) |
| 36 | return false; |
| 37 | |
| 38 | out_base_handle->index = m_current_offset; |
| 39 | out_base_handle->cpu_handle.ptr = |
| 40 | m_heap_base_cpu.ptr + m_current_offset * m_descriptor_increment_size; |
| 41 | out_base_handle->gpu_handle.ptr = |
| 42 | m_heap_base_gpu.ptr + m_current_offset * m_descriptor_increment_size; |
| 43 | m_current_offset += num_handles; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | void DescriptorAllocator::Reset() |
| 48 | { |
| 49 | m_current_offset = 0; |
| 50 | } |
| 51 | |
| 52 | bool operator==(const SamplerStateSet& lhs, const SamplerStateSet& rhs) |
| 53 | { |
| 54 | // There shouldn't be any padding here, so this will be safe. |
| 55 | return std::memcmp(lhs.states, rhs.states, sizeof(lhs.states)) == 0; |
| 56 | } |
| 57 | |
| 58 | bool operator!=(const SamplerStateSet& lhs, const SamplerStateSet& rhs) |
| 59 | { |
| 60 | return std::memcmp(lhs.states, rhs.states, sizeof(lhs.states)) != 0; |
| 61 | } |
| 62 | |
| 63 | bool operator<(const SamplerStateSet& lhs, const SamplerStateSet& rhs) |
| 64 | { |
| 65 | return std::memcmp(lhs.states, rhs.states, sizeof(lhs.states)) < 0; |
| 66 | } |
| 67 | |
| 68 | SamplerAllocator::SamplerAllocator() = default; |
| 69 | SamplerAllocator::~SamplerAllocator() = default; |
| 70 | |
| 71 | bool SamplerAllocator::Create(ID3D12Device* device) |
| 72 | { |
| 73 | return DescriptorAllocator::Create(device, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, |
| 74 | D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE); |
| 75 | } |
| 76 | |
| 77 | bool SamplerAllocator::GetGroupHandle(const SamplerStateSet& sss, |
| 78 | D3D12_GPU_DESCRIPTOR_HANDLE* handle) |
| 79 | { |
| 80 | auto it = m_sampler_map.find(sss); |
| 81 | if (it != m_sampler_map.end()) |
| 82 | { |
| 83 | *handle = it->second; |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | // Allocate a group of descriptors. |
| 88 | DescriptorHandle allocation; |
| 89 | if (!Allocate(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, &allocation)) |
| 90 | return false; |
| 91 | |
| 92 | // Lookup sampler handles from global cache. |
| 93 | std::array<D3D12_CPU_DESCRIPTOR_HANDLE, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> source_handles; |
| 94 | for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) |
| 95 | { |
| 96 | if (!g_dx_context->GetSamplerHeapManager().Lookup(sss.states[i], &source_handles[i])) |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Copy samplers from the sampler heap. |
| 101 | static constexpr std::array<UINT, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> source_sizes = { |
| 102 | {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}; |
| 103 | g_dx_context->GetDevice()->CopyDescriptors( |
| 104 | 1, &allocation.cpu_handle, &VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, |
| 105 | VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, source_handles.data(), source_sizes.data(), |
| 106 | D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); |
| 107 | *handle = allocation.gpu_handle; |
| 108 | m_sampler_map.emplace(sss, allocation.gpu_handle); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool SamplerAllocator::ShouldReset() const |
| 113 | { |
| 114 | // We only reset the sampler heap if more than half of the descriptors are used. |
| 115 | // This saves descriptor copying when there isn't a large number of sampler configs per frame. |
| 116 | return m_sampler_map.size() >= (D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE / 2); |
| 117 | } |
| 118 | |
| 119 | void SamplerAllocator::Reset() |
| 120 | { |
| 121 | DescriptorAllocator::Reset(); |
| 122 | m_sampler_map.clear(); |
| 123 | } |
| 124 | } // namespace DX12 |