state.c 347 KB
Newer Older
1 2 3
/*
 * Direct3D state management
 *
4 5 6 7 8 9
 * Copyright 2002 Lionel Ulmer
 * Copyright 2002-2005 Jason Edmeades
 * Copyright 2003-2004 Raphael Junqueira
 * Copyright 2004 Christian Costa
 * Copyright 2005 Oliver Stieber
 * Copyright 2006 Henri Verbeet
10
 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
11
 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
29 30
#include "wine/port.h"

31 32 33 34
#include <stdio.h>
#ifdef HAVE_FLOAT_H
# include <float.h>
#endif
35

36 37 38
#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39
WINE_DECLARE_DEBUG_CHANNEL(d3d_shader);
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
ULONG CDECL wined3d_rasterizer_state_incref(struct wined3d_rasterizer_state *state)
{
    ULONG refcount = InterlockedIncrement(&state->refcount);

    TRACE("%p increasing refcount to %u.\n", state, refcount);

    return refcount;
}

static void wined3d_rasterizer_state_destroy_object(void *object)
{
    HeapFree(GetProcessHeap(), 0, object);
}

ULONG CDECL wined3d_rasterizer_state_decref(struct wined3d_rasterizer_state *state)
{
    ULONG refcount = InterlockedDecrement(&state->refcount);
    struct wined3d_device *device = state->device;

    TRACE("%p decreasing refcount to %u.\n", state, refcount);

    if (!refcount)
        wined3d_cs_emit_destroy_object(device->cs, wined3d_rasterizer_state_destroy_object, state);

    return refcount;
}

HRESULT CDECL wined3d_rasterizer_state_create(struct wined3d_device *device,
        const struct wined3d_rasterizer_state_desc *desc, struct wined3d_rasterizer_state **state)
{
    struct wined3d_rasterizer_state *object;

    TRACE("device %p, desc %p, state %p.\n", device, desc, state);

    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
        return E_OUTOFMEMORY;

    object->refcount = 1;
    object->desc = *desc;
    object->device = device;

    TRACE("Created rasterizer state %p.\n", object);
    *state = object;

    return WINED3D_OK;
}

88
/* Context activation for state handler is done by the caller. */
89

90
static void state_undefined(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
91
{
92
    ERR("Undefined state %s (%#x).\n", debug_d3dstate(state_id), state_id);
93 94
}

95
void state_nop(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
96
{
97
    TRACE("%s: nop in current pipe config.\n", debug_d3dstate(state_id));
98 99
}

100
static void state_fillmode(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
101
{
102
    enum wined3d_fill_mode mode = state->render_states[WINED3D_RS_FILLMODE];
103
    const struct wined3d_gl_info *gl_info = context->gl_info;
104

105 106
    switch (mode)
    {
107
        case WINED3D_FILL_POINT:
108
            gl_info->gl_ops.gl.p_glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
109 110
            checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)");
            break;
111
        case WINED3D_FILL_WIREFRAME:
112
            gl_info->gl_ops.gl.p_glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
113 114
            checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)");
            break;
115
        case WINED3D_FILL_SOLID:
116
            gl_info->gl_ops.gl.p_glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
117 118 119
            checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
            break;
        default:
120
            FIXME("Unrecognized fill mode %#x.\n", mode);
121 122 123
    }
}

124
static void state_lighting(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
125
{
126 127
    const struct wined3d_gl_info *gl_info = context->gl_info;

128 129 130 131 132 133
    /* Lighting is not enabled if transformed vertices are drawn, but lighting
     * does not affect the stream sources, so it is not grouped for
     * performance reasons. This state reads the decoded vertex declaration,
     * so if it is dirty don't do anything. The vertex declaration applying
     * function calls this function for updating. */
    if (isStateDirty(context, STATE_VDECL))
134
        return;
135

136
    if (state->render_states[WINED3D_RS_LIGHTING]
137
            && !context->stream_info.position_transformed)
138
    {
139
        gl_info->gl_ops.gl.p_glEnable(GL_LIGHTING);
140
        checkGLcall("glEnable GL_LIGHTING");
141 142 143 144
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
145 146 147 148
        checkGLcall("glDisable GL_LIGHTING");
    }
}

149
static void state_zenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
150
{
151
    enum wined3d_depth_buffer_type zenable = state->render_states[WINED3D_RS_ZENABLE];
152
    const struct wined3d_gl_info *gl_info = context->gl_info;
153

154
    /* No z test without depth stencil buffers */
155
    if (!state->fb->depth_stencil)
156
    {
157
        TRACE("No Z buffer - disabling depth test\n");
158
        zenable = WINED3D_ZB_FALSE;
159 160
    }

161
    switch (zenable)
162
    {
163
        case WINED3D_ZB_FALSE:
164
            gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
165 166
            checkGLcall("glDisable GL_DEPTH_TEST");
            break;
167
        case WINED3D_ZB_TRUE:
168
            gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_TEST);
169 170
            checkGLcall("glEnable GL_DEPTH_TEST");
            break;
171
        case WINED3D_ZB_USEW:
172
            gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_TEST);
173 174 175 176
            checkGLcall("glEnable GL_DEPTH_TEST");
            FIXME("W buffer is not well handled\n");
            break;
        default:
177 178 179 180
            FIXME("Unrecognized depth buffer type %#x.\n", zenable);
            break;
    }

181
    if (context->last_was_rhw && !isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
182
        context_apply_state(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
183 184
}

185
static void state_cullmode(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
186
{
187 188
    const struct wined3d_gl_info *gl_info = context->gl_info;

189 190
    /* glFrontFace() is set in context.c at context init and on an
     * offscreen / onscreen rendering switch. */
191
    switch (state->render_states[WINED3D_RS_CULLMODE])
192
    {
193
        case WINED3D_CULL_NONE:
194
            gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
195 196
            checkGLcall("glDisable GL_CULL_FACE");
            break;
197
        case WINED3D_CULL_FRONT:
198
            gl_info->gl_ops.gl.p_glEnable(GL_CULL_FACE);
199
            checkGLcall("glEnable GL_CULL_FACE");
200
            gl_info->gl_ops.gl.p_glCullFace(GL_FRONT);
201
            checkGLcall("glCullFace(GL_FRONT)");
202
            break;
203
        case WINED3D_CULL_BACK:
204
            gl_info->gl_ops.gl.p_glEnable(GL_CULL_FACE);
205
            checkGLcall("glEnable GL_CULL_FACE");
206
            gl_info->gl_ops.gl.p_glCullFace(GL_BACK);
207
            checkGLcall("glCullFace(GL_BACK)");
208 209
            break;
        default:
210
            FIXME("Unrecognized cull mode %#x.\n",
211
                    state->render_states[WINED3D_RS_CULLMODE]);
212 213 214
    }
}

215
void state_shademode(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
216
{
217 218
    const struct wined3d_gl_info *gl_info = context->gl_info;

219
    switch (state->render_states[WINED3D_RS_SHADEMODE])
220
    {
221
        case WINED3D_SHADE_FLAT:
222
            gl_info->gl_ops.gl.p_glShadeModel(GL_FLAT);
223 224
            checkGLcall("glShadeModel(GL_FLAT)");
            break;
225
        case WINED3D_SHADE_GOURAUD:
226 227 228
        /* WINED3D_SHADE_PHONG in practice is the same as WINED3D_SHADE_GOURAUD
         * in D3D. */
        case WINED3D_SHADE_PHONG:
229
            gl_info->gl_ops.gl.p_glShadeModel(GL_SMOOTH);
230 231 232
            checkGLcall("glShadeModel(GL_SMOOTH)");
            break;
        default:
233
            FIXME("Unrecognized shade mode %#x.\n",
234
                    state->render_states[WINED3D_RS_SHADEMODE]);
235 236 237
    }
}

238
static void state_ditherenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
239
{
240 241
    const struct wined3d_gl_info *gl_info = context->gl_info;

242
    if (state->render_states[WINED3D_RS_DITHERENABLE])
243
    {
244
        gl_info->gl_ops.gl.p_glEnable(GL_DITHER);
245
        checkGLcall("glEnable GL_DITHER");
246 247 248
    }
    else
    {
249
        gl_info->gl_ops.gl.p_glDisable(GL_DITHER);
250 251 252 253
        checkGLcall("glDisable GL_DITHER");
    }
}

254
static void state_zwriteenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
255
{
256 257
    const struct wined3d_gl_info *gl_info = context->gl_info;

258
    if (state->render_states[WINED3D_RS_ZWRITEENABLE])
259
    {
260
        gl_info->gl_ops.gl.p_glDepthMask(1);
261
        checkGLcall("glDepthMask(1)");
262 263 264
    }
    else
    {
265
        gl_info->gl_ops.gl.p_glDepthMask(0);
266 267 268 269
        checkGLcall("glDepthMask(0)");
    }
}

270
GLenum wined3d_gl_compare_func(enum wined3d_cmp_func f)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
{
    switch (f)
    {
        case WINED3D_CMP_NEVER:
            return GL_NEVER;
        case WINED3D_CMP_LESS:
            return GL_LESS;
        case WINED3D_CMP_EQUAL:
            return GL_EQUAL;
        case WINED3D_CMP_LESSEQUAL:
            return GL_LEQUAL;
        case WINED3D_CMP_GREATER:
            return GL_GREATER;
        case WINED3D_CMP_NOTEQUAL:
            return GL_NOTEQUAL;
        case WINED3D_CMP_GREATEREQUAL:
            return GL_GEQUAL;
        case WINED3D_CMP_ALWAYS:
            return GL_ALWAYS;
        default:
            FIXME("Unrecognized compare function %#x.\n", f);
            return GL_NONE;
    }
}

296
static void state_zfunc(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
297
{
298
    GLenum depth_func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ZFUNC]);
299
    const struct wined3d_gl_info *gl_info = context->gl_info;
300

301
    if (!depth_func) return;
302

303
    gl_info->gl_ops.gl.p_glDepthFunc(depth_func);
304
    checkGLcall("glDepthFunc");
305 306
}

307
static void state_ambient(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
308
{
309
    const struct wined3d_gl_info *gl_info = context->gl_info;
310
    struct wined3d_color color;
311

312
    wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
313
    TRACE("Setting ambient to %s.\n", debug_color(&color));
314
    gl_info->gl_ops.gl.p_glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &color.r);
315 316 317
    checkGLcall("glLightModel for MODEL_AMBIENT");
}

318 319 320 321 322
static void state_blendop_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
{
    WARN("Unsupported in local OpenGL implementation: glBlendEquation\n");
}

323
static GLenum gl_blend_op(const struct wined3d_gl_info *gl_info, enum wined3d_blend_op op)
324 325 326
{
    switch (op)
    {
327
        case WINED3D_BLEND_OP_ADD:
328
            return GL_FUNC_ADD;
329
        case WINED3D_BLEND_OP_SUBTRACT:
330
            return gl_info->supported[EXT_BLEND_SUBTRACT] ? GL_FUNC_SUBTRACT : GL_FUNC_ADD;
331
        case WINED3D_BLEND_OP_REVSUBTRACT:
332
            return gl_info->supported[EXT_BLEND_SUBTRACT] ? GL_FUNC_REVERSE_SUBTRACT : GL_FUNC_ADD;
333
        case WINED3D_BLEND_OP_MIN:
334
            return gl_info->supported[EXT_BLEND_MINMAX] ? GL_MIN : GL_FUNC_ADD;
335
        case WINED3D_BLEND_OP_MAX:
336
            return gl_info->supported[EXT_BLEND_MINMAX] ? GL_MAX : GL_FUNC_ADD;
337 338
        default:
            FIXME("Unhandled blend op %#x.\n", op);
339
            return GL_FUNC_ADD;
340 341 342 343 344 345 346 347 348 349
    }
}

static void state_blendop(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
{
    const struct wined3d_gl_info *gl_info = context->gl_info;
    GLenum blend_equation_alpha = GL_FUNC_ADD_EXT;
    GLenum blend_equation = GL_FUNC_ADD_EXT;

    /* BLENDOPALPHA requires GL_EXT_blend_equation_separate, so make sure it is around */
350
    if (state->render_states[WINED3D_RS_BLENDOPALPHA]
351 352
            && !gl_info->supported[EXT_BLEND_EQUATION_SEPARATE])
    {
353
        WARN("Unsupported in local OpenGL implementation: glBlendEquationSeparate.\n");
354 355 356
        return;
    }

357 358
    blend_equation = gl_blend_op(gl_info, state->render_states[WINED3D_RS_BLENDOP]);
    blend_equation_alpha = gl_blend_op(gl_info, state->render_states[WINED3D_RS_BLENDOPALPHA]);
359 360
    TRACE("blend_equation %#x, blend_equation_alpha %#x.\n", blend_equation, blend_equation_alpha);

361
    if (state->render_states[WINED3D_RS_SEPARATEALPHABLENDENABLE])
362
    {
363 364
        GL_EXTCALL(glBlendEquationSeparate(blend_equation, blend_equation_alpha));
        checkGLcall("glBlendEquationSeparate");
365 366 367
    }
    else
    {
368
        GL_EXTCALL(glBlendEquation(blend_equation));
369 370 371 372
        checkGLcall("glBlendEquation");
    }
}

373
static GLenum gl_blend_factor(enum wined3d_blend factor, const struct wined3d_format *dst_format)
374 375 376
{
    switch (factor)
    {
377
        case WINED3D_BLEND_ZERO:
378
            return GL_ZERO;
379
        case WINED3D_BLEND_ONE:
380
            return GL_ONE;
381
        case WINED3D_BLEND_SRCCOLOR:
382
            return GL_SRC_COLOR;
383
        case WINED3D_BLEND_INVSRCCOLOR:
384
            return GL_ONE_MINUS_SRC_COLOR;
385
        case WINED3D_BLEND_SRCALPHA:
386
            return GL_SRC_ALPHA;
387
        case WINED3D_BLEND_INVSRCALPHA:
388
            return GL_ONE_MINUS_SRC_ALPHA;
389
        case WINED3D_BLEND_DESTCOLOR:
390
            return GL_DST_COLOR;
391
        case WINED3D_BLEND_INVDESTCOLOR:
392 393 394 395
            return GL_ONE_MINUS_DST_COLOR;
        /* To compensate for the lack of format switching with backbuffer
         * offscreen rendering, and with onscreen rendering, we modify the
         * alpha test parameters for (INV)DESTALPHA if the render target
396
         * doesn't support alpha blending. A nonexistent alpha channel
397 398 399
         * returns 1.0, so WINED3D_BLEND_DESTALPHA becomes GL_ONE, and
         * WINED3D_BLEND_INVDESTALPHA becomes GL_ZERO. */
        case WINED3D_BLEND_DESTALPHA:
400
            return dst_format->alpha_size ? GL_DST_ALPHA : GL_ONE;
401
        case WINED3D_BLEND_INVDESTALPHA:
402
            return dst_format->alpha_size ? GL_ONE_MINUS_DST_ALPHA : GL_ZERO;
403
        case WINED3D_BLEND_SRCALPHASAT:
404
            return GL_SRC_ALPHA_SATURATE;
405
        case WINED3D_BLEND_BLENDFACTOR:
406
            return GL_CONSTANT_COLOR_EXT;
407
        case WINED3D_BLEND_INVBLENDFACTOR:
408 409 410 411 412 413 414
            return GL_ONE_MINUS_CONSTANT_COLOR_EXT;
        default:
            FIXME("Unhandled blend factor %#x.\n", factor);
            return GL_NONE;
    }
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static void gl_blend_from_d3d(GLenum *src_blend, GLenum *dst_blend,
        enum wined3d_blend d3d_src_blend, enum wined3d_blend d3d_dst_blend,
        const struct wined3d_format *rt_format)
{
    /* WINED3D_BLEND_BOTHSRCALPHA and WINED3D_BLEND_BOTHINVSRCALPHA are legacy
     * source blending values which are still valid up to d3d9. They should
     * not occur as dest blend values. */
    if (d3d_src_blend == WINED3D_BLEND_BOTHSRCALPHA)
    {
        *src_blend = GL_SRC_ALPHA;
        *dst_blend = GL_ONE_MINUS_SRC_ALPHA;
    }
    else if (d3d_src_blend == WINED3D_BLEND_BOTHINVSRCALPHA)
    {
        *src_blend = GL_ONE_MINUS_SRC_ALPHA;
        *dst_blend = GL_SRC_ALPHA;
    }
    else
    {
        *src_blend = gl_blend_factor(d3d_src_blend, rt_format);
        *dst_blend = gl_blend_factor(d3d_dst_blend, rt_format);
    }
}

439
static void state_blend(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
440
{
441
    const struct wined3d_gl_info *gl_info = context->gl_info;
442
    const struct wined3d_format *rt_format;
443
    BOOL enable_blend, enable_line_smooth;
444
    GLenum src_blend, dst_blend;
445 446
    unsigned int rt_fmt_flags;

447 448 449
    enable_line_smooth = state->render_states[WINED3D_RS_EDGEANTIALIAS]
            || state->render_states[WINED3D_RS_ANTIALIASEDLINEENABLE];
    if (enable_line_smooth)
450
    {
451 452 453 454 455 456 457
        gl_info->gl_ops.gl.p_glEnable(GL_LINE_SMOOTH);
        checkGLcall("glEnable(GL_LINE_SMOOTH)");
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_LINE_SMOOTH);
        checkGLcall("glDisable(GL_LINE_SMOOTH)");
458 459
    }

460 461
    enable_blend = state->fb->render_targets[0] && state->render_states[WINED3D_RS_ALPHABLENDENABLE];
    if (enable_blend)
462
    {
463 464 465
        rt_format = state->fb->render_targets[0]->format;
        rt_fmt_flags = state->fb->render_targets[0]->format_flags;

466 467
        /* Disable blending in all cases even without pixelshaders.
         * With blending on we could face a big performance penalty.
468
         * The d3d9 visual test confirms the behavior. */
469
        if (context->render_offscreen && !(rt_fmt_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
470 471 472 473 474 475 476
            enable_blend = FALSE;
    }

    if (enable_blend)
    {
        gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
        checkGLcall("glEnable(GL_BLEND)");
477 478 479 480
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
481 482 483
        checkGLcall("glDisable(GL_BLEND)");
        if (enable_line_smooth)
            WARN("LINE/EDGEANTIALIAS enabled with disabled blending.\n");
484
        return;
485
    }
486

487 488 489
    gl_blend_from_d3d(&src_blend, &dst_blend,
            state->render_states[WINED3D_RS_SRCBLEND],
            state->render_states[WINED3D_RS_DESTBLEND], rt_format);
490

491 492 493
    /* According to the red book, GL_LINE_SMOOTH needs GL_BLEND with specific
     * blending parameters to work. */
    if (enable_line_smooth)
494
    {
495
        if (src_blend != GL_SRC_ALPHA)
496
            WARN("LINE/EDGEANTIALIAS enabled, but unexpected src blending param.\n");
497
        if (dst_blend != GL_ONE_MINUS_SRC_ALPHA && dst_blend != GL_ONE)
498
            WARN("LINE/EDGEANTIALIAS enabled, but unexpected dst blending param.\n");
499 500
    }

501
    /* Re-apply BLENDOP(ALPHA) because of a possible SEPARATEALPHABLENDENABLE change */
502 503
    if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_BLENDOP)))
        state_blendop(context, state, STATE_RENDER(WINED3D_RS_BLENDOPALPHA));
504

505
    if (state->render_states[WINED3D_RS_SEPARATEALPHABLENDENABLE])
506
    {
507
        GLenum src_blend_alpha, dst_blend_alpha;
508 509

        /* Separate alpha blending requires GL_EXT_blend_function_separate, so make sure it is around */
510 511
        if (!context->gl_info->supported[EXT_BLEND_FUNC_SEPARATE])
        {
512
            WARN("Unsupported in local OpenGL implementation: glBlendFuncSeparate.\n");
513 514 515
            return;
        }

516 517 518
        gl_blend_from_d3d(&src_blend_alpha, &dst_blend_alpha,
                state->render_states[WINED3D_RS_SRCBLENDALPHA],
                state->render_states[WINED3D_RS_DESTBLENDALPHA], rt_format);
519

520
        GL_EXTCALL(glBlendFuncSeparate(src_blend, dst_blend, src_blend_alpha, dst_blend_alpha));
521
        checkGLcall("glBlendFuncSeparate");
522 523 524
    }
    else
    {
525 526
        TRACE("glBlendFunc src=%x, dst=%x.\n", src_blend, dst_blend);
        gl_info->gl_ops.gl.p_glBlendFunc(src_blend, dst_blend);
527 528
        checkGLcall("glBlendFunc");
    }
529

530
    /* Colorkey fixup for stage 0 alphaop depends on
531 532
     * WINED3D_RS_ALPHABLENDENABLE state, so it may need updating. */
    if (state->render_states[WINED3D_RS_COLORKEYENABLE])
533
        context_apply_state(context, state, STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP));
534 535
}

536
static void state_blendfactor_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
537
{
538
    WARN("Unsupported in local OpenGL implementation: glBlendColor.\n");
539 540
}

541
static void state_blendfactor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
542
{
543
    const struct wined3d_gl_info *gl_info = context->gl_info;
544
    struct wined3d_color color;
545

546 547
    TRACE("Setting blend factor to %#x.\n", state->render_states[WINED3D_RS_BLENDFACTOR]);

548 549
    wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_BLENDFACTOR]);
    GL_EXTCALL(glBlendColor(color.r, color.g, color.b, color.a));
550 551 552
    checkGLcall("glBlendColor");
}

553
void state_alpha_test(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
554
{
555
    const struct wined3d_gl_info *gl_info = context->gl_info;
556 557 558 559
    int glParm = 0;
    float ref;
    BOOL enable_ckey = FALSE;

560
    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
561

562 563 564 565 566 567 568
    /* Find out if the texture on the first stage has a ckey set. The alpha
     * state func reads the texture settings, even though alpha and texture
     * are not grouped together. This is to avoid making a huge alpha +
     * texture + texture stage + ckey block due to the hardly used
     * WINED3D_RS_COLORKEYENABLE state(which is d3d <= 3 only). The texture
     * function will call alpha in case it finds some texture + colorkeyenable
     * combination which needs extra care. */
569
    if (state->textures[0] && (state->textures[0]->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
570
        enable_ckey = TRUE;
571

572
    if (enable_ckey || context->last_was_ckey)
573
        context_apply_state(context, state, STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP));
574 575
    context->last_was_ckey = enable_ckey;

576 577
    if (state->render_states[WINED3D_RS_ALPHATESTENABLE]
            || (state->render_states[WINED3D_RS_COLORKEYENABLE] && enable_ckey))
578
    {
579
        gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
580
        checkGLcall("glEnable GL_ALPHA_TEST");
581 582 583 584
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
585 586 587 588 589 590 591
        checkGLcall("glDisable GL_ALPHA_TEST");
        /* Alpha test is disabled, don't bother setting the params - it will happen on the next
         * enable call
         */
        return;
    }

592
    if (state->render_states[WINED3D_RS_COLORKEYENABLE] && enable_ckey)
593
    {
594
        glParm = GL_NOTEQUAL;
595
        ref = 0.0f;
596 597 598
    }
    else
    {
599
        ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
600
        glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
601
    }
602 603 604
    if (glParm)
    {
        gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
605 606 607 608
        checkGLcall("glAlphaFunc");
    }
}

609
void state_clipping(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
610
{
611
    const struct wined3d_gl_info *gl_info = context->gl_info;
612 613
    unsigned int clipplane_count = gl_info->limits.clipplanes;
    unsigned int i, enable_mask, disable_mask;
614

615 616 617 618 619 620 621 622 623 624 625 626 627
    if (use_vs(state) && !context->d3d_info->vs_clipping)
    {
        static BOOL warned;

        /* The OpenGL spec says that clipping planes are disabled when using
         * shaders. Direct3D planes aren't, so that is an issue. The MacOS ATI
         * driver keeps clipping planes activated with shaders in some
         * conditions I got sick of tracking down. The shader state handler
         * disables all clip planes because of that - don't do anything here
         * and keep them disabled. */
        if (state->render_states[WINED3D_RS_CLIPPLANEENABLE] && !warned++)
            FIXME("Clipping not supported with vertex shaders\n");
        return;
628 629
    }

630 631 632 633
    /* glEnable(GL_CLIP_PLANEx) doesn't apply to (ARB backend) vertex shaders.
     * The enabled / disabled planes are hardcoded into the shader. Update the
     * shader to update the enabled clipplanes. In case of fixed function, we
     * need to update the clipping field from ffp_vertex_settings. */
634
    context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
635

636
    /* TODO: Keep track of previously enabled clipplanes to avoid unnecessary resetting
637 638 639 640 641 642
     * of already set values
     */

    /* If enabling / disabling all
     * TODO: Is this correct? Doesn't D3DRS_CLIPPING disable clipping on the viewport frustrum?
     */
643
    if (state->render_states[WINED3D_RS_CLIPPING])
644
    {
645 646
        enable_mask = state->render_states[WINED3D_RS_CLIPPLANEENABLE];
        disable_mask = ~state->render_states[WINED3D_RS_CLIPPLANEENABLE];
647 648 649
    }
    else
    {
650 651
        enable_mask = 0;
        disable_mask = ~0u;
652 653
    }

654 655 656 657 658 659 660 661 662
    if (clipplane_count < 32)
    {
        enable_mask &= (1u << clipplane_count) - 1;
        disable_mask &= (1u << clipplane_count) - 1;
    }

    for (i = 0; enable_mask && i < clipplane_count; enable_mask >>= 1, ++i)
        if (enable_mask & 1)
            gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
663 664
    checkGLcall("clip plane enable");

665 666 667
    for (i = 0; disable_mask && i < clipplane_count; disable_mask >>= 1, ++i)
        if (disable_mask & 1)
            gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
668
    checkGLcall("clip plane disable");
669 670
}

671
static void state_specularenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
672
{
673
    const struct wined3d_gl_info *gl_info = context->gl_info;
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    /* Originally this used glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR)
     * and (GL_LIGHT_MODEL_COLOR_CONTROL,GL_SINGLE_COLOR) to swap between enabled/disabled
     * specular color. This is wrong:
     * Separate specular color means the specular colour is maintained separately, whereas
     * single color means it is merged in. However in both cases they are being used to
     * some extent.
     * To disable specular color, set it explicitly to black and turn off GL_COLOR_SUM_EXT
     * NOTE: If not supported don't give FIXMEs the impact is really minimal and very few people are
     * running 1.4 yet!
     *
     *
     * If register combiners are enabled, enabling / disabling GL_COLOR_SUM has no effect.
     * Instead, we need to setup the FinalCombiner properly.
     *
     * The default setup for the FinalCombiner is:
     *
     * <variable>       <input>                             <mapping>               <usage>
     * GL_VARIABLE_A_NV GL_FOG,                             GL_UNSIGNED_IDENTITY_NV GL_ALPHA
     * GL_VARIABLE_B_NV GL_SPARE0_PLUS_SECONDARY_COLOR_NV   GL_UNSIGNED_IDENTITY_NV GL_RGB
     * GL_VARIABLE_C_NV GL_FOG                              GL_UNSIGNED_IDENTITY_NV GL_RGB
     * GL_VARIABLE_D_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
     * GL_VARIABLE_E_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
     * GL_VARIABLE_F_NV GL_ZERO                             GL_UNSIGNED_IDENTITY_NV GL_RGB
     * GL_VARIABLE_G_NV GL_SPARE0_NV                        GL_UNSIGNED_IDENTITY_NV GL_ALPHA
     *
     * That's pretty much fine as it is, except for variable B, which needs to take
     * either GL_SPARE0_PLUS_SECONDARY_COLOR_NV or GL_SPARE0_NV, depending on
701
     * whether WINED3D_RS_SPECULARENABLE is enabled or not.
702 703
     */

704
    TRACE("Setting specular enable state and materials\n");
705
    if (state->render_states[WINED3D_RS_SPECULARENABLE])
706
    {
707
        gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (float *)&state->material.specular);
708
        checkGLcall("glMaterialfv");
709

710
        if (state->material.power > gl_info->limits.shininess)
711
        {
712
            /* glMaterialf man page says that the material says that GL_SHININESS must be between 0.0
713
             * and 128.0, although in d3d neither -1 nor 129 produce an error. GL_NV_max_light_exponent
714 715
             * allows bigger values. If the extension is supported, gl_info->limits.shininess contains the
             * value reported by the extension, otherwise 128. For values > gl_info->limits.shininess clamp
Austin English's avatar
Austin English committed
716
             * them, it should be safe to do so without major visual distortions.
717
             */
718
            WARN("Material power = %.8e, limit %.8e\n", state->material.power, gl_info->limits.shininess);
719
            gl_info->gl_ops.gl.p_glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, gl_info->limits.shininess);
720 721 722
        }
        else
        {
723
            gl_info->gl_ops.gl.p_glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, state->material.power);
724
        }
725
        checkGLcall("glMaterialf(GL_SHININESS)");
726

727
        if (gl_info->supported[EXT_SECONDARY_COLOR])
728
            gl_info->gl_ops.gl.p_glEnable(GL_COLOR_SUM_EXT);
729
        else
730 731 732
            TRACE("Specular colors cannot be enabled in this version of opengl\n");
        checkGLcall("glEnable(GL_COLOR_SUM)");

733
        if (gl_info->supported[NV_REGISTER_COMBINERS])
734
        {
735 736 737 738
            GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_PLUS_SECONDARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
            checkGLcall("glFinalCombinerInputNV()");
        }
    } else {
739
        static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
740 741

        /* for the case of enabled lighting: */
742
        gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &black[0]);
743 744 745
        checkGLcall("glMaterialfv");

        /* for the case of disabled lighting: */
746
        if (gl_info->supported[EXT_SECONDARY_COLOR])
747
            gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
748
        else
749 750 751
            TRACE("Specular colors cannot be disabled in this version of opengl\n");
        checkGLcall("glDisable(GL_COLOR_SUM)");

752
        if (gl_info->supported[NV_REGISTER_COMBINERS])
753
        {
754 755 756 757
            GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
            checkGLcall("glFinalCombinerInputNV()");
        }
    }
758

759 760 761 762
    TRACE("diffuse %s\n", debug_color(&state->material.diffuse));
    TRACE("ambient %s\n", debug_color(&state->material.ambient));
    TRACE("specular %s\n", debug_color(&state->material.specular));
    TRACE("emissive %s\n", debug_color(&state->material.emissive));
763

764
    gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (float *)&state->material.ambient);
