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* 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 pshader_get_write_mask(const DWORD output_reg, char *write_mask) {
211 if ((output_reg & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
212 strcat(write_mask, ".");
213 if (output_reg & WINED3DSP_WRITEMASK_0) strcat(write_mask, "r");
214 if (output_reg & WINED3DSP_WRITEMASK_1) strcat(write_mask, "g");
215 if (output_reg & WINED3DSP_WRITEMASK_2) strcat(write_mask, "b");
216 if (output_reg & WINED3DSP_WRITEMASK_3) strcat(write_mask, "a");
220 /* TODO: merge with pixel shader */
221 static void vshader_program_add_output_param_swizzle(const DWORD param, int is_color, char *hwLine) {
222 /** operand output */
223 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
225 if (param & WINED3DSP_WRITEMASK_0) { strcat(hwLine, "x"); }
226 if (param & WINED3DSP_WRITEMASK_1) { strcat(hwLine, "y"); }
227 if (param & WINED3DSP_WRITEMASK_2) { strcat(hwLine, "z"); }
228 if (param & WINED3DSP_WRITEMASK_3) { strcat(hwLine, "w"); }
232 static void pshader_get_input_register_swizzle(const DWORD instr, char *swzstring) {
233 static const char swizzle_reg_chars[] = "rgba";
234 DWORD swizzle = (instr & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
235 DWORD swizzle_x = swizzle & 0x03;
236 DWORD swizzle_y = (swizzle >> 2) & 0x03;
237 DWORD swizzle_z = (swizzle >> 4) & 0x03;
238 DWORD swizzle_w = (swizzle >> 6) & 0x03;
240 * swizzle bits fields:
244 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle) {
245 if (swizzle_x == swizzle_y &&
246 swizzle_x == swizzle_z &&
247 swizzle_x == swizzle_w) {
248 sprintf(swzstring, ".%c", swizzle_reg_chars[swizzle_x]);
250 sprintf(swzstring, ".%c%c%c%c",
251 swizzle_reg_chars[swizzle_x],
252 swizzle_reg_chars[swizzle_y],
253 swizzle_reg_chars[swizzle_z],
254 swizzle_reg_chars[swizzle_w]);
259 /* TODO: merge with pixel shader */
260 static void vshader_program_add_input_param_swizzle(const DWORD param, int is_color, char *hwLine) {
261 static const char swizzle_reg_chars_color_fix[] = "zyxw";
262 static const char swizzle_reg_chars[] = "xyzw";
263 const char* swizzle_regs = NULL;
267 DWORD swizzle = (param & WINED3DVS_SWIZZLE_MASK) >> WINED3DVS_SWIZZLE_SHIFT;
268 DWORD swizzle_x = swizzle & 0x03;
269 DWORD swizzle_y = (swizzle >> 2) & 0x03;
270 DWORD swizzle_z = (swizzle >> 4) & 0x03;
271 DWORD swizzle_w = (swizzle >> 6) & 0x03;
274 swizzle_regs = swizzle_reg_chars_color_fix;
276 swizzle_regs = swizzle_reg_chars;
280 * swizzle bits fields:
283 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) == swizzle) {
285 sprintf(tmpReg, ".%c%c%c%c",
286 swizzle_regs[swizzle_x],
287 swizzle_regs[swizzle_y],
288 swizzle_regs[swizzle_z],
289 swizzle_regs[swizzle_w]);
290 strcat(hwLine, tmpReg);
294 if (swizzle_x == swizzle_y &&
295 swizzle_x == swizzle_z &&
296 swizzle_x == swizzle_w)
298 sprintf(tmpReg, ".%c", swizzle_regs[swizzle_x]);
299 strcat(hwLine, tmpReg);
301 sprintf(tmpReg, ".%c%c%c%c",
302 swizzle_regs[swizzle_x],
303 swizzle_regs[swizzle_y],
304 swizzle_regs[swizzle_z],
305 swizzle_regs[swizzle_w]);
306 strcat(hwLine, tmpReg);
310 static void pshader_get_register_name(
311 const DWORD param, char* regstr) {
313 DWORD reg = param & WINED3DSP_REGNUM_MASK;
314 DWORD regtype = shader_get_regtype(param);
317 case WINED3DSPR_TEMP:
318 sprintf(regstr, "R%u", reg);
320 case WINED3DSPR_INPUT:
322 strcpy(regstr, "fragment.color.primary");
324 strcpy(regstr, "fragment.color.secondary");
327 case WINED3DSPR_CONST:
328 sprintf(regstr, "C[%u]", reg);
330 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
331 sprintf(regstr,"T%u", reg);
333 case WINED3DSPR_COLOROUT:
335 sprintf(regstr, "result.color");
337 /* TODO: See GL_ARB_draw_buffers */
338 FIXME("Unsupported write to render target %u\n", reg);
339 sprintf(regstr, "unsupported_register");
342 case WINED3DSPR_DEPTHOUT:
343 sprintf(regstr, "result.depth");
345 case WINED3DSPR_ATTROUT:
346 sprintf(regstr, "oD[%u]", reg);
348 case WINED3DSPR_TEXCRDOUT:
349 sprintf(regstr, "oT[%u]", reg);
352 FIXME("Unhandled register name Type(%d)\n", regtype);
353 sprintf(regstr, "unrecognized_register");
358 /* TODO: merge with pixel shader */
359 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
361 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
363 /* oPos, oFog and oPts in D3D */
364 static const char* hwrastout_reg_names[] = { "TMP_OUT", "TMP_FOG", "result.pointsize" };
366 DWORD reg = param & WINED3DSP_REGNUM_MASK;
367 DWORD regtype = shader_get_regtype(param);
369 BOOL is_color = FALSE;
371 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
372 strcat(hwLine, " -");
378 case WINED3DSPR_TEMP:
379 sprintf(tmpReg, "R%u", reg);
380 strcat(hwLine, tmpReg);
382 case WINED3DSPR_INPUT:
384 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
387 sprintf(tmpReg, "vertex.attrib[%u]", reg);
388 strcat(hwLine, tmpReg);
390 case WINED3DSPR_CONST:
391 sprintf(tmpReg, "C[%s%u]", (param & WINED3DSHADER_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
392 strcat(hwLine, tmpReg);
394 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
395 sprintf(tmpReg, "A%u", reg);
396 strcat(hwLine, tmpReg);
398 case WINED3DSPR_RASTOUT:
399 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
400 strcat(hwLine, tmpReg);
402 case WINED3DSPR_ATTROUT:
404 strcat(hwLine, "result.color.primary");
406 strcat(hwLine, "result.color.secondary");
409 case WINED3DSPR_TEXCRDOUT:
410 sprintf(tmpReg, "result.texcoord[%u]", reg);
411 strcat(hwLine, tmpReg);
414 FIXME("Unknown reg type %d %d\n", regtype, reg);
415 strcat(hwLine, "unrecognized_register");
420 vshader_program_add_output_param_swizzle(param, is_color, hwLine);
422 vshader_program_add_input_param_swizzle(param, is_color, hwLine);
426 static void shader_hw_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
427 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
428 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
430 SHADER_BUFFER* buffer = arg->buffer;
431 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
432 const char *tex_type;
434 switch(sampler_type) {
443 case WINED3DSTT_VOLUME:
447 case WINED3DSTT_CUBE:
452 ERR("Unexpected texture type %d\n", sampler_type);
456 if (deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
457 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
459 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
464 static void pshader_gen_input_modifier_line (
465 SHADER_BUFFER* buffer,
470 /* Generate a line that does the input modifier computation and return the input register to use */
475 /* Assume a new line will be added */
478 /* Get register name */
479 pshader_get_register_name(instr, regstr);
480 pshader_get_input_register_swizzle(instr, swzstr);
482 switch (instr & WINED3DSP_SRCMOD_MASK) {
483 case WINED3DSPSM_NONE:
484 sprintf(outregstr, "%s%s", regstr, swzstr);
487 case WINED3DSPSM_NEG:
488 sprintf(outregstr, "-%s%s", regstr, swzstr);
491 case WINED3DSPSM_BIAS:
492 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
494 case WINED3DSPSM_BIASNEG:
495 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
497 case WINED3DSPSM_SIGN:
498 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
500 case WINED3DSPSM_SIGNNEG:
501 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
503 case WINED3DSPSM_COMP:
504 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
507 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
509 case WINED3DSPSM_X2NEG:
510 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
513 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
514 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
517 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
518 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
521 sprintf(outregstr, "%s%s", regstr, swzstr);
525 /* Return modified or original register, with swizzle */
527 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
530 inline static void pshader_gen_output_modifier_line(
531 SHADER_BUFFER* buffer,
537 /* Generate a line that does the output modifier computation */
538 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
539 regstr, write_mask, regstr, shift_tab[shift]);
542 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
544 SHADER_BUFFER* buffer = arg->buffer;
547 char src_name[3][50];
549 /* FIXME: support output modifiers */
551 /* Handle output register */
552 pshader_get_register_name(arg->dst, dst_name);
553 pshader_get_write_mask(arg->dst, dst_wmask);
554 strcat(dst_name, dst_wmask);
556 /* Generate input register names (with modifiers) */
557 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
558 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
559 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
561 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
562 shader_addline(buffer, "CMP %s, TMP, %s, %s;\n", dst_name, src_name[1], src_name[2]);
565 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
567 SHADER_BUFFER* buffer = arg->buffer;
570 char src_name[3][50];
572 /* FIXME: support output modifiers */
574 /* Handle output register */
575 pshader_get_register_name(arg->dst, dst_name);
576 pshader_get_write_mask(arg->dst, dst_wmask);
577 strcat(dst_name, dst_wmask);
579 /* Generate input register names (with modifiers) */
580 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
581 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
582 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
584 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
585 src_name[0], src_name[2], src_name[1]);
588 /* Map the opcode 1-to-1 to the GL code */
589 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
591 CONST SHADER_OPCODE* curOpcode = arg->opcode;
592 SHADER_BUFFER* buffer = arg->buffer;
593 DWORD dst = arg->dst;
594 DWORD* src = arg->src;
599 /* Output token related */
600 char output_rname[256];
601 char output_wmask[20];
602 BOOL saturate = FALSE;
603 BOOL centroid = FALSE;
604 BOOL partialprecision = FALSE;
607 strcpy(tmpLine, curOpcode->glname);
609 /* Process modifiers */
610 if (0 != (dst & WINED3DSP_DSTMOD_MASK)) {
611 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
613 saturate = mask & WINED3DSPDM_SATURATE;
614 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
615 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
616 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
618 FIXME("Unrecognized modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
621 FIXME("Unhandled modifier(0x%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
623 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
625 /* Generate input and output registers */
626 if (curOpcode->num_params > 0) {
627 char operands[4][100];
629 /* Generate input register names (with modifiers) */
630 for (i = 1; i < curOpcode->num_params; ++i)
631 pshader_gen_input_modifier_line(buffer, src[i-1], i-1, operands[i]);
633 /* Handle output register */
634 pshader_get_register_name(dst, output_rname);
635 strcpy(operands[0], output_rname);
636 pshader_get_write_mask(dst, output_wmask);
637 strcat(operands[0], output_wmask);
639 if (saturate && (shift == 0))
640 strcat(tmpLine, "_SAT");
641 strcat(tmpLine, " ");
642 strcat(tmpLine, operands[0]);
643 for (i = 1; i < curOpcode->num_params; i++) {
644 strcat(tmpLine, ", ");
645 strcat(tmpLine, operands[i]);
647 strcat(tmpLine,";\n");
648 shader_addline(buffer, tmpLine);
650 /* A shift requires another line. */
652 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
656 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
658 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
660 DWORD dst = arg->dst;
661 DWORD* src = arg->src;
662 SHADER_BUFFER* buffer = arg->buffer;
663 DWORD hex_version = This->baseShader.hex_version;
668 DWORD reg_sampler_code;
670 /* All versions have a destination register */
671 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
672 pshader_get_register_name(dst, reg_dest);
674 /* 1.0-1.3: Use destination register as coordinate source.
675 1.4+: Use provided coordinate source register. */
676 if (hex_version < WINED3DPS_VERSION(1,4))
677 strcpy(reg_coord, reg_dest);
679 pshader_gen_input_modifier_line(buffer, src[0], 0, reg_coord);
681 /* 1.0-1.4: Use destination register number as texture code.
682 2.0+: Use provided sampler number as texure code. */
683 if (hex_version < WINED3DPS_VERSION(2,0))
684 reg_sampler_code = reg_dest_code;
686 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
688 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord);
691 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
693 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
694 DWORD dst = arg->dst;
695 DWORD* src = arg->src;
696 SHADER_BUFFER* buffer = arg->buffer;
697 DWORD hex_version = This->baseShader.hex_version;
700 pshader_get_write_mask(dst, tmp);
701 if (hex_version != WINED3DPS_VERSION(1,4)) {
702 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
703 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
705 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
706 DWORD reg2 = src[0] & WINED3DSP_REGNUM_MASK;
707 shader_addline(buffer, "MOV R%u%s, fragment.texcoord[%u];\n", reg1, tmp, reg2);
711 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg) {
713 SHADER_BUFFER* buffer = arg->buffer;
715 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
716 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
719 sprintf(dst_str, "T%u", reg1);
720 shader_addline(buffer, "MOV TMP.r, T%u.a;\n", reg2);
721 shader_addline(buffer, "MOV TMP.g, T%u.r;\n", reg2);
722 shader_hw_sample(arg, reg1, dst_str, "TMP");
725 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg) {
727 SHADER_BUFFER* buffer = arg->buffer;
729 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
730 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
733 sprintf(dst_str, "T%u", reg1);
734 shader_addline(buffer, "MOV TMP.r, T%u.g;\n", reg2);
735 shader_addline(buffer, "MOV TMP.g, T%u.b;\n", reg2);
736 shader_hw_sample(arg, reg1, dst_str, "TMP");
739 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
741 SHADER_BUFFER* buffer = arg->buffer;
742 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
743 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
746 /* FIXME: Should apply the BUMPMAPENV matrix */
747 sprintf(dst_str, "T%u", reg1);
748 shader_addline(buffer, "ADD TMP.rg, fragment.texcoord[%u], T%u;\n", reg1, reg2);
749 shader_hw_sample(arg, reg1, dst_str, "TMP");
752 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
754 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
755 SHADER_BUFFER* buffer = arg->buffer;
758 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
759 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
762 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
764 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
765 SHADER_BUFFER* buffer = arg->buffer;
769 sprintf(dst_str, "T%u", reg);
770 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
771 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
772 shader_hw_sample(arg, reg, dst_str, "TMP");
775 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
777 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
778 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
779 SHADER_BUFFER* buffer = arg->buffer;
780 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
783 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
784 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
785 current_state->texcoord_w[current_state->current_row++] = reg;
788 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
790 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
791 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
792 SHADER_BUFFER* buffer = arg->buffer;
793 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
797 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
798 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
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 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
808 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
809 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
810 SHADER_BUFFER* buffer = arg->buffer;
811 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
815 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
816 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
818 /* Construct the eye-ray vector from w coordinates */
819 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
820 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
821 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
823 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
824 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
825 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
826 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
828 /* Sample the texture using the calculated coordinates */
829 sprintf(dst_str, "T%u", reg);
830 shader_hw_sample(arg, reg, dst_str, "TMP");
831 current_state->current_row = 0;
834 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
836 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
837 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
838 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
839 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
840 SHADER_BUFFER* buffer = arg->buffer;
844 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
845 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
847 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
848 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
849 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
850 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
852 /* Sample the texture using the calculated coordinates */
853 sprintf(dst_str, "T%u", reg);
854 shader_hw_sample(arg, reg, dst_str, "TMP");
855 current_state->current_row = 0;
858 /** Handles transforming all WINED3DSIO_M?x? opcodes for
859 Vertex shaders to ARB_vertex_program codes */
860 void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
864 SHADER_OPCODE_ARG tmpArg;
866 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
868 /* Set constants for the temporary argument */
869 tmpArg.shader = arg->shader;
870 tmpArg.buffer = arg->buffer;
871 tmpArg.src[0] = arg->src[0];
872 tmpArg.src_addr[0] = arg->src_addr[0];
873 tmpArg.src_addr[1] = arg->src_addr[1];
874 tmpArg.reg_maps = arg->reg_maps;
876 switch(arg->opcode->opcode) {
877 case WINED3DSIO_M4x4:
879 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
881 case WINED3DSIO_M4x3:
883 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
885 case WINED3DSIO_M3x4:
887 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
889 case WINED3DSIO_M3x3:
891 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
893 case WINED3DSIO_M3x2:
895 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
901 for (i = 0; i < nComponents; i++) {
902 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
903 tmpArg.src[1] = arg->src[1]+i;
904 vshader_hw_map2gl(&tmpArg);
908 /* TODO: merge with pixel shader */
909 /* Map the opcode 1-to-1 to the GL code */
910 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
912 CONST SHADER_OPCODE* curOpcode = arg->opcode;
913 SHADER_BUFFER* buffer = arg->buffer;
914 DWORD dst = arg->dst;
915 DWORD* src = arg->src;
917 DWORD dst_regtype = shader_get_regtype(dst);
921 if ((curOpcode->opcode == WINED3DSIO_MOV && dst_regtype == WINED3DSPR_ADDR) || curOpcode->opcode == WINED3DSIO_MOVA)
922 strcpy(tmpLine, "ARL");
924 strcpy(tmpLine, curOpcode->glname);
926 if (curOpcode->num_params > 0) {
927 vshader_program_add_param(arg, dst, FALSE, tmpLine);
928 for (i = 1; i < curOpcode->num_params; ++i) {
929 strcat(tmpLine, ",");
930 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
933 shader_addline(buffer, "%s;\n", tmpLine);
936 static GLuint create_arb_blt_vertex_program(WineD3D_GL_Info *gl_info) {
937 GLuint program_id = 0;
938 const char *blt_vprogram =
940 "PARAM c[1] = { { 1, 0.5 } };\n"
941 "MOV result.position, vertex.position;\n"
942 "MOV result.color, c[0].x;\n"
943 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
944 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
947 GL_EXTCALL(glGenProgramsARB(1, &program_id));
948 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
949 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
951 if (glGetError() == GL_INVALID_OPERATION) {
953 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
954 FIXME("Vertex program error at position %d: %s\n", pos,
955 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
961 static GLuint create_arb_blt_fragment_program(WineD3D_GL_Info *gl_info) {
962 GLuint program_id = 0;
963 const char *blt_fprogram =
966 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
967 "MOV result.depth.z, R0.x;\n"
970 GL_EXTCALL(glGenProgramsARB(1, &program_id));
971 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
972 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprogram), blt_fprogram));
974 if (glGetError() == GL_INVALID_OPERATION) {
976 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
977 FIXME("Fragment program error at position %d: %s\n", pos,
978 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
984 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
985 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
986 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
989 TRACE("Using vertex shader\n");
991 /* Bind the vertex program */
992 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB,
993 ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId));
994 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
996 /* Enable OpenGL vertex programs */
997 glEnable(GL_VERTEX_PROGRAM_ARB);
998 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
999 TRACE_(d3d_shader)("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
1000 This, ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId);
1004 TRACE("Using pixel shader\n");
1006 /* Bind the fragment program */
1007 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
1008 ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId));
1009 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1011 /* Enable OpenGL fragment programs */
1012 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1013 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1014 TRACE_(d3d_shader)("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
1015 This, ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId);
1019 static void shader_arb_select_depth_blt(IWineD3DDevice *iface) {
1020 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1021 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1022 static GLuint vprogram_id = 0;
1023 static GLuint fprogram_id = 0;
1025 if (!vprogram_id) vprogram_id = create_arb_blt_vertex_program(gl_info);
1026 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vprogram_id));
1027 glEnable(GL_VERTEX_PROGRAM_ARB);
1029 if (!fprogram_id) fprogram_id = create_arb_blt_fragment_program(gl_info);
1030 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprogram_id));
1031 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1034 static void shader_arb_cleanup(BOOL usePS, BOOL useVS) {
1035 if (useVS) glDisable(GL_VERTEX_PROGRAM_ARB);
1036 if (usePS) glDisable(GL_FRAGMENT_PROGRAM_ARB);
1039 const shader_backend_t arb_program_shader_backend = {
1041 &shader_arb_select_depth_blt,
1042 &shader_arb_load_constants,