[PATCH vkd3d v4 07/10] vkd3d-shader/hlsl: Handle additional dimension types for textures.

Francisco Casas fcasas at codeweavers.com
Tue Dec 21 07:04:21 CST 2021


Signed-off-by: Francisco Casas <fcasas at codeweavers.com>

---

Also, sampler_dim_count() is moved from hlsl.y to hlsl.h, since it can be useful
in other files.

Note: This patch should superseed:
[PATCH vkd3d v3 07/12] vkd3d-shader/hlsl: Add dimension types for textures to enum hlsl_sampler_dim.
[PATCH vkd3d v3 09/12] vkd3d-shader/hlsl: Parse texture array types.
[PATCH vkd3d v3 10/12] vkd3d-shader/hlsl: Include new texture dimension types in sampler_dim_count().
[PATCH vkd3d v3 11/12] vkd3d-shader/hlsl: Handle mipmap level for texture arrays in sm4 ld instruction.

Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
---
 libs/vkd3d-shader/hlsl.c     | 25 ++++++++++++--------
 libs/vkd3d-shader/hlsl.h     | 33 ++++++++++++++++++++++++--
 libs/vkd3d-shader/hlsl.y     | 17 --------------
 libs/vkd3d-shader/hlsl_sm4.c | 45 ++++++++++++++++++++++--------------
 4 files changed, 74 insertions(+), 46 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index 530f7357..6c8d2f2d 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -929,10 +929,15 @@ struct vkd3d_string_buffer *hlsl_type_to_string(struct hlsl_ctx *ctx, const stru
         {
             static const char *const dimensions[] =
             {
-                [HLSL_SAMPLER_DIM_1D] = "1D",
-                [HLSL_SAMPLER_DIM_2D] = "2D",
-                [HLSL_SAMPLER_DIM_3D] = "3D",
-                [HLSL_SAMPLER_DIM_CUBE] = "Cube"
+                [HLSL_SAMPLER_DIM_1D]        = "1D",
+                [HLSL_SAMPLER_DIM_2D]        = "2D",
+                [HLSL_SAMPLER_DIM_3D]        = "3D",
+                [HLSL_SAMPLER_DIM_CUBE]      = "Cube",
+                [HLSL_SAMPLER_DIM_1DARRAY]   = "1DArray",
+                [HLSL_SAMPLER_DIM_2DARRAY]   = "2DArray",
+                [HLSL_SAMPLER_DIM_2DMS]      = "2DMS",
+                [HLSL_SAMPLER_DIM_2DMSARRAY] = "2DMSArray",
+                [HLSL_SAMPLER_DIM_CUBEARRAY] = "CubeArray",
             };
 
             switch (type->base_type)
@@ -1776,11 +1781,11 @@ static void declare_predefined_types(struct hlsl_ctx *ctx)
 
     static const char *const sampler_names[] =
     {
-        "sampler",
-        "sampler1D",
-        "sampler2D",
-        "sampler3D",
-        "samplerCUBE"
+        [HLSL_SAMPLER_DIM_GENERIC] = "sampler",
+        [HLSL_SAMPLER_DIM_1D]      = "sampler1D",
+        [HLSL_SAMPLER_DIM_2D]      = "sampler2D",
+        [HLSL_SAMPLER_DIM_3D]      = "sampler3D",
+        [HLSL_SAMPLER_DIM_CUBE]    = "samplerCUBE",
     };
 
     static const struct
@@ -1832,7 +1837,7 @@ static void declare_predefined_types(struct hlsl_ctx *ctx)
         }
     }
 
-    for (bt = 0; bt <= HLSL_SAMPLER_DIM_MAX; ++bt)
+    for (bt = 0; bt <= HLSL_SAMPLER_DIM_LAST_SAMPLER; ++bt)
     {
         type = hlsl_new_type(ctx, sampler_names[bt], HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1);
         type->sampler_dim = bt;
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index 49fa8d9d..927dca02 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -98,9 +98,38 @@ enum hlsl_sampler_dim
    HLSL_SAMPLER_DIM_2D,
    HLSL_SAMPLER_DIM_3D,
    HLSL_SAMPLER_DIM_CUBE,
-   HLSL_SAMPLER_DIM_MAX = HLSL_SAMPLER_DIM_CUBE
+   HLSL_SAMPLER_DIM_LAST_SAMPLER = HLSL_SAMPLER_DIM_CUBE,
+   HLSL_SAMPLER_DIM_1DARRAY,
+   HLSL_SAMPLER_DIM_2DARRAY,
+   HLSL_SAMPLER_DIM_2DMS,
+   HLSL_SAMPLER_DIM_2DMSARRAY,
+   HLSL_SAMPLER_DIM_CUBEARRAY,
+   HLSL_SAMPLER_DIM_MAX = HLSL_SAMPLER_DIM_CUBEARRAY,
 };
 
