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