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);
37 #define GLINFO_LOCATION (*gl_info)
39 /********************************************************
40 * ARB_[vertex/fragment]_program helper functions follow
41 ********************************************************/
44 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
45 * When constant_list == NULL, it will load all the constants.
47 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
48 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
50 static void shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info, GLuint target_type,
51 unsigned int max_constants, float* constants, struct list *constant_list) {
52 constant_entry *constant;
53 local_constant* lconst;
57 if (TRACE_ON(d3d_shader)) {
58 for (i = 0; i < max_constants; ++i) {
59 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
60 constants[i * 4 + 0], constants[i * 4 + 1],
61 constants[i * 4 + 2], constants[i * 4 + 3]);
64 for (i = 0; i < max_constants; ++i) {
65 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
67 checkGLcall("glProgramEnvParameter4fvARB()");
69 if (TRACE_ON(d3d_shader)) {
70 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
72 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
73 constants[i * 4 + 0], constants[i * 4 + 1],
74 constants[i * 4 + 2], constants[i * 4 + 3]);
77 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
79 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
81 checkGLcall("glProgramEnvParameter4fvARB()");
84 /* Load immediate constants */
85 if (TRACE_ON(d3d_shader)) {
86 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
87 GLfloat* values = (GLfloat*)lconst->value;
88 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
89 values[0], values[1], values[2], values[3]);
92 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
93 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
95 checkGLcall("glProgramEnvParameter4fvARB()");
99 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
101 * We only support float constants in ARB at the moment, so don't
102 * worry about the Integers or Booleans
104 void shader_arb_load_constants(
105 IWineD3DDevice* device,
107 char useVertexShader) {
109 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
110 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
111 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
113 if (useVertexShader) {
114 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
115 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) stateBlock->vertexShader;
116 IWineD3DVertexDeclarationImpl* vertexDeclaration =
117 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
119 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
120 /* Load DirectX 8 float constants for vertex shader */
121 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
122 GL_LIMITS(vshader_constantsF),
123 vertexDeclaration->constants, NULL);
126 /* Load DirectX 9 float constants for vertex shader */
127 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
128 GL_LIMITS(vshader_constantsF),
129 stateBlock->vertexShaderConstantF,
130 &stateBlock->set_vconstantsF);
132 /* Upload the position fixup */
133 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
136 if (usePixelShader) {
138 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
140 /* Load DirectX 9 float constants for pixel shader */
141 shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
142 GL_LIMITS(pshader_constantsF),
143 stateBlock->pixelShaderConstantF,
144 &stateBlock->set_pconstantsF);
148 /* Generate the variable & register declarations for the ARB_vertex_program output target */
149 void shader_generate_arb_declarations(
150 IWineD3DBaseShader *iface,
151 shader_reg_maps* reg_maps,
152 SHADER_BUFFER* buffer,
153 WineD3D_GL_Info* gl_info) {
155 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
157 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
158 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
159 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
161 /* Temporary Output register */
162 shader_addline(buffer, "TEMP TMP_OUT;\n");
164 for(i = 0; i < This->baseShader.limits.temporary; i++) {
165 if (reg_maps->temporary[i])
166 shader_addline(buffer, "TEMP R%u;\n", i);
169 for (i = 0; i < This->baseShader.limits.address; i++) {
170 if (reg_maps->address[i])
171 shader_addline(buffer, "ADDRESS A%d;\n", i);
174 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
175 if (reg_maps->texcoord[i])
176 shader_addline(buffer,"TEMP T%u;\n", i);
179 /* Texture coordinate registers must be pre-loaded */
180 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
181 if (reg_maps->texcoord[i])
182 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
185 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
186 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
187 max_constantsF, max_constantsF - 1);
190 static const char * const shift_tab[] = {
191 "dummy", /* 0 (none) */
192 "coefmul.x", /* 1 (x2) */
193 "coefmul.y", /* 2 (x4) */
194 "coefmul.z", /* 3 (x8) */
195 "coefmul.w", /* 4 (x16) */
196 "dummy", /* 5 (x32) */
197 "dummy", /* 6 (x64) */
198 "dummy", /* 7 (x128) */
199 "dummy", /* 8 (d256) */
200 "dummy", /* 9 (d128) */
201 "dummy", /* 10 (d64) */
202 "dummy", /* 11 (d32) */
203 "coefdiv.w", /* 12 (d16) */
204 "coefdiv.z", /* 13 (d8) */
205 "coefdiv.y", /* 14 (d4) */
206 "coefdiv.x" /* 15 (d2) */
209 static void shader_arb_get_write_mask(const DWORD param, char *write_mask) {
210 char *ptr = write_mask;
212 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
214 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
215 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
216 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
217 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
223 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
224 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
225 * but addressed as "rgba". To fix this we need to swap the register's x
226 * and z components. */
227 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
228 char *ptr = swizzle_str;
230 /* swizzle bits fields: wwzzyyxx */
231 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
232 DWORD swizzle_x = swizzle & 0x03;
233 DWORD swizzle_y = (swizzle >> 2) & 0x03;
234 DWORD swizzle_z = (swizzle >> 4) & 0x03;
235 DWORD swizzle_w = (swizzle >> 6) & 0x03;
237 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
238 * generate a swizzle string. Unless we need to our own swizzling. */
239 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
241 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
242 *ptr++ = swizzle_chars[swizzle_x];
244 *ptr++ = swizzle_chars[swizzle_x];
245 *ptr++ = swizzle_chars[swizzle_y];
246 *ptr++ = swizzle_chars[swizzle_z];
247 *ptr++ = swizzle_chars[swizzle_w];
254 static void pshader_get_register_name(
255 const DWORD param, char* regstr) {
257 DWORD reg = param & WINED3DSP_REGNUM_MASK;
258 DWORD regtype = shader_get_regtype(param);
261 case WINED3DSPR_TEMP:
262 sprintf(regstr, "R%u", reg);
264 case WINED3DSPR_INPUT:
266 strcpy(regstr, "fragment.color.primary");
268 strcpy(regstr, "fragment.color.secondary");
271 case WINED3DSPR_CONST:
272 sprintf(regstr, "C[%u]", reg);
274 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
275 sprintf(regstr,"T%u", reg);
277 case WINED3DSPR_COLOROUT:
279 sprintf(regstr, "result.color");
281 /* TODO: See GL_ARB_draw_buffers */
282 FIXME("Unsupported write to render target %u\n", reg);
283 sprintf(regstr, "unsupported_register");
286 case WINED3DSPR_DEPTHOUT:
287 sprintf(regstr, "result.depth");
289 case WINED3DSPR_ATTROUT:
290 sprintf(regstr, "oD[%u]", reg);
292 case WINED3DSPR_TEXCRDOUT:
293 sprintf(regstr, "oT[%u]", reg);
296 FIXME("Unhandled register name Type(%d)\n", regtype);
297 sprintf(regstr, "unrecognized_register");
302 /* TODO: merge with pixel shader */
303 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
305 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
307 /* oPos, oFog and oPts in D3D */
308 static const char * const hwrastout_reg_names[] = { "TMP_OUT", "TMP_FOG", "result.pointsize" };
310 DWORD reg = param & WINED3DSP_REGNUM_MASK;
311 DWORD regtype = shader_get_regtype(param);
313 BOOL is_color = FALSE;
315 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
316 strcat(hwLine, " -");
322 case WINED3DSPR_TEMP:
323 sprintf(tmpReg, "R%u", reg);
324 strcat(hwLine, tmpReg);
326 case WINED3DSPR_INPUT:
328 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
331 sprintf(tmpReg, "vertex.attrib[%u]", reg);
332 strcat(hwLine, tmpReg);
334 case WINED3DSPR_CONST:
335 sprintf(tmpReg, "C[%s%u]", (param & WINED3DSHADER_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
336 strcat(hwLine, tmpReg);
338 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
339 sprintf(tmpReg, "A%u", reg);
340 strcat(hwLine, tmpReg);
342 case WINED3DSPR_RASTOUT:
343 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
344 strcat(hwLine, tmpReg);
346 case WINED3DSPR_ATTROUT:
348 strcat(hwLine, "result.color.primary");
350 strcat(hwLine, "result.color.secondary");
353 case WINED3DSPR_TEXCRDOUT:
354 sprintf(tmpReg, "result.texcoord[%u]", reg);
355 strcat(hwLine, tmpReg);
358 FIXME("Unknown reg type %d %d\n", regtype, reg);
359 strcat(hwLine, "unrecognized_register");
365 shader_arb_get_write_mask(param, write_mask);
366 strcat(hwLine, write_mask);
369 shader_arb_get_swizzle(param, is_color, swizzle);
370 strcat(hwLine, swizzle);
374 static void shader_hw_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
375 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
376 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
378 SHADER_BUFFER* buffer = arg->buffer;
379 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
380 const char *tex_type;
382 switch(sampler_type) {
391 case WINED3DSTT_VOLUME:
395 case WINED3DSTT_CUBE:
400 ERR("Unexpected texture type %d\n", sampler_type);
404 if (deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
405 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
407 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
412 static void pshader_gen_input_modifier_line (
413 SHADER_BUFFER* buffer,
418 /* Generate a line that does the input modifier computation and return the input register to use */
423 /* Assume a new line will be added */
426 /* Get register name */
427 pshader_get_register_name(instr, regstr);
428 shader_arb_get_swizzle(instr, FALSE, swzstr);
430 switch (instr & WINED3DSP_SRCMOD_MASK) {
431 case WINED3DSPSM_NONE:
432 sprintf(outregstr, "%s%s", regstr, swzstr);
435 case WINED3DSPSM_NEG:
436 sprintf(outregstr, "-%s%s", regstr, swzstr);
439 case WINED3DSPSM_BIAS:
440 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
442 case WINED3DSPSM_BIASNEG:
443 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
445 case WINED3DSPSM_SIGN:
446 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
448 case WINED3DSPSM_SIGNNEG:
449 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
451 case WINED3DSPSM_COMP:
452 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
455 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
457 case WINED3DSPSM_X2NEG:
458 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
461 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
462 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
465 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
466 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
469 sprintf(outregstr, "%s%s", regstr, swzstr);
473 /* Return modified or original register, with swizzle */
475 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
478 inline static void pshader_gen_output_modifier_line(
479 SHADER_BUFFER* buffer,
485 /* Generate a line that does the output modifier computation */
486 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
487 regstr, write_mask, regstr, shift_tab[shift]);
490 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
492 SHADER_BUFFER* buffer = arg->buffer;
495 char src_name[3][50];
497 /* FIXME: support output modifiers */
499 /* Handle output register */
500 pshader_get_register_name(arg->dst, dst_name);
501 shader_arb_get_write_mask(arg->dst, dst_wmask);
502 strcat(dst_name, dst_wmask);
504 /* Generate input register names (with modifiers) */
505 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
506 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
507 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
509 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
510 shader_addline(buffer, "CMP %s, TMP, %s, %s;\n", dst_name, src_name[1], src_name[2]);
513 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
515 SHADER_BUFFER* buffer = arg->buffer;
518 char src_name[3][50];
520 /* FIXME: support output modifiers */
522 /* Handle output register */
523 pshader_get_register_name(arg->dst, dst_name);
524 shader_arb_get_write_mask(arg->dst, dst_wmask);
525 strcat(dst_name, dst_wmask);
527 /* Generate input register names (with modifiers) */
528 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
529 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
530 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
532 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
533 src_name[0], src_name[2], src_name[1]);
536 /* Map the opcode 1-to-1 to the GL code */
537 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
539 CONST SHADER_OPCODE* curOpcode = arg->opcode;
540 SHADER_BUFFER* buffer = arg->buffer;
541 DWORD dst = arg->dst;
542 DWORD* src = arg->src;
547 /* Output token related */
548 char output_rname[256];
549 char output_wmask[20];
550 BOOL saturate = FALSE;
551 BOOL centroid = FALSE;
552 BOOL partialprecision = FALSE;
555 strcpy(tmpLine, curOpcode->glname);
557 /* Process modifiers */
558 if (0 != (dst & WINED3DSP_DSTMOD_MASK)) {
559 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
561 saturate = mask & WINED3DSPDM_SATURATE;
562 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
563 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
564 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
566 FIXME("Unrecognized modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
569 FIXME("Unhandled modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
571 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
573 /* Generate input and output registers */
574 if (curOpcode->num_params > 0) {
575 char operands[4][100];
577 /* Generate input register names (with modifiers) */
578 for (i = 1; i < curOpcode->num_params; ++i)
579 pshader_gen_input_modifier_line(buffer, src[i-1], i-1, operands[i]);
581 /* Handle output register */
582 pshader_get_register_name(dst, output_rname);
583 strcpy(operands[0], output_rname);
584 shader_arb_get_write_mask(dst, output_wmask);
585 strcat(operands[0], output_wmask);
587 if (saturate && (shift == 0))
588 strcat(tmpLine, "_SAT");
589 strcat(tmpLine, " ");
590 strcat(tmpLine, operands[0]);
591 for (i = 1; i < curOpcode->num_params; i++) {
592 strcat(tmpLine, ", ");
593 strcat(tmpLine, operands[i]);
595 strcat(tmpLine,";\n");
596 shader_addline(buffer, tmpLine);
598 /* A shift requires another line. */
600 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
604 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
606 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
608 DWORD dst = arg->dst;
609 DWORD* src = arg->src;
610 SHADER_BUFFER* buffer = arg->buffer;
611 DWORD hex_version = This->baseShader.hex_version;
616 DWORD reg_sampler_code;
618 /* All versions have a destination register */
619 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
620 pshader_get_register_name(dst, reg_dest);
622 /* 1.0-1.3: Use destination register as coordinate source.
623 1.4+: Use provided coordinate source register. */
624 if (hex_version < WINED3DPS_VERSION(1,4))
625 strcpy(reg_coord, reg_dest);
627 pshader_gen_input_modifier_line(buffer, src[0], 0, reg_coord);
629 /* 1.0-1.4: Use destination register number as texture code.
630 2.0+: Use provided sampler number as texure code. */
631 if (hex_version < WINED3DPS_VERSION(2,0))
632 reg_sampler_code = reg_dest_code;
634 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
636 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord);
639 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
641 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
642 DWORD dst = arg->dst;
643 DWORD* src = arg->src;
644 SHADER_BUFFER* buffer = arg->buffer;
645 DWORD hex_version = This->baseShader.hex_version;
648 shader_arb_get_write_mask(dst, tmp);
649 if (hex_version != WINED3DPS_VERSION(1,4)) {
650 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
651 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
653 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
654 DWORD reg2 = src[0] & WINED3DSP_REGNUM_MASK;
655 shader_addline(buffer, "MOV R%u%s, fragment.texcoord[%u];\n", reg1, tmp, reg2);
659 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg) {
661 SHADER_BUFFER* buffer = arg->buffer;
663 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
664 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
667 sprintf(dst_str, "T%u", reg1);
668 shader_addline(buffer, "MOV TMP.r, T%u.a;\n", reg2);
669 shader_addline(buffer, "MOV TMP.g, T%u.r;\n", reg2);
670 shader_hw_sample(arg, reg1, dst_str, "TMP");
673 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg) {
675 SHADER_BUFFER* buffer = arg->buffer;
677 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
678 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
681 sprintf(dst_str, "T%u", reg1);
682 shader_addline(buffer, "MOV TMP.r, T%u.g;\n", reg2);
683 shader_addline(buffer, "MOV TMP.g, T%u.b;\n", reg2);
684 shader_hw_sample(arg, reg1, dst_str, "TMP");
687 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
689 SHADER_BUFFER* buffer = arg->buffer;
690 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
691 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
694 /* FIXME: Should apply the BUMPMAPENV matrix */
695 sprintf(dst_str, "T%u", reg1);
696 shader_addline(buffer, "ADD TMP.rg, fragment.texcoord[%u], T%u;\n", reg1, reg2);
697 shader_hw_sample(arg, reg1, dst_str, "TMP");
700 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
702 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
703 SHADER_BUFFER* buffer = arg->buffer;
706 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
707 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
710 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
712 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
713 SHADER_BUFFER* buffer = arg->buffer;
717 sprintf(dst_str, "T%u", reg);
718 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
719 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
720 shader_hw_sample(arg, reg, dst_str, "TMP");
723 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
725 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
726 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
727 SHADER_BUFFER* buffer = arg->buffer;
728 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
731 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
732 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
733 current_state->texcoord_w[current_state->current_row++] = reg;
736 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
738 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
739 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
740 SHADER_BUFFER* buffer = arg->buffer;
741 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
745 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
746 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
748 /* Sample the texture using the calculated coordinates */
749 sprintf(dst_str, "T%u", reg);
750 shader_hw_sample(arg, reg, dst_str, "TMP");
751 current_state->current_row = 0;
754 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
756 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
757 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
758 SHADER_BUFFER* buffer = arg->buffer;
759 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
763 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
764 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
766 /* Construct the eye-ray vector from w coordinates */
767 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
768 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
769 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
771 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
772 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
773 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
774 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
776 /* Sample the texture using the calculated coordinates */
777 sprintf(dst_str, "T%u", reg);
778 shader_hw_sample(arg, reg, dst_str, "TMP");
779 current_state->current_row = 0;
782 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
784 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
785 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
786 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
787 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
788 SHADER_BUFFER* buffer = arg->buffer;
792 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
793 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
795 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
796 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
797 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
798 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
800 /* Sample the texture using the calculated coordinates */
801 sprintf(dst_str, "T%u", reg);
802 shader_hw_sample(arg, reg, dst_str, "TMP");
803 current_state->current_row = 0;
806 /** Handles transforming all WINED3DSIO_M?x? opcodes for
807 Vertex shaders to ARB_vertex_program codes */
808 void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
812 SHADER_OPCODE_ARG tmpArg;
814 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
816 /* Set constants for the temporary argument */
817 tmpArg.shader = arg->shader;
818 tmpArg.buffer = arg->buffer;
819 tmpArg.src[0] = arg->src[0];
820 tmpArg.src_addr[0] = arg->src_addr[0];
821 tmpArg.src_addr[1] = arg->src_addr[1];
822 tmpArg.reg_maps = arg->reg_maps;
824 switch(arg->opcode->opcode) {
825 case WINED3DSIO_M4x4:
827 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
829 case WINED3DSIO_M4x3:
831 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
833 case WINED3DSIO_M3x4:
835 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
837 case WINED3DSIO_M3x3:
839 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
841 case WINED3DSIO_M3x2:
843 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
849 for (i = 0; i < nComponents; i++) {
850 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
851 tmpArg.src[1] = arg->src[1]+i;
852 vshader_hw_map2gl(&tmpArg);
856 /* TODO: merge with pixel shader */
857 /* Map the opcode 1-to-1 to the GL code */
858 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
860 CONST SHADER_OPCODE* curOpcode = arg->opcode;
861 SHADER_BUFFER* buffer = arg->buffer;
862 DWORD dst = arg->dst;
863 DWORD* src = arg->src;
865 DWORD dst_regtype = shader_get_regtype(dst);
869 if ((curOpcode->opcode == WINED3DSIO_MOV && dst_regtype == WINED3DSPR_ADDR) || curOpcode->opcode == WINED3DSIO_MOVA)
870 strcpy(tmpLine, "ARL");
872 strcpy(tmpLine, curOpcode->glname);
874 if (curOpcode->num_params > 0) {
875 vshader_program_add_param(arg, dst, FALSE, tmpLine);
876 for (i = 1; i < curOpcode->num_params; ++i) {
877 strcat(tmpLine, ",");
878 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
881 shader_addline(buffer, "%s;\n", tmpLine);
884 static GLuint create_arb_blt_vertex_program(WineD3D_GL_Info *gl_info) {
885 GLuint program_id = 0;
886 const char *blt_vprogram =
888 "PARAM c[1] = { { 1, 0.5 } };\n"
889 "MOV result.position, vertex.position;\n"
890 "MOV result.color, c[0].x;\n"
891 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
892 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
895 GL_EXTCALL(glGenProgramsARB(1, &program_id));
896 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
897 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
899 if (glGetError() == GL_INVALID_OPERATION) {
901 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
902 FIXME("Vertex program error at position %d: %s\n", pos,
903 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
909 static GLuint create_arb_blt_fragment_program(WineD3D_GL_Info *gl_info) {
910 GLuint program_id = 0;
911 const char *blt_fprogram =
914 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
915 "MOV result.depth.z, R0.x;\n"
918 GL_EXTCALL(glGenProgramsARB(1, &program_id));
919 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
920 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprogram), blt_fprogram));
922 if (glGetError() == GL_INVALID_OPERATION) {
924 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
925 FIXME("Fragment program error at position %d: %s\n", pos,
926 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
932 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
933 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
934 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
937 TRACE("Using vertex shader\n");
939 /* Bind the vertex program */
940 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB,
941 ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId));
942 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
944 /* Enable OpenGL vertex programs */
945 glEnable(GL_VERTEX_PROGRAM_ARB);
946 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
947 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
948 This, ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId);
949 } else if(GL_SUPPORT(GL_VERTEX_PROGRAM_ARB)) {
950 glDisable(GL_VERTEX_PROGRAM_ARB);
951 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
955 TRACE("Using pixel shader\n");
957 /* Bind the fragment program */
958 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
959 ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId));
960 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
962 /* Enable OpenGL fragment programs */
963 glEnable(GL_FRAGMENT_PROGRAM_ARB);
964 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
965 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
966 This, ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId);
967 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
968 glDisable(GL_FRAGMENT_PROGRAM_ARB);
969 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
973 static void shader_arb_select_depth_blt(IWineD3DDevice *iface) {
974 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
975 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
976 static GLuint vprogram_id = 0;
977 static GLuint fprogram_id = 0;
979 if (!vprogram_id) vprogram_id = create_arb_blt_vertex_program(gl_info);
980 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vprogram_id));
981 glEnable(GL_VERTEX_PROGRAM_ARB);
983 if (!fprogram_id) fprogram_id = create_arb_blt_fragment_program(gl_info);
984 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprogram_id));
985 glEnable(GL_FRAGMENT_PROGRAM_ARB);
988 static void shader_arb_cleanup(BOOL usePS, BOOL useVS) {
989 if (useVS) glDisable(GL_VERTEX_PROGRAM_ARB);
990 if (usePS) glDisable(GL_FRAGMENT_PROGRAM_ARB);
993 const shader_backend_t arb_program_shader_backend = {
995 &shader_arb_select_depth_blt,
996 &shader_arb_load_constants,