msxml3: Change order of interface recognition and fix typo.
[wine] / dlls / wined3d / state.c
1
2 /*
3  * Direct3D state management
4  *
5  * Copyright 2002 Lionel Ulmer
6  * Copyright 2002-2005 Jason Edmeades
7  * Copyright 2003-2004 Raphael Junqueira
8  * Copyright 2004 Christian Costa
9  * Copyright 2005 Oliver Stieber
10  * Copyright 2006 Henri Verbeet
11  * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27
28 #include "config.h"
29 #include <stdio.h>
30 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
33 #include "wined3d_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_shader);
37
38 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
39
40 static void state_blendop(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context);
41
42 static void state_nogl(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
43     /* Used for states which are not mapped to a gl state as-is, but used somehow different,
44      * e.g as a parameter for drawing, or which are unimplemented in windows d3d
45      */
46     if(STATE_IS_RENDER(state)) {
47         WINED3DRENDERSTATETYPE RenderState = state - STATE_RENDER(0);
48         TRACE("(%s,%d) no direct mapping to gl\n", debug_d3drenderstate(RenderState), stateblock->renderState[RenderState]);
49     } else {
50         /* Shouldn't have an unknown type here */
51         FIXME("%d no direct mapping to gl of state with unknown type\n", state);
52     }
53 }
54
55 static void state_undefined(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
56     /* Print a WARN, this allows the stateblock code to loop over all states to generate a display
57      * list without causing confusing terminal output. Deliberately no special debug name here
58      * because its undefined.
59      */
60     WARN("undefined state %d\n", state);
61 }
62
63 static void state_fillmode(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
64     WINED3DFILLMODE Value = stateblock->renderState[WINED3DRS_FILLMODE];
65
66     switch(Value) {
67         case WINED3DFILL_POINT:
68             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
69             checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)");
70             break;
71         case WINED3DFILL_WIREFRAME:
72             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
73             checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)");
74             break;
75         case WINED3DFILL_SOLID:
76             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
77             checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
78             break;
79         default:
80             FIXME("Unrecognized WINED3DRS_FILLMODE value %d\n", Value);
81     }
82 }
83
84 static void state_lighting(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
85     BOOL transformed;
86
87     /* Lighting is not enabled if transformed vertices are drawn
88      * but lighting does not affect the stream sources, so it is not grouped for performance reasons.
89      * This state reads the decoded vertex declaration, so if it is dirty don't do anything. The
90      * vertex declaration applying function calls this function for updating
91      */
92
93     if(isStateDirty(context, STATE_VDECL)) {
94         return;
95     }
96
97     transformed = ((stateblock->wineD3DDevice->strided_streams.u.s.position.lpData != NULL ||
98                     stateblock->wineD3DDevice->strided_streams.u.s.position.VBO != 0) &&
99                     stateblock->wineD3DDevice->strided_streams.u.s.position_transformed) ? TRUE : FALSE;
100
101     if (stateblock->renderState[WINED3DRS_LIGHTING] && !transformed) {
102         glEnable(GL_LIGHTING);
103         checkGLcall("glEnable GL_LIGHTING");
104     } else {
105         glDisable(GL_LIGHTING);
106         checkGLcall("glDisable GL_LIGHTING");
107     }
108 }
109
110 static void state_zenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
111     /* No z test without depth stencil buffers */
112     if(stateblock->wineD3DDevice->stencilBufferTarget == NULL) {
113         TRACE("No Z buffer - disabling depth test\n");
114         glDisable(GL_DEPTH_TEST); /* This also disables z writing in gl */
115         checkGLcall("glDisable GL_DEPTH_TEST");
116         return;
117     }
118
119     switch ((WINED3DZBUFFERTYPE) stateblock->renderState[WINED3DRS_ZENABLE]) {
120         case WINED3DZB_FALSE:
121             glDisable(GL_DEPTH_TEST);
122             checkGLcall("glDisable GL_DEPTH_TEST");
123             break;
124         case WINED3DZB_TRUE:
125             glEnable(GL_DEPTH_TEST);
126             checkGLcall("glEnable GL_DEPTH_TEST");
127             break;
128         case WINED3DZB_USEW:
129             glEnable(GL_DEPTH_TEST);
130             checkGLcall("glEnable GL_DEPTH_TEST");
131             FIXME("W buffer is not well handled\n");
132             break;
133         default:
134             FIXME("Unrecognized D3DZBUFFERTYPE value %d\n", stateblock->renderState[WINED3DRS_ZENABLE]);
135     }
136 }
137
138 static void state_cullmode(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
139     /* glFrontFace() is set in context.c at context init and on an offscreen / onscreen rendering
140      * switch
141      */
142     switch ((WINED3DCULL) stateblock->renderState[WINED3DRS_CULLMODE]) {
143         case WINED3DCULL_NONE:
144             glDisable(GL_CULL_FACE);
145             checkGLcall("glDisable GL_CULL_FACE");
146             break;
147         case WINED3DCULL_CW:
148             glEnable(GL_CULL_FACE);
149             checkGLcall("glEnable GL_CULL_FACE");
150             glCullFace(GL_FRONT);
151             checkGLcall("glCullFace(GL_FRONT)");
152             break;
153         case WINED3DCULL_CCW:
154             glEnable(GL_CULL_FACE);
155             checkGLcall("glEnable GL_CULL_FACE");
156             glCullFace(GL_BACK);
157             checkGLcall("glCullFace(GL_BACK)");
158             break;
159         default:
160             FIXME("Unrecognized/Unhandled WINED3DCULL value %d\n", stateblock->renderState[WINED3DRS_CULLMODE]);
161     }
162 }
163
164 static void state_shademode(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
165     switch ((WINED3DSHADEMODE) stateblock->renderState[WINED3DRS_SHADEMODE]) {
166         case WINED3DSHADE_FLAT:
167             glShadeModel(GL_FLAT);
168             checkGLcall("glShadeModel(GL_FLAT)");
169             break;
170         case WINED3DSHADE_GOURAUD:
171             glShadeModel(GL_SMOOTH);
172             checkGLcall("glShadeModel(GL_SMOOTH)");
173             break;
174         case WINED3DSHADE_PHONG:
175             FIXME("WINED3DSHADE_PHONG isn't supported\n");
176             break;
177         default:
178             FIXME("Unrecognized/Unhandled WINED3DSHADEMODE value %d\n", stateblock->renderState[WINED3DRS_SHADEMODE]);
179     }
180 }
181
182 static void state_ditherenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
183     if (stateblock->renderState[WINED3DRS_DITHERENABLE]) {
184         glEnable(GL_DITHER);
185         checkGLcall("glEnable GL_DITHER");
186     } else {
187         glDisable(GL_DITHER);
188         checkGLcall("glDisable GL_DITHER");
189     }
190 }
191
192 static void state_zwritenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
193     /* TODO: Test if in d3d z writing is enabled even if ZENABLE is off. If yes,
194      * this has to be merged with ZENABLE and ZFUNC
195      */
196     if (stateblock->renderState[WINED3DRS_ZWRITEENABLE]) {
197         glDepthMask(1);
198         checkGLcall("glDepthMask(1)");
199     } else {
200         glDepthMask(0);
201         checkGLcall("glDepthMask(0)");
202     }
203 }
204
205 static void state_zfunc(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
206     int glParm = CompareFunc(stateblock->renderState[WINED3DRS_ZFUNC]);
207
208     if(glParm) {
209         if(glParm == GL_EQUAL || glParm == GL_NOTEQUAL) {
210             static BOOL once = FALSE;
211             /* There are a few issues with this: First, our inability to
212              * select a proper Z depth, most of the time we're stuck with
213              * D24S8, even if the app selects D32 or D16. There seem to be
214              * some other precision problems which have to be debugged to
215              * make NOTEQUAL and EQUAL work properly
216              */
217             if(!once) {
218                 once = TRUE;
219                 FIXME("D3DCMP_NOTEQUAL and D3DCMP_EQUAL do not work correctly yet\n");
220             }
221         }
222
223         glDepthFunc(glParm);
224         checkGLcall("glDepthFunc");
225     }
226 }
227
228 static void state_ambient(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
229     float col[4];
230     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_AMBIENT], col);
231
232     TRACE("Setting ambient to (%f,%f,%f,%f)\n", col[0], col[1], col[2], col[3]);
233     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, col);
234     checkGLcall("glLightModel for MODEL_AMBIENT");
235 }
236
237 static void state_blend(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
238     int srcBlend = GL_ZERO;
239     int dstBlend = GL_ZERO;
240     const StaticPixelFormatDesc *rtFormat;
241     IWineD3DSurfaceImpl *target = (IWineD3DSurfaceImpl *) stateblock->wineD3DDevice->render_targets[0];
242
243     /* GL_LINE_SMOOTH needs GL_BLEND to work, according to the red book, and special blending params */
244     if (stateblock->renderState[WINED3DRS_ALPHABLENDENABLE]      ||
245         stateblock->renderState[WINED3DRS_EDGEANTIALIAS]         ||
246         stateblock->renderState[WINED3DRS_ANTIALIASEDLINEENABLE]) {
247         const GlPixelFormatDesc *glDesc;
248         getFormatDescEntry(target->resource.format, &GLINFO_LOCATION, &glDesc);
249
250         /* Disable blending in all cases even without pixelshaders. With blending on we could face a big performance penalty.
251          * The d3d9 visual test confirms the behavior. */
252         if(!(glDesc->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING)) {
253             glDisable(GL_BLEND);
254             checkGLcall("glDisable GL_BLEND");
255             return;
256         } else {
257             glEnable(GL_BLEND);
258             checkGLcall("glEnable GL_BLEND");
259         }
260     } else {
261         glDisable(GL_BLEND);
262         checkGLcall("glDisable GL_BLEND");
263         /* Nothing more to do - get out */
264         return;
265     };
266
267     switch (stateblock->renderState[WINED3DRS_DESTBLEND]) {
268         case WINED3DBLEND_ZERO               : dstBlend = GL_ZERO;  break;
269         case WINED3DBLEND_ONE                : dstBlend = GL_ONE;  break;
270         case WINED3DBLEND_SRCCOLOR           : dstBlend = GL_SRC_COLOR;  break;
271         case WINED3DBLEND_INVSRCCOLOR        : dstBlend = GL_ONE_MINUS_SRC_COLOR;  break;
272         case WINED3DBLEND_SRCALPHA           : dstBlend = GL_SRC_ALPHA;  break;
273         case WINED3DBLEND_INVSRCALPHA        : dstBlend = GL_ONE_MINUS_SRC_ALPHA;  break;
274         case WINED3DBLEND_DESTCOLOR          : dstBlend = GL_DST_COLOR;  break;
275         case WINED3DBLEND_INVDESTCOLOR       : dstBlend = GL_ONE_MINUS_DST_COLOR;  break;
276
277         /* To compensate the lack of format switching with backbuffer offscreen rendering,
278          * and with onscreen rendering, we modify the alpha test parameters for (INV)DESTALPHA
279          * if the render target doesn't support alpha blending. A nonexistent alpha channel
280          * returns 1.0, so D3DBLEND_DESTALPHA is GL_ONE, and D3DBLEND_INVDESTALPHA is GL_ZERO
281          */
282         case WINED3DBLEND_DESTALPHA          :
283             rtFormat = getFormatDescEntry(target->resource.format, NULL, NULL);
284             dstBlend = rtFormat->alphaMask ? GL_DST_ALPHA : GL_ONE;
285             break;
286         case WINED3DBLEND_INVDESTALPHA       :
287             rtFormat = getFormatDescEntry(target->resource.format, NULL, NULL);
288             dstBlend = rtFormat->alphaMask ? GL_ONE_MINUS_DST_ALPHA : GL_ZERO;
289             break;
290
291         case WINED3DBLEND_SRCALPHASAT        :
292             dstBlend = GL_SRC_ALPHA_SATURATE;
293             WARN("Application uses SRCALPHASAT as dest blend factor, expect problems\n");
294             break;
295
296         /* WINED3DBLEND_BOTHSRCALPHA and WINED3DBLEND_BOTHINVSRCALPHA are legacy source blending
297          * values which are still valid up to d3d9. They should not occur as dest blend values
298          */
299         case WINED3DBLEND_BOTHSRCALPHA       : dstBlend = GL_SRC_ALPHA;
300             srcBlend = GL_SRC_ALPHA;
301             FIXME("WINED3DRS_DESTBLEND = WINED3DBLEND_BOTHSRCALPHA, what to do?\n");
302             break;
303
304         case WINED3DBLEND_BOTHINVSRCALPHA    : dstBlend = GL_ONE_MINUS_SRC_ALPHA;
305             srcBlend = GL_ONE_MINUS_SRC_ALPHA;
306             FIXME("WINED3DRS_DESTBLEND = WINED3DBLEND_BOTHINVSRCALPHA, what to do?\n");
307             break;
308
309         case WINED3DBLEND_BLENDFACTOR        : dstBlend = GL_CONSTANT_COLOR;   break;
310         case WINED3DBLEND_INVBLENDFACTOR     : dstBlend = GL_ONE_MINUS_CONSTANT_COLOR;  break;
311         default:
312             FIXME("Unrecognized dst blend value %d\n", stateblock->renderState[WINED3DRS_DESTBLEND]);
313     }
314
315     switch (stateblock->renderState[WINED3DRS_SRCBLEND]) {
316         case WINED3DBLEND_ZERO               : srcBlend = GL_ZERO;  break;
317         case WINED3DBLEND_ONE                : srcBlend = GL_ONE;  break;
318         case WINED3DBLEND_SRCCOLOR           : srcBlend = GL_SRC_COLOR;  break;
319         case WINED3DBLEND_INVSRCCOLOR        : srcBlend = GL_ONE_MINUS_SRC_COLOR;  break;
320         case WINED3DBLEND_SRCALPHA           : srcBlend = GL_SRC_ALPHA;  break;
321         case WINED3DBLEND_INVSRCALPHA        : srcBlend = GL_ONE_MINUS_SRC_ALPHA;  break;
322         case WINED3DBLEND_DESTCOLOR          : srcBlend = GL_DST_COLOR;  break;
323         case WINED3DBLEND_INVDESTCOLOR       : srcBlend = GL_ONE_MINUS_DST_COLOR;  break;
324         case WINED3DBLEND_SRCALPHASAT        : srcBlend = GL_SRC_ALPHA_SATURATE;  break;
325
326         case WINED3DBLEND_DESTALPHA          :
327             rtFormat = getFormatDescEntry(target->resource.format, NULL, NULL);
328             srcBlend = rtFormat->alphaMask ? GL_DST_ALPHA : GL_ONE;
329             break;
330         case WINED3DBLEND_INVDESTALPHA       :
331             rtFormat = getFormatDescEntry(target->resource.format, NULL, NULL);
332             srcBlend = rtFormat->alphaMask ? GL_ONE_MINUS_DST_ALPHA : GL_ZERO;
333             break;
334
335         case WINED3DBLEND_BOTHSRCALPHA       : srcBlend = GL_SRC_ALPHA;
336             dstBlend = GL_ONE_MINUS_SRC_ALPHA;
337             break;
338
339         case WINED3DBLEND_BOTHINVSRCALPHA    : srcBlend = GL_ONE_MINUS_SRC_ALPHA;
340             dstBlend = GL_SRC_ALPHA;
341             break;
342
343         case WINED3DBLEND_BLENDFACTOR        : srcBlend = GL_CONSTANT_COLOR;   break;
344         case WINED3DBLEND_INVBLENDFACTOR     : srcBlend = GL_ONE_MINUS_CONSTANT_COLOR;  break;
345         default:
346             FIXME("Unrecognized src blend value %d\n", stateblock->renderState[WINED3DRS_SRCBLEND]);
347     }
348
349     if(stateblock->renderState[WINED3DRS_EDGEANTIALIAS] ||
350        stateblock->renderState[WINED3DRS_ANTIALIASEDLINEENABLE]) {
351         glEnable(GL_LINE_SMOOTH);
352         checkGLcall("glEnable(GL_LINE_SMOOTH)");
353         if(srcBlend != GL_SRC_ALPHA) {
354             WARN("WINED3DRS_EDGEANTIALIAS enabled, but unexpected src blending param\n");
355         }
356         if(dstBlend != GL_ONE_MINUS_SRC_ALPHA && dstBlend != GL_ONE) {
357             WARN("WINED3DRS_EDGEANTIALIAS enabled, but unexpected dst blending param\n");
358         }
359     } else {
360         glDisable(GL_LINE_SMOOTH);
361         checkGLcall("glDisable(GL_LINE_SMOOTH)");
362     }
363
364     /* Re-apply BLENDOP(ALPHA) because of a possible SEPARATEALPHABLENDENABLE change */
365     if(!isStateDirty(context, STATE_RENDER(WINED3DRS_BLENDOP))) {
366         state_blendop(STATE_RENDER(WINED3DRS_BLENDOPALPHA), stateblock, context);
367     }
368
369     if(stateblock->renderState[WINED3DRS_SEPARATEALPHABLENDENABLE]) {
370         int srcBlendAlpha = GL_ZERO;
371         int dstBlendAlpha = GL_ZERO;
372
373         /* Separate alpha blending requires GL_EXT_blend_function_separate, so make sure it is around */
374         if(!GL_SUPPORT(EXT_BLEND_FUNC_SEPARATE)) {
375             WARN("Unsupported in local OpenGL implementation: glBlendFuncSeparateEXT\n");
376             return;
377         }
378
379         switch (stateblock->renderState[WINED3DRS_DESTBLENDALPHA]) {
380             case WINED3DBLEND_ZERO               : dstBlendAlpha = GL_ZERO;  break;
381             case WINED3DBLEND_ONE                : dstBlendAlpha = GL_ONE;  break;
382             case WINED3DBLEND_SRCCOLOR           : dstBlendAlpha = GL_SRC_COLOR;  break;
383             case WINED3DBLEND_INVSRCCOLOR        : dstBlendAlpha = GL_ONE_MINUS_SRC_COLOR;  break;
384             case WINED3DBLEND_SRCALPHA           : dstBlendAlpha = GL_SRC_ALPHA;  break;
385             case WINED3DBLEND_INVSRCALPHA        : dstBlendAlpha = GL_ONE_MINUS_SRC_ALPHA;  break;
386             case WINED3DBLEND_DESTCOLOR          : dstBlendAlpha = GL_DST_COLOR;  break;
387             case WINED3DBLEND_INVDESTCOLOR       : dstBlendAlpha = GL_ONE_MINUS_DST_COLOR;  break;
388             case WINED3DBLEND_DESTALPHA          : dstBlendAlpha = GL_DST_ALPHA;  break;
389             case WINED3DBLEND_INVDESTALPHA       : dstBlendAlpha = GL_DST_ALPHA;  break;
390             case WINED3DBLEND_SRCALPHASAT        :
391                 dstBlend = GL_SRC_ALPHA_SATURATE;
392                 WARN("Application uses SRCALPHASAT as dest blend factor, expect problems\n");
393                 break;
394             /* WINED3DBLEND_BOTHSRCALPHA and WINED3DBLEND_BOTHINVSRCALPHA are legacy source blending
395             * values which are still valid up to d3d9. They should not occur as dest blend values
396             */
397             case WINED3DBLEND_BOTHSRCALPHA       :
398                 dstBlendAlpha = GL_SRC_ALPHA;
399                 srcBlendAlpha = GL_SRC_ALPHA;
400                 FIXME("WINED3DRS_DESTBLENDALPHA = WINED3DBLEND_BOTHSRCALPHA, what to do?\n");
401                 break;
402             case WINED3DBLEND_BOTHINVSRCALPHA    :
403                 dstBlendAlpha = GL_ONE_MINUS_SRC_ALPHA;
404                 srcBlendAlpha = GL_ONE_MINUS_SRC_ALPHA;
405                 FIXME("WINED3DRS_DESTBLENDALPHA = WINED3DBLEND_BOTHINVSRCALPHA, what to do?\n");
406                 break;
407             case WINED3DBLEND_BLENDFACTOR        : dstBlendAlpha = GL_CONSTANT_COLOR;   break;
408             case WINED3DBLEND_INVBLENDFACTOR     : dstBlendAlpha = GL_ONE_MINUS_CONSTANT_COLOR;  break;
409             default:
410                 FIXME("Unrecognized dst blend alpha value %d\n", stateblock->renderState[WINED3DRS_DESTBLENDALPHA]);
411         }
412
413         switch (stateblock->renderState[WINED3DRS_SRCBLENDALPHA]) {
414             case WINED3DBLEND_ZERO               : srcBlendAlpha = GL_ZERO;  break;
415             case WINED3DBLEND_ONE                : srcBlendAlpha = GL_ONE;  break;
416             case WINED3DBLEND_SRCCOLOR           : srcBlendAlpha = GL_SRC_COLOR;  break;
417             case WINED3DBLEND_INVSRCCOLOR        : srcBlendAlpha = GL_ONE_MINUS_SRC_COLOR;  break;
418             case WINED3DBLEND_SRCALPHA           : srcBlendAlpha = GL_SRC_ALPHA;  break;
419             case WINED3DBLEND_INVSRCALPHA        : srcBlendAlpha = GL_ONE_MINUS_SRC_ALPHA;  break;
420             case WINED3DBLEND_DESTCOLOR          : srcBlendAlpha = GL_DST_COLOR;  break;
421             case WINED3DBLEND_INVDESTCOLOR       : srcBlendAlpha = GL_ONE_MINUS_DST_COLOR;  break;
422             case WINED3DBLEND_SRCALPHASAT        : srcBlendAlpha = GL_SRC_ALPHA_SATURATE;  break;
423             case WINED3DBLEND_DESTALPHA          : srcBlendAlpha = GL_DST_ALPHA;  break;
424             case WINED3DBLEND_INVDESTALPHA       : srcBlendAlpha = GL_DST_ALPHA;  break;
425             case WINED3DBLEND_BOTHSRCALPHA       :
426                 srcBlendAlpha = GL_SRC_ALPHA;
427                 dstBlendAlpha = GL_ONE_MINUS_SRC_ALPHA;
428                 break;
429             case WINED3DBLEND_BOTHINVSRCALPHA    :
430                 srcBlendAlpha = GL_ONE_MINUS_SRC_ALPHA;
431                 dstBlendAlpha = GL_SRC_ALPHA;
432                 break;
433             case WINED3DBLEND_BLENDFACTOR        : srcBlendAlpha = GL_CONSTANT_COLOR;   break;
434             case WINED3DBLEND_INVBLENDFACTOR     : srcBlendAlpha = GL_ONE_MINUS_CONSTANT_COLOR;  break;
435             default:
436                 FIXME("Unrecognized src blend alpha value %d\n", stateblock->renderState[WINED3DRS_SRCBLENDALPHA]);
437         }
438
439         GL_EXTCALL(glBlendFuncSeparateEXT(srcBlend, dstBlend, srcBlendAlpha, dstBlendAlpha));
440         checkGLcall("glBlendFuncSeparateEXT");
441     } else {
442         TRACE("glBlendFunc src=%x, dst=%x\n", srcBlend, dstBlend);
443         glBlendFunc(srcBlend, dstBlend);
444         checkGLcall("glBlendFunc");
445     }
446
447     /* colorkey fixup for stage 0 alphaop depends on WINED3DRS_ALPHABLENDENABLE state,
448         so it may need updating */
449     if (stateblock->renderState[WINED3DRS_COLORKEYENABLE]) {
450         const struct StateEntry *StateTable = stateblock->wineD3DDevice->StateTable;
451         StateTable[STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP)].apply(STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), stateblock, context);
452     }
453 }
454
455 static void state_blendfactor_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
456     WARN("Unsupported in local OpenGL implementation: glBlendColorEXT\n");
457 }
458
459 static void state_blendfactor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
460     float col[4];
461
462     TRACE("Setting BlendFactor to %d\n", stateblock->renderState[WINED3DRS_BLENDFACTOR]);
463     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_BLENDFACTOR], col);
464     GL_EXTCALL(glBlendColorEXT (col[0],col[1],col[2],col[3]));
465     checkGLcall("glBlendColor");
466 }
467
468 static void state_alpha(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
469     int glParm = 0;
470     float ref;
471     BOOL enable_ckey = FALSE;
472
473     IWineD3DSurfaceImpl *surf;
474
475     /* Find out if the texture on the first stage has a ckey set
476      * The alpha state func reads the texture settings, even though alpha and texture are not grouped
477      * together. This is to avoid making a huge alpha+texture+texture stage+ckey block due to the hardly
478      * used WINED3DRS_COLORKEYENABLE state(which is d3d <= 3 only). The texture function will call alpha
479      * in case it finds some texture+colorkeyenable combination which needs extra care.
480      */
481     if(stateblock->textures[0] && (
482        stateblock->textureDimensions[0] == GL_TEXTURE_2D ||
483        stateblock->textureDimensions[0] == GL_TEXTURE_RECTANGLE_ARB)) {
484         surf = (IWineD3DSurfaceImpl *) ((IWineD3DTextureImpl *)stateblock->textures[0])->surfaces[0];
485
486         if(surf->CKeyFlags & WINEDDSD_CKSRCBLT) {
487             const StaticPixelFormatDesc *fmt = getFormatDescEntry(surf->resource.format, NULL, NULL);
488             /* The surface conversion does not do color keying conversion for surfaces that have an alpha
489              * channel on their own. Likewise, the alpha test shouldn't be set up for color keying if the
490              * surface has alpha bits
491              */
492             if(fmt->alphaMask == 0x00000000) {
493                 enable_ckey = TRUE;
494             }
495         }
496     }
497
498     if(enable_ckey || context->last_was_ckey) {
499         const struct StateEntry *StateTable = stateblock->wineD3DDevice->StateTable;
500         StateTable[STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP)].apply(STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), stateblock, context);
501     }
502     context->last_was_ckey = enable_ckey;
503
504     if (stateblock->renderState[WINED3DRS_ALPHATESTENABLE] ||
505         (stateblock->renderState[WINED3DRS_COLORKEYENABLE] && enable_ckey)) {
506         glEnable(GL_ALPHA_TEST);
507         checkGLcall("glEnable GL_ALPHA_TEST");
508     } else {
509         glDisable(GL_ALPHA_TEST);
510         checkGLcall("glDisable GL_ALPHA_TEST");
511         /* Alpha test is disabled, don't bother setting the params - it will happen on the next
512          * enable call
513          */
514         return;
515     }
516
517     if(stateblock->renderState[WINED3DRS_COLORKEYENABLE] && enable_ckey) {
518         glParm = GL_NOTEQUAL;
519         ref = 0.0;
520     } else {
521         ref = ((float) stateblock->renderState[WINED3DRS_ALPHAREF]) / 255.0f;
522         glParm = CompareFunc(stateblock->renderState[WINED3DRS_ALPHAFUNC]);
523     }
524     if(glParm) {
525         glAlphaFunc(glParm, ref);
526         checkGLcall("glAlphaFunc");
527     }
528 }
529
530 static void state_clipping(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
531     DWORD enable  = 0xFFFFFFFF;
532     DWORD disable = 0x00000000;
533
534     if (use_vs(stateblock->wineD3DDevice)) {
535         /* The spec says that opengl clipping planes are disabled when using shaders. Direct3D planes aren't,
536          * so that is an issue. The MacOS ATI driver keeps clipping planes activated with shaders in some
537          * conditions I got sick of tracking down. The shader state handler disables all clip planes because
538          * of that - don't do anything here and keep them disabled
539          */
540         if(stateblock->renderState[WINED3DRS_CLIPPLANEENABLE]) {
541             static BOOL warned = FALSE;
542             if(!warned) {
543                 FIXME("Clipping not supported with vertex shaders\n");
544                 warned = TRUE;
545             }
546         }
547         return;
548     }
549
550     /* TODO: Keep track of previously enabled clipplanes to avoid unnecessary resetting
551      * of already set values
552      */
553
554     /* If enabling / disabling all
555      * TODO: Is this correct? Doesn't D3DRS_CLIPPING disable clipping on the viewport frustrum?
556      */
557     if (stateblock->renderState[WINED3DRS_CLIPPING]) {
558         enable  = stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
559         disable = ~stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
560         if(GL_SUPPORT(NV_DEPTH_CLAMP)) {
561             glDisable(GL_DEPTH_CLAMP_NV);
562             checkGLcall("glDisable(GL_DEPTH_CLAMP_NV)");
563         }
564     } else {
565         disable = 0xffffffff;
566         enable  = 0x00;
567         if(GL_SUPPORT(NV_DEPTH_CLAMP)) {
568             glEnable(GL_DEPTH_CLAMP_NV);
569             checkGLcall("glEnable(GL_DEPTH_CLAMP_NV)");
570         }
571     }
572
573     if (enable & WINED3DCLIPPLANE0)  { glEnable(GL_CLIP_PLANE0);  checkGLcall("glEnable(clip plane 0)"); }
574     if (enable & WINED3DCLIPPLANE1)  { glEnable(GL_CLIP_PLANE1);  checkGLcall("glEnable(clip plane 1)"); }
575     if (enable & WINED3DCLIPPLANE2)  { glEnable(GL_CLIP_PLANE2);  checkGLcall("glEnable(clip plane 2)"); }
576     if (enable & WINED3DCLIPPLANE3)  { glEnable(GL_CLIP_PLANE3);  checkGLcall("glEnable(clip plane 3)"); }
577     if (enable & WINED3DCLIPPLANE4)  { glEnable(GL_CLIP_PLANE4);  checkGLcall("glEnable(clip plane 4)"); }
578     if (enable & WINED3DCLIPPLANE5)  { glEnable(GL_CLIP_PLANE5);  checkGLcall("glEnable(clip plane 5)"); }
579
580     if (disable & WINED3DCLIPPLANE0) { glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)"); }
581     if (disable & WINED3DCLIPPLANE1) { glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)"); }
582     if (disable & WINED3DCLIPPLANE2) { glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)"); }
583     if (disable & WINED3DCLIPPLANE3) { glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)"); }
584     if (disable & WINED3DCLIPPLANE4) { glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)"); }
585     if (disable & WINED3DCLIPPLANE5) { glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)"); }
586
587     /** update clipping status */
588     if (enable) {
589         stateblock->clip_status.ClipUnion = 0;
590         stateblock->clip_status.ClipIntersection = 0xFFFFFFFF;
591     } else {
592         stateblock->clip_status.ClipUnion = 0;
593         stateblock->clip_status.ClipIntersection = 0;
594     }
595 }
596
597 static void state_blendop_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
598     WARN("Unsupported in local OpenGL implementation: glBlendEquation\n");
599 }
600
601 static void state_blendop(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
602     int blendEquation = GL_FUNC_ADD;
603     int blendEquationAlpha = GL_FUNC_ADD;
604
605     /* BLENDOPALPHA requires GL_EXT_blend_equation_separate, so make sure it is around */
606     if(stateblock->renderState[WINED3DRS_BLENDOPALPHA] && !GL_SUPPORT(EXT_BLEND_EQUATION_SEPARATE)) {
607         WARN("Unsupported in local OpenGL implementation: glBlendEquationSeparateEXT\n");
608         return;
609     }
610
611     switch ((WINED3DBLENDOP) stateblock->renderState[WINED3DRS_BLENDOP]) {
612         case WINED3DBLENDOP_ADD              : blendEquation = GL_FUNC_ADD;              break;
613         case WINED3DBLENDOP_SUBTRACT         : blendEquation = GL_FUNC_SUBTRACT;         break;
614         case WINED3DBLENDOP_REVSUBTRACT      : blendEquation = GL_FUNC_REVERSE_SUBTRACT; break;
615         case WINED3DBLENDOP_MIN              : blendEquation = GL_MIN;                   break;
616         case WINED3DBLENDOP_MAX              : blendEquation = GL_MAX;                   break;
617         default:
618             FIXME("Unrecognized/Unhandled D3DBLENDOP value %d\n", stateblock->renderState[WINED3DRS_BLENDOP]);
619     }
620
621     switch ((WINED3DBLENDOP) stateblock->renderState[WINED3DRS_BLENDOPALPHA]) {
622         case WINED3DBLENDOP_ADD              : blendEquationAlpha = GL_FUNC_ADD;              break;
623         case WINED3DBLENDOP_SUBTRACT         : blendEquationAlpha = GL_FUNC_SUBTRACT;         break;
624         case WINED3DBLENDOP_REVSUBTRACT      : blendEquationAlpha = GL_FUNC_REVERSE_SUBTRACT; break;
625         case WINED3DBLENDOP_MIN              : blendEquationAlpha = GL_MIN;                   break;
626         case WINED3DBLENDOP_MAX              : blendEquationAlpha = GL_MAX;                   break;
627         default:
628             FIXME("Unrecognized/Unhandled D3DBLENDOP value %d\n", stateblock->renderState[WINED3DRS_BLENDOPALPHA]);
629     }
630
631     if(stateblock->renderState[WINED3DRS_SEPARATEALPHABLENDENABLE]) {
632         TRACE("glBlendEquationSeparateEXT(%x, %x)\n", blendEquation, blendEquationAlpha);
633         GL_EXTCALL(glBlendEquationSeparateEXT(blendEquation, blendEquationAlpha));
634         checkGLcall("glBlendEquationSeparateEXT");
635     } else {
636         TRACE("glBlendEquation(%x)\n", blendEquation);
637         GL_EXTCALL(glBlendEquationEXT(blendEquation));
638         checkGLcall("glBlendEquation");
639     }
640 }
641
642 static void
643 state_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
644     /* Originally this used glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR)
645      * and (GL_LIGHT_MODEL_COLOR_CONTROL,GL_SINGLE_COLOR) to swap between enabled/disabled
646      * specular color. This is wrong:
647      * Separate specular color means the specular colour is maintained separately, whereas
648      * single color means it is merged in. However in both cases they are being used to
649      * some extent.
650      * To disable specular color, set it explicitly to black and turn off GL_COLOR_SUM_EXT
651      * NOTE: If not supported don't give FIXMEs the impact is really minimal and very few people are
652      * running 1.4 yet!
653      *
654      *
655      * If register combiners are enabled, enabling / disabling GL_COLOR_SUM has no effect.
656      * Instead, we need to setup the FinalCombiner properly.
657      *
658      * The default setup for the FinalCombiner is:
659      *
660      * <variable>       <input>                             <mapping>               <usage>
661      * GL_VARIABLE_A_NV GL_FOG,                             GL_UNSIGNED_IDENTITY_NV GL_ALPHA
662      * GL_VARIABLE_B_NV GL_SPARE0_PLUS_SECONDARY_COLOR_NV   GL_UNSIGNED_IDENTITY_NV GL_RGB
663      * GL_VARIABLE_C_NV GL_FOG                              GL_UNSIGNED_IDENTITY_NV GL_RGB
664      * GL_VARIABLE_D_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
665      * GL_VARIABLE_E_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
666      * GL_VARIABLE_F_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
667      * GL_VARIABLE_G_NV GL_SPARE0_NV                        GL_UNSIGNED_IDENTITY_NV GL_ALPHA
668      *
669      * That's pretty much fine as it is, except for variable B, which needs to take
670      * either GL_SPARE0_PLUS_SECONDARY_COLOR_NV or GL_SPARE0_NV, depending on
671      * whether WINED3DRS_SPECULARENABLE is enabled or not.
672      */
673
674     TRACE("Setting specular enable state and materials\n");
675     if (stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
676         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (float*) &stateblock->material.Specular);
677         checkGLcall("glMaterialfv");
678
679         if(stateblock->material.Power > GL_LIMITS(shininess)) {
680             /* glMaterialf man page says that the material says that GL_SHININESS must be between 0.0
681              * and 128.0, although in d3d neither -1 nor 129 produce an error. GL_NV_max_light_exponent
682              * allows bigger values. If the extension is supported, GL_LIMITS(shininess) contains the
683              * value reported by the extension, otherwise 128. For values > GL_LIMITS(shininess) clamp
684              * them, it should be safe to do so without major visual distortions.
685              */
686             WARN("Material power = %f, limit %f\n", stateblock->material.Power, GL_LIMITS(shininess));
687             glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, GL_LIMITS(shininess));
688         } else {
689             glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, stateblock->material.Power);
690         }
691         checkGLcall("glMaterialf(GL_SHININESS)");
692
693         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
694             glEnable(GL_COLOR_SUM_EXT);
695         } else {
696             TRACE("Specular colors cannot be enabled in this version of opengl\n");
697         }
698         checkGLcall("glEnable(GL_COLOR_SUM)");
699
700         if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
701             GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_PLUS_SECONDARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
702             checkGLcall("glFinalCombinerInputNV()");
703         }
704     } else {
705         float black[4] = {0.0f, 0.0f, 0.0f, 0.0f};
706
707         /* for the case of enabled lighting: */
708         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &black[0]);
709         checkGLcall("glMaterialfv");
710
711         /* for the case of disabled lighting: */
712         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
713             glDisable(GL_COLOR_SUM_EXT);
714         } else {
715             TRACE("Specular colors cannot be disabled in this version of opengl\n");
716         }
717         checkGLcall("glDisable(GL_COLOR_SUM)");
718
719         if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
720             GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
721             checkGLcall("glFinalCombinerInputNV()");
722         }
723     }
724
725     TRACE("(%p) : Diffuse (%f,%f,%f,%f)\n", stateblock->wineD3DDevice, stateblock->material.Diffuse.r, stateblock->material.Diffuse.g,
726           stateblock->material.Diffuse.b, stateblock->material.Diffuse.a);
727     TRACE("(%p) : Ambient (%f,%f,%f,%f)\n", stateblock->wineD3DDevice, stateblock->material.Ambient.r, stateblock->material.Ambient.g,
728           stateblock->material.Ambient.b, stateblock->material.Ambient.a);
729     TRACE("(%p) : Specular (%f,%f,%f,%f)\n", stateblock->wineD3DDevice, stateblock->material.Specular.r, stateblock->material.Specular.g,
730           stateblock->material.Specular.b, stateblock->material.Specular.a);
731     TRACE("(%p) : Emissive (%f,%f,%f,%f)\n", stateblock->wineD3DDevice, stateblock->material.Emissive.r, stateblock->material.Emissive.g,
732           stateblock->material.Emissive.b, stateblock->material.Emissive.a);
733
734     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (float*) &stateblock->material.Ambient);
735     checkGLcall("glMaterialfv(GL_AMBIENT)");
736     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (float*) &stateblock->material.Diffuse);
737     checkGLcall("glMaterialfv(GL_DIFFUSE)");
738     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (float*) &stateblock->material.Emissive);
739     checkGLcall("glMaterialfv(GL_EMISSION)");
740 }
741
742 static void state_texfactor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
743     unsigned int i;
744
745     /* Note the texture color applies to all textures whereas
746      * GL_TEXTURE_ENV_COLOR applies to active only
747      */
748     float col[4];
749     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
750
751     /* And now the default texture color as well */
752     for (i = 0; i < GL_LIMITS(texture_stages); i++) {
753         /* Note the WINED3DRS value applies to all textures, but GL has one
754             * per texture, so apply it now ready to be used!
755             */
756         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
757             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
758             checkGLcall("glActiveTextureARB");
759         } else if (i>0) {
760             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
761         }
762
763         glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &col[0]);
764         checkGLcall("glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color);");
765     }
766 }
767
768 static void
769 renderstate_stencil_twosided(IWineD3DStateBlockImpl *stateblock, GLint face, GLint func, GLint ref, GLuint mask, GLint stencilFail, GLint depthFail, GLint stencilPass ) {
770     glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
771     checkGLcall("glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT)");
772     GL_EXTCALL(glActiveStencilFaceEXT(face));
773     checkGLcall("glActiveStencilFaceEXT(...)");
774     glStencilFunc(func, ref, mask);
775     checkGLcall("glStencilFunc(...)");
776     glStencilOp(stencilFail, depthFail, stencilPass);
777     checkGLcall("glStencilOp(...)");
778 }
779
780 static void
781 state_stencil(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
782     DWORD onesided_enable = FALSE;
783     DWORD twosided_enable = FALSE;
784     GLint func = GL_ALWAYS;
785     GLint func_ccw = GL_ALWAYS;
786     GLint ref = 0;
787     GLuint mask = 0;
788     GLint stencilFail = GL_KEEP;
789     GLint depthFail = GL_KEEP;
790     GLint stencilPass = GL_KEEP;
791     GLint stencilFail_ccw = GL_KEEP;
792     GLint depthFail_ccw = GL_KEEP;
793     GLint stencilPass_ccw = GL_KEEP;
794
795     /* No stencil test without a stencil buffer */
796     if(stateblock->wineD3DDevice->stencilBufferTarget == NULL) {
797         glDisable(GL_STENCIL_TEST);
798         checkGLcall("glDisable GL_STENCIL_TEST");
799         return;
800     }
801
802     onesided_enable = stateblock->renderState[WINED3DRS_STENCILENABLE];
803     twosided_enable = stateblock->renderState[WINED3DRS_TWOSIDEDSTENCILMODE];
804     if( !( func = CompareFunc(stateblock->renderState[WINED3DRS_STENCILFUNC]) ) )
805         func = GL_ALWAYS;
806     if( !( func_ccw = CompareFunc(stateblock->renderState[WINED3DRS_CCW_STENCILFUNC]) ) )
807         func_ccw = GL_ALWAYS;
808     ref = stateblock->renderState[WINED3DRS_STENCILREF];
809     mask = stateblock->renderState[WINED3DRS_STENCILMASK];
810     stencilFail = StencilOp(stateblock->renderState[WINED3DRS_STENCILFAIL]);
811     depthFail = StencilOp(stateblock->renderState[WINED3DRS_STENCILZFAIL]);
812     stencilPass = StencilOp(stateblock->renderState[WINED3DRS_STENCILPASS]);
813     stencilFail_ccw = StencilOp(stateblock->renderState[WINED3DRS_CCW_STENCILFAIL]);
814     depthFail_ccw = StencilOp(stateblock->renderState[WINED3DRS_CCW_STENCILZFAIL]);
815     stencilPass_ccw = StencilOp(stateblock->renderState[WINED3DRS_CCW_STENCILPASS]);
816
817     TRACE("(onesided %d, twosided %d, ref %x, mask %x, "
818           "GL_FRONT: func: %x, fail %x, zfail %x, zpass %x "
819           "GL_BACK: func: %x, fail %x, zfail %x, zpass %x )\n",
820     onesided_enable, twosided_enable, ref, mask,
821     func, stencilFail, depthFail, stencilPass,
822     func_ccw, stencilFail_ccw, depthFail_ccw, stencilPass_ccw);
823
824     if (twosided_enable && onesided_enable) {
825         glEnable(GL_STENCIL_TEST);
826         checkGLcall("glEnable GL_STENCIL_TEST");
827
828         if(GL_SUPPORT(EXT_STENCIL_TWO_SIDE)) {
829             /* Apply back first, then front. This function calls glActiveStencilFaceEXT,
830              * which has an effect on the code below too. If we apply the front face
831              * afterwards, we are sure that the active stencil face is set to front,
832              * and other stencil functions which do not use two sided stencil do not have
833              * to set it back
834              */
835             renderstate_stencil_twosided(stateblock, GL_BACK, func_ccw, ref, mask,
836                                          stencilFail_ccw, depthFail_ccw, stencilPass_ccw);
837             renderstate_stencil_twosided(stateblock, GL_FRONT, func, ref, mask,
838                                          stencilFail, depthFail, stencilPass);
839         } else if(GL_SUPPORT(ATI_SEPARATE_STENCIL)) {
840             GL_EXTCALL(glStencilFuncSeparateATI(func, func_ccw, ref, mask));
841             checkGLcall("glStencilFuncSeparateATI(...)");
842             GL_EXTCALL(glStencilOpSeparateATI(GL_FRONT, stencilFail, depthFail, stencilPass));
843             checkGLcall("glStencilOpSeparateATI(GL_FRONT, ...)");
844             GL_EXTCALL(glStencilOpSeparateATI(GL_BACK, stencilFail_ccw, depthFail_ccw, stencilPass_ccw));
845             checkGLcall("glStencilOpSeparateATI(GL_BACK, ...)");
846         } else {
847             ERR("Separate (two sided) stencil not supported on this version of opengl. Caps weren't honored?\n");
848         }
849     } else if(onesided_enable) {
850         if(GL_SUPPORT(EXT_STENCIL_TWO_SIDE)) {
851             glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
852             checkGLcall("glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT)");
853         }
854
855         /* This code disables the ATI extension as well, since the standard stencil functions are equal
856          * to calling the ATI functions with GL_FRONT_AND_BACK as face parameter
857          */
858         glEnable(GL_STENCIL_TEST);
859         checkGLcall("glEnable GL_STENCIL_TEST");
860         glStencilFunc(func, ref, mask);
861         checkGLcall("glStencilFunc(...)");
862         glStencilOp(stencilFail, depthFail, stencilPass);
863         checkGLcall("glStencilOp(...)");
864     } else {
865         glDisable(GL_STENCIL_TEST);
866         checkGLcall("glDisable GL_STENCIL_TEST");
867     }
868 }
869
870 static void state_stencilwrite2s(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
871     DWORD mask;
872
873     if(stateblock->wineD3DDevice->stencilBufferTarget) {
874         mask = stateblock->renderState[WINED3DRS_STENCILWRITEMASK];
875     } else {
876         mask = 0;
877     }
878
879     GL_EXTCALL(glActiveStencilFaceEXT(GL_BACK));
880     checkGLcall("glActiveStencilFaceEXT(GL_BACK)");
881     glStencilMask(mask);
882     checkGLcall("glStencilMask");
883     GL_EXTCALL(glActiveStencilFaceEXT(GL_FRONT));
884     checkGLcall("glActiveStencilFaceEXT(GL_FRONT)");
885     glStencilMask(mask);
886 }
887
888 static void state_stencilwrite(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
889     DWORD mask;
890
891     if(stateblock->wineD3DDevice->stencilBufferTarget) {
892         mask = stateblock->renderState[WINED3DRS_STENCILWRITEMASK];
893     } else {
894         mask = 0;
895     }
896
897     glStencilMask(mask);
898     checkGLcall("glStencilMask");
899 }
900
901 static void state_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
902     BOOL fogenable = stateblock->renderState[WINED3DRS_FOGENABLE];
903     BOOL is_ps3 = use_ps(stateblock->wineD3DDevice)
904                   && ((IWineD3DPixelShaderImpl *)stateblock->pixelShader)->baseShader.hex_version >= WINED3DPS_VERSION(3,0);
905     float fogstart, fogend;
906
907     union {
908         DWORD d;
909         float f;
910     } tmpvalue;
911
912     if (!fogenable) {
913         /* No fog? Disable it, and we're done :-) */
914         glDisable(GL_FOG);
915         checkGLcall("glDisable GL_FOG");
916         if( use_ps(stateblock->wineD3DDevice)
917                 && ((IWineD3DPixelShaderImpl *)stateblock->pixelShader)->baseShader.hex_version < WINED3DPS_VERSION(3,0) ) {
918             /* disable fog in the pixel shader
919              * NOTE: For pixel shader, GL_FOG_START and GL_FOG_END don't hold fog start s and end e but
920              * -1/(e-s) and e/(e-s) respectively.
921              */
922             glFogf(GL_FOG_START, 0.0f);
923             checkGLcall("glFogf(GL_FOG_START, fogstart)");
924             glFogf(GL_FOG_END, 1.0f);
925             checkGLcall("glFogf(GL_FOG_END, fogend)");
926         }
927         return;
928     }
929
930     tmpvalue.d = stateblock->renderState[WINED3DRS_FOGSTART];
931     fogstart = tmpvalue.f;
932     tmpvalue.d = stateblock->renderState[WINED3DRS_FOGEND];
933     fogend = tmpvalue.f;
934
935     /* Fog Rules:
936      *
937      * With fixed function vertex processing, Direct3D knows 2 different fog input sources.
938      * It can use the Z value of the vertex, or the alpha component of the specular color.
939      * This depends on the fog vertex, fog table and the vertex declaration. If the Z value
940      * is used, fogstart, fogend and the equation type are used, otherwise linear fog with
941      * start = 255, end = 0 is used. Obviously the msdn is not very clear on that.
942      *
943      * FOGTABLEMODE != NONE:
944      *  The Z value is used, with the equation specified, no matter what vertex type.
945      *
946      * FOGTABLEMODE == NONE, FOGVERTEXMODE != NONE, untransformed:
947      *  Per vertex fog is calculated using the specified fog equation and the parameters
948      *
949      * FOGTABLEMODE == NONE, FOGVERTEXMODE != NONE, transformed, OR
950      * FOGTABLEMODE == NONE, FOGVERTEXMODE == NONE, untransformed:
951      *  Linear fog with start = 255.0, end = 0.0, input comes from the specular color
952      *
953      *
954      * Rules for vertex fog with shaders:
955      *
956      * When mixing fixed function functionality with the programmable pipeline, D3D expects
957      * the fog computation to happen during transformation while openGL expects it to happen
958      * during rasterization. Also, prior to pixel shader 3.0 D3D handles fog blending after
959      * the pixel shader while openGL always expects the pixel shader to handle the blending.
960      * To solve this problem, WineD3D does:
961      * 1) implement a linear fog equation and fog blending at the end of every pre 3.0 pixel
962      * shader,
963      * and 2) disables the fog computation (in either the fixed function or programmable
964      * rasterizer) if using a vertex program.
965      *
966      *
967      * If a fogtablemode and a fogvertexmode are specified, table fog is applied (with or
968      * without shaders).
969      */
970
971     if( is_ps3 ) {
972         if( !use_vs(stateblock->wineD3DDevice)
973                 && stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE ) {
974             FIXME("Implement vertex fog for pixel shader >= 3.0 and fixed function pipeline\n");
975         }
976     }
977
978     if (use_vs(stateblock->wineD3DDevice)
979             && ((IWineD3DVertexShaderImpl *)stateblock->vertexShader)->baseShader.reg_maps.fog) {
980         if( stateblock->renderState[WINED3DRS_FOGTABLEMODE] != WINED3DFOG_NONE ) {
981             if(!is_ps3) FIXME("Implement table fog for foggy vertex shader\n");
982             /* Disable fog */
983             fogenable = FALSE;
984         } else {
985             /* Set fog computation in the rasterizer to pass through the value (just blend it) */
986             glFogi(GL_FOG_MODE, GL_LINEAR);
987             checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
988             fogstart = 1.0;
989             fogend = 0.0;
990         }
991
992         if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
993             glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
994             checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
995             context->fog_coord = FALSE;
996         }
997         context->last_was_foggy_shader = TRUE;
998     }
999     else if( use_ps(stateblock->wineD3DDevice) ) {
1000         /* NOTE: For pixel shader, GL_FOG_START and GL_FOG_END don't hold fog start s and end e but
1001          * -1/(e-s) and e/(e-s) respectively to simplify fog computation in the shader.
1002          */
1003         WINED3DFOGMODE mode;
1004         context->last_was_foggy_shader = FALSE;
1005
1006         /* If both fogmodes are set use the table fog mode */
1007         if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
1008             mode = stateblock->renderState[WINED3DRS_FOGVERTEXMODE];
1009         else
1010             mode = stateblock->renderState[WINED3DRS_FOGTABLEMODE];
1011
1012         switch (mode) {
1013             case WINED3DFOG_EXP:
1014             case WINED3DFOG_EXP2:
1015                 if(!is_ps3) FIXME("Implement non linear fog for pixel shader < 3.0\n");
1016                 /* Disable fog */
1017                 fogenable = FALSE;
1018                 break;
1019
1020             case WINED3DFOG_LINEAR:
1021                 fogstart = -1.0f/(fogend-fogstart);
1022                 fogend *= -fogstart;
1023                 break;
1024
1025             case WINED3DFOG_NONE:
1026                 if(!is_ps3) FIXME("Implement software vertex fog for pixel shader < 3.0\n");
1027                 /* Disable fog */
1028                 fogenable = FALSE;
1029                 break;
1030             default: FIXME("Unexpected WINED3DRS_FOGVERTEXMODE %d\n", stateblock->renderState[WINED3DRS_FOGVERTEXMODE]);
1031         }
1032
1033         if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1034             glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1035             checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1036             context->fog_coord = FALSE;
1037         }
1038     }
1039     /* DX 7 sdk: "If both render states(vertex and table fog) are set to valid modes,
1040      * the system will apply only pixel(=table) fog effects."
1041      */
1042     else if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
1043         glHint(GL_FOG_HINT, GL_FASTEST);
1044         checkGLcall("glHint(GL_FOG_HINT, GL_FASTEST)");
1045         context->last_was_foggy_shader = FALSE;
1046
1047         switch (stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
1048             /* If processed vertices are used, fall through to the NONE case */
1049             case WINED3DFOG_EXP:  {
1050                 if(!context->last_was_rhw) {
1051                     glFogi(GL_FOG_MODE, GL_EXP);
1052                     checkGLcall("glFogi(GL_FOG_MODE, GL_EXP)");
1053                     if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1054                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1055                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1056                         context->fog_coord = FALSE;
1057                     }
1058                     break;
1059                 }
1060             }
1061             case WINED3DFOG_EXP2: {
1062                 if(!context->last_was_rhw) {
1063                     glFogi(GL_FOG_MODE, GL_EXP2);
1064                     checkGLcall("glFogi(GL_FOG_MODE, GL_EXP2)");
1065                     if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1066                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1067                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1068                         context->fog_coord = FALSE;
1069                     }
1070                     break;
1071                 }
1072             }
1073             case WINED3DFOG_LINEAR: {
1074                 if(!context->last_was_rhw) {
1075                     glFogi(GL_FOG_MODE, GL_LINEAR);
1076                     checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
1077                     if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1078                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1079                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1080                         context->fog_coord = FALSE;
1081                     }
1082                     break;
1083                 }
1084             }
1085             case WINED3DFOG_NONE: {
1086                 /* Both are none? According to msdn the alpha channel of the specular
1087                  * color contains a fog factor. Set it in drawStridedSlow.
1088                  * Same happens with Vertexfog on transformed vertices
1089                  */
1090                 if(GL_SUPPORT(EXT_FOG_COORD)) {
1091                     if(context->fog_coord == FALSE) {
1092                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
1093                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT)");
1094                         context->fog_coord = TRUE;
1095                     }
1096                     glFogi(GL_FOG_MODE, GL_LINEAR);
1097                     checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
1098                     fogstart = 0xff;
1099                     fogend = 0x0;
1100                 } else {
1101                     /* Disable GL fog, handle this in software in drawStridedSlow */
1102                     fogenable = FALSE;
1103                 }
1104                 break;
1105             }
1106             default: FIXME("Unexpected WINED3DRS_FOGVERTEXMODE %d\n", stateblock->renderState[WINED3DRS_FOGVERTEXMODE]);
1107         }
1108     } else {
1109         glHint(GL_FOG_HINT, GL_NICEST);
1110         checkGLcall("glHint(GL_FOG_HINT, GL_NICEST)");
1111         context->last_was_foggy_shader = FALSE;
1112
1113         switch (stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
1114             case WINED3DFOG_EXP:
1115                 glFogi(GL_FOG_MODE, GL_EXP);
1116                 checkGLcall("glFogi(GL_FOG_MODE, GL_EXP)");
1117                 if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1118                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1119                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1120                     context->fog_coord = FALSE;
1121                 }
1122                 break;
1123
1124             case WINED3DFOG_EXP2:
1125                 glFogi(GL_FOG_MODE, GL_EXP2);
1126                 checkGLcall("glFogi(GL_FOG_MODE, GL_EXP2)");
1127                 if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1128                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1129                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1130                     context->fog_coord = FALSE;
1131                 }
1132                 break;
1133
1134             case WINED3DFOG_LINEAR:
1135                 glFogi(GL_FOG_MODE, GL_LINEAR);
1136                 checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
1137                 if(GL_SUPPORT(EXT_FOG_COORD) && context->fog_coord) {
1138                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1139                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
1140                     context->fog_coord = FALSE;
1141                 }
1142                 break;
1143
1144             case WINED3DFOG_NONE:   /* Won't happen */
1145             default:
1146                 FIXME("Unexpected WINED3DRS_FOGTABLEMODE %d\n", stateblock->renderState[WINED3DRS_FOGTABLEMODE]);
1147         }
1148     }
1149
1150     if(fogenable) {
1151         glEnable(GL_FOG);
1152         checkGLcall("glEnable GL_FOG");
1153
1154         if(fogstart != fogend)
1155         {
1156             glFogfv(GL_FOG_START, &fogstart);
1157             checkGLcall("glFogf(GL_FOG_START, fogstart)");
1158             TRACE("Fog Start == %f\n", fogstart);
1159
1160             glFogfv(GL_FOG_END, &fogend);
1161             checkGLcall("glFogf(GL_FOG_END, fogend)");
1162             TRACE("Fog End == %f\n", fogend);
1163         }
1164         else
1165         {
1166             glFogf(GL_FOG_START, -1.0 / 0.0);
1167             checkGLcall("glFogf(GL_FOG_START, fogstart)");
1168             TRACE("Fog Start == %f\n", fogstart);
1169
1170             glFogf(GL_FOG_END, 0.0);
1171             checkGLcall("glFogf(GL_FOG_END, fogend)");
1172             TRACE("Fog End == %f\n", fogend);
1173         }
1174     } else {
1175         glDisable(GL_FOG);
1176         checkGLcall("glDisable GL_FOG");
1177         if( use_ps(stateblock->wineD3DDevice) ) {
1178             /* disable fog in the pixel shader
1179              * NOTE: For pixel shader, GL_FOG_START and GL_FOG_END don't hold fog start s and end e but
1180              * -1/(e-s) and e/(e-s) respectively.
1181              */
1182             glFogf(GL_FOG_START, 0.0f);
1183             checkGLcall("glFogf(GL_FOG_START, fogstart)");
1184             glFogf(GL_FOG_END, 1.0f);
1185             checkGLcall("glFogf(GL_FOG_END, fogend)");
1186         }
1187     }
1188 }
1189
1190 static void state_rangefog_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1191     if(stateblock->renderState[WINED3DRS_RANGEFOGENABLE]) {
1192         WARN("Range fog enabled, but not supported by this opengl implementation\n");
1193     }
1194 }
1195
1196 static void state_rangefog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1197     if(stateblock->renderState[WINED3DRS_RANGEFOGENABLE]) {
1198         glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_RADIAL_NV);
1199         checkGLcall("glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_RADIAL_NV)");
1200     } else {
1201         glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV);
1202         checkGLcall("glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV)");
1203     }
1204 }
1205
1206 static void state_fogcolor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1207     float col[4];
1208     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_FOGCOLOR], col);
1209     glFogfv(GL_FOG_COLOR, &col[0]);
1210     checkGLcall("glFog GL_FOG_COLOR");
1211 }
1212
1213 static void state_fogdensity(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1214     union {
1215         DWORD d;
1216         float f;
1217     } tmpvalue;
1218     tmpvalue.d = stateblock->renderState[WINED3DRS_FOGDENSITY];
1219     glFogfv(GL_FOG_DENSITY, &tmpvalue.f);
1220     checkGLcall("glFogf(GL_FOG_DENSITY, (float) Value)");
1221 }
1222
1223 /* TODO: Merge with primitive type + init_materials()!! */
1224 static void state_colormat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1225     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
1226     GLenum Parm = 0;
1227     WineDirect3DStridedData *diffuse = &device->strided_streams.u.s.diffuse;
1228     BOOL isDiffuseSupplied;
1229
1230     /* Depends on the decoded vertex declaration to read the existence of diffuse data.
1231      * The vertex declaration will call this function if the fixed function pipeline is used.
1232      */
1233
1234     if(isStateDirty(context, STATE_VDECL)) {
1235         return;
1236     }
1237
1238     isDiffuseSupplied = diffuse->lpData || diffuse->VBO;
1239
1240     context->num_untracked_materials = 0;
1241     if (isDiffuseSupplied && stateblock->renderState[WINED3DRS_COLORVERTEX]) {
1242         TRACE("diff %d, amb %d, emis %d, spec %d\n",
1243               stateblock->renderState[WINED3DRS_DIFFUSEMATERIALSOURCE],
1244               stateblock->renderState[WINED3DRS_AMBIENTMATERIALSOURCE],
1245               stateblock->renderState[WINED3DRS_EMISSIVEMATERIALSOURCE],
1246               stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE]);
1247
1248         if (stateblock->renderState[WINED3DRS_DIFFUSEMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1249             if (stateblock->renderState[WINED3DRS_AMBIENTMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1250                 Parm = GL_AMBIENT_AND_DIFFUSE;
1251             } else {
1252                 Parm = GL_DIFFUSE;
1253             }
1254             if(stateblock->renderState[WINED3DRS_EMISSIVEMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1255                 context->untracked_materials[context->num_untracked_materials] = GL_EMISSION;
1256                 context->num_untracked_materials++;
1257             }
1258             if(stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1259                 context->untracked_materials[context->num_untracked_materials] = GL_SPECULAR;
1260                 context->num_untracked_materials++;
1261             }
1262         } else if (stateblock->renderState[WINED3DRS_AMBIENTMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1263             Parm = GL_AMBIENT;
1264             if(stateblock->renderState[WINED3DRS_EMISSIVEMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1265                 context->untracked_materials[context->num_untracked_materials] = GL_EMISSION;
1266                 context->num_untracked_materials++;
1267             }
1268             if(stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1269                 context->untracked_materials[context->num_untracked_materials] = GL_SPECULAR;
1270                 context->num_untracked_materials++;
1271             }
1272         } else if (stateblock->renderState[WINED3DRS_EMISSIVEMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1273             Parm = GL_EMISSION;
1274             if(stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1275                 context->untracked_materials[context->num_untracked_materials] = GL_SPECULAR;
1276                 context->num_untracked_materials++;
1277             }
1278         } else if (stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE] == WINED3DMCS_COLOR1) {
1279             Parm = GL_SPECULAR;
1280         }
1281     }
1282
1283     /* Nothing changed, return. */
1284     if (Parm == context->tracking_parm) return;
1285
1286     if(!Parm) {
1287         glDisable(GL_COLOR_MATERIAL);
1288         checkGLcall("glDisable GL_COLOR_MATERIAL");
1289     } else {
1290         glColorMaterial(GL_FRONT_AND_BACK, Parm);
1291         checkGLcall("glColorMaterial(GL_FRONT_AND_BACK, Parm)");
1292         glEnable(GL_COLOR_MATERIAL);
1293         checkGLcall("glEnable(GL_COLOR_MATERIAL)");
1294     }
1295
1296     /* Apparently calls to glMaterialfv are ignored for properties we're
1297      * tracking with glColorMaterial, so apply those here. */
1298     switch (context->tracking_parm) {
1299         case GL_AMBIENT_AND_DIFFUSE:
1300             glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (float*)&device->updateStateBlock->material.Ambient);
1301             glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (float*)&device->updateStateBlock->material.Diffuse);
1302             checkGLcall("glMaterialfv");
1303             break;
1304
1305         case GL_DIFFUSE:
1306             glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (float*)&device->updateStateBlock->material.Diffuse);
1307             checkGLcall("glMaterialfv");
1308             break;
1309
1310         case GL_AMBIENT:
1311             glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (float*)&device->updateStateBlock->material.Ambient);
1312             checkGLcall("glMaterialfv");
1313             break;
1314
1315         case GL_EMISSION:
1316             glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (float*)&device->updateStateBlock->material.Emissive);
1317             checkGLcall("glMaterialfv");
1318             break;
1319
1320         case GL_SPECULAR:
1321             /* Only change material color if specular is enabled, otherwise it is set to black */
1322             if (device->stateBlock->renderState[WINED3DRS_SPECULARENABLE]) {
1323                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (float*)&device->updateStateBlock->material.Specular);
1324                 checkGLcall("glMaterialfv");
1325             } else {
1326                 float black[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1327                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &black[0]);
1328                 checkGLcall("glMaterialfv");
1329             }
1330             break;
1331     }
1332
1333     context->tracking_parm = Parm;
1334 }
1335
1336 static void state_linepattern(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1337     union {
1338         DWORD                 d;
1339         WINED3DLINEPATTERN    lp;
1340     } tmppattern;
1341     tmppattern.d = stateblock->renderState[WINED3DRS_LINEPATTERN];
1342
1343     TRACE("Line pattern: repeat %d bits %x\n", tmppattern.lp.wRepeatFactor, tmppattern.lp.wLinePattern);
1344
1345     if (tmppattern.lp.wRepeatFactor) {
1346         glLineStipple(tmppattern.lp.wRepeatFactor, tmppattern.lp.wLinePattern);
1347         checkGLcall("glLineStipple(repeat, linepattern)");
1348         glEnable(GL_LINE_STIPPLE);
1349         checkGLcall("glEnable(GL_LINE_STIPPLE);");
1350     } else {
1351         glDisable(GL_LINE_STIPPLE);
1352         checkGLcall("glDisable(GL_LINE_STIPPLE);");
1353     }
1354 }
1355
1356 static void state_zbias(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1357     union {
1358         DWORD d;
1359         float f;
1360     } tmpvalue;
1361
1362     if (stateblock->renderState[WINED3DRS_ZBIAS]) {
1363         tmpvalue.d = stateblock->renderState[WINED3DRS_ZBIAS];
1364         TRACE("ZBias value %f\n", tmpvalue.f);
1365         glPolygonOffset(0, -tmpvalue.f);
1366         checkGLcall("glPolygonOffset(0, -Value)");
1367         glEnable(GL_POLYGON_OFFSET_FILL);
1368         checkGLcall("glEnable(GL_POLYGON_OFFSET_FILL);");
1369         glEnable(GL_POLYGON_OFFSET_LINE);
1370         checkGLcall("glEnable(GL_POLYGON_OFFSET_LINE);");
1371         glEnable(GL_POLYGON_OFFSET_POINT);
1372         checkGLcall("glEnable(GL_POLYGON_OFFSET_POINT);");
1373     } else {
1374         glDisable(GL_POLYGON_OFFSET_FILL);
1375         checkGLcall("glDisable(GL_POLYGON_OFFSET_FILL);");
1376         glDisable(GL_POLYGON_OFFSET_LINE);
1377         checkGLcall("glDisable(GL_POLYGON_OFFSET_LINE);");
1378         glDisable(GL_POLYGON_OFFSET_POINT);
1379         checkGLcall("glDisable(GL_POLYGON_OFFSET_POINT);");
1380     }
1381 }
1382
1383
1384 static void state_normalize(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1385     if(isStateDirty(context, STATE_VDECL)) {
1386         return;
1387     }
1388     /* Without vertex normals, we set the current normal to 0/0/0 to remove the diffuse factor
1389      * from the opengl lighting equation, as d3d does. Normalization of 0/0/0 can lead to a division
1390      * by zero and is not properly defined in opengl, so avoid it
1391      */
1392     if (stateblock->renderState[WINED3DRS_NORMALIZENORMALS] && (
1393         stateblock->wineD3DDevice->strided_streams.u.s.normal.lpData ||
1394         stateblock->wineD3DDevice->strided_streams.u.s.normal.VBO)) {
1395         glEnable(GL_NORMALIZE);
1396         checkGLcall("glEnable(GL_NORMALIZE);");
1397     } else {
1398         glDisable(GL_NORMALIZE);
1399         checkGLcall("glDisable(GL_NORMALIZE);");
1400     }
1401 }
1402
1403 static void state_psizemin_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1404     union {
1405         DWORD d;
1406         float f;
1407     } tmpvalue;
1408
1409     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MIN];
1410     if(tmpvalue.f != 1.0) {
1411         FIXME("WINED3DRS_POINTSIZE_MIN not supported on this opengl, value is %f\n", tmpvalue.f);
1412     }
1413 }
1414
1415 static void state_psizemin_ext(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1416     union {
1417         DWORD d;
1418         float f;
1419     } tmpvalue;
1420
1421     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MIN];
1422     GL_EXTCALL(glPointParameterfEXT)(GL_POINT_SIZE_MIN_EXT, tmpvalue.f);
1423     checkGLcall("glPointParameterfEXT(...)");
1424 }
1425
1426 static void state_psizemin_arb(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1427     union {
1428         DWORD d;
1429         float f;
1430     } tmpvalue;
1431
1432     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MIN];
1433     GL_EXTCALL(glPointParameterfARB)(GL_POINT_SIZE_MIN_ARB, tmpvalue.f);
1434     checkGLcall("glPointParameterfARB(...)");
1435 }
1436
1437 static void state_psizemax_arb(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1438     union {
1439         DWORD d;
1440         float f;
1441     } tmpvalue;
1442
1443     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MAX];
1444     GL_EXTCALL(glPointParameterfARB)(GL_POINT_SIZE_MAX_ARB, tmpvalue.f);
1445     checkGLcall("glPointParameterfARB(...)");
1446 }
1447
1448 static void state_psizemax_ext(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1449     union {
1450         DWORD d;
1451         float f;
1452     } tmpvalue;
1453
1454     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MAX];
1455     GL_EXTCALL(glPointParameterfEXT)(GL_POINT_SIZE_MAX_EXT, tmpvalue.f);
1456     checkGLcall("glPointParameterfEXT(...)");
1457 }
1458
1459 static void state_psizemax_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1460     union {
1461         DWORD d;
1462         float f;
1463     } tmpvalue;
1464
1465     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MAX];
1466     if(tmpvalue.f != 64.0) {
1467         FIXME("WINED3DRS_POINTSIZE_MAX not supported on this opengl, value is %f\n", tmpvalue.f);
1468     }
1469 }
1470
1471 static void state_pscale(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1472     /* TODO: Group this with the viewport */
1473     /*
1474      * POINTSCALEENABLE controls how point size value is treated. If set to
1475      * true, the point size is scaled with respect to height of viewport.
1476      * When set to false point size is in pixels.
1477      */
1478
1479     /* Default values */
1480     GLfloat att[3] = {1.0f, 0.0f, 0.0f};
1481     union {
1482         DWORD d;
1483         float f;
1484     } pointSize, A, B, C;
1485
1486     pointSize.d = stateblock->renderState[WINED3DRS_POINTSIZE];
1487     A.d = stateblock->renderState[WINED3DRS_POINTSCALE_A];
1488     B.d = stateblock->renderState[WINED3DRS_POINTSCALE_B];
1489     C.d = stateblock->renderState[WINED3DRS_POINTSCALE_C];
1490
1491     if(stateblock->renderState[WINED3DRS_POINTSCALEENABLE]) {
1492         GLfloat scaleFactor;
1493         float h = stateblock->viewport.Height;
1494
1495         if(pointSize.f < GL_LIMITS(pointsizemin)) {
1496             /*
1497              * Minimum valid point size for OpenGL is driver specific. For Direct3D it is
1498              * 0.0f. This means that OpenGL will clamp really small point sizes to the
1499              * driver minimum. To correct for this we need to multiply by the scale factor when sizes
1500              * are less than 1.0f. scale_factor =  1.0f / point_size.
1501              */
1502             scaleFactor = pointSize.f / GL_LIMITS(pointsizemin);
1503             /* Clamp the point size, don't rely on the driver to do it. MacOS says min point size
1504              * is 1.0, but then accepts points below that and draws too small points
1505              */
1506             pointSize.f = GL_LIMITS(pointsizemin);
1507         } else if(pointSize.f > GL_LIMITS(pointsize)) {
1508             /* gl already scales the input to glPointSize,
1509              * d3d scales the result after the point size scale.
1510              * If the point size is bigger than the max size, use the
1511              * scaling to scale it bigger, and set the gl point size to max
1512              */
1513             scaleFactor = pointSize.f / GL_LIMITS(pointsize);
1514             TRACE("scale: %f\n", scaleFactor);
1515             pointSize.f = GL_LIMITS(pointsize);
1516         } else {
1517             scaleFactor = 1.0f;
1518         }
1519         scaleFactor = pow(h * scaleFactor, 2);
1520
1521         att[0] = A.f / scaleFactor;
1522         att[1] = B.f / scaleFactor;
1523         att[2] = C.f / scaleFactor;
1524     }
1525
1526     if(GL_SUPPORT(ARB_POINT_PARAMETERS)) {
1527         GL_EXTCALL(glPointParameterfvARB)(GL_POINT_DISTANCE_ATTENUATION_ARB, att);
1528         checkGLcall("glPointParameterfvARB(GL_DISTANCE_ATTENUATION_ARB, ...)");
1529     }
1530     else if(GL_SUPPORT(EXT_POINT_PARAMETERS)) {
1531         GL_EXTCALL(glPointParameterfvEXT)(GL_DISTANCE_ATTENUATION_EXT, att);
1532         checkGLcall("glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, ...)");
1533     } else if(stateblock->renderState[WINED3DRS_POINTSCALEENABLE]) {
1534         WARN("POINT_PARAMETERS not supported in this version of opengl\n");
1535     }
1536
1537     glPointSize(pointSize.f);
1538     checkGLcall("glPointSize(...);");
1539 }
1540
1541 static void state_colorwrite(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1542     DWORD Value = stateblock->renderState[WINED3DRS_COLORWRITEENABLE];
1543
1544     TRACE("Color mask: r(%d) g(%d) b(%d) a(%d)\n",
1545         Value & WINED3DCOLORWRITEENABLE_RED   ? 1 : 0,
1546         Value & WINED3DCOLORWRITEENABLE_GREEN ? 1 : 0,
1547         Value & WINED3DCOLORWRITEENABLE_BLUE  ? 1 : 0,
1548         Value & WINED3DCOLORWRITEENABLE_ALPHA ? 1 : 0);
1549     glColorMask(Value & WINED3DCOLORWRITEENABLE_RED   ? GL_TRUE : GL_FALSE,
1550                 Value & WINED3DCOLORWRITEENABLE_GREEN ? GL_TRUE : GL_FALSE,
1551                 Value & WINED3DCOLORWRITEENABLE_BLUE  ? GL_TRUE : GL_FALSE,
1552                 Value & WINED3DCOLORWRITEENABLE_ALPHA ? GL_TRUE : GL_FALSE);
1553     checkGLcall("glColorMask(...)");
1554
1555     /* depends on WINED3DRS_COLORWRITEENABLE. */
1556     if(stateblock->renderState[WINED3DRS_COLORWRITEENABLE1] != 0x0000000F ||
1557        stateblock->renderState[WINED3DRS_COLORWRITEENABLE2] != 0x0000000F ||
1558        stateblock->renderState[WINED3DRS_COLORWRITEENABLE3] != 0x0000000F ) {
1559         ERR("(WINED3DRS_COLORWRITEENABLE1/2/3,%d,%d,%d) not yet implemented. Missing of cap D3DPMISCCAPS_INDEPENDENTWRITEMASKS wasn't honored?\n",
1560             stateblock->renderState[WINED3DRS_COLORWRITEENABLE1],
1561             stateblock->renderState[WINED3DRS_COLORWRITEENABLE2],
1562             stateblock->renderState[WINED3DRS_COLORWRITEENABLE3]);
1563     }
1564 }
1565
1566 static void state_localviewer(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1567     if(stateblock->renderState[WINED3DRS_LOCALVIEWER]) {
1568         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
1569         checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)");
1570     } else {
1571         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
1572         checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0)");
1573     }
1574 }
1575
1576 static void state_lastpixel(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1577     if(stateblock->renderState[WINED3DRS_LASTPIXEL]) {
1578         TRACE("Last Pixel Drawing Enabled\n");
1579     } else {
1580         static BOOL first = TRUE;
1581         if(first) {
1582             FIXME("Last Pixel Drawing Disabled, not handled yet\n");
1583             first = FALSE;
1584         } else {
1585             TRACE("Last Pixel Drawing Disabled, not handled yet\n");
1586         }
1587     }
1588 }
1589
1590 static void state_pointsprite_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1591     /* TODO: NV_POINT_SPRITE */
1592     if (stateblock->renderState[WINED3DRS_POINTSPRITEENABLE]) {
1593         TRACE("Point sprites not supported\n");
1594     }
1595 }
1596
1597 static void state_pointsprite(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1598     if (stateblock->renderState[WINED3DRS_POINTSPRITEENABLE]) {
1599         glEnable(GL_POINT_SPRITE_ARB);
1600         checkGLcall("glEnable(GL_POINT_SPRITE_ARB)");
1601     } else {
1602         glDisable(GL_POINT_SPRITE_ARB);
1603         checkGLcall("glDisable(GL_POINT_SPRITE_ARB)");
1604     }
1605 }
1606
1607 static void state_wrap(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1608     /**
1609      http://www.cosc.brocku.ca/Offerings/3P98/course/lectures/texture/
1610      http://www.gamedev.net/reference/programming/features/rendererdll3/page2.asp
1611      Discussion on the ways to turn on WRAPing to solve an OpenGL conversion problem.
1612      http://www.flipcode.org/cgi-bin/fcmsg.cgi?thread_show=10248
1613
1614      so far as I can tell, wrapping and texture-coordinate generate go hand in hand,
1615      */
1616     TRACE("Stub\n");
1617     if(stateblock->renderState[WINED3DRS_WRAP0] ||
1618        stateblock->renderState[WINED3DRS_WRAP1] ||
1619        stateblock->renderState[WINED3DRS_WRAP2] ||
1620        stateblock->renderState[WINED3DRS_WRAP3] ||
1621        stateblock->renderState[WINED3DRS_WRAP4] ||
1622        stateblock->renderState[WINED3DRS_WRAP5] ||
1623        stateblock->renderState[WINED3DRS_WRAP6] ||
1624        stateblock->renderState[WINED3DRS_WRAP7] ||
1625        stateblock->renderState[WINED3DRS_WRAP8] ||
1626        stateblock->renderState[WINED3DRS_WRAP9] ||
1627        stateblock->renderState[WINED3DRS_WRAP10] ||
1628        stateblock->renderState[WINED3DRS_WRAP11] ||
1629        stateblock->renderState[WINED3DRS_WRAP12] ||
1630        stateblock->renderState[WINED3DRS_WRAP13] ||
1631        stateblock->renderState[WINED3DRS_WRAP14] ||
1632        stateblock->renderState[WINED3DRS_WRAP15] ) {
1633         FIXME("(WINED3DRS_WRAP0) Texture wraping not yet supported\n");
1634     }
1635 }
1636
1637 static void state_msaa_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1638     if(stateblock->renderState[WINED3DRS_MULTISAMPLEANTIALIAS]) {
1639         WARN("Multisample antialiasing not supported by gl\n");
1640     }
1641 }
1642
1643 static void state_msaa(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1644     if(stateblock->renderState[WINED3DRS_MULTISAMPLEANTIALIAS]) {
1645         glEnable(GL_MULTISAMPLE_ARB);
1646         checkGLcall("glEnable(GL_MULTISAMPLE_ARB)");
1647     } else {
1648         glDisable(GL_MULTISAMPLE_ARB);
1649         checkGLcall("glDisable(GL_MULTISAMPLE_ARB)");
1650     }
1651 }
1652
1653 static void state_scissor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1654     if(stateblock->renderState[WINED3DRS_SCISSORTESTENABLE]) {
1655         glEnable(GL_SCISSOR_TEST);
1656         checkGLcall("glEnable(GL_SCISSOR_TEST)");
1657     } else {
1658         glDisable(GL_SCISSOR_TEST);
1659         checkGLcall("glDisable(GL_SCISSOR_TEST)");
1660     }
1661 }
1662
1663 static void state_depthbias(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1664     union {
1665         DWORD d;
1666         float f;
1667     } tmpvalue;
1668
1669     if(stateblock->renderState[WINED3DRS_SLOPESCALEDEPTHBIAS] ||
1670        stateblock->renderState[WINED3DRS_DEPTHBIAS]) {
1671         tmpvalue.d = stateblock->renderState[WINED3DRS_SLOPESCALEDEPTHBIAS];
1672         glEnable(GL_POLYGON_OFFSET_FILL);
1673         checkGLcall("glEnable(GL_POLYGON_OFFSET_FILL)");
1674         glPolygonOffset(tmpvalue.f, *((float*)&stateblock->renderState[WINED3DRS_DEPTHBIAS]));
1675         checkGLcall("glPolygonOffset(...)");
1676     } else {
1677         glDisable(GL_POLYGON_OFFSET_FILL);
1678         checkGLcall("glDisable(GL_POLYGON_OFFSET_FILL)");
1679     }
1680 }
1681
1682 static void state_perspective(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1683     if (stateblock->renderState[WINED3DRS_TEXTUREPERSPECTIVE]) {
1684         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1685         checkGLcall("glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)");
1686     } else {
1687         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1688         checkGLcall("glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST)");
1689     }
1690 }
1691
1692 static void state_stippledalpha(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1693     TRACE("Stub\n");
1694     if (stateblock->renderState[WINED3DRS_STIPPLEDALPHA])
1695         FIXME(" Stippled Alpha not supported yet.\n");
1696 }
1697
1698 static void state_antialias(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1699     TRACE("Stub\n");
1700     if (stateblock->renderState[WINED3DRS_ANTIALIAS])
1701         FIXME(" Antialias not supported yet.\n");
1702 }
1703
1704 static void state_multisampmask(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1705     TRACE("Stub\n");
1706     if (stateblock->renderState[WINED3DRS_MULTISAMPLEMASK] != 0xFFFFFFFF)
1707         FIXME("(WINED3DRS_MULTISAMPLEMASK,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_MULTISAMPLEMASK]);
1708 }
1709
1710 static void state_patchedgestyle(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1711     TRACE("Stub\n");
1712     if (stateblock->renderState[WINED3DRS_PATCHEDGESTYLE] != WINED3DPATCHEDGE_DISCRETE)
1713         FIXME("(WINED3DRS_PATCHEDGESTYLE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_PATCHEDGESTYLE]);
1714 }
1715
1716 static void state_patchsegments(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1717     union {
1718         DWORD d;
1719         float f;
1720     } tmpvalue;
1721     tmpvalue.f = 1.0f;
1722
1723     TRACE("Stub\n");
1724     if (stateblock->renderState[WINED3DRS_PATCHSEGMENTS] != tmpvalue.d)
1725     {
1726         static BOOL displayed = FALSE;
1727
1728         tmpvalue.d = stateblock->renderState[WINED3DRS_PATCHSEGMENTS];
1729         if(!displayed)
1730             FIXME("(WINED3DRS_PATCHSEGMENTS,%f) not yet implemented\n", tmpvalue.f);
1731
1732         displayed = TRUE;
1733     }
1734 }
1735
1736 static void state_positiondegree(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1737     TRACE("Stub\n");
1738     if (stateblock->renderState[WINED3DRS_POSITIONDEGREE] != WINED3DDEGREE_CUBIC)
1739         FIXME("(WINED3DRS_POSITIONDEGREE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_POSITIONDEGREE]);
1740 }
1741
1742 static void state_normaldegree(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1743     TRACE("Stub\n");
1744     if (stateblock->renderState[WINED3DRS_NORMALDEGREE] != WINED3DDEGREE_LINEAR)
1745         FIXME("(WINED3DRS_NORMALDEGREE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_NORMALDEGREE]);
1746 }
1747
1748 static void state_tessellation(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1749     TRACE("Stub\n");
1750     if(stateblock->renderState[WINED3DRS_ENABLEADAPTIVETESSELLATION])
1751         FIXME("(WINED3DRS_ENABLEADAPTIVETESSELLATION,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_ENABLEADAPTIVETESSELLATION]);
1752 }
1753
1754 static void state_wrapu(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1755     if(stateblock->renderState[WINED3DRS_WRAPU]) {
1756         FIXME("Render state WINED3DRS_WRAPU not implemented yet\n");
1757     }
1758 }
1759
1760 static void state_wrapv(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1761     if(stateblock->renderState[WINED3DRS_WRAPV]) {
1762         FIXME("Render state WINED3DRS_WRAPV not implemented yet\n");
1763     }
1764 }
1765
1766 static void state_monoenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1767     if(stateblock->renderState[WINED3DRS_MONOENABLE]) {
1768         FIXME("Render state WINED3DRS_MONOENABLE not implemented yet\n");
1769     }
1770 }
1771
1772 static void state_rop2(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1773     if(stateblock->renderState[WINED3DRS_ROP2]) {
1774         FIXME("Render state WINED3DRS_ROP2 not implemented yet\n");
1775     }
1776 }
1777
1778 static void state_planemask(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1779     if(stateblock->renderState[WINED3DRS_PLANEMASK]) {
1780         FIXME("Render state WINED3DRS_PLANEMASK not implemented yet\n");
1781     }
1782 }
1783
1784 static void state_subpixel(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1785     if(stateblock->renderState[WINED3DRS_SUBPIXEL]) {
1786         FIXME("Render state WINED3DRS_SUBPIXEL not implemented yet\n");
1787     }
1788 }
1789
1790 static void state_subpixelx(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1791     if(stateblock->renderState[WINED3DRS_SUBPIXELX]) {
1792         FIXME("Render state WINED3DRS_SUBPIXELX not implemented yet\n");
1793     }
1794 }
1795
1796 static void state_stippleenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1797     if(stateblock->renderState[WINED3DRS_STIPPLEENABLE]) {
1798         FIXME("Render state WINED3DRS_STIPPLEENABLE not implemented yet\n");
1799     }
1800 }
1801
1802 static void state_bordercolor(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1803     if(stateblock->renderState[WINED3DRS_BORDERCOLOR]) {
1804         FIXME("Render state WINED3DRS_BORDERCOLOR not implemented yet\n");
1805     }
1806 }
1807
1808 static void state_mipmaplodbias(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1809     if(stateblock->renderState[WINED3DRS_MIPMAPLODBIAS]) {
1810         FIXME("Render state WINED3DRS_MIPMAPLODBIAS not implemented yet\n");
1811     }
1812 }
1813
1814 static void state_anisotropy(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1815     if(stateblock->renderState[WINED3DRS_ANISOTROPY]) {
1816         FIXME("Render state WINED3DRS_ANISOTROPY not implemented yet\n");
1817     }
1818 }
1819
1820 static void state_flushbatch(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1821     if(stateblock->renderState[WINED3DRS_FLUSHBATCH]) {
1822         FIXME("Render state WINED3DRS_FLUSHBATCH not implemented yet\n");
1823     }
1824 }
1825
1826 static void state_translucentsi(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1827     if(stateblock->renderState[WINED3DRS_TRANSLUCENTSORTINDEPENDENT]) {
1828         FIXME("Render state WINED3DRS_TRANSLUCENTSORTINDEPENDENT not implemented yet\n");
1829     }
1830 }
1831
1832 static void state_extents(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1833     if(stateblock->renderState[WINED3DRS_EXTENTS]) {
1834         FIXME("Render state WINED3DRS_EXTENTS not implemented yet\n");
1835     }
1836 }
1837
1838 static void state_ckeyblend(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
1839     if(stateblock->renderState[WINED3DRS_COLORKEYBLENDENABLE]) {
1840         FIXME("Render state WINED3DRS_COLORKEYBLENDENABLE not implemented yet\n");
1841     }
1842 }
1843
1844 /* Set texture operations up - The following avoids lots of ifdefs in this routine!*/
1845 #if defined (GL_VERSION_1_3)
1846 # define useext(A) A
1847 #elif defined (GL_EXT_texture_env_combine)
1848 # define useext(A) A##_EXT
1849 #elif defined (GL_ARB_texture_env_combine)
1850 # define useext(A) A##_ARB
1851 #endif
1852
1853 static void get_src_and_opr(DWORD arg, BOOL is_alpha, GLenum* source, GLenum* operand) {
1854     /* The WINED3DTA_ALPHAREPLICATE flag specifies the alpha component of the
1855     * input should be used for all input components. The WINED3DTA_COMPLEMENT
1856     * flag specifies the complement of the input should be used. */
1857     BOOL from_alpha = is_alpha || arg & WINED3DTA_ALPHAREPLICATE;
1858     BOOL complement = arg & WINED3DTA_COMPLEMENT;
1859
1860     /* Calculate the operand */
1861     if (complement) {
1862         if (from_alpha) *operand = GL_ONE_MINUS_SRC_ALPHA;
1863         else *operand = GL_ONE_MINUS_SRC_COLOR;
1864     } else {
1865         if (from_alpha) *operand = GL_SRC_ALPHA;
1866         else *operand = GL_SRC_COLOR;
1867     }
1868
1869     /* Calculate the source */
1870     switch (arg & WINED3DTA_SELECTMASK) {
1871         case WINED3DTA_CURRENT: *source = GL_PREVIOUS_EXT; break;
1872         case WINED3DTA_DIFFUSE: *source = GL_PRIMARY_COLOR_EXT; break;
1873         case WINED3DTA_TEXTURE: *source = GL_TEXTURE; break;
1874         case WINED3DTA_TFACTOR: *source = GL_CONSTANT_EXT; break;
1875         case WINED3DTA_SPECULAR:
1876             /*
1877             * According to the GL_ARB_texture_env_combine specs, SPECULAR is
1878             * 'Secondary color' and isn't supported until base GL supports it
1879             * There is no concept of temp registers as far as I can tell
1880             */
1881             FIXME("Unhandled texture arg WINED3DTA_SPECULAR\n");
1882             *source = GL_TEXTURE;
1883             break;
1884         default:
1885             FIXME("Unrecognized texture arg %#x\n", arg);
1886             *source = GL_TEXTURE;
1887             break;
1888     }
1889 }
1890
1891 /* Setup the texture operations texture stage states */
1892 static void set_tex_op(IWineD3DDevice *iface, BOOL isAlpha, int Stage, WINED3DTEXTUREOP op, DWORD arg1, DWORD arg2, DWORD arg3)
1893 {
1894     GLenum src1, src2, src3;
1895     GLenum opr1, opr2, opr3;
1896     GLenum comb_target;
1897     GLenum src0_target, src1_target, src2_target;
1898     GLenum opr0_target, opr1_target, opr2_target;
1899     GLenum scal_target;
1900     GLenum opr=0, invopr, src3_target, opr3_target;
1901     BOOL Handled = FALSE;
1902     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1903     IWineD3DStateBlockImpl *stateblock = This->stateBlock; /* for GLINFO_LOCATION */
1904
1905     TRACE("Alpha?(%d), Stage:%d Op(%s), a1(%d), a2(%d), a3(%d)\n", isAlpha, Stage, debug_d3dtop(op), arg1, arg2, arg3);
1906
1907     /* This is called by a state handler which has the gl lock held and a context for the thread */
1908
1909         /* Note: Operations usually involve two ars, src0 and src1 and are operations of
1910     the form (a1 <operation> a2). However, some of the more complex operations
1911     take 3 parameters. Instead of the (sensible) addition of a3, Microsoft added
1912     in a third parameter called a0. Therefore these are operations of the form
1913     a0 <operation> a1 <operation> a2, i.e., the new parameter goes to the front.
1914
1915     However, below we treat the new (a0) parameter as src2/opr2, so in the actual
1916     functions below, expect their syntax to differ slightly to those listed in the
1917     manuals, i.e., replace arg1 with arg3, arg2 with arg1 and arg3 with arg2
1918     This affects WINED3DTOP_MULTIPLYADD and WINED3DTOP_LERP                     */
1919
1920     if (isAlpha) {
1921         comb_target = useext(GL_COMBINE_ALPHA);
1922         src0_target = useext(GL_SOURCE0_ALPHA);
1923         src1_target = useext(GL_SOURCE1_ALPHA);
1924         src2_target = useext(GL_SOURCE2_ALPHA);
1925         opr0_target = useext(GL_OPERAND0_ALPHA);
1926         opr1_target = useext(GL_OPERAND1_ALPHA);
1927         opr2_target = useext(GL_OPERAND2_ALPHA);
1928         scal_target = GL_ALPHA_SCALE;
1929     }
1930     else {
1931         comb_target = useext(GL_COMBINE_RGB);
1932         src0_target = useext(GL_SOURCE0_RGB);
1933         src1_target = useext(GL_SOURCE1_RGB);
1934         src2_target = useext(GL_SOURCE2_RGB);
1935         opr0_target = useext(GL_OPERAND0_RGB);
1936         opr1_target = useext(GL_OPERAND1_RGB);
1937         opr2_target = useext(GL_OPERAND2_RGB);
1938         scal_target = useext(GL_RGB_SCALE);
1939     }
1940
1941         /* If a texture stage references an invalid texture unit the stage just
1942         * passes through the result from the previous stage */
1943     if (is_invalid_op(This, Stage, op, arg1, arg2, arg3)) {
1944         arg1 = WINED3DTA_CURRENT;
1945         op = WINED3DTOP_SELECTARG1;
1946     }
1947
1948         /* From MSDN (WINED3DTSS_ALPHAARG1) :
1949     The default argument is WINED3DTA_TEXTURE. If no texture is set for this stage,
1950     then the default argument is WINED3DTA_DIFFUSE.
1951     FIXME? If texture added/removed, may need to reset back as well?    */
1952     if (isAlpha && This->stateBlock->textures[Stage] == NULL && arg1 == WINED3DTA_TEXTURE) {
1953         get_src_and_opr(WINED3DTA_DIFFUSE, isAlpha, &src1, &opr1);
1954     } else {
1955         get_src_and_opr(arg1, isAlpha, &src1, &opr1);
1956     }
1957     get_src_and_opr(arg2, isAlpha, &src2, &opr2);
1958     get_src_and_opr(arg3, isAlpha, &src3, &opr3);
1959
1960     TRACE("ct(%x), 1:(%x,%x), 2:(%x,%x), 3:(%x,%x)\n", comb_target, src1, opr1, src2, opr2, src3, opr3);
1961
1962     Handled = TRUE; /* Assume will be handled */
1963
1964     /* Other texture operations require special extensions: */
1965     if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1966         if (isAlpha) {
1967             opr = GL_SRC_ALPHA;
1968             invopr = GL_ONE_MINUS_SRC_ALPHA;
1969             src3_target = GL_SOURCE3_ALPHA_NV;
1970             opr3_target = GL_OPERAND3_ALPHA_NV;
1971         } else {
1972             opr = GL_SRC_COLOR;
1973             invopr = GL_ONE_MINUS_SRC_COLOR;
1974             src3_target = GL_SOURCE3_RGB_NV;
1975             opr3_target = GL_OPERAND3_RGB_NV;
1976         }
1977         switch (op) {
1978             case WINED3DTOP_DISABLE: /* Only for alpha */
1979                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
1980                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
1981                 glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
1982                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
1983                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
1984                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
1985                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
1986                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
1987                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
1988                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
1989                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
1990                 checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
1991                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
1992                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
1993                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
1994                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
1995                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
1996                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
1997                 break;
1998                 case WINED3DTOP_SELECTARG1:                                          /* = a1 * 1 + 0 * 0 */
1999                 case WINED3DTOP_SELECTARG2:                                          /* = a2 * 1 + 0 * 0 */
2000                     glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2001                     checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2002                     if (op == WINED3DTOP_SELECTARG1) {
2003                         glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2004                         checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2005                         glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2006                         checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2007                     } else {
2008                         glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
2009                         checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
2010                         glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
2011                         checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
2012                     }
2013                     glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2014                     checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2015                     glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2016                     checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2017                     glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2018                     checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
2019                     glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2020                     checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
2021                     glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2022                     checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2023                     glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2024                     checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2025                     break;
2026
2027             case WINED3DTOP_MODULATE:
2028                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2029                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
2030                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2031                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2032                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2033                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2034                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2035                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2036                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2037                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2038                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2039                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2040                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2041                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2042                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2043                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2044                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2045                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
2046                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2047                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2048                 break;
2049             case WINED3DTOP_MODULATE2X:
2050                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2051                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
2052                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2053                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2054                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2055                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2056                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2057                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2058                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2059                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2060                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2061                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2062                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2063                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2064                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2065                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2066                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2067                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
2068                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2069                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
2070                 break;
2071             case WINED3DTOP_MODULATE4X:
2072                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2073                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
2074                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2075                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2076                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2077                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2078                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2079                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2080                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2081                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2082                 glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2083                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2084                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2085                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2086                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2087                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2088                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2089                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
2090                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
2091                 checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
2092                 break;
2093
2094             case WINED3DTOP_ADD:
2095                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2096                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2097                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2098                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2099                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2100                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2101                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2102                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2103                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2104                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2105                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2106                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2107                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2108                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2109                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2110                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2111                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2112                 checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2113                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2114                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2115                 break;
2116
2117             case WINED3DTOP_ADDSIGNED:
2118                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
2119                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
2120                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2121                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2122                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2123                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2124                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2125                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2126                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2127                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2128                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2129                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2130                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2131                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2132                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2133                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2134                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2135                 checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2136                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2137                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2138                 break;
2139
2140             case WINED3DTOP_ADDSIGNED2X:
2141                 glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
2142                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
2143                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2144                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2145                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2146                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2147                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2148                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2149                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2150                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2151                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2152                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2153                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2154                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2155                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2156                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2157                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2158                 checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2159                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2160                 checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
2161                 break;
2162
2163             case WINED3DTOP_ADDSMOOTH:
2164                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2165                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2166                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2167                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2168                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2169                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2170                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2171                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2172                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2173                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2174                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2175                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2176                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2177                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2178                 glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
2179                 checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
2180                 switch (opr1) {
2181                     case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2182                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2183                     case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2184                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2185                 }
2186                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2187                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2188                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2189                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2190                 break;
2191
2192             case WINED3DTOP_BLENDDIFFUSEALPHA:
2193                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2194                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2195                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2196                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2197                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2198                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2199                 glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR));
2200                 checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_PRIMARY_COLOR)");
2201                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2202                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2203                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2204                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2205                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2206                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2207                 glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR));
2208                 checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_PRIMARY_COLOR)");
2209                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2210                 checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2211                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2212                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2213                 break;
2214             case WINED3DTOP_BLENDTEXTUREALPHA:
2215                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2216                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2217                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2218                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2219                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2220                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2221                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_TEXTURE);
2222                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_TEXTURE");
2223                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2224                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2225                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2226                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2227                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2228                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2229                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
2230                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
2231                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2232                 checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2233                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2234                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2235                 break;
2236             case WINED3DTOP_BLENDFACTORALPHA:
2237                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2238                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2239                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2240                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2241                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2242                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2243                 glTexEnvi(GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT));
2244                 checkGLcall("GL_TEXTURE_ENV, src1_target, useext(GL_CONSTANT)");
2245                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2246                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2247                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2248                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2249                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2250                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2251                 glTexEnvi(GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT));
2252                 checkGLcall("GL_TEXTURE_ENV, src3_target, useext(GL_CONSTANT)");
2253                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2254                 checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2255                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2256                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2257                 break;
2258             case WINED3DTOP_BLENDTEXTUREALPHAPM:
2259                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2260                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2261                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2262                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2263                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2264                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2265                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2266                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2267                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2268                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2269                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2270                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2271                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2272                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2273                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
2274                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
2275                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2276                 checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2277                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2278                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2279                 break;
2280             case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2281                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2282                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");  /* Add = a0*a1 + a2*a3 */
2283                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);        /*   a0 = src1/opr1    */
2284                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2285                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2286                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");    /*   a1 = 1 (see docs) */
2287                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2288                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2289                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2290                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2291                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);        /*   a2 = arg2         */
2292                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2293                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2294                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");     /*  a3 = src1 alpha   */
2295                 glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
2296                 checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
2297                 switch (opr) {
2298                     case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2299                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2300                 }
2301                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2302                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2303                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2304                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2305                 break;
2306             case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2307                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2308                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2309                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2310                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2311                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2312                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2313                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2314                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2315                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2316                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2317                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
2318                 checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
2319                 switch (opr1) {
2320                     case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2321                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2322                 }
2323                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2324                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
2325                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2326                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2327                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2328                 checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2329                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2330                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2331                 break;
2332             case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2333                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2334                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2335                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2336                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2337                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2338                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2339                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2340                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2341                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2342                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2343                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2344                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2345                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2346                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2347                 glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
2348                 checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
2349                 switch (opr1) {
2350                     case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2351                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2352                     case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2353                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2354                 }
2355                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2356                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2357                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2358                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2359                 break;
2360             case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2361                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2362                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2363                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2364                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2365                 switch (opr1) {
2366                     case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2367                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2368                     case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2369                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2370                 }
2371                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2372                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2373                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2374                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2375                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2376                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2377                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
2378                 checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
2379                 switch (opr1) {
2380                     case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2381                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2382                 }
2383                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2384                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
2385                 glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2386                 checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2387                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2388                 checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2389                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2390                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2391                 break;
2392             case WINED3DTOP_MULTIPLYADD:
2393                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2394                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2395                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
2396                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2397                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
2398                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2399                 glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2400                 checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2401                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2402                 checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2403                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
2404                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2405                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr1);
2406                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2407                 glTexEnvi(GL_TEXTURE_ENV, src3_target, src2);
2408                 checkGLcall("GL_TEXTURE_ENV, src3_target, src3");
2409                 glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr2);
2410                 checkGLcall("GL_TEXTURE_ENV, opr3_target, opr3");
2411                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2412                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2413                 break;
2414
2415             case WINED3DTOP_BUMPENVMAP:
2416             {
2417             }
2418
2419             case WINED3DTOP_BUMPENVMAPLUMINANCE:
2420                 FIXME("Implement bump environment mapping in GL_NV_texture_env_combine4 path\n");
2421
2422             default:
2423                 Handled = FALSE;
2424         }
2425         if (Handled) {
2426             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
2427             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV");
2428
2429             return;
2430         }
2431     } /* GL_NV_texture_env_combine4 */
2432
2433     Handled = TRUE; /* Again, assume handled */
2434     switch (op) {
2435         case WINED3DTOP_DISABLE: /* Only for alpha */
2436             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
2437             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2438             glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
2439             checkGLcall("GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT");
2440             glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
2441             checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA");
2442             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2443             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2444             break;
2445         case WINED3DTOP_SELECTARG1:
2446             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
2447             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2448             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2449             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2450             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2451             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2452             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2453             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2454             break;
2455         case WINED3DTOP_SELECTARG2:
2456             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
2457             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2458             glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
2459             checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
2460             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
2461             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
2462             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2463             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2464             break;
2465         case WINED3DTOP_MODULATE:
2466             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
2467             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
2468             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2469             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2470             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2471             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2472             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2473             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2474             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2475             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2476             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2477             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2478             break;
2479         case WINED3DTOP_MODULATE2X:
2480             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
2481             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
2482             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2483             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2484             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2485             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2486             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2487             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2488             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2489             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2490             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2491             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
2492             break;
2493         case WINED3DTOP_MODULATE4X:
2494             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
2495             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
2496             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2497             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2498             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2499             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2500             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2501             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2502             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2503             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2504             glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
2505             checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
2506             break;
2507         case WINED3DTOP_ADD:
2508             glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2509             checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2510             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2511             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2512             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2513             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2514             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2515             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2516             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2517             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2518             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2519             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2520             break;
2521         case WINED3DTOP_ADDSIGNED:
2522             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
2523             checkGLcall("GL_TEXTURE_ENV, comb_target, useext((GL_ADD_SIGNED)");
2524             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2525             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2526             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2527             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2528             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2529             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2530             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2531             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2532             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2533             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2534             break;
2535         case WINED3DTOP_ADDSIGNED2X:
2536             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED));
2537             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_ADD_SIGNED)");
2538             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2539             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2540             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2541             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2542             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2543             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2544             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2545             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2546             glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2547             checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
2548             break;
2549         case WINED3DTOP_SUBTRACT:
2550             if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE)) {
2551                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_SUBTRACT);
2552                 checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_SUBTRACT)");
2553                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2554                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2555                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2556                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2557                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2558                 checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2559                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2560                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2561                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2562                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2563             } else {
2564                 FIXME("This version of opengl does not support GL_SUBTRACT\n");
2565             }
2566             break;
2567
2568         case WINED3DTOP_BLENDDIFFUSEALPHA:
2569             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
2570             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
2571             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2572             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2573             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2574             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2575             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2576             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2577             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2578             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2579             glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PRIMARY_COLOR));
2580             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PRIMARY_COLOR");
2581             glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2582             checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2583             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2584             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2585             break;
2586         case WINED3DTOP_BLENDTEXTUREALPHA:
2587             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
2588             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
2589             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2590             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2591             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2592             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2593             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2594             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2595             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2596             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2597             glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_TEXTURE);
2598             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_TEXTURE");
2599             glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2600             checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2601             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2602             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2603             break;
2604         case WINED3DTOP_BLENDFACTORALPHA:
2605             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
2606             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
2607             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2608             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2609             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2610             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2611             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2612             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2613             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2614             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2615             glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_CONSTANT));
2616             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_CONSTANT");
2617             glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2618             checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2619             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2620             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2621             break;
2622         case WINED3DTOP_BLENDCURRENTALPHA:
2623             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
2624             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
2625             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2626             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2627             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2628             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2629             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2630             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2631             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2632             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2633             glTexEnvi(GL_TEXTURE_ENV, src2_target, useext(GL_PREVIOUS));
2634             checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PREVIOUS");
2635             glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2636             checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2637             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2638             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2639             break;
2640         case WINED3DTOP_DOTPRODUCT3:
2641             if (GL_SUPPORT(ARB_TEXTURE_ENV_DOT3)) {
2642                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB);
2643                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB");
2644             } else if (GL_SUPPORT(EXT_TEXTURE_ENV_DOT3)) {
2645                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT);
2646                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT");
2647             } else {
2648                 FIXME("This version of opengl does not support GL_DOT3\n");
2649             }
2650             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2651             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2652             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2653             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2654             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2655             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2656             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2657             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2658             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2659             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2660             break;
2661         case WINED3DTOP_LERP:
2662             glTexEnvi(GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE));
2663             checkGLcall("GL_TEXTURE_ENV, comb_target, useext(GL_INTERPOLATE)");
2664             glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2665             checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2666             glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2667             checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2668             glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2669             checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2670             glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2671             checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2672             glTexEnvi(GL_TEXTURE_ENV, src2_target, src3);
2673             checkGLcall("GL_TEXTURE_ENV, src2_target, src3");
2674             glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr3);
2675             checkGLcall("GL_TEXTURE_ENV, opr2_target, opr3");
2676             glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2677             checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2678             break;
2679         case WINED3DTOP_ADDSMOOTH:
2680             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2681                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2682                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2683                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2684                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2685                 switch (opr1) {
2686                     case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2687                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2688                     case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2689                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2690                 }
2691                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2692                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2693                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2694                 checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2695                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2696                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2697                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2698                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2699                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2700                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2701                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2702                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2703             } else
2704                 Handled = FALSE;
2705                 break;
2706         case WINED3DTOP_BLENDTEXTUREALPHAPM:
2707             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2708                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2709                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2710                 glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_TEXTURE);
2711                 checkGLcall("GL_TEXTURE_ENV, src0_target, GL_TEXTURE");
2712                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_ALPHA);
2713                 checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_APHA");
2714                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2715                 checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2716                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2717                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2718                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2719                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2720                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2721                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2722                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2723                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2724             } else
2725                 Handled = FALSE;
2726                 break;
2727         case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2728             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2729                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2730                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2731                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2732                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2733                 switch (opr1) {
2734                     case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2735                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2736                     case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2737                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2738                 }
2739                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2740                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2741                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2742                 checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2743                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2744                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2745                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2746                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2747                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2748                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2749                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2750                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2751             } else
2752                 Handled = FALSE;
2753                 break;
2754         case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2755             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2756                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2757                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2758                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2759                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2760                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2761                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2762                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2763                 checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2764                 switch (opr1) {
2765                     case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2766                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2767                     case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2768                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2769                 }
2770                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2771                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2772                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2773                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2774                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2775                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2776                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2777                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2778             } else
2779                 Handled = FALSE;
2780                 break;
2781         case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2782             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2783                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2784                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2785                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2786                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2787                 switch (opr1) {
2788                     case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2789                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2790                     case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2791                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2792                 }
2793                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2794                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2795                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2796                 checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2797                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2798                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2799                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2800                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2801                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2802                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2803                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2804                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2805             } else
2806                 Handled = FALSE;
2807                 break;
2808         case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2809             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2810                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2811                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2812                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2813                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2814                 switch (opr1) {
2815                     case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
2816                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
2817                     case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2818                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2819                 }
2820                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2821                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2822                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2823                 checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2824                 switch (opr1) {
2825                     case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
2826                     case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2827                     case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
2828                     case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
2829                 }
2830                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2831                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2832                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2833                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2834                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2835                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2836                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2837                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2838             } else
2839                 Handled = FALSE;
2840                 break;
2841         case WINED3DTOP_MULTIPLYADD:
2842             if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3)) {
2843                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2844                 checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2845                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2846                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2847                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2848                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2849                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src3);
2850                 checkGLcall("GL_TEXTURE_ENV, src1_target, src3");
2851                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr3);
2852                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr3");
2853                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2854                 checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2855                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2856                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2857                 glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2858                 checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
2859             } else
2860                 Handled = FALSE;
2861                 break;
2862         case WINED3DTOP_BUMPENVMAPLUMINANCE:
2863             if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
2864                     /* Some apps use BUMPENVMAPLUMINANCE instead of D3DTOP_BUMPENVMAP, although
2865                 * they check for the non-luminance cap flag. Well, give them what they asked
2866                 * for :-)
2867                     */
2868                 WARN("Application uses WINED3DTOP_BUMPENVMAPLUMINANCE\n");
2869             } else {
2870                 Handled = FALSE;
2871                 break;
2872             }
2873             /* Fall through */
2874         case WINED3DTOP_BUMPENVMAP:
2875             if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
2876                 TRACE("Using ati bumpmap on stage %d, target %d\n", Stage, Stage + 1);
2877                 glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_BUMP_ENVMAP_ATI);
2878                 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_BUMP_ENVMAP_ATI)");
2879                 glTexEnvi(GL_TEXTURE_ENV, GL_BUMP_TARGET_ATI, GL_TEXTURE0_ARB + Stage + 1);
2880                 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_BUMP_TARGET_ATI, GL_TEXTURE0_ARB + Stage + 1)");
2881                 glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
2882                 checkGLcall("GL_TEXTURE_ENV, src0_target, src3");
2883                 glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
2884                 checkGLcall("GL_TEXTURE_ENV, opr0_target, opr3");
2885                 glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2886                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2887                 glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2888                 checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2889                 glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2890                 checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2891                 glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2892                 checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2893
2894                 Handled = TRUE;
2895                 break;
2896             } else if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
2897                 /* Technically texture shader support without register combiners is possible, but not expected to occur
2898                  * on real world cards, so for now a fixme should be enough
2899                  */
2900                 FIXME("Implement bump mapping with GL_NV_texture_shader in non register combiner path\n");
2901             }
2902         default:
2903             Handled = FALSE;
2904     }
2905
2906     if (Handled) {
2907         BOOL  combineOK = TRUE;
2908         if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
2909             DWORD op2;
2910
2911             if (isAlpha) {
2912                 op2 = This->stateBlock->textureState[Stage][WINED3DTSS_COLOROP];
2913             } else {
2914                 op2 = This->stateBlock->textureState[Stage][WINED3DTSS_ALPHAOP];
2915             }
2916
2917             /* Note: If COMBINE4 in effect can't go back to combine! */
2918             switch (op2) {
2919                 case WINED3DTOP_ADDSMOOTH:
2920                 case WINED3DTOP_BLENDTEXTUREALPHAPM:
2921                 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2922                 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2923                 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2924                 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2925                 case WINED3DTOP_MULTIPLYADD:
2926                     /* Ignore those implemented in both cases */
2927                     switch (op) {
2928                         case WINED3DTOP_SELECTARG1:
2929                         case WINED3DTOP_SELECTARG2:
2930                             combineOK = FALSE;
2931                             Handled   = FALSE;
2932                             break;
2933                         default:
2934                             FIXME("Can't use COMBINE4 and COMBINE together, thisop=%s, otherop=%s, isAlpha(%d)\n", debug_d3dtop(op), debug_d3dtop(op2), isAlpha);
2935                             return;
2936                     }
2937             }
2938         }
2939
2940         if (combineOK) {
2941             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE));
2942             checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, useext(GL_COMBINE)");
2943
2944             return;
2945         }
2946     }
2947
2948     /* After all the extensions, if still unhandled, report fixme */
2949     FIXME("Unhandled texture operation %s\n", debug_d3dtop(op));
2950 }
2951
2952
2953 static void tex_colorop(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2954     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
2955     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[stage];
2956     BOOL tex_used = stateblock->wineD3DDevice->fixed_function_usage_map[stage];
2957
2958     TRACE("Setting color op for stage %d\n", stage);
2959
2960     if (stateblock->pixelShader && stateblock->wineD3DDevice->ps_selected_mode != SHADER_NONE &&
2961         ((IWineD3DPixelShaderImpl *)stateblock->pixelShader)->baseShader.function) {
2962         /* Using a pixel shader? Don't care for anything here, the shader applying does it */
2963         return;
2964     }
2965
2966     if (stage != mapped_stage) WARN("Using non 1:1 mapping: %d -> %d!\n", stage, mapped_stage);
2967
2968     if (mapped_stage != -1) {
2969         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
2970             if (tex_used && mapped_stage >= GL_LIMITS(textures)) {
2971                 FIXME("Attempt to enable unsupported stage!\n");
2972                 return;
2973             }
2974             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
2975             checkGLcall("glActiveTextureARB");
2976         } else if (stage > 0) {
2977             WARN("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
2978             return;
2979         }
2980     }
2981
2982     if(stage >= stateblock->lowest_disabled_stage) {
2983         TRACE("Stage disabled\n");
2984         if (mapped_stage != -1) {
2985             /* Disable everything here */
2986             glDisable(GL_TEXTURE_2D);
2987             checkGLcall("glDisable(GL_TEXTURE_2D)");
2988             glDisable(GL_TEXTURE_3D);
2989             checkGLcall("glDisable(GL_TEXTURE_3D)");
2990             if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
2991                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2992                 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
2993             }
2994             if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
2995                 glDisable(GL_TEXTURE_RECTANGLE_ARB);
2996                 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
2997             }
2998         }
2999         /* All done */
3000         return;
3001     }
3002
3003     /* The sampler will also activate the correct texture dimensions, so no need to do it here
3004      * if the sampler for this stage is dirty
3005      */
3006     if(!isStateDirty(context, STATE_SAMPLER(stage))) {
3007         if (tex_used) texture_activate_dimensions(stage, stateblock, context);
3008     }
3009
3010     set_tex_op((IWineD3DDevice *)stateblock->wineD3DDevice, FALSE, stage,
3011                 stateblock->textureState[stage][WINED3DTSS_COLOROP],
3012                 stateblock->textureState[stage][WINED3DTSS_COLORARG1],
3013                 stateblock->textureState[stage][WINED3DTSS_COLORARG2],
3014                 stateblock->textureState[stage][WINED3DTSS_COLORARG0]);
3015 }
3016
3017 void tex_alphaop(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3018     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
3019     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[stage];
3020     BOOL tex_used = stateblock->wineD3DDevice->fixed_function_usage_map[stage];
3021     DWORD op, arg1, arg2, arg0;
3022
3023     TRACE("Setting alpha op for stage %d\n", stage);
3024     /* Do not care for enabled / disabled stages, just assign the settings. colorop disables / enables required stuff */
3025     if (mapped_stage != -1) {
3026         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
3027             if (tex_used && mapped_stage >= GL_LIMITS(textures)) {
3028                 FIXME("Attempt to enable unsupported stage!\n");
3029                 return;
3030             }
3031             GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
3032             checkGLcall("glActiveTextureARB");
3033         } else if (stage > 0) {
3034             /* We can't do anything here */
3035             WARN("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
3036             return;
3037         }
3038     }
3039
3040     op = stateblock->textureState[stage][WINED3DTSS_ALPHAOP];
3041     arg1 = stateblock->textureState[stage][WINED3DTSS_ALPHAARG1];
3042     arg2 = stateblock->textureState[stage][WINED3DTSS_ALPHAARG2];
3043     arg0 = stateblock->textureState[stage][WINED3DTSS_ALPHAARG0];
3044
3045     if(stateblock->renderState[WINED3DRS_COLORKEYENABLE] && stage == 0 &&
3046        stateblock->textures[0] &&
3047        (stateblock->textureDimensions[0] == GL_TEXTURE_2D || stateblock->textureDimensions[0] == GL_TEXTURE_RECTANGLE_ARB)) {
3048         IWineD3DSurfaceImpl *surf = (IWineD3DSurfaceImpl *) ((IWineD3DTextureImpl *) stateblock->textures[0])->surfaces[0];
3049
3050         if(surf->CKeyFlags & WINEDDSD_CKSRCBLT &&
3051            getFormatDescEntry(surf->resource.format, NULL, NULL)->alphaMask == 0x00000000) {
3052
3053             /* Color keying needs to pass alpha values from the texture through to have the alpha test work properly.
3054              * On the other hand applications can still use texture combiners apparently. This code takes care that apps
3055              * cannot remove the texture's alpha channel entirely.
3056              *
3057              * The fixup is required for Prince of Persia 3D(prison bars), while Moto racer 2 requires D3DTOP_MODULATE to work
3058              * on color keyed surfaces. Aliens vs Predator 1 uses color keyed textures and alpha component of diffuse color to
3059              * draw things like translucent text and perform other blending effects.
3060              *
3061              * Aliens vs Predator 1 relies on diffuse alpha having an effect, so it cannot be ignored. To provide the
3062              * behavior expected by the game, while emulating the colorkey, diffuse alpha must be modulated with texture alpha.
3063              * OTOH, Moto racer 2 at some points sets alphaop/alphaarg to SELECTARG/CURRENT, yet puts garbage in diffuse alpha
3064              * (zeroes). This works on native, because the game disables alpha test and alpha blending. Alpha test is overwritten by
3065              * wine's for purposes of color-keying though, so this will lead to missing geometry if texture alpha is modulated
3066              * (pixels fail alpha test). To get around this, ALPHABLENDENABLE state is checked: if the app enables alpha blending,
3067              * it can be expected to provide meaningful values in diffuse alpha, so it should be modulated with texture alpha;
3068              * otherwise, selecting diffuse alpha is ignored in favour of texture alpha.
3069
3070              * What to do with multitexturing? So far no app has been found that uses color keying with multitexturing
3071              */
3072             if(op == WINED3DTOP_DISABLE) {
3073                 arg1 = WINED3DTA_TEXTURE;
3074                 op = WINED3DTOP_SELECTARG1;
3075             }
3076             else if(op == WINED3DTOP_SELECTARG1 && arg1 != WINED3DTA_TEXTURE) {
3077                 if (stateblock->renderState[WINED3DRS_ALPHABLENDENABLE]) {
3078                     arg2 = WINED3DTA_TEXTURE;
3079                     op = WINED3DTOP_MODULATE;
3080                 }
3081                 else arg1 = WINED3DTA_TEXTURE;
3082             }
3083             else if(op == WINED3DTOP_SELECTARG2 && arg2 != WINED3DTA_TEXTURE) {
3084                 if (stateblock->renderState[WINED3DRS_ALPHABLENDENABLE]) {
3085                     arg1 = WINED3DTA_TEXTURE;
3086                     op = WINED3DTOP_MODULATE;
3087                 }
3088                 else arg2 = WINED3DTA_TEXTURE;
3089             }
3090         }
3091     }
3092
3093     /* tex_alphaop is shared between the ffp and nvrc because the difference only comes down to
3094      * this if block here, and the other code(color keying, texture unit selection) are the same
3095      */
3096     TRACE("Setting alpha op for stage %d\n", stage);
3097     if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
3098         set_tex_op_nvrc((IWineD3DDevice *)stateblock->wineD3DDevice, TRUE, stage,
3099                          op, arg1, arg2, arg0,
3100                          mapped_stage,
3101                          stateblock->textureState[stage][WINED3DTSS_RESULTARG]);
3102     } else {
3103         set_tex_op((IWineD3DDevice *)stateblock->wineD3DDevice, TRUE, stage,
3104                     op, arg1, arg2, arg0);
3105     }
3106 }
3107
3108 static void transform_texture(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3109     DWORD texUnit = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
3110     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[texUnit];
3111     BOOL generated;
3112     int coordIdx;
3113
3114     /* Ignore this when a vertex shader is used, or if the streams aren't sorted out yet */
3115     if(use_vs(stateblock->wineD3DDevice) ||
3116        isStateDirty(context, STATE_VDECL)) {
3117         TRACE("Using a vertex shader, or stream sources not sorted out yet, skipping\n");
3118         return;
3119     }
3120
3121     if (mapped_stage == -1) return;
3122
3123     if (GL_SUPPORT(ARB_MULTITEXTURE)) {
3124         if(mapped_stage >= GL_LIMITS(textures)) {
3125             return;
3126         }
3127         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
3128         checkGLcall("glActiveTextureARB");
3129     } else if (mapped_stage > 0) {
3130         /* We can't do anything here */
3131         WARN("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
3132         return;
3133     }
3134     generated = (stateblock->textureState[texUnit][WINED3DTSS_TEXCOORDINDEX] & 0xFFFF0000) != WINED3DTSS_TCI_PASSTHRU;
3135     coordIdx = min(stateblock->textureState[texUnit][WINED3DTSS_TEXCOORDINDEX & 0x0000FFFF], MAX_TEXTURES - 1);
3136
3137     set_texture_matrix(&stateblock->transforms[WINED3DTS_TEXTURE0 + texUnit].u.m[0][0],
3138                         stateblock->textureState[texUnit][WINED3DTSS_TEXTURETRANSFORMFLAGS],
3139                         generated,
3140                         context->last_was_rhw,
3141                         stateblock->wineD3DDevice->strided_streams.u.s.texCoords[coordIdx].dwStride ?
3142                             stateblock->wineD3DDevice->strided_streams.u.s.texCoords[coordIdx].dwType:
3143                             WINED3DDECLTYPE_UNUSED,
3144                         stateblock->wineD3DDevice->frag_pipe->ffp_proj_control);
3145
3146     /* The sampler applying function calls us if this changes */
3147     if(context->lastWasPow2Texture[texUnit] && stateblock->textures[texUnit]) {
3148         if(generated) {
3149             FIXME("Non-power2 texture being used with generated texture coords\n");
3150         }
3151         TRACE("Non power two matrix multiply fixup\n");
3152         glMultMatrixf(((IWineD3DTextureImpl *) stateblock->textures[texUnit])->baseTexture.pow2Matrix);
3153     }
3154 }
3155
3156 static void unloadTexCoords(IWineD3DStateBlockImpl *stateblock) {
3157     int texture_idx;
3158
3159     for (texture_idx = 0; texture_idx < GL_LIMITS(texture_stages); ++texture_idx) {
3160         GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx));
3161         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3162     }
3163 }
3164
3165 static void loadTexCoords(IWineD3DStateBlockImpl *stateblock, WineDirect3DVertexStridedData *sd, GLint *curVBO) {
3166     UINT *offset = stateblock->streamOffset;
3167     unsigned int mapped_stage = 0;
3168     unsigned int textureNo = 0;
3169
3170     /* The code below uses glClientActiveTexture and glMultiTexCoord* which are all part of the GL_ARB_multitexture extension. */
3171     /* Abort if we don't support the extension. */
3172     if (!GL_SUPPORT(ARB_MULTITEXTURE)) {
3173         FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
3174         return;
3175     }
3176
3177     for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
3178         int coordIdx = stateblock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
3179
3180         mapped_stage = stateblock->wineD3DDevice->texUnitMap[textureNo];
3181         if (mapped_stage == -1) continue;
3182
3183         if (coordIdx < MAX_TEXTURES && (sd->u.s.texCoords[coordIdx].lpData || sd->u.s.texCoords[coordIdx].VBO)) {
3184             TRACE("Setting up texture %u, idx %d, cordindx %u, data %p\n",
3185                     textureNo, mapped_stage, coordIdx, sd->u.s.texCoords[coordIdx].lpData);
3186
3187             if (*curVBO != sd->u.s.texCoords[coordIdx].VBO) {
3188                 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, sd->u.s.texCoords[coordIdx].VBO));
3189                 checkGLcall("glBindBufferARB");
3190                 *curVBO = sd->u.s.texCoords[coordIdx].VBO;
3191             }
3192
3193             GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
3194             checkGLcall("glClientActiveTextureARB");
3195
3196             /* The coords to supply depend completely on the fvf / vertex shader */
3197             glTexCoordPointer(
3198                     WINED3D_ATR_SIZE(sd->u.s.texCoords[coordIdx].dwType),
3199                     WINED3D_ATR_GLTYPE(sd->u.s.texCoords[coordIdx].dwType),
3200                     sd->u.s.texCoords[coordIdx].dwStride,
3201                     sd->u.s.texCoords[coordIdx].lpData + stateblock->loadBaseVertexIndex * sd->u.s.texCoords[coordIdx].dwStride + offset[sd->u.s.texCoords[coordIdx].streamNo]);
3202             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3203         } else {
3204             GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
3205         }
3206     }
3207     if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
3208         /* The number of the mapped stages increases monotonically, so it's fine to use the last used one */
3209         for (textureNo = mapped_stage + 1; textureNo < GL_LIMITS(textures); ++textureNo) {
3210             GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + textureNo, 0, 0, 0, 1));
3211         }
3212     }
3213
3214     checkGLcall("loadTexCoords");
3215 }
3216
3217 static void tex_coordindex(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3218     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
3219     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[stage];
3220
3221     if (mapped_stage == -1) {
3222         TRACE("No texture unit mapped to stage %d. Skipping texture coordinates.\n", stage);
3223         return;
3224     }
3225
3226     if (GL_SUPPORT(ARB_MULTITEXTURE)) {
3227         if(mapped_stage >= GL_LIMITS(fragment_samplers)) {
3228             return;
3229         }
3230         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
3231         checkGLcall("glActiveTextureARB");
3232     } else if (stage > 0) {
3233         /* We can't do anything here */
3234         WARN("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
3235         return;
3236     }
3237
3238     /* Values 0-7 are indexes into the FVF tex coords - See comments in DrawPrimitive
3239      *
3240      * FIXME: From MSDN: The WINED3DTSS_TCI_* flags are mutually exclusive. If you include
3241      * one flag, you can still specify an index value, which the system uses to
3242      * determine the texture wrapping mode.
3243      * eg. SetTextureStageState( 0, WINED3DTSS_TEXCOORDINDEX, WINED3DTSS_TCI_CAMERASPACEPOSITION | 1 );
3244      * means use the vertex position (camera-space) as the input texture coordinates
3245      * for this texture stage, and the wrap mode set in the WINED3DRS_WRAP1 render
3246      * state. We do not (yet) support the WINED3DRENDERSTATE_WRAPx values, nor tie them up
3247      * to the TEXCOORDINDEX value
3248      */
3249
3250     /*
3251      * Be careful the value of the mask 0xF0000 come from d3d8types.h infos
3252      */
3253     switch (stateblock->textureState[stage][WINED3DTSS_TEXCOORDINDEX] & 0xFFFF0000) {
3254     case WINED3DTSS_TCI_PASSTHRU:
3255         /*Use the specified texture coordinates contained within the vertex format. This value resolves to zero.*/
3256         glDisable(GL_TEXTURE_GEN_S);
3257         glDisable(GL_TEXTURE_GEN_T);
3258         glDisable(GL_TEXTURE_GEN_R);
3259         glDisable(GL_TEXTURE_GEN_Q);
3260         checkGLcall("glDisable(GL_TEXTURE_GEN_S,T,R,Q)");
3261         break;
3262
3263     case WINED3DTSS_TCI_CAMERASPACEPOSITION:
3264         /* CameraSpacePosition means use the vertex position, transformed to camera space,
3265          * as the input texture coordinates for this stage's texture transformation. This
3266          * equates roughly to EYE_LINEAR
3267          */
3268         {
3269             float s_plane[] = { 1.0, 0.0, 0.0, 0.0 };
3270             float t_plane[] = { 0.0, 1.0, 0.0, 0.0 };
3271             float r_plane[] = { 0.0, 0.0, 1.0, 0.0 };
3272             float q_plane[] = { 0.0, 0.0, 0.0, 1.0 };
3273             TRACE("WINED3DTSS_TCI_CAMERASPACEPOSITION - Set eye plane\n");
3274
3275             glMatrixMode(GL_MODELVIEW);
3276             glPushMatrix();
3277             glLoadIdentity();
3278             glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
3279             glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
3280             glTexGenfv(GL_R, GL_EYE_PLANE, r_plane);
3281             glTexGenfv(GL_Q, GL_EYE_PLANE, q_plane);
3282             glPopMatrix();
3283
3284             TRACE("WINED3DTSS_TCI_CAMERASPACEPOSITION - Set GL_TEXTURE_GEN_x and GL_x, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR\n");
3285             glEnable(GL_TEXTURE_GEN_S);
3286             checkGLcall("glEnable(GL_TEXTURE_GEN_S)");
3287             glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
3288             checkGLcall("glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)");
3289             glEnable(GL_TEXTURE_GEN_T);
3290             checkGLcall("glEnable(GL_TEXTURE_GEN_T)");
3291             glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
3292             checkGLcall("glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)");
3293             glEnable(GL_TEXTURE_GEN_R);
3294             checkGLcall("glEnable(GL_TEXTURE_GEN_R)");
3295             glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
3296             checkGLcall("glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)");
3297         }
3298         break;
3299
3300     case WINED3DTSS_TCI_CAMERASPACENORMAL:
3301         {
3302             if (GL_SUPPORT(NV_TEXGEN_REFLECTION)) {
3303                 float s_plane[] = { 1.0, 0.0, 0.0, 0.0 };
3304                 float t_plane[] = { 0.0, 1.0, 0.0, 0.0 };
3305                 float r_plane[] = { 0.0, 0.0, 1.0, 0.0 };
3306                 float q_plane[] = { 0.0, 0.0, 0.0, 1.0 };
3307                 TRACE("WINED3DTSS_TCI_CAMERASPACENORMAL - Set eye plane\n");
3308
3309                 glMatrixMode(GL_MODELVIEW);
3310                 glPushMatrix();
3311                 glLoadIdentity();
3312                 glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
3313                 glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
3314                 glTexGenfv(GL_R, GL_EYE_PLANE, r_plane);
3315                 glTexGenfv(GL_Q, GL_EYE_PLANE, q_plane);
3316                 glPopMatrix();
3317
3318                 glEnable(GL_TEXTURE_GEN_S);
3319                 checkGLcall("glEnable(GL_TEXTURE_GEN_S)");
3320                 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
3321                 checkGLcall("glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV)");
3322                 glEnable(GL_TEXTURE_GEN_T);
3323                 checkGLcall("glEnable(GL_TEXTURE_GEN_T)");
3324                 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
3325                 checkGLcall("glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV)");
3326                 glEnable(GL_TEXTURE_GEN_R);
3327                 checkGLcall("glEnable(GL_TEXTURE_GEN_R)");
3328                 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
3329                 checkGLcall("glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV)");
3330             }
3331         }
3332         break;
3333
3334     case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
3335         {
3336             if (GL_SUPPORT(NV_TEXGEN_REFLECTION)) {
3337             float s_plane[] = { 1.0, 0.0, 0.0, 0.0 };
3338             float t_plane[] = { 0.0, 1.0, 0.0, 0.0 };
3339             float r_plane[] = { 0.0, 0.0, 1.0, 0.0 };
3340             float q_plane[] = { 0.0, 0.0, 0.0, 1.0 };
3341             TRACE("WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR - Set eye plane\n");
3342
3343             glMatrixMode(GL_MODELVIEW);
3344             glPushMatrix();
3345             glLoadIdentity();
3346             glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
3347             glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
3348             glTexGenfv(GL_R, GL_EYE_PLANE, r_plane);
3349             glTexGenfv(GL_Q, GL_EYE_PLANE, q_plane);
3350             glPopMatrix();
3351
3352             glEnable(GL_TEXTURE_GEN_S);
3353             checkGLcall("glEnable(GL_TEXTURE_GEN_S)");
3354             glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV);
3355             checkGLcall("glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV)");
3356             glEnable(GL_TEXTURE_GEN_T);
3357             checkGLcall("glEnable(GL_TEXTURE_GEN_T)");
3358             glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV);
3359             checkGLcall("glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV)");
3360             glEnable(GL_TEXTURE_GEN_R);
3361             checkGLcall("glEnable(GL_TEXTURE_GEN_R)");
3362             glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV);
3363             checkGLcall("glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV)");
3364             }
3365         }
3366         break;
3367
3368     /* Unhandled types: */
3369     default:
3370         /* Todo: */
3371         /* ? disable GL_TEXTURE_GEN_n ? */
3372         glDisable(GL_TEXTURE_GEN_S);
3373         glDisable(GL_TEXTURE_GEN_T);
3374         glDisable(GL_TEXTURE_GEN_R);
3375         glDisable(GL_TEXTURE_GEN_Q);
3376         FIXME("Unhandled WINED3DTSS_TEXCOORDINDEX %x\n", stateblock->textureState[stage][WINED3DTSS_TEXCOORDINDEX]);
3377         break;
3378     }
3379
3380     /* Update the texture matrix */
3381     if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + stage))) {
3382         transform_texture(STATE_TEXTURESTAGE(stage, WINED3DTSS_TEXTURETRANSFORMFLAGS), stateblock, context);
3383     }
3384
3385     if(!isStateDirty(context, STATE_VDECL) && context->namedArraysLoaded) {
3386         /* Reload the arrays if we are using fixed function arrays to reflect the selected coord input
3387          * source. Call loadTexCoords directly because there is no need to reparse the vertex declaration
3388          * and do all the things linked to it
3389          * TODO: Tidy that up to reload only the arrays of the changed unit
3390          */
3391         GLint curVBO = GL_SUPPORT(ARB_VERTEX_BUFFER_OBJECT) ? -1 : 0;
3392
3393         unloadTexCoords(stateblock);
3394         loadTexCoords(stateblock, &stateblock->wineD3DDevice->strided_streams, &curVBO);
3395     }
3396 }
3397
3398 static void shaderconstant(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3399     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
3400
3401     /* Vertex and pixel shader states will call a shader upload, don't do anything as long one of them
3402      * has an update pending
3403      */
3404     if(isStateDirty(context, STATE_VDECL) ||
3405        isStateDirty(context, STATE_PIXELSHADER)) {
3406        return;
3407     }
3408
3409     device->shader_backend->shader_load_constants((IWineD3DDevice *) device, use_ps(device), use_vs(device));
3410 }
3411
3412 static void tex_bumpenvlscale(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3413     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
3414
3415     if(stateblock->pixelShader && stage != 0 &&
3416        ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.luminanceparams[stage]) {
3417         /* The pixel shader has to know the luminance scale. Do a constants update if it
3418          * isn't scheduled anyway
3419          */
3420         if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT) &&
3421            !isStateDirty(context, STATE_PIXELSHADER)) {
3422             shaderconstant(STATE_PIXELSHADERCONSTANT, stateblock, context);
3423         }
3424     }
3425 }
3426
3427 static void sampler_texmatrix(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3428     BOOL texIsPow2 = FALSE;
3429     DWORD sampler = state - STATE_SAMPLER(0);
3430     IWineD3DBaseTexture *texture = stateblock->textures[sampler];
3431
3432     if(!texture) return;
3433     /* The fixed function np2 texture emulation uses the texture matrix to fix up the coordinates
3434      * IWineD3DBaseTexture::ApplyStateChanges multiplies the set matrix with a fixup matrix. Before the
3435      * scaling is reapplied or removed, the texture matrix has to be reapplied
3436      *
3437      * The mapped stage is alrady active because the sampler() function below, which is part of the
3438      * misc pipeline
3439      */
3440     if(sampler < MAX_TEXTURES) {
3441         if(stateblock->textureDimensions[sampler] == GL_TEXTURE_2D ||
3442            stateblock->textureDimensions[sampler] == GL_TEXTURE_RECTANGLE_ARB) {
3443             if(((IWineD3DTextureImpl *)texture)->baseTexture.pow2Matrix[0] != 1.0 ||
3444                ((IWineD3DTextureImpl *)texture)->baseTexture.pow2Matrix[5] != 1.0 ) {
3445                 texIsPow2 = TRUE;
3446             }
3447         } else if(stateblock->textureDimensions[sampler] == GL_TEXTURE_CUBE_MAP_ARB) {
3448             if(((IWineD3DCubeTextureImpl *)texture)->baseTexture.pow2Matrix[0] != 1.0) {
3449                 texIsPow2 = TRUE;
3450             }
3451         }
3452
3453         if(texIsPow2 || context->lastWasPow2Texture[sampler]) {
3454             context->lastWasPow2Texture[sampler] = texIsPow2;
3455             transform_texture(STATE_TEXTURESTAGE(stateblock->wineD3DDevice->texUnitMap[sampler], WINED3DTSS_TEXTURETRANSFORMFLAGS), stateblock, context);
3456         }
3457     }
3458 }
3459
3460 static void sampler(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3461     DWORD sampler = state - STATE_SAMPLER(0);
3462     DWORD mapped_stage = stateblock->wineD3DDevice->texUnitMap[sampler];
3463     union {
3464         float f;
3465         DWORD d;
3466     } tmpvalue;
3467
3468     TRACE("Sampler: %d\n", sampler);
3469     /* Enabling and disabling texture dimensions is done by texture stage state / pixel shader setup, this function
3470      * only has to bind textures and set the per texture states
3471      */
3472
3473     if (mapped_stage == -1) {
3474         TRACE("No sampler mapped to stage %d. Returning.\n", sampler);
3475         return;
3476     }
3477
3478     if (GL_SUPPORT(ARB_MULTITEXTURE)) {
3479         if (mapped_stage >= GL_LIMITS(combined_samplers)) {
3480             return;
3481         }
3482         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
3483         checkGLcall("glActiveTextureARB");
3484     } else if (sampler > 0) {
3485         /* We can't do anything here */
3486         WARN("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
3487         return;
3488     }
3489
3490     if(stateblock->textures[sampler]) {
3491         IWineD3DBaseTexture_PreLoad(stateblock->textures[sampler]);
3492         IWineD3DBaseTexture_BindTexture(stateblock->textures[sampler]);
3493         IWineD3DBaseTexture_ApplyStateChanges(stateblock->textures[sampler], stateblock->textureState[sampler], stateblock->samplerState[sampler]);
3494
3495         if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS)) {
3496             tmpvalue.d = stateblock->samplerState[sampler][WINED3DSAMP_MIPMAPLODBIAS];
3497             glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
3498                       GL_TEXTURE_LOD_BIAS_EXT,
3499                       tmpvalue.f);
3500             checkGLcall("glTexEnvi(GL_TEXTURE_LOD_BIAS_EXT, ...)");
3501         }
3502
3503         if(!use_ps(stateblock->wineD3DDevice) && sampler < stateblock->lowest_disabled_stage) {
3504             if(stateblock->renderState[WINED3DRS_COLORKEYENABLE] && sampler == 0) {
3505                 /* If color keying is enabled update the alpha test, it depends on the existence
3506                  * of a color key in stage 0
3507                  */
3508                 state_alpha(WINED3DRS_COLORKEYENABLE, stateblock, context);
3509             }
3510         }
3511     } else if(mapped_stage < GL_LIMITS(textures)) {
3512         if(sampler < stateblock->lowest_disabled_stage) {
3513             /* TODO: What should I do with pixel shaders here ??? */
3514             if(stateblock->renderState[WINED3DRS_COLORKEYENABLE] && sampler == 0) {
3515                 /* If color keying is enabled update the alpha test, it depends on the existence
3516                 * of a color key in stage 0
3517                 */
3518                 state_alpha(WINED3DRS_COLORKEYENABLE, stateblock, context);
3519             }
3520         } /* Otherwise tex_colorop disables the stage */
3521         glBindTexture(GL_TEXTURE_2D, stateblock->wineD3DDevice->dummyTextureName[sampler]);
3522         checkGLcall("glBindTexture(GL_TEXTURE_2D, stateblock->wineD3DDevice->dummyTextureName[sampler])");
3523     }
3524 }
3525
3526 void apply_pshader_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3527     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
3528
3529     if (use_ps(device)) {
3530         if(!context->last_was_pshader) {
3531             state_fog(state, stateblock, context);
3532         }
3533         context->last_was_pshader = TRUE;
3534     } else {
3535         if(context->last_was_pshader) {
3536             state_fog(state, stateblock, context);
3537         }
3538         context->last_was_pshader = FALSE;
3539     }
3540 }
3541
3542 void apply_pixelshader(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3543     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
3544     BOOL use_pshader = use_ps(device);
3545     BOOL use_vshader = use_vs(device);
3546     int i;
3547
3548     if (use_pshader) {
3549         if(!context->last_was_pshader) {
3550             /* Former draw without a pixel shader, some samplers
3551              * may be disabled because of WINED3DTSS_COLOROP = WINED3DTOP_DISABLE
3552              * make sure to enable them
3553              */
3554             for(i=0; i < MAX_FRAGMENT_SAMPLERS; i++) {
3555                 if(!isStateDirty(context, STATE_SAMPLER(i))) {
3556                     sampler(STATE_SAMPLER(i), stateblock, context);
3557                 }
3558             }
3559         } else {
3560            /* Otherwise all samplers were activated by the code above in earlier draws, or by sampler()
3561             * if a different texture was bound. I don't have to do anything.
3562             */
3563         }
3564
3565         /* Compile and bind the shader */
3566         IWineD3DPixelShader_CompileShader(stateblock->pixelShader);
3567     } else {
3568         /* Disabled the pixel shader - color ops weren't applied
3569          * while it was enabled, so re-apply them.
3570          */
3571         for(i=0; i < MAX_TEXTURES; i++) {
3572             if(!isStateDirty(context, STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP))) {
3573                 device->StateTable[STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP)].apply
3574                         (STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP), stateblock, context);
3575             }
3576         }
3577     }
3578
3579     if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3580         device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3581
3582         if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
3583             shaderconstant(STATE_VERTEXSHADERCONSTANT, stateblock, context);
3584         }
3585     }
3586 }
3587
3588 static void shader_bumpenvmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3589     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
3590     if(stateblock->pixelShader && stage != 0 &&
3591        ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.bumpmat[stage]) {
3592         /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
3593          * anyway
3594          */
3595         if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT) &&
3596             !isStateDirty(context, STATE_PIXELSHADER)) {
3597             shaderconstant(STATE_PIXELSHADERCONSTANT, stateblock, context);
3598         }
3599     }
3600 }
3601
3602 static void tex_bumpenvmat(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3603     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
3604     float mat[2][2];
3605
3606     if(stage >= GL_LIMITS(texture_stages)) {
3607         WARN("Bump env matrix of unsupported stage set\n");
3608     } else if(GL_SUPPORT(ARB_MULTITEXTURE)) {
3609         GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + stage));
3610         checkGLcall("GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + stage))");
3611     }
3612     mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
3613     mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
3614     mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
3615     mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
3616     GL_EXTCALL(glTexBumpParameterfvATI(GL_BUMP_ROT_MATRIX_ATI, (float *) mat));
3617     checkGLcall("glTexBumpParameterfvATI");
3618 }
3619
3620 static void transform_world(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3621     /* This function is called by transform_view below if the view matrix was changed too
3622      *
3623      * Deliberately no check if the vertex declaration is dirty because the vdecl state
3624      * does not always update the world matrix, only on a switch between transformed
3625      * and untransformed draws. It *may* happen that the world matrix is set 2 times during one
3626      * draw, but that should be rather rare and cheaper in total.
3627      */
3628     glMatrixMode(GL_MODELVIEW);
3629     checkGLcall("glMatrixMode");
3630
3631     if(context->last_was_rhw) {
3632         glLoadIdentity();
3633         checkGLcall("glLoadIdentity()");
3634     } else {
3635         /* In the general case, the view matrix is the identity matrix */
3636         if (stateblock->wineD3DDevice->view_ident) {
3637             glLoadMatrixf(&stateblock->transforms[WINED3DTS_WORLDMATRIX(0)].u.m[0][0]);
3638             checkGLcall("glLoadMatrixf");
3639         } else {
3640             glLoadMatrixf(&stateblock->transforms[WINED3DTS_VIEW].u.m[0][0]);
3641             checkGLcall("glLoadMatrixf");
3642             glMultMatrixf(&stateblock->transforms[WINED3DTS_WORLDMATRIX(0)].u.m[0][0]);
3643             checkGLcall("glMultMatrixf");
3644         }
3645     }
3646 }
3647
3648 static void clipplane(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3649     UINT index = state - STATE_CLIPPLANE(0);
3650
3651     if(isStateDirty(context, STATE_TRANSFORM(WINED3DTS_VIEW)) || index >= GL_LIMITS(clipplanes)) {
3652         return;
3653     }
3654
3655     /* Clip Plane settings are affected by the model view in OpenGL, the View transform in direct3d */
3656     glMatrixMode(GL_MODELVIEW);
3657     glPushMatrix();
3658     glLoadMatrixf(&stateblock->transforms[WINED3DTS_VIEW].u.m[0][0]);
3659
3660     TRACE("Clipplane [%f,%f,%f,%f]\n",
3661           stateblock->clipplane[index][0],
3662           stateblock->clipplane[index][1],
3663           stateblock->clipplane[index][2],
3664           stateblock->clipplane[index][3]);
3665     glClipPlane(GL_CLIP_PLANE0 + index, stateblock->clipplane[index]);
3666     checkGLcall("glClipPlane");
3667
3668     glPopMatrix();
3669 }
3670
3671 static void transform_worldex(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3672     UINT matrix = state - STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0));
3673     GLenum glMat;
3674     TRACE("Setting world matrix %d\n", matrix);
3675
3676     if(matrix >= GL_LIMITS(blends)) {
3677         WARN("Unsupported blend matrix set\n");
3678         return;
3679     } else if(isStateDirty(context, STATE_TRANSFORM(WINED3DTS_VIEW))) {
3680         return;
3681     }
3682
3683     /* GL_MODELVIEW0_ARB:  0x1700
3684      * GL_MODELVIEW1_ARB:  0x0x850a
3685      * GL_MODELVIEW2_ARB:  0x8722
3686      * GL_MODELVIEW3_ARB:  0x8723
3687      * etc
3688      * GL_MODELVIEW31_ARB: 0x873F
3689      */
3690     if(matrix == 1) glMat = GL_MODELVIEW1_ARB;
3691     else glMat = GL_MODELVIEW2_ARB - 2 + matrix;
3692
3693     glMatrixMode(glMat);
3694     checkGLcall("glMatrixMode(glMat)");
3695
3696     /* World matrix 0 is multiplied with the view matrix because d3d uses 3 matrices while gl uses only 2. To avoid
3697      * weighting the view matrix incorrectly it has to be multiplied into every gl modelview matrix
3698      */
3699     if(stateblock->wineD3DDevice->view_ident) {
3700         glLoadMatrixf(&stateblock->transforms[WINED3DTS_WORLDMATRIX(matrix)].u.m[0][0]);
3701         checkGLcall("glLoadMatrixf")
3702     } else {
3703         glLoadMatrixf(&stateblock->transforms[WINED3DTS_VIEW].u.m[0][0]);
3704         checkGLcall("glLoadMatrixf")
3705         glMultMatrixf(&stateblock->transforms[WINED3DTS_WORLDMATRIX(matrix)].u.m[0][0]);
3706         checkGLcall("glMultMatrixf")
3707     }
3708 }
3709
3710 static void state_vertexblend_w(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3711     static BOOL once = FALSE;
3712
3713     switch(stateblock->renderState[WINED3DRS_VERTEXBLEND]) {
3714         case WINED3DVBF_1WEIGHTS:
3715         case WINED3DVBF_2WEIGHTS:
3716         case WINED3DVBF_3WEIGHTS:
3717             if(!once) {
3718                 once = TRUE;
3719                 /* TODO: Implement vertex blending in drawStridedSlow */
3720                 FIXME("Vertex blending enabled, but not supported by hardware\n");
3721             }
3722             break;
3723
3724         case WINED3DVBF_TWEENING:
3725             WARN("Tweening not supported yet\n");
3726     }
3727 }
3728
3729 static void state_vertexblend(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3730     WINED3DVERTEXBLENDFLAGS val = stateblock->renderState[WINED3DRS_VERTEXBLEND];
3731
3732     switch(val) {
3733         case WINED3DVBF_1WEIGHTS:
3734         case WINED3DVBF_2WEIGHTS:
3735         case WINED3DVBF_3WEIGHTS:
3736             glEnable(GL_VERTEX_BLEND_ARB);
3737             checkGLcall("glEnable(GL_VERTEX_BLEND_ARB)");
3738
3739             /* D3D adds one more matrix which has weight (1 - sum(weights)). This is enabled at context
3740              * creation with enabling GL_WEIGHT_SUM_UNITY_ARB.
3741              */
3742             GL_EXTCALL(glVertexBlendARB(stateblock->renderState[WINED3DRS_VERTEXBLEND] + 1));
3743
3744             if(!stateblock->wineD3DDevice->vertexBlendUsed) {
3745                 int i;
3746                 for(i = 1; i < GL_LIMITS(blends); i++) {
3747                     if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(i)))) {
3748                         transform_worldex(STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(i)), stateblock, context);
3749                     }
3750                 }
3751                 stateblock->wineD3DDevice->vertexBlendUsed = TRUE;
3752             }
3753             break;
3754
3755         case WINED3DVBF_DISABLE:
3756         case WINED3DVBF_0WEIGHTS: /* for Indexed vertex blending - not supported */
3757             glDisable(GL_VERTEX_BLEND_ARB);
3758             checkGLcall("glDisable(GL_VERTEX_BLEND_ARB)");
3759             break;
3760
3761         case WINED3DVBF_TWEENING:
3762             /* Just set the vertex weight for weight 0, enable vertex blending and hope the app doesn't have
3763              * vertex weights in the vertices?
3764              * For now we don't report that as supported, so a warn should suffice
3765              */
3766             WARN("Tweening not supported yet\n");
3767             break;
3768     }
3769 }
3770
3771 static void transform_view(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3772     unsigned int k;
3773
3774     /* If we are changing the View matrix, reset the light and clipping planes to the new view
3775      * NOTE: We have to reset the positions even if the light/plane is not currently
3776      *       enabled, since the call to enable it will not reset the position.
3777      * NOTE2: Apparently texture transforms do NOT need reapplying
3778      */
3779
3780     PLIGHTINFOEL *light = NULL;
3781
3782     glMatrixMode(GL_MODELVIEW);
3783     checkGLcall("glMatrixMode(GL_MODELVIEW)");
3784     glLoadMatrixf(&stateblock->transforms[WINED3DTS_VIEW].u.m[0][0]);
3785     checkGLcall("glLoadMatrixf(...)");
3786
3787     /* Reset lights. TODO: Call light apply func */
3788     for(k = 0; k < stateblock->wineD3DDevice->maxConcurrentLights; k++) {
3789         light = stateblock->activeLights[k];
3790         if(!light) continue;
3791         glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, light->lightPosn);
3792         checkGLcall("glLightfv posn");
3793         glLightfv(GL_LIGHT0 + light->glIndex, GL_SPOT_DIRECTION, light->lightDirn);
3794         checkGLcall("glLightfv dirn");
3795     }
3796
3797     /* Reset Clipping Planes  */
3798     for (k = 0; k < GL_LIMITS(clipplanes); k++) {
3799         if(!isStateDirty(context, STATE_CLIPPLANE(k))) {
3800             clipplane(STATE_CLIPPLANE(k), stateblock, context);
3801         }
3802     }
3803
3804     if(context->last_was_rhw) {
3805         glLoadIdentity();
3806         checkGLcall("glLoadIdentity()");
3807         /* No need to update the world matrix, the identity is fine */
3808         return;
3809     }
3810
3811     /* Call the world matrix state, this will apply the combined WORLD + VIEW matrix
3812      * No need to do it here if the state is scheduled for update.
3813      */
3814     if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)))) {
3815         transform_world(STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), stateblock, context);
3816     }
3817
3818     /* Avoid looping over a number of matrices if the app never used the functionality */
3819     if(stateblock->wineD3DDevice->vertexBlendUsed) {
3820         for(k = 1; k < GL_LIMITS(blends); k++) {
3821             if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(k)))) {
3822                 transform_worldex(STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(k)), stateblock, context);
3823             }
3824         }
3825     }
3826 }
3827
3828 static void transform_projection(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3829     glMatrixMode(GL_PROJECTION);
3830     checkGLcall("glMatrixMode(GL_PROJECTION)");
3831     glLoadIdentity();
3832     checkGLcall("glLoadIdentity");
3833
3834     if(context->last_was_rhw) {
3835         double X, Y, height, width, minZ, maxZ;
3836
3837         X      = stateblock->viewport.X;
3838         Y      = stateblock->viewport.Y;
3839         height = stateblock->viewport.Height;
3840         width  = stateblock->viewport.Width;
3841         minZ   = stateblock->viewport.MinZ;
3842         maxZ   = stateblock->viewport.MaxZ;
3843
3844         if(!stateblock->wineD3DDevice->untransformed) {
3845             /* Transformed vertices are supposed to bypass the whole transform pipeline including
3846              * frustum clipping. This can't be done in opengl, so this code adjusts the Z range to
3847              * suppress depth clipping. This can be done because it is an orthogonal projection and
3848              * the Z coordinate does not affect the size of the primitives. Half Life 1 and Prince of
3849              * Persia 3D need this.
3850              *
3851              * Note that using minZ and maxZ here doesn't entirely fix the problem, since view frustum
3852              * clipping is still enabled, but it seems to fix it for all apps tested so far. A minor
3853              * problem can be witnessed in half-life 1 engine based games, the weapon is clipped close
3854              * to the viewer.
3855              *
3856              * Also note that this breaks z comparison against z values filled in with clear,
3857              * but no app depending on that and disabled clipping has been found yet. Comparing
3858              * primitives against themselves works, so the Z buffer is still intact for normal hidden
3859              * surface removal.
3860              *
3861              * We could disable clipping entirely by setting the near to infinity and far to -infinity,
3862              * but this would break Z buffer operation. Raising the range to something less than
3863              * infinity would help a bit at the cost of Z precision, but it wouldn't eliminate the
3864              * problem either.
3865              */
3866             TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
3867             if(stateblock->wineD3DDevice->render_offscreen) {
3868                 glOrtho(X, X + width, -Y, -Y - height, -minZ, -maxZ);
3869             } else {
3870                 glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
3871             }
3872         } else {
3873             /* If the app mixes transformed and untransformed primitives we can't use the coordinate system
3874              * trick above because this would mess up transformed and untransformed Z order. Pass the z position
3875              * unmodified to opengl.
3876              *
3877              * If the app depends on mixed types and disabled clipping we're out of luck without a pipeline
3878              * replacement shader.
3879              */
3880             TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, 1.0, -1.0);
3881             if(stateblock->wineD3DDevice->render_offscreen) {
3882                 glOrtho(X, X + width, -Y, -Y - height, 0.0, -1.0);
3883             } else {
3884                 glOrtho(X, X + width, Y + height, Y, 0.0, -1.0);
3885             }
3886         }
3887         checkGLcall("glOrtho");
3888
3889         /* Window Coord 0 is the middle of the first pixel, so translate by 1/2 pixels */
3890         glTranslatef(0.5, 0.5, 0);
3891         checkGLcall("glTranslatef(0.5, 0.5, 0)");
3892         /* D3D texture coordinates are flipped compared to OpenGL ones, so
3893          * render everything upside down when rendering offscreen. */
3894         if (stateblock->wineD3DDevice->render_offscreen) {
3895             glScalef(1.0, -1.0, 1.0);
3896             checkGLcall("glScalef");
3897         }
3898     } else {
3899         /* The rule is that the window coordinate 0 does not correspond to the
3900             beginning of the first pixel, but the center of the first pixel.
3901             As a consequence if you want to correctly draw one line exactly from
3902             the left to the right end of the viewport (with all matrices set to
3903             be identity), the x coords of both ends of the line would be not
3904             -1 and 1 respectively but (-1-1/viewport_widh) and (1-1/viewport_width)
3905             instead.
3906
3907             1.0 / Width is used because the coord range goes from -1.0 to 1.0, then we
3908             divide by the Width/Height, so we need the half range(1.0) to translate by
3909             half a pixel.
3910
3911             The other fun is that d3d's output z range after the transformation is [0;1],
3912             but opengl's is [-1;1]. Since the z buffer is in range [0;1] for both, gl
3913             scales [-1;1] to [0;1]. This would mean that we end up in [0.5;1] and loose a lot
3914             of Z buffer precision and the clear values do not match in the z test. Thus scale
3915             [0;1] to [-1;1], so when gl undoes that we utilize the full z range
3916          */
3917
3918         /*
3919          * Careful with the order of operations here, we're essentially working backwards:
3920          * x = x + 1/w;
3921          * y = (y - 1/h) * flip;
3922          * z = z * 2 - 1;
3923          *
3924          * Becomes:
3925          * glTranslatef(0.0, 0.0, -1.0);
3926          * glScalef(1.0, 1.0, 2.0);
3927          *
3928          * glScalef(1.0, flip, 1.0);
3929          * glTranslatef(1/w, -1/h, 0.0);
3930          *
3931          * This is equivalent to:
3932          * glTranslatef(1/w, -flip/h, -1.0)
3933          * glScalef(1.0, flip, 2.0);
3934          */
3935
3936         if (stateblock->wineD3DDevice->render_offscreen) {
3937             /* D3D texture coordinates are flipped compared to OpenGL ones, so
3938              * render everything upside down when rendering offscreen. */
3939             glTranslatef(1.0 / stateblock->viewport.Width, 1.0 / stateblock->viewport.Height, -1.0);
3940             checkGLcall("glTranslatef(1.0 / width, 1.0 / height, -1.0)");
3941             glScalef(1.0, -1.0, 2.0);
3942         } else {
3943             glTranslatef(1.0 / stateblock->viewport.Width, -1.0 / stateblock->viewport.Height, -1.0);
3944             checkGLcall("glTranslatef(1.0 / width, -1.0 / height, -1.0)");
3945             glScalef(1.0, 1.0, 2.0);
3946         }
3947         checkGLcall("glScalef");
3948
3949         glMultMatrixf(&stateblock->transforms[WINED3DTS_PROJECTION].u.m[0][0]);
3950         checkGLcall("glLoadMatrixf");
3951     }
3952 }
3953
3954 /* This should match any arrays loaded in loadVertexData.
3955  * stateblock impl is required for GL_SUPPORT
3956  * TODO: Only load / unload arrays if we have to.
3957  */
3958 static inline void unloadVertexData(IWineD3DStateBlockImpl *stateblock) {
3959     glDisableClientState(GL_VERTEX_ARRAY);
3960     glDisableClientState(GL_NORMAL_ARRAY);
3961     glDisableClientState(GL_COLOR_ARRAY);
3962     if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
3963         glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
3964     }
3965     if (GL_SUPPORT(ARB_VERTEX_BLEND)) {
3966         glDisableClientState(GL_WEIGHT_ARRAY_ARB);
3967     }
3968     unloadTexCoords(stateblock);
3969 }
3970
3971 /* This should match any arrays loaded in loadNumberedArrays
3972  * TODO: Only load / unload arrays if we have to.
3973  */
3974 static inline void unloadNumberedArrays(IWineD3DStateBlockImpl *stateblock) {
3975     /* disable any attribs (this is the same for both GLSL and ARB modes) */
3976     GLint maxAttribs = 16;
3977     int i;
3978
3979     /* Leave all the attribs disabled */
3980     glGetIntegerv(GL_MAX_VERTEX_ATTRIBS_ARB, &maxAttribs);
3981     /* MESA does not support it right not */
3982     if (glGetError() != GL_NO_ERROR)
3983         maxAttribs = 16;
3984     for (i = 0; i < maxAttribs; ++i) {
3985         GL_EXTCALL(glDisableVertexAttribArrayARB(i));
3986         checkGLcall("glDisableVertexAttribArrayARB(reg)");
3987         /* Some Windows drivers(NV GF 7) use the latest value that was used when drawing with the now
3988          * deactivated stream disabled, some other drivers(ATI, NV GF 8) set the undefined values to 0x00.
3989          * Let's set them to 0x00 to avoid hitting some undefined aspects of OpenGL. All that is really
3990          * important here is the glDisableVertexAttribArrayARB call above. The test shows that the refrast
3991          * keeps dereferencing the pointers, which would cause crashes in some games like Half Life 2: Episode Two.
3992          */
3993         GL_EXTCALL(glVertexAttrib4NubARB(i, 0, 0, 0, 0));
3994         checkGLcall("glVertexAttrib4NubARB(i, 0, 0, 0, 0)");
3995     }
3996 }
3997
3998 static inline void loadNumberedArrays(IWineD3DStateBlockImpl *stateblock, WineDirect3DVertexStridedData *strided) {
3999     GLint curVBO = GL_SUPPORT(ARB_VERTEX_BUFFER_OBJECT) ? -1 : 0;
4000     int i;
4001     UINT *offset = stateblock->streamOffset;
4002     IWineD3DVertexBufferImpl *vb;
4003     DWORD_PTR shift_index;
4004
4005     /* Default to no instancing */
4006     stateblock->wineD3DDevice->instancedDraw = FALSE;
4007
4008     for (i = 0; i < MAX_ATTRIBS; i++) {
4009
4010         if (!strided->u.input[i].lpData && !strided->u.input[i].VBO)
4011             continue;
4012
4013         /* Do not load instance data. It will be specified using glTexCoord by drawprim */
4014         if(stateblock->streamFlags[strided->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
4015             GL_EXTCALL(glDisableVertexAttribArrayARB(i));
4016             stateblock->wineD3DDevice->instancedDraw = TRUE;
4017             continue;
4018         }
4019
4020         TRACE_(d3d_shader)("Loading array %u [VBO=%u]\n", i, strided->u.input[i].VBO);
4021
4022         if(strided->u.input[i].dwStride) {
4023             if(curVBO != strided->u.input[i].VBO) {
4024                 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, strided->u.input[i].VBO));
4025                 checkGLcall("glBindBufferARB");
4026                 curVBO = strided->u.input[i].VBO;
4027             }
4028             vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[strided->u.input[i].streamNo];
4029             /* Use the VBO to find out if a vertex buffer exists, not the vb pointer. vb can point to a
4030              * user pointer data blob. In that case curVBO will be 0. If there is a vertex buffer but no
4031              * vbo we won't be load converted attributes anyway
4032              */
4033             if(curVBO && vb->conv_shift) {
4034                 TRACE("Loading attribute from shifted buffer\n");
4035                 TRACE("Attrib %d has original stride %d, new stride %d\n", i, strided->u.input[i].dwStride, vb->conv_stride);
4036                 TRACE("Original offset %p, additional offset 0x%08x\n",strided->u.input[i].lpData, vb->conv_shift[(DWORD_PTR) strided->u.input[i].lpData]);
4037                 TRACE("Opengl type %x\n", WINED3D_ATR_GLTYPE(strided->u.input[i].dwType));
4038                 shift_index = ((DWORD_PTR) strided->u.input[i].lpData + offset[strided->u.input[i].streamNo]);
4039                 shift_index = shift_index % strided->u.input[i].dwStride;
4040                 GL_EXTCALL(glVertexAttribPointerARB(i,
4041                                 WINED3D_ATR_SIZE(strided->u.input[i].dwType),
4042                                 WINED3D_ATR_GLTYPE(strided->u.input[i].dwType),
4043                                 WINED3D_ATR_NORMALIZED(strided->u.input[i].dwType),
4044                                 vb->conv_stride,
4045
4046                                 strided->u.input[i].lpData + vb->conv_shift[shift_index] +
4047                                 stateblock->loadBaseVertexIndex * strided->u.input[i].dwStride +
4048                                 offset[strided->u.input[i].streamNo]));
4049
4050             } else {
4051                 GL_EXTCALL(glVertexAttribPointerARB(i,
4052                                 WINED3D_ATR_SIZE(strided->u.input[i].dwType),
4053                                 WINED3D_ATR_GLTYPE(strided->u.input[i].dwType),
4054                                 WINED3D_ATR_NORMALIZED(strided->u.input[i].dwType),
4055                                 strided->u.input[i].dwStride,
4056
4057                                 strided->u.input[i].lpData +
4058                                 stateblock->loadBaseVertexIndex * strided->u.input[i].dwStride +
4059                                 offset[strided->u.input[i].streamNo]) );
4060                 }
4061             GL_EXTCALL(glEnableVertexAttribArrayARB(i));
4062         } else {
4063             /* Stride = 0 means always the same values. glVertexAttribPointerARB doesn't do that. Instead disable the pointer and
4064              * set up the attribute statically. But we have to figure out the system memory address.
4065              */
4066             BYTE *ptr = strided->u.input[i].lpData + offset[strided->u.input[i].streamNo];
4067             if(strided->u.input[i].VBO) {
4068                 vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[strided->u.input[i].streamNo];
4069                 ptr += (long) vb->resource.allocatedMemory;
4070             }
4071             GL_EXTCALL(glDisableVertexAttribArrayARB(i));
4072
4073             switch(strided->u.input[i].dwType) {
4074                 case WINED3DDECLTYPE_FLOAT1:
4075                     GL_EXTCALL(glVertexAttrib1fvARB(i, (float *) ptr));
4076                     break;
4077                 case WINED3DDECLTYPE_FLOAT2:
4078                     GL_EXTCALL(glVertexAttrib2fvARB(i, (float *) ptr));
4079                     break;
4080                 case WINED3DDECLTYPE_FLOAT3:
4081                     GL_EXTCALL(glVertexAttrib3fvARB(i, (float *) ptr));
4082                     break;
4083                 case WINED3DDECLTYPE_FLOAT4:
4084                     GL_EXTCALL(glVertexAttrib4fvARB(i, (float *) ptr));
4085                     break;
4086
4087                 case WINED3DDECLTYPE_UBYTE4:
4088                     GL_EXTCALL(glVertexAttrib4NubvARB(i, ptr));
4089                     break;
4090                 case WINED3DDECLTYPE_UBYTE4N:
4091                 case WINED3DDECLTYPE_D3DCOLOR:
4092                     GL_EXTCALL(glVertexAttrib4NubvARB(i, ptr));
4093                     break;
4094
4095                 case WINED3DDECLTYPE_SHORT2:
4096                     GL_EXTCALL(glVertexAttrib4svARB(i, (GLshort *) ptr));
4097                     break;
4098                 case WINED3DDECLTYPE_SHORT4:
4099                     GL_EXTCALL(glVertexAttrib4svARB(i, (GLshort *) ptr));
4100                     break;
4101
4102                 case WINED3DDECLTYPE_SHORT2N:
4103                 {
4104                     GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
4105                     GL_EXTCALL(glVertexAttrib4NsvARB(i, s));
4106                     break;
4107                 }
4108                 case WINED3DDECLTYPE_USHORT2N:
4109                 {
4110                     GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
4111                     GL_EXTCALL(glVertexAttrib4NusvARB(i, s));
4112                     break;
4113                 }
4114                 case WINED3DDECLTYPE_SHORT4N:
4115                     GL_EXTCALL(glVertexAttrib4NsvARB(i, (GLshort *) ptr));
4116                     break;
4117                 case WINED3DDECLTYPE_USHORT4N:
4118                     GL_EXTCALL(glVertexAttrib4NusvARB(i, (GLushort *) ptr));
4119                     break;
4120
4121                 case WINED3DDECLTYPE_UDEC3:
4122                     FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
4123                     /*glVertexAttrib3usvARB(i, (GLushort *) ptr); Does not exist */
4124                     break;
4125                 case WINED3DDECLTYPE_DEC3N:
4126                     FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
4127                     /*glVertexAttrib3NusvARB(i, (GLushort *) ptr); Does not exist */
4128                     break;
4129
4130                 case WINED3DDECLTYPE_FLOAT16_2:
4131                     /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
4132                      * byte float according to the IEEE standard
4133                      */
4134                     FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_2\n");
4135                     break;
4136                 case WINED3DDECLTYPE_FLOAT16_4:
4137                     FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_4\n");
4138                     break;
4139
4140                 case WINED3DDECLTYPE_UNUSED:
4141                 default:
4142                     ERR("Unexpected declaration in stride 0 attributes\n");
4143                     break;
4144
4145             }
4146         }
4147     }
4148     checkGLcall("Loading numbered arrays");
4149 }
4150
4151 /* Used from 2 different functions, and too big to justify making it inlined */
4152 static void loadVertexData(IWineD3DStateBlockImpl *stateblock, WineDirect3DVertexStridedData *sd) {
4153     UINT *offset = stateblock->streamOffset;
4154     GLint curVBO = GL_SUPPORT(ARB_VERTEX_BUFFER_OBJECT) ? -1 : 0;
4155
4156     TRACE("Using fast vertex array code\n");
4157
4158     /* This is fixed function pipeline only, and the fixed function pipeline doesn't do instancing */
4159     stateblock->wineD3DDevice->instancedDraw = FALSE;
4160
4161     /* Blend Data ---------------------------------------------- */
4162     if( (sd->u.s.blendWeights.lpData) || (sd->u.s.blendWeights.VBO) ||
4163         (sd->u.s.blendMatrixIndices.lpData) || (sd->u.s.blendMatrixIndices.VBO) ) {
4164
4165         if (GL_SUPPORT(ARB_VERTEX_BLEND)) {
4166             TRACE("Blend %d %p %d\n", WINED3D_ATR_SIZE(sd->u.s.blendWeights.dwType),
4167                 sd->u.s.blendWeights.lpData + stateblock->loadBaseVertexIndex * sd->u.s.blendWeights.dwStride, sd->u.s.blendWeights.dwStride + offset[sd->u.s.blendWeights.streamNo]);
4168
4169             glEnableClientState(GL_WEIGHT_ARRAY_ARB);
4170             checkGLcall("glEnableClientState(GL_WEIGHT_ARRAY_ARB)");
4171
4172             GL_EXTCALL(glVertexBlendARB(WINED3D_ATR_SIZE(sd->u.s.blendWeights.dwType) + 1));
4173
4174             VTRACE(("glWeightPointerARB(%d, GL_FLOAT, %d, %p)\n",
4175                 WINED3D_ATR_SIZE(sd->u.s.blendWeights.dwType) ,
4176                 sd->u.s.blendWeights.dwStride,
4177                 sd->u.s.blendWeights.lpData + stateblock->loadBaseVertexIndex * sd->u.s.blendWeights.dwStride + offset[sd->u.s.blendWeights.streamNo]));
4178
4179             if(curVBO != sd->u.s.blendWeights.VBO) {
4180                 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, sd->u.s.blendWeights.VBO));
4181                 checkGLcall("glBindBufferARB");
4182                 curVBO = sd->u.s.blendWeights.VBO;
4183             }
4184
4185             GL_EXTCALL(glWeightPointerARB)(
4186                 WINED3D_ATR_SIZE(sd->u.s.blendWeights.dwType),
4187                 WINED3D_ATR_GLTYPE(sd->u.s.blendWeights.dwType),
4188                 sd->u.s.blendWeights.dwStride,
4189                 sd->u.s.blendWeights.lpData + stateblock->loadBaseVertexIndex * sd->u.s.blendWeights.dwStride + offset[sd->u.s.blendWeights.streamNo]);
4190
4191             checkGLcall("glWeightPointerARB");
4192
4193             if((sd->u.s.blendMatrixIndices.lpData) || (sd->u.s.blendMatrixIndices.VBO)){
4194                 static BOOL showfixme = TRUE;
4195                 if(showfixme){
4196                     FIXME("blendMatrixIndices support\n");
4197                     showfixme = FALSE;
4198                 }
4199             }
4200         } else {
4201             /* TODO: support blends in drawStridedSlow
4202              * No need to write a FIXME here, this is done after the general vertex decl decoding
4203              */
4204             WARN("unsupported blending in openGl\n");
4205         }
4206     } else {
4207         if (GL_SUPPORT(ARB_VERTEX_BLEND)) {
4208             static const GLbyte one = 1;
4209             GL_EXTCALL(glWeightbvARB(1, &one));
4210             checkGLcall("glWeightivARB(GL_LIMITS(blends), weights)");
4211         }
4212     }
4213
4214     /* Point Size ----------------------------------------------*/
4215     if (sd->u.s.pSize.lpData || sd->u.s.pSize.VBO) {
4216
4217         /* no such functionality in the fixed function GL pipeline */
4218         TRACE("Cannot change ptSize here in openGl\n");
4219         /* TODO: Implement this function in using shaders if they are available */
4220
4221     }
4222
4223     /* Vertex Pointers -----------------------------------------*/
4224     if (sd->u.s.position.lpData != NULL || sd->u.s.position.VBO != 0) {
4225         /* Note dwType == float3 or float4 == 2 or 3 */
4226         VTRACE(("glVertexPointer(%d, GL_FLOAT, %d, %p)\n",
4227                 sd->u.s.position.dwStride,
4228                 sd->u.s.position.dwType + 1,
4229                 sd->u.s.position.lpData));
4230
4231         if(curVBO != sd->u.s.position.VBO) {
4232             GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, sd->u.s.position.VBO));
4233             checkGLcall("glBindBufferARB");
4234             curVBO = sd->u.s.position.VBO;
4235         }
4236
4237         /* min(WINED3D_ATR_SIZE(position),3) to Disable RHW mode as 'w' coord
4238            handling for rhw mode should not impact screen position whereas in GL it does.
4239            This may result in very slightly distorted textures in rhw mode.
4240            There's always the other option of fixing the view matrix to
4241            prevent w from having any effect.
4242
4243            This only applies to user pointer sources, in VBOs the vertices are fixed up
4244          */
4245         if(sd->u.s.position.VBO == 0) {
4246             glVertexPointer(3 /* min(WINED3D_ATR_SIZE(sd->u.s.position.dwType),3) */,
4247                 WINED3D_ATR_GLTYPE(sd->u.s.position.dwType),
4248                 sd->u.s.position.dwStride, sd->u.s.position.lpData + stateblock->loadBaseVertexIndex * sd->u.s.position.dwStride + offset[sd->u.s.position.streamNo]);
4249         } else {
4250             glVertexPointer(
4251                 WINED3D_ATR_SIZE(sd->u.s.position.dwType),
4252                 WINED3D_ATR_GLTYPE(sd->u.s.position.dwType),
4253                 sd->u.s.position.dwStride, sd->u.s.position.lpData + stateblock->loadBaseVertexIndex * sd->u.s.position.dwStride + offset[sd->u.s.position.streamNo]);
4254         }
4255         checkGLcall("glVertexPointer(...)");
4256         glEnableClientState(GL_VERTEX_ARRAY);
4257         checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
4258     }
4259
4260     /* Normals -------------------------------------------------*/
4261     if (sd->u.s.normal.lpData || sd->u.s.normal.VBO) {
4262         /* Note dwType == float3 or float4 == 2 or 3 */
4263         VTRACE(("glNormalPointer(GL_FLOAT, %d, %p)\n",
4264                 sd->u.s.normal.dwStride,
4265                 sd->u.s.normal.lpData));
4266         if(curVBO != sd->u.s.normal.VBO) {
4267             GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, sd->u.s.normal.VBO));
4268             checkGLcall("glBindBufferARB");
4269             curVBO = sd->u.s.normal.VBO;
4270         }
4271         glNormalPointer(
4272             WINED3D_ATR_GLTYPE(sd->u.s.normal.dwType),
4273             sd->u.s.normal.dwStride,
4274             sd->u.s.normal.lpData + stateblock->loadBaseVertexIndex * sd->u.s.normal.dwStride + offset[sd->u.s.normal.streamNo]);
4275         checkGLcall("glNormalPointer(...)");
4276         glEnableClientState(GL_NORMAL_ARRAY);
4277         checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
4278
4279     } else {
4280         glNormal3f(0, 0, 0);
4281         checkGLcall("glNormal3f(0, 0, 0)");
4282     }
4283
4284     /* Diffuse Colour --------------------------------------------*/
4285     /*  WARNING: Data here MUST be in RGBA format, so cannot      */
4286     /*     go directly into fast mode from app pgm, because       */
4287     /*     directx requires data in BGRA format.                  */
4288     /* currently fixupVertices swizzles the format, but this isn't*/
4289     /* very practical when using VBOs                             */
4290     /* NOTE: Unless we write a vertex shader to swizzle the colour*/
4291     /* , or the user doesn't care and wants the speed advantage   */
4292
4293     if (sd->u.s.diffuse.lpData || sd->u.s.diffuse.VBO) {
4294         /* Note dwType == float3 or float4 == 2 or 3 */
4295         VTRACE(("glColorPointer(4, GL_UNSIGNED_BYTE, %d, %p)\n",
4296                 sd->u.s.diffuse.dwStride,
4297                 sd->u.s.diffuse.lpData));
4298
4299         if(curVBO != sd->u.s.diffuse.VBO) {
4300             GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, sd->u.s.diffuse.VBO));
4301             checkGLcall("glBindBufferARB");
4302             curVBO = sd->u.s.diffuse.VBO;
4303         }
4304
4305         glColorPointer(WINED3D_ATR_SIZE(sd->u.s.diffuse.dwType),
4306                        WINED3D_ATR_GLTYPE(sd->u.s.diffuse.dwType),
4307                        sd->u.s.diffuse.dwStride,
4308                        sd->u.s.diffuse.lpData + stateblock->loadBaseVertexIndex * sd->u.s.diffuse.dwStride + offset[sd->u.s.diffuse.streamNo]);
4309         checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
4310         glEnableClientState(GL_COLOR_ARRAY);
4311         checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
4312
4313     } else {
4314         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4315         checkGLcall("glColor4f(1, 1, 1, 1)");
4316     }
4317
4318     /* Specular Colour ------------------------------------------*/
4319     if (sd->u.s.specular.lpData || sd->u.s.specular.VBO) {
4320         TRACE("setting specular colour\n");
4321         /* Note dwType == float3 or float4 == 2 or 3 */
4322         VTRACE(("glSecondaryColorPointer(4, GL_UNSIGNED_BYTE, %d, %p)\n",
4323                 sd->u.s.specular.dwStride,
4324                 sd->u.s.specular.lpData));
4325         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
4326             if(curVBO != sd->u.s.specular.VBO) {
4327                 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, sd->u.s.specular.VBO));
4328                 checkGLcall("glBindBufferARB");
4329                 curVBO = sd->u.s.specular.VBO;
4330             }
4331             GL_EXTCALL(glSecondaryColorPointerEXT)(WINED3D_ATR_SIZE(sd->u.s.specular.dwType),
4332                                                    WINED3D_ATR_GLTYPE(sd->u.s.specular.dwType),
4333                                                    sd->u.s.specular.dwStride,
4334                                                    sd->u.s.specular.lpData + stateblock->loadBaseVertexIndex * sd->u.s.specular.dwStride + offset[sd->u.s.specular.streamNo]);
4335             vcheckGLcall("glSecondaryColorPointerEXT(4, GL_UNSIGNED_BYTE, ...)");
4336             glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
4337             vcheckGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
4338         } else {
4339
4340         /* Missing specular color is not critical, no warnings */
4341         VTRACE(("Specular colour is not supported in this GL implementation\n"));
4342         }
4343
4344     } else {
4345         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
4346             GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
4347             checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
4348         } else {
4349
4350             /* Missing specular color is not critical, no warnings */
4351             VTRACE(("Specular colour is not supported in this GL implementation\n"));
4352         }
4353     }
4354
4355     /* Texture coords -------------------------------------------*/
4356     loadTexCoords(stateblock, sd, &curVBO);
4357 }
4358
4359 static inline void drawPrimitiveTraceDataLocations(
4360     WineDirect3DVertexStridedData *dataLocations) {
4361
4362     /* Dump out what parts we have supplied */
4363     TRACE("Strided Data:\n");
4364     TRACE_STRIDED((dataLocations), position);
4365     TRACE_STRIDED((dataLocations), blendWeights);
4366     TRACE_STRIDED((dataLocations), blendMatrixIndices);
4367     TRACE_STRIDED((dataLocations), normal);
4368     TRACE_STRIDED((dataLocations), pSize);
4369     TRACE_STRIDED((dataLocations), diffuse);
4370     TRACE_STRIDED((dataLocations), specular);
4371     TRACE_STRIDED((dataLocations), texCoords[0]);
4372     TRACE_STRIDED((dataLocations), texCoords[1]);
4373     TRACE_STRIDED((dataLocations), texCoords[2]);
4374     TRACE_STRIDED((dataLocations), texCoords[3]);
4375     TRACE_STRIDED((dataLocations), texCoords[4]);
4376     TRACE_STRIDED((dataLocations), texCoords[5]);
4377     TRACE_STRIDED((dataLocations), texCoords[6]);
4378     TRACE_STRIDED((dataLocations), texCoords[7]);
4379     TRACE_STRIDED((dataLocations), position2);
4380     TRACE_STRIDED((dataLocations), normal2);
4381     TRACE_STRIDED((dataLocations), tangent);
4382     TRACE_STRIDED((dataLocations), binormal);
4383     TRACE_STRIDED((dataLocations), tessFactor);
4384     TRACE_STRIDED((dataLocations), fog);
4385     TRACE_STRIDED((dataLocations), depth);
4386     TRACE_STRIDED((dataLocations), sample);
4387
4388     return;
4389 }
4390
4391 static void streamsrc(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4392     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
4393     BOOL fixup = FALSE;
4394     WineDirect3DVertexStridedData *dataLocations = &device->strided_streams;
4395     BOOL useVertexShaderFunction;
4396
4397     if (device->vs_selected_mode != SHADER_NONE && stateblock->vertexShader &&
4398         ((IWineD3DVertexShaderImpl *)stateblock->vertexShader)->baseShader.function != NULL) {
4399         useVertexShaderFunction = TRUE;
4400     } else {
4401         useVertexShaderFunction = FALSE;
4402     }
4403
4404
4405     if(device->up_strided) {
4406         /* Note: this is a ddraw fixed-function code path */
4407         TRACE("================ Strided Input ===================\n");
4408         memcpy(dataLocations, device->up_strided, sizeof(*dataLocations));
4409
4410         if(TRACE_ON(d3d)) {
4411             drawPrimitiveTraceDataLocations(dataLocations);
4412         }
4413     } else {
4414         /* Note: This is a fixed function or shader codepath.
4415          * This means it must handle both types of strided data.
4416          * Shaders must go through here to zero the strided data, even if they
4417          * don't set any declaration at all
4418          */
4419         TRACE("================ Vertex Declaration  ===================\n");
4420         memset(dataLocations, 0, sizeof(*dataLocations));
4421         primitiveDeclarationConvertToStridedData((IWineD3DDevice *) device,
4422                 useVertexShaderFunction, dataLocations, &fixup);
4423     }
4424
4425     if (dataLocations->u.s.position_transformed) {
4426         useVertexShaderFunction = FALSE;
4427     }
4428
4429     /* Unload the old arrays before loading the new ones to get old junk out */
4430     if(context->numberedArraysLoaded) {
4431         unloadNumberedArrays(stateblock);
4432         context->numberedArraysLoaded = FALSE;
4433     }
4434     if(context->namedArraysLoaded) {
4435         unloadVertexData(stateblock);
4436         context->namedArraysLoaded = FALSE;
4437     }
4438
4439     if(useVertexShaderFunction) {
4440         if(((IWineD3DVertexDeclarationImpl *) stateblock->vertexDecl)->half_float_conv_needed && !fixup) {
4441             TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion\n");
4442             device->useDrawStridedSlow = TRUE;
4443             context->numberedArraysLoaded = FALSE;
4444         } else {
4445             TRACE("Loading numbered arrays\n");
4446             loadNumberedArrays(stateblock, dataLocations);
4447             device->useDrawStridedSlow = FALSE;
4448             context->numberedArraysLoaded = TRUE;
4449         }
4450     } else if (fixup ||
4451                (dataLocations->u.s.pSize.lpData == NULL &&
4452                 dataLocations->u.s.diffuse.lpData == NULL &&
4453                 dataLocations->u.s.specular.lpData == NULL)) {
4454         /* Load the vertex data using named arrays */
4455         TRACE("Loading vertex data\n");
4456         loadVertexData(stateblock, dataLocations);
4457         device->useDrawStridedSlow = FALSE;
4458         context->namedArraysLoaded = TRUE;
4459     } else {
4460         TRACE("Not loading vertex data\n");
4461         device->useDrawStridedSlow = TRUE;
4462     }
4463
4464 /* Generate some fixme's if unsupported functionality is being used */
4465 #define BUFFER_OR_DATA(_attribute) dataLocations->u.s._attribute.lpData
4466     /* TODO: Either support missing functionality in fixupVertices or by creating a shader to replace the pipeline. */
4467     if (!useVertexShaderFunction && (BUFFER_OR_DATA(position2) || BUFFER_OR_DATA(normal2))) {
4468         FIXME("Tweening is only valid with vertex shaders\n");
4469     }
4470     if (!useVertexShaderFunction && BUFFER_OR_DATA(binormal)) {
4471         FIXME("Binormal bump mapping is only valid with vertex shaders\n");
4472     }
4473     if (!useVertexShaderFunction && (BUFFER_OR_DATA(tessFactor) || BUFFER_OR_DATA(fog) || BUFFER_OR_DATA(depth) || BUFFER_OR_DATA(sample))) {
4474         FIXME("Extended attributes are only valid with vertex shaders\n");
4475     }
4476 #undef BUFFER_OR_DATA
4477 }
4478
4479 static void vertexdeclaration(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4480     BOOL useVertexShaderFunction = FALSE, updateFog = FALSE;
4481     BOOL usePixelShaderFunction = stateblock->wineD3DDevice->ps_selected_mode != SHADER_NONE && stateblock->pixelShader
4482             && ((IWineD3DPixelShaderImpl *)stateblock->pixelShader)->baseShader.function;
4483     BOOL transformed;
4484     /* Some stuff is in the device until we have per context tracking */
4485     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
4486     BOOL wasrhw = context->last_was_rhw;
4487
4488     /* Shaders can be implemented using ARB_PROGRAM, GLSL, or software -
4489      * here simply check whether a shader was set, or the user disabled shaders
4490      */
4491     if (use_vs(device)) {
4492         useVertexShaderFunction = TRUE;
4493
4494         if(((IWineD3DVertexShaderImpl *)stateblock->vertexShader)->baseShader.reg_maps.fog != context->last_was_foggy_shader) {
4495             updateFog = TRUE;
4496         }
4497     } else if(context->last_was_foggy_shader) {
4498         updateFog = TRUE;
4499     }
4500
4501     transformed = device->strided_streams.u.s.position_transformed;
4502     if (transformed) useVertexShaderFunction = FALSE;
4503
4504     if(transformed != context->last_was_rhw && !useVertexShaderFunction) {
4505         updateFog = TRUE;
4506     }
4507
4508     /* Reapply lighting if it is not scheduled for reapplication already */
4509     if(!isStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING))) {
4510         state_lighting(STATE_RENDER(WINED3DRS_LIGHTING), stateblock, context);
4511     }
4512
4513     if (transformed) {
4514         context->last_was_rhw = TRUE;
4515     } else {
4516
4517         /* Untransformed, so relies on the view and projection matrices */
4518         context->last_was_rhw = FALSE;
4519         /* This turns off the Z scale trick to 'disable' viewport frustum clipping in rhw mode*/
4520         device->untransformed = TRUE;
4521
4522         /* Todo for sw shaders: Vertex Shader output is already transformed, so set up identity matrices
4523          * Not needed as long as only hw shaders are supported
4524          */
4525
4526         /* This sets the shader output position correction constants.
4527          * TODO: Move to the viewport state
4528          */
4529         if (useVertexShaderFunction) {
4530             device->posFixup[1] = device->render_offscreen ? -1.0 : 1.0;
4531             device->posFixup[3] = -device->posFixup[1] / stateblock->viewport.Height;
4532         }
4533     }
4534
4535     /* Don't have to apply the matrices when vertex shaders are used. When vshaders are turned
4536      * off this function will be called again anyway to make sure they're properly set
4537      */
4538     if(!useVertexShaderFunction) {
4539         /* TODO: Move this mainly to the viewport state and only apply when the vp has changed
4540          * or transformed / untransformed was switched
4541          */
4542        if(wasrhw != context->last_was_rhw &&
4543           !isStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION)) &&
4544           !isStateDirty(context, STATE_VIEWPORT)) {
4545             transform_projection(STATE_TRANSFORM(WINED3DTS_PROJECTION), stateblock, context);
4546         }
4547         /* World matrix needs reapplication here only if we're switching between rhw and non-rhw
4548          * mode.
4549          *
4550          * If a vertex shader is used, the world matrix changed and then vertex shader unbound
4551          * this check will fail and the matrix not applied again. This is OK because a simple
4552          * world matrix change reapplies the matrix - These checks here are only to satisfy the
4553          * needs of the vertex declaration.
4554          *
4555          * World and view matrix go into the same gl matrix, so only apply them when neither is
4556          * dirty
4557          */
4558         if(transformed != wasrhw &&
4559            !isStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0))) &&
4560            !isStateDirty(context, STATE_TRANSFORM(WINED3DTS_VIEW))) {
4561             transform_world(STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), stateblock, context);
4562         }
4563
4564         if(!isStateDirty(context, STATE_RENDER(WINED3DRS_COLORVERTEX))) {
4565             state_colormat(STATE_RENDER(WINED3DRS_COLORVERTEX), stateblock, context);
4566         }
4567
4568         if(context->last_was_vshader && !isStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPLANEENABLE))) {
4569             state_clipping(STATE_RENDER(WINED3DRS_CLIPPLANEENABLE), stateblock, context);
4570         }
4571         if(!isStateDirty(context, STATE_RENDER(WINED3DRS_NORMALIZENORMALS))) {
4572             state_normalize(STATE_RENDER(WINED3DRS_NORMALIZENORMALS), stateblock, context);
4573         }
4574     } else {
4575         /* We compile the shader here because we need the vertex declaration
4576          * in order to determine if we need to do any swizzling for D3DCOLOR
4577          * registers. If the shader is already compiled this call will do nothing. */
4578         IWineD3DVertexShader_CompileShader(stateblock->vertexShader);
4579
4580         if(!context->last_was_vshader) {
4581             int i;
4582             static BOOL warned = FALSE;
4583             /* Disable all clip planes to get defined results on all drivers. See comment in the
4584              * state_clipping state handler
4585              */
4586             for(i = 0; i < GL_LIMITS(clipplanes); i++) {
4587                 glDisable(GL_CLIP_PLANE0 + i);
4588                 checkGLcall("glDisable(GL_CLIP_PLANE0 + i)");
4589             }
4590
4591             if(!warned && stateblock->renderState[WINED3DRS_CLIPPLANEENABLE]) {
4592                 FIXME("Clipping not supported with vertex shaders\n");
4593                 warned = TRUE;
4594             }
4595             if(wasrhw) {
4596                 /* Apply the transform matrices when switching from rhw drawing to vertex shaders. Vertex
4597                  * shaders themselves do not need it, but the matrices are not reapplied automatically when
4598                  * switching back from vertex shaders to fixed function processing. So make sure we leave the
4599                  * fixed function vertex processing states back in a sane state before switching to shaders
4600                  */
4601                 if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION))) {
4602                     transform_projection(STATE_TRANSFORM(WINED3DTS_PROJECTION), stateblock, context);
4603                 }
4604                 if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)))) {
4605                     transform_world(STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), stateblock, context);
4606                 }
4607             }
4608         }
4609     }
4610
4611     /* Vertex and pixel shaders are applied together for now, so let the last dirty state do the
4612      * application
4613      */
4614     if (!isStateDirty(context, STATE_PIXELSHADER)) {
4615         device->shader_backend->shader_select((IWineD3DDevice *)device, usePixelShaderFunction, useVertexShaderFunction);
4616
4617         if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (useVertexShaderFunction || usePixelShaderFunction)) {
4618             shaderconstant(STATE_VERTEXSHADERCONSTANT, stateblock, context);
4619         }
4620     }
4621
4622     context->last_was_vshader = useVertexShaderFunction;
4623
4624     if(updateFog) {
4625         state_fog(STATE_RENDER(WINED3DRS_FOGENABLE), stateblock, context);
4626     }
4627     if(!useVertexShaderFunction) {
4628         int i;
4629         for(i = 0; i < MAX_TEXTURES; i++) {
4630             if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + i))) {
4631                 transform_texture(STATE_TEXTURESTAGE(i, WINED3DTSS_TEXTURETRANSFORMFLAGS), stateblock, context);
4632             }
4633         }
4634     }
4635 }
4636
4637 static void viewport_miscpart(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4638     UINT width, height;
4639     IWineD3DSurfaceImpl *target;
4640
4641     glDepthRange(stateblock->viewport.MinZ, stateblock->viewport.MaxZ);
4642     checkGLcall("glDepthRange");
4643     /* Note: GL requires lower left, DirectX supplies upper left. This is reversed when using offscreen rendering
4644      */
4645     if(stateblock->wineD3DDevice->render_offscreen) {
4646         glViewport(stateblock->viewport.X,
4647                    stateblock->viewport.Y,
4648                    stateblock->viewport.Width, stateblock->viewport.Height);
4649     } else {
4650         target = (IWineD3DSurfaceImpl *) stateblock->wineD3DDevice->render_targets[0];
4651         target->get_drawable_size(target, &width, &height);
4652
4653         glViewport(stateblock->viewport.X,
4654                    (height - (stateblock->viewport.Y + stateblock->viewport.Height)),
4655                    stateblock->viewport.Width, stateblock->viewport.Height);
4656     }
4657
4658     checkGLcall("glViewport");
4659 }
4660
4661 static void viewport_vertexpart(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4662     stateblock->wineD3DDevice->posFixup[2] = 1.0 / stateblock->viewport.Width;
4663     stateblock->wineD3DDevice->posFixup[3] = -stateblock->wineD3DDevice->posFixup[1] / stateblock->viewport.Height;
4664     if(!isStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION))) {
4665         transform_projection(STATE_TRANSFORM(WINED3DTS_PROJECTION), stateblock, context);
4666     }
4667     if(!isStateDirty(context, STATE_RENDER(WINED3DRS_POINTSCALEENABLE))) {
4668         state_pscale(STATE_RENDER(WINED3DRS_POINTSCALEENABLE), stateblock, context);
4669     }
4670 }
4671
4672 static void light(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4673     UINT Index = state - STATE_ACTIVELIGHT(0);
4674     PLIGHTINFOEL *lightInfo = stateblock->activeLights[Index];
4675
4676     if(!lightInfo) {
4677         glDisable(GL_LIGHT0 + Index);
4678         checkGLcall("glDisable(GL_LIGHT0 + Index)");
4679     } else {
4680         float quad_att;
4681         float colRGBA[] = {0.0, 0.0, 0.0, 0.0};
4682
4683         /* Light settings are affected by the model view in OpenGL, the View transform in direct3d*/
4684         glMatrixMode(GL_MODELVIEW);
4685         glPushMatrix();
4686         glLoadMatrixf(&stateblock->transforms[WINED3DTS_VIEW].u.m[0][0]);
4687
4688         /* Diffuse: */
4689         colRGBA[0] = lightInfo->OriginalParms.Diffuse.r;
4690         colRGBA[1] = lightInfo->OriginalParms.Diffuse.g;
4691         colRGBA[2] = lightInfo->OriginalParms.Diffuse.b;
4692         colRGBA[3] = lightInfo->OriginalParms.Diffuse.a;
4693         glLightfv(GL_LIGHT0 + Index, GL_DIFFUSE, colRGBA);
4694         checkGLcall("glLightfv");
4695
4696         /* Specular */
4697         colRGBA[0] = lightInfo->OriginalParms.Specular.r;
4698         colRGBA[1] = lightInfo->OriginalParms.Specular.g;
4699         colRGBA[2] = lightInfo->OriginalParms.Specular.b;
4700         colRGBA[3] = lightInfo->OriginalParms.Specular.a;
4701         glLightfv(GL_LIGHT0 + Index, GL_SPECULAR, colRGBA);
4702         checkGLcall("glLightfv");
4703
4704         /* Ambient */
4705         colRGBA[0] = lightInfo->OriginalParms.Ambient.r;
4706         colRGBA[1] = lightInfo->OriginalParms.Ambient.g;
4707         colRGBA[2] = lightInfo->OriginalParms.Ambient.b;
4708         colRGBA[3] = lightInfo->OriginalParms.Ambient.a;
4709         glLightfv(GL_LIGHT0 + Index, GL_AMBIENT, colRGBA);
4710         checkGLcall("glLightfv");
4711
4712         if ((lightInfo->OriginalParms.Range *lightInfo->OriginalParms.Range) >= FLT_MIN) {
4713             quad_att = 1.4/(lightInfo->OriginalParms.Range *lightInfo->OriginalParms.Range);
4714         } else {
4715             quad_att = 0; /*  0 or  MAX?  (0 seems to be ok) */
4716         }
4717
4718         /* Do not assign attenuation values for lights that do not use them. D3D apps are free to pass any junk,
4719          * but gl drivers use them and may crash due to bad Attenuation values. Need for Speed most wanted sets
4720          * Attenuation0 to NaN and crashes in the gl lib
4721          */
4722
4723         switch (lightInfo->OriginalParms.Type) {
4724             case WINED3DLIGHT_POINT:
4725                 /* Position */
4726                 glLightfv(GL_LIGHT0 + Index, GL_POSITION, &lightInfo->lightPosn[0]);
4727                 checkGLcall("glLightfv");
4728                 glLightf(GL_LIGHT0 + Index, GL_SPOT_CUTOFF, lightInfo->cutoff);
4729                 checkGLcall("glLightf");
4730                 /* Attenuation - Are these right? guessing... */
4731                 glLightf(GL_LIGHT0 + Index, GL_CONSTANT_ATTENUATION,  lightInfo->OriginalParms.Attenuation0);
4732                 checkGLcall("glLightf");
4733                 glLightf(GL_LIGHT0 + Index, GL_LINEAR_ATTENUATION,    lightInfo->OriginalParms.Attenuation1);
4734                 checkGLcall("glLightf");
4735                 if (quad_att < lightInfo->OriginalParms.Attenuation2) quad_att = lightInfo->OriginalParms.Attenuation2;
4736                 glLightf(GL_LIGHT0 + Index, GL_QUADRATIC_ATTENUATION, quad_att);
4737                 checkGLcall("glLightf");
4738                 /* FIXME: Range */
4739                 break;
4740
4741             case WINED3DLIGHT_SPOT:
4742                 /* Position */
4743                 glLightfv(GL_LIGHT0 + Index, GL_POSITION, &lightInfo->lightPosn[0]);
4744                 checkGLcall("glLightfv");
4745                 /* Direction */
4746                 glLightfv(GL_LIGHT0 + Index, GL_SPOT_DIRECTION, &lightInfo->lightDirn[0]);
4747                 checkGLcall("glLightfv");
4748                 glLightf(GL_LIGHT0 + Index, GL_SPOT_EXPONENT, lightInfo->exponent);
4749                 checkGLcall("glLightf");
4750                 glLightf(GL_LIGHT0 + Index, GL_SPOT_CUTOFF, lightInfo->cutoff);
4751                 checkGLcall("glLightf");
4752                 /* Attenuation - Are these right? guessing... */
4753                 glLightf(GL_LIGHT0 + Index, GL_CONSTANT_ATTENUATION,  lightInfo->OriginalParms.Attenuation0);
4754                 checkGLcall("glLightf");
4755                 glLightf(GL_LIGHT0 + Index, GL_LINEAR_ATTENUATION,    lightInfo->OriginalParms.Attenuation1);
4756                 checkGLcall("glLightf");
4757                 if (quad_att < lightInfo->OriginalParms.Attenuation2) quad_att = lightInfo->OriginalParms.Attenuation2;
4758                 glLightf(GL_LIGHT0 + Index, GL_QUADRATIC_ATTENUATION, quad_att);
4759                 checkGLcall("glLightf");
4760                 /* FIXME: Range */
4761                 break;
4762
4763             case WINED3DLIGHT_DIRECTIONAL:
4764                 /* Direction */
4765                 glLightfv(GL_LIGHT0 + Index, GL_POSITION, &lightInfo->lightPosn[0]); /* Note gl uses w position of 0 for direction! */
4766                 checkGLcall("glLightfv");
4767                 glLightf(GL_LIGHT0 + Index, GL_SPOT_CUTOFF, lightInfo->cutoff);
4768                 checkGLcall("glLightf");
4769                 glLightf(GL_LIGHT0 + Index, GL_SPOT_EXPONENT, 0.0f);
4770                 checkGLcall("glLightf");
4771                 break;
4772
4773             default:
4774                 FIXME("Unrecognized light type %d\n", lightInfo->OriginalParms.Type);
4775         }
4776
4777         /* Restore the modelview matrix */
4778         glPopMatrix();
4779
4780         glEnable(GL_LIGHT0 + Index);
4781         checkGLcall("glEnable(GL_LIGHT0 + Index)");
4782     }
4783
4784     return;
4785 }
4786
4787 static void scissorrect(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4788     RECT *pRect = &stateblock->scissorRect;
4789     UINT height;
4790     UINT width;
4791     IWineD3DSurfaceImpl *target = (IWineD3DSurfaceImpl *) stateblock->wineD3DDevice->render_targets[0];
4792
4793     target->get_drawable_size(target, &width, &height);
4794     /* Warning: glScissor uses window coordinates, not viewport coordinates, so our viewport correction does not apply
4795      * Warning2: Even in windowed mode the coords are relative to the window, not the screen
4796      */
4797     TRACE("(%p) Setting new Scissor Rect to %d:%d-%d:%d\n", stateblock->wineD3DDevice, pRect->left, pRect->bottom - height,
4798           pRect->right - pRect->left, pRect->bottom - pRect->top);
4799
4800     if (stateblock->wineD3DDevice->render_offscreen) {
4801         glScissor(pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
4802     } else {
4803         glScissor(pRect->left, height - pRect->bottom, pRect->right - pRect->left, pRect->bottom - pRect->top);
4804     }
4805     checkGLcall("glScissor");
4806 }
4807
4808 static void indexbuffer(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4809     if(stateblock->streamIsUP || stateblock->pIndexData == NULL ) {
4810         GL_EXTCALL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
4811     } else {
4812         IWineD3DIndexBufferImpl *ib = (IWineD3DIndexBufferImpl *) stateblock->pIndexData;
4813         GL_EXTCALL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ib->vbo));
4814     }
4815 }
4816
4817 static void frontface(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
4818     if(stateblock->wineD3DDevice->render_offscreen) {
4819         glFrontFace(GL_CCW);
4820         checkGLcall("glFrontFace(GL_CCW)");
4821     } else {
4822         glFrontFace(GL_CW);
4823         checkGLcall("glFrontFace(GL_CW)");
4824     }
4825 }
4826
4827 const struct StateEntryTemplate misc_state_template[] = {
4828     { STATE_RENDER(WINED3DRS_SRCBLEND),                   { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4829     { STATE_RENDER(WINED3DRS_DESTBLEND),                  { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4830     { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4831     { STATE_RENDER(WINED3DRS_EDGEANTIALIAS),              { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4832     { STATE_RENDER(WINED3DRS_ANTIALIASEDLINEENABLE),      { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4833     { STATE_RENDER(WINED3DRS_SEPARATEALPHABLENDENABLE),   { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4834     { STATE_RENDER(WINED3DRS_SRCBLENDALPHA),              { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4835     { STATE_RENDER(WINED3DRS_DESTBLENDALPHA),             { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4836     { STATE_RENDER(WINED3DRS_DESTBLENDALPHA),             { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4837     { STATE_RENDER(WINED3DRS_BLENDOPALPHA),               { STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         }, 0                               },
4838     { STATE_STREAMSRC,                                    { STATE_VDECL,                                        streamsrc           }, 0                               },
4839     { STATE_VDECL,                                        { STATE_VDECL,                                        streamsrc           }, 0                               },
4840     { STATE_FRONTFACE,                                    { STATE_FRONTFACE,                                    frontface           }, 0                               },
4841     { STATE_SCISSORRECT,                                  { STATE_SCISSORRECT,                                  scissorrect         }, 0                               },
4842     /* TODO: Move shader constant loading to vertex and fragment pipeline repectively, as soon as the pshader and
4843      * vshader loadings are untied from each other
4844      */
4845     { STATE_VERTEXSHADERCONSTANT,                         { STATE_VERTEXSHADERCONSTANT,                         shaderconstant      }, 0                               },
4846     { STATE_PIXELSHADERCONSTANT,                          { STATE_VERTEXSHADERCONSTANT,                         shaderconstant      }, 0                               },
4847     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4848     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4849     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4850     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4851     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4852     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4853     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4854     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4855     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4856     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4857     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4858     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4859     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4860     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4861     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4862     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4863     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4864     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4865     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4866     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4867     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4868     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4869     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4870     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4871     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4872     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4873     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4874     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4875     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4876     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4877     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4878     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     shader_bumpenvmat   }, 0                               },
4879     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4880     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4881     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4882     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4883     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4884     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4885     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4886     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4887     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4888     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4889     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4890     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4891     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4892     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4893     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE),    { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4894     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET),   { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlscale   }, 0                               },
4895
4896     { STATE_VIEWPORT,                                     { STATE_VIEWPORT,                                     viewport_miscpart   }, 0                               },
4897     { STATE_INDEXBUFFER,                                  { STATE_INDEXBUFFER,                                  indexbuffer         }, ARB_VERTEX_BUFFER_OBJECT        },
4898     { STATE_RENDER(WINED3DRS_ANTIALIAS),                  { STATE_RENDER(WINED3DRS_ANTIALIAS),                  state_antialias     }, 0                               },
4899     { STATE_RENDER(WINED3DRS_TEXTUREPERSPECTIVE),         { STATE_RENDER(WINED3DRS_TEXTUREPERSPECTIVE),         state_perspective   }, 0                               },
4900     { STATE_RENDER(WINED3DRS_ZENABLE),                    { STATE_RENDER(WINED3DRS_ZENABLE),                    state_zenable       }, 0                               },
4901     { STATE_RENDER(WINED3DRS_WRAPU),                      { STATE_RENDER(WINED3DRS_WRAPU),                      state_wrapu         }, 0                               },
4902     { STATE_RENDER(WINED3DRS_WRAPV),                      { STATE_RENDER(WINED3DRS_WRAPV),                      state_wrapv         }, 0                               },
4903     { STATE_RENDER(WINED3DRS_FILLMODE),                   { STATE_RENDER(WINED3DRS_FILLMODE),                   state_fillmode      }, 0                               },
4904     { STATE_RENDER(WINED3DRS_SHADEMODE),                  { STATE_RENDER(WINED3DRS_SHADEMODE),                  state_shademode     }, 0                               },
4905     { STATE_RENDER(WINED3DRS_LINEPATTERN),                { STATE_RENDER(WINED3DRS_LINEPATTERN),                state_linepattern   }, 0                               },
4906     { STATE_RENDER(WINED3DRS_MONOENABLE),                 { STATE_RENDER(WINED3DRS_MONOENABLE),                 state_monoenable    }, 0                               },
4907     { STATE_RENDER(WINED3DRS_ROP2),                       { STATE_RENDER(WINED3DRS_ROP2),                       state_rop2          }, 0                               },
4908     { STATE_RENDER(WINED3DRS_PLANEMASK),                  { STATE_RENDER(WINED3DRS_PLANEMASK),                  state_planemask     }, 0                               },
4909     { STATE_RENDER(WINED3DRS_ZWRITEENABLE),               { STATE_RENDER(WINED3DRS_ZWRITEENABLE),               state_zwritenable   }, 0                               },
4910     { STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            { STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         }, 0                               },
4911     { STATE_RENDER(WINED3DRS_ALPHAREF),                   { STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         }, 0                               },
4912     { STATE_RENDER(WINED3DRS_ALPHAFUNC),                  { STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         }, 0                               },
4913     { STATE_RENDER(WINED3DRS_COLORKEYENABLE),             { STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         }, 0                               },
4914     { STATE_RENDER(WINED3DRS_LASTPIXEL),                  { STATE_RENDER(WINED3DRS_LASTPIXEL),                  state_lastpixel     }, 0                               },
4915     { STATE_RENDER(WINED3DRS_CULLMODE),                   { STATE_RENDER(WINED3DRS_CULLMODE),                   state_cullmode      }, 0                               },
4916     { STATE_RENDER(WINED3DRS_ZFUNC),                      { STATE_RENDER(WINED3DRS_ZFUNC),                      state_zfunc         }, 0                               },
4917     { STATE_RENDER(WINED3DRS_DITHERENABLE),               { STATE_RENDER(WINED3DRS_DITHERENABLE),               state_ditherenable  }, 0                               },
4918     { STATE_RENDER(WINED3DRS_SUBPIXEL),                   { STATE_RENDER(WINED3DRS_SUBPIXEL),                   state_subpixel      }, 0                               },
4919     { STATE_RENDER(WINED3DRS_SUBPIXELX),                  { STATE_RENDER(WINED3DRS_SUBPIXELX),                  state_subpixelx     }, 0                               },
4920     { STATE_RENDER(WINED3DRS_STIPPLEDALPHA),              { STATE_RENDER(WINED3DRS_STIPPLEDALPHA),              state_stippledalpha }, 0                               },
4921     { STATE_RENDER(WINED3DRS_ZBIAS),                      { STATE_RENDER(WINED3DRS_ZBIAS),                      state_zbias         }, 0                               },
4922     { STATE_RENDER(WINED3DRS_STIPPLEENABLE),              { STATE_RENDER(WINED3DRS_STIPPLEENABLE),              state_stippleenable }, 0                               },
4923     { STATE_RENDER(WINED3DRS_MIPMAPLODBIAS),              { STATE_RENDER(WINED3DRS_MIPMAPLODBIAS),              state_mipmaplodbias }, 0                               },
4924     { STATE_RENDER(WINED3DRS_ANISOTROPY),                 { STATE_RENDER(WINED3DRS_ANISOTROPY),                 state_anisotropy    }, 0                               },
4925     { STATE_RENDER(WINED3DRS_FLUSHBATCH),                 { STATE_RENDER(WINED3DRS_FLUSHBATCH),                 state_flushbatch    }, 0                               },
4926     { STATE_RENDER(WINED3DRS_TRANSLUCENTSORTINDEPENDENT), { STATE_RENDER(WINED3DRS_TRANSLUCENTSORTINDEPENDENT), state_translucentsi }, 0                               },
4927     { STATE_RENDER(WINED3DRS_STENCILENABLE),              { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4928     { STATE_RENDER(WINED3DRS_STENCILFAIL),                { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4929     { STATE_RENDER(WINED3DRS_STENCILZFAIL),               { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4930     { STATE_RENDER(WINED3DRS_STENCILPASS),                { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4931     { STATE_RENDER(WINED3DRS_STENCILFUNC),                { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4932     { STATE_RENDER(WINED3DRS_STENCILREF),                 { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4933     { STATE_RENDER(WINED3DRS_STENCILMASK),                { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4934     { STATE_RENDER(WINED3DRS_STENCILWRITEMASK),           { STATE_RENDER(WINED3DRS_STENCILWRITEMASK),           state_stencilwrite2s}, EXT_STENCIL_TWO_SIDE            },
4935     { STATE_RENDER(WINED3DRS_STENCILWRITEMASK),           { STATE_RENDER(WINED3DRS_STENCILWRITEMASK),           state_stencilwrite  }, 0                               },
4936     { STATE_RENDER(WINED3DRS_TWOSIDEDSTENCILMODE),        { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4937     { STATE_RENDER(WINED3DRS_CCW_STENCILFAIL),            { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4938     { STATE_RENDER(WINED3DRS_CCW_STENCILZFAIL),           { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4939     { STATE_RENDER(WINED3DRS_CCW_STENCILPASS),            { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4940     { STATE_RENDER(WINED3DRS_CCW_STENCILFUNC),            { STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       }, 0                               },
4941     { STATE_RENDER(WINED3DRS_WRAP0),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4942     { STATE_RENDER(WINED3DRS_WRAP1),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4943     { STATE_RENDER(WINED3DRS_WRAP2),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4944     { STATE_RENDER(WINED3DRS_WRAP3),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4945     { STATE_RENDER(WINED3DRS_WRAP4),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4946     { STATE_RENDER(WINED3DRS_WRAP5),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4947     { STATE_RENDER(WINED3DRS_WRAP6),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4948     { STATE_RENDER(WINED3DRS_WRAP7),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4949     { STATE_RENDER(WINED3DRS_WRAP8),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4950     { STATE_RENDER(WINED3DRS_WRAP9),                      { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4951     { STATE_RENDER(WINED3DRS_WRAP10),                     { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4952     { STATE_RENDER(WINED3DRS_WRAP11),                     { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4953     { STATE_RENDER(WINED3DRS_WRAP12),                     { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4954     { STATE_RENDER(WINED3DRS_WRAP13),                     { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4955     { STATE_RENDER(WINED3DRS_WRAP14),                     { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4956     { STATE_RENDER(WINED3DRS_WRAP15),                     { STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          }, 0                               },
4957     { STATE_RENDER(WINED3DRS_EXTENTS),                    { STATE_RENDER(WINED3DRS_EXTENTS),                    state_extents       }, 0                               },
4958     { STATE_RENDER(WINED3DRS_COLORKEYBLENDENABLE),        { STATE_RENDER(WINED3DRS_COLORKEYBLENDENABLE),        state_ckeyblend     }, 0                               },
4959     { STATE_RENDER(WINED3DRS_PATCHEDGESTYLE),             { STATE_RENDER(WINED3DRS_PATCHEDGESTYLE),             state_patchedgestyle}, 0                               },
4960     { STATE_RENDER(WINED3DRS_PATCHSEGMENTS),              { STATE_RENDER(WINED3DRS_PATCHSEGMENTS),              state_patchsegments }, 0                               },
4961     { STATE_RENDER(WINED3DRS_POSITIONDEGREE),             { STATE_RENDER(WINED3DRS_POSITIONDEGREE),             state_positiondegree}, 0                               },
4962     { STATE_RENDER(WINED3DRS_NORMALDEGREE),               { STATE_RENDER(WINED3DRS_NORMALDEGREE),               state_normaldegree  }, 0                               },
4963     { STATE_RENDER(WINED3DRS_MINTESSELLATIONLEVEL),       { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4964     { STATE_RENDER(WINED3DRS_MAXTESSELLATIONLEVEL),       { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4965     { STATE_RENDER(WINED3DRS_ADAPTIVETESS_X),             { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4966     { STATE_RENDER(WINED3DRS_ADAPTIVETESS_Y),             { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4967     { STATE_RENDER(WINED3DRS_ADAPTIVETESS_Z),             { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4968     { STATE_RENDER(WINED3DRS_ADAPTIVETESS_W),             { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4969     { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), { STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  }, 0                               },
4970     { STATE_RENDER(WINED3DRS_MULTISAMPLEANTIALIAS),       { STATE_RENDER(WINED3DRS_MULTISAMPLEANTIALIAS),       state_msaa          }, ARB_MULTISAMPLE                 },
4971     { STATE_RENDER(WINED3DRS_MULTISAMPLEANTIALIAS),       { STATE_RENDER(WINED3DRS_MULTISAMPLEANTIALIAS),       state_msaa_w        }, 0                               },
4972     { STATE_RENDER(WINED3DRS_MULTISAMPLEMASK),            { STATE_RENDER(WINED3DRS_MULTISAMPLEMASK),            state_multisampmask }, 0                               },
4973     { STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           { STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    }, 0                               },
4974     { STATE_RENDER(WINED3DRS_BLENDOP),                    { STATE_RENDER(WINED3DRS_BLENDOP),                    state_blendop       }, EXT_BLEND_MINMAX                },
4975     { STATE_RENDER(WINED3DRS_BLENDOP),                    { STATE_RENDER(WINED3DRS_BLENDOP),                    state_blendop_w     }, 0                               },
4976     { STATE_RENDER(WINED3DRS_SCISSORTESTENABLE),          { STATE_RENDER(WINED3DRS_SCISSORTESTENABLE),          state_scissor       }, 0                               },
4977     { STATE_RENDER(WINED3DRS_SLOPESCALEDEPTHBIAS),        { STATE_RENDER(WINED3DRS_DEPTHBIAS),                  state_depthbias     }, 0                               },
4978     { STATE_RENDER(WINED3DRS_COLORWRITEENABLE1),          { STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    }, 0                               },
4979     { STATE_RENDER(WINED3DRS_COLORWRITEENABLE2),          { STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    }, 0                               },
4980     { STATE_RENDER(WINED3DRS_COLORWRITEENABLE3),          { STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    }, 0                               },
4981     { STATE_RENDER(WINED3DRS_BLENDFACTOR),                { STATE_RENDER(WINED3DRS_BLENDFACTOR),                state_blendfactor   }, EXT_BLEND_COLOR                 },
4982     { STATE_RENDER(WINED3DRS_BLENDFACTOR),                { STATE_RENDER(WINED3DRS_BLENDFACTOR),                state_blendfactor_w }, 0                               },
4983     { STATE_RENDER(WINED3DRS_DEPTHBIAS),                  { STATE_RENDER(WINED3DRS_DEPTHBIAS),                  state_depthbias     }, 0                               },
4984     /* Samplers */
4985     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler             }, 0                               },
4986     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler             }, 0                               },
4987     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler             }, 0                               },
4988     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler             }, 0                               },
4989     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler             }, 0                               },
4990     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler             }, 0                               },
4991     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler             }, 0                               },
4992     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler             }, 0                               },
4993     { STATE_SAMPLER(8),                                   { STATE_SAMPLER(8),                                   sampler             }, 0                               },
4994     { STATE_SAMPLER(9),                                   { STATE_SAMPLER(9),                                   sampler             }, 0                               },
4995     { STATE_SAMPLER(10),                                  { STATE_SAMPLER(10),                                  sampler             }, 0                               },
4996     { STATE_SAMPLER(11),                                  { STATE_SAMPLER(11),                                  sampler             }, 0                               },
4997     { STATE_SAMPLER(12),                                  { STATE_SAMPLER(12),                                  sampler             }, 0                               },
4998     { STATE_SAMPLER(13),                                  { STATE_SAMPLER(13),                                  sampler             }, 0                               },
4999     { STATE_SAMPLER(14),                                  { STATE_SAMPLER(14),                                  sampler             }, 0                               },
5000     { STATE_SAMPLER(15),                                  { STATE_SAMPLER(15),                                  sampler             }, 0                               },
5001     { STATE_SAMPLER(16), /* Vertex sampler 0 */           { STATE_SAMPLER(16),                                  sampler             }, 0                               },
5002     { STATE_SAMPLER(17), /* Vertex sampler 1 */           { STATE_SAMPLER(17),                                  sampler             }, 0                               },
5003     { STATE_SAMPLER(18), /* Vertex sampler 2 */           { STATE_SAMPLER(18),                                  sampler             }, 0                               },
5004     { STATE_SAMPLER(19), /* Vertex sampler 3 */           { STATE_SAMPLER(19),                                  sampler             }, 0                               },
5005     {0 /* Terminate */,                                   { 0,                                                  0                   }, 0                               },
5006 };
5007
5008 const struct StateEntryTemplate ffp_vertexstate_template[] = {
5009     { STATE_VDECL,                                        { STATE_VDECL,                                        vertexdeclaration   }, 0                               },
5010     { STATE_VSHADER,                                      { STATE_VDECL,                                        vertexdeclaration   }, 0                               },
5011     { STATE_MATERIAL,                                     { STATE_RENDER(WINED3DRS_SPECULARENABLE),             state_specularenable}, 0                               },
5012     { STATE_RENDER(WINED3DRS_SPECULARENABLE),             { STATE_RENDER(WINED3DRS_SPECULARENABLE),             state_specularenable}, 0                               },
5013       /* Clip planes */
5014     { STATE_CLIPPLANE(0),                                 { STATE_CLIPPLANE(0),                                 clipplane           }, 0                               },
5015     { STATE_CLIPPLANE(1),                                 { STATE_CLIPPLANE(1),                                 clipplane           }, 0                               },
5016     { STATE_CLIPPLANE(2),                                 { STATE_CLIPPLANE(2),                                 clipplane           }, 0                               },
5017     { STATE_CLIPPLANE(3),                                 { STATE_CLIPPLANE(3),                                 clipplane           }, 0                               },
5018     { STATE_CLIPPLANE(4),                                 { STATE_CLIPPLANE(4),                                 clipplane           }, 0                               },
5019     { STATE_CLIPPLANE(5),                                 { STATE_CLIPPLANE(5),                                 clipplane           }, 0                               },
5020     { STATE_CLIPPLANE(6),                                 { STATE_CLIPPLANE(6),                                 clipplane           }, 0                               },
5021     { STATE_CLIPPLANE(7),                                 { STATE_CLIPPLANE(7),                                 clipplane           }, 0                               },
5022     { STATE_CLIPPLANE(8),                                 { STATE_CLIPPLANE(8),                                 clipplane           }, 0                               },
5023     { STATE_CLIPPLANE(9),                                 { STATE_CLIPPLANE(9),                                 clipplane           }, 0                               },
5024     { STATE_CLIPPLANE(10),                                { STATE_CLIPPLANE(10),                                clipplane           }, 0                               },
5025     { STATE_CLIPPLANE(11),                                { STATE_CLIPPLANE(11),                                clipplane           }, 0                               },
5026     { STATE_CLIPPLANE(12),                                { STATE_CLIPPLANE(12),                                clipplane           }, 0                               },
5027     { STATE_CLIPPLANE(13),                                { STATE_CLIPPLANE(13),                                clipplane           }, 0                               },
5028     { STATE_CLIPPLANE(14),                                { STATE_CLIPPLANE(14),                                clipplane           }, 0                               },
5029     { STATE_CLIPPLANE(15),                                { STATE_CLIPPLANE(15),                                clipplane           }, 0                               },
5030     { STATE_CLIPPLANE(16),                                { STATE_CLIPPLANE(16),                                clipplane           }, 0                               },
5031     { STATE_CLIPPLANE(17),                                { STATE_CLIPPLANE(17),                                clipplane           }, 0                               },
5032     { STATE_CLIPPLANE(18),                                { STATE_CLIPPLANE(18),                                clipplane           }, 0                               },
5033     { STATE_CLIPPLANE(19),                                { STATE_CLIPPLANE(19),                                clipplane           }, 0                               },
5034     { STATE_CLIPPLANE(20),                                { STATE_CLIPPLANE(20),                                clipplane           }, 0                               },
5035     { STATE_CLIPPLANE(21),                                { STATE_CLIPPLANE(21),                                clipplane           }, 0                               },
5036     { STATE_CLIPPLANE(22),                                { STATE_CLIPPLANE(22),                                clipplane           }, 0                               },
5037     { STATE_CLIPPLANE(23),                                { STATE_CLIPPLANE(23),                                clipplane           }, 0                               },
5038     { STATE_CLIPPLANE(24),                                { STATE_CLIPPLANE(24),                                clipplane           }, 0                               },
5039     { STATE_CLIPPLANE(25),                                { STATE_CLIPPLANE(25),                                clipplane           }, 0                               },
5040     { STATE_CLIPPLANE(26),                                { STATE_CLIPPLANE(26),                                clipplane           }, 0                               },
5041     { STATE_CLIPPLANE(27),                                { STATE_CLIPPLANE(27),                                clipplane           }, 0                               },
5042     { STATE_CLIPPLANE(28),                                { STATE_CLIPPLANE(28),                                clipplane           }, 0                               },
5043     { STATE_CLIPPLANE(29),                                { STATE_CLIPPLANE(29),                                clipplane           }, 0                               },
5044     { STATE_CLIPPLANE(30),                                { STATE_CLIPPLANE(30),                                clipplane           }, 0                               },
5045     { STATE_CLIPPLANE(31),                                { STATE_CLIPPLANE(31),                                clipplane           }, 0                               },
5046       /* Lights */
5047     { STATE_ACTIVELIGHT(0),                               { STATE_ACTIVELIGHT(0),                               light               }, 0                               },
5048     { STATE_ACTIVELIGHT(1),                               { STATE_ACTIVELIGHT(1),                               light               }, 0                               },
5049     { STATE_ACTIVELIGHT(2),                               { STATE_ACTIVELIGHT(2),                               light               }, 0                               },
5050     { STATE_ACTIVELIGHT(3),                               { STATE_ACTIVELIGHT(3),                               light               }, 0                               },
5051     { STATE_ACTIVELIGHT(4),                               { STATE_ACTIVELIGHT(4),                               light               }, 0                               },
5052     { STATE_ACTIVELIGHT(5),                               { STATE_ACTIVELIGHT(5),                               light               }, 0                               },
5053     { STATE_ACTIVELIGHT(6),                               { STATE_ACTIVELIGHT(6),                               light               }, 0                               },
5054     { STATE_ACTIVELIGHT(7),                               { STATE_ACTIVELIGHT(7),                               light               }, 0                               },
5055     /* Viewport */
5056     { STATE_VIEWPORT,                                     { STATE_VIEWPORT,                                     viewport_vertexpart }, 0                               },
5057       /* Transform states follow                    */
5058     { STATE_TRANSFORM(WINED3DTS_VIEW),                    { STATE_TRANSFORM(WINED3DTS_VIEW),                    transform_view      }, 0                               },
5059     { STATE_TRANSFORM(WINED3DTS_PROJECTION),              { STATE_TRANSFORM(WINED3DTS_PROJECTION),              transform_projection}, 0                               },
5060     { STATE_TRANSFORM(WINED3DTS_TEXTURE0),                { STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5061     { STATE_TRANSFORM(WINED3DTS_TEXTURE1),                { STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5062     { STATE_TRANSFORM(WINED3DTS_TEXTURE2),                { STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5063     { STATE_TRANSFORM(WINED3DTS_TEXTURE3),                { STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5064     { STATE_TRANSFORM(WINED3DTS_TEXTURE4),                { STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5065     { STATE_TRANSFORM(WINED3DTS_TEXTURE5),                { STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5066     { STATE_TRANSFORM(WINED3DTS_TEXTURE6),                { STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5067     { STATE_TRANSFORM(WINED3DTS_TEXTURE7),                { STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),  transform_texture   }, 0                               },
5068     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  0)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  0)),        transform_world     }, 0                               },
5069     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  1)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  1)),        transform_worldex   }, 0                               },
5070     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  2)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  2)),        transform_worldex   }, 0                               },
5071     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  3)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  3)),        transform_worldex   }, 0                               },
5072     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  4)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  4)),        transform_worldex   }, 0                               },
5073     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  5)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  5)),        transform_worldex   }, 0                               },
5074     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  6)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  6)),        transform_worldex   }, 0                               },
5075     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  7)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  7)),        transform_worldex   }, 0                               },
5076     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  8)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  8)),        transform_worldex   }, 0                               },
5077     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  9)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(  9)),        transform_worldex   }, 0                               },
5078     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 10)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 10)),        transform_worldex   }, 0                               },
5079     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 11)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 11)),        transform_worldex   }, 0                               },
5080     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 12)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 12)),        transform_worldex   }, 0                               },
5081     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 13)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 13)),        transform_worldex   }, 0                               },
5082     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 14)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 14)),        transform_worldex   }, 0                               },
5083     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 15)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 15)),        transform_worldex   }, 0                               },
5084     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 16)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 16)),        transform_worldex   }, 0                               },
5085     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 17)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 17)),        transform_worldex   }, 0                               },
5086     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 18)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 18)),        transform_worldex   }, 0                               },
5087     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 19)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 19)),        transform_worldex   }, 0                               },
5088     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 20)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 20)),        transform_worldex   }, 0                               },
5089     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 21)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 21)),        transform_worldex   }, 0                               },
5090     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 22)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 22)),        transform_worldex   }, 0                               },
5091     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 23)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 23)),        transform_worldex   }, 0                               },
5092     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 24)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 24)),        transform_worldex   }, 0                               },
5093     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 25)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 25)),        transform_worldex   }, 0                               },
5094     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 26)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 26)),        transform_worldex   }, 0                               },
5095     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 27)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 27)),        transform_worldex   }, 0                               },
5096     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 28)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 28)),        transform_worldex   }, 0                               },
5097     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 29)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 29)),        transform_worldex   }, 0                               },
5098     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 30)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 30)),        transform_worldex   }, 0                               },
5099     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 31)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 31)),        transform_worldex   }, 0                               },
5100     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 32)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 32)),        transform_worldex   }, 0                               },
5101     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 33)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 33)),        transform_worldex   }, 0                               },
5102     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 34)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 34)),        transform_worldex   }, 0                               },
5103     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 35)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 35)),        transform_worldex   }, 0                               },
5104     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 36)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 36)),        transform_worldex   }, 0                               },
5105     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 37)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 37)),        transform_worldex   }, 0                               },
5106     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 38)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 38)),        transform_worldex   }, 0                               },
5107     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 39)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 39)),        transform_worldex   }, 0                               },
5108     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 40)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 40)),        transform_worldex   }, 0                               },
5109     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 41)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 41)),        transform_worldex   }, 0                               },
5110     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 42)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 42)),        transform_worldex   }, 0                               },
5111     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 43)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 43)),        transform_worldex   }, 0                               },
5112     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 44)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 44)),        transform_worldex   }, 0                               },
5113     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 45)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 45)),        transform_worldex   }, 0                               },
5114     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 46)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 46)),        transform_worldex   }, 0                               },
5115     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 47)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 47)),        transform_worldex   }, 0                               },
5116     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 48)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 48)),        transform_worldex   }, 0                               },
5117     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 49)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 49)),        transform_worldex   }, 0                               },
5118     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 50)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 50)),        transform_worldex   }, 0                               },
5119     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 51)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 51)),        transform_worldex   }, 0                               },
5120     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 52)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 52)),        transform_worldex   }, 0                               },
5121     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 53)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 53)),        transform_worldex   }, 0                               },
5122     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 54)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 54)),        transform_worldex   }, 0                               },
5123     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 55)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 55)),        transform_worldex   }, 0                               },
5124     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 56)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 56)),        transform_worldex   }, 0                               },
5125     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 57)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 57)),        transform_worldex   }, 0                               },
5126     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 58)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 58)),        transform_worldex   }, 0                               },
5127     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 59)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 59)),        transform_worldex   }, 0                               },
5128     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 60)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 60)),        transform_worldex   }, 0                               },
5129     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 61)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 61)),        transform_worldex   }, 0                               },
5130     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 62)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 62)),        transform_worldex   }, 0                               },
5131     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 63)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 63)),        transform_worldex   }, 0                               },
5132     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 64)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 64)),        transform_worldex   }, 0                               },
5133     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 65)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 65)),        transform_worldex   }, 0                               },
5134     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 66)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 66)),        transform_worldex   }, 0                               },
5135     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 67)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 67)),        transform_worldex   }, 0                               },
5136     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 68)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 68)),        transform_worldex   }, 0                               },
5137     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 69)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 69)),        transform_worldex   }, 0                               },
5138     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 70)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 70)),        transform_worldex   }, 0                               },
5139     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 71)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 71)),        transform_worldex   }, 0                               },
5140     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 72)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 72)),        transform_worldex   }, 0                               },
5141     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 73)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 73)),        transform_worldex   }, 0                               },
5142     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 74)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 74)),        transform_worldex   }, 0                               },
5143     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 75)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 75)),        transform_worldex   }, 0                               },
5144     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 76)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 76)),        transform_worldex   }, 0                               },
5145     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 77)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 77)),        transform_worldex   }, 0                               },
5146     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 78)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 78)),        transform_worldex   }, 0                               },
5147     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 79)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 79)),        transform_worldex   }, 0                               },
5148     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 80)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 80)),        transform_worldex   }, 0                               },
5149     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 81)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 81)),        transform_worldex   }, 0                               },
5150     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 82)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 82)),        transform_worldex   }, 0                               },
5151     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 83)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 83)),        transform_worldex   }, 0                               },
5152     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 84)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 84)),        transform_worldex   }, 0                               },
5153     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 85)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 85)),        transform_worldex   }, 0                               },
5154     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 86)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 86)),        transform_worldex   }, 0                               },
5155     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 87)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 87)),        transform_worldex   }, 0                               },
5156     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 88)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 88)),        transform_worldex   }, 0                               },
5157     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 89)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 89)),        transform_worldex   }, 0                               },
5158     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 90)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 90)),        transform_worldex   }, 0                               },
5159     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 91)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 91)),        transform_worldex   }, 0                               },
5160     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 92)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 92)),        transform_worldex   }, 0                               },
5161     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 93)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 93)),        transform_worldex   }, 0                               },
5162     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 94)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 94)),        transform_worldex   }, 0                               },
5163     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 95)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 95)),        transform_worldex   }, 0                               },
5164     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 96)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 96)),        transform_worldex   }, 0                               },
5165     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 97)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 97)),        transform_worldex   }, 0                               },
5166     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 98)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 98)),        transform_worldex   }, 0                               },
5167     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 99)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX( 99)),        transform_worldex   }, 0                               },
5168     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(100)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(100)),        transform_worldex   }, 0                               },
5169     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(101)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(101)),        transform_worldex   }, 0                               },
5170     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(102)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(102)),        transform_worldex   }, 0                               },
5171     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(103)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(103)),        transform_worldex   }, 0                               },
5172     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(104)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(104)),        transform_worldex   }, 0                               },
5173     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(105)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(105)),        transform_worldex   }, 0                               },
5174     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(106)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(106)),        transform_worldex   }, 0                               },
5175     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(107)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(107)),        transform_worldex   }, 0                               },
5176     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(108)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(108)),        transform_worldex   }, 0                               },
5177     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(109)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(109)),        transform_worldex   }, 0                               },
5178     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(110)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(110)),        transform_worldex   }, 0                               },
5179     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(111)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(111)),        transform_worldex   }, 0                               },
5180     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(112)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(112)),        transform_worldex   }, 0                               },
5181     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(113)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(113)),        transform_worldex   }, 0                               },
5182     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(114)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(114)),        transform_worldex   }, 0                               },
5183     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(115)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(115)),        transform_worldex   }, 0                               },
5184     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(116)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(116)),        transform_worldex   }, 0                               },
5185     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(117)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(117)),        transform_worldex   }, 0                               },
5186     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(118)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(118)),        transform_worldex   }, 0                               },
5187     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(119)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(119)),        transform_worldex   }, 0                               },
5188     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(120)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(120)),        transform_worldex   }, 0                               },
5189     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(121)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(121)),        transform_worldex   }, 0                               },
5190     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(122)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(122)),        transform_worldex   }, 0                               },
5191     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(123)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(123)),        transform_worldex   }, 0                               },
5192     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(124)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(124)),        transform_worldex   }, 0                               },
5193     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(125)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(125)),        transform_worldex   }, 0                               },
5194     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(126)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(126)),        transform_worldex   }, 0                               },
5195     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(127)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(127)),        transform_worldex   }, 0                               },
5196     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(128)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(128)),        transform_worldex   }, 0                               },
5197     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(129)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(129)),        transform_worldex   }, 0                               },
5198     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(130)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(130)),        transform_worldex   }, 0                               },
5199     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(131)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(131)),        transform_worldex   }, 0                               },
5200     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(132)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(132)),        transform_worldex   }, 0                               },
5201     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(133)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(133)),        transform_worldex   }, 0                               },
5202     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(134)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(134)),        transform_worldex   }, 0                               },
5203     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(135)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(135)),        transform_worldex   }, 0                               },
5204     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(136)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(136)),        transform_worldex   }, 0                               },
5205     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(137)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(137)),        transform_worldex   }, 0                               },
5206     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(138)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(138)),        transform_worldex   }, 0                               },
5207     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(139)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(139)),        transform_worldex   }, 0                               },
5208     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(140)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(140)),        transform_worldex   }, 0                               },
5209     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(141)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(141)),        transform_worldex   }, 0                               },
5210     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(142)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(142)),        transform_worldex   }, 0                               },
5211     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(143)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(143)),        transform_worldex   }, 0                               },
5212     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(144)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(144)),        transform_worldex   }, 0                               },
5213     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(145)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(145)),        transform_worldex   }, 0                               },
5214     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(146)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(146)),        transform_worldex   }, 0                               },
5215     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(147)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(147)),        transform_worldex   }, 0                               },
5216     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(148)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(148)),        transform_worldex   }, 0                               },
5217     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(149)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(149)),        transform_worldex   }, 0                               },
5218     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(150)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(150)),        transform_worldex   }, 0                               },
5219     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(151)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(151)),        transform_worldex   }, 0                               },
5220     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(152)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(152)),        transform_worldex   }, 0                               },
5221     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(153)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(153)),        transform_worldex   }, 0                               },
5222     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(154)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(154)),        transform_worldex   }, 0                               },
5223     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(155)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(155)),        transform_worldex   }, 0                               },
5224     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(156)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(156)),        transform_worldex   }, 0                               },
5225     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(157)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(157)),        transform_worldex   }, 0                               },
5226     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(158)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(158)),        transform_worldex   }, 0                               },
5227     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(159)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(159)),        transform_worldex   }, 0                               },
5228     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(160)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(160)),        transform_worldex   }, 0                               },
5229     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(161)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(161)),        transform_worldex   }, 0                               },
5230     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(162)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(162)),        transform_worldex   }, 0                               },
5231     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(163)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(163)),        transform_worldex   }, 0                               },
5232     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(164)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(164)),        transform_worldex   }, 0                               },
5233     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(165)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(165)),        transform_worldex   }, 0                               },
5234     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(166)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(166)),        transform_worldex   }, 0                               },
5235     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(167)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(167)),        transform_worldex   }, 0                               },
5236     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(168)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(168)),        transform_worldex   }, 0                               },
5237     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(169)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(169)),        transform_worldex   }, 0                               },
5238     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(170)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(170)),        transform_worldex   }, 0                               },
5239     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(171)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(171)),        transform_worldex   }, 0                               },
5240     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(172)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(172)),        transform_worldex   }, 0                               },
5241     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(173)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(173)),        transform_worldex   }, 0                               },
5242     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(174)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(174)),        transform_worldex   }, 0                               },
5243     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(175)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(175)),        transform_worldex   }, 0                               },
5244     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(176)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(176)),        transform_worldex   }, 0                               },
5245     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(177)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(177)),        transform_worldex   }, 0                               },
5246     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(178)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(178)),        transform_worldex   }, 0                               },
5247     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(179)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(179)),        transform_worldex   }, 0                               },
5248     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(180)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(180)),        transform_worldex   }, 0                               },
5249     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(181)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(181)),        transform_worldex   }, 0                               },
5250     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(182)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(182)),        transform_worldex   }, 0                               },
5251     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(183)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(183)),        transform_worldex   }, 0                               },
5252     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(184)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(184)),        transform_worldex   }, 0                               },
5253     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(185)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(185)),        transform_worldex   }, 0                               },
5254     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(186)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(186)),        transform_worldex   }, 0                               },
5255     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(187)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(187)),        transform_worldex   }, 0                               },
5256     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(188)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(188)),        transform_worldex   }, 0                               },
5257     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(189)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(189)),        transform_worldex   }, 0                               },
5258     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(190)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(190)),        transform_worldex   }, 0                               },
5259     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(191)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(191)),        transform_worldex   }, 0                               },
5260     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(192)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(192)),        transform_worldex   }, 0                               },
5261     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(193)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(193)),        transform_worldex   }, 0                               },
5262     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(194)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(194)),        transform_worldex   }, 0                               },
5263     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(195)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(195)),        transform_worldex   }, 0                               },
5264     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(196)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(196)),        transform_worldex   }, 0                               },
5265     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(197)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(197)),        transform_worldex   }, 0                               },
5266     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(198)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(198)),        transform_worldex   }, 0                               },
5267     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(199)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(199)),        transform_worldex   }, 0                               },
5268     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(200)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(200)),        transform_worldex   }, 0                               },
5269     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(201)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(201)),        transform_worldex   }, 0                               },
5270     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(202)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(202)),        transform_worldex   }, 0                               },
5271     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(203)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(203)),        transform_worldex   }, 0                               },
5272     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(204)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(204)),        transform_worldex   }, 0                               },
5273     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(205)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(205)),        transform_worldex   }, 0                               },
5274     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(206)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(206)),        transform_worldex   }, 0                               },
5275     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(207)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(207)),        transform_worldex   }, 0                               },
5276     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(208)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(208)),        transform_worldex   }, 0                               },
5277     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(209)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(209)),        transform_worldex   }, 0                               },
5278     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(210)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(210)),        transform_worldex   }, 0                               },
5279     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(211)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(211)),        transform_worldex   }, 0                               },
5280     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(212)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(212)),        transform_worldex   }, 0                               },
5281     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(213)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(213)),        transform_worldex   }, 0                               },
5282     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(214)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(214)),        transform_worldex   }, 0                               },
5283     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(215)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(215)),        transform_worldex   }, 0                               },
5284     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(216)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(216)),        transform_worldex   }, 0                               },
5285     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(217)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(217)),        transform_worldex   }, 0                               },
5286     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(218)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(218)),        transform_worldex   }, 0                               },
5287     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(219)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(219)),        transform_worldex   }, 0                               },
5288     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(220)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(220)),        transform_worldex   }, 0                               },
5289     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(221)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(221)),        transform_worldex   }, 0                               },
5290     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(222)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(222)),        transform_worldex   }, 0                               },
5291     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(223)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(223)),        transform_worldex   }, 0                               },
5292     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(224)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(224)),        transform_worldex   }, 0                               },
5293     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(225)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(225)),        transform_worldex   }, 0                               },
5294     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(226)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(226)),        transform_worldex   }, 0                               },
5295     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(227)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(227)),        transform_worldex   }, 0                               },
5296     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(228)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(228)),        transform_worldex   }, 0                               },
5297     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(229)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(229)),        transform_worldex   }, 0                               },
5298     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(230)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(230)),        transform_worldex   }, 0                               },
5299     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(231)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(231)),        transform_worldex   }, 0                               },
5300     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(232)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(232)),        transform_worldex   }, 0                               },
5301     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(233)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(233)),        transform_worldex   }, 0                               },
5302     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(234)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(234)),        transform_worldex   }, 0                               },
5303     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(235)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(235)),        transform_worldex   }, 0                               },
5304     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(236)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(236)),        transform_worldex   }, 0                               },
5305     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(237)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(237)),        transform_worldex   }, 0                               },
5306     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(238)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(238)),        transform_worldex   }, 0                               },
5307     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(239)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(239)),        transform_worldex   }, 0                               },
5308     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(240)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(240)),        transform_worldex   }, 0                               },
5309     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(241)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(241)),        transform_worldex   }, 0                               },
5310     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(242)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(242)),        transform_worldex   }, 0                               },
5311     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(243)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(243)),        transform_worldex   }, 0                               },
5312     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(244)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(244)),        transform_worldex   }, 0                               },
5313     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(245)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(245)),        transform_worldex   }, 0                               },
5314     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(246)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(246)),        transform_worldex   }, 0                               },
5315     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(247)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(247)),        transform_worldex   }, 0                               },
5316     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(248)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(248)),        transform_worldex   }, 0                               },
5317     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(249)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(249)),        transform_worldex   }, 0                               },
5318     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(250)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(250)),        transform_worldex   }, 0                               },
5319     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(251)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(251)),        transform_worldex   }, 0                               },
5320     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(252)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(252)),        transform_worldex   }, 0                               },
5321     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(253)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(253)),        transform_worldex   }, 0                               },
5322     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(254)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(254)),        transform_worldex   }, 0                               },
5323     { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(255)),        { STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(255)),        transform_worldex   }, 0                               },
5324     { STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5325     { STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5326     { STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5327     { STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5328     { STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5329     { STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5330     { STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5331     { STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),transform_texture   }, 0                               },
5332     { STATE_TEXTURESTAGE(0, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(0, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5333     { STATE_TEXTURESTAGE(1, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(1, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5334     { STATE_TEXTURESTAGE(2, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(2, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5335     { STATE_TEXTURESTAGE(3, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(3, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5336     { STATE_TEXTURESTAGE(4, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(4, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5337     { STATE_TEXTURESTAGE(5, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(5, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5338     { STATE_TEXTURESTAGE(6, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(6, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5339     { STATE_TEXTURESTAGE(7, WINED3DTSS_TEXCOORDINDEX),    { STATE_TEXTURESTAGE(7, WINED3DTSS_TEXCOORDINDEX),    tex_coordindex      }, 0                               },
5340       /* Fog */
5341     { STATE_RENDER(WINED3DRS_FOGENABLE),                  { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           }, 0                               },
5342     { STATE_RENDER(WINED3DRS_FOGTABLEMODE),               { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           }, 0                               },
5343     { STATE_RENDER(WINED3DRS_FOGSTART),                   { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           }, 0                               },
5344     { STATE_RENDER(WINED3DRS_FOGEND),                     { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           }, 0                               },
5345     { STATE_RENDER(WINED3DRS_FOGVERTEXMODE),              { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           }, 0                               },
5346     { STATE_RENDER(WINED3DRS_FOGCOLOR),                   { STATE_RENDER(WINED3DRS_FOGCOLOR),                   state_fogcolor      }, 0                               },
5347     { STATE_RENDER(WINED3DRS_FOGDENSITY),                 { STATE_RENDER(WINED3DRS_FOGDENSITY),                 state_fogdensity    }, 0                               },
5348     { STATE_RENDER(WINED3DRS_RANGEFOGENABLE),             { STATE_RENDER(WINED3DRS_RANGEFOGENABLE),             state_rangefog      }, NV_FOG_DISTANCE                 },
5349     { STATE_RENDER(WINED3DRS_RANGEFOGENABLE),             { STATE_RENDER(WINED3DRS_RANGEFOGENABLE),             state_rangefog_w    }, 0                               },
5350     { STATE_RENDER(WINED3DRS_CLIPPING),                   { STATE_RENDER(WINED3DRS_CLIPPING),                   state_clipping      }, 0                               },
5351     { STATE_RENDER(WINED3DRS_CLIPPLANEENABLE),            { STATE_RENDER(WINED3DRS_CLIPPING),                   state_clipping      }, 0                               },
5352     { STATE_RENDER(WINED3DRS_LIGHTING),                   { STATE_RENDER(WINED3DRS_LIGHTING),                   state_lighting      }, 0                               },
5353     { STATE_RENDER(WINED3DRS_AMBIENT),                    { STATE_RENDER(WINED3DRS_AMBIENT),                    state_ambient       }, 0                               },
5354     { STATE_RENDER(WINED3DRS_COLORVERTEX),                { STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      }, 0                               },
5355     { STATE_RENDER(WINED3DRS_LOCALVIEWER),                { STATE_RENDER(WINED3DRS_LOCALVIEWER),                state_localviewer   }, 0                               },
5356     { STATE_RENDER(WINED3DRS_NORMALIZENORMALS),           { STATE_RENDER(WINED3DRS_NORMALIZENORMALS),           state_normalize     }, 0                               },
5357     { STATE_RENDER(WINED3DRS_DIFFUSEMATERIALSOURCE),      { STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      }, 0                               },
5358     { STATE_RENDER(WINED3DRS_SPECULARMATERIALSOURCE),     { STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      }, 0                               },
5359     { STATE_RENDER(WINED3DRS_AMBIENTMATERIALSOURCE),      { STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      }, 0                               },
5360     { STATE_RENDER(WINED3DRS_EMISSIVEMATERIALSOURCE),     { STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      }, 0                               },
5361     { STATE_RENDER(WINED3DRS_VERTEXBLEND),                { STATE_RENDER(WINED3DRS_VERTEXBLEND),                state_vertexblend   }, ARB_VERTEX_BLEND                },
5362     { STATE_RENDER(WINED3DRS_VERTEXBLEND),                { STATE_RENDER(WINED3DRS_VERTEXBLEND),                state_vertexblend_w }, 0                               },
5363     { STATE_RENDER(WINED3DRS_POINTSIZE),                  { STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        }, 0                               },
5364     { STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              { STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              state_psizemin_arb  }, ARB_POINT_PARAMETERS            },
5365     { STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              { STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              state_psizemin_ext  }, EXT_POINT_PARAMETERS            },
5366     { STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              { STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              state_psizemin_w    }, 0                               },
5367     { STATE_RENDER(WINED3DRS_POINTSPRITEENABLE),          { STATE_RENDER(WINED3DRS_POINTSPRITEENABLE),          state_pointsprite   }, ARB_POINT_SPRITE                },
5368     { STATE_RENDER(WINED3DRS_POINTSPRITEENABLE),          { STATE_RENDER(WINED3DRS_POINTSPRITEENABLE),          state_pointsprite_w }, 0                               },
5369     { STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           { STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        }, 0                               },
5370     { STATE_RENDER(WINED3DRS_POINTSCALE_A),               { STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        }, 0                               },
5371     { STATE_RENDER(WINED3DRS_POINTSCALE_B),               { STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        }, 0                               },
5372     { STATE_RENDER(WINED3DRS_POINTSCALE_C),               { STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        }, 0                               },
5373     { STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              { STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              state_psizemax_arb  }, ARB_POINT_PARAMETERS            },
5374     { STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              { STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              state_psizemax_ext  }, EXT_POINT_PARAMETERS            },
5375     { STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              { STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              state_psizemax_w    }, 0                               },
5376     /* pixel shaders need a different fog input */
5377     { STATE_PIXELSHADER,                                  { STATE_PIXELSHADER,                                  apply_pshader_fog   }, 0                               },
5378     /* Samplers for NP2 texture matrix adjustions. They are not needed if GL_ARB_texture_non_power_of_two is supported,
5379      * so register a NULL state handler in that case to get the vertex part of sampler() skipped(VTF is handled in the misc states.
5380      * otherwise, register sampler_texmatrix, which takes care of updating the texture matrix
5381      */
5382     { STATE_SAMPLER(0),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5383     { STATE_SAMPLER(0),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5384     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texmatrix   }, 0                               },
5385     { STATE_SAMPLER(1),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5386     { STATE_SAMPLER(1),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5387     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texmatrix   }, 0                               },
5388     { STATE_SAMPLER(2),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5389     { STATE_SAMPLER(2),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5390     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texmatrix   }, 0                               },
5391     { STATE_SAMPLER(3),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5392     { STATE_SAMPLER(3),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5393     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texmatrix   }, 0                               },
5394     { STATE_SAMPLER(4),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5395     { STATE_SAMPLER(4),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5396     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texmatrix   }, 0                               },
5397     { STATE_SAMPLER(5),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5398     { STATE_SAMPLER(5),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5399     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texmatrix   }, 0                               },
5400     { STATE_SAMPLER(6),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5401     { STATE_SAMPLER(6),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5402     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texmatrix   }, 0                               },
5403     { STATE_SAMPLER(7),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5404     { STATE_SAMPLER(7),                                   { 0,                                                  NULL                }, WINE_NORMALIZED_TEXRECT         },
5405     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texmatrix   }, 0                               },
5406     {0 /* Terminate */,                                   { 0,                                                  0                   }, 0                               },
5407 };
5408
5409 static const struct StateEntryTemplate ffp_fragmentstate_template[] = {
5410     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5411     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5412     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5413     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5414     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5415     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5416     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5417     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5418     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5419     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5420     { STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5421     { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5422     { STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5423     { STATE_TEXTURESTAGE(0, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5424     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5425     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5426     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5427     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5428     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5429     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5430     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5431     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5432     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5433     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5434     { STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5435     { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5436     { STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5437     { STATE_TEXTURESTAGE(1, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5438     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5439     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5440     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5441     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5442     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5443     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5444     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5445     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5446     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5447     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5448     { STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5449     { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5450     { STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5451     { STATE_TEXTURESTAGE(2, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5452     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5453     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5454     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5455     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5456     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5457     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5458     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5459     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5460     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5461     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5462     { STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5463     { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5464     { STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5465     { STATE_TEXTURESTAGE(3, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5466     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5467     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5468     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5469     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5470     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5471     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5472     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5473     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5474     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5475     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5476     { STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5477     { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5478     { STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5479     { STATE_TEXTURESTAGE(4, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5480     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5481     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5482     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5483     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5484     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5485     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5486     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5487     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5488     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5489     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5490     { STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5491     { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5492     { STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5493     { STATE_TEXTURESTAGE(5, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5494     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5495     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5496     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5497     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5498     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5499     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5500     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5501     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5502     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5503     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5504     { STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5505     { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5506     { STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5507     { STATE_TEXTURESTAGE(6, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5508     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5509     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5510     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5511     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5512     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5513     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5514     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5515     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5516     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5517     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     tex_bumpenvmat      }, ATI_ENVMAP_BUMPMAP              },
5518     { STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5519     { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),        { STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),          tex_alphaop         }, 0                               },
5520     { STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),        { STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),          tex_colorop         }, 0                               },
5521     { STATE_TEXTURESTAGE(7, WINED3DTSS_CONSTANT),         { 0 /* As long as we don't support D3DTA_CONSTANT */, state_nogl          }, 0                               },
5522     { STATE_PIXELSHADER,                                  { STATE_PIXELSHADER,                                  apply_pixelshader   }, 0                               },
5523     { STATE_RENDER(WINED3DRS_SRGBWRITEENABLE),            { STATE_PIXELSHADER,                                  apply_pixelshader   }, 0                               },
5524     { STATE_RENDER(WINED3DRS_BORDERCOLOR),                { STATE_RENDER(WINED3DRS_BORDERCOLOR),                state_bordercolor   }, 0                               },
5525     { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              state_texfactor     }, 0                               },
5526     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim      }, 0                               },
5527     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim      }, 0                               },
5528     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim      }, 0                               },
5529     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim      }, 0                               },
5530     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim      }, 0                               },
5531     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim      }, 0                               },
5532     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim      }, 0                               },
5533     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim      }, 0                               },
5534     {0 /* Terminate */,                                   { 0,                                                  0                   }, 0                               },
5535 };
5536 #undef GLINFO_LOCATION
5537
5538 #define GLINFO_LOCATION (*gl_info)
5539 static void ffp_enable(IWineD3DDevice *iface, BOOL enable) { }
5540
5541 static void ffp_fragment_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct fragment_caps *pCaps) {
5542     pCaps->TextureOpCaps =  WINED3DTEXOPCAPS_ADD         |
5543                             WINED3DTEXOPCAPS_ADDSIGNED   |
5544                             WINED3DTEXOPCAPS_ADDSIGNED2X |
5545                             WINED3DTEXOPCAPS_MODULATE    |
5546                             WINED3DTEXOPCAPS_MODULATE2X  |
5547                             WINED3DTEXOPCAPS_MODULATE4X  |
5548                             WINED3DTEXOPCAPS_SELECTARG1  |
5549                             WINED3DTEXOPCAPS_SELECTARG2  |
5550                             WINED3DTEXOPCAPS_DISABLE;
5551
5552     if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE) ||
5553         GL_SUPPORT(EXT_TEXTURE_ENV_COMBINE) ||
5554         GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
5555         pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA  |
5556                                 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA  |
5557                                 WINED3DTEXOPCAPS_BLENDFACTORALPHA   |
5558                                 WINED3DTEXOPCAPS_BLENDCURRENTALPHA  |
5559                                 WINED3DTEXOPCAPS_LERP               |
5560                                 WINED3DTEXOPCAPS_SUBTRACT;
5561     }
5562     if (GL_SUPPORT(ATI_TEXTURE_ENV_COMBINE3) ||
5563         GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
5564         pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_ADDSMOOTH              |
5565                                 WINED3DTEXOPCAPS_MULTIPLYADD            |
5566                                 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
5567                                 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5568                                 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
5569     }
5570     if (GL_SUPPORT(ARB_TEXTURE_ENV_DOT3))
5571         pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_DOTPRODUCT3;
5572
5573     if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
5574         pCaps->TextureOpCaps |= WINED3DTEXOPCAPS_BUMPENVMAP;
5575     }
5576
5577     pCaps->MaxTextureBlendStages   = GL_LIMITS(texture_stages);
5578     pCaps->MaxSimultaneousTextures = GL_LIMITS(textures);
5579 }
5580
5581 static HRESULT ffp_fragment_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
5582 static void ffp_fragment_free(IWineD3DDevice *iface) {}
5583 static BOOL ffp_conv_supported(WINED3DFORMAT fmt) {
5584     TRACE("Checking shader format support for format %s: [FAILED]\n", debug_d3dformat(fmt));
5585     return FALSE;
5586 }
5587
5588 const struct fragment_pipeline ffp_fragment_pipeline = {
5589     ffp_enable,
5590     ffp_fragment_get_caps,
5591     ffp_fragment_alloc,
5592     ffp_fragment_free,
5593     ffp_conv_supported,
5594     ffp_fragmentstate_template,
5595     FALSE /* we cannot disable projected textures. The vertex pipe has to do it */
5596 };
5597
5598 static int num_handlers(APPLYSTATEFUNC *funcs) {
5599     unsigned int i;
5600     for(i = 0; funcs[i]; i++);
5601     return i;
5602 }
5603
5604 static void multistate_apply_2(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5605     stateblock->wineD3DDevice->multistate_funcs[state][0](state, stateblock, context);
5606     stateblock->wineD3DDevice->multistate_funcs[state][1](state, stateblock, context);
5607 }
5608
5609 static void multistate_apply_3(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5610     stateblock->wineD3DDevice->multistate_funcs[state][0](state, stateblock, context);
5611     stateblock->wineD3DDevice->multistate_funcs[state][1](state, stateblock, context);
5612     stateblock->wineD3DDevice->multistate_funcs[state][2](state, stateblock, context);
5613 }
5614
5615 void compile_state_table(struct StateEntry *StateTable,
5616                          APPLYSTATEFUNC **dev_multistate_funcs,
5617                          WineD3D_GL_Info *gl_info,
5618                          const struct StateEntryTemplate *vertex,
5619                          const struct fragment_pipeline *fragment,
5620                          const struct StateEntryTemplate *misc) {
5621     unsigned int i, type, handlers;
5622     APPLYSTATEFUNC multistate_funcs[STATE_HIGHEST + 1][3];
5623     const struct StateEntryTemplate *cur;
5624     BOOL set[STATE_HIGHEST + 1];
5625
5626     memset(multistate_funcs, 0, sizeof(multistate_funcs));
5627
5628     for(i = 0; i < STATE_HIGHEST + 1; i++) {
5629         StateTable[i].representative = 0;
5630         StateTable[i].apply = state_undefined;
5631     }
5632
5633     for(type = 0; type < 3; type++) {
5634         /* This switch decides the order in which the states are applied */
5635         switch(type) {
5636             case 0: cur = misc; break;
5637             case 1: cur = fragment->states; break;
5638             case 2: cur = vertex; break;
5639             default: cur = NULL; /* Stupid compiler */
5640         }
5641         if(!cur) continue;
5642
5643         /* GL extension filtering should not prevent multiple handlers being applied from different
5644          * pipeline parts
5645          */
5646         memset(set, 0, sizeof(set));
5647
5648         for(i = 0; cur[i].state; i++) {
5649
5650             /* Only use the first matching state with the available extension from one template.
5651              * e.g.
5652              * {D3DRS_FOOBAR, {D3DRS_FOOBAR, func1}, XYZ_FANCY},
5653              * {D3DRS_FOOBAR, {D3DRS_FOOBAR, func2}, 0        }
5654              *
5655              * if GL_XYZ_fancy is supported, ignore the 2nd line
5656              */
5657             if(set[cur[i].state]) continue;
5658             /* Skip state lines depending on unsupported extensions */
5659             if(cur[i].extension && !GL_SUPPORT(cur[i].extension)) continue;
5660             set[cur[i].state] = TRUE;
5661             /* In some cases having an extension means that nothing has to be
5662              * done for a state, e.g. if GL_ARB_texture_non_power_of_two is
5663              * supported, the texture coordinate fixup can be ignored. If the
5664              * apply function is used, mark the state set(done above) to prevent
5665              * applying later lines, but do not record anything in the state
5666              * table
5667              */
5668             if(!cur[i].content.apply) continue;
5669
5670             handlers = num_handlers(multistate_funcs[cur[i].state]);
5671             multistate_funcs[cur[i].state][handlers] = cur[i].content.apply;
5672             switch(handlers) {
5673                 case 0:
5674                     StateTable[cur[i].state].apply = cur[i].content.apply;
5675                     break;
5676                 case 1:
5677                     StateTable[cur[i].state].apply = multistate_apply_2;
5678                     dev_multistate_funcs[cur[i].state] = HeapAlloc(GetProcessHeap(),
5679                                                                    0,
5680                                                                    sizeof(**dev_multistate_funcs) * 2);
5681                     dev_multistate_funcs[cur[i].state][0] = multistate_funcs[cur[i].state][0];
5682                     dev_multistate_funcs[cur[i].state][1] = multistate_funcs[cur[i].state][1];
5683                     break;
5684                 case 2:
5685                     StateTable[cur[i].state].apply = multistate_apply_3;
5686                     HeapFree(GetProcessHeap(), 0, multistate_funcs[cur[i].state]);
5687                     dev_multistate_funcs[cur[i].state] = HeapAlloc(GetProcessHeap(),
5688                                                                    0,
5689                                                                    sizeof(**dev_multistate_funcs) * 3);
5690                     dev_multistate_funcs[cur[i].state][0] = multistate_funcs[cur[i].state][0];
5691                     dev_multistate_funcs[cur[i].state][1] = multistate_funcs[cur[i].state][1];
5692                     dev_multistate_funcs[cur[i].state][2] = multistate_funcs[cur[i].state][2];
5693                     break;
5694                 default:
5695                     ERR("Unexpected amount of state handlers for state %u: %u\n",
5696                         cur[i].state, handlers + 1);
5697             }
5698
5699             if(StateTable[cur[i].state].representative &&
5700             StateTable[cur[i].state].representative != cur[i].content.representative) {
5701                 FIXME("State %u has different representatives in different pipeline parts\n",
5702                     cur[i].state);
5703             }
5704             StateTable[cur[i].state].representative = cur[i].content.representative;
5705         }
5706     }
5707 }
5708 #undef GLINFO_LOCATION