shader.c 81.5 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright 2002-2003 Jason Edmeades
 * Copyright 2002-2003 Raphael Junqueira
 * Copyright 2004 Christian Costa
 * Copyright 2005 Oliver Stieber
 * Copyright 2006 Ivan Gyurdiev
 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8
 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 10 11 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"

#include <math.h>
#include <stdio.h>
29
#include <string.h>
30 31 32 33

#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34 35
WINE_DECLARE_DEBUG_CHANNEL(d3d);

36
static const char * const shader_opcode_names[] =
37 38 39
{
    /* WINED3DSIH_ABS           */ "abs",
    /* WINED3DSIH_ADD           */ "add",
40
    /* WINED3DSIH_AND           */ "and",
41 42 43 44 45 46 47 48 49 50 51 52 53 54
    /* WINED3DSIH_BEM           */ "bem",
    /* WINED3DSIH_BREAK         */ "break",
    /* WINED3DSIH_BREAKC        */ "breakc",
    /* WINED3DSIH_BREAKP        */ "breakp",
    /* WINED3DSIH_CALL          */ "call",
    /* WINED3DSIH_CALLNZ        */ "callnz",
    /* WINED3DSIH_CMP           */ "cmp",
    /* WINED3DSIH_CND           */ "cnd",
    /* WINED3DSIH_CRS           */ "crs",
    /* WINED3DSIH_CUT           */ "cut",
    /* WINED3DSIH_DCL           */ "dcl",
    /* WINED3DSIH_DEF           */ "def",
    /* WINED3DSIH_DEFB          */ "defb",
    /* WINED3DSIH_DEFI          */ "defi",
55
    /* WINED3DSIH_DIV           */ "div",
56 57 58 59 60 61 62 63 64 65 66
    /* WINED3DSIH_DP2ADD        */ "dp2add",
    /* WINED3DSIH_DP3           */ "dp3",
    /* WINED3DSIH_DP4           */ "dp4",
    /* WINED3DSIH_DST           */ "dst",
    /* WINED3DSIH_DSX           */ "dsx",
    /* WINED3DSIH_DSY           */ "dsy",
    /* WINED3DSIH_ELSE          */ "else",
    /* WINED3DSIH_EMIT          */ "emit",
    /* WINED3DSIH_ENDIF         */ "endif",
    /* WINED3DSIH_ENDLOOP       */ "endloop",
    /* WINED3DSIH_ENDREP        */ "endrep",
67
    /* WINED3DSIH_EQ            */ "eq",
68 69 70
    /* WINED3DSIH_EXP           */ "exp",
    /* WINED3DSIH_EXPP          */ "expp",
    /* WINED3DSIH_FRC           */ "frc",
71
    /* WINED3DSIH_FTOI          */ "ftoi",
72
    /* WINED3DSIH_GE            */ "ge",
73
    /* WINED3DSIH_IADD          */ "iadd",
74
    /* WINED3DSIH_IEQ           */ "ieq",
75 76 77
    /* WINED3DSIH_IF            */ "if",
    /* WINED3DSIH_IFC           */ "ifc",
    /* WINED3DSIH_IGE           */ "ige",
78
    /* WINED3DSIH_IMUL          */ "imul",
79
    /* WINED3DSIH_ITOF          */ "itof",
80
    /* WINED3DSIH_LABEL         */ "label",
81
    /* WINED3DSIH_LD            */ "ld",
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    /* WINED3DSIH_LIT           */ "lit",
    /* WINED3DSIH_LOG           */ "log",
    /* WINED3DSIH_LOGP          */ "logp",
    /* WINED3DSIH_LOOP          */ "loop",
    /* WINED3DSIH_LRP           */ "lrp",
    /* WINED3DSIH_LT            */ "lt",
    /* WINED3DSIH_M3x2          */ "m3x2",
    /* WINED3DSIH_M3x3          */ "m3x3",
    /* WINED3DSIH_M3x4          */ "m3x4",
    /* WINED3DSIH_M4x3          */ "m4x3",
    /* WINED3DSIH_M4x4          */ "m4x4",
    /* WINED3DSIH_MAD           */ "mad",
    /* WINED3DSIH_MAX           */ "max",
    /* WINED3DSIH_MIN           */ "min",
    /* WINED3DSIH_MOV           */ "mov",
    /* WINED3DSIH_MOVA          */ "mova",
98
    /* WINED3DSIH_MOVC          */ "movc",
99 100 101 102 103 104 105 106
    /* WINED3DSIH_MUL           */ "mul",
    /* WINED3DSIH_NOP           */ "nop",
    /* WINED3DSIH_NRM           */ "nrm",
    /* WINED3DSIH_PHASE         */ "phase",
    /* WINED3DSIH_POW           */ "pow",
    /* WINED3DSIH_RCP           */ "rcp",
    /* WINED3DSIH_REP           */ "rep",
    /* WINED3DSIH_RET           */ "ret",
107
    /* WINED3DSIH_ROUND_NI      */ "round_ni",
108
    /* WINED3DSIH_RSQ           */ "rsq",
109
    /* WINED3DSIH_SAMPLE        */ "sample",
110
    /* WINED3DSIH_SAMPLE_GRAD   */ "sample_d",
111
    /* WINED3DSIH_SAMPLE_LOD    */ "sample_l",
112 113 114 115 116
    /* WINED3DSIH_SETP          */ "setp",
    /* WINED3DSIH_SGE           */ "sge",
    /* WINED3DSIH_SGN           */ "sgn",
    /* WINED3DSIH_SINCOS        */ "sincos",
    /* WINED3DSIH_SLT           */ "slt",
117
    /* WINED3DSIH_SQRT          */ "sqrt",
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /* WINED3DSIH_SUB           */ "sub",
    /* WINED3DSIH_TEX           */ "texld",
    /* WINED3DSIH_TEXBEM        */ "texbem",
    /* WINED3DSIH_TEXBEML       */ "texbeml",
    /* WINED3DSIH_TEXCOORD      */ "texcrd",
    /* WINED3DSIH_TEXDEPTH      */ "texdepth",
    /* WINED3DSIH_TEXDP3        */ "texdp3",
    /* WINED3DSIH_TEXDP3TEX     */ "texdp3tex",
    /* WINED3DSIH_TEXKILL       */ "texkill",
    /* WINED3DSIH_TEXLDD        */ "texldd",
    /* WINED3DSIH_TEXLDL        */ "texldl",
    /* WINED3DSIH_TEXM3x2DEPTH  */ "texm3x2depth",
    /* WINED3DSIH_TEXM3x2PAD    */ "texm3x2pad",
    /* WINED3DSIH_TEXM3x2TEX    */ "texm3x2tex",
    /* WINED3DSIH_TEXM3x3       */ "texm3x3",
    /* WINED3DSIH_TEXM3x3DIFF   */ "texm3x3diff",
    /* WINED3DSIH_TEXM3x3PAD    */ "texm3x3pad",
    /* WINED3DSIH_TEXM3x3SPEC   */ "texm3x3spec",
    /* WINED3DSIH_TEXM3x3TEX    */ "texm3x3tex",
    /* WINED3DSIH_TEXM3x3VSPEC  */ "texm3x3vspec",
    /* WINED3DSIH_TEXREG2AR     */ "texreg2ar",
    /* WINED3DSIH_TEXREG2GB     */ "texreg2gb",
    /* WINED3DSIH_TEXREG2RGB    */ "texreg2rgb",
141
    /* WINED3DSIH_UDIV          */ "udiv",
142
    /* WINED3DSIH_USHR          */ "ushr",
143
    /* WINED3DSIH_UTOF          */ "utof",
144
    /* WINED3DSIH_XOR           */ "xor",
145 146
};

147
static const char * const semantic_names[] =
148
{
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    /* WINED3D_DECL_USAGE_POSITION      */ "SV_POSITION",
    /* WINED3D_DECL_USAGE_BLEND_WEIGHT  */ "BLENDWEIGHT",
    /* WINED3D_DECL_USAGE_BLEND_INDICES */ "BLENDINDICES",
    /* WINED3D_DECL_USAGE_NORMAL        */ "NORMAL",
    /* WINED3D_DECL_USAGE_PSIZE         */ "PSIZE",
    /* WINED3D_DECL_USAGE_TEXCOORD      */ "TEXCOORD",
    /* WINED3D_DECL_USAGE_TANGENT       */ "TANGENT",
    /* WINED3D_DECL_USAGE_BINORMAL      */ "BINORMAL",
    /* WINED3D_DECL_USAGE_TESS_FACTOR   */ "TESSFACTOR",
    /* WINED3D_DECL_USAGE_POSITIONT     */ "POSITIONT",
    /* WINED3D_DECL_USAGE_COLOR         */ "COLOR",
    /* WINED3D_DECL_USAGE_FOG           */ "FOG",
    /* WINED3D_DECL_USAGE_DEPTH         */ "DEPTH",
    /* WINED3D_DECL_USAGE_SAMPLE        */ "SAMPLE",
163 164
};

165
static const char *shader_semantic_name_from_usage(enum wined3d_decl_usage usage)
166 167 168 169 170 171 172 173 174 175
{
    if (usage >= sizeof(semantic_names) / sizeof(*semantic_names))
    {
        FIXME("Unrecognized usage %#x.\n", usage);
        return "UNRECOGNIZED";
    }

    return semantic_names[usage];
}

176
static enum wined3d_decl_usage shader_usage_from_semantic_name(const char *name)
177 178 179 180 181 182 183 184 185 186 187
{
    unsigned int i;

    for (i = 0; i < sizeof(semantic_names) / sizeof(*semantic_names); ++i)
    {
        if (!strcmp(name, semantic_names[i])) return i;
    }

    return ~0U;
}

188
BOOL shader_match_semantic(const char *semantic_name, enum wined3d_decl_usage usage)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
    return !strcmp(semantic_name, shader_semantic_name_from_usage(usage));
}

static void shader_signature_from_semantic(struct wined3d_shader_signature_element *e,
        const struct wined3d_shader_semantic *s)
{
    e->semantic_name = shader_semantic_name_from_usage(s->usage);
    e->semantic_idx = s->usage_idx;
    e->sysval_semantic = 0;
    e->component_type = 0;
    e->register_idx = s->reg.reg.idx;
    e->mask = s->reg.write_mask;
}

204
static void shader_signature_from_usage(struct wined3d_shader_signature_element *e,
205
        enum wined3d_decl_usage usage, UINT usage_idx, UINT reg_idx, DWORD write_mask)
