[PATCH vkd3d v4 5/7] vkd3d-shader/hlsl: Support all complex initalizers.

Giovanni Mascellani gmascellani at codeweavers.com
Tue Apr 26 05:26:16 CDT 2022


From: Francisco Casas <fcasas at codeweavers.com>

Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
---
v3:
* Patch reworked from scratch by Francisco
* Some tests are still failing because they depend on matrix copying
v4:
* Renamed hlsl_compute_component_reg_offset() to
  hlsl_compute_component_offset().
* Removed 'loc' argument from hlsl_compute_component_offset().
* Removed comp_type != NULL checks.
* Use '%' in HLSL_CLASS_ARRAY case.
* Inlined initialize_var_from_initializer().
---
---
 libs/vkd3d-shader/hlsl.c                      |  79 +++++++++++
 libs/vkd3d-shader/hlsl.h                      |   2 +
 libs/vkd3d-shader/hlsl.y                      | 123 +++++-------------
 libs/vkd3d-shader/hlsl_codegen.c              |   2 +-
 tests/hlsl-initializer-flatten.shader_test    |  10 +-
 ...-initializer-invalid-arg-count.shader_test |   4 +-
 .../hlsl-initializer-local-array.shader_test  |   4 +-
 tests/hlsl-initializer-nested.shader_test     |   4 +-
 .../hlsl-initializer-static-array.shader_test |   4 +-
 tests/hlsl-initializer-struct.shader_test     |   6 +-
 10 files changed, 129 insertions(+), 109 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index eabe189f..7239b183 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -232,6 +232,85 @@ static struct hlsl_type *hlsl_new_type(struct hlsl_ctx *ctx, const char *name, e
     return type;
 }
 
