]> git.tdb.fi Git - libs/gl.git/commitdiff
Adjust naming of generated variables
authorMikko Rasa <tdb@tdb.fi>
Tue, 16 Mar 2021 17:06:37 +0000 (19:06 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 16 Mar 2021 17:06:37 +0000 (19:06 +0200)
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
source/glsl/optimize.cpp
source/glsl/optimize.h
source/glsl/syntax.cpp
source/glsl/syntax.h
tests/glsl/function_inline_global_name_conflict.glsl
tests/glsl/function_inline_multi_name_conflict.glsl
tests/glsl/function_inline_name_conflict.glsl
tests/glsl/function_inline_return_conflict.glsl
tests/glsl/function_overloading.glsl

index 70b8ad18efbca4ca6279435c0f908bb8d37eae15..42b24a9caa949655c6f2a475187aa573d4376447 100644 (file)
@@ -1015,7 +1015,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                                        stage inline it if that's reasonable. */
                                        RefPtr<VariableDeclaration> 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);
 
index 7d605f1f12bc755812a3252172828a84d3720777..d57002552540c6c375cb7446f8465a9538a0a42d 100644 (file)
@@ -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<RefPtr<VariableDeclaration> > 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<VariableDeclaration> 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<VariableReference> ref = new VariableReference;
                ref->name = result_name;
index 6d4b1bcb5d2c2e5d883d7ab640390481c8c7e84e..e7592c00a29956e7ad4ab09384cf9798b3d1aacf 100644 (file)
@@ -49,7 +49,6 @@ private:
 
        FunctionDeclaration *source_func;
        Block staging_block;
-       std::string remap_prefix;
        Pass pass;
        RefPtr<Statement> r_inlined_statement;
        std::set<Node *> dependencies;
index c94e2e27042c63a6a7f0b2c91f3d26c5b00f1e30..e7a3990e5c1478b3fcdf8e5727226b05ba688582 100644 (file)
@@ -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;
        }
 }
 
index 55823d34bb8acad73c17a1018f2c56804f8e7b7d..6129421133b0d02ccb416758cf105758a956b01d 100644 (file)
@@ -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
index 8ebbfb4666ab112af31ae794d85d0a328322131b..4c19a640e5ab4097c39433c68bef028b02b8b2c0 100644 (file)
@@ -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;
 }
 */
index 6740b94469805ca1386770125b5b175d816f0ffa..cf40df14dced064f7716ff4927ffab5f17d53e19 100644 (file)
@@ -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;
 }
 */
index 93c4eca32c761f1204af41a472ef43927e96354f..f99a5ce3110b5cda55adf5ead5ee173c1fbaab49 100644 (file)
@@ -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;
 }
 */
index 50bf1bd5dd3b8e7dabb54abf563c51c55982f323..d0ebabc901fe2e34f26435fa301677d94323aeb5 100644 (file)
@@ -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;
 }
 */
index e5561705fca8265eb4b205008984a50fbe972032..6924a6286b64745b6e940f9440461acc91412445 100644 (file)
@@ -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);
 }
 */