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