2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
38 #define GLINFO_LOCATION (*gl_info)
40 /********************************************************
41 * ARB_[vertex/fragment]_program helper functions follow
42 ********************************************************/
45 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
46 * When constant_list == NULL, it will load all the constants.
48 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
49 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
51 static void shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info, GLuint target_type,
52 unsigned int max_constants, float* constants, struct list *constant_list) {
53 constants_entry *constant;
54 local_constant* lconst;
58 if (TRACE_ON(d3d_shader)) {
59 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
64 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
65 constants[i * 4 + 0], constants[i * 4 + 1],
66 constants[i * 4 + 2], constants[i * 4 + 3]);
70 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
75 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
78 checkGLcall("glProgramEnvParameter4fvARB()");
80 /* Load immediate constants */
81 if (TRACE_ON(d3d_shader)) {
82 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
83 GLfloat* values = (GLfloat*)lconst->value;
84 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
85 values[0], values[1], values[2], values[3]);
88 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
89 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
91 checkGLcall("glProgramEnvParameter4fvARB()");
95 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
97 * We only support float constants in ARB at the moment, so don't
98 * worry about the Integers or Booleans
100 void shader_arb_load_constants(
101 IWineD3DDevice* device,
103 char useVertexShader) {
105 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
106 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
107 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
109 if (useVertexShader) {
110 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
112 /* Load DirectX 9 float constants for vertex shader */
113 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
114 GL_LIMITS(vshader_constantsF),
115 stateBlock->vertexShaderConstantF,
116 &stateBlock->set_vconstantsF);
118 /* Upload the position fixup */
119 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
122 if (usePixelShader) {
124 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
126 /* Load DirectX 9 float constants for pixel shader */
127 shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
128 GL_LIMITS(pshader_constantsF),
129 stateBlock->pixelShaderConstantF,
130 &stateBlock->set_pconstantsF);
131 if(((IWineD3DPixelShaderImpl *) pshader)->bumpenvmatconst) {
132 /* needsbumpmat stores the stage number from where to load the matrix. bumpenvmatconst stores the
133 * number of the constant to load the matrix into.
134 * The state manager takes care that this function is always called if the bump env matrix changes
136 IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
137 float *data = (float *) &stateBlock->textureState[(int) psi->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
138 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst, data));
143 /* Generate the variable & register declarations for the ARB_vertex_program output target */
144 void shader_generate_arb_declarations(
145 IWineD3DBaseShader *iface,
146 shader_reg_maps* reg_maps,
147 SHADER_BUFFER* buffer,
148 WineD3D_GL_Info* gl_info) {
150 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
152 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
153 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
154 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
156 /* Temporary Output register */
157 shader_addline(buffer, "TEMP TMP_OUT;\n");
159 for(i = 0; i < This->baseShader.limits.temporary; i++) {
160 if (reg_maps->temporary[i])
161 shader_addline(buffer, "TEMP R%u;\n", i);
164 for (i = 0; i < This->baseShader.limits.address; i++) {
165 if (reg_maps->address[i])
166 shader_addline(buffer, "ADDRESS A%d;\n", i);
169 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
170 if (reg_maps->texcoord[i])
171 shader_addline(buffer,"TEMP T%u;\n", i);
174 /* Texture coordinate registers must be pre-loaded */
175 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
176 if (reg_maps->texcoord[i])
177 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
180 if(reg_maps->bumpmat /* Only a pshader can use texbem */) {
181 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
182 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
185 if(max_constantsF < GL_LIMITS(pshader_constantsF)) {
186 ((IWineD3DPixelShaderImpl *)This)->bumpenvmatconst = max_constantsF;
187 shader_addline(buffer, "PARAM bumpenvmat = program.env[%d];\n", ((IWineD3DPixelShaderImpl *)This)->bumpenvmatconst);
189 FIXME("No free constant found to load environemnt bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
193 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
194 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
195 max_constantsF, max_constantsF - 1);
198 static const char * const shift_tab[] = {
199 "dummy", /* 0 (none) */
200 "coefmul.x", /* 1 (x2) */
201 "coefmul.y", /* 2 (x4) */
202 "coefmul.z", /* 3 (x8) */
203 "coefmul.w", /* 4 (x16) */
204 "dummy", /* 5 (x32) */
205 "dummy", /* 6 (x64) */
206 "dummy", /* 7 (x128) */
207 "dummy", /* 8 (d256) */
208 "dummy", /* 9 (d128) */
209 "dummy", /* 10 (d64) */
210 "dummy", /* 11 (d32) */
211 "coefdiv.w", /* 12 (d16) */
212 "coefdiv.z", /* 13 (d8) */
213 "coefdiv.y", /* 14 (d4) */
214 "coefdiv.x" /* 15 (d2) */
217 static void shader_arb_get_write_mask(const DWORD param, char *write_mask) {
218 char *ptr = write_mask;
220 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
222 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
223 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
224 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
225 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
231 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
232 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
233 * but addressed as "rgba". To fix this we need to swap the register's x
234 * and z components. */
235 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
236 char *ptr = swizzle_str;
238 /* swizzle bits fields: wwzzyyxx */
239 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
240 DWORD swizzle_x = swizzle & 0x03;
241 DWORD swizzle_y = (swizzle >> 2) & 0x03;
242 DWORD swizzle_z = (swizzle >> 4) & 0x03;
243 DWORD swizzle_w = (swizzle >> 6) & 0x03;
245 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
246 * generate a swizzle string. Unless we need to our own swizzling. */
247 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
249 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
250 *ptr++ = swizzle_chars[swizzle_x];
252 *ptr++ = swizzle_chars[swizzle_x];
253 *ptr++ = swizzle_chars[swizzle_y];
254 *ptr++ = swizzle_chars[swizzle_z];
255 *ptr++ = swizzle_chars[swizzle_w];
262 static void pshader_get_register_name(
263 const DWORD param, char* regstr) {
265 DWORD reg = param & WINED3DSP_REGNUM_MASK;
266 DWORD regtype = shader_get_regtype(param);
269 case WINED3DSPR_TEMP:
270 sprintf(regstr, "R%u", reg);
272 case WINED3DSPR_INPUT:
274 strcpy(regstr, "fragment.color.primary");
276 strcpy(regstr, "fragment.color.secondary");
279 case WINED3DSPR_CONST:
280 sprintf(regstr, "C[%u]", reg);
282 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
283 sprintf(regstr,"T%u", reg);
285 case WINED3DSPR_COLOROUT:
287 sprintf(regstr, "result.color");
289 /* TODO: See GL_ARB_draw_buffers */
290 FIXME("Unsupported write to render target %u\n", reg);
291 sprintf(regstr, "unsupported_register");
294 case WINED3DSPR_DEPTHOUT:
295 sprintf(regstr, "result.depth");
297 case WINED3DSPR_ATTROUT:
298 sprintf(regstr, "oD[%u]", reg);
300 case WINED3DSPR_TEXCRDOUT:
301 sprintf(regstr, "oT[%u]", reg);
304 FIXME("Unhandled register name Type(%d)\n", regtype);
305 sprintf(regstr, "unrecognized_register");
310 /* TODO: merge with pixel shader */
311 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
313 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
315 /* oPos, oFog and oPts in D3D */
316 static const char * const hwrastout_reg_names[] = { "TMP_OUT", "TMP_FOG", "result.pointsize" };
318 DWORD reg = param & WINED3DSP_REGNUM_MASK;
319 DWORD regtype = shader_get_regtype(param);
321 BOOL is_color = FALSE;
323 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
324 strcat(hwLine, " -");
330 case WINED3DSPR_TEMP:
331 sprintf(tmpReg, "R%u", reg);
332 strcat(hwLine, tmpReg);
334 case WINED3DSPR_INPUT:
336 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
339 sprintf(tmpReg, "vertex.attrib[%u]", reg);
340 strcat(hwLine, tmpReg);
342 case WINED3DSPR_CONST:
343 sprintf(tmpReg, "C[%s%u]", (param & WINED3DSHADER_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
344 strcat(hwLine, tmpReg);
346 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
347 sprintf(tmpReg, "A%u", reg);
348 strcat(hwLine, tmpReg);
350 case WINED3DSPR_RASTOUT:
351 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
352 strcat(hwLine, tmpReg);
354 case WINED3DSPR_ATTROUT:
356 strcat(hwLine, "result.color.primary");
358 strcat(hwLine, "result.color.secondary");
361 case WINED3DSPR_TEXCRDOUT:
362 sprintf(tmpReg, "result.texcoord[%u]", reg);
363 strcat(hwLine, tmpReg);
366 FIXME("Unknown reg type %d %d\n", regtype, reg);
367 strcat(hwLine, "unrecognized_register");
373 shader_arb_get_write_mask(param, write_mask);
374 strcat(hwLine, write_mask);
377 shader_arb_get_swizzle(param, is_color, swizzle);
378 strcat(hwLine, swizzle);
382 static void shader_hw_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
383 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
384 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
386 SHADER_BUFFER* buffer = arg->buffer;
387 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
388 const char *tex_type;
390 switch(sampler_type) {
399 case WINED3DSTT_VOLUME:
403 case WINED3DSTT_CUBE:
408 ERR("Unexpected texture type %d\n", sampler_type);
412 if (deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
413 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
415 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
420 static void pshader_gen_input_modifier_line (
421 SHADER_BUFFER* buffer,
426 /* Generate a line that does the input modifier computation and return the input register to use */
431 /* Assume a new line will be added */
434 /* Get register name */
435 pshader_get_register_name(instr, regstr);
436 shader_arb_get_swizzle(instr, FALSE, swzstr);
438 switch (instr & WINED3DSP_SRCMOD_MASK) {
439 case WINED3DSPSM_NONE:
440 sprintf(outregstr, "%s%s", regstr, swzstr);
443 case WINED3DSPSM_NEG:
444 sprintf(outregstr, "-%s%s", regstr, swzstr);
447 case WINED3DSPSM_BIAS:
448 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
450 case WINED3DSPSM_BIASNEG:
451 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
453 case WINED3DSPSM_SIGN:
454 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
456 case WINED3DSPSM_SIGNNEG:
457 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
459 case WINED3DSPSM_COMP:
460 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
463 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
465 case WINED3DSPSM_X2NEG:
466 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
469 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
470 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
473 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
474 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
477 sprintf(outregstr, "%s%s", regstr, swzstr);
481 /* Return modified or original register, with swizzle */
483 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
486 inline static void pshader_gen_output_modifier_line(
487 SHADER_BUFFER* buffer,
493 /* Generate a line that does the output modifier computation */
494 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
495 regstr, write_mask, regstr, shift_tab[shift]);
498 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
500 SHADER_BUFFER* buffer = arg->buffer;
503 char src_name[3][50];
505 /* FIXME: support output modifiers */
507 /* Handle output register */
508 pshader_get_register_name(arg->dst, dst_name);
509 shader_arb_get_write_mask(arg->dst, dst_wmask);
510 strcat(dst_name, dst_wmask);
512 /* Generate input register names (with modifiers) */
513 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
514 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
515 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
517 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
518 shader_addline(buffer, "CMP %s, TMP, %s, %s;\n", dst_name, src_name[1], src_name[2]);
521 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
523 SHADER_BUFFER* buffer = arg->buffer;
526 char src_name[3][50];
528 /* FIXME: support output modifiers */
530 /* Handle output register */
531 pshader_get_register_name(arg->dst, dst_name);
532 shader_arb_get_write_mask(arg->dst, dst_wmask);
533 strcat(dst_name, dst_wmask);
535 /* Generate input register names (with modifiers) */
536 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
537 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
538 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
540 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
541 src_name[0], src_name[2], src_name[1]);
544 /* Map the opcode 1-to-1 to the GL code */
545 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
547 CONST SHADER_OPCODE* curOpcode = arg->opcode;
548 SHADER_BUFFER* buffer = arg->buffer;
549 DWORD dst = arg->dst;
550 DWORD* src = arg->src;
555 /* Output token related */
556 char output_rname[256];
557 char output_wmask[20];
558 BOOL saturate = FALSE;
559 BOOL centroid = FALSE;
560 BOOL partialprecision = FALSE;
563 strcpy(tmpLine, curOpcode->glname);
565 /* Process modifiers */
566 if (0 != (dst & WINED3DSP_DSTMOD_MASK)) {
567 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
569 saturate = mask & WINED3DSPDM_SATURATE;
570 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
571 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
572 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
574 FIXME("Unrecognized modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
577 FIXME("Unhandled modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
579 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
581 /* Generate input and output registers */
582 if (curOpcode->num_params > 0) {
583 char operands[4][100];
585 /* Generate input register names (with modifiers) */
586 for (i = 1; i < curOpcode->num_params; ++i)
587 pshader_gen_input_modifier_line(buffer, src[i-1], i-1, operands[i]);
589 /* Handle output register */
590 pshader_get_register_name(dst, output_rname);
591 strcpy(operands[0], output_rname);
592 shader_arb_get_write_mask(dst, output_wmask);
593 strcat(operands[0], output_wmask);
595 if (saturate && (shift == 0))
596 strcat(tmpLine, "_SAT");
597 strcat(tmpLine, " ");
598 strcat(tmpLine, operands[0]);
599 for (i = 1; i < curOpcode->num_params; i++) {
600 strcat(tmpLine, ", ");
601 strcat(tmpLine, operands[i]);
603 strcat(tmpLine,";\n");
604 shader_addline(buffer, tmpLine);
606 /* A shift requires another line. */
608 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
612 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
614 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
616 DWORD dst = arg->dst;
617 DWORD* src = arg->src;
618 SHADER_BUFFER* buffer = arg->buffer;
619 DWORD hex_version = This->baseShader.hex_version;
624 DWORD reg_sampler_code;
626 /* All versions have a destination register */
627 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
628 pshader_get_register_name(dst, reg_dest);
630 /* 1.0-1.3: Use destination register as coordinate source.
631 1.4+: Use provided coordinate source register. */
632 if (hex_version < WINED3DPS_VERSION(1,4))
633 strcpy(reg_coord, reg_dest);
635 pshader_gen_input_modifier_line(buffer, src[0], 0, reg_coord);
637 /* 1.0-1.4: Use destination register number as texture code.
638 2.0+: Use provided sampler number as texure code. */
639 if (hex_version < WINED3DPS_VERSION(2,0))
640 reg_sampler_code = reg_dest_code;
642 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
644 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord);
647 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
649 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
650 DWORD dst = arg->dst;
651 DWORD* src = arg->src;
652 SHADER_BUFFER* buffer = arg->buffer;
653 DWORD hex_version = This->baseShader.hex_version;
656 shader_arb_get_write_mask(dst, tmp);
657 if (hex_version != WINED3DPS_VERSION(1,4)) {
658 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
659 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
661 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
662 DWORD reg2 = src[0] & WINED3DSP_REGNUM_MASK;
663 shader_addline(buffer, "MOV R%u%s, fragment.texcoord[%u];\n", reg1, tmp, reg2);
667 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg) {
669 SHADER_BUFFER* buffer = arg->buffer;
671 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
672 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
675 sprintf(dst_str, "T%u", reg1);
676 shader_addline(buffer, "MOV TMP.r, T%u.a;\n", reg2);
677 shader_addline(buffer, "MOV TMP.g, T%u.r;\n", reg2);
678 shader_hw_sample(arg, reg1, dst_str, "TMP");
681 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg) {
683 SHADER_BUFFER* buffer = arg->buffer;
685 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
686 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
689 sprintf(dst_str, "T%u", reg1);
690 shader_addline(buffer, "MOV TMP.r, T%u.g;\n", reg2);
691 shader_addline(buffer, "MOV TMP.g, T%u.b;\n", reg2);
692 shader_hw_sample(arg, reg1, dst_str, "TMP");
695 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
696 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
698 DWORD dst = arg->dst;
699 DWORD src = arg->src[0] & WINED3DSP_REGNUM_MASK;
700 SHADER_BUFFER* buffer = arg->buffer;
705 /* All versions have a destination register */
706 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
707 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
708 pshader_get_register_name(dst, reg_coord);
710 if(This->bumpenvmatconst) {
711 /*shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", 1, 1); Not needed - done already */
713 /* Plain GL does not have any signed formats suitable for that instruction.
714 * So the surface loading code converts the -128 ... 127 signed integers to
715 * 0 ... 255 unsigned ones. The following line undoes that.
717 * TODO: Both GL_NV_texture_shader and GL_ATI_envmap_bumpmap provide pixel formats
718 * suitable for loading the Direct3D perturbation data. If one of them is used, do
719 * not correct the signedness
721 shader_addline(buffer, "MAD T%u, T%u, coefmul.x, -one;\n", src, src);
723 shader_addline(buffer, "SWZ TMP2, bumpenvmat, x, z, 0, 0;\n");
724 shader_addline(buffer, "DP3 TMP.r, TMP2, T%u;\n", src);
725 shader_addline(buffer, "SWZ TMP2, bumpenvmat, y, w, 0, 0;\n");
726 shader_addline(buffer, "DP3 TMP.g, TMP2, T%u;\n", src);
727 shader_addline(buffer, "ADD TMP.rg, TMP, %s;\n", reg_coord);
728 /* Not sure about this, but hl2 needs it. It uses a projected texture with texbem and depends on the 4th coordinate */
729 shader_addline(buffer, "MOV TMP.a, %s;\n", reg_coord);
730 shader_hw_sample(arg, reg_dest_code, reg_coord, "TMP");
732 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
733 shader_hw_sample(arg, reg_dest_code, reg_coord, reg_coord);
737 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
739 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
740 SHADER_BUFFER* buffer = arg->buffer;
743 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
744 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
747 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
749 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
750 SHADER_BUFFER* buffer = arg->buffer;
754 sprintf(dst_str, "T%u", reg);
755 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
756 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
757 shader_hw_sample(arg, reg, dst_str, "TMP");
760 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
762 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
763 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
764 SHADER_BUFFER* buffer = arg->buffer;
765 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
768 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
769 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
770 current_state->texcoord_w[current_state->current_row++] = reg;
773 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
775 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
776 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
777 SHADER_BUFFER* buffer = arg->buffer;
778 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
782 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
783 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
785 /* Sample the texture using the calculated coordinates */
786 sprintf(dst_str, "T%u", reg);
787 shader_hw_sample(arg, reg, dst_str, "TMP");
788 current_state->current_row = 0;
791 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
793 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
794 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
795 SHADER_BUFFER* buffer = arg->buffer;
796 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
800 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
801 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
803 /* Construct the eye-ray vector from w coordinates */
804 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
805 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
806 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
808 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
809 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
810 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
811 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
813 /* Sample the texture using the calculated coordinates */
814 sprintf(dst_str, "T%u", reg);
815 shader_hw_sample(arg, reg, dst_str, "TMP");
816 current_state->current_row = 0;
819 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
821 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
822 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
823 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
824 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
825 SHADER_BUFFER* buffer = arg->buffer;
829 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
830 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
832 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
833 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
834 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
835 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
837 /* Sample the texture using the calculated coordinates */
838 sprintf(dst_str, "T%u", reg);
839 shader_hw_sample(arg, reg, dst_str, "TMP");
840 current_state->current_row = 0;
843 /** Handles transforming all WINED3DSIO_M?x? opcodes for
844 Vertex shaders to ARB_vertex_program codes */
845 void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
849 SHADER_OPCODE_ARG tmpArg;
851 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
853 /* Set constants for the temporary argument */
854 tmpArg.shader = arg->shader;
855 tmpArg.buffer = arg->buffer;
856 tmpArg.src[0] = arg->src[0];
857 tmpArg.src_addr[0] = arg->src_addr[0];
858 tmpArg.src_addr[1] = arg->src_addr[1];
859 tmpArg.reg_maps = arg->reg_maps;
861 switch(arg->opcode->opcode) {
862 case WINED3DSIO_M4x4:
864 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
866 case WINED3DSIO_M4x3:
868 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
870 case WINED3DSIO_M3x4:
872 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
874 case WINED3DSIO_M3x3:
876 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
878 case WINED3DSIO_M3x2:
880 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
886 for (i = 0; i < nComponents; i++) {
887 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
888 tmpArg.src[1] = arg->src[1]+i;
889 vshader_hw_map2gl(&tmpArg);
893 void vshader_hw_rsq_rcp(SHADER_OPCODE_ARG* arg) {
894 CONST SHADER_OPCODE* curOpcode = arg->opcode;
895 SHADER_BUFFER* buffer = arg->buffer;
896 DWORD dst = arg->dst;
897 DWORD src = arg->src[0];
898 DWORD swizzle = (src & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
902 strcpy(tmpLine, curOpcode->glname); /* Opcode */
903 vshader_program_add_param(arg, dst, FALSE, tmpLine); /* Destination */
904 strcat(tmpLine, ",");
905 vshader_program_add_param(arg, src, TRUE, tmpLine);
906 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) == swizzle) {
907 /* Dx sdk says .x is used if no swizzle is given */
908 strcat(tmpLine, ".x");
911 shader_addline(buffer, "%s;\n", tmpLine);
914 /* TODO: merge with pixel shader */
915 /* Map the opcode 1-to-1 to the GL code */
916 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
918 CONST SHADER_OPCODE* curOpcode = arg->opcode;
919 SHADER_BUFFER* buffer = arg->buffer;
920 DWORD dst = arg->dst;
921 DWORD* src = arg->src;
923 DWORD dst_regtype = shader_get_regtype(dst);
927 if ((curOpcode->opcode == WINED3DSIO_MOV && dst_regtype == WINED3DSPR_ADDR) || curOpcode->opcode == WINED3DSIO_MOVA)
928 strcpy(tmpLine, "ARL");
930 strcpy(tmpLine, curOpcode->glname);
932 if (curOpcode->num_params > 0) {
933 vshader_program_add_param(arg, dst, FALSE, tmpLine);
934 for (i = 1; i < curOpcode->num_params; ++i) {
935 strcat(tmpLine, ",");
936 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
939 shader_addline(buffer, "%s;\n", tmpLine);
942 static GLuint create_arb_blt_vertex_program(WineD3D_GL_Info *gl_info) {
943 GLuint program_id = 0;
944 const char *blt_vprogram =
946 "PARAM c[1] = { { 1, 0.5 } };\n"
947 "MOV result.position, vertex.position;\n"
948 "MOV result.color, c[0].x;\n"
949 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
950 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
953 GL_EXTCALL(glGenProgramsARB(1, &program_id));
954 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
955 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
957 if (glGetError() == GL_INVALID_OPERATION) {
959 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
960 FIXME("Vertex program error at position %d: %s\n", pos,
961 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
967 static GLuint create_arb_blt_fragment_program(WineD3D_GL_Info *gl_info) {
968 GLuint program_id = 0;
969 const char *blt_fprogram =
972 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
973 "MOV result.depth.z, R0.x;\n"
976 GL_EXTCALL(glGenProgramsARB(1, &program_id));
977 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
978 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprogram), blt_fprogram));
980 if (glGetError() == GL_INVALID_OPERATION) {
982 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
983 FIXME("Fragment program error at position %d: %s\n", pos,
984 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
990 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
991 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
992 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
995 TRACE("Using vertex shader\n");
997 /* Bind the vertex program */
998 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB,
999 ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId));
1000 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1002 /* Enable OpenGL vertex programs */
1003 glEnable(GL_VERTEX_PROGRAM_ARB);
1004 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1005 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
1006 This, ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId);
1007 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1008 glDisable(GL_VERTEX_PROGRAM_ARB);
1009 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1013 TRACE("Using pixel shader\n");
1015 /* Bind the fragment program */
1016 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
1017 ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId));
1018 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1020 /* Enable OpenGL fragment programs */
1021 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1022 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1023 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
1024 This, ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId);
1025 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1026 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1027 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1031 static void shader_arb_select_depth_blt(IWineD3DDevice *iface) {
1032 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1033 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1034 static GLuint vprogram_id = 0;
1035 static GLuint fprogram_id = 0;
1037 if (!vprogram_id) vprogram_id = create_arb_blt_vertex_program(gl_info);
1038 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vprogram_id));
1039 glEnable(GL_VERTEX_PROGRAM_ARB);
1041 if (!fprogram_id) fprogram_id = create_arb_blt_fragment_program(gl_info);
1042 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprogram_id));
1043 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1046 static void shader_arb_cleanup(IWineD3DDevice *iface) {
1047 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1048 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1049 if (GL_SUPPORT(ARB_VERTEX_PROGRAM)) glDisable(GL_VERTEX_PROGRAM_ARB);
1050 if (GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) glDisable(GL_FRAGMENT_PROGRAM_ARB);
1053 const shader_backend_t arb_program_shader_backend = {
1055 &shader_arb_select_depth_blt,
1056 &shader_arb_load_constants,