wined3d: Minor vshader_program_add_param cleanup.
[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  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28
29 /*****************************************************************************
30  * Trace formatting of useful values
31  */
32 const char* debug_d3dformat(WINED3DFORMAT fmt) {
33   switch (fmt) {
34 #define FMT_TO_STR(fmt) case fmt: return #fmt
35     FMT_TO_STR(WINED3DFMT_UNKNOWN);
36     FMT_TO_STR(WINED3DFMT_R8G8B8);
37     FMT_TO_STR(WINED3DFMT_A8R8G8B8);
38     FMT_TO_STR(WINED3DFMT_A8B8G8R8);
39     FMT_TO_STR(WINED3DFMT_X8R8G8B8);
40     FMT_TO_STR(WINED3DFMT_R5G6B5);
41     FMT_TO_STR(WINED3DFMT_X1R5G5B5);
42     FMT_TO_STR(WINED3DFMT_A1R5G5B5);
43     FMT_TO_STR(WINED3DFMT_A4R4G4B4);
44     FMT_TO_STR(WINED3DFMT_R3G3B2);
45     FMT_TO_STR(WINED3DFMT_A8);
46     FMT_TO_STR(WINED3DFMT_A8R3G3B2);
47     FMT_TO_STR(WINED3DFMT_X4R4G4B4);
48     FMT_TO_STR(WINED3DFMT_G16R16);
49     FMT_TO_STR(WINED3DFMT_A2R10G10B10);
50     FMT_TO_STR(WINED3DFMT_A16B16G16R16);
51     FMT_TO_STR(WINED3DFMT_A8P8);
52     FMT_TO_STR(WINED3DFMT_P8);
53     FMT_TO_STR(WINED3DFMT_L8);
54     FMT_TO_STR(WINED3DFMT_A8L8);
55     FMT_TO_STR(WINED3DFMT_A4L4);
56     FMT_TO_STR(WINED3DFMT_V8U8);
57     FMT_TO_STR(WINED3DFMT_L6V5U5);
58     FMT_TO_STR(WINED3DFMT_X8L8V8U8);
59     FMT_TO_STR(WINED3DFMT_Q8W8V8U8);
60     FMT_TO_STR(WINED3DFMT_V16U16);
61     FMT_TO_STR(WINED3DFMT_W11V11U10);
62     FMT_TO_STR(WINED3DFMT_A2W10V10U10);
63     FMT_TO_STR(WINED3DFMT_UYVY);
64     FMT_TO_STR(WINED3DFMT_YUY2);
65     FMT_TO_STR(WINED3DFMT_DXT1);
66     FMT_TO_STR(WINED3DFMT_DXT2);
67     FMT_TO_STR(WINED3DFMT_DXT3);
68     FMT_TO_STR(WINED3DFMT_DXT4);
69     FMT_TO_STR(WINED3DFMT_DXT5);
70     FMT_TO_STR(WINED3DFMT_MULTI2_ARGB);
71     FMT_TO_STR(WINED3DFMT_G8R8_G8B8);
72     FMT_TO_STR(WINED3DFMT_R8G8_B8G8);
73     FMT_TO_STR(WINED3DFMT_D16_LOCKABLE);
74     FMT_TO_STR(WINED3DFMT_D32);
75     FMT_TO_STR(WINED3DFMT_D15S1);
76     FMT_TO_STR(WINED3DFMT_D24S8);
77     FMT_TO_STR(WINED3DFMT_D24FS8);
78     FMT_TO_STR(WINED3DFMT_D24X8);
79     FMT_TO_STR(WINED3DFMT_D24X4S4);
80     FMT_TO_STR(WINED3DFMT_D16);
81     FMT_TO_STR(WINED3DFMT_D32F_LOCKABLE);
82     FMT_TO_STR(WINED3DFMT_VERTEXDATA);
83     FMT_TO_STR(WINED3DFMT_INDEX16);
84     FMT_TO_STR(WINED3DFMT_INDEX32);
85     FMT_TO_STR(WINED3DFMT_Q16W16V16U16);
86     FMT_TO_STR(WINED3DFMT_R16F);
87     FMT_TO_STR(WINED3DFMT_G16R16F);
88     FMT_TO_STR(WINED3DFMT_A16B16G16R16F);
89     FMT_TO_STR(WINED3DFMT_R32F);
90     FMT_TO_STR(WINED3DFMT_G32R32F);
91     FMT_TO_STR(WINED3DFMT_A32B32G32R32F);
92     FMT_TO_STR(WINED3DFMT_CxV8U8);
93 #undef FMT_TO_STR
94   default:
95     FIXME("Unrecognized %u D3DFORMAT!\n", fmt);
96     return "unrecognized";
97   }
98 }
99
100 const char* debug_d3ddevicetype(D3DDEVTYPE devtype) {
101   switch (devtype) {
102 #define DEVTYPE_TO_STR(dev) case dev: return #dev
103     DEVTYPE_TO_STR(D3DDEVTYPE_HAL);
104     DEVTYPE_TO_STR(D3DDEVTYPE_REF);
105     DEVTYPE_TO_STR(D3DDEVTYPE_SW);
106 #undef DEVTYPE_TO_STR
107   default:
108     FIXME("Unrecognized %u D3DDEVTYPE!\n", devtype);
109     return "unrecognized";
110   }
111 }
112
113 const char* debug_d3dusage(DWORD usage) {
114   switch (usage) {
115 #define WINED3DUSAGE_TO_STR(u) case u: return #u
116     WINED3DUSAGE_TO_STR(WINED3DUSAGE_RENDERTARGET);
117     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DEPTHSTENCIL);
118     WINED3DUSAGE_TO_STR(WINED3DUSAGE_WRITEONLY);
119     WINED3DUSAGE_TO_STR(WINED3DUSAGE_SOFTWAREPROCESSING);
120     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DONOTCLIP);
121     WINED3DUSAGE_TO_STR(WINED3DUSAGE_POINTS);
122     WINED3DUSAGE_TO_STR(WINED3DUSAGE_RTPATCHES);
123     WINED3DUSAGE_TO_STR(WINED3DUSAGE_NPATCHES);
124     WINED3DUSAGE_TO_STR(WINED3DUSAGE_DYNAMIC);
125 #undef WINED3DUSAGE_TO_STR
126   case 0: return "none";
127   default:
128     FIXME("Unrecognized %lu Usage!\n", usage);
129     return "unrecognized";
130   }
131 }
132
133 const char* debug_d3dresourcetype(D3DRESOURCETYPE res) {
134   switch (res) {
135 #define RES_TO_STR(res) case res: return #res;
136     RES_TO_STR(D3DRTYPE_SURFACE);
137     RES_TO_STR(D3DRTYPE_VOLUME);
138     RES_TO_STR(D3DRTYPE_TEXTURE);
139     RES_TO_STR(D3DRTYPE_VOLUMETEXTURE);
140     RES_TO_STR(D3DRTYPE_CUBETEXTURE);
141     RES_TO_STR(D3DRTYPE_VERTEXBUFFER);
142     RES_TO_STR(D3DRTYPE_INDEXBUFFER);
143 #undef  RES_TO_STR
144   default:
145     FIXME("Unrecognized %u D3DRESOURCETYPE!\n", res);
146     return "unrecognized";
147   }
148 }
149
150 const char* debug_d3dprimitivetype(D3DPRIMITIVETYPE PrimitiveType) {
151   switch (PrimitiveType) {
152 #define PRIM_TO_STR(prim) case prim: return #prim;
153     PRIM_TO_STR(D3DPT_POINTLIST);
154     PRIM_TO_STR(D3DPT_LINELIST);
155     PRIM_TO_STR(D3DPT_LINESTRIP);
156     PRIM_TO_STR(D3DPT_TRIANGLELIST);
157     PRIM_TO_STR(D3DPT_TRIANGLESTRIP);
158     PRIM_TO_STR(D3DPT_TRIANGLEFAN);
159 #undef  PRIM_TO_STR
160   default:
161     FIXME("Unrecognized %u D3DPRIMITIVETYPE!\n", PrimitiveType);
162     return "unrecognized";
163   }
164 }
165
166 const char* debug_d3drenderstate(DWORD state) {
167   switch (state) {
168 #define D3DSTATE_TO_STR(u) case u: return #u
169     D3DSTATE_TO_STR(WINED3DRS_ZENABLE                   );
170     D3DSTATE_TO_STR(WINED3DRS_FILLMODE                  );
171     D3DSTATE_TO_STR(WINED3DRS_SHADEMODE                 );
172     D3DSTATE_TO_STR(WINED3DRS_LINEPATTERN               );
173     D3DSTATE_TO_STR(WINED3DRS_ZWRITEENABLE              );
174     D3DSTATE_TO_STR(WINED3DRS_ALPHATESTENABLE           );
175     D3DSTATE_TO_STR(WINED3DRS_LASTPIXEL                 );
176     D3DSTATE_TO_STR(WINED3DRS_SRCBLEND                  );
177     D3DSTATE_TO_STR(WINED3DRS_DESTBLEND                 );
178     D3DSTATE_TO_STR(WINED3DRS_CULLMODE                  );
179     D3DSTATE_TO_STR(WINED3DRS_ZFUNC                     );
180     D3DSTATE_TO_STR(WINED3DRS_ALPHAREF                  );
181     D3DSTATE_TO_STR(WINED3DRS_ALPHAFUNC                 );
182     D3DSTATE_TO_STR(WINED3DRS_DITHERENABLE              );
183     D3DSTATE_TO_STR(WINED3DRS_ALPHABLENDENABLE          );
184     D3DSTATE_TO_STR(WINED3DRS_FOGENABLE                 );
185     D3DSTATE_TO_STR(WINED3DRS_SPECULARENABLE            );
186     D3DSTATE_TO_STR(WINED3DRS_ZVISIBLE                  );
187     D3DSTATE_TO_STR(WINED3DRS_FOGCOLOR                  );
188     D3DSTATE_TO_STR(WINED3DRS_FOGTABLEMODE              );
189     D3DSTATE_TO_STR(WINED3DRS_FOGSTART                  );
190     D3DSTATE_TO_STR(WINED3DRS_FOGEND                    );
191     D3DSTATE_TO_STR(WINED3DRS_FOGDENSITY                );
192     D3DSTATE_TO_STR(WINED3DRS_EDGEANTIALIAS             );
193     D3DSTATE_TO_STR(WINED3DRS_ZBIAS                     );
194     D3DSTATE_TO_STR(WINED3DRS_RANGEFOGENABLE            );
195     D3DSTATE_TO_STR(WINED3DRS_STENCILENABLE             );
196     D3DSTATE_TO_STR(WINED3DRS_STENCILFAIL               );
197     D3DSTATE_TO_STR(WINED3DRS_STENCILZFAIL              );
198     D3DSTATE_TO_STR(WINED3DRS_STENCILPASS               );
199     D3DSTATE_TO_STR(WINED3DRS_STENCILFUNC               );
200     D3DSTATE_TO_STR(WINED3DRS_STENCILREF                );
201     D3DSTATE_TO_STR(WINED3DRS_STENCILMASK               );
202     D3DSTATE_TO_STR(WINED3DRS_STENCILWRITEMASK          );
203     D3DSTATE_TO_STR(WINED3DRS_TEXTUREFACTOR             );
204     D3DSTATE_TO_STR(WINED3DRS_WRAP0                     );
205     D3DSTATE_TO_STR(WINED3DRS_WRAP1                     );
206     D3DSTATE_TO_STR(WINED3DRS_WRAP2                     );
207     D3DSTATE_TO_STR(WINED3DRS_WRAP3                     );
208     D3DSTATE_TO_STR(WINED3DRS_WRAP4                     );
209     D3DSTATE_TO_STR(WINED3DRS_WRAP5                     );
210     D3DSTATE_TO_STR(WINED3DRS_WRAP6                     );
211     D3DSTATE_TO_STR(WINED3DRS_WRAP7                     );
212     D3DSTATE_TO_STR(WINED3DRS_CLIPPING                  );
213     D3DSTATE_TO_STR(WINED3DRS_LIGHTING                  );
214     D3DSTATE_TO_STR(WINED3DRS_AMBIENT                   );
215     D3DSTATE_TO_STR(WINED3DRS_FOGVERTEXMODE             );
216     D3DSTATE_TO_STR(WINED3DRS_COLORVERTEX               );
217     D3DSTATE_TO_STR(WINED3DRS_LOCALVIEWER               );
218     D3DSTATE_TO_STR(WINED3DRS_NORMALIZENORMALS          );
219     D3DSTATE_TO_STR(WINED3DRS_DIFFUSEMATERIALSOURCE     );
220     D3DSTATE_TO_STR(WINED3DRS_SPECULARMATERIALSOURCE    );
221     D3DSTATE_TO_STR(WINED3DRS_AMBIENTMATERIALSOURCE     );
222     D3DSTATE_TO_STR(WINED3DRS_EMISSIVEMATERIALSOURCE    );
223     D3DSTATE_TO_STR(WINED3DRS_VERTEXBLEND               );
224     D3DSTATE_TO_STR(WINED3DRS_CLIPPLANEENABLE           );
225     D3DSTATE_TO_STR(WINED3DRS_SOFTWAREVERTEXPROCESSING  );
226     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE                 );
227     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MIN             );
228     D3DSTATE_TO_STR(WINED3DRS_POINTSPRITEENABLE         );
229     D3DSTATE_TO_STR(WINED3DRS_POINTSCALEENABLE          );
230     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_A              );
231     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_B              );
232     D3DSTATE_TO_STR(WINED3DRS_POINTSCALE_C              );
233     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEANTIALIAS      );
234     D3DSTATE_TO_STR(WINED3DRS_MULTISAMPLEMASK           );
235     D3DSTATE_TO_STR(WINED3DRS_PATCHEDGESTYLE            );
236     D3DSTATE_TO_STR(WINED3DRS_PATCHSEGMENTS             );
237     D3DSTATE_TO_STR(WINED3DRS_DEBUGMONITORTOKEN         );
238     D3DSTATE_TO_STR(WINED3DRS_POINTSIZE_MAX             );
239     D3DSTATE_TO_STR(WINED3DRS_INDEXEDVERTEXBLENDENABLE  );
240     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE          );
241     D3DSTATE_TO_STR(WINED3DRS_TWEENFACTOR               );
242     D3DSTATE_TO_STR(WINED3DRS_BLENDOP                   );
243     D3DSTATE_TO_STR(WINED3DRS_POSITIONDEGREE            );
244     D3DSTATE_TO_STR(WINED3DRS_NORMALDEGREE              );
245     D3DSTATE_TO_STR(WINED3DRS_SCISSORTESTENABLE         );
246     D3DSTATE_TO_STR(WINED3DRS_SLOPESCALEDEPTHBIAS       );
247     D3DSTATE_TO_STR(WINED3DRS_ANTIALIASEDLINEENABLE     );
248     D3DSTATE_TO_STR(WINED3DRS_MINTESSELLATIONLEVEL      );
249     D3DSTATE_TO_STR(WINED3DRS_MAXTESSELLATIONLEVEL      );
250     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_X            );
251     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Y            );
252     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_Z            );
253     D3DSTATE_TO_STR(WINED3DRS_ADAPTIVETESS_W            );
254     D3DSTATE_TO_STR(WINED3DRS_ENABLEADAPTIVETESSELLATION);
255     D3DSTATE_TO_STR(WINED3DRS_TWOSIDEDSTENCILMODE       );
256     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFAIL           );
257     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILZFAIL          );
258     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILPASS           );
259     D3DSTATE_TO_STR(WINED3DRS_CCW_STENCILFUNC           );
260     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE1         );
261     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE2         );
262     D3DSTATE_TO_STR(WINED3DRS_COLORWRITEENABLE3         );
263     D3DSTATE_TO_STR(WINED3DRS_BLENDFACTOR               );
264     D3DSTATE_TO_STR(WINED3DRS_SRGBWRITEENABLE           );
265     D3DSTATE_TO_STR(WINED3DRS_DEPTHBIAS                 );
266     D3DSTATE_TO_STR(WINED3DRS_WRAP8                     );
267     D3DSTATE_TO_STR(WINED3DRS_WRAP9                     );
268     D3DSTATE_TO_STR(WINED3DRS_WRAP10                    );
269     D3DSTATE_TO_STR(WINED3DRS_WRAP11                    );
270     D3DSTATE_TO_STR(WINED3DRS_WRAP12                    );
271     D3DSTATE_TO_STR(WINED3DRS_WRAP13                    );
272     D3DSTATE_TO_STR(WINED3DRS_WRAP14                    );
273     D3DSTATE_TO_STR(WINED3DRS_WRAP15                    );
274     D3DSTATE_TO_STR(WINED3DRS_SEPARATEALPHABLENDENABLE  );
275     D3DSTATE_TO_STR(WINED3DRS_SRCBLENDALPHA             );
276     D3DSTATE_TO_STR(WINED3DRS_DESTBLENDALPHA            );
277     D3DSTATE_TO_STR(WINED3DRS_BLENDOPALPHA              );
278 #undef D3DSTATE_TO_STR
279   default:
280     FIXME("Unrecognized %lu render state!\n", state);
281     return "unrecognized";
282   }
283 }
284
285 const char* debug_d3dsamplerstate(DWORD state) {
286   switch (state) {
287 #define D3DSTATE_TO_STR(u) case u: return #u
288     D3DSTATE_TO_STR(WINED3DSAMP_BORDERCOLOR  );
289     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSU     );
290     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSV     );
291     D3DSTATE_TO_STR(WINED3DSAMP_ADDRESSW     );
292     D3DSTATE_TO_STR(WINED3DSAMP_MAGFILTER    );
293     D3DSTATE_TO_STR(WINED3DSAMP_MINFILTER    );
294     D3DSTATE_TO_STR(WINED3DSAMP_MIPFILTER    );
295     D3DSTATE_TO_STR(WINED3DSAMP_MIPMAPLODBIAS);
296     D3DSTATE_TO_STR(WINED3DSAMP_MAXMIPLEVEL  );
297     D3DSTATE_TO_STR(WINED3DSAMP_MAXANISOTROPY);
298     D3DSTATE_TO_STR(WINED3DSAMP_SRGBTEXTURE  );
299     D3DSTATE_TO_STR(WINED3DSAMP_ELEMENTINDEX );
300     D3DSTATE_TO_STR(WINED3DSAMP_DMAPOFFSET   );
301 #undef D3DSTATE_TO_STR
302   default:
303     FIXME("Unrecognized %lu sampler state!\n", state);
304     return "unrecognized";
305   }
306 }
307
308 const char* debug_d3dtexturestate(DWORD state) {
309   switch (state) {
310 #define D3DSTATE_TO_STR(u) case u: return #u
311     D3DSTATE_TO_STR(WINED3DTSS_COLOROP               );
312     D3DSTATE_TO_STR(WINED3DTSS_COLORARG1             );
313     D3DSTATE_TO_STR(WINED3DTSS_COLORARG2             );
314     D3DSTATE_TO_STR(WINED3DTSS_ALPHAOP               );
315     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG1             );
316     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG2             );
317     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT00          );
318     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT01          );
319     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT10          );
320     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVMAT11          );
321     D3DSTATE_TO_STR(WINED3DTSS_TEXCOORDINDEX         );
322     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLSCALE         );
323     D3DSTATE_TO_STR(WINED3DTSS_BUMPENVLOFFSET        );
324     D3DSTATE_TO_STR(WINED3DTSS_TEXTURETRANSFORMFLAGS );
325     D3DSTATE_TO_STR(WINED3DTSS_ADDRESSW              );
326     D3DSTATE_TO_STR(WINED3DTSS_COLORARG0             );
327     D3DSTATE_TO_STR(WINED3DTSS_ALPHAARG0             );
328     D3DSTATE_TO_STR(WINED3DTSS_RESULTARG             );
329     D3DSTATE_TO_STR(WINED3DTSS_CONSTANT              );
330 #undef D3DSTATE_TO_STR
331   case 12:
332     /* Note D3DTSS are not consecutive, so skip these */
333     return "unused";
334     break;
335   default:
336     FIXME("Unrecognized %lu texture state!\n", state);
337     return "unrecognized";
338   }
339 }
340
341 const char* debug_d3dpool(D3DPOOL Pool) {
342   switch (Pool) {
343 #define POOL_TO_STR(p) case p: return #p;
344     POOL_TO_STR(D3DPOOL_DEFAULT);
345     POOL_TO_STR(D3DPOOL_MANAGED);
346     POOL_TO_STR(D3DPOOL_SYSTEMMEM);
347     POOL_TO_STR(D3DPOOL_SCRATCH);
348 #undef  POOL_TO_STR
349   default:
350     FIXME("Unrecognized %u D3DPOOL!\n", Pool);
351     return "unrecognized";
352   }
353 }
354 /*****************************************************************************
355  * Useful functions mapping GL <-> D3D values
356  */
357 GLenum StencilOp(DWORD op) {
358     switch(op) {
359     case D3DSTENCILOP_KEEP    : return GL_KEEP;
360     case D3DSTENCILOP_ZERO    : return GL_ZERO;
361     case D3DSTENCILOP_REPLACE : return GL_REPLACE;
362     case D3DSTENCILOP_INCRSAT : return GL_INCR;
363     case D3DSTENCILOP_DECRSAT : return GL_DECR;
364     case D3DSTENCILOP_INVERT  : return GL_INVERT;
365     case D3DSTENCILOP_INCR    : return GL_INCR_WRAP_EXT;
366     case D3DSTENCILOP_DECR    : return GL_DECR_WRAP_EXT;
367     default:
368         FIXME("Invalid stencil op %ld\n", op);
369         return GL_ALWAYS;
370     }
371 }
372
373 /* Set texture operations up - The following avoids lots of ifdefs in this routine!*/
374 #if defined (GL_VERSION_1_3)
375 # define useext(A) A
376 # define combine_ext 1
377 #elif defined (GL_EXT_texture_env_combine)
378 # define useext(A) A##_EXT
379 # define combine_ext 1
380 #elif defined (GL_ARB_texture_env_combine)
381 # define useext(A) A##_ARB
382 # define combine_ext 1
383 #else
384 # undef combine_ext
385 #endif
386
387 #if !defined(combine_ext)
388 void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
389 {
390         FIXME("Requires opengl combine extensions to work\n");
391         return;
392 }
393 #else
394 /* Setup the texture operations texture stage states */
395 void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, D3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
396 {
397 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
398         GLenum src1, src2, src3;
399         GLenum opr1, opr2, opr3;
400         GLenum comb_target;
401         GLenum src0_target, src1_target, src2_target;
402         GLenum opr0_target, opr1_target, opr2_target;
403         GLenum scal_target;
404         GLenum opr=0, invopr, src3_target, opr3_target;
405         BOOL Handled = FALSE;
406         IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
407
408         TRACE("Alpha?(%d), Stage:%d Op(%d), a1(%ld), a2(%ld), a3(%ld)\n", isAlpha, Stage, op, arg1, arg2, arg3);
409
410         ENTER_GL();
411
412         /* Note: Operations usually involve two ars, src0 and src1 and are operations of
413            the form (a1 <operation> a2). However, some of the more complex operations
414            take 3 parameters. Instead of the (sensible) addition of a3, Microsoft added
415            in a third parameter called a0. Therefore these are operations of the form
416            a0 <operation> a1 <operation> a2, ie the new parameter goes to the front.
417
418            However, below we treat the new (a0) parameter as src2/opr2, so in the actual
419            functions below, expect their syntax to differ slightly to those listed in the
420            manuals, ie replace arg1 with arg3, arg2 with arg1 and arg3 with arg2
421            This affects D3DTOP_MULTIPLYADD and D3DTOP_LERP                               */
422
423         if (isAlpha) {
424                 comb_target = useext(GL_COMBINE_ALPHA);
425                 src0_target = useext(GL_SOURCE0_ALPHA);
426                 src1_target = useext(GL_SOURCE1_ALPHA);
427                 src2_target = useext(GL_SOURCE2_ALPHA);
428                 opr0_target = useext(GL_OPERAND0_ALPHA);
429                 opr1_target = useext(GL_OPERAND1_ALPHA);
430                 opr2_target = useext(GL_OPERAND2_ALPHA);
431                 scal_target = GL_ALPHA_SCALE;
432         }
433         else {
434                 comb_target = useext(GL_COMBINE_RGB);
435                 src0_target = useext(GL_SOURCE0_RGB);
436                 src1_target = useext(GL_SOURCE1_RGB);
437                 src2_target = useext(GL_SOURCE2_RGB);
438                 opr0_target = useext(GL_OPERAND0_RGB);
439                 opr1_target = useext(GL_OPERAND1_RGB);
440                 opr2_target = useext(GL_OPERAND2_RGB);
441                 scal_target = useext(GL_RGB_SCALE);
442         }
443
444         /* From MSDN (WINED3DTSS_ALPHAARG1) :
445            The default argument is D3DTA_TEXTURE. If no texture is set for this stage,
446                    then the default argument is D3DTA_DIFFUSE.
447                    FIXME? If texture added/removed, may need to reset back as well?    */
448         if (isAlpha && This->stateBlock->textures[Stage] == NULL && arg1 == D3DTA_TEXTURE) {
449             GetSrcAndOpFromValue(D3DTA_DIFFUSE, isAlpha, &src1, &opr1);
450         } else {
451             GetSrcAndOpFromValue(arg1, isAlpha, &src1, &opr1);
452         }
453         GetSrcAndOpFromValue(arg2, isAlpha, &src2, &opr2);
454         GetSrcAndOpFromValue(arg3, isAlpha, &src3, &opr3);
455
456         TRACE("ct(%x), 1:(%x,%x), 2:(%x,%x), 3:(%x,%x)\n", comb_target, src1, opr1, src2, opr2, src3, opr3);
457
458         Handled = TRUE; /* Assume will be handled */
459
460         /* Other texture operations require special extensions: */
461         if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
462           if (isAlpha) {
463             opr = GL_SRC_ALPHA;
464             invopr = GL_ONE_MINUS_SRC_ALPHA;
465             src3_target = GL_SOURCE3_ALPHA_NV;
466             opr3_target = GL_OPERAND3_ALPHA_NV;
467           } else {
468             opr = GL_SRC_COLOR;
469             invopr = GL_ONE_MINUS_SRC_COLOR;
470             src3_target = GL_SOURCE3_RGB_NV;
471             opr3_target = GL_OPERAND3_RGB_NV;
472           }
473           switch (op) {
474           case D3DTOP_DISABLE: /* Only for alpha */
475             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
476             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
477             glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
478             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
479             glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
480             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
481             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
482             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
483             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
484             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
485             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
486             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
487             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
488             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
489             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
490             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
491             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
492             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
493             break;
494           case D3DTOP_SELECTARG1:                                          /* = a1 * 1 + 0 * 0 */
495           case D3DTOP_SELECTARG2:                                          /* = a2 * 1 + 0 * 0 */
496             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
497             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
498             if (op == D3DTOP_SELECTARG1) {
499               glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
500               checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
501               glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
502               checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
503             } else {
504               glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
505               checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
506               glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
507               checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
508             }
509             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
510             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
511             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
512             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
513             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
514             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
515             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
516             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
517             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
518             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
519             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
520             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
521             break;
522
523           case D3DTOP_MODULATE:
524             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
525             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
526             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
527             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
528             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
529             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
530             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
531             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
532             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
533             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
534             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
535             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
536             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
537             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
538             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
539             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
540             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
541             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
542             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
543             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
544             break;
545           case D3DTOP_MODULATE2X:
546             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
547             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
548             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
549             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
550             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
551             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
552             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
553             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
554             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
555             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
556             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
557             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
558             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
559             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
560             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
561             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
562             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
563             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
564             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
565             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
566             break;
567           case D3DTOP_MODULATE4X:
568             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
569             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
570             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
571             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
572             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
573             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
574             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
575             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
576             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
577             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
578             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
579             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
580             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
581             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
582             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
583             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
584             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
585             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
586             glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
587             checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
588             break;
589
590           case D3DTOP_ADD:
591             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
592             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
593             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
594             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
595             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
596             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
597             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
598             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
599             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
600             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
601             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
602             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
603             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
604             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
605             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
606             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
607             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
608             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
609             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
610             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
611             break;
612
613           case D3DTOP_ADDSIGNED:
614             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
615             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
616             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
617             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
618             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
619             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
620             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
621             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
622             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
623             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
624             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
625             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
626             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
627             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
628             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
629             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
630             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
631             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
632             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
633             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
634             break;
635
636           case D3DTOP_ADDSIGNED2X:
637             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
638             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
639             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
640             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
641             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
642             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
643             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
644             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
645             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
646             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
647             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
648             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
649             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
650             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
651             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
652             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
653             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
654             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
655             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
656             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
657             break;
658
659           case D3DTOP_ADDSMOOTH:
660             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
661             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
662             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
663             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
664             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
665             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
666             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
667             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
668             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
669             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
670             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
671             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
672             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
673             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
674             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
675             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
676             switch (opr1) {
677             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
678             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
679             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
680             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
681             }
682             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
683             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
684             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
685             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
686             break;
687
688           case D3DTOP_BLENDDIFFUSEALPHA:
689             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
690             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
691             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
692             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
693             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
694             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
695             glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR));
696             checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR)");
697             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
698             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
699             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
700             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
701             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
702             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
703             glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR));
704             checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR)");
705             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
706             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
707             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
708             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
709             break;
710           case D3DTOP_BLENDTEXTUREALPHA:
711             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
712             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
713             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
714             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
715             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
716             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
717             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_TEXTURE);
718             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_TEXTURE");
719             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
720             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
721             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
722             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
723             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
724             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
725             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
726             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
727             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
728             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
729             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
730             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
731             break;
732           case D3DTOP_BLENDFACTORALPHA:
733             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
734             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
735             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
736             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
737             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
738             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
739             glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT));
740             checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT)");
741             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
742             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
743             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
744             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
745             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
746             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
747             glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT));
748             checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT)");
749             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
750             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
751             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
752             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
753             break;
754           case D3DTOP_BLENDTEXTUREALPHAPM:
755             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
756             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
757             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
758             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
759             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
760             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
761             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
762             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
763             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
764             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
765             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
766             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
767             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
768             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
769             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
770             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
771             glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
772             checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
773             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
774             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
775             break;
776           case D3DTOP_MODULATEALPHA_ADDCOLOR:
777             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
778             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");  /* Add = a0*a1 + a2*a3 */
779             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);        /*   a0 = src1/opr1    */
780             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
781             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
782             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");    /*   a1 = 1 (see docs) */
783             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
784             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
785             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
786             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
787             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);        /*   a2 = arg2         */
788             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
789             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
790             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");     /*  a3 = src1 alpha   */
791             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
792             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
793             switch (opr) {
794             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
795             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
796             }
797             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
798             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
799             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
800             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
801             break;
802           case D3DTOP_MODULATECOLOR_ADDALPHA:
803             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
804             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
805             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
806             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
807             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
808             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
809             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
810             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
811             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
812             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
813             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
814             checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
815             switch (opr1) {
816             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
817             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
818             }
819             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
820             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
821             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
822             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
823             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
824             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
825             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
826             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
827             break;
828           case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
829             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
830             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
831             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
832             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
833             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
834             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
835             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
836             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
837             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
838             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
839             glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
840             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
841             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
842             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
843             glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
844             checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
845             switch (opr1) {
846             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
847             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
848             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
849             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
850             }
851             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
852             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
853             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
854             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
855             break;
856           case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
857             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
858             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
859             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
860             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
861             switch (opr1) {
862             case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
863             case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
864             case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
865             case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
866             }
867             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
868             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
869             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
870             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
871             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
872             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
873             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
874             checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
875             switch (opr1) {
876             case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
877             case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
878             }
879             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
880             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
881             glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
882             checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
883             glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
884             checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
885             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
886             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
887             break;
888           case D3DTOP_MULTIPLYADD:
889             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
890             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
891             glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
892             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
893             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
894             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
895             glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
896             checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
897             glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
898             checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
899             glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
900             checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
901             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr1);
902             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
903             glTexEnvi(GL_TEXTURE_ENV, src3_target, src2);
904             checkGLcall("GL_TEXTURE_ENV, src3_target, src3");
905             glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr2);
906             checkGLcall("GL_TEXTURE_ENV, opr3_target, opr3");
907             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
908             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
909             break;
910
911           case D3DTOP_BUMPENVMAP:
912             {
913               if (GL_SUPPORT(NV_TEXTURE_SHADER)) {
914                 /*
915                   texture unit 0: GL_TEXTURE_2D
916                   texture unit 1: GL_DOT_PRODUCT_NV
917                   texture unit 2: GL_DOT_PRODUCT_DIFFUSE_CUBE_MAP_NV
918                   texture unit 3: GL_DOT_PRODUCT_REFLECT_CUBE_MAP_NV
919                 */
920                 float m[2][2];
921
922                 union {
923                   float f;
924                   DWORD d;
925                 } tmpvalue;
926
927                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT00];
928                 m[0][0] = tmpvalue.f;
929                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT01];
930                 m[0][1] = tmpvalue.f;
931                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT10];
932                 m[1][0] = tmpvalue.f;
933                 tmpvalue.d = This->stateBlock->textureState[Stage][WINED3DTSS_BUMPENVMAT11];
934                 m[1][1] = tmpvalue.f;
935
936                 /*FIXME("Stage %d matrix is (%.2f,%.2f),(%.2f,%.2f)\n", Stage, m[0][0], m[0][1], m[1][0], m[1][0]);*/
937
938                 if (FALSE == This->texture_shader_active) {
939                   This->texture_shader_active = TRUE;
940                   glEnable(GL_TEXTURE_SHADER_NV);
941                 }
942
943                 /*
944                 glEnable(GL_REGISTER_COMBINERS_NV);
945                 glCombinerParameteriNV (GL_NUM_GENERAL_COMBINERS_NV, 1);
946                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV, GL_TEXTURE0_ARB, GL_SIGNED_IDENTITY_NV, GL_RGB);
947                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_RGB);
948                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV, GL_TEXTURE2_ARB, GL_SIGNED_IDENTITY_NV, GL_RGB);
949                 glCombinerInputNV (GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_RGB);
950                 glCombinerOutputNV (GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV, GL_PRIMARY_COLOR_NV, 0, 0, 0, 0, 0);
951                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_A_NV, GL_TEXTURE0_ARB, GL_SIGNED_IDENTITY_NV, GL_ALPHA);
952                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_B_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_ALPHA);
953                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_C_NV, GL_TEXTURE2_ARB, GL_SIGNED_IDENTITY_NV, GL_ALPHA);
954                 glCombinerInputNV (GL_COMBINER0_NV, GL_ALPHA, GL_VARIABLE_D_NV, GL_NONE, GL_UNSIGNED_INVERT_NV, GL_ALPHA);
955                 glCombinerOutputNV (GL_COMBINER0_NV, GL_ALPHA, GL_DISCARD_NV, GL_DISCARD_NV, GL_PRIMARY_COLOR_NV, 0, 0, 0, 0, 0);
956                 glDisable (GL_PER_STAGE_CONSTANTS_NV);
957                 glCombinerParameteriNV (GL_COLOR_SUM_CLAMP_NV, 0);
958                 glFinalCombinerInputNV (GL_VARIABLE_A_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
959                 glFinalCombinerInputNV (GL_VARIABLE_B_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
960                 glFinalCombinerInputNV (GL_VARIABLE_C_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
961                 glFinalCombinerInputNV (GL_VARIABLE_D_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
962                 glFinalCombinerInputNV (GL_VARIABLE_E_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
963                 glFinalCombinerInputNV (GL_VARIABLE_F_NV, 0, GL_UNSIGNED_IDENTITY_NV, GL_RGB);
964                 glFinalCombinerInputNV (GL_VARIABLE_G_NV, GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA);
965                 */
966                 /*
967                 int i;
968                 for (i = 0; i < Stage; i++) {
969                   if (GL_SUPPORT(ARB_MULTITEXTURE)) {
970                     GL_ACTIVETEXTURE(i);
971                     glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
972                     checkGLcall("Activate texture..");
973                   } else if (i>0) {
974                     FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
975                   }
976                 }
977                 */
978                 /*
979                   GL_ACTIVETEXTURE(Stage - 1);
980                   glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
981                 */
982                 /*
983                 GL_ACTIVETEXTURE(Stage);
984                 checkGLcall("Activate texture.. to update const color");
985                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
986                 checkGLcall("glTexEnv");
987                 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + Stage - 1);
988                 checkGLcall("glTexEnv");
989                 glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, (float*)&m[0]);
990                 checkGLcall("glTexEnv");
991                 */
992                 LEAVE_GL();
993                 return;
994                 break;
995               }
996             }
997
998           case D3DTOP_BUMPENVMAPLUMINANCE:
999
1000           default:
1001             Handled = FALSE;
1002           }
1003           if (Handled) {
1004             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
1005             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV");
1006
1007             LEAVE_GL();
1008             return;
1009           }
1010         } /* GL_NV_texture_env_combine4 */
1011
1012         Handled = TRUE; /* Again, assume handled */
1013         switch (op) {
1014         case D3DTOP_DISABLE: /* Only for alpha */
1015                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1016                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1017                 glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
1018                 checkGLcall("GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT");
1019                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
1020                 checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA");
1021                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1022                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1023                 break;
1024         case D3DTOP_SELECTARG1:
1025                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1026                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1027                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1028                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1029                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1030                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1031                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1032                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1033                 break;
1034         case D3DTOP_SELECTARG2:
1035                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
1036                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1037                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
1038                 checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
1039                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
1040                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
1041                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1042                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1043                 break;
1044         case D3DTOP_MODULATE:
1045                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1046                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1047                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1048                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1049                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1050                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1051                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1052                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1053                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1054                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1055                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1056                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1057                 break;
1058         case D3DTOP_MODULATE2X:
1059                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1060                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1061                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1062                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1063                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1064                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1065                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1066                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1067                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1068                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1069                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1070                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1071                 break;
1072         case D3DTOP_MODULATE4X:
1073                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
1074                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
1075                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1076                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1077                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1078                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1079                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1080                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1081                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1082                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1083                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
1084                 checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
1085                 break;
1086         case D3DTOP_ADD:
1087                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1088                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
1089                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1090                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1091                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1092                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1093                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1094                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1095                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1096                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1097                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1098                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1099                 break;
1100         case D3DTOP_ADDSIGNED:
1101                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1102                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext((GL_ADD_SIGNED)");
1103                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1104                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1105                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1106                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1107                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1108                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1109                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1110                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1111                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1112                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1113                 break;
1114         case D3DTOP_ADDSIGNED2X:
1115                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
1116                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
1117                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1118                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1119                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1120                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1121                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1122                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1123                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1124                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1125                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
1126                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
1127                 break;
1128         case D3DTOP_SUBTRACT:
1129           if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE)) {
1130                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_SUBTRACT);
1131                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_SUBTRACT)");
1132                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1133                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1134                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1135                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1136                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1137                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1138                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1139                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1140                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1141                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1142           } else {
1143                 FIXME("This version of opengl does not support GL_SUBTRACT\n");
1144           }
1145           break;
1146
1147         case D3DTOP_BLENDDIFFUSEALPHA:
1148                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1149                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1150                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1151                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1152                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1153                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1154                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1155                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1156                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1157                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1158                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PRIMARY_COLOR));
1159                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PRIMARY_COLOR");
1160                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1161                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1162                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1163                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1164                 break;
1165         case D3DTOP_BLENDTEXTUREALPHA:
1166                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1167                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1168                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1169                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1170                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1171                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1172                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1173                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1174                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1175                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1176                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_TEXTURE);
1177                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_TEXTURE");
1178                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1179                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1180                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1181                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1182                 break;
1183         case D3DTOP_BLENDFACTORALPHA:
1184                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1185                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1186                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1187                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1188                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1189                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1190                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1191                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1192                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1193                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1194                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_CONSTANT));
1195                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_CONSTANT");
1196                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1197                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1198                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1199                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1200                 break;
1201         case D3DTOP_BLENDCURRENTALPHA:
1202                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1203                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1204                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1205                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1206                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1207                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1208                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1209                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1210                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1211                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1212                 glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PREVIOUS));
1213                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PREVIOUS");
1214                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
1215                 checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
1216                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1217                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1218                 break;
1219         case D3DTOP_DOTPRODUCT3:
1220                 if (GL_SUPPORT(ARB_TEXTURE_ENV_DOT3)) {
1221                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB);
1222                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB");
1223                 } else if (GL_SUPPORT(EXT_TEXTURE_ENV_DOT3)) {
1224                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT);
1225                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT");
1226                 } else {
1227                   FIXME("This version of opengl does not support GL_DOT3\n");
1228                 }
1229                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1230                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1231                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1232                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1233                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1234                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1235                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1236                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1237                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1238                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1239                 break;
1240         case D3DTOP_LERP:
1241                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
1242                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
1243                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1244                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1245                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1246                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1247                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
1248                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
1249                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
1250                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
1251                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src3);
1252                 checkGLcall("GL_TEXTURE_ENV, src2_target, src3");
1253                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr3);
1254                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr3");
1255                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1256                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1257                 break;
1258         case D3DTOP_ADDSMOOTH:
1259                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1260                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1261                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1262                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1263                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1264                   switch (opr1) {
1265                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1266                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1267                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1268                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1269                   }
1270                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1271                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1272                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1273                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1274                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
1275                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
1276                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1277                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1278                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1279                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1280                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1281                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1282                 } else
1283                   Handled = FALSE;
1284                 break;
1285         case D3DTOP_BLENDTEXTUREALPHAPM:
1286                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1287                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1288                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1289                   glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_TEXTURE);
1290                   checkGLcall("GL_TEXTURE_ENV, src0_target, GL_TEXTURE");
1291                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_ALPHA);
1292                   checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_APHA");
1293                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1294                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1295                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
1296                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
1297                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1298                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1299                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1300                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1301                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1302                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1303                 } else
1304                   Handled = FALSE;
1305                 break;
1306         case D3DTOP_MODULATEALPHA_ADDCOLOR:
1307                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1308                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1309                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1310                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1311                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1312                   switch (opr1) {
1313                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1314                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1315                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1316                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1317                   }
1318                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1319                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1320                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1321                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1322                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
1323                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
1324                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1325                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1326                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1327                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1328                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1329                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1330                 } else
1331                   Handled = FALSE;
1332                 break;
1333         case D3DTOP_MODULATECOLOR_ADDALPHA:
1334                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1335                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1336                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1337                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1338                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1339                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
1340                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1341                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1342                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1343                   switch (opr1) {
1344                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1345                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1346                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1347                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1348                   }
1349                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
1350                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
1351                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1352                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1353                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1354                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1355                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1356                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1357                 } else
1358                   Handled = FALSE;
1359                 break;
1360         case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
1361                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1362                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1363                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1364                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1365                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1366                   switch (opr1) {
1367                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1368                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1369                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1370                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1371                   }
1372                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1373                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1374                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1375                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1376                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
1377                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
1378                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1379                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1380                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1381                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1382                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1383                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1384                 } else
1385                   Handled = FALSE;
1386                 break;
1387         case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
1388                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1389                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1390                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1391                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
1392                   checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1393                   switch (opr1) {
1394                   case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
1395                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
1396                   case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1397                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1398                   }
1399                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
1400                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
1401                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1402                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1403                   switch (opr1) {
1404                   case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
1405                   case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1406                   case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
1407                   case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
1408                   }
1409                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
1410                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
1411                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1412                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1413                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1414                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1415                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1416                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1417                 } else
1418                   Handled = FALSE;
1419                 break;
1420         case D3DTOP_MULTIPLYADD:
1421                 if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
1422                   glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
1423                   checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
1424                   glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
1425                   checkGLcall("GL_TEXTURE_ENV, src0_target, src3");
1426                   glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
1427                   checkGLcall("GL_TEXTURE_ENV, opr0_target, opr3");
1428                   glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
1429                   checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
1430                   glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
1431                   checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
1432                   glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
1433                   checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
1434                   glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
1435                   checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
1436                   glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
1437                   checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
1438                 } else
1439                   Handled = FALSE;
1440                 break;
1441         default:
1442                 Handled = FALSE;
1443         }
1444
1445         if (Handled) {
1446           BOOL  combineOK = TRUE;
1447           if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1448             DWORD op2;
1449
1450             if (isAlpha) {
1451               op2 = This->stateBlock->textureState[Stage][WINED3DTSS_COLOROP];
1452             } else {
1453               op2 = This->stateBlock->textureState[Stage][WINED3DTSS_ALPHAOP];
1454             }
1455
1456             /* Note: If COMBINE4 in effect can't go back to combine! */
1457             switch (op2) {
1458             case D3DTOP_ADDSMOOTH:
1459             case D3DTOP_BLENDTEXTUREALPHAPM:
1460             case D3DTOP_MODULATEALPHA_ADDCOLOR:
1461             case D3DTOP_MODULATECOLOR_ADDALPHA:
1462             case D3DTOP_MODULATEINVALPHA_ADDCOLOR:
1463             case D3DTOP_MODULATEINVCOLOR_ADDALPHA:
1464             case D3DTOP_MULTIPLYADD:
1465               /* Ignore those implemented in both cases */
1466               switch (op) {
1467               case D3DTOP_SELECTARG1:
1468               case D3DTOP_SELECTARG2:
1469                 combineOK = FALSE;
1470                 Handled   = FALSE;
1471                 break;
1472               default:
1473                 FIXME("Can't use COMBINE4 and COMBINE together, thisop=%d, otherop=%ld, isAlpha(%d)\n", op, op2, isAlpha);
1474                 LEAVE_GL();
1475                 return;
1476               }
1477             }
1478           }
1479
1480           if (combineOK) {
1481             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE));
1482             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE)");
1483
1484             LEAVE_GL();
1485             return;
1486           }
1487         }
1488
1489         LEAVE_GL();
1490
1491         /* After all the extensions, if still unhandled, report fixme */
1492         FIXME("Unhandled texture operation %d\n", op);
1493         #undef GLINFO_LOCATION
1494 }
1495 #endif
1496
1497 /* Setup this textures matrix according to the texture flags*/
1498 void set_texture_matrix(const float *smat, DWORD flags, BOOL calculatedCoords)
1499 {
1500     float mat[16];
1501
1502     glMatrixMode(GL_TEXTURE);
1503     checkGLcall("glMatrixMode(GL_TEXTURE)");
1504
1505     if (flags == D3DTTFF_DISABLE) {
1506         glLoadIdentity();
1507         checkGLcall("glLoadIdentity()");
1508         return;
1509     }
1510
1511     if (flags == (D3DTTFF_COUNT1|D3DTTFF_PROJECTED)) {
1512         ERR("Invalid texture transform flags: D3DTTFF_COUNT1|D3DTTFF_PROJECTED\n");
1513         return;
1514     }
1515
1516     memcpy(mat, smat, 16 * sizeof(float));
1517
1518     switch (flags & ~D3DTTFF_PROJECTED) {
1519     case D3DTTFF_COUNT1: mat[1] = mat[5] = mat[13] = 0;
1520     case D3DTTFF_COUNT2: mat[2] = mat[6] = mat[10] = mat[14] = 0;
1521     default: mat[3] = mat[7] = mat[11] = 0, mat[15] = 1;
1522     }
1523
1524     if (flags & D3DTTFF_PROJECTED) {
1525         switch (flags & ~D3DTTFF_PROJECTED) {
1526         case D3DTTFF_COUNT2:
1527             mat[3] = mat[1], mat[7] = mat[5], mat[11] = mat[9], mat[15] = mat[13];
1528             mat[1] = mat[5] = mat[9] = mat[13] = 0;
1529             break;
1530         case D3DTTFF_COUNT3:
1531             mat[3] = mat[2], mat[7] = mat[6], mat[11] = mat[10], mat[15] = mat[14];
1532             mat[2] = mat[6] = mat[10] = mat[14] = 0;
1533             break;
1534         }
1535     } else if(!calculatedCoords) { /* under directx the R/Z coord can be used for translation, under opengl we use the Q coord instead */
1536         mat[12] = mat[8];
1537         mat[13] = mat[9];
1538     }
1539
1540     glLoadMatrixf(mat);
1541     checkGLcall("glLoadMatrixf(mat)");
1542 }
1543
1544 void GetSrcAndOpFromValue(DWORD iValue, BOOL isAlphaArg, GLenum* source, GLenum* operand)
1545 {
1546   BOOL isAlphaReplicate = FALSE;
1547   BOOL isComplement     = FALSE;
1548
1549   *operand = GL_SRC_COLOR;
1550   *source = GL_TEXTURE;
1551
1552   /* Catch alpha replicate */
1553   if (iValue & D3DTA_ALPHAREPLICATE) {
1554     iValue = iValue & ~D3DTA_ALPHAREPLICATE;
1555     isAlphaReplicate = TRUE;
1556   }
1557
1558   /* Catch Complement */
1559   if (iValue & D3DTA_COMPLEMENT) {
1560     iValue = iValue & ~D3DTA_COMPLEMENT;
1561     isComplement = TRUE;
1562   }
1563
1564   /* Calculate the operand */
1565   if (isAlphaReplicate && !isComplement) {
1566     *operand = GL_SRC_ALPHA;
1567   } else if (isAlphaReplicate && isComplement) {
1568     *operand = GL_ONE_MINUS_SRC_ALPHA;
1569   } else if (isComplement) {
1570     if (isAlphaArg) {
1571       *operand = GL_ONE_MINUS_SRC_ALPHA;
1572     } else {
1573       *operand = GL_ONE_MINUS_SRC_COLOR;
1574     }
1575   } else {
1576     if (isAlphaArg) {
1577       *operand = GL_SRC_ALPHA;
1578     } else {
1579       *operand = GL_SRC_COLOR;
1580     }
1581   }
1582
1583   /* Calculate the source */
1584   switch (iValue & D3DTA_SELECTMASK) {
1585   case D3DTA_CURRENT:   *source  = GL_PREVIOUS_EXT;
1586     break;
1587   case D3DTA_DIFFUSE:   *source  = GL_PRIMARY_COLOR_EXT;
1588     break;
1589   case D3DTA_TEXTURE:   *source  = GL_TEXTURE;
1590     break;
1591   case D3DTA_TFACTOR:   *source  = GL_CONSTANT_EXT;
1592     break;
1593   case D3DTA_SPECULAR:
1594     /*
1595      * According to the GL_ARB_texture_env_combine specs, SPECULAR is
1596      * 'Secondary color' and isn't supported until base GL supports it
1597      * There is no concept of temp registers as far as I can tell
1598      */
1599     FIXME("Unhandled texture arg D3DTA_SPECULAR\n");
1600     *source = GL_TEXTURE;
1601     break;
1602
1603   default:
1604     FIXME("Unrecognized texture arg %ld\n", iValue);
1605     *source = GL_TEXTURE;
1606   }
1607 }
1608
1609
1610 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
1611 GLint D3DFmt2GLIntFmt(IWineD3DDeviceImpl* This, D3DFORMAT fmt) {
1612     GLint retVal = 0;
1613
1614     if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
1615         switch (fmt) {
1616         case WINED3DFMT_DXT1:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
1617         case WINED3DFMT_DXT2:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
1618         case WINED3DFMT_DXT3:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
1619         case WINED3DFMT_DXT4:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
1620         case WINED3DFMT_DXT5:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
1621         default:
1622             /* stupid compiler */
1623             break;
1624         }
1625     }
1626
1627     if (retVal == 0) {
1628         switch (fmt) {
1629             /* Paletted */
1630         case WINED3DFMT_P8:               retVal = GL_COLOR_INDEX8_EXT; break;
1631         case WINED3DFMT_A8P8:             retVal = GL_COLOR_INDEX8_EXT; break;
1632             /* Luminance */
1633         case WINED3DFMT_L8:               retVal = GL_LUMINANCE8; break;
1634         case WINED3DFMT_A8L8:             retVal = GL_LUMINANCE8_ALPHA8; break;
1635         case WINED3DFMT_A4L4:             retVal = GL_LUMINANCE4_ALPHA4; break;
1636             /* Bump */
1637         case WINED3DFMT_V8U8:             retVal = GL_COLOR_INDEX8_EXT; break;
1638         case WINED3DFMT_V16U16:           retVal = GL_COLOR_INDEX; break;
1639         case WINED3DFMT_L6V5U5:           retVal = GL_COLOR_INDEX8_EXT; break;
1640         case WINED3DFMT_X8L8V8U8:         retVal = GL_RGBA8; break;
1641         case WINED3DFMT_Q8W8V8U8:         retVal = GL_RGBA8; break; /* almost but not quite... */
1642         case WINED3DFMT_Q16W16V16U16:     retVal = GL_COLOR_INDEX; break; /* almost but not quite... */
1643             /* color buffer */
1644         case WINED3DFMT_R3G3B2:           retVal = GL_R3_G3_B2; break;
1645         case WINED3DFMT_R5G6B5:           retVal = GL_RGB5; break; /* fixme: internal format 6 for g? */
1646         case WINED3DFMT_R8G8B8:           retVal = GL_RGB8; break;
1647         case WINED3DFMT_A1R5G5B5:         retVal = GL_RGB5_A1; break;
1648         case WINED3DFMT_X1R5G5B5:         retVal = GL_RGB5_A1; break;
1649         case WINED3DFMT_A4R4G4B4:         retVal = GL_RGBA4; break;
1650         case WINED3DFMT_X4R4G4B4:         retVal = GL_RGB4; break;
1651         case WINED3DFMT_A8R8G8B8:         retVal = GL_RGBA8; break;
1652         case WINED3DFMT_A8B8G8R8:         retVal = GL_RGBA8; break;
1653         case WINED3DFMT_A2R10G10B10:      retVal = GL_RGBA8; break;
1654         case WINED3DFMT_X8R8G8B8:         retVal = GL_RGB; break;
1655             /* to see */
1656         case WINED3DFMT_A8:               retVal = GL_ALPHA8; break;
1657
1658         /* Depth + Stencil NOTE: OpenGL doesn't support depth-stencil surfaces so the formats are the closes bits match for the data */
1659         case WINED3DFMT_D24S8:            retVal = GL_COLOR_INDEX; break;
1660         case WINED3DFMT_D24FS8:           retVal = GL_COLOR_INDEX; break;
1661         case WINED3DFMT_D24X8:            retVal = GL_COLOR_INDEX; break;
1662         case WINED3DFMT_D24X4S4:          retVal = GL_COLOR_INDEX; break;
1663         case WINED3DFMT_D32:              retVal = GL_COLOR_INDEX; break;
1664         case WINED3DFMT_D16:              retVal = GL_COLOR_INDEX; break;
1665         case WINED3DFMT_D15S1:            retVal = GL_COLOR_INDEX; break;
1666         case WINED3DFMT_D16_LOCKABLE:     retVal = GL_COLOR_INDEX; break;
1667
1668         default:
1669             FIXME("Unhandled fmt(%u,%s)\n", fmt, debug_d3dformat(fmt));
1670             retVal = GL_RGB8;
1671         }
1672     }
1673     TRACE("fmt2glintFmt for fmt(%u,%s) = %x\n", fmt, debug_d3dformat(fmt), retVal);
1674     return retVal;
1675 }
1676
1677 GLenum D3DFmt2GLFmt(IWineD3DDeviceImpl* This, D3DFORMAT fmt) {
1678     GLenum retVal = 0;
1679
1680     if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
1681         switch (fmt) {
1682         case WINED3DFMT_DXT1:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
1683         case WINED3DFMT_DXT2:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
1684         case WINED3DFMT_DXT3:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
1685         case WINED3DFMT_DXT4:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
1686         case WINED3DFMT_DXT5:             retVal = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
1687         default:
1688             /* stupid compiler */
1689             break;
1690         }
1691     }
1692
1693     if (retVal == 0) {
1694         switch (fmt) {
1695         /* Paletted */
1696         case WINED3DFMT_P8:               retVal = GL_COLOR_INDEX; break;
1697         case WINED3DFMT_A8P8:             retVal = GL_COLOR_INDEX; break;
1698             /* Luminance */
1699         case WINED3DFMT_L8:               retVal = GL_LUMINANCE; break;
1700         case WINED3DFMT_A8L8:             retVal = GL_LUMINANCE_ALPHA; break;
1701         case WINED3DFMT_A4L4:             retVal = GL_LUMINANCE_ALPHA; break;
1702             /* Bump */
1703         case WINED3DFMT_V8U8:             retVal = GL_COLOR_INDEX; break;
1704         case WINED3DFMT_V16U16:           retVal = GL_COLOR_INDEX; break;
1705         case WINED3DFMT_L6V5U5:           retVal = GL_COLOR_INDEX; break;
1706         case WINED3DFMT_X8L8V8U8:         retVal = GL_BGRA; break;
1707         case WINED3DFMT_Q8W8V8U8:         retVal = GL_RGBA; break;
1708         case WINED3DFMT_Q16W16V16U16:     retVal = GL_COLOR_INDEX; break;
1709             /* color buffer */
1710         case WINED3DFMT_R3G3B2:           retVal = GL_RGB; break;
1711         case WINED3DFMT_R5G6B5:           retVal = GL_RGB; break;
1712         case WINED3DFMT_R8G8B8:           retVal = GL_RGB; break;
1713         case WINED3DFMT_A1R5G5B5:         retVal = GL_BGRA; break;
1714         case WINED3DFMT_X1R5G5B5:         retVal = GL_BGRA; break;
1715         case WINED3DFMT_A4R4G4B4:         retVal = GL_BGRA; break;
1716         case WINED3DFMT_X4R4G4B4:         retVal = GL_BGRA; break;
1717         case WINED3DFMT_A8R8G8B8:         retVal = GL_BGRA; break;
1718         case WINED3DFMT_A8B8G8R8:         retVal = GL_RGBA; break;
1719         case WINED3DFMT_A2R10G10B10:      retVal = GL_BGRA; break;
1720         case WINED3DFMT_X8R8G8B8:         retVal = GL_BGRA; break;
1721             /* to see */
1722         case WINED3DFMT_A8:               retVal = GL_ALPHA; break;
1723             /* Depth + Stencil */
1724         case WINED3DFMT_D24S8:            retVal = GL_COLOR_INDEX; break;
1725         case WINED3DFMT_D24FS8:           retVal = GL_COLOR_INDEX; break;
1726         case WINED3DFMT_D24X8:            retVal = GL_COLOR_INDEX; break;
1727         case WINED3DFMT_D24X4S4:          retVal = GL_COLOR_INDEX; break;
1728         case WINED3DFMT_D32:              retVal = GL_COLOR_INDEX; break;
1729         case WINED3DFMT_D16:              retVal = GL_COLOR_INDEX; break;
1730         case WINED3DFMT_D15S1:            retVal = GL_COLOR_INDEX; break;
1731         case WINED3DFMT_D16_LOCKABLE:     retVal = GL_COLOR_INDEX; break;
1732         default:
1733             FIXME("Unhandled fmt(%u,%s)\n", fmt, debug_d3dformat(fmt));
1734             retVal = GL_BGR;
1735         }
1736     }
1737
1738     TRACE("fmt2glFmt for fmt(%u,%s) = %x\n", fmt, debug_d3dformat(fmt), retVal);
1739     return retVal;
1740 }
1741
1742 GLenum D3DFmt2GLType(IWineD3DDeviceImpl* This, D3DFORMAT fmt) {
1743     GLenum retVal = 0;
1744
1745     if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
1746         switch (fmt) {
1747         case WINED3DFMT_DXT1:             retVal = 0; break;
1748         case WINED3DFMT_DXT2:             retVal = 0; break;
1749         case WINED3DFMT_DXT3:             retVal = 0; break;
1750         case WINED3DFMT_DXT4:             retVal = 0; break;
1751         case WINED3DFMT_DXT5:             retVal = 0; break;
1752         default:
1753             /* stupid compiler */
1754             break;
1755         }
1756     }
1757
1758     if (retVal == 0) {
1759         switch (fmt) {
1760         /* Paletted */
1761         case WINED3DFMT_P8:               retVal = GL_UNSIGNED_BYTE; break;
1762         case WINED3DFMT_A8P8:             retVal = GL_UNSIGNED_BYTE; break;
1763             /* Luminance */
1764         case WINED3DFMT_L8:               retVal = GL_UNSIGNED_BYTE; break;
1765         case WINED3DFMT_A8L8:             retVal = GL_UNSIGNED_BYTE; break;
1766         case WINED3DFMT_A4L4:             retVal = GL_UNSIGNED_BYTE; break;
1767             /* Bump */
1768         case WINED3DFMT_V8U8:             retVal = GL_UNSIGNED_BYTE; break;
1769         case WINED3DFMT_V16U16:           retVal = GL_UNSIGNED_SHORT; break;
1770         case WINED3DFMT_L6V5U5:           retVal = GL_UNSIGNED_SHORT_5_5_5_1; break;
1771         case WINED3DFMT_X8L8V8U8:         retVal = GL_UNSIGNED_BYTE; break;
1772             /* Color buffer */
1773         case WINED3DFMT_R3G3B2:           retVal = GL_UNSIGNED_BYTE_2_3_3_REV; break;
1774         case WINED3DFMT_R5G6B5:           retVal = GL_UNSIGNED_SHORT_5_6_5; break;
1775         case WINED3DFMT_R8G8B8:           retVal = GL_UNSIGNED_BYTE; break;
1776         case WINED3DFMT_A1R5G5B5:         retVal = GL_UNSIGNED_SHORT_1_5_5_5_REV; break;
1777         case WINED3DFMT_X1R5G5B5:         retVal = GL_UNSIGNED_SHORT_1_5_5_5_REV; break;
1778         case WINED3DFMT_A4R4G4B4:         retVal = GL_UNSIGNED_SHORT_4_4_4_4_REV; break;
1779         case WINED3DFMT_X4R4G4B4:         retVal = GL_UNSIGNED_SHORT_4_4_4_4_REV; break;
1780         case WINED3DFMT_A8R8G8B8:         retVal = GL_UNSIGNED_INT_8_8_8_8_REV; break;
1781         case WINED3DFMT_A8B8G8R8:         retVal = GL_UNSIGNED_INT_8_8_8_8_REV; break;
1782         case WINED3DFMT_A2R10G10B10:      retVal = GL_UNSIGNED_INT_2_10_10_10_REV; break;
1783         case WINED3DFMT_X8R8G8B8:         retVal = GL_UNSIGNED_INT_8_8_8_8_REV; break;
1784             /* to see */
1785         case WINED3DFMT_A8:               retVal = GL_ALPHA; break;
1786             /* Depth + Stencil */
1787         case WINED3DFMT_D24S8:            retVal = GL_UNSIGNED_INT; break;
1788         case WINED3DFMT_D24FS8:           retVal = GL_UNSIGNED_INT; break;
1789         case WINED3DFMT_D24X8:            retVal = GL_UNSIGNED_INT; break;
1790         case WINED3DFMT_D24X4S4:          retVal = GL_UNSIGNED_INT; break;
1791         case WINED3DFMT_D32:              retVal = GL_UNSIGNED_INT; break;
1792         case WINED3DFMT_D16:              retVal = GL_UNSIGNED_SHORT; break;
1793         case WINED3DFMT_D15S1:            retVal = GL_UNSIGNED_SHORT; break;
1794         case WINED3DFMT_D16_LOCKABLE:     retVal = GL_UNSIGNED_SHORT; break;
1795             /* compressed textures */
1796         case WINED3DFMT_DXT1:             retVal = 0; break;
1797         case WINED3DFMT_DXT2:             retVal = 0; break;
1798         case WINED3DFMT_DXT3:             retVal = 0; break;
1799         case WINED3DFMT_DXT4:             retVal = 0; break;
1800         case WINED3DFMT_DXT5:             retVal = 0; break;
1801
1802         default:
1803             FIXME("Unhandled fmt(%u,%s)\n", fmt, debug_d3dformat(fmt));
1804             retVal = GL_UNSIGNED_BYTE;
1805         }
1806     }
1807
1808     TRACE("fmt2glType for fmt(%u,%s) = %x\n", fmt, debug_d3dformat(fmt), retVal);
1809     return retVal;
1810 }
1811
1812 SHORT D3DFmtGetBpp(IWineD3DDeviceImpl* This, D3DFORMAT fmt) {
1813     SHORT retVal;
1814
1815     switch (fmt) {
1816     /* color buffer */
1817     case WINED3DFMT_R3G3B2:           retVal = 1; break;
1818     case WINED3DFMT_R5G6B5:           retVal = 2; break;
1819     case WINED3DFMT_R8G8B8:           retVal = 3; break;
1820     case WINED3DFMT_A1R5G5B5:         retVal = 2; break;
1821     case WINED3DFMT_X1R5G5B5:         retVal = 2; break;
1822     case WINED3DFMT_A4R4G4B4:         retVal = 2; break;
1823     case WINED3DFMT_X4R4G4B4:         retVal = 2; break;
1824     case WINED3DFMT_A8R8G8B8:         retVal = 4; break;
1825     case WINED3DFMT_A8B8G8R8:         retVal = 4; break;
1826     case WINED3DFMT_A2R10G10B10:      retVal = 4; break;
1827     case WINED3DFMT_X8R8G8B8:         retVal = 4; break;
1828         /* Paletted */
1829     case WINED3DFMT_P8:               retVal = 1; break;
1830     case WINED3DFMT_A8P8:             retVal = 2; break;
1831         /* depth/stencil buffer */
1832     case WINED3DFMT_D16_LOCKABLE:     retVal = 2; break;
1833     case WINED3DFMT_D16:              retVal = 2; break;
1834     case WINED3DFMT_D32:              retVal = 4; break;
1835     case WINED3DFMT_D15S1:            retVal = 2; break;
1836     case WINED3DFMT_D24X4S4:          retVal = 4; break;
1837     case WINED3DFMT_D24S8:            retVal = 4; break;
1838     case WINED3DFMT_D24FS8:           retVal = 4; break;
1839     case WINED3DFMT_D24X8:            retVal = 4; break;
1840         /* Luminance */
1841     case WINED3DFMT_L8:               retVal = 1; break;
1842     case WINED3DFMT_A4L4:             retVal = 1; break;
1843     case WINED3DFMT_A8L8:             retVal = 2; break;
1844         /* Bump */
1845     case WINED3DFMT_V8U8:             retVal = 2; break;
1846     case WINED3DFMT_L6V5U5:           retVal = 2; break;
1847     case WINED3DFMT_V16U16:           retVal = 4; break;
1848     case WINED3DFMT_X8L8V8U8:         retVal = 4; break;
1849     case WINED3DFMT_Q8W8V8U8:         retVal = 4; break;
1850     case WINED3DFMT_Q16W16V16U16:     retVal = 8; break;
1851         /* Compressed */
1852     case WINED3DFMT_DXT1:             retVal = 1; break; /* Actually  8 bytes per 16 pixels - Special cased later */
1853     case WINED3DFMT_DXT2:             retVal = 1; break; /* Actually 16 bytes per 16 pixels */
1854     case WINED3DFMT_DXT3:             retVal = 1; break; /* Actually 16 bytes per 16 pixels */
1855     case WINED3DFMT_DXT4:             retVal = 1; break; /* Actually 16 bytes per 16 pixels */
1856     case WINED3DFMT_DXT5:             retVal = 1; break; /* Actually 16 bytes per 16 pixels */
1857         /* to see */
1858     case WINED3DFMT_A8:               retVal = 1; break;
1859         /* unknown */
1860     case WINED3DFMT_UNKNOWN:
1861         /* Guess at the highest value of the above */
1862         TRACE("WINED3DFMT_UNKNOWN - Guessing at 4 bytes/pixel %u\n", fmt);
1863         retVal = 4;
1864         break;
1865
1866     default:
1867         FIXME("Unhandled fmt(%u,%s)\n", fmt, debug_d3dformat(fmt));
1868         retVal = 4;
1869     }
1870     TRACE("bytes/Pxl for fmt(%u,%s) = %d\n", fmt, debug_d3dformat(fmt), retVal);
1871     return retVal;
1872 }
1873
1874 /* Convertes a D3D format into a OpenGL configuration format */
1875 int D3DFmtMakeGlCfg(D3DFORMAT BackBufferFormat, D3DFORMAT StencilBufferFormat, int *attribs, int* nAttribs, BOOL alternate){
1876 #define PUSH1(att)        attribs[(*nAttribs)++] = (att);
1877 #define PUSH2(att,value)  attribs[(*nAttribs)++] = (att); attribs[(*nAttribs)++] = (value);
1878     /*We need to do some Card specific stuff in here at some point,
1879     D3D now supports floating point format buffers, and there are a number of different OpelGl ways of managing these e.g.
1880     GLX_ATI_pixel_format_float
1881     */
1882     switch (BackBufferFormat) {
1883         /* color buffer */
1884     case WINED3DFMT_P8:
1885         PUSH2(GLX_RENDER_TYPE,  GLX_COLOR_INDEX_BIT);
1886         PUSH2(GLX_BUFFER_SIZE,  8);
1887         PUSH2(GLX_DOUBLEBUFFER, TRUE);
1888         break;
1889
1890     case WINED3DFMT_R3G3B2:
1891         PUSH2(GLX_RENDER_TYPE,  GLX_RGBA_BIT);
1892         PUSH2(GLX_RED_SIZE,     3);
1893         PUSH2(GLX_GREEN_SIZE,   3);
1894         PUSH2(GLX_BLUE_SIZE,    2);
1895         break;
1896
1897     case WINED3DFMT_A1R5G5B5:
1898         PUSH2(GLX_ALPHA_SIZE,   1);
1899     case WINED3DFMT_X1R5G5B5:
1900         PUSH2(GLX_RED_SIZE,     5);
1901         PUSH2(GLX_GREEN_SIZE,   5);
1902         PUSH2(GLX_BLUE_SIZE,    5);
1903         break;
1904
1905     case WINED3DFMT_R5G6B5:
1906         PUSH2(GLX_RED_SIZE,     5);
1907         PUSH2(GLX_GREEN_SIZE,   6);
1908         PUSH2(GLX_BLUE_SIZE,    5);
1909         break;
1910
1911     case WINED3DFMT_A4R4G4B4:
1912         PUSH2(GLX_ALPHA_SIZE,   4);
1913     case WINED3DFMT_X4R4G4B4:
1914         PUSH2(GLX_RED_SIZE,     4);
1915         PUSH2(GLX_GREEN_SIZE,   4);
1916         PUSH2(GLX_BLUE_SIZE,    4);
1917         break;
1918
1919     case WINED3DFMT_A8R8G8B8:
1920         PUSH2(GLX_ALPHA_SIZE,   8);
1921     case WINED3DFMT_R8G8B8:
1922     case WINED3DFMT_X8R8G8B8:
1923         PUSH2(GLX_RED_SIZE,     8);
1924         PUSH2(GLX_GREEN_SIZE,   8);
1925         PUSH2(GLX_BLUE_SIZE,    8);
1926         break;
1927
1928     case WINED3DFMT_A2R10G10B10:
1929         PUSH2(GLX_ALPHA_SIZE,   2);
1930         PUSH2(GLX_RED_SIZE,    10);
1931         PUSH2(GLX_GREEN_SIZE,  10);
1932         PUSH2(GLX_BLUE_SIZE,   10);
1933         break;
1934
1935     default:
1936         break;
1937     }
1938     if(!alternate){
1939         switch (StencilBufferFormat) {
1940     case WINED3DFMT_D16_LOCKABLE:
1941     case WINED3DFMT_D16:
1942         PUSH2(GLX_DEPTH_SIZE,   16);
1943         break;
1944
1945     case WINED3DFMT_D15S1:
1946         PUSH2(GLX_DEPTH_SIZE,   15);
1947         PUSH2(GLX_STENCIL_SIZE, 1);
1948         /*Does openGl support a 1bit stencil?, I've seen it used elsewhere
1949         e.g. http://www.ks.uiuc.edu/Research/vmd/doxygen/OpenGLDisplayDevice_8C-source.html*/
1950         break;
1951
1952     case WINED3DFMT_D24X8:
1953         PUSH2(GLX_DEPTH_SIZE,   24);
1954         break;
1955
1956     case WINED3DFMT_D24X4S4:
1957         PUSH2(GLX_DEPTH_SIZE,   24);
1958         PUSH2(GLX_STENCIL_SIZE, 4);
1959         break;
1960
1961     case WINED3DFMT_D24S8:
1962         PUSH2(GLX_DEPTH_SIZE,   24);
1963         PUSH2(GLX_STENCIL_SIZE, 8);
1964         break;
1965
1966     case WINED3DFMT_D24FS8:
1967         PUSH2(GLX_DEPTH_SIZE,   24);
1968         PUSH2(GLX_STENCIL_SIZE, 8);
1969         break;
1970
1971     case WINED3DFMT_D32:
1972         PUSH2(GLX_DEPTH_SIZE,   32);
1973         break;
1974
1975     default:
1976         break;
1977     }
1978
1979     } else { /* it the device doesn't support the 'exact' format, try to find something close */
1980         switch (StencilBufferFormat) {
1981         case WINED3DFMT_D16_LOCKABLE:
1982         case WINED3DFMT_D16:
1983             PUSH2(GLX_DEPTH_SIZE,   1);
1984             break;
1985
1986         case WINED3DFMT_D15S1:
1987             PUSH2(GLX_DEPTH_SIZE,   1);
1988             PUSH2(GLX_STENCIL_SIZE, 1);
1989             /*Does openGl support a 1bit stencil?, I've seen it used elsewhere
1990             e.g. http://www.ks.uiuc.edu/Research/vmd/doxygen/OpenGLDisplayDevice_8C-source.html*/
1991             break;
1992
1993         case WINED3DFMT_D24X8:
1994             PUSH2(GLX_DEPTH_SIZE,   1);
1995             break;
1996
1997         case WINED3DFMT_D24X4S4:
1998             PUSH2(GLX_DEPTH_SIZE,   1);
1999             PUSH2(GLX_STENCIL_SIZE, 1);
2000             break;
2001
2002         case WINED3DFMT_D24S8:
2003             PUSH2(GLX_DEPTH_SIZE,   1);
2004             PUSH2(GLX_STENCIL_SIZE, 1);
2005             break;
2006
2007         case WINED3DFMT_D24FS8:
2008             PUSH2(GLX_DEPTH_SIZE,   1);
2009             PUSH2(GLX_STENCIL_SIZE, 1);
2010             break;
2011
2012         case WINED3DFMT_D32:
2013             PUSH2(GLX_DEPTH_SIZE,   1);
2014             break;
2015
2016         default:
2017             break;
2018         }
2019     }
2020
2021     return *nAttribs;
2022 }
2023
2024 #undef GLINFO_LOCATION