+/* Returns the register offset of a given component within a type, given its index.
+ * *comp_type will be set to the type of the component. */
+unsigned int hlsl_compute_component_offset(struct hlsl_ctx *ctx, struct hlsl_type *type,
+        unsigned int idx, struct hlsl_type **comp_type)
+{
+    switch (type->type)
+    {
+        case HLSL_CLASS_SCALAR:
+        case HLSL_CLASS_VECTOR:
+        {
+            assert(idx < type->dimx * type->dimy);
+            *comp_type = hlsl_get_scalar_type(ctx, type->base_type);
+            return idx;
+        }
+        case HLSL_CLASS_MATRIX:
+        {
+            unsigned int minor, major, x = idx % type->dimx, y = idx / type->dimx;
+
+            assert(idx < type->dimx * type->dimy);
+
+            if (hlsl_type_is_row_major(type))
+            {
+                minor = x;
+                major = y;
+            }
+            else
+            {
+                minor = y;
+                major = x;
+            }
+
+            *comp_type = hlsl_get_scalar_type(ctx, type->base_type);
+            return 4 * major + minor;
+        }
+
+        case HLSL_CLASS_ARRAY:
+        {
+            unsigned int elem_comp_count = hlsl_type_component_count(type->e.array.type);
+            unsigned int array_idx = idx / elem_comp_count;
+            unsigned int idx_in_elem = idx % elem_comp_count;
+
+            assert(array_idx < type->e.array.elements_count);
+
+            return array_idx * hlsl_type_get_array_element_reg_size(type->e.array.type) +
+                    hlsl_compute_component_offset(ctx, type->e.array.type, idx_in_elem, comp_type);
+        }
+
+        case HLSL_CLASS_STRUCT:
+        {
+            struct hlsl_struct_field *field;
+
+            LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
+            {
+                unsigned int elem_comp_count = hlsl_type_component_count(field->type);
+
+                if (idx < elem_comp_count)
+                {
+                    return field->reg_offset +
+                            hlsl_compute_component_offset(ctx, field->type, idx, comp_type);
+                }
+                idx -= elem_comp_count;
+            }
+
+            assert(0);
+            return 0;
+        }
+
+        case HLSL_CLASS_OBJECT:
+        {
+            assert(idx == 0);
+            *comp_type = type;
+            return 0;
+        }
+    }
+
+    assert(0);
+    return 0;
+}
+
 struct hlsl_type *hlsl_new_array_type(struct hlsl_ctx *ctx, struct hlsl_type *basic_type, unsigned int array_size)
 {
     struct hlsl_type *type;
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index 802adf87..28b2ff1b 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -786,6 +786,8 @@ struct hlsl_type *hlsl_type_clone(struct hlsl_ctx *ctx, struct hlsl_type *old,
         unsigned int default_majority, unsigned int modifiers);
 unsigned int hlsl_type_component_count(struct hlsl_type *type);
 unsigned int hlsl_type_get_array_element_reg_size(const struct hlsl_type *type);
+unsigned int hlsl_compute_component_offset(struct hlsl_ctx *ctx, struct hlsl_type *type,
+        unsigned int idx, struct hlsl_type **comp_type);
 unsigned int hlsl_type_get_sm4_offset(const struct hlsl_type *type, unsigned int offset);
 bool hlsl_types_are_equal(const struct hlsl_type *t1, const struct hlsl_type *t2);
 
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 968f36ae..d67ffb79 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -1450,77 +1450,44 @@ static bool add_increment(struct hlsl_ctx *ctx, struct list *instrs, bool decrem
     return true;
 }
 
-static void initialize_numeric_var(struct hlsl_ctx *ctx, struct hlsl_ir_var *var,
-        struct parse_initializer *initializer, unsigned int reg_offset, struct hlsl_type *type,
-        unsigned int *initializer_offset)
+static void initialize_var_components(struct hlsl_ctx *ctx, struct list *instrs,
+        struct hlsl_ir_var *dst, unsigned int *store_index, struct hlsl_ir_node *src,
+        const struct vkd3d_shader_location *loc)
 {
-    unsigned int i;
-
-    if (type->type == HLSL_CLASS_MATRIX)
-        hlsl_fixme(ctx, &var->loc, "Matrix initializer.");
+    unsigned int src_comp_count = hlsl_type_component_count(src->data_type);
+    unsigned int k;
 
-    for (i = 0; i < type->dimx; i++)
+    for (k = 0; k < src_comp_count; ++k)
     {
+        struct hlsl_type *dst_comp_type, *src_comp_type;
+        unsigned int dst_reg_offset, src_reg_offset;
         struct hlsl_ir_store *store;
         struct hlsl_ir_constant *c;
-        struct hlsl_ir_node *node;
+        struct hlsl_ir_load *load;
+        struct hlsl_ir_node *conv;
 
-        node = initializer->args[*initializer_offset];
-        *initializer_offset += 1;
+        dst_reg_offset = hlsl_compute_component_offset(ctx, dst->data_type, *store_index, &dst_comp_type);
+        src_reg_offset = hlsl_compute_component_offset(ctx, src->data_type, k, &src_comp_type);
 
-        if (!(node = add_implicit_conversion(ctx, initializer->instrs, node,
-                hlsl_get_scalar_type(ctx, type->base_type), &node->loc)))
+        if (!(c = hlsl_new_uint_constant(ctx, src_reg_offset, loc)))
             return;
+        list_add_tail(instrs, &c->node.entry);
 
-        if (!(c = hlsl_new_uint_constant(ctx, reg_offset + i, &node->loc)))
+        if (!(load = add_load(ctx, instrs, src, &c->node, src_comp_type, *loc)))
             return;
-        list_add_tail(initializer->instrs, &c->node.entry);
 
-        if (!(store = hlsl_new_store(ctx, var, &c->node, node, 0, node->loc)))
+        if (!(conv = add_implicit_conversion(ctx, instrs, &load->node, dst_comp_type, loc)))
             return;
 
-        list_add_tail(initializer->instrs, &store->node.entry);
-    }
-}
-
-static void struct_var_initializer(struct hlsl_ctx *ctx, struct hlsl_ir_var *var,
-        struct parse_initializer *initializer)
-{
-    struct hlsl_type *type = var->data_type;
-    struct hlsl_struct_field *field;
-    unsigned int i = 0;
-
-    if (initializer_size(initializer) != hlsl_type_component_count(type))
-    {
-        hlsl_error(ctx, &var->loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT,
-                "Expected %u components in initializer, but got %u.",
-                hlsl_type_component_count(type), initializer_size(initializer));
-        return;
-    }
-
-    LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
-    {
-        struct hlsl_ir_node *node = initializer->args[i];
-        struct hlsl_ir_store *store;
-        struct hlsl_ir_constant *c;
-
-        if (i++ >= initializer->args_count)
-            break;
+        if (!(c = hlsl_new_uint_constant(ctx, dst_reg_offset, loc)))
+            return;
+        list_add_tail(instrs, &c->node.entry);
 
-        if (hlsl_type_component_count(field->type) == hlsl_type_component_count(node->data_type))
-        {
-            if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, &node->loc)))
-                break;
-            list_add_tail(initializer->instrs, &c->node.entry);
+        if (!(store = hlsl_new_store(ctx, dst, &c->node, conv, 0, *loc)))
+            return;
+        list_add_tail(instrs, &store->node.entry);
 
-            if (!(store = hlsl_new_store(ctx, var, &c->node, node, 0, node->loc)))
-                break;
-            list_add_tail(initializer->instrs, &store->node.entry);
-        }
-        else
-        {
-            hlsl_fixme(ctx, &node->loc, "Implicit cast in structure initializer.");
-        }
+        ++*store_index;
     }
 }
 
