Module: vkd3d
Branch: master
Commit: 43ff28b00b2be949cb9f5cb31d28a73a4648dde2
URL: https://gitlab.winehq.org/wine/vkd3d/-/commit/43ff28b00b2be949cb9f5cb31d28a…
Author: Francisco Casas <fcasas(a)codeweavers.com>
Date: Thu Jan 11 16:54:20 2024 -0300
vkd3d-shader/hlsl: Emit fixmes on non-constant vector addressing.
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=56162
Storing to a vector component using a non-constant index is not allowed
on profiles lower than 6.0. Unless this happens inside a loop that can be
unrolled, which we are not doing yet.
For this reason, a validate_nonconstant_vector_store_derefs pass is
added to detect these cases.
Ideally we would want to emit an hlsl_error on this pass, but before
implementing loop unrolling, we could reach this point on valid HLSL.
Also, as pointed out by Nikolay in the mentioned bug, currently
new_offset_from_path_index() fails an assertion when this happens,
because it expects an hlsl_ir_constant, so a check is added.
It also felt correct to emit an hlsl_fixme there, despite the
redundancy.
---
libs/vkd3d-shader/hlsl_codegen.c | 39 ++++++++++++++++++++++++++++++++++
tests/hlsl/vector-indexing.shader_test | 13 ++++++++++++
2 files changed, 52 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index acc19dd8..d37bef15 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -32,6 +32,11 @@ static struct hlsl_ir_node *new_offset_from_path_index(struct hlsl_ctx *ctx, str
switch (type->class)
{
case HLSL_CLASS_VECTOR:
+ if (idx->type != HLSL_IR_CONSTANT)
+ {
+ hlsl_fixme(ctx, &idx->loc, "Non-constant vector addressing.");
+ break;
+ }
*offset_component += hlsl_ir_constant(idx)->value.u[0].u;
break;
@@ -2478,6 +2483,38 @@ static bool lower_nonconstant_vector_derefs(struct hlsl_ctx *ctx, struct hlsl_ir
return false;
}
+static bool validate_nonconstant_vector_store_derefs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block)
+{
+ struct hlsl_ir_node *idx;
+ struct hlsl_deref *deref;
+ struct hlsl_type *type;
+ unsigned int i;
+
+ if (instr->type != HLSL_IR_STORE)
+ return false;
+
+ deref = &hlsl_ir_store(instr)->lhs;
+ assert(deref->var);
+
+ if (deref->path_len == 0)
+ return false;
+
+ type = deref->var->data_type;
+ for (i = 0; i < deref->path_len - 1; ++i)
+ type = hlsl_get_element_type_from_path_index(ctx, type, deref->path[i].node);
+
+ idx = deref->path[deref->path_len - 1].node;
+
+ if (type->class == HLSL_CLASS_VECTOR && idx->type != HLSL_IR_CONSTANT)
+ {
+ /* We should turn this into an hlsl_error after we implement unrolling, because if we get
+ * here after that, it means that the HLSL is invalid. */
+ hlsl_fixme(ctx, &instr->loc, "Non-constant vector addressing on store. Unrolling may be missing.");
+ }
+
+ return false;
+}
+
/* Lower combined samples and sampler variables to synthesized separated textures and samplers.
* That is, translate SM1-style samples in the source to SM4-style samples in the bytecode. */
static bool lower_combined_samples(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
@@ -4969,6 +5006,8 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry
lower_ir(ctx, lower_abs, body);
}
+ lower_ir(ctx, validate_nonconstant_vector_store_derefs, body);
+
/* TODO: move forward, remove when no longer needed */
transform_derefs(ctx, replace_deref_path_with_offset, body);
while (hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL));
diff --git a/tests/hlsl/vector-indexing.shader_test b/tests/hlsl/vector-indexing.shader_test
index 9f6f9a2c..446b2d0e 100644
--- a/tests/hlsl/vector-indexing.shader_test
+++ b/tests/hlsl/vector-indexing.shader_test
@@ -49,3 +49,16 @@ float4 main() : SV_TARGET
[test]
draw quad
probe all rgba (2.0, 2.0, 2.0, 2.0)
+
+
+[pixel shader fail(sm<6) todo]
+float4 v;
+float i;
+
+float4 main() : sv_target
+{
+ float4 p = v;
+ p[i] = 2.0;
+
+ return 0;
+}