Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- v2: * New --- libs/vkd3d-shader/hlsl_constant_ops.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 51cee179..109fc2ee 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -194,11 +194,12 @@ static bool fold_mul(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct hlsl_ir_constant *src1, struct hlsl_ir_constant *src2) { enum hlsl_base_type type = dst->node.data_type->base_type; + unsigned int k;
assert(type == src1->node.data_type->base_type); assert(type == src2->node.data_type->base_type);
- for (int k = 0; k < 4; ++k) + for (k = 0; k < 4; ++k) { switch (type) {
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- v2: * New --- libs/vkd3d-shader/hlsl_constant_ops.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 109fc2ee..e4e60310 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -236,10 +236,17 @@ bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void return false; expr = hlsl_ir_expr(instr);
+ if (instr->data_type->type > HLSL_CLASS_VECTOR) + return false; + for (i = 0; i < ARRAY_SIZE(expr->operands); ++i) { - if (expr->operands[i].node && expr->operands[i].node->type != HLSL_IR_CONSTANT) - return false; + if (expr->operands[i].node) + { + if (expr->operands[i].node->type != HLSL_IR_CONSTANT) + return false; + assert(expr->operands[i].node->data_type->type <= HLSL_CLASS_VECTOR); + } } arg1 = hlsl_ir_constant(expr->operands[0].node); if (expr->operands[1].node)
On 3/28/22 03:59, Giovanni Mascellani wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- New
libs/vkd3d-shader/hlsl_constant_ops.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
We currently can't construct HLSL_IR_CONSTANT nodes that aren't vectors anyway, though (as evidenced by patch 6/9). Is there a real situation, now or in the future, where we might need this?
Hi,
Il 28/03/22 20:35, Zebediah Figura ha scritto:
We currently can't construct HLSL_IR_CONSTANT nodes that aren't vectors anyway, though (as evidenced by patch 6/9). Is there a real situation, now or in the future, where we might need this?
WRT the check on the arguments, as you say no HLSL_IR_CONSTANT object should ever be of type > VECTOR, which is why the check is an assert(). Any assert() is strictly speaking not necessary, but they are useful for making the code easier to read.
As for the check on the result type, let us consider the expression "(float1x4)float4(1.0, 2.0, 3.0, 4.0)". Unless I am missing something (and supposing that the compiler was able to see "float4(1.0, 2.0, 3.0, 4.0)" as a constant), hlsl_fold_constants() would see that it is an expression with constant operands, and would try to create an HLSL_IR_CONSTANT to accommodate the result, without realizing that it is a matrix type. This would introduce an inconsistency. So I think it is appropriate to have one additional check.
I guess in the long run this problem would go away because eventually all matrix types will have been lowered before we go through constant folding, but we're not still there.
Giovanni.
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 29, 2022 6:48 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Hi,
Il 28/03/22 20:35, Zebediah Figura ha scritto:
We currently can't construct HLSL_IR_CONSTANT nodes that aren't vectors > anyway, though (as evidenced by patch 6/9). Is there a real situation, > now or in the future, where we might need this?
WRT the check on the arguments, as you say no HLSL_IR_CONSTANT object should ever be of type > VECTOR, which is why the check is an assert(). Any assert() is strictly speaking not necessary, but they are useful for making the code easier to read.
As for the check on the result type, let us consider the expression "(float1x4)float4(1.0, 2.0, 3.0, 4.0)". Unless I am missing something (and supposing that the compiler was able to see "float4(1.0, 2.0, 3.0, 4.0)" as a constant), hlsl_fold_constants() would see that it is an expression with constant operands, and would try to create an HLSL_IR_CONSTANT to accommodate the result, without realizing that it is a matrix type. This would introduce an inconsistency. So I think it is appropriate to have one additional check.
I guess in the long run this problem would go away because eventually all matrix types will have been lowered before we go through constant folding, but we're not still there.
Giovanni.
On 3/29/22 04:47, Giovanni Mascellani wrote:
Hi,
Il 28/03/22 20:35, Zebediah Figura ha scritto:
We currently can't construct HLSL_IR_CONSTANT nodes that aren't vectors anyway, though (as evidenced by patch 6/9). Is there a real situation, now or in the future, where we might need this?
WRT the check on the arguments, as you say no HLSL_IR_CONSTANT object should ever be of type > VECTOR, which is why the check is an assert(). Any assert() is strictly speaking not necessary, but they are useful for making the code easier to read.
As for the check on the result type, let us consider the expression "(float1x4)float4(1.0, 2.0, 3.0, 4.0)". Unless I am missing something (and supposing that the compiler was able to see "float4(1.0, 2.0, 3.0, 4.0)" as a constant), hlsl_fold_constants() would see that it is an expression with constant operands, and would try to create an HLSL_IR_CONSTANT to accommodate the result, without realizing that it is a matrix type. This would introduce an inconsistency. So I think it is appropriate to have one additional check.
Right, that makes sense.
I guess in the long run this problem would go away because eventually all matrix types will have been lowered before we go through constant folding, but we're not still there.
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- v2: * Renamed (it was "Abort when an unexpected type appears.") --- libs/vkd3d-shader/hlsl_sm4.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index fa845dc4..af5c777f 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1468,7 +1468,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default: - break; + assert(0); } break;
@@ -1494,7 +1494,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default: - break; + assert(0); } break;
@@ -1520,7 +1520,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default: - break; + assert(0); } break;
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- Renamed (it was "Abort when an unexpected type appears.")
libs/vkd3d-shader/hlsl_sm4.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index fa845dc4..af5c777f 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1468,7 +1468,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default:
- break;
- assert(0);
} break;
@@ -1494,7 +1494,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default:
- break;
- assert(0);
} break;
@@ -1520,7 +1520,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default:
- break;
- assert(0);
} break;
-- 2.35.1
On Mon, Mar 28, 2022 at 10:59 AM Giovanni Mascellani gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- Renamed (it was "Abort when an unexpected type appears.")
libs/vkd3d-shader/hlsl_sm4.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index fa845dc4..af5c777f 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1468,7 +1468,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default:
break;
assert(0); } break;
One option to make these asserts more descriptive is to do something like:
assert(!"Non-numeric source expression");
On 4/5/22 05:47, Matteo Bruni wrote:
On Mon, Mar 28, 2022 at 10:59 AM Giovanni Mascellani gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- Renamed (it was "Abort when an unexpected type appears.")
libs/vkd3d-shader/hlsl_sm4.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index fa845dc4..af5c777f 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1468,7 +1468,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
default:
break;
assert(0); } break;
One option to make these asserts more descriptive is to do something like:
assert(!"Non-numeric source expression");
Perhaps, although in a case like this I don't think it's worthwhile; this assert is more for code clarity than actual runtime validation.
Hi,
Il 05/04/22 12:47, Matteo Bruni ha scritto:
One option to make these asserts more descriptive is to do something like:
assert(!"Non-numeric source expression");
What I'd really use for this kind of "control should never get here" construct is rather a helper marked as noreturn, so you don't have to add some awkward "return 0" or "break" just below to silence "non-void function does not return" warnings or similar. But I've never decided it was worthy enough of my time to actually write such a patch.
I am not opposed to a message either, but usually a line number is enough to understand what is the problem.
Giovanni.
On Wed, Apr 6, 2022 at 2:06 PM Giovanni Mascellani gmascellani@codeweavers.com wrote:
Hi,
Il 05/04/22 12:47, Matteo Bruni ha scritto:
One option to make these asserts more descriptive is to do something like:
assert(!"Non-numeric source expression");
What I'd really use for this kind of "control should never get here" construct is rather a helper marked as noreturn, so you don't have to add some awkward "return 0" or "break" just below to silence "non-void function does not return" warnings or similar. But I've never decided it was worthy enough of my time to actually write such a patch.
Mesa has unreachable() for this kind of thing, we could probably pick it up in vkd3d.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com -- v2: * New --- libs/vkd3d-shader/hlsl_constant_ops.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index e4e60310..92f5a856 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -83,8 +83,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
default: - FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, src->node.data_type), - debug_hlsl_type(ctx, dst->node.data_type)); + assert(0); return false; }
@@ -112,8 +111,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
default: - FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, src->node.data_type), - debug_hlsl_type(ctx, dst->node.data_type)); + assert(0); return false; } }
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- New
libs/vkd3d-shader/hlsl_constant_ops.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index e4e60310..92f5a856 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -83,8 +83,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
default:
- FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, src->node.data_type),
- debug_hlsl_type(ctx, dst->node.data_type));
- assert(0);
return false; }
@@ -112,8 +111,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
default:
- FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, src->node.data_type),
- debug_hlsl_type(ctx, dst->node.data_type));
- assert(0);
return false; } } -- 2.35.1
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- libs/vkd3d-shader/hlsl.c | 8 ++++---- libs/vkd3d-shader/hlsl.h | 4 ++-- libs/vkd3d-shader/hlsl.y | 8 ++++---- libs/vkd3d-shader/hlsl_codegen.c | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index d88b0b6d..36cc3a91 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -564,25 +564,25 @@ struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ctx *ctx, struct hlsl_ir }
struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, - const struct vkd3d_shader_location loc) + const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c;
if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL; - init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), loc); + init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), *loc); c->value[0].i = n; return c; }
struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n, - const struct vkd3d_shader_location loc) + const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c;
if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL; - init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc); + init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), *loc); c->value[0].u = n; return c; } diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 0543e144..c9abbd4a 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -738,7 +738,7 @@ struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hl struct list *parameters, const struct hlsl_semantic *semantic, struct vkd3d_shader_location loc); struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct vkd3d_shader_location loc); struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, - const struct vkd3d_shader_location loc); + const struct vkd3d_shader_location *loc); struct hlsl_ir_jump *hlsl_new_jump(struct hlsl_ctx *ctx, enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc); struct hlsl_ir_load *hlsl_new_load(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct hlsl_ir_node *offset, struct hlsl_type *type, struct vkd3d_shader_location loc); @@ -757,7 +757,7 @@ struct hlsl_ir_var *hlsl_new_synthetic_var(struct hlsl_ctx *ctx, const char *nam const struct vkd3d_shader_location loc); struct hlsl_type *hlsl_new_texture_type(struct hlsl_ctx *ctx, enum hlsl_sampler_dim dim, struct hlsl_type *format); struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n, - const struct vkd3d_shader_location loc); + const struct vkd3d_shader_location *loc); struct hlsl_ir_node *hlsl_new_unary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg, struct vkd3d_shader_location loc); struct hlsl_ir_var *hlsl_new_var(struct hlsl_ctx *ctx, const char *name, struct hlsl_type *type, diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 1ab56fba..41fa7df4 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -573,7 +573,7 @@ static struct hlsl_ir_load *add_record_load(struct hlsl_ctx *ctx, struct list *i { struct hlsl_ir_constant *c;
- if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, loc))) + if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, &loc))) return NULL; list_add_tail(instrs, &c->node.entry);
@@ -607,7 +607,7 @@ static struct hlsl_ir_load *add_array_load(struct hlsl_ctx *ctx, struct list *in return NULL; }
- if (!(c = hlsl_new_uint_constant(ctx, hlsl_type_get_array_element_reg_size(data_type), loc))) + if (!(c = hlsl_new_uint_constant(ctx, hlsl_type_get_array_element_reg_size(data_type), &loc))) return NULL; list_add_tail(instrs, &c->node.entry); if (!(mul = hlsl_new_binary_expr(ctx, HLSL_OP2_MUL, index, &c->node))) @@ -1399,7 +1399,7 @@ static bool add_increment(struct hlsl_ctx *ctx, struct list *instrs, bool decrem hlsl_error(ctx, &loc, VKD3D_SHADER_ERROR_HLSL_MODIFIES_CONST, "Argument to %s%screment operator is const.", post ? "post" : "pre", decrement ? "de" : "in");
- if (!(one = hlsl_new_int_constant(ctx, 1, loc))) + if (!(one = hlsl_new_int_constant(ctx, 1, &loc))) return false; list_add_tail(instrs, &one->node.entry);
@@ -1452,7 +1452,7 @@ static void struct_var_initializer(struct hlsl_ctx *ctx, struct list *list, stru
if (hlsl_type_component_count(field->type) == hlsl_type_component_count(node->data_type)) { - if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, node->loc))) + if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, &node->loc))) break; list_add_tail(list, &c->node.entry);
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 98be3aeb..3d0f9e41 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -92,7 +92,7 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct list *instrs, struct return; list_add_head(instrs, &load->node.entry);
- if (!(offset = hlsl_new_uint_constant(ctx, field_offset, var->loc))) + if (!(offset = hlsl_new_uint_constant(ctx, field_offset, &var->loc))) return; list_add_after(&load->node.entry, &offset->node.entry);
@@ -159,7 +159,7 @@ static void append_output_copy(struct hlsl_ctx *ctx, struct list *instrs, struct list_add_before(&var->scope_entry, &output->scope_entry); list_add_tail(&ctx->extern_vars, &output->extern_entry);
- if (!(offset = hlsl_new_uint_constant(ctx, field_offset, var->loc))) + if (!(offset = hlsl_new_uint_constant(ctx, field_offset, &var->loc))) return; list_add_tail(instrs, &offset->node.entry);
@@ -572,7 +572,7 @@ static bool split_copy(struct hlsl_ctx *ctx, struct hlsl_ir_store *store, struct hlsl_ir_load *split_load; struct hlsl_ir_constant *c;
- if (!(c = hlsl_new_uint_constant(ctx, offset, store->node.loc))) + if (!(c = hlsl_new_uint_constant(ctx, offset, &store->node.loc))) return false; list_add_before(&store->node.entry, &c->node.entry);
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
libs/vkd3d-shader/hlsl.c | 8 ++++---- libs/vkd3d-shader/hlsl.h | 4 ++-- libs/vkd3d-shader/hlsl.y | 8 ++++---- libs/vkd3d-shader/hlsl_codegen.c | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index d88b0b6d..36cc3a91 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -564,25 +564,25 @@ struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ctx *ctx, struct hlsl_ir }
struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n,
- const struct vkd3d_shader_location loc)
- const struct vkd3d_shader_location *loc)
{ struct hlsl_ir_constant *c;
if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL;
- init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), loc);
- init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), *loc);
c->value[0].i = n; return c; }
struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n,
- const struct vkd3d_shader_location loc)
- const struct vkd3d_shader_location *loc)
{ struct hlsl_ir_constant *c;
if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL;
- init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc);
- init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), *loc);
c->value[0].u = n; return c; } diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 0543e144..c9abbd4a 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -738,7 +738,7 @@ struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hl struct list *parameters, const struct hlsl_semantic *semantic, struct vkd3d_shader_location loc); struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct vkd3d_shader_location loc); struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n,
- const struct vkd3d_shader_location loc);
- const struct vkd3d_shader_location *loc);
struct hlsl_ir_jump *hlsl_new_jump(struct hlsl_ctx *ctx, enum hlsl_ir_jump_type type, struct vkd3d_shader_location loc); struct hlsl_ir_load *hlsl_new_load(struct hlsl_ctx *ctx, struct hlsl_ir_var *var, struct hlsl_ir_node *offset, struct hlsl_type *type, struct vkd3d_shader_location loc); @@ -757,7 +757,7 @@ struct hlsl_ir_var *hlsl_new_synthetic_var(struct hlsl_ctx *ctx, const char *nam const struct vkd3d_shader_location loc); struct hlsl_type *hlsl_new_texture_type(struct hlsl_ctx *ctx, enum hlsl_sampler_dim dim, struct hlsl_type *format); struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned int n,
- const struct vkd3d_shader_location loc);
- const struct vkd3d_shader_location *loc);
struct hlsl_ir_node *hlsl_new_unary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg, struct vkd3d_shader_location loc); struct hlsl_ir_var *hlsl_new_var(struct hlsl_ctx *ctx, const char *name, struct hlsl_type *type, diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 1ab56fba..41fa7df4 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -573,7 +573,7 @@ static struct hlsl_ir_load *add_record_load(struct hlsl_ctx *ctx, struct list *i { struct hlsl_ir_constant *c;
- if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, loc)))
- if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, &loc)))
return NULL; list_add_tail(instrs, &c->node.entry);
@@ -607,7 +607,7 @@ static struct hlsl_ir_load *add_array_load(struct hlsl_ctx *ctx, struct list *in return NULL; }
- if (!(c = hlsl_new_uint_constant(ctx, hlsl_type_get_array_element_reg_size(data_type), loc)))
- if (!(c = hlsl_new_uint_constant(ctx, hlsl_type_get_array_element_reg_size(data_type), &loc)))
return NULL; list_add_tail(instrs, &c->node.entry); if (!(mul = hlsl_new_binary_expr(ctx, HLSL_OP2_MUL, index, &c->node))) @@ -1399,7 +1399,7 @@ static bool add_increment(struct hlsl_ctx *ctx, struct list *instrs, bool decrem hlsl_error(ctx, &loc, VKD3D_SHADER_ERROR_HLSL_MODIFIES_CONST, "Argument to %s%screment operator is const.", post ? "post" : "pre", decrement ? "de" : "in");
- if (!(one = hlsl_new_int_constant(ctx, 1, loc)))
- if (!(one = hlsl_new_int_constant(ctx, 1, &loc)))
return false; list_add_tail(instrs, &one->node.entry);
@@ -1452,7 +1452,7 @@ static void struct_var_initializer(struct hlsl_ctx *ctx, struct list *list, stru
if (hlsl_type_component_count(field->type) == hlsl_type_component_count(node->data_type)) {
- if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, node->loc)))
- if (!(c = hlsl_new_uint_constant(ctx, field->reg_offset, &node->loc)))
break; list_add_tail(list, &c->node.entry);
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 98be3aeb..3d0f9e41 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -92,7 +92,7 @@ static void prepend_input_copy(struct hlsl_ctx *ctx, struct list *instrs, struct return; list_add_head(instrs, &load->node.entry);
- if (!(offset = hlsl_new_uint_constant(ctx, field_offset, var->loc)))
- if (!(offset = hlsl_new_uint_constant(ctx, field_offset, &var->loc)))
return; list_add_after(&load->node.entry, &offset->node.entry);
@@ -159,7 +159,7 @@ static void append_output_copy(struct hlsl_ctx *ctx, struct list *instrs, struct list_add_before(&var->scope_entry, &output->scope_entry); list_add_tail(&ctx->extern_vars, &output->extern_entry);
- if (!(offset = hlsl_new_uint_constant(ctx, field_offset, var->loc)))
- if (!(offset = hlsl_new_uint_constant(ctx, field_offset, &var->loc)))
return; list_add_tail(instrs, &offset->node.entry);
@@ -572,7 +572,7 @@ static bool split_copy(struct hlsl_ctx *ctx, struct hlsl_ir_store *store, struct hlsl_ir_load *split_load; struct hlsl_ir_constant *c;
- if (!(c = hlsl_new_uint_constant(ctx, offset, store->node.loc)))
- if (!(c = hlsl_new_uint_constant(ctx, offset, &store->node.loc)))
return false; list_add_before(&store->node.entry, &c->node.entry);
-- 2.35.1
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- v2: * Assert type class --- libs/vkd3d-shader/hlsl.c | 31 ++++++++++++++++++++++++------- libs/vkd3d-shader/hlsl.h | 2 ++ 2 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 36cc3a91..664f7813 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -563,15 +563,31 @@ struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ctx *ctx, struct hlsl_ir return hlsl_new_store(ctx, lhs, NULL, rhs, 0, rhs->loc); }
-struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, +struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *type, const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c;
+ assert(type->type <= HLSL_CLASS_VECTOR); + if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL; - init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), *loc); - c->value[0].i = n; + + init_node(&c->node, HLSL_IR_CONSTANT, type, *loc); + + return c; +} + +struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, + const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_constant *c; + + c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), loc); + + if (c) + c->value[0].i = n; + return c; }
@@ -580,10 +596,11 @@ struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned i { struct hlsl_ir_constant *c;
- if (!(c = hlsl_alloc(ctx, sizeof(*c)))) - return NULL; - init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), *loc); - c->value[0].u = n; + c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc); + + if (c) + c->value[0].u = n; + return c; }
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index c9abbd4a..e4db386f 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -733,6 +733,8 @@ struct hlsl_buffer *hlsl_new_buffer(struct hlsl_ctx *ctx, enum hlsl_buffer_type const struct hlsl_reg_reservation *reservation, struct vkd3d_shader_location loc); struct hlsl_ir_expr *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); struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node); struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, struct list *parameters, const struct hlsl_semantic *semantic, struct vkd3d_shader_location loc);
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- Assert type class
libs/vkd3d-shader/hlsl.c | 31 ++++++++++++++++++++++++------- libs/vkd3d-shader/hlsl.h | 2 ++ 2 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 36cc3a91..664f7813 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -563,15 +563,31 @@ struct hlsl_ir_store *hlsl_new_simple_store(struct hlsl_ctx *ctx, struct hlsl_ir return hlsl_new_store(ctx, lhs, NULL, rhs, 0, rhs->loc); }
-struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n, +struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *type, const struct vkd3d_shader_location *loc) { struct hlsl_ir_constant *c;
- assert(type->type <= HLSL_CLASS_VECTOR);
if (!(c = hlsl_alloc(ctx, sizeof(*c)))) return NULL;
- init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), *loc);
- c->value[0].i = n;
- init_node(&c->node, HLSL_IR_CONSTANT, type, *loc);
- return c;
+}
+struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n,
- const struct vkd3d_shader_location *loc)
+{
- struct hlsl_ir_constant *c;
- c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), loc);
- if (c)
- c->value[0].i = n;
return c; }
@@ -580,10 +596,11 @@ struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned i { struct hlsl_ir_constant *c;
- if (!(c = hlsl_alloc(ctx, sizeof(*c))))
- return NULL;
- init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), *loc);
- c->value[0].u = n;
- c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc);
- if (c)
- c->value[0].u = n;
return c; }
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index c9abbd4a..e4db386f 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -733,6 +733,8 @@ struct hlsl_buffer *hlsl_new_buffer(struct hlsl_ctx *ctx, enum hlsl_buffer_type const struct hlsl_reg_reservation *reservation, struct vkd3d_shader_location loc); struct hlsl_ir_expr *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);
struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node); struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type, struct list *parameters, const struct hlsl_semantic *semantic, struct vkd3d_shader_location loc); -- 2.35.1
With this change it is possible to store booleans as 0xffffffff, similarly as what happens at runtime.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- v2: * New --- libs/vkd3d-shader/hlsl.c | 2 +- libs/vkd3d-shader/hlsl.h | 1 - libs/vkd3d-shader/hlsl.y | 4 ++-- libs/vkd3d-shader/hlsl_codegen.c | 2 +- libs/vkd3d-shader/hlsl_constant_ops.c | 20 ++++++++++---------- 5 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 664f7813..eabe189f 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1168,7 +1168,7 @@ static void dump_ir_constant(struct vkd3d_string_buffer *buffer, const struct hl switch (type->base_type) { case HLSL_TYPE_BOOL: - vkd3d_string_buffer_printf(buffer, "%s ", value->b ? "true" : "false"); + vkd3d_string_buffer_printf(buffer, "%s ", value->u ? "true" : "false"); break;
case HLSL_TYPE_DOUBLE: diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index e4db386f..6b00d117 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -415,7 +415,6 @@ struct hlsl_ir_constant int32_t i; float f; double d; - bool b; } value[4]; struct hlsl_reg reg; }; diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 41fa7df4..481347ee 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -842,7 +842,7 @@ static unsigned int evaluate_array_dimension(struct hlsl_ir_node *node) case HLSL_TYPE_DOUBLE: return value->d; case HLSL_TYPE_BOOL: - return value->b; + return !!value->u; default: assert(0); return 0; @@ -3394,7 +3394,7 @@ primary_expr: if (!(c = hlsl_alloc(ctx, sizeof(*c)))) YYABORT; init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), @1); - c->value[0].b = $1; + c->value[0].u = $1 ? ~0u : 0; if (!($$ = make_list(ctx, &c->node))) YYABORT; } diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 3d0f9e41..c1f5560e 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1200,7 +1200,7 @@ static void allocate_const_registers_recurse(struct hlsl_ctx *ctx, struct hlsl_b switch (type->base_type) { case HLSL_TYPE_BOOL: - f = value->b; + f = !!value->u; break;
case HLSL_TYPE_FLOAT: diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 92f5a856..87b5bc90 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -47,7 +47,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].f; f = src->value[k].f; d = src->value[k].f; - b = src->value[k].f; + b = !!src->value[k].f; break;
case HLSL_TYPE_DOUBLE: @@ -55,7 +55,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].d; f = src->value[k].d; d = src->value[k].d; - b = src->value[k].d; + b = !!src->value[k].d; break;
case HLSL_TYPE_INT: @@ -63,7 +63,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].i; f = src->value[k].i; d = src->value[k].i; - b = src->value[k].i; + b = !!src->value[k].i; break;
case HLSL_TYPE_UINT: @@ -71,15 +71,15 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].u; f = src->value[k].u; d = src->value[k].u; - b = src->value[k].u; + b = !!src->value[k].u; break;
case HLSL_TYPE_BOOL: - u = src->value[k].b; - i = src->value[k].b; - f = src->value[k].b; - d = src->value[k].b; - b = src->value[k].b; + u = !!src->value[k].u; + i = !!src->value[k].u; + f = !!src->value[k].u; + d = !!src->value[k].u; + b = !!src->value[k].u; break;
default: @@ -107,7 +107,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
case HLSL_TYPE_BOOL: - dst->value[k].b = b; + dst->value[k].u = b ? ~0u : 0; break;
default:
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
With this change it is possible to store booleans as 0xffffffff, similarly as what happens at runtime.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- New
libs/vkd3d-shader/hlsl.c | 2 +- libs/vkd3d-shader/hlsl.h | 1 - libs/vkd3d-shader/hlsl.y | 4 ++-- libs/vkd3d-shader/hlsl_codegen.c | 2 +- libs/vkd3d-shader/hlsl_constant_ops.c | 20 ++++++++++---------- 5 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 664f7813..eabe189f 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1168,7 +1168,7 @@ static void dump_ir_constant(struct vkd3d_string_buffer *buffer, const struct hl switch (type->base_type) { case HLSL_TYPE_BOOL:
- vkd3d_string_buffer_printf(buffer, "%s ", value->b ? "true" : "false");
- vkd3d_string_buffer_printf(buffer, "%s ", value->u ? "true" : "false");
break;
case HLSL_TYPE_DOUBLE: diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index e4db386f..6b00d117 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -415,7 +415,6 @@ struct hlsl_ir_constant int32_t i; float f; double d;
- bool b;
} value[4]; struct hlsl_reg reg; }; diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 41fa7df4..481347ee 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -842,7 +842,7 @@ static unsigned int evaluate_array_dimension(struct hlsl_ir_node *node) case HLSL_TYPE_DOUBLE: return value->d; case HLSL_TYPE_BOOL:
- return value->b;
- return !!value->u;
default: assert(0); return 0; @@ -3394,7 +3394,7 @@ primary_expr: if (!(c = hlsl_alloc(ctx, sizeof(*c)))) YYABORT; init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), @1);
- c->value[0].b = $1;
- c->value[0].u = $1 ? ~0u : 0;
if (!($$ = make_list(ctx, &c->node))) YYABORT; } diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index 3d0f9e41..c1f5560e 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1200,7 +1200,7 @@ static void allocate_const_registers_recurse(struct hlsl_ctx *ctx, struct hlsl_b switch (type->base_type) { case HLSL_TYPE_BOOL:
- f = value->b;
- f = !!value->u;
break;
case HLSL_TYPE_FLOAT: diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 92f5a856..87b5bc90 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -47,7 +47,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].f; f = src->value[k].f; d = src->value[k].f;
- b = src->value[k].f;
- b = !!src->value[k].f;
break;
case HLSL_TYPE_DOUBLE: @@ -55,7 +55,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].d; f = src->value[k].d; d = src->value[k].d;
- b = src->value[k].d;
- b = !!src->value[k].d;
break;
case HLSL_TYPE_INT: @@ -63,7 +63,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].i; f = src->value[k].i; d = src->value[k].i;
- b = src->value[k].i;
- b = !!src->value[k].i;
break;
case HLSL_TYPE_UINT: @@ -71,15 +71,15 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].u; f = src->value[k].u; d = src->value[k].u;
- b = src->value[k].u;
- b = !!src->value[k].u;
break;
case HLSL_TYPE_BOOL:
- u = src->value[k].b;
- i = src->value[k].b;
- f = src->value[k].b;
- d = src->value[k].b;
- b = src->value[k].b;
- u = !!src->value[k].u;
- i = !!src->value[k].u;
- f = !!src->value[k].u;
- d = !!src->value[k].u;
- b = !!src->value[k].u;
break;
default: @@ -107,7 +107,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
case HLSL_TYPE_BOOL:
- dst->value[k].b = b;
- dst->value[k].u = b ? ~0u : 0;
break;
default:
2.35.1
On Mon, Mar 28, 2022 at 10:59 AM Giovanni Mascellani gmascellani@codeweavers.com wrote:
With this change it is possible to store booleans as 0xffffffff, similarly as what happens at runtime.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- New
libs/vkd3d-shader/hlsl.c | 2 +- libs/vkd3d-shader/hlsl.h | 1 - libs/vkd3d-shader/hlsl.y | 4 ++-- libs/vkd3d-shader/hlsl_codegen.c | 2 +- libs/vkd3d-shader/hlsl_constant_ops.c | 20 ++++++++++---------- 5 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 664f7813..eabe189f 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1168,7 +1168,7 @@ static void dump_ir_constant(struct vkd3d_string_buffer *buffer, const struct hl switch (type->base_type) { case HLSL_TYPE_BOOL:
vkd3d_string_buffer_printf(buffer, "%s ", value->b ? "true" : "false");
vkd3d_string_buffer_printf(buffer, "%s ", value->u ? "true" : "false"); break; case HLSL_TYPE_DOUBLE:
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index e4db386f..6b00d117 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -415,7 +415,6 @@ struct hlsl_ir_constant int32_t i; float f; double d;
} value[4]; struct hlsl_reg reg;bool b;
}; diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 41fa7df4..481347ee 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -842,7 +842,7 @@ static unsigned int evaluate_array_dimension(struct hlsl_ir_node *node) case HLSL_TYPE_DOUBLE: return value->d; case HLSL_TYPE_BOOL:
return value->b;
return !!value->u; default: assert(0); return 0;
@@ -3394,7 +3394,7 @@ primary_expr: if (!(c = hlsl_alloc(ctx, sizeof(*c)))) YYABORT; init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), @1);
c->value[0].b = $1;
c->value[0].u = $1 ? ~0u : 0; if (!($$ = make_list(ctx, &c->node))) YYABORT; }
I think I would have slightly preferred if this change (together with the similar one in fold_cast()) was in a separate patch. This works though.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com -- v2: * Use unsigned int for iteration * Do not uselessly store the bool type in a variable * Assert (instead of emitting an error) when there is a type inconsistency --- libs/vkd3d-shader/hlsl_constant_ops.c | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 87b5bc90..e282dbd4 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -223,6 +223,43 @@ static bool fold_mul(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, return true; }
+static bool fold_nequal(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, + struct hlsl_ir_constant *src1, struct hlsl_ir_constant *src2) +{ + unsigned int k; + + assert(dst->node.data_type->base_type == HLSL_TYPE_BOOL); + assert(src1->node.data_type->base_type == src2->node.data_type->base_type); + + for (k = 0; k < 4; ++k) + { + switch (src1->node.data_type->base_type) + { + case HLSL_TYPE_FLOAT: + case HLSL_TYPE_HALF: + dst->value[k].u = src1->value[k].f != src2->value[k].f; + break; + + case HLSL_TYPE_DOUBLE: + dst->value[k].u = src1->value[k].d != src2->value[k].d; + break; + + case HLSL_TYPE_INT: + case HLSL_TYPE_UINT: + case HLSL_TYPE_BOOL: + dst->value[k].u = src1->value[k].u != src2->value[k].u; + break; + + default: + assert(0); + return false; + } + + dst->value[k].u *= ~0u; + } + return true; +} + bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { struct hlsl_ir_constant *arg1, *arg2 = NULL, *res; @@ -272,6 +309,10 @@ bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void success = fold_mul(ctx, res, arg1, arg2); break;
+ case HLSL_OP2_NEQUAL: + success = fold_nequal(ctx, res, arg1, arg2); + break; + default: FIXME("Fold "%s" expression.\n", debug_hlsl_expr_op(expr->op)); success = false;
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- Use unsigned int for iteration
- Do not uselessly store the bool type in a variable
- Assert (instead of emitting an error) when there is a type inconsistency
libs/vkd3d-shader/hlsl_constant_ops.c | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 87b5bc90..e282dbd4 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -223,6 +223,43 @@ static bool fold_mul(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, return true; }
+static bool fold_nequal(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst,
- struct hlsl_ir_constant *src1, struct hlsl_ir_constant *src2)
+{
- unsigned int k;
- assert(dst->node.data_type->base_type == HLSL_TYPE_BOOL);
- assert(src1->node.data_type->base_type == src2->node.data_type->base_type);
- for (k = 0; k < 4; ++k)
- {
- switch (src1->node.data_type->base_type)
- {
- case HLSL_TYPE_FLOAT:
- case HLSL_TYPE_HALF:
- dst->value[k].u = src1->value[k].f != src2->value[k].f;
- break;
- case HLSL_TYPE_DOUBLE:
- dst->value[k].u = src1->value[k].d != src2->value[k].d;
- break;
- case HLSL_TYPE_INT:
- case HLSL_TYPE_UINT:
- case HLSL_TYPE_BOOL:
- dst->value[k].u = src1->value[k].u != src2->value[k].u;
- break;
- default:
- assert(0);
- return false;
- }
- dst->value[k].u *= ~0u;
- }
- return true;
+}
bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { struct hlsl_ir_constant *arg1, *arg2 = NULL, *res; @@ -272,6 +309,10 @@ bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void success = fold_mul(ctx, res, arg1, arg2); break;
- case HLSL_OP2_NEQUAL:
- success = fold_nequal(ctx, res, arg1, arg2);
- break;
default: FIXME("Fold "%s" expression.\n", debug_hlsl_expr_op(expr->op)); success = false; -- 2.35.1
On Mon, Mar 28, 2022 at 10:59 AM Giovanni Mascellani gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- Use unsigned int for iteration
- Do not uselessly store the bool type in a variable
- Assert (instead of emitting an error) when there is a type inconsistency
Not hard to fix up but, as a heads up, notice that there are only 2 dashes above, which would make the changelog part of the commit message.
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com --- libs/vkd3d-shader/hlsl_codegen.c | 32 +++++++++++++++++++++++++++ libs/vkd3d-shader/hlsl_constant_ops.c | 9 ++------ libs/vkd3d-shader/hlsl_sm4.c | 3 ++- 3 files changed, 36 insertions(+), 8 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index c1f5560e..f7814135 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -745,6 +745,37 @@ static bool lower_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi return true; }
+static bool lower_cast_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{ + struct hlsl_type *type = instr->data_type, *arg_type; + struct hlsl_ir_constant *zero; + struct hlsl_ir_expr *expr; + + if (instr->type != HLSL_IR_EXPR) + return false; + expr = hlsl_ir_expr(instr); + if (expr->op != HLSL_OP1_CAST) + return false; + arg_type = expr->operands[0].node->data_type; + if (type->type > HLSL_CLASS_VECTOR || arg_type->type > HLSL_CLASS_VECTOR) + return false; + if (type->base_type != HLSL_TYPE_BOOL) + return false; + + /* Narrowing casts should have already been lowered. */ + assert(type->dimx == arg_type->dimx); + + zero = hlsl_new_constant(ctx, arg_type, &instr->loc); + if (!zero) + return false; + list_add_before(&instr->entry, &zero->node.entry); + + expr->op = HLSL_OP2_NEQUAL; + hlsl_src_from_node(&expr->operands[1], &zero->node); + + return true; +} + static bool dce(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { switch (instr->type) @@ -1642,6 +1673,7 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry }
transform_ir(ctx, lower_broadcasts, body, NULL); + transform_ir(ctx, lower_cast_to_bool, body, NULL); while (transform_ir(ctx, fold_redundant_casts, body, NULL)); do { diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index e282dbd4..5cac4bde 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -27,7 +27,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct int32_t i; double d; float f; - bool b;
if (dst->node.data_type->dimx != src->node.data_type->dimx || dst->node.data_type->dimy != src->node.data_type->dimy) @@ -47,7 +46,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].f; f = src->value[k].f; d = src->value[k].f; - b = !!src->value[k].f; break;
case HLSL_TYPE_DOUBLE: @@ -55,7 +53,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].d; f = src->value[k].d; d = src->value[k].d; - b = !!src->value[k].d; break;
case HLSL_TYPE_INT: @@ -63,7 +60,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].i; f = src->value[k].i; d = src->value[k].i; - b = !!src->value[k].i; break;
case HLSL_TYPE_UINT: @@ -71,7 +67,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].u; f = src->value[k].u; d = src->value[k].u; - b = !!src->value[k].u; break;
case HLSL_TYPE_BOOL: @@ -79,7 +74,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = !!src->value[k].u; f = !!src->value[k].u; d = !!src->value[k].u; - b = !!src->value[k].u; break;
default: @@ -107,7 +101,8 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
case HLSL_TYPE_BOOL: - dst->value[k].u = b ? ~0u : 0; + /* Casts to bool should have already been lowered. */ + assert(0); break;
default: diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index af5c777f..9bc9a679 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1533,7 +1533,8 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
case HLSL_TYPE_BOOL: - hlsl_fixme(ctx, &expr->node.loc, "SM4 cast to bool.\n"); + /* Casts to bool should have already been lowered. */ + assert(0); break;
default:
Signed-off-by: Zebediah Figura zfigura@codeweavers.com
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
libs/vkd3d-shader/hlsl_codegen.c | 32 +++++++++++++++++++++++++++ libs/vkd3d-shader/hlsl_constant_ops.c | 9 ++------ libs/vkd3d-shader/hlsl_sm4.c | 3 ++- 3 files changed, 36 insertions(+), 8 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index c1f5560e..f7814135 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -745,6 +745,37 @@ static bool lower_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi return true; }
+static bool lower_cast_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) +{
- struct hlsl_type *type = instr->data_type, *arg_type;
- struct hlsl_ir_constant *zero;
- struct hlsl_ir_expr *expr;
- if (instr->type != HLSL_IR_EXPR)
- return false;
- expr = hlsl_ir_expr(instr);
- if (expr->op != HLSL_OP1_CAST)
- return false;
- arg_type = expr->operands[0].node->data_type;
- if (type->type > HLSL_CLASS_VECTOR || arg_type->type > HLSL_CLASS_VECTOR)
- return false;
- if (type->base_type != HLSL_TYPE_BOOL)
- return false;
- /* Narrowing casts should have already been lowered. */
- assert(type->dimx == arg_type->dimx);
- zero = hlsl_new_constant(ctx, arg_type, &instr->loc);
- if (!zero)
- return false;
- list_add_before(&instr->entry, &zero->node.entry);
- expr->op = HLSL_OP2_NEQUAL;
- hlsl_src_from_node(&expr->operands[1], &zero->node);
- return true;
+}
static bool dce(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context) { switch (instr->type) @@ -1642,6 +1673,7 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry }
transform_ir(ctx, lower_broadcasts, body, NULL);
- transform_ir(ctx, lower_cast_to_bool, body, NULL);
while (transform_ir(ctx, fold_redundant_casts, body, NULL)); do { diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index e282dbd4..5cac4bde 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -27,7 +27,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct int32_t i; double d; float f;
- bool b;
if (dst->node.data_type->dimx != src->node.data_type->dimx || dst->node.data_type->dimy != src->node.data_type->dimy) @@ -47,7 +46,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].f; f = src->value[k].f; d = src->value[k].f;
- b = !!src->value[k].f;
break;
case HLSL_TYPE_DOUBLE: @@ -55,7 +53,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].d; f = src->value[k].d; d = src->value[k].d;
- b = !!src->value[k].d;
break;
case HLSL_TYPE_INT: @@ -63,7 +60,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].i; f = src->value[k].i; d = src->value[k].i;
- b = !!src->value[k].i;
break;
case HLSL_TYPE_UINT: @@ -71,7 +67,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = src->value[k].u; f = src->value[k].u; d = src->value[k].u;
- b = !!src->value[k].u;
break;
case HLSL_TYPE_BOOL: @@ -79,7 +74,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct i = !!src->value[k].u; f = !!src->value[k].u; d = !!src->value[k].u;
- b = !!src->value[k].u;
break;
default: @@ -107,7 +101,8 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break;
case HLSL_TYPE_BOOL:
- dst->value[k].u = b ? ~0u : 0;
- /* Casts to bool should have already been lowered. */
- assert(0);
break;
default: diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index af5c777f..9bc9a679 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1533,7 +1533,8 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break;
case HLSL_TYPE_BOOL:
- hlsl_fixme(ctx, &expr->node.loc, "SM4 cast to bool.\n");
- /* Casts to bool should have already been lowered. */
- assert(0);
break;
default:
2.35.1
Signed-off-by: Francisco Casas fcasas@codeweavers.com
March 28, 2022 5:59 AM, "Giovanni Mascellani" gmascellani@codeweavers.com wrote:
Signed-off-by: Giovanni Mascellani gmascellani@codeweavers.com
v2:
- New
libs/vkd3d-shader/hlsl_constant_ops.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 51cee179..109fc2ee 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -194,11 +194,12 @@ static bool fold_mul(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct hlsl_ir_constant *src1, struct hlsl_ir_constant *src2) { enum hlsl_base_type type = dst->node.data_type->base_type;
- unsigned int k;
assert(type == src1->node.data_type->base_type); assert(type == src2->node.data_type->base_type);
- for (int k = 0; k < 4; ++k)
- for (k = 0; k < 4; ++k)
{ switch (type) { -- 2.35.1