765
    checkGLcall("glMaterialfv(GL_AMBIENT)");
766
    gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (float *)&state->material.diffuse);
767
    checkGLcall("glMaterialfv(GL_DIFFUSE)");
768
    gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (float *)&state->material.emissive);
769
    checkGLcall("glMaterialfv(GL_EMISSION)");
770 771
}

772
static void state_texfactor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
773
{
774
    const struct wined3d_gl_info *gl_info = context->gl_info;
775
    struct wined3d_color color;
776 777 778
    unsigned int i;

    /* Note the texture color applies to all textures whereas
779
     * GL_TEXTURE_ENV_COLOR applies to active only. */
780
    wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
781

782
    /* And now the default texture color as well */
783
    for (i = 0; i < context->d3d_info->limits.ffp_blend_stages; ++i)
784
    {
785 786
        /* Note the WINED3D_RS value applies to all textures, but GL has one
         * per texture, so apply it now ready to be used! */
787
        context_active_texture(context, gl_info, i);
788

789
        gl_info->gl_ops.gl.p_glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &color.r);
790
        checkGLcall("glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color);");
791 792 793
    }
}

794 795 796
static void renderstate_stencil_twosided(struct wined3d_context *context, GLint face,
        GLint func, GLint ref, GLuint mask, GLint stencilFail, GLint depthFail, GLint stencilPass)
{
797 798
    const struct wined3d_gl_info *gl_info = context->gl_info;

799
    gl_info->gl_ops.gl.p_glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
800 801 802
    checkGLcall("glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT)");
    GL_EXTCALL(glActiveStencilFaceEXT(face));
    checkGLcall("glActiveStencilFaceEXT(...)");
803
    gl_info->gl_ops.gl.p_glStencilFunc(func, ref, mask);
804
    checkGLcall("glStencilFunc(...)");
805
    gl_info->gl_ops.gl.p_glStencilOp(stencilFail, depthFail, stencilPass);
806
    checkGLcall("glStencilOp(...)");
807 808
}

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
static GLenum gl_stencil_op(enum wined3d_stencil_op op)
{
    switch (op)
    {
        case WINED3D_STENCIL_OP_KEEP:
            return GL_KEEP;
        case WINED3D_STENCIL_OP_ZERO:
            return GL_ZERO;
        case WINED3D_STENCIL_OP_REPLACE:
            return GL_REPLACE;
        case WINED3D_STENCIL_OP_INCR_SAT:
            return GL_INCR;
        case WINED3D_STENCIL_OP_DECR_SAT:
            return GL_DECR;
        case WINED3D_STENCIL_OP_INVERT:
            return GL_INVERT;
        case WINED3D_STENCIL_OP_INCR:
826
            return GL_INCR_WRAP;
827
        case WINED3D_STENCIL_OP_DECR:
828
            return GL_DECR_WRAP;
829 830 831 832 833 834
        default:
            FIXME("Unrecognized stencil op %#x.\n", op);
            return GL_KEEP;
    }
}

835
static void state_stencil(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
836
{
837
    const struct wined3d_gl_info *gl_info = context->gl_info;
838 839
    DWORD onesided_enable;
    DWORD twosided_enable;
840 841 842 843 844 845 846 847 848 849
    GLint func;
    GLint func_ccw;
    GLint ref;
    GLuint mask;
    GLint stencilFail;
    GLint stencilFail_ccw;
    GLint stencilPass;
    GLint stencilPass_ccw;
    GLint depthFail;
    GLint depthFail_ccw;
850

851
    /* No stencil test without a stencil buffer. */
852
    if (!state->fb->depth_stencil)
853
    {
854
        gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
855 856 857 858
        checkGLcall("glDisable GL_STENCIL_TEST");
        return;
    }

859 860
    onesided_enable = state->render_states[WINED3D_RS_STENCILENABLE];
    twosided_enable = state->render_states[WINED3D_RS_TWOSIDEDSTENCILMODE];
861
    if (!(func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_STENCILFUNC])))
862
        func = GL_ALWAYS;
863
    if (!(func_ccw = wined3d_gl_compare_func(state->render_states[WINED3D_RS_CCW_STENCILFUNC])))
864
        func_ccw = GL_ALWAYS;
865 866
    ref = state->render_states[WINED3D_RS_STENCILREF];
    mask = state->render_states[WINED3D_RS_STENCILMASK];
867 868 869 870 871 872
    stencilFail = gl_stencil_op(state->render_states[WINED3D_RS_STENCILFAIL]);
    depthFail = gl_stencil_op(state->render_states[WINED3D_RS_STENCILZFAIL]);
    stencilPass = gl_stencil_op(state->render_states[WINED3D_RS_STENCILPASS]);
    stencilFail_ccw = gl_stencil_op(state->render_states[WINED3D_RS_CCW_STENCILFAIL]);
    depthFail_ccw = gl_stencil_op(state->render_states[WINED3D_RS_CCW_STENCILZFAIL]);
    stencilPass_ccw = gl_stencil_op(state->render_states[WINED3D_RS_CCW_STENCILPASS]);
873

874 875 876
    TRACE("(onesided %d, twosided %d, ref %x, mask %x, "
          "GL_FRONT: func: %x, fail %x, zfail %x, zpass %x "
          "GL_BACK: func: %x, fail %x, zfail %x, zpass %x )\n",
877 878 879 880
    onesided_enable, twosided_enable, ref, mask,
    func, stencilFail, depthFail, stencilPass,
    func_ccw, stencilFail_ccw, depthFail_ccw, stencilPass_ccw);

881 882 883
    if (twosided_enable && onesided_enable)
    {
        gl_info->gl_ops.gl.p_glEnable(GL_STENCIL_TEST);
884 885
        checkGLcall("glEnable GL_STENCIL_TEST");

886 887 888 889 890 891 892 893 894
        if (gl_info->supported[WINED3D_GL_VERSION_2_0])
        {
            GL_EXTCALL(glStencilFuncSeparate(GL_FRONT, func, ref, mask));
            GL_EXTCALL(glStencilOpSeparate(GL_FRONT, stencilFail, depthFail, stencilPass));
            GL_EXTCALL(glStencilFuncSeparate(GL_BACK, func_ccw, ref, mask));
            GL_EXTCALL(glStencilOpSeparate(GL_BACK, stencilFail_ccw, depthFail_ccw, stencilPass_ccw));
            checkGLcall("setting two sided stencil state");
        }
        else if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
895
        {
896 897 898 899 900 901
            /* Apply back first, then front. This function calls glActiveStencilFaceEXT,
             * which has an effect on the code below too. If we apply the front face
             * afterwards, we are sure that the active stencil face is set to front,
             * and other stencil functions which do not use two sided stencil do not have
             * to set it back
             */
902 903 904 905
            renderstate_stencil_twosided(context, GL_BACK,
                    func_ccw, ref, mask, stencilFail_ccw, depthFail_ccw, stencilPass_ccw);
            renderstate_stencil_twosided(context, GL_FRONT,
                    func, ref, mask, stencilFail, depthFail, stencilPass);
906
        }
907
        else if (gl_info->supported[ATI_SEPARATE_STENCIL])
908
        {
909 910 911 912 913 914
            GL_EXTCALL(glStencilFuncSeparateATI(func, func_ccw, ref, mask));
            checkGLcall("glStencilFuncSeparateATI(...)");
            GL_EXTCALL(glStencilOpSeparateATI(GL_FRONT, stencilFail, depthFail, stencilPass));
            checkGLcall("glStencilOpSeparateATI(GL_FRONT, ...)");
            GL_EXTCALL(glStencilOpSeparateATI(GL_BACK, stencilFail_ccw, depthFail_ccw, stencilPass_ccw));
            checkGLcall("glStencilOpSeparateATI(GL_BACK, ...)");
915 916 917
        }
        else
        {
918 919
            ERR("Separate (two sided) stencil not supported on this version of opengl. Caps weren't honored?\n");
        }
920 921 922
    }
    else if(onesided_enable)
    {
923
        if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
924
        {
925
            gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
926
            checkGLcall("glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT)");
927
        }
928

929 930 931
        /* This code disables the ATI extension as well, since the standard stencil functions are equal
         * to calling the ATI functions with GL_FRONT_AND_BACK as face parameter
         */
932
        gl_info->gl_ops.gl.p_glEnable(GL_STENCIL_TEST);
933
        checkGLcall("glEnable GL_STENCIL_TEST");
934
        gl_info->gl_ops.gl.p_glStencilFunc(func, ref, mask);
935
        checkGLcall("glStencilFunc(...)");
936
        gl_info->gl_ops.gl.p_glStencilOp(stencilFail, depthFail, stencilPass);
937
        checkGLcall("glStencilOp(...)");
938 939 940 941
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
942
        checkGLcall("glDisable GL_STENCIL_TEST");
943 944 945
    }
}

946
static void state_stencilwrite2s(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
947
{
948
    DWORD mask = state->fb->depth_stencil ? state->render_states[WINED3D_RS_STENCILWRITEMASK] : 0;
949
    const struct wined3d_gl_info *gl_info = context->gl_info;
950

951 952
    GL_EXTCALL(glActiveStencilFaceEXT(GL_BACK));
    checkGLcall("glActiveStencilFaceEXT(GL_BACK)");
953
    gl_info->gl_ops.gl.p_glStencilMask(mask);
954 955 956
    checkGLcall("glStencilMask");
    GL_EXTCALL(glActiveStencilFaceEXT(GL_FRONT));
    checkGLcall("glActiveStencilFaceEXT(GL_FRONT)");
957
    gl_info->gl_ops.gl.p_glStencilMask(mask);
958 959
}

960
static void state_stencilwrite(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
961
{
962
    DWORD mask = state->fb->depth_stencil ? state->render_states[WINED3D_RS_STENCILWRITEMASK] : 0;
963
    const struct wined3d_gl_info *gl_info = context->gl_info;
964

965
    gl_info->gl_ops.gl.p_glStencilMask(mask);
966 967 968
    checkGLcall("glStencilMask");
}

969
static void state_fog_vertexpart(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
970
{
971
    const struct wined3d_gl_info *gl_info = context->gl_info;
972

973
    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
974

975
    if (!state->render_states[WINED3D_RS_FOGENABLE])
976
        return;
977 978

    /* Table fog on: Never use fog coords, and use per-fragment fog */
979
    if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
980
    {
981 982 983 984
        gl_info->gl_ops.gl.p_glHint(GL_FOG_HINT, GL_NICEST);
        if (context->fog_coord)
        {
            gl_info->gl_ops.gl.p_glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
985 986 987
            checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
            context->fog_coord = FALSE;
        }
988 989 990 991

        /* Range fog is only used with per-vertex fog in d3d */
        if (gl_info->supported[NV_FOG_DISTANCE])
        {
992
            gl_info->gl_ops.gl.p_glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV);
993 994
            checkGLcall("glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV)");
        }
995 996 997 998
        return;
    }

    /* Otherwise use per-vertex fog in any case */
999
    gl_info->gl_ops.gl.p_glHint(GL_FOG_HINT, GL_FASTEST);
1000

1001
    if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
1002
    {
1003
        /* No fog at all, or transformed vertices: Use fog coord */
1004 1005 1006
        if (!context->fog_coord)
        {
            gl_info->gl_ops.gl.p_glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
1007 1008 1009
            checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT)");
            context->fog_coord = TRUE;
        }
1010 1011 1012
    }
    else
    {
1013
        /* Otherwise, use the fragment depth */
1014 1015 1016
        if (context->fog_coord)
        {
            gl_info->gl_ops.gl.p_glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
1017 1018 1019
            checkGLcall("glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT)");
            context->fog_coord = FALSE;
        }
1020

1021
        if (state->render_states[WINED3D_RS_RANGEFOGENABLE])
1022 1023 1024
        {
            if (gl_info->supported[NV_FOG_DISTANCE])
            {
1025
                gl_info->gl_ops.gl.p_glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_RADIAL_NV);
1026 1027 1028 1029 1030 1031 1032 1033 1034
                checkGLcall("glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_RADIAL_NV)");
            }
            else
            {
                WARN("Range fog enabled, but not supported by this GL implementation.\n");
            }
        }
        else if (gl_info->supported[NV_FOG_DISTANCE])
        {
1035
            gl_info->gl_ops.gl.p_glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV);
1036 1037
            checkGLcall("glFogi(GL_FOG_DISTANCE_MODE_NV, GL_EYE_PLANE_ABSOLUTE_NV)");
        }
1038 1039
    }
}
1040

1041
void state_fogstartend(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1042
{
1043
    const struct wined3d_gl_info *gl_info = context->gl_info;
1044 1045
    float fogstart, fogend;

1046
    get_fog_start_end(context, state, &fogstart, &fogend);
1047

1048
    gl_info->gl_ops.gl.p_glFogf(GL_FOG_START, fogstart);
1049 1050 1051
    checkGLcall("glFogf(GL_FOG_START, fogstart)");
    TRACE("Fog Start == %f\n", fogstart);

1052
    gl_info->gl_ops.gl.p_glFogf(GL_FOG_END, fogend);
1053 1054 1055 1056
    checkGLcall("glFogf(GL_FOG_END, fogend)");
    TRACE("Fog End == %f\n", fogend);
}

1057
void state_fog_fragpart(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1058
{
1059
    const struct wined3d_gl_info *gl_info = context->gl_info;
1060
    enum fogsource new_source;
1061 1062
    DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
    DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
1063

1064
    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
1065

1066
    if (!state->render_states[WINED3D_RS_FOGENABLE])
1067
    {
1068
        /* No fog? Disable it, and we're done :-) */
1069
        glDisableWINE(GL_FOG);
1070
        checkGLcall("glDisable GL_FOG");
1071
        return;
1072 1073
    }

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    /* Fog Rules:
     *
     * With fixed function vertex processing, Direct3D knows 2 different fog input sources.
     * It can use the Z value of the vertex, or the alpha component of the specular color.
     * This depends on the fog vertex, fog table and the vertex declaration. If the Z value
     * is used, fogstart, fogend and the equation type are used, otherwise linear fog with
     * start = 255, end = 0 is used. Obviously the msdn is not very clear on that.
     *
     * FOGTABLEMODE != NONE:
     *  The Z value is used, with the equation specified, no matter what vertex type.
     *
     * FOGTABLEMODE == NONE, FOGVERTEXMODE != NONE, untransformed:
     *  Per vertex fog is calculated using the specified fog equation and the parameters
     *
     * FOGTABLEMODE == NONE, FOGVERTEXMODE != NONE, transformed, OR
     * FOGTABLEMODE == NONE, FOGVERTEXMODE == NONE, untransformed:
     *  Linear fog with start = 255.0, end = 0.0, input comes from the specular color
     *
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
     *
     * Rules for vertex fog with shaders:
     *
     * When mixing fixed function functionality with the programmable pipeline, D3D expects
     * the fog computation to happen during transformation while openGL expects it to happen
     * during rasterization. Also, prior to pixel shader 3.0 D3D handles fog blending after
     * the pixel shader while openGL always expects the pixel shader to handle the blending.
     * To solve this problem, WineD3D does:
     * 1) implement a linear fog equation and fog blending at the end of every pre 3.0 pixel
     * shader,
     * and 2) disables the fog computation (in either the fixed function or programmable
     * rasterizer) if using a vertex program.
     *
1105 1106 1107 1108 1109 1110 1111
     * D3D shaders can provide an explicit fog coordinate. This fog coordinate is used with
     * D3DRS_FOGTABLEMODE==D3DFOG_NONE. The FOGVERTEXMODE is ignored, d3d always uses linear
     * fog with start=1.0 and end=0.0 in this case. This is similar to fog coordinates in
     * the specular color, a vertex shader counts as pretransformed geometry in this case.
     * There are some GL differences between specular fog coords and vertex shaders though.
     *
     * With table fog the vertex shader fog coordinate is ignored.
1112 1113 1114
     *
     * If a fogtablemode and a fogvertexmode are specified, table fog is applied (with or
     * without shaders).
1115
     */
1116

1117 1118 1119
    /* DX 7 sdk: "If both render states(vertex and table fog) are set to valid modes,
     * the system will apply only pixel(=table) fog effects."
     */
1120
    if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
1121
    {
1122 1123
        if (use_vs(state))
        {
1124
            gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_LINEAR);
1125 1126
            checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
            new_source = FOGSOURCE_VS;
1127 1128 1129
        }
        else
        {
1130
            switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
1131
            {
1132
                /* If processed vertices are used, fall through to the NONE case */
1133 1134 1135
                case WINED3D_FOG_EXP:
                    if (!context->last_was_rhw)
                    {
1136
                        gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_EXP);
1137 1138 1139 1140 1141 1142
                        checkGLcall("glFogi(GL_FOG_MODE, GL_EXP)");
                        new_source = FOGSOURCE_FFP;
                        break;
                    }
                    /* drop through */

1143 1144 1145
                case WINED3D_FOG_EXP2:
                    if (!context->last_was_rhw)
                    {
1146
                        gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_EXP2);
1147 1148 1149 1150 1151 1152
                        checkGLcall("glFogi(GL_FOG_MODE, GL_EXP2)");
                        new_source = FOGSOURCE_FFP;
                        break;
                    }
                    /* drop through */

1153 1154 1155
                case WINED3D_FOG_LINEAR:
                    if (!context->last_was_rhw)
                    {
1156
                        gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_LINEAR);
1157 1158 1159 1160 1161
                        checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
                        new_source = FOGSOURCE_FFP;
                        break;
                    }
                    /* drop through */
1162

1163
                case WINED3D_FOG_NONE:
1164 1165 1166 1167
                    /* Both are none? According to msdn the alpha channel of
                     * the specular colour contains a fog factor. Set it in
                     * draw_primitive_immediate_mode(). Same happens with
                     * vertex fog on transformed vertices. */
1168
                    new_source = FOGSOURCE_COORD;
1169
                    gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_LINEAR);
1170
                    checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
1171
                    break;
1172

1173
                default:
1174 1175
                    FIXME("Unexpected WINED3D_RS_FOGVERTEXMODE %#x.\n",
                            state->render_states[WINED3D_RS_FOGVERTEXMODE]);
1176 1177
                    new_source = FOGSOURCE_FFP; /* Make the compiler happy */
            }
1178 1179
        }
    } else {
1180
        new_source = FOGSOURCE_FFP;
1181

1182
        switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
1183
        {
1184
            case WINED3D_FOG_EXP:
1185
                gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_EXP);
1186
                checkGLcall("glFogi(GL_FOG_MODE, GL_EXP)");
1187 1188
                break;

1189
            case WINED3D_FOG_EXP2:
1190
                gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_EXP2);
1191
                checkGLcall("glFogi(GL_FOG_MODE, GL_EXP2)");
1192 1193
                break;

1194
            case WINED3D_FOG_LINEAR:
1195
                gl_info->gl_ops.gl.p_glFogi(GL_FOG_MODE, GL_LINEAR);
1196
                checkGLcall("glFogi(GL_FOG_MODE, GL_LINEAR)");
1197 1198
                break;

1199
            case WINED3D_FOG_NONE:   /* Won't happen */
1200
            default:
1201 1202
                FIXME("Unexpected WINED3D_RS_FOGTABLEMODE %#x.\n",
                        state->render_states[WINED3D_RS_FOGTABLEMODE]);
1203 1204 1205
        }
    }

1206
    glEnableWINE(GL_FOG);
1207
    checkGLcall("glEnable GL_FOG");
1208
    if (new_source != context->fog_source || fogstart == fogend)
1209
    {
1210
        context->fog_source = new_source;
1211
        state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
1212
    }
1213
}
1214

1215
void state_fogcolor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1216
{
1217
    const struct wined3d_gl_info *gl_info = context->gl_info;
1218
    struct wined3d_color color;
1219

1220 1221
    wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
    gl_info->gl_ops.gl.p_glFogfv(GL_FOG_COLOR, &color.r);
1222 1223 1224
    checkGLcall("glFog GL_FOG_COLOR");
}

