]> git.tdb.fi Git - libs/gl.git/commitdiff
Compare array sizes in TypeComparer
authorMikko Rasa <tdb@tdb.fi>
Tue, 6 Apr 2021 23:02:40 +0000 (02:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 6 Apr 2021 23:10:09 +0000 (02:10 +0300)
source/glsl/reflect.cpp
source/glsl/reflect.h
tests/glsl/array_in_uniform_block.glsl [new file with mode: 0644]

index 93d64e5fdb62aac1087d080efb37a0687fd99901..e63553163c8435f23e2199effa17cc91eb5e52ee 100644 (file)
@@ -51,6 +51,36 @@ T *TypeComparer::multi_visit(T &node)
        return 0;
 }
 
+void TypeComparer::visit(Literal &literal)
+{
+       if(Literal *lit1 = multi_visit(literal))
+       {
+               if(!lit1->type || !literal.type)
+                       r_result = false;
+               else
+               {
+                       compare(*lit1->type, *literal.type);
+                       if(r_result)
+                               r_result = (literal.value.check_type<int>() && lit1->value.value<int>()==literal.value.value<int>());
+               }
+       }
+}
+
+void TypeComparer::visit(VariableReference &var)
+{
+       if(VariableReference *var1 = multi_visit(var))
+       {
+               if(!var1->declaration || !var.declaration)
+                       r_result = false;
+               else if(!var1->declaration->constant || !var.declaration->constant)
+                       r_result = false;
+               else if(!var1->declaration->init_expression || !var.declaration->init_expression)
+                       r_result = false;
+               else
+                       compare(*var1->declaration->init_expression, *var.declaration->init_expression);
+       }
+}
+
 void TypeComparer::visit(BasicTypeDeclaration &basic)
 {
        if(BasicTypeDeclaration *basic1 = multi_visit(basic))
@@ -106,8 +136,13 @@ void TypeComparer::visit(VariableDeclaration &var)
                        r_result = false;
                else
                {
-                       // TODO Compare array sizes
-                       if(var1->type_declaration!=var.type_declaration)
+                       if(var1->array)
+                       {
+                               r_result = false;
+                               if(var1->array_size && var.array_size)
+                                       compare(*var1->array_size, *var.array_size);
+                       }
+                       if(r_result && var1->type_declaration!=var.type_declaration)
                                compare(*var1->type_declaration, *var.type_declaration);
                        // TODO Compare layout qualifiers for interface block members
                }
index 9520feb6a924c797ee50b7e8a062dbd7b1f74795..56ead82ceface5a35fc72af0a2f2b60ae874efc0 100644 (file)
@@ -27,6 +27,8 @@ private:
        void compare(Node &, Node &);
        template<typename T>
        T *multi_visit(T &);
+       virtual void visit(Literal &);
+       virtual void visit(VariableReference &);
        virtual void visit(BasicTypeDeclaration &);
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
diff --git a/tests/glsl/array_in_uniform_block.glsl b/tests/glsl/array_in_uniform_block.glsl
new file mode 100644 (file)
index 0000000..3d53b3d
--- /dev/null
@@ -0,0 +1,86 @@
+const int n_lights = 2;
+struct LightParams
+{
+       vec3 direction;
+       vec3 color;
+};
+uniform Lighting
+{
+       LightParams lights[n_lights];
+};
+uniform vec3 ambient;
+uniform mat4 model_matrix;
+uniform mat4 view_matrix;
+uniform mat4 proj_matrix;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+layout(location=1) in vec3 normal;
+out vec3 eye_light_dir[n_lights];
+void main()
+{
+       for(int i=0; i<n_lights; ++i)
+               eye_light_dir[i] = mat3(view_matrix)*lights[i].direction;
+       out vec3 eye_normal = mat3(view_matrix*model_matrix)*normal;
+       gl_Position = proj_matrix*view_matrix*model_matrix*position;
+}
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec4 frag_color;
+void main()
+{
+       vec3 color = ambient;
+       for(int i=0; i<n_lights; ++i)
+               color += max(dot(normalize(eye_normal), eye_light_dir[i]), 0.0)*lights[i].color;
+       frag_color = vec4(color, 1.0);
+}
+
+/* Expected output: vertex
+struct LightParams
+{
+  vec3 direction;
+  vec3 color;
+};
+layout(binding=5) uniform Lighting
+{
+  LightParams lights[2];
+};
+layout(location=0) uniform mat4 model_matrix;
+layout(location=4) uniform mat4 view_matrix;
+layout(location=8) uniform mat4 proj_matrix;
+layout(location=0) in vec4 position;
+layout(location=1) in vec3 normal;
+layout(location=0) out vec3 eye_light_dir[2];
+layout(location=2) out vec3 eye_normal;
+void main()
+{
+  for(int i = 0; i<2; ++i)
+    eye_light_dir[i] = mat3(view_matrix[0].xyz, view_matrix[1].xyz, view_matrix[2].xyz)*lights[i].direction;
+  mat4 _temp = view_matrix*model_matrix;
+  eye_normal = mat3(_temp[0].xyz, _temp[1].xyz, _temp[2].xyz)*normal;
+  gl_Position = proj_matrix*view_matrix*model_matrix*position;
+}
+*/
+
+/* Expected output: fragment
+struct LightParams
+{
+  vec3 direction;
+  vec3 color;
+};
+layout(binding=5) uniform Lighting
+{
+  LightParams lights[2];
+};
+layout(location=12) uniform vec3 ambient;
+layout(location=0) out vec4 frag_color;
+layout(location=2) in vec3 eye_normal;
+layout(location=0) in vec3 eye_light_dir[2];
+void main()
+{
+  vec3 color = ambient;
+  for(int i = 0; i<2; ++i)
+    color += max(dot(normalize(eye_normal), eye_light_dir[i]), 0.0)*lights[i].color;
+  frag_color = vec4(color, 1.0);
+}
+*/