206 207 208 209 210 211 212 213 214
{
    e->semantic_name = shader_semantic_name_from_usage(usage);
    e->semantic_idx = usage_idx;
    e->sysval_semantic = 0;
    e->component_type = 0;
    e->register_idx = reg_idx;
    e->mask = write_mask;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 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 296 297 298 299 300 301 302 303 304 305 306
static const struct wined3d_shader_frontend *shader_select_frontend(DWORD version_token)
{
    switch (version_token >> 16)
    {
        case WINED3D_SM1_VS:
        case WINED3D_SM1_PS:
            return &sm1_shader_frontend;

        case WINED3D_SM4_PS:
        case WINED3D_SM4_VS:
        case WINED3D_SM4_GS:
            return &sm4_shader_frontend;

        default:
            FIXME("Unrecognised version token %#x\n", version_token);
            return NULL;
    }
}

void shader_buffer_clear(struct wined3d_shader_buffer *buffer)
{
    buffer->buffer[0] = '\0';
    buffer->bsize = 0;
    buffer->lineNo = 0;
    buffer->newline = TRUE;
}

BOOL shader_buffer_init(struct wined3d_shader_buffer *buffer)
{
    buffer->buffer = HeapAlloc(GetProcessHeap(), 0, SHADER_PGMSIZE);
    if (!buffer->buffer)
    {
        ERR("Failed to allocate shader buffer memory.\n");
        return FALSE;
    }

    shader_buffer_clear(buffer);
    return TRUE;
}

void shader_buffer_free(struct wined3d_shader_buffer *buffer)
{
    HeapFree(GetProcessHeap(), 0, buffer->buffer);
}

int shader_vaddline(struct wined3d_shader_buffer *buffer, const char *format, va_list args)
{
    char *base = buffer->buffer + buffer->bsize;
    int rc;

    rc = vsnprintf(base, SHADER_PGMSIZE - 1 - buffer->bsize, format, args);

    if (rc < 0 /* C89 */ || (unsigned int)rc > SHADER_PGMSIZE - 1 - buffer->bsize /* C99 */)
    {
        ERR("The buffer allocated for the shader program string "
            "is too small at %d bytes.\n", SHADER_PGMSIZE);
        buffer->bsize = SHADER_PGMSIZE - 1;
        return -1;
    }

    if (buffer->newline)
    {
        TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
        buffer->newline = FALSE;
    }
    else
    {
        TRACE("%s", base);
    }

    buffer->bsize += rc;
    if (buffer->buffer[buffer->bsize-1] == '\n')
    {
        ++buffer->lineNo;
        buffer->newline = TRUE;
    }

    return 0;
}

int shader_addline(struct wined3d_shader_buffer *buffer, const char *format, ...)
{
    va_list args;
    int ret;

    va_start(args, format);
    ret = shader_vaddline(buffer, format, args);
    va_end(args);

    return ret;
}

307
static void shader_init(struct wined3d_shader *shader, struct wined3d_device *device,
308
        void *parent, const struct wined3d_parent_ops *parent_ops)
309 310
{
    shader->ref = 1;
311
    shader->device = device;
312 313 314 315 316 317 318 319
    shader->parent = parent;
    shader->parent_ops = parent_ops;
    list_init(&shader->linked_programs);
    list_add_head(&device->shaders, &shader->shader_list_entry);
}

/* Convert floating point offset relative to a register file to an absolute
 * offset for float constants. */
320
static unsigned int shader_get_float_offset(enum wined3d_shader_register_type register_type, UINT register_idx)
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
{
    switch (register_type)
    {
        case WINED3DSPR_CONST: return register_idx;
        case WINED3DSPR_CONST2: return 2048 + register_idx;
        case WINED3DSPR_CONST3: return 4096 + register_idx;
        case WINED3DSPR_CONST4: return 6144 + register_idx;
        default:
            FIXME("Unsupported register type: %u.\n", register_type);
            return register_idx;
    }
}

static void shader_delete_constant_list(struct list *clist)
{
336
    struct wined3d_shader_lconst *constant;
337 338 339 340 341
    struct list *ptr;

    ptr = list_head(clist);
    while (ptr)
    {
342
        constant = LIST_ENTRY(ptr, struct wined3d_shader_lconst, entry);
343 344 345 346 347 348 349 350 351 352 353 354 355 356
        ptr = list_next(clist, ptr);
        HeapFree(GetProcessHeap(), 0, constant);
    }
    list_init(clist);
}

static inline void set_bitmap_bit(DWORD *bitmap, DWORD bit)
{
    DWORD idx, shift;
    idx = bit >> 5;
    shift = bit & 0x1f;
    bitmap[idx] |= (1 << shift);
}

357
static void shader_record_register_usage(struct wined3d_shader *shader, struct wined3d_shader_reg_maps *reg_maps,
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        const struct wined3d_shader_register *reg, enum wined3d_shader_type shader_type)
{
    switch (reg->type)
    {
        case WINED3DSPR_TEXTURE: /* WINED3DSPR_ADDR */
            if (shader_type == WINED3D_SHADER_TYPE_PIXEL) reg_maps->texcoord |= 1 << reg->idx;
            else reg_maps->address |= 1 << reg->idx;
            break;

        case WINED3DSPR_TEMP:
            reg_maps->temporary |= 1 << reg->idx;
            break;

        case WINED3DSPR_INPUT:
            if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
            {
                if (reg->rel_addr)
                {
                    /* If relative addressing is used, we must assume that all registers
                     * are used. Even if it is a construct like v3[aL], we can't assume
                     * that v0, v1 and v2 aren't read because aL can be negative */
                    unsigned int i;
                    for (i = 0; i < MAX_REG_INPUT; ++i)
                    {
382
                        shader->u.ps.input_reg_used[i] = TRUE;
383 384 385 386
                    }
                }
                else
                {
387
                    shader->u.ps.input_reg_used[reg->idx] = TRUE;
388 389 390 391 392 393 394 395 396 397 398 399
                }
            }
            else reg_maps->input_registers |= 1 << reg->idx;
            break;

        case WINED3DSPR_RASTOUT:
            if (reg->idx == 1) reg_maps->fog = 1;
            break;

        case WINED3DSPR_MISCTYPE:
            if (shader_type == WINED3D_SHADER_TYPE_PIXEL)
            {
400
                if (!reg->idx) reg_maps->vpos = 1;
401 402 403 404 405 406 407
                else if (reg->idx == 1) reg_maps->usesfacing = 1;
            }
            break;

        case WINED3DSPR_CONST:
            if (reg->rel_addr)
            {
408 409
                if (reg->idx < reg_maps->min_rel_offset) reg_maps->min_rel_offset = reg->idx;
                if (reg->idx > reg_maps->max_rel_offset) reg_maps->max_rel_offset = reg->idx;
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
                reg_maps->usesrelconstF = TRUE;
            }
            else
            {
                set_bitmap_bit(reg_maps->constf, reg->idx);
            }
            break;

        case WINED3DSPR_CONSTINT:
            reg_maps->integer_constants |= (1 << reg->idx);
            break;

        case WINED3DSPR_CONSTBOOL:
            reg_maps->boolean_constants |= (1 << reg->idx);
            break;

        case WINED3DSPR_COLOROUT:
427
            reg_maps->rt_mask |= (1 << reg->idx);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
            break;

        default:
            TRACE("Not recording register of type %#x and idx %u\n", reg->type, reg->idx);
            break;
    }
}

static unsigned int get_instr_extra_regcount(enum WINED3D_SHADER_INSTRUCTION_HANDLER instr, unsigned int param)
{
    switch (instr)
    {
        case WINED3DSIH_M4x4:
        case WINED3DSIH_M3x4:
            return param == 1 ? 3 : 0;

        case WINED3DSIH_M4x3:
        case WINED3DSIH_M3x3:
            return param == 1 ? 2 : 0;

        case WINED3DSIH_M3x2:
            return param == 1 ? 1 : 0;

        default:
            return 0;
    }
}

/* Note that this does not count the loop register as an address register. */
457
static HRESULT shader_get_registers_used(struct wined3d_shader *shader, const struct wined3d_shader_frontend *fe,
458
        struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_signature_element *input_signature,
459 460 461
        struct wined3d_shader_signature_element *output_signature, const DWORD *byte_code, DWORD constf_size)
{
    unsigned int cur_loop_depth = 0, max_loop_depth = 0;
462
    void *fe_data = shader->frontend_data;
463 464 465 466
    struct wined3d_shader_version shader_version;
    const DWORD *ptr = byte_code;

    memset(reg_maps, 0, sizeof(*reg_maps));
467
    reg_maps->min_rel_offset = ~0U;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

    fe->shader_read_header(fe_data, &ptr, &shader_version);
    reg_maps->shader_version = shader_version;

    reg_maps->constf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
            sizeof(*reg_maps->constf) * ((constf_size + 31) / 32));
    if (!reg_maps->constf)
    {
        ERR("Failed to allocate constant map memory.\n");
        return E_OUTOFMEMORY;
    }

    while (!fe->shader_is_end(fe_data, &ptr))
    {
        struct wined3d_shader_instruction ins;
        const char *comment;
484
        UINT comment_size;
485 486 487
        UINT param_size;

        /* Skip comments. */
488
        fe->shader_read_comment(&ptr, &comment, &comment_size);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
        if (comment) continue;

        /* Fetch opcode. */
        fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);

        /* Unhandled opcode, and its parameters. */
        if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
        {
            TRACE("Skipping unrecognized instruction.\n");
            ptr += param_size;
            continue;
        }

        /* Handle declarations. */
        if (ins.handler_idx == WINED3DSIH_DCL)
        {
            struct wined3d_shader_semantic semantic;

            fe->shader_read_semantic(&ptr, &semantic);

            switch (semantic.reg.reg.type)
            {
                /* Mark input registers used. */
                case WINED3DSPR_INPUT:
                    reg_maps->input_registers |= 1 << semantic.reg.reg.idx;
                    shader_signature_from_semantic(&input_signature[semantic.reg.reg.idx], &semantic);
                    break;

                /* Vertex shader: mark 3.0 output registers used, save token. */
                case WINED3DSPR_OUTPUT:
                    reg_maps->output_registers |= 1 << semantic.reg.reg.idx;
                    shader_signature_from_semantic(&output_signature[semantic.reg.reg.idx], &semantic);
521 522
                    if (semantic.usage == WINED3D_DECL_USAGE_FOG)
                        reg_maps->fog = 1;
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
                    break;

                /* Save sampler usage token. */
                case WINED3DSPR_SAMPLER:
                    reg_maps->sampler_type[semantic.reg.reg.idx] = semantic.sampler_type;
                    break;

                default:
                    TRACE("Not recording DCL register type %#x.\n", semantic.reg.reg.type);
                    break;
            }
        }
        else if (ins.handler_idx == WINED3DSIH_DEF)
        {
            struct wined3d_shader_src_param rel_addr;
            struct wined3d_shader_dst_param dst;

540
            struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
            if (!lconst) return E_OUTOFMEMORY;

            fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
            lconst->idx = dst.reg.idx;

            memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
            ptr += 4;

            /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
            if (shader_version.major == 1 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
            {
                float *value = (float *)lconst->value;
                if (value[0] < -1.0f) value[0] = -1.0f;
                else if (value[0] > 1.0f) value[0] = 1.0f;
                if (value[1] < -1.0f) value[1] = -1.0f;
                else if (value[1] > 1.0f) value[1] = 1.0f;
                if (value[2] < -1.0f) value[2] = -1.0f;
                else if (value[2] > 1.0f) value[2] = 1.0f;
                if (value[3] < -1.0f) value[3] = -1.0f;
                else if (value[3] > 1.0f) value[3] = 1.0f;
            }

563
            list_add_head(&shader->constantsF, &lconst->entry);
564 565 566 567 568 569
        }
        else if (ins.handler_idx == WINED3DSIH_DEFI)
        {
            struct wined3d_shader_src_param rel_addr;
            struct wined3d_shader_dst_param dst;

570
            struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
571 572 573 574 575 576 577 578
            if (!lconst) return E_OUTOFMEMORY;

            fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
            lconst->idx = dst.reg.idx;

            memcpy(lconst->value, ptr, 4 * sizeof(DWORD));
            ptr += 4;

579
            list_add_head(&shader->constantsI, &lconst->entry);
580 581 582 583 584 585 586
            reg_maps->local_int_consts |= (1 << dst.reg.idx);
        }
        else if (ins.handler_idx == WINED3DSIH_DEFB)
        {
            struct wined3d_shader_src_param rel_addr;
            struct wined3d_shader_dst_param dst;

587
            struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
588 589 590 591 592 593 594 595
            if (!lconst) return E_OUTOFMEMORY;

            fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);
            lconst->idx = dst.reg.idx;

            memcpy(lconst->value, ptr, sizeof(DWORD));
            ++ptr;

596
            list_add_head(&shader->constantsB, &lconst->entry);
597 598 599 600 601 602 603 604 605 606 607 608 609 610
            reg_maps->local_bool_consts |= (1 << dst.reg.idx);
        }
        /* For subroutine prototypes. */
        else if (ins.handler_idx == WINED3DSIH_LABEL)
        {
            struct wined3d_shader_src_param src, rel_addr;

            fe->shader_read_src_param(fe_data, &ptr, &src, &rel_addr);
            reg_maps->labels |= 1 << src.reg.idx;
        }
        /* Set texture, address, temporary registers. */
        else
        {
            BOOL color0_mov = FALSE;
611
            unsigned int i, limit;
612 613 614 615 616 617 618

            /* This will loop over all the registers and try to
             * make a bitmask of the ones we're interested in.
             *
             * Relative addressing tokens are ignored, but that's
             * okay, since we'll catch any address registers when
             * they are initialized (required by spec). */
619
            for (i = 0; i < ins.dst_count; ++i)
620 621 622 623 624 625 626 627 628 629 630
            {
                struct wined3d_shader_src_param dst_rel_addr;
                struct wined3d_shader_dst_param dst_param;

                fe->shader_read_dst_param(fe_data, &ptr, &dst_param, &dst_rel_addr);

                shader_record_register_usage(shader, reg_maps, &dst_param.reg, shader_version.type);

                /* WINED3DSPR_TEXCRDOUT is the same as WINED3DSPR_OUTPUT. _OUTPUT can be > MAX_REG_TEXCRD and
                 * is used in >= 3.0 shaders. Filter 3.0 shaders to prevent overflows, and also filter pixel
                 * shaders because TECRDOUT isn't used in them, but future register types might cause issues */
631
                if (shader_version.type == WINED3D_SHADER_TYPE_VERTEX && shader_version.major < 3)
632
                {
633 634 635 636 637 638 639 640 641 642
                    UINT idx = dst_param.reg.idx;

                    switch (dst_param.reg.type)
                    {
                        case WINED3DSPR_RASTOUT:
                            switch (idx)
                            {
                                case 0: /* oPos */
                                    reg_maps->output_registers |= 1 << 10;
                                    shader_signature_from_usage(&output_signature[10],
643
                                            WINED3D_DECL_USAGE_POSITION, 0, 10, WINED3DSP_WRITEMASK_ALL);
644 645 646 647 648
                                    break;

                                case 1: /* oFog */
                                    reg_maps->output_registers |= 1 << 11;
                                    shader_signature_from_usage(&output_signature[11],
649
                                            WINED3D_DECL_USAGE_FOG, 0, 11, WINED3DSP_WRITEMASK_0);
650 651 652 653 654
                                    break;

                                case 2: /* oPts */
                                    reg_maps->output_registers |= 1 << 11;
                                    shader_signature_from_usage(&output_signature[11],
655
                                            WINED3D_DECL_USAGE_PSIZE, 0, 11, WINED3DSP_WRITEMASK_1);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
                                    break;
                            }
                            break;

                        case WINED3DSPR_ATTROUT:
                            if (idx < 2)
                            {
                                idx += 8;
                                if (reg_maps->output_registers & (1 << idx))
                                {
                                    output_signature[idx].mask |= dst_param.write_mask;
                                }
                                else
                                {
                                    reg_maps->output_registers |= 1 << idx;
                                    shader_signature_from_usage(&output_signature[idx],
672
                                            WINED3D_DECL_USAGE_COLOR, idx - 8, idx, dst_param.write_mask);
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
                                }
                            }
                            break;

                        case WINED3DSPR_TEXCRDOUT:

                            reg_maps->texcoord_mask[idx] |= dst_param.write_mask;
                            if (reg_maps->output_registers & (1 << idx))
                            {
                                output_signature[idx].mask |= dst_param.write_mask;
                            }
                            else
                            {
                                reg_maps->output_registers |= 1 << idx;
                                shader_signature_from_usage(&output_signature[idx],
688
                                        WINED3D_DECL_USAGE_TEXCOORD, idx, idx, dst_param.write_mask);
689 690 691 692 693 694
                            }
                            break;

                        default:
                            break;
                    }
695 696 697 698
                }

                if (shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
                {
699
                    if (dst_param.reg.type == WINED3DSPR_COLOROUT && !dst_param.reg.idx)
700
                    {
701 702 703 704 705 706 707 708
                        /* Many 2.0 and 3.0 pixel shaders end with a MOV from a temp register to
                         * COLOROUT 0. If we know this in advance, the ARB shader backend can skip
                         * the mov and perform the sRGB write correction from the source register.
                         *
                         * However, if the mov is only partial, we can't do this, and if the write
                         * comes from an instruction other than MOV it is hard to do as well. If
                         * COLOROUT 0 is overwritten partially later, the marker is dropped again. */
                        shader->u.ps.color0_mov = FALSE;
709 710
                        if (ins.handler_idx == WINED3DSIH_MOV
                                && dst_param.write_mask == WINED3DSP_WRITEMASK_ALL)
711 712 713 714 715 716 717 718
                        {
                            /* Used later when the source register is read. */
                            color0_mov = TRUE;
                        }
                    }
                    /* Also drop the MOV marker if the source register is overwritten prior to the shader
                     * end
                     */
719 720
                    else if (dst_param.reg.type == WINED3DSPR_TEMP
                            && dst_param.reg.idx == shader->u.ps.color0_reg)
721
                    {
722
                        shader->u.ps.color0_mov = FALSE;
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
                    }
                }

                /* Declare 1.x samplers implicitly, based on the destination reg. number. */
                if (shader_version.major == 1
                        && (ins.handler_idx == WINED3DSIH_TEX
                            || ins.handler_idx == WINED3DSIH_TEXBEM
                            || ins.handler_idx == WINED3DSIH_TEXBEML
                            || ins.handler_idx == WINED3DSIH_TEXDP3TEX
                            || ins.handler_idx == WINED3DSIH_TEXM3x2TEX
                            || ins.handler_idx == WINED3DSIH_TEXM3x3SPEC
                            || ins.handler_idx == WINED3DSIH_TEXM3x3TEX
                            || ins.handler_idx == WINED3DSIH_TEXM3x3VSPEC
                            || ins.handler_idx == WINED3DSIH_TEXREG2AR
                            || ins.handler_idx == WINED3DSIH_TEXREG2GB
                            || ins.handler_idx == WINED3DSIH_TEXREG2RGB))
                {
                    /* Fake sampler usage, only set reserved bit and type. */
                    DWORD sampler_code = dst_param.reg.idx;

                    TRACE("Setting fake 2D sampler for 1.x pixelshader.\n");
                    reg_maps->sampler_type[sampler_code] = WINED3DSTT_2D;

                    /* texbem is only valid with < 1.4 pixel shaders */
                    if (ins.handler_idx == WINED3DSIH_TEXBEM
                            || ins.handler_idx == WINED3DSIH_TEXBEML)
                    {
                        reg_maps->bumpmat |= 1 << dst_param.reg.idx;
                        if (ins.handler_idx == WINED3DSIH_TEXBEML)
                        {
                            reg_maps->luminanceparams |= 1 << dst_param.reg.idx;
                        }
                    }
                }
                else if (ins.handler_idx == WINED3DSIH_BEM)
                {
                    reg_maps->bumpmat |= 1 << dst_param.reg.idx;
                }
            }

            if (ins.handler_idx == WINED3DSIH_NRM) reg_maps->usesnrm = 1;
            else if (ins.handler_idx == WINED3DSIH_DSY) reg_maps->usesdsy = 1;
            else if (ins.handler_idx == WINED3DSIH_DSX) reg_maps->usesdsx = 1;
            else if (ins.handler_idx == WINED3DSIH_TEXLDD) reg_maps->usestexldd = 1;
            else if (ins.handler_idx == WINED3DSIH_TEXLDL) reg_maps->usestexldl = 1;
            else if (ins.handler_idx == WINED3DSIH_MOVA) reg_maps->usesmova = 1;
            else if (ins.handler_idx == WINED3DSIH_IFC) reg_maps->usesifc = 1;
            else if (ins.handler_idx == WINED3DSIH_CALL) reg_maps->usescall = 1;
771
            else if (ins.handler_idx == WINED3DSIH_POW) reg_maps->usespow = 1;
772 773 774 775 776 777 778 779 780 781
            else if (ins.handler_idx == WINED3DSIH_LOOP
                    || ins.handler_idx == WINED3DSIH_REP)
            {
                ++cur_loop_depth;
                if (cur_loop_depth > max_loop_depth)
                    max_loop_depth = cur_loop_depth;
            }
            else if (ins.handler_idx == WINED3DSIH_ENDLOOP
                    || ins.handler_idx == WINED3DSIH_ENDREP)
                --cur_loop_depth;
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

            limit = ins.src_count + (ins.predicate ? 1 : 0);
            for (i = 0; i < limit; ++i)
            {
                struct wined3d_shader_src_param src_param, src_rel_addr;
                unsigned int count;

                fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
                count = get_instr_extra_regcount(ins.handler_idx, i);

                shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
                while (count)
                {
                    ++src_param.reg.idx;
                    shader_record_register_usage(shader, reg_maps, &src_param.reg, shader_version.type);
                    --count;
                }

                if (color0_mov)
                {
                    if (src_param.reg.type == WINED3DSPR_TEMP
                            && src_param.swizzle == WINED3DSP_NOSWIZZLE)
                    {
805 806
                        shader->u.ps.color0_mov = TRUE;
                        shader->u.ps.color0_reg = src_param.reg.idx;
807 808 809 810 811 812 813
                    }
                }
            }
        }
    }
    reg_maps->loop_depth = max_loop_depth;

814 815 816 817 818
    /* PS before 2.0 don't have explicit color outputs. Instead the value of
     * R0 is written to the render target. */
    if (shader_version.major < 2 && shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
        reg_maps->rt_mask |= (1 << 0);

819
    shader->functionLength = ((const char *)ptr - (const char *)byte_code);
820 821 822 823

    return WINED3D_OK;
}

824
unsigned int shader_find_free_input_register(const struct wined3d_shader_reg_maps *reg_maps, unsigned int max)
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
{
    DWORD map = 1 << max;
    map |= map - 1;
    map &= reg_maps->shader_version.major < 3 ? ~reg_maps->texcoord : ~reg_maps->input_registers;

    return wined3d_log2i(map);
}

static void shader_dump_decl_usage(const struct wined3d_shader_semantic *semantic,
        const struct wined3d_shader_version *shader_version)
{
    TRACE("dcl");

    if (semantic->reg.reg.type == WINED3DSPR_SAMPLER)
    {
        switch (semantic->sampler_type)
        {
            case WINED3DSTT_2D: TRACE("_2d"); break;
            case WINED3DSTT_CUBE: TRACE("_cube"); break;
            case WINED3DSTT_VOLUME: TRACE("_volume"); break;
            default: TRACE("_unknown_ttype(0x%08x)", semantic->sampler_type);
        }
    }
    else
    {
        /* Pixel shaders 3.0 don't have usage semantics. */
        if (shader_version->major < 3 && shader_version->type == WINED3D_SHADER_TYPE_PIXEL) return;
        else TRACE("_");

        switch (semantic->usage)
        {
856
            case WINED3D_DECL_USAGE_POSITION:
857 858 859
                TRACE("position%u", semantic->usage_idx);
                break;

860
            case WINED3D_DECL_USAGE_BLEND_INDICES:
861 862 863
                TRACE("blend");
                break;

864
            case WINED3D_DECL_USAGE_BLEND_WEIGHT:
865 866 867
                TRACE("weight");
                break;

868
            case WINED3D_DECL_USAGE_NORMAL:
869 870 871
                TRACE("normal%u", semantic->usage_idx);
                break;

872
            case WINED3D_DECL_USAGE_PSIZE:
873 874 875
                TRACE("psize");
                break;

876
            case WINED3D_DECL_USAGE_COLOR:
877
                if (!semantic->usage_idx) TRACE("color");
878 879 880
                else TRACE("specular%u", (semantic->usage_idx - 1));
                break;

881
            case WINED3D_DECL_USAGE_TEXCOORD:
882 883 884
                TRACE("texture%u", semantic->usage_idx);
                break;

885
            case WINED3D_DECL_USAGE_TANGENT:
886 887 888
                TRACE("tangent");
                break;

889
            case WINED3D_DECL_USAGE_BINORMAL:
890 891 892
                TRACE("binormal");
                break;

893
            case WINED3D_DECL_USAGE_TESS_FACTOR:
894 895 896
                TRACE("tessfactor");
                break;

897
            case WINED3D_DECL_USAGE_POSITIONT:
898 899 900
                TRACE("positionT%u", semantic->usage_idx);
                break;

901
            case WINED3D_DECL_USAGE_FOG:
902 903 904
                TRACE("fog");
                break;

905
            case WINED3D_DECL_USAGE_DEPTH:
906 907 908
                TRACE("depth");
                break;

909
            case WINED3D_DECL_USAGE_SAMPLE:
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
                TRACE("sample");
                break;

            default:
                FIXME("unknown_semantics(0x%08x)", semantic->usage);
        }
    }
}

static void shader_dump_register(const struct wined3d_shader_register *reg,
        const struct wined3d_shader_version *shader_version)
{
    static const char * const rastout_reg_names[] = {"oPos", "oFog", "oPts"};
    static const char * const misctype_reg_names[] = {"vPos", "vFace"};
    UINT offset = reg->idx;

    switch (reg->type)
    {
        case WINED3DSPR_TEMP:
            TRACE("r");
            break;

        case WINED3DSPR_INPUT:
            TRACE("v");
            break;

        case WINED3DSPR_CONST:
        case WINED3DSPR_CONST2:
        case WINED3DSPR_CONST3:
        case WINED3DSPR_CONST4:
            TRACE("c");
            offset = shader_get_float_offset(reg->type, reg->idx);
            break;

        case WINED3DSPR_TEXTURE: /* vs: case WINED3DSPR_ADDR */
            TRACE("%c", shader_version->type == WINED3D_SHADER_TYPE_PIXEL ? 't' : 'a');
            break;

        case WINED3DSPR_RASTOUT:
            TRACE("%s", rastout_reg_names[reg->idx]);
            break;

        case WINED3DSPR_COLOROUT:
            TRACE("oC");
            break;

        case WINED3DSPR_DEPTHOUT:
            TRACE("oDepth");
            break;

        case WINED3DSPR_ATTROUT:
            TRACE("oD");
            break;

        case WINED3DSPR_TEXCRDOUT:
            /* Vertex shaders >= 3.0 use general purpose output registers
             * (WINED3DSPR_OUTPUT), which can include an address token. */
            if (shader_version->major >= 3) TRACE("o");
            else TRACE("oT");
            break;

        case WINED3DSPR_CONSTINT:
            TRACE("i");
            break;

        case WINED3DSPR_CONSTBOOL:
            TRACE("b");
            break;

        case WINED3DSPR_LABEL:
            TRACE("l");
            break;

        case WINED3DSPR_LOOP:
            TRACE("aL");
            break;

        case WINED3DSPR_SAMPLER:
            TRACE("s");
            break;

        case WINED3DSPR_MISCTYPE:
            if (reg->idx > 1) FIXME("Unhandled misctype register %u.\n", reg->idx);
            else TRACE("%s", misctype_reg_names[reg->idx]);
            break;

        case WINED3DSPR_PREDICATE:
            TRACE("p");
            break;

        case WINED3DSPR_IMMCONST:
            TRACE("l");
            break;

        case WINED3DSPR_CONSTBUFFER:
            TRACE("cb");
            break;

1008 1009 1010 1011
        case WINED3DSPR_NULL:
            TRACE("null");
            break;

1012 1013 1014 1015
        case WINED3DSPR_RESOURCE:
            TRACE("t");
            break;

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        default:
            TRACE("unhandled_rtype(%#x)", reg->type);
            break;
    }

    if (reg->type == WINED3DSPR_IMMCONST)
    {
        TRACE("(");
        switch (reg->immconst_type)
        {
1026
            case WINED3D_IMMCONST_SCALAR:
1027 1028 1029
                TRACE("%.8e", *(const float *)reg->immconst_data);
                break;

1030
            case WINED3D_IMMCONST_VEC4:
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
                TRACE("%.8e, %.8e, %.8e, %.8e",
                        *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
                        *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
                break;

            default:
                TRACE("<unhandled immconst_type %#x>", reg->immconst_type);
                break;
        }
        TRACE(")");
    }
1042 1043 1044
    else if (reg->type != WINED3DSPR_RASTOUT
            && reg->type != WINED3DSPR_MISCTYPE
            && reg->type != WINED3DSPR_NULL)
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    {
        if (reg->array_idx != ~0U)
        {
            TRACE("%u[%u", offset, reg->array_idx);
            if (reg->rel_addr)
            {
                TRACE(" + ");
                shader_dump_src_param(reg->rel_addr, shader_version);
            }
            TRACE("]");
        }
        else
        {
            if (reg->rel_addr)
            {
                TRACE("[");
                shader_dump_src_param(reg->rel_addr, shader_version);
                TRACE(" + ");
            }
            TRACE("%u", offset);
            if (reg->rel_addr) TRACE("]");
        }
    }
}

void shader_dump_dst_param(const struct wined3d_shader_dst_param *param,
        const struct wined3d_shader_version *shader_version)
{
    DWORD write_mask = param->write_mask;

    shader_dump_register(&param->reg, shader_version);

1077
    if (write_mask && write_mask != WINED3DSP_WRITEMASK_ALL)
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    {
        static const char *write_mask_chars = "xyzw";

        TRACE(".");
        if (write_mask & WINED3DSP_WRITEMASK_0) TRACE("%c", write_mask_chars[0]);
        if (write_mask & WINED3DSP_WRITEMASK_1) TRACE("%c", write_mask_chars[1]);
        if (write_mask & WINED3DSP_WRITEMASK_2) TRACE("%c", write_mask_chars[2]);
        if (write_mask & WINED3DSP_WRITEMASK_3) TRACE("%c", write_mask_chars[3]);
    }
}

void shader_dump_src_param(const struct wined3d_shader_src_param *param,
        const struct wined3d_shader_version *shader_version)
{
1092
    enum wined3d_shader_src_modifier src_modifier = param->modifiers;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    DWORD swizzle = param->swizzle;

    if (src_modifier == WINED3DSPSM_NEG
            || src_modifier == WINED3DSPSM_BIASNEG
            || src_modifier == WINED3DSPSM_SIGNNEG
            || src_modifier == WINED3DSPSM_X2NEG
            || src_modifier == WINED3DSPSM_ABSNEG)
        TRACE("-");
    else if (src_modifier == WINED3DSPSM_COMP)
        TRACE("1-");
    else if (src_modifier == WINED3DSPSM_NOT)
        TRACE("!");

    if (src_modifier == WINED3DSPSM_ABS || src_modifier == WINED3DSPSM_ABSNEG)
        TRACE("abs(");

    shader_dump_register(&param->reg, shader_version);

    if (src_modifier)
    {
        switch (src_modifier)
        {
            case WINED3DSPSM_NONE:    break;
            case WINED3DSPSM_NEG:     break;
            case WINED3DSPSM_NOT:     break;
            case WINED3DSPSM_BIAS:    TRACE("_bias"); break;
            case WINED3DSPSM_BIASNEG: TRACE("_bias"); break;
            case WINED3DSPSM_SIGN:    TRACE("_bx2"); break;
            case WINED3DSPSM_SIGNNEG: TRACE("_bx2"); break;
            case WINED3DSPSM_COMP:    break;
            case WINED3DSPSM_X2:      TRACE("_x2"); break;
            case WINED3DSPSM_X2NEG:   TRACE("_x2"); break;
            case WINED3DSPSM_DZ:      TRACE("_dz"); break;
            case WINED3DSPSM_DW:      TRACE("_dw"); break;
            case WINED3DSPSM_ABSNEG:  TRACE(")"); break;
            case WINED3DSPSM_ABS:     TRACE(")"); break;
            default:                  TRACE("_unknown_modifier(%#x)", src_modifier);
        }
    }

    if (swizzle != WINED3DSP_NOSWIZZLE)
    {
        static const char *swizzle_chars = "xyzw";
        DWORD swizzle_x = swizzle & 0x03;
        DWORD swizzle_y = (swizzle >> 2) & 0x03;
        DWORD swizzle_z = (swizzle >> 4) & 0x03;
        DWORD swizzle_w = (swizzle >> 6) & 0x03;

        if (swizzle_x == swizzle_y
                && swizzle_x == swizzle_z
                && swizzle_x == swizzle_w)
        {
            TRACE(".%c", swizzle_chars[swizzle_x]);
        }
        else
        {
            TRACE(".%c%c%c%c", swizzle_chars[swizzle_x], swizzle_chars[swizzle_y],
                    swizzle_chars[swizzle_z], swizzle_chars[swizzle_w]);
        }
    }
}

/* Shared code in order to generate the bulk of the shader string.
 * NOTE: A description of how to parse tokens can be found on MSDN. */
1157
void shader_generate_main(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
1158
        const struct wined3d_shader_reg_maps *reg_maps, const DWORD *byte_code, void *backend_ctx)
1159
{
1160
    struct wined3d_device *device = shader->device;
1161 1162
    const struct wined3d_shader_frontend *fe = shader->frontend;
    void *fe_data = shader->frontend_data;
1163
    struct wined3d_shader_src_param dst_rel_addr[2];
1164
    struct wined3d_shader_src_param src_rel_addr[4];
1165
    struct wined3d_shader_dst_param dst_param[2];
1166 1167
    struct wined3d_shader_src_param src_param[4];
    struct wined3d_shader_version shader_version;
1168
    struct wined3d_shader_loop_state loop_state;
1169
    struct wined3d_shader_instruction ins;
1170
    struct wined3d_shader_tex_mx tex_mx;
1171 1172 1173 1174 1175
    struct wined3d_shader_context ctx;
    const DWORD *ptr = byte_code;
    DWORD i;

    /* Initialize current parsing state. */
1176
    tex_mx.current_row = 0;
1177 1178
    loop_state.current_depth = 0;
    loop_state.current_reg = 0;
1179

1180
    ctx.shader = shader;
1181
    ctx.gl_info = &device->adapter->gl_info;
1182 1183
    ctx.reg_maps = reg_maps;
    ctx.buffer = buffer;
1184
    ctx.tex_mx = &tex_mx;
1185
    ctx.loop_state = &loop_state;
1186 1187 1188
    ctx.backend_data = backend_ctx;

    ins.ctx = &ctx;
1189
    ins.dst = dst_param;
1190 1191 1192 1193 1194 1195 1196
    ins.src = src_param;

    fe->shader_read_header(fe_data, &ptr, &shader_version);

    while (!fe->shader_is_end(fe_data, &ptr))
    {
        const char *comment;
1197
        UINT comment_size;
1198 1199 1200
        UINT param_size;

        /* Skip comment tokens. */
1201
        fe->shader_read_comment(&ptr, &comment, &comment_size);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        if (comment) continue;

        /* Read opcode. */
        fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);

        /* Unknown opcode and its parameters. */
        if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
        {
            TRACE("Skipping unrecognized instruction.\n");
            ptr += param_size;
            continue;
        }

        /* Nothing to do. */
        if (ins.handler_idx == WINED3DSIH_DCL
                || ins.handler_idx == WINED3DSIH_NOP
                || ins.handler_idx == WINED3DSIH_DEF
                || ins.handler_idx == WINED3DSIH_DEFI
                || ins.handler_idx == WINED3DSIH_DEFB
                || ins.handler_idx == WINED3DSIH_PHASE)
        {
            ptr += param_size;
            continue;
        }

1227 1228 1229 1230 1231
        /* Destination tokens */
        for (i = 0; i < ins.dst_count; ++i)
        {
            fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
        }
1232 1233

        /* Predication token */
1234 1235 1236 1237 1238
        if (ins.predicate)
        {
            FIXME("Predicates not implemented.\n");
            ins.predicate = *ptr++;
        }
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311

        /* Other source tokens */
        for (i = 0; i < ins.src_count; ++i)
        {
            fe->shader_read_src_param(fe_data, &ptr, &src_param[i], &src_rel_addr[i]);
        }

        /* Call appropriate function for output target */
        device->shader_backend->shader_handle_instruction(&ins);
    }
}

static void shader_dump_ins_modifiers(const struct wined3d_shader_dst_param *dst)
{
    DWORD mmask = dst->modifiers;

    switch (dst->shift)
    {
        case 0: break;
        case 13: TRACE("_d8"); break;
        case 14: TRACE("_d4"); break;
        case 15: TRACE("_d2"); break;
        case 1: TRACE("_x2"); break;
        case 2: TRACE("_x4"); break;
        case 3: TRACE("_x8"); break;
        default: TRACE("_unhandled_shift(%d)", dst->shift); break;
    }

    if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
    if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
    if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");

    mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
    if (mmask) FIXME("_unrecognized_modifier(%#x)", mmask);
}

static void shader_trace_init(const struct wined3d_shader_frontend *fe, void *fe_data, const DWORD *byte_code)
{
    struct wined3d_shader_version shader_version;
    const DWORD *ptr = byte_code;
    const char *type_prefix;
    DWORD i;

    TRACE("Parsing %p.\n", byte_code);

    fe->shader_read_header(fe_data, &ptr, &shader_version);

    switch (shader_version.type)
    {
        case WINED3D_SHADER_TYPE_VERTEX:
            type_prefix = "vs";
            break;

        case WINED3D_SHADER_TYPE_GEOMETRY:
            type_prefix = "gs";
            break;

        case WINED3D_SHADER_TYPE_PIXEL:
            type_prefix = "ps";
            break;

        default:
            FIXME("Unhandled shader type %#x.\n", shader_version.type);
            type_prefix = "unknown";
            break;
    }

    TRACE("%s_%u_%u\n", type_prefix, shader_version.major, shader_version.minor);

    while (!fe->shader_is_end(fe_data, &ptr))
    {
        struct wined3d_shader_instruction ins;
        const char *comment;
1312
        UINT comment_size;
1313 1314 1315
        UINT param_size;

        /* comment */
1316
        fe->shader_read_comment(&ptr, &comment, &comment_size);
1317 1318
        if (comment)
        {
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
            if (comment_size > 4 && *(const DWORD *)comment == WINEMAKEFOURCC('T', 'E', 'X', 'T'))
            {
                const char *end = comment + comment_size;
                const char *ptr = comment + 4;
                const char *line = ptr;

                TRACE("// TEXT\n");
                while (ptr != end)
                {
                    if (*ptr == '\n')
                    {
                        UINT len = ptr - line;
                        if (len && *(ptr - 1) == '\r') --len;
                        TRACE("// %s\n", debugstr_an(line, len));
                        line = ++ptr;
                    }
                    else ++ptr;
                }
                if (line != ptr) TRACE("// %s\n", debugstr_an(line, ptr - line));
            }
            else TRACE("// %s\n", debugstr_an(comment, comment_size));
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
            continue;
        }

        fe->shader_read_opcode(fe_data, &ptr, &ins, &param_size);
        if (ins.handler_idx == WINED3DSIH_TABLE_SIZE)
        {
            TRACE("Skipping unrecognized instruction.\n");
            ptr += param_size;
            continue;
        }

        if (ins.handler_idx == WINED3DSIH_DCL)
        {
            struct wined3d_shader_semantic semantic;

            fe->shader_read_semantic(&ptr, &semantic);

            shader_dump_decl_usage(&semantic, &shader_version);
            shader_dump_ins_modifiers(&semantic.reg);
            TRACE(" ");
            shader_dump_dst_param(&semantic.reg, &shader_version);
        }
        else if (ins.handler_idx == WINED3DSIH_DEF)
        {
            struct wined3d_shader_src_param rel_addr;
            struct wined3d_shader_dst_param dst;

            fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);

            TRACE("def c%u = %f, %f, %f, %f", shader_get_float_offset(dst.reg.type, dst.reg.idx),
                    *(const float *)(ptr),
                    *(const float *)(ptr + 1),
                    *(const float *)(ptr + 2),
                    *(const float *)(ptr + 3));
            ptr += 4;
        }
        else if (ins.handler_idx == WINED3DSIH_DEFI)
        {
            struct wined3d_shader_src_param rel_addr;
            struct wined3d_shader_dst_param dst;

            fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);

            TRACE("defi i%u = %d, %d, %d, %d", dst.reg.idx,
                    *(ptr),
                    *(ptr + 1),
                    *(ptr + 2),
                    *(ptr + 3));
            ptr += 4;
        }
        else if (ins.handler_idx == WINED3DSIH_DEFB)
        {
            struct wined3d_shader_src_param rel_addr;
            struct wined3d_shader_dst_param dst;

            fe->shader_read_dst_param(fe_data, &ptr, &dst, &rel_addr);

            TRACE("defb b%u = %s", dst.reg.idx, *ptr ? "true" : "false");
            ++ptr;
        }
        else
        {
1402 1403 1404
            struct wined3d_shader_src_param dst_rel_addr[2];
            struct wined3d_shader_src_param src_rel_addr;
            struct wined3d_shader_dst_param dst_param[2];
1405 1406
            struct wined3d_shader_src_param src_param;

1407
            for (i = 0; i < ins.dst_count; ++i)
1408
            {
1409
                fe->shader_read_dst_param(fe_data, &ptr, &dst_param[i], &dst_rel_addr[i]);
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
            }

            /* Print out predication source token first - it follows
             * the destination token. */
            if (ins.predicate)
            {
                fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
                TRACE("(");
                shader_dump_src_param(&src_param, &shader_version);
                TRACE(") ");
            }

            /* PixWin marks instructions with the coissue flag with a '+' */
            if (ins.coissue) TRACE("+");

            TRACE("%s", shader_opcode_names[ins.handler_idx]);

            if (ins.handler_idx == WINED3DSIH_IFC
                    || ins.handler_idx == WINED3DSIH_BREAKC)
            {
                switch (ins.flags)
                {
1432 1433 1434 1435 1436 1437
                    case WINED3D_SHADER_REL_OP_GT: TRACE("_gt"); break;
                    case WINED3D_SHADER_REL_OP_EQ: TRACE("_eq"); break;
                    case WINED3D_SHADER_REL_OP_GE: TRACE("_ge"); break;
                    case WINED3D_SHADER_REL_OP_LT: TRACE("_lt"); break;
                    case WINED3D_SHADER_REL_OP_NE: TRACE("_ne"); break;
                    case WINED3D_SHADER_REL_OP_LE: TRACE("_le"); break;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
                    default: TRACE("_(%u)", ins.flags);
                }
            }
            else if (ins.handler_idx == WINED3DSIH_TEX
                    && shader_version.major >= 2
                    && (ins.flags & WINED3DSI_TEXLD_PROJECT))
            {
                TRACE("p");
            }

1448 1449
            /* We already read the destination tokens, print them. */
            for (i = 0; i < ins.dst_count; ++i)
1450
            {
1451 1452 1453
                shader_dump_ins_modifiers(&dst_param[i]);
                TRACE(!i ? " " : ", ");
                shader_dump_dst_param(&dst_param[i], &shader_version);
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
            }

            /* Other source tokens */
            for (i = ins.dst_count; i < (ins.dst_count + ins.src_count); ++i)
            {
                fe->shader_read_src_param(fe_data, &ptr, &src_param, &src_rel_addr);
                TRACE(!i ? " " : ", ");
                shader_dump_src_param(&src_param, &shader_version);
            }
        }
        TRACE("\n");
    }
}