1225
void state_fogdensity(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1226
{
1227
    const struct wined3d_gl_info *gl_info = context->gl_info;
1228 1229 1230 1231
    union {
        DWORD d;
        float f;
    } tmpvalue;
1232

1233
    tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1234
    gl_info->gl_ops.gl.p_glFogfv(GL_FOG_DENSITY, &tmpvalue.f);
1235 1236 1237
    checkGLcall("glFogf(GL_FOG_DENSITY, (float) Value)");
}

1238
static void state_colormat(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1239
{
1240
    const struct wined3d_gl_info *gl_info = context->gl_info;
1241
    GLenum Parm = 0;
1242

1243
    /* Depends on the decoded vertex declaration to read the existence of diffuse data.
1244 1245
     * The vertex declaration will call this function if the fixed function pipeline is used.
     */
1246 1247

    if(isStateDirty(context, STATE_VDECL)) {
1248 1249 1250
        return;
    }

1251
    context->num_untracked_materials = 0;
1252
    if ((context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE))
1253
            && state->render_states[WINED3D_RS_COLORVERTEX])
1254
    {
1255
        TRACE("diff %d, amb %d, emis %d, spec %d\n",
1256 1257 1258 1259
                state->render_states[WINED3D_RS_DIFFUSEMATERIALSOURCE],
                state->render_states[WINED3D_RS_AMBIENTMATERIALSOURCE],
                state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE],
                state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE]);
1260

1261
        if (state->render_states[WINED3D_RS_DIFFUSEMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1262
        {
1263
            if (state->render_states[WINED3D_RS_AMBIENTMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1264
                Parm = GL_AMBIENT_AND_DIFFUSE;
1265
            else
1266
                Parm = GL_DIFFUSE;
1267
            if (state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1268
            {
1269 1270 1271
                context->untracked_materials[context->num_untracked_materials] = GL_EMISSION;
                context->num_untracked_materials++;
            }
1272
            if (state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1273
            {
1274 1275 1276
                context->untracked_materials[context->num_untracked_materials] = GL_SPECULAR;
                context->num_untracked_materials++;
            }
1277
        }
1278
        else if (state->render_states[WINED3D_RS_AMBIENTMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1279
        {
1280
            Parm = GL_AMBIENT;
1281
            if (state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1282
            {
1283 1284 1285
                context->untracked_materials[context->num_untracked_materials] = GL_EMISSION;
                context->num_untracked_materials++;
            }
1286
            if (state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1287
            {
1288 1289 1290
                context->untracked_materials[context->num_untracked_materials] = GL_SPECULAR;
                context->num_untracked_materials++;
            }
1291
        }
1292
        else if (state->render_states[WINED3D_RS_EMISSIVEMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1293
        {
1294
            Parm = GL_EMISSION;
1295
            if (state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1296
            {
1297 1298 1299
                context->untracked_materials[context->num_untracked_materials] = GL_SPECULAR;
                context->num_untracked_materials++;
            }
1300
        }
1301
        else if (state->render_states[WINED3D_RS_SPECULARMATERIALSOURCE] == WINED3D_MCS_COLOR1)
1302
        {
1303 1304
            Parm = GL_SPECULAR;
        }
1305
    }
1306

1307
    /* Nothing changed, return. */
1308
    if (Parm == context->tracking_parm) return;
1309

1310 1311 1312
    if (!Parm)
    {
        gl_info->gl_ops.gl.p_glDisable(GL_COLOR_MATERIAL);
1313
        checkGLcall("glDisable GL_COLOR_MATERIAL");
1314 1315 1316 1317
    }
    else
    {
        gl_info->gl_ops.gl.p_glColorMaterial(GL_FRONT_AND_BACK, Parm);
1318
        checkGLcall("glColorMaterial(GL_FRONT_AND_BACK, Parm)");
1319
        gl_info->gl_ops.gl.p_glEnable(GL_COLOR_MATERIAL);
1320
        checkGLcall("glEnable(GL_COLOR_MATERIAL)");
1321
    }
1322 1323 1324

    /* Apparently calls to glMaterialfv are ignored for properties we're
     * tracking with glColorMaterial, so apply those here. */
1325 1326
    switch (context->tracking_parm)
    {
1327
        case GL_AMBIENT_AND_DIFFUSE:
1328 1329
            gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (float *)&state->material.ambient);
            gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (float *)&state->material.diffuse);
1330 1331 1332 1333
            checkGLcall("glMaterialfv");
            break;

        case GL_DIFFUSE:
1334
            gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (float *)&state->material.diffuse);
1335 1336 1337 1338
            checkGLcall("glMaterialfv");
            break;

        case GL_AMBIENT:
1339
            gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (float *)&state->material.ambient);
1340 1341 1342 1343
            checkGLcall("glMaterialfv");
            break;

        case GL_EMISSION:
1344
            gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (float *)&state->material.emissive);
1345 1346 1347 1348 1349
            checkGLcall("glMaterialfv");
            break;

        case GL_SPECULAR:
            /* Only change material color if specular is enabled, otherwise it is set to black */
1350
            if (state->render_states[WINED3D_RS_SPECULARENABLE])
1351
            {
1352
                gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (float *)&state->material.specular);
1353
                checkGLcall("glMaterialfv");
1354 1355 1356
            }
            else
            {
1357
                static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1358
                gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &black[0]);
1359 1360 1361 1362 1363
                checkGLcall("glMaterialfv");
            }
            break;
    }

1364
    context->tracking_parm = Parm;
1365 1366
}

1367
static void state_linepattern(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1368
{
1369
    const struct wined3d_gl_info *gl_info = context->gl_info;
1370 1371 1372 1373
    union
    {
        DWORD d;
        struct wined3d_line_pattern lp;
1374
    } tmppattern;
1375
    tmppattern.d = state->render_states[WINED3D_RS_LINEPATTERN];
1376

1377
    TRACE("Line pattern: repeat %d bits %x.\n", tmppattern.lp.repeat_factor, tmppattern.lp.line_pattern);
1378

1379 1380
    if (tmppattern.lp.repeat_factor)
    {
1381
        gl_info->gl_ops.gl.p_glLineStipple(tmppattern.lp.repeat_factor, tmppattern.lp.line_pattern);
1382
        checkGLcall("glLineStipple(repeat, linepattern)");
1383
        gl_info->gl_ops.gl.p_glEnable(GL_LINE_STIPPLE);
1384
        checkGLcall("glEnable(GL_LINE_STIPPLE);");
1385 1386 1387
    }
    else
    {
1388
        gl_info->gl_ops.gl.p_glDisable(GL_LINE_STIPPLE);
1389 1390 1391 1392
        checkGLcall("glDisable(GL_LINE_STIPPLE);");
    }
}

1393 1394 1395 1396 1397 1398 1399 1400
static void state_linepattern_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
{
    static unsigned int once;

    if (!once++)
        FIXME("Setting line patterns is not supported in OpenGL core contexts.\n");
}

1401
static void state_normalize(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1402
{
1403 1404
    const struct wined3d_gl_info *gl_info = context->gl_info;

1405
    if (isStateDirty(context, STATE_VDECL))
1406
        return;
1407

1408 1409 1410 1411
    /* Without vertex normals, we set the current normal to 0/0/0 to remove the diffuse factor
     * from the opengl lighting equation, as d3d does. Normalization of 0/0/0 can lead to a division
     * by zero and is not properly defined in opengl, so avoid it
     */
1412
    if (state->render_states[WINED3D_RS_NORMALIZENORMALS]
1413
            && (context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL)))
1414
    {
1415
        gl_info->gl_ops.gl.p_glEnable(GL_NORMALIZE);
1416
        checkGLcall("glEnable(GL_NORMALIZE);");
1417 1418 1419
    }
    else
    {
1420
        gl_info->gl_ops.gl.p_glDisable(GL_NORMALIZE);
1421 1422 1423 1424
        checkGLcall("glDisable(GL_NORMALIZE);");
    }
}

1425
static void state_psizemin_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1426
{
1427
    float min, max;
1428

1429
    get_pointsize_minmax(context, state, &min, &max);
1430

1431 1432 1433 1434
    if (min != 1.0f)
        FIXME("WINED3D_RS_POINTSIZE_MIN value %.8e not supported on this OpenGL implementation.\n", min);
    if (max != 64.0f)
        FIXME("WINED3D_RS_POINTSIZE_MAX value %.8e not supported on this OpenGL implementation.\n", max);
1435 1436
}

1437
static void state_psizemin_ext(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1438
{
1439
    const struct wined3d_gl_info *gl_info = context->gl_info;
1440
    float min, max;
1441

1442
    get_pointsize_minmax(context, state, &min, &max);
1443

1444
    GL_EXTCALL(glPointParameterfEXT)(GL_POINT_SIZE_MIN_EXT, min);
1445
    checkGLcall("glPointParameterfEXT(...)");
1446
    GL_EXTCALL(glPointParameterfEXT)(GL_POINT_SIZE_MAX_EXT, max);
1447 1448 1449
    checkGLcall("glPointParameterfEXT(...)");
}

1450
static void state_psizemin_arb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1451
{
1452
    const struct wined3d_gl_info *gl_info = context->gl_info;
1453
    float min, max;
1454

1455
    get_pointsize_minmax(context, state, &min, &max);
1456

1457
    GL_EXTCALL(glPointParameterfARB)(GL_POINT_SIZE_MIN_ARB, min);
1458
    checkGLcall("glPointParameterfARB(...)");
1459
    GL_EXTCALL(glPointParameterfARB)(GL_POINT_SIZE_MAX_ARB, max);
1460
    checkGLcall("glPointParameterfARB(...)");
1461 1462
}

1463
static void state_pscale(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1464
{
1465
    const struct wined3d_gl_info *gl_info = context->gl_info;
1466 1467
    float att[3];
    float pointsize;
1468

1469
    get_pointsize(context, state, &pointsize, att);
1470

1471
    if (gl_info->supported[ARB_POINT_PARAMETERS])
1472
    {
1473
        GL_EXTCALL(glPointParameterfvARB)(GL_POINT_DISTANCE_ATTENUATION_ARB, att);
1474
        checkGLcall("glPointParameterfvARB(GL_DISTANCE_ATTENUATION_ARB, ...)");
1475
    }
1476
    else if (gl_info->supported[EXT_POINT_PARAMETERS])
1477
    {
1478
        GL_EXTCALL(glPointParameterfvEXT)(GL_DISTANCE_ATTENUATION_EXT, att);
1479
        checkGLcall("glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, ...)");
1480
    }
1481
    else if (state->render_states[WINED3D_RS_POINTSCALEENABLE])
1482
    {
1483
        WARN("POINT_PARAMETERS not supported in this version of opengl\n");
1484
    }
1485

1486
    gl_info->gl_ops.gl.p_glPointSize(max(pointsize, FLT_MIN));
1487
    checkGLcall("glPointSize(...);");
1488 1489
}

1490
static void state_debug_monitor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1491
{
1492
    WARN("token: %#x.\n", state->render_states[WINED3D_RS_DEBUGMONITORTOKEN]);
1493 1494
}

1495
static void state_colorwrite(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1496
{
1497 1498 1499 1500
    DWORD mask0 = state->render_states[WINED3D_RS_COLORWRITEENABLE];
    DWORD mask1 = state->render_states[WINED3D_RS_COLORWRITEENABLE1];
    DWORD mask2 = state->render_states[WINED3D_RS_COLORWRITEENABLE2];
    DWORD mask3 = state->render_states[WINED3D_RS_COLORWRITEENABLE3];
1501
    const struct wined3d_gl_info *gl_info = context->gl_info;
1502 1503

    TRACE("Color mask: r(%d) g(%d) b(%d) a(%d)\n",
1504 1505 1506 1507
            mask0 & WINED3DCOLORWRITEENABLE_RED ? 1 : 0,
            mask0 & WINED3DCOLORWRITEENABLE_GREEN ? 1 : 0,
            mask0 & WINED3DCOLORWRITEENABLE_BLUE ? 1 : 0,
            mask0 & WINED3DCOLORWRITEENABLE_ALPHA ? 1 : 0);
1508
    gl_info->gl_ops.gl.p_glColorMask(mask0 & WINED3DCOLORWRITEENABLE_RED ? GL_TRUE : GL_FALSE,
1509 1510 1511
            mask0 & WINED3DCOLORWRITEENABLE_GREEN ? GL_TRUE : GL_FALSE,
            mask0 & WINED3DCOLORWRITEENABLE_BLUE ? GL_TRUE : GL_FALSE,
            mask0 & WINED3DCOLORWRITEENABLE_ALPHA ? GL_TRUE : GL_FALSE);
1512 1513
    checkGLcall("glColorMask(...)");

1514 1515 1516
    if (!((mask1 == mask0 && mask2 == mask0 && mask3 == mask0)
        || (mask1 == 0xf && mask2 == 0xf && mask3 == 0xf)))
    {
1517
        FIXME("WINED3D_RS_COLORWRITEENABLE/1/2/3, %#x/%#x/%#x/%#x not yet implemented.\n",
1518 1519
            mask0, mask1, mask2, mask3);
        FIXME("Missing of cap D3DPMISCCAPS_INDEPENDENTWRITEMASKS wasn't honored?\n");
1520 1521 1522
    }
}

1523
static void set_color_mask(const struct wined3d_gl_info *gl_info, UINT index, DWORD mask)
1524
{
1525
    GL_EXTCALL(glColorMaski(index,
1526 1527 1528 1529
            mask & WINED3DCOLORWRITEENABLE_RED ? GL_TRUE : GL_FALSE,
            mask & WINED3DCOLORWRITEENABLE_GREEN ? GL_TRUE : GL_FALSE,
            mask & WINED3DCOLORWRITEENABLE_BLUE ? GL_TRUE : GL_FALSE,
            mask & WINED3DCOLORWRITEENABLE_ALPHA ? GL_TRUE : GL_FALSE));
1530
    checkGLcall("glColorMaski");
1531 1532
}

1533
static void state_colorwrite0(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1534
{
1535
    set_color_mask(context->gl_info, 0, state->render_states[WINED3D_RS_COLORWRITEENABLE]);
1536 1537
}

1538
static void state_colorwrite1(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1539
{
1540
    set_color_mask(context->gl_info, 1, state->render_states[WINED3D_RS_COLORWRITEENABLE1]);
1541 1542
}

1543
static void state_colorwrite2(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1544
{
1545
    set_color_mask(context->gl_info, 2, state->render_states[WINED3D_RS_COLORWRITEENABLE2]);
1546 1547
}

1548
static void state_colorwrite3(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1549
{
1550
    set_color_mask(context->gl_info, 3, state->render_states[WINED3D_RS_COLORWRITEENABLE3]);
1551 1552
}

1553
static void state_localviewer(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1554
{
1555 1556
    const struct wined3d_gl_info *gl_info = context->gl_info;

1557
    if (state->render_states[WINED3D_RS_LOCALVIEWER])
1558
    {
1559
        gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
1560
        checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)");
1561 1562 1563 1564
    }
    else
    {
        gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
1565 1566 1567 1568
        checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0)");
    }
}

1569
static void state_lastpixel(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1570
{
1571
    if (state->render_states[WINED3D_RS_LASTPIXEL])
1572
    {
1573
        TRACE("Last Pixel Drawing Enabled\n");
1574 1575 1576
    }
    else
    {
1577 1578
        static BOOL warned;
        if (!warned) {
1579
            FIXME("Last Pixel Drawing Disabled, not handled yet\n");
1580
            warned = TRUE;
1581 1582 1583
        } else {
            TRACE("Last Pixel Drawing Disabled, not handled yet\n");
        }
1584 1585 1586
    }
}

1587
void state_pointsprite_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1588
{
1589 1590
    static BOOL warned;

1591
    /* TODO: NV_POINT_SPRITE */
1592
    if (!warned && state->render_states[WINED3D_RS_POINTSPRITEENABLE])
1593
    {
1594 1595 1596
        /* A FIXME, not a WARN because point sprites should be software emulated if not supported by HW */
        FIXME("Point sprites not supported\n");
        warned = TRUE;
1597
    }
1598
}
1599

1600
void state_pointsprite(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1601
{
1602 1603
    const struct wined3d_gl_info *gl_info = context->gl_info;

1604
    if (state->render_states[WINED3D_RS_POINTSPRITEENABLE])
1605
    {
1606
        gl_info->gl_ops.gl.p_glEnable(GL_POINT_SPRITE_ARB);
1607
        checkGLcall("glEnable(GL_POINT_SPRITE_ARB)");
1608 1609 1610 1611
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
1612
        checkGLcall("glDisable(GL_POINT_SPRITE_ARB)");
1613 1614 1615
    }
}

1616
static void state_wrap(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1617
{
1618 1619 1620
    static unsigned int once;

    if ((state->render_states[WINED3D_RS_WRAP0]
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
            || state->render_states[WINED3D_RS_WRAP1]
            || state->render_states[WINED3D_RS_WRAP2]
            || state->render_states[WINED3D_RS_WRAP3]
            || state->render_states[WINED3D_RS_WRAP4]
            || state->render_states[WINED3D_RS_WRAP5]
            || state->render_states[WINED3D_RS_WRAP6]
            || state->render_states[WINED3D_RS_WRAP7]
            || state->render_states[WINED3D_RS_WRAP8]
            || state->render_states[WINED3D_RS_WRAP9]
            || state->render_states[WINED3D_RS_WRAP10]
            || state->render_states[WINED3D_RS_WRAP11]
            || state->render_states[WINED3D_RS_WRAP12]
            || state->render_states[WINED3D_RS_WRAP13]
            || state->render_states[WINED3D_RS_WRAP14]
            || state->render_states[WINED3D_RS_WRAP15])
1636
            && !once++)
1637
        FIXME("(WINED3D_RS_WRAP0) Texture wrapping not yet supported.\n");
1638 1639
}

1640
static void state_msaa_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1641
{
1642
    if (state->render_states[WINED3D_RS_MULTISAMPLEANTIALIAS])
1643
        WARN("Multisample antialiasing not supported by GL.\n");
1644 1645
}

1646
static void state_msaa(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1647
{
1648 1649
    const struct wined3d_gl_info *gl_info = context->gl_info;

1650
    if (state->render_states[WINED3D_RS_MULTISAMPLEANTIALIAS])
1651
    {
1652
        gl_info->gl_ops.gl.p_glEnable(GL_MULTISAMPLE_ARB);
1653
        checkGLcall("glEnable(GL_MULTISAMPLE_ARB)");
1654 1655 1656 1657
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_MULTISAMPLE_ARB);
1658
        checkGLcall("glDisable(GL_MULTISAMPLE_ARB)");
1659 1660 1661
    }
}

1662
static void state_scissor(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1663
{
1664 1665
    const struct wined3d_gl_info *gl_info = context->gl_info;

1666
    if (state->render_states[WINED3D_RS_SCISSORTESTENABLE])
1667
    {
1668
        gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
1669
        checkGLcall("glEnable(GL_SCISSOR_TEST)");
1670 1671 1672 1673
    }
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
1674 1675 1676 1677
        checkGLcall("glDisable(GL_SCISSOR_TEST)");
    }
}

1678 1679 1680 1681
/* The Direct3D depth bias is specified in normalized depth coordinates. In
 * OpenGL the bias is specified in units of "the smallest value that is
 * guaranteed to produce a resolvable offset for a given implementation". To
 * convert from D3D to GL we need to divide the D3D depth bias by that value.
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
 * We try to detect the value from GL with test draws. On most drivers (r300g,
 * 600g, Nvidia, i965 on Mesa) the value is 2^23 for fixed point depth buffers,
 * for r200 and i965 on OSX it is 2^24, for r500 on OSX it is 2^22. For floating
 * point buffers it is 2^22, 2^23 or 2^24 depending on the GPU. The value does
 * not depend on the depth buffer precision on any driver.
 *
 * Two games that are picky regarding depth bias are Mass Effect 2 (flickering
 * decals) and F.E.A.R and F.E.A.R. 2 (semi-transparent guns).
 *
 * Note that SLOPESCALEDEPTHBIAS is a scaling factor for the depth slope, and
 * doesn't need to be scaled to account for GL vs D3D differences. */
1693
static void state_depthbias(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1694
{
1695 1696
    const struct wined3d_gl_info *gl_info = context->gl_info;

1697 1698
    if (state->render_states[WINED3D_RS_SLOPESCALEDEPTHBIAS]
            || state->render_states[WINED3D_RS_DEPTHBIAS])
1699
    {
1700
        const struct wined3d_rendertarget_view *depth = state->fb->depth_stencil;
1701 1702
        float scale;

1703 1704 1705 1706 1707 1708
        union
        {
            DWORD d;
            float f;
        } scale_bias, const_bias;

1709 1710
        scale_bias.d = state->render_states[WINED3D_RS_SLOPESCALEDEPTHBIAS];
        const_bias.d = state->render_states[WINED3D_RS_DEPTHBIAS];
1711

1712
        gl_info->gl_ops.gl.p_glEnable(GL_POLYGON_OFFSET_FILL);
1713
        checkGLcall("glEnable(GL_POLYGON_OFFSET_FILL)");
1714

1715
        if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_DEPTH_BIAS)
1716
        {
1717
            float bias = -(float)const_bias.d;
1718
            gl_info->gl_ops.gl.p_glPolygonOffset(bias, bias);
1719
            checkGLcall("glPolygonOffset");
1720 1721 1722
        }
        else
        {
1723 1724
            if (depth)
            {
1725 1726 1727 1728 1729
                if (depth->format_flags & WINED3DFMT_FLAG_FLOAT)
                    scale = gl_info->float_polyoffset_scale;
                else
                    scale = gl_info->fixed_polyoffset_scale;

1730
                TRACE("Depth format %s, using depthbias scale of %.8e.\n",
1731
                      debug_d3dformat(depth->format->id), scale);
1732 1733 1734 1735 1736 1737 1738
            }
            else
            {
                /* The context manager will reapply this state on a depth stencil change */
                TRACE("No depth stencil, using depthbias scale of 0.0.\n");
                scale = 0.0f;
            }
1739

1740
            gl_info->gl_ops.gl.p_glPolygonOffset(scale_bias.f, const_bias.f * scale);
1741 1742 1743 1744 1745
            checkGLcall("glPolygonOffset(...)");
        }
    }
    else
    {
1746
        gl_info->gl_ops.gl.p_glDisable(GL_POLYGON_OFFSET_FILL);
1747 1748 1749 1750
        checkGLcall("glDisable(GL_POLYGON_OFFSET_FILL)");
    }
}

1751
static void state_zvisible(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1752
{
1753 1754
    if (state->render_states[WINED3D_RS_ZVISIBLE])
        FIXME("WINED3D_RS_ZVISIBLE not implemented.\n");
1755 1756
}

1757
static void state_stippledalpha(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1758
{
1759
    if (state->render_states[WINED3D_RS_STIPPLEDALPHA])
1760
        FIXME("Stippled Alpha not supported yet.\n");
1761 1762
}

1763
static void state_antialias(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1764
{
1765
    if (state->render_states[WINED3D_RS_ANTIALIAS])
1766
        FIXME("Antialias not supported yet.\n");
1767 1768
}

1769
static void state_multisampmask(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1770
{
1771 1772 1773
    if (state->render_states[WINED3D_RS_MULTISAMPLEMASK] != 0xffffffff)
        FIXME("WINED3D_RS_MULTISAMPLEMASK %#x not yet implemented.\n",
                state->render_states[WINED3D_RS_MULTISAMPLEMASK]);
1774 1775
}

1776
static void state_patchedgestyle(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1777
{
1778
    if (state->render_states[WINED3D_RS_PATCHEDGESTYLE] != WINED3D_PATCH_EDGE_DISCRETE)
1779 1780
        FIXME("WINED3D_RS_PATCHEDGESTYLE %#x not yet implemented.\n",
                state->render_states[WINED3D_RS_PATCHEDGESTYLE]);
1781 1782
}

1783
static void state_patchsegments(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1784
{
1785 1786 1787 1788 1789 1790
    union {
        DWORD d;
        float f;
    } tmpvalue;
    tmpvalue.f = 1.0f;

1791
    if (state->render_states[WINED3D_RS_PATCHSEGMENTS] != tmpvalue.d)
1792
    {
1793 1794
        static BOOL displayed = FALSE;

1795
        tmpvalue.d = state->render_states[WINED3D_RS_PATCHSEGMENTS];
1796
        if(!displayed)
1797
            FIXME("(WINED3D_RS_PATCHSEGMENTS,%f) not yet implemented\n", tmpvalue.f);
1798 1799

        displayed = TRUE;
1800
    }
1801 1802
}

1803
static void state_positiondegree(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1804
{
1805 1806 1807
    if (state->render_states[WINED3D_RS_POSITIONDEGREE] != WINED3D_DEGREE_CUBIC)
        FIXME("WINED3D_RS_POSITIONDEGREE %#x not yet implemented.\n",
                state->render_states[WINED3D_RS_POSITIONDEGREE]);
1808 1809
}

1810
static void state_normaldegree(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1811
{
1812 1813 1814
    if (state->render_states[WINED3D_RS_NORMALDEGREE] != WINED3D_DEGREE_LINEAR)
        FIXME("WINED3D_RS_NORMALDEGREE %#x not yet implemented.\n",
                state->render_states[WINED3D_RS_NORMALDEGREE]);
1815 1816
}

1817
static void state_tessellation(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1818
{
1819 1820 1821
    if (state->render_states[WINED3D_RS_ENABLEADAPTIVETESSELLATION])
        FIXME("WINED3D_RS_ENABLEADAPTIVETESSELLATION %#x not yet implemented.\n",
                state->render_states[WINED3D_RS_ENABLEADAPTIVETESSELLATION]);
1822 1823
}

1824
static void state_nvdb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1825 1826 1827 1828 1829 1830 1831 1832
{
    union {
        DWORD d;
        float f;
    } zmin, zmax;

    const struct wined3d_gl_info *gl_info = context->gl_info;

1833
    if (state->render_states[WINED3D_RS_ADAPTIVETESS_X] == WINED3DFMT_NVDB)
1834
    {
1835 1836
        zmin.d = state->render_states[WINED3D_RS_ADAPTIVETESS_Z];
        zmax.d = state->render_states[WINED3D_RS_ADAPTIVETESS_W];
1837 1838 1839 1840 1841

        /* If zmin is larger than zmax INVALID_VALUE error is generated.
         * In d3d9 test is not performed in this case*/
        if (zmin.f <= zmax.f)
        {
1842
            gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_BOUNDS_TEST_EXT);
1843 1844 1845 1846
            checkGLcall("glEnable(GL_DEPTH_BOUNDS_TEST_EXT)");
            GL_EXTCALL(glDepthBoundsEXT(zmin.f, zmax.f));
            checkGLcall("glDepthBoundsEXT(...)");
        }
1847 1848 1849
        else
        {
            gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_BOUNDS_TEST_EXT);
1850 1851 1852
            checkGLcall("glDisable(GL_DEPTH_BOUNDS_TEST_EXT)");
        }
    }
1853 1854 1855
    else
    {
        gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_BOUNDS_TEST_EXT);
1856 1857 1858
        checkGLcall("glDisable(GL_DEPTH_BOUNDS_TEST_EXT)");
    }

1859
    state_tessellation(context, state, STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION));
1860 1861
}

1862
static void state_wrapu(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1863
{
1864 1865
    if (state->render_states[WINED3D_RS_WRAPU])
        FIXME("Render state WINED3D_RS_WRAPU not implemented yet.\n");
1866 1867
}

1868
static void state_wrapv(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1869
{
1870 1871
    if (state->render_states[WINED3D_RS_WRAPV])
        FIXME("Render state WINED3D_RS_WRAPV not implemented yet.\n");
1872 1873
}

1874
static void state_monoenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1875
{
1876 1877
    if (state->render_states[WINED3D_RS_MONOENABLE])
        FIXME("Render state WINED3D_RS_MONOENABLE not implemented yet.\n");
1878 1879
}

1880
static void state_rop2(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1881
{
1882 1883
    if (state->render_states[WINED3D_RS_ROP2])
        FIXME("Render state WINED3D_RS_ROP2 not implemented yet.\n");
1884 1885
}

1886
static void state_planemask(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1887
{
1888 1889
    if (state->render_states[WINED3D_RS_PLANEMASK])
        FIXME("Render state WINED3D_RS_PLANEMASK not implemented yet.\n");
1890 1891
}

1892
static void state_subpixel(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1893
{
1894 1895
    if (state->render_states[WINED3D_RS_SUBPIXEL])
        FIXME("Render state WINED3D_RS_SUBPIXEL not implemented yet.\n");
1896 1897
}

1898
static void state_subpixelx(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1899
{
1900 1901
    if (state->render_states[WINED3D_RS_SUBPIXELX])
        FIXME("Render state WINED3D_RS_SUBPIXELX not implemented yet.\n");
1902 1903
}

1904
static void state_stippleenable(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1905
{
1906 1907
    if (state->render_states[WINED3D_RS_STIPPLEENABLE])
        FIXME("Render state WINED3D_RS_STIPPLEENABLE not implemented yet.\n");
1908 1909
}

1910
static void state_mipmaplodbias(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1911
{
1912 1913
    if (state->render_states[WINED3D_RS_MIPMAPLODBIAS])
        FIXME("Render state WINED3D_RS_MIPMAPLODBIAS not implemented yet.\n");
1914 1915
}

1916
static void state_anisotropy(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1917
{
1918 1919
    if (state->render_states[WINED3D_RS_ANISOTROPY])
        FIXME("Render state WINED3D_RS_ANISOTROPY not implemented yet.\n");
1920 1921
}

1922
static void state_flushbatch(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1923
{
1924 1925
    if (state->render_states[WINED3D_RS_FLUSHBATCH])
        FIXME("Render state WINED3D_RS_FLUSHBATCH not implemented yet.\n");
1926 1927
}

1928
static void state_translucentsi(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1929
{
1930 1931
    if (state->render_states[WINED3D_RS_TRANSLUCENTSORTINDEPENDENT])
        FIXME("Render state WINED3D_RS_TRANSLUCENTSORTINDEPENDENT not implemented yet.\n");
1932 1933
}

1934
static void state_extents(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1935
{
1936 1937
    if (state->render_states[WINED3D_RS_EXTENTS])
        FIXME("Render state WINED3D_RS_EXTENTS not implemented yet.\n");
1938 1939
}

1940
static void state_ckeyblend(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1941
{
1942 1943
    if (state->render_states[WINED3D_RS_COLORKEYBLENDENABLE])
        FIXME("Render state WINED3D_RS_COLORKEYBLENDENABLE not implemented yet.\n");
1944 1945
}

1946
static void state_swvp(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
1947
{
1948
    static int once;
1949
    if (state->render_states[WINED3D_RS_SOFTWAREVERTEXPROCESSING])
1950 1951 1952 1953
    {
        if (!once++)
            FIXME("Software vertex processing not implemented.\n");
    }
1954 1955
}

1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
static void get_src_and_opr(DWORD arg, BOOL is_alpha, GLenum* source, GLenum* operand) {
    /* The WINED3DTA_ALPHAREPLICATE flag specifies the alpha component of the
    * input should be used for all input components. The WINED3DTA_COMPLEMENT
    * flag specifies the complement of the input should be used. */
    BOOL from_alpha = is_alpha || arg & WINED3DTA_ALPHAREPLICATE;
    BOOL complement = arg & WINED3DTA_COMPLEMENT;

    /* Calculate the operand */
    if (complement) {
        if (from_alpha) *operand = GL_ONE_MINUS_SRC_ALPHA;
        else *operand = GL_ONE_MINUS_SRC_COLOR;
    } else {
        if (from_alpha) *operand = GL_SRC_ALPHA;
        else *operand = GL_SRC_COLOR;
    }

    /* Calculate the source */
    switch (arg & WINED3DTA_SELECTMASK) {
        case WINED3DTA_CURRENT: *source = GL_PREVIOUS_EXT; break;
        case WINED3DTA_DIFFUSE: *source = GL_PRIMARY_COLOR_EXT; break;
        case WINED3DTA_TEXTURE: *source = GL_TEXTURE; break;
        case WINED3DTA_TFACTOR: *source = GL_CONSTANT_EXT; break;
        case WINED3DTA_SPECULAR:
            /*
            * According to the GL_ARB_texture_env_combine specs, SPECULAR is
            * 'Secondary color' and isn't supported until base GL supports it
            * There is no concept of temp registers as far as I can tell
            */
            FIXME("Unhandled texture arg WINED3DTA_SPECULAR\n");
            *source = GL_TEXTURE;
            break;
        default:
            FIXME("Unrecognized texture arg %#x\n", arg);
            *source = GL_TEXTURE;
            break;
    }
}

/* Setup the texture operations texture stage states */
1995
static void set_tex_op(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
1996
        BOOL isAlpha, int Stage, enum wined3d_texture_op op, DWORD arg1, DWORD arg2, DWORD arg3)
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
{
    GLenum src1, src2, src3;
    GLenum opr1, opr2, opr3;
    GLenum comb_target;
    GLenum src0_target, src1_target, src2_target;
    GLenum opr0_target, opr1_target, opr2_target;
    GLenum scal_target;
    GLenum opr=0, invopr, src3_target, opr3_target;
    BOOL Handled = FALSE;

    TRACE("Alpha?(%d), Stage:%d Op(%s), a1(%d), a2(%d), a3(%d)\n", isAlpha, Stage, debug_d3dtop(op), arg1, arg2, arg3);

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
    /* Operations usually involve two args, src0 and src1 and are operations
     * of the form (a1 <operation> a2). However, some of the more complex
     * operations take 3 parameters. Instead of the (sensible) addition of a3,
     * Microsoft added in a third parameter called a0. Therefore these are
     * operations of the form a0 <operation> a1 <operation> a2. I.e., the new
     * parameter goes to the front.
     *
     * However, below we treat the new (a0) parameter as src2/opr2, so in the
     * actual functions below, expect their syntax to differ slightly to those
     * listed in the manuals. I.e., replace arg1 with arg3, arg2 with arg1 and
     * arg3 with arg2. This affects WINED3DTOP_MULTIPLYADD and WINED3DTOP_LERP. */
2020

2021 2022 2023 2024 2025 2026 2027 2028 2029
    if (isAlpha)
    {
        comb_target = GL_COMBINE_ALPHA;
        src0_target = GL_SOURCE0_ALPHA;
        src1_target = GL_SOURCE1_ALPHA;
        src2_target = GL_SOURCE2_ALPHA;
        opr0_target = GL_OPERAND0_ALPHA;
        opr1_target = GL_OPERAND1_ALPHA;
        opr2_target = GL_OPERAND2_ALPHA;
2030 2031
        scal_target = GL_ALPHA_SCALE;
    }
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
    else
    {
        comb_target = GL_COMBINE_RGB;
        src0_target = GL_SOURCE0_RGB;
        src1_target = GL_SOURCE1_RGB;
        src2_target = GL_SOURCE2_RGB;
        opr0_target = GL_OPERAND0_RGB;
        opr1_target = GL_OPERAND1_RGB;
        opr2_target = GL_OPERAND2_RGB;
        scal_target = GL_RGB_SCALE;
2042 2043 2044 2045
    }

        /* If a texture stage references an invalid texture unit the stage just
        * passes through the result from the previous stage */
2046
    if (is_invalid_op(state, Stage, op, arg1, arg2, arg3))
2047
    {
2048
        arg1 = WINED3DTA_CURRENT;
2049
        op = WINED3D_TOP_SELECT_ARG1;
2050 2051
    }

2052
    if (isAlpha && !state->textures[Stage] && arg1 == WINED3DTA_TEXTURE)
2053
    {
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
        get_src_and_opr(WINED3DTA_DIFFUSE, isAlpha, &src1, &opr1);
    } else {
        get_src_and_opr(arg1, isAlpha, &src1, &opr1);
    }
    get_src_and_opr(arg2, isAlpha, &src2, &opr2);
    get_src_and_opr(arg3, isAlpha, &src3, &opr3);

    TRACE("ct(%x), 1:(%x,%x), 2:(%x,%x), 3:(%x,%x)\n", comb_target, src1, opr1, src2, opr2, src3, opr3);

    Handled = TRUE; /* Assume will be handled */

    /* Other texture operations require special extensions: */
2066
    if (gl_info->supported[NV_TEXTURE_ENV_COMBINE4])
2067
    {
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
        if (isAlpha) {
            opr = GL_SRC_ALPHA;
            invopr = GL_ONE_MINUS_SRC_ALPHA;
            src3_target = GL_SOURCE3_ALPHA_NV;
            opr3_target = GL_OPERAND3_ALPHA_NV;
        } else {
            opr = GL_SRC_COLOR;
            invopr = GL_ONE_MINUS_SRC_COLOR;
            src3_target = GL_SOURCE3_RGB_NV;
            opr3_target = GL_OPERAND3_RGB_NV;
        }
2079 2080 2081
        switch (op)
        {
            case WINED3D_TOP_DISABLE: /* Only for alpha */
2082
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2083
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2084
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
2085
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2086
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
2087
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2088
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2089
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2090
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2091
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2092
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2093
                checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
2094
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2095
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
2096
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2097
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
                break;

            case WINED3D_TOP_SELECT_ARG1: /* = a1 * 1 + 0 * 0 */
            case WINED3D_TOP_SELECT_ARG2: /* = a2 * 1 + 0 * 0 */
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
                if (op == WINED3D_TOP_SELECT_ARG1)
                {
                    gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
                    checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
                    gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
                    checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
                }
                else
                {
                    gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
                    checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
                    gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
                    checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
                }
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
                checkGLcall("GL_TEXTURE_ENV, src2_target, GL_ZERO");
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2131 2132 2133
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
                break;

2134
            case WINED3D_TOP_MODULATE:
2135
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2136
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
2137
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2138
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2139
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2140
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2141
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2142
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2143
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2144
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2145
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2146
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2147
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2148
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2149
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2150
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2151
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2152
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
2153
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2154 2155
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2156
            case WINED3D_TOP_MODULATE_2X:
2157
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2158
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
2159
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2160
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2161
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2162
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2163
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2164
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2165
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2166
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2167
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2168
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2169
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2170
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2171
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2172
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2173
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2174
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
2175
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2176 2177
                checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
                break;
2178
            case WINED3D_TOP_MODULATE_4X:
2179
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2180
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD"); /* Add = a0*a1 + a2*a3 */
2181
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2182
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2183
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2184
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2185
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2186
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2187
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2188
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2189
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_ZERO);
2190
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2191
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2192
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2193
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2194
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2195
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2196
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr1");
2197
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
2198 2199 2200
                checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
                break;

2201
            case WINED3D_TOP_ADD:
2202
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2203
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2204
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2205
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2206
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2207
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2208
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2209
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2210
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2211
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2212
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2213
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2214
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2215
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2216
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2217
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2218
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2219
                checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2220
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2221 2222 2223
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;

2224
            case WINED3D_TOP_ADD_SIGNED:
2225
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED);
2226
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED");
2227
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2228
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2229
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2230
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2231
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2232
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2233
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2234
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2235
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2236
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2237
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2238
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2239
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2240
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2241
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2242
                checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2243
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2244 2245 2246
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;

2247
            case WINED3D_TOP_ADD_SIGNED_2X:
2248
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED);
2249
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED");
2250
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2251
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2252
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2253
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2254
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2255
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2256
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2257
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2258
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2259
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2260
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2261
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2262
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2263
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2264
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2265
                checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2266
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2267 2268 2269
                checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
                break;

2270
            case WINED3D_TOP_ADD_SMOOTH:
2271
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2272
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2273
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2274
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2275
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2276
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2277
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2278
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2279
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2280
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2281
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2282
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2283
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2284
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2285
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
2286 2287 2288 2289 2290 2291 2292
                checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
                    case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                }
2293
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2294
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2295
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2296 2297 2298
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;

2299
            case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
2300
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2301
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2302
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2303
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2304
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2305
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2306
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_PRIMARY_COLOR);
2307
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_PRIMARY_COLOR");
2308
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2309
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2310
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2311
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2312
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2313
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2314
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_PRIMARY_COLOR);
2315
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_PRIMARY_COLOR");
2316
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2317
                checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2318
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2319 2320
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2321
            case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
2322
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2323
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2324
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2325
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2326
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2327
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2328
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_TEXTURE);
2329
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_TEXTURE");
2330
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2331
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2332
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2333
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2334
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2335
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2336
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
2337
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
2338
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2339
                checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2340
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2341 2342
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2343
            case WINED3D_TOP_BLEND_FACTOR_ALPHA:
2344
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2345
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2346
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2347
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2348
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2349
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2350
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_CONSTANT);
2351
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_CONSTANT");
2352
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2353
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2354
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2355
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2356
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2357
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2358
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_CONSTANT);
2359
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_CONSTANT");
2360
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2361
                checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2362
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2363 2364
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2365
            case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
2366
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2367
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2368
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2369
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2370
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2371
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2372
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2373
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2374
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2375
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2376
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2377
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2378
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2379
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2380
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_TEXTURE);
2381
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_TEXTURE");
2382
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA);
2383
                checkGLcall("GL_TEXTURE_ENV, opr3_target, GL_ONE_MINUS_SRC_ALPHA");
2384
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2385 2386
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2387
            case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
2388 2389 2390
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");                 /* Add = a0*a1 + a2*a3 */
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);  /*  a0 = src1/opr1     */
2391
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2392 2393 2394
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");                   /*  a1 = 1 (see docs)  */
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2395
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2396
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2397
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2398
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);  /*  a2 = arg2          */
2399
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2400 2401 2402
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");                   /*  a3 = src1 alpha    */
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
2403 2404 2405 2406 2407
                checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
                switch (opr) {
                    case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                }
2408
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2409
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2410
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2411 2412
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2413
            case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
2414
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2415
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2416
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2417
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2418
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2419
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2420
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2421
                checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2422
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2423
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2424
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
2425 2426 2427 2428 2429
                checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                }
2430
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2431
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
2432
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2433
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2434
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2435
                checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2436
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2437 2438
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2439
            case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
2440
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2441
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2442
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2443
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2444
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2445
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2446
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2447
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2448
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2449
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2450
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2451
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2452
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2453
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2454
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, src1);
2455 2456 2457 2458 2459 2460 2461
                checkGLcall("GL_TEXTURE_ENV, src3_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                }
2462
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr);
2463
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr");
2464
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2465 2466
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2467
            case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
2468
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2469
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2470
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2471 2472 2473 2474 2475 2476 2477
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
                    case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                }
2478
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2479
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2480
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2481
                checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2482
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2483
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2484
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
2485 2486 2487 2488 2489
                checkGLcall("GL_TEXTURE_ENV, src2_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                }
2490
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr);
2491
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr");
2492
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, GL_ZERO);
2493
                checkGLcall("GL_TEXTURE_ENV, src3_target, GL_ZERO");
2494
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, invopr);
2495
                checkGLcall("GL_TEXTURE_ENV, opr3_target, invopr");
2496
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2497 2498
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;
2499
            case WINED3D_TOP_MULTIPLY_ADD:
2500
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2501
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2502
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src3);
2503
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2504
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr3);
2505
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2506
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, GL_ZERO);
2507
                checkGLcall("GL_TEXTURE_ENV, src1_target, GL_ZERO");
2508
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, invopr);
2509
                checkGLcall("GL_TEXTURE_ENV, opr1_target, invopr");
2510
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src1);
2511
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2512
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr1);
2513
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2514
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src3_target, src2);
2515
                checkGLcall("GL_TEXTURE_ENV, src3_target, src3");
2516
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr3_target, opr2);
2517
                checkGLcall("GL_TEXTURE_ENV, opr3_target, opr3");
2518
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2519 2520 2521
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
                break;

2522 2523
            case WINED3D_TOP_BUMPENVMAP:
            case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
2524
                FIXME("Implement bump environment mapping in GL_NV_texture_env_combine4 path\n");
2525 2526
                Handled = FALSE;
                break;
2527 2528 2529 2530

            default:
                Handled = FALSE;
        }
2531 2532 2533
        if (Handled)
        {
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
2534 2535 2536 2537 2538 2539 2540 2541
            checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV");

            return;
        }
    } /* GL_NV_texture_env_combine4 */

    Handled = TRUE; /* Again, assume handled */
    switch (op) {
2542
        case WINED3D_TOP_DISABLE: /* Only for alpha */
2543
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
2544
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2545
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT);
2546
            checkGLcall("GL_TEXTURE_ENV, src0_target, GL_PREVIOUS_EXT");
2547
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA);
2548
            checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_SRC_ALPHA");
2549
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2550 2551
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2552
        case WINED3D_TOP_SELECT_ARG1:
2553
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
2554
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2555
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2556
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2557
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2558
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2559
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2560 2561
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2562
        case WINED3D_TOP_SELECT_ARG2:
2563
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_REPLACE);
2564
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_REPLACE");
2565
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src2);
2566
            checkGLcall("GL_TEXTURE_ENV, src0_target, src2");
2567
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr2);
2568
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr2");
2569
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2570 2571
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2572
        case WINED3D_TOP_MODULATE:
2573
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
2574
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
2575
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2576
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2577
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2578
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2579
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2580
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2581
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2582
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2583
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2584 2585
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2586
        case WINED3D_TOP_MODULATE_2X:
2587
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
2588
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
2589
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2590
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2591
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2592
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2593
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2594
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2595
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2596
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2597
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2598 2599
            checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
            break;
2600
        case WINED3D_TOP_MODULATE_4X:
2601
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE);
2602
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE");
2603
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2604
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2605
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2606
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2607
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2608
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2609
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2610
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2611
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 4);
2612 2613
            checkGLcall("GL_TEXTURE_ENV, scal_target, 4");
            break;
2614
        case WINED3D_TOP_ADD:
2615
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD);
2616
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD");
2617
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2618
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2619
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2620
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2621
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2622
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2623
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2624
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2625
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2626 2627
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2628
        case WINED3D_TOP_ADD_SIGNED:
2629
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED);
2630
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED");
2631
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2632
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2633
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2634
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2635
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2636
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2637
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2638
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2639
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2640 2641
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2642
        case WINED3D_TOP_ADD_SIGNED_2X:
2643
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED);
2644
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_ADD_SIGNED");
2645
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2646
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2647
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2648
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2649
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2650
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2651
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2652
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2653
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 2);
2654 2655
            checkGLcall("GL_TEXTURE_ENV, scal_target, 2");
            break;
