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 struct wined3d_gl_info *gl_info)
47 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) shader;
48 if(!This->baseShader.reg_maps.usesmova) return FALSE;
49 return !GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION);
52 /* Returns TRUE if result.clip from GL_NV_vertex_program2 should be used and FALSE otherwise */
53 static inline BOOL use_nv_clip(const struct wined3d_gl_info *gl_info)
55 return GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION);
58 static BOOL need_helper_const(const struct wined3d_gl_info *gl_info)
60 if (!GL_SUPPORT(NV_VERTEX_PROGRAM) /* Need to init colors. */
61 || gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT /* Load the immval offset. */
62 || gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W /* Have to init texcoords. */
63 || (!use_nv_clip(gl_info)) /* Init the clip texcoord */)
70 static unsigned int reserved_vs_const(IWineD3DBaseShader *shader, const struct wined3d_gl_info *gl_info)
73 /* We use one PARAM for the pos fixup, and in some cases one to load
74 * some immediate values into the shader
76 if(need_helper_const(gl_info)) ret++;
77 if(need_mova_const(shader, gl_info)) ret++;
81 static inline BOOL ffp_clip_emul(IWineD3DStateBlockImpl *stateblock)
83 return stateblock->lowest_disabled_stage < 7;
86 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
87 * so upload them above that
89 #define ARB_SHADER_PRIVCONST_BASE (GL_LIMITS(vshader_constantsF) - 1)
90 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
92 /* ARB_program_shader private data */
108 unsigned int loop_no;
111 struct wined3d_shader_loop_control loop_control;
115 struct arb_ps_np2fixup_info
117 struct ps_np2fixup_info super;
118 /* For ARB we need a offset value:
119 * With both GLSL and ARB mode the NP2 fixup information (the texture dimensions) are stored in a
120 * consecutive way (GLSL uses a uniform array). Since ARB doesn't know the notion of a "standalone"
121 * array we need an offset to the index inside the program local parameter array. */
125 struct arb_ps_compile_args
127 struct ps_compile_args super;
129 WORD clip; /* only a boolean, use a WORD for alignment */
130 unsigned char loop_ctrl[MAX_CONST_I][3];
133 struct stb_const_desc
135 unsigned char texunit;
139 struct arb_ps_compiled_shader
141 struct arb_ps_compile_args args;
142 struct arb_ps_np2fixup_info np2fixup_info;
143 struct stb_const_desc bumpenvmatconst[MAX_TEXTURES];
144 struct stb_const_desc luminanceconst[MAX_TEXTURES];
145 UINT int_consts[MAX_CONST_I];
148 unsigned char numbumpenvmatconsts;
152 struct arb_vs_compile_args
154 struct vs_compile_args super;
160 char clip_control[2];
162 DWORD boolclip_compare;
167 unsigned char vertex_samplers[4];
168 DWORD vertex_samplers_compare;
170 unsigned char loop_ctrl[MAX_CONST_I][3];
173 struct arb_vs_compiled_shader
175 struct arb_vs_compile_args args;
177 UINT int_consts[MAX_CONST_I];
179 char need_color_unclamp;
183 struct recorded_instruction
185 struct wined3d_shader_instruction ins;
189 struct shader_arb_ctx_priv
194 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
196 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
198 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
202 const struct arb_vs_compile_args *cur_vs_args;
203 const struct arb_ps_compile_args *cur_ps_args;
204 const struct arb_ps_compiled_shader *compiled_fprog;
205 const struct arb_vs_compiled_shader *compiled_vprog;
206 struct arb_ps_np2fixup_info *cur_np2fixup_info;
207 struct list control_frames;
211 unsigned int num_loops, loop_depth, num_ifcs;
214 unsigned int vs_clipplanes;
218 /* For 3.0 vertex shaders */
219 const char *vs_output[MAX_REG_OUTPUT];
220 /* For 2.x and earlier vertex shaders */
221 const char *texcrd_output[8], *color_output[2], *fog_output;
223 /* 3.0 pshader input for compatibility with fixed function */
224 const char *ps_input[MAX_REG_INPUT];
229 struct wined3d_shader_signature_element *sig;
231 struct wine_rb_entry entry;
234 struct arb_pshader_private {
235 struct arb_ps_compiled_shader *gl_shaders;
236 UINT num_gl_shaders, shader_array_size;
237 BOOL has_signature_idx;
238 DWORD input_signature_idx;
239 DWORD clipplane_emulation;
243 struct arb_vshader_private {
244 struct arb_vs_compiled_shader *gl_shaders;
245 UINT num_gl_shaders, shader_array_size;
248 struct shader_arb_priv
250 GLuint current_vprogram_id;
251 GLuint current_fprogram_id;
252 const struct arb_ps_compiled_shader *compiled_fprog;
253 const struct arb_vs_compiled_shader *compiled_vprog;
254 GLuint depth_blt_vprogram_id;
255 GLuint depth_blt_fprogram_id[tex_type_count];
256 BOOL use_arbfp_fixed_func;
257 struct wine_rb_tree fragment_shaders;
258 BOOL last_ps_const_clamped;
259 BOOL last_vs_color_unclamp;
261 struct wine_rb_tree signature_tree;
265 /********************************************************
266 * ARB_[vertex/fragment]_program helper functions follow
267 ********************************************************/
269 /* Loads floating point constants into the currently set ARB_vertex/fragment_program.
270 * When constant_list == NULL, it will load all the constants.
272 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
273 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
275 /* GL locking is done by the caller */
276 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl *This, const struct wined3d_gl_info *gl_info,
277 GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
279 local_constant* lconst;
283 if (TRACE_ON(d3d_shader)) {
284 for(i = 0; i < max_constants; i++) {
285 if(!dirty_consts[i]) continue;
286 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
287 constants[i * 4 + 0], constants[i * 4 + 1],
288 constants[i * 4 + 2], constants[i * 4 + 3]);
294 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
295 if (target_type == GL_FRAGMENT_PROGRAM_ARB && This->baseShader.reg_maps.shader_version.major == 1)
298 /* ps 1.x supports only 8 constants, clamp only those. When switching between 1.x and higher
299 * shaders, the first 8 constants are marked dirty for reload
301 for(; i < min(8, max_constants); i++) {
302 if(!dirty_consts[i]) continue;
306 if (constants[j + 0] > 1.0f) lcl_const[0] = 1.0f;
307 else if (constants[j + 0] < -1.0f) lcl_const[0] = -1.0f;
308 else lcl_const[0] = constants[j + 0];
310 if (constants[j + 1] > 1.0f) lcl_const[1] = 1.0f;
311 else if (constants[j + 1] < -1.0f) lcl_const[1] = -1.0f;
312 else lcl_const[1] = constants[j + 1];
314 if (constants[j + 2] > 1.0f) lcl_const[2] = 1.0f;
315 else if (constants[j + 2] < -1.0f) lcl_const[2] = -1.0f;
316 else lcl_const[2] = constants[j + 2];
318 if (constants[j + 3] > 1.0f) lcl_const[3] = 1.0f;
319 else if (constants[j + 3] < -1.0f) lcl_const[3] = -1.0f;
320 else lcl_const[3] = constants[j + 3];
322 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
325 /* If further constants are dirty, reload them without clamping.
327 * The alternative is not to touch them, but then we cannot reset the dirty constant count
328 * to zero. That's bad for apps that only use PS 1.x shaders, because in that case the code
329 * above would always re-check the first 8 constants since max_constant remains at the init
334 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
335 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
336 * or just reloading *all* constants at once
338 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, i, max_constants, constants + (i * 4)));
340 for(; i < max_constants; i++) {
341 if(!dirty_consts[i]) continue;
343 /* Find the next block of dirty constants */
346 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
350 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
353 for(; i < max_constants; i++) {
354 if(dirty_consts[i]) {
356 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
360 checkGLcall("glProgramEnvParameter4fvARB()");
362 /* Load immediate constants */
363 if(This->baseShader.load_local_constsF) {
364 if (TRACE_ON(d3d_shader)) {
365 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
366 GLfloat* values = (GLfloat*)lconst->value;
367 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
368 values[0], values[1], values[2], values[3]);
371 /* Immediate constants are clamped for 1.X shaders at loading times */
373 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
374 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
375 ret = max(ret, lconst->idx + 1);
376 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
378 checkGLcall("glProgramEnvParameter4fvARB()");
379 return ret; /* The loaded immediate constants need reloading for the next shader */
381 return 0; /* No constants are dirty now */
386 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
388 static void shader_arb_load_np2fixup_constants(
389 IWineD3DDevice* device,
391 char useVertexShader) {
393 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl *) device;
394 const struct shader_arb_priv* const priv = (const struct shader_arb_priv *) deviceImpl->shader_priv;
395 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
396 const struct wined3d_gl_info *gl_info = &deviceImpl->adapter->gl_info;
398 if (!usePixelShader) {
399 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
403 if (priv->compiled_fprog && priv->compiled_fprog->np2fixup_info.super.active) {
404 const struct arb_ps_np2fixup_info* const fixup = &priv->compiled_fprog->np2fixup_info;
406 WORD active = fixup->super.active;
407 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
409 for (i = 0; active; active >>= 1, ++i) {
410 const unsigned char idx = fixup->super.idx[i];
411 const IWineD3DTextureImpl* const tex = (const IWineD3DTextureImpl*) stateBlock->textures[i];
412 GLfloat* tex_dim = &np2fixup_constants[(idx >> 1) * 4];
414 if (!(active & 1)) continue;
417 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
422 tex_dim[2] = tex->baseTexture.pow2Matrix[0]; tex_dim[3] = tex->baseTexture.pow2Matrix[5];
424 tex_dim[0] = tex->baseTexture.pow2Matrix[0]; tex_dim[1] = tex->baseTexture.pow2Matrix[5];
428 for (i = 0; i < fixup->super.num_consts; ++i) {
429 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
430 fixup->offset + i, &np2fixup_constants[i * 4]));
435 /* GL locking is done by the caller. */
436 static inline void shader_arb_ps_local_constants(IWineD3DDeviceImpl* deviceImpl)
438 const struct wined3d_context *context = context_get_current();
439 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
440 const struct wined3d_gl_info *gl_info = context->gl_info;
442 struct shader_arb_priv *priv = deviceImpl->shader_priv;
443 const struct arb_ps_compiled_shader *gl_shader = priv->compiled_fprog;
445 for(i = 0; i < gl_shader->numbumpenvmatconsts; i++)
447 int texunit = gl_shader->bumpenvmatconst[i].texunit;
449 /* The state manager takes care that this function is always called if the bump env matrix changes */
450 const float *data = (const float *)&stateBlock->textureState[texunit][WINED3DTSS_BUMPENVMAT00];
451 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->bumpenvmatconst[i].const_num, data));
453 if (gl_shader->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED)
455 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
456 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
457 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
458 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
460 const float *scale = (const float *)&stateBlock->textureState[texunit][WINED3DTSS_BUMPENVLSCALE];
461 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->luminanceconst[i].const_num, scale));
464 checkGLcall("Load bumpmap consts");
466 if(gl_shader->ycorrection != WINED3D_CONST_NUM_UNUSED)
468 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
469 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
474 val[0] = context->render_offscreen ? 0.0f
475 : ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
476 val[1] = context->render_offscreen ? 1.0f : -1.0f;
479 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->ycorrection, val));
480 checkGLcall("y correction loading");
483 if(gl_shader->num_int_consts == 0) return;
485 for(i = 0; i < MAX_CONST_I; i++)
487 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
490 val[0] = stateBlock->pixelShaderConstantI[4 * i];
491 val[1] = stateBlock->pixelShaderConstantI[4 * i + 1];
492 val[2] = stateBlock->pixelShaderConstantI[4 * i + 2];
495 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->int_consts[i], val));
498 checkGLcall("Load ps int consts");
501 /* GL locking is done by the caller. */
502 static inline void shader_arb_vs_local_constants(IWineD3DDeviceImpl* deviceImpl)
504 IWineD3DStateBlockImpl* stateBlock;
505 const struct wined3d_gl_info *gl_info = &deviceImpl->adapter->gl_info;
507 struct shader_arb_priv *priv = deviceImpl->shader_priv;
508 const struct arb_vs_compiled_shader *gl_shader = priv->compiled_vprog;
510 /* Upload the position fixup */
511 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->pos_fixup, deviceImpl->posFixup));
513 if(gl_shader->num_int_consts == 0) return;
515 stateBlock = deviceImpl->stateBlock;
517 for(i = 0; i < MAX_CONST_I; i++)
519 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
522 val[0] = stateBlock->vertexShaderConstantI[4 * i];
523 val[1] = stateBlock->vertexShaderConstantI[4 * i + 1];
524 val[2] = stateBlock->vertexShaderConstantI[4 * i + 2];
527 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->int_consts[i], val));
530 checkGLcall("Load vs int consts");
534 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
536 * We only support float constants in ARB at the moment, so don't
537 * worry about the Integers or Booleans
539 /* GL locking is done by the caller (state handler) */
540 static void shader_arb_load_constants(const struct wined3d_context *context, char usePixelShader, char useVertexShader)
542 IWineD3DDeviceImpl *device = ((IWineD3DSurfaceImpl *)context->surface)->resource.wineD3DDevice;
543 IWineD3DStateBlockImpl* stateBlock = device->stateBlock;
544 const struct wined3d_gl_info *gl_info = context->gl_info;
546 if (useVertexShader) {
547 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
549 /* Load DirectX 9 float constants for vertex shader */
550 device->highest_dirty_vs_const = shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
551 device->highest_dirty_vs_const, stateBlock->vertexShaderConstantF, context->vshader_const_dirty);
552 shader_arb_vs_local_constants(device);
555 if (usePixelShader) {
556 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
558 /* Load DirectX 9 float constants for pixel shader */
559 device->highest_dirty_ps_const = shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
560 device->highest_dirty_ps_const, stateBlock->pixelShaderConstantF, context->pshader_const_dirty);
561 shader_arb_ps_local_constants(device);
565 static void shader_arb_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
567 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
568 struct wined3d_context *context = context_get_current();
570 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
571 * context. On a context switch the old context will be fully dirtified */
572 if (!context || ((IWineD3DSurfaceImpl *)context->surface)->resource.wineD3DDevice != This) return;
574 memset(context->vshader_const_dirty + start, 1, sizeof(*context->vshader_const_dirty) * count);
575 This->highest_dirty_vs_const = max(This->highest_dirty_vs_const, start + count);
578 static void shader_arb_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
580 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
581 struct wined3d_context *context = context_get_current();
583 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
584 * context. On a context switch the old context will be fully dirtified */
585 if (!context || ((IWineD3DSurfaceImpl *)context->surface)->resource.wineD3DDevice != This) return;
587 memset(context->pshader_const_dirty + start, 1, sizeof(*context->pshader_const_dirty) * count);
588 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, start + count);
591 static DWORD *local_const_mapping(IWineD3DBaseShaderImpl *This)
595 const local_constant *lconst;
597 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) return NULL;
599 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * This->baseShader.limits.constant_float);
601 ERR("Out of memory\n");
605 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
606 ret[lconst->idx] = idx++;
611 /* Generate the variable & register declarations for the ARB_vertex_program output target */
612 static DWORD shader_generate_arb_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
613 struct wined3d_shader_buffer *buffer, const struct wined3d_gl_info *gl_info, DWORD *lconst_map,
614 DWORD *num_clipplanes, struct shader_arb_ctx_priv *ctx)
616 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
617 DWORD i, next_local = 0;
618 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
619 unsigned max_constantsF;
620 const local_constant *lconst;
623 /* In pixel shaders, all private constants are program local, we don't need anything
624 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
625 * If we need a private constant the GL implementation will squeeze it in somewhere
627 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
628 * immediate values. The posFixup is loaded using program.env for now, so always
629 * subtract one from the number of constants. If the shader uses indirect addressing,
630 * account for the helper const too because we have to declare all availabke d3d constants
631 * and don't know which are actually used.
634 max_constantsF = GL_LIMITS(pshader_constantsF);
636 if(This->baseShader.reg_maps.usesrelconstF) {
637 DWORD highest_constf = 0, clip_limit;
638 max_constantsF = GL_LIMITS(vshader_constantsF) - reserved_vs_const(iface, gl_info);
639 max_constantsF -= count_bits(This->baseShader.reg_maps.integer_constants);
641 for(i = 0; i < This->baseShader.limits.constant_float; i++)
644 DWORD shift = i & 0x1f;
645 if(reg_maps->constf[idx] & (1 << shift)) highest_constf = i;
648 clip_limit = GL_LIMITS(clipplanes);
649 if(ctx->target_version == ARB) clip_limit = min(clip_limit, 4);
650 *num_clipplanes = min(clip_limit, max_constantsF - highest_constf - 1);
651 max_constantsF -= *num_clipplanes;
652 if(*num_clipplanes < clip_limit)
654 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes, GL_LIMITS(clipplanes));
659 if(ctx->target_version >= NV2) *num_clipplanes = GL_LIMITS(clipplanes);
660 else *num_clipplanes = min(GL_LIMITS(clipplanes), 4);
661 max_constantsF = GL_LIMITS(vshader_constantsF);
665 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
667 if (map & 1) shader_addline(buffer, "TEMP R%u;\n", i);
670 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
672 if (map & 1) shader_addline(buffer, "ADDRESS A%u;\n", i);
675 if (pshader && reg_maps->shader_version.major == 1 && reg_maps->shader_version.minor <= 3)
677 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
679 if (map & 1) shader_addline(buffer, "TEMP T%u;\n", i);
683 /* Load local constants using the program-local space,
684 * this avoids reloading them each time the shader is used
687 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
688 shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
689 lconst_map[lconst->idx]);
690 next_local = max(next_local, lconst_map[lconst->idx] + 1);
694 /* we use the array-based constants array if the local constants are marked for loading,
695 * because then we use indirect addressing, or when the local constant list is empty,
696 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
697 * local constants do not declare the loaded constants as an array because ARB compilers usually
698 * do not optimize unused constants away
700 if(This->baseShader.reg_maps.usesrelconstF) {
701 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
702 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
703 max_constantsF, max_constantsF - 1);
705 for(i = 0; i < max_constantsF; i++) {
708 mask = 1 << (i & 0x1f);
709 if(!shader_constant_is_local(This, i) && (This->baseShader.reg_maps.constf[idx] & mask)) {
710 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
718 static const char * const shift_tab[] = {
719 "dummy", /* 0 (none) */
720 "coefmul.x", /* 1 (x2) */
721 "coefmul.y", /* 2 (x4) */
722 "coefmul.z", /* 3 (x8) */
723 "coefmul.w", /* 4 (x16) */
724 "dummy", /* 5 (x32) */
725 "dummy", /* 6 (x64) */
726 "dummy", /* 7 (x128) */
727 "dummy", /* 8 (d256) */
728 "dummy", /* 9 (d128) */
729 "dummy", /* 10 (d64) */
730 "dummy", /* 11 (d32) */
731 "coefdiv.w", /* 12 (d16) */
732 "coefdiv.z", /* 13 (d8) */
733 "coefdiv.y", /* 14 (d4) */
734 "coefdiv.x" /* 15 (d2) */
737 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction *ins,
738 const struct wined3d_shader_dst_param *dst, char *write_mask)
740 char *ptr = write_mask;
742 if (dst->write_mask != WINED3DSP_WRITEMASK_ALL)
745 if (dst->write_mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
746 if (dst->write_mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
747 if (dst->write_mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
748 if (dst->write_mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
754 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param *param, BOOL fixup, char *swizzle_str)
756 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
757 * but addressed as "rgba". To fix this we need to swap the register's x
758 * and z components. */
759 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
760 char *ptr = swizzle_str;
762 /* swizzle bits fields: wwzzyyxx */
763 DWORD swizzle = param->swizzle;
764 DWORD swizzle_x = swizzle & 0x03;
765 DWORD swizzle_y = (swizzle >> 2) & 0x03;
766 DWORD swizzle_z = (swizzle >> 4) & 0x03;
767 DWORD swizzle_w = (swizzle >> 6) & 0x03;
769 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
770 * generate a swizzle string. Unless we need to our own swizzling. */
771 if (swizzle != WINED3DSP_NOSWIZZLE || fixup)
774 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
775 *ptr++ = swizzle_chars[swizzle_x];
777 *ptr++ = swizzle_chars[swizzle_x];
778 *ptr++ = swizzle_chars[swizzle_y];
779 *ptr++ = swizzle_chars[swizzle_z];
780 *ptr++ = swizzle_chars[swizzle_w];
787 static void shader_arb_request_a0(const struct wined3d_shader_instruction *ins, const char *src)
789 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
790 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
792 if(strcmp(priv->addr_reg, src) == 0) return;
794 strcpy(priv->addr_reg, src);
795 shader_addline(buffer, "ARL A0.x, %s;\n", src);
798 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
799 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr);
801 static void shader_arb_get_register_name(const struct wined3d_shader_instruction *ins,
802 const struct wined3d_shader_register *reg, char *register_name, BOOL *is_color)
804 /* oPos, oFog and oPts in D3D */
805 static const char * const rastout_reg_names[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
806 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
807 BOOL pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
808 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
814 case WINED3DSPR_TEMP:
815 sprintf(register_name, "R%u", reg->idx);
818 case WINED3DSPR_INPUT:
821 if(This->baseShader.reg_maps.shader_version.major < 3)
823 if (reg->idx == 0) strcpy(register_name, "fragment.color.primary");
824 else strcpy(register_name, "fragment.color.secondary");
831 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
833 if(strcmp(rel_reg, "**aL_emul**") == 0)
835 DWORD idx = ctx->aL + reg->idx;
836 if(idx < MAX_REG_INPUT)
838 strcpy(register_name, ctx->ps_input[idx]);
842 ERR("Pixel shader input register out of bounds: %u\n", idx);
843 sprintf(register_name, "out_of_bounds_%u", idx);
846 else if(This->baseShader.reg_maps.input_registers & 0x0300)
848 /* There are two ways basically:
850 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
851 * That means trouble if the loop also contains a breakc or if the control values
852 * aren't local constants.
853 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
854 * source dynamically. The trouble is that we cannot simply read aL.y because it
855 * is an ADDRESS register. We could however push it, load .zw with a value and use
856 * ADAC to load the condition code register and pop it again afterwards
858 FIXME("Relative input register addressing with more than 8 registers\n");
860 /* This is better than nothing for now */
861 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
863 else if(ctx->cur_ps_args->super.vp_mode != vertexshader)
865 /* This is problematic because we'd have to consult the ctx->ps_input strings
866 * for where to find the varying. Some may be "0.0", others can be texcoords or
867 * colors. This needs either a pipeline replacement to make the vertex shader feed
868 * proper varyings, or loop unrolling
870 * For now use the texcoords and hope for the best
872 FIXME("Non-vertex shader varying input with indirect addressing\n");
873 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
877 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
878 * pulls GL_NV_fragment_program2 in
880 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
885 if(reg->idx < MAX_REG_INPUT)
887 strcpy(register_name, ctx->ps_input[reg->idx]);
891 ERR("Pixel shader input register out of bounds: %u\n", reg->idx);
892 sprintf(register_name, "out_of_bounds_%u", reg->idx);
899 if (ctx->cur_vs_args->super.swizzle_map & (1 << reg->idx)) *is_color = TRUE;
900 sprintf(register_name, "vertex.attrib[%u]", reg->idx);
904 case WINED3DSPR_CONST:
905 if (!pshader && reg->rel_addr)
909 UINT rel_offset = ((IWineD3DVertexShaderImpl *)This)->rel_offset;
910 if(This->baseShader.reg_maps.shader_version.major < 2) {
911 sprintf(rel_reg, "A0.x");
913 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
914 if(ctx->target_version == ARB) {
915 if(strcmp(rel_reg, "**aL_emul**") == 0) {
918 shader_arb_request_a0(ins, rel_reg);
919 sprintf(rel_reg, "A0.x");
924 sprintf(register_name, "C[%u]", ctx->aL + reg->idx);
925 else if (reg->idx >= rel_offset)
926 sprintf(register_name, "C[%s + %u]", rel_reg, reg->idx - rel_offset);
928 sprintf(register_name, "C[%s - %u]", rel_reg, -reg->idx + rel_offset);
932 if (This->baseShader.reg_maps.usesrelconstF)
933 sprintf(register_name, "C[%u]", reg->idx);
935 sprintf(register_name, "C%u", reg->idx);
939 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
941 if(This->baseShader.reg_maps.shader_version.major == 1 &&
942 This->baseShader.reg_maps.shader_version.minor <= 3) {
943 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
944 * and as source to most instructions. For some instructions it is the texcoord
945 * input. Those instructions know about the special use
947 sprintf(register_name, "T%u", reg->idx);
949 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
950 sprintf(register_name, "fragment.texcoord[%u]", reg->idx);
955 if(This->baseShader.reg_maps.shader_version.major == 1 || ctx->target_version >= NV2)
957 sprintf(register_name, "A%u", reg->idx);
961 sprintf(register_name, "A%u_SHADOW", reg->idx);
966 case WINED3DSPR_COLOROUT:
967 if(ctx->cur_ps_args->super.srgb_correction && reg->idx == 0)
969 strcpy(register_name, "TMP_COLOR");
973 if(ctx->cur_ps_args->super.srgb_correction) FIXME("sRGB correction on higher render targets\n");
974 if(This->baseShader.reg_maps.highest_render_target > 0)
976 sprintf(register_name, "result.color[%u]", reg->idx);
980 strcpy(register_name, "result.color");
985 case WINED3DSPR_RASTOUT:
986 if(reg->idx == 1) sprintf(register_name, "%s", ctx->fog_output);
987 else sprintf(register_name, "%s", rastout_reg_names[reg->idx]);
990 case WINED3DSPR_DEPTHOUT:
991 strcpy(register_name, "result.depth");
994 case WINED3DSPR_ATTROUT:
995 /* case WINED3DSPR_OUTPUT: */
996 if (pshader) sprintf(register_name, "oD[%u]", reg->idx);
997 else strcpy(register_name, ctx->color_output[reg->idx]);
1000 case WINED3DSPR_TEXCRDOUT:
1003 sprintf(register_name, "oT[%u]", reg->idx);
1007 if(This->baseShader.reg_maps.shader_version.major < 3)
1009 strcpy(register_name, ctx->texcrd_output[reg->idx]);
1013 strcpy(register_name, ctx->vs_output[reg->idx]);
1018 case WINED3DSPR_LOOP:
1019 if(ctx->target_version >= NV2)
1021 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
1022 if(pshader) sprintf(register_name, "A0.x");
1023 else sprintf(register_name, "aL.y");
1027 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
1028 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
1029 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
1030 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
1033 sprintf(register_name, "**aL_emul**");
1038 case WINED3DSPR_CONSTINT:
1039 sprintf(register_name, "I%u", reg->idx);
1042 case WINED3DSPR_MISCTYPE:
1045 sprintf(register_name, "vpos");
1047 else if(reg->idx == 1)
1049 sprintf(register_name, "fragment.facing.x");
1053 FIXME("Unknown MISCTYPE register index %u\n", reg->idx);
1058 FIXME("Unhandled register type %#x[%u]\n", reg->type, reg->idx);
1059 sprintf(register_name, "unrecognized_register[%u]", reg->idx);
1064 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction *ins,
1065 const struct wined3d_shader_dst_param *wined3d_dst, char *str)
1067 char register_name[255];
1071 shader_arb_get_register_name(ins, &wined3d_dst->reg, register_name, &is_color);
1072 strcpy(str, register_name);
1074 shader_arb_get_write_mask(ins, wined3d_dst, write_mask);
1075 strcat(str, write_mask);
1078 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
1080 switch(channel_source)
1082 case CHANNEL_SOURCE_ZERO: return "0";
1083 case CHANNEL_SOURCE_ONE: return "1";
1084 case CHANNEL_SOURCE_X: return "x";
1085 case CHANNEL_SOURCE_Y: return "y";
1086 case CHANNEL_SOURCE_Z: return "z";
1087 case CHANNEL_SOURCE_W: return "w";
1089 FIXME("Unhandled channel source %#x\n", channel_source);
1094 static void gen_color_correction(struct wined3d_shader_buffer *buffer, const char *reg,
1095 DWORD dst_mask, const char *one, const char *two, struct color_fixup_desc fixup)
1099 if (is_yuv_fixup(fixup))
1101 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1102 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1107 if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1108 if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1109 if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1110 if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1115 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
1116 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
1117 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
1121 if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
1122 if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
1123 if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
1124 if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
1130 char *ptr = reg_mask;
1132 if (mask != WINED3DSP_WRITEMASK_ALL)
1135 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1136 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1137 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1138 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1142 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
1146 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction *ins)
1149 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1150 if (!ins->dst_count) return "";
1152 mod = ins->dst[0].modifiers;
1154 /* Silently ignore PARTIALPRECISION if its not supported */
1155 if(priv->target_version == ARB) mod &= ~WINED3DSPDM_PARTIALPRECISION;
1157 if(mod & WINED3DSPDM_MSAMPCENTROID)
1159 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1160 mod &= ~WINED3DSPDM_MSAMPCENTROID;
1165 case WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION:
1168 case WINED3DSPDM_SATURATE:
1171 case WINED3DSPDM_PARTIALPRECISION:
1178 FIXME("Unknown modifiers 0x%08x\n", mod);
1183 #define TEX_PROJ 0x1
1184 #define TEX_BIAS 0x2
1186 #define TEX_DERIV 0x10
1188 static void shader_hw_sample(const struct wined3d_shader_instruction *ins, DWORD sampler_idx,
1189 const char *dst_str, const char *coord_reg, WORD flags, const char *dsx, const char *dsy)
1191 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1192 DWORD sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
1193 const char *tex_type;
1194 BOOL np2_fixup = FALSE;
1195 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1196 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
1197 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1199 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
1201 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1202 if(!pshader) sampler_idx += MAX_FRAGMENT_SAMPLERS;
1204 switch(sampler_type) {
1210 if(device->stateBlock->textures[sampler_idx] &&
1211 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
1216 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1218 if (priv->cur_np2fixup_info->super.active & (1 << sampler_idx))
1220 if (flags) FIXME("Only ordinary sampling from NP2 textures is supported.\n");
1221 else np2_fixup = TRUE;
1226 case WINED3DSTT_VOLUME:
1230 case WINED3DSTT_CUBE:
1235 ERR("Unexpected texture type %d\n", sampler_type);
1239 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1240 * so don't use shader_arb_get_modifier
1242 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) mod = "_SAT";
1245 /* Fragment samplers always have indentity mapping */
1246 if(sampler_idx >= MAX_FRAGMENT_SAMPLERS)
1248 sampler_idx = priv->cur_vs_args->vertex_samplers[sampler_idx - MAX_FRAGMENT_SAMPLERS];
1251 if (flags & TEX_DERIV)
1253 if(flags & TEX_PROJ) FIXME("Projected texture sampling with custom derivatives\n");
1254 if(flags & TEX_BIAS) FIXME("Biased texture sampling with custom derivatives\n");
1255 shader_addline(buffer, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg,
1256 dsx, dsy,sampler_idx, tex_type);
1258 else if(flags & TEX_LOD)
1260 if(flags & TEX_PROJ) FIXME("Projected texture sampling with explicit lod\n");
1261 if(flags & TEX_BIAS) FIXME("Biased texture sampling with explicit lod\n");
1262 shader_addline(buffer, "TXL%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg,
1263 sampler_idx, tex_type);
1265 else if (flags & TEX_BIAS)
1267 /* Shouldn't be possible, but let's check for it */
1268 if(flags & TEX_PROJ) FIXME("Biased and Projected texture sampling\n");
1269 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1270 shader_addline(buffer, "TXB%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1272 else if (flags & TEX_PROJ)
1274 shader_addline(buffer, "TXP%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1280 const unsigned char idx = priv->cur_np2fixup_info->super.idx[sampler_idx];
1281 shader_addline(buffer, "MUL TA, np2fixup[%u].%s, %s;\n", idx >> 1,
1282 (idx % 2) ? "zwxy" : "xyzw", coord_reg);
1284 shader_addline(buffer, "TEX%s %s, TA, texture[%u], %s;\n", mod, dst_str, sampler_idx, tex_type);
1287 shader_addline(buffer, "TEX%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1292 gen_color_correction(buffer, dst_str, ins->dst[0].write_mask,
1293 "one", "coefmul.x", priv->cur_ps_args->super.color_fixup[sampler_idx]);
1297 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
1298 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr)
1300 /* Generate a line that does the input modifier computation and return the input register to use */
1301 BOOL is_color = FALSE;
1305 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1306 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1308 /* Assume a new line will be added */
1311 /* Get register name */
1312 shader_arb_get_register_name(ins, &src->reg, regstr, &is_color);
1313 shader_arb_get_swizzle(src, is_color, swzstr);
1315 switch (src->modifiers)
1317 case WINED3DSPSM_NONE:
1318 sprintf(outregstr, "%s%s", regstr, swzstr);
1321 case WINED3DSPSM_NEG:
1322 sprintf(outregstr, "-%s%s", regstr, swzstr);
1325 case WINED3DSPSM_BIAS:
1326 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
1328 case WINED3DSPSM_BIASNEG:
1329 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
1331 case WINED3DSPSM_SIGN:
1332 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
1334 case WINED3DSPSM_SIGNNEG:
1335 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
1337 case WINED3DSPSM_COMP:
1338 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
1340 case WINED3DSPSM_X2:
1341 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
1343 case WINED3DSPSM_X2NEG:
1344 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
1346 case WINED3DSPSM_DZ:
1347 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
1348 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1350 case WINED3DSPSM_DW:
1351 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
1352 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1354 case WINED3DSPSM_ABS:
1355 if(ctx->target_version >= NV2) {
1356 sprintf(outregstr, "|%s%s|", regstr, swzstr);
1359 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1362 case WINED3DSPSM_ABSNEG:
1363 if(ctx->target_version >= NV2) {
1364 sprintf(outregstr, "-|%s%s|", regstr, swzstr);
1366 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1367 sprintf(outregstr, "-T%c%s", 'A' + tmpreg, swzstr);
1372 sprintf(outregstr, "%s%s", regstr, swzstr);
1376 /* Return modified or original register, with swizzle */
1378 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
1381 static void pshader_hw_bem(const struct wined3d_shader_instruction *ins)
1383 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1384 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1386 char src_name[2][50];
1387 DWORD sampler_code = dst->reg.idx;
1389 shader_arb_get_dst_param(ins, dst, dst_name);
1391 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1393 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1394 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1397 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1398 shader_addline(buffer, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
1399 shader_addline(buffer, "DP3 TC.r, TA, %s;\n", src_name[1]);
1400 shader_addline(buffer, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
1401 shader_addline(buffer, "DP3 TC.g, TA, %s;\n", src_name[1]);
1403 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1404 shader_addline(buffer, "ADD %s, %s, TC;\n", dst_name, src_name[0]);
1407 static DWORD negate_modifiers(DWORD mod, char *extra_char)
1412 case WINED3DSPSM_NONE: return WINED3DSPSM_NEG;
1413 case WINED3DSPSM_NEG: return WINED3DSPSM_NONE;
1414 case WINED3DSPSM_BIAS: return WINED3DSPSM_BIASNEG;
1415 case WINED3DSPSM_BIASNEG: return WINED3DSPSM_BIAS;
1416 case WINED3DSPSM_SIGN: return WINED3DSPSM_SIGNNEG;
1417 case WINED3DSPSM_SIGNNEG: return WINED3DSPSM_SIGN;
1418 case WINED3DSPSM_COMP: *extra_char = '-'; return WINED3DSPSM_COMP;
1419 case WINED3DSPSM_X2: return WINED3DSPSM_X2NEG;
1420 case WINED3DSPSM_X2NEG: return WINED3DSPSM_X2;
1421 case WINED3DSPSM_DZ: *extra_char = '-'; return WINED3DSPSM_DZ;
1422 case WINED3DSPSM_DW: *extra_char = '-'; return WINED3DSPSM_DW;
1423 case WINED3DSPSM_ABS: return WINED3DSPSM_ABSNEG;
1424 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
1426 FIXME("Unknown modifier %u\n", mod);
1430 static void pshader_hw_cnd(const struct wined3d_shader_instruction *ins)
1432 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1433 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1435 char src_name[3][50];
1436 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1437 ins->ctx->reg_maps->shader_version.minor);
1440 shader_arb_get_dst_param(ins, dst, dst_name);
1441 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1443 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
1444 if (shader_version <= WINED3D_SHADER_VERSION(1, 3) && ins->coissue)
1446 shader_addline(buffer, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[1]);
1448 struct wined3d_shader_src_param src0_copy = ins->src[0];
1451 /* src0 may have a negate srcmod set, so we can't blindly add "-" to the name */
1452 src0_copy.modifiers = negate_modifiers(src0_copy.modifiers, &extra_neg);
1454 shader_arb_get_src_param(ins, &src0_copy, 0, src_name[0]);
1455 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1456 shader_addline(buffer, "ADD TA, %c%s, coefdiv.x;\n", extra_neg, src_name[0]);
1457 /* No modifiers supported on CMP */
1458 shader_addline(buffer, "CMP %s, TA, %s, %s;\n", dst_name, src_name[1], src_name[2]);
1460 /* _SAT on CMP doesn't make much sense, but it is not a pure NOP */
1461 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
1463 shader_arb_get_register_name(ins, &dst->reg, src_name[0], &is_color);
1464 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, dst_name);
1469 static void pshader_hw_cmp(const struct wined3d_shader_instruction *ins)
1471 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1472 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1474 char src_name[3][50];
1477 shader_arb_get_dst_param(ins, dst, dst_name);
1479 /* Generate input register names (with modifiers) */
1480 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1481 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1482 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1484 /* No modifiers are supported on CMP */
1485 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
1486 src_name[0], src_name[2], src_name[1]);
1488 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
1490 shader_arb_get_register_name(ins, &dst->reg, src_name[0], &is_color);
1491 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name[0]);
1495 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1496 * dst = dot2(src0, src1) + src2 */
1497 static void pshader_hw_dp2add(const struct wined3d_shader_instruction *ins)
1499 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1500 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1502 char src_name[3][50];
1503 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1505 shader_arb_get_dst_param(ins, dst, dst_name);
1506 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1507 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1509 if(ctx->target_version >= NV3)
1511 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1512 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1513 shader_addline(buffer, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1514 dst_name, src_name[0], src_name[1], src_name[2]);
1516 else if(ctx->target_version >= NV2)
1518 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1519 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1520 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1521 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1523 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1525 * .xyxy and other swizzles that we could get with this are not valid in
1526 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1528 struct wined3d_shader_src_param tmp_param = ins->src[1];
1529 DWORD swizzle = tmp_param.swizzle & 0xf; /* Selects .xy */
1530 tmp_param.swizzle = swizzle | (swizzle << 4); /* Creates .xyxy */
1532 shader_arb_get_src_param(ins, &tmp_param, 1, src_name[1]);
1534 shader_addline(buffer, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1535 dst_name, src_name[2], src_name[0], src_name[1]);
1539 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1540 /* 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]
1541 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1543 shader_addline(buffer, "MOV TA, %s;\n", src_name[0]);
1544 shader_addline(buffer, "MOV TA.z, 0.0;\n");
1545 shader_addline(buffer, "DP3 TA, TA, %s;\n", src_name[1]);
1546 shader_addline(buffer, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[2]);
1550 /* Map the opcode 1-to-1 to the GL code */
1551 static void shader_hw_map2gl(const struct wined3d_shader_instruction *ins)
1553 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1554 const char *instruction;
1555 char arguments[256], dst_str[50];
1557 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1559 switch (ins->handler_idx)
1561 case WINED3DSIH_ABS: instruction = "ABS"; break;
1562 case WINED3DSIH_ADD: instruction = "ADD"; break;
1563 case WINED3DSIH_CRS: instruction = "XPD"; break;
1564 case WINED3DSIH_DP3: instruction = "DP3"; break;
1565 case WINED3DSIH_DP4: instruction = "DP4"; break;
1566 case WINED3DSIH_DST: instruction = "DST"; break;
1567 case WINED3DSIH_FRC: instruction = "FRC"; break;
1568 case WINED3DSIH_LIT: instruction = "LIT"; break;
1569 case WINED3DSIH_LRP: instruction = "LRP"; break;
1570 case WINED3DSIH_MAD: instruction = "MAD"; break;
1571 case WINED3DSIH_MAX: instruction = "MAX"; break;
1572 case WINED3DSIH_MIN: instruction = "MIN"; break;
1573 case WINED3DSIH_MOV: instruction = "MOV"; break;
1574 case WINED3DSIH_MUL: instruction = "MUL"; break;
1575 case WINED3DSIH_SGE: instruction = "SGE"; break;
1576 case WINED3DSIH_SLT: instruction = "SLT"; break;
1577 case WINED3DSIH_SUB: instruction = "SUB"; break;
1578 case WINED3DSIH_MOVA:instruction = "ARR"; break;
1579 case WINED3DSIH_SGN: instruction = "SSG"; break;
1580 case WINED3DSIH_DSX: instruction = "DDX"; break;
1581 default: instruction = "";
1582 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1586 /* Note that shader_arb_add_dst_param() adds spaces. */
1587 arguments[0] = '\0';
1588 shader_arb_get_dst_param(ins, dst, dst_str);
1589 for (i = 0; i < ins->src_count; ++i)
1592 strcat(arguments, ", ");
1593 shader_arb_get_src_param(ins, &ins->src[i], i, operand);
1594 strcat(arguments, operand);
1596 shader_addline(buffer, "%s%s %s%s;\n", instruction, shader_arb_get_modifier(ins), dst_str, arguments);
1599 static void shader_hw_nop(const struct wined3d_shader_instruction *ins)
1601 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1602 shader_addline(buffer, "NOP;\n");
1605 static void shader_hw_mov(const struct wined3d_shader_instruction *ins)
1607 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1608 BOOL pshader = shader_is_pshader_version(shader->baseShader.reg_maps.shader_version.type);
1609 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1611 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1612 char src0_param[256];
1614 if(ins->handler_idx == WINED3DSIH_MOVA) {
1617 if(ctx->target_version >= NV2) {
1618 shader_hw_map2gl(ins);
1621 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1622 shader_arb_get_write_mask(ins, &ins->dst[0], write_mask);
1624 /* This implements the mova formula used in GLSL. The first two instructions
1625 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1629 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1631 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1632 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1634 shader_addline(buffer, "SGE A0_SHADOW%s, %s, mova_const.y;\n", write_mask, src0_param);
1635 shader_addline(buffer, "MAD A0_SHADOW%s, A0_SHADOW, mova_const.z, -mova_const.w;\n", write_mask);
1637 shader_addline(buffer, "ABS TA%s, %s;\n", write_mask, src0_param);
1638 shader_addline(buffer, "ADD TA%s, TA, mova_const.x;\n", write_mask);
1639 shader_addline(buffer, "FLR TA%s, TA;\n", write_mask);
1640 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1642 shader_addline(buffer, "ADD TA%s, TA, helper_const.z;\n", write_mask);
1644 shader_addline(buffer, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask);
1646 ((struct shader_arb_ctx_priv *)ins->ctx->backend_data)->addr_reg[0] = '\0';
1647 } else if (ins->ctx->reg_maps->shader_version.major == 1
1648 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
1649 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1651 src0_param[0] = '\0';
1652 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1654 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1655 shader_addline(buffer, "ADD TA.x, %s, helper_const.z;\n", src0_param);
1656 shader_addline(buffer, "ARL A0.x, TA.x;\n");
1660 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1661 * with more than one component. Thus replicate the first source argument over all
1662 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1663 struct wined3d_shader_src_param tmp_src = ins->src[0];
1664 tmp_src.swizzle = (tmp_src.swizzle & 0x3) * 0x55;
1665 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1666 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1669 else if(ins->dst[0].reg.type == WINED3DSPR_COLOROUT && ins->dst[0].reg.idx == 0 && pshader)
1671 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) shader;
1672 if(ctx->cur_ps_args->super.srgb_correction && ps->color0_mov)
1674 shader_addline(buffer, "#mov handled in srgb write code\n");
1677 shader_hw_map2gl(ins);
1681 shader_hw_map2gl(ins);
1685 static void pshader_hw_texkill(const struct wined3d_shader_instruction *ins)
1687 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1688 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1691 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1692 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1694 shader_arb_get_dst_param(ins, dst, reg_dest);
1696 if (ins->ctx->reg_maps->shader_version.major >= 2)
1698 const char *kilsrc = "TA";
1701 shader_arb_get_register_name(ins, &dst->reg, reg_dest, &is_color);
1702 if(dst->write_mask == WINED3DSP_WRITEMASK_ALL)
1708 /* Sigh. KIL doesn't support swizzles/writemasks. KIL passes a writemask, but ".xy" for example
1709 * is not valid as a swizzle in ARB (needs ".xyyy"). Use SWZ to load the register properly, and set
1710 * masked out components to 0(won't kill)
1712 char x = '0', y = '0', z = '0', w = '0';
1713 if(dst->write_mask & WINED3DSP_WRITEMASK_0) x = 'x';
1714 if(dst->write_mask & WINED3DSP_WRITEMASK_1) y = 'y';
1715 if(dst->write_mask & WINED3DSP_WRITEMASK_2) z = 'z';
1716 if(dst->write_mask & WINED3DSP_WRITEMASK_3) w = 'w';
1717 shader_addline(buffer, "SWZ TA, %s, %c, %c, %c, %c;\n", reg_dest, x, y, z, w);
1719 shader_addline(buffer, "KIL %s;\n", kilsrc);
1721 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1722 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1724 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1725 * or pass in any temporary register(in shader phase 2)
1727 if(ins->ctx->reg_maps->shader_version.minor <= 3) {
1728 sprintf(reg_dest, "fragment.texcoord[%u]", dst->reg.idx);
1730 shader_arb_get_dst_param(ins, dst, reg_dest);
1732 shader_addline(buffer, "SWZ TA, %s, x, y, z, 1;\n", reg_dest);
1733 shader_addline(buffer, "KIL TA;\n");
1737 static void pshader_hw_tex(const struct wined3d_shader_instruction *ins)
1739 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1740 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1741 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1742 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1743 ins->ctx->reg_maps->shader_version.minor);
1744 struct wined3d_shader_src_param src;
1748 DWORD reg_sampler_code;
1751 /* All versions have a destination register */
1752 shader_arb_get_dst_param(ins, dst, reg_dest);
1754 /* 1.0-1.4: Use destination register number as texture code.
1755 2.0+: Use provided sampler number as texure code. */
1756 if (shader_version < WINED3D_SHADER_VERSION(2,0))
1757 reg_sampler_code = dst->reg.idx;
1759 reg_sampler_code = ins->src[1].reg.idx;
1761 /* 1.0-1.3: Use the texcoord varying.
1762 1.4+: Use provided coordinate source register. */
1763 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1764 sprintf(reg_coord, "fragment.texcoord[%u]", reg_sampler_code);
1766 /* TEX is the only instruction that can handle DW and DZ natively */
1768 if(src.modifiers == WINED3DSPSM_DW) src.modifiers = WINED3DSPSM_NONE;
1769 if(src.modifiers == WINED3DSPSM_DZ) src.modifiers = WINED3DSPSM_NONE;
1770 shader_arb_get_src_param(ins, &src, 0, reg_coord);
1774 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1775 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1776 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1778 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1781 if(reg_sampler_code < MAX_TEXTURES) {
1782 flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1784 if (flags & WINED3DTTFF_PROJECTED) {
1785 myflags |= TEX_PROJ;
1788 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
1790 DWORD src_mod = ins->src[0].modifiers;
1791 if (src_mod == WINED3DSPSM_DZ) {
1792 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1793 * varying register, so we need a temp reg
1795 shader_addline(ins->ctx->buffer, "SWZ TA, %s, x, y, z, z;\n", reg_coord);
1796 strcpy(reg_coord, "TA");
1797 myflags |= TEX_PROJ;
1798 } else if(src_mod == WINED3DSPSM_DW) {
1799 myflags |= TEX_PROJ;
1802 if (ins->flags & WINED3DSI_TEXLD_PROJECT) myflags |= TEX_PROJ;
1803 if (ins->flags & WINED3DSI_TEXLD_BIAS) myflags |= TEX_BIAS;
1805 shader_hw_sample(ins, reg_sampler_code, reg_dest, reg_coord, myflags, NULL, NULL);
1808 static void pshader_hw_texcoord(const struct wined3d_shader_instruction *ins)
1810 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1811 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1812 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1813 ins->ctx->reg_maps->shader_version.minor);
1816 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1818 DWORD reg = dst->reg.idx;
1820 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1821 shader_addline(buffer, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str, reg);
1825 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src);
1826 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1827 shader_addline(buffer, "MOV %s, %s;\n", dst_str, reg_src);
1831 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction *ins)
1833 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1834 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1835 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1838 DWORD reg1 = ins->dst[0].reg.idx;
1842 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
1843 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1844 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1845 /* Move .x first in case src_str is "TA" */
1846 shader_addline(buffer, "MOV TA.y, %s.x;\n", src_str);
1847 shader_addline(buffer, "MOV TA.x, %s.w;\n", src_str);
1848 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1849 shader_hw_sample(ins, reg1, dst_str, "TA", flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
1852 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction *ins)
1854 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1856 DWORD reg1 = ins->dst[0].reg.idx;
1860 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
1861 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1862 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1863 shader_addline(buffer, "MOV TA.x, %s.y;\n", src_str);
1864 shader_addline(buffer, "MOV TA.y, %s.z;\n", src_str);
1865 shader_hw_sample(ins, reg1, dst_str, "TA", 0, NULL, NULL);
1868 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction *ins)
1870 DWORD reg1 = ins->dst[0].reg.idx;
1874 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
1875 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1876 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1877 shader_hw_sample(ins, reg1, dst_str, src_str, 0, NULL, NULL);
1880 static void pshader_hw_texbem(const struct wined3d_shader_instruction *ins)
1882 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1883 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1884 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1885 char reg_coord[40], dst_reg[50], src_reg[50];
1886 DWORD reg_dest_code;
1888 /* All versions have a destination register. The Tx where the texture coordinates come
1889 * from is the varying incarnation of the texture register
1891 reg_dest_code = dst->reg.idx;
1892 shader_arb_get_dst_param(ins, &ins->dst[0], dst_reg);
1893 shader_arb_get_src_param(ins, &ins->src[0], 0, src_reg);
1894 sprintf(reg_coord, "fragment.texcoord[%u]", reg_dest_code);
1896 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1897 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
1899 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
1900 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
1902 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
1903 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
1906 shader_addline(buffer, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1907 shader_addline(buffer, "DP3 TA.x, TB, %s;\n", src_reg);
1908 shader_addline(buffer, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1909 shader_addline(buffer, "DP3 TA.y, TB, %s;\n", src_reg);
1911 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1912 * so we can't let the GL handle this.
1914 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1915 & WINED3DTTFF_PROJECTED) {
1916 shader_addline(buffer, "RCP TB.w, %s.w;\n", reg_coord);
1917 shader_addline(buffer, "MUL TB.xy, %s, TB.w;\n", reg_coord);
1918 shader_addline(buffer, "ADD TA.xy, TA, TB;\n");
1920 shader_addline(buffer, "ADD TA.xy, TA, %s;\n", reg_coord);
1923 shader_hw_sample(ins, reg_dest_code, dst_reg, "TA", 0, NULL, NULL);
1925 if (ins->handler_idx == WINED3DSIH_TEXBEML)
1927 /* No src swizzles are allowed, so this is ok */
1928 shader_addline(buffer, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
1929 src_reg, reg_dest_code, reg_dest_code);
1930 shader_addline(buffer, "MUL %s, %s, TA;\n", dst_reg, dst_reg);
1934 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction *ins)
1936 DWORD reg = ins->dst[0].reg.idx;
1937 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1938 char src0_name[50], dst_name[50];
1940 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
1942 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1943 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
1944 * T<reg+1> register. Use this register to store the calculated vector
1946 tmp_reg.idx = reg + 1;
1947 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
1948 shader_addline(buffer, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
1951 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction *ins)
1953 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1954 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1956 DWORD reg = ins->dst[0].reg.idx;
1957 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1963 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
1964 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
1966 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1967 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1968 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
1969 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1970 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
1973 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction *ins)
1975 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1976 DWORD reg = ins->dst[0].reg.idx;
1977 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1978 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1979 char src0_name[50], dst_name[50];
1980 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
1983 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
1984 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
1985 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
1987 tmp_reg.idx = reg + 2 - current_state->current_row;
1988 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
1990 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1991 shader_addline(buffer, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
1992 dst_name, 'x' + current_state->current_row, reg, src0_name);
1993 current_state->texcoord_w[current_state->current_row++] = reg;
1996 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction *ins)
1998 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1999 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2001 DWORD reg = ins->dst[0].reg.idx;
2002 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2003 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2005 char src0_name[50], dst_name[50];
2008 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2009 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2010 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
2012 /* Sample the texture using the calculated coordinates */
2013 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2014 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
2015 shader_hw_sample(ins, reg, dst_str, dst_name, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2016 current_state->current_row = 0;
2019 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2021 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2022 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2024 DWORD reg = ins->dst[0].reg.idx;
2025 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2026 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2032 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
2033 * components for temporary data storage
2035 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2036 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2037 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2039 /* Construct the eye-ray vector from w coordinates */
2040 shader_addline(buffer, "MOV TB.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
2041 shader_addline(buffer, "MOV TB.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
2042 shader_addline(buffer, "MOV TB.z, fragment.texcoord[%u].w;\n", reg);
2044 /* Calculate reflection vector
2046 shader_addline(buffer, "DP3 %s.w, %s, TB;\n", dst_reg, dst_reg);
2047 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
2048 shader_addline(buffer, "DP3 TB.w, %s, %s;\n", dst_reg, dst_reg);
2049 shader_addline(buffer, "RCP TB.w, TB.w;\n");
2050 shader_addline(buffer, "MUL %s.w, %s.w, TB.w;\n", dst_reg, dst_reg);
2051 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2052 shader_addline(buffer, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg, dst_reg);
2054 /* Sample the texture using the calculated coordinates */
2055 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2056 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
2057 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2058 current_state->current_row = 0;
2061 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction *ins)
2063 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2064 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2066 DWORD reg = ins->dst[0].reg.idx;
2067 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2068 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2075 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2076 shader_arb_get_src_param(ins, &ins->src[0], 1, src1_name);
2077 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2078 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
2079 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2081 /* Calculate reflection vector.
2084 * dst_reg.xyz = 2 * --------- * N - E
2087 * Which normalizes the normal vector
2089 shader_addline(buffer, "DP3 %s.w, %s, %s;\n", dst_reg, dst_reg, src1_name);
2090 shader_addline(buffer, "DP3 TC.w, %s, %s;\n", dst_reg, dst_reg);
2091 shader_addline(buffer, "RCP TC.w, TC.w;\n");
2092 shader_addline(buffer, "MUL %s.w, %s.w, TC.w;\n", dst_reg, dst_reg);
2093 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2094 shader_addline(buffer, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg, dst_reg, src1_name);
2096 /* Sample the texture using the calculated coordinates */
2097 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2098 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
2099 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2100 current_state->current_row = 0;
2103 static void pshader_hw_texdepth(const struct wined3d_shader_instruction *ins)
2105 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2106 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2109 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
2110 * which is essentially an input, is the destination register because it is the first
2111 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
2112 * here(writemasks/swizzles are not valid on texdepth)
2114 shader_arb_get_dst_param(ins, dst, dst_name);
2116 /* According to the msdn, the source register(must be r5) is unusable after
2117 * the texdepth instruction, so we're free to modify it
2119 shader_addline(buffer, "MIN %s.y, %s.y, one.y;\n", dst_name, dst_name);
2121 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2122 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2123 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2125 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2126 shader_addline(buffer, "MUL TA.x, %s.x, %s.y;\n", dst_name, dst_name);
2127 shader_addline(buffer, "MIN TA.x, TA.x, one.x;\n");
2128 shader_addline(buffer, "MAX result.depth, TA.x, 0.0;\n");
2131 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
2132 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2133 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2134 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction *ins)
2136 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2137 DWORD sampler_idx = ins->dst[0].reg.idx;
2141 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2142 shader_addline(buffer, "MOV TB, 0.0;\n");
2143 shader_addline(buffer, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx, src0);
2145 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2146 shader_hw_sample(ins, sampler_idx, dst_str, "TB", 0 /* Only one coord, can't be projected */, NULL, NULL);
2149 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2150 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2151 static void pshader_hw_texdp3(const struct wined3d_shader_instruction *ins)
2153 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2156 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2158 /* Handle output register */
2159 shader_arb_get_dst_param(ins, dst, dst_str);
2160 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2161 shader_addline(buffer, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str, dst->reg.idx, src0);
2164 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2165 * Perform the 3rd row of a 3x3 matrix multiply */
2166 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction *ins)
2168 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2169 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2170 char dst_str[50], dst_name[50];
2174 shader_arb_get_dst_param(ins, dst, dst_str);
2175 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2176 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2177 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx, src0);
2178 shader_addline(buffer, "MOV %s, %s;\n", dst_str, dst_name);
2181 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2182 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2183 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2184 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2186 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction *ins)
2188 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2189 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2190 char src0[50], dst_name[50];
2193 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2194 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2195 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx, src0);
2197 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2198 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2199 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2201 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2202 shader_addline(buffer, "MUL %s.x, %s.x, %s.y;\n", dst_name, dst_name, dst_name);
2203 shader_addline(buffer, "MIN %s.x, %s.x, one.x;\n", dst_name, dst_name);
2204 shader_addline(buffer, "MAX result.depth, %s.x, 0.0;\n", dst_name);
2207 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2208 Vertex/Pixel shaders to ARB_vertex_program codes */
2209 static void shader_hw_mnxn(const struct wined3d_shader_instruction *ins)
2212 int nComponents = 0;
2213 struct wined3d_shader_dst_param tmp_dst = {{0}};
2214 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2215 struct wined3d_shader_instruction tmp_ins;
2217 memset(&tmp_ins, 0, sizeof(tmp_ins));
2219 /* Set constants for the temporary argument */
2220 tmp_ins.ctx = ins->ctx;
2221 tmp_ins.dst_count = 1;
2222 tmp_ins.dst = &tmp_dst;
2223 tmp_ins.src_count = 2;
2224 tmp_ins.src = tmp_src;
2226 switch(ins->handler_idx)
2228 case WINED3DSIH_M4x4:
2230 tmp_ins.handler_idx = WINED3DSIH_DP4;
2232 case WINED3DSIH_M4x3:
2234 tmp_ins.handler_idx = WINED3DSIH_DP4;
2236 case WINED3DSIH_M3x4:
2238 tmp_ins.handler_idx = WINED3DSIH_DP3;
2240 case WINED3DSIH_M3x3:
2242 tmp_ins.handler_idx = WINED3DSIH_DP3;
2244 case WINED3DSIH_M3x2:
2246 tmp_ins.handler_idx = WINED3DSIH_DP3;
2249 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
2253 tmp_dst = ins->dst[0];
2254 tmp_src[0] = ins->src[0];
2255 tmp_src[1] = ins->src[1];
2256 for (i = 0; i < nComponents; i++) {
2257 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2258 shader_hw_map2gl(&tmp_ins);
2259 ++tmp_src[1].reg.idx;
2263 static void shader_hw_scalar_op(const struct wined3d_shader_instruction *ins)
2265 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2266 const char *instruction;
2271 switch(ins->handler_idx)
2273 case WINED3DSIH_RSQ: instruction = "RSQ"; break;
2274 case WINED3DSIH_RCP: instruction = "RCP"; break;
2275 case WINED3DSIH_EXP: instruction = "EX2"; break;
2276 case WINED3DSIH_EXPP: instruction = "EXP"; break;
2277 default: instruction = "";
2278 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
2282 shader_arb_get_dst_param(ins, &ins->dst[0], dst); /* Destination */
2283 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2284 if (ins->src[0].swizzle == WINED3DSP_NOSWIZZLE)
2286 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2292 shader_addline(buffer, "%s%s %s, %s;\n", instruction, shader_arb_get_modifier(ins), dst, src);
2295 static void shader_hw_nrm(const struct wined3d_shader_instruction *ins)
2297 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2300 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2301 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
2303 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2304 shader_arb_get_src_param(ins, &ins->src[0], 1 /* Use TB */, src_name);
2306 if(pshader && priv->target_version >= NV3)
2308 shader_addline(buffer, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name);
2312 shader_addline(buffer, "DP3 TA, %s, %s;\n", src_name, src_name);
2313 shader_addline(buffer, "RSQ TA, TA.x;\n");
2314 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2315 shader_addline(buffer, "MUL%s %s, %s, TA;\n", shader_arb_get_modifier(ins), dst_name,
2320 static void shader_hw_lrp(const struct wined3d_shader_instruction *ins)
2322 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2324 char src_name[3][50];
2326 /* ARB_fragment_program has a convenient LRP instruction */
2327 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2328 shader_hw_map2gl(ins);
2332 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2333 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
2334 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
2335 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
2337 shader_addline(buffer, "SUB TA, %s, %s;\n", src_name[1], src_name[2]);
2338 shader_addline(buffer, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins),
2339 dst_name, src_name[0], src_name[2]);
2342 static void shader_hw_sincos(const struct wined3d_shader_instruction *ins)
2344 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2345 * must contain fixed constants. So we need a separate function to filter those constants and
2348 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2349 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2350 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2352 char src_name0[50], src_name1[50], src_name2[50];
2355 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2356 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2357 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2358 /* No modifiers are supported on SCS */
2359 shader_addline(buffer, "SCS %s, %s;\n", dst_name, src_name0);
2361 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
2363 shader_arb_get_register_name(ins, &dst->reg, src_name0, &is_color);
2364 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name0);
2366 } else if(priv->target_version >= NV2) {
2367 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2369 /* Sincos writemask must be .x, .y or .xy */
2370 if(dst->write_mask & WINED3DSP_WRITEMASK_0)
2371 shader_addline(buffer, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2372 if(dst->write_mask & WINED3DSP_WRITEMASK_1)
2373 shader_addline(buffer, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2375 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2376 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2378 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2379 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2381 * The constants we get are:
2383 * +1 +1, -1 -1 +1 +1 -1 -1
2384 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2385 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2387 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2391 * (x/2)^4 = x^4 / 16
2392 * (x/2)^5 = x^5 / 32
2395 * To get the final result:
2396 * sin(x) = 2 * sin(x/2) * cos(x/2)
2397 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2398 * (from sin(x+y) and cos(x+y) rules)
2400 * As per MSDN, dst.z is undefined after the operation, and so is
2401 * dst.x and dst.y if they're masked out by the writemask. Ie
2402 * sincos dst.y, src1, c0, c1
2403 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2404 * vsa.exe also stops with an error if the dest register is the same register as the source
2405 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2406 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2408 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2409 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name2);
2410 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2412 shader_addline(buffer, "MUL %s.x, %s, %s;\n", dst_name, src_name0, src_name0); /* x ^ 2 */
2413 shader_addline(buffer, "MUL TA.y, %s.x, %s;\n", dst_name, src_name0); /* x ^ 3 */
2414 shader_addline(buffer, "MUL %s.y, TA.y, %s;\n", dst_name, src_name0); /* x ^ 4 */
2415 shader_addline(buffer, "MUL TA.z, %s.y, %s;\n", dst_name, src_name0); /* x ^ 5 */
2416 shader_addline(buffer, "MUL %s.z, TA.z, %s;\n", dst_name, src_name0); /* x ^ 6 */
2417 shader_addline(buffer, "MUL TA.w, %s.z, %s;\n", dst_name, src_name0); /* x ^ 7 */
2421 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2422 * properly merge that with MULs in the code above?
2423 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2424 * we can merge the sine and cosine MAD rows to calculate them together.
2426 shader_addline(buffer, "MUL TA.x, %s, %s.w;\n", src_name0, src_name2); /* x^1, +1/(1!*2) */
2427 shader_addline(buffer, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2); /* -1/(3!*8) */
2428 shader_addline(buffer, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1); /* +1/(5!*32) */
2429 shader_addline(buffer, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1); /* -1/(7!*128) */
2432 shader_addline(buffer, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name, src_name2, src_name2); /* -1/(2!*4), +1.0 */
2433 shader_addline(buffer, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name, src_name1); /* +1/(4!*16) */
2434 shader_addline(buffer, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name, src_name1); /* -1/(6!*64) */
2436 if(dst->write_mask & WINED3DSP_WRITEMASK_0) {
2438 shader_addline(buffer, "MUL TA.z, TA.y, TA.y;\n");
2439 shader_addline(buffer, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name);
2441 if(dst->write_mask & WINED3DSP_WRITEMASK_1) {
2443 shader_addline(buffer, "MUL %s.y, TA.x, TA.y;\n", dst_name);
2444 shader_addline(buffer, "ADD %s.y, %s.y, %s.y;\n", dst_name, dst_name, dst_name);
2449 static void shader_hw_sgn(const struct wined3d_shader_instruction *ins)
2451 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2454 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
2456 /* SGN is only valid in vertex shaders */
2457 if(ctx->target_version == NV2) {
2458 shader_hw_map2gl(ins);
2461 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2462 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2464 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2465 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2467 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) {
2468 shader_addline(buffer, "SLT %s, -%s, %s;\n", dst_name, src_name, src_name);
2470 /* src contains TA? Write to the dest first. This won't overwrite our destination.
2471 * Then use TA, and calculate the final result
2473 * Not reading from TA? Store the first result in TA to avoid overwriting the
2474 * destination if src reg = dst reg
2476 if(strstr(src_name, "TA"))
2478 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2479 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2480 shader_addline(buffer, "ADD %s, %s, -TA;\n", dst_name, dst_name);
2484 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2485 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2486 shader_addline(buffer, "ADD %s, TA, -%s;\n", dst_name, dst_name);
2491 static void shader_hw_dsy(const struct wined3d_shader_instruction *ins)
2493 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2499 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2500 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2501 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2503 shader_addline(buffer, "DDY %s, %s;\n", dst, src);
2504 shader_addline(buffer, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins), dst, dst_name);
2507 static DWORD abs_modifier(DWORD mod, BOOL *need_abs)
2513 case WINED3DSPSM_NONE: return WINED3DSPSM_ABS;
2514 case WINED3DSPSM_NEG: return WINED3DSPSM_ABS;
2515 case WINED3DSPSM_BIAS: *need_abs = TRUE; return WINED3DSPSM_BIAS;
2516 case WINED3DSPSM_BIASNEG: *need_abs = TRUE; return WINED3DSPSM_BIASNEG;
2517 case WINED3DSPSM_SIGN: *need_abs = TRUE; return WINED3DSPSM_SIGN;
2518 case WINED3DSPSM_SIGNNEG: *need_abs = TRUE; return WINED3DSPSM_SIGNNEG;
2519 case WINED3DSPSM_COMP: *need_abs = TRUE; return WINED3DSPSM_COMP;
2520 case WINED3DSPSM_X2: *need_abs = TRUE; return WINED3DSPSM_X2;
2521 case WINED3DSPSM_X2NEG: *need_abs = TRUE; return WINED3DSPSM_X2NEG;
2522 case WINED3DSPSM_DZ: *need_abs = TRUE; return WINED3DSPSM_DZ;
2523 case WINED3DSPSM_DW: *need_abs = TRUE; return WINED3DSPSM_DW;
2524 case WINED3DSPSM_ABS: return WINED3DSPSM_ABS;
2525 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
2527 FIXME("Unknown modifier %u\n", mod);
2531 static void shader_hw_log_pow(const struct wined3d_shader_instruction *ins)
2533 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2534 char src0[50], src1[50], dst[50];
2535 struct wined3d_shader_src_param src0_copy = ins->src[0];
2536 BOOL need_abs = FALSE;
2540 switch(ins->handler_idx)
2542 case WINED3DSIH_LOG: instr = "LG2"; break;
2543 case WINED3DSIH_LOGP: instr = "LOG"; break;
2544 case WINED3DSIH_POW: instr = "POW"; arg2 = TRUE; break;
2546 ERR("Unexpected instruction %d\n", ins->handler_idx);
2550 /* LOG, LOGP and POW operate on the absolute value of the input */
2551 src0_copy.modifiers = abs_modifier(src0_copy.modifiers, &need_abs);
2553 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2554 shader_arb_get_src_param(ins, &src0_copy, 0, src0);
2555 if(arg2) shader_arb_get_src_param(ins, &ins->src[1], 1, src1);
2559 shader_addline(buffer, "ABS TA, %s;\n", src0);
2562 shader_addline(buffer, "%s%s %s, TA, %s;\n", instr, shader_arb_get_modifier(ins), dst, src1);
2566 shader_addline(buffer, "%s%s %s, TA;\n", instr, shader_arb_get_modifier(ins), dst);
2571 shader_addline(buffer, "%s%s %s, %s, %s;\n", instr, shader_arb_get_modifier(ins), dst, src0, src1);
2575 shader_addline(buffer, "%s%s %s, %s;\n", instr, shader_arb_get_modifier(ins), dst, src0);
2579 static void shader_hw_loop(const struct wined3d_shader_instruction *ins)
2581 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2583 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2586 shader_arb_get_src_param(ins, &ins->src[1], 0, src_name);
2590 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2591 struct list *e = list_head(&priv->control_frames);
2592 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2594 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2595 /* The constant loader makes sure to load -1 into iX.w */
2596 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2597 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->loop_no);
2598 shader_addline(buffer, "loop_%u_start:\n", control_frame->loop_no);
2602 shader_addline(buffer, "LOOP %s;\n", src_name);
2606 static void shader_hw_rep(const struct wined3d_shader_instruction *ins)
2608 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2610 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2612 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2614 /* The constant loader makes sure to load -1 into iX.w */
2617 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2618 struct list *e = list_head(&priv->control_frames);
2619 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2621 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2623 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2624 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->loop_no);
2625 shader_addline(buffer, "loop_%u_start:\n", control_frame->loop_no);
2629 shader_addline(buffer, "REP %s;\n", src_name);
2633 static void shader_hw_endloop(const struct wined3d_shader_instruction *ins)
2635 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2636 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2640 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2641 struct list *e = list_head(&priv->control_frames);
2642 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2644 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2645 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->loop_no);
2646 shader_addline(buffer, "loop_%u_end:\n", control_frame->loop_no);
2648 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2652 shader_addline(buffer, "ENDLOOP;\n");
2656 static void shader_hw_endrep(const struct wined3d_shader_instruction *ins)
2658 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2659 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2663 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2664 struct list *e = list_head(&priv->control_frames);
2665 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2667 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2668 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->loop_no);
2669 shader_addline(buffer, "loop_%u_end:\n", control_frame->loop_no);
2671 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2675 shader_addline(buffer, "ENDREP;\n");
2679 static const struct control_frame *find_last_loop(const struct shader_arb_ctx_priv *priv)
2681 struct control_frame *control_frame;
2683 LIST_FOR_EACH_ENTRY(control_frame, &priv->control_frames, struct control_frame, entry)
2685 if(control_frame->type == LOOP || control_frame->type == REP) return control_frame;
2687 ERR("Could not find loop for break\n");
2691 static void shader_hw_break(const struct wined3d_shader_instruction *ins)
2693 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2694 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2695 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2699 shader_addline(buffer, "BRA loop_%u_end;\n", control_frame->loop_no);
2703 shader_addline(buffer, "BRK;\n");
2707 static const char *get_compare(COMPARISON_TYPE flags)
2711 case COMPARISON_GT: return "GT";
2712 case COMPARISON_EQ: return "EQ";
2713 case COMPARISON_GE: return "GE";
2714 case COMPARISON_LT: return "LT";
2715 case COMPARISON_NE: return "NE";
2716 case COMPARISON_LE: return "LE";
2718 FIXME("Unrecognized comparison value: %u\n", flags);
2723 static COMPARISON_TYPE invert_compare(COMPARISON_TYPE flags)
2727 case COMPARISON_GT: return COMPARISON_LE;
2728 case COMPARISON_EQ: return COMPARISON_NE;
2729 case COMPARISON_GE: return COMPARISON_LT;
2730 case COMPARISON_LT: return COMPARISON_GE;
2731 case COMPARISON_NE: return COMPARISON_EQ;
2732 case COMPARISON_LE: return COMPARISON_GT;
2734 FIXME("Unrecognized comparison value: %u\n", flags);
2739 static void shader_hw_breakc(const struct wined3d_shader_instruction *ins)
2741 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2742 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2743 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2746 const char *comp = get_compare(ins->flags);
2748 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2749 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2753 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
2754 * away the subtraction result
2756 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2757 shader_addline(buffer, "BRA loop_%u_end (%s.x);\n", control_frame->loop_no, comp);
2761 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2762 shader_addline(buffer, "BRK (%s.x);\n", comp);
2766 static void shader_hw_ifc(const struct wined3d_shader_instruction *ins)
2768 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2769 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2770 struct list *e = list_head(&priv->control_frames);
2771 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2775 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2777 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2778 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2782 /* Invert the flag. We jump to the else label if the condition is NOT true */
2783 comp = get_compare(invert_compare(ins->flags));
2784 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2785 shader_addline(buffer, "BRA ifc_%u_else (%s.x);\n", control_frame->ifc_no, comp);
2789 comp = get_compare(ins->flags);
2790 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2791 shader_addline(buffer, "IF %s.x;\n", comp);
2795 static void shader_hw_else(const struct wined3d_shader_instruction *ins)
2797 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2798 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2799 struct list *e = list_head(&priv->control_frames);
2800 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2801 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2805 shader_addline(buffer, "BRA ifc_%u_endif;\n", control_frame->ifc_no);
2806 shader_addline(buffer, "ifc_%u_else:\n", control_frame->ifc_no);
2807 control_frame->had_else = TRUE;
2811 shader_addline(buffer, "ELSE;\n");
2815 static void shader_hw_endif(const struct wined3d_shader_instruction *ins)
2817 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2818 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2819 struct list *e = list_head(&priv->control_frames);
2820 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2821 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2825 if(control_frame->had_else)
2827 shader_addline(buffer, "ifc_%u_endif:\n", control_frame->ifc_no);
2831 shader_addline(buffer, "#No else branch. else is endif\n");
2832 shader_addline(buffer, "ifc_%u_else:\n", control_frame->ifc_no);
2837 shader_addline(buffer, "ENDIF;\n");
2841 static void shader_hw_texldd(const struct wined3d_shader_instruction *ins)
2843 DWORD sampler_idx = ins->src[1].reg.idx;
2845 char reg_src[3][40];
2846 DWORD flags = TEX_DERIV;
2848 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
2849 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src[0]);
2850 shader_arb_get_src_param(ins, &ins->src[2], 1, reg_src[1]);
2851 shader_arb_get_src_param(ins, &ins->src[3], 2, reg_src[2]);
2853 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
2854 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
2856 shader_hw_sample(ins, sampler_idx, reg_dest, reg_src[0], flags, reg_src[1], reg_src[2]);
2859 static void shader_hw_texldl(const struct wined3d_shader_instruction *ins)
2861 DWORD sampler_idx = ins->src[1].reg.idx;
2864 DWORD flags = TEX_LOD;
2866 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
2867 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_coord);
2869 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
2870 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
2872 shader_hw_sample(ins, sampler_idx, reg_dest, reg_coord, flags, NULL, NULL);
2875 static void shader_hw_label(const struct wined3d_shader_instruction *ins)
2877 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2878 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2880 priv->in_main_func = FALSE;
2881 /* Call instructions activate the NV extensions, not labels and rets. If there is an uncalled
2882 * subroutine, don't generate a label that will make GL complain
2884 if(priv->target_version == ARB) return;
2886 shader_addline(buffer, "l%u:\n", ins->src[0].reg.idx);
2889 static void vshader_add_footer(IWineD3DVertexShaderImpl *This, struct wined3d_shader_buffer *buffer,
2890 const struct arb_vs_compile_args *args, struct shader_arb_ctx_priv *priv_ctx)
2892 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
2893 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
2894 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2897 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
2898 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
2899 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
2900 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
2902 if(args->super.fog_src == VS_FOG_Z) {
2903 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2904 } else if (!reg_maps->fog) {
2905 /* posFixup.x is always 1.0, so we can savely use it */
2906 shader_addline(buffer, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
2909 /* Write the final position.
2911 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2912 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2913 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2914 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2916 shader_addline(buffer, "MUL TA, posFixup, TMP_OUT.w;\n");
2917 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
2918 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
2920 if(use_nv_clip(gl_info) && priv_ctx->target_version >= NV2)
2922 for(i = 0; i < priv_ctx->vs_clipplanes; i++)
2924 shader_addline(buffer, "DP4 result.clip[%u].x, TMP_OUT, state.clip[%u].plane;\n", i, i);
2927 else if(args->boolclip.clip_control[0])
2929 unsigned int cur_clip = 0;
2930 char component[4] = {'x', 'y', 'z', 'w'};
2932 for(i = 0; i < GL_LIMITS(clipplanes); i++)
2934 if(args->boolclip.clip_control[1] & (1 << i))
2936 shader_addline(buffer, "DP4 TA.%c, TMP_OUT, state.clip[%u].plane;\n",
2937 component[cur_clip++], i);
2943 shader_addline(buffer, "MOV TA, -helper_const.w;\n");
2946 shader_addline(buffer, "MOV TA.yzw, -helper_const.w;\n");
2949 shader_addline(buffer, "MOV TA.zw, -helper_const.w;\n");
2952 shader_addline(buffer, "MOV TA.w, -helper_const.w;\n");
2955 shader_addline(buffer, "MOV result.texcoord[%u], TA;\n",
2956 args->boolclip.clip_control[0] - 1);
2959 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2960 * and the glsl equivalent
2962 if(need_helper_const(gl_info)) {
2963 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2965 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
2966 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
2969 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2971 priv_ctx->footer_written = TRUE;
2974 static void shader_hw_ret(const struct wined3d_shader_instruction *ins)
2976 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2977 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2978 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *) ins->ctx->shader;
2979 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2981 if(priv->target_version == ARB) return;
2985 if(priv->in_main_func) vshader_add_footer((IWineD3DVertexShaderImpl *) shader, buffer, priv->cur_vs_args, priv);
2988 shader_addline(buffer, "RET;\n");
2991 static void shader_hw_call(const struct wined3d_shader_instruction *ins)
2993 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2994 shader_addline(buffer, "CAL l%u;\n", ins->src[0].reg.idx);
2997 /* GL locking is done by the caller */
2998 static GLuint create_arb_blt_vertex_program(const struct wined3d_gl_info *gl_info)
3000 GLuint program_id = 0;
3003 const char *blt_vprogram =
3005 "PARAM c[1] = { { 1, 0.5 } };\n"
3006 "MOV result.position, vertex.position;\n"
3007 "MOV result.color, c[0].x;\n"
3008 "MOV result.texcoord[0], vertex.texcoord[0];\n"
3011 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3012 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
3013 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3014 strlen(blt_vprogram), blt_vprogram));
3015 checkGLcall("glProgramStringARB()");
3017 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3020 FIXME("Vertex program error at position %d: %s\n", pos,
3021 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3027 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3028 checkGLcall("glGetProgramivARB()");
3029 if (!native) WARN("Program exceeds native resource limits.\n");
3035 /* GL locking is done by the caller */
3036 static GLuint create_arb_blt_fragment_program(const struct wined3d_gl_info *gl_info, enum tex_types tex_type)
3038 GLuint program_id = 0;
3041 static const char * const blt_fprograms[tex_type_count] =
3048 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3049 "MOV result.depth.z, R0.x;\n"
3056 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3057 "MOV result.depth.z, R0.x;\n"
3062 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3063 "MOV result.depth.z, R0.x;\n"
3067 if (!blt_fprograms[tex_type])
3069 FIXME("tex_type %#x not supported\n", tex_type);
3073 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3074 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
3075 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3076 strlen(blt_fprograms[tex_type]), blt_fprograms[tex_type]));
3077 checkGLcall("glProgramStringARB()");
3079 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3082 FIXME("Fragment program error at position %d: %s\n", pos,
3083 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3089 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3090 checkGLcall("glGetProgramivARB()");
3091 if (!native) WARN("Program exceeds native resource limits.\n");
3097 static void arbfp_add_sRGB_correction(struct wined3d_shader_buffer *buffer, const char *fragcolor,
3098 const char *tmp1, const char *tmp2, const char *tmp3, const char *tmp4, BOOL condcode)
3100 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
3104 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
3105 shader_addline(buffer, "SUBC %s, %s.x, srgb_consts1.y;\n", tmp1, fragcolor);
3106 /* Calculate the > 0.0031308 case */
3107 shader_addline(buffer, "POW %s.x (GE), %s.x, srgb_consts1.z;\n", fragcolor, fragcolor);
3108 shader_addline(buffer, "POW %s.y (GE), %s.y, srgb_consts1.z;\n", fragcolor, fragcolor);
3109 shader_addline(buffer, "POW %s.z (GE), %s.z, srgb_consts1.z;\n", fragcolor, fragcolor);
3110 shader_addline(buffer, "MUL %s.xyz (GE), %s, srgb_consts1.w;\n", fragcolor, fragcolor);
3111 shader_addline(buffer, "SUB %s.xyz (GE), %s, srgb_consts2.x;\n", fragcolor, fragcolor);
3112 /* Calculate the < case */
3113 shader_addline(buffer, "MUL %s.xyz (LT), srgb_consts1.x, %s;\n", fragcolor, fragcolor);
3117 /* Calculate the > 0.0031308 case */
3118 shader_addline(buffer, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1, fragcolor);
3119 shader_addline(buffer, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1, fragcolor);
3120 shader_addline(buffer, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1, fragcolor);
3121 shader_addline(buffer, "MUL %s, %s, srgb_consts1.w;\n", tmp1, tmp1);
3122 shader_addline(buffer, "SUB %s, %s, srgb_consts2.x;\n", tmp1, tmp1);
3123 /* Calculate the < case */
3124 shader_addline(buffer, "MUL %s, srgb_consts1.x, %s;\n", tmp2, fragcolor);
3125 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
3126 shader_addline(buffer, "SLT %s, srgb_consts1.y, %s;\n", tmp3, fragcolor);
3127 shader_addline(buffer, "SGE %s, srgb_consts1.y, %s;\n", tmp4, fragcolor);
3128 /* Store the components > 0.0031308 in the destination */
3129 shader_addline(buffer, "MUL %s.xyz, %s, %s;\n", fragcolor, tmp1, tmp3);
3130 /* Add the components that are < 0.0031308 */
3131 shader_addline(buffer, "MAD %s.xyz, %s, %s, %s;\n", fragcolor, tmp2, tmp4, fragcolor);
3132 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
3133 * result.color writes(.rgb first, then .a), or handle overwriting already written
3134 * components. The assembler uses a temporary register in this case, which is usually
3135 * not allocated from one of our registers that were used earlier.
3138 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
3139 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
3142 static const DWORD *find_loop_control_values(IWineD3DBaseShaderImpl *This, DWORD idx)
3144 const local_constant *constant;
3146 LIST_FOR_EACH_ENTRY(constant, &This->baseShader.constantsI, local_constant, entry)
3148 if (constant->idx == idx)
3150 return constant->value;
3156 static void init_ps_input(const IWineD3DPixelShaderImpl *This, const struct arb_ps_compile_args *args,
3157 struct shader_arb_ctx_priv *priv)
3159 const char *texcoords[8] =
3161 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
3162 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
3165 const struct wined3d_shader_signature_element *sig = This->input_signature;
3166 const char *semantic_name;
3169 switch(args->super.vp_mode)
3171 case pretransformed:
3173 /* The pixelshader has to collect the varyings on its own. In any case properly load
3174 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
3175 * other attribs to 0.0.
3177 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
3178 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
3179 * load the texcoord attrib pointers to match the pixel shader signature
3181 for(i = 0; i < MAX_REG_INPUT; i++)
3183 semantic_name = sig[i].semantic_name;
3184 semantic_idx = sig[i].semantic_idx;
3185 if(semantic_name == NULL) continue;
3187 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3189 if(semantic_idx == 0) priv->ps_input[i] = "fragment.color.primary";
3190 else if(semantic_idx == 1) priv->ps_input[i] = "fragment.color.secondary";
3191 else priv->ps_input[i] = "0.0";
3193 else if(args->super.vp_mode == fixedfunction)
3195 priv->ps_input[i] = "0.0";
3197 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3199 if(semantic_idx < 8) priv->ps_input[i] = texcoords[semantic_idx];
3200 else priv->ps_input[i] = "0.0";
3202 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3204 if(semantic_idx == 0) priv->ps_input[i] = "fragment.fogcoord";
3205 else priv->ps_input[i] = "0.0";
3209 priv->ps_input[i] = "0.0";
3212 TRACE("v%u, semantic %s%u is %s\n", i, semantic_name, semantic_idx, priv->ps_input[i]);
3217 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
3220 for(i = 0; i < 8; i++)
3222 priv->ps_input[i] = texcoords[i];
3224 priv->ps_input[8] = "fragment.color.primary";
3225 priv->ps_input[9] = "fragment.color.secondary";
3230 /* GL locking is done by the caller */
3231 static GLuint shader_arb_generate_pshader(IWineD3DPixelShaderImpl *This, struct wined3d_shader_buffer *buffer,
3232 const struct arb_ps_compile_args *args, struct arb_ps_compiled_shader *compiled)
3234 const shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3235 CONST DWORD *function = This->baseShader.function;
3236 const struct wined3d_gl_info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3237 const local_constant *lconst;
3240 DWORD *lconst_map = local_const_mapping((IWineD3DBaseShaderImpl *) This), next_local, cur;
3241 struct shader_arb_ctx_priv priv_ctx;
3242 BOOL dcl_tmp = args->super.srgb_correction, dcl_td = FALSE;
3243 BOOL want_nv_prog = FALSE;
3244 struct arb_pshader_private *shader_priv = This->backend_priv;
3249 unsigned int i, found = 0;
3251 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
3254 || (This->color0_mov && i == This->color0_reg)
3255 || (reg_maps->shader_version.major < 2 && i == 0))
3258 sprintf(srgbtmp[found], "R%u", i);
3260 if (found == 4) break;
3264 case 4: dcl_tmp = FALSE; break;
3266 sprintf(srgbtmp[0], "TA");
3267 sprintf(srgbtmp[1], "TB");
3268 sprintf(srgbtmp[2], "TC");
3269 sprintf(srgbtmp[3], "TD");
3273 sprintf(srgbtmp[1], "TA");
3274 sprintf(srgbtmp[2], "TB");
3275 sprintf(srgbtmp[3], "TC");
3278 sprintf(srgbtmp[2], "TA");
3279 sprintf(srgbtmp[3], "TB");
3282 sprintf(srgbtmp[3], "TA");
3286 /* Create the hw ARB shader */
3287 memset(&priv_ctx, 0, sizeof(priv_ctx));
3288 priv_ctx.cur_ps_args = args;
3289 priv_ctx.compiled_fprog = compiled;
3290 priv_ctx.cur_np2fixup_info = &compiled->np2fixup_info;
3291 init_ps_input(This, args, &priv_ctx);
3292 list_init(&priv_ctx.control_frames);
3294 /* Avoid enabling NV_fragment_program* if we do not need it.
3296 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
3297 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
3298 * is faster than what we gain from using higher native instructions. There are some things though
3299 * that cannot be emulated. In that case enable the extensions.
3300 * If the extension is enabled, instruction handlers that support both ways will use it.
3302 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
3303 * So enable the best we can get.
3305 if(reg_maps->usesdsx || reg_maps->usesdsy || reg_maps->loop_depth > 0 || reg_maps->usestexldd ||
3306 reg_maps->usestexldl || reg_maps->usesfacing || reg_maps->usesifc || reg_maps->usescall)
3308 want_nv_prog = TRUE;
3311 shader_addline(buffer, "!!ARBfp1.0\n");
3312 if(want_nv_prog && GL_SUPPORT(NV_FRAGMENT_PROGRAM2)) {
3313 shader_addline(buffer, "OPTION NV_fragment_program2;\n");
3314 priv_ctx.target_version = NV3;
3315 } else if(want_nv_prog && GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION)) {
3316 shader_addline(buffer, "OPTION NV_fragment_program;\n");
3317 priv_ctx.target_version = NV2;
3321 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
3324 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
3327 priv_ctx.target_version = ARB;
3330 if(This->baseShader.reg_maps.highest_render_target > 0)
3332 shader_addline(buffer, "OPTION ARB_draw_buffers;\n");
3335 if (reg_maps->shader_version.major < 3)
3337 switch(args->super.fog) {
3341 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
3344 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
3347 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
3352 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
3353 * unused temps away(but occupies them for the whole shader if they're used once). Always
3354 * declaring them avoids tricky bookkeeping work
3356 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
3357 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
3358 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
3359 if(dcl_td) shader_addline(buffer, "TEMP TD;\n"); /* Used for sRGB writing */
3360 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
3361 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
3362 shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
3364 if (reg_maps->shader_version.major < 2)
3366 strcpy(fragcolor, "R0");
3368 if(args->super.srgb_correction) {
3369 if(This->color0_mov) {
3370 sprintf(fragcolor, "R%u", This->color0_reg);
3372 shader_addline(buffer, "TEMP TMP_COLOR;\n");
3373 strcpy(fragcolor, "TMP_COLOR");
3376 strcpy(fragcolor, "result.color");
3380 if(args->super.srgb_correction) {
3381 shader_addline(buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
3382 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
3383 shader_addline(buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
3384 srgb_sub_high, 0.0, 0.0, 0.0);
3387 /* Base Declarations */
3388 next_local = shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION,
3389 lconst_map, NULL, &priv_ctx);
3391 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
3393 if (!(map & 1)) continue;
3395 cur = compiled->numbumpenvmatconsts;
3396 compiled->bumpenvmatconst[cur].const_num = WINED3D_CONST_NUM_UNUSED;
3397 compiled->bumpenvmatconst[cur].texunit = i;
3398 compiled->luminanceconst[cur].const_num = WINED3D_CONST_NUM_UNUSED;
3399 compiled->luminanceconst[cur].texunit = i;
3401 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3402 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3403 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3404 * textures due to conditional NP2 restrictions)
3406 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3407 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3408 * their location is shader dependent anyway and they cannot be loaded globally.
3410 compiled->bumpenvmatconst[cur].const_num = next_local++;
3411 shader_addline(buffer, "PARAM bumpenvmat%d = program.local[%d];\n",
3412 i, compiled->bumpenvmatconst[cur].const_num);
3413 compiled->numbumpenvmatconsts = cur + 1;
3415 if (!(reg_maps->luminanceparams & (1 << i))) continue;
3417 compiled->luminanceconst[cur].const_num = next_local++;
3418 shader_addline(buffer, "PARAM luminance%d = program.local[%d];\n",
3419 i, compiled->luminanceconst[cur].const_num);
3422 for(i = 0; i < MAX_CONST_I; i++)
3424 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
3425 if (reg_maps->integer_constants & (1 << i) && priv_ctx.target_version >= NV2)
3427 const DWORD *control_values = find_loop_control_values((IWineD3DBaseShaderImpl *) This, i);
3431 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
3432 control_values[0], control_values[1], control_values[2]);
3436 compiled->int_consts[i] = next_local;
3437 compiled->num_int_consts++;
3438 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
3443 if(reg_maps->vpos || reg_maps->usesdsy)
3445 compiled->ycorrection = next_local;
3446 shader_addline(buffer, "PARAM ycorrection = program.local[%u];\n", next_local++);
3450 shader_addline(buffer, "TEMP vpos;\n");
3451 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3452 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3453 * ycorrection.z: 1.0
3454 * ycorrection.w: 0.0
3456 shader_addline(buffer, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3457 shader_addline(buffer, "FLR vpos.xy, vpos;\n");
3462 compiled->ycorrection = WINED3D_CONST_NUM_UNUSED;
3465 /* Load constants to fixup NP2 texcoords if there are still free constants left:
3466 * Constants (texture dimensions) for the NP2 fixup are loaded as local program parameters. This will consume
3467 * at most 8 (MAX_FRAGMENT_SAMPLERS / 2) parameters, which is highly unlikely, since the application had to
3468 * use 16 NP2 textures at the same time. In case that we run out of constants the fixup is simply not
3469 * applied / activated. This will probably result in wrong rendering of the texture, but will save us from
3470 * shader compilation errors and the subsequent errors when drawing with this shader. */
3471 if (priv_ctx.cur_ps_args->super.np2_fixup) {
3473 struct arb_ps_np2fixup_info* const fixup = priv_ctx.cur_np2fixup_info;
3474 const WORD map = priv_ctx.cur_ps_args->super.np2_fixup;
3475 const UINT max_lconsts = gl_info->ps_arb_max_local_constants;
3477 fixup->offset = next_local;
3478 fixup->super.active = 0;
3481 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
3482 if (!(map & (1 << i))) continue;
3484 if (fixup->offset + (cur >> 1) < max_lconsts) {
3485 fixup->super.active |= (1 << i);
3486 fixup->super.idx[i] = cur++;
3488 FIXME("No free constant found to load NP2 fixup data into shader. "
3489 "Sampling from this texture will probably look wrong.\n");
3494 fixup->super.num_consts = (cur + 1) >> 1;
3495 if (fixup->super.num_consts) {
3496 shader_addline(buffer, "PARAM np2fixup[%u] = { program.env[%u..%u] };\n",
3497 fixup->super.num_consts, fixup->offset, fixup->super.num_consts + fixup->offset - 1);
3500 next_local += fixup->super.num_consts;
3503 if (shader_priv->clipplane_emulation != ~0U && args->clip)
3505 shader_addline(buffer, "KIL fragment.texcoord[%u];\n", shader_priv->clipplane_emulation);
3508 /* Base Shader Body */
3509 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
3511 if(args->super.srgb_correction) {
3512 arbfp_add_sRGB_correction(buffer, fragcolor, srgbtmp[0], srgbtmp[1], srgbtmp[2], srgbtmp[3],
3513 priv_ctx.target_version >= NV2);
3514 } else if(reg_maps->shader_version.major < 2) {
3515 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
3517 shader_addline(buffer, "END\n");
3519 /* TODO: change to resource.glObjectHandle or something like that */
3520 GL_EXTCALL(glGenProgramsARB(1, &retval));
3522 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
3523 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
3525 TRACE("Created hw pixel shader, prg=%d\n", retval);
3526 /* Create the program and check for errors */
3527 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3528 buffer->bsize, buffer->buffer));
3529 checkGLcall("glProgramStringARB()");
3531 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
3534 FIXME("HW PixelShader Error at position %d: %s\n",
3535 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3542 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3543 checkGLcall("glGetProgramivARB()");
3544 if (!native) WARN("Program exceeds native resource limits.\n");
3547 /* Load immediate constants */
3549 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
3550 const float *value = (const float *)lconst->value;
3551 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst_map[lconst->idx], value));
3552 checkGLcall("glProgramLocalParameter4fvARB");
3554 HeapFree(GetProcessHeap(), 0, lconst_map);
3560 static int compare_sig(const struct wined3d_shader_signature_element *sig1, const struct wined3d_shader_signature_element *sig2)
3565 for(i = 0; i < MAX_REG_INPUT; i++)
3567 if(sig1[i].semantic_name == NULL || sig2[i].semantic_name == NULL)
3569 /* Compare pointers, not contents. One string is NULL(element does not exist), the other one is not NULL */
3570 if(sig1[i].semantic_name != sig2[i].semantic_name) return sig1[i].semantic_name < sig2[i].semantic_name ? -1 : 1;
3574 ret = strcmp(sig1[i].semantic_name, sig2[i].semantic_name);
3575 if(ret != 0) return ret;
3576 if(sig1[i].semantic_idx != sig2[i].semantic_idx) return sig1[i].semantic_idx < sig2[i].semantic_idx ? -1 : 1;
3577 if(sig1[i].sysval_semantic != sig2[i].sysval_semantic) return sig1[i].sysval_semantic < sig2[i].sysval_semantic ? -1 : 1;
3578 if(sig1[i].component_type != sig2[i].component_type) return sig1[i].sysval_semantic < sig2[i].component_type ? -1 : 1;
3579 if(sig1[i].register_idx != sig2[i].register_idx) return sig1[i].register_idx < sig2[i].register_idx ? -1 : 1;
3580 if(sig1[i].mask != sig2->mask) return sig1[i].mask < sig2[i].mask ? -1 : 1;
3585 static struct wined3d_shader_signature_element *clone_sig(const struct wined3d_shader_signature_element *sig)
3587 struct wined3d_shader_signature_element *new;
3591 new = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*new) * MAX_REG_INPUT);
3592 for(i = 0; i < MAX_REG_INPUT; i++)
3594 if(sig[i].semantic_name == NULL)
3600 /* Clone the semantic string */
3601 name = HeapAlloc(GetProcessHeap(), 0, strlen(sig[i].semantic_name) + 1);
3602 strcpy(name, sig[i].semantic_name);
3603 new[i].semantic_name = name;
3608 static DWORD find_input_signature(struct shader_arb_priv *priv, const struct wined3d_shader_signature_element *sig)
3610 struct wine_rb_entry *entry = wine_rb_get(&priv->signature_tree, sig);
3611 struct ps_signature *found_sig;
3615 found_sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
3616 TRACE("Found existing signature %u\n", found_sig->idx);
3617 return found_sig->idx;
3619 found_sig = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*sig));
3620 found_sig->sig = clone_sig(sig);
3621 found_sig->idx = priv->ps_sig_number++;
3622 TRACE("New signature stored and assigned number %u\n", found_sig->idx);
3623 if(wine_rb_put(&priv->signature_tree, sig, &found_sig->entry) == -1)
3625 ERR("Failed to insert program entry.\n");
3627 return found_sig->idx;
3630 static void init_output_registers(IWineD3DVertexShaderImpl *shader, DWORD sig_num, struct shader_arb_ctx_priv *priv_ctx,
3631 struct arb_vs_compiled_shader *compiled)
3634 static const char *texcoords[8] =
3636 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3637 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
3639 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) shader->baseShader.device;
3640 const struct wined3d_shader_signature_element *sig;
3641 const char *semantic_name;
3642 DWORD semantic_idx, reg_idx;
3644 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
3645 * and varying 9 to result.color.secondary
3647 const char *decl_idx_to_string[MAX_REG_INPUT] =
3649 texcoords[0], texcoords[1], texcoords[2], texcoords[3],
3650 texcoords[4], texcoords[5], texcoords[6], texcoords[7],
3651 "result.color.primary", "result.color.secondary"
3656 TRACE("Pixel shader uses builtin varyings\n");
3657 /* Map builtins to builtins */
3658 for(i = 0; i < 8; i++)
3660 priv_ctx->texcrd_output[i] = texcoords[i];
3662 priv_ctx->color_output[0] = "result.color.primary";
3663 priv_ctx->color_output[1] = "result.color.secondary";
3664 priv_ctx->fog_output = "result.fogcoord";
3666 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
3667 for(i = 0; i < (sizeof(shader->output_signature) / sizeof(*shader->output_signature)); i++)
3669 semantic_name = shader->output_signature[i].semantic_name;
3670 if(semantic_name == NULL) continue;
3672 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3674 TRACE("o%u is TMP_OUT\n", i);
3675 if(shader->output_signature[i].semantic_idx == 0) priv_ctx->vs_output[i] = "TMP_OUT";
3676 else priv_ctx->vs_output[i] = "TA";
3678 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3680 TRACE("o%u is result.pointsize\n", i);
3681 if(shader->output_signature[i].semantic_idx == 0) priv_ctx->vs_output[i] = "result.pointsize";
3682 else priv_ctx->vs_output[i] = "TA";
3684 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3686 TRACE("o%u is result.color.?, idx %u\n", i, shader->output_signature[i].semantic_idx);
3687 if(shader->output_signature[i].semantic_idx == 0) priv_ctx->vs_output[i] = "result.color.primary";
3688 else if(shader->output_signature[i].semantic_idx == 1) priv_ctx->vs_output[i] = "result.color.secondary";
3689 else priv_ctx->vs_output[i] = "TA";
3691 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3693 TRACE("o%u is %s\n", i, texcoords[shader->output_signature[i].semantic_idx]);
3694 if(shader->output_signature[i].semantic_idx >= 8) priv_ctx->vs_output[i] = "TA";
3695 else priv_ctx->vs_output[i] = texcoords[shader->output_signature[i].semantic_idx];
3697 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3699 TRACE("o%u is result.fogcoord\n", i);
3700 if(shader->output_signature[i].semantic_idx > 0) priv_ctx->vs_output[i] = "TA";
3701 else priv_ctx->vs_output[i] = "result.fogcoord";
3705 priv_ctx->vs_output[i] = "TA";
3711 /* Instead of searching for the signature in the signature list, read the one from the current pixel shader.
3712 * Its maybe not the shader where the signature came from, but it is the same signature and faster to find
3714 sig = ((IWineD3DPixelShaderImpl *)device->stateBlock->pixelShader)->input_signature;
3715 TRACE("Pixel shader uses declared varyings\n");
3717 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
3718 for(i = 0; i < 8; i++)
3720 priv_ctx->texcrd_output[i] = "TA";
3722 priv_ctx->color_output[0] = "TA";
3723 priv_ctx->color_output[1] = "TA";
3724 priv_ctx->fog_output = "TA";
3726 for(i = 0; i < MAX_REG_INPUT; i++)
3728 semantic_name = sig[i].semantic_name;
3729 semantic_idx = sig[i].semantic_idx;
3730 reg_idx = sig[i].register_idx;
3731 if(semantic_name == NULL) continue;
3733 /* If a declared input register is not written by builtin arguments, don't write to it.
3734 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
3736 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
3737 * to TMP_OUT in any case
3739 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3741 if(semantic_idx < 8) priv_ctx->texcrd_output[semantic_idx] = decl_idx_to_string[reg_idx];
3743 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3745 if(semantic_idx < 2) priv_ctx->color_output[semantic_idx] = decl_idx_to_string[reg_idx];
3747 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3749 if(semantic_idx == 0) priv_ctx->fog_output = decl_idx_to_string[reg_idx];
3756 if(strcmp(decl_idx_to_string[reg_idx], "result.color.primary") == 0 ||
3757 strcmp(decl_idx_to_string[reg_idx], "result.color.secondary") == 0)
3759 compiled->need_color_unclamp = TRUE;
3763 /* Map declared to declared */
3764 for(i = 0; i < (sizeof(shader->output_signature) / sizeof(*shader->output_signature)); i++)
3766 /* Write unread output to TA to throw them away */
3767 priv_ctx->vs_output[i] = "TA";
3768 semantic_name = shader->output_signature[i].semantic_name;
3769 if(semantic_name == NULL)
3774 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION) &&
3775 shader->output_signature[i].semantic_idx == 0)
3777 priv_ctx->vs_output[i] = "TMP_OUT";
3780 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE) &&
3781 shader->output_signature[i].semantic_idx == 0)
3783 priv_ctx->vs_output[i] = "result.pointsize";
3787 for(j = 0; j < MAX_REG_INPUT; j++)
3789 if(sig[j].semantic_name == NULL)
3794 if(strcmp(sig[j].semantic_name, semantic_name) == 0 &&
3795 sig[j].semantic_idx == shader->output_signature[i].semantic_idx)
3797 priv_ctx->vs_output[i] = decl_idx_to_string[sig[j].register_idx];
3799 if(strcmp(priv_ctx->vs_output[i], "result.color.primary") == 0 ||
3800 strcmp(priv_ctx->vs_output[i], "result.color.secondary") == 0)
3802 compiled->need_color_unclamp = TRUE;
3809 /* GL locking is done by the caller */
3810 static GLuint shader_arb_generate_vshader(IWineD3DVertexShaderImpl *This, struct wined3d_shader_buffer *buffer,
3811 const struct arb_vs_compile_args *args, struct arb_vs_compiled_shader *compiled)
3813 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3814 CONST DWORD *function = This->baseShader.function;
3815 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3816 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3817 const local_constant *lconst;
3819 DWORD next_local, *lconst_map = local_const_mapping((IWineD3DBaseShaderImpl *) This);
3820 struct shader_arb_ctx_priv priv_ctx;
3824 memset(&priv_ctx, 0, sizeof(priv_ctx));
3825 priv_ctx.cur_vs_args = args;
3826 list_init(&priv_ctx.control_frames);
3827 init_output_registers(This, args->ps_signature, &priv_ctx, compiled);
3829 /* Create the hw ARB shader */
3830 shader_addline(buffer, "!!ARBvp1.0\n");
3832 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
3833 * mesurable performance penalty, and we can always make use of it for clipplanes.
3835 if(GL_SUPPORT(NV_VERTEX_PROGRAM3)) {
3836 shader_addline(buffer, "OPTION NV_vertex_program3;\n");
3837 priv_ctx.target_version = NV3;
3838 shader_addline(buffer, "ADDRESS aL;\n");
3839 } else if(GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION)) {
3840 shader_addline(buffer, "OPTION NV_vertex_program2;\n");
3841 priv_ctx.target_version = NV2;
3842 shader_addline(buffer, "ADDRESS aL;\n");
3844 priv_ctx.target_version = ARB;
3847 shader_addline(buffer, "TEMP TMP_OUT;\n");
3848 if(need_helper_const(gl_info)) {
3849 shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
3851 if(need_mova_const((IWineD3DBaseShader *) This, gl_info)) {
3852 shader_addline(buffer, "PARAM mova_const = { 0.5, 0.0, 2.0, 1.0 };\n");
3853 shader_addline(buffer, "TEMP A0_SHADOW;\n");
3856 shader_addline(buffer, "TEMP TA;\n");
3858 /* Base Declarations */
3859 next_local = shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION,
3860 lconst_map, &priv_ctx.vs_clipplanes, &priv_ctx);
3862 for(i = 0; i < MAX_CONST_I; i++)
3864 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
3865 if(reg_maps->integer_constants & (1 << i) && priv_ctx.target_version >= NV2)
3867 const DWORD *control_values = find_loop_control_values((IWineD3DBaseShaderImpl *) This, i);
3871 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
3872 control_values[0], control_values[1], control_values[2]);
3876 compiled->int_consts[i] = next_local;
3877 compiled->num_int_consts++;
3878 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
3883 /* We need a constant to fixup the final position */
3884 shader_addline(buffer, "PARAM posFixup = program.local[%u];\n", next_local);
3885 compiled->pos_fixup = next_local++;
3887 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
3888 * for output parameters. D3D in theory does not do that either, but some applications depend on a
3889 * proper initialization of the secondary color, and programs using the fixed function pipeline without
3890 * a replacement shader depend on the texcoord.w being set properly.
3892 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
3893 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
3894 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
3895 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
3896 * this can eat a number of instructions, so skip it unless this cap is set as well
3898 if(!GL_SUPPORT(NV_VERTEX_PROGRAM)) {
3899 shader_addline(buffer, "MOV result.color.secondary, -helper_const.wwwy;\n");
3901 if ((GLINFO_LOCATION).quirks & WINED3D_QUIRK_SET_TEXCOORD_W && !device->frag_pipe->ffp_proj_control)
3904 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3905 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3906 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3907 shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
3913 /* The shader starts with the main function */
3914 priv_ctx.in_main_func = TRUE;
3915 /* Base Shader Body */
3916 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
3918 if(!priv_ctx.footer_written) vshader_add_footer(This, buffer, args, &priv_ctx);
3920 shader_addline(buffer, "END\n");
3922 /* TODO: change to resource.glObjectHandle or something like that */
3923 GL_EXTCALL(glGenProgramsARB(1, &ret));
3925 TRACE("Creating a hw vertex shader, prg=%d\n", ret);
3926 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, ret));
3928 TRACE("Created hw vertex shader, prg=%d\n", ret);
3929 /* Create the program and check for errors */
3930 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3931 buffer->bsize, buffer->buffer));
3932 checkGLcall("glProgramStringARB()");
3934 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
3937 FIXME("HW VertexShader Error at position %d: %s\n",
3938 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3945 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3946 checkGLcall("glGetProgramivARB()");
3947 if (!native) WARN("Program exceeds native resource limits.\n");
3949 /* Load immediate constants */
3951 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
3952 const float *value = (const float *)lconst->value;
3953 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst_map[lconst->idx], value));
3957 HeapFree(GetProcessHeap(), 0, lconst_map);
3962 /* GL locking is done by the caller */
3963 static struct arb_ps_compiled_shader *find_arb_pshader(IWineD3DPixelShaderImpl *shader, const struct arb_ps_compile_args *args)
3967 struct arb_ps_compiled_shader *new_array;
3968 struct wined3d_shader_buffer buffer;
3969 struct arb_pshader_private *shader_data;
3972 if(!shader->backend_priv) {
3973 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) shader->baseShader.device;
3974 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3975 struct shader_arb_priv *priv = device->shader_priv;
3977 shader->backend_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
3978 shader_data = shader->backend_priv;
3979 shader_data->clamp_consts = shader->baseShader.reg_maps.shader_version.major == 1;
3981 if(shader->baseShader.reg_maps.shader_version.major < 3) shader_data->input_signature_idx = ~0;
3982 else shader_data->input_signature_idx = find_input_signature(priv, shader->input_signature);
3984 shader_data->has_signature_idx = TRUE;
3985 TRACE("Shader got assigned input signature index %u\n", shader_data->input_signature_idx);
3987 if (!device->vs_clipping)
3988 shader_data->clipplane_emulation = shader_find_free_input_register(&shader->baseShader.reg_maps,
3989 GL_LIMITS(texture_stages) - 1);
3991 shader_data->clipplane_emulation = ~0U;
3993 shader_data = shader->backend_priv;
3995 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
3996 * so a linear search is more performant than a hashmap or a binary search
3997 * (cache coherency etc)
3999 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4000 if(memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)) == 0) {
4001 return &shader_data->gl_shaders[i];
4005 TRACE("No matching GL shader found, compiling a new shader\n");
4006 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4007 if (shader_data->num_gl_shaders)
4009 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4010 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4011 new_size * sizeof(*shader_data->gl_shaders));
4013 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4018 ERR("Out of memory\n");
4021 shader_data->gl_shaders = new_array;
4022 shader_data->shader_array_size = new_size;
4025 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4027 pixelshader_update_samplers(&shader->baseShader.reg_maps,
4028 ((IWineD3DDeviceImpl *)shader->baseShader.device)->stateBlock->textures);
4030 if (!shader_buffer_init(&buffer))
4032 ERR("Failed to initialize shader buffer.\n");
4036 ret = shader_arb_generate_pshader(shader, &buffer, args,
4037 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4038 shader_buffer_free(&buffer);
4039 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4041 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4044 static inline BOOL vs_args_equal(const struct arb_vs_compile_args *stored, const struct arb_vs_compile_args *new,
4045 const DWORD use_map, BOOL skip_int) {
4046 if((stored->super.swizzle_map & use_map) != new->super.swizzle_map) return FALSE;
4047 if(stored->super.fog_src != new->super.fog_src) return FALSE;
4048 if(stored->boolclip_compare != new->boolclip_compare) return FALSE;
4049 if(stored->ps_signature != new->ps_signature) return FALSE;
4050 if(stored->vertex_samplers_compare != new->vertex_samplers_compare) return FALSE;
4051 if(skip_int) return TRUE;
4053 return memcmp(stored->loop_ctrl, new->loop_ctrl, sizeof(stored->loop_ctrl)) == 0;
4056 static struct arb_vs_compiled_shader *find_arb_vshader(IWineD3DVertexShaderImpl *shader, const struct arb_vs_compile_args *args)
4060 struct arb_vs_compiled_shader *new_array;
4061 DWORD use_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.use_map;
4062 struct wined3d_shader_buffer buffer;
4063 struct arb_vshader_private *shader_data;
4065 const struct wined3d_gl_info *gl_info = &((IWineD3DDeviceImpl *)shader->baseShader.device)->adapter->gl_info;
4067 if(!shader->backend_priv) {
4068 shader->backend_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4070 shader_data = shader->backend_priv;
4072 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4073 * so a linear search is more performant than a hashmap or a binary search
4074 * (cache coherency etc)
4076 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4077 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map, GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION))) {
4078 return &shader_data->gl_shaders[i];
4082 TRACE("No matching GL shader found, compiling a new shader\n");
4084 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4085 if (shader_data->num_gl_shaders)
4087 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4088 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4089 new_size * sizeof(*shader_data->gl_shaders));
4091 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4096 ERR("Out of memory\n");
4099 shader_data->gl_shaders = new_array;
4100 shader_data->shader_array_size = new_size;
4103 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4105 if (!shader_buffer_init(&buffer))
4107 ERR("Failed to initialize shader buffer.\n");
4111 ret = shader_arb_generate_vshader(shader, &buffer, args,
4112 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4113 shader_buffer_free(&buffer);
4114 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4116 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4119 static inline void find_arb_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock,
4120 struct arb_ps_compile_args *args)
4124 const struct wined3d_gl_info *gl_info = &((IWineD3DDeviceImpl *)shader->baseShader.device)->adapter->gl_info;
4125 find_ps_compile_args(shader, stateblock, &args->super);
4127 /* This forces all local boolean constants to 1 to make them stateblock independent */
4128 args->bools = shader->baseShader.reg_maps.local_bool_consts;
4130 for(i = 0; i < MAX_CONST_B; i++)
4132 if(stateblock->pixelShaderConstantB[i]) args->bools |= ( 1 << i);
4135 /* Only enable the clip plane emulation KIL if at least one clipplane is enabled. The KIL instruction
4136 * is quite expensive because it forces the driver to disable early Z discards. It is cheaper to
4137 * duplicate the shader than have a no-op KIL instruction in every shader
4139 if((!((IWineD3DDeviceImpl *) shader->baseShader.device)->vs_clipping) && use_vs(stateblock) &&
4140 stateblock->renderState[WINED3DRS_CLIPPING] && stateblock->renderState[WINED3DRS_CLIPPLANEENABLE])
4149 /* Skip if unused or local, or supported natively */
4150 int_skip = ~shader->baseShader.reg_maps.integer_constants | shader->baseShader.reg_maps.local_int_consts;
4151 if(int_skip == 0xffff || GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION))
4153 memset(&args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4157 for(i = 0; i < MAX_CONST_I; i++)
4159 if(int_skip & (1 << i))
4161 args->loop_ctrl[i][0] = 0;
4162 args->loop_ctrl[i][1] = 0;
4163 args->loop_ctrl[i][2] = 0;
4167 args->loop_ctrl[i][0] = stateblock->pixelShaderConstantI[i * 4];
4168 args->loop_ctrl[i][1] = stateblock->pixelShaderConstantI[i * 4 + 1];
4169 args->loop_ctrl[i][2] = stateblock->pixelShaderConstantI[i * 4 + 2];
4174 static inline void find_arb_vs_compile_args(IWineD3DVertexShaderImpl *shader, IWineD3DStateBlockImpl *stateblock,
4175 struct arb_vs_compile_args *args)
4179 IWineD3DDeviceImpl *dev = (IWineD3DDeviceImpl *)shader->baseShader.device;
4180 const struct wined3d_gl_info *gl_info = &dev->adapter->gl_info;
4181 find_vs_compile_args(shader, stateblock, &args->super);
4183 args->boolclip_compare = 0;
4184 if(use_ps(stateblock))
4186 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) stateblock->pixelShader;
4187 struct arb_pshader_private *shader_priv = ps->backend_priv;
4188 args->ps_signature = shader_priv->input_signature_idx;
4190 args->boolclip.clip_control[0] = shader_priv->clipplane_emulation + 1;
4194 args->ps_signature = ~0;
4195 if(!dev->vs_clipping)
4197 args->boolclip.clip_control[0] = ffp_clip_emul(stateblock) ? GL_LIMITS(texture_stages) : 0;
4199 /* Otherwise: Setting boolclip_compare set clip_control[0] to 0 */
4202 if(args->boolclip.clip_control[0])
4204 if(stateblock->renderState[WINED3DRS_CLIPPING])
4206 args->boolclip.clip_control[1] = stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
4208 /* clip_control[1] was set to 0 by setting boolclip_compare to 0 */
4211 /* This forces all local boolean constants to 1 to make them stateblock independent */
4212 args->boolclip.bools = shader->baseShader.reg_maps.local_bool_consts;
4213 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
4214 for(i = 0; i < MAX_CONST_B; i++)
4216 if(stateblock->vertexShaderConstantB[i]) args->boolclip.bools |= ( 1 << i);
4219 args->vertex_samplers[0] = dev->texUnitMap[MAX_FRAGMENT_SAMPLERS + 0];
4220 args->vertex_samplers[1] = dev->texUnitMap[MAX_FRAGMENT_SAMPLERS + 1];
4221 args->vertex_samplers[2] = dev->texUnitMap[MAX_FRAGMENT_SAMPLERS + 2];
4222 args->vertex_samplers[3] = 0;
4224 /* Skip if unused or local */
4225 int_skip = ~shader->baseShader.reg_maps.integer_constants | shader->baseShader.reg_maps.local_int_consts;
4226 if(int_skip == 0xffff || GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION)) /* This is about flow control, not clipping */
4228 memset(&args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4232 for(i = 0; i < MAX_CONST_I; i++)
4234 if(int_skip & (1 << i))
4236 args->loop_ctrl[i][0] = 0;
4237 args->loop_ctrl[i][1] = 0;
4238 args->loop_ctrl[i][2] = 0;
4242 args->loop_ctrl[i][0] = stateblock->vertexShaderConstantI[i * 4];
4243 args->loop_ctrl[i][1] = stateblock->vertexShaderConstantI[i * 4 + 1];
4244 args->loop_ctrl[i][2] = stateblock->vertexShaderConstantI[i * 4 + 2];
4249 /* GL locking is done by the caller */
4250 static void shader_arb_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4252 IWineD3DDeviceImpl *This = ((IWineD3DSurfaceImpl *)context->surface)->resource.wineD3DDevice;
4253 struct shader_arb_priv *priv = This->shader_priv;
4254 const struct wined3d_gl_info *gl_info = context->gl_info;
4257 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
4259 struct arb_ps_compile_args compile_args;
4260 struct arb_ps_compiled_shader *compiled;
4261 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader;
4263 TRACE("Using pixel shader %p\n", This->stateBlock->pixelShader);
4264 find_arb_ps_compile_args(ps, This->stateBlock, &compile_args);
4265 compiled = find_arb_pshader(ps, &compile_args);
4266 priv->current_fprogram_id = compiled->prgId;
4267 priv->compiled_fprog = compiled;
4269 /* Bind the fragment program */
4270 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4271 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
4273 if(!priv->use_arbfp_fixed_func) {
4274 /* Enable OpenGL fragment programs */
4275 glEnable(GL_FRAGMENT_PROGRAM_ARB);
4276 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
4278 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
4280 /* Pixel Shader 1.x constants are clamped to [-1;1], Pixel Shader 2.0 constants are not. If switching between
4281 * a 1.x and newer shader, reload the first 8 constants
4283 if(priv->last_ps_const_clamped != ((struct arb_pshader_private *) ps->backend_priv)->clamp_consts)
4285 priv->last_ps_const_clamped = ((struct arb_pshader_private *) ps->backend_priv)->clamp_consts;
4286 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, 8);
4287 for(i = 0; i < 8; i++)
4289 context->pshader_const_dirty[i] = 1;
4291 /* Also takes care of loading local constants */
4292 shader_arb_load_constants(context, TRUE, FALSE);
4296 shader_arb_ps_local_constants(This);
4299 /* Force constant reloading for the NP2 fixup (see comment in shader_glsl_select for more info) */
4300 if (compiled->np2fixup_info.super.active)
4301 shader_arb_load_np2fixup_constants((IWineD3DDevice *)This, usePS, useVS);
4302 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && !priv->use_arbfp_fixed_func) {
4303 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
4304 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
4305 * replacement shader
4307 glDisable(GL_FRAGMENT_PROGRAM_ARB);
4308 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4309 priv->current_fprogram_id = 0;
4313 struct arb_vs_compile_args compile_args;
4314 struct arb_vs_compiled_shader *compiled;
4315 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) This->stateBlock->vertexShader;
4317 TRACE("Using vertex shader %p\n", This->stateBlock->vertexShader);
4318 find_arb_vs_compile_args(vs, This->stateBlock, &compile_args);
4319 compiled = find_arb_vshader(vs, &compile_args);
4320 priv->current_vprogram_id = compiled->prgId;
4321 priv->compiled_vprog = compiled;
4323 /* Bind the vertex program */
4324 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4325 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
4327 /* Enable OpenGL vertex programs */
4328 glEnable(GL_VERTEX_PROGRAM_ARB);
4329 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
4330 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
4331 shader_arb_vs_local_constants(This);
4333 if(priv->last_vs_color_unclamp != compiled->need_color_unclamp) {
4334 priv->last_vs_color_unclamp = compiled->need_color_unclamp;
4336 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
4337 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, !compiled->need_color_unclamp));
4338 checkGLcall("glClampColorARB");
4340 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4343 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
4344 priv->current_vprogram_id = 0;
4345 glDisable(GL_VERTEX_PROGRAM_ARB);
4346 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4350 /* GL locking is done by the caller */
4351 static void shader_arb_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
4352 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4353 struct shader_arb_priv *priv = This->shader_priv;
4354 GLuint *blt_fprogram = &priv->depth_blt_fprogram_id[tex_type];
4355 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4357 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
4358 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
4359 glEnable(GL_VERTEX_PROGRAM_ARB);
4361 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type);
4362 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
4363 glEnable(GL_FRAGMENT_PROGRAM_ARB);
4366 /* GL locking is done by the caller */
4367 static void shader_arb_deselect_depth_blt(IWineD3DDevice *iface) {
4368 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4369 struct shader_arb_priv *priv = This->shader_priv;
4370 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4372 if (priv->current_vprogram_id) {
4373 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4374 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
4376 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
4378 glDisable(GL_VERTEX_PROGRAM_ARB);
4379 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4382 if (priv->current_fprogram_id) {
4383 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4384 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
4386 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
4387 } else if(!priv->use_arbfp_fixed_func) {
4388 glDisable(GL_FRAGMENT_PROGRAM_ARB);
4389 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4393 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
4394 IWineD3DBaseShaderImpl *baseShader = (IWineD3DBaseShaderImpl *) iface;
4395 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)baseShader->baseShader.device;
4396 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4398 if (shader_is_pshader_version(baseShader->baseShader.reg_maps.shader_version.type))
4400 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
4401 struct arb_pshader_private *shader_data = This->backend_priv;
4404 if(!shader_data) return; /* This can happen if a shader was never compiled */
4407 if(shader_data->num_gl_shaders) ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
4409 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4410 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4411 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4414 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4415 HeapFree(GetProcessHeap(), 0, shader_data);
4416 This->backend_priv = NULL;
4418 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *) iface;
4419 struct arb_vshader_private *shader_data = This->backend_priv;
4422 if(!shader_data) return; /* This can happen if a shader was never compiled */
4425 if(shader_data->num_gl_shaders) ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
4427 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4428 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4429 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4432 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4433 HeapFree(GetProcessHeap(), 0, shader_data);
4434 This->backend_priv = NULL;
4438 static int sig_tree_compare(const void *key, const struct wine_rb_entry *entry)
4440 struct ps_signature *e = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4441 return compare_sig(key, e->sig);
4444 struct wine_rb_functions sig_tree_functions =
4452 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
4453 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4454 struct shader_arb_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv));
4455 if(wine_rb_init(&priv->signature_tree, &sig_tree_functions) == -1)
4457 ERR("RB tree init failed\n");
4458 HeapFree(GetProcessHeap(), 0, priv);
4459 return E_OUTOFMEMORY;
4461 This->shader_priv = priv;
4465 static void release_signature(struct wine_rb_entry *entry, void *context)
4467 struct ps_signature *sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4469 for(i = 0; i < MAX_REG_INPUT; i++)
4471 HeapFree(GetProcessHeap(), 0, (char *) sig->sig[i].semantic_name);
4473 HeapFree(GetProcessHeap(), 0, sig->sig);
4474 HeapFree(GetProcessHeap(), 0, sig);
4477 /* Context activation is done by the caller. */
4478 static void shader_arb_free(IWineD3DDevice *iface) {
4479 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4480 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4481 struct shader_arb_priv *priv = This->shader_priv;
4485 if(priv->depth_blt_vprogram_id) {
4486 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
4488 for (i = 0; i < tex_type_count; ++i) {
4489 if (priv->depth_blt_fprogram_id[i]) {
4490 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id[i]));
4495 wine_rb_destroy(&priv->signature_tree, release_signature, NULL);
4496 HeapFree(GetProcessHeap(), 0, This->shader_priv);
4499 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
4503 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, const struct wined3d_gl_info *gl_info,
4504 struct shader_caps *pCaps)
4506 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
4507 * then overwrite the shader specific ones
4509 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
4511 if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
4512 if(GL_SUPPORT(NV_VERTEX_PROGRAM3))
4514 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4515 TRACE_(d3d_caps)("Hardware vertex shader version 3.0 enabled (NV_VERTEX_PROGRAM3)\n");
4517 else if(GL_LIMITS(vshader_constantsF) >= 256)
4519 /* Shader Model 2.0 requires at least 256 vertex shader constants */
4520 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4521 TRACE_(d3d_caps)("Hardware vertex shader version 2.0 enabled (ARB_PROGRAM)\n");
4525 pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
4526 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
4528 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
4531 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
4532 if(GL_SUPPORT(NV_FRAGMENT_PROGRAM2))
4534 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4535 TRACE_(d3d_caps)("Hardware pixel shader version 3.0 enabled (NV_FRAGMENT_PROGRAM2)\n");
4537 else if(GL_LIMITS(pshader_constantsF) >= 32)
4539 /* Shader Model 2.0 requires at least 32 pixel shader constants */
4540 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4541 TRACE_(d3d_caps)("Hardware pixel shader version 2.0 enabled (ARB_PROGRAM)\n");
4545 pCaps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
4546 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
4548 pCaps->PixelShader1xMaxValue = 8.0f;
4549 pCaps->MaxPixelShaderConst = GL_LIMITS(pshader_constantsF);
4552 pCaps->VSClipping = use_nv_clip(gl_info);
4555 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
4557 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4559 TRACE("Checking support for color_fixup:\n");
4560 dump_color_fixup_desc(fixup);
4563 /* We support everything except YUV conversions. */
4564 if (!is_yuv_fixup(fixup))
4570 TRACE("[FAILED]\n");
4574 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction *ins) {
4576 char write_mask[20], regstr[50];
4577 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4578 BOOL is_color = FALSE;
4579 const struct wined3d_shader_dst_param *dst;
4581 if (!ins->dst_count) return;
4585 if(shift == 0) return; /* Saturate alone is handled by the instructions */
4587 shader_arb_get_write_mask(ins, dst, write_mask);
4588 shader_arb_get_register_name(ins, &dst->reg, regstr, &is_color);
4590 /* Generate a line that does the output modifier computation
4591 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
4592 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
4594 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins),
4595 regstr, write_mask, regstr, shift_tab[shift]);
4598 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4600 /* WINED3DSIH_ABS */ shader_hw_map2gl,
4601 /* WINED3DSIH_ADD */ shader_hw_map2gl,
4602 /* WINED3DSIH_BEM */ pshader_hw_bem,
4603 /* WINED3DSIH_BREAK */ shader_hw_break,
4604 /* WINED3DSIH_BREAKC */ shader_hw_breakc,
4605 /* WINED3DSIH_BREAKP */ NULL,
4606 /* WINED3DSIH_CALL */ shader_hw_call,
4607 /* WINED3DSIH_CALLNZ */ NULL,
4608 /* WINED3DSIH_CMP */ pshader_hw_cmp,
4609 /* WINED3DSIH_CND */ pshader_hw_cnd,
4610 /* WINED3DSIH_CRS */ shader_hw_map2gl,
4611 /* WINED3DSIH_DCL */ NULL,
4612 /* WINED3DSIH_DEF */ NULL,
4613 /* WINED3DSIH_DEFB */ NULL,
4614 /* WINED3DSIH_DEFI */ NULL,
4615 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
4616 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
4617 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
4618 /* WINED3DSIH_DST */ shader_hw_map2gl,
4619 /* WINED3DSIH_DSX */ shader_hw_map2gl,
4620 /* WINED3DSIH_DSY */ shader_hw_dsy,
4621 /* WINED3DSIH_ELSE */ shader_hw_else,
4622 /* WINED3DSIH_ENDIF */ shader_hw_endif,
4623 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop,
4624 /* WINED3DSIH_ENDREP */ shader_hw_endrep,
4625 /* WINED3DSIH_EXP */ shader_hw_scalar_op,
4626 /* WINED3DSIH_EXPP */ shader_hw_scalar_op,
4627 /* WINED3DSIH_FRC */ shader_hw_map2gl,
4628 /* WINED3DSIH_IF */ NULL /* Hardcoded into the shader */,
4629 /* WINED3DSIH_IFC */ shader_hw_ifc,
4630 /* WINED3DSIH_LABEL */ shader_hw_label,
4631 /* WINED3DSIH_LIT */ shader_hw_map2gl,
4632 /* WINED3DSIH_LOG */ shader_hw_log_pow,
4633 /* WINED3DSIH_LOGP */ shader_hw_log_pow,
4634 /* WINED3DSIH_LOOP */ shader_hw_loop,
4635 /* WINED3DSIH_LRP */ shader_hw_lrp,
4636 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
4637 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
4638 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
4639 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
4640 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
4641 /* WINED3DSIH_MAD */ shader_hw_map2gl,
4642 /* WINED3DSIH_MAX */ shader_hw_map2gl,
4643 /* WINED3DSIH_MIN */ shader_hw_map2gl,
4644 /* WINED3DSIH_MOV */ shader_hw_mov,
4645 /* WINED3DSIH_MOVA */ shader_hw_mov,
4646 /* WINED3DSIH_MUL */ shader_hw_map2gl,
4647 /* WINED3DSIH_NOP */ shader_hw_nop,
4648 /* WINED3DSIH_NRM */ shader_hw_nrm,
4649 /* WINED3DSIH_PHASE */ NULL,
4650 /* WINED3DSIH_POW */ shader_hw_log_pow,
4651 /* WINED3DSIH_RCP */ shader_hw_scalar_op,
4652 /* WINED3DSIH_REP */ shader_hw_rep,
4653 /* WINED3DSIH_RET */ shader_hw_ret,
4654 /* WINED3DSIH_RSQ */ shader_hw_scalar_op,
4655 /* WINED3DSIH_SETP */ NULL,
4656 /* WINED3DSIH_SGE */ shader_hw_map2gl,
4657 /* WINED3DSIH_SGN */ shader_hw_sgn,
4658 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
4659 /* WINED3DSIH_SLT */ shader_hw_map2gl,
4660 /* WINED3DSIH_SUB */ shader_hw_map2gl,
4661 /* WINED3DSIH_TEX */ pshader_hw_tex,
4662 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
4663 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
4664 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
4665 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
4666 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
4667 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
4668 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
4669 /* WINED3DSIH_TEXLDD */ shader_hw_texldd,
4670 /* WINED3DSIH_TEXLDL */ shader_hw_texldl,
4671 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
4672 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
4673 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
4674 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
4675 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
4676 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
4677 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
4678 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
4679 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
4680 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
4681 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
4682 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
4685 static inline BOOL get_bool_const(const struct wined3d_shader_instruction *ins, IWineD3DBaseShaderImpl *This, DWORD idx)
4687 BOOL vshader = shader_is_vshader_version(This->baseShader.reg_maps.shader_version.type);
4689 WORD flag = (1 << idx);
4690 const local_constant *constant;
4691 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
4693 if(This->baseShader.reg_maps.local_bool_consts & flag)
4695 /* What good is a if(bool) with a hardcoded local constant? I don't know, but handle it */
4696 LIST_FOR_EACH_ENTRY(constant, &This->baseShader.constantsB, local_constant, entry)
4698 if (constant->idx == idx)
4700 return constant->value[0];
4703 ERR("Local constant not found\n");
4708 if(vshader) bools = priv->cur_vs_args->boolclip.bools;
4709 else bools = priv->cur_ps_args->bools;
4710 return bools & flag;
4714 static void get_loop_control_const(const struct wined3d_shader_instruction *ins,
4715 IWineD3DBaseShaderImpl *This, UINT idx, struct wined3d_shader_loop_control *loop_control)
4717 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
4719 /* Integer constants can either be a local constant, or they can be stored in the shader
4720 * type specific compile args. */
4721 if (This->baseShader.reg_maps.local_int_consts & (1 << idx))
4723 const local_constant *constant;
4725 LIST_FOR_EACH_ENTRY(constant, &This->baseShader.constantsI, local_constant, entry)
4727 if (constant->idx == idx)
4729 loop_control->count = constant->value[0];
4730 loop_control->start = constant->value[1];
4731 /* Step is signed. */
4732 loop_control->step = (int)constant->value[2];
4736 /* If this happens the flag was set incorrectly */
4737 ERR("Local constant not found\n");
4738 loop_control->count = 0;
4739 loop_control->start = 0;
4740 loop_control->step = 0;
4744 switch (This->baseShader.reg_maps.shader_version.type)
4746 case WINED3D_SHADER_TYPE_VERTEX:
4747 /* Count and aL start value are unsigned */
4748 loop_control->count = priv->cur_vs_args->loop_ctrl[idx][0];
4749 loop_control->start = priv->cur_vs_args->loop_ctrl[idx][1];
4750 /* Step is signed. */
4751 loop_control->step = ((char)priv->cur_vs_args->loop_ctrl[idx][2]);
4754 case WINED3D_SHADER_TYPE_PIXEL:
4755 loop_control->count = priv->cur_ps_args->loop_ctrl[idx][0];
4756 loop_control->start = priv->cur_ps_args->loop_ctrl[idx][1];
4757 loop_control->step = ((char)priv->cur_ps_args->loop_ctrl[idx][2]);
4761 FIXME("Unhandled shader type %#x.\n", This->baseShader.reg_maps.shader_version.type);
4766 static void record_instruction(struct list *list, const struct wined3d_shader_instruction *ins)
4769 struct wined3d_shader_dst_param *dst_param = NULL;
4770 struct wined3d_shader_src_param *src_param = NULL, *rel_addr = NULL;
4771 struct recorded_instruction *rec = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rec));
4774 ERR("Out of memory\n");
4779 dst_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param));
4780 if(!dst_param) goto free;
4781 *dst_param = *ins->dst;
4782 if(ins->dst->reg.rel_addr)
4784 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param->reg.rel_addr));
4785 if(!rel_addr) goto free;
4786 *rel_addr = *ins->dst->reg.rel_addr;
4787 dst_param->reg.rel_addr = rel_addr;
4789 rec->ins.dst = dst_param;
4791 src_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*src_param) * ins->src_count);
4792 if(!src_param) goto free;
4793 for(i = 0; i < ins->src_count; i++)
4795 src_param[i] = ins->src[i];
4796 if(ins->src[i].reg.rel_addr)
4798 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr));
4799 if(!rel_addr) goto free;
4800 *rel_addr = *ins->src[i].reg.rel_addr;
4801 src_param[i].reg.rel_addr = rel_addr;
4804 rec->ins.src = src_param;
4805 list_add_tail(list, &rec->entry);
4809 ERR("Out of memory\n");
4812 HeapFree(GetProcessHeap(), 0, (void *) dst_param->reg.rel_addr);
4813 HeapFree(GetProcessHeap(), 0, dst_param);
4817 for(i = 0; i < ins->src_count; i++)
4819 HeapFree(GetProcessHeap(), 0, (void *) src_param[i].reg.rel_addr);
4821 HeapFree(GetProcessHeap(), 0, src_param);
4823 HeapFree(GetProcessHeap(), 0, rec);
4826 static void free_recorded_instruction(struct list *list)
4828 struct recorded_instruction *rec_ins, *entry2;
4831 LIST_FOR_EACH_ENTRY_SAFE(rec_ins, entry2, list, struct recorded_instruction, entry)
4833 list_remove(&rec_ins->entry);
4834 if(rec_ins->ins.dst)
4836 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.dst->reg.rel_addr);
4837 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.dst);
4839 if(rec_ins->ins.src)
4841 for(i = 0; i < rec_ins->ins.src_count; i++)
4843 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.src[i].reg.rel_addr);
4845 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.src);
4847 HeapFree(GetProcessHeap(), 0, rec_ins);
4851 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction *ins) {
4852 SHADER_HANDLER hw_fct;
4853 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
4854 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
4855 struct control_frame *control_frame;
4856 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4859 if(ins->handler_idx == WINED3DSIH_LOOP || ins->handler_idx == WINED3DSIH_REP)
4861 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
4862 list_add_head(&priv->control_frames, &control_frame->entry);
4864 if(ins->handler_idx == WINED3DSIH_LOOP) control_frame->type = LOOP;
4865 if(ins->handler_idx == WINED3DSIH_REP) control_frame->type = REP;
4867 if(priv->target_version >= NV2)
4869 control_frame->loop_no = priv->num_loops++;
4874 /* Don't bother recording when we're in a not used if branch */
4880 if(!priv->recording)
4882 list_init(&priv->record);
4883 priv->recording = TRUE;
4884 control_frame->outer_loop = TRUE;
4885 get_loop_control_const(ins, This, ins->src[0].reg.idx, &control_frame->loop_control);
4886 return; /* Instruction is handled */
4888 /* Record this loop in the outer loop's recording */
4891 else if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
4893 if(priv->target_version >= NV2)
4895 /* Nothing to do. The control frame is popped after the HW instr handler */
4899 struct list *e = list_head(&priv->control_frames);
4900 control_frame = LIST_ENTRY(e, struct control_frame, entry);
4901 list_remove(&control_frame->entry);
4903 if(control_frame->outer_loop)
4905 int iteration, aL = 0;
4908 /* Turn off recording before playback */
4909 priv->recording = FALSE;
4911 /* Move the recorded instructions to a separate list and get them out of the private data
4912 * structure. If there are nested loops, the shader_arb_handle_instruction below will
4913 * be recorded again, thus priv->record might be overwritten
4916 list_move_tail(©, &priv->record);
4917 list_init(&priv->record);
4919 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
4921 shader_addline(buffer, "#unrolling loop: %u iterations, aL=%u, inc %d\n",
4922 control_frame->loop_control.count, control_frame->loop_control.start,
4923 control_frame->loop_control.step);
4924 aL = control_frame->loop_control.start;
4928 shader_addline(buffer, "#unrolling rep: %u iterations\n", control_frame->loop_control.count);
4931 for (iteration = 0; iteration < control_frame->loop_control.count; ++iteration)
4933 struct recorded_instruction *rec_ins;
4934 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
4937 shader_addline(buffer, "#Iteration %d, aL=%d\n", iteration, aL);
4941 shader_addline(buffer, "#Iteration %d\n", iteration);
4944 LIST_FOR_EACH_ENTRY(rec_ins, ©, struct recorded_instruction, entry)
4946 shader_arb_handle_instruction(&rec_ins->ins);
4949 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
4951 aL += control_frame->loop_control.step;
4954 shader_addline(buffer, "#end loop/rep\n");
4956 free_recorded_instruction(©);
4957 HeapFree(GetProcessHeap(), 0, control_frame);
4958 return; /* Instruction is handled */
4962 /* This is a nested loop. Proceed to the normal recording function */
4963 HeapFree(GetProcessHeap(), 0, control_frame);
4970 record_instruction(&priv->record, ins);
4975 if(ins->handler_idx == WINED3DSIH_IF)
4977 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
4978 list_add_head(&priv->control_frames, &control_frame->entry);
4979 control_frame->type = IF;
4981 bool_const = get_bool_const(ins, This, ins->src[0].reg.idx);
4982 if(ins->src[0].modifiers == WINED3DSPSM_NOT) bool_const = !bool_const;
4983 if(!priv->muted && bool_const == FALSE)
4985 shader_addline(buffer, "#if(FALSE){\n");
4987 control_frame->muting = TRUE;
4989 else shader_addline(buffer, "#if(TRUE) {\n");
4991 return; /* Instruction is handled */
4993 else if(ins->handler_idx == WINED3DSIH_IFC)
4995 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
4996 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
4997 control_frame->type = IFC;
4998 control_frame->ifc_no = priv->num_ifcs++;
4999 list_add_head(&priv->control_frames, &control_frame->entry);
5001 else if(ins->handler_idx == WINED3DSIH_ELSE)
5003 struct list *e = list_head(&priv->control_frames);
5004 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5006 if(control_frame->type == IF)
5008 shader_addline(buffer, "#} else {\n");
5009 if(!priv->muted && !control_frame->muting)
5012 control_frame->muting = TRUE;
5014 else if(control_frame->muting) priv->muted = FALSE;
5015 return; /* Instruction is handled. */
5017 /* In case of an ifc, generate a HW shader instruction */
5019 else if(ins->handler_idx == WINED3DSIH_ENDIF)
5021 struct list *e = list_head(&priv->control_frames);
5022 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5024 if(control_frame->type == IF)
5026 shader_addline(buffer, "#} endif\n");
5027 if(control_frame->muting) priv->muted = FALSE;
5028 list_remove(&control_frame->entry);
5029 HeapFree(GetProcessHeap(), 0, control_frame);
5030 return; /* Instruction is handled */
5034 if(priv->muted) return;
5036 /* Select handler */
5037 hw_fct = shader_arb_instruction_handler_table[ins->handler_idx];
5039 /* Unhandled opcode */
5042 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5047 if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
5049 struct list *e = list_head(&priv->control_frames);
5050 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5051 list_remove(&control_frame->entry);
5052 HeapFree(GetProcessHeap(), 0, control_frame);
5055 else if(ins->handler_idx == WINED3DSIH_ENDIF)
5057 /* Non-ifc ENDIFs don't reach that place because of the return in the if block above */
5058 struct list *e = list_head(&priv->control_frames);
5059 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5060 list_remove(&control_frame->entry);
5061 HeapFree(GetProcessHeap(), 0, control_frame);
5065 shader_arb_add_instruction_modifiers(ins);
5068 const shader_backend_t arb_program_shader_backend = {
5069 shader_arb_handle_instruction,
5071 shader_arb_select_depth_blt,
5072 shader_arb_deselect_depth_blt,
5073 shader_arb_update_float_vertex_constants,
5074 shader_arb_update_float_pixel_constants,
5075 shader_arb_load_constants,
5076 shader_arb_load_np2fixup_constants,
5080 shader_arb_dirty_const,
5081 shader_arb_get_caps,
5082 shader_arb_color_fixup_supported,
5085 /* ARB_fragment_program fixed function pipeline replacement definitions */
5086 #define ARB_FFP_CONST_TFACTOR 0
5087 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
5088 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
5089 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
5090 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
5092 struct arbfp_ffp_desc
5094 struct ffp_frag_desc parent;
5096 unsigned int num_textures_used;
5099 /* Context activation is done by the caller. */
5100 static void arbfp_enable(IWineD3DDevice *iface, BOOL enable) {
5103 glEnable(GL_FRAGMENT_PROGRAM_ARB);
5104 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
5106 glDisable(GL_FRAGMENT_PROGRAM_ARB);
5107 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
5112 static HRESULT arbfp_alloc(IWineD3DDevice *iface) {
5113 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
5114 struct shader_arb_priv *priv;
5115 /* Share private data between the shader backend and the pipeline replacement, if both
5116 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
5117 * if no pixel shader is bound or not
5119 if(This->shader_backend == &arb_program_shader_backend) {
5120 This->fragment_priv = This->shader_priv;
5122 This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
5123 if(!This->fragment_priv) return E_OUTOFMEMORY;
5125 priv = This->fragment_priv;
5126 if (wine_rb_init(&priv->fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
5128 ERR("Failed to initialize rbtree.\n");
5129 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
5130 return E_OUTOFMEMORY;
5132 priv->use_arbfp_fixed_func = TRUE;
5136 /* Context activation is done by the caller. */
5137 static void arbfp_free_ffpshader(struct wine_rb_entry *entry, void *context)
5139 const struct wined3d_gl_info *gl_info = context;
5140 struct arbfp_ffp_desc *entry_arb = WINE_RB_ENTRY_VALUE(entry, struct arbfp_ffp_desc, parent.entry);
5143 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
5144 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
5145 HeapFree(GetProcessHeap(), 0, entry_arb);
5149 /* Context activation is done by the caller. */
5150 static void arbfp_free(IWineD3DDevice *iface) {
5151 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
5152 struct shader_arb_priv *priv = This->fragment_priv;
5154 wine_rb_destroy(&priv->fragment_shaders, arbfp_free_ffpshader, &This->adapter->gl_info);
5155 priv->use_arbfp_fixed_func = FALSE;
5157 if(This->shader_backend != &arb_program_shader_backend) {
5158 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
5162 static void arbfp_get_caps(WINED3DDEVTYPE devtype, const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
5164 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
5165 WINED3DTEXOPCAPS_SELECTARG1 |
5166 WINED3DTEXOPCAPS_SELECTARG2 |
5167 WINED3DTEXOPCAPS_MODULATE4X |
5168 WINED3DTEXOPCAPS_MODULATE2X |
5169 WINED3DTEXOPCAPS_MODULATE |
5170 WINED3DTEXOPCAPS_ADDSIGNED2X |
5171 WINED3DTEXOPCAPS_ADDSIGNED |
5172 WINED3DTEXOPCAPS_ADD |
5173 WINED3DTEXOPCAPS_SUBTRACT |
5174 WINED3DTEXOPCAPS_ADDSMOOTH |
5175 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
5176 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
5177 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
5178 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
5179 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
5180 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
5181 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5182 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
5183 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
5184 WINED3DTEXOPCAPS_DOTPRODUCT3 |
5185 WINED3DTEXOPCAPS_MULTIPLYADD |
5186 WINED3DTEXOPCAPS_LERP |
5187 WINED3DTEXOPCAPS_BUMPENVMAP |
5188 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
5190 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
5192 caps->MaxTextureBlendStages = 8;
5193 caps->MaxSimultaneousTextures = min(GL_LIMITS(fragment_samplers), 8);
5195 caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
5197 #undef GLINFO_LOCATION
5199 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
5200 static void state_texfactor_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5203 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5205 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5206 * application provided constants
5208 if(device->shader_backend == &arb_program_shader_backend) {
5209 if (use_ps(stateblock)) return;
5211 device = stateblock->wineD3DDevice;
5212 context->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
5213 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
5216 D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
5217 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
5218 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
5222 static void state_arb_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5225 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5227 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5228 * application provided constants
5230 if(device->shader_backend == &arb_program_shader_backend) {
5231 if (use_ps(stateblock)) return;
5233 device = stateblock->wineD3DDevice;
5234 context->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
5235 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
5238 if(stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
5239 /* The specular color has no alpha */
5240 col[0] = 1.0f; col[1] = 1.0f;
5241 col[2] = 1.0f; col[3] = 0.0f;
5243 col[0] = 0.0f; col[1] = 0.0f;
5244 col[2] = 0.0f; col[3] = 0.0f;
5246 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
5247 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
5250 static void set_bumpmat_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5252 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5253 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5256 if (use_ps(stateblock))
5259 && (((IWineD3DPixelShaderImpl *)stateblock->pixelShader)->baseShader.reg_maps.bumpmat & (1 << stage)))
5261 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
5264 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
5265 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
5269 if(device->shader_backend == &arb_program_shader_backend) {
5270 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5273 } else if(device->shader_backend == &arb_program_shader_backend) {
5274 context->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
5275 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
5278 mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
5279 mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
5280 mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
5281 mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
5283 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
5284 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
5287 static void tex_bumpenvlum_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5289 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5290 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5293 if (use_ps(stateblock))
5296 && (((IWineD3DPixelShaderImpl *)stateblock->pixelShader)->baseShader.reg_maps.luminanceparams & (1 << stage)))
5298 /* The pixel shader has to know the luminance offset. Do a constants update if it
5299 * isn't scheduled anyway
5301 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
5302 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
5306 if(device->shader_backend == &arb_program_shader_backend) {
5307 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5310 } else if(device->shader_backend == &arb_program_shader_backend) {
5311 context->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
5312 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
5315 param[0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLSCALE]);
5316 param[1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET]);
5320 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
5321 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
5324 static const char *get_argreg(struct wined3d_shader_buffer *buffer, DWORD argnum, unsigned int stage, DWORD arg)
5328 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
5330 switch(arg & WINED3DTA_SELECTMASK) {
5331 case WINED3DTA_DIFFUSE:
5332 ret = "fragment.color.primary"; break;
5334 case WINED3DTA_CURRENT:
5335 if(stage == 0) ret = "fragment.color.primary";
5339 case WINED3DTA_TEXTURE:
5341 case 0: ret = "tex0"; break;
5342 case 1: ret = "tex1"; break;
5343 case 2: ret = "tex2"; break;
5344 case 3: ret = "tex3"; break;
5345 case 4: ret = "tex4"; break;
5346 case 5: ret = "tex5"; break;
5347 case 6: ret = "tex6"; break;
5348 case 7: ret = "tex7"; break;
5349 default: ret = "unknown texture";
5353 case WINED3DTA_TFACTOR:
5354 ret = "tfactor"; break;
5356 case WINED3DTA_SPECULAR:
5357 ret = "fragment.color.secondary"; break;
5359 case WINED3DTA_TEMP:
5360 ret = "tempreg"; break;
5362 case WINED3DTA_CONSTANT:
5363 FIXME("Implement perstage constants\n");
5365 case 0: ret = "const0"; break;
5366 case 1: ret = "const1"; break;
5367 case 2: ret = "const2"; break;
5368 case 3: ret = "const3"; break;
5369 case 4: ret = "const4"; break;
5370 case 5: ret = "const5"; break;
5371 case 6: ret = "const6"; break;
5372 case 7: ret = "const7"; break;
5373 default: ret = "unknown constant";
5381 if(arg & WINED3DTA_COMPLEMENT) {
5382 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
5383 if(argnum == 0) ret = "arg0";
5384 if(argnum == 1) ret = "arg1";
5385 if(argnum == 2) ret = "arg2";
5387 if(arg & WINED3DTA_ALPHAREPLICATE) {
5388 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
5389 if(argnum == 0) ret = "arg0";
5390 if(argnum == 1) ret = "arg1";
5391 if(argnum == 2) ret = "arg2";
5396 static void gen_ffp_instr(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5397 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5399 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5400 unsigned int mul = 1;
5401 BOOL mul_final_dest = FALSE;
5403 if(color && alpha) dstmask = "";
5404 else if(color) dstmask = ".xyz";
5405 else dstmask = ".w";
5407 if(dst == tempreg) dstreg = "tempreg";
5408 else dstreg = "ret";
5410 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
5411 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
5412 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
5415 case WINED3DTOP_DISABLE:
5416 if(stage == 0) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
5419 case WINED3DTOP_SELECTARG2:
5421 case WINED3DTOP_SELECTARG1:
5422 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
5425 case WINED3DTOP_MODULATE4X:
5427 case WINED3DTOP_MODULATE2X:
5429 if(strcmp(dstreg, "result.color") == 0) {
5431 mul_final_dest = TRUE;
5433 case WINED3DTOP_MODULATE:
5434 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5437 case WINED3DTOP_ADDSIGNED2X:
5439 if(strcmp(dstreg, "result.color") == 0) {
5441 mul_final_dest = TRUE;
5443 case WINED3DTOP_ADDSIGNED:
5444 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
5446 case WINED3DTOP_ADD:
5447 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5450 case WINED3DTOP_SUBTRACT:
5451 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5454 case WINED3DTOP_ADDSMOOTH:
5455 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
5456 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
5459 case WINED3DTOP_BLENDCURRENTALPHA:
5460 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
5461 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5463 case WINED3DTOP_BLENDFACTORALPHA:
5464 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
5465 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5467 case WINED3DTOP_BLENDTEXTUREALPHA:
5468 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
5469 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5471 case WINED3DTOP_BLENDDIFFUSEALPHA:
5472 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5473 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5476 case WINED3DTOP_BLENDTEXTUREALPHAPM:
5477 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
5478 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
5479 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
5482 /* D3DTOP_PREMODULATE ???? */
5484 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
5485 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
5486 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
5488 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
5489 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
5491 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
5492 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
5493 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
5495 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
5496 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
5499 case WINED3DTOP_DOTPRODUCT3:
5501 if(strcmp(dstreg, "result.color") == 0) {
5503 mul_final_dest = TRUE;
5505 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
5506 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
5507 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
5510 case WINED3DTOP_MULTIPLYADD:
5511 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
5514 case WINED3DTOP_LERP:
5515 /* The msdn is not quite right here */
5516 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5519 case WINED3DTOP_BUMPENVMAP:
5520 case WINED3DTOP_BUMPENVMAPLUMINANCE:
5521 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
5525 FIXME("Unhandled texture op %08x\n", op);
5529 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
5530 } else if(mul == 4) {
5531 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
5535 /* The stateblock is passed for GLINFO_LOCATION */
5536 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, IWineD3DStateBlockImpl *stateblock)
5539 struct wined3d_shader_buffer buffer;
5540 BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5541 BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5542 BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5543 const char *textype;
5544 const char *instr, *sat;
5545 char colorcor_dst[8];
5547 DWORD arg0, arg1, arg2;
5548 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5550 const char *final_combiner_src = "ret";
5553 /* Find out which textures are read */
5554 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5555 if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
5556 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5557 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5558 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5559 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5560 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5561 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5563 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
5564 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
5565 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
5566 bump_used[stage] = TRUE;
5567 tex_read[stage] = TRUE;
5569 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
5570 bump_used[stage] = TRUE;
5571 tex_read[stage] = TRUE;
5572 luminance_used[stage] = TRUE;
5573 } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
5574 tfactor_used = TRUE;
5577 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
5578 tfactor_used = TRUE;
5581 if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
5582 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
5583 tempreg_used = TRUE;
5586 if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
5587 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5588 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5589 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5590 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5591 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5592 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5594 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
5595 tempreg_used = TRUE;
5597 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
5598 tfactor_used = TRUE;
5603 if (!shader_buffer_init(&buffer))
5605 ERR("Failed to initialize shader buffer.\n");
5609 shader_addline(&buffer, "!!ARBfp1.0\n");
5611 switch(settings->fog) {
5612 case FOG_OFF: break;
5613 case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
5614 case FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
5615 case FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
5616 default: FIXME("Unexpected fog setting %d\n", settings->fog);
5619 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
5620 shader_addline(&buffer, "TEMP TMP;\n");
5621 shader_addline(&buffer, "TEMP ret;\n");
5622 if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
5623 shader_addline(&buffer, "TEMP arg0;\n");
5624 shader_addline(&buffer, "TEMP arg1;\n");
5625 shader_addline(&buffer, "TEMP arg2;\n");
5626 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5627 if(!tex_read[stage]) continue;
5628 shader_addline(&buffer, "TEMP tex%u;\n", stage);
5629 if(!bump_used[stage]) continue;
5630 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
5631 if(!luminance_used[stage]) continue;
5632 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
5635 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
5637 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
5639 if(settings->sRGB_write) {
5640 shader_addline(&buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
5641 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
5642 shader_addline(&buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
5643 srgb_sub_high, 0.0, 0.0, 0.0);
5646 if(ffp_clip_emul(stateblock) && settings->emul_clipplanes) shader_addline(&buffer, "KIL fragment.texcoord[7];\n");
5648 /* Generate texture sampling instructions) */
5649 for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
5650 if(!tex_read[stage]) continue;
5652 switch(settings->op[stage].tex_type) {
5653 case tex_1d: textype = "1D"; break;
5654 case tex_2d: textype = "2D"; break;
5655 case tex_3d: textype = "3D"; break;
5656 case tex_cube: textype = "CUBE"; break;
5657 case tex_rect: textype = "RECT"; break;
5658 default: textype = "unexpected_textype"; break;
5661 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
5662 settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
5668 if(settings->op[stage].projected == proj_none) {
5670 } else if(settings->op[stage].projected == proj_count4 ||
5671 settings->op[stage].projected == proj_count3) {
5674 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5679 (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
5680 settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
5681 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
5682 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
5683 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
5684 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
5686 /* with projective textures, texbem only divides the static texture coord, not the displacement,
5687 * so multiply the displacement with the dividing parameter before passing it to TXP
5689 if (settings->op[stage].projected != proj_none) {
5690 if(settings->op[stage].projected == proj_count4) {
5691 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
5692 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
5694 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
5695 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
5698 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
5701 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
5702 instr, sat, stage, stage, textype);
5703 if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
5704 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
5705 stage - 1, stage - 1, stage - 1);
5706 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
5708 } else if(settings->op[stage].projected == proj_count3) {
5709 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
5710 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
5711 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
5712 instr, sat, stage, stage, textype);
5714 shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
5715 instr, sat, stage, stage, stage, textype);
5718 sprintf(colorcor_dst, "tex%u", stage);
5719 gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
5720 settings->op[stage].color_fixup);
5723 /* Generate the main shader */
5724 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5725 if(settings->op[stage].cop == WINED3DTOP_DISABLE) {
5727 final_combiner_src = "fragment.color.primary";
5732 if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
5733 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
5734 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5735 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
5736 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
5737 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5738 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
5739 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
5740 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5741 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
5742 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
5743 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5745 op_equal = settings->op[stage].aop == settings->op[stage].cop &&
5746 settings->op[stage].carg0 == settings->op[stage].aarg0 &&
5747 settings->op[stage].carg1 == settings->op[stage].aarg1 &&
5748 settings->op[stage].carg2 == settings->op[stage].aarg2;
5751 if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
5752 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5753 settings->op[stage].cop, settings->op[stage].carg0,
5754 settings->op[stage].carg1, settings->op[stage].carg2);
5756 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
5758 } else if(op_equal) {
5759 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5760 settings->op[stage].cop, settings->op[stage].carg0,
5761 settings->op[stage].carg1, settings->op[stage].carg2);
5763 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5764 settings->op[stage].cop, settings->op[stage].carg0,
5765 settings->op[stage].carg1, settings->op[stage].carg2);
5766 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5767 settings->op[stage].aop, settings->op[stage].aarg0,
5768 settings->op[stage].aarg1, settings->op[stage].aarg2);
5772 if(settings->sRGB_write) {
5773 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
5774 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE);
5776 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
5780 shader_addline(&buffer, "END\n");
5782 /* Generate the shader */
5783 GL_EXTCALL(glGenProgramsARB(1, &ret));
5784 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
5785 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
5786 strlen(buffer.buffer), buffer.buffer));
5787 checkGLcall("glProgramStringARB()");
5789 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
5792 FIXME("Fragment program error at position %d: %s\n", pos,
5793 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
5799 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
5800 checkGLcall("glGetProgramivARB()");
5801 if (!native) WARN("Program exceeds native resource limits.\n");
5804 shader_buffer_free(&buffer);
5808 static void fragment_prog_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5810 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5811 struct shader_arb_priv *priv = device->fragment_priv;
5812 BOOL use_pshader = use_ps(stateblock);
5813 BOOL use_vshader = use_vs(stateblock);
5814 struct ffp_frag_settings settings;
5815 const struct arbfp_ffp_desc *desc;
5818 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
5820 if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
5821 if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
5822 /* Reload fixed function constants since they collide with the pixel shader constants */
5823 for(i = 0; i < MAX_TEXTURES; i++) {
5824 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
5826 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
5827 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
5828 } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
5829 device->shader_backend->shader_select(context, use_pshader, use_vshader);
5835 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
5836 gen_ffp_frag_op(stateblock, &settings, FALSE);
5837 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
5839 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
5842 ERR("Out of memory\n");
5845 new_desc->num_textures_used = 0;
5846 for(i = 0; i < GL_LIMITS(texture_stages); i++) {
5847 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
5848 new_desc->num_textures_used = i;
5851 memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
5852 new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
5853 add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
5854 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
5858 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
5859 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
5862 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
5863 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
5864 priv->current_fprogram_id = desc->shader;
5866 if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
5867 /* Reload fixed function constants since they collide with the pixel shader constants */
5868 for(i = 0; i < MAX_TEXTURES; i++) {
5869 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
5871 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
5872 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
5874 context->last_was_pshader = FALSE;
5876 context->last_was_pshader = TRUE;
5879 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
5880 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
5881 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
5882 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
5883 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
5885 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
5888 if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
5889 device->shader_backend->shader_select(context, use_pshader, use_vshader);
5891 if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
5892 device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
5896 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
5900 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
5901 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
5902 * state table, so we need to handle that with a forwarding function. The other invisible side effect
5903 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
5904 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
5906 static void state_arbfp_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5908 enum fogsource new_source;
5910 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
5912 if(!isStateDirty(context, STATE_PIXELSHADER)) {
5913 fragment_prog_arbfp(state, stateblock, context);
5916 if(!stateblock->renderState[WINED3DRS_FOGENABLE]) return;
5918 if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
5919 if(use_vs(stateblock)) {
5920 new_source = FOGSOURCE_VS;
5922 if(stateblock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || context->last_was_rhw) {
5923 new_source = FOGSOURCE_COORD;
5925 new_source = FOGSOURCE_FFP;
5929 new_source = FOGSOURCE_FFP;
5931 if(new_source != context->fog_source) {
5932 context->fog_source = new_source;
5933 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART), stateblock, context);
5937 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, struct wined3d_context *context)
5939 if(!isStateDirty(context, STATE_PIXELSHADER)) {
5940 fragment_prog_arbfp(state, stateblock, context);
5944 #undef GLINFO_LOCATION
5946 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
5947 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR), state_texfactor_arbfp }, WINED3D_GL_EXT_NONE },
5948 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5949 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5950 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5951 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5952 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5953 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5954 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5955 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5956 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5957 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5958 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5959 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5960 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5961 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5962 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5963 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5964 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5965 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5966 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5967 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5968 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5969 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5970 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5971 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5972 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5973 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5974 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5975 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5976 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5977 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5978 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5979 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5980 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5981 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5982 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5983 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5984 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5985 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5986 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5987 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5988 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5989 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5990 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5991 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5992 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5993 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5994 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5995 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5996 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5997 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5998 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5999 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6000 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6001 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6002 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6003 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6004 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6005 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6006 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6007 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6008 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6009 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6010 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6011 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6012 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6013 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6014 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6015 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6016 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6017 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6018 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6019 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6020 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6021 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6022 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6023 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6024 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6025 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6026 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6027 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6028 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6029 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6030 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6031 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6032 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6033 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6034 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6035 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6036 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6037 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6038 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6039 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6040 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6041 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6042 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6043 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6044 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6045 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6046 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6047 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6048 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6049 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6050 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6051 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6052 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6053 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6054 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6055 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6056 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6057 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6058 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6059 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6060 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6061 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6062 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6063 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6064 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6065 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6066 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6067 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6068 {STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim }, WINED3D_GL_EXT_NONE },
6069 {STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim }, WINED3D_GL_EXT_NONE },
6070 {STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim }, WINED3D_GL_EXT_NONE },
6071 {STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim }, WINED3D_GL_EXT_NONE },
6072 {STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim }, WINED3D_GL_EXT_NONE },
6073 {STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim }, WINED3D_GL_EXT_NONE },
6074 {STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim }, WINED3D_GL_EXT_NONE },
6075 {STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim }, WINED3D_GL_EXT_NONE },
6076 {STATE_PIXELSHADER, { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6077 {STATE_RENDER(WINED3DRS_FOGENABLE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
6078 {STATE_RENDER(WINED3DRS_FOGTABLEMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
6079 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
6080 {STATE_RENDER(WINED3DRS_FOGSTART), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6081 {STATE_RENDER(WINED3DRS_FOGEND), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6082 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6083 {STATE_RENDER(WINED3DRS_FOGCOLOR), { STATE_RENDER(WINED3DRS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
6084 {STATE_RENDER(WINED3DRS_FOGDENSITY), { STATE_RENDER(WINED3DRS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
6085 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6086 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6087 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6088 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6089 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6090 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6091 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6092 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6093 {STATE_RENDER(WINED3DRS_SPECULARENABLE), { STATE_RENDER(WINED3DRS_SPECULARENABLE), state_arb_specularenable}, WINED3D_GL_EXT_NONE },
6094 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE },
6097 const struct fragment_pipeline arbfp_fragment_pipeline = {
6102 shader_arb_color_fixup_supported,
6103 arbfp_fragmentstate_template,
6104 TRUE /* We can disable projected textures */
6107 #define GLINFO_LOCATION device->adapter->gl_info
6109 struct arbfp_blit_priv {
6110 GLenum yuy2_rect_shader, yuy2_2d_shader;
6111 GLenum uyvy_rect_shader, uyvy_2d_shader;
6112 GLenum yv12_rect_shader, yv12_2d_shader;
6115 static HRESULT arbfp_blit_alloc(IWineD3DDevice *iface) {
6116 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6117 device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
6118 if(!device->blit_priv) {
6119 ERR("Out of memory\n");
6120 return E_OUTOFMEMORY;
6125 /* Context activation is done by the caller. */
6126 static void arbfp_blit_free(IWineD3DDevice *iface) {
6127 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6128 struct arbfp_blit_priv *priv = device->blit_priv;
6131 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
6132 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
6133 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
6134 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
6135 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
6136 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
6137 checkGLcall("Delete yuv programs");
6141 static BOOL gen_planar_yuv_read(struct wined3d_shader_buffer *buffer, enum yuv_fixup yuv_fixup,
6142 GLenum textype, char *luminance)
6145 const char *tex, *texinstr;
6147 if (yuv_fixup == YUV_FIXUP_UYVY) {
6155 case GL_TEXTURE_2D: tex = "2D"; texinstr = "TXP"; break;
6156 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; texinstr = "TEX"; break;
6158 /* This is more tricky than just replacing the texture type - we have to navigate
6159 * properly in the texture to find the correct chroma values
6161 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
6165 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
6166 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
6167 * filtering when we sample the texture.
6169 * These are the rules for reading the chroma:
6175 * So we have to get the sampling x position in non-normalized coordinates in integers
6177 if(textype != GL_TEXTURE_RECTANGLE_ARB) {
6178 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
6179 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
6181 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6183 /* We must not allow filtering between pixel x and x+1, this would mix U and V
6184 * Vertical filtering is ok. However, bear in mind that the pixel center is at
6187 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
6188 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
6190 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
6191 * even and odd pixels respectively
6193 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
6194 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
6196 /* Sample Pixel 1 */
6197 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
6199 /* Put the value into either of the chroma values */
6200 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
6201 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
6202 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
6203 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
6205 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
6206 * the pixel right to the current one. Otherwise, sample the left pixel.
6207 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
6209 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
6210 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
6211 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
6213 /* Put the value into the other chroma */
6214 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
6215 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
6216 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
6217 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
6219 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
6220 * the current one and lerp the two U and V values
6223 /* This gives the correctly filtered luminance value */
6224 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
6229 static BOOL gen_yv12_read(struct wined3d_shader_buffer *buffer, GLenum textype, char *luminance)
6234 case GL_TEXTURE_2D: tex = "2D"; break;
6235 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; break;
6237 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
6241 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
6242 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
6243 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
6244 * pitch of the luminance plane, the packing into the gl texture is a bit
6245 * unfortunate. If the whole texture is interpreted as luminance data it looks
6246 * approximately like this:
6248 * +----------------------------------+----
6260 * +----------------+-----------------+----
6262 * | U even rows | U odd rows |
6264 * +----------------+------------------ -
6266 * | V even rows | V odd rows |
6268 * +----------------+-----------------+----
6272 * So it appears as if there are 4 chroma images, but in fact the odd rows
6273 * in the chroma images are in the same row as the even ones. So its is
6274 * kinda tricky to read
6276 * When reading from rectangle textures, keep in mind that the input y coordinates
6277 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
6279 shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
6280 2.0f / 3.0f, 1.0f / 6.0f, (2.0f / 3.0f) + (1.0f / 6.0f), 1.0f / 3.0f);
6282 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6283 /* the chroma planes have only half the width */
6284 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
6286 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
6287 * the coordinate. Also read the right side of the image when reading odd lines
6289 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
6292 if(textype == GL_TEXTURE_2D) {
6294 shader_addline(buffer, "RCP chroma.w, size.y;\n");
6296 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
6298 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
6299 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
6301 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6302 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6303 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
6304 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6305 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6307 /* clamp, keep the half pixel origin in mind */
6308 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
6309 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
6310 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
6311 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6313 /* Read from [size - size+size/4] */
6314 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
6315 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
6317 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6318 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6319 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
6320 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6321 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
6322 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6324 /* Make sure to read exactly from the pixel center */
6325 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
6326 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
6329 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
6330 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
6331 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6332 shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
6333 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
6335 /* Read the texture, put the result into the output register */
6336 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
6337 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
6339 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
6340 * No need to clamp because we're just reusing the already clamped value from above
6342 if(textype == GL_TEXTURE_2D) {
6343 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
6345 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
6347 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
6348 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
6350 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
6351 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
6352 * values due to filtering
6354 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6355 if(textype == GL_TEXTURE_2D) {
6356 /* Multiply the y coordinate by 2/3 and clamp it */
6357 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
6358 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
6359 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6360 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
6362 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
6363 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
6366 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
6367 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
6368 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
6375 /* Context activation is done by the caller. */
6376 static GLuint gen_yuv_shader(IWineD3DDeviceImpl *device, enum yuv_fixup yuv_fixup, GLenum textype)
6379 struct wined3d_shader_buffer buffer;
6380 char luminance_component;
6381 struct arbfp_blit_priv *priv = device->blit_priv;
6385 if (!shader_buffer_init(&buffer))
6387 ERR("Failed to initialize shader buffer.\n");
6392 GL_EXTCALL(glGenProgramsARB(1, &shader));
6393 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
6394 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
6395 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6398 shader_buffer_free(&buffer);
6402 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
6403 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
6404 * two chroma(U and V) values. Each macropixel has two luminance values, one for
6405 * each single pixel it contains, and one U and one V value shared between both
6408 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
6409 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
6410 * take the format into account when generating the read swizzles
6412 * Reading the Y value is straightforward - just sample the texture. The hardware
6413 * takes care of filtering in the horizontal and vertical direction.
6415 * Reading the U and V values is harder. We have to avoid filtering horizontally,
6416 * because that would mix the U and V values of one pixel or two adjacent pixels.
6417 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
6418 * regardless of the filtering setting. Vertical filtering works automatically
6419 * though - the U and V values of two rows are mixed nicely.
6421 * Appart of avoiding filtering issues, the code has to know which value it just
6422 * read, and where it can find the other one. To determine this, it checks if
6423 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
6425 * Handling horizontal filtering of U and V values requires reading a 2nd pair
6426 * of pixels, extracting U and V and mixing them. This is not implemented yet.
6428 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
6429 * with width / 2. This way one read gives all 3 values, finding U and V is easy
6430 * in an unfiltered situation. Finding the luminance on the other hand requires
6431 * finding out if it is an odd or even pixel. The real drawback of this approach
6432 * is filtering. This would have to be emulated completely in the shader, reading
6433 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
6434 * vertically. Beyond that it would require adjustments to the texture handling
6435 * code to deal with the width scaling
6437 shader_addline(&buffer, "!!ARBfp1.0\n");
6438 shader_addline(&buffer, "TEMP luminance;\n");
6439 shader_addline(&buffer, "TEMP temp;\n");
6440 shader_addline(&buffer, "TEMP chroma;\n");
6441 shader_addline(&buffer, "TEMP texcrd;\n");
6442 shader_addline(&buffer, "TEMP texcrd2;\n");
6443 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
6444 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
6445 shader_addline(&buffer, "PARAM size = program.local[0];\n");
6449 case YUV_FIXUP_UYVY:
6450 case YUV_FIXUP_YUY2:
6451 if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
6453 shader_buffer_free(&buffer);
6458 case YUV_FIXUP_YV12:
6459 if (!gen_yv12_read(&buffer, textype, &luminance_component))
6461 shader_buffer_free(&buffer);
6467 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
6468 shader_buffer_free(&buffer);
6472 /* Calculate the final result. Formula is taken from
6473 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
6474 * ranges from -0.5 to 0.5
6476 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
6478 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
6479 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
6480 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
6481 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
6482 shader_addline(&buffer, "END\n");
6485 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
6486 strlen(buffer.buffer), buffer.buffer));
6487 checkGLcall("glProgramStringARB()");
6489 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
6492 FIXME("Fragment program error at position %d: %s\n", pos,
6493 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
6499 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
6500 checkGLcall("glGetProgramivARB()");
6501 if (!native) WARN("Program exceeds native resource limits.\n");
6504 shader_buffer_free(&buffer);
6509 case YUV_FIXUP_YUY2:
6510 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
6511 else priv->yuy2_2d_shader = shader;
6514 case YUV_FIXUP_UYVY:
6515 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
6516 else priv->uyvy_2d_shader = shader;
6519 case YUV_FIXUP_YV12:
6520 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
6521 else priv->yv12_2d_shader = shader;
6528 /* Context activation is done by the caller. */
6529 static HRESULT arbfp_blit_set(IWineD3DDevice *iface, const struct GlPixelFormatDesc *format_desc,
6530 GLenum textype, UINT width, UINT height)
6533 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6534 float size[4] = {width, height, 1, 1};
6535 struct arbfp_blit_priv *priv = device->blit_priv;
6536 enum yuv_fixup yuv_fixup;
6538 if (!is_yuv_fixup(format_desc->color_fixup))
6541 dump_color_fixup_desc(format_desc->color_fixup);
6542 /* Don't bother setting up a shader for unconverted formats */
6545 checkGLcall("glEnable(textype)");
6550 yuv_fixup = get_yuv_fixup(format_desc->color_fixup);
6554 case YUV_FIXUP_YUY2:
6555 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
6558 case YUV_FIXUP_UYVY:
6559 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
6562 case YUV_FIXUP_YV12:
6563 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
6567 FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup);
6570 checkGLcall("glEnable(textype)");
6575 if (!shader) shader = gen_yuv_shader(device, yuv_fixup, textype);
6578 glEnable(GL_FRAGMENT_PROGRAM_ARB);
6579 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
6580 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
6581 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6582 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
6583 checkGLcall("glProgramLocalParameter4fvARB");
6589 /* Context activation is done by the caller. */
6590 static void arbfp_blit_unset(IWineD3DDevice *iface) {
6591 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6594 glDisable(GL_FRAGMENT_PROGRAM_ARB);
6595 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
6596 glDisable(GL_TEXTURE_2D);
6597 checkGLcall("glDisable(GL_TEXTURE_2D)");
6598 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
6599 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6600 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6602 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
6603 glDisable(GL_TEXTURE_RECTANGLE_ARB);
6604 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6609 static BOOL arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup)
6611 enum yuv_fixup yuv_fixup;
6613 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6615 TRACE("Checking support for fixup:\n");
6616 dump_color_fixup_desc(fixup);
6619 if (is_identity_fixup(fixup))
6625 /* We only support YUV conversions. */
6626 if (!is_yuv_fixup(fixup))
6628 TRACE("[FAILED]\n");
6632 yuv_fixup = get_yuv_fixup(fixup);
6635 case YUV_FIXUP_YUY2:
6636 case YUV_FIXUP_UYVY:
6637 case YUV_FIXUP_YV12:
6642 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
6643 TRACE("[FAILED]\n");
6648 const struct blit_shader arbfp_blit = {
6653 arbfp_blit_color_fixup_supported,
6656 #undef GLINFO_LOCATION