From c028a8c7c5884d1cca63e941300e5bc9b13125d8 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Fri, 28 Oct 2022 16:24:03 +0300 Subject: [PATCH] renderer_vulkan: Remove AttribType * Use VertexAttributeFormat to avoid unnecessary enum casts --- .../renderer_vulkan/vk_pipeline_cache.cpp | 22 ++++++++----------- .../renderer_vulkan/vk_pipeline_cache.h | 6 ++--- .../renderer_vulkan/vk_rasterizer.cpp | 22 ++++++------------- .../renderer_vulkan/vk_shader_gen.cpp | 8 +++---- .../renderer_vulkan/vk_shader_gen.h | 5 ++--- 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp index fb4c781c4..19138a0ba 100644 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp @@ -19,14 +19,12 @@ namespace Vulkan { u32 AttribBytes(VertexAttribute attrib) { switch (attrib.type) { - case AttribType::Float: + case Pica::PipelineRegs::VertexAttributeFormat::FLOAT: return sizeof(float) * attrib.size; - case AttribType::Int: - return sizeof(u32) * attrib.size; - case AttribType::Short: + case Pica::PipelineRegs::VertexAttributeFormat::SHORT: return sizeof(u16) * attrib.size; - case AttribType::Byte: - case AttribType::Ubyte: + case Pica::PipelineRegs::VertexAttributeFormat::BYTE: + case Pica::PipelineRegs::VertexAttributeFormat::UBYTE: return sizeof(u8) * attrib.size; } @@ -35,16 +33,14 @@ u32 AttribBytes(VertexAttribute attrib) { vk::Format ToVkAttributeFormat(VertexAttribute attrib) { constexpr std::array attribute_formats = { - std::array{vk::Format::eR32Sfloat, vk::Format::eR32G32Sfloat, vk::Format::eR32G32B32Sfloat, - vk::Format::eR32G32B32A32Sfloat}, - std::array{vk::Format::eR32Sint, vk::Format::eR32G32Sint, vk::Format::eR32G32B32Sint, - vk::Format::eR32G32B32A32Sint}, - std::array{vk::Format::eR16Sint, vk::Format::eR16G16Sint, vk::Format::eR16G16B16Sint, - vk::Format::eR16G16B16A16Sint}, std::array{vk::Format::eR8Sint, vk::Format::eR8G8Sint, vk::Format::eR8G8B8Sint, vk::Format::eR8G8B8A8Sint}, std::array{vk::Format::eR8Uint, vk::Format::eR8G8Uint, vk::Format::eR8G8B8Uint, - vk::Format::eR8G8B8A8Uint}}; + vk::Format::eR8G8B8A8Uint}, + std::array{vk::Format::eR16Sint, vk::Format::eR16G16Sint, vk::Format::eR16G16B16Sint, + vk::Format::eR16G16B16A16Sint}, + std::array{vk::Format::eR32Sfloat, vk::Format::eR32G32Sfloat, vk::Format::eR32G32B32Sfloat, + vk::Format::eR32G32B32A32Sfloat}}; ASSERT(attrib.size <= 4); return attribute_formats[static_cast(attrib.type.Value())][attrib.size.Value() - 1]; diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.h b/src/video_core/renderer_vulkan/vk_pipeline_cache.h index 5f0695306..368847548 100644 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.h +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.h @@ -41,8 +41,6 @@ struct DepthStencilState { BitField<12, 3, Pica::FramebufferRegs::StencilAction> stencil_depth_fail_op; BitField<15, 3, Pica::FramebufferRegs::CompareFunc> stencil_compare_op; }; - - // These are dynamic state so keep them separate u8 stencil_reference; u8 stencil_compare_mask; u8 stencil_write_mask; @@ -72,7 +70,7 @@ union VertexAttribute { u32 value = 0; BitField<0, 4, u32> binding; BitField<4, 4, u32> location; - BitField<8, 3, AttribType> type; + BitField<8, 3, Pica::PipelineRegs::VertexAttributeFormat> type; BitField<11, 3, u32> size; BitField<14, 11, u32> offset; }; @@ -95,7 +93,7 @@ struct PipelineInfo { RasterizationState rasterization{}; DepthStencilState depth_stencil{}; - bool IsDepthWriteEnabled() const { + [[nodiscard]] bool IsDepthWriteEnabled() const noexcept { const bool has_stencil = depth_attachment == VideoCore::PixelFormat::D24S8; const bool depth_write = depth_stencil.depth_test_enable && depth_stencil.depth_write_enable; diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp index 74ccb7c98..b41df72b6 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp +++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp @@ -8,6 +8,7 @@ #include "common/microprofile.h" #include "video_core/pica_state.h" #include "video_core/regs_framebuffer.h" +#include "video_core/regs_pipeline.h" #include "video_core/regs_rasterizer.h" #include "video_core/renderer_vulkan/pica_to_vk.h" #include "video_core/renderer_vulkan/renderer_vulkan.h" @@ -175,13 +176,6 @@ void RasterizerVulkan::SyncFixedState() { SyncDepthWriteMask(); } -static constexpr std::array vs_attrib_types = { - AttribType::Byte, // VertexAttributeFormat::BYTE - AttribType::Ubyte, // VertexAttributeFormat::UBYTE - AttribType::Short, // VertexAttributeFormat::SHORT - AttribType::Float // VertexAttributeFormat::FLOAT -}; - void RasterizerVulkan::SetupVertexArray(u32 vs_input_size, u32 vs_input_index_min, u32 vs_input_index_max) { auto [array_ptr, array_offset, invalidate] = vertex_buffer.Map(vs_input_size, 4); @@ -215,16 +209,14 @@ void RasterizerVulkan::SetupVertexArray(u32 vs_input_size, u32 vs_input_index_mi offset, vertex_attributes.GetElementSizeInBytes(attribute_index)); const u32 input_reg = regs.vs.GetRegisterForAttribute(attribute_index); - const u32 attrib_format = - static_cast(vertex_attributes.GetFormat(attribute_index)); - const AttribType type = vs_attrib_types[attrib_format]; + const Pica::PipelineRegs::VertexAttributeFormat format = + vertex_attributes.GetFormat(attribute_index); - // Define the attribute VertexAttribute& attribute = layout.attributes[layout.attribute_count++]; attribute.binding.Assign(layout.binding_count); attribute.location.Assign(input_reg); attribute.offset.Assign(offset); - attribute.type.Assign(type); + attribute.type.Assign(format); attribute.size.Assign(size); enable_attributes[input_reg] = true; @@ -282,7 +274,7 @@ void RasterizerVulkan::SetupVertexArray(u32 vs_input_size, u32 vs_input_index_mi attribute.binding.Assign(layout.binding_count); attribute.location.Assign(reg); attribute.offset.Assign(offset); - attribute.type.Assign(AttribType::Float); + attribute.type.Assign(Pica::PipelineRegs::VertexAttributeFormat::FLOAT); attribute.size.Assign(4); offset += data_size; @@ -304,7 +296,7 @@ void RasterizerVulkan::SetupVertexArray(u32 vs_input_size, u32 vs_input_index_mi attribute.binding.Assign(layout.binding_count); attribute.location.Assign(i); attribute.offset.Assign(0); - attribute.type.Assign(AttribType::Float); + attribute.type.Assign(Pica::PipelineRegs::VertexAttributeFormat::FLOAT); attribute.size.Assign(4); } } @@ -1481,7 +1473,7 @@ void RasterizerVulkan::MakeSoftwareVertexLayout() { attribute.binding.Assign(0); attribute.location.Assign(i); attribute.offset.Assign(offset); - attribute.type.Assign(AttribType::Float); + attribute.type.Assign(Pica::PipelineRegs::VertexAttributeFormat::FLOAT); attribute.size.Assign(sizes[i]); offset += sizes[i] * sizeof(float); } diff --git a/src/video_core/renderer_vulkan/vk_shader_gen.cpp b/src/video_core/renderer_vulkan/vk_shader_gen.cpp index 77c1bb349..54deaea64 100644 --- a/src/video_core/renderer_vulkan/vk_shader_gen.cpp +++ b/src/video_core/renderer_vulkan/vk_shader_gen.cpp @@ -1627,14 +1627,14 @@ layout (set = 0, binding = 0, std140) uniform vs_config { if (used_regs[i]) { std::string_view prefix; switch (config.state.attrib_types[i]) { - case AttribType::Float: + case Pica::PipelineRegs::VertexAttributeFormat::FLOAT: prefix = ""; break; - case AttribType::Byte: - case AttribType::Short: + case Pica::PipelineRegs::VertexAttributeFormat::BYTE: + case Pica::PipelineRegs::VertexAttributeFormat::SHORT: prefix = "i"; break; - case AttribType::Ubyte: + case Pica::PipelineRegs::VertexAttributeFormat::UBYTE: prefix = "u"; break; default: diff --git a/src/video_core/renderer_vulkan/vk_shader_gen.h b/src/video_core/renderer_vulkan/vk_shader_gen.h index 336fa274e..ae5531855 100644 --- a/src/video_core/renderer_vulkan/vk_shader_gen.h +++ b/src/video_core/renderer_vulkan/vk_shader_gen.h @@ -8,12 +8,11 @@ #include #include "common/hash.h" #include "video_core/regs.h" +#include "video_core/regs_pipeline.h" #include "video_core/shader/shader.h" namespace Vulkan { -enum class AttribType : u32 { Float = 0, Int = 1, Short = 2, Byte = 3, Ubyte = 4 }; - enum Attributes { ATTRIBUTE_POSITION, ATTRIBUTE_COLOR, @@ -149,7 +148,7 @@ struct PicaShaderConfigCommon { u64 swizzle_hash; u32 main_offset; bool sanitize_mul; - std::array attrib_types; + std::array attrib_types; u32 num_outputs;