[PATCH vkd3d 1/6] vkd3d-shader/hlsl: Introduce a helper for getting numeric types.

Giovanni Mascellani gmascellani at codeweavers.com
Thu Oct 14 08:37:07 CDT 2021


Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
---
 libs/vkd3d-shader/hlsl.c |  4 ++--
 libs/vkd3d-shader/hlsl.h | 28 ++++++++++++++++++++++++++++
 libs/vkd3d-shader/hlsl.y | 28 ++++++++++++----------------
 3 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index 9bce6beb..421aaf27 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -560,7 +560,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);
+    init_node(&c->node, HLSL_IR_CONSTANT, get_scalar_type(ctx, HLSL_TYPE_UINT), loc);
     c->value[0].u = n;
     return c;
 }
@@ -650,7 +650,7 @@ struct hlsl_ir_swizzle *hlsl_new_swizzle(struct hlsl_ctx *ctx, DWORD s, unsigned
     if (!(swizzle = hlsl_alloc(ctx, sizeof(*swizzle))))
         return NULL;
     init_node(&swizzle->node, HLSL_IR_SWIZZLE,
-            ctx->builtin_types.vector[val->data_type->base_type][components - 1], *loc);
+            get_vector_type(ctx, val->data_type->base_type, components), *loc);
     hlsl_src_from_node(&swizzle->val, val);
     swizzle->swizzle = s;
     return swizzle;
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index e401971e..2e6b1bf7 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -629,6 +629,34 @@ static inline void hlsl_release_string_buffer(struct hlsl_ctx *ctx, struct vkd3d
     vkd3d_string_buffer_release(&ctx->string_buffers, buffer);
 }
 
+static inline struct hlsl_type *get_scalar_type(const struct hlsl_ctx *ctx, enum hlsl_base_type base_type)
+{
+    return ctx->builtin_types.scalar[base_type];
+}
+
+static inline struct hlsl_type *get_vector_type(const struct hlsl_ctx *ctx, enum hlsl_base_type base_type,
+        unsigned int dimx)
+{
+    return ctx->builtin_types.vector[base_type][dimx - 1];
+}
+
+static inline struct hlsl_type *get_matrix_type(const struct hlsl_ctx *ctx, enum hlsl_base_type base_type,
+        unsigned int dimx, unsigned int dimy)
+{
+    return ctx->builtin_types.matrix[base_type][dimx - 1][dimy - 1];
+}
+
+static inline struct hlsl_type *get_numeric_type(const struct hlsl_ctx *ctx, enum hlsl_type_class type,
+        enum hlsl_base_type base_type, unsigned int dimx, unsigned int dimy)
+{
+    if (type == HLSL_CLASS_SCALAR)
+        return get_scalar_type(ctx, base_type);
+    else if (type == HLSL_CLASS_VECTOR)
+        return get_vector_type(ctx, base_type, dimx);
+    else
+        return get_matrix_type(ctx, base_type, dimx, dimy);
+}
+
 const char *debug_hlsl_expr_op(enum hlsl_ir_expr_op op);
 const char *debug_hlsl_type(struct hlsl_ctx *ctx, const struct hlsl_type *type);
 const char *debug_hlsl_writemask(unsigned int writemask);
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index ae19cd1a..addb9c59 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -1021,11 +1021,7 @@ static struct hlsl_type *expr_common_type(struct hlsl_ctx *ctx, struct hlsl_type
         }
     }
 
