Assorted spelling fixes.
[wine] / dlls / wined3d / state.c
1 /*
2  * Direct3D state management
3  *
4  * Copyright 2006 Stefan Dösinger for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #ifdef HAVE_FLOAT_H
24 # include <float.h>
25 #endif
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29
30 #define GLINFO_LOCATION ((IWineD3DImpl *)(stateblock->wineD3DDevice->wineD3D))->gl_info
31
32 static void state_unknown(DWORD state, IWineD3DStateBlockImpl *stateblock) {
33     /* State which does exist, but wined3d doesn't know about */
34     if(STATE_IS_RENDER(state)) {
35         WINED3DRENDERSTATETYPE RenderState = state - STATE_RENDER(0);
36         FIXME("(%s, %d) Unknown renderstate\n", debug_d3drenderstate(RenderState), stateblock->renderState[RenderState]);
37     } else {
38         FIXME("(%d) Unknown state with unknown type\n", state);
39     }
40 }
41
42 static void state_nogl(DWORD state, IWineD3DStateBlockImpl *stateblock) {
43     /* Used for states which are not mapped to a gl state as-is, but used somehow different,
44      * e.g as a parameter for drawing, or which are unimplemented in windows d3d
45      */
46     if(STATE_IS_RENDER(state)) {
47         WINED3DRENDERSTATETYPE RenderState = state - STATE_RENDER(0);
48         TRACE("(%s,%d) no direct mapping to gl\n", debug_d3drenderstate(RenderState), stateblock->renderState[RenderState]);
49     } else {
50         /* Shouldn't have an unknown type here */
51         FIXME("%d no direct mapping to gl of state with unknown type\n", state);
52     }
53 }
54
55 static void state_undefined(DWORD state, IWineD3DStateBlockImpl *stateblock) {
56     /* Print a WARN, this allows the stateblock code to loop over all states to generate a display
57      * list without causing confusing terminal output. Deliberately no special debug name here
58      * because its undefined.
59      */
60     WARN("undefined state %d\n", state);
61 }
62
63 static void state_fillmode(DWORD state, IWineD3DStateBlockImpl *stateblock) {
64     D3DFILLMODE Value = stateblock->renderState[WINED3DRS_FILLMODE];
65
66     switch(Value) {
67         case D3DFILL_POINT:
68             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
69             checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)");
70             break;
71         case D3DFILL_WIREFRAME:
72             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
73             checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)");
74             break;
75         case D3DFILL_SOLID:
76             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
77             checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
78             break;
79         default:
80             FIXME("Unrecognized WINED3DRS_FILLMODE value %d\n", Value);
81     }
82 }
83
84 static void state_lighting(DWORD state, IWineD3DStateBlockImpl *stateblock) {
85
86     /* TODO: Lighting is only enabled if Vertex normals are passed by the application,
87      * so merge the lighting render state with the vertex declaration once it is available
88      */
89
90     if (stateblock->renderState[WINED3DRS_LIGHTING]) {
91         glEnable(GL_LIGHTING);
92         checkGLcall("glEnable GL_LIGHTING");
93     } else {
94         glDisable(GL_LIGHTING);
95         checkGLcall("glDisable GL_LIGHTING");
96     }
97 }
98
99 static void state_zenable(DWORD state, IWineD3DStateBlockImpl *stateblock) {
100     switch ((WINED3DZBUFFERTYPE) stateblock->renderState[WINED3DRS_ZENABLE]) {
101         case WINED3DZB_FALSE:
102             glDisable(GL_DEPTH_TEST);
103             checkGLcall("glDisable GL_DEPTH_TEST");
104             break;
105         case WINED3DZB_TRUE:
106             glEnable(GL_DEPTH_TEST);
107             checkGLcall("glEnable GL_DEPTH_TEST");
108             break;
109         case WINED3DZB_USEW:
110             glEnable(GL_DEPTH_TEST);
111             checkGLcall("glEnable GL_DEPTH_TEST");
112             FIXME("W buffer is not well handled\n");
113             break;
114         default:
115             FIXME("Unrecognized D3DZBUFFERTYPE value %d\n", stateblock->renderState[WINED3DRS_ZENABLE]);
116     }
117 }
118
119 static void state_cullmode(DWORD state, IWineD3DStateBlockImpl *stateblock) {
120     /* TODO: Put this into the offscreen / onscreen rendering block due to device->render_offscreen */
121
122     /* If we are culling "back faces with clockwise vertices" then
123        set front faces to be counter clockwise and enable culling
124        of back faces                                               */
125     switch ((WINED3DCULL) stateblock->renderState[WINED3DRS_CULLMODE]) {
126         case WINED3DCULL_NONE:
127             glDisable(GL_CULL_FACE);
128             checkGLcall("glDisable GL_CULL_FACE");
129             break;
130         case WINED3DCULL_CW:
131             glEnable(GL_CULL_FACE);
132             checkGLcall("glEnable GL_CULL_FACE");
133             if (stateblock->wineD3DDevice->render_offscreen) {
134                 glFrontFace(GL_CW);
135                 checkGLcall("glFrontFace GL_CW");
136             } else {
137                 glFrontFace(GL_CCW);
138                 checkGLcall("glFrontFace GL_CCW");
139             }
140             glCullFace(GL_BACK);
141             break;
142         case WINED3DCULL_CCW:
143             glEnable(GL_CULL_FACE);
144             checkGLcall("glEnable GL_CULL_FACE");
145             if (stateblock->wineD3DDevice->render_offscreen) {
146                 glFrontFace(GL_CCW);
147                 checkGLcall("glFrontFace GL_CCW");
148             } else {
149                 glFrontFace(GL_CW);
150                 checkGLcall("glFrontFace GL_CW");
151             }
152             glCullFace(GL_BACK);
153             break;
154         default:
155             FIXME("Unrecognized/Unhandled WINED3DCULL value %d\n", stateblock->renderState[WINED3DRS_CULLMODE]);
156     }
157 }
158
159 static void state_shademode(DWORD state, IWineD3DStateBlockImpl *stateblock) {
160     switch ((WINED3DSHADEMODE) stateblock->renderState[WINED3DRS_SHADEMODE]) {
161         case WINED3DSHADE_FLAT:
162             glShadeModel(GL_FLAT);
163             checkGLcall("glShadeModel(GL_FLAT)");
164             break;
165         case WINED3DSHADE_GOURAUD:
166             glShadeModel(GL_SMOOTH);
167             checkGLcall("glShadeModel(GL_SMOOTH)");
168             break;
169         case WINED3DSHADE_PHONG:
170             FIXME("WINED3DSHADE_PHONG isn't supported\n");
171             break;
172         default:
173             FIXME("Unrecognized/Unhandled WINED3DSHADEMODE value %d\n", stateblock->renderState[WINED3DRS_SHADEMODE]);
174     }
175 }
176
177 static void state_ditherenable(DWORD state, IWineD3DStateBlockImpl *stateblock) {
178     if (stateblock->renderState[WINED3DRS_DITHERENABLE]) {
179         glEnable(GL_DITHER);
180         checkGLcall("glEnable GL_DITHER");
181     } else {
182         glDisable(GL_DITHER);
183         checkGLcall("glDisable GL_DITHER");
184     }
185 }
186
187 static void state_zwritenable(DWORD state, IWineD3DStateBlockImpl *stateblock) {
188     /* TODO: Test if in d3d z writing is enabled even if ZENABLE is off. If yes,
189      * this has to be merged with ZENABLE and ZFUNC
190      */
191     if (stateblock->renderState[WINED3DRS_ZWRITEENABLE]) {
192         glDepthMask(1);
193         checkGLcall("glDepthMask(1)");
194     } else {
195         glDepthMask(0);
196         checkGLcall("glDepthMask(0)");
197     }
198 }
199
200 static void state_zfunc(DWORD state, IWineD3DStateBlockImpl *stateblock) {
201     int glParm = CompareFunc(stateblock->renderState[WINED3DRS_ZFUNC]);
202
203     if(glParm) {
204         glDepthFunc(glParm);
205         checkGLcall("glDepthFunc");
206     }
207 }
208
209 static void state_ambient(DWORD state, IWineD3DStateBlockImpl *stateblock) {
210     float col[4];
211     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_AMBIENT], col);
212
213     TRACE("Setting ambient to (%f,%f,%f,%f)\n", col[0], col[1], col[2], col[3]);
214     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, col);
215     checkGLcall("glLightModel for MODEL_AMBIENT");
216 }
217
218 static void state_blend(DWORD state, IWineD3DStateBlockImpl *stateblock) {
219     int srcBlend = GL_ZERO;
220     int dstBlend = GL_ZERO;
221     float col[4];
222
223     /* GL_LINE_SMOOTH needs GL_BLEND to work, according to the red book, and special blending params */
224     /* TODO: Is enabling blending really affected by the blendfactor??? */
225     if (stateblock->renderState[WINED3DRS_ALPHABLENDENABLE]      ||
226         stateblock->renderState[WINED3DRS_EDGEANTIALIAS]         ||
227         stateblock->renderState[WINED3DRS_ANTIALIASEDLINEENABLE] ||
228         stateblock->renderState[WINED3DRS_BLENDFACTOR] != 0xFFFFFFFF) {
229         glEnable(GL_BLEND);
230         checkGLcall("glEnable GL_BLEND");
231     } else {
232         glDisable(GL_BLEND);
233         checkGLcall("glDisable GL_BLEND");
234         /* Nothing more to do - get out */
235         return;
236     };
237
238     switch (stateblock->renderState[WINED3DRS_SRCBLEND]) {
239         case WINED3DBLEND_ZERO               : srcBlend = GL_ZERO;  break;
240         case WINED3DBLEND_ONE                : srcBlend = GL_ONE;  break;
241         case WINED3DBLEND_SRCCOLOR           : srcBlend = GL_SRC_COLOR;  break;
242         case WINED3DBLEND_INVSRCCOLOR        : srcBlend = GL_ONE_MINUS_SRC_COLOR;  break;
243         case WINED3DBLEND_SRCALPHA           : srcBlend = GL_SRC_ALPHA;  break;
244         case WINED3DBLEND_INVSRCALPHA        : srcBlend = GL_ONE_MINUS_SRC_ALPHA;  break;
245         case WINED3DBLEND_DESTALPHA          : srcBlend = GL_DST_ALPHA;  break;
246         case WINED3DBLEND_INVDESTALPHA       : srcBlend = GL_ONE_MINUS_DST_ALPHA;  break;
247         case WINED3DBLEND_DESTCOLOR          : srcBlend = GL_DST_COLOR;  break;
248         case WINED3DBLEND_INVDESTCOLOR       : srcBlend = GL_ONE_MINUS_DST_COLOR;  break;
249         case WINED3DBLEND_SRCALPHASAT        : srcBlend = GL_SRC_ALPHA_SATURATE;  break;
250
251         case WINED3DBLEND_BOTHSRCALPHA       : srcBlend = GL_SRC_ALPHA;
252             dstBlend = GL_SRC_ALPHA;
253             break;
254
255         case WINED3DBLEND_BOTHINVSRCALPHA    : srcBlend = GL_ONE_MINUS_SRC_ALPHA;
256             dstBlend = GL_ONE_MINUS_SRC_ALPHA;
257             break;
258
259         case WINED3DBLEND_BLENDFACTOR        : srcBlend = GL_CONSTANT_COLOR;   break;
260         case WINED3DBLEND_INVBLENDFACTOR     : srcBlend = GL_ONE_MINUS_CONSTANT_COLOR;  break;
261         default:
262             FIXME("Unrecognized src blend value %d\n", stateblock->renderState[WINED3DRS_SRCBLEND]);
263     }
264
265     switch (stateblock->renderState[WINED3DRS_DESTBLEND]) {
266         case WINED3DBLEND_ZERO               : dstBlend = GL_ZERO;  break;
267         case WINED3DBLEND_ONE                : dstBlend = GL_ONE;  break;
268         case WINED3DBLEND_SRCCOLOR           : dstBlend = GL_SRC_COLOR;  break;
269         case WINED3DBLEND_INVSRCCOLOR        : dstBlend = GL_ONE_MINUS_SRC_COLOR;  break;
270         case WINED3DBLEND_SRCALPHA           : dstBlend = GL_SRC_ALPHA;  break;
271         case WINED3DBLEND_INVSRCALPHA        : dstBlend = GL_ONE_MINUS_SRC_ALPHA;  break;
272         case WINED3DBLEND_DESTALPHA          : dstBlend = GL_DST_ALPHA;  break;
273         case WINED3DBLEND_INVDESTALPHA       : dstBlend = GL_ONE_MINUS_DST_ALPHA;  break;
274         case WINED3DBLEND_DESTCOLOR          : dstBlend = GL_DST_COLOR;  break;
275         case WINED3DBLEND_INVDESTCOLOR       : dstBlend = GL_ONE_MINUS_DST_COLOR;  break;
276         case WINED3DBLEND_SRCALPHASAT        : dstBlend = GL_SRC_ALPHA_SATURATE;  break;
277
278         case WINED3DBLEND_BOTHSRCALPHA       : dstBlend = GL_SRC_ALPHA;
279             srcBlend = GL_SRC_ALPHA;
280             break;
281
282         case WINED3DBLEND_BOTHINVSRCALPHA    : dstBlend = GL_ONE_MINUS_SRC_ALPHA;
283             srcBlend = GL_ONE_MINUS_SRC_ALPHA;
284             break;
285
286         case D3DBLEND_BLENDFACTOR        : dstBlend = GL_CONSTANT_COLOR;   break;
287         case D3DBLEND_INVBLENDFACTOR     : dstBlend = GL_ONE_MINUS_CONSTANT_COLOR;  break;
288         default:
289             FIXME("Unrecognized dst blend value %d\n", stateblock->renderState[WINED3DRS_DESTBLEND]);
290     }
291
292     if(stateblock->renderState[WINED3DRS_EDGEANTIALIAS] ||
293        stateblock->renderState[WINED3DRS_ANTIALIASEDLINEENABLE]) {
294         glEnable(GL_LINE_SMOOTH);
295         checkGLcall("glEnable(GL_LINE_SMOOTH)");
296         if(srcBlend != GL_SRC_ALPHA) {
297             FIXME("WINED3DRS_EDGEANTIALIAS enabled, but incompatible src blending param - what to do?\n");
298             srcBlend = GL_SRC_ALPHA;
299         }
300         if(dstBlend != GL_ONE_MINUS_SRC_ALPHA) {
301             FIXME("WINED3DRS_EDGEANTIALIAS enabled, but incompatible dst blending param - what to do?\n");
302             dstBlend = GL_ONE_MINUS_SRC_ALPHA;
303         }
304     } else {
305         glDisable(GL_LINE_SMOOTH);
306         checkGLcall("glDisable(GL_LINE_SMOOTH)");
307     }
308
309     TRACE("glBlendFunc src=%x, dst=%x\n", srcBlend, dstBlend);
310     glBlendFunc(srcBlend, dstBlend);
311     checkGLcall("glBlendFunc");
312
313     /* TODO: Remove when state management done */
314     stateblock->wineD3DDevice->dstBlend = dstBlend;
315     stateblock->wineD3DDevice->srcBlend = srcBlend;
316
317     TRACE("Setting BlendFactor to %d\n", stateblock->renderState[WINED3DRS_BLENDFACTOR]);
318     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_BLENDFACTOR], col);
319     glBlendColor (col[0],col[1],col[2],col[3]);
320     checkGLcall("glBlendColor");
321 }
322
323 static void state_alpha(DWORD state, IWineD3DStateBlockImpl *stateblock) {
324     int glParm = 0;
325     float ref;
326     BOOL enable_ckey = FALSE;
327
328     IWineD3DSurfaceImpl *surf;
329
330     /* Find out if the texture on the first stage has a ckey set
331      * The alpha state func reads the texture settings, even though alpha and texture are not grouped
332      * together. This is to avoid making a huge alpha+texture+texture stage+ckey block due to the hardly
333      * used WINED3DRS_COLORKEYENABLE state(which is d3d <= 3 only). The texture function will call alpha
334      * in case it finds some texture+colorkeyenable combination which needs extra care.
335      */
336     if(stateblock->textures[0]) {
337         surf = (IWineD3DSurfaceImpl *) ((IWineD3DTextureImpl *)stateblock->textures[0])->surfaces[0];
338         if(surf->CKeyFlags & DDSD_CKSRCBLT) enable_ckey = TRUE;
339     }
340
341     if (stateblock->renderState[WINED3DRS_ALPHATESTENABLE] ||
342         (stateblock->renderState[WINED3DRS_COLORKEYENABLE] && enable_ckey)) {
343         glEnable(GL_ALPHA_TEST);
344         checkGLcall("glEnable GL_ALPHA_TEST");
345     } else {
346         glDisable(GL_ALPHA_TEST);
347         checkGLcall("glDisable GL_ALPHA_TEST");
348         /* Alpha test is disabled, don't bother setting the params - it will happen on the next
349          * enable call
350          */
351         return;
352     }
353
354     if(stateblock->renderState[WINED3DRS_COLORKEYENABLE] && enable_ckey) {
355         glParm = GL_NOTEQUAL;
356         ref = 0.0;
357     } else {
358         ref = ((float) stateblock->renderState[WINED3DRS_ALPHAREF]) / 255.0f;
359         glParm = CompareFunc(stateblock->renderState[WINED3DRS_ALPHAFUNC]);
360     }
361     if(glParm) {
362         stateblock->wineD3DDevice->alphafunc = glParm; /* Remove when state management done */
363         glAlphaFunc(glParm, ref);
364         checkGLcall("glAlphaFunc");
365     }
366     /* TODO: Some texture blending operations seem to affect the alpha test */
367 }
368
369 static void state_clipping(DWORD state, IWineD3DStateBlockImpl *stateblock) {
370     DWORD enable  = 0xFFFFFFFF;
371     DWORD disable = 0x00000000;
372
373     /* TODO: Keep track of previously enabled clipplanes to avoid unneccessary resetting
374      * of already set values
375      */
376
377     /* If enabling / disabling all
378      * TODO: Is this correct? Doesn't D3DRS_CLIPPING disable clipping on the viewport frustrum?
379      */
380     if (stateblock->renderState[WINED3DRS_CLIPPING]) {
381         enable  = stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
382         disable = ~stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
383     } else {
384         disable = 0xffffffff;
385         enable  = 0x00;
386     }
387
388     if (enable & WINED3DCLIPPLANE0)  { glEnable(GL_CLIP_PLANE0);  checkGLcall("glEnable(clip plane 0)"); }
389     if (enable & WINED3DCLIPPLANE1)  { glEnable(GL_CLIP_PLANE1);  checkGLcall("glEnable(clip plane 1)"); }
390     if (enable & WINED3DCLIPPLANE2)  { glEnable(GL_CLIP_PLANE2);  checkGLcall("glEnable(clip plane 2)"); }
391     if (enable & WINED3DCLIPPLANE3)  { glEnable(GL_CLIP_PLANE3);  checkGLcall("glEnable(clip plane 3)"); }
392     if (enable & WINED3DCLIPPLANE4)  { glEnable(GL_CLIP_PLANE4);  checkGLcall("glEnable(clip plane 4)"); }
393     if (enable & WINED3DCLIPPLANE5)  { glEnable(GL_CLIP_PLANE5);  checkGLcall("glEnable(clip plane 5)"); }
394
395     if (disable & WINED3DCLIPPLANE0) { glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)"); }
396     if (disable & WINED3DCLIPPLANE1) { glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)"); }
397     if (disable & WINED3DCLIPPLANE2) { glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)"); }
398     if (disable & WINED3DCLIPPLANE3) { glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)"); }
399     if (disable & WINED3DCLIPPLANE4) { glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)"); }
400     if (disable & WINED3DCLIPPLANE5) { glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)"); }
401
402     /** update clipping status */
403     if (enable) {
404         stateblock->clip_status.ClipUnion = 0;
405         stateblock->clip_status.ClipIntersection = 0xFFFFFFFF;
406     } else {
407         stateblock->clip_status.ClipUnion = 0;
408         stateblock->clip_status.ClipIntersection = 0;
409     }
410 }
411
412 static void state_blendop(DWORD state, IWineD3DStateBlockImpl *stateblock) {
413     int glParm = GL_FUNC_ADD;
414
415     if(!GL_SUPPORT(EXT_BLEND_MINMAX)) {
416         WARN("Unsupported in local OpenGL implementation: glBlendEquation\n");
417         return;
418     }
419
420     switch ((WINED3DBLENDOP) stateblock->renderState[WINED3DRS_BLENDOP]) {
421         case WINED3DBLENDOP_ADD              : glParm = GL_FUNC_ADD;              break;
422         case WINED3DBLENDOP_SUBTRACT         : glParm = GL_FUNC_SUBTRACT;         break;
423         case WINED3DBLENDOP_REVSUBTRACT      : glParm = GL_FUNC_REVERSE_SUBTRACT; break;
424         case WINED3DBLENDOP_MIN              : glParm = GL_MIN;                   break;
425         case WINED3DBLENDOP_MAX              : glParm = GL_MAX;                   break;
426         default:
427             FIXME("Unrecognized/Unhandled D3DBLENDOP value %d\n", stateblock->renderState[WINED3DRS_BLENDOP]);
428     }
429
430     TRACE("glBlendEquation(%x)\n", glParm);
431     GL_EXTCALL(glBlendEquation(glParm));
432     checkGLcall("glBlendEquation");
433 }
434
435 static void
436 state_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock) {
437     /* Originally this used glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR)
438      * and (GL_LIGHT_MODEL_COLOR_CONTROL,GL_SINGLE_COLOR) to swap between enabled/disabled
439      * specular color. This is wrong:
440      * Separate specular color means the specular colour is maintained separately, whereas
441      * single color means it is merged in. However in both cases they are being used to
442      * some extent.
443      * To disable specular color, set it explicitly to black and turn off GL_COLOR_SUM_EXT
444      * NOTE: If not supported don't give FIXMEs the impact is really minimal and very few people are
445      * running 1.4 yet!
446      *
447      *
448      * If register combiners are enabled, enabling / disabling GL_COLOR_SUM has no effect.
449      * Instead, we need to setup the FinalCombiner properly.
450      *
451      * The default setup for the FinalCombiner is:
452      *
453      * <variable>       <input>                             <mapping>               <usage>
454      * GL_VARIABLE_A_NV GL_FOG,                             GL_UNSIGNED_IDENTITY_NV GL_ALPHA
455      * GL_VARIABLE_B_NV GL_SPARE0_PLUS_SECONDARY_COLOR_NV   GL_UNSIGNED_IDENTITY_NV GL_RGB
456      * GL_VARIABLE_C_NV GL_FOG                              GL_UNSIGNED_IDENTITY_NV GL_RGB
457      * GL_VARIABLE_D_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
458      * GL_VARIABLE_E_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
459      * GL_VARIABLE_F_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
460      * GL_VARIABLE_G_NV GL_SPARE0_NV                        GL_UNSIGNED_IDENTITY_NV GL_ALPHA
461      *
462      * That's pretty much fine as it is, except for variable B, which needs to take
463      * either GL_SPARE0_PLUS_SECONDARY_COLOR_NV or GL_SPARE0_NV, depending on
464      * whether WINED3DRS_SPECULARENABLE is enabled or not.
465      */
466
467     TRACE("Setting specular enable state\n");
468     /* TODO: Add to the material setting functions */
469     if (stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
470         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (float*) &stateblock->material.Specular);
471         checkGLcall("glMaterialfv");
472         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
473             glEnable(GL_COLOR_SUM_EXT);
474         } else {
475             TRACE("Specular colors cannot be enabled in this version of opengl\n");
476         }
477         checkGLcall("glEnable(GL_COLOR_SUM)");
478
479         if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
480             GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_PLUS_SECONDARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
481             checkGLcall("glFinalCombinerInputNV()");
482         }
483     } else {
484         float black[4] = {0.0f, 0.0f, 0.0f, 0.0f};
485
486         /* for the case of enabled lighting: */
487         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &black[0]);
488         checkGLcall("glMaterialfv");
489
490         /* for the case of disabled lighting: */
491         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
492             glDisable(GL_COLOR_SUM_EXT);
493         } else {
494             TRACE("Specular colors cannot be disabled in this version of opengl\n");
495         }
496         checkGLcall("glDisable(GL_COLOR_SUM)");
497
498         if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
499             GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
500             checkGLcall("glFinalCombinerInputNV()");
501         }
502     }
503 }
504
505 static void state_texfactor(DWORD state, IWineD3DStateBlockImpl *stateblock) {
506     unsigned int i;
507
508     /* Note the texture color applies to all textures whereas
509      * GL_TEXTURE_ENV_COLOR applies to active only
510      */
511     float col[4];
512     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
513
514     if (!GL_SUPPORT(NV_REGISTER_COMBINERS)) {
515         /* And now the default texture color as well */
516         for (i = 0; i < GL_LIMITS(texture_stages); i++) {
517             /* Note the WINED3DRS value applies to all textures, but GL has one
518              * per texture, so apply it now ready to be used!
519              */
520             if (GL_SUPPORT(ARB_MULTITEXTURE)) {
521                 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
522                 checkGLcall("glActiveTextureARB");
523             } else if (i>0) {
524                 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
525             }
526
527             glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &col[0]);
528             checkGLcall("glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color);");
529         }
530     }
531 }
532
533 static void
534 renderstate_stencil_twosided(IWineD3DStateBlockImpl *stateblock, GLint face, GLint func, GLint ref, GLuint mask, GLint stencilFail, GLint depthFail, GLint stencilPass ) {
535 #if 0 /* Don't use OpenGL 2.0 calls for now */
536             if(GL_EXTCALL(glStencilFuncSeparate) && GL_EXTCALL(glStencilOpSeparate)) {
537                 GL_EXTCALL(glStencilFuncSeparate(face, func, ref, mask));
538                 checkGLcall("glStencilFuncSeparate(...)");
539                 GL_EXTCALL(glStencilOpSeparate(face, stencilFail, depthFail, stencilPass));
540                 checkGLcall("glStencilOpSeparate(...)");
541         }
542             else
543 #endif
544     if(GL_SUPPORT(EXT_STENCIL_TWO_SIDE)) {
545         glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
546         checkGLcall("glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT)");
547         GL_EXTCALL(glActiveStencilFaceEXT(face));
548         checkGLcall("glActiveStencilFaceEXT(...)");
549         glStencilFunc(func, ref, mask);
550         checkGLcall("glStencilFunc(...)");
551         glStencilOp(stencilFail, depthFail, stencilPass);
552         checkGLcall("glStencilOp(...)");
553     } else if(GL_SUPPORT(ATI_SEPARATE_STENCIL)) {
554         GL_EXTCALL(glStencilFuncSeparateATI(face, func, ref, mask));
555         checkGLcall("glStencilFuncSeparateATI(...)");
556         GL_EXTCALL(glStencilOpSeparateATI(face, stencilFail, depthFail, stencilPass));
557         checkGLcall("glStencilOpSeparateATI(...)");
558     } else {
559         ERR("Separate (two sided) stencil not supported on this version of opengl. Caps weren't honored?\n");
560     }
561 }
562
563 static void
564 state_stencil(DWORD state, IWineD3DStateBlockImpl *stateblock) {
565     DWORD onesided_enable = FALSE;
566     DWORD twosided_enable = FALSE;
567     GLint func = GL_ALWAYS;
568     GLint func_ccw = GL_ALWAYS;
569     GLint ref = 0;
570     GLuint mask = 0;
571     GLint stencilFail = GL_KEEP;
572     GLint depthFail = GL_KEEP;
573     GLint stencilPass = GL_KEEP;
574     GLint stencilFail_ccw = GL_KEEP;
575     GLint depthFail_ccw = GL_KEEP;
576     GLint stencilPass_ccw = GL_KEEP;
577
578     if( stateblock->set.renderState[WINED3DRS_STENCILENABLE] )
579         onesided_enable = stateblock->renderState[WINED3DRS_STENCILENABLE];
580     if( stateblock->set.renderState[WINED3DRS_TWOSIDEDSTENCILMODE] )
581         twosided_enable = stateblock->renderState[WINED3DRS_TWOSIDEDSTENCILMODE];
582     if( stateblock->set.renderState[WINED3DRS_STENCILFUNC] )
583         if( !( func = CompareFunc(stateblock->renderState[WINED3DRS_STENCILFUNC]) ) )
584             func = GL_ALWAYS;
585     if( stateblock->set.renderState[WINED3DRS_CCW_STENCILFUNC] )
586         if( !( func_ccw = CompareFunc(stateblock->renderState[WINED3DRS_CCW_STENCILFUNC]) ) )
587             func = GL_ALWAYS;
588     if( stateblock->set.renderState[WINED3DRS_STENCILREF] )
589         ref = stateblock->renderState[WINED3DRS_STENCILREF];
590     if( stateblock->set.renderState[WINED3DRS_STENCILMASK] )
591         mask = stateblock->renderState[WINED3DRS_STENCILMASK];
592     if( stateblock->set.renderState[WINED3DRS_STENCILFAIL] )
593         stencilFail = StencilOp(stateblock->renderState[WINED3DRS_STENCILFAIL]);
594     if( stateblock->set.renderState[WINED3DRS_STENCILZFAIL] )
595         depthFail = StencilOp(stateblock->renderState[WINED3DRS_STENCILZFAIL]);
596     if( stateblock->set.renderState[WINED3DRS_STENCILPASS] )
597         stencilPass = StencilOp(stateblock->renderState[WINED3DRS_STENCILPASS]);
598     if( stateblock->set.renderState[WINED3DRS_CCW_STENCILFAIL] )
599         stencilFail_ccw = StencilOp(stateblock->renderState[WINED3DRS_CCW_STENCILFAIL]);
600     if( stateblock->set.renderState[WINED3DRS_CCW_STENCILZFAIL] )
601         depthFail_ccw = StencilOp(stateblock->renderState[WINED3DRS_CCW_STENCILZFAIL]);
602     if( stateblock->set.renderState[WINED3DRS_CCW_STENCILPASS] )
603         stencilPass_ccw = StencilOp(stateblock->renderState[WINED3DRS_CCW_STENCILPASS]);
604
605     TRACE("(onesided %d, twosided %d, ref %x, mask %x,  \
606             GL_FRONT: func: %x, fail %x, zfail %x, zpass %x  \
607             GL_BACK: func: %x, fail %x, zfail %x, zpass %x )\n",
608     onesided_enable, twosided_enable, ref, mask,
609     func, stencilFail, depthFail, stencilPass,
610     func_ccw, stencilFail_ccw, depthFail_ccw, stencilPass_ccw);
611
612     if (twosided_enable) {
613         renderstate_stencil_twosided(stateblock, GL_FRONT, func, ref, mask, stencilFail, depthFail, stencilPass);
614         renderstate_stencil_twosided(stateblock, GL_BACK, func_ccw, ref, mask, stencilFail_ccw, depthFail_ccw, stencilPass_ccw);
615     } else {
616         if (onesided_enable) {
617             glEnable(GL_STENCIL_TEST);
618             checkGLcall("glEnable GL_STENCIL_TEST");
619             glStencilFunc(func, ref, mask);
620             checkGLcall("glStencilFunc(...)");
621             glStencilOp(stencilFail, depthFail, stencilPass);
622             checkGLcall("glStencilOp(...)");
623         } else {
624             glDisable(GL_STENCIL_TEST);
625             checkGLcall("glDisable GL_STENCIL_TEST");
626         }
627     }
628 }
629
630 static void state_stencilwrite(DWORD state, IWineD3DStateBlockImpl *stateblock) {
631     glStencilMask(stateblock->renderState[WINED3DRS_STENCILWRITEMASK]);
632     checkGLcall("glStencilMask");
633 }
634
635 static void state_fog(DWORD state, IWineD3DStateBlockImpl *stateblock) {
636     /* TODO: Put this into the vertex type block once that is in the state table */
637     BOOL fogenable = stateblock->renderState[WINED3DRS_FOGENABLE];
638     float fogstart, fogend;
639
640     union {
641         DWORD d;
642         float f;
643     } tmpvalue;
644
645     if (!fogenable) {
646         /* No fog? Disable it, and we're done :-) */
647         glDisable(GL_FOG);
648         checkGLcall("glDisable GL_FOG");
649     }
650
651     tmpvalue.d = stateblock->renderState[WINED3DRS_FOGSTART];
652     fogstart = tmpvalue.f;
653     tmpvalue.d = stateblock->renderState[WINED3DRS_FOGEND];
654     fogend = tmpvalue.f;
655
656 #if 0
657     /* Activate when vertex shaders are in the state table */
658     if(stateblock->vertexShader && ((IWineD3DVertexShaderImpl *)stateblock->vertexShader)->baseShader.function &&
659        ((IWineD3DVertexShaderImpl *)stateblock->vertexShader)->usesFog) {
660         glFogi(GL_FOG_MODE, GL_LINEAR);
661         checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
662         fogstart = 1.0;
663         fogend = 0.0;
664         stateblock->wineD3DDevice->last_was_foggy_shader = TRUE;
665     }
666 #endif
667
668     /* DX 7 sdk: "If both render states(vertex and table fog) are set to valid modes,
669      * the system will apply only pixel(=table) fog effects."
670      */
671     /* else */ if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == D3DFOG_NONE) {
672         glHint(GL_FOG_HINT, GL_FASTEST);
673         checkGLcall("glHint(GL_FOG_HINT, GL_FASTEST)");
674 #if 0
675         stateblock->wineD3DDevice->last_was_foggy_shader = FALSE;
676 #endif
677         switch (stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
678             /* Processed vertices have their fog factor stored in the specular value. Fall too the none case.
679              * If we are drawing untransformed vertices atm, d3ddevice_set_ortho will update the fog
680              */
681             case D3DFOG_EXP:  {
682                 if(!stateblock->wineD3DDevice->last_was_rhw) {
683                     glFogi(GL_FOG_MODE, GL_EXP);
684                     checkGLcall("glFogi(GL_FOG_MODE, GL_EXP");
685                     if(GL_SUPPORT(EXT_FOG_COORD)) {
686                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
687                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT");
688                     }
689                     break;
690                 }
691             }
692             case D3DFOG_EXP2: {
693                 if(!stateblock->wineD3DDevice->last_was_rhw) {
694                     glFogi(GL_FOG_MODE, GL_EXP2);
695                     checkGLcall("glFogi(GL_FOG_MODE, GL_EXP2");
696                     if(GL_SUPPORT(EXT_FOG_COORD)) {
697                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
698                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT");
699                     }
700                     break;
701                 }
702             }
703             case D3DFOG_LINEAR: {
704                 if(!stateblock->wineD3DDevice->last_was_rhw) {
705                     glFogi(GL_FOG_MODE, GL_LINEAR);
706                     checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR");
707                     if(GL_SUPPORT(EXT_FOG_COORD)) {
708                         glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
709                         checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT");
710                     }
711                     break;
712                 }
713             }
714             case D3DFOG_NONE: {
715                 /* Both are none? According to msdn the alpha channel of the specular
716                  * color contains a fog factor. Set it in drawStridedSlow.
717                  * Same happens with Vertexfog on transformed vertices
718                  */
719                 if(GL_SUPPORT(EXT_FOG_COORD)) {
720                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
721                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT)\n");
722                     glFogi(GL_FOG_MODE, GL_LINEAR);
723                     checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
724                     fogstart = 0xff;
725                     fogend = 0x0;
726                 } else {
727                     /* Disable GL fog, handle this in software in drawStridedSlow */
728                     fogenable = FALSE;
729                 }
730                 break;
731             }
732             default: FIXME("Unexpected WINED3DRS_FOGVERTEXMODE %d\n", stateblock->renderState[WINED3DRS_FOGVERTEXMODE]);
733         }
734     } else {
735         glHint(GL_FOG_HINT, GL_NICEST);
736         checkGLcall("glHint(GL_FOG_HINT, GL_NICEST)");
737 #if 0
738         stateblock->wineD3DDevice->last_was_foggy_shader = FALSE;
739 #endif
740         switch (stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
741             case D3DFOG_EXP:
742                 glFogi(GL_FOG_MODE, GL_EXP);
743                 checkGLcall("glFogi(GL_FOG_MODE, GL_EXP");
744                 if(GL_SUPPORT(EXT_FOG_COORD)) {
745                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
746                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT");
747                 }
748                 break;
749
750             case D3DFOG_EXP2:
751                 glFogi(GL_FOG_MODE, GL_EXP2);
752                 checkGLcall("glFogi(GL_FOG_MODE, GL_EXP2");
753                 if(GL_SUPPORT(EXT_FOG_COORD)) {
754                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
755                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT");
756                 }
757                 break;
758
759             case D3DFOG_LINEAR:
760                 glFogi(GL_FOG_MODE, GL_LINEAR);
761                 checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR");
762                 if(GL_SUPPORT(EXT_FOG_COORD)) {
763                     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
764                     checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT");
765                 }
766                 break;
767
768             case D3DFOG_NONE:   /* Won't happen */
769             default:
770                 FIXME("Unexpected WINED3DRS_FOGTABLEMODE %d\n", stateblock->renderState[WINED3DRS_FOGTABLEMODE]);
771         }
772     }
773
774     if(fogenable) {
775         glEnable(GL_FOG);
776         checkGLcall("glEnable GL_FOG");
777
778         glFogfv(GL_FOG_START, &fogstart);
779         checkGLcall("glFogf(GL_FOG_START, fogstart");
780         TRACE("Fog Start == %f\n", fogstart);
781
782         glFogfv(GL_FOG_END, &fogend);
783         checkGLcall("glFogf(GL_FOG_END, fogend");
784         TRACE("Fog End == %f\n", fogend);
785     } else {
786         glDisable(GL_FOG);
787         checkGLcall("glDisable GL_FOG");
788     }
789
790     if (GL_SUPPORT(NV_FOG_DISTANCE)) {
791         glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV);
792     }
793 }
794
795 static void state_fogcolor(DWORD state, IWineD3DStateBlockImpl *stateblock) {
796     float col[4];
797     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_FOGCOLOR], col);
798     /* Set the default alpha blend color */
799     glFogfv(GL_FOG_COLOR, &col[0]);
800     checkGLcall("glFog GL_FOG_COLOR");
801 }
802
803 static void state_fogdensity(DWORD state, IWineD3DStateBlockImpl *stateblock) {
804     union {
805         DWORD d;
806         float f;
807     } tmpvalue;
808     tmpvalue.d = stateblock->renderState[WINED3DRS_FOGDENSITY];
809     glFogfv(GL_FOG_DENSITY, &tmpvalue.f);
810     checkGLcall("glFogf(GL_FOG_DENSITY, (float) Value)");
811 }
812
813 /* TODO: Merge with primitive type + init_materials()!! */
814 static void state_colormat(DWORD state, IWineD3DStateBlockImpl *stateblock) {
815     GLenum Parm = GL_AMBIENT_AND_DIFFUSE;
816
817     if (stateblock->renderState[WINED3DRS_COLORVERTEX]) {
818         TRACE("diff %d, amb %d, emis %d, spec %d\n",
819               stateblock->renderState[WINED3DRS_DIFFUSEMATERIALSOURCE],
820               stateblock->renderState[WINED3DRS_AMBIENTMATERIALSOURCE],
821               stateblock->renderState[WINED3DRS_EMISSIVEMATERIALSOURCE],
822               stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE]);
823
824         if (stateblock->renderState[WINED3DRS_DIFFUSEMATERIALSOURCE] == D3DMCS_COLOR1) {
825             if (stateblock->renderState[WINED3DRS_AMBIENTMATERIALSOURCE] == D3DMCS_COLOR1) {
826                 Parm = GL_AMBIENT_AND_DIFFUSE;
827             } else {
828                 Parm = GL_DIFFUSE;
829             }
830         } else if (stateblock->renderState[WINED3DRS_AMBIENTMATERIALSOURCE] == D3DMCS_COLOR1) {
831             Parm = GL_AMBIENT;
832         } else if (stateblock->renderState[WINED3DRS_EMISSIVEMATERIALSOURCE] == D3DMCS_COLOR1) {
833             Parm = GL_EMISSION;
834         } else if (stateblock->renderState[WINED3DRS_SPECULARMATERIALSOURCE] == D3DMCS_COLOR1) {
835             Parm = GL_SPECULAR;
836         } else {
837             Parm = -1;
838         }
839
840         if (Parm == -1) {
841             if (stateblock->wineD3DDevice->tracking_color != DISABLED_TRACKING) stateblock->wineD3DDevice->tracking_color = NEEDS_DISABLE;
842         } else {
843             stateblock->wineD3DDevice->tracking_color = NEEDS_TRACKING;
844             stateblock->wineD3DDevice->tracking_parm  = Parm;
845         }
846
847     } else {
848         if (stateblock->wineD3DDevice->tracking_color != DISABLED_TRACKING) stateblock->wineD3DDevice->tracking_color = NEEDS_DISABLE;
849     }
850 }
851
852 static void state_linepattern(DWORD state, IWineD3DStateBlockImpl *stateblock) {
853     union {
854         DWORD                 d;
855         WINED3DLINEPATTERN    lp;
856     } tmppattern;
857     tmppattern.d = stateblock->renderState[WINED3DRS_LINEPATTERN];
858
859     TRACE("Line pattern: repeat %d bits %x\n", tmppattern.lp.wRepeatFactor, tmppattern.lp.wLinePattern);
860
861     if (tmppattern.lp.wRepeatFactor) {
862         glLineStipple(tmppattern.lp.wRepeatFactor, tmppattern.lp.wLinePattern);
863         checkGLcall("glLineStipple(repeat, linepattern)");
864         glEnable(GL_LINE_STIPPLE);
865         checkGLcall("glEnable(GL_LINE_STIPPLE);");
866     } else {
867         glDisable(GL_LINE_STIPPLE);
868         checkGLcall("glDisable(GL_LINE_STIPPLE);");
869     }
870 }
871
872 static void state_zbias(DWORD state, IWineD3DStateBlockImpl *stateblock) {
873     union {
874         DWORD d;
875         float f;
876     } tmpvalue;
877
878     if (stateblock->renderState[WINED3DRS_ZBIAS]) {
879         tmpvalue.d = stateblock->renderState[WINED3DRS_ZBIAS];
880         TRACE("ZBias value %f\n", tmpvalue.f);
881         glPolygonOffset(0, -tmpvalue.f);
882         checkGLcall("glPolygonOffset(0, -Value)");
883         glEnable(GL_POLYGON_OFFSET_FILL);
884         checkGLcall("glEnable(GL_POLYGON_OFFSET_FILL);");
885         glEnable(GL_POLYGON_OFFSET_LINE);
886         checkGLcall("glEnable(GL_POLYGON_OFFSET_LINE);");
887         glEnable(GL_POLYGON_OFFSET_POINT);
888         checkGLcall("glEnable(GL_POLYGON_OFFSET_POINT);");
889     } else {
890         glDisable(GL_POLYGON_OFFSET_FILL);
891         checkGLcall("glDisable(GL_POLYGON_OFFSET_FILL);");
892         glDisable(GL_POLYGON_OFFSET_LINE);
893         checkGLcall("glDisable(GL_POLYGON_OFFSET_LINE);");
894         glDisable(GL_POLYGON_OFFSET_POINT);
895         checkGLcall("glDisable(GL_POLYGON_OFFSET_POINT);");
896     }
897 }
898
899
900 static void state_normalize(DWORD state, IWineD3DStateBlockImpl *stateblock) {
901     if (stateblock->renderState[WINED3DRS_NORMALIZENORMALS]) {
902         glEnable(GL_NORMALIZE);
903         checkGLcall("glEnable(GL_NORMALIZE);");
904     } else {
905         glDisable(GL_NORMALIZE);
906         checkGLcall("glDisable(GL_NORMALIZE);");
907     }
908 }
909
910 static void state_psize(DWORD state, IWineD3DStateBlockImpl *stateblock) {
911     union {
912         DWORD d;
913         float f;
914     } tmpvalue;
915
916     /* FIXME: check that pointSize isn't outside glGetFloatv( GL_POINT_SIZE_MAX_ARB, &maxSize ); or -ve */
917     tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE];
918     TRACE("Set point size to %f\n", tmpvalue.f);
919     glPointSize(tmpvalue.f);
920     checkGLcall("glPointSize(...);");
921 }
922
923 static void state_psizemin(DWORD state, IWineD3DStateBlockImpl *stateblock) {
924     union {
925         DWORD d;
926         float f;
927     } tmpvalue;
928
929     if (GL_SUPPORT(EXT_POINT_PARAMETERS)) {
930         tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MIN];
931         GL_EXTCALL(glPointParameterfEXT)(GL_POINT_SIZE_MIN_EXT, tmpvalue.f);
932         checkGLcall("glPointParameterfEXT(...);");
933     } else {
934         FIXME("WINED3DRS_POINTSIZE_MIN not supported on this opengl\n");
935     }
936 }
937
938 static void state_psizemax(DWORD state, IWineD3DStateBlockImpl *stateblock) {
939     union {
940         DWORD d;
941         float f;
942     } tmpvalue;
943
944     if (GL_SUPPORT(EXT_POINT_PARAMETERS)) {
945         tmpvalue.d = stateblock->renderState[WINED3DRS_POINTSIZE_MAX];
946         GL_EXTCALL(glPointParameterfEXT)(GL_POINT_SIZE_MAX_EXT, tmpvalue.f);
947         checkGLcall("glPointParameterfEXT(...);");
948     } else {
949         FIXME("WINED3DRS_POINTSIZE_MAX not supported on this opengl\n");
950     }
951 }
952
953 static void state_pscale(DWORD state, IWineD3DStateBlockImpl *stateblock) {
954     /* TODO: Group this with the viewport */
955     /*
956      * POINTSCALEENABLE controls how point size value is treated. If set to
957      * true, the point size is scaled with respect to height of viewport.
958      * When set to false point size is in pixels.
959      *
960      * http://msdn.microsoft.com/library/en-us/directx9_c/point_sprites.asp
961      */
962
963     /* Default values */
964     GLfloat att[3] = {1.0f, 0.0f, 0.0f};
965
966     /*
967      * Minimum valid point size for OpenGL is 1.0f. For Direct3D it is 0.0f.
968      * This means that OpenGL will clamp really small point sizes to 1.0f.
969      * To correct for this we need to multiply by the scale factor when sizes
970      * are less than 1.0f. scale_factor =  1.0f / point_size.
971      */
972     GLfloat pointSize = *((float*)&stateblock->renderState[WINED3DRS_POINTSIZE]);
973     if(pointSize > 0.0f) {
974         GLfloat scaleFactor;
975
976         if(pointSize < 1.0f) {
977             scaleFactor = pointSize * pointSize;
978         } else {
979             scaleFactor = 1.0f;
980         }
981
982         if(stateblock->renderState[WINED3DRS_POINTSCALEENABLE]) {
983             att[0] = *((float*)&stateblock->renderState[WINED3DRS_POINTSCALE_A]) /
984                     (stateblock->viewport.Height * stateblock->viewport.Height * scaleFactor);
985             att[1] = *((float*)&stateblock->renderState[WINED3DRS_POINTSCALE_B]) /
986                     (stateblock->viewport.Height * stateblock->viewport.Height * scaleFactor);
987             att[2] = *((float*)&stateblock->renderState[WINED3DRS_POINTSCALE_C]) /
988                     (stateblock->viewport.Height * stateblock->viewport.Height * scaleFactor);
989         }
990     }
991
992     if(GL_SUPPORT(ARB_POINT_PARAMETERS)) {
993         GL_EXTCALL(glPointParameterfvARB)(GL_POINT_DISTANCE_ATTENUATION_ARB, att);
994         checkGLcall("glPointParameterfvARB(GL_DISTANCE_ATTENUATION_ARB, ...");
995     }
996     else if(GL_SUPPORT(EXT_POINT_PARAMETERS)) {
997         GL_EXTCALL(glPointParameterfvEXT)(GL_DISTANCE_ATTENUATION_EXT, att);
998         checkGLcall("glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, ...");
999     } else {
1000         TRACE("POINT_PARAMETERS not supported in this version of opengl\n");
1001     }
1002 }
1003
1004 static void state_colorwrite(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1005     DWORD Value = stateblock->renderState[WINED3DRS_COLORWRITEENABLE];
1006
1007     TRACE("Color mask: r(%d) g(%d) b(%d) a(%d)\n",
1008         Value & D3DCOLORWRITEENABLE_RED   ? 1 : 0,
1009         Value & D3DCOLORWRITEENABLE_GREEN ? 1 : 0,
1010         Value & D3DCOLORWRITEENABLE_BLUE  ? 1 : 0,
1011         Value & D3DCOLORWRITEENABLE_ALPHA ? 1 : 0);
1012     glColorMask(Value & D3DCOLORWRITEENABLE_RED   ? GL_TRUE : GL_FALSE,
1013                 Value & D3DCOLORWRITEENABLE_GREEN ? GL_TRUE : GL_FALSE,
1014                 Value & D3DCOLORWRITEENABLE_BLUE  ? GL_TRUE : GL_FALSE,
1015                 Value & D3DCOLORWRITEENABLE_ALPHA ? GL_TRUE : GL_FALSE);
1016     checkGLcall("glColorMask(...)");
1017
1018     /* depends on WINED3DRS_COLORWRITEENABLE. */
1019     if(stateblock->renderState[WINED3DRS_COLORWRITEENABLE1] != 0x0000000F ||
1020        stateblock->renderState[WINED3DRS_COLORWRITEENABLE2] != 0x0000000F ||
1021        stateblock->renderState[WINED3DRS_COLORWRITEENABLE3] != 0x0000000F ) {
1022         ERR("(WINED3DRS_COLORWRITEENABLE1/2/3,%d,%d,%d) not yet implemented. Missing of cap D3DPMISCCAPS_INDEPENDENTWRITEMASKS wasn't honored?\n",
1023             stateblock->renderState[WINED3DRS_COLORWRITEENABLE1],
1024             stateblock->renderState[WINED3DRS_COLORWRITEENABLE2],
1025             stateblock->renderState[WINED3DRS_COLORWRITEENABLE3]);
1026     }
1027 }
1028
1029 static void state_localviewer(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1030     if(stateblock->renderState[WINED3DRS_LOCALVIEWER]) {
1031         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
1032         checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)");
1033     } else {
1034         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
1035         checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0)");
1036     }
1037 }
1038
1039 static void state_lastpixel(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1040     if(stateblock->renderState[WINED3DRS_LASTPIXEL]) {
1041         TRACE("Last Pixel Drawing Enabled\n");
1042     } else {
1043         FIXME("Last Pixel Drawing Disabled, not handled yet\n");
1044     }
1045 }
1046
1047 static void state_pointsprite(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1048     /* TODO: NV_POINT_SPRITE */
1049     if (!GL_SUPPORT(ARB_POINT_SPRITE)) {
1050         TRACE("Point sprites not supported\n");
1051         return;
1052     }
1053
1054     /*
1055      * Point sprites are always enabled. Value controls texture coordinate
1056      * replacement mode. Must be set true for point sprites to use
1057      * textures.
1058      */
1059     glEnable(GL_POINT_SPRITE_ARB);
1060     checkGLcall("glEnable(GL_POINT_SPRITE_ARB)");
1061
1062     if (stateblock->renderState[WINED3DRS_POINTSPRITEENABLE]) {
1063         glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, TRUE);
1064         checkGLcall("glTexEnvf(GL_POINT_SPRITE, GL_COORD_REPLACE, TRUE)");
1065     } else {
1066         glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, FALSE);
1067         checkGLcall("glTexEnvf(GL_POINT_SPRITE, GL_COORD_REPLACE, FALSE)");
1068     }
1069 }
1070
1071 static void state_wrap(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1072     /**
1073      http://www.cosc.brocku.ca/Offerings/3P98/course/lectures/texture/
1074      http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/graphics/programmingguide/FixedFunction/Textures/texturewrapping.asp
1075      http://www.gamedev.net/reference/programming/features/rendererdll3/page2.asp
1076      Descussion that ways to turn on WRAPing to solve an opengl conversion problem.
1077      http://www.flipcode.org/cgi-bin/fcmsg.cgi?thread_show=10248
1078
1079      so far as I can tell, wrapping and texture-coordinate generate go hand in hand,
1080      */
1081     TRACE("Stub\n");
1082     if(stateblock->renderState[WINED3DRS_WRAP0] ||
1083        stateblock->renderState[WINED3DRS_WRAP1] ||
1084        stateblock->renderState[WINED3DRS_WRAP2] ||
1085        stateblock->renderState[WINED3DRS_WRAP3] ||
1086        stateblock->renderState[WINED3DRS_WRAP4] ||
1087        stateblock->renderState[WINED3DRS_WRAP5] ||
1088        stateblock->renderState[WINED3DRS_WRAP6] ||
1089        stateblock->renderState[WINED3DRS_WRAP7] ||
1090        stateblock->renderState[WINED3DRS_WRAP8] ||
1091        stateblock->renderState[WINED3DRS_WRAP9] ||
1092        stateblock->renderState[WINED3DRS_WRAP10] ||
1093        stateblock->renderState[WINED3DRS_WRAP11] ||
1094        stateblock->renderState[WINED3DRS_WRAP12] ||
1095        stateblock->renderState[WINED3DRS_WRAP13] ||
1096        stateblock->renderState[WINED3DRS_WRAP14] ||
1097        stateblock->renderState[WINED3DRS_WRAP15] ) {
1098         ERR("(WINED3DRS_WRAP0) Texture wraping not yet supported\n");
1099     }
1100 }
1101
1102 static void state_multisampleaa(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1103     if( GL_SUPPORT(ARB_MULTISAMPLE) ) {
1104         if(stateblock->renderState[WINED3DRS_MULTISAMPLEANTIALIAS]) {
1105             glEnable(GL_MULTISAMPLE_ARB);
1106             checkGLcall("glEnable(GL_MULTISAMPLE_ARB)");
1107         } else {
1108             glDisable(GL_MULTISAMPLE_ARB);
1109             checkGLcall("glDisable(GL_MULTISAMPLE_ARB)");
1110         }
1111     } else {
1112         if(stateblock->renderState[WINED3DRS_MULTISAMPLEANTIALIAS]) {
1113             ERR("Multisample antialiasing not supported by gl\n");
1114         }
1115     }
1116 }
1117
1118 static void state_scissor(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1119     if(stateblock->renderState[WINED3DRS_SCISSORTESTENABLE]) {
1120         glEnable(GL_SCISSOR_TEST);
1121         checkGLcall("glEnable(GL_SCISSOR_TEST)");
1122     } else {
1123         glDisable(GL_SCISSOR_TEST);
1124         checkGLcall("glDisable(GL_SCISSOR_TEST)");
1125     }
1126 }
1127
1128 static void state_depthbias(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1129     union {
1130         DWORD d;
1131         float f;
1132     } tmpvalue;
1133
1134     if(stateblock->renderState[WINED3DRS_SLOPESCALEDEPTHBIAS] ||
1135        stateblock->renderState[WINED3DRS_DEPTHBIAS]) {
1136         tmpvalue.d = stateblock->renderState[WINED3DRS_SLOPESCALEDEPTHBIAS];
1137         glEnable(GL_POLYGON_OFFSET_FILL);
1138         checkGLcall("glEnable(GL_POLYGON_OFFSET_FILL)");
1139         glPolygonOffset(tmpvalue.f, *((float*)&stateblock->renderState[WINED3DRS_DEPTHBIAS]));
1140         checkGLcall("glPolygonOffset(...)");
1141     } else {
1142         glDisable(GL_POLYGON_OFFSET_FILL);
1143         checkGLcall("glDisable(GL_POLYGON_OFFSET_FILL)");
1144     }
1145 }
1146
1147 static void state_perspective(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1148     if (stateblock->renderState[WINED3DRS_TEXTUREPERSPECTIVE]) {
1149         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1150         checkGLcall("glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)");
1151     } else {
1152         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1153         checkGLcall("glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST)");
1154     }
1155 }
1156
1157 static void state_stippledalpha(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1158     TRACE("Stub\n");
1159     if (stateblock->renderState[WINED3DRS_STIPPLEDALPHA])
1160         ERR(" Stippled Alpha not supported yet.\n");
1161 }
1162
1163 static void state_antialias(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1164     TRACE("Stub\n");
1165     if (stateblock->renderState[WINED3DRS_ANTIALIAS])
1166         ERR(" Antialias not supported yet.\n");
1167 }
1168
1169 static void state_multisampmask(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1170     TRACE("Stub\n");
1171     if (stateblock->renderState[WINED3DRS_MULTISAMPLEMASK] != 0xFFFFFFFF)
1172         ERR("(WINED3DRS_MULTISAMPLEMASK,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_MULTISAMPLEMASK]);
1173 }
1174
1175 static void state_patchededge(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1176     TRACE("Stub\n");
1177     if (stateblock->renderState[WINED3DRS_PATCHEDGESTYLE] != D3DPATCHEDGE_DISCRETE)
1178         ERR("(WINED3DRS_PATCHEDGESTYLE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_PATCHEDGESTYLE]);
1179 }
1180
1181 static void state_patchsegments(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1182     union {
1183         DWORD d;
1184         float f;
1185     } tmpvalue;
1186     tmpvalue.f = 1.0f;
1187
1188     TRACE("Stub\n");
1189     if (stateblock->renderState[WINED3DRS_PATCHSEGMENTS] != tmpvalue.d)
1190         ERR("(WINED3DRS_PATCHSEGMENTS,%d) not yet implemented\n", tmpvalue.d);
1191 }
1192
1193 static void state_positiondegree(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1194     TRACE("Stub\n");
1195     if (stateblock->renderState[WINED3DRS_POSITIONDEGREE] != D3DDEGREE_CUBIC)
1196         ERR("(WINED3DRS_POSITIONDEGREE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_POSITIONDEGREE]);
1197 }
1198
1199 static void state_normaldegree(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1200     TRACE("Stub\n");
1201     if (stateblock->renderState[WINED3DRS_NORMALDEGREE] != D3DDEGREE_LINEAR)
1202         ERR("(WINED3DRS_NORMALDEGREE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_NORMALDEGREE]);
1203 }
1204
1205 static void state_tessellation(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1206     TRACE("Stub\n");
1207     if(stateblock->renderState[WINED3DRS_ENABLEADAPTIVETESSELLATION])
1208         FIXME("(WINED3DRS_ENABLEADAPTIVETESSELLATION,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_ENABLEADAPTIVETESSELLATION]);
1209 }
1210
1211
1212 static void state_srgbwrite(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1213     if(stateblock->renderState[WINED3DRS_SRGBWRITEENABLE])
1214         ERR("Render state WINED3DRS_SRGBWRITEENABLE not yet implemented\n");
1215 }
1216
1217 static void state_seperateblend(DWORD state, IWineD3DStateBlockImpl *stateblock) {
1218     TRACE("Stub\n");
1219     if(stateblock->renderState[WINED3DRS_SEPARATEALPHABLENDENABLE])
1220         FIXME("(WINED3DRS_SEPARATEALPHABLENDENABLE,%d) not yet implemented\n", stateblock->renderState[WINED3DRS_SEPARATEALPHABLENDENABLE]);
1221 }
1222
1223 const struct StateEntry StateTable[] =
1224 {
1225       /* State name                                         representative,                                     apply function */
1226     { /* 0,  Undefined                              */      0,                                                  state_undefined     },
1227     { /* 1,  WINED3DRS_TEXTUREHANDLE                */      0 /* Handled in ddraw */,                           state_undefined     },
1228     { /* 2,  WINED3DRS_ANTIALIAS                    */      STATE_RENDER(WINED3DRS_ANTIALIAS),                  state_antialias     },
1229     { /* 3,  WINED3DRS_TEXTUREADDRESS               */      0 /* Handled in ddraw */,                           state_undefined     },
1230     { /* 4,  WINED3DRS_TEXTUREPERSPECTIVE           */      STATE_RENDER(WINED3DRS_TEXTUREPERSPECTIVE),         state_perspective   },
1231     { /* 5,  WINED3DRS_WRAPU                        */      STATE_RENDER(WINED3DRS_WRAPU),                      state_unknown       },
1232     { /* 6,  WINED3DRS_WRAPV                        */      STATE_RENDER(WINED3DRS_WRAPV),                      state_unknown       },
1233     { /* 7,  WINED3DRS_ZENABLE                      */      STATE_RENDER(WINED3DRS_ZENABLE),                    state_zenable       },
1234     { /* 8,  WINED3DRS_FILLMODE                     */      STATE_RENDER(WINED3DRS_FILLMODE),                   state_fillmode      },
1235     { /* 9,  WINED3DRS_SHADEMODE                    */      STATE_RENDER(WINED3DRS_SHADEMODE),                  state_shademode     },
1236     { /* 10, WINED3DRS_LINEPATTERN                  */      STATE_RENDER(WINED3DRS_LINEPATTERN),                state_linepattern   },
1237     { /* 11, WINED3DRS_MONOENABLE                   */      STATE_RENDER(WINED3DRS_MONOENABLE),                 state_unknown       },
1238     { /* 12, WINED3DRS_ROP2                         */      STATE_RENDER(WINED3DRS_ROP2),                       state_unknown       },
1239     { /* 13, WINED3DRS_PLANEMASK                    */      STATE_RENDER(WINED3DRS_PLANEMASK),                  state_unknown       },
1240     { /* 14, WINED3DRS_ZWRITEENABLE                 */      STATE_RENDER(WINED3DRS_ZWRITEENABLE),               state_zwritenable   },
1241     { /* 15, WINED3DRS_ALPHATESTENABLE              */      STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         },
1242     { /* 16, WINED3DRS_LASTPIXEL                    */      STATE_RENDER(WINED3DRS_LASTPIXEL),                  state_lastpixel     },
1243     { /* 17, WINED3DRS_TEXTUREMAG                   */      0 /* Handled in ddraw */,                           state_undefined     },
1244     { /* 18, WINED3DRS_TEXTUREMIN                   */      0 /* Handled in ddraw */,                           state_undefined     },
1245     { /* 19, WINED3DRS_SRCBLEND                     */      STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         },
1246     { /* 20, WINED3DRS_DESTBLEND                    */      STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         },
1247     { /* 21, WINED3DRS_TEXTUREMAPBLEND              */      0 /* Handled in ddraw */,                           state_undefined     },
1248     { /* 22, WINED3DRS_CULLMODE                     */      STATE_RENDER(WINED3DRS_CULLMODE),                   state_cullmode      },
1249     { /* 23, WINED3DRS_ZFUNC                        */      STATE_RENDER(WINED3DRS_ZFUNC),                      state_zfunc         },
1250     { /* 24, WINED3DRS_ALPHAREF                     */      STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         },
1251     { /* 25, WINED3DRS_ALPHAFUNC                    */      STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         },
1252     { /* 26, WINED3DRS_DITHERENABLE                 */      STATE_RENDER(WINED3DRS_DITHERENABLE),               state_ditherenable  },
1253     { /* 27, WINED3DRS_ALPHABLENDENABLE             */      STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         },
1254     { /* 28, WINED3DRS_FOGENABLE                    */      STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           },
1255     { /* 29, WINED3DRS_SPECULARENABLE               */      STATE_RENDER(WINED3DRS_SPECULARENABLE),             state_specularenable},
1256     { /* 30, WINED3DRS_ZVISIBLE                     */      0 /* Not supported according to the msdn */,        state_nogl          },
1257     { /* 31, WINED3DRS_SUBPIXEL                     */      STATE_RENDER(WINED3DRS_SUBPIXEL),                   state_unknown       },
1258     { /* 32, WINED3DRS_SUBPIXELX                    */      STATE_RENDER(WINED3DRS_SUBPIXELX),                  state_unknown       },
1259     { /* 33, WINED3DRS_STIPPLEDALPHA                */      STATE_RENDER(WINED3DRS_STIPPLEDALPHA),              state_stippledalpha },
1260     { /* 34, WINED3DRS_FOGCOLOR                     */      STATE_RENDER(WINED3DRS_FOGCOLOR),                   state_fogcolor      },
1261     { /* 35, WINED3DRS_FOGTABLEMODE                 */      STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           },
1262     { /* 36, WINED3DRS_FOGSTART                     */      STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           },
1263     { /* 37, WINED3DRS_FOGEND                       */      STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           },
1264     { /* 38, WINED3DRS_FOGDENSITY                   */      STATE_RENDER(WINED3DRS_FOGDENSITY),                 state_fogdensity    },
1265     { /* 39, WINED3DRS_STIPPLEENABLE                */      STATE_RENDER(WINED3DRS_STIPPLEENABLE),              state_unknown       },
1266     { /* 40, WINED3DRS_EDGEANTIALIAS                */      STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         },
1267     { /* 41, WINED3DRS_COLORKEYENABLE               */      STATE_RENDER(WINED3DRS_ALPHATESTENABLE),            state_alpha         },
1268     { /* 42, undefined                              */      0,                                                  state_undefined     },
1269     { /* 43, WINED3DRS_BORDERCOLOR                  */      STATE_RENDER(WINED3DRS_BORDERCOLOR),                state_unknown       },
1270     { /* 44, WINED3DRS_TEXTUREADDRESSU              */      0, /* Handled in ddraw */                           state_undefined     },
1271     { /* 45, WINED3DRS_TEXTUREADDRESSV              */      0, /* Handled in ddraw */                           state_undefined     },
1272     { /* 46, WINED3DRS_MIPMAPLODBIAS                */      STATE_RENDER(WINED3DRS_MIPMAPLODBIAS),              state_unknown       },
1273     { /* 47, WINED3DRS_ZBIAS                        */      STATE_RENDER(WINED3DRS_ZBIAS),                      state_zbias         },
1274     { /* 48, WINED3DRS_RANGEFOGENABLE               */      0,                                                  state_nogl          },
1275     { /* 49, WINED3DRS_ANISOTROPY                   */      STATE_RENDER(WINED3DRS_ANISOTROPY),                 state_unknown       },
1276     { /* 50, WINED3DRS_FLUSHBATCH                   */      STATE_RENDER(WINED3DRS_FLUSHBATCH),                 state_unknown       },
1277     { /* 51, WINED3DRS_TRANSLUCENTSORTINDEPENDENT   */      STATE_RENDER(WINED3DRS_TRANSLUCENTSORTINDEPENDENT), state_unknown       },
1278     { /* 52, WINED3DRS_STENCILENABLE                */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1279     { /* 53, WINED3DRS_STENCILFAIL                  */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1280     { /* 54, WINED3DRS_STENCILZFAIL                 */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1281     { /* 55, WINED3DRS_STENCILPASS                  */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1282     { /* 56, WINED3DRS_STENCILFUNC                  */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1283     { /* 57, WINED3DRS_STENCILREF                   */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1284     { /* 58, WINED3DRS_STENCILMASK                  */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1285     { /* 59, WINED3DRS_STENCILWRITEMASK             */      STATE_RENDER(WINED3DRS_STENCILWRITEMASK),           state_stencilwrite  },
1286     { /* 60, WINED3DRS_TEXTUREFACTOR                */      STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              state_texfactor     },
1287     /* A BIG hole. If wanted, 'fixed' states like the vertex type or the bound shaders can be put here */
1288     { /* 61, Undefined                              */      0,                                                  state_undefined     },
1289     { /* 62, Undefined                              */      0,                                                  state_undefined     },
1290     { /* 63, Undefined                              */      0,                                                  state_undefined     },
1291     { /* 64, Undefined                              */      0,                                                  state_undefined     },
1292     { /* 65, Undefined                              */      0,                                                  state_undefined     },
1293     { /* 66, Undefined                              */      0,                                                  state_undefined     },
1294     { /* 67, Undefined                              */      0,                                                  state_undefined     },
1295     { /* 68, Undefined                              */      0,                                                  state_undefined     },
1296     { /* 69, Undefined                              */      0,                                                  state_undefined     },
1297     { /* 70, Undefined                              */      0,                                                  state_undefined     },
1298     { /* 71, Undefined                              */      0,                                                  state_undefined     },
1299     { /* 72, Undefined                              */      0,                                                  state_undefined     },
1300     { /* 73, Undefined                              */      0,                                                  state_undefined     },
1301     { /* 74, Undefined                              */      0,                                                  state_undefined     },
1302     { /* 75, Undefined                              */      0,                                                  state_undefined     },
1303     { /* 76, Undefined                              */      0,                                                  state_undefined     },
1304     { /* 77, Undefined                              */      0,                                                  state_undefined     },
1305     { /* 78, Undefined                              */      0,                                                  state_undefined     },
1306     { /* 79, Undefined                              */      0,                                                  state_undefined     },
1307     { /* 80, Undefined                              */      0,                                                  state_undefined     },
1308     { /* 81, Undefined                              */      0,                                                  state_undefined     },
1309     { /* 82, Undefined                              */      0,                                                  state_undefined     },
1310     { /* 83, Undefined                              */      0,                                                  state_undefined     },
1311     { /* 84, Undefined                              */      0,                                                  state_undefined     },
1312     { /* 85, Undefined                              */      0,                                                  state_undefined     },
1313     { /* 86, Undefined                              */      0,                                                  state_undefined     },
1314     { /* 87, Undefined                              */      0,                                                  state_undefined     },
1315     { /* 88, Undefined                              */      0,                                                  state_undefined     },
1316     { /* 89, Undefined                              */      0,                                                  state_undefined     },
1317     { /* 90, Undefined                              */      0,                                                  state_undefined     },
1318     { /* 91, Undefined                              */      0,                                                  state_undefined     },
1319     { /* 92, Undefined                              */      0,                                                  state_undefined     },
1320     { /* 93, Undefined                              */      0,                                                  state_undefined     },
1321     { /* 94, Undefined                              */      0,                                                  state_undefined     },
1322     { /* 95, Undefined                              */      0,                                                  state_undefined     },
1323     { /* 96, Undefined                              */      0,                                                  state_undefined     },
1324     { /* 97, Undefined                              */      0,                                                  state_undefined     },
1325     { /* 98, Undefined                              */      0,                                                  state_undefined     },
1326     { /* 99, Undefined                              */      0,                                                  state_undefined     },
1327     { /*100, Undefined                              */      0,                                                  state_undefined     },
1328     { /*101, Undefined                              */      0,                                                  state_undefined     },
1329     { /*102, Undefined                              */      0,                                                  state_undefined     },
1330     { /*103, Undefined                              */      0,                                                  state_undefined     },
1331     { /*104, Undefined                              */      0,                                                  state_undefined     },
1332     { /*105, Undefined                              */      0,                                                  state_undefined     },
1333     { /*106, Undefined                              */      0,                                                  state_undefined     },
1334     { /*107, Undefined                              */      0,                                                  state_undefined     },
1335     { /*108, Undefined                              */      0,                                                  state_undefined     },
1336     { /*109, Undefined                              */      0,                                                  state_undefined     },
1337     { /*110, Undefined                              */      0,                                                  state_undefined     },
1338     { /*111, Undefined                              */      0,                                                  state_undefined     },
1339     { /*112, Undefined                              */      0,                                                  state_undefined     },
1340     { /*113, Undefined                              */      0,                                                  state_undefined     },
1341     { /*114, Undefined                              */      0,                                                  state_undefined     },
1342     { /*115, Undefined                              */      0,                                                  state_undefined     },
1343     { /*116, Undefined                              */      0,                                                  state_undefined     },
1344     { /*117, Undefined                              */      0,                                                  state_undefined     },
1345     { /*118, Undefined                              */      0,                                                  state_undefined     },
1346     { /*119, Undefined                              */      0,                                                  state_undefined     },
1347     { /*120, Undefined                              */      0,                                                  state_undefined     },
1348     { /*121, Undefined                              */      0,                                                  state_undefined     },
1349     { /*122, Undefined                              */      0,                                                  state_undefined     },
1350     { /*123, Undefined                              */      0,                                                  state_undefined     },
1351     { /*124, Undefined                              */      0,                                                  state_undefined     },
1352     { /*125, Undefined                              */      0,                                                  state_undefined     },
1353     { /*126, Undefined                              */      0,                                                  state_undefined     },
1354     { /*127, Undefined                              */      0,                                                  state_undefined     },
1355     /* Big hole ends */
1356     { /*128, WINED3DRS_WRAP0                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1357     { /*129, WINED3DRS_WRAP1                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1358     { /*130, WINED3DRS_WRAP2                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1359     { /*131, WINED3DRS_WRAP3                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1360     { /*132, WINED3DRS_WRAP4                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1361     { /*133, WINED3DRS_WRAP5                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1362     { /*134, WINED3DRS_WRAP6                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1363     { /*135, WINED3DRS_WRAP7                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1364     { /*136, WINED3DRS_CLIPPING                     */      STATE_RENDER(WINED3DRS_CLIPPING),                   state_clipping      },
1365     { /*137, WINED3DRS_LIGHTING                     */      STATE_RENDER(WINED3DRS_LIGHTING) /* Vertex decl! */,state_lighting      },
1366     { /*138, WINED3DRS_EXTENTS                      */      STATE_RENDER(WINED3DRS_EXTENTS),                    state_unknown       },
1367     { /*139, WINED3DRS_AMBIENT                      */      STATE_RENDER(WINED3DRS_AMBIENT),                    state_ambient       },
1368     { /*140, WINED3DRS_FOGVERTEXMODE                */      STATE_RENDER(WINED3DRS_FOGENABLE),                  state_fog           },
1369     { /*141, WINED3DRS_COLORVERTEX                  */      STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      },
1370     { /*142, WINED3DRS_LOCALVIEWER                  */      STATE_RENDER(WINED3DRS_LOCALVIEWER),                state_localviewer   },
1371     { /*143, WINED3DRS_NORMALIZENORMALS             */      STATE_RENDER(WINED3DRS_NORMALIZENORMALS),           state_normalize     },
1372     { /*144, WINED3DRS_COLORKEYBLENDENABLE          */      STATE_RENDER(WINED3DRS_COLORKEYBLENDENABLE),        state_unknown       },
1373     { /*145, WINED3DRS_DIFFUSEMATERIALSOURCE        */      STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      },
1374     { /*146, WINED3DRS_SPECULARMATERIALSOURCE       */      STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      },
1375     { /*147, WINED3DRS_AMBIENTMATERIALSOURCE        */      STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      },
1376     { /*148, WINED3DRS_EMISSIVEMATERIALSOURCE       */      STATE_RENDER(WINED3DRS_COLORVERTEX),                state_colormat      },
1377     { /*149, Undefined                              */      0,                                                  state_undefined     },
1378     { /*150, Undefined                              */      0,                                                  state_undefined     },
1379     { /*151, WINED3DRS_VERTEXBLEND                  */      0,                                                  state_nogl          },
1380     { /*152, WINED3DRS_CLIPPLANEENABLE              */      STATE_RENDER(WINED3DRS_CLIPPING),                   state_clipping      },
1381     { /*153, WINED3DRS_SOFTWAREVERTEXPROCESSING     */      0,                                                  state_nogl          },
1382     { /*154, WINED3DRS_POINTSIZE                    */      STATE_RENDER(WINED3DRS_POINTSIZE),                  state_psize         },
1383     { /*155, WINED3DRS_POINTSIZE_MIN                */      STATE_RENDER(WINED3DRS_POINTSIZE_MIN),              state_psizemin      },
1384     { /*156, WINED3DRS_POINTSPRITEENABLE            */      STATE_RENDER(WINED3DRS_POINTSPRITEENABLE),          state_pointsprite   },
1385     { /*157, WINED3DRS_POINTSCALEENABLE             */      STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        },
1386     { /*158, WINED3DRS_POINTSCALE_A                 */      STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        },
1387     { /*159, WINED3DRS_POINTSCALE_B                 */      STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        },
1388     { /*160, WINED3DRS_POINTSCALE_C                 */      STATE_RENDER(WINED3DRS_POINTSCALEENABLE),           state_pscale        },
1389     { /*161, WINED3DRS_MULTISAMPLEANTIALIAS         */      STATE_RENDER(WINED3DRS_MULTISAMPLEANTIALIAS),       state_multisampleaa },
1390     { /*162, WINED3DRS_MULTISAMPLEMASK              */      STATE_RENDER(WINED3DRS_MULTISAMPLEMASK),            state_multisampmask },
1391     { /*163, WINED3DRS_PATCHEDGESTYLE               */      STATE_RENDER(WINED3DRS_PATCHEDGESTYLE),             state_patchededge   },
1392     { /*164, WINED3DRS_PATCHSEGMENTS                */      STATE_RENDER(WINED3DRS_PATCHSEGMENTS),              state_patchsegments },
1393     { /*165, WINED3DRS_DEBUGMONITORTOKEN            */      STATE_RENDER(WINED3DRS_DEBUGMONITORTOKEN),          state_nogl          },
1394     { /*166, WINED3DRS_POINTSIZE_MAX                */      STATE_RENDER(WINED3DRS_POINTSIZE_MAX),              state_psizemax      },
1395     { /*167, WINED3DRS_INDEXEDVERTEXBLENDENABLE     */      0,                                                  state_nogl          },
1396     { /*168, WINED3DRS_COLORWRITEENABLE             */      STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    },
1397     { /*169, Undefined                              */      0,                                                  state_undefined     },
1398     { /*170, WINED3DRS_TWEENFACTOR                  */      0,                                                  state_nogl          },
1399     { /*171, WINED3DRS_BLENDOP                      */      STATE_RENDER(WINED3DRS_BLENDOP),                    state_blendop       },
1400     { /*172, WINED3DRS_POSITIONDEGREE               */      STATE_RENDER(WINED3DRS_POSITIONDEGREE),             state_positiondegree},
1401     { /*173, WINED3DRS_NORMALDEGREE                 */      STATE_RENDER(WINED3DRS_NORMALDEGREE),               state_normaldegree  },
1402       /*172, WINED3DRS_POSITIONORDER                */      /* Value assigned to 2 state names */
1403       /*173, WINED3DRS_NORMALORDER                  */      /* Value assigned to 2 state names */
1404     { /*174, WINED3DRS_SCISSORTESTENABLE            */      STATE_RENDER(WINED3DRS_SCISSORTESTENABLE),          state_scissor       },
1405     { /*175, WINED3DRS_SLOPESCALEDEPTHBIAS          */      STATE_RENDER(WINED3DRS_DEPTHBIAS),                  state_depthbias     },
1406     { /*176, WINED3DRS_ANTIALIASEDLINEENABLE        */      STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         },
1407     { /*177, undefined                              */      0,                                                  state_undefined     },
1408     { /*178, WINED3DRS_MINTESSELLATIONLEVEL         */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1409     { /*179, WINED3DRS_MAXTESSELLATIONLEVEL         */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1410     { /*180, WINED3DRS_ADAPTIVETESS_X               */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1411     { /*181, WINED3DRS_ADAPTIVETESS_Y               */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1412     { /*182, WINED3DRS_ADAPTIVETESS_Z               */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1413     { /*183, WINED3DRS_ADAPTIVETESS_W               */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1414     { /*184, WINED3DRS_ENABLEADAPTIVETESSELLATION   */      STATE_RENDER(WINED3DRS_ENABLEADAPTIVETESSELLATION), state_tessellation  },
1415     { /*185, WINED3DRS_TWOSIDEDSTENCILMODE          */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1416     { /*186, WINED3DRS_CCW_STENCILFAIL              */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1417     { /*187, WINED3DRS_CCW_STENCILZFAIL             */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1418     { /*188, WINED3DRS_CCW_STENCILPASS              */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1419     { /*189, WINED3DRS_CCW_STENCILFUNC              */      STATE_RENDER(WINED3DRS_STENCILENABLE),              state_stencil       },
1420     { /*190, WINED3DRS_COLORWRITEENABLE1            */      STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    },
1421     { /*191, WINED3DRS_COLORWRITEENABLE2            */      STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    },
1422     { /*192, WINED3DRS_COLORWRITEENABLE3            */      STATE_RENDER(WINED3DRS_COLORWRITEENABLE),           state_colorwrite    },
1423     { /*193, WINED3DRS_BLENDFACTOR                  */      STATE_RENDER(WINED3DRS_ALPHABLENDENABLE),           state_blend         },
1424     { /*194, WINED3DRS_SRGBWRITEENABLE              */      STATE_RENDER(WINED3DRS_SRGBWRITEENABLE),            state_srgbwrite     },
1425     { /*195, WINED3DRS_DEPTHBIAS                    */      STATE_RENDER(WINED3DRS_DEPTHBIAS),                  state_depthbias     },
1426     { /*196, undefined                              */      0,                                                  state_undefined     },
1427     { /*197, undefined                              */      0,                                                  state_undefined     },
1428     { /*198, WINED3DRS_WRAP8                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1429     { /*199, WINED3DRS_WRAP9                        */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1430     { /*200, WINED3DRS_WRAP10                       */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1431     { /*201, WINED3DRS_WRAP11                       */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1432     { /*202, WINED3DRS_WRAP12                       */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1433     { /*203, WINED3DRS_WRAP13                       */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1434     { /*204, WINED3DRS_WRAP14                       */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1435     { /*205, WINED3DRS_WRAP15                       */      STATE_RENDER(WINED3DRS_WRAP0),                      state_wrap          },
1436     { /*206, WINED3DRS_SEPARATEALPHABLENDENABLE     */      STATE_RENDER(WINED3DRS_SEPARATEALPHABLENDENABLE),   state_seperateblend },
1437     { /*207, WINED3DRS_SRCBLENDALPHA                */      STATE_RENDER(WINED3DRS_SEPARATEALPHABLENDENABLE),   state_seperateblend },
1438     { /*208, WINED3DRS_DESTBLENDALPHA               */      STATE_RENDER(WINED3DRS_SEPARATEALPHABLENDENABLE),   state_seperateblend },
1439     { /*209, WINED3DRS_BLENDOPALPHA                 */      STATE_RENDER(WINED3DRS_SEPARATEALPHABLENDENABLE),   state_seperateblend },
1440 };