2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
38 #define GLINFO_LOCATION (*gl_info)
40 /********************************************************
41 * ARB_[vertex/fragment]_program helper functions follow
42 ********************************************************/
45 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
46 * When constant_list == NULL, it will load all the constants.
48 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
49 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
51 static void shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info, GLuint target_type,
52 unsigned int max_constants, float* constants, struct list *constant_list) {
53 constants_entry *constant;
54 local_constant* lconst;
58 if (TRACE_ON(d3d_shader)) {
59 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
64 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
65 constants[i * 4 + 0], constants[i * 4 + 1],
66 constants[i * 4 + 2], constants[i * 4 + 3]);
70 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
75 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
78 checkGLcall("glProgramEnvParameter4fvARB()");
80 /* Load immediate constants */
81 if (TRACE_ON(d3d_shader)) {
82 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
83 GLfloat* values = (GLfloat*)lconst->value;
84 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
85 values[0], values[1], values[2], values[3]);
88 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
89 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
91 checkGLcall("glProgramEnvParameter4fvARB()");
95 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
97 * We only support float constants in ARB at the moment, so don't
98 * worry about the Integers or Booleans
100 void shader_arb_load_constants(
101 IWineD3DDevice* device,
103 char useVertexShader) {
105 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
106 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
107 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
109 if (useVertexShader) {
110 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
112 /* Load DirectX 9 float constants for vertex shader */
113 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
114 GL_LIMITS(vshader_constantsF),
115 stateBlock->vertexShaderConstantF,
116 &stateBlock->set_vconstantsF);
118 /* Upload the position fixup */
119 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
122 if (usePixelShader) {
124 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
126 /* Load DirectX 9 float constants for pixel shader */
127 shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
128 GL_LIMITS(pshader_constantsF),
129 stateBlock->pixelShaderConstantF,
130 &stateBlock->set_pconstantsF);
131 if(((IWineD3DPixelShaderImpl *) pshader)->bumpenvmatconst) {
132 /* needsbumpmat stores the stage number from where to load the matrix. bumpenvmatconst stores the
133 * number of the constant to load the matrix into.
134 * The state manager takes care that this function is always called if the bump env matrix changes
136 IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
137 float *data = (float *) &stateBlock->textureState[(int) psi->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
138 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst, data));
143 /* Generate the variable & register declarations for the ARB_vertex_program output target */
144 void shader_generate_arb_declarations(
145 IWineD3DBaseShader *iface,
146 shader_reg_maps* reg_maps,
147 SHADER_BUFFER* buffer,
148 WineD3D_GL_Info* gl_info) {
150 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
152 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
153 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
154 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
156 /* Temporary Output register */
157 shader_addline(buffer, "TEMP TMP_OUT;\n");
159 for(i = 0; i < This->baseShader.limits.temporary; i++) {
160 if (reg_maps->temporary[i])
161 shader_addline(buffer, "TEMP R%u;\n", i);
164 for (i = 0; i < This->baseShader.limits.address; i++) {
165 if (reg_maps->address[i])
166 shader_addline(buffer, "ADDRESS A%d;\n", i);
169 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
170 if (reg_maps->texcoord[i])
171 shader_addline(buffer,"TEMP T%u;\n", i);
174 /* Texture coordinate registers must be pre-loaded */
175 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
176 if (reg_maps->texcoord[i])
177 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
180 if(reg_maps->bumpmat != -1 /* Only a pshader can use texbem */) {
181 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
182 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
185 if(max_constantsF < GL_LIMITS(pshader_constantsF)) {
186 ((IWineD3DPixelShaderImpl *)This)->bumpenvmatconst = max_constantsF;
187 shader_addline(buffer, "PARAM bumpenvmat = program.env[%d];\n", ((IWineD3DPixelShaderImpl *)This)->bumpenvmatconst);
189 FIXME("No free constant found to load environemnt bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
193 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
194 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
195 max_constantsF, max_constantsF - 1);
198 static const char * const shift_tab[] = {
199 "dummy", /* 0 (none) */
200 "coefmul.x", /* 1 (x2) */
201 "coefmul.y", /* 2 (x4) */
202 "coefmul.z", /* 3 (x8) */
203 "coefmul.w", /* 4 (x16) */
204 "dummy", /* 5 (x32) */
205 "dummy", /* 6 (x64) */
206 "dummy", /* 7 (x128) */
207 "dummy", /* 8 (d256) */
208 "dummy", /* 9 (d128) */
209 "dummy", /* 10 (d64) */
210 "dummy", /* 11 (d32) */
211 "coefdiv.w", /* 12 (d16) */
212 "coefdiv.z", /* 13 (d8) */
213 "coefdiv.y", /* 14 (d4) */
214 "coefdiv.x" /* 15 (d2) */
217 static void shader_arb_get_write_mask(const DWORD param, char *write_mask) {
218 char *ptr = write_mask;
220 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
222 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
223 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
224 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
225 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
231 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
232 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
233 * but addressed as "rgba". To fix this we need to swap the register's x
234 * and z components. */
235 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
236 char *ptr = swizzle_str;
238 /* swizzle bits fields: wwzzyyxx */
239 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
240 DWORD swizzle_x = swizzle & 0x03;
241 DWORD swizzle_y = (swizzle >> 2) & 0x03;
242 DWORD swizzle_z = (swizzle >> 4) & 0x03;
243 DWORD swizzle_w = (swizzle >> 6) & 0x03;
245 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
246 * generate a swizzle string. Unless we need to our own swizzling. */
247 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
249 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
250 *ptr++ = swizzle_chars[swizzle_x];
252 *ptr++ = swizzle_chars[swizzle_x];
253 *ptr++ = swizzle_chars[swizzle_y];
254 *ptr++ = swizzle_chars[swizzle_z];
255 *ptr++ = swizzle_chars[swizzle_w];
262 static void pshader_get_register_name(
263 const DWORD param, char* regstr) {
265 DWORD reg = param & WINED3DSP_REGNUM_MASK;
266 DWORD regtype = shader_get_regtype(param);
269 case WINED3DSPR_TEMP:
270 sprintf(regstr, "R%u", reg);
272 case WINED3DSPR_INPUT:
274 strcpy(regstr, "fragment.color.primary");
276 strcpy(regstr, "fragment.color.secondary");
279 case WINED3DSPR_CONST:
280 sprintf(regstr, "C[%u]", reg);
282 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
283 sprintf(regstr,"T%u", reg);
285 case WINED3DSPR_COLOROUT:
287 sprintf(regstr, "TMP_COLOR");
289 /* TODO: See GL_ARB_draw_buffers */
290 FIXME("Unsupported write to render target %u\n", reg);
291 sprintf(regstr, "unsupported_register");
294 case WINED3DSPR_DEPTHOUT:
295 sprintf(regstr, "result.depth");
297 case WINED3DSPR_ATTROUT:
298 sprintf(regstr, "oD[%u]", reg);
300 case WINED3DSPR_TEXCRDOUT:
301 sprintf(regstr, "oT[%u]", reg);
304 FIXME("Unhandled register name Type(%d)\n", regtype);
305 sprintf(regstr, "unrecognized_register");
310 /* TODO: merge with pixel shader */
311 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
313 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
315 /* oPos, oFog and oPts in D3D */
316 static const char * const hwrastout_reg_names[] = { "TMP_OUT", "result.fogcoord", "result.pointsize" };
318 DWORD reg = param & WINED3DSP_REGNUM_MASK;
319 DWORD regtype = shader_get_regtype(param);
321 BOOL is_color = FALSE;
323 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
324 strcat(hwLine, " -");
330 case WINED3DSPR_TEMP:
331 sprintf(tmpReg, "R%u", reg);
332 strcat(hwLine, tmpReg);
334 case WINED3DSPR_INPUT:
336 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
339 sprintf(tmpReg, "vertex.attrib[%u]", reg);
340 strcat(hwLine, tmpReg);
342 case WINED3DSPR_CONST:
343 sprintf(tmpReg, "C[%s%u]", (param & WINED3DSHADER_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
344 strcat(hwLine, tmpReg);
346 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
347 sprintf(tmpReg, "A%u", reg);
348 strcat(hwLine, tmpReg);
350 case WINED3DSPR_RASTOUT:
351 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
352 strcat(hwLine, tmpReg);
354 case WINED3DSPR_ATTROUT:
356 strcat(hwLine, "result.color.primary");
358 strcat(hwLine, "result.color.secondary");
361 case WINED3DSPR_TEXCRDOUT:
362 sprintf(tmpReg, "result.texcoord[%u]", reg);
363 strcat(hwLine, tmpReg);
366 FIXME("Unknown reg type %d %d\n", regtype, reg);
367 strcat(hwLine, "unrecognized_register");
373 shader_arb_get_write_mask(param, write_mask);
374 strcat(hwLine, write_mask);
377 shader_arb_get_swizzle(param, is_color, swizzle);
378 strcat(hwLine, swizzle);
382 static void shader_hw_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg, BOOL projective) {
383 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
384 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
385 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
386 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
388 SHADER_BUFFER* buffer = arg->buffer;
389 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
390 const char *tex_type;
392 switch(sampler_type) {
401 case WINED3DSTT_VOLUME:
405 case WINED3DSTT_CUBE:
410 ERR("Unexpected texture type %d\n", sampler_type);
414 if (projective && deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
415 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
417 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
420 /* Signedness correction */
421 if(!GL_SUPPORT(NV_TEXTURE_SHADER3) /* Provides signed formats */ && texture) {
422 WINED3DFORMAT format = texture->resource.format;
424 if((format == WINED3DFMT_V8U8 && !GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) ||
425 format == WINED3DFMT_Q8W8V8U8 ||
426 format == WINED3DFMT_V16U16) {
427 shader_addline(buffer, "MAD %s, %s, coefmul.x, -one;\n", dst_str, dst_str);
428 } else if(format == WINED3DFMT_X8L8V8U8) {
429 shader_addline(buffer, "MAD %s.rg, %s, coefmul.x, -one;\n", dst_str, dst_str);
435 static void pshader_gen_input_modifier_line (
436 SHADER_BUFFER* buffer,
441 /* Generate a line that does the input modifier computation and return the input register to use */
446 /* Assume a new line will be added */
449 /* Get register name */
450 pshader_get_register_name(instr, regstr);
451 shader_arb_get_swizzle(instr, FALSE, swzstr);
453 switch (instr & WINED3DSP_SRCMOD_MASK) {
454 case WINED3DSPSM_NONE:
455 sprintf(outregstr, "%s%s", regstr, swzstr);
458 case WINED3DSPSM_NEG:
459 sprintf(outregstr, "-%s%s", regstr, swzstr);
462 case WINED3DSPSM_BIAS:
463 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
465 case WINED3DSPSM_BIASNEG:
466 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
468 case WINED3DSPSM_SIGN:
469 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
471 case WINED3DSPSM_SIGNNEG:
472 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
474 case WINED3DSPSM_COMP:
475 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
478 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
480 case WINED3DSPSM_X2NEG:
481 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
484 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
485 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
488 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
489 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
492 sprintf(outregstr, "%s%s", regstr, swzstr);
496 /* Return modified or original register, with swizzle */
498 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
501 static inline void pshader_gen_output_modifier_line(
502 SHADER_BUFFER* buffer,
508 /* Generate a line that does the output modifier computation */
509 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
510 regstr, write_mask, regstr, shift_tab[shift]);
513 void pshader_hw_bem(SHADER_OPCODE_ARG* arg) {
514 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
516 SHADER_BUFFER* buffer = arg->buffer;
518 char src_name[2][50];
521 pshader_get_register_name(arg->dst, dst_name);
522 shader_arb_get_write_mask(arg->dst, dst_wmask);
523 strcat(dst_name, dst_wmask);
525 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
526 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
528 if(This->bumpenvmatconst != -1) {
529 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
530 shader_addline(buffer, "SWZ TMP2, bumpenvmat, x, z, 0, 0;\n");
531 shader_addline(buffer, "DP3 TMP.r, TMP2, %s;\n", src_name[1]);
532 shader_addline(buffer, "SWZ TMP2, bumpenvmat, y, w, 0, 0;\n");
533 shader_addline(buffer, "DP3 TMP.g, TMP2, %s;\n", src_name[1]);
535 shader_addline(buffer, "ADD %s, %s, TMP;\n", dst_name, src_name[0]);
537 shader_addline(buffer, "MOV %s, %s;\n", dst_name, src_name[0]);
541 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
543 SHADER_BUFFER* buffer = arg->buffer;
546 char src_name[3][50];
548 /* FIXME: support output modifiers */
550 /* Handle output register */
551 pshader_get_register_name(arg->dst, dst_name);
552 shader_arb_get_write_mask(arg->dst, dst_wmask);
553 strcat(dst_name, dst_wmask);
555 /* Generate input register names (with modifiers) */
556 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
557 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
558 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
560 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
561 shader_addline(buffer, "CMP %s, TMP, %s, %s;\n", dst_name, src_name[1], src_name[2]);
564 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
566 SHADER_BUFFER* buffer = arg->buffer;
569 char src_name[3][50];
571 /* FIXME: support output modifiers */
573 /* Handle output register */
574 pshader_get_register_name(arg->dst, dst_name);
575 shader_arb_get_write_mask(arg->dst, dst_wmask);
576 strcat(dst_name, dst_wmask);
578 /* Generate input register names (with modifiers) */
579 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
580 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
581 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
583 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
584 src_name[0], src_name[2], src_name[1]);
587 /* Map the opcode 1-to-1 to the GL code */
588 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
590 CONST SHADER_OPCODE* curOpcode = arg->opcode;
591 SHADER_BUFFER* buffer = arg->buffer;
592 DWORD dst = arg->dst;
593 DWORD* src = arg->src;
598 /* Output token related */
599 char output_rname[256];
600 char output_wmask[20];
601 BOOL saturate = FALSE;
602 BOOL centroid = FALSE;
603 BOOL partialprecision = FALSE;
606 strcpy(tmpLine, curOpcode->glname);
608 /* Process modifiers */
609 if (0 != (dst & WINED3DSP_DSTMOD_MASK)) {
610 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
612 saturate = mask & WINED3DSPDM_SATURATE;
613 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
614 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
615 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
617 FIXME("Unrecognized modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
620 FIXME("Unhandled modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
622 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
624 /* Generate input and output registers */
625 if (curOpcode->num_params > 0) {
626 char operands[4][100];
628 /* Generate input register names (with modifiers) */
629 for (i = 1; i < curOpcode->num_params; ++i)
630 pshader_gen_input_modifier_line(buffer, src[i-1], i-1, operands[i]);
632 /* Handle output register */
633 pshader_get_register_name(dst, output_rname);
634 strcpy(operands[0], output_rname);
635 shader_arb_get_write_mask(dst, output_wmask);
636 strcat(operands[0], output_wmask);
638 if (saturate && (shift == 0))
639 strcat(tmpLine, "_SAT");
640 strcat(tmpLine, " ");
641 strcat(tmpLine, operands[0]);
642 for (i = 1; i < curOpcode->num_params; i++) {
643 strcat(tmpLine, ", ");
644 strcat(tmpLine, operands[i]);
646 strcat(tmpLine,";\n");
647 shader_addline(buffer, tmpLine);
649 /* A shift requires another line. */
651 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
655 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
657 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
659 DWORD dst = arg->dst;
660 DWORD* src = arg->src;
661 SHADER_BUFFER* buffer = arg->buffer;
662 DWORD hex_version = This->baseShader.hex_version;
667 DWORD reg_sampler_code;
669 /* All versions have a destination register */
670 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
671 pshader_get_register_name(dst, reg_dest);
673 /* 1.0-1.3: Use destination register as coordinate source.
674 1.4+: Use provided coordinate source register. */
675 if (hex_version < WINED3DPS_VERSION(1,4))
676 strcpy(reg_coord, reg_dest);
678 pshader_gen_input_modifier_line(buffer, src[0], 0, reg_coord);
680 /* 1.0-1.4: Use destination register number as texture code.
681 2.0+: Use provided sampler number as texure code. */
682 if (hex_version < WINED3DPS_VERSION(2,0))
683 reg_sampler_code = reg_dest_code;
685 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
687 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord, TRUE);
690 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
692 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
693 DWORD dst = arg->dst;
694 SHADER_BUFFER* buffer = arg->buffer;
695 DWORD hex_version = This->baseShader.hex_version;
698 shader_arb_get_write_mask(dst, tmp);
699 if (hex_version != WINED3DPS_VERSION(1,4)) {
700 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
701 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
703 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
706 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, reg_src);
707 shader_addline(buffer, "MOV R%u%s, %s;\n", reg1, tmp, reg_src);
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", TRUE);
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", TRUE);
739 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
740 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
742 DWORD dst = arg->dst;
743 DWORD src = arg->src[0] & WINED3DSP_REGNUM_MASK;
744 SHADER_BUFFER* buffer = arg->buffer;
749 /* All versions have a destination register */
750 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
751 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
752 pshader_get_register_name(dst, reg_coord);
754 if(This->bumpenvmatconst != -1) {
755 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
757 shader_addline(buffer, "SWZ TMP2, bumpenvmat, x, z, 0, 0;\n");
758 shader_addline(buffer, "DP3 TMP.r, TMP2, T%u;\n", src);
759 shader_addline(buffer, "SWZ TMP2, bumpenvmat, y, w, 0, 0;\n");
760 shader_addline(buffer, "DP3 TMP.g, TMP2, T%u;\n", src);
762 /* with projective textures, texbem only divides the static texture coord, not the displacement,
763 * so we can't let the GL handle this.
765 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
766 & WINED3DTTFF_PROJECTED) {
767 shader_addline(buffer, "RCP TMP2.a, %s.a;\n", reg_coord);
768 shader_addline(buffer, "MUL TMP2.rg, %s, TMP2.a;\n", reg_coord);
769 shader_addline(buffer, "ADD TMP.rg, TMP, TMP2;\n");
771 shader_addline(buffer, "ADD TMP.rg, TMP, %s;\n", reg_coord);
774 shader_hw_sample(arg, reg_dest_code, reg_coord, "TMP", FALSE);
776 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
777 shader_hw_sample(arg, reg_dest_code, reg_coord, reg_coord, TRUE);
781 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
783 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
784 SHADER_BUFFER* buffer = arg->buffer;
787 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
788 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
791 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
793 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
794 SHADER_BUFFER* buffer = arg->buffer;
798 sprintf(dst_str, "T%u", reg);
799 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
800 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
801 shader_hw_sample(arg, reg, dst_str, "TMP", TRUE);
804 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
806 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
807 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
808 SHADER_BUFFER* buffer = arg->buffer;
809 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
812 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
813 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
814 current_state->texcoord_w[current_state->current_row++] = reg;
817 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
819 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
820 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
821 SHADER_BUFFER* buffer = arg->buffer;
822 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
826 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
827 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
829 /* Sample the texture using the calculated coordinates */
830 sprintf(dst_str, "T%u", reg);
831 shader_hw_sample(arg, reg, dst_str, "TMP", TRUE);
832 current_state->current_row = 0;
835 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
837 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
838 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
839 SHADER_BUFFER* buffer = arg->buffer;
840 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
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 /* Construct the eye-ray vector from w coordinates */
848 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
849 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
850 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
852 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
853 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
854 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
855 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
857 /* Sample the texture using the calculated coordinates */
858 sprintf(dst_str, "T%u", reg);
859 shader_hw_sample(arg, reg, dst_str, "TMP", TRUE);
860 current_state->current_row = 0;
863 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
865 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
866 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
867 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
868 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
869 SHADER_BUFFER* buffer = arg->buffer;
873 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
874 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
876 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
877 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
878 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
879 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
881 /* Sample the texture using the calculated coordinates */
882 sprintf(dst_str, "T%u", reg);
883 shader_hw_sample(arg, reg, dst_str, "TMP", TRUE);
884 current_state->current_row = 0;
887 /** Handles transforming all WINED3DSIO_M?x? opcodes for
888 Vertex shaders to ARB_vertex_program codes */
889 void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
893 SHADER_OPCODE_ARG tmpArg;
895 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
897 /* Set constants for the temporary argument */
898 tmpArg.shader = arg->shader;
899 tmpArg.buffer = arg->buffer;
900 tmpArg.src[0] = arg->src[0];
901 tmpArg.src_addr[0] = arg->src_addr[0];
902 tmpArg.src_addr[1] = arg->src_addr[1];
903 tmpArg.reg_maps = arg->reg_maps;
905 switch(arg->opcode->opcode) {
906 case WINED3DSIO_M4x4:
908 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
910 case WINED3DSIO_M4x3:
912 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
914 case WINED3DSIO_M3x4:
916 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
918 case WINED3DSIO_M3x3:
920 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
922 case WINED3DSIO_M3x2:
924 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
930 for (i = 0; i < nComponents; i++) {
931 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
932 tmpArg.src[1] = arg->src[1]+i;
933 vshader_hw_map2gl(&tmpArg);
937 void vshader_hw_rsq_rcp(SHADER_OPCODE_ARG* arg) {
938 CONST SHADER_OPCODE* curOpcode = arg->opcode;
939 SHADER_BUFFER* buffer = arg->buffer;
940 DWORD dst = arg->dst;
941 DWORD src = arg->src[0];
942 DWORD swizzle = (src & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
946 strcpy(tmpLine, curOpcode->glname); /* Opcode */
947 vshader_program_add_param(arg, dst, FALSE, tmpLine); /* Destination */
948 strcat(tmpLine, ",");
949 vshader_program_add_param(arg, src, TRUE, tmpLine);
950 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) == swizzle) {
951 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
954 strcat(tmpLine, ".w");
957 shader_addline(buffer, "%s;\n", tmpLine);
960 /* TODO: merge with pixel shader */
961 /* Map the opcode 1-to-1 to the GL code */
962 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
964 CONST SHADER_OPCODE* curOpcode = arg->opcode;
965 SHADER_BUFFER* buffer = arg->buffer;
966 DWORD dst = arg->dst;
967 DWORD* src = arg->src;
969 DWORD dst_regtype = shader_get_regtype(dst);
973 if ((curOpcode->opcode == WINED3DSIO_MOV && dst_regtype == WINED3DSPR_ADDR) || curOpcode->opcode == WINED3DSIO_MOVA)
974 strcpy(tmpLine, "ARL");
976 strcpy(tmpLine, curOpcode->glname);
978 if (curOpcode->num_params > 0) {
979 vshader_program_add_param(arg, dst, FALSE, tmpLine);
980 for (i = 1; i < curOpcode->num_params; ++i) {
981 strcat(tmpLine, ",");
982 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
985 shader_addline(buffer, "%s;\n", tmpLine);
988 static GLuint create_arb_blt_vertex_program(WineD3D_GL_Info *gl_info) {
989 GLuint program_id = 0;
990 const char *blt_vprogram =
992 "PARAM c[1] = { { 1, 0.5 } };\n"
993 "MOV result.position, vertex.position;\n"
994 "MOV result.color, c[0].x;\n"
995 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
996 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
999 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1000 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
1001 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
1003 if (glGetError() == GL_INVALID_OPERATION) {
1005 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1006 FIXME("Vertex program error at position %d: %s\n", pos,
1007 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1013 static GLuint create_arb_blt_fragment_program(WineD3D_GL_Info *gl_info) {
1014 GLuint program_id = 0;
1015 const char *blt_fprogram =
1018 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1019 "MOV result.depth.z, R0.x;\n"
1022 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1023 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
1024 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprogram), blt_fprogram));
1026 if (glGetError() == GL_INVALID_OPERATION) {
1028 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1029 FIXME("Fragment program error at position %d: %s\n", pos,
1030 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1036 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1037 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1038 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1041 TRACE("Using vertex shader\n");
1043 /* Bind the vertex program */
1044 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB,
1045 ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId));
1046 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1048 /* Enable OpenGL vertex programs */
1049 glEnable(GL_VERTEX_PROGRAM_ARB);
1050 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1051 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
1052 This, ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.prgId);
1053 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1054 glDisable(GL_VERTEX_PROGRAM_ARB);
1055 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1059 TRACE("Using pixel shader\n");
1061 /* Bind the fragment program */
1062 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
1063 ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId));
1064 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1066 /* Enable OpenGL fragment programs */
1067 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1068 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1069 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
1070 This, ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.prgId);
1071 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1072 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1073 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1077 static void shader_arb_select_depth_blt(IWineD3DDevice *iface) {
1078 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1079 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1080 static GLuint vprogram_id = 0;
1081 static GLuint fprogram_id = 0;
1083 if (!vprogram_id) vprogram_id = create_arb_blt_vertex_program(gl_info);
1084 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vprogram_id));
1085 glEnable(GL_VERTEX_PROGRAM_ARB);
1087 if (!fprogram_id) fprogram_id = create_arb_blt_fragment_program(gl_info);
1088 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprogram_id));
1089 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1092 static void shader_arb_cleanup(IWineD3DDevice *iface) {
1093 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1094 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1095 if (GL_SUPPORT(ARB_VERTEX_PROGRAM)) glDisable(GL_VERTEX_PROGRAM_ARB);
1096 if (GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) glDisable(GL_FRAGMENT_PROGRAM_ARB);
1099 const shader_backend_t arb_program_shader_backend = {
1101 &shader_arb_select_depth_blt,
1102 &shader_arb_load_constants,