1468
static void shader_cleanup(struct wined3d_shader *shader)
1469
{
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
    shader->device->shader_backend->shader_destroy(shader);
    HeapFree(GetProcessHeap(), 0, shader->reg_maps.constf);
    HeapFree(GetProcessHeap(), 0, shader->function);
    shader_delete_constant_list(&shader->constantsF);
    shader_delete_constant_list(&shader->constantsB);
    shader_delete_constant_list(&shader->constantsI);
    list_remove(&shader->shader_list_entry);

    if (shader->frontend && shader->frontend_data)
        shader->frontend->shader_free(shader->frontend_data);
1480 1481 1482 1483
}

static void shader_none_handle_instruction(const struct wined3d_shader_instruction *ins) {}
static void shader_none_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS) {}
1484 1485
static void shader_none_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
        enum tex_types tex_type, const SIZE *ds_mask_size) {}
1486
static void shader_none_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info) {}
1487 1488
static void shader_none_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count) {}
static void shader_none_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count) {}
1489
static void shader_none_load_constants(const struct wined3d_context *context, char usePS, char useVS) {}
1490 1491
static void shader_none_load_np2fixup_constants(void *shader_priv,
        const struct wined3d_gl_info *gl_info, const struct wined3d_state *state) {}
1492
static void shader_none_destroy(struct wined3d_shader *shader) {}
1493 1494
static HRESULT shader_none_alloc(struct wined3d_device *device) {return WINED3D_OK;}
static void shader_none_free(struct wined3d_device *device) {}
1495
static void shader_none_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
1496