+static inline unsigned int sampler_dim_count(enum hlsl_sampler_dim dim)
+{
+    switch (dim)
+    {
+        case HLSL_SAMPLER_DIM_1D:
+            return 1;
+        case HLSL_SAMPLER_DIM_1DARRAY:
+        case HLSL_SAMPLER_DIM_2D:
+        case HLSL_SAMPLER_DIM_2DMS:
+            return 2;
+        case HLSL_SAMPLER_DIM_2DARRAY:
+        case HLSL_SAMPLER_DIM_2DMSARRAY:
+        case HLSL_SAMPLER_DIM_3D:
+        case HLSL_SAMPLER_DIM_CUBE:
+            return 3;
+        case HLSL_SAMPLER_DIM_CUBEARRAY:
+            return 4;
+        default:
+            assert(0);
+            return 0;
+    }
+}
+
 enum hlsl_matrix_majority
 {
     HLSL_COLUMN_MAJOR,
@@ -489,7 +518,7 @@ struct hlsl_ctx
         struct hlsl_type *vector[HLSL_TYPE_LAST_SCALAR + 1][4];
         /* matrix[float][2][4] is a float4x2, i.e. dimx = 2, dimy = 4 */
         struct hlsl_type *matrix[HLSL_TYPE_LAST_SCALAR + 1][4][4];
-        struct hlsl_type *sampler[HLSL_SAMPLER_DIM_MAX + 1];
+        struct hlsl_type *sampler[HLSL_SAMPLER_DIM_LAST_SAMPLER + 1];
         struct hlsl_type *Void;
     } builtin_types;
 
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index a321789c..0fd6b9a3 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -1513,23 +1513,6 @@ static struct list *declare_vars(struct hlsl_ctx *ctx, struct hlsl_type *basic_t
     return statements_list;
 }
 
