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