From: Zebediah Figura zfigura@codeweavers.com
--- libs/vkd3d-shader/hlsl.c | 38 ++++++++--------- libs/vkd3d-shader/hlsl.h | 2 +- libs/vkd3d-shader/hlsl.y | 22 +++++----- libs/vkd3d-shader/hlsl_codegen.c | 36 +++++++++------- libs/vkd3d-shader/hlsl_constant_ops.c | 60 +++++++++++++-------------- 5 files changed, 79 insertions(+), 79 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 152ec627..203b86fd 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1147,7 +1147,7 @@ struct hlsl_ir_node *hlsl_new_call(struct hlsl_ctx *ctx, struct hlsl_ir_function }
struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *type, - const struct vkd3d_shader_location *loc) + const struct hlsl_constant_value *value, const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c;
@@ -1157,53 +1157,54 @@ struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_typ return NULL;
init_node(&c->node, HLSL_IR_CONSTANT, type, loc); + c->value = *value;
return c; }
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; struct hlsl_ir_constant *c;
- if ((c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), loc))) - c->value.u[0].u = b ? ~0u : 0; - + value.u[0].u = b ? ~0u : 0; + if (!(c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), &value, loc))) + return NULL; return &c->node; }
struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, const struct vkd3d_shader_location *loc) { + struct hlsl_constant_value value; struct hlsl_ir_constant *c;
- if ((c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_FLOAT), loc))) - c->value.u[0].f = f; - + value.u[0].f = f; + if (!(c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_FLOAT), &value, loc))) + return NULL; return &c->node; }
struct hlsl_ir_node *hlsl_new_int_constant(struct hlsl_ctx *ctx, int32_t n, const struct vkd3d_shader_location *loc) { + struct hlsl_constant_value value; struct hlsl_ir_constant *c;
- c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), loc); - - if (c) - c->value.u[0].i = n; - + value.u[0].i = n; + if (!(c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), &value, loc))) + return NULL; return &c->node; }
struct hlsl_ir_node *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n, const struct vkd3d_shader_location *loc) { + struct hlsl_constant_value value; struct hlsl_ir_constant *c;
- c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc); - - if (c) - c->value.u[0].u = n; - + value.u[0].u = n; + if (!(c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), &value, loc))) + return NULL; return &c->node; }
@@ -1548,9 +1549,8 @@ static struct hlsl_ir_node *clone_constant(struct hlsl_ctx *ctx, struct hlsl_ir_ { struct hlsl_ir_constant *dst;
- if (!(dst = hlsl_new_constant(ctx, src->node.data_type, &src->node.loc))) + if (!(dst = hlsl_new_constant(ctx, src->node.data_type, &src->value, &src->node.loc))) return NULL; - dst->value = src->value; return &dst->node; }
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 7d02448e..d0227f0d 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1107,7 +1107,7 @@ struct hlsl_ir_node *hlsl_new_call(struct hlsl_ctx *ctx, struct hlsl_ir_function struct hlsl_ir_node *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type, const struct vkd3d_shader_location *loc); struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *type, - const struct vkd3d_shader_location *loc); + const struct hlsl_constant_value *value, const struct vkd3d_shader_location *loc); struct hlsl_ir_node *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node); struct hlsl_ir_node *hlsl_new_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS], diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index cf483d82..f5a4a751 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2697,8 +2697,8 @@ static bool intrinsic_fmod(struct hlsl_ctx *ctx, const struct parse_initializer const struct vkd3d_shader_location *loc) { struct hlsl_ir_node *x, *y, *div, *abs, *frac, *neg_frac, *ge, *select; + static const struct hlsl_constant_value zero_value; struct hlsl_ir_constant *zero; - unsigned int count, i;
if (!(x = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) return false; @@ -2709,14 +2709,10 @@ 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, loc))) + if (!(zero = hlsl_new_constant(ctx, div->data_type, &zero_value, loc))) return false; list_add_tail(params->instrs, &zero->node.entry);
- count = hlsl_type_element_count(div->data_type); - for (i = 0; i < count; ++i) - zero->value.u[i].f = 0.0f; - if (!(abs = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_ABS, div, loc))) return false;
@@ -2825,6 +2821,7 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, { struct hlsl_ir_node *n_l_neg, *n_h_neg, *specular_or, *specular_pow, *load; struct hlsl_ir_node *n_l, *n_h, *m, *diffuse, *zero, *store; + struct hlsl_constant_value init_value; struct hlsl_ir_constant *init; struct hlsl_ir_load *var_load; struct hlsl_deref var_deref; @@ -2855,12 +2852,12 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx, return false; hlsl_init_simple_deref_from_var(&var_deref, var);
- if (!(init = hlsl_new_constant(ctx, ret_type, loc))) + init_value.u[0].f = 1.0f; + init_value.u[1].f = 0.0f; + init_value.u[2].f = 0.0f; + init_value.u[3].f = 1.0f; + if (!(init = hlsl_new_constant(ctx, ret_type, &init_value, loc))) return false; - init->value.u[0].f = 1.0f; - init->value.u[1].f = 0.0f; - init->value.u[2].f = 0.0f; - init->value.u[3].f = 1.0f; list_add_tail(params->instrs, &init->node.entry);
if (!(store = hlsl_new_simple_store(ctx, var, &init->node))) @@ -3162,12 +3159,13 @@ static bool intrinsic_sign(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { struct hlsl_ir_node *lt, *neg, *op1, *op2, *arg = params->args[0]; + static const struct hlsl_constant_value zero_value; struct hlsl_ir_constant *zero;
struct hlsl_type *int_type = hlsl_get_numeric_type(ctx, arg->data_type->class, HLSL_TYPE_INT, arg->data_type->dimx, arg->data_type->dimy);
- if (!(zero = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, arg->data_type->base_type), loc))) + if (!(zero = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, arg->data_type->base_type), &zero_value, loc))) return false; list_add_tail(params->instrs, &zero->node.entry);
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 2b6c595a..d92b0ac9 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1342,9 +1342,8 @@ static bool copy_propagation_replace_with_constant_vector(struct hlsl_ctx *ctx, values.u[i] = hlsl_ir_constant(value->node)->value.u[value->component]; }
- if (!(cons = hlsl_new_constant(ctx, instr->data_type, &instr->loc))) + if (!(cons = hlsl_new_constant(ctx, instr->data_type, &values, &instr->loc))) return false; - cons->value = values; list_add_before(&instr->entry, &cons->node.entry);
TRACE("Load from %s[%u-%u]%s turned into a constant %p.\n", @@ -2107,6 +2106,7 @@ static bool lower_round(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void * { struct hlsl_ir_node *arg, *neg, *sum, *frc, *replacement; struct hlsl_type *type = instr->data_type; + struct hlsl_constant_value half_value; unsigned int i, component_count; struct hlsl_ir_constant *half; struct hlsl_ir_expr *expr; @@ -2119,12 +2119,12 @@ static bool lower_round(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void * if (expr->op != HLSL_OP1_ROUND) return false;
- if (!(half = hlsl_new_constant(ctx, type, &expr->node.loc))) - return false; - component_count = hlsl_type_component_count(type); for (i = 0; i < component_count; ++i) - half->value.u[i].f = 0.5f; + half_value.u[i].f = 0.5f; + if (!(half = hlsl_new_constant(ctx, type, &half_value, &expr->node.loc))) + return false; + list_add_before(&instr->entry, &half->node.entry);
if (!(sum = hlsl_new_binary_expr(ctx, HLSL_OP2_ADD, arg, &half->node))) @@ -2150,6 +2150,7 @@ static bool lower_round(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void * static bool lower_casts_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { struct hlsl_type *type = instr->data_type, *arg_type; + static const struct hlsl_constant_value zero_value; struct hlsl_ir_constant *zero; struct hlsl_ir_expr *expr;
@@ -2167,7 +2168,7 @@ static bool lower_casts_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr /* Narrowing casts should have already been lowered. */ assert(type->dimx == arg_type->dimx);
- zero = hlsl_new_constant(ctx, arg_type, &instr->loc); + zero = hlsl_new_constant(ctx, arg_type, &zero_value, &instr->loc); if (!zero) return false; list_add_before(&instr->entry, &zero->node.entry); @@ -2217,6 +2218,7 @@ static bool lower_int_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, { struct hlsl_ir_node *arg1, *arg2, *xor, *and, *abs1, *abs2, *div, *neg, *cast1, *cast2, *cast3, *cond; struct hlsl_type *type = instr->data_type, *utype; + struct hlsl_constant_value high_bit_value; struct hlsl_ir_constant *high_bit; struct hlsl_ir_expr *expr; unsigned int i; @@ -2238,10 +2240,10 @@ static bool lower_int_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, return false; list_add_before(&instr->entry, &xor->entry);
- if (!(high_bit = hlsl_new_constant(ctx, type, &instr->loc))) - return false; for (i = 0; i < type->dimx; ++i) - high_bit->value.u[i].u = 0x80000000; + high_bit_value.u[i].u = 0x80000000; + if (!(high_bit = hlsl_new_constant(ctx, type, &high_bit_value, &instr->loc))) + return false; list_add_before(&instr->entry, &high_bit->node.entry);
if (!(and = hlsl_new_binary_expr(ctx, HLSL_OP2_BIT_AND, xor, &high_bit->node))) @@ -2287,6 +2289,7 @@ static bool lower_int_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, { struct hlsl_ir_node *arg1, *arg2, *and, *abs1, *abs2, *div, *neg, *cast1, *cast2, *cast3, *cond; struct hlsl_type *type = instr->data_type, *utype; + struct hlsl_constant_value high_bit_value; struct hlsl_ir_constant *high_bit; struct hlsl_ir_expr *expr; unsigned int i; @@ -2304,10 +2307,10 @@ static bool lower_int_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, return false; utype = hlsl_get_numeric_type(ctx, type->class, HLSL_TYPE_UINT, type->dimx, type->dimy);
- if (!(high_bit = hlsl_new_constant(ctx, type, &instr->loc))) - return false; for (i = 0; i < type->dimx; ++i) - high_bit->value.u[i].u = 0x80000000; + high_bit_value.u[i].u = 0x80000000; + if (!(high_bit = hlsl_new_constant(ctx, type, &high_bit_value, &instr->loc))) + return false; list_add_before(&instr->entry, &high_bit->node.entry);
if (!(and = hlsl_new_binary_expr(ctx, HLSL_OP2_BIT_AND, arg1, &high_bit->node))) @@ -2382,6 +2385,7 @@ static bool lower_float_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr { struct hlsl_ir_node *arg1, *arg2, *mul1, *neg1, *ge, *neg2, *div, *mul2, *frc, *cond; struct hlsl_type *type = instr->data_type, *btype; + struct hlsl_constant_value one_value; struct hlsl_ir_constant *one; struct hlsl_ir_expr *expr; unsigned int i; @@ -2419,10 +2423,10 @@ static bool lower_float_modulus(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr if (!(cond = hlsl_add_conditional(ctx, &instr->entry, ge, arg2, neg2))) return false;
- if (!(one = hlsl_new_constant(ctx, type, &instr->loc))) - return false; for (i = 0; i < type->dimx; ++i) - one->value.u[i].f = 1.0f; + one_value.u[i].f = 1.0f; + if (!(one = hlsl_new_constant(ctx, type, &one_value, &instr->loc))) + return false; list_add_before(&instr->entry, &one->node.entry);
if (!(div = hlsl_new_binary_expr(ctx, HLSL_OP2_DIV, &one->node, cond))) diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 9c521215..a30ebffd 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -544,7 +544,8 @@ static bool fold_bit_or(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, c
bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { - struct hlsl_ir_constant *arg1, *arg2 = NULL, *res; + struct hlsl_ir_constant *arg1, *arg2 = NULL, *res_node; + struct hlsl_constant_value res = {0}; struct hlsl_ir_expr *expr; unsigned int i; bool success; @@ -571,61 +572,58 @@ bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, if (expr->operands[1].node) arg2 = hlsl_ir_constant(expr->operands[1].node);
- if (!(res = hlsl_new_constant(ctx, instr->data_type, &instr->loc))) - return false; - switch (expr->op) { case HLSL_OP1_ABS: - success = fold_abs(ctx, &res->value, instr->data_type, arg1); + success = fold_abs(ctx, &res, instr->data_type, arg1); break;
case HLSL_OP1_CAST: - success = fold_cast(ctx, &res->value, instr->data_type, arg1); + success = fold_cast(ctx, &res, instr->data_type, arg1); break;
case HLSL_OP1_NEG: - success = fold_neg(ctx, &res->value, instr->data_type, arg1); + success = fold_neg(ctx, &res, instr->data_type, arg1); break;
case HLSL_OP2_ADD: - success = fold_add(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_add(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_MUL: - success = fold_mul(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_mul(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_NEQUAL: - success = fold_nequal(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_nequal(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_DIV: - success = fold_div(ctx, &res->value, instr->data_type, arg1, arg2, &instr->loc); + success = fold_div(ctx, &res, instr->data_type, arg1, arg2, &instr->loc); break;
case HLSL_OP2_MOD: - success = fold_mod(ctx, &res->value, instr->data_type, arg1, arg2, &instr->loc); + success = fold_mod(ctx, &res, instr->data_type, arg1, arg2, &instr->loc); break;
case HLSL_OP2_MAX: - success = fold_max(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_max(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_MIN: - success = fold_min(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_min(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_BIT_XOR: - success = fold_bit_xor(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_bit_xor(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_BIT_AND: - success = fold_bit_and(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_bit_and(ctx, &res, instr->data_type, arg1, arg2); break;
case HLSL_OP2_BIT_OR: - success = fold_bit_or(ctx, &res->value, instr->data_type, arg1, arg2); + success = fold_bit_or(ctx, &res, instr->data_type, arg1, arg2); break;
default: @@ -636,19 +634,19 @@ bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
if (success) { - list_add_before(&expr->node.entry, &res->node.entry); - hlsl_replace_node(&expr->node, &res->node); - } - else - { - vkd3d_free(res); + if (!(res_node = hlsl_new_constant(ctx, instr->data_type, &res, &instr->loc))) + return false; + + list_add_before(&expr->node.entry, &res_node->node.entry); + hlsl_replace_node(&expr->node, &res_node->node); } return success; }
bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { - struct hlsl_ir_constant *value, *res; + struct hlsl_ir_constant *src, *dst; + struct hlsl_constant_value value; struct hlsl_ir_swizzle *swizzle; unsigned int i;
@@ -657,15 +655,15 @@ bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *inst swizzle = hlsl_ir_swizzle(instr); if (swizzle->val.node->type != HLSL_IR_CONSTANT) return false; - value = hlsl_ir_constant(swizzle->val.node); - - if (!(res = hlsl_new_constant(ctx, instr->data_type, &instr->loc))) - return false; + src = hlsl_ir_constant(swizzle->val.node);
for (i = 0; i < swizzle->node.data_type->dimx; ++i) - res->value.u[i] = value->value.u[hlsl_swizzle_get_component(swizzle->swizzle, i)]; + value.u[i] = src->value.u[hlsl_swizzle_get_component(swizzle->swizzle, i)]; + + if (!(dst = hlsl_new_constant(ctx, instr->data_type, &value, &instr->loc))) + return false;
- list_add_before(&swizzle->node.entry, &res->node.entry); - hlsl_replace_node(&swizzle->node, &res->node); + list_add_before(&swizzle->node.entry, &dst->node.entry); + hlsl_replace_node(&swizzle->node, &dst->node); return true; }