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