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);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d);
41 #define GLINFO_LOCATION (*gl_info)
43 /* We have to subtract any other PARAMs that we might use in our shader programs.
44 * ATI seems to count 2 implicit PARAMs when we use fog and NVIDIA counts 1,
45 * and we reference one row of the PROJECTION matrix which counts as 1 PARAM. */
46 #define ARB_SHADER_RESERVED_VS_CONSTS 3
48 /* The arb shader only loads the bump mapping environment matrix into the shader if it finds
49 * a free constant to do that, so only reduce the number of available constants by 2 for the fog states.
51 #define ARB_SHADER_RESERVED_PS_CONSTS 2
53 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
54 * so upload them above that
56 #define ARB_SHADER_PRIVCONST_BASE (GL_LIMITS(vshader_constantsF) - ARB_SHADER_RESERVED_VS_CONSTS)
57 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
59 /* ARB_program_shader private data */
60 struct shader_arb_priv {
61 GLuint current_vprogram_id;
62 GLuint current_fprogram_id;
63 GLuint depth_blt_vprogram_id;
64 GLuint depth_blt_fprogram_id[tex_type_count];
65 BOOL use_arbfp_fixed_func;
66 struct hash_table_t *fragment_shaders;
69 /********************************************************
70 * ARB_[vertex/fragment]_program helper functions follow
71 ********************************************************/
74 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
75 * When constant_list == NULL, it will load all the constants.
77 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
78 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
80 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, const WineD3D_GL_Info *gl_info,
81 GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
83 DWORD shader_version = This->baseShader.reg_maps.shader_version;
84 local_constant* lconst;
88 if (TRACE_ON(d3d_shader)) {
89 for(i = 0; i < max_constants; i++) {
90 if(!dirty_consts[i]) continue;
91 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
92 constants[i * 4 + 0], constants[i * 4 + 1],
93 constants[i * 4 + 2], constants[i * 4 + 3]);
96 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
97 if (target_type == GL_FRAGMENT_PROGRAM_ARB && WINED3DSHADER_VERSION_MAJOR(shader_version) == 1)
100 for(i = 0; i < max_constants; i++) {
101 if(!dirty_consts[i]) continue;
105 if(constants[j + 0] > 1.0) lcl_const[0] = 1.0;
106 else if(constants[j + 0] < -1.0) lcl_const[0] = -1.0;
107 else lcl_const[0] = constants[j + 0];
109 if(constants[j + 1] > 1.0) lcl_const[1] = 1.0;
110 else if(constants[j + 1] < -1.0) lcl_const[1] = -1.0;
111 else lcl_const[1] = constants[j + 1];
113 if(constants[j + 2] > 1.0) lcl_const[2] = 1.0;
114 else if(constants[j + 2] < -1.0) lcl_const[2] = -1.0;
115 else lcl_const[2] = constants[j + 2];
117 if(constants[j + 3] > 1.0) lcl_const[3] = 1.0;
118 else if(constants[j + 3] < -1.0) lcl_const[3] = -1.0;
119 else lcl_const[3] = constants[j + 3];
121 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
124 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
125 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
126 * or just reloading *all* constants at once
128 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
130 for(i = 0; i < max_constants; i++) {
131 if(!dirty_consts[i]) continue;
133 /* Find the next block of dirty constants */
136 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
140 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
143 for(i = 0; i < max_constants; i++) {
144 if(dirty_consts[i]) {
146 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
151 checkGLcall("glProgramEnvParameter4fvARB()");
153 /* Load immediate constants */
154 if(This->baseShader.load_local_constsF) {
155 if (TRACE_ON(d3d_shader)) {
156 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
157 GLfloat* values = (GLfloat*)lconst->value;
158 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
159 values[0], values[1], values[2], values[3]);
162 /* Immediate constants are clamped for 1.X shaders at loading times */
164 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
165 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
166 ret = max(ret, lconst->idx + 1);
167 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
169 checkGLcall("glProgramEnvParameter4fvARB()");
170 return ret; /* The loaded immediate constants need reloading for the next shader */
172 return 0; /* No constants are dirty now */
177 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
179 static void shader_arb_load_np2fixup_constants(
180 IWineD3DDevice* device,
182 char useVertexShader) {
183 /* not implemented */
187 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
189 * We only support float constants in ARB at the moment, so don't
190 * worry about the Integers or Booleans
192 static void shader_arb_load_constants(
193 IWineD3DDevice* device,
195 char useVertexShader) {
197 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
198 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
199 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
202 if (useVertexShader) {
203 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
205 /* Load DirectX 9 float constants for vertex shader */
206 deviceImpl->highest_dirty_vs_const = shader_arb_load_constantsF(
207 vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
208 deviceImpl->highest_dirty_vs_const,
209 stateBlock->vertexShaderConstantF,
210 deviceImpl->activeContext->vshader_const_dirty);
212 /* Upload the position fixup */
213 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
216 if (usePixelShader) {
218 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
219 IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
221 /* Load DirectX 9 float constants for pixel shader */
222 deviceImpl->highest_dirty_ps_const = shader_arb_load_constantsF(
223 pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
224 deviceImpl->highest_dirty_ps_const,
225 stateBlock->pixelShaderConstantF,
226 deviceImpl->activeContext->pshader_const_dirty);
228 for(i = 0; i < psi->numbumpenvmatconsts; i++) {
229 /* The state manager takes care that this function is always called if the bump env matrix changes
231 const float *data = (const float *)&stateBlock->textureState[(int) psi->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
232 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst[i].const_num, data));
233 deviceImpl->activeContext->pshader_const_dirty[psi->bumpenvmatconst[i].const_num] = 1;
235 if (psi->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED)
237 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
238 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
239 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
240 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
242 const float *scale = (const float *)&stateBlock->textureState[(int) psi->luminanceconst[i].texunit][WINED3DTSS_BUMPENVLSCALE];
243 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->luminanceconst[i].const_num, scale));
244 deviceImpl->activeContext->pshader_const_dirty[psi->luminanceconst[i].const_num] = 1;
250 static void shader_arb_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
252 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
254 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
255 * context. On a context switch the old context will be fully dirtified */
256 memset(This->activeContext->vshader_const_dirty + start, 1,
257 sizeof(*This->activeContext->vshader_const_dirty) * count);
258 This->highest_dirty_vs_const = max(This->highest_dirty_vs_const, start + count + 1);
261 static void shader_arb_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
263 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
265 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
266 * context. On a context switch the old context will be fully dirtified */
267 memset(This->activeContext->pshader_const_dirty + start, 1,
268 sizeof(*This->activeContext->pshader_const_dirty) * count);
269 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, start + count + 1);
272 /* Generate the variable & register declarations for the ARB_vertex_program output target */
273 static void shader_generate_arb_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
274 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info)
276 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
277 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
279 char pshader = shader_is_pshader_version(reg_maps->shader_version);
280 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
281 (pshader ? GL_LIMITS(pshader_constantsF) - ARB_SHADER_RESERVED_PS_CONSTS :
282 GL_LIMITS(vshader_constantsF) - ARB_SHADER_RESERVED_VS_CONSTS));
283 UINT extra_constants_needed = 0;
284 const local_constant *lconst;
286 /* Temporary Output register */
287 shader_addline(buffer, "TEMP TMP_OUT;\n");
289 for(i = 0; i < This->baseShader.limits.temporary; i++) {
290 if (reg_maps->temporary[i])
291 shader_addline(buffer, "TEMP R%u;\n", i);
294 for (i = 0; i < This->baseShader.limits.address; i++) {
295 if (reg_maps->address[i])
296 shader_addline(buffer, "ADDRESS A%d;\n", i);
299 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
300 if (reg_maps->texcoord[i])
301 shader_addline(buffer,"TEMP T%u;\n", i);
304 /* Texture coordinate registers must be pre-loaded */
305 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
306 if (reg_maps->texcoord[i])
307 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
310 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
311 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
312 if(!reg_maps->bumpmat[i]) continue;
314 cur = ps->numbumpenvmatconsts;
315 ps->bumpenvmatconst[cur].const_num = -1;
316 ps->bumpenvmatconst[cur].texunit = i;
317 ps->luminanceconst[cur].const_num = -1;
318 ps->luminanceconst[cur].texunit = i;
320 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
321 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
324 if(max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF) - ARB_SHADER_RESERVED_PS_CONSTS) {
325 ps->bumpenvmatconst[cur].const_num = max_constantsF + extra_constants_needed;
326 shader_addline(buffer, "PARAM bumpenvmat%d = program.env[%d];\n",
327 i, ps->bumpenvmatconst[cur].const_num);
328 extra_constants_needed++;
330 if(reg_maps->luminanceparams && max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF) - ARB_SHADER_RESERVED_PS_CONSTS) {
331 ((IWineD3DPixelShaderImpl *)This)->luminanceconst[cur].const_num = max_constantsF + extra_constants_needed;
332 shader_addline(buffer, "PARAM luminance%d = program.env[%d];\n",
333 i, ps->luminanceconst[cur].const_num);
334 extra_constants_needed++;
335 } else if(reg_maps->luminanceparams) {
336 FIXME("No free constant to load the luminance parameters\n");
339 FIXME("No free constant found to load environment bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
342 ps->numbumpenvmatconsts = cur + 1;
345 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] && pshader) {
346 shader_addline(buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
347 srgb_mul_low, srgb_mul_low, srgb_mul_low);
348 shader_addline(buffer, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
349 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
350 shader_addline(buffer, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
351 srgb_pow, srgb_pow, srgb_pow);
352 shader_addline(buffer, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
353 srgb_mul_high, srgb_mul_high, srgb_mul_high);
354 shader_addline(buffer, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
355 srgb_sub_high, srgb_sub_high, srgb_sub_high);
358 /* Load local constants using the program-local space,
359 * this avoids reloading them each time the shader is used
361 if(!This->baseShader.load_local_constsF) {
362 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
363 shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
368 /* we use the array-based constants array if the local constants are marked for loading,
369 * because then we use indirect addressing, or when the local constant list is empty,
370 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
371 * local constants do not declare the loaded constants as an array because ARB compilers usually
372 * do not optimize unused constants away
374 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
375 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
376 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
377 max_constantsF, max_constantsF - 1);
379 for(i = 0; i < max_constantsF; i++) {
380 if(!shader_constant_is_local(This, i)) {
381 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
387 static const char * const shift_tab[] = {
388 "dummy", /* 0 (none) */
389 "coefmul.x", /* 1 (x2) */
390 "coefmul.y", /* 2 (x4) */
391 "coefmul.z", /* 3 (x8) */
392 "coefmul.w", /* 4 (x16) */
393 "dummy", /* 5 (x32) */
394 "dummy", /* 6 (x64) */
395 "dummy", /* 7 (x128) */
396 "dummy", /* 8 (d256) */
397 "dummy", /* 9 (d128) */
398 "dummy", /* 10 (d64) */
399 "dummy", /* 11 (d32) */
400 "coefdiv.w", /* 12 (d16) */
401 "coefdiv.z", /* 13 (d8) */
402 "coefdiv.y", /* 14 (d4) */
403 "coefdiv.x" /* 15 (d2) */
406 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction *ins,
407 const struct wined3d_shader_dst_param *dst, char *write_mask)
409 char *ptr = write_mask;
410 char vshader = shader_is_vshader_version(ins->reg_maps->shader_version);
412 if (vshader && dst->register_type == WINED3DSPR_ADDR)
417 else if (dst->write_mask != WINED3DSP_WRITEMASK_ALL)
420 if (dst->write_mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
421 if (dst->write_mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
422 if (dst->write_mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
423 if (dst->write_mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
429 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
430 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
431 * but addressed as "rgba". To fix this we need to swap the register's x
432 * and z components. */
433 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
434 char *ptr = swizzle_str;
436 /* swizzle bits fields: wwzzyyxx */
437 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
438 DWORD swizzle_x = swizzle & 0x03;
439 DWORD swizzle_y = (swizzle >> 2) & 0x03;
440 DWORD swizzle_z = (swizzle >> 4) & 0x03;
441 DWORD swizzle_w = (swizzle >> 6) & 0x03;
443 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
444 * generate a swizzle string. Unless we need to our own swizzling. */
445 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
447 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
448 *ptr++ = swizzle_chars[swizzle_x];
450 *ptr++ = swizzle_chars[swizzle_x];
451 *ptr++ = swizzle_chars[swizzle_y];
452 *ptr++ = swizzle_chars[swizzle_z];
453 *ptr++ = swizzle_chars[swizzle_w];
460 static void shader_arb_get_register_name(IWineD3DBaseShader *iface, WINED3DSHADER_PARAM_REGISTER_TYPE register_type,
461 UINT register_idx, BOOL rel_addr, char *register_name, BOOL *is_color)
463 /* oPos, oFog and oPts in D3D */
464 static const char * const rastout_reg_names[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
465 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)iface;
466 DWORD shader_version = This->baseShader.reg_maps.shader_version;
467 BOOL pshader = shader_is_pshader_version(shader_version);
471 switch (register_type)
473 case WINED3DSPR_TEMP:
474 sprintf(register_name, "R%u", register_idx);
477 case WINED3DSPR_INPUT:
480 if (register_idx == 0) strcpy(register_name, "fragment.color.primary");
481 else strcpy(register_name, "fragment.color.secondary");
485 if (((IWineD3DVertexShaderImpl *)This)->cur_args->swizzle_map & (1 << register_idx)) *is_color = TRUE;
486 sprintf(register_name, "vertex.attrib[%u]", register_idx);
490 case WINED3DSPR_CONST:
491 if (!pshader && rel_addr)
493 UINT rel_offset = ((IWineD3DVertexShaderImpl *)This)->rel_offset;
494 if (register_idx >= rel_offset)
495 sprintf(register_name, "C[A0.x + %u]", register_idx - rel_offset);
497 sprintf(register_name, "C[A0.x - %u]", -register_idx + rel_offset);
501 if (This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF))
502 sprintf(register_name, "C[%u]", register_idx);
504 sprintf(register_name, "C%u", register_idx);
508 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
509 if (pshader) sprintf(register_name, "T%u", register_idx);
510 else sprintf(register_name, "A%u", register_idx);
513 case WINED3DSPR_COLOROUT:
514 if (register_idx == 0)
516 strcpy(register_name, "TMP_COLOR");
520 /* TODO: See GL_ARB_draw_buffers */
521 FIXME("Unsupported write to render target %u\n", register_idx);
522 sprintf(register_name, "unsupported_register");
526 case WINED3DSPR_RASTOUT:
527 sprintf(register_name, "%s", rastout_reg_names[register_idx]);
530 case WINED3DSPR_DEPTHOUT:
531 strcpy(register_name, "result.depth");
534 case WINED3DSPR_ATTROUT:
535 if (pshader) sprintf(register_name, "oD[%u]", register_idx);
536 else if (register_idx == 0) strcpy(register_name, "result.color.primary");
537 else strcpy(register_name, "result.color.secondary");
540 case WINED3DSPR_TEXCRDOUT:
541 if (pshader) sprintf(register_name, "oT[%u]", register_idx);
542 else sprintf(register_name, "result.texcoord[%u]", register_idx);
546 FIXME("Unhandled register type %#x[%u]\n", register_type, register_idx);
547 sprintf(register_name, "unrecognized_register[%u]", register_idx);
552 static void shader_arb_add_src_param(const struct wined3d_shader_instruction *ins,
553 DWORD param, char *str)
555 char register_name[255];
559 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) strcat(str, " -");
560 else strcat(str, " ");
562 shader_arb_get_register_name(ins->shader, shader_get_regtype(param), param & WINED3DSP_REGNUM_MASK,
563 param & WINED3DSHADER_ADDRMODE_RELATIVE, register_name, &is_color);
564 strcat(str, register_name);
566 shader_arb_get_swizzle(param, is_color, swizzle);
567 strcat(str, swizzle);
570 static void shader_arb_add_dst_param(const struct wined3d_shader_instruction *ins,
571 const struct wined3d_shader_dst_param *wined3d_dst, char *str)
573 char register_name[255];
579 shader_arb_get_register_name(ins->shader, wined3d_dst->register_type,
580 wined3d_dst->register_idx, wined3d_dst->has_rel_addr, register_name, &is_color);
581 strcat(str, register_name);
583 shader_arb_get_write_mask(ins, wined3d_dst, write_mask);
584 strcat(str, write_mask);
587 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
589 switch(channel_source)
591 case CHANNEL_SOURCE_ZERO: return "0";
592 case CHANNEL_SOURCE_ONE: return "1";
593 case CHANNEL_SOURCE_X: return "x";
594 case CHANNEL_SOURCE_Y: return "y";
595 case CHANNEL_SOURCE_Z: return "z";
596 case CHANNEL_SOURCE_W: return "w";
598 FIXME("Unhandled channel source %#x\n", channel_source);
603 static void gen_color_correction(SHADER_BUFFER *buffer, const char *reg, DWORD dst_mask,
604 const char *one, const char *two, struct color_fixup_desc fixup)
608 if (is_yuv_fixup(fixup))
610 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
611 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
616 if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
617 if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
618 if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
619 if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
624 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
625 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
626 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
630 if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
631 if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
632 if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
633 if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
639 char *ptr = reg_mask;
641 if (mask != WINED3DSP_WRITEMASK_ALL)
644 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
645 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
646 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
647 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
651 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
655 static void shader_hw_sample(const struct wined3d_shader_instruction *ins, DWORD sampler_idx,
656 const char *dst_str, const char *coord_reg, BOOL projected, BOOL bias)
658 SHADER_BUFFER *buffer = ins->buffer;
659 DWORD sampler_type = ins->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
660 const char *tex_type;
661 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->shader;
662 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
664 switch(sampler_type) {
670 if(device->stateBlock->textures[sampler_idx] &&
671 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
676 if(shader_is_pshader_version(ins->reg_maps->shader_version)) {
677 const IWineD3DPixelShaderImpl* const ps = (const IWineD3DPixelShaderImpl*)This;
678 if(ps->cur_args->np2_fixup & (1 << sampler_idx)) {
679 FIXME("NP2 texcoord fixup is currently not implemented in ARB mode (use GLSL instead).\n");
684 case WINED3DSTT_VOLUME:
688 case WINED3DSTT_CUBE:
693 ERR("Unexpected texture type %d\n", sampler_type);
698 /* Shouldn't be possible, but let's check for it */
699 if(projected) FIXME("Biased and Projected texture sampling\n");
700 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
701 shader_addline(buffer, "TXB %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
702 } else if (projected) {
703 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
705 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
708 if (shader_is_pshader_version(ins->reg_maps->shader_version))
710 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)ins->shader;
711 gen_color_correction(buffer, dst_str, ins->dst[0].write_mask,
712 "one", "coefmul.x", ps->cur_args->color_fixup[sampler_idx]);
716 static void pshader_gen_input_modifier_line (
717 IWineD3DBaseShader *iface,
718 SHADER_BUFFER* buffer,
723 /* Generate a line that does the input modifier computation and return the input register to use */
724 BOOL is_color = FALSE;
729 /* Assume a new line will be added */
732 /* Get register name */
733 shader_arb_get_register_name(iface, shader_get_regtype(instr), instr & WINED3DSP_REGNUM_MASK,
734 instr & WINED3DSHADER_ADDRMODE_RELATIVE, regstr, &is_color);
735 shader_arb_get_swizzle(instr, is_color, swzstr);
737 switch (instr & WINED3DSP_SRCMOD_MASK) {
738 case WINED3DSPSM_NONE:
739 sprintf(outregstr, "%s%s", regstr, swzstr);
742 case WINED3DSPSM_NEG:
743 sprintf(outregstr, "-%s%s", regstr, swzstr);
746 case WINED3DSPSM_BIAS:
747 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
749 case WINED3DSPSM_BIASNEG:
750 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
752 case WINED3DSPSM_SIGN:
753 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
755 case WINED3DSPSM_SIGNNEG:
756 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
758 case WINED3DSPSM_COMP:
759 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
762 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
764 case WINED3DSPSM_X2NEG:
765 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
768 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
769 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
772 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
773 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
776 sprintf(outregstr, "%s%s", regstr, swzstr);
780 /* Return modified or original register, with swizzle */
782 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
785 static inline void pshader_gen_output_modifier_line(SHADER_BUFFER *buffer, int saturate, const char *write_mask,
786 int shift, const char *regstr)
788 /* Generate a line that does the output modifier computation */
789 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
790 regstr, write_mask, regstr, shift_tab[shift]);
793 static void pshader_hw_bem(const struct wined3d_shader_instruction *ins)
795 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
796 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
797 SHADER_BUFFER *buffer = ins->buffer;
799 char src_name[2][50];
801 DWORD sampler_code = dst->register_idx;
802 BOOL has_bumpmat = FALSE;
806 for(i = 0; i < This->numbumpenvmatconsts; i++) {
807 if (This->bumpenvmatconst[i].const_num != WINED3D_CONST_NUM_UNUSED
808 && This->bumpenvmatconst[i].texunit == sampler_code)
815 shader_arb_get_register_name(ins->shader, dst->register_type,
816 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
817 shader_arb_get_write_mask(ins, dst, dst_wmask);
818 strcat(dst_name, dst_wmask);
820 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_name[0]);
821 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[1], 1, src_name[1]);
824 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
825 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
826 shader_addline(buffer, "DP3 TMP.r, TMP2, %s;\n", src_name[1]);
827 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
828 shader_addline(buffer, "DP3 TMP.g, TMP2, %s;\n", src_name[1]);
830 shader_addline(buffer, "ADD %s, %s, TMP;\n", dst_name, src_name[0]);
832 shader_addline(buffer, "MOV %s, %s;\n", dst_name, src_name[0]);
836 static void pshader_hw_cnd(const struct wined3d_shader_instruction *ins)
838 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
839 SHADER_BUFFER *buffer = ins->buffer;
842 char src_name[3][50];
843 BOOL sat = dst->modifiers & WINED3DSPDM_SATURATE;
844 DWORD shift = dst->shift;
847 /* FIXME: support output modifiers */
849 /* Handle output register */
850 shader_arb_get_register_name(ins->shader, dst->register_type,
851 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
852 shader_arb_get_write_mask(ins, dst, dst_wmask);
854 /* Generate input register names (with modifiers) */
855 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_name[0]);
856 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[1], 1, src_name[1]);
857 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[2], 2, src_name[2]);
859 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
860 if (ins->reg_maps->shader_version <= WINED3DPS_VERSION(1, 3) && ins->coissue)
862 shader_addline(buffer, "MOV%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1]);
864 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
865 shader_addline(buffer, "CMP%s %s%s, TMP, %s, %s;\n",
866 sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1], src_name[2]);
869 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
872 static void pshader_hw_cmp(const struct wined3d_shader_instruction *ins)
874 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
875 SHADER_BUFFER *buffer = ins->buffer;
878 char src_name[3][50];
879 DWORD shift = dst->shift;
880 BOOL sat = dst->modifiers & WINED3DSPDM_SATURATE;
883 /* FIXME: support output modifiers */
885 /* Handle output register */
886 shader_arb_get_register_name(ins->shader, dst->register_type,
887 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
888 shader_arb_get_write_mask(ins, dst, dst_wmask);
890 /* Generate input register names (with modifiers) */
891 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_name[0]);
892 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[1], 1, src_name[1]);
893 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[2], 2, src_name[2]);
895 shader_addline(buffer, "CMP%s %s%s, %s, %s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
896 src_name[0], src_name[2], src_name[1]);
899 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
902 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
903 * dst = dot2(src0, src1) + src2 */
904 static void pshader_hw_dp2add(const struct wined3d_shader_instruction *ins)
906 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
907 SHADER_BUFFER *buffer = ins->buffer;
910 char src_name[3][50];
911 DWORD shift = dst->shift;
912 BOOL sat = dst->modifiers & WINED3DSPDM_SATURATE;
915 shader_arb_get_register_name(ins->shader, dst->register_type,
916 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
917 shader_arb_get_write_mask(ins, dst, dst_wmask);
919 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_name[0]);
920 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[1], 1, src_name[1]);
921 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[2], 2, src_name[2]);
923 /* Emulate a DP2 with a DP3 and 0.0 */
924 shader_addline(buffer, "MOV TMP, %s;\n", src_name[0]);
925 shader_addline(buffer, "MOV TMP.z, 0.0;\n");
926 shader_addline(buffer, "DP3 TMP2, TMP, %s;\n", src_name[1]);
927 shader_addline(buffer, "ADD%s %s%s, TMP2, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[2]);
930 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
933 /* Map the opcode 1-to-1 to the GL code */
934 static void shader_hw_map2gl(const struct wined3d_shader_instruction *ins)
936 SHADER_BUFFER *buffer = ins->buffer;
937 const DWORD *src = ins->src;
938 const char *instruction;
942 switch (ins->handler_idx)
944 case WINED3DSIH_ABS: instruction = "ABS"; break;
945 case WINED3DSIH_ADD: instruction = "ADD"; break;
946 case WINED3DSIH_CRS: instruction = "XPD"; break;
947 case WINED3DSIH_DP3: instruction = "DP3"; break;
948 case WINED3DSIH_DP4: instruction = "DP4"; break;
949 case WINED3DSIH_DST: instruction = "DST"; break;
950 case WINED3DSIH_EXP: instruction = "EX2"; break;
951 case WINED3DSIH_EXPP: instruction = "EXP"; break;
952 case WINED3DSIH_FRC: instruction = "FRC"; break;
953 case WINED3DSIH_LIT: instruction = "LIT"; break;
954 case WINED3DSIH_LOG: instruction = "LG2"; break;
955 case WINED3DSIH_LOGP: instruction = "LOG"; break;
956 case WINED3DSIH_LRP: instruction = "LRP"; break;
957 case WINED3DSIH_MAD: instruction = "MAD"; break;
958 case WINED3DSIH_MAX: instruction = "MAX"; break;
959 case WINED3DSIH_MIN: instruction = "MIN"; break;
960 case WINED3DSIH_MOV: instruction = "MOV"; break;
961 case WINED3DSIH_MUL: instruction = "MUL"; break;
962 case WINED3DSIH_NOP: instruction = "NOP"; break;
963 case WINED3DSIH_POW: instruction = "POW"; break;
964 case WINED3DSIH_SGE: instruction = "SGE"; break;
965 case WINED3DSIH_SLT: instruction = "SLT"; break;
966 case WINED3DSIH_SUB: instruction = "SUB"; break;
967 default: instruction = "";
968 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
972 if (shader_is_pshader_version(ins->reg_maps->shader_version))
974 /* Output token related */
975 const struct wined3d_shader_dst_param *dst;
976 char output_rname[256];
977 char output_wmask[20];
978 char operands[4][100];
979 BOOL saturate = FALSE;
980 BOOL centroid = FALSE;
981 BOOL partialprecision = FALSE;
982 const char *modifier;
986 if (!(ins->dst_count + ins->src_count))
988 ERR("Opcode \"%#x\" has no parameters\n", ins->handler_idx);
993 /* Process modifiers */
996 DWORD mask = dst->modifiers;
998 saturate = mask & WINED3DSPDM_SATURATE;
999 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
1000 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
1001 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
1003 FIXME("Unrecognized modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
1006 FIXME("Unhandled modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
1009 modifier = (saturate && !shift) ? "_SAT" : "";
1011 /* Generate input register names (with modifiers) */
1012 for (i = 0; i < ins->src_count; ++i)
1014 pshader_gen_input_modifier_line(ins->shader, buffer, src[i], i, operands[i + 1]);
1017 /* Handle output register */
1018 shader_arb_get_register_name(ins->shader, dst->register_type,
1019 dst->register_idx, dst->has_rel_addr, output_rname, &is_color);
1020 strcpy(operands[0], output_rname);
1021 shader_arb_get_write_mask(ins, dst, output_wmask);
1022 strcat(operands[0], output_wmask);
1024 arguments[0] = '\0';
1025 strcat(arguments, operands[0]);
1026 for (i = 0; i < ins->src_count; ++i)
1028 strcat(arguments, ", ");
1029 strcat(arguments, operands[i + 1]);
1031 shader_addline(buffer, "%s%s %s;\n", instruction, modifier, arguments);
1033 /* A shift requires another line. */
1034 if (shift) pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
1036 /* Note that shader_arb_add_*_param() adds spaces. */
1038 arguments[0] = '\0';
1041 shader_arb_add_dst_param(ins, &ins->dst[0], arguments);
1042 for (i = 0; i < ins->src_count; ++i)
1044 strcat(arguments, ",");
1045 shader_arb_add_src_param(ins, src[i], arguments);
1048 shader_addline(buffer, "%s%s;\n", instruction, arguments);
1052 static void shader_hw_mov(const struct wined3d_shader_instruction *ins)
1054 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->shader;
1056 if ((WINED3DSHADER_VERSION_MAJOR(ins->reg_maps->shader_version) == 1
1057 && !shader_is_pshader_version(ins->reg_maps->shader_version)
1058 && ins->dst[0].register_type == WINED3DSPR_ADDR)
1059 || ins->handler_idx == WINED3DSIH_MOVA)
1061 SHADER_BUFFER *buffer = ins->buffer;
1062 char src0_param[256];
1064 if (ins->handler_idx == WINED3DSIH_MOVA)
1065 FIXME("mova should round\n");
1067 src0_param[0] = '\0';
1068 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1070 shader_arb_add_src_param(ins, ins->src[0], src0_param);
1071 shader_addline(buffer, "ADD TMP.x, %s, helper_const.z;\n", src0_param);
1072 shader_addline(buffer, "ARL A0.x, TMP.x;\n");
1076 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1077 * with more than one component. Thus replicate the first source argument over all
1078 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc)
1080 DWORD parm = ins->src[0] & ~(WINED3DVS_SWIZZLE_MASK);
1081 if((ins->src[0] & WINED3DVS_X_W) == WINED3DVS_X_W)
1082 parm |= WINED3DVS_X_W | WINED3DVS_Y_W | WINED3DVS_Z_W | WINED3DVS_W_W;
1083 else if((ins->src[0] & WINED3DVS_X_Z) == WINED3DVS_X_Z)
1084 parm |= WINED3DVS_X_Z | WINED3DVS_Y_Z | WINED3DVS_Z_Z | WINED3DVS_W_Z;
1085 else if((ins->src[0] & WINED3DVS_X_Y) == WINED3DVS_X_Y)
1086 parm |= WINED3DVS_X_Y | WINED3DVS_Y_Y | WINED3DVS_Z_Y | WINED3DVS_W_Y;
1087 else if((ins->src[0] & WINED3DVS_X_X) == WINED3DVS_X_X)
1088 parm |= WINED3DVS_X_X | WINED3DVS_Y_X | WINED3DVS_Z_X | WINED3DVS_W_X;
1089 shader_arb_add_src_param(ins, parm, src0_param);
1090 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1095 shader_hw_map2gl(ins);
1099 static void pshader_hw_texkill(const struct wined3d_shader_instruction *ins)
1101 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1102 DWORD shader_version = ins->reg_maps->shader_version;
1103 SHADER_BUFFER *buffer = ins->buffer;
1107 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1108 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1110 shader_arb_get_register_name(ins->shader, dst->register_type,
1111 dst->register_idx, dst->has_rel_addr, reg_dest, &is_color);
1113 if (shader_version >= WINED3DPS_VERSION(2,0))
1115 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1116 shader_addline(buffer, "KIL %s;\n", reg_dest);
1118 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1119 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1121 shader_addline(buffer, "MOV TMP, %s;\n", reg_dest);
1122 shader_addline(buffer, "MOV TMP.w, one.w;\n");
1123 shader_addline(buffer, "KIL TMP;\n");
1127 static void pshader_hw_tex(const struct wined3d_shader_instruction *ins)
1129 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1130 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1131 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1134 const DWORD *src = ins->src;
1135 SHADER_BUFFER* buffer = ins->buffer;
1136 DWORD shader_version = ins->reg_maps->shader_version;
1137 BOOL projected = FALSE, bias = FALSE;
1141 DWORD reg_sampler_code;
1143 /* All versions have a destination register */
1144 shader_arb_get_register_name(ins->shader, dst->register_type,
1145 dst->register_idx, dst->has_rel_addr, reg_dest, &is_color);
1147 /* 1.0-1.3: Use destination register as coordinate source.
1148 1.4+: Use provided coordinate source register. */
1149 if (shader_version < WINED3DPS_VERSION(1,4))
1150 strcpy(reg_coord, reg_dest);
1152 pshader_gen_input_modifier_line(ins->shader, buffer, src[0], 0, reg_coord);
1154 /* 1.0-1.4: Use destination register number as texture code.
1155 2.0+: Use provided sampler number as texure code. */
1156 if (shader_version < WINED3DPS_VERSION(2,0))
1157 reg_sampler_code = dst->register_idx;
1159 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
1162 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1163 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1164 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1166 if (shader_version < WINED3DPS_VERSION(1,4))
1169 if(reg_sampler_code < MAX_TEXTURES) {
1170 flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1172 if (flags & WINED3DTTFF_PROJECTED) {
1176 else if (shader_version < WINED3DPS_VERSION(2,0))
1178 DWORD src_mod = ins->src[0] & WINED3DSP_SRCMOD_MASK;
1179 if (src_mod == WINED3DSPSM_DZ) {
1181 } else if(src_mod == WINED3DSPSM_DW) {
1185 if (ins->flags & WINED3DSI_TEXLD_PROJECT) projected = TRUE;
1186 if (ins->flags & WINED3DSI_TEXLD_BIAS) bias = TRUE;
1188 shader_hw_sample(ins, reg_sampler_code, reg_dest, reg_coord, projected, bias);
1191 static void pshader_hw_texcoord(const struct wined3d_shader_instruction *ins)
1193 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1194 SHADER_BUFFER *buffer = ins->buffer;
1197 shader_arb_get_write_mask(ins, dst, tmp);
1198 if (ins->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
1200 DWORD reg = dst->register_idx;
1201 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
1205 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, reg_src);
1206 shader_addline(buffer, "MOV R%u%s, %s;\n", dst->register_idx, tmp, reg_src);
1210 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction *ins)
1212 SHADER_BUFFER *buffer = ins->buffer;
1213 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1214 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1217 DWORD reg1 = ins->dst[0].register_idx;
1221 sprintf(dst_str, "T%u", reg1);
1222 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_str);
1223 shader_addline(buffer, "MOV TMP.x, %s.w;\n", src_str);
1224 shader_addline(buffer, "MOV TMP.y, %s.x;\n", src_str);
1225 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1226 shader_hw_sample(ins, reg1, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1229 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction *ins)
1231 SHADER_BUFFER *buffer = ins->buffer;
1233 DWORD reg1 = ins->dst[0].register_idx;
1237 sprintf(dst_str, "T%u", reg1);
1238 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_str);
1239 shader_addline(buffer, "MOV TMP.x, %s.y;\n", src_str);
1240 shader_addline(buffer, "MOV TMP.y, %s.z;\n", src_str);
1241 shader_hw_sample(ins, reg1, dst_str, "TMP", FALSE, FALSE);
1244 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction *ins)
1246 SHADER_BUFFER *buffer = ins->buffer;
1247 DWORD reg1 = ins->dst[0].register_idx;
1251 sprintf(dst_str, "T%u", reg1);
1252 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_str);
1253 shader_hw_sample(ins, reg1, dst_str, src_str, FALSE, FALSE);
1256 static void pshader_hw_texbem(const struct wined3d_shader_instruction *ins)
1258 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1259 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1260 BOOL has_bumpmat = FALSE;
1261 BOOL has_luminance = FALSE;
1265 DWORD src = ins->src[0] & WINED3DSP_REGNUM_MASK;
1266 SHADER_BUFFER* buffer = ins->buffer;
1269 DWORD reg_dest_code;
1271 /* All versions have a destination register */
1272 reg_dest_code = dst->register_idx;
1273 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
1274 shader_arb_get_register_name(ins->shader, dst->register_type,
1275 dst->register_idx, dst->has_rel_addr, reg_coord, &is_color);
1277 for(i = 0; i < This->numbumpenvmatconsts; i++) {
1278 if (This->bumpenvmatconst[i].const_num != WINED3D_CONST_NUM_UNUSED
1279 && reg_dest_code == This->bumpenvmatconst[i].texunit)
1285 for(i = 0; i < This->numbumpenvmatconsts; i++) {
1286 if (This->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED
1287 && reg_dest_code == This->luminanceconst[i].texunit)
1289 has_luminance = TRUE;
1295 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
1297 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1298 shader_addline(buffer, "DP3 TMP.x, TMP2, T%u;\n", src);
1299 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1300 shader_addline(buffer, "DP3 TMP.y, TMP2, T%u;\n", src);
1302 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1303 * so we can't let the GL handle this.
1305 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1306 & WINED3DTTFF_PROJECTED) {
1307 shader_addline(buffer, "RCP TMP2.w, %s.w;\n", reg_coord);
1308 shader_addline(buffer, "MUL TMP2.xy, %s, TMP2.w;\n", reg_coord);
1309 shader_addline(buffer, "ADD TMP.xy, TMP, TMP2;\n");
1311 shader_addline(buffer, "ADD TMP.xy, TMP, %s;\n", reg_coord);
1314 shader_hw_sample(ins, reg_dest_code, reg_coord, "TMP", FALSE, FALSE);
1316 if (ins->handler_idx == WINED3DSIH_TEXBEML && has_luminance)
1318 shader_addline(buffer, "MAD TMP, T%u.z, luminance%d.x, luminance%d.y;\n",
1319 src, reg_dest_code, reg_dest_code);
1320 shader_addline(buffer, "MUL %s, %s, TMP;\n", reg_coord, reg_coord);
1325 if(reg_dest_code < MAX_TEXTURES) {
1326 tf = ((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1330 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
1331 shader_hw_sample(ins, reg_dest_code, reg_coord, reg_coord, tf & WINED3DTTFF_PROJECTED, FALSE);
1335 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction *ins)
1337 DWORD reg = ins->dst[0].register_idx;
1338 SHADER_BUFFER *buffer = ins->buffer;
1341 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0_name);
1342 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
1345 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction *ins)
1347 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1348 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1350 DWORD reg = ins->dst[0].register_idx;
1351 SHADER_BUFFER *buffer = ins->buffer;
1355 sprintf(dst_str, "T%u", reg);
1356 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0_name);
1357 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
1358 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1359 shader_hw_sample(ins, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1362 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction *ins)
1364 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1365 DWORD reg = ins->dst[0].register_idx;
1366 SHADER_BUFFER *buffer = ins->buffer;
1367 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1370 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0_name);
1371 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
1372 current_state->texcoord_w[current_state->current_row++] = reg;
1375 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction *ins)
1377 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1378 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1380 DWORD reg = ins->dst[0].register_idx;
1381 SHADER_BUFFER *buffer = ins->buffer;
1382 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1386 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0_name);
1387 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1389 /* Sample the texture using the calculated coordinates */
1390 sprintf(dst_str, "T%u", reg);
1391 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1392 shader_hw_sample(ins, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1393 current_state->current_row = 0;
1396 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction *ins)
1398 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1399 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1401 DWORD reg = ins->dst[0].register_idx;
1402 SHADER_BUFFER *buffer = ins->buffer;
1403 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1407 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0_name);
1408 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1410 /* Construct the eye-ray vector from w coordinates */
1411 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
1412 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
1413 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
1415 /* Calculate reflection vector
1417 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
1418 /* The .w is ignored when sampling, so I can use TMP2.w to calculate dot(N, N) */
1419 shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1420 shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1421 shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1422 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1423 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
1425 /* Sample the texture using the calculated coordinates */
1426 sprintf(dst_str, "T%u", reg);
1427 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1428 shader_hw_sample(ins, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1429 current_state->current_row = 0;
1432 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction *ins)
1434 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->shader;
1435 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1437 DWORD reg = ins->dst[0].register_idx;
1438 DWORD reg3 = ins->src[1] & WINED3DSP_REGNUM_MASK;
1439 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1440 SHADER_BUFFER *buffer = ins->buffer;
1444 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0_name);
1445 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1447 /* Calculate reflection vector.
1450 * TMP.xyz = 2 * --------- * N - E
1453 * Which normalizes the normal vector
1455 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
1456 shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1457 shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1458 shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1459 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1460 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
1462 /* Sample the texture using the calculated coordinates */
1463 sprintf(dst_str, "T%u", reg);
1464 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1465 shader_hw_sample(ins, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1466 current_state->current_row = 0;
1469 static void pshader_hw_texdepth(const struct wined3d_shader_instruction *ins)
1471 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1472 SHADER_BUFFER *buffer = ins->buffer;
1476 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1477 * which is essentially an input, is the destination register because it is the first
1478 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1481 shader_arb_get_register_name(ins->shader, dst->register_type,
1482 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
1484 /* According to the msdn, the source register(must be r5) is unusable after
1485 * the texdepth instruction, so we're free to modify it
1487 shader_addline(buffer, "MIN %s.y, %s.y, one.y;\n", dst_name, dst_name);
1489 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1490 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1491 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1493 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
1494 shader_addline(buffer, "MUL TMP.x, %s.x, %s.y;\n", dst_name, dst_name);
1495 shader_addline(buffer, "MIN TMP.x, TMP.x, one.x;\n");
1496 shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1499 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1500 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1501 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1502 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction *ins)
1504 SHADER_BUFFER *buffer = ins->buffer;
1505 DWORD sampler_idx = ins->dst[0].register_idx;
1509 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0);
1510 shader_addline(buffer, "MOV TMP, 0.0;\n");
1511 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", sampler_idx, src0);
1513 sprintf(dst_str, "T%u", sampler_idx);
1514 shader_hw_sample(ins, sampler_idx, dst_str, "TMP", FALSE /* Only one coord, can't be projected */, FALSE);
1517 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1518 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1519 static void pshader_hw_texdp3(const struct wined3d_shader_instruction *ins)
1521 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1525 SHADER_BUFFER *buffer = ins->buffer;
1528 /* Handle output register */
1529 shader_arb_get_register_name(ins->shader, dst->register_type,
1530 dst->register_idx, dst->has_rel_addr, dst_str, &is_color);
1531 shader_arb_get_write_mask(ins, dst, dst_mask);
1533 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0);
1534 shader_addline(buffer, "DP3 %s%s, T%u, %s;\n", dst_str, dst_mask, dst->register_idx, src0);
1536 /* TODO: Handle output modifiers */
1539 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1540 * Perform the 3rd row of a 3x3 matrix multiply */
1541 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction *ins)
1543 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1544 SHADER_BUFFER *buffer = ins->buffer;
1550 shader_arb_get_register_name(ins->shader, dst->register_type,
1551 dst->register_idx, dst->has_rel_addr, dst_str, &is_color);
1552 shader_arb_get_write_mask(ins, dst, dst_mask);
1554 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0);
1555 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", dst->register_idx, src0);
1556 shader_addline(buffer, "MOV %s%s, TMP;\n", dst_str, dst_mask);
1558 /* TODO: Handle output modifiers */
1561 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1562 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1563 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1564 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1566 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction *ins)
1568 SHADER_BUFFER *buffer = ins->buffer;
1569 DWORD dst_reg = ins->dst[0].register_idx;
1572 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src0);
1573 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", dst_reg, src0);
1575 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1576 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1577 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1579 shader_addline(buffer, "RCP TMP.y, TMP.y;\n");
1580 shader_addline(buffer, "MUL TMP.x, TMP.x, TMP.y;\n");
1581 shader_addline(buffer, "MIN TMP.x, TMP.x, one.x;\n");
1582 shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1585 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1586 Vertex/Pixel shaders to ARB_vertex_program codes */
1587 static void shader_hw_mnxn(const struct wined3d_shader_instruction *ins)
1590 int nComponents = 0;
1591 struct wined3d_shader_dst_param tmp_dst = {0};
1592 struct wined3d_shader_instruction tmp_ins;
1594 memset(&tmp_ins, 0, sizeof(tmp_ins));
1596 /* Set constants for the temporary argument */
1597 tmp_ins.shader = ins->shader;
1598 tmp_ins.buffer = ins->buffer;
1599 tmp_ins.src[0] = ins->src[0];
1600 tmp_ins.src_addr[0] = ins->src_addr[0];
1601 tmp_ins.src_addr[1] = ins->src_addr[1];
1602 tmp_ins.reg_maps = ins->reg_maps;
1603 tmp_ins.dst_count = 1;
1604 tmp_ins.dst = &tmp_dst;
1605 tmp_ins.src_count = 2;
1607 switch(ins->handler_idx)
1609 case WINED3DSIH_M4x4:
1611 tmp_ins.handler_idx = WINED3DSIH_DP4;
1613 case WINED3DSIH_M4x3:
1615 tmp_ins.handler_idx = WINED3DSIH_DP4;
1617 case WINED3DSIH_M3x4:
1619 tmp_ins.handler_idx = WINED3DSIH_DP3;
1621 case WINED3DSIH_M3x3:
1623 tmp_ins.handler_idx = WINED3DSIH_DP3;
1625 case WINED3DSIH_M3x2:
1627 tmp_ins.handler_idx = WINED3DSIH_DP3;
1630 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1634 tmp_dst = ins->dst[0];
1635 for (i = 0; i < nComponents; i++) {
1636 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
1637 tmp_ins.src[1] = ins->src[1]+i;
1638 shader_hw_map2gl(&tmp_ins);
1642 static void vshader_hw_rsq_rcp(const struct wined3d_shader_instruction *ins)
1644 SHADER_BUFFER *buffer = ins->buffer;
1645 DWORD src = ins->src[0];
1646 DWORD swizzle = (src & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1647 const char *instruction;
1651 switch(ins->handler_idx)
1653 case WINED3DSIH_RSQ: instruction = "RSQ"; break;
1654 case WINED3DSIH_RCP: instruction = "RCP"; break;
1655 default: instruction = "";
1656 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1660 strcpy(tmpLine, instruction);
1661 shader_arb_add_dst_param(ins, &ins->dst[0], tmpLine); /* Destination */
1662 strcat(tmpLine, ",");
1663 shader_arb_add_src_param(ins, src, tmpLine);
1664 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) == swizzle) {
1665 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1668 strcat(tmpLine, ".w");
1671 shader_addline(buffer, "%s;\n", tmpLine);
1674 static void shader_hw_nrm(const struct wined3d_shader_instruction *ins)
1676 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1677 SHADER_BUFFER *buffer = ins->buffer;
1681 DWORD shift = dst->shift;
1682 BOOL sat = dst->modifiers & WINED3DSPDM_SATURATE;
1685 shader_arb_get_register_name(ins->shader, dst->register_type,
1686 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
1687 shader_arb_get_write_mask(ins, dst, dst_wmask);
1689 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_name);
1690 shader_addline(buffer, "DP3 TMP, %s, %s;\n", src_name, src_name);
1691 shader_addline(buffer, "RSQ TMP, TMP.x;\n");
1692 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1693 shader_addline(buffer, "MUL%s %s%s, %s, TMP;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1697 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1700 static void shader_hw_sincos(const struct wined3d_shader_instruction *ins)
1702 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1703 * must contain fixed constants. So we need a separate function to filter those constants and
1706 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1707 SHADER_BUFFER *buffer = ins->buffer;
1711 DWORD shift = dst->shift;
1712 BOOL sat = dst->modifiers & WINED3DSPDM_SATURATE;
1715 shader_arb_get_register_name(ins->shader, dst->register_type,
1716 dst->register_idx, dst->has_rel_addr, dst_name, &is_color);
1717 shader_arb_get_write_mask(ins, dst, dst_wmask);
1719 pshader_gen_input_modifier_line(ins->shader, buffer, ins->src[0], 0, src_name);
1720 shader_addline(buffer, "SCS%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1724 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1728 static GLuint create_arb_blt_vertex_program(const WineD3D_GL_Info *gl_info)
1730 GLuint program_id = 0;
1731 const char *blt_vprogram =
1733 "PARAM c[1] = { { 1, 0.5 } };\n"
1734 "MOV result.position, vertex.position;\n"
1735 "MOV result.color, c[0].x;\n"
1736 "MOV result.texcoord[0], vertex.texcoord[0];\n"
1739 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1740 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
1741 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
1743 if (glGetError() == GL_INVALID_OPERATION) {
1745 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1746 FIXME("Vertex program error at position %d: %s\n", pos,
1747 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1753 static GLuint create_arb_blt_fragment_program(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
1755 GLuint program_id = 0;
1756 static const char * const blt_fprograms[tex_type_count] =
1763 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1764 "MOV result.depth.z, R0.x;\n"
1771 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
1772 "MOV result.depth.z, R0.x;\n"
1777 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
1778 "MOV result.depth.z, R0.x;\n"
1782 if (!blt_fprograms[tex_type])
1784 FIXME("tex_type %#x not supported\n", tex_type);
1788 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1789 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
1790 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprograms[tex_type]), blt_fprograms[tex_type]));
1792 if (glGetError() == GL_INVALID_OPERATION) {
1794 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1795 FIXME("Fragment program error at position %d: %s\n", pos,
1796 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1802 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1803 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1804 struct shader_arb_priv *priv = This->shader_priv;
1805 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1808 struct vs_compile_args compile_args;
1810 TRACE("Using vertex shader\n");
1811 find_vs_compile_args((IWineD3DVertexShaderImpl *) This->stateBlock->vertexShader, This->stateBlock, &compile_args);
1812 priv->current_vprogram_id = find_gl_vshader((IWineD3DVertexShaderImpl *) This->stateBlock->vertexShader, &compile_args);
1814 /* Bind the vertex program */
1815 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1816 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
1818 /* Enable OpenGL vertex programs */
1819 glEnable(GL_VERTEX_PROGRAM_ARB);
1820 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1821 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1822 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1823 priv->current_vprogram_id = 0;
1824 glDisable(GL_VERTEX_PROGRAM_ARB);
1825 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1829 struct ps_compile_args compile_args;
1830 TRACE("Using pixel shader\n");
1831 find_ps_compile_args((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader, This->stateBlock, &compile_args);
1832 priv->current_fprogram_id = find_gl_pshader((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader,
1835 /* Bind the fragment program */
1836 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1837 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
1839 if(!priv->use_arbfp_fixed_func) {
1840 /* Enable OpenGL fragment programs */
1841 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1842 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1844 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1845 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && !priv->use_arbfp_fixed_func) {
1846 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
1847 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
1848 * replacement shader
1850 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1851 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1852 priv->current_fprogram_id = 0;
1856 static void shader_arb_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
1857 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1858 struct shader_arb_priv *priv = This->shader_priv;
1859 GLuint *blt_fprogram = &priv->depth_blt_fprogram_id[tex_type];
1860 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1862 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
1863 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
1864 glEnable(GL_VERTEX_PROGRAM_ARB);
1866 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type);
1867 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
1868 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1871 static void shader_arb_deselect_depth_blt(IWineD3DDevice *iface) {
1872 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1873 struct shader_arb_priv *priv = This->shader_priv;
1874 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1876 if (priv->current_vprogram_id) {
1877 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1878 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1880 glEnable(GL_VERTEX_PROGRAM_ARB);
1881 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1883 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1885 glDisable(GL_VERTEX_PROGRAM_ARB);
1886 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1889 if (priv->current_fprogram_id) {
1890 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1891 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1893 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1894 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1896 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1898 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1899 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1903 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
1904 IWineD3DBaseShaderImpl *baseShader = (IWineD3DBaseShaderImpl *) iface;
1905 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)baseShader->baseShader.device)->adapter->gl_info;
1907 if (shader_is_pshader_version(baseShader->baseShader.reg_maps.shader_version))
1909 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
1913 for(i = 0; i < This->num_gl_shaders; i++) {
1914 GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId));
1915 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1918 HeapFree(GetProcessHeap(), 0, This->gl_shaders);
1919 This->gl_shaders = NULL;
1920 This->num_gl_shaders = 0;
1921 This->shader_array_size = 0;
1923 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *) iface;
1927 for(i = 0; i < This->num_gl_shaders; i++) {
1928 GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId));
1929 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1932 HeapFree(GetProcessHeap(), 0, This->gl_shaders);
1933 This->gl_shaders = NULL;
1934 This->num_gl_shaders = 0;
1935 This->shader_array_size = 0;
1939 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
1940 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1941 This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
1945 static void shader_arb_free(IWineD3DDevice *iface) {
1946 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1947 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1948 struct shader_arb_priv *priv = This->shader_priv;
1951 if(priv->depth_blt_vprogram_id) {
1952 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
1954 for (i = 0; i < tex_type_count; ++i) {
1955 if (priv->depth_blt_fprogram_id[i]) {
1956 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id[i]));
1960 HeapFree(GetProcessHeap(), 0, This->shader_priv);
1963 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
1967 static void arbfp_add_sRGB_correction(SHADER_BUFFER *buffer, const char *fragcolor, const char *tmp1,
1968 const char *tmp2, const char *tmp3, const char *tmp4) {
1969 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
1971 /* Calculate the > 0.0031308 case */
1972 shader_addline(buffer, "POW %s.x, %s.x, srgb_pow.x;\n", tmp1, fragcolor);
1973 shader_addline(buffer, "POW %s.y, %s.y, srgb_pow.y;\n", tmp1, fragcolor);
1974 shader_addline(buffer, "POW %s.z, %s.z, srgb_pow.z;\n", tmp1, fragcolor);
1975 shader_addline(buffer, "MUL %s, %s, srgb_mul_hi;\n", tmp1, tmp1);
1976 shader_addline(buffer, "SUB %s, %s, srgb_sub_hi;\n", tmp1, tmp1);
1977 /* Calculate the < case */
1978 shader_addline(buffer, "MUL %s, srgb_mul_low, %s;\n", tmp2, fragcolor);
1979 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
1980 shader_addline(buffer, "SLT %s, srgb_comparison, %s;\n", tmp3, fragcolor);
1981 shader_addline(buffer, "SGE %s, srgb_comparison, %s;\n", tmp4, fragcolor);
1982 /* Store the components > 0.0031308 in the destination */
1983 shader_addline(buffer, "MUL %s, %s, %s;\n", fragcolor, tmp1, tmp3);
1984 /* Add the components that are < 0.0031308 */
1985 shader_addline(buffer, "MAD result.color.xyz, %s, %s, %s;\n", tmp2, tmp4, fragcolor);
1986 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
1989 static GLuint shader_arb_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1990 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1991 const shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
1992 CONST DWORD *function = This->baseShader.function;
1993 DWORD shader_version = reg_maps->shader_version;
1994 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
1995 const local_constant *lconst;
1997 const char *fragcolor;
1999 /* Create the hw ARB shader */
2000 shader_addline(buffer, "!!ARBfp1.0\n");
2002 if (shader_version < WINED3DPS_VERSION(3,0)) {
2007 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
2010 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
2013 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
2018 shader_addline(buffer, "TEMP TMP;\n"); /* Used in matrix ops */
2019 shader_addline(buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
2020 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
2021 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
2022 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
2023 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
2024 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
2025 shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
2027 if (shader_version < WINED3DPS_VERSION(2,0)) {
2030 shader_addline(buffer, "TEMP TMP_COLOR;\n");
2031 fragcolor = "TMP_COLOR";
2034 /* Base Declarations */
2035 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
2037 /* Base Shader Body */
2038 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
2040 if(args->srgb_correction) {
2041 arbfp_add_sRGB_correction(buffer, fragcolor, "TMP", "TMP2", "TA", "TB");
2043 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
2044 shader_addline(buffer, "END\n");
2046 /* TODO: change to resource.glObjectHandle or something like that */
2047 GL_EXTCALL(glGenProgramsARB(1, &retval));
2049 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
2050 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
2052 TRACE("Created hw pixel shader, prg=%d\n", retval);
2053 /* Create the program and check for errors */
2054 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2055 buffer->bsize, buffer->buffer));
2057 if (glGetError() == GL_INVALID_OPERATION) {
2059 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2060 FIXME("HW PixelShader Error at position %d: %s\n",
2061 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2065 /* Load immediate constants */
2066 if(!This->baseShader.load_local_constsF) {
2067 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2068 const float *value = (const float *)lconst->value;
2069 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst->idx, value));
2070 checkGLcall("glProgramLocalParameter4fvARB");
2077 static GLuint shader_arb_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer, const struct vs_compile_args *args) {
2078 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
2079 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
2080 CONST DWORD *function = This->baseShader.function;
2081 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
2082 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
2083 const local_constant *lconst;
2086 /* Create the hw ARB shader */
2087 shader_addline(buffer, "!!ARBvp1.0\n");
2088 shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
2090 /* Mesa supports only 95 constants */
2091 if (GL_VEND(MESA) || GL_VEND(WINE))
2092 This->baseShader.limits.constant_float =
2093 min(95, This->baseShader.limits.constant_float);
2095 shader_addline(buffer, "TEMP TMP;\n");
2097 /* Base Declarations */
2098 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
2100 /* We need a constant to fixup the final position */
2101 shader_addline(buffer, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS);
2103 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
2104 * for output parameters. D3D in theory does not do that either, but some applications depend on a
2105 * proper initialization of the secondary color, and programs using the fixed function pipeline without
2106 * a replacement shader depend on the texcoord.w being set properly.
2108 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
2109 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
2110 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
2111 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
2112 * this can eat a number of instructions, so skip it unless this cap is set as well
2114 if(!GL_SUPPORT(NV_VERTEX_PROGRAM)) {
2115 shader_addline(buffer, "MOV result.color.secondary, -helper_const.wwwy;\n");
2117 if((GLINFO_LOCATION).set_texcoord_w && !device->frag_pipe->ffp_proj_control) {
2119 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2120 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2121 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2122 shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
2128 /* Base Shader Body */
2129 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
2131 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
2132 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
2133 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
2134 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
2136 if(args->fog_src == VS_FOG_Z) {
2137 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2138 } else if (!reg_maps->fog) {
2139 shader_addline(buffer, "MOV result.fogcoord, 0.0;\n");
2142 /* Write the final position.
2144 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2145 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2146 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2147 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2149 shader_addline(buffer, "MUL TMP, posFixup, TMP_OUT.w;\n");
2150 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TMP.z;\n");
2151 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TMP.w;\n");
2153 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2154 * and the glsl equivalent
2156 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2158 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2160 shader_addline(buffer, "END\n");
2162 /* TODO: change to resource.glObjectHandle or something like that */
2163 GL_EXTCALL(glGenProgramsARB(1, &ret));
2165 TRACE("Creating a hw vertex shader, prg=%d\n", ret);
2166 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, ret));
2168 TRACE("Created hw vertex shader, prg=%d\n", ret);
2169 /* Create the program and check for errors */
2170 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2171 buffer->bsize, buffer->buffer));
2173 if (glGetError() == GL_INVALID_OPERATION) {
2175 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2176 FIXME("HW VertexShader Error at position %d: %s\n",
2177 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2180 /* Load immediate constants */
2181 if(!This->baseShader.load_local_constsF) {
2182 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2183 const float *value = (const float *)lconst->value;
2184 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst->idx, value));
2191 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
2193 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2194 * then overwrite the shader specific ones
2196 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
2198 if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
2199 pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
2200 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2201 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF) - ARB_SHADER_RESERVED_VS_CONSTS;
2204 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
2205 pCaps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
2206 pCaps->PixelShader1xMaxValue = 8.0;
2207 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2208 pCaps->MaxPixelShaderConst = GL_LIMITS(pshader_constantsF) - ARB_SHADER_RESERVED_PS_CONSTS;
2212 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
2214 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
2216 TRACE("Checking support for color_fixup:\n");
2217 dump_color_fixup_desc(fixup);
2220 /* We support everything except YUV conversions. */
2221 if (!is_yuv_fixup(fixup))
2227 TRACE("[FAILED]\n");
2231 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
2233 /* WINED3DSIH_ABS */ shader_hw_map2gl,
2234 /* WINED3DSIH_ADD */ shader_hw_map2gl,
2235 /* WINED3DSIH_BEM */ pshader_hw_bem,
2236 /* WINED3DSIH_BREAK */ NULL,
2237 /* WINED3DSIH_BREAKC */ NULL,
2238 /* WINED3DSIH_BREAKP */ NULL,
2239 /* WINED3DSIH_CALL */ NULL,
2240 /* WINED3DSIH_CALLNZ */ NULL,
2241 /* WINED3DSIH_CMP */ pshader_hw_cmp,
2242 /* WINED3DSIH_CND */ pshader_hw_cnd,
2243 /* WINED3DSIH_CRS */ shader_hw_map2gl,
2244 /* WINED3DSIH_DCL */ NULL,
2245 /* WINED3DSIH_DEF */ NULL,
2246 /* WINED3DSIH_DEFB */ NULL,
2247 /* WINED3DSIH_DEFI */ NULL,
2248 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
2249 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
2250 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
2251 /* WINED3DSIH_DST */ shader_hw_map2gl,
2252 /* WINED3DSIH_DSX */ NULL,
2253 /* WINED3DSIH_DSY */ NULL,
2254 /* WINED3DSIH_ELSE */ NULL,
2255 /* WINED3DSIH_ENDIF */ NULL,
2256 /* WINED3DSIH_ENDLOOP */ NULL,
2257 /* WINED3DSIH_ENDREP */ NULL,
2258 /* WINED3DSIH_EXP */ shader_hw_map2gl,
2259 /* WINED3DSIH_EXPP */ shader_hw_map2gl,
2260 /* WINED3DSIH_FRC */ shader_hw_map2gl,
2261 /* WINED3DSIH_IF */ NULL,
2262 /* WINED3DSIH_IFC */ NULL,
2263 /* WINED3DSIH_LABEL */ NULL,
2264 /* WINED3DSIH_LIT */ shader_hw_map2gl,
2265 /* WINED3DSIH_LOG */ shader_hw_map2gl,
2266 /* WINED3DSIH_LOGP */ shader_hw_map2gl,
2267 /* WINED3DSIH_LOOP */ NULL,
2268 /* WINED3DSIH_LRP */ shader_hw_map2gl,
2269 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
2270 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
2271 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
2272 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
2273 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
2274 /* WINED3DSIH_MAD */ shader_hw_map2gl,
2275 /* WINED3DSIH_MAX */ shader_hw_map2gl,
2276 /* WINED3DSIH_MIN */ shader_hw_map2gl,
2277 /* WINED3DSIH_MOV */ shader_hw_mov,
2278 /* WINED3DSIH_MOVA */ shader_hw_mov,
2279 /* WINED3DSIH_MUL */ shader_hw_map2gl,
2280 /* WINED3DSIH_NOP */ shader_hw_map2gl,
2281 /* WINED3DSIH_NRM */ shader_hw_nrm,
2282 /* WINED3DSIH_PHASE */ NULL,
2283 /* WINED3DSIH_POW */ shader_hw_map2gl,
2284 /* WINED3DSIH_RCP */ vshader_hw_rsq_rcp,
2285 /* WINED3DSIH_REP */ NULL,
2286 /* WINED3DSIH_RET */ NULL,
2287 /* WINED3DSIH_RSQ */ vshader_hw_rsq_rcp,
2288 /* WINED3DSIH_SETP */ NULL,
2289 /* WINED3DSIH_SGE */ shader_hw_map2gl,
2290 /* WINED3DSIH_SGN */ NULL,
2291 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
2292 /* WINED3DSIH_SLT */ shader_hw_map2gl,
2293 /* WINED3DSIH_SUB */ shader_hw_map2gl,
2294 /* WINED3DSIH_TEX */ pshader_hw_tex,
2295 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
2296 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
2297 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
2298 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
2299 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
2300 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
2301 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
2302 /* WINED3DSIH_TEXLDD */ NULL,
2303 /* WINED3DSIH_TEXLDL */ NULL,
2304 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
2305 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
2306 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
2307 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
2308 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
2309 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
2310 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
2311 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
2312 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
2313 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
2314 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
2315 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
2318 const shader_backend_t arb_program_shader_backend = {
2319 shader_arb_instruction_handler_table,
2321 shader_arb_select_depth_blt,
2322 shader_arb_deselect_depth_blt,
2323 shader_arb_update_float_vertex_constants,
2324 shader_arb_update_float_pixel_constants,
2325 shader_arb_load_constants,
2326 shader_arb_load_np2fixup_constants,
2330 shader_arb_dirty_const,
2331 shader_arb_generate_pshader,
2332 shader_arb_generate_vshader,
2333 shader_arb_get_caps,
2334 shader_arb_color_fixup_supported,
2337 /* ARB_fragment_program fixed function pipeline replacement definitions */
2338 #define ARB_FFP_CONST_TFACTOR 0
2339 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
2340 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
2341 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
2342 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
2344 struct arbfp_ffp_desc
2346 struct ffp_frag_desc parent;
2348 unsigned int num_textures_used;
2351 static void arbfp_enable(IWineD3DDevice *iface, BOOL enable) {
2353 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2354 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
2356 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2357 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
2361 static HRESULT arbfp_alloc(IWineD3DDevice *iface) {
2362 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2363 struct shader_arb_priv *priv;
2364 /* Share private data between the shader backend and the pipeline replacement, if both
2365 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
2366 * if no pixel shader is bound or not
2368 if(This->shader_backend == &arb_program_shader_backend) {
2369 This->fragment_priv = This->shader_priv;
2371 This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
2372 if(!This->fragment_priv) return E_OUTOFMEMORY;
2374 priv = This->fragment_priv;
2375 priv->fragment_shaders = hash_table_create(ffp_frag_program_key_hash, ffp_frag_program_key_compare);
2376 priv->use_arbfp_fixed_func = TRUE;
2380 static void arbfp_free_ffpshader(void *value, void *gli) {
2381 const WineD3D_GL_Info *gl_info = gli;
2382 struct arbfp_ffp_desc *entry_arb = value;
2385 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
2386 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
2387 HeapFree(GetProcessHeap(), 0, entry_arb);
2391 static void arbfp_free(IWineD3DDevice *iface) {
2392 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2393 struct shader_arb_priv *priv = This->fragment_priv;
2395 hash_table_destroy(priv->fragment_shaders, arbfp_free_ffpshader, &This->adapter->gl_info);
2396 priv->use_arbfp_fixed_func = FALSE;
2398 if(This->shader_backend != &arb_program_shader_backend) {
2399 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
2403 static void arbfp_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps)
2405 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
2406 WINED3DTEXOPCAPS_SELECTARG1 |
2407 WINED3DTEXOPCAPS_SELECTARG2 |
2408 WINED3DTEXOPCAPS_MODULATE4X |
2409 WINED3DTEXOPCAPS_MODULATE2X |
2410 WINED3DTEXOPCAPS_MODULATE |
2411 WINED3DTEXOPCAPS_ADDSIGNED2X |
2412 WINED3DTEXOPCAPS_ADDSIGNED |
2413 WINED3DTEXOPCAPS_ADD |
2414 WINED3DTEXOPCAPS_SUBTRACT |
2415 WINED3DTEXOPCAPS_ADDSMOOTH |
2416 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
2417 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
2418 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
2419 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
2420 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
2421 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
2422 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
2423 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
2424 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
2425 WINED3DTEXOPCAPS_DOTPRODUCT3 |
2426 WINED3DTEXOPCAPS_MULTIPLYADD |
2427 WINED3DTEXOPCAPS_LERP |
2428 WINED3DTEXOPCAPS_BUMPENVMAP |
2429 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
2431 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
2433 caps->MaxTextureBlendStages = 8;
2434 caps->MaxSimultaneousTextures = min(GL_LIMITS(fragment_samplers), 8);
2436 caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
2438 #undef GLINFO_LOCATION
2440 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
2441 static void state_texfactor_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2443 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2445 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2446 * application provided constants
2448 if(device->shader_backend == &arb_program_shader_backend) {
2449 if (use_ps(stateblock)) return;
2451 device = stateblock->wineD3DDevice;
2452 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
2453 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
2456 D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
2457 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
2458 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
2462 static void state_arb_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2464 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2466 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2467 * application provided constants
2469 if(device->shader_backend == &arb_program_shader_backend) {
2470 if (use_ps(stateblock)) return;
2472 device = stateblock->wineD3DDevice;
2473 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
2474 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
2477 if(stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
2478 /* The specular color has no alpha */
2479 col[0] = 1.0; col[1] = 1.0;
2480 col[2] = 1.0; col[3] = 0.0;
2482 col[0] = 0.0; col[1] = 0.0;
2483 col[2] = 0.0; col[3] = 0.0;
2485 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
2486 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
2489 static void set_bumpmat_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2490 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
2491 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2494 if (use_ps(stateblock))
2497 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.bumpmat[stage]) {
2498 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
2501 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2502 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2506 if(device->shader_backend == &arb_program_shader_backend) {
2507 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2510 } else if(device->shader_backend == &arb_program_shader_backend) {
2511 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
2512 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
2515 mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
2516 mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
2517 mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
2518 mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
2520 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
2521 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
2524 static void tex_bumpenvlum_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2525 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
2526 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2529 if (use_ps(stateblock))
2532 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.luminanceparams[stage]) {
2533 /* The pixel shader has to know the luminance offset. Do a constants update if it
2534 * isn't scheduled anyway
2536 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2537 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2541 if(device->shader_backend == &arb_program_shader_backend) {
2542 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2545 } else if(device->shader_backend == &arb_program_shader_backend) {
2546 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
2547 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
2550 param[0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLSCALE]);
2551 param[1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET]);
2555 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
2556 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
2559 static const char *get_argreg(SHADER_BUFFER *buffer, DWORD argnum, unsigned int stage, DWORD arg) {
2562 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
2564 switch(arg & WINED3DTA_SELECTMASK) {
2565 case WINED3DTA_DIFFUSE:
2566 ret = "fragment.color.primary"; break;
2568 case WINED3DTA_CURRENT:
2569 if(stage == 0) ret = "fragment.color.primary";
2573 case WINED3DTA_TEXTURE:
2575 case 0: ret = "tex0"; break;
2576 case 1: ret = "tex1"; break;
2577 case 2: ret = "tex2"; break;
2578 case 3: ret = "tex3"; break;
2579 case 4: ret = "tex4"; break;
2580 case 5: ret = "tex5"; break;
2581 case 6: ret = "tex6"; break;
2582 case 7: ret = "tex7"; break;
2583 default: ret = "unknown texture";
2587 case WINED3DTA_TFACTOR:
2588 ret = "tfactor"; break;
2590 case WINED3DTA_SPECULAR:
2591 ret = "fragment.color.secondary"; break;
2593 case WINED3DTA_TEMP:
2594 ret = "tempreg"; break;
2596 case WINED3DTA_CONSTANT:
2597 FIXME("Implement perstage constants\n");
2599 case 0: ret = "const0"; break;
2600 case 1: ret = "const1"; break;
2601 case 2: ret = "const2"; break;
2602 case 3: ret = "const3"; break;
2603 case 4: ret = "const4"; break;
2604 case 5: ret = "const5"; break;
2605 case 6: ret = "const6"; break;
2606 case 7: ret = "const7"; break;
2607 default: ret = "unknown constant";
2615 if(arg & WINED3DTA_COMPLEMENT) {
2616 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
2617 if(argnum == 0) ret = "arg0";
2618 if(argnum == 1) ret = "arg1";
2619 if(argnum == 2) ret = "arg2";
2621 if(arg & WINED3DTA_ALPHAREPLICATE) {
2622 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
2623 if(argnum == 0) ret = "arg0";
2624 if(argnum == 1) ret = "arg1";
2625 if(argnum == 2) ret = "arg2";
2630 static void gen_ffp_instr(SHADER_BUFFER *buffer, unsigned int stage, BOOL color, BOOL alpha,
2631 DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2) {
2632 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
2633 unsigned int mul = 1;
2634 BOOL mul_final_dest = FALSE;
2636 if(color && alpha) dstmask = "";
2637 else if(color) dstmask = ".xyz";
2638 else dstmask = ".w";
2640 if(dst == tempreg) dstreg = "tempreg";
2641 else dstreg = "ret";
2643 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
2644 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
2645 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
2648 case WINED3DTOP_DISABLE:
2649 if(stage == 0) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
2652 case WINED3DTOP_SELECTARG2:
2654 case WINED3DTOP_SELECTARG1:
2655 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
2658 case WINED3DTOP_MODULATE4X:
2660 case WINED3DTOP_MODULATE2X:
2662 if(strcmp(dstreg, "result.color") == 0) {
2664 mul_final_dest = TRUE;
2666 case WINED3DTOP_MODULATE:
2667 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2670 case WINED3DTOP_ADDSIGNED2X:
2672 if(strcmp(dstreg, "result.color") == 0) {
2674 mul_final_dest = TRUE;
2676 case WINED3DTOP_ADDSIGNED:
2677 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2679 case WINED3DTOP_ADD:
2680 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2683 case WINED3DTOP_SUBTRACT:
2684 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2687 case WINED3DTOP_ADDSMOOTH:
2688 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
2689 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2692 case WINED3DTOP_BLENDCURRENTALPHA:
2693 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
2694 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2696 case WINED3DTOP_BLENDFACTORALPHA:
2697 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
2698 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2700 case WINED3DTOP_BLENDTEXTUREALPHA:
2701 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2702 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2704 case WINED3DTOP_BLENDDIFFUSEALPHA:
2705 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
2706 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2709 case WINED3DTOP_BLENDTEXTUREALPHAPM:
2710 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2711 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
2712 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
2715 /* D3DTOP_PREMODULATE ???? */
2717 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2718 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
2719 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2721 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2722 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
2724 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2725 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
2726 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
2728 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2729 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
2732 case WINED3DTOP_DOTPRODUCT3:
2734 if(strcmp(dstreg, "result.color") == 0) {
2736 mul_final_dest = TRUE;
2738 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
2739 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2740 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
2743 case WINED3DTOP_MULTIPLYADD:
2744 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
2747 case WINED3DTOP_LERP:
2748 /* The msdn is not quite right here */
2749 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2752 case WINED3DTOP_BUMPENVMAP:
2753 case WINED3DTOP_BUMPENVMAPLUMINANCE:
2754 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
2758 FIXME("Unhandled texture op %08x\n", op);
2762 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2763 } else if(mul == 4) {
2764 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2768 /* The stateblock is passed for GLINFO_LOCATION */
2769 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, IWineD3DStateBlockImpl *stateblock)
2772 SHADER_BUFFER buffer;
2773 BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2774 BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2775 BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2776 const char *textype;
2777 const char *instr, *sat;
2778 char colorcor_dst[8];
2780 DWORD arg0, arg1, arg2;
2781 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
2783 const char *final_combiner_src = "ret";
2785 /* Find out which textures are read */
2786 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2787 if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
2788 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
2789 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
2790 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
2791 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2792 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2793 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2795 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
2796 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
2797 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
2798 bump_used[stage] = TRUE;
2799 tex_read[stage] = TRUE;
2801 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2802 bump_used[stage] = TRUE;
2803 tex_read[stage] = TRUE;
2804 luminance_used[stage] = TRUE;
2805 } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
2806 tfactor_used = TRUE;
2809 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2810 tfactor_used = TRUE;
2813 if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
2814 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2815 tempreg_used = TRUE;
2818 if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
2819 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
2820 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
2821 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
2822 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2823 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2824 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2826 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2827 tempreg_used = TRUE;
2829 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2830 tfactor_used = TRUE;
2835 shader_buffer_init(&buffer);
2837 shader_addline(&buffer, "!!ARBfp1.0\n");
2839 switch(settings->fog) {
2840 case FOG_OFF: break;
2841 case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
2842 case FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
2843 case FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
2844 default: FIXME("Unexpected fog setting %d\n", settings->fog);
2847 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
2848 shader_addline(&buffer, "TEMP TMP;\n");
2849 shader_addline(&buffer, "TEMP ret;\n");
2850 if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
2851 shader_addline(&buffer, "TEMP arg0;\n");
2852 shader_addline(&buffer, "TEMP arg1;\n");
2853 shader_addline(&buffer, "TEMP arg2;\n");
2854 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2855 if(!tex_read[stage]) continue;
2856 shader_addline(&buffer, "TEMP tex%u;\n", stage);
2857 if(!bump_used[stage]) continue;
2858 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
2859 if(!luminance_used[stage]) continue;
2860 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
2863 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
2865 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
2867 if(settings->sRGB_write) {
2868 shader_addline(&buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
2869 srgb_mul_low, srgb_mul_low, srgb_mul_low);
2870 shader_addline(&buffer, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
2871 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
2872 shader_addline(&buffer, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
2873 srgb_pow, srgb_pow, srgb_pow);
2874 shader_addline(&buffer, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
2875 srgb_mul_high, srgb_mul_high, srgb_mul_high);
2876 shader_addline(&buffer, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
2877 srgb_sub_high, srgb_sub_high, srgb_sub_high);
2880 /* Generate texture sampling instructions) */
2881 for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
2882 if(!tex_read[stage]) continue;
2884 switch(settings->op[stage].tex_type) {
2885 case tex_1d: textype = "1D"; break;
2886 case tex_2d: textype = "2D"; break;
2887 case tex_3d: textype = "3D"; break;
2888 case tex_cube: textype = "CUBE"; break;
2889 case tex_rect: textype = "RECT"; break;
2890 default: textype = "unexpected_textype"; break;
2893 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
2894 settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2900 if(settings->op[stage].projected == proj_none) {
2902 } else if(settings->op[stage].projected == proj_count4 ||
2903 settings->op[stage].projected == proj_count3) {
2906 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
2911 (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
2912 settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
2913 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
2914 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
2915 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
2916 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
2918 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2919 * so multiply the displacement with the dividing parameter before passing it to TXP
2921 if (settings->op[stage].projected != proj_none) {
2922 if(settings->op[stage].projected == proj_count4) {
2923 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
2924 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
2926 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
2927 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
2930 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
2933 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
2934 instr, sat, stage, stage, textype);
2935 if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2936 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
2937 stage - 1, stage - 1, stage - 1);
2938 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
2940 } else if(settings->op[stage].projected == proj_count3) {
2941 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
2942 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
2943 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
2944 instr, sat, stage, stage, textype);
2946 shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
2947 instr, sat, stage, stage, stage, textype);
2950 sprintf(colorcor_dst, "tex%u", stage);
2951 gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
2952 settings->op[stage].color_fixup);
2955 /* Generate the main shader */
2956 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2957 if(settings->op[stage].cop == WINED3DTOP_DISABLE) {
2959 final_combiner_src = "fragment.color.primary";
2964 if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
2965 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
2966 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
2967 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
2968 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
2969 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
2970 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
2971 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
2972 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
2973 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
2974 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
2975 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
2977 op_equal = settings->op[stage].aop == settings->op[stage].cop &&
2978 settings->op[stage].carg0 == settings->op[stage].aarg0 &&
2979 settings->op[stage].carg1 == settings->op[stage].aarg1 &&
2980 settings->op[stage].carg2 == settings->op[stage].aarg2;
2983 if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
2984 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
2985 settings->op[stage].cop, settings->op[stage].carg0,
2986 settings->op[stage].carg1, settings->op[stage].carg2);
2988 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
2990 } else if(op_equal) {
2991 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
2992 settings->op[stage].cop, settings->op[stage].carg0,
2993 settings->op[stage].carg1, settings->op[stage].carg2);
2995 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
2996 settings->op[stage].cop, settings->op[stage].carg0,
2997 settings->op[stage].carg1, settings->op[stage].carg2);
2998 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
2999 settings->op[stage].aop, settings->op[stage].aarg0,
3000 settings->op[stage].aarg1, settings->op[stage].aarg2);
3004 if(settings->sRGB_write) {
3005 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
3006 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg");
3007 shader_addline(&buffer, "MOV result.color.w, ret.w;\n");
3009 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
3013 shader_addline(&buffer, "END\n");
3015 /* Generate the shader */
3016 GL_EXTCALL(glGenProgramsARB(1, &ret));
3017 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
3018 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
3020 if (glGetError() == GL_INVALID_OPERATION) {
3022 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3023 FIXME("Fragment program error at position %d: %s\n", pos,
3024 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3026 shader_buffer_free(&buffer);
3030 static void fragment_prog_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3031 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
3032 struct shader_arb_priv *priv = device->fragment_priv;
3033 BOOL use_pshader = use_ps(stateblock);
3034 BOOL use_vshader = use_vs(stateblock);
3035 struct ffp_frag_settings settings;
3036 const struct arbfp_ffp_desc *desc;
3039 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
3041 if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
3042 if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
3043 /* Reload fixed function constants since they collide with the pixel shader constants */
3044 for(i = 0; i < MAX_TEXTURES; i++) {
3045 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
3047 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
3048 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
3049 } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3050 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3056 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
3057 gen_ffp_frag_op(stateblock, &settings, FALSE);
3058 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(priv->fragment_shaders, &settings);
3060 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
3063 ERR("Out of memory\n");
3066 new_desc->num_textures_used = 0;
3067 for(i = 0; i < GL_LIMITS(texture_stages); i++) {
3068 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
3069 new_desc->num_textures_used = i;
3072 memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
3073 new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
3074 add_ffp_frag_shader(priv->fragment_shaders, &new_desc->parent);
3075 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
3079 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
3080 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
3083 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
3084 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
3085 priv->current_fprogram_id = desc->shader;
3087 if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
3088 /* Reload fixed function constants since they collide with the pixel shader constants */
3089 for(i = 0; i < MAX_TEXTURES; i++) {
3090 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
3092 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
3093 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
3095 context->last_was_pshader = FALSE;
3097 context->last_was_pshader = TRUE;
3100 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
3101 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
3102 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
3103 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
3104 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
3106 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
3109 if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3110 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3112 if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
3113 device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
3117 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
3121 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
3122 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
3123 * state table, so we need to handle that with a forwarding function. The other invisible side effect
3124 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
3125 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
3127 static void state_arbfp_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3128 enum fogsource new_source;
3130 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
3132 if(!isStateDirty(context, STATE_PIXELSHADER)) {
3133 fragment_prog_arbfp(state, stateblock, context);
3136 if(!stateblock->renderState[WINED3DRS_FOGENABLE]) return;
3138 if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
3139 if(use_vs(stateblock)) {
3140 new_source = FOGSOURCE_VS;
3142 if(stateblock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || context->last_was_rhw) {
3143 new_source = FOGSOURCE_COORD;
3145 new_source = FOGSOURCE_FFP;
3149 new_source = FOGSOURCE_FFP;
3151 if(new_source != context->fog_source) {
3152 context->fog_source = new_source;
3153 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART), stateblock, context);
3157 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3158 if(!isStateDirty(context, STATE_PIXELSHADER)) {
3159 fragment_prog_arbfp(state, stateblock, context);
3163 #undef GLINFO_LOCATION
3165 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
3166 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR), state_texfactor_arbfp }, 0 },
3167 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3168 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3169 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3170 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3171 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3172 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3173 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3174 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3175 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3176 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3177 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3178 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3179 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3180 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3181 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3182 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3183 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3184 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3185 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3186 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3187 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3188 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3189 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3190 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3191 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3192 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3193 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3194 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3195 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3196 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3197 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3198 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3199 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3200 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3201 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3202 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3203 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3204 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3205 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3206 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3207 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3208 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3209 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3210 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3211 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3212 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3213 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3214 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3215 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3216 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3217 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3218 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3219 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3220 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3221 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3222 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3223 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3224 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3225 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3226 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3227 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3228 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3229 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3230 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3231 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3232 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3233 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3234 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3235 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3236 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3237 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3238 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3239 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3240 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3241 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3242 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3243 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3244 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3245 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3246 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3247 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3248 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3249 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3250 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3251 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3252 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3253 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3254 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3255 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3256 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3257 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3258 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3259 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3260 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3261 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3262 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3263 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3264 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3265 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3266 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3267 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3268 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3269 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3270 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3271 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3272 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3273 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3274 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3275 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3276 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3277 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3278 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3279 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3280 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3281 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3282 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3283 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3284 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3285 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3286 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3287 {STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim }, 0 },
3288 {STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim }, 0 },
3289 {STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim }, 0 },
3290 {STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim }, 0 },
3291 {STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim }, 0 },
3292 {STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim }, 0 },
3293 {STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim }, 0 },
3294 {STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim }, 0 },
3295 {STATE_PIXELSHADER, { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3296 {STATE_RENDER(WINED3DRS_FOGENABLE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3297 {STATE_RENDER(WINED3DRS_FOGTABLEMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3298 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3299 {STATE_RENDER(WINED3DRS_FOGSTART), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, 0 },
3300 {STATE_RENDER(WINED3DRS_FOGEND), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, 0 },
3301 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3302 {STATE_RENDER(WINED3DRS_FOGCOLOR), { STATE_RENDER(WINED3DRS_FOGCOLOR), state_fogcolor }, 0 },
3303 {STATE_RENDER(WINED3DRS_FOGDENSITY), { STATE_RENDER(WINED3DRS_FOGDENSITY), state_fogdensity }, 0 },
3304 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3305 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3306 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3307 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3308 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3309 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3310 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3311 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3312 {STATE_RENDER(WINED3DRS_SPECULARENABLE), { STATE_RENDER(WINED3DRS_SPECULARENABLE), state_arb_specularenable}, 0 },
3313 {0 /* Terminate */, { 0, 0 }, 0 },
3316 const struct fragment_pipeline arbfp_fragment_pipeline = {
3321 shader_arb_color_fixup_supported,
3322 arbfp_fragmentstate_template,
3323 TRUE /* We can disable projected textures */
3326 #define GLINFO_LOCATION device->adapter->gl_info
3328 struct arbfp_blit_priv {
3329 GLenum yuy2_rect_shader, yuy2_2d_shader;
3330 GLenum uyvy_rect_shader, uyvy_2d_shader;
3331 GLenum yv12_rect_shader, yv12_2d_shader;
3334 static HRESULT arbfp_blit_alloc(IWineD3DDevice *iface) {
3335 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3336 device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
3337 if(!device->blit_priv) {
3338 ERR("Out of memory\n");
3339 return E_OUTOFMEMORY;
3343 static void arbfp_blit_free(IWineD3DDevice *iface) {
3344 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3345 struct arbfp_blit_priv *priv = device->blit_priv;
3348 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
3349 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
3350 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
3351 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
3352 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
3353 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
3354 checkGLcall("Delete yuv programs\n");
3358 static BOOL gen_planar_yuv_read(SHADER_BUFFER *buffer, enum yuv_fixup yuv_fixup, GLenum textype, char *luminance)
3361 const char *tex, *texinstr;
3363 if (yuv_fixup == YUV_FIXUP_UYVY) {
3371 case GL_TEXTURE_2D: tex = "2D"; texinstr = "TXP"; break;
3372 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; texinstr = "TEX"; break;
3374 /* This is more tricky than just replacing the texture type - we have to navigate
3375 * properly in the texture to find the correct chroma values
3377 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
3381 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
3382 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
3383 * filtering when we sample the texture.
3385 * These are the rules for reading the chroma:
3391 * So we have to get the sampling x position in non-normalized coordinates in integers
3393 if(textype != GL_TEXTURE_RECTANGLE_ARB) {
3394 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
3395 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
3397 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3399 /* We must not allow filtering between pixel x and x+1, this would mix U and V
3400 * Vertical filtering is ok. However, bear in mind that the pixel center is at
3403 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
3404 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
3406 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
3407 * even and odd pixels respectively
3409 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
3410 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
3412 /* Sample Pixel 1 */
3413 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3415 /* Put the value into either of the chroma values */
3416 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3417 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
3418 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3419 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
3421 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
3422 * the pixel right to the current one. Otherwise, sample the left pixel.
3423 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
3425 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
3426 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
3427 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3429 /* Put the value into the other chroma */
3430 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3431 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
3432 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3433 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
3435 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
3436 * the current one and lerp the two U and V values
3439 /* This gives the correctly filtered luminance value */
3440 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
3445 static BOOL gen_yv12_read(SHADER_BUFFER *buffer, GLenum textype, char *luminance)
3450 case GL_TEXTURE_2D: tex = "2D"; break;
3451 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; break;
3453 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
3457 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
3458 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
3459 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
3460 * pitch of the luminance plane, the packing into the gl texture is a bit
3461 * unfortunate. If the whole texture is interpreted as luminance data it looks
3462 * approximately like this:
3464 * +----------------------------------+----
3476 * +----------------+-----------------+----
3478 * | U even rows | U odd rows |
3480 * +----------------+------------------ -
3482 * | V even rows | V odd rows |
3484 * +----------------+-----------------+----
3488 * So it appears as if there are 4 chroma images, but in fact the odd rows
3489 * in the chroma images are in the same row as the even ones. So its is
3490 * kinda tricky to read
3492 * When reading from rectangle textures, keep in mind that the input y coordinates
3493 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
3495 shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
3496 2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
3498 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3499 /* the chroma planes have only half the width */
3500 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
3502 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
3503 * the coordinate. Also read the right side of the image when reading odd lines
3505 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
3508 if(textype == GL_TEXTURE_2D) {
3510 shader_addline(buffer, "RCP chroma.w, size.y;\n");
3512 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
3514 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
3515 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
3517 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3518 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3519 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3520 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3521 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3523 /* clamp, keep the half pixel origin in mind */
3524 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
3525 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3526 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
3527 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3529 /* Read from [size - size+size/4] */
3530 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3531 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
3533 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3534 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3535 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3536 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3537 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
3538 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3540 /* Make sure to read exactly from the pixel center */
3541 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3542 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
3545 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
3546 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
3547 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3548 shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
3549 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3551 /* Read the texture, put the result into the output register */
3552 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3553 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
3555 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
3556 * No need to clamp because we're just reusing the already clamped value from above
3558 if(textype == GL_TEXTURE_2D) {
3559 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
3561 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
3563 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3564 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
3566 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
3567 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
3568 * values due to filtering
3570 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3571 if(textype == GL_TEXTURE_2D) {
3572 /* Multiply the y coordinate by 2/3 and clamp it */
3573 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
3574 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
3575 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3576 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3578 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
3579 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
3582 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
3583 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
3584 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3591 static GLuint gen_yuv_shader(IWineD3DDeviceImpl *device, enum yuv_fixup yuv_fixup, GLenum textype)
3594 SHADER_BUFFER buffer;
3595 char luminance_component;
3596 struct arbfp_blit_priv *priv = device->blit_priv;
3599 shader_buffer_init(&buffer);
3602 GL_EXTCALL(glGenProgramsARB(1, &shader));
3603 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
3604 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3605 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3608 shader_buffer_free(&buffer);
3612 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
3613 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
3614 * two chroma(U and V) values. Each macropixel has two luminance values, one for
3615 * each single pixel it contains, and one U and one V value shared between both
3618 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
3619 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
3620 * take the format into account when generating the read swizzles
3622 * Reading the Y value is straightforward - just sample the texture. The hardware
3623 * takes care of filtering in the horizontal and vertical direction.
3625 * Reading the U and V values is harder. We have to avoid filtering horizontally,
3626 * because that would mix the U and V values of one pixel or two adjacent pixels.
3627 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
3628 * regardless of the filtering setting. Vertical filtering works automatically
3629 * though - the U and V values of two rows are mixed nicely.
3631 * Appart of avoiding filtering issues, the code has to know which value it just
3632 * read, and where it can find the other one. To determine this, it checks if
3633 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
3635 * Handling horizontal filtering of U and V values requires reading a 2nd pair
3636 * of pixels, extracting U and V and mixing them. This is not implemented yet.
3638 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
3639 * with width / 2. This way one read gives all 3 values, finding U and V is easy
3640 * in an unfiltered situation. Finding the luminance on the other hand requires
3641 * finding out if it is an odd or even pixel. The real drawback of this approach
3642 * is filtering. This would have to be emulated completely in the shader, reading
3643 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
3644 * vertically. Beyond that it would require adjustments to the texture handling
3645 * code to deal with the width scaling
3647 shader_addline(&buffer, "!!ARBfp1.0\n");
3648 shader_addline(&buffer, "TEMP luminance;\n");
3649 shader_addline(&buffer, "TEMP temp;\n");
3650 shader_addline(&buffer, "TEMP chroma;\n");
3651 shader_addline(&buffer, "TEMP texcrd;\n");
3652 shader_addline(&buffer, "TEMP texcrd2;\n");
3653 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
3654 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
3655 shader_addline(&buffer, "PARAM size = program.local[0];\n");
3659 case YUV_FIXUP_UYVY:
3660 case YUV_FIXUP_YUY2:
3661 if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
3663 shader_buffer_free(&buffer);
3668 case YUV_FIXUP_YV12:
3669 if (!gen_yv12_read(&buffer, textype, &luminance_component))
3671 shader_buffer_free(&buffer);
3677 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3678 shader_buffer_free(&buffer);
3682 /* Calculate the final result. Formula is taken from
3683 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
3684 * ranges from -0.5 to 0.5
3686 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
3688 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
3689 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
3690 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
3691 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
3692 shader_addline(&buffer, "END\n");
3695 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
3697 if (glGetError() == GL_INVALID_OPERATION) {
3699 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3700 FIXME("Fragment program error at position %d: %s\n", pos,
3701 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3703 shader_buffer_free(&buffer);
3708 case YUV_FIXUP_YUY2:
3709 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
3710 else priv->yuy2_2d_shader = shader;
3713 case YUV_FIXUP_UYVY:
3714 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
3715 else priv->uyvy_2d_shader = shader;
3718 case YUV_FIXUP_YV12:
3719 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
3720 else priv->yv12_2d_shader = shader;
3727 static HRESULT arbfp_blit_set(IWineD3DDevice *iface, const struct GlPixelFormatDesc *format_desc,
3728 GLenum textype, UINT width, UINT height)
3731 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3732 float size[4] = {width, height, 1, 1};
3733 struct arbfp_blit_priv *priv = device->blit_priv;
3734 enum yuv_fixup yuv_fixup;
3736 if (!is_yuv_fixup(format_desc->color_fixup))
3739 dump_color_fixup_desc(format_desc->color_fixup);
3740 /* Don't bother setting up a shader for unconverted formats */
3743 checkGLcall("glEnable(textype)");
3748 yuv_fixup = get_yuv_fixup(format_desc->color_fixup);
3752 case YUV_FIXUP_YUY2:
3753 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
3756 case YUV_FIXUP_UYVY:
3757 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
3760 case YUV_FIXUP_YV12:
3761 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
3765 FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup);
3768 checkGLcall("glEnable(textype)");
3773 if (!shader) shader = gen_yuv_shader(device, yuv_fixup, textype);
3776 glEnable(GL_FRAGMENT_PROGRAM_ARB);
3777 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
3778 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3779 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3780 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
3781 checkGLcall("glProgramLocalParameter4fvARB");
3787 static void arbfp_blit_unset(IWineD3DDevice *iface) {
3788 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3791 glDisable(GL_FRAGMENT_PROGRAM_ARB);
3792 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3793 glDisable(GL_TEXTURE_2D);
3794 checkGLcall("glDisable(GL_TEXTURE_2D)");
3795 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
3796 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3797 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3799 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3800 glDisable(GL_TEXTURE_RECTANGLE_ARB);
3801 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
3806 static BOOL arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup)
3808 enum yuv_fixup yuv_fixup;
3810 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3812 TRACE("Checking support for fixup:\n");
3813 dump_color_fixup_desc(fixup);
3816 if (is_identity_fixup(fixup))
3822 /* We only support YUV conversions. */
3823 if (!is_yuv_fixup(fixup))
3825 TRACE("[FAILED]\n");
3829 yuv_fixup = get_yuv_fixup(fixup);
3832 case YUV_FIXUP_YUY2:
3833 case YUV_FIXUP_UYVY:
3834 case YUV_FIXUP_YV12:
3839 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3840 TRACE("[FAILED]\n");
3845 const struct blit_shader arbfp_blit = {
3850 arbfp_blit_color_fixup_supported,
3853 #undef GLINFO_LOCATION