From: Nikolay Sivov nsivov@codeweavers.com
Signed-off-by: Nikolay Sivov nsivov@codeweavers.com --- libs/vkd3d-shader/hlsl_codegen.c | 20 ++++++++++++++++++++ tests/hlsl/conditional.shader_test | 17 +++++++++++++++++ tests/hlsl/static-initializer.shader_test | 4 ++-- 3 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 76572cf93..a958bedb7 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -2062,6 +2062,25 @@ static bool remove_trivial_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *i return true; }
+static bool remove_trivial_conditional_branches(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{ + struct hlsl_ir_constant *condition; + struct hlsl_ir_if *iff; + + if (instr->type != HLSL_IR_IF) + return false; + iff = hlsl_ir_if(instr); + if (iff->condition.node->type != HLSL_IR_CONSTANT) + return false; + condition = hlsl_ir_constant(iff->condition.node); + + list_move_before(&instr->entry, condition->value.u[0].u ? &iff->then_block.instrs : &iff->else_block.instrs); + list_remove(&instr->entry); + hlsl_free_instr(instr); + + return true; +} + static bool lower_nonconstant_vector_derefs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block) { struct hlsl_ir_node *idx; @@ -4408,6 +4427,7 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry 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); + progress |= hlsl_transform_ir(ctx, remove_trivial_conditional_branches, body, NULL); } while (progress);
diff --git a/tests/hlsl/conditional.shader_test b/tests/hlsl/conditional.shader_test index b3b18dc1a..810f7366a 100644 --- a/tests/hlsl/conditional.shader_test +++ b/tests/hlsl/conditional.shader_test @@ -82,3 +82,20 @@ float4 main(uniform float4 u) : sv_target uniform 0 float4 0.0 0.0 0.0 0.0 draw quad probe all rgba (0.9, 0.8, 0.7, 0.6) + +[pixel shader] +float4 main() : sv_target +{ + bool c = false; + float a = -1.0f; + if (c) + return float4(1.0, 2.0, 3.0, 4.0); + else if (a > 0) + return float4(5.0, 6.0, 7.0, 8.0); + else + return float4(9.0, 10.0, 11.0, 12.0); +} + +[test] +draw quad +probe all rgba (9.0, 10.0, 11.0, 12.0) diff --git a/tests/hlsl/static-initializer.shader_test b/tests/hlsl/static-initializer.shader_test index 8415d851a..394b4a9af 100644 --- a/tests/hlsl/static-initializer.shader_test +++ b/tests/hlsl/static-initializer.shader_test @@ -85,7 +85,7 @@ float4 main() : sv_target }
-[pixel shader todo] +[pixel shader] // This is allowed in 10.0.10011.16384 but not in 9.29.952.3111 static Texture2D tex; sampler sam; @@ -177,7 +177,7 @@ size (1, 1) 0.5
-[pixel shader todo] +[pixel shader] // This is allowed in 10.0.10011.16384 but not in 9.29.952.3111 static RWTexture2D<float> tex;
This merge request was approved by Giovanni Mascellani.
This merge request was approved by Zebediah Figura.
This merge request was approved by Henri Verbeet.