vkd3d-shader/hlsl: Lower integer modulus for d3dbc target profiles.

This commit is contained in:
Francisco Casas
2025-02-28 18:49:01 -03:00
committed by Henri Verbeet
parent 828afe188c
commit ea99d2c2cd
Notes: Henri Verbeet 2025-03-18 16:03:46 +01:00
Approved-by: Elizabeth Figura (@zfigura)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1427
2 changed files with 50 additions and 10 deletions

View File

@@ -3587,6 +3587,45 @@ static bool lower_trunc(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct
return true;
}
/* Lower modulus using:
*
* mod(x, y) = x - trunc(x / y) * y;
*
*/
static bool lower_int_modulus_sm1(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block)
{
struct hlsl_ir_node *div, *trunc, *mul, *neg, *operands[2], *ret;
struct hlsl_type *float_type;
struct hlsl_ir_expr *expr;
bool is_float;
if (instr->type != HLSL_IR_EXPR)
return false;
expr = hlsl_ir_expr(instr);
if (expr->op != HLSL_OP2_MOD)
return false;
is_float = instr->data_type->e.numeric.type == HLSL_TYPE_FLOAT
|| instr->data_type->e.numeric.type == HLSL_TYPE_HALF;
if (is_float)
return false;
float_type = hlsl_get_vector_type(ctx, HLSL_TYPE_FLOAT, instr->data_type->e.numeric.dimx);
for (unsigned int i = 0; i < 2; ++i)
{
operands[i] = hlsl_block_add_cast(ctx, block, expr->operands[i].node, float_type, &instr->loc);
}
div = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_DIV, operands[0], operands[1]);
trunc = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_TRUNC, div, &instr->loc);
mul = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_MUL, trunc, operands[1]);
neg = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_NEG, mul, &instr->loc);
ret = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_ADD, operands[0], neg);
hlsl_block_add_cast(ctx, block, ret, instr->data_type, &instr->loc);
return true;
}
/* Lower DIV to RCP + MUL. */
static bool lower_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block)
{
@@ -12698,6 +12737,7 @@ static void process_entry_function(struct hlsl_ctx *ctx,
while (lower_ir(ctx, lower_nonconstant_array_loads, body));
lower_ir(ctx, lower_ternary, body);
lower_ir(ctx, lower_int_modulus_sm1, body);
lower_ir(ctx, lower_division, body);
/* Constants casted to float must be folded, and new casts to bool also need to be lowered. */
hlsl_transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL);

View File

@@ -13,7 +13,7 @@ uniform 0 float4 5.0 16.0 0.0 0.0
todo(glsl | msl) draw quad
probe (0, 0) rgba (21.0, -11.0, 80.0, 0.0)
[pixel shader todo(sm<4)]
[pixel shader]
uniform float2 a;
float4 main() : SV_TARGET
@@ -25,7 +25,7 @@ float4 main() : SV_TARGET
[test]
uniform 0 float4 5.0 16.0 0.0 0.0
todo(sm<4 | glsl | msl) draw quad
todo(glsl | msl) draw quad
probe (0, 0) rgba (5.0, 5.0, -5.0, 3.0)
[pixel shader]
@@ -43,7 +43,7 @@ uniform 0 float4 42.0 5.0 0.0 0.0
todo(glsl | msl) draw quad
probe (0, 0) rgba (8.0, -8.0, -8.0, 8.0)
[pixel shader todo(sm<4)]
[pixel shader]
uniform float2 a;
float4 main() : SV_TARGET
@@ -55,7 +55,7 @@ float4 main() : SV_TARGET
[test]
uniform 0 float4 42.0 5.0 0.0 0.0
todo(sm<4 | glsl | msl) draw quad
todo(glsl | msl) draw quad
probe (0, 0) rgba (2.0, -2.0, 2.0, -2.0)
[pixel shader]
@@ -73,7 +73,7 @@ uniform 0 float4 45.0 5.0 0.0 0.0
todo(glsl | msl) draw quad
probe (0, 0) rgba (9.0, -9.0, -9.0, 9.0)
[pixel shader todo(sm<4)]
[pixel shader]
uniform float2 a;
float4 main() : SV_TARGET
@@ -85,7 +85,7 @@ float4 main() : SV_TARGET
[test]
uniform 0 float4 45.0 5.0 0.0 0.0
todo(sm<4 | glsl | msl) draw quad
todo(glsl | msl) draw quad
probe (0, 0) rgba (0.0, 0.0, 0.0, 0.0)
[pixel shader]
@@ -101,7 +101,7 @@ uniform 0 float4 5.0 -7.0 0.0 -10.0
todo(msl) draw quad
probe (0, 0) rgba (5.0, 7.0, 0.0, 10.0)
[pixel shader todo(sm<4)]
[pixel shader]
uniform float4 a;
uniform float4 b;
@@ -117,10 +117,10 @@ float4 main() : sv_target
[test]
uniform 0 float4 45.0 5.0 50.0 10.0
uniform 4 float4 3.0 8.0 2.0 5.0
todo(sm<4 | glsl | msl) draw quad
todo(glsl | msl) draw quad
probe (0, 0) rgba (9.0, 5.0, 1.0, 3.0)
[pixel shader todo(sm<4)]
[pixel shader]
float f;
float4 main() : sv_target
@@ -133,7 +133,7 @@ float4 main() : sv_target
[test]
uniform 0 float 10.0
todo(sm<4 | glsl | msl) draw quad
todo(glsl | msl) draw quad
if(sm<4) probe (0, 0) rgba(6, 6, -6, -6)
if(sm>=4) probe (0, 0) rgba(7, 7, -7, -7)