glsl: Properly handle .length() of an unsized array
authorYevhenii Kolesnikov <yevhenii.kolesnikov@globallogic.com>
Wed, 14 Jul 2021 17:52:56 +0000 (20:52 +0300)
committerMarge Bot <eric+marge@anholt.net>
Tue, 27 Jul 2021 10:02:50 +0000 (10:02 +0000)
There are two distinct cases:
- The last member of a shader storage block (length determined at run-time)
- Implicitly-sized array (length determined at link-time)

Fixes: 273f61a0051a ("glsl: Add parser/compiler support for unsized array's length()")
Signed-off-by: Yevhenii Kolesnikov <yevhenii.kolesnikov@globallogic.com>
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11952>

src/compiler/glsl/ast_function.cpp

index 73e72b5..92ca6a4 100644 (file)
@@ -2047,10 +2047,18 @@ ast_function_expression::handle_method(exec_list *instructions,
                                 "length called on unsized array"
                                 " only available with"
                                 " ARB_shader_storage_buffer_object");
+               goto fail;
+            } else if (op->variable_referenced()->is_in_shader_storage_block()) {
+               /* Calculate length of an unsized array in run-time */
+               result = new(ctx)
+                  ir_expression(ir_unop_ssbo_unsized_array_length, op);
+            } else {
+               /* When actual size is known at link-time, this will be
+                * replaced with a constant expression.
+                */
+               result = new (ctx)
+                  ir_expression(ir_unop_implicitly_sized_array_length, op);
             }
-            /* Calculate length of an unsized array in run-time */
-            result = new(ctx) ir_expression(ir_unop_ssbo_unsized_array_length,
-                                            op);
          } else {
             result = new(ctx) ir_constant(op->type->array_size());
          }