From 22d5405729048ee2677a1e45e309e6328de64a26 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 16 Mar 2021 19:06:37 +0200 Subject: [PATCH] Adjust naming of generated variables Using the inlined function name as a prefix easily produces inconsistent results, so better to just use numbers after the name. --- source/glsl/generate.cpp | 2 +- source/glsl/optimize.cpp | 8 +++---- source/glsl/optimize.h | 1 - source/glsl/syntax.cpp | 24 ++++--------------- source/glsl/syntax.h | 2 +- .../function_inline_global_name_conflict.glsl | 8 +++---- .../function_inline_multi_name_conflict.glsl | 4 ++-- tests/glsl/function_inline_name_conflict.glsl | 4 ++-- .../glsl/function_inline_return_conflict.glsl | 4 ++-- tests/glsl/function_overloading.glsl | 4 ++-- 10 files changed, 22 insertions(+), 39 deletions(-) diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 70b8ad18..42b24a9c 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -1015,7 +1015,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) stage inline it if that's reasonable. */ RefPtr temporary = new VariableDeclaration; temporary->type = args.front().type->name; - temporary->name = get_unused_variable_name(*current_block, "_temp", string()); + temporary->name = get_unused_variable_name(*current_block, "_temp"); temporary->init_expression = call.arguments.front(); current_block->body.insert(insert_point, temporary); diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 7d605f1f..d5700255 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -91,7 +91,6 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta pass = INLINE; staging_block.parent = &tgt_blk; staging_block.variables.clear(); - remap_prefix = source_func->name; std::vector > params; params.reserve(source_func->parameters.size()); @@ -133,7 +132,6 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta global identifiers used by the source function. */ pass = RENAME; staging_block.parent = source_func->body.parent; - remap_prefix = target_func.name; target_func.visit(*this); // Put the argument expressions in place after all renaming has been done. @@ -193,7 +191,7 @@ void InlineContentInjector::visit(VariableDeclaration &var) staging_block.variables[var.name] = &var; if(referenced_names.count(var.name)) { - string mapped_name = get_unused_variable_name(staging_block, var.name, remap_prefix); + string mapped_name = get_unused_variable_name(staging_block, var.name); if(mapped_name!=var.name) { staging_block.variables[mapped_name] = &var; @@ -217,7 +215,7 @@ void InlineContentInjector::visit(Return &ret) if(pass==INLINE && ret.expression) { // Create a new variable to hold the return value of the inlined function. - r_result_name = get_unused_variable_name(staging_block, "_return", source_func->name); + r_result_name = get_unused_variable_name(staging_block, "_return"); RefPtr var = new VariableDeclaration; var->source = ret.source; var->line = ret.line; @@ -285,7 +283,7 @@ void FunctionInliner::visit(FunctionCall &call) // This will later get removed by UnusedVariableRemover. if(result_name.empty()) - result_name = "msp_unused_from_inline"; + result_name = "_msp_unused_from_inline"; RefPtr ref = new VariableReference; ref->name = result_name; diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 6d4b1bcb..e7592c00 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -49,7 +49,6 @@ private: FunctionDeclaration *source_func; Block staging_block; - std::string remap_prefix; Pass pass; RefPtr r_inlined_statement; std::set dependencies; diff --git a/source/glsl/syntax.cpp b/source/glsl/syntax.cpp index c94e2e27..e7a3990e 100644 --- a/source/glsl/syntax.cpp +++ b/source/glsl/syntax.cpp @@ -465,13 +465,12 @@ Module::Module(): { } -string get_unused_variable_name(const Block &block, const string &base, const string &prefix_hint) +string get_unused_variable_name(const Block &block, const string &base) { string name = base; - bool prefixed = false; unsigned number = 1; - unsigned size_without_number = name.size(); + unsigned base_size = name.size(); while(1) { bool unused = true; @@ -480,22 +479,9 @@ string get_unused_variable_name(const Block &block, const string &base, const st if(unused) return name; - if(!prefixed && !prefix_hint.empty()) - { - if(name.front()!='_') - name = "_"+name; - name = prefix_hint+name; - if(name.front()!='_') - name = "_"+name; - prefixed = true; - size_without_number = name.size(); - } - else - { - name.erase(size_without_number); - name += format("_%d", number); - ++number; - } + name.erase(base_size); + name += format("_%d", number); + ++number; } } diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 55823d34..61294211 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -545,7 +545,7 @@ struct Module Module(); }; -std::string get_unused_variable_name(const Block &, const std::string &, const std::string &); +std::string get_unused_variable_name(const Block &, const std::string &); } // namespace SL } // namespace GL diff --git a/tests/glsl/function_inline_global_name_conflict.glsl b/tests/glsl/function_inline_global_name_conflict.glsl index 8ebbfb46..4c19a640 100644 --- a/tests/glsl/function_inline_global_name_conflict.glsl +++ b/tests/glsl/function_inline_global_name_conflict.glsl @@ -20,9 +20,9 @@ layout(location=1) in float scale; layout(location=2) in float size; void main() { - float _main_size = scale+1.0; - float _func_scale = size*2.0; - float s = _func_scale*_func_scale+1.0; - gl_Position = position*_main_size*_main_size*s*s; + float size_1 = scale+1.0; + float scale_1 = size*2.0; + float s = scale_1*scale_1+1.0; + gl_Position = position*size_1*size_1*s*s; } */ diff --git a/tests/glsl/function_inline_multi_name_conflict.glsl b/tests/glsl/function_inline_multi_name_conflict.glsl index 6740b944..cf40df14 100644 --- a/tests/glsl/function_inline_multi_name_conflict.glsl +++ b/tests/glsl/function_inline_multi_name_conflict.glsl @@ -24,7 +24,7 @@ layout(location=2) in float size; void main() { float s = scale*2.0; - float _get_size_s = size*0.5; - gl_Position = position*s*s*_get_size_s*_get_size_s; + float s_1 = size*0.5; + gl_Position = position*s*s*s_1*s_1; } */ diff --git a/tests/glsl/function_inline_name_conflict.glsl b/tests/glsl/function_inline_name_conflict.glsl index 93c4eca3..f99a5ce3 100644 --- a/tests/glsl/function_inline_name_conflict.glsl +++ b/tests/glsl/function_inline_name_conflict.glsl @@ -18,7 +18,7 @@ layout(location=1) in float scale; void main() { float s = scale+1.0; - float _func_s = scale*2.0; - gl_Position = position*_func_s*_func_s*s*s; + float s_1 = scale*2.0; + gl_Position = position*s_1*s_1*s*s; } */ diff --git a/tests/glsl/function_inline_return_conflict.glsl b/tests/glsl/function_inline_return_conflict.glsl index 50bf1bd5..d0ebabc9 100644 --- a/tests/glsl/function_inline_return_conflict.glsl +++ b/tests/glsl/function_inline_return_conflict.glsl @@ -18,7 +18,7 @@ layout(location=1) in float scale; void main() { float _return = scale+1.0; - float _func_return = scale*2.0; - gl_Position = position*vec2(_func_return, _func_return).xxyy*_return*_return; + float _return_1 = scale*2.0; + gl_Position = position*vec2(_return_1, _return_1).xxyy*_return*_return; } */ diff --git a/tests/glsl/function_overloading.glsl b/tests/glsl/function_overloading.glsl index e5561705..6924a628 100644 --- a/tests/glsl/function_overloading.glsl +++ b/tests/glsl/function_overloading.glsl @@ -48,7 +48,7 @@ in vec2 _vs_out_texcoord; void main() { vec4 color = texture(tex, _vs_out_texcoord); - vec3 _srgb_to_linear_color = color.rgb; - frag_color = vec4(mix(_srgb_to_linear_color/12.92, pow((_srgb_to_linear_color+0.055)/1.055, vec3(2.4)), lessThan(_srgb_to_linear_color, vec3(0.04045))), color.a); + vec3 color_1 = color.rgb; + frag_color = vec4(mix(color_1/12.92, pow((color_1+0.055)/1.055, vec3(2.4)), lessThan(color_1, vec3(0.04045))), color.a); } */ -- 2.43.0