vkd3d-shader/hlsl: Introduce hlsl_block_add_constant().

This commit is contained in:
Elizabeth Figura
2025-08-20 17:45:22 -05:00
committed by Henri Verbeet
parent dedb14e55e
commit deb7a67d67
Notes: Henri Verbeet 2025-10-28 16:58:15 +01:00
Approved-by: Francisco Casas (@fcasas)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1794
4 changed files with 36 additions and 87 deletions

View File

@@ -1737,6 +1737,12 @@ struct hlsl_ir_node *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *t
return &c->node;
}
struct hlsl_ir_node *hlsl_block_add_constant(struct hlsl_ctx *ctx, struct hlsl_block *block,
struct hlsl_type *type, const struct hlsl_constant_value *value, const struct vkd3d_shader_location *loc)
{
return append_new_instr(ctx, block, hlsl_new_constant(ctx, type, value, loc));
}
struct hlsl_ir_node *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc)
{
struct hlsl_constant_value value;

View File

@@ -1579,6 +1579,8 @@ struct hlsl_ir_node *hlsl_block_add_binary_expr(struct hlsl_ctx *ctx, struct hls
enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg1, struct hlsl_ir_node *arg2);
struct hlsl_ir_node *hlsl_block_add_cast(struct hlsl_ctx *ctx, struct hlsl_block *block,
struct hlsl_ir_node *arg, struct hlsl_type *type, const struct vkd3d_shader_location *loc);
struct hlsl_ir_node *hlsl_block_add_constant(struct hlsl_ctx *ctx, struct hlsl_block *block,
struct hlsl_type *type, const struct hlsl_constant_value *value, const struct vkd3d_shader_location *loc);
struct hlsl_ir_node *hlsl_block_add_expr(struct hlsl_ctx *ctx, struct hlsl_block *block,
enum hlsl_ir_expr_op op, struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS],
struct hlsl_type *data_type, const struct vkd3d_shader_location *loc);

View File

@@ -3038,10 +3038,7 @@ static struct hlsl_ir_node *add_user_call(struct hlsl_ctx *ctx,
if (!param->default_values[j].string)
{
value.u[0] = param->default_values[j].number;
if (!(comp = hlsl_new_constant(ctx, type, &value, loc)))
return NULL;
hlsl_block_add_instr(args->instrs, comp);
comp = hlsl_block_add_constant(ctx, args->instrs, type, &value, loc);
hlsl_block_add_store_component(ctx, args->instrs, &param_deref, j, comp);
}
}
@@ -3956,7 +3953,6 @@ static bool intrinsic_firstbithigh(struct hlsl_ctx *ctx,
struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS] = {0};
struct hlsl_type *type = params->args[0]->data_type;
struct hlsl_ir_node *c, *clz, *eq, *xor;
struct hlsl_constant_value v;
if (hlsl_version_lt(ctx, 4, 0))
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INCOMPATIBLE_PROFILE,
@@ -3978,20 +3974,14 @@ static bool intrinsic_firstbithigh(struct hlsl_ctx *ctx,
if (hlsl_version_lt(ctx, 5, 0))
return add_expr(ctx, params->instrs, HLSL_OP1_FIND_MSB, operands, type, loc);
v.u[0].u = 0x1f;
if (!(c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), &v, loc)))
return false;
hlsl_block_add_instr(params->instrs, c);
c = hlsl_block_add_uint_constant(ctx, params->instrs, 0x1f, loc);
if (!(clz = add_expr(ctx, params->instrs, HLSL_OP1_CLZ, operands, type, loc)))
return false;
if (!(xor = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_BIT_XOR, c, clz, loc)))
return false;
v.u[0].i = -1;
if (!(c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), &v, loc)))
return false;
hlsl_block_add_instr(params->instrs, c);
c = hlsl_block_add_uint_constant(ctx, params->instrs, ~0u, loc);
if (!(eq = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_EQUAL, clz, c, loc)))
return false;
@@ -4043,9 +4033,7 @@ static bool intrinsic_fmod(struct hlsl_ctx *ctx, const struct parse_initializer
if (!(div = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_DIV, x, y, loc)))
return false;
if (!(zero = hlsl_new_constant(ctx, div->data_type, &zero_value, loc)))
return false;
hlsl_block_add_instr(params->instrs, zero);
zero = hlsl_block_add_constant(ctx, params->instrs, div->data_type, &zero_value, loc);
if (!(abs = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_ABS, div, loc)))
return false;
@@ -4641,9 +4629,8 @@ static bool intrinsic_sign(struct hlsl_ctx *ctx,
struct hlsl_type *int_type = hlsl_get_numeric_type(ctx, arg->data_type->class, HLSL_TYPE_INT,
arg->data_type->e.numeric.dimx, arg->data_type->e.numeric.dimy);
if (!(zero = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, arg->data_type->e.numeric.type), &zero_value, loc)))
return false;
hlsl_block_add_instr(params->instrs, zero);
zero = hlsl_block_add_constant(ctx, params->instrs,
hlsl_get_scalar_type(ctx, arg->data_type->e.numeric.type), &zero_value, loc);
/* Check if 0 < arg, cast bool to int */

