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
;
717 shader_addline(buffer
, "MOV TMP.r, T%u.a;\n", reg2
);
718 shader_addline(buffer
, "MOV TMP.g, T%u.r;\n", reg2
);
719 shader_addline(buffer
, "TEX T%u, TMP, texture[%u], 2D;\n", reg1
, reg1
);
722 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
724 SHADER_BUFFER
* buffer
= arg
->buffer
;
726 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
727 DWORD reg2
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
728 shader_addline(buffer
, "MOV TMP.r, T%u.g;\n", reg2
);
729 shader_addline(buffer
, "MOV TMP.g, T%u.b;\n", reg2
);
730 shader_addline(buffer
, "TEX T%u, TMP, texture[%u], 2D;\n", reg1
, reg1
);
733 void pshader_hw_texbem(SHADER_OPCODE_ARG
* arg
) {
735 SHADER_BUFFER
* buffer
= arg
->buffer
;
736 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
737 DWORD reg2
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
739 /* FIXME: Should apply the BUMPMAPENV matrix */
740 shader_addline(buffer
, "ADD TMP.rg, fragment.texcoord[%u], T%u;\n", reg1
, reg2
);
741 shader_addline(buffer
, "TEX T%u, TMP, texture[%u], 2D;\n", reg1
, reg1
);
744 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
746 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
747 SHADER_BUFFER
* buffer
= arg
->buffer
;
750 pshader_gen_input_modifier_line(buffer
, arg
->src
[0], 0, src0_name
);
751 shader_addline(buffer
, "DP3 TMP.x, T%u, %s;\n", reg
, src0_name
);
754 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
756 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
757 SHADER_BUFFER
* buffer
= arg
->buffer
;
760 pshader_gen_input_modifier_line(buffer
, arg
->src
[0], 0, src0_name
);
761 shader_addline(buffer
, "DP3 TMP.y, T%u, %s;\n", reg
, src0_name
);
762 shader_addline(buffer
, "TEX T%u, TMP, texture[%u], 2D;\n", reg
, reg
);
765 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
767 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
768 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
769 SHADER_BUFFER
* buffer
= arg
->buffer
;
770 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
773 pshader_gen_input_modifier_line(buffer
, arg
->src
[0], 0, src0_name
);
774 shader_addline(buffer
, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state
->current_row
, reg
, src0_name
);
775 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
778 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
780 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
781 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
782 SHADER_BUFFER
* buffer
= arg
->buffer
;
783 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
787 pshader_gen_input_modifier_line(buffer
, arg
->src
[0], 0, src0_name
);
788 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
790 /* Sample the texture using the calculated coordinates */
791 sprintf(dst_str
, "T%u", reg
);
792 shader_hw_sample(arg
, reg
, dst_str
, "TMP");
793 current_state
->current_row
= 0;
796 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
798 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
799 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
800 SHADER_BUFFER
* buffer
= arg
->buffer
;
801 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
804 pshader_gen_input_modifier_line(buffer
, arg
->src
[0], 0, src0_name
);
805 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
807 /* Construct the eye-ray vector from w coordinates */
808 shader_addline(buffer
, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[0]);
809 shader_addline(buffer
, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[1]);
810 shader_addline(buffer
, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg
);
812 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
813 shader_addline(buffer
, "DP3 TMP.w, TMP, TMP2;\n");
814 shader_addline(buffer
, "MUL TMP, TMP.w, TMP;\n");
815 shader_addline(buffer
, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
817 /* Cubemap textures will be more used than 3D ones. */
818 shader_addline(buffer
, "TEX T%u, TMP, texture[%u], CUBE;\n", reg
, reg
);
819 current_state
->current_row
= 0;
822 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
824 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
825 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
826 DWORD reg3
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
827 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
828 SHADER_BUFFER
* buffer
= arg
->buffer
;
831 pshader_gen_input_modifier_line(buffer
, arg
->src
[0], 0, src0_name
);
832 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
834 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
835 shader_addline(buffer
, "DP3 TMP.w, TMP, C[%u];\n", reg3
);
836 shader_addline(buffer
, "MUL TMP, TMP.w, TMP;\n");
837 shader_addline(buffer
, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3
);
839 /* Cubemap textures will be more used than 3D ones. */
840 shader_addline(buffer
, "TEX T%u, TMP, texture[%u], CUBE;\n", reg
, reg
);
841 current_state
->current_row
= 0;
844 /** Handles transforming all WINED3DSIO_M?x? opcodes for
845 Vertex shaders to ARB_vertex_program codes */
846 void vshader_hw_mnxn(SHADER_OPCODE_ARG
* arg
) {
850 SHADER_OPCODE_ARG tmpArg
;
852 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
854 /* Set constants for the temporary argument */
855 tmpArg
.shader
= arg
->shader
;
856 tmpArg
.buffer
= arg
->buffer
;
857 tmpArg
.src
[0] = arg
->src
[0];
858 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
859 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
860 tmpArg
.reg_maps
= arg
->reg_maps
;
862 switch(arg
->opcode
->opcode
) {
863 case WINED3DSIO_M4x4
:
865 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
867 case WINED3DSIO_M4x3
:
869 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
871 case WINED3DSIO_M3x4
:
873 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
875 case WINED3DSIO_M3x3
:
877 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
879 case WINED3DSIO_M3x2
:
881 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
887 for (i
= 0; i
< nComponents
; i
++) {
888 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
889 tmpArg
.src
[1] = arg
->src
[1]+i
;
890 vshader_hw_map2gl(&tmpArg
);
894 /* TODO: merge with pixel shader */
895 /* Map the opcode 1-to-1 to the GL code */
896 void vshader_hw_map2gl(SHADER_OPCODE_ARG
* arg
) {
898 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
899 SHADER_BUFFER
* buffer
= arg
->buffer
;
900 DWORD dst
= arg
->dst
;
901 DWORD
* src
= arg
->src
;
903 DWORD dst_regtype
= shader_get_regtype(dst
);
907 if ((curOpcode
->opcode
== WINED3DSIO_MOV
&& dst_regtype
== WINED3DSPR_ADDR
) || curOpcode
->opcode
== WINED3DSIO_MOVA
)
908 strcpy(tmpLine
, "ARL");
910 strcpy(tmpLine
, curOpcode
->glname
);
912 if (curOpcode
->num_params
> 0) {
913 vshader_program_add_param(arg
, dst
, FALSE
, tmpLine
);
914 for (i
= 1; i
< curOpcode
->num_params
; ++i
) {
915 strcat(tmpLine
, ",");
916 vshader_program_add_param(arg
, src
[i
-1], TRUE
, tmpLine
);
919 shader_addline(buffer
, "%s;\n", tmpLine
);
922 static GLuint
create_arb_blt_vertex_program(WineD3D_GL_Info
*gl_info
) {
923 GLuint program_id
= 0;
924 const char *blt_vprogram
=
926 "PARAM c[1] = { { 1, 0.5 } };\n"
927 "MOV result.position, vertex.position;\n"
928 "MOV result.color, c[0].x;\n"
929 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
930 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
933 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
934 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, program_id
));
935 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_vprogram
), blt_vprogram
));
937 if (glGetError() == GL_INVALID_OPERATION
) {
939 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
940 FIXME("Vertex program error at position %d: %s\n", pos
,
941 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
947 static GLuint
create_arb_blt_fragment_program(WineD3D_GL_Info
*gl_info
) {
948 GLuint program_id
= 0;
949 const char *blt_fprogram
=
952 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
953 "MOV result.depth.z, R0.x;\n"
956 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
957 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, program_id
));
958 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_fprogram
), blt_fprogram
));
960 if (glGetError() == GL_INVALID_OPERATION
) {
962 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
963 FIXME("Fragment program error at position %d: %s\n", pos
,
964 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
970 static void shader_arb_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
971 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
972 WineD3D_GL_Info
*gl_info
= &((IWineD3DImpl
*)(This
->wineD3D
))->gl_info
;
975 TRACE("Using vertex shader\n");
977 /* Bind the vertex program */
978 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
,
979 ((IWineD3DVertexShaderImpl
*)This
->stateBlock
->vertexShader
)->baseShader
.prgId
));
980 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
982 /* Enable OpenGL vertex programs */
983 glEnable(GL_VERTEX_PROGRAM_ARB
);
984 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
985 TRACE_(d3d_shader
)("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n",
986 This
, ((IWineD3DVertexShaderImpl
*)This
->stateBlock
->vertexShader
)->baseShader
.prgId
);
990 TRACE("Using pixel shader\n");
992 /* Bind the fragment program */
993 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
,
994 ((IWineD3DPixelShaderImpl
*)This
->stateBlock
->pixelShader
)->baseShader
.prgId
));
995 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
997 /* Enable OpenGL fragment programs */
998 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
999 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1000 TRACE_(d3d_shader
)("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
1001 This
, ((IWineD3DPixelShaderImpl
*)This
->stateBlock
->pixelShader
)->baseShader
.prgId
);
1005 static void shader_arb_select_depth_blt(IWineD3DDevice
*iface
) {
1006 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1007 WineD3D_GL_Info
*gl_info
= &((IWineD3DImpl
*)(This
->wineD3D
))->gl_info
;
1008 static GLuint vprogram_id
= 0;
1009 static GLuint fprogram_id
= 0;
1011 if (!vprogram_id
) vprogram_id
= create_arb_blt_vertex_program(gl_info
);
1012 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, vprogram_id
));
1013 glEnable(GL_VERTEX_PROGRAM_ARB
);
1015 if (!fprogram_id
) fprogram_id
= create_arb_blt_fragment_program(gl_info
);
1016 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, fprogram_id
));
1017 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1020 static void shader_arb_cleanup(BOOL usePS
, BOOL useVS
) {
1021 if (useVS
) glDisable(GL_VERTEX_PROGRAM_ARB
);
1022 if (usePS
) glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1025 const shader_backend_t arb_program_shader_backend
= {
1027 &shader_arb_select_depth_blt
,
1028 &shader_arb_load_constants
,