vkd3d-shader/hlsl: Remove conditional branching when condition is a compile time constant.

Signed-off-by: Nikolay Sivov <nsivov@codeweavers.com>
This commit is contained in:
Nikolay Sivov 2023-09-27 19:17:40 +02:00 committed by Alexandre Julliard
parent f3389789b2
commit 7c378cc6f9
Notes: Alexandre Julliard 2023-10-05 22:37:46 +02:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Zebediah Figura (@zfigura)
Approved-by: Henri Verbeet (@hverbeet)
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/375
3 changed files with 39 additions and 2 deletions

View File

@ -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;
@ -4417,6 +4436,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);

View File

@ -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)

View File

@ -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;