2656
        case WINED3D_TOP_SUBTRACT:
2657
            if (gl_info->supported[ARB_TEXTURE_ENV_COMBINE])
2658
            {
2659
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_SUBTRACT);
2660
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_SUBTRACT");
2661
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2662
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2663
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2664
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2665
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2666
                checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2667
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2668
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2669
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2670 2671 2672 2673 2674 2675
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else {
                FIXME("This version of opengl does not support GL_SUBTRACT\n");
            }
            break;

2676
        case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
2677
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE);
2678
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE");
2679
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2680
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2681
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2682
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2683
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2684
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2685
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2686
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2687
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_PRIMARY_COLOR);
2688
            checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PRIMARY_COLOR");
2689
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2690
            checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2691
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2692 2693
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2694
        case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
2695
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE);
2696
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE");
2697
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2698
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2699
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2700
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2701
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2702
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2703
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2704
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2705
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_TEXTURE);
2706
            checkGLcall("GL_TEXTURE_ENV, src2_target, GL_TEXTURE");
2707
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2708
            checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2709
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2710 2711
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2712
        case WINED3D_TOP_BLEND_FACTOR_ALPHA:
2713
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE);
2714
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE");
2715
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2716
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2717
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2718
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2719
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2720
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2721
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2722
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2723
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_CONSTANT);
2724
            checkGLcall("GL_TEXTURE_ENV, src2_target, GL_CONSTANT");
2725
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2726
            checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2727
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2728 2729
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2730
        case WINED3D_TOP_BLEND_CURRENT_ALPHA:
2731
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE);
2732
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE");
2733
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2734
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2735
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2736
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2737
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2738
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2739
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2740
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2741
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, GL_PREVIOUS);
2742
            checkGLcall("GL_TEXTURE_ENV, src2_target, GL_PREVIOUS");
2743
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA);
2744
            checkGLcall("GL_TEXTURE_ENV, opr2_target, GL_SRC_ALPHA");
2745
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2746 2747
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2748
        case WINED3D_TOP_DOTPRODUCT3:
2749
            if (gl_info->supported[ARB_TEXTURE_ENV_DOT3])
2750
            {
2751
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB);
2752
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_ARB");
2753
            }
2754
            else if (gl_info->supported[EXT_TEXTURE_ENV_DOT3])
2755
            {
2756
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT);
2757 2758 2759 2760
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_DOT3_RGBA_EXT");
            } else {
                FIXME("This version of opengl does not support GL_DOT3\n");
            }
2761
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2762
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2763
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2764
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2765
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2766
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2767
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2768
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2769
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2770 2771
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2772
        case WINED3D_TOP_LERP:
2773
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE);
2774
            checkGLcall("GL_TEXTURE_ENV, comb_target, GL_INTERPOLATE");
2775
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2776
            checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2777
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2778
            checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2779
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src2);
2780
            checkGLcall("GL_TEXTURE_ENV, src1_target, src2");
2781
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr2);
2782
            checkGLcall("GL_TEXTURE_ENV, opr1_target, opr2");
2783
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src3);
2784
            checkGLcall("GL_TEXTURE_ENV, src2_target, src3");
2785
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr3);
2786
            checkGLcall("GL_TEXTURE_ENV, opr2_target, opr3");
2787
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2788 2789
            checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            break;
2790
        case WINED3D_TOP_ADD_SMOOTH:
2791
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2792
            {
2793
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2794
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2795
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2796 2797 2798 2799 2800 2801 2802
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
                    case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                }
2803
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2804
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2805
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2806
                checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2807
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2808
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2809
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2810
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2811
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2812
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2813
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2814 2815 2816
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2817
            break;
2818
        case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
2819
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2820
            {
2821
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2822
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2823
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, GL_TEXTURE);
2824
                checkGLcall("GL_TEXTURE_ENV, src0_target, GL_TEXTURE");
2825
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_ALPHA);
2826
                checkGLcall("GL_TEXTURE_ENV, opr0_target, GL_ONE_MINUS_SRC_APHA");
2827
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2828
                checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2829
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2830
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2831
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2832
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2833
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2834
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2835
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2836 2837 2838
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2839
            break;
2840
        case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
2841
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2842
            {
2843
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2844
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2845
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2846 2847 2848 2849 2850 2851 2852
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                }
2853
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2854
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2855
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2856
                checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2857
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2858
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2859
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2860
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2861
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2862
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2863
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2864 2865 2866
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2867
            break;
2868
        case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
2869
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2870
            {
2871
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2872
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2873
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2874
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2875
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2876
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2877
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2878 2879 2880 2881 2882 2883 2884
                checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                }
2885
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2886
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2887
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2888
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2889
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2890
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2891
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2892 2893 2894
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2895
            break;
2896
        case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
2897
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2898
            {
2899
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2900
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2901
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2902 2903 2904 2905 2906 2907 2908
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                }
2909
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2910
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2911
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2912
                checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
2913
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr1);
2914
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr1");
2915
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2916
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2917
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2918
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2919
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2920 2921 2922
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2923
            break;
2924
        case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
2925
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2926
            {
2927
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2928
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2929
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2930 2931 2932 2933 2934 2935 2936
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_ONE_MINUS_SRC_COLOR; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_SRC_COLOR; break;
                    case GL_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                }
2937
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr);
2938
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr");
2939
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src1);
2940 2941 2942 2943 2944 2945 2946
                checkGLcall("GL_TEXTURE_ENV, src1_target, src1");
                switch (opr1) {
                    case GL_SRC_COLOR: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_COLOR: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                    case GL_SRC_ALPHA: opr = GL_SRC_ALPHA; break;
                    case GL_ONE_MINUS_SRC_ALPHA: opr = GL_ONE_MINUS_SRC_ALPHA; break;
                }
2947
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr);
2948
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr");
2949
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2950
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2951
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2952
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2953
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2954 2955 2956
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2957
            break;
2958
        case WINED3D_TOP_MULTIPLY_ADD:
2959
            if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3])
2960
            {
2961
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI);
2962
                checkGLcall("GL_TEXTURE_ENV, comb_target, GL_MODULATE_ADD_ATI");
2963
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src0_target, src1);
2964
                checkGLcall("GL_TEXTURE_ENV, src0_target, src1");
2965
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr0_target, opr1);
2966
                checkGLcall("GL_TEXTURE_ENV, opr0_target, opr1");
2967
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src1_target, src3);
2968
                checkGLcall("GL_TEXTURE_ENV, src1_target, src3");
2969
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr1_target, opr3);
2970
                checkGLcall("GL_TEXTURE_ENV, opr1_target, opr3");
2971
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, src2_target, src2);
2972
                checkGLcall("GL_TEXTURE_ENV, src2_target, src2");
2973
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, opr2_target, opr2);
2974
                checkGLcall("GL_TEXTURE_ENV, opr2_target, opr2");
2975
                gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, scal_target, 1);
2976 2977 2978
                checkGLcall("GL_TEXTURE_ENV, scal_target, 1");
            } else
                Handled = FALSE;
2979
            break;
2980 2981
        case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
        case WINED3D_TOP_BUMPENVMAP:
2982
            if (gl_info->supported[NV_TEXTURE_SHADER2])
2983
            {
2984 2985 2986 2987 2988
                /* Technically texture shader support without register combiners is possible, but not expected to occur
                 * on real world cards, so for now a fixme should be enough
                 */
                FIXME("Implement bump mapping with GL_NV_texture_shader in non register combiner path\n");
            }
2989 2990 2991
            Handled = FALSE;
            break;

2992 2993 2994 2995 2996 2997
        default:
            Handled = FALSE;
    }

    if (Handled) {
        BOOL  combineOK = TRUE;
2998
        if (gl_info->supported[NV_TEXTURE_ENV_COMBINE4])
2999
        {
3000 3001
            DWORD op2;

3002
            if (isAlpha)
3003
                op2 = state->texture_states[Stage][WINED3D_TSS_COLOR_OP];
3004
            else
3005
                op2 = state->texture_states[Stage][WINED3D_TSS_ALPHA_OP];
3006 3007

            /* Note: If COMBINE4 in effect can't go back to combine! */
3008 3009 3010 3011 3012 3013 3014 3015 3016
            switch (op2)
            {
                case WINED3D_TOP_ADD_SMOOTH:
                case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
                case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
                case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
                case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
                case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
                case WINED3D_TOP_MULTIPLY_ADD:
3017
                    /* Ignore those implemented in both cases */
3018 3019 3020 3021
                    switch (op)
                    {
                        case WINED3D_TOP_SELECT_ARG1:
                        case WINED3D_TOP_SELECT_ARG2:
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
                            combineOK = FALSE;
                            Handled   = FALSE;
                            break;
                        default:
                            FIXME("Can't use COMBINE4 and COMBINE together, thisop=%s, otherop=%s, isAlpha(%d)\n", debug_d3dtop(op), debug_d3dtop(op2), isAlpha);
                            return;
                    }
            }
        }

3032 3033
        if (combineOK)
        {
3034
            gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
3035
            checkGLcall("GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE");
3036 3037 3038 3039 3040 3041 3042 3043 3044 3045

            return;
        }
    }

    /* After all the extensions, if still unhandled, report fixme */
    FIXME("Unhandled texture operation %s\n", debug_d3dtop(op));
}


3046
static void tex_colorop(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3047
{
3048
    DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
3049
    BOOL tex_used = context->fixed_function_usage_map & (1u << stage);
3050
    DWORD mapped_stage = context->tex_unit_map[stage];
3051
    const struct wined3d_gl_info *gl_info = context->gl_info;
3052 3053 3054

    TRACE("Setting color op for stage %d\n", stage);

3055
    /* Using a pixel shader? Don't care for anything here, the shader applying does it */
3056
    if (use_ps(state)) return;
3057

3058 3059
    if (stage != mapped_stage) WARN("Using non 1:1 mapping: %d -> %d!\n", stage, mapped_stage);

3060 3061
    if (mapped_stage != WINED3D_UNMAPPED_STAGE)
    {
3062
        if (tex_used && mapped_stage >= gl_info->limits.textures)
3063
        {
3064
            FIXME("Attempt to enable unsupported stage!\n");
3065 3066
            return;
        }
3067
        context_active_texture(context, gl_info, mapped_stage);
3068 3069
    }

3070
    if (stage >= context->lowest_disabled_stage)
3071
    {
3072
        TRACE("Stage disabled\n");
3073 3074
        if (mapped_stage != WINED3D_UNMAPPED_STAGE)
        {
3075
            /* Disable everything here */
3076
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3077
            checkGLcall("glDisable(GL_TEXTURE_2D)");
3078
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3079
            checkGLcall("glDisable(GL_TEXTURE_3D)");
3080
            if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3081
            {
3082
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3083 3084
                checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
            }
3085
            if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3086
            {
3087
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3088 3089
                checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
            }
3090
        }
3091 3092 3093 3094
        /* All done */
        return;
    }

3095 3096 3097
    /* The sampler will also activate the correct texture dimensions, so no
     * need to do it here if the sampler for this stage is dirty. */
    if (!isStateDirty(context, STATE_SAMPLER(stage)) && tex_used)
3098
        texture_activate_dimensions(state->textures[stage], gl_info);
3099

3100
    set_tex_op(gl_info, state, FALSE, stage,
3101 3102 3103 3104
            state->texture_states[stage][WINED3D_TSS_COLOR_OP],
            state->texture_states[stage][WINED3D_TSS_COLOR_ARG1],
            state->texture_states[stage][WINED3D_TSS_COLOR_ARG2],
            state->texture_states[stage][WINED3D_TSS_COLOR_ARG0]);
3105 3106
}

3107
void tex_alphaop(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3108
{
3109
    DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
3110
    BOOL tex_used = context->fixed_function_usage_map & (1u << stage);
3111
    DWORD mapped_stage = context->tex_unit_map[stage];
3112
    const struct wined3d_gl_info *gl_info = context->gl_info;
3113
    DWORD op, arg1, arg2, arg0;
3114 3115

    TRACE("Setting alpha op for stage %d\n", stage);
3116
    /* Do not care for enabled / disabled stages, just assign the settings. colorop disables / enables required stuff */
3117 3118
    if (mapped_stage != WINED3D_UNMAPPED_STAGE)
    {
3119
        if (tex_used && mapped_stage >= gl_info->limits.textures)
3120
        {
3121
            FIXME("Attempt to enable unsupported stage!\n");
3122 3123
            return;
        }
3124
        context_active_texture(context, gl_info, mapped_stage);
3125 3126
    }

3127 3128 3129 3130
    op = state->texture_states[stage][WINED3D_TSS_ALPHA_OP];
    arg1 = state->texture_states[stage][WINED3D_TSS_ALPHA_ARG1];
    arg2 = state->texture_states[stage][WINED3D_TSS_ALPHA_ARG2];
    arg0 = state->texture_states[stage][WINED3D_TSS_ALPHA_ARG0];
3131

3132
    if (state->render_states[WINED3D_RS_COLORKEYENABLE] && !stage && state->textures[0])
3133
    {
3134
        struct wined3d_texture *texture = state->textures[0];
3135
        GLenum texture_dimensions = texture->target;
3136 3137 3138

        if (texture_dimensions == GL_TEXTURE_2D || texture_dimensions == GL_TEXTURE_RECTANGLE_ARB)
        {
3139
            if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT && !texture->resource.format->alpha_size)
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
            {
                /* Color keying needs to pass alpha values from the texture through to have the alpha test work
                 * properly. On the other hand applications can still use texture combiners apparently. This code
                 * takes care that apps cannot remove the texture's alpha channel entirely.
                 *
                 * The fixup is required for Prince of Persia 3D(prison bars), while Moto racer 2 requires
                 * D3DTOP_MODULATE to work on color keyed surfaces. Aliens vs Predator 1 uses color keyed textures
                 * and alpha component of diffuse color to draw things like translucent text and perform other
                 * blending effects.
                 *
                 * Aliens vs Predator 1 relies on diffuse alpha having an effect, so it cannot be ignored. To
                 * provide the behavior expected by the game, while emulating the colorkey, diffuse alpha must be
                 * modulated with texture alpha. OTOH, Moto racer 2 at some points sets alphaop/alphaarg to
                 * SELECTARG/CURRENT, yet puts garbage in diffuse alpha (zeroes). This works on native, because the
                 * game disables alpha test and alpha blending. Alpha test is overwritten by wine's for purposes of
                 * color-keying though, so this will lead to missing geometry if texture alpha is modulated (pixels
                 * fail alpha test). To get around this, ALPHABLENDENABLE state is checked: if the app enables alpha
                 * blending, it can be expected to provide meaningful values in diffuse alpha, so it should be
                 * modulated with texture alpha; otherwise, selecting diffuse alpha is ignored in favour of texture
                 * alpha.
                 *
                 * What to do with multitexturing? So far no app has been found that uses color keying with
                 * multitexturing */
3163
                if (op == WINED3D_TOP_DISABLE)
3164
                {
3165
                    arg1 = WINED3DTA_TEXTURE;
3166
                    op = WINED3D_TOP_SELECT_ARG1;
3167
                }
3168
                else if (op == WINED3D_TOP_SELECT_ARG1 && arg1 != WINED3DTA_TEXTURE)
3169
                {
3170
                    if (state->render_states[WINED3D_RS_ALPHABLENDENABLE])
3171 3172
                    {
                        arg2 = WINED3DTA_TEXTURE;
3173
                        op = WINED3D_TOP_MODULATE;
3174 3175 3176
                    }
                    else arg1 = WINED3DTA_TEXTURE;
                }
3177
                else if (op == WINED3D_TOP_SELECT_ARG2 && arg2 != WINED3DTA_TEXTURE)
3178
                {
3179
                    if (state->render_states[WINED3D_RS_ALPHABLENDENABLE])
3180 3181
                    {
                        arg1 = WINED3DTA_TEXTURE;
3182
                        op = WINED3D_TOP_MODULATE;
3183 3184
                    }
                    else arg2 = WINED3DTA_TEXTURE;
3185 3186
                }
            }
3187 3188 3189
        }
    }

3190 3191 3192
    /* tex_alphaop is shared between the ffp and nvrc because the difference only comes down to
     * this if block here, and the other code(color keying, texture unit selection) are the same
     */
3193
    TRACE("Setting alpha op for stage %d\n", stage);
3194
    if (gl_info->supported[NV_REGISTER_COMBINERS])
3195
    {
3196
        set_tex_op_nvrc(gl_info, state, TRUE, stage, op, arg1, arg2, arg0,
3197
                mapped_stage, state->texture_states[stage][WINED3D_TSS_RESULT_ARG]);
3198 3199 3200
    }
    else
    {
3201
        set_tex_op(gl_info, state, TRUE, stage, op, arg1, arg2, arg0);
3202 3203 3204
    }
}

3205
static void transform_texture(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3206
{
3207
    const struct wined3d_gl_info *gl_info = context->gl_info;
3208 3209 3210
    unsigned int tex = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
    unsigned int mapped_stage = context->tex_unit_map[tex];
    struct wined3d_matrix mat;
3211

3212
    /* Ignore this when a vertex shader is used, or if the streams aren't sorted out yet */
3213
    if (use_vs(state) || isStateDirty(context, STATE_VDECL))
3214
    {
3215 3216 3217 3218
        TRACE("Using a vertex shader, or stream sources not sorted out yet, skipping\n");
        return;
    }

3219
    if (mapped_stage == WINED3D_UNMAPPED_STAGE) return;
3220
    if (mapped_stage >= gl_info->limits.textures) return;
3221

3222
    context_active_texture(context, gl_info, mapped_stage);
3223 3224 3225 3226 3227 3228 3229
    gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
    checkGLcall("glMatrixMode(GL_TEXTURE)");

    get_texture_matrix(context, state, mapped_stage, &mat);

    gl_info->gl_ops.gl.p_glLoadMatrixf(&mat._11);
    checkGLcall("glLoadMatrixf");
3230 3231
}

3232
static void unload_tex_coords(const struct wined3d_gl_info *gl_info)
3233
{
3234
    unsigned int texture_idx;
3235

3236
    for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
3237
    {
3238
        GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx));
3239
        gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3240 3241 3242
    }
}

3243 3244
static void load_tex_coords(const struct wined3d_context *context, const struct wined3d_stream_info *si,
        GLuint *curVBO, const struct wined3d_state *state)
