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].tmp_dst)
345             lowest_write = i;
346 
347         if(op[i].carg1 == WINED3DTA_TEXTURE || op[i].carg2 == WINED3DTA_TEXTURE || op[i].carg0 == WINED3DTA_TEXTURE ||
348            op[i].aarg1 == WINED3DTA_TEXTURE || op[i].aarg2 == WINED3DTA_TEXTURE || op[i].aarg0 == WINED3DTA_TEXTURE) {
349             tex_used[i] = TRUE;
350         }
351     }
352 
353     /* Temp reg not read? We don't need it, return GL_NONE */
354     if(lowest_read == -1) return GL_NONE;
355 
356     if(lowest_write >= lowest_read) {
357         FIXME("Temp register read before being written\n");
358     }
359 
360     if(lowest_write == -1) {
361         /* This needs a test. Maybe we are supposed to return 0.0/0.0/0.0/0.0, or fail drawprim, or whatever */
362         FIXME("Temp register read without being written\n");
363         return GL_REG_1_ATI;
364     } else if(lowest_write >= 1) {
365         /* If we're writing to the temp reg at earliest in stage 1, we can use register 1 for the temp result.
366          * there may be texture data stored in reg 1, but we do not need it any longer since stage 1 already
367          * read it
368          */
369         return GL_REG_1_ATI;
370     } else {
371         /* Search for a free texture register. We have 6 registers available. GL_REG_0_ATI is already used
372          * for the regular result
373          */
374         for(i = 1; i < 6; i++) {
375             if(!tex_used[i]) {
376                 return GL_REG_0_ATI + i;
377             }
378         }
379         /* What to do here? Report it in ValidateDevice? */
380         FIXME("Could not find a register for the temporary register\n");
381         return 0;
382     }
383 }
384 
385 static const struct color_fixup_desc color_fixup_rg =
386 {
387     1, CHANNEL_SOURCE_X,
388     1, CHANNEL_SOURCE_Y,
389     0, CHANNEL_SOURCE_ONE,
390     0, CHANNEL_SOURCE_ONE
391 };
392 static const struct color_fixup_desc color_fixup_rgl =
393 {
394     1, CHANNEL_SOURCE_X,
395     1, CHANNEL_SOURCE_Y,
396     0, CHANNEL_SOURCE_Z,
397     0, CHANNEL_SOURCE_W
398 };
399 static const struct color_fixup_desc color_fixup_rgba =
400 {
401     1, CHANNEL_SOURCE_X,
402     1, CHANNEL_SOURCE_Y,
403     1, CHANNEL_SOURCE_Z,
404     1, CHANNEL_SOURCE_W
405 };
406 
407 static BOOL op_reads_texture(const struct texture_stage_op *op)
408 {
409     return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
410             || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
411             || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
412             || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
413             || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
414             || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
415             || op->cop == WINED3D_TOP_BLEND_TEXTURE_ALPHA;
416 }
417 
418 static void atifs_color_fixup(const struct wined3d_gl_info *gl_info, struct color_fixup_desc fixup, GLuint reg)
419 {
420     if(is_same_fixup(fixup, color_fixup_rg))
421     {
422         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_RED_BIT_ATI | GL_GREEN_BIT_ATI, GL_NONE,
423                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
424         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_BLUE_BIT_ATI, GL_NONE,
425                 GL_ONE, GL_NONE, GL_NONE);
426         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_ALPHA, GL_NONE,
427                 GL_ONE, GL_NONE, GL_NONE);
428     }
429     else if(is_same_fixup(fixup, color_fixup_rgl))
430     {
431         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_RED_BIT_ATI | GL_GREEN_BIT_ATI, GL_NONE,
432                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
433     }
434     else if (is_same_fixup(fixup, color_fixup_rgba))
435     {
436         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_NONE, GL_NONE,
437                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
438         wrap_op1(gl_info, GL_MOV_ATI, reg, GL_ALPHA, GL_NONE,
439                 reg, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI);
440     }
441     else
442     {
443         /* Should not happen - atifs_color_fixup_supported refuses other fixups. */
444         ERR("Unsupported color fixup.\n");
445     }
446 }
447 
448 static BOOL op_reads_tfactor(const struct texture_stage_op *op)
449 {
450     return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
451             || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
452             || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
453             || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
454             || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
455             || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TFACTOR
456             || op->cop == WINED3D_TOP_BLEND_FACTOR_ALPHA
457             || op->aop == WINED3D_TOP_BLEND_FACTOR_ALPHA;
458 }
459 
460 static BOOL op_reads_constant(const struct texture_stage_op *op)
461 {
462     return (op->carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
463             || (op->carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
464             || (op->carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
465             || (op->aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
466             || (op->aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT
467             || (op->aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_CONSTANT;
468 }
469 
470 static GLuint gen_ati_shader(const struct texture_stage_op op[MAX_TEXTURES],
471         const struct wined3d_gl_info *gl_info, enum atifs_constant_value *constants)
472 {
473     GLuint ret = GL_EXTCALL(glGenFragmentShadersATI(1));
474     unsigned int stage;
475     GLuint arg0, arg1, arg2, extrarg;
476     GLuint dstmod, argmod0, argmod1, argmod2, argmodextra;
477     GLuint rep0, rep1, rep2;
478     GLuint swizzle;
479     GLuint tmparg = find_tmpreg(op);
480     GLuint dstreg;
481     BOOL tfactor_used = FALSE;
482 
483     if(!ret) {
484         ERR("Failed to generate a GL_ATI_fragment_shader shader id\n");
485         return 0;
486     }
487     GL_EXTCALL(glBindFragmentShaderATI(ret));
488     checkGLcall("GL_EXTCALL(glBindFragmentShaderATI(ret))");
489 
490     TRACE("glBeginFragmentShaderATI()\n");
491     GL_EXTCALL(glBeginFragmentShaderATI());
492     checkGLcall("GL_EXTCALL(glBeginFragmentShaderATI())");
493 
494     /* Pass 1: Generate sampling instructions for perturbation maps */
495     for (stage = 0; stage < gl_info->limits.textures; ++stage)
496     {
497         if (op[stage].cop == WINED3D_TOP_DISABLE)
498             break;
499         if (op[stage].cop != WINED3D_TOP_BUMPENVMAP
500                 && op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
501             continue;
502 
503         constants[stage] = ATIFS_CONSTANT_BUMP;
504 
505         TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, GL_SWIZZLE_STR_ATI)\n",
506               stage, stage);
507         GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage, GL_TEXTURE0_ARB + stage, GL_SWIZZLE_STR_ATI));
508         if (op[stage + 1].projected == WINED3D_PROJECTION_NONE)
509             swizzle = GL_SWIZZLE_STR_ATI;
510         else if (op[stage + 1].projected == WINED3D_PROJECTION_COUNT4)
511             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
512         else
513             swizzle = GL_SWIZZLE_STR_DR_ATI;
514         TRACE("glPassTexCoordATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
515               stage + 1, stage + 1, debug_swizzle(swizzle));
516         GL_EXTCALL(glPassTexCoordATI(GL_REG_0_ATI + stage + 1,
517                    GL_TEXTURE0_ARB + stage + 1,
518                    swizzle));
519     }
520 
521     /* Pass 2: Generate perturbation calculations */
522     for (stage = 0; stage < gl_info->limits.textures; ++stage)
523     {
524         GLuint argmodextra_x, argmodextra_y;
525         struct color_fixup_desc fixup;
526 
527         if (op[stage].cop == WINED3D_TOP_DISABLE)
528             break;
529         if (op[stage].cop != WINED3D_TOP_BUMPENVMAP
530                 && op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
531             continue;
532 
533         fixup = op[stage].color_fixup;
534         if (fixup.x_source != CHANNEL_SOURCE_X || fixup.y_source != CHANNEL_SOURCE_Y)
535         {
536             FIXME("Swizzles not implemented\n");
537             argmodextra_x = GL_NONE;
538             argmodextra_y = GL_NONE;
539         }
540         else
541         {
542             /* Nice thing, we get the color correction for free :-) */
543             argmodextra_x = fixup.x_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
544             argmodextra_y = fixup.y_sign_fixup ? GL_2X_BIT_ATI | GL_BIAS_BIT_ATI : GL_NONE;
545         }
546 
547         wrap_op3(gl_info, GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_RED_BIT_ATI, GL_NONE,
548                  GL_REG_0_ATI + stage, GL_NONE, argmodextra_x,
549                  ATIFS_CONST_BUMPMAT(stage), GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
550                  GL_REG_0_ATI + stage + 1, GL_RED, GL_NONE);
551 
552         /* Don't use GL_DOT2_ADD_ATI here because we cannot configure it to read the blue and alpha
553          * component of the bump matrix. Instead do this with two MADs:
554          *
555          * coord.a = tex.r * bump.b + coord.g
556          * coord.g = tex.g * bump.a + coord.a
557          *
558          * The first instruction writes to alpha so it can be coissued with the above DOT2_ADD.
559          * coord.a is unused. If the perturbed texture is projected, this was already handled
560          * in the glPassTexCoordATI above.
561          */
562         wrap_op3(gl_info, GL_MAD_ATI, GL_REG_0_ATI + stage + 1, GL_ALPHA, GL_NONE,
563                  GL_REG_0_ATI + stage, GL_RED, argmodextra_y,
564                  ATIFS_CONST_BUMPMAT(stage), GL_BLUE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
565                  GL_REG_0_ATI + stage + 1, GL_GREEN, GL_NONE);
566         wrap_op3(gl_info, GL_MAD_ATI, GL_REG_0_ATI + stage + 1, GL_GREEN_BIT_ATI, GL_NONE,
567                  GL_REG_0_ATI + stage, GL_GREEN, argmodextra_y,
568                  ATIFS_CONST_BUMPMAT(stage), GL_ALPHA, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
569                  GL_REG_0_ATI + stage + 1, GL_ALPHA, GL_NONE);
570     }
571 
572     /* Pass 3: Generate sampling instructions for regular textures */
573     for (stage = 0; stage < gl_info->limits.textures; ++stage)
574     {
575         if (op[stage].cop == WINED3D_TOP_DISABLE)
576             break;
577 
578         if (op[stage].projected == WINED3D_PROJECTION_NONE)
579             swizzle = GL_SWIZZLE_STR_ATI;
580         else if (op[stage].projected == WINED3D_PROJECTION_COUNT3)
581             swizzle = GL_SWIZZLE_STR_DR_ATI;
582         else
583             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
584 
585         if (op_reads_texture(&op[stage]))
586         {
587             if (stage > 0
588                     && (op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
589                     || op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
590             {
591                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_REG_%d_ATI, GL_SWIZZLE_STR_ATI)\n",
592                       stage, stage);
593                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
594                            GL_REG_0_ATI + stage,
595                            GL_SWIZZLE_STR_ATI));
596             } else {
597                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
598                     stage, stage, debug_swizzle(swizzle));
599                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
600                                         GL_TEXTURE0_ARB + stage,
601                                         swizzle));
602             }
603         }
604     }
605 
606     /* Pass 4: Generate the arithmetic instructions */
607     for (stage = 0; stage < MAX_TEXTURES; ++stage)
608     {
609         if (op[stage].cop == WINED3D_TOP_DISABLE)
610         {
611             if (!stage)
612             {
613                 /* Handle complete texture disabling gracefully */
614                 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
615                          GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
616                 wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
617                          GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
618             }
619             break;
620         }
621 
622         if (op[stage].tmp_dst)
623         {
624             /* If we're writing to D3DTA_TEMP, but never reading from it we
625              * don't have to write there in the first place. Skip the entire
626              * stage, this saves some GPU time. */
627             if (tmparg == GL_NONE)
628                 continue;
629 
630             dstreg = tmparg;
631         }
632         else
633         {
634             dstreg = GL_REG_0_ATI;
635         }
636 
637         if (op[stage].cop == WINED3D_TOP_BUMPENVMAP || op[stage].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
638         {
639             /* Those are handled in the first pass of the shader (generation pass 1 and 2) already */
640             continue;
641         }
642 
643         arg0 = register_for_arg(op[stage].carg0, gl_info, stage, &argmod0, &rep0, tmparg);
644         arg1 = register_for_arg(op[stage].carg1, gl_info, stage, &argmod1, &rep1, tmparg);
645         arg2 = register_for_arg(op[stage].carg2, gl_info, stage, &argmod2, &rep2, tmparg);
646         dstmod = GL_NONE;
647         argmodextra = GL_NONE;
648         extrarg = GL_NONE;
649 
650         if (op_reads_tfactor(&op[stage]))
651             tfactor_used = TRUE;
652 
653         if (op_reads_constant(&op[stage]))
654         {
655             if (constants[stage] != ATIFS_CONSTANT_UNUSED)
656                 FIXME("Constant %u already used.\n", stage);
657             constants[stage] = ATIFS_CONSTANT_STAGE;
658         }
659 
660         if (op_reads_texture(&op[stage]) && !is_identity_fixup(op[stage].color_fixup))
661             atifs_color_fixup(gl_info, op[stage].color_fixup, GL_REG_0_ATI + stage);
662 
663         switch (op[stage].cop)
664         {
665             case WINED3D_TOP_SELECT_ARG2:
666                 arg1 = arg2;
667                 argmod1 = argmod2;
668                 rep1 = rep2;
669                 /* fall through */
670             case WINED3D_TOP_SELECT_ARG1:
671                 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_NONE, GL_NONE,
672                          arg1, rep1, argmod1);
673                 break;
674 
675             case WINED3D_TOP_MODULATE_4X:
676                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
677                 /* fall through */
678             case WINED3D_TOP_MODULATE_2X:
679                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
680                 dstmod |= GL_SATURATE_BIT_ATI;
681                 /* fall through */
682             case WINED3D_TOP_MODULATE:
683                 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_NONE, dstmod,
684                          arg1, rep1, argmod1,
685                          arg2, rep2, argmod2);
686                 break;
687 
688             case WINED3D_TOP_ADD_SIGNED_2X:
689                 dstmod = GL_2X_BIT_ATI;
690                 /* fall through */
691             case WINED3D_TOP_ADD_SIGNED:
692                 argmodextra = GL_BIAS_BIT_ATI;
693                 /* fall through */
694             case WINED3D_TOP_ADD:
695                 dstmod |= GL_SATURATE_BIT_ATI;
696                 wrap_op2(gl_info, GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, dstmod,
697                          arg1, rep1, argmod1,
698                          arg2, rep2, argmodextra | argmod2);
699                 break;
700 
701             case WINED3D_TOP_SUBTRACT:
702                 dstmod |= GL_SATURATE_BIT_ATI;
703                 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_NONE, dstmod,
704                          arg1, rep1, argmod1,
705                          arg2, rep2, argmod2);
706                 break;
707 
708             case WINED3D_TOP_ADD_SMOOTH:
709                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
710                 /* Dst = arg1 + * arg2(1 -arg 1)
711                  *     = arg2 * (1 - arg1) + arg1
712                  */
713                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
714                          arg2, rep2, argmod2,
715                          arg1, rep1, argmodextra,
716                          arg1, rep1, argmod1);
717                 break;
718 
719             case WINED3D_TOP_BLEND_CURRENT_ALPHA:
720                 if (extrarg == GL_NONE)
721                     extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
722                 /* fall through */
723             case WINED3D_TOP_BLEND_FACTOR_ALPHA:
724                 if (extrarg == GL_NONE)
725                     extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
726                 /* fall through */
727             case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
728                 if (extrarg == GL_NONE)
729                     extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
730                 /* fall through */
731             case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
732                 if (extrarg == GL_NONE)
733                     extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
734                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
735                          extrarg, GL_ALPHA, GL_NONE,
736                          arg1, rep1, argmod1,
737                          arg2, rep2, argmod2);
738                 break;
739 
740             case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
741                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
742                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_NONE,
743                          arg2, rep2,  argmod2,
744                          arg0, GL_ALPHA, GL_COMP_BIT_ATI,
745                          arg1, rep1,  argmod1);
746                 break;
747 
748             /* D3DTOP_PREMODULATE ???? */
749 
750             case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
751                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
752                 /* fall through */
753             case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
754                 if (!argmodextra)
755                     argmodextra = argmod1;
756                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
757                          arg2, rep2,  argmod2,
758                          arg1, GL_ALPHA, argmodextra,
759                          arg1, rep1,  argmod1);
760                 break;
761 
762             case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
763                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
764                 /* fall through */
765             case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
766                 if (!argmodextra)
767                     argmodextra = argmod1;
768                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
769                          arg2, rep2,  argmod2,
770                          arg1, rep1,  argmodextra,
771                          arg1, GL_ALPHA, argmod1);
772                 break;
773 
774             case WINED3D_TOP_DOTPRODUCT3:
775                 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
776                          arg1, rep1, argmod1 | GL_BIAS_BIT_ATI,
777                          arg2, rep2, argmod2 | GL_BIAS_BIT_ATI);
778                 break;
779 
780             case WINED3D_TOP_MULTIPLY_ADD:
781                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
782                          arg1, rep1, argmod1,
783                          arg2, rep2, argmod2,
784                          arg0, rep0, argmod0);
785                 break;
786 
787             case WINED3D_TOP_LERP:
788                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
789                          arg0, rep0, argmod0,
790                          arg1, rep1, argmod1,
791                          arg2, rep2, argmod2);
792                 break;
793 
794             default: FIXME("Unhandled color operation %d on stage %d\n", op[stage].cop, stage);
795         }
796 
797         arg0 = register_for_arg(op[stage].aarg0, gl_info, stage, &argmod0, NULL, tmparg);
798         arg1 = register_for_arg(op[stage].aarg1, gl_info, stage, &argmod1, NULL, tmparg);
799         arg2 = register_for_arg(op[stage].aarg2, gl_info, stage, &argmod2, NULL, tmparg);
800         dstmod = GL_NONE;
801         argmodextra = GL_NONE;
802         extrarg = GL_NONE;
803 
804         switch (op[stage].aop)
805         {
806             case WINED3D_TOP_DISABLE:
807                 /* Get the primary color to the output if on stage 0, otherwise leave register 0 untouched */
808                 if (!stage)
809                 {
810                     wrap_op1(gl_info, GL_MOV_ATI, GL_REG_0_ATI, GL_ALPHA, GL_NONE,
811                              GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
812                 }
813                 break;
814 
815             case WINED3D_TOP_SELECT_ARG2:
816                 arg1 = arg2;
817                 argmod1 = argmod2;
818                 /* fall through */
819             case WINED3D_TOP_SELECT_ARG1:
820                 wrap_op1(gl_info, GL_MOV_ATI, dstreg, GL_ALPHA, GL_NONE,
821                          arg1, GL_NONE, argmod1);
822                 break;
823 
824             case WINED3D_TOP_MODULATE_4X:
825                 if (dstmod == GL_NONE)
826                     dstmod = GL_4X_BIT_ATI;
827                 /* fall through */
828             case WINED3D_TOP_MODULATE_2X:
829                 if (dstmod == GL_NONE)
830                     dstmod = GL_2X_BIT_ATI;
831                 dstmod |= GL_SATURATE_BIT_ATI;
832                 /* fall through */
833             case WINED3D_TOP_MODULATE:
834                 wrap_op2(gl_info, GL_MUL_ATI, dstreg, GL_ALPHA, dstmod,
835                          arg1, GL_NONE, argmod1,
836                          arg2, GL_NONE, argmod2);
837                 break;
838 
839             case WINED3D_TOP_ADD_SIGNED_2X:
840                 dstmod = GL_2X_BIT_ATI;
841                 /* fall through */
842             case WINED3D_TOP_ADD_SIGNED:
843                 argmodextra = GL_BIAS_BIT_ATI;
844                 /* fall through */
845             case WINED3D_TOP_ADD:
846                 dstmod |= GL_SATURATE_BIT_ATI;
847                 wrap_op2(gl_info, GL_ADD_ATI, dstreg, GL_ALPHA, dstmod,
848                          arg1, GL_NONE, argmod1,
849                          arg2, GL_NONE, argmodextra | argmod2);
850                 break;
851 
852             case WINED3D_TOP_SUBTRACT:
853                 dstmod |= GL_SATURATE_BIT_ATI;
854                 wrap_op2(gl_info, GL_SUB_ATI, dstreg, GL_ALPHA, dstmod,
855                          arg1, GL_NONE, argmod1,
856                          arg2, GL_NONE, argmod2);
857                 break;
858 
859             case WINED3D_TOP_ADD_SMOOTH:
860                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
861                 /* Dst = arg1 + * arg2(1 -arg 1)
862                  *     = arg2 * (1 - arg1) + arg1
863                  */
864                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
865                          arg2, GL_NONE, argmod2,
866                          arg1, GL_NONE, argmodextra,
867                          arg1, GL_NONE, argmod1);
868                 break;
869 
870             case WINED3D_TOP_BLEND_CURRENT_ALPHA:
871                 if (extrarg == GL_NONE)
872                     extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
873                 /* fall through */
874             case WINED3D_TOP_BLEND_FACTOR_ALPHA:
875                 if (extrarg == GL_NONE)
876                     extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
877                 /* fall through */
878             case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
879                 if (extrarg == GL_NONE)
880                     extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
881                 /* fall through */
882             case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
883                 if (extrarg == GL_NONE)
884                     extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
885                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_NONE,
886                          extrarg, GL_ALPHA, GL_NONE,
887                          arg1, GL_NONE, argmod1,
888                          arg2, GL_NONE, argmod2);
889                 break;
890 
891             case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
892                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
893                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_NONE,
894                          arg2, GL_NONE,  argmod2,
895                          arg0, GL_ALPHA, GL_COMP_BIT_ATI,
896                          arg1, GL_NONE,  argmod1);
897                 break;
898 
899             /* D3DTOP_PREMODULATE ???? */
900 
901             case WINED3D_TOP_DOTPRODUCT3:
902                 wrap_op2(gl_info, GL_DOT3_ATI, dstreg, GL_ALPHA, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
903                          arg1, GL_NONE, argmod1 | GL_BIAS_BIT_ATI,
904                          arg2, GL_NONE, argmod2 | GL_BIAS_BIT_ATI);
905                 break;
906 
907             case WINED3D_TOP_MULTIPLY_ADD:
908                 wrap_op3(gl_info, GL_MAD_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
909                          arg1, GL_NONE, argmod1,
910                          arg2, GL_NONE, argmod2,
911                          arg0, GL_NONE, argmod0);
912                 break;
913 
914             case WINED3D_TOP_LERP:
915                 wrap_op3(gl_info, GL_LERP_ATI, dstreg, GL_ALPHA, GL_SATURATE_BIT_ATI,
916                          arg1, GL_NONE, argmod1,
917                          arg2, GL_NONE, argmod2,
918                          arg0, GL_NONE, argmod0);
919                 break;
920 
921             case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
922             case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
923             case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
924             case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
925             case WINED3D_TOP_BUMPENVMAP:
926             case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
927                 ERR("Application uses an invalid alpha operation\n");
928                 break;
929 
930             default: FIXME("Unhandled alpha operation %d on stage %d\n", op[stage].aop, stage);
931         }
932     }
933 
934     if (tfactor_used && constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] != ATIFS_CONSTANT_UNUSED)
935         FIXME("Texture factor constant already used.\n");
936     constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] = ATIFS_CONSTANT_TFACTOR;
937 
938     /* Assign unused constants to avoid reloading due to unused <-> bump matrix switches. */
939     for (stage = 0; stage < MAX_TEXTURES; ++stage)
940     {
941         if (constants[stage] == ATIFS_CONSTANT_UNUSED)
942             constants[stage] = ATIFS_CONSTANT_BUMP;
943     }
944 
945     TRACE("glEndFragmentShaderATI()\n");
946     GL_EXTCALL(glEndFragmentShaderATI());
947     checkGLcall("GL_EXTCALL(glEndFragmentShaderATI())");
948     return ret;
949 }
950 
951 static void atifs_tfactor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
952 {
953     const struct wined3d_gl_info *gl_info = context->gl_info;
954     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
955     struct wined3d_color color;
956 
957     if (!ctx_priv->last_shader
958             || ctx_priv->last_shader->constants[ATIFS_CONST_TFACTOR - GL_CON_0_ATI] != ATIFS_CONSTANT_TFACTOR)
959         return;
960 
961     wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
962     GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_TFACTOR, &color.r));
963     checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_TFACTOR, &color.r)");
964 }
965 
966 static void set_bumpmat(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
967 {
968     DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
969     const struct wined3d_gl_info *gl_info = context->gl_info;
970     float mat[2][2];
971     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
972 
973     if (!ctx_priv->last_shader
974             || ctx_priv->last_shader->constants[stage] != ATIFS_CONSTANT_BUMP)
975         return;
976 
977     mat[0][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT00]);
978     mat[1][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT01]);
979     mat[0][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT10]);
980     mat[1][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT11]);
981     /* GL_ATI_fragment_shader allows only constants from 0.0 to 1.0, but the bumpmat
982      * constants can be in any range. While they should stay between [-1.0 and 1.0] because
983      * Shader Model 1.x pixel shaders are clamped to that range negative values are used occasionally,
984      * for example by our d3d9 test. So to get negative values scale -1;1 to 0;1 and undo that in the
985      * shader(it is free). This might potentially reduce precision. However, if the hardware does
986      * support proper floats it shouldn't, and if it doesn't we can't get anything better anyway. */
987     mat[0][0] = (mat[0][0] + 1.0f) * 0.5f;
988     mat[1][0] = (mat[1][0] + 1.0f) * 0.5f;
989     mat[0][1] = (mat[0][1] + 1.0f) * 0.5f;
990     mat[1][1] = (mat[1][1] + 1.0f) * 0.5f;
991     GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_BUMPMAT(stage), (float *) mat));
992     checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_BUMPMAT(stage), mat)");
993 }
994 
995 static void atifs_stage_constant(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
996 {
997     DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
998     const struct wined3d_gl_info *gl_info = context->gl_info;
999     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
1000     struct wined3d_color color;
1001 
1002     if (!ctx_priv->last_shader
1003             || ctx_priv->last_shader->constants[stage] != ATIFS_CONSTANT_STAGE)
1004         return;
1005 
1006     wined3d_color_from_d3dcolor(&color, state->texture_states[stage][WINED3D_TSS_CONSTANT]);
1007     GL_EXTCALL(glSetFragmentShaderConstantATI(ATIFS_CONST_STAGE(stage), &color.r));
1008     checkGLcall("glSetFragmentShaderConstantATI(ATIFS_CONST_STAGE(stage), &color.r)");
1009 }
1010 
1011 static void set_tex_op_atifs(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1012 {
1013     const struct wined3d_device *device = context->device;
1014     const struct wined3d_gl_info *gl_info = context->gl_info;
1015     const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1016     struct atifs_context_private_data *ctx_priv = context->fragment_pipe_data;
1017     const struct atifs_ffp_desc *desc, *last_shader = ctx_priv->last_shader;
1018     struct ffp_frag_settings settings;
1019     struct atifs_private_data *priv = device->fragment_priv;
1020     DWORD mapped_stage;
1021     unsigned int i;
1022 
1023     gen_ffp_frag_op(context, state, &settings, TRUE);
1024     desc = (const struct atifs_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
1025     if (!desc)
1026     {
1027         struct atifs_ffp_desc *new_desc;
1028 
1029         if (!(new_desc = heap_alloc_zero(sizeof(*new_desc))))
1030         {
1031             ERR("Out of memory\n");
1032             return;
1033         }
1034         new_desc->num_textures_used = 0;
1035         for (i = 0; i < d3d_info->limits.ffp_blend_stages; ++i)
1036         {
1037             if (settings.op[i].cop == WINED3D_TOP_DISABLE)
1038                 break;
1039             ++new_desc->num_textures_used;
1040         }
1041 
1042         new_desc->parent.settings = settings;
1043         new_desc->shader = gen_ati_shader(settings.op, gl_info, new_desc->constants);
1044         add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
1045         TRACE("Allocated fixed function replacement shader descriptor %p.\n", new_desc);
1046         desc = new_desc;
1047     }
1048 
1049     /* GL_ATI_fragment_shader depends on the GL_TEXTURE_xD enable settings. Update the texture stages
1050      * used by this shader
1051      */
1052     for (i = 0; i < desc->num_textures_used; ++i)
1053     {
1054         mapped_stage = context->tex_unit_map[i];
1055         if (mapped_stage != WINED3D_UNMAPPED_STAGE)
1056         {
1057             context_active_texture(context, gl_info, mapped_stage);
1058             texture_activate_dimensions(state->textures[i], gl_info);
1059         }
1060     }
1061 
1062     GL_EXTCALL(glBindFragmentShaderATI(desc->shader));
1063     ctx_priv->last_shader = desc;
1064 
1065     for (i = 0; i < MAX_TEXTURES; i++)
1066     {
1067         if (last_shader && last_shader->constants[i] == desc->constants[i])
1068             continue;
1069 
1070         switch (desc->constants[i])
1071         {
1072             case ATIFS_CONSTANT_BUMP:
1073                 set_bumpmat(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_BUMPENV_MAT00));
1074                 break;
1075 
1076             case ATIFS_CONSTANT_TFACTOR:
1077                 atifs_tfactor(context, state, STATE_RENDER(WINED3D_RS_TEXTUREFACTOR));
1078                 break;
1079 
1080             case ATIFS_CONSTANT_STAGE:
1081                 atifs_stage_constant(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_CONSTANT));
1082                 break;
1083 
1084             default:
1085                 ERR("Unexpected constant type %u.\n", desc->constants[i]);
1086         }
1087     }
1088 }
1089 
1090 static void textransform(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1091 {
1092     if (!isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL)))
1093         set_tex_op_atifs(context, state, state_id);
1094 }
1095 
1096 static void atifs_srgbwriteenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1097 {
1098     if (state->render_states[WINED3D_RS_SRGBWRITEENABLE])
1099         WARN("sRGB writes are not supported by this fragment pipe.\n");
1100 }
1101 
1102 static const struct StateEntryTemplate atifs_fragmentstate_template[] = {
1103     {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR),              { STATE_RENDER(WINED3D_RS_TEXTUREFACTOR),             atifs_tfactor           }, WINED3D_GL_EXT_NONE             },
1104     {STATE_RENDER(WINED3D_RS_ALPHAFUNC),                  { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                    }, WINED3D_GL_EXT_NONE             },
1105     {STATE_RENDER(WINED3D_RS_ALPHAREF),                   { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                    }, WINED3D_GL_EXT_NONE             },
1106     {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),            { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           state_alpha_test        }, WINED3D_GL_EXT_NONE             },
1107     {STATE_RENDER(WINED3D_RS_COLORKEYENABLE),             { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                    }, WINED3D_GL_EXT_NONE             },
1108     {STATE_RENDER(WINED3D_RS_FOGCOLOR),                   { STATE_RENDER(WINED3D_RS_FOGCOLOR),                  state_fogcolor          }, WINED3D_GL_EXT_NONE             },
1109     {STATE_RENDER(WINED3D_RS_FOGDENSITY),                 { STATE_RENDER(WINED3D_RS_FOGDENSITY),                state_fogdensity        }, WINED3D_GL_EXT_NONE             },
1110     {STATE_RENDER(WINED3D_RS_FOGENABLE),                  { STATE_RENDER(WINED3D_RS_FOGENABLE),                 state_fog_fragpart      }, WINED3D_GL_EXT_NONE             },
1111     {STATE_RENDER(WINED3D_RS_FOGTABLEMODE),               { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                    }, WINED3D_GL_EXT_NONE             },
1112     {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE),              { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                    }, WINED3D_GL_EXT_NONE             },
1113     {STATE_RENDER(WINED3D_RS_FOGSTART),                   { STATE_RENDER(WINED3D_RS_FOGSTART),                  state_fogstartend       }, WINED3D_GL_EXT_NONE             },
1114     {STATE_RENDER(WINED3D_RS_FOGEND),                     { STATE_RENDER(WINED3D_RS_FOGSTART),                  NULL                    }, WINED3D_GL_EXT_NONE             },
1115     {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE),            { STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE),           atifs_srgbwriteenable   }, WINED3D_GL_EXT_NONE             },
1116     {STATE_COLOR_KEY,                                     { STATE_COLOR_KEY,                                    state_nop               }, WINED3D_GL_EXT_NONE             },
1117     {STATE_RENDER(WINED3D_RS_SHADEMODE),                  { STATE_RENDER(WINED3D_RS_SHADEMODE),                 state_shademode         }, WINED3D_GL_EXT_NONE             },
1118     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1119     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1120     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1121     {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1122     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1123     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1124     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1125     {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1126     {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1127     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1128     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1129     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1130     {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1131     {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1132     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1133     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1134     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1135     {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1136     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1137     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1138     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1139     {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1140     {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1141     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1142     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1143     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1144     {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1145     {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1146     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1147     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1148     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1149     {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1150     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1151     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1152     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1153     {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1154     {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1155     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1156     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1157     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1158     {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1159     {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1160     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1161     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1162     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1163     {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1164     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1165     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1166     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1167     {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1168     {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1169     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1170     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1171     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1172     {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1173     {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1174     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1175     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1176     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1177     {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1178     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1179     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1180     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1181     {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1182     {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1183     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1184     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1185     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1186     {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1187     {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1188     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1189     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1190     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1191     {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1192     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1193     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1194     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1195     {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1196     {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1197     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1198     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1199     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1200     {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1201     {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1202     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1203     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1204     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1205     {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1206     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1207     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1208     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1209     {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1210     {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1211     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1212     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1213     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1214     {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1215     {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1216     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1217     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1218     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1219     {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1220     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),         { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1221     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1222     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1223     {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1224     {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG),       { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                    }, WINED3D_GL_EXT_NONE             },
1225     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   set_bumpmat             }, WINED3D_GL_EXT_NONE             },
1226     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1227     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1228     {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11),    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                    }, WINED3D_GL_EXT_NONE             },
1229     {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT),         { STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT),        atifs_stage_constant    }, WINED3D_GL_EXT_NONE             },
1230     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1231     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1232     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1233     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1234     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1235     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1236     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1237     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim          }, WINED3D_GL_EXT_NONE             },
1238     {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1239     {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1240     {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1241     {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1242     {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1243     {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1244     {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1245     {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform      }, WINED3D_GL_EXT_NONE             },
1246     {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),             { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            set_tex_op_atifs        }, WINED3D_GL_EXT_NONE             },
1247     {0 /* Terminate */,                                   { 0,                                                  0                       }, WINED3D_GL_EXT_NONE             },
1248 };
1249 
1250 /* Context activation is done by the caller. */
1251 static void atifs_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
1252 {
1253     if (enable)
1254     {
1255         gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_SHADER_ATI);
1256         checkGLcall("glEnable(GL_FRAGMENT_SHADER_ATI)");
1257     }
1258     else
1259     {
1260         gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_SHADER_ATI);
1261         checkGLcall("glDisable(GL_FRAGMENT_SHADER_ATI)");
1262     }
1263 }
1264 
1265 static void atifs_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
1266 {
1267     caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
1268     caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP               |
1269             WINED3DPMISCCAPS_PERSTAGECONSTANT;
1270     caps->TextureOpCaps =  WINED3DTEXOPCAPS_DISABLE                     |
1271                            WINED3DTEXOPCAPS_SELECTARG1                  |
1272                            WINED3DTEXOPCAPS_SELECTARG2                  |
1273                            WINED3DTEXOPCAPS_MODULATE4X                  |
1274                            WINED3DTEXOPCAPS_MODULATE2X                  |
1275                            WINED3DTEXOPCAPS_MODULATE                    |
1276                            WINED3DTEXOPCAPS_ADDSIGNED2X                 |
1277                            WINED3DTEXOPCAPS_ADDSIGNED                   |
1278                            WINED3DTEXOPCAPS_ADD                         |
1279                            WINED3DTEXOPCAPS_SUBTRACT                    |
1280                            WINED3DTEXOPCAPS_ADDSMOOTH                   |
1281                            WINED3DTEXOPCAPS_BLENDCURRENTALPHA           |
1282                            WINED3DTEXOPCAPS_BLENDFACTORALPHA            |
1283                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHA           |
1284                            WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA           |
1285                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM         |
1286                            WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR      |
1287                            WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA      |
1288                            WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA   |
1289                            WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR   |
1290                            WINED3DTEXOPCAPS_DOTPRODUCT3                 |
1291                            WINED3DTEXOPCAPS_MULTIPLYADD                 |
1292                            WINED3DTEXOPCAPS_LERP                        |
1293                            WINED3DTEXOPCAPS_BUMPENVMAP;
1294 
1295     /* TODO: Implement WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1296     and WINED3DTEXOPCAPS_PREMODULATE */
1297 
1298     /* GL_ATI_fragment_shader always supports 6 textures, which was the limit on r200 cards
1299      * which this extension is exclusively focused on(later cards have GL_ARB_fragment_program).
1300      * If the current card has more than 8 fixed function textures in OpenGL's regular fixed
1301      * function pipeline then the ATI_fragment_shader backend imposes a stricter limit. This
1302      * shouldn't be too hard since Nvidia cards have a limit of 4 textures with the default ffp
1303      * pipeline, and almost all games are happy with that. We can however support up to 8
1304      * texture stages because we have a 2nd pass limit of 8 instructions, and per stage we use
1305      * only 1 instruction.
1306      *
1307      * The proper fix for this is not to use GL_ATI_fragment_shader on cards newer than the
1308      * r200 series and use an ARB or GLSL shader instead
1309      */
1310     caps->MaxTextureBlendStages   = MAX_TEXTURES;
1311     caps->MaxSimultaneousTextures = 6;
1312 }
1313 
1314 static DWORD atifs_get_emul_mask(const struct wined3d_gl_info *gl_info)
1315 {
1316     return GL_EXT_EMUL_ARB_MULTITEXTURE | GL_EXT_EMUL_EXT_FOG_COORD;
1317 }
1318 
1319 static void *atifs_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
1320 {
1321     struct atifs_private_data *priv;
1322 
1323     if (!(priv = heap_alloc_zero(sizeof(*priv))))
1324         return NULL;
1325 
1326     wine_rb_init(&priv->fragment_shaders, wined3d_ffp_frag_program_key_compare);
1327     return priv;
1328 }
1329 
1330 /* Context activation is done by the caller. */
1331 static void atifs_free_ffpshader(struct wine_rb_entry *entry, void *cb_ctx)
1332 {
1333     const struct wined3d_gl_info *gl_info = cb_ctx;
1334     struct atifs_ffp_desc *entry_ati = WINE_RB_ENTRY_VALUE(entry, struct atifs_ffp_desc, parent.entry);
1335 
1336     GL_EXTCALL(glDeleteFragmentShaderATI(entry_ati->shader));
1337     checkGLcall("glDeleteFragmentShaderATI(entry->shader)");
1338     heap_free(entry_ati);
1339 }
1340 
1341 /* Context activation is done by the caller. */
1342 static void atifs_free(struct wined3d_device *device)
1343 {
1344     struct atifs_private_data *priv = device->fragment_priv;
1345 
1346     wine_rb_destroy(&priv->fragment_shaders, atifs_free_ffpshader, &device->adapter->gl_info);
1347 
1348     heap_free(priv);
1349     device->fragment_priv = NULL;
1350 }
1351 
1352 static BOOL atifs_color_fixup_supported(struct color_fixup_desc fixup)
1353 {
1354     /* We only support sign fixup of the first two channels. */
1355     return is_identity_fixup(fixup) || is_same_fixup(fixup, color_fixup_rg)
1356             || is_same_fixup(fixup, color_fixup_rgl) || is_same_fixup(fixup, color_fixup_rgba);
1357 }
1358 
1359 static BOOL atifs_alloc_context_data(struct wined3d_context *context)
1360 {
1361     struct atifs_context_private_data *priv;
1362 
1363     if (!(priv = heap_alloc_zero(sizeof(*priv))))
1364         return FALSE;
1365     context->fragment_pipe_data = priv;
1366     return TRUE;
1367 }
1368 
1369 static void atifs_free_context_data(struct wined3d_context *context)
1370 {
1371     heap_free(context->fragment_pipe_data);
1372 }
1373 
1374 const struct fragment_pipeline atifs_fragment_pipeline = {
1375     atifs_enable,
1376     atifs_get_caps,
1377     atifs_get_emul_mask,
1378     atifs_alloc,
1379     atifs_free,
1380     atifs_alloc_context_data,
1381     atifs_free_context_data,
1382     atifs_color_fixup_supported,
1383     atifs_fragmentstate_template,
1384 };
1385