From 73b11f390e3dabf42d22af78670c8aa6d8f52cee Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Wed, 1 Sep 2021 00:07:25 +0800 Subject: Add colorfront and txtcoord support --- src/shader_recompiler/backend/spirv/emit_context.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 2d29d8c14..89c75c52d 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1201,6 +1201,12 @@ void EmitContext::DefineInputs(const IR::Program& program) { } } } + if (loads.AllComponents(IR::Attribute::ColorFrontDiffuseR)) { + input_front_color = DefineInput(*this, F32[4], true); + } + if (loads.AllComponents(IR::Attribute::FixedFncTexture0S)) { + input_txt_coord = DefineInput(*this, F32[4], true); + } if (loads[IR::Attribute::InstanceId]) { if (profile.support_vertex_instance_id) { instance_id = DefineInput(*this, U32[1], true, spv::BuiltIn::InstanceId); @@ -1282,6 +1288,9 @@ void EmitContext::DefineOutputs(const IR::Program& program) { if (info.stores.AnyComponent(IR::Attribute::PositionX) || stage == Stage::VertexB) { output_position = DefineOutput(*this, F32[4], invocations, spv::BuiltIn::Position); } + if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR) || stage == Stage::VertexB) { + output_front_color = DefineOutput(*this, F32[4], invocations); + } if (info.stores[IR::Attribute::PointSize] || runtime_info.fixed_state_point_size) { if (stage == Stage::Fragment) { throw NotImplementedException("Storing PointSize in fragment stage"); @@ -1313,6 +1322,11 @@ void EmitContext::DefineOutputs(const IR::Program& program) { viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt, spv::BuiltIn::ViewportMaskNV); } + + if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S)) { + output_txt_coord = DefineOutput(*this, F32[4], invocations); + } + for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { if (info.stores.Generic(index)) { DefineGenericOutput(*this, index, invocations); -- cgit v1.2.3 From 1e2a89d3061bbee3c73cd55fb8d580b56aabacec Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Thu, 2 Sep 2021 23:34:51 +0800 Subject: Add input/output location --- src/shader_recompiler/backend/spirv/emit_context.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 89c75c52d..ff0501b76 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1202,10 +1202,14 @@ void EmitContext::DefineInputs(const IR::Program& program) { } } if (loads.AllComponents(IR::Attribute::ColorFrontDiffuseR)) { - input_front_color = DefineInput(*this, F32[4], true); + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, static_cast(55)); + input_front_color = id; } - if (loads.AllComponents(IR::Attribute::FixedFncTexture0S)) { - input_txt_coord = DefineInput(*this, F32[4], true); + if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S)) { + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, static_cast(56)); + input_txt_coord = id; } if (loads[IR::Attribute::InstanceId]) { if (profile.support_vertex_instance_id) { @@ -1289,7 +1293,9 @@ void EmitContext::DefineOutputs(const IR::Program& program) { output_position = DefineOutput(*this, F32[4], invocations, spv::BuiltIn::Position); } if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR) || stage == Stage::VertexB) { - output_front_color = DefineOutput(*this, F32[4], invocations); + const Id id{DefineOutput(*this, F32[4], invocations)}; + Decorate(id, spv::Decoration::Location, static_cast(55)); + output_front_color = id; } if (info.stores[IR::Attribute::PointSize] || runtime_info.fixed_state_point_size) { if (stage == Stage::Fragment) { @@ -1324,7 +1330,9 @@ void EmitContext::DefineOutputs(const IR::Program& program) { } if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S)) { - output_txt_coord = DefineOutput(*this, F32[4], invocations); + const Id id{DefineOutput(*this, F32[4], invocations)}; + Decorate(id, spv::Decoration::Location, static_cast(56)); + output_txt_coord = id; } for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { -- cgit v1.2.3 From cf26f375ff2bfc84445270cf1077504ab4cc41f6 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Fri, 3 Sep 2021 22:55:53 +0800 Subject: Fix create GraphicsPipelines crash --- src/shader_recompiler/backend/spirv/emit_context.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index ff0501b76..78ca97d91 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1201,14 +1201,14 @@ void EmitContext::DefineInputs(const IR::Program& program) { } } } - if (loads.AllComponents(IR::Attribute::ColorFrontDiffuseR)) { + if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, static_cast(55)); + Decorate(id, spv::Decoration::Location, static_cast(11)); input_front_color = id; } if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S)) { const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, static_cast(56)); + Decorate(id, spv::Decoration::Location, static_cast(12)); input_txt_coord = id; } if (loads[IR::Attribute::InstanceId]) { @@ -1294,7 +1294,7 @@ void EmitContext::DefineOutputs(const IR::Program& program) { } if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR) || stage == Stage::VertexB) { const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(55)); + Decorate(id, spv::Decoration::Location, static_cast(11)); output_front_color = id; } if (info.stores[IR::Attribute::PointSize] || runtime_info.fixed_state_point_size) { @@ -1331,7 +1331,7 @@ void EmitContext::DefineOutputs(const IR::Program& program) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S)) { const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(56)); + Decorate(id, spv::Decoration::Location, static_cast(12)); output_txt_coord = id; } -- cgit v1.2.3 From a7bbaa489755c9847416c8c96f0eefb9e78a50a0 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Fri, 3 Sep 2021 23:52:20 +0800 Subject: Rename parameters --- src/shader_recompiler/backend/spirv/emit_context.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 78ca97d91..3ec5a4570 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1209,7 +1209,7 @@ void EmitContext::DefineInputs(const IR::Program& program) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S)) { const Id id{DefineInput(*this, F32[4], true)}; Decorate(id, spv::Decoration::Location, static_cast(12)); - input_txt_coord = id; + input_fixed_fnc_texture = id; } if (loads[IR::Attribute::InstanceId]) { if (profile.support_vertex_instance_id) { @@ -1332,7 +1332,7 @@ void EmitContext::DefineOutputs(const IR::Program& program) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S)) { const Id id{DefineOutput(*this, F32[4], invocations)}; Decorate(id, spv::Decoration::Location, static_cast(12)); - output_txt_coord = id; + output_fixed_fnc_texture = id; } for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { -- cgit v1.2.3 From d994466a08efaa2c06237e6ac840bc0e9000d433 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Sat, 4 Sep 2021 00:12:06 +0800 Subject: Implement intput and output fixed fnc textures --- .../backend/spirv/emit_context.cpp | 30 ++++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 3ec5a4570..81c79e1ed 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -1206,10 +1206,12 @@ void EmitContext::DefineInputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, static_cast(11)); input_front_color = id; } - if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, static_cast(12)); - input_fixed_fnc_texture = id; + for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) { + if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, static_cast(12)); + input_fixed_fnc_textures[index] = id; + } } if (loads[IR::Attribute::InstanceId]) { if (profile.support_vertex_instance_id) { @@ -1292,11 +1294,6 @@ void EmitContext::DefineOutputs(const IR::Program& program) { if (info.stores.AnyComponent(IR::Attribute::PositionX) || stage == Stage::VertexB) { output_position = DefineOutput(*this, F32[4], invocations, spv::BuiltIn::Position); } - if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR) || stage == Stage::VertexB) { - const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(11)); - output_front_color = id; - } if (info.stores[IR::Attribute::PointSize] || runtime_info.fixed_state_point_size) { if (stage == Stage::Fragment) { throw NotImplementedException("Storing PointSize in fragment stage"); @@ -1328,13 +1325,18 @@ void EmitContext::DefineOutputs(const IR::Program& program) { viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt, spv::BuiltIn::ViewportMaskNV); } - - if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S)) { + if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR) || stage == Stage::VertexB) { const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(12)); - output_fixed_fnc_texture = id; + Decorate(id, spv::Decoration::Location, static_cast(11)); + output_front_color = id; + } + for (size_t index = 0; index < IR::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, static_cast(12)); + output_fixed_fnc_textures[index] = id; + } } - for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { if (info.stores.Generic(index)) { DefineGenericOutput(*this, index, invocations); -- cgit v1.2.3 From 1de9e4e121a6648521debc21a46ef145156697c0 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Mon, 6 Sep 2021 10:46:03 +0800 Subject: Dynamic get unused location --- .../backend/spirv/emit_context.cpp | 76 ++++++++++++++-------- 1 file changed, 49 insertions(+), 27 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 81c79e1ed..2809f9281 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -1201,18 +1202,6 @@ void EmitContext::DefineInputs(const IR::Program& program) { } } } - if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, static_cast(11)); - input_front_color = id; - } - for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) { - if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - const Id id{DefineInput(*this, F32[4], true)}; - Decorate(id, spv::Decoration::Location, static_cast(12)); - input_fixed_fnc_textures[index] = id; - } - } if (loads[IR::Attribute::InstanceId]) { if (profile.support_vertex_instance_id) { instance_id = DefineInput(*this, U32[1], true, spv::BuiltIn::InstanceId); @@ -1239,17 +1228,15 @@ void EmitContext::DefineInputs(const IR::Program& program) { loads[IR::Attribute::TessellationEvaluationPointV]) { tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord); } + std::queue ununsed_location; for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { const AttributeType input_type{runtime_info.generic_input_types[index]}; - if (!runtime_info.previous_stage_stores.Generic(index)) { - continue; - } - if (!loads.Generic(index)) { - continue; - } - if (input_type == AttributeType::Disabled) { + if (!runtime_info.previous_stage_stores.Generic(index) || !loads.Generic(index) || + input_type == AttributeType::Disabled) { + ununsed_location.push(static_cast(index)); continue; } + const Id type{GetAttributeType(*this, input_type)}; const Id id{DefineInput(*this, type, true)}; Decorate(id, spv::Decoration::Location, static_cast(index)); @@ -1275,6 +1262,28 @@ void EmitContext::DefineInputs(const IR::Program& program) { break; } } + if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { + if (ununsed_location.empty()) { + throw RuntimeError("Unable to get an unused location"); + } + u32 location = ununsed_location.front(); + ununsed_location.pop(); + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, location); + input_front_color = id; + } + for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) { + if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { + if (ununsed_location.empty()) { + throw RuntimeError("Unable to get an unused location"); + } + u32 location = ununsed_location.front(); + ununsed_location.pop(); + const Id id{DefineInput(*this, F32[4], true)}; + Decorate(id, spv::Decoration::Location, location); + input_fixed_fnc_textures[index] = id; + } + } if (stage == Stage::TessellationEval) { for (size_t index = 0; index < info.uses_patches.size(); ++index) { if (!info.uses_patches[index]) { @@ -1325,23 +1334,36 @@ void EmitContext::DefineOutputs(const IR::Program& program) { viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt, spv::BuiltIn::ViewportMaskNV); } - if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR) || stage == Stage::VertexB) { + std::queue ununsed_location; + for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { + if (info.stores.Generic(index)) { + DefineGenericOutput(*this, index, invocations); + } else { + ununsed_location.push(static_cast(index)); + } + } + if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { + if (ununsed_location.empty()) { + throw RuntimeError("Unable to get an unused location"); + } + u32 location = ununsed_location.front(); + ununsed_location.pop(); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(11)); + Decorate(id, spv::Decoration::Location, location); output_front_color = id; } for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { + if (ununsed_location.empty()) { + throw RuntimeError("Unable to get an unused location"); + } + u32 location = ununsed_location.front(); + ununsed_location.pop(); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, static_cast(12)); + Decorate(id, spv::Decoration::Location, location); output_fixed_fnc_textures[index] = id; } } - for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { - if (info.stores.Generic(index)) { - DefineGenericOutput(*this, index, invocations); - } - } switch (stage) { case Stage::TessellationControl: if (info.stores_tess_level_outer) { -- cgit v1.2.3 From 9cdf2383e99fac2110d788da070f16b2b5c678e7 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Tue, 7 Sep 2021 12:34:35 +0800 Subject: Move attribute related definitions to spirv anonymous namespace --- src/shader_recompiler/backend/spirv/emit_context.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 2809f9281..f048174cb 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -428,6 +428,8 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) { return pointer_type; } } + +constexpr size_t NUM_FIXEDFNCTEXTURE = 10; } // Anonymous namespace void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) { @@ -1272,7 +1274,7 @@ void EmitContext::DefineInputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, location); input_front_color = id; } - for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) { + for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { if (ununsed_location.empty()) { throw RuntimeError("Unable to get an unused location"); @@ -1352,7 +1354,7 @@ void EmitContext::DefineOutputs(const IR::Program& program) { Decorate(id, spv::Decoration::Location, location); output_front_color = id; } - for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) { + for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { if (ununsed_location.empty()) { throw RuntimeError("Unable to get an unused location"); -- cgit v1.2.3 From e5ca733722865daa43eca8679f3c55826f0b88e6 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Tue, 7 Sep 2021 13:21:17 +0800 Subject: Re-implement get unused location --- .../backend/spirv/emit_context.cpp | 60 +++++++++++----------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index f048174cb..b9fa5098c 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include @@ -430,6 +429,16 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) { } constexpr size_t NUM_FIXEDFNCTEXTURE = 10; + +size_t FindFistUnUsedLocation(const auto& used_location) { + if (used_location.all()) { + throw RuntimeError("Unable to get an unused location"); + } + size_t location = 0; + while (location < used_location.size() && used_location.test(location)) + ++location; + return location; +} } // Anonymous namespace void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) { @@ -1230,15 +1239,19 @@ void EmitContext::DefineInputs(const IR::Program& program) { loads[IR::Attribute::TessellationEvaluationPointV]) { tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord); } - std::queue ununsed_location; + std::bitset used_location; for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { const AttributeType input_type{runtime_info.generic_input_types[index]}; - if (!runtime_info.previous_stage_stores.Generic(index) || !loads.Generic(index) || - input_type == AttributeType::Disabled) { - ununsed_location.push(static_cast(index)); + if (!runtime_info.previous_stage_stores.Generic(index)) { continue; } - + if (!loads.Generic(index)) { + continue; + } + if (input_type == AttributeType::Disabled) { + continue; + } + used_location.set(index); const Id type{GetAttributeType(*this, input_type)}; const Id id{DefineInput(*this, type, true)}; Decorate(id, spv::Decoration::Location, static_cast(index)); @@ -1265,22 +1278,16 @@ void EmitContext::DefineInputs(const IR::Program& program) { } } if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - if (ununsed_location.empty()) { - throw RuntimeError("Unable to get an unused location"); - } - u32 location = ununsed_location.front(); - ununsed_location.pop(); + size_t location = FindFistUnUsedLocation(used_location); + used_location.set(location); const Id id{DefineInput(*this, F32[4], true)}; Decorate(id, spv::Decoration::Location, location); input_front_color = id; } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - if (ununsed_location.empty()) { - throw RuntimeError("Unable to get an unused location"); - } - u32 location = ununsed_location.front(); - ununsed_location.pop(); + size_t location = FindFistUnUsedLocation(used_location); + used_location.set(location); const Id id{DefineInput(*this, F32[4], true)}; Decorate(id, spv::Decoration::Location, location); input_fixed_fnc_textures[index] = id; @@ -1336,31 +1343,24 @@ void EmitContext::DefineOutputs(const IR::Program& program) { viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt, spv::BuiltIn::ViewportMaskNV); } - std::queue ununsed_location; + std::bitset used_location; for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { if (info.stores.Generic(index)) { DefineGenericOutput(*this, index, invocations); - } else { - ununsed_location.push(static_cast(index)); + used_location.set(index); } } if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - if (ununsed_location.empty()) { - throw RuntimeError("Unable to get an unused location"); - } - u32 location = ununsed_location.front(); - ununsed_location.pop(); + size_t location = FindFistUnUsedLocation(used_location); + used_location.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; - Decorate(id, spv::Decoration::Location, location); + Decorate(id, spv::Decoration::Location, static_cast(location)); output_front_color = id; } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - if (ununsed_location.empty()) { - throw RuntimeError("Unable to get an unused location"); - } - u32 location = ununsed_location.front(); - ununsed_location.pop(); + size_t location = FindFistUnUsedLocation(used_location); + used_location.set(location); const Id id{DefineOutput(*this, F32[4], invocations)}; Decorate(id, spv::Decoration::Location, location); output_fixed_fnc_textures[index] = id; -- cgit v1.2.3 From bbc1800c1b22097c56ecb799c0e475aaf8502038 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Wed, 8 Sep 2021 09:53:10 +0800 Subject: Detail adjustment --- .../backend/spirv/emit_context.cpp | 47 +++++++++++++--------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index b9fa5098c..118a80165 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -15,6 +15,8 @@ namespace Shader::Backend::SPIRV { namespace { +constexpr size_t NUM_FIXEDFNCTEXTURE = 10; + enum class Operation { Increment, Decrement, @@ -428,15 +430,14 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) { } } -constexpr size_t NUM_FIXEDFNCTEXTURE = 10; - -size_t FindFistUnUsedLocation(const auto& used_location) { - if (used_location.all()) { - throw RuntimeError("Unable to get an unused location"); - } - size_t location = 0; - while (location < used_location.size() && used_location.test(location)) +size_t FindFistUnUsedLocation(const std::bitset& used_locations, + size_t previous_unused_location) { + size_t location = previous_unused_location + 1; + while (location < used_locations.size() && used_locations.test(location)) ++location; + if (location == used_locations.size()) { + throw RuntimeError("Unable to get an unused location for legacy attribute"); + } return location; } } // Anonymous namespace @@ -1239,7 +1240,7 @@ void EmitContext::DefineInputs(const IR::Program& program) { loads[IR::Attribute::TessellationEvaluationPointV]) { tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord); } - std::bitset used_location; + std::bitset used_locations{}; for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { const AttributeType input_type{runtime_info.generic_input_types[index]}; if (!runtime_info.previous_stage_stores.Generic(index)) { @@ -1251,7 +1252,7 @@ void EmitContext::DefineInputs(const IR::Program& program) { if (input_type == AttributeType::Disabled) { continue; } - used_location.set(index); + used_locations.set(index); const Id type{GetAttributeType(*this, input_type)}; const Id id{DefineInput(*this, type, true)}; Decorate(id, spv::Decoration::Location, static_cast(index)); @@ -1277,17 +1278,20 @@ void EmitContext::DefineInputs(const IR::Program& program) { break; } } + size_t previous_unused_location = 0; if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - size_t location = FindFistUnUsedLocation(used_location); - used_location.set(location); + size_t location = FindFistUnUsedLocation(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_color = id; } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - size_t location = FindFistUnUsedLocation(used_location); - used_location.set(location); + size_t location = FindFistUnUsedLocation(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_fixed_fnc_textures[index] = id; @@ -1343,24 +1347,27 @@ void EmitContext::DefineOutputs(const IR::Program& program) { viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt, spv::BuiltIn::ViewportMaskNV); } - std::bitset used_location; + std::bitset used_locations{}; for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { if (info.stores.Generic(index)) { DefineGenericOutput(*this, index, invocations); - used_location.set(index); + used_locations.set(index); } } + size_t previous_unused_location = 0; if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - size_t location = FindFistUnUsedLocation(used_location); - used_location.set(location); + size_t location = FindFistUnUsedLocation(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_color = id; } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - size_t location = FindFistUnUsedLocation(used_location); - used_location.set(location); + size_t location = FindFistUnUsedLocation(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); output_fixed_fnc_textures[index] = id; -- cgit v1.2.3 From b1e655f89824bb1c43fc5160cd879b60bb60dbad Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Wed, 8 Sep 2021 10:28:09 +0800 Subject: Detail adjustment --- .../backend/spirv/emit_context.cpp | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp') diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 118a80165..2885e6799 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -430,15 +430,14 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) { } } -size_t FindFistUnUsedLocation(const std::bitset& used_locations, - size_t previous_unused_location) { - size_t location = previous_unused_location + 1; - while (location < used_locations.size() && used_locations.test(location)) - ++location; - if (location == used_locations.size()) { - throw RuntimeError("Unable to get an unused location for legacy attribute"); - } - return location; +size_t FindNextUnusedLocation(const std::bitset& used_locations, + size_t start_offset) { + for (size_t location = start_offset; location < used_locations.size(); ++location) { + if (!used_locations.test(location)) { + return location; + } + } + throw RuntimeError("Unable to get an unused location for legacy attribute"); } } // Anonymous namespace @@ -1280,7 +1279,7 @@ void EmitContext::DefineInputs(const IR::Program& program) { } size_t previous_unused_location = 0; if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - size_t location = FindFistUnUsedLocation(used_locations, previous_unused_location); + 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)}; @@ -1289,7 +1288,8 @@ void EmitContext::DefineInputs(const IR::Program& program) { } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - size_t location = FindFistUnUsedLocation(used_locations, previous_unused_location); + 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)}; @@ -1356,7 +1356,7 @@ void EmitContext::DefineOutputs(const IR::Program& program) { } size_t previous_unused_location = 0; if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { - size_t location = FindFistUnUsedLocation(used_locations, previous_unused_location); + 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)}; @@ -1365,7 +1365,8 @@ void EmitContext::DefineOutputs(const IR::Program& program) { } for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { - size_t location = FindFistUnUsedLocation(used_locations, previous_unused_location); + 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)}; -- cgit v1.2.3