3245
{
3246
    const struct wined3d_gl_info *gl_info = context->gl_info;
3247
    unsigned int mapped_stage = 0;
3248
    unsigned int textureNo;
3249

3250
    for (textureNo = 0; textureNo < context->d3d_info->limits.ffp_blend_stages; ++textureNo)
3251
    {
3252
        int coordIdx = state->texture_states[textureNo][WINED3D_TSS_TEXCOORD_INDEX];
3253

3254
        mapped_stage = context->tex_unit_map[textureNo];
3255
        if (mapped_stage == WINED3D_UNMAPPED_STAGE) continue;
3256

3257 3258 3259 3260 3261 3262
        if (mapped_stage >= gl_info->limits.texture_coords)
        {
            FIXME("Attempted to load unsupported texture coordinate %u\n", mapped_stage);
            continue;
        }

3263
        if (coordIdx < MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coordIdx))))
3264
        {
3265 3266
            const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];

3267 3268
            TRACE("Setting up texture %u, idx %d, coordindx %u, data {%#x:%p}.\n",
                    textureNo, mapped_stage, coordIdx, e->data.buffer_object, e->data.addr);
3269

3270
            if (*curVBO != e->data.buffer_object)
3271
            {
3272 3273
                GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
                checkGLcall("glBindBuffer");
3274
                *curVBO = e->data.buffer_object;
3275
            }
3276 3277 3278 3279

            GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
            checkGLcall("glClientActiveTextureARB");

3280
            /* The coords to supply depend completely on the fvf / vertex shader */
3281
            gl_info->gl_ops.gl.p_glTexCoordPointer(e->format->gl_vtx_format, e->format->gl_vtx_type, e->stride,
3282
                    e->data.addr + state->load_base_vertex_index * e->stride);
3283
            gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3284 3285 3286
        }
        else
        {
3287
            GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
3288 3289
        }
    }
3290
    if (gl_info->supported[NV_REGISTER_COMBINERS])
3291 3292
    {
        /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
3293
        for (textureNo = mapped_stage + 1; textureNo < gl_info->limits.textures; ++textureNo)
3294
        {
3295 3296 3297
            GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + textureNo, 0, 0, 0, 1));
        }
    }
3298 3299

    checkGLcall("loadTexCoords");
3300
}
3301

3302
static void tex_coordindex(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3303
{
3304
    DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
3305 3306 3307 3308
    static const GLfloat s_plane[] = { 1.0f, 0.0f, 0.0f, 0.0f };
    static const GLfloat t_plane[] = { 0.0f, 1.0f, 0.0f, 0.0f };
    static const GLfloat r_plane[] = { 0.0f, 0.0f, 1.0f, 0.0f };
    static const GLfloat q_plane[] = { 0.0f, 0.0f, 0.0f, 1.0f };
3309
    const struct wined3d_gl_info *gl_info = context->gl_info;
3310
    DWORD mapped_stage = context->tex_unit_map[stage];
3311

3312 3313
    if (mapped_stage == WINED3D_UNMAPPED_STAGE)
    {
3314 3315 3316
        TRACE("No texture unit mapped to stage %d. Skipping texture coordinates.\n", stage);
        return;
    }
3317

3318
    if (mapped_stage >= gl_info->limits.fragment_samplers)
3319
    {
3320
        WARN("stage %u not mapped to a valid texture unit (%u)\n", stage, mapped_stage);
3321 3322
        return;
    }
3323
    context_active_texture(context, gl_info, mapped_stage);
3324 3325 3326

    /* Values 0-7 are indexes into the FVF tex coords - See comments in DrawPrimitive
     *
3327
     * FIXME: When using generated texture coordinates, the index value is used to specify the wrapping mode.
3328
     * eg. SetTextureStageState( 0, WINED3D_TSS_TEXCOORDINDEX, WINED3D_TSS_TCI_CAMERASPACEPOSITION | 1 );
3329
     * means use the vertex position (camera-space) as the input texture coordinates
3330
     * for this texture stage, and the wrap mode set in the WINED3D_RS_WRAP1 render
3331 3332 3333
     * state. We do not (yet) support the WINED3DRENDERSTATE_WRAPx values, nor tie them up
     * to the TEXCOORDINDEX value
     */
3334
    switch (state->texture_states[stage][WINED3D_TSS_TEXCOORD_INDEX] & 0xffff0000)
3335 3336 3337 3338
    {
        case WINED3DTSS_TCI_PASSTHRU:
            /* Use the specified texture coordinates contained within the
             * vertex format. This value resolves to zero. */
3339 3340 3341 3342
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_S);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_T);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_R);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_Q);
3343
            checkGLcall("WINED3DTSS_TCI_PASSTHRU - Disable texgen.");
3344
            break;
3345

3346 3347 3348 3349
        case WINED3DTSS_TCI_CAMERASPACEPOSITION:
            /* CameraSpacePosition means use the vertex position, transformed to camera space,
             * as the input texture coordinates for this stage's texture transformation. This
             * equates roughly to EYE_LINEAR */
3350

3351 3352 3353 3354 3355 3356 3357 3358
            gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
            gl_info->gl_ops.gl.p_glPushMatrix();
            gl_info->gl_ops.gl.p_glLoadIdentity();
            gl_info->gl_ops.gl.p_glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_R, GL_EYE_PLANE, r_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_Q, GL_EYE_PLANE, q_plane);
            gl_info->gl_ops.gl.p_glPopMatrix();
3359
            checkGLcall("WINED3DTSS_TCI_CAMERASPACEPOSITION - Set eye plane.");
3360

3361 3362 3363
            gl_info->gl_ops.gl.p_glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
            gl_info->gl_ops.gl.p_glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
            gl_info->gl_ops.gl.p_glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
3364 3365
            checkGLcall("WINED3DTSS_TCI_CAMERASPACEPOSITION - Set texgen mode.");

3366 3367 3368
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_S);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_T);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_R);
3369 3370
            checkGLcall("WINED3DTSS_TCI_CAMERASPACEPOSITION - Enable texgen.");

3371
            break;
3372

3373
        case WINED3DTSS_TCI_CAMERASPACENORMAL:
3374 3375
            /* Note that NV_TEXGEN_REFLECTION support is implied when
             * ARB_TEXTURE_CUBE_MAP is supported */
3376
            if (!gl_info->supported[NV_TEXGEN_REFLECTION])
3377
            {
3378
                FIXME("WINED3DTSS_TCI_CAMERASPACENORMAL not supported.\n");
3379 3380 3381
                break;
            }

3382 3383 3384 3385 3386 3387 3388 3389
            gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
            gl_info->gl_ops.gl.p_glPushMatrix();
            gl_info->gl_ops.gl.p_glLoadIdentity();
            gl_info->gl_ops.gl.p_glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_R, GL_EYE_PLANE, r_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_Q, GL_EYE_PLANE, q_plane);
            gl_info->gl_ops.gl.p_glPopMatrix();
3390
            checkGLcall("WINED3DTSS_TCI_CAMERASPACENORMAL - Set eye plane.");
3391

3392 3393 3394
            gl_info->gl_ops.gl.p_glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
            gl_info->gl_ops.gl.p_glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
            gl_info->gl_ops.gl.p_glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
3395 3396
            checkGLcall("WINED3DTSS_TCI_CAMERASPACENORMAL - Set texgen mode.");

3397 3398 3399
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_S);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_T);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_R);
3400
            checkGLcall("WINED3DTSS_TCI_CAMERASPACENORMAL - Enable texgen.");
3401 3402 3403 3404

            break;

        case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
3405 3406
            /* Note that NV_TEXGEN_REFLECTION support is implied when
             * ARB_TEXTURE_CUBE_MAP is supported */
3407
            if (!gl_info->supported[NV_TEXGEN_REFLECTION])
3408
            {
3409
                FIXME("WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR not supported.\n");
3410
                break;
3411 3412
            }

3413 3414 3415 3416 3417 3418 3419 3420
            gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
            gl_info->gl_ops.gl.p_glPushMatrix();
            gl_info->gl_ops.gl.p_glLoadIdentity();
            gl_info->gl_ops.gl.p_glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_R, GL_EYE_PLANE, r_plane);
            gl_info->gl_ops.gl.p_glTexGenfv(GL_Q, GL_EYE_PLANE, q_plane);
            gl_info->gl_ops.gl.p_glPopMatrix();
3421
            checkGLcall("WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR - Set eye plane.");
3422

3423 3424 3425
            gl_info->gl_ops.gl.p_glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV);
            gl_info->gl_ops.gl.p_glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV);
            gl_info->gl_ops.gl.p_glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_NV);
3426 3427
            checkGLcall("WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR - Set texgen mode.");

3428 3429 3430
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_S);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_T);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_R);
3431
            checkGLcall("WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR - Enable texgen.");
3432 3433 3434

            break;

3435
        case WINED3DTSS_TCI_SPHEREMAP:
3436 3437
            gl_info->gl_ops.gl.p_glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
            gl_info->gl_ops.gl.p_glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
3438 3439
            checkGLcall("WINED3DTSS_TCI_SPHEREMAP - Set texgen mode.");

3440 3441 3442
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_S);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_GEN_T);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_R);
3443 3444 3445 3446
            checkGLcall("WINED3DTSS_TCI_SPHEREMAP - Enable texgen.");

            break;

3447
        default:
3448 3449
            FIXME("Unhandled WINED3D_TSS_TEXCOORD_INDEX %#x.\n",
                    state->texture_states[stage][WINED3D_TSS_TEXCOORD_INDEX]);
3450 3451 3452 3453
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_S);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_T);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_R);
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_GEN_Q);
3454 3455
            checkGLcall("Disable texgen.");

3456
            break;
3457
    }
3458

3459
    /* Update the texture matrix. */
3460
    if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + stage)))
3461
        transform_texture(context, state, STATE_TEXTURESTAGE(stage, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS));
3462

3463 3464
    if (!isStateDirty(context, STATE_VDECL) && context->namedArraysLoaded)
    {
3465
        /* Reload the arrays if we are using fixed function arrays to reflect the selected coord input
3466
         * source. Call loadTexCoords directly because there is no need to reparse the vertex declaration
3467 3468 3469
         * and do all the things linked to it
         * TODO: Tidy that up to reload only the arrays of the changed unit
         */
3470
        GLuint curVBO = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0U : 0;
3471

3472
        unload_tex_coords(gl_info);
3473
        load_tex_coords(context, &context->stream_info, &curVBO, state);
3474
    }
3475 3476
}

3477
static void sampler_texmatrix(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3478
{
3479 3480
    const DWORD sampler = state_id - STATE_SAMPLER(0);
    const struct wined3d_texture *texture = state->textures[sampler];
3481

3482
    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
3483

3484
    if(!texture) return;
3485
    /* The fixed function np2 texture emulation uses the texture matrix to fix up the coordinates
3486
     * wined3d_texture_apply_state_changes() multiplies the set matrix with a fixup matrix. Before the
3487 3488
     * scaling is reapplied or removed, the texture matrix has to be reapplied
     *
Tobias Jakobi's avatar
Tobias Jakobi committed
3489
     * The mapped stage is already active because the sampler() function below, which is part of the
3490 3491
     * misc pipeline
     */
3492 3493
    if (sampler < MAX_TEXTURES)
    {
3494
        const BOOL texIsPow2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT);
3495

3496
        if (texIsPow2 || (context->lastWasPow2Texture & (1u << sampler)))
3497
        {
3498
            if (texIsPow2)
3499
                context->lastWasPow2Texture |= 1u << sampler;
3500
            else
3501
                context->lastWasPow2Texture &= ~(1u << sampler);
3502 3503

            transform_texture(context, state,
3504
                    STATE_TEXTURESTAGE(context->tex_unit_map[sampler], WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS));
3505 3506 3507 3508
        }
    }
}

3509 3510
static enum wined3d_texture_address wined3d_texture_address_mode(const struct wined3d_texture *texture,
        enum wined3d_texture_address t)
3511
{
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
    if (t < WINED3D_TADDRESS_WRAP || t > WINED3D_TADDRESS_MIRROR_ONCE)
    {
        FIXME("Unrecognized or unsupported texture address mode %#x.\n", t);
        return WINED3D_TADDRESS_WRAP;
    }

    /* Cubemaps are always set to clamp, regardless of the sampler state. */
    if (texture->target == GL_TEXTURE_CUBE_MAP_ARB || ((texture->flags & WINED3D_TEXTURE_COND_NP2)
            && t == WINED3D_TADDRESS_WRAP))
        return WINED3D_TADDRESS_CLAMP;

    return t;
}

static void wined3d_sampler_desc_from_sampler_states(struct wined3d_sampler_desc *desc,
3527
        const struct wined3d_context *context, const DWORD *sampler_states, const struct wined3d_texture *texture)
3528 3529 3530
{
    union
    {
3531 3532
        float f;
        DWORD d;
3533 3534 3535 3536 3537
    } lod_bias;

    desc->address_u = wined3d_texture_address_mode(texture, sampler_states[WINED3D_SAMP_ADDRESS_U]);
    desc->address_v = wined3d_texture_address_mode(texture, sampler_states[WINED3D_SAMP_ADDRESS_V]);
    desc->address_w = wined3d_texture_address_mode(texture, sampler_states[WINED3D_SAMP_ADDRESS_W]);
3538 3539
    wined3d_color_from_d3dcolor((struct wined3d_color *)desc->border_color,
            sampler_states[WINED3D_SAMP_BORDER_COLOR]);
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
    if (sampler_states[WINED3D_SAMP_MAG_FILTER] > WINED3D_TEXF_ANISOTROPIC)
        FIXME("Unrecognized or unsupported WINED3D_SAMP_MAG_FILTER %#x.\n",
                sampler_states[WINED3D_SAMP_MAG_FILTER]);
    desc->mag_filter = min(max(sampler_states[WINED3D_SAMP_MAG_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR);
    if (sampler_states[WINED3D_SAMP_MIN_FILTER] > WINED3D_TEXF_ANISOTROPIC)
        FIXME("Unrecognized or unsupported WINED3D_SAMP_MIN_FILTER %#x.\n",
                sampler_states[WINED3D_SAMP_MIN_FILTER]);
    desc->min_filter = min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR);
    if (sampler_states[WINED3D_SAMP_MIP_FILTER] > WINED3D_TEXF_ANISOTROPIC)
        FIXME("Unrecognized or unsupported WINED3D_SAMP_MIP_FILTER %#x.\n",
                sampler_states[WINED3D_SAMP_MIP_FILTER]);
    desc->mip_filter = min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR);
    lod_bias.d = sampler_states[WINED3D_SAMP_MIPMAP_LOD_BIAS];
    desc->lod_bias = lod_bias.f;
    desc->min_lod = -1000.0f;
    desc->max_lod = 1000.0f;
    desc->max_anisotropy = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
    if ((sampler_states[WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_ANISOTROPIC
                && sampler_states[WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_ANISOTROPIC
                && sampler_states[WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_ANISOTROPIC)
            || (texture->flags & WINED3D_TEXTURE_COND_NP2))
        desc->max_anisotropy = 1;
3562
    desc->compare = texture->resource.format_flags & WINED3DFMT_FLAG_SHADOW;
3563
    desc->comparison_func = WINED3D_CMP_LESSEQUAL;
3564
    desc->srgb_decode = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
3565

3566
    if (!(texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING))
3567 3568 3569 3570 3571 3572 3573 3574 3575
    {
        desc->mag_filter = WINED3D_TEXF_POINT;
        desc->min_filter = WINED3D_TEXF_POINT;
        desc->mip_filter = WINED3D_TEXF_NONE;
    }

    if (texture->flags & WINED3D_TEXTURE_COND_NP2)
    {
        desc->mip_filter = WINED3D_TEXF_NONE;
3576
        if (context->gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
            desc->min_filter = WINED3D_TEXF_POINT;
    }
}

/* Enabling and disabling texture dimensions is done by texture stage state /
 * pixel shader setup, this function only has to bind textures and set the per
 * texture states. */
static void sampler(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
{
    DWORD sampler_idx = state_id - STATE_SAMPLER(0);
    DWORD mapped_stage = context->tex_unit_map[sampler_idx];
    const struct wined3d_gl_info *gl_info = context->gl_info;

    TRACE("Sampler %u.\n", sampler_idx);
3591

3592

3593 3594
    if (mapped_stage == WINED3D_UNMAPPED_STAGE)
    {
3595
        TRACE("No sampler mapped to stage %u. Returning.\n", sampler_idx);
3596 3597 3598
        return;
    }

3599
    if (mapped_stage >= gl_info->limits.combined_samplers)
3600
    {
3601 3602
        return;
    }
3603
    context_active_texture(context, gl_info, mapped_stage);
3604

3605
    if (state->textures[sampler_idx])
3606
    {
3607 3608 3609 3610 3611 3612
        struct wined3d_texture *texture = state->textures[sampler_idx];
        BOOL srgb = state->sampler_states[sampler_idx][WINED3D_SAMP_SRGB_TEXTURE];
        const DWORD *sampler_states = state->sampler_states[sampler_idx];
        struct wined3d_sampler_desc desc;
        struct gl_texture *gl_tex;
        unsigned int base_level;
3613

3614
        wined3d_sampler_desc_from_sampler_states(&desc, context, sampler_states, texture);
3615

3616
        wined3d_texture_bind(texture, context, srgb);
3617 3618
        if (!gl_info->supported[ARB_SAMPLER_OBJECTS])
        {
3619
            wined3d_texture_apply_sampler_desc(texture, &desc, context);
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
        }
        else
        {
            struct wined3d_device *device = context->swapchain->device;
            struct wined3d_sampler *sampler;
            struct wine_rb_entry *entry;

            if ((entry = wine_rb_get(&device->samplers, &desc)))
            {
                sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
            }
            else
            {
                if (FAILED(wined3d_sampler_create(device, &desc, NULL, &sampler)))
                {
                    ERR("Failed to create sampler.\n");
                    sampler = NULL;
                }
                else
                {
                    if (wine_rb_put(&device->samplers, &desc, &sampler->entry) == -1)
                        ERR("Failed to insert sampler.\n");
                }
            }

            if (sampler)
            {
3647
                GL_EXTCALL(glBindSampler(mapped_stage, sampler->name));
3648 3649 3650
                checkGLcall("glBindSampler");
            }
        }
3651 3652 3653 3654 3655 3656 3657 3658

        if (texture->flags & WINED3D_TEXTURE_COND_NP2)
            base_level = 0;
        else if (desc.mip_filter == WINED3D_TEXF_NONE)
            base_level = texture->lod;
        else
            base_level = min(max(sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL],
                    texture->lod), texture->level_count - 1);
3659

3660 3661
        gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
        if (base_level != gl_tex->base_level)
3662
        {
3663 3664
            /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
             * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
3665
             * mipmap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
3666 3667 3668
             * corresponds to GL_TEXTURE_BASE_LEVEL. */
            gl_info->gl_ops.gl.p_glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, base_level);
            gl_tex->base_level = base_level;
3669 3670
        }

3671
        /* Trigger shader constant reloading (for NP2 texcoord fixup) */
3672
        if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
3673
            context->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
3674
    }
3675
    else
3676
    {
3677
        context_bind_texture(context, GL_NONE, 0);
3678 3679 3680 3681 3682
        if (gl_info->supported[ARB_SAMPLER_OBJECTS])
        {
            GL_EXTCALL(glBindSampler(mapped_stage, 0));
            checkGLcall("glBindSampler");
        }
3683 3684 3685
    }
}

3686
void apply_pixelshader(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3687
{
3688
    unsigned int i;
3689

3690
    if (use_ps(state))
3691 3692 3693 3694
    {
        if (!context->last_was_pshader)
        {
            /* Former draw without a pixel shader, some samplers may be
3695
             * disabled because of WINED3D_TSS_COLOR_OP = WINED3DTOP_DISABLE
3696 3697 3698 3699 3700
             * make sure to enable them. */
            for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
            {
                if (!isStateDirty(context, STATE_SAMPLER(i)))
                    sampler(context, state, STATE_SAMPLER(i));
3701
            }
3702
            context->last_was_pshader = TRUE;
3703
        }
3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
        else
        {
            /* Otherwise all samplers were activated by the code above in
             * earlier draws, or by sampler() if a different texture was
             * bound. I don't have to do anything. */
        }
    }
    else
    {
        /* Disabled the pixel shader - color ops weren't applied while it was
         * enabled, so re-apply them. */
3715
        for (i = 0; i < context->d3d_info->limits.ffp_blend_stages; ++i)
3716
        {
3717 3718
            if (!isStateDirty(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP)))
                context_apply_state(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3719
        }
3720
        context->last_was_pshader = FALSE;
3721
    }
3722

3723
    context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
3724 3725
}

3726
static void state_geometry_shader(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3727
{
3728
    context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
3729 3730
}

3731
static void shader_bumpenv(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3732
{
3733
    context->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
3734
}
3735

3736
static void transform_world(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3737
{
3738
    const struct wined3d_gl_info *gl_info = context->gl_info;
3739
    struct wined3d_matrix mat;
3740

3741 3742 3743 3744
    /* This function is called by transform_view below if the view matrix was changed too
     *
     * Deliberately no check if the vertex declaration is dirty because the vdecl state
     * does not always update the world matrix, only on a switch between transformed
3745
     * and untransformed draws. It *may* happen that the world matrix is set 2 times during one
3746
     * draw, but that should be rather rare and cheaper in total.
3747
     */
3748
    gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3749 3750
    checkGLcall("glMatrixMode");

3751
    get_modelview_matrix(context, state, 0, &mat);
3752 3753 3754

    gl_info->gl_ops.gl.p_glLoadMatrixf((GLfloat *)&mat);
    checkGLcall("glLoadMatrixf");
3755 3756
}

3757
void clipplane(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3758
{
3759
    const struct wined3d_gl_info *gl_info = context->gl_info;
3760
    UINT index = state_id - STATE_CLIPPLANE(0);
3761
    GLdouble plane[4];
3762

3763
    if (isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_VIEW)) || index >= gl_info->limits.clipplanes)
3764 3765
        return;

3766 3767
    gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
    gl_info->gl_ops.gl.p_glPushMatrix();
3768

3769
    /* Clip Plane settings are affected by the model view in OpenGL, the View transform in direct3d */
3770
    if (!use_vs(state))
3771
        gl_info->gl_ops.gl.p_glLoadMatrixf(&state->transforms[WINED3D_TS_VIEW]._11);
3772
    else
3773
        /* With vertex shaders, clip planes are not transformed in Direct3D,
3774
         * while in OpenGL they are still transformed by the model view matrix. */
3775
        gl_info->gl_ops.gl.p_glLoadIdentity();
3776

3777 3778 3779 3780 3781
    plane[0] = state->clip_planes[index].x;
    plane[1] = state->clip_planes[index].y;
    plane[2] = state->clip_planes[index].z;
    plane[3] = state->clip_planes[index].w;

3782
    TRACE("Clipplane [%.8e, %.8e, %.8e, %.8e]\n",
3783
            plane[0], plane[1], plane[2], plane[3]);
3784
    gl_info->gl_ops.gl.p_glClipPlane(GL_CLIP_PLANE0 + index, plane);
3785 3786
    checkGLcall("glClipPlane");

3787
    gl_info->gl_ops.gl.p_glPopMatrix();
3788 3789
}

3790
static void transform_worldex(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3791
{
3792
    UINT matrix = state_id - STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0));
3793
    const struct wined3d_gl_info *gl_info = context->gl_info;
3794
    GLenum glMat;
3795

3796 3797
    TRACE("Setting world matrix %d\n", matrix);

3798
    if (matrix >= gl_info->limits.blends)
3799
    {
3800 3801 3802 3803
        WARN("Unsupported blend matrix set\n");
        return;
    }

3804 3805 3806
    if (isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_VIEW)))
        return;

3807
    /* GL_MODELVIEW0_ARB:  0x1700
Tobias Jakobi's avatar
Tobias Jakobi committed
3808
     * GL_MODELVIEW1_ARB:  0x850a
3809 3810 3811
     * GL_MODELVIEW2_ARB:  0x8722
     * GL_MODELVIEW3_ARB:  0x8723
     * etc
3812
     * GL_MODELVIEW31_ARB: 0x873f
3813 3814 3815 3816
     */
    if(matrix == 1) glMat = GL_MODELVIEW1_ARB;
    else glMat = GL_MODELVIEW2_ARB - 2 + matrix;

3817
    gl_info->gl_ops.gl.p_glMatrixMode(glMat);
3818 3819
    checkGLcall("glMatrixMode(glMat)");

3820 3821 3822
    /* World matrix 0 is multiplied with the view matrix because d3d uses 3
     * matrices while gl uses only 2. To avoid weighting the view matrix
     * incorrectly it has to be multiplied into every GL modelview matrix. */
3823
    gl_info->gl_ops.gl.p_glLoadMatrixf(&state->transforms[WINED3D_TS_VIEW]._11);
3824
    checkGLcall("glLoadMatrixf");
3825
    gl_info->gl_ops.gl.p_glMultMatrixf(&state->transforms[WINED3D_TS_WORLD_MATRIX(matrix)]._11);
3826
    checkGLcall("glMultMatrixf");
3827 3828
}

3829
static void state_vertexblend_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3830
{
3831
    enum wined3d_vertex_blend_flags f = state->render_states[WINED3D_RS_VERTEXBLEND];
3832
    static unsigned int once;
3833

3834 3835
    if (f == WINED3D_VBF_DISABLE)
        return;
3836

3837 3838
    if (!once++) FIXME("Vertex blend flags %#x not supported.\n", f);
    else WARN("Vertex blend flags %#x not supported.\n", f);
3839 3840
}

3841
static void state_vertexblend(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3842
{
3843
    enum wined3d_vertex_blend_flags val = state->render_states[WINED3D_RS_VERTEXBLEND];
3844
    struct wined3d_device *device = context->swapchain->device;
3845
    const struct wined3d_gl_info *gl_info = context->gl_info;
3846
    static unsigned int once;
3847

3848 3849 3850 3851 3852
    switch (val)
    {
        case WINED3D_VBF_1WEIGHTS:
        case WINED3D_VBF_2WEIGHTS:
        case WINED3D_VBF_3WEIGHTS:
3853
            gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_BLEND_ARB);
3854
            checkGLcall("glEnable(GL_VERTEX_BLEND_ARB)");
3855

3856 3857 3858
            /* D3D adds one more matrix which has weight (1 - sum(weights)).
             * This is enabled at context creation with enabling
             * GL_WEIGHT_SUM_UNITY_ARB. */
3859
            GL_EXTCALL(glVertexBlendARB(state->render_states[WINED3D_RS_VERTEXBLEND] + 1));
3860

3861
            if (!device->vertexBlendUsed)
3862
            {
3863
                unsigned int i;
3864
                for (i = 1; i < gl_info->limits.blends; ++i)
3865
                {
3866 3867
                    if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(i))))
                        transform_worldex(context, state, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(i)));
3868
                }
3869
                device->vertexBlendUsed = TRUE;
3870 3871 3872
            }
            break;

3873 3874
        case WINED3D_VBF_TWEENING:
        case WINED3D_VBF_0WEIGHTS: /* Indexed vertex blending, not supported. */
3875 3876 3877
            if (!once++) FIXME("Vertex blend flags %#x not supported.\n", val);
            else WARN("Vertex blend flags %#x not supported.\n", val);
            /* Fall through. */
3878
        case WINED3D_VBF_DISABLE:
3879
            gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_BLEND_ARB);
3880
            checkGLcall("glDisable(GL_VERTEX_BLEND_ARB)");
3881 3882 3883 3884
            break;
    }
}

3885
static void transform_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3886
{
3887
    const struct wined3d_gl_info *gl_info = context->gl_info;
3888
    const struct wined3d_light_info *light = NULL;
3889 3890 3891 3892 3893 3894 3895 3896
    unsigned int k;

    /* If we are changing the View matrix, reset the light and clipping planes to the new view
     * NOTE: We have to reset the positions even if the light/plane is not currently
     *       enabled, since the call to enable it will not reset the position.
     * NOTE2: Apparently texture transforms do NOT need reapplying
     */

3897
    gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3898
    checkGLcall("glMatrixMode(GL_MODELVIEW)");
3899
    gl_info->gl_ops.gl.p_glLoadMatrixf(&state->transforms[WINED3D_TS_VIEW]._11);
3900 3901 3902
    checkGLcall("glLoadMatrixf(...)");

    /* Reset lights. TODO: Call light apply func */
3903
    for (k = 0; k < gl_info->limits.lights; ++k)
3904
    {
3905 3906
        if (!(light = state->lights[k]))
            continue;
3907
        if (light->OriginalParms.type == WINED3D_LIGHT_DIRECTIONAL)
3908
            gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, &light->direction.x);
3909
        else
3910
            gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, &light->position.x);
3911
        checkGLcall("glLightfv posn");
3912
        gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_SPOT_DIRECTION, &light->direction.x);
3913 3914 3915
        checkGLcall("glLightfv dirn");
    }

3916
    /* Reset Clipping Planes  */
3917
    for (k = 0; k < gl_info->limits.clipplanes; ++k)
3918
    {
3919 3920
        if (!isStateDirty(context, STATE_CLIPPLANE(k)))
            clipplane(context, state, STATE_CLIPPLANE(k));
3921
    }
3922

3923 3924 3925
    if (context->last_was_rhw)
    {
        gl_info->gl_ops.gl.p_glLoadIdentity();
3926 3927 3928 3929 3930 3931
        checkGLcall("glLoadIdentity()");
        /* No need to update the world matrix, the identity is fine */
        return;
    }

    /* Call the world matrix state, this will apply the combined WORLD + VIEW matrix
3932
     * No need to do it here if the state is scheduled for update. */
3933 3934
    if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0))))
        transform_world(context, state, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3935

3936
    /* Avoid looping over a number of matrices if the app never used the functionality */
3937
    if (context->swapchain->device->vertexBlendUsed)
3938
    {
3939
        for (k = 1; k < gl_info->limits.blends; ++k)
3940
        {
3941 3942
            if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(k))))
                transform_worldex(context, state, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(k)));
3943 3944
        }
    }
3945 3946
}

3947
static void transform_projection(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3948
{
3949
    const struct wined3d_gl_info *gl_info = context->gl_info;
3950
    struct wined3d_matrix projection;
3951 3952

    gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3953
    checkGLcall("glMatrixMode(GL_PROJECTION)");
3954

3955 3956 3957
    get_projection_matrix(context, state, &projection);
    gl_info->gl_ops.gl.p_glLoadMatrixf(&projection._11);
    checkGLcall("glLoadMatrixf");
3958 3959
}

3960 3961 3962
/* This should match any arrays loaded in load_vertex_data.
 * TODO: Only load / unload arrays if we have to. */
static void unload_vertex_data(const struct wined3d_gl_info *gl_info)
3963
{
3964 3965 3966
    gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
    gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
    gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
3967
    if (gl_info->supported[EXT_SECONDARY_COLOR])
3968
        gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
3969
    if (gl_info->supported[ARB_VERTEX_BLEND])
3970
        gl_info->gl_ops.gl.p_glDisableClientState(GL_WEIGHT_ARRAY_ARB);
3971
    unload_tex_coords(gl_info);
3972 3973
}

3974
static inline void unload_numbered_array(struct wined3d_context *context, int i)
3975
{
3976 3977
    const struct wined3d_gl_info *gl_info = context->gl_info;

3978 3979
    GL_EXTCALL(glDisableVertexAttribArray(i));
    checkGLcall("glDisableVertexAttribArray");
3980
    if (gl_info->supported[ARB_INSTANCED_ARRAYS])
3981
        GL_EXTCALL(glVertexAttribDivisor(i, 0));
3982

3983
    context->numbered_array_mask &= ~(1u << i);
3984 3985
}

3986
/* This should match any arrays loaded in loadNumberedArrays
3987 3988
 * TODO: Only load / unload arrays if we have to. */
static void unload_numbered_arrays(struct wined3d_context *context)
3989
{
3990 3991 3992
    /* disable any attribs (this is the same for both GLSL and ARB modes) */
    int i;

3993
    for (i = 0; i < context->gl_info->limits.vertex_attribs; ++i) {
3994
        unload_numbered_array(context, i);
3995 3996 3997
    }
}

3998 3999
static void load_numbered_arrays(struct wined3d_context *context,
        const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
4000
{
4001
    const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
4002 4003
    const struct wined3d_gl_info *gl_info = context->gl_info;
    GLuint curVBO = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0U : 0;
4004
    unsigned int i;
4005

4006
    /* Default to no instancing */
4007
    context->instance_count = 0;
4008

4009
    for (i = 0; i < MAX_ATTRIBS; ++i)
4010
    {
4011
        const struct wined3d_stream_info_element *element = &stream_info->elements[i];
4012 4013
        const struct wined3d_stream_state *stream;

4014
        if (!(stream_info->use_map & (1u << i)))
4015
        {
4016
            if (context->numbered_array_mask & (1u << i))
4017
                unload_numbered_array(context, i);
4018 4019 4020
            if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
                GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
            else
4021
                GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
4022
            continue;
4023
        }
4024

4025
        stream = &state->streams[element->stream_idx];
4026

4027 4028
        if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
            context->instance_count = state->streams[0].frequency ? state->streams[0].frequency : 1;
4029

4030 4031
        if (gl_info->supported[ARB_INSTANCED_ARRAYS])
        {
4032
            GL_EXTCALL(glVertexAttribDivisor(i, element->divisor));
4033
        }
4034
        else if (element->divisor)
4035
        {
4036 4037
            /* Unload instanced arrays, they will be loaded using
             * immediate mode instead. */
4038
            if (context->numbered_array_mask & (1u << i))
4039 4040
                unload_numbered_array(context, i);
            continue;
4041 4042
        }

4043
        TRACE_(d3d_shader)("Loading array %u [VBO=%u].\n", i, element->data.buffer_object);
4044

4045
        if (element->stride)
4046
        {
4047
            if (curVBO != element->data.buffer_object)
4048
            {
4049
                GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, element->data.buffer_object));
4050
                checkGLcall("glBindBuffer");
4051
                curVBO = element->data.buffer_object;
4052
            }
4053 4054 4055 4056
            /* Use the VBO to find out if a vertex buffer exists, not the vb
             * pointer. vb can point to a user pointer data blob. In that case
             * curVBO will be 0. If there is a vertex buffer but no vbo we
             * won't be load converted attributes anyway. */
4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068
            if (vs && vs->reg_maps.shader_version.major >= 4
                    && (element->format->flags[WINED3D_GL_RES_TYPE_BUFFER] & WINED3DFMT_FLAG_INTEGER))
            {
                GL_EXTCALL(glVertexAttribIPointer(i, element->format->gl_vtx_format, element->format->gl_vtx_type,
                        element->stride, element->data.addr + state->load_base_vertex_index * element->stride));
            }
            else
            {
                GL_EXTCALL(glVertexAttribPointer(i, element->format->gl_vtx_format, element->format->gl_vtx_type,
                        element->format->gl_normalized, element->stride,
                        element->data.addr + state->load_base_vertex_index * element->stride));
            }
4069

4070
            if (!(context->numbered_array_mask & (1u << i)))
4071
            {
4072
                GL_EXTCALL(glEnableVertexAttribArray(i));
4073
                context->numbered_array_mask |= (1u << i);
4074
            }
4075 4076 4077 4078
        }
        else
        {
            /* Stride = 0 means always the same values.
4079
             * glVertexAttribPointer doesn't do that. Instead disable the
4080 4081
             * pointer and set up the attribute statically. But we have to
             * figure out the system memory address. */
4082 4083
            const BYTE *ptr = element->data.addr;
            if (element->data.buffer_object)
4084
                ptr += (ULONG_PTR)buffer_get_sysmem(stream->buffer, context);
4085

4086 4087
            if (context->numbered_array_mask & (1u << i))
                unload_numbered_array(context, i);
4088

4089
            switch (element->format->id)
4090
            {
4091
                case WINED3DFMT_R32_FLOAT:
4092
                    GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
4093
                    break;
4094
                case WINED3DFMT_R32G32_FLOAT:
4095
                    GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
4096
                    break;
4097
                case WINED3DFMT_R32G32B32_FLOAT:
4098
                    GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
4099
                    break;
4100
                case WINED3DFMT_R32G32B32A32_FLOAT:
4101
                    GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
4102 4103
                    break;

4104
                case WINED3DFMT_R8G8B8A8_UINT:
4105
                    GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
4106
                    break;
4107
                case WINED3DFMT_B8G8R8A8_UNORM:
4108
                    if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
4109 4110
                    {
                        const DWORD *src = (const DWORD *)ptr;
4111 4112 4113
                        DWORD c = *src & 0xff00ff00u;
                        c |= (*src & 0xff0000u) >> 16;
                        c |= (*src & 0xffu) << 16;
4114
                        GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
4115 4116 4117
                        break;
                    }
                    /* else fallthrough */
4118
                case WINED3DFMT_R8G8B8A8_UNORM:
4119
                    GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
4120 4121
                    break;

4122
                case WINED3DFMT_R16G16_SINT:
4123
                    GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
4124
                    break;
4125
                case WINED3DFMT_R16G16B16A16_SINT:
4126
                    GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
4127 4128
                    break;

4129
                case WINED3DFMT_R16G16_SNORM:
4130
                {
4131
                    const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
4132
                    GL_EXTCALL(glVertexAttrib4Nsv(i, s));
4133 4134
                    break;
                }
4135
                case WINED3DFMT_R16G16_UNORM:
4136
                {
4137
                    const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
4138
                    GL_EXTCALL(glVertexAttrib4Nusv(i, s));
4139 4140
                    break;
                }
4141
                case WINED3DFMT_R16G16B16A16_SNORM:
4142
                    GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
4143
                    break;
4144
                case WINED3DFMT_R16G16B16A16_UNORM:
4145
                    GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
4146 4147
                    break;

4148
                case WINED3DFMT_R10G10B10X2_UINT:
4149
                    FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
4150
                    /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
4151
                    break;
4152
                case WINED3DFMT_R10G10B10X2_SNORM:
4153
                    FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
4154
                    /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
4155 4156
                    break;

4157
                case WINED3DFMT_R16G16_FLOAT:
4158 4159 4160 4161 4162 4163 4164 4165 4166
                    if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
                    {
                        /* Not supported by GL_ARB_half_float_vertex. */
                        GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
                    }
                    else
                    {
                        float x = float_16_to_32(((const unsigned short *)ptr) + 0);
                        float y = float_16_to_32(((const unsigned short *)ptr) + 1);
4167
                        GL_EXTCALL(glVertexAttrib2f(i, x, y));
4168
                    }
4169
                    break;
4170
                case WINED3DFMT_R16G16B16A16_FLOAT:
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181
                    if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
                    {
                        /* Not supported by GL_ARB_half_float_vertex. */
                        GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
                    }
                    else
                    {
                        float x = float_16_to_32(((const unsigned short *)ptr) + 0);
                        float y = float_16_to_32(((const unsigned short *)ptr) + 1);
                        float z = float_16_to_32(((const unsigned short *)ptr) + 2);
                        float w = float_16_to_32(((const unsigned short *)ptr) + 3);
4182
                        GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
4183
                    }
4184 4185 4186
                    break;

                default:
4187
                    ERR("Unexpected declaration in stride 0 attributes.\n");
4188 4189 4190
                    break;

            }
4191
        }
4192
    }
4193
    checkGLcall("Loading numbered arrays");
4194 4195
}

4196
static void load_vertex_data(struct wined3d_context *context,
4197
        const struct wined3d_stream_info *si, const struct wined3d_state *state)
4198
{
4199 4200
    const struct wined3d_gl_info *gl_info = context->gl_info;
    GLuint curVBO = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0U : 0;
4201
    const struct wined3d_stream_info_element *e;
4202 4203

    TRACE("Using fast vertex array code\n");
4204 4205

    /* This is fixed function pipeline only, and the fixed function pipeline doesn't do instancing */
4206
    context->instance_count = 0;
4207

4208
    /* Blend Data ---------------------------------------------- */
4209 4210
    if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
            || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
4211
    {
4212 4213
        e = &si->elements[WINED3D_FFP_BLENDWEIGHT];

4214
        if (gl_info->supported[ARB_VERTEX_BLEND])
4215
        {
4216
            TRACE("Blend %u %p %u\n", e->format->component_count,
4217
                    e->data.addr + state->load_base_vertex_index * e->stride, e->stride);
4218

4219
            gl_info->gl_ops.gl.p_glEnableClientState(GL_WEIGHT_ARRAY_ARB);
4220
            checkGLcall("glEnableClientState(GL_WEIGHT_ARRAY_ARB)");
4221

4222
            GL_EXTCALL(glVertexBlendARB(e->format->component_count + 1));
4223

4224
            if (curVBO != e->data.buffer_object)
4225
            {
4226 4227
                GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
                checkGLcall("glBindBuffer");
4228
                curVBO = e->data.buffer_object;
4229 4230
            }

4231
            TRACE("glWeightPointerARB(%#x, %#x, %#x, %p);\n",
4232 4233
                    e->format->gl_vtx_format,
                    e->format->gl_vtx_type,
4234
                    e->stride,
4235
                    e->data.addr + state->load_base_vertex_index * e->stride);
4236
            GL_EXTCALL(glWeightPointerARB(e->format->gl_vtx_format, e->format->gl_vtx_type, e->stride,
4237
                    e->data.addr + state->load_base_vertex_index * e->stride));
4238 4239 4240

            checkGLcall("glWeightPointerARB");

4241
            if (si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
4242
            {
4243 4244 4245
                static BOOL warned;
                if (!warned)
                {
4246
                    FIXME("blendMatrixIndices support\n");
4247
                    warned = TRUE;
4248 4249
                }
            }
4250 4251 4252 4253 4254 4255 4256
        }
        else
        {
            /* TODO: Support vertex blending in immediate mode draws. No need
             * to write a FIXME here, this is done after the general vertex
             * declaration decoding. */
            WARN("Vertex blending not supported.\n");
4257
        }
4258 4259 4260
    }
    else
    {
4261
        if (gl_info->supported[ARB_VERTEX_BLEND])
4262
        {
4263 4264
            static const GLbyte one = 1;
            GL_EXTCALL(glWeightbvARB(1, &one));
4265
            checkGLcall("glWeightbvARB(gl_info->max_blends, weights)");
4266 4267 4268 4269
        }
    }

    /* Point Size ----------------------------------------------*/
4270
    if (si->use_map & (1u << WINED3D_FFP_PSIZE))
4271
    {
4272 4273 4274 4275 4276 4277
        /* no such functionality in the fixed function GL pipeline */
        TRACE("Cannot change ptSize here in openGl\n");
        /* TODO: Implement this function in using shaders if they are available */
    }

    /* Vertex Pointers -----------------------------------------*/
4278
    if (si->use_map & (1u << WINED3D_FFP_POSITION))
4279
    {
4280
        e = &si->elements[WINED3D_FFP_POSITION];
4281

4282
        if (curVBO != e->data.buffer_object)
4283
        {
4284 4285
            GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
            checkGLcall("glBindBuffer");
4286
            curVBO = e->data.buffer_object;
4287 4288
        }

4289 4290 4291
        TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n",
                e->format->gl_vtx_format, e->format->gl_vtx_type, e->stride,
                e->data.addr + state->load_base_vertex_index * e->stride);
4292
        gl_info->gl_ops.gl.p_glVertexPointer(e->format->gl_vtx_format, e->format->gl_vtx_type, e->stride,
4293
                e->data.addr + state->load_base_vertex_index * e->stride);
4294
        checkGLcall("glVertexPointer(...)");
4295
        gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
4296 4297 4298 4299
        checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
    }

    /* Normals -------------------------------------------------*/
4300
    if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4301
    {
4302
        e = &si->elements[WINED3D_FFP_NORMAL];
4303

4304
        if (curVBO != e->data.buffer_object)
4305
        {
4306 4307
            GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
            checkGLcall("glBindBuffer");
4308
            curVBO = e->data.buffer_object;
4309
        }
4310

4311
        TRACE("glNormalPointer(%#x, %#x, %p);\n", e->format->gl_vtx_type, e->stride,
4312
                e->data.addr + state->load_base_vertex_index * e->stride);
4313
        gl_info->gl_ops.gl.p_glNormalPointer(e->format->gl_vtx_type, e->stride,
4314
                e->data.addr + state->load_base_vertex_index * e->stride);
4315
        checkGLcall("glNormalPointer(...)");
4316
        gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
4317 4318
        checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");

4319 4320 4321 4322
    }
    else
    {
        gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
4323
        checkGLcall("glNormal3f(0, 0, 0)");
4324 4325 4326
    }

    /* Diffuse Colour --------------------------------------------*/
4327
    if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4328
    {
4329
        e = &si->elements[WINED3D_FFP_DIFFUSE];
4330

4331
        if (curVBO != e->data.buffer_object)
4332
        {
4333 4334
            GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
            checkGLcall("glBindBuffer");
4335
            curVBO = e->data.buffer_object;
4336
        }
4337

4338
        TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
4339
                e->format->gl_vtx_format, e->format->gl_vtx_type, e->stride,
4340
                e->data.addr + state->load_base_vertex_index * e->stride);
4341
        gl_info->gl_ops.gl.p_glColorPointer(e->format->gl_vtx_format, e->format->gl_vtx_type, e->stride,
4342
                e->data.addr + state->load_base_vertex_index * e->stride);
4343
        checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
4344
        gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
4345 4346
        checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");

4347 4348 4349 4350
    }
    else
    {
        gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4351 4352 4353 4354
        checkGLcall("glColor4f(1, 1, 1, 1)");
    }

    /* Specular Colour ------------------------------------------*/
4355
    if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4356
    {
4357
        TRACE("setting specular colour\n");
4358

4359
        e = &si->elements[WINED3D_FFP_SPECULAR];
4360

4361
        if (gl_info->supported[EXT_SECONDARY_COLOR])
4362
        {
4363 4364
            GLenum type = e->format->gl_vtx_type;
            GLint format = e->format->gl_vtx_format;
4365

4366
            if (curVBO != e->data.buffer_object)
4367
            {
4368 4369
                GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
                checkGLcall("glBindBuffer");
4370
                curVBO = e->data.buffer_object;
4371
            }
4372

4373
            if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
4374 4375 4376 4377 4378 4379
            {
                /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
                 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
                 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
                 * 4 component secondary colors use it
                 */
4380
                TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride,
4381
                        e->data.addr + state->load_base_vertex_index * e->stride);
4382
                GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride,
4383
                        e->data.addr + state->load_base_vertex_index * e->stride));
4384 4385 4386 4387 4388 4389 4390
                checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
            }
            else
            {
                switch(type)
                {
                    case GL_UNSIGNED_BYTE:
4391
                        TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride,
4392
                                e->data.addr + state->load_base_vertex_index * e->stride);
4393
                        GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride,
4394
                                e->data.addr + state->load_base_vertex_index * e->stride));
4395 4396 4397 4398 4399 4400
                        checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
                        break;

                    default:
                        FIXME("Add 4 component specular color pointers for type %x\n", type);
                        /* Make sure that the right color component is dropped */
4401
                        TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride,
4402
                                e->data.addr + state->load_base_vertex_index * e->stride);
4403
                        GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride,
4404
                                e->data.addr + state->load_base_vertex_index * e->stride));
4405 4406 4407
                        checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
                }
            }
4408
            gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
4409
            checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
4410 4411 4412 4413
        }
        else
        {
            WARN("Specular colour is not supported in this GL implementation.\n");
4414
        }
4415 4416 4417
    }
    else
    {
4418
        if (gl_info->supported[EXT_SECONDARY_COLOR])
4419
        {
4420 4421
            GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
            checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
4422 4423 4424 4425
        }
        else
        {
            WARN("Specular colour is not supported in this GL implementation.\n");
4426 4427 4428 4429
        }
    }

    /* Texture coords -------------------------------------------*/
4430
    load_tex_coords(context, si, &curVBO, state);
4431 4432
}

4433
static void streamsrc(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4434
{
4435 4436 4437 4438
    BOOL load_numbered = context->d3d_info->ffp_generic_attributes
            || (use_vs(state) && !context->use_immediate_mode_draw);
    BOOL load_named = !context->d3d_info->ffp_generic_attributes
            && !use_vs(state) && !context->use_immediate_mode_draw;
4439

4440
    if (isStateDirty(context, STATE_VDECL)) return;
4441 4442
    if (context->numberedArraysLoaded && !load_numbered)
    {
4443
        unload_numbered_arrays(context);
4444 4445 4446 4447 4448
        context->numberedArraysLoaded = FALSE;
        context->numbered_array_mask = 0;
    }
    else if (context->namedArraysLoaded)
    {
4449
        unload_vertex_data(context->gl_info);
4450 4451 4452 4453 4454 4455
        context->namedArraysLoaded = FALSE;
    }

    if (load_numbered)
    {
        TRACE("Loading numbered arrays\n");
4456
        load_numbered_arrays(context, &context->stream_info, state);
4457 4458 4459 4460 4461
        context->numberedArraysLoaded = TRUE;
    }
    else if (load_named)
    {
        TRACE("Loading vertex data\n");
4462
        load_vertex_data(context, &context->stream_info, state);
4463 4464
        context->namedArraysLoaded = TRUE;
    }
4465 4466
}

4467
static void vdecl_miscpart(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4468
{
4469 4470 4471
    if (isStateDirty(context, STATE_STREAMSRC))
        return;
    streamsrc(context, state, STATE_STREAMSRC);
4472 4473
}

4474
static void vertexdeclaration(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4475
{
4476
    const struct wined3d_gl_info *gl_info = context->gl_info;
4477
    BOOL useVertexShaderFunction = use_vs(state);
4478
    BOOL updateFog = FALSE;
4479
    BOOL transformed;
4480
    BOOL wasrhw = context->last_was_rhw;
4481
    unsigned int i;
4482

4483
    transformed = context->stream_info.position_transformed;
4484
    if (transformed != context->last_was_rhw && !useVertexShaderFunction)
4485 4486
        updateFog = TRUE;

4487
    context->last_was_rhw = transformed;
4488

4489 4490 4491 4492 4493
    if (context->stream_info.swizzle_map != context->last_swizzle_map)
        context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;

    context->last_swizzle_map = context->stream_info.swizzle_map;

4494 4495 4496 4497 4498 4499 4500 4501
    /* Don't have to apply the matrices when vertex shaders are used. When
     * vshaders are turned off this function will be called again anyway to
     * make sure they're properly set. */
    if (!useVertexShaderFunction)
    {
        /* TODO: Move this mainly to the viewport state and only apply when
         * the vp has changed or transformed / untransformed was switched. */
        if (wasrhw != context->last_was_rhw
4502
                && !isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION))
4503
                && !isStateDirty(context, STATE_VIEWPORT))
4504
            transform_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515
        /* World matrix needs reapplication here only if we're switching between rhw and non-rhw
         * mode.
         *
         * If a vertex shader is used, the world matrix changed and then vertex shader unbound
         * this check will fail and the matrix not applied again. This is OK because a simple
         * world matrix change reapplies the matrix - These checks here are only to satisfy the
         * needs of the vertex declaration.
         *
         * World and view matrix go into the same gl matrix, so only apply them when neither is
         * dirty
         */
4516 4517 4518
        if (transformed != wasrhw && !isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)))
                && !isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_VIEW)))
            transform_world(context, state, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
4519
        if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_COLORVERTEX)))
4520
            context_apply_state(context, state, STATE_RENDER(WINED3D_RS_COLORVERTEX));
4521 4522
        if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_LIGHTING)))
            state_lighting(context, state, STATE_RENDER(WINED3D_RS_LIGHTING));
4523 4524 4525 4526

        if (context->last_was_vshader)
        {
            updateFog = TRUE;
4527

4528 4529 4530
            if (!context->d3d_info->vs_clipping
                    && !isStateDirty(context, STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE)))
            {
4531
                state_clipping(context, state, STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE));
4532
            }
4533

4534
            for (i = 0; i < gl_info->limits.clipplanes; ++i)
4535
            {
4536
                clipplane(context, state, STATE_CLIPPLANE(i));
4537
            }
4538
        }
4539 4540
        if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS)))
            state_normalize(context, state, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS));
4541 4542 4543
    }
    else
    {
4544 4545
        if(!context->last_was_vshader) {
            static BOOL warned = FALSE;
4546 4547
            if (!context->d3d_info->vs_clipping)
            {
4548 4549 4550
                /* Disable all clip planes to get defined results on all drivers. See comment in the
                 * state_clipping state handler
                 */
4551
                for (i = 0; i < gl_info->limits.clipplanes; ++i)
4552
                {
4553
                    gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE0 + i);
4554 4555
                    checkGLcall("glDisable(GL_CLIP_PLANE0 + i)");
                }
4556

4557
                if (!warned && state->render_states[WINED3D_RS_CLIPPLANEENABLE])
4558
                {
4559 4560 4561
                    FIXME("Clipping not supported with vertex shaders\n");
                    warned = TRUE;
                }
4562
            }
4563 4564 4565 4566 4567 4568 4569 4570 4571
            if (wasrhw)
            {
                /* Apply the transform matrices when switching from rhw
                 * drawing to vertex shaders. Vertex shaders themselves do
                 * not need it, but the matrices are not reapplied
                 * automatically when switching back from vertex shaders to
                 * fixed function processing. So make sure we leave the fixed
                 * function vertex processing states back in a sane state
                 * before switching to shaders. */
4572 4573 4574 4575
                if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
                    transform_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
                if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0))))
                    transform_world(context, state, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
4576
            }
4577
            updateFog = TRUE;
4578 4579 4580 4581 4582

            /* Vertex shader clipping ignores the view matrix. Update all clipplanes
             * (Note: ARB shaders can read the clip planes for clipping emulation even if
             * device->vs_clipping is false.
             */
4583
            for (i = 0; i < gl_info->limits.clipplanes; ++i)
4584
            {
4585
                clipplane(context, state, STATE_CLIPPLANE(i));
4586
            }
4587
        }
4588
    }
4589

4590
    context->last_was_vshader = useVertexShaderFunction;
4591
    context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
4592

4593
    if (updateFog)
4594
        context_apply_state(context, state, STATE_RENDER(WINED3D_RS_FOGVERTEXMODE));
4595

4596 4597 4598 4599 4600 4601
    if (!useVertexShaderFunction)
    {
        unsigned int i;

        for (i = 0; i < MAX_TEXTURES; ++i)
        {
4602
            if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + i)))
4603
                transform_texture(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS));
4604
        }
4605

4606 4607
        if (use_ps(state) && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
                && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
4608
            context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
4609
    }
4610 4611
}

4612
static void viewport_miscpart(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4613
{
4614
    const struct wined3d_rendertarget_view *depth_stencil = state->fb->depth_stencil;
4615
    const struct wined3d_rendertarget_view *target = state->fb->render_targets[0];
4616
    const struct wined3d_gl_info *gl_info = context->gl_info;
4617
    struct wined3d_viewport vp = state->viewport;
4618 4619 4620 4621 4622 4623 4624 4625
    unsigned int width, height;

    if (target)
    {
        if (vp.width > target->width)
            vp.width = target->width;
        if (vp.height > target->height)
            vp.height = target->height;
4626

4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638
        surface_get_drawable_size(wined3d_rendertarget_view_get_surface(target), context, &width, &height);
    }
    else if (depth_stencil)
    {
        width = depth_stencil->width;
        height = depth_stencil->height;
    }
    else
    {
        FIXME("No attachments draw calls not supported.\n");
        return;
    }
4639

4640
    gl_info->gl_ops.gl.p_glDepthRange(vp.min_z, vp.max_z);
4641
    checkGLcall("glDepthRange");
4642 4643
    /* Note: GL requires lower left, DirectX supplies upper left. This is
     * reversed when using offscreen rendering. */
4644
    if (context->render_offscreen)
4645
        gl_info->gl_ops.gl.p_glViewport(vp.x, vp.y, vp.width, vp.height);
4646
    else
4647
        gl_info->gl_ops.gl.p_glViewport(vp.x, (height - (vp.y + vp.height)), vp.width, vp.height);
4648
    checkGLcall("glViewport");
4649
}
4650

4651
static void viewport_vertexpart(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4652
{
4653 4654
    if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
        transform_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
4655 4656
    if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
            && state->render_states[WINED3D_RS_POINTSCALEENABLE])
4657
        state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
4658
    /* Update the position fixup. */
4659
    context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
4660 4661
}

4662
static void light(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4663
{
4664
    const struct wined3d_gl_info *gl_info = context->gl_info;
4665 4666
    UINT Index = state_id - STATE_ACTIVELIGHT(0);
    const struct wined3d_light_info *lightInfo = state->lights[Index];
4667

4668 4669
    if (!lightInfo)
    {
4670
        gl_info->gl_ops.gl.p_glDisable(GL_LIGHT0 + Index);
4671
        checkGLcall("glDisable(GL_LIGHT0 + Index)");
4672 4673 4674
    }
    else
    {
4675
        float quad_att;
4676
        float colRGBA[] = {0.0f, 0.0f, 0.0f, 0.0f};
4677 4678

        /* Light settings are affected by the model view in OpenGL, the View transform in direct3d*/
4679 4680
        gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
        gl_info->gl_ops.gl.p_glPushMatrix();
4681
        gl_info->gl_ops.gl.p_glLoadMatrixf(&state->transforms[WINED3D_TS_VIEW]._11);
4682 4683

        /* Diffuse: */
4684 4685 4686 4687
        colRGBA[0] = lightInfo->OriginalParms.diffuse.r;
        colRGBA[1] = lightInfo->OriginalParms.diffuse.g;
        colRGBA[2] = lightInfo->OriginalParms.diffuse.b;
        colRGBA[3] = lightInfo->OriginalParms.diffuse.a;
4688
        gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_DIFFUSE, colRGBA);
4689 4690 4691
        checkGLcall("glLightfv");

        /* Specular */
4692 4693 4694 4695
        colRGBA[0] = lightInfo->OriginalParms.specular.r;
        colRGBA[1] = lightInfo->OriginalParms.specular.g;
        colRGBA[2] = lightInfo->OriginalParms.specular.b;
        colRGBA[3] = lightInfo->OriginalParms.specular.a;
4696
        gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_SPECULAR, colRGBA);
4697 4698 4699
        checkGLcall("glLightfv");

        /* Ambient */
4700 4701 4702 4703
        colRGBA[0] = lightInfo->OriginalParms.ambient.r;
        colRGBA[1] = lightInfo->OriginalParms.ambient.g;
        colRGBA[2] = lightInfo->OriginalParms.ambient.b;
        colRGBA[3] = lightInfo->OriginalParms.ambient.a;
4704
        gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_AMBIENT, colRGBA);
4705 4706
        checkGLcall("glLightfv");

4707 4708 4709
        if ((lightInfo->OriginalParms.range * lightInfo->OriginalParms.range) >= FLT_MIN)
            quad_att = 1.4f / (lightInfo->OriginalParms.range * lightInfo->OriginalParms.range);
        else
4710
            quad_att = 0.0f; /*  0 or  MAX?  (0 seems to be ok) */
4711

4712 4713 4714 4715
        /* Do not assign attenuation values for lights that do not use them. D3D apps are free to pass any junk,
         * but gl drivers use them and may crash due to bad Attenuation values. Need for Speed most wanted sets
         * Attenuation0 to NaN and crashes in the gl lib
         */
4716

4717 4718
        switch (lightInfo->OriginalParms.type)
        {
4719
            case WINED3D_LIGHT_POINT:
4720
                /* Position */
4721
                gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_POSITION, &lightInfo->position.x);
4722
                checkGLcall("glLightfv");
4723
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_SPOT_CUTOFF, lightInfo->cutoff);
4724
                checkGLcall("glLightf");
