From bbc1800c1b22097c56ecb799c0e475aaf8502038 Mon Sep 17 00:00:00 2001 From: Feng Chen Date: Wed, 8 Sep 2021 09:53:10 +0800 Subject: [PATCH] Detail adjustment --- .../backend/spirv/emit_context.cpp | 47 +++++++++++-------- .../spirv/emit_spirv_context_get_set.cpp | 16 +++---- 2 files changed, 35 insertions(+), 28 deletions(-) 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; 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 dd0d01b2a..67df46499 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 @@ -50,16 +50,16 @@ bool IsFixedFncTexture(IR::Attribute attribute) { u32 FixedFncTextureAttributeIndex(IR::Attribute attribute) { if (!IsFixedFncTexture(attribute)) { - throw InvalidArgument("Attribute is not fixedfnctexture {}", 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 fixedfnctexture {}", attribute); + throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute); } - return static_cast(attribute) % 4; + return static_cast(attribute) % 4u; } template @@ -93,7 +93,7 @@ std::optional OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id); } } - if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture9Q) { + if (IsFixedFncTexture(attr)) { const u32 index{FixedFncTextureAttributeIndex(attr)}; const u32 element{FixedFncTextureAttributeElement(attr)}; const Id element_id{ctx.Const(element)}; @@ -341,11 +341,11 @@ 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 (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture9Q) { + if (IsFixedFncTexture(attr)) { const u32 index{FixedFncTextureAttributeIndex(attr)}; - return ctx.OpLoad(ctx.F32[1], - AttrPointer(ctx, ctx.input_f32, vertex, - ctx.input_fixed_fnc_textures[index], ctx.Const(element))); + 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); } switch (attr) { case IR::Attribute::PrimitiveId: