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 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
43 * so upload them above that
45 #define ARB_SHADER_PRIVCONST_BASE GL_LIMITS(vshader_constantsF)
46 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
48 /* ARB_program_shader private data */
49 struct shader_arb_priv
{
50 GLuint current_vprogram_id
;
51 GLuint current_fprogram_id
;
52 GLuint depth_blt_vprogram_id
;
53 GLuint depth_blt_fprogram_id
[tex_type_count
];
54 BOOL use_arbfp_fixed_func
;
55 struct hash_table_t
*fragment_shaders
;
58 /********************************************************
59 * ARB_[vertex/fragment]_program helper functions follow
60 ********************************************************/
63 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
64 * When constant_list == NULL, it will load all the constants.
66 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
67 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
69 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl
* This
, const WineD3D_GL_Info
*gl_info
,
70 GLuint target_type
, unsigned int max_constants
, const float *constants
, char *dirty_consts
)
72 local_constant
* lconst
;
76 if (TRACE_ON(d3d_shader
)) {
77 for(i
= 0; i
< max_constants
; i
++) {
78 if(!dirty_consts
[i
]) continue;
79 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
80 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
81 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
84 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
85 if(target_type
== GL_FRAGMENT_PROGRAM_ARB
&&
86 WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1) {
88 for(i
= 0; i
< max_constants
; i
++) {
89 if(!dirty_consts
[i
]) continue;
93 if(constants
[j
+ 0] > 1.0) lcl_const
[0] = 1.0;
94 else if(constants
[j
+ 0] < -1.0) lcl_const
[0] = -1.0;
95 else lcl_const
[0] = constants
[j
+ 0];
97 if(constants
[j
+ 1] > 1.0) lcl_const
[1] = 1.0;
98 else if(constants
[j
+ 1] < -1.0) lcl_const
[1] = -1.0;
99 else lcl_const
[1] = constants
[j
+ 1];
101 if(constants
[j
+ 2] > 1.0) lcl_const
[2] = 1.0;
102 else if(constants
[j
+ 2] < -1.0) lcl_const
[2] = -1.0;
103 else lcl_const
[2] = constants
[j
+ 2];
105 if(constants
[j
+ 3] > 1.0) lcl_const
[3] = 1.0;
106 else if(constants
[j
+ 3] < -1.0) lcl_const
[3] = -1.0;
107 else lcl_const
[3] = constants
[j
+ 3];
109 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, lcl_const
));
112 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS
)) {
113 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
114 * or just reloading *all* constants at once
116 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
118 for(i
= 0; i
< max_constants
; i
++) {
119 if(!dirty_consts
[i
]) continue;
121 /* Find the next block of dirty constants */
124 for(i
++; (i
< max_constants
) && dirty_consts
[i
]; i
++) {
128 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type
, j
, i
- j
, constants
+ (j
* 4)));
131 for(i
= 0; i
< max_constants
; i
++) {
132 if(dirty_consts
[i
]) {
134 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, constants
+ (i
* 4)));
139 checkGLcall("glProgramEnvParameter4fvARB()");
141 /* Load immediate constants */
142 if(This
->baseShader
.load_local_constsF
) {
143 if (TRACE_ON(d3d_shader
)) {
144 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
145 GLfloat
* values
= (GLfloat
*)lconst
->value
;
146 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
147 values
[0], values
[1], values
[2], values
[3]);
150 /* Immediate constants are clamped for 1.X shaders at loading times */
152 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
153 dirty_consts
[lconst
->idx
] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
154 ret
= max(ret
, lconst
->idx
+ 1);
155 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, lconst
->idx
, (GLfloat
*)lconst
->value
));
157 checkGLcall("glProgramEnvParameter4fvARB()");
158 return ret
; /* The loaded immediate constants need reloading for the next shader */
160 return 0; /* No constants are dirty now */
165 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
167 * We only support float constants in ARB at the moment, so don't
168 * worry about the Integers or Booleans
170 static void shader_arb_load_constants(
171 IWineD3DDevice
* device
,
173 char useVertexShader
) {
175 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
176 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
177 const WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
180 if (useVertexShader
) {
181 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
183 /* Load DirectX 9 float constants for vertex shader */
184 deviceImpl
->highest_dirty_vs_const
= shader_arb_load_constantsF(
185 vshader
, gl_info
, GL_VERTEX_PROGRAM_ARB
,
186 deviceImpl
->highest_dirty_vs_const
,
187 stateBlock
->vertexShaderConstantF
,
188 deviceImpl
->activeContext
->vshader_const_dirty
);
190 /* Upload the position fixup */
191 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, ARB_SHADER_PRIVCONST_POS
, deviceImpl
->posFixup
));
194 if (usePixelShader
) {
196 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
197 IWineD3DPixelShaderImpl
*psi
= (IWineD3DPixelShaderImpl
*) pshader
;
199 /* Load DirectX 9 float constants for pixel shader */
200 deviceImpl
->highest_dirty_ps_const
= shader_arb_load_constantsF(
201 pshader
, gl_info
, GL_FRAGMENT_PROGRAM_ARB
,
202 deviceImpl
->highest_dirty_ps_const
,
203 stateBlock
->pixelShaderConstantF
,
204 deviceImpl
->activeContext
->pshader_const_dirty
);
206 for(i
= 0; i
< psi
->numbumpenvmatconsts
; i
++) {
207 /* The state manager takes care that this function is always called if the bump env matrix changes
209 const float *data
= (const float *)&stateBlock
->textureState
[(int) psi
->bumpenvmatconst
[i
].texunit
][WINED3DTSS_BUMPENVMAT00
];
210 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, psi
->bumpenvmatconst
[i
].const_num
, data
));
211 deviceImpl
->activeContext
->pshader_const_dirty
[psi
->bumpenvmatconst
[i
].const_num
] = 1;
213 if(psi
->luminanceconst
[i
].const_num
!= -1) {
214 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
215 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
216 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
217 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
219 const float *scale
= (const float *)&stateBlock
->textureState
[(int) psi
->luminanceconst
[i
].texunit
][WINED3DTSS_BUMPENVLSCALE
];
220 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, psi
->luminanceconst
[i
].const_num
, scale
));
221 deviceImpl
->activeContext
->pshader_const_dirty
[psi
->luminanceconst
[i
].const_num
] = 1;
227 /* Generate the variable & register declarations for the ARB_vertex_program output target */
228 static void shader_generate_arb_declarations(IWineD3DBaseShader
*iface
, const shader_reg_maps
*reg_maps
,
229 SHADER_BUFFER
*buffer
, const WineD3D_GL_Info
*gl_info
)
231 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
232 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
234 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
235 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
236 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
237 UINT extra_constants_needed
= 0;
238 const local_constant
*lconst
;
240 /* Temporary Output register */
241 shader_addline(buffer
, "TEMP TMP_OUT;\n");
243 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
244 if (reg_maps
->temporary
[i
])
245 shader_addline(buffer
, "TEMP R%u;\n", i
);
248 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
249 if (reg_maps
->address
[i
])
250 shader_addline(buffer
, "ADDRESS A%d;\n", i
);
253 for(i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
254 if (reg_maps
->texcoord
[i
])
255 shader_addline(buffer
,"TEMP T%u;\n", i
);
258 /* Texture coordinate registers must be pre-loaded */
259 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
260 if (reg_maps
->texcoord
[i
])
261 shader_addline(buffer
, "MOV T%u, fragment.texcoord[%u];\n", i
, i
);
264 for(i
= 0; i
< (sizeof(reg_maps
->bumpmat
) / sizeof(reg_maps
->bumpmat
[0])); i
++) {
265 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) This
;
266 if(!reg_maps
->bumpmat
[i
]) continue;
268 cur
= ps
->numbumpenvmatconsts
;
269 ps
->bumpenvmatconst
[cur
].const_num
= -1;
270 ps
->bumpenvmatconst
[cur
].texunit
= i
;
271 ps
->luminanceconst
[cur
].const_num
= -1;
272 ps
->luminanceconst
[cur
].texunit
= i
;
274 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
275 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
278 if(max_constantsF
+ extra_constants_needed
< GL_LIMITS(pshader_constantsF
)) {
279 ps
->bumpenvmatconst
[cur
].const_num
= max_constantsF
+ extra_constants_needed
;
280 shader_addline(buffer
, "PARAM bumpenvmat%d = program.env[%d];\n",
281 i
, ps
->bumpenvmatconst
[cur
].const_num
);
282 extra_constants_needed
++;
284 if(reg_maps
->luminanceparams
&& max_constantsF
+ extra_constants_needed
< GL_LIMITS(pshader_constantsF
)) {
285 ((IWineD3DPixelShaderImpl
*)This
)->luminanceconst
[cur
].const_num
= max_constantsF
+ extra_constants_needed
;
286 shader_addline(buffer
, "PARAM luminance%d = program.env[%d];\n",
287 i
, ps
->luminanceconst
[cur
].const_num
);
288 extra_constants_needed
++;
289 } else if(reg_maps
->luminanceparams
) {
290 FIXME("No free constant to load the luminance parameters\n");
293 FIXME("No free constant found to load environemnt bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
296 ps
->numbumpenvmatconsts
= cur
+ 1;
299 if(device
->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
] && pshader
) {
300 shader_addline(buffer
, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
301 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
302 shader_addline(buffer
, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
303 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
304 shader_addline(buffer
, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
305 srgb_pow
, srgb_pow
, srgb_pow
);
306 shader_addline(buffer
, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
307 srgb_mul_high
, srgb_mul_high
, srgb_mul_high
);
308 shader_addline(buffer
, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
309 srgb_sub_high
, srgb_sub_high
, srgb_sub_high
);
312 /* Load local constants using the program-local space,
313 * this avoids reloading them each time the shader is used
315 if(!This
->baseShader
.load_local_constsF
) {
316 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
317 shader_addline(buffer
, "PARAM C%u = program.local[%u];\n", lconst
->idx
,
322 /* we use the array-based constants array if the local constants are marked for loading,
323 * because then we use indirect addressing, or when the local constant list is empty,
324 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
325 * local constants do not declare the loaded constants as an array because ARB compilers usually
326 * do not optimize unused constants away
328 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) {
329 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
330 shader_addline(buffer
, "PARAM C[%d] = { program.env[0..%d] };\n",
331 max_constantsF
, max_constantsF
- 1);
333 for(i
= 0; i
< max_constantsF
; i
++) {
334 if(!shader_constant_is_local(This
, i
)) {
335 shader_addline(buffer
, "PARAM C%d = program.env[%d];\n",i
, i
);
341 static const char * const shift_tab
[] = {
342 "dummy", /* 0 (none) */
343 "coefmul.x", /* 1 (x2) */
344 "coefmul.y", /* 2 (x4) */
345 "coefmul.z", /* 3 (x8) */
346 "coefmul.w", /* 4 (x16) */
347 "dummy", /* 5 (x32) */
348 "dummy", /* 6 (x64) */
349 "dummy", /* 7 (x128) */
350 "dummy", /* 8 (d256) */
351 "dummy", /* 9 (d128) */
352 "dummy", /* 10 (d64) */
353 "dummy", /* 11 (d32) */
354 "coefdiv.w", /* 12 (d16) */
355 "coefdiv.z", /* 13 (d8) */
356 "coefdiv.y", /* 14 (d4) */
357 "coefdiv.x" /* 15 (d2) */
360 static void shader_arb_get_write_mask(const SHADER_OPCODE_ARG
*arg
, const DWORD param
, char *write_mask
)
362 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
363 char *ptr
= write_mask
;
364 char vshader
= shader_is_vshader_version(This
->baseShader
.hex_version
);
366 if(vshader
&& shader_get_regtype(param
) == WINED3DSPR_ADDR
) {
369 } else if ((param
& WINED3DSP_WRITEMASK_ALL
) != WINED3DSP_WRITEMASK_ALL
) {
371 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
372 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
373 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
374 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
380 static void shader_arb_get_swizzle(const DWORD param
, BOOL fixup
, char *swizzle_str
) {
381 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
382 * but addressed as "rgba". To fix this we need to swap the register's x
383 * and z components. */
384 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
385 char *ptr
= swizzle_str
;
387 /* swizzle bits fields: wwzzyyxx */
388 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
389 DWORD swizzle_x
= swizzle
& 0x03;
390 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
391 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
392 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
394 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
395 * generate a swizzle string. Unless we need to our own swizzling. */
396 if ((WINED3DSP_NOSWIZZLE
>> WINED3DSP_SWIZZLE_SHIFT
) != swizzle
|| fixup
) {
398 if (swizzle_x
== swizzle_y
&& swizzle_x
== swizzle_z
&& swizzle_x
== swizzle_w
) {
399 *ptr
++ = swizzle_chars
[swizzle_x
];
401 *ptr
++ = swizzle_chars
[swizzle_x
];
402 *ptr
++ = swizzle_chars
[swizzle_y
];
403 *ptr
++ = swizzle_chars
[swizzle_z
];
404 *ptr
++ = swizzle_chars
[swizzle_w
];
411 static void pshader_get_register_name(IWineD3DBaseShader
* iface
,
412 const DWORD param
, char* regstr
) {
414 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
415 DWORD regtype
= shader_get_regtype(param
);
416 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
419 case WINED3DSPR_TEMP
:
420 sprintf(regstr
, "R%u", reg
);
422 case WINED3DSPR_INPUT
:
424 strcpy(regstr
, "fragment.color.primary");
426 strcpy(regstr
, "fragment.color.secondary");
429 case WINED3DSPR_CONST
:
430 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) {
431 sprintf(regstr
, "C[%u]", reg
);
433 sprintf(regstr
, "C%u", reg
);
436 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
437 sprintf(regstr
,"T%u", reg
);
439 case WINED3DSPR_COLOROUT
:
441 sprintf(regstr
, "TMP_COLOR");
443 /* TODO: See GL_ARB_draw_buffers */
444 FIXME("Unsupported write to render target %u\n", reg
);
445 sprintf(regstr
, "unsupported_register");
448 case WINED3DSPR_DEPTHOUT
:
449 sprintf(regstr
, "result.depth");
451 case WINED3DSPR_ATTROUT
:
452 sprintf(regstr
, "oD[%u]", reg
);
454 case WINED3DSPR_TEXCRDOUT
:
455 sprintf(regstr
, "oT[%u]", reg
);
458 FIXME("Unhandled register name Type(%d)\n", regtype
);
459 sprintf(regstr
, "unrecognized_register");
464 /* TODO: merge with pixel shader */
465 static void vshader_program_add_param(const SHADER_OPCODE_ARG
*arg
, const DWORD param
, BOOL is_input
, char *hwLine
)
467 IWineD3DVertexShaderImpl
* This
= (IWineD3DVertexShaderImpl
*) arg
->shader
;
469 /* oPos, oFog and oPts in D3D */
470 static const char * const hwrastout_reg_names
[] = { "TMP_OUT", "result.fogcoord", "result.pointsize" };
472 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
473 DWORD regtype
= shader_get_regtype(param
);
475 BOOL is_color
= FALSE
;
477 if ((param
& WINED3DSP_SRCMOD_MASK
) == WINED3DSPSM_NEG
) {
478 strcat(hwLine
, " -");
484 case WINED3DSPR_TEMP
:
485 sprintf(tmpReg
, "R%u", reg
);
486 strcat(hwLine
, tmpReg
);
488 case WINED3DSPR_INPUT
:
490 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
493 sprintf(tmpReg
, "vertex.attrib[%u]", reg
);
494 strcat(hwLine
, tmpReg
);
496 case WINED3DSPR_CONST
:
497 if(param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
498 if(reg
>= This
->rel_offset
) {
499 sprintf(tmpReg
, "C[A0.x + %u]", reg
- This
->rel_offset
);
501 sprintf(tmpReg
, "C[A0.x - %u]", -reg
+ This
->rel_offset
);
504 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) {
505 sprintf(tmpReg
, "C[%u]", reg
);
507 sprintf(tmpReg
, "C%u", reg
);
510 strcat(hwLine
, tmpReg
);
512 case WINED3DSPR_ADDR
: /*case D3DSPR_TEXTURE:*/
513 sprintf(tmpReg
, "A%u", reg
);
514 strcat(hwLine
, tmpReg
);
516 case WINED3DSPR_RASTOUT
:
517 sprintf(tmpReg
, "%s", hwrastout_reg_names
[reg
]);
518 strcat(hwLine
, tmpReg
);
520 case WINED3DSPR_ATTROUT
:
522 strcat(hwLine
, "result.color.primary");
524 strcat(hwLine
, "result.color.secondary");
527 case WINED3DSPR_TEXCRDOUT
:
528 sprintf(tmpReg
, "result.texcoord[%u]", reg
);
529 strcat(hwLine
, tmpReg
);
532 FIXME("Unknown reg type %d %d\n", regtype
, reg
);
533 strcat(hwLine
, "unrecognized_register");
539 shader_arb_get_write_mask(arg
, param
, write_mask
);
540 strcat(hwLine
, write_mask
);
543 shader_arb_get_swizzle(param
, is_color
, swizzle
);
544 strcat(hwLine
, swizzle
);
548 static void shader_hw_sample(const SHADER_OPCODE_ARG
*arg
, DWORD sampler_idx
, const char *dst_str
,
549 const char *coord_reg
, BOOL projected
, BOOL bias
)
551 SHADER_BUFFER
* buffer
= arg
->buffer
;
552 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
553 const char *tex_type
;
555 switch(sampler_type
) {
562 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
563 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
564 if(device
->stateBlock
->textures
[sampler_idx
] &&
565 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
573 case WINED3DSTT_VOLUME
:
577 case WINED3DSTT_CUBE
:
582 ERR("Unexpected texture type %d\n", sampler_type
);
587 /* Shouldn't be possible, but let's check for it */
588 if(projected
) FIXME("Biased and Projected texture sampling\n");
589 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
590 shader_addline(buffer
, "TXB %s, %s, texture[%u], %s;\n", dst_str
, coord_reg
, sampler_idx
, tex_type
);
591 } else if (projected
) {
592 shader_addline(buffer
, "TXP %s, %s, texture[%u], %s;\n", dst_str
, coord_reg
, sampler_idx
, tex_type
);
594 shader_addline(buffer
, "TEX %s, %s, texture[%u], %s;\n", dst_str
, coord_reg
, sampler_idx
, tex_type
);
598 static void gen_color_correction(SHADER_BUFFER
*buffer
, const char *reg
, const char *writemask
,
599 const char *one
, const char *two
, WINED3DFORMAT fmt
,
600 const WineD3D_GL_Info
*gl_info
)
603 case WINED3DFMT_V8U8
:
604 case WINED3DFMT_V16U16
:
605 if(GL_SUPPORT(NV_TEXTURE_SHADER
) && fmt
== WINED3DFMT_V8U8
) {
606 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-)
607 * The dx7 sdk BumpEarth demo needs it because it uses BUMPENVMAPLUMINANCE with V8U8.
608 * With the luminance(b) value = 1.0, BUMPENVMAPLUMINANCE == BUMPENVMAP, but if b is
609 * 0.0(without this fixup), the rendering breaks.
611 if(strlen(writemask
) >= 4) {
612 shader_addline(buffer
, "MOV %s.%c, %s;\n", reg
, writemask
[3], one
);
615 /* Correct the sign, but leave the blue as it is - it was loaded correctly already
616 * ARB shaders are a bit picky wrt writemasks and swizzles. If we're free to scale
617 * all registers, do so, this saves an instruction.
619 if(strlen(writemask
) >= 5) {
620 shader_addline(buffer
, "MAD %s, %s, %s, -%s;\n", reg
, reg
, two
, one
);
621 } else if(strlen(writemask
) >= 3) {
622 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n",
626 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n",
630 } else if(strlen(writemask
) == 2) {
631 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n", reg
, writemask
[1],
632 reg
, writemask
[1], two
, one
);
637 case WINED3DFMT_X8L8V8U8
:
638 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
639 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
640 * and a(X) is always 1.0. Cannot do a full conversion due to L(blue)
642 if(strlen(writemask
) >= 3) {
643 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n",
647 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n",
651 } else if(strlen(writemask
) == 2) {
652 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n",
660 case WINED3DFMT_L6V5U5
:
661 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
662 if(strlen(writemask
) >= 4) {
663 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
664 shader_addline(buffer
, "MOV TMP.g, %s.%c;\n",
666 shader_addline(buffer
, "MAD %s.%c%c, %s.%c%c%c%c, %s, -%s;\n",
667 reg
, writemask
[1], writemask
[2],
668 reg
, writemask
[3], writemask
[1], writemask
[3], writemask
[1],
670 shader_addline(buffer
, "MOV %s.%c, TMP.g;\n", reg
,
672 } else if(strlen(writemask
) == 3) {
673 /* This is bad: We have VL, but we need VU */
674 FIXME("2 components sampled from a converted L6V5U5 texture\n");
676 shader_addline(buffer
, "MAD %s.%c, %s.%c, %s, -%s;\n",
684 case WINED3DFMT_Q8W8V8U8
:
685 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
686 /* Correct the sign in all channels */
687 switch(strlen(writemask
)) {
689 shader_addline(buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
694 shader_addline(buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
699 shader_addline(buffer
, "MAD %s.%c, %s.%c, coefmul.x, -one;\n",
704 /* Should not occur, since it's at minimum '.' and a letter */
706 ERR("Unexpected writemask: \"%s\"\n", writemask
);
711 shader_addline(buffer
, "MAD %s, %s, coefmul.x, -one;\n", reg
, reg
);
716 case WINED3DFMT_ATI2N
:
717 /* GL_ATI_texture_compression_3dc returns the two channels as luminance-alpha,
718 * which means the first one is replicated across .rgb, and the 2nd one is in
719 * .a. We need the 2nd in .g
721 * GL_EXT_texture_compression_rgtc returns the values in .rg, however, they
722 * are swapped compared to d3d. So swap red and green.
724 if(GL_SUPPORT(EXT_TEXTURE_COMPRESSION_RGTC
)) {
725 shader_addline(buffer
, "SWZ %s, %s, %c, %c, 1, 0;\n",
726 reg
, reg
, writemask
[2], writemask
[1]);
728 if(strlen(writemask
) == 5) {
729 shader_addline(buffer
, "MOV %s.%c, %s.%c;\n",
730 reg
, writemask
[2], reg
, writemask
[4]);
731 } else if(strlen(writemask
) == 2) {
734 /* This is bad: We have VL, but we need VU */
735 FIXME("2 or 3 components sampled from a converted ATI2N texture\n");
740 /* stupid compiler */
746 static void shader_arb_color_correction(const SHADER_OPCODE_ARG
* arg
)
748 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
749 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
750 const WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
752 WINED3DFORMAT conversion_group
;
753 IWineD3DBaseTextureImpl
*texture
;
755 BOOL recorded
= FALSE
;
757 DWORD hex_version
= shader
->baseShader
.hex_version
;
761 switch(arg
->opcode
->opcode
) {
763 if (hex_version
< WINED3DPS_VERSION(2,0)) {
764 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
766 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
770 case WINED3DSIO_TEXLDL
:
771 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
774 case WINED3DSIO_TEXDP3TEX
:
775 case WINED3DSIO_TEXM3x3TEX
:
776 case WINED3DSIO_TEXM3x3SPEC
:
777 case WINED3DSIO_TEXM3x3VSPEC
:
778 case WINED3DSIO_TEXBEM
:
779 case WINED3DSIO_TEXREG2AR
:
780 case WINED3DSIO_TEXREG2GB
:
781 case WINED3DSIO_TEXREG2RGB
:
782 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
786 /* Not a texture sampling instruction, nothing to do */
790 texture
= (IWineD3DBaseTextureImpl
*) deviceImpl
->stateBlock
->textures
[sampler_idx
];
792 fmt
= texture
->resource
.format
;
793 conversion_group
= texture
->baseTexture
.shader_conversion_group
;
795 fmt
= WINED3DFMT_UNKNOWN
;
796 conversion_group
= WINED3DFMT_UNKNOWN
;
799 /* before doing anything, record the sampler with the format in the format conversion list,
800 * but check if it's not there already
802 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
803 if(shader
->baseShader
.sampled_samplers
[i
] == sampler_idx
) {
808 shader
->baseShader
.sampled_samplers
[shader
->baseShader
.num_sampled_samplers
] = sampler_idx
;
809 shader
->baseShader
.num_sampled_samplers
++;
810 shader
->baseShader
.sampled_format
[sampler_idx
] = conversion_group
;
813 pshader_get_register_name(arg
->shader
, arg
->dst
, reg
);
814 shader_arb_get_write_mask(arg
, arg
->dst
, writemask
);
815 if(strlen(writemask
) == 0) strcpy(writemask
, ".xyzw");
817 gen_color_correction(arg
->buffer
, reg
, writemask
, "one", "coefmul.x", fmt
, gl_info
);
822 static void pshader_gen_input_modifier_line (
823 IWineD3DBaseShader
*iface
,
824 SHADER_BUFFER
* buffer
,
829 /* Generate a line that does the input modifier computation and return the input register to use */
834 /* Assume a new line will be added */
837 /* Get register name */
838 pshader_get_register_name(iface
, instr
, regstr
);
839 shader_arb_get_swizzle(instr
, FALSE
, swzstr
);
841 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
842 case WINED3DSPSM_NONE
:
843 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
846 case WINED3DSPSM_NEG
:
847 sprintf(outregstr
, "-%s%s", regstr
, swzstr
);
850 case WINED3DSPSM_BIAS
:
851 shader_addline(buffer
, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg
, regstr
);
853 case WINED3DSPSM_BIASNEG
:
854 shader_addline(buffer
, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg
, regstr
);
856 case WINED3DSPSM_SIGN
:
857 shader_addline(buffer
, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg
, regstr
);
859 case WINED3DSPSM_SIGNNEG
:
860 shader_addline(buffer
, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg
, regstr
);
862 case WINED3DSPSM_COMP
:
863 shader_addline(buffer
, "SUB T%c, one.x, %s;\n", 'A' + tmpreg
, regstr
);
866 shader_addline(buffer
, "ADD T%c, %s, %s;\n", 'A' + tmpreg
, regstr
, regstr
);
868 case WINED3DSPSM_X2NEG
:
869 shader_addline(buffer
, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg
, regstr
, regstr
);
872 shader_addline(buffer
, "RCP T%c, %s.z;\n", 'A' + tmpreg
, regstr
);
873 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
876 shader_addline(buffer
, "RCP T%c, %s.w;\n", 'A' + tmpreg
, regstr
);
877 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
880 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
884 /* Return modified or original register, with swizzle */
886 sprintf(outregstr
, "T%c%s", 'A' + tmpreg
, swzstr
);
889 static inline void pshader_gen_output_modifier_line(SHADER_BUFFER
*buffer
, int saturate
, const char *write_mask
,
890 int shift
, const char *regstr
)
892 /* Generate a line that does the output modifier computation */
893 shader_addline(buffer
, "MUL%s %s%s, %s, %s;\n", saturate
? "_SAT" : "",
894 regstr
, write_mask
, regstr
, shift_tab
[shift
]);
897 static void pshader_hw_bem(const SHADER_OPCODE_ARG
*arg
)
899 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
901 SHADER_BUFFER
* buffer
= arg
->buffer
;
903 char src_name
[2][50];
905 DWORD sampler_code
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
906 BOOL has_bumpmat
= FALSE
;
909 for(i
= 0; i
< This
->numbumpenvmatconsts
; i
++) {
910 if(This
->bumpenvmatconst
[i
].const_num
!= -1 && This
->bumpenvmatconst
[i
].texunit
== sampler_code
) {
916 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
917 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
918 strcat(dst_name
, dst_wmask
);
920 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
921 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
924 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
925 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", sampler_code
);
926 shader_addline(buffer
, "DP3 TMP.r, TMP2, %s;\n", src_name
[1]);
927 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", sampler_code
);
928 shader_addline(buffer
, "DP3 TMP.g, TMP2, %s;\n", src_name
[1]);
930 shader_addline(buffer
, "ADD %s, %s, TMP;\n", dst_name
, src_name
[0]);
932 shader_addline(buffer
, "MOV %s, %s;\n", dst_name
, src_name
[0]);
936 static void pshader_hw_cnd(const SHADER_OPCODE_ARG
*arg
)
938 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
939 SHADER_BUFFER
* buffer
= arg
->buffer
;
942 char src_name
[3][50];
943 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
944 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
946 /* FIXME: support output modifiers */
948 /* Handle output register */
949 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
950 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
952 /* Generate input register names (with modifiers) */
953 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
954 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
955 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[2], 2, src_name
[2]);
957 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
958 if (shader
->baseShader
.hex_version
<= WINED3DPS_VERSION(1, 3) &&
959 arg
->opcode_token
& WINED3DSI_COISSUE
) {
960 shader_addline(buffer
, "MOV%s %s%s, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
, src_name
[1]);
962 shader_addline(buffer
, "ADD TMP, -%s, coefdiv.x;\n", src_name
[0]);
963 shader_addline(buffer
, "CMP%s %s%s, TMP, %s, %s;\n",
964 sat
? "_SAT" : "", dst_name
, dst_wmask
, src_name
[1], src_name
[2]);
967 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
970 static void pshader_hw_cmp(const SHADER_OPCODE_ARG
*arg
)
972 SHADER_BUFFER
* buffer
= arg
->buffer
;
975 char src_name
[3][50];
976 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
977 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
979 /* FIXME: support output modifiers */
981 /* Handle output register */
982 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
983 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
985 /* Generate input register names (with modifiers) */
986 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
987 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
988 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[2], 2, src_name
[2]);
990 shader_addline(buffer
, "CMP%s %s%s, %s, %s, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
,
991 src_name
[0], src_name
[2], src_name
[1]);
994 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
997 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
998 * dst = dot2(src0, src1) + src2 */
999 static void pshader_hw_dp2add(const SHADER_OPCODE_ARG
*arg
)
1001 SHADER_BUFFER
* buffer
= arg
->buffer
;
1004 char src_name
[3][50];
1005 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1006 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1008 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1009 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1011 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
[0]);
1012 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[1], 1, src_name
[1]);
1013 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[2], 2, src_name
[2]);
1015 /* Emulate a DP2 with a DP3 and 0.0 */
1016 shader_addline(buffer
, "MOV TMP, %s;\n", src_name
[0]);
1017 shader_addline(buffer
, "MOV TMP.z, 0.0;\n");
1018 shader_addline(buffer
, "DP3 TMP2, TMP, %s;\n", src_name
[1]);
1019 shader_addline(buffer
, "ADD%s %s%s, TMP2, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
, src_name
[2]);
1022 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1025 /* Map the opcode 1-to-1 to the GL code */
1026 static void shader_hw_map2gl(const SHADER_OPCODE_ARG
*arg
)
1028 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1029 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1030 SHADER_BUFFER
* buffer
= arg
->buffer
;
1031 DWORD dst
= arg
->dst
;
1032 const DWORD
*src
= arg
->src
;
1033 char arguments
[256];
1036 if (shader_is_pshader_version(shader
->baseShader
.hex_version
))
1038 /* Output token related */
1039 char output_rname
[256];
1040 char output_wmask
[20];
1041 char operands
[4][100];
1042 BOOL saturate
= FALSE
;
1043 BOOL centroid
= FALSE
;
1044 BOOL partialprecision
= FALSE
;
1045 const char *modifier
;
1048 if (!curOpcode
->num_params
)
1050 ERR("Opcode \"%s\" has no parameters\n", curOpcode
->name
);
1054 /* Process modifiers */
1055 if (dst
& WINED3DSP_DSTMOD_MASK
)
1057 DWORD mask
= dst
& WINED3DSP_DSTMOD_MASK
;
1059 saturate
= mask
& WINED3DSPDM_SATURATE
;
1060 centroid
= mask
& WINED3DSPDM_MSAMPCENTROID
;
1061 partialprecision
= mask
& WINED3DSPDM_PARTIALPRECISION
;
1062 mask
&= ~(WINED3DSPDM_MSAMPCENTROID
| WINED3DSPDM_PARTIALPRECISION
| WINED3DSPDM_SATURATE
);
1064 FIXME("Unrecognized modifier(%#x)\n", mask
>> WINED3DSP_DSTMOD_SHIFT
);
1067 FIXME("Unhandled modifier(%#x)\n", mask
>> WINED3DSP_DSTMOD_SHIFT
);
1069 shift
= (dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1070 modifier
= (saturate
&& !shift
) ? "_SAT" : "";
1072 /* Generate input register names (with modifiers) */
1073 for (i
= 1; i
< curOpcode
->num_params
; ++i
)
1074 pshader_gen_input_modifier_line(arg
->shader
, buffer
, src
[i
-1], i
-1, operands
[i
]);
1076 /* Handle output register */
1077 pshader_get_register_name(arg
->shader
, dst
, output_rname
);
1078 strcpy(operands
[0], output_rname
);
1079 shader_arb_get_write_mask(arg
, dst
, output_wmask
);
1080 strcat(operands
[0], output_wmask
);
1082 arguments
[0] = '\0';
1083 strcat(arguments
, operands
[0]);
1084 for (i
= 1; i
< curOpcode
->num_params
; i
++)
1086 strcat(arguments
, ", ");
1087 strcat(arguments
, operands
[i
]);
1089 shader_addline(buffer
, "%s%s %s;\n", curOpcode
->glname
, modifier
, arguments
);
1091 /* A shift requires another line. */
1092 if (shift
) pshader_gen_output_modifier_line(buffer
, saturate
, output_wmask
, shift
, output_rname
);
1094 /* Note that vshader_program_add_param() adds spaces. */
1096 arguments
[0] = '\0';
1097 if (curOpcode
->num_params
> 0)
1099 vshader_program_add_param(arg
, dst
, FALSE
, arguments
);
1100 for (i
= 1; i
< curOpcode
->num_params
; ++i
)
1102 strcat(arguments
, ",");
1103 vshader_program_add_param(arg
, src
[i
-1], TRUE
, arguments
);
1106 shader_addline(buffer
, "%s%s;\n", curOpcode
->glname
, arguments
);
1110 static void shader_hw_mov(const SHADER_OPCODE_ARG
*arg
)
1112 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1114 if ((WINED3DSHADER_VERSION_MAJOR(shader
->baseShader
.hex_version
) == 1
1115 && !shader_is_pshader_version(shader
->baseShader
.hex_version
)
1116 && shader_get_regtype(arg
->dst
) == WINED3DSPR_ADDR
)
1117 || arg
->opcode
->opcode
== WINED3DSIO_MOVA
)
1119 SHADER_BUFFER
*buffer
= arg
->buffer
;
1120 char src0_param
[256];
1122 if (arg
->opcode
->opcode
== WINED3DSIO_MOVA
)
1123 FIXME("mova should round\n");
1125 src0_param
[0] = '\0';
1126 if (((IWineD3DVertexShaderImpl
*)shader
)->rel_offset
)
1128 vshader_program_add_param(arg
, arg
->src
[0], TRUE
, src0_param
);
1129 shader_addline(buffer
, "ADD TMP.x, %s, helper_const.z;\n", src0_param
);
1130 shader_addline(buffer
, "ARL A0.x, TMP.x;\n");
1134 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1135 * with more than one component. Thus replicate the first source argument over all
1136 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc)
1138 DWORD parm
= arg
->src
[0] & ~(WINED3DVS_SWIZZLE_MASK
);
1139 if((arg
->src
[0] & WINED3DVS_X_W
) == WINED3DVS_X_W
)
1140 parm
|= WINED3DVS_X_W
| WINED3DVS_Y_W
| WINED3DVS_Z_W
| WINED3DVS_W_W
;
1141 else if((arg
->src
[0] & WINED3DVS_X_Z
) == WINED3DVS_X_Z
)
1142 parm
|= WINED3DVS_X_Z
| WINED3DVS_Y_Z
| WINED3DVS_Z_Z
| WINED3DVS_W_Z
;
1143 else if((arg
->src
[0] & WINED3DVS_X_Y
) == WINED3DVS_X_Y
)
1144 parm
|= WINED3DVS_X_Y
| WINED3DVS_Y_Y
| WINED3DVS_Z_Y
| WINED3DVS_W_Y
;
1145 else if((arg
->src
[0] & WINED3DVS_X_X
) == WINED3DVS_X_X
)
1146 parm
|= WINED3DVS_X_X
| WINED3DVS_Y_X
| WINED3DVS_Z_X
| WINED3DVS_W_X
;
1147 vshader_program_add_param(arg
, parm
, TRUE
, src0_param
);
1148 shader_addline(buffer
, "ARL A0.x, %s;\n", src0_param
);
1153 shader_hw_map2gl(arg
);
1157 static void pshader_hw_texkill(const SHADER_OPCODE_ARG
*arg
)
1159 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1160 DWORD hex_version
= This
->baseShader
.hex_version
;
1161 SHADER_BUFFER
* buffer
= arg
->buffer
;
1164 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1165 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1167 pshader_get_register_name(arg
->shader
, arg
->dst
, reg_dest
);
1169 if(hex_version
>= WINED3DPS_VERSION(2,0)) {
1170 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1171 shader_addline(buffer
, "KIL %s;\n", reg_dest
);
1173 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1174 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1176 shader_addline(buffer
, "MOV TMP, %s;\n", reg_dest
);
1177 shader_addline(buffer
, "MOV TMP.w, one.w;\n");
1178 shader_addline(buffer
, "KIL TMP;\n");
1182 static void pshader_hw_tex(const SHADER_OPCODE_ARG
*arg
)
1184 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1185 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1187 DWORD dst
= arg
->dst
;
1188 const DWORD
*src
= arg
->src
;
1189 SHADER_BUFFER
* buffer
= arg
->buffer
;
1190 DWORD hex_version
= This
->baseShader
.hex_version
;
1191 BOOL projected
= FALSE
, bias
= FALSE
;
1195 DWORD reg_dest_code
;
1196 DWORD reg_sampler_code
;
1198 /* All versions have a destination register */
1199 reg_dest_code
= dst
& WINED3DSP_REGNUM_MASK
;
1200 pshader_get_register_name(arg
->shader
, dst
, reg_dest
);
1202 /* 1.0-1.3: Use destination register as coordinate source.
1203 1.4+: Use provided coordinate source register. */
1204 if (hex_version
< WINED3DPS_VERSION(1,4))
1205 strcpy(reg_coord
, reg_dest
);
1207 pshader_gen_input_modifier_line(arg
->shader
, buffer
, src
[0], 0, reg_coord
);
1209 /* 1.0-1.4: Use destination register number as texture code.
1210 2.0+: Use provided sampler number as texure code. */
1211 if (hex_version
< WINED3DPS_VERSION(2,0))
1212 reg_sampler_code
= reg_dest_code
;
1214 reg_sampler_code
= src
[1] & WINED3DSP_REGNUM_MASK
;
1217 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1218 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1219 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1221 if(hex_version
< WINED3DPS_VERSION(1,4)) {
1223 if(reg_sampler_code
< MAX_TEXTURES
) {
1224 flags
= deviceImpl
->stateBlock
->textureState
[reg_sampler_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1226 if (flags
& WINED3DTTFF_PROJECTED
) {
1229 } else if(hex_version
< WINED3DPS_VERSION(2,0)) {
1230 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
1231 if (src_mod
== WINED3DSPSM_DZ
) {
1233 } else if(src_mod
== WINED3DSPSM_DW
) {
1237 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
1240 if(arg
->opcode_token
& WINED3DSI_TEXLD_BIAS
) {
1244 shader_hw_sample(arg
, reg_sampler_code
, reg_dest
, reg_coord
, projected
, bias
);
1247 static void pshader_hw_texcoord(const SHADER_OPCODE_ARG
*arg
)
1249 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1250 DWORD dst
= arg
->dst
;
1251 SHADER_BUFFER
* buffer
= arg
->buffer
;
1252 DWORD hex_version
= This
->baseShader
.hex_version
;
1255 shader_arb_get_write_mask(arg
, dst
, tmp
);
1256 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
1257 DWORD reg
= dst
& WINED3DSP_REGNUM_MASK
;
1258 shader_addline(buffer
, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg
, tmp
, reg
);
1260 DWORD reg1
= dst
& WINED3DSP_REGNUM_MASK
;
1263 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, reg_src
);
1264 shader_addline(buffer
, "MOV R%u%s, %s;\n", reg1
, tmp
, reg_src
);
1268 static void pshader_hw_texreg2ar(const SHADER_OPCODE_ARG
*arg
)
1270 SHADER_BUFFER
* buffer
= arg
->buffer
;
1271 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1272 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1275 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1279 sprintf(dst_str
, "T%u", reg1
);
1280 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_str
);
1281 shader_addline(buffer
, "MOV TMP.r, %s.a;\n", src_str
);
1282 shader_addline(buffer
, "MOV TMP.g, %s.r;\n", src_str
);
1283 flags
= reg1
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg1
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1284 shader_hw_sample(arg
, reg1
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1287 static void pshader_hw_texreg2gb(const SHADER_OPCODE_ARG
*arg
)
1289 SHADER_BUFFER
* buffer
= arg
->buffer
;
1291 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1295 sprintf(dst_str
, "T%u", reg1
);
1296 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_str
);
1297 shader_addline(buffer
, "MOV TMP.r, %s.g;\n", src_str
);
1298 shader_addline(buffer
, "MOV TMP.g, %s.b;\n", src_str
);
1299 shader_hw_sample(arg
, reg1
, dst_str
, "TMP", FALSE
, FALSE
);
1302 static void pshader_hw_texreg2rgb(const SHADER_OPCODE_ARG
*arg
)
1304 SHADER_BUFFER
* buffer
= arg
->buffer
;
1305 DWORD reg1
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1309 sprintf(dst_str
, "T%u", reg1
);
1310 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_str
);
1311 shader_hw_sample(arg
, reg1
, dst_str
, src_str
, FALSE
, FALSE
);
1314 static void pshader_hw_texbem(const SHADER_OPCODE_ARG
*arg
)
1316 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1317 BOOL has_bumpmat
= FALSE
;
1318 BOOL has_luminance
= FALSE
;
1321 DWORD dst
= arg
->dst
;
1322 DWORD src
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1323 SHADER_BUFFER
* buffer
= arg
->buffer
;
1326 DWORD reg_dest_code
;
1328 /* All versions have a destination register */
1329 reg_dest_code
= dst
& WINED3DSP_REGNUM_MASK
;
1330 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
1331 pshader_get_register_name(arg
->shader
, dst
, reg_coord
);
1333 for(i
= 0; i
< This
->numbumpenvmatconsts
; i
++) {
1334 if(This
->bumpenvmatconst
[i
].const_num
!= -1 && reg_dest_code
== This
->bumpenvmatconst
[i
].texunit
) {
1339 for(i
= 0; i
< This
->numbumpenvmatconsts
; i
++) {
1340 if(This
->luminanceconst
[i
].const_num
!= -1 && reg_dest_code
== This
->luminanceconst
[i
].texunit
) {
1341 has_luminance
= TRUE
;
1347 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
1349 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code
);
1350 shader_addline(buffer
, "DP3 TMP.r, TMP2, T%u;\n", src
);
1351 shader_addline(buffer
, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code
);
1352 shader_addline(buffer
, "DP3 TMP.g, TMP2, T%u;\n", src
);
1354 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1355 * so we can't let the GL handle this.
1357 if (((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->stateBlock
->textureState
[reg_dest_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
]
1358 & WINED3DTTFF_PROJECTED
) {
1359 shader_addline(buffer
, "RCP TMP2.a, %s.a;\n", reg_coord
);
1360 shader_addline(buffer
, "MUL TMP2.rg, %s, TMP2.a;\n", reg_coord
);
1361 shader_addline(buffer
, "ADD TMP.rg, TMP, TMP2;\n");
1363 shader_addline(buffer
, "ADD TMP.rg, TMP, %s;\n", reg_coord
);
1366 shader_hw_sample(arg
, reg_dest_code
, reg_coord
, "TMP", FALSE
, FALSE
);
1368 if(arg
->opcode
->opcode
== WINED3DSIO_TEXBEML
&& has_luminance
) {
1369 shader_addline(buffer
, "MAD TMP, T%u.z, luminance%d.x, luminance%d.y;\n",
1370 src
, reg_dest_code
, reg_dest_code
);
1371 shader_addline(buffer
, "MUL %s, %s, TMP;\n", reg_coord
, reg_coord
);
1376 if(reg_dest_code
< MAX_TEXTURES
) {
1377 tf
= ((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->stateBlock
->textureState
[reg_dest_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1381 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
1382 shader_hw_sample(arg
, reg_dest_code
, reg_coord
, reg_coord
, tf
& WINED3DTTFF_PROJECTED
, FALSE
);
1386 static void pshader_hw_texm3x2pad(const SHADER_OPCODE_ARG
*arg
)
1388 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1389 SHADER_BUFFER
* buffer
= arg
->buffer
;
1392 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1393 shader_addline(buffer
, "DP3 TMP.x, T%u, %s;\n", reg
, src0_name
);
1396 static void pshader_hw_texm3x2tex(const SHADER_OPCODE_ARG
*arg
)
1398 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1399 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1401 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1402 SHADER_BUFFER
* buffer
= arg
->buffer
;
1406 sprintf(dst_str
, "T%u", reg
);
1407 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1408 shader_addline(buffer
, "DP3 TMP.y, T%u, %s;\n", reg
, src0_name
);
1409 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1410 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1413 static void pshader_hw_texm3x3pad(const SHADER_OPCODE_ARG
*arg
)
1415 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1416 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1417 SHADER_BUFFER
* buffer
= arg
->buffer
;
1418 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.%c, T%u, %s;\n", 'x' + current_state
->current_row
, reg
, src0_name
);
1423 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
1426 static void pshader_hw_texm3x3tex(const SHADER_OPCODE_ARG
*arg
)
1428 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1429 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1431 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1432 SHADER_BUFFER
* buffer
= arg
->buffer
;
1433 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1437 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1438 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
1440 /* Sample the texture using the calculated coordinates */
1441 sprintf(dst_str
, "T%u", reg
);
1442 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1443 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1444 current_state
->current_row
= 0;
1447 static void pshader_hw_texm3x3vspec(const SHADER_OPCODE_ARG
*arg
)
1449 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1450 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1452 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1453 SHADER_BUFFER
* buffer
= arg
->buffer
;
1454 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
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 /* Construct the eye-ray vector from w coordinates */
1462 shader_addline(buffer
, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[0]);
1463 shader_addline(buffer
, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[1]);
1464 shader_addline(buffer
, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg
);
1466 /* Calculate reflection vector
1468 shader_addline(buffer
, "DP3 TMP.w, TMP, TMP2;\n");
1469 /* The .w is ignored when sampling, so I can use TMP2.w to calculate dot(N, N) */
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, -TMP2;\n");
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 static void pshader_hw_texm3x3spec(const SHADER_OPCODE_ARG
*arg
)
1485 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1486 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1488 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1489 DWORD reg3
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1490 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1491 SHADER_BUFFER
* buffer
= arg
->buffer
;
1495 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0_name
);
1496 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", reg
, src0_name
);
1498 /* Calculate reflection vector.
1501 * TMP.xyz = 2 * --------- * N - E
1504 * Which normalizes the normal vector
1506 shader_addline(buffer
, "DP3 TMP.w, TMP, C[%u];\n", reg3
);
1507 shader_addline(buffer
, "DP3 TMP2.w, TMP, TMP;\n");
1508 shader_addline(buffer
, "RCP TMP2.w, TMP2.w;\n");
1509 shader_addline(buffer
, "MUL TMP.w, TMP.w, TMP2.w;\n");
1510 shader_addline(buffer
, "MUL TMP, TMP.w, TMP;\n");
1511 shader_addline(buffer
, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3
);
1513 /* Sample the texture using the calculated coordinates */
1514 sprintf(dst_str
, "T%u", reg
);
1515 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1516 shader_hw_sample(arg
, reg
, dst_str
, "TMP", flags
& WINED3DTTFF_PROJECTED
, FALSE
);
1517 current_state
->current_row
= 0;
1520 static void pshader_hw_texdepth(const SHADER_OPCODE_ARG
*arg
)
1522 SHADER_BUFFER
* buffer
= arg
->buffer
;
1525 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1526 * which is essentially an input, is the destination register because it is the first
1527 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1530 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1532 /* According to the msdn, the source register(must be r5) is unusable after
1533 * the texdepth instruction, so we're free to modify it
1535 shader_addline(buffer
, "MIN %s.g, %s.g, one.g;\n", dst_name
, dst_name
);
1537 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1538 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1539 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1541 shader_addline(buffer
, "RCP %s.g, %s.g;\n", dst_name
, dst_name
);
1542 shader_addline(buffer
, "MUL TMP.x, %s.r, %s.g;\n", dst_name
, dst_name
);
1543 shader_addline(buffer
, "MIN TMP.x, TMP.x, one.r;\n");
1544 shader_addline(buffer
, "MAX result.depth, TMP.x, 0.0;\n");
1547 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1548 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1549 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1550 static void pshader_hw_texdp3tex(const SHADER_OPCODE_ARG
*arg
)
1552 SHADER_BUFFER
* buffer
= arg
->buffer
;
1553 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1557 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1558 shader_addline(buffer
, "MOV TMP, 0.0;\n");
1559 shader_addline(buffer
, "DP3 TMP.x, T%u, %s;\n", sampler_idx
, src0
);
1561 sprintf(dst_str
, "T%u", sampler_idx
);
1562 shader_hw_sample(arg
, sampler_idx
, dst_str
, "TMP", FALSE
/* Only one coord, can't be projected */, FALSE
);
1565 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1566 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1567 static void pshader_hw_texdp3(const SHADER_OPCODE_ARG
*arg
)
1572 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1573 SHADER_BUFFER
* buffer
= arg
->buffer
;
1575 /* Handle output register */
1576 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_str
);
1577 shader_arb_get_write_mask(arg
, arg
->dst
, dst_mask
);
1579 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1580 shader_addline(buffer
, "DP3 %s%s, T%u, %s;\n", dst_str
, dst_mask
, dstreg
, src0
);
1582 /* TODO: Handle output modifiers */
1585 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1586 * Perform the 3rd row of a 3x3 matrix multiply */
1587 static void pshader_hw_texm3x3(const SHADER_OPCODE_ARG
*arg
)
1589 SHADER_BUFFER
* buffer
= arg
->buffer
;
1593 DWORD dst_reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1595 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_str
);
1596 shader_arb_get_write_mask(arg
, arg
->dst
, dst_mask
);
1598 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1599 shader_addline(buffer
, "DP3 TMP.z, T%u, %s;\n", dst_reg
, src0
);
1600 shader_addline(buffer
, "MOV %s%s, TMP;\n", dst_str
, dst_mask
);
1602 /* TODO: Handle output modifiers */
1605 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1606 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1607 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1608 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1610 static void pshader_hw_texm3x2depth(const SHADER_OPCODE_ARG
*arg
)
1612 SHADER_BUFFER
* buffer
= arg
->buffer
;
1613 DWORD dst_reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1616 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src0
);
1617 shader_addline(buffer
, "DP3 TMP.y, T%u, %s;\n", dst_reg
, src0
);
1619 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1620 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1621 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1623 shader_addline(buffer
, "RCP TMP.y, TMP.y;\n");
1624 shader_addline(buffer
, "MUL TMP.x, TMP.x, TMP.y;\n");
1625 shader_addline(buffer
, "MIN TMP.x, TMP.x, one.r;\n");
1626 shader_addline(buffer
, "MAX result.depth, TMP.x, 0.0;\n");
1629 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1630 Vertex/Pixel shaders to ARB_vertex_program codes */
1631 static void shader_hw_mnxn(const SHADER_OPCODE_ARG
*arg
)
1634 int nComponents
= 0;
1635 SHADER_OPCODE_ARG tmpArg
;
1637 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1639 /* Set constants for the temporary argument */
1640 tmpArg
.shader
= arg
->shader
;
1641 tmpArg
.buffer
= arg
->buffer
;
1642 tmpArg
.src
[0] = arg
->src
[0];
1643 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1644 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1645 tmpArg
.reg_maps
= arg
->reg_maps
;
1647 switch(arg
->opcode
->opcode
) {
1648 case WINED3DSIO_M4x4
:
1650 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1652 case WINED3DSIO_M4x3
:
1654 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1656 case WINED3DSIO_M3x4
:
1658 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1660 case WINED3DSIO_M3x3
:
1662 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1664 case WINED3DSIO_M3x2
:
1666 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1672 for (i
= 0; i
< nComponents
; i
++) {
1673 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1674 tmpArg
.src
[1] = arg
->src
[1]+i
;
1675 shader_hw_map2gl(&tmpArg
);
1679 static void vshader_hw_rsq_rcp(const SHADER_OPCODE_ARG
*arg
)
1681 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1682 SHADER_BUFFER
* buffer
= arg
->buffer
;
1683 DWORD dst
= arg
->dst
;
1684 DWORD src
= arg
->src
[0];
1685 DWORD swizzle
= (src
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
1689 strcpy(tmpLine
, curOpcode
->glname
); /* Opcode */
1690 vshader_program_add_param(arg
, dst
, FALSE
, tmpLine
); /* Destination */
1691 strcat(tmpLine
, ",");
1692 vshader_program_add_param(arg
, src
, TRUE
, tmpLine
);
1693 if ((WINED3DSP_NOSWIZZLE
>> WINED3DSP_SWIZZLE_SHIFT
) == swizzle
) {
1694 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1697 strcat(tmpLine
, ".w");
1700 shader_addline(buffer
, "%s;\n", tmpLine
);
1703 static void shader_hw_nrm(const SHADER_OPCODE_ARG
*arg
)
1705 SHADER_BUFFER
* buffer
= arg
->buffer
;
1709 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1710 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1712 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1713 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1715 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
);
1716 shader_addline(buffer
, "DP3 TMP, %s, %s;\n", src_name
, src_name
);
1717 shader_addline(buffer
, "RSQ TMP, TMP.x;\n");
1718 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1719 shader_addline(buffer
, "MUL%s %s%s, %s, TMP;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
,
1723 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1726 static void shader_hw_sincos(const SHADER_OPCODE_ARG
*arg
)
1728 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1729 * must contain fixed constants. So we need a separate function to filter those constants and
1732 SHADER_BUFFER
* buffer
= arg
->buffer
;
1736 DWORD shift
= (arg
->dst
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1737 BOOL sat
= (arg
->dst
& WINED3DSP_DSTMOD_MASK
) & WINED3DSPDM_SATURATE
;
1739 pshader_get_register_name(arg
->shader
, arg
->dst
, dst_name
);
1740 shader_arb_get_write_mask(arg
, arg
->dst
, dst_wmask
);
1742 pshader_gen_input_modifier_line(arg
->shader
, buffer
, arg
->src
[0], 0, src_name
);
1743 shader_addline(buffer
, "SCS%s %s%s, %s;\n", sat
? "_SAT" : "", dst_name
, dst_wmask
,
1747 pshader_gen_output_modifier_line(buffer
, FALSE
, dst_wmask
, shift
, dst_name
);
1751 static GLuint
create_arb_blt_vertex_program(const WineD3D_GL_Info
*gl_info
)
1753 GLuint program_id
= 0;
1754 const char *blt_vprogram
=
1756 "PARAM c[1] = { { 1, 0.5 } };\n"
1757 "MOV result.position, vertex.position;\n"
1758 "MOV result.color, c[0].x;\n"
1759 "MOV result.texcoord[0], vertex.texcoord[0];\n"
1762 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
1763 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, program_id
));
1764 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_vprogram
), blt_vprogram
));
1766 if (glGetError() == GL_INVALID_OPERATION
) {
1768 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
1769 FIXME("Vertex program error at position %d: %s\n", pos
,
1770 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
1776 static GLuint
create_arb_blt_fragment_program(const WineD3D_GL_Info
*gl_info
, enum tex_types tex_type
)
1778 GLuint program_id
= 0;
1779 const char *blt_fprograms
[tex_type_count
] =
1786 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1787 "MOV result.depth.z, R0.x;\n"
1794 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
1795 "MOV result.depth.z, R0.x;\n"
1800 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
1801 "MOV result.depth.z, R0.x;\n"
1805 if (!blt_fprograms
[tex_type
])
1807 FIXME("tex_type %#x not supported\n", tex_type
);
1811 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
1812 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, program_id
));
1813 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_fprograms
[tex_type
]), blt_fprograms
[tex_type
]));
1815 if (glGetError() == GL_INVALID_OPERATION
) {
1817 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
1818 FIXME("Fragment program error at position %d: %s\n", pos
,
1819 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
1825 static void shader_arb_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
1826 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1827 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1828 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1831 TRACE("Using vertex shader\n");
1832 IWineD3DVertexShaderImpl_CompileShader(This
->stateBlock
->vertexShader
);
1834 priv
->current_vprogram_id
= ((IWineD3DVertexShaderImpl
*)This
->stateBlock
->vertexShader
)->prgId
;
1836 /* Bind the vertex program */
1837 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
1838 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1840 /* Enable OpenGL vertex programs */
1841 glEnable(GL_VERTEX_PROGRAM_ARB
);
1842 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1843 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
1844 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM
)) {
1845 priv
->current_vprogram_id
= 0;
1846 glDisable(GL_VERTEX_PROGRAM_ARB
);
1847 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1851 struct ps_compile_args compile_args
;
1852 TRACE("Using pixel shader\n");
1853 find_ps_compile_args((IWineD3DPixelShaderImpl
*) This
->stateBlock
->pixelShader
, This
->stateBlock
, &compile_args
);
1854 priv
->current_fprogram_id
= find_gl_pshader((IWineD3DPixelShaderImpl
*) This
->stateBlock
->pixelShader
,
1857 /* Bind the fragment program */
1858 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
1859 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1861 if(!priv
->use_arbfp_fixed_func
) {
1862 /* Enable OpenGL fragment programs */
1863 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1864 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1866 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
1867 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
) && !priv
->use_arbfp_fixed_func
) {
1868 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
1869 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
1870 * replacement shader
1872 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1873 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1874 priv
->current_fprogram_id
= 0;
1878 static void shader_arb_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {
1879 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1880 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1881 GLuint
*blt_fprogram
= &priv
->depth_blt_fprogram_id
[tex_type
];
1882 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1884 if (!priv
->depth_blt_vprogram_id
) priv
->depth_blt_vprogram_id
= create_arb_blt_vertex_program(gl_info
);
1885 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->depth_blt_vprogram_id
));
1886 glEnable(GL_VERTEX_PROGRAM_ARB
);
1888 if (!*blt_fprogram
) *blt_fprogram
= create_arb_blt_fragment_program(gl_info
, tex_type
);
1889 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, *blt_fprogram
));
1890 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1893 static void shader_arb_deselect_depth_blt(IWineD3DDevice
*iface
) {
1894 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1895 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1896 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1898 if (priv
->current_vprogram_id
) {
1899 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
1900 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1902 glEnable(GL_VERTEX_PROGRAM_ARB
);
1903 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1905 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
1907 glDisable(GL_VERTEX_PROGRAM_ARB
);
1908 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1911 if (priv
->current_fprogram_id
) {
1912 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
1913 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1915 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
1916 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1918 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
1920 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1921 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1925 static void shader_arb_cleanup(IWineD3DDevice
*iface
) {
1926 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1927 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1928 if (GL_SUPPORT(ARB_VERTEX_PROGRAM
)) glDisable(GL_VERTEX_PROGRAM_ARB
);
1929 if (GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) glDisable(GL_FRAGMENT_PROGRAM_ARB
);
1932 static void shader_arb_destroy(IWineD3DBaseShader
*iface
) {
1933 IWineD3DBaseShaderImpl
*baseShader
= (IWineD3DBaseShaderImpl
*) iface
;
1934 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)baseShader
->baseShader
.device
)->adapter
->gl_info
;
1935 char pshader
= shader_is_pshader_version(baseShader
->baseShader
.hex_version
);
1938 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*) iface
;
1942 for(i
= 0; i
< This
->num_gl_shaders
; i
++) {
1943 GL_EXTCALL(glDeleteProgramsARB(1, &This
->gl_shaders
[i
].prgId
));
1944 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1947 HeapFree(GetProcessHeap(), 0, This
->gl_shaders
);
1948 This
->gl_shaders
= NULL
;
1949 This
->num_gl_shaders
= 0;
1951 IWineD3DVertexShaderImpl
*This
= (IWineD3DVertexShaderImpl
*) iface
;
1954 GL_EXTCALL(glDeleteProgramsARB(1, &This
->prgId
));
1955 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->prgId))");
1956 ((IWineD3DVertexShaderImpl
*) This
)->prgId
= 0;
1959 baseShader
->baseShader
.is_compiled
= FALSE
;
1962 static HRESULT
shader_arb_alloc(IWineD3DDevice
*iface
) {
1963 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1964 This
->shader_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_arb_priv
));
1968 static void shader_arb_free(IWineD3DDevice
*iface
) {
1969 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1970 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
1971 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->shader_priv
;
1974 if(priv
->depth_blt_vprogram_id
) {
1975 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_vprogram_id
));
1977 for (i
= 0; i
< tex_type_count
; ++i
) {
1978 if (priv
->depth_blt_fprogram_id
[i
]) {
1979 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id
[i
]));
1983 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
1986 static BOOL
shader_arb_dirty_const(IWineD3DDevice
*iface
) {
1990 static void arbfp_add_sRGB_correction(SHADER_BUFFER
*buffer
, const char *fragcolor
, const char *tmp1
,
1991 const char *tmp2
, const char *tmp3
, const char *tmp4
) {
1992 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
1994 /* Calculate the > 0.0031308 case */
1995 shader_addline(buffer
, "POW %s.x, %s.x, srgb_pow.x;\n", tmp1
, fragcolor
);
1996 shader_addline(buffer
, "POW %s.y, %s.y, srgb_pow.y;\n", tmp1
, fragcolor
);
1997 shader_addline(buffer
, "POW %s.z, %s.z, srgb_pow.z;\n", tmp1
, fragcolor
);
1998 shader_addline(buffer
, "MUL %s, %s, srgb_mul_hi;\n", tmp1
, tmp1
);
1999 shader_addline(buffer
, "SUB %s, %s, srgb_sub_hi;\n", tmp1
, tmp1
);
2000 /* Calculate the < case */
2001 shader_addline(buffer
, "MUL %s, srgb_mul_low, %s;\n", tmp2
, fragcolor
);
2002 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
2003 shader_addline(buffer
, "SLT %s, srgb_comparison, %s;\n", tmp3
, fragcolor
);
2004 shader_addline(buffer
, "SGE %s, srgb_comparison, %s;\n", tmp4
, fragcolor
);
2005 /* Store the components > 0.0031308 in the destination */
2006 shader_addline(buffer
, "MUL %s, %s, %s;\n", fragcolor
, tmp1
, tmp3
);
2007 /* Add the components that are < 0.0031308 */
2008 shader_addline(buffer
, "MAD result.color.xyz, %s, %s, %s;\n", tmp2
, tmp4
, fragcolor
);
2009 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
2012 static GLuint
shader_arb_generate_pshader(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
) {
2013 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
2014 const shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
2015 CONST DWORD
*function
= This
->baseShader
.function
;
2016 const char *fragcolor
;
2017 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
2018 const local_constant
*lconst
;
2021 /* Create the hw ARB shader */
2022 shader_addline(buffer
, "!!ARBfp1.0\n");
2024 shader_addline(buffer
, "TEMP TMP;\n"); /* Used in matrix ops */
2025 shader_addline(buffer
, "TEMP TMP2;\n"); /* Used in matrix ops */
2026 shader_addline(buffer
, "TEMP TA;\n"); /* Used for modifiers */
2027 shader_addline(buffer
, "TEMP TB;\n"); /* Used for modifiers */
2028 shader_addline(buffer
, "TEMP TC;\n"); /* Used for modifiers */
2029 shader_addline(buffer
, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
2030 shader_addline(buffer
, "PARAM coefmul = { 2, 4, 8, 16 };\n");
2031 shader_addline(buffer
, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
2033 /* Base Declarations */
2034 shader_generate_arb_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
);
2036 /* We need two variables for fog blending */
2037 shader_addline(buffer
, "TEMP TMP_FOG;\n");
2038 if (This
->baseShader
.hex_version
>= WINED3DPS_VERSION(2,0)) {
2039 shader_addline(buffer
, "TEMP TMP_COLOR;\n");
2042 /* Base Shader Body */
2043 shader_generate_main( (IWineD3DBaseShader
*) This
, buffer
, reg_maps
, function
);
2045 /* calculate fog and blend it
2046 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
2047 * -1/(e-s) and e/(e-s) respectively.
2049 shader_addline(buffer
, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
2051 if (This
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
2054 fragcolor
= "TMP_COLOR";
2056 if(((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
2057 arbfp_add_sRGB_correction(buffer
, fragcolor
, "TMP", "TMP2", "TA", "TB");
2059 if (This
->baseShader
.hex_version
< WINED3DPS_VERSION(3,0)) {
2060 shader_addline(buffer
, "LRP result.color.rgb, TMP_FOG.x, %s, state.fog.color;\n", fragcolor
);
2061 shader_addline(buffer
, "MOV result.color.a, %s.a;\n", fragcolor
);
2064 shader_addline(buffer
, "END\n");
2066 /* TODO: change to resource.glObjectHandle or something like that */
2067 GL_EXTCALL(glGenProgramsARB(1, &retval
));
2069 TRACE("Creating a hw pixel shader, prg=%d\n", retval
);
2070 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, retval
));
2072 TRACE("Created hw pixel shader, prg=%d\n", retval
);
2073 /* Create the program and check for errors */
2074 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2075 buffer
->bsize
, buffer
->buffer
));
2077 if (glGetError() == GL_INVALID_OPERATION
) {
2079 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
2080 FIXME("HW PixelShader Error at position %d: %s\n",
2081 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
2085 /* Load immediate constants */
2086 if(!This
->baseShader
.load_local_constsF
) {
2087 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
2088 const float *value
= (const float *)lconst
->value
;
2089 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, lconst
->idx
, value
));
2090 checkGLcall("glProgramLocalParameter4fvARB");
2097 static void shader_arb_generate_vshader(IWineD3DVertexShader
*iface
, SHADER_BUFFER
*buffer
) {
2098 IWineD3DVertexShaderImpl
*This
= (IWineD3DVertexShaderImpl
*)iface
;
2099 const shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
2100 CONST DWORD
*function
= This
->baseShader
.function
;
2101 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
2102 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
2103 const local_constant
*lconst
;
2105 /* Create the hw ARB shader */
2106 shader_addline(buffer
, "!!ARBvp1.0\n");
2107 shader_addline(buffer
, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This
->rel_offset
);
2109 /* Mesa supports only 95 constants */
2110 if (GL_VEND(MESA
) || GL_VEND(WINE
))
2111 This
->baseShader
.limits
.constant_float
=
2112 min(95, This
->baseShader
.limits
.constant_float
);
2114 shader_addline(buffer
, "TEMP TMP;\n");
2116 /* Base Declarations */
2117 shader_generate_arb_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
);
2119 /* We need a constant to fixup the final position */
2120 shader_addline(buffer
, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS
);
2122 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
2123 * for output parameters. D3D in theory does not do that either, but some applications depend on a
2124 * proper initialization of the secondary color, and programs using the fixed function pipeline without
2125 * a replacement shader depend on the texcoord.w being set properly.
2127 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
2128 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
2129 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
2130 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
2131 * this can eat a number of instructions, so skip it unless this cap is set as well
2133 if(!GL_SUPPORT(NV_VERTEX_PROGRAM
)) {
2134 shader_addline(buffer
, "MOV result.color.secondary, -helper_const.wwwy;\n");
2136 if((GLINFO_LOCATION
).set_texcoord_w
&& !device
->frag_pipe
->ffp_proj_control
) {
2138 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
2139 if(This
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
2140 This
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
2141 shader_addline(buffer
, "MOV result.texcoord[%u].w, -helper_const.y;\n", i
);
2147 /* Base Shader Body */
2148 shader_generate_main( (IWineD3DBaseShader
*) This
, buffer
, reg_maps
, function
);
2150 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
2152 shader_addline(buffer
, "MOV result.fogcoord, TMP_OUT.z;\n");
2154 /* Write the final position.
2156 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2157 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2158 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2159 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2161 shader_addline(buffer
, "MUL TMP, posFixup, TMP_OUT.w;\n");
2162 shader_addline(buffer
, "ADD TMP_OUT.x, TMP_OUT.x, TMP.z;\n");
2163 shader_addline(buffer
, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TMP.w;\n");
2165 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2166 * and the glsl equivalent
2168 shader_addline(buffer
, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2170 shader_addline(buffer
, "MOV result.position, TMP_OUT;\n");
2172 shader_addline(buffer
, "END\n");
2174 /* TODO: change to resource.glObjectHandle or something like that */
2175 GL_EXTCALL(glGenProgramsARB(1, &This
->prgId
));
2177 TRACE("Creating a hw vertex shader, prg=%d\n", This
->prgId
);
2178 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, This
->prgId
));
2180 TRACE("Created hw vertex shader, prg=%d\n", This
->prgId
);
2181 /* Create the program and check for errors */
2182 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2183 buffer
->bsize
, buffer
->buffer
));
2185 if (glGetError() == GL_INVALID_OPERATION
) {
2187 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
2188 FIXME("HW VertexShader Error at position %d: %s\n",
2189 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
2193 /* Load immediate constants */
2194 if(!This
->baseShader
.load_local_constsF
) {
2195 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
2196 const float *value
= (const float *)lconst
->value
;
2197 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, lconst
->idx
, value
));
2202 static void shader_arb_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
)
2204 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2205 * then overwrite the shader specific ones
2207 none_shader_backend
.shader_get_caps(devtype
, gl_info
, pCaps
);
2209 if(GL_SUPPORT(ARB_VERTEX_PROGRAM
)) {
2210 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(1,1);
2211 TRACE_(d3d_caps
)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2212 pCaps
->MaxVertexShaderConst
= GL_LIMITS(vshader_constantsF
);
2215 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) {
2216 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(1,4);
2217 pCaps
->PixelShader1xMaxValue
= 8.0;
2218 TRACE_(d3d_caps
)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2222 static BOOL
shader_arb_conv_supported(WINED3DFORMAT fmt
) {
2223 TRACE("Checking shader format support for format %s:", debug_d3dformat(fmt
));
2225 case WINED3DFMT_V8U8
:
2226 case WINED3DFMT_V16U16
:
2227 case WINED3DFMT_X8L8V8U8
:
2228 case WINED3DFMT_L6V5U5
:
2229 case WINED3DFMT_Q8W8V8U8
:
2230 case WINED3DFMT_ATI2N
:
2239 static const SHADER_HANDLER shader_arb_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
2241 /* WINED3DSIH_ABS */ shader_hw_map2gl
,
2242 /* WINED3DSIH_ADD */ shader_hw_map2gl
,
2243 /* WINED3DSIH_BEM */ pshader_hw_bem
,
2244 /* WINED3DSIH_BREAK */ NULL
,
2245 /* WINED3DSIH_BREAKC */ NULL
,
2246 /* WINED3DSIH_BREAKP */ NULL
,
2247 /* WINED3DSIH_CALL */ NULL
,
2248 /* WINED3DSIH_CALLNZ */ NULL
,
2249 /* WINED3DSIH_CMP */ pshader_hw_cmp
,
2250 /* WINED3DSIH_CND */ pshader_hw_cnd
,
2251 /* WINED3DSIH_CRS */ shader_hw_map2gl
,
2252 /* WINED3DSIH_DCL */ NULL
,
2253 /* WINED3DSIH_DEF */ NULL
,
2254 /* WINED3DSIH_DEFB */ NULL
,
2255 /* WINED3DSIH_DEFI */ NULL
,
2256 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add
,
2257 /* WINED3DSIH_DP3 */ shader_hw_map2gl
,
2258 /* WINED3DSIH_DP4 */ shader_hw_map2gl
,
2259 /* WINED3DSIH_DST */ shader_hw_map2gl
,
2260 /* WINED3DSIH_DSX */ NULL
,
2261 /* WINED3DSIH_DSY */ NULL
,
2262 /* WINED3DSIH_ELSE */ NULL
,
2263 /* WINED3DSIH_ENDIF */ NULL
,
2264 /* WINED3DSIH_ENDLOOP */ NULL
,
2265 /* WINED3DSIH_ENDREP */ NULL
,
2266 /* WINED3DSIH_EXP */ shader_hw_map2gl
,
2267 /* WINED3DSIH_EXPP */ shader_hw_map2gl
,
2268 /* WINED3DSIH_FRC */ shader_hw_map2gl
,
2269 /* WINED3DSIH_IF */ NULL
,
2270 /* WINED3DSIH_IFC */ NULL
,
2271 /* WINED3DSIH_LABEL */ NULL
,
2272 /* WINED3DSIH_LIT */ shader_hw_map2gl
,
2273 /* WINED3DSIH_LOG */ shader_hw_map2gl
,
2274 /* WINED3DSIH_LOGP */ shader_hw_map2gl
,
2275 /* WINED3DSIH_LOOP */ NULL
,
2276 /* WINED3DSIH_LRP */ shader_hw_map2gl
,
2277 /* WINED3DSIH_M3x2 */ shader_hw_mnxn
,
2278 /* WINED3DSIH_M3x3 */ shader_hw_mnxn
,
2279 /* WINED3DSIH_M3x4 */ shader_hw_mnxn
,
2280 /* WINED3DSIH_M4x3 */ shader_hw_mnxn
,
2281 /* WINED3DSIH_M4x4 */ shader_hw_mnxn
,
2282 /* WINED3DSIH_MAD */ shader_hw_map2gl
,
2283 /* WINED3DSIH_MAX */ shader_hw_map2gl
,
2284 /* WINED3DSIH_MIN */ shader_hw_map2gl
,
2285 /* WINED3DSIH_MOV */ shader_hw_mov
,
2286 /* WINED3DSIH_MOVA */ shader_hw_mov
,
2287 /* WINED3DSIH_MUL */ shader_hw_map2gl
,
2288 /* WINED3DSIH_NOP */ shader_hw_map2gl
,
2289 /* WINED3DSIH_NRM */ shader_hw_nrm
,
2290 /* WINED3DSIH_PHASE */ NULL
,
2291 /* WINED3DSIH_POW */ shader_hw_map2gl
,
2292 /* WINED3DSIH_RCP */ vshader_hw_rsq_rcp
,
2293 /* WINED3DSIH_REP */ NULL
,
2294 /* WINED3DSIH_RET */ NULL
,
2295 /* WINED3DSIH_RSQ */ vshader_hw_rsq_rcp
,
2296 /* WINED3DSIH_SETP */ NULL
,
2297 /* WINED3DSIH_SGE */ shader_hw_map2gl
,
2298 /* WINED3DSIH_SGN */ NULL
,
2299 /* WINED3DSIH_SINCOS */ shader_hw_sincos
,
2300 /* WINED3DSIH_SLT */ shader_hw_map2gl
,
2301 /* WINED3DSIH_SUB */ shader_hw_map2gl
,
2302 /* WINED3DSIH_TEX */ pshader_hw_tex
,
2303 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem
,
2304 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem
,
2305 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord
,
2306 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth
,
2307 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3
,
2308 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex
,
2309 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill
,
2310 /* WINED3DSIH_TEXLDD */ NULL
,
2311 /* WINED3DSIH_TEXLDL */ NULL
,
2312 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth
,
2313 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad
,
2314 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex
,
2315 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3
,
2316 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
2317 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad
,
2318 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec
,
2319 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex
,
2320 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec
,
2321 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar
,
2322 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb
,
2323 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb
,
2326 const shader_backend_t arb_program_shader_backend
= {
2327 shader_arb_instruction_handler_table
,
2329 shader_arb_select_depth_blt
,
2330 shader_arb_deselect_depth_blt
,
2331 shader_arb_load_constants
,
2333 shader_arb_color_correction
,
2337 shader_arb_dirty_const
,
2338 shader_arb_generate_pshader
,
2339 shader_arb_generate_vshader
,
2340 shader_arb_get_caps
,
2341 shader_arb_conv_supported
,
2344 /* ARB_fragment_program fixed function pipeline replacement definitions */
2345 #define ARB_FFP_CONST_TFACTOR 0
2346 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
2347 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
2348 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
2349 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
2351 struct arbfp_ffp_desc
2353 struct ffp_frag_desc parent
;
2355 unsigned int num_textures_used
;
2358 static void arbfp_enable(IWineD3DDevice
*iface
, BOOL enable
) {
2360 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
2361 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
2363 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
2364 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
2368 static HRESULT
arbfp_alloc(IWineD3DDevice
*iface
) {
2369 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
2370 struct shader_arb_priv
*priv
;
2371 /* Share private data between the shader backend and the pipeline replacement, if both
2372 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
2373 * if no pixel shader is bound or not
2375 if(This
->shader_backend
== &arb_program_shader_backend
) {
2376 This
->fragment_priv
= This
->shader_priv
;
2378 This
->fragment_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_arb_priv
));
2379 if(!This
->fragment_priv
) return E_OUTOFMEMORY
;
2381 priv
= (struct shader_arb_priv
*) This
->fragment_priv
;
2382 priv
->fragment_shaders
= hash_table_create(ffp_frag_program_key_hash
, ffp_frag_program_key_compare
);
2383 priv
->use_arbfp_fixed_func
= TRUE
;
2387 static void arbfp_free_ffpshader(void *value
, void *gli
) {
2388 const WineD3D_GL_Info
*gl_info
= gli
;
2389 struct arbfp_ffp_desc
*entry_arb
= value
;
2392 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb
->shader
));
2393 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
2394 HeapFree(GetProcessHeap(), 0, entry_arb
);
2398 static void arbfp_free(IWineD3DDevice
*iface
) {
2399 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
2400 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) This
->fragment_priv
;
2402 hash_table_destroy(priv
->fragment_shaders
, arbfp_free_ffpshader
, &This
->adapter
->gl_info
);
2403 priv
->use_arbfp_fixed_func
= FALSE
;
2405 if(This
->shader_backend
!= &arb_program_shader_backend
) {
2406 HeapFree(GetProcessHeap(), 0, This
->fragment_priv
);
2410 static void arbfp_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct fragment_caps
*caps
)
2412 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
|
2413 WINED3DTEXOPCAPS_SELECTARG1
|
2414 WINED3DTEXOPCAPS_SELECTARG2
|
2415 WINED3DTEXOPCAPS_MODULATE4X
|
2416 WINED3DTEXOPCAPS_MODULATE2X
|
2417 WINED3DTEXOPCAPS_MODULATE
|
2418 WINED3DTEXOPCAPS_ADDSIGNED2X
|
2419 WINED3DTEXOPCAPS_ADDSIGNED
|
2420 WINED3DTEXOPCAPS_ADD
|
2421 WINED3DTEXOPCAPS_SUBTRACT
|
2422 WINED3DTEXOPCAPS_ADDSMOOTH
|
2423 WINED3DTEXOPCAPS_BLENDCURRENTALPHA
|
2424 WINED3DTEXOPCAPS_BLENDFACTORALPHA
|
2425 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
|
2426 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
|
2427 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
|
2428 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
|
2429 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
|
2430 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
|
2431 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
|
2432 WINED3DTEXOPCAPS_DOTPRODUCT3
|
2433 WINED3DTEXOPCAPS_MULTIPLYADD
|
2434 WINED3DTEXOPCAPS_LERP
|
2435 WINED3DTEXOPCAPS_BUMPENVMAP
|
2436 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
2438 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
2440 caps
->MaxTextureBlendStages
= 8;
2441 caps
->MaxSimultaneousTextures
= min(GL_LIMITS(fragment_samplers
), 8);
2443 caps
->PrimitiveMiscCaps
|= WINED3DPMISCCAPS_TSSARGTEMP
;
2445 #undef GLINFO_LOCATION
2447 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
2448 static void state_texfactor_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
2450 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
2452 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2453 * application provided constants
2455 if(device
->shader_backend
== &arb_program_shader_backend
) {
2456 if(use_ps(device
)) return;
2458 device
= stateblock
->wineD3DDevice
;
2459 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_TFACTOR
] = 1;
2460 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_TFACTOR
+ 1);
2463 D3DCOLORTOGLFLOAT4(stateblock
->renderState
[WINED3DRS_TEXTUREFACTOR
], col
);
2464 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_TFACTOR
, col
));
2465 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
2469 static void state_arb_specularenable(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
2471 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
2473 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2474 * application provided constants
2476 if(device
->shader_backend
== &arb_program_shader_backend
) {
2477 if(use_ps(device
)) return;
2479 device
= stateblock
->wineD3DDevice
;
2480 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_SPECULAR_ENABLE
] = 1;
2481 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_SPECULAR_ENABLE
+ 1);
2484 if(stateblock
->renderState
[WINED3DRS_SPECULARENABLE
]) {
2485 /* The specular color has no alpha */
2486 col
[0] = 1.0; col
[1] = 1.0;
2487 col
[2] = 1.0; col
[3] = 0.0;
2489 col
[0] = 0.0; col
[1] = 0.0;
2490 col
[2] = 0.0; col
[3] = 0.0;
2492 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_SPECULAR_ENABLE
, col
));
2493 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
2496 static void set_bumpmat_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
2497 DWORD stage
= (state
- STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE
;
2498 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
2501 if(use_ps(device
)) {
2503 ((IWineD3DPixelShaderImpl
*) stateblock
->pixelShader
)->baseShader
.reg_maps
.bumpmat
[stage
]) {
2504 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
2507 if(!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
)) {
2508 device
->StateTable
[STATE_PIXELSHADERCONSTANT
].apply(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
2512 if(device
->shader_backend
== &arb_program_shader_backend
) {
2513 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2516 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
2517 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_BUMPMAT(stage
)] = 1;
2518 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_BUMPMAT(stage
) + 1);
2521 mat
[0][0] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT00
]);
2522 mat
[0][1] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT01
]);
2523 mat
[1][0] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT10
]);
2524 mat
[1][1] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT11
]);
2526 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_BUMPMAT(stage
), &mat
[0][0]));
2527 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
2530 static void tex_bumpenvlum_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
2531 DWORD stage
= (state
- STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE
;
2532 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
2535 if(use_ps(device
)) {
2537 ((IWineD3DPixelShaderImpl
*) stateblock
->pixelShader
)->baseShader
.reg_maps
.luminanceparams
[stage
]) {
2538 /* The pixel shader has to know the luminance offset. Do a constants update if it
2539 * isn't scheduled anyway
2541 if(!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
)) {
2542 device
->StateTable
[STATE_PIXELSHADERCONSTANT
].apply(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
2546 if(device
->shader_backend
== &arb_program_shader_backend
) {
2547 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2550 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
2551 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_LUMINANCE(stage
)] = 1;
2552 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_LUMINANCE(stage
) + 1);
2555 param
[0] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
]);
2556 param
[1] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
]);
2560 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_LUMINANCE(stage
), param
));
2561 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
2564 static const char *get_argreg(SHADER_BUFFER
*buffer
, DWORD argnum
, unsigned int stage
, DWORD arg
) {
2567 if(arg
== ARG_UNUSED
) return "unused"; /* This is the marker for unused registers */
2569 switch(arg
& WINED3DTA_SELECTMASK
) {
2570 case WINED3DTA_DIFFUSE
:
2571 ret
= "fragment.color.primary"; break;
2573 case WINED3DTA_CURRENT
:
2574 if(stage
== 0) ret
= "fragment.color.primary";
2578 case WINED3DTA_TEXTURE
:
2580 case 0: ret
= "tex0"; break;
2581 case 1: ret
= "tex1"; break;
2582 case 2: ret
= "tex2"; break;
2583 case 3: ret
= "tex3"; break;
2584 case 4: ret
= "tex4"; break;
2585 case 5: ret
= "tex5"; break;
2586 case 6: ret
= "tex6"; break;
2587 case 7: ret
= "tex7"; break;
2588 default: ret
= "unknown texture";
2592 case WINED3DTA_TFACTOR
:
2593 ret
= "tfactor"; break;
2595 case WINED3DTA_SPECULAR
:
2596 ret
= "fragment.color.secondary"; break;
2598 case WINED3DTA_TEMP
:
2599 ret
= "tempreg"; break;
2601 case WINED3DTA_CONSTANT
:
2602 FIXME("Implement perstage constants\n");
2604 case 0: ret
= "const0"; break;
2605 case 1: ret
= "const1"; break;
2606 case 2: ret
= "const2"; break;
2607 case 3: ret
= "const3"; break;
2608 case 4: ret
= "const4"; break;
2609 case 5: ret
= "const5"; break;
2610 case 6: ret
= "const6"; break;
2611 case 7: ret
= "const7"; break;
2612 default: ret
= "unknown constant";
2620 if(arg
& WINED3DTA_COMPLEMENT
) {
2621 shader_addline(buffer
, "SUB arg%u, const.x, %s;\n", argnum
, ret
);
2622 if(argnum
== 0) ret
= "arg0";
2623 if(argnum
== 1) ret
= "arg1";
2624 if(argnum
== 2) ret
= "arg2";
2626 if(arg
& WINED3DTA_ALPHAREPLICATE
) {
2627 shader_addline(buffer
, "MOV arg%u, %s.a;\n", argnum
, ret
);
2628 if(argnum
== 0) ret
= "arg0";
2629 if(argnum
== 1) ret
= "arg1";
2630 if(argnum
== 2) ret
= "arg2";
2635 static void gen_ffp_instr(SHADER_BUFFER
*buffer
, unsigned int stage
, BOOL color
, BOOL alpha
,
2636 DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
) {
2637 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
2638 unsigned int mul
= 1;
2639 BOOL mul_final_dest
= FALSE
;
2641 if(color
&& alpha
) dstmask
= "";
2642 else if(color
) dstmask
= ".rgb";
2643 else dstmask
= ".a";
2645 if(dst
== tempreg
) dstreg
= "tempreg";
2646 else dstreg
= "ret";
2648 arg0
= get_argreg(buffer
, 0, stage
, dw_arg0
);
2649 arg1
= get_argreg(buffer
, 1, stage
, dw_arg1
);
2650 arg2
= get_argreg(buffer
, 2, stage
, dw_arg2
);
2653 case WINED3DTOP_DISABLE
:
2654 if(stage
== 1) shader_addline(buffer
, "MOV %s%s, fragment.color.primary;\n", dstreg
, dstmask
);
2657 case WINED3DTOP_SELECTARG2
:
2659 case WINED3DTOP_SELECTARG1
:
2660 shader_addline(buffer
, "MOV %s%s, %s;\n", dstreg
, dstmask
, arg1
);
2663 case WINED3DTOP_MODULATE4X
:
2665 case WINED3DTOP_MODULATE2X
:
2667 if(strcmp(dstreg
, "result.color") == 0) {
2669 mul_final_dest
= TRUE
;
2671 case WINED3DTOP_MODULATE
:
2672 shader_addline(buffer
, "MUL %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
2675 case WINED3DTOP_ADDSIGNED2X
:
2677 if(strcmp(dstreg
, "result.color") == 0) {
2679 mul_final_dest
= TRUE
;
2681 case WINED3DTOP_ADDSIGNED
:
2682 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
2684 case WINED3DTOP_ADD
:
2685 shader_addline(buffer
, "ADD_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
2688 case WINED3DTOP_SUBTRACT
:
2689 shader_addline(buffer
, "SUB_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
2692 case WINED3DTOP_ADDSMOOTH
:
2693 shader_addline(buffer
, "SUB arg1, const.x, %s;\n", arg1
);
2694 shader_addline(buffer
, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
2697 case WINED3DTOP_BLENDCURRENTALPHA
:
2698 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
2699 shader_addline(buffer
, "LRP %s%s, %s.a, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
2701 case WINED3DTOP_BLENDFACTORALPHA
:
2702 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
2703 shader_addline(buffer
, "LRP %s%s, %s.a, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
2705 case WINED3DTOP_BLENDTEXTUREALPHA
:
2706 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
2707 shader_addline(buffer
, "LRP %s%s, %s.a, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
2709 case WINED3DTOP_BLENDDIFFUSEALPHA
:
2710 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
2711 shader_addline(buffer
, "LRP %s%s, %s.a, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
2714 case WINED3DTOP_BLENDTEXTUREALPHAPM
:
2715 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
2716 shader_addline(buffer
, "SUB arg0.a, const.x, %s.a;\n", arg0
);
2717 shader_addline(buffer
, "MAD_SAT %s%s, %s, arg0.a, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
2720 /* D3DTOP_PREMODULATE ???? */
2722 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR
:
2723 shader_addline(buffer
, "SUB arg0.a, const.x, %s;\n", arg1
);
2724 shader_addline(buffer
, "MAD_SAT %s%s, arg0.a, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
2726 case WINED3DTOP_MODULATEALPHA_ADDCOLOR
:
2727 shader_addline(buffer
, "MAD_SAT %s%s, %s.a, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
2729 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA
:
2730 shader_addline(buffer
, "SUB arg0, const.x, %s;\n", arg1
);
2731 shader_addline(buffer
, "MAD_SAT %s%s, arg0, %s, %s.a;\n", dstreg
, dstmask
, arg2
, arg1
);
2733 case WINED3DTOP_MODULATECOLOR_ADDALPHA
:
2734 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s.a;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
2737 case WINED3DTOP_DOTPRODUCT3
:
2739 if(strcmp(dstreg
, "result.color") == 0) {
2741 mul_final_dest
= TRUE
;
2743 shader_addline(buffer
, "SUB arg1, %s, const.w;\n", arg1
);
2744 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
2745 shader_addline(buffer
, "DP3_SAT %s%s, arg1, arg2;\n", dstreg
, dstmask
);
2748 case WINED3DTOP_MULTIPLYADD
:
2749 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg0
);
2752 case WINED3DTOP_LERP
:
2753 /* The msdn is not quite right here */
2754 shader_addline(buffer
, "LRP %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
2757 case WINED3DTOP_BUMPENVMAP
:
2758 case WINED3DTOP_BUMPENVMAPLUMINANCE
:
2759 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
2763 FIXME("Unhandled texture op %08x\n", op
);
2767 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
2768 } else if(mul
== 4) {
2769 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
2773 /* The stateblock is passed for GLINFO_LOCATION */
2774 static GLuint
gen_arbfp_ffp_shader(const struct ffp_frag_settings
*settings
, IWineD3DStateBlockImpl
*stateblock
)
2777 SHADER_BUFFER buffer
;
2778 BOOL tex_read
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
2779 BOOL bump_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
2780 BOOL luminance_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
2781 const char *textype
;
2782 const char *instr
, *sat
;
2783 char colorcor_dst
[8];
2785 DWORD arg0
, arg1
, arg2
;
2786 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
2788 const char *final_combiner_src
= "ret";
2790 /* Find out which textures are read */
2791 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
2792 if(settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
) break;
2793 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
2794 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
2795 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
2796 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
2797 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
2798 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
2800 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHA
) tex_read
[stage
] = TRUE
;
2801 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHAPM
) tex_read
[stage
] = TRUE
;
2802 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
) {
2803 bump_used
[stage
] = TRUE
;
2804 tex_read
[stage
] = TRUE
;
2806 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
2807 bump_used
[stage
] = TRUE
;
2808 tex_read
[stage
] = TRUE
;
2809 luminance_used
[stage
] = TRUE
;
2810 } else if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDFACTORALPHA
) {
2811 tfactor_used
= TRUE
;
2814 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
2815 tfactor_used
= TRUE
;
2818 if(settings
->op
[stage
].dst
== tempreg
) tempreg_used
= TRUE
;
2819 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
2820 tempreg_used
= TRUE
;
2823 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) continue;
2824 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
2825 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
2826 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
2827 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
2828 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
2829 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
2831 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
2832 tempreg_used
= TRUE
;
2834 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
2835 tfactor_used
= TRUE
;
2842 buffer
.newline
= TRUE
;
2843 buffer
.buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, SHADER_PGMSIZE
);
2845 shader_addline(&buffer
, "!!ARBfp1.0\n");
2847 switch(settings
->fog
) {
2848 case FOG_OFF
: break;
2849 case FOG_LINEAR
: shader_addline(&buffer
, "OPTION ARB_fog_linear;\n"); break;
2850 case FOG_EXP
: shader_addline(&buffer
, "OPTION ARB_fog_exp;\n"); break;
2851 case FOG_EXP2
: shader_addline(&buffer
, "OPTION ARB_fog_exp2;\n"); break;
2852 default: FIXME("Unexpected fog setting %d\n", settings
->fog
);
2855 shader_addline(&buffer
, "PARAM const = {1, 2, 4, 0.5};\n");
2856 shader_addline(&buffer
, "TEMP TMP;\n");
2857 shader_addline(&buffer
, "TEMP ret;\n");
2858 if(tempreg_used
|| settings
->sRGB_write
) shader_addline(&buffer
, "TEMP tempreg;\n");
2859 shader_addline(&buffer
, "TEMP arg0;\n");
2860 shader_addline(&buffer
, "TEMP arg1;\n");
2861 shader_addline(&buffer
, "TEMP arg2;\n");
2862 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
2863 if(!tex_read
[stage
]) continue;
2864 shader_addline(&buffer
, "TEMP tex%u;\n", stage
);
2865 if(!bump_used
[stage
]) continue;
2866 shader_addline(&buffer
, "PARAM bumpmat%u = program.env[%u];\n", stage
, ARB_FFP_CONST_BUMPMAT(stage
));
2867 if(!luminance_used
[stage
]) continue;
2868 shader_addline(&buffer
, "PARAM luminance%u = program.env[%u];\n", stage
, ARB_FFP_CONST_LUMINANCE(stage
));
2871 shader_addline(&buffer
, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR
);
2873 shader_addline(&buffer
, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE
);
2875 if(settings
->sRGB_write
) {
2876 shader_addline(&buffer
, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
2877 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
2878 shader_addline(&buffer
, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
2879 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
2880 shader_addline(&buffer
, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
2881 srgb_pow
, srgb_pow
, srgb_pow
);
2882 shader_addline(&buffer
, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
2883 srgb_mul_high
, srgb_mul_high
, srgb_mul_high
);
2884 shader_addline(&buffer
, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
2885 srgb_sub_high
, srgb_sub_high
, srgb_sub_high
);
2888 /* Generate texture sampling instructions) */
2889 for(stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3DTOP_DISABLE
; stage
++) {
2890 if(!tex_read
[stage
]) continue;
2892 switch(settings
->op
[stage
].tex_type
) {
2893 case tex_1d
: textype
= "1D"; break;
2894 case tex_2d
: textype
= "2D"; break;
2895 case tex_3d
: textype
= "3D"; break;
2896 case tex_cube
: textype
= "CUBE"; break;
2897 case tex_rect
: textype
= "RECT"; break;
2898 default: textype
= "unexpected_textype"; break;
2901 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
||
2902 settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
2908 if(settings
->op
[stage
].projected
== proj_none
) {
2910 } else if(settings
->op
[stage
].projected
== proj_count4
||
2911 settings
->op
[stage
].projected
== proj_count3
) {
2914 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
2919 (settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAP
||
2920 settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
)) {
2921 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage
- 1);
2922 shader_addline(&buffer
, "DP3 ret.r, arg1, tex%u;\n", stage
- 1);
2923 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage
- 1);
2924 shader_addline(&buffer
, "DP3 ret.g, arg1, tex%u;\n", stage
- 1);
2926 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2927 * so multiply the displacement with the dividing parameter before passing it to TXP
2929 if (settings
->op
[stage
].projected
!= proj_none
) {
2930 if(settings
->op
[stage
].projected
== proj_count4
) {
2931 shader_addline(&buffer
, "MOV ret.a, fragment.texcoord[%u].a;\n", stage
);
2932 shader_addline(&buffer
, "MUL ret.rgb, ret, fragment.texcoord[%u].a, fragment.texcoord[%u];\n", stage
, stage
);
2934 shader_addline(&buffer
, "MOV ret.a, fragment.texcoord[%u].b;\n", stage
);
2935 shader_addline(&buffer
, "MAD ret.rgb, ret, fragment.texcoord[%u].b, fragment.texcoord[%u];\n", stage
, stage
);
2938 shader_addline(&buffer
, "ADD ret, ret, fragment.texcoord[%u];\n", stage
);
2941 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
2942 instr
, sat
, stage
, stage
, textype
);
2943 if(settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
2944 shader_addline(&buffer
, "MAD_SAT ret.r, tex%u.b, luminance%u.r, luminance%u.g;\n",
2945 stage
- 1, stage
- 1, stage
- 1);
2946 shader_addline(&buffer
, "MUL tex%u, tex%u, ret.r;\n", stage
, stage
);
2948 } else if(settings
->op
[stage
].projected
== proj_count3
) {
2949 shader_addline(&buffer
, "MOV ret, fragment.texcoord[%u];\n", stage
);
2950 shader_addline(&buffer
, "MOV ret.a, ret.b;\n");
2951 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
2952 instr
, sat
, stage
, stage
, textype
);
2954 shader_addline(&buffer
, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
2955 instr
, sat
, stage
, stage
, stage
, textype
);
2958 sprintf(colorcor_dst
, "tex%u", stage
);
2959 gen_color_correction(&buffer
, colorcor_dst
, ".rgba", "const.x", "const.y",
2960 settings
->op
[stage
].color_correction
, &GLINFO_LOCATION
);
2963 /* Generate the main shader */
2964 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
2965 if(settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
) {
2967 final_combiner_src
= "fragment.color.primary";
2972 if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
2973 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
2974 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
2975 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
2976 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
2977 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
2978 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
2979 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
2980 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
2981 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
2982 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
2983 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
2985 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
&&
2986 settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
&&
2987 settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
&&
2988 settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
2991 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) {
2992 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
2993 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
2994 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
2996 shader_addline(&buffer
, "MOV ret.a, fragment.color.primary.a;\n");
2998 } else if(op_equal
) {
2999 gen_ffp_instr(&buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
3000 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
3001 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
3003 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
3004 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
3005 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
3006 gen_ffp_instr(&buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
3007 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
3008 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
3012 if(settings
->sRGB_write
) {
3013 shader_addline(&buffer
, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
3014 arbfp_add_sRGB_correction(&buffer
, "ret", "arg0", "arg1", "arg2", "tempreg");
3015 shader_addline(&buffer
, "MOV result.color.a, ret.a;\n");
3017 shader_addline(&buffer
, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
3021 shader_addline(&buffer
, "END\n");
3023 /* Generate the shader */
3024 GL_EXTCALL(glGenProgramsARB(1, &ret
));
3025 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
));
3026 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(buffer
.buffer
), buffer
.buffer
));
3028 if (glGetError() == GL_INVALID_OPERATION
) {
3030 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
3031 FIXME("Fragment program error at position %d: %s\n", pos
,
3032 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3034 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3038 static void fragment_prog_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
3039 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
3040 struct shader_arb_priv
*priv
= (struct shader_arb_priv
*) device
->fragment_priv
;
3041 BOOL use_pshader
= use_ps(device
);
3042 BOOL use_vshader
= use_vs(device
);
3043 struct ffp_frag_settings settings
;
3044 const struct arbfp_ffp_desc
*desc
;
3047 if(isStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
))) {
3048 if(!use_pshader
&& device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
3049 /* Reload fixed function constants since they collide with the pixel shader constants */
3050 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
3051 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
3053 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
3054 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
3060 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
3061 gen_ffp_frag_op(stateblock
, &settings
, FALSE
);
3062 desc
= (const struct arbfp_ffp_desc
*)find_ffp_frag_shader(priv
->fragment_shaders
, &settings
);
3064 struct arbfp_ffp_desc
*new_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc
));
3067 ERR("Out of memory\n");
3070 new_desc
->num_textures_used
= 0;
3071 for(i
= 0; i
< GL_LIMITS(texture_stages
); i
++) {
3072 if(settings
.op
[i
].cop
== WINED3DTOP_DISABLE
) break;
3073 new_desc
->num_textures_used
= i
;
3076 memcpy(&new_desc
->parent
.settings
, &settings
, sizeof(settings
));
3077 new_desc
->shader
= gen_arbfp_ffp_shader(&settings
, stateblock
);
3078 add_ffp_frag_shader(priv
->fragment_shaders
, &new_desc
->parent
);
3079 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc
);
3083 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
3084 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
3087 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, desc
->shader
));
3088 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
3089 priv
->current_fprogram_id
= desc
->shader
;
3091 if(device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
3092 /* Reload fixed function constants since they collide with the pixel shader constants */
3093 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
3094 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
3096 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
3097 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
3101 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
3102 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
3103 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
3104 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
3105 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
3107 * Don't call shader_select if the vertex shader is dirty, because some shader backends(GLSL) need both shaders
3108 * to be compiled before activating them(needs some cleanups in the shader backend interface)
3110 if(!isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
3111 device
->shader_backend
->shader_select((IWineD3DDevice
*)stateblock
->wineD3DDevice
, use_pshader
, use_vshader
);
3113 if (!isStateDirty(context
, STATE_VERTEXSHADERCONSTANT
) && (use_vshader
|| use_pshader
)) {
3114 device
->StateTable
[STATE_VERTEXSHADERCONSTANT
].apply(STATE_VERTEXSHADERCONSTANT
, stateblock
, context
);
3118 device
->StateTable
[STATE_PIXELSHADERCONSTANT
].apply(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
3122 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
3123 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
3124 * state table, so we need to handle that with a forwarding function. The other invisible side effect
3125 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
3126 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
3128 static void state_arbfp_fog(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
3129 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
3130 fragment_prog_arbfp(state
, stateblock
, context
);
3134 static void textransform(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
3135 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
3136 fragment_prog_arbfp(state
, stateblock
, context
);
3140 #undef GLINFO_LOCATION
3142 static const struct StateEntryTemplate arbfp_fragmentstate_template
[] = {
3143 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), state_texfactor_arbfp
}, 0 },
3144 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3145 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3146 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3147 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3148 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3149 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3150 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3151 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3152 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3153 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3154 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3155 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3156 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3157 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3158 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3159 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3160 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3161 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3162 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3163 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3164 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3165 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3166 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3167 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3168 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3169 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3170 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3171 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3172 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3173 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3174 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3175 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3176 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3177 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3178 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3179 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3180 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3181 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3182 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3183 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3184 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3185 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3186 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3187 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3188 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3189 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3190 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3191 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3192 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3193 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3194 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3195 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3196 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3197 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3198 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3199 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3200 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3201 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3202 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3203 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3204 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3205 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3206 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3207 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3208 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3209 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3210 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3211 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3212 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3213 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3214 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3215 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3216 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3217 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3218 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3219 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3220 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3221 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3222 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3223 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3224 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3225 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3226 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3227 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3228 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3229 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3230 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3231 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3232 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3233 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3234 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3235 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3236 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3237 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3238 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3239 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3240 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3241 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3242 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3243 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3244 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3245 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3246 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3247 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3248 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3249 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3250 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3251 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3252 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3253 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3254 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3255 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3256 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3257 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3258 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3259 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3260 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3261 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, 0 },
3262 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3263 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, 0 },
3264 { STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim
}, 0 },
3265 { STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim
}, 0 },
3266 { STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim
}, 0 },
3267 { STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim
}, 0 },
3268 { STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim
}, 0 },
3269 { STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim
}, 0 },
3270 { STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim
}, 0 },
3271 { STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim
}, 0 },
3272 { STATE_PIXELSHADER
, { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3273 { STATE_RENDER(WINED3DRS_FOGENABLE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, 0 },
3274 { STATE_RENDER(WINED3DRS_FOGTABLEMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, 0 },
3275 { STATE_RENDER(WINED3DRS_FOGVERTEXMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, 0 },
3276 { STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, 0 },
3277 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3278 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3279 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3280 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3281 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3282 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3283 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3284 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, 0 },
3285 { STATE_RENDER(WINED3DRS_SPECULARENABLE
), { STATE_RENDER(WINED3DRS_SPECULARENABLE
), state_arb_specularenable
}, 0 },
3286 {0 /* Terminate */, { 0, 0 }, 0 },
3289 const struct fragment_pipeline arbfp_fragment_pipeline
= {
3294 shader_arb_conv_supported
,
3295 arbfp_fragmentstate_template
,
3296 TRUE
/* We can disable projected textures */
3299 #define GLINFO_LOCATION device->adapter->gl_info
3301 struct arbfp_blit_priv
{
3302 GLenum yuy2_rect_shader
, yuy2_2d_shader
;
3303 GLenum uyvy_rect_shader
, uyvy_2d_shader
;
3304 GLenum yv12_rect_shader
, yv12_2d_shader
;
3307 static HRESULT
arbfp_blit_alloc(IWineD3DDevice
*iface
) {
3308 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
3309 device
->blit_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct arbfp_blit_priv
));
3310 if(!device
->blit_priv
) {
3311 ERR("Out of memory\n");
3312 return E_OUTOFMEMORY
;
3316 static void arbfp_blit_free(IWineD3DDevice
*iface
) {
3317 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
3318 struct arbfp_blit_priv
*priv
= (struct arbfp_blit_priv
*) device
->blit_priv
;
3321 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_rect_shader
));
3322 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_2d_shader
));
3323 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_rect_shader
));
3324 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_2d_shader
));
3325 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_rect_shader
));
3326 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_2d_shader
));
3327 checkGLcall("Delete yuv programs\n");
3331 static BOOL
gen_planar_yuv_read(SHADER_BUFFER
*buffer
, WINED3DFORMAT fmt
, GLenum textype
, char *luminance
) {
3333 const char *tex
, *texinstr
;
3335 if(fmt
== WINED3DFMT_UYVY
) {
3343 case GL_TEXTURE_2D
: tex
= "2D"; texinstr
= "TXP"; break;
3344 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; texinstr
= "TEX"; break;
3346 /* This is more tricky than just replacing the texture type - we have to navigate
3347 * properly in the texture to find the correct chroma values
3349 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
3353 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
3354 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
3355 * filtering when we sample the texture.
3357 * These are the rules for reading the chroma:
3363 * So we have to get the sampling x position in non-normalized coordinates in integers
3365 if(textype
!= GL_TEXTURE_RECTANGLE_ARB
) {
3366 shader_addline(buffer
, "MUL texcrd.rg, fragment.texcoord[0], size.x;\n");
3367 shader_addline(buffer
, "MOV texcrd.a, size.x;\n");
3369 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
3371 /* We must not allow filtering between pixel x and x+1, this would mix U and V
3372 * Vertical filtering is ok. However, bear in mind that the pixel center is at
3375 shader_addline(buffer
, "FLR texcrd.x, texcrd.x;\n");
3376 shader_addline(buffer
, "ADD texcrd.x, texcrd.x, coef.y;\n");
3378 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
3379 * even and odd pixels respectively
3381 shader_addline(buffer
, "MUL texcrd2, texcrd, coef.y;\n");
3382 shader_addline(buffer
, "FRC texcrd2, texcrd2;\n");
3384 /* Sample Pixel 1 */
3385 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
3387 /* Put the value into either of the chroma values */
3388 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
3389 shader_addline(buffer
, "MUL chroma.r, luminance.%c, temp.x;\n", chroma
);
3390 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
3391 shader_addline(buffer
, "MUL chroma.g, luminance.%c, temp.x;\n", chroma
);
3393 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
3394 * the pixel right to the current one. Otherwise, sample the left pixel.
3395 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
3397 shader_addline(buffer
, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
3398 shader_addline(buffer
, "ADD texcrd.x, texcrd, temp.x;\n");
3399 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
3401 /* Put the value into the other chroma */
3402 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
3403 shader_addline(buffer
, "MAD chroma.g, luminance.%c, temp.x, chroma.g;\n", chroma
);
3404 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
3405 shader_addline(buffer
, "MAD chroma.r, luminance.%c, temp.x, chroma.r;\n", chroma
);
3407 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
3408 * the current one and lerp the two U and V values
3411 /* This gives the correctly filtered luminance value */
3412 shader_addline(buffer
, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex
);
3417 static BOOL
gen_yv12_read(SHADER_BUFFER
*buffer
, WINED3DFORMAT fmt
, GLenum textype
, char *luminance
) {
3421 case GL_TEXTURE_2D
: tex
= "2D"; break;
3422 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; break;
3424 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
3428 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
3429 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
3430 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
3431 * pitch of the luminance plane, the packing into the gl texture is a bit
3432 * unfortunate. If the whole texture is interpreted as luminance data it looks
3433 * approximately like this:
3435 * +----------------------------------+----
3447 * +----------------+-----------------+----
3449 * | U even rows | U odd rows |
3451 * +----------------+------------------ -
3453 * | V even rows | V odd rows |
3455 * +----------------+-----------------+----
3459 * So it appears as if there are 4 chroma images, but in fact the odd rows
3460 * in the chroma images are in the same row as the even ones. So its is
3461 * kinda tricky to read
3463 * When reading from rectangle textures, keep in mind that the input y coordinates
3464 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
3466 shader_addline(buffer
, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
3467 2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
3469 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
3470 /* the chroma planes have only half the width */
3471 shader_addline(buffer
, "MUL texcrd.x, texcrd.x, coef.y;\n");
3473 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
3474 * the coordinate. Also read the right side of the image when reading odd lines
3476 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
3479 if(textype
== GL_TEXTURE_2D
) {
3481 shader_addline(buffer
, "RCP chroma.w, size.y;\n");
3483 shader_addline(buffer
, "MUL texcrd2.y, texcrd.y, size.y;\n");
3485 shader_addline(buffer
, "FLR texcrd2.y, texcrd2.y;\n");
3486 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
3488 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3489 shader_addline(buffer
, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3490 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
3491 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3492 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3494 /* clamp, keep the half pixel origin in mind */
3495 shader_addline(buffer
, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
3496 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
3497 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
3498 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
3500 /* Read from [size - size+size/4] */
3501 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
3502 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
3504 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3505 shader_addline(buffer
, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3506 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
3507 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3508 shader_addline(buffer
, "MUL texcrd2.x, texcrd2.x, size.x;\n");
3509 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3511 /* Make sure to read exactly from the pixel center */
3512 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
3513 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, coef.y;\n");
3516 shader_addline(buffer
, "MAD temp.y, size.y, coef.w, size.y;\n");
3517 shader_addline(buffer
, "ADD temp.y, temp.y, -coef.y;\n");
3518 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
3519 shader_addline(buffer
, "ADD temp.y, size.y, -coef.y;\n");
3520 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
3522 /* Read the texture, put the result into the output register */
3523 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
3524 shader_addline(buffer
, "MOV chroma.r, temp.a;\n");
3526 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
3527 * No need to clamp because we're just reusing the already clamped value from above
3529 if(textype
== GL_TEXTURE_2D
) {
3530 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
3532 shader_addline(buffer
, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
3534 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
3535 shader_addline(buffer
, "MOV chroma.g, temp.a;\n");
3537 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
3538 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
3539 * values due to filtering
3541 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
3542 if(textype
== GL_TEXTURE_2D
) {
3543 /* Multiply the y coordinate by 2/3 and clamp it */
3544 shader_addline(buffer
, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
3545 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
3546 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
3547 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
3549 /* Reading from texture_rectangles is pretty streightforward, just use the unmodified
3550 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
3553 shader_addline(buffer
, "ADD temp.x, size.y, -coef.y;\n");
3554 shader_addline(buffer
, "MIN texcrd.y, texcrd.y, size.x;\n");
3555 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
3562 static GLuint
gen_yuv_shader(IWineD3DDeviceImpl
*device
, WINED3DFORMAT fmt
, GLenum textype
) {
3564 SHADER_BUFFER buffer
;
3565 char luminance_component
;
3566 struct arbfp_blit_priv
*priv
= (struct arbfp_blit_priv
*) device
->blit_priv
;
3571 buffer
.newline
= TRUE
;
3572 buffer
.buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, SHADER_PGMSIZE
);
3575 GL_EXTCALL(glGenProgramsARB(1, &shader
));
3576 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
3577 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
3578 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3581 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3585 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
3586 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
3587 * two chroma(U and V) values. Each macropixel has two luminance values, one for
3588 * each single pixel it contains, and one U and one V value shared between both
3591 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
3592 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
3593 * take the format into account when generating the read swizzles
3595 * Reading the Y value is streightforward - just sample the texture. The hardware
3596 * takes care of filtering in the horizontal and vertical direction.
3598 * Reading the U and V values is harder. We have to avoid filtering horizontally,
3599 * because that would mix the U and V values of one pixel or two adjacent pixels.
3600 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
3601 * regardless of the filtering setting. Vertical filtering works automatically
3602 * though - the U and V values of two rows are mixed nicely.
3604 * Appart of avoiding filtering issues, the code has to know which value it just
3605 * read, and where it can find the other one. To determine this, it checks if
3606 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
3608 * Handling horizontal filtering of U and V values requires reading a 2nd pair
3609 * of pixels, extracting U and V and mixing them. This is not implemented yet.
3611 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
3612 * with width / 2. This way one read gives all 3 values, finding U and V is easy
3613 * in an unfiltered situation. Finding the luminance on the other hand requires
3614 * finding out if it is an odd or even pixel. The real drawback of this approach
3615 * is filtering. This would have to be emulated completely in the shader, reading
3616 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
3617 * vertically. Beyond that it would require adjustments to the texture handling
3618 * code to deal with the width scaling
3620 shader_addline(&buffer
, "!!ARBfp1.0\n");
3621 shader_addline(&buffer
, "TEMP luminance;\n");
3622 shader_addline(&buffer
, "TEMP temp;\n");
3623 shader_addline(&buffer
, "TEMP chroma;\n");
3624 shader_addline(&buffer
, "TEMP texcrd;\n");
3625 shader_addline(&buffer
, "TEMP texcrd2;\n");
3626 shader_addline(&buffer
, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
3627 shader_addline(&buffer
, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
3628 shader_addline(&buffer
, "PARAM size = program.local[0];\n");
3630 if(fmt
== WINED3DFMT_UYVY
|| fmt
==WINED3DFMT_YUY2
) {
3631 if(gen_planar_yuv_read(&buffer
, fmt
, textype
, &luminance_component
) == FALSE
) {
3632 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3636 if(gen_yv12_read(&buffer
, fmt
, textype
, &luminance_component
) == FALSE
) {
3637 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3642 /* Calculate the final result. Formula is taken from
3643 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
3644 * ranges from -0.5 to 0.5
3646 shader_addline(&buffer
, "SUB chroma.rg, chroma, coef.y;\n");
3648 shader_addline(&buffer
, "MAD result.color.r, chroma.r, yuv_coef.x, luminance.%c;\n", luminance_component
);
3649 shader_addline(&buffer
, "MAD temp.r, -chroma.g, yuv_coef.y, luminance.%c;\n", luminance_component
);
3650 shader_addline(&buffer
, "MAD result.color.g, -chroma.r, yuv_coef.z, temp.r;\n");
3651 shader_addline(&buffer
, "MAD result.color.b, chroma.g, yuv_coef.w, luminance.%c;\n", luminance_component
);
3652 shader_addline(&buffer
, "END\n");
3655 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(buffer
.buffer
), buffer
.buffer
));
3657 if (glGetError() == GL_INVALID_OPERATION
) {
3659 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
3660 FIXME("Fragment program error at position %d: %s\n", pos
,
3661 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3663 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3666 if(fmt
== WINED3DFMT_YUY2
) {
3667 if(textype
== GL_TEXTURE_RECTANGLE_ARB
) {
3668 priv
->yuy2_rect_shader
= shader
;
3670 priv
->yuy2_2d_shader
= shader
;
3672 } else if(fmt
== WINED3DFMT_UYVY
) {
3673 if(textype
== GL_TEXTURE_RECTANGLE_ARB
) {
3674 priv
->uyvy_rect_shader
= shader
;
3676 priv
->uyvy_2d_shader
= shader
;
3679 if(textype
== GL_TEXTURE_RECTANGLE_ARB
) {
3680 priv
->yv12_rect_shader
= shader
;
3682 priv
->yv12_2d_shader
= shader
;
3688 static HRESULT
arbfp_blit_set(IWineD3DDevice
*iface
, WINED3DFORMAT fmt
, GLenum textype
, UINT width
, UINT height
) {
3690 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
3691 float size
[4] = {width
, height
, 1, 1};
3692 struct arbfp_blit_priv
*priv
= (struct arbfp_blit_priv
*) device
->blit_priv
;
3693 const GlPixelFormatDesc
*glDesc
;
3695 getFormatDescEntry(fmt
, &GLINFO_LOCATION
, &glDesc
);
3697 if(glDesc
->conversion_group
!= WINED3DFMT_YUY2
&& glDesc
->conversion_group
!= WINED3DFMT_UYVY
&&
3698 glDesc
->conversion_group
!= WINED3DFMT_YV12
) {
3699 TRACE("Format: %s\n", debug_d3dformat(glDesc
->conversion_group
));
3700 /* Don't bother setting up a shader for unconverted formats */
3703 checkGLcall("glEnable(textype)");
3708 if(glDesc
->conversion_group
== WINED3DFMT_YUY2
) {
3709 if(textype
== GL_TEXTURE_RECTANGLE_ARB
) {
3710 shader
= priv
->yuy2_rect_shader
;
3712 shader
= priv
->yuy2_2d_shader
;
3714 } else if(glDesc
->conversion_group
== WINED3DFMT_UYVY
) {
3715 if(textype
== GL_TEXTURE_RECTANGLE_ARB
) {
3716 shader
= priv
->uyvy_rect_shader
;
3718 shader
= priv
->uyvy_2d_shader
;
3721 if(textype
== GL_TEXTURE_RECTANGLE_ARB
) {
3722 shader
= priv
->yv12_rect_shader
;
3724 shader
= priv
->yv12_2d_shader
;
3729 shader
= gen_yuv_shader(device
, glDesc
->conversion_group
, textype
);
3733 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
3734 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
3735 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
3736 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3737 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0, size
));
3738 checkGLcall("glProgramLocalParameter4fvARB");
3744 static void arbfp_blit_unset(IWineD3DDevice
*iface
) {
3745 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
3748 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
3749 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3750 glDisable(GL_TEXTURE_2D
);
3751 checkGLcall("glDisable(GL_TEXTURE_2D)");
3752 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
3753 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3754 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3756 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
3757 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
3758 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
3763 static BOOL
arbfp_blit_conv_supported(WINED3DFORMAT fmt
) {
3764 TRACE("Checking blit format support for format %s:", debug_d3dformat(fmt
));
3766 case WINED3DFMT_YUY2
:
3767 case WINED3DFMT_UYVY
:
3768 case WINED3DFMT_YV12
:
3772 TRACE("[FAILED]\n");
3777 const struct blit_shader arbfp_blit
= {
3782 arbfp_blit_conv_supported
3785 #undef GLINFO_LOCATION