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

Francisco Casas fcasas at codeweavers.com
Thu Nov 11 10:18:48 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.

Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
---
 libs/vkd3d-shader/hlsl_sm4.c | 59 ++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 33 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c
index 9d45e163..35cf4820 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)
+        unsigned int *writemask, enum vkd3d_sm4_swizzle_type *swizzle_type,
+        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,17 +838,20 @@ 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;
     }
 }
 
-static void sm4_register_from_node(struct sm4_register *reg, unsigned int *writemask, const struct hlsl_ir_node *instr)
+static void sm4_register_from_node(struct sm4_register *reg, unsigned int *writemask,
+        enum vkd3d_sm4_swizzle_type *swizzle_type, const struct hlsl_ir_node *instr)
 {
     assert(instr->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] = instr->reg.id;
     reg->idx_count = 1;
     *writemask = instr->reg.writemask;
@@ -914,7 +903,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 |= (uint32_t)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 +942,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,
     };
@@ -1100,10 +1090,10 @@ static void write_sm4_unary_op(struct vkd3d_bytecode_buffer *buffer, enum vkd3d_
     memset(&instr, 0, sizeof(instr));
     instr.opcode = opcode;
 
-    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, dst);
+    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, dst);
     instr.dst_count = 1;
 
-    sm4_register_from_node(&instr.srcs[0].reg, &writemask, src);
+    sm4_register_from_node(&instr.srcs[0].reg, &writemask, &instr.srcs[0].swizzle_type, src);
     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;
@@ -1120,12 +1110,12 @@ static void write_sm4_binary_op(struct vkd3d_bytecode_buffer *buffer, enum vkd3d
     memset(&instr, 0, sizeof(instr));
     instr.opcode = opcode;
 
-    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, dst);
+    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, dst);
     instr.dst_count = 1;
 
-    sm4_register_from_node(&instr.srcs[0].reg, &writemask, src1);
+    sm4_register_from_node(&instr.srcs[0].reg, &writemask, &instr.srcs[0].swizzle_type, src1);
     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);
+    sm4_register_from_node(&instr.srcs[1].reg, &writemask, &instr.srcs[1].swizzle_type, src2);
     instr.srcs[1].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     instr.src_count = 2;
 
@@ -1142,11 +1132,13 @@ static void write_sm4_constant(struct hlsl_ctx *ctx,
     memset(&instr, 0, sizeof(instr));
     instr.opcode = VKD3D_SM4_OP_MOV;
 
-    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, &constant->node);
+    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, &constant->node);
     instr.dst_count = 1;
 
     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,
@@ -1365,10 +1357,11 @@ static void write_sm4_load(struct hlsl_ctx *ctx,
     memset(&instr, 0, sizeof(instr));
     instr.opcode = VKD3D_SM4_OP_MOV;
 
-    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, &load->node);
+    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, &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, &writemask, &instr.srcs[0].swizzle_type,
+            &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 +1384,11 @@ 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, &writemask, NULL, &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);
+    sm4_register_from_node(&instr.srcs[0].reg, &writemask, &instr.srcs[0].swizzle_type, rhs);
     instr.srcs[0].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
     instr.src_count = 1;
 
@@ -1411,10 +1404,10 @@ static void write_sm4_swizzle(struct hlsl_ctx *ctx,
     memset(&instr, 0, sizeof(instr));
     instr.opcode = VKD3D_SM4_OP_MOV;
 
-    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, &swizzle->node);
+    sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, &swizzle->node);
     instr.dst_count = 1;
 
-    sm4_register_from_node(&instr.srcs[0].reg, &writemask, swizzle->val.node);
+    sm4_register_from_node(&instr.srcs[0].reg, &writemask, &instr.srcs[0].swizzle_type, swizzle->val.node);
     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;
-- 
2.25.1




More information about the wine-devel mailing list