xref: /reactos/dll/directx/wine/wined3d/shader_sm4.c (revision f4d29a74)
1 /*
2  * Copyright 2009 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "config.h"
20 #include "wine/port.h"
21 
22 #include "wined3d_private.h"
23 
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_bytecode);
26 
27 #define WINED3D_SM4_INSTRUCTION_MODIFIER        (0x1u << 31)
28 
29 #define WINED3D_SM4_MODIFIER_AOFFIMMI           0x1
30 #define WINED3D_SM4_AOFFIMMI_U_SHIFT            9
31 #define WINED3D_SM4_AOFFIMMI_U_MASK             (0xfu << WINED3D_SM4_AOFFIMMI_U_SHIFT)
32 #define WINED3D_SM4_AOFFIMMI_V_SHIFT            13
33 #define WINED3D_SM4_AOFFIMMI_V_MASK             (0xfu << WINED3D_SM4_AOFFIMMI_V_SHIFT)
34 #define WINED3D_SM4_AOFFIMMI_W_SHIFT            17
35 #define WINED3D_SM4_AOFFIMMI_W_MASK             (0xfu << WINED3D_SM4_AOFFIMMI_W_SHIFT)
36 
37 #define WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT    24
38 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK     (0x1fu << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
39 
40 #define WINED3D_SM4_INSTRUCTION_FLAGS_SHIFT     11
41 #define WINED3D_SM4_INSTRUCTION_FLAGS_MASK      (0x7u << WINED3D_SM4_INSTRUCTION_FLAGS_SHIFT)
42 
43 #define WINED3D_SM4_RESOURCE_TYPE_SHIFT         11
44 #define WINED3D_SM4_RESOURCE_TYPE_MASK          (0xfu << WINED3D_SM4_RESOURCE_TYPE_SHIFT)
45 
46 #define WINED3D_SM4_PRIMITIVE_TYPE_SHIFT        11
47 #define WINED3D_SM4_PRIMITIVE_TYPE_MASK         (0x3fu << WINED3D_SM4_PRIMITIVE_TYPE_SHIFT)
48 
49 #define WINED3D_SM4_INDEX_TYPE_SHIFT            11
50 #define WINED3D_SM4_INDEX_TYPE_MASK             (0x1u << WINED3D_SM4_INDEX_TYPE_SHIFT)
51 
52 #define WINED3D_SM4_SAMPLER_MODE_SHIFT          11
53 #define WINED3D_SM4_SAMPLER_MODE_MASK           (0xfu << WINED3D_SM4_SAMPLER_MODE_SHIFT)
54 
55 #define WINED3D_SM4_SHADER_DATA_TYPE_SHIFT      11
56 #define WINED3D_SM4_SHADER_DATA_TYPE_MASK       (0xfu << WINED3D_SM4_SHADER_DATA_TYPE_SHIFT)
57 
58 #define WINED3D_SM4_INTERPOLATION_MODE_SHIFT    11
59 #define WINED3D_SM4_INTERPOLATION_MODE_MASK     (0xfu << WINED3D_SM4_INTERPOLATION_MODE_SHIFT)
60 
61 #define WINED3D_SM4_GLOBAL_FLAGS_SHIFT          11
62 #define WINED3D_SM4_GLOBAL_FLAGS_MASK           (0xffu << WINED3D_SM4_GLOBAL_FLAGS_SHIFT)
63 
64 #define WINED3D_SM5_PRECISE_SHIFT               19
65 #define WINED3D_SM5_PRECISE_MASK                (0xfu << WINED3D_SM5_PRECISE_SHIFT)
66 
67 #define WINED3D_SM5_CONTROL_POINT_COUNT_SHIFT   11
68 #define WINED3D_SM5_CONTROL_POINT_COUNT_MASK    (0xffu << WINED3D_SM5_CONTROL_POINT_COUNT_SHIFT)
69 
70 #define WINED3D_SM5_FP_ARRAY_SIZE_SHIFT         16
71 #define WINED3D_SM5_FP_TABLE_COUNT_MASK         0xffffu
72 
73 #define WINED3D_SM5_UAV_FLAGS_SHIFT             15
74 #define WINED3D_SM5_UAV_FLAGS_MASK              (0x1ffu << WINED3D_SM5_UAV_FLAGS_SHIFT)
75 
76 #define WINED3D_SM5_SYNC_FLAGS_SHIFT            11
77 #define WINED3D_SM5_SYNC_FLAGS_MASK             (0xffu << WINED3D_SM5_SYNC_FLAGS_SHIFT)
78 
79 #define WINED3D_SM5_TESSELLATOR_SHIFT           11
80 #define WINED3D_SM5_TESSELLATOR_MASK            (0xfu << WINED3D_SM5_TESSELLATOR_SHIFT)
81 
82 #define WINED3D_SM4_OPCODE_MASK                 0xff
83 
84 #define WINED3D_SM4_REGISTER_MODIFIER           (0x1u << 31)
85 
86 #define WINED3D_SM4_ADDRESSING_SHIFT1           25
87 #define WINED3D_SM4_ADDRESSING_MASK1            (0x3u << WINED3D_SM4_ADDRESSING_SHIFT1)
88 
89 #define WINED3D_SM4_ADDRESSING_SHIFT0           22
90 #define WINED3D_SM4_ADDRESSING_MASK0            (0x3u << WINED3D_SM4_ADDRESSING_SHIFT0)
91 
92 #define WINED3D_SM4_REGISTER_ORDER_SHIFT        20
93 #define WINED3D_SM4_REGISTER_ORDER_MASK         (0x3u << WINED3D_SM4_REGISTER_ORDER_SHIFT)
94 
95 #define WINED3D_SM4_REGISTER_TYPE_SHIFT         12
96 #define WINED3D_SM4_REGISTER_TYPE_MASK          (0xffu << WINED3D_SM4_REGISTER_TYPE_SHIFT)
97 
98 #define WINED3D_SM4_SWIZZLE_TYPE_SHIFT          2
99 #define WINED3D_SM4_SWIZZLE_TYPE_MASK           (0x3u << WINED3D_SM4_SWIZZLE_TYPE_SHIFT)
100 
101 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT         0
102 #define WINED3D_SM4_IMMCONST_TYPE_MASK          (0x3u << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
103 
104 #define WINED3D_SM4_WRITEMASK_SHIFT             4
105 #define WINED3D_SM4_WRITEMASK_MASK              (0xfu << WINED3D_SM4_WRITEMASK_SHIFT)
106 
107 #define WINED3D_SM4_SWIZZLE_SHIFT               4
108 #define WINED3D_SM4_SWIZZLE_MASK                (0xffu << WINED3D_SM4_SWIZZLE_SHIFT)
109 
110 #define WINED3D_SM4_VERSION_MAJOR(version)      (((version) >> 4) & 0xf)
111 #define WINED3D_SM4_VERSION_MINOR(version)      (((version) >> 0) & 0xf)
112 
113 #define WINED3D_SM4_ADDRESSING_RELATIVE         0x2
114 #define WINED3D_SM4_ADDRESSING_OFFSET           0x1
115 
116 #define WINED3D_SM4_INSTRUCTION_FLAG_SATURATE   0x4
117 
118 #define WINED3D_SM4_CONDITIONAL_NZ              (0x1u << 18)
119 
120 enum wined3d_sm4_opcode
121 {
122     WINED3D_SM4_OP_ADD                              = 0x00,
123     WINED3D_SM4_OP_AND                              = 0x01,
124     WINED3D_SM4_OP_BREAK                            = 0x02,
125     WINED3D_SM4_OP_BREAKC                           = 0x03,
126     WINED3D_SM4_OP_CASE                             = 0x06,
127     WINED3D_SM4_OP_CONTINUE                         = 0x07,
128     WINED3D_SM4_OP_CONTINUEC                        = 0x08,
129     WINED3D_SM4_OP_CUT                              = 0x09,
130     WINED3D_SM4_OP_DEFAULT                          = 0x0a,
131     WINED3D_SM4_OP_DERIV_RTX                        = 0x0b,
132     WINED3D_SM4_OP_DERIV_RTY                        = 0x0c,
133     WINED3D_SM4_OP_DISCARD                          = 0x0d,
134     WINED3D_SM4_OP_DIV                              = 0x0e,
135     WINED3D_SM4_OP_DP2                              = 0x0f,
136     WINED3D_SM4_OP_DP3                              = 0x10,
137     WINED3D_SM4_OP_DP4                              = 0x11,
138     WINED3D_SM4_OP_ELSE                             = 0x12,
139     WINED3D_SM4_OP_EMIT                             = 0x13,
140     WINED3D_SM4_OP_ENDIF                            = 0x15,
141     WINED3D_SM4_OP_ENDLOOP                          = 0x16,
142     WINED3D_SM4_OP_ENDSWITCH                        = 0x17,
143     WINED3D_SM4_OP_EQ                               = 0x18,
144     WINED3D_SM4_OP_EXP                              = 0x19,
145     WINED3D_SM4_OP_FRC                              = 0x1a,
146     WINED3D_SM4_OP_FTOI                             = 0x1b,
147     WINED3D_SM4_OP_FTOU                             = 0x1c,
148     WINED3D_SM4_OP_GE                               = 0x1d,
149     WINED3D_SM4_OP_IADD                             = 0x1e,
150     WINED3D_SM4_OP_IF                               = 0x1f,
151     WINED3D_SM4_OP_IEQ                              = 0x20,
152     WINED3D_SM4_OP_IGE                              = 0x21,
153     WINED3D_SM4_OP_ILT                              = 0x22,
154     WINED3D_SM4_OP_IMAD                             = 0x23,
155     WINED3D_SM4_OP_IMAX                             = 0x24,
156     WINED3D_SM4_OP_IMIN                             = 0x25,
157     WINED3D_SM4_OP_IMUL                             = 0x26,
158     WINED3D_SM4_OP_INE                              = 0x27,
159     WINED3D_SM4_OP_INEG                             = 0x28,
160     WINED3D_SM4_OP_ISHL                             = 0x29,
161     WINED3D_SM4_OP_ISHR                             = 0x2a,
162     WINED3D_SM4_OP_ITOF                             = 0x2b,
163     WINED3D_SM4_OP_LABEL                            = 0x2c,
164     WINED3D_SM4_OP_LD                               = 0x2d,
165     WINED3D_SM4_OP_LD2DMS                           = 0x2e,
166     WINED3D_SM4_OP_LOG                              = 0x2f,
167     WINED3D_SM4_OP_LOOP                             = 0x30,
168     WINED3D_SM4_OP_LT                               = 0x31,
169     WINED3D_SM4_OP_MAD                              = 0x32,
170     WINED3D_SM4_OP_MIN                              = 0x33,
171     WINED3D_SM4_OP_MAX                              = 0x34,
172     WINED3D_SM4_OP_SHADER_DATA                      = 0x35,
173     WINED3D_SM4_OP_MOV                              = 0x36,
174     WINED3D_SM4_OP_MOVC                             = 0x37,
175     WINED3D_SM4_OP_MUL                              = 0x38,
176     WINED3D_SM4_OP_NE                               = 0x39,
177     WINED3D_SM4_OP_NOP                              = 0x3a,
178     WINED3D_SM4_OP_NOT                              = 0x3b,
179     WINED3D_SM4_OP_OR                               = 0x3c,
180     WINED3D_SM4_OP_RESINFO                          = 0x3d,
181     WINED3D_SM4_OP_RET                              = 0x3e,
182     WINED3D_SM4_OP_RETC                             = 0x3f,
183     WINED3D_SM4_OP_ROUND_NE                         = 0x40,
184     WINED3D_SM4_OP_ROUND_NI                         = 0x41,
185     WINED3D_SM4_OP_ROUND_PI                         = 0x42,
186     WINED3D_SM4_OP_ROUND_Z                          = 0x43,
187     WINED3D_SM4_OP_RSQ                              = 0x44,
188     WINED3D_SM4_OP_SAMPLE                           = 0x45,
189     WINED3D_SM4_OP_SAMPLE_C                         = 0x46,
190     WINED3D_SM4_OP_SAMPLE_C_LZ                      = 0x47,
191     WINED3D_SM4_OP_SAMPLE_LOD                       = 0x48,
192     WINED3D_SM4_OP_SAMPLE_GRAD                      = 0x49,
193     WINED3D_SM4_OP_SAMPLE_B                         = 0x4a,
194     WINED3D_SM4_OP_SQRT                             = 0x4b,
195     WINED3D_SM4_OP_SWITCH                           = 0x4c,
196     WINED3D_SM4_OP_SINCOS                           = 0x4d,
197     WINED3D_SM4_OP_UDIV                             = 0x4e,
198     WINED3D_SM4_OP_ULT                              = 0x4f,
199     WINED3D_SM4_OP_UGE                              = 0x50,
200     WINED3D_SM4_OP_UMUL                             = 0x51,
201     WINED3D_SM4_OP_UMAX                             = 0x53,
202     WINED3D_SM4_OP_UMIN                             = 0x54,
203     WINED3D_SM4_OP_USHR                             = 0x55,
204     WINED3D_SM4_OP_UTOF                             = 0x56,
205     WINED3D_SM4_OP_XOR                              = 0x57,
206     WINED3D_SM4_OP_DCL_RESOURCE                     = 0x58,
207     WINED3D_SM4_OP_DCL_CONSTANT_BUFFER              = 0x59,
208     WINED3D_SM4_OP_DCL_SAMPLER                      = 0x5a,
209     WINED3D_SM4_OP_DCL_INDEX_RANGE                  = 0x5b,
210     WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY              = 0x5c,
211     WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE              = 0x5d,
212     WINED3D_SM4_OP_DCL_VERTICES_OUT                 = 0x5e,
213     WINED3D_SM4_OP_DCL_INPUT                        = 0x5f,
214     WINED3D_SM4_OP_DCL_INPUT_SGV                    = 0x60,
215     WINED3D_SM4_OP_DCL_INPUT_SIV                    = 0x61,
216     WINED3D_SM4_OP_DCL_INPUT_PS                     = 0x62,
217     WINED3D_SM4_OP_DCL_INPUT_PS_SGV                 = 0x63,
218     WINED3D_SM4_OP_DCL_INPUT_PS_SIV                 = 0x64,
219     WINED3D_SM4_OP_DCL_OUTPUT                       = 0x65,
220     WINED3D_SM4_OP_DCL_OUTPUT_SIV                   = 0x67,
221     WINED3D_SM4_OP_DCL_TEMPS                        = 0x68,
222     WINED3D_SM4_OP_DCL_INDEXABLE_TEMP               = 0x69,
223     WINED3D_SM4_OP_DCL_GLOBAL_FLAGS                 = 0x6a,
224     WINED3D_SM4_OP_LOD                              = 0x6c,
225     WINED3D_SM4_OP_GATHER4                          = 0x6d,
226     WINED3D_SM4_OP_SAMPLE_POS                       = 0x6e,
227     WINED3D_SM4_OP_SAMPLE_INFO                      = 0x6f,
228     WINED3D_SM5_OP_HS_DECLS                         = 0x71,
229     WINED3D_SM5_OP_HS_CONTROL_POINT_PHASE           = 0x72,
230     WINED3D_SM5_OP_HS_FORK_PHASE                    = 0x73,
231     WINED3D_SM5_OP_HS_JOIN_PHASE                    = 0x74,
232     WINED3D_SM5_OP_EMIT_STREAM                      = 0x75,
233     WINED3D_SM5_OP_CUT_STREAM                       = 0x76,
234     WINED3D_SM5_OP_FCALL                            = 0x78,
235     WINED3D_SM5_OP_BUFINFO                          = 0x79,
236     WINED3D_SM5_OP_DERIV_RTX_COARSE                 = 0x7a,
237     WINED3D_SM5_OP_DERIV_RTX_FINE                   = 0x7b,
238     WINED3D_SM5_OP_DERIV_RTY_COARSE                 = 0x7c,
239     WINED3D_SM5_OP_DERIV_RTY_FINE                   = 0x7d,
240     WINED3D_SM5_OP_GATHER4_C                        = 0x7e,
241     WINED3D_SM5_OP_GATHER4_PO                       = 0x7f,
242     WINED3D_SM5_OP_GATHER4_PO_C                     = 0x80,
243     WINED3D_SM5_OP_RCP                              = 0x81,
244     WINED3D_SM5_OP_F32TOF16                         = 0x82,
245     WINED3D_SM5_OP_F16TOF32                         = 0x83,
246     WINED3D_SM5_OP_COUNTBITS                        = 0x86,
247     WINED3D_SM5_OP_FIRSTBIT_HI                      = 0x87,
248     WINED3D_SM5_OP_FIRSTBIT_LO                      = 0x88,
249     WINED3D_SM5_OP_FIRSTBIT_SHI                     = 0x89,
250     WINED3D_SM5_OP_UBFE                             = 0x8a,
251     WINED3D_SM5_OP_IBFE                             = 0x8b,
252     WINED3D_SM5_OP_BFI                              = 0x8c,
253     WINED3D_SM5_OP_BFREV                            = 0x8d,
254     WINED3D_SM5_OP_SWAPC                            = 0x8e,
255     WINED3D_SM5_OP_DCL_STREAM                       = 0x8f,
256     WINED3D_SM5_OP_DCL_FUNCTION_BODY                = 0x90,
257     WINED3D_SM5_OP_DCL_FUNCTION_TABLE               = 0x91,
258     WINED3D_SM5_OP_DCL_INTERFACE                    = 0x92,
259     WINED3D_SM5_OP_DCL_INPUT_CONTROL_POINT_COUNT    = 0x93,
260     WINED3D_SM5_OP_DCL_OUTPUT_CONTROL_POINT_COUNT   = 0x94,
261     WINED3D_SM5_OP_DCL_TESSELLATOR_DOMAIN           = 0x95,
262     WINED3D_SM5_OP_DCL_TESSELLATOR_PARTITIONING     = 0x96,
263     WINED3D_SM5_OP_DCL_TESSELLATOR_OUTPUT_PRIMITIVE = 0x97,
264     WINED3D_SM5_OP_DCL_HS_MAX_TESSFACTOR            = 0x98,
265     WINED3D_SM5_OP_DCL_HS_FORK_PHASE_INSTANCE_COUNT = 0x99,
266     WINED3D_SM5_OP_DCL_HS_JOIN_PHASE_INSTANCE_COUNT = 0x9a,
267     WINED3D_SM5_OP_DCL_THREAD_GROUP                 = 0x9b,
268     WINED3D_SM5_OP_DCL_UAV_TYPED                    = 0x9c,
269     WINED3D_SM5_OP_DCL_UAV_RAW                      = 0x9d,
270     WINED3D_SM5_OP_DCL_UAV_STRUCTURED               = 0x9e,
271     WINED3D_SM5_OP_DCL_TGSM_RAW                     = 0x9f,
272     WINED3D_SM5_OP_DCL_TGSM_STRUCTURED              = 0xa0,
273     WINED3D_SM5_OP_DCL_RESOURCE_RAW                 = 0xa1,
274     WINED3D_SM5_OP_DCL_RESOURCE_STRUCTURED          = 0xa2,
275     WINED3D_SM5_OP_LD_UAV_TYPED                     = 0xa3,
276     WINED3D_SM5_OP_STORE_UAV_TYPED                  = 0xa4,
277     WINED3D_SM5_OP_LD_RAW                           = 0xa5,
278     WINED3D_SM5_OP_STORE_RAW                        = 0xa6,
279     WINED3D_SM5_OP_LD_STRUCTURED                    = 0xa7,
280     WINED3D_SM5_OP_STORE_STRUCTURED                 = 0xa8,
281     WINED3D_SM5_OP_ATOMIC_AND                       = 0xa9,
282     WINED3D_SM5_OP_ATOMIC_OR                        = 0xaa,
283     WINED3D_SM5_OP_ATOMIC_XOR                       = 0xab,
284     WINED3D_SM5_OP_ATOMIC_CMP_STORE                 = 0xac,
285     WINED3D_SM5_OP_ATOMIC_IADD                      = 0xad,
286     WINED3D_SM5_OP_ATOMIC_IMAX                      = 0xae,
287     WINED3D_SM5_OP_ATOMIC_IMIN                      = 0xaf,
288     WINED3D_SM5_OP_ATOMIC_UMAX                      = 0xb0,
289     WINED3D_SM5_OP_ATOMIC_UMIN                      = 0xb1,
290     WINED3D_SM5_OP_IMM_ATOMIC_ALLOC                 = 0xb2,
291     WINED3D_SM5_OP_IMM_ATOMIC_CONSUME               = 0xb3,
292     WINED3D_SM5_OP_IMM_ATOMIC_IADD                  = 0xb4,
293     WINED3D_SM5_OP_IMM_ATOMIC_AND                   = 0xb5,
294     WINED3D_SM5_OP_IMM_ATOMIC_OR                    = 0xb6,
295     WINED3D_SM5_OP_IMM_ATOMIC_XOR                   = 0xb7,
296     WINED3D_SM5_OP_IMM_ATOMIC_EXCH                  = 0xb8,
297     WINED3D_SM5_OP_IMM_ATOMIC_CMP_EXCH              = 0xb9,
298     WINED3D_SM5_OP_IMM_ATOMIC_IMAX                  = 0xba,
299     WINED3D_SM5_OP_IMM_ATOMIC_IMIN                  = 0xbb,
300     WINED3D_SM5_OP_IMM_ATOMIC_UMAX                  = 0xbc,
301     WINED3D_SM5_OP_IMM_ATOMIC_UMIN                  = 0xbd,
302     WINED3D_SM5_OP_SYNC                             = 0xbe,
303     WINED3D_SM5_OP_EVAL_SAMPLE_INDEX                = 0xcc,
304     WINED3D_SM5_OP_DCL_GS_INSTANCES                 = 0xce,
305 };
306 
307 enum wined3d_sm4_register_type
308 {
309     WINED3D_SM4_RT_TEMP                    = 0x00,
310     WINED3D_SM4_RT_INPUT                   = 0x01,
311     WINED3D_SM4_RT_OUTPUT                  = 0x02,
312     WINED3D_SM4_RT_INDEXABLE_TEMP          = 0x03,
313     WINED3D_SM4_RT_IMMCONST                = 0x04,
314     WINED3D_SM4_RT_SAMPLER                 = 0x06,
315     WINED3D_SM4_RT_RESOURCE                = 0x07,
316     WINED3D_SM4_RT_CONSTBUFFER             = 0x08,
317     WINED3D_SM4_RT_IMMCONSTBUFFER          = 0x09,
318     WINED3D_SM4_RT_PRIMID                  = 0x0b,
319     WINED3D_SM4_RT_DEPTHOUT                = 0x0c,
320     WINED3D_SM4_RT_NULL                    = 0x0d,
321     WINED3D_SM4_RT_RASTERIZER              = 0x0e,
322     WINED3D_SM4_RT_OMASK                   = 0x0f,
323     WINED3D_SM5_RT_STREAM                  = 0x10,
324     WINED3D_SM5_RT_FUNCTION_BODY           = 0x11,
325     WINED3D_SM5_RT_FUNCTION_POINTER        = 0x13,
326     WINED3D_SM5_RT_OUTPUT_CONTROL_POINT_ID = 0x16,
327     WINED3D_SM5_RT_FORK_INSTANCE_ID        = 0x17,
328     WINED3D_SM5_RT_JOIN_INSTANCE_ID        = 0x18,
329     WINED3D_SM5_RT_INPUT_CONTROL_POINT     = 0x19,
330     WINED3D_SM5_RT_OUTPUT_CONTROL_POINT    = 0x1a,
331     WINED3D_SM5_RT_PATCH_CONSTANT_DATA     = 0x1b,
332     WINED3D_SM5_RT_DOMAIN_LOCATION         = 0x1c,
333     WINED3D_SM5_RT_UAV                     = 0x1e,
334     WINED3D_SM5_RT_SHARED_MEMORY           = 0x1f,
335     WINED3D_SM5_RT_THREAD_ID               = 0x20,
336     WINED3D_SM5_RT_THREAD_GROUP_ID         = 0x21,
337     WINED3D_SM5_RT_LOCAL_THREAD_ID         = 0x22,
338     WINED3D_SM5_RT_COVERAGE                = 0x23,
339     WINED3D_SM5_RT_LOCAL_THREAD_INDEX      = 0x24,
340     WINED3D_SM5_RT_GS_INSTANCE_ID          = 0x25,
341     WINED3D_SM5_RT_DEPTHOUT_GREATER_EQUAL  = 0x26,
342     WINED3D_SM5_RT_DEPTHOUT_LESS_EQUAL     = 0x27,
343 };
344 
345 enum wined3d_sm4_output_primitive_type
346 {
347     WINED3D_SM4_OUTPUT_PT_POINTLIST     = 0x1,
348     WINED3D_SM4_OUTPUT_PT_LINESTRIP     = 0x3,
349     WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP = 0x5,
350 };
351 
352 enum wined3d_sm4_input_primitive_type
353 {
354     WINED3D_SM4_INPUT_PT_POINT          = 0x01,
355     WINED3D_SM4_INPUT_PT_LINE           = 0x02,
356     WINED3D_SM4_INPUT_PT_TRIANGLE       = 0x03,
357     WINED3D_SM4_INPUT_PT_LINEADJ        = 0x06,
358     WINED3D_SM4_INPUT_PT_TRIANGLEADJ    = 0x07,
359     WINED3D_SM5_INPUT_PT_PATCH1         = 0x08,
360     WINED3D_SM5_INPUT_PT_PATCH2         = 0x09,
361     WINED3D_SM5_INPUT_PT_PATCH3         = 0x0a,
362     WINED3D_SM5_INPUT_PT_PATCH4         = 0x0b,
363     WINED3D_SM5_INPUT_PT_PATCH5         = 0x0c,
364     WINED3D_SM5_INPUT_PT_PATCH6         = 0x0d,
365     WINED3D_SM5_INPUT_PT_PATCH7         = 0x0e,
366     WINED3D_SM5_INPUT_PT_PATCH8         = 0x0f,
367     WINED3D_SM5_INPUT_PT_PATCH9         = 0x10,
368     WINED3D_SM5_INPUT_PT_PATCH10        = 0x11,
369     WINED3D_SM5_INPUT_PT_PATCH11        = 0x12,
370     WINED3D_SM5_INPUT_PT_PATCH12        = 0x13,
371     WINED3D_SM5_INPUT_PT_PATCH13        = 0x14,
372     WINED3D_SM5_INPUT_PT_PATCH14        = 0x15,
373     WINED3D_SM5_INPUT_PT_PATCH15        = 0x16,
374     WINED3D_SM5_INPUT_PT_PATCH16        = 0x17,
375     WINED3D_SM5_INPUT_PT_PATCH17        = 0x18,
376     WINED3D_SM5_INPUT_PT_PATCH18        = 0x19,
377     WINED3D_SM5_INPUT_PT_PATCH19        = 0x1a,
378     WINED3D_SM5_INPUT_PT_PATCH20        = 0x1b,
379     WINED3D_SM5_INPUT_PT_PATCH21        = 0x1c,
380     WINED3D_SM5_INPUT_PT_PATCH22        = 0x1d,
381     WINED3D_SM5_INPUT_PT_PATCH23        = 0x1e,
382     WINED3D_SM5_INPUT_PT_PATCH24        = 0x1f,
383     WINED3D_SM5_INPUT_PT_PATCH25        = 0x20,
384     WINED3D_SM5_INPUT_PT_PATCH26        = 0x21,
385     WINED3D_SM5_INPUT_PT_PATCH27        = 0x22,
386     WINED3D_SM5_INPUT_PT_PATCH28        = 0x23,
387     WINED3D_SM5_INPUT_PT_PATCH29        = 0x24,
388     WINED3D_SM5_INPUT_PT_PATCH30        = 0x25,
389     WINED3D_SM5_INPUT_PT_PATCH31        = 0x26,
390     WINED3D_SM5_INPUT_PT_PATCH32        = 0x27,
391 };
392 
393 enum wined3d_sm4_swizzle_type
394 {
395     WINED3D_SM4_SWIZZLE_NONE            = 0x0,
396     WINED3D_SM4_SWIZZLE_VEC4            = 0x1,
397     WINED3D_SM4_SWIZZLE_SCALAR          = 0x2,
398 };
399 
400 enum wined3d_sm4_immconst_type
401 {
402     WINED3D_SM4_IMMCONST_SCALAR = 0x1,
403     WINED3D_SM4_IMMCONST_VEC4   = 0x2,
404 };
405 
406 enum wined3d_sm4_resource_type
407 {
408     WINED3D_SM4_RESOURCE_BUFFER             = 0x1,
409     WINED3D_SM4_RESOURCE_TEXTURE_1D         = 0x2,
410     WINED3D_SM4_RESOURCE_TEXTURE_2D         = 0x3,
411     WINED3D_SM4_RESOURCE_TEXTURE_2DMS       = 0x4,
412     WINED3D_SM4_RESOURCE_TEXTURE_3D         = 0x5,
413     WINED3D_SM4_RESOURCE_TEXTURE_CUBE       = 0x6,
414     WINED3D_SM4_RESOURCE_TEXTURE_1DARRAY    = 0x7,
415     WINED3D_SM4_RESOURCE_TEXTURE_2DARRAY    = 0x8,
416     WINED3D_SM4_RESOURCE_TEXTURE_2DMSARRAY  = 0x9,
417     WINED3D_SM4_RESOURCE_TEXTURE_CUBEARRAY  = 0xa,
418 };
419 
420 enum wined3d_sm4_data_type
421 {
422     WINED3D_SM4_DATA_UNORM  = 0x1,
423     WINED3D_SM4_DATA_SNORM  = 0x2,
424     WINED3D_SM4_DATA_INT    = 0x3,
425     WINED3D_SM4_DATA_UINT   = 0x4,
426     WINED3D_SM4_DATA_FLOAT  = 0x5,
427 };
428 
429 enum wined3d_sm4_sampler_mode
430 {
431     WINED3D_SM4_SAMPLER_DEFAULT    = 0x0,
432     WINED3D_SM4_SAMPLER_COMPARISON = 0x1,
433 };
434 
435 enum wined3d_sm4_shader_data_type
436 {
437     WINED3D_SM4_SHADER_DATA_IMMEDIATE_CONSTANT_BUFFER = 0x3,
438     WINED3D_SM4_SHADER_DATA_MESSAGE                   = 0x4,
439 };
440 
441 struct wined3d_shader_src_param_entry
442 {
443     struct list entry;
444     struct wined3d_shader_src_param param;
445 };
446 
447 struct wined3d_sm4_data
448 {
449     struct wined3d_shader_version shader_version;
450     const DWORD *start, *end;
451 
452     unsigned int output_map[MAX_REG_OUTPUT];
453 
454     struct wined3d_shader_src_param src_param[5];
455     struct wined3d_shader_dst_param dst_param[2];
456     struct list src_free;
457     struct list src;
458     struct wined3d_shader_immediate_constant_buffer icb;
459 };
460 
461 struct wined3d_sm4_opcode_info
462 {
463     enum wined3d_sm4_opcode opcode;
464     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
465     const char *dst_info;
466     const char *src_info;
467     void (*read_opcode_func)(struct wined3d_shader_instruction *ins,
468             DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
469             struct wined3d_sm4_data *priv);
470 };
471 
472 static const enum wined3d_primitive_type output_primitive_type_table[] =
473 {
474     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
475     /* WINED3D_SM4_OUTPUT_PT_POINTLIST */       WINED3D_PT_POINTLIST,
476     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
477     /* WINED3D_SM4_OUTPUT_PT_LINESTRIP */       WINED3D_PT_LINESTRIP,
478     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
479     /* WINED3D_SM4_OUTPUT_PT_TRIANGLESTRIP */   WINED3D_PT_TRIANGLESTRIP,
480 };
481 
482 static const enum wined3d_primitive_type input_primitive_type_table[] =
483 {
484     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
485     /* WINED3D_SM4_INPUT_PT_POINT */            WINED3D_PT_POINTLIST,
486     /* WINED3D_SM4_INPUT_PT_LINE */             WINED3D_PT_LINELIST,
487     /* WINED3D_SM4_INPUT_PT_TRIANGLE */         WINED3D_PT_TRIANGLELIST,
488     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
489     /* UNKNOWN */                               WINED3D_PT_UNDEFINED,
490     /* WINED3D_SM4_INPUT_PT_LINEADJ */          WINED3D_PT_LINELIST_ADJ,
491     /* WINED3D_SM4_INPUT_PT_TRIANGLEADJ */      WINED3D_PT_TRIANGLELIST_ADJ,
492 };
493 
494 static const enum wined3d_shader_resource_type resource_type_table[] =
495 {
496     /* 0 */                                         WINED3D_SHADER_RESOURCE_NONE,
497     /* WINED3D_SM4_RESOURCE_BUFFER */               WINED3D_SHADER_RESOURCE_BUFFER,
498     /* WINED3D_SM4_RESOURCE_TEXTURE_1D */           WINED3D_SHADER_RESOURCE_TEXTURE_1D,
499     /* WINED3D_SM4_RESOURCE_TEXTURE_2D */           WINED3D_SHADER_RESOURCE_TEXTURE_2D,
500     /* WINED3D_SM4_RESOURCE_TEXTURE_2DMS */         WINED3D_SHADER_RESOURCE_TEXTURE_2DMS,
501     /* WINED3D_SM4_RESOURCE_TEXTURE_3D */           WINED3D_SHADER_RESOURCE_TEXTURE_3D,
502     /* WINED3D_SM4_RESOURCE_TEXTURE_CUBE */         WINED3D_SHADER_RESOURCE_TEXTURE_CUBE,
503     /* WINED3D_SM4_RESOURCE_TEXTURE_1DARRAY */      WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY,
504     /* WINED3D_SM4_RESOURCE_TEXTURE_2DARRAY */      WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY,
505     /* WINED3D_SM4_RESOURCE_TEXTURE_2DMSARRAY */    WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY,
506     /* WINED3D_SM4_RESOURCE_TEXTURE_CUBEARRAY */    WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY,
507 };
508 
509 static const enum wined3d_data_type data_type_table[] =
510 {
511     /* 0 */                         WINED3D_DATA_FLOAT,
512     /* WINED3D_SM4_DATA_UNORM */    WINED3D_DATA_UNORM,
513     /* WINED3D_SM4_DATA_SNORM */    WINED3D_DATA_SNORM,
514     /* WINED3D_SM4_DATA_INT */      WINED3D_DATA_INT,
515     /* WINED3D_SM4_DATA_UINT */     WINED3D_DATA_UINT,
516     /* WINED3D_SM4_DATA_FLOAT */    WINED3D_DATA_FLOAT,
517 };
518 
519 static BOOL shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr, const DWORD *end,
520         enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param);
521 static BOOL shader_sm4_read_dst_param(struct wined3d_sm4_data *priv, const DWORD **ptr, const DWORD *end,
522         enum wined3d_data_type data_type, struct wined3d_shader_dst_param *dst_param);
523 
524 static void shader_sm4_read_conditional_op(struct wined3d_shader_instruction *ins,
525         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
526         struct wined3d_sm4_data *priv)
527 {
528     shader_sm4_read_src_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_UINT, &priv->src_param[0]);
529     ins->flags = (opcode_token & WINED3D_SM4_CONDITIONAL_NZ) ?
530             WINED3D_SHADER_CONDITIONAL_OP_NZ : WINED3D_SHADER_CONDITIONAL_OP_Z;
531 }
532 
533 static void shader_sm4_read_shader_data(struct wined3d_shader_instruction *ins,
534         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
535         struct wined3d_sm4_data *priv)
536 {
537     enum wined3d_sm4_shader_data_type type;
538     unsigned int icb_size;
539 
540     type = (opcode_token & WINED3D_SM4_SHADER_DATA_TYPE_MASK) >> WINED3D_SM4_SHADER_DATA_TYPE_SHIFT;
541     if (type != WINED3D_SM4_SHADER_DATA_IMMEDIATE_CONSTANT_BUFFER)
542     {
543         FIXME("Ignoring shader data type %#x.\n", type);
544         ins->handler_idx = WINED3DSIH_NOP;
545         return;
546     }
547 
548     ++tokens;
549     icb_size = token_count - 1;
550     if (icb_size % 4 || icb_size > MAX_IMMEDIATE_CONSTANT_BUFFER_SIZE)
551     {
552         FIXME("Unexpected immediate constant buffer size %u.\n", icb_size);
553         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
554         return;
555     }
556 
557     priv->icb.vec4_count = icb_size / 4;
558     memcpy(priv->icb.data, tokens, sizeof(*tokens) * icb_size);
559     ins->declaration.icb = &priv->icb;
560 }
561 
562 static void shader_sm4_read_dcl_resource(struct wined3d_shader_instruction *ins,
563         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
564         struct wined3d_sm4_data *priv)
565 {
566     enum wined3d_sm4_resource_type resource_type;
567     enum wined3d_sm4_data_type data_type;
568     enum wined3d_data_type reg_data_type;
569     DWORD components;
570 
571     resource_type = (opcode_token & WINED3D_SM4_RESOURCE_TYPE_MASK) >> WINED3D_SM4_RESOURCE_TYPE_SHIFT;
572     if (!resource_type || (resource_type >= ARRAY_SIZE(resource_type_table)))
573     {
574         FIXME("Unhandled resource type %#x.\n", resource_type);
575         ins->declaration.semantic.resource_type = WINED3D_SHADER_RESOURCE_NONE;
576     }
577     else
578     {
579         ins->declaration.semantic.resource_type = resource_type_table[resource_type];
580     }
581     reg_data_type = opcode == WINED3D_SM4_OP_DCL_RESOURCE ? WINED3D_DATA_RESOURCE : WINED3D_DATA_UAV;
582     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], reg_data_type, &ins->declaration.semantic.reg);
583 
584     components = *tokens++;
585     if ((components & 0xfff0) != (components & 0xf) * 0x1110)
586         FIXME("Components (%#x) have different data types.\n", components);
587     data_type = components & 0xf;
588 
589     if (!data_type || (data_type >= ARRAY_SIZE(data_type_table)))
590     {
591         FIXME("Unhandled data type %#x.\n", data_type);
592         ins->declaration.semantic.resource_data_type = WINED3D_DATA_FLOAT;
593     }
594     else
595     {
596         ins->declaration.semantic.resource_data_type = data_type_table[data_type];
597     }
598 
599     if (reg_data_type == WINED3D_DATA_UAV)
600         ins->flags = (opcode_token & WINED3D_SM5_UAV_FLAGS_MASK) >> WINED3D_SM5_UAV_FLAGS_SHIFT;
601 }
602 
603 static void shader_sm4_read_dcl_constant_buffer(struct wined3d_shader_instruction *ins,
604         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
605         struct wined3d_sm4_data *priv)
606 {
607     shader_sm4_read_src_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT, &ins->declaration.src);
608     if (opcode_token & WINED3D_SM4_INDEX_TYPE_MASK)
609         ins->flags |= WINED3DSI_INDEXED_DYNAMIC;
610 }
611 
612 static void shader_sm4_read_dcl_sampler(struct wined3d_shader_instruction *ins,
613         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
614         struct wined3d_sm4_data *priv)
615 {
616     ins->flags = (opcode_token & WINED3D_SM4_SAMPLER_MODE_MASK) >> WINED3D_SM4_SAMPLER_MODE_SHIFT;
617     if (ins->flags & ~WINED3D_SM4_SAMPLER_COMPARISON)
618         FIXME("Unhandled sampler mode %#x.\n", ins->flags);
619     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_SAMPLER, &ins->declaration.dst);
620 }
621 
622 static void shader_sm4_read_dcl_index_range(struct wined3d_shader_instruction *ins,
623         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
624         struct wined3d_sm4_data *priv)
625 {
626     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_OPAQUE,
627             &ins->declaration.index_range.first_register);
628     ins->declaration.index_range.last_register = *tokens;
629 }
630 
631 static void shader_sm4_read_dcl_output_topology(struct wined3d_shader_instruction *ins,
632         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
633         struct wined3d_sm4_data *priv)
634 {
635     enum wined3d_sm4_output_primitive_type primitive_type;
636 
637     primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
638     if (primitive_type >= ARRAY_SIZE(output_primitive_type_table))
639         ins->declaration.primitive_type.type = WINED3D_PT_UNDEFINED;
640     else
641         ins->declaration.primitive_type.type = output_primitive_type_table[primitive_type];
642 
643     if (ins->declaration.primitive_type.type == WINED3D_PT_UNDEFINED)
644         FIXME("Unhandled output primitive type %#x.\n", primitive_type);
645 }
646 
647 static void shader_sm4_read_dcl_input_primitive(struct wined3d_shader_instruction *ins,
648         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
649         struct wined3d_sm4_data *priv)
650 {
651     enum wined3d_sm4_input_primitive_type primitive_type;
652 
653     primitive_type = (opcode_token & WINED3D_SM4_PRIMITIVE_TYPE_MASK) >> WINED3D_SM4_PRIMITIVE_TYPE_SHIFT;
654     if (WINED3D_SM5_INPUT_PT_PATCH1 <= primitive_type && primitive_type <= WINED3D_SM5_INPUT_PT_PATCH32)
655     {
656         ins->declaration.primitive_type.type = WINED3D_PT_PATCH;
657         ins->declaration.primitive_type.patch_vertex_count = primitive_type - WINED3D_SM5_INPUT_PT_PATCH1 + 1;
658     }
659     else if (primitive_type >= ARRAY_SIZE(input_primitive_type_table))
660     {
661         ins->declaration.primitive_type.type = WINED3D_PT_UNDEFINED;
662     }
663     else
664     {
665         ins->declaration.primitive_type.type = input_primitive_type_table[primitive_type];
666     }
667 
668     if (ins->declaration.primitive_type.type == WINED3D_PT_UNDEFINED)
669         FIXME("Unhandled input primitive type %#x.\n", primitive_type);
670 }
671 
672 static void shader_sm4_read_declaration_count(struct wined3d_shader_instruction *ins,
673         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
674         struct wined3d_sm4_data *priv)
675 {
676     ins->declaration.count = *tokens;
677 }
678 
679 static void shader_sm4_read_declaration_dst(struct wined3d_shader_instruction *ins,
680         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
681         struct wined3d_sm4_data *priv)
682 {
683     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT, &ins->declaration.dst);
684 }
685 
686 static void shader_sm4_read_declaration_register_semantic(struct wined3d_shader_instruction *ins,
687         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
688         struct wined3d_sm4_data *priv)
689 {
690     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT,
691             &ins->declaration.register_semantic.reg);
692     ins->declaration.register_semantic.sysval_semantic = *tokens;
693 }
694 
695 static void shader_sm4_read_dcl_input_ps(struct wined3d_shader_instruction *ins,
696         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
697         struct wined3d_sm4_data *priv)
698 {
699     ins->flags = (opcode_token & WINED3D_SM4_INTERPOLATION_MODE_MASK) >> WINED3D_SM4_INTERPOLATION_MODE_SHIFT;
700     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT, &ins->declaration.dst);
701 }
702 
703 static void shader_sm4_read_dcl_input_ps_siv(struct wined3d_shader_instruction *ins,
704         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
705         struct wined3d_sm4_data *priv)
706 {
707     ins->flags = (opcode_token & WINED3D_SM4_INTERPOLATION_MODE_MASK) >> WINED3D_SM4_INTERPOLATION_MODE_SHIFT;
708     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT,
709             &ins->declaration.register_semantic.reg);
710     ins->declaration.register_semantic.sysval_semantic = *tokens;
711 }
712 
713 static void shader_sm4_read_dcl_indexable_temp(struct wined3d_shader_instruction *ins,
714         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
715         struct wined3d_sm4_data *priv)
716 {
717     ins->declaration.indexable_temp.register_idx = *tokens++;
718     ins->declaration.indexable_temp.register_size = *tokens++;
719     ins->declaration.indexable_temp.component_count = *tokens;
720 }
721 
722 static void shader_sm4_read_dcl_global_flags(struct wined3d_shader_instruction *ins,
723         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
724         struct wined3d_sm4_data *priv)
725 {
726     ins->flags = (opcode_token & WINED3D_SM4_GLOBAL_FLAGS_MASK) >> WINED3D_SM4_GLOBAL_FLAGS_SHIFT;
727 }
728 
729 static void shader_sm5_read_fcall(struct wined3d_shader_instruction *ins,
730         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
731         struct wined3d_sm4_data *priv)
732 {
733     priv->src_param[0].reg.u.fp_body_idx = *tokens++;
734     shader_sm4_read_src_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_OPAQUE, &priv->src_param[0]);
735 }
736 
737 static void shader_sm5_read_dcl_function_body(struct wined3d_shader_instruction *ins,
738         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
739         struct wined3d_sm4_data *priv)
740 {
741     ins->declaration.index = *tokens;
742 }
743 
744 static void shader_sm5_read_dcl_function_table(struct wined3d_shader_instruction *ins,
745         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
746         struct wined3d_sm4_data *priv)
747 {
748     ins->declaration.index = *tokens++;
749     FIXME("Ignoring set of function bodies (count %u).\n", *tokens);
750 }
751 
752 static void shader_sm5_read_dcl_interface(struct wined3d_shader_instruction *ins,
753         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
754         struct wined3d_sm4_data *priv)
755 {
756     ins->declaration.fp.index = *tokens++;
757     ins->declaration.fp.body_count = *tokens++;
758     ins->declaration.fp.array_size = *tokens >> WINED3D_SM5_FP_ARRAY_SIZE_SHIFT;
759     ins->declaration.fp.table_count = *tokens++ & WINED3D_SM5_FP_TABLE_COUNT_MASK;
760     FIXME("Ignoring set of function tables (count %u).\n", ins->declaration.fp.table_count);
761 }
762 
763 static void shader_sm5_read_control_point_count(struct wined3d_shader_instruction *ins,
764         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
765         struct wined3d_sm4_data *priv)
766 {
767     ins->declaration.count = (opcode_token & WINED3D_SM5_CONTROL_POINT_COUNT_MASK)
768             >> WINED3D_SM5_CONTROL_POINT_COUNT_SHIFT;
769 }
770 
771 static void shader_sm5_read_dcl_tessellator_domain(struct wined3d_shader_instruction *ins,
772         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
773         struct wined3d_sm4_data *priv)
774 {
775     ins->declaration.tessellator_domain = (opcode_token & WINED3D_SM5_TESSELLATOR_MASK)
776         >> WINED3D_SM5_TESSELLATOR_SHIFT;
777 }
778 
779 static void shader_sm5_read_dcl_tessellator_partitioning(struct wined3d_shader_instruction *ins,
780         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
781         struct wined3d_sm4_data *priv)
782 {
783     ins->declaration.tessellator_partitioning = (opcode_token & WINED3D_SM5_TESSELLATOR_MASK)
784             >> WINED3D_SM5_TESSELLATOR_SHIFT;
785 }
786 
787 static void shader_sm5_read_dcl_tessellator_output_primitive(struct wined3d_shader_instruction *ins,
788         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
789         struct wined3d_sm4_data *priv)
790 {
791     ins->declaration.tessellator_output_primitive = (opcode_token & WINED3D_SM5_TESSELLATOR_MASK)
792             >> WINED3D_SM5_TESSELLATOR_SHIFT;
793 }
794 
795 static void shader_sm5_read_dcl_hs_max_tessfactor(struct wined3d_shader_instruction *ins,
796         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
797         struct wined3d_sm4_data *priv)
798 {
799     ins->declaration.max_tessellation_factor = *(float *)tokens;
800 }
801 
802 static void shader_sm5_read_dcl_thread_group(struct wined3d_shader_instruction *ins,
803         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
804         struct wined3d_sm4_data *priv)
805 {
806     ins->declaration.thread_group_size.x = *tokens++;
807     ins->declaration.thread_group_size.y = *tokens++;
808     ins->declaration.thread_group_size.z = *tokens++;
809 }
810 
811 static void shader_sm5_read_dcl_uav_raw(struct wined3d_shader_instruction *ins,
812         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
813         struct wined3d_sm4_data *priv)
814 {
815     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_UAV, &ins->declaration.dst);
816     ins->flags = (opcode_token & WINED3D_SM5_UAV_FLAGS_MASK) >> WINED3D_SM5_UAV_FLAGS_SHIFT;
817 }
818 
819 static void shader_sm5_read_dcl_uav_structured(struct wined3d_shader_instruction *ins,
820         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
821         struct wined3d_sm4_data *priv)
822 {
823     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_UAV,
824             &ins->declaration.structured_resource.reg);
825     ins->flags = (opcode_token & WINED3D_SM5_UAV_FLAGS_MASK) >> WINED3D_SM5_UAV_FLAGS_SHIFT;
826     ins->declaration.structured_resource.byte_stride = *tokens;
827     if (ins->declaration.structured_resource.byte_stride % 4)
828         FIXME("Byte stride %u is not multiple of 4.\n", ins->declaration.structured_resource.byte_stride);
829 }
830 
831 static void shader_sm5_read_dcl_tgsm_raw(struct wined3d_shader_instruction *ins,
832         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
833         struct wined3d_sm4_data *priv)
834 {
835     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT, &ins->declaration.tgsm_raw.reg);
836     ins->declaration.tgsm_raw.byte_count = *tokens;
837     if (ins->declaration.tgsm_raw.byte_count % 4)
838         FIXME("Byte count %u is not multiple of 4.\n", ins->declaration.tgsm_raw.byte_count);
839 }
840 
841 static void shader_sm5_read_dcl_tgsm_structured(struct wined3d_shader_instruction *ins,
842         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
843         struct wined3d_sm4_data *priv)
844 {
845     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_FLOAT,
846             &ins->declaration.tgsm_structured.reg);
847     ins->declaration.tgsm_structured.byte_stride = *tokens++;
848     ins->declaration.tgsm_structured.structure_count = *tokens;
849     if (ins->declaration.tgsm_structured.byte_stride % 4)
850         FIXME("Byte stride %u is not multiple of 4.\n", ins->declaration.tgsm_structured.byte_stride);
851 }
852 
853 static void shader_sm5_read_dcl_resource_structured(struct wined3d_shader_instruction *ins,
854         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
855         struct wined3d_sm4_data *priv)
856 {
857     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_RESOURCE,
858             &ins->declaration.structured_resource.reg);
859     ins->declaration.structured_resource.byte_stride = *tokens;
860     if (ins->declaration.structured_resource.byte_stride % 4)
861         FIXME("Byte stride %u is not multiple of 4.\n", ins->declaration.structured_resource.byte_stride);
862 }
863 
864 static void shader_sm5_read_dcl_resource_raw(struct wined3d_shader_instruction *ins,
865         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
866         struct wined3d_sm4_data *priv)
867 {
868     shader_sm4_read_dst_param(priv, &tokens, &tokens[token_count], WINED3D_DATA_RESOURCE, &ins->declaration.dst);
869 }
870 
871 static void shader_sm5_read_sync(struct wined3d_shader_instruction *ins,
872         DWORD opcode, DWORD opcode_token, const DWORD *tokens, unsigned int token_count,
873         struct wined3d_sm4_data *priv)
874 {
875     ins->flags = (opcode_token & WINED3D_SM5_SYNC_FLAGS_MASK) >> WINED3D_SM5_SYNC_FLAGS_SHIFT;
876 }
877 
878 /*
879  * f -> WINED3D_DATA_FLOAT
880  * i -> WINED3D_DATA_INT
881  * u -> WINED3D_DATA_UINT
882  * O -> WINED3D_DATA_OPAQUE
883  * R -> WINED3D_DATA_RESOURCE
884  * S -> WINED3D_DATA_SAMPLER
885  * U -> WINED3D_DATA_UAV
886  */
887 static const struct wined3d_sm4_opcode_info opcode_table[] =
888 {
889     {WINED3D_SM4_OP_ADD,                              WINED3DSIH_ADD,                              "f",    "ff"},
890     {WINED3D_SM4_OP_AND,                              WINED3DSIH_AND,                              "u",    "uu"},
891     {WINED3D_SM4_OP_BREAK,                            WINED3DSIH_BREAK,                            "",     ""},
892     {WINED3D_SM4_OP_BREAKC,                           WINED3DSIH_BREAKP,                           "",     "u",
893             shader_sm4_read_conditional_op},
894     {WINED3D_SM4_OP_CASE,                             WINED3DSIH_CASE,                             "",     "u"},
895     {WINED3D_SM4_OP_CONTINUE,                         WINED3DSIH_CONTINUE,                         "",     ""},
896     {WINED3D_SM4_OP_CONTINUEC,                        WINED3DSIH_CONTINUEP,                        "",     "u",
897             shader_sm4_read_conditional_op},
898     {WINED3D_SM4_OP_CUT,                              WINED3DSIH_CUT,                              "",     ""},
899     {WINED3D_SM4_OP_DEFAULT,                          WINED3DSIH_DEFAULT,                          "",     ""},
900     {WINED3D_SM4_OP_DERIV_RTX,                        WINED3DSIH_DSX,                              "f",    "f"},
901     {WINED3D_SM4_OP_DERIV_RTY,                        WINED3DSIH_DSY,                              "f",    "f"},
902     {WINED3D_SM4_OP_DISCARD,                          WINED3DSIH_TEXKILL,                          "",     "u",
903             shader_sm4_read_conditional_op},
904     {WINED3D_SM4_OP_DIV,                              WINED3DSIH_DIV,                              "f",    "ff"},
905     {WINED3D_SM4_OP_DP2,                              WINED3DSIH_DP2,                              "f",    "ff"},
906     {WINED3D_SM4_OP_DP3,                              WINED3DSIH_DP3,                              "f",    "ff"},
907     {WINED3D_SM4_OP_DP4,                              WINED3DSIH_DP4,                              "f",    "ff"},
908     {WINED3D_SM4_OP_ELSE,                             WINED3DSIH_ELSE,                             "",     ""},
909     {WINED3D_SM4_OP_EMIT,                             WINED3DSIH_EMIT,                             "",     ""},
910     {WINED3D_SM4_OP_ENDIF,                            WINED3DSIH_ENDIF,                            "",     ""},
911     {WINED3D_SM4_OP_ENDLOOP,                          WINED3DSIH_ENDLOOP,                          "",     ""},
912     {WINED3D_SM4_OP_ENDSWITCH,                        WINED3DSIH_ENDSWITCH,                        "",     ""},
913     {WINED3D_SM4_OP_EQ,                               WINED3DSIH_EQ,                               "u",    "ff"},
914     {WINED3D_SM4_OP_EXP,                              WINED3DSIH_EXP,                              "f",    "f"},
915     {WINED3D_SM4_OP_FRC,                              WINED3DSIH_FRC,                              "f",    "f"},
916     {WINED3D_SM4_OP_FTOI,                             WINED3DSIH_FTOI,                             "i",    "f"},
917     {WINED3D_SM4_OP_FTOU,                             WINED3DSIH_FTOU,                             "u",    "f"},
918     {WINED3D_SM4_OP_GE,                               WINED3DSIH_GE,                               "u",    "ff"},
919     {WINED3D_SM4_OP_IADD,                             WINED3DSIH_IADD,                             "i",    "ii"},
920     {WINED3D_SM4_OP_IF,                               WINED3DSIH_IF,                               "",     "u",
921             shader_sm4_read_conditional_op},
922     {WINED3D_SM4_OP_IEQ,                              WINED3DSIH_IEQ,                              "u",    "ii"},
923     {WINED3D_SM4_OP_IGE,                              WINED3DSIH_IGE,                              "u",    "ii"},
924     {WINED3D_SM4_OP_ILT,                              WINED3DSIH_ILT,                              "u",    "ii"},
925     {WINED3D_SM4_OP_IMAD,                             WINED3DSIH_IMAD,                             "i",    "iii"},
926     {WINED3D_SM4_OP_IMAX,                             WINED3DSIH_IMAX,                             "i",    "ii"},
927     {WINED3D_SM4_OP_IMIN,                             WINED3DSIH_IMIN,                             "i",    "ii"},
928     {WINED3D_SM4_OP_IMUL,                             WINED3DSIH_IMUL,                             "ii",   "ii"},
929     {WINED3D_SM4_OP_INE,                              WINED3DSIH_INE,                              "u",    "ii"},
930     {WINED3D_SM4_OP_INEG,                             WINED3DSIH_INEG,                             "i",    "i"},
931     {WINED3D_SM4_OP_ISHL,                             WINED3DSIH_ISHL,                             "i",    "ii"},
932     {WINED3D_SM4_OP_ISHR,                             WINED3DSIH_ISHR,                             "i",    "ii"},
933     {WINED3D_SM4_OP_ITOF,                             WINED3DSIH_ITOF,                             "f",    "i"},
934     {WINED3D_SM4_OP_LABEL,                            WINED3DSIH_LABEL,                            "",     "O"},
935     {WINED3D_SM4_OP_LD,                               WINED3DSIH_LD,                               "u",    "iR"},
936     {WINED3D_SM4_OP_LD2DMS,                           WINED3DSIH_LD2DMS,                           "u",    "iRi"},
937     {WINED3D_SM4_OP_LOG,                              WINED3DSIH_LOG,                              "f",    "f"},
938     {WINED3D_SM4_OP_LOOP,                             WINED3DSIH_LOOP,                             "",     ""},
939     {WINED3D_SM4_OP_LT,                               WINED3DSIH_LT,                               "u",    "ff"},
940     {WINED3D_SM4_OP_MAD,                              WINED3DSIH_MAD,                              "f",    "fff"},
941     {WINED3D_SM4_OP_MIN,                              WINED3DSIH_MIN,                              "f",    "ff"},
942     {WINED3D_SM4_OP_MAX,                              WINED3DSIH_MAX,                              "f",    "ff"},
943     {WINED3D_SM4_OP_SHADER_DATA,                      WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER,    "",     "",
944             shader_sm4_read_shader_data},
945     {WINED3D_SM4_OP_MOV,                              WINED3DSIH_MOV,                              "f",    "f"},
946     {WINED3D_SM4_OP_MOVC,                             WINED3DSIH_MOVC,                             "f",    "uff"},
947     {WINED3D_SM4_OP_MUL,                              WINED3DSIH_MUL,                              "f",    "ff"},
948     {WINED3D_SM4_OP_NE,                               WINED3DSIH_NE,                               "u",    "ff"},
949     {WINED3D_SM4_OP_NOP,                              WINED3DSIH_NOP,                              "",     ""},
950     {WINED3D_SM4_OP_NOT,                              WINED3DSIH_NOT,                              "u",    "u"},
951     {WINED3D_SM4_OP_OR,                               WINED3DSIH_OR,                               "u",    "uu"},
952     {WINED3D_SM4_OP_RESINFO,                          WINED3DSIH_RESINFO,                          "f",    "iR"},
953     {WINED3D_SM4_OP_RET,                              WINED3DSIH_RET,                              "",     ""},
954     {WINED3D_SM4_OP_RETC,                             WINED3DSIH_RETP,                             "",     "u",
955             shader_sm4_read_conditional_op},
956     {WINED3D_SM4_OP_ROUND_NE,                         WINED3DSIH_ROUND_NE,                         "f",    "f"},
957     {WINED3D_SM4_OP_ROUND_NI,                         WINED3DSIH_ROUND_NI,                         "f",    "f"},
958     {WINED3D_SM4_OP_ROUND_PI,                         WINED3DSIH_ROUND_PI,                         "f",    "f"},
959     {WINED3D_SM4_OP_ROUND_Z,                          WINED3DSIH_ROUND_Z,                          "f",    "f"},
960     {WINED3D_SM4_OP_RSQ,                              WINED3DSIH_RSQ,                              "f",    "f"},
961     {WINED3D_SM4_OP_SAMPLE,                           WINED3DSIH_SAMPLE,                           "u",    "fRS"},
962     {WINED3D_SM4_OP_SAMPLE_C,                         WINED3DSIH_SAMPLE_C,                         "f",    "fRSf"},
963     {WINED3D_SM4_OP_SAMPLE_C_LZ,                      WINED3DSIH_SAMPLE_C_LZ,                      "f",    "fRSf"},
964     {WINED3D_SM4_OP_SAMPLE_LOD,                       WINED3DSIH_SAMPLE_LOD,                       "u",    "fRSf"},
965     {WINED3D_SM4_OP_SAMPLE_GRAD,                      WINED3DSIH_SAMPLE_GRAD,                      "u",    "fRSff"},
966     {WINED3D_SM4_OP_SAMPLE_B,                         WINED3DSIH_SAMPLE_B,                         "u",    "fRSf"},
967     {WINED3D_SM4_OP_SQRT,                             WINED3DSIH_SQRT,                             "f",    "f"},
968     {WINED3D_SM4_OP_SWITCH,                           WINED3DSIH_SWITCH,                           "",     "u"},
969     {WINED3D_SM4_OP_SINCOS,                           WINED3DSIH_SINCOS,                           "ff",   "f"},
970     {WINED3D_SM4_OP_UDIV,                             WINED3DSIH_UDIV,                             "uu",   "uu"},
971     {WINED3D_SM4_OP_ULT,                              WINED3DSIH_ULT,                              "u",    "uu"},
972     {WINED3D_SM4_OP_UGE,                              WINED3DSIH_UGE,                              "u",    "uu"},
973     {WINED3D_SM4_OP_UMUL,                             WINED3DSIH_UMUL,                             "uu",   "uu"},
974     {WINED3D_SM4_OP_UMAX,                             WINED3DSIH_UMAX,                             "u",    "uu"},
975     {WINED3D_SM4_OP_UMIN,                             WINED3DSIH_UMIN,                             "u",    "uu"},
976     {WINED3D_SM4_OP_USHR,                             WINED3DSIH_USHR,                             "u",    "uu"},
977     {WINED3D_SM4_OP_UTOF,                             WINED3DSIH_UTOF,                             "f",    "u"},
978     {WINED3D_SM4_OP_XOR,                              WINED3DSIH_XOR,                              "u",    "uu"},
979     {WINED3D_SM4_OP_DCL_RESOURCE,                     WINED3DSIH_DCL,                              "R",    "",
980             shader_sm4_read_dcl_resource},
981     {WINED3D_SM4_OP_DCL_CONSTANT_BUFFER,              WINED3DSIH_DCL_CONSTANT_BUFFER,              "",     "",
982             shader_sm4_read_dcl_constant_buffer},
983     {WINED3D_SM4_OP_DCL_SAMPLER,                      WINED3DSIH_DCL_SAMPLER,                      "",     "",
984             shader_sm4_read_dcl_sampler},
985     {WINED3D_SM4_OP_DCL_INDEX_RANGE,                  WINED3DSIH_DCL_INDEX_RANGE,                  "",     "",
986             shader_sm4_read_dcl_index_range},
987     {WINED3D_SM4_OP_DCL_OUTPUT_TOPOLOGY,              WINED3DSIH_DCL_OUTPUT_TOPOLOGY,              "",     "",
988             shader_sm4_read_dcl_output_topology},
989     {WINED3D_SM4_OP_DCL_INPUT_PRIMITIVE,              WINED3DSIH_DCL_INPUT_PRIMITIVE,              "",     "",
990             shader_sm4_read_dcl_input_primitive},
991     {WINED3D_SM4_OP_DCL_VERTICES_OUT,                 WINED3DSIH_DCL_VERTICES_OUT,                 "",     "",
992             shader_sm4_read_declaration_count},
993     {WINED3D_SM4_OP_DCL_INPUT,                        WINED3DSIH_DCL_INPUT,                        "",     "",
994             shader_sm4_read_declaration_dst},
995     {WINED3D_SM4_OP_DCL_INPUT_SGV,                    WINED3DSIH_DCL_INPUT_SGV,                    "",     "",
996             shader_sm4_read_declaration_register_semantic},
997     {WINED3D_SM4_OP_DCL_INPUT_SIV,                    WINED3DSIH_DCL_INPUT_SIV,                    "",     "",
998             shader_sm4_read_declaration_register_semantic},
999     {WINED3D_SM4_OP_DCL_INPUT_PS,                     WINED3DSIH_DCL_INPUT_PS,                     "",     "",
1000             shader_sm4_read_dcl_input_ps},
1001     {WINED3D_SM4_OP_DCL_INPUT_PS_SGV,                 WINED3DSIH_DCL_INPUT_PS_SGV,                 "",     "",
1002             shader_sm4_read_declaration_register_semantic},
1003     {WINED3D_SM4_OP_DCL_INPUT_PS_SIV,                 WINED3DSIH_DCL_INPUT_PS_SIV,                 "",     "",
1004             shader_sm4_read_dcl_input_ps_siv},
1005     {WINED3D_SM4_OP_DCL_OUTPUT,                       WINED3DSIH_DCL_OUTPUT,                       "",     "",
1006             shader_sm4_read_declaration_dst},
1007     {WINED3D_SM4_OP_DCL_OUTPUT_SIV,                   WINED3DSIH_DCL_OUTPUT_SIV,                   "",     "",
1008             shader_sm4_read_declaration_register_semantic},
1009     {WINED3D_SM4_OP_DCL_TEMPS,                        WINED3DSIH_DCL_TEMPS,                        "",     "",
1010             shader_sm4_read_declaration_count},
1011     {WINED3D_SM4_OP_DCL_INDEXABLE_TEMP,               WINED3DSIH_DCL_INDEXABLE_TEMP,               "",     "",
1012             shader_sm4_read_dcl_indexable_temp},
1013     {WINED3D_SM4_OP_DCL_GLOBAL_FLAGS,                 WINED3DSIH_DCL_GLOBAL_FLAGS,                 "",     "",
1014             shader_sm4_read_dcl_global_flags},
1015     {WINED3D_SM4_OP_LOD,                              WINED3DSIH_LOD,                              "f",    "fRS"},
1016     {WINED3D_SM4_OP_GATHER4,                          WINED3DSIH_GATHER4,                          "u",    "fRS"},
1017     {WINED3D_SM4_OP_SAMPLE_POS,                       WINED3DSIH_SAMPLE_POS,                       "f",    "Ru"},
1018     {WINED3D_SM4_OP_SAMPLE_INFO,                      WINED3DSIH_SAMPLE_INFO,                      "f",    "R"},
1019     {WINED3D_SM5_OP_HS_DECLS,                         WINED3DSIH_HS_DECLS,                         "",     ""},
1020     {WINED3D_SM5_OP_HS_CONTROL_POINT_PHASE,           WINED3DSIH_HS_CONTROL_POINT_PHASE,           "",     ""},
1021     {WINED3D_SM5_OP_HS_FORK_PHASE,                    WINED3DSIH_HS_FORK_PHASE,                    "",     ""},
1022     {WINED3D_SM5_OP_HS_JOIN_PHASE,                    WINED3DSIH_HS_JOIN_PHASE,                    "",     ""},
1023     {WINED3D_SM5_OP_EMIT_STREAM,                      WINED3DSIH_EMIT_STREAM,                      "",     "f"},
1024     {WINED3D_SM5_OP_CUT_STREAM,                       WINED3DSIH_CUT_STREAM,                       "",     "f"},
1025     {WINED3D_SM5_OP_FCALL,                            WINED3DSIH_FCALL,                            "",     "O",
1026             shader_sm5_read_fcall},
1027     {WINED3D_SM5_OP_BUFINFO,                          WINED3DSIH_BUFINFO,                          "i",    "U"},
1028     {WINED3D_SM5_OP_DERIV_RTX_COARSE,                 WINED3DSIH_DSX_COARSE,                       "f",    "f"},
1029     {WINED3D_SM5_OP_DERIV_RTX_FINE,                   WINED3DSIH_DSX_FINE,                         "f",    "f"},
1030     {WINED3D_SM5_OP_DERIV_RTY_COARSE,                 WINED3DSIH_DSY_COARSE,                       "f",    "f"},
1031     {WINED3D_SM5_OP_DERIV_RTY_FINE,                   WINED3DSIH_DSY_FINE,                         "f",    "f"},
1032     {WINED3D_SM5_OP_GATHER4_C,                        WINED3DSIH_GATHER4_C,                        "f",    "fRSf"},
1033     {WINED3D_SM5_OP_GATHER4_PO,                       WINED3DSIH_GATHER4_PO,                       "f",    "fiRS"},
1034     {WINED3D_SM5_OP_GATHER4_PO_C,                     WINED3DSIH_GATHER4_PO_C,                     "f",    "fiRSf"},
1035     {WINED3D_SM5_OP_RCP,                              WINED3DSIH_RCP,                              "f",    "f"},
1036     {WINED3D_SM5_OP_F32TOF16,                         WINED3DSIH_F32TOF16,                         "u",    "f"},
1037     {WINED3D_SM5_OP_F16TOF32,                         WINED3DSIH_F16TOF32,                         "f",    "u"},
1038     {WINED3D_SM5_OP_COUNTBITS,                        WINED3DSIH_COUNTBITS,                        "u",    "u"},
1039     {WINED3D_SM5_OP_FIRSTBIT_HI,                      WINED3DSIH_FIRSTBIT_HI,                      "u",    "u"},
1040     {WINED3D_SM5_OP_FIRSTBIT_LO,                      WINED3DSIH_FIRSTBIT_LO,                      "u",    "u"},
1041     {WINED3D_SM5_OP_FIRSTBIT_SHI,                     WINED3DSIH_FIRSTBIT_SHI,                     "u",    "i"},
1042     {WINED3D_SM5_OP_UBFE,                             WINED3DSIH_UBFE,                             "u",    "iiu"},
1043     {WINED3D_SM5_OP_IBFE,                             WINED3DSIH_IBFE,                             "i",    "iii"},
1044     {WINED3D_SM5_OP_BFI,                              WINED3DSIH_BFI,                              "u",    "iiuu"},
1045     {WINED3D_SM5_OP_BFREV,                            WINED3DSIH_BFREV,                            "u",    "u"},
1046     {WINED3D_SM5_OP_SWAPC,                            WINED3DSIH_SWAPC,                            "ff",   "uff"},
1047     {WINED3D_SM5_OP_DCL_STREAM,                       WINED3DSIH_DCL_STREAM,                       "",     "O"},
1048     {WINED3D_SM5_OP_DCL_FUNCTION_BODY,                WINED3DSIH_DCL_FUNCTION_BODY,                "",     "",
1049             shader_sm5_read_dcl_function_body},
1050     {WINED3D_SM5_OP_DCL_FUNCTION_TABLE,               WINED3DSIH_DCL_FUNCTION_TABLE,               "",     "",
1051             shader_sm5_read_dcl_function_table},
1052     {WINED3D_SM5_OP_DCL_INTERFACE,                    WINED3DSIH_DCL_INTERFACE,                    "",     "",
1053             shader_sm5_read_dcl_interface},
1054     {WINED3D_SM5_OP_DCL_INPUT_CONTROL_POINT_COUNT,    WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT,    "",     "",
1055             shader_sm5_read_control_point_count},
1056     {WINED3D_SM5_OP_DCL_OUTPUT_CONTROL_POINT_COUNT,   WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT,   "",     "",
1057             shader_sm5_read_control_point_count},
1058     {WINED3D_SM5_OP_DCL_TESSELLATOR_DOMAIN,           WINED3DSIH_DCL_TESSELLATOR_DOMAIN,           "",     "",
1059             shader_sm5_read_dcl_tessellator_domain},
1060     {WINED3D_SM5_OP_DCL_TESSELLATOR_PARTITIONING,     WINED3DSIH_DCL_TESSELLATOR_PARTITIONING,     "",     "",
1061             shader_sm5_read_dcl_tessellator_partitioning},
1062     {WINED3D_SM5_OP_DCL_TESSELLATOR_OUTPUT_PRIMITIVE, WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE, "",     "",
1063             shader_sm5_read_dcl_tessellator_output_primitive},
1064     {WINED3D_SM5_OP_DCL_HS_MAX_TESSFACTOR,            WINED3DSIH_DCL_HS_MAX_TESSFACTOR,            "",     "",
1065             shader_sm5_read_dcl_hs_max_tessfactor},
1066     {WINED3D_SM5_OP_DCL_HS_FORK_PHASE_INSTANCE_COUNT, WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT, "",     "",
1067             shader_sm4_read_declaration_count},
1068     {WINED3D_SM5_OP_DCL_HS_JOIN_PHASE_INSTANCE_COUNT, WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT, "",     "",
1069             shader_sm4_read_declaration_count},
1070     {WINED3D_SM5_OP_DCL_THREAD_GROUP,                 WINED3DSIH_DCL_THREAD_GROUP,                 "",     "",
1071             shader_sm5_read_dcl_thread_group},
1072     {WINED3D_SM5_OP_DCL_UAV_TYPED,                    WINED3DSIH_DCL_UAV_TYPED,                    "",     "",
1073             shader_sm4_read_dcl_resource},
1074     {WINED3D_SM5_OP_DCL_UAV_RAW,                      WINED3DSIH_DCL_UAV_RAW,                      "",     "",
1075             shader_sm5_read_dcl_uav_raw},
1076     {WINED3D_SM5_OP_DCL_UAV_STRUCTURED,               WINED3DSIH_DCL_UAV_STRUCTURED,               "",     "",
1077             shader_sm5_read_dcl_uav_structured},
1078     {WINED3D_SM5_OP_DCL_TGSM_RAW,                     WINED3DSIH_DCL_TGSM_RAW,                     "",     "",
1079             shader_sm5_read_dcl_tgsm_raw},
1080     {WINED3D_SM5_OP_DCL_TGSM_STRUCTURED,              WINED3DSIH_DCL_TGSM_STRUCTURED,              "",     "",
1081             shader_sm5_read_dcl_tgsm_structured},
1082     {WINED3D_SM5_OP_DCL_RESOURCE_RAW,                 WINED3DSIH_DCL_RESOURCE_RAW,                 "",     "",
1083             shader_sm5_read_dcl_resource_raw},
1084     {WINED3D_SM5_OP_DCL_RESOURCE_STRUCTURED,          WINED3DSIH_DCL_RESOURCE_STRUCTURED,          "",     "",
1085             shader_sm5_read_dcl_resource_structured},
1086     {WINED3D_SM5_OP_LD_UAV_TYPED,                     WINED3DSIH_LD_UAV_TYPED,                     "u",    "iU"},
1087     {WINED3D_SM5_OP_STORE_UAV_TYPED,                  WINED3DSIH_STORE_UAV_TYPED,                  "U",    "iu"},
1088     {WINED3D_SM5_OP_LD_RAW,                           WINED3DSIH_LD_RAW,                           "u",    "iU"},
1089     {WINED3D_SM5_OP_STORE_RAW,                        WINED3DSIH_STORE_RAW,                        "U",    "iu"},
1090     {WINED3D_SM5_OP_LD_STRUCTURED,                    WINED3DSIH_LD_STRUCTURED,                    "u",    "iiR"},
1091     {WINED3D_SM5_OP_STORE_STRUCTURED,                 WINED3DSIH_STORE_STRUCTURED,                 "U",    "iiu"},
1092     {WINED3D_SM5_OP_ATOMIC_AND,                       WINED3DSIH_ATOMIC_AND,                       "U",    "iu"},
1093     {WINED3D_SM5_OP_ATOMIC_OR,                        WINED3DSIH_ATOMIC_OR,                        "U",    "iu"},
1094     {WINED3D_SM5_OP_ATOMIC_XOR,                       WINED3DSIH_ATOMIC_XOR,                       "U",    "iu"},
1095     {WINED3D_SM5_OP_ATOMIC_CMP_STORE,                 WINED3DSIH_ATOMIC_CMP_STORE,                 "U",    "iuu"},
1096     {WINED3D_SM5_OP_ATOMIC_IADD,                      WINED3DSIH_ATOMIC_IADD,                      "U",    "ii"},
1097     {WINED3D_SM5_OP_ATOMIC_IMAX,                      WINED3DSIH_ATOMIC_IMAX,                      "U",    "ii"},
1098     {WINED3D_SM5_OP_ATOMIC_IMIN,                      WINED3DSIH_ATOMIC_IMIN,                      "U",    "ii"},
1099     {WINED3D_SM5_OP_ATOMIC_UMAX,                      WINED3DSIH_ATOMIC_UMAX,                      "U",    "iu"},
1100     {WINED3D_SM5_OP_ATOMIC_UMIN,                      WINED3DSIH_ATOMIC_UMIN,                      "U",    "iu"},
1101     {WINED3D_SM5_OP_IMM_ATOMIC_ALLOC,                 WINED3DSIH_IMM_ATOMIC_ALLOC,                 "u",    "U"},
1102     {WINED3D_SM5_OP_IMM_ATOMIC_CONSUME,               WINED3DSIH_IMM_ATOMIC_CONSUME,               "u",    "U"},
1103     {WINED3D_SM5_OP_IMM_ATOMIC_IADD,                  WINED3DSIH_IMM_ATOMIC_IADD,                  "uU",   "ii"},
1104     {WINED3D_SM5_OP_IMM_ATOMIC_AND,                   WINED3DSIH_IMM_ATOMIC_AND,                   "uU",   "iu"},
1105     {WINED3D_SM5_OP_IMM_ATOMIC_OR,                    WINED3DSIH_IMM_ATOMIC_OR,                    "uU",   "iu"},
1106     {WINED3D_SM5_OP_IMM_ATOMIC_XOR,                   WINED3DSIH_IMM_ATOMIC_XOR,                   "uU",   "iu"},
1107     {WINED3D_SM5_OP_IMM_ATOMIC_EXCH,                  WINED3DSIH_IMM_ATOMIC_EXCH,                  "uU",   "iu"},
1108     {WINED3D_SM5_OP_IMM_ATOMIC_CMP_EXCH,              WINED3DSIH_IMM_ATOMIC_CMP_EXCH,              "uU",   "iuu"},
1109     {WINED3D_SM5_OP_IMM_ATOMIC_IMAX,                  WINED3DSIH_IMM_ATOMIC_IMAX,                  "iU",   "ii"},
1110     {WINED3D_SM5_OP_IMM_ATOMIC_IMIN,                  WINED3DSIH_IMM_ATOMIC_IMIN,                  "iU",   "ii"},
1111     {WINED3D_SM5_OP_IMM_ATOMIC_UMAX,                  WINED3DSIH_IMM_ATOMIC_UMAX,                  "uU",   "iu"},
1112     {WINED3D_SM5_OP_IMM_ATOMIC_UMIN,                  WINED3DSIH_IMM_ATOMIC_UMIN,                  "uU",   "iu"},
1113     {WINED3D_SM5_OP_SYNC,                             WINED3DSIH_SYNC,                             "",     "",
1114             shader_sm5_read_sync},
1115     {WINED3D_SM5_OP_EVAL_SAMPLE_INDEX,                WINED3DSIH_EVAL_SAMPLE_INDEX,                "f",    "fi"},
1116     {WINED3D_SM5_OP_DCL_GS_INSTANCES,                 WINED3DSIH_DCL_GS_INSTANCES,                 "",     "",
1117             shader_sm4_read_declaration_count},
1118 };
1119 
1120 static const enum wined3d_shader_register_type register_type_table[] =
1121 {
1122     /* WINED3D_SM4_RT_TEMP */                    WINED3DSPR_TEMP,
1123     /* WINED3D_SM4_RT_INPUT */                   WINED3DSPR_INPUT,
1124     /* WINED3D_SM4_RT_OUTPUT */                  WINED3DSPR_OUTPUT,
1125     /* WINED3D_SM4_RT_INDEXABLE_TEMP */          WINED3DSPR_IDXTEMP,
1126     /* WINED3D_SM4_RT_IMMCONST */                WINED3DSPR_IMMCONST,
1127     /* UNKNOWN */                                ~0u,
1128     /* WINED3D_SM4_RT_SAMPLER */                 WINED3DSPR_SAMPLER,
1129     /* WINED3D_SM4_RT_RESOURCE */                WINED3DSPR_RESOURCE,
1130     /* WINED3D_SM4_RT_CONSTBUFFER */             WINED3DSPR_CONSTBUFFER,
1131     /* WINED3D_SM4_RT_IMMCONSTBUFFER */          WINED3DSPR_IMMCONSTBUFFER,
1132     /* UNKNOWN */                                ~0u,
1133     /* WINED3D_SM4_RT_PRIMID */                  WINED3DSPR_PRIMID,
1134     /* WINED3D_SM4_RT_DEPTHOUT */                WINED3DSPR_DEPTHOUT,
1135     /* WINED3D_SM4_RT_NULL */                    WINED3DSPR_NULL,
1136     /* WINED3D_SM4_RT_RASTERIZER */              WINED3DSPR_RASTERIZER,
1137     /* WINED3D_SM4_RT_OMASK */                   WINED3DSPR_SAMPLEMASK,
1138     /* WINED3D_SM5_RT_STREAM */                  WINED3DSPR_STREAM,
1139     /* WINED3D_SM5_RT_FUNCTION_BODY */           WINED3DSPR_FUNCTIONBODY,
1140     /* UNKNOWN */                                ~0u,
1141     /* WINED3D_SM5_RT_FUNCTION_POINTER */        WINED3DSPR_FUNCTIONPOINTER,
1142     /* UNKNOWN */                                ~0u,
1143     /* UNKNOWN */                                ~0u,
1144     /* WINED3D_SM5_RT_OUTPUT_CONTROL_POINT_ID */ WINED3DSPR_OUTPOINTID,
1145     /* WINED3D_SM5_RT_FORK_INSTANCE_ID */        WINED3DSPR_FORKINSTID,
1146     /* WINED3D_SM5_RT_JOIN_INSTANCE_ID */        WINED3DSPR_JOININSTID,
1147     /* WINED3D_SM5_RT_INPUT_CONTROL_POINT */     WINED3DSPR_INCONTROLPOINT,
1148     /* WINED3D_SM5_RT_OUTPUT_CONTROL_POINT */    WINED3DSPR_OUTCONTROLPOINT,
1149     /* WINED3D_SM5_RT_PATCH_CONSTANT_DATA */     WINED3DSPR_PATCHCONST,
1150     /* WINED3D_SM5_RT_DOMAIN_LOCATION */         WINED3DSPR_TESSCOORD,
1151     /* UNKNOWN */                                ~0u,
1152     /* WINED3D_SM5_RT_UAV */                     WINED3DSPR_UAV,
1153     /* WINED3D_SM5_RT_SHARED_MEMORY */           WINED3DSPR_GROUPSHAREDMEM,
1154     /* WINED3D_SM5_RT_THREAD_ID */               WINED3DSPR_THREADID,
1155     /* WINED3D_SM5_RT_THREAD_GROUP_ID */         WINED3DSPR_THREADGROUPID,
1156     /* WINED3D_SM5_RT_LOCAL_THREAD_ID */         WINED3DSPR_LOCALTHREADID,
1157     /* WINED3D_SM5_RT_COVERAGE */                WINED3DSPR_COVERAGE,
1158     /* WINED3D_SM5_RT_LOCAL_THREAD_INDEX */      WINED3DSPR_LOCALTHREADINDEX,
1159     /* WINED3D_SM5_RT_GS_INSTANCE_ID */          WINED3DSPR_GSINSTID,
1160     /* WINED3D_SM5_RT_DEPTHOUT_GREATER_EQUAL */  WINED3DSPR_DEPTHOUTGE,
1161     /* WINED3D_SM5_RT_DEPTHOUT_LESS_EQUAL */     WINED3DSPR_DEPTHOUTLE,
1162 };
1163 
1164 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
1165 {
1166     unsigned int i;
1167 
1168     for (i = 0; i < ARRAY_SIZE(opcode_table); ++i)
1169     {
1170         if (opcode == opcode_table[i].opcode) return &opcode_table[i];
1171     }
1172 
1173     return NULL;
1174 }
1175 
1176 static void map_register(const struct wined3d_sm4_data *priv, struct wined3d_shader_register *reg)
1177 {
1178     switch (priv->shader_version.type)
1179     {
1180         case WINED3D_SHADER_TYPE_PIXEL:
1181             if (reg->type == WINED3DSPR_OUTPUT)
1182             {
1183                 unsigned int reg_idx = reg->idx[0].offset;
1184 
1185                 if (reg_idx >= ARRAY_SIZE(priv->output_map))
1186                 {
1187                     ERR("Invalid output index %u.\n", reg_idx);
1188                     break;
1189                 }
1190 
1191                 reg->type = WINED3DSPR_COLOROUT;
1192                 reg->idx[0].offset = priv->output_map[reg_idx];
1193             }
1194             break;
1195 
1196         default:
1197             break;
1198     }
1199 }
1200 
1201 static enum wined3d_data_type map_data_type(char t)
1202 {
1203     switch (t)
1204     {
1205         case 'f':
1206             return WINED3D_DATA_FLOAT;
1207         case 'i':
1208             return WINED3D_DATA_INT;
1209         case 'u':
1210             return WINED3D_DATA_UINT;
1211         case 'O':
1212             return WINED3D_DATA_OPAQUE;
1213         case 'R':
1214             return WINED3D_DATA_RESOURCE;
1215         case 'S':
1216             return WINED3D_DATA_SAMPLER;
1217         case 'U':
1218             return WINED3D_DATA_UAV;
1219         default:
1220             ERR("Invalid data type '%c'.\n", t);
1221             return WINED3D_DATA_FLOAT;
1222     }
1223 }
1224 
1225 enum wined3d_shader_type wined3d_get_sm4_shader_type(const DWORD *byte_code, size_t byte_code_size)
1226 {
1227     DWORD shader_type;
1228 
1229     if (byte_code_size / sizeof(*byte_code) < 1)
1230     {
1231         WARN("Invalid byte code size %lu.\n", (long)byte_code_size);
1232         return WINED3D_SHADER_TYPE_INVALID;
1233     }
1234 
1235     shader_type = byte_code[0] >> 16;
1236     switch (shader_type)
1237     {
1238         case WINED3D_SM4_PS:
1239             return WINED3D_SHADER_TYPE_PIXEL;
1240             break;
1241         case WINED3D_SM4_VS:
1242             return WINED3D_SHADER_TYPE_VERTEX;
1243             break;
1244         case WINED3D_SM4_GS:
1245             return WINED3D_SHADER_TYPE_GEOMETRY;
1246             break;
1247         case WINED3D_SM5_HS:
1248             return WINED3D_SHADER_TYPE_HULL;
1249             break;
1250         case WINED3D_SM5_DS:
1251             return WINED3D_SHADER_TYPE_DOMAIN;
1252             break;
1253         case WINED3D_SM5_CS:
1254             return WINED3D_SHADER_TYPE_COMPUTE;
1255             break;
1256         default:
1257             FIXME("Unrecognised shader type %#x.\n", shader_type);
1258             return WINED3D_SHADER_TYPE_INVALID;
1259     }
1260 }
1261 
1262 static void *shader_sm4_init(const DWORD *byte_code, size_t byte_code_size,
1263         const struct wined3d_shader_signature *output_signature)
1264 {
1265     DWORD version_token, token_count;
1266     struct wined3d_sm4_data *priv;
1267     unsigned int i;
1268 
1269     if (byte_code_size / sizeof(*byte_code) < 2)
1270     {
1271         WARN("Invalid byte code size %lu.\n", (long)byte_code_size);
1272         return NULL;
1273     }
1274 
1275     version_token = byte_code[0];
1276     TRACE("Version: 0x%08x.\n", version_token);
1277     token_count = byte_code[1];
1278     TRACE("Token count: %u.\n", token_count);
1279 
1280     if (token_count < 2 || byte_code_size / sizeof(*byte_code) < token_count)
1281     {
1282         WARN("Invalid token count %u.\n", token_count);
1283         return NULL;
1284     }
1285 
1286     if (!(priv = heap_alloc(sizeof(*priv))))
1287     {
1288         ERR("Failed to allocate private data\n");
1289         return NULL;
1290     }
1291 
1292     priv->start = &byte_code[2];
1293     priv->end = &byte_code[token_count];
1294 
1295     priv->shader_version.type = wined3d_get_sm4_shader_type(byte_code, byte_code_size);
1296     if (priv->shader_version.type == WINED3D_SHADER_TYPE_INVALID)
1297     {
1298         heap_free(priv);
1299         return NULL;
1300     }
1301 
1302     priv->shader_version.major = WINED3D_SM4_VERSION_MAJOR(version_token);
1303     priv->shader_version.minor = WINED3D_SM4_VERSION_MINOR(version_token);
1304 
1305     memset(priv->output_map, 0xff, sizeof(priv->output_map));
1306     for (i = 0; i < output_signature->element_count; ++i)
1307     {
1308         struct wined3d_shader_signature_element *e = &output_signature->elements[i];
1309 
1310         if (e->register_idx >= ARRAY_SIZE(priv->output_map))
1311         {
1312             WARN("Invalid output index %u.\n", e->register_idx);
1313             continue;
1314         }
1315 
1316         priv->output_map[e->register_idx] = e->semantic_idx;
1317     }
1318 
1319     list_init(&priv->src_free);
1320     list_init(&priv->src);
1321 
1322     return priv;
1323 }
1324 
1325 static void shader_sm4_free(void *data)
1326 {
1327     struct wined3d_shader_src_param_entry *e1, *e2;
1328     struct wined3d_sm4_data *priv = data;
1329 
1330     list_move_head(&priv->src_free, &priv->src);
1331     LIST_FOR_EACH_ENTRY_SAFE(e1, e2, &priv->src_free, struct wined3d_shader_src_param_entry, entry)
1332     {
1333         heap_free(e1);
1334     }
1335     heap_free(priv);
1336 }
1337 
1338 static struct wined3d_shader_src_param *get_src_param(struct wined3d_sm4_data *priv)
1339 {
1340     struct wined3d_shader_src_param_entry *e;
1341     struct list *elem;
1342 
1343     if (!list_empty(&priv->src_free))
1344     {
1345         elem = list_head(&priv->src_free);
1346         list_remove(elem);
1347     }
1348     else
1349     {
1350         if (!(e = heap_alloc(sizeof(*e))))
1351             return NULL;
1352         elem = &e->entry;
1353     }
1354 
1355     list_add_tail(&priv->src, elem);
1356     e = LIST_ENTRY(elem, struct wined3d_shader_src_param_entry, entry);
1357     return &e->param;
1358 }
1359 
1360 static void shader_sm4_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
1361 {
1362     struct wined3d_sm4_data *priv = data;
1363 
1364     *ptr = priv->start;
1365     *shader_version = priv->shader_version;
1366 }
1367 
1368 static BOOL shader_sm4_read_reg_idx(struct wined3d_sm4_data *priv, const DWORD **ptr, const DWORD *end,
1369         DWORD addressing, struct wined3d_shader_register_index *reg_idx)
1370 {
1371     if (addressing & WINED3D_SM4_ADDRESSING_RELATIVE)
1372     {
1373         struct wined3d_shader_src_param *rel_addr = get_src_param(priv);
1374 
1375         if (!(reg_idx->rel_addr = rel_addr))
1376         {
1377             ERR("Failed to get src param for relative addressing.\n");
1378             return FALSE;
1379         }
1380 
1381         if (addressing & WINED3D_SM4_ADDRESSING_OFFSET)
1382             reg_idx->offset = *(*ptr)++;
1383         else
1384             reg_idx->offset = 0;
1385         shader_sm4_read_src_param(priv, ptr, end, WINED3D_DATA_INT, rel_addr);
1386     }
1387     else
1388     {
1389         reg_idx->rel_addr = NULL;
1390         reg_idx->offset = *(*ptr)++;
1391     }
1392 
1393     return TRUE;
1394 }
1395 
1396 static BOOL shader_sm4_read_param(struct wined3d_sm4_data *priv, const DWORD **ptr, const DWORD *end,
1397         enum wined3d_data_type data_type, struct wined3d_shader_register *param,
1398         enum wined3d_shader_src_modifier *modifier)
1399 {
1400     enum wined3d_sm4_register_type register_type;
1401     DWORD token, order;
1402 
1403     if (*ptr >= end)
1404     {
1405         WARN("Invalid ptr %p >= end %p.\n", *ptr, end);
1406         return FALSE;
1407     }
1408     token = *(*ptr)++;
1409 
1410     register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
1411     if (register_type >= ARRAY_SIZE(register_type_table)
1412             || register_type_table[register_type] == ~0u)
1413     {
1414         FIXME("Unhandled register type %#x.\n", register_type);
1415         param->type = WINED3DSPR_TEMP;
1416     }
1417     else
1418     {
1419         param->type = register_type_table[register_type];
1420     }
1421     param->data_type = data_type;
1422 
1423     if (token & WINED3D_SM4_REGISTER_MODIFIER)
1424     {
1425         DWORD m;
1426 
1427         if (*ptr >= end)
1428         {
1429             WARN("Invalid ptr %p >= end %p.\n", *ptr, end);
1430             return FALSE;
1431         }
1432         m = *(*ptr)++;
1433 
1434         switch (m)
1435         {
1436             case 0x41:
1437                 *modifier = WINED3DSPSM_NEG;
1438                 break;
1439 
1440             case 0x81:
1441                 *modifier = WINED3DSPSM_ABS;
1442                 break;
1443 
1444             case 0xc1:
1445                 *modifier = WINED3DSPSM_ABSNEG;
1446                 break;
1447 
1448             default:
1449                 FIXME("Skipping modifier 0x%08x.\n", m);
1450                 *modifier = WINED3DSPSM_NONE;
1451                 break;
1452         }
1453     }
1454     else
1455     {
1456         *modifier = WINED3DSPSM_NONE;
1457     }
1458 
1459     order = (token & WINED3D_SM4_REGISTER_ORDER_MASK) >> WINED3D_SM4_REGISTER_ORDER_SHIFT;
1460 
1461     if (order < 1)
1462         param->idx[0].offset = ~0U;
1463     else
1464     {
1465         DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK0) >> WINED3D_SM4_ADDRESSING_SHIFT0;
1466         if (!(shader_sm4_read_reg_idx(priv, ptr, end, addressing, &param->idx[0])))
1467         {
1468             ERR("Failed to read register index.\n");
1469             return FALSE;
1470         }
1471     }
1472 
1473     if (order < 2)
1474         param->idx[1].offset = ~0U;
1475     else
1476     {
1477         DWORD addressing = (token & WINED3D_SM4_ADDRESSING_MASK1) >> WINED3D_SM4_ADDRESSING_SHIFT1;
1478         if (!(shader_sm4_read_reg_idx(priv, ptr, end, addressing, &param->idx[1])))
1479         {
1480             ERR("Failed to read register index.\n");
1481             return FALSE;
1482         }
1483     }
1484 
1485     if (order > 2)
1486         FIXME("Unhandled order %u.\n", order);
1487 
1488     if (register_type == WINED3D_SM4_RT_IMMCONST)
1489     {
1490         enum wined3d_sm4_immconst_type immconst_type =
1491                 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
1492 
1493         switch (immconst_type)
1494         {
1495             case WINED3D_SM4_IMMCONST_SCALAR:
1496                 param->immconst_type = WINED3D_IMMCONST_SCALAR;
1497                 if (end - *ptr < 1)
1498                 {
1499                     WARN("Invalid ptr %p, end %p.\n", *ptr, end);
1500                     return FALSE;
1501                 }
1502                 memcpy(param->u.immconst_data, *ptr, 1 * sizeof(DWORD));
1503                 *ptr += 1;
1504                 break;
1505 
1506             case WINED3D_SM4_IMMCONST_VEC4:
1507                 param->immconst_type = WINED3D_IMMCONST_VEC4;
1508                 if (end - *ptr < 4)
1509                 {
1510                     WARN("Invalid ptr %p, end %p.\n", *ptr, end);
1511                     return FALSE;
1512                 }
1513                 memcpy(param->u.immconst_data, *ptr, 4 * sizeof(DWORD));
1514                 *ptr += 4;
1515                 break;
1516 
1517             default:
1518                 FIXME("Unhandled immediate constant type %#x.\n", immconst_type);
1519                 break;
1520         }
1521     }
1522 
1523     map_register(priv, param);
1524 
1525     return TRUE;
1526 }
1527 
1528 static BOOL shader_sm4_read_src_param(struct wined3d_sm4_data *priv, const DWORD **ptr, const DWORD *end,
1529         enum wined3d_data_type data_type, struct wined3d_shader_src_param *src_param)
1530 {
1531     DWORD token;
1532 
1533     if (*ptr >= end)
1534     {
1535         WARN("Invalid ptr %p >= end %p.\n", *ptr, end);
1536         return FALSE;
1537     }
1538     token = **ptr;
1539 
1540     if (!shader_sm4_read_param(priv, ptr, end, data_type, &src_param->reg, &src_param->modifiers))
1541     {
1542         ERR("Failed to read parameter.\n");
1543         return FALSE;
1544     }
1545 
1546     if (src_param->reg.type == WINED3DSPR_IMMCONST)
1547     {
1548         src_param->swizzle = WINED3DSP_NOSWIZZLE;
1549     }
1550     else
1551     {
1552         enum wined3d_sm4_swizzle_type swizzle_type =
1553                 (token & WINED3D_SM4_SWIZZLE_TYPE_MASK) >> WINED3D_SM4_SWIZZLE_TYPE_SHIFT;
1554 
1555         switch (swizzle_type)
1556         {
1557             case WINED3D_SM4_SWIZZLE_NONE:
1558                 src_param->swizzle = WINED3DSP_NOSWIZZLE;
1559                 break;
1560 
1561             case WINED3D_SM4_SWIZZLE_SCALAR:
1562                 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
1563                 src_param->swizzle = (src_param->swizzle & 0x3) * 0x55;
1564                 break;
1565 
1566             case WINED3D_SM4_SWIZZLE_VEC4:
1567                 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
1568                 break;
1569 
1570             default:
1571                 FIXME("Unhandled swizzle type %#x.\n", swizzle_type);
1572                 break;
1573         }
1574     }
1575 
1576     return TRUE;
1577 }
1578 
1579 static BOOL shader_sm4_read_dst_param(struct wined3d_sm4_data *priv, const DWORD **ptr, const DWORD *end,
1580         enum wined3d_data_type data_type, struct wined3d_shader_dst_param *dst_param)
1581 {
1582     enum wined3d_shader_src_modifier modifier;
1583     DWORD token;
1584 
1585     if (*ptr >= end)
1586     {
1587         WARN("Invalid ptr %p >= end %p.\n", *ptr, end);
1588         return FALSE;
1589     }
1590     token = **ptr;
1591 
1592     if (!shader_sm4_read_param(priv, ptr, end, data_type, &dst_param->reg, &modifier))
1593     {
1594         ERR("Failed to read parameter.\n");
1595         return FALSE;
1596     }
1597 
1598     if (modifier != WINED3DSPSM_NONE)
1599     {
1600         ERR("Invalid source modifier %#x on destination register.\n", modifier);
1601         return FALSE;
1602     }
1603 
1604     dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
1605     dst_param->modifiers = 0;
1606     dst_param->shift = 0;
1607 
1608     return TRUE;
1609 }
1610 
1611 static void shader_sm4_read_instruction_modifier(DWORD modifier, struct wined3d_shader_instruction *ins)
1612 {
1613     static const DWORD recognized_bits = WINED3D_SM4_INSTRUCTION_MODIFIER
1614             | WINED3D_SM4_MODIFIER_AOFFIMMI
1615             | WINED3D_SM4_AOFFIMMI_U_MASK
1616             | WINED3D_SM4_AOFFIMMI_V_MASK
1617             | WINED3D_SM4_AOFFIMMI_W_MASK;
1618 
1619     if (modifier & ~recognized_bits)
1620     {
1621         FIXME("Unhandled modifier 0x%08x.\n", modifier);
1622     }
1623     else
1624     {
1625         /* Bit fields are used for sign extension */
1626         struct
1627         {
1628             int u : 4;
1629             int v : 4;
1630             int w : 4;
1631         }
1632         aoffimmi;
1633         aoffimmi.u = (modifier & WINED3D_SM4_AOFFIMMI_U_MASK) >> WINED3D_SM4_AOFFIMMI_U_SHIFT;
1634         aoffimmi.v = (modifier & WINED3D_SM4_AOFFIMMI_V_MASK) >> WINED3D_SM4_AOFFIMMI_V_SHIFT;
1635         aoffimmi.w = (modifier & WINED3D_SM4_AOFFIMMI_W_MASK) >> WINED3D_SM4_AOFFIMMI_W_SHIFT;
1636         ins->texel_offset.u = aoffimmi.u;
1637         ins->texel_offset.v = aoffimmi.v;
1638         ins->texel_offset.w = aoffimmi.w;
1639     }
1640 }
1641 
1642 static void shader_sm4_read_instruction(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins)
1643 {
1644     const struct wined3d_sm4_opcode_info *opcode_info;
1645     DWORD opcode_token, opcode, previous_token;
1646     struct wined3d_sm4_data *priv = data;
1647     unsigned int i, len;
1648     SIZE_T remaining;
1649     const DWORD *p;
1650     DWORD precise;
1651 
1652     list_move_head(&priv->src_free, &priv->src);
1653 
1654     if (*ptr >= priv->end)
1655     {
1656         WARN("End of byte-code, failed to read opcode.\n");
1657         goto fail;
1658     }
1659     remaining = priv->end - *ptr;
1660 
1661     opcode_token = *(*ptr)++;
1662     opcode = opcode_token & WINED3D_SM4_OPCODE_MASK;
1663 
1664     len = ((opcode_token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT);
1665     if (!len)
1666     {
1667         if (remaining < 2)
1668         {
1669             WARN("End of byte-code, failed to read length token.\n");
1670             goto fail;
1671         }
1672         len = **ptr;
1673     }
1674     if (!len || remaining < len)
1675     {
1676         WARN("Read invalid length %u (remaining %lu).\n", len, remaining);
1677         goto fail;
1678     }
1679     --len;
1680 
1681     if (TRACE_ON(d3d_bytecode))
1682     {
1683         TRACE_(d3d_bytecode)("[ %08x ", opcode_token);
1684         for (i = 0; i < len; ++i)
1685         {
1686             TRACE_(d3d_bytecode)("%08x ", (*ptr)[i]);
1687         }
1688         TRACE_(d3d_bytecode)("]\n");
1689     }
1690 
1691     if (!(opcode_info = get_opcode_info(opcode)))
1692     {
1693         FIXME("Unrecognized opcode %#x, opcode_token 0x%08x.\n", opcode, opcode_token);
1694         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
1695         *ptr += len;
1696         return;
1697     }
1698 
1699     ins->handler_idx = opcode_info->handler_idx;
1700     ins->flags = 0;
1701     ins->coissue = 0;
1702     ins->predicate = NULL;
1703     ins->dst_count = strlen(opcode_info->dst_info);
1704     ins->dst = priv->dst_param;
1705     ins->src_count = strlen(opcode_info->src_info);
1706     ins->src = priv->src_param;
1707     memset(&ins->texel_offset, 0, sizeof(ins->texel_offset));
1708 
1709     p = *ptr;
1710     *ptr += len;
1711 
1712     if (opcode_info->read_opcode_func)
1713     {
1714         opcode_info->read_opcode_func(ins, opcode, opcode_token, p, len, priv);
1715     }
1716     else
1717     {
1718         enum wined3d_shader_dst_modifier instruction_dst_modifier = WINED3DSPDM_NONE;
1719 
1720         previous_token = opcode_token;
1721         while (previous_token & WINED3D_SM4_INSTRUCTION_MODIFIER && p != *ptr)
1722             shader_sm4_read_instruction_modifier(previous_token = *p++, ins);
1723 
1724         ins->flags = (opcode_token & WINED3D_SM4_INSTRUCTION_FLAGS_MASK) >> WINED3D_SM4_INSTRUCTION_FLAGS_SHIFT;
1725         if (ins->flags & WINED3D_SM4_INSTRUCTION_FLAG_SATURATE)
1726         {
1727             ins->flags &= ~WINED3D_SM4_INSTRUCTION_FLAG_SATURATE;
1728             instruction_dst_modifier = WINED3DSPDM_SATURATE;
1729         }
1730         precise = (opcode_token & WINED3D_SM5_PRECISE_MASK) >> WINED3D_SM5_PRECISE_SHIFT;
1731         ins->flags |= precise << WINED3DSI_PRECISE_SHIFT;
1732 
1733         for (i = 0; i < ins->dst_count; ++i)
1734         {
1735             if (!(shader_sm4_read_dst_param(priv, &p, *ptr, map_data_type(opcode_info->dst_info[i]),
1736                     &priv->dst_param[i])))
1737             {
1738                 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
1739                 return;
1740             }
1741             priv->dst_param[i].modifiers |= instruction_dst_modifier;
1742         }
1743 
1744         for (i = 0; i < ins->src_count; ++i)
1745         {
1746             if (!(shader_sm4_read_src_param(priv, &p, *ptr, map_data_type(opcode_info->src_info[i]),
1747                     &priv->src_param[i])))
1748             {
1749                 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
1750                 return;
1751             }
1752         }
1753     }
1754 
1755     return;
1756 
1757 fail:
1758     *ptr = priv->end;
1759     ins->handler_idx = WINED3DSIH_TABLE_SIZE;
1760     return;
1761 }
1762 
1763 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
1764 {
1765     struct wined3d_sm4_data *priv = data;
1766     return *ptr == priv->end;
1767 }
1768 
1769 const struct wined3d_shader_frontend sm4_shader_frontend =
1770 {
1771     shader_sm4_init,
1772     shader_sm4_free,
1773     shader_sm4_read_header,
1774     shader_sm4_read_instruction,
1775     shader_sm4_is_end,
1776 };
1777