From b7d412c99bb42291dca616910a269dabca7a3938 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 30 Apr 2019 00:18:28 -0300 Subject: [PATCH] gl_shader_decompiler: Abstract generic attribute operations --- .../renderer_opengl/gl_shader_decompiler.cpp | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index 8df91a4c68..1ad33107d9 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -134,6 +134,19 @@ bool IsPrecise(Node node) { return false; } +constexpr bool IsGenericAttribute(Attribute::Index index) { + return index >= Attribute::Index::Attribute_0 && index <= Attribute::Index::Attribute_31; +} + +constexpr Attribute::Index ToGenericAttribute(u32 value) { + return static_cast(value + static_cast(Attribute::Index::Attribute_0)); +} + +u32 GetGenericAttributeIndex(Attribute::Index index) { + ASSERT(IsGenericAttribute(index)); + return static_cast(index) - static_cast(Attribute::Index::Attribute_0); +} + class GLSLDecompiler final { public: explicit GLSLDecompiler(const Device& device, const ShaderIR& ir, ShaderStage stage, @@ -320,9 +333,7 @@ private: const u32 num_inputs{stage == ShaderStage::Vertex ? GetNumPhysicalAttributes() : GetNumPhysicalVaryings()}; for (u32 i = 0; i < num_inputs; ++i) { - constexpr auto generic_base{static_cast(Attribute::Index::Attribute_0)}; - const auto index{static_cast(generic_base + i)}; - DeclareInputAttribute(index); + DeclareInputAttribute(ToGenericAttribute(i)); } code.AddNewLine(); return; @@ -330,24 +341,22 @@ private: const auto& attributes = ir.GetInputAttributes(); for (const auto index : attributes) { - if (index < Attribute::Index::Attribute_0 || index > Attribute::Index::Attribute_31) { - // Skip when it's not a generic attribute - continue; + if (IsGenericAttribute(index)) { + DeclareInputAttribute(index); } - DeclareInputAttribute(index); } if (!attributes.empty()) code.AddNewLine(); } void DeclareInputAttribute(Attribute::Index index) { - const u32 generic_index{static_cast(index) - - static_cast(Attribute::Index::Attribute_0)}; + const u32 generic_index{GetGenericAttributeIndex(index)}; std::string name{GetInputAttribute(index)}; if (stage == ShaderStage::Geometry) { name = "gs_" + name + "[]"; } + std::string suffix; if (stage == ShaderStage::Fragment) { const auto input_mode{header.ps.GetAttributeUse(generic_index)}; @@ -367,9 +376,7 @@ private: void DeclareOutputAttributes() { if (ir.HasPhysicalAttributes()) { for (u32 i = 0; i < GetNumPhysicalVaryings(); ++i) { - constexpr auto generic_base{static_cast(Attribute::Index::Attribute_0)}; - const auto index{static_cast(generic_base + i)}; - DeclareOutputAttribute(index); + DeclareOutputAttribute(ToGenericAttribute(i)); } code.AddNewLine(); return; @@ -377,20 +384,16 @@ private: const auto& attributes = ir.GetOutputAttributes(); for (const auto index : attributes) { - if (index < Attribute::Index::Attribute_0 || index > Attribute::Index::Attribute_31) { - // Skip when it's not a generic attribute - continue; + if (IsGenericAttribute(index)) { + DeclareOutputAttribute(index); } - DeclareOutputAttribute(index); } if (!attributes.empty()) code.AddNewLine(); } void DeclareOutputAttribute(Attribute::Index index) { - const auto location{static_cast(index) - - static_cast(Attribute::Index::Attribute_0) + - GENERIC_VARYING_START_LOCATION}; + const u32 location{GetGenericAttributeIndex(index) + GENERIC_VARYING_START_LOCATION}; code.AddLine("layout (location = " + std::to_string(location) + ") out vec4 " + GetOutputAttribute(index) + ';'); } @@ -569,8 +572,7 @@ private: UNIMPLEMENTED_MSG("Unmanaged FrontFacing element={}", element); return "0"; default: - if (attribute >= Attribute::Index::Attribute_0 && - attribute <= Attribute::Index::Attribute_31) { + if (IsGenericAttribute(attribute)) { return GeometryPass(GetInputAttribute(attribute)) + GetSwizzle(element); } break; @@ -873,8 +875,7 @@ private: case Attribute::Index::ClipDistances4567: return "gl_ClipDistance[" + std::to_string(abuf->GetElement() + 4) + ']'; default: - if (attribute >= Attribute::Index::Attribute_0 && - attribute <= Attribute::Index::Attribute_31) { + if (IsGenericAttribute(attribute)) { return GetOutputAttribute(attribute) + GetSwizzle(abuf->GetElement()); } UNIMPLEMENTED_MSG("Unhandled output attribute: {}", @@ -1631,15 +1632,11 @@ private: } std::string GetInputAttribute(Attribute::Index attribute) const { - const auto index{static_cast(attribute) - - static_cast(Attribute::Index::Attribute_0)}; - return GetDeclarationWithSuffix(index, "input_attr"); + return GetDeclarationWithSuffix(GetGenericAttributeIndex(attribute), "input_attr"); } std::string GetOutputAttribute(Attribute::Index attribute) const { - const auto index{static_cast(attribute) - - static_cast(Attribute::Index::Attribute_0)}; - return GetDeclarationWithSuffix(index, "output_attr"); + return GetDeclarationWithSuffix(GetGenericAttributeIndex(attribute), "output_attr"); } std::string GetConstBuffer(u32 index) const {