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 <stdio.h>
25 
26 #include "wined3d_private.h"
27 
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
29 WINE_DECLARE_DEBUG_CHANNEL(d3d);
30 
31 /* Context activation for state handlers is done by the caller. */
32 
33 /* Some private defines, Constant associations, etc.
34  * Env bump matrix and per stage constant should be independent,
35  * a stage that bump maps can't read the per state constant
36  */
37 #define ATIFS_CONST_BUMPMAT(i)  (GL_CON_0_ATI + i)
38 #define ATIFS_CONST_STAGE(i)    (GL_CON_0_ATI + i)
39 #define ATIFS_CONST_TFACTOR     GL_CON_6_ATI
40 
41 enum atifs_constant_value
42 {
43     ATIFS_CONSTANT_UNUSED = 0,
44     ATIFS_CONSTANT_BUMP,
45     ATIFS_CONSTANT_TFACTOR,
46     ATIFS_CONSTANT_STAGE,
47 };
48 
49 /* GL_ATI_fragment_shader specific fixed function pipeline description. "Inherits" from the common one */
50 struct atifs_ffp_desc
51 {
52     struct ffp_frag_desc parent;
53     GLuint shader;
54     unsigned int num_textures_used;
55     enum atifs_constant_value constants[MAX_TEXTURES];
56 };
57 
58 struct atifs_private_data
59 {
60     struct wine_rb_tree fragment_shaders; /* A rb-tree to track fragment pipeline replacement shaders */
61 };
62 
63 struct atifs_context_private_data
64 {
65     const struct atifs_ffp_desc *last_shader;
66 };
67 
68 static const char *debug_dstmod(GLuint mod) {
69     switch(mod) {
70         case GL_NONE:               return "GL_NONE";
71         case GL_2X_BIT_ATI:         return "GL_2X_BIT_ATI";
72         case GL_4X_BIT_ATI:         return "GL_4X_BIT_ATI";
73         case GL_8X_BIT_ATI:         return "GL_8X_BIT_ATI";
74         case GL_HALF_BIT_ATI:       return "GL_HALF_BIT_ATI";
75         case GL_QUARTER_BIT_ATI:    return "GL_QUARTER_BIT_ATI";
76         case GL_EIGHTH_BIT_ATI:     return "GL_EIGHTH_BIT_ATI";
77         case GL_SATURATE_BIT_ATI:   return "GL_SATURATE_BIT_ATI";
78         default:                    return "Unexpected modifier\n";
79     }
80 }
81 
82 static const char *debug_argmod(GLuint mod) {
83     switch(mod) {
84         case GL_NONE:
85             return "GL_NONE";
86 
87         case GL_2X_BIT_ATI:
88             return "GL_2X_BIT_ATI";
89         case GL_COMP_BIT_ATI:
90             return "GL_COMP_BIT_ATI";
91         case GL_NEGATE_BIT_ATI:
92             return "GL_NEGATE_BIT_ATI";
93         case GL_BIAS_BIT_ATI:
94             return "GL_BIAS_BIT_ATI";
95 
96         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI:
97             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI";
98         case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI:
99             return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI";
100         case GL_2X_BIT_ATI | GL_BIAS_BIT_ATI:
101             return "GL_2X_BIT_ATI | GL_BIAS_BIT_ATI";
102         case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
103             return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
104         case GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
105             return "GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
106         case GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
107             return "GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
108 
109         case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
110             return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
111         case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
112             return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
113         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
114             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
115         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
116             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
117 
118         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
119             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
120 
121         default:
122             return "Unexpected argmod combination\n";
123     }
124 }
125 static const char *debug_register(GLuint reg) {
126     switch(reg) {
127         case GL_REG_0_ATI:                  return "GL_REG_0_ATI";
128         case GL_REG_1_ATI:                  return "GL_REG_1_ATI";
129         case GL_REG_2_ATI:                  return "GL_REG_2_ATI";
130         case GL_REG_3_ATI:                  return "GL_REG_3_ATI";
131         case GL_REG_4_ATI:                  return "GL_REG_4_ATI";
132         case GL_REG_5_ATI:                  return "GL_REG_5_ATI";
133 
134         case GL_CON_0_ATI:                  return "GL_CON_0_ATI";
135         case GL_CON_1_ATI:                  return "GL_CON_1_ATI";
136         case GL_CON_2_ATI:                  return "GL_CON_2_ATI";
137         case GL_CON_3_ATI:                  return "GL_CON_3_ATI";
138         case GL_CON_4_ATI:                  return "GL_CON_4_ATI";
139         case GL_CON_5_ATI:                  return "GL_CON_5_ATI";
140         case GL_CON_6_ATI:                  return "GL_CON_6_ATI";
141         case GL_CON_7_ATI:                  return "GL_CON_7_ATI";
142 
143         case GL_ZERO:                       return "GL_ZERO";
144         case GL_ONE:                        return "GL_ONE";
145         case GL_PRIMARY_COLOR:              return "GL_PRIMARY_COLOR";
146         case GL_SECONDARY_INTERPOLATOR_ATI: return "GL_SECONDARY_INTERPOLATOR_ATI";
147 
148         default:                            return "Unknown register\n";
149     }
150 }
151 
152 static const char *debug_swizzle(GLuint swizzle) {
153     switch(swizzle) {
154         case GL_SWIZZLE_STR_ATI:        return "GL_SWIZZLE_STR_ATI";
155         case GL_SWIZZLE_STQ_ATI:        return "GL_SWIZZLE_STQ_ATI";
156         case GL_SWIZZLE_STR_DR_ATI:     return "GL_SWIZZLE_STR_DR_ATI";
157         case GL_SWIZZLE_STQ_DQ_ATI:     return "GL_SWIZZLE_STQ_DQ_ATI";
158         default:                        return "unknown swizzle";
159     }
160 }
161 
162 static const char *debug_rep(GLuint rep) {
163     switch(rep) {
164         case GL_NONE:                   return "GL_NONE";
165         case GL_RED:                    return "GL_RED";
166         case GL_GREEN:                  return "GL_GREEN";
167         case GL_BLUE:                   return "GL_BLUE";
168         case GL_ALPHA:                  return "GL_ALPHA";
169         default:                        return "unknown argrep";
170     }
171 }
172 
173 static const char *debug_op(GLuint op) {
174     switch(op) {
175         case GL_MOV_ATI:                return "GL_MOV_ATI";
176         case GL_ADD_ATI:                return "GL_ADD_ATI";
177         case GL_MUL_ATI:                return "GL_MUL_ATI";
178         case GL_SUB_ATI:                return "GL_SUB_ATI";
179         case GL_DOT3_ATI:               return "GL_DOT3_ATI";
180         case GL_DOT4_ATI:               return "GL_DOT4_ATI";
181         case GL_MAD_ATI:                return "GL_MAD_ATI";
182         case GL_LERP_ATI:               return "GL_LERP_ATI";
183         case GL_CND_ATI:                return "GL_CND_ATI";
184         case GL_CND0_ATI:               return "GL_CND0_ATI";
185         case GL_DOT2_ADD_ATI:           return "GL_DOT2_ADD_ATI";
186         default:                        return "unexpected op";
187     }
188 }
189 
190 static const char *debug_mask(GLuint mask) {
191     switch(mask) {
192         case GL_NONE:                           return "GL_NONE";
193         case GL_RED_BIT_ATI:                    return "GL_RED_BIT_ATI";
194         case GL_GREEN_BIT_ATI:                  return "GL_GREEN_BIT_ATI";
195         case GL_BLUE_BIT_ATI:                   return "GL_BLUE_BIT_ATI";
196         case GL_RED_BIT_ATI | GL_GREEN_BIT_ATI: return "GL_RED_BIT_ATI | GL_GREEN_BIT_ATI";
197         case GL_RED_BIT_ATI | GL_BLUE_BIT_ATI:  return "GL_RED_BIT_ATI | GL_BLUE_BIT_ATI";
198         case GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI:return "GL_GREEN_BIT_ATI | GL_BLUE_BIT_ATI";
199         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";
200         default:                                return "Unexpected writemask";
201     }
202 }
203 
204 static void wrap_op1(const struct wined3d_gl_info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
205         GLuint arg1, GLuint arg1Rep, GLuint arg1Mod)
206 {
207     if(dstMask == GL_ALPHA) {
208         TRACE("glAlphaFragmentOp1ATI(%s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
209               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod));
210         GL_EXTCALL(glAlphaFragmentOp1ATI(op, dst, dstMod, arg1, arg1Rep, arg1Mod));
211     } else {
212         TRACE("glColorFragmentOp1ATI(%s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
213               debug_mask(dstMask), debug_dstmod(dstMod),
214               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod));
215         GL_EXTCALL(glColorFragmentOp1ATI(op, dst, dstMask, dstMod, arg1, arg1Rep, arg1Mod));
216     }
217 }
218 
219 static void wrap_op2(const struct wined3d_gl_info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
220         GLuint arg1, GLuint arg1Rep, GLuint arg1Mod, GLuint arg2, GLuint arg2Rep, GLuint arg2Mod)
221 {
222     if(dstMask == GL_ALPHA) {
223         TRACE("glAlphaFragmentOp2ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
224               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
225               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod));
226         GL_EXTCALL(glAlphaFragmentOp2ATI(op, dst, dstMod, arg1, arg1Rep, arg1Mod, arg2, arg2Rep, arg2Mod));
227     } else {
228         TRACE("glColorFragmentOp2ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
229               debug_mask(dstMask), debug_dstmod(dstMod),
230               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
231               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod));
232         GL_EXTCALL(glColorFragmentOp2ATI(op, dst, dstMask, dstMod, arg1, arg1Rep, arg1Mod, arg2, arg2Rep, arg2Mod));
233     }
234 }
235 
236 static void wrap_op3(const struct wined3d_gl_info *gl_info, GLuint op, GLuint dst, GLuint dstMask, GLuint dstMod,
237         GLuint arg1, GLuint arg1Rep, GLuint arg1Mod, GLuint arg2, GLuint arg2Rep, GLuint arg2Mod,
238         GLuint arg3, GLuint arg3Rep, GLuint arg3Mod)
239 {
240     if(dstMask == GL_ALPHA) {
241         /* Leave some free space to fit "GL_NONE, " in to align most alpha and color op lines */
242         TRACE("glAlphaFragmentOp3ATI(%s, %s,          %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst), debug_dstmod(dstMod),
243               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
244               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod),
245               debug_register(arg3), debug_rep(arg3Rep), debug_argmod(arg3Mod));
246         GL_EXTCALL(glAlphaFragmentOp3ATI(op, dst, dstMod,
247                                          arg1, arg1Rep, arg1Mod,
248                                          arg2, arg2Rep, arg2Mod,
249                                          arg3, arg3Rep, arg3Mod));
250     } else {
251         TRACE("glColorFragmentOp3ATI(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n", debug_op(op), debug_register(dst),
252               debug_mask(dstMask), debug_dstmod(dstMod),
253               debug_register(arg1), debug_rep(arg1Rep), debug_argmod(arg1Mod),
254               debug_register(arg2), debug_rep(arg2Rep), debug_argmod(arg2Mod),
255               debug_register(arg3), debug_rep(arg3Rep), debug_argmod(arg3Mod));
256         GL_EXTCALL(glColorFragmentOp3ATI(op, dst, dstMask, dstMod,
257                                          arg1, arg1Rep, arg1Mod,
258                                          arg2, arg2Rep, arg2Mod,
259                                          arg3, arg3Rep, arg3Mod));
260     }
261 }
262 
263 static GLuint register_for_arg(DWORD arg, const struct wined3d_gl_info *gl_info,
264         unsigned int stage, GLuint *mod, GLuint *rep, GLuint tmparg)
265 {
266     GLenum ret;
267 
268     if(mod) *mod = GL_NONE;
269     if(arg == ARG_UNUSED)
270     {
271         if (rep) *rep = GL_NONE;
272         return -1; /* This is the marker for unused registers */
273     }
274 
275     switch(arg & WINED3DTA_SELECTMASK) {
276         case WINED3DTA_DIFFUSE:
277             ret = GL_PRIMARY_COLOR;
278             break;
279 
280         case WINED3DTA_CURRENT:
281             /* Note that using GL_REG_0_ATI for the passed on register is safe because
282              * texture0 is read at stage0, so in the worst case it is read in the
283              * instruction writing to reg0. Afterwards texture0 is not used any longer.
284              * If we're reading from current
285              */
286             ret = stage ? GL_REG_0_ATI : GL_PRIMARY_COLOR;
287             break;
288 
289         case WINED3DTA_TEXTURE:
290             ret = GL_REG_0_ATI + stage;
291             break;
292 
293         case WINED3DTA_TFACTOR:
294             ret = ATIFS_CONST_TFACTOR;
295             break;
296 
297         case WINED3DTA_SPECULAR:
298             ret = GL_SECONDARY_INTERPOLATOR_ATI;
299             break;
300 
301         case WINED3DTA_TEMP:
302             ret = tmparg;
303             break;
304 
305         case WINED3DTA_CONSTANT:
306             ret = ATIFS_CONST_STAGE(stage);
307             break;
308 
309         default:
310             FIXME("Unknown source argument %d\n", arg);
311             ret = GL_ZERO;
312     }
313 
314     if(arg & WINED3DTA_COMPLEMENT) {
315         if(mod) *mod |= GL_COMP_BIT_ATI;
316     }
317     if(arg & WINED3DTA_ALPHAREPLICATE) {
318         if(rep) *rep = GL_ALPHA;
319     } else {
320         if(rep) *rep = GL_NONE;
321     }
322     return ret;
323 }
324 
325 static GLuint find_tmpreg(const struct texture_stage_op op[MAX_TEXTURES])
326 {
327     int lowest_read = -1;
328     int lowest_write = -1;
329     int i;
330     BOOL tex_used[MAX_TEXTURES];
331 
332     memset(tex_used, 0, sizeof(tex_used));
333     for (i = 0; i < MAX_TEXTURES; ++i)
334     {
335         if (op[i].cop == WINED3D_TOP_DISABLE)
336             break;
337 
338         if(lowest_read == -1 &&
339           (op[i].carg1 == WINED3DTA_TEMP || op[i].carg2 == WINED3DTA_TEMP || op[i].carg0 == WINED3DTA_TEMP ||
340            op[i].aarg1 == WINED3DTA_TEMP || op[i].aarg2 == WINED3DTA_TEMP || op[i].aarg0 == WINED3DTA_TEMP)) {
341             lowest_read = i;
342         }
343 
344         if(lowest_write == -1 && op[i].dst == tempreg) {
345             lowest_write = i;
346         }
347 
348         if(op[i].carg1 == WINED3DTA_TEXTURE || op[i].carg2 == WINED3DTA_TEXTURE || op[i].carg0 == WINED3DTA_TEXTURE ||
349            op[i].aarg1 == WINED3DTA_TEXTURE || op[i].aarg2 == WINED3DTA_TEXTURE || op[i].aarg0 == WINED3DTA_TEXTURE) {
350             tex_used[i] = TRUE;
351         }
352     }
353 
354     /* Temp reg not read? We don't need it, return GL_NONE */
355     if(lowest_read == -1) return GL_NONE;
356 
357     if(lowest_write >= lowest_read) {
358         FIXME("Temp register read before being written\n");
359     }
360 
361     if(lowest_write == -1) {
362         /* This needs a test. Maybe we are supposed to return 0.0/0.0/0.0/0.0, or fail drawprim, or whatever */
363         FIXME("Temp register read without being written\n");
364         return GL_REG_1_ATI;
365     } else if(lowest_write >= 1) {
366         /* If we're writing to the temp reg at earliest in stage 1, we can use register 1 for the temp result.
367          * there may be texture data stored in reg 1, but we do not need it any longer since stage 1 already
368          * read it
369          */
370         return GL_REG_1_ATI;
371     } else {
372         /* Search for a free texture register. We have 6 registers available. GL_REG_0_ATI is already used
373          * for the regular result
374          */
375         for(i = 1; i < 6; i++) {
376             if(!tex_used[i]) {
377                 return GL_REG_0_ATI + i;
378             }
379         }
380         /* What to do here? Report it in ValidateDevice? */
381         FIXME("Could not find a register for the temporary register\n");
382         return 0;
383     }
384 }
385 
386 static const struct color_fixup_desc color_fixup_rg =
387 {
388     1, CHANNEL_SOURCE_X,
389     1, CHANNEL_SOURCE_Y,
390     0, CHANNEL_SOURCE_ONE,
391     0, CHANNEL_SOURCE_ONE
392 };
393 static const struct color_fixup_desc color_fixup_rgl =
394 {
395     1, CHANNEL_SOURCE_X,
396     1, CHANNEL_SOURCE_Y,
397     0, CHANNEL_SOURCE_Z,
398     0, CHANNEL_SOURCE_W
399 };
400 static const struct color_fixup_desc color_fixup_rgba =
401 {
402     1, CHANNEL_SOURCE_X,
403     1, CHANNEL_SOURCE_Y,
404     1, CHANNEL_SOURCE_Z,
405     1, CHANNEL_SOURCE_W
406 };
407 
408 static BOOL op_reads_texture(const struct texture_stage_op *op)
409 {
410     return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
411             || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
412             || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
413             || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
414             || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
415             || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
416             || op->cop == WINED3D_TOP_BLEND_TEXTURE_ALPHA;
417 }
418 
419 static void atifs_color_fixup(const struct wined3d_gl_info *gl_info, struct color_fixup_desc fixup, GLuint reg)
420 {
421     if(is_same_fixup(fixup, color_fixup_rg))
422     {
423         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_RED_BIT_ATI | GL_GREEN_BIT_ATI, GL_NONE,
424                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
425         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_BLUE_BIT_ATI, GL_NONE,
426                 GL_ONE, GL_NONE, GL_NONE);
427         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_ALPHA, GL_NONE,
428                 GL_ONE, GL_NONE, GL_NONE);
429     }
430     else if(is_same_fixup(fixup, color_fixup_rgl))
431     {
432         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_RED_BIT_ATI | GL_GREEN_BIT_ATI, GL_NONE,
433                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
434     }
435     else if (is_same_fixup(fixup, color_fixup_rgba))
436     {
437         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_NONE, GL_NONE,
438                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
439         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_ALPHA, GL_NONE,
440                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
441     }
442     else
443     {
444         /* Should not happen - atifs_color_fixup_supported refuses other fixups. */
445         ERR("Unsupported color fixup.\n");
446     }
447 }
448 
449 static BOOL op_reads_tfactor(const struct texture_stage_op *op)
450 {
451     return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
452             || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
453             || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
454             || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
455             || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
456             || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
457             || op->cop == WINED3D_TOP_BLEND_FACTOR_ALPHA
458             || op->aop == WINED3D_TOP_BLEND_FACTOR_ALPHA;
459 }
460 
461 static BOOL op_reads_constant(const struct texture_stage_op *op)
462 {
463     return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
464             || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
465             || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
466             || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
467             || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
468             || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT;
469 }
470 
471 static GLuint gen_ati_shader(const struct texture_stage_op op[MAX_TEXTURES],
472         const struct wined3d_gl_info *gl_info, enum atifs_constant_value *constants)
473 {
474     GLuint ret = GL_EXTCALL(glGenFragmentShadersATI(1));
475     unsigned int stage;
476     GLuint arg0, arg1, arg2, extrarg;
477     GLuint dstmod, argmod0, argmod1, argmod2, argmodextra;
478     GLuint rep0, rep1, rep2;
479     GLuint swizzle;
480     GLuint tmparg = find_tmpreg(op);
481     GLuint dstreg;
482     BOOL tfactor_used = FALSE;
483 
484     if(!ret) {
485         ERR("Failed to generate a GL_ATI_fragment_shader shader id\n");
486         return 0;
487     }
488     GL_EXTCALL(glBindFragmentShaderATI(ret));
489     checkGLcall("GL_EXTCALL(glBindFragmentShaderATI(ret))");
490 
491     TRACE("glBeginFragmentShaderATI()\n");
492     GL_EXTCALL(glBeginFragmentShaderATI());
493     checkGLcall("GL_EXTCALL(glBeginFragmentShaderATI())");
494 
495     /* Pass 1: Generate sampling instructions for perturbation maps */
496     for (stage = 0; stage < gl_info->limits.textures; ++stage)
497     {
498         if (op[stage].cop == WINED3D_TOP_DISABLE)
499             break;
500         if (op[stage].cop != WINED3D_TOP_BUMPENVMAP
501                 && op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
502             continue;
503 
504         constants[stage] = ATIFS_CONSTANT_BUMP;
505 
506         TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, GL_SWIZZLE_STR_ATI)\n",
507               stage, stage);
508         GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
509                    GL_TEXTURE0_ARB + stage,
510                    GL_SWIZZLE_STR_ATI));
511         if(op[stage + 1].projected == proj_none) {
512             swizzle = GL_SWIZZLE_STR_ATI;
513         } else if(op[stage + 1].projected == proj_count4) {
514             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
515         } else {
516             swizzle = GL_SWIZZLE_STR_DR_ATI;
517         }
518         TRACE("glPassTexCoordATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
519               stage + 1, stage + 1, debug_swizzle(swizzle));
520         GL_EXTCALL(glPassTexCoordATI(GL_REG_0_ATI + stage + 1,
521                    GL_TEXTURE0_ARB + stage + 1,
522                    swizzle));
523     }
524 
525     /* Pass 2: Generate perturbation calculations */
526     for (stage = 0; stage < gl_info->limits.textures; ++stage)
527     {
528         GLuint argmodextra_x, argmodextra_y;
529         struct color_fixup_desc fixup;
530 
531         if (op[stage].cop == WINED3D_TOP_DISABLE)
532             break;
533         if (op[stage].cop != WINED3D_TOP_BUMPENVMAP
534                 && op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
535             continue;
536 
537         fixup = op[stage].color_fixup;
538         if (fixup.x_source != CHANNEL_SOURCE_X || fixup.y_source != CHANNEL_SOURCE_Y)
539         {
540             FIXME("Swizzles not implemented\n");
541             argmodextra_x = GL_NONE;
542             argmodextra_y = GL_NONE;
543         }
544         else
545         {
546             /* Nice thing, we get the color correction for free :-) */
547             argmodextra_x = fixup.x_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
548             argmodextra_y = fixup.y_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
549         }
550 
551         wrap_op3(gl_info, GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_RED_BIT_ATI, GL_NONE,
552                  GL_REG_0_ATI + stage, GL_NONE, argmodextra_x,
553                  ATIFS_CONST_BUMPMAT(stage), GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
554                  GL_REG_0_ATI + stage + 1, GL_RED, GL_NONE);
555 
556         /* Don't use GL_DOT2_ADD_ATI here because we cannot configure it to read the blue and alpha
557          * component of the bump matrix. Instead do this with two MADs:
558          *
559          * coord.a = tex.r * bump.b + coord.g
560          * coord.g = tex.g * bump.a + coord.a
561          *
562          * The first instruction writes to alpha so it can be coissued with the above DOT2_ADD.
563          * coord.a is unused. If the perturbed texture is projected, this was already handled
564          * in the glPassTexCoordATI above.
565          */
566         wrap_op3(gl_info, GL_MAD_ATI, GL_REG_0_ATI + stage + 1, GL_ALPHA, GL_NONE,
567                  GL_REG_0_ATI + stage, GL_RED, argmodextra_y,
568                  ATIFS_CONST_BUMPMAT(stage), GL_BLUE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
569                  GL_REG_0_ATI + stage + 1, GL_GREEN, GL_NONE);
570         wrap_op3(gl_info, GL_MAD_ATI, GL_REG_0_ATI + stage + 1, GL_GREEN_BIT_ATI, GL_NONE,
571                  GL_REG_0_ATI + stage, GL_GREEN, argmodextra_y,
572                  ATIFS_CONST_BUMPMAT(stage), GL_ALPHA, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
573                  GL_REG_0_ATI + stage + 1, GL_ALPHA, GL_NONE);
574     }
575 
576     /* Pass 3: Generate sampling instructions for regular textures */
577     for (stage = 0; stage < gl_info->limits.textures; ++stage)
578     {
579         if (op[stage].cop == WINED3D_TOP_DISABLE)
580             break;
581 
582         if(op[stage].projected == proj_none) {
583             swizzle = GL_SWIZZLE_STR_ATI;
584         } else if(op[stage].projected == proj_count3) {
585             swizzle = GL_SWIZZLE_STR_DR_ATI;
586         } else {
587             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
588         }
589 
590         if (op_reads_texture(&op[stage]))
591         {
592             if (stage > 0
593                     && (op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
594                     || op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
595             {
596                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_REG_%d_ATI, GL_SWIZZLE_STR_ATI)\n",
597                       stage, stage);
598                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
599                            GL_REG_0_ATI + stage,
600                            GL_SWIZZLE_STR_ATI));
601             } else {
602                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
603                     stage, stage, debug_swizzle(swizzle));
604                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
605                                         GL_TEXTURE0_ARB + stage,
606                                         swizzle));
607             }
608         }
609     }
610 
611     /* Pass 4: Generate the arithmetic instructions */
612     for (stage = 0; stage < MAX_TEXTURES; ++stage)
613     {
614         if (op[stage].cop == WINED3D_TOP_DISABLE)
615         {
616             if (!stage)
617             {
618                 /* Handle complete texture disabling gracefully */
619                 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
620                          GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
621                 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
622                          GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
623             }
624             break;
625         }
626 
627         if(op[stage].dst == tempreg) {
628             /* If we're writing to D3DTA_TEMP, but never reading from it we don't have to write there in the first place.
629              * skip the entire stage, this saves some GPU time
630              */
631             if(tmparg == GL_NONE) continue;
632 
633             dstreg = tmparg;
634         } else {
635             dstreg = GL_REG_0_ATI;
636         }
637 
638         if (op[stage].cop == WINED3D_TOP_BUMPENVMAP || op[stage].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
639         {
640             /* Those are handled in the first pass of the shader (generation pass 1 and 2) already */
641             continue;
642         }
643 
644         arg0 = register_for_arg(op[stage].carg0, gl_info, stage, &argmod0, &rep0, tmparg);
645         arg1 = register_for_arg(op[stage].carg1, gl_info, stage, &argmod1, &rep1, tmparg);
646         arg2 = register_for_arg(op[stage].carg2, gl_info, stage, &argmod2, &rep2, tmparg);
647         dstmod = GL_NONE;
648         argmodextra = GL_NONE;
649         extrarg = GL_NONE;
650 
651         if (op_reads_tfactor(&op[stage]))
652             tfactor_used = TRUE;
653 
654         if (op_reads_constant(&op[stage]))
655         {
656             if (constants[stage] != ATIFS_CONSTANT_UNUSED)
657                 FIXME("Constant %u already used.\n", stage);
658             constants[stage] = ATIFS_CONSTANT_STAGE;
659         }
660 
661         if (op_reads_texture(&op[stage]) && !is_identity_fixup(op[stage].color_fixup))
662             atifs_color_fixup(gl_info, op[stage].color_fixup, GL_REG_0_ATI + stage);
663 
664         switch (op[stage].cop)
665         {
666             case WINED3D_TOP_SELECT_ARG2:
667                 arg1 = arg2;
668                 argmod1 = argmod2;
669                 rep1 = rep2;
670                 /* fall through */
671             case WINED3D_TOP_SELECT_ARG1:
672                 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_NONE, GL_NONE,
673                          arg1, rep1, argmod1);
674                 break;
675 
676             case WINED3D_TOP_MODULATE_4X:
677                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
678                 /* fall through */
679             case WINED3D_TOP_MODULATE_2X:
680                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
681                 dstmod |= GL_SATURATE_BIT_ATI;
682                 /* fall through */
683             case WINED3D_TOP_MODULATE:
684                 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_NONE, dstmod,
685                          arg1, rep1, argmod1,
686                          arg2, rep2, argmod2);
687                 break;
688 
689             case WINED3D_TOP_ADD_SIGNED_2X:
690                 dstmod = GL_2X_BIT_ATI;
691                 /* fall through */
692             case WINED3D_TOP_ADD_SIGNED:
693                 argmodextra = GL_BIAS_BIT_ATI;
694                 /* fall through */
695             case WINED3D_TOP_ADD:
696                 dstmod |= GL_SATURATE_BIT_ATI;
697                 wrap_op2(gl_info, GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, dstmod,
698                          arg1, rep1, argmod1,
699                          arg2, rep2, argmodextra | argmod2);
700                 break;
701 
702             case WINED3D_TOP_SUBTRACT:
703                 dstmod |= GL_SATURATE_BIT_ATI;
704                 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_NONE, dstmod,
705                          arg1, rep1, argmod1,
706                          arg2, rep2, argmod2);
707                 break;
708 
709             case WINED3D_TOP_ADD_SMOOTH:
710                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
711                 /* Dst = arg1 + * arg2(1 -arg 1)
712                  *     = arg2 * (1 - arg1) + arg1
713                  */
714                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
715                          arg2, rep2, argmod2,
716                          arg1, rep1, argmodextra,
717                          arg1, rep1, argmod1);
718                 break;
719 
720             case WINED3D_TOP_BLEND_CURRENT_ALPHA:
721                 if (extrarg == GL_NONE)
722                     extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
723                 /* fall through */
724             case WINED3D_TOP_BLEND_FACTOR_ALPHA:
725                 if (extrarg == GL_NONE)
726                     extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
727                 /* fall through */
728             case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
729                 if (extrarg == GL_NONE)
730                     extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
731                 /* fall through */
732             case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
733                 if (extrarg == GL_NONE)
734                     extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
735                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
736                          extrarg, GL_ALPHA, GL_NONE,
737                          arg1, rep1, argmod1,
738                          arg2, rep2, argmod2);
739                 break;
740 
741             case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
742                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
743                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_NONE,
744                          arg2, rep2,  argmod2,
745                          arg0, GL_ALPHA, GL_COMP_BIT_ATI,
746                          arg1, rep1,  argmod1);
747                 break;
748 
749             /* D3DTOP_PREMODULATE ???? */
750 
751             case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
752                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
753                 /* fall through */
754             case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
755                 if (!argmodextra)
756                     argmodextra = argmod1;
757                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
758                          arg2, rep2,  argmod2,
759                          arg1, GL_ALPHA, argmodextra,
760                          arg1, rep1,  argmod1);
761                 break;
762 
763             case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
764                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
765                 /* fall through */
766             case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
767                 if (!argmodextra)
768                     argmodextra = argmod1;
769                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
770                          arg2, rep2,  argmod2,
771                          arg1, rep1,  argmodextra,
772                          arg1, GL_ALPHA, argmod1);
773                 break;
774 
775             case WINED3D_TOP_DOTPRODUCT3:
776                 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
777                          arg1, rep1, argmod1 | GL_BIAS_BIT_ATI,
778                          arg2, rep2, argmod2 | GL_BIAS_BIT_ATI);
779                 break;
780 
781             case WINED3D_TOP_MULTIPLY_ADD:
782                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
783                          arg1, rep1, argmod1,
784                          arg2, rep2, argmod2,
785                          arg0, rep0, argmod0);
786                 break;
787 
788             case WINED3D_TOP_LERP:
789                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
790                          arg0, rep0, argmod0,
791                          arg1, rep1, argmod1,
792                          arg2, rep2, argmod2);
793                 break;
794 
795             default: FIXME("Unhandled color operation %d on stage %d\n", op[stage].cop, stage);
796         }
797 
798         arg0 = register_for_arg(op[stage].aarg0, gl_info, stage, &argmod0, NULL, tmparg);
799         arg1 = register_for_arg(op[stage].aarg1, gl_info, stage, &argmod1, NULL, tmparg);
800         arg2 = register_for_arg(op[stage].aarg2, gl_info, stage, &argmod2, NULL, tmparg);
801         dstmod = GL_NONE;
802         argmodextra = GL_NONE;
803         extrarg = GL_NONE;
804 
805         switch (op[stage].aop)
806         {
807             case WINED3D_TOP_DISABLE:
808                 /* Get the primary color to the output if on stage 0, otherwise leave register 0 untouched */
809                 if (!stage)
810                 {
811                     wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
812                              GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
813                 }
814                 break;
815 
816             case WINED3D_TOP_SELECT_ARG2:
817                 arg1 = arg2;
818                 argmod1 = argmod2;
819                 /* fall through */
820             case WINED3D_TOP_SELECT_ARG1:
821                 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_ALPHA, GL_NONE,
822                          arg1, GL_NONE, argmod1);
823                 break;
824 
825             case WINED3D_TOP_MODULATE_4X:
826                 if (dstmod == GL_NONE)
827                     dstmod = GL_4X_BIT_ATI;
828                 /* fall through */
829             case WINED3D_TOP_MODULATE_2X:
830                 if (dstmod == GL_NONE)
831                     dstmod = GL_2X_BIT_ATI;
832                 dstmod |= GL_SATURATE_BIT_ATI;
833                 /* fall through */
834             case WINED3D_TOP_MODULATE:
835                 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_ALPHA, dstmod,
836                          arg1, GL_NONE, argmod1,
837                          arg2, GL_NONE, argmod2);
838                 break;
839 
840             case WINED3D_TOP_ADD_SIGNED_2X:
841                 dstmod = GL_2X_BIT_ATI;
842                 /* fall through */
843             case WINED3D_TOP_ADD_SIGNED:
844                 argmodextra = GL_BIAS_BIT_ATI;
845                 /* fall through */
846             case WINED3D_TOP_ADD:
847                 dstmod |= GL_SATURATE_BIT_ATI;
848                 wrap_op2(gl_info, GL_ADD_ATI, dstreg, GL_ALPHA, dstmod,
849                          arg1, GL_NONE, argmod1,
850                          arg2, GL_NONE, argmodextra | argmod2);
851                 break;
852 
853             case WINED3D_TOP_SUBTRACT:
854                 dstmod |= GL_SATURATE_BIT_ATI;
855                 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_ALPHA, dstmod,
856                          arg1, GL_NONE, argmod1,
857                          arg2, GL_NONE, argmod2);
858                 break;
859 
860             case WINED3D_TOP_ADD_SMOOTH:
861                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
862                 /* Dst = arg1 + * arg2(1 -arg 1)
863                  *     = arg2 * (1 - arg1) + arg1
864                  */
865                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
866                          arg2, GL_NONE, argmod2,
867                          arg1, GL_NONE, argmodextra,
868                          arg1, GL_NONE, argmod1);
869                 break;
870 
871             case WINED3D_TOP_BLEND_CURRENT_ALPHA:
872                 if (extrarg == GL_NONE)
873                     extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
874                 /* fall through */
875             case WINED3D_TOP_BLEND_FACTOR_ALPHA:
876                 if (extrarg == GL_NONE)
877                     extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
878                 /* fall through */
879             case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
880                 if (extrarg == GL_NONE)
881                     extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
882                 /* fall through */
883             case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
884                 if (extrarg == GL_NONE)
885                     extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
886                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_NONE,
887                          extrarg, GL_ALPHA, GL_NONE,
888                          arg1, GL_NONE, argmod1,
889                          arg2, GL_NONE, argmod2);
890                 break;
891 
892             case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
893                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
894                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_NONE,
895                          arg2, GL_NONE,  argmod2,
896                          arg0, GL_ALPHA, GL_COMP_BIT_ATI,
897                          arg1, GL_NONE,  argmod1);
898                 break;
899 
900             /* D3DTOP_PREMODULATE ???? */
901 
902             case WINED3D_TOP_DOTPRODUCT3:
903                 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_ALPHA, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
904                          arg1, GL_NONE, argmod1 | GL_BIAS_BIT_ATI,
905                          arg2, GL_NONE, argmod2 | GL_BIAS_BIT_ATI);
906                 break;
907 
908             case WINED3D_TOP_MULTIPLY_ADD:
909                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
910                          arg1, GL_NONE, argmod1,
911                          arg2, GL_NONE, argmod2,
912                          arg0, GL_NONE, argmod0);
913                 break;
914 
915             case WINED3D_TOP_LERP:
916                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
917                          arg1, GL_NONE, argmod1,
918                          arg2, GL_NONE, argmod2,
919                          arg0, GL_NONE, argmod0);
920                 break;
921 
922             case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
923             case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
924             case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
925             case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
926             case WINED3D_TOP_BUMPENVMAP:
927             case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
928                 ERR("Application uses an invalid alpha operation\n");
929                 break;
930 
931             default: FIXME("Unhandled alpha operation %d on stage %d\n", op[stage].aop, stage);
932         }
933     }
934 
935     if (tfactor_used && constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] != ATIFS_CONSTANT_UNUSED)
936         FIXME("Texture factor constant already used.\n");
937     constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] = ATIFS_CONSTANT_TFACTOR;
938 
939     /* Assign unused constants to avoid reloading due to unused <-> bump matrix switches. */
940     for (stage = 0; stage < MAX_TEXTURES; ++stage)
941     {
942         if (constants[stage] == ATIFS_CONSTANT_UNUSED)
943             constants[stage] = ATIFS_CONSTANT_BUMP;
944     }
945 
946     TRACE("glEndFragmentShaderATI()\n");
947     GL_EXTCALL(glEndFragmentShaderATI());
948     checkGLcall("GL_EXTCALL(glEndFragmentShaderATI())");
949     return ret;
950 }
951 
952 static void atifs_tfactor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
953 {
954     const struct wined3d_gl_info *gl_info = context->gl_info;
955     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
956     struct wined3d_color color;
957 
958     if (!ctx_priv->last_shader
959             || ctx_priv->last_shader->constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] != ATIFS_CONSTANT_TFACTOR)
960         return;
961 
962     wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
963     GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_TFACTOR, &color.r));
964     checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_TFACTOR, &color.r)");
965 }
966 
967 static void set_bumpmat(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
968 {
969     DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
970     const struct wined3d_gl_info *gl_info = context->gl_info;
971     float mat[2][2];
972     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
973 
974     if (!ctx_priv->last_shader
975             || ctx_priv->last_shader->constants[stage] != ATIFS_CONSTANT_BUMP)
976         return;
977 
978     mat[0][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT00]);
979     mat[1][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT01]);
980     mat[0][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT10]);
981     mat[1][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT11]);
982     /* GL_ATI_fragment_shader allows only constants from 0.0 to 1.0, but the bumpmat
983      * constants can be in any range. While they should stay between [-1.0 and 1.0] because
984      * Shader Model 1.x pixel shaders are clamped to that range negative values are used occasionally,
985      * for example by our d3d9 test. So to get negative values scale -1;1 to 0;1 and undo that in the
986      * shader(it is free). This might potentially reduce precision. However, if the hardware does
987      * support proper floats it shouldn't, and if it doesn't we can't get anything better anyway. */
988     mat[0][0] = (mat[0][0] + 1.0f) * 0.5f;
989     mat[1][0] = (mat[1][0] + 1.0f) * 0.5f;
990     mat[0][1] = (mat[0][1] + 1.0f) * 0.5f;
991     mat[1][1] = (mat[1][1] + 1.0f) * 0.5f;
992     GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_BUMPMAT(stage), (float *) mat));
993     checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_BUMPMAT(stage), mat)");
994 }
995 
996 static void atifs_stage_constant(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
997 {
998     DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
999     const struct wined3d_gl_info *gl_info = context->gl_info;
1000     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
1001     struct wined3d_color color;
1002 
1003     if (!ctx_priv->last_shader
1004             || ctx_priv->last_shader->constants[stage] != ATIFS_CONSTANT_STAGE)
1005         return;
1006 
1007     wined3d_color_from_d3dcolor(&color, state->texture_states[stage][WINED3D_TSS_CONSTANT]);
1008     GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_STAGE(stage), &color.r));
1009     checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_STAGE(stage), &color.r)");
1010 }
1011 
1012 static void set_tex_op_atifs(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1013 {
1014     const struct wined3d_device *device = context->device;
1015     const struct wined3d_gl_info *gl_info = context->gl_info;
1016     const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1017     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
1018     const struct atifs_ffp_desc *desc, *last_shader = ctx_priv->last_shader;
1019     struct ffp_frag_settings settings;
1020     struct atifs_private_data *priv = device->fragment_priv;
1021     DWORD mapped_stage;
1022     unsigned int i;
1023 
1024     gen_ffp_frag_op(context, state, &settings, TRUE);
1025     desc = (const struct atifs_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
1026     if (!desc)
1027     {
1028         struct atifs_ffp_desc *new_desc;
1029 
1030         if (!(new_desc = heap_alloc_zero(sizeof(*new_desc))))
1031         {
1032             ERR("Out of memory\n");
1033             return;
1034         }
1035         new_desc->num_textures_used = 0;
1036         for (i = 0; i < d3d_info->limits.ffp_blend_stages; ++i)
1037         {
1038             if (settings.op[i].cop == WINED3D_TOP_DISABLE)
1039                 break;
1040             ++new_desc->num_textures_used;
1041         }
1042 
1043         new_desc->parent.settings = settings;
1044         new_desc->shader = gen_ati_shader(settings.op, gl_info, new_desc->constants);
1045         add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
1046         TRACE("Allocated fixed function replacement shader descriptor %p.\n", new_desc);
1047         desc = new_desc;
1048     }
1049 
1050     /* GL_ATI_fragment_shader depends on the GL_TEXTURE_xD enable settings. Update the texture stages
1051      * used by this shader
1052      */
1053     for (i = 0; i < desc->num_textures_used; ++i)
1054     {
1055         mapped_stage = context->tex_unit_map[i];
1056         if (mapped_stage != WINED3D_UNMAPPED_STAGE)
1057         {
1058             context_active_texture(context, gl_info, mapped_stage);
1059             texture_activate_dimensions(state->textures[i], gl_info);
1060         }
1061     }
1062 
1063     GL_EXTCALL(glBindFragmentShaderATI(desc->shader));
1064     ctx_priv->last_shader = desc;
1065 
1066     for (i = 0; i < MAX_TEXTURES; i++)
1067     {
1068         if (last_shader && last_shader->constants[i] == desc->constants[i])
1069             continue;
1070 
1071         switch (desc->constants[i])
1072         {
1073             case ATIFS_CONSTANT_BUMP:
1074                 set_bumpmat(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_BUMPENV_MAT00));
1075                 break;
1076 
1077             case ATIFS_CONSTANT_TFACTOR:
1078                 atifs_tfactor(context, state, STATE_RENDER(WINED3D_RS_TEXTUREFACTOR));
1079                 break;
1080 
1081             case ATIFS_CONSTANT_STAGE:
1082                 atifs_stage_constant(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_CONSTANT));
1083                 break;
1084 
1085             default:
1086                 ERR("Unexpected constant type %u.\n", desc->constants[i]);
1087         }
1088     }
1089 }
1090 
1091 static void textransform(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1092 {
1093     if (!isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL)))
1094         set_tex_op_atifs(context, state, state_id);
1095 }
1096 
1097 static void atifs_srgbwriteenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1098 {
1099     if (state->render_states[WINED3D_RS_SRGBWRITEENABLE])
1100         WARN("sRGB writes are not supported by this fragment pipe.\n");
1101 }
1102 
1103 static const struct StateEntryTemplate atifs_fragmentstate_template[] = {
1104     {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR),              { STATE_RENDER(WINED3D_RS_TEXTUREFACTOR),             atifs_tfactor           }, WINED3D_GL_EXT_NONE             },
1105     {STATE_RENDER(WINED3D_RS_ALPHAFUNC),                  { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                    }, WINED3D_GL_EXT_NONE             },
1106     {STATE_RENDER(WINED3D_RS_ALPHAREF),                   { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                    }, WINED3D_GL_EXT_NONE             },
1107     {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),            { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           state_alpha_test        }, WINED3D_GL_EXT_NONE             },
1108     {STATE_RENDER(WINED3D_RS_COLORKEYENABLE),             { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                    }, WINED3D_GL_EXT_NONE             },
1109     {STATE_RENDER(WINED3D_RS_FOGCOLOR),                   { STATE_RENDER(WINED3D_RS_FOGCOLOR),                  state_fogcolor          }, WINED3D_GL_EXT_NONE             },
1110     {STATE_RENDER(WINED3D_RS_FOGDENSITY),                 { STATE_RENDER(WINED3D_RS_FOGDENSITY),                state_fogdensity        }, WINED3D_GL_EXT_NONE             },
1111     {STATE_RENDER(WINED3D_RS_FOGENABLE),                  { STATE_RENDER(WINED3D_RS_FOGENABLE),                 state_fog_fragpart      }, WINED3D_GL_EXT_NONE             },
1112     {STATE_RENDER(WINED3D_RS_FOGTABLEMODE),               { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                    }, WINED3D_GL_EXT_NONE             },
1113     {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE),              { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                    }, WINED3D_GL_EXT_NONE             },
1114     {STATE_RENDER(WINED3D_RS_FOGSTART),                   { STATE_RENDER(WINED3D_RS_FOGSTART),                  state_fogstartend       }, WINED3D_GL_EXT_NONE             },
1115     {STATE_RENDER(WINED3D_RS_FOGEND),                     { STATE_RENDER(WINED3D_RS_FOGSTART),                  NULL                    }, WINED3D_GL_EXT_NONE             },
1116     {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE),            { STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE),           atifs_srgbwriteenable   }, WINED3D_GL_EXT_NONE             },
1117     {STATE_COLOR_KEY,                                     { STATE_COLOR_KEY,                                    state_nop               }, WINED3D_GL_EXT_NONE             },
1118     {STATE_RENDER(WINED3D_RS_SHADEMODE),                  { STATE_RENDER(WINED3D_RS_SHADEMODE),                 state_shademode         }, WINED3D_GL_EXT_NONE             },
1119     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1120     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1121     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1122     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1123     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1124     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1125     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1126     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1127     {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1128     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1129     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1130     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1131     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1132     {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1133     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1134     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1135     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1136     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1137     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1138     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1139     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1140     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1141     {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1142     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1143     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1144     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1145     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1146     {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1147     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1148     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1149     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1150     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1151     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1152     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1153     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1154     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1155     {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1156     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1157     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1158     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1159     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1160     {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1161     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1162     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1163     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1164     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1165     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1166     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1167     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1168     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1169     {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1170     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1171     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1172     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1173     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1174     {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1175     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1176     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1177     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1178     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1179     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1180     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1181     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1182     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1183     {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1184     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1185     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1186     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1187     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1188     {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1189     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1190     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1191     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1192     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1193     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1194     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1195     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1196     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1197     {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1198     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1199     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1200     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1201     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1202     {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1203     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1204     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1205     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1206     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1207     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1208     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1209     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1210     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1211     {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1212     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1213     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1214     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1215     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1216     {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1217     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1218     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1219     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1220     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1221     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1222     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1223     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1224     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1225     {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1226     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1227     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1228     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1229     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1230     {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1231     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1232     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1233     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1234     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1235     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1236     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1237     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1238     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1239     {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1240     {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1241     {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1242     {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1243     {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1244     {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1245     {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1246     {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1247     {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),             { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            set_tex_op_atifs        }, WINED3D_GL_EXT_NONE             },
1248     {0 /* Terminate */,                                   { 0,                                                  0                       }, WINED3D_GL_EXT_NONE             },
1249 };
1250 
1251 /* Context activation is done by the caller. */
1252 static void atifs_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
1253 {
1254     if (enable)
1255     {
1256         gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_SHADER_ATI);
1257         checkGLcall("glEnable(GL_FRAGMENT_SHADER_ATI)");
1258     }
1259     else
1260     {
1261         gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_SHADER_ATI);
1262         checkGLcall("glDisable(GL_FRAGMENT_SHADER_ATI)");
1263     }
1264 }
1265 
1266 static void atifs_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
1267 {
1268     caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
1269     caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP               |
1270             WINED3DPMISCCAPS_PERSTAGECONSTANT;
1271     caps->TextureOpCaps =  WINED3DTEXOPCAPS_DISABLE                     |
1272                            WINED3DTEXOPCAPS_SELECTARG1                  |
1273                            WINED3DTEXOPCAPS_SELECTARG2                  |
1274                            WINED3DTEXOPCAPS_MODULATE4X                  |
1275                            WINED3DTEXOPCAPS_MODULATE2X                  |
1276                            WINED3DTEXOPCAPS_MODULATE                    |
1277                            WINED3DTEXOPCAPS_ADDSIGNED2X                 |
1278                            WINED3DTEXOPCAPS_ADDSIGNED                   |
1279                            WINED3DTEXOPCAPS_ADD                         |
1280                            WINED3DTEXOPCAPS_SUBTRACT                    |
1281                            WINED3DTEXOPCAPS_ADDSMOOTH                   |
1282                            WINED3DTEXOPCAPS_BLENDCURRENTALPHA           |
1283                            WINED3DTEXOPCAPS_BLENDFACTORALPHA            |
1284                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHA           |
1285                            WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA           |
1286                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM         |
1287                            WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR      |
1288                            WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA      |
1289                            WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA   |
1290                            WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR   |
1291                            WINED3DTEXOPCAPS_DOTPRODUCT3                 |
1292                            WINED3DTEXOPCAPS_MULTIPLYADD                 |
1293                            WINED3DTEXOPCAPS_LERP                        |
1294                            WINED3DTEXOPCAPS_BUMPENVMAP;
1295 
1296     /* TODO: Implement WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1297     and WINED3DTEXOPCAPS_PREMODULATE */
1298 
1299     /* GL_ATI_fragment_shader always supports 6 textures, which was the limit on r200 cards
1300      * which this extension is exclusively focused on(later cards have GL_ARB_fragment_program).
1301      * If the current card has more than 8 fixed function textures in OpenGL's regular fixed
1302      * function pipeline then the ATI_fragment_shader backend imposes a stricter limit. This
1303      * shouldn't be too hard since Nvidia cards have a limit of 4 textures with the default ffp
1304      * pipeline, and almost all games are happy with that. We can however support up to 8
1305      * texture stages because we have a 2nd pass limit of 8 instructions, and per stage we use
1306      * only 1 instruction.
1307      *
1308      * The proper fix for this is not to use GL_ATI_fragment_shader on cards newer than the
1309      * r200 series and use an ARB or GLSL shader instead
1310      */
1311     caps->MaxTextureBlendStages   = MAX_TEXTURES;
1312     caps->MaxSimultaneousTextures = 6;
1313 }
1314 
1315 static DWORD atifs_get_emul_mask(const struct wined3d_gl_info *gl_info)
1316 {
1317     return GL_EXT_EMUL_ARB_MULTITEXTURE | GL_EXT_EMUL_EXT_FOG_COORD;
1318 }
1319 
1320 static void *atifs_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
1321 {
1322     struct atifs_private_data *priv;
1323 
1324     if (!(priv = heap_alloc_zero(sizeof(*priv))))
1325         return NULL;
1326 
1327     wine_rb_init(&priv->fragment_shaders, wined3d_ffp_frag_program_key_compare);
1328     return priv;
1329 }
1330 
1331 /* Context activation is done by the caller. */
1332 static void atifs_free_ffpshader(struct wine_rb_entry *entry, void *cb_ctx)
1333 {
1334     const struct wined3d_gl_info *gl_info = cb_ctx;
1335     struct atifs_ffp_desc *entry_ati = WINE_RB_ENTRY_VALUE(entry, struct atifs_ffp_desc, parent.entry);
1336 
1337     GL_EXTCALL(glDeleteFragmentShaderATI(entry_ati->shader));
1338     checkGLcall("glDeleteFragmentShaderATI(entry->shader)");
1339     heap_free(entry_ati);
1340 }
1341 
1342 /* Context activation is done by the caller. */
1343 static void atifs_free(struct wined3d_device *device)
1344 {
1345     struct atifs_private_data *priv = device->fragment_priv;
1346 
1347     wine_rb_destroy(&priv->fragment_shaders, atifs_free_ffpshader, &device->adapter->gl_info);
1348 
1349     heap_free(priv);
1350     device->fragment_priv = NULL;
1351 }
1352 
1353 static BOOL atifs_color_fixup_supported(struct color_fixup_desc fixup)
1354 {
1355     /* We only support sign fixup of the first two channels. */
1356     return is_identity_fixup(fixup) || is_same_fixup(fixup, color_fixup_rg)
1357             || is_same_fixup(fixup, color_fixup_rgl) || is_same_fixup(fixup, color_fixup_rgba);
1358 }
1359 
1360 static BOOL atifs_alloc_context_data(struct wined3d_context *context)
1361 {
1362     struct atifs_context_private_data *priv;
1363 
1364     if (!(priv = heap_alloc_zero(sizeof(*priv))))
1365         return FALSE;
1366     context->fragment_pipe_data = priv;
1367     return TRUE;
1368 }
1369 
1370 static void atifs_free_context_data(struct wined3d_context *context)
1371 {
1372     heap_free(context->fragment_pipe_data);
1373 }
1374 
1375 const struct fragment_pipeline atifs_fragment_pipeline = {
1376     atifs_enable,
1377     atifs_get_caps,
1378     atifs_get_emul_mask,
1379     atifs_alloc,
1380     atifs_free,
1381     atifs_alloc_context_data,
1382     atifs_free_context_data,
1383     atifs_color_fixup_supported,
1384     atifs_fragmentstate_template,
1385 };
1386