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 constant_entry *constant;
54 local_constant* lconst;
58 if (TRACE_ON(d3d_shader)) {
59 for (i = 0; i < max_constants; ++i) {
60 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
61 constants[i * 4 + 0], constants[i * 4 + 1],
62 constants[i * 4 + 2], constants[i * 4 + 3]);
65 for (i = 0; i < max_constants; ++i) {
66 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
68 checkGLcall("glProgramEnvParameter4fvARB()");
70 if (TRACE_ON(d3d_shader)) {
71 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
73 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
74 constants[i * 4 + 0], constants[i * 4 + 1],
75 constants[i * 4 + 2], constants[i * 4 + 3]);
78 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
80 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
82 checkGLcall("glProgramEnvParameter4fvARB()");
85 /* Load immediate constants */
86 if (TRACE_ON(d3d_shader)) {
87 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
88 GLfloat* values = (GLfloat*)lconst->value;
89 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
90 values[0], values[1], values[2], values[3]);
93 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
94 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
96 checkGLcall("glProgramEnvParameter4fvARB()");
100 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
102 * We only support float constants in ARB at the moment, so don't
103 * worry about the Integers or Booleans
105 void shader_arb_load_constants(
106 IWineD3DDevice* device,
108 char useVertexShader) {
110 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
111 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
112 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
114 if (useVertexShader) {
115 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
116 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) stateBlock->vertexShader;
117 IWineD3DVertexDeclarationImpl* vertexDeclaration =
118 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
120 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
121 /* Load DirectX 8 float constants for vertex shader */
122 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
123 GL_LIMITS(vshader_constantsF),
124 vertexDeclaration->constants, NULL);
127 /* Load DirectX 9 float constants for vertex shader */
128 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
129 GL_LIMITS(vshader_constantsF),
130 stateBlock->vertexShaderConstantF,
131 &stateBlock->set_vconstantsF);
133 /* Upload the position fixup */
134 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
137 if (usePixelShader) {
139 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
141 /* Load DirectX 9 float constants for pixel shader */
142 shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
143 GL_LIMITS(pshader_constantsF),
144 stateBlock->pixelShaderConstantF,
145 &stateBlock->set_pconstantsF);
149 /* Generate the variable & register declarations for the ARB_vertex_program output target */
150 void shader_generate_arb_declarations(
151 IWineD3DBaseShader *iface,
152 shader_reg_maps* reg_maps,
153 SHADER_BUFFER* buffer,
154 WineD3D_GL_Info* gl_info) {
156 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
158 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
159 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
160 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
162 /* Temporary Output register */
163 shader_addline(buffer, "TEMP TMP_OUT;\n");
165 for(i = 0; i < This->baseShader.limits.temporary; i++) {
166 if (reg_maps->temporary[i])
167 shader_addline(buffer, "TEMP R%u;\n", i);
170 for (i = 0; i < This->baseShader.limits.address; i++) {
171 if (reg_maps->address[i])
172 shader_addline(buffer, "ADDRESS A%d;\n", i);
175 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
176 if (reg_maps->texcoord[i])
177 shader_addline(buffer,"TEMP T%u;\n", i);
180 /* Texture coordinate registers must be pre-loaded */
181 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
182 if (reg_maps->texcoord[i])
183 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
186 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
187 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
188 max_constantsF, max_constantsF - 1);
191 static const char * const shift_tab[] = {
192 "dummy", /* 0 (none) */
193 "coefmul.x", /* 1 (x2) */
194 "coefmul.y", /* 2 (x4) */
195 "coefmul.z", /* 3 (x8) */
196 "coefmul.w", /* 4 (x16) */
197 "dummy", /* 5 (x32) */
198 "dummy", /* 6 (x64) */
199 "dummy", /* 7 (x128) */
200 "dummy", /* 8 (d256) */
201 "dummy", /* 9 (d128) */
202 "dummy", /* 10 (d64) */
203 "dummy", /* 11 (d32) */
204 "coefdiv.w", /* 12 (d16) */
205 "coefdiv.z", /* 13 (d8) */
206 "coefdiv.y", /* 14 (d4) */
207 "coefdiv.x" /* 15 (d2) */
210 static void shader_arb_get_write_mask(const DWORD param, char *write_mask) {
211 char *ptr = write_mask;
213 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
215 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
216 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
217 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
218 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
224 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
225 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
226 * but addressed as "rgba". To fix this we need to swap the register's x
227 * and z components. */
228 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
229 char *ptr = swizzle_str;
231 /* swizzle bits fields: wwzzyyxx */
232 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
233 DWORD swizzle_x = swizzle & 0x03;
234 DWORD swizzle_y = (swizzle >> 2) & 0x03;
235 DWORD swizzle_z = (swizzle >> 4) & 0x03;
236 DWORD swizzle_w = (swizzle >> 6) & 0x03;
238 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
239 * generate a swizzle string. Unless we need to our own swizzling. */
240 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
242 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
243 *ptr++ = swizzle_chars[swizzle_x];
245 *ptr++ = swizzle_chars[swizzle_x];
246 *ptr++ = swizzle_chars[swizzle_y];
247 *ptr++ = swizzle_chars[swizzle_z];
248 *ptr++ = swizzle_chars[swizzle_w];
255 static void pshader_get_register_name(
256 const DWORD param, char* regstr) {
258 DWORD reg = param & WINED3DSP_REGNUM_MASK;
259 DWORD regtype = shader_get_regtype(param);
262 case WINED3DSPR_TEMP:
263 sprintf(regstr, "R%u", reg);
265 case WINED3DSPR_INPUT:
267 strcpy(regstr, "fragment.color.primary");
269 strcpy(regstr, "fragment.color.secondary");
272 case WINED3DSPR_CONST:
273 sprintf(regstr, "C[%u]", reg);
275 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
276 sprintf(regstr,"T%u", reg);
278 case WINED3DSPR_COLOROUT:
280 sprintf(regstr, "result.color");
282 /* TODO: See GL_ARB_draw_buffers */
283 FIXME("Unsupported write to render target %u\n", reg);
284 sprintf(regstr, "unsupported_register");
287 case WINED3DSPR_DEPTHOUT:
288 sprintf(regstr, "result.depth");
290 case WINED3DSPR_ATTROUT:
291 sprintf(regstr, "oD[%u]", reg);
293 case WINED3DSPR_TEXCRDOUT:
294 sprintf(regstr, "oT[%u]", reg);
297 FIXME("Unhandled register name Type(%d)\n", regtype);
298 sprintf(regstr, "unrecognized_register");
303 /* TODO: merge with pixel shader */
304 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
306 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
308 /* oPos, oFog and oPts in D3D */
309 static const char * const hwrastout_reg_names[] = { "TMP_OUT", "TMP_FOG", "result.pointsize" };
311 DWORD reg = param & WINED3DSP_REGNUM_MASK;
312 DWORD regtype = shader_get_regtype(param);
314 BOOL is_color = FALSE;
316 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
317 strcat(hwLine, " -");
323 case WINED3DSPR_TEMP:
324 sprintf(tmpReg, "R%u", reg);
325 strcat(hwLine, tmpReg);
327 case WINED3DSPR_INPUT:
329 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
332 sprintf(tmpReg, "vertex.attrib[%u]", reg);
333 strcat(hwLine, tmpReg);
335 case WINED3DSPR_CONST:
336 sprintf(tmpReg, "C[%s%u]", (param & WINED3DSHADER_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
337 strcat(hwLine, tmpReg);
339 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
340 sprintf(tmpReg, "A%u", reg);
341 strcat(hwLine, tmpReg);
343 case WINED3DSPR_RASTOUT:
344 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
345 strcat(hwLine, tmpReg);
347 case WINED3DSPR_ATTROUT:
349 strcat(hwLine, "result.color.primary");
351 strcat(hwLine, "result.color.secondary");
354 case WINED3DSPR_TEXCRDOUT:
355 sprintf(tmpReg, "result.texcoord[%u]", reg);
356 strcat(hwLine, tmpReg);
359 FIXME("Unknown reg type %d %d\n", regtype, reg);
360 strcat(hwLine, "unrecognized_register");
366 shader_arb_get_write_mask(param, write_mask);
367 strcat(hwLine, write_mask);
370 shader_arb_get_swizzle(param, is_color, swizzle);
371 strcat(hwLine, swizzle);
375 static void shader_hw_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
376 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
377 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
379 SHADER_BUFFER* buffer = arg->buffer;
380 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
381 const char *tex_type;
383 switch(sampler_type) {
392 case WINED3DSTT_VOLUME:
396 case WINED3DSTT_CUBE:
401 ERR("Unexpected texture type %d\n", sampler_type);
405 if (deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
406 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
408 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
413 static void pshader_gen_input_modifier_line (
414 SHADER_BUFFER* buffer,
419 /* Generate a line that does the input modifier computation and return the input register to use */
424 /* Assume a new line will be added */
427 /* Get register name */
428 pshader_get_register_name(instr, regstr);
429 shader_arb_get_swizzle(instr, FALSE, swzstr);
431 switch (instr & WINED3DSP_SRCMOD_MASK) {
432 case WINED3DSPSM_NONE:
433 sprintf(outregstr, "%s%s", regstr, swzstr);
436 case WINED3DSPSM_NEG:
437 sprintf(outregstr, "-%s%s", regstr, swzstr);
440 case WINED3DSPSM_BIAS:
441 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
443 case WINED3DSPSM_BIASNEG:
444 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
446 case WINED3DSPSM_SIGN:
447 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
449 case WINED3DSPSM_SIGNNEG:
450 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
452 case WINED3DSPSM_COMP:
453 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
456 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
458 case WINED3DSPSM_X2NEG:
459 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
462 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
463 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
466 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
467 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
470 sprintf(outregstr, "%s%s", regstr, swzstr);
474 /* Return modified or original register, with swizzle */
476 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
479 inline static void pshader_gen_output_modifier_line(
480 SHADER_BUFFER* buffer,
486 /* Generate a line that does the output modifier computation */
487 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
488 regstr, write_mask, regstr, shift_tab[shift]);
491 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
493 SHADER_BUFFER* buffer = arg->buffer;
496 char src_name[3][50];
498 /* FIXME: support output modifiers */
500 /* Handle output register */
501 pshader_get_register_name(arg->dst, dst_name);
502 shader_arb_get_write_mask(arg->dst, dst_wmask);
503 strcat(dst_name, dst_wmask);
505 /* Generate input register names (with modifiers) */
506 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
507 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
508 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
510 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
511 shader_addline(buffer, "CMP %s, TMP, %s, %s;\n", dst_name, src_name[1], src_name[2]);
514 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
516 SHADER_BUFFER* buffer = arg->buffer;
519 char src_name[3][50];
521 /* FIXME: support output modifiers */
523 /* Handle output register */
524 pshader_get_register_name(arg->dst, dst_name);
525 shader_arb_get_write_mask(arg->dst, dst_wmask);
526 strcat(dst_name, dst_wmask);
528 /* Generate input register names (with modifiers) */
529 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
530 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
531 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
533 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
534 src_name[0], src_name[2], src_name[1]);
537 /* Map the opcode 1-to-1 to the GL code */
538 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
540 CONST SHADER_OPCODE* curOpcode = arg->opcode;
541 SHADER_BUFFER* buffer = arg->buffer;
542 DWORD dst = arg->dst;
543 DWORD* src = arg->src;
548 /* Output token related */
549 char output_rname[256];
550 char output_wmask[20];
551 BOOL saturate = FALSE;
552 BOOL centroid = FALSE;
553 BOOL partialprecision = FALSE;
556 strcpy(tmpLine, curOpcode->glname);
558 /* Process modifiers */
559 if (0 != (dst & WINED3DSP_DSTMOD_MASK)) {
560 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
562 saturate = mask & WINED3DSPDM_SATURATE;
563 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
564 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
565 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
567 FIXME("Unrecognized modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
570 FIXME("Unhandled modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
572 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
574 /* Generate input and output registers */
575 if (curOpcode->num_params > 0) {
576 char operands[4][100];
578 /* Generate input register names (with modifiers) */
579 for (i = 1; i < curOpcode->num_params; ++i)
580 pshader_gen_input_modifier_line(buffer, src[i-1], i-1, operands[i]);
582 /* Handle output register */
583 pshader_get_register_name(dst, output_rname);
584 strcpy(operands[0], output_rname);
585 shader_arb_get_write_mask(dst, output_wmask);
586 strcat(operands[0], output_wmask);
588 if (saturate && (shift == 0))
589 strcat(tmpLine, "_SAT");
590 strcat(tmpLine, " ");
591 strcat(tmpLine, operands[0]);
592 for (i = 1; i < curOpcode->num_params; i++) {
593 strcat(tmpLine, ", ");
594 strcat(tmpLine, operands[i]);
596 strcat(tmpLine,";\n");
597 shader_addline(buffer, tmpLine);
599 /* A shift requires another line. */
601 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
605 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
607 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
609 DWORD dst = arg->dst;
610 DWORD* src = arg->src;
611 SHADER_BUFFER* buffer = arg->buffer;
612 DWORD hex_version = This->baseShader.hex_version;
617 DWORD reg_sampler_code;
619 /* All versions have a destination register */
620 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
621 pshader_get_register_name(dst, reg_dest);
623 /* 1.0-1.3: Use destination register as coordinate source.
624 1.4+: Use provided coordinate source register. */
625 if (hex_version < WINED3DPS_VERSION(1,4))
626 strcpy(reg_coord, reg_dest);
628 pshader_gen_input_modifier_line(buffer, src[0], 0, reg_coord);
630 /* 1.0-1.4: Use destination register number as texture code.
631 2.0+: Use provided sampler number as texure code. */
632 if (hex_version < WINED3DPS_VERSION(2,0))
633 reg_sampler_code = reg_dest_code;
635 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
637 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord);
640 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
642 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
643 DWORD dst = arg->dst;
644 DWORD* src = arg->src;
645 SHADER_BUFFER* buffer = arg->buffer;
646 DWORD hex_version = This->baseShader.hex_version;
649 shader_arb_get_write_mask(dst, tmp);
650 if (hex_version != WINED3DPS_VERSION(1,4)) {
651 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
652 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
654 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
655 DWORD reg2 = src[0] & WINED3DSP_REGNUM_MASK;
656 shader_addline(buffer, "MOV R%u%s, fragment.texcoord[%u];\n", reg1, tmp, reg2);
660 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg) {
662 SHADER_BUFFER* buffer = arg->buffer;
664 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
665 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
668 sprintf(dst_str, "T%u", reg1);
669 shader_addline(buffer, "MOV TMP.r, T%u.a;\n", reg2);
670 shader_addline(buffer, "MOV TMP.g, T%u.r;\n", reg2);
671 shader_hw_sample(arg, reg1, dst_str, "TMP");
674 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg) {
676 SHADER_BUFFER* buffer = arg->buffer;
678 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
679 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
682 sprintf(dst_str, "T%u", reg1);
683 shader_addline(buffer, "MOV TMP.r, T%u.g;\n", reg2);
684 shader_addline(buffer, "MOV TMP.g, T%u.b;\n", reg2);
685 shader_hw_sample(arg, reg1, dst_str, "TMP");
688 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
690 SHADER_BUFFER* buffer = arg->buffer;
691 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
692 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
695 /* FIXME: Should apply the BUMPMAPENV matrix */
696 sprintf(dst_str, "T%u", reg1);
697 shader_addline(buffer, "ADD TMP.rg, fragment.texcoord[%u], T%u;\n", reg1, reg2);
698 shader_hw_sample(arg, reg1, dst_str, "TMP");
701 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
703 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
704 SHADER_BUFFER* buffer = arg->buffer;
707 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
708 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
711 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
713 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
714 SHADER_BUFFER* buffer = arg->buffer;
718 sprintf(dst_str, "T%u", reg);
719 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
720 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
721 shader_hw_sample(arg, reg, dst_str, "TMP");
724 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
726 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
727 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
728 SHADER_BUFFER* buffer = arg->buffer;
729 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
732 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
733 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
734 current_state->texcoord_w[current_state->current_row++] = reg;
737 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
739 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
740 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
741 SHADER_BUFFER* buffer = arg->buffer;
742 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
746 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
747 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
749 /* Sample the texture using the calculated coordinates */
750 sprintf(dst_str, "T%u", reg);
751 shader_hw_sample(arg, reg, dst_str, "TMP");
752 current_state->current_row = 0;
755 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
757 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
758 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
759 SHADER_BUFFER* buffer = arg->buffer;
760 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
764 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
765 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
767 /* Construct the eye-ray vector from w coordinates */
768 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
769 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
770 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
772 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
773 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
774 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
775 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
777 /* Sample the texture using the calculated coordinates */
778 sprintf(dst_str, "T%u", reg);
779 shader_hw_sample(arg, reg, dst_str, "TMP");
780 current_state->current_row = 0;
783 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
785 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
786 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
787 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
788 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
789 SHADER_BUFFER* buffer = arg->buffer;
793 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
794 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
796 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
797 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
798 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
799 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
801 /* Sample the texture using the calculated coordinates */
802 sprintf(dst_str, "T%u", reg);
803 shader_hw_sample(arg, reg, dst_str, "TMP");
804 current_state->current_row = 0;
807 /** Handles transforming all WINED3DSIO_M?x? opcodes for
808 Vertex shaders to ARB_vertex_program codes */
809 void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
813 SHADER_OPCODE_ARG tmpArg;
815 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
817 /* Set constants for the temporary argument */
818 tmpArg.shader = arg->shader;
819 tmpArg.buffer = arg->buffer;
820 tmpArg.src[0] = arg->src[0];
821 tmpArg.src_addr[0] = arg->src_addr[0];
822 tmpArg.src_addr[1] = arg->src_addr[1];
823 tmpArg.reg_maps = arg->reg_maps;
825 switch(arg->opcode->opcode) {
826 case WINED3DSIO_M4x4:
828 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
830 case WINED3DSIO_M4x3:
832 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
834 case WINED3DSIO_M3x4:
836 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
838 case WINED3DSIO_M3x3:
840 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
842 case WINED3DSIO_M3x2:
844 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
850 for (i = 0; i < nComponents; i++) {
851 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
852 tmpArg.src[1] = arg->src[1]+i;
853 vshader_hw_map2gl(&tmpArg);
857 /* TODO: merge with pixel shader */
858 /* Map the opcode 1-to-1 to the GL code */
859 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
861 CONST SHADER_OPCODE* curOpcode = arg->opcode;
862 SHADER_BUFFER* buffer = arg->buffer;
863 DWORD dst = arg->dst;
864 DWORD* src = arg->src;
866 DWORD dst_regtype = shader_get_regtype(dst);
870 if ((curOpcode->opcode == WINED3DSIO_MOV && dst_regtype == WINED3DSPR_ADDR) || curOpcode->opcode == WINED3DSIO_MOVA)
871 strcpy(tmpLine, "ARL");
873 strcpy(tmpLine, curOpcode->glname);
875 if (curOpcode->num_params > 0) {
876 vshader_program_add_param(arg, dst, FALSE, tmpLine);
877 for (i = 1; i < curOpcode->num_params; ++i) {
878 strcat(tmpLine, ",");
879 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
882 shader_addline(buffer, "%s;\n", tmpLine);
885 static GLuint create_arb_blt_vertex_program(WineD3D_GL_Info *gl_info) {
886 GLuint program_id = 0;
887 const char *blt_vprogram =
889 "PARAM c[1] = { { 1, 0.5 } };\n"
890 "MOV result.position, vertex.position;\n"
891 "MOV result.color, c[0].x;\n"
892 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
893 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
896 GL_EXTCALL(glGenProgramsARB(1, &program_id));
897 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
898 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
900 if (glGetError() == GL_INVALID_OPERATION) {
902 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
903 FIXME("Vertex program error at position %d: %s\n", pos,
904 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
910 static GLuint create_arb_blt_fragment_program(WineD3D_GL_Info *gl_info) {
911 GLuint program_id = 0;
912 const char *blt_fprogram =
915 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
916 "MOV result.depth.z, R0.x;\n"
919 GL_EXTCALL(glGenProgramsARB(1, &program_id));
920 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
921 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprogram), blt_fprogram));
923 if (glGetError() == GL_INVALID_OPERATION) {
925 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
926 FIXME("Fragment program error at position %d: %s\n", pos,
927 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
933 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
934 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
935 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
938 TRACE("Using vertex shader\n");
940 /* Bind the vertex program */
941 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB,
942 ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId));
943 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
945 /* Enable OpenGL vertex programs */
946 glEnable(GL_VERTEX_PROGRAM_ARB);
947 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
948 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
949 This, ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId);
950 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
951 glDisable(GL_VERTEX_PROGRAM_ARB);
952 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
956 TRACE("Using pixel shader\n");
958 /* Bind the fragment program */
959 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
960 ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId));
961 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
963 /* Enable OpenGL fragment programs */
964 glEnable(GL_FRAGMENT_PROGRAM_ARB);
965 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
966 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
967 This, ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId);
968 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
969 glDisable(GL_FRAGMENT_PROGRAM_ARB);
970 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
974 static void shader_arb_select_depth_blt(IWineD3DDevice *iface) {
975 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
976 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
977 static GLuint vprogram_id = 0;
978 static GLuint fprogram_id = 0;
980 if (!vprogram_id) vprogram_id = create_arb_blt_vertex_program(gl_info);
981 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vprogram_id));
982 glEnable(GL_VERTEX_PROGRAM_ARB);
984 if (!fprogram_id) fprogram_id = create_arb_blt_fragment_program(gl_info);
985 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprogram_id));
986 glEnable(GL_FRAGMENT_PROGRAM_ARB);
989 static void shader_arb_cleanup(BOOL usePS, BOOL useVS) {
990 if (useVS) glDisable(GL_VERTEX_PROGRAM_ARB);
991 if (usePS) glDisable(GL_FRAGMENT_PROGRAM_ARB);
994 const shader_backend_t arb_program_shader_backend = {
996 &shader_arb_select_depth_blt,
997 &shader_arb_load_constants,