1497
static void shader_none_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
1498 1499 1500
{
    /* Set the shader caps to 0 for the none shader backend */
    caps->VertexShaderVersion = 0;
1501
    caps->MaxVertexShaderConst = 0;
1502 1503
    caps->PixelShaderVersion = 0;
    caps->PixelShader1xMaxValue = 0.0f;
1504 1505
    caps->MaxPixelShaderConst = 0;
    caps->VSClipping = FALSE;
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
}

static BOOL shader_none_color_fixup_supported(struct color_fixup_desc fixup)
{
    if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
    {
        TRACE("Checking support for fixup:\n");
        dump_color_fixup_desc(fixup);
    }

    /* Faked to make some apps happy. */
1517
    if (!is_complex_fixup(fixup))
1518 1519 1520 1521 1522 1523 1524 1525 1526
    {
        TRACE("[OK]\n");
        return TRUE;
    }

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

1527 1528
const struct wined3d_shader_backend_ops none_shader_backend =
{
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
    shader_none_handle_instruction,
    shader_none_select,
    shader_none_select_depth_blt,
    shader_none_deselect_depth_blt,
    shader_none_update_float_vertex_constants,
    shader_none_update_float_pixel_constants,
    shader_none_load_constants,
    shader_none_load_np2fixup_constants,
    shader_none_destroy,
    shader_none_alloc,
    shader_none_free,
1540
    shader_none_context_destroyed,
1541 1542 1543
    shader_none_get_caps,
    shader_none_color_fixup_supported,
};
1544

1545
static HRESULT shader_set_function(struct wined3d_shader *shader, const DWORD *byte_code,
1546 1547
        const struct wined3d_shader_signature *output_signature, DWORD float_const_count,
        enum wined3d_shader_type type, unsigned int max_version)
1548
{
1549
    struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1550 1551
    const struct wined3d_shader_frontend *fe;
    HRESULT hr;
1552
    unsigned int backend_version;
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562

    TRACE("shader %p, byte_code %p, output_signature %p, float_const_count %u.\n",
            shader, byte_code, output_signature, float_const_count);

    fe = shader_select_frontend(*byte_code);
    if (!fe)
    {
        FIXME("Unable to find frontend for shader.\n");
        return WINED3DERR_INVALIDCALL;
    }
1563 1564 1565
    shader->frontend = fe;
    shader->frontend_data = fe->shader_init(byte_code, output_signature);
    if (!shader->frontend_data)
1566 1567 1568 1569 1570 1571
    {
        FIXME("Failed to initialize frontend.\n");
        return WINED3DERR_INVALIDCALL;
    }

    /* First pass: trace shader. */
1572 1573
    if (TRACE_ON(d3d_shader))
        shader_trace_init(fe, shader->frontend_data, byte_code);
1574 1575

    /* Initialize immediate constant lists. */
1576 1577 1578
    list_init(&shader->constantsF);
    list_init(&shader->constantsB);
    list_init(&shader->constantsI);
1579 1580

    /* Second pass: figure out which registers are used, what the semantics are, etc. */
1581
    hr = shader_get_registers_used(shader, fe,
1582
            reg_maps, shader->input_signature, shader->output_signature,
1583 1584 1585
            byte_code, float_const_count);
    if (FAILED(hr)) return hr;

1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    if (reg_maps->shader_version.type != type)
    {
        WARN("Wrong shader type %d.\n", reg_maps->shader_version.type);
        return WINED3DERR_INVALIDCALL;
    }
    if (reg_maps->shader_version.major > max_version)
    {
        WARN("Shader version %d not supported by this D3D API version.\n", reg_maps->shader_version.major);
        return WINED3DERR_INVALIDCALL;
    }
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    switch (type)
    {
        case WINED3D_SHADER_TYPE_VERTEX:
            backend_version = shader->device->vshader_version;
            break;
        case WINED3D_SHADER_TYPE_PIXEL:
            backend_version = shader->device->pshader_version;
            break;
        default:
            FIXME("No backend version-checking for this shader type\n");
            backend_version = 0;
    }
    if (reg_maps->shader_version.major > backend_version)
    {
        WARN("Shader version %d.%d not supported by your GPU with the current shader backend.\n",
                reg_maps->shader_version.major, reg_maps->shader_version.minor);
        return WINED3DERR_INVALIDCALL;
    }
1614

1615 1616 1617 1618
    shader->function = HeapAlloc(GetProcessHeap(), 0, shader->functionLength);
    if (!shader->function)
        return E_OUTOFMEMORY;
    memcpy(shader->function, byte_code, shader->functionLength);
1619 1620 1621 1622

    return WINED3D_OK;
}

1623
ULONG CDECL wined3d_shader_incref(struct wined3d_shader *shader)
1624
{
1625
    ULONG refcount = InterlockedIncrement(&shader->ref);
1626 1627 1628 1629 1630 1631

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

    return refcount;
}

1632
/* Do not call while under the GL lock. */
1633
ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
1634
{
1635
    ULONG refcount = InterlockedDecrement(&shader->ref);
1636 1637 1638 1639 1640

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

    if (!refcount)
    {
1641
        shader_cleanup(shader);
1642
        shader->parent_ops->wined3d_object_destroyed(shader->parent);
1643 1644 1645 1646 1647 1648
        HeapFree(GetProcessHeap(), 0, shader);
    }

    return refcount;
}

1649
void * CDECL wined3d_shader_get_parent(const struct wined3d_shader *shader)
1650
{
1651
    TRACE("shader %p.\n", shader);
1652

1653
    return shader->parent;
1654 1655
}

1656 1657
HRESULT CDECL wined3d_shader_get_byte_code(const struct wined3d_shader *shader,
        void *byte_code, UINT *byte_code_size)
1658
{
1659
    TRACE("shader %p, byte_code %p, byte_code_size %p.\n", shader, byte_code, byte_code_size);
1660

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
    if (!byte_code)
    {
        *byte_code_size = shader->functionLength;
        return WINED3D_OK;
    }

    if (*byte_code_size < shader->functionLength)
    {
        /* MSDN claims (for d3d8 at least) that if *byte_code_size is smaller
         * than the required size we should write the required size and
         * return D3DERR_MOREDATA. That's not actually true. */
        return WINED3DERR_INVALIDCALL;
    }

    memcpy(byte_code, shader->function, shader->functionLength);

    return WINED3D_OK;
1678 1679
}

1680 1681
/* Set local constants for d3d8 shaders. */
HRESULT CDECL wined3d_shader_set_local_constants_float(struct wined3d_shader *shader,
1682 1683
        UINT start_idx, const float *src_data, UINT count)
{
1684 1685
    UINT end_idx = start_idx + count;
    UINT i;
1686

1687
    TRACE("shader %p, start_idx %u, src_data %p, count %u.\n", shader, start_idx, src_data, count);
1688

1689 1690 1691 1692 1693 1694 1695 1696 1697
    if (end_idx > shader->limits.constant_float)
    {
        WARN("end_idx %u > float constants limit %u.\n",
                end_idx, shader->limits.constant_float);
        end_idx = shader->limits.constant_float;
    }

    for (i = start_idx; i < end_idx; ++i)
    {
1698
        struct wined3d_shader_lconst *lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(*lconst));
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
        if (!lconst)
            return E_OUTOFMEMORY;

        lconst->idx = i;
        memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
        list_add_head(&shader->constantsF, &lconst->entry);
    }

    return WINED3D_OK;
}
1709

