From d856be05196adc2dc83c5228acf2a171fe9a5899 Mon Sep 17 00:00:00 2001 From: Nikolay Sivov Date: Fri, 9 Jun 2023 15:39:38 +0200 Subject: [PATCH] vkd3d-shader/hlsl: Handle static constants in array size expressions. --- libs/vkd3d-shader/hlsl.h | 1 + libs/vkd3d-shader/hlsl.y | 9 +++++++-- libs/vkd3d-shader/hlsl_codegen.c | 4 ++-- tests/hlsl-array-size-expr.shader_test | 6 +++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 3f43ab25..a797ef6a 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1213,6 +1213,7 @@ bool hlsl_offset_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref unsigned int hlsl_offset_from_deref_safe(struct hlsl_ctx *ctx, const struct hlsl_deref *deref); struct hlsl_reg hlsl_reg_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref); +bool hlsl_copy_propagation_execute(struct hlsl_ctx *ctx, struct hlsl_block *block); bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context); bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context); bool hlsl_transform_ir(struct hlsl_ctx *ctx, bool (*func)(struct hlsl_ctx *ctx, struct hlsl_ir_node *, void *), diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index c25f5c4a..0e07fe57 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -1098,12 +1098,17 @@ static unsigned int evaluate_static_expression_as_uint(struct hlsl_ctx *ctx, str struct hlsl_ir_constant *constant; struct hlsl_ir_node *node; unsigned int ret = 0; + bool progress; if (!add_implicit_conversion(ctx, &block->instrs, node_from_list(&block->instrs), hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc)) return 0; - while (hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, block, NULL)); + do + { + progress = hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, block, NULL); + progress |= hlsl_copy_propagation_execute(ctx, block); + } while (progress); node = node_from_list(&block->instrs); if (node->type == HLSL_IR_CONSTANT) @@ -5494,7 +5499,7 @@ arrays: uint32_t *new_array; unsigned int size; - hlsl_block_init(&block); + hlsl_clone_block(ctx, &block, &ctx->static_initializers); list_move_tail(&block.instrs, $2); size = evaluate_static_expression_as_uint(ctx, &block, &@2); diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 7a235986..96b4cb66 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1649,7 +1649,7 @@ static bool copy_propagation_transform_block(struct hlsl_ctx *ctx, struct hlsl_b return progress; } -static bool copy_propagation_execute(struct hlsl_ctx *ctx, struct hlsl_block *block) +bool hlsl_copy_propagation_execute(struct hlsl_ctx *ctx, struct hlsl_block *block) { struct copy_propagation_state state; bool progress; @@ -4073,7 +4073,7 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry { progress = hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL); progress |= hlsl_transform_ir(ctx, hlsl_fold_constant_swizzles, body, NULL); - progress |= copy_propagation_execute(ctx, body); + progress |= hlsl_copy_propagation_execute(ctx, body); progress |= hlsl_transform_ir(ctx, fold_swizzle_chains, body, NULL); progress |= hlsl_transform_ir(ctx, remove_trivial_swizzles, body, NULL); } diff --git a/tests/hlsl-array-size-expr.shader_test b/tests/hlsl-array-size-expr.shader_test index 7235ac73..ac774be1 100644 --- a/tests/hlsl-array-size-expr.shader_test +++ b/tests/hlsl-array-size-expr.shader_test @@ -39,7 +39,7 @@ float4 main() : SV_TARGET draw quad probe all rgba (61, 62, 63, 64) -[pixel shader todo] +[pixel shader] static const int size = 8; static const float array[size] = {1, 2, 3, 4, 5, 6, 7, 8}; @@ -49,5 +49,5 @@ float4 main() : sv_target } [test] -todo draw quad -todo probe all rgba (2, 3, 6, 1) +draw quad +probe all rgba (2, 3, 6, 1)