mcicda: Compare GetLastError() to kernel error codes, not ntdll ones.
[wine] / dlls / wined3d / utils.c
1 /*
2  * Utility functions for the WineD3D Library
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  * Copyright 2006 Henri Verbeet
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29
30 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
31
32 /*****************************************************************************
33  * Pixel format array
34  */
35 static const PixelFormatDesc formats[] = {
36   /*{WINED3DFORMAT          ,alphamask  ,redmask    ,greenmask  ,bluemask   ,bpp    ,isFourcc   ,internal   ,format ,type   }*/
37     {WINED3DFMT_UNKNOWN     ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,FALSE      ,0                      ,0                  ,0                              },
38     /* FourCC formats, kept here to have WINED3DFMT_R8G8B8(=20) at position 20 */
39     {WINED3DFMT_UYVY        ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,TRUE       ,0                      ,0                  ,0                              },
40     {WINED3DFMT_YUY2        ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,TRUE       ,0                      ,0                  ,0                              },
41     {WINED3DFMT_DXT1        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,TRUE       ,GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,GL_RGBA   ,GL_UNSIGNED_BYTE               },
42     {WINED3DFMT_DXT2        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,TRUE       ,GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,GL_RGBA   ,GL_UNSIGNED_BYTE               },
43     {WINED3DFMT_DXT3        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,TRUE       ,GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,GL_RGBA   ,GL_UNSIGNED_BYTE               },
44     {WINED3DFMT_DXT4        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,TRUE       ,GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,GL_RGBA   ,GL_UNSIGNED_BYTE               },
45     {WINED3DFMT_DXT5        ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,TRUE       ,GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,GL_RGBA   ,GL_UNSIGNED_BYTE               },
46     {WINED3DFMT_MULTI2_ARGB ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,TRUE       ,0                      ,0                  ,0                              },
47     {WINED3DFMT_G8R8_G8B8   ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,TRUE       ,0                      ,0                  ,0                              },
48     {WINED3DFMT_R8G8_B8G8   ,0x0        ,0x0        ,0x0        ,0x0        ,1/*?*/ ,TRUE       ,0                      ,0                  ,0                              },
49     /* IEEE formats */
50     {WINED3DFMT_R32F        ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_RGB32F_ARB         ,GL_RED             ,GL_FLOAT                       },
51     {WINED3DFMT_G32R32F     ,0x0        ,0x0        ,0x0        ,0x0        ,8      ,FALSE      ,0                      ,0                  ,0                              },
52     {WINED3DFMT_A32B32G32R32F,0x0       ,0x0        ,0x0        ,0x0        ,16     ,FALSE      ,GL_RGBA32F_ARB         ,GL_RGBA            ,GL_FLOAT                       },
53     /* Hmm? */
54     {WINED3DFMT_CxV8U8      ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,0                      ,0                  ,0                              },
55     /* Float */
56     {WINED3DFMT_R16F        ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_RGB16F_ARB         ,GL_RED             ,GL_HALF_FLOAT_ARB              },
57     {WINED3DFMT_G16R16F     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,0                      ,0                  ,0                              },
58     {WINED3DFMT_A16B16G16R16F,0x0       ,0x0        ,0x0        ,0x0        ,8      ,FALSE      ,GL_RGBA16F_ARB         ,GL_RGBA            ,GL_HALF_FLOAT_ARB              },
59     /* Palettized formats */
60     {WINED3DFMT_A8P8        ,0x0000ff00 ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,0                      ,0                  ,0                              },
61     {WINED3DFMT_P8          ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,FALSE      ,GL_COLOR_INDEX8_EXT    ,GL_COLOR_INDEX     ,GL_UNSIGNED_BYTE               },
62     /* Standard ARGB formats. Keep WINED3DFMT_R8G8B8(=20) at position 20 */
63     {WINED3DFMT_R8G8B8      ,0x0        ,0x00ff0000 ,0x0000ff00 ,0x000000ff ,3      ,FALSE      ,GL_RGB8                ,GL_BGR             ,GL_UNSIGNED_BYTE               },
64     {WINED3DFMT_A8R8G8B8    ,0xff000000 ,0x00ff0000 ,0x0000ff00 ,0x000000ff ,4      ,FALSE      ,GL_RGBA8               ,GL_BGRA            ,GL_UNSIGNED_INT_8_8_8_8_REV    },
65     {WINED3DFMT_X8R8G8B8    ,0x0        ,0x00ff0000 ,0x0000ff00 ,0x000000ff ,4      ,FALSE      ,GL_RGB8                ,GL_BGRA            ,GL_UNSIGNED_INT_8_8_8_8_REV    },
66     {WINED3DFMT_R5G6B5      ,0x0        ,0x0000F800 ,0x000007e0 ,0x0000001f ,2      ,FALSE      ,GL_RGB5                ,GL_RGB             ,GL_UNSIGNED_SHORT_5_6_5        },
67     {WINED3DFMT_X1R5G5B5    ,0x0        ,0x00007c00 ,0x000003e0 ,0x0000001f ,2      ,FALSE      ,GL_RGB5_A1             ,GL_BGRA            ,GL_UNSIGNED_SHORT_1_5_5_5_REV  },
68     {WINED3DFMT_A1R5G5B5    ,0x00008000 ,0x00007c00 ,0x000003e0 ,0x0000001f ,2      ,FALSE      ,GL_RGB5_A1             ,GL_BGRA            ,GL_UNSIGNED_SHORT_1_5_5_5_REV  },
69     {WINED3DFMT_A4R4G4B4    ,0x0000f000 ,0x00000f00 ,0x000000f0 ,0x0000000f ,2      ,FALSE      ,GL_RGBA4               ,GL_BGRA            ,GL_UNSIGNED_SHORT_4_4_4_4_REV  },
70     {WINED3DFMT_R3G3B2      ,0x0        ,0x000000e0 ,0x0000001c ,0x00000003 ,1      ,FALSE      ,GL_R3_G3_B2            ,GL_RGB             ,GL_UNSIGNED_BYTE_2_3_3_REV     },
71     {WINED3DFMT_A8          ,0x000000ff ,0x0        ,0x0        ,0x0        ,1      ,FALSE      ,GL_ALPHA8              ,GL_ALPHA           ,GL_ALPHA                       },
72     {WINED3DFMT_A8R3G3B2    ,0x0000ff00 ,0x000000e0 ,0x0000001c ,0x00000003 ,2      ,FALSE      ,0                      ,0                  ,0                              },
73     {WINED3DFMT_X4R4G4B4    ,0x0        ,0x00000f00 ,0x000000f0 ,0x0000000f ,2      ,FALSE      ,GL_RGB4                ,GL_BGRA            ,GL_UNSIGNED_SHORT_4_4_4_4_REV  },
74     {WINED3DFMT_A2B10G10R10 ,0xb0000000 ,0x000003ff ,0x000ffc00 ,0x3ff00000 ,4      ,FALSE      ,GL_RGB                 ,GL_RGBA            ,GL_UNSIGNED_INT_2_10_10_10_REV },
75     {WINED3DFMT_A8B8G8R8    ,0xff000000 ,0x000000ff ,0x0000ff00 ,0x00ff0000 ,4      ,FALSE      ,GL_RGBA8               ,GL_RGBA            ,GL_UNSIGNED_INT_8_8_8_8_REV    },
76     {WINED3DFMT_X8B8G8R8    ,0x0        ,0x000000ff ,0x0000ff00 ,0x00ff0000 ,4      ,FALSE      ,GL_RGB8                ,GL_RGBA            ,GL_UNSIGNED_INT_8_8_8_8_REV    },
77     {WINED3DFMT_G16R16      ,0x0        ,0x0000ffff ,0xffff0000 ,0x0        ,4      ,FALSE      ,0                      ,0                  ,0                              },
78     {WINED3DFMT_A2R10G10B10 ,0xb0000000 ,0x3ff00000 ,0x000ffc00 ,0x000003ff ,4      ,FALSE      ,GL_RGBA                ,GL_BGRA            ,GL_UNSIGNED_INT_2_10_10_10_REV },
79     {WINED3DFMT_A16B16G16R16,0x0        ,0x0000ffff ,0xffff0000 ,0x0        ,8      ,FALSE      ,GL_RGBA16_EXT          ,GL_RGBA            ,GL_UNSIGNED_SHORT              },
80     /* Luminance */
81     {WINED3DFMT_L8          ,0x0        ,0x0        ,0x0        ,0x0        ,1      ,FALSE      ,GL_LUMINANCE8          ,GL_LUMINANCE       ,GL_UNSIGNED_BYTE               },
82     {WINED3DFMT_A8L8        ,0x0000ff00 ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_LUMINANCE8_ALPHA8   ,GL_LUMINANCE_ALPHA ,GL_UNSIGNED_BYTE               },
83     {WINED3DFMT_A4L4        ,0x000000f0 ,0x0        ,0x0        ,0x0        ,1      ,FALSE      ,GL_LUMINANCE4_ALPHA4   ,GL_LUMINANCE_ALPHA ,GL_UNSIGNED_BYTE               },
84     /* Bump mapping stuff */
85     {WINED3DFMT_V8U8        ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_COLOR_INDEX8_EXT    ,GL_COLOR_INDEX     ,GL_UNSIGNED_BYTE               },
86     {WINED3DFMT_L6V5U5      ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_COLOR_INDEX8_EXT    ,GL_COLOR_INDEX     ,GL_UNSIGNED_SHORT_5_5_5_1      },
87     {WINED3DFMT_X8L8V8U8    ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_RGBA8               ,GL_BGRA            ,GL_UNSIGNED_BYTE               },
88     {WINED3DFMT_Q8W8V8U8    ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_RGBA8               ,GL_RGBA            ,GL_UNSIGNED_INT_8_8_8_8_REV/*?*/},
89     {WINED3DFMT_V16U16      ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_SHORT              },
90     {WINED3DFMT_W11V11U10   ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,0                      ,0                  ,0                              },
91     {WINED3DFMT_A2W10V10U10 ,0xb0000000 ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,0                      ,0                  ,0                              },
92     /* Depth stencil formats */
93     {WINED3DFMT_D16_LOCKABLE,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_SHORT              },
94     {WINED3DFMT_D32         ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT32_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_INT                },
95     {WINED3DFMT_D15S1       ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_SHORT              },
96     {WINED3DFMT_D24S8       ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_INT                },
97     {WINED3DFMT_D24X8       ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_INT                },
98     {WINED3DFMT_D24X4S4     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_INT                },
99     {WINED3DFMT_D16         ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_UNSIGNED_SHORT              },
100     {WINED3DFMT_L16         ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_LUMINANCE16_EXT     ,GL_LUMINANCE       ,GL_UNSIGNED_SHORT              },
101     {WINED3DFMT_D32F_LOCKABLE,0x0       ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT32_ARB,GL_DEPTH_COMPONENT,GL_FLOAT                       },
102     {WINED3DFMT_D24FS8      ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_DEPTH_COMPONENT24_ARB,GL_DEPTH_COMPONENT,GL_FLOAT                       },
103     /* Is this a vertex buffer? */
104     {WINED3DFMT_VERTEXDATA  ,0x0        ,0x0        ,0x0        ,0x0        ,0      ,FALSE      ,0                      ,0                  ,0                              },
105     {WINED3DFMT_INDEX16     ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,0                      ,0                  ,0                              },
106     {WINED3DFMT_INDEX32     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,0                      ,0                  ,0                              },
107     {WINED3DFMT_Q16W16V16U16,0x0        ,0x0        ,0x0        ,0x0        ,8      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_SHORT              }
108 };
109
110 const PixelFormatDesc *getFormatDescEntry(WINED3DFORMAT fmt)
111 {
112     /* First check if the format is at the position of its value.
113      * This will catch the argb formats before the loop is entered
114      */
115     if(fmt < (sizeof(formats) / sizeof(formats[0])) && formats[fmt].format == fmt) {
116         return &formats[fmt];
117     } else {
118         unsigned int i;
119         for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
120             if(formats[i].format == fmt) {
121                 return &formats[i];
122             }
123         }
124     }
125     FIXME("Can't find format %s(%d) in the format lookup table\n", debug_d3dformat(fmt), fmt);
126     if(fmt == WINED3DFMT_UNKNOWN) {
127         ERR("Format table corrupt - Can't find WINED3DFMT_UNKNOWN\n");
128         return NULL;
129     }
130     /* Get the caller a valid pointer */
131     return getFormatDescEntry(WINED3DFMT_UNKNOWN);
132 }
133
134 /*****************************************************************************
135  * Trace formatting of useful values
136  */
137 const char* debug_d3dformat(WINED3DFORMAT fmt) {
138   switch (fmt) {
139 #define FMT_TO_STR(fmt) case fmt: return #fmt
140     FMT_TO_STR(WINED3DFMT_UNKNOWN);
141     FMT_TO_STR(WINED3DFMT_R8G8B8);
142     FMT_TO_STR(WINED3DFMT_A8R8G8B8);
143     FMT_TO_STR(WINED3DFMT_X8R8G8B8);
144     FMT_TO_STR(WINED3DFMT_R5G6B5);
145     FMT_TO_STR(WINED3DFMT_X1R5G5B5);
146     FMT_TO_STR(WINED3DFMT_A1R5G5B5);
147     FMT_TO_STR(WINED3DFMT_A4R4G4B4);
148     FMT_TO_STR(WINED3DFMT_R3G3B2);
149     FMT_TO_STR(WINED3DFMT_A8);
150     FMT_TO_STR(WINED3DFMT_A8R3G3B2);
151     FMT_TO_STR(WINED3DFMT_X4R4G4B4);
152     FMT_TO_STR(WINED3DFMT_A2B10G10R10);
153     FMT_TO_STR(WINED3DFMT_A8B8G8R8);
154     FMT_TO_STR(WINED3DFMT_X8B8G8R8);
155     FMT_TO_STR(WINED3DFMT_G16R16);
156     FMT_TO_STR(WINED3DFMT_A2R10G10B10);
157     FMT_TO_STR(WINED3DFMT_A16B16G16R16);
158     FMT_TO_STR(WINED3DFMT_A8P8);
159     FMT_TO_STR(WINED3DFMT_P8);
160     FMT_TO_STR(WINED3DFMT_L8);
161     FMT_TO_STR(WINED3DFMT_A8L8);
162     FMT_TO_STR(WINED3DFMT_A4L4);
163     FMT_TO_STR(WINED3DFMT_V8U8);
164     FMT_TO_STR(WINED3DFMT_L6V5U5);
165     FMT_TO_STR(WINED3DFMT_X8L8V8U8);
166     FMT_TO_STR(WINED3DFMT_Q8W8V8U8);
167     FMT_TO_STR(WINED3DFMT_V16U16);
168     FMT_TO_STR(WINED3DFMT_W11V11U10);
169     FMT_TO_STR(WINED3DFMT_A2W10V10U10);
170     FMT_TO_STR(WINED3DFMT_UYVY);
171     FMT_TO_STR(WINED3DFMT_YUY2);
172     FMT_TO_STR(WINED3DFMT_DXT1);
173     FMT_TO_STR(WINED3DFMT_DXT2);
174     FMT_TO_STR(WINED3DFMT_DXT3);
175     FMT_TO_STR(WINED3DFMT_DXT4);
176     FMT_TO_STR(WINED3DFMT_DXT5);
177     FMT_TO_STR(WINED3DFMT_MULTI2_ARGB);
178     FMT_TO_STR(WINED3DFMT_G8R8_G8B8);
179     FMT_TO_STR(WINED3DFMT_R8G8_B8G8);
180     FMT_TO_STR(WINED3DFMT_D16_LOCKABLE);
181     FMT_TO_STR(WINED3DFMT_D32);
182     FMT_TO_STR(WINED3DFMT_D15S1);
183     FMT_TO_STR(WINED3DFMT_D24S8);
184     FMT_TO_STR(WINED3DFMT_D24X8);
185     FMT_TO_STR(WINED3DFMT_D24X4S4);
186     FMT_TO_STR(WINED3DFMT_D16);
187     FMT_TO_STR(WINED3DFMT_L16);
188     FMT_TO_STR(WINED3DFMT_D32F_LOCKABLE);
189     FMT_TO_STR(WINED3DFMT_D24FS8);
190     FMT_TO_STR(WINED3DFMT_VERTEXDATA);
191     FMT_TO_STR(WINED3DFMT_INDEX16);
192     FMT_TO_STR(WINED3DFMT_INDEX32);
193     FMT_TO_STR(WINED3DFMT_Q16W16V16U16);
194     FMT_TO_STR(WINED3DFMT_R16F);
195     FMT_TO_STR(WINED3DFMT_G16R16F);
196     FMT_TO_STR(WINED3DFMT_A16B16G16R16F);
197     FMT_TO_STR(WINED3DFMT_R32F);
198     FMT_TO_STR(WINED3DFMT_G32R32F);
199     FMT_TO_STR(WINED3DFMT_A32B32G32R32F);
200     FMT_TO_STR(WINED3DFMT_CxV8U8);
201 #undef FMT_TO_STR
202   default:
203     {
204       char fourcc[5];
205       fourcc[0] = (char)(fmt);
206       fourcc[1] = (char)(fmt >> 8);
207       fourcc[2] = (char)(fmt >> 16);
208       fourcc[3] = (char)(fmt >> 24);
209       fourcc[4] = 0;
210       if( isprint(fourcc[0]) && isprint(fourcc[1]) && isprint(fourcc[2]) && isprint(fourcc[3]) )
211         FIXME("Unrecognized %u (as fourcc: %s) WINED3DFORMAT!\n", fmt, fourcc);
212       else
213         FIXME("Unrecognized %u WINED3DFORMAT!\n", fmt);
214     }
215     return "unrecognized";
216   }
217 }
218
219 const char* debug_d3ddevicetype(WINED3DDEVTYPE devtype) {
220   switch (devtype) {
221 #define DEVTYPE_TO_STR(dev) case dev: return #dev
222     DEVTYPE_TO_STR(WINED3DDEVTYPE_HAL);
223     DEVTYPE_TO_STR(WINED3DDEVTYPE_REF);
224     DEVTYPE_TO_STR(WINED3DDEVTYPE_SW);
225 #undef DEVTYPE_TO_STR
226   default:
227     FIXME("Unrecognized %u WINED3DDEVTYPE!\n", devtype);
228     return "unrecognized";
229   }
230 }
231
232 const char* debug_d3dusage(DWORD usage) {
233   switch (usage & WINED3DUSAGE_MASK) {
234 #define WINED3DUSAGE_TO_STR(u) case u: return #u
235     WINED3DUSAGE_TO_STR(WINED3DUSAGE_RENDERTARGET);
236     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DEPTHSTENCIL);
237     WINED3DUSAGE_TO_STR(WINED3DUSAGE_WRITEONLY);
238     WINED3DUSAGE_TO_STR(WINED3DUSAGE_SOFTWAREPROCESSING);
239     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DONOTCLIP);
240     WINED3DUSAGE_TO_STR(WINED3DUSAGE_POINTS);
241     WINED3DUSAGE_TO_STR(WINED3DUSAGE_RTPATCHES);
242     WINED3DUSAGE_TO_STR(WINED3DUSAGE_NPATCHES);
243     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DYNAMIC);
244     WINED3DUSAGE_TO_STR(WINED3DUSAGE_AUTOGENMIPMAP);
245     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DMAP);
246 #undef WINED3DUSAGE_TO_STR
247   case 0: return "none";
248   default:
249     FIXME("Unrecognized %u Usage!\n", usage);
250     return "unrecognized";
251   }
252 }
253
254 const char* debug_d3dusagequery(DWORD usagequery) {
255   switch (usagequery & WINED3DUSAGE_QUERY_MASK) {
256 #define WINED3DUSAGEQUERY_TO_STR(u) case u: return #u
257     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_FILTER);
258     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_LEGACYBUMPMAP);
259     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING);
260     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_SRGBREAD);
261     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_SRGBWRITE);
262     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_VERTEXTEXTURE);
263     WINED3DUSAGEQUERY_TO_STR(WINED3DUSAGE_QUERY_WRAPANDMIP);
264 #undef WINED3DUSAGEQUERY_TO_STR
265   case 0: return "none";
266   default:
267     FIXME("Unrecognized %u Usage Query!\n", usagequery);
268     return "unrecognized";
269   }
270 }
271
272 const char* debug_d3ddeclmethod(WINED3DDECLMETHOD method) {
273     switch (method) {
274 #define WINED3DDECLMETHOD_TO_STR(u) case u: return #u
275         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_DEFAULT);
276         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_PARTIALU);
277         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_PARTIALV);
278         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_CROSSUV);
279         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_UV);
280         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_LOOKUP);
281         WINED3DDECLMETHOD_TO_STR(WINED3DDECLMETHOD_LOOKUPPRESAMPLED);
282 #undef WINED3DDECLMETHOD_TO_STR
283         default:
284             FIXME("Unrecognized %u declaration method!\n", method);
285             return "unrecognized";
286     }
287 }
288
289 const char* debug_d3ddecltype(WINED3DDECLTYPE type) {
290     switch (type) {
291 #define WINED3DDECLTYPE_TO_STR(u) case u: return #u
292         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT1);
293         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT2);
294         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT3);
295         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT4);
296         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_D3DCOLOR);
297         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UBYTE4);
298         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT2);
299         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT4);
300         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UBYTE4N);
301         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT2N);
302         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_SHORT4N);
303         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_USHORT2N);
304         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_USHORT4N);
305         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UDEC3);
306         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_DEC3N);
307         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT16_2);
308         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_FLOAT16_4);
309         WINED3DDECLTYPE_TO_STR(WINED3DDECLTYPE_UNUSED);
310 #undef WINED3DDECLTYPE_TO_STR
311         default:
312             FIXME("Unrecognized %u declaration type!\n", type);
313             return "unrecognized";
314     }
315 }
316
317 const char* debug_d3ddeclusage(BYTE usage) {
318     switch (usage) {
319 #define WINED3DDECLUSAGE_TO_STR(u) case u: return #u
320         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_POSITION);
321         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_BLENDWEIGHT);
322         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_BLENDINDICES);
323         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_NORMAL);
324         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_PSIZE);
325         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_TEXCOORD);
326         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_TANGENT);
327         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_BINORMAL);
328         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_TESSFACTOR);
329         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_POSITIONT);
330         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_COLOR);
331         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_FOG);
332         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_DEPTH);
333         WINED3DDECLUSAGE_TO_STR(D3DDECLUSAGE_SAMPLE);
334 #undef WINED3DDECLUSAGE_TO_STR
335         default:
336             FIXME("Unrecognized %u declaration usage!\n", usage);
337             return "unrecognized";
338     }
339 }
340
341 const char* debug_d3dresourcetype(WINED3DRESOURCETYPE res) {
342   switch (res) {
343 #define RES_TO_STR(res) case res: return #res;
344     RES_TO_STR(WINED3DRTYPE_SURFACE);
345     RES_TO_STR(WINED3DRTYPE_VOLUME);
346     RES_TO_STR(WINED3DRTYPE_TEXTURE);
347     RES_TO_STR(WINED3DRTYPE_VOLUMETEXTURE);
348     RES_TO_STR(WINED3DRTYPE_CUBETEXTURE);
349     RES_TO_STR(WINED3DRTYPE_VERTEXBUFFER);
350     RES_TO_STR(WINED3DRTYPE_INDEXBUFFER);
351 #undef  RES_TO_STR
352   default:
353     FIXME("Unrecognized %u WINED3DRESOURCETYPE!\n", res);
354     return "unrecognized";
355   }
356 }
357
358 const char* debug_d3dprimitivetype(WINED3DPRIMITIVETYPE PrimitiveType) {
359   switch (PrimitiveType) {
360 #define PRIM_TO_STR(prim) case prim: return #prim;
361     PRIM_TO_STR(WINED3DPT_POINTLIST);
362     PRIM_TO_STR(WINED3DPT_LINELIST);
363     PRIM_TO_STR(WINED3DPT_LINESTRIP);
364     PRIM_TO_STR(WINED3DPT_TRIANGLELIST);
365     PRIM_TO_STR(WINED3DPT_TRIANGLESTRIP);
366     PRIM_TO_STR(WINED3DPT_TRIANGLEFAN);
367 #undef  PRIM_TO_STR
368   default:
369     FIXME("Unrecognized %u WINED3DPRIMITIVETYPE!\n", PrimitiveType);
370     return "unrecognized";
371   }
372 }
373
374 const char* debug_d3drenderstate(DWORD state) {
375   switch (state) {
376 #define D3DSTATE_TO_STR(u) case u: return #u
377     D3DSTATE_TO_STR(WINED3DRS_TEXTUREHANDLE             );
378     D3DSTATE_TO_STR(WINED3DRS_ANTIALIAS                 );
379     D3DSTATE_TO_STR(WINED3DRS_TEXTUREADDRESS            );
380     D3DSTATE_TO_STR(WINED3DRS_TEXTUREPERSPECTIVE        );
381     D3DSTATE_TO_STR(WINED3DRS_WRAPU                     );
382     D3DSTATE_TO_STR(WINED3DRS_WRAPV                     );
383     D3DSTATE_TO_STR(WINED3DRS_ZENABLE                   );
384     D3DSTATE_TO_STR(WINED3DRS_FILLMODE                  );
385     D3DSTATE_TO_STR(WINED3DRS_SHADEMODE                 );
386     D3DSTATE_TO_STR(WINED3DRS_LINEPATTERN               );
387     D3DSTATE_TO_STR(WINED3DRS_MONOENABLE                );
388     D3DSTATE_TO_STR(WINED3DRS_ROP2                      );
389     D3DSTATE_TO_STR(WINED3DRS_PLANEMASK                 );
390     D3DSTATE_TO_STR(WINED3DRS_ZWRITEENABLE              );
391     D3DSTATE_TO_STR(WINED3DRS_ALPHATESTENABLE           );
392     D3DSTATE_TO_STR(WINED3DRS_LASTPIXEL                 );
393     D3DSTATE_TO_STR(WINED3DRS_TEXTUREMAG                );
394     D3DSTATE_TO_STR(WINED3DRS_TEXTUREMIN                );
395     D3DSTATE_TO_STR(WINED3DRS_SRCBLEND                  );
396     D3DSTATE_TO_STR(WINED3DRS_DESTBLEND                 );
397     D3DSTATE_TO_STR(WINED3DRS_TEXTUREMAPBLEND           );
398     D3DSTATE_TO_STR(WINED3DRS_CULLMODE                  );
399     D3DSTATE_TO_STR(WINED3DRS_ZFUNC                     );
400     D3DSTATE_TO_STR(WINED3DRS_ALPHAREF                  );
401     D3DSTATE_TO_STR(WINED3DRS_ALPHAFUNC                 );
402     D3DSTATE_TO_STR(WINED3DRS_DITHERENABLE              );
403     D3DSTATE_TO_STR(WINED3DRS_ALPHABLENDENABLE          );
404     D3DSTATE_TO_STR(WINED3DRS_FOGENABLE                 );
405     D3DSTATE_TO_STR(WINED3DRS_SPECULARENABLE            );
406     D3DSTATE_TO_STR(WINED3DRS_ZVISIBLE                  );
407     D3DSTATE_TO_STR(WINED3DRS_SUBPIXEL                  );
408     D3DSTATE_TO_STR(WINED3DRS_SUBPIXELX                 );
409     D3DSTATE_TO_STR(WINED3DRS_STIPPLEDALPHA             );
410     D3DSTATE_TO_STR(WINED3DRS_FOGCOLOR                  );
411     D3DSTATE_TO_STR(WINED3DRS_FOGTABLEMODE              );
412     D3DSTATE_TO_STR(WINED3DRS_FOGSTART                  );
413     D3DSTATE_TO_STR(WINED3DRS_FOGEND                    );
414     D3DSTATE_TO_STR(WINED3DRS_FOGDENSITY                );
415     D3DSTATE_TO_STR(WINED3DRS_STIPPLEENABLE             );
416     D3DSTATE_TO_STR(WINED3DRS_EDGEANTIALIAS             );
417     D3DSTATE_TO_STR(WINED3DRS_COLORKEYENABLE            );
418     D3DSTATE_TO_STR(WINED3DRS_BORDERCOLOR               );
419     D3DSTATE_TO_STR(WINED3DRS_TEXTUREADDRESSU           );
420     D3DSTATE_TO_STR(WINED3DRS_TEXTUREADDRESSV           );
421     D3DSTATE_TO_STR(WINED3DRS_MIPMAPLODBIAS             );
422     D3DSTATE_TO_STR(WINED3DRS_ZBIAS                     );
423     D3DSTATE_TO_STR(WINED3DRS_RANGEFOGENABLE            );
424     D3DSTATE_TO_STR(WINED3DRS_ANISOTROPY                );
425     D3DSTATE_TO_STR(WINED3DRS_FLUSHBATCH                );
426     D3DSTATE_TO_STR(WINED3DRS_TRANSLUCENTSORTINDEPENDENT);
427     D3DSTATE_TO_STR(WINED3DRS_STENCILENABLE             );
428     D3DSTATE_TO_STR(WINED3DRS_STENCILFAIL               );
429     D3DSTATE_TO_STR(WINED3DRS_STENCILZFAIL              );
430     D3DSTATE_TO_STR(WINED3DRS_STENCILPASS               );
431     D3DSTATE_TO_STR(WINED3DRS_STENCILFUNC               );
432     D3DSTATE_TO_STR(WINED3DRS_STENCILREF                );
433     D3DSTATE_TO_STR(WINED3DRS_STENCILMASK               );
434     D3DSTATE_TO_STR(WINED3DRS_STENCILWRITEMASK          );
435     D3DSTATE_TO_STR(WINED3DRS_TEXTUREFACTOR             );
436     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN00          );
437     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN01          );
438     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN02          );
439     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN03          );
440     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN04          );
441     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN05          );
442     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN06          );
443     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN07          );
444     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN08          );
445     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN09          );
446     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN10          );
447     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN11          );
448     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN12          );
449     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN13          );
450     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN14          );
451     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN15          );
452     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN16          );
453     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN17          );
454     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN18          );
455     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN19          );
456     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN20          );
457     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN21          );
458     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN22          );
459     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN23          );
460     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN24          );
461     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN25          );
462     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN26          );
463     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN27          );
464     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN28          );
465     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN29          );
466     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN30          );
467     D3DSTATE_TO_STR(WINED3DRS_STIPPLEPATTERN31          );
468     D3DSTATE_TO_STR(WINED3DRS_WRAP0                     );
469     D3DSTATE_TO_STR(WINED3DRS_WRAP1                     );
470     D3DSTATE_TO_STR(WINED3DRS_WRAP2                     );
471     D3DSTATE_TO_STR(WINED3DRS_WRAP3                     );
472     D3DSTATE_TO_STR(WINED3DRS_WRAP4                     );
473     D3DSTATE_TO_STR(WINED3DRS_WRAP5                     );
474     D3DSTATE_TO_STR(WINED3DRS_WRAP6                     );
475     D3DSTATE_TO_STR(WINED3DRS_WRAP7                     );
476     D3DSTATE_TO_STR(WINED3DRS_CLIPPING                  );
477     D3DSTATE_TO_STR(WINED3DRS_LIGHTING                  );
478     D3DSTATE_TO_STR(WINED3DRS_EXTENTS                   );
479     D3DSTATE_TO_STR(WINED3DRS_AMBIENT                   );
480     D3DSTATE_TO_STR(WINED3DRS_FOGVERTEXMODE             );
481     D3DSTATE_TO_STR(WINED3DRS_COLORVERTEX               );
482     D3DSTATE_TO_STR(WINED3DRS_LOCALVIEWER               );
483     D3DSTATE_TO_STR(WINED3DRS_NORMALIZENORMALS          );
484     D3DSTATE_TO_STR(WINED3DRS_COLORKEYBLENDENABLE       );
485     D3DSTATE_TO_STR(WINED3DRS_DIFFUSEMATERIALSOURCE     );
486     D3DSTATE_TO_STR(WINED3DRS_SPECULARMATERIALSOURCE    );
487     D3DSTATE_TO_STR(WINED3DRS_AMBIENTMATERIALSOURCE     );
488     D3DSTATE_TO_STR(WINED3DRS_EMISSIVEMATERIALSOURCE    );
489     D3DSTATE_TO_STR(WINED3DRS_VERTEXBLEND               );
490     D3DSTATE_TO_STR(WINED3DRS_CLIPPLANEENABLE           );
491     D3DSTATE_TO_STR(WINED3DRS_SOFTWAREVERTEXPROCESSING  );
492     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE                 );
493     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MIN             );
494     D3DSTATE_TO_STR(WINED3DRS_POINTSPRITEENABLE         );
495     D3DSTATE_TO_STR(WINED3DRS_POINTSCALEENABLE          );
496     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_A              );
497     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_B              );
498     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_C              );
499     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEANTIALIAS      );
500     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEMASK           );
501     D3DSTATE_TO_STR(WINED3DRS_PATCHEDGESTYLE            );
502     D3DSTATE_TO_STR(WINED3DRS_PATCHSEGMENTS             );
503     D3DSTATE_TO_STR(WINED3DRS_DEBUGMONITORTOKEN         );
504     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MAX             );
505     D3DSTATE_TO_STR(WINED3DRS_INDEXEDVERTEXBLENDENABLE  );
506     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE          );
507     D3DSTATE_TO_STR(WINED3DRS_TWEENFACTOR               );
508     D3DSTATE_TO_STR(WINED3DRS_BLENDOP                   );
509     D3DSTATE_TO_STR(WINED3DRS_POSITIONDEGREE            );
510     D3DSTATE_TO_STR(WINED3DRS_NORMALDEGREE              );
511     D3DSTATE_TO_STR(WINED3DRS_SCISSORTESTENABLE         );
512     D3DSTATE_TO_STR(WINED3DRS_SLOPESCALEDEPTHBIAS       );
513     D3DSTATE_TO_STR(WINED3DRS_ANTIALIASEDLINEENABLE     );
514     D3DSTATE_TO_STR(WINED3DRS_MINTESSELLATIONLEVEL      );
515     D3DSTATE_TO_STR(WINED3DRS_MAXTESSELLATIONLEVEL      );
516     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_X            );
517     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Y            );
518     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Z            );
519     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_W            );
520     D3DSTATE_TO_STR(WINED3DRS_ENABLEADAPTIVETESSELLATION);
521     D3DSTATE_TO_STR(WINED3DRS_TWOSIDEDSTENCILMODE       );
522     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFAIL           );
523     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILZFAIL          );
524     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILPASS           );
525     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFUNC           );
526     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE1         );
527     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE2         );
528     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE3         );
529     D3DSTATE_TO_STR(WINED3DRS_BLENDFACTOR               );
530     D3DSTATE_TO_STR(WINED3DRS_SRGBWRITEENABLE           );
531     D3DSTATE_TO_STR(WINED3DRS_DEPTHBIAS                 );
532     D3DSTATE_TO_STR(WINED3DRS_WRAP8                     );
533     D3DSTATE_TO_STR(WINED3DRS_WRAP9                     );
534     D3DSTATE_TO_STR(WINED3DRS_WRAP10                    );
535     D3DSTATE_TO_STR(WINED3DRS_WRAP11                    );
536     D3DSTATE_TO_STR(WINED3DRS_WRAP12                    );
537     D3DSTATE_TO_STR(WINED3DRS_WRAP13                    );
538     D3DSTATE_TO_STR(WINED3DRS_WRAP14                    );
539     D3DSTATE_TO_STR(WINED3DRS_WRAP15                    );
540     D3DSTATE_TO_STR(WINED3DRS_SEPARATEALPHABLENDENABLE  );
541     D3DSTATE_TO_STR(WINED3DRS_SRCBLENDALPHA             );
542     D3DSTATE_TO_STR(WINED3DRS_DESTBLENDALPHA            );
543     D3DSTATE_TO_STR(WINED3DRS_BLENDOPALPHA              );
544 #undef D3DSTATE_TO_STR
545   default:
546     FIXME("Unrecognized %u render state!\n", state);
547     return "unrecognized";
548   }
549 }
550
551 const char* debug_d3dsamplerstate(DWORD state) {
552   switch (state) {
553 #define D3DSTATE_TO_STR(u) case u: return #u
554     D3DSTATE_TO_STR(WINED3DSAMP_BORDERCOLOR  );
555     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSU     );
556     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSV     );
557     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSW     );
558     D3DSTATE_TO_STR(WINED3DSAMP_MAGFILTER    );
559     D3DSTATE_TO_STR(WINED3DSAMP_MINFILTER    );
560     D3DSTATE_TO_STR(WINED3DSAMP_MIPFILTER    );
561     D3DSTATE_TO_STR(WINED3DSAMP_MIPMAPLODBIAS);
562     D3DSTATE_TO_STR(WINED3DSAMP_MAXMIPLEVEL  );
563     D3DSTATE_TO_STR(WINED3DSAMP_MAXANISOTROPY);
564     D3DSTATE_TO_STR(WINED3DSAMP_SRGBTEXTURE  );
565     D3DSTATE_TO_STR(WINED3DSAMP_ELEMENTINDEX );
566     D3DSTATE_TO_STR(WINED3DSAMP_DMAPOFFSET   );
567 #undef D3DSTATE_TO_STR
568   default:
569     FIXME("Unrecognized %u sampler state!\n", state);
570     return "unrecognized";
571   }
572 }
573
574 const char* debug_d3dtexturestate(DWORD state) {
575   switch (state) {
576 #define D3DSTATE_TO_STR(u) case u: return #u
577     D3DSTATE_TO_STR(WINED3DTSS_COLOROP               );
578     D3DSTATE_TO_STR(WINED3DTSS_COLORARG1             );
579     D3DSTATE_TO_STR(WINED3DTSS_COLORARG2             );
580     D3DSTATE_TO_STR(WINED3DTSS_ALPHAOP               );
581     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG1             );
582     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG2             );
583     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT00          );
584     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT01          );
585     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT10          );
586     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT11          );
587     D3DSTATE_TO_STR(WINED3DTSS_TEXCOORDINDEX         );
588     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLSCALE         );
589     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLOFFSET        );
590     D3DSTATE_TO_STR(WINED3DTSS_TEXTURETRANSFORMFLAGS );
591     D3DSTATE_TO_STR(WINED3DTSS_ADDRESSW              );
592     D3DSTATE_TO_STR(WINED3DTSS_COLORARG0             );
593     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG0             );
594     D3DSTATE_TO_STR(WINED3DTSS_RESULTARG             );
595     D3DSTATE_TO_STR(WINED3DTSS_CONSTANT              );
596 #undef D3DSTATE_TO_STR
597   case 12:
598     /* Note WINED3DTSS are not consecutive, so skip these */
599     return "unused";
600     break;
601   default:
602     FIXME("Unrecognized %u texture state!\n", state);
603     return "unrecognized";
604   }
605 }
606
607 const char* debug_d3dtop(WINED3DTEXTUREOP d3dtop) {
608     switch (d3dtop) {
609 #define D3DTOP_TO_STR(u) case u: return #u
610         D3DTOP_TO_STR(WINED3DTOP_DISABLE);
611         D3DTOP_TO_STR(WINED3DTOP_SELECTARG1);
612         D3DTOP_TO_STR(WINED3DTOP_SELECTARG2);
613         D3DTOP_TO_STR(WINED3DTOP_MODULATE);
614         D3DTOP_TO_STR(WINED3DTOP_MODULATE2X);
615         D3DTOP_TO_STR(WINED3DTOP_MODULATE4X);
616         D3DTOP_TO_STR(WINED3DTOP_ADD);
617         D3DTOP_TO_STR(WINED3DTOP_ADDSIGNED);
618         D3DTOP_TO_STR(WINED3DTOP_SUBTRACT);
619         D3DTOP_TO_STR(WINED3DTOP_ADDSMOOTH);
620         D3DTOP_TO_STR(WINED3DTOP_BLENDDIFFUSEALPHA);
621         D3DTOP_TO_STR(WINED3DTOP_BLENDTEXTUREALPHA);
622         D3DTOP_TO_STR(WINED3DTOP_BLENDFACTORALPHA);
623         D3DTOP_TO_STR(WINED3DTOP_BLENDTEXTUREALPHAPM);
624         D3DTOP_TO_STR(WINED3DTOP_BLENDCURRENTALPHA);
625         D3DTOP_TO_STR(WINED3DTOP_PREMODULATE);
626         D3DTOP_TO_STR(WINED3DTOP_MODULATEALPHA_ADDCOLOR);
627         D3DTOP_TO_STR(WINED3DTOP_MODULATECOLOR_ADDALPHA);
628         D3DTOP_TO_STR(WINED3DTOP_MODULATEINVALPHA_ADDCOLOR);
629         D3DTOP_TO_STR(WINED3DTOP_MODULATEINVCOLOR_ADDALPHA);
630         D3DTOP_TO_STR(WINED3DTOP_BUMPENVMAP);
631         D3DTOP_TO_STR(WINED3DTOP_BUMPENVMAPLUMINANCE);
632         D3DTOP_TO_STR(WINED3DTOP_DOTPRODUCT3);
633         D3DTOP_TO_STR(WINED3DTOP_MULTIPLYADD);
634         D3DTOP_TO_STR(WINED3DTOP_LERP);
635 #undef D3DTOP_TO_STR
636         default:
637             FIXME("Unrecognized %u WINED3DTOP\n", d3dtop);
638             return "unrecognized";
639     }
640 }
641
642 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype) {
643     switch (tstype) {
644 #define TSTYPE_TO_STR(tstype) case tstype: return #tstype
645     TSTYPE_TO_STR(WINED3DTS_VIEW);
646     TSTYPE_TO_STR(WINED3DTS_PROJECTION);
647     TSTYPE_TO_STR(WINED3DTS_TEXTURE0);
648     TSTYPE_TO_STR(WINED3DTS_TEXTURE1);
649     TSTYPE_TO_STR(WINED3DTS_TEXTURE2);
650     TSTYPE_TO_STR(WINED3DTS_TEXTURE3);
651     TSTYPE_TO_STR(WINED3DTS_TEXTURE4);
652     TSTYPE_TO_STR(WINED3DTS_TEXTURE5);
653     TSTYPE_TO_STR(WINED3DTS_TEXTURE6);
654     TSTYPE_TO_STR(WINED3DTS_TEXTURE7);
655     TSTYPE_TO_STR(WINED3DTS_WORLDMATRIX(0));
656 #undef TSTYPE_TO_STR
657     default:
658         if (tstype > 256 && tstype < 512) {
659             FIXME("WINED3DTS_WORLDMATRIX(%u). 1..255 not currently supported\n", tstype);
660             return ("WINED3DTS_WORLDMATRIX > 0");
661         }
662         FIXME("Unrecognized %u WINED3DTS\n", tstype);
663         return "unrecognized";
664     }
665 }
666
667 const char* debug_d3dpool(WINED3DPOOL Pool) {
668   switch (Pool) {
669 #define POOL_TO_STR(p) case p: return #p;
670     POOL_TO_STR(WINED3DPOOL_DEFAULT);
671     POOL_TO_STR(WINED3DPOOL_MANAGED);
672     POOL_TO_STR(WINED3DPOOL_SYSTEMMEM);
673     POOL_TO_STR(WINED3DPOOL_SCRATCH);
674 #undef  POOL_TO_STR
675   default:
676     FIXME("Unrecognized %u WINED3DPOOL!\n", Pool);
677     return "unrecognized";
678   }
679 }
680
681 /*****************************************************************************
682  * Useful functions mapping GL <-> D3D values
683  */
684 GLenum StencilOp(DWORD op) {
685     switch(op) {
686     case WINED3DSTENCILOP_KEEP    : return GL_KEEP;
687     case WINED3DSTENCILOP_ZERO    : return GL_ZERO;
688     case WINED3DSTENCILOP_REPLACE : return GL_REPLACE;
689     case WINED3DSTENCILOP_INCRSAT : return GL_INCR;
690     case WINED3DSTENCILOP_DECRSAT : return GL_DECR;
691     case WINED3DSTENCILOP_INVERT  : return GL_INVERT;
692     case WINED3DSTENCILOP_INCR    : return GL_INCR_WRAP_EXT;
693     case WINED3DSTENCILOP_DECR    : return GL_DECR_WRAP_EXT;
694     default:
695         FIXME("Unrecognized stencil op %d\n", op);
696         return GL_KEEP;
697     }
698 }
699
700 GLenum CompareFunc(DWORD func) {
701     switch ((WINED3DCMPFUNC)func) {
702     case WINED3DCMP_NEVER        : return GL_NEVER;
703     case WINED3DCMP_LESS         : return GL_LESS;
704     case WINED3DCMP_EQUAL        : return GL_EQUAL;
705     case WINED3DCMP_LESSEQUAL    : return GL_LEQUAL;
706     case WINED3DCMP_GREATER      : return GL_GREATER;
707     case WINED3DCMP_NOTEQUAL     : return GL_NOTEQUAL;
708     case WINED3DCMP_GREATEREQUAL : return GL_GEQUAL;
709     case WINED3DCMP_ALWAYS       : return GL_ALWAYS;
710     default:
711         FIXME("Unrecognized WINED3DCMPFUNC value %d\n", func);
712         return 0;
713     }
714 }
715
716 static GLenum d3dta_to_combiner_input(DWORD d3dta, DWORD stage, INT texture_idx) {
717     switch (d3dta) {
718         case WINED3DTA_DIFFUSE:
719             return GL_PRIMARY_COLOR_NV;
720
721         case WINED3DTA_CURRENT:
722             if (stage) return GL_SPARE0_NV;
723             else return GL_PRIMARY_COLOR_NV;
724
725         case WINED3DTA_TEXTURE:
726             if (texture_idx > -1) return GL_TEXTURE0_ARB + texture_idx;
727             else return GL_PRIMARY_COLOR_NV;
728
729         case WINED3DTA_TFACTOR:
730             return GL_CONSTANT_COLOR0_NV;
731
732         case WINED3DTA_SPECULAR:
733             return GL_SECONDARY_COLOR_NV;
734
735         case WINED3DTA_TEMP:
736             /* TODO: Support WINED3DTSS_RESULTARG */
737             FIXME("WINED3DTA_TEMP, not properly supported.\n");
738             return GL_SPARE1_NV;
739
740         case WINED3DTA_CONSTANT:
741             /* TODO: Support per stage constants (WINED3DTSS_CONSTANT, NV_register_combiners2) */
742             FIXME("WINED3DTA_CONSTANT, not properly supported.\n");
743             return GL_CONSTANT_COLOR1_NV;
744
745         default:
746             FIXME("Unrecognized texture arg %#x\n", d3dta);
747             return GL_TEXTURE;
748     }
749 }
750
751 static GLenum invert_mapping(GLenum mapping) {
752     if (mapping == GL_UNSIGNED_INVERT_NV) return GL_SIGNED_IDENTITY_NV;
753     else if (mapping == GL_SIGNED_IDENTITY_NV) return GL_UNSIGNED_INVERT_NV;
754
755     FIXME("Unhandled mapping %#x\n", mapping);
756     return mapping;
757 }
758
759 static void get_src_and_opr_nvrc(DWORD stage, DWORD arg, BOOL is_alpha, GLenum* input, GLenum* mapping, GLenum *component_usage, INT texture_idx) {
760     /* The WINED3DTA_COMPLEMENT flag specifies the complement of the input should
761      * be used. */
762     if (arg & WINED3DTA_COMPLEMENT) *mapping = GL_UNSIGNED_INVERT_NV;
763     else *mapping = GL_SIGNED_IDENTITY_NV;
764
765     /* The WINED3DTA_ALPHAREPLICATE flag specifies the alpha component of the input
766      * should be used for all input components. */
767     if (is_alpha || arg & WINED3DTA_ALPHAREPLICATE) *component_usage = GL_ALPHA;
768     else *component_usage = GL_RGB;
769
770     *input = d3dta_to_combiner_input(arg & WINED3DTA_SELECTMASK, stage, texture_idx);
771 }
772
773 typedef struct {
774     GLenum input[3];
775     GLenum mapping[3];
776     GLenum component_usage[3];
777 } tex_op_args;
778
779 static BOOL is_invalid_op(IWineD3DDeviceImpl *This, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3) {
780     if (op == WINED3DTOP_DISABLE) return FALSE;
781     if (This->stateBlock->textures[stage]) return FALSE;
782
783     if (arg1 == WINED3DTA_TEXTURE && op != WINED3DTOP_SELECTARG2) return TRUE;
784     if (arg2 == WINED3DTA_TEXTURE && op != WINED3DTOP_SELECTARG1) return TRUE;
785     if (arg3 == WINED3DTA_TEXTURE && (op == WINED3DTOP_MULTIPLYADD || op == WINED3DTOP_LERP)) return TRUE;
786
787     return FALSE;
788 }
789
790 void set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx) {
791     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl*)iface;
792     tex_op_args tex_op_args = {{0}, {0}, {0}};
793     GLenum portion = is_alpha ? GL_ALPHA : GL_RGB;
794     GLenum target = GL_COMBINER0_NV + stage;
795
796     TRACE("stage %d, is_alpha %d, op %s, arg1 %#x, arg2 %#x, arg3 %#x, texture_idx %d\n",
797             stage, is_alpha, debug_d3dtop(op), arg1, arg2, arg3, texture_idx);
798
799     /* If a texture stage references an invalid texture unit the stage just
800      * passes through the result from the previous stage */
801     if (is_invalid_op(This, stage, op, arg1, arg2, arg3)) {
802         arg1 = WINED3DTA_CURRENT;
803         op = WINED3DTOP_SELECTARG1;
804     }
805
806     get_src_and_opr_nvrc(stage, arg1, is_alpha, &tex_op_args.input[0],
807             &tex_op_args.mapping[0], &tex_op_args.component_usage[0], texture_idx);
808     get_src_and_opr_nvrc(stage, arg2, is_alpha, &tex_op_args.input[1],
809             &tex_op_args.mapping[1], &tex_op_args.component_usage[1], texture_idx);
810     get_src_and_opr_nvrc(stage, arg3, is_alpha, &tex_op_args.input[2],
811             &tex_op_args.mapping[2], &tex_op_args.component_usage[2], texture_idx);
812
813     ENTER_GL();
814
815     switch(op)
816     {
817         case WINED3DTOP_DISABLE:
818             /* Only for alpha */
819             if (!is_alpha) ERR("Shouldn't be called for WINED3DTSS_COLOROP (WINED3DTOP_DISABLE)\n");
820             /* Input, prev_alpha*1 */
821             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
822                     GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA));
823             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
824                     GL_ZERO, GL_UNSIGNED_INVERT_NV, GL_ALPHA));
825
826             /* Output */
827             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
828                     GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
829             break;
830
831         case WINED3DTOP_SELECTARG1:
832         case WINED3DTOP_SELECTARG2:
833             /* Input, arg*1 */
834             if (op == WINED3DTOP_SELECTARG1) {
835                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
836                         tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
837             } else {
838                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
839                         tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
840             }
841             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
842                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
843
844             /* Output */
845             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
846                     GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
847             break;
848
849         case WINED3DTOP_MODULATE:
850         case WINED3DTOP_MODULATE2X:
851         case WINED3DTOP_MODULATE4X:
852             /* Input, arg1*arg2 */
853             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
854                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
855             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
856                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
857
858             /* Output */
859             if (op == WINED3DTOP_MODULATE) {
860                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
861                         GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
862             } else if (op == WINED3DTOP_MODULATE2X) {
863                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
864                         GL_DISCARD_NV, GL_SCALE_BY_TWO_NV, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
865             } else if (op == WINED3DTOP_MODULATE4X) {
866                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
867                         GL_DISCARD_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
868             }
869             break;
870
871         case WINED3DTOP_ADD:
872         case WINED3DTOP_ADDSIGNED:
873         case WINED3DTOP_ADDSIGNED2X:
874             /* Input, arg1*1+arg2*1 */
875             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
876                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
877             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
878                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
879             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
880                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
881             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
882                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
883
884             /* Output */
885             if (op == WINED3DTOP_ADD) {
886                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
887                         GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
888             } else if (op == WINED3DTOP_ADDSIGNED) {
889                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
890                         GL_SPARE0_NV, GL_NONE, GL_BIAS_BY_NEGATIVE_ONE_HALF_NV, GL_FALSE, GL_FALSE, GL_FALSE));
891             } else if (op == WINED3DTOP_ADDSIGNED2X) {
892                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
893                         GL_SPARE0_NV, GL_SCALE_BY_TWO_NV, GL_BIAS_BY_NEGATIVE_ONE_HALF_NV, GL_FALSE, GL_FALSE, GL_FALSE));
894             }
895             break;
896
897         case WINED3DTOP_SUBTRACT:
898             /* Input, arg1*1+-arg2*1 */
899             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
900                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
901             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
902                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
903             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
904                     tex_op_args.input[1], GL_SIGNED_NEGATE_NV, tex_op_args.component_usage[1]));
905             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
906                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
907
908             /* Output */
909             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
910                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
911             break;
912
913         case WINED3DTOP_ADDSMOOTH:
914             /* Input, arg1*1+(1-arg1)*arg2 */
915             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
916                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
917             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
918                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
919             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
920                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
921             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
922                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
923
924             /* Output */
925             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
926                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
927             break;
928
929         case WINED3DTOP_BLENDDIFFUSEALPHA:
930         case WINED3DTOP_BLENDTEXTUREALPHA:
931         case WINED3DTOP_BLENDFACTORALPHA:
932         case WINED3DTOP_BLENDTEXTUREALPHAPM:
933         case WINED3DTOP_BLENDCURRENTALPHA:
934         {
935             GLenum alpha_src = GL_PRIMARY_COLOR_NV;
936             if (op == WINED3DTOP_BLENDDIFFUSEALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_DIFFUSE, stage, texture_idx);
937             else if (op == WINED3DTOP_BLENDTEXTUREALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_TEXTURE, stage, texture_idx);
938             else if (op == WINED3DTOP_BLENDFACTORALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_TFACTOR, stage, texture_idx);
939             else if (op == WINED3DTOP_BLENDTEXTUREALPHAPM) alpha_src = d3dta_to_combiner_input(WINED3DTA_TEXTURE, stage, texture_idx);
940             else if (op == WINED3DTOP_BLENDCURRENTALPHA) alpha_src = d3dta_to_combiner_input(WINED3DTA_CURRENT, stage, texture_idx);
941             else FIXME("Unhandled WINED3DTOP %s, shouldn't happen\n", debug_d3dtop(op));
942
943             /* Input, arg1*alpha_src+arg2*(1-alpha_src) */
944             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
945                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
946             if (op == WINED3DTOP_BLENDTEXTUREALPHAPM)
947             {
948                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
949                         GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
950             } else {
951                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
952                         alpha_src, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA));
953             }
954             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
955                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
956             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
957                     alpha_src, GL_UNSIGNED_INVERT_NV, GL_ALPHA));
958
959             /* Output */
960             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
961                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
962             break;
963         }
964
965         case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
966             /* Input, arg1_alpha*arg2_rgb+arg1_rgb*1 */
967             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATEALPHA_ADDCOLOR)\n");
968             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
969                     tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
970             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
971                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
972             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
973                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
974             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
975                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
976
977             /* Output */
978             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
979                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
980             break;
981
982         case WINED3DTOP_MODULATECOLOR_ADDALPHA:
983             /* Input, arg1_rgb*arg2_rgb+arg1_alpha*1 */
984             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATECOLOR_ADDALPHA)\n");
985             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
986                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
987             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
988                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
989             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
990                     tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
991             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
992                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
993
994             /* Output */
995             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
996                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
997             break;
998
999         case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
1000             /* Input, (1-arg1_alpha)*arg2_rgb+arg1_rgb*1 */
1001             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATEINVALPHA_ADDCOLOR)\n");
1002             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1003                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), GL_ALPHA));
1004             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1005                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
1006             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
1007                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
1008             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
1009                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
1010
1011             /* Output */
1012             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
1013                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
1014             break;
1015
1016         case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
1017             /* Input, (1-arg1_rgb)*arg2_rgb+arg1_alpha*1 */
1018             if (is_alpha) ERR("Only supported for WINED3DTSS_COLOROP (WINED3DTOP_MODULATEINVCOLOR_ADDALPHA)\n");
1019             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1020                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
1021             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1022                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
1023             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
1024                     tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
1025             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
1026                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
1027
1028             /* Output */
1029             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
1030                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
1031             break;
1032
1033         case WINED3DTOP_DOTPRODUCT3:
1034             /* Input, arg1 . arg2 */
1035             /* FIXME: DX7 uses a different calculation? */
1036             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1037                     tex_op_args.input[0], GL_EXPAND_NORMAL_NV, tex_op_args.component_usage[0]));
1038             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1039                     tex_op_args.input[1], GL_EXPAND_NORMAL_NV, tex_op_args.component_usage[1]));
1040
1041             /* Output */
1042             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
1043                     GL_DISCARD_NV, GL_NONE, GL_NONE, GL_TRUE, GL_FALSE, GL_FALSE));
1044             break;
1045
1046         case WINED3DTOP_MULTIPLYADD:
1047             /* Input, arg1*1+arg2*arg3 */
1048             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1049                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
1050             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1051                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
1052             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
1053                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
1054             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
1055                     tex_op_args.input[2], tex_op_args.mapping[2], tex_op_args.component_usage[2]));
1056
1057             /* Output */
1058             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
1059                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
1060             break;
1061
1062         case WINED3DTOP_LERP:
1063             /* Input, arg1*arg2+(1-arg1)*arg3 */
1064             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1065                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
1066             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1067                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
1068             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
1069                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
1070             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
1071                     tex_op_args.input[2], tex_op_args.mapping[2], tex_op_args.component_usage[2]));
1072
1073             /* Output */
1074             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
1075                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
1076             break;
1077
1078         default:
1079             FIXME("Unhandled WINED3DTOP: stage %d, is_alpha %d, op %s (%#x), arg1 %#x, arg2 %#x, arg3 %#x, texture_idx %d\n",
1080                     stage, is_alpha, debug_d3dtop(op), op, arg1, arg2, arg3, texture_idx);
1081     }
1082
1083     checkGLcall("set_tex_op_nvrc()\n");
1084
1085     LEAVE_GL();
1086 }
1087
1088 static void get_src_and_opr(DWORD arg, BOOL is_alpha, GLenum* source, GLenum* operand) {
1089     /* The WINED3DTA_ALPHAREPLICATE flag specifies the alpha component of the
1090      * input should be used for all input components. The WINED3DTA_COMPLEMENT
1091      * flag specifies the complement of the input should be used. */
1092     BOOL from_alpha = is_alpha || arg & WINED3DTA_ALPHAREPLICATE;
1093     BOOL complement = arg & WINED3DTA_COMPLEMENT;
1094
1095     /* Calculate the operand */
1096     if (complement) {
1097         if (from_alpha) *operand = GL_ONE_MINUS_SRC_ALPHA;
1098         else *operand = GL_ONE_MINUS_SRC_COLOR;
1099     } else {
1100         if (from_alpha) *operand = GL_SRC_ALPHA;
1101         else *operand = GL_SRC_COLOR;
1102     }
1103
1104     /* Calculate the source */
1105     switch (arg & WINED3DTA_SELECTMASK) {
1106         case WINED3DTA_CURRENT: *source = GL_PREVIOUS_EXT; break;
1107         case WINED3DTA_DIFFUSE: *source = GL_PRIMARY_COLOR_EXT; break;
1108         case WINED3DTA_TEXTURE: *source = GL_TEXTURE; break;
1109         case WINED3DTA_TFACTOR: *source = GL_CONSTANT_EXT; break;
1110         case WINED3DTA_SPECULAR:
1111             /*
1112              * According to the GL_ARB_texture_env_combine specs, SPECULAR is
1113              * 'Secondary color' and isn't supported until base GL supports it
1114              * There is no concept of temp registers as far as I can tell
1115              */
1116             FIXME("Unhandled texture arg WINED3DTA_SPECULAR\n");
1117             *source = GL_TEXTURE;
1118             break;
1119         default:
1120             FIXME("Unrecognized texture arg %#x\n", arg);
1121             *source = GL_TEXTURE;
1122             break;
1123     }
1124 }
1125
1126 /* Set texture operations up - The following avoids lots of ifdefs in this routine!*/
1127 #if defined (GL_VERSION_1_3)
1128 # define useext(A) A
1129 # define combine_ext 1
1130 #elif defined (GL_EXT_texture_env_combine)
1131 # define useext(A) A##_EXT
1132 # define combine_ext 1
1133 #elif defined (GL_ARB_texture_env_combine)
1134 # define useext(A) A##_ARB
1135 # define combine_ext 1
1136 #else
1137 # undef combine_ext
1138 #endif
1139
1140 #if !defined(combine_ext)
1141 void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
1142 {
1143         FIXME("Requires opengl combine extensions to work\n");
1144         return;
1145 }
1146 #else
1147 /* Setup the texture operations texture stage states */
1148 void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
1149 {
1150 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
1151         GLenum src1, src2, src3;
1152         GLenum opr1, opr2, opr3;
1153         GLenum comb_target;
1154         GLenum src0_target, src1_target, src2_target;
1155         GLenum opr0_target, opr1_target, opr2_target;
1156         GLenum scal_target;
1157         GLenum opr=0, invopr, src3_target, opr3_target;
1158         BOOL Handled = FALSE;
1159         IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1160
1161         TRACE("Alpha?(%d), Stage:%d Op(%s), a1(%d), a2(%d), a3(%d)\n", isAlpha, Stage, debug_d3dtop(op), arg1, arg2, arg3);
1162
1163         ENTER_GL();
1164
1165         /* Note: Operations usually involve two ars, src0 and src1 and are operations of
1166            the form (a1 <operation> a2). However, some of the more complex operations
1167            take 3 parameters. Instead of the (sensible) addition of a3, Microsoft added
1168            in a third parameter called a0. Therefore these are operations of the form
1169            a0 <operation> a1 <operation> a2, ie the new parameter goes to the front.
1170
1171            However, below we treat the new (a0) parameter as src2/opr2, so in the actual
1172            functions below, expect their syntax to differ slightly to those listed in the
1173            manuals, ie replace arg1 with arg3, arg2 with arg1 and arg3 with arg2
1174            This affects WINED3DTOP_MULTIPLYADD and WINED3DTOP_LERP                     */
1175
1176         if (isAlpha) {
1177                 comb_target = useext(GL_COMBINE_ALPHA);
1178                 src0_target = useext(GL_SOURCE0_ALPHA);
1179                 src1_target = useext(GL_SOURCE1_ALPHA);
1180                 src2_target = useext(GL_SOURCE2_ALPHA);
1181                 opr0_target = useext(GL_OPERAND0_ALPHA);
1182                 opr1_target = useext(GL_OPERAND1_ALPHA);
1183                 opr2_target = useext(GL_OPERAND2_ALPHA);
1184                 scal_target = GL_ALPHA_SCALE;
1185         }
1186         else {
1187                 comb_target = useext(GL_COMBINE_RGB);
1188                 src0_target = useext(GL_SOURCE0_RGB);
1189                 src1_target = useext(GL_SOURCE1_RGB);
1190                 src2_target = useext(GL_SOURCE2_RGB);
1191                 opr0_target = useext(GL_OPERAND0_RGB);
1192                 opr1_target = useext(GL_OPERAND1_RGB);
1193                 opr2_target = useext(GL_OPERAND2_RGB);
1194                 scal_target = useext(GL_RGB_SCALE);
1195         }
1196
1197         /* If a texture stage references an invalid texture unit the stage just
1198          * passes through the result from the previous stage */
1199         if (is_invalid_op(This, Stage, op, arg1, arg2, arg3)) {
1200             arg1 = WINED3DTA_CURRENT;
1201             op = WINED3DTOP_SELECTARG1;
1202         }
1203
1204         /* From MSDN (WINED3DTSS_ALPHAARG1) :
1205            The default argument is WINED3DTA_TEXTURE. If no texture is set for this stage,
1206                    then the default argument is WINED3DTA_DIFFUSE.
1207                    FIXME? If texture added/removed, may need to reset back as well?    */
1208         if (isAlpha && This->stateBlock->textures[Stage] == NULL && arg1 == WINED3DTA_TEXTURE) {
1209             get_src_and_opr(WINED3DTA_DIFFUSE, isAlpha, &src1, &opr1);
1210         } else {
1211             get_src_and_opr(arg1, isAlpha, &src1, &opr1);
1212         }
1213         get_src_and_opr(arg2, isAlpha, &src2, &opr2);
1214         get_src_and_opr(arg3, isAlpha, &src3, &opr3);
1215
1216         TRACE("ct(%x), 1:(%x,%x), 2:(%x,%x), 3:(%x,%x)\n", comb_target, src1, opr1, src2, opr2, src3, opr3);
1217
1218         Handled = TRUE; /* Assume will be handled */
1219
1220         /* Other texture operations require special extensions: */
1221         if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1222           if (isAlpha) {
1223             opr = GL_SRC_ALPHA;
1224             invopr = GL_ONE_MINUS_SRC_ALPHA;
1225             src3_target = GL_SOURCE3_ALPHA_NV;
1226             opr3_target = GL_OPERAND3_ALPHA_NV;
1227           } else {
1228             opr = GL_SRC_COLOR;
1229             invopr = GL_ONE_MINUS_SRC_COLOR;
1230             src3_target = GL_SOURCE3_RGB_NV;
1231             opr3_target = GL_OPERAND3_RGB_NV;
1232           }
1233           switch (op) {
1234           case WINED3DTOP_DISABLE: /* Only for alpha */
1235             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1236             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1237             glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
1238             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1239             glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
1240             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1241             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1242             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1243             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1244             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1245             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1246             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
1247             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1248             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1249             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1250             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1251             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1252             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1253             break;
1254           case WINED3DTOP_SELECTARG1:                                          /* = a1 * 1 + 0 * 0 */
1255           case WINED3DTOP_SELECTARG2:                                          /* = a2 * 1 + 0 * 0 */
1256             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1257             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1258             if (op == WINED3DTOP_SELECTARG1) {
1259               glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1260               checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1261               glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1262               checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1263             } else {
1264               glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
1265               checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
1266               glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
1267               checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
1268             }
1269             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1270             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1271             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1272             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1273             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1274             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
1275             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1276             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1277             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1278             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1279             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1280             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1281             break;
1282
1283           case WINED3DTOP_MODULATE:
1284             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1285             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
1286             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1287             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1288             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1289             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1290             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1291             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1292             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1293             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1294             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1295             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1296             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1297             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1298             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1299             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1300             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1301             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
1302             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1303             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1304             break;
1305           case WINED3DTOP_MODULATE2X:
1306             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1307             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
1308             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1309             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1310             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1311             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1312             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1313             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1314             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1315             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1316             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1317             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1318             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1319             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1320             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1321             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1322             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1323             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
1324             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1325             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1326             break;
1327           case WINED3DTOP_MODULATE4X:
1328             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1329             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
1330             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1331             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1332             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1333             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1334             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1335             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1336             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1337             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1338             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1339             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1340             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1341             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1342             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1343             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1344             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1345             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
1346             glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
1347             checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
1348             break;
1349
1350           case WINED3DTOP_ADD:
1351             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1352             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1353             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1354             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1355             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1356             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1357             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1358             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1359             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1360             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1361             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1362             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1363             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1364             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1365             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1366             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1367             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1368             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1369             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1370             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1371             break;
1372
1373           case WINED3DTOP_ADDSIGNED:
1374             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1375             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1376             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1377             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1378             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1379             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1380             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1381             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1382             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1383             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1384             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1385             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1386             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1387             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1388             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1389             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1390             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1391             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1392             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1393             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1394             break;
1395
1396           case WINED3DTOP_ADDSIGNED2X:
1397             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1398             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1399             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1400             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1401             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1402             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1403             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1404             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1405             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1406             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1407             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1408             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1409             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1410             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1411             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1412             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1413             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1414             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1415             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1416             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1417             break;
1418
1419           case WINED3DTOP_ADDSMOOTH:
1420             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1421             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1422             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1423             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1424             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1425             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1426             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1427             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1428             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1429             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1430             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1431             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1432             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1433             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1434             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
1435             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
1436             switch (opr1) {
1437             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1438             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1439             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1440             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1441             }
1442             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1443             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1444             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1445             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1446             break;
1447
1448           case WINED3DTOP_BLENDDIFFUSEALPHA:
1449             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1450             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1451             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1452             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1453             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1454             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1455             glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR));
1456             checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR)");
1457             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1458             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1459             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1460             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1461             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1462             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1463             glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR));
1464             checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR)");
1465             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1466             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1467             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1468             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1469             break;
1470           case WINED3DTOP_BLENDTEXTUREALPHA:
1471             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1472             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1473             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1474             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1475             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1476             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1477             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_TEXTURE);
1478             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_TEXTURE");
1479             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1480             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1481             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1482             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1483             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1484             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1485             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
1486             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
1487             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1488             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1489             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1490             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1491             break;
1492           case WINED3DTOP_BLENDFACTORALPHA:
1493             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1494             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1495             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1496             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1497             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1498             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1499             glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT));
1500             checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT)");
1501             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1502             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1503             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1504             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1505             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1506             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1507             glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT));
1508             checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT)");
1509             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1510             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1511             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1512             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1513             break;
1514           case WINED3DTOP_BLENDTEXTUREALPHAPM:
1515             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1516             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1517             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1518             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1519             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1520             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1521             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1522             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1523             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1524             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1525             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1526             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1527             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1528             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1529             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
1530             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
1531             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1532             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1533             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1534             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1535             break;
1536           case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
1537             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1538             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");  /* Add = a0*a1 + a2*a3 */
1539             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);        /*   a0 = src1/opr1    */
1540             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1541             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1542             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");    /*   a1 = 1 (see docs) */
1543             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1544             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1545             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1546             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1547             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);        /*   a2 = arg2         */
1548             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1549             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1550             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");     /*  a3 = src1 alpha   */
1551             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
1552             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
1553             switch (opr) {
1554             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1555             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1556             }
1557             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1558             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1559             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1560             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1561             break;
1562           case WINED3DTOP_MODULATECOLOR_ADDALPHA:
1563             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1564             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1565             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1566             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1567             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1568             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1569             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1570             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1571             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1572             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1573             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
1574             checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
1575             switch (opr1) {
1576             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1577             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1578             }
1579             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1580             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1581             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1582             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1583             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1584             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1585             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1586             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1587             break;
1588           case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
1589             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1590             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1591             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1592             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1593             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1594             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1595             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1596             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1597             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1598             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1599             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1600             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1601             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1602             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1603             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
1604             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
1605             switch (opr1) {
1606             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1607             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1608             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1609             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1610             }
1611             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1612             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1613             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1614             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1615             break;
1616           case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
1617             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1618             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1619             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1620             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1621             switch (opr1) {
1622             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1623             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1624             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1625             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1626             }
1627             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1628             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1629             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1630             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1631             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1632             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1633             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
1634             checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
1635             switch (opr1) {
1636             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1637             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1638             }
1639             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1640             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1641             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1642             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1643             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1644             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1645             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1646             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1647             break;
1648           case WINED3DTOP_MULTIPLYADD:
1649             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1650             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1651             glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
1652             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1653             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
1654             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1655             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1656             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1657             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1658             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1659             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
1660             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1661             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr1);
1662             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1663             glTexEnvi(GL_TEXTURE_ENV, src3_target, src2);
1664             checkGLcall("GL_TEXTURE_ENV, src3_target, src3");
1665             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr2);
1666             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr3");
1667             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1668             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1669             break;
1670
1671           case WINED3DTOP_BUMPENVMAP:
1672             {
1673               if (GL_SUPPORT(NV_TEXTURE_SHADER)) {
1674                 /*
1675                   texture unit 0: GL_TEXTURE_2D
1676                   texture unit 1: GL_DOT_PRODUCT_NV
1677                   texture unit 2: GL_DOT_PRODUCT_DIFFUSE_CUBE_MAP_NV
1678                   texture unit 3: GL_DOT_PRODUCT_REFLECT_CUBE_MAP_NV
1679                 */
1680                 float m[2][2];
1681
1682                 union {
1683                   float f;
1684                   DWORD d;
1685                 } tmpvalue;
1686
1687                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT00];
1688                 m[0][0] = tmpvalue.f;
1689                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT01];
1690                 m[0][1] = tmpvalue.f;
1691                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT10];
1692                 m[1][0] = tmpvalue.f;
1693                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT11];
1694                 m[1][1] = tmpvalue.f;
1695
1696                 /*FIXME("Stage %d matrix is (%.2f,%.2f),(%.2f,%.2f)\n", Stage, m[0][0], m[0][1], m[1][0], m[1][0]);*/
1697
1698                 if (!This->texture_shader_active) {
1699                   This->texture_shader_active = TRUE;
1700                   glEnable(GL_TEXTURE_SHADER_NV);
1701                 }
1702
1703                 /*
1704                 glEnable(GL_REGISTER_COMBINERS_NV);
1705                 glCombinerParameteriNV (GL_NUM_GENERAL_COMBINERS_NV, 1);
1706                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV, GL_TEXTURE0_ARB, GL_SIGNED_IDENTITY_NV, GL_RGB);
1707                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_RGB);
1708                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV, GL_TEXTURE2_ARB, GL_SIGNED_IDENTITY_NV, GL_RGB);
1709                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_RGB);
1710                 glCombinerOutputNV (GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV, GL_PRIMARY_COLOR_NV, 0, 0, 0, 0, 0);
1711                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_A_NV, GL_TEXTURE0_ARB, GL_SIGNED_IDENTITY_NV, GL_ALPHA);
1712                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_B_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_ALPHA);
1713                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_C_NV, GL_TEXTURE2_ARB, GL_SIGNED_IDENTITY_NV, GL_ALPHA);
1714                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_D_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_ALPHA);
1715                 glCombinerOutputNV (GL_COMBINER0_NV, GL_ALPHA, GL_DISCARD_NV, GL_DISCARD_NV, GL_PRIMARY_COLOR_NV, 0, 0, 0, 0, 0);
1716                 glDisable (GL_PER_STAGE_CONSTANTS_NV);
1717                 glCombinerParameteriNV (GL_COLOR_SUM_CLAMP_NV, 0);
1718                 glFinalCombinerInputNV (GL_VARIABLE_A_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1719                 glFinalCombinerInputNV (GL_VARIABLE_B_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1720                 glFinalCombinerInputNV (GL_VARIABLE_C_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1721                 glFinalCombinerInputNV (GL_VARIABLE_D_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1722                 glFinalCombinerInputNV (GL_VARIABLE_E_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1723                 glFinalCombinerInputNV (GL_VARIABLE_F_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1724                 glFinalCombinerInputNV (GL_VARIABLE_G_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
1725                 */
1726                 /*
1727                 int i;
1728                 for (i = 0; i < Stage; i++) {
1729                   if (GL_SUPPORT(ARB_MULTITEXTURE)) {
1730                     GL_ACTIVETEXTURE(i);
1731                     glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
1732                     checkGLcall("Activate texture..");
1733                   } else if (i>0) {
1734                     FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
1735                   }
1736                 }
1737                 */
1738                 /*
1739                   GL_ACTIVETEXTURE(Stage - 1);
1740                   glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
1741                 */
1742                 /*
1743                 GL_ACTIVETEXTURE(Stage);
1744                 checkGLcall("Activate texture.. to update const color");
1745                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
1746                 checkGLcall("glTexEnv");
1747                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + Stage - 1);
1748                 checkGLcall("glTexEnv");
1749                 glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, (float*)&m[0]);
1750                 checkGLcall("glTexEnv");
1751                 */
1752                 LEAVE_GL();
1753                 return;
1754               }
1755             }
1756
1757           case WINED3DTOP_BUMPENVMAPLUMINANCE:
1758
1759           default:
1760             Handled = FALSE;
1761           }
1762           if (Handled) {
1763             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
1764             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV");
1765
1766             LEAVE_GL();
1767             return;
1768           }
1769         } /* GL_NV_texture_env_combine4 */
1770
1771         Handled = TRUE; /* Again, assume handled */
1772         switch (op) {
1773         case WINED3DTOP_DISABLE: /* Only for alpha */
1774                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1775                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1776                 glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
1777                 checkGLcall("GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT");
1778                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
1779                 checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA");
1780                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1781                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1782                 break;
1783         case WINED3DTOP_SELECTARG1:
1784                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1785                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1786                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1787                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1788                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1789                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1790                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1791                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1792                 break;
1793         case WINED3DTOP_SELECTARG2:
1794                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1795                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1796                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
1797                 checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
1798                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
1799                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
1800                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1801                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1802                 break;
1803         case WINED3DTOP_MODULATE:
1804                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1805                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1806                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1807                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1808                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1809                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1810                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1811                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1812                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1813                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1814                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1815                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1816                 break;
1817         case WINED3DTOP_MODULATE2X:
1818                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1819                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1820                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1821                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1822                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1823                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1824                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1825                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1826                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1827                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1828                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1829                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1830                 break;
1831         case WINED3DTOP_MODULATE4X:
1832                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1833                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1834                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1835                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1836                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1837                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1838                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1839                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1840                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1841                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1842                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
1843                 checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
1844                 break;
1845         case WINED3DTOP_ADD:
1846                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1847                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1848                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1849                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1850                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1851                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1852                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1853                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1854                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1855                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1856                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1857                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1858                 break;
1859         case WINED3DTOP_ADDSIGNED:
1860                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1861                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext((GL_ADD_SIGNED)");
1862                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1863                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1864                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1865                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1866                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1867                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1868                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1869                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1870                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1871                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1872                 break;
1873         case WINED3DTOP_ADDSIGNED2X:
1874                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1875                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1876                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1877                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1878                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1879                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1880                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1881                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1882                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1883                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1884                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1885                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1886                 break;
1887         case WINED3DTOP_SUBTRACT:
1888           if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE)) {
1889                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_SUBTRACT);
1890                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_SUBTRACT)");
1891                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1892                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1893                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1894                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1895                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1896                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1897                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1898                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1899                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1900                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1901           } else {
1902                 FIXME("This version of opengl does not support GL_SUBTRACT\n");
1903           }
1904           break;
1905
1906         case WINED3DTOP_BLENDDIFFUSEALPHA:
1907                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1908                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1909                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1910                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1911                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1912                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1913                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1914                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1915                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1916                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1917                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PRIMARY_COLOR));
1918                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PRIMARY_COLOR");
1919                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1920                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1921                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1922                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1923                 break;
1924         case WINED3DTOP_BLENDTEXTUREALPHA:
1925                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1926                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1927                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1928                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1929                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1930                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1931                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1932                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1933                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1934                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1935                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_TEXTURE);
1936                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_TEXTURE");
1937                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1938                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1939                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1940                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1941                 break;
1942         case WINED3DTOP_BLENDFACTORALPHA:
1943                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1944                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1945                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1946                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1947                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1948                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1949                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1950                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1951                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1952                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1953                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_CONSTANT));
1954                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_CONSTANT");
1955                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1956                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1957                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1958                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1959                 break;
1960         case WINED3DTOP_BLENDCURRENTALPHA:
1961                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1962                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1963                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1964                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1965                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1966                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1967                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1968                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1969                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1970                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1971                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PREVIOUS));
1972                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PREVIOUS");
1973                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1974                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1975                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1976                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1977                 break;
1978         case WINED3DTOP_DOTPRODUCT3:
1979                 if (GL_SUPPORT(ARB_TEXTURE_ENV_DOT3)) {
1980                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB);
1981                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB");
1982                 } else if (GL_SUPPORT(EXT_TEXTURE_ENV_DOT3)) {
1983                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT);
1984                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT");
1985                 } else {
1986                   FIXME("This version of opengl does not support GL_DOT3\n");
1987                 }
1988                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1989                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1990                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1991                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1992                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1993                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1994                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1995                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1996                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1997                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1998                 break;
1999         case WINED3DTOP_LERP:
2000                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
2001                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
2002                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2003                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2004                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2005                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2006                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2007                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2008                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2009                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2010                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src3);
2011                 checkGLcall("GL_TEXTURE_ENV, src2_target, src3");
2012                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr3);
2013                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr3");
2014                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2015                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2016                 break;
2017         case WINED3DTOP_ADDSMOOTH:
2018                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2019                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2020                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2021                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2022                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2023                   switch (opr1) {
2024                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2025                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2026                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2027                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2028                   }
2029                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2030                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2031                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2032                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2033                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2034                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2035                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2036                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2037                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2038                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2039                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2040                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2041                 } else
2042                   Handled = FALSE;
2043                 break;
2044         case WINED3DTOP_BLENDTEXTUREALPHAPM:
2045                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2046                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2047                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2048                   glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_TEXTURE);
2049                   checkGLcall("GL_TEXTURE_ENV, src0_target, GL_TEXTURE");
2050                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_ALPHA);
2051                   checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_APHA");
2052                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2053                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2054                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2055                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2056                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2057                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2058                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2059                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2060                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2061                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2062                 } else
2063                   Handled = FALSE;
2064                 break;
2065         case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2066                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2067                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2068                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2069                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2070                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2071                   switch (opr1) {
2072                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2073                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2074                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2075                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2076                   }
2077                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2078                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2079                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2080                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2081                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2082                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2083                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2084                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2085                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2086                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2087                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2088                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2089                 } else
2090                   Handled = FALSE;
2091                 break;
2092         case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2093                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2094                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2095                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2096                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2097                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2098                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2099                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2100                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2101                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2102                   switch (opr1) {
2103                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2104                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2105                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2106                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2107                   }
2108                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2109                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2110                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2111                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2112                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2113                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2114                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2115                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2116                 } else
2117                   Handled = FALSE;
2118                 break;
2119         case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2120                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2121                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2122                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2123                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2124                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2125                   switch (opr1) {
2126                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2127                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2128                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2129                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2130                   }
2131                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2132                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2133                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2134                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2135                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2136                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2137                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2138                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2139                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2140                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2141                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2142                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2143                 } else
2144                   Handled = FALSE;
2145                 break;
2146         case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2147                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2148                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2149                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2150                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2151                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2152                   switch (opr1) {
2153                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2154                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2155                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2156                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2157                   }
2158                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2159                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2160                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2161                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2162                   switch (opr1) {
2163                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2164                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2165                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2166                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2167                   }
2168                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2169                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2170                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2171                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2172                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2173                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2174                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2175                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2176                 } else
2177                   Handled = FALSE;
2178                 break;
2179         case WINED3DTOP_MULTIPLYADD:
2180                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2181                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2182                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2183                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
2184                   checkGLcall("GL_TEXTURE_ENV, src0_target, src3");
2185                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
2186                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr3");
2187                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2188                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2189                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2190                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2191                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2192                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2193                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2194                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2195                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2196                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2197                 } else
2198                   Handled = FALSE;
2199                 break;
2200         default:
2201                 Handled = FALSE;
2202         }
2203
2204         if (Handled) {
2205           BOOL  combineOK = TRUE;
2206           if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
2207             DWORD op2;
2208
2209             if (isAlpha) {
2210               op2 = This->stateBlock->textureState[Stage][WINED3DTSS_COLOROP];
2211             } else {
2212               op2 = This->stateBlock->textureState[Stage][WINED3DTSS_ALPHAOP];
2213             }
2214
2215             /* Note: If COMBINE4 in effect can't go back to combine! */
2216             switch (op2) {
2217             case WINED3DTOP_ADDSMOOTH:
2218             case WINED3DTOP_BLENDTEXTUREALPHAPM:
2219             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2220             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2221             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2222             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2223             case WINED3DTOP_MULTIPLYADD:
2224               /* Ignore those implemented in both cases */
2225               switch (op) {
2226               case WINED3DTOP_SELECTARG1:
2227               case WINED3DTOP_SELECTARG2:
2228                 combineOK = FALSE;
2229                 Handled   = FALSE;
2230                 break;
2231               default:
2232                 FIXME("Can't use COMBINE4 and COMBINE together, thisop=%s, otherop=%s, isAlpha(%d)\n", debug_d3dtop(op), debug_d3dtop(op2), isAlpha);
2233                 LEAVE_GL();
2234                 return;
2235               }
2236             }
2237           }
2238
2239           if (combineOK) {
2240             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE));
2241             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE)");
2242
2243             LEAVE_GL();
2244             return;
2245           }
2246         }
2247
2248         LEAVE_GL();
2249
2250         /* After all the extensions, if still unhandled, report fixme */
2251         FIXME("Unhandled texture operation %s\n", debug_d3dtop(op));
2252         #undef GLINFO_LOCATION
2253 }
2254 #endif
2255
2256 /* Setup this textures matrix according to the texture flags*/
2257 void set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords)
2258 {
2259     float mat[16];
2260
2261     glMatrixMode(GL_TEXTURE);
2262     checkGLcall("glMatrixMode(GL_TEXTURE)");
2263
2264     if (flags == WINED3DTTFF_DISABLE) {
2265         glLoadIdentity();
2266         checkGLcall("glLoadIdentity()");
2267         return;
2268     }
2269
2270     if (flags == (WINED3DTTFF_COUNT1|WINED3DTTFF_PROJECTED)) {
2271         ERR("Invalid texture transform flags: WINED3DTTFF_COUNT1|WINED3DTTFF_PROJECTED\n");
2272         return;
2273     }
2274
2275     memcpy(mat, smat, 16 * sizeof(float));
2276
2277     switch (flags & ~WINED3DTTFF_PROJECTED) {
2278     case WINED3DTTFF_COUNT1: mat[1] = mat[5] = mat[13] = 0;
2279     case WINED3DTTFF_COUNT2: mat[2] = mat[6] = mat[10] = mat[14] = 0;
2280     default: mat[3] = mat[7] = mat[11] = 0, mat[15] = 1;
2281     }
2282
2283     if (flags & WINED3DTTFF_PROJECTED) {
2284         switch (flags & ~WINED3DTTFF_PROJECTED) {
2285         case WINED3DTTFF_COUNT2:
2286             mat[3] = mat[1], mat[7] = mat[5], mat[11] = mat[9], mat[15] = mat[13];
2287             mat[1] = mat[5] = mat[9] = mat[13] = 0;
2288             break;
2289         case WINED3DTTFF_COUNT3:
2290             mat[3] = mat[2], mat[7] = mat[6], mat[11] = mat[10], mat[15] = mat[14];
2291             mat[2] = mat[6] = mat[10] = mat[14] = 0;
2292             break;
2293         }
2294     } else if(!calculatedCoords) { /* under directx the R/Z coord can be used for translation, under opengl we use the Q coord instead */
2295         mat[12] = mat[8];
2296         mat[13] = mat[9];
2297     }
2298
2299     glLoadMatrixf(mat);
2300     checkGLcall("glLoadMatrixf(mat)");
2301 }
2302
2303 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
2304
2305 /* Convertes a D3D format into a OpenGL configuration format */
2306 int D3DFmtMakeGlCfg(WINED3DFORMAT BackBufferFormat, WINED3DFORMAT StencilBufferFormat, int *attribs, int* nAttribs, BOOL alternate){
2307 #define PUSH1(att)        attribs[(*nAttribs)++] = (att);
2308 #define PUSH2(att,value)  attribs[(*nAttribs)++] = (att); attribs[(*nAttribs)++] = (value);
2309     /*We need to do some Card specific stuff in here at some point,
2310     D3D now supports floating point format buffers, and there are a number of different OpelGl ways of managing these e.g.
2311     GLX_ATI_pixel_format_float
2312     */
2313     switch (BackBufferFormat) {
2314         /* color buffer */
2315     case WINED3DFMT_P8:
2316         PUSH2(GLX_RENDER_TYPE,  GLX_COLOR_INDEX_BIT);
2317         PUSH2(GLX_BUFFER_SIZE,  8);
2318         PUSH2(GLX_DOUBLEBUFFER, TRUE);
2319         break;
2320
2321     case WINED3DFMT_R3G3B2:
2322         PUSH2(GLX_RENDER_TYPE,  GLX_RGBA_BIT);
2323         PUSH2(GLX_RED_SIZE,     3);
2324         PUSH2(GLX_GREEN_SIZE,   3);
2325         PUSH2(GLX_BLUE_SIZE,    2);
2326         break;
2327
2328     case WINED3DFMT_A1R5G5B5:
2329         PUSH2(GLX_ALPHA_SIZE,   1);
2330     case WINED3DFMT_X1R5G5B5:
2331         PUSH2(GLX_RED_SIZE,     5);
2332         PUSH2(GLX_GREEN_SIZE,   5);
2333         PUSH2(GLX_BLUE_SIZE,    5);
2334         break;
2335
2336     case WINED3DFMT_R5G6B5:
2337         PUSH2(GLX_RED_SIZE,     5);
2338         PUSH2(GLX_GREEN_SIZE,   6);
2339         PUSH2(GLX_BLUE_SIZE,    5);
2340         break;
2341
2342     case WINED3DFMT_A4R4G4B4:
2343         PUSH2(GLX_ALPHA_SIZE,   4);
2344     case WINED3DFMT_X4R4G4B4:
2345         PUSH2(GLX_RED_SIZE,     4);
2346         PUSH2(GLX_GREEN_SIZE,   4);
2347         PUSH2(GLX_BLUE_SIZE,    4);
2348         break;
2349
2350     case WINED3DFMT_A8R8G8B8:
2351         PUSH2(GLX_ALPHA_SIZE,   8);
2352     case WINED3DFMT_R8G8B8:
2353     case WINED3DFMT_X8R8G8B8:
2354         PUSH2(GLX_RED_SIZE,     8);
2355         PUSH2(GLX_GREEN_SIZE,   8);
2356         PUSH2(GLX_BLUE_SIZE,    8);
2357         break;
2358
2359     case WINED3DFMT_A2R10G10B10:
2360         PUSH2(GLX_ALPHA_SIZE,   2);
2361         PUSH2(GLX_RED_SIZE,    10);
2362         PUSH2(GLX_GREEN_SIZE,  10);
2363         PUSH2(GLX_BLUE_SIZE,   10);
2364         break;
2365
2366     case WINED3DFMT_A16B16G16R16:        
2367         PUSH2(GLX_ALPHA_SIZE,  16);
2368         PUSH2(GLX_RED_SIZE,    16);
2369         PUSH2(GLX_GREEN_SIZE,  16);
2370         PUSH2(GLX_BLUE_SIZE,   16);
2371         break;
2372         
2373     default:
2374         FIXME("Unsupported color format: %s\n", debug_d3dformat(BackBufferFormat));
2375         break;
2376     }
2377     if(!alternate){
2378         switch (StencilBufferFormat) {
2379     case 0:
2380         break;
2381
2382     case WINED3DFMT_D16_LOCKABLE:
2383     case WINED3DFMT_D16:
2384         PUSH2(GLX_DEPTH_SIZE,   16);
2385         break;
2386
2387     case WINED3DFMT_D15S1:
2388         PUSH2(GLX_DEPTH_SIZE,   15);
2389         PUSH2(GLX_STENCIL_SIZE, 1);
2390         /*Does openGl support a 1bit stencil?, I've seen it used elsewhere
2391         e.g. http://www.ks.uiuc.edu/Research/vmd/doxygen/OpenGLDisplayDevice_8C-source.html*/
2392         break;
2393
2394     case WINED3DFMT_D24X8:
2395         PUSH2(GLX_DEPTH_SIZE,   24);
2396         break;
2397
2398     case WINED3DFMT_D24X4S4:
2399         PUSH2(GLX_DEPTH_SIZE,   24);
2400         PUSH2(GLX_STENCIL_SIZE, 4);
2401         break;
2402
2403     case WINED3DFMT_D24S8:
2404         PUSH2(GLX_DEPTH_SIZE,   24);
2405         PUSH2(GLX_STENCIL_SIZE, 8);
2406         break;
2407
2408     case WINED3DFMT_D24FS8:
2409         PUSH2(GLX_DEPTH_SIZE,   24);
2410         PUSH2(GLX_STENCIL_SIZE, 8);
2411         break;
2412
2413     case WINED3DFMT_D32:
2414         PUSH2(GLX_DEPTH_SIZE,   32);
2415         break;
2416
2417     default:
2418         FIXME("Unsupported stencil format: %s\n", debug_d3dformat(StencilBufferFormat));
2419         break;
2420     }
2421
2422     } else { /* it the device doesn't support the 'exact' format, try to find something close */
2423         switch (StencilBufferFormat) {
2424         case 0:
2425             break;
2426             
2427         case WINED3DFMT_D16_LOCKABLE:
2428         case WINED3DFMT_D16:
2429             PUSH2(GLX_DEPTH_SIZE,   1);
2430             break;
2431
2432         case WINED3DFMT_D15S1:
2433             PUSH2(GLX_DEPTH_SIZE,   1);
2434             PUSH2(GLX_STENCIL_SIZE, 1);
2435             /*Does openGl support a 1bit stencil?, I've seen it used elsewhere
2436             e.g. http://www.ks.uiuc.edu/Research/vmd/doxygen/OpenGLDisplayDevice_8C-source.html*/
2437             break;
2438
2439         case WINED3DFMT_D24X8:
2440             PUSH2(GLX_DEPTH_SIZE,   1);
2441             break;
2442
2443         case WINED3DFMT_D24X4S4:
2444             PUSH2(GLX_DEPTH_SIZE,   1);
2445             PUSH2(GLX_STENCIL_SIZE, 1);
2446             break;
2447
2448         case WINED3DFMT_D24S8:
2449             PUSH2(GLX_DEPTH_SIZE,   1);
2450             PUSH2(GLX_STENCIL_SIZE, 1);
2451             break;
2452
2453         case WINED3DFMT_D24FS8:
2454             PUSH2(GLX_DEPTH_SIZE,   1);
2455             PUSH2(GLX_STENCIL_SIZE, 1);
2456             break;
2457
2458         case WINED3DFMT_D32:
2459             PUSH2(GLX_DEPTH_SIZE,   1);
2460             break;
2461
2462         default:
2463             FIXME("Unsupported stencil format: %s\n", debug_d3dformat(StencilBufferFormat));
2464             break;
2465         }
2466     }
2467
2468     return *nAttribs;
2469 }
2470
2471 #undef GLINFO_LOCATION
2472
2473 /* DirectDraw stuff */
2474 WINED3DFORMAT pixelformat_for_depth(DWORD depth) {
2475     switch(depth) {
2476         case 8:  return WINED3DFMT_P8; break;
2477         case 15: return WINED3DFMT_X1R5G5B5; break;
2478         case 16: return WINED3DFMT_R5G6B5; break;
2479         case 24: return WINED3DFMT_R8G8B8; break;
2480         case 32: return WINED3DFMT_X8R8G8B8; break;
2481         default: return WINED3DFMT_UNKNOWN;
2482     }
2483 }
2484
2485 void multiply_matrix(WINED3DMATRIX *dest, const WINED3DMATRIX *src1, const WINED3DMATRIX *src2) {
2486     WINED3DMATRIX temp;
2487
2488     /* Now do the multiplication 'by hand'.
2489        I know that all this could be optimised, but this will be done later :-) */
2490     temp.u.s._11 = (src1->u.s._11 * src2->u.s._11) + (src1->u.s._21 * src2->u.s._12) + (src1->u.s._31 * src2->u.s._13) + (src1->u.s._41 * src2->u.s._14);
2491     temp.u.s._21 = (src1->u.s._11 * src2->u.s._21) + (src1->u.s._21 * src2->u.s._22) + (src1->u.s._31 * src2->u.s._23) + (src1->u.s._41 * src2->u.s._24);
2492     temp.u.s._31 = (src1->u.s._11 * src2->u.s._31) + (src1->u.s._21 * src2->u.s._32) + (src1->u.s._31 * src2->u.s._33) + (src1->u.s._41 * src2->u.s._34);
2493     temp.u.s._41 = (src1->u.s._11 * src2->u.s._41) + (src1->u.s._21 * src2->u.s._42) + (src1->u.s._31 * src2->u.s._43) + (src1->u.s._41 * src2->u.s._44);
2494
2495     temp.u.s._12 = (src1->u.s._12 * src2->u.s._11) + (src1->u.s._22 * src2->u.s._12) + (src1->u.s._32 * src2->u.s._13) + (src1->u.s._42 * src2->u.s._14);
2496     temp.u.s._22 = (src1->u.s._12 * src2->u.s._21) + (src1->u.s._22 * src2->u.s._22) + (src1->u.s._32 * src2->u.s._23) + (src1->u.s._42 * src2->u.s._24);
2497     temp.u.s._32 = (src1->u.s._12 * src2->u.s._31) + (src1->u.s._22 * src2->u.s._32) + (src1->u.s._32 * src2->u.s._33) + (src1->u.s._42 * src2->u.s._34);
2498     temp.u.s._42 = (src1->u.s._12 * src2->u.s._41) + (src1->u.s._22 * src2->u.s._42) + (src1->u.s._32 * src2->u.s._43) + (src1->u.s._42 * src2->u.s._44);
2499
2500     temp.u.s._13 = (src1->u.s._13 * src2->u.s._11) + (src1->u.s._23 * src2->u.s._12) + (src1->u.s._33 * src2->u.s._13) + (src1->u.s._43 * src2->u.s._14);
2501     temp.u.s._23 = (src1->u.s._13 * src2->u.s._21) + (src1->u.s._23 * src2->u.s._22) + (src1->u.s._33 * src2->u.s._23) + (src1->u.s._43 * src2->u.s._24);
2502     temp.u.s._33 = (src1->u.s._13 * src2->u.s._31) + (src1->u.s._23 * src2->u.s._32) + (src1->u.s._33 * src2->u.s._33) + (src1->u.s._43 * src2->u.s._34);
2503     temp.u.s._43 = (src1->u.s._13 * src2->u.s._41) + (src1->u.s._23 * src2->u.s._42) + (src1->u.s._33 * src2->u.s._43) + (src1->u.s._43 * src2->u.s._44);
2504
2505     temp.u.s._14 = (src1->u.s._14 * src2->u.s._11) + (src1->u.s._24 * src2->u.s._12) + (src1->u.s._34 * src2->u.s._13) + (src1->u.s._44 * src2->u.s._14);
2506     temp.u.s._24 = (src1->u.s._14 * src2->u.s._21) + (src1->u.s._24 * src2->u.s._22) + (src1->u.s._34 * src2->u.s._23) + (src1->u.s._44 * src2->u.s._24);
2507     temp.u.s._34 = (src1->u.s._14 * src2->u.s._31) + (src1->u.s._24 * src2->u.s._32) + (src1->u.s._34 * src2->u.s._33) + (src1->u.s._44 * src2->u.s._34);
2508     temp.u.s._44 = (src1->u.s._14 * src2->u.s._41) + (src1->u.s._24 * src2->u.s._42) + (src1->u.s._34 * src2->u.s._43) + (src1->u.s._44 * src2->u.s._44);
2509
2510     /* And copy the new matrix in the good storage.. */
2511     memcpy(dest, &temp, 16 * sizeof(float));
2512 }
2513
2514 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType) {
2515     DWORD size = 0;
2516     int i;
2517     int numTextures = (d3dvtVertexType & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2518
2519     if (d3dvtVertexType & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2520     if (d3dvtVertexType & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2521     if (d3dvtVertexType & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2522     if (d3dvtVertexType & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2523     switch (d3dvtVertexType & WINED3DFVF_POSITION_MASK) {
2524         case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2525         case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2526         default: TRACE(" matrix weighting not handled yet...\n");
2527     }
2528     for (i = 0; i < numTextures; i++) {
2529         size += GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, i) * sizeof(float);
2530     }
2531
2532     return size;
2533 }
2534
2535 /***********************************************************************
2536  * CalculateTexRect
2537  *
2538  * Calculates the dimensions of the opengl texture used for blits.
2539  * Handled oversized opengl textures and updates the source rectangle
2540  * accordingly
2541  *
2542  * Params:
2543  *  This: Surface to operate on
2544  *  Rect: Requested rectangle
2545  *
2546  * Returns:
2547  *  TRUE if the texture part can be loaded,
2548  *  FALSE otherwise
2549  *
2550  *********************************************************************/
2551 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->resource.wineD3DDevice->wineD3D))->gl_info
2552
2553 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]) {
2554     int x1 = Rect->left, x2 = Rect->right;
2555     int y1 = Rect->top, y2 = Rect->bottom;
2556     GLint maxSize = GL_LIMITS(texture_size);
2557
2558     TRACE("(%p)->(%d,%d)-(%d,%d)\n", This,
2559           Rect->left, Rect->top, Rect->right, Rect->bottom);
2560
2561     /* The sizes might be reversed */
2562     if(Rect->left > Rect->right) {
2563         x1 = Rect->right;
2564         x2 = Rect->left;
2565     }
2566     if(Rect->top > Rect->bottom) {
2567         y1 = Rect->bottom;
2568         y2 = Rect->top;
2569     }
2570
2571     /* No oversized texture? This is easy */
2572     if(!(This->Flags & SFLAG_OVERSIZE)) {
2573         /* Which rect from the texture do I need? */
2574         glTexCoord[0] = (float) Rect->left / (float) This->pow2Width;
2575         glTexCoord[2] = (float) Rect->top / (float) This->pow2Height;
2576         glTexCoord[1] = (float) Rect->right / (float) This->pow2Width;
2577         glTexCoord[3] = (float) Rect->bottom / (float) This->pow2Height;
2578
2579         return TRUE;
2580     } else {
2581         /* Check if we can succeed at all */
2582         if( (x2 - x1) > maxSize ||
2583             (y2 - y1) > maxSize ) {
2584             TRACE("Requested rectangle is too large for gl\n");
2585             return FALSE;
2586         }
2587
2588         /* A part of the texture has to be picked. First, check if
2589          * some texture part is loaded already, if yes try to re-use it.
2590          * If the texture is dirty, or the part can't be used,
2591          * re-position the part to load
2592          */
2593         if(!(This->Flags & SFLAG_DIRTY)) {
2594             if(This->glRect.left <= x1 && This->glRect.right >= x2 &&
2595                This->glRect.top <= y1 && This->glRect.bottom >= x2 ) {
2596                 /* Ok, the rectangle is ok, re-use it */
2597                 TRACE("Using existing gl Texture\n");
2598             } else {
2599                 /* Rectangle is not ok, dirtify the texture to reload it */
2600                 TRACE("Dirtifying texture to force reload\n");
2601                 This->Flags |= SFLAG_DIRTY;
2602             }
2603         }
2604
2605         /* Now if we are dirty(no else if!) */
2606         if(This->Flags & SFLAG_DIRTY) {
2607             /* Set the new rectangle. Use the following strategy:
2608              * 1) Use as big textures as possible.
2609              * 2) Place the texture part in the way that the requested
2610              *    part is in the middle of the texture(well, almost)
2611              * 3) If the texture is moved over the edges of the
2612              *    surface, replace it nicely
2613              * 4) If the coord is not limiting the texture size,
2614              *    use the whole size
2615              */
2616             if((This->pow2Width) > maxSize) {
2617                 This->glRect.left = x1 - maxSize / 2;
2618                 if(This->glRect.left < 0) {
2619                     This->glRect.left = 0;
2620                 }
2621                 This->glRect.right = This->glRect.left + maxSize;
2622                 if(This->glRect.right > This->currentDesc.Width) {
2623                     This->glRect.right = This->currentDesc.Width;
2624                     This->glRect.left = This->glRect.right - maxSize;
2625                 }
2626             } else {
2627                 This->glRect.left = 0;
2628                 This->glRect.right = This->pow2Width;
2629             }
2630
2631             if(This->pow2Height > maxSize) {
2632                 This->glRect.top = x1 - GL_LIMITS(texture_size) / 2;
2633                 if(This->glRect.top < 0) This->glRect.top = 0;
2634                 This->glRect.bottom = This->glRect.left + maxSize;
2635                 if(This->glRect.bottom > This->currentDesc.Height) {
2636                     This->glRect.bottom = This->currentDesc.Height;
2637                     This->glRect.top = This->glRect.bottom - maxSize;
2638                 }
2639             } else {
2640                 This->glRect.top = 0;
2641                 This->glRect.bottom = This->pow2Height;
2642             }
2643             TRACE("(%p): Using rect (%d,%d)-(%d,%d)\n", This,
2644                    This->glRect.left, This->glRect.top, This->glRect.right, This->glRect.bottom);
2645         }
2646
2647         /* Re-calculate the rect to draw */
2648         Rect->left -= This->glRect.left;
2649         Rect->right -= This->glRect.left;
2650         Rect->top -= This->glRect.top;
2651         Rect->bottom -= This->glRect.top;
2652
2653         /* Get the gl coordinates. The gl rectangle is a power of 2, eigher the max size,
2654          * or the pow2Width / pow2Height of the surface
2655          */
2656         glTexCoord[0] = (float) Rect->left / (float) (This->glRect.right - This->glRect.left);
2657         glTexCoord[2] = (float) Rect->top / (float) (This->glRect.bottom - This->glRect.top);
2658         glTexCoord[1] = (float) Rect->right / (float) (This->glRect.right - This->glRect.left);
2659         glTexCoord[3] = (float) Rect->bottom / (float) (This->glRect.bottom - This->glRect.top);
2660     }
2661     return TRUE;
2662 }
2663 #undef GLINFO_LOCATION