wined3d: Add IWineD3DSurface::SetOverlayPosition.
[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_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_SHORT              },
94     {WINED3DFMT_D32         ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_INT                },
95     {WINED3DFMT_D15S1       ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_SHORT              },
96     {WINED3DFMT_D24S8       ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_INT                },
97     {WINED3DFMT_D24X8       ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_INT                },
98     {WINED3DFMT_D24X4S4     ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,GL_UNSIGNED_INT                },
99     {WINED3DFMT_D16         ,0x0        ,0x0        ,0x0        ,0x0        ,2      ,FALSE      ,GL_COLOR_INDEX         ,GL_COLOR_INDEX     ,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      ,0                      ,0                  ,0                              },
102     {WINED3DFMT_D24FS8      ,0x0        ,0x0        ,0x0        ,0x0        ,4      ,FALSE      ,0                      ,0                  ,0                              },
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) D3DFORMAT!\n", fmt, fourcc);
212       else
213         FIXME("Unrecognized %u D3DFORMAT!\n", fmt);
214     }
215     return "unrecognized";
216   }
217 }
218
219 const char* debug_d3ddevicetype(D3DDEVTYPE devtype) {
220   switch (devtype) {
221 #define DEVTYPE_TO_STR(dev) case dev: return #dev
222     DEVTYPE_TO_STR(D3DDEVTYPE_HAL);
223     DEVTYPE_TO_STR(D3DDEVTYPE_REF);
224     DEVTYPE_TO_STR(D3DDEVTYPE_SW);
225 #undef DEVTYPE_TO_STR
226   default:
227     FIXME("Unrecognized %u D3DDEVTYPE!\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(D3DPRIMITIVETYPE PrimitiveType) {
359   switch (PrimitiveType) {
360 #define PRIM_TO_STR(prim) case prim: return #prim;
361     PRIM_TO_STR(D3DPT_POINTLIST);
362     PRIM_TO_STR(D3DPT_LINELIST);
363     PRIM_TO_STR(D3DPT_LINESTRIP);
364     PRIM_TO_STR(D3DPT_TRIANGLELIST);
365     PRIM_TO_STR(D3DPT_TRIANGLESTRIP);
366     PRIM_TO_STR(D3DPT_TRIANGLEFAN);
367 #undef  PRIM_TO_STR
368   default:
369     FIXME("Unrecognized %u D3DPRIMITIVETYPE!\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_WRAP0                     );
437     D3DSTATE_TO_STR(WINED3DRS_WRAP1                     );
438     D3DSTATE_TO_STR(WINED3DRS_WRAP2                     );
439     D3DSTATE_TO_STR(WINED3DRS_WRAP3                     );
440     D3DSTATE_TO_STR(WINED3DRS_WRAP4                     );
441     D3DSTATE_TO_STR(WINED3DRS_WRAP5                     );
442     D3DSTATE_TO_STR(WINED3DRS_WRAP6                     );
443     D3DSTATE_TO_STR(WINED3DRS_WRAP7                     );
444     D3DSTATE_TO_STR(WINED3DRS_CLIPPING                  );
445     D3DSTATE_TO_STR(WINED3DRS_LIGHTING                  );
446     D3DSTATE_TO_STR(WINED3DRS_EXTENTS                   );
447     D3DSTATE_TO_STR(WINED3DRS_AMBIENT                   );
448     D3DSTATE_TO_STR(WINED3DRS_FOGVERTEXMODE             );
449     D3DSTATE_TO_STR(WINED3DRS_COLORVERTEX               );
450     D3DSTATE_TO_STR(WINED3DRS_LOCALVIEWER               );
451     D3DSTATE_TO_STR(WINED3DRS_NORMALIZENORMALS          );
452     D3DSTATE_TO_STR(WINED3DRS_COLORKEYBLENDENABLE       );
453     D3DSTATE_TO_STR(WINED3DRS_DIFFUSEMATERIALSOURCE     );
454     D3DSTATE_TO_STR(WINED3DRS_SPECULARMATERIALSOURCE    );
455     D3DSTATE_TO_STR(WINED3DRS_AMBIENTMATERIALSOURCE     );
456     D3DSTATE_TO_STR(WINED3DRS_EMISSIVEMATERIALSOURCE    );
457     D3DSTATE_TO_STR(WINED3DRS_VERTEXBLEND               );
458     D3DSTATE_TO_STR(WINED3DRS_CLIPPLANEENABLE           );
459     D3DSTATE_TO_STR(WINED3DRS_SOFTWAREVERTEXPROCESSING  );
460     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE                 );
461     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MIN             );
462     D3DSTATE_TO_STR(WINED3DRS_POINTSPRITEENABLE         );
463     D3DSTATE_TO_STR(WINED3DRS_POINTSCALEENABLE          );
464     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_A              );
465     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_B              );
466     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_C              );
467     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEANTIALIAS      );
468     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEMASK           );
469     D3DSTATE_TO_STR(WINED3DRS_PATCHEDGESTYLE            );
470     D3DSTATE_TO_STR(WINED3DRS_PATCHSEGMENTS             );
471     D3DSTATE_TO_STR(WINED3DRS_DEBUGMONITORTOKEN         );
472     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MAX             );
473     D3DSTATE_TO_STR(WINED3DRS_INDEXEDVERTEXBLENDENABLE  );
474     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE          );
475     D3DSTATE_TO_STR(WINED3DRS_TWEENFACTOR               );
476     D3DSTATE_TO_STR(WINED3DRS_BLENDOP                   );
477     D3DSTATE_TO_STR(WINED3DRS_POSITIONDEGREE            );
478     D3DSTATE_TO_STR(WINED3DRS_NORMALDEGREE              );
479     D3DSTATE_TO_STR(WINED3DRS_SCISSORTESTENABLE         );
480     D3DSTATE_TO_STR(WINED3DRS_SLOPESCALEDEPTHBIAS       );
481     D3DSTATE_TO_STR(WINED3DRS_ANTIALIASEDLINEENABLE     );
482     D3DSTATE_TO_STR(WINED3DRS_MINTESSELLATIONLEVEL      );
483     D3DSTATE_TO_STR(WINED3DRS_MAXTESSELLATIONLEVEL      );
484     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_X            );
485     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Y            );
486     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Z            );
487     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_W            );
488     D3DSTATE_TO_STR(WINED3DRS_ENABLEADAPTIVETESSELLATION);
489     D3DSTATE_TO_STR(WINED3DRS_TWOSIDEDSTENCILMODE       );
490     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFAIL           );
491     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILZFAIL          );
492     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILPASS           );
493     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFUNC           );
494     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE1         );
495     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE2         );
496     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE3         );
497     D3DSTATE_TO_STR(WINED3DRS_BLENDFACTOR               );
498     D3DSTATE_TO_STR(WINED3DRS_SRGBWRITEENABLE           );
499     D3DSTATE_TO_STR(WINED3DRS_DEPTHBIAS                 );
500     D3DSTATE_TO_STR(WINED3DRS_WRAP8                     );
501     D3DSTATE_TO_STR(WINED3DRS_WRAP9                     );
502     D3DSTATE_TO_STR(WINED3DRS_WRAP10                    );
503     D3DSTATE_TO_STR(WINED3DRS_WRAP11                    );
504     D3DSTATE_TO_STR(WINED3DRS_WRAP12                    );
505     D3DSTATE_TO_STR(WINED3DRS_WRAP13                    );
506     D3DSTATE_TO_STR(WINED3DRS_WRAP14                    );
507     D3DSTATE_TO_STR(WINED3DRS_WRAP15                    );
508     D3DSTATE_TO_STR(WINED3DRS_SEPARATEALPHABLENDENABLE  );
509     D3DSTATE_TO_STR(WINED3DRS_SRCBLENDALPHA             );
510     D3DSTATE_TO_STR(WINED3DRS_DESTBLENDALPHA            );
511     D3DSTATE_TO_STR(WINED3DRS_BLENDOPALPHA              );
512 #undef D3DSTATE_TO_STR
513   default:
514     FIXME("Unrecognized %u render state!\n", state);
515     return "unrecognized";
516   }
517 }
518
519 const char* debug_d3dsamplerstate(DWORD state) {
520   switch (state) {
521 #define D3DSTATE_TO_STR(u) case u: return #u
522     D3DSTATE_TO_STR(WINED3DSAMP_BORDERCOLOR  );
523     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSU     );
524     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSV     );
525     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSW     );
526     D3DSTATE_TO_STR(WINED3DSAMP_MAGFILTER    );
527     D3DSTATE_TO_STR(WINED3DSAMP_MINFILTER    );
528     D3DSTATE_TO_STR(WINED3DSAMP_MIPFILTER    );
529     D3DSTATE_TO_STR(WINED3DSAMP_MIPMAPLODBIAS);
530     D3DSTATE_TO_STR(WINED3DSAMP_MAXMIPLEVEL  );
531     D3DSTATE_TO_STR(WINED3DSAMP_MAXANISOTROPY);
532     D3DSTATE_TO_STR(WINED3DSAMP_SRGBTEXTURE  );
533     D3DSTATE_TO_STR(WINED3DSAMP_ELEMENTINDEX );
534     D3DSTATE_TO_STR(WINED3DSAMP_DMAPOFFSET   );
535 #undef D3DSTATE_TO_STR
536   default:
537     FIXME("Unrecognized %u sampler state!\n", state);
538     return "unrecognized";
539   }
540 }
541
542 const char* debug_d3dtexturestate(DWORD state) {
543   switch (state) {
544 #define D3DSTATE_TO_STR(u) case u: return #u
545     D3DSTATE_TO_STR(WINED3DTSS_COLOROP               );
546     D3DSTATE_TO_STR(WINED3DTSS_COLORARG1             );
547     D3DSTATE_TO_STR(WINED3DTSS_COLORARG2             );
548     D3DSTATE_TO_STR(WINED3DTSS_ALPHAOP               );
549     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG1             );
550     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG2             );
551     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT00          );
552     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT01          );
553     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT10          );
554     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT11          );
555     D3DSTATE_TO_STR(WINED3DTSS_TEXCOORDINDEX         );
556     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLSCALE         );
557     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLOFFSET        );
558     D3DSTATE_TO_STR(WINED3DTSS_TEXTURETRANSFORMFLAGS );
559     D3DSTATE_TO_STR(WINED3DTSS_ADDRESSW              );
560     D3DSTATE_TO_STR(WINED3DTSS_COLORARG0             );
561     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG0             );
562     D3DSTATE_TO_STR(WINED3DTSS_RESULTARG             );
563     D3DSTATE_TO_STR(WINED3DTSS_CONSTANT              );
564 #undef D3DSTATE_TO_STR
565   case 12:
566     /* Note D3DTSS are not consecutive, so skip these */
567     return "unused";
568     break;
569   default:
570     FIXME("Unrecognized %u texture state!\n", state);
571     return "unrecognized";
572   }
573 }
574
575 const char* debug_d3dtop(D3DTEXTUREOP d3dtop) {
576     switch (d3dtop) {
577 #define D3DTOP_TO_STR(u) case u: return #u
578         D3DTOP_TO_STR(D3DTOP_DISABLE);
579         D3DTOP_TO_STR(D3DTOP_SELECTARG1);
580         D3DTOP_TO_STR(D3DTOP_SELECTARG2);
581         D3DTOP_TO_STR(D3DTOP_MODULATE);
582         D3DTOP_TO_STR(D3DTOP_MODULATE2X);
583         D3DTOP_TO_STR(D3DTOP_MODULATE4X);
584         D3DTOP_TO_STR(D3DTOP_ADD);
585         D3DTOP_TO_STR(D3DTOP_ADDSIGNED);
586         D3DTOP_TO_STR(D3DTOP_SUBTRACT);
587         D3DTOP_TO_STR(D3DTOP_ADDSMOOTH);
588         D3DTOP_TO_STR(D3DTOP_BLENDDIFFUSEALPHA);
589         D3DTOP_TO_STR(D3DTOP_BLENDTEXTUREALPHA);
590         D3DTOP_TO_STR(D3DTOP_BLENDFACTORALPHA);
591         D3DTOP_TO_STR(D3DTOP_BLENDTEXTUREALPHAPM);
592         D3DTOP_TO_STR(D3DTOP_BLENDCURRENTALPHA);
593         D3DTOP_TO_STR(D3DTOP_PREMODULATE);
594         D3DTOP_TO_STR(D3DTOP_MODULATEALPHA_ADDCOLOR);
595         D3DTOP_TO_STR(D3DTOP_MODULATECOLOR_ADDALPHA);
596         D3DTOP_TO_STR(D3DTOP_MODULATEINVALPHA_ADDCOLOR);
597         D3DTOP_TO_STR(D3DTOP_MODULATEINVCOLOR_ADDALPHA);
598         D3DTOP_TO_STR(D3DTOP_BUMPENVMAP);
599         D3DTOP_TO_STR(D3DTOP_BUMPENVMAPLUMINANCE);
600         D3DTOP_TO_STR(D3DTOP_DOTPRODUCT3);
601         D3DTOP_TO_STR(D3DTOP_MULTIPLYADD);
602         D3DTOP_TO_STR(D3DTOP_LERP);
603 #undef D3DTOP_TO_STR
604         default:
605             FIXME("Unrecognized %u D3DTOP\n", d3dtop);
606             return "unrecognized";
607     }
608 }
609
610 const char* debug_d3dtstype(WINED3DTRANSFORMSTATETYPE tstype) {
611     switch (tstype) {
612 #define TSTYPE_TO_STR(tstype) case tstype: return #tstype
613     TSTYPE_TO_STR(WINED3DTS_VIEW);
614     TSTYPE_TO_STR(WINED3DTS_PROJECTION);
615     TSTYPE_TO_STR(WINED3DTS_TEXTURE0);
616     TSTYPE_TO_STR(WINED3DTS_TEXTURE1);
617     TSTYPE_TO_STR(WINED3DTS_TEXTURE2);
618     TSTYPE_TO_STR(WINED3DTS_TEXTURE3);
619     TSTYPE_TO_STR(WINED3DTS_TEXTURE4);
620     TSTYPE_TO_STR(WINED3DTS_TEXTURE5);
621     TSTYPE_TO_STR(WINED3DTS_TEXTURE6);
622     TSTYPE_TO_STR(WINED3DTS_TEXTURE7);
623     TSTYPE_TO_STR(WINED3DTS_WORLDMATRIX(0));
624 #undef TSTYPE_TO_STR
625     default:
626         if (tstype > 256 && tstype < 512) {
627             FIXME("WINED3DTS_WORLDMATRIX(%u). 1..255 not currently supported\n", tstype);
628             return ("WINED3DTS_WORLDMATRIX > 0");
629         }
630         FIXME("Unrecognized %u WINED3DTS\n", tstype);
631         return "unrecognized";
632     }
633 }
634
635 const char* debug_d3dpool(WINED3DPOOL Pool) {
636   switch (Pool) {
637 #define POOL_TO_STR(p) case p: return #p;
638     POOL_TO_STR(WINED3DPOOL_DEFAULT);
639     POOL_TO_STR(WINED3DPOOL_MANAGED);
640     POOL_TO_STR(WINED3DPOOL_SYSTEMMEM);
641     POOL_TO_STR(WINED3DPOOL_SCRATCH);
642 #undef  POOL_TO_STR
643   default:
644     FIXME("Unrecognized %u WINED3DPOOL!\n", Pool);
645     return "unrecognized";
646   }
647 }
648
649 /*****************************************************************************
650  * Useful functions mapping GL <-> D3D values
651  */
652 GLenum StencilOp(DWORD op) {
653     switch(op) {
654     case D3DSTENCILOP_KEEP    : return GL_KEEP;
655     case D3DSTENCILOP_ZERO    : return GL_ZERO;
656     case D3DSTENCILOP_REPLACE : return GL_REPLACE;
657     case D3DSTENCILOP_INCRSAT : return GL_INCR;
658     case D3DSTENCILOP_DECRSAT : return GL_DECR;
659     case D3DSTENCILOP_INVERT  : return GL_INVERT;
660     case D3DSTENCILOP_INCR    : return GL_INCR_WRAP_EXT;
661     case D3DSTENCILOP_DECR    : return GL_DECR_WRAP_EXT;
662     default:
663         FIXME("Unrecognized stencil op %d\n", op);
664         return GL_KEEP;
665     }
666 }
667
668 GLenum CompareFunc(DWORD func) {
669     switch ((D3DCMPFUNC)func) {
670     case D3DCMP_NEVER        : return GL_NEVER;
671     case D3DCMP_LESS         : return GL_LESS;
672     case D3DCMP_EQUAL        : return GL_EQUAL;
673     case D3DCMP_LESSEQUAL    : return GL_LEQUAL;
674     case D3DCMP_GREATER      : return GL_GREATER;
675     case D3DCMP_NOTEQUAL     : return GL_NOTEQUAL;
676     case D3DCMP_GREATEREQUAL : return GL_GEQUAL;
677     case D3DCMP_ALWAYS       : return GL_ALWAYS;
678     default:
679         FIXME("Unrecognized D3DCMPFUNC value %d\n", func);
680         return 0;
681     }
682 }
683
684 static GLenum d3dta_to_combiner_input(DWORD d3dta, DWORD stage, INT texture_idx) {
685     switch (d3dta) {
686         case D3DTA_DIFFUSE:
687             return GL_PRIMARY_COLOR_NV;
688
689         case D3DTA_CURRENT:
690             if (stage) return GL_SPARE0_NV;
691             else return GL_PRIMARY_COLOR_NV;
692
693         case D3DTA_TEXTURE:
694             if (texture_idx > -1) return GL_TEXTURE0_ARB + texture_idx;
695             else return GL_PRIMARY_COLOR_NV;
696
697         case D3DTA_TFACTOR:
698             return GL_CONSTANT_COLOR0_NV;
699
700         case D3DTA_SPECULAR:
701             return GL_SECONDARY_COLOR_NV;
702
703         case D3DTA_TEMP:
704             /* TODO: Support D3DTSS_RESULTARG */
705             FIXME("D3DTA_TEMP, not properly supported.\n");
706             return GL_SPARE1_NV;
707
708         case D3DTA_CONSTANT:
709             /* TODO: Support per stage constants (D3DTSS_CONSTANT, NV_register_combiners2) */
710             FIXME("D3DTA_CONSTANT, not properly supported.\n");
711             return GL_CONSTANT_COLOR1_NV;
712
713         default:
714             FIXME("Unrecognized texture arg %#x\n", d3dta);
715             return GL_TEXTURE;
716     }
717 }
718
719 static GLenum invert_mapping(GLenum mapping) {
720     if (mapping == GL_UNSIGNED_INVERT_NV) return GL_SIGNED_IDENTITY_NV;
721     else if (mapping == GL_SIGNED_IDENTITY_NV) return GL_UNSIGNED_INVERT_NV;
722
723     FIXME("Unhandled mapping %#x\n", mapping);
724     return mapping;
725 }
726
727 static void get_src_and_opr_nvrc(DWORD stage, DWORD arg, BOOL is_alpha, GLenum* input, GLenum* mapping, GLenum *component_usage, INT texture_idx) {
728     /* The D3DTA_COMPLEMENT flag specifies the complement of the input should
729      * be used. */
730     if (arg & D3DTA_COMPLEMENT) *mapping = GL_UNSIGNED_INVERT_NV;
731     else *mapping = GL_SIGNED_IDENTITY_NV;
732
733     /* The D3DTA_ALPHAREPLICATE flag specifies the alpha component of the input
734      * should be used for all input components. */
735     if (is_alpha || arg & D3DTA_ALPHAREPLICATE) *component_usage = GL_ALPHA;
736     else *component_usage = GL_RGB;
737
738     *input = d3dta_to_combiner_input(arg & D3DTA_SELECTMASK, stage, texture_idx);
739 }
740
741 typedef struct {
742     GLenum input[3];
743     GLenum mapping[3];
744     GLenum component_usage[3];
745 } tex_op_args;
746
747 static BOOL is_invalid_op(IWineD3DDeviceImpl *This, int stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3) {
748     if (op == D3DTOP_DISABLE) return FALSE;
749     if (This->stateBlock->textures[stage]) return FALSE;
750
751     if (arg1 == D3DTA_TEXTURE && op != D3DTOP_SELECTARG2) return TRUE;
752     if (arg2 == D3DTA_TEXTURE && op != D3DTOP_SELECTARG1) return TRUE;
753     if (arg3 == D3DTA_TEXTURE && (op == D3DTOP_MULTIPLYADD || op == D3DTOP_LERP)) return TRUE;
754
755     return FALSE;
756 }
757
758 void set_tex_op_nvrc(IWineD3DDevice *iface, BOOL is_alpha, int stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3, INT texture_idx) {
759     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl*)iface;
760     tex_op_args tex_op_args = {{0}, {0}, {0}};
761     GLenum portion = is_alpha ? GL_ALPHA : GL_RGB;
762     GLenum target = GL_COMBINER0_NV + stage;
763
764     TRACE("stage %d, is_alpha %d, op %s, arg1 %#x, arg2 %#x, arg3 %#x, texture_idx %d\n",
765             stage, is_alpha, debug_d3dtop(op), arg1, arg2, arg3, texture_idx);
766
767     /* If a texture stage references an invalid texture unit the stage just
768      * passes through the result from the previous stage */
769     if (is_invalid_op(This, stage, op, arg1, arg2, arg3)) {
770         arg1 = D3DTA_CURRENT;
771         op = D3DTOP_SELECTARG1;
772     }
773
774     get_src_and_opr_nvrc(stage, arg1, is_alpha, &tex_op_args.input[0],
775             &tex_op_args.mapping[0], &tex_op_args.component_usage[0], texture_idx);
776     get_src_and_opr_nvrc(stage, arg2, is_alpha, &tex_op_args.input[1],
777             &tex_op_args.mapping[1], &tex_op_args.component_usage[1], texture_idx);
778     get_src_and_opr_nvrc(stage, arg3, is_alpha, &tex_op_args.input[2],
779             &tex_op_args.mapping[2], &tex_op_args.component_usage[2], texture_idx);
780
781     ENTER_GL();
782
783     switch(op)
784     {
785         case D3DTOP_DISABLE:
786             /* Only for alpha */
787             if (!is_alpha) ERR("Shouldn't be called for D3DTSS_COLOROP (D3DTOP_DISABLE)\n");
788             /* Input, prev_alpha*1 */
789             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
790                     GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA));
791             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
792                     GL_ZERO, GL_UNSIGNED_INVERT_NV, GL_ALPHA));
793
794             /* Output */
795             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
796                     GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
797             break;
798
799         case D3DTOP_SELECTARG1:
800         case D3DTOP_SELECTARG2:
801             /* Input, arg*1 */
802             if (op == D3DTOP_SELECTARG1) {
803                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
804                         tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
805             } else {
806                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
807                         tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
808             }
809             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
810                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
811
812             /* Output */
813             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
814                     GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
815             break;
816
817         case D3DTOP_MODULATE:
818         case D3DTOP_MODULATE2X:
819         case D3DTOP_MODULATE4X:
820             /* Input, arg1*arg2 */
821             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
822                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
823             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
824                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
825
826             /* Output */
827             if (op == D3DTOP_MODULATE) {
828                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
829                         GL_DISCARD_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
830             } else if (op == D3DTOP_MODULATE2X) {
831                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
832                         GL_DISCARD_NV, GL_SCALE_BY_TWO_NV, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
833             } else if (op == D3DTOP_MODULATE4X) {
834                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
835                         GL_DISCARD_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
836             }
837             break;
838
839         case D3DTOP_ADD:
840         case D3DTOP_ADDSIGNED:
841         case D3DTOP_ADDSIGNED2X:
842             /* Input, arg1*1+arg2*1 */
843             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
844                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
845             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
846                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
847             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
848                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
849             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
850                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
851
852             /* Output */
853             if (op == D3DTOP_ADD) {
854                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
855                         GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
856             } else if (op == D3DTOP_ADDSIGNED) {
857                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
858                         GL_SPARE0_NV, GL_NONE, GL_BIAS_BY_NEGATIVE_ONE_HALF_NV, GL_FALSE, GL_FALSE, GL_FALSE));
859             } else if (op == D3DTOP_ADDSIGNED2X) {
860                 GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
861                         GL_SPARE0_NV, GL_SCALE_BY_TWO_NV, GL_BIAS_BY_NEGATIVE_ONE_HALF_NV, GL_FALSE, GL_FALSE, GL_FALSE));
862             }
863             break;
864
865         case D3DTOP_SUBTRACT:
866             /* Input, arg1*1+-arg2*1 */
867             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
868                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
869             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
870                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
871             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
872                     tex_op_args.input[1], GL_SIGNED_NEGATE_NV, tex_op_args.component_usage[1]));
873             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
874                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
875
876             /* Output */
877             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
878                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
879             break;
880
881         case D3DTOP_ADDSMOOTH:
882             /* Input, arg1*1+(1-arg1)*arg2 */
883             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
884                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
885             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
886                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
887             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
888                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
889             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
890                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
891
892             /* Output */
893             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
894                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
895             break;
896
897         case D3DTOP_BLENDDIFFUSEALPHA:
898         case D3DTOP_BLENDTEXTUREALPHA:
899         case D3DTOP_BLENDFACTORALPHA:
900         case D3DTOP_BLENDTEXTUREALPHAPM:
901         case D3DTOP_BLENDCURRENTALPHA:
902         {
903             GLenum alpha_src = GL_PRIMARY_COLOR_NV;
904             if (op == D3DTOP_BLENDDIFFUSEALPHA) alpha_src = d3dta_to_combiner_input(D3DTA_DIFFUSE, stage, texture_idx);
905             else if (op == D3DTOP_BLENDTEXTUREALPHA) alpha_src = d3dta_to_combiner_input(D3DTA_TEXTURE, stage, texture_idx);
906             else if (op == D3DTOP_BLENDFACTORALPHA) alpha_src = d3dta_to_combiner_input(D3DTA_TFACTOR, stage, texture_idx);
907             else if (op == D3DTOP_BLENDTEXTUREALPHAPM) alpha_src = d3dta_to_combiner_input(D3DTA_TEXTURE, stage, texture_idx);
908             else if (op == D3DTOP_BLENDCURRENTALPHA) alpha_src = d3dta_to_combiner_input(D3DTA_CURRENT, stage, texture_idx);
909             else FIXME("Unhandled D3DTOP %s, shouldn't happen\n", debug_d3dtop(op));
910
911             /* Input, arg1*alpha_src+arg2*(1-alpha_src) */
912             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
913                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
914             if (op == D3DTOP_BLENDTEXTUREALPHAPM)
915             {
916                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
917                         GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
918             } else {
919                 GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
920                         alpha_src, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA));
921             }
922             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
923                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
924             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
925                     alpha_src, GL_UNSIGNED_INVERT_NV, GL_ALPHA));
926
927             /* Output */
928             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
929                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
930             break;
931         }
932
933         case D3DTOP_MODULATEALPHA_ADDCOLOR:
934             /* Input, arg1_alpha*arg2_rgb+arg1_rgb*1 */
935             if (is_alpha) ERR("Only supported for D3DTSS_COLOROP (D3DTOP_MODULATEALPHA_ADDCOLOR)\n");
936             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
937                     tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
938             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
939                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
940             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
941                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
942             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
943                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
944
945             /* Output */
946             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
947                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
948             break;
949
950         case D3DTOP_MODULATECOLOR_ADDALPHA:
951             /* Input, arg1_rgb*arg2_rgb+arg1_alpha*1 */
952             if (is_alpha) ERR("Only supported for D3DTSS_COLOROP (D3DTOP_MODULATECOLOR_ADDALPHA)\n");
953             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
954                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
955             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
956                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
957             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
958                     tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
959             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
960                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
961
962             /* Output */
963             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
964                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
965             break;
966
967         case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
968             /* Input, (1-arg1_alpha)*arg2_rgb+arg1_rgb*1 */
969             if (is_alpha) ERR("Only supported for D3DTSS_COLOROP (D3DTOP_MODULATEINVALPHA_ADDCOLOR)\n");
970             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
971                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), GL_ALPHA));
972             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
973                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
974             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
975                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
976             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
977                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
978
979             /* Output */
980             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
981                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
982             break;
983
984         case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
985             /* Input, (1-arg1_rgb)*arg2_rgb+arg1_alpha*1 */
986             if (is_alpha) ERR("Only supported for D3DTSS_COLOROP (D3DTOP_MODULATEINVCOLOR_ADDALPHA)\n");
987             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
988                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
989             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
990                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
991             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
992                     tex_op_args.input[0], tex_op_args.mapping[0], GL_ALPHA));
993             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
994                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
995
996             /* Output */
997             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
998                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
999             break;
1000
1001         case D3DTOP_DOTPRODUCT3:
1002             /* Input, arg1 . arg2 */
1003             /* FIXME: DX7 uses a different calculation? */
1004             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1005                     tex_op_args.input[0], GL_EXPAND_NORMAL_NV, tex_op_args.component_usage[0]));
1006             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1007                     tex_op_args.input[1], GL_EXPAND_NORMAL_NV, tex_op_args.component_usage[1]));
1008
1009             /* Output */
1010             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_SPARE0_NV, GL_DISCARD_NV,
1011                     GL_DISCARD_NV, GL_NONE, GL_NONE, GL_TRUE, GL_FALSE, GL_FALSE));
1012             break;
1013
1014         case D3DTOP_MULTIPLYADD:
1015             /* Input, arg1*1+arg2*arg3 */
1016             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1017                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
1018             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1019                     GL_ZERO, GL_UNSIGNED_INVERT_NV, portion));
1020             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
1021                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
1022             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
1023                     tex_op_args.input[2], tex_op_args.mapping[2], tex_op_args.component_usage[2]));
1024
1025             /* Output */
1026             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
1027                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
1028             break;
1029
1030         case D3DTOP_LERP:
1031             /* Input, arg1*arg2+(1-arg1)*arg3 */
1032             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_A_NV,
1033                     tex_op_args.input[0], tex_op_args.mapping[0], tex_op_args.component_usage[0]));
1034             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_B_NV,
1035                     tex_op_args.input[1], tex_op_args.mapping[1], tex_op_args.component_usage[1]));
1036             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_C_NV,
1037                     tex_op_args.input[0], invert_mapping(tex_op_args.mapping[0]), tex_op_args.component_usage[0]));
1038             GL_EXTCALL(glCombinerInputNV(target, portion, GL_VARIABLE_D_NV,
1039                     tex_op_args.input[2], tex_op_args.mapping[2], tex_op_args.component_usage[2]));
1040
1041             /* Output */
1042             GL_EXTCALL(glCombinerOutputNV(target, portion, GL_DISCARD_NV, GL_DISCARD_NV,
1043                     GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE, GL_FALSE, GL_FALSE));
1044             break;
1045
1046         default:
1047             FIXME("Unhandled D3DTOP: stage %d, is_alpha %d, op %s (%#x), arg1 %#x, arg2 %#x, arg3 %#x, texture_idx %d\n",
1048                     stage, is_alpha, debug_d3dtop(op), op, arg1, arg2, arg3, texture_idx);
1049     }
1050
1051     checkGLcall("set_tex_op_nvrc()\n");
1052
1053     LEAVE_GL();
1054 }
1055
1056 static void get_src_and_opr(DWORD arg, BOOL is_alpha, GLenum* source, GLenum* operand) {
1057     /* The D3DTA_ALPHAREPLICATE flag specifies the alpha component of the
1058      * input should be used for all input components. The D3DTA_COMPLEMENT
1059      * flag specifies the complement of the input should be used. */
1060     BOOL from_alpha = is_alpha || arg & D3DTA_ALPHAREPLICATE;
1061     BOOL complement = arg & D3DTA_COMPLEMENT;
1062
1063     /* Calculate the operand */
1064     if (complement) {
1065         if (from_alpha) *operand = GL_ONE_MINUS_SRC_ALPHA;
1066         else *operand = GL_ONE_MINUS_SRC_COLOR;
1067     } else {
1068         if (from_alpha) *operand = GL_SRC_ALPHA;
1069         else *operand = GL_SRC_COLOR;
1070     }
1071
1072     /* Calculate the source */
1073     switch (arg & D3DTA_SELECTMASK) {
1074         case D3DTA_CURRENT: *source = GL_PREVIOUS_EXT; break;
1075         case D3DTA_DIFFUSE: *source = GL_PRIMARY_COLOR_EXT; break;
1076         case D3DTA_TEXTURE: *source = GL_TEXTURE; break;
1077         case D3DTA_TFACTOR: *source = GL_CONSTANT_EXT; break;
1078         case D3DTA_SPECULAR:
1079             /*
1080              * According to the GL_ARB_texture_env_combine specs, SPECULAR is
1081              * 'Secondary color' and isn't supported until base GL supports it
1082              * There is no concept of temp registers as far as I can tell
1083              */
1084             FIXME("Unhandled texture arg D3DTA_SPECULAR\n");
1085             *source = GL_TEXTURE;
1086             break;
1087         default:
1088             FIXME("Unrecognized texture arg %#x\n", arg);
1089             *source = GL_TEXTURE;
1090             break;
1091     }
1092 }
1093
1094 /* Set texture operations up - The following avoids lots of ifdefs in this routine!*/
1095 #if defined (GL_VERSION_1_3)
1096 # define useext(A) A
1097 # define combine_ext 1
1098 #elif defined (GL_EXT_texture_env_combine)
1099 # define useext(A) A##_EXT
1100 # define combine_ext 1
1101 #elif defined (GL_ARB_texture_env_combine)
1102 # define useext(A) A##_ARB
1103 # define combine_ext 1
1104 #else
1105 # undef combine_ext
1106 #endif
1107
1108 #if !defined(combine_ext)
1109 void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
1110 {
1111         FIXME("Requires opengl combine extensions to work\n");
1112         return;
1113 }
1114 #else
1115 /* Setup the texture operations texture stage states */
1116 void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
1117 {
1118 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
1119         GLenum src1, src2, src3;
1120         GLenum opr1, opr2, opr3;
1121         GLenum comb_target;
1122         GLenum src0_target, src1_target, src2_target;
1123         GLenum opr0_target, opr1_target, opr2_target;
1124         GLenum scal_target;
1125         GLenum opr=0, invopr, src3_target, opr3_target;
1126         BOOL Handled = FALSE;
1127         IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1128
1129         TRACE("Alpha?(%d), Stage:%d Op(%s), a1(%d), a2(%d), a3(%d)\n", isAlpha, Stage, debug_d3dtop(op), arg1, arg2, arg3);
1130
1131         ENTER_GL();
1132
1133         /* Note: Operations usually involve two ars, src0 and src1 and are operations of
1134            the form (a1 <operation> a2). However, some of the more complex operations
1135            take 3 parameters. Instead of the (sensible) addition of a3, Microsoft added
1136            in a third parameter called a0. Therefore these are operations of the form
1137            a0 <operation> a1 <operation> a2, ie the new parameter goes to the front.
1138
1139            However, below we treat the new (a0) parameter as src2/opr2, so in the actual
1140            functions below, expect their syntax to differ slightly to those listed in the
1141            manuals, ie replace arg1 with arg3, arg2 with arg1 and arg3 with arg2
1142            This affects D3DTOP_MULTIPLYADD and D3DTOP_LERP                               */
1143
1144         if (isAlpha) {
1145                 comb_target = useext(GL_COMBINE_ALPHA);
1146                 src0_target = useext(GL_SOURCE0_ALPHA);
1147                 src1_target = useext(GL_SOURCE1_ALPHA);
1148                 src2_target = useext(GL_SOURCE2_ALPHA);
1149                 opr0_target = useext(GL_OPERAND0_ALPHA);
1150                 opr1_target = useext(GL_OPERAND1_ALPHA);
1151                 opr2_target = useext(GL_OPERAND2_ALPHA);
1152                 scal_target = GL_ALPHA_SCALE;
1153         }
1154         else {
1155                 comb_target = useext(GL_COMBINE_RGB);
1156                 src0_target = useext(GL_SOURCE0_RGB);
1157                 src1_target = useext(GL_SOURCE1_RGB);
1158                 src2_target = useext(GL_SOURCE2_RGB);
1159                 opr0_target = useext(GL_OPERAND0_RGB);
1160                 opr1_target = useext(GL_OPERAND1_RGB);
1161                 opr2_target = useext(GL_OPERAND2_RGB);
1162                 scal_target = useext(GL_RGB_SCALE);
1163         }
1164
1165         /* If a texture stage references an invalid texture unit the stage just
1166          * passes through the result from the previous stage */
1167         if (is_invalid_op(This, Stage, op, arg1, arg2, arg3)) {
1168             arg1 = D3DTA_CURRENT;
1169             op = D3DTOP_SELECTARG1;
1170         }
1171
1172         /* From MSDN (WINED3DTSS_ALPHAARG1) :
1173            The default argument is D3DTA_TEXTURE. If no texture is set for this stage,
1174                    then the default argument is D3DTA_DIFFUSE.
1175                    FIXME? If texture added/removed, may need to reset back as well?    */
1176         if (isAlpha && This->stateBlock->textures[Stage] == NULL && arg1 == D3DTA_TEXTURE) {
1177             get_src_and_opr(D3DTA_DIFFUSE, isAlpha, &src1, &opr1);
1178         } else {
1179             get_src_and_opr(arg1, isAlpha, &src1, &opr1);
1180         }
1181         get_src_and_opr(arg2, isAlpha, &src2, &opr2);
1182         get_src_and_opr(arg3, isAlpha, &src3, &opr3);
1183
1184         TRACE("ct(%x), 1:(%x,%x), 2:(%x,%x), 3:(%x,%x)\n", comb_target, src1, opr1, src2, opr2, src3, opr3);
1185
1186         Handled = TRUE; /* Assume will be handled */
1187
1188         /* Other texture operations require special extensions: */
1189         if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1190           if (isAlpha) {
1191             opr = GL_SRC_ALPHA;
1192             invopr = GL_ONE_MINUS_SRC_ALPHA;
1193             src3_target = GL_SOURCE3_ALPHA_NV;
1194             opr3_target = GL_OPERAND3_ALPHA_NV;
1195           } else {
1196             opr = GL_SRC_COLOR;
1197             invopr = GL_ONE_MINUS_SRC_COLOR;
1198             src3_target = GL_SOURCE3_RGB_NV;
1199             opr3_target = GL_OPERAND3_RGB_NV;
1200           }
1201           switch (op) {
1202           case D3DTOP_DISABLE: /* Only for alpha */
1203             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1204             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1205             glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
1206             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1207             glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
1208             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1209             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1210             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1211             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1212             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1213             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1214             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
1215             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1216             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1217             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1218             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1219             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1220             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1221             break;
1222           case D3DTOP_SELECTARG1:                                          /* = a1 * 1 + 0 * 0 */
1223           case D3DTOP_SELECTARG2:                                          /* = a2 * 1 + 0 * 0 */
1224             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1225             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1226             if (op == D3DTOP_SELECTARG1) {
1227               glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1228               checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1229               glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1230               checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1231             } else {
1232               glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
1233               checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
1234               glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
1235               checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
1236             }
1237             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1238             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1239             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1240             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1241             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1242             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
1243             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1244             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1245             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1246             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1247             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1248             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1249             break;
1250
1251           case D3DTOP_MODULATE:
1252             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1253             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
1254             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1255             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1256             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1257             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1258             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1259             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1260             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1261             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1262             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1263             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1264             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1265             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1266             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1267             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1268             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1269             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
1270             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1271             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1272             break;
1273           case D3DTOP_MODULATE2X:
1274             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1275             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
1276             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1277             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1278             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1279             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1280             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1281             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1282             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1283             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1284             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1285             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1286             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1287             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1288             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1289             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1290             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1291             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
1292             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1293             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1294             break;
1295           case D3DTOP_MODULATE4X:
1296             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1297             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
1298             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1299             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1300             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1301             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1302             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1303             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1304             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1305             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1306             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1307             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1308             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1309             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1310             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1311             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1312             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1313             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
1314             glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
1315             checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
1316             break;
1317
1318           case D3DTOP_ADD:
1319             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1320             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1321             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1322             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1323             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1324             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1325             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1326             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1327             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1328             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1329             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1330             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1331             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1332             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1333             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1334             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1335             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1336             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1337             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1338             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1339             break;
1340
1341           case D3DTOP_ADDSIGNED:
1342             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1343             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1344             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1345             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1346             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1347             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1348             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1349             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1350             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1351             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1352             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1353             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1354             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1355             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1356             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1357             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1358             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1359             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1360             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1361             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1362             break;
1363
1364           case D3DTOP_ADDSIGNED2X:
1365             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1366             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1367             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1368             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1369             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1370             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1371             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1372             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1373             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1374             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1375             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1376             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1377             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1378             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1379             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1380             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1381             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1382             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1383             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1384             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1385             break;
1386
1387           case D3DTOP_ADDSMOOTH:
1388             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1389             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1390             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1391             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1392             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1393             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1394             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1395             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1396             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1397             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1398             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1399             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1400             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1401             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1402             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
1403             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
1404             switch (opr1) {
1405             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1406             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1407             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1408             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1409             }
1410             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1411             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1412             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1413             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1414             break;
1415
1416           case D3DTOP_BLENDDIFFUSEALPHA:
1417             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1418             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1419             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1420             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1421             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1422             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1423             glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR));
1424             checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR)");
1425             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1426             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1427             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1428             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1429             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1430             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1431             glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR));
1432             checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR)");
1433             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1434             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1435             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1436             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1437             break;
1438           case D3DTOP_BLENDTEXTUREALPHA:
1439             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1440             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1441             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1442             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1443             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1444             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1445             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_TEXTURE);
1446             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_TEXTURE");
1447             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1448             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1449             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1450             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1451             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1452             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1453             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
1454             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
1455             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1456             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1457             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1458             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1459             break;
1460           case D3DTOP_BLENDFACTORALPHA:
1461             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1462             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1463             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1464             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1465             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1466             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1467             glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT));
1468             checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT)");
1469             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1470             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1471             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1472             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1473             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1474             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1475             glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT));
1476             checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT)");
1477             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1478             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1479             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1480             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1481             break;
1482           case D3DTOP_BLENDTEXTUREALPHAPM:
1483             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1484             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1485             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1486             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1487             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1488             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1489             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1490             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1491             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1492             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1493             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1494             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1495             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1496             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1497             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
1498             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
1499             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
1500             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
1501             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1502             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1503             break;
1504           case D3DTOP_MODULATEALPHA_ADDCOLOR:
1505             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1506             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");  /* Add = a0*a1 + a2*a3 */
1507             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);        /*   a0 = src1/opr1    */
1508             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1509             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1510             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");    /*   a1 = 1 (see docs) */
1511             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1512             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1513             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1514             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1515             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);        /*   a2 = arg2         */
1516             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1517             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1518             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");     /*  a3 = src1 alpha   */
1519             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
1520             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
1521             switch (opr) {
1522             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1523             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1524             }
1525             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1526             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1527             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1528             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1529             break;
1530           case D3DTOP_MODULATECOLOR_ADDALPHA:
1531             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1532             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1533             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1534             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1535             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1536             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1537             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1538             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1539             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1540             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1541             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
1542             checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
1543             switch (opr1) {
1544             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1545             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1546             }
1547             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1548             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1549             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1550             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1551             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1552             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1553             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1554             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1555             break;
1556           case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
1557             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1558             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1559             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1560             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1561             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1562             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1563             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1564             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1565             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1566             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1567             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1568             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1569             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1570             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1571             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
1572             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
1573             switch (opr1) {
1574             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1575             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1576             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1577             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1578             }
1579             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1580             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1581             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1582             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1583             break;
1584           case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
1585             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1586             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1587             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1588             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1589             switch (opr1) {
1590             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1591             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1592             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1593             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1594             }
1595             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1596             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1597             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1598             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1599             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1600             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1601             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
1602             checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
1603             switch (opr1) {
1604             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1605             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1606             }
1607             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1608             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1609             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1610             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1611             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
1612             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
1613             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1614             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1615             break;
1616           case D3DTOP_MULTIPLYADD:
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, src3);
1620             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1621             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
1622             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1623             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1624             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1625             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1626             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1627             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
1628             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1629             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr1);
1630             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1631             glTexEnvi(GL_TEXTURE_ENV, src3_target, src2);
1632             checkGLcall("GL_TEXTURE_ENV, src3_target, src3");
1633             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr2);
1634             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr3");
1635             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1636             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1637             break;
1638
1639           case D3DTOP_BUMPENVMAP:
1640             {
1641               if (GL_SUPPORT(NV_TEXTURE_SHADER)) {
1642                 /*
1643                   texture unit 0: GL_TEXTURE_2D
1644                   texture unit 1: GL_DOT_PRODUCT_NV
1645                   texture unit 2: GL_DOT_PRODUCT_DIFFUSE_CUBE_MAP_NV
1646                   texture unit 3: GL_DOT_PRODUCT_REFLECT_CUBE_MAP_NV
1647                 */
1648                 float m[2][2];
1649
1650                 union {
1651                   float f;
1652                   DWORD d;
1653                 } tmpvalue;
1654
1655                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT00];
1656                 m[0][0] = tmpvalue.f;
1657                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT01];
1658                 m[0][1] = tmpvalue.f;
1659                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT10];
1660                 m[1][0] = tmpvalue.f;
1661                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT11];
1662                 m[1][1] = tmpvalue.f;
1663
1664                 /*FIXME("Stage %d matrix is (%.2f,%.2f),(%.2f,%.2f)\n", Stage, m[0][0], m[0][1], m[1][0], m[1][0]);*/
1665
1666                 if (!This->texture_shader_active) {
1667                   This->texture_shader_active = TRUE;
1668                   glEnable(GL_TEXTURE_SHADER_NV);
1669                 }
1670
1671                 /*
1672                 glEnable(GL_REGISTER_COMBINERS_NV);
1673                 glCombinerParameteriNV (GL_NUM_GENERAL_COMBINERS_NV, 1);
1674                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV, GL_TEXTURE0_ARB, GL_SIGNED_IDENTITY_NV, GL_RGB);
1675                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_RGB);
1676                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV, GL_TEXTURE2_ARB, GL_SIGNED_IDENTITY_NV, GL_RGB);
1677                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_RGB);
1678                 glCombinerOutputNV (GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV, GL_PRIMARY_COLOR_NV, 0, 0, 0, 0, 0);
1679                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_A_NV, GL_TEXTURE0_ARB, GL_SIGNED_IDENTITY_NV, GL_ALPHA);
1680                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_B_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_ALPHA);
1681                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_C_NV, GL_TEXTURE2_ARB, GL_SIGNED_IDENTITY_NV, GL_ALPHA);
1682                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_D_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_ALPHA);
1683                 glCombinerOutputNV (GL_COMBINER0_NV, GL_ALPHA, GL_DISCARD_NV, GL_DISCARD_NV, GL_PRIMARY_COLOR_NV, 0, 0, 0, 0, 0);
1684                 glDisable (GL_PER_STAGE_CONSTANTS_NV);
1685                 glCombinerParameteriNV (GL_COLOR_SUM_CLAMP_NV, 0);
1686                 glFinalCombinerInputNV (GL_VARIABLE_A_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1687                 glFinalCombinerInputNV (GL_VARIABLE_B_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1688                 glFinalCombinerInputNV (GL_VARIABLE_C_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1689                 glFinalCombinerInputNV (GL_VARIABLE_D_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1690                 glFinalCombinerInputNV (GL_VARIABLE_E_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1691                 glFinalCombinerInputNV (GL_VARIABLE_F_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
1692                 glFinalCombinerInputNV (GL_VARIABLE_G_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
1693                 */
1694                 /*
1695                 int i;
1696                 for (i = 0; i < Stage; i++) {
1697                   if (GL_SUPPORT(ARB_MULTITEXTURE)) {
1698                     GL_ACTIVETEXTURE(i);
1699                     glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
1700                     checkGLcall("Activate texture..");
1701                   } else if (i>0) {
1702                     FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
1703                   }
1704                 }
1705                 */
1706                 /*
1707                   GL_ACTIVETEXTURE(Stage - 1);
1708                   glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
1709                 */
1710                 /*
1711                 GL_ACTIVETEXTURE(Stage);
1712                 checkGLcall("Activate texture.. to update const color");
1713                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
1714                 checkGLcall("glTexEnv");
1715                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + Stage - 1);
1716                 checkGLcall("glTexEnv");
1717                 glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, (float*)&m[0]);
1718                 checkGLcall("glTexEnv");
1719                 */
1720                 LEAVE_GL();
1721                 return;
1722               }
1723             }
1724
1725           case D3DTOP_BUMPENVMAPLUMINANCE:
1726
1727           default:
1728             Handled = FALSE;
1729           }
1730           if (Handled) {
1731             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
1732             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV");
1733
1734             LEAVE_GL();
1735             return;
1736           }
1737         } /* GL_NV_texture_env_combine4 */
1738
1739         Handled = TRUE; /* Again, assume handled */
1740         switch (op) {
1741         case D3DTOP_DISABLE: /* Only for alpha */
1742                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1743                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1744                 glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
1745                 checkGLcall("GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT");
1746                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
1747                 checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA");
1748                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1749                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1750                 break;
1751         case D3DTOP_SELECTARG1:
1752                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1753                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1754                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1755                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1756                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1757                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1758                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1759                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1760                 break;
1761         case D3DTOP_SELECTARG2:
1762                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1763                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1764                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
1765                 checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
1766                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
1767                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
1768                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1769                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1770                 break;
1771         case D3DTOP_MODULATE:
1772                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1773                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1774                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1775                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1776                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1777                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1778                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1779                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1780                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1781                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1782                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1783                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1784                 break;
1785         case D3DTOP_MODULATE2X:
1786                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1787                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1788                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1789                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1790                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1791                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1792                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1793                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1794                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1795                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1796                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1797                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1798                 break;
1799         case D3DTOP_MODULATE4X:
1800                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1801                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1802                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1803                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1804                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1805                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1806                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1807                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1808                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1809                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1810                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
1811                 checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
1812                 break;
1813         case D3DTOP_ADD:
1814                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1815                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1816                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1817                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1818                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1819                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1820                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1821                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1822                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1823                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1824                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1825                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1826                 break;
1827         case D3DTOP_ADDSIGNED:
1828                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1829                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext((GL_ADD_SIGNED)");
1830                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1831                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1832                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1833                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1834                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1835                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1836                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1837                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1838                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1839                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1840                 break;
1841         case D3DTOP_ADDSIGNED2X:
1842                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1843                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1844                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1845                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1846                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1847                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1848                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1849                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1850                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1851                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1852                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1853                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1854                 break;
1855         case D3DTOP_SUBTRACT:
1856           if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE)) {
1857                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_SUBTRACT);
1858                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_SUBTRACT)");
1859                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1860                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1861                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1862                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1863                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1864                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1865                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1866                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1867                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1868                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1869           } else {
1870                 FIXME("This version of opengl does not support GL_SUBTRACT\n");
1871           }
1872           break;
1873
1874         case D3DTOP_BLENDDIFFUSEALPHA:
1875                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1876                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1877                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1878                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1879                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1880                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1881                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1882                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1883                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1884                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1885                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PRIMARY_COLOR));
1886                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PRIMARY_COLOR");
1887                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1888                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1889                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1890                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1891                 break;
1892         case D3DTOP_BLENDTEXTUREALPHA:
1893                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1894                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1895                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1896                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1897                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1898                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1899                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1900                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1901                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1902                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1903                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_TEXTURE);
1904                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_TEXTURE");
1905                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1906                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1907                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1908                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1909                 break;
1910         case D3DTOP_BLENDFACTORALPHA:
1911                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1912                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1913                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1914                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1915                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1916                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1917                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1918                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1919                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1920                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1921                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_CONSTANT));
1922                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_CONSTANT");
1923                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1924                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1925                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1926                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1927                 break;
1928         case D3DTOP_BLENDCURRENTALPHA:
1929                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1930                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1931                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1932                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1933                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1934                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1935                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1936                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1937                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1938                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1939                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PREVIOUS));
1940                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PREVIOUS");
1941                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1942                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1943                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1944                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1945                 break;
1946         case D3DTOP_DOTPRODUCT3:
1947                 if (GL_SUPPORT(ARB_TEXTURE_ENV_DOT3)) {
1948                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB);
1949                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB");
1950                 } else if (GL_SUPPORT(EXT_TEXTURE_ENV_DOT3)) {
1951                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT);
1952                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT");
1953                 } else {
1954                   FIXME("This version of opengl does not support GL_DOT3\n");
1955                 }
1956                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1957                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1958                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1959                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1960                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1961                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1962                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1963                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1964                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1965                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1966                 break;
1967         case D3DTOP_LERP:
1968                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1969                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1970                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1971                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1972                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1973                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1974                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1975                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1976                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1977                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1978                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src3);
1979                 checkGLcall("GL_TEXTURE_ENV, src2_target, src3");
1980                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr3);
1981                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr3");
1982                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1983                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1984                 break;
1985         case D3DTOP_ADDSMOOTH:
1986                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1987                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1988                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1989                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1990                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1991                   switch (opr1) {
1992                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1993                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1994                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1995                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1996                   }
1997                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1998                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1999                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2000                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2001                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2002                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2003                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2004                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2005                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2006                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2007                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2008                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2009                 } else
2010                   Handled = FALSE;
2011                 break;
2012         case D3DTOP_BLENDTEXTUREALPHAPM:
2013                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2014                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2015                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2016                   glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_TEXTURE);
2017                   checkGLcall("GL_TEXTURE_ENV, src0_target, GL_TEXTURE");
2018                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_ALPHA);
2019                   checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_APHA");
2020                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2021                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2022                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2023                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2024                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2025                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2026                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2027                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2028                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2029                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2030                 } else
2031                   Handled = FALSE;
2032                 break;
2033         case D3DTOP_MODULATEALPHA_ADDCOLOR:
2034                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2035                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2036                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2037                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2038                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2039                   switch (opr1) {
2040                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2041                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2042                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2043                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2044                   }
2045                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2046                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2047                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2048                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2049                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2050                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2051                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2052                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2053                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2054                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2055                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2056                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2057                 } else
2058                   Handled = FALSE;
2059                 break;
2060         case D3DTOP_MODULATECOLOR_ADDALPHA:
2061                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2062                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2063                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2064                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2065                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2066                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2067                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2068                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2069                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2070                   switch (opr1) {
2071                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2072                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2073                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2074                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2075                   }
2076                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2077                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2078                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2079                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2080                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2081                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2082                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2083                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2084                 } else
2085                   Handled = FALSE;
2086                 break;
2087         case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
2088                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2089                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2090                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2091                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2092                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2093                   switch (opr1) {
2094                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2095                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2096                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2097                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2098                   }
2099                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2100                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2101                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2102                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2103                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2104                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2105                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2106                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2107                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2108                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2109                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2110                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2111                 } else
2112                   Handled = FALSE;
2113                 break;
2114         case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
2115                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2116                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2117                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2118                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2119                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2120                   switch (opr1) {
2121                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2122                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2123                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2124                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2125                   }
2126                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2127                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2128                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2129                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2130                   switch (opr1) {
2131                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2132                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2133                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2134                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2135                   }
2136                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2137                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2138                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2139                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2140                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2141                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2142                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2143                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2144                 } else
2145                   Handled = FALSE;
2146                 break;
2147         case D3DTOP_MULTIPLYADD:
2148                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2149                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2150                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2151                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
2152                   checkGLcall("GL_TEXTURE_ENV, src0_target, src3");
2153                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
2154                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr3");
2155                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2156                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2157                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2158                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2159                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2160                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2161                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2162                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2163                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2164                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2165                 } else
2166                   Handled = FALSE;
2167                 break;
2168         default:
2169                 Handled = FALSE;
2170         }
2171
2172         if (Handled) {
2173           BOOL  combineOK = TRUE;
2174           if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
2175             DWORD op2;
2176
2177             if (isAlpha) {
2178               op2 = This->stateBlock->textureState[Stage][WINED3DTSS_COLOROP];
2179             } else {
2180               op2 = This->stateBlock->textureState[Stage][WINED3DTSS_ALPHAOP];
2181             }
2182
2183             /* Note: If COMBINE4 in effect can't go back to combine! */
2184             switch (op2) {
2185             case D3DTOP_ADDSMOOTH:
2186             case D3DTOP_BLENDTEXTUREALPHAPM:
2187             case D3DTOP_MODULATEALPHA_ADDCOLOR:
2188             case D3DTOP_MODULATECOLOR_ADDALPHA:
2189             case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
2190             case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
2191             case D3DTOP_MULTIPLYADD:
2192               /* Ignore those implemented in both cases */
2193               switch (op) {
2194               case D3DTOP_SELECTARG1:
2195               case D3DTOP_SELECTARG2:
2196                 combineOK = FALSE;
2197                 Handled   = FALSE;
2198                 break;
2199               default:
2200                 FIXME("Can't use COMBINE4 and COMBINE together, thisop=%s, otherop=%s, isAlpha(%d)\n", debug_d3dtop(op), debug_d3dtop(op2), isAlpha);
2201                 LEAVE_GL();
2202                 return;
2203               }
2204             }
2205           }
2206
2207           if (combineOK) {
2208             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE));
2209             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE)");
2210
2211             LEAVE_GL();
2212             return;
2213           }
2214         }
2215
2216         LEAVE_GL();
2217
2218         /* After all the extensions, if still unhandled, report fixme */
2219         FIXME("Unhandled texture operation %s\n", debug_d3dtop(op));
2220         #undef GLINFO_LOCATION
2221 }
2222 #endif
2223
2224 /* Setup this textures matrix according to the texture flags*/
2225 void set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords)
2226 {
2227     float mat[16];
2228
2229     glMatrixMode(GL_TEXTURE);
2230     checkGLcall("glMatrixMode(GL_TEXTURE)");
2231
2232     if (flags == D3DTTFF_DISABLE) {
2233         glLoadIdentity();
2234         checkGLcall("glLoadIdentity()");
2235         return;
2236     }
2237
2238     if (flags == (D3DTTFF_COUNT1|D3DTTFF_PROJECTED)) {
2239         ERR("Invalid texture transform flags: D3DTTFF_COUNT1|D3DTTFF_PROJECTED\n");
2240         return;
2241     }
2242
2243     memcpy(mat, smat, 16 * sizeof(float));
2244
2245     switch (flags & ~D3DTTFF_PROJECTED) {
2246     case D3DTTFF_COUNT1: mat[1] = mat[5] = mat[13] = 0;
2247     case D3DTTFF_COUNT2: mat[2] = mat[6] = mat[10] = mat[14] = 0;
2248     default: mat[3] = mat[7] = mat[11] = 0, mat[15] = 1;
2249     }
2250
2251     if (flags & D3DTTFF_PROJECTED) {
2252         switch (flags & ~D3DTTFF_PROJECTED) {
2253         case D3DTTFF_COUNT2:
2254             mat[3] = mat[1], mat[7] = mat[5], mat[11] = mat[9], mat[15] = mat[13];
2255             mat[1] = mat[5] = mat[9] = mat[13] = 0;
2256             break;
2257         case D3DTTFF_COUNT3:
2258             mat[3] = mat[2], mat[7] = mat[6], mat[11] = mat[10], mat[15] = mat[14];
2259             mat[2] = mat[6] = mat[10] = mat[14] = 0;
2260             break;
2261         }
2262     } else if(!calculatedCoords) { /* under directx the R/Z coord can be used for translation, under opengl we use the Q coord instead */
2263         mat[12] = mat[8];
2264         mat[13] = mat[9];
2265     }
2266
2267     glLoadMatrixf(mat);
2268     checkGLcall("glLoadMatrixf(mat)");
2269 }
2270
2271 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
2272
2273 /* Convertes a D3D format into a OpenGL configuration format */
2274 int D3DFmtMakeGlCfg(D3DFORMAT BackBufferFormat, D3DFORMAT StencilBufferFormat, int *attribs, int* nAttribs, BOOL alternate){
2275 #define PUSH1(att)        attribs[(*nAttribs)++] = (att);
2276 #define PUSH2(att,value)  attribs[(*nAttribs)++] = (att); attribs[(*nAttribs)++] = (value);
2277     /*We need to do some Card specific stuff in here at some point,
2278     D3D now supports floating point format buffers, and there are a number of different OpelGl ways of managing these e.g.
2279     GLX_ATI_pixel_format_float
2280     */
2281     switch (BackBufferFormat) {
2282         /* color buffer */
2283     case WINED3DFMT_P8:
2284         PUSH2(GLX_RENDER_TYPE,  GLX_COLOR_INDEX_BIT);
2285         PUSH2(GLX_BUFFER_SIZE,  8);
2286         PUSH2(GLX_DOUBLEBUFFER, TRUE);
2287         break;
2288
2289     case WINED3DFMT_R3G3B2:
2290         PUSH2(GLX_RENDER_TYPE,  GLX_RGBA_BIT);
2291         PUSH2(GLX_RED_SIZE,     3);
2292         PUSH2(GLX_GREEN_SIZE,   3);
2293         PUSH2(GLX_BLUE_SIZE,    2);
2294         break;
2295
2296     case WINED3DFMT_A1R5G5B5:
2297         PUSH2(GLX_ALPHA_SIZE,   1);
2298     case WINED3DFMT_X1R5G5B5:
2299         PUSH2(GLX_RED_SIZE,     5);
2300         PUSH2(GLX_GREEN_SIZE,   5);
2301         PUSH2(GLX_BLUE_SIZE,    5);
2302         break;
2303
2304     case WINED3DFMT_R5G6B5:
2305         PUSH2(GLX_RED_SIZE,     5);
2306         PUSH2(GLX_GREEN_SIZE,   6);
2307         PUSH2(GLX_BLUE_SIZE,    5);
2308         break;
2309
2310     case WINED3DFMT_A4R4G4B4:
2311         PUSH2(GLX_ALPHA_SIZE,   4);
2312     case WINED3DFMT_X4R4G4B4:
2313         PUSH2(GLX_RED_SIZE,     4);
2314         PUSH2(GLX_GREEN_SIZE,   4);
2315         PUSH2(GLX_BLUE_SIZE,    4);
2316         break;
2317
2318     case WINED3DFMT_A8R8G8B8:
2319         PUSH2(GLX_ALPHA_SIZE,   8);
2320     case WINED3DFMT_R8G8B8:
2321     case WINED3DFMT_X8R8G8B8:
2322         PUSH2(GLX_RED_SIZE,     8);
2323         PUSH2(GLX_GREEN_SIZE,   8);
2324         PUSH2(GLX_BLUE_SIZE,    8);
2325         break;
2326
2327     case WINED3DFMT_A2R10G10B10:
2328         PUSH2(GLX_ALPHA_SIZE,   2);
2329         PUSH2(GLX_RED_SIZE,    10);
2330         PUSH2(GLX_GREEN_SIZE,  10);
2331         PUSH2(GLX_BLUE_SIZE,   10);
2332         break;
2333
2334     case WINED3DFMT_A16B16G16R16:        
2335         PUSH2(GLX_ALPHA_SIZE,  16);
2336         PUSH2(GLX_RED_SIZE,    16);
2337         PUSH2(GLX_GREEN_SIZE,  16);
2338         PUSH2(GLX_BLUE_SIZE,   16);
2339         break;
2340         
2341     default:
2342         FIXME("Unsupported color format: %s\n", debug_d3dformat(BackBufferFormat));
2343         break;
2344     }
2345     if(!alternate){
2346         switch (StencilBufferFormat) {
2347     case 0:
2348         break;
2349
2350     case WINED3DFMT_D16_LOCKABLE:
2351     case WINED3DFMT_D16:
2352         PUSH2(GLX_DEPTH_SIZE,   16);
2353         break;
2354
2355     case WINED3DFMT_D15S1:
2356         PUSH2(GLX_DEPTH_SIZE,   15);
2357         PUSH2(GLX_STENCIL_SIZE, 1);
2358         /*Does openGl support a 1bit stencil?, I've seen it used elsewhere
2359         e.g. http://www.ks.uiuc.edu/Research/vmd/doxygen/OpenGLDisplayDevice_8C-source.html*/
2360         break;
2361
2362     case WINED3DFMT_D24X8:
2363         PUSH2(GLX_DEPTH_SIZE,   24);
2364         break;
2365
2366     case WINED3DFMT_D24X4S4:
2367         PUSH2(GLX_DEPTH_SIZE,   24);
2368         PUSH2(GLX_STENCIL_SIZE, 4);
2369         break;
2370
2371     case WINED3DFMT_D24S8:
2372         PUSH2(GLX_DEPTH_SIZE,   24);
2373         PUSH2(GLX_STENCIL_SIZE, 8);
2374         break;
2375
2376     case WINED3DFMT_D24FS8:
2377         PUSH2(GLX_DEPTH_SIZE,   24);
2378         PUSH2(GLX_STENCIL_SIZE, 8);
2379         break;
2380
2381     case WINED3DFMT_D32:
2382         PUSH2(GLX_DEPTH_SIZE,   32);
2383         break;
2384
2385     default:
2386         FIXME("Unsupported stencil format: %s\n", debug_d3dformat(StencilBufferFormat));
2387         break;
2388     }
2389
2390     } else { /* it the device doesn't support the 'exact' format, try to find something close */
2391         switch (StencilBufferFormat) {
2392         case 0:
2393             break;
2394             
2395         case WINED3DFMT_D16_LOCKABLE:
2396         case WINED3DFMT_D16:
2397             PUSH2(GLX_DEPTH_SIZE,   1);
2398             break;
2399
2400         case WINED3DFMT_D15S1:
2401             PUSH2(GLX_DEPTH_SIZE,   1);
2402             PUSH2(GLX_STENCIL_SIZE, 1);
2403             /*Does openGl support a 1bit stencil?, I've seen it used elsewhere
2404             e.g. http://www.ks.uiuc.edu/Research/vmd/doxygen/OpenGLDisplayDevice_8C-source.html*/
2405             break;
2406
2407         case WINED3DFMT_D24X8:
2408             PUSH2(GLX_DEPTH_SIZE,   1);
2409             break;
2410
2411         case WINED3DFMT_D24X4S4:
2412             PUSH2(GLX_DEPTH_SIZE,   1);
2413             PUSH2(GLX_STENCIL_SIZE, 1);
2414             break;
2415
2416         case WINED3DFMT_D24S8:
2417             PUSH2(GLX_DEPTH_SIZE,   1);
2418             PUSH2(GLX_STENCIL_SIZE, 1);
2419             break;
2420
2421         case WINED3DFMT_D24FS8:
2422             PUSH2(GLX_DEPTH_SIZE,   1);
2423             PUSH2(GLX_STENCIL_SIZE, 1);
2424             break;
2425
2426         case WINED3DFMT_D32:
2427             PUSH2(GLX_DEPTH_SIZE,   1);
2428             break;
2429
2430         default:
2431             FIXME("Unsupported stencil format: %s\n", debug_d3dformat(StencilBufferFormat));
2432             break;
2433         }
2434     }
2435
2436     return *nAttribs;
2437 }
2438
2439 #undef GLINFO_LOCATION
2440
2441 /* DirectDraw stuff */
2442 WINED3DFORMAT pixelformat_for_depth(DWORD depth) {
2443     switch(depth) {
2444         case 8:  return D3DFMT_P8; break;
2445         case 15: return WINED3DFMT_X1R5G5B5; break;
2446         case 16: return WINED3DFMT_R5G6B5; break;
2447         case 24: return WINED3DFMT_R8G8B8; break;
2448         case 32: return WINED3DFMT_X8R8G8B8; break;
2449         default: return WINED3DFMT_UNKNOWN;
2450     }
2451 }
2452
2453 void multiply_matrix(D3DMATRIX *dest, D3DMATRIX *src1, D3DMATRIX *src2) {
2454     D3DMATRIX temp;
2455
2456     /* Now do the multiplication 'by hand'.
2457        I know that all this could be optimised, but this will be done later :-) */
2458     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);
2459     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);
2460     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);
2461     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);
2462
2463     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);
2464     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);
2465     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);
2466     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);
2467
2468     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);
2469     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);
2470     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);
2471     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);
2472
2473     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);
2474     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);
2475     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);
2476     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);
2477
2478     /* And copy the new matrix in the good storage.. */
2479     memcpy(dest, &temp, 16 * sizeof(float));
2480 }
2481
2482 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType) {
2483     DWORD size = 0;
2484     int i;
2485     int numTextures = (d3dvtVertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
2486
2487     if (d3dvtVertexType & D3DFVF_NORMAL) size += 3 * sizeof(float);
2488     if (d3dvtVertexType & D3DFVF_DIFFUSE) size += sizeof(DWORD);
2489     if (d3dvtVertexType & D3DFVF_SPECULAR) size += sizeof(DWORD);
2490     if (d3dvtVertexType & D3DFVF_PSIZE) size += sizeof(DWORD);
2491     switch (d3dvtVertexType & D3DFVF_POSITION_MASK) {
2492         case D3DFVF_XYZ: size += 3 * sizeof(float); break;
2493         case D3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2494         default: TRACE(" matrix weighting not handled yet...\n");
2495     }
2496     for (i = 0; i < numTextures; i++) {
2497         size += GET_TEXCOORD_SIZE_FROM_FVF(d3dvtVertexType, i) * sizeof(float);
2498     }
2499
2500     return size;
2501 }
2502
2503 /***********************************************************************
2504  * CalculateTexRect
2505  *
2506  * Calculates the dimensions of the opengl texture used for blits.
2507  * Handled oversized opengl textures and updates the source rectangle
2508  * accordingly
2509  *
2510  * Params:
2511  *  This: Surface to operate on
2512  *  Rect: Requested rectangle
2513  *
2514  * Returns:
2515  *  TRUE if the texture part can be loaded,
2516  *  FALSE otherwise
2517  *
2518  *********************************************************************/
2519 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->resource.wineD3DDevice->wineD3D))->gl_info
2520
2521 BOOL CalculateTexRect(IWineD3DSurfaceImpl *This, RECT *Rect, float glTexCoord[4]) {
2522     int x1 = Rect->left, x2 = Rect->right;
2523     int y1 = Rect->top, y2 = Rect->bottom;
2524     GLint maxSize = GL_LIMITS(texture_size);
2525
2526     TRACE("(%p)->(%d,%d)-(%d,%d)\n", This,
2527           Rect->left, Rect->top, Rect->right, Rect->bottom);
2528
2529     /* The sizes might be reversed */
2530     if(Rect->left > Rect->right) {
2531         x1 = Rect->right;
2532         x2 = Rect->left;
2533     }
2534     if(Rect->top > Rect->bottom) {
2535         y1 = Rect->bottom;
2536         y2 = Rect->top;
2537     }
2538
2539     /* No oversized texture? This is easy */
2540     if(!(This->Flags & SFLAG_OVERSIZE)) {
2541         /* Which rect from the texture do I need? */
2542         glTexCoord[0] = (float) Rect->left / (float) This->pow2Width;
2543         glTexCoord[2] = (float) Rect->top / (float) This->pow2Height;
2544         glTexCoord[1] = (float) Rect->right / (float) This->pow2Width;
2545         glTexCoord[3] = (float) Rect->bottom / (float) This->pow2Height;
2546
2547         return TRUE;
2548     } else {
2549         /* Check if we can succeed at all */
2550         if( (x2 - x1) > maxSize ||
2551             (y2 - y1) > maxSize ) {
2552             TRACE("Requested rectangle is too large for gl\n");
2553             return FALSE;
2554         }
2555
2556         /* A part of the texture has to be picked. First, check if
2557          * some texture part is loaded already, if yes try to re-use it.
2558          * If the texture is dirty, or the part can't be used,
2559          * re-position the part to load
2560          */
2561         if(!(This->Flags & SFLAG_DIRTY)) {
2562             if(This->glRect.left <= x1 && This->glRect.right >= x2 &&
2563                This->glRect.top <= y1 && This->glRect.bottom >= x2 ) {
2564                 /* Ok, the rectangle is ok, re-use it */
2565                 TRACE("Using existing gl Texture\n");
2566             } else {
2567                 /* Rectangle is not ok, dirtify the texture to reload it */
2568                 TRACE("Dirtifying texture to force reload\n");
2569                 This->Flags |= SFLAG_DIRTY;
2570             }
2571         }
2572
2573         /* Now if we are dirty(no else if!) */
2574         if(This->Flags & SFLAG_DIRTY) {
2575             /* Set the new rectangle. Use the following strategy:
2576              * 1) Use as big textures as possible.
2577              * 2) Place the texture part in the way that the requested
2578              *    part is in the middle of the texture(well, almost)
2579              * 3) If the texture is moved over the edges of the
2580              *    surface, replace it nicely
2581              * 4) If the coord is not limiting the texture size,
2582              *    use the whole size
2583              */
2584             if((This->pow2Width) > maxSize) {
2585                 This->glRect.left = x1 - maxSize / 2;
2586                 if(This->glRect.left < 0) {
2587                     This->glRect.left = 0;
2588                 }
2589                 This->glRect.right = This->glRect.left + maxSize;
2590                 if(This->glRect.right > This->currentDesc.Width) {
2591                     This->glRect.right = This->currentDesc.Width;
2592                     This->glRect.left = This->glRect.right - maxSize;
2593                 }
2594             } else {
2595                 This->glRect.left = 0;
2596                 This->glRect.right = This->pow2Width;
2597             }
2598
2599             if(This->pow2Height > maxSize) {
2600                 This->glRect.top = x1 - GL_LIMITS(texture_size) / 2;
2601                 if(This->glRect.top < 0) This->glRect.top = 0;
2602                 This->glRect.bottom = This->glRect.left + maxSize;
2603                 if(This->glRect.bottom > This->currentDesc.Height) {
2604                     This->glRect.bottom = This->currentDesc.Height;
2605                     This->glRect.top = This->glRect.bottom - maxSize;
2606                 }
2607             } else {
2608                 This->glRect.top = 0;
2609                 This->glRect.bottom = This->pow2Height;
2610             }
2611             TRACE("(%p): Using rect (%d,%d)-(%d,%d)\n", This,
2612                    This->glRect.left, This->glRect.top, This->glRect.right, This->glRect.bottom);
2613         }
2614
2615         /* Re-calculate the rect to draw */
2616         Rect->left -= This->glRect.left;
2617         Rect->right -= This->glRect.left;
2618         Rect->top -= This->glRect.top;
2619         Rect->bottom -= This->glRect.top;
2620
2621         /* Get the gl coordinates. The gl rectangle is a power of 2, eigher the max size,
2622          * or the pow2Width / pow2Height of the surface
2623          */
2624         glTexCoord[0] = (float) Rect->left / (float) (This->glRect.right - This->glRect.left);
2625         glTexCoord[2] = (float) Rect->top / (float) (This->glRect.bottom - This->glRect.top);
2626         glTexCoord[1] = (float) Rect->right / (float) (This->glRect.right - This->glRect.left);
2627         glTexCoord[3] = (float) Rect->bottom / (float) (This->glRect.bottom - This->glRect.top);
2628     }
2629     return TRUE;
2630 }
2631 #undef GLINFO_LOCATION