1710
void find_vs_compile_args(const struct wined3d_state *state,
1711
        const struct wined3d_shader *shader, struct vs_compile_args *args)
1712
{
1713
    args->fog_src = state->render_states[WINED3D_RS_FOGTABLEMODE]
1714
            == WINED3D_FOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
1715 1716
    args->clip_enabled = state->render_states[WINED3D_RS_CLIPPING]
            && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
1717
    args->swizzle_map = shader->device->strided_streams.swizzle_map;
1718 1719 1720 1721
}

static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2)
{
1722 1723 1724 1725 1726 1727 1728 1729
    if (usage_idx1 != usage_idx2)
        return FALSE;
    if (usage1 == usage2)
        return TRUE;
    if (usage1 == WINED3D_DECL_USAGE_POSITION && usage2 == WINED3D_DECL_USAGE_POSITIONT)
        return TRUE;
    if (usage2 == WINED3D_DECL_USAGE_POSITION && usage1 == WINED3D_DECL_USAGE_POSITIONT)
        return TRUE;
1730 1731 1732 1733

    return FALSE;
}

1734
BOOL vshader_get_input(const struct wined3d_shader *shader,
1735
        BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
1736
{
1737
    WORD map = shader->reg_maps.input_registers;
1738 1739 1740 1741 1742 1743
    unsigned int i;

    for (i = 0; map; map >>= 1, ++i)
    {
        if (!(map & 1)) continue;

1744 1745
        if (match_usage(shader->u.vs.attributes[i].usage,
                shader->u.vs.attributes[i].usage_idx, usage_req, usage_idx_req))
1746 1747 1748 1749 1750 1751 1752 1753
        {
            *regnum = i;
            return TRUE;
        }
    }
    return FALSE;
}

1754
static void vertexshader_set_limits(struct wined3d_shader *shader)
1755
{
1756 1757
    DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
            shader->reg_maps.shader_version.minor);
1758
    struct wined3d_device *device = shader->device;
1759

1760 1761 1762
    shader->limits.texcoord = 0;
    shader->limits.attributes = 16;
    shader->limits.packed_input = 0;
1763 1764 1765 1766 1767

    switch (shader_version)
    {
        case WINED3D_SHADER_VERSION(1, 0):
        case WINED3D_SHADER_VERSION(1, 1):
1768 1769 1770 1771 1772 1773 1774
            shader->limits.temporary = 12;
            shader->limits.constant_bool = 0;
            shader->limits.constant_int = 0;
            shader->limits.address = 1;
            shader->limits.packed_output = 12;
            shader->limits.sampler = 0;
            shader->limits.label = 0;
1775 1776 1777
            /* TODO: vs_1_1 has a minimum of 96 constants. What happens when
             * a vs_1_1 shader is used on a vs_3_0 capable card that has 256
             * constants? */
1778
            shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1779 1780 1781 1782
            break;

        case WINED3D_SHADER_VERSION(2, 0):
        case WINED3D_SHADER_VERSION(2, 1):
1783 1784 1785 1786 1787 1788 1789 1790
            shader->limits.temporary = 12;
            shader->limits.constant_bool = 16;
            shader->limits.constant_int = 16;
            shader->limits.address = 1;
            shader->limits.packed_output = 12;
            shader->limits.sampler = 0;
            shader->limits.label = 16;
            shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1791 1792 1793 1794 1795 1796 1797
            break;

        case WINED3D_SHADER_VERSION(4, 0):
            FIXME("Using 3.0 limits for 4.0 shader.\n");
            /* Fall through. */

        case WINED3D_SHADER_VERSION(3, 0):
1798 1799 1800 1801 1802 1803 1804
            shader->limits.temporary = 32;
            shader->limits.constant_bool = 32;
            shader->limits.constant_int = 32;
            shader->limits.address = 1;
            shader->limits.packed_output = 12;
            shader->limits.sampler = 4;
            shader->limits.label = 16; /* FIXME: 2048 */
1805 1806 1807 1808 1809
            /* DX10 cards on Windows advertise a d3d9 constant limit of 256
             * even though they are capable of supporting much more (GL
             * drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
             * wined3d-advertised maximum. Clamp the constant limit for <= 3.0
             * shaders to 256. */
1810
            shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1811 1812 1813
            break;

        default:
1814 1815 1816 1817 1818 1819 1820 1821
            shader->limits.temporary = 12;
            shader->limits.constant_bool = 16;
            shader->limits.constant_int = 16;
            shader->limits.address = 1;
            shader->limits.packed_output = 12;
            shader->limits.sampler = 0;
            shader->limits.label = 16;
            shader->limits.constant_float = min(256, device->d3d_vshader_constantF);
1822
            FIXME("Unrecognized vertex shader version \"%u.%u\".\n",
1823 1824
                    shader->reg_maps.shader_version.major,
                    shader->reg_maps.shader_version.minor);
1825 1826 1827
    }
}

