[PATCH vkd3d 1/5] vkd3d-shader/hlsl: Store constant values as an array of unions.

Giovanni Mascellani gmascellani at codeweavers.com
Tue Sep 21 04:04:26 CDT 2021


Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>

Il 20/09/21 23:40, Zebediah Figura ha scritto:
> This allows us to more easily manipulate individual elements in a type-agnostic
> way. For example, it allows easier implementation of constant swizzle folding.
> 
> Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
> ---
>   libs/vkd3d-shader/hlsl.c         | 14 ++++++++------
>   libs/vkd3d-shader/hlsl.h         | 14 +++++++-------
>   libs/vkd3d-shader/hlsl.y         | 17 +++++++++--------
>   libs/vkd3d-shader/hlsl_codegen.c | 22 ++++++++++++----------
>   libs/vkd3d-shader/hlsl_sm4.c     |  2 +-
>   5 files changed, 37 insertions(+), 32 deletions(-)
> 
> diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
> index 96473d057..8a2286c45 100644
> --- a/libs/vkd3d-shader/hlsl.c
> +++ b/libs/vkd3d-shader/hlsl.c
> @@ -535,7 +535,7 @@ struct hlsl_ir_constant *hlsl_new_uint_constant(struct hlsl_ctx *ctx, unsigned i
>       if (!(c = hlsl_alloc(ctx, sizeof(*c))))
>           return NULL;
>       init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_UINT], loc);
> -    c->value.u[0] = n;
> +    c->value[0].u = n;
>       return c;
>   }
>   
> @@ -1018,26 +1018,28 @@ static void dump_ir_constant(struct vkd3d_string_buffer *buffer, const struct hl
>           vkd3d_string_buffer_printf(buffer, "{");
>       for (x = 0; x < type->dimx; ++x)
>       {
> +        const union hlsl_constant_value *value = &constant->value[x];
> +
>           switch (type->base_type)
>           {
>               case HLSL_TYPE_BOOL:
> -                vkd3d_string_buffer_printf(buffer, "%s ", constant->value.b[x] ? "true" : "false");
> +                vkd3d_string_buffer_printf(buffer, "%s ", value->b ? "true" : "false");
>                   break;
>   
>               case HLSL_TYPE_DOUBLE:
> -                vkd3d_string_buffer_printf(buffer, "%.16e ", constant->value.d[x]);
> +                vkd3d_string_buffer_printf(buffer, "%.16e ", value->d);
>                   break;
>   
>               case HLSL_TYPE_FLOAT:
> -                vkd3d_string_buffer_printf(buffer, "%.8e ", constant->value.f[x]);
> +                vkd3d_string_buffer_printf(buffer, "%.8e ", value->f);
>                   break;
>   
>               case HLSL_TYPE_INT:
> -                vkd3d_string_buffer_printf(buffer, "%d ", constant->value.i[x]);
> +                vkd3d_string_buffer_printf(buffer, "%d ", value->i);
>                   break;
>   
>               case HLSL_TYPE_UINT:
> -                vkd3d_string_buffer_printf(buffer, "%u ", constant->value.u[x]);
> +                vkd3d_string_buffer_printf(buffer, "%u ", value->u);
>                   break;
>   
>               default:
> diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
> index ce1f1b756..7079171b8 100644
> --- a/libs/vkd3d-shader/hlsl.h
> +++ b/libs/vkd3d-shader/hlsl.h
> @@ -374,14 +374,14 @@ struct hlsl_ir_store
>   struct hlsl_ir_constant
>   {
>       struct hlsl_ir_node node;
> -    union
> +    union hlsl_constant_value
>       {
> -        unsigned u[4];
> -        int i[4];
> -        float f[4];
> -        double d[4];
> -        bool b[4];
> -    } value;
> +        uint32_t u;
> +        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 2b381dc8a..2206d641d 100644
> --- a/libs/vkd3d-shader/hlsl.y
> +++ b/libs/vkd3d-shader/hlsl.y
> @@ -828,20 +828,21 @@ static unsigned int evaluate_array_dimension(struct hlsl_ir_node *node)
>           case HLSL_IR_CONSTANT:
>           {
>               struct hlsl_ir_constant *constant = hlsl_ir_constant(node);
> +            const union hlsl_constant_value *value = &constant->value[0];
>   
>               switch (constant->node.data_type->base_type)
>               {
>                   case HLSL_TYPE_UINT:
> -                    return constant->value.u[0];
> +                    return value->u;
>                   case HLSL_TYPE_INT:
> -                    return constant->value.i[0];
> +                    return value->i;
>                   case HLSL_TYPE_FLOAT:
>                   case HLSL_TYPE_HALF:
> -                    return constant->value.f[0];
> +                    return value->f;
>                   case HLSL_TYPE_DOUBLE:
> -                    return constant->value.d[0];
> +                    return value->d;
>                   case HLSL_TYPE_BOOL:
> -                    return constant->value.b[0];
> +                    return value->b;
>                   default:
>                       assert(0);
>                       return 0;
> @@ -2770,7 +2771,7 @@ primary_expr:
>               if (!(c = hlsl_alloc(ctx, sizeof(*c))))
>                   YYABORT;
>               init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_FLOAT], @1);
> -            c->value.f[0] = $1;
> +            c->value[0].f = $1;
>               if (!($$ = make_list(ctx, &c->node)))
>                   YYABORT;
>           }
> @@ -2781,7 +2782,7 @@ primary_expr:
>               if (!(c = hlsl_alloc(ctx, sizeof(*c))))
>                   YYABORT;
>               init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_INT], @1);
> -            c->value.i[0] = $1;
> +            c->value[0].i = $1;
>               if (!($$ = make_list(ctx, &c->node)))
>                   YYABORT;
>           }
> @@ -2792,7 +2793,7 @@ primary_expr:
>               if (!(c = hlsl_alloc(ctx, sizeof(*c))))
>                   YYABORT;
>               init_node(&c->node, HLSL_IR_CONSTANT, ctx->builtin_types.scalar[HLSL_TYPE_BOOL], @1);
> -            c->value.b[0] = $1;
> +            c->value[0].b = $1;
>               if (!($$ = make_list(ctx, &c->node)))
>                   YYABORT;
>           }
> diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
> index 275920159..8bd9d1385 100644
> --- a/libs/vkd3d-shader/hlsl_codegen.c
> +++ b/libs/vkd3d-shader/hlsl_codegen.c
> @@ -413,12 +413,12 @@ static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi
>                       {
>                           case HLSL_TYPE_INT:
>                               for (i = 0; i < dimx; ++i)
> -                                res->value.f[i] = arg1->value.i[i];
> +                                res->value[i].f = arg1->value[i].i;
>                               break;
>   
>                           case HLSL_TYPE_UINT:
>                               for (i = 0; i < dimx; ++i)
> -                                res->value.f[i] = arg1->value.u[i];
> +                                res->value[i].f = arg1->value[i].u;
>                               break;
>   
>                           default:
> @@ -443,17 +443,17 @@ static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi
>               {
>                   case HLSL_OP1_NEG:
>                       for (i = 0; i < instr->data_type->dimx; ++i)
> -                        res->value.u[i] = -arg1->value.u[i];
> +                        res->value[i].u = -arg1->value[i].u;
>                       break;
>   
>                   case HLSL_OP2_ADD:
>                       for (i = 0; i < instr->data_type->dimx; ++i)
> -                        res->value.u[i] = arg1->value.u[i] + arg2->value.u[i];
> +                        res->value[i].u = arg1->value[i].u + arg2->value[i].u;
>                       break;
>   
>                   case HLSL_OP2_MUL:
>                       for (i = 0; i < instr->data_type->dimx; ++i)
> -                        res->value.u[i] = arg1->value.u[i] * arg2->value.u[i];
> +                        res->value[i].u = arg1->value[i].u * arg2->value[i].u;
>                       break;
>   
>                   default:
> @@ -920,28 +920,30 @@ static void allocate_const_registers_recurse(struct hlsl_ctx *ctx, struct list *
>                   {
>                       for (x = 0, i = 0; x < 4; ++x)
>                       {
> +                        const union hlsl_constant_value *value;
>                           float f;
>   
>                           if (!(writemask & (1u << x)))
>                               continue;
> +                        value = &constant->value[i++];
>   
>                           switch (type->base_type)
>                           {
>                               case HLSL_TYPE_BOOL:
> -                                f = constant->value.b[i++];
> +                                f = value->b;
>                                   break;
>   
>                               case HLSL_TYPE_FLOAT:
>                               case HLSL_TYPE_HALF:
> -                                f = constant->value.f[i++];
> +                                f = value->f;
>                                   break;
>   
>                               case HLSL_TYPE_INT:
> -                                f = constant->value.i[i++];
> +                                f = value->i;
>                                   break;
>   
>                               case HLSL_TYPE_UINT:
> -                                f = constant->value.u[i++];
> +                                f = value->u;
>                                   break;
>   
>                               case HLSL_TYPE_DOUBLE:
> @@ -1208,7 +1210,7 @@ struct hlsl_reg hlsl_reg_from_deref(const struct hlsl_deref *deref, const struct
>       ret.allocated = var->reg.allocated;
>       ret.id = var->reg.id;
>       if (offset_node)
> -        offset = hlsl_ir_constant(offset_node)->value.u[0];
> +        offset = hlsl_ir_constant(offset_node)->value[0].u;
>       ret.id += offset / 4;
>   
>       if (type_is_single_reg(var->data_type))
> diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c
> index b347e3526..5558a1ff2 100644
> --- a/libs/vkd3d-shader/hlsl_sm4.c
> +++ b/libs/vkd3d-shader/hlsl_sm4.c
> @@ -962,7 +962,7 @@ static void write_sm4_constant(struct hlsl_ctx *ctx,
>       instr.srcs[0].reg.idx[0] = constant->reg.id;
>       instr.srcs[0].reg.idx_count = 1;
>       for (i = 0; i < dimx; ++i)
> -        instr.srcs[0].reg.immconst_uint[i] = constant->value.u[i];
> +        instr.srcs[0].reg.immconst_uint[i] = constant->value[i].u;
>       instr.src_count = 1,
>   
>       write_sm4_instruction(buffer, &instr);
> 



More information about the wine-devel mailing list