]> git.tdb.fi Git - libs/gl.git/commitdiff
Apply implicit conversion to return expressions in GLSL functions
authorMikko Rasa <tdb@tdb.fi>
Sun, 7 Nov 2021 10:46:29 +0000 (12:46 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 7 Nov 2021 10:46:29 +0000 (12:46 +0200)
source/glsl/resolve.cpp
source/glsl/resolve.h
tests/glsl/return_type_conversion.glsl [new file with mode: 0644]

index b80ae2176f5c8fae81a3c36cdaaf22a383f59256..eb99bec2ab57d9c801724b8803a3397e270ae363 100644 (file)
@@ -1141,6 +1141,28 @@ void ExpressionResolver::visit(VariableDeclaration &var)
                convert_to(var.init_expression, *var_basic);
 }
 
+void ExpressionResolver::visit(FunctionDeclaration &func)
+{
+       SetForScope<const FunctionDeclaration *> set_func(current_function, &func);
+       TraversingVisitor::visit(func);
+}
+
+void ExpressionResolver::visit(Return &ret)
+{
+       TraversingVisitor::visit(ret);
+       if(!current_function || !ret.expression)
+               return;
+
+       BasicTypeDeclaration *ret_basic = dynamic_cast<BasicTypeDeclaration *>(current_function->return_type_declaration);
+       BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(ret.expression->type);
+       if(!ret_basic || !expr_basic)
+               return;
+
+       Compatibility compat = get_compatibility(*ret_basic, *expr_basic);
+       if(compat==RIGHT_CONVERTIBLE)
+               convert_to(ret.expression, *ret_basic);
+}
+
 
 bool FunctionResolver::apply(Stage &s)
 {
index 10322d49f67e89d80f61b994177f18c1b2b0588b..fdbebfe8e3a8e8249407545d5abfef5ca291dda0 100644 (file)
@@ -102,6 +102,7 @@ private:
        };
 
        Stage *stage = 0;
+       const FunctionDeclaration *current_function = 0;
        std::vector<BasicTypeDeclaration *> basic_types;
        NodeList<Statement>::iterator insert_point;
        bool r_any_resolved = false;
@@ -133,6 +134,8 @@ private:
        virtual void visit(FunctionCall &);
        virtual void visit(BasicTypeDeclaration &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &);
+       virtual void visit(Return &);
 };
 
 /** Resolves function declarations and calls. */
diff --git a/tests/glsl/return_type_conversion.glsl b/tests/glsl/return_type_conversion.glsl
new file mode 100644 (file)
index 0000000..3467311
--- /dev/null
@@ -0,0 +1,78 @@
+uniform Shadow
+{
+       mat4 shadow_matrix;
+};
+uniform Camera
+{
+       mat4 viewproj_matrix;
+};
+uniform Surface
+{
+       vec4 color;
+};
+uniform mat4 model_matrix;
+uniform sampler2DShadow shadow_map;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+void main()
+{
+       out vec4 world_position = model_matrix*position;
+       gl_Position = viewproj_matrix*world_position;
+}
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec4 frag_out;
+float get_shadow()
+{
+       vec3 shadow_coord = (shadow_matrix*world_position).xyz;
+       if(shadow_coord.x>=0 && shadow_coord.x<=1 && shadow_coord.y>=0 && shadow_coord.y<=1)
+               return texture(shadow_map, shadow_coord);
+       else
+               return 1;
+}
+void main()
+{
+       frag_out = vec4(color.rgb*get_shadow(), color.a);
+}
+
+/* Expected output: vertex
+layout(binding=72) uniform Camera
+{
+       mat4 viewproj_matrix;
+};
+layout(location=0) uniform mat4 model_matrix;
+layout(location=0) in vec4 position;
+layout(location=0) out vec4 world_position;
+void main()
+{
+       world_position = model_matrix*position;
+       gl_Position = viewproj_matrix*world_position;
+}
+*/
+
+/* Expected output: fragment
+layout(binding=71) uniform Shadow
+{
+       mat4 shadow_matrix;
+};
+layout(binding=77) uniform Surface
+{
+       vec4 color;
+};
+layout(location=4, binding=73) uniform sampler2DShadow shadow_map;
+layout(location=0) out vec4 frag_out;
+layout(location=0) in vec4 world_position;
+float get_shadow()
+{
+       vec3 shadow_coord = (shadow_matrix*world_position).xyz;
+       if(shadow_coord.x>=0.0&&shadow_coord.x<=1.0&&shadow_coord.y>=0.0&&shadow_coord.y<=1.0)
+               return texture(shadow_map, shadow_coord);
+       else
+               return 1.0;
+}
+void main()
+{
+       frag_out = vec4(color.rgb*get_shadow(), color.a);
+}
+*/