-    if (type == HLSL_CLASS_SCALAR)
-        return ctx->builtin_types.scalar[base];
-    if (type == HLSL_CLASS_VECTOR)
-        return ctx->builtin_types.vector[base][dimx - 1];
-    return ctx->builtin_types.matrix[base][dimx - 1][dimy - 1];
+    return get_numeric_type(ctx, type, base, dimx, dimy);
 }
 
 static struct hlsl_ir_expr *add_expr(struct hlsl_ctx *ctx, struct list *instrs, enum hlsl_ir_expr_op op,
@@ -1761,7 +1757,7 @@ static struct list *add_constructor(struct hlsl_ctx *ctx, struct hlsl_type *type
         }
 
         if (!(arg = add_implicit_conversion(ctx, params->instrs, arg,
-                ctx->builtin_types.vector[type->base_type][width - 1], &arg->loc)))
+                get_vector_type(ctx, type->base_type, width), &arg->loc)))
             continue;
 
         if (!(store = hlsl_new_store(ctx, var, NULL, arg,
@@ -1816,9 +1812,9 @@ static bool add_method_call(struct hlsl_ctx *ctx, struct list *instrs, struct hl
         if (params->args_count >= 2)
             FIXME("Ignoring index and/or offset parameter(s).\n");
 
-        /* -1 for zero-indexing; +1 for the mipmap level */
+        /* +1 for the mipmap level */
         if (!(coords = add_implicit_conversion(ctx, instrs, params->args[0],
-                ctx->builtin_types.vector[HLSL_TYPE_INT][sampler_dim - 1 + 1], loc)))
+                get_vector_type(ctx, HLSL_TYPE_INT, sampler_dim + 1), loc)))
             return false;
 
         if (!(load = hlsl_new_resource_load(ctx, object_type->e.resource_format, HLSL_RESOURCE_LOAD,
@@ -2500,7 +2496,7 @@ type:
                 YYABORT;
             }
 
-            $$ = ctx->builtin_types.vector[$3->base_type][$5 - 1];
+            $$ = get_vector_type(ctx, $3->base_type, $5);
         }
     | KW_MATRIX '<' type ',' C_INTEGER ',' C_INTEGER '>'
         {
@@ -2528,7 +2524,7 @@ type:
                 YYABORT;
             }
 
-            $$ = ctx->builtin_types.matrix[$3->base_type][$7 - 1][$5 - 1];
+            $$ = get_matrix_type(ctx, $3->base_type, $7, $5);
         }
     | KW_VOID
         {
@@ -2560,7 +2556,7 @@ type:
         }
     | texture_type
         {
-            $$ = hlsl_new_texture_type(ctx, $1, ctx->builtin_types.vector[HLSL_TYPE_FLOAT][4 - 1]);
+            $$ = hlsl_new_texture_type(ctx, $1, get_vector_type(ctx, HLSL_TYPE_FLOAT, 4));
         }
     | texture_type '<' type '>'
         {
@@ -2971,7 +2967,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);
+            init_node(&c->node, HLSL_IR_CONSTANT, get_scalar_type(ctx, HLSL_TYPE_FLOAT), @1);
             c->value[0].f = $1;
             if (!($$ = make_list(ctx, &c->node)))
                 YYABORT;
@@ -2982,7 +2978,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);
+            init_node(&c->node, HLSL_IR_CONSTANT, get_scalar_type(ctx, HLSL_TYPE_INT), @1);
             c->value[0].i = $1;
             if (!($$ = make_list(ctx, &c->node)))
                 YYABORT;
@@ -2993,7 +2989,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);
+            init_node(&c->node, HLSL_IR_CONSTANT, get_scalar_type(ctx, HLSL_TYPE_BOOL), @1);
             c->value[0].b = $1;
             if (!($$ = make_list(ctx, &c->node)))
                 YYABORT;
@@ -3030,7 +3026,7 @@ primary_expr:
                 struct hlsl_ir_var *var;
 
                 if (!(var = hlsl_new_synthetic_var(ctx, "<state-block-expr>",
-                        ctx->builtin_types.scalar[HLSL_TYPE_INT], @1)))
+                        get_scalar_type(ctx, HLSL_TYPE_INT), @1)))
                     YYABORT;
                 if (!(load = hlsl_new_var_load(ctx, var, @1)))
                     YYABORT;
@@ -3116,7 +3112,7 @@ postfix_expr:
                 YYABORT;
             }
 
-            if (!(cast = hlsl_new_cast(ctx, index, ctx->builtin_types.scalar[HLSL_TYPE_UINT], &index->loc)))
+            if (!(cast = hlsl_new_cast(ctx, index, get_scalar_type(ctx, HLSL_TYPE_UINT), &index->loc)))
             {
                 destroy_instr_list($1);
                 YYABORT;
-- 
2.33.0




More information about the wine-devel mailing list