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
12 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants
);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps
);
40 #define GLINFO_LOCATION (*gl_info)
42 /********************************************************
43 * ARB_[vertex/fragment]_program helper functions follow
44 ********************************************************/
47 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
48 * When constant_list == NULL, it will load all the constants.
50 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
51 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
53 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl
* This
, WineD3D_GL_Info
*gl_info
, GLuint target_type
,
54 unsigned int max_constants
, float* constants
, char *dirty_consts
) {
55 local_constant
* lconst
;
59 if (TRACE_ON(d3d_shader
)) {
60 for(i
= 0; i
< max_constants
; i
++) {
61 if(!dirty_consts
[i
]) continue;
62 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
63 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
64 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
67 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
68 if(target_type
== GL_FRAGMENT_PROGRAM_ARB
&&
69 WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1) {
71 for(i
= 0; i
< max_constants
; i
++) {
72 if(!dirty_consts
[i
]) continue;
76 if(constants
[j
+ 0] > 1.0) lcl_const
[0] = 1.0;
77 else if(constants
[j
+ 0] < -1.0) lcl_const
[0] = -1.0;
78 else lcl_const
[0] = constants
[j
+ 0];
80 if(constants
[j
+ 1] > 1.0) lcl_const
[1] = 1.0;
81 else if(constants
[j
+ 1] < -1.0) lcl_const
[1] = -1.0;
82 else lcl_const
[1] = constants
[j
+ 1];
84 if(constants
[j
+ 2] > 1.0) lcl_const
[2] = 1.0;
85 else if(constants
[j
+ 2] < -1.0) lcl_const
[2] = -1.0;
86 else lcl_const
[2] = constants
[j
+ 2];
88 if(constants
[j
+ 3] > 1.0) lcl_const
[3] = 1.0;
89 else if(constants
[j
+ 3] < -1.0) lcl_const
[3] = -1.0;
90 else lcl_const
[3] = constants
[j
+ 3];
92 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, lcl_const
));
95 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS
)) {
96 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
97 * or just reloading *all* constants at once
99 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
101 for(i
= 0; i
< max_constants
; i
++) {
102 if(!dirty_consts
[i
]) continue;
104 /* Find the next block of dirty constants */
107 for(i
++; (i
< max_constants
) && dirty_consts
[i
]; i
++) {
111 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type
, j
, i
- j
, constants
+ (j
* 4)));
114 for(i
= 0; i
< max_constants
; i
++) {
115 if(dirty_consts
[i
]) {
117 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, constants
+ (i
* 4)));
122 checkGLcall("glProgramEnvParameter4fvARB()");
124 /* Load immediate constants */
125 if(This
->baseShader
.load_local_constsF
) {
126 if (TRACE_ON(d3d_shader
)) {
127 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
128 GLfloat
* values
= (GLfloat
*)lconst
->value
;
129 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
130 values
[0], values
[1], values
[2], values
[3]);
133 /* Immediate constants are clamped for 1.X shaders at loading times */
135 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
136 dirty_consts
[lconst
->idx
] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
137 ret
= max(ret
, lconst
->idx
+ 1);
138 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, lconst
->idx
, (GLfloat
*)lconst
->value
));
140 checkGLcall("glProgramEnvParameter4fvARB()");
141 return ret
; /* The loaded immediate constants need reloading for the next shader */
143 return 0; /* No constants are dirty now */
148 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
150 * We only support float constants in ARB at the moment, so don't
151 * worry about the Integers or Booleans
153 void shader_arb_load_constants(
154 IWineD3DDevice
* device
,
156 char useVertexShader
) {
158 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
159 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
160 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
163 if (useVertexShader
) {
164 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
166 /* Load DirectX 9 float constants for vertex shader */
167 deviceImpl
->highest_dirty_vs_const
= shader_arb_load_constantsF(
168 vshader
, gl_info
, GL_VERTEX_PROGRAM_ARB
,
169 deviceImpl
->highest_dirty_vs_const
,
170 stateBlock
->vertexShaderConstantF
,
171 deviceImpl
->activeContext
->vshader_const_dirty
);
173 /* Upload the position fixup */
174 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, ARB_SHADER_PRIVCONST_POS
, deviceImpl
->posFixup
));
177 if (usePixelShader
) {
179 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
180 IWineD3DPixelShaderImpl
*psi
= (IWineD3DPixelShaderImpl
*) pshader
;
182 /* Load DirectX 9 float constants for pixel shader */
183 deviceImpl
->highest_dirty_ps_const
= shader_arb_load_constantsF(
184 pshader
, gl_info
, GL_FRAGMENT_PROGRAM_ARB
,
185 deviceImpl
->highest_dirty_ps_const
,
186 stateBlock
->pixelShaderConstantF
,
187 deviceImpl
->activeContext
->pshader_const_dirty
);
189 for(i
= 0; i
< psi
->numbumpenvmatconsts
; i
++) {
190 /* The state manager takes care that this function is always called if the bump env matrix changes
192 float *data
= (float *) &stateBlock
->textureState
[(int) psi
->bumpenvmatconst
[i
].texunit
][WINED3DTSS_BUMPENVMAT00
];
193 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, psi
->bumpenvmatconst
[i
].const_num
, data
));
194 deviceImpl
->activeContext
->pshader_const_dirty
[psi
->bumpenvmatconst
[i
].const_num
] = 1;
196 if(psi
->luminanceconst
[i
].const_num
!= -1) {
197 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
198 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
199 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
200 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
202 float *scale
= (float *) &stateBlock
->textureState
[(int) psi
->luminanceconst
[i
].texunit
][WINED3DTSS_BUMPENVLSCALE
];
203 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, psi
->luminanceconst
[i
].const_num
, scale
));
204 deviceImpl
->activeContext
->pshader_const_dirty
[psi
->luminanceconst
[i
].const_num
] = 1;
208 if(((IWineD3DPixelShaderImpl
*) pshader
)->srgb_enabled
&&
209 !((IWineD3DPixelShaderImpl
*) pshader
)->srgb_mode_hardcoded
) {
213 if(stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
214 comparison
[0] = srgb_cmp
; comparison
[1] = srgb_cmp
;
215 comparison
[2] = srgb_cmp
; comparison
[3] = srgb_cmp
;
217 mul_low
[0] = srgb_mul_low
; mul_low
[1] = srgb_mul_low
;
218 mul_low
[2] = srgb_mul_low
; mul_low
[3] = srgb_mul_low
;
220 comparison
[0] = 1.0 / 0.0; comparison
[1] = 1.0 / 0.0;
221 comparison
[2] = 1.0 / 0.0; comparison
[3] = 1.0 / 0.0;
223 mul_low
[0] = 1.0; mul_low
[1] = 1.0;
224 mul_low
[2] = 1.0; mul_low
[3] = 1.0;
226 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, psi
->srgb_cmp_const
, comparison
));
227 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, psi
->srgb_low_const
, mul_low
));
228 checkGLcall("Load sRGB correction constants\n");
229 deviceImpl
->activeContext
->pshader_const_dirty
[psi
->srgb_low_const
] = 1;
230 deviceImpl
->activeContext
->pshader_const_dirty
[psi
->srgb_cmp_const
] = 1;
236 /* Generate the variable & register declarations for the ARB_vertex_program output target */
237 void shader_generate_arb_declarations(
238 IWineD3DBaseShader
*iface
,
239 shader_reg_maps
* reg_maps
,
240 SHADER_BUFFER
* buffer
,
241 WineD3D_GL_Info
* gl_info
) {
243 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
244 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
246 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
247 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
248 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
249 UINT extra_constants_needed
= 0;
250 local_constant
* lconst
;
252 /* Temporary Output register */
253 shader_addline(buffer
, "TEMP TMP_OUT;\n");
255 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
256 if (reg_maps
->temporary
[i
])
257 shader_addline(buffer
, "TEMP R%u;\n", i
);
260 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
261 if (reg_maps
->address
[i
])
262 shader_addline(buffer
, "ADDRESS A%d;\n", i
);
265 for(i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
266 if (reg_maps
->texcoord
[i
])
267 shader_addline(buffer
,"TEMP T%u;\n", i
);
270 /* Texture coordinate registers must be pre-loaded */
271 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
272 if (reg_maps
->texcoord
[i
])
273 shader_addline(buffer
, "MOV T%u, fragment.texcoord[%u];\n", i
, i
);
276 for(i
= 0; i
< (sizeof(reg_maps
->bumpmat
) / sizeof(reg_maps
->bumpmat
[0])); i
++) {
277 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) This
;
278 if(!reg_maps
->bumpmat
[i
]) continue;
280 cur
= ps
->numbumpenvmatconsts
;
281 ps
->bumpenvmatconst
[cur
].const_num
= -1;
282 ps
->bumpenvmatconst
[cur
].texunit
= i
;
283 ps
->luminanceconst
[cur
].const_num
= -1;
284 ps
->luminanceconst
[cur
].texunit
= i
;
286 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
287 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
290 if(max_constantsF
+ extra_constants_needed
< GL_LIMITS(pshader_constantsF
)) {
291 ps
->bumpenvmatconst
[cur
].const_num
= max_constantsF
+ extra_constants_needed
;
292 shader_addline(buffer
, "PARAM bumpenvmat%d = program.env[%d];\n",
293 i
, ps
->bumpenvmatconst
[cur
].const_num
);
294 extra_constants_needed
++;
296 if(reg_maps
->luminanceparams
&& max_constantsF
+ extra_constants_needed
< GL_LIMITS(pshader_constantsF
)) {
297 ((IWineD3DPixelShaderImpl
*)This
)->luminanceconst
[cur
].const_num
= max_constantsF
+ extra_constants_needed
;
298 shader_addline(buffer
, "PARAM luminance%d = program.env[%d];\n",
299 i
, ps
->luminanceconst
[cur
].const_num
);
300 extra_constants_needed
++;
301 } else if(reg_maps
->luminanceparams
) {
302 FIXME("No free constant to load the luminance parameters\n");
305 FIXME("No free constant found to load environemnt bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
308 ps
->numbumpenvmatconsts
= cur
+ 1;
311 if(device
->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
] && pshader
) {
312 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
313 /* If there are 2 constants left to use, use them to pass the sRGB correction values in. This way
314 * srgb write correction can be turned on and off dynamically without recompilation. Otherwise
315 * hardcode them. The drawback of hardcoding is that the shader needs recompilation to turn sRGB
318 if(max_constantsF
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
) && FALSE
) {
319 /* The idea is that if srgb is enabled, then disabled, the constant loading code
320 * can effectively disable sRGB correction by passing 1.0 and INF as the multiplication
321 * and comparison constants. If it disables it that way, the shader won't be recompiled
322 * and the code will stay in, so sRGB writing can be turned on again by setting the
323 * constants from the spec
325 ps_impl
->srgb_mode_hardcoded
= 0;
326 ps_impl
->srgb_low_const
= GL_LIMITS(pshader_constantsF
) - extra_constants_needed
;
327 ps_impl
->srgb_cmp_const
= GL_LIMITS(pshader_constantsF
) - extra_constants_needed
- 1;
328 shader_addline(buffer
, "PARAM srgb_mul_low = program.env[%d];\n", ps_impl
->srgb_low_const
);
329 shader_addline(buffer
, "PARAM srgb_comparison = program.env[%d];\n", ps_impl
->srgb_cmp_const
);
331 shader_addline(buffer
, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
332 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
333 shader_addline(buffer
, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
334 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
335 ps_impl
->srgb_mode_hardcoded
= 1;
337 /* These can be hardcoded, they do not cause any harm because no fragment will enter the high
338 * path if the comparison value is set to INF
340 shader_addline(buffer
, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
341 srgb_pow
, srgb_pow
, srgb_pow
);
342 shader_addline(buffer
, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
343 srgb_mul_high
, srgb_mul_high
, srgb_mul_high
);
344 shader_addline(buffer
, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
345 srgb_sub_high
, srgb_sub_high
, srgb_sub_high
);
346 ps_impl
->srgb_enabled
= 1;
348 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
350 /* Do not write any srgb fixup into the shader to save shader size and processing time.
351 * As a consequence, we can't toggle srgb write on without recompilation
353 ps_impl
->srgb_enabled
= 0;
354 ps_impl
->srgb_mode_hardcoded
= 1;
357 /* Load local constants using the program-local space,
358 * this avoids reloading them each time the shader is used
360 if(!This
->baseShader
.load_local_constsF
) {
361 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
362 shader_addline(buffer
, "PARAM C%u = program.local[%u];\n", lconst
->idx
,
367 /* we use the array-based constants array if the local constants are marked for loading,
368 * because then we use indirect addressing, or when the local constant list is empty,
369 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
370 * local constants do not declare the loaded constants as an array because ARB compilers usually
371 * do not optimize unused constants away
373 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) {
374 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
375 shader_addline(buffer
, "PARAM C[%d] = { program.env[0..%d] };\n",
376 max_constantsF
, max_constantsF
- 1);
378 for(i
= 0; i
< max_constantsF
; i
++) {
379 if(!shader_constant_is_local(This
, i
)) {
380 shader_addline(buffer
, "PARAM C%d = program.env[%d];\n",i
, i
);
386 static const char * const shift_tab
[] = {
387 "dummy", /* 0 (none) */
388 "coefmul.x", /* 1 (x2) */
389 "coefmul.y", /* 2 (x4) */
390 "coefmul.z", /* 3 (x8) */
391 "coefmul.w", /* 4 (x16) */
392 "dummy", /* 5 (x32) */
393 "dummy", /* 6 (x64) */
394 "dummy", /* 7 (x128) */
395 "dummy", /* 8 (d256) */
396 "dummy", /* 9 (d128) */
397 "dummy", /* 10 (d64) */
398 "dummy", /* 11 (d32) */
399 "coefdiv.w", /* 12 (d16) */
400 "coefdiv.z", /* 13 (d8) */
401 "coefdiv.y", /* 14 (d4) */
402 "coefdiv.x" /* 15 (d2) */
405 static void shader_arb_get_write_mask(SHADER_OPCODE_ARG
* arg
, const DWORD param
, char *write_mask
) {
406 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
407 char *ptr
= write_mask
;
408 char vshader
= shader_is_vshader_version(This
->baseShader
.hex_version
);
410 if(vshader
&& shader_get_regtype(param
) == WINED3DSPR_ADDR
) {
413 } else if ((param
& WINED3DSP_WRITEMASK_ALL
) != WINED3DSP_WRITEMASK_ALL
) {
415 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
416 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
417 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
418 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
424 static void shader_arb_get_swizzle(const DWORD param
, BOOL fixup
, char *swizzle_str
) {
425 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
426 * but addressed as "rgba". To fix this we need to swap the register's x
427 * and z components. */
428 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
429 char *ptr
= swizzle_str
;
431 /* swizzle bits fields: wwzzyyxx */
432 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
433 DWORD swizzle_x
= swizzle
& 0x03;
434 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
435 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
436 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
438 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
439 * generate a swizzle string. Unless we need to our own swizzling. */
440 if ((WINED3DSP_NOSWIZZLE
>> WINED3DSP_SWIZZLE_SHIFT
) != swizzle
|| fixup
) {
442 if (swizzle_x
== swizzle_y
&& swizzle_x
== swizzle_z
&& swizzle_x
== swizzle_w
) {
443 *ptr
++ = swizzle_chars
[swizzle_x
];
445 *ptr
++ = swizzle_chars
[swizzle_x
];
446 *ptr
++ = swizzle_chars
[swizzle_y
];
447 *ptr
++ = swizzle_chars
[swizzle_z
];
448 *ptr
++ = swizzle_chars
[swizzle_w
];
455 static void pshader_get_register_name(IWineD3DBaseShader
* iface
,
456 const DWORD param
, char* regstr
) {
458 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
459 DWORD regtype
= shader_get_regtype(param
);
460 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
463 case WINED3DSPR_TEMP
:
464 sprintf(regstr
, "R%u", reg
);
466 case WINED3DSPR_INPUT
:
468 strcpy(regstr
, "fragment.color.primary");
470 strcpy(regstr
, "fragment.color.secondary");
473 case WINED3DSPR_CONST
:
474 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) {
475 sprintf(regstr
, "C[%u]", reg
);
477 sprintf(regstr
, "C%u", reg
);
480 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
481 sprintf(regstr
,"T%u", reg
);
483 case WINED3DSPR_COLOROUT
:
485 sprintf(regstr
, "TMP_COLOR");
487 /* TODO: See GL_ARB_draw_buffers */
488 FIXME("Unsupported write to render target %u\n", reg
);
489 sprintf(regstr
, "unsupported_register");
492 case WINED3DSPR_DEPTHOUT
:
493 sprintf(regstr
, "result.depth");
495 case WINED3DSPR_ATTROUT
:
496 sprintf(regstr
, "oD[%u]", reg
);
498 case WINED3DSPR_TEXCRDOUT
:
499 sprintf(regstr
, "oT[%u]", reg
);
502 FIXME("Unhandled register name Type(%d)\n", regtype
);
503 sprintf(regstr
, "unrecognized_register");
508 /* TODO: merge with pixel shader */
509 static void vshader_program_add_param(SHADER_OPCODE_ARG
*arg
, const DWORD param
, BOOL is_input
, char *hwLine
) {
511 IWineD3DVertexShaderImpl
* This
= (IWineD3DVertexShaderImpl
*) arg
->shader
;
513 /* oPos, oFog and oPts in D3D */
514 static const char * const hwrastout_reg_names
[] = { "TMP_OUT", "result.fogcoord", "result.pointsize" };
516 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
517 DWORD regtype
= shader_get_regtype(param
);
519 BOOL is_color
= FALSE
;
521 if ((param
& WINED3DSP_SRCMOD_MASK
) == WINED3DSPSM_NEG
) {
522 strcat(hwLine
, " -");
528 case WINED3DSPR_TEMP
:
529 sprintf(tmpReg
, "R%u", reg
);
530 strcat(hwLine
, tmpReg
);
532 case WINED3DSPR_INPUT
:
534 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
537 sprintf(tmpReg
, "vertex.attrib[%u]", reg
);
538 strcat(hwLine
, tmpReg
);
540 case WINED3DSPR_CONST
:
541 if(param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
542 if(reg
>= This
->rel_offset
) {
543 sprintf(tmpReg
, "C[A0.x + %u]", reg
- This
->rel_offset
);
545 sprintf(tmpReg
, "C[A0.x - %u]", -reg
+ This
->rel_offset
);
548 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) {
549 sprintf(tmpReg
, "C[%u]", reg
);
551 sprintf(tmpReg
, "C%u", reg
);
554 strcat(hwLine
, tmpReg
);
556 case WINED3DSPR_ADDR
: /*case D3DSPR_TEXTURE:*/
557 sprintf(tmpReg
, "A%u", reg
);
558 strcat(hwLine
, tmpReg
);
560 case WINED3DSPR_RASTOUT
:
561 sprintf(tmpReg
, "%s", hwrastout_reg_names
[reg
]);
562 strcat(hwLine
, tmpReg
);
564 case WINED3DSPR_ATTROUT
:
566 strcat(hwLine
, "result.color.primary");
568 strcat(hwLine
, "result.color.secondary");
571 case WINED3DSPR_TEXCRDOUT
:
572 sprintf(tmpReg
, "result.texcoord[%u]", reg
);
573 strcat(hwLine
, tmpReg
);
576 FIXME("Unknown reg type %d %d\n", regtype
, reg
);
577 strcat(hwLine
, "unrecognized_register");
583 shader_arb_get_write_mask(arg
, param
, write_mask
);
584 strcat(hwLine
, write_mask
);
587 shader_arb_get_swizzle(param
, is_color
, swizzle
);
588 strcat(hwLine
, swizzle
);
592 static void shader_hw_sample(SHADER_OPCODE_ARG
* arg
, DWORD sampler_idx
, const char *dst_str
, const char *coord_reg
, BOOL projected
, BOOL bias
) {
593 SHADER_BUFFER
* buffer
= arg
->buffer
;
594 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
595 const char *tex_type
;
597 switch(sampler_type
) {
604 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
605 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
606 if(device
->stateBlock
->textures
[sampler_idx
] &&
607 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
615 case WINED3DSTT_VOLUME
:
619 case WINED3DSTT_CUBE
:
624 ERR("Unexpected texture type %d\n", sampler_type
);
629 /* Shouldn't be possible, but let's check for it */
630 if(projected
) FIXME("Biased and Projected texture sampling\n");
631 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
632 shader_addline(buffer
, "TXB %s, %s, texture[%u], %s;\n", dst_str
, coord_reg
, sampler_idx
, tex_type
);
633 } else if (projected
) {
634 shader_addline(buffer
, "TXP %s, %s, texture[%u], %s;\n", dst_str
, coord_reg
, sampler_idx
, tex_type
);
636 shader_addline(buffer
, "TEX %s, %s, texture[%u], %s;\n", dst_str
, coord_reg
, sampler_idx
, tex_type
);
640 static void shader_arb_color_correction(SHADER_OPCODE_ARG
* arg
) {
641 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
642 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
643 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
645 WINED3DFORMAT conversion_group
;
646 IWineD3DBaseTextureImpl
*texture
;
648 BOOL recorded
= FALSE
;
650 DWORD hex_version
= shader
->baseShader
.hex_version
;
654 switch(arg
->opcode
->opcode
) {
656 if (hex_version
< WINED3DPS_VERSION(2,0)) {
657 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
659 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
663 case WINED3DSIO_TEXLDL
:
664 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
667 case WINED3DSIO_TEXDP3TEX
:
668 case WINED3DSIO_TEXM3x3TEX
:
669 case WINED3DSIO_TEXM3x3SPEC
:
670 case WINED3DSIO_TEXM3x3VSPEC
:
671 case WINED3DSIO_TEXBEM
:
672 case WINED3DSIO_TEXREG2AR
:
673 case WINED3DSIO_TEXREG2GB
:
674 case WINED3DSIO_TEXREG2RGB
:
675 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
679 /* Not a texture sampling instruction, nothing to do */
683 texture
= (IWineD3DBaseTextureImpl
*) deviceImpl
->stateBlock
->textures
[sampler_idx
];
685 fmt
= texture
->resource
.format
;
686 conversion_group
= texture
->baseTexture
.shader_conversion_group
;
688 fmt
= WINED3DFMT_UNKNOWN
;
689 conversion_group
= WINED3DFMT_UNKNOWN
;
692 /* before doing anything, record the sampler with the format in the format conversion list,
693 * but check if it's not there already
695 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
696 if(shader
->baseShader
.sampled_samplers
[i
] == sampler_idx
) {
701 shader
->baseShader
.sampled_samplers
[shader
->baseShader
.num_sampled_samplers
] = sampler_idx
;
702 shader
->baseShader
.num_sampled_samplers
++;
703 shader
->baseShader
.sampled_format
[sampler_idx
] = conversion_group
;
706 pshader_get_register_name(arg
->shader
, arg
->dst
, reg
);
707 shader_arb_get_write_mask(arg
, arg
->dst
, writemask
);
708 if(strlen(writemask
) == 0) strcpy(writemask
, ".xyzw");
711 case WINED3DFMT_V8U8
:
712 case WINED3DFMT_V16U16
:
713 if(GL_SUPPORT(NV_TEXTURE_SHADER
) ||
714 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP
) && fmt
== WINED3DFMT_V8U8
)) {
716 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-)
717 * disabled until an application that needs it is found because it causes unneeded
718 * shader recompilation in some game
720 if(strlen(writemask
) >= 4) {
721 shader_addline(arg
->buffer
, "MOV %s.%c, one.z;\n", reg
, writemask
[3]);
725 /* Correct the sign, but leave the blue as it is - it was loaded correctly already
726 * ARB shaders are a bit picky wrt writemasks and swizzles. If we're free to scale
727 * all registers, do so, this saves an instruction.
729 if(strlen(writemask
) >= 5) {
730 shader_addline(arg
->buffer
, "MAD %s, %s, coefmul.x, -one;\n", reg
, reg
);
731 } else if(strlen(writemask
) >= 3) {
732 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
735 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
738 } else if(strlen(writemask
) == 2) {
739 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n", reg
, writemask
[1],
745 case WINED3DFMT_X8L8V8U8
:
746 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
747 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
748 * and a(X) is always 1.0. Cannot do a full conversion due to L(blue)
750 if(strlen(writemask
) >= 3) {
751 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
754 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
757 } else if(strlen(writemask
) == 2) {
758 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
765 case WINED3DFMT_L6V5U5
:
766 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
767 if(strlen(writemask
) >= 4) {
768 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
769 shader_addline(arg
->buffer
, "MOV TMP.g, %s.%c;\n",
771 shader_addline(arg
->buffer
, "MAD %s.%c%c, %s.%c%c, coefmul.x, -one;\n",
772 reg
, writemask
[1], writemask
[1],
773 reg
, writemask
[1], writemask
[3]);
774 shader_addline(arg
->buffer
, "MOV %s.%c, TMP.g;\n", reg
,
776 } else if(strlen(writemask
) == 3) {
777 /* This is bad: We have VL, but we need VU */
778 FIXME("2 components sampled from a converted L6V5U5 texture\n");
780 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
787 case WINED3DFMT_Q8W8V8U8
:
788 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
789 /* Correct the sign in all channels */
790 switch(strlen(writemask
)) {
792 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
797 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
802 shader_addline(arg
->buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
807 /* Should not occur, since it's at minimum '.' and a letter */
809 ERR("Unexpected writemask: \"%s\"\n", writemask
);
814 shader_addline(arg
->buffer
, "MAD %s, %s, coefmul.x, -one;\n", reg
, reg
);
819 case WINED3DFMT_ATI2N
:
820 /* GL_ATI_texture_compression_3dc returns the two channels as luminance-alpha,
821 * which means the first one is replicated accross .rgb, and the 2nd one is in
822 * .a. We need the 2nd in .g
824 * GL_EXT_texture_compression_rgtc returns the values in .rg, however, they
825 * are swapped compared to d3d. So swap red and green.
827 if(GL_SUPPORT(EXT_TEXTURE_COMPRESSION_RGTC
)) {
828 shader_addline(arg
->buffer
, "SWZ %s, %s, %c, %c, 1, 0;\n",
829 reg
, reg
, writemask
[2], writemask
[1]);
831 if(strlen(writemask
) == 5) {
832 shader_addline(arg
->buffer
, "MOV %s.%c, %s.%c;\n",
833 reg
, writemask
[2], reg
, writemask
[4]);
834 } else if(strlen(writemask
) == 2) {
837 /* This is bad: We have VL, but we need VU */
838 FIXME("2 or 3 components sampled from a converted ATI2N texture\n");
843 /* stupid compiler */
850 static void pshader_gen_input_modifier_line (
851 IWineD3DBaseShader
*iface
,
852 SHADER_BUFFER
* buffer
,
857 /* Generate a line that does the input modifier computation and return the input register to use */
862 /* Assume a new line will be added */
865 /* Get register name */
866 pshader_get_register_name(iface
, instr
, regstr
);
867 shader_arb_get_swizzle(instr
, FALSE
, swzstr
);
869 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
870 case WINED3DSPSM_NONE
:
871 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
874 case WINED3DSPSM_NEG
:
875 sprintf(outregstr
, "-%s%s", regstr
, swzstr
);
878 case WINED3DSPSM_BIAS
:
879 shader_addline(buffer
, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg
, regstr
);
881 case WINED3DSPSM_BIASNEG
:
882 shader_addline(buffer
, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg
, regstr
);
884 case WINED3DSPSM_SIGN
:
885 shader_addline(buffer
, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg
, regstr
);
887 case WINED3DSPSM_SIGNNEG
:
888 shader_addline(buffer
, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg
, regstr
);
890 case WINED3DSPSM_COMP
:
891 shader_addline(buffer
, "SUB T%c, one.x, %s;\n", 'A' + tmpreg
, regstr
);
894 shader_addline(buffer
, "ADD T%c, %s, %s;\n", 'A' + tmpreg
, regstr
, regstr
);
896 case WINED3DSPSM_X2NEG
:
897 shader_addline(buffer
, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg
, regstr
, regstr
);
900 shader_addline(buffer
, "RCP T%c, %s.z;\n", 'A' + tmpreg
, regstr
);
901 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
904 shader_addline(buffer
, "RCP T%c, %s.w;\n", 'A' + tmpreg
, regstr
);
905 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
908 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
912 /* Return modified or original register, with swizzle */
914 sprintf(outregstr
, "T%c%s", 'A' + tmpreg
, swzstr
);
917 static inline void pshader_gen_output_modifier_line(
918 SHADER_BUFFER
* buffer
,
924 /* Generate a line that does the output modifier computation */
925 shader_addline(buffer
, "MUL%s %s%s, %s, %s;\n", saturate
? "_SAT" : "",
926 regstr
, write_mask
, regstr
, shift_tab
[shift
]);
929 void pshader_hw_bem(SHADER_OPCODE_ARG
* arg
) {
930 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
932 SHADER_BUFFER
* buffer
= arg
->buffer
;
934 char src_name
[2][50];
936 DWORD sampler_code
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
937 BOOL has_bumpmat
= FALSE
;
940 for(i
= 0; i
< This
->numbumpenvmatconsts
; i
++) {
941 if(This
->bumpenvmatconst
[i
].const_num
!= -1 && This
->bumpenvmatconst
[i
].texunit
== sampler_code
) {
947 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
948 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
949 strcat(dst_name
, dst_wmask
);
951 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
952 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
955 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
956 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", sampler_code
);
957 shader_addline(buffer
, "DP3 TMP.r, TMP2, %s;\n", src_name
[1]);
958 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", sampler_code
);
959 shader_addline(buffer
, "DP3 TMP.g, TMP2, %s;\n", src_name
[1]);
961 shader_addline(buffer
, "ADD %s, %s, TMP;\n", dst_name
, src_name
[0]);
963 shader_addline(buffer
, "MOV %s, %s;\n", dst_name
, src_name
[0]);
967 void pshader_hw_cnd(SHADER_OPCODE_ARG
* arg
) {
969 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
970 SHADER_BUFFER
* buffer
= arg
->buffer
;
973 char src_name
[3][50];
974 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
975 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
977 /* FIXME: support output modifiers */
979 /* Handle output register */
980 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
981 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
983 /* Generate input register names (with modifiers) */
984 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
985 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
986 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[2], 2, src_name
[2]);
988 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
989 if (shader
->baseShader
.hex_version
<= WINED3DPS_VERSION(1, 3) &&
990 arg
->opcode_token
& WINED3DSI_COISSUE
) {
991 shader_addline(buffer
, "MOV%s %s%s, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
, src_name
[1]);
993 shader_addline(buffer
, "ADD TMP, -%s, coefdiv.x;\n", src_name
[0]);
994 shader_addline(buffer
, "CMP%s %s%s, TMP, %s, %s;\n",
995 sat
? "_SAT" : "", dst_name
, dst_wmask
, src_name
[1], src_name
[2]);
998 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1001 void pshader_hw_cmp(SHADER_OPCODE_ARG
* arg
) {
1003 SHADER_BUFFER
* buffer
= arg
->buffer
;
1006 char src_name
[3][50];
1007 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1008 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1010 /* FIXME: support output modifiers */
1012 /* Handle output register */
1013 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1014 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1016 /* Generate input register names (with modifiers) */
1017 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
1018 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
1019 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[2], 2, src_name
[2]);
1021 shader_addline(buffer
, "CMP%s %s%s, %s, %s, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
,
1022 src_name
[0], src_name
[2], src_name
[1]);
1025 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1028 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1029 * dst = dot2(src0, src1) + src2 */
1030 void pshader_hw_dp2add(SHADER_OPCODE_ARG
* arg
) {
1031 SHADER_BUFFER
* buffer
= arg
->buffer
;
1034 char src_name
[3][50];
1035 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1036 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1038 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1039 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1041 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
1042 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
1043 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[2], 2, src_name
[2]);
1045 /* Emulate a DP2 with a DP3 and 0.0 */
1046 shader_addline(buffer
, "MOV TMP, %s;\n", src_name
[0]);
1047 shader_addline(buffer
, "MOV TMP.z, 0.0;\n");
1048 shader_addline(buffer
, "DP3 TMP2, TMP, %s;\n", src_name
[1]);
1049 shader_addline(buffer
, "ADD%s %s%s, TMP2, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
, src_name
[2]);
1052 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1055 /* Map the opcode 1-to-1 to the GL code */
1056 void pshader_hw_map2gl(SHADER_OPCODE_ARG
* arg
) {
1058 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1059 SHADER_BUFFER
* buffer
= arg
->buffer
;
1060 DWORD dst
= arg
->dst
;
1061 DWORD
* src
= arg
->src
;
1066 /* Output token related */
1067 char output_rname
[256];
1068 char output_wmask
[20];
1069 BOOL saturate
= FALSE
;
1070 BOOL centroid
= FALSE
;
1071 BOOL partialprecision
= FALSE
;
1074 strcpy(tmpLine
, curOpcode
->glname
);
1076 /* Process modifiers */
1077 if (0 != (dst
& WINED3DSP_DSTMOD_MASK
)) {
1078 DWORD mask
= dst
& WINED3DSP_DSTMOD_MASK
;
1080 saturate
= mask
& WINED3DSPDM_SATURATE
;
1081 centroid
= mask
& WINED3DSPDM_MSAMPCENTROID
;
1082 partialprecision
= mask
& WINED3DSPDM_PARTIALPRECISION
;
1083 mask
&= ~(WINED3DSPDM_MSAMPCENTROID
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_SATURATE
);
1085 FIXME("Unrecognized modifier(%#x)\n", mask
>> WINED3DSP_DSTMOD_SHIFT
);
1088 FIXME("Unhandled modifier(%#x)\n", mask
>> WINED3DSP_DSTMOD_SHIFT
);
1090 shift
= (dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1092 /* Generate input and output registers */
1093 if (curOpcode
->num_params
> 0) {
1094 char operands
[4][100];
1096 /* Generate input register names (with modifiers) */
1097 for (i
= 1; i
< curOpcode
->num_params
; ++i
)
1098 pshader_gen_input_modifier_line(arg
->shader
, buffer
, src
[i
-1], i
-1, operands
[i
]);
1100 /* Handle output register */
1101 pshader_get_register_name(arg
->shader
, dst
, output_rname
);
1102 strcpy(operands
[0], output_rname
);
1103 shader_arb_get_write_mask(arg
, dst
, output_wmask
);
1104 strcat(operands
[0], output_wmask
);
1106 if (saturate
&& (shift
== 0))
1107 strcat(tmpLine
, "_SAT");
1108 strcat(tmpLine
, " ");
1109 strcat(tmpLine
, operands
[0]);
1110 for (i
= 1; i
< curOpcode
->num_params
; i
++) {
1111 strcat(tmpLine
, ", ");
1112 strcat(tmpLine
, operands
[i
]);
1114 strcat(tmpLine
,";\n");
1115 shader_addline(buffer
, tmpLine
);
1117 /* A shift requires another line. */
1119 pshader_gen_output_modifier_line(buffer
, saturate
, output_wmask
, shift
, output_rname
);
1123 void pshader_hw_texkill(SHADER_OPCODE_ARG
* arg
) {
1124 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1125 DWORD hex_version
= This
->baseShader
.hex_version
;
1126 SHADER_BUFFER
* buffer
= arg
->buffer
;
1129 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1130 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1132 pshader_get_register_name(arg
->shader
, arg
->dst
, reg_dest
);
1134 if(hex_version
>= WINED3DPS_VERSION(2,0)) {
1135 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1136 shader_addline(buffer
, "KIL %s;\n", reg_dest
);
1138 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1139 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1141 shader_addline(buffer
, "MOV TMP, %s;\n", reg_dest
);
1142 shader_addline(buffer
, "MOV TMP.w, one.w;\n");
1143 shader_addline(buffer
, "KIL TMP;\n");
1147 void pshader_hw_tex(SHADER_OPCODE_ARG
* arg
) {
1148 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1149 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1151 DWORD dst
= arg
->dst
;
1152 DWORD
* src
= arg
->src
;
1153 SHADER_BUFFER
* buffer
= arg
->buffer
;
1154 DWORD hex_version
= This
->baseShader
.hex_version
;
1155 BOOL projected
= FALSE
, bias
= FALSE
;
1159 DWORD reg_dest_code
;
1160 DWORD reg_sampler_code
;
1162 /* All versions have a destination register */
1163 reg_dest_code
= dst
& WINED3DSP_REGNUM_MASK
;
1164 pshader_get_register_name(arg
->shader
, dst
, reg_dest
);
1166 /* 1.0-1.3: Use destination register as coordinate source.
1167 1.4+: Use provided coordinate source register. */
1168 if (hex_version
< WINED3DPS_VERSION(1,4))
1169 strcpy(reg_coord
, reg_dest
);
1171 pshader_gen_input_modifier_line(arg
->shader
, buffer
, src
[0], 0, reg_coord
);
1173 /* 1.0-1.4: Use destination register number as texture code.
1174 2.0+: Use provided sampler number as texure code. */
1175 if (hex_version
< WINED3DPS_VERSION(2,0))
1176 reg_sampler_code
= reg_dest_code
;
1178 reg_sampler_code
= src
[1] & WINED3DSP_REGNUM_MASK
;
1181 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1182 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1183 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1185 if(hex_version
< WINED3DPS_VERSION(1,4)) {
1187 if(reg_sampler_code
< MAX_TEXTURES
) {
1188 flags
= deviceImpl
->stateBlock
->textureState
[reg_sampler_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1190 if (flags
& WINED3DTTFF_PROJECTED
) {
1193 } else if(hex_version
< WINED3DPS_VERSION(2,0)) {
1194 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
1195 if (src_mod
== WINED3DSPSM_DZ
) {
1197 } else if(src_mod
== WINED3DSPSM_DW
) {
1201 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
1204 if(arg
->opcode_token
& WINED3DSI_TEXLD_BIAS
) {
1208 shader_hw_sample(arg
, reg_sampler_code
, reg_dest
, reg_coord
, projected
, bias
);
1211 void pshader_hw_texcoord(SHADER_OPCODE_ARG
* arg
) {
1213 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1214 DWORD dst
= arg
->dst
;
1215 SHADER_BUFFER
* buffer
= arg
->buffer
;
1216 DWORD hex_version
= This
->baseShader
.hex_version
;
1219 shader_arb_get_write_mask(arg
, dst
, tmp
);
1220 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
1221 DWORD reg
= dst
& WINED3DSP_REGNUM_MASK
;
1222 shader_addline(buffer
, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg
, tmp
, reg
);
1224 DWORD reg1
= dst
& WINED3DSP_REGNUM_MASK
;
1227 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, reg_src
);
1228 shader_addline(buffer
, "MOV R%u%s, %s;\n", reg1
, tmp
, reg_src
);
1232 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG
* arg
) {
1234 SHADER_BUFFER
* buffer
= arg
->buffer
;
1235 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1236 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1239 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1243 sprintf(dst_str
, "T%u", reg1
);
1244 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_str
);
1245 shader_addline(buffer
, "MOV TMP.r, %s.a;\n", src_str
);
1246 shader_addline(buffer
, "MOV TMP.g, %s.r;\n", src_str
);
1247 flags
= reg1
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg1
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1248 shader_hw_sample(arg
, reg1
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1251 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
1253 SHADER_BUFFER
* buffer
= arg
->buffer
;
1255 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1259 sprintf(dst_str
, "T%u", reg1
);
1260 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_str
);
1261 shader_addline(buffer
, "MOV TMP.r, %s.g;\n", src_str
);
1262 shader_addline(buffer
, "MOV TMP.g, %s.b;\n", src_str
);
1263 shader_hw_sample(arg
, reg1
, dst_str
, "TMP", FALSE
, FALSE
);
1266 void pshader_hw_texreg2rgb(SHADER_OPCODE_ARG
* arg
) {
1268 SHADER_BUFFER
* buffer
= arg
->buffer
;
1269 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1273 sprintf(dst_str
, "T%u", reg1
);
1274 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_str
);
1275 shader_hw_sample(arg
, reg1
, dst_str
, src_str
, FALSE
, FALSE
);
1278 void pshader_hw_texbem(SHADER_OPCODE_ARG
* arg
) {
1279 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1280 BOOL has_bumpmat
= FALSE
;
1281 BOOL has_luminance
= FALSE
;
1284 DWORD dst
= arg
->dst
;
1285 DWORD src
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1286 SHADER_BUFFER
* buffer
= arg
->buffer
;
1289 DWORD reg_dest_code
;
1291 /* All versions have a destination register */
1292 reg_dest_code
= dst
& WINED3DSP_REGNUM_MASK
;
1293 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
1294 pshader_get_register_name(arg
->shader
, dst
, reg_coord
);
1296 for(i
= 0; i
< This
->numbumpenvmatconsts
; i
++) {
1297 if(This
->bumpenvmatconst
[i
].const_num
!= -1 && reg_dest_code
== This
->bumpenvmatconst
[i
].texunit
) {
1302 for(i
= 0; i
< This
->numbumpenvmatconsts
; i
++) {
1303 if(This
->luminanceconst
[i
].const_num
!= -1 && reg_dest_code
== This
->luminanceconst
[i
].texunit
) {
1304 has_luminance
= TRUE
;
1310 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
1312 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code
);
1313 shader_addline(buffer
, "DP3 TMP.r, TMP2, T%u;\n", src
);
1314 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code
);
1315 shader_addline(buffer
, "DP3 TMP.g, TMP2, T%u;\n", src
);
1317 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1318 * so we can't let the GL handle this.
1320 if (((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->stateBlock
->textureState
[reg_dest_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
]
1321 & WINED3DTTFF_PROJECTED
) {
1322 shader_addline(buffer
, "RCP TMP2.a, %s.a;\n", reg_coord
);
1323 shader_addline(buffer
, "MUL TMP2.rg, %s, TMP2.a;\n", reg_coord
);
1324 shader_addline(buffer
, "ADD TMP.rg, TMP, TMP2;\n");
1326 shader_addline(buffer
, "ADD TMP.rg, TMP, %s;\n", reg_coord
);
1329 shader_hw_sample(arg
, reg_dest_code
, reg_coord
, "TMP", FALSE
, FALSE
);
1331 if(arg
->opcode
->opcode
== WINED3DSIO_TEXBEML
&& has_luminance
) {
1332 shader_addline(buffer
, "MAD TMP, T%u.z, luminance%d.x, luminance%d.y;\n",
1333 src
, reg_dest_code
, reg_dest_code
);
1334 shader_addline(buffer
, "MUL %s, %s, TMP;\n", reg_coord
, reg_coord
);
1339 if(reg_dest_code
< MAX_TEXTURES
) {
1340 tf
= ((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->stateBlock
->textureState
[reg_dest_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1344 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
1345 shader_hw_sample(arg
, reg_dest_code
, reg_coord
, reg_coord
, tf
& WINED3DTTFF_PROJECTED
, FALSE
);
1349 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
1351 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1352 SHADER_BUFFER
* buffer
= arg
->buffer
;
1355 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1356 shader_addline(buffer
, "DP3 TMP.x, T%u, %s;\n", reg
, src0_name
);
1359 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
1361 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1362 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1364 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1365 SHADER_BUFFER
* buffer
= arg
->buffer
;
1369 sprintf(dst_str
, "T%u", reg
);
1370 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1371 shader_addline(buffer
, "DP3 TMP.y, T%u, %s;\n", reg
, src0_name
);
1372 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1373 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1376 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
1378 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1379 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1380 SHADER_BUFFER
* buffer
= arg
->buffer
;
1381 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1384 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1385 shader_addline(buffer
, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state
->current_row
, reg
, src0_name
);
1386 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
1389 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
1391 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1392 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1394 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1395 SHADER_BUFFER
* buffer
= arg
->buffer
;
1396 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1400 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1401 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
1403 /* Sample the texture using the calculated coordinates */
1404 sprintf(dst_str
, "T%u", reg
);
1405 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1406 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1407 current_state
->current_row
= 0;
1410 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
1412 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1413 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1415 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1416 SHADER_BUFFER
* buffer
= arg
->buffer
;
1417 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1421 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1422 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
1424 /* Construct the eye-ray vector from w coordinates */
1425 shader_addline(buffer
, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[0]);
1426 shader_addline(buffer
, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[1]);
1427 shader_addline(buffer
, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg
);
1429 /* Calculate reflection vector
1431 shader_addline(buffer
, "DP3 TMP.w, TMP, TMP2;\n");
1432 /* The .w is ignored when sampling, so I can use TMP2.w to calculate dot(N, N) */
1433 shader_addline(buffer
, "DP3 TMP2.w, TMP, TMP;\n");
1434 shader_addline(buffer
, "RCP TMP2.w, TMP2.w;\n");
1435 shader_addline(buffer
, "MUL TMP.w, TMP.w, TMP2.w;\n");
1436 shader_addline(buffer
, "MUL TMP, TMP.w, TMP;\n");
1437 shader_addline(buffer
, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
1439 /* Sample the texture using the calculated coordinates */
1440 sprintf(dst_str
, "T%u", reg
);
1441 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1442 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1443 current_state
->current_row
= 0;
1446 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
1448 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1449 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1451 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1452 DWORD reg3
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1453 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1454 SHADER_BUFFER
* buffer
= arg
->buffer
;
1458 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1459 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
1461 /* Calculate reflection vector.
1464 * TMP.xyz = 2 * --------- * N - E
1467 * Which normalizes the normal vector
1469 shader_addline(buffer
, "DP3 TMP.w, TMP, C[%u];\n", reg3
);
1470 shader_addline(buffer
, "DP3 TMP2.w, TMP, TMP;\n");
1471 shader_addline(buffer
, "RCP TMP2.w, TMP2.w;\n");
1472 shader_addline(buffer
, "MUL TMP.w, TMP.w, TMP2.w;\n");
1473 shader_addline(buffer
, "MUL TMP, TMP.w, TMP;\n");
1474 shader_addline(buffer
, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3
);
1476 /* Sample the texture using the calculated coordinates */
1477 sprintf(dst_str
, "T%u", reg
);
1478 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1479 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1480 current_state
->current_row
= 0;
1483 void pshader_hw_texdepth(SHADER_OPCODE_ARG
* arg
) {
1484 SHADER_BUFFER
* buffer
= arg
->buffer
;
1487 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1488 * which is essentially an input, is the destination register because it is the first
1489 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1492 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1494 /* According to the msdn, the source register(must be r5) is unusable after
1495 * the texdepth instruction, so we're free to modify it
1497 shader_addline(buffer
, "MIN %s.g, %s.g, one.g;\n", dst_name
, dst_name
);
1499 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1500 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1501 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1503 shader_addline(buffer
, "RCP %s.g, %s.g;\n", dst_name
, dst_name
);
1504 shader_addline(buffer
, "MUL TMP.x, %s.r, %s.g;\n", dst_name
, dst_name
);
1505 shader_addline(buffer
, "MIN TMP.x, TMP.x, one.r;\n");
1506 shader_addline(buffer
, "MAX result.depth, TMP.x, 0.0;\n");
1509 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1510 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1511 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1512 void pshader_hw_texdp3tex(SHADER_OPCODE_ARG
* arg
) {
1513 SHADER_BUFFER
* buffer
= arg
->buffer
;
1514 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1518 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1519 shader_addline(buffer
, "MOV TMP, 0.0;\n");
1520 shader_addline(buffer
, "DP3 TMP.x, T%u, %s;\n", sampler_idx
, src0
);
1522 sprintf(dst_str
, "T%u", sampler_idx
);
1523 shader_hw_sample(arg
, sampler_idx
, dst_str
, "TMP", FALSE
/* Only one coord, can't be projected */, FALSE
);
1526 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1527 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1528 void pshader_hw_texdp3(SHADER_OPCODE_ARG
* arg
) {
1532 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1533 SHADER_BUFFER
* buffer
= arg
->buffer
;
1535 /* Handle output register */
1536 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_str
);
1537 shader_arb_get_write_mask(arg
, arg
->dst
, dst_mask
);
1539 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1540 shader_addline(buffer
, "DP3 %s%s, T%u, %s;\n", dst_str
, dst_mask
, dstreg
, src0
);
1542 /* TODO: Handle output modifiers */
1545 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1546 * Perform the 3rd row of a 3x3 matrix multiply */
1547 void pshader_hw_texm3x3(SHADER_OPCODE_ARG
* arg
) {
1548 SHADER_BUFFER
* buffer
= arg
->buffer
;
1552 DWORD dst_reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1554 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_str
);
1555 shader_arb_get_write_mask(arg
, arg
->dst
, dst_mask
);
1557 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1558 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", dst_reg
, src0
);
1559 shader_addline(buffer
, "MOV %s%s, TMP;\n", dst_str
, dst_mask
);
1561 /* TODO: Handle output modifiers */
1564 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1565 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1566 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1567 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1569 void pshader_hw_texm3x2depth(SHADER_OPCODE_ARG
* arg
) {
1570 SHADER_BUFFER
* buffer
= arg
->buffer
;
1571 DWORD dst_reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1574 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1575 shader_addline(buffer
, "DP3 TMP.y, T%u, %s;\n", dst_reg
, src0
);
1577 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1578 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1579 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1581 shader_addline(buffer
, "RCP TMP.y, TMP.y;\n");
1582 shader_addline(buffer
, "MUL TMP.x, TMP.x, TMP.y;\n");
1583 shader_addline(buffer
, "MIN TMP.x, TMP.x, one.r;\n");
1584 shader_addline(buffer
, "MAX result.depth, TMP.x, 0.0;\n");
1587 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1588 Vertex/Pixel shaders to ARB_vertex_program codes */
1589 void shader_hw_mnxn(SHADER_OPCODE_ARG
* arg
) {
1592 int nComponents
= 0;
1593 SHADER_OPCODE_ARG tmpArg
;
1595 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1597 /* Set constants for the temporary argument */
1598 tmpArg
.shader
= arg
->shader
;
1599 tmpArg
.buffer
= arg
->buffer
;
1600 tmpArg
.src
[0] = arg
->src
[0];
1601 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1602 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1603 tmpArg
.reg_maps
= arg
->reg_maps
;
1605 switch(arg
->opcode
->opcode
) {
1606 case WINED3DSIO_M4x4
:
1608 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1610 case WINED3DSIO_M4x3
:
1612 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1614 case WINED3DSIO_M3x4
:
1616 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1618 case WINED3DSIO_M3x3
:
1620 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1622 case WINED3DSIO_M3x2
:
1624 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1630 for (i
= 0; i
< nComponents
; i
++) {
1631 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1632 tmpArg
.src
[1] = arg
->src
[1]+i
;
1633 vshader_hw_map2gl(&tmpArg
);
1637 void vshader_hw_rsq_rcp(SHADER_OPCODE_ARG
* arg
) {
1638 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1639 SHADER_BUFFER
* buffer
= arg
->buffer
;
1640 DWORD dst
= arg
->dst
;
1641 DWORD src
= arg
->src
[0];
1642 DWORD swizzle
= (src
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
1646 strcpy(tmpLine
, curOpcode
->glname
); /* Opcode */
1647 vshader_program_add_param(arg
, dst
, FALSE
, tmpLine
); /* Destination */
1648 strcat(tmpLine
, ",");
1649 vshader_program_add_param(arg
, src
, TRUE
, tmpLine
);
1650 if ((WINED3DSP_NOSWIZZLE
>> WINED3DSP_SWIZZLE_SHIFT
) == swizzle
) {
1651 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1654 strcat(tmpLine
, ".w");
1657 shader_addline(buffer
, "%s;\n", tmpLine
);
1660 void shader_hw_nrm(SHADER_OPCODE_ARG
* arg
) {
1661 SHADER_BUFFER
* buffer
= arg
->buffer
;
1665 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1666 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1668 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1669 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1671 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
);
1672 shader_addline(buffer
, "DP3 TMP, %s, %s;\n", src_name
, src_name
);
1673 shader_addline(buffer
, "RSQ TMP, TMP.x;\n");
1674 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1675 shader_addline(buffer
, "MUL%s %s%s, %s, TMP;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
,
1679 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1682 void shader_hw_sincos(SHADER_OPCODE_ARG
* arg
) {
1683 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1684 * must contain fixed constants. So we need a separate function to filter those constants and
1687 SHADER_BUFFER
* buffer
= arg
->buffer
;
1691 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1692 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1694 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1695 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1697 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
);
1698 shader_addline(buffer
, "SCS%s %s%s, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
,
1702 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1706 /* TODO: merge with pixel shader */
1707 /* Map the opcode 1-to-1 to the GL code */
1708 void vshader_hw_map2gl(SHADER_OPCODE_ARG
* arg
) {
1710 IWineD3DVertexShaderImpl
*shader
= (IWineD3DVertexShaderImpl
*) arg
->shader
;
1711 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1712 SHADER_BUFFER
* buffer
= arg
->buffer
;
1713 DWORD dst
= arg
->dst
;
1714 DWORD
* src
= arg
->src
;
1716 DWORD dst_regtype
= shader_get_regtype(dst
);
1720 if ((curOpcode
->opcode
== WINED3DSIO_MOV
&& dst_regtype
== WINED3DSPR_ADDR
) || curOpcode
->opcode
== WINED3DSIO_MOVA
) {
1721 if(shader
->rel_offset
) {
1722 memset(tmpLine
, 0, sizeof(tmpLine
));
1723 vshader_program_add_param(arg
, src
[0], TRUE
, tmpLine
);
1724 shader_addline(buffer
, "ADD TMP.x, %s, helper_const.z;\n", tmpLine
);
1725 shader_addline(buffer
, "ARL A0.x, TMP.x;\n");
1728 strcpy(tmpLine
, "ARL");
1731 strcpy(tmpLine
, curOpcode
->glname
);
1733 if (curOpcode
->num_params
> 0) {
1734 vshader_program_add_param(arg
, dst
, FALSE
, tmpLine
);
1735 for (i
= 1; i
< curOpcode
->num_params
; ++i
) {
1736 strcat(tmpLine
, ",");
1737 vshader_program_add_param(arg
, src
[i
-1], TRUE
, tmpLine
);
1740 shader_addline(buffer
, "%s;\n", tmpLine
);
1743 static GLuint
create_arb_blt_vertex_program(WineD3D_GL_Info
*gl_info
) {
1744 GLuint program_id
= 0;
1745 const char *blt_vprogram
=
1747 "PARAM c[1] = { { 1, 0.5 } };\n"
1748 "MOV result.position, vertex.position;\n"
1749 "MOV result.color, c[0].x;\n"
1750 "MAD result.texcoord[0].y, -vertex.position, c[0], c[0];\n"
1751 "MAD result.texcoord[0].x, vertex.position, c[0].y, c[0].y;\n"
1754 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
1755 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, program_id
));
1756 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_vprogram
), blt_vprogram
));
1758 if (glGetError() == GL_INVALID_OPERATION
) {
1760 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
1761 FIXME("Vertex program error at position %d: %s\n", pos
,
1762 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
1768 static GLuint
create_arb_blt_fragment_program(WineD3D_GL_Info
*gl_info
) {
1769 GLuint program_id
= 0;
1770 const char *blt_fprogram
=
1773 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1774 "MOV result.depth.z, R0.x;\n"
1777 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
1778 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, program_id
));
1779 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_fprogram
), blt_fprogram
));
1781 if (glGetError() == GL_INVALID_OPERATION
) {
1783 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
1784 FIXME("Fragment program error at position %d: %s\n", pos
,
1785 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
1791 static void shader_arb_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
1792 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1793 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1794 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1797 TRACE("Using vertex shader\n");
1799 priv
->current_vprogram_id
= ((IWineD3DVertexShaderImpl
*)This
->stateBlock
->vertexShader
)->baseShader
.prgId
;
1801 /* Bind the vertex program */
1802 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
1803 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1805 /* Enable OpenGL vertex programs */
1806 glEnable(GL_VERTEX_PROGRAM_ARB
);
1807 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1808 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
1809 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM
)) {
1810 priv
->current_vprogram_id
= 0;
1811 glDisable(GL_VERTEX_PROGRAM_ARB
);
1812 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1816 TRACE("Using pixel shader\n");
1818 priv
->current_fprogram_id
= ((IWineD3DPixelShaderImpl
*)This
->stateBlock
->pixelShader
)->baseShader
.prgId
;
1820 /* Bind the fragment program */
1821 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
1822 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1824 /* Enable OpenGL fragment programs */
1825 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1826 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1827 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
1828 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) {
1829 priv
->current_fprogram_id
= 0;
1830 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1831 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1835 static void shader_arb_select_depth_blt(IWineD3DDevice
*iface
) {
1836 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1837 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1838 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1840 if (!priv
->depth_blt_vprogram_id
) priv
->depth_blt_vprogram_id
= create_arb_blt_vertex_program(gl_info
);
1841 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->depth_blt_vprogram_id
));
1842 glEnable(GL_VERTEX_PROGRAM_ARB
);
1844 if (!priv
->depth_blt_fprogram_id
) priv
->depth_blt_fprogram_id
= create_arb_blt_fragment_program(gl_info
);
1845 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->depth_blt_fprogram_id
));
1846 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1849 static void shader_arb_deselect_depth_blt(IWineD3DDevice
*iface
) {
1850 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1851 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1852 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1854 if (priv
->current_vprogram_id
) {
1855 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
1856 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1858 glEnable(GL_VERTEX_PROGRAM_ARB
);
1859 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1861 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
1863 glDisable(GL_VERTEX_PROGRAM_ARB
);
1864 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1867 if (priv
->current_fprogram_id
) {
1868 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
1869 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1871 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1872 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1874 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
1875 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) {
1876 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1877 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1881 static void shader_arb_cleanup(IWineD3DDevice
*iface
) {
1882 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1883 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1884 if (GL_SUPPORT(ARB_VERTEX_PROGRAM
)) glDisable(GL_VERTEX_PROGRAM_ARB
);
1885 if (GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1888 static void shader_arb_destroy(IWineD3DBaseShader
*iface
) {
1889 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
1890 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->adapter
->gl_info
;
1893 GL_EXTCALL(glDeleteProgramsARB(1, &This
->baseShader
.prgId
));
1894 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->baseShader.prgId))");
1896 This
->baseShader
.prgId
= 0;
1897 This
->baseShader
.is_compiled
= FALSE
;
1900 static HRESULT
shader_arb_alloc(IWineD3DDevice
*iface
) {
1901 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1902 This
->shader_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_arb_priv
));
1906 static void shader_arb_free(IWineD3DDevice
*iface
) {
1907 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1908 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1909 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1911 if(priv
->depth_blt_vprogram_id
) {
1912 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_vprogram_id
));
1914 if(priv
->depth_blt_fprogram_id
) {
1915 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id
));
1918 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
1921 static BOOL
shader_arb_dirty_const(IWineD3DDevice
*iface
) {
1925 static void shader_arb_generate_pshader(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
) {
1926 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
1927 shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
1928 CONST DWORD
*function
= This
->baseShader
.function
;
1929 const char *fragcolor
;
1930 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
1931 local_constant
* lconst
;
1933 /* Create the hw ARB shader */
1934 shader_addline(buffer
, "!!ARBfp1.0\n");
1936 shader_addline(buffer
, "TEMP TMP;\n"); /* Used in matrix ops */
1937 shader_addline(buffer
, "TEMP TMP2;\n"); /* Used in matrix ops */
1938 shader_addline(buffer
, "TEMP TA;\n"); /* Used for modifiers */
1939 shader_addline(buffer
, "TEMP TB;\n"); /* Used for modifiers */
1940 shader_addline(buffer
, "TEMP TC;\n"); /* Used for modifiers */
1941 shader_addline(buffer
, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
1942 shader_addline(buffer
, "PARAM coefmul = { 2, 4, 8, 16 };\n");
1943 shader_addline(buffer
, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
1945 /* Base Declarations */
1946 shader_generate_arb_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
);
1948 /* We need two variables for fog blending */
1949 shader_addline(buffer
, "TEMP TMP_FOG;\n");
1950 if (This
->baseShader
.hex_version
>= WINED3DPS_VERSION(2,0)) {
1951 shader_addline(buffer
, "TEMP TMP_COLOR;\n");
1954 /* Base Shader Body */
1955 shader_generate_main( (IWineD3DBaseShader
*) This
, buffer
, reg_maps
, function
);
1957 /* calculate fog and blend it
1958 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
1959 * -1/(e-s) and e/(e-s) respectively.
1961 shader_addline(buffer
, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
1963 if (This
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
1966 fragcolor
= "TMP_COLOR";
1968 if(This
->srgb_enabled
) {
1969 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
1971 /* Calculate the > 0.0031308 case */
1972 shader_addline(buffer
, "POW TMP.x, %s.x, srgb_pow.x;\n", fragcolor
);
1973 shader_addline(buffer
, "POW TMP.y, %s.y, srgb_pow.y;\n", fragcolor
);
1974 shader_addline(buffer
, "POW TMP.z, %s.z, srgb_pow.z;\n", fragcolor
);
1975 shader_addline(buffer
, "MUL TMP, TMP, srgb_mul_hi;\n");
1976 shader_addline(buffer
, "SUB TMP, TMP, srgb_sub_hi;\n");
1977 /* Calculate the < case */
1978 shader_addline(buffer
, "MUL TMP2, srgb_mul_low, %s;\n", fragcolor
);
1979 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
1980 shader_addline(buffer
, "SLT TA, srgb_comparison, %s;\n", fragcolor
);
1981 shader_addline(buffer
, "SGE TB, srgb_comparison, %s;\n", fragcolor
);
1982 /* Store the components > 0.0031308 in the destination */
1983 shader_addline(buffer
, "MUL %s, TMP, TA;\n", fragcolor
);
1984 /* Add the components that are < 0.0031308 */
1985 shader_addline(buffer
, "MAD result.color.xyz, TMP2, TB, %s;\n", fragcolor
);
1986 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
1988 if (This
->baseShader
.hex_version
< WINED3DPS_VERSION(3,0)) {
1989 shader_addline(buffer
, "LRP result.color.rgb, TMP_FOG.x, %s, state.fog.color;\n", fragcolor
);
1990 shader_addline(buffer
, "MOV result.color.a, %s.a;\n", fragcolor
);
1993 shader_addline(buffer
, "END\n");
1995 /* TODO: change to resource.glObjectHandle or something like that */
1996 GL_EXTCALL(glGenProgramsARB(1, &This
->baseShader
.prgId
));
1998 TRACE("Creating a hw pixel shader, prg=%d\n", This
->baseShader
.prgId
);
1999 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, This
->baseShader
.prgId
));
2001 TRACE("Created hw pixel shader, prg=%d\n", This
->baseShader
.prgId
);
2002 /* Create the program and check for errors */
2003 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2004 buffer
->bsize
, buffer
->buffer
));
2006 if (glGetError() == GL_INVALID_OPERATION
) {
2008 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
2009 FIXME("HW PixelShader Error at position %d: %s\n",
2010 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
2011 This
->baseShader
.prgId
= -1;
2014 /* Load immediate constants */
2015 if(!This
->baseShader
.load_local_constsF
) {
2016 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
2017 float *value
= (float *) lconst
->value
;
2018 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, lconst
->idx
, value
));
2019 checkGLcall("glProgramLocalParameter4fvARB");
2024 static void shader_arb_generate_vshader(IWineD3DVertexShader
*iface
, SHADER_BUFFER
*buffer
) {
2025 IWineD3DVertexShaderImpl
*This
= (IWineD3DVertexShaderImpl
*)iface
;
2026 shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
2027 CONST DWORD
*function
= This
->baseShader
.function
;
2028 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
2029 local_constant
* lconst
;
2031 /* Create the hw ARB shader */
2032 shader_addline(buffer
, "!!ARBvp1.0\n");
2033 shader_addline(buffer
, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This
->rel_offset
);
2035 /* Mesa supports only 95 constants */
2036 if (GL_VEND(MESA
) || GL_VEND(WINE
))
2037 This
->baseShader
.limits
.constant_float
=
2038 min(95, This
->baseShader
.limits
.constant_float
);
2040 shader_addline(buffer
, "TEMP TMP;\n");
2042 /* Base Declarations */
2043 shader_generate_arb_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
);
2045 /* We need a constant to fixup the final position */
2046 shader_addline(buffer
, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS
);
2048 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
2049 * for output parameters. D3D in theory does not do that either, but some applications depend on a
2050 * proper initialization of the secondary color, and programs using the fixed function pipeline without
2051 * a replacement shader depend on the texcoord.w being set properly.
2053 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
2054 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
2055 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
2056 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
2057 * this can eat a number of instructions, so skip it unless this cap is set as well
2059 if(!GL_SUPPORT(NV_VERTEX_PROGRAM
)) {
2060 shader_addline(buffer
, "MOV result.color.secondary, -helper_const.wwwy;\n");
2062 if((GLINFO_LOCATION
).set_texcoord_w
) {
2064 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
2065 if(This
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
2066 This
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
2067 shader_addline(buffer
, "MOV result.texcoord[%u].w, -helper_const.y;\n", i
);
2073 /* Base Shader Body */
2074 shader_generate_main( (IWineD3DBaseShader
*) This
, buffer
, reg_maps
, function
);
2076 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
2078 shader_addline(buffer
, "MOV result.fogcoord, TMP_OUT.z;\n");
2080 /* Write the final position.
2082 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2083 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2084 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2085 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2087 shader_addline(buffer
, "MUL TMP, posFixup, TMP_OUT.w;\n");
2088 shader_addline(buffer
, "ADD TMP_OUT.x, TMP_OUT.x, TMP.z;\n");
2089 shader_addline(buffer
, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TMP.w;\n");
2091 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2092 * and the glsl equivalent
2094 shader_addline(buffer
, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2096 shader_addline(buffer
, "MOV result.position, TMP_OUT;\n");
2098 shader_addline(buffer
, "END\n");
2100 /* TODO: change to resource.glObjectHandle or something like that */
2101 GL_EXTCALL(glGenProgramsARB(1, &This
->baseShader
.prgId
));
2103 TRACE("Creating a hw vertex shader, prg=%d\n", This
->baseShader
.prgId
);
2104 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, This
->baseShader
.prgId
));
2106 TRACE("Created hw vertex shader, prg=%d\n", This
->baseShader
.prgId
);
2107 /* Create the program and check for errors */
2108 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2109 buffer
->bsize
, buffer
->buffer
));
2111 if (glGetError() == GL_INVALID_OPERATION
) {
2113 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
2114 FIXME("HW VertexShader Error at position %d: %s\n",
2115 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
2116 This
->baseShader
.prgId
= -1;
2119 /* Load immediate constants */
2120 if(!This
->baseShader
.load_local_constsF
) {
2121 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
2122 float *value
= (float *) lconst
->value
;
2123 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, lconst
->idx
, value
));
2128 static void shader_arb_get_caps(WINED3DDEVTYPE devtype
, WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
) {
2129 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2130 * then overwrite the shader specific ones
2132 none_shader_backend
.shader_get_caps(devtype
, gl_info
, pCaps
);
2134 if(GL_SUPPORT(ARB_VERTEX_PROGRAM
)) {
2135 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(1,1);
2136 TRACE_(d3d_caps
)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2137 pCaps
->MaxVertexShaderConst
= GL_LIMITS(vshader_constantsF
);
2140 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) {
2141 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(1,4);
2142 pCaps
->PixelShader1xMaxValue
= 8.0;
2143 TRACE_(d3d_caps
)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2147 const shader_backend_t arb_program_shader_backend
= {
2149 shader_arb_select_depth_blt
,
2150 shader_arb_deselect_depth_blt
,
2151 shader_arb_load_constants
,
2153 shader_arb_color_correction
,
2157 shader_arb_dirty_const
,
2158 shader_arb_generate_pshader
,
2159 shader_arb_generate_vshader
,
2160 shader_arb_get_caps
,