-static unsigned int sampler_dim_count(enum hlsl_sampler_dim dim)
-{
-    switch (dim)
-    {
-        case HLSL_SAMPLER_DIM_1D:
-            return 1;
-        case HLSL_SAMPLER_DIM_2D:
-            return 2;
-        case HLSL_SAMPLER_DIM_3D:
-        case HLSL_SAMPLER_DIM_CUBE:
-            return 3;
-        default:
-            assert(0);
-            return 0;
-    }
-}
-
 struct find_function_call_args
 {
     const struct parse_initializer *params;
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c
index 61247031..3ee68736 100644
--- a/libs/vkd3d-shader/hlsl_sm4.c
+++ b/libs/vkd3d-shader/hlsl_sm4.c
@@ -424,6 +424,7 @@ static D3D_RESOURCE_RETURN_TYPE sm4_resource_format(const struct hlsl_type *type
 
 static D3D_SRV_DIMENSION sm4_rdef_resource_dimension(const struct hlsl_type *type)
 {
+    assert(type->base_type == HLSL_TYPE_TEXTURE); /* for now, until other resources are handled. */
     switch (type->sampler_dim)
     {
         case HLSL_SAMPLER_DIM_1D:
@@ -434,6 +435,16 @@ static D3D_SRV_DIMENSION sm4_rdef_resource_dimension(const struct hlsl_type *typ
             return D3D_SRV_DIMENSION_TEXTURE3D;
         case HLSL_SAMPLER_DIM_CUBE:
             return D3D_SRV_DIMENSION_TEXTURECUBE;
+        case HLSL_SAMPLER_DIM_1DARRAY:
+            return D3D_SRV_DIMENSION_TEXTURE1DARRAY;
+        case HLSL_SAMPLER_DIM_2DARRAY:
+            return D3D_SRV_DIMENSION_TEXTURE2DARRAY;
+        case HLSL_SAMPLER_DIM_2DMS:
+            return D3D_SRV_DIMENSION_TEXTURE2DMS;
+        case HLSL_SAMPLER_DIM_2DMSARRAY:
+            return D3D_SRV_DIMENSION_TEXTURE2DMSARRAY;
+        case HLSL_SAMPLER_DIM_CUBEARRAY:
+            return D3D_SRV_DIMENSION_TEXTURECUBEARRAY;
         default:
             assert(0);
             return D3D_SRV_DIMENSION_UNKNOWN;
@@ -711,6 +722,7 @@ static void write_sm4_rdef(struct hlsl_ctx *ctx, struct dxbc_writer *dxbc)
 
 static enum vkd3d_sm4_resource_type sm4_resource_dimension(const struct hlsl_type *type)
 {
+    assert(type->base_type == HLSL_TYPE_TEXTURE); /* for now, until other resources are handled. */
     switch (type->sampler_dim)
     {
         case HLSL_SAMPLER_DIM_1D:
@@ -721,6 +733,16 @@ static enum vkd3d_sm4_resource_type sm4_resource_dimension(const struct hlsl_typ
             return VKD3D_SM4_RESOURCE_TEXTURE_3D;
         case HLSL_SAMPLER_DIM_CUBE:
             return VKD3D_SM4_RESOURCE_TEXTURE_CUBE;
+        case HLSL_SAMPLER_DIM_1DARRAY:
+            return VKD3D_SM4_RESOURCE_TEXTURE_1DARRAY;
+        case HLSL_SAMPLER_DIM_2DARRAY:
+            return VKD3D_SM4_RESOURCE_TEXTURE_2DARRAY;
+        case HLSL_SAMPLER_DIM_2DMS:
+            return VKD3D_SM4_RESOURCE_TEXTURE_2DMS;
+        case HLSL_SAMPLER_DIM_2DMSARRAY:
+            return VKD3D_SM4_RESOURCE_TEXTURE_2DMSARRAY;
+        case HLSL_SAMPLER_DIM_CUBEARRAY:
+            return VKD3D_SM4_RESOURCE_TEXTURE_CUBEARRAY;
         default:
             assert(0);
             return 0;
@@ -1294,6 +1316,7 @@ static void write_sm4_ld(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buf
 {
     struct sm4_instruction instr;
     unsigned int writemask;
+    unsigned int dim_count;
 
     memset(&instr, 0, sizeof(instr));
     instr.opcode = VKD3D_SM4_OP_LD;
@@ -1306,23 +1329,11 @@ static void write_sm4_ld(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buf
 
     /* Mipmap level is in the last component in the IR, but needs to be in the W
      * component in the instruction. */
-    switch (resource_type->sampler_dim)
-    {
-        case HLSL_SAMPLER_DIM_1D:
-            instr.srcs[0].swizzle = hlsl_combine_swizzles(instr.srcs[0].swizzle, HLSL_SWIZZLE(X, X, X, Y), 4);
-            break;
-
-        case HLSL_SAMPLER_DIM_2D:
-            instr.srcs[0].swizzle = hlsl_combine_swizzles(instr.srcs[0].swizzle, HLSL_SWIZZLE(X, Y, X, Z), 4);
-            break;
-
-        case HLSL_SAMPLER_DIM_3D:
-        case HLSL_SAMPLER_DIM_CUBE:
-            break;
-
-        case HLSL_SAMPLER_DIM_GENERIC:
-            assert(0);
-    }
+    dim_count = sampler_dim_count(resource_type->sampler_dim);
+    if (dim_count == 1)
+        instr.srcs[0].swizzle = hlsl_combine_swizzles(instr.srcs[0].swizzle, HLSL_SWIZZLE(X, X, X, Y), 4);
+    if (dim_count == 2)
+        instr.srcs[0].swizzle = hlsl_combine_swizzles(instr.srcs[0].swizzle, HLSL_SWIZZLE(X, Y, X, Z), 4);
 
     sm4_register_from_deref(ctx, &instr.srcs[1].reg, &writemask, &instr.srcs[1].swizzle_type,
             resource, resource_type);
-- 
2.25.1




More information about the wine-devel mailing list