mshtml: Add tests for get_scrollLeft.
[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     struct 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         if(op[stage + 1].projected == proj_none) {
316             swizzle = GL_SWIZZLE_STR_ATI;
317         } else if(op[stage + 1].projected == proj_count4) {
318             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
319         } else {
320             swizzle = GL_SWIZZLE_STR_DR_ATI;
321         }
322         TRACE("glPassTexCoordATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
323               stage + 1, stage + 1, debug_swizzle(swizzle));
324         GL_EXTCALL(glPassTexCoordATI(GL_REG_0_ATI + stage + 1,
325                    GL_TEXTURE0_ARB + stage + 1,
326                    swizzle));
327
328         /* We need GL_REG_5_ATI as a temporary register to swizzle the bump matrix. So we run into
329          * issues if we're bump mapping on stage 4 or 5
330          */
331         if(stage >= 4) {
332             FIXME("Bump mapping in stage %d\n", stage);
333         }
334     }
335
336     /* Pass 2: Generate perturbation calculations */
337     for(stage = 0; stage < GL_LIMITS(textures); stage++) {
338         if(op[stage].cop == WINED3DTOP_DISABLE) break;
339         if(op[stage].cop != WINED3DTOP_BUMPENVMAP &&
340            op[stage].cop != WINED3DTOP_BUMPENVMAPLUMINANCE) continue;
341
342         /* Nice thing, we get the color correction for free :-) */
343         if(op[stage].color_correction == WINED3DFMT_V8U8) {
344             argmodextra = GL_2X_BIT_ATI | GL_BIAS_BIT_ATI;
345         } else {
346             argmodextra = 0;
347         }
348
349         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",
350               stage + 1, stage, debug_argmod(argmodextra), stage, stage + 1);
351         GL_EXTCALL(glColorFragmentOp3ATI(GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_RED_BIT_ATI, GL_NONE,
352                                          GL_REG_0_ATI + stage, GL_NONE, argmodextra,
353                                          ATI_FFP_CONST_BUMPMAT(stage), GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
354                                          GL_REG_0_ATI + stage + 1, GL_RED, GL_NONE));
355
356         /* FIXME: How can I make GL_DOT2_ADD_ATI read the factors from blue and alpha? It defaults to red and green,
357          * 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
358          * one register and two instructions in this pass for a simple swizzling operation.
359          * For starters it might be good enough to merge the two movs into one, but even that isn't possible :-(
360          *
361          * NOTE: GL_BLUE | GL_ALPHA is not possible. It doesn't throw a compilation error, but an OR operation on the
362          * constants doesn't make sense, considering their values.
363          */
364         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);
365         GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_5_ATI, GL_RED_BIT_ATI, GL_NONE,
366                                          ATI_FFP_CONST_BUMPMAT(stage), GL_BLUE, GL_NONE));
367         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);
368         GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_5_ATI, GL_GREEN_BIT_ATI, GL_NONE,
369                                         ATI_FFP_CONST_BUMPMAT(stage), GL_ALPHA, GL_NONE));
370         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",
371               stage + 1, stage, debug_argmod(argmodextra), stage + 1);
372         GL_EXTCALL(glColorFragmentOp3ATI(GL_DOT2_ADD_ATI, GL_REG_0_ATI + stage + 1, GL_GREEN_BIT_ATI, GL_NONE,
373                                          GL_REG_0_ATI + stage, GL_NONE, argmodextra,
374                                          GL_REG_5_ATI, GL_NONE, GL_2X_BIT_ATI | GL_BIAS_BIT_ATI,
375                                          GL_REG_0_ATI + stage + 1, GL_GREEN, GL_NONE));
376     }
377
378     /* Pass 3: Generate sampling instructions for regular textures */
379     for(stage = 0; stage < GL_LIMITS(textures); stage++) {
380         if(op[stage].cop == WINED3DTOP_DISABLE) {
381             break;
382         }
383
384         if(op[stage].projected == proj_none) {
385             swizzle = GL_SWIZZLE_STR_ATI;
386         } else if(op[stage].projected == proj_count3) {
387             swizzle = GL_SWIZZLE_STR_DR_ATI;
388         } else {
389             swizzle = GL_SWIZZLE_STQ_DQ_ATI;
390         }
391
392         if((op[stage].carg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
393            (op[stage].carg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
394            (op[stage].carg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
395            (op[stage].aarg0 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
396            (op[stage].aarg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
397            (op[stage].aarg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE ||
398             op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) {
399
400             if(stage > 0 &&
401                (op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
402                 op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
403                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_REG_%d_ATI, GL_SWIZZLE_STR_ATI)\n",
404                       stage, stage);
405                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
406                            GL_REG_0_ATI + stage,
407                            GL_SWIZZLE_STR_ATI));
408             } else {
409                 TRACE("glSampleMapATI(GL_REG_%d_ATI, GL_TEXTURE_%d_ARB, %s)\n",
410                     stage, stage, debug_swizzle(swizzle));
411                 GL_EXTCALL(glSampleMapATI(GL_REG_0_ATI + stage,
412                                         GL_TEXTURE0_ARB + stage,
413                                         swizzle));
414             }
415         }
416     }
417
418     /* Pass 4: Generate the arithmetic instructions */
419     for(stage = 0; stage < MAX_TEXTURES; stage++) {
420         if(op[stage].cop == WINED3DTOP_DISABLE) {
421             if(stage == 0) {
422                 /* Handle complete texture disabling gracefully */
423                 TRACE("glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE, GL_PRIMARY_COLOR, GL_NONE, GL_NONE)\n");
424                 GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
425                                                  GL_PRIMARY_COLOR, GL_NONE, GL_NONE));
426                 TRACE("glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_PRIMARY_COLOR, GL_NONE, GL_NONE)\n");
427                 GL_EXTCALL(glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE,
428                                                  GL_PRIMARY_COLOR, GL_NONE, GL_NONE));
429             }
430             break;
431         }
432
433         if(op[stage].dst == tempreg) {
434             /* If we're writing to D3DTA_TEMP, but never reading from it we don't have to write there in the first place.
435              * skip the entire stage, this saves some GPU time
436              */
437             if(tmparg == GL_NONE) continue;
438
439             dstreg = tmparg;
440         } else {
441             dstreg = GL_REG_0_ATI;
442         }
443
444         arg0 = register_for_arg(op[stage].carg0, gl_info, stage, &argmod0, &rep0, tmparg);
445         arg1 = register_for_arg(op[stage].carg1, gl_info, stage, &argmod1, &rep1, tmparg);
446         arg2 = register_for_arg(op[stage].carg2, gl_info, stage, &argmod2, &rep2, tmparg);
447         dstmod = GL_NONE;
448         argmodextra = GL_NONE;
449         extrarg = GL_NONE;
450
451         switch(op[stage].cop) {
452             case WINED3DTOP_SELECTARG2:
453                 arg1 = arg2;
454                 argmod1 = argmod2;
455                 rep1 = rep2;
456             case WINED3DTOP_SELECTARG1:
457                 TRACE("glColorFragmentOp1ATI(GL_MOV_ATI, %s, GL_NONE, GL_NONE, %s, %s, %s)\n",
458                       debug_register(dstreg), debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1));
459                 GL_EXTCALL(glColorFragmentOp1ATI(GL_MOV_ATI, dstreg, GL_NONE, GL_NONE,
460                                                  arg1, rep1, argmod1));
461                 break;
462
463             case WINED3DTOP_MODULATE4X:
464                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
465             case WINED3DTOP_MODULATE2X:
466                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
467                 dstmod |= GL_SATURATE_BIT_ATI;
468             case WINED3DTOP_MODULATE:
469                 TRACE("glColorFragmentOp2ATI(GL_MUL_ATI, %s, GL_NONE, %s, %s, %s, %s, %s, %s, %s)\n",
470                       debug_register(dstreg), debug_dstmod(dstmod),
471                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
472                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2));
473                 GL_EXTCALL(glColorFragmentOp2ATI(GL_MUL_ATI, dstreg, GL_NONE, dstmod,
474                                                  arg1, rep1, argmod1,
475                                                  arg2, rep2, argmod2));
476                 break;
477
478             case WINED3DTOP_ADDSIGNED2X:
479                 dstmod = GL_2X_BIT_ATI;
480             case WINED3DTOP_ADDSIGNED:
481                 argmodextra = GL_BIAS_BIT_ATI;
482             case WINED3DTOP_ADD:
483                 dstmod |= GL_SATURATE_BIT_ATI;
484                 TRACE("glColorFragmentOp2ATI(GL_ADD_ATI, %s, GL_NONE, %s, %s, %s, %s, %s, %s, %s)\n",
485                       debug_register(dstreg), debug_dstmod(dstmod),
486                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
487                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmodextra | argmod2));
488                 GL_EXTCALL(glColorFragmentOp2ATI(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, dstmod,
489                                                  arg1, rep1, argmod1,
490                                                  arg2, rep2, argmodextra | argmod2));
491                 break;
492
493             case WINED3DTOP_SUBTRACT:
494                 dstmod |= GL_SATURATE_BIT_ATI;
495                 TRACE("glColorFragmentOp2ATI(GL_SUB_ATI, %s, GL_NONE, %s, %s, %s, %s, %s, %s, %s)\n",
496                       debug_register(dstreg), debug_dstmod(dstmod),
497                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
498                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2));
499                 GL_EXTCALL(glColorFragmentOp2ATI(GL_SUB_ATI, dstreg, GL_NONE, dstmod,
500                                                  arg1, rep1, argmod1,
501                                                  arg2, rep2, argmod2));
502                 break;
503
504             case WINED3DTOP_ADDSMOOTH:
505                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
506                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n",
507                       debug_register(dstreg),
508                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
509                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmodextra),
510                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1));
511                 /* Dst = arg1 + * arg2(1 -arg 1)
512                  *     = arg2 * (1 - arg1) + arg1
513                  */
514                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
515                                                  arg2, rep2, argmod2,
516                                                  arg1, rep1, argmodextra,
517                                                  arg1, rep1, argmod1));
518                 break;
519
520             case WINED3DTOP_BLENDCURRENTALPHA:
521                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
522             case WINED3DTOP_BLENDFACTORALPHA:
523                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
524             case WINED3DTOP_BLENDTEXTUREALPHA:
525                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
526             case WINED3DTOP_BLENDDIFFUSEALPHA:
527                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
528                 TRACE("glColorFragmentOp3ATI(GL_LERP_ATI, %s, GL_NONE, GL_NONE, %s, GL_ALPHA, GL_NONE, %s, %s, %s, %s, %s, %s)\n",
529                       debug_register(dstreg),
530                       debug_register(extrarg),
531                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
532                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2));
533                 GL_EXTCALL(glColorFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
534                                                  extrarg, GL_ALPHA, GL_NONE,
535                                                  arg1, rep1, argmod1,
536                                                  arg2, rep2, argmod2));
537                 break;
538
539             case WINED3DTOP_BLENDTEXTUREALPHAPM:
540                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
541                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_NONE, %s, %s, %s, %s, GL_ALPHA, GL_COMP_BIT_ATI, %s, %s, %s)\n",
542                       debug_register(dstreg),
543                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
544                       debug_register(arg0),
545                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1));
546                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_NONE,
547                                                  arg2, rep2,  argmod2,
548                                                  arg0, GL_ALPHA, GL_COMP_BIT_ATI,
549                                                  arg1, rep1,  argmod1));
550                 break;
551
552             /* D3DTOP_PREMODULATE ???? */
553
554             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
555                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
556             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
557                 if(!argmodextra) argmodextra = argmod1;
558                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, GL_ALPHA, %s, %s, %s, %s)\n",
559                       debug_register(dstreg),
560                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
561                       debug_register(arg1), debug_argmod(argmodextra), debug_register(arg1), debug_rep(rep1), debug_argmod(arg1));
562                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
563                                                  arg2, rep2,  argmod2,
564                                                  arg1, GL_ALPHA, argmodextra,
565                                                  arg1, rep1,  argmod1));
566                 break;
567
568             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
569                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
570             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
571                 if(!argmodextra) argmodextra = argmod1;
572                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s, %s, GL_ALPHA, %s)\n",
573                       debug_register(dstreg),
574                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
575                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmodextra),
576                       debug_register(arg1), debug_argmod(argmod1));
577                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
578                                                  arg2, rep2,  argmod2,
579                                                  arg1, rep1,  argmodextra,
580                                                  arg1, GL_ALPHA, argmod1));
581                 break;
582
583             case WINED3DTOP_DOTPRODUCT3:
584                 TRACE("glColorFragmentOp2ATI(GL_DOT3_ATI, %s, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s)\n",
585                       debug_register(dstreg),
586                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1 | GL_BIAS_BIT_ATI),
587                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2 | GL_BIAS_BIT_ATI));
588                 GL_EXTCALL(glColorFragmentOp2ATI(GL_DOT3_ATI, dstreg, GL_NONE, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
589                                                  arg1, rep1, argmod1 | GL_BIAS_BIT_ATI,
590                                                  arg2, rep2, argmod2 | GL_BIAS_BIT_ATI));
591                 break;
592
593             case WINED3DTOP_MULTIPLYADD:
594                 TRACE("glColorFragmentOp3ATI(GL_MAD_ATI, %s, GL_NONE, GL_SATURATE_BIT_ATI, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n",
595                       debug_register(dstreg),
596                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
597                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
598                       debug_register(arg0), debug_rep(rep0), debug_argmod(argmod0));
599                 GL_EXTCALL(glColorFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE, GL_SATURATE_BIT_ATI,
600                                                  arg1, rep1, argmod1,
601                                                  arg2, rep2, argmod2,
602                                                  arg0, rep0, argmod0));
603                 break;
604
605             case WINED3DTOP_LERP:
606                 TRACE("glColorFragmentOp3ATI(GL_LERP_ATI, %s, GL_NONE, GL_NONE, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n",
607                       debug_register(dstreg),
608                       debug_register(arg1), debug_rep(rep1), debug_argmod(argmod1),
609                       debug_register(arg2), debug_rep(rep2), debug_argmod(argmod2),
610                       debug_register(arg0), debug_rep(rep0), debug_argmod(argmod0));
611                 GL_EXTCALL(glColorFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_NONE, GL_NONE,
612                                                  arg0, rep0, argmod0,
613                                                  arg1, rep1, argmod1,
614                                                  arg2, rep2, argmod2));
615                 break;
616
617             case WINED3DTOP_BUMPENVMAP:
618             case WINED3DTOP_BUMPENVMAPLUMINANCE:
619                 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
620                 break;
621
622             default: FIXME("Unhandled color operation %d on stage %d\n", op[stage].cop, stage);
623         }
624
625         arg0 = register_for_arg(op[stage].aarg0, gl_info, stage, &argmod0, NULL, tmparg);
626         arg1 = register_for_arg(op[stage].aarg1, gl_info, stage, &argmod1, NULL, tmparg);
627         arg2 = register_for_arg(op[stage].aarg2, gl_info, stage, &argmod2, NULL, tmparg);
628         dstmod = GL_NONE;
629         argmodextra = GL_NONE;
630         extrarg = GL_NONE;
631
632         switch(op[stage].aop) {
633             case WINED3DTOP_DISABLE:
634                 /* Get the primary color to the output if on stage 0, otherwise leave register 0 untouched */
635                 if(stage == 0) {
636                     TRACE("glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE, GL_PRIMARY_COLOR, GL_NONE, GL_NONE)\n");
637                     GL_EXTCALL(glAlphaFragmentOp1ATI(GL_MOV_ATI, GL_REG_0_ATI, GL_NONE,
638                                GL_PRIMARY_COLOR, GL_NONE, GL_NONE));
639                 }
640                 break;
641
642             case WINED3DTOP_SELECTARG2:
643                 arg1 = arg2;
644                 argmod1 = argmod2;
645             case WINED3DTOP_SELECTARG1:
646                 TRACE("glAlphaFragmentOp1ATI(GL_MOV_ATI, %s,          GL_NONE, %s, GL_NONE, %s)\n",
647                       debug_register(dstreg),
648                       debug_register(arg1), debug_argmod(argmod1));
649                 GL_EXTCALL(glAlphaFragmentOp1ATI(GL_MOV_ATI, dstreg, GL_NONE,
650                                                  arg1, GL_NONE, argmod1));
651                 break;
652
653             case WINED3DTOP_MODULATE4X:
654                 if(dstmod == GL_NONE) dstmod = GL_4X_BIT_ATI;
655             case WINED3DTOP_MODULATE2X:
656                 if(dstmod == GL_NONE) dstmod = GL_2X_BIT_ATI;
657                 dstmod |= GL_SATURATE_BIT_ATI;
658             case WINED3DTOP_MODULATE:
659                 TRACE("glAlphaFragmentOp2ATI(GL_MUL_ATI, %s,          %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
660                       debug_register(dstreg), debug_dstmod(dstmod),
661                       debug_register(arg1), debug_argmod(argmod1),
662                       debug_register(arg2), debug_argmod(argmod2));
663                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_MUL_ATI, dstreg, dstmod,
664                                                  arg1, GL_NONE, argmod1,
665                                                  arg2, GL_NONE, argmod2));
666                 break;
667
668             case WINED3DTOP_ADDSIGNED2X:
669                 dstmod = GL_2X_BIT_ATI;
670             case WINED3DTOP_ADDSIGNED:
671                 argmodextra = GL_BIAS_BIT_ATI;
672             case WINED3DTOP_ADD:
673                 dstmod |= GL_SATURATE_BIT_ATI;
674                 TRACE("glAlphaFragmentOp2ATI(GL_ADD_ATI, %s,          %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
675                       debug_register(dstreg), debug_dstmod(dstmod),
676                       debug_register(arg1), debug_argmod(argmod1),
677                       debug_register(arg2), debug_argmod(argmodextra | argmod2));
678                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_ADD_ATI, dstreg, dstmod,
679                                                  arg1, GL_NONE, argmod1,
680                                                  arg2, GL_NONE, argmodextra | argmod2));
681                 break;
682
683             case WINED3DTOP_SUBTRACT:
684                 dstmod |= GL_SATURATE_BIT_ATI;
685                 TRACE("glAlphaFragmentOp2ATI(GL_SUB_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
686                       debug_register(dstreg), debug_dstmod(dstmod),
687                       debug_register(arg1), debug_argmod(argmod1),
688                       debug_register(arg2), debug_argmod(argmod2));
689                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_SUB_ATI, dstreg, dstmod,
690                                                  arg1, GL_NONE, argmod1,
691                                                  arg2, GL_NONE, argmod2));
692                 break;
693
694             case WINED3DTOP_ADDSMOOTH:
695                 argmodextra = argmod1 & GL_COMP_BIT_ATI ? argmod1 & ~GL_COMP_BIT_ATI : argmod1 | GL_COMP_BIT_ATI;
696                 TRACE("glAlphaFragmentOp3ATI(GL_MAD_ATI, %s,          GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
697                       debug_register(dstreg),
698                       debug_register(arg2), debug_argmod(argmod2),
699                       debug_register(arg1), debug_argmod(argmodextra),
700                       debug_register(arg1), debug_argmod(argmod1));
701                 /* Dst = arg1 + * arg2(1 -arg 1)
702                  *     = arg2 * (1 - arg1) + arg1
703                  */
704                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_SATURATE_BIT_ATI,
705                                                  arg2, GL_NONE, argmod2,
706                                                  arg1, GL_NONE, argmodextra,
707                                                  arg1, GL_NONE, argmod1));
708                 break;
709
710             case WINED3DTOP_BLENDCURRENTALPHA:
711                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_CURRENT, gl_info, stage, NULL, NULL, -1);
712             case WINED3DTOP_BLENDFACTORALPHA:
713                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TFACTOR, gl_info, stage, NULL, NULL, -1);
714             case WINED3DTOP_BLENDTEXTUREALPHA:
715                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
716             case WINED3DTOP_BLENDDIFFUSEALPHA:
717                 if(extrarg == GL_NONE) extrarg = register_for_arg(WINED3DTA_DIFFUSE, gl_info, stage, NULL, NULL, -1);
718                 TRACE("glAlphaFragmentOp3ATI(GL_LERP_ATI, %s,          GL_NONE, %s, GL_ALPHA, GL_NONE, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
719                       debug_register(dstreg),
720                       debug_register(extrarg),
721                       debug_register(arg1), debug_argmod(argmod1),
722                       debug_register(arg2), debug_argmod(argmod2));
723                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_NONE,
724                                                  extrarg, GL_ALPHA, GL_NONE,
725                                                  arg1, GL_NONE, argmod1,
726                                                  arg2, GL_NONE, argmod2));
727                 break;
728
729             case WINED3DTOP_BLENDTEXTUREALPHAPM:
730                 arg0 = register_for_arg(WINED3DTA_TEXTURE, gl_info, stage, NULL, NULL, -1);
731                 TRACE("glAlphaFragmentOp3ATI(GL_MAD_ATI, %s,          GL_NONE, %s, GL_NONE, %s, %s, GL_ALPHA, GL_COMP_BIT_ATI, %s, GL_NONE, %s)\n",
732                       debug_register(dstreg),
733                       debug_register(arg2), debug_argmod(argmod2),
734                       debug_register(arg0),
735                       debug_register(arg1), debug_argmod(argmod1));
736                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_MAD_ATI, dstreg, GL_NONE,
737                                                  arg2, GL_NONE,  argmod2,
738                                                  arg0, GL_ALPHA, GL_COMP_BIT_ATI,
739                                                  arg1, GL_NONE,  argmod1));
740                 break;
741
742             /* D3DTOP_PREMODULATE ???? */
743
744             case WINED3DTOP_DOTPRODUCT3:
745                 TRACE("glAlphaFragmentOp2ATI(GL_DOT3_ATI, %s,          GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
746                       debug_register(dstreg),
747                       debug_register(arg1), debug_argmod(argmod1 | GL_BIAS_BIT_ATI),
748                       debug_register(arg2), debug_argmod(argmod2 | GL_BIAS_BIT_ATI));
749                 GL_EXTCALL(glAlphaFragmentOp2ATI(GL_DOT3_ATI, dstreg, GL_4X_BIT_ATI | GL_SATURATE_BIT_ATI,
750                                                  arg1, GL_NONE, argmod1 | GL_BIAS_BIT_ATI,
751                                                  arg2, GL_NONE, argmod2 | GL_BIAS_BIT_ATI));
752                 break;
753
754             case WINED3DTOP_MULTIPLYADD:
755                 TRACE("glAlphaFragmentOp3ATI(GL_MAD_ATI, %s,          GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
756                       debug_register(dstreg),
757                       debug_register(arg1), debug_argmod(argmod1),
758                       debug_register(arg2), debug_argmod(argmod2),
759                       debug_register(arg0), debug_argmod(argmod0));
760                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_MAD_ATI, dstreg,          GL_SATURATE_BIT_ATI,
761                            arg1, GL_NONE, argmod1,
762                            arg2, GL_NONE, argmod2,
763                            arg0, GL_NONE, argmod0));
764                 break;
765
766             case WINED3DTOP_LERP:
767                 TRACE("glAlphaFragmentOp3ATI(GL_LERP_ATI, %s,          GL_SATURATE_BIT_ATI, %s, GL_NONE, %s, %s, GL_NONE, %s, %s, GL_NONE, %s)\n",
768                       debug_register(dstreg),
769                       debug_register(arg1), debug_argmod(argmod1),
770                       debug_register(arg2), debug_argmod(argmod2),
771                       debug_register(arg0), debug_argmod(argmod0));
772                 GL_EXTCALL(glAlphaFragmentOp3ATI(GL_LERP_ATI, dstreg, GL_SATURATE_BIT_ATI,
773                                                  arg1, GL_NONE, argmod1,
774                                                  arg2, GL_NONE, argmod2,
775                                                  arg0, GL_NONE, argmod0));
776                 break;
777
778             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
779             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
780             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
781             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
782             case WINED3DTOP_BUMPENVMAP:
783             case WINED3DTOP_BUMPENVMAPLUMINANCE:
784                 ERR("Application uses an invalid alpha operation\n");
785                 break;
786
787             default: FIXME("Unhandled alpha operation %d on stage %d\n", op[stage].aop, stage);
788         }
789     }
790
791     TRACE("glEndFragmentShaderATI()\n");
792     GL_EXTCALL(glEndFragmentShaderATI());
793     checkGLcall("GL_EXTCALL(glEndFragmentShaderATI())");
794     return ret;
795 }
796 #undef GLINFO_LOCATION
797
798 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
799 static void set_tex_op_atifs(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
800     IWineD3DDeviceImpl          *This = stateblock->wineD3DDevice;
801     struct atifs_ffp_desc       *desc;
802     struct ffp_settings settings;
803     struct atifs_private_data   *priv = (struct atifs_private_data *) This->fragment_priv;
804     DWORD mapped_stage;
805     unsigned int i;
806
807     gen_ffp_op(stateblock, &settings, TRUE);
808     desc = (struct atifs_ffp_desc *) find_ffp_shader(priv->fragment_shaders, &settings);
809     if(!desc) {
810         desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
811         if(!desc) {
812             ERR("Out of memory\n");
813             return;
814         }
815         desc->num_textures_used = 0;
816         for(i = 0; i < GL_LIMITS(texture_stages); i++) {
817             if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
818             desc->num_textures_used = i;
819         }
820
821         memcpy(&desc->parent.settings, &settings, sizeof(settings));
822         desc->shader = gen_ati_shader(settings.op, &GLINFO_LOCATION);
823         add_ffp_shader(priv->fragment_shaders, &desc->parent);
824         TRACE("Allocated fixed function replacement shader descriptor %p\n", desc);
825     }
826
827     /* GL_ATI_fragment_shader depends on the GL_TEXTURE_xD enable settings. Update the texture stages
828      * used by this shader
829      */
830     for(i = 0; i < desc->num_textures_used; i++) {
831         mapped_stage = This->texUnitMap[i];
832         if(mapped_stage != -1) {
833             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
834             checkGLcall("glActiveTextureARB");
835             texture_activate_dimensions(i, stateblock, context);
836         }
837     }
838
839     GL_EXTCALL(glBindFragmentShaderATI(desc->shader));
840 }
841
842 static void state_texfactor_atifs(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
843     float col[4];
844     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
845
846     GL_EXTCALL(glSetFragmentShaderConstantATI(ATI_FFP_CONST_TFACTOR, col));
847     checkGLcall("glSetFragmentShaderConstantATI(ATI_FFP_CONST_TFACTOR, col)");
848 }
849
850 static void set_bumpmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
851     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
852     float mat[2][2];
853
854     mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
855     mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
856     mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
857     mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
858     /* GL_ATI_fragment_shader allows only constants from 0.0 to 1.0, but the bumpmat
859      * constants can be in any range. While they should stay between [-1.0 and 1.0] because
860      * Shader Model 1.x pixel shaders are clamped to that range negative values are used occasionally,
861      * for example by our d3d9 test. So to get negative values scale -1;1 to 0;1 and undo that in the
862      * shader(it is free). This might potentially reduce precision. However, if the hardware does
863      * support proper floats it shouldn't, and if it doesn't we can't get anything better anyway
864      */
865     mat[0][0] = (mat[0][0] + 1.0) * 0.5;
866     mat[1][0] = (mat[1][0] + 1.0) * 0.5;
867     mat[0][1] = (mat[0][1] + 1.0) * 0.5;
868     mat[1][1] = (mat[1][1] + 1.0) * 0.5;
869     GL_EXTCALL(glSetFragmentShaderConstantATI(ATI_FFP_CONST_BUMPMAT(stage), (float *) mat));
870     checkGLcall("glSetFragmentShaderConstantATI(ATI_FFP_CONST_BUMPMAT(stage), mat)");
871 }
872
873 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
874     if(!isStateDirty(context, STATE_PIXELSHADER)) {
875         set_tex_op_atifs(state, stateblock, context);
876     }
877 }
878
879 #undef GLINFO_LOCATION
880
881 static const struct StateEntryTemplate atifs_fragmentstate_template[] = {
882     {STATE_RENDER(WINED3DRS_TEXTUREFACTOR),               { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              state_texfactor_atifs   }, 0                               },
883     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
884     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
885     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
886     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
887     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
888     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
889     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
890     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
891     {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
892     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
893     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
894     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
895     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
896     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
897     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
898     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
899     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
900     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
901     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
902     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
903     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
904     {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
905     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
906     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
907     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
908     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
909     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
910     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
911     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
912     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
913     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
914     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
915     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
916     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
917     {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
918     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
919     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
920     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
921     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
922     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
923     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
924     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
925     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
926     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
927     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
928     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
929     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
930     {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
931     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
932     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
933     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
934     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
935     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
936     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
937     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
938     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
939     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
940     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
941     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
942     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
943     {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
944     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
945     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
946     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
947     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
948     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
949     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
950     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
951     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
952     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
953     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
954     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
955     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
956     {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
957     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
958     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
959     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
960     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
961     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
962     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
963     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
964     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
965     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
966     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
967     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
968     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
969     {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
970     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
971     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
972     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
973     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
974     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
975     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
976     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
977     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
978     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),           { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
979     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
980     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
981     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
982     {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),         { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          set_tex_op_atifs        }, 0                               },
983     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
984     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
985     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
986     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat             }, 0                               },
987     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim          }, 0                               },
988     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim          }, 0                               },
989     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim          }, 0                               },
990     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim          }, 0                               },
991     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim          }, 0                               },
992     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim          }, 0                               },
993     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim          }, 0                               },
994     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim          }, 0                               },
995     {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
996     {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
997     {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
998     {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
999     {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1000     {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1001     {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1002     {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
1003     {0 /* Terminate */,                                   { 0,                                                  0                       }, 0                               },
1004 };
1005
1006 static void atifs_enable(IWineD3DDevice *iface, BOOL enable) {
1007     if(enable) {
1008         glEnable(GL_FRAGMENT_SHADER_ATI);
1009         checkGLcall("glEnable(GL_FRAGMENT_SHADER_ATI)");
1010     } else {
1011         glDisable(GL_FRAGMENT_SHADER_ATI);
1012         checkGLcall("glDisable(GL_FRAGMENT_SHADER_ATI)");
1013     }
1014 }
1015
1016 static void atifs_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct fragment_caps *caps) {
1017     caps->TextureOpCaps =  WINED3DTEXOPCAPS_DISABLE                     |
1018                            WINED3DTEXOPCAPS_SELECTARG1                  |
1019                            WINED3DTEXOPCAPS_SELECTARG2                  |
1020                            WINED3DTEXOPCAPS_MODULATE4X                  |
1021                            WINED3DTEXOPCAPS_MODULATE2X                  |
1022                            WINED3DTEXOPCAPS_MODULATE                    |
1023                            WINED3DTEXOPCAPS_ADDSIGNED2X                 |
1024                            WINED3DTEXOPCAPS_ADDSIGNED                   |
1025                            WINED3DTEXOPCAPS_ADD                         |
1026                            WINED3DTEXOPCAPS_SUBTRACT                    |
1027                            WINED3DTEXOPCAPS_ADDSMOOTH                   |
1028                            WINED3DTEXOPCAPS_BLENDCURRENTALPHA           |
1029                            WINED3DTEXOPCAPS_BLENDFACTORALPHA            |
1030                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHA           |
1031                            WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA           |
1032                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM         |
1033                            WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR      |
1034                            WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA      |
1035                            WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA   |
1036                            WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR   |
1037                            WINED3DTEXOPCAPS_DOTPRODUCT3                 |
1038                            WINED3DTEXOPCAPS_MULTIPLYADD                 |
1039                            WINED3DTEXOPCAPS_LERP                        |
1040                            WINED3DTEXOPCAPS_BUMPENVMAP;
1041
1042     /* TODO: Implement WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
1043     and WINED3DTEXOPCAPS_PREMODULATE */
1044
1045     /* GL_ATI_fragment_shader always supports 6 textures, which was the limit on r200 cards
1046      * which this extension is exclusively focused on(later cards have GL_ARB_fragment_program).
1047      * If the current card has more than 8 fixed function textures in OpenGL's regular fixed
1048      * function pipeline then the ATI_fragment_shader backend imposes a stricter limit. This
1049      * shouldn't be too hard since Nvidia cards have a limit of 4 textures with the default ffp
1050      * pipeline, and almost all games are happy with that. We can however support up to 8
1051      * texture stages because we have a 2nd pass limit of 8 instructions, and per stage we use
1052      * only 1 instruction.
1053      *
1054      * The proper fix for this is not to use GL_ATI_fragment_shader on cards newer than the
1055      * r200 series and use an ARB or GLSL shader instead
1056      */
1057     caps->MaxTextureBlendStages   = 8;
1058     caps->MaxSimultaneousTextures = 6;
1059
1060     caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
1061 }
1062
1063 static HRESULT atifs_alloc(IWineD3DDevice *iface) {
1064     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1065     struct atifs_private_data *priv;
1066
1067     This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct atifs_private_data));
1068     if(!This->fragment_priv) {
1069         ERR("Out of memory\n");
1070         return E_OUTOFMEMORY;
1071     }
1072     priv = (struct atifs_private_data *) This->fragment_priv;
1073     priv->fragment_shaders = hash_table_create(ffp_program_key_hash, ffp_program_key_compare);
1074     return WINED3D_OK;
1075 }
1076
1077 #define GLINFO_LOCATION This->adapter->gl_info
1078 static void atifs_free_ffpshader(void *value, void *device) {
1079     IWineD3DDeviceImpl *This = device;
1080     struct atifs_ffp_desc *entry_ati = value;
1081
1082     ENTER_GL();
1083     GL_EXTCALL(glDeleteFragmentShaderATI(entry_ati->shader));
1084     checkGLcall("glDeleteFragmentShaderATI(entry->shader)");
1085     HeapFree(GetProcessHeap(), 0, entry_ati);
1086     LEAVE_GL();
1087 }
1088
1089 static void atifs_free(IWineD3DDevice *iface) {
1090     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1091     struct atifs_private_data *priv = (struct atifs_private_data *) This->fragment_priv;
1092
1093     hash_table_destroy(priv->fragment_shaders, atifs_free_ffpshader, This);
1094
1095     HeapFree(GetProcessHeap(), 0, priv);
1096     This->fragment_priv = NULL;
1097 }
1098 #undef GLINFO_LOCATION
1099
1100 static BOOL atifs_conv_supported(WINED3DFORMAT fmt) {
1101     TRACE("Checking shader format support for format %s:", debug_d3dformat(fmt));
1102     switch(fmt) {
1103         case WINED3DFMT_V8U8:
1104         case WINED3DFMT_V16U16:
1105             TRACE("[OK]\n");
1106             return TRUE;
1107         default:
1108             TRACE("[FAILED\n");
1109             return FALSE;
1110     }
1111 }
1112
1113 const struct fragment_pipeline atifs_fragment_pipeline = {
1114     atifs_enable,
1115     atifs_get_caps,
1116     atifs_alloc,
1117     atifs_free,
1118     atifs_conv_supported,
1119     atifs_fragmentstate_template,
1120     TRUE /* We can disable projected textures */
1121 };