@@ -1648,51 +1615,23 @@ static struct list *declare_vars(struct hlsl_ctx *ctx, struct hlsl_type *basic_t
             if (v->initializer.braces)
             {
                 unsigned int size = initializer_size(&v->initializer);
-                unsigned int initializer_offset = 0;
+                unsigned int store_index = 0;
+                unsigned int k;
 
-                if (type->type <= HLSL_CLASS_LAST_NUMERIC && type->dimx * type->dimy != size)
+                if (hlsl_type_component_count(type) != size)
                 {
                     hlsl_error(ctx, &v->loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT,
-                            "Expected %u components in numeric initializer, but got %u.",
-                            type->dimx * type->dimy, v->initializer.args_count);
+                            "Expected %u components in initializer, but got %u.",
+                            hlsl_type_component_count(type), size);
                     free_parse_initializer(&v->initializer);
                     vkd3d_free(v);
                     continue;
                 }
 
-                if ((type->type == HLSL_CLASS_STRUCT || type->type == HLSL_CLASS_ARRAY)
-                        && hlsl_type_component_count(type) != size)
-                {
-                    hlsl_error(ctx, &v->loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT,
-                            "Expected %u components in initializer, but got %u.", hlsl_type_component_count(type), size);
-                    free_parse_initializer(&v->initializer);
-                    vkd3d_free(v);
-                    continue;
-                }
-
-                if (type->type > HLSL_CLASS_LAST_NUMERIC && type->type != HLSL_CLASS_STRUCT)
-                {
-                    FIXME("Initializers for non scalar/struct variables not supported yet.\n");
-                    free_parse_initializer(&v->initializer);
-                    vkd3d_free(v);
-                    continue;
-                }
-
-                if (type->type == HLSL_CLASS_STRUCT)
-                {
-                    struct_var_initializer(ctx, var, &v->initializer);
-                }
-                else
+                for (k = 0; k < v->initializer.args_count; ++k)
                 {
-                    if (v->initializer.args_count != size)
-                    {
-                        hlsl_fixme(ctx, &v->loc, "Flatten initializer.");
-                        free_parse_initializer(&v->initializer);
-                        vkd3d_free(v);
-                        continue;
-                    }
-
-                    initialize_numeric_var(ctx, var, &v->initializer, 0, type, &initializer_offset);
+                    initialize_var_components(ctx, v->initializer.instrs, var,
+                            &store_index, v->initializer.args[k], &v->initializer.args[k]->loc);
                 }
             }
             else
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index 5f1d5997..e945b94d 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -1660,7 +1660,7 @@ bool hlsl_offset_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref
     if (*offset >= deref->var->data_type->reg_size)
     {
         hlsl_error(ctx, &deref->offset.node->loc, VKD3D_SHADER_ERROR_HLSL_OFFSET_OUT_OF_BOUNDS,
-                "Dereference is out of bounds.");
+                "Dereference is out of bounds. %u/%u", *offset, deref->var->data_type->reg_size);
         return false;
     }
 
