baseshader.c 39.7 KB
Newer Older
1 2 3 4 5
/*
 * shaders implementation
 *
 * Copyright 2002-2003 Jason Edmeades
 * Copyright 2002-2003 Raphael Junqueira
6
 * Copyright 2004 Christian Costa
7
 * Copyright 2005 Oliver Stieber
8
 * Copyright 2006 Ivan Gyurdiev
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24 25 26 27 28 29 30 31
 */

#include "config.h"
#include <string.h>
#include <stdio.h>
#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);

32
#define GLNAME_REQUIRE_GLSL  ((const char *)1)
33

34
static inline BOOL shader_is_version_token(DWORD token) {
35 36
    return shader_is_pshader_version(token) ||
           shader_is_vshader_version(token);
37 38
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
int shader_addline(
    SHADER_BUFFER* buffer,  
    const char *format, ...) {

    char* base = buffer->buffer + buffer->bsize;
    int rc;

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

    if (rc < 0 ||                                   /* C89 */ 
        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;
    }

60 61 62 63
    if (buffer->newline) {
        TRACE("GL HW (%u, %u) : %s", buffer->lineNo + 1, buffer->bsize, base);
        buffer->newline = FALSE;
    } else {
H. Verbeet's avatar
H. Verbeet committed
64
        TRACE("%s", base);
65 66
    }

67
    buffer->bsize += rc;
68 69 70 71
    if (buffer->buffer[buffer->bsize-1] == '\n') {
        buffer->lineNo++;
        buffer->newline = TRUE;
    }
72 73 74
    return 0;
}

75 76 77 78 79 80 81 82 83 84 85
const SHADER_OPCODE* shader_get_opcode(
    IWineD3DBaseShader *iface, const DWORD code) {

    IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl*) iface;

    DWORD i = 0;
    DWORD hex_version = This->baseShader.hex_version;
    const SHADER_OPCODE *shader_ins = This->baseShader.shader_ins;

    /** TODO: use dichotomic search */
    while (NULL != shader_ins[i].name) {
86
        if (((code & WINED3DSI_OPCODE_MASK) == shader_ins[i].opcode) &&
87 88 89 90 91 92
            (((hex_version >= shader_ins[i].min_version) && (hex_version <= shader_ins[i].max_version)) ||
            ((shader_ins[i].min_version == 0) && (shader_ins[i].max_version == 0)))) {
            return &shader_ins[i];
        }
        ++i;
    }
93
    FIXME("Unsupported opcode %#x(%d) masked %#x, shader version %#x\n", 
94
       code, code, code & WINED3DSI_OPCODE_MASK, hex_version);
95 96 97
    return NULL;
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/* Read a parameter opcode from the input stream,
 * and possibly a relative addressing token.
 * Return the number of tokens read */
int shader_get_param(
    IWineD3DBaseShader* iface,
    const DWORD* pToken,
    DWORD* param,
    DWORD* addr_token) {

    /* PS >= 3.0 have relative addressing (with token)
     * VS >= 2.0 have relative addressing (with token)
     * VS >= 1.0 < 2.0 have relative addressing (without token)
     * The version check below should work in general */

    IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
113
    char rel_token = WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2 &&
114
        ((*pToken & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
115 116 117 118 119 120

    *param = *pToken;
    *addr_token = rel_token? *(pToken + 1): 0;
    return rel_token? 2:1;
}

121 122 123 124 125 126 127 128 129
/* Return the number of parameters to skip for an opcode */
static inline int shader_skip_opcode(
    IWineD3DBaseShaderImpl* This,
    const SHADER_OPCODE* curOpcode,
    DWORD opcode_token) {

   /* Shaders >= 2.0 may contain address tokens, but fortunately they
    * have a useful legnth mask - use it here. Shaders 1.0 contain no such tokens */

130
    return (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)?
131
        ((opcode_token & WINED3DSI_INSTLENGTH_MASK) >> WINED3DSI_INSTLENGTH_SHIFT):
132 133 134
        curOpcode->num_params;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* Read the parameters of an unrecognized opcode from the input stream
 * Return the number of tokens read. 
 * 
 * Note: This function assumes source or destination token format.
 * It will not work with specially-formatted tokens like DEF or DCL, 
 * but hopefully those would be recognized */

int shader_skip_unrecognized(
    IWineD3DBaseShader* iface,
    const DWORD* pToken) {

    int tokens_read = 0;
    int i = 0;

    /* TODO: Think of a good name for 0x80000000 and replace it with a constant */
    while (*pToken & 0x80000000) {

        DWORD param, addr_token;
        tokens_read += shader_get_param(iface, pToken, &param, &addr_token);
        pToken += tokens_read;

156 157
        FIXME("Unrecognized opcode param: token=%08x "
            "addr_token=%08x name=", param, addr_token);
158
        shader_dump_param(iface, param, addr_token, i);
159 160 161 162 163 164
        FIXME("\n");
        ++i;
    }
    return tokens_read;
}

165 166 167 168 169
/* Convert floating point offset relative
 * to a register file to an absolute offset for float constants */

unsigned int shader_get_float_offset(const DWORD reg) {

170
     unsigned int regnum = reg & WINED3DSP_REGNUM_MASK;
171 172 173
     int regtype = shader_get_regtype(reg);

     switch (regtype) {
174 175 176 177
        case WINED3DSPR_CONST: return regnum;
        case WINED3DSPR_CONST2: return 2048 + regnum;
        case WINED3DSPR_CONST3: return 4096 + regnum;
        case WINED3DSPR_CONST4: return 6144 + regnum;
178 179 180 181 182 183
        default:
            FIXME("Unsupported register type: %d\n", regtype);
            return regnum;
     }
}

184 185
/* Note that this does not count the loop register
 * as an address register. */
186

187
HRESULT shader_get_registers_used(
188
    IWineD3DBaseShader *iface,
189
    shader_reg_maps* reg_maps,
190 191
    semantic* semantics_in,
    semantic* semantics_out,
192 193
    CONST DWORD* pToken,
    IWineD3DStateBlockImpl *stateBlock) {
194 195

    IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
196
    unsigned int cur_loop_depth = 0, max_loop_depth = 0;
197

198 199 200
    /* There are some minor differences between pixel and vertex shaders */
    char pshader = shader_is_pshader_version(This->baseShader.hex_version);

201
    reg_maps->bumpmat = -1;
202
    reg_maps->luminanceparams = -1;
203

204
    if (pToken == NULL)
205
        return WINED3D_OK;
206

207
    while (WINED3DVS_END() != *pToken) {
208
        CONST SHADER_OPCODE* curOpcode;
209
        DWORD opcode_token;
210 211 212 213 214 215 216

        /* Skip version */
        if (shader_is_version_token(*pToken)) {
             ++pToken;
             continue;

        /* Skip comments */
217
        } else if (shader_is_comment(*pToken)) {
218
             DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
219 220 221 222 223 224
             ++pToken;
             pToken += comment_len;
             continue;
        }

        /* Fetch opcode */
225 226
        opcode_token = *pToken++;
        curOpcode = shader_get_opcode(iface, opcode_token);
227 228 229 230 231 232

        /* Unhandled opcode, and its parameters */
        if (NULL == curOpcode) {
           while (*pToken & 0x80000000)
               ++pToken;

233
        /* Handle declarations */
234
        } else if (WINED3DSIO_DCL == curOpcode->opcode) {
235 236 237 238

            DWORD usage = *pToken++;
            DWORD param = *pToken++;
            DWORD regtype = shader_get_regtype(param);
239
            unsigned int regnum = param & WINED3DSP_REGNUM_MASK;
240

241 242
            /* Vshader: mark attributes used
               Pshader: mark 3.0 input registers used, save token */
243
            if (WINED3DSPR_INPUT == regtype) {
244 245 246

                if (!pshader)
                    reg_maps->attributes[regnum] = 1;
247 248
                else
                    reg_maps->packed_input[regnum] = 1;
249

250 251
                semantics_in[regnum].usage = usage;
                semantics_in[regnum].reg = param;
252

253
            /* Vshader: mark 3.0 output registers used, save token */
254
            } else if (WINED3DSPR_OUTPUT == regtype) {
255
                reg_maps->packed_output[regnum] = 1;
256 257
                semantics_out[regnum].usage = usage;
                semantics_out[regnum].reg = param;
258 259
                if (usage & (WINED3DDECLUSAGE_FOG << WINED3DSP_DCL_USAGE_SHIFT))
                    reg_maps->fog = 1;
260

261
            /* Save sampler usage token */
262
            } else if (WINED3DSPR_SAMPLER == regtype)
263
                reg_maps->samplers[regnum] = usage;
264

265
        } else if (WINED3DSIO_DEF == curOpcode->opcode) {
266 267 268

            local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
            if (!lconst) return E_OUTOFMEMORY;
269
            lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
270
            memcpy(&lconst->value, pToken + 1, 4 * sizeof(DWORD));
271 272 273 274 275 276 277 278 279 280 281 282 283 284

            /* In pixel shader 1.X shaders, the constants are clamped between [-1;1] */
            if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && pshader) {
                float *value = (float *) lconst->value;
                if(value[0] < -1.0) value[0] = -1.0;
                else if(value[0] >  1.0) value[0] =  1.0;
                if(value[1] < -1.0) value[1] = -1.0;
                else if(value[1] >  1.0) value[1] =  1.0;
                if(value[2] < -1.0) value[2] = -1.0;
                else if(value[2] >  1.0) value[2] =  1.0;
                if(value[3] < -1.0) value[3] = -1.0;
                else if(value[3] >  1.0) value[3] =  1.0;
            }

285 286 287
            list_add_head(&This->baseShader.constantsF, &lconst->entry);
            pToken += curOpcode->num_params;

288
        } else if (WINED3DSIO_DEFI == curOpcode->opcode) {
289 290 291

            local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
            if (!lconst) return E_OUTOFMEMORY;
292
            lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
293 294 295 296
            memcpy(&lconst->value, pToken + 1, 4 * sizeof(DWORD));
            list_add_head(&This->baseShader.constantsI, &lconst->entry);
            pToken += curOpcode->num_params;

297
        } else if (WINED3DSIO_DEFB == curOpcode->opcode) {
298 299 300

            local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
            if (!lconst) return E_OUTOFMEMORY;
301
            lconst->idx = *pToken & WINED3DSP_REGNUM_MASK;
302 303
            memcpy(&lconst->value, pToken + 1, 1 * sizeof(DWORD));
            list_add_head(&This->baseShader.constantsB, &lconst->entry);
304 305
            pToken += curOpcode->num_params;

306
        /* If there's a loop in the shader */
307 308
        } else if (WINED3DSIO_LOOP == curOpcode->opcode ||
                   WINED3DSIO_REP == curOpcode->opcode) {
309 310 311
            cur_loop_depth++;
            if(cur_loop_depth > max_loop_depth)
                max_loop_depth = cur_loop_depth;
312
            pToken += curOpcode->num_params;
313 314 315 316 317

        } else if (WINED3DSIO_ENDLOOP == curOpcode->opcode ||
                   WINED3DSIO_ENDREP == curOpcode->opcode) {
            cur_loop_depth--;

318
        /* For subroutine prototypes */
319
        } else if (WINED3DSIO_LABEL == curOpcode->opcode) {
320

321
            DWORD snum = *pToken & WINED3DSP_REGNUM_MASK; 
322 323
            reg_maps->labels[snum] = 1;
            pToken += curOpcode->num_params;
324

325
        /* Set texture, address, temporary registers */
326
        } else {
327
            int i, limit;
328

329
            /* Declare 1.X samplers implicitly, based on the destination reg. number */
330
            if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 && 
331 332
                (WINED3DSIO_TEX == curOpcode->opcode ||
                 WINED3DSIO_TEXBEM == curOpcode->opcode ||
333 334
                 WINED3DSIO_TEXBEML == curOpcode->opcode ||
                 WINED3DSIO_TEXDP3TEX == curOpcode->opcode ||
335
                 WINED3DSIO_TEXM3x2TEX == curOpcode->opcode ||
336 337 338 339 340 341
                 WINED3DSIO_TEXM3x3SPEC == curOpcode->opcode ||
                 WINED3DSIO_TEXM3x3TEX == curOpcode->opcode ||
                 WINED3DSIO_TEXM3x3VSPEC == curOpcode->opcode ||
                 WINED3DSIO_TEXREG2AR == curOpcode->opcode ||
                 WINED3DSIO_TEXREG2GB == curOpcode->opcode ||
                 WINED3DSIO_TEXREG2RGB == curOpcode->opcode)) {
342 343

                /* Fake sampler usage, only set reserved bit and ttype */
344
                DWORD sampler_code = *pToken & WINED3DSP_REGNUM_MASK;
345 346

                if(!stateBlock->textures[sampler_code]) {
347
                    ERR("No texture bound to sampler %d\n", sampler_code);
348 349 350 351 352 353 354 355 356 357 358 359
                    reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
                } else {
                    int texType = IWineD3DBaseTexture_GetTextureDimensions(stateBlock->textures[sampler_code]);
                    switch(texType) {
                        case GL_TEXTURE_2D:
                            reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
                            break;

                        case GL_TEXTURE_3D:
                            reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_VOLUME;
                            break;

360
                        case GL_TEXTURE_CUBE_MAP_ARB:
361 362 363 364 365 366 367 368
                            reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_CUBE;
                            break;

                        default:
                            ERR("Unexpected gl texture type found: %d\n", texType);
                            reg_maps->samplers[sampler_code] = (0x1 << 31) | WINED3DSTT_2D;
                    }
                }
369 370

                /* texbem is only valid with < 1.4 pixel shaders */
371 372
                if(WINED3DSIO_TEXBEM  == curOpcode->opcode ||
                    WINED3DSIO_TEXBEML == curOpcode->opcode) {
373
                    if(reg_maps->bumpmat != -1 && reg_maps->bumpmat != sampler_code) {
374 375 376
                        FIXME("Pixel shader uses texbem instruction on more than 1 sampler\n");
                    } else {
                        reg_maps->bumpmat = sampler_code;
377 378 379
                        if(WINED3DSIO_TEXBEML == curOpcode->opcode) {
                            reg_maps->luminanceparams = sampler_code;
                        }
380 381
                    }
                }
382
            }
383 384
            if(WINED3DSIO_NRM  == curOpcode->opcode) {
                reg_maps->usesnrm = 1;
385 386 387 388 389 390 391
            } else if(WINED3DSIO_BEM == curOpcode->opcode) {
                DWORD regnum = *pToken & WINED3DSP_REGNUM_MASK;
                if(reg_maps->bumpmat != -1 && reg_maps->bumpmat != regnum) {
                    FIXME("Pixel shader uses bem or texbem instruction on more than 1 sampler\n");
                } else {
                    reg_maps->bumpmat = regnum;
                }
392 393
            } else if(WINED3DSIO_DSY  == curOpcode->opcode) {
                reg_maps->usesdsy = 1;
394
            }
395

396 397 398 399 400 401 402
            /* 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) */

403
            limit = (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED)?
404 405 406
                curOpcode->num_params + 1: curOpcode->num_params;

            for (i = 0; i < limit; ++i) {
407 408 409 410

                DWORD param, addr_token, reg, regtype;
                pToken += shader_get_param(iface, pToken, &param, &addr_token);

411
                regtype = shader_get_regtype(param);
412
                reg = param & WINED3DSP_REGNUM_MASK;
413

414
                if (WINED3DSPR_TEXTURE == regtype) { /* vs: WINED3DSPR_ADDR */
415

416 417
                    if (pshader)
                        reg_maps->texcoord[reg] = 1;
418
                    else
419
                        reg_maps->address[reg] = 1;
420 421
                }

422
                else if (WINED3DSPR_TEMP == regtype)
423 424
                    reg_maps->temporary[reg] = 1;

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
                else if (WINED3DSPR_INPUT == regtype) {
                    if( !pshader)
                        reg_maps->attributes[reg] = 1;
                    else {
                        if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
                            /* 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++) {
                                ((IWineD3DPixelShaderImpl *) This)->input_reg_used[i] = TRUE;
                            }
                        } else {
                            ((IWineD3DPixelShaderImpl *) This)->input_reg_used[reg] = TRUE;
                        }
                    }
                }
443

444
                else if (WINED3DSPR_RASTOUT == regtype && reg == 1)
445
                    reg_maps->fog = 1;
446 447 448

                else if (WINED3DSPR_MISCTYPE == regtype && reg == 0 && pshader)
                    reg_maps->vpos = 1;
449 450 451 452 453 454 455 456 457

                else if(WINED3DSPR_CONST == regtype && !pshader &&
                        param & WINED3DSHADER_ADDRMODE_RELATIVE) {
                    if(reg <= ((IWineD3DVertexShaderImpl *) This)->min_rel_offset) {
                        ((IWineD3DVertexShaderImpl *) This)->min_rel_offset = reg;
                    } else if(reg >= ((IWineD3DVertexShaderImpl *) This)->max_rel_offset) {
                        ((IWineD3DVertexShaderImpl *) This)->max_rel_offset = reg;
                    }
                }
458
            }
459 460
        }
    }
461
    reg_maps->loop_depth = max_loop_depth;
462 463

    return WINED3D_OK;
464 465
}

466
static void shader_dump_decl_usage(
467
    IWineD3DBaseShaderImpl* This,
468 469 470 471
    DWORD decl, 
    DWORD param) {

    DWORD regtype = shader_get_regtype(param);
472 473

    TRACE("dcl");
474

475
    if (regtype == WINED3DSPR_SAMPLER) {
476
        DWORD ttype = decl & WINED3DSP_TEXTURETYPE_MASK;
477 478

        switch (ttype) {
479 480 481
            case WINED3DSTT_2D: TRACE("_2d"); break;
            case WINED3DSTT_CUBE: TRACE("_cube"); break;
            case WINED3DSTT_VOLUME: TRACE("_volume"); break;
482
            default: TRACE("_unknown_ttype(%08x)", ttype); 
483 484 485 486
       }

    } else { 

487 488
        DWORD usage = decl & WINED3DSP_DCL_USAGE_MASK;
        DWORD idx = (decl & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
489

490 491
        /* Pixel shaders 3.0 don't have usage semantics */
        char pshader = shader_is_pshader_version(This->baseShader.hex_version);
492
        if (pshader && This->baseShader.hex_version < WINED3DPS_VERSION(3,0))
493 494 495 496
            return;
        else
            TRACE("_");

497
        switch(usage) {
498
        case WINED3DDECLUSAGE_POSITION:
499
            TRACE("position%d", idx);
500
            break;
501
        case WINED3DDECLUSAGE_BLENDINDICES:
502
            TRACE("blend");
503
            break;
504
        case WINED3DDECLUSAGE_BLENDWEIGHT:
505
            TRACE("weight");
506
            break;
507
        case WINED3DDECLUSAGE_NORMAL:
508
            TRACE("normal%d", idx);
509
            break;
510
        case WINED3DDECLUSAGE_PSIZE:
511
            TRACE("psize");
512
            break;
513
        case WINED3DDECLUSAGE_COLOR:
514
            if(idx == 0)  {
515
                TRACE("color");
516
            } else {
517
                TRACE("specular%d", (idx - 1));
518 519
            }
            break;
520
        case WINED3DDECLUSAGE_TEXCOORD:
521
            TRACE("texture%d", idx);
522
            break;
523
        case WINED3DDECLUSAGE_TANGENT:
524
            TRACE("tangent");
525
            break;
526
        case WINED3DDECLUSAGE_BINORMAL:
527
            TRACE("binormal");
528
            break;
529
        case WINED3DDECLUSAGE_TESSFACTOR:
530
            TRACE("tessfactor");
531
            break;
532
        case WINED3DDECLUSAGE_POSITIONT:
533
            TRACE("positionT%d", idx);
534
            break;
535
        case WINED3DDECLUSAGE_FOG:
536
            TRACE("fog");
537
            break;
538
        case WINED3DDECLUSAGE_DEPTH:
539
            TRACE("depth");
540
            break;
541
        case WINED3DDECLUSAGE_SAMPLE:
542
            TRACE("sample");
543 544
            break;
        default:
545
            FIXME("unknown_semantics(%08x)", usage);
546 547 548 549
        }
    }
}

550 551 552 553
static void shader_dump_arr_entry(
    IWineD3DBaseShader *iface,
    const DWORD param,
    const DWORD addr_token,
554
    unsigned int reg,
555 556 557
    int input) {

    char relative =
558
        ((param & WINED3DSHADER_ADDRESSMODE_MASK) == WINED3DSHADER_ADDRMODE_RELATIVE);
559 560

    if (relative) {
561
        TRACE("[");
562 563 564 565 566 567
        if (addr_token)
            shader_dump_param(iface, addr_token, 0, input);
        else
            TRACE("a0.x");
        TRACE(" + ");
     }
568 569 570
     TRACE("%u", reg);
     if (relative)
         TRACE("]");
571 572
}

573 574 575
void shader_dump_param(
    IWineD3DBaseShader *iface,
    const DWORD param, 
576
    const DWORD addr_token,
577 578 579
    int input) {

    IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
580
    static const char * const rastout_reg_names[] = { "oPos", "oFog", "oPts" };
581
    static const char * const misctype_reg_names[] = { "vPos", "vFace"};
582 583
    char swizzle_reg_chars[4];

584
    DWORD reg = param & WINED3DSP_REGNUM_MASK;
585
    DWORD regtype = shader_get_regtype(param);
586
    DWORD modifier = param & WINED3DSP_SRCMOD_MASK;
587 588

    /* There are some minor differences between pixel and vertex shaders */
589
    char pshader = shader_is_pshader_version(This->baseShader.hex_version);
590 591 592 593 594 595 596 597 598 599

    /* For one, we'd prefer color components to be shown for pshaders.
     * FIXME: use the swizzle function for this */

    swizzle_reg_chars[0] = pshader? 'r': 'x';
    swizzle_reg_chars[1] = pshader? 'g': 'y';
    swizzle_reg_chars[2] = pshader? 'b': 'z';
    swizzle_reg_chars[3] = pshader? 'a': 'w';

    if (input) {
600 601 602 603 604
        if ( (modifier == WINED3DSPSM_NEG) ||
             (modifier == WINED3DSPSM_BIASNEG) ||
             (modifier == WINED3DSPSM_SIGNNEG) ||
             (modifier == WINED3DSPSM_X2NEG) ||
             (modifier == WINED3DSPSM_ABSNEG) )
605
            TRACE("-");
606
        else if (modifier == WINED3DSPSM_COMP)
607
            TRACE("1-");
608
        else if (modifier == WINED3DSPSM_NOT)
609
            TRACE("!");
610

611
        if (modifier == WINED3DSPSM_ABS || modifier == WINED3DSPSM_ABSNEG) 
612
            TRACE("abs(");
613 614 615
    }

    switch (regtype) {
616
        case WINED3DSPR_TEMP:
617
            TRACE("r%u", reg);
618
            break;
619
        case WINED3DSPR_INPUT:
620 621
            TRACE("v");
            shader_dump_arr_entry(iface, param, addr_token, reg, input);
622
            break;
623 624 625 626
        case WINED3DSPR_CONST:
        case WINED3DSPR_CONST2:
        case WINED3DSPR_CONST3:
        case WINED3DSPR_CONST4:
627
            TRACE("c");
628
            shader_dump_arr_entry(iface, param, addr_token, shader_get_float_offset(param), input);
629
            break;
630
        case WINED3DSPR_TEXTURE: /* vs: case D3DSPR_ADDR */
631
            TRACE("%c%u", (pshader? 't':'a'), reg);
632
            break;        
633
        case WINED3DSPR_RASTOUT:
634 635
            TRACE("%s", rastout_reg_names[reg]);
            break;
636
        case WINED3DSPR_COLOROUT:
637
            TRACE("oC%u", reg);
638
            break;
639
        case WINED3DSPR_DEPTHOUT:
640 641
            TRACE("oDepth");
            break;
642
        case WINED3DSPR_ATTROUT:
643
            TRACE("oD%u", reg);
644
            break;
645
        case WINED3DSPR_TEXCRDOUT: 
646 647

            /* Vertex shaders >= 3.0 use general purpose output registers
648
             * (WINED3DSPR_OUTPUT), which can include an address token */
649

650
            if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
651
                TRACE("o");
652
                shader_dump_arr_entry(iface, param, addr_token, reg, input);
653 654
            }
            else 
655
               TRACE("oT%u", reg);
656
            break;
657
        case WINED3DSPR_CONSTINT:
658
            TRACE("i");
659
            shader_dump_arr_entry(iface, param, addr_token, reg, input);
660
            break;
661
        case WINED3DSPR_CONSTBOOL:
662
            TRACE("b");
663
            shader_dump_arr_entry(iface, param, addr_token, reg, input);
664
            break;
665
        case WINED3DSPR_LABEL:
666
            TRACE("l%u", reg);
667
            break;
668
        case WINED3DSPR_LOOP:
669
            TRACE("aL");
670
            break;
671
        case WINED3DSPR_SAMPLER:
672
            TRACE("s%u", reg);
673
            break;
674 675 676 677 678 679 680
        case WINED3DSPR_MISCTYPE:
            if (reg > 1) {
                FIXME("Unhandled misctype register %d\n", reg);
            } else {
                TRACE("%s", misctype_reg_names[reg]);
            }
            break;
681
        case WINED3DSPR_PREDICATE:
682
            TRACE("p%u", reg);
683
            break;
684
        default:
685
            TRACE("unhandled_rtype(%#x)", regtype);
686 687 688 689 690 691
            break;
   }

   if (!input) {
       /* operand output (for modifiers and shift, see dump_ins_modifiers) */

692
       if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
693
           TRACE(".");
694 695 696 697
           if (param & WINED3DSP_WRITEMASK_0) TRACE("%c", swizzle_reg_chars[0]);
           if (param & WINED3DSP_WRITEMASK_1) TRACE("%c", swizzle_reg_chars[1]);
           if (param & WINED3DSP_WRITEMASK_2) TRACE("%c", swizzle_reg_chars[2]);
           if (param & WINED3DSP_WRITEMASK_3) TRACE("%c", swizzle_reg_chars[3]);
698 699 700 701
       }

   } else {
        /** operand input */
702
        DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
703 704 705 706 707
        DWORD swizzle_r = swizzle & 0x03;
        DWORD swizzle_g = (swizzle >> 2) & 0x03;
        DWORD swizzle_b = (swizzle >> 4) & 0x03;
        DWORD swizzle_a = (swizzle >> 6) & 0x03;

708 709
        if (0 != modifier) {
            switch (modifier) {
710 711 712 713 714 715 716 717 718 719 720 721 722 723
                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;
724
                default:
725
                    TRACE("_unknown_modifier(%#x)", modifier >> WINED3DSP_SRCMOD_SHIFT);
726 727 728 729 730 731 732
            }
        }

        /**
        * swizzle bits fields:
        *  RRGGBBAA
        */
733
        if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) != swizzle) {
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
            if (swizzle_r == swizzle_g &&
                swizzle_r == swizzle_b &&
                swizzle_r == swizzle_a) {
                    TRACE(".%c", swizzle_reg_chars[swizzle_r]);
            } else {
                TRACE(".%c%c%c%c",
                swizzle_reg_chars[swizzle_r],
                swizzle_reg_chars[swizzle_g],
                swizzle_reg_chars[swizzle_b],
                swizzle_reg_chars[swizzle_a]);
            }
        }
    }
}

749 750 751 752 753
/** Shared code in order to generate the bulk of the shader string.
    Use the shader_header_fct & shader_footer_fct to add strings
    that are specific to pixel or vertex functions
    NOTE: A description of how to parse tokens can be found at:
          http://msdn.microsoft.com/library/default.asp?url=/library/en-us/graphics/hh/graphics/usermodedisplaydriver_shader_cc8e4e05-f5c3-4ec0-8853-8ce07c1551b2.xml.asp */
754
void shader_generate_main(
755 756
    IWineD3DBaseShader *iface,
    SHADER_BUFFER* buffer,
757
    shader_reg_maps* reg_maps,
758 759 760
    CONST DWORD* pFunction) {

    IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
761
    IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device; /* To access shader backend callbacks */
762 763
    const DWORD *pToken = pFunction;
    const SHADER_OPCODE *curOpcode = NULL;
764
    SHADER_HANDLER hw_fct = NULL;
765
    DWORD i;
766 767
    SHADER_OPCODE_ARG hw_arg;

768
    /* Initialize current parsing state */
769 770
    hw_arg.shader = iface;
    hw_arg.buffer = buffer;
771
    hw_arg.reg_maps = reg_maps;
772 773 774 775
    This->baseShader.parse_state.current_row = 0;

    /* Second pass, process opcodes */
    if (NULL != pToken) {
776
        while (WINED3DPS_END() != *pToken) {
777 778 779 780 781 782 783 784

            /* Skip version token */
            if (shader_is_version_token(*pToken)) {
                ++pToken;
                continue;
            }

            /* Skip comment tokens */
785
            if (shader_is_comment(*pToken)) {
786
                DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
787
                ++pToken;
788
                TRACE("#%s\n", (const char*)pToken);
789 790 791 792 793
                pToken += comment_len;
                continue;
            }

            /* Read opcode */
794 795
            hw_arg.opcode_token = *pToken++;
            curOpcode = shader_get_opcode(iface, hw_arg.opcode_token);
796 797 798 799 800 801 802 803

            /* Select handler */
            if (curOpcode == NULL)
                hw_fct = NULL;
            else if (This->baseShader.shader_mode == SHADER_GLSL)
                hw_fct = curOpcode->hw_glsl_fct;
            else if (This->baseShader.shader_mode == SHADER_ARB)
                hw_fct = curOpcode->hw_fct;
804 805

            /* Unknown opcode and its parameters */
806
            if (NULL == curOpcode) {
807
                FIXME("Unrecognized opcode: token=%08x\n", hw_arg.opcode_token);
808
                pToken += shader_skip_unrecognized(iface, pToken); 
809

810
            /* Nothing to do */
811 812 813 814 815 816 817
            } else if (WINED3DSIO_DCL == curOpcode->opcode ||
                       WINED3DSIO_NOP == curOpcode->opcode ||
                       WINED3DSIO_DEF == curOpcode->opcode ||
                       WINED3DSIO_DEFI == curOpcode->opcode ||
                       WINED3DSIO_DEFB == curOpcode->opcode ||
                       WINED3DSIO_PHASE == curOpcode->opcode ||
                       WINED3DSIO_RET == curOpcode->opcode) {
818

819
                pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
820

821
            /* If a generator function is set for current shader target, use it */
822
            } else if (hw_fct != NULL) {
823 824

                hw_arg.opcode = curOpcode;
825

826 827
                /* Destination token */
                if (curOpcode->dst_token) {
828

829
                    DWORD param, addr_token = 0;
830
                    pToken += shader_get_param(iface, pToken, &param, &addr_token);
831 832
                    hw_arg.dst = param;
                    hw_arg.dst_addr = addr_token;
833
                }
834

835
                /* Predication token */
836
                if (hw_arg.opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) 
837
                    hw_arg.predicate = *pToken++;
838

839
                /* Other source tokens */
840
                for (i = 0; i < (curOpcode->num_params - curOpcode->dst_token); i++) {
841

842
                    DWORD param, addr_token = 0; 
843
                    pToken += shader_get_param(iface, pToken, &param, &addr_token);
844 845
                    hw_arg.src[i] = param;
                    hw_arg.src_addr[i] = addr_token;
846 847
                }

848
                /* Call appropriate function for output target */
849
                hw_fct(&hw_arg);
850

851 852 853
                /* Add color correction if needed */
                device->shader_backend->shader_color_correction(&hw_arg);

854
                /* Process instruction modifiers for GLSL apps ( _sat, etc. ) */
855
                if (This->baseShader.shader_mode == SHADER_GLSL)
856 857
                    shader_glsl_add_instruction_modifiers(&hw_arg);

858
            /* Unhandled opcode */
859 860
            } else {

861
                FIXME("Can't handle opcode %s in hwShader\n", curOpcode->name);
862
                pToken += shader_skip_opcode(This, curOpcode, hw_arg.opcode_token);
863 864 865 866 867 868 869
            }
        }
        /* TODO: What about result.depth? */

    }
}

870 871
void shader_dump_ins_modifiers(const DWORD output) {

872 873
    DWORD shift = (output & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
    DWORD mmask = output & WINED3DSP_DSTMOD_MASK;
874 875 876 877 878 879 880 881 882

    switch (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;
883
        default: TRACE("_unhandled_shift(%d)", shift); break;
884 885
    }

886 887 888
    if (mmask & WINED3DSPDM_SATURATE)         TRACE("_sat");
    if (mmask & WINED3DSPDM_PARTIALPRECISION) TRACE("_pp");
    if (mmask & WINED3DSPDM_MSAMPCENTROID)    TRACE("_centroid");
889

890
    mmask &= ~(WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_MSAMPCENTROID);
891
    if (mmask)
892
        FIXME("_unrecognized_modifier(%#x)", mmask >> WINED3DSP_DSTMOD_SHIFT);
893 894
}

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
/* First pass: trace shader, initialize length and version */
void shader_trace_init(
    IWineD3DBaseShader *iface,
    const DWORD* pFunction) {

    IWineD3DBaseShaderImpl *This =(IWineD3DBaseShaderImpl *)iface;

    const DWORD* pToken = pFunction;
    const SHADER_OPCODE* curOpcode = NULL;
    DWORD opcode_token;
    unsigned int len = 0;
    DWORD i;

    TRACE("(%p) : Parsing programme\n", This);

    if (NULL != pToken) {
911
        while (WINED3DVS_END() != *pToken) {
912 913
            if (shader_is_version_token(*pToken)) { /** version */
                This->baseShader.hex_version = *pToken;
914
                TRACE("%s_%u_%u\n", shader_is_pshader_version(This->baseShader.hex_version)? "ps": "vs",
915 916
                    WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version),
                    WINED3DSHADER_VERSION_MINOR(This->baseShader.hex_version));
917 918 919 920 921
                ++pToken;
                ++len;
                continue;
            }
            if (shader_is_comment(*pToken)) { /** comment */
922
                DWORD comment_len = (*pToken & WINED3DSI_COMMENTSIZE_MASK) >> WINED3DSI_COMMENTSIZE_SHIFT;
923
                ++pToken;
924
                TRACE("//%s\n", (const char*)pToken);
925 926 927 928 929 930 931 932 933 934
                pToken += comment_len;
                len += comment_len + 1;
                continue;
            }
            opcode_token = *pToken++;
            curOpcode = shader_get_opcode(iface, opcode_token);
            len++;

            if (NULL == curOpcode) {
                int tokens_read;
935
                FIXME("Unrecognized opcode: token=%08x\n", opcode_token);
936 937 938 939 940
                tokens_read = shader_skip_unrecognized(iface, pToken);
                pToken += tokens_read;
                len += tokens_read;

            } else {
941
                if (curOpcode->opcode == WINED3DSIO_DCL) {
942 943 944 945

                    DWORD usage = *pToken;
                    DWORD param = *(pToken + 1);

946
                    shader_dump_decl_usage(This, usage, param);
947 948 949 950 951 952
                    shader_dump_ins_modifiers(param);
                    TRACE(" ");
                    shader_dump_param(iface, param, 0, 0);
                    pToken += 2;
                    len += 2;

953
                } else if (curOpcode->opcode == WINED3DSIO_DEF) {
954 955 956 957

                        unsigned int offset = shader_get_float_offset(*pToken);

                        TRACE("def c%u = %f, %f, %f, %f", offset,
958 959 960 961
                            *(const float *)(pToken + 1),
                            *(const float *)(pToken + 2),
                            *(const float *)(pToken + 3),
                            *(const float *)(pToken + 4));
962 963 964

                        pToken += 5;
                        len += 5;
965
                } else if (curOpcode->opcode == WINED3DSIO_DEFI) {
966

967
                        TRACE("defi i%u = %d, %d, %d, %d", *pToken & WINED3DSP_REGNUM_MASK,
968 969 970 971
                            *(pToken + 1),
                            *(pToken + 2),
                            *(pToken + 3),
                            *(pToken + 4));
972 973 974 975

                        pToken += 5;
                        len += 5;

976
                } else if (curOpcode->opcode == WINED3DSIO_DEFB) {
977

978
                        TRACE("defb b%u = %s", *pToken & WINED3DSP_REGNUM_MASK,
979 980 981 982 983 984 985 986 987 988 989 990
                            *(pToken + 1)? "true": "false");

                        pToken += 2;
                        len += 2;

                } else {

                    DWORD param, addr_token;
                    int tokens_read;

                    /* Print out predication source token first - it follows
                     * the destination token. */
991
                    if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
992 993 994 995
                        TRACE("(");
                        shader_dump_param(iface, *(pToken + 2), 0, 1);
                        TRACE(") ");
                    }
996 997 998 999
                    if (opcode_token & WINED3DSI_COISSUE) {
                        /* PixWin marks instructions with the coissue flag with a '+' */
                        TRACE("+");
                    }
1000 1001

                    TRACE("%s", curOpcode->name);
1002

1003 1004
                    if (curOpcode->opcode == WINED3DSIO_IFC ||
                        curOpcode->opcode == WINED3DSIO_BREAKC) {
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

                        DWORD op = (opcode_token & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
                        switch (op) {
                            case COMPARISON_GT: TRACE("_gt"); break;
                            case COMPARISON_EQ: TRACE("_eq"); break;
                            case COMPARISON_GE: TRACE("_ge"); break;
                            case COMPARISON_LT: TRACE("_lt"); break;
                            case COMPARISON_NE: TRACE("_ne"); break;
                            case COMPARISON_LE: TRACE("_le"); break;
                            default:
1015
                                TRACE("_(%u)", op);
1016
                        }
1017 1018 1019
                    } else if (curOpcode->opcode == WINED3DSIO_TEX &&
                               This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
                        if(opcode_token & WINED3DSI_TEXLD_PROJECT) TRACE("p");
1020 1021
                    }

1022 1023
                    /* Destination token */
                    if (curOpcode->dst_token) {
1024 1025 1026 1027 1028 1029 1030 1031 1032

                        /* Destination token */
                        tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
                        pToken += tokens_read;
                        len += tokens_read;

                        shader_dump_ins_modifiers(param);
                        TRACE(" ");
                        shader_dump_param(iface, param, addr_token, 0);
1033 1034 1035
                    }

                    /* Predication token - already printed out, just skip it */
1036
                    if (opcode_token & WINED3DSHADER_INSTRUCTION_PREDICATED) {
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
                        pToken++;
                        len++;
                    }

                    /* Other source tokens */
                    for (i = curOpcode->dst_token; i < curOpcode->num_params; ++i) {

                        tokens_read = shader_get_param(iface, pToken, &param, &addr_token);
                        pToken += tokens_read;
                        len += tokens_read;
1047

1048 1049
                        TRACE((i == 0)? " " : ", ");
                        shader_dump_param(iface, param, addr_token, 1);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
                    }
                }
                TRACE("\n");
            }
        }
        This->baseShader.functionLength = (len + 1) * sizeof(DWORD);
    } else {
        This->baseShader.functionLength = 1; /* no Function defined use fixed function vertex processing */
    }
}

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
void shader_delete_constant_list(
    struct list* clist) {

    struct list *ptr;
    struct local_constant* constant;

    ptr = list_head(clist);
    while (ptr) {
        constant = LIST_ENTRY(ptr, struct local_constant, entry);
        ptr = list_next(clist, ptr);
        HeapFree(GetProcessHeap(), 0, constant);
    }
}
1074 1075 1076 1077

static void shader_none_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {}
static void shader_none_select_depth_blt(IWineD3DDevice *iface) {}
static void shader_none_load_constants(IWineD3DDevice *iface, char usePS, char useVS) {}
1078
static void shader_none_cleanup(IWineD3DDevice *iface) {}
1079 1080 1081 1082 1083 1084 1085

const shader_backend_t none_shader_backend = {
    &shader_none_select,
    &shader_none_select_depth_blt,
    &shader_none_load_constants,
    &shader_none_cleanup
};