gdiplus: Implemented GdipSetClipRegion with basic tests.
[wine] / dlls / wined3d / ati_fragment_shader.c
1 /*
2  * Fixed function pipeline replacement using GL_ATI_fragment_shader
3  *
4  * Copyright 2008 Stefan Dösinger(for CodeWeavers)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <math.h>
24 #include <stdio.h>
25
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
29
30 /* Some private defines, Constant associations, etc.
31  * Env bump matrix and per stage constant should be independent,
32  * a stage that bump maps can't read the per state constant
33  */
34 #define ATI_FFP_CONST_BUMPMAT(i) (GL_CON_0_ATI + i)
35 #define ATI_FFP_CONST_CONSTANT0 GL_CON_0_ATI
36 #define ATI_FFP_CONST_CONSTANT1 GL_CON_1_ATI
37 #define ATI_FFP_CONST_CONSTANT2 GL_CON_2_ATI
38 #define ATI_FFP_CONST_CONSTANT3 GL_CON_3_ATI
39 #define ATI_FFP_CONST_CONSTANT4 GL_CON_4_ATI
40 #define ATI_FFP_CONST_CONSTANT5 GL_CON_5_ATI
41 #define ATI_FFP_CONST_TFACTOR   GL_CON_6_ATI
42
43 /* GL_ATI_fragment_shader specific fixed function pipeline description. "Inherits" from the common one */
44 struct atifs_ffp_desc
45 {
46     struct ffp_desc parent;
47     GLuint shader;
48     unsigned int num_textures_used;
49 };
50
51 struct atifs_private_data
52 {
53     hash_table_t *fragment_shaders; /* A hashtable to track fragment pipeline replacement shaders */
54
55 };
56
57 static const char *debug_dstmod(GLuint mod) {
58     switch(mod) {
59         case GL_NONE:               return "GL_NONE";
60         case GL_2X_BIT_ATI:         return "GL_2X_BIT_ATI";
61         case GL_4X_BIT_ATI:         return "GL_4X_BIT_ATI";
62         case GL_8X_BIT_ATI:         return "GL_8X_BIT_ATI";
63         case GL_HALF_BIT_ATI:       return "GL_HALF_BIT_ATI";
64         case GL_QUARTER_BIT_ATI:    return "GL_QUARTER_BIT_ATI";
65         case GL_EIGHTH_BIT_ATI:     return "GL_EIGHTH_BIT_ATI";
66         case GL_SATURATE_BIT_ATI:   return "GL_SATURATE_BIT_ATI";
67         default:                    return "Unexpected modifier\n";
68     }
69 }
70
71 static const char *debug_argmod(GLuint mod) {
72     switch(mod) {
73         case GL_NONE:
74             return "GL_NONE";
75
76         case GL_2X_BIT_ATI:
77             return "GL_2X_BIT_ATI";
78         case GL_COMP_BIT_ATI:
79             return "GL_COMP_BIT_ATI";
80         case GL_NEGATE_BIT_ATI:
81             return "GL_NEGATE_BIT_ATI";
82         case GL_BIAS_BIT_ATI:
83             return "GL_BIAS_BIT_ATI";
84
85         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI:
86             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI";
87         case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI:
88             return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI";
89         case GL_2X_BIT_ATI | GL_BIAS_BIT_ATI:
90             return "GL_2X_BIT_ATI | GL_BIAS_BIT_ATI";
91         case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
92             return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
93         case GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
94             return "GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
95         case GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
96             return "GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
97
98         case GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
99             return "GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
100         case GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
101             return "GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
102         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI:
103             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_BIAS_BIT_ATI";
104         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI:
105             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI";
106
107         case GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI:
108             return "GL_2X_BIT_ATI | GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI | GL_BIAS_BIT_ATI";
109
110         default:
111             return "Unexpected argmod combination\n";
112     }
113 }
114 static const char *debug_register(GLuint reg) {
115     switch(reg) {
116         case GL_REG_0_ATI:                  return "GL_REG_0_ATI";
117         case GL_REG_1_ATI:                  return "GL_REG_1_ATI";
118         case GL_REG_2_ATI:                  return "GL_REG_2_ATI";
119         case GL_REG_3_ATI:                  return "GL_REG_3_ATI";
120         case GL_REG_4_ATI:                  return "GL_REG_4_ATI";
121         case GL_REG_5_ATI:                  return "GL_REG_5_ATI";
122
123         case GL_CON_0_ATI:                  return "GL_CON_0_ATI";
124         case GL_CON_1_ATI:                  return "GL_CON_1_ATI";
125         case GL_CON_2_ATI:                  return "GL_CON_2_ATI";
126         case GL_CON_3_ATI:                  return "GL_CON_3_ATI";
127         case GL_CON_4_ATI:                  return "GL_CON_4_ATI";
128         case GL_CON_5_ATI:                  return "GL_CON_5_ATI";
129         case GL_CON_6_ATI:                  return "GL_CON_6_ATI";
130         case GL_CON_7_ATI:                  return "GL_CON_7_ATI";
131
132         case GL_ZERO:                       return "GL_ZERO";
133         case GL_ONE:                        return "GL_ONE";
134         case GL_PRIMARY_COLOR:              return "GL_PRIMARY_COLOR";
135         case GL_SECONDARY_INTERPOLATOR_ATI: return "GL_SECONDARY_INTERPOLATOR_ATI";
136
137         default:                            return "Unknown register\n";
138     }
139 }
140
141 static const char *debug_swizzle(GLuint swizzle) {
142     switch(swizzle) {
143         case GL_SWIZZLE_STR_ATI:        return "GL_SWIZZLE_STR_ATI";
144         case GL_SWIZZLE_STQ_ATI:        return "GL_SWIZZLE_STQ_ATI";
145         case GL_SWIZZLE_STR_DR_ATI:     return "GL_SWIZZLE_STR_DR_ATI";
146         case GL_SWIZZLE_STQ_DQ_ATI:     return "GL_SWIZZLE_STQ_DQ_ATI";
147         default:                        return "unknown swizzle";
148     }
149 }
150
151 static const char *debug_rep(GLuint rep) {
152     switch(rep) {
153         case GL_NONE:                   return "GL_NONE";
154         case GL_RED:                    return "GL_RED";
155         case GL_GREEN:                  return "GL_GREEN";
156         case GL_BLUE:                   return "GL_BLUE";
157         default:                        return "unknown argrep";
158     }
159 }
160
161 #define GLINFO_LOCATION (*gl_info)
162 static GLuint register_for_arg(DWORD arg, WineD3D_GL_Info *gl_info, unsigned int stage, GLuint *mod, GLuint *rep, GLuint tmparg) {
163     GLenum ret;
164
165     if(mod) *mod = GL_NONE;
166     if(arg == ARG_UNUSED) return -1; /* This is the marker for unused registers */
167
168     switch(arg & WINED3DTA_SELECTMASK) {
169         case WINED3DTA_DIFFUSE:
170             ret = GL_PRIMARY_COLOR;
171             break;
172
173         case WINED3DTA_CURRENT:
174             /* Note that using GL_REG_0_ATI for the passed on register is safe because
175              * texture0 is read at stage0, so in the worst case it is read in the
176              * instruction writing to reg0. Afterwards texture0 is not used any longer.
177              * If we're reading from current
178              */
179             if(stage == 0) {
180                 ret = GL_PRIMARY_COLOR;
181             } else {
182                 ret = GL_REG_0_ATI;
183             }
184             break;
185
186         case WINED3DTA_TEXTURE:
187             ret = GL_REG_0_ATI + stage;
188             break;
189
190         case WINED3DTA_TFACTOR:
191             ret = ATI_FFP_CONST_TFACTOR;
192             break;
193
194         case WINED3DTA_SPECULAR:
195             ret = GL_SECONDARY_INTERPOLATOR_ATI;
196             break;
197
198         case WINED3DTA_TEMP:
199             ret = tmparg;
200             break;
201
202         case WINED3DTA_CONSTANT:
203             FIXME("Unhandled source argument WINED3DTA_TEMP\n");
204             ret = GL_CON_0_ATI;
205             break;
206
207         default:
208             FIXME("Unknown source argument %d\n", arg);
209             ret = GL_ZERO;
210     }
211
212     if(arg & WINED3DTA_COMPLEMENT) {
213         if(mod) *mod |= GL_COMP_BIT_ATI;
214     }
215     if(arg & WINED3DTA_ALPHAREPLICATE) {
216         if(rep) *rep = GL_ALPHA;
217     } else {
218         if(rep) *rep = GL_NONE;
219     }
220     return ret;
221 }
222
223 static GLuint find_tmpreg(struct texture_stage_op op[MAX_TEXTURES]) {
224     int lowest_read = -1;
225     int lowest_write = -1;
226     int i;
227     BOOL tex_used[MAX_TEXTURES];
228
229     memset(tex_used, 0, sizeof(tex_used));
230     for(i = 0; i < MAX_TEXTURES; i++) {
231         if(op[i].cop == WINED3DTOP_DISABLE) {
232             break;
233         }
234
235         if(lowest_read == -1 &&
236           (op[i].carg1 == WINED3DTA_TEMP || op[i].carg2 == WINED3DTA_TEMP || op[i].carg0 == WINED3DTA_TEMP ||
237            op[i].aarg1 == WINED3DTA_TEMP || op[i].aarg2 == WINED3DTA_TEMP || op[i].aarg0 == WINED3DTA_TEMP)) {
238             lowest_read = i;
239         }
240
241         if(lowest_write == -1 && op[i].dst == tempreg) {
242             lowest_write = i;
243         }
244
245         if(op[i].carg1 == WINED3DTA_TEXTURE || op[i].carg2 == WINED3DTA_TEXTURE || op[i].carg0 == WINED3DTA_TEXTURE ||
246            op[i].aarg1 == WINED3DTA_TEXTURE || op[i].aarg2 == WINED3DTA_TEXTURE || op[i].aarg0 == WINED3DTA_TEXTURE) {
247             tex_used[i] = TRUE;
248         }
249     }
250
251     /* Temp reg not read? We don't need it, return GL_NONE */
252     if(lowest_read == -1) return GL_NONE;
253
254     if(lowest_write >= lowest_read) {
255         FIXME("Temp register read before being written\n");
256     }
257
258     if(lowest_write == -1) {
259         /* This needs a test. Maybe we are supposed to return 0.0/0.0/0.0/0.0, or fail drawprim, or whatever */
260         FIXME("Temp register read without being written\n");
261         return GL_REG_1_ATI;
262     } else if(lowest_write >= 1) {
263         /* If we're writing to the temp reg at earliest in stage 1, we can use register 1 for the temp result.
264          * there may be texture data stored in reg 1, but we do not need it any longer since stage 1 already
265          * read it
266          */
267         return GL_REG_1_ATI;
268     } else {
269         /* Search for a free texture register. We have 6 registers available. GL_REG_0_ATI is already used
270          * for the regular result
271          */
272         for(i = 1; i < 6; i++) {
273             if(!tex_used[i]) {
274                 return GL_REG_0_ATI + i;
275             }
276         }
277         /* What to do here? Report it in ValidateDevice? */
278         FIXME("Could not find a register for the temporary register\n");
279         return 0;
280     }
281 }
282
283 static GLuint gen_ati_shader(struct texture_stage_op op[MAX_TEXTURES], WineD3D_GL_Info *gl_info) {
284     GLuint ret = GL_EXTCALL(glGenFragmentShadersATI(1));
285     unsigned int stage;
286     GLuint arg0, arg1, arg2, extrarg;
287     GLuint dstmod, argmod0, argmod1, argmod2, argmodextra;
288     GLuint rep0, rep1, rep2;
289     GLuint swizzle;
290     GLuint tmparg = find_tmpreg(op);
291     GLuint dstreg;
292
293     if(!ret) {
294         ERR("Failed to generate a GL_ATI_fragment_shader shader id\n");
295         return 0;
296     }
297     GL_EXTCALL(glBindFragmentShaderATI(ret));
298     checkGLcall("GL_EXTCALL(glBindFragmentShaderATI(ret))");
299
300     TRACE("glBeginFragmentShaderATI()\n");
301     GL_EXTCALL(glBeginFragmentShaderATI());
302     checkGLcall("GL_EXTCALL(glBeginFragmentShaderATI())");
303
304     /* Pass 1: Generate sampling instructions for perturbation maps */
305       for(stage = 0; stage < GL_LIMITS(textures); stage++) {
306         if(op[stage].cop == WINED3DTOP_DISABLE) break;
307         if(op[stage].cop != WINED3DTOP_BUMPENVMAP &&
308            op[stage].cop != WINED3DTOP_BUMPENVMAPLUMINANCE) continue;
309
310         TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, GL_SWIZZLE_STR_ATI)\n",
311               stage, stage);
312         GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
313                    GL_TEXTURE0_ARB + stage,
314                    GL_SWIZZLE_STR_ATI));
315         TRACE("glPassTexCoordATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, GL_SWIZZLE_STR_ATI)\n",
316               stage + 1, stage + 1);
317         GL_EXTCALL(glPassTexCoordATI(GL_REG_0_ATI + stage + 1,
318                    GL_TEXTURE0_ARB + stage + 1,
319                    GL_SWIZZLE_STR_ATI));
320
321         /* We need GL_REG_5_ATI as a temporary register to swizzle the bump matrix. So we run into
322          * issues if we're bump mapping on stage 4 or 5
323          */
324         if(stage >= 4) {
325             FIXME("Bump mapping in stage %d\n", stage);
326         }
327     }
328
329     /* Pass 2: Generate perturbation calculations */
330     for(stage = 0; stage < GL_LIMITS(textures); stage++) {
331         if(op[stage].cop == WINED3DTOP_DISABLE) break;
332         if(op[stage].cop != WINED3DTOP_BUMPENVMAP &&
333            op[stage].cop != WINED3DTOP_BUMPENVMAPLUMINANCE) continue;
334
335         /* Nice thing, we get the color correction for free :-) */
336         if(op[stage].color_correction == WINED3DFMT_V8U8) {
337             argmodextra = GL_2X_BIT_ATI | GL_BIAS_BIT_ATI;
338         } else {
339             argmodextra = 0;
340         }
341
342         TRACE("glColorFragmentOp3ATI(GL_DOT2_ADD_ATI, GL_REG_%d_ATI, GL_RED_BIT_ATI, GL_NONE, GL_REG_%d_ATI, GL_NONE, %s, ATI_FFP_CONST_BUMPMAT(%d), GL_NONE, GL_NONE, GL_REG_%d_ATI, GL_RED, GL_NONE)\n",
343               stage + 1, stage, debug_argmod(argmodextra), stage, stage + 1);
344         GL_EXTCALL(glColorFragmentOp3ATI(GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_RED_BIT_ATI, GL_NONE,
345                                          GL_REG_0_ATI + stage, GL_NONE, argmodextra,
346                                          ATI_FFP_CONST_BUMPMAT(stage), GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
347                                          GL_REG_0_ATI + stage + 1, GL_RED, GL_NONE));
348
349         /* FIXME: How can I make GL_DOT2_ADD_ATI read the factors from blue and alpha? It defaults to red and green,
350          * and it is fairly easy to make it read GL_BLUE or BL_ALPHA, but I can't get an R * B + G * A. So we're wasting
351          * one register and two instructions in this pass for a simple swizzling operation.
352          * For starters it might be good enough to merge the two movs into one, but even that isn't possible :-(
353          *
354          * NOTE: GL_BLUE | GL_ALPHA is not possible. It doesn't throw a compilation error, but an OR operation on the
355          * constants doesn't make sense, considering their values.
356          */
357         TRACE("glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_5_ATI, GL_RED_BIT_ATI, GL_NONE, ATI_FFP_CONST_BUMPMAT(%d), GL_BLUE, GL_NONE)\n", stage);
358         GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_5_ATI, GL_RED_BIT_ATI, GL_NONE,
359                                          ATI_FFP_CONST_BUMPMAT(stage), GL_BLUE, GL_NONE));
360         TRACE("glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_5_ATI, GL_GREEN_BIT_ATI, GL_NONE, ATI_FFP_CONST_BUMPMAT(%d), GL_ALPHA, GL_NONE)\n", stage);
361         GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_5_ATI, GL_GREEN_BIT_ATI, GL_NONE,
362                                         ATI_FFP_CONST_BUMPMAT(stage), GL_ALPHA, GL_NONE));
363         TRACE("glColorFragmentOp3ATI(GL_DOT2_ADD_ATI, GL_REG_%d_ATI, GL_GREEN_BIT_ATI, GL_NONE, GL_REG_%d_ATI, GL_NONE, %s, GL_REG_5_ATI, GL_NONE, GL_NONE, GL_REG_%d_ATI, GL_GREEN, GL_NONE)\n",
364               stage + 1, stage, debug_argmod(argmodextra), stage + 1);
365         GL_EXTCALL(glColorFragmentOp3ATI(GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_GREEN_BIT_ATI, GL_NONE,
366                                          GL_REG_0_ATI + stage, GL_NONE, argmodextra,
367                                          GL_REG_5_ATI, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
368                                          GL_REG_0_ATI + stage + 1, GL_GREEN, GL_NONE));
369     }
370
371     /* Pass 3: Generate sampling instructions for regular textures */
372     for(stage = 0; stage < GL_LIMITS(textures); stage++) {
373         if(op[stage].cop == WINED3DTOP_DISABLE) {
374             break;
375         }
376
377         if(op[stage].projected == proj_none) {
378             swizzle = GL_SWIZZLE_STR_ATI;
379         } else if(op[stage].projected == proj_count3) {
380             /* TODO: D3DTTFF_COUNT3 | D3DTTFF_PROJECTED would be GL_SWIZZLE_STR_DR_ATI.
381              * However, the FFP vertex processing texture transform matrix handler does
382              * some transformations in the texture matrix which makes the 3rd coordinate
383              * arrive in Q, not R in that case. This is needed for opengl fixed function
384              * fragment processing which always divides by Q. In this backend we can
385              * handle that properly and be compatible with vertex shader output and avoid
386              * side effects of the texture matrix games
387              */
388             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
389         } else {
390             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
391         }
392
393         if((op[stage].carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
394            (op[stage].carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
395            (op[stage].carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
396            (op[stage].aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
397            (op[stage].aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
398            (op[stage].aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
399             op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) {
400
401             if(stage > 0 &&
402                (op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
403                 op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
404                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_REG_%d_ATI, GL_SWIZZLE_STR_ATI)\n",
405                       stage, stage);
406                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
407                            GL_REG_0_ATI + stage,
408                            GL_SWIZZLE_STR_ATI));
409             } else {
410                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
411                     stage, stage, debug_swizzle(swizzle));
412                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
413                                         GL_TEXTURE0_ARB + stage,
414                                         swizzle));
415             }
416         }
417     }
418
419     /* Pass 4: Generate the arithmetic instructions */
420     for(stage = 0; stage < MAX_TEXTURES; stage++) {
421         if(op[stage].cop == WINED3DTOP_DISABLE) {
422             if(stage == 0) {
423                 /* Handle complete texture disabling gracefully */
424                 TRACE("glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE, GL_PRIMARY_COLOR, GL_NONE, GL_NONE)\n");
425                 GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
426                                                  GL_PRIMARY_COLOR, GL_NONE, GL_NONE));
427                 TRACE("glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_PRIMARY_COLOR, GL_NONE, GL_NONE)\n");
428                 GL_EXTCALL(glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE,
429                                                  GL_PRIMARY_COLOR, GL_NONE, GL_NONE));
430             }
431             break;
432         }
433
434         if(op[stage].dst == tempreg) {
435             /* If we're writing to D3DTA_TEMP, but never reading from it we don't have to write there in the first place.
436              * skip the entire stage, this saves some GPU time
437              */
438             if(tmparg == GL_NONE) continue;
439
440             dstreg = tmparg;
441         } else {
442             dstreg = GL_REG_0_ATI;
443         }
444
445         arg0 = register_for_arg(op[stage].carg0, gl_info, stage, &argmod0, &rep0, tmparg);
446         arg1 = register_for_arg(op[stage].carg1, gl_info, stage, &argmod1, &rep1, tmparg);
447         arg2 = register_for_arg(op[stage].carg2, gl_info, stage, &argmod2, &rep2, tmparg);
448         dstmod = GL_NONE;
449         argmodextra = GL_NONE;
450         extrarg = GL_NONE;
451
452         switch(op[stage].cop) {
453             case WINED3DTOP_SELECTARG2:
454                 arg1 = arg2;
455                 argmod1 = argmod2;
456                 rep1 = rep2;
457             case WINED3DTOP_SELECTARG1:
458                 TRACE("glColorFragmentOp1ATI(GL_MOV_ATI, %s, GL_NONE, GL_NONE, %s, %s, %s)\n",
459                       debug_register(dstreg), debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1));
460                 GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, dstreg, GL_NONE, GL_NONE,
461                                                  arg1, rep1, argmod1));
462                 break;
463
464             case WINED3DTOP_MODULATE4X:
465                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
466             case WINED3DTOP_MODULATE2X:
467                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
468                 dstmod |= GL_SATURATE_BIT_ATI;
469             case WINED3DTOP_MODULATE:
470                 TRACE("glColorFragmentOp2ATI(GL_MUL_ATI, %s, GL_NONE, %s, %s, %s, %s, %s, %s, %s)\n",
471                       debug_register(dstreg), debug_dstmod(dstmod),
472                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
473                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2));
474                 GL_EXTCALL(glColorFragmentOp2ATI(GL_MUL_ATI, dstreg, GL_NONE, dstmod,
475                                                  arg1, rep1, argmod1,
476                                                  arg2, rep2, argmod2));
477                 break;
478
479             case WINED3DTOP_ADDSIGNED2X:
480                 dstmod = GL_2X_BIT_ATI;
481             case WINED3DTOP_ADDSIGNED:
482                 argmodextra = GL_BIAS_BIT_ATI;
483             case WINED3DTOP_ADD:
484                 dstmod |= GL_SATURATE_BIT_ATI;
485                 TRACE("glColorFragmentOp2ATI(GL_ADD_ATI, %s, GL_NONE, %s, %s, %s, %s, %s, %s, %s)\n",
486                       debug_register(dstreg), debug_dstmod(dstmod),
487                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
488                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmodextra | argmod2));
489                 GL_EXTCALL(glColorFragmentOp2ATI(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, dstmod,
490                                                  arg1, rep1, argmod1,
491                                                  arg2, rep2, argmodextra | argmod2));
492                 break;
493
494             case WINED3DTOP_SUBTRACT:
495                 dstmod |= GL_SATURATE_BIT_ATI;
496                 TRACE("glColorFragmentOp2ATI(GL_SUB_ATI, %s, GL_NONE, %s, %s, %s, %s, %s, %s, %s)\n",
497                       debug_register(dstreg), debug_dstmod(dstmod),
498                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
499                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2));
500                 GL_EXTCALL(glColorFragmentOp2ATI(GL_SUB_ATI, dstreg, GL_NONE, dstmod,
501                                                  arg1, rep1, argmod1,
502                                                  arg2, rep2, argmod2));
503                 break;
504
505             case WINED3DTOP_ADDSMOOTH:
506                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
507                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n",
508                       debug_register(dstreg),
509                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
510                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmodextra),
511                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1));
512                 /* Dst = arg1 + * arg2(1 -arg 1)
513                  *     = arg2 * (1 - arg1) + arg1
514                  */
515                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
516                                                  arg2, rep2, argmod2,
517                                                  arg1, rep1, argmodextra,
518                                                  arg1, rep1, argmod1));
519                 break;
520
521             case WINED3DTOP_BLENDCURRENTALPHA:
522                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
523             case WINED3DTOP_BLENDFACTORALPHA:
524                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
525             case WINED3DTOP_BLENDTEXTUREALPHA:
526                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
527             case WINED3DTOP_BLENDDIFFUSEALPHA:
528                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
529                 TRACE("glColorFragmentOp3ATI(GL_LERP_ATI, %s, GL_NONE, GL_NONE, %s, GL_ALPHA, GL_NONE, %s, %s, %s, %s, %s, %s)\n",
530                       debug_register(dstreg),
531                       debug_register(extrarg),
532                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
533                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2));
534                 GL_EXTCALL(glColorFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
535                                                  extrarg, GL_ALPHA, GL_NONE,
536                                                  arg1, rep1, argmod1,
537                                                  arg2, rep2, argmod2));
538                 break;
539
540             case WINED3DTOP_BLENDTEXTUREALPHAPM:
541                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
542                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_NONE, %s, %s, %s, %s, GL_ALPHA, GL_COMP_BIT_ATI, %s, %s, %s)\n",
543                       debug_register(dstreg),
544                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
545                       debug_register(arg0),
546                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1));
547                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_NONE,
548                                                  arg2, rep2,  argmod2,
549                                                  arg0, GL_ALPHA, GL_COMP_BIT_ATI,
550                                                  arg1, rep1,  argmod1));
551                 break;
552
553             /* D3DTOP_PREMODULATE ???? */
554
555             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
556                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
557             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
558                 if(!argmodextra) argmodextra = argmod1;
559                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, GL_ALPHA, %s, %s, %s, %s)\n",
560                       debug_register(dstreg),
561                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
562                       debug_register(arg1), debug_argmod(argmodextra), debug_register(arg1), debug_rep(rep1), debug_argmod(arg1));
563                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
564                                                  arg2, rep2,  argmod2,
565                                                  arg1, GL_ALPHA, argmodextra,
566                                                  arg1, rep1,  argmod1));
567                 break;
568
569             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
570                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
571             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
572                 if(!argmodextra) argmodextra = argmod1;
573                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s, %s, GL_ALPHA, %s)\n",
574                       debug_register(dstreg),
575                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
576                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmodextra),
577                       debug_register(arg1), debug_argmod(argmod1));
578                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
579                                                  arg2, rep2,  argmod2,
580                                                  arg1, rep1,  argmodextra,
581                                                  arg1, GL_ALPHA, argmod1));
582                 break;
583
584             case WINED3DTOP_DOTPRODUCT3:
585                 TRACE("glColorFragmentOp2ATI(GL_DOT3_ATI, %s, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s)\n",
586                       debug_register(dstreg),
587                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1 | GL_BIAS_BIT_ATI),
588                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2 | GL_BIAS_BIT_ATI));
589                 GL_EXTCALL(glColorFragmentOp2ATI(GL_DOT3_ATI, dstreg, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
590                                                  arg1, rep1, argmod1 | GL_BIAS_BIT_ATI,
591                                                  arg2, rep2, argmod2 | GL_BIAS_BIT_ATI));
592                 break;
593
594             case WINED3DTOP_MULTIPLYADD:
595                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n",
596                       debug_register(dstreg),
597                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
598                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
599                       debug_register(arg0), debug_rep(rep0), debug_argmod(argmod0));
600                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
601                                                  arg1, rep1, argmod1,
602                                                  arg2, rep2, argmod2,
603                                                  arg0, rep0, argmod0));
604                 break;
605
606             case WINED3DTOP_LERP:
607                 TRACE("glColorFragmentOp3ATI(GL_LERP_ATI, %s, GL_NONE, GL_NONE, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n",
608                       debug_register(dstreg),
609                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
610                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
611                       debug_register(arg0), debug_rep(rep0), debug_argmod(argmod0));
612                 GL_EXTCALL(glColorFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
613                                                  arg0, rep0, argmod0,
614                                                  arg1, rep1, argmod1,
615                                                  arg2, rep2, argmod2));
616                 break;
617
618             case WINED3DTOP_BUMPENVMAP:
619             case WINED3DTOP_BUMPENVMAPLUMINANCE:
620                 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
621                 break;
622
623             default: FIXME("Unhandled color operation %d on stage %d\n", op[stage].cop, stage);
624         }
625
626         arg0 = register_for_arg(op[stage].aarg0, gl_info, stage, &argmod0, NULL, tmparg);
627         arg1 = register_for_arg(op[stage].aarg1, gl_info, stage, &argmod1, NULL, tmparg);
628         arg2 = register_for_arg(op[stage].aarg2, gl_info, stage, &argmod2, NULL, tmparg);
629         dstmod = GL_NONE;
630         argmodextra = GL_NONE;
631         extrarg = GL_NONE;
632
633         switch(op[stage].aop) {
634             case WINED3DTOP_DISABLE:
635                 /* Get the primary color to the output if on stage 0, otherwise leave register 0 untouched */
636                 if(stage == 0) {
637                     TRACE("glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_PRIMARY_COLOR, GL_NONE, GL_NONE)\n");
638                     GL_EXTCALL(glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE,
639                                GL_PRIMARY_COLOR, GL_NONE, GL_NONE));
640                 }
641                 break;
642
643             case WINED3DTOP_SELECTARG2:
644                 arg1 = arg2;
645                 argmod1 = argmod2;
646             case WINED3DTOP_SELECTARG1:
647                 TRACE("glAlphaFragmentOp1ATI(GL_MOV_ATI, %s,          GL_NONE, %s, GL_NONE, %s)\n",
648                       debug_register(dstreg),
649                       debug_register(arg1), debug_argmod(argmod1));
650                 GL_EXTCALL(glAlphaFragmentOp1ATI(GL_MOV_ATI, dstreg, GL_NONE,
651                                                  arg1, GL_NONE, argmod1));
652                 break;
653
654             case WINED3DTOP_MODULATE4X:
655                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
656             case WINED3DTOP_MODULATE2X:
657                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
658                 dstmod |= GL_SATURATE_BIT_ATI;
659             case WINED3DTOP_MODULATE:
660                 TRACE("glAlphaFragmentOp2ATI(GL_MUL_ATI, %s,          %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
661                       debug_register(dstreg), debug_dstmod(dstmod),
662                       debug_register(arg1), debug_argmod(argmod1),
663                       debug_register(arg2), debug_argmod(argmod2));
664                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_MUL_ATI, dstreg, dstmod,
665                                                  arg1, GL_NONE, argmod1,
666                                                  arg2, GL_NONE, argmod2));
667                 break;
668
669             case WINED3DTOP_ADDSIGNED2X:
670                 dstmod = GL_2X_BIT_ATI;
671             case WINED3DTOP_ADDSIGNED:
672                 argmodextra = GL_BIAS_BIT_ATI;
673             case WINED3DTOP_ADD:
674                 dstmod |= GL_SATURATE_BIT_ATI;
675                 TRACE("glAlphaFragmentOp2ATI(GL_ADD_ATI, %s,          %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
676                       debug_register(dstreg), debug_dstmod(dstmod),
677                       debug_register(arg1), debug_argmod(argmod1),
678                       debug_register(arg2), debug_argmod(argmodextra | argmod2));
679                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_ADD_ATI, dstreg, dstmod,
680                                                  arg1, GL_NONE, argmod1,
681                                                  arg2, GL_NONE, argmodextra | argmod2));
682                 break;
683
684             case WINED3DTOP_SUBTRACT:
685                 dstmod |= GL_SATURATE_BIT_ATI;
686                 TRACE("glAlphaFragmentOp2ATI(GL_SUB_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
687                       debug_register(dstreg), debug_dstmod(dstmod),
688                       debug_register(arg1), debug_argmod(argmod1),
689                       debug_register(arg2), debug_argmod(argmod2));
690                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_SUB_ATI, dstreg, dstmod,
691                                                  arg1, GL_NONE, argmod1,
692                                                  arg2, GL_NONE, argmod2));
693                 break;
694
695             case WINED3DTOP_ADDSMOOTH:
696                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
697                 TRACE("glAlphaFragmentOp3ATI(GL_MAD_ATI, %s,          GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
698                       debug_register(dstreg),
699                       debug_register(arg2), debug_argmod(argmod2),
700                       debug_register(arg1), debug_argmod(argmodextra),
701                       debug_register(arg1), debug_argmod(argmod1));
702                 /* Dst = arg1 + * arg2(1 -arg 1)
703                  *     = arg2 * (1 - arg1) + arg1
704                  */
705                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_SATURATE_BIT_ATI,
706                                                  arg2, GL_NONE, argmod2,
707                                                  arg1, GL_NONE, argmodextra,
708                                                  arg1, GL_NONE, argmod1));
709                 break;
710
711             case WINED3DTOP_BLENDCURRENTALPHA:
712                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
713             case WINED3DTOP_BLENDFACTORALPHA:
714                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
715             case WINED3DTOP_BLENDTEXTUREALPHA:
716                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
717             case WINED3DTOP_BLENDDIFFUSEALPHA:
718                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
719                 TRACE("glAlphaFragmentOp3ATI(GL_LERP_ATI, %s,          GL_NONE, %s, GL_ALPHA, GL_NONE, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
720                       debug_register(dstreg),
721                       debug_register(extrarg),
722                       debug_register(arg1), debug_argmod(argmod1),
723                       debug_register(arg2), debug_argmod(argmod2));
724                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_NONE,
725                                                  extrarg, GL_ALPHA, GL_NONE,
726                                                  arg1, GL_NONE, argmod1,
727                                                  arg2, GL_NONE, argmod2));
728                 break;
729
730             case WINED3DTOP_BLENDTEXTUREALPHAPM:
731                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
732                 TRACE("glAlphaFragmentOp3ATI(GL_MAD_ATI, %s,          GL_NONE, %s, GL_NONE, %s, %s, GL_ALPHA, GL_COMP_BIT_ATI, %s, GL_NONE, %s)\n",
733                       debug_register(dstreg),
734                       debug_register(arg2), debug_argmod(argmod2),
735                       debug_register(arg0),
736                       debug_register(arg1), debug_argmod(argmod1));
737                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE,
738                                                  arg2, GL_NONE,  argmod2,
739                                                  arg0, GL_ALPHA, GL_COMP_BIT_ATI,
740                                                  arg1, GL_NONE,  argmod1));
741                 break;
742
743             /* D3DTOP_PREMODULATE ???? */
744
745             case WINED3DTOP_DOTPRODUCT3:
746                 TRACE("glAlphaFragmentOp2ATI(GL_DOT3_ATI, %s,          GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
747                       debug_register(dstreg),
748                       debug_register(arg1), debug_argmod(argmod1 | GL_BIAS_BIT_ATI),
749                       debug_register(arg2), debug_argmod(argmod2 | GL_BIAS_BIT_ATI));
750                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_DOT3_ATI, dstreg, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
751                                                  arg1, GL_NONE, argmod1 | GL_BIAS_BIT_ATI,
752                                                  arg2, GL_NONE, argmod2 | GL_BIAS_BIT_ATI));
753                 break;
754
755             case WINED3DTOP_MULTIPLYADD:
756                 TRACE("glAlphaFragmentOp3ATI(GL_MAD_ATI, %s,          GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
757                       debug_register(dstreg),
758                       debug_register(arg1), debug_argmod(argmod1),
759                       debug_register(arg2), debug_argmod(argmod2),
760                       debug_register(arg0), debug_argmod(argmod0));
761                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_MAD_ATI, dstreg,          GL_SATURATE_BIT_ATI,
762                            arg1, GL_NONE, argmod1,
763                            arg2, GL_NONE, argmod2,
764                            arg0, GL_NONE, argmod0));
765                 break;
766
767             case WINED3DTOP_LERP:
768                 TRACE("glAlphaFragmentOp3ATI(GL_LERP_ATI, %s,          GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
769                       debug_register(dstreg),
770                       debug_register(arg1), debug_argmod(argmod1),
771                       debug_register(arg2), debug_argmod(argmod2),
772                       debug_register(arg0), debug_argmod(argmod0));
773                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_SATURATE_BIT_ATI,
774                                                  arg1, GL_NONE, argmod1,
775                                                  arg2, GL_NONE, argmod2,
776                                                  arg0, GL_NONE, argmod0));
777                 break;
778
779             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
780             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
781             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
782             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
783             case WINED3DTOP_BUMPENVMAP:
784             case WINED3DTOP_BUMPENVMAPLUMINANCE:
785                 ERR("Application uses an invalid alpha operation\n");
786                 break;
787
788             default: FIXME("Unhandled alpha operation %d on stage %d\n", op[stage].aop, stage);
789         }
790     }
791
792     TRACE("glEndFragmentShaderATI()\n");
793     GL_EXTCALL(glEndFragmentShaderATI());
794     checkGLcall("GL_EXTCALL(glEndFragmentShaderATI())");
795     return ret;
796 }
797 #undef GLINFO_LOCATION
798
799 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
800 static void set_tex_op_atifs(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
801     IWineD3DDeviceImpl          *This = stateblock->wineD3DDevice;
802     struct atifs_ffp_desc       *desc;
803     struct ffp_settings settings;
804     struct atifs_private_data   *priv = (struct atifs_private_data *) This->fragment_priv;
805     DWORD mapped_stage;
806     unsigned int i;
807
808     gen_ffp_op(stateblock, &settings, TRUE);
809     desc = (struct atifs_ffp_desc *) find_ffp_shader(priv->fragment_shaders, &settings);
810     if(!desc) {
811         desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
812         if(!desc) {
813             ERR("Out of memory\n");
814             return;
815         }
816         desc->num_textures_used = 0;
817         for(i = 0; i < GL_LIMITS(texture_stages); i++) {
818             if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
819             desc->num_textures_used = i;
820         }
821
822         memcpy(&desc->parent.settings, &settings, sizeof(settings));
823         desc->shader = gen_ati_shader(settings.op, &GLINFO_LOCATION);
824         add_ffp_shader(priv->fragment_shaders, &desc->parent);
825         TRACE("Allocated fixed function replacement shader descriptor %p\n", desc);
826     }
827
828     /* GL_ATI_fragment_shader depends on the GL_TEXTURE_xD enable settings. Update the texture stages
829      * used by this shader
830      */
831     for(i = 0; i < desc->num_textures_used; i++) {
832         mapped_stage = This->texUnitMap[i];
833         if(mapped_stage != -1) {
834             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
835             checkGLcall("glActiveTextureARB");
836             texture_activate_dimensions(i, stateblock, context);
837         }
838     }
839
840     GL_EXTCALL(glBindFragmentShaderATI(desc->shader));
841 }
842
843 static void state_texfactor_atifs(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
844     float col[4];
845     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
846
847     GL_EXTCALL(glSetFragmentShaderConstantATI(ATI_FFP_CONST_TFACTOR, col));
848     checkGLcall("glSetFragmentShaderConstantATI(ATI_FFP_CONST_TFACTOR, col)");
849 }
850
851 static void set_bumpmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
852     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
853     float mat[2][2];
854
855     mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
856     mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
857     mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
858     mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
859     /* GL_ATI_fragment_shader allows only constants from 0.0 to 1.0, but the bumpmat
860      * constants can be in any range. While they should stay between [-1.0 and 1.0] because
861      * Shader Model 1.x pixel shaders are clamped to that range negative values are used occasionally,
862      * for example by our d3d9 test. So to get negative values scale -1;1 to 0;1 and undo that in the
863      * shader(it is free). This might potentially reduce precision. However, if the hardware does
864      * support proper floats it shouldn't, and if it doesn't we can't get anything better anyway
865      */
866     mat[0][0] = (mat[0][0] + 1.0) * 0.5;
867     mat[1][0] = (mat[1][0] + 1.0) * 0.5;
868     mat[0][1] = (mat[0][1] + 1.0) * 0.5;
869     mat[1][1] = (mat[1][1] + 1.0) * 0.5;
870     GL_EXTCALL(glSetFragmentShaderConstantATI(ATI_FFP_CONST_BUMPMAT(stage), (float *) mat));
871     checkGLcall("glSetFragmentShaderConstantATI(ATI_FFP_CONST_BUMPMAT(stage), mat)");
872 }
873 #undef GLINFO_LOCATION
874
875 static const struct StateEntryTemplate atifs_fragmentstate_template[] = {
876     {STATE_RENDER(WINED3DRS_TEXTUREFACTOR),               { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              state_texfactor_atifs   }, 0                               },
877     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
878     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
879     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
880     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
881     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
882     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
883     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
884     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
885     {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
886     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
887     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
888     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
889     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
890     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
891     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
892     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
893     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
894     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
895     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
896     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
897     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
898     {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
899     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
900     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
901     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
902     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
903     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
904     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
905     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
906     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
907     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
908     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
909     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
910     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
911     {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
912     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
913     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
914     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
915     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
916     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
917     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
918     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
919     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
920     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
921     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
922     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
923     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
924     {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
925     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
926     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
927     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
928     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
929     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
930     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
931     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
932     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
933     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
934     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
935     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
936     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
937     {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
938     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
939     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
940     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
941     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
942     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
943     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
944     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
945     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
946     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
947     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
948     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
949     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
950     {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
951     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
952     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
953     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
954     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
955     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
956     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
957     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
958     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
959     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
960     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
961     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
962     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
963     {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
964     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
965     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
966     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
967     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
968     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
969     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
970     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
971     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
972     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
973     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
974     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
975     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
976     {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
977     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
978     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
979     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
980     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
981     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim          }, 0                               },
982     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim          }, 0                               },
983     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim          }, 0                               },
984     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim          }, 0                               },
985     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim          }, 0                               },
986     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim          }, 0                               },
987     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim          }, 0                               },
988     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim          }, 0                               },
989     {0 /* Terminate */,                                   { 0,                                                  0                       }, 0                               },
990 };
991
992 static void atifs_enable(IWineD3DDevice *iface, BOOL enable) {
993     if(enable) {
994         glEnable(GL_FRAGMENT_SHADER_ATI);
995         checkGLcall("glEnable(GL_FRAGMENT_SHADER_ATI)");
996     } else {
997         glDisable(GL_FRAGMENT_SHADER_ATI);
998         checkGLcall("glDisable(GL_FRAGMENT_SHADER_ATI)");
999     }
1000 }
1001
1002 static void atifs_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct fragment_caps *caps) {
1003     caps->TextureOpCaps =  WINED3DTEXOPCAPS_DISABLE                     |
1004                            WINED3DTEXOPCAPS_SELECTARG1                  |
1005                            WINED3DTEXOPCAPS_SELECTARG2                  |
1006                            WINED3DTEXOPCAPS_MODULATE4X                  |
1007                            WINED3DTEXOPCAPS_MODULATE2X                  |
1008                            WINED3DTEXOPCAPS_MODULATE                    |
1009                            WINED3DTEXOPCAPS_ADDSIGNED2X                 |
1010                            WINED3DTEXOPCAPS_ADDSIGNED                   |
1011                            WINED3DTEXOPCAPS_ADD                         |
1012                            WINED3DTEXOPCAPS_SUBTRACT                    |
1013                            WINED3DTEXOPCAPS_ADDSMOOTH                   |
1014                            WINED3DTEXOPCAPS_BLENDCURRENTALPHA           |
1015                            WINED3DTEXOPCAPS_BLENDFACTORALPHA            |
1016                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHA           |
1017                            WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA           |
1018                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM         |
1019                            WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR      |
1020                            WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA      |
1021                            WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA   |
1022                            WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR   |
1023                            WINED3DTEXOPCAPS_DOTPRODUCT3                 |
1024                            WINED3DTEXOPCAPS_MULTIPLYADD                 |
1025                            WINED3DTEXOPCAPS_LERP                        |
1026                            WINED3DTEXOPCAPS_BUMPENVMAP;
1027
1028     /* TODO: Implement WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1029     and WINED3DTEXOPCAPS_PREMODULATE */
1030
1031     /* GL_ATI_fragment_shader always supports 6 textures, which was the limit on r200 cards
1032      * which this extension is exclusively focused on(later cards have GL_ARB_fragment_program).
1033      * If the current card has more than 8 fixed function textures in OpenGL's regular fixed
1034      * function pipeline then the ATI_fragment_shader backend imposes a stricter limit. This
1035      * shouldn't be too hard since Nvidia cards have a limit of 4 textures with the default ffp
1036      * pipeline, and almost all games are happy with that. We can however support up to 8
1037      * texture stages because we have a 2nd pass limit of 8 instructions, and per stage we use
1038      * only 1 instruction.
1039      *
1040      * The proper fix for this is not to use GL_ATI_fragment_shader on cards newer than the
1041      * r200 series and use an ARB or GLSL shader instead
1042      */
1043     caps->MaxTextureBlendStages   = 8;
1044     caps->MaxSimultaneousTextures = 6;
1045
1046     caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
1047 }
1048
1049 static HRESULT atifs_alloc(IWineD3DDevice *iface) {
1050     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1051     struct atifs_private_data *priv;
1052
1053     This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct atifs_private_data));
1054     if(!This->fragment_priv) {
1055         ERR("Out of memory\n");
1056         return E_OUTOFMEMORY;
1057     }
1058     priv = (struct atifs_private_data *) This->fragment_priv;
1059     priv->fragment_shaders = hash_table_create(ffp_program_key_hash, ffp_program_key_compare);
1060     return WINED3D_OK;
1061 }
1062
1063 #define GLINFO_LOCATION This->adapter->gl_info
1064 static void atifs_free_ffpshader(void *value, void *device) {
1065     IWineD3DDeviceImpl *This = device;
1066     struct atifs_ffp_desc *entry_ati = value;
1067
1068     ENTER_GL();
1069     GL_EXTCALL(glDeleteFragmentShaderATI(entry_ati->shader));
1070     checkGLcall("glDeleteFragmentShaderATI(entry->shader)");
1071     HeapFree(GetProcessHeap(), 0, entry_ati);
1072     LEAVE_GL();
1073 }
1074
1075 static void atifs_free(IWineD3DDevice *iface) {
1076     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1077     struct atifs_private_data *priv = (struct atifs_private_data *) This->fragment_priv;
1078
1079     hash_table_destroy(priv->fragment_shaders, atifs_free_ffpshader, This);
1080
1081     HeapFree(GetProcessHeap(), 0, priv);
1082     This->fragment_priv = NULL;
1083 }
1084 #undef GLINFO_LOCATION
1085
1086 static BOOL atifs_conv_supported(WINED3DFORMAT fmt) {
1087     TRACE("Checking shader format support for format %s:", debug_d3dformat(fmt));
1088     switch(fmt) {
1089         case WINED3DFMT_V8U8:
1090         case WINED3DFMT_V16U16:
1091             TRACE("[OK]\n");
1092             return TRUE;
1093         default:
1094             TRACE("[FAILED\n");
1095             return FALSE;
1096     }
1097 }
1098
1099 const struct fragment_pipeline atifs_fragment_pipeline = {
1100     atifs_enable,
1101     atifs_get_caps,
1102     atifs_alloc,
1103     atifs_free,
1104     atifs_conv_supported,
1105     atifs_fragmentstate_template
1106 };