[PATCH vkd3d v3 09/11] vkd3d-shader/hlsl: Support all complex initalizers.
Giovanni Mascellani
gmascellani at codeweavers.com
Fri Apr 22 05:25:05 CDT 2022
From: Francisco Casas <fcasas at codeweavers.com>
---
v3:
* Patch reworked from scratch by Francisco
* Some tests are still failing because they depend on matrix copying
---
libs/vkd3d-shader/hlsl.c | 83 ++++++++++++
libs/vkd3d-shader/hlsl.h | 2 +
libs/vkd3d-shader/hlsl.y | 124 +++++-------------
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, 137 insertions(+), 106 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index eabe189f..66fdd45b 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -232,6 +232,89 @@ 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, unless it is NULL. */
+unsigned int hlsl_compute_component_reg_offset(struct hlsl_ctx *ctx, struct hlsl_type *type,
+ unsigned int idx, struct hlsl_type **comp_type, const struct vkd3d_shader_location *loc)
+{
+ switch (type->type)
+ {
+ case HLSL_CLASS_SCALAR:
+ case HLSL_CLASS_VECTOR:
+ {
+ assert(idx < type->dimx * type->dimy);
+
+ if (comp_type)
+ *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;
+ }
+
+ if (comp_type)
+ *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 - array_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_reg_offset(ctx, type->e.array.type, idx_in_elem, comp_type, loc);
+ }
+
+ 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_reg_offset(ctx, field->type, idx, comp_type, loc);
+ }
+ idx -= elem_comp_count;
+ }
+
+ assert(0);
+ return 0;
+ }
+
+ case HLSL_CLASS_OBJECT:
+ {
+ assert(idx == 0);
+ if (comp_type)
+ *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..b50a7f24 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_reg_offset(struct hlsl_ctx *ctx, struct hlsl_type *type,
+ unsigned int idx, struct hlsl_type **comp_type, const struct vkd3d_shader_location *loc);
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 ec0e23bb..f072aa15 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -1454,77 +1454,57 @@ 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;
+
+ dst_reg_offset = hlsl_compute_component_reg_offset(ctx, dst->data_type, *store_index, &dst_comp_type, loc);
+ src_reg_offset = hlsl_compute_component_reg_offset(ctx, src->data_type, k, &src_comp_type, loc);
+
+ if (!(c = hlsl_new_uint_constant(ctx, src_reg_offset, loc)))
+ return;
+ list_add_tail(instrs, &c->node.entry);
- node = initializer->args[*initializer_offset];
- *initializer_offset += 1;
+ if (!(load = add_load(ctx, instrs, src, &c->node, src_comp_type, *loc)))
+ return;
- if (!(node = add_implicit_conversion(ctx, initializer->instrs, node,
- hlsl_get_scalar_type(ctx, type->base_type), &node->loc)))
+ if (!(conv = add_implicit_conversion(ctx, instrs, &load->node, dst_comp_type, loc)))
return;
- if (!(c = hlsl_new_uint_constant(ctx, reg_offset + i, &node->loc)))
+ if (!(c = hlsl_new_uint_constant(ctx, dst_reg_offset, loc)))
return;
- list_add_tail(initializer->instrs, &c->node.entry);
+ list_add_tail(instrs, &c->node.entry);
- if (!(store = hlsl_new_store(ctx, var, &c->node, node, 0, node->loc)))
+ if (!(store = hlsl_new_store(ctx, dst, &c->node, conv, 0, *loc)))
return;
+ list_add_tail(instrs, &store->node.entry);
- list_add_tail(initializer->instrs, &store->node.entry);
+ ++*store_index;
}
}
-static void struct_var_initializer(struct hlsl_ctx *ctx, struct hlsl_ir_var *var,
+static void initialize_var_from_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;
+ unsigned int store_index = 0;
+ unsigned int i;
- if (initializer_size(initializer) != hlsl_type_component_count(type))
+ for (i = 0; i < initializer->args_count; ++i)
{
- 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 (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, 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.");
- }
+ initialize_var_components(ctx, initializer->instrs, var, &store_index, initializer->args[i],
+ &initializer->args[i]->loc);
}
}
@@ -1652,52 +1632,18 @@ 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;
-
- if (type->type <= HLSL_CLASS_LAST_NUMERIC && type->dimx * type->dimy != 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);
- 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)
+ if (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);
+ "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
- {
- 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_from_initializer(ctx, var, &v->initializer);
}
else
{
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index f31bfe9f..9eb2fccb 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -1653,7 +1653,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