diff --git a/tests/hlsl-initializer-flatten.shader_test b/tests/hlsl-initializer-flatten.shader_test
index 3a430e0d..6b35c6b7 100644
--- a/tests/hlsl-initializer-flatten.shader_test
+++ b/tests/hlsl-initializer-flatten.shader_test
@@ -6,7 +6,7 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (1, 2, 3, 4)
 
 
@@ -24,7 +24,7 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (4, 5, 6, 7)
 
 
@@ -38,7 +38,7 @@ float4 main() : sv_target
 
 [test]
 draw quad
-todo probe all rgba (40, 10, 20, 30)
+probe all rgba (40, 10, 20, 30)
 
 
 [pixel shader]
@@ -56,7 +56,7 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (1.0, 2.0, 3.0, 4.0)
 
 
@@ -69,5 +69,5 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (1.0, 2.0, 3.0, 4.0)
diff --git a/tests/hlsl-initializer-invalid-arg-count.shader_test b/tests/hlsl-initializer-invalid-arg-count.shader_test
index acd449af..4332fbe8 100644
--- a/tests/hlsl-initializer-invalid-arg-count.shader_test
+++ b/tests/hlsl-initializer-invalid-arg-count.shader_test
@@ -10,7 +10,7 @@ float4 main() : sv_target
 
 [test]
 draw quad
-todo probe all rgba (17, 18, 19, 20)
+probe all rgba (17, 18, 19, 20)
 
 
 [pixel shader fail]
@@ -57,7 +57,7 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (22, 23, 24, 25)
 
 
diff --git a/tests/hlsl-initializer-local-array.shader_test b/tests/hlsl-initializer-local-array.shader_test
index 13670dc6..0862d4c9 100644
--- a/tests/hlsl-initializer-local-array.shader_test
+++ b/tests/hlsl-initializer-local-array.shader_test
@@ -11,7 +11,7 @@ float4 main() : SV_TARGET
 
 [test]
 draw quad
-todo probe all rgba (21, 22, 23, 24)
+probe all rgba (21, 22, 23, 24)
 
 
 [pixel shader]
@@ -32,4 +32,4 @@ float4 main() : SV_TARGET
 
 [test]
 draw quad
-todo probe all rgba (71, 72, 73, 74)
+probe all rgba (71, 72, 73, 74)
diff --git a/tests/hlsl-initializer-nested.shader_test b/tests/hlsl-initializer-nested.shader_test
index bcb37cf4..b00259c9 100644
--- a/tests/hlsl-initializer-nested.shader_test
+++ b/tests/hlsl-initializer-nested.shader_test
@@ -24,7 +24,7 @@ float4 main() : sv_target
 
 [test]
 draw quad
-todo probe all rgba (21, 22, 23, 24)
+probe all rgba (21, 22, 23, 24)
 
 
 [pixel shader]
@@ -52,5 +52,5 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (21, 22, 23, 24)
diff --git a/tests/hlsl-initializer-static-array.shader_test b/tests/hlsl-initializer-static-array.shader_test
index f276c629..57733502 100644
--- a/tests/hlsl-initializer-static-array.shader_test
+++ b/tests/hlsl-initializer-static-array.shader_test
@@ -12,7 +12,7 @@ float4 main() : SV_TARGET
 
 [test]
 draw quad
-todo probe all rgba (21, 22, 23, 24)
+probe all rgba (21, 22, 23, 24)
 
 
 [pixel shader]
@@ -34,4 +34,4 @@ float4 main() : SV_TARGET
 
 [test]
 draw quad
-todo probe all rgba (61, 62, 63, 64)
+probe all rgba (61, 62, 63, 64)
diff --git a/tests/hlsl-initializer-struct.shader_test b/tests/hlsl-initializer-struct.shader_test
index 2a824e23..f4028b5b 100644
--- a/tests/hlsl-initializer-struct.shader_test
+++ b/tests/hlsl-initializer-struct.shader_test
@@ -22,7 +22,7 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (41, 42, 43, 44)
 
 
@@ -52,7 +52,7 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (4311, 4312, 4313, 4314)
 
 
@@ -80,5 +80,5 @@ float4 main() : sv_target
 }
 
 [test]
-todo draw quad
+draw quad
 probe all rgba (21, 22, 23, 24)
-- 
2.35.2




More information about the wine-devel mailing list