d3d10: Implement ID3D10EffectVariable::AsVector().
[wine] / dlls / wined3d / shader_sm1.c
1 /*
2  * Copyright 2002-2003 Jason Edmeades
3  * Copyright 2002-2003 Raphael Junqueira
4  * Copyright 2004 Christian Costa
5  * Copyright 2005 Oliver Stieber
6  * Copyright 2006 Ivan Gyurdiev
7  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8  * Copyright 2009 Henri Verbeet for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include "wined3d_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
31
32 /* DCL usage masks */
33 #define WINED3DSP_DCL_USAGE_SHIFT               0
34 #define WINED3DSP_DCL_USAGE_MASK                (0xf << WINED3DSP_DCL_USAGE_SHIFT)
35 #define WINED3DSP_DCL_USAGEINDEX_SHIFT          16
36 #define WINED3DSP_DCL_USAGEINDEX_MASK           (0xf << WINED3DSP_DCL_USAGEINDEX_SHIFT)
37
38 /* DCL sampler type */
39 #define WINED3DSP_TEXTURETYPE_SHIFT             27
40 #define WINED3DSP_TEXTURETYPE_MASK              (0xf << WINED3DSP_TEXTURETYPE_SHIFT)
41
42 /* Opcode-related masks */
43 #define WINED3DSI_OPCODE_MASK                   0x0000ffff
44
45 #define WINED3D_OPCODESPECIFICCONTROL_SHIFT     16
46 #define WINED3D_OPCODESPECIFICCONTROL_MASK      (0xff << WINED3D_OPCODESPECIFICCONTROL_SHIFT)
47
48 #define WINED3DSI_INSTLENGTH_SHIFT              24
49 #define WINED3DSI_INSTLENGTH_MASK               (0xf << WINED3DSI_INSTLENGTH_SHIFT)
50
51 #define WINED3DSI_COISSUE                       (1 << 30)
52
53 #define WINED3DSI_COMMENTSIZE_SHIFT             16
54 #define WINED3DSI_COMMENTSIZE_MASK              (0x7fff << WINED3DSI_COMMENTSIZE_SHIFT)
55
56 #define WINED3DSHADER_INSTRUCTION_PREDICATED    (1 << 28)
57
58 /* Register number mask */
59 #define WINED3DSP_REGNUM_MASK                   0x000007ff
60
61 /* Register type masks  */
62 #define WINED3DSP_REGTYPE_SHIFT                 28
63 #define WINED3DSP_REGTYPE_MASK                  (0x7 << WINED3DSP_REGTYPE_SHIFT)
64 #define WINED3DSP_REGTYPE_SHIFT2                8
65 #define WINED3DSP_REGTYPE_MASK2                 (0x18 << WINED3DSP_REGTYPE_SHIFT2)
66
67 /* Relative addressing mask */
68 #define WINED3DSHADER_ADDRESSMODE_SHIFT         13
69 #define WINED3DSHADER_ADDRESSMODE_MASK          (1 << WINED3DSHADER_ADDRESSMODE_SHIFT)
70
71 /* Destination modifier mask */
72 #define WINED3DSP_DSTMOD_SHIFT                  20
73 #define WINED3DSP_DSTMOD_MASK                   (0xf << WINED3DSP_DSTMOD_SHIFT)
74
75 /* Destination shift mask */
76 #define WINED3DSP_DSTSHIFT_SHIFT                24
77 #define WINED3DSP_DSTSHIFT_MASK                 (0xf << WINED3DSP_DSTSHIFT_SHIFT)
78
79 /* Write mask */
80 #define WINED3D_SM1_WRITEMASK_SHIFT             16
81 #define WINED3D_SM1_WRITEMASK_MASK              (0xf << WINED3D_SM1_WRITEMASK_SHIFT)
82
83 /* Swizzle mask */
84 #define WINED3DSP_SWIZZLE_SHIFT                 16
85 #define WINED3DSP_SWIZZLE_MASK                  (0xff << WINED3DSP_SWIZZLE_SHIFT)
86
87 /* Source modifier mask */
88 #define WINED3DSP_SRCMOD_SHIFT                  24
89 #define WINED3DSP_SRCMOD_MASK                   (0xf << WINED3DSP_SRCMOD_SHIFT)
90
91 #define WINED3DSP_END                           0x0000ffff
92
93 #define WINED3D_SM1_VERSION_MAJOR(version)      (((version) >> 8) & 0xff)
94 #define WINED3D_SM1_VERSION_MINOR(version)      (((version) >> 0) & 0xff)
95
96 enum WINED3DSHADER_ADDRESSMODE_TYPE
97 {
98     WINED3DSHADER_ADDRMODE_ABSOLUTE = 0 << WINED3DSHADER_ADDRESSMODE_SHIFT,
99     WINED3DSHADER_ADDRMODE_RELATIVE = 1 << WINED3DSHADER_ADDRESSMODE_SHIFT,
100 };
101
102 struct wined3d_sm1_opcode_info
103 {
104     unsigned int opcode;
105     UINT dst_count;
106     UINT param_count;
107     enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
108     DWORD min_version;
109     DWORD max_version;
110 };
111
112 struct wined3d_sm1_data
113 {
114     struct wined3d_shader_version shader_version;
115     const struct wined3d_sm1_opcode_info *opcode_table;
116 };
117
118 /* This table is not order or position dependent. */
119 static const struct wined3d_sm1_opcode_info vs_opcode_table[] =
120 {
121     /* Arithmetic */
122     {WINED3DSIO_NOP,          0, 0, WINED3DSIH_NOP,          0,                           0                          },
123     {WINED3DSIO_MOV,          1, 2, WINED3DSIH_MOV,          0,                           0                          },
124     {WINED3DSIO_MOVA,         1, 2, WINED3DSIH_MOVA,         WINED3D_SHADER_VERSION(2,0), -1                         },
125     {WINED3DSIO_ADD,          1, 3, WINED3DSIH_ADD,          0,                           0                          },
126     {WINED3DSIO_SUB,          1, 3, WINED3DSIH_SUB,          0,                           0                          },
127     {WINED3DSIO_MAD,          1, 4, WINED3DSIH_MAD,          0,                           0                          },
128     {WINED3DSIO_MUL,          1, 3, WINED3DSIH_MUL,          0,                           0                          },
129     {WINED3DSIO_RCP,          1, 2, WINED3DSIH_RCP,          0,                           0                          },
130     {WINED3DSIO_RSQ,          1, 2, WINED3DSIH_RSQ,          0,                           0                          },
131     {WINED3DSIO_DP3,          1, 3, WINED3DSIH_DP3,          0,                           0                          },
132     {WINED3DSIO_DP4,          1, 3, WINED3DSIH_DP4,          0,                           0                          },
133     {WINED3DSIO_MIN,          1, 3, WINED3DSIH_MIN,          0,                           0                          },
134     {WINED3DSIO_MAX,          1, 3, WINED3DSIH_MAX,          0,                           0                          },
135     {WINED3DSIO_SLT,          1, 3, WINED3DSIH_SLT,          0,                           0                          },
136     {WINED3DSIO_SGE,          1, 3, WINED3DSIH_SGE,          0,                           0                          },
137     {WINED3DSIO_ABS,          1, 2, WINED3DSIH_ABS,          0,                           0                          },
138     {WINED3DSIO_EXP,          1, 2, WINED3DSIH_EXP,          0,                           0                          },
139     {WINED3DSIO_LOG,          1, 2, WINED3DSIH_LOG,          0,                           0                          },
140     {WINED3DSIO_EXPP,         1, 2, WINED3DSIH_EXPP,         0,                           0                          },
141     {WINED3DSIO_LOGP,         1, 2, WINED3DSIH_LOGP,         0,                           0                          },
142     {WINED3DSIO_LIT,          1, 2, WINED3DSIH_LIT,          0,                           0                          },
143     {WINED3DSIO_DST,          1, 3, WINED3DSIH_DST,          0,                           0                          },
144     {WINED3DSIO_LRP,          1, 4, WINED3DSIH_LRP,          0,                           0                          },
145     {WINED3DSIO_FRC,          1, 2, WINED3DSIH_FRC,          0,                           0                          },
146     {WINED3DSIO_POW,          1, 3, WINED3DSIH_POW,          0,                           0                          },
147     {WINED3DSIO_CRS,          1, 3, WINED3DSIH_CRS,          0,                           0                          },
148     {WINED3DSIO_SGN,          1, 2, WINED3DSIH_SGN,          0,                           0                          },
149     {WINED3DSIO_NRM,          1, 2, WINED3DSIH_NRM,          0,                           0                          },
150     {WINED3DSIO_SINCOS,       1, 4, WINED3DSIH_SINCOS,       WINED3D_SHADER_VERSION(2,0), WINED3D_SHADER_VERSION(2,1)},
151     {WINED3DSIO_SINCOS,       1, 2, WINED3DSIH_SINCOS,       WINED3D_SHADER_VERSION(3,0), -1                         },
152     /* Matrix */
153     {WINED3DSIO_M4x4,         1, 3, WINED3DSIH_M4x4,         0,                           0                          },
154     {WINED3DSIO_M4x3,         1, 3, WINED3DSIH_M4x3,         0,                           0                          },
155     {WINED3DSIO_M3x4,         1, 3, WINED3DSIH_M3x4,         0,                           0                          },
156     {WINED3DSIO_M3x3,         1, 3, WINED3DSIH_M3x3,         0,                           0                          },
157     {WINED3DSIO_M3x2,         1, 3, WINED3DSIH_M3x2,         0,                           0                          },
158     /* Declare registers */
159     {WINED3DSIO_DCL,          0, 2, WINED3DSIH_DCL,          0,                           0                          },
160     /* Constant definitions */
161     {WINED3DSIO_DEF,          1, 5, WINED3DSIH_DEF,          0,                           0                          },
162     {WINED3DSIO_DEFB,         1, 2, WINED3DSIH_DEFB,         0,                           0                          },
163     {WINED3DSIO_DEFI,         1, 5, WINED3DSIH_DEFI,         0,                           0                          },
164     /* Flow control */
165     {WINED3DSIO_REP,          0, 1, WINED3DSIH_REP,          WINED3D_SHADER_VERSION(2,0), -1                         },
166     {WINED3DSIO_ENDREP,       0, 0, WINED3DSIH_ENDREP,       WINED3D_SHADER_VERSION(2,0), -1                         },
167     {WINED3DSIO_IF,           0, 1, WINED3DSIH_IF,           WINED3D_SHADER_VERSION(2,0), -1                         },
168     {WINED3DSIO_IFC,          0, 2, WINED3DSIH_IFC,          WINED3D_SHADER_VERSION(2,1), -1                         },
169     {WINED3DSIO_ELSE,         0, 0, WINED3DSIH_ELSE,         WINED3D_SHADER_VERSION(2,0), -1                         },
170     {WINED3DSIO_ENDIF,        0, 0, WINED3DSIH_ENDIF,        WINED3D_SHADER_VERSION(2,0), -1                         },
171     {WINED3DSIO_BREAK,        0, 0, WINED3DSIH_BREAK,        WINED3D_SHADER_VERSION(2,1), -1                         },
172     {WINED3DSIO_BREAKC,       0, 2, WINED3DSIH_BREAKC,       WINED3D_SHADER_VERSION(2,1), -1                         },
173     {WINED3DSIO_BREAKP,       0, 1, WINED3DSIH_BREAKP,       0,                           0                          },
174     {WINED3DSIO_CALL,         0, 1, WINED3DSIH_CALL,         WINED3D_SHADER_VERSION(2,0), -1                         },
175     {WINED3DSIO_CALLNZ,       0, 2, WINED3DSIH_CALLNZ,       WINED3D_SHADER_VERSION(2,0), -1                         },
176     {WINED3DSIO_LOOP,         0, 2, WINED3DSIH_LOOP,         WINED3D_SHADER_VERSION(2,0), -1                         },
177     {WINED3DSIO_RET,          0, 0, WINED3DSIH_RET,          WINED3D_SHADER_VERSION(2,0), -1                         },
178     {WINED3DSIO_ENDLOOP,      0, 0, WINED3DSIH_ENDLOOP,      WINED3D_SHADER_VERSION(2,0), -1                         },
179     {WINED3DSIO_LABEL,        0, 1, WINED3DSIH_LABEL,        WINED3D_SHADER_VERSION(2,0), -1                         },
180
181     {WINED3DSIO_SETP,         1, 3, WINED3DSIH_SETP,         0,                           0                          },
182     {WINED3DSIO_TEXLDL,       1, 3, WINED3DSIH_TEXLDL,       WINED3D_SHADER_VERSION(3,0), -1                         },
183     {0,                       0, 0, WINED3DSIH_TABLE_SIZE,   0,                           0                          },
184 };
185
186 static const struct wined3d_sm1_opcode_info ps_opcode_table[] =
187 {
188     /* Arithmetic */
189     {WINED3DSIO_NOP,          0, 0, WINED3DSIH_NOP,          0,                           0                          },
190     {WINED3DSIO_MOV,          1, 2, WINED3DSIH_MOV,          0,                           0                          },
191     {WINED3DSIO_ADD,          1, 3, WINED3DSIH_ADD,          0,                           0                          },
192     {WINED3DSIO_SUB,          1, 3, WINED3DSIH_SUB,          0,                           0                          },
193     {WINED3DSIO_MAD,          1, 4, WINED3DSIH_MAD,          0,                           0                          },
194     {WINED3DSIO_MUL,          1, 3, WINED3DSIH_MUL,          0,                           0                          },
195     {WINED3DSIO_RCP,          1, 2, WINED3DSIH_RCP,          0,                           0                          },
196     {WINED3DSIO_RSQ,          1, 2, WINED3DSIH_RSQ,          0,                           0                          },
197     {WINED3DSIO_DP3,          1, 3, WINED3DSIH_DP3,          0,                           0                          },
198     {WINED3DSIO_DP4,          1, 3, WINED3DSIH_DP4,          0,                           0                          },
199     {WINED3DSIO_MIN,          1, 3, WINED3DSIH_MIN,          0,                           0                          },
200     {WINED3DSIO_MAX,          1, 3, WINED3DSIH_MAX,          0,                           0                          },
201     {WINED3DSIO_SLT,          1, 3, WINED3DSIH_SLT,          0,                           0                          },
202     {WINED3DSIO_SGE,          1, 3, WINED3DSIH_SGE,          0,                           0                          },
203     {WINED3DSIO_ABS,          1, 2, WINED3DSIH_ABS,          0,                           0                          },
204     {WINED3DSIO_EXP,          1, 2, WINED3DSIH_EXP,          0,                           0                          },
205     {WINED3DSIO_LOG,          1, 2, WINED3DSIH_LOG,          0,                           0                          },
206     {WINED3DSIO_EXPP,         1, 2, WINED3DSIH_EXPP,         0,                           0                          },
207     {WINED3DSIO_LOGP,         1, 2, WINED3DSIH_LOGP,         0,                           0                          },
208     {WINED3DSIO_DST,          1, 3, WINED3DSIH_DST,          0,                           0                          },
209     {WINED3DSIO_LRP,          1, 4, WINED3DSIH_LRP,          0,                           0                          },
210     {WINED3DSIO_FRC,          1, 2, WINED3DSIH_FRC,          0,                           0                          },
211     {WINED3DSIO_CND,          1, 4, WINED3DSIH_CND,          WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,4)},
212     {WINED3DSIO_CMP,          1, 4, WINED3DSIH_CMP,          WINED3D_SHADER_VERSION(1,2), WINED3D_SHADER_VERSION(3,0)},
213     {WINED3DSIO_POW,          1, 3, WINED3DSIH_POW,          0,                           0                          },
214     {WINED3DSIO_CRS,          1, 3, WINED3DSIH_CRS,          0,                           0                          },
215     {WINED3DSIO_NRM,          1, 2, WINED3DSIH_NRM,          0,                           0                          },
216     {WINED3DSIO_SINCOS,       1, 4, WINED3DSIH_SINCOS,       WINED3D_SHADER_VERSION(2,0), WINED3D_SHADER_VERSION(2,1)},
217     {WINED3DSIO_SINCOS,       1, 2, WINED3DSIH_SINCOS,       WINED3D_SHADER_VERSION(3,0), -1                         },
218     {WINED3DSIO_DP2ADD,       1, 4, WINED3DSIH_DP2ADD,       WINED3D_SHADER_VERSION(2,0), -1                         },
219     /* Matrix */
220     {WINED3DSIO_M4x4,         1, 3, WINED3DSIH_M4x4,         0,                           0                          },
221     {WINED3DSIO_M4x3,         1, 3, WINED3DSIH_M4x3,         0,                           0                          },
222     {WINED3DSIO_M3x4,         1, 3, WINED3DSIH_M3x4,         0,                           0                          },
223     {WINED3DSIO_M3x3,         1, 3, WINED3DSIH_M3x3,         0,                           0                          },
224     {WINED3DSIO_M3x2,         1, 3, WINED3DSIH_M3x2,         0,                           0                          },
225     /* Register declarations */
226     {WINED3DSIO_DCL,          0, 2, WINED3DSIH_DCL,          0,                           0                          },
227     /* Flow control */
228     {WINED3DSIO_REP,          0, 1, WINED3DSIH_REP,          WINED3D_SHADER_VERSION(2,1), -1                         },
229     {WINED3DSIO_ENDREP,       0, 0, WINED3DSIH_ENDREP,       WINED3D_SHADER_VERSION(2,1), -1                         },
230     {WINED3DSIO_IF,           0, 1, WINED3DSIH_IF,           WINED3D_SHADER_VERSION(2,1), -1                         },
231     {WINED3DSIO_IFC,          0, 2, WINED3DSIH_IFC,          WINED3D_SHADER_VERSION(2,1), -1                         },
232     {WINED3DSIO_ELSE,         0, 0, WINED3DSIH_ELSE,         WINED3D_SHADER_VERSION(2,1), -1                         },
233     {WINED3DSIO_ENDIF,        0, 0, WINED3DSIH_ENDIF,        WINED3D_SHADER_VERSION(2,1), -1                         },
234     {WINED3DSIO_BREAK,        0, 0, WINED3DSIH_BREAK,        WINED3D_SHADER_VERSION(2,1), -1                         },
235     {WINED3DSIO_BREAKC,       0, 2, WINED3DSIH_BREAKC,       WINED3D_SHADER_VERSION(2,1), -1                         },
236     {WINED3DSIO_BREAKP,       0, 1, WINED3DSIH_BREAKP,       0,                           0                          },
237     {WINED3DSIO_CALL,         0, 1, WINED3DSIH_CALL,         WINED3D_SHADER_VERSION(2,1), -1                         },
238     {WINED3DSIO_CALLNZ,       0, 2, WINED3DSIH_CALLNZ,       WINED3D_SHADER_VERSION(2,1), -1                         },
239     {WINED3DSIO_LOOP,         0, 2, WINED3DSIH_LOOP,         WINED3D_SHADER_VERSION(3,0), -1                         },
240     {WINED3DSIO_RET,          0, 0, WINED3DSIH_RET,          WINED3D_SHADER_VERSION(2,1), -1                         },
241     {WINED3DSIO_ENDLOOP,      0, 0, WINED3DSIH_ENDLOOP,      WINED3D_SHADER_VERSION(3,0), -1                         },
242     {WINED3DSIO_LABEL,        0, 1, WINED3DSIH_LABEL,        WINED3D_SHADER_VERSION(2,1), -1                         },
243     /* Constant definitions */
244     {WINED3DSIO_DEF,          1, 5, WINED3DSIH_DEF,          0,                           0                          },
245     {WINED3DSIO_DEFB,         1, 2, WINED3DSIH_DEFB,         0,                           0                          },
246     {WINED3DSIO_DEFI,         1, 5, WINED3DSIH_DEFI,         0,                           0                          },
247     /* Texture */
248     {WINED3DSIO_TEXCOORD,     1, 1, WINED3DSIH_TEXCOORD,     0,                           WINED3D_SHADER_VERSION(1,3)},
249     {WINED3DSIO_TEXCOORD,     1, 2, WINED3DSIH_TEXCOORD,     WINED3D_SHADER_VERSION(1,4), WINED3D_SHADER_VERSION(1,4)},
250     {WINED3DSIO_TEXKILL,      1, 1, WINED3DSIH_TEXKILL,      WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(3,0)},
251     {WINED3DSIO_TEX,          1, 1, WINED3DSIH_TEX,          0,                           WINED3D_SHADER_VERSION(1,3)},
252     {WINED3DSIO_TEX,          1, 2, WINED3DSIH_TEX,          WINED3D_SHADER_VERSION(1,4), WINED3D_SHADER_VERSION(1,4)},
253     {WINED3DSIO_TEX,          1, 3, WINED3DSIH_TEX,          WINED3D_SHADER_VERSION(2,0), -1                         },
254     {WINED3DSIO_TEXBEM,       1, 2, WINED3DSIH_TEXBEM,       0,                           WINED3D_SHADER_VERSION(1,3)},
255     {WINED3DSIO_TEXBEML,      1, 2, WINED3DSIH_TEXBEML,      WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
256     {WINED3DSIO_TEXREG2AR,    1, 2, WINED3DSIH_TEXREG2AR,    WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
257     {WINED3DSIO_TEXREG2GB,    1, 2, WINED3DSIH_TEXREG2GB,    WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
258     {WINED3DSIO_TEXREG2RGB,   1, 2, WINED3DSIH_TEXREG2RGB,   WINED3D_SHADER_VERSION(1,2), WINED3D_SHADER_VERSION(1,3)},
259     {WINED3DSIO_TEXM3x2PAD,   1, 2, WINED3DSIH_TEXM3x2PAD,   WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
260     {WINED3DSIO_TEXM3x2TEX,   1, 2, WINED3DSIH_TEXM3x2TEX,   WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
261     {WINED3DSIO_TEXM3x3PAD,   1, 2, WINED3DSIH_TEXM3x3PAD,   WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
262     {WINED3DSIO_TEXM3x3DIFF,  1, 2, WINED3DSIH_TEXM3x3DIFF,  WINED3D_SHADER_VERSION(0,0), WINED3D_SHADER_VERSION(0,0)},
263     {WINED3DSIO_TEXM3x3SPEC,  1, 3, WINED3DSIH_TEXM3x3SPEC,  WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
264     {WINED3DSIO_TEXM3x3VSPEC, 1, 2, WINED3DSIH_TEXM3x3VSPEC, WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
265     {WINED3DSIO_TEXM3x3TEX,   1, 2, WINED3DSIH_TEXM3x3TEX,   WINED3D_SHADER_VERSION(1,0), WINED3D_SHADER_VERSION(1,3)},
266     {WINED3DSIO_TEXDP3TEX,    1, 2, WINED3DSIH_TEXDP3TEX,    WINED3D_SHADER_VERSION(1,2), WINED3D_SHADER_VERSION(1,3)},
267     {WINED3DSIO_TEXM3x2DEPTH, 1, 2, WINED3DSIH_TEXM3x2DEPTH, WINED3D_SHADER_VERSION(1,3), WINED3D_SHADER_VERSION(1,3)},
268     {WINED3DSIO_TEXDP3,       1, 2, WINED3DSIH_TEXDP3,       WINED3D_SHADER_VERSION(1,2), WINED3D_SHADER_VERSION(1,3)},
269     {WINED3DSIO_TEXM3x3,      1, 2, WINED3DSIH_TEXM3x3,      WINED3D_SHADER_VERSION(1,2), WINED3D_SHADER_VERSION(1,3)},
270     {WINED3DSIO_TEXDEPTH,     1, 1, WINED3DSIH_TEXDEPTH,     WINED3D_SHADER_VERSION(1,4), WINED3D_SHADER_VERSION(1,4)},
271     {WINED3DSIO_BEM,          1, 3, WINED3DSIH_BEM,          WINED3D_SHADER_VERSION(1,4), WINED3D_SHADER_VERSION(1,4)},
272     {WINED3DSIO_DSX,          1, 2, WINED3DSIH_DSX,          WINED3D_SHADER_VERSION(2,1), -1                         },
273     {WINED3DSIO_DSY,          1, 2, WINED3DSIH_DSY,          WINED3D_SHADER_VERSION(2,1), -1                         },
274     {WINED3DSIO_TEXLDD,       1, 5, WINED3DSIH_TEXLDD,       WINED3D_SHADER_VERSION(2,1), -1                         },
275     {WINED3DSIO_SETP,         1, 3, WINED3DSIH_SETP,         0,                           0                          },
276     {WINED3DSIO_TEXLDL,       1, 3, WINED3DSIH_TEXLDL,       WINED3D_SHADER_VERSION(3,0), -1                         },
277     {WINED3DSIO_PHASE,        0, 0, WINED3DSIH_PHASE,        0,                           0                          },
278     {0,                       0, 0, WINED3DSIH_TABLE_SIZE,   0,                           0                          },
279 };
280
281 /* Read a parameter opcode from the input stream,
282  * and possibly a relative addressing token.
283  * Return the number of tokens read */
284 static int shader_get_param(const struct wined3d_sm1_data *priv, const DWORD *ptr, DWORD *token, DWORD *addr_token)
285 {
286     UINT count = 1;
287
288     *token = *ptr;
289
290     /* PS >= 3.0 have relative addressing (with token)
291      * VS >= 2.0 have relative addressing (with token)
292      * VS >= 1.0 < 2.0 have relative addressing (without token)
293      * The version check below should work in general */
294     if (*ptr & WINED3DSHADER_ADDRMODE_RELATIVE)
295     {
296         if (priv->shader_version.major < 2)
297         {
298             *addr_token = (1 << 31)
299                     | ((WINED3DSPR_ADDR << WINED3DSP_REGTYPE_SHIFT2) & WINED3DSP_REGTYPE_MASK2)
300                     | ((WINED3DSPR_ADDR << WINED3DSP_REGTYPE_SHIFT) & WINED3DSP_REGTYPE_MASK)
301                     | (WINED3DSP_NOSWIZZLE << WINED3DSP_SWIZZLE_SHIFT);
302         }
303         else
304         {
305             *addr_token = *(ptr + 1);
306             ++count;
307         }
308     }
309
310     return count;
311 }
312
313 static const struct wined3d_sm1_opcode_info *shader_get_opcode(const struct wined3d_sm1_data *priv, DWORD code)
314 {
315     DWORD shader_version = WINED3D_SHADER_VERSION(priv->shader_version.major, priv->shader_version.minor);
316     const struct wined3d_sm1_opcode_info *opcode_table = priv->opcode_table;
317     DWORD i = 0;
318
319     while (opcode_table[i].handler_idx != WINED3DSIH_TABLE_SIZE)
320     {
321         if ((code & WINED3DSI_OPCODE_MASK) == opcode_table[i].opcode
322                 && shader_version >= opcode_table[i].min_version
323                 && (!opcode_table[i].max_version || shader_version <= opcode_table[i].max_version))
324         {
325             return &opcode_table[i];
326         }
327         ++i;
328     }
329
330     FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n",
331             code, code, code & WINED3DSI_OPCODE_MASK, shader_version);
332
333     return NULL;
334 }
335
336 /* Return the number of parameters to skip for an opcode */
337 static int shader_skip_opcode(const struct wined3d_sm1_data *priv,
338         const struct wined3d_sm1_opcode_info *opcode_info, DWORD opcode_token)
339 {
340    /* Shaders >= 2.0 may contain address tokens, but fortunately they
341     * have a useful length mask - use it here. Shaders 1.0 contain no such tokens */
342     return (priv->shader_version.major >= 2)
343             ? ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT) : opcode_info->param_count;
344 }
345
346 static void shader_parse_src_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
347         struct wined3d_shader_src_param *src)
348 {
349     src->reg.type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
350             | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
351     src->reg.idx = param & WINED3DSP_REGNUM_MASK;
352     src->reg.array_idx = ~0U;
353     src->swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
354     src->modifiers = (param & WINED3DSP_SRCMOD_MASK) >> WINED3DSP_SRCMOD_SHIFT;
355     src->reg.rel_addr = rel_addr;
356 }
357
358 static void shader_parse_dst_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
359         struct wined3d_shader_dst_param *dst)
360 {
361     dst->reg.type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
362             | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
363     dst->reg.idx = param & WINED3DSP_REGNUM_MASK;
364     dst->reg.array_idx = ~0U;
365     dst->write_mask = (param & WINED3D_SM1_WRITEMASK_MASK) >> WINED3D_SM1_WRITEMASK_SHIFT;
366     dst->modifiers = (param & WINED3DSP_DSTMOD_MASK) >> WINED3DSP_DSTMOD_SHIFT;
367     dst->shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
368     dst->reg.rel_addr = rel_addr;
369 }
370
371 /* Read the parameters of an unrecognized opcode from the input stream
372  * Return the number of tokens read.
373  *
374  * Note: This function assumes source or destination token format.
375  * It will not work with specially-formatted tokens like DEF or DCL,
376  * but hopefully those would be recognized */
377 static int shader_skip_unrecognized(const struct wined3d_sm1_data *priv, const DWORD *ptr)
378 {
379     int tokens_read = 0;
380     int i = 0;
381
382     /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
383     while (*ptr & 0x80000000)
384     {
385         DWORD token, addr_token = 0;
386         struct wined3d_shader_src_param rel_addr;
387
388         tokens_read += shader_get_param(priv, ptr, &token, &addr_token);
389         ptr += tokens_read;
390
391         FIXME("Unrecognized opcode param: token=0x%08x addr_token=0x%08x name=", token, addr_token);
392
393         if (token & WINED3DSHADER_ADDRMODE_RELATIVE) shader_parse_src_param(addr_token, NULL, &rel_addr);
394
395         if (!i)
396         {
397             struct wined3d_shader_dst_param dst;
398
399             shader_parse_dst_param(token, token & WINED3DSHADER_ADDRMODE_RELATIVE ? &rel_addr : NULL, &dst);
400             shader_dump_dst_param(&dst, &priv->shader_version);
401         }
402         else
403         {
404             struct wined3d_shader_src_param src;
405
406             shader_parse_src_param(token, token & WINED3DSHADER_ADDRMODE_RELATIVE ? &rel_addr : NULL, &src);
407             shader_dump_src_param(&src, &priv->shader_version);
408         }
409         FIXME("\n");
410         ++i;
411     }
412     return tokens_read;
413 }
414
415 static void *shader_sm1_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
416 {
417     struct wined3d_sm1_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
418     if (!priv)
419     {
420         ERR("Failed to allocate private data\n");
421         return NULL;
422     }
423
424     if (output_signature)
425     {
426         FIXME("SM 1-3 shader shouldn't have output signatures.\n");
427     }
428
429     switch (*byte_code >> 16)
430     {
431         case WINED3D_SM1_VS:
432             priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
433             priv->opcode_table = vs_opcode_table;
434             break;
435
436         case WINED3D_SM1_PS:
437             priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
438             priv->opcode_table = ps_opcode_table;
439             break;
440
441         default:
442             FIXME("Unrecognized shader type %#x\n", *byte_code >> 16);
443             HeapFree(GetProcessHeap(), 0, priv);
444             return NULL;
445     }
446
447     return priv;
448 }
449
450 static void shader_sm1_free(void *data)
451 {
452     HeapFree(GetProcessHeap(), 0, data);
453 }
454
455 static void shader_sm1_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
456 {
457     struct wined3d_sm1_data *priv = data;
458     DWORD version_token;
459
460     version_token = *(*ptr)++;
461     TRACE("version: 0x%08x\n", version_token);
462
463     priv->shader_version.major = WINED3D_SM1_VERSION_MAJOR(version_token);
464     priv->shader_version.minor = WINED3D_SM1_VERSION_MINOR(version_token);
465     *shader_version = priv->shader_version;
466 }
467
468 static void shader_sm1_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
469         UINT *param_size)
470 {
471     struct wined3d_sm1_data *priv = data;
472     const struct wined3d_sm1_opcode_info *opcode_info;
473     DWORD opcode_token;
474
475     opcode_token = *(*ptr)++;
476     opcode_info = shader_get_opcode(priv, opcode_token);
477     if (!opcode_info)
478     {
479         FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
480         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
481         *param_size = shader_skip_unrecognized(priv, *ptr);
482         return;
483     }
484
485     ins->handler_idx = opcode_info->handler_idx;
486     ins->flags = (opcode_token & WINED3D_OPCODESPECIFICCONTROL_MASK) >> WINED3D_OPCODESPECIFICCONTROL_SHIFT;
487     ins->coissue = opcode_token & WINED3DSI_COISSUE;
488     ins->predicate = opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED;
489     ins->dst_count = opcode_info->dst_count ? 1 : 0;
490     ins->src_count = opcode_info->param_count - opcode_info->dst_count;
491     *param_size = shader_skip_opcode(priv, opcode_info, opcode_token);
492 }
493
494 static void shader_sm1_read_src_param(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
495         struct wined3d_shader_src_param *src_rel_addr)
496 {
497     struct wined3d_sm1_data *priv = data;
498     DWORD token, addr_token;
499
500     *ptr += shader_get_param(priv, *ptr, &token, &addr_token);
501     if (token & WINED3DSHADER_ADDRMODE_RELATIVE)
502     {
503         shader_parse_src_param(addr_token, NULL, src_rel_addr);
504         shader_parse_src_param(token, src_rel_addr, src_param);
505     }
506     else
507     {
508         shader_parse_src_param(token, NULL, src_param);
509     }
510 }
511
512 static void shader_sm1_read_dst_param(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
513         struct wined3d_shader_src_param *dst_rel_addr)
514 {
515     struct wined3d_sm1_data *priv = data;
516     DWORD token, addr_token;
517
518     *ptr += shader_get_param(priv, *ptr, &token, &addr_token);
519     if (token & WINED3DSHADER_ADDRMODE_RELATIVE)
520     {
521         shader_parse_src_param(addr_token, NULL, dst_rel_addr);
522         shader_parse_dst_param(token, dst_rel_addr, dst_param);
523     }
524     else
525     {
526         shader_parse_dst_param(token, NULL, dst_param);
527     }
528 }
529
530 static void shader_sm1_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
531 {
532     DWORD usage_token = *(*ptr)++;
533     DWORD dst_token = *(*ptr)++;
534
535     semantic->usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
536     semantic->usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
537     semantic->sampler_type = (usage_token & WINED3DSP_TEXTURETYPE_MASK) >> WINED3DSP_TEXTURETYPE_SHIFT;
538     shader_parse_dst_param(dst_token, NULL, &semantic->reg);
539 }
540
541 static void shader_sm1_read_comment(const DWORD **ptr, const char **comment)
542 {
543     DWORD token = **ptr;
544
545     if ((token & WINED3DSI_OPCODE_MASK) != WINED3DSIO_COMMENT)
546     {
547         *comment = NULL;
548         return;
549     }
550
551     *comment = (const char *)++(*ptr);
552     *ptr += (token & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
553 }
554
555 static BOOL shader_sm1_is_end(void *data, const DWORD **ptr)
556 {
557     if (**ptr == WINED3DSP_END)
558     {
559         ++(*ptr);
560         return TRUE;
561     }
562
563     return FALSE;
564 }
565
566 const struct wined3d_shader_frontend sm1_shader_frontend =
567 {
568     shader_sm1_init,
569     shader_sm1_free,
570     shader_sm1_read_header,
571     shader_sm1_read_opcode,
572     shader_sm1_read_src_param,
573     shader_sm1_read_dst_param,
574     shader_sm1_read_semantic,
575     shader_sm1_read_comment,
576     shader_sm1_is_end,
577 };