From e5ca733722865daa43eca8679f3c55826f0b88e6 Mon Sep 17 00:00:00 2001
From: Feng Chen <vonchenplus@gmail.com>
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 f048174cb5..b9fa5098ca 100644
--- a/src/shader_recompiler/backend/spirv/emit_context.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_context.cpp
@@ -6,7 +6,6 @@
 #include <array>
 #include <climits>
 #include <string_view>
-#include <queue>
 
 #include <fmt/format.h>
 
@@ -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<u32> ununsed_location;
+    std::bitset<IR::NUM_GENERICS> 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<u32>(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<u32>(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<u32> ununsed_location;
+    std::bitset<IR::NUM_GENERICS> 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<u32>(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<u32>(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-70-g09d2