[PATCH vkd3d] vkd3d-shader/hlsl: Explicitly specify the swizzle type for sm4 source registers

Francisco Casas fcasas at codeweavers.com
Thu Nov 11 08:47:06 CST 2021


Some register types do not use a consistent swizzle type, so the
sm4_swizzle_type() function is removed.

The swizzle type now must be specified using the swizzle_type field.

A FIXME is issued when swizzle_type is used but unset, which may be useful
so that this is not forgotten on new patches.
This is at the expense of having to map between vkd3d_sm4_swizzle_type
values and their bit representations, through sm4_encode_swizzle_type()
and sm4_decode_swizzle_type().

Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
---
 libs/vkd3d-shader/dxbc.c     |  5 +--
 libs/vkd3d-shader/hlsl_sm4.c | 65 ++++++++++++++++++++++++------------
 libs/vkd3d-shader/sm4.h      |  9 +++--
 3 files changed, 52 insertions(+), 27 deletions(-)

diff --git a/libs/vkd3d-shader/dxbc.c b/libs/vkd3d-shader/dxbc.c
index b83e2654..ad971017 100644
--- a/libs/vkd3d-shader/dxbc.c
+++ b/libs/vkd3d-shader/dxbc.c
@@ -1290,8 +1290,9 @@ static bool shader_sm4_read_src_param(struct vkd3d_shader_sm4_parser *priv, cons
     }
     else
     {
-        enum vkd3d_sm4_swizzle_type swizzle_type =
+        uint32_t swizzle_type_bits =
                 (token & VKD3D_SM4_SWIZZLE_TYPE_MASK) >> VKD3D_SM4_SWIZZLE_TYPE_SHIFT;
+        enum vkd3d_sm4_swizzle_type swizzle_type = sm4_decode_swizzle_type(swizzle_type_bits);
 
         switch (swizzle_type)
         {
@@ -1312,7 +1313,7 @@ static bool shader_sm4_read_src_param(struct vkd3d_shader_sm4_parser *priv, cons
                 break;
 
             default:
-                FIXME("Unhandled swizzle type %#x.\n", swizzle_type);
+                FIXME("Unhandled swizzle bits %#x.\n", swizzle_type_bits);
                 break;
         }
     }
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c
index 9d45e163..8603b746 100644
--- a/libs/vkd3d-shader/hlsl_sm4.c
+++ b/libs/vkd3d-shader/hlsl_sm4.c
@@ -746,6 +746,7 @@ struct sm4_instruction
     struct
     {
         struct sm4_register reg;
+        enum vkd3d_sm4_swizzle_type swizzle_type;
         unsigned int swizzle;
     } srcs[2];
     unsigned int src_count;
@@ -754,26 +755,9 @@ struct sm4_instruction
     unsigned int idx_count;
 };
 
