diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index db216b5d..d00b655b 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1581,7 +1581,7 @@ struct hlsl_ir_node *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const return hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), &value, loc); } -struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, +static 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; @@ -1590,6 +1590,12 @@ struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f, return hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_FLOAT), &value, loc); } +struct hlsl_ir_node *hlsl_block_add_float_constant(struct hlsl_ctx *ctx, struct hlsl_block *block, + float f, const struct vkd3d_shader_location *loc) +{ + return append_new_instr(ctx, block, hlsl_new_float_constant(ctx, f, loc)); +} + static struct hlsl_ir_node *hlsl_new_int_constant(struct hlsl_ctx *ctx, int32_t n, const struct vkd3d_shader_location *loc) { diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index d9030ebb..2b6abd34 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -1505,6 +1505,8 @@ struct hlsl_ir_node *hlsl_add_conditional(struct hlsl_ctx *ctx, struct hlsl_bloc void hlsl_add_function(struct hlsl_ctx *ctx, char *name, struct hlsl_ir_function_decl *decl); void hlsl_add_var(struct hlsl_ctx *ctx, struct hlsl_ir_var *decl); +struct hlsl_ir_node *hlsl_block_add_float_constant(struct hlsl_ctx *ctx, struct hlsl_block *block, + float f, const struct vkd3d_shader_location *loc); struct hlsl_ir_node *hlsl_block_add_int_constant(struct hlsl_ctx *ctx, struct hlsl_block *block, int32_t n, const struct vkd3d_shader_location *loc); struct hlsl_ir_node *hlsl_block_add_uint_constant(struct hlsl_ctx *ctx, struct hlsl_block *block, @@ -1583,8 +1585,6 @@ struct hlsl_ir_node *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *no 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], struct hlsl_type *data_type, const struct vkd3d_shader_location *loc); -struct hlsl_ir_node *hlsl_new_float_constant(struct hlsl_ctx *ctx, - float f, const struct vkd3d_shader_location *loc); struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, const struct hlsl_func_parameters *parameters, const struct hlsl_semantic *semantic, const struct vkd3d_shader_location *loc); diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index e125c7b9..7c969140 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3875,10 +3875,7 @@ static bool intrinsic_degrees(struct hlsl_ctx *ctx, return false; /* 1 rad = 180/pi degree = 57.2957795 degree */ - if (!(deg = hlsl_new_float_constant(ctx, 57.2957795f, loc))) - return false; - hlsl_block_add_instr(params->instrs, deg); - + deg = hlsl_block_add_float_constant(ctx, params->instrs, 57.2957795f, loc); return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, arg, deg, loc); } @@ -4062,9 +4059,7 @@ static bool intrinsic_exp(struct hlsl_ctx *ctx, return false; /* 1/ln(2) */ - if (!(coeff = hlsl_new_float_constant(ctx, 1.442695f, loc))) - return false; - hlsl_block_add_instr(params->instrs, coeff); + coeff = hlsl_block_add_float_constant(ctx, params->instrs, 1.442695f, loc); if (!(mul = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, coeff, arg, loc))) return false; @@ -4344,10 +4339,7 @@ static bool intrinsic_log(struct hlsl_ctx *ctx, return false; /* ln(2) */ - if (!(coeff = hlsl_new_float_constant(ctx, 0.69314718055f, loc))) - return false; - hlsl_block_add_instr(params->instrs, coeff); - + coeff = hlsl_block_add_float_constant(ctx, params->instrs, 0.69314718055f, loc); return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, log, coeff, loc); } @@ -4363,10 +4355,7 @@ static bool intrinsic_log10(struct hlsl_ctx *ctx, return false; /* 1 / log2(10) */ - if (!(coeff = hlsl_new_float_constant(ctx, 0.301029996f, loc))) - return false; - hlsl_block_add_instr(params->instrs, coeff); - + coeff = hlsl_block_add_float_constant(ctx, params->instrs, 0.301029996f, loc); return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, log, coeff, loc); } @@ -4594,10 +4583,7 @@ static bool intrinsic_radians(struct hlsl_ctx *ctx, return false; /* 1 degree = pi/180 rad = 0.0174532925f rad */ - if (!(rad = hlsl_new_float_constant(ctx, 0.0174532925f, loc))) - return false; - hlsl_block_add_instr(params->instrs, rad); - + rad = hlsl_block_add_float_constant(ctx, params->instrs, 0.0174532925f, loc); return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, arg, rad, loc); } @@ -5035,7 +5021,6 @@ static bool intrinsic_tex(struct hlsl_ctx *ctx, const struct parse_initializer * if (dim == HLSL_SAMPLER_DIM_1D) { struct hlsl_ir_load *load; - struct hlsl_ir_node *half; struct hlsl_ir_var *var; unsigned int idx = 0; @@ -5044,15 +5029,8 @@ static bool intrinsic_tex(struct hlsl_ctx *ctx, const struct parse_initializer * initialize_var_components(ctx, params->instrs, var, &idx, coords, false); if (hlsl_version_ge(ctx, 4, 0)) - { - if (!(half = hlsl_new_float_constant(ctx, 0.5f, loc))) - return false; - hlsl_block_add_instr(params->instrs, half); - - initialize_var_components(ctx, params->instrs, var, &idx, half, false); - } - else - initialize_var_components(ctx, params->instrs, var, &idx, coords, false); + coords = hlsl_block_add_float_constant(ctx, params->instrs, 0.5f, loc); + initialize_var_components(ctx, params->instrs, var, &idx, coords, false); if (!(load = hlsl_new_var_load(ctx, var, loc))) return false; @@ -5245,9 +5223,7 @@ static bool intrinsic_d3dcolor_to_ubyte4(struct hlsl_ctx *ctx, if (!(arg = intrinsic_float_convert_arg(ctx, params, arg, loc))) return false; - if (!(c = hlsl_new_float_constant(ctx, 255.0f + (0.5f / 256.0f), loc))) - return false; - hlsl_block_add_instr(params->instrs, c); + c = hlsl_block_add_float_constant(ctx, params->instrs, 255.0f + (0.5f / 256.0f), loc); if (arg_type->class == HLSL_CLASS_VECTOR) { @@ -9358,12 +9334,9 @@ func_arguments: primary_expr: C_FLOAT { - struct hlsl_ir_node *c; - - if (!(c = hlsl_new_float_constant(ctx, $1, &@1))) - YYABORT; - if (!($$ = make_block(ctx, c))) + if (!($$ = make_empty_block(ctx))) YYABORT; + hlsl_block_add_float_constant(ctx, $$, $1, &@1); } | C_INTEGER { diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 798ad862..6826efee 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -3435,7 +3435,7 @@ static bool lower_sqrt(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct /* Lower DP2 to MUL + ADD */ static bool lower_dot(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block) { - struct hlsl_ir_node *arg1, *arg2, *mul, *replacement, *zero, *add_x, *add_y; + struct hlsl_ir_node *arg1, *arg2, *mul, *replacement, *add_x, *add_y; struct hlsl_ir_expr *expr; if (instr->type != HLSL_IR_EXPR) @@ -3452,13 +3452,9 @@ static bool lower_dot(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct h { struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS] = { 0 }; - if (!(zero = hlsl_new_float_constant(ctx, 0.0f, &expr->node.loc))) - return false; - hlsl_block_add_instr(block, zero); - operands[0] = arg1; operands[1] = arg2; - operands[2] = zero; + operands[2] = hlsl_block_add_float_constant(ctx, block, 0.0f, &expr->node.loc); if (!(replacement = hlsl_new_expr(ctx, HLSL_OP3_DP2ADD, operands, instr->data_type, &expr->node.loc))) return false;