From b04c7b63435ad51ce747d678be3fe5899f14609e Mon Sep 17 00:00:00 2001 From: vonchenplus Date: Tue, 26 Oct 2021 23:01:57 +0800 Subject: [PATCH 1/5] Support gl_BackColor attribute --- .../backend/spirv/emit_context.cpp | 16 ++++++++++++++++ .../backend/spirv/emit_context.h | 2 ++ .../backend/spirv/emit_spirv_context_get_set.cpp | 15 +++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 2885e6799c..9d84c4fc62 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1286,6 +1286,14 @@ void EmitContext::DefineInputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, location); input_front_color = id; } + if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { + const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); + previous_unused_location = location; + used_locations.set(location); + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, location); + input_back_color = id; + } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { const size_t location = @@ -1363,6 +1371,14 @@ void EmitContext::DefineOutputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, static_cast(location)); output_front_color = id; } + if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { + const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); + previous_unused_location = location; + used_locations.set(location); + const Id id{DefineOutput(*this, F32[4], invocations)}; + Decorate(id, spv::Decoration::Location, static_cast(location)); + output_back_color = id; + } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { const size_t location = diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index 847d0c0e64..d2bc0a952f 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h @@ -269,12 +269,14 @@ public: Id input_position{}; Id input_front_color{}; + Id input_back_color{}; std::array input_fixed_fnc_textures{}; std::array input_generics{}; Id output_point_size{}; Id output_position{}; Id output_front_color{}; + Id output_back_color{}; std::array output_fixed_fnc_textures{}; std::array, 32> output_generics{}; diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 6f60c65747..899df62941 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -119,6 +119,14 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { const Id element_id{ctx.Const(element)}; return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_color, element_id); } + case IR::Attribute::ColorBackDiffuseR: + case IR::Attribute::ColorBackDiffuseG: + case IR::Attribute::ColorBackDiffuseB: + case IR::Attribute::ColorBackDiffuseA: { + const u32 element{static_cast(attr) % 4}; + const Id element_id{ctx.Const(element)}; + return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_color, element_id); + } case IR::Attribute::ClipDistance0: case IR::Attribute::ClipDistance1: case IR::Attribute::ClipDistance2: @@ -363,6 +371,13 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_color, ctx.Const(element))); } + case IR::Attribute::ColorBackDiffuseR: + case IR::Attribute::ColorBackDiffuseG: + case IR::Attribute::ColorBackDiffuseB: + case IR::Attribute::ColorBackDiffuseA: { + return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_color, + ctx.Const(element))); + } case IR::Attribute::InstanceId: if (ctx.profile.support_vertex_instance_id) { return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id)); From 92bebecf4632fb5435d74a77d45bf56535eb85e4 Mon Sep 17 00:00:00 2001 From: vonchenplus Date: Tue, 26 Oct 2021 23:09:44 +0800 Subject: [PATCH 2/5] Support gl_FrontSecondaryColor attribute --- .../backend/spirv/emit_context.cpp | 16 ++++++++++++++++ .../backend/spirv/emit_context.h | 2 ++ .../backend/spirv/emit_spirv_context_get_set.cpp | 15 +++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 9d84c4fc62..7726bc9c6d 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1286,6 +1286,14 @@ void EmitContext::DefineInputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, location); input_front_color = id; } + if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { + const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); + previous_unused_location = location; + used_locations.set(location); + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, location); + input_front_secondary_color = id; + } if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); previous_unused_location = location; @@ -1371,6 +1379,14 @@ void EmitContext::DefineOutputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, static_cast(location)); output_front_color = id; } + if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { + const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); + previous_unused_location = location; + used_locations.set(location); + const Id id{DefineOutput(*this, F32[4], invocations)}; + Decorate(id, spv::Decoration::Location, static_cast(location)); + output_front_secondary_color = id; + } if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); previous_unused_location = location; diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index d2bc0a952f..8818269a55 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h @@ -269,6 +269,7 @@ public: Id input_position{}; Id input_front_color{}; + Id input_front_secondary_color{}; Id input_back_color{}; std::array input_fixed_fnc_textures{}; std::array input_generics{}; @@ -276,6 +277,7 @@ public: Id output_point_size{}; Id output_position{}; Id output_front_color{}; + Id output_front_secondary_color{}; Id output_back_color{}; std::array output_fixed_fnc_textures{}; std::array, 32> output_generics{}; diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 899df62941..2da76493a7 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -119,6 +119,14 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { const Id element_id{ctx.Const(element)}; return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_color, element_id); } + case IR::Attribute::ColorFrontSpecularR: + case IR::Attribute::ColorFrontSpecularG: + case IR::Attribute::ColorFrontSpecularB: + case IR::Attribute::ColorFrontSpecularA: { + const u32 element{static_cast(attr) % 4}; + const Id element_id{ctx.Const(element)}; + return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_secondary_color, element_id); + } case IR::Attribute::ColorBackDiffuseR: case IR::Attribute::ColorBackDiffuseG: case IR::Attribute::ColorBackDiffuseB: @@ -371,6 +379,13 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_color, ctx.Const(element))); } + case IR::Attribute::ColorFrontSpecularR: + case IR::Attribute::ColorFrontSpecularG: + case IR::Attribute::ColorFrontSpecularB: + case IR::Attribute::ColorFrontSpecularA: { + return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_secondary_color, + ctx.Const(element))); + } case IR::Attribute::ColorBackDiffuseR: case IR::Attribute::ColorBackDiffuseG: case IR::Attribute::ColorBackDiffuseB: From 36c21ff6cbffd55ba9314314ee8f414181fd4f9b Mon Sep 17 00:00:00 2001 From: vonchenplus Date: Tue, 26 Oct 2021 23:14:40 +0800 Subject: [PATCH 3/5] Support gl_BackSecondaryColor attribute --- .../backend/spirv/emit_context.cpp | 16 ++++++++++++++++ .../backend/spirv/emit_context.h | 2 ++ .../backend/spirv/emit_spirv_context_get_set.cpp | 15 +++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 7726bc9c6d..d60ad139d5 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1302,6 +1302,14 @@ void EmitContext::DefineInputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, location); input_back_color = id; } + if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) { + const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); + previous_unused_location = location; + used_locations.set(location); + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, location); + input_back_secondary_color = id; + } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { const size_t location = @@ -1395,6 +1403,14 @@ void EmitContext::DefineOutputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, static_cast(location)); output_back_color = id; } + if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) { + const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); + previous_unused_location = location; + used_locations.set(location); + const Id id{DefineOutput(*this, F32[4], invocations)}; + Decorate(id, spv::Decoration::Location, static_cast(location)); + output_back_secondary_color = id; + } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { const size_t location = diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index 8818269a55..a08622099a 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h @@ -271,6 +271,7 @@ public: Id input_front_color{}; Id input_front_secondary_color{}; Id input_back_color{}; + Id input_back_secondary_color{}; std::array input_fixed_fnc_textures{}; std::array input_generics{}; @@ -279,6 +280,7 @@ public: Id output_front_color{}; Id output_front_secondary_color{}; Id output_back_color{}; + Id output_back_secondary_color{}; std::array output_fixed_fnc_textures{}; std::array, 32> output_generics{}; diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 2da76493a7..2d1545851b 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -135,6 +135,14 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { const Id element_id{ctx.Const(element)}; return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_color, element_id); } + case IR::Attribute::ColorBackSpecularR: + case IR::Attribute::ColorBackSpecularG: + case IR::Attribute::ColorBackSpecularB: + case IR::Attribute::ColorBackSpecularA: { + const u32 element{static_cast(attr) % 4}; + const Id element_id{ctx.Const(element)}; + return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_secondary_color, element_id); + } case IR::Attribute::ClipDistance0: case IR::Attribute::ClipDistance1: case IR::Attribute::ClipDistance2: @@ -393,6 +401,13 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_color, ctx.Const(element))); } + case IR::Attribute::ColorBackSpecularR: + case IR::Attribute::ColorBackSpecularG: + case IR::Attribute::ColorBackSpecularB: + case IR::Attribute::ColorBackSpecularA: { + return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_secondary_color, + ctx.Const(element))); + } case IR::Attribute::InstanceId: if (ctx.profile.support_vertex_instance_id) { return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id)); From 49f9a442356d582d31832e79098fd6db22f0f4c3 Mon Sep 17 00:00:00 2001 From: vonchenplus Date: Sun, 31 Oct 2021 22:02:05 +0800 Subject: [PATCH 4/5] Support gl_FogFragCoord attribute --- .../backend/spirv/emit_context.cpp | 85 +++++++++---------- .../backend/spirv/emit_context.h | 2 + .../spirv/emit_spirv_context_get_set.cpp | 19 ++++- 3 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index d60ad139d5..386c14ce51 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -430,10 +430,12 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) { } } -size_t FindNextUnusedLocation(const std::bitset& used_locations, - size_t start_offset) { +size_t FindAndSetNextUnusedLocation(std::bitset& used_locations, + size_t& start_offset) { for (size_t location = start_offset; location < used_locations.size(); ++location) { if (!used_locations.test(location)) { + start_offset = location; + used_locations.set(location); return location; } } @@ -1279,45 +1281,40 @@ void EmitContext::DefineInputs(const IR::Program& program) { } size_t previous_unused_location = 0; if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); input_front_color = id; } if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); input_front_secondary_color = id; } if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); input_back_color = id; } if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); input_back_secondary_color = id; } + if (loads.AnyComponent(IR::Attribute::FogCoordinate)) { + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); + input_fog_frag_coord = id; + } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - const size_t location = - FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); input_fixed_fnc_textures[index] = id; } } @@ -1380,45 +1377,45 @@ void EmitContext::DefineOutputs(const IR::Program& program) { } size_t previous_unused_location = 0; if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(location)); + Decorate(id, spv::Decoration::Location, + static_cast( + FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); output_front_color = id; } if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(location)); + Decorate(id, spv::Decoration::Location, + static_cast( + FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); output_front_secondary_color = id; } if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(location)); + Decorate(id, spv::Decoration::Location, + static_cast( + FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); output_back_color = id; } if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) { - const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(location)); + Decorate(id, spv::Decoration::Location, + static_cast( + FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); output_back_secondary_color = id; } + if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) { + const Id id{DefineOutput(*this, F32[4], invocations)}; + Decorate(id, spv::Decoration::Location, + static_cast( + FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); + output_fog_frag_coord = id; + } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - const size_t location = - FindNextUnusedLocation(used_locations, previous_unused_location); - previous_unused_location = location; - used_locations.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, + FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); output_fixed_fnc_textures[index] = id; } } diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index a08622099a..e1af12a38a 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h @@ -272,6 +272,7 @@ public: Id input_front_secondary_color{}; Id input_back_color{}; Id input_back_secondary_color{}; + Id input_fog_frag_coord{}; std::array input_fixed_fnc_textures{}; std::array input_generics{}; @@ -281,6 +282,7 @@ public: Id output_front_secondary_color{}; Id output_back_color{}; Id output_back_secondary_color{}; + Id output_fog_frag_coord{}; std::array output_fixed_fnc_textures{}; std::array, 32> output_generics{}; diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 2d1545851b..6fa99cc1a5 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -143,6 +143,11 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { const Id element_id{ctx.Const(element)}; return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_secondary_color, element_id); } + case IR::Attribute::FogCoordinate: { + const u32 element{static_cast(attr) % 4}; + const Id element_id{ctx.Const(element)}; + return OutputAccessChain(ctx, ctx.output_f32, ctx.output_fog_frag_coord, element_id); + } case IR::Attribute::ClipDistance0: case IR::Attribute::ClipDistance1: case IR::Attribute::ClipDistance2: @@ -391,8 +396,9 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { case IR::Attribute::ColorFrontSpecularG: case IR::Attribute::ColorFrontSpecularB: case IR::Attribute::ColorFrontSpecularA: { - return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_secondary_color, - ctx.Const(element))); + return ctx.OpLoad(ctx.F32[1], + AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_secondary_color, + ctx.Const(element))); } case IR::Attribute::ColorBackDiffuseR: case IR::Attribute::ColorBackDiffuseG: @@ -405,8 +411,13 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { case IR::Attribute::ColorBackSpecularG: case IR::Attribute::ColorBackSpecularB: case IR::Attribute::ColorBackSpecularA: { - return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_secondary_color, - ctx.Const(element))); + return ctx.OpLoad(ctx.F32[1], + AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_secondary_color, + ctx.Const(element))); + } + case IR::Attribute::FogCoordinate: { + return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, + ctx.input_fog_frag_coord, ctx.Const(element))); } case IR::Attribute::InstanceId: if (ctx.profile.support_vertex_instance_id) { From f2a420424546fbc031594c5927b553b9d928b3a4 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Mon, 1 Nov 2021 12:00:13 +0800 Subject: [PATCH 5/5] Simply legacy attribute implement --- .../backend/spirv/emit_context.cpp | 148 +++++++++++------- .../backend/spirv/emit_context.h | 3 + .../spirv/emit_spirv_context_get_set.cpp | 126 ++++----------- 3 files changed, 125 insertions(+), 152 deletions(-) diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 386c14ce51..3c84e64666 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -441,6 +441,22 @@ size_t FindAndSetNextUnusedLocation(std::bitset& used_location } throw RuntimeError("Unable to get an unused location for legacy attribute"); } + +Id DefineLegacyInput(EmitContext& ctx, std::bitset& used_locations, + size_t& start_offset) { + const Id id{DefineInput(ctx, ctx.F32[4], true)}; + const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset); + ctx.Decorate(id, spv::Decoration::Location, location); + return id; +} + +Id DefineLegacyOutput(EmitContext& ctx, std::bitset& used_locations, + size_t& start_offset, std::optional invocations) { + const Id id{DefineOutput(ctx, ctx.F32[4], invocations)}; + const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset); + ctx.Decorate(id, spv::Decoration::Location, location); + return id; +} } // Anonymous namespace void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) { @@ -522,6 +538,64 @@ Id EmitContext::BitOffset16(const IR::Value& offset) { return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u)); } +Id EmitContext::InputLegacyAttribute(IR::Attribute attribute) { + if (attribute >= IR::Attribute::ColorFrontDiffuseR && + attribute <= IR::Attribute::ColorFrontDiffuseA) { + return input_front_color; + } + if (attribute >= IR::Attribute::ColorFrontSpecularR && + attribute <= IR::Attribute::ColorFrontSpecularA) { + return input_front_secondary_color; + } + if (attribute >= IR::Attribute::ColorBackDiffuseR && + attribute <= IR::Attribute::ColorBackDiffuseA) { + return input_back_color; + } + if (attribute >= IR::Attribute::ColorBackSpecularR && + attribute <= IR::Attribute::ColorBackSpecularA) { + return input_back_secondary_color; + } + if (attribute == IR::Attribute::FogCoordinate) { + return input_fog_frag_coord; + } + if (attribute >= IR::Attribute::FixedFncTexture0S && + attribute <= IR::Attribute::FixedFncTexture9Q) { + u32 index = + (static_cast(attribute) - static_cast(IR::Attribute::FixedFncTexture0S)) / 4; + return input_fixed_fnc_textures[index]; + } + throw InvalidArgument("Attribute is not legacy attribute {}", attribute); +} + +Id EmitContext::OutputLegacyAttribute(IR::Attribute attribute) { + if (attribute >= IR::Attribute::ColorFrontDiffuseR && + attribute <= IR::Attribute::ColorFrontDiffuseA) { + return output_front_color; + } + if (attribute >= IR::Attribute::ColorFrontSpecularR && + attribute <= IR::Attribute::ColorFrontSpecularA) { + return output_front_secondary_color; + } + if (attribute >= IR::Attribute::ColorBackDiffuseR && + attribute <= IR::Attribute::ColorBackDiffuseA) { + return output_back_color; + } + if (attribute >= IR::Attribute::ColorBackSpecularR && + attribute <= IR::Attribute::ColorBackSpecularA) { + return output_back_secondary_color; + } + if (attribute == IR::Attribute::FogCoordinate) { + return output_fog_frag_coord; + } + if (attribute >= IR::Attribute::FixedFncTexture0S && + attribute <= IR::Attribute::FixedFncTexture9Q) { + u32 index = + (static_cast(attribute) - static_cast(IR::Attribute::FixedFncTexture0S)) / 4; + return output_fixed_fnc_textures[index]; + } + throw InvalidArgument("Attribute is not legacy attribute {}", attribute); +} + void EmitContext::DefineCommonTypes(const Info& info) { void_id = TypeVoid(); @@ -1281,41 +1355,26 @@ void EmitContext::DefineInputs(const IR::Program& program) { } size_t previous_unused_location = 0; if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - input_front_color = id; + input_front_color = DefineLegacyInput(*this, used_locations, previous_unused_location); } if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - input_front_secondary_color = id; + input_front_secondary_color = + DefineLegacyInput(*this, used_locations, previous_unused_location); } if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - input_back_color = id; + input_back_color = DefineLegacyInput(*this, used_locations, previous_unused_location); } if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - input_back_secondary_color = id; + input_back_secondary_color = + DefineLegacyInput(*this, used_locations, previous_unused_location); } if (loads.AnyComponent(IR::Attribute::FogCoordinate)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - input_fog_frag_coord = id; + input_fog_frag_coord = DefineLegacyInput(*this, used_locations, previous_unused_location); } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - input_fixed_fnc_textures[index] = id; + input_fixed_fnc_textures[index] = + DefineLegacyInput(*this, used_locations, previous_unused_location); } } if (stage == Stage::TessellationEval) { @@ -1377,46 +1436,29 @@ void EmitContext::DefineOutputs(const IR::Program& program) { } size_t previous_unused_location = 0; if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, - static_cast( - FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); - output_front_color = id; + output_front_color = + DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations); } if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, - static_cast( - FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); - output_front_secondary_color = id; + output_front_secondary_color = + DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations); } if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, - static_cast( - FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); - output_back_color = id; + output_back_color = + DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations); } if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, - static_cast( - FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); - output_back_secondary_color = id; + output_back_secondary_color = + DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations); } if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, - static_cast( - FindAndSetNextUnusedLocation(used_locations, previous_unused_location))); - output_fog_frag_coord = id; + output_fog_frag_coord = + DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations); } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, - FindAndSetNextUnusedLocation(used_locations, previous_unused_location)); - output_fixed_fnc_textures[index] = id; + output_fixed_fnc_textures[index] = + DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations); } } switch (stage) { diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index e1af12a38a..112c52382a 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h @@ -113,6 +113,9 @@ public: [[nodiscard]] Id BitOffset8(const IR::Value& offset); [[nodiscard]] Id BitOffset16(const IR::Value& offset); + Id InputLegacyAttribute(IR::Attribute attribute); + Id OutputLegacyAttribute(IR::Attribute attribute); + Id Const(u32 value) { return Constant(U32[1], value); } diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 6fa99cc1a5..d3a93d5f43 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -43,23 +43,12 @@ Id AttrPointer(EmitContext& ctx, Id pointer_type, Id vertex, Id base, Args&&... } } -bool IsFixedFncTexture(IR::Attribute attribute) { - return attribute >= IR::Attribute::FixedFncTexture0S && - attribute <= IR::Attribute::FixedFncTexture9Q; -} - -u32 FixedFncTextureAttributeIndex(IR::Attribute attribute) { - if (!IsFixedFncTexture(attribute)) { - throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute); - } - return (static_cast(attribute) - static_cast(IR::Attribute::FixedFncTexture0S)) / 4u; -} - -u32 FixedFncTextureAttributeElement(IR::Attribute attribute) { - if (!IsFixedFncTexture(attribute)) { - throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute); - } - return static_cast(attribute) % 4u; +bool IsLegacyAttribute(IR::Attribute attribute) { + return (attribute >= IR::Attribute::ColorFrontDiffuseR && + attribute <= IR::Attribute::ColorBackSpecularA) || + attribute == IR::Attribute::FogCoordinate || + (attribute >= IR::Attribute::FixedFncTexture0S && + attribute <= IR::Attribute::FixedFncTexture9Q); } template @@ -93,12 +82,16 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id); } } - if (IsFixedFncTexture(attr)) { - const u32 index{FixedFncTextureAttributeIndex(attr)}; - const u32 element{FixedFncTextureAttributeElement(attr)}; - const Id element_id{ctx.Const(element)}; - return OutputAccessChain(ctx, ctx.output_f32, ctx.output_fixed_fnc_textures[index], - element_id); + if (IsLegacyAttribute(attr)) { + if (attr == IR::Attribute::FogCoordinate) { + return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr), + ctx.Const(0u)); + } else { + const u32 element{static_cast(attr) % 4}; + const Id element_id{ctx.Const(element)}; + return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr), + element_id); + } } switch (attr) { case IR::Attribute::PointSize: @@ -111,43 +104,6 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { const Id element_id{ctx.Const(element)}; return OutputAccessChain(ctx, ctx.output_f32, ctx.output_position, element_id); } - case IR::Attribute::ColorFrontDiffuseR: - case IR::Attribute::ColorFrontDiffuseG: - case IR::Attribute::ColorFrontDiffuseB: - case IR::Attribute::ColorFrontDiffuseA: { - const u32 element{static_cast(attr) % 4}; - const Id element_id{ctx.Const(element)}; - return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_color, element_id); - } - case IR::Attribute::ColorFrontSpecularR: - case IR::Attribute::ColorFrontSpecularG: - case IR::Attribute::ColorFrontSpecularB: - case IR::Attribute::ColorFrontSpecularA: { - const u32 element{static_cast(attr) % 4}; - const Id element_id{ctx.Const(element)}; - return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_secondary_color, element_id); - } - case IR::Attribute::ColorBackDiffuseR: - case IR::Attribute::ColorBackDiffuseG: - case IR::Attribute::ColorBackDiffuseB: - case IR::Attribute::ColorBackDiffuseA: { - const u32 element{static_cast(attr) % 4}; - const Id element_id{ctx.Const(element)}; - return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_color, element_id); - } - case IR::Attribute::ColorBackSpecularR: - case IR::Attribute::ColorBackSpecularG: - case IR::Attribute::ColorBackSpecularB: - case IR::Attribute::ColorBackSpecularA: { - const u32 element{static_cast(attr) % 4}; - const Id element_id{ctx.Const(element)}; - return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_secondary_color, element_id); - } - case IR::Attribute::FogCoordinate: { - const u32 element{static_cast(attr) % 4}; - const Id element_id{ctx.Const(element)}; - return OutputAccessChain(ctx, ctx.output_f32, ctx.output_fog_frag_coord, element_id); - } case IR::Attribute::ClipDistance0: case IR::Attribute::ClipDistance1: case IR::Attribute::ClipDistance2: @@ -370,11 +326,17 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { const Id value{ctx.OpLoad(type->id, pointer)}; return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value; } - if (IsFixedFncTexture(attr)) { - const u32 index{FixedFncTextureAttributeIndex(attr)}; - const Id attr_id{ctx.input_fixed_fnc_textures[index]}; - const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex, attr_id, ctx.Const(element))}; - return ctx.OpLoad(ctx.F32[1], attr_ptr); + if (IsLegacyAttribute(attr)) { + if (attr == IR::Attribute::FogCoordinate) { + const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex, + ctx.InputLegacyAttribute(attr), ctx.Const(0u))}; + return ctx.OpLoad(ctx.F32[1], attr_ptr); + } else { + const Id element_id{ctx.Const(element)}; + const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex, + ctx.InputLegacyAttribute(attr), element_id)}; + return ctx.OpLoad(ctx.F32[1], attr_ptr); + } } switch (attr) { case IR::Attribute::PrimitiveId: @@ -385,40 +347,6 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { case IR::Attribute::PositionW: return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position, ctx.Const(element))); - case IR::Attribute::ColorFrontDiffuseR: - case IR::Attribute::ColorFrontDiffuseG: - case IR::Attribute::ColorFrontDiffuseB: - case IR::Attribute::ColorFrontDiffuseA: { - return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_color, - ctx.Const(element))); - } - case IR::Attribute::ColorFrontSpecularR: - case IR::Attribute::ColorFrontSpecularG: - case IR::Attribute::ColorFrontSpecularB: - case IR::Attribute::ColorFrontSpecularA: { - return ctx.OpLoad(ctx.F32[1], - AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_secondary_color, - ctx.Const(element))); - } - case IR::Attribute::ColorBackDiffuseR: - case IR::Attribute::ColorBackDiffuseG: - case IR::Attribute::ColorBackDiffuseB: - case IR::Attribute::ColorBackDiffuseA: { - return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_color, - ctx.Const(element))); - } - case IR::Attribute::ColorBackSpecularR: - case IR::Attribute::ColorBackSpecularG: - case IR::Attribute::ColorBackSpecularB: - case IR::Attribute::ColorBackSpecularA: { - return ctx.OpLoad(ctx.F32[1], - AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_secondary_color, - ctx.Const(element))); - } - case IR::Attribute::FogCoordinate: { - return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, - ctx.input_fog_frag_coord, ctx.Const(element))); - } case IR::Attribute::InstanceId: if (ctx.profile.support_vertex_instance_id) { return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id));