View File

@@ -4058,9 +4058,7 @@ static struct hlsl_ir_node *lower_nonconstant_vector_derefs(struct hlsl_ctx *ctx
value.u[1].u = 1;
value.u[2].u = 2;
value.u[3].u = 3;
if (!(c = hlsl_new_constant(ctx, hlsl_get_vector_type(ctx, HLSL_TYPE_UINT, width), &value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, c);
c = hlsl_block_add_constant(ctx, block, hlsl_get_vector_type(ctx, HLSL_TYPE_UINT, width), &value, &instr->loc);
operands[0] = swizzle;
operands[1] = c;
@@ -4172,10 +4170,7 @@ static struct hlsl_ir_node *lower_nonconstant_array_loads(struct hlsl_ctx *ctx,
if (!(var = hlsl_new_synthetic_var(ctx, row_major ? "row_major-load" : "array-load", instr->data_type, &instr->loc)))
return NULL;
if (!(zero = hlsl_new_constant(ctx, instr->data_type, &zero_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, zero);
zero = hlsl_block_add_constant(ctx, block, instr->data_type, &zero_value, &instr->loc);
hlsl_block_add_simple_store(ctx, block, var, zero);
TRACE("Lowering non-constant %s load on variable '%s'.\n", row_major ? "row_major" : "array", deref->var->name);
@@ -4540,17 +4535,13 @@ static struct hlsl_ir_node *lower_trunc(struct hlsl_ctx *ctx, struct hlsl_ir_nod
struct hlsl_constant_value zero_value, one_value;
memset(&zero_value, 0, sizeof(zero_value));
if (!(zero = hlsl_new_constant(ctx, arg->data_type, &zero_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, zero);
zero = hlsl_block_add_constant(ctx, block, arg->data_type, &zero_value, &instr->loc);
one_value.u[0].f = 1.0;
one_value.u[1].f = 1.0;
one_value.u[2].f = 1.0;
one_value.u[3].f = 1.0;
if (!(one = hlsl_new_constant(ctx, arg->data_type, &one_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, one);
one = hlsl_block_add_constant(ctx, block, arg->data_type, &one_value, &instr->loc);
fract = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_FRACT, arg, &instr->loc);
neg_fract = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_NEG, fract, &instr->loc);
@@ -4745,9 +4736,7 @@ static struct hlsl_ir_node *lower_round(struct hlsl_ctx *ctx, struct hlsl_ir_nod
component_count = hlsl_type_component_count(type);
for (i = 0; i < component_count; ++i)
half_value.u[i].f = 0.5f;
if (!(half = hlsl_new_constant(ctx, type, &half_value, &expr->node.loc)))
return NULL;
hlsl_block_add_instr(block, half);
half = hlsl_block_add_constant(ctx, block, type, &half_value, &expr->node.loc);
sum = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_ADD, arg, half);
frc = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_FRACT, sum, &instr->loc);
@@ -4828,15 +4817,10 @@ static struct hlsl_ir_node *lower_trig(struct hlsl_ctx *ctx, struct hlsl_ir_node
neg_pi_value.u[i].f = -M_PI;
}
if (!(half = hlsl_new_constant(ctx, type, &half_value, &instr->loc))
|| !(two_pi = hlsl_new_constant(ctx, type, &two_pi_value, &instr->loc))
|| !(reciprocal_two_pi = hlsl_new_constant(ctx, type, &reciprocal_two_pi_value, &instr->loc))
|| !(neg_pi = hlsl_new_constant(ctx, type, &neg_pi_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, half);
hlsl_block_add_instr(block, two_pi);
hlsl_block_add_instr(block, reciprocal_two_pi);
hlsl_block_add_instr(block, neg_pi);
half = hlsl_block_add_constant(ctx, block, type, &half_value, &instr->loc);
two_pi = hlsl_block_add_constant(ctx, block, type, &two_pi_value, &instr->loc);
reciprocal_two_pi = hlsl_block_add_constant(ctx, block, type, &reciprocal_two_pi_value, &instr->loc);
neg_pi = hlsl_block_add_constant(ctx, block, type, &neg_pi_value, &instr->loc);
if (!(mad = hlsl_new_ternary_expr(ctx, HLSL_OP3_MAD, arg, reciprocal_two_pi, half)))
return NULL;
@@ -4905,10 +4889,7 @@ static struct hlsl_ir_node *lower_logic_not(struct hlsl_ctx *ctx, struct hlsl_ir
one_value.u[1].f = 1.0;
one_value.u[2].f = 1.0;
one_value.u[3].f = 1.0;
if (!(one = hlsl_new_constant(ctx, float_type, &one_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, one);
one = hlsl_block_add_constant(ctx, block, float_type, &one_value, &instr->loc);
sub = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_ADD, one, neg);
memset(operands, 0, sizeof(operands));
@@ -5069,10 +5050,7 @@ static struct hlsl_ir_node *lower_comparison_operators(struct hlsl_ctx *ctx, str
one_value.u[1].f = 1.0;
one_value.u[2].f = 1.0;
one_value.u[3].f = 1.0;
if (!(one = hlsl_new_constant(ctx, float_type, &one_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, one);
one = hlsl_block_add_constant(ctx, block, float_type, &one_value, &instr->loc);
slt_neg = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_NEG, slt, &instr->loc);
res = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_ADD, one, slt_neg);
}
@@ -5119,17 +5097,13 @@ static struct hlsl_ir_node *lower_slt(struct hlsl_ctx *ctx, struct hlsl_ir_node
sub = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_ADD, arg1_cast, neg);
memset(&zero_value, 0, sizeof(zero_value));
if (!(zero = hlsl_new_constant(ctx, float_type, &zero_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, zero);
zero = hlsl_block_add_constant(ctx, block, float_type, &zero_value, &instr->loc);
one_value.u[0].f = 1.0;
one_value.u[1].f = 1.0;
one_value.u[2].f = 1.0;
one_value.u[3].f = 1.0;
if (!(one = hlsl_new_constant(ctx, float_type, &one_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, one);
one = hlsl_block_add_constant(ctx, block, float_type, &one_value, &instr->loc);
if (!(cmp = hlsl_new_ternary_expr(ctx, HLSL_OP3_CMP, sub, zero, one)))
return NULL;
@@ -5168,17 +5142,13 @@ static struct hlsl_ir_node *lower_cmp(struct hlsl_ctx *ctx, struct hlsl_ir_node
}
memset(&zero_value, 0, sizeof(zero_value));
if (!(zero = hlsl_new_constant(ctx, float_type, &zero_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, zero);
zero = hlsl_block_add_constant(ctx, block, float_type, &zero_value, &instr->loc);
one_value.u[0].f = 1.0;
one_value.u[1].f = 1.0;
one_value.u[2].f = 1.0;
one_value.u[3].f = 1.0;
if (!(one = hlsl_new_constant(ctx, float_type, &one_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, one);
one = hlsl_block_add_constant(ctx, block, float_type, &one_value, &instr->loc);
slt = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_SLT, args_cast[0], zero);
mul1 = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_MUL, args_cast[2], slt);
@@ -5210,11 +5180,7 @@ static struct hlsl_ir_node *lower_casts_to_bool(struct hlsl_ctx *ctx,
/* Narrowing casts should have already been lowered. */
VKD3D_ASSERT(type->e.numeric.dimx == arg_type->e.numeric.dimx);
zero = hlsl_new_constant(ctx, arg_type, &zero_value, &instr->loc);
if (!zero)
return NULL;
hlsl_block_add_instr(block, zero);
zero = hlsl_block_add_constant(ctx, block, arg_type, &zero_value, &instr->loc);
neq = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_NEQUAL, expr->operands[0].node, zero);
neq->data_type = expr->node.data_type;
return neq;
@@ -5267,10 +5233,7 @@ static struct hlsl_ir_node *lower_int_division_sm4(struct hlsl_ctx *ctx,
for (i = 0; i < type->e.numeric.dimx; ++i)
high_bit_value.u[i].u = 0x80000000;
if (!(high_bit = hlsl_new_constant(ctx, type, &high_bit_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, high_bit);
high_bit = hlsl_block_add_constant(ctx, block, type, &high_bit_value, &instr->loc);
and = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_BIT_AND, xor, high_bit);
abs1 = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_ABS, arg1, &instr->loc);
cast1 = hlsl_block_add_cast(ctx, block, abs1, utype, &instr->loc);
@@ -5306,10 +5269,7 @@ static struct hlsl_ir_node *lower_int_modulus_sm4(struct hlsl_ctx *ctx,
for (i = 0; i < type->e.numeric.dimx; ++i)
high_bit_value.u[i].u = 0x80000000;
if (!(high_bit = hlsl_new_constant(ctx, type, &high_bit_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, high_bit);
high_bit = hlsl_block_add_constant(ctx, block, type, &high_bit_value, &instr->loc);
and = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_BIT_AND, arg1, high_bit);
abs1 = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_ABS, arg1, &instr->loc);
cast1 = hlsl_block_add_cast(ctx, block, abs1, utype, &instr->loc);
@@ -5418,10 +5378,7 @@ static struct hlsl_ir_node *lower_float_modulus(struct hlsl_ctx *ctx,
for (i = 0; i < type->e.numeric.dimx; ++i)
one_value.u[i].f = 1.0f;
if (!(one = hlsl_new_constant(ctx, type, &one_value, &instr->loc)))
return NULL;
hlsl_block_add_instr(block, one);
one = hlsl_block_add_constant(ctx, block, type, &one_value, &instr->loc);
div = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_DIV, one, cond);
mul2 = hlsl_block_add_binary_expr(ctx, block, HLSL_OP2_MUL, div, arg1);
frc = hlsl_block_add_unary_expr(ctx, block, HLSL_OP1_FRACT, mul2, &instr->loc);
@@ -5447,9 +5404,7 @@ static bool lower_discard_neg(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
hlsl_block_init(&block);
arg_type = jump->condition.node->data_type;
if (!(zero = hlsl_new_constant(ctx, arg_type, &zero_value, &instr->loc)))
return false;
hlsl_block_add_instr(&block, zero);
zero = hlsl_block_add_constant(ctx, &block, arg_type, &zero_value, &instr->loc);
operands[0] = jump->condition.node;
operands[1] = zero;
@@ -5457,9 +5412,8 @@ static bool lower_discard_neg(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
arg_type->e.numeric.dimx, arg_type->e.numeric.dimy);
cmp = hlsl_block_add_expr(ctx, &block, HLSL_OP2_LESS, operands, cmp_type, &instr->loc);
if (!(bool_false = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), &zero_value, &instr->loc)))
return false;
hlsl_block_add_instr(&block, bool_false);
bool_false = hlsl_block_add_constant(ctx, &block,
hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), &zero_value, &instr->loc);
or = bool_false;