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