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