msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / wined3d / arb_program_shader.c
blobcf434a6086cd8041a5f6c635cbc44768f4635508
1 /*
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
29 #include "config.h"
31 #include <math.h>
32 #include <stdio.h>
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 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
44 * so upload them above that
46 #define ARB_SHADER_PRIVCONST_BASE GL_LIMITS(vshader_constantsF)
47 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
49 /* ARB_program_shader private data */
50 struct shader_arb_priv {
51 GLuint current_vprogram_id;
52 GLuint current_fprogram_id;
53 GLuint depth_blt_vprogram_id;
54 GLuint depth_blt_fprogram_id[tex_type_count];
55 BOOL use_arbfp_fixed_func;
56 struct hash_table_t *fragment_shaders;
59 /********************************************************
60 * ARB_[vertex/fragment]_program helper functions follow
61 ********************************************************/
63 /**
64 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
65 * When constant_list == NULL, it will load all the constants.
67 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
68 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
70 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, const WineD3D_GL_Info *gl_info,
71 GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
73 DWORD shader_version = This->baseShader.reg_maps.shader_version;
74 local_constant* lconst;
75 DWORD i, j;
76 unsigned int ret;
78 if (TRACE_ON(d3d_shader)) {
79 for(i = 0; i < max_constants; i++) {
80 if(!dirty_consts[i]) continue;
81 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
82 constants[i * 4 + 0], constants[i * 4 + 1],
83 constants[i * 4 + 2], constants[i * 4 + 3]);
86 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
87 if (target_type == GL_FRAGMENT_PROGRAM_ARB && WINED3DSHADER_VERSION_MAJOR(shader_version) == 1)
89 float lcl_const[4];
90 for(i = 0; i < max_constants; i++) {
91 if(!dirty_consts[i]) continue;
92 dirty_consts[i] = 0;
94 j = 4 * i;
95 if(constants[j + 0] > 1.0) lcl_const[0] = 1.0;
96 else if(constants[j + 0] < -1.0) lcl_const[0] = -1.0;
97 else lcl_const[0] = constants[j + 0];
99 if(constants[j + 1] > 1.0) lcl_const[1] = 1.0;
100 else if(constants[j + 1] < -1.0) lcl_const[1] = -1.0;
101 else lcl_const[1] = constants[j + 1];
103 if(constants[j + 2] > 1.0) lcl_const[2] = 1.0;
104 else if(constants[j + 2] < -1.0) lcl_const[2] = -1.0;
105 else lcl_const[2] = constants[j + 2];
107 if(constants[j + 3] > 1.0) lcl_const[3] = 1.0;
108 else if(constants[j + 3] < -1.0) lcl_const[3] = -1.0;
109 else lcl_const[3] = constants[j + 3];
111 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
113 } else {
114 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
115 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
116 * or just reloading *all* constants at once
118 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
120 for(i = 0; i < max_constants; i++) {
121 if(!dirty_consts[i]) continue;
123 /* Find the next block of dirty constants */
124 dirty_consts[i] = 0;
125 j = i;
126 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
127 dirty_consts[i] = 0;
130 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
132 } else {
133 for(i = 0; i < max_constants; i++) {
134 if(dirty_consts[i]) {
135 dirty_consts[i] = 0;
136 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
141 checkGLcall("glProgramEnvParameter4fvARB()");
143 /* Load immediate constants */
144 if(This->baseShader.load_local_constsF) {
145 if (TRACE_ON(d3d_shader)) {
146 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
147 GLfloat* values = (GLfloat*)lconst->value;
148 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
149 values[0], values[1], values[2], values[3]);
152 /* Immediate constants are clamped for 1.X shaders at loading times */
153 ret = 0;
154 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
155 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
156 ret = max(ret, lconst->idx + 1);
157 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
159 checkGLcall("glProgramEnvParameter4fvARB()");
160 return ret; /* The loaded immediate constants need reloading for the next shader */
161 } else {
162 return 0; /* No constants are dirty now */
167 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
169 * We only support float constants in ARB at the moment, so don't
170 * worry about the Integers or Booleans
172 static void shader_arb_load_constants(
173 IWineD3DDevice* device,
174 char usePixelShader,
175 char useVertexShader) {
177 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
178 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
179 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
180 unsigned char i;
182 if (useVertexShader) {
183 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
185 /* Load DirectX 9 float constants for vertex shader */
186 deviceImpl->highest_dirty_vs_const = shader_arb_load_constantsF(
187 vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
188 deviceImpl->highest_dirty_vs_const,
189 stateBlock->vertexShaderConstantF,
190 deviceImpl->activeContext->vshader_const_dirty);
192 /* Upload the position fixup */
193 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
196 if (usePixelShader) {
198 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
199 IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
201 /* Load DirectX 9 float constants for pixel shader */
202 deviceImpl->highest_dirty_ps_const = shader_arb_load_constantsF(
203 pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
204 deviceImpl->highest_dirty_ps_const,
205 stateBlock->pixelShaderConstantF,
206 deviceImpl->activeContext->pshader_const_dirty);
208 for(i = 0; i < psi->numbumpenvmatconsts; i++) {
209 /* The state manager takes care that this function is always called if the bump env matrix changes
211 const float *data = (const float *)&stateBlock->textureState[(int) psi->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
212 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst[i].const_num, data));
213 deviceImpl->activeContext->pshader_const_dirty[psi->bumpenvmatconst[i].const_num] = 1;
215 if(psi->luminanceconst[i].const_num != -1) {
216 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
217 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
218 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
219 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
221 const float *scale = (const float *)&stateBlock->textureState[(int) psi->luminanceconst[i].texunit][WINED3DTSS_BUMPENVLSCALE];
222 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->luminanceconst[i].const_num, scale));
223 deviceImpl->activeContext->pshader_const_dirty[psi->luminanceconst[i].const_num] = 1;
229 static void shader_arb_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
231 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
233 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
234 * context. On a context switch the old context will be fully dirtified */
235 memset(This->activeContext->vshader_const_dirty + start, 1,
236 sizeof(*This->activeContext->vshader_const_dirty) * count);
237 This->highest_dirty_vs_const = max(This->highest_dirty_vs_const, start + count + 1);
240 static void shader_arb_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
242 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
244 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
245 * context. On a context switch the old context will be fully dirtified */
246 memset(This->activeContext->pshader_const_dirty + start, 1,
247 sizeof(*This->activeContext->pshader_const_dirty) * count);
248 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, start + count + 1);
251 /* Generate the variable & register declarations for the ARB_vertex_program output target */
252 static void shader_generate_arb_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
253 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info)
255 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
256 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
257 DWORD i, cur;
258 char pshader = shader_is_pshader_version(reg_maps->shader_version);
259 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
260 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
261 UINT extra_constants_needed = 0;
262 const local_constant *lconst;
264 /* Temporary Output register */
265 shader_addline(buffer, "TEMP TMP_OUT;\n");
267 for(i = 0; i < This->baseShader.limits.temporary; i++) {
268 if (reg_maps->temporary[i])
269 shader_addline(buffer, "TEMP R%u;\n", i);
272 for (i = 0; i < This->baseShader.limits.address; i++) {
273 if (reg_maps->address[i])
274 shader_addline(buffer, "ADDRESS A%d;\n", i);
277 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
278 if (reg_maps->texcoord[i])
279 shader_addline(buffer,"TEMP T%u;\n", i);
282 /* Texture coordinate registers must be pre-loaded */
283 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
284 if (reg_maps->texcoord[i])
285 shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
288 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
289 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
290 if(!reg_maps->bumpmat[i]) continue;
292 cur = ps->numbumpenvmatconsts;
293 ps->bumpenvmatconst[cur].const_num = -1;
294 ps->bumpenvmatconst[cur].texunit = i;
295 ps->luminanceconst[cur].const_num = -1;
296 ps->luminanceconst[cur].texunit = i;
298 /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
299 * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
300 * bump mapping.
302 if(max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF)) {
303 ps->bumpenvmatconst[cur].const_num = max_constantsF + extra_constants_needed;
304 shader_addline(buffer, "PARAM bumpenvmat%d = program.env[%d];\n",
305 i, ps->bumpenvmatconst[cur].const_num);
306 extra_constants_needed++;
308 if(reg_maps->luminanceparams && max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF)) {
309 ((IWineD3DPixelShaderImpl *)This)->luminanceconst[cur].const_num = max_constantsF + extra_constants_needed;
310 shader_addline(buffer, "PARAM luminance%d = program.env[%d];\n",
311 i, ps->luminanceconst[cur].const_num);
312 extra_constants_needed++;
313 } else if(reg_maps->luminanceparams) {
314 FIXME("No free constant to load the luminance parameters\n");
316 } else {
317 FIXME("No free constant found to load environment bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
320 ps->numbumpenvmatconsts = cur + 1;
323 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] && pshader) {
324 shader_addline(buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
325 srgb_mul_low, srgb_mul_low, srgb_mul_low);
326 shader_addline(buffer, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
327 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
328 shader_addline(buffer, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
329 srgb_pow, srgb_pow, srgb_pow);
330 shader_addline(buffer, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
331 srgb_mul_high, srgb_mul_high, srgb_mul_high);
332 shader_addline(buffer, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
333 srgb_sub_high, srgb_sub_high, srgb_sub_high);
336 /* Load local constants using the program-local space,
337 * this avoids reloading them each time the shader is used
339 if(!This->baseShader.load_local_constsF) {
340 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
341 shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
342 lconst->idx);
346 /* we use the array-based constants array if the local constants are marked for loading,
347 * because then we use indirect addressing, or when the local constant list is empty,
348 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
349 * local constants do not declare the loaded constants as an array because ARB compilers usually
350 * do not optimize unused constants away
352 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
353 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
354 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
355 max_constantsF, max_constantsF - 1);
356 } else {
357 for(i = 0; i < max_constantsF; i++) {
358 if(!shader_constant_is_local(This, i)) {
359 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
365 static const char * const shift_tab[] = {
366 "dummy", /* 0 (none) */
367 "coefmul.x", /* 1 (x2) */
368 "coefmul.y", /* 2 (x4) */
369 "coefmul.z", /* 3 (x8) */
370 "coefmul.w", /* 4 (x16) */
371 "dummy", /* 5 (x32) */
372 "dummy", /* 6 (x64) */
373 "dummy", /* 7 (x128) */
374 "dummy", /* 8 (d256) */
375 "dummy", /* 9 (d128) */
376 "dummy", /* 10 (d64) */
377 "dummy", /* 11 (d32) */
378 "coefdiv.w", /* 12 (d16) */
379 "coefdiv.z", /* 13 (d8) */
380 "coefdiv.y", /* 14 (d4) */
381 "coefdiv.x" /* 15 (d2) */
384 static void shader_arb_get_write_mask(const SHADER_OPCODE_ARG *arg, const DWORD param, char *write_mask)
386 char *ptr = write_mask;
387 char vshader = shader_is_vshader_version(arg->reg_maps->shader_version);
389 if(vshader && shader_get_regtype(param) == WINED3DSPR_ADDR) {
390 *ptr++ = '.';
391 *ptr++ = 'x';
392 } else if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
393 *ptr++ = '.';
394 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
395 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
396 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
397 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
400 *ptr = '\0';
403 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
404 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
405 * but addressed as "rgba". To fix this we need to swap the register's x
406 * and z components. */
407 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
408 char *ptr = swizzle_str;
410 /* swizzle bits fields: wwzzyyxx */
411 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
412 DWORD swizzle_x = swizzle & 0x03;
413 DWORD swizzle_y = (swizzle >> 2) & 0x03;
414 DWORD swizzle_z = (swizzle >> 4) & 0x03;
415 DWORD swizzle_w = (swizzle >> 6) & 0x03;
417 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
418 * generate a swizzle string. Unless we need to our own swizzling. */
419 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
420 *ptr++ = '.';
421 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
422 *ptr++ = swizzle_chars[swizzle_x];
423 } else {
424 *ptr++ = swizzle_chars[swizzle_x];
425 *ptr++ = swizzle_chars[swizzle_y];
426 *ptr++ = swizzle_chars[swizzle_z];
427 *ptr++ = swizzle_chars[swizzle_w];
431 *ptr = '\0';
434 static void pshader_get_register_name(IWineD3DBaseShader* iface,
435 const DWORD param, char* regstr) {
437 DWORD reg = param & WINED3DSP_REGNUM_MASK;
438 DWORD regtype = shader_get_regtype(param);
439 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
441 switch (regtype) {
442 case WINED3DSPR_TEMP:
443 sprintf(regstr, "R%u", reg);
444 break;
445 case WINED3DSPR_INPUT:
446 if (reg==0) {
447 strcpy(regstr, "fragment.color.primary");
448 } else {
449 strcpy(regstr, "fragment.color.secondary");
451 break;
452 case WINED3DSPR_CONST:
453 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
454 sprintf(regstr, "C[%u]", reg);
455 } else {
456 sprintf(regstr, "C%u", reg);
458 break;
459 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
460 sprintf(regstr,"T%u", reg);
461 break;
462 case WINED3DSPR_COLOROUT:
463 if (reg == 0)
464 sprintf(regstr, "TMP_COLOR");
465 else {
466 /* TODO: See GL_ARB_draw_buffers */
467 FIXME("Unsupported write to render target %u\n", reg);
468 sprintf(regstr, "unsupported_register");
470 break;
471 case WINED3DSPR_DEPTHOUT:
472 sprintf(regstr, "result.depth");
473 break;
474 case WINED3DSPR_ATTROUT:
475 sprintf(regstr, "oD[%u]", reg);
476 break;
477 case WINED3DSPR_TEXCRDOUT:
478 sprintf(regstr, "oT[%u]", reg);
479 break;
480 default:
481 FIXME("Unhandled register name Type(%d)\n", regtype);
482 sprintf(regstr, "unrecognized_register");
483 break;
487 /* TODO: merge with pixel shader */
488 static void vshader_program_add_param(const SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine)
490 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
492 /* oPos, oFog and oPts in D3D */
493 static const char * const hwrastout_reg_names[] = { "TMP_OUT", "result.fogcoord", "result.pointsize" };
495 DWORD reg = param & WINED3DSP_REGNUM_MASK;
496 DWORD regtype = shader_get_regtype(param);
497 char tmpReg[255];
498 BOOL is_color = FALSE;
500 if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
501 strcat(hwLine, " -");
502 } else {
503 strcat(hwLine, " ");
506 switch (regtype) {
507 case WINED3DSPR_TEMP:
508 sprintf(tmpReg, "R%u", reg);
509 strcat(hwLine, tmpReg);
510 break;
511 case WINED3DSPR_INPUT:
513 if (This->swizzle_map & (1 << reg)) is_color = TRUE;
515 sprintf(tmpReg, "vertex.attrib[%u]", reg);
516 strcat(hwLine, tmpReg);
517 break;
518 case WINED3DSPR_CONST:
519 if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
520 if(reg >= This->rel_offset) {
521 sprintf(tmpReg, "C[A0.x + %u]", reg - This->rel_offset);
522 } else {
523 sprintf(tmpReg, "C[A0.x - %u]", -reg + This->rel_offset);
525 } else {
526 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
527 sprintf(tmpReg, "C[%u]", reg);
528 } else {
529 sprintf(tmpReg, "C%u", reg);
532 strcat(hwLine, tmpReg);
533 break;
534 case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
535 sprintf(tmpReg, "A%u", reg);
536 strcat(hwLine, tmpReg);
537 break;
538 case WINED3DSPR_RASTOUT:
539 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
540 strcat(hwLine, tmpReg);
541 break;
542 case WINED3DSPR_ATTROUT:
543 if (reg==0) {
544 strcat(hwLine, "result.color.primary");
545 } else {
546 strcat(hwLine, "result.color.secondary");
548 break;
549 case WINED3DSPR_TEXCRDOUT:
550 sprintf(tmpReg, "result.texcoord[%u]", reg);
551 strcat(hwLine, tmpReg);
552 break;
553 default:
554 FIXME("Unknown reg type %d %d\n", regtype, reg);
555 strcat(hwLine, "unrecognized_register");
556 break;
559 if (!is_input) {
560 char write_mask[6];
561 shader_arb_get_write_mask(arg, param, write_mask);
562 strcat(hwLine, write_mask);
563 } else {
564 char swizzle[6];
565 shader_arb_get_swizzle(param, is_color, swizzle);
566 strcat(hwLine, swizzle);
570 static void shader_hw_sample(const SHADER_OPCODE_ARG *arg, DWORD sampler_idx, const char *dst_str,
571 const char *coord_reg, BOOL projected, BOOL bias)
573 SHADER_BUFFER* buffer = arg->buffer;
574 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
575 const char *tex_type;
577 switch(sampler_type) {
578 case WINED3DSTT_1D:
579 tex_type = "1D";
580 break;
582 case WINED3DSTT_2D:
584 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) arg->shader;
585 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
586 if(device->stateBlock->textures[sampler_idx] &&
587 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
588 tex_type = "RECT";
589 } else {
590 tex_type = "2D";
592 break;
595 case WINED3DSTT_VOLUME:
596 tex_type = "3D";
597 break;
599 case WINED3DSTT_CUBE:
600 tex_type = "CUBE";
601 break;
603 default:
604 ERR("Unexpected texture type %d\n", sampler_type);
605 tex_type = "";
608 if (bias) {
609 /* Shouldn't be possible, but let's check for it */
610 if(projected) FIXME("Biased and Projected texture sampling\n");
611 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
612 shader_addline(buffer, "TXB %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
613 } else if (projected) {
614 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
615 } else {
616 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
620 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
622 switch(channel_source)
624 case CHANNEL_SOURCE_ZERO: return "0";
625 case CHANNEL_SOURCE_ONE: return "1";
626 case CHANNEL_SOURCE_X: return "x";
627 case CHANNEL_SOURCE_Y: return "y";
628 case CHANNEL_SOURCE_Z: return "z";
629 case CHANNEL_SOURCE_W: return "w";
630 default:
631 FIXME("Unhandled channel source %#x\n", channel_source);
632 return "undefined";
636 static void gen_color_correction(SHADER_BUFFER *buffer, const char *reg, DWORD dst_mask,
637 const char *one, const char *two, struct color_fixup_desc fixup)
639 DWORD mask;
641 if (is_yuv_fixup(fixup))
643 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
644 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
645 return;
648 mask = 0;
649 if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
650 if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
651 if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
652 if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
653 mask &= dst_mask;
655 if (mask)
657 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
658 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
659 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
662 mask = 0;
663 if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
664 if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
665 if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
666 if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
667 mask &= dst_mask;
669 if (mask)
671 char reg_mask[6];
672 char *ptr = reg_mask;
674 if (mask != WINED3DSP_WRITEMASK_ALL)
676 *ptr++ = '.';
677 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
678 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
679 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
680 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
682 *ptr = '\0';
684 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
688 static void shader_arb_color_correction(const struct SHADER_OPCODE_ARG* arg, struct color_fixup_desc fixup)
690 char reg[256];
691 pshader_get_register_name(arg->shader, arg->dst, reg);
692 gen_color_correction(arg->buffer, reg, arg->dst & WINED3DSP_WRITEMASK_ALL, "one", "coefmul.x", fixup);
695 static void pshader_gen_input_modifier_line (
696 IWineD3DBaseShader *iface,
697 SHADER_BUFFER* buffer,
698 const DWORD instr,
699 int tmpreg,
700 char *outregstr) {
702 /* Generate a line that does the input modifier computation and return the input register to use */
703 char regstr[256];
704 char swzstr[20];
705 int insert_line;
707 /* Assume a new line will be added */
708 insert_line = 1;
710 /* Get register name */
711 pshader_get_register_name(iface, instr, regstr);
712 shader_arb_get_swizzle(instr, FALSE, swzstr);
714 switch (instr & WINED3DSP_SRCMOD_MASK) {
715 case WINED3DSPSM_NONE:
716 sprintf(outregstr, "%s%s", regstr, swzstr);
717 insert_line = 0;
718 break;
719 case WINED3DSPSM_NEG:
720 sprintf(outregstr, "-%s%s", regstr, swzstr);
721 insert_line = 0;
722 break;
723 case WINED3DSPSM_BIAS:
724 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
725 break;
726 case WINED3DSPSM_BIASNEG:
727 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
728 break;
729 case WINED3DSPSM_SIGN:
730 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
731 break;
732 case WINED3DSPSM_SIGNNEG:
733 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
734 break;
735 case WINED3DSPSM_COMP:
736 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
737 break;
738 case WINED3DSPSM_X2:
739 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
740 break;
741 case WINED3DSPSM_X2NEG:
742 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
743 break;
744 case WINED3DSPSM_DZ:
745 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
746 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
747 break;
748 case WINED3DSPSM_DW:
749 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
750 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
751 break;
752 default:
753 sprintf(outregstr, "%s%s", regstr, swzstr);
754 insert_line = 0;
757 /* Return modified or original register, with swizzle */
758 if (insert_line)
759 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
762 static inline void pshader_gen_output_modifier_line(SHADER_BUFFER *buffer, int saturate, const char *write_mask,
763 int shift, const char *regstr)
765 /* Generate a line that does the output modifier computation */
766 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
767 regstr, write_mask, regstr, shift_tab[shift]);
770 static void pshader_hw_bem(const SHADER_OPCODE_ARG *arg)
772 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
774 SHADER_BUFFER* buffer = arg->buffer;
775 char dst_name[50];
776 char src_name[2][50];
777 char dst_wmask[20];
778 DWORD sampler_code = arg->dst & WINED3DSP_REGNUM_MASK;
779 BOOL has_bumpmat = FALSE;
780 int i;
782 for(i = 0; i < This->numbumpenvmatconsts; i++) {
783 if(This->bumpenvmatconst[i].const_num != -1 && This->bumpenvmatconst[i].texunit == sampler_code) {
784 has_bumpmat = TRUE;
785 break;
789 pshader_get_register_name(arg->shader, arg->dst, dst_name);
790 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
791 strcat(dst_name, dst_wmask);
793 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
794 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
796 if(has_bumpmat) {
797 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
798 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
799 shader_addline(buffer, "DP3 TMP.r, TMP2, %s;\n", src_name[1]);
800 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
801 shader_addline(buffer, "DP3 TMP.g, TMP2, %s;\n", src_name[1]);
803 shader_addline(buffer, "ADD %s, %s, TMP;\n", dst_name, src_name[0]);
804 } else {
805 shader_addline(buffer, "MOV %s, %s;\n", dst_name, src_name[0]);
809 static void pshader_hw_cnd(const SHADER_OPCODE_ARG *arg)
811 SHADER_BUFFER* buffer = arg->buffer;
812 char dst_wmask[20];
813 char dst_name[50];
814 char src_name[3][50];
815 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
816 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
818 /* FIXME: support output modifiers */
820 /* Handle output register */
821 pshader_get_register_name(arg->shader, arg->dst, dst_name);
822 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
824 /* Generate input register names (with modifiers) */
825 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
826 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
827 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
829 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
830 if (arg->reg_maps->shader_version <= WINED3DPS_VERSION(1, 3) && arg->opcode_token & WINED3DSI_COISSUE)
832 shader_addline(buffer, "MOV%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1]);
833 } else {
834 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
835 shader_addline(buffer, "CMP%s %s%s, TMP, %s, %s;\n",
836 sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1], src_name[2]);
838 if (shift != 0)
839 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
842 static void pshader_hw_cmp(const SHADER_OPCODE_ARG *arg)
844 SHADER_BUFFER* buffer = arg->buffer;
845 char dst_wmask[20];
846 char dst_name[50];
847 char src_name[3][50];
848 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
849 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
851 /* FIXME: support output modifiers */
853 /* Handle output register */
854 pshader_get_register_name(arg->shader, arg->dst, dst_name);
855 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
857 /* Generate input register names (with modifiers) */
858 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
859 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
860 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
862 shader_addline(buffer, "CMP%s %s%s, %s, %s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
863 src_name[0], src_name[2], src_name[1]);
865 if (shift != 0)
866 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
869 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
870 * dst = dot2(src0, src1) + src2 */
871 static void pshader_hw_dp2add(const SHADER_OPCODE_ARG *arg)
873 SHADER_BUFFER* buffer = arg->buffer;
874 char dst_wmask[20];
875 char dst_name[50];
876 char src_name[3][50];
877 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
878 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
880 pshader_get_register_name(arg->shader, arg->dst, dst_name);
881 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
883 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
884 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
885 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
887 /* Emulate a DP2 with a DP3 and 0.0 */
888 shader_addline(buffer, "MOV TMP, %s;\n", src_name[0]);
889 shader_addline(buffer, "MOV TMP.z, 0.0;\n");
890 shader_addline(buffer, "DP3 TMP2, TMP, %s;\n", src_name[1]);
891 shader_addline(buffer, "ADD%s %s%s, TMP2, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[2]);
893 if (shift != 0)
894 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
897 /* Map the opcode 1-to-1 to the GL code */
898 static void shader_hw_map2gl(const SHADER_OPCODE_ARG *arg)
900 CONST SHADER_OPCODE* curOpcode = arg->opcode;
901 SHADER_BUFFER* buffer = arg->buffer;
902 DWORD dst = arg->dst;
903 const DWORD *src = arg->src;
904 const char *instruction;
905 char arguments[256];
906 unsigned int i;
908 switch (curOpcode->opcode)
910 case WINED3DSIO_ABS: instruction = "ABS"; break;
911 case WINED3DSIO_ADD: instruction = "ADD"; break;
912 case WINED3DSIO_CRS: instruction = "XPD"; break;
913 case WINED3DSIO_DP3: instruction = "DP3"; break;
914 case WINED3DSIO_DP4: instruction = "DP4"; break;
915 case WINED3DSIO_DST: instruction = "DST"; break;
916 case WINED3DSIO_EXP: instruction = "EX2"; break;
917 case WINED3DSIO_EXPP: instruction = "EXP"; break;
918 case WINED3DSIO_FRC: instruction = "FRC"; break;
919 case WINED3DSIO_LIT: instruction = "LIT"; break;
920 case WINED3DSIO_LOG: instruction = "LG2"; break;
921 case WINED3DSIO_LOGP: instruction = "LOG"; break;
922 case WINED3DSIO_LRP: instruction = "LRP"; break;
923 case WINED3DSIO_MAD: instruction = "MAD"; break;
924 case WINED3DSIO_MAX: instruction = "MAX"; break;
925 case WINED3DSIO_MIN: instruction = "MIN"; break;
926 case WINED3DSIO_MOV: instruction = "MOV"; break;
927 case WINED3DSIO_MUL: instruction = "MUL"; break;
928 case WINED3DSIO_NOP: instruction = "NOP"; break;
929 case WINED3DSIO_POW: instruction = "POW"; break;
930 case WINED3DSIO_SGE: instruction = "SGE"; break;
931 case WINED3DSIO_SLT: instruction = "SLT"; break;
932 case WINED3DSIO_SUB: instruction = "SUB"; break;
933 default: instruction = "";
934 FIXME("Unhandled opcode %s\n", curOpcode->name);
935 break;
938 if (shader_is_pshader_version(arg->reg_maps->shader_version))
940 /* Output token related */
941 char output_rname[256];
942 char output_wmask[20];
943 char operands[4][100];
944 BOOL saturate = FALSE;
945 BOOL centroid = FALSE;
946 BOOL partialprecision = FALSE;
947 const char *modifier;
948 DWORD shift;
950 if (!curOpcode->num_params)
952 ERR("Opcode \"%s\" has no parameters\n", curOpcode->name);
953 return;
956 /* Process modifiers */
957 if (dst & WINED3DSP_DSTMOD_MASK)
959 DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
961 saturate = mask & WINED3DSPDM_SATURATE;
962 centroid = mask & WINED3DSPDM_MSAMPCENTROID;
963 partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
964 mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
965 if (mask)
966 FIXME("Unrecognized modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
968 if (centroid)
969 FIXME("Unhandled modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
971 shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
972 modifier = (saturate && !shift) ? "_SAT" : "";
974 /* Generate input register names (with modifiers) */
975 for (i = 1; i < curOpcode->num_params; ++i)
976 pshader_gen_input_modifier_line(arg->shader, buffer, src[i-1], i-1, operands[i]);
978 /* Handle output register */
979 pshader_get_register_name(arg->shader, dst, output_rname);
980 strcpy(operands[0], output_rname);
981 shader_arb_get_write_mask(arg, dst, output_wmask);
982 strcat(operands[0], output_wmask);
984 arguments[0] = '\0';
985 strcat(arguments, operands[0]);
986 for (i = 1; i < curOpcode->num_params; i++)
988 strcat(arguments, ", ");
989 strcat(arguments, operands[i]);
991 shader_addline(buffer, "%s%s %s;\n", instruction, modifier, arguments);
993 /* A shift requires another line. */
994 if (shift) pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
995 } else {
996 /* Note that vshader_program_add_param() adds spaces. */
998 arguments[0] = '\0';
999 if (curOpcode->num_params > 0)
1001 vshader_program_add_param(arg, dst, FALSE, arguments);
1002 for (i = 1; i < curOpcode->num_params; ++i)
1004 strcat(arguments, ",");
1005 vshader_program_add_param(arg, src[i-1], TRUE, arguments);
1008 shader_addline(buffer, "%s%s;\n", instruction, arguments);
1012 static void shader_hw_mov(const SHADER_OPCODE_ARG *arg)
1014 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl*)arg->shader;
1016 if ((WINED3DSHADER_VERSION_MAJOR(arg->reg_maps->shader_version) == 1
1017 && !shader_is_pshader_version(arg->reg_maps->shader_version)
1018 && shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)
1019 || arg->opcode->opcode == WINED3DSIO_MOVA)
1021 SHADER_BUFFER *buffer = arg->buffer;
1022 char src0_param[256];
1024 if (arg->opcode->opcode == WINED3DSIO_MOVA)
1025 FIXME("mova should round\n");
1027 src0_param[0] = '\0';
1028 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1030 vshader_program_add_param(arg, arg->src[0], TRUE, src0_param);
1031 shader_addline(buffer, "ADD TMP.x, %s, helper_const.z;\n", src0_param);
1032 shader_addline(buffer, "ARL A0.x, TMP.x;\n");
1034 else
1036 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1037 * with more than one component. Thus replicate the first source argument over all
1038 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc)
1040 DWORD parm = arg->src[0] & ~(WINED3DVS_SWIZZLE_MASK);
1041 if((arg->src[0] & WINED3DVS_X_W) == WINED3DVS_X_W)
1042 parm |= WINED3DVS_X_W | WINED3DVS_Y_W | WINED3DVS_Z_W | WINED3DVS_W_W;
1043 else if((arg->src[0] & WINED3DVS_X_Z) == WINED3DVS_X_Z)
1044 parm |= WINED3DVS_X_Z | WINED3DVS_Y_Z | WINED3DVS_Z_Z | WINED3DVS_W_Z;
1045 else if((arg->src[0] & WINED3DVS_X_Y) == WINED3DVS_X_Y)
1046 parm |= WINED3DVS_X_Y | WINED3DVS_Y_Y | WINED3DVS_Z_Y | WINED3DVS_W_Y;
1047 else if((arg->src[0] & WINED3DVS_X_X) == WINED3DVS_X_X)
1048 parm |= WINED3DVS_X_X | WINED3DVS_Y_X | WINED3DVS_Z_X | WINED3DVS_W_X;
1049 vshader_program_add_param(arg, parm, TRUE, src0_param);
1050 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1053 else
1055 shader_hw_map2gl(arg);
1059 static void pshader_hw_texkill(const SHADER_OPCODE_ARG *arg)
1061 DWORD shader_version = arg->reg_maps->shader_version;
1062 SHADER_BUFFER* buffer = arg->buffer;
1063 char reg_dest[40];
1065 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1066 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1068 pshader_get_register_name(arg->shader, arg->dst, reg_dest);
1070 if (shader_version >= WINED3DPS_VERSION(2,0))
1072 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1073 shader_addline(buffer, "KIL %s;\n", reg_dest);
1074 } else {
1075 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1076 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1078 shader_addline(buffer, "MOV TMP, %s;\n", reg_dest);
1079 shader_addline(buffer, "MOV TMP.w, one.w;\n");
1080 shader_addline(buffer, "KIL TMP;\n");
1084 static void pshader_hw_tex(const SHADER_OPCODE_ARG *arg)
1086 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1087 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1089 DWORD dst = arg->dst;
1090 const DWORD *src = arg->src;
1091 SHADER_BUFFER* buffer = arg->buffer;
1092 DWORD shader_version = arg->reg_maps->shader_version;
1093 BOOL projected = FALSE, bias = FALSE;
1095 char reg_dest[40];
1096 char reg_coord[40];
1097 DWORD reg_dest_code;
1098 DWORD reg_sampler_code;
1100 /* All versions have a destination register */
1101 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
1102 pshader_get_register_name(arg->shader, dst, reg_dest);
1104 /* 1.0-1.3: Use destination register as coordinate source.
1105 1.4+: Use provided coordinate source register. */
1106 if (shader_version < WINED3DPS_VERSION(1,4))
1107 strcpy(reg_coord, reg_dest);
1108 else
1109 pshader_gen_input_modifier_line(arg->shader, buffer, src[0], 0, reg_coord);
1111 /* 1.0-1.4: Use destination register number as texture code.
1112 2.0+: Use provided sampler number as texure code. */
1113 if (shader_version < WINED3DPS_VERSION(2,0))
1114 reg_sampler_code = reg_dest_code;
1115 else
1116 reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
1118 /* projection flag:
1119 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1120 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1121 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1123 if (shader_version < WINED3DPS_VERSION(1,4))
1125 DWORD flags = 0;
1126 if(reg_sampler_code < MAX_TEXTURES) {
1127 flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1129 if (flags & WINED3DTTFF_PROJECTED) {
1130 projected = TRUE;
1133 else if (shader_version < WINED3DPS_VERSION(2,0))
1135 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1136 if (src_mod == WINED3DSPSM_DZ) {
1137 projected = TRUE;
1138 } else if(src_mod == WINED3DSPSM_DW) {
1139 projected = TRUE;
1141 } else {
1142 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1143 projected = TRUE;
1145 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
1146 bias = TRUE;
1149 shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord, projected, bias);
1152 static void pshader_hw_texcoord(const SHADER_OPCODE_ARG *arg)
1154 DWORD dst = arg->dst;
1155 SHADER_BUFFER* buffer = arg->buffer;
1157 char tmp[20];
1158 shader_arb_get_write_mask(arg, dst, tmp);
1159 if (arg->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
1161 DWORD reg = dst & WINED3DSP_REGNUM_MASK;
1162 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
1163 } else {
1164 DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
1165 char reg_src[40];
1167 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, reg_src);
1168 shader_addline(buffer, "MOV R%u%s, %s;\n", reg1, tmp, reg_src);
1172 static void pshader_hw_texreg2ar(const SHADER_OPCODE_ARG *arg)
1174 SHADER_BUFFER* buffer = arg->buffer;
1175 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1176 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1177 DWORD flags;
1179 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1180 char dst_str[8];
1181 char src_str[50];
1183 sprintf(dst_str, "T%u", reg1);
1184 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1185 shader_addline(buffer, "MOV TMP.x, %s.w;\n", src_str);
1186 shader_addline(buffer, "MOV TMP.y, %s.x;\n", src_str);
1187 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1188 shader_hw_sample(arg, reg1, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1191 static void pshader_hw_texreg2gb(const SHADER_OPCODE_ARG *arg)
1193 SHADER_BUFFER* buffer = arg->buffer;
1195 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1196 char dst_str[8];
1197 char src_str[50];
1199 sprintf(dst_str, "T%u", reg1);
1200 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1201 shader_addline(buffer, "MOV TMP.x, %s.y;\n", src_str);
1202 shader_addline(buffer, "MOV TMP.y, %s.z;\n", src_str);
1203 shader_hw_sample(arg, reg1, dst_str, "TMP", FALSE, FALSE);
1206 static void pshader_hw_texreg2rgb(const SHADER_OPCODE_ARG *arg)
1208 SHADER_BUFFER* buffer = arg->buffer;
1209 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1210 char dst_str[8];
1211 char src_str[50];
1213 sprintf(dst_str, "T%u", reg1);
1214 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1215 shader_hw_sample(arg, reg1, dst_str, src_str, FALSE, FALSE);
1218 static void pshader_hw_texbem(const SHADER_OPCODE_ARG *arg)
1220 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1221 BOOL has_bumpmat = FALSE;
1222 BOOL has_luminance = FALSE;
1223 int i;
1225 DWORD dst = arg->dst;
1226 DWORD src = arg->src[0] & WINED3DSP_REGNUM_MASK;
1227 SHADER_BUFFER* buffer = arg->buffer;
1229 char reg_coord[40];
1230 DWORD reg_dest_code;
1232 /* All versions have a destination register */
1233 reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
1234 /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
1235 pshader_get_register_name(arg->shader, dst, reg_coord);
1237 for(i = 0; i < This->numbumpenvmatconsts; i++) {
1238 if(This->bumpenvmatconst[i].const_num != -1 && reg_dest_code == This->bumpenvmatconst[i].texunit) {
1239 has_bumpmat = TRUE;
1240 break;
1243 for(i = 0; i < This->numbumpenvmatconsts; i++) {
1244 if(This->luminanceconst[i].const_num != -1 && reg_dest_code == This->luminanceconst[i].texunit) {
1245 has_luminance = TRUE;
1246 break;
1250 if(has_bumpmat) {
1251 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
1253 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1254 shader_addline(buffer, "DP3 TMP.x, TMP2, T%u;\n", src);
1255 shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1256 shader_addline(buffer, "DP3 TMP.y, TMP2, T%u;\n", src);
1258 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1259 * so we can't let the GL handle this.
1261 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1262 & WINED3DTTFF_PROJECTED) {
1263 shader_addline(buffer, "RCP TMP2.w, %s.w;\n", reg_coord);
1264 shader_addline(buffer, "MUL TMP2.xy, %s, TMP2.w;\n", reg_coord);
1265 shader_addline(buffer, "ADD TMP.xy, TMP, TMP2;\n");
1266 } else {
1267 shader_addline(buffer, "ADD TMP.xy, TMP, %s;\n", reg_coord);
1270 shader_hw_sample(arg, reg_dest_code, reg_coord, "TMP", FALSE, FALSE);
1272 if(arg->opcode->opcode == WINED3DSIO_TEXBEML && has_luminance) {
1273 shader_addline(buffer, "MAD TMP, T%u.z, luminance%d.x, luminance%d.y;\n",
1274 src, reg_dest_code, reg_dest_code);
1275 shader_addline(buffer, "MUL %s, %s, TMP;\n", reg_coord, reg_coord);
1278 } else {
1279 DWORD tf;
1280 if(reg_dest_code < MAX_TEXTURES) {
1281 tf = ((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1282 } else {
1283 tf = 0;
1285 /* Without a bump matrix loaded, just sample with the unmodified coordinates */
1286 shader_hw_sample(arg, reg_dest_code, reg_coord, reg_coord, tf & WINED3DTTFF_PROJECTED, FALSE);
1290 static void pshader_hw_texm3x2pad(const SHADER_OPCODE_ARG *arg)
1292 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1293 SHADER_BUFFER* buffer = arg->buffer;
1294 char src0_name[50];
1296 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1297 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
1300 static void pshader_hw_texm3x2tex(const SHADER_OPCODE_ARG *arg)
1302 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1303 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1304 DWORD flags;
1305 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1306 SHADER_BUFFER* buffer = arg->buffer;
1307 char dst_str[8];
1308 char src0_name[50];
1310 sprintf(dst_str, "T%u", reg);
1311 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1312 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
1313 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1314 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1317 static void pshader_hw_texm3x3pad(const SHADER_OPCODE_ARG *arg)
1319 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1320 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1321 SHADER_BUFFER* buffer = arg->buffer;
1322 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1323 char src0_name[50];
1325 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1326 shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
1327 current_state->texcoord_w[current_state->current_row++] = reg;
1330 static void pshader_hw_texm3x3tex(const SHADER_OPCODE_ARG *arg)
1332 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1333 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1334 DWORD flags;
1335 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1336 SHADER_BUFFER* buffer = arg->buffer;
1337 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1338 char dst_str[8];
1339 char src0_name[50];
1341 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1342 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1344 /* Sample the texture using the calculated coordinates */
1345 sprintf(dst_str, "T%u", reg);
1346 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1347 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1348 current_state->current_row = 0;
1351 static void pshader_hw_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
1353 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1354 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1355 DWORD flags;
1356 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1357 SHADER_BUFFER* buffer = arg->buffer;
1358 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1359 char dst_str[8];
1360 char src0_name[50];
1362 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1363 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1365 /* Construct the eye-ray vector from w coordinates */
1366 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
1367 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
1368 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
1370 /* Calculate reflection vector
1372 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
1373 /* The .w is ignored when sampling, so I can use TMP2.w to calculate dot(N, N) */
1374 shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1375 shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1376 shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1377 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1378 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
1380 /* Sample the texture using the calculated coordinates */
1381 sprintf(dst_str, "T%u", reg);
1382 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1383 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1384 current_state->current_row = 0;
1387 static void pshader_hw_texm3x3spec(const SHADER_OPCODE_ARG *arg)
1389 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1390 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1391 DWORD flags;
1392 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1393 DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
1394 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1395 SHADER_BUFFER* buffer = arg->buffer;
1396 char dst_str[8];
1397 char src0_name[50];
1399 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1400 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1402 /* Calculate reflection vector.
1404 * dot(N, E)
1405 * TMP.xyz = 2 * --------- * N - E
1406 * dot(N, N)
1408 * Which normalizes the normal vector
1410 shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
1411 shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1412 shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1413 shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1414 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1415 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
1417 /* Sample the texture using the calculated coordinates */
1418 sprintf(dst_str, "T%u", reg);
1419 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1420 shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1421 current_state->current_row = 0;
1424 static void pshader_hw_texdepth(const SHADER_OPCODE_ARG *arg)
1426 SHADER_BUFFER* buffer = arg->buffer;
1427 char dst_name[50];
1429 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1430 * which is essentially an input, is the destination register because it is the first
1431 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1432 * here
1434 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1436 /* According to the msdn, the source register(must be r5) is unusable after
1437 * the texdepth instruction, so we're free to modify it
1439 shader_addline(buffer, "MIN %s.y, %s.y, one.y;\n", dst_name, dst_name);
1441 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1442 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1443 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1445 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
1446 shader_addline(buffer, "MUL TMP.x, %s.x, %s.y;\n", dst_name, dst_name);
1447 shader_addline(buffer, "MIN TMP.x, TMP.x, one.x;\n");
1448 shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1451 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1452 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1453 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1454 static void pshader_hw_texdp3tex(const SHADER_OPCODE_ARG *arg)
1456 SHADER_BUFFER* buffer = arg->buffer;
1457 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1458 char src0[50];
1459 char dst_str[8];
1461 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1462 shader_addline(buffer, "MOV TMP, 0.0;\n");
1463 shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", sampler_idx, src0);
1465 sprintf(dst_str, "T%u", sampler_idx);
1466 shader_hw_sample(arg, sampler_idx, dst_str, "TMP", FALSE /* Only one coord, can't be projected */, FALSE);
1469 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1470 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1471 static void pshader_hw_texdp3(const SHADER_OPCODE_ARG *arg)
1473 char src0[50];
1474 char dst_str[50];
1475 char dst_mask[6];
1476 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1477 SHADER_BUFFER* buffer = arg->buffer;
1479 /* Handle output register */
1480 pshader_get_register_name(arg->shader, arg->dst, dst_str);
1481 shader_arb_get_write_mask(arg, arg->dst, dst_mask);
1483 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1484 shader_addline(buffer, "DP3 %s%s, T%u, %s;\n", dst_str, dst_mask, dstreg, src0);
1486 /* TODO: Handle output modifiers */
1489 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1490 * Perform the 3rd row of a 3x3 matrix multiply */
1491 static void pshader_hw_texm3x3(const SHADER_OPCODE_ARG *arg)
1493 SHADER_BUFFER* buffer = arg->buffer;
1494 char dst_str[50];
1495 char dst_mask[6];
1496 char src0[50];
1497 DWORD dst_reg = arg->dst & WINED3DSP_REGNUM_MASK;
1499 pshader_get_register_name(arg->shader, arg->dst, dst_str);
1500 shader_arb_get_write_mask(arg, arg->dst, dst_mask);
1502 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1503 shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", dst_reg, src0);
1504 shader_addline(buffer, "MOV %s%s, TMP;\n", dst_str, dst_mask);
1506 /* TODO: Handle output modifiers */
1509 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1510 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1511 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1512 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1514 static void pshader_hw_texm3x2depth(const SHADER_OPCODE_ARG *arg)
1516 SHADER_BUFFER* buffer = arg->buffer;
1517 DWORD dst_reg = arg->dst & WINED3DSP_REGNUM_MASK;
1518 char src0[50];
1520 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1521 shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", dst_reg, src0);
1523 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1524 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1525 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1527 shader_addline(buffer, "RCP TMP.y, TMP.y;\n");
1528 shader_addline(buffer, "MUL TMP.x, TMP.x, TMP.y;\n");
1529 shader_addline(buffer, "MIN TMP.x, TMP.x, one.x;\n");
1530 shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1533 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1534 Vertex/Pixel shaders to ARB_vertex_program codes */
1535 static void shader_hw_mnxn(const SHADER_OPCODE_ARG *arg)
1537 int i;
1538 int nComponents = 0;
1539 SHADER_OPCODE_ARG tmpArg;
1540 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1541 const SHADER_OPCODE *opcode_table = shader->baseShader.shader_ins;
1542 DWORD shader_version = arg->reg_maps->shader_version;
1544 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1546 /* Set constants for the temporary argument */
1547 tmpArg.shader = arg->shader;
1548 tmpArg.buffer = arg->buffer;
1549 tmpArg.src[0] = arg->src[0];
1550 tmpArg.src_addr[0] = arg->src_addr[0];
1551 tmpArg.src_addr[1] = arg->src_addr[1];
1552 tmpArg.reg_maps = arg->reg_maps;
1554 switch(arg->opcode->opcode) {
1555 case WINED3DSIO_M4x4:
1556 nComponents = 4;
1557 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1558 break;
1559 case WINED3DSIO_M4x3:
1560 nComponents = 3;
1561 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1562 break;
1563 case WINED3DSIO_M3x4:
1564 nComponents = 4;
1565 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1566 break;
1567 case WINED3DSIO_M3x3:
1568 nComponents = 3;
1569 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1570 break;
1571 case WINED3DSIO_M3x2:
1572 nComponents = 2;
1573 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1574 break;
1575 default:
1576 break;
1579 for (i = 0; i < nComponents; i++) {
1580 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1581 tmpArg.src[1] = arg->src[1]+i;
1582 shader_hw_map2gl(&tmpArg);
1586 static void vshader_hw_rsq_rcp(const SHADER_OPCODE_ARG *arg)
1588 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1589 SHADER_BUFFER* buffer = arg->buffer;
1590 DWORD dst = arg->dst;
1591 DWORD src = arg->src[0];
1592 DWORD swizzle = (src & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1593 const char *instruction;
1595 char tmpLine[256];
1597 switch(curOpcode->opcode)
1599 case WINED3DSIO_RSQ: instruction = "RSQ"; break;
1600 case WINED3DSIO_RCP: instruction = "RCP"; break;
1601 default: instruction = "";
1602 FIXME("Unhandled opcode %s\n", curOpcode->name);
1603 break;
1606 strcpy(tmpLine, instruction);
1607 vshader_program_add_param(arg, dst, FALSE, tmpLine); /* Destination */
1608 strcat(tmpLine, ",");
1609 vshader_program_add_param(arg, src, TRUE, tmpLine);
1610 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) == swizzle) {
1611 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1612 * .w is used
1614 strcat(tmpLine, ".w");
1617 shader_addline(buffer, "%s;\n", tmpLine);
1620 static void shader_hw_nrm(const SHADER_OPCODE_ARG *arg)
1622 SHADER_BUFFER* buffer = arg->buffer;
1623 char dst_name[50];
1624 char src_name[50];
1625 char dst_wmask[20];
1626 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1627 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1629 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1630 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1632 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name);
1633 shader_addline(buffer, "DP3 TMP, %s, %s;\n", src_name, src_name);
1634 shader_addline(buffer, "RSQ TMP, TMP.x;\n");
1635 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1636 shader_addline(buffer, "MUL%s %s%s, %s, TMP;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1637 src_name);
1639 if (shift != 0)
1640 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1643 static void shader_hw_sincos(const SHADER_OPCODE_ARG *arg)
1645 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1646 * must contain fixed constants. So we need a separate function to filter those constants and
1647 * can't use map2gl
1649 SHADER_BUFFER* buffer = arg->buffer;
1650 char dst_name[50];
1651 char src_name[50];
1652 char dst_wmask[20];
1653 DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1654 BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1656 pshader_get_register_name(arg->shader, arg->dst, dst_name);
1657 shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1659 pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name);
1660 shader_addline(buffer, "SCS%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1661 src_name);
1663 if (shift != 0)
1664 pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1668 static GLuint create_arb_blt_vertex_program(const WineD3D_GL_Info *gl_info)
1670 GLuint program_id = 0;
1671 const char *blt_vprogram =
1672 "!!ARBvp1.0\n"
1673 "PARAM c[1] = { { 1, 0.5 } };\n"
1674 "MOV result.position, vertex.position;\n"
1675 "MOV result.color, c[0].x;\n"
1676 "MOV result.texcoord[0], vertex.texcoord[0];\n"
1677 "END\n";
1679 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1680 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
1681 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
1683 if (glGetError() == GL_INVALID_OPERATION) {
1684 GLint pos;
1685 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1686 FIXME("Vertex program error at position %d: %s\n", pos,
1687 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1690 return program_id;
1693 static GLuint create_arb_blt_fragment_program(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
1695 GLuint program_id = 0;
1696 static const char * const blt_fprograms[tex_type_count] =
1698 /* tex_1d */
1699 NULL,
1700 /* tex_2d */
1701 "!!ARBfp1.0\n"
1702 "TEMP R0;\n"
1703 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1704 "MOV result.depth.z, R0.x;\n"
1705 "END\n",
1706 /* tex_3d */
1707 NULL,
1708 /* tex_cube */
1709 "!!ARBfp1.0\n"
1710 "TEMP R0;\n"
1711 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
1712 "MOV result.depth.z, R0.x;\n"
1713 "END\n",
1714 /* tex_rect */
1715 "!!ARBfp1.0\n"
1716 "TEMP R0;\n"
1717 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
1718 "MOV result.depth.z, R0.x;\n"
1719 "END\n",
1722 if (!blt_fprograms[tex_type])
1724 FIXME("tex_type %#x not supported\n", tex_type);
1725 tex_type = tex_2d;
1728 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1729 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
1730 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprograms[tex_type]), blt_fprograms[tex_type]));
1732 if (glGetError() == GL_INVALID_OPERATION) {
1733 GLint pos;
1734 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1735 FIXME("Fragment program error at position %d: %s\n", pos,
1736 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1739 return program_id;
1742 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1743 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1744 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1745 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1747 if (useVS) {
1748 TRACE("Using vertex shader\n");
1749 IWineD3DVertexShaderImpl_CompileShader(This->stateBlock->vertexShader);
1751 priv->current_vprogram_id = ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->prgId;
1753 /* Bind the vertex program */
1754 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1755 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1757 /* Enable OpenGL vertex programs */
1758 glEnable(GL_VERTEX_PROGRAM_ARB);
1759 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1760 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1761 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1762 priv->current_vprogram_id = 0;
1763 glDisable(GL_VERTEX_PROGRAM_ARB);
1764 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1767 if (usePS) {
1768 struct ps_compile_args compile_args;
1769 TRACE("Using pixel shader\n");
1770 find_ps_compile_args((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader, This->stateBlock, &compile_args);
1771 priv->current_fprogram_id = find_gl_pshader((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader,
1772 &compile_args);
1774 /* Bind the fragment program */
1775 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1776 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1778 if(!priv->use_arbfp_fixed_func) {
1779 /* Enable OpenGL fragment programs */
1780 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1781 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1783 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1784 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && !priv->use_arbfp_fixed_func) {
1785 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
1786 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
1787 * replacement shader
1789 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1790 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1791 priv->current_fprogram_id = 0;
1795 static void shader_arb_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
1796 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1797 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1798 GLuint *blt_fprogram = &priv->depth_blt_fprogram_id[tex_type];
1799 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1801 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
1802 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
1803 glEnable(GL_VERTEX_PROGRAM_ARB);
1805 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type);
1806 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
1807 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1810 static void shader_arb_deselect_depth_blt(IWineD3DDevice *iface) {
1811 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1812 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1813 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1815 if (priv->current_vprogram_id) {
1816 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1817 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1819 glEnable(GL_VERTEX_PROGRAM_ARB);
1820 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1822 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1823 } else {
1824 glDisable(GL_VERTEX_PROGRAM_ARB);
1825 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1828 if (priv->current_fprogram_id) {
1829 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1830 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1832 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1833 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1835 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1836 } else {
1837 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1838 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1842 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
1843 IWineD3DBaseShaderImpl *baseShader = (IWineD3DBaseShaderImpl *) iface;
1844 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)baseShader->baseShader.device)->adapter->gl_info;
1846 if (shader_is_pshader_version(baseShader->baseShader.reg_maps.shader_version))
1848 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
1849 UINT i;
1851 ENTER_GL();
1852 for(i = 0; i < This->num_gl_shaders; i++) {
1853 GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId));
1854 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1856 LEAVE_GL();
1857 HeapFree(GetProcessHeap(), 0, This->gl_shaders);
1858 This->gl_shaders = NULL;
1859 This->num_gl_shaders = 0;
1860 This->shader_array_size = 0;
1861 } else {
1862 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *) iface;
1864 ENTER_GL();
1865 GL_EXTCALL(glDeleteProgramsARB(1, &This->prgId));
1866 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->prgId))");
1867 ((IWineD3DVertexShaderImpl *) This)->prgId = 0;
1868 LEAVE_GL();
1870 baseShader->baseShader.is_compiled = FALSE;
1873 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
1874 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1875 This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
1876 return WINED3D_OK;
1879 static void shader_arb_free(IWineD3DDevice *iface) {
1880 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1881 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1882 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1883 int i;
1885 if(priv->depth_blt_vprogram_id) {
1886 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
1888 for (i = 0; i < tex_type_count; ++i) {
1889 if (priv->depth_blt_fprogram_id[i]) {
1890 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id[i]));
1894 HeapFree(GetProcessHeap(), 0, This->shader_priv);
1897 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
1898 return TRUE;
1901 static void arbfp_add_sRGB_correction(SHADER_BUFFER *buffer, const char *fragcolor, const char *tmp1,
1902 const char *tmp2, const char *tmp3, const char *tmp4) {
1903 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
1905 /* Calculate the > 0.0031308 case */
1906 shader_addline(buffer, "POW %s.x, %s.x, srgb_pow.x;\n", tmp1, fragcolor);
1907 shader_addline(buffer, "POW %s.y, %s.y, srgb_pow.y;\n", tmp1, fragcolor);
1908 shader_addline(buffer, "POW %s.z, %s.z, srgb_pow.z;\n", tmp1, fragcolor);
1909 shader_addline(buffer, "MUL %s, %s, srgb_mul_hi;\n", tmp1, tmp1);
1910 shader_addline(buffer, "SUB %s, %s, srgb_sub_hi;\n", tmp1, tmp1);
1911 /* Calculate the < case */
1912 shader_addline(buffer, "MUL %s, srgb_mul_low, %s;\n", tmp2, fragcolor);
1913 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
1914 shader_addline(buffer, "SLT %s, srgb_comparison, %s;\n", tmp3, fragcolor);
1915 shader_addline(buffer, "SGE %s, srgb_comparison, %s;\n", tmp4, fragcolor);
1916 /* Store the components > 0.0031308 in the destination */
1917 shader_addline(buffer, "MUL %s, %s, %s;\n", fragcolor, tmp1, tmp3);
1918 /* Add the components that are < 0.0031308 */
1919 shader_addline(buffer, "MAD result.color.xyz, %s, %s, %s;\n", tmp2, tmp4, fragcolor);
1920 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
1923 static GLuint shader_arb_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1924 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1925 const shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
1926 CONST DWORD *function = This->baseShader.function;
1927 DWORD shader_version = reg_maps->shader_version;
1928 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
1929 const local_constant *lconst;
1930 GLuint retval;
1931 const char *fragcolor;
1933 /* Create the hw ARB shader */
1934 shader_addline(buffer, "!!ARBfp1.0\n");
1936 if (shader_version < WINED3DPS_VERSION(3,0)) {
1937 switch(args->fog) {
1938 case FOG_OFF:
1939 break;
1940 case FOG_LINEAR:
1941 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
1942 break;
1943 case FOG_EXP:
1944 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
1945 break;
1946 case FOG_EXP2:
1947 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
1948 break;
1952 shader_addline(buffer, "TEMP TMP;\n"); /* Used in matrix ops */
1953 shader_addline(buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
1954 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
1955 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
1956 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
1957 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
1958 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
1959 shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
1961 if (shader_version < WINED3DPS_VERSION(2,0)) {
1962 fragcolor = "R0";
1963 } else {
1964 shader_addline(buffer, "TEMP TMP_COLOR;\n");
1965 fragcolor = "TMP_COLOR";
1968 /* Base Declarations */
1969 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
1971 /* Base Shader Body */
1972 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
1974 if(args->srgb_correction) {
1975 arbfp_add_sRGB_correction(buffer, fragcolor, "TMP", "TMP2", "TA", "TB");
1977 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
1978 shader_addline(buffer, "END\n");
1980 /* TODO: change to resource.glObjectHandle or something like that */
1981 GL_EXTCALL(glGenProgramsARB(1, &retval));
1983 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
1984 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
1986 TRACE("Created hw pixel shader, prg=%d\n", retval);
1987 /* Create the program and check for errors */
1988 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1989 buffer->bsize, buffer->buffer));
1991 if (glGetError() == GL_INVALID_OPERATION) {
1992 GLint errPos;
1993 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
1994 FIXME("HW PixelShader Error at position %d: %s\n",
1995 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1996 retval = 0;
1999 /* Load immediate constants */
2000 if(!This->baseShader.load_local_constsF) {
2001 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2002 const float *value = (const float *)lconst->value;
2003 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst->idx, value));
2004 checkGLcall("glProgramLocalParameter4fvARB");
2008 return retval;
2011 static void shader_arb_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
2012 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
2013 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
2014 CONST DWORD *function = This->baseShader.function;
2015 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
2016 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
2017 const local_constant *lconst;
2019 /* Create the hw ARB shader */
2020 shader_addline(buffer, "!!ARBvp1.0\n");
2021 shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
2023 /* Mesa supports only 95 constants */
2024 if (GL_VEND(MESA) || GL_VEND(WINE))
2025 This->baseShader.limits.constant_float =
2026 min(95, This->baseShader.limits.constant_float);
2028 shader_addline(buffer, "TEMP TMP;\n");
2030 /* Base Declarations */
2031 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
2033 /* We need a constant to fixup the final position */
2034 shader_addline(buffer, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS);
2036 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
2037 * for output parameters. D3D in theory does not do that either, but some applications depend on a
2038 * proper initialization of the secondary color, and programs using the fixed function pipeline without
2039 * a replacement shader depend on the texcoord.w being set properly.
2041 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
2042 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
2043 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
2044 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
2045 * this can eat a number of instructions, so skip it unless this cap is set as well
2047 if(!GL_SUPPORT(NV_VERTEX_PROGRAM)) {
2048 shader_addline(buffer, "MOV result.color.secondary, -helper_const.wwwy;\n");
2050 if((GLINFO_LOCATION).set_texcoord_w && !device->frag_pipe->ffp_proj_control) {
2051 int i;
2052 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2053 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2054 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2055 shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
2061 /* Base Shader Body */
2062 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
2064 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
2065 if (!reg_maps->fog)
2066 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2068 /* Write the final position.
2070 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2071 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2072 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2073 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2075 shader_addline(buffer, "MUL TMP, posFixup, TMP_OUT.w;\n");
2076 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TMP.z;\n");
2077 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TMP.w;\n");
2079 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2080 * and the glsl equivalent
2082 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2084 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2086 shader_addline(buffer, "END\n");
2088 /* TODO: change to resource.glObjectHandle or something like that */
2089 GL_EXTCALL(glGenProgramsARB(1, &This->prgId));
2091 TRACE("Creating a hw vertex shader, prg=%d\n", This->prgId);
2092 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, This->prgId));
2094 TRACE("Created hw vertex shader, prg=%d\n", This->prgId);
2095 /* Create the program and check for errors */
2096 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2097 buffer->bsize, buffer->buffer));
2099 if (glGetError() == GL_INVALID_OPERATION) {
2100 GLint errPos;
2101 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2102 FIXME("HW VertexShader Error at position %d: %s\n",
2103 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2104 This->prgId = -1;
2107 /* Load immediate constants */
2108 if(!This->baseShader.load_local_constsF) {
2109 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2110 const float *value = (const float *)lconst->value;
2111 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst->idx, value));
2116 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
2118 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2119 * then overwrite the shader specific ones
2121 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
2123 if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
2124 pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
2125 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2126 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
2129 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
2130 pCaps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
2131 pCaps->PixelShader1xMaxValue = 8.0;
2132 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2136 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
2138 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
2140 TRACE("Checking support for color_fixup:\n");
2141 dump_color_fixup_desc(fixup);
2144 /* We support everything except YUV conversions. */
2145 if (!is_yuv_fixup(fixup))
2147 TRACE("[OK]\n");
2148 return TRUE;
2151 TRACE("[FAILED]\n");
2152 return FALSE;
2155 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
2157 /* WINED3DSIH_ABS */ shader_hw_map2gl,
2158 /* WINED3DSIH_ADD */ shader_hw_map2gl,
2159 /* WINED3DSIH_BEM */ pshader_hw_bem,
2160 /* WINED3DSIH_BREAK */ NULL,
2161 /* WINED3DSIH_BREAKC */ NULL,
2162 /* WINED3DSIH_BREAKP */ NULL,
2163 /* WINED3DSIH_CALL */ NULL,
2164 /* WINED3DSIH_CALLNZ */ NULL,
2165 /* WINED3DSIH_CMP */ pshader_hw_cmp,
2166 /* WINED3DSIH_CND */ pshader_hw_cnd,
2167 /* WINED3DSIH_CRS */ shader_hw_map2gl,
2168 /* WINED3DSIH_DCL */ NULL,
2169 /* WINED3DSIH_DEF */ NULL,
2170 /* WINED3DSIH_DEFB */ NULL,
2171 /* WINED3DSIH_DEFI */ NULL,
2172 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
2173 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
2174 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
2175 /* WINED3DSIH_DST */ shader_hw_map2gl,
2176 /* WINED3DSIH_DSX */ NULL,
2177 /* WINED3DSIH_DSY */ NULL,
2178 /* WINED3DSIH_ELSE */ NULL,
2179 /* WINED3DSIH_ENDIF */ NULL,
2180 /* WINED3DSIH_ENDLOOP */ NULL,
2181 /* WINED3DSIH_ENDREP */ NULL,
2182 /* WINED3DSIH_EXP */ shader_hw_map2gl,
2183 /* WINED3DSIH_EXPP */ shader_hw_map2gl,
2184 /* WINED3DSIH_FRC */ shader_hw_map2gl,
2185 /* WINED3DSIH_IF */ NULL,
2186 /* WINED3DSIH_IFC */ NULL,
2187 /* WINED3DSIH_LABEL */ NULL,
2188 /* WINED3DSIH_LIT */ shader_hw_map2gl,
2189 /* WINED3DSIH_LOG */ shader_hw_map2gl,
2190 /* WINED3DSIH_LOGP */ shader_hw_map2gl,
2191 /* WINED3DSIH_LOOP */ NULL,
2192 /* WINED3DSIH_LRP */ shader_hw_map2gl,
2193 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
2194 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
2195 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
2196 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
2197 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
2198 /* WINED3DSIH_MAD */ shader_hw_map2gl,
2199 /* WINED3DSIH_MAX */ shader_hw_map2gl,
2200 /* WINED3DSIH_MIN */ shader_hw_map2gl,
2201 /* WINED3DSIH_MOV */ shader_hw_mov,
2202 /* WINED3DSIH_MOVA */ shader_hw_mov,
2203 /* WINED3DSIH_MUL */ shader_hw_map2gl,
2204 /* WINED3DSIH_NOP */ shader_hw_map2gl,
2205 /* WINED3DSIH_NRM */ shader_hw_nrm,
2206 /* WINED3DSIH_PHASE */ NULL,
2207 /* WINED3DSIH_POW */ shader_hw_map2gl,
2208 /* WINED3DSIH_RCP */ vshader_hw_rsq_rcp,
2209 /* WINED3DSIH_REP */ NULL,
2210 /* WINED3DSIH_RET */ NULL,
2211 /* WINED3DSIH_RSQ */ vshader_hw_rsq_rcp,
2212 /* WINED3DSIH_SETP */ NULL,
2213 /* WINED3DSIH_SGE */ shader_hw_map2gl,
2214 /* WINED3DSIH_SGN */ NULL,
2215 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
2216 /* WINED3DSIH_SLT */ shader_hw_map2gl,
2217 /* WINED3DSIH_SUB */ shader_hw_map2gl,
2218 /* WINED3DSIH_TEX */ pshader_hw_tex,
2219 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
2220 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
2221 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
2222 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
2223 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
2224 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
2225 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
2226 /* WINED3DSIH_TEXLDD */ NULL,
2227 /* WINED3DSIH_TEXLDL */ NULL,
2228 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
2229 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
2230 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
2231 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
2232 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
2233 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
2234 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
2235 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
2236 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
2237 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
2238 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
2239 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
2242 const shader_backend_t arb_program_shader_backend = {
2243 shader_arb_instruction_handler_table,
2244 shader_arb_select,
2245 shader_arb_select_depth_blt,
2246 shader_arb_deselect_depth_blt,
2247 shader_arb_update_float_vertex_constants,
2248 shader_arb_update_float_pixel_constants,
2249 shader_arb_load_constants,
2250 shader_arb_color_correction,
2251 shader_arb_destroy,
2252 shader_arb_alloc,
2253 shader_arb_free,
2254 shader_arb_dirty_const,
2255 shader_arb_generate_pshader,
2256 shader_arb_generate_vshader,
2257 shader_arb_get_caps,
2258 shader_arb_color_fixup_supported,
2261 /* ARB_fragment_program fixed function pipeline replacement definitions */
2262 #define ARB_FFP_CONST_TFACTOR 0
2263 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
2264 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
2265 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
2266 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
2268 struct arbfp_ffp_desc
2270 struct ffp_frag_desc parent;
2271 GLuint shader;
2272 unsigned int num_textures_used;
2275 static void arbfp_enable(IWineD3DDevice *iface, BOOL enable) {
2276 if(enable) {
2277 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2278 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
2279 } else {
2280 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2281 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
2285 static HRESULT arbfp_alloc(IWineD3DDevice *iface) {
2286 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2287 struct shader_arb_priv *priv;
2288 /* Share private data between the shader backend and the pipeline replacement, if both
2289 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
2290 * if no pixel shader is bound or not
2292 if(This->shader_backend == &arb_program_shader_backend) {
2293 This->fragment_priv = This->shader_priv;
2294 } else {
2295 This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
2296 if(!This->fragment_priv) return E_OUTOFMEMORY;
2298 priv = (struct shader_arb_priv *) This->fragment_priv;
2299 priv->fragment_shaders = hash_table_create(ffp_frag_program_key_hash, ffp_frag_program_key_compare);
2300 priv->use_arbfp_fixed_func = TRUE;
2301 return WINED3D_OK;
2304 static void arbfp_free_ffpshader(void *value, void *gli) {
2305 const WineD3D_GL_Info *gl_info = gli;
2306 struct arbfp_ffp_desc *entry_arb = value;
2308 ENTER_GL();
2309 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
2310 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
2311 HeapFree(GetProcessHeap(), 0, entry_arb);
2312 LEAVE_GL();
2315 static void arbfp_free(IWineD3DDevice *iface) {
2316 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2317 struct shader_arb_priv *priv = (struct shader_arb_priv *) This->fragment_priv;
2319 hash_table_destroy(priv->fragment_shaders, arbfp_free_ffpshader, &This->adapter->gl_info);
2320 priv->use_arbfp_fixed_func = FALSE;
2322 if(This->shader_backend != &arb_program_shader_backend) {
2323 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
2327 static void arbfp_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps)
2329 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
2330 WINED3DTEXOPCAPS_SELECTARG1 |
2331 WINED3DTEXOPCAPS_SELECTARG2 |
2332 WINED3DTEXOPCAPS_MODULATE4X |
2333 WINED3DTEXOPCAPS_MODULATE2X |
2334 WINED3DTEXOPCAPS_MODULATE |
2335 WINED3DTEXOPCAPS_ADDSIGNED2X |
2336 WINED3DTEXOPCAPS_ADDSIGNED |
2337 WINED3DTEXOPCAPS_ADD |
2338 WINED3DTEXOPCAPS_SUBTRACT |
2339 WINED3DTEXOPCAPS_ADDSMOOTH |
2340 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
2341 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
2342 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
2343 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
2344 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
2345 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
2346 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
2347 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
2348 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
2349 WINED3DTEXOPCAPS_DOTPRODUCT3 |
2350 WINED3DTEXOPCAPS_MULTIPLYADD |
2351 WINED3DTEXOPCAPS_LERP |
2352 WINED3DTEXOPCAPS_BUMPENVMAP |
2353 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
2355 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
2357 caps->MaxTextureBlendStages = 8;
2358 caps->MaxSimultaneousTextures = min(GL_LIMITS(fragment_samplers), 8);
2360 caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
2362 #undef GLINFO_LOCATION
2364 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
2365 static void state_texfactor_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2366 float col[4];
2367 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2369 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2370 * application provided constants
2372 if(device->shader_backend == &arb_program_shader_backend) {
2373 if (use_ps(stateblock)) return;
2375 device = stateblock->wineD3DDevice;
2376 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
2377 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
2380 D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
2381 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
2382 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
2386 static void state_arb_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2387 float col[4];
2388 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2390 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2391 * application provided constants
2393 if(device->shader_backend == &arb_program_shader_backend) {
2394 if (use_ps(stateblock)) return;
2396 device = stateblock->wineD3DDevice;
2397 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
2398 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
2401 if(stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
2402 /* The specular color has no alpha */
2403 col[0] = 1.0; col[1] = 1.0;
2404 col[2] = 1.0; col[3] = 0.0;
2405 } else {
2406 col[0] = 0.0; col[1] = 0.0;
2407 col[2] = 0.0; col[3] = 0.0;
2409 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
2410 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
2413 static void set_bumpmat_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2414 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
2415 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2416 float mat[2][2];
2418 if (use_ps(stateblock))
2420 if(stage != 0 &&
2421 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.bumpmat[stage]) {
2422 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
2423 * anyway
2425 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2426 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2430 if(device->shader_backend == &arb_program_shader_backend) {
2431 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2432 return;
2434 } else if(device->shader_backend == &arb_program_shader_backend) {
2435 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
2436 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
2439 mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
2440 mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
2441 mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
2442 mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
2444 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
2445 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
2448 static void tex_bumpenvlum_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2449 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
2450 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2451 float param[4];
2453 if (use_ps(stateblock))
2455 if(stage != 0 &&
2456 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.luminanceparams[stage]) {
2457 /* The pixel shader has to know the luminance offset. Do a constants update if it
2458 * isn't scheduled anyway
2460 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2461 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2465 if(device->shader_backend == &arb_program_shader_backend) {
2466 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2467 return;
2469 } else if(device->shader_backend == &arb_program_shader_backend) {
2470 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
2471 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
2474 param[0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLSCALE]);
2475 param[1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET]);
2476 param[2] = 0.0;
2477 param[3] = 0.0;
2479 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
2480 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
2483 static const char *get_argreg(SHADER_BUFFER *buffer, DWORD argnum, unsigned int stage, DWORD arg) {
2484 const char *ret;
2486 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
2488 switch(arg & WINED3DTA_SELECTMASK) {
2489 case WINED3DTA_DIFFUSE:
2490 ret = "fragment.color.primary"; break;
2492 case WINED3DTA_CURRENT:
2493 if(stage == 0) ret = "fragment.color.primary";
2494 else ret = "ret";
2495 break;
2497 case WINED3DTA_TEXTURE:
2498 switch(stage) {
2499 case 0: ret = "tex0"; break;
2500 case 1: ret = "tex1"; break;
2501 case 2: ret = "tex2"; break;
2502 case 3: ret = "tex3"; break;
2503 case 4: ret = "tex4"; break;
2504 case 5: ret = "tex5"; break;
2505 case 6: ret = "tex6"; break;
2506 case 7: ret = "tex7"; break;
2507 default: ret = "unknown texture";
2509 break;
2511 case WINED3DTA_TFACTOR:
2512 ret = "tfactor"; break;
2514 case WINED3DTA_SPECULAR:
2515 ret = "fragment.color.secondary"; break;
2517 case WINED3DTA_TEMP:
2518 ret = "tempreg"; break;
2520 case WINED3DTA_CONSTANT:
2521 FIXME("Implement perstage constants\n");
2522 switch(stage) {
2523 case 0: ret = "const0"; break;
2524 case 1: ret = "const1"; break;
2525 case 2: ret = "const2"; break;
2526 case 3: ret = "const3"; break;
2527 case 4: ret = "const4"; break;
2528 case 5: ret = "const5"; break;
2529 case 6: ret = "const6"; break;
2530 case 7: ret = "const7"; break;
2531 default: ret = "unknown constant";
2533 break;
2535 default:
2536 return "unknown";
2539 if(arg & WINED3DTA_COMPLEMENT) {
2540 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
2541 if(argnum == 0) ret = "arg0";
2542 if(argnum == 1) ret = "arg1";
2543 if(argnum == 2) ret = "arg2";
2545 if(arg & WINED3DTA_ALPHAREPLICATE) {
2546 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
2547 if(argnum == 0) ret = "arg0";
2548 if(argnum == 1) ret = "arg1";
2549 if(argnum == 2) ret = "arg2";
2551 return ret;
2554 static void gen_ffp_instr(SHADER_BUFFER *buffer, unsigned int stage, BOOL color, BOOL alpha,
2555 DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2) {
2556 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
2557 unsigned int mul = 1;
2558 BOOL mul_final_dest = FALSE;
2560 if(color && alpha) dstmask = "";
2561 else if(color) dstmask = ".xyz";
2562 else dstmask = ".w";
2564 if(dst == tempreg) dstreg = "tempreg";
2565 else dstreg = "ret";
2567 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
2568 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
2569 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
2571 switch(op) {
2572 case WINED3DTOP_DISABLE:
2573 if(stage == 0) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
2574 break;
2576 case WINED3DTOP_SELECTARG2:
2577 arg1 = arg2;
2578 case WINED3DTOP_SELECTARG1:
2579 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
2580 break;
2582 case WINED3DTOP_MODULATE4X:
2583 mul = 2;
2584 case WINED3DTOP_MODULATE2X:
2585 mul *= 2;
2586 if(strcmp(dstreg, "result.color") == 0) {
2587 dstreg = "ret";
2588 mul_final_dest = TRUE;
2590 case WINED3DTOP_MODULATE:
2591 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2592 break;
2594 case WINED3DTOP_ADDSIGNED2X:
2595 mul = 2;
2596 if(strcmp(dstreg, "result.color") == 0) {
2597 dstreg = "ret";
2598 mul_final_dest = TRUE;
2600 case WINED3DTOP_ADDSIGNED:
2601 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2602 arg2 = "arg2";
2603 case WINED3DTOP_ADD:
2604 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2605 break;
2607 case WINED3DTOP_SUBTRACT:
2608 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2609 break;
2611 case WINED3DTOP_ADDSMOOTH:
2612 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
2613 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2614 break;
2616 case WINED3DTOP_BLENDCURRENTALPHA:
2617 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
2618 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2619 break;
2620 case WINED3DTOP_BLENDFACTORALPHA:
2621 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
2622 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2623 break;
2624 case WINED3DTOP_BLENDTEXTUREALPHA:
2625 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2626 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2627 break;
2628 case WINED3DTOP_BLENDDIFFUSEALPHA:
2629 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
2630 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2631 break;
2633 case WINED3DTOP_BLENDTEXTUREALPHAPM:
2634 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2635 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
2636 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
2637 break;
2639 /* D3DTOP_PREMODULATE ???? */
2641 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2642 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
2643 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2644 break;
2645 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2646 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
2647 break;
2648 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2649 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
2650 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
2651 break;
2652 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2653 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
2654 break;
2656 case WINED3DTOP_DOTPRODUCT3:
2657 mul = 4;
2658 if(strcmp(dstreg, "result.color") == 0) {
2659 dstreg = "ret";
2660 mul_final_dest = TRUE;
2662 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
2663 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2664 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
2665 break;
2667 case WINED3DTOP_MULTIPLYADD:
2668 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
2669 break;
2671 case WINED3DTOP_LERP:
2672 /* The msdn is not quite right here */
2673 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2674 break;
2676 case WINED3DTOP_BUMPENVMAP:
2677 case WINED3DTOP_BUMPENVMAPLUMINANCE:
2678 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
2679 break;
2681 default:
2682 FIXME("Unhandled texture op %08x\n", op);
2685 if(mul == 2) {
2686 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2687 } else if(mul == 4) {
2688 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2692 /* The stateblock is passed for GLINFO_LOCATION */
2693 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, IWineD3DStateBlockImpl *stateblock)
2695 unsigned int stage;
2696 SHADER_BUFFER buffer;
2697 BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2698 BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2699 BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2700 const char *textype;
2701 const char *instr, *sat;
2702 char colorcor_dst[8];
2703 GLuint ret;
2704 DWORD arg0, arg1, arg2;
2705 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
2706 BOOL op_equal;
2707 const char *final_combiner_src = "ret";
2709 /* Find out which textures are read */
2710 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2711 if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
2712 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
2713 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
2714 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
2715 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2716 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2717 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2719 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
2720 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
2721 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
2722 bump_used[stage] = TRUE;
2723 tex_read[stage] = TRUE;
2725 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2726 bump_used[stage] = TRUE;
2727 tex_read[stage] = TRUE;
2728 luminance_used[stage] = TRUE;
2729 } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
2730 tfactor_used = TRUE;
2733 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2734 tfactor_used = TRUE;
2737 if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
2738 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2739 tempreg_used = TRUE;
2742 if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
2743 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
2744 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
2745 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
2746 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2747 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2748 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2750 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2751 tempreg_used = TRUE;
2753 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2754 tfactor_used = TRUE;
2758 /* Shader header */
2759 shader_buffer_init(&buffer);
2761 shader_addline(&buffer, "!!ARBfp1.0\n");
2763 switch(settings->fog) {
2764 case FOG_OFF: break;
2765 case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
2766 case FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
2767 case FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
2768 default: FIXME("Unexpected fog setting %d\n", settings->fog);
2771 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
2772 shader_addline(&buffer, "TEMP TMP;\n");
2773 shader_addline(&buffer, "TEMP ret;\n");
2774 if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
2775 shader_addline(&buffer, "TEMP arg0;\n");
2776 shader_addline(&buffer, "TEMP arg1;\n");
2777 shader_addline(&buffer, "TEMP arg2;\n");
2778 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2779 if(!tex_read[stage]) continue;
2780 shader_addline(&buffer, "TEMP tex%u;\n", stage);
2781 if(!bump_used[stage]) continue;
2782 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
2783 if(!luminance_used[stage]) continue;
2784 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
2786 if(tfactor_used) {
2787 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
2789 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
2791 if(settings->sRGB_write) {
2792 shader_addline(&buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
2793 srgb_mul_low, srgb_mul_low, srgb_mul_low);
2794 shader_addline(&buffer, "PARAM srgb_comparison = {%f, %f, %f, %f};\n",
2795 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
2796 shader_addline(&buffer, "PARAM srgb_pow = {%f, %f, %f, 1.0};\n",
2797 srgb_pow, srgb_pow, srgb_pow);
2798 shader_addline(&buffer, "PARAM srgb_mul_hi = {%f, %f, %f, 1.0};\n",
2799 srgb_mul_high, srgb_mul_high, srgb_mul_high);
2800 shader_addline(&buffer, "PARAM srgb_sub_hi = {%f, %f, %f, 0.0};\n",
2801 srgb_sub_high, srgb_sub_high, srgb_sub_high);
2804 /* Generate texture sampling instructions) */
2805 for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
2806 if(!tex_read[stage]) continue;
2808 switch(settings->op[stage].tex_type) {
2809 case tex_1d: textype = "1D"; break;
2810 case tex_2d: textype = "2D"; break;
2811 case tex_3d: textype = "3D"; break;
2812 case tex_cube: textype = "CUBE"; break;
2813 case tex_rect: textype = "RECT"; break;
2814 default: textype = "unexpected_textype"; break;
2817 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
2818 settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2819 sat = "";
2820 } else {
2821 sat = "_SAT";
2824 if(settings->op[stage].projected == proj_none) {
2825 instr = "TEX";
2826 } else if(settings->op[stage].projected == proj_count4 ||
2827 settings->op[stage].projected == proj_count3) {
2828 instr = "TXP";
2829 } else {
2830 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
2831 instr = "TXP";
2834 if(stage > 0 &&
2835 (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
2836 settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
2837 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
2838 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
2839 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
2840 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
2842 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2843 * so multiply the displacement with the dividing parameter before passing it to TXP
2845 if (settings->op[stage].projected != proj_none) {
2846 if(settings->op[stage].projected == proj_count4) {
2847 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
2848 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
2849 } else {
2850 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
2851 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
2853 } else {
2854 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
2857 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
2858 instr, sat, stage, stage, textype);
2859 if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2860 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
2861 stage - 1, stage - 1, stage - 1);
2862 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
2864 } else if(settings->op[stage].projected == proj_count3) {
2865 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
2866 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
2867 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
2868 instr, sat, stage, stage, textype);
2869 } else {
2870 shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
2871 instr, sat, stage, stage, stage, textype);
2874 sprintf(colorcor_dst, "tex%u", stage);
2875 gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
2876 settings->op[stage].color_fixup);
2879 /* Generate the main shader */
2880 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2881 if(settings->op[stage].cop == WINED3DTOP_DISABLE) {
2882 if(stage == 0) {
2883 final_combiner_src = "fragment.color.primary";
2885 break;
2888 if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
2889 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
2890 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
2891 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
2892 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
2893 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
2894 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
2895 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
2896 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
2897 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
2898 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
2899 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
2900 } else {
2901 op_equal = settings->op[stage].aop == settings->op[stage].cop &&
2902 settings->op[stage].carg0 == settings->op[stage].aarg0 &&
2903 settings->op[stage].carg1 == settings->op[stage].aarg1 &&
2904 settings->op[stage].carg2 == settings->op[stage].aarg2;
2907 if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
2908 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
2909 settings->op[stage].cop, settings->op[stage].carg0,
2910 settings->op[stage].carg1, settings->op[stage].carg2);
2911 if(stage == 0) {
2912 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
2914 } else if(op_equal) {
2915 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
2916 settings->op[stage].cop, settings->op[stage].carg0,
2917 settings->op[stage].carg1, settings->op[stage].carg2);
2918 } else {
2919 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
2920 settings->op[stage].cop, settings->op[stage].carg0,
2921 settings->op[stage].carg1, settings->op[stage].carg2);
2922 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
2923 settings->op[stage].aop, settings->op[stage].aarg0,
2924 settings->op[stage].aarg1, settings->op[stage].aarg2);
2928 if(settings->sRGB_write) {
2929 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
2930 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg");
2931 shader_addline(&buffer, "MOV result.color.w, ret.w;\n");
2932 } else {
2933 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
2936 /* Footer */
2937 shader_addline(&buffer, "END\n");
2939 /* Generate the shader */
2940 GL_EXTCALL(glGenProgramsARB(1, &ret));
2941 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
2942 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
2944 if (glGetError() == GL_INVALID_OPERATION) {
2945 GLint pos;
2946 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
2947 FIXME("Fragment program error at position %d: %s\n", pos,
2948 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2950 shader_buffer_free(&buffer);
2951 return ret;
2954 static void fragment_prog_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2955 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2956 struct shader_arb_priv *priv = (struct shader_arb_priv *) device->fragment_priv;
2957 BOOL use_pshader = use_ps(stateblock);
2958 BOOL use_vshader = use_vs(stateblock);
2959 struct ffp_frag_settings settings;
2960 const struct arbfp_ffp_desc *desc;
2961 unsigned int i;
2963 if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
2964 if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
2965 /* Reload fixed function constants since they collide with the pixel shader constants */
2966 for(i = 0; i < MAX_TEXTURES; i++) {
2967 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
2969 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
2970 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
2971 } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
2972 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
2974 return;
2977 if(!use_pshader) {
2978 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
2979 gen_ffp_frag_op(stateblock, &settings, FALSE);
2980 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(priv->fragment_shaders, &settings);
2981 if(!desc) {
2982 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
2983 if (!new_desc)
2985 ERR("Out of memory\n");
2986 return;
2988 new_desc->num_textures_used = 0;
2989 for(i = 0; i < GL_LIMITS(texture_stages); i++) {
2990 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
2991 new_desc->num_textures_used = i;
2994 memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
2995 new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
2996 add_ffp_frag_shader(priv->fragment_shaders, &new_desc->parent);
2997 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
2998 desc = new_desc;
3001 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
3002 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
3003 * deactivate it.
3005 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
3006 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
3007 priv->current_fprogram_id = desc->shader;
3009 if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
3010 /* Reload fixed function constants since they collide with the pixel shader constants */
3011 for(i = 0; i < MAX_TEXTURES; i++) {
3012 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
3014 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
3015 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
3019 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
3020 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
3021 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
3022 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
3023 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
3025 * Don't call shader_select if the vertex shader is dirty, because some shader backends(GLSL) need both shaders
3026 * to be compiled before activating them(needs some cleanups in the shader backend interface)
3028 if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3029 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3031 if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
3032 device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
3035 if(use_pshader) {
3036 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
3040 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
3041 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
3042 * state table, so we need to handle that with a forwarding function. The other invisible side effect
3043 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
3044 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
3046 static void state_arbfp_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3047 enum fogsource new_source;
3049 if(!isStateDirty(context, STATE_PIXELSHADER)) {
3050 fragment_prog_arbfp(state, stateblock, context);
3053 if(!stateblock->renderState[WINED3DRS_FOGENABLE]) return;
3055 if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
3056 if(use_vs(stateblock)) {
3057 new_source = FOGSOURCE_VS;
3058 } else {
3059 if(stateblock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || context->last_was_rhw) {
3060 new_source = FOGSOURCE_COORD;
3061 } else {
3062 new_source = FOGSOURCE_FFP;
3065 } else {
3066 new_source = FOGSOURCE_FFP;
3068 if(new_source != context->fog_source) {
3069 context->fog_source = new_source;
3070 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART), stateblock, context);
3074 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3075 if(!isStateDirty(context, STATE_PIXELSHADER)) {
3076 fragment_prog_arbfp(state, stateblock, context);
3080 #undef GLINFO_LOCATION
3082 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
3083 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR), state_texfactor_arbfp }, 0 },
3084 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3085 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3086 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3087 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3088 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3089 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3090 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3091 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3092 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3093 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3094 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3095 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3096 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3097 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3098 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3099 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3100 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3101 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3102 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3103 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3104 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3105 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3106 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3107 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3108 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3109 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3110 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3111 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3112 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3113 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3114 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3115 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3116 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3117 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3118 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3119 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3120 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3121 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3122 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3123 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3124 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3125 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3126 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3127 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3128 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3129 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3130 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3131 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3132 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3133 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3134 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3135 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3136 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3137 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3138 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3139 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3140 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3141 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3142 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3143 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3144 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3145 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3146 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3147 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3148 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3149 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3150 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3151 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3152 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3153 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3154 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3155 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3156 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3157 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3158 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3159 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3160 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3161 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3162 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3163 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3164 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3165 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3166 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3167 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3168 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3169 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3170 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3171 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3172 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3173 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3174 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3175 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3176 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3177 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3178 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3179 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3180 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3181 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3182 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3183 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3184 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3185 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3186 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3187 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3188 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3189 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3190 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3191 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3192 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3193 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3194 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3195 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3196 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3197 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3198 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3199 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3200 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3201 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3202 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3203 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3204 { STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim }, 0 },
3205 { STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim }, 0 },
3206 { STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim }, 0 },
3207 { STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim }, 0 },
3208 { STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim }, 0 },
3209 { STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim }, 0 },
3210 { STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim }, 0 },
3211 { STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim }, 0 },
3212 { STATE_PIXELSHADER, { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3213 { STATE_RENDER(WINED3DRS_FOGENABLE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3214 { STATE_RENDER(WINED3DRS_FOGTABLEMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3215 { STATE_RENDER(WINED3DRS_FOGVERTEXMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3216 { STATE_RENDER(WINED3DRS_FOGSTART), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, 0 },
3217 { STATE_RENDER(WINED3DRS_FOGEND), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, 0 },
3218 { STATE_RENDER(WINED3DRS_SRGBWRITEENABLE), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3219 { STATE_RENDER(WINED3DRS_FOGCOLOR), { STATE_RENDER(WINED3DRS_FOGCOLOR), state_fogcolor }, 0 },
3220 { STATE_RENDER(WINED3DRS_FOGDENSITY), { STATE_RENDER(WINED3DRS_FOGDENSITY), state_fogdensity }, 0 },
3221 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3222 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3223 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3224 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3225 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3226 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3227 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3228 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3229 { STATE_RENDER(WINED3DRS_SPECULARENABLE), { STATE_RENDER(WINED3DRS_SPECULARENABLE), state_arb_specularenable}, 0 },
3230 {0 /* Terminate */, { 0, 0 }, 0 },
3233 const struct fragment_pipeline arbfp_fragment_pipeline = {
3234 arbfp_enable,
3235 arbfp_get_caps,
3236 arbfp_alloc,
3237 arbfp_free,
3238 shader_arb_color_fixup_supported,
3239 arbfp_fragmentstate_template,
3240 TRUE /* We can disable projected textures */
3243 #define GLINFO_LOCATION device->adapter->gl_info
3245 struct arbfp_blit_priv {
3246 GLenum yuy2_rect_shader, yuy2_2d_shader;
3247 GLenum uyvy_rect_shader, uyvy_2d_shader;
3248 GLenum yv12_rect_shader, yv12_2d_shader;
3251 static HRESULT arbfp_blit_alloc(IWineD3DDevice *iface) {
3252 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3253 device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
3254 if(!device->blit_priv) {
3255 ERR("Out of memory\n");
3256 return E_OUTOFMEMORY;
3258 return WINED3D_OK;
3260 static void arbfp_blit_free(IWineD3DDevice *iface) {
3261 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3262 struct arbfp_blit_priv *priv = (struct arbfp_blit_priv *) device->blit_priv;
3264 ENTER_GL();
3265 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
3266 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
3267 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
3268 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
3269 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
3270 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
3271 checkGLcall("Delete yuv programs\n");
3272 LEAVE_GL();
3275 static BOOL gen_planar_yuv_read(SHADER_BUFFER *buffer, enum yuv_fixup yuv_fixup, GLenum textype, char *luminance)
3277 char chroma;
3278 const char *tex, *texinstr;
3280 if (yuv_fixup == YUV_FIXUP_UYVY) {
3281 chroma = 'x';
3282 *luminance = 'w';
3283 } else {
3284 chroma = 'w';
3285 *luminance = 'x';
3287 switch(textype) {
3288 case GL_TEXTURE_2D: tex = "2D"; texinstr = "TXP"; break;
3289 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; texinstr = "TEX"; break;
3290 default:
3291 /* This is more tricky than just replacing the texture type - we have to navigate
3292 * properly in the texture to find the correct chroma values
3294 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
3295 return FALSE;
3298 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
3299 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
3300 * filtering when we sample the texture.
3302 * These are the rules for reading the chroma:
3304 * Even pixel: Cr
3305 * Even pixel: U
3306 * Odd pixel: V
3308 * So we have to get the sampling x position in non-normalized coordinates in integers
3310 if(textype != GL_TEXTURE_RECTANGLE_ARB) {
3311 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
3312 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
3313 } else {
3314 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3316 /* We must not allow filtering between pixel x and x+1, this would mix U and V
3317 * Vertical filtering is ok. However, bear in mind that the pixel center is at
3318 * 0.5, so add 0.5.
3320 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
3321 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
3323 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
3324 * even and odd pixels respectively
3326 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
3327 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
3329 /* Sample Pixel 1 */
3330 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3332 /* Put the value into either of the chroma values */
3333 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3334 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
3335 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3336 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
3338 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
3339 * the pixel right to the current one. Otherwise, sample the left pixel.
3340 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
3342 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
3343 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
3344 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3346 /* Put the value into the other chroma */
3347 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3348 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
3349 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3350 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
3352 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
3353 * the current one and lerp the two U and V values
3356 /* This gives the correctly filtered luminance value */
3357 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
3359 return TRUE;
3362 static BOOL gen_yv12_read(SHADER_BUFFER *buffer, GLenum textype, char *luminance)
3364 const char *tex;
3366 switch(textype) {
3367 case GL_TEXTURE_2D: tex = "2D"; break;
3368 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; break;
3369 default:
3370 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
3371 return FALSE;
3374 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
3375 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
3376 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
3377 * pitch of the luminance plane, the packing into the gl texture is a bit
3378 * unfortunate. If the whole texture is interpreted as luminance data it looks
3379 * approximately like this:
3381 * +----------------------------------+----
3382 * | |
3383 * | |
3384 * | |
3385 * | |
3386 * | | 2
3387 * | LUMINANCE | -
3388 * | | 3
3389 * | |
3390 * | |
3391 * | |
3392 * | |
3393 * +----------------+-----------------+----
3394 * | | |
3395 * | U even rows | U odd rows |
3396 * | | | 1
3397 * +----------------+------------------ -
3398 * | | | 3
3399 * | V even rows | V odd rows |
3400 * | | |
3401 * +----------------+-----------------+----
3402 * | | |
3403 * | 0.5 | 0.5 |
3405 * So it appears as if there are 4 chroma images, but in fact the odd rows
3406 * in the chroma images are in the same row as the even ones. So its is
3407 * kinda tricky to read
3409 * When reading from rectangle textures, keep in mind that the input y coordinates
3410 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
3412 shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
3413 2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
3415 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3416 /* the chroma planes have only half the width */
3417 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
3419 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
3420 * the coordinate. Also read the right side of the image when reading odd lines
3422 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
3423 * bleeding
3425 if(textype == GL_TEXTURE_2D) {
3427 shader_addline(buffer, "RCP chroma.w, size.y;\n");
3429 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
3431 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
3432 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
3434 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3435 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3436 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3437 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3438 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3440 /* clamp, keep the half pixel origin in mind */
3441 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
3442 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3443 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
3444 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3445 } else {
3446 /* Read from [size - size+size/4] */
3447 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3448 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
3450 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3451 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3452 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3453 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3454 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
3455 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3457 /* Make sure to read exactly from the pixel center */
3458 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3459 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
3461 /* Clamp */
3462 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
3463 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
3464 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3465 shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
3466 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3468 /* Read the texture, put the result into the output register */
3469 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3470 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
3472 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
3473 * No need to clamp because we're just reusing the already clamped value from above
3475 if(textype == GL_TEXTURE_2D) {
3476 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
3477 } else {
3478 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
3480 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3481 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
3483 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
3484 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
3485 * values due to filtering
3487 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3488 if(textype == GL_TEXTURE_2D) {
3489 /* Multiply the y coordinate by 2/3 and clamp it */
3490 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
3491 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
3492 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3493 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3494 } else {
3495 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
3496 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
3497 * is bigger
3499 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
3500 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
3501 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3503 *luminance = 'a';
3505 return TRUE;
3508 static GLuint gen_yuv_shader(IWineD3DDeviceImpl *device, enum yuv_fixup yuv_fixup, GLenum textype)
3510 GLenum shader;
3511 SHADER_BUFFER buffer;
3512 char luminance_component;
3513 struct arbfp_blit_priv *priv = (struct arbfp_blit_priv *) device->blit_priv;
3515 /* Shader header */
3516 shader_buffer_init(&buffer);
3518 ENTER_GL();
3519 GL_EXTCALL(glGenProgramsARB(1, &shader));
3520 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
3521 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3522 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3523 LEAVE_GL();
3524 if(!shader) {
3525 shader_buffer_free(&buffer);
3526 return 0;
3529 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
3530 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
3531 * two chroma(U and V) values. Each macropixel has two luminance values, one for
3532 * each single pixel it contains, and one U and one V value shared between both
3533 * pixels.
3535 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
3536 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
3537 * take the format into account when generating the read swizzles
3539 * Reading the Y value is straightforward - just sample the texture. The hardware
3540 * takes care of filtering in the horizontal and vertical direction.
3542 * Reading the U and V values is harder. We have to avoid filtering horizontally,
3543 * because that would mix the U and V values of one pixel or two adjacent pixels.
3544 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
3545 * regardless of the filtering setting. Vertical filtering works automatically
3546 * though - the U and V values of two rows are mixed nicely.
3548 * Appart of avoiding filtering issues, the code has to know which value it just
3549 * read, and where it can find the other one. To determine this, it checks if
3550 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
3552 * Handling horizontal filtering of U and V values requires reading a 2nd pair
3553 * of pixels, extracting U and V and mixing them. This is not implemented yet.
3555 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
3556 * with width / 2. This way one read gives all 3 values, finding U and V is easy
3557 * in an unfiltered situation. Finding the luminance on the other hand requires
3558 * finding out if it is an odd or even pixel. The real drawback of this approach
3559 * is filtering. This would have to be emulated completely in the shader, reading
3560 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
3561 * vertically. Beyond that it would require adjustments to the texture handling
3562 * code to deal with the width scaling
3564 shader_addline(&buffer, "!!ARBfp1.0\n");
3565 shader_addline(&buffer, "TEMP luminance;\n");
3566 shader_addline(&buffer, "TEMP temp;\n");
3567 shader_addline(&buffer, "TEMP chroma;\n");
3568 shader_addline(&buffer, "TEMP texcrd;\n");
3569 shader_addline(&buffer, "TEMP texcrd2;\n");
3570 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
3571 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
3572 shader_addline(&buffer, "PARAM size = program.local[0];\n");
3574 switch (yuv_fixup)
3576 case YUV_FIXUP_UYVY:
3577 case YUV_FIXUP_YUY2:
3578 if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
3580 shader_buffer_free(&buffer);
3581 return 0;
3583 break;
3585 case YUV_FIXUP_YV12:
3586 if (!gen_yv12_read(&buffer, textype, &luminance_component))
3588 shader_buffer_free(&buffer);
3589 return 0;
3591 break;
3593 default:
3594 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3595 shader_buffer_free(&buffer);
3596 return 0;
3599 /* Calculate the final result. Formula is taken from
3600 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
3601 * ranges from -0.5 to 0.5
3603 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
3605 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
3606 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
3607 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
3608 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
3609 shader_addline(&buffer, "END\n");
3611 ENTER_GL();
3612 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
3614 if (glGetError() == GL_INVALID_OPERATION) {
3615 GLint pos;
3616 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3617 FIXME("Fragment program error at position %d: %s\n", pos,
3618 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3620 shader_buffer_free(&buffer);
3621 LEAVE_GL();
3623 switch (yuv_fixup)
3625 case YUV_FIXUP_YUY2:
3626 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
3627 else priv->yuy2_2d_shader = shader;
3628 break;
3630 case YUV_FIXUP_UYVY:
3631 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
3632 else priv->uyvy_2d_shader = shader;
3633 break;
3635 case YUV_FIXUP_YV12:
3636 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
3637 else priv->yv12_2d_shader = shader;
3638 break;
3641 return shader;
3644 static HRESULT arbfp_blit_set(IWineD3DDevice *iface, WINED3DFORMAT fmt, GLenum textype, UINT width, UINT height) {
3645 GLenum shader;
3646 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3647 float size[4] = {width, height, 1, 1};
3648 struct arbfp_blit_priv *priv = (struct arbfp_blit_priv *) device->blit_priv;
3649 const struct GlPixelFormatDesc *glDesc;
3650 enum yuv_fixup yuv_fixup;
3652 getFormatDescEntry(fmt, &GLINFO_LOCATION, &glDesc);
3654 if (!is_yuv_fixup(glDesc->color_fixup))
3656 TRACE("Fixup:\n");
3657 dump_color_fixup_desc(glDesc->color_fixup);
3658 /* Don't bother setting up a shader for unconverted formats */
3659 ENTER_GL();
3660 glEnable(textype);
3661 checkGLcall("glEnable(textype)");
3662 LEAVE_GL();
3663 return WINED3D_OK;
3666 yuv_fixup = get_yuv_fixup(glDesc->color_fixup);
3668 switch(yuv_fixup)
3670 case YUV_FIXUP_YUY2:
3671 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
3672 break;
3674 case YUV_FIXUP_UYVY:
3675 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
3676 break;
3678 case YUV_FIXUP_YV12:
3679 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
3680 break;
3682 default:
3683 FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup);
3684 ENTER_GL();
3685 glEnable(textype);
3686 checkGLcall("glEnable(textype)");
3687 LEAVE_GL();
3688 return E_NOTIMPL;
3691 if (!shader) shader = gen_yuv_shader(device, yuv_fixup, textype);
3693 ENTER_GL();
3694 glEnable(GL_FRAGMENT_PROGRAM_ARB);
3695 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
3696 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3697 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3698 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
3699 checkGLcall("glProgramLocalParameter4fvARB");
3700 LEAVE_GL();
3702 return WINED3D_OK;
3705 static void arbfp_blit_unset(IWineD3DDevice *iface) {
3706 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3708 ENTER_GL();
3709 glDisable(GL_FRAGMENT_PROGRAM_ARB);
3710 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3711 glDisable(GL_TEXTURE_2D);
3712 checkGLcall("glDisable(GL_TEXTURE_2D)");
3713 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
3714 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3715 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3717 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3718 glDisable(GL_TEXTURE_RECTANGLE_ARB);
3719 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
3721 LEAVE_GL();
3724 static BOOL arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup)
3726 enum yuv_fixup yuv_fixup;
3728 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3730 TRACE("Checking support for fixup:\n");
3731 dump_color_fixup_desc(fixup);
3734 if (is_identity_fixup(fixup))
3736 TRACE("[OK]\n");
3737 return TRUE;
3740 /* We only support YUV conversions. */
3741 if (!is_yuv_fixup(fixup))
3743 TRACE("[FAILED]\n");
3744 return FALSE;
3747 yuv_fixup = get_yuv_fixup(fixup);
3748 switch(yuv_fixup)
3750 case YUV_FIXUP_YUY2:
3751 case YUV_FIXUP_UYVY:
3752 case YUV_FIXUP_YV12:
3753 TRACE("[OK]\n");
3754 return TRUE;
3756 default:
3757 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3758 TRACE("[FAILED]\n");
3759 return FALSE;
3763 const struct blit_shader arbfp_blit = {
3764 arbfp_blit_alloc,
3765 arbfp_blit_free,
3766 arbfp_blit_set,
3767 arbfp_blit_unset,
3768 arbfp_blit_color_fixup_supported,
3771 #undef GLINFO_LOCATION