mirror of
https://gitlab.winehq.org/wine/vkd3d.git
synced 2025-09-12 18:50:22 -07:00
vkd3d-shader/hlsl: Ensure that TERNARY condition is always bool.
Also, properly casting it to float in lower_ternary() for SM1 avoids creating ABS and NEG on bool types.
This commit is contained in:
committed by
Alexandre Julliard
parent
9c0d04c862
commit
19fd43214b
Notes:
Alexandre Julliard
2024-04-09 15:44:37 -05: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/744
@@ -601,12 +601,10 @@ enum hlsl_ir_expr_op
|
||||
/* DP2ADD(a, b, c) computes the scalar product of a.xy and b.xy,
|
||||
* then adds c. */
|
||||
HLSL_OP3_DP2ADD,
|
||||
/* MOVC(a, b, c) returns c if a is bitwise zero and b otherwise.
|
||||
* TERNARY(a, b, c) returns c if a == 0 and b otherwise.
|
||||
* They differ for floating point numbers, because
|
||||
* -0.0 == 0.0, but it is not bitwise zero. CMP(a, b, c) returns b
|
||||
if a >= 0, and c otherwise. It's used only for SM1-SM3 targets, while
|
||||
SM4+ is using MOVC in such cases. */
|
||||
/* TERNARY(a, b, c) returns 'b' if 'a' is true and 'c' otherwise. 'a' must always be boolean.
|
||||
* MOVC(a, b, c) returns 'c' if 'a' is bitwise zero and 'b' otherwise.
|
||||
* CMP(a, b, c) returns 'b' if 'a' >= 0, and 'c' otherwise. It's used only for SM1-SM3 targets,
|
||||
while SM4+ is using MOVC in such cases. */
|
||||
HLSL_OP3_CMP,
|
||||
HLSL_OP3_MOVC,
|
||||
HLSL_OP3_TERNARY,
|
||||
|
@@ -4405,26 +4405,34 @@ static bool add_ternary(struct hlsl_ctx *ctx, struct hlsl_block *block,
|
||||
if (!(cond = add_implicit_conversion(ctx, block, cond, cond_type, &cond->loc)))
|
||||
return false;
|
||||
}
|
||||
else if (common_type->dimx == 1 && common_type->dimy == 1)
|
||||
else
|
||||
{
|
||||
common_type = hlsl_get_numeric_type(ctx, cond_type->class,
|
||||
common_type->base_type, cond_type->dimx, cond_type->dimy);
|
||||
}
|
||||
else if (cond_type->dimx != common_type->dimx || cond_type->dimy != common_type->dimy)
|
||||
{
|
||||
/* This condition looks wrong but is correct.
|
||||
* floatN is compatible with float1xN, but not with floatNx1. */
|
||||
cond_type = hlsl_get_numeric_type(ctx, cond_type->class, HLSL_TYPE_BOOL,
|
||||
cond_type->dimx, cond_type->dimy);
|
||||
if (!(cond = add_implicit_conversion(ctx, block, cond, cond_type, &cond->loc)))
|
||||
return false;
|
||||
|
||||
struct vkd3d_string_buffer *cond_string, *value_string;
|
||||
if (common_type->dimx == 1 && common_type->dimy == 1)
|
||||
{
|
||||
common_type = hlsl_get_numeric_type(ctx, cond_type->class,
|
||||
common_type->base_type, cond_type->dimx, cond_type->dimy);
|
||||
}
|
||||
else if (cond_type->dimx != common_type->dimx || cond_type->dimy != common_type->dimy)
|
||||
{
|
||||
/* This condition looks wrong but is correct.
|
||||
* floatN is compatible with float1xN, but not with floatNx1. */
|
||||
|
||||
cond_string = hlsl_type_to_string(ctx, cond_type);
|
||||
value_string = hlsl_type_to_string(ctx, common_type);
|
||||
if (cond_string && value_string)
|
||||
hlsl_error(ctx, &first->loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE,
|
||||
"Ternary condition type '%s' is not compatible with value type '%s'.",
|
||||
cond_string->buffer, value_string->buffer);
|
||||
hlsl_release_string_buffer(ctx, cond_string);
|
||||
hlsl_release_string_buffer(ctx, value_string);
|
||||
struct vkd3d_string_buffer *cond_string, *value_string;
|
||||
|
||||
cond_string = hlsl_type_to_string(ctx, cond_type);
|
||||
value_string = hlsl_type_to_string(ctx, common_type);
|
||||
if (cond_string && value_string)
|
||||
hlsl_error(ctx, &first->loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE,
|
||||
"Ternary condition type '%s' is not compatible with value type '%s'.",
|
||||
cond_string->buffer, value_string->buffer);
|
||||
hlsl_release_string_buffer(ctx, cond_string);
|
||||
hlsl_release_string_buffer(ctx, value_string);
|
||||
}
|
||||
}
|
||||
|
||||
if (!(first = add_implicit_conversion(ctx, block, first, common_type, &first->loc)))
|
||||
@@ -4449,9 +4457,16 @@ static bool add_ternary(struct hlsl_ctx *ctx, struct hlsl_block *block,
|
||||
hlsl_release_string_buffer(ctx, second_string);
|
||||
}
|
||||
|
||||
cond_type = hlsl_get_numeric_type(ctx, cond_type->class, HLSL_TYPE_BOOL,
|
||||
cond_type->dimx, cond_type->dimy);
|
||||
if (!(cond = add_implicit_conversion(ctx, block, cond, cond_type, &cond->loc)))
|
||||
return false;
|
||||
|
||||
common_type = first->data_type;
|
||||
}
|
||||
|
||||
assert(cond->data_type->base_type == HLSL_TYPE_BOOL);
|
||||
|
||||
args[0] = cond;
|
||||
args[1] = first;
|
||||
args[2] = second;
|
||||
|
@@ -2958,8 +2958,7 @@ static bool lower_logic_not(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, st
|
||||
static bool lower_ternary(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, struct hlsl_block *block)
|
||||
{
|
||||
struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS] = { 0 }, *replacement;
|
||||
struct hlsl_ir_node *zero, *cond, *first, *second;
|
||||
struct hlsl_constant_value zero_value = { 0 };
|
||||
struct hlsl_ir_node *cond, *first, *second, *float_cond, *neg;
|
||||
struct hlsl_ir_expr *expr;
|
||||
struct hlsl_type *type;
|
||||
|
||||
@@ -2980,18 +2979,22 @@ static bool lower_ternary(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, stru
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(cond->data_type->base_type == HLSL_TYPE_BOOL);
|
||||
|
||||
if (ctx->profile->major_version < 4)
|
||||
{
|
||||
struct hlsl_ir_node *abs, *neg;
|
||||
type = hlsl_get_numeric_type(ctx, instr->data_type->class, HLSL_TYPE_FLOAT,
|
||||
instr->data_type->dimx, instr->data_type->dimy);
|
||||
|
||||
if (!(abs = hlsl_new_unary_expr(ctx, HLSL_OP1_ABS, cond, &instr->loc)))
|
||||
if (!(float_cond = hlsl_new_cast(ctx, cond, type, &instr->loc)))
|
||||
return false;
|
||||
hlsl_block_add_instr(block, abs);
|
||||
hlsl_block_add_instr(block, float_cond);
|
||||
|
||||
if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, abs, &instr->loc)))
|
||||
if (!(neg = hlsl_new_unary_expr(ctx, HLSL_OP1_NEG, float_cond, &instr->loc)))
|
||||
return false;
|
||||
hlsl_block_add_instr(block, neg);
|
||||
|
||||
memset(operands, 0, sizeof(operands));
|
||||
operands[0] = neg;
|
||||
operands[1] = second;
|
||||
operands[2] = first;
|
||||
@@ -3000,21 +3003,6 @@ static bool lower_ternary(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, stru
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cond->data_type->base_type == HLSL_TYPE_FLOAT)
|
||||
{
|
||||
if (!(zero = hlsl_new_constant(ctx, cond->data_type, &zero_value, &instr->loc)))
|
||||
return false;
|
||||
hlsl_block_add_instr(block, zero);
|
||||
|
||||
operands[0] = zero;
|
||||
operands[1] = cond;
|
||||
type = cond->data_type;
|
||||
type = hlsl_get_numeric_type(ctx, type->class, HLSL_TYPE_BOOL, type->dimx, type->dimy);
|
||||
if (!(cond = hlsl_new_expr(ctx, HLSL_OP2_NEQUAL, operands, type, &instr->loc)))
|
||||
return false;
|
||||
hlsl_block_add_instr(block, cond);
|
||||
}
|
||||
|
||||
memset(operands, 0, sizeof(operands));
|
||||
operands[0] = cond;
|
||||
operands[1] = first;
|
||||
@@ -3319,11 +3307,21 @@ static bool lower_casts_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr
|
||||
struct hlsl_ir_node *hlsl_add_conditional(struct hlsl_ctx *ctx, struct hlsl_block *instrs,
|
||||
struct hlsl_ir_node *condition, struct hlsl_ir_node *if_true, struct hlsl_ir_node *if_false)
|
||||
{
|
||||
struct hlsl_type *cond_type = condition->data_type;
|
||||
struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS];
|
||||
struct hlsl_ir_node *cond;
|
||||
|
||||
assert(hlsl_types_are_equal(if_true->data_type, if_false->data_type));
|
||||
|
||||
if (cond_type->base_type != HLSL_TYPE_BOOL)
|
||||
{
|
||||
cond_type = hlsl_get_numeric_type(ctx, cond_type->class, HLSL_TYPE_BOOL, cond_type->dimx, cond_type->dimy);
|
||||
|
||||
if (!(condition = hlsl_new_cast(ctx, condition, cond_type, &condition->loc)))
|
||||
return NULL;
|
||||
hlsl_block_add_instr(instrs, condition);
|
||||
}
|
||||
|
||||
operands[0] = condition;
|
||||
operands[1] = if_true;
|
||||
operands[2] = if_false;
|
||||
@@ -5400,11 +5398,11 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry
|
||||
hlsl_transform_ir(ctx, split_matrix_copies, body, NULL);
|
||||
|
||||
lower_ir(ctx, lower_narrowing_casts, body);
|
||||
lower_ir(ctx, lower_casts_to_bool, body);
|
||||
lower_ir(ctx, lower_int_dot, body);
|
||||
lower_ir(ctx, lower_int_division, body);
|
||||
lower_ir(ctx, lower_int_modulus, body);
|
||||
lower_ir(ctx, lower_int_abs, body);
|
||||
lower_ir(ctx, lower_casts_to_bool, body);
|
||||
lower_ir(ctx, lower_float_modulus, body);
|
||||
hlsl_transform_ir(ctx, fold_redundant_casts, body, NULL);
|
||||
do
|
||||
|
@@ -1177,30 +1177,11 @@ static bool fold_ternary(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
||||
|
||||
assert(dst_type->base_type == src2->node.data_type->base_type);
|
||||
assert(dst_type->base_type == src3->node.data_type->base_type);
|
||||
assert(src1->node.data_type->base_type == HLSL_TYPE_BOOL);
|
||||
|
||||
for (k = 0; k < dst_type->dimx; ++k)
|
||||
{
|
||||
switch (src1->node.data_type->base_type)
|
||||
{
|
||||
case HLSL_TYPE_FLOAT:
|
||||
case HLSL_TYPE_HALF:
|
||||
dst->u[k] = src1->value.u[k].f != 0.0f ? src2->value.u[k] : src3->value.u[k];
|
||||
break;
|
||||
dst->u[k] = src1->value.u[k].u ? src2->value.u[k] : src3->value.u[k];
|
||||
|
||||
case HLSL_TYPE_DOUBLE:
|
||||
dst->u[k] = src1->value.u[k].d != 0.0 ? src2->value.u[k] : src3->value.u[k];
|
||||
break;
|
||||
|
||||
case HLSL_TYPE_INT:
|
||||
case HLSL_TYPE_UINT:
|
||||
case HLSL_TYPE_BOOL:
|
||||
dst->u[k] = src1->value.u[k].u ? src2->value.u[k] : src3->value.u[k];
|
||||
break;
|
||||
|
||||
default:
|
||||
vkd3d_unreachable();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user