4725 4726
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_CONSTANT_ATTENUATION,
                        lightInfo->OriginalParms.attenuation0);
4727
                checkGLcall("glLightf");
4728 4729
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_LINEAR_ATTENUATION,
                        lightInfo->OriginalParms.attenuation1);
4730
                checkGLcall("glLightf");
4731 4732
                if (quad_att < lightInfo->OriginalParms.attenuation2)
                    quad_att = lightInfo->OriginalParms.attenuation2;
4733
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_QUADRATIC_ATTENUATION, quad_att);
4734
                checkGLcall("glLightf");
4735 4736 4737
                /* FIXME: Range */
                break;

4738
            case WINED3D_LIGHT_SPOT:
4739
                /* Position */
4740
                gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_POSITION, &lightInfo->position.x);
4741 4742
                checkGLcall("glLightfv");
                /* Direction */
4743
                gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_SPOT_DIRECTION, &lightInfo->direction.x);
4744
                checkGLcall("glLightfv");
4745
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_SPOT_EXPONENT, lightInfo->exponent);
4746
                checkGLcall("glLightf");
4747
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_SPOT_CUTOFF, lightInfo->cutoff);
4748
                checkGLcall("glLightf");
4749 4750
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_CONSTANT_ATTENUATION,
                        lightInfo->OriginalParms.attenuation0);
4751
                checkGLcall("glLightf");
4752 4753
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_LINEAR_ATTENUATION,
                        lightInfo->OriginalParms.attenuation1);
4754
                checkGLcall("glLightf");
4755 4756
                if (quad_att < lightInfo->OriginalParms.attenuation2)
                    quad_att = lightInfo->OriginalParms.attenuation2;
4757
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_QUADRATIC_ATTENUATION, quad_att);
4758
                checkGLcall("glLightf");
4759 4760 4761
                /* FIXME: Range */
                break;

4762
            case WINED3D_LIGHT_DIRECTIONAL:
4763
                /* Direction */
4764
                /* Note GL uses w position of 0 for direction! */
4765
                gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + Index, GL_POSITION, &lightInfo->direction.x);
4766
                checkGLcall("glLightfv");
4767
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_SPOT_CUTOFF, lightInfo->cutoff);
4768
                checkGLcall("glLightf");
4769
                gl_info->gl_ops.gl.p_glLightf(GL_LIGHT0 + Index, GL_SPOT_EXPONENT, 0.0f);
4770 4771 4772 4773
                checkGLcall("glLightf");
                break;

            default:
4774
                FIXME("Unrecognized light type %#x.\n", lightInfo->OriginalParms.type);
4775 4776 4777
        }

        /* Restore the modelview matrix */
4778
        gl_info->gl_ops.gl.p_glPopMatrix();
4779

4780
        gl_info->gl_ops.gl.p_glEnable(GL_LIGHT0 + Index);
4781 4782 4783 4784
        checkGLcall("glEnable(GL_LIGHT0 + Index)");
    }
}

4785
static void scissorrect(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4786
{
4787
    const struct wined3d_gl_info *gl_info = context->gl_info;
4788 4789 4790 4791 4792 4793
    const RECT *r = &state->scissor_rect;

    /* Warning: glScissor uses window coordinates, not viewport coordinates,
     * so our viewport correction does not apply. Warning2: Even in windowed
     * mode the coords are relative to the window, not the screen. */
    TRACE("Setting new scissor rect to %s.\n", wine_dbgstr_rect(r));
4794

4795 4796
    if (context->render_offscreen)
    {
4797
        gl_info->gl_ops.gl.p_glScissor(r->left, r->top, r->right - r->left, r->bottom - r->top);
4798 4799 4800
    }
    else
    {
4801
        const struct wined3d_rendertarget_view *target = state->fb->render_targets[0];
4802 4803 4804
        UINT height;
        UINT width;

4805
        surface_get_drawable_size(wined3d_rendertarget_view_get_surface(target), context, &width, &height);
4806
        gl_info->gl_ops.gl.p_glScissor(r->left, height - r->bottom, r->right - r->left, r->bottom - r->top);
4807
    }
4808 4809 4810
    checkGLcall("glScissor");
}

4811
static void indexbuffer(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4812
{
4813
    const struct wined3d_stream_info *stream_info = &context->stream_info;
4814 4815
    const struct wined3d_gl_info *gl_info = context->gl_info;

4816
    if (!state->index_buffer || !stream_info->all_vbo)
4817
    {
4818
        GL_EXTCALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
4819 4820 4821
    }
    else
    {
4822
        struct wined3d_buffer *ib = state->index_buffer;
4823
        GL_EXTCALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib->buffer_object));
4824 4825 4826
    }
}

4827
static void frontface(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4828
{
4829
    const struct wined3d_gl_info *gl_info = context->gl_info;
4830
    GLenum mode;
4831

4832
    mode = state->rasterizer_state && state->rasterizer_state->desc.front_ccw ? GL_CCW : GL_CW;
4833
    if (context->render_offscreen)
4834 4835 4836 4837
        mode = (mode == GL_CW) ? GL_CCW : GL_CW;

    gl_info->gl_ops.gl.p_glFrontFace(mode);
    checkGLcall("glFrontFace");
4838 4839
}

4840
static void psorigin_w(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
{
    static BOOL warned;

    if (!warned)
    {
        WARN("Point sprite coordinate origin switching not supported.\n");
        warned = TRUE;
    }
}

4851
static void psorigin(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4852 4853 4854 4855
{
    const struct wined3d_gl_info *gl_info = context->gl_info;
    GLint origin = context->render_offscreen ? GL_LOWER_LEFT : GL_UPPER_LEFT;

4856 4857
    GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, origin));
    checkGLcall("glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, ...)");
4858 4859
}

4860 4861 4862 4863 4864 4865
void state_srgbwrite(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
{
    const struct wined3d_gl_info *gl_info = context->gl_info;

    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);

4866
    if (needs_srgb_write(context, state, state->fb))
4867 4868 4869 4870 4871
        gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
    else
        gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
}

4872
static void state_cb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
4873
{
4874 4875
    enum wined3d_shader_type shader_type = state_id - STATE_CONSTANT_BUFFER(0);
    const struct wined3d_gl_info *gl_info = context->gl_info;
4876
    struct wined3d_buffer *buffer;
4877 4878 4879
    unsigned int i, base, count;

    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
4880

4881
    wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, shader_type, &base, &count);
4882 4883
    for (i = 0; i < count; ++i)
    {
4884
        buffer = state->cb[shader_type][i];
4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896
        GL_EXTCALL(glBindBufferBase(GL_UNIFORM_BUFFER, base + i, buffer ? buffer->buffer_object : 0));
    }
    checkGLcall("glBindBufferBase");
}

static void state_cb_warn(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
{
    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);

    WARN("Constant buffers (%s) no supported.\n", debug_d3dstate(state_id));
}

4897 4898 4899 4900 4901 4902 4903 4904
static void state_shader_resource_binding(struct wined3d_context *context,
        const struct wined3d_state *state, DWORD state_id)
{
    TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);

    context->update_shader_resource_bindings = 1;
}

4905 4906
const struct StateEntryTemplate misc_state_template[] =
{
4907
    { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX),  { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX),  state_cb,           }, ARB_UNIFORM_BUFFER_OBJECT       },
4908
    { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX),  { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX),  state_cb_warn,      }, WINED3D_GL_EXT_NONE             },
4909
    { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY),{ STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY),state_cb,           }, ARB_UNIFORM_BUFFER_OBJECT       },
4910
    { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY),{ STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY),state_cb_warn,      }, WINED3D_GL_EXT_NONE             },
4911
    { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL),   { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL),   state_cb,           }, ARB_UNIFORM_BUFFER_OBJECT       },
4912
    { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL),   { STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL),   state_cb_warn,      }, WINED3D_GL_EXT_NONE             },
4913
    { STATE_SHADER_RESOURCE_BINDING,                      { STATE_SHADER_RESOURCE_BINDING,                      state_shader_resource_binding}, WINED3D_GL_EXT_NONE    },
4914 4915 4916 4917 4918 4919 4920 4921 4922 4923
    { STATE_RENDER(WINED3D_RS_SRCBLEND),                  { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DESTBLEND),                 { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          state_blend         }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_EDGEANTIALIAS),             { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ANTIALIASEDLINEENABLE),     { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SEPARATEALPHABLENDENABLE),  { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SRCBLENDALPHA),             { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DESTBLENDALPHA),            { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DESTBLENDALPHA),            { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_BLENDOPALPHA),              { STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
4924 4925
    { STATE_STREAMSRC,                                    { STATE_STREAMSRC,                                    streamsrc           }, WINED3D_GL_EXT_NONE             },
    { STATE_VDECL,                                        { STATE_VDECL,                                        vdecl_miscpart      }, WINED3D_GL_EXT_NONE             },
4926 4927
    { STATE_FRONTFACE,                                    { STATE_FRONTFACE,                                    frontface           }, WINED3D_GL_EXT_NONE             },
    { STATE_SCISSORRECT,                                  { STATE_SCISSORRECT,                                  scissorrect         }, WINED3D_GL_EXT_NONE             },
4928 4929 4930
    { STATE_POINTSPRITECOORDORIGIN,                       { STATE_POINTSPRITECOORDORIGIN,                       psorigin            }, WINED3D_GL_VERSION_2_0          },
    { STATE_POINTSPRITECOORDORIGIN,                       { STATE_POINTSPRITECOORDORIGIN,                       psorigin_w          }, WINED3D_GL_EXT_NONE             },

4931
    /* TODO: Move shader constant loading to vertex and fragment pipeline respectively, as soon as the pshader and
4932 4933
     * vshader loadings are untied from each other
     */
4934
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4935 4936 4937
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4938
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4939 4940 4941
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4942
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4943 4944 4945
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4946
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4947 4948 4949
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4950
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4951 4952 4953
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4954
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4955 4956 4957
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4958
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4959 4960 4961
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4962
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4963 4964 4965
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01),   { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10),   { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11),   { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00),   NULL                }, WINED3D_GL_EXT_NONE             },
4966
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4967
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4968
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4969
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4970
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4971
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4972
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4973
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4974
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4975
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4976
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4977
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4978
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4979
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4980
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE),  { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE),  shader_bumpenv      }, WINED3D_GL_EXT_NONE             },
4981
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE),  NULL                }, WINED3D_GL_EXT_NONE             },
4982 4983

    { STATE_VIEWPORT,                                     { STATE_VIEWPORT,                                     viewport_miscpart   }, WINED3D_GL_EXT_NONE             },
4984
    { STATE_INDEXBUFFER,                                  { STATE_INDEXBUFFER,                                  indexbuffer         }, ARB_VERTEX_BUFFER_OBJECT        },
4985
    { STATE_INDEXBUFFER,                                  { STATE_INDEXBUFFER,                                  state_nop           }, WINED3D_GL_EXT_NONE             },
4986
    { STATE_RENDER(WINED3D_RS_ANTIALIAS),                 { STATE_RENDER(WINED3D_RS_ANTIALIAS),                 state_antialias     }, WINED3D_GL_EXT_NONE             },
4987
    { STATE_RENDER(WINED3D_RS_TEXTUREPERSPECTIVE),        { STATE_RENDER(WINED3D_RS_TEXTUREPERSPECTIVE),        state_nop           }, WINED3D_GL_EXT_NONE             },
4988 4989 4990 4991
    { STATE_RENDER(WINED3D_RS_ZENABLE),                   { STATE_RENDER(WINED3D_RS_ZENABLE),                   state_zenable       }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAPU),                     { STATE_RENDER(WINED3D_RS_WRAPU),                     state_wrapu         }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAPV),                     { STATE_RENDER(WINED3D_RS_WRAPV),                     state_wrapv         }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FILLMODE),                  { STATE_RENDER(WINED3D_RS_FILLMODE),                  state_fillmode      }, WINED3D_GL_EXT_NONE             },
4992 4993
    { STATE_RENDER(WINED3D_RS_LINEPATTERN),               { STATE_RENDER(WINED3D_RS_LINEPATTERN),               state_linepattern   }, WINED3D_GL_LEGACY_CONTEXT       },
    { STATE_RENDER(WINED3D_RS_LINEPATTERN),               { STATE_RENDER(WINED3D_RS_LINEPATTERN),               state_linepattern_w }, WINED3D_GL_EXT_NONE             },
4994 4995 4996
    { STATE_RENDER(WINED3D_RS_MONOENABLE),                { STATE_RENDER(WINED3D_RS_MONOENABLE),                state_monoenable    }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ROP2),                      { STATE_RENDER(WINED3D_RS_ROP2),                      state_rop2          }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_PLANEMASK),                 { STATE_RENDER(WINED3D_RS_PLANEMASK),                 state_planemask     }, WINED3D_GL_EXT_NONE             },
4997
    { STATE_RENDER(WINED3D_RS_ZWRITEENABLE),              { STATE_RENDER(WINED3D_RS_ZWRITEENABLE),              state_zwriteenable  }, WINED3D_GL_EXT_NONE             },
4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060
    { STATE_RENDER(WINED3D_RS_LASTPIXEL),                 { STATE_RENDER(WINED3D_RS_LASTPIXEL),                 state_lastpixel     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CULLMODE),                  { STATE_RENDER(WINED3D_RS_CULLMODE),                  state_cullmode      }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ZFUNC),                     { STATE_RENDER(WINED3D_RS_ZFUNC),                     state_zfunc         }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DITHERENABLE),              { STATE_RENDER(WINED3D_RS_DITHERENABLE),              state_ditherenable  }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SUBPIXEL),                  { STATE_RENDER(WINED3D_RS_SUBPIXEL),                  state_subpixel      }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SUBPIXELX),                 { STATE_RENDER(WINED3D_RS_SUBPIXELX),                 state_subpixelx     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STIPPLEDALPHA),             { STATE_RENDER(WINED3D_RS_STIPPLEDALPHA),             state_stippledalpha }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STIPPLEENABLE),             { STATE_RENDER(WINED3D_RS_STIPPLEENABLE),             state_stippleenable }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_MIPMAPLODBIAS),             { STATE_RENDER(WINED3D_RS_MIPMAPLODBIAS),             state_mipmaplodbias }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ANISOTROPY),                { STATE_RENDER(WINED3D_RS_ANISOTROPY),                state_anisotropy    }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FLUSHBATCH),                { STATE_RENDER(WINED3D_RS_FLUSHBATCH),                state_flushbatch    }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_TRANSLUCENTSORTINDEPENDENT),{ STATE_RENDER(WINED3D_RS_TRANSLUCENTSORTINDEPENDENT),state_translucentsi }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILENABLE),             { STATE_RENDER(WINED3D_RS_STENCILENABLE),             state_stencil       }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILFAIL),               { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILZFAIL),              { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILPASS),               { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILFUNC),               { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILREF),                { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILMASK),               { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_STENCILWRITEMASK),          { STATE_RENDER(WINED3D_RS_STENCILWRITEMASK),          state_stencilwrite2s}, EXT_STENCIL_TWO_SIDE            },
    { STATE_RENDER(WINED3D_RS_STENCILWRITEMASK),          { STATE_RENDER(WINED3D_RS_STENCILWRITEMASK),          state_stencilwrite  }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE),       { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CCW_STENCILFAIL),           { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CCW_STENCILZFAIL),          { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CCW_STENCILPASS),           { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CCW_STENCILFUNC),           { STATE_RENDER(WINED3D_RS_STENCILENABLE),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP0),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     state_wrap          }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP1),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP2),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP3),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP4),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP5),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP6),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP7),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP8),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP9),                     { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP10),                    { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP11),                    { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP12),                    { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP13),                    { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP14),                    { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_WRAP15),                    { STATE_RENDER(WINED3D_RS_WRAP0),                     NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_EXTENTS),                   { STATE_RENDER(WINED3D_RS_EXTENTS),                   state_extents       }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORKEYBLENDENABLE),       { STATE_RENDER(WINED3D_RS_COLORKEYBLENDENABLE),       state_ckeyblend     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SOFTWAREVERTEXPROCESSING),  { STATE_RENDER(WINED3D_RS_SOFTWAREVERTEXPROCESSING),  state_swvp          }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_PATCHEDGESTYLE),            { STATE_RENDER(WINED3D_RS_PATCHEDGESTYLE),            state_patchedgestyle}, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_PATCHSEGMENTS),             { STATE_RENDER(WINED3D_RS_PATCHSEGMENTS),             state_patchsegments }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POSITIONDEGREE),            { STATE_RENDER(WINED3D_RS_POSITIONDEGREE),            state_positiondegree}, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_NORMALDEGREE),              { STATE_RENDER(WINED3D_RS_NORMALDEGREE),              state_normaldegree  }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_MINTESSELLATIONLEVEL),      { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_MAXTESSELLATIONLEVEL),      { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ADAPTIVETESS_X),            { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ADAPTIVETESS_Y),            { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ADAPTIVETESS_Z),            { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ADAPTIVETESS_W),            { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),{ STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),state_nvdb          }, EXT_DEPTH_BOUNDS_TEST           },
    { STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),{ STATE_RENDER(WINED3D_RS_ENABLEADAPTIVETESSELLATION),state_tessellation  }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_MULTISAMPLEANTIALIAS),      { STATE_RENDER(WINED3D_RS_MULTISAMPLEANTIALIAS),      state_msaa          }, ARB_MULTISAMPLE                 },
    { STATE_RENDER(WINED3D_RS_MULTISAMPLEANTIALIAS),      { STATE_RENDER(WINED3D_RS_MULTISAMPLEANTIALIAS),      state_msaa_w        }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_MULTISAMPLEMASK),           { STATE_RENDER(WINED3D_RS_MULTISAMPLEMASK),           state_multisampmask }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DEBUGMONITORTOKEN),         { STATE_RENDER(WINED3D_RS_DEBUGMONITORTOKEN),         state_debug_monitor }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          state_colorwrite0   }, EXT_DRAW_BUFFERS2               },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          state_colorwrite    }, WINED3D_GL_EXT_NONE             },
5061
    { STATE_RENDER(WINED3D_RS_BLENDOP),                   { STATE_RENDER(WINED3D_RS_BLENDOP),                   state_blendop       }, WINED3D_GL_BLEND_EQUATION       },
5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074
    { STATE_RENDER(WINED3D_RS_BLENDOP),                   { STATE_RENDER(WINED3D_RS_BLENDOP),                   state_blendop_w     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE),         { STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE),         state_scissor       }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SLOPESCALEDEPTHBIAS),       { STATE_RENDER(WINED3D_RS_DEPTHBIAS),                 NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1),         { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1),         state_colorwrite1   }, EXT_DRAW_BUFFERS2               },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1),         { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2),         { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2),         state_colorwrite2   }, EXT_DRAW_BUFFERS2               },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2),         { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3),         { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3),         state_colorwrite3   }, EXT_DRAW_BUFFERS2               },
    { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3),         { STATE_RENDER(WINED3D_RS_COLORWRITEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_BLENDFACTOR),               { STATE_RENDER(WINED3D_RS_BLENDFACTOR),               state_blendfactor   }, EXT_BLEND_COLOR                 },
    { STATE_RENDER(WINED3D_RS_BLENDFACTOR),               { STATE_RENDER(WINED3D_RS_BLENDFACTOR),               state_blendfactor_w }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DEPTHBIAS),                 { STATE_RENDER(WINED3D_RS_DEPTHBIAS),                 state_depthbias     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ZVISIBLE),                  { STATE_RENDER(WINED3D_RS_ZVISIBLE),                  state_zvisible      }, WINED3D_GL_EXT_NONE             },
5075
    /* Samplers */
5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095
    { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(8),                                   { STATE_SAMPLER(8),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(9),                                   { STATE_SAMPLER(9),                                   sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(10),                                  { STATE_SAMPLER(10),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(11),                                  { STATE_SAMPLER(11),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(12),                                  { STATE_SAMPLER(12),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(13),                                  { STATE_SAMPLER(13),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(14),                                  { STATE_SAMPLER(14),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(15),                                  { STATE_SAMPLER(15),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(16), /* Vertex sampler 0 */           { STATE_SAMPLER(16),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(17), /* Vertex sampler 1 */           { STATE_SAMPLER(17),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(18), /* Vertex sampler 2 */           { STATE_SAMPLER(18),                                  sampler             }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(19), /* Vertex sampler 3 */           { STATE_SAMPLER(19),                                  sampler             }, WINED3D_GL_EXT_NONE             },
5096
    { STATE_BASEVERTEXINDEX,                              { STATE_BASEVERTEXINDEX,                              state_nop,          }, ARB_DRAW_ELEMENTS_BASE_VERTEX   },
5097
    { STATE_BASEVERTEXINDEX,                              { STATE_STREAMSRC,                                    NULL,               }, WINED3D_GL_EXT_NONE             },
5098
    { STATE_FRAMEBUFFER,                                  { STATE_FRAMEBUFFER,                                  context_state_fb    }, WINED3D_GL_EXT_NONE             },
5099 5100
    { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            context_state_drawbuf},WINED3D_GL_EXT_NONE             },
    { STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY),         { STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY),         state_geometry_shader}, WINED3D_GL_EXT_NONE             },
5101
    {0 /* Terminate */,                                   { 0,                                                  0                   }, WINED3D_GL_EXT_NONE             },
5102 5103
};

5104
static const struct StateEntryTemplate vp_ffp_states[] =
5105
{
5106
    { STATE_VDECL,                                        { STATE_VDECL,                                        vertexdeclaration   }, WINED3D_GL_EXT_NONE             },
5107
    { STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX),           { STATE_VDECL,                                        NULL                }, WINED3D_GL_EXT_NONE             },
5108 5109
    { STATE_MATERIAL,                                     { STATE_RENDER(WINED3D_RS_SPECULARENABLE),            NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SPECULARENABLE),            { STATE_RENDER(WINED3D_RS_SPECULARENABLE),            state_specularenable}, WINED3D_GL_EXT_NONE             },
5110
      /* Clip planes */
5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142
    { STATE_CLIPPLANE(0),                                 { STATE_CLIPPLANE(0),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(1),                                 { STATE_CLIPPLANE(1),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(2),                                 { STATE_CLIPPLANE(2),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(3),                                 { STATE_CLIPPLANE(3),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(4),                                 { STATE_CLIPPLANE(4),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(5),                                 { STATE_CLIPPLANE(5),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(6),                                 { STATE_CLIPPLANE(6),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(7),                                 { STATE_CLIPPLANE(7),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(8),                                 { STATE_CLIPPLANE(8),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(9),                                 { STATE_CLIPPLANE(9),                                 clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(10),                                { STATE_CLIPPLANE(10),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(11),                                { STATE_CLIPPLANE(11),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(12),                                { STATE_CLIPPLANE(12),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(13),                                { STATE_CLIPPLANE(13),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(14),                                { STATE_CLIPPLANE(14),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(15),                                { STATE_CLIPPLANE(15),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(16),                                { STATE_CLIPPLANE(16),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(17),                                { STATE_CLIPPLANE(17),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(18),                                { STATE_CLIPPLANE(18),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(19),                                { STATE_CLIPPLANE(19),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(20),                                { STATE_CLIPPLANE(20),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(21),                                { STATE_CLIPPLANE(21),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(22),                                { STATE_CLIPPLANE(22),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(23),                                { STATE_CLIPPLANE(23),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(24),                                { STATE_CLIPPLANE(24),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(25),                                { STATE_CLIPPLANE(25),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(26),                                { STATE_CLIPPLANE(26),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(27),                                { STATE_CLIPPLANE(27),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(28),                                { STATE_CLIPPLANE(28),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(29),                                { STATE_CLIPPLANE(29),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(30),                                { STATE_CLIPPLANE(30),                                clipplane           }, WINED3D_GL_EXT_NONE             },
    { STATE_CLIPPLANE(31),                                { STATE_CLIPPLANE(31),                                clipplane           }, WINED3D_GL_EXT_NONE             },
5143
      /* Lights */
5144
    { STATE_LIGHT_TYPE,                                   { STATE_LIGHT_TYPE,                                   state_nop           }, WINED3D_GL_EXT_NONE             },
5145 5146 5147 5148 5149 5150 5151 5152
    { STATE_ACTIVELIGHT(0),                               { STATE_ACTIVELIGHT(0),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(1),                               { STATE_ACTIVELIGHT(1),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(2),                               { STATE_ACTIVELIGHT(2),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(3),                               { STATE_ACTIVELIGHT(3),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(4),                               { STATE_ACTIVELIGHT(4),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(5),                               { STATE_ACTIVELIGHT(5),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(6),                               { STATE_ACTIVELIGHT(6),                               light               }, WINED3D_GL_EXT_NONE             },
    { STATE_ACTIVELIGHT(7),                               { STATE_ACTIVELIGHT(7),                               light               }, WINED3D_GL_EXT_NONE             },
5153
    /* Viewport */
5154
    { STATE_VIEWPORT,                                     { STATE_VIEWPORT,                                     viewport_vertexpart }, WINED3D_GL_EXT_NONE             },
5155
      /* Transform states follow                    */
5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421
    { STATE_TRANSFORM(WINED3D_TS_VIEW),                   { STATE_TRANSFORM(WINED3D_TS_VIEW),                   transform_view      }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_PROJECTION),             { STATE_TRANSFORM(WINED3D_TS_PROJECTION),             transform_projection}, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE0),               { STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE1),               { STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE2),               { STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE3),               { STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE4),               { STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE5),               { STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE6),               { STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_TEXTURE7),               { STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL            }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  0)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  0)),      transform_world     }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  1)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  1)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  2)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  2)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  3)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  3)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  4)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  4)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  5)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  5)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  6)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  6)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  7)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  7)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  8)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  8)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  9)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(  9)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 10)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 10)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 11)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 11)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 12)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 12)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 13)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 13)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 14)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 14)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 15)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 15)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 16)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 16)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 17)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 17)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 18)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 18)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 19)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 19)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 20)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 20)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 21)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 21)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 22)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 22)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 23)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 23)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 24)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 24)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 25)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 25)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 26)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 26)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 27)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 27)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 28)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 28)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 29)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 29)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 30)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 30)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 31)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 31)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 32)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 32)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 33)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 33)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 34)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 34)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 35)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 35)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 36)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 36)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 37)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 37)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 38)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 38)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 39)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 39)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 40)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 40)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 41)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 41)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 42)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 42)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 43)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 43)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 44)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 44)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 45)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 45)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 46)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 46)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 47)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 47)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 48)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 48)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 49)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 49)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 50)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 50)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 51)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 51)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 52)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 52)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 53)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 53)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 54)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 54)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 55)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 55)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 56)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 56)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 57)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 57)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 58)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 58)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 59)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 59)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 60)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 60)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 61)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 61)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 62)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 62)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 63)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 63)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 64)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 64)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 65)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 65)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 66)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 66)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 67)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 67)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 68)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 68)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 69)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 69)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 70)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 70)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 71)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 71)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 72)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 72)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 73)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 73)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 74)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 74)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 75)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 75)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 76)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 76)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 77)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 77)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 78)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 78)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 79)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 79)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 80)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 80)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 81)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 81)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 82)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 82)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 83)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 83)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 84)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 84)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 85)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 85)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 86)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 86)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 87)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 87)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 88)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 88)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 89)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 89)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 90)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 90)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 91)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 91)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 92)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 92)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 93)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 93)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 94)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 94)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 95)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 95)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 96)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 96)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 97)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 97)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 98)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 98)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 99)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX( 99)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(100)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(100)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(101)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(101)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(102)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(102)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(103)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(103)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(104)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(104)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(105)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(105)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(106)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(106)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(107)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(107)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(108)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(108)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(109)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(109)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(110)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(110)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(111)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(111)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(112)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(112)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(113)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(113)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(114)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(114)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(115)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(115)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(116)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(116)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(117)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(117)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(118)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(118)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(119)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(119)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(120)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(120)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(121)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(121)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(122)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(122)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(123)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(123)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(124)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(124)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(125)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(125)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(126)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(126)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(127)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(127)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(128)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(128)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(129)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(129)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(130)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(130)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(131)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(131)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(132)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(132)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(133)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(133)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(134)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(134)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(135)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(135)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(136)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(136)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(137)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(137)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(138)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(138)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(139)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(139)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(140)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(140)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(141)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(141)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(142)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(142)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(143)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(143)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(144)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(144)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(145)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(145)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(146)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(146)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(147)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(147)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(148)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(148)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(149)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(149)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(150)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(150)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(151)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(151)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(152)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(152)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(153)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(153)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(154)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(154)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(155)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(155)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(156)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(156)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(157)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(157)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(158)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(158)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(159)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(159)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(160)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(160)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(161)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(161)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(162)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(162)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(163)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(163)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(164)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(164)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(165)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(165)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(166)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(166)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(167)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(167)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(168)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(168)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(169)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(169)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(170)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(170)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(171)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(171)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(172)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(172)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(173)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(173)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(174)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(174)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(175)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(175)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(176)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(176)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(177)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(177)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(178)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(178)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(179)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(179)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(180)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(180)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(181)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(181)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(182)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(182)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(183)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(183)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(184)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(184)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(185)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(185)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(186)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(186)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(187)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(187)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(188)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(188)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(189)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(189)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(190)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(190)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(191)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(191)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(192)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(192)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(193)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(193)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(194)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(194)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(195)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(195)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(196)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(196)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(197)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(197)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(198)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(198)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(199)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(199)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(200)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(200)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(201)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(201)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(202)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(202)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(203)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(203)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(204)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(204)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(205)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(205)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(206)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(206)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(207)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(207)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(208)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(208)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(209)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(209)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(210)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(210)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(211)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(211)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(212)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(212)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(213)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(213)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(214)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(214)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(215)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(215)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(216)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(216)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(217)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(217)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(218)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(218)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(219)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(219)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(220)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(220)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(221)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(221)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(222)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(222)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(223)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(223)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(224)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(224)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(225)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(225)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(226)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(226)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(227)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(227)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(228)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(228)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(229)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(229)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(230)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(230)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(231)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(231)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(232)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(232)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(233)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(233)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(234)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(234)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(235)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(235)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(236)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(236)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(237)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(237)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(238)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(238)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(239)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(239)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(240)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(240)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(241)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(241)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(242)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(242)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(243)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(243)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(244)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(244)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(245)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(245)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(246)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(246)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(247)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(247)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(248)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(248)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(249)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(249)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(250)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(250)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(251)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(251)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(252)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(252)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(253)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(253)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(254)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(254)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
    { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(255)),      { STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(255)),      transform_worldex   }, WINED3D_GL_EXT_NONE             },
