]> git.tdb.fi Git - libs/gl.git/commitdiff
Avoid emitting duplicate specialization constants in SPIR-V
authorMikko Rasa <tdb@tdb.fi>
Wed, 17 Nov 2021 13:34:06 +0000 (15:34 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 17 Nov 2021 13:35:39 +0000 (15:35 +0200)
source/glsl/generate.cpp
source/glsl/spirv.cpp
source/glsl/spirv.h

index 85c3009bc312e6426b570d4f85451aad7a146752..3175c6c9f206fd0c64b4eb130dc461d6979865cb 100644 (file)
@@ -32,6 +32,7 @@ void ConstantIdAssigner::apply(Module &module, const Features &features)
                        i->value = id;
 
                used_ids.insert(id);
+               existing_constants[v->name] = id;
        }
 }
 
index 40e731fe63fa9fcfb384dd61baf8429e34be3382..7f0d0b702d1ba3c75d2e3465308dc090bc83e6a0 100644 (file)
@@ -1591,6 +1591,12 @@ void SpirVGenerator::visit(VariableDeclaration &var)
                        throw internal_error("const variable without initializer");
 
                int spec_id = get_layout_value(var.layout.get(), "constant_id");
+               Id *spec_var_id = (spec_id>=0 ? &declared_spec_ids[spec_id] : 0);
+               if(spec_id>=0 && *spec_var_id)
+               {
+                       insert_unique(declared_ids, &var, Declaration(*spec_var_id, type_id));
+                       return;
+               }
 
                SetFlag set_const(constant_expression);
                SetFlag set_spec(spec_constant, spec_id>=0);
@@ -1598,7 +1604,11 @@ void SpirVGenerator::visit(VariableDeclaration &var)
                var.init_expression->visit(*this);
                var_id = r_expression_result_id;
                insert_unique(declared_ids, &var, Declaration(var_id, type_id));
-               writer.write_op_decorate(var_id, DECO_SPEC_ID, spec_id);
+               if(spec_id>=0)
+               {
+                       writer.write_op_decorate(var_id, DECO_SPEC_ID, spec_id);
+                       *spec_var_id = var_id;
+               }
 
                /* It's unclear what should be done if a specialization constant is
                initialized with anything other than a literal.  GLSL doesn't seem to
index 6971e2a9c43eb2ec8cc180b94043a6388229858e..59c7b50b0cc45fede654eff5d3580840c5eecf1a 100644 (file)
@@ -79,6 +79,7 @@ private:
        std::map<std::string, Id> imported_extension_ids;
        std::map<Node *, Declaration> declared_ids;
        std::map<std::string, Id> declared_uniform_ids;
+       std::map<unsigned, Id> declared_spec_ids;
        std::map<TypeKey, Id> standard_type_ids;
        std::map<Id, Id> image_type_ids;
        std::map<TypeKey, Id> array_type_ids;