1828
static HRESULT vertexshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1829
        const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1830
        void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1831
{
1832
    struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1833 1834
    unsigned int i;
    HRESULT hr;
1835
    WORD map;
1836

1837
    if (!byte_code) return WINED3DERR_INVALIDCALL;
1838

1839
    shader_init(shader, device, parent, parent_ops);
1840 1841
    hr = shader_set_function(shader, byte_code, output_signature, device->d3d_vshader_constantF,
            WINED3D_SHADER_TYPE_VERTEX, max_version);
1842
    if (FAILED(hr))
1843
    {
1844
        WARN("Failed to set function, hr %#x.\n", hr);
1845
        shader_cleanup(shader);
1846
        return hr;
1847 1848
    }

1849
    map = reg_maps->input_registers;
1850 1851
    for (i = 0; map; map >>= 1, ++i)
    {
1852 1853
        if (!(map & 1) || !shader->input_signature[i].semantic_name)
            continue;
1854

1855
        shader->u.vs.attributes[i].usage =
1856 1857
                shader_usage_from_semantic_name(shader->input_signature[i].semantic_name);
        shader->u.vs.attributes[i].usage_idx = shader->input_signature[i].semantic_idx;
1858 1859
    }

1860 1861 1862 1863 1864 1865
    if (output_signature)
    {
        for (i = 0; i < output_signature->element_count; ++i)
        {
            struct wined3d_shader_signature_element *e = &output_signature->elements[i];
            reg_maps->output_registers |= 1 << e->register_idx;
1866
            shader->output_signature[e->register_idx] = *e;
1867 1868 1869 1870 1871
        }
    }

    vertexshader_set_limits(shader);

1872 1873
    shader->load_local_constsF = reg_maps->usesrelconstF
            && !list_empty(&shader->constantsF);
1874 1875 1876 1877

    return WINED3D_OK;
}

