wined3d: Add support for immediate constants to shader_dump_register().
[wine] / dlls / wined3d / ati_fragment_shader.c
1 /*
2  * Fixed function pipeline replacement using GL_ATI_fragment_shader
3  *
4  * Copyright 2008 Stefan Dösinger(for CodeWeavers)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <math.h>
24 #include <stdio.h>
25
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
29 WINE_DECLARE_DEBUG_CHANNEL(d3d);
30
31 /* Some private defines, Constant associations, etc.
32  * Env bump matrix and per stage constant should be independent,
33  * a stage that bump maps can't read the per state constant
34  */
35 #define ATI_FFP_CONST_BUMPMAT(i) (GL_CON_0_ATI + i)
36 #define ATI_FFP_CONST_CONSTANT0 GL_CON_0_ATI
37 #define ATI_FFP_CONST_CONSTANT1 GL_CON_1_ATI
38 #define ATI_FFP_CONST_CONSTANT2 GL_CON_2_ATI
39 #define ATI_FFP_CONST_CONSTANT3 GL_CON_3_ATI
40 #define ATI_FFP_CONST_CONSTANT4 GL_CON_4_ATI
41 #define ATI_FFP_CONST_CONSTANT5 GL_CON_5_ATI
42 #define ATI_FFP_CONST_TFACTOR   GL_CON_6_ATI
43
44 /* GL_ATI_fragment_shader specific fixed function pipeline description. "Inherits" from the common one */
45 struct atifs_ffp_desc
46 {
47     struct ffp_frag_desc parent;
48     GLuint shader;
49     unsigned int num_textures_used;
50 };
51
52 struct atifs_private_data
53 {
54     struct hash_table_t *fragment_shaders; /* A hashtable to track fragment pipeline replacement shaders */
55
56 };
57
58 static const char *debug_dstmod(GLuint mod) {
59     switch(mod) {
60         case GL_NONE:               return "GL_NONE";
61         case GL_2X_BIT_ATI:         return "GL_2X_BIT_ATI";
62         case GL_4X_BIT_ATI:         return "GL_4X_BIT_ATI";
63         case GL_8X_BIT_ATI:         return "GL_8X_BIT_ATI";
64         case GL_HALF_BIT_ATI:       return "GL_HALF_BIT_ATI";
65         case GL_QUARTER_BIT_ATI:    return "GL_QUARTER_BIT_ATI";
66         case GL_EIGHTH_BIT_ATI:     return "GL_EIGHTH_BIT_ATI";
67         case GL_SATURATE_BIT_ATI:   return "GL_SATURATE_BIT_ATI";
68         default:                    return "Unexpected modifier\n";
69     }
70 }
71
72 static const char *debug_argmod(GLuint mod) {
73     switch(mod) {
74         case GL_NONE:
75             return "GL_NONE";
76
77         case GL_2X_BIT_ATI:
78             return "GL_2X_BIT_ATI";
79         case GL_COMP_BIT_ATI:
80             return "GL_COMP_BIT_ATI";
81         case GL_NEGATE_BIT_ATI:
82             return "GL_NEGATE_BIT_ATI";
83         case GL_BIAS_BIT_ATI:
84             return "GL_BIAS_BIT_ATI";
85
86         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI:
87             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI";
88         case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI:
89             return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI";
90         case GL_2X_BIT_ATI | GL_BIAS_BIT_ATI:
91             return "GL_2X_BIT_ATI | GL_BIAS_BIT_ATI";
92         case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
93             return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
94         case GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
95             return "GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
96         case GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
97             return "GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
98
99         case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
100             return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
101         case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
102             return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
103         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
104             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
105         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
106             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
107
108         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
109             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
110
111         default:
112             return "Unexpected argmod combination\n";
113     }
114 }
115 static const char *debug_register(GLuint reg) {
116     switch(reg) {
117         case GL_REG_0_ATI:                  return "GL_REG_0_ATI";
118         case GL_REG_1_ATI:                  return "GL_REG_1_ATI";
119         case GL_REG_2_ATI:                  return "GL_REG_2_ATI";
120         case GL_REG_3_ATI:                  return "GL_REG_3_ATI";
121         case GL_REG_4_ATI:                  return "GL_REG_4_ATI";
122         case GL_REG_5_ATI:                  return "GL_REG_5_ATI";
123
124         case GL_CON_0_ATI:                  return "GL_CON_0_ATI";
125         case GL_CON_1_ATI:                  return "GL_CON_1_ATI";
126         case GL_CON_2_ATI:                  return "GL_CON_2_ATI";
127         case GL_CON_3_ATI:                  return "GL_CON_3_ATI";
128         case GL_CON_4_ATI:                  return "GL_CON_4_ATI";
129         case GL_CON_5_ATI:                  return "GL_CON_5_ATI";
130         case GL_CON_6_ATI:                  return "GL_CON_6_ATI";
131         case GL_CON_7_ATI:                  return "GL_CON_7_ATI";
132
133         case GL_ZERO:                       return "GL_ZERO";
134         case GL_ONE:                        return "GL_ONE";
135         case GL_PRIMARY_COLOR:              return "GL_PRIMARY_COLOR";
136         case GL_SECONDARY_INTERPOLATOR_ATI: return "GL_SECONDARY_INTERPOLATOR_ATI";
137
138         default:                            return "Unknown register\n";
139     }
140 }
141
142 static const char *debug_swizzle(GLuint swizzle) {
143     switch(swizzle) {
144         case GL_SWIZZLE_STR_ATI:        return "GL_SWIZZLE_STR_ATI";
145         case GL_SWIZZLE_STQ_ATI:        return "GL_SWIZZLE_STQ_ATI";
146         case GL_SWIZZLE_STR_DR_ATI:     return "GL_SWIZZLE_STR_DR_ATI";
147         case GL_SWIZZLE_STQ_DQ_ATI:     return "GL_SWIZZLE_STQ_DQ_ATI";
148         default:                        return "unknown swizzle";
149     }
150 }
151
152 static const char *debug_rep(GLuint rep) {
153     switch(rep) {
154         case GL_NONE:                   return "GL_NONE";
155         case GL_RED:                    return "GL_RED";
156         case GL_GREEN:                  return "GL_GREEN";
157         case GL_BLUE:                   return "GL_BLUE";
158         default:                        return "unknown argrep";
159     }
160 }
161
162 static const char *debug_op(GLuint op) {
163     switch(op) {
164         case GL_MOV_ATI:                return "GL_MOV_ATI";
165         case GL_ADD_ATI:                return "GL_ADD_ATI";
166         case GL_MUL_ATI:                return "GL_MUL_ATI";
167         case GL_SUB_ATI:                return "GL_SUB_ATI";
168         case GL_DOT3_ATI:               return "GL_DOT3_ATI";
169         case GL_DOT4_ATI:               return "GL_DOT4_ATI";
170         case GL_MAD_ATI:                return "GL_MAD_ATI";
171         case GL_LERP_ATI:               return "GL_LERP_ATI";
172         case GL_CND_ATI:                return "GL_CND_ATI";
173         case GL_CND0_ATI:               return "GL_CND0_ATI";
174         case GL_DOT2_ADD_ATI:           return "GL_DOT2_ADD_ATI";
175         default:                        return "unexpected op";
176     }
177 }
178
179 static const char *debug_mask(GLuint mask) {
180     switch(mask) {
181         case GL_NONE:                           return "GL_NONE";
182         case GL_RED_BIT_ATI:                    return "GL_RED_BIT_ATI";
183         case GL_GREEN_BIT_ATI:                  return "GL_GREEN_BIT_ATI";
184         case GL_BLUE_BIT_ATI:                   return "GL_BLUE_BIT_ATI";
185         case GL_RED_BIT_ATI | GL_GREEN_BIT_ATI: return "GL_RED_BIT_ATI | GL_GREEN_BIT_ATI";
186         case GL_RED_BIT_ATI | GL_BLUE_BIT_ATI:  return "GL_RED_BIT_ATI | GL_BLUE_BIT_ATI";
187         case GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI:return "GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI";
188         case GL_RED_BIT_ATI | GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI:return "GL_RED_BIT_ATI | GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI";
189         default:                                return "Unexpected writemask";
190     }
191 }
192 #define GLINFO_LOCATION (*gl_info)
193
194 static void wrap_op1(const WineD3D_GL_Info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
195                      GLuint arg1, GLuint arg1Rep, GLuint arg1Mod) {
196     if(dstMask == GL_ALPHA) {
197         TRACE("glAlphaFragmentOp1ATI(%s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
198               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod));
199         GL_EXTCALL(glAlphaFragmentOp1ATI(op, dst, dstMod, arg1, arg1Rep, arg1Mod));
200     } else {
201         TRACE("glColorFragmentOp1ATI(%s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
202               debug_mask(dstMask), debug_dstmod(dstMod),
203               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod));
204         GL_EXTCALL(glColorFragmentOp1ATI(op, dst, dstMask, dstMod, arg1, arg1Rep, arg1Mod));
205     }
206 }
207
208 static void wrap_op2(const WineD3D_GL_Info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
209                      GLuint arg1, GLuint arg1Rep, GLuint arg1Mod,
210                      GLuint arg2, GLuint arg2Rep, GLuint arg2Mod) {
211     if(dstMask == GL_ALPHA) {
212         TRACE("glAlphaFragmentOp2ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
213               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
214               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod));
215         GL_EXTCALL(glAlphaFragmentOp2ATI(op, dst, dstMod, arg1, arg1Rep, arg1Mod, arg2, arg2Rep, arg2Mod));
216     } else {
217         TRACE("glColorFragmentOp2ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
218               debug_mask(dstMask), debug_dstmod(dstMod),
219               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
220               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod));
221         GL_EXTCALL(glColorFragmentOp2ATI(op, dst, dstMask, dstMod, arg1, arg1Rep, arg1Mod, arg2, arg2Rep, arg2Mod));
222     }
223 }
224
225 static void wrap_op3(const WineD3D_GL_Info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
226                      GLuint arg1, GLuint arg1Rep, GLuint arg1Mod,
227                      GLuint arg2, GLuint arg2Rep, GLuint arg2Mod,
228                      GLuint arg3, GLuint arg3Rep, GLuint arg3Mod) {
229     if(dstMask == GL_ALPHA) {
230         /* Leave some free space to fit "GL_NONE, " in to align most alpha and color op lines */
231         TRACE("glAlphaFragmentOp3ATI(%s, %s,          %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
232               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
233               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod),
234               debug_register(arg3), debug_rep(arg3Rep), debug_argmod(arg3Mod));
235         GL_EXTCALL(glAlphaFragmentOp3ATI(op, dst, dstMod,
236                                          arg1, arg1Rep, arg1Mod,
237                                          arg2, arg2Rep, arg2Mod,
238                                          arg3, arg3Rep, arg3Mod));
239     } else {
240         TRACE("glColorFragmentOp3ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
241               debug_mask(dstMask), debug_dstmod(dstMod),
242               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
243               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod),
244               debug_register(arg3), debug_rep(arg3Rep), debug_argmod(arg3Mod));
245         GL_EXTCALL(glColorFragmentOp3ATI(op, dst, dstMask, dstMod,
246                                          arg1, arg1Rep, arg1Mod,
247                                          arg2, arg2Rep, arg2Mod,
248                                          arg3, arg3Rep, arg3Mod));
249     }
250 }
251
252 static GLuint register_for_arg(DWORD arg, const WineD3D_GL_Info *gl_info,
253         unsigned int stage, GLuint *mod, GLuint *rep, GLuint tmparg)
254 {
255     GLenum ret;
256
257     if(mod) *mod = GL_NONE;
258     if(arg == ARG_UNUSED)
259     {
260         if (rep) *rep = GL_NONE;
261         return -1; /* This is the marker for unused registers */
262     }
263
264     switch(arg & WINED3DTA_SELECTMASK) {
265         case WINED3DTA_DIFFUSE:
266             ret = GL_PRIMARY_COLOR;
267             break;
268
269         case WINED3DTA_CURRENT:
270             /* Note that using GL_REG_0_ATI for the passed on register is safe because
271              * texture0 is read at stage0, so in the worst case it is read in the
272              * instruction writing to reg0. Afterwards texture0 is not used any longer.
273              * If we're reading from current
274              */
275             if(stage == 0) {
276                 ret = GL_PRIMARY_COLOR;
277             } else {
278                 ret = GL_REG_0_ATI;
279             }
280             break;
281
282         case WINED3DTA_TEXTURE:
283             ret = GL_REG_0_ATI + stage;
284             break;
285
286         case WINED3DTA_TFACTOR:
287             ret = ATI_FFP_CONST_TFACTOR;
288             break;
289
290         case WINED3DTA_SPECULAR:
291             ret = GL_SECONDARY_INTERPOLATOR_ATI;
292             break;
293
294         case WINED3DTA_TEMP:
295             ret = tmparg;
296             break;
297
298         case WINED3DTA_CONSTANT:
299             FIXME("Unhandled source argument WINED3DTA_TEMP\n");
300             ret = GL_CON_0_ATI;
301             break;
302
303         default:
304             FIXME("Unknown source argument %d\n", arg);
305             ret = GL_ZERO;
306     }
307
308     if(arg & WINED3DTA_COMPLEMENT) {
309         if(mod) *mod |= GL_COMP_BIT_ATI;
310     }
311     if(arg & WINED3DTA_ALPHAREPLICATE) {
312         if(rep) *rep = GL_ALPHA;
313     } else {
314         if(rep) *rep = GL_NONE;
315     }
316     return ret;
317 }
318
319 static GLuint find_tmpreg(const struct texture_stage_op op[MAX_TEXTURES])
320 {
321     int lowest_read = -1;
322     int lowest_write = -1;
323     int i;
324     BOOL tex_used[MAX_TEXTURES];
325
326     memset(tex_used, 0, sizeof(tex_used));
327     for(i = 0; i < MAX_TEXTURES; i++) {
328         if(op[i].cop == WINED3DTOP_DISABLE) {
329             break;
330         }
331
332         if(lowest_read == -1 &&
333           (op[i].carg1 == WINED3DTA_TEMP || op[i].carg2 == WINED3DTA_TEMP || op[i].carg0 == WINED3DTA_TEMP ||
334            op[i].aarg1 == WINED3DTA_TEMP || op[i].aarg2 == WINED3DTA_TEMP || op[i].aarg0 == WINED3DTA_TEMP)) {
335             lowest_read = i;
336         }
337
338         if(lowest_write == -1 && op[i].dst == tempreg) {
339             lowest_write = i;
340         }
341
342         if(op[i].carg1 == WINED3DTA_TEXTURE || op[i].carg2 == WINED3DTA_TEXTURE || op[i].carg0 == WINED3DTA_TEXTURE ||
343            op[i].aarg1 == WINED3DTA_TEXTURE || op[i].aarg2 == WINED3DTA_TEXTURE || op[i].aarg0 == WINED3DTA_TEXTURE) {
344             tex_used[i] = TRUE;
345         }
346     }
347
348     /* Temp reg not read? We don't need it, return GL_NONE */
349     if(lowest_read == -1) return GL_NONE;
350
351     if(lowest_write >= lowest_read) {
352         FIXME("Temp register read before being written\n");
353     }
354
355     if(lowest_write == -1) {
356         /* This needs a test. Maybe we are supposed to return 0.0/0.0/0.0/0.0, or fail drawprim, or whatever */
357         FIXME("Temp register read without being written\n");
358         return GL_REG_1_ATI;
359     } else if(lowest_write >= 1) {
360         /* If we're writing to the temp reg at earliest in stage 1, we can use register 1 for the temp result.
361          * there may be texture data stored in reg 1, but we do not need it any longer since stage 1 already
362          * read it
363          */
364         return GL_REG_1_ATI;
365     } else {
366         /* Search for a free texture register. We have 6 registers available. GL_REG_0_ATI is already used
367          * for the regular result
368          */
369         for(i = 1; i < 6; i++) {
370             if(!tex_used[i]) {
371                 return GL_REG_0_ATI + i;
372             }
373         }
374         /* What to do here? Report it in ValidateDevice? */
375         FIXME("Could not find a register for the temporary register\n");
376         return 0;
377     }
378 }
379
380 static GLuint gen_ati_shader(const struct texture_stage_op op[MAX_TEXTURES], const WineD3D_GL_Info *gl_info)
381 {
382     GLuint ret = GL_EXTCALL(glGenFragmentShadersATI(1));
383     unsigned int stage;
384     GLuint arg0, arg1, arg2, extrarg;
385     GLuint dstmod, argmod0, argmod1, argmod2, argmodextra;
386     GLuint rep0, rep1, rep2;
387     GLuint swizzle;
388     GLuint tmparg = find_tmpreg(op);
389     GLuint dstreg;
390
391     if(!ret) {
392         ERR("Failed to generate a GL_ATI_fragment_shader shader id\n");
393         return 0;
394     }
395     GL_EXTCALL(glBindFragmentShaderATI(ret));
396     checkGLcall("GL_EXTCALL(glBindFragmentShaderATI(ret))");
397
398     TRACE("glBeginFragmentShaderATI()\n");
399     GL_EXTCALL(glBeginFragmentShaderATI());
400     checkGLcall("GL_EXTCALL(glBeginFragmentShaderATI())");
401
402     /* Pass 1: Generate sampling instructions for perturbation maps */
403       for(stage = 0; stage < GL_LIMITS(textures); stage++) {
404         if(op[stage].cop == WINED3DTOP_DISABLE) break;
405         if(op[stage].cop != WINED3DTOP_BUMPENVMAP &&
406            op[stage].cop != WINED3DTOP_BUMPENVMAPLUMINANCE) continue;
407
408         TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, GL_SWIZZLE_STR_ATI)\n",
409               stage, stage);
410         GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
411                    GL_TEXTURE0_ARB + stage,
412                    GL_SWIZZLE_STR_ATI));
413         if(op[stage + 1].projected == proj_none) {
414             swizzle = GL_SWIZZLE_STR_ATI;
415         } else if(op[stage + 1].projected == proj_count4) {
416             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
417         } else {
418             swizzle = GL_SWIZZLE_STR_DR_ATI;
419         }
420         TRACE("glPassTexCoordATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
421               stage + 1, stage + 1, debug_swizzle(swizzle));
422         GL_EXTCALL(glPassTexCoordATI(GL_REG_0_ATI + stage + 1,
423                    GL_TEXTURE0_ARB + stage + 1,
424                    swizzle));
425
426         /* We need GL_REG_5_ATI as a temporary register to swizzle the bump matrix. So we run into
427          * issues if we're bump mapping on stage 4 or 5
428          */
429         if(stage >= 4) {
430             FIXME("Bump mapping in stage %d\n", stage);
431         }
432     }
433
434     /* Pass 2: Generate perturbation calculations */
435     for(stage = 0; stage < GL_LIMITS(textures); stage++) {
436         GLuint argmodextra_x, argmodextra_y;
437         struct color_fixup_desc fixup;
438
439         if(op[stage].cop == WINED3DTOP_DISABLE) break;
440         if(op[stage].cop != WINED3DTOP_BUMPENVMAP &&
441            op[stage].cop != WINED3DTOP_BUMPENVMAPLUMINANCE) continue;
442
443         fixup = op[stage].color_fixup;
444         if (fixup.x_source != CHANNEL_SOURCE_X || fixup.y_source != CHANNEL_SOURCE_Y)
445         {
446             FIXME("Swizzles not implemented\n");
447             argmodextra_x = GL_NONE;
448             argmodextra_y = GL_NONE;
449         }
450         else
451         {
452             /* Nice thing, we get the color correction for free :-) */
453             argmodextra_x = fixup.x_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
454             argmodextra_y = fixup.y_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
455         }
456
457         wrap_op3(gl_info, GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_RED_BIT_ATI, GL_NONE,
458                  GL_REG_0_ATI + stage, GL_NONE, argmodextra_x,
459                  ATI_FFP_CONST_BUMPMAT(stage), GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
460                  GL_REG_0_ATI + stage + 1, GL_RED, GL_NONE);
461
462         /* FIXME: How can I make GL_DOT2_ADD_ATI read the factors from blue and alpha? It defaults to red and green,
463          * and it is fairly easy to make it read GL_BLUE or BL_ALPHA, but I can't get an R * B + G * A. So we're wasting
464          * one register and two instructions in this pass for a simple swizzling operation.
465          * For starters it might be good enough to merge the two movs into one, but even that isn't possible :-(
466          *
467          * NOTE: GL_BLUE | GL_ALPHA is not possible. It doesn't throw a compilation error, but an OR operation on the
468          * constants doesn't make sense, considering their values.
469          */
470         wrap_op1(gl_info, GL_MOV_ATI, GL_REG_5_ATI, GL_RED_BIT_ATI, GL_NONE,
471                  ATI_FFP_CONST_BUMPMAT(stage), GL_BLUE, GL_NONE);
472         wrap_op1(gl_info, GL_MOV_ATI, GL_REG_5_ATI, GL_GREEN_BIT_ATI, GL_NONE,
473                  ATI_FFP_CONST_BUMPMAT(stage), GL_ALPHA, GL_NONE);
474         wrap_op3(gl_info, GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_GREEN_BIT_ATI, GL_NONE,
475                  GL_REG_0_ATI + stage, GL_NONE, argmodextra_y,
476                  GL_REG_5_ATI, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
477                  GL_REG_0_ATI + stage + 1, GL_GREEN, GL_NONE);
478     }
479
480     /* Pass 3: Generate sampling instructions for regular textures */
481     for(stage = 0; stage < GL_LIMITS(textures); stage++) {
482         if(op[stage].cop == WINED3DTOP_DISABLE) {
483             break;
484         }
485
486         if(op[stage].projected == proj_none) {
487             swizzle = GL_SWIZZLE_STR_ATI;
488         } else if(op[stage].projected == proj_count3) {
489             swizzle = GL_SWIZZLE_STR_DR_ATI;
490         } else {
491             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
492         }
493
494         if((op[stage].carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
495            (op[stage].carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
496            (op[stage].carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
497            (op[stage].aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
498            (op[stage].aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
499            (op[stage].aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
500             op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) {
501
502             if(stage > 0 &&
503                (op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
504                 op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
505                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_REG_%d_ATI, GL_SWIZZLE_STR_ATI)\n",
506                       stage, stage);
507                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
508                            GL_REG_0_ATI + stage,
509                            GL_SWIZZLE_STR_ATI));
510             } else {
511                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
512                     stage, stage, debug_swizzle(swizzle));
513                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
514                                         GL_TEXTURE0_ARB + stage,
515                                         swizzle));
516             }
517         }
518     }
519
520     /* Pass 4: Generate the arithmetic instructions */
521     for(stage = 0; stage < MAX_TEXTURES; stage++) {
522         if(op[stage].cop == WINED3DTOP_DISABLE) {
523             if(stage == 0) {
524                 /* Handle complete texture disabling gracefully */
525                 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
526                          GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
527                 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
528                          GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
529             }
530             break;
531         }
532
533         if(op[stage].dst == tempreg) {
534             /* If we're writing to D3DTA_TEMP, but never reading from it we don't have to write there in the first place.
535              * skip the entire stage, this saves some GPU time
536              */
537             if(tmparg == GL_NONE) continue;
538
539             dstreg = tmparg;
540         } else {
541             dstreg = GL_REG_0_ATI;
542         }
543
544         arg0 = register_for_arg(op[stage].carg0, gl_info, stage, &argmod0, &rep0, tmparg);
545         arg1 = register_for_arg(op[stage].carg1, gl_info, stage, &argmod1, &rep1, tmparg);
546         arg2 = register_for_arg(op[stage].carg2, gl_info, stage, &argmod2, &rep2, tmparg);
547         dstmod = GL_NONE;
548         argmodextra = GL_NONE;
549         extrarg = GL_NONE;
550
551         switch(op[stage].cop) {
552             case WINED3DTOP_SELECTARG2:
553                 arg1 = arg2;
554                 argmod1 = argmod2;
555                 rep1 = rep2;
556             case WINED3DTOP_SELECTARG1:
557                 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_NONE, GL_NONE,
558                          arg1, rep1, argmod1);
559                 break;
560
561             case WINED3DTOP_MODULATE4X:
562                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
563             case WINED3DTOP_MODULATE2X:
564                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
565                 dstmod |= GL_SATURATE_BIT_ATI;
566             case WINED3DTOP_MODULATE:
567                 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_NONE, dstmod,
568                          arg1, rep1, argmod1,
569                          arg2, rep2, argmod2);
570                 break;
571
572             case WINED3DTOP_ADDSIGNED2X:
573                 dstmod = GL_2X_BIT_ATI;
574             case WINED3DTOP_ADDSIGNED:
575                 argmodextra = GL_BIAS_BIT_ATI;
576             case WINED3DTOP_ADD:
577                 dstmod |= GL_SATURATE_BIT_ATI;
578                 wrap_op2(gl_info, GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, dstmod,
579                          arg1, rep1, argmod1,
580                          arg2, rep2, argmodextra | argmod2);
581                 break;
582
583             case WINED3DTOP_SUBTRACT:
584                 dstmod |= GL_SATURATE_BIT_ATI;
585                 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_NONE, dstmod,
586                          arg1, rep1, argmod1,
587                          arg2, rep2, argmod2);
588                 break;
589
590             case WINED3DTOP_ADDSMOOTH:
591                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
592                 /* Dst = arg1 + * arg2(1 -arg 1)
593                  *     = arg2 * (1 - arg1) + arg1
594                  */
595                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
596                          arg2, rep2, argmod2,
597                          arg1, rep1, argmodextra,
598                          arg1, rep1, argmod1);
599                 break;
600
601             case WINED3DTOP_BLENDCURRENTALPHA:
602                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
603             case WINED3DTOP_BLENDFACTORALPHA:
604                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
605             case WINED3DTOP_BLENDTEXTUREALPHA:
606                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
607             case WINED3DTOP_BLENDDIFFUSEALPHA:
608                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
609                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
610                          extrarg, GL_ALPHA, GL_NONE,
611                          arg1, rep1, argmod1,
612                          arg2, rep2, argmod2);
613                 break;
614
615             case WINED3DTOP_BLENDTEXTUREALPHAPM:
616                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
617                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_NONE,
618                          arg2, rep2,  argmod2,
619                          arg0, GL_ALPHA, GL_COMP_BIT_ATI,
620                          arg1, rep1,  argmod1);
621                 break;
622
623             /* D3DTOP_PREMODULATE ???? */
624
625             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
626                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
627             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
628                 if(!argmodextra) argmodextra = argmod1;
629                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
630                          arg2, rep2,  argmod2,
631                          arg1, GL_ALPHA, argmodextra,
632                          arg1, rep1,  argmod1);
633                 break;
634
635             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
636                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
637             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
638                 if(!argmodextra) argmodextra = argmod1;
639                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
640                          arg2, rep2,  argmod2,
641                          arg1, rep1,  argmodextra,
642                          arg1, GL_ALPHA, argmod1);
643                 break;
644
645             case WINED3DTOP_DOTPRODUCT3:
646                 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
647                          arg1, rep1, argmod1 | GL_BIAS_BIT_ATI,
648                          arg2, rep2, argmod2 | GL_BIAS_BIT_ATI);
649                 break;
650
651             case WINED3DTOP_MULTIPLYADD:
652                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
653                          arg1, rep1, argmod1,
654                          arg2, rep2, argmod2,
655                          arg0, rep0, argmod0);
656                 break;
657
658             case WINED3DTOP_LERP:
659                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
660                          arg0, rep0, argmod0,
661                          arg1, rep1, argmod1,
662                          arg2, rep2, argmod2);
663                 break;
664
665             case WINED3DTOP_BUMPENVMAP:
666             case WINED3DTOP_BUMPENVMAPLUMINANCE:
667                 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
668                 break;
669
670             default: FIXME("Unhandled color operation %d on stage %d\n", op[stage].cop, stage);
671         }
672
673         arg0 = register_for_arg(op[stage].aarg0, gl_info, stage, &argmod0, NULL, tmparg);
674         arg1 = register_for_arg(op[stage].aarg1, gl_info, stage, &argmod1, NULL, tmparg);
675         arg2 = register_for_arg(op[stage].aarg2, gl_info, stage, &argmod2, NULL, tmparg);
676         dstmod = GL_NONE;
677         argmodextra = GL_NONE;
678         extrarg = GL_NONE;
679
680         switch(op[stage].aop) {
681             case WINED3DTOP_DISABLE:
682                 /* Get the primary color to the output if on stage 0, otherwise leave register 0 untouched */
683                 if(stage == 0) {
684                     wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
685                              GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
686                 }
687                 break;
688
689             case WINED3DTOP_SELECTARG2:
690                 arg1 = arg2;
691                 argmod1 = argmod2;
692             case WINED3DTOP_SELECTARG1:
693                 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_ALPHA, GL_NONE,
694                          arg1, GL_NONE, argmod1);
695                 break;
696
697             case WINED3DTOP_MODULATE4X:
698                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
699             case WINED3DTOP_MODULATE2X:
700                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
701                 dstmod |= GL_SATURATE_BIT_ATI;
702             case WINED3DTOP_MODULATE:
703                 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_ALPHA, dstmod,
704                          arg1, GL_NONE, argmod1,
705                          arg2, GL_NONE, argmod2);
706                 break;
707
708             case WINED3DTOP_ADDSIGNED2X:
709                 dstmod = GL_2X_BIT_ATI;
710             case WINED3DTOP_ADDSIGNED:
711                 argmodextra = GL_BIAS_BIT_ATI;
712             case WINED3DTOP_ADD:
713                 dstmod |= GL_SATURATE_BIT_ATI;
714                 wrap_op2(gl_info, GL_ADD_ATI, dstreg, GL_ALPHA, dstmod,
715                          arg1, GL_NONE, argmod1,
716                          arg2, GL_NONE, argmodextra | argmod2);
717                 break;
718
719             case WINED3DTOP_SUBTRACT:
720                 dstmod |= GL_SATURATE_BIT_ATI;
721                 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_ALPHA, dstmod,
722                          arg1, GL_NONE, argmod1,
723                          arg2, GL_NONE, argmod2);
724                 break;
725
726             case WINED3DTOP_ADDSMOOTH:
727                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
728                 /* Dst = arg1 + * arg2(1 -arg 1)
729                  *     = arg2 * (1 - arg1) + arg1
730                  */
731                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
732                          arg2, GL_NONE, argmod2,
733                          arg1, GL_NONE, argmodextra,
734                          arg1, GL_NONE, argmod1);
735                 break;
736
737             case WINED3DTOP_BLENDCURRENTALPHA:
738                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
739             case WINED3DTOP_BLENDFACTORALPHA:
740                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
741             case WINED3DTOP_BLENDTEXTUREALPHA:
742                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
743             case WINED3DTOP_BLENDDIFFUSEALPHA:
744                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
745                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_NONE,
746                          extrarg, GL_ALPHA, GL_NONE,
747                          arg1, GL_NONE, argmod1,
748                          arg2, GL_NONE, argmod2);
749                 break;
750
751             case WINED3DTOP_BLENDTEXTUREALPHAPM:
752                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
753                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_NONE,
754                          arg2, GL_NONE,  argmod2,
755                          arg0, GL_ALPHA, GL_COMP_BIT_ATI,
756                          arg1, GL_NONE,  argmod1);
757                 break;
758
759             /* D3DTOP_PREMODULATE ???? */
760
761             case WINED3DTOP_DOTPRODUCT3:
762                 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_ALPHA, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
763                          arg1, GL_NONE, argmod1 | GL_BIAS_BIT_ATI,
764                          arg2, GL_NONE, argmod2 | GL_BIAS_BIT_ATI);
765                 break;
766
767             case WINED3DTOP_MULTIPLYADD:
768                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
769                          arg1, GL_NONE, argmod1,
770                          arg2, GL_NONE, argmod2,
771                          arg0, GL_NONE, argmod0);
772                 break;
773
774             case WINED3DTOP_LERP:
775                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
776                          arg1, GL_NONE, argmod1,
777                          arg2, GL_NONE, argmod2,
778                          arg0, GL_NONE, argmod0);
779                 break;
780
781             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
782             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
783             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
784             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
785             case WINED3DTOP_BUMPENVMAP:
786             case WINED3DTOP_BUMPENVMAPLUMINANCE:
787                 ERR("Application uses an invalid alpha operation\n");
788                 break;
789
790             default: FIXME("Unhandled alpha operation %d on stage %d\n", op[stage].aop, stage);
791         }
792     }
793
794     TRACE("glEndFragmentShaderATI()\n");
795     GL_EXTCALL(glEndFragmentShaderATI());
796     checkGLcall("GL_EXTCALL(glEndFragmentShaderATI())");
797     return ret;
798 }
799 #undef GLINFO_LOCATION
800
801 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
802 static void set_tex_op_atifs(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
803     IWineD3DDeviceImpl          *This = stateblock->wineD3DDevice;
804     const struct atifs_ffp_desc *desc;
805     struct ffp_frag_settings     settings;
806     struct atifs_private_data   *priv = This->fragment_priv;
807     DWORD mapped_stage;
808     unsigned int i;
809
810     gen_ffp_frag_op(stateblock, &settings, TRUE);
811     desc = (const struct atifs_ffp_desc *)find_ffp_frag_shader(priv->fragment_shaders, &settings);
812     if(!desc) {
813         struct atifs_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
814         if (!new_desc)
815         {
816             ERR("Out of memory\n");
817             return;
818         }
819         new_desc->num_textures_used = 0;
820         for(i = 0; i < GL_LIMITS(texture_stages); i++) {
821             if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
822             new_desc->num_textures_used = i;
823         }
824
825         memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
826         new_desc->shader = gen_ati_shader(settings.op, &GLINFO_LOCATION);
827         add_ffp_frag_shader(priv->fragment_shaders, &new_desc->parent);
828         TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
829         desc = new_desc;
830     }
831
832     /* GL_ATI_fragment_shader depends on the GL_TEXTURE_xD enable settings. Update the texture stages
833      * used by this shader
834      */
835     for(i = 0; i < desc->num_textures_used; i++) {
836         mapped_stage = This->texUnitMap[i];
837         if (mapped_stage != WINED3D_UNMAPPED_STAGE)
838         {
839             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
840             checkGLcall("glActiveTextureARB");
841             texture_activate_dimensions(i, stateblock, context);
842         }
843     }
844
845     GL_EXTCALL(glBindFragmentShaderATI(desc->shader));
846 }
847
848 static void state_texfactor_atifs(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
849     float col[4];
850     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
851
852     GL_EXTCALL(glSetFragmentShaderConstantATI(ATI_FFP_CONST_TFACTOR, col));
853     checkGLcall("glSetFragmentShaderConstantATI(ATI_FFP_CONST_TFACTOR, col)");
854 }
855
856 static void set_bumpmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
857     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
858     float mat[2][2];
859
860     mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
861     mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
862     mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
863     mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
864     /* GL_ATI_fragment_shader allows only constants from 0.0 to 1.0, but the bumpmat
865      * constants can be in any range. While they should stay between [-1.0 and 1.0] because
866      * Shader Model 1.x pixel shaders are clamped to that range negative values are used occasionally,
867      * for example by our d3d9 test. So to get negative values scale -1;1 to 0;1 and undo that in the
868      * shader(it is free). This might potentially reduce precision. However, if the hardware does
869      * support proper floats it shouldn't, and if it doesn't we can't get anything better anyway
870      */
871     mat[0][0] = (mat[0][0] + 1.0) * 0.5;
872     mat[1][0] = (mat[1][0] + 1.0) * 0.5;
873     mat[0][1] = (mat[0][1] + 1.0) * 0.5;
874     mat[1][1] = (mat[1][1] + 1.0) * 0.5;
875     GL_EXTCALL(glSetFragmentShaderConstantATI(ATI_FFP_CONST_BUMPMAT(stage), (float *) mat));
876     checkGLcall("glSetFragmentShaderConstantATI(ATI_FFP_CONST_BUMPMAT(stage), mat)");
877 }
878
879 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
880     if(!isStateDirty(context, STATE_PIXELSHADER)) {
881         set_tex_op_atifs(state, stateblock, context);
882     }
883 }
884
885 static void atifs_apply_pixelshader(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
886     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
887     BOOL use_vshader = use_vs(stateblock);
888
889     context->last_was_pshader = use_ps(stateblock);
890     /* The ATIFS code does not support pixel shaders currently, but we have to provide a state handler
891      * to call shader_select to select a vertex shader if one is applied because the vertex shader state
892      * may defer calling the shader backend if the pshader state is dirty.
893      *
894      * In theory the application should not be able to mark the pixel shader dirty because it cannot
895      * create a shader, and thus has no way to set the state to something != NULL. However, a different
896      * pipeline part may link a different state to its pixelshader handler, thus a pshader state exists
897      * and can be dirtified. Also the pshader is always dirtified at startup, and blitting disables all
898      * shaders and dirtifies all shader states. If atifs can deal with this it keeps the rest of the code
899      * simpler.
900      */
901     if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
902         device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, FALSE, use_vshader);
903
904         if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && use_vshader) {
905             device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
906         }
907     }
908 }
909
910 #undef GLINFO_LOCATION
911
912 static const struct StateEntryTemplate atifs_fragmentstate_template[] = {
913     {STATE_RENDER(WINED3DRS_TEXTUREFACTOR),               { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              state_texfactor_atifs   }, 0                               },
914     {STATE_RENDER(WINED3DRS_FOGCOLOR),                    { STATE_RENDER(WINED3DRS_FOGCOLOR),                   state_fogcolor          }, 0                               },
915     {STATE_RENDER(WINED3DRS_FOGDENSITY),                  { STATE_RENDER(WINED3DRS_FOGDENSITY),                 state_fogdensity        }, 0                               },
916     {STATE_RENDER(WINED3DRS_FOGENABLE),                   { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart      }, 0                               },
917     {STATE_RENDER(WINED3DRS_FOGTABLEMODE),                { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart      }, 0                               },
918     {STATE_RENDER(WINED3DRS_FOGVERTEXMODE),               { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog_fragpart      }, 0                               },
919     {STATE_RENDER(WINED3DRS_FOGSTART),                    { STATE_RENDER(WINED3DRS_FOGSTART),                   state_fogstartend       }, 0                               },
920     {STATE_RENDER(WINED3DRS_FOGEND),                      { STATE_RENDER(WINED3DRS_FOGSTART),                   state_fogstartend       }, 0                               },
921     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
922     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
923     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
924     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
925     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
926     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
927     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
928     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
929     {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
930     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
931     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
932     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
933     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
934     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
935     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
936     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
937     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
938     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
939     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
940     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
941     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
942     {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
943     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
944     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
945     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
946     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
947     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
948     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
949     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
950     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
951     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
952     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
953     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
954     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
955     {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
956     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
957     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
958     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
959     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
960     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
961     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
962     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
963     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
964     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
965     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
966     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
967     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
968     {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
969     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
970     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
971     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
972     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
973     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
974     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
975     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
976     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
977     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
978     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
979     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
980     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
981     {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
982     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
983     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
984     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
985     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
986     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
987     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
988     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
989     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
990     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
991     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
992     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
993     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
994     {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
995     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
996     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
997     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
998     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
999     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1000     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1001     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1002     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1003     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1004     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1005     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1006     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1007     {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1008     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1009     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1010     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1011     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1012     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1013     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1014     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1015     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1016     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1017     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1018     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1019     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1020     {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
1021     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1022     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1023     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1024     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
1025     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim          }, 0                               },
1026     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim          }, 0                               },
1027     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim          }, 0                               },
1028     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim          }, 0                               },
1029     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim          }, 0                               },
1030     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim          }, 0                               },
1031     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim          }, 0                               },
1032     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim          }, 0                               },
1033     {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1034     {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1035     {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1036     {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1037     {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1038     {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1039     {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1040     {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1041     {STATE_PIXELSHADER,                                   { STATE_PIXELSHADER,                                  atifs_apply_pixelshader }, 0                               },
1042     {0 /* Terminate */,                                   { 0,                                                  0                       }, 0                               },
1043 };
1044
1045 static void atifs_enable(IWineD3DDevice *iface, BOOL enable) {
1046     if(enable) {
1047         glEnable(GL_FRAGMENT_SHADER_ATI);
1048         checkGLcall("glEnable(GL_FRAGMENT_SHADER_ATI)");
1049     } else {
1050         glDisable(GL_FRAGMENT_SHADER_ATI);
1051         checkGLcall("glDisable(GL_FRAGMENT_SHADER_ATI)");
1052     }
1053 }
1054
1055 static void atifs_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps)
1056 {
1057     caps->TextureOpCaps =  WINED3DTEXOPCAPS_DISABLE                     |
1058                            WINED3DTEXOPCAPS_SELECTARG1                  |
1059                            WINED3DTEXOPCAPS_SELECTARG2                  |
1060                            WINED3DTEXOPCAPS_MODULATE4X                  |
1061                            WINED3DTEXOPCAPS_MODULATE2X                  |
1062                            WINED3DTEXOPCAPS_MODULATE                    |
1063                            WINED3DTEXOPCAPS_ADDSIGNED2X                 |
1064                            WINED3DTEXOPCAPS_ADDSIGNED                   |
1065                            WINED3DTEXOPCAPS_ADD                         |
1066                            WINED3DTEXOPCAPS_SUBTRACT                    |
1067                            WINED3DTEXOPCAPS_ADDSMOOTH                   |
1068                            WINED3DTEXOPCAPS_BLENDCURRENTALPHA           |
1069                            WINED3DTEXOPCAPS_BLENDFACTORALPHA            |
1070                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHA           |
1071                            WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA           |
1072                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM         |
1073                            WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR      |
1074                            WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA      |
1075                            WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA   |
1076                            WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR   |
1077                            WINED3DTEXOPCAPS_DOTPRODUCT3                 |
1078                            WINED3DTEXOPCAPS_MULTIPLYADD                 |
1079                            WINED3DTEXOPCAPS_LERP                        |
1080                            WINED3DTEXOPCAPS_BUMPENVMAP;
1081
1082     /* TODO: Implement WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1083     and WINED3DTEXOPCAPS_PREMODULATE */
1084
1085     /* GL_ATI_fragment_shader always supports 6 textures, which was the limit on r200 cards
1086      * which this extension is exclusively focused on(later cards have GL_ARB_fragment_program).
1087      * If the current card has more than 8 fixed function textures in OpenGL's regular fixed
1088      * function pipeline then the ATI_fragment_shader backend imposes a stricter limit. This
1089      * shouldn't be too hard since Nvidia cards have a limit of 4 textures with the default ffp
1090      * pipeline, and almost all games are happy with that. We can however support up to 8
1091      * texture stages because we have a 2nd pass limit of 8 instructions, and per stage we use
1092      * only 1 instruction.
1093      *
1094      * The proper fix for this is not to use GL_ATI_fragment_shader on cards newer than the
1095      * r200 series and use an ARB or GLSL shader instead
1096      */
1097     caps->MaxTextureBlendStages   = 8;
1098     caps->MaxSimultaneousTextures = 6;
1099
1100     caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
1101 }
1102
1103 static HRESULT atifs_alloc(IWineD3DDevice *iface) {
1104     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1105     struct atifs_private_data *priv;
1106
1107     This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct atifs_private_data));
1108     if(!This->fragment_priv) {
1109         ERR("Out of memory\n");
1110         return E_OUTOFMEMORY;
1111     }
1112     priv = This->fragment_priv;
1113     priv->fragment_shaders = hash_table_create(ffp_frag_program_key_hash, ffp_frag_program_key_compare);
1114     return WINED3D_OK;
1115 }
1116
1117 #define GLINFO_LOCATION This->adapter->gl_info
1118 static void atifs_free_ffpshader(void *value, void *device) {
1119     IWineD3DDeviceImpl *This = device;
1120     struct atifs_ffp_desc *entry_ati = value;
1121
1122     ENTER_GL();
1123     GL_EXTCALL(glDeleteFragmentShaderATI(entry_ati->shader));
1124     checkGLcall("glDeleteFragmentShaderATI(entry->shader)");
1125     HeapFree(GetProcessHeap(), 0, entry_ati);
1126     LEAVE_GL();
1127 }
1128
1129 static void atifs_free(IWineD3DDevice *iface) {
1130     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1131     struct atifs_private_data *priv = This->fragment_priv;
1132
1133     hash_table_destroy(priv->fragment_shaders, atifs_free_ffpshader, This);
1134
1135     HeapFree(GetProcessHeap(), 0, priv);
1136     This->fragment_priv = NULL;
1137 }
1138 #undef GLINFO_LOCATION
1139
1140 static BOOL atifs_color_fixup_supported(struct color_fixup_desc fixup)
1141 {
1142     if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
1143     {
1144         TRACE("Checking support for fixup:\n");
1145         dump_color_fixup_desc(fixup);
1146     }
1147
1148     /* We only support sign fixup of the first two channels. */
1149     if (fixup.x_source == CHANNEL_SOURCE_X && fixup.y_source == CHANNEL_SOURCE_Y
1150             && fixup.z_source == CHANNEL_SOURCE_Z && fixup.w_source == CHANNEL_SOURCE_W
1151             && !fixup.z_sign_fixup && !fixup.w_sign_fixup)
1152     {
1153         TRACE("[OK]\n");
1154         return TRUE;
1155     }
1156
1157     TRACE("[FAILED]\n");
1158     return FALSE;
1159 }
1160
1161 const struct fragment_pipeline atifs_fragment_pipeline = {
1162     atifs_enable,
1163     atifs_get_caps,
1164     atifs_alloc,
1165     atifs_free,
1166     atifs_color_fixup_supported,
1167     atifs_fragmentstate_template,
1168     TRUE /* We can disable projected textures */
1169 };