5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture   }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX),  { STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX),  tex_coordindex      }, WINED3D_GL_EXT_NONE             },
5438
      /* Fog */
5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470
    { STATE_RENDER(WINED3D_RS_FOGENABLE),                 { STATE_RENDER(WINED3D_RS_FOGENABLE),                 state_fog_vertexpart}, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGTABLEMODE),              { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGVERTEXMODE),             { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_RANGEFOGENABLE),            { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CLIPPING),                  { STATE_RENDER(WINED3D_RS_CLIPPING),                  state_clipping      }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE),           { STATE_RENDER(WINED3D_RS_CLIPPING),                  NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_LIGHTING),                  { STATE_RENDER(WINED3D_RS_LIGHTING),                  state_lighting      }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_AMBIENT),                   { STATE_RENDER(WINED3D_RS_AMBIENT),                   state_ambient       }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORVERTEX),               { STATE_RENDER(WINED3D_RS_COLORVERTEX),               state_colormat      }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_LOCALVIEWER),               { STATE_RENDER(WINED3D_RS_LOCALVIEWER),               state_localviewer   }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_NORMALIZENORMALS),          { STATE_RENDER(WINED3D_RS_NORMALIZENORMALS),          state_normalize     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE),     { STATE_RENDER(WINED3D_RS_COLORVERTEX),               NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE),    { STATE_RENDER(WINED3D_RS_COLORVERTEX),               NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE),     { STATE_RENDER(WINED3D_RS_COLORVERTEX),               NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE),    { STATE_RENDER(WINED3D_RS_COLORVERTEX),               NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_VERTEXBLEND),               { STATE_RENDER(WINED3D_RS_VERTEXBLEND),               state_vertexblend   }, ARB_VERTEX_BLEND                },
    { STATE_RENDER(WINED3D_RS_VERTEXBLEND),               { STATE_RENDER(WINED3D_RS_VERTEXBLEND),               state_vertexblend_w }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSIZE),                 { STATE_RENDER(WINED3D_RS_POINTSCALEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             state_psizemin_arb  }, ARB_POINT_PARAMETERS            },
    { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             state_psizemin_ext  }, EXT_POINT_PARAMETERS            },
    { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             state_psizemin_w    }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE),         { STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE),         state_pointsprite   }, ARB_POINT_SPRITE                },
    { STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE),         { STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE),         state_pointsprite_w }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSCALEENABLE),          { STATE_RENDER(WINED3D_RS_POINTSCALEENABLE),          state_pscale        }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSCALE_A),              { STATE_RENDER(WINED3D_RS_POINTSCALEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSCALE_B),              { STATE_RENDER(WINED3D_RS_POINTSCALEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSCALE_C),              { STATE_RENDER(WINED3D_RS_POINTSCALEENABLE),          NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_POINTSIZE_MAX),             { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             NULL                }, ARB_POINT_PARAMETERS            },
    { STATE_RENDER(WINED3D_RS_POINTSIZE_MAX),             { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             NULL                }, EXT_POINT_PARAMETERS            },
    { STATE_RENDER(WINED3D_RS_POINTSIZE_MAX),             { STATE_RENDER(WINED3D_RS_POINTSIZE_MIN),             NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_TWEENFACTOR),               { STATE_RENDER(WINED3D_RS_VERTEXBLEND),               NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE),  { STATE_RENDER(WINED3D_RS_VERTEXBLEND),               NULL                }, WINED3D_GL_EXT_NONE             },
5471

5472 5473 5474 5475 5476
    /* Samplers for NP2 texture matrix adjustions. They are not needed if GL_ARB_texture_non_power_of_two is supported,
     * so register a NULL state handler in that case to get the vertex part of sampler() skipped(VTF is handled in the misc states.
     * otherwise, register sampler_texmatrix, which takes care of updating the texture matrix
     */
    { STATE_SAMPLER(0),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5477
    { STATE_SAMPLER(0),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5478
    { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5479
    { STATE_SAMPLER(1),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5480
    { STATE_SAMPLER(1),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5481
    { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5482
    { STATE_SAMPLER(2),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5483
    { STATE_SAMPLER(2),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5484
    { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5485
    { STATE_SAMPLER(3),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5486
    { STATE_SAMPLER(3),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5487
    { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5488
    { STATE_SAMPLER(4),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5489
    { STATE_SAMPLER(4),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5490
    { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5491
    { STATE_SAMPLER(5),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5492
    { STATE_SAMPLER(5),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5493
    { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5494
    { STATE_SAMPLER(6),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5495
    { STATE_SAMPLER(6),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5496
    { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5497
    { STATE_SAMPLER(7),                                   { 0,                                                  NULL                }, ARB_TEXTURE_NON_POWER_OF_TWO    },
5498
    { STATE_SAMPLER(7),                                   { 0,                                                  NULL                }, WINED3D_GL_NORMALIZED_TEXRECT   },
5499
    { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texmatrix   }, WINED3D_GL_EXT_NONE             },
5500
    { STATE_POINT_ENABLE,                                 { STATE_POINT_ENABLE,                                 state_nop           }, WINED3D_GL_EXT_NONE             },
5501
    {0 /* Terminate */,                                   { 0,                                                  0                   }, WINED3D_GL_EXT_NONE             },
5502 5503
};

5504
static const struct StateEntryTemplate ffp_fragmentstate_template[] = {
5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),        { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),        tex_colorop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),        { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),        tex_alphaop         }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG),      { STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP),        NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT),        { 0 /* As long as we don't support D3DTA_CONSTANT */, NULL                }, WINED3D_GL_EXT_NONE             },
5585
    { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            apply_pixelshader   }, WINED3D_GL_EXT_NONE             },
5586 5587 5588 5589
    { STATE_RENDER(WINED3D_RS_ALPHAFUNC),                 { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ALPHAREF),                  { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           state_alpha_test    }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_COLORKEYENABLE),            { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE),           NULL                }, WINED3D_GL_EXT_NONE             },
5590
    { STATE_COLOR_KEY,                                    { STATE_COLOR_KEY,                                    state_nop           }, WINED3D_GL_EXT_NONE             },
5591
    { STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE),           { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),            NULL                }, WINED3D_GL_EXT_NONE             },
5592 5593 5594 5595 5596 5597 5598 5599
    { STATE_RENDER(WINED3D_RS_TEXTUREFACTOR),             { STATE_RENDER(WINED3D_RS_TEXTUREFACTOR),             state_texfactor     }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGCOLOR),                  { STATE_RENDER(WINED3D_RS_FOGCOLOR),                  state_fogcolor      }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGDENSITY),                { STATE_RENDER(WINED3D_RS_FOGDENSITY),                state_fogdensity    }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGENABLE),                 { STATE_RENDER(WINED3D_RS_FOGENABLE),                 state_fog_fragpart  }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGTABLEMODE),              { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGVERTEXMODE),             { STATE_RENDER(WINED3D_RS_FOGENABLE),                 NULL                }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGSTART),                  { STATE_RENDER(WINED3D_RS_FOGSTART),                  state_fogstartend   }, WINED3D_GL_EXT_NONE             },
    { STATE_RENDER(WINED3D_RS_FOGEND),                    { STATE_RENDER(WINED3D_RS_FOGSTART),                  NULL                }, WINED3D_GL_EXT_NONE             },
5600
    { STATE_RENDER(WINED3D_RS_SHADEMODE),                 { STATE_RENDER(WINED3D_RS_SHADEMODE),                 state_shademode     }, WINED3D_GL_EXT_NONE             },
5601 5602 5603 5604 5605 5606 5607 5608 5609
    { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim      }, WINED3D_GL_EXT_NONE             },
    {0 /* Terminate */,                                   { 0,                                                  0                   }, WINED3D_GL_EXT_NONE             },
5610
};
5611

5612
/* Context activation is done by the caller. */
5613
static void ffp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
5614

5615 5616 5617 5618 5619 5620 5621 5622 5623
static void *ffp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
{
    return shader_priv;
}

static void ffp_free(struct wined3d_device *device) {}

static void vp_ffp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
{
5624
    caps->xyzrhw = FALSE;
5625
    caps->ffp_generic_attributes = FALSE;
5626 5627 5628 5629 5630 5631 5632 5633
    caps->max_active_lights = gl_info->limits.lights;
    caps->max_vertex_blend_matrices = gl_info->limits.blends;
    caps->max_vertex_blend_matrix_index = 0;
    caps->vertex_processing_caps = WINED3DVTXPCAPS_DIRECTIONALLIGHTS
            | WINED3DVTXPCAPS_MATERIALSOURCE7
            | WINED3DVTXPCAPS_POSITIONALLIGHTS
            | WINED3DVTXPCAPS_LOCALVIEWER
            | WINED3DVTXPCAPS_VERTEXFOG
5634 5635
            | WINED3DVTXPCAPS_TEXGEN
            | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
5636 5637 5638 5639 5640 5641 5642
    caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 0x0008; /* 8 texture coords */
    caps->max_user_clip_planes = gl_info->limits.clipplanes;
    caps->raster_caps = 0;
    if (gl_info->supported[NV_FOG_DISTANCE])
        caps->raster_caps |= WINED3DPRASTERCAPS_FOGRANGE;
}

5643 5644 5645 5646 5647
static DWORD vp_ffp_get_emul_mask(const struct wined3d_gl_info *gl_info)
{
    return GL_EXT_EMUL_ARB_MULTITEXTURE | GL_EXT_EMUL_EXT_FOG_COORD;
}

5648 5649 5650 5651
const struct wined3d_vertex_pipe_ops ffp_vertex_pipe =
{
    ffp_enable,
    vp_ffp_get_caps,
5652
    vp_ffp_get_emul_mask,
5653 5654 5655 5656 5657
    ffp_alloc,
    ffp_free,
    vp_ffp_states,
};

5658
static void ffp_fragment_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
5659
{
5660
    caps->wined3d_caps = 0;
5661 5662 5663 5664 5665 5666 5667 5668 5669 5670
    caps->PrimitiveMiscCaps = 0;
    caps->TextureOpCaps = WINED3DTEXOPCAPS_ADD
            | WINED3DTEXOPCAPS_ADDSIGNED
            | WINED3DTEXOPCAPS_ADDSIGNED2X
            | WINED3DTEXOPCAPS_MODULATE
            | WINED3DTEXOPCAPS_MODULATE2X
            | WINED3DTEXOPCAPS_MODULATE4X
            | WINED3DTEXOPCAPS_SELECTARG1
            | WINED3DTEXOPCAPS_SELECTARG2
            | WINED3DTEXOPCAPS_DISABLE;
5671

5672 5673 5674 5675
    if (gl_info->supported[ARB_TEXTURE_ENV_COMBINE]
            || gl_info->supported[EXT_TEXTURE_ENV_COMBINE]
            || gl_info->supported[NV_TEXTURE_ENV_COMBINE4])
    {
5676 5677 5678 5679 5680 5681
        caps->TextureOpCaps |= WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
                | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
                | WINED3DTEXOPCAPS_BLENDFACTORALPHA
                | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
                | WINED3DTEXOPCAPS_LERP
                | WINED3DTEXOPCAPS_SUBTRACT;
5682
    }
5683 5684 5685
    if (gl_info->supported[ATI_TEXTURE_ENV_COMBINE3]
            || gl_info->supported[NV_TEXTURE_ENV_COMBINE4])
    {
5686 5687 5688 5689 5690
        caps->TextureOpCaps |= WINED3DTEXOPCAPS_ADDSMOOTH
                | WINED3DTEXOPCAPS_MULTIPLYADD
                | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
                | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
                | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
5691
    }
5692
    if (gl_info->supported[ARB_TEXTURE_ENV_DOT3])
5693
        caps->TextureOpCaps |= WINED3DTEXOPCAPS_DOTPRODUCT3;
5694

5695 5696
    caps->MaxTextureBlendStages = gl_info->limits.textures;
    caps->MaxSimultaneousTextures = gl_info->limits.textures;
5697 5698
}

5699 5700 5701 5702 5703
static DWORD ffp_fragment_get_emul_mask(const struct wined3d_gl_info *gl_info)
{
    return GL_EXT_EMUL_ARB_MULTITEXTURE | GL_EXT_EMUL_EXT_FOG_COORD;
}

5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719
static BOOL ffp_color_fixup_supported(struct color_fixup_desc fixup)
{
    if (TRACE_ON(d3d))
    {
        TRACE("Checking support for fixup:\n");
        dump_color_fixup_desc(fixup);
    }

    /* We only support identity conversions. */
    if (is_identity_fixup(fixup))
    {
        TRACE("[OK]\n");
        return TRUE;
    }

    TRACE("[FAILED]\n");
5720 5721
    return FALSE;
}
5722

5723 5724 5725 5726 5727 5728 5729 5730 5731
static BOOL ffp_none_context_alloc(struct wined3d_context *context)
{
    return TRUE;
}

static void ffp_none_context_free(struct wined3d_context *context)
{
}

5732
const struct fragment_pipeline ffp_fragment_pipeline = {
5733
    ffp_enable,
5734
    ffp_fragment_get_caps,
5735
    ffp_fragment_get_emul_mask,
5736 5737
    ffp_alloc,
    ffp_free,
5738 5739
    ffp_none_context_alloc,
    ffp_none_context_free,
5740
    ffp_color_fixup_supported,
5741
    ffp_fragmentstate_template,
5742
};
5743

5744
static void none_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
5745

5746
static void *none_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
5747
{
5748
    return shader_priv;
5749 5750
}

5751 5752 5753
static void none_free(struct wined3d_device *device) {}

static void vp_none_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
5754
{
5755
    memset(caps, 0, sizeof(*caps));
5756 5757
}

5758 5759 5760 5761 5762
static DWORD vp_none_get_emul_mask(const struct wined3d_gl_info *gl_info)
{
    return 0;
}

5763 5764 5765 5766
const struct wined3d_vertex_pipe_ops none_vertex_pipe =
{
    none_enable,
    vp_none_get_caps,
5767
    vp_none_get_emul_mask,
5768 5769 5770 5771 5772 5773 5774 5775 5776
    none_alloc,
    none_free,
    NULL,
};

static void fp_none_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
{
    memset(caps, 0, sizeof(*caps));
}
5777

5778 5779 5780 5781 5782
static DWORD fp_none_get_emul_mask(const struct wined3d_gl_info *gl_info)
{
    return 0;
}

5783 5784 5785 5786 5787 5788 5789
static BOOL fp_none_color_fixup_supported(struct color_fixup_desc fixup)
{
    return is_identity_fixup(fixup);
}

const struct fragment_pipeline none_fragment_pipe =
{
5790
    none_enable,
5791
    fp_none_get_caps,
5792
    fp_none_get_emul_mask,
5793 5794
    none_alloc,
    none_free,
5795 5796
    ffp_none_context_alloc,
    ffp_none_context_free,
5797 5798 5799 5800
    fp_none_color_fixup_supported,
    NULL,
};

5801 5802
static unsigned int num_handlers(const APPLYSTATEFUNC *funcs)
{
5803 5804 5805 5806 5807
    unsigned int i;
    for(i = 0; funcs[i]; i++);
    return i;
}

5808
static void multistate_apply_2(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
5809
{
5810 5811
    context->swapchain->device->multistate_funcs[state_id][0](context, state, state_id);
    context->swapchain->device->multistate_funcs[state_id][1](context, state, state_id);
5812 5813
}

5814
static void multistate_apply_3(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
5815
{
5816 5817 5818
    context->swapchain->device->multistate_funcs[state_id][0](context, state, state_id);
    context->swapchain->device->multistate_funcs[state_id][1](context, state, state_id);
    context->swapchain->device->multistate_funcs[state_id][2](context, state, state_id);
5819 5820
}

5821 5822
static void prune_invalid_states(struct StateEntry *state_table, const struct wined3d_gl_info *gl_info,
        const struct wined3d_d3d_info *d3d_info)
5823 5824 5825
{
    unsigned int start, last, i;

5826
    start = STATE_TEXTURESTAGE(d3d_info->limits.ffp_blend_stages, 0);
5827 5828 5829 5830 5831 5832 5833
    last = STATE_TEXTURESTAGE(MAX_TEXTURES - 1, WINED3D_HIGHEST_TEXTURE_STATE);
    for (i = start; i <= last; ++i)
    {
        state_table[i].representative = 0;
        state_table[i].apply = state_undefined;
    }

5834
    start = STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + d3d_info->limits.ffp_blend_stages);
5835
    last = STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + MAX_TEXTURES - 1);
5836 5837 5838 5839 5840
    for (i = start; i <= last; ++i)
    {
        state_table[i].representative = 0;
        state_table[i].apply = state_undefined;
    }
5841

5842
    start = STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(d3d_info->limits.ffp_vertex_blend_matrices));
5843
    last = STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(255));
5844 5845 5846 5847 5848
    for (i = start; i <= last; ++i)
    {
        state_table[i].representative = 0;
        state_table[i].apply = state_undefined;
    }
5849 5850
}

5851 5852
static void validate_state_table(struct StateEntry *state_table)
{
5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864
    static const struct
    {
        DWORD first;
        DWORD last;
    }
    rs_holes[] =
    {
        {  1,   1},
        {  3,   3},
        { 17,  18},
        { 21,  21},
        { 42,  45},
5865
        { 47,  47},
5866 5867 5868 5869 5870 5871 5872
        { 61, 127},
        {149, 150},
        {169, 169},
        {177, 177},
        {196, 197},
        {  0,   0},
    };
5873 5874 5875 5876 5877 5878
    static const DWORD simple_states[] =
    {
        STATE_MATERIAL,
        STATE_VDECL,
        STATE_STREAMSRC,
        STATE_INDEXBUFFER,
5879 5880 5881
        STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX),
        STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY),
        STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL),
5882 5883 5884
        STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_VERTEX),
        STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_GEOMETRY),
        STATE_CONSTANT_BUFFER(WINED3D_SHADER_TYPE_PIXEL),
5885
        STATE_SHADER_RESOURCE_BINDING,
5886
        STATE_VIEWPORT,
5887
        STATE_LIGHT_TYPE,
5888 5889
        STATE_SCISSORRECT,
        STATE_FRONTFACE,
5890
        STATE_POINTSPRITECOORDORIGIN,
5891
        STATE_BASEVERTEXINDEX,
5892
        STATE_FRAMEBUFFER,
5893
        STATE_POINT_ENABLE,
5894
        STATE_COLOR_KEY,
5895
    };
5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909
    unsigned int i, current;

    for (i = STATE_RENDER(1), current = 0; i <= STATE_RENDER(WINEHIGHEST_RENDER_STATE); ++i)
    {
        if (!rs_holes[current].first || i < STATE_RENDER(rs_holes[current].first))
        {
            if (!state_table[i].representative)
                ERR("State %s (%#x) should have a representative.\n", debug_d3dstate(i), i);
        }
        else if (state_table[i].representative)
            ERR("State %s (%#x) shouldn't have a representative.\n", debug_d3dstate(i), i);

        if (i == STATE_RENDER(rs_holes[current].last)) ++current;
    }
5910

5911 5912 5913 5914 5915 5916 5917
    for (i = 0; i < sizeof(simple_states) / sizeof(*simple_states); ++i)
    {
        if (!state_table[simple_states[i]].representative)
            ERR("State %s (%#x) should have a representative.\n",
                    debug_d3dstate(simple_states[i]), simple_states[i]);
    }

5918 5919 5920
    for (i = 0; i < STATE_HIGHEST + 1; ++i)
    {
        DWORD rep = state_table[i].representative;
5921
        if (rep)
5922
        {
5923
            if (state_table[rep].representative != rep)
5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938
            {
                ERR("State %s (%#x) has invalid representative %s (%#x).\n",
                        debug_d3dstate(i), i, debug_d3dstate(rep), rep);
                state_table[i].representative = 0;
            }

            if (rep != i)
            {
                if (state_table[i].apply)
                    ERR("State %s (%#x) has both a handler and representative.\n", debug_d3dstate(i), i);
            }
            else if (!state_table[i].apply)
            {
                ERR("Self representing state %s (%#x) has no handler.\n", debug_d3dstate(i), i);
            }
5939 5940 5941 5942
        }
    }
}

5943
HRESULT compile_state_table(struct StateEntry *StateTable, APPLYSTATEFUNC **dev_multistate_funcs,
5944 5945 5946
        const struct wined3d_gl_info *gl_info, const struct wined3d_d3d_info *d3d_info,
        const struct wined3d_vertex_pipe_ops *vertex, const struct fragment_pipeline *fragment,
        const struct StateEntryTemplate *misc)
5947
{
5948 5949 5950
    unsigned int i, type, handlers;
    APPLYSTATEFUNC multistate_funcs[STATE_HIGHEST + 1][3];
    const struct StateEntryTemplate *cur;
5951
    BOOL set[STATE_HIGHEST + 1];
5952 5953 5954

    memset(multistate_funcs, 0, sizeof(multistate_funcs));

5955 5956 5957 5958 5959
    for(i = 0; i < STATE_HIGHEST + 1; i++) {
        StateTable[i].representative = 0;
        StateTable[i].apply = state_undefined;
    }

5960 5961 5962 5963
    for(type = 0; type < 3; type++) {
        /* This switch decides the order in which the states are applied */
        switch(type) {
            case 0: cur = misc; break;
5964
            case 1: cur = fragment->states; break;
5965
            case 2: cur = vertex->vp_states; break;
5966 5967 5968 5969
            default: cur = NULL; /* Stupid compiler */
        }
        if(!cur) continue;

5970 5971 5972 5973 5974
        /* GL extension filtering should not prevent multiple handlers being applied from different
         * pipeline parts
         */
        memset(set, 0, sizeof(set));

5975
        for(i = 0; cur[i].state; i++) {
5976
            APPLYSTATEFUNC *funcs_array;
5977 5978 5979 5980 5981 5982 5983 5984 5985 5986

            /* Only use the first matching state with the available extension from one template.
             * e.g.
             * {D3DRS_FOOBAR, {D3DRS_FOOBAR, func1}, XYZ_FANCY},
             * {D3DRS_FOOBAR, {D3DRS_FOOBAR, func2}, 0        }
             *
             * if GL_XYZ_fancy is supported, ignore the 2nd line
             */
            if(set[cur[i].state]) continue;
            /* Skip state lines depending on unsupported extensions */
5987
            if (!gl_info->supported[cur[i].extension]) continue;
5988 5989 5990 5991 5992 5993 5994 5995
            set[cur[i].state] = TRUE;
            /* In some cases having an extension means that nothing has to be
             * done for a state, e.g. if GL_ARB_texture_non_power_of_two is
             * supported, the texture coordinate fixup can be ignored. If the
             * apply function is used, mark the state set(done above) to prevent
             * applying later lines, but do not record anything in the state
             * table
             */
5996
            if (!cur[i].content.representative) continue;
5997

5998 5999 6000 6001 6002 6003 6004 6005
            handlers = num_handlers(multistate_funcs[cur[i].state]);
            multistate_funcs[cur[i].state][handlers] = cur[i].content.apply;
            switch(handlers) {
                case 0:
                    StateTable[cur[i].state].apply = cur[i].content.apply;
                    break;
                case 1:
                    StateTable[cur[i].state].apply = multistate_apply_2;
6006
                    if (!(dev_multistate_funcs[cur[i].state] = wined3d_calloc(2, sizeof(**dev_multistate_funcs))))
6007 6008
                        goto out_of_mem;

6009 6010 6011 6012 6013
                    dev_multistate_funcs[cur[i].state][0] = multistate_funcs[cur[i].state][0];
                    dev_multistate_funcs[cur[i].state][1] = multistate_funcs[cur[i].state][1];
                    break;
                case 2:
                    StateTable[cur[i].state].apply = multistate_apply_3;
6014 6015 6016 6017 6018 6019 6020 6021 6022
                    funcs_array = HeapReAlloc(GetProcessHeap(),
                                              0,
                                              dev_multistate_funcs[cur[i].state],
                                              sizeof(**dev_multistate_funcs) * 3);
                    if (!funcs_array) {
                        goto out_of_mem;
                    }

                    dev_multistate_funcs[cur[i].state] = funcs_array;
6023 6024 6025 6026 6027 6028 6029
                    dev_multistate_funcs[cur[i].state][2] = multistate_funcs[cur[i].state][2];
                    break;
                default:
                    ERR("Unexpected amount of state handlers for state %u: %u\n",
                        cur[i].state, handlers + 1);
            }

6030 6031 6032 6033 6034
            if (StateTable[cur[i].state].representative
                    && StateTable[cur[i].state].representative != cur[i].content.representative)
            {
                FIXME("State %s (%#x) has different representatives in different pipeline parts.\n",
                        debug_d3dstate(cur[i].state), cur[i].state);
6035 6036 6037 6038
            }
            StateTable[cur[i].state].representative = cur[i].content.representative;
        }
    }
6039

6040
    prune_invalid_states(StateTable, gl_info, d3d_info);
6041 6042
    validate_state_table(StateTable);

6043 6044 6045 6046 6047 6048 6049 6050 6051 6052
    return WINED3D_OK;

out_of_mem:
    for (i = 0; i <= STATE_HIGHEST; ++i) {
        HeapFree(GetProcessHeap(), 0, dev_multistate_funcs[i]);
    }

    memset(dev_multistate_funcs, 0, (STATE_HIGHEST + 1)*sizeof(*dev_multistate_funcs));

    return E_OUTOFMEMORY;
6053
}