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
13 * Copyright 2009 Henri Verbeet for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
42 #define GLINFO_LOCATION (*gl_info)
44 /* GL locking for state handlers is done by the caller. */
45 static BOOL need_mova_const(IWineD3DBaseShader *shader, const WineD3D_GL_Info *gl_info) {
46 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) shader;
47 if(!This->baseShader.reg_maps.usesmova) return FALSE;
48 return !GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION);
51 static BOOL need_helper_const(const WineD3D_GL_Info *gl_info) {
52 if(!GL_SUPPORT(NV_VERTEX_PROGRAM) || /* Need to init colors */
53 gl_info->arb_vs_offset_limit || /* Have to init texcoords */
54 gl_info->set_texcoord_w) { /* Load the immval offset */
60 static unsigned int reserved_vs_const(IWineD3DBaseShader *shader, const WineD3D_GL_Info *gl_info) {
62 /* We use one PARAM for the pos fixup, and in some cases one to load
63 * some immediate values into the shader
65 if(need_helper_const(gl_info)) ret++;
66 if(need_mova_const(shader, gl_info)) ret++;
70 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
71 * so upload them above that
73 #define ARB_SHADER_PRIVCONST_BASE (GL_LIMITS(vshader_constantsF) - 1)
74 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
76 /* ARB_program_shader private data */
77 struct shader_arb_priv {
78 GLuint current_vprogram_id;
79 GLuint current_fprogram_id;
80 GLuint depth_blt_vprogram_id;
81 GLuint depth_blt_fprogram_id[tex_type_count];
82 BOOL use_arbfp_fixed_func;
83 struct hash_table_t *fragment_shaders;
86 struct shader_arb_ctx_priv {
89 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
91 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
93 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
98 /********************************************************
99 * ARB_[vertex/fragment]_program helper functions follow
100 ********************************************************/
103 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
104 * When constant_list == NULL, it will load all the constants.
106 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
107 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
109 /* GL locking is done by the caller */
110 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, const WineD3D_GL_Info *gl_info,
111 GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
113 local_constant* lconst;
117 if (TRACE_ON(d3d_shader)) {
118 for(i = 0; i < max_constants; i++) {
119 if(!dirty_consts[i]) continue;
120 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
121 constants[i * 4 + 0], constants[i * 4 + 1],
122 constants[i * 4 + 2], constants[i * 4 + 3]);
125 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
126 if (target_type == GL_FRAGMENT_PROGRAM_ARB && This->baseShader.reg_maps.shader_version.major == 1)
129 for(i = 0; i < max_constants; i++) {
130 if(!dirty_consts[i]) continue;
134 if(constants[j + 0] > 1.0) lcl_const[0] = 1.0;
135 else if(constants[j + 0] < -1.0) lcl_const[0] = -1.0;
136 else lcl_const[0] = constants[j + 0];
138 if(constants[j + 1] > 1.0) lcl_const[1] = 1.0;
139 else if(constants[j + 1] < -1.0) lcl_const[1] = -1.0;
140 else lcl_const[1] = constants[j + 1];
142 if(constants[j + 2] > 1.0) lcl_const[2] = 1.0;
143 else if(constants[j + 2] < -1.0) lcl_const[2] = -1.0;
144 else lcl_const[2] = constants[j + 2];
146 if(constants[j + 3] > 1.0) lcl_const[3] = 1.0;
147 else if(constants[j + 3] < -1.0) lcl_const[3] = -1.0;
148 else lcl_const[3] = constants[j + 3];
150 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
153 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
154 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
155 * or just reloading *all* constants at once
157 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
159 for(i = 0; i < max_constants; i++) {
160 if(!dirty_consts[i]) continue;
162 /* Find the next block of dirty constants */
165 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
169 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
172 for(i = 0; i < max_constants; i++) {
173 if(dirty_consts[i]) {
175 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
180 checkGLcall("glProgramEnvParameter4fvARB()");
182 /* Load immediate constants */
183 if(This->baseShader.load_local_constsF) {
184 if (TRACE_ON(d3d_shader)) {
185 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
186 GLfloat* values = (GLfloat*)lconst->value;
187 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
188 values[0], values[1], values[2], values[3]);
191 /* Immediate constants are clamped for 1.X shaders at loading times */
193 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
194 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
195 ret = max(ret, lconst->idx + 1);
196 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
198 checkGLcall("glProgramEnvParameter4fvARB()");
199 return ret; /* The loaded immediate constants need reloading for the next shader */
201 return 0; /* No constants are dirty now */
206 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
208 static void shader_arb_load_np2fixup_constants(
209 IWineD3DDevice* device,
211 char useVertexShader) {
212 /* not implemented */
215 static inline void shader_arb_ps_local_constants(IWineD3DDeviceImpl* deviceImpl)
217 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
218 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
219 IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
220 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
223 for(i = 0; i < psi->numbumpenvmatconsts; i++)
225 /* The state manager takes care that this function is always called if the bump env matrix changes */
226 const float *data = (const float *)&stateBlock->textureState[(int) psi->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
227 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst[i].const_num, data));
229 if (psi->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED)
231 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
232 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
233 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
234 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
236 const float *scale = (const float *)&stateBlock->textureState[(int) psi->luminanceconst[i].texunit][WINED3DTSS_BUMPENVLSCALE];
237 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->luminanceconst[i].const_num, scale));
242 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
244 * We only support float constants in ARB at the moment, so don't
245 * worry about the Integers or Booleans
247 /* GL locking is done by the caller (state handler) */
248 static void shader_arb_load_constants(
249 IWineD3DDevice* device,
251 char useVertexShader) {
253 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
254 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
255 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
257 if (useVertexShader) {
258 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
260 /* Load DirectX 9 float constants for vertex shader */
261 deviceImpl->highest_dirty_vs_const = shader_arb_load_constantsF(
262 vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
263 deviceImpl->highest_dirty_vs_const,
264 stateBlock->vertexShaderConstantF,
265 deviceImpl->activeContext->vshader_const_dirty);
267 /* Upload the position fixup */
268 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
271 if (usePixelShader) {
272 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
274 /* Load DirectX 9 float constants for pixel shader */
275 deviceImpl->highest_dirty_ps_const = shader_arb_load_constantsF(
276 pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
277 deviceImpl->highest_dirty_ps_const,
278 stateBlock->pixelShaderConstantF,
279 deviceImpl->activeContext->pshader_const_dirty);
280 shader_arb_ps_local_constants(deviceImpl);
284 static void shader_arb_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
286 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
288 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
289 * context. On a context switch the old context will be fully dirtified */
290 memset(This->activeContext->vshader_const_dirty + start, 1,
291 sizeof(*This->activeContext->vshader_const_dirty) * count);
292 This->highest_dirty_vs_const = max(This->highest_dirty_vs_const, start + count + 1);
295 static void shader_arb_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
297 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
299 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
300 * context. On a context switch the old context will be fully dirtified */
301 memset(This->activeContext->pshader_const_dirty + start, 1,
302 sizeof(*This->activeContext->pshader_const_dirty) * count);
303 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, start + count + 1);
306 static DWORD *local_const_mapping(IWineD3DBaseShaderImpl *This)
310 const local_constant *lconst;
312 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) return NULL;
314 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * This->baseShader.limits.temporary);
316 ERR("Out of memory\n");
320 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
321 ret[lconst->idx] = idx++;
326 /* Generate the variable & register declarations for the ARB_vertex_program output target */
327 static void shader_generate_arb_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
328 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info, DWORD *lconst_map)
330 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
331 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
332 DWORD i, cur, next_local = 0;
333 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
334 unsigned max_constantsF;
335 const local_constant *lconst;
337 /* In pixel shaders, all private constants are program local, we don't need anything
338 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
339 * If we need a private constant the GL implementation will squeeze it in somewhere
341 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
342 * immediate values. The posFixup is loaded using program.env for now, so always
343 * subtract one from the number of constants. If the shader uses indirect addressing,
344 * account for the helper const too because we have to declare all availabke d3d constants
345 * and don't know which are actually used.
348 max_constantsF = GL_LIMITS(pshader_constantsF);
350 if(This->baseShader.reg_maps.usesrelconstF) {
351 max_constantsF = GL_LIMITS(vshader_constantsF) - reserved_vs_const(iface, gl_info);
353 max_constantsF = GL_LIMITS(vshader_constantsF) - 1;
355 /* Temporary Output register */
356 shader_addline(buffer, "TEMP TMP_OUT;\n");
359 for(i = 0; i < This->baseShader.limits.temporary; i++) {
360 if (reg_maps->temporary[i])
361 shader_addline(buffer, "TEMP R%u;\n", i);
364 for (i = 0; i < This->baseShader.limits.address; i++) {
365 if (reg_maps->address[i])
366 shader_addline(buffer, "ADDRESS A%d;\n", i);
369 if(pshader && reg_maps->shader_version.major == 1 && reg_maps->shader_version.minor <= 3) {
370 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
371 if (reg_maps->texcoord[i] && pshader)
372 shader_addline(buffer,"TEMP T%u;\n", i);
376 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] && pshader) {
377 shader_addline(buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
378 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
379 shader_addline(buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
380 srgb_sub_high, 0.0, 0.0, 0.0);
383 /* Load local constants using the program-local space,
384 * this avoids reloading them each time the shader is used
387 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
388 shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
389 lconst_map[lconst->idx]);
390 next_local = max(next_local, lconst_map[lconst->idx] + 1);
394 /* we use the array-based constants array if the local constants are marked for loading,
395 * because then we use indirect addressing, or when the local constant list is empty,
396 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
397 * local constants do not declare the loaded constants as an array because ARB compilers usually
398 * do not optimize unused constants away
400 if(This->baseShader.reg_maps.usesrelconstF) {
401 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
402 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
403 max_constantsF, max_constantsF - 1);
405 for(i = 0; i < max_constantsF; i++) {
408 mask = 1 << (i & 0x1f);
409 if(!shader_constant_is_local(This, i) && (This->baseShader.reg_maps.constf[idx] & mask)) {
410 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
415 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
416 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
417 if(!reg_maps->bumpmat[i]) continue;
419 cur = ps->numbumpenvmatconsts;
420 ps->bumpenvmatconst[cur].const_num = -1;
421 ps->bumpenvmatconst[cur].texunit = i;
422 ps->luminanceconst[cur].const_num = -1;
423 ps->luminanceconst[cur].texunit = i;
425 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
426 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
427 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
428 * textures due to conditional NP2 restrictions)
430 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
431 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
432 * their location is shader dependent anyway and they cannot be loaded globally.
434 ps->bumpenvmatconst[cur].const_num = next_local++;
435 shader_addline(buffer, "PARAM bumpenvmat%d = program.local[%d];\n",
436 i, ps->bumpenvmatconst[cur].const_num);
437 ps->numbumpenvmatconsts = cur + 1;
439 if(!reg_maps->luminanceparams[i]) continue;
441 ((IWineD3DPixelShaderImpl *)This)->luminanceconst[cur].const_num = next_local++;
442 shader_addline(buffer, "PARAM luminance%d = program.local[%d];\n",
443 i, ps->luminanceconst[cur].const_num);
448 static const char * const shift_tab[] = {
449 "dummy", /* 0 (none) */
450 "coefmul.x", /* 1 (x2) */
451 "coefmul.y", /* 2 (x4) */
452 "coefmul.z", /* 3 (x8) */
453 "coefmul.w", /* 4 (x16) */
454 "dummy", /* 5 (x32) */
455 "dummy", /* 6 (x64) */
456 "dummy", /* 7 (x128) */
457 "dummy", /* 8 (d256) */
458 "dummy", /* 9 (d128) */
459 "dummy", /* 10 (d64) */
460 "dummy", /* 11 (d32) */
461 "coefdiv.w", /* 12 (d16) */
462 "coefdiv.z", /* 13 (d8) */
463 "coefdiv.y", /* 14 (d4) */
464 "coefdiv.x" /* 15 (d2) */
467 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction *ins,
468 const struct wined3d_shader_dst_param *dst, char *write_mask)
470 char *ptr = write_mask;
472 if (dst->write_mask != WINED3DSP_WRITEMASK_ALL)
475 if (dst->write_mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
476 if (dst->write_mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
477 if (dst->write_mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
478 if (dst->write_mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
484 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param *param, BOOL fixup, char *swizzle_str)
486 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
487 * but addressed as "rgba". To fix this we need to swap the register's x
488 * and z components. */
489 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
490 char *ptr = swizzle_str;
492 /* swizzle bits fields: wwzzyyxx */
493 DWORD swizzle = param->swizzle;
494 DWORD swizzle_x = swizzle & 0x03;
495 DWORD swizzle_y = (swizzle >> 2) & 0x03;
496 DWORD swizzle_z = (swizzle >> 4) & 0x03;
497 DWORD swizzle_w = (swizzle >> 6) & 0x03;
499 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
500 * generate a swizzle string. Unless we need to our own swizzling. */
501 if (swizzle != WINED3DSP_NOSWIZZLE || fixup)
504 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
505 *ptr++ = swizzle_chars[swizzle_x];
507 *ptr++ = swizzle_chars[swizzle_x];
508 *ptr++ = swizzle_chars[swizzle_y];
509 *ptr++ = swizzle_chars[swizzle_z];
510 *ptr++ = swizzle_chars[swizzle_w];
517 static void shader_arb_request_a0(const struct wined3d_shader_instruction *ins, const char *src)
519 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
520 SHADER_BUFFER *buffer = ins->ctx->buffer;
522 if(strcmp(priv->addr_reg, src) == 0) return;
524 strcpy(priv->addr_reg, src);
525 shader_addline(buffer, "ARL A0.x, %s;\n", src);
528 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
529 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr);
531 static void shader_arb_get_register_name(const struct wined3d_shader_instruction *ins,
532 const struct wined3d_shader_register *reg, char *register_name, BOOL *is_color)
534 /* oPos, oFog and oPts in D3D */
535 static const char * const rastout_reg_names[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
536 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
537 BOOL pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
538 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
544 case WINED3DSPR_TEMP:
545 sprintf(register_name, "R%u", reg->idx);
548 case WINED3DSPR_INPUT:
551 if (reg->idx == 0) strcpy(register_name, "fragment.color.primary");
552 else strcpy(register_name, "fragment.color.secondary");
556 if (((IWineD3DVertexShaderImpl *)This)->cur_args->swizzle_map & (1 << reg->idx)) *is_color = TRUE;
557 sprintf(register_name, "vertex.attrib[%u]", reg->idx);
561 case WINED3DSPR_CONST:
562 if (!pshader && reg->rel_addr)
565 UINT rel_offset = ((IWineD3DVertexShaderImpl *)This)->rel_offset;
566 if(This->baseShader.reg_maps.shader_version.major < 2) {
567 sprintf(rel_reg, "A0.x");
569 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
570 if(ctx->target_version == ARB) {
571 shader_arb_request_a0(ins, rel_reg);
572 sprintf(rel_reg, "A0.x");
575 if (reg->idx >= rel_offset)
576 sprintf(register_name, "C[%s + %u]", rel_reg, reg->idx - rel_offset);
578 sprintf(register_name, "C[%s - %u]", rel_reg, -reg->idx + rel_offset);
582 if (This->baseShader.reg_maps.usesrelconstF)
583 sprintf(register_name, "C[%u]", reg->idx);
585 sprintf(register_name, "C%u", reg->idx);
589 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
591 if(This->baseShader.reg_maps.shader_version.major == 1 &&
592 This->baseShader.reg_maps.shader_version.minor <= 3) {
593 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
594 * and as source to most instructions. For some instructions it is the texcoord
595 * input. Those instructions know about the special use
597 sprintf(register_name, "T%u", reg->idx);
599 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
600 sprintf(register_name, "fragment.texcoord[%u]", reg->idx);
605 if(This->baseShader.reg_maps.shader_version.major == 1 || ctx->target_version >= NV2)
607 sprintf(register_name, "A%u", reg->idx);
611 sprintf(register_name, "A%u_SHADOW", reg->idx);
616 case WINED3DSPR_COLOROUT:
619 if(((IWineD3DPixelShaderImpl *)This)->cur_args->srgb_correction)
621 strcpy(register_name, "TMP_COLOR");
625 strcpy(register_name, "result.color");
630 /* TODO: See GL_ARB_draw_buffers */
631 FIXME("Unsupported write to render target %u\n", reg->idx);
632 sprintf(register_name, "unsupported_register");
636 case WINED3DSPR_RASTOUT:
637 sprintf(register_name, "%s", rastout_reg_names[reg->idx]);
640 case WINED3DSPR_DEPTHOUT:
641 strcpy(register_name, "result.depth");
644 case WINED3DSPR_ATTROUT:
645 if (pshader) sprintf(register_name, "oD[%u]", reg->idx);
646 else if (reg->idx == 0) strcpy(register_name, "result.color.primary");
647 else strcpy(register_name, "result.color.secondary");
650 case WINED3DSPR_TEXCRDOUT:
651 if (pshader) sprintf(register_name, "oT[%u]", reg->idx);
652 else sprintf(register_name, "result.texcoord[%u]", reg->idx);
656 FIXME("Unhandled register type %#x[%u]\n", reg->type, reg->idx);
657 sprintf(register_name, "unrecognized_register[%u]", reg->idx);
662 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction *ins,
663 const struct wined3d_shader_dst_param *wined3d_dst, char *str)
665 char register_name[255];
669 shader_arb_get_register_name(ins, &wined3d_dst->reg, register_name, &is_color);
670 strcpy(str, register_name);
672 shader_arb_get_write_mask(ins, wined3d_dst, write_mask);
673 strcat(str, write_mask);
676 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
678 switch(channel_source)
680 case CHANNEL_SOURCE_ZERO: return "0";
681 case CHANNEL_SOURCE_ONE: return "1";
682 case CHANNEL_SOURCE_X: return "x";
683 case CHANNEL_SOURCE_Y: return "y";
684 case CHANNEL_SOURCE_Z: return "z";
685 case CHANNEL_SOURCE_W: return "w";
687 FIXME("Unhandled channel source %#x\n", channel_source);
692 static void gen_color_correction(SHADER_BUFFER *buffer, const char *reg, DWORD dst_mask,
693 const char *one, const char *two, struct color_fixup_desc fixup)
697 if (is_yuv_fixup(fixup))
699 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
700 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
705 if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
706 if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
707 if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
708 if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
713 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
714 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
715 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
719 if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
720 if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
721 if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
722 if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
728 char *ptr = reg_mask;
730 if (mask != WINED3DSP_WRITEMASK_ALL)
733 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
734 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
735 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
736 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
740 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
744 static void shader_hw_sample(const struct wined3d_shader_instruction *ins, DWORD sampler_idx,
745 const char *dst_str, const char *coord_reg, BOOL projected, BOOL bias)
747 SHADER_BUFFER *buffer = ins->ctx->buffer;
748 DWORD sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
749 const char *tex_type;
750 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
751 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
753 switch(sampler_type) {
759 if(device->stateBlock->textures[sampler_idx] &&
760 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
765 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
767 const IWineD3DPixelShaderImpl* const ps = (const IWineD3DPixelShaderImpl*)This;
768 if(ps->cur_args->np2_fixup & (1 << sampler_idx)) {
769 FIXME("NP2 texcoord fixup is currently not implemented in ARB mode (use GLSL instead).\n");
774 case WINED3DSTT_VOLUME:
778 case WINED3DSTT_CUBE:
783 ERR("Unexpected texture type %d\n", sampler_type);
788 /* Shouldn't be possible, but let's check for it */
789 if(projected) FIXME("Biased and Projected texture sampling\n");
790 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
791 shader_addline(buffer, "TXB %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
792 } else if (projected) {
793 shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
795 shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
798 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
800 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
801 gen_color_correction(buffer, dst_str, ins->dst[0].write_mask,
802 "one", "coefmul.x", ps->cur_args->color_fixup[sampler_idx]);
806 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
807 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr)
809 /* Generate a line that does the input modifier computation and return the input register to use */
810 BOOL is_color = FALSE;
814 SHADER_BUFFER *buffer = ins->ctx->buffer;
815 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
817 /* Assume a new line will be added */
820 /* Get register name */
821 shader_arb_get_register_name(ins, &src->reg, regstr, &is_color);
822 shader_arb_get_swizzle(src, is_color, swzstr);
824 switch (src->modifiers)
826 case WINED3DSPSM_NONE:
827 sprintf(outregstr, "%s%s", regstr, swzstr);
830 case WINED3DSPSM_NEG:
831 sprintf(outregstr, "-%s%s", regstr, swzstr);
834 case WINED3DSPSM_BIAS:
835 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
837 case WINED3DSPSM_BIASNEG:
838 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
840 case WINED3DSPSM_SIGN:
841 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
843 case WINED3DSPSM_SIGNNEG:
844 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
846 case WINED3DSPSM_COMP:
847 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
850 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
852 case WINED3DSPSM_X2NEG:
853 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
856 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
857 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
860 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
861 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
863 case WINED3DSPSM_ABS:
864 if(ctx->target_version >= NV2) {
865 sprintf(outregstr, "|%s%s|", regstr, swzstr);
868 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
871 case WINED3DSPSM_ABSNEG:
872 if(ctx->target_version >= NV2) {
873 sprintf(outregstr, "-|%s%s|", regstr, swzstr);
875 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
876 sprintf(outregstr, "-T%c%s", 'A' + tmpreg, swzstr);
881 sprintf(outregstr, "%s%s", regstr, swzstr);
885 /* Return modified or original register, with swizzle */
887 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
890 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction *ins)
893 const char *ret = "";
894 if (!ins->dst_count) return "";
896 mod = ins->dst[0].modifiers;
897 if(mod & WINED3DSPDM_SATURATE) {
899 mod &= ~WINED3DSPDM_SATURATE;
901 if(mod & WINED3DSPDM_PARTIALPRECISION) {
902 FIXME("Unhandled modifier WINED3DSPDM_PARTIALPRECISION\n");
903 mod &= ~WINED3DSPDM_PARTIALPRECISION;
905 if(mod & WINED3DSPDM_MSAMPCENTROID) {
906 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
907 mod &= ~WINED3DSPDM_MSAMPCENTROID;
910 FIXME("Unknown modifiers 0x%08x\n", mod);
915 static void pshader_hw_bem(const struct wined3d_shader_instruction *ins)
917 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
918 SHADER_BUFFER *buffer = ins->ctx->buffer;
920 char src_name[2][50];
921 DWORD sampler_code = dst->reg.idx;
923 shader_arb_get_dst_param(ins, dst, dst_name);
925 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
927 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
928 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
931 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
932 shader_addline(buffer, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
933 shader_addline(buffer, "DP3 TC.r, TA, %s;\n", src_name[1]);
934 shader_addline(buffer, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
935 shader_addline(buffer, "DP3 TC.g, TA, %s;\n", src_name[1]);
937 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
938 shader_addline(buffer, "ADD %s, %s, TC;\n", dst_name, src_name[0]);
941 static void pshader_hw_cnd(const struct wined3d_shader_instruction *ins)
943 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
944 SHADER_BUFFER *buffer = ins->ctx->buffer;
946 char src_name[3][50];
947 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
948 ins->ctx->reg_maps->shader_version.minor);
950 shader_arb_get_dst_param(ins, dst, dst_name);
951 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
953 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
954 if (shader_version <= WINED3D_SHADER_VERSION(1, 3) && ins->coissue)
956 shader_addline(buffer, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[1]);
958 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
959 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
960 shader_addline(buffer, "ADD TA, -%s, coefdiv.x;\n", src_name[0]);
961 shader_addline(buffer, "CMP%s %s, TA, %s, %s;\n",
962 shader_arb_get_modifier(ins), dst_name, src_name[1], src_name[2]);
966 static void pshader_hw_cmp(const struct wined3d_shader_instruction *ins)
968 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
969 SHADER_BUFFER *buffer = ins->ctx->buffer;
971 char src_name[3][50];
973 shader_arb_get_dst_param(ins, dst, dst_name);
975 /* Generate input register names (with modifiers) */
976 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
977 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
978 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
980 shader_addline(buffer, "CMP%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins), dst_name,
981 src_name[0], src_name[2], src_name[1]);
984 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
985 * dst = dot2(src0, src1) + src2 */
986 static void pshader_hw_dp2add(const struct wined3d_shader_instruction *ins)
988 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
989 SHADER_BUFFER *buffer = ins->ctx->buffer;
991 char src_name[3][50];
993 shader_arb_get_dst_param(ins, dst, dst_name);
994 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
995 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
996 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
998 /* Emulate a DP2 with a DP3 and 0.0. Don't use the dest as temp register, it could be src[1] or src[2]
999 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1001 shader_addline(buffer, "MOV TA, %s;\n", src_name[0]);
1002 shader_addline(buffer, "MOV TA.z, 0.0;\n");
1003 shader_addline(buffer, "DP3 TA, TA, %s;\n", src_name[1]);
1004 shader_addline(buffer, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[2]);
1007 /* Map the opcode 1-to-1 to the GL code */
1008 static void shader_hw_map2gl(const struct wined3d_shader_instruction *ins)
1010 SHADER_BUFFER *buffer = ins->ctx->buffer;
1011 const char *instruction;
1012 char arguments[256], dst_str[50];
1014 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1016 switch (ins->handler_idx)
1018 case WINED3DSIH_ABS: instruction = "ABS"; break;
1019 case WINED3DSIH_ADD: instruction = "ADD"; break;
1020 case WINED3DSIH_CRS: instruction = "XPD"; break;
1021 case WINED3DSIH_DP3: instruction = "DP3"; break;
1022 case WINED3DSIH_DP4: instruction = "DP4"; break;
1023 case WINED3DSIH_DST: instruction = "DST"; break;
1024 case WINED3DSIH_EXP: instruction = "EX2"; break;
1025 case WINED3DSIH_EXPP: instruction = "EXP"; break;
1026 case WINED3DSIH_FRC: instruction = "FRC"; break;
1027 case WINED3DSIH_LIT: instruction = "LIT"; break;
1028 case WINED3DSIH_LOG: instruction = "LG2"; break;
1029 case WINED3DSIH_LOGP: instruction = "LOG"; break;
1030 case WINED3DSIH_LRP: instruction = "LRP"; break;
1031 case WINED3DSIH_MAD: instruction = "MAD"; break;
1032 case WINED3DSIH_MAX: instruction = "MAX"; break;
1033 case WINED3DSIH_MIN: instruction = "MIN"; break;
1034 case WINED3DSIH_MOV: instruction = "MOV"; break;
1035 case WINED3DSIH_MUL: instruction = "MUL"; break;
1036 case WINED3DSIH_POW: instruction = "POW"; break;
1037 case WINED3DSIH_SGE: instruction = "SGE"; break;
1038 case WINED3DSIH_SLT: instruction = "SLT"; break;
1039 case WINED3DSIH_SUB: instruction = "SUB"; break;
1040 case WINED3DSIH_MOVA:instruction = "ARR"; break;
1041 case WINED3DSIH_SGN: instruction = "SSG"; break;
1042 default: instruction = "";
1043 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1047 /* Note that shader_arb_add_dst_param() adds spaces. */
1048 arguments[0] = '\0';
1049 shader_arb_get_dst_param(ins, dst, dst_str);
1050 for (i = 0; i < ins->src_count; ++i)
1053 strcat(arguments, ", ");
1054 shader_arb_get_src_param(ins, &ins->src[i], i, operand);
1055 strcat(arguments, operand);
1057 shader_addline(buffer, "%s%s %s%s;\n", instruction, shader_arb_get_modifier(ins), dst_str, arguments);
1060 static void shader_hw_nop(const struct wined3d_shader_instruction *ins)
1062 SHADER_BUFFER *buffer = ins->ctx->buffer;
1063 shader_addline(buffer, "NOP;\n");
1066 static void shader_hw_mov(const struct wined3d_shader_instruction *ins)
1068 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1070 SHADER_BUFFER *buffer = ins->ctx->buffer;
1071 char src0_param[256];
1073 if(ins->handler_idx == WINED3DSIH_MOVA) {
1074 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1075 struct wined3d_shader_src_param tmp_src = ins->src[0];
1078 if(ctx->target_version >= NV2) {
1079 shader_hw_map2gl(ins);
1082 tmp_src.swizzle = (tmp_src.swizzle & 0x3) * 0x55;
1083 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1084 shader_arb_get_write_mask(ins, &ins->dst[0], write_mask);
1086 /* This implements the mova formula used in GLSL. The first two instructions
1087 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1091 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1093 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1094 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1096 shader_addline(buffer, "SGE A0_SHADOW%s, %s, mova_const.y;\n", write_mask, src0_param);
1097 shader_addline(buffer, "MAD A0_SHADOW%s, A0_SHADOW, mova_const.z, -mova_const.w;\n", write_mask);
1099 shader_addline(buffer, "ABS TA%s, %s;\n", write_mask, src0_param);
1100 shader_addline(buffer, "ADD TA%s, TA, mova_const.x;\n", write_mask);
1101 shader_addline(buffer, "FLR TA%s, TA;\n", write_mask);
1102 shader_addline(buffer, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask);
1104 ((struct shader_arb_ctx_priv *)ins->ctx->backend_data)->addr_reg[0] = '\0';
1105 } else if (ins->ctx->reg_maps->shader_version.major == 1
1106 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
1107 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1109 src0_param[0] = '\0';
1110 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1112 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1113 shader_addline(buffer, "ADD TA.x, %s, helper_const.z;\n", src0_param);
1114 shader_addline(buffer, "ARL A0.x, TA.x;\n");
1118 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1119 * with more than one component. Thus replicate the first source argument over all
1120 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1121 struct wined3d_shader_src_param tmp_src = ins->src[0];
1122 tmp_src.swizzle = (tmp_src.swizzle & 0x3) * 0x55;
1123 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1124 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1129 shader_hw_map2gl(ins);
1133 static void pshader_hw_texkill(const struct wined3d_shader_instruction *ins)
1135 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1136 SHADER_BUFFER *buffer = ins->ctx->buffer;
1139 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1140 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1142 shader_arb_get_dst_param(ins, dst, reg_dest);
1144 if (ins->ctx->reg_maps->shader_version.major >= 2)
1146 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1147 shader_arb_get_dst_param(ins, dst, reg_dest);
1148 shader_addline(buffer, "KIL %s;\n", reg_dest);
1150 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1151 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1153 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1154 * or pass in any temporary register(in shader phase 2)
1156 if(ins->ctx->reg_maps->shader_version.minor <= 3) {
1157 sprintf(reg_dest, "fragment.texcoord[%u]", dst->reg.idx);
1159 shader_arb_get_dst_param(ins, dst, reg_dest);
1161 shader_addline(buffer, "SWZ TA, %s, x, y, z, 1;\n", reg_dest);
1162 shader_addline(buffer, "KIL TA;\n");
1166 static void pshader_hw_tex(const struct wined3d_shader_instruction *ins)
1168 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1169 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1170 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1171 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1172 ins->ctx->reg_maps->shader_version.minor);
1173 BOOL projected = FALSE, bias = FALSE;
1174 struct wined3d_shader_src_param src;
1178 DWORD reg_sampler_code;
1180 /* All versions have a destination register */
1181 shader_arb_get_dst_param(ins, dst, reg_dest);
1183 /* 1.0-1.4: Use destination register number as texture code.
1184 2.0+: Use provided sampler number as texure code. */
1185 if (shader_version < WINED3D_SHADER_VERSION(2,0))
1186 reg_sampler_code = dst->reg.idx;
1188 reg_sampler_code = ins->src[1].reg.idx;
1190 /* 1.0-1.3: Use the texcoord varying.
1191 1.4+: Use provided coordinate source register. */
1192 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1193 sprintf(reg_coord, "fragment.texcoord[%u]", reg_sampler_code);
1195 /* TEX is the only instruction that can handle DW and DZ natively */
1197 if(src.modifiers == WINED3DSPSM_DW) src.modifiers = WINED3DSPSM_NONE;
1198 if(src.modifiers == WINED3DSPSM_DZ) src.modifiers = WINED3DSPSM_NONE;
1199 shader_arb_get_src_param(ins, &src, 0, reg_coord);
1203 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1204 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1205 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1207 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1210 if(reg_sampler_code < MAX_TEXTURES) {
1211 flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1213 if (flags & WINED3DTTFF_PROJECTED) {
1217 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
1219 DWORD src_mod = ins->src[0].modifiers;
1220 if (src_mod == WINED3DSPSM_DZ) {
1221 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1222 * varying register, so we need a temp reg
1224 shader_addline(ins->ctx->buffer, "SWZ TA, %s, x, y, z, z;\n", reg_coord);
1225 strcpy(reg_coord, "TA");
1227 } else if(src_mod == WINED3DSPSM_DW) {
1231 if (ins->flags & WINED3DSI_TEXLD_PROJECT) projected = TRUE;
1232 if (ins->flags & WINED3DSI_TEXLD_BIAS) bias = TRUE;
1234 shader_hw_sample(ins, reg_sampler_code, reg_dest, reg_coord, projected, bias);
1237 static void pshader_hw_texcoord(const struct wined3d_shader_instruction *ins)
1239 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1240 SHADER_BUFFER *buffer = ins->ctx->buffer;
1241 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1242 ins->ctx->reg_maps->shader_version.minor);
1245 shader_arb_get_write_mask(ins, dst, tmp);
1246 if (shader_version != WINED3D_SHADER_VERSION(1,4))
1248 DWORD reg = dst->reg.idx;
1249 shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
1253 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src);
1254 shader_addline(buffer, "MOV R%u%s, %s;\n", dst->reg.idx, tmp, reg_src);
1258 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction *ins)
1260 SHADER_BUFFER *buffer = ins->ctx->buffer;
1261 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1262 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1265 DWORD reg1 = ins->dst[0].reg.idx;
1269 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
1270 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1271 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1272 /* Move .x first in case src_str is "TA" */
1273 shader_addline(buffer, "MOV TA.y, %s.x;\n", src_str);
1274 shader_addline(buffer, "MOV TA.x, %s.w;\n", src_str);
1275 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1276 shader_hw_sample(ins, reg1, dst_str, "TA", flags & WINED3DTTFF_PROJECTED, FALSE);
1279 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction *ins)
1281 SHADER_BUFFER *buffer = ins->ctx->buffer;
1283 DWORD reg1 = ins->dst[0].reg.idx;
1287 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
1288 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1289 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1290 shader_addline(buffer, "MOV TA.x, %s.y;\n", src_str);
1291 shader_addline(buffer, "MOV TA.y, %s.z;\n", src_str);
1292 shader_hw_sample(ins, reg1, dst_str, "TA", FALSE, FALSE);
1295 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction *ins)
1297 DWORD reg1 = ins->dst[0].reg.idx;
1301 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
1302 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1303 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1304 shader_hw_sample(ins, reg1, dst_str, src_str, FALSE, FALSE);
1307 static void pshader_hw_texbem(const struct wined3d_shader_instruction *ins)
1309 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1310 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1311 DWORD src = ins->src[0].reg.idx;
1312 SHADER_BUFFER *buffer = ins->ctx->buffer;
1313 char reg_coord[40], dst_reg[50];
1314 DWORD reg_dest_code;
1316 /* All versions have a destination register. The Tx where the texture coordinates come
1317 * from is the varying incarnation of the texture register
1319 reg_dest_code = dst->reg.idx;
1320 shader_arb_get_dst_param(ins, &ins->dst[0], dst_reg);
1321 sprintf(reg_coord, "fragment.texcoord[%u]", reg_dest_code);
1323 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1324 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
1326 shader_addline(buffer, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1327 shader_addline(buffer, "DP3 TA.x, TB, T%u;\n", src);
1328 shader_addline(buffer, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1329 shader_addline(buffer, "DP3 TA.y, TB, T%u;\n", src);
1331 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1332 * so we can't let the GL handle this.
1334 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1335 & WINED3DTTFF_PROJECTED) {
1336 shader_addline(buffer, "RCP TB.w, %s.w;\n", reg_coord);
1337 shader_addline(buffer, "MUL TB.xy, %s, TB.w;\n", reg_coord);
1338 shader_addline(buffer, "ADD TA.xy, TA, TB;\n");
1340 shader_addline(buffer, "ADD TA.xy, TA, %s;\n", reg_coord);
1343 shader_hw_sample(ins, reg_dest_code, dst_reg, "TA", FALSE, FALSE);
1345 if (ins->handler_idx == WINED3DSIH_TEXBEML)
1347 shader_addline(buffer, "MAD TA, T%u.z, luminance%d.x, luminance%d.y;\n",
1348 src, reg_dest_code, reg_dest_code);
1349 shader_addline(buffer, "MUL %s, %s, TA;\n", dst_reg, dst_reg);
1353 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction *ins)
1355 DWORD reg = ins->dst[0].reg.idx;
1356 SHADER_BUFFER *buffer = ins->ctx->buffer;
1359 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1360 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
1361 * T<reg+1> register. Use this register to store the calculated vector
1363 shader_addline(buffer, "DP3 T%u.x, fragment.texcoord[%u], %s;\n", reg + 1, reg, src0_name);
1366 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction *ins)
1368 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1369 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1371 DWORD reg = ins->dst[0].reg.idx;
1372 SHADER_BUFFER *buffer = ins->ctx->buffer;
1377 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
1378 sprintf(dst_reg, "T%u", reg);
1380 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1381 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1382 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
1383 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1384 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED, FALSE);
1387 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction *ins)
1389 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1390 DWORD reg = ins->dst[0].reg.idx;
1391 SHADER_BUFFER *buffer = ins->ctx->buffer;
1392 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1396 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
1397 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
1398 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
1400 dst = reg + 2 - current_state->current_row;
1402 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1403 shader_addline(buffer, "DP3 T%u.%c, fragment.texcoord[%u], %s;\n", dst, 'x' + current_state->current_row, reg, src0_name);
1404 current_state->texcoord_w[current_state->current_row++] = reg;
1407 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction *ins)
1409 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1410 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1412 DWORD reg = ins->dst[0].reg.idx;
1413 SHADER_BUFFER *buffer = ins->ctx->buffer;
1414 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1419 sprintf(dst_reg, "T%u", reg);
1420 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1421 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
1423 /* Sample the texture using the calculated coordinates */
1424 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1425 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1426 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED, FALSE);
1427 current_state->current_row = 0;
1430 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction *ins)
1432 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1433 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1435 DWORD reg = ins->dst[0].reg.idx;
1436 SHADER_BUFFER *buffer = ins->ctx->buffer;
1437 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1442 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
1443 * components for temporary data storage
1445 sprintf(dst_reg, "T%u", reg);
1446 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1447 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
1449 /* Construct the eye-ray vector from w coordinates */
1450 shader_addline(buffer, "MOV TB.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
1451 shader_addline(buffer, "MOV TB.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
1452 shader_addline(buffer, "MOV TB.z, fragment.texcoord[%u].w;\n", reg);
1454 /* Calculate reflection vector
1456 shader_addline(buffer, "DP3 %s.w, %s, TB;\n", dst_reg, dst_reg);
1457 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
1458 shader_addline(buffer, "DP3 TB.w, %s, %s;\n", dst_reg, dst_reg);
1459 shader_addline(buffer, "RCP TB.w, TB.w;\n");
1460 shader_addline(buffer, "MUL %s.w, %s.w, TB.w;\n", dst_reg, dst_reg);
1461 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
1462 shader_addline(buffer, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg, dst_reg);
1464 /* Sample the texture using the calculated coordinates */
1465 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1466 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1467 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED, FALSE);
1468 current_state->current_row = 0;
1471 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction *ins)
1473 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1474 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1476 DWORD reg = ins->dst[0].reg.idx;
1477 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1478 SHADER_BUFFER *buffer = ins->ctx->buffer;
1484 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1485 shader_arb_get_src_param(ins, &ins->src[0], 1, src1_name);
1486 /* Note: TMP.xy is input here, generated by two texm3x3pad instructions */
1487 sprintf(dst_reg, "T%u", reg);
1488 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
1490 /* Calculate reflection vector.
1493 * TMP.xyz = 2 * --------- * N - E
1496 * Which normalizes the normal vector
1498 shader_addline(buffer, "DP3 %s.w, %s, %s;\n", dst_reg, dst_reg, src1_name);
1499 shader_addline(buffer, "DP3 TC.w, %s, %s;\n", dst_reg, dst_reg);
1500 shader_addline(buffer, "RCP TC.w, TC.w;\n");
1501 shader_addline(buffer, "MUL %s.w, %s.w, TC.w;\n", dst_reg, dst_reg);
1502 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
1503 shader_addline(buffer, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg, dst_reg, src1_name);
1505 /* Sample the texture using the calculated coordinates */
1506 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1507 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1508 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED, FALSE);
1509 current_state->current_row = 0;
1512 static void pshader_hw_texdepth(const struct wined3d_shader_instruction *ins)
1514 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1515 SHADER_BUFFER *buffer = ins->ctx->buffer;
1518 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1519 * which is essentially an input, is the destination register because it is the first
1520 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1521 * here(writemasks/swizzles are not valid on texdepth)
1523 shader_arb_get_dst_param(ins, dst, dst_name);
1525 /* According to the msdn, the source register(must be r5) is unusable after
1526 * the texdepth instruction, so we're free to modify it
1528 shader_addline(buffer, "MIN %s.y, %s.y, one.y;\n", dst_name, dst_name);
1530 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1531 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1532 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1534 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
1535 shader_addline(buffer, "MUL TA.x, %s.x, %s.y;\n", dst_name, dst_name);
1536 shader_addline(buffer, "MIN TA.x, TA.x, one.x;\n");
1537 shader_addline(buffer, "MAX result.depth, TA.x, 0.0;\n");
1540 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1541 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1542 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1543 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction *ins)
1545 SHADER_BUFFER *buffer = ins->ctx->buffer;
1546 DWORD sampler_idx = ins->dst[0].reg.idx;
1550 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
1551 shader_addline(buffer, "MOV TB, 0.0;\n");
1552 shader_addline(buffer, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx, src0);
1554 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1555 shader_hw_sample(ins, sampler_idx, dst_str, "TB", FALSE /* Only one coord, can't be projected */, FALSE);
1558 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1559 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1560 static void pshader_hw_texdp3(const struct wined3d_shader_instruction *ins)
1562 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1565 SHADER_BUFFER *buffer = ins->ctx->buffer;
1567 /* Handle output register */
1568 shader_arb_get_dst_param(ins, dst, dst_str);
1569 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
1570 shader_addline(buffer, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str, dst->reg.idx, src0);
1573 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1574 * Perform the 3rd row of a 3x3 matrix multiply */
1575 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction *ins)
1577 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1578 SHADER_BUFFER *buffer = ins->ctx->buffer;
1582 shader_arb_get_dst_param(ins, dst, dst_str);
1583 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
1584 shader_addline(buffer, "DP3 T%u.z, fragment.texcoord[%u], %s;\n", dst->reg.idx, dst->reg.idx, src0);
1585 shader_addline(buffer, "MOV %s, T%u;\n", dst_str, dst->reg.idx);
1588 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1589 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1590 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1591 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1593 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction *ins)
1595 SHADER_BUFFER *buffer = ins->ctx->buffer;
1596 DWORD dst_reg = ins->dst[0].reg.idx;
1599 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
1600 shader_addline(buffer, "DP3 T%u.y, fragment.texcoord[%u], %s;\n", dst_reg, dst_reg, src0);
1602 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1603 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1604 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1606 shader_addline(buffer, "RCP T%u.y, T%u.y;\n", dst_reg, dst_reg);
1607 shader_addline(buffer, "MUL T%u.x, T%u.x, T%u.y;\n", dst_reg, dst_reg, dst_reg);
1608 shader_addline(buffer, "MIN T%u.x, T%u.x, one.x;\n", dst_reg, dst_reg);
1609 shader_addline(buffer, "MAX result.depth, T%u.x, 0.0;\n", dst_reg);
1612 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1613 Vertex/Pixel shaders to ARB_vertex_program codes */
1614 static void shader_hw_mnxn(const struct wined3d_shader_instruction *ins)
1617 int nComponents = 0;
1618 struct wined3d_shader_dst_param tmp_dst = {{0}};
1619 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
1620 struct wined3d_shader_instruction tmp_ins;
1622 memset(&tmp_ins, 0, sizeof(tmp_ins));
1624 /* Set constants for the temporary argument */
1625 tmp_ins.ctx = ins->ctx;
1626 tmp_ins.dst_count = 1;
1627 tmp_ins.dst = &tmp_dst;
1628 tmp_ins.src_count = 2;
1629 tmp_ins.src = tmp_src;
1631 switch(ins->handler_idx)
1633 case WINED3DSIH_M4x4:
1635 tmp_ins.handler_idx = WINED3DSIH_DP4;
1637 case WINED3DSIH_M4x3:
1639 tmp_ins.handler_idx = WINED3DSIH_DP4;
1641 case WINED3DSIH_M3x4:
1643 tmp_ins.handler_idx = WINED3DSIH_DP3;
1645 case WINED3DSIH_M3x3:
1647 tmp_ins.handler_idx = WINED3DSIH_DP3;
1649 case WINED3DSIH_M3x2:
1651 tmp_ins.handler_idx = WINED3DSIH_DP3;
1654 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1658 tmp_dst = ins->dst[0];
1659 tmp_src[0] = ins->src[0];
1660 tmp_src[1] = ins->src[1];
1661 for (i = 0; i < nComponents; i++) {
1662 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
1663 shader_hw_map2gl(&tmp_ins);
1664 ++tmp_src[1].reg.idx;
1668 static void shader_hw_rsq_rcp(const struct wined3d_shader_instruction *ins)
1670 SHADER_BUFFER *buffer = ins->ctx->buffer;
1671 const char *instruction;
1676 switch(ins->handler_idx)
1678 case WINED3DSIH_RSQ: instruction = "RSQ"; break;
1679 case WINED3DSIH_RCP: instruction = "RCP"; break;
1680 default: instruction = "";
1681 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1685 shader_arb_get_dst_param(ins, &ins->dst[0], dst); /* Destination */
1686 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
1687 if (ins->src[0].swizzle == WINED3DSP_NOSWIZZLE)
1689 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1695 shader_addline(buffer, "%s%s %s, %s;\n", instruction, shader_arb_get_modifier(ins), dst, src);
1698 static void shader_hw_nrm(const struct wined3d_shader_instruction *ins)
1700 SHADER_BUFFER *buffer = ins->ctx->buffer;
1704 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
1705 shader_arb_get_src_param(ins, &ins->src[0], 1 /* Use TB */, src_name);
1706 shader_addline(buffer, "DP3 TA, %s, %s;\n", src_name, src_name);
1707 shader_addline(buffer, "RSQ TA, TA.x;\n");
1708 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1709 shader_addline(buffer, "MUL%s %s, %s, TA;\n", shader_arb_get_modifier(ins), dst_name,
1713 static void shader_hw_sincos(const struct wined3d_shader_instruction *ins)
1715 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1716 * must contain fixed constants. So we need a separate function to filter those constants and
1719 SHADER_BUFFER *buffer = ins->ctx->buffer;
1723 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
1724 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
1725 shader_addline(buffer, "SCS%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name,
1729 /* GL locking is done by the caller */
1730 static void shader_hw_sgn(const struct wined3d_shader_instruction *ins)
1732 SHADER_BUFFER *buffer = ins->ctx->buffer;
1735 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1737 /* SGN is only valid in vertex shaders */
1738 if(ctx->target_version == NV2) {
1739 shader_hw_map2gl(ins);
1742 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
1743 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
1745 FIXME("Emulated SGN untested\n");
1746 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
1747 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
1749 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) {
1750 shader_addline(buffer, "SLT %s, -%s, %s;\n", dst_name, src_name, src_name);
1752 shader_addline(buffer, "SLT TB, -%s, %s;\n", src_name, src_name);
1753 shader_addline(buffer, "SLT TC, %s, -%s;\n", src_name, src_name);
1754 shader_addline(buffer, "ADD %s, TB, -TC;\n", dst_name);
1758 static GLuint create_arb_blt_vertex_program(const WineD3D_GL_Info *gl_info)
1760 GLuint program_id = 0;
1761 const char *blt_vprogram =
1763 "PARAM c[1] = { { 1, 0.5 } };\n"
1764 "MOV result.position, vertex.position;\n"
1765 "MOV result.color, c[0].x;\n"
1766 "MOV result.texcoord[0], vertex.texcoord[0];\n"
1769 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1770 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
1771 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
1773 if (glGetError() == GL_INVALID_OPERATION) {
1775 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1776 FIXME("Vertex program error at position %d: %s\n", pos,
1777 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1783 /* GL locking is done by the caller */
1784 static GLuint create_arb_blt_fragment_program(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
1786 GLuint program_id = 0;
1787 static const char * const blt_fprograms[tex_type_count] =
1794 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1795 "MOV result.depth.z, R0.x;\n"
1802 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
1803 "MOV result.depth.z, R0.x;\n"
1808 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
1809 "MOV result.depth.z, R0.x;\n"
1813 if (!blt_fprograms[tex_type])
1815 FIXME("tex_type %#x not supported\n", tex_type);
1819 GL_EXTCALL(glGenProgramsARB(1, &program_id));
1820 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
1821 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprograms[tex_type]), blt_fprograms[tex_type]));
1823 if (glGetError() == GL_INVALID_OPERATION) {
1825 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1826 FIXME("Fragment program error at position %d: %s\n", pos,
1827 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1833 /* GL locking is done by the caller */
1834 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1835 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1836 struct shader_arb_priv *priv = This->shader_priv;
1837 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1840 struct vs_compile_args compile_args;
1842 TRACE("Using vertex shader\n");
1843 find_vs_compile_args((IWineD3DVertexShaderImpl *) This->stateBlock->vertexShader, This->stateBlock, &compile_args);
1844 priv->current_vprogram_id = find_gl_vshader((IWineD3DVertexShaderImpl *) This->stateBlock->vertexShader, &compile_args);
1846 /* Bind the vertex program */
1847 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1848 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
1850 /* Enable OpenGL vertex programs */
1851 glEnable(GL_VERTEX_PROGRAM_ARB);
1852 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1853 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1854 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1855 priv->current_vprogram_id = 0;
1856 glDisable(GL_VERTEX_PROGRAM_ARB);
1857 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1861 struct ps_compile_args compile_args;
1862 TRACE("Using pixel shader\n");
1863 find_ps_compile_args((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader, This->stateBlock, &compile_args);
1864 priv->current_fprogram_id = find_gl_pshader((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader,
1867 /* Bind the fragment program */
1868 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1869 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
1871 if(!priv->use_arbfp_fixed_func) {
1872 /* Enable OpenGL fragment programs */
1873 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1874 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1876 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1878 shader_arb_ps_local_constants(This);
1879 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && !priv->use_arbfp_fixed_func) {
1880 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
1881 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
1882 * replacement shader
1884 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1885 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1886 priv->current_fprogram_id = 0;
1890 /* GL locking is done by the caller */
1891 static void shader_arb_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
1892 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1893 struct shader_arb_priv *priv = This->shader_priv;
1894 GLuint *blt_fprogram = &priv->depth_blt_fprogram_id[tex_type];
1895 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1897 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
1898 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
1899 glEnable(GL_VERTEX_PROGRAM_ARB);
1901 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type);
1902 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
1903 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1906 /* GL locking is done by the caller */
1907 static void shader_arb_deselect_depth_blt(IWineD3DDevice *iface) {
1908 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1909 struct shader_arb_priv *priv = This->shader_priv;
1910 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1912 if (priv->current_vprogram_id) {
1913 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1914 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1916 glEnable(GL_VERTEX_PROGRAM_ARB);
1917 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1919 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1921 glDisable(GL_VERTEX_PROGRAM_ARB);
1922 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1925 if (priv->current_fprogram_id) {
1926 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1927 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1929 glEnable(GL_FRAGMENT_PROGRAM_ARB);
1930 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1932 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1934 glDisable(GL_FRAGMENT_PROGRAM_ARB);
1935 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1939 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
1940 IWineD3DBaseShaderImpl *baseShader = (IWineD3DBaseShaderImpl *) iface;
1941 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)baseShader->baseShader.device)->adapter->gl_info;
1943 if (shader_is_pshader_version(baseShader->baseShader.reg_maps.shader_version.type))
1945 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
1949 for(i = 0; i < This->num_gl_shaders; i++) {
1950 GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId));
1951 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1954 HeapFree(GetProcessHeap(), 0, This->gl_shaders);
1955 This->gl_shaders = NULL;
1956 This->num_gl_shaders = 0;
1957 This->shader_array_size = 0;
1959 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *) iface;
1963 for(i = 0; i < This->num_gl_shaders; i++) {
1964 GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId));
1965 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1968 HeapFree(GetProcessHeap(), 0, This->gl_shaders);
1969 This->gl_shaders = NULL;
1970 This->num_gl_shaders = 0;
1971 This->shader_array_size = 0;
1975 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
1976 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1977 This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
1981 static void shader_arb_free(IWineD3DDevice *iface) {
1982 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1983 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1984 struct shader_arb_priv *priv = This->shader_priv;
1988 if(priv->depth_blt_vprogram_id) {
1989 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
1991 for (i = 0; i < tex_type_count; ++i) {
1992 if (priv->depth_blt_fprogram_id[i]) {
1993 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id[i]));
1998 HeapFree(GetProcessHeap(), 0, This->shader_priv);
2001 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
2005 static void arbfp_add_sRGB_correction(SHADER_BUFFER *buffer, const char *fragcolor, const char *tmp1,
2006 const char *tmp2, const char *tmp3) {
2007 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
2009 /* Calculate the > 0.0031308 case */
2010 shader_addline(buffer, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1, fragcolor);
2011 shader_addline(buffer, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1, fragcolor);
2012 shader_addline(buffer, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1, fragcolor);
2013 shader_addline(buffer, "MUL %s, %s, srgb_consts1.w;\n", tmp1, tmp1);
2014 shader_addline(buffer, "SUB %s, %s, srgb_consts2.x;\n", tmp1, tmp1);
2015 /* Calculate the < case */
2016 shader_addline(buffer, "MUL %s, srgb_consts1.x, %s;\n", tmp2, fragcolor);
2017 /* Subtract the comparison value from the fragcolor and use CMP to pick either the > case * or the < case */
2018 shader_addline(buffer, "SUB %s, %s, srgb_consts1.y;\n", tmp3, fragcolor);
2019 shader_addline(buffer, "CMP result.color.xyz, %s, %s, %s;\n", tmp3, tmp2, tmp1);
2022 /* GL locking is done by the caller */
2023 static GLuint shader_arb_generate_pshader(IWineD3DPixelShader *iface,
2024 SHADER_BUFFER *buffer, const struct ps_compile_args *args)
2026 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
2027 const shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
2028 CONST DWORD *function = This->baseShader.function;
2029 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
2030 const local_constant *lconst;
2032 const char *fragcolor;
2033 DWORD *lconst_map = local_const_mapping((IWineD3DBaseShaderImpl *) This);
2034 struct shader_arb_ctx_priv priv_ctx;
2036 /* Create the hw ARB shader */
2037 memset(&priv_ctx, 0, sizeof(priv_ctx));
2038 shader_addline(buffer, "!!ARBfp1.0\n");
2039 if(GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION)) {
2040 shader_addline(buffer, "OPTION NV_fragment_program;\n");
2041 priv_ctx.target_version = NV2;
2043 priv_ctx.target_version = ARB;
2046 if (reg_maps->shader_version.major < 3)
2052 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
2055 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
2058 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
2063 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
2064 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
2065 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
2066 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
2067 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
2068 shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
2070 if (reg_maps->shader_version.major < 2)
2074 if(args->srgb_correction) {
2075 shader_addline(buffer, "TEMP TMP_COLOR;\n");
2076 fragcolor = "TMP_COLOR";
2078 fragcolor = "result.color";
2082 /* Base Declarations */
2083 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, lconst_map);
2085 /* Base Shader Body */
2086 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
2088 if(args->srgb_correction) {
2089 arbfp_add_sRGB_correction(buffer, fragcolor, "TA", "TB", "TC");
2090 shader_addline(buffer, "MOV result.color.a, %s;\n", fragcolor);
2091 } else if(reg_maps->shader_version.major < 2) {
2092 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
2094 shader_addline(buffer, "END\n");
2096 /* TODO: change to resource.glObjectHandle or something like that */
2097 GL_EXTCALL(glGenProgramsARB(1, &retval));
2099 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
2100 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
2102 TRACE("Created hw pixel shader, prg=%d\n", retval);
2103 /* Create the program and check for errors */
2104 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2105 buffer->bsize, buffer->buffer));
2107 if (glGetError() == GL_INVALID_OPERATION) {
2109 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2110 FIXME("HW PixelShader Error at position %d: %s\n",
2111 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2115 /* Load immediate constants */
2117 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2118 const float *value = (const float *)lconst->value;
2119 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst_map[lconst->idx], value));
2120 checkGLcall("glProgramLocalParameter4fvARB");
2122 HeapFree(GetProcessHeap(), 0, lconst_map);
2128 /* GL locking is done by the caller */
2129 static GLuint shader_arb_generate_vshader(IWineD3DVertexShader *iface,
2130 SHADER_BUFFER *buffer, const struct vs_compile_args *args)
2132 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
2133 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
2134 CONST DWORD *function = This->baseShader.function;
2135 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
2136 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
2137 const local_constant *lconst;
2139 DWORD *lconst_map = local_const_mapping((IWineD3DBaseShaderImpl *) This);
2140 struct shader_arb_ctx_priv priv_ctx;
2142 memset(&priv_ctx, 0, sizeof(priv_ctx));
2143 /* Create the hw ARB shader */
2144 shader_addline(buffer, "!!ARBvp1.0\n");
2146 if(GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION)) {
2147 shader_addline(buffer, "OPTION NV_vertex_program2;\n");
2148 priv_ctx.target_version = NV2;
2150 priv_ctx.target_version = ARB;
2153 if(need_helper_const(gl_info)) {
2154 shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
2156 if(need_mova_const((IWineD3DBaseShader *) iface, gl_info)) {
2157 shader_addline(buffer, "PARAM mova_const = { 0.5, 0.0, 2.0, 1.0 };\n");
2158 shader_addline(buffer, "TEMP A0_SHADOW;\n");
2161 /* Mesa supports only 95 constants */
2162 if (GL_VEND(MESA) || GL_VEND(WINE))
2163 This->baseShader.limits.constant_float =
2164 min(95, This->baseShader.limits.constant_float);
2166 shader_addline(buffer, "TEMP TA;\n");
2168 /* Base Declarations */
2169 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, lconst_map);
2171 /* We need a constant to fixup the final position */
2172 shader_addline(buffer, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS);
2174 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
2175 * for output parameters. D3D in theory does not do that either, but some applications depend on a
2176 * proper initialization of the secondary color, and programs using the fixed function pipeline without
2177 * a replacement shader depend on the texcoord.w being set properly.
2179 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
2180 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
2181 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
2182 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
2183 * this can eat a number of instructions, so skip it unless this cap is set as well
2185 if(!GL_SUPPORT(NV_VERTEX_PROGRAM)) {
2186 shader_addline(buffer, "MOV result.color.secondary, -helper_const.wwwy;\n");
2188 if((GLINFO_LOCATION).set_texcoord_w && !device->frag_pipe->ffp_proj_control) {
2190 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2191 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2192 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2193 shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
2199 /* Base Shader Body */
2200 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
2202 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
2203 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
2204 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
2205 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
2207 if(args->fog_src == VS_FOG_Z) {
2208 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2209 } else if (!reg_maps->fog) {
2210 /* posFixup.x is always 1.0, so we can savely use it */
2211 shader_addline(buffer, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
2214 /* Write the final position.
2216 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2217 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2218 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2219 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2221 shader_addline(buffer, "MUL TA, posFixup, TMP_OUT.w;\n");
2222 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
2223 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
2225 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2226 * and the glsl equivalent
2228 if(need_helper_const(gl_info)) {
2229 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2231 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
2232 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
2235 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2237 shader_addline(buffer, "END\n");
2239 /* TODO: change to resource.glObjectHandle or something like that */
2240 GL_EXTCALL(glGenProgramsARB(1, &ret));
2242 TRACE("Creating a hw vertex shader, prg=%d\n", ret);
2243 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, ret));
2245 TRACE("Created hw vertex shader, prg=%d\n", ret);
2246 /* Create the program and check for errors */
2247 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2248 buffer->bsize, buffer->buffer));
2250 if (glGetError() == GL_INVALID_OPERATION) {
2252 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2253 FIXME("HW VertexShader Error at position %d: %s\n",
2254 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2257 /* Load immediate constants */
2259 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2260 const float *value = (const float *)lconst->value;
2261 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst_map[lconst->idx], value));
2265 HeapFree(GetProcessHeap(), 0, lconst_map);
2270 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
2272 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2273 * then overwrite the shader specific ones
2275 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
2277 if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
2278 pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
2279 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2280 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF) - 1;
2283 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
2284 pCaps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
2285 pCaps->PixelShader1xMaxValue = 8.0;
2286 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2287 pCaps->MaxPixelShaderConst = GL_LIMITS(pshader_constantsF);
2290 pCaps->VSClipping = FALSE; /* TODO: GL_NV_vertex_program2_option provides this */
2293 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
2295 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
2297 TRACE("Checking support for color_fixup:\n");
2298 dump_color_fixup_desc(fixup);
2301 /* We support everything except YUV conversions. */
2302 if (!is_yuv_fixup(fixup))
2308 TRACE("[FAILED]\n");
2312 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction *ins) {
2314 char write_mask[20], regstr[50];
2315 SHADER_BUFFER *buffer = ins->ctx->buffer;
2316 BOOL is_color = FALSE;
2317 const struct wined3d_shader_dst_param *dst;
2319 if (!ins->dst_count) return;
2323 if(shift == 0) return; /* Saturate alone is handled by the instructions */
2325 shader_arb_get_write_mask(ins, dst, write_mask);
2326 shader_arb_get_register_name(ins, &dst->reg, regstr, &is_color);
2328 /* Generate a line that does the output modifier computation
2329 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
2330 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
2332 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins),
2333 regstr, write_mask, regstr, shift_tab[shift]);
2336 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
2338 /* WINED3DSIH_ABS */ shader_hw_map2gl,
2339 /* WINED3DSIH_ADD */ shader_hw_map2gl,
2340 /* WINED3DSIH_BEM */ pshader_hw_bem,
2341 /* WINED3DSIH_BREAK */ NULL,
2342 /* WINED3DSIH_BREAKC */ NULL,
2343 /* WINED3DSIH_BREAKP */ NULL,
2344 /* WINED3DSIH_CALL */ NULL,
2345 /* WINED3DSIH_CALLNZ */ NULL,
2346 /* WINED3DSIH_CMP */ pshader_hw_cmp,
2347 /* WINED3DSIH_CND */ pshader_hw_cnd,
2348 /* WINED3DSIH_CRS */ shader_hw_map2gl,
2349 /* WINED3DSIH_DCL */ NULL,
2350 /* WINED3DSIH_DEF */ NULL,
2351 /* WINED3DSIH_DEFB */ NULL,
2352 /* WINED3DSIH_DEFI */ NULL,
2353 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
2354 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
2355 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
2356 /* WINED3DSIH_DST */ shader_hw_map2gl,
2357 /* WINED3DSIH_DSX */ NULL,
2358 /* WINED3DSIH_DSY */ NULL,
2359 /* WINED3DSIH_ELSE */ NULL,
2360 /* WINED3DSIH_ENDIF */ NULL,
2361 /* WINED3DSIH_ENDLOOP */ NULL,
2362 /* WINED3DSIH_ENDREP */ NULL,
2363 /* WINED3DSIH_EXP */ shader_hw_map2gl,
2364 /* WINED3DSIH_EXPP */ shader_hw_map2gl,
2365 /* WINED3DSIH_FRC */ shader_hw_map2gl,
2366 /* WINED3DSIH_IF */ NULL,
2367 /* WINED3DSIH_IFC */ NULL,
2368 /* WINED3DSIH_LABEL */ NULL,
2369 /* WINED3DSIH_LIT */ shader_hw_map2gl,
2370 /* WINED3DSIH_LOG */ shader_hw_map2gl,
2371 /* WINED3DSIH_LOGP */ shader_hw_map2gl,
2372 /* WINED3DSIH_LOOP */ NULL,
2373 /* WINED3DSIH_LRP */ shader_hw_map2gl,
2374 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
2375 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
2376 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
2377 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
2378 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
2379 /* WINED3DSIH_MAD */ shader_hw_map2gl,
2380 /* WINED3DSIH_MAX */ shader_hw_map2gl,
2381 /* WINED3DSIH_MIN */ shader_hw_map2gl,
2382 /* WINED3DSIH_MOV */ shader_hw_mov,
2383 /* WINED3DSIH_MOVA */ shader_hw_mov,
2384 /* WINED3DSIH_MUL */ shader_hw_map2gl,
2385 /* WINED3DSIH_NOP */ shader_hw_nop,
2386 /* WINED3DSIH_NRM */ shader_hw_nrm,
2387 /* WINED3DSIH_PHASE */ NULL,
2388 /* WINED3DSIH_POW */ shader_hw_map2gl,
2389 /* WINED3DSIH_RCP */ shader_hw_rsq_rcp,
2390 /* WINED3DSIH_REP */ NULL,
2391 /* WINED3DSIH_RET */ NULL,
2392 /* WINED3DSIH_RSQ */ shader_hw_rsq_rcp,
2393 /* WINED3DSIH_SETP */ NULL,
2394 /* WINED3DSIH_SGE */ shader_hw_map2gl,
2395 /* WINED3DSIH_SGN */ shader_hw_sgn,
2396 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
2397 /* WINED3DSIH_SLT */ shader_hw_map2gl,
2398 /* WINED3DSIH_SUB */ shader_hw_map2gl,
2399 /* WINED3DSIH_TEX */ pshader_hw_tex,
2400 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
2401 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
2402 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
2403 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
2404 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
2405 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
2406 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
2407 /* WINED3DSIH_TEXLDD */ NULL,
2408 /* WINED3DSIH_TEXLDL */ NULL,
2409 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
2410 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
2411 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
2412 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
2413 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
2414 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
2415 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
2416 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
2417 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
2418 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
2419 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
2420 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
2423 const shader_backend_t arb_program_shader_backend = {
2424 shader_arb_instruction_handler_table,
2426 shader_arb_select_depth_blt,
2427 shader_arb_deselect_depth_blt,
2428 shader_arb_update_float_vertex_constants,
2429 shader_arb_update_float_pixel_constants,
2430 shader_arb_load_constants,
2431 shader_arb_load_np2fixup_constants,
2435 shader_arb_dirty_const,
2436 shader_arb_generate_pshader,
2437 shader_arb_generate_vshader,
2438 shader_arb_get_caps,
2439 shader_arb_color_fixup_supported,
2440 shader_arb_add_instruction_modifiers,
2443 /* ARB_fragment_program fixed function pipeline replacement definitions */
2444 #define ARB_FFP_CONST_TFACTOR 0
2445 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
2446 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
2447 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
2448 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
2450 struct arbfp_ffp_desc
2452 struct ffp_frag_desc parent;
2454 unsigned int num_textures_used;
2457 static void arbfp_enable(IWineD3DDevice *iface, BOOL enable) {
2460 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2461 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
2463 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2464 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
2469 static HRESULT arbfp_alloc(IWineD3DDevice *iface) {
2470 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2471 struct shader_arb_priv *priv;
2472 /* Share private data between the shader backend and the pipeline replacement, if both
2473 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
2474 * if no pixel shader is bound or not
2476 if(This->shader_backend == &arb_program_shader_backend) {
2477 This->fragment_priv = This->shader_priv;
2479 This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
2480 if(!This->fragment_priv) return E_OUTOFMEMORY;
2482 priv = This->fragment_priv;
2483 priv->fragment_shaders = hash_table_create(ffp_frag_program_key_hash, ffp_frag_program_key_compare);
2484 priv->use_arbfp_fixed_func = TRUE;
2488 static void arbfp_free_ffpshader(void *value, void *gli) {
2489 const WineD3D_GL_Info *gl_info = gli;
2490 struct arbfp_ffp_desc *entry_arb = value;
2493 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
2494 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
2495 HeapFree(GetProcessHeap(), 0, entry_arb);
2499 static void arbfp_free(IWineD3DDevice *iface) {
2500 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2501 struct shader_arb_priv *priv = This->fragment_priv;
2503 hash_table_destroy(priv->fragment_shaders, arbfp_free_ffpshader, &This->adapter->gl_info);
2504 priv->use_arbfp_fixed_func = FALSE;
2506 if(This->shader_backend != &arb_program_shader_backend) {
2507 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
2511 static void arbfp_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps)
2513 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
2514 WINED3DTEXOPCAPS_SELECTARG1 |
2515 WINED3DTEXOPCAPS_SELECTARG2 |
2516 WINED3DTEXOPCAPS_MODULATE4X |
2517 WINED3DTEXOPCAPS_MODULATE2X |
2518 WINED3DTEXOPCAPS_MODULATE |
2519 WINED3DTEXOPCAPS_ADDSIGNED2X |
2520 WINED3DTEXOPCAPS_ADDSIGNED |
2521 WINED3DTEXOPCAPS_ADD |
2522 WINED3DTEXOPCAPS_SUBTRACT |
2523 WINED3DTEXOPCAPS_ADDSMOOTH |
2524 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
2525 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
2526 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
2527 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
2528 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
2529 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
2530 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
2531 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
2532 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
2533 WINED3DTEXOPCAPS_DOTPRODUCT3 |
2534 WINED3DTEXOPCAPS_MULTIPLYADD |
2535 WINED3DTEXOPCAPS_LERP |
2536 WINED3DTEXOPCAPS_BUMPENVMAP |
2537 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
2539 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
2541 caps->MaxTextureBlendStages = 8;
2542 caps->MaxSimultaneousTextures = min(GL_LIMITS(fragment_samplers), 8);
2544 caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
2546 #undef GLINFO_LOCATION
2548 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
2549 static void state_texfactor_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2551 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2553 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2554 * application provided constants
2556 if(device->shader_backend == &arb_program_shader_backend) {
2557 if (use_ps(stateblock)) return;
2559 device = stateblock->wineD3DDevice;
2560 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
2561 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
2564 D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
2565 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
2566 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
2570 static void state_arb_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2572 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2574 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2575 * application provided constants
2577 if(device->shader_backend == &arb_program_shader_backend) {
2578 if (use_ps(stateblock)) return;
2580 device = stateblock->wineD3DDevice;
2581 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
2582 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
2585 if(stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
2586 /* The specular color has no alpha */
2587 col[0] = 1.0; col[1] = 1.0;
2588 col[2] = 1.0; col[3] = 0.0;
2590 col[0] = 0.0; col[1] = 0.0;
2591 col[2] = 0.0; col[3] = 0.0;
2593 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
2594 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
2597 static void set_bumpmat_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2598 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
2599 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2602 if (use_ps(stateblock))
2605 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.bumpmat[stage]) {
2606 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
2609 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2610 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2614 if(device->shader_backend == &arb_program_shader_backend) {
2615 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2618 } else if(device->shader_backend == &arb_program_shader_backend) {
2619 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
2620 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
2623 mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
2624 mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
2625 mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
2626 mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
2628 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
2629 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
2632 static void tex_bumpenvlum_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2633 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
2634 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2637 if (use_ps(stateblock))
2640 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.luminanceparams[stage]) {
2641 /* The pixel shader has to know the luminance offset. Do a constants update if it
2642 * isn't scheduled anyway
2644 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2645 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2649 if(device->shader_backend == &arb_program_shader_backend) {
2650 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2653 } else if(device->shader_backend == &arb_program_shader_backend) {
2654 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
2655 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
2658 param[0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLSCALE]);
2659 param[1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET]);
2663 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
2664 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
2667 static const char *get_argreg(SHADER_BUFFER *buffer, DWORD argnum, unsigned int stage, DWORD arg) {
2670 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
2672 switch(arg & WINED3DTA_SELECTMASK) {
2673 case WINED3DTA_DIFFUSE:
2674 ret = "fragment.color.primary"; break;
2676 case WINED3DTA_CURRENT:
2677 if(stage == 0) ret = "fragment.color.primary";
2681 case WINED3DTA_TEXTURE:
2683 case 0: ret = "tex0"; break;
2684 case 1: ret = "tex1"; break;
2685 case 2: ret = "tex2"; break;
2686 case 3: ret = "tex3"; break;
2687 case 4: ret = "tex4"; break;
2688 case 5: ret = "tex5"; break;
2689 case 6: ret = "tex6"; break;
2690 case 7: ret = "tex7"; break;
2691 default: ret = "unknown texture";
2695 case WINED3DTA_TFACTOR:
2696 ret = "tfactor"; break;
2698 case WINED3DTA_SPECULAR:
2699 ret = "fragment.color.secondary"; break;
2701 case WINED3DTA_TEMP:
2702 ret = "tempreg"; break;
2704 case WINED3DTA_CONSTANT:
2705 FIXME("Implement perstage constants\n");
2707 case 0: ret = "const0"; break;
2708 case 1: ret = "const1"; break;
2709 case 2: ret = "const2"; break;
2710 case 3: ret = "const3"; break;
2711 case 4: ret = "const4"; break;
2712 case 5: ret = "const5"; break;
2713 case 6: ret = "const6"; break;
2714 case 7: ret = "const7"; break;
2715 default: ret = "unknown constant";
2723 if(arg & WINED3DTA_COMPLEMENT) {
2724 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
2725 if(argnum == 0) ret = "arg0";
2726 if(argnum == 1) ret = "arg1";
2727 if(argnum == 2) ret = "arg2";
2729 if(arg & WINED3DTA_ALPHAREPLICATE) {
2730 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
2731 if(argnum == 0) ret = "arg0";
2732 if(argnum == 1) ret = "arg1";
2733 if(argnum == 2) ret = "arg2";
2738 static void gen_ffp_instr(SHADER_BUFFER *buffer, unsigned int stage, BOOL color, BOOL alpha,
2739 DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2) {
2740 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
2741 unsigned int mul = 1;
2742 BOOL mul_final_dest = FALSE;
2744 if(color && alpha) dstmask = "";
2745 else if(color) dstmask = ".xyz";
2746 else dstmask = ".w";
2748 if(dst == tempreg) dstreg = "tempreg";
2749 else dstreg = "ret";
2751 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
2752 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
2753 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
2756 case WINED3DTOP_DISABLE:
2757 if(stage == 0) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
2760 case WINED3DTOP_SELECTARG2:
2762 case WINED3DTOP_SELECTARG1:
2763 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
2766 case WINED3DTOP_MODULATE4X:
2768 case WINED3DTOP_MODULATE2X:
2770 if(strcmp(dstreg, "result.color") == 0) {
2772 mul_final_dest = TRUE;
2774 case WINED3DTOP_MODULATE:
2775 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2778 case WINED3DTOP_ADDSIGNED2X:
2780 if(strcmp(dstreg, "result.color") == 0) {
2782 mul_final_dest = TRUE;
2784 case WINED3DTOP_ADDSIGNED:
2785 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2787 case WINED3DTOP_ADD:
2788 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2791 case WINED3DTOP_SUBTRACT:
2792 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2795 case WINED3DTOP_ADDSMOOTH:
2796 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
2797 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2800 case WINED3DTOP_BLENDCURRENTALPHA:
2801 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
2802 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2804 case WINED3DTOP_BLENDFACTORALPHA:
2805 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
2806 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2808 case WINED3DTOP_BLENDTEXTUREALPHA:
2809 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2810 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2812 case WINED3DTOP_BLENDDIFFUSEALPHA:
2813 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
2814 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2817 case WINED3DTOP_BLENDTEXTUREALPHAPM:
2818 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2819 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
2820 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
2823 /* D3DTOP_PREMODULATE ???? */
2825 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2826 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
2827 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2829 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2830 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
2832 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2833 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
2834 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
2836 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2837 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
2840 case WINED3DTOP_DOTPRODUCT3:
2842 if(strcmp(dstreg, "result.color") == 0) {
2844 mul_final_dest = TRUE;
2846 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
2847 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2848 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
2851 case WINED3DTOP_MULTIPLYADD:
2852 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
2855 case WINED3DTOP_LERP:
2856 /* The msdn is not quite right here */
2857 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2860 case WINED3DTOP_BUMPENVMAP:
2861 case WINED3DTOP_BUMPENVMAPLUMINANCE:
2862 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
2866 FIXME("Unhandled texture op %08x\n", op);
2870 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2871 } else if(mul == 4) {
2872 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2876 /* The stateblock is passed for GLINFO_LOCATION */
2877 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, IWineD3DStateBlockImpl *stateblock)
2880 SHADER_BUFFER buffer;
2881 BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2882 BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2883 BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2884 const char *textype;
2885 const char *instr, *sat;
2886 char colorcor_dst[8];
2888 DWORD arg0, arg1, arg2;
2889 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
2891 const char *final_combiner_src = "ret";
2893 /* Find out which textures are read */
2894 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2895 if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
2896 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
2897 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
2898 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
2899 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2900 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2901 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2903 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
2904 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
2905 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
2906 bump_used[stage] = TRUE;
2907 tex_read[stage] = TRUE;
2909 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2910 bump_used[stage] = TRUE;
2911 tex_read[stage] = TRUE;
2912 luminance_used[stage] = TRUE;
2913 } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
2914 tfactor_used = TRUE;
2917 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2918 tfactor_used = TRUE;
2921 if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
2922 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2923 tempreg_used = TRUE;
2926 if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
2927 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
2928 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
2929 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
2930 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2931 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2932 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2934 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2935 tempreg_used = TRUE;
2937 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2938 tfactor_used = TRUE;
2943 shader_buffer_init(&buffer);
2945 shader_addline(&buffer, "!!ARBfp1.0\n");
2947 switch(settings->fog) {
2948 case FOG_OFF: break;
2949 case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
2950 case FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
2951 case FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
2952 default: FIXME("Unexpected fog setting %d\n", settings->fog);
2955 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
2956 shader_addline(&buffer, "TEMP TMP;\n");
2957 shader_addline(&buffer, "TEMP ret;\n");
2958 if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
2959 shader_addline(&buffer, "TEMP arg0;\n");
2960 shader_addline(&buffer, "TEMP arg1;\n");
2961 shader_addline(&buffer, "TEMP arg2;\n");
2962 for(stage = 0; stage < MAX_TEXTURES; stage++) {
2963 if(!tex_read[stage]) continue;
2964 shader_addline(&buffer, "TEMP tex%u;\n", stage);
2965 if(!bump_used[stage]) continue;
2966 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
2967 if(!luminance_used[stage]) continue;
2968 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
2971 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
2973 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
2975 if(settings->sRGB_write) {
2976 shader_addline(&buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
2977 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
2978 shader_addline(&buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
2979 srgb_sub_high, 0.0, 0.0, 0.0);
2982 /* Generate texture sampling instructions) */
2983 for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
2984 if(!tex_read[stage]) continue;
2986 switch(settings->op[stage].tex_type) {
2987 case tex_1d: textype = "1D"; break;
2988 case tex_2d: textype = "2D"; break;
2989 case tex_3d: textype = "3D"; break;
2990 case tex_cube: textype = "CUBE"; break;
2991 case tex_rect: textype = "RECT"; break;
2992 default: textype = "unexpected_textype"; break;
2995 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
2996 settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
3002 if(settings->op[stage].projected == proj_none) {
3004 } else if(settings->op[stage].projected == proj_count4 ||
3005 settings->op[stage].projected == proj_count3) {
3008 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
3013 (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
3014 settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
3015 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
3016 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
3017 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
3018 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
3020 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3021 * so multiply the displacement with the dividing parameter before passing it to TXP
3023 if (settings->op[stage].projected != proj_none) {
3024 if(settings->op[stage].projected == proj_count4) {
3025 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
3026 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
3028 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
3029 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
3032 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
3035 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
3036 instr, sat, stage, stage, textype);
3037 if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
3038 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
3039 stage - 1, stage - 1, stage - 1);
3040 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
3042 } else if(settings->op[stage].projected == proj_count3) {
3043 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
3044 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
3045 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
3046 instr, sat, stage, stage, textype);
3048 shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
3049 instr, sat, stage, stage, stage, textype);
3052 sprintf(colorcor_dst, "tex%u", stage);
3053 gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
3054 settings->op[stage].color_fixup);
3057 /* Generate the main shader */
3058 for(stage = 0; stage < MAX_TEXTURES; stage++) {
3059 if(settings->op[stage].cop == WINED3DTOP_DISABLE) {
3061 final_combiner_src = "fragment.color.primary";
3066 if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
3067 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
3068 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
3069 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
3070 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
3071 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
3072 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
3073 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
3074 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
3075 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
3076 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
3077 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
3079 op_equal = settings->op[stage].aop == settings->op[stage].cop &&
3080 settings->op[stage].carg0 == settings->op[stage].aarg0 &&
3081 settings->op[stage].carg1 == settings->op[stage].aarg1 &&
3082 settings->op[stage].carg2 == settings->op[stage].aarg2;
3085 if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
3086 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
3087 settings->op[stage].cop, settings->op[stage].carg0,
3088 settings->op[stage].carg1, settings->op[stage].carg2);
3090 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
3092 } else if(op_equal) {
3093 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
3094 settings->op[stage].cop, settings->op[stage].carg0,
3095 settings->op[stage].carg1, settings->op[stage].carg2);
3097 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
3098 settings->op[stage].cop, settings->op[stage].carg0,
3099 settings->op[stage].carg1, settings->op[stage].carg2);
3100 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
3101 settings->op[stage].aop, settings->op[stage].aarg0,
3102 settings->op[stage].aarg1, settings->op[stage].aarg2);
3106 if(settings->sRGB_write) {
3107 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
3108 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2");
3109 shader_addline(&buffer, "MOV result.color.w, ret.w;\n");
3111 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
3115 shader_addline(&buffer, "END\n");
3117 /* Generate the shader */
3118 GL_EXTCALL(glGenProgramsARB(1, &ret));
3119 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
3120 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
3122 if (glGetError() == GL_INVALID_OPERATION) {
3124 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3125 FIXME("Fragment program error at position %d: %s\n", pos,
3126 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3128 shader_buffer_free(&buffer);
3132 static void fragment_prog_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3133 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
3134 struct shader_arb_priv *priv = device->fragment_priv;
3135 BOOL use_pshader = use_ps(stateblock);
3136 BOOL use_vshader = use_vs(stateblock);
3137 struct ffp_frag_settings settings;
3138 const struct arbfp_ffp_desc *desc;
3141 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
3143 if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
3144 if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
3145 /* Reload fixed function constants since they collide with the pixel shader constants */
3146 for(i = 0; i < MAX_TEXTURES; i++) {
3147 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
3149 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
3150 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
3151 } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3152 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3158 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
3159 gen_ffp_frag_op(stateblock, &settings, FALSE);
3160 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(priv->fragment_shaders, &settings);
3162 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
3165 ERR("Out of memory\n");
3168 new_desc->num_textures_used = 0;
3169 for(i = 0; i < GL_LIMITS(texture_stages); i++) {
3170 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
3171 new_desc->num_textures_used = i;
3174 memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
3175 new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
3176 add_ffp_frag_shader(priv->fragment_shaders, &new_desc->parent);
3177 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
3181 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
3182 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
3185 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
3186 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
3187 priv->current_fprogram_id = desc->shader;
3189 if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
3190 /* Reload fixed function constants since they collide with the pixel shader constants */
3191 for(i = 0; i < MAX_TEXTURES; i++) {
3192 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
3194 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
3195 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
3197 context->last_was_pshader = FALSE;
3199 context->last_was_pshader = TRUE;
3202 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
3203 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
3204 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
3205 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
3206 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
3208 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
3211 if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3212 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3214 if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
3215 device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
3219 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
3223 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
3224 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
3225 * state table, so we need to handle that with a forwarding function. The other invisible side effect
3226 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
3227 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
3229 static void state_arbfp_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3230 enum fogsource new_source;
3232 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
3234 if(!isStateDirty(context, STATE_PIXELSHADER)) {
3235 fragment_prog_arbfp(state, stateblock, context);
3238 if(!stateblock->renderState[WINED3DRS_FOGENABLE]) return;
3240 if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
3241 if(use_vs(stateblock)) {
3242 new_source = FOGSOURCE_VS;
3244 if(stateblock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || context->last_was_rhw) {
3245 new_source = FOGSOURCE_COORD;
3247 new_source = FOGSOURCE_FFP;
3251 new_source = FOGSOURCE_FFP;
3253 if(new_source != context->fog_source) {
3254 context->fog_source = new_source;
3255 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART), stateblock, context);
3259 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3260 if(!isStateDirty(context, STATE_PIXELSHADER)) {
3261 fragment_prog_arbfp(state, stateblock, context);
3265 #undef GLINFO_LOCATION
3267 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
3268 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR), state_texfactor_arbfp }, 0 },
3269 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3270 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3271 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3272 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3273 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3274 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3275 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3276 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3277 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3278 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3279 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3280 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3281 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3282 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3283 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3284 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3285 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3286 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3287 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3288 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3289 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3290 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3291 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3292 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3293 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3294 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3295 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3296 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3297 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3298 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3299 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3300 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3301 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3302 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3303 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3304 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3305 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3306 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3307 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3308 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3309 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3310 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3311 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3312 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3313 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3314 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3315 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3316 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3317 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3318 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3319 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3320 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3321 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3322 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3323 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3324 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3325 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3326 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3327 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3328 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3329 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3330 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3331 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3332 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3333 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3334 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3335 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3336 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3337 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3338 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3339 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3340 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3341 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3342 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3343 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3344 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3345 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3346 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3347 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3348 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3349 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3350 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3351 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3352 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3353 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3354 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3355 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3356 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3357 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3358 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3359 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3360 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3361 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3362 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3363 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3364 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3365 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3366 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3367 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3368 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3369 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3370 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3371 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3372 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3373 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3374 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3375 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3376 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3377 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3378 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3379 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3380 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3381 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3382 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3383 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3384 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3385 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3386 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, 0 },
3387 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3388 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, 0 },
3389 {STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim }, 0 },
3390 {STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim }, 0 },
3391 {STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim }, 0 },
3392 {STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim }, 0 },
3393 {STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim }, 0 },
3394 {STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim }, 0 },
3395 {STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim }, 0 },
3396 {STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim }, 0 },
3397 {STATE_PIXELSHADER, { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3398 {STATE_RENDER(WINED3DRS_FOGENABLE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3399 {STATE_RENDER(WINED3DRS_FOGTABLEMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3400 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, 0 },
3401 {STATE_RENDER(WINED3DRS_FOGSTART), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, 0 },
3402 {STATE_RENDER(WINED3DRS_FOGEND), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, 0 },
3403 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE), { STATE_PIXELSHADER, fragment_prog_arbfp }, 0 },
3404 {STATE_RENDER(WINED3DRS_FOGCOLOR), { STATE_RENDER(WINED3DRS_FOGCOLOR), state_fogcolor }, 0 },
3405 {STATE_RENDER(WINED3DRS_FOGDENSITY), { STATE_RENDER(WINED3DRS_FOGDENSITY), state_fogdensity }, 0 },
3406 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3407 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3408 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3409 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3410 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3411 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3412 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3413 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, 0 },
3414 {STATE_RENDER(WINED3DRS_SPECULARENABLE), { STATE_RENDER(WINED3DRS_SPECULARENABLE), state_arb_specularenable}, 0 },
3415 {0 /* Terminate */, { 0, 0 }, 0 },
3418 const struct fragment_pipeline arbfp_fragment_pipeline = {
3423 shader_arb_color_fixup_supported,
3424 arbfp_fragmentstate_template,
3425 TRUE /* We can disable projected textures */
3428 #define GLINFO_LOCATION device->adapter->gl_info
3430 struct arbfp_blit_priv {
3431 GLenum yuy2_rect_shader, yuy2_2d_shader;
3432 GLenum uyvy_rect_shader, uyvy_2d_shader;
3433 GLenum yv12_rect_shader, yv12_2d_shader;
3436 static HRESULT arbfp_blit_alloc(IWineD3DDevice *iface) {
3437 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3438 device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
3439 if(!device->blit_priv) {
3440 ERR("Out of memory\n");
3441 return E_OUTOFMEMORY;
3445 static void arbfp_blit_free(IWineD3DDevice *iface) {
3446 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3447 struct arbfp_blit_priv *priv = device->blit_priv;
3450 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
3451 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
3452 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
3453 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
3454 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
3455 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
3456 checkGLcall("Delete yuv programs\n");
3460 static BOOL gen_planar_yuv_read(SHADER_BUFFER *buffer, enum yuv_fixup yuv_fixup, GLenum textype, char *luminance)
3463 const char *tex, *texinstr;
3465 if (yuv_fixup == YUV_FIXUP_UYVY) {
3473 case GL_TEXTURE_2D: tex = "2D"; texinstr = "TXP"; break;
3474 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; texinstr = "TEX"; break;
3476 /* This is more tricky than just replacing the texture type - we have to navigate
3477 * properly in the texture to find the correct chroma values
3479 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
3483 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
3484 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
3485 * filtering when we sample the texture.
3487 * These are the rules for reading the chroma:
3493 * So we have to get the sampling x position in non-normalized coordinates in integers
3495 if(textype != GL_TEXTURE_RECTANGLE_ARB) {
3496 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
3497 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
3499 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3501 /* We must not allow filtering between pixel x and x+1, this would mix U and V
3502 * Vertical filtering is ok. However, bear in mind that the pixel center is at
3505 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
3506 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
3508 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
3509 * even and odd pixels respectively
3511 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
3512 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
3514 /* Sample Pixel 1 */
3515 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3517 /* Put the value into either of the chroma values */
3518 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3519 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
3520 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3521 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
3523 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
3524 * the pixel right to the current one. Otherwise, sample the left pixel.
3525 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
3527 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
3528 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
3529 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3531 /* Put the value into the other chroma */
3532 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3533 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
3534 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3535 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
3537 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
3538 * the current one and lerp the two U and V values
3541 /* This gives the correctly filtered luminance value */
3542 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
3547 static BOOL gen_yv12_read(SHADER_BUFFER *buffer, GLenum textype, char *luminance)
3552 case GL_TEXTURE_2D: tex = "2D"; break;
3553 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; break;
3555 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
3559 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
3560 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
3561 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
3562 * pitch of the luminance plane, the packing into the gl texture is a bit
3563 * unfortunate. If the whole texture is interpreted as luminance data it looks
3564 * approximately like this:
3566 * +----------------------------------+----
3578 * +----------------+-----------------+----
3580 * | U even rows | U odd rows |
3582 * +----------------+------------------ -
3584 * | V even rows | V odd rows |
3586 * +----------------+-----------------+----
3590 * So it appears as if there are 4 chroma images, but in fact the odd rows
3591 * in the chroma images are in the same row as the even ones. So its is
3592 * kinda tricky to read
3594 * When reading from rectangle textures, keep in mind that the input y coordinates
3595 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
3597 shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
3598 2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
3600 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3601 /* the chroma planes have only half the width */
3602 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
3604 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
3605 * the coordinate. Also read the right side of the image when reading odd lines
3607 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
3610 if(textype == GL_TEXTURE_2D) {
3612 shader_addline(buffer, "RCP chroma.w, size.y;\n");
3614 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
3616 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
3617 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
3619 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3620 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3621 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3622 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3623 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3625 /* clamp, keep the half pixel origin in mind */
3626 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
3627 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3628 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
3629 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3631 /* Read from [size - size+size/4] */
3632 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3633 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
3635 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3636 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3637 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3638 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3639 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
3640 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3642 /* Make sure to read exactly from the pixel center */
3643 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3644 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
3647 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
3648 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
3649 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3650 shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
3651 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3653 /* Read the texture, put the result into the output register */
3654 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3655 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
3657 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
3658 * No need to clamp because we're just reusing the already clamped value from above
3660 if(textype == GL_TEXTURE_2D) {
3661 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
3663 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
3665 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3666 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
3668 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
3669 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
3670 * values due to filtering
3672 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3673 if(textype == GL_TEXTURE_2D) {
3674 /* Multiply the y coordinate by 2/3 and clamp it */
3675 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
3676 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
3677 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3678 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3680 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
3681 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
3684 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
3685 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
3686 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3693 static GLuint gen_yuv_shader(IWineD3DDeviceImpl *device, enum yuv_fixup yuv_fixup, GLenum textype)
3696 SHADER_BUFFER buffer;
3697 char luminance_component;
3698 struct arbfp_blit_priv *priv = device->blit_priv;
3701 shader_buffer_init(&buffer);
3704 GL_EXTCALL(glGenProgramsARB(1, &shader));
3705 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
3706 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3707 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3710 shader_buffer_free(&buffer);
3714 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
3715 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
3716 * two chroma(U and V) values. Each macropixel has two luminance values, one for
3717 * each single pixel it contains, and one U and one V value shared between both
3720 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
3721 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
3722 * take the format into account when generating the read swizzles
3724 * Reading the Y value is straightforward - just sample the texture. The hardware
3725 * takes care of filtering in the horizontal and vertical direction.
3727 * Reading the U and V values is harder. We have to avoid filtering horizontally,
3728 * because that would mix the U and V values of one pixel or two adjacent pixels.
3729 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
3730 * regardless of the filtering setting. Vertical filtering works automatically
3731 * though - the U and V values of two rows are mixed nicely.
3733 * Appart of avoiding filtering issues, the code has to know which value it just
3734 * read, and where it can find the other one. To determine this, it checks if
3735 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
3737 * Handling horizontal filtering of U and V values requires reading a 2nd pair
3738 * of pixels, extracting U and V and mixing them. This is not implemented yet.
3740 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
3741 * with width / 2. This way one read gives all 3 values, finding U and V is easy
3742 * in an unfiltered situation. Finding the luminance on the other hand requires
3743 * finding out if it is an odd or even pixel. The real drawback of this approach
3744 * is filtering. This would have to be emulated completely in the shader, reading
3745 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
3746 * vertically. Beyond that it would require adjustments to the texture handling
3747 * code to deal with the width scaling
3749 shader_addline(&buffer, "!!ARBfp1.0\n");
3750 shader_addline(&buffer, "TEMP luminance;\n");
3751 shader_addline(&buffer, "TEMP temp;\n");
3752 shader_addline(&buffer, "TEMP chroma;\n");
3753 shader_addline(&buffer, "TEMP texcrd;\n");
3754 shader_addline(&buffer, "TEMP texcrd2;\n");
3755 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
3756 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
3757 shader_addline(&buffer, "PARAM size = program.local[0];\n");
3761 case YUV_FIXUP_UYVY:
3762 case YUV_FIXUP_YUY2:
3763 if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
3765 shader_buffer_free(&buffer);
3770 case YUV_FIXUP_YV12:
3771 if (!gen_yv12_read(&buffer, textype, &luminance_component))
3773 shader_buffer_free(&buffer);
3779 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3780 shader_buffer_free(&buffer);
3784 /* Calculate the final result. Formula is taken from
3785 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
3786 * ranges from -0.5 to 0.5
3788 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
3790 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
3791 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
3792 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
3793 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
3794 shader_addline(&buffer, "END\n");
3797 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
3799 if (glGetError() == GL_INVALID_OPERATION) {
3801 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3802 FIXME("Fragment program error at position %d: %s\n", pos,
3803 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3805 shader_buffer_free(&buffer);
3810 case YUV_FIXUP_YUY2:
3811 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
3812 else priv->yuy2_2d_shader = shader;
3815 case YUV_FIXUP_UYVY:
3816 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
3817 else priv->uyvy_2d_shader = shader;
3820 case YUV_FIXUP_YV12:
3821 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
3822 else priv->yv12_2d_shader = shader;
3829 static HRESULT arbfp_blit_set(IWineD3DDevice *iface, const struct GlPixelFormatDesc *format_desc,
3830 GLenum textype, UINT width, UINT height)
3833 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3834 float size[4] = {width, height, 1, 1};
3835 struct arbfp_blit_priv *priv = device->blit_priv;
3836 enum yuv_fixup yuv_fixup;
3838 if (!is_yuv_fixup(format_desc->color_fixup))
3841 dump_color_fixup_desc(format_desc->color_fixup);
3842 /* Don't bother setting up a shader for unconverted formats */
3845 checkGLcall("glEnable(textype)");
3850 yuv_fixup = get_yuv_fixup(format_desc->color_fixup);
3854 case YUV_FIXUP_YUY2:
3855 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
3858 case YUV_FIXUP_UYVY:
3859 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
3862 case YUV_FIXUP_YV12:
3863 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
3867 FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup);
3870 checkGLcall("glEnable(textype)");
3875 if (!shader) shader = gen_yuv_shader(device, yuv_fixup, textype);
3878 glEnable(GL_FRAGMENT_PROGRAM_ARB);
3879 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
3880 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3881 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3882 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
3883 checkGLcall("glProgramLocalParameter4fvARB");
3889 static void arbfp_blit_unset(IWineD3DDevice *iface) {
3890 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3893 glDisable(GL_FRAGMENT_PROGRAM_ARB);
3894 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3895 glDisable(GL_TEXTURE_2D);
3896 checkGLcall("glDisable(GL_TEXTURE_2D)");
3897 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
3898 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3899 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3901 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3902 glDisable(GL_TEXTURE_RECTANGLE_ARB);
3903 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
3908 static BOOL arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup)
3910 enum yuv_fixup yuv_fixup;
3912 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3914 TRACE("Checking support for fixup:\n");
3915 dump_color_fixup_desc(fixup);
3918 if (is_identity_fixup(fixup))
3924 /* We only support YUV conversions. */
3925 if (!is_yuv_fixup(fixup))
3927 TRACE("[FAILED]\n");
3931 yuv_fixup = get_yuv_fixup(fixup);
3934 case YUV_FIXUP_YUY2:
3935 case YUV_FIXUP_UYVY:
3936 case YUV_FIXUP_YV12:
3941 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3942 TRACE("[FAILED]\n");
3947 const struct blit_shader arbfp_blit = {
3952 arbfp_blit_color_fixup_supported,
3955 #undef GLINFO_LOCATION