From: Mikko Rasa Date: Wed, 17 Nov 2021 13:34:06 +0000 (+0200) Subject: Avoid emitting duplicate specialization constants in SPIR-V X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f7682c3c2e9f028cef6945ffbada66cff33585c0;p=libs%2Fgl.git Avoid emitting duplicate specialization constants in SPIR-V --- diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 85c3009b..3175c6c9 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -32,6 +32,7 @@ void ConstantIdAssigner::apply(Module &module, const Features &features) i->value = id; used_ids.insert(id); + existing_constants[v->name] = id; } } diff --git a/source/glsl/spirv.cpp b/source/glsl/spirv.cpp index 40e731fe..7f0d0b70 100644 --- a/source/glsl/spirv.cpp +++ b/source/glsl/spirv.cpp @@ -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 diff --git a/source/glsl/spirv.h b/source/glsl/spirv.h index 6971e2a9..59c7b50b 100644 --- a/source/glsl/spirv.h +++ b/source/glsl/spirv.h @@ -79,6 +79,7 @@ private: std::map imported_extension_ids; std::map declared_ids; std::map declared_uniform_ids; + std::map declared_spec_ids; std::map standard_type_ids; std::map image_type_ids; std::map array_type_ids;