-static unsigned int sm4_swizzle_type(enum vkd3d_sm4_register_type type)
-{
-    switch (type)
-    {
-        case VKD3D_SM4_RT_IMMCONST:
-            return VKD3D_SM4_SWIZZLE_NONE;
-
-        case VKD3D_SM4_RT_CONSTBUFFER:
-        case VKD3D_SM4_RT_INPUT:
-        case VKD3D_SM4_RT_TEMP:
-            return VKD3D_SM4_SWIZZLE_VEC4;
-
-        default:
-            FIXME("Unhandled register type %#x.\n", type);
-            return VKD3D_SM4_SWIZZLE_VEC4;
-    }
-}
-
 static void sm4_register_from_deref(struct hlsl_ctx *ctx, struct sm4_register *reg,
-        unsigned int *writemask, const struct hlsl_deref *deref, const struct hlsl_type *data_type)
+        enum vkd3d_sm4_swizzle_type *swizzle_type, unsigned int *writemask,
+        const struct hlsl_deref *deref, const struct hlsl_type *data_type)
 {
     const struct hlsl_ir_var *var = deref->var;
 
@@ -783,6 +767,7 @@ static void sm4_register_from_deref(struct hlsl_ctx *ctx, struct sm4_register *r
 
         reg->type = VKD3D_SM4_RT_CONSTBUFFER;
         reg->dim = VKD3D_SM4_DIMENSION_VEC4;
+        if(swizzle_type) *swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
         reg->idx[0] = var->buffer->reg.id;
         reg->idx[1] = offset / 4;
         reg->idx_count = 2;
@@ -810,6 +795,7 @@ static void sm4_register_from_deref(struct hlsl_ctx *ctx, struct sm4_register *r
             assert(hlsl_reg.allocated);
             reg->type = VKD3D_SM4_RT_INPUT;
             reg->dim = VKD3D_SM4_DIMENSION_VEC4;
+            if(swizzle_type) *swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
             reg->idx[0] = hlsl_reg.id;
             reg->idx_count = 1;
             *writemask = hlsl_reg.writemask;
@@ -852,6 +838,7 @@ static void sm4_register_from_deref(struct hlsl_ctx *ctx, struct sm4_register *r
         assert(hlsl_reg.allocated);
         reg->type = VKD3D_SM4_RT_TEMP;
         reg->dim = VKD3D_SM4_DIMENSION_VEC4;
+        if(swizzle_type) *swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
         reg->idx[0] = hlsl_reg.id;
         reg->idx_count = 1;
         *writemask = hlsl_reg.writemask;
@@ -875,6 +862,31 @@ static uint32_t sm4_encode_register(const struct sm4_register *reg)
             | (reg->dim << VKD3D_SM4_DIMENSION_SHIFT);
 }
 
+static uint32_t sm4_encode_swizzle_type(enum vkd3d_sm4_swizzle_type type){
+    switch (type)
+    {
+        case VKD3D_SM4_SWIZZLE_UNSET:
+            FIXME("swizzle_type unset on src sm4_register\n");
+            return 0x1;
+        case VKD3D_SM4_SWIZZLE_NONE:   return 0x0;
+        case VKD3D_SM4_SWIZZLE_VEC4:   return 0x1;
+        case VKD3D_SM4_SWIZZLE_SCALAR: return 0x2;
+    }
+    assert(!"Invalid swizzle type");
+    return 0;
+}
+
+enum vkd3d_sm4_swizzle_type sm4_decode_swizzle_type(uint32_t bits){
+    switch(bits){
+        case 0x0: return VKD3D_SM4_SWIZZLE_NONE;
+        case 0x1: return VKD3D_SM4_SWIZZLE_VEC4;
+        case 0x2: return VKD3D_SM4_SWIZZLE_SCALAR;
+        default:
+            FIXME("Unhandled swizzle bits %#x.\n", bits);
+            return VKD3D_SM4_SWIZZLE_UNSET;
+    }
+}
+
 static uint32_t sm4_register_order(const struct sm4_register *reg)
 {
     uint32_t order = 1;
@@ -914,7 +926,7 @@ static void write_sm4_instruction(struct vkd3d_bytecode_buffer *buffer, const st
     for (i = 0; i < instr->src_count; ++i)
     {
         token = sm4_encode_register(&instr->srcs[i].reg);
-        token |= sm4_swizzle_type(instr->srcs[i].reg.type) << VKD3D_SM4_SWIZZLE_TYPE_SHIFT;
+        token |= sm4_encode_swizzle_type(instr->srcs[i].swizzle_type) << VKD3D_SM4_SWIZZLE_TYPE_SHIFT;
         token |= instr->srcs[i].swizzle << VKD3D_SM4_SWIZZLE_SHIFT;
         if (instr->srcs[i].reg.mod)
             token |= VKD3D_SM4_EXTENDED_OPERAND;
@@ -953,6 +965,7 @@ static void write_sm4_dcl_constant_buffer(struct vkd3d_bytecode_buffer *buffer,
         .srcs[0].reg.type = VKD3D_SM4_RT_CONSTBUFFER,
         .srcs[0].reg.idx = {cbuffer->reg.id, (cbuffer->used_size + 3) / 4},
         .srcs[0].reg.idx_count = 2,
+        .srcs[0].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4,
         .srcs[0].swizzle = HLSL_SWIZZLE(X, Y, Z, W),
         .src_count = 1,
     };
@@ -1104,6 +1117,7 @@ static void write_sm4_unary_op(struct vkd3d_bytecode_buffer *buffer, enum vkd3d_
     instr.dst_count = 1;
 
     sm4_register_from_node(&instr.srcs[0].reg, &writemask, src);
+    instr.srcs[0].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
     instr.srcs[0].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     instr.srcs[0].reg.mod = src_mod;
     instr.src_count = 1;
@@ -1124,8 +1138,10 @@ static void write_sm4_binary_op(struct vkd3d_bytecode_buffer *buffer, enum vkd3d
     instr.dst_count = 1;
 
     sm4_register_from_node(&instr.srcs[0].reg, &writemask, src1);
+    instr.srcs[0].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
     instr.srcs[0].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     sm4_register_from_node(&instr.srcs[1].reg, &writemask, src2);
+    instr.srcs[1].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
     instr.srcs[1].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     instr.src_count = 2;
 
@@ -1147,6 +1163,8 @@ static void write_sm4_constant(struct hlsl_ctx *ctx,
 
     instr.srcs[0].reg.dim = (dimx > 1) ? VKD3D_SM4_DIMENSION_VEC4 : VKD3D_SM4_DIMENSION_SCALAR;
     instr.srcs[0].reg.type = VKD3D_SM4_RT_IMMCONST;
+    instr.srcs[0].swizzle_type = VKD3D_SM4_SWIZZLE_NONE;
+
     for (i = 0; i < dimx; ++i)
         instr.srcs[0].reg.immconst_uint[i] = constant->value[i].u;
     instr.src_count = 1,
@@ -1368,7 +1386,8 @@ static void write_sm4_load(struct hlsl_ctx *ctx,
     sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, &load->node);
     instr.dst_count = 1;
 
-    sm4_register_from_deref(ctx, &instr.srcs[0].reg, &writemask, &load->src, load->node.data_type);
+    sm4_register_from_deref(ctx, &instr.srcs[0].reg, &instr.srcs[0].swizzle_type,
+            &writemask, &load->src, load->node.data_type);
     instr.srcs[0].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     instr.src_count = 1;
 
@@ -1391,11 +1410,12 @@ static void write_sm4_store(struct hlsl_ctx *ctx,
     memset(&instr, 0, sizeof(instr));
     instr.opcode = VKD3D_SM4_OP_MOV;
 
-    sm4_register_from_deref(ctx, &instr.dsts[0].reg, &writemask, &store->lhs, rhs->data_type);
+    sm4_register_from_deref(ctx, &instr.dsts[0].reg, NULL, &writemask, &store->lhs, rhs->data_type);
     instr.dsts[0].writemask = hlsl_combine_writemasks(writemask, store->writemask);
     instr.dst_count = 1;
 
     sm4_register_from_node(&instr.srcs[0].reg, &writemask, rhs);
+    instr.srcs[0].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
     instr.srcs[0].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     instr.src_count = 1;
 
@@ -1415,6 +1435,7 @@ static void write_sm4_swizzle(struct hlsl_ctx *ctx,
     instr.dst_count = 1;
 
     sm4_register_from_node(&instr.srcs[0].reg, &writemask, swizzle->val.node);
+    instr.srcs[0].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
     instr.srcs[0].swizzle = hlsl_map_swizzle(hlsl_combine_swizzles(hlsl_swizzle_from_writemask(writemask),
             swizzle->swizzle, swizzle->node.data_type->dimx), instr.dsts[0].writemask);
     instr.src_count = 1;
diff --git a/libs/vkd3d-shader/sm4.h b/libs/vkd3d-shader/sm4.h
index ddcb9a86..f0486e67 100644
--- a/libs/vkd3d-shader/sm4.h
+++ b/libs/vkd3d-shader/sm4.h
@@ -473,11 +473,14 @@ enum vkd3d_sm4_input_primitive_type
 
 enum vkd3d_sm4_swizzle_type
 {
-    VKD3D_SM4_SWIZZLE_NONE            = 0x0,
-    VKD3D_SM4_SWIZZLE_VEC4            = 0x1,
-    VKD3D_SM4_SWIZZLE_SCALAR          = 0x2,
+    VKD3D_SM4_SWIZZLE_UNSET = 0,
+    VKD3D_SM4_SWIZZLE_NONE,
+    VKD3D_SM4_SWIZZLE_VEC4,
+    VKD3D_SM4_SWIZZLE_SCALAR,
 };
 
+enum vkd3d_sm4_swizzle_type sm4_decode_swizzle_type(uint32_t sec);
+
 enum vkd3d_sm4_dimension
 {
     VKD3D_SM4_DIMENSION_NONE    = 0x0,
-- 
2.25.1




More information about the wine-devel mailing list