From af313f84198d2eb52690b406164eebcb1866d251 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Fri, 10 Feb 2023 00:28:06 -0600 Subject: [PATCH] VideoCommon: add constant value to set the allowed maximum number of pixel samplers --- Source/Core/DolphinLib.props | 1 + Source/Core/VideoBackends/D3D/D3DState.cpp | 2 +- Source/Core/VideoBackends/D3D/D3DState.h | 5 +++-- Source/Core/VideoBackends/D3D12/D3D12Gfx.cpp | 15 ++++++++------- Source/Core/VideoBackends/D3D12/D3D12Gfx.h | 4 ++-- Source/Core/VideoBackends/D3D12/DX12Context.cpp | 8 ++++---- .../VideoBackends/D3D12/DescriptorAllocator.cpp | 12 ++++++------ .../VideoBackends/D3D12/DescriptorAllocator.h | 4 ++-- Source/Core/VideoBackends/OGL/OGLGfx.h | 3 ++- Source/Core/VideoBackends/OGL/SamplerCache.h | 4 +++- .../VideoBackends/Vulkan/CommandBufferManager.cpp | 8 ++++++-- Source/Core/VideoBackends/Vulkan/Constants.h | 2 +- Source/Core/VideoBackends/Vulkan/ObjectCache.cpp | 5 +++-- Source/Core/VideoBackends/Vulkan/StateTracker.cpp | 7 ++++--- Source/Core/VideoBackends/Vulkan/StateTracker.h | 3 ++- Source/Core/VideoBackends/Vulkan/VKGfx.h | 3 ++- Source/Core/VideoCommon/CMakeLists.txt | 1 + Source/Core/VideoCommon/Constants.h | 11 +++++++++++ 18 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 Source/Core/VideoCommon/Constants.h diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index f7f7606982..ddb2cd86e7 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -635,6 +635,7 @@ + diff --git a/Source/Core/VideoBackends/D3D/D3DState.cpp b/Source/Core/VideoBackends/D3D/D3DState.cpp index 66ccec2416..8ab1991e90 100644 --- a/Source/Core/VideoBackends/D3D/D3DState.cpp +++ b/Source/Core/VideoBackends/D3D/D3DState.cpp @@ -205,7 +205,7 @@ u32 StateManager::UnsetTexture(ID3D11ShaderResourceView* srv) { u32 mask = 0; - for (u32 index = 0; index < 8; ++index) + for (u32 index = 0; index < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; ++index) { if (m_current.textures[index] == srv) { diff --git a/Source/Core/VideoBackends/D3D/D3DState.h b/Source/Core/VideoBackends/D3D/D3DState.h index 795e7a0432..812fc778ae 100644 --- a/Source/Core/VideoBackends/D3D/D3DState.h +++ b/Source/Core/VideoBackends/D3D/D3DState.h @@ -12,6 +12,7 @@ #include "Common/BitField.h" #include "Common/CommonTypes.h" #include "VideoBackends/D3D/D3DBase.h" +#include "VideoCommon/Constants.h" #include "VideoCommon/RenderState.h" namespace DX11 @@ -263,8 +264,8 @@ private: struct Resources { - std::array textures; - std::array samplers; + std::array textures; + std::array samplers; std::array pixelConstants; ID3D11Buffer* vertexConstants; ID3D11Buffer* geometryConstants; diff --git a/Source/Core/VideoBackends/D3D12/D3D12Gfx.cpp b/Source/Core/VideoBackends/D3D12/D3D12Gfx.cpp index b3ade2e5d3..e86579b87f 100644 --- a/Source/Core/VideoBackends/D3D12/D3D12Gfx.cpp +++ b/Source/Core/VideoBackends/D3D12/D3D12Gfx.cpp @@ -33,8 +33,7 @@ Gfx::Gfx(std::unique_ptr swap_chain, float backbuffer_scale) { m_state.root_signature = g_dx_context->GetGXRootSignature(); - // Textures must be populated with null descriptors, since we copy directly from this array. - for (u32 i = 0; i < MAX_TEXTURES; i++) + for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) { m_state.textures[i].ptr = g_dx_context->GetNullSRVDescriptor().cpu_handle.ptr; m_state.samplers.states[i] = RenderState::GetPointSamplerState(); @@ -301,7 +300,7 @@ void Gfx::UnbindTexture(const AbstractTexture* texture) { const auto srv_shadow_descriptor = static_cast(texture)->GetSRVDescriptor().cpu_handle; - for (u32 i = 0; i < MAX_TEXTURES; i++) + for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) { if (m_state.textures[i].ptr == srv_shadow_descriptor.ptr) { @@ -666,15 +665,17 @@ void Gfx::UpdateDescriptorTables() bool Gfx::UpdateSRVDescriptorTable() { - static constexpr std::array src_sizes = {1, 1, 1, 1, 1, 1, 1, 1}; + static constexpr std::array src_sizes = { + 1, 1, 1, 1, 1, 1, 1, 1}; DescriptorHandle dst_base_handle; const UINT dst_handle_sizes = 8; - if (!g_dx_context->GetDescriptorAllocator()->Allocate(MAX_TEXTURES, &dst_base_handle)) + if (!g_dx_context->GetDescriptorAllocator()->Allocate(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, + &dst_base_handle)) return false; g_dx_context->GetDevice()->CopyDescriptors( - 1, &dst_base_handle.cpu_handle, &dst_handle_sizes, MAX_TEXTURES, m_state.textures.data(), - src_sizes.data(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + 1, &dst_base_handle.cpu_handle, &dst_handle_sizes, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, + m_state.textures.data(), src_sizes.data(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); m_state.srv_descriptor_base = dst_base_handle.gpu_handle; m_dirty_bits = (m_dirty_bits & ~DirtyState_Textures) | DirtyState_SRV_Descriptor; return true; diff --git a/Source/Core/VideoBackends/D3D12/D3D12Gfx.h b/Source/Core/VideoBackends/D3D12/D3D12Gfx.h index 95774dd71d..9855ae7ca6 100644 --- a/Source/Core/VideoBackends/D3D12/D3D12Gfx.h +++ b/Source/Core/VideoBackends/D3D12/D3D12Gfx.h @@ -7,6 +7,7 @@ #include "VideoBackends/D3D12/DescriptorAllocator.h" #include "VideoBackends/D3D12/DescriptorHeapManager.h" #include "VideoCommon/AbstractGfx.h" +#include "VideoCommon/Constants.h" namespace DX12 { @@ -96,7 +97,6 @@ protected: void OnConfigChanged(u32 bits) override; private: - static const u32 MAX_TEXTURES = 8; static const u32 NUM_CONSTANT_BUFFERS = 3; // Dirty bits @@ -158,7 +158,7 @@ private: ID3D12RootSignature* root_signature = nullptr; DXShader* compute_shader = nullptr; std::array constant_buffers = {}; - std::array textures = {}; + std::array textures = {}; D3D12_CPU_DESCRIPTOR_HANDLE vs_srv = {}; D3D12_CPU_DESCRIPTOR_HANDLE ps_uav = {}; SamplerStateSet samplers = {}; diff --git a/Source/Core/VideoBackends/D3D12/DX12Context.cpp b/Source/Core/VideoBackends/D3D12/DX12Context.cpp index e0fd41e4f0..8343e39a6e 100644 --- a/Source/Core/VideoBackends/D3D12/DX12Context.cpp +++ b/Source/Core/VideoBackends/D3D12/DX12Context.cpp @@ -334,8 +334,8 @@ bool DXContext::CreateGXRootSignature() { // GX: // - 3 constant buffers (bindings 0-2), 0/1 visible in PS, 2 visible in VS, 1 visible in GS. - // - 8 textures (visible in PS). - // - 8 samplers (visible in PS). + // - VideoCommon::MAX_PIXEL_SHADER_SAMPLERS textures (visible in PS). + // - VideoCommon::MAX_PIXEL_SHADER_SAMPLERS samplers (visible in PS). // - 1 UAV (visible in PS). std::array params; @@ -344,10 +344,10 @@ bool DXContext::CreateGXRootSignature() SetRootParamCBV(¶ms[param_count], 0, D3D12_SHADER_VISIBILITY_PIXEL); param_count++; SetRootParamTable(¶ms[param_count], &ranges[param_count], D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0, - 8, D3D12_SHADER_VISIBILITY_PIXEL); + VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, D3D12_SHADER_VISIBILITY_PIXEL); param_count++; SetRootParamTable(¶ms[param_count], &ranges[param_count], D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, - 0, 8, D3D12_SHADER_VISIBILITY_PIXEL); + 0, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, D3D12_SHADER_VISIBILITY_PIXEL); param_count++; SetRootParamCBV(¶ms[param_count], 0, D3D12_SHADER_VISIBILITY_VERTEX); param_count++; diff --git a/Source/Core/VideoBackends/D3D12/DescriptorAllocator.cpp b/Source/Core/VideoBackends/D3D12/DescriptorAllocator.cpp index bd50b97d87..d77d7f454d 100644 --- a/Source/Core/VideoBackends/D3D12/DescriptorAllocator.cpp +++ b/Source/Core/VideoBackends/D3D12/DescriptorAllocator.cpp @@ -86,23 +86,23 @@ bool SamplerAllocator::GetGroupHandle(const SamplerStateSet& sss, // Allocate a group of descriptors. DescriptorHandle allocation; - if (!Allocate(SamplerStateSet::NUM_SAMPLERS_PER_GROUP, &allocation)) + if (!Allocate(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, &allocation)) return false; // Lookup sampler handles from global cache. - std::array source_handles; - for (u32 i = 0; i < SamplerStateSet::NUM_SAMPLERS_PER_GROUP; i++) + std::array source_handles; + for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) { if (!g_dx_context->GetSamplerHeapManager().Lookup(sss.states[i], &source_handles[i])) return false; } // Copy samplers from the sampler heap. - static constexpr std::array source_sizes = { + static constexpr std::array source_sizes = { {1, 1, 1, 1, 1, 1, 1, 1}}; g_dx_context->GetDevice()->CopyDescriptors( - 1, &allocation.cpu_handle, &SamplerStateSet::NUM_SAMPLERS_PER_GROUP, - SamplerStateSet::NUM_SAMPLERS_PER_GROUP, source_handles.data(), source_sizes.data(), + 1, &allocation.cpu_handle, &VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, + VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, source_handles.data(), source_sizes.data(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); *handle = allocation.gpu_handle; m_sampler_map.emplace(sss, allocation.gpu_handle); diff --git a/Source/Core/VideoBackends/D3D12/DescriptorAllocator.h b/Source/Core/VideoBackends/D3D12/DescriptorAllocator.h index 278df058a1..bdc5f3996b 100644 --- a/Source/Core/VideoBackends/D3D12/DescriptorAllocator.h +++ b/Source/Core/VideoBackends/D3D12/DescriptorAllocator.h @@ -5,6 +5,7 @@ #include #include "VideoBackends/D3D12/DescriptorHeapManager.h" +#include "VideoCommon/Constants.h" namespace DX12 { @@ -34,8 +35,7 @@ protected: struct SamplerStateSet final { - static const u32 NUM_SAMPLERS_PER_GROUP = 8; - SamplerState states[NUM_SAMPLERS_PER_GROUP]; + SamplerState states[VideoCommon::MAX_PIXEL_SHADER_SAMPLERS]; }; bool operator==(const SamplerStateSet& lhs, const SamplerStateSet& rhs); diff --git a/Source/Core/VideoBackends/OGL/OGLGfx.h b/Source/Core/VideoBackends/OGL/OGLGfx.h index 410b7d562e..51784c2ec8 100644 --- a/Source/Core/VideoBackends/OGL/OGLGfx.h +++ b/Source/Core/VideoBackends/OGL/OGLGfx.h @@ -4,6 +4,7 @@ #pragma once #include "VideoCommon/AbstractGfx.h" +#include "VideoCommon/Constants.h" class GLContext; @@ -100,7 +101,7 @@ private: std::unique_ptr m_main_gl_context; std::unique_ptr m_system_framebuffer; - std::array m_bound_textures{}; + std::array m_bound_textures{}; AbstractTexture* m_bound_image_texture = nullptr; RasterizationState m_current_rasterization_state; DepthState m_current_depth_state; diff --git a/Source/Core/VideoBackends/OGL/SamplerCache.h b/Source/Core/VideoBackends/OGL/SamplerCache.h index be8860c9f8..5d87c4daa2 100644 --- a/Source/Core/VideoBackends/OGL/SamplerCache.h +++ b/Source/Core/VideoBackends/OGL/SamplerCache.h @@ -9,6 +9,7 @@ #include "Common/CommonTypes.h" #include "Common/GL/GLUtil.h" +#include "VideoCommon/Constants.h" #include "VideoCommon/RenderState.h" namespace OGL @@ -35,7 +36,8 @@ private: static void SetParameters(GLuint sampler_id, const SamplerState& params); std::map m_cache; - std::array, 8> m_active_samplers{}; + std::array, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> + m_active_samplers{}; GLuint m_point_sampler; GLuint m_linear_sampler; diff --git a/Source/Core/VideoBackends/Vulkan/CommandBufferManager.cpp b/Source/Core/VideoBackends/Vulkan/CommandBufferManager.cpp index 8c9f744216..80bbd7e5b5 100644 --- a/Source/Core/VideoBackends/Vulkan/CommandBufferManager.cpp +++ b/Source/Core/VideoBackends/Vulkan/CommandBufferManager.cpp @@ -11,6 +11,7 @@ #include "Common/Thread.h" #include "VideoBackends/Vulkan/VulkanContext.h" +#include "VideoCommon/Constants.h" namespace Vulkan { @@ -148,14 +149,17 @@ VkDescriptorPool CommandBufferManager::CreateDescriptorPool(u32 max_descriptor_s /* * Worst case descriptor counts according to the descriptor layout created in ObjectCache.cpp: * UNIFORM_BUFFER_DYNAMIC: 3 - * COMBINED_IMAGE_SAMPLER: 18 + * COMBINED_IMAGE_SAMPLER: NUM_UTILITY_PIXEL_SAMPLERS + NUM_COMPUTE_SHADER_SAMPLERS + + * VideoCommon::MAX_PIXEL_SHADER_SAMPLERS * STORAGE_BUFFER: 2 * UNIFORM_TEXEL_BUFFER: 3 * STORAGE_IMAGE: 1 */ const std::array pool_sizes{{ {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, max_descriptor_sets * 3}, - {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, max_descriptor_sets * 18}, + {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + max_descriptor_sets * (VideoCommon::MAX_PIXEL_SHADER_SAMPLERS + NUM_COMPUTE_SHADER_SAMPLERS + + NUM_UTILITY_PIXEL_SAMPLERS)}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, max_descriptor_sets * 2}, {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, max_descriptor_sets * 3}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, max_descriptor_sets * 1}, diff --git a/Source/Core/VideoBackends/Vulkan/Constants.h b/Source/Core/VideoBackends/Vulkan/Constants.h index f166f9b9b4..255cfe4929 100644 --- a/Source/Core/VideoBackends/Vulkan/Constants.h +++ b/Source/Core/VideoBackends/Vulkan/Constants.h @@ -78,8 +78,8 @@ enum UNIFORM_BUFFER_DESCRIPTOR_SET_BINDING constexpr u32 MAX_VERTEX_ATTRIBUTES = 16; // Number of pixel shader texture slots -constexpr u32 NUM_PIXEL_SHADER_SAMPLERS = 8; constexpr u32 NUM_COMPUTE_SHADER_SAMPLERS = 2; +constexpr u32 NUM_UTILITY_PIXEL_SAMPLERS = 8; // Number of texel buffer binding points. constexpr u32 NUM_COMPUTE_TEXEL_BUFFERS = 2; diff --git a/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp b/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp index 57dbe6f5e3..68683790e7 100644 --- a/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp +++ b/Source/Core/VideoBackends/Vulkan/ObjectCache.cpp @@ -21,6 +21,7 @@ #include "VideoBackends/Vulkan/VKTexture.h" #include "VideoBackends/Vulkan/VKVertexFormat.h" #include "VideoBackends/Vulkan/VulkanContext.h" +#include "VideoCommon/Constants.h" #include "VideoCommon/VideoCommon.h" namespace Vulkan @@ -119,8 +120,8 @@ bool ObjectCache::CreateDescriptorSetLayouts() }}; static const std::array standard_sampler_bindings{{ - {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, static_cast(NUM_PIXEL_SHADER_SAMPLERS), - VK_SHADER_STAGE_FRAGMENT_BIT}, + {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + static_cast(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS), VK_SHADER_STAGE_FRAGMENT_BIT}, }}; // The dynamic veretex loader's vertex buffer must be last here, for similar reasons diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp index 8cee69cfa9..edf57ae3b1 100644 --- a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp +++ b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp @@ -13,6 +13,7 @@ #include "VideoBackends/Vulkan/VKTexture.h" #include "VideoBackends/Vulkan/VKVertexFormat.h" #include "VideoBackends/Vulkan/VulkanContext.h" +#include "VideoCommon/Constants.h" namespace Vulkan { @@ -65,7 +66,7 @@ bool StateTracker::Initialize() VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); // Initialize all samplers to point by default - for (size_t i = 0; i < NUM_PIXEL_SHADER_SAMPLERS; i++) + for (size_t i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) { m_bindings.samplers[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; m_bindings.samplers[i].imageView = m_dummy_texture->GetView(); @@ -498,7 +499,7 @@ void StateTracker::UpdateGXDescriptorSet() m_gx_descriptor_sets[1], 0, 0, - static_cast(NUM_PIXEL_SHADER_SAMPLERS), + static_cast(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS), VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, m_bindings.samplers.data(), nullptr, @@ -602,7 +603,7 @@ void StateTracker::UpdateUtilityDescriptorSet() m_utility_descriptor_sets[1], 0, 0, - NUM_PIXEL_SHADER_SAMPLERS, + NUM_UTILITY_PIXEL_SAMPLERS, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, m_bindings.samplers.data(), nullptr, diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.h b/Source/Core/VideoBackends/Vulkan/StateTracker.h index d90496631c..1113cfc994 100644 --- a/Source/Core/VideoBackends/Vulkan/StateTracker.h +++ b/Source/Core/VideoBackends/Vulkan/StateTracker.h @@ -9,6 +9,7 @@ #include "Common/CommonTypes.h" #include "VideoBackends/Vulkan/Constants.h" +#include "VideoCommon/Constants.h" namespace Vulkan { @@ -141,7 +142,7 @@ private: std::array gx_ubo_offsets; VkDescriptorBufferInfo utility_ubo_binding; u32 utility_ubo_offset; - std::array samplers; + std::array samplers; std::array texel_buffers; VkDescriptorBufferInfo ssbo; VkDescriptorBufferInfo gx_uber_vertex_ssbo; diff --git a/Source/Core/VideoBackends/Vulkan/VKGfx.h b/Source/Core/VideoBackends/Vulkan/VKGfx.h index bc254874fd..97ddcbf585 100644 --- a/Source/Core/VideoBackends/Vulkan/VKGfx.h +++ b/Source/Core/VideoBackends/Vulkan/VKGfx.h @@ -10,6 +10,7 @@ #include "Common/CommonTypes.h" #include "VideoBackends/Vulkan/Constants.h" #include "VideoCommon/AbstractGfx.h" +#include "VideoCommon/Constants.h" namespace Vulkan { @@ -96,6 +97,6 @@ private: float m_backbuffer_scale; // Keep a copy of sampler states to avoid cache lookups every draw - std::array m_sampler_states = {}; + std::array m_sampler_states = {}; }; } // namespace Vulkan diff --git a/Source/Core/VideoCommon/CMakeLists.txt b/Source/Core/VideoCommon/CMakeLists.txt index 036a5c2c24..2737d5a514 100644 --- a/Source/Core/VideoCommon/CMakeLists.txt +++ b/Source/Core/VideoCommon/CMakeLists.txt @@ -23,6 +23,7 @@ add_library(videocommon CommandProcessor.cpp CommandProcessor.h ConstantManager.h + Constants.h CPMemory.cpp CPMemory.h CPUCull.cpp diff --git a/Source/Core/VideoCommon/Constants.h b/Source/Core/VideoCommon/Constants.h new file mode 100644 index 0000000000..8da73e8054 --- /dev/null +++ b/Source/Core/VideoCommon/Constants.h @@ -0,0 +1,11 @@ +// Copyright 2023 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "Common/CommonTypes.h" + +namespace VideoCommon +{ +constexpr u32 MAX_PIXEL_SHADER_SAMPLERS = 8; +}