wined3d: Properly load the y correction.
[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->swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
353     src->modifiers = (param & WINED3DSP_SRCMOD_MASK) >> WINED3DSP_SRCMOD_SHIFT;
354     src->reg.rel_addr = rel_addr;
355 }
356
357 static void shader_parse_dst_param(DWORD param, const struct wined3d_shader_src_param *rel_addr,
358         struct wined3d_shader_dst_param *dst)
359 {
360     dst->reg.type = ((param & WINED3DSP_REGTYPE_MASK) >> WINED3DSP_REGTYPE_SHIFT)
361             | ((param & WINED3DSP_REGTYPE_MASK2) >> WINED3DSP_REGTYPE_SHIFT2);
362     dst->reg.idx = param & WINED3DSP_REGNUM_MASK;
363     dst->write_mask = (param & WINED3D_SM1_WRITEMASK_MASK) >> WINED3D_SM1_WRITEMASK_SHIFT;
364     dst->modifiers = (param & WINED3DSP_DSTMOD_MASK) >> WINED3DSP_DSTMOD_SHIFT;
365     dst->shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
366     dst->reg.rel_addr = rel_addr;
367 }
368
369 /* Read the parameters of an unrecognized opcode from the input stream
370  * Return the number of tokens read.
371  *
372  * Note: This function assumes source or destination token format.
373  * It will not work with specially-formatted tokens like DEF or DCL,
374  * but hopefully those would be recognized */
375 static int shader_skip_unrecognized(const struct wined3d_sm1_data *priv, const DWORD *ptr)
376 {
377     int tokens_read = 0;
378     int i = 0;
379
380     /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
381     while (*ptr & 0x80000000)
382     {
383         DWORD token, addr_token = 0;
384         struct wined3d_shader_src_param rel_addr;
385
386         tokens_read += shader_get_param(priv, ptr, &token, &addr_token);
387         ptr += tokens_read;
388
389         FIXME("Unrecognized opcode param: token=0x%08x addr_token=0x%08x name=", token, addr_token);
390
391         if (token & WINED3DSHADER_ADDRMODE_RELATIVE) shader_parse_src_param(addr_token, NULL, &rel_addr);
392
393         if (!i)
394         {
395             struct wined3d_shader_dst_param dst;
396
397             shader_parse_dst_param(token, token & WINED3DSHADER_ADDRMODE_RELATIVE ? &rel_addr : NULL, &dst);
398             shader_dump_dst_param(&dst, &priv->shader_version);
399         }
400         else
401         {
402             struct wined3d_shader_src_param src;
403
404             shader_parse_src_param(token, token & WINED3DSHADER_ADDRMODE_RELATIVE ? &rel_addr : NULL, &src);
405             shader_dump_src_param(&src, &priv->shader_version);
406         }
407         FIXME("\n");
408         ++i;
409     }
410     return tokens_read;
411 }
412
413 static void *shader_sm1_init(const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
414 {
415     struct wined3d_sm1_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
416     if (!priv)
417     {
418         ERR("Failed to allocate private data\n");
419         return NULL;
420     }
421
422     if (output_signature)
423     {
424         FIXME("SM 1-3 shader shouldn't have output signatures.\n");
425     }
426
427     switch (*byte_code >> 16)
428     {
429         case WINED3D_SM1_VS:
430             priv->shader_version.type = WINED3D_SHADER_TYPE_VERTEX;
431             priv->opcode_table = vs_opcode_table;
432             break;
433
434         case WINED3D_SM1_PS:
435             priv->shader_version.type = WINED3D_SHADER_TYPE_PIXEL;
436             priv->opcode_table = ps_opcode_table;
437             break;
438
439         default:
440             FIXME("Unrecognized shader type %#x\n", *byte_code >> 16);
441             HeapFree(GetProcessHeap(), 0, priv);
442             return NULL;
443     }
444
445     return priv;
446 }
447
448 static void shader_sm1_free(void *data)
449 {
450     HeapFree(GetProcessHeap(), 0, data);
451 }
452
453 static void shader_sm1_read_header(void *data, const DWORD **ptr, struct wined3d_shader_version *shader_version)
454 {
455     struct wined3d_sm1_data *priv = data;
456     DWORD version_token;
457
458     version_token = *(*ptr)++;
459     TRACE("version: 0x%08x\n", version_token);
460
461     priv->shader_version.major = WINED3D_SM1_VERSION_MAJOR(version_token);
462     priv->shader_version.minor = WINED3D_SM1_VERSION_MINOR(version_token);
463     *shader_version = priv->shader_version;
464 }
465
466 static void shader_sm1_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
467         UINT *param_size)
468 {
469     struct wined3d_sm1_data *priv = data;
470     const struct wined3d_sm1_opcode_info *opcode_info;
471     DWORD opcode_token;
472
473     opcode_token = *(*ptr)++;
474     opcode_info = shader_get_opcode(priv, opcode_token);
475     if (!opcode_info)
476     {
477         FIXME("Unrecognized opcode: token=0x%08x\n", opcode_token);
478         ins->handler_idx = WINED3DSIH_TABLE_SIZE;
479         *param_size = shader_skip_unrecognized(priv, *ptr);
480         return;
481     }
482
483     ins->handler_idx = opcode_info->handler_idx;
484     ins->flags = (opcode_token & WINED3D_OPCODESPECIFICCONTROL_MASK) >> WINED3D_OPCODESPECIFICCONTROL_SHIFT;
485     ins->coissue = opcode_token & WINED3DSI_COISSUE;
486     ins->predicate = opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED;
487     ins->dst_count = opcode_info->dst_count ? 1 : 0;
488     ins->src_count = opcode_info->param_count - opcode_info->dst_count;
489     *param_size = shader_skip_opcode(priv, opcode_info, opcode_token);
490 }
491
492 static void shader_sm1_read_src_param(void *data, const DWORD **ptr, struct wined3d_shader_src_param *src_param,
493         struct wined3d_shader_src_param *src_rel_addr)
494 {
495     struct wined3d_sm1_data *priv = data;
496     DWORD token, addr_token;
497
498     *ptr += shader_get_param(priv, *ptr, &token, &addr_token);
499     if (token & WINED3DSHADER_ADDRMODE_RELATIVE)
500     {
501         shader_parse_src_param(addr_token, NULL, src_rel_addr);
502         shader_parse_src_param(token, src_rel_addr, src_param);
503     }
504     else
505     {
506         shader_parse_src_param(token, NULL, src_param);
507     }
508 }
509
510 static void shader_sm1_read_dst_param(void *data, const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
511         struct wined3d_shader_src_param *dst_rel_addr)
512 {
513     struct wined3d_sm1_data *priv = data;
514     DWORD token, addr_token;
515
516     *ptr += shader_get_param(priv, *ptr, &token, &addr_token);
517     if (token & WINED3DSHADER_ADDRMODE_RELATIVE)
518     {
519         shader_parse_src_param(addr_token, NULL, dst_rel_addr);
520         shader_parse_dst_param(token, dst_rel_addr, dst_param);
521     }
522     else
523     {
524         shader_parse_dst_param(token, NULL, dst_param);
525     }
526 }
527
528 static void shader_sm1_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
529 {
530     DWORD usage_token = *(*ptr)++;
531     DWORD dst_token = *(*ptr)++;
532
533     semantic->usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
534     semantic->usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
535     semantic->sampler_type = (usage_token & WINED3DSP_TEXTURETYPE_MASK) >> WINED3DSP_TEXTURETYPE_SHIFT;
536     shader_parse_dst_param(dst_token, NULL, &semantic->reg);
537 }
538
539 static void shader_sm1_read_comment(const DWORD **ptr, const char **comment)
540 {
541     DWORD token = **ptr;
542
543     if ((token & WINED3DSI_OPCODE_MASK) != WINED3DSIO_COMMENT)
544     {
545         *comment = NULL;
546         return;
547     }
548
549     *comment = (const char *)++(*ptr);
550     *ptr += (token & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
551 }
552
553 static BOOL shader_sm1_is_end(void *data, const DWORD **ptr)
554 {
555     if (**ptr == WINED3DSP_END)
556     {
557         ++(*ptr);
558         return TRUE;
559     }
560
561     return FALSE;
562 }
563
564 const struct wined3d_shader_frontend sm1_shader_frontend =
565 {
566     shader_sm1_init,
567     shader_sm1_free,
568     shader_sm1_read_header,
569     shader_sm1_read_opcode,
570     shader_sm1_read_src_param,
571     shader_sm1_read_dst_param,
572     shader_sm1_read_semantic,
573     shader_sm1_read_comment,
574     shader_sm1_is_end,
575 };