1878
static HRESULT geometryshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
1879
        const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
1880
        void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
1881 1882 1883
{
    HRESULT hr;

1884
    shader_init(shader, device, parent, parent_ops);
1885 1886
    hr = shader_set_function(shader, byte_code, output_signature, 0,
            WINED3D_SHADER_TYPE_GEOMETRY, max_version);
1887 1888 1889
    if (FAILED(hr))
    {
        WARN("Failed to set function, hr %#x.\n", hr);
1890
        shader_cleanup(shader);
1891 1892 1893
        return hr;
    }

1894
    shader->load_local_constsF = FALSE;
1895 1896 1897 1898

    return WINED3D_OK;
}

1899
void find_ps_compile_args(const struct wined3d_state *state,
1900
        const struct wined3d_shader *shader, struct ps_compile_args *args)
1901
{
1902
    struct wined3d_device *device = shader->device;
1903
    const struct wined3d_texture *texture;
1904 1905 1906
    UINT i;

    memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set. */
1907
    if (state->render_states[WINED3D_RS_SRGBWRITEENABLE])
1908
    {
1909
        const struct wined3d_surface *rt = state->fb->render_targets[0];
1910
        if (rt->resource.format->flags & WINED3DFMT_FLAG_SRGB_WRITE) args->srgb_correction = 1;
1911 1912
    }

1913 1914
    if (shader->reg_maps.shader_version.major == 1
            && shader->reg_maps.shader_version.minor <= 3)
1915 1916 1917
    {
        for (i = 0; i < 4; ++i)
        {
1918
            DWORD flags = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
1919

1920
            if (flags & WINED3D_TTFF_PROJECTED)
1921
            {
1922
                DWORD tex_transform = flags & ~WINED3D_TTFF_PROJECTED;
1923 1924 1925 1926

                if (!state->vertex_shader)
                {
                    unsigned int j;
1927
                    unsigned int index = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
1928 1929 1930
                    DWORD max_valid = WINED3D_TTFF_COUNT4;
                    enum wined3d_sampler_texture_type sampler_type = shader->reg_maps.sampler_type[i];

1931 1932 1933 1934 1935
                    for (j = 0; j < state->vertex_declaration->element_count; ++j)
                    {
                        struct wined3d_vertex_declaration_element *element =
                                &state->vertex_declaration->elements[j];

1936
                        if (element->usage == WINED3D_DECL_USAGE_TEXCOORD
1937 1938 1939 1940 1941 1942
                                && element->usage_idx == index)
                        {
                            max_valid = element->format->component_count;
                            break;
                        }
                    }
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
                    if (!tex_transform || tex_transform > max_valid)
                    {
                        WARN("Fixing up projected texture transform flags from %#x to %#x.\n",
                                tex_transform, max_valid);
                        tex_transform = max_valid;
                    }
                    if ((sampler_type == WINED3DSTT_1D && tex_transform > WINED3D_TTFF_COUNT1)
                            || (sampler_type == WINED3DSTT_2D && tex_transform > WINED3D_TTFF_COUNT2)
                            || (sampler_type == WINED3DSTT_VOLUME && tex_transform > WINED3D_TTFF_COUNT3))
                        tex_transform |= WINED3D_PSARGS_PROJECTED;
                    else
                    {
                        WARN("Application requested projected texture with unsuitable texture coordinates.\n");
                        WARN("(texture unit %u, transform flags %#x, sampler type %u).\n",
                                i, tex_transform, sampler_type);
                    }
1959
                }
1960
                else
1961
                    tex_transform = WINED3D_TTFF_COUNT4 | WINED3D_PSARGS_PROJECTED;
1962 1963

                args->tex_transform |= tex_transform << i * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
1964
            }
1965 1966
        }
    }
1967 1968 1969

    for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
    {
1970 1971 1972
        if (!shader->reg_maps.sampler_type[i])
            continue;

1973
        texture = state->textures[i];
1974 1975 1976 1977 1978
        if (!texture)
        {
            args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
            continue;
        }
1979
        args->color_fixup[i] = texture->resource.format->color_fixup;
1980

1981
        if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
1982 1983
            args->shadow |= 1 << i;

1984
        /* Flag samplers that need NP2 texcoord fixup. */
1985
        if (!(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
1986 1987
            args->np2_fixup |= (1 << i);
    }
1988
    if (shader->reg_maps.shader_version.major >= 3)
1989
    {
1990
        if (device->strided_streams.position_transformed)
1991 1992 1993
        {
            args->vp_mode = pretransformed;
        }
1994
        else if (use_vs(state))
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
        {
            args->vp_mode = vertexshader;
        }
        else
        {
            args->vp_mode = fixedfunction;
        }
        args->fog = FOG_OFF;
    }
    else
    {
        args->vp_mode = vertexshader;
2007
        if (state->render_states[WINED3D_RS_FOGENABLE])
2008
        {
2009
            switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
2010
            {
2011
                case WINED3D_FOG_NONE:
2012
                    if (device->strided_streams.position_transformed || use_vs(state))
2013 2014 2015 2016 2017
                    {
                        args->fog = FOG_LINEAR;
                        break;
                    }

2018
                    switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
2019
                    {
2020 2021 2022 2023
                        case WINED3D_FOG_NONE: /* Fall through. */
                        case WINED3D_FOG_LINEAR: args->fog = FOG_LINEAR; break;
                        case WINED3D_FOG_EXP:    args->fog = FOG_EXP;    break;
                        case WINED3D_FOG_EXP2:   args->fog = FOG_EXP2;   break;
2024 2025 2026
                    }
                    break;

2027 2028 2029
                case WINED3D_FOG_LINEAR: args->fog = FOG_LINEAR; break;
                case WINED3D_FOG_EXP:    args->fog = FOG_EXP;    break;
                case WINED3D_FOG_EXP2:   args->fog = FOG_EXP2;   break;
2030 2031 2032 2033 2034 2035 2036 2037 2038
            }
        }
        else
        {
            args->fog = FOG_OFF;
        }
    }
}

2039
static void pixelshader_set_limits(struct wined3d_shader *shader)
2040
{
2041 2042
    DWORD shader_version = WINED3D_SHADER_VERSION(shader->reg_maps.shader_version.major,
            shader->reg_maps.shader_version.minor);
2043

2044 2045 2046
    shader->limits.attributes = 0;
    shader->limits.address = 0;
    shader->limits.packed_output = 0;
2047 2048 2049 2050 2051 2052 2053

    switch (shader_version)
    {
        case WINED3D_SHADER_VERSION(1, 0):
        case WINED3D_SHADER_VERSION(1, 1):
        case WINED3D_SHADER_VERSION(1, 2):
        case WINED3D_SHADER_VERSION(1, 3):
2054 2055 2056 2057 2058 2059 2060 2061
            shader->limits.temporary = 2;
            shader->limits.constant_float = 8;
            shader->limits.constant_int = 0;
            shader->limits.constant_bool = 0;
            shader->limits.texcoord = 4;
            shader->limits.sampler = 4;
            shader->limits.packed_input = 0;
            shader->limits.label = 0;
2062 2063 2064
            break;

        case WINED3D_SHADER_VERSION(1, 4):
2065 2066 2067 2068 2069 2070 2071 2072
            shader->limits.temporary = 6;
            shader->limits.constant_float = 8;
            shader->limits.constant_int = 0;
            shader->limits.constant_bool = 0;
            shader->limits.texcoord = 6;
            shader->limits.sampler = 6;
            shader->limits.packed_input = 0;
            shader->limits.label = 0;
2073 2074 2075 2076
            break;

        /* FIXME: Temporaries must match D3DPSHADERCAPS2_0.NumTemps. */
        case WINED3D_SHADER_VERSION(2, 0):
2077 2078 2079 2080 2081 2082 2083
            shader->limits.temporary = 32;
            shader->limits.constant_float = 32;
            shader->limits.constant_int = 16;
            shader->limits.constant_bool = 16;
            shader->limits.texcoord = 8;
            shader->limits.sampler = 16;
            shader->limits.packed_input = 0;
2084 2085 2086
            break;

        case WINED3D_SHADER_VERSION(2, 1):
2087 2088 2089 2090 2091 2092 2093 2094
            shader->limits.temporary = 32;
            shader->limits.constant_float = 32;
            shader->limits.constant_int = 16;
            shader->limits.constant_bool = 16;
            shader->limits.texcoord = 8;
            shader->limits.sampler = 16;
            shader->limits.packed_input = 0;
            shader->limits.label = 16;
2095 2096 2097 2098 2099 2100 2101
            break;

        case WINED3D_SHADER_VERSION(4, 0):
            FIXME("Using 3.0 limits for 4.0 shader.\n");
            /* Fall through. */

        case WINED3D_SHADER_VERSION(3, 0):
2102 2103 2104 2105 2106 2107 2108 2109
            shader->limits.temporary = 32;
            shader->limits.constant_float = 224;
            shader->limits.constant_int = 16;
            shader->limits.constant_bool = 16;
            shader->limits.texcoord = 0;
            shader->limits.sampler = 16;
            shader->limits.packed_input = 12;
            shader->limits.label = 16; /* FIXME: 2048 */
2110 2111 2112
            break;

        default:
2113 2114 2115 2116 2117 2118 2119 2120
            shader->limits.temporary = 32;
            shader->limits.constant_float = 32;
            shader->limits.constant_int = 16;
            shader->limits.constant_bool = 16;
            shader->limits.texcoord = 8;
            shader->limits.sampler = 16;
            shader->limits.packed_input = 0;
            shader->limits.label = 0;
2121
            FIXME("Unrecognized pixel shader version %u.%u\n",
2122 2123
                    shader->reg_maps.shader_version.major,
                    shader->reg_maps.shader_version.minor);
2124 2125 2126
    }
}

2127
static HRESULT pixelshader_init(struct wined3d_shader *shader, struct wined3d_device *device,
2128
        const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
2129
        void *parent, const struct wined3d_parent_ops *parent_ops, unsigned int max_version)
2130 2131 2132 2133 2134
{
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
    unsigned int i, highest_reg_used = 0, num_regs_used = 0;
    HRESULT hr;

2135
    if (!byte_code) return WINED3DERR_INVALIDCALL;
2136

2137
    shader_init(shader, device, parent, parent_ops);
2138 2139
    hr = shader_set_function(shader, byte_code, output_signature, device->d3d_pshader_constantF,
            WINED3D_SHADER_TYPE_PIXEL, max_version);
2140
    if (FAILED(hr))
2141
    {
2142
        WARN("Failed to set function, hr %#x.\n", hr);
2143
        shader_cleanup(shader);
2144
        return hr;
2145 2146 2147 2148 2149 2150
    }

    pixelshader_set_limits(shader);

    for (i = 0; i < MAX_REG_INPUT; ++i)
    {
2151
        if (shader->u.ps.input_reg_used[i])
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
        {
            ++num_regs_used;
            highest_reg_used = i;
        }
    }

    /* Don't do any register mapping magic if it is not needed, or if we can't
     * achieve anything anyway */
    if (highest_reg_used < (gl_info->limits.glsl_varyings / 4)
            || num_regs_used > (gl_info->limits.glsl_varyings / 4))
    {
        if (num_regs_used > (gl_info->limits.glsl_varyings / 4))
        {
            /* This happens with relative addressing. The input mapper function
             * warns about this if the higher registers are declared too, so
             * don't write a FIXME here */
            WARN("More varying registers used than supported\n");
        }

        for (i = 0; i < MAX_REG_INPUT; ++i)
        {
2173
            shader->u.ps.input_reg_map[i] = i;
2174 2175
        }

2176
        shader->u.ps.declared_in_count = highest_reg_used + 1;
2177 2178 2179
    }
    else
    {
2180
        shader->u.ps.declared_in_count = 0;
2181 2182
        for (i = 0; i < MAX_REG_INPUT; ++i)
        {
2183 2184 2185
            if (shader->u.ps.input_reg_used[i])
                shader->u.ps.input_reg_map[i] = shader->u.ps.declared_in_count++;
            else shader->u.ps.input_reg_map[i] = ~0U;
2186 2187 2188
        }
    }

2189
    shader->load_local_constsF = FALSE;
2190 2191 2192 2193

    return WINED3D_OK;
}

2194
void pixelshader_update_samplers(struct wined3d_shader_reg_maps *reg_maps, struct wined3d_texture * const *textures)
2195
{
2196
    enum wined3d_sampler_texture_type *sampler_type = reg_maps->sampler_type;
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
    unsigned int i;

    if (reg_maps->shader_version.major != 1) return;

    for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
    {
        /* We don't sample from this sampler. */
        if (!sampler_type[i]) continue;

        if (!textures[i])
        {
            WARN("No texture bound to sampler %u, using 2D.\n", i);
            sampler_type[i] = WINED3DSTT_2D;
            continue;
        }

2213
        switch (textures[i]->target)
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
        {
            case GL_TEXTURE_RECTANGLE_ARB:
            case GL_TEXTURE_2D:
                /* We have to select between texture rectangles and 2D
                 * textures later because 2.0 and 3.0 shaders only have
                 * WINED3DSTT_2D as well. */
                sampler_type[i] = WINED3DSTT_2D;
                break;

            case GL_TEXTURE_3D:
                sampler_type[i] = WINED3DSTT_VOLUME;
                break;

            case GL_TEXTURE_CUBE_MAP_ARB:
                sampler_type[i] = WINED3DSTT_CUBE;
                break;

            default:
2232
                FIXME("Unrecognized texture type %#x, using 2D.\n", textures[i]->target);
2233 2234 2235 2236
                sampler_type[i] = WINED3DSTT_2D;
        }
    }
}
2237

2238
HRESULT CDECL wined3d_shader_create_gs(struct wined3d_device *device, const DWORD *byte_code,
2239
        const struct wined3d_shader_signature *output_signature, void *parent,
2240
        const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
{
    struct wined3d_shader *object;
    HRESULT hr;

    TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
            device, byte_code, output_signature, parent, parent_ops, shader);

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate shader memory.\n");
        return E_OUTOFMEMORY;
    }

2255
    hr = geometryshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
    if (FAILED(hr))
    {
        WARN("Failed to initialize geometry shader, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created geometry shader %p.\n", object);
    *shader = object;

    return WINED3D_OK;
}

2269
HRESULT CDECL wined3d_shader_create_ps(struct wined3d_device *device, const DWORD *byte_code,
2270
        const struct wined3d_shader_signature *output_signature, void *parent,
2271
        const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
{
    struct wined3d_shader *object;
    HRESULT hr;

    TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
            device, byte_code, output_signature, parent, parent_ops, shader);

    if (device->ps_selected_mode == SHADER_NONE)
        return WINED3DERR_INVALIDCALL;

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate shader memory.\n");
        return E_OUTOFMEMORY;
    }

2289
    hr = pixelshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
    if (FAILED(hr))
    {
        WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created pixel shader %p.\n", object);
    *shader = object;

    return WINED3D_OK;
}

2303
HRESULT CDECL wined3d_shader_create_vs(struct wined3d_device *device, const DWORD *byte_code,
2304
        const struct wined3d_shader_signature *output_signature, void *parent,
2305
        const struct wined3d_parent_ops *parent_ops, struct wined3d_shader **shader, unsigned int max_version)
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
{
    struct wined3d_shader *object;
    HRESULT hr;

    TRACE("device %p, byte_code %p, output_signature %p, parent %p, parent_ops %p, shader %p.\n",
            device, byte_code, output_signature, parent, parent_ops, shader);

    if (device->vs_selected_mode == SHADER_NONE)
        return WINED3DERR_INVALIDCALL;

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate shader memory.\n");
        return E_OUTOFMEMORY;
    }

2323
    hr = vertexshader_init(object, device, byte_code, output_signature, parent, parent_ops, max_version);
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
    if (FAILED(hr))
    {
        WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created vertex shader %p.\n", object);
    *shader = object;

    return WINED3D_OK;
}