oledb32/tests: Fix incorrect usage of broken() function.
[wine.git] / dlls / wined3d / arb_program_shader.c
blobf809e4aad280344e9216d08cda9eeb4ba716dfd6
1 /*
2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
12 * Copyright 2007-2011, 2013-2014 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
30 #include "config.h"
31 #include "wine/port.h"
33 #include <math.h>
34 #include <stdio.h>
36 #include "wined3d_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
41 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
43 static BOOL shader_is_pshader_version(enum wined3d_shader_type type)
45 return type == WINED3D_SHADER_TYPE_PIXEL;
48 static BOOL shader_is_vshader_version(enum wined3d_shader_type type)
50 return type == WINED3D_SHADER_TYPE_VERTEX;
53 static const char *get_line(const char **ptr)
55 const char *p, *q;
57 p = *ptr;
58 if (!(q = strstr(p, "\n")))
60 if (!*p) return NULL;
61 *ptr += strlen(p);
62 return p;
64 *ptr = q + 1;
66 return p;
69 enum arb_helper_value
71 ARB_ZERO,
72 ARB_ONE,
73 ARB_TWO,
74 ARB_0001,
75 ARB_EPS,
77 ARB_VS_REL_OFFSET
80 static const char *arb_get_helper_value(enum wined3d_shader_type shader, enum arb_helper_value value)
82 if (shader != WINED3D_SHADER_TYPE_VERTEX && shader != WINED3D_SHADER_TYPE_PIXEL)
84 ERR("Unsupported shader type '%s'.\n", debug_shader_type(shader));
85 return "bad";
88 if (shader == WINED3D_SHADER_TYPE_PIXEL)
90 switch (value)
92 case ARB_ZERO: return "ps_helper_const.x";
93 case ARB_ONE: return "ps_helper_const.y";
94 case ARB_TWO: return "coefmul.x";
95 case ARB_0001: return "ps_helper_const.xxxy";
96 case ARB_EPS: return "ps_helper_const.z";
97 default: break;
100 else
102 switch (value)
104 case ARB_ZERO: return "helper_const.x";
105 case ARB_ONE: return "helper_const.y";
106 case ARB_TWO: return "helper_const.z";
107 case ARB_EPS: return "helper_const.w";
108 case ARB_0001: return "helper_const.xxxy";
109 case ARB_VS_REL_OFFSET: return "rel_addr_const.y";
112 FIXME("Unmanaged %s shader helper constant requested: %u.\n",
113 shader == WINED3D_SHADER_TYPE_PIXEL ? "pixel" : "vertex", value);
114 switch (value)
116 case ARB_ZERO: return "0.0";
117 case ARB_ONE: return "1.0";
118 case ARB_TWO: return "2.0";
119 case ARB_0001: return "{0.0, 0.0, 0.0, 1.0}";
120 case ARB_EPS: return "1e-8";
121 default: return "bad";
125 static inline BOOL ffp_clip_emul(const struct wined3d_context *context)
127 return context->lowest_disabled_stage < 7;
130 /* ARB_program_shader private data */
132 struct control_frame
134 struct list entry;
135 enum
138 IFC,
139 LOOP,
141 } type;
142 BOOL muting;
143 BOOL outer_loop;
144 union
146 unsigned int loop;
147 unsigned int ifc;
148 } no;
149 struct wined3d_shader_loop_control loop_control;
150 BOOL had_else;
153 struct arb_ps_np2fixup_info
155 struct ps_np2fixup_info super;
156 /* For ARB we need an offset value:
157 * With both GLSL and ARB mode the NP2 fixup information (the texture dimensions) are stored in a
158 * consecutive way (GLSL uses a uniform array). Since ARB doesn't know the notion of a "standalone"
159 * array we need an offset to the index inside the program local parameter array. */
160 UINT offset;
163 struct arb_ps_compile_args
165 struct ps_compile_args super;
166 WORD bools;
167 WORD clip; /* only a boolean, use a WORD for alignment */
168 unsigned char loop_ctrl[WINED3D_MAX_CONSTS_I][3];
171 struct stb_const_desc
173 unsigned char texunit;
174 UINT const_num;
177 struct arb_ps_compiled_shader
179 struct arb_ps_compile_args args;
180 struct arb_ps_np2fixup_info np2fixup_info;
181 struct stb_const_desc bumpenvmatconst[MAX_TEXTURES];
182 struct stb_const_desc luminanceconst[MAX_TEXTURES];
183 UINT int_consts[WINED3D_MAX_CONSTS_I];
184 GLuint prgId;
185 UINT ycorrection;
186 unsigned char numbumpenvmatconsts;
187 char num_int_consts;
190 struct arb_vs_compile_args
192 struct vs_compile_args super;
193 union
195 struct
197 WORD bools;
198 unsigned char clip_texcoord;
199 unsigned char clipplane_mask;
200 } boolclip;
201 DWORD boolclip_compare;
202 } clip;
203 DWORD ps_signature;
204 union
206 unsigned char samplers[4];
207 DWORD samplers_compare;
208 } vertex;
209 unsigned char loop_ctrl[WINED3D_MAX_CONSTS_I][3];
212 struct arb_vs_compiled_shader
214 struct arb_vs_compile_args args;
215 GLuint prgId;
216 UINT int_consts[WINED3D_MAX_CONSTS_I];
217 char num_int_consts;
218 char need_color_unclamp;
219 UINT pos_fixup;
222 struct recorded_instruction
224 struct wined3d_shader_instruction ins;
225 struct list entry;
228 struct shader_arb_ctx_priv
230 char addr_reg[20];
231 enum
233 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
234 ARB,
235 /* GL_NV_vertex_program2_option or GL_NV_fragment_program_option */
236 NV2,
237 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
239 } target_version;
241 const struct arb_vs_compile_args *cur_vs_args;
242 const struct arb_ps_compile_args *cur_ps_args;
243 const struct arb_ps_compiled_shader *compiled_fprog;
244 const struct arb_vs_compiled_shader *compiled_vprog;
245 struct arb_ps_np2fixup_info *cur_np2fixup_info;
246 struct list control_frames;
247 struct list record;
248 BOOL recording;
249 BOOL muted;
250 unsigned int num_loops, loop_depth, num_ifcs;
251 int aL;
252 BOOL ps_post_process;
254 unsigned int vs_clipplanes;
255 BOOL footer_written;
256 BOOL in_main_func;
258 /* For 3.0 vertex shaders */
259 const char *vs_output[MAX_REG_OUTPUT];
260 /* For 2.x and earlier vertex shaders */
261 const char *texcrd_output[8], *color_output[2], *fog_output;
263 /* 3.0 pshader input for compatibility with fixed function */
264 const char *ps_input[MAX_REG_INPUT];
267 struct ps_signature
269 struct wined3d_shader_signature sig;
270 DWORD idx;
271 struct wine_rb_entry entry;
274 struct arb_pshader_private {
275 struct arb_ps_compiled_shader *gl_shaders;
276 UINT num_gl_shaders, shader_array_size;
277 DWORD input_signature_idx;
278 DWORD clipplane_emulation;
279 BOOL clamp_consts;
282 struct arb_vshader_private {
283 struct arb_vs_compiled_shader *gl_shaders;
284 UINT num_gl_shaders, shader_array_size;
285 UINT rel_offset;
288 struct shader_arb_priv
290 GLuint current_vprogram_id;
291 GLuint current_fprogram_id;
292 const struct arb_ps_compiled_shader *compiled_fprog;
293 const struct arb_vs_compiled_shader *compiled_vprog;
294 GLuint depth_blt_vprogram_id;
295 GLuint depth_blt_fprogram_id_full[WINED3D_GL_RES_TYPE_COUNT];
296 GLuint depth_blt_fprogram_id_masked[WINED3D_GL_RES_TYPE_COUNT];
297 BOOL use_arbfp_fixed_func;
298 struct wine_rb_tree fragment_shaders;
299 BOOL last_ps_const_clamped;
300 BOOL last_vs_color_unclamp;
302 struct wine_rb_tree signature_tree;
303 DWORD ps_sig_number;
305 unsigned int highest_dirty_ps_const, highest_dirty_vs_const;
306 char vshader_const_dirty[WINED3D_MAX_VS_CONSTS_F];
307 char pshader_const_dirty[WINED3D_MAX_PS_CONSTS_F];
308 const struct wined3d_context *last_context;
310 const struct wined3d_vertex_pipe_ops *vertex_pipe;
311 const struct fragment_pipeline *fragment_pipe;
312 BOOL ffp_proj_control;
315 /* Context activation for state handlers is done by the caller. */
317 static BOOL need_rel_addr_const(const struct arb_vshader_private *shader_data,
318 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info)
320 if (shader_data->rel_offset) return TRUE;
321 if (!reg_maps->usesmova) return FALSE;
322 return !gl_info->supported[NV_VERTEX_PROGRAM2_OPTION];
325 /* Returns TRUE if result.clip from GL_NV_vertex_program2 should be used and FALSE otherwise */
326 static inline BOOL use_nv_clip(const struct wined3d_gl_info *gl_info)
328 return gl_info->supported[NV_VERTEX_PROGRAM2_OPTION]
329 && !(gl_info->quirks & WINED3D_QUIRK_NV_CLIP_BROKEN);
332 static BOOL need_helper_const(const struct arb_vshader_private *shader_data,
333 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info)
335 if (need_rel_addr_const(shader_data, reg_maps, gl_info)) return TRUE;
336 if (!gl_info->supported[NV_VERTEX_PROGRAM]) return TRUE; /* Need to init colors. */
337 if (gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT) return TRUE; /* Load the immval offset. */
338 if (gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) return TRUE; /* Have to init texcoords. */
339 if (!use_nv_clip(gl_info)) return TRUE; /* Init the clip texcoord */
340 if (reg_maps->usesnrm) return TRUE; /* 0.0 */
341 if (reg_maps->usespow) return TRUE; /* EPS, 0.0 and 1.0 */
342 if (reg_maps->fog) return TRUE; /* Clamping fog coord, 0.0 and 1.0 */
343 return FALSE;
346 static unsigned int reserved_vs_const(const struct arb_vshader_private *shader_data,
347 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info)
349 unsigned int ret = 1;
350 /* We use one PARAM for the pos fixup, and in some cases one to load
351 * some immediate values into the shader. */
352 if (need_helper_const(shader_data, reg_maps, gl_info)) ++ret;
353 if (need_rel_addr_const(shader_data, reg_maps, gl_info)) ++ret;
354 return ret;
357 /* Loads floating point constants into the currently set ARB_vertex/fragment_program.
358 * When constant_list == NULL, it will load all the constants.
360 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
361 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
363 /* Context activation is done by the caller. */
364 static unsigned int shader_arb_load_constants_f(const struct wined3d_shader *shader,
365 const struct wined3d_gl_info *gl_info, GLuint target_type, unsigned int max_constants,
366 const struct wined3d_vec4 *constants, char *dirty_consts)
368 struct wined3d_shader_lconst *lconst;
369 unsigned int ret, i, j;
371 if (TRACE_ON(d3d_constants))
373 for (i = 0; i < max_constants; ++i)
375 if (!dirty_consts[i])
376 continue;
377 TRACE_(d3d_constants)("Loading constant %u: %s.\n", i, debug_vec4(&constants[i]));
381 i = 0;
383 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
384 if (target_type == GL_FRAGMENT_PROGRAM_ARB && shader->reg_maps.shader_version.major == 1)
386 float lcl_const[4];
387 /* ps 1.x supports only 8 constants, clamp only those. When switching between 1.x and higher
388 * shaders, the first 8 constants are marked dirty for reload
390 for (; i < min(8, max_constants); ++i)
392 if (!dirty_consts[i])
393 continue;
394 dirty_consts[i] = 0;
396 if (constants[i].x > 1.0f)
397 lcl_const[0] = 1.0f;
398 else if (constants[i].x < -1.0f)
399 lcl_const[0] = -1.0f;
400 else
401 lcl_const[0] = constants[i].x;
403 if (constants[i].y > 1.0f)
404 lcl_const[1] = 1.0f;
405 else if (constants[i].y < -1.0f)
406 lcl_const[1] = -1.0f;
407 else
408 lcl_const[1] = constants[i].y;
410 if (constants[i].z > 1.0f)
411 lcl_const[2] = 1.0f;
412 else if (constants[i].z < -1.0f)
413 lcl_const[2] = -1.0f;
414 else
415 lcl_const[2] = constants[i].z;
417 if (constants[i].w > 1.0f)
418 lcl_const[3] = 1.0f;
419 else if (constants[i].w < -1.0f)
420 lcl_const[3] = -1.0f;
421 else
422 lcl_const[3] = constants[i].w;
424 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
427 /* If further constants are dirty, reload them without clamping.
429 * The alternative is not to touch them, but then we cannot reset the dirty constant count
430 * to zero. That's bad for apps that only use PS 1.x shaders, because in that case the code
431 * above would always re-check the first 8 constants since max_constant remains at the init
432 * value
436 if (gl_info->supported[EXT_GPU_PROGRAM_PARAMETERS])
438 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
439 * or just reloading *all* constants at once
441 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, i, max_constants, constants + (i * 4)));
443 for (; i < max_constants; ++i)
445 if (!dirty_consts[i])
446 continue;
448 /* Find the next block of dirty constants */
449 dirty_consts[i] = 0;
450 j = i;
451 for (++i; (i < max_constants) && dirty_consts[i]; ++i)
453 dirty_consts[i] = 0;
456 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, &constants[j].x));
459 else
461 for (; i < max_constants; ++i)
463 if (dirty_consts[i])
465 dirty_consts[i] = 0;
466 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, &constants[i].x));
470 checkGLcall("glProgramEnvParameter4fvARB()");
472 /* Load immediate constants */
473 if (shader->load_local_constsF)
475 if (TRACE_ON(d3d_shader))
477 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
479 GLfloat* values = (GLfloat*)lconst->value;
480 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
481 values[0], values[1], values[2], values[3]);
484 /* Immediate constants are clamped for 1.X shaders at loading times */
485 ret = 0;
486 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
488 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
489 ret = max(ret, lconst->idx + 1);
490 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
492 checkGLcall("glProgramEnvParameter4fvARB()");
493 return ret; /* The loaded immediate constants need reloading for the next shader */
494 } else {
495 return 0; /* No constants are dirty now */
499 /* Loads the texture dimensions for NP2 fixup into the currently set
500 * ARB_[vertex/fragment]_programs. */
501 static void shader_arb_load_np2fixup_constants(const struct arb_ps_np2fixup_info *fixup,
502 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
504 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
505 WORD active = fixup->super.active;
506 UINT i;
508 if (!active)
509 return;
511 for (i = 0; active; active >>= 1, ++i)
513 const struct wined3d_texture *tex = state->textures[i];
514 unsigned char idx = fixup->super.idx[i];
515 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
517 if (!(active & 1))
518 continue;
520 if (!tex)
522 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
523 continue;
526 if (idx % 2)
528 tex_dim[2] = tex->pow2_matrix[0];
529 tex_dim[3] = tex->pow2_matrix[5];
531 else
533 tex_dim[0] = tex->pow2_matrix[0];
534 tex_dim[1] = tex->pow2_matrix[5];
538 for (i = 0; i < fixup->super.num_consts; ++i)
540 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
541 fixup->offset + i, &np2fixup_constants[i * 4]));
545 /* Context activation is done by the caller. */
546 static void shader_arb_ps_local_constants(const struct arb_ps_compiled_shader *gl_shader,
547 const struct wined3d_context *context, const struct wined3d_state *state, UINT rt_height)
549 const struct wined3d_gl_info *gl_info = context->gl_info;
550 unsigned char i;
552 for(i = 0; i < gl_shader->numbumpenvmatconsts; i++)
554 int texunit = gl_shader->bumpenvmatconst[i].texunit;
556 /* The state manager takes care that this function is always called if the bump env matrix changes */
557 const float *data = (const float *)&state->texture_states[texunit][WINED3D_TSS_BUMPENV_MAT00];
558 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
559 gl_shader->bumpenvmatconst[i].const_num, data));
561 if (gl_shader->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED)
563 /* WINED3D_TSS_BUMPENVLSCALE and WINED3D_TSS_BUMPENVLOFFSET are next to each other.
564 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
565 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
566 * (they're WINED3D_TSS_TEXTURETRANSFORMFLAGS and WINED3D_TSS_ADDRESSW, so most likely 0 or NaN
568 const float *scale = (const float *)&state->texture_states[texunit][WINED3D_TSS_BUMPENV_LSCALE];
569 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
570 gl_shader->luminanceconst[i].const_num, scale));
573 checkGLcall("Load bumpmap consts");
575 if(gl_shader->ycorrection != WINED3D_CONST_NUM_UNUSED)
577 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
578 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
579 * ycorrection.z: 1.0
580 * ycorrection.w: 0.0
582 float val[4];
583 val[0] = context->render_offscreen ? 0.0f : (float) rt_height;
584 val[1] = context->render_offscreen ? 1.0f : -1.0f;
585 val[2] = 1.0f;
586 val[3] = 0.0f;
587 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->ycorrection, val));
588 checkGLcall("y correction loading");
591 if (!gl_shader->num_int_consts) return;
593 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
595 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
597 float val[4];
598 val[0] = (float)state->ps_consts_i[i].x;
599 val[1] = (float)state->ps_consts_i[i].y;
600 val[2] = (float)state->ps_consts_i[i].z;
601 val[3] = -1.0f;
603 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->int_consts[i], val));
606 checkGLcall("Load ps int consts");
609 /* Context activation is done by the caller. */
610 static void shader_arb_vs_local_constants(const struct arb_vs_compiled_shader *gl_shader,
611 const struct wined3d_context *context, const struct wined3d_state *state)
613 const struct wined3d_gl_info *gl_info = context->gl_info;
614 float position_fixup[4];
615 unsigned char i;
617 /* Upload the position fixup */
618 shader_get_position_fixup(context, state, position_fixup);
619 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->pos_fixup, position_fixup));
621 if (!gl_shader->num_int_consts) return;
623 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
625 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
627 float val[4];
628 val[0] = (float)state->vs_consts_i[i].x;
629 val[1] = (float)state->vs_consts_i[i].y;
630 val[2] = (float)state->vs_consts_i[i].z;
631 val[3] = -1.0f;
633 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->int_consts[i], val));
636 checkGLcall("Load vs int consts");
639 static void shader_arb_select(void *shader_priv, struct wined3d_context *context,
640 const struct wined3d_state *state);
643 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
645 * We only support float constants in ARB at the moment, so don't
646 * worry about the Integers or Booleans
648 /* Context activation is done by the caller (state handler). */
649 static void shader_arb_load_constants_internal(struct shader_arb_priv *priv,
650 struct wined3d_context *context, const struct wined3d_state *state,
651 BOOL usePixelShader, BOOL useVertexShader, BOOL from_shader_select)
653 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
654 const struct wined3d_gl_info *gl_info = context->gl_info;
656 if (!from_shader_select)
658 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
659 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
661 if (vshader
662 && (vshader->reg_maps.boolean_constants
663 || (!gl_info->supported[NV_VERTEX_PROGRAM2_OPTION]
664 && (vshader->reg_maps.integer_constants & ~vshader->reg_maps.local_int_consts))))
666 TRACE("bool/integer vertex shader constants potentially modified, forcing shader reselection.\n");
667 shader_arb_select(priv, context, state);
669 else if (pshader
670 && (pshader->reg_maps.boolean_constants
671 || (!gl_info->supported[NV_FRAGMENT_PROGRAM_OPTION]
672 && (pshader->reg_maps.integer_constants & ~pshader->reg_maps.local_int_consts))))
674 TRACE("bool/integer pixel shader constants potentially modified, forcing shader reselection.\n");
675 shader_arb_select(priv, context, state);
679 if (context != priv->last_context)
681 memset(priv->vshader_const_dirty, 1,
682 sizeof(*priv->vshader_const_dirty) * d3d_info->limits.vs_uniform_count);
683 priv->highest_dirty_vs_const = d3d_info->limits.vs_uniform_count;
685 memset(priv->pshader_const_dirty, 1,
686 sizeof(*priv->pshader_const_dirty) * d3d_info->limits.ps_uniform_count);
687 priv->highest_dirty_ps_const = d3d_info->limits.ps_uniform_count;
689 priv->last_context = context;
692 if (useVertexShader)
694 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
695 const struct arb_vs_compiled_shader *gl_shader = priv->compiled_vprog;
697 /* Load DirectX 9 float constants for vertex shader */
698 priv->highest_dirty_vs_const = shader_arb_load_constants_f(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
699 priv->highest_dirty_vs_const, state->vs_consts_f, priv->vshader_const_dirty);
700 shader_arb_vs_local_constants(gl_shader, context, state);
703 if (usePixelShader)
705 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
706 const struct arb_ps_compiled_shader *gl_shader = priv->compiled_fprog;
707 UINT rt_height = state->fb->render_targets[0]->height;
709 /* Load DirectX 9 float constants for pixel shader */
710 priv->highest_dirty_ps_const = shader_arb_load_constants_f(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
711 priv->highest_dirty_ps_const, state->ps_consts_f, priv->pshader_const_dirty);
712 shader_arb_ps_local_constants(gl_shader, context, state, rt_height);
714 if (context->constant_update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
715 shader_arb_load_np2fixup_constants(&gl_shader->np2fixup_info, gl_info, state);
719 static void shader_arb_load_constants(void *shader_priv, struct wined3d_context *context,
720 const struct wined3d_state *state)
722 BOOL vs = use_vs(state);
723 BOOL ps = use_ps(state);
725 shader_arb_load_constants_internal(shader_priv, context, state, ps, vs, FALSE);
728 static void shader_arb_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
730 struct wined3d_context *context = context_get_current();
731 struct shader_arb_priv *priv = device->shader_priv;
733 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
734 * context. On a context switch the old context will be fully dirtified */
735 if (!context || context->swapchain->device != device) return;
737 memset(priv->vshader_const_dirty + start, 1, sizeof(*priv->vshader_const_dirty) * count);
738 priv->highest_dirty_vs_const = max(priv->highest_dirty_vs_const, start + count);
741 static void shader_arb_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
743 struct wined3d_context *context = context_get_current();
744 struct shader_arb_priv *priv = device->shader_priv;
746 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
747 * context. On a context switch the old context will be fully dirtified */
748 if (!context || context->swapchain->device != device) return;
750 memset(priv->pshader_const_dirty + start, 1, sizeof(*priv->pshader_const_dirty) * count);
751 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, start + count);
754 static void shader_arb_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
756 char str[4][17];
758 wined3d_ftoa(values[0], str[0]);
759 wined3d_ftoa(values[1], str[1]);
760 wined3d_ftoa(values[2], str[2]);
761 wined3d_ftoa(values[3], str[3]);
762 shader_addline(buffer, "{%s, %s, %s, %s}", str[0], str[1], str[2], str[3]);
765 /* Generate the variable & register declarations for the ARB_vertex_program output target */
766 static void shader_generate_arb_declarations(const struct wined3d_shader *shader,
767 const struct wined3d_shader_reg_maps *reg_maps, struct wined3d_string_buffer *buffer,
768 const struct wined3d_gl_info *gl_info, DWORD *num_clipplanes,
769 const struct shader_arb_ctx_priv *ctx)
771 DWORD i;
772 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
773 const struct wined3d_shader_lconst *lconst;
774 unsigned max_constantsF;
775 DWORD map;
777 /* In pixel shaders, all private constants are program local, we don't need anything
778 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
779 * If we need a private constant the GL implementation will squeeze it in somewhere
781 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
782 * immediate values. The posFixup is loaded using program.env for now, so always
783 * subtract one from the number of constants. If the shader uses indirect addressing,
784 * account for the helper const too because we have to declare all available d3d constants
785 * and don't know which are actually used.
787 if (pshader)
789 max_constantsF = gl_info->limits.arb_ps_native_constants;
790 /* 24 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value. */
791 if (max_constantsF < 24)
792 max_constantsF = gl_info->limits.arb_ps_float_constants;
794 else
796 const struct arb_vshader_private *shader_data = shader->backend_data;
797 max_constantsF = gl_info->limits.arb_vs_native_constants;
798 /* 96 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value.
799 * Also prevents max_constantsF from becoming less than 0 and
800 * wrapping . */
801 if (max_constantsF < 96)
802 max_constantsF = gl_info->limits.arb_vs_float_constants;
804 if (reg_maps->usesrelconstF)
806 DWORD highest_constf = 0, clip_limit;
808 max_constantsF -= reserved_vs_const(shader_data, reg_maps, gl_info);
809 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
810 max_constantsF -= gl_info->reserved_arb_constants;
812 for (i = 0; i < shader->limits->constant_float; ++i)
814 DWORD idx = i >> 5;
815 DWORD shift = i & 0x1f;
816 if (reg_maps->constf[idx] & (1u << shift))
817 highest_constf = i;
820 if(use_nv_clip(gl_info) && ctx->target_version >= NV2)
822 if(ctx->cur_vs_args->super.clip_enabled)
823 clip_limit = gl_info->limits.clipplanes;
824 else
825 clip_limit = 0;
827 else
829 unsigned int mask = ctx->cur_vs_args->clip.boolclip.clipplane_mask;
830 clip_limit = min(wined3d_popcount(mask), 4);
832 *num_clipplanes = min(clip_limit, max_constantsF - highest_constf - 1);
833 max_constantsF -= *num_clipplanes;
834 if(*num_clipplanes < clip_limit)
836 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes, gl_info->limits.clipplanes);
839 else
841 if (ctx->target_version >= NV2) *num_clipplanes = gl_info->limits.clipplanes;
842 else *num_clipplanes = min(gl_info->limits.clipplanes, 4);
846 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
848 if (map & 1) shader_addline(buffer, "TEMP R%u;\n", i);
851 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
853 if (map & 1) shader_addline(buffer, "ADDRESS A%u;\n", i);
856 if (pshader && reg_maps->shader_version.major == 1 && reg_maps->shader_version.minor <= 3)
858 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
860 if (map & 1) shader_addline(buffer, "TEMP T%u;\n", i);
864 if (!shader->load_local_constsF)
866 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
868 const float *value;
869 value = (const float *)lconst->value;
870 shader_addline(buffer, "PARAM C%u = ", lconst->idx);
871 shader_arb_append_imm_vec4(buffer, value);
872 shader_addline(buffer, ";\n");
876 /* After subtracting privately used constants from the hardware limit(they are loaded as
877 * local constants), make sure the shader doesn't violate the env constant limit
879 if(pshader)
881 max_constantsF = min(max_constantsF, gl_info->limits.arb_ps_float_constants);
883 else
885 max_constantsF = min(max_constantsF, gl_info->limits.arb_vs_float_constants);
888 /* Avoid declaring more constants than needed */
889 max_constantsF = min(max_constantsF, shader->limits->constant_float);
891 /* we use the array-based constants array if the local constants are marked for loading,
892 * because then we use indirect addressing, or when the local constant list is empty,
893 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
894 * local constants do not declare the loaded constants as an array because ARB compilers usually
895 * do not optimize unused constants away
897 if (reg_maps->usesrelconstF)
899 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
900 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
901 max_constantsF, max_constantsF - 1);
903 else
905 for (i = 0; i < max_constantsF; ++i)
907 DWORD idx, mask;
908 idx = i >> 5;
909 mask = 1u << (i & 0x1fu);
910 if (!shader_constant_is_local(shader, i) && (reg_maps->constf[idx] & mask))
912 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
918 static const char * const shift_tab[] = {
919 "dummy", /* 0 (none) */
920 "coefmul.x", /* 1 (x2) */
921 "coefmul.y", /* 2 (x4) */
922 "coefmul.z", /* 3 (x8) */
923 "coefmul.w", /* 4 (x16) */
924 "dummy", /* 5 (x32) */
925 "dummy", /* 6 (x64) */
926 "dummy", /* 7 (x128) */
927 "dummy", /* 8 (d256) */
928 "dummy", /* 9 (d128) */
929 "dummy", /* 10 (d64) */
930 "dummy", /* 11 (d32) */
931 "coefdiv.w", /* 12 (d16) */
932 "coefdiv.z", /* 13 (d8) */
933 "coefdiv.y", /* 14 (d4) */
934 "coefdiv.x" /* 15 (d2) */
937 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction *ins,
938 const struct wined3d_shader_dst_param *dst, char *write_mask)
940 char *ptr = write_mask;
942 if (dst->write_mask != WINED3DSP_WRITEMASK_ALL)
944 *ptr++ = '.';
945 if (dst->write_mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
946 if (dst->write_mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
947 if (dst->write_mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
948 if (dst->write_mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
951 *ptr = '\0';
954 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param *param, BOOL fixup, char *swizzle_str)
956 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
957 * but addressed as "rgba". To fix this we need to swap the register's x
958 * and z components. */
959 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
960 char *ptr = swizzle_str;
962 /* swizzle bits fields: wwzzyyxx */
963 DWORD swizzle = param->swizzle;
964 DWORD swizzle_x = swizzle & 0x03;
965 DWORD swizzle_y = (swizzle >> 2) & 0x03;
966 DWORD swizzle_z = (swizzle >> 4) & 0x03;
967 DWORD swizzle_w = (swizzle >> 6) & 0x03;
969 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
970 * generate a swizzle string. Unless we need to our own swizzling. */
971 if (swizzle != WINED3DSP_NOSWIZZLE || fixup)
973 *ptr++ = '.';
974 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
975 *ptr++ = swizzle_chars[swizzle_x];
976 } else {
977 *ptr++ = swizzle_chars[swizzle_x];
978 *ptr++ = swizzle_chars[swizzle_y];
979 *ptr++ = swizzle_chars[swizzle_z];
980 *ptr++ = swizzle_chars[swizzle_w];
984 *ptr = '\0';
987 static void shader_arb_request_a0(const struct wined3d_shader_instruction *ins, const char *src)
989 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
990 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
992 if (!strcmp(priv->addr_reg, src)) return;
994 strcpy(priv->addr_reg, src);
995 shader_addline(buffer, "ARL A0.x, %s;\n", src);
998 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
999 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr);
1001 static void shader_arb_get_register_name(const struct wined3d_shader_instruction *ins,
1002 const struct wined3d_shader_register *reg, char *register_name, BOOL *is_color)
1004 /* oPos, oFog and oPts in D3D */
1005 static const char * const rastout_reg_names[] = {"TMP_OUT", "TMP_FOGCOORD", "result.pointsize"};
1006 const struct wined3d_shader *shader = ins->ctx->shader;
1007 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1008 BOOL pshader = shader_is_pshader_version(reg_maps->shader_version.type);
1009 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1011 *is_color = FALSE;
1013 switch (reg->type)
1015 case WINED3DSPR_TEMP:
1016 sprintf(register_name, "R%u", reg->idx[0].offset);
1017 break;
1019 case WINED3DSPR_INPUT:
1020 if (pshader)
1022 if (reg_maps->shader_version.major < 3)
1024 if (!reg->idx[0].offset)
1025 strcpy(register_name, "fragment.color.primary");
1026 else
1027 strcpy(register_name, "fragment.color.secondary");
1029 else
1031 if (reg->idx[0].rel_addr)
1033 char rel_reg[50];
1034 shader_arb_get_src_param(ins, reg->idx[0].rel_addr, 0, rel_reg);
1036 if (!strcmp(rel_reg, "**aL_emul**"))
1038 DWORD idx = ctx->aL + reg->idx[0].offset;
1039 if(idx < MAX_REG_INPUT)
1041 strcpy(register_name, ctx->ps_input[idx]);
1043 else
1045 ERR("Pixel shader input register out of bounds: %u\n", idx);
1046 sprintf(register_name, "out_of_bounds_%u", idx);
1049 else if (reg_maps->input_registers & 0x0300)
1051 /* There are two ways basically:
1053 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
1054 * That means trouble if the loop also contains a breakc or if the control values
1055 * aren't local constants.
1056 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
1057 * source dynamically. The trouble is that we cannot simply read aL.y because it
1058 * is an ADDRESS register. We could however push it, load .zw with a value and use
1059 * ADAC to load the condition code register and pop it again afterwards
1061 FIXME("Relative input register addressing with more than 8 registers\n");
1063 /* This is better than nothing for now */
1064 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx[0].offset);
1066 else if(ctx->cur_ps_args->super.vp_mode != vertexshader)
1068 /* This is problematic because we'd have to consult the ctx->ps_input strings
1069 * for where to find the varying. Some may be "0.0", others can be texcoords or
1070 * colors. This needs either a pipeline replacement to make the vertex shader feed
1071 * proper varyings, or loop unrolling
1073 * For now use the texcoords and hope for the best
1075 FIXME("Non-vertex shader varying input with indirect addressing\n");
1076 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx[0].offset);
1078 else
1080 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
1081 * pulls GL_NV_fragment_program2 in
1083 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx[0].offset);
1086 else
1088 if (reg->idx[0].offset < MAX_REG_INPUT)
1090 strcpy(register_name, ctx->ps_input[reg->idx[0].offset]);
1092 else
1094 ERR("Pixel shader input register out of bounds: %u\n", reg->idx[0].offset);
1095 sprintf(register_name, "out_of_bounds_%u", reg->idx[0].offset);
1100 else
1102 if (ctx->cur_vs_args->super.swizzle_map & (1u << reg->idx[0].offset))
1103 *is_color = TRUE;
1104 sprintf(register_name, "vertex.attrib[%u]", reg->idx[0].offset);
1106 break;
1108 case WINED3DSPR_CONST:
1109 if (!pshader && reg->idx[0].rel_addr)
1111 const struct arb_vshader_private *shader_data = shader->backend_data;
1112 UINT rel_offset = ctx->target_version == ARB ? shader_data->rel_offset : 0;
1113 BOOL aL = FALSE;
1114 char rel_reg[50];
1115 if (reg_maps->shader_version.major < 2)
1117 sprintf(rel_reg, "A0.x");
1119 else
1121 shader_arb_get_src_param(ins, reg->idx[0].rel_addr, 0, rel_reg);
1122 if (ctx->target_version == ARB)
1124 if (!strcmp(rel_reg, "**aL_emul**"))
1126 aL = TRUE;
1127 } else {
1128 shader_arb_request_a0(ins, rel_reg);
1129 sprintf(rel_reg, "A0.x");
1133 if (aL)
1134 sprintf(register_name, "C[%u]", ctx->aL + reg->idx[0].offset);
1135 else if (reg->idx[0].offset >= rel_offset)
1136 sprintf(register_name, "C[%s + %u]", rel_reg, reg->idx[0].offset - rel_offset);
1137 else
1138 sprintf(register_name, "C[%s - %u]", rel_reg, rel_offset - reg->idx[0].offset);
1140 else
1142 if (reg_maps->usesrelconstF)
1143 sprintf(register_name, "C[%u]", reg->idx[0].offset);
1144 else
1145 sprintf(register_name, "C%u", reg->idx[0].offset);
1147 break;
1149 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1150 if (pshader)
1152 if (reg_maps->shader_version.major == 1
1153 && reg_maps->shader_version.minor <= 3)
1154 /* In ps <= 1.3, Tx is a temporary register as destination
1155 * to all instructions, and as source to most instructions.
1156 * For some instructions it is the texcoord input. Those
1157 * instructions know about the special use. */
1158 sprintf(register_name, "T%u", reg->idx[0].offset);
1159 else
1160 /* In ps 1.4 and 2.x Tx is always a (read-only) varying. */
1161 sprintf(register_name, "fragment.texcoord[%u]", reg->idx[0].offset);
1163 else
1165 if (reg_maps->shader_version.major == 1 || ctx->target_version >= NV2)
1166 sprintf(register_name, "A%u", reg->idx[0].offset);
1167 else
1168 sprintf(register_name, "A%u_SHADOW", reg->idx[0].offset);
1170 break;
1172 case WINED3DSPR_COLOROUT:
1173 if (ctx->ps_post_process && !reg->idx[0].offset)
1175 strcpy(register_name, "TMP_COLOR");
1177 else
1179 if (ctx->cur_ps_args->super.srgb_correction)
1180 FIXME("sRGB correction on higher render targets.\n");
1181 if (reg_maps->rt_mask > 1)
1182 sprintf(register_name, "result.color[%u]", reg->idx[0].offset);
1183 else
1184 strcpy(register_name, "result.color");
1186 break;
1188 case WINED3DSPR_RASTOUT:
1189 if (reg->idx[0].offset == 1)
1190 sprintf(register_name, "%s", ctx->fog_output);
1191 else
1192 sprintf(register_name, "%s", rastout_reg_names[reg->idx[0].offset]);
1193 break;
1195 case WINED3DSPR_DEPTHOUT:
1196 strcpy(register_name, "result.depth");
1197 break;
1199 case WINED3DSPR_ATTROUT:
1200 /* case WINED3DSPR_OUTPUT: */
1201 if (pshader)
1202 sprintf(register_name, "oD[%u]", reg->idx[0].offset);
1203 else
1204 strcpy(register_name, ctx->color_output[reg->idx[0].offset]);
1205 break;
1207 case WINED3DSPR_TEXCRDOUT:
1208 if (pshader)
1209 sprintf(register_name, "oT[%u]", reg->idx[0].offset);
1210 else if (reg_maps->shader_version.major < 3)
1211 strcpy(register_name, ctx->texcrd_output[reg->idx[0].offset]);
1212 else
1213 strcpy(register_name, ctx->vs_output[reg->idx[0].offset]);
1214 break;
1216 case WINED3DSPR_LOOP:
1217 if(ctx->target_version >= NV2)
1219 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
1220 if(pshader) sprintf(register_name, "A0.x");
1221 else sprintf(register_name, "aL.y");
1223 else
1225 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
1226 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
1227 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
1228 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
1229 * indexing
1231 sprintf(register_name, "**aL_emul**");
1234 break;
1236 case WINED3DSPR_CONSTINT:
1237 sprintf(register_name, "I%u", reg->idx[0].offset);
1238 break;
1240 case WINED3DSPR_MISCTYPE:
1241 if (!reg->idx[0].offset)
1242 sprintf(register_name, "vpos");
1243 else if (reg->idx[0].offset == 1)
1244 sprintf(register_name, "fragment.facing.x");
1245 else
1246 FIXME("Unknown MISCTYPE register index %u.\n", reg->idx[0].offset);
1247 break;
1249 default:
1250 FIXME("Unhandled register type %#x[%u].\n", reg->type, reg->idx[0].offset);
1251 sprintf(register_name, "unrecognized_register[%u]", reg->idx[0].offset);
1252 break;
1256 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction *ins,
1257 const struct wined3d_shader_dst_param *wined3d_dst, char *str)
1259 char register_name[255];
1260 char write_mask[6];
1261 BOOL is_color;
1263 shader_arb_get_register_name(ins, &wined3d_dst->reg, register_name, &is_color);
1264 strcpy(str, register_name);
1266 shader_arb_get_write_mask(ins, wined3d_dst, write_mask);
1267 strcat(str, write_mask);
1270 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
1272 switch(channel_source)
1274 case CHANNEL_SOURCE_ZERO: return "0";
1275 case CHANNEL_SOURCE_ONE: return "1";
1276 case CHANNEL_SOURCE_X: return "x";
1277 case CHANNEL_SOURCE_Y: return "y";
1278 case CHANNEL_SOURCE_Z: return "z";
1279 case CHANNEL_SOURCE_W: return "w";
1280 default:
1281 FIXME("Unhandled channel source %#x\n", channel_source);
1282 return "undefined";
1286 struct color_fixup_masks
1288 DWORD source;
1289 DWORD sign;
1292 static struct color_fixup_masks calc_color_correction(struct color_fixup_desc fixup, DWORD dst_mask)
1294 struct color_fixup_masks masks = {0, 0};
1296 if (is_complex_fixup(fixup))
1298 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
1299 FIXME("Complex fixup (%#x) not supported\n", complex_fixup);
1300 return masks;
1303 if (fixup.x_source != CHANNEL_SOURCE_X)
1304 masks.source |= WINED3DSP_WRITEMASK_0;
1305 if (fixup.y_source != CHANNEL_SOURCE_Y)
1306 masks.source |= WINED3DSP_WRITEMASK_1;
1307 if (fixup.z_source != CHANNEL_SOURCE_Z)
1308 masks.source |= WINED3DSP_WRITEMASK_2;
1309 if (fixup.w_source != CHANNEL_SOURCE_W)
1310 masks.source |= WINED3DSP_WRITEMASK_3;
1311 masks.source &= dst_mask;
1313 if (fixup.x_sign_fixup)
1314 masks.sign |= WINED3DSP_WRITEMASK_0;
1315 if (fixup.y_sign_fixup)
1316 masks.sign |= WINED3DSP_WRITEMASK_1;
1317 if (fixup.z_sign_fixup)
1318 masks.sign |= WINED3DSP_WRITEMASK_2;
1319 if (fixup.w_sign_fixup)
1320 masks.sign |= WINED3DSP_WRITEMASK_3;
1321 masks.sign &= dst_mask;
1323 return masks;
1326 static void gen_color_correction(struct wined3d_string_buffer *buffer, const char *dst,
1327 const char *src, const char *one, const char *two,
1328 struct color_fixup_desc fixup, struct color_fixup_masks masks)
1330 const char *sign_fixup_src = dst;
1332 if (masks.source)
1334 if (masks.sign)
1335 sign_fixup_src = "TA";
1337 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", sign_fixup_src, src,
1338 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
1339 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
1341 else if (masks.sign)
1343 sign_fixup_src = src;
1346 if (masks.sign)
1348 char reg_mask[6];
1349 char *ptr = reg_mask;
1351 if (masks.sign != WINED3DSP_WRITEMASK_ALL)
1353 *ptr++ = '.';
1354 if (masks.sign & WINED3DSP_WRITEMASK_0)
1355 *ptr++ = 'x';
1356 if (masks.sign & WINED3DSP_WRITEMASK_1)
1357 *ptr++ = 'y';
1358 if (masks.sign & WINED3DSP_WRITEMASK_2)
1359 *ptr++ = 'z';
1360 if (masks.sign & WINED3DSP_WRITEMASK_3)
1361 *ptr++ = 'w';
1363 *ptr = '\0';
1365 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", dst, reg_mask, sign_fixup_src, two, one);
1369 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction *ins)
1371 DWORD mod;
1372 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1373 if (!ins->dst_count) return "";
1375 mod = ins->dst[0].modifiers;
1377 /* Silently ignore PARTIALPRECISION if it's not supported */
1378 if(priv->target_version == ARB) mod &= ~WINED3DSPDM_PARTIALPRECISION;
1380 if(mod & WINED3DSPDM_MSAMPCENTROID)
1382 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1383 mod &= ~WINED3DSPDM_MSAMPCENTROID;
1386 switch(mod)
1388 case WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION:
1389 return "H_SAT";
1391 case WINED3DSPDM_SATURATE:
1392 return "_SAT";
1394 case WINED3DSPDM_PARTIALPRECISION:
1395 return "H";
1397 case 0:
1398 return "";
1400 default:
1401 FIXME("Unknown modifiers 0x%08x\n", mod);
1402 return "";
1406 #define TEX_PROJ 0x1
1407 #define TEX_BIAS 0x2
1408 #define TEX_LOD 0x4
1409 #define TEX_DERIV 0x10
1411 static void shader_hw_sample(const struct wined3d_shader_instruction *ins, DWORD sampler_idx,
1412 const char *dst_str, const char *coord_reg, WORD flags, const char *dsx, const char *dsy)
1414 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[sampler_idx].type;
1415 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1416 const char *tex_type;
1417 BOOL np2_fixup = FALSE;
1418 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1419 const char *mod;
1420 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
1421 const struct wined3d_shader *shader;
1422 const struct wined3d_device *device;
1423 const struct wined3d_gl_info *gl_info;
1424 const char *tex_dst = dst_str;
1425 struct color_fixup_masks masks;
1427 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1428 if(!pshader) sampler_idx += MAX_FRAGMENT_SAMPLERS;
1430 switch (resource_type)
1432 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1433 tex_type = "1D";
1434 break;
1436 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1437 shader = ins->ctx->shader;
1438 device = shader->device;
1439 gl_info = &device->adapter->gl_info;
1441 if (pshader && priv->cur_ps_args->super.np2_fixup & (1u << sampler_idx)
1442 && gl_info->supported[ARB_TEXTURE_RECTANGLE])
1443 tex_type = "RECT";
1444 else
1445 tex_type = "2D";
1446 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1448 if (priv->cur_np2fixup_info->super.active & (1u << sampler_idx))
1450 if (flags) FIXME("Only ordinary sampling from NP2 textures is supported.\n");
1451 else np2_fixup = TRUE;
1454 break;
1456 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1457 tex_type = "3D";
1458 break;
1460 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1461 tex_type = "CUBE";
1462 break;
1464 default:
1465 ERR("Unexpected resource type %#x.\n", resource_type);
1466 tex_type = "";
1469 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1470 * so don't use shader_arb_get_modifier
1472 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) mod = "_SAT";
1473 else mod = "";
1475 /* Fragment samplers always have indentity mapping */
1476 if(sampler_idx >= MAX_FRAGMENT_SAMPLERS)
1478 sampler_idx = priv->cur_vs_args->vertex.samplers[sampler_idx - MAX_FRAGMENT_SAMPLERS];
1481 if (pshader)
1483 masks = calc_color_correction(priv->cur_ps_args->super.color_fixup[sampler_idx],
1484 ins->dst[0].write_mask);
1486 if (masks.source || masks.sign)
1487 tex_dst = "TA";
1490 if (flags & TEX_DERIV)
1492 if(flags & TEX_PROJ) FIXME("Projected texture sampling with custom derivatives\n");
1493 if(flags & TEX_BIAS) FIXME("Biased texture sampling with custom derivatives\n");
1494 shader_addline(buffer, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod, tex_dst, coord_reg,
1495 dsx, dsy, sampler_idx, tex_type);
1497 else if(flags & TEX_LOD)
1499 if(flags & TEX_PROJ) FIXME("Projected texture sampling with explicit lod\n");
1500 if(flags & TEX_BIAS) FIXME("Biased texture sampling with explicit lod\n");
1501 shader_addline(buffer, "TXL%s %s, %s, texture[%u], %s;\n", mod, tex_dst, coord_reg,
1502 sampler_idx, tex_type);
1504 else if (flags & TEX_BIAS)
1506 /* Shouldn't be possible, but let's check for it */
1507 if(flags & TEX_PROJ) FIXME("Biased and Projected texture sampling\n");
1508 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1509 shader_addline(buffer, "TXB%s %s, %s, texture[%u], %s;\n", mod, tex_dst, coord_reg, sampler_idx, tex_type);
1511 else if (flags & TEX_PROJ)
1513 shader_addline(buffer, "TXP%s %s, %s, texture[%u], %s;\n", mod, tex_dst, coord_reg, sampler_idx, tex_type);
1515 else
1517 if (np2_fixup)
1519 const unsigned char idx = priv->cur_np2fixup_info->super.idx[sampler_idx];
1520 shader_addline(buffer, "MUL TA, np2fixup[%u].%s, %s;\n", idx >> 1,
1521 (idx % 2) ? "zwxy" : "xyzw", coord_reg);
1523 shader_addline(buffer, "TEX%s %s, TA, texture[%u], %s;\n", mod, tex_dst, sampler_idx, tex_type);
1525 else
1526 shader_addline(buffer, "TEX%s %s, %s, texture[%u], %s;\n", mod, tex_dst, coord_reg, sampler_idx, tex_type);
1529 if (pshader)
1531 gen_color_correction(buffer, dst_str, tex_dst,
1532 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL, ARB_ONE),
1533 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL, ARB_TWO),
1534 priv->cur_ps_args->super.color_fixup[sampler_idx], masks);
1538 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
1539 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr)
1541 /* Generate a line that does the input modifier computation and return the input register to use */
1542 BOOL is_color = FALSE, insert_line;
1543 char regstr[256];
1544 char swzstr[20];
1545 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1546 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1547 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
1548 const char *two = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_TWO);
1550 /* Assume a new line will be added */
1551 insert_line = TRUE;
1553 /* Get register name */
1554 shader_arb_get_register_name(ins, &src->reg, regstr, &is_color);
1555 shader_arb_get_swizzle(src, is_color, swzstr);
1557 switch (src->modifiers)
1559 case WINED3DSPSM_NONE:
1560 sprintf(outregstr, "%s%s", regstr, swzstr);
1561 insert_line = FALSE;
1562 break;
1563 case WINED3DSPSM_NEG:
1564 sprintf(outregstr, "-%s%s", regstr, swzstr);
1565 insert_line = FALSE;
1566 break;
1567 case WINED3DSPSM_BIAS:
1568 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
1569 break;
1570 case WINED3DSPSM_BIASNEG:
1571 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
1572 break;
1573 case WINED3DSPSM_SIGN:
1574 shader_addline(buffer, "MAD T%c, %s, %s, -%s;\n", 'A' + tmpreg, regstr, two, one);
1575 break;
1576 case WINED3DSPSM_SIGNNEG:
1577 shader_addline(buffer, "MAD T%c, %s, -%s, %s;\n", 'A' + tmpreg, regstr, two, one);
1578 break;
1579 case WINED3DSPSM_COMP:
1580 shader_addline(buffer, "SUB T%c, %s, %s;\n", 'A' + tmpreg, one, regstr);
1581 break;
1582 case WINED3DSPSM_X2:
1583 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
1584 break;
1585 case WINED3DSPSM_X2NEG:
1586 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
1587 break;
1588 case WINED3DSPSM_DZ:
1589 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
1590 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1591 break;
1592 case WINED3DSPSM_DW:
1593 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
1594 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1595 break;
1596 case WINED3DSPSM_ABS:
1597 if(ctx->target_version >= NV2) {
1598 sprintf(outregstr, "|%s%s|", regstr, swzstr);
1599 insert_line = FALSE;
1600 } else {
1601 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1603 break;
1604 case WINED3DSPSM_ABSNEG:
1605 if(ctx->target_version >= NV2) {
1606 sprintf(outregstr, "-|%s%s|", regstr, swzstr);
1607 } else {
1608 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1609 sprintf(outregstr, "-T%c%s", 'A' + tmpreg, swzstr);
1611 insert_line = FALSE;
1612 break;
1613 default:
1614 sprintf(outregstr, "%s%s", regstr, swzstr);
1615 insert_line = FALSE;
1618 /* Return modified or original register, with swizzle */
1619 if (insert_line)
1620 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
1623 static void pshader_hw_bem(const struct wined3d_shader_instruction *ins)
1625 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1626 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1627 DWORD sampler_code = dst->reg.idx[0].offset;
1628 char dst_name[50];
1629 char src_name[2][50];
1631 shader_arb_get_dst_param(ins, dst, dst_name);
1633 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1635 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1636 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1637 * temps is done.
1639 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1640 shader_addline(buffer, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
1641 shader_addline(buffer, "DP3 TC.r, TA, %s;\n", src_name[1]);
1642 shader_addline(buffer, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
1643 shader_addline(buffer, "DP3 TC.g, TA, %s;\n", src_name[1]);
1645 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1646 shader_addline(buffer, "ADD %s, %s, TC;\n", dst_name, src_name[0]);
1649 static DWORD negate_modifiers(DWORD mod, char *extra_char)
1651 *extra_char = ' ';
1652 switch(mod)
1654 case WINED3DSPSM_NONE: return WINED3DSPSM_NEG;
1655 case WINED3DSPSM_NEG: return WINED3DSPSM_NONE;
1656 case WINED3DSPSM_BIAS: return WINED3DSPSM_BIASNEG;
1657 case WINED3DSPSM_BIASNEG: return WINED3DSPSM_BIAS;
1658 case WINED3DSPSM_SIGN: return WINED3DSPSM_SIGNNEG;
1659 case WINED3DSPSM_SIGNNEG: return WINED3DSPSM_SIGN;
1660 case WINED3DSPSM_COMP: *extra_char = '-'; return WINED3DSPSM_COMP;
1661 case WINED3DSPSM_X2: return WINED3DSPSM_X2NEG;
1662 case WINED3DSPSM_X2NEG: return WINED3DSPSM_X2;
1663 case WINED3DSPSM_DZ: *extra_char = '-'; return WINED3DSPSM_DZ;
1664 case WINED3DSPSM_DW: *extra_char = '-'; return WINED3DSPSM_DW;
1665 case WINED3DSPSM_ABS: return WINED3DSPSM_ABSNEG;
1666 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
1668 FIXME("Unknown modifier %u\n", mod);
1669 return mod;
1672 static void pshader_hw_cnd(const struct wined3d_shader_instruction *ins)
1674 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1675 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1676 char dst_name[50];
1677 char src_name[3][50];
1678 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1679 ins->ctx->reg_maps->shader_version.minor);
1681 shader_arb_get_dst_param(ins, dst, dst_name);
1682 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1684 if (shader_version <= WINED3D_SHADER_VERSION(1, 3) && ins->coissue
1685 && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
1687 shader_addline(buffer, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[1]);
1689 else
1691 struct wined3d_shader_src_param src0_copy = ins->src[0];
1692 char extra_neg;
1694 /* src0 may have a negate srcmod set, so we can't blindly add "-" to the name */
1695 src0_copy.modifiers = negate_modifiers(src0_copy.modifiers, &extra_neg);
1697 shader_arb_get_src_param(ins, &src0_copy, 0, src_name[0]);
1698 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1699 shader_addline(buffer, "ADD TA, %c%s, coefdiv.x;\n", extra_neg, src_name[0]);
1700 shader_addline(buffer, "CMP%s %s, TA, %s, %s;\n", shader_arb_get_modifier(ins),
1701 dst_name, src_name[1], src_name[2]);
1705 static void pshader_hw_cmp(const struct wined3d_shader_instruction *ins)
1707 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1708 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1709 char dst_name[50];
1710 char src_name[3][50];
1712 shader_arb_get_dst_param(ins, dst, dst_name);
1714 /* Generate input register names (with modifiers) */
1715 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1716 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1717 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1719 shader_addline(buffer, "CMP%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1720 dst_name, src_name[0], src_name[2], src_name[1]);
1723 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1724 * dst = dot2(src0, src1) + src2 */
1725 static void pshader_hw_dp2add(const struct wined3d_shader_instruction *ins)
1727 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1728 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1729 char dst_name[50];
1730 char src_name[3][50];
1731 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1733 shader_arb_get_dst_param(ins, dst, dst_name);
1734 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1735 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1737 if(ctx->target_version >= NV3)
1739 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1740 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1741 shader_addline(buffer, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1742 dst_name, src_name[0], src_name[1], src_name[2]);
1744 else if(ctx->target_version >= NV2)
1746 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1747 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1748 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1749 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1751 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1753 * .xyxy and other swizzles that we could get with this are not valid in
1754 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1756 struct wined3d_shader_src_param tmp_param = ins->src[1];
1757 DWORD swizzle = tmp_param.swizzle & 0xf; /* Selects .xy */
1758 tmp_param.swizzle = swizzle | (swizzle << 4); /* Creates .xyxy */
1760 shader_arb_get_src_param(ins, &tmp_param, 1, src_name[1]);
1762 shader_addline(buffer, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1763 dst_name, src_name[2], src_name[0], src_name[1]);
1765 else
1767 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1768 /* 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]
1769 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1771 shader_addline(buffer, "MOV TA, %s;\n", src_name[0]);
1772 shader_addline(buffer, "MOV TA.z, 0.0;\n");
1773 shader_addline(buffer, "DP3 TA, TA, %s;\n", src_name[1]);
1774 shader_addline(buffer, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[2]);
1778 /* Map the opcode 1-to-1 to the GL code */
1779 static void shader_hw_map2gl(const struct wined3d_shader_instruction *ins)
1781 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1782 const char *instruction;
1783 char arguments[256], dst_str[50];
1784 unsigned int i;
1785 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1787 switch (ins->handler_idx)
1789 case WINED3DSIH_ABS: instruction = "ABS"; break;
1790 case WINED3DSIH_ADD: instruction = "ADD"; break;
1791 case WINED3DSIH_CRS: instruction = "XPD"; break;
1792 case WINED3DSIH_DP3: instruction = "DP3"; break;
1793 case WINED3DSIH_DP4: instruction = "DP4"; break;
1794 case WINED3DSIH_DST: instruction = "DST"; break;
1795 case WINED3DSIH_FRC: instruction = "FRC"; break;
1796 case WINED3DSIH_LIT: instruction = "LIT"; break;
1797 case WINED3DSIH_LRP: instruction = "LRP"; break;
1798 case WINED3DSIH_MAD: instruction = "MAD"; break;
1799 case WINED3DSIH_MAX: instruction = "MAX"; break;
1800 case WINED3DSIH_MIN: instruction = "MIN"; break;
1801 case WINED3DSIH_MOV: instruction = "MOV"; break;
1802 case WINED3DSIH_MUL: instruction = "MUL"; break;
1803 case WINED3DSIH_SGE: instruction = "SGE"; break;
1804 case WINED3DSIH_SLT: instruction = "SLT"; break;
1805 case WINED3DSIH_SUB: instruction = "SUB"; break;
1806 case WINED3DSIH_MOVA:instruction = "ARR"; break;
1807 case WINED3DSIH_DSX: instruction = "DDX"; break;
1808 default: instruction = "";
1809 FIXME("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
1810 break;
1813 /* Note that shader_arb_add_dst_param() adds spaces. */
1814 arguments[0] = '\0';
1815 shader_arb_get_dst_param(ins, dst, dst_str);
1816 for (i = 0; i < ins->src_count; ++i)
1818 char operand[100];
1819 strcat(arguments, ", ");
1820 shader_arb_get_src_param(ins, &ins->src[i], i, operand);
1821 strcat(arguments, operand);
1823 shader_addline(buffer, "%s%s %s%s;\n", instruction, shader_arb_get_modifier(ins), dst_str, arguments);
1826 static void shader_hw_nop(const struct wined3d_shader_instruction *ins) {}
1828 static DWORD shader_arb_select_component(DWORD swizzle, DWORD component)
1830 return ((swizzle >> 2 * component) & 0x3) * 0x55;
1833 static void shader_hw_mov(const struct wined3d_shader_instruction *ins)
1835 const struct wined3d_shader *shader = ins->ctx->shader;
1836 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1837 BOOL pshader = shader_is_pshader_version(reg_maps->shader_version.type);
1838 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1839 const char *zero = arb_get_helper_value(reg_maps->shader_version.type, ARB_ZERO);
1840 const char *one = arb_get_helper_value(reg_maps->shader_version.type, ARB_ONE);
1841 const char *two = arb_get_helper_value(reg_maps->shader_version.type, ARB_TWO);
1843 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1844 char src0_param[256];
1846 if (ins->handler_idx == WINED3DSIH_MOVA)
1848 const struct arb_vshader_private *shader_data = shader->backend_data;
1849 char write_mask[6];
1850 const char *offset = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_VS_REL_OFFSET);
1852 if(ctx->target_version >= NV2) {
1853 shader_hw_map2gl(ins);
1854 return;
1856 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1857 shader_arb_get_write_mask(ins, &ins->dst[0], write_mask);
1859 /* This implements the mova formula used in GLSL. The first two instructions
1860 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1861 * in this case:
1862 * mova A0.x, 0.0
1864 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1866 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1867 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1869 shader_addline(buffer, "SGE A0_SHADOW%s, %s, %s;\n", write_mask, src0_param, zero);
1870 shader_addline(buffer, "MAD A0_SHADOW%s, A0_SHADOW, %s, -%s;\n", write_mask, two, one);
1872 shader_addline(buffer, "ABS TA%s, %s;\n", write_mask, src0_param);
1873 shader_addline(buffer, "ADD TA%s, TA, rel_addr_const.x;\n", write_mask);
1874 shader_addline(buffer, "FLR TA%s, TA;\n", write_mask);
1875 if (shader_data->rel_offset)
1877 shader_addline(buffer, "ADD TA%s, TA, %s;\n", write_mask, offset);
1879 shader_addline(buffer, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask);
1881 ((struct shader_arb_ctx_priv *)ins->ctx->backend_data)->addr_reg[0] = '\0';
1883 else if (reg_maps->shader_version.major == 1
1884 && !shader_is_pshader_version(reg_maps->shader_version.type)
1885 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1887 const struct arb_vshader_private *shader_data = shader->backend_data;
1888 src0_param[0] = '\0';
1890 if (shader_data->rel_offset && ctx->target_version == ARB)
1892 const char *offset = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_VS_REL_OFFSET);
1893 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1894 shader_addline(buffer, "ADD TA.x, %s, %s;\n", src0_param, offset);
1895 shader_addline(buffer, "ARL A0.x, TA.x;\n");
1897 else
1899 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1900 * with more than one component. Thus replicate the first source argument over all
1901 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1902 struct wined3d_shader_src_param tmp_src = ins->src[0];
1903 tmp_src.swizzle = shader_arb_select_component(tmp_src.swizzle, 0);
1904 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1905 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1908 else if (ins->dst[0].reg.type == WINED3DSPR_COLOROUT && !ins->dst[0].reg.idx[0].offset && pshader)
1910 if (ctx->ps_post_process && shader->u.ps.color0_mov)
1912 shader_addline(buffer, "#mov handled in srgb write or fog code\n");
1913 return;
1915 shader_hw_map2gl(ins);
1917 else
1919 shader_hw_map2gl(ins);
1923 static void pshader_hw_texkill(const struct wined3d_shader_instruction *ins)
1925 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1926 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
1927 char reg_dest[40];
1929 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1930 * but >= 2.0 honors it (undocumented, but tested by the d3d9 testsuite)
1932 shader_arb_get_dst_param(ins, dst, reg_dest);
1934 if (ins->ctx->reg_maps->shader_version.major >= 2)
1936 const char *kilsrc = "TA";
1937 BOOL is_color;
1939 shader_arb_get_register_name(ins, &dst->reg, reg_dest, &is_color);
1940 if(dst->write_mask == WINED3DSP_WRITEMASK_ALL)
1942 kilsrc = reg_dest;
1944 else
1946 /* Sigh. KIL doesn't support swizzles/writemasks. KIL passes a writemask, but ".xy" for example
1947 * is not valid as a swizzle in ARB (needs ".xyyy"). Use SWZ to load the register properly, and set
1948 * masked out components to 0(won't kill)
1950 char x = '0', y = '0', z = '0', w = '0';
1951 if(dst->write_mask & WINED3DSP_WRITEMASK_0) x = 'x';
1952 if(dst->write_mask & WINED3DSP_WRITEMASK_1) y = 'y';
1953 if(dst->write_mask & WINED3DSP_WRITEMASK_2) z = 'z';
1954 if(dst->write_mask & WINED3DSP_WRITEMASK_3) w = 'w';
1955 shader_addline(buffer, "SWZ TA, %s, %c, %c, %c, %c;\n", reg_dest, x, y, z, w);
1957 shader_addline(buffer, "KIL %s;\n", kilsrc);
1959 else
1961 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1962 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1964 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1965 * or pass in any temporary register(in shader phase 2)
1967 if (ins->ctx->reg_maps->shader_version.minor <= 3)
1968 sprintf(reg_dest, "fragment.texcoord[%u]", dst->reg.idx[0].offset);
1969 else
1970 shader_arb_get_dst_param(ins, dst, reg_dest);
1971 shader_addline(buffer, "SWZ TA, %s, x, y, z, 1;\n", reg_dest);
1972 shader_addline(buffer, "KIL TA;\n");
1976 static void pshader_hw_tex(const struct wined3d_shader_instruction *ins)
1978 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1979 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1980 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1981 ins->ctx->reg_maps->shader_version.minor);
1982 struct wined3d_shader_src_param src;
1984 char reg_dest[40];
1985 char reg_coord[40];
1986 DWORD reg_sampler_code;
1987 WORD myflags = 0;
1988 BOOL swizzle_coord = FALSE;
1990 /* All versions have a destination register */
1991 shader_arb_get_dst_param(ins, dst, reg_dest);
1993 /* 1.0-1.4: Use destination register number as texture code.
1994 2.0+: Use provided sampler number as texture code. */
1995 if (shader_version < WINED3D_SHADER_VERSION(2,0))
1996 reg_sampler_code = dst->reg.idx[0].offset;
1997 else
1998 reg_sampler_code = ins->src[1].reg.idx[0].offset;
2000 /* 1.0-1.3: Use the texcoord varying.
2001 1.4+: Use provided coordinate source register. */
2002 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2003 sprintf(reg_coord, "fragment.texcoord[%u]", reg_sampler_code);
2004 else {
2005 /* TEX is the only instruction that can handle DW and DZ natively */
2006 src = ins->src[0];
2007 if(src.modifiers == WINED3DSPSM_DW) src.modifiers = WINED3DSPSM_NONE;
2008 if(src.modifiers == WINED3DSPSM_DZ) src.modifiers = WINED3DSPSM_NONE;
2009 shader_arb_get_src_param(ins, &src, 0, reg_coord);
2012 /* projection flag:
2013 * 1.1, 1.2, 1.3: Use WINED3D_TSS_TEXTURETRANSFORMFLAGS
2014 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
2015 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
2017 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2019 DWORD flags = 0;
2020 if (reg_sampler_code < MAX_TEXTURES)
2021 flags = priv->cur_ps_args->super.tex_transform >> reg_sampler_code * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2022 if (flags & WINED3D_PSARGS_PROJECTED)
2024 myflags |= TEX_PROJ;
2025 if ((flags & ~WINED3D_PSARGS_PROJECTED) == WINED3D_TTFF_COUNT3)
2026 swizzle_coord = TRUE;
2029 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
2031 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
2032 if (src_mod == WINED3DSPSM_DZ)
2034 swizzle_coord = TRUE;
2035 myflags |= TEX_PROJ;
2036 } else if(src_mod == WINED3DSPSM_DW) {
2037 myflags |= TEX_PROJ;
2039 } else {
2040 if (ins->flags & WINED3DSI_TEXLD_PROJECT) myflags |= TEX_PROJ;
2041 if (ins->flags & WINED3DSI_TEXLD_BIAS) myflags |= TEX_BIAS;
2044 if (swizzle_coord)
2046 /* TXP cannot handle DZ natively, so move the z coordinate to .w.
2047 * reg_coord is a read-only varying register, so we need a temp reg */
2048 shader_addline(ins->ctx->buffer, "SWZ TA, %s, x, y, z, z;\n", reg_coord);
2049 strcpy(reg_coord, "TA");
2052 shader_hw_sample(ins, reg_sampler_code, reg_dest, reg_coord, myflags, NULL, NULL);
2055 static void pshader_hw_texcoord(const struct wined3d_shader_instruction *ins)
2057 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2058 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2059 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2060 ins->ctx->reg_maps->shader_version.minor);
2061 char dst_str[50];
2063 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2065 DWORD reg = dst->reg.idx[0].offset;
2067 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2068 shader_addline(buffer, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str, reg);
2069 } else {
2070 char reg_src[40];
2072 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src);
2073 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2074 shader_addline(buffer, "MOV %s, %s;\n", dst_str, reg_src);
2078 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction *ins)
2080 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2081 DWORD flags = 0;
2083 DWORD reg1 = ins->dst[0].reg.idx[0].offset;
2084 char dst_str[50];
2085 char src_str[50];
2087 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
2088 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2089 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
2090 /* Move .x first in case src_str is "TA" */
2091 shader_addline(buffer, "MOV TA.y, %s.x;\n", src_str);
2092 shader_addline(buffer, "MOV TA.x, %s.w;\n", src_str);
2093 if (reg1 < MAX_TEXTURES)
2095 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2096 flags = priv->cur_ps_args->super.tex_transform >> reg1 * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2098 shader_hw_sample(ins, reg1, dst_str, "TA", flags & WINED3D_PSARGS_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2101 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction *ins)
2103 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2105 DWORD reg1 = ins->dst[0].reg.idx[0].offset;
2106 char dst_str[50];
2107 char src_str[50];
2109 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
2110 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2111 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
2112 shader_addline(buffer, "MOV TA.x, %s.y;\n", src_str);
2113 shader_addline(buffer, "MOV TA.y, %s.z;\n", src_str);
2114 shader_hw_sample(ins, reg1, dst_str, "TA", 0, NULL, NULL);
2117 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction *ins)
2119 DWORD reg1 = ins->dst[0].reg.idx[0].offset;
2120 char dst_str[50];
2121 char src_str[50];
2123 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
2124 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2125 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
2126 shader_hw_sample(ins, reg1, dst_str, src_str, 0, NULL, NULL);
2129 static void pshader_hw_texbem(const struct wined3d_shader_instruction *ins)
2131 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2132 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2133 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2134 char reg_coord[40], dst_reg[50], src_reg[50];
2135 DWORD reg_dest_code;
2137 /* All versions have a destination register. The Tx where the texture coordinates come
2138 * from is the varying incarnation of the texture register
2140 reg_dest_code = dst->reg.idx[0].offset;
2141 shader_arb_get_dst_param(ins, &ins->dst[0], dst_reg);
2142 shader_arb_get_src_param(ins, &ins->src[0], 0, src_reg);
2143 sprintf(reg_coord, "fragment.texcoord[%u]", reg_dest_code);
2145 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
2146 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
2148 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
2149 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
2151 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
2152 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
2153 * extension.
2155 shader_addline(buffer, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
2156 shader_addline(buffer, "DP3 TA.x, TB, %s;\n", src_reg);
2157 shader_addline(buffer, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
2158 shader_addline(buffer, "DP3 TA.y, TB, %s;\n", src_reg);
2160 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2161 * so we can't let the GL handle this.
2163 if ((priv->cur_ps_args->super.tex_transform >> reg_dest_code * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
2164 & WINED3D_PSARGS_PROJECTED)
2166 shader_addline(buffer, "RCP TB.w, %s.w;\n", reg_coord);
2167 shader_addline(buffer, "MUL TB.xy, %s, TB.w;\n", reg_coord);
2168 shader_addline(buffer, "ADD TA.xy, TA, TB;\n");
2169 } else {
2170 shader_addline(buffer, "ADD TA.xy, TA, %s;\n", reg_coord);
2173 shader_hw_sample(ins, reg_dest_code, dst_reg, "TA", 0, NULL, NULL);
2175 if (ins->handler_idx == WINED3DSIH_TEXBEML)
2177 /* No src swizzles are allowed, so this is ok */
2178 shader_addline(buffer, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
2179 src_reg, reg_dest_code, reg_dest_code);
2180 shader_addline(buffer, "MUL %s, %s, TA;\n", dst_reg, dst_reg);
2184 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction *ins)
2186 DWORD reg = ins->dst[0].reg.idx[0].offset;
2187 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2188 char src0_name[50], dst_name[50];
2189 BOOL is_color;
2190 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
2192 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2193 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
2194 * T<reg+1> register. Use this register to store the calculated vector
2196 tmp_reg.idx[0].offset = reg + 1;
2197 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
2198 shader_addline(buffer, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
2201 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction *ins)
2203 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2204 DWORD flags;
2205 DWORD reg = ins->dst[0].reg.idx[0].offset;
2206 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2207 char dst_str[50];
2208 char src0_name[50];
2209 char dst_reg[50];
2210 BOOL is_color;
2212 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
2213 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2215 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2216 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2217 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2218 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2219 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3D_PSARGS_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2222 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction *ins)
2224 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2225 DWORD reg = ins->dst[0].reg.idx[0].offset;
2226 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2227 char src0_name[50], dst_name[50];
2228 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
2229 BOOL is_color;
2231 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
2232 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
2233 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
2235 tmp_reg.idx[0].offset = reg + 2 - tex_mx->current_row;
2236 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
2238 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2239 shader_addline(buffer, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
2240 dst_name, 'x' + tex_mx->current_row, reg, src0_name);
2241 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
2244 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction *ins)
2246 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2247 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2248 DWORD flags;
2249 DWORD reg = ins->dst[0].reg.idx[0].offset;
2250 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2251 char dst_str[50];
2252 char src0_name[50], dst_name[50];
2253 BOOL is_color;
2255 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2256 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2257 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
2259 /* Sample the texture using the calculated coordinates */
2260 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2261 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2262 shader_hw_sample(ins, reg, dst_str, dst_name, flags & WINED3D_PSARGS_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2263 tex_mx->current_row = 0;
2266 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2268 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2269 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2270 DWORD flags;
2271 DWORD reg = ins->dst[0].reg.idx[0].offset;
2272 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2273 char dst_str[50];
2274 char src0_name[50];
2275 char dst_reg[50];
2276 BOOL is_color;
2278 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
2279 * components for temporary data storage
2281 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2282 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2283 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2285 /* Construct the eye-ray vector from w coordinates */
2286 shader_addline(buffer, "MOV TB.x, fragment.texcoord[%u].w;\n", tex_mx->texcoord_w[0]);
2287 shader_addline(buffer, "MOV TB.y, fragment.texcoord[%u].w;\n", tex_mx->texcoord_w[1]);
2288 shader_addline(buffer, "MOV TB.z, fragment.texcoord[%u].w;\n", reg);
2290 /* Calculate reflection vector
2292 shader_addline(buffer, "DP3 %s.w, %s, TB;\n", dst_reg, dst_reg);
2293 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
2294 shader_addline(buffer, "DP3 TB.w, %s, %s;\n", dst_reg, dst_reg);
2295 shader_addline(buffer, "RCP TB.w, TB.w;\n");
2296 shader_addline(buffer, "MUL %s.w, %s.w, TB.w;\n", dst_reg, dst_reg);
2297 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2298 shader_addline(buffer, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg, dst_reg);
2300 /* Sample the texture using the calculated coordinates */
2301 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2302 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2303 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3D_PSARGS_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2304 tex_mx->current_row = 0;
2307 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction *ins)
2309 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2310 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2311 DWORD flags;
2312 DWORD reg = ins->dst[0].reg.idx[0].offset;
2313 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2314 char dst_str[50];
2315 char src0_name[50];
2316 char src1_name[50];
2317 char dst_reg[50];
2318 BOOL is_color;
2320 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2321 shader_arb_get_src_param(ins, &ins->src[0], 1, src1_name);
2322 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2323 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
2324 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2326 /* Calculate reflection vector.
2328 * dot(N, E)
2329 * dst_reg.xyz = 2 * --------- * N - E
2330 * dot(N, N)
2332 * Which normalizes the normal vector
2334 shader_addline(buffer, "DP3 %s.w, %s, %s;\n", dst_reg, dst_reg, src1_name);
2335 shader_addline(buffer, "DP3 TC.w, %s, %s;\n", dst_reg, dst_reg);
2336 shader_addline(buffer, "RCP TC.w, TC.w;\n");
2337 shader_addline(buffer, "MUL %s.w, %s.w, TC.w;\n", dst_reg, dst_reg);
2338 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2339 shader_addline(buffer, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg, dst_reg, src1_name);
2341 /* Sample the texture using the calculated coordinates */
2342 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2343 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2344 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3D_PSARGS_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2345 tex_mx->current_row = 0;
2348 static void pshader_hw_texdepth(const struct wined3d_shader_instruction *ins)
2350 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2351 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2352 char dst_name[50];
2353 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2354 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2356 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
2357 * which is essentially an input, is the destination register because it is the first
2358 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
2359 * here(writemasks/swizzles are not valid on texdepth)
2361 shader_arb_get_dst_param(ins, dst, dst_name);
2363 /* According to the msdn, the source register(must be r5) is unusable after
2364 * the texdepth instruction, so we're free to modify it
2366 shader_addline(buffer, "MIN %s.y, %s.y, %s;\n", dst_name, dst_name, one);
2368 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2369 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2370 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2372 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2373 shader_addline(buffer, "MUL TA.x, %s.x, %s.y;\n", dst_name, dst_name);
2374 shader_addline(buffer, "MIN TA.x, TA.x, %s;\n", one);
2375 shader_addline(buffer, "MAX result.depth, TA.x, %s;\n", zero);
2378 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
2379 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2380 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2381 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction *ins)
2383 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2384 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
2385 char src0[50];
2386 char dst_str[50];
2388 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2389 shader_addline(buffer, "MOV TB, 0.0;\n");
2390 shader_addline(buffer, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx, src0);
2392 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2393 shader_hw_sample(ins, sampler_idx, dst_str, "TB", 0 /* Only one coord, can't be projected */, NULL, NULL);
2396 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2397 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2398 static void pshader_hw_texdp3(const struct wined3d_shader_instruction *ins)
2400 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2401 char src0[50];
2402 char dst_str[50];
2403 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2405 /* Handle output register */
2406 shader_arb_get_dst_param(ins, dst, dst_str);
2407 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2408 shader_addline(buffer, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str, dst->reg.idx[0].offset, src0);
2411 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2412 * Perform the 3rd row of a 3x3 matrix multiply */
2413 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction *ins)
2415 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2416 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2417 char dst_str[50], dst_name[50];
2418 char src0[50];
2419 BOOL is_color;
2421 shader_arb_get_dst_param(ins, dst, dst_str);
2422 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2423 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2424 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx[0].offset, src0);
2425 shader_addline(buffer, "MOV %s, %s;\n", dst_str, dst_name);
2428 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2429 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2430 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2431 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2433 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction *ins)
2435 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2436 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2437 char src0[50], dst_name[50];
2438 BOOL is_color;
2439 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2440 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2442 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2443 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2444 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx[0].offset, src0);
2446 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2447 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2448 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2450 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2451 shader_addline(buffer, "MUL %s.x, %s.x, %s.y;\n", dst_name, dst_name, dst_name);
2452 shader_addline(buffer, "MIN %s.x, %s.x, %s;\n", dst_name, dst_name, one);
2453 shader_addline(buffer, "MAX result.depth, %s.x, %s;\n", dst_name, zero);
2456 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2457 Vertex/Pixel shaders to ARB_vertex_program codes */
2458 static void shader_hw_mnxn(const struct wined3d_shader_instruction *ins)
2460 int i;
2461 int nComponents = 0;
2462 struct wined3d_shader_dst_param tmp_dst = {{0}};
2463 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2464 struct wined3d_shader_instruction tmp_ins;
2466 memset(&tmp_ins, 0, sizeof(tmp_ins));
2468 /* Set constants for the temporary argument */
2469 tmp_ins.ctx = ins->ctx;
2470 tmp_ins.dst_count = 1;
2471 tmp_ins.dst = &tmp_dst;
2472 tmp_ins.src_count = 2;
2473 tmp_ins.src = tmp_src;
2475 switch(ins->handler_idx)
2477 case WINED3DSIH_M4x4:
2478 nComponents = 4;
2479 tmp_ins.handler_idx = WINED3DSIH_DP4;
2480 break;
2481 case WINED3DSIH_M4x3:
2482 nComponents = 3;
2483 tmp_ins.handler_idx = WINED3DSIH_DP4;
2484 break;
2485 case WINED3DSIH_M3x4:
2486 nComponents = 4;
2487 tmp_ins.handler_idx = WINED3DSIH_DP3;
2488 break;
2489 case WINED3DSIH_M3x3:
2490 nComponents = 3;
2491 tmp_ins.handler_idx = WINED3DSIH_DP3;
2492 break;
2493 case WINED3DSIH_M3x2:
2494 nComponents = 2;
2495 tmp_ins.handler_idx = WINED3DSIH_DP3;
2496 break;
2497 default:
2498 FIXME("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
2499 break;
2502 tmp_dst = ins->dst[0];
2503 tmp_src[0] = ins->src[0];
2504 tmp_src[1] = ins->src[1];
2505 for (i = 0; i < nComponents; ++i)
2507 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2508 shader_hw_map2gl(&tmp_ins);
2509 ++tmp_src[1].reg.idx[0].offset;
2513 static DWORD abs_modifier(DWORD mod, BOOL *need_abs)
2515 *need_abs = FALSE;
2517 switch(mod)
2519 case WINED3DSPSM_NONE: return WINED3DSPSM_ABS;
2520 case WINED3DSPSM_NEG: return WINED3DSPSM_ABS;
2521 case WINED3DSPSM_BIAS: *need_abs = TRUE; return WINED3DSPSM_BIAS;
2522 case WINED3DSPSM_BIASNEG: *need_abs = TRUE; return WINED3DSPSM_BIASNEG;
2523 case WINED3DSPSM_SIGN: *need_abs = TRUE; return WINED3DSPSM_SIGN;
2524 case WINED3DSPSM_SIGNNEG: *need_abs = TRUE; return WINED3DSPSM_SIGNNEG;
2525 case WINED3DSPSM_COMP: *need_abs = TRUE; return WINED3DSPSM_COMP;
2526 case WINED3DSPSM_X2: *need_abs = TRUE; return WINED3DSPSM_X2;
2527 case WINED3DSPSM_X2NEG: *need_abs = TRUE; return WINED3DSPSM_X2NEG;
2528 case WINED3DSPSM_DZ: *need_abs = TRUE; return WINED3DSPSM_DZ;
2529 case WINED3DSPSM_DW: *need_abs = TRUE; return WINED3DSPSM_DW;
2530 case WINED3DSPSM_ABS: return WINED3DSPSM_ABS;
2531 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
2533 FIXME("Unknown modifier %u\n", mod);
2534 return mod;
2537 static void shader_hw_scalar_op(const struct wined3d_shader_instruction *ins)
2539 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2540 const char *instruction;
2541 struct wined3d_shader_src_param src0_copy = ins->src[0];
2542 BOOL need_abs = FALSE;
2544 char dst[50];
2545 char src[50];
2547 switch(ins->handler_idx)
2549 case WINED3DSIH_RSQ: instruction = "RSQ"; break;
2550 case WINED3DSIH_RCP: instruction = "RCP"; break;
2551 case WINED3DSIH_EXPP:
2552 if (ins->ctx->reg_maps->shader_version.major < 2)
2554 instruction = "EXP";
2555 break;
2557 /* Drop through. */
2558 case WINED3DSIH_EXP:
2559 instruction = "EX2";
2560 break;
2561 case WINED3DSIH_LOG:
2562 case WINED3DSIH_LOGP:
2563 /* The precision requirements suggest that LOGP matches ARBvp's LOG
2564 * instruction, but notice that the output of those instructions is
2565 * different. */
2566 src0_copy.modifiers = abs_modifier(src0_copy.modifiers, &need_abs);
2567 instruction = "LG2";
2568 break;
2569 default: instruction = "";
2570 FIXME("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
2571 break;
2574 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2575 * .w is used. */
2576 src0_copy.swizzle = shader_arb_select_component(src0_copy.swizzle, 3);
2578 shader_arb_get_dst_param(ins, &ins->dst[0], dst); /* Destination */
2579 shader_arb_get_src_param(ins, &src0_copy, 0, src);
2581 if(need_abs)
2583 shader_addline(buffer, "ABS TA.w, %s;\n", src);
2584 shader_addline(buffer, "%s%s %s, TA.w;\n", instruction, shader_arb_get_modifier(ins), dst);
2586 else
2588 shader_addline(buffer, "%s%s %s, %s;\n", instruction, shader_arb_get_modifier(ins), dst, src);
2593 static void shader_hw_nrm(const struct wined3d_shader_instruction *ins)
2595 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2596 char dst_name[50];
2597 char src_name[50];
2598 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2599 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
2600 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2602 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2603 shader_arb_get_src_param(ins, &ins->src[0], 1 /* Use TB */, src_name);
2605 /* In D3D, NRM of a vector with length zero returns zero. Catch this situation, as
2606 * otherwise NRM or RSQ would return NaN */
2607 if(pshader && priv->target_version >= NV3)
2609 /* GL_NV_fragment_program2's NRM needs protection against length zero vectors too
2611 * TODO: Find out if DP3+NRM+MOV is really faster than DP3+RSQ+MUL
2613 shader_addline(buffer, "DP3C TA, %s, %s;\n", src_name, src_name);
2614 shader_addline(buffer, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name);
2615 shader_addline(buffer, "MOV %s (EQ), %s;\n", dst_name, zero);
2617 else if(priv->target_version >= NV2)
2619 shader_addline(buffer, "DP3C TA.x, %s, %s;\n", src_name, src_name);
2620 shader_addline(buffer, "RSQ TA.x (NE), TA.x;\n");
2621 shader_addline(buffer, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins), dst_name,
2622 src_name);
2624 else
2626 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2628 shader_addline(buffer, "DP3 TA.x, %s, %s;\n", src_name, src_name);
2629 /* Pass any non-zero value to RSQ if the input vector has a length of zero. The
2630 * RSQ result doesn't matter, as long as multiplying it by 0 returns 0.
2632 shader_addline(buffer, "SGE TA.y, -TA.x, %s;\n", zero);
2633 shader_addline(buffer, "MAD TA.x, %s, TA.y, TA.x;\n", one);
2635 shader_addline(buffer, "RSQ TA.x, TA.x;\n");
2636 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2637 shader_addline(buffer, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins), dst_name,
2638 src_name);
2642 static void shader_hw_lrp(const struct wined3d_shader_instruction *ins)
2644 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2645 char dst_name[50];
2646 char src_name[3][50];
2648 /* ARB_fragment_program has a convenient LRP instruction */
2649 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2650 shader_hw_map2gl(ins);
2651 return;
2654 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2655 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
2656 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
2657 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
2659 shader_addline(buffer, "SUB TA, %s, %s;\n", src_name[1], src_name[2]);
2660 shader_addline(buffer, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins),
2661 dst_name, src_name[0], src_name[2]);
2664 static void shader_hw_sincos(const struct wined3d_shader_instruction *ins)
2666 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2667 * must contain fixed constants. So we need a separate function to filter those constants and
2668 * can't use map2gl
2670 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2671 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2672 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2673 char dst_name[50];
2674 char src_name0[50], src_name1[50], src_name2[50];
2675 BOOL is_color;
2677 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2678 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2679 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2680 /* No modifiers are supported on SCS */
2681 shader_addline(buffer, "SCS %s, %s;\n", dst_name, src_name0);
2683 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
2685 shader_arb_get_register_name(ins, &dst->reg, src_name0, &is_color);
2686 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name0);
2688 } else if(priv->target_version >= NV2) {
2689 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2691 /* Sincos writemask must be .x, .y or .xy */
2692 if(dst->write_mask & WINED3DSP_WRITEMASK_0)
2693 shader_addline(buffer, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2694 if(dst->write_mask & WINED3DSP_WRITEMASK_1)
2695 shader_addline(buffer, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2696 } else {
2697 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2698 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2700 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2701 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2703 * The constants we get are:
2705 * +1 +1, -1 -1 +1 +1 -1 -1
2706 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2707 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2709 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2711 * (x/2)^2 = x^2 / 4
2712 * (x/2)^3 = x^3 / 8
2713 * (x/2)^4 = x^4 / 16
2714 * (x/2)^5 = x^5 / 32
2715 * etc
2717 * To get the final result:
2718 * sin(x) = 2 * sin(x/2) * cos(x/2)
2719 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2720 * (from sin(x+y) and cos(x+y) rules)
2722 * As per MSDN, dst.z is undefined after the operation, and so is
2723 * dst.x and dst.y if they're masked out by the writemask. Ie
2724 * sincos dst.y, src1, c0, c1
2725 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2726 * vsa.exe also stops with an error if the dest register is the same register as the source
2727 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2728 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2730 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2731 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name2);
2732 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2734 shader_addline(buffer, "MUL %s.x, %s, %s;\n", dst_name, src_name0, src_name0); /* x ^ 2 */
2735 shader_addline(buffer, "MUL TA.y, %s.x, %s;\n", dst_name, src_name0); /* x ^ 3 */
2736 shader_addline(buffer, "MUL %s.y, TA.y, %s;\n", dst_name, src_name0); /* x ^ 4 */
2737 shader_addline(buffer, "MUL TA.z, %s.y, %s;\n", dst_name, src_name0); /* x ^ 5 */
2738 shader_addline(buffer, "MUL %s.z, TA.z, %s;\n", dst_name, src_name0); /* x ^ 6 */
2739 shader_addline(buffer, "MUL TA.w, %s.z, %s;\n", dst_name, src_name0); /* x ^ 7 */
2741 /* sin(x/2)
2743 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2744 * properly merge that with MULs in the code above?
2745 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2746 * we can merge the sine and cosine MAD rows to calculate them together.
2748 shader_addline(buffer, "MUL TA.x, %s, %s.w;\n", src_name0, src_name2); /* x^1, +1/(1!*2) */
2749 shader_addline(buffer, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2); /* -1/(3!*8) */
2750 shader_addline(buffer, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1); /* +1/(5!*32) */
2751 shader_addline(buffer, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1); /* -1/(7!*128) */
2753 /* cos(x/2) */
2754 shader_addline(buffer, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name, src_name2, src_name2); /* -1/(2!*4), +1.0 */
2755 shader_addline(buffer, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name, src_name1); /* +1/(4!*16) */
2756 shader_addline(buffer, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name, src_name1); /* -1/(6!*64) */
2758 if(dst->write_mask & WINED3DSP_WRITEMASK_0) {
2759 /* cos x */
2760 shader_addline(buffer, "MUL TA.z, TA.y, TA.y;\n");
2761 shader_addline(buffer, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name);
2763 if(dst->write_mask & WINED3DSP_WRITEMASK_1) {
2764 /* sin x */
2765 shader_addline(buffer, "MUL %s.y, TA.x, TA.y;\n", dst_name);
2766 shader_addline(buffer, "ADD %s.y, %s.y, %s.y;\n", dst_name, dst_name, dst_name);
2771 static void shader_hw_sgn(const struct wined3d_shader_instruction *ins)
2773 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2774 char dst_name[50];
2775 char src_name[50];
2776 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
2778 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2779 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2781 /* SGN is only valid in vertex shaders */
2782 if(ctx->target_version >= NV2) {
2783 shader_addline(buffer, "SSG%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name);
2784 return;
2787 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2788 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2790 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) {
2791 shader_addline(buffer, "SLT %s, -%s, %s;\n", dst_name, src_name, src_name);
2792 } else {
2793 /* src contains TA? Write to the dest first. This won't overwrite our destination.
2794 * Then use TA, and calculate the final result
2796 * Not reading from TA? Store the first result in TA to avoid overwriting the
2797 * destination if src reg = dst reg
2799 if(strstr(src_name, "TA"))
2801 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2802 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2803 shader_addline(buffer, "ADD %s, %s, -TA;\n", dst_name, dst_name);
2805 else
2807 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2808 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2809 shader_addline(buffer, "ADD %s, TA, -%s;\n", dst_name, dst_name);
2814 static void shader_hw_dsy(const struct wined3d_shader_instruction *ins)
2816 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2817 char src[50];
2818 char dst[50];
2819 char dst_name[50];
2820 BOOL is_color;
2822 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2823 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2824 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2826 shader_addline(buffer, "DDY %s, %s;\n", dst, src);
2827 shader_addline(buffer, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins), dst, dst_name);
2830 static void shader_hw_pow(const struct wined3d_shader_instruction *ins)
2832 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2833 char src0[50], src1[50], dst[50];
2834 struct wined3d_shader_src_param src0_copy = ins->src[0];
2835 BOOL need_abs = FALSE;
2836 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2837 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2839 /* POW operates on the absolute value of the input */
2840 src0_copy.modifiers = abs_modifier(src0_copy.modifiers, &need_abs);
2842 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2843 shader_arb_get_src_param(ins, &src0_copy, 0, src0);
2844 shader_arb_get_src_param(ins, &ins->src[1], 1, src1);
2846 if (need_abs)
2847 shader_addline(buffer, "ABS TA.x, %s;\n", src0);
2848 else
2849 shader_addline(buffer, "MOV TA.x, %s;\n", src0);
2851 if (priv->target_version >= NV2)
2853 shader_addline(buffer, "MOVC TA.y, %s;\n", src1);
2854 shader_addline(buffer, "POW%s %s, TA.x, TA.y;\n", shader_arb_get_modifier(ins), dst);
2855 shader_addline(buffer, "MOV %s (EQ.y), %s;\n", dst, one);
2857 else
2859 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2860 const char *flt_eps = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_EPS);
2862 shader_addline(buffer, "ABS TA.y, %s;\n", src1);
2863 shader_addline(buffer, "SGE TA.y, -TA.y, %s;\n", zero);
2864 /* Possibly add flt_eps to avoid getting float special values */
2865 shader_addline(buffer, "MAD TA.z, TA.y, %s, %s;\n", flt_eps, src1);
2866 shader_addline(buffer, "POW%s TA.x, TA.x, TA.z;\n", shader_arb_get_modifier(ins));
2867 shader_addline(buffer, "MAD TA.x, -TA.x, TA.y, TA.x;\n");
2868 shader_addline(buffer, "MAD %s, TA.y, %s, TA.x;\n", dst, one);
2872 static void shader_hw_loop(const struct wined3d_shader_instruction *ins)
2874 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2875 char src_name[50];
2876 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2878 /* src0 is aL */
2879 shader_arb_get_src_param(ins, &ins->src[1], 0, src_name);
2881 if(vshader)
2883 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2884 struct list *e = list_head(&priv->control_frames);
2885 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2887 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2888 /* The constant loader makes sure to load -1 into iX.w */
2889 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2890 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->no.loop);
2891 shader_addline(buffer, "loop_%u_start:\n", control_frame->no.loop);
2893 else
2895 shader_addline(buffer, "LOOP %s;\n", src_name);
2899 static void shader_hw_rep(const struct wined3d_shader_instruction *ins)
2901 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2902 char src_name[50];
2903 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2905 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2907 /* The constant loader makes sure to load -1 into iX.w */
2908 if(vshader)
2910 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2911 struct list *e = list_head(&priv->control_frames);
2912 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2914 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2916 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2917 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->no.loop);
2918 shader_addline(buffer, "loop_%u_start:\n", control_frame->no.loop);
2920 else
2922 shader_addline(buffer, "REP %s;\n", src_name);
2926 static void shader_hw_endloop(const struct wined3d_shader_instruction *ins)
2928 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2929 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2931 if(vshader)
2933 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2934 struct list *e = list_head(&priv->control_frames);
2935 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2937 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2938 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->no.loop);
2939 shader_addline(buffer, "loop_%u_end:\n", control_frame->no.loop);
2941 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2943 else
2945 shader_addline(buffer, "ENDLOOP;\n");
2949 static void shader_hw_endrep(const struct wined3d_shader_instruction *ins)
2951 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2952 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2954 if(vshader)
2956 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2957 struct list *e = list_head(&priv->control_frames);
2958 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2960 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2961 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->no.loop);
2962 shader_addline(buffer, "loop_%u_end:\n", control_frame->no.loop);
2964 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2966 else
2968 shader_addline(buffer, "ENDREP;\n");
2972 static const struct control_frame *find_last_loop(const struct shader_arb_ctx_priv *priv)
2974 struct control_frame *control_frame;
2976 LIST_FOR_EACH_ENTRY(control_frame, &priv->control_frames, struct control_frame, entry)
2978 if(control_frame->type == LOOP || control_frame->type == REP) return control_frame;
2980 ERR("Could not find loop for break\n");
2981 return NULL;
2984 static void shader_hw_break(const struct wined3d_shader_instruction *ins)
2986 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2987 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2988 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2990 if(vshader)
2992 shader_addline(buffer, "BRA loop_%u_end;\n", control_frame->no.loop);
2994 else
2996 shader_addline(buffer, "BRK;\n");
3000 static const char *get_compare(enum wined3d_shader_rel_op op)
3002 switch (op)
3004 case WINED3D_SHADER_REL_OP_GT: return "GT";
3005 case WINED3D_SHADER_REL_OP_EQ: return "EQ";
3006 case WINED3D_SHADER_REL_OP_GE: return "GE";
3007 case WINED3D_SHADER_REL_OP_LT: return "LT";
3008 case WINED3D_SHADER_REL_OP_NE: return "NE";
3009 case WINED3D_SHADER_REL_OP_LE: return "LE";
3010 default:
3011 FIXME("Unrecognized operator %#x.\n", op);
3012 return "(\?\?)";
3016 static enum wined3d_shader_rel_op invert_compare(enum wined3d_shader_rel_op op)
3018 switch (op)
3020 case WINED3D_SHADER_REL_OP_GT: return WINED3D_SHADER_REL_OP_LE;
3021 case WINED3D_SHADER_REL_OP_EQ: return WINED3D_SHADER_REL_OP_NE;
3022 case WINED3D_SHADER_REL_OP_GE: return WINED3D_SHADER_REL_OP_LT;
3023 case WINED3D_SHADER_REL_OP_LT: return WINED3D_SHADER_REL_OP_GE;
3024 case WINED3D_SHADER_REL_OP_NE: return WINED3D_SHADER_REL_OP_EQ;
3025 case WINED3D_SHADER_REL_OP_LE: return WINED3D_SHADER_REL_OP_GT;
3026 default:
3027 FIXME("Unrecognized operator %#x.\n", op);
3028 return -1;
3032 static void shader_hw_breakc(const struct wined3d_shader_instruction *ins)
3034 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3035 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3036 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
3037 char src_name0[50];
3038 char src_name1[50];
3039 const char *comp = get_compare(ins->flags);
3041 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
3042 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
3044 if(vshader)
3046 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
3047 * away the subtraction result
3049 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
3050 shader_addline(buffer, "BRA loop_%u_end (%s.x);\n", control_frame->no.loop, comp);
3052 else
3054 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
3055 shader_addline(buffer, "BRK (%s.x);\n", comp);
3059 static void shader_hw_ifc(const struct wined3d_shader_instruction *ins)
3061 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3062 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3063 struct list *e = list_head(&priv->control_frames);
3064 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
3065 const char *comp;
3066 char src_name0[50];
3067 char src_name1[50];
3068 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3070 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
3071 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
3073 if(vshader)
3075 /* Invert the flag. We jump to the else label if the condition is NOT true */
3076 comp = get_compare(invert_compare(ins->flags));
3077 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
3078 shader_addline(buffer, "BRA ifc_%u_else (%s.x);\n", control_frame->no.ifc, comp);
3080 else
3082 comp = get_compare(ins->flags);
3083 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
3084 shader_addline(buffer, "IF %s.x;\n", comp);
3088 static void shader_hw_else(const struct wined3d_shader_instruction *ins)
3090 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3091 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3092 struct list *e = list_head(&priv->control_frames);
3093 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
3094 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3096 if(vshader)
3098 shader_addline(buffer, "BRA ifc_%u_endif;\n", control_frame->no.ifc);
3099 shader_addline(buffer, "ifc_%u_else:\n", control_frame->no.ifc);
3100 control_frame->had_else = TRUE;
3102 else
3104 shader_addline(buffer, "ELSE;\n");
3108 static void shader_hw_endif(const struct wined3d_shader_instruction *ins)
3110 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3111 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3112 struct list *e = list_head(&priv->control_frames);
3113 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
3114 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3116 if(vshader)
3118 if(control_frame->had_else)
3120 shader_addline(buffer, "ifc_%u_endif:\n", control_frame->no.ifc);
3122 else
3124 shader_addline(buffer, "#No else branch. else is endif\n");
3125 shader_addline(buffer, "ifc_%u_else:\n", control_frame->no.ifc);
3128 else
3130 shader_addline(buffer, "ENDIF;\n");
3134 static void shader_hw_texldd(const struct wined3d_shader_instruction *ins)
3136 DWORD sampler_idx = ins->src[1].reg.idx[0].offset;
3137 char reg_dest[40];
3138 char reg_src[3][40];
3139 WORD flags = TEX_DERIV;
3141 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
3142 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src[0]);
3143 shader_arb_get_src_param(ins, &ins->src[2], 1, reg_src[1]);
3144 shader_arb_get_src_param(ins, &ins->src[3], 2, reg_src[2]);
3146 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
3147 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
3149 shader_hw_sample(ins, sampler_idx, reg_dest, reg_src[0], flags, reg_src[1], reg_src[2]);
3152 static void shader_hw_texldl(const struct wined3d_shader_instruction *ins)
3154 DWORD sampler_idx = ins->src[1].reg.idx[0].offset;
3155 char reg_dest[40];
3156 char reg_coord[40];
3157 WORD flags = TEX_LOD;
3159 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
3160 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_coord);
3162 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
3163 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
3165 shader_hw_sample(ins, sampler_idx, reg_dest, reg_coord, flags, NULL, NULL);
3168 static void shader_hw_label(const struct wined3d_shader_instruction *ins)
3170 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3171 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3173 priv->in_main_func = FALSE;
3174 /* Call instructions activate the NV extensions, not labels and rets. If there is an uncalled
3175 * subroutine, don't generate a label that will make GL complain
3177 if(priv->target_version == ARB) return;
3179 shader_addline(buffer, "l%u:\n", ins->src[0].reg.idx[0].offset);
3182 static void vshader_add_footer(struct shader_arb_ctx_priv *priv_ctx,
3183 const struct arb_vshader_private *shader_data, const struct arb_vs_compile_args *args,
3184 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info,
3185 struct wined3d_string_buffer *buffer)
3187 unsigned int i;
3189 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
3190 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
3191 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
3192 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
3194 if (args->super.fog_src == VS_FOG_Z)
3196 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
3198 else
3200 if (!reg_maps->fog)
3202 /* posFixup.x is always 1.0, so we can safely use it */
3203 shader_addline(buffer, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
3205 else
3207 /* Clamp fogcoord */
3208 const char *zero = arb_get_helper_value(reg_maps->shader_version.type, ARB_ZERO);
3209 const char *one = arb_get_helper_value(reg_maps->shader_version.type, ARB_ONE);
3211 shader_addline(buffer, "MIN TMP_FOGCOORD.x, TMP_FOGCOORD.x, %s;\n", one);
3212 shader_addline(buffer, "MAX result.fogcoord.x, TMP_FOGCOORD.x, %s;\n", zero);
3216 /* Clipplanes are always stored without y inversion */
3217 if (use_nv_clip(gl_info) && priv_ctx->target_version >= NV2)
3219 if (args->super.clip_enabled)
3221 for (i = 0; i < priv_ctx->vs_clipplanes; i++)
3223 shader_addline(buffer, "DP4 result.clip[%u].x, TMP_OUT, state.clip[%u].plane;\n", i, i);
3227 else if (args->clip.boolclip.clip_texcoord)
3229 static const char component[4] = {'x', 'y', 'z', 'w'};
3230 unsigned int cur_clip = 0;
3231 const char *zero = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_ZERO);
3233 for (i = 0; i < gl_info->limits.clipplanes; ++i)
3235 if (args->clip.boolclip.clipplane_mask & (1u << i))
3237 shader_addline(buffer, "DP4 TA.%c, TMP_OUT, state.clip[%u].plane;\n",
3238 component[cur_clip++], i);
3241 switch (cur_clip)
3243 case 0:
3244 shader_addline(buffer, "MOV TA, %s;\n", zero);
3245 break;
3246 case 1:
3247 shader_addline(buffer, "MOV TA.yzw, %s;\n", zero);
3248 break;
3249 case 2:
3250 shader_addline(buffer, "MOV TA.zw, %s;\n", zero);
3251 break;
3252 case 3:
3253 shader_addline(buffer, "MOV TA.w, %s;\n", zero);
3254 break;
3256 shader_addline(buffer, "MOV result.texcoord[%u], TA;\n",
3257 args->clip.boolclip.clip_texcoord - 1);
3260 /* Write the final position.
3262 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3263 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3264 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3265 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
3267 shader_addline(buffer, "MUL TA, posFixup, TMP_OUT.w;\n");
3268 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
3269 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
3271 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3272 * and the glsl equivalent
3274 if (need_helper_const(shader_data, reg_maps, gl_info))
3276 const char *two = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_TWO);
3277 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, %s, -TMP_OUT.w;\n", two);
3279 else
3281 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
3282 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
3285 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
3287 priv_ctx->footer_written = TRUE;
3290 static void shader_hw_ret(const struct wined3d_shader_instruction *ins)
3292 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3293 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3294 const struct wined3d_shader *shader = ins->ctx->shader;
3295 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3297 if(priv->target_version == ARB) return;
3299 if(vshader)
3301 if (priv->in_main_func) vshader_add_footer(priv, shader->backend_data,
3302 priv->cur_vs_args, ins->ctx->reg_maps, ins->ctx->gl_info, buffer);
3305 shader_addline(buffer, "RET;\n");
3308 static void shader_hw_call(const struct wined3d_shader_instruction *ins)
3310 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3311 shader_addline(buffer, "CAL l%u;\n", ins->src[0].reg.idx[0].offset);
3314 static BOOL shader_arb_compile(const struct wined3d_gl_info *gl_info, GLenum target, const char *src)
3316 const char *ptr, *line;
3317 GLint native, pos;
3319 if (TRACE_ON(d3d_shader))
3321 ptr = src;
3322 while ((line = get_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
3325 GL_EXTCALL(glProgramStringARB(target, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(src), src));
3326 checkGLcall("glProgramStringARB()");
3328 if (FIXME_ON(d3d_shader))
3330 gl_info->gl_ops.gl.p_glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3331 if (pos != -1)
3333 FIXME_(d3d_shader)("Program error at position %d: %s\n\n", pos,
3334 debugstr_a((const char *)gl_info->gl_ops.gl.p_glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3335 ptr = src;
3336 while ((line = get_line(&ptr))) FIXME_(d3d_shader)(" %.*s", (int)(ptr - line), line);
3337 FIXME_(d3d_shader)("\n");
3339 return FALSE;
3343 if (WARN_ON(d3d_perf))
3345 GL_EXTCALL(glGetProgramivARB(target, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3346 checkGLcall("glGetProgramivARB()");
3347 if (!native)
3348 WARN_(d3d_perf)("Program exceeds native resource limits.\n");
3351 return TRUE;
3354 /* Context activation is done by the caller. */
3355 static GLuint create_arb_blt_vertex_program(const struct wined3d_gl_info *gl_info)
3357 GLuint program_id = 0;
3359 static const char blt_vprogram[] =
3360 "!!ARBvp1.0\n"
3361 "PARAM c[1] = { { 1, 0.5 } };\n"
3362 "MOV result.position, vertex.position;\n"
3363 "MOV result.color, c[0].x;\n"
3364 "MOV result.texcoord[0], vertex.texcoord[0];\n"
3365 "END\n";
3367 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3368 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
3369 shader_arb_compile(gl_info, GL_VERTEX_PROGRAM_ARB, blt_vprogram);
3371 return program_id;
3374 /* Context activation is done by the caller. */
3375 static GLuint create_arb_blt_fragment_program(const struct wined3d_gl_info *gl_info,
3376 enum wined3d_gl_resource_type tex_type, BOOL masked)
3378 GLuint program_id = 0;
3379 const char *fprogram;
3381 static const char * const blt_fprograms_full[WINED3D_GL_RES_TYPE_COUNT] =
3383 /* WINED3D_GL_RES_TYPE_TEX_1D */
3384 NULL,
3385 /* WINED3D_GL_RES_TYPE_TEX_2D */
3386 "!!ARBfp1.0\n"
3387 "TEMP R0;\n"
3388 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3389 "MOV result.depth.z, R0.x;\n"
3390 "END\n",
3391 /* WINED3D_GL_RES_TYPE_TEX_3D */
3392 NULL,
3393 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
3394 "!!ARBfp1.0\n"
3395 "TEMP R0;\n"
3396 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3397 "MOV result.depth.z, R0.x;\n"
3398 "END\n",
3399 /* WINED3D_GL_RES_TYPE_TEX_RECT */
3400 "!!ARBfp1.0\n"
3401 "TEMP R0;\n"
3402 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3403 "MOV result.depth.z, R0.x;\n"
3404 "END\n",
3405 /* WINED3D_GL_RES_TYPE_BUFFER */
3406 NULL,
3407 /* WINED3D_GL_RES_TYPE_RB */
3408 NULL,
3411 static const char * const blt_fprograms_masked[WINED3D_GL_RES_TYPE_COUNT] =
3413 /* WINED3D_GL_RES_TYPE_TEX_1D */
3414 NULL,
3415 /* WINED3D_GL_RES_TYPE_TEX_2D */
3416 "!!ARBfp1.0\n"
3417 "PARAM mask = program.local[0];\n"
3418 "TEMP R0;\n"
3419 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3420 "MUL R0.x, R0.x, R0.y;\n"
3421 "KIL -R0.x;\n"
3422 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3423 "MOV result.depth.z, R0.x;\n"
3424 "END\n",
3425 /* WINED3D_GL_RES_TYPE_TEX_3D */
3426 NULL,
3427 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
3428 "!!ARBfp1.0\n"
3429 "PARAM mask = program.local[0];\n"
3430 "TEMP R0;\n"
3431 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3432 "MUL R0.x, R0.x, R0.y;\n"
3433 "KIL -R0.x;\n"
3434 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3435 "MOV result.depth.z, R0.x;\n"
3436 "END\n",
3437 /* WINED3D_GL_RES_TYPE_TEX_RECT */
3438 "!!ARBfp1.0\n"
3439 "PARAM mask = program.local[0];\n"
3440 "TEMP R0;\n"
3441 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3442 "MUL R0.x, R0.x, R0.y;\n"
3443 "KIL -R0.x;\n"
3444 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3445 "MOV result.depth.z, R0.x;\n"
3446 "END\n",
3447 /* WINED3D_GL_RES_TYPE_BUFFER */
3448 NULL,
3449 /* WINED3D_GL_RES_TYPE_RB */
3450 NULL,
3453 fprogram = masked ? blt_fprograms_masked[tex_type] : blt_fprograms_full[tex_type];
3454 if (!fprogram)
3456 FIXME("tex_type %#x not supported, falling back to 2D\n", tex_type);
3457 tex_type = WINED3D_GL_RES_TYPE_TEX_2D;
3458 fprogram = masked ? blt_fprograms_masked[tex_type] : blt_fprograms_full[tex_type];
3461 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3462 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
3463 shader_arb_compile(gl_info, GL_FRAGMENT_PROGRAM_ARB, fprogram);
3465 return program_id;
3468 static void arbfp_add_sRGB_correction(struct wined3d_string_buffer *buffer, const char *fragcolor,
3469 const char *tmp1, const char *tmp2, const char *tmp3, const char *tmp4, BOOL condcode)
3471 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
3473 if(condcode)
3475 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
3476 shader_addline(buffer, "SUBC %s, %s.x, srgb_consts1.x;\n", tmp1, fragcolor);
3477 /* Calculate the > 0.0031308 case */
3478 shader_addline(buffer, "POW %s.x (GE), %s.x, srgb_consts0.x;\n", fragcolor, fragcolor);
3479 shader_addline(buffer, "POW %s.y (GE), %s.y, srgb_consts0.x;\n", fragcolor, fragcolor);
3480 shader_addline(buffer, "POW %s.z (GE), %s.z, srgb_consts0.x;\n", fragcolor, fragcolor);
3481 shader_addline(buffer, "MUL %s.xyz (GE), %s, srgb_consts0.y;\n", fragcolor, fragcolor);
3482 shader_addline(buffer, "SUB %s.xyz (GE), %s, srgb_consts0.z;\n", fragcolor, fragcolor);
3483 /* Calculate the < case */
3484 shader_addline(buffer, "MUL %s.xyz (LT), srgb_consts0.w, %s;\n", fragcolor, fragcolor);
3486 else
3488 /* Calculate the > 0.0031308 case */
3489 shader_addline(buffer, "POW %s.x, %s.x, srgb_consts0.x;\n", tmp1, fragcolor);
3490 shader_addline(buffer, "POW %s.y, %s.y, srgb_consts0.x;\n", tmp1, fragcolor);
3491 shader_addline(buffer, "POW %s.z, %s.z, srgb_consts0.x;\n", tmp1, fragcolor);
3492 shader_addline(buffer, "MUL %s, %s, srgb_consts0.y;\n", tmp1, tmp1);
3493 shader_addline(buffer, "SUB %s, %s, srgb_consts0.z;\n", tmp1, tmp1);
3494 /* Calculate the < case */
3495 shader_addline(buffer, "MUL %s, srgb_consts0.w, %s;\n", tmp2, fragcolor);
3496 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
3497 shader_addline(buffer, "SLT %s, srgb_consts1.x, %s;\n", tmp3, fragcolor);
3498 shader_addline(buffer, "SGE %s, srgb_consts1.x, %s;\n", tmp4, fragcolor);
3499 /* Store the components > 0.0031308 in the destination */
3500 shader_addline(buffer, "MUL %s.xyz, %s, %s;\n", fragcolor, tmp1, tmp3);
3501 /* Add the components that are < 0.0031308 */
3502 shader_addline(buffer, "MAD %s.xyz, %s, %s, %s;\n", fragcolor, tmp2, tmp4, fragcolor);
3503 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
3504 * result.color writes(.rgb first, then .a), or handle overwriting already written
3505 * components. The assembler uses a temporary register in this case, which is usually
3506 * not allocated from one of our registers that were used earlier.
3509 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
3512 static const DWORD *find_loop_control_values(const struct wined3d_shader *shader, DWORD idx)
3514 const struct wined3d_shader_lconst *constant;
3516 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3518 if (constant->idx == idx)
3520 return constant->value;
3523 return NULL;
3526 static void init_ps_input(const struct wined3d_shader *shader,
3527 const struct arb_ps_compile_args *args, struct shader_arb_ctx_priv *priv)
3529 static const char * const texcoords[8] =
3531 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
3532 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
3534 unsigned int i;
3535 const struct wined3d_shader_signature_element *input;
3536 const char *semantic_name;
3537 DWORD semantic_idx;
3539 switch(args->super.vp_mode)
3541 case pretransformed:
3542 case fixedfunction:
3543 /* The pixelshader has to collect the varyings on its own. In any case properly load
3544 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
3545 * other attribs to 0.0.
3547 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
3548 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
3549 * load the texcoord attrib pointers to match the pixel shader signature
3551 for (i = 0; i < shader->input_signature.element_count; ++i)
3553 input = &shader->input_signature.elements[i];
3554 if (!(semantic_name = input->semantic_name))
3555 continue;
3556 semantic_idx = input->semantic_idx;
3558 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
3560 if (!semantic_idx)
3561 priv->ps_input[input->register_idx] = "fragment.color.primary";
3562 else if (semantic_idx == 1)
3563 priv->ps_input[input->register_idx] = "fragment.color.secondary";
3564 else
3565 priv->ps_input[input->register_idx] = "0.0";
3567 else if (args->super.vp_mode == fixedfunction)
3569 priv->ps_input[input->register_idx] = "0.0";
3571 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
3573 if (semantic_idx < 8)
3574 priv->ps_input[input->register_idx] = texcoords[semantic_idx];
3575 else
3576 priv->ps_input[input->register_idx] = "0.0";
3578 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
3580 if (!semantic_idx)
3581 priv->ps_input[input->register_idx] = "fragment.fogcoord";
3582 else
3583 priv->ps_input[input->register_idx] = "0.0";
3585 else
3587 priv->ps_input[input->register_idx] = "0.0";
3590 TRACE("v%u, semantic %s%u is %s\n", input->register_idx,
3591 semantic_name, semantic_idx, priv->ps_input[input->register_idx]);
3593 break;
3595 case vertexshader:
3596 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
3597 * fragment.color
3599 for(i = 0; i < 8; i++)
3601 priv->ps_input[i] = texcoords[i];
3603 priv->ps_input[8] = "fragment.color.primary";
3604 priv->ps_input[9] = "fragment.color.secondary";
3605 break;
3609 static void arbfp_add_linear_fog(struct wined3d_string_buffer *buffer,
3610 const char *fragcolor, const char *tmp)
3612 shader_addline(buffer, "SUB %s.x, state.fog.params.z, fragment.fogcoord.x;\n", tmp);
3613 shader_addline(buffer, "MUL_SAT %s.x, %s.x, state.fog.params.w;\n", tmp, tmp);
3614 shader_addline(buffer, "LRP %s.rgb, %s.x, %s, state.fog.color;\n", fragcolor, tmp, fragcolor);
3617 /* Context activation is done by the caller. */
3618 static GLuint shader_arb_generate_pshader(const struct wined3d_shader *shader,
3619 const struct wined3d_gl_info *gl_info, struct wined3d_string_buffer *buffer,
3620 const struct arb_ps_compile_args *args, struct arb_ps_compiled_shader *compiled)
3622 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3623 const DWORD *function = shader->function;
3624 GLuint retval;
3625 char fragcolor[16];
3626 DWORD next_local = 0;
3627 struct shader_arb_ctx_priv priv_ctx;
3628 BOOL dcl_td = FALSE;
3629 BOOL want_nv_prog = FALSE;
3630 struct arb_pshader_private *shader_priv = shader->backend_data;
3631 DWORD map;
3632 BOOL custom_linear_fog = FALSE;
3634 char srgbtmp[4][4];
3635 char ftoa_tmp[17];
3636 unsigned int i, found = 0;
3638 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
3640 if (!(map & 1)
3641 || (shader->u.ps.color0_mov && i == shader->u.ps.color0_reg)
3642 || (reg_maps->shader_version.major < 2 && !i))
3643 continue;
3645 sprintf(srgbtmp[found], "R%u", i);
3646 ++found;
3647 if (found == 4) break;
3650 switch(found) {
3651 case 0:
3652 sprintf(srgbtmp[0], "TA");
3653 sprintf(srgbtmp[1], "TB");
3654 sprintf(srgbtmp[2], "TC");
3655 sprintf(srgbtmp[3], "TD");
3656 dcl_td = TRUE;
3657 break;
3658 case 1:
3659 sprintf(srgbtmp[1], "TA");
3660 sprintf(srgbtmp[2], "TB");
3661 sprintf(srgbtmp[3], "TC");
3662 break;
3663 case 2:
3664 sprintf(srgbtmp[2], "TA");
3665 sprintf(srgbtmp[3], "TB");
3666 break;
3667 case 3:
3668 sprintf(srgbtmp[3], "TA");
3669 break;
3670 case 4:
3671 break;
3674 /* Create the hw ARB shader */
3675 memset(&priv_ctx, 0, sizeof(priv_ctx));
3676 priv_ctx.cur_ps_args = args;
3677 priv_ctx.compiled_fprog = compiled;
3678 priv_ctx.cur_np2fixup_info = &compiled->np2fixup_info;
3679 init_ps_input(shader, args, &priv_ctx);
3680 list_init(&priv_ctx.control_frames);
3681 priv_ctx.ps_post_process = args->super.srgb_correction;
3683 /* Avoid enabling NV_fragment_program* if we do not need it.
3685 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
3686 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
3687 * is faster than what we gain from using higher native instructions. There are some things though
3688 * that cannot be emulated. In that case enable the extensions.
3689 * If the extension is enabled, instruction handlers that support both ways will use it.
3691 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
3692 * So enable the best we can get.
3694 if(reg_maps->usesdsx || reg_maps->usesdsy || reg_maps->loop_depth > 0 || reg_maps->usestexldd ||
3695 reg_maps->usestexldl || reg_maps->usesfacing || reg_maps->usesifc || reg_maps->usescall)
3697 want_nv_prog = TRUE;
3700 shader_addline(buffer, "!!ARBfp1.0\n");
3701 if (want_nv_prog && gl_info->supported[NV_FRAGMENT_PROGRAM2])
3703 shader_addline(buffer, "OPTION NV_fragment_program2;\n");
3704 priv_ctx.target_version = NV3;
3706 else if (want_nv_prog && gl_info->supported[NV_FRAGMENT_PROGRAM_OPTION])
3708 shader_addline(buffer, "OPTION NV_fragment_program;\n");
3709 priv_ctx.target_version = NV2;
3710 } else {
3711 if(want_nv_prog)
3713 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
3714 * limits properly
3716 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
3717 ERR("Try GLSL\n");
3719 priv_ctx.target_version = ARB;
3722 if (reg_maps->rt_mask > 1)
3724 shader_addline(buffer, "OPTION ARB_draw_buffers;\n");
3727 if (reg_maps->shader_version.major < 3)
3729 switch (args->super.fog)
3731 case WINED3D_FFP_PS_FOG_OFF:
3732 break;
3733 case WINED3D_FFP_PS_FOG_LINEAR:
3734 if (gl_info->quirks & WINED3D_QUIRK_BROKEN_ARB_FOG)
3736 custom_linear_fog = TRUE;
3737 priv_ctx.ps_post_process = TRUE;
3738 break;
3740 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
3741 break;
3742 case WINED3D_FFP_PS_FOG_EXP:
3743 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
3744 break;
3745 case WINED3D_FFP_PS_FOG_EXP2:
3746 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
3747 break;
3751 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
3752 * unused temps away(but occupies them for the whole shader if they're used once). Always
3753 * declaring them avoids tricky bookkeeping work
3755 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
3756 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
3757 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
3758 if(dcl_td) shader_addline(buffer, "TEMP TD;\n"); /* Used for sRGB writing */
3759 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
3760 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
3761 wined3d_ftoa(eps, ftoa_tmp);
3762 shader_addline(buffer, "PARAM ps_helper_const = { 0.0, 1.0, %s, 0.0 };\n", ftoa_tmp);
3764 if (reg_maps->shader_version.major < 2)
3766 strcpy(fragcolor, "R0");
3768 else
3770 if (priv_ctx.ps_post_process)
3772 if (shader->u.ps.color0_mov)
3774 sprintf(fragcolor, "R%u", shader->u.ps.color0_reg);
3776 else
3778 shader_addline(buffer, "TEMP TMP_COLOR;\n");
3779 strcpy(fragcolor, "TMP_COLOR");
3781 } else {
3782 strcpy(fragcolor, "result.color");
3786 if (args->super.srgb_correction)
3788 shader_addline(buffer, "PARAM srgb_consts0 = ");
3789 shader_arb_append_imm_vec4(buffer, wined3d_srgb_const0);
3790 shader_addline(buffer, ";\n");
3791 shader_addline(buffer, "PARAM srgb_consts1 = ");
3792 shader_arb_append_imm_vec4(buffer, wined3d_srgb_const1);
3793 shader_addline(buffer, ";\n");
3796 /* Base Declarations */
3797 shader_generate_arb_declarations(shader, reg_maps, buffer, gl_info, NULL, &priv_ctx);
3799 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
3801 unsigned char bump_const;
3803 if (!(map & 1)) continue;
3805 bump_const = compiled->numbumpenvmatconsts;
3806 compiled->bumpenvmatconst[bump_const].const_num = WINED3D_CONST_NUM_UNUSED;
3807 compiled->bumpenvmatconst[bump_const].texunit = i;
3808 compiled->luminanceconst[bump_const].const_num = WINED3D_CONST_NUM_UNUSED;
3809 compiled->luminanceconst[bump_const].texunit = i;
3811 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3812 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3813 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3814 * textures due to conditional NP2 restrictions)
3816 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3817 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3818 * their location is shader dependent anyway and they cannot be loaded globally.
3820 compiled->bumpenvmatconst[bump_const].const_num = next_local++;
3821 shader_addline(buffer, "PARAM bumpenvmat%d = program.local[%d];\n",
3822 i, compiled->bumpenvmatconst[bump_const].const_num);
3823 compiled->numbumpenvmatconsts = bump_const + 1;
3825 if (!(reg_maps->luminanceparams & (1u << i)))
3826 continue;
3828 compiled->luminanceconst[bump_const].const_num = next_local++;
3829 shader_addline(buffer, "PARAM luminance%d = program.local[%d];\n",
3830 i, compiled->luminanceconst[bump_const].const_num);
3833 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
3835 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
3836 if (reg_maps->integer_constants & (1u << i) && priv_ctx.target_version >= NV2)
3838 const DWORD *control_values = find_loop_control_values(shader, i);
3840 if(control_values)
3842 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
3843 control_values[0], control_values[1], control_values[2]);
3845 else
3847 compiled->int_consts[i] = next_local;
3848 compiled->num_int_consts++;
3849 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
3854 if(reg_maps->vpos || reg_maps->usesdsy)
3856 compiled->ycorrection = next_local;
3857 shader_addline(buffer, "PARAM ycorrection = program.local[%u];\n", next_local++);
3859 if(reg_maps->vpos)
3861 shader_addline(buffer, "TEMP vpos;\n");
3862 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3863 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3864 * ycorrection.z: 1.0
3865 * ycorrection.w: 0.0
3867 shader_addline(buffer, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3868 shader_addline(buffer, "FLR vpos.xy, vpos;\n");
3871 else
3873 compiled->ycorrection = WINED3D_CONST_NUM_UNUSED;
3876 /* Load constants to fixup NP2 texcoords if there are still free constants left:
3877 * Constants (texture dimensions) for the NP2 fixup are loaded as local program parameters. This will consume
3878 * at most 8 (MAX_FRAGMENT_SAMPLERS / 2) parameters, which is highly unlikely, since the application had to
3879 * use 16 NP2 textures at the same time. In case that we run out of constants the fixup is simply not
3880 * applied / activated. This will probably result in wrong rendering of the texture, but will save us from
3881 * shader compilation errors and the subsequent errors when drawing with this shader. */
3882 if (priv_ctx.cur_ps_args->super.np2_fixup) {
3883 unsigned char cur_fixup_sampler = 0;
3885 struct arb_ps_np2fixup_info* const fixup = priv_ctx.cur_np2fixup_info;
3886 const WORD map = priv_ctx.cur_ps_args->super.np2_fixup;
3887 const UINT max_lconsts = gl_info->limits.arb_ps_local_constants;
3889 fixup->offset = next_local;
3890 fixup->super.active = 0;
3892 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3894 if (!(map & (1u << i)))
3895 continue;
3897 if (fixup->offset + (cur_fixup_sampler >> 1) < max_lconsts)
3899 fixup->super.active |= (1u << i);
3900 fixup->super.idx[i] = cur_fixup_sampler++;
3902 else
3904 FIXME("No free constant found to load NP2 fixup data into shader. "
3905 "Sampling from this texture will probably look wrong.\n");
3906 break;
3910 fixup->super.num_consts = (cur_fixup_sampler + 1) >> 1;
3911 if (fixup->super.num_consts) {
3912 shader_addline(buffer, "PARAM np2fixup[%u] = { program.env[%u..%u] };\n",
3913 fixup->super.num_consts, fixup->offset, fixup->super.num_consts + fixup->offset - 1);
3917 if (shader_priv->clipplane_emulation != ~0U && args->clip)
3919 shader_addline(buffer, "KIL fragment.texcoord[%u];\n", shader_priv->clipplane_emulation);
3922 /* Base Shader Body */
3923 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
3925 if(args->super.srgb_correction) {
3926 arbfp_add_sRGB_correction(buffer, fragcolor, srgbtmp[0], srgbtmp[1], srgbtmp[2], srgbtmp[3],
3927 priv_ctx.target_version >= NV2);
3930 if (custom_linear_fog)
3931 arbfp_add_linear_fog(buffer, fragcolor, "TA");
3933 if(strcmp(fragcolor, "result.color")) {
3934 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
3936 shader_addline(buffer, "END\n");
3938 /* TODO: change to resource.glObjectHandle or something like that */
3939 GL_EXTCALL(glGenProgramsARB(1, &retval));
3941 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
3942 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
3944 TRACE("Created hw pixel shader, prg=%d\n", retval);
3945 if (!shader_arb_compile(gl_info, GL_FRAGMENT_PROGRAM_ARB, buffer->buffer))
3946 return 0;
3948 return retval;
3951 static int compare_sig(const struct wined3d_shader_signature *sig1, const struct wined3d_shader_signature *sig2)
3953 unsigned int i;
3954 int ret;
3956 if (sig1->element_count != sig2->element_count)
3957 return sig1->element_count < sig2->element_count ? -1 : 1;
3959 for (i = 0; i < sig1->element_count; ++i)
3961 const struct wined3d_shader_signature_element *e1, *e2;
3963 e1 = &sig1->elements[i];
3964 e2 = &sig2->elements[i];
3966 if (!e1->semantic_name || !e2->semantic_name)
3968 /* Compare pointers, not contents. One string is NULL (element
3969 * does not exist), the other one is not NULL. */
3970 if (e1->semantic_name != e2->semantic_name)
3971 return e1->semantic_name < e2->semantic_name ? -1 : 1;
3972 continue;
3975 if ((ret = strcmp(e1->semantic_name, e2->semantic_name)))
3976 return ret;
3977 if (e1->semantic_idx != e2->semantic_idx)
3978 return e1->semantic_idx < e2->semantic_idx ? -1 : 1;
3979 if (e1->sysval_semantic != e2->sysval_semantic)
3980 return e1->sysval_semantic < e2->sysval_semantic ? -1 : 1;
3981 if (e1->component_type != e2->component_type)
3982 return e1->component_type < e2->component_type ? -1 : 1;
3983 if (e1->register_idx != e2->register_idx)
3984 return e1->register_idx < e2->register_idx ? -1 : 1;
3985 if (e1->mask != e2->mask)
3986 return e1->mask < e2->mask ? -1 : 1;
3988 return 0;
3991 static void clone_sig(struct wined3d_shader_signature *new, const struct wined3d_shader_signature *sig)
3993 unsigned int i;
3994 char *name;
3996 new->element_count = sig->element_count;
3997 new->elements = wined3d_calloc(new->element_count, sizeof(*new->elements));
3998 for (i = 0; i < sig->element_count; ++i)
4000 new->elements[i] = sig->elements[i];
4002 if (!new->elements[i].semantic_name)
4003 continue;
4005 /* Clone the semantic string */
4006 name = HeapAlloc(GetProcessHeap(), 0, strlen(sig->elements[i].semantic_name) + 1);
4007 strcpy(name, sig->elements[i].semantic_name);
4008 new->elements[i].semantic_name = name;
4012 static DWORD find_input_signature(struct shader_arb_priv *priv, const struct wined3d_shader_signature *sig)
4014 struct wine_rb_entry *entry = wine_rb_get(&priv->signature_tree, sig);
4015 struct ps_signature *found_sig;
4017 if (entry)
4019 found_sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4020 TRACE("Found existing signature %u\n", found_sig->idx);
4021 return found_sig->idx;
4023 found_sig = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*found_sig));
4024 clone_sig(&found_sig->sig, sig);
4025 found_sig->idx = priv->ps_sig_number++;
4026 TRACE("New signature stored and assigned number %u\n", found_sig->idx);
4027 if(wine_rb_put(&priv->signature_tree, sig, &found_sig->entry) == -1)
4029 ERR("Failed to insert program entry.\n");
4031 return found_sig->idx;
4034 static void init_output_registers(const struct wined3d_shader *shader,
4035 const struct wined3d_shader_signature *ps_input_sig,
4036 struct shader_arb_ctx_priv *priv_ctx, struct arb_vs_compiled_shader *compiled)
4038 unsigned int i, j;
4039 static const char * const texcoords[8] =
4041 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
4042 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
4044 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
4045 * and varying 9 to result.color.secondary
4047 static const char * const decl_idx_to_string[MAX_REG_INPUT] =
4049 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
4050 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]",
4051 "result.color.primary", "result.color.secondary"
4054 if (!ps_input_sig)
4056 TRACE("Pixel shader uses builtin varyings\n");
4057 /* Map builtins to builtins */
4058 for(i = 0; i < 8; i++)
4060 priv_ctx->texcrd_output[i] = texcoords[i];
4062 priv_ctx->color_output[0] = "result.color.primary";
4063 priv_ctx->color_output[1] = "result.color.secondary";
4064 priv_ctx->fog_output = "TMP_FOGCOORD";
4066 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
4067 for (i = 0; i < shader->output_signature.element_count; ++i)
4069 const struct wined3d_shader_signature_element *output = &shader->output_signature.elements[i];
4071 if (!output->semantic_name)
4072 continue;
4074 if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_POSITION))
4076 TRACE("o%u is TMP_OUT\n", output->register_idx);
4077 if (!output->semantic_idx)
4078 priv_ctx->vs_output[output->register_idx] = "TMP_OUT";
4079 else
4080 priv_ctx->vs_output[output->register_idx] = "TA";
4082 else if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_PSIZE))
4084 TRACE("o%u is result.pointsize\n", output->register_idx);
4085 if (!output->semantic_idx)
4086 priv_ctx->vs_output[output->register_idx] = "result.pointsize";
4087 else
4088 priv_ctx->vs_output[output->register_idx] = "TA";
4090 else if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_COLOR))
4092 TRACE("o%u is result.color.?, idx %u\n", output->register_idx, output->semantic_idx);
4093 if (!output->semantic_idx)
4094 priv_ctx->vs_output[output->register_idx] = "result.color.primary";
4095 else if (output->semantic_idx == 1)
4096 priv_ctx->vs_output[output->register_idx] = "result.color.secondary";
4097 else priv_ctx->vs_output[output->register_idx] = "TA";
4099 else if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4101 TRACE("o%u is result.texcoord[%u]\n", output->register_idx, output->semantic_idx);
4102 if (output->semantic_idx >= 8)
4103 priv_ctx->vs_output[output->register_idx] = "TA";
4104 else
4105 priv_ctx->vs_output[output->register_idx] = texcoords[output->semantic_idx];
4107 else if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_FOG))
4109 TRACE("o%u is result.fogcoord\n", output->register_idx);
4110 if (output->semantic_idx > 0)
4111 priv_ctx->vs_output[output->register_idx] = "TA";
4112 else
4113 priv_ctx->vs_output[output->register_idx] = "result.fogcoord";
4115 else
4117 priv_ctx->vs_output[output->register_idx] = "TA";
4120 return;
4123 TRACE("Pixel shader uses declared varyings\n");
4125 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
4126 for(i = 0; i < 8; i++)
4128 priv_ctx->texcrd_output[i] = "TA";
4130 priv_ctx->color_output[0] = "TA";
4131 priv_ctx->color_output[1] = "TA";
4132 priv_ctx->fog_output = "TA";
4134 for (i = 0; i < ps_input_sig->element_count; ++i)
4136 const struct wined3d_shader_signature_element *input = &ps_input_sig->elements[i];
4138 if (!input->semantic_name)
4139 continue;
4141 /* If a declared input register is not written by builtin arguments, don't write to it.
4142 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
4144 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
4145 * to TMP_OUT in any case
4147 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4149 if (input->semantic_idx < 8)
4150 priv_ctx->texcrd_output[input->semantic_idx] = decl_idx_to_string[input->register_idx];
4152 else if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR))
4154 if (input->semantic_idx < 2)
4155 priv_ctx->color_output[input->semantic_idx] = decl_idx_to_string[input->register_idx];
4157 else if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_FOG))
4159 if (!input->semantic_idx)
4160 priv_ctx->fog_output = decl_idx_to_string[input->register_idx];
4162 else
4164 continue;
4167 if (!strcmp(decl_idx_to_string[input->register_idx], "result.color.primary")
4168 || !strcmp(decl_idx_to_string[input->register_idx], "result.color.secondary"))
4170 compiled->need_color_unclamp = TRUE;
4174 /* Map declared to declared */
4175 for (i = 0; i < shader->output_signature.element_count; ++i)
4177 const struct wined3d_shader_signature_element *output = &shader->output_signature.elements[i];
4179 /* Write unread output to TA to throw them away */
4180 priv_ctx->vs_output[output->register_idx] = "TA";
4182 if (!output->semantic_name)
4183 continue;
4185 if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_POSITION) && !output->semantic_idx)
4187 priv_ctx->vs_output[output->register_idx] = "TMP_OUT";
4188 continue;
4190 else if (shader_match_semantic(output->semantic_name, WINED3D_DECL_USAGE_PSIZE) && !output->semantic_idx)
4192 priv_ctx->vs_output[output->register_idx] = "result.pointsize";
4193 continue;
4196 for (j = 0; j < ps_input_sig->element_count; ++j)
4198 const struct wined3d_shader_signature_element *input = &ps_input_sig->elements[j];
4200 if (!input->semantic_name)
4201 continue;
4203 if (!strcmp(input->semantic_name, output->semantic_name)
4204 && input->semantic_idx == output->semantic_idx)
4206 priv_ctx->vs_output[output->register_idx] = decl_idx_to_string[input->register_idx];
4208 if (!strcmp(priv_ctx->vs_output[output->register_idx], "result.color.primary")
4209 || !strcmp(priv_ctx->vs_output[output->register_idx], "result.color.secondary"))
4211 compiled->need_color_unclamp = TRUE;
4218 /* Context activation is done by the caller. */
4219 static GLuint shader_arb_generate_vshader(const struct wined3d_shader *shader,
4220 const struct wined3d_gl_info *gl_info, struct wined3d_string_buffer *buffer,
4221 const struct arb_vs_compile_args *args, struct arb_vs_compiled_shader *compiled,
4222 const struct wined3d_shader_signature *ps_input_sig)
4224 const struct arb_vshader_private *shader_data = shader->backend_data;
4225 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4226 struct shader_arb_priv *priv = shader->device->shader_priv;
4227 const DWORD *function = shader->function;
4228 GLuint ret;
4229 DWORD next_local = 0;
4230 struct shader_arb_ctx_priv priv_ctx;
4231 unsigned int i;
4233 memset(&priv_ctx, 0, sizeof(priv_ctx));
4234 priv_ctx.cur_vs_args = args;
4235 list_init(&priv_ctx.control_frames);
4236 init_output_registers(shader, ps_input_sig, &priv_ctx, compiled);
4238 /* Create the hw ARB shader */
4239 shader_addline(buffer, "!!ARBvp1.0\n");
4241 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
4242 * mesurable performance penalty, and we can always make use of it for clipplanes.
4244 if (gl_info->supported[NV_VERTEX_PROGRAM3])
4246 shader_addline(buffer, "OPTION NV_vertex_program3;\n");
4247 priv_ctx.target_version = NV3;
4248 shader_addline(buffer, "ADDRESS aL;\n");
4250 else if (gl_info->supported[NV_VERTEX_PROGRAM2_OPTION])
4252 shader_addline(buffer, "OPTION NV_vertex_program2;\n");
4253 priv_ctx.target_version = NV2;
4254 shader_addline(buffer, "ADDRESS aL;\n");
4255 } else {
4256 priv_ctx.target_version = ARB;
4259 shader_addline(buffer, "TEMP TMP_OUT;\n");
4260 if (reg_maps->fog)
4261 shader_addline(buffer, "TEMP TMP_FOGCOORD;\n");
4262 if (need_helper_const(shader_data, reg_maps, gl_info))
4264 char ftoa_tmp[17];
4265 wined3d_ftoa(eps, ftoa_tmp);
4266 shader_addline(buffer, "PARAM helper_const = { 0.0, 1.0, 2.0, %s};\n", ftoa_tmp);
4268 if (need_rel_addr_const(shader_data, reg_maps, gl_info))
4270 shader_addline(buffer, "PARAM rel_addr_const = { 0.5, %d.0, 0.0, 0.0 };\n", shader_data->rel_offset);
4271 shader_addline(buffer, "TEMP A0_SHADOW;\n");
4274 shader_addline(buffer, "TEMP TA;\n");
4275 shader_addline(buffer, "TEMP TB;\n");
4277 /* Base Declarations */
4278 shader_generate_arb_declarations(shader, reg_maps, buffer, gl_info,
4279 &priv_ctx.vs_clipplanes, &priv_ctx);
4281 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
4283 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
4284 if (reg_maps->integer_constants & (1u << i) && priv_ctx.target_version >= NV2)
4286 const DWORD *control_values = find_loop_control_values(shader, i);
4288 if(control_values)
4290 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
4291 control_values[0], control_values[1], control_values[2]);
4293 else
4295 compiled->int_consts[i] = next_local;
4296 compiled->num_int_consts++;
4297 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
4302 /* We need a constant to fixup the final position */
4303 shader_addline(buffer, "PARAM posFixup = program.local[%u];\n", next_local);
4304 compiled->pos_fixup = next_local++;
4306 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
4307 * for output parameters. D3D in theory does not do that either, but some applications depend on a
4308 * proper initialization of the secondary color, and programs using the fixed function pipeline without
4309 * a replacement shader depend on the texcoord.w being set properly.
4311 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
4312 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
4313 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
4314 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
4315 * this can eat a number of instructions, so skip it unless this cap is set as well
4317 if (!gl_info->supported[NV_VERTEX_PROGRAM])
4319 const char *color_init = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_0001);
4320 shader_addline(buffer, "MOV result.color.secondary, %s;\n", color_init);
4322 if (gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W && !priv->ffp_proj_control)
4324 int i;
4325 const char *one = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_ONE);
4326 for(i = 0; i < MAX_REG_TEXCRD; i++)
4328 if (reg_maps->u.texcoord_mask[i] && reg_maps->u.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL)
4329 shader_addline(buffer, "MOV result.texcoord[%u].w, %s\n", i, one);
4334 /* The shader starts with the main function */
4335 priv_ctx.in_main_func = TRUE;
4336 /* Base Shader Body */
4337 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4339 if (!priv_ctx.footer_written) vshader_add_footer(&priv_ctx,
4340 shader_data, args, reg_maps, gl_info, buffer);
4342 shader_addline(buffer, "END\n");
4344 /* TODO: change to resource.glObjectHandle or something like that */
4345 GL_EXTCALL(glGenProgramsARB(1, &ret));
4347 TRACE("Creating a hw vertex shader, prg=%d\n", ret);
4348 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, ret));
4350 TRACE("Created hw vertex shader, prg=%d\n", ret);
4351 if (!shader_arb_compile(gl_info, GL_VERTEX_PROGRAM_ARB, buffer->buffer))
4352 return -1;
4354 return ret;
4357 /* Context activation is done by the caller. */
4358 static struct arb_ps_compiled_shader *find_arb_pshader(struct wined3d_shader *shader,
4359 const struct arb_ps_compile_args *args)
4361 struct wined3d_device *device = shader->device;
4362 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4363 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4364 UINT i;
4365 DWORD new_size;
4366 struct arb_ps_compiled_shader *new_array;
4367 struct wined3d_string_buffer buffer;
4368 struct arb_pshader_private *shader_data;
4369 GLuint ret;
4371 if (!shader->backend_data)
4373 struct shader_arb_priv *priv = device->shader_priv;
4375 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4376 shader_data = shader->backend_data;
4377 shader_data->clamp_consts = shader->reg_maps.shader_version.major == 1;
4379 if (shader->reg_maps.shader_version.major < 3)
4380 shader_data->input_signature_idx = ~0U;
4381 else
4382 shader_data->input_signature_idx = find_input_signature(priv, &shader->input_signature);
4384 TRACE("Shader got assigned input signature index %u\n", shader_data->input_signature_idx);
4386 if (!d3d_info->vs_clipping)
4387 shader_data->clipplane_emulation = shader_find_free_input_register(&shader->reg_maps,
4388 d3d_info->limits.ffp_blend_stages - 1);
4389 else
4390 shader_data->clipplane_emulation = ~0U;
4392 shader_data = shader->backend_data;
4394 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4395 * so a linear search is more performant than a hashmap or a binary search
4396 * (cache coherency etc)
4398 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4400 if (!memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)))
4401 return &shader_data->gl_shaders[i];
4404 TRACE("No matching GL shader found, compiling a new shader\n");
4405 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4406 if (shader_data->num_gl_shaders)
4408 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4409 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4410 new_size * sizeof(*shader_data->gl_shaders));
4411 } else {
4412 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4413 new_size = 1;
4416 if(!new_array) {
4417 ERR("Out of memory\n");
4418 return 0;
4420 shader_data->gl_shaders = new_array;
4421 shader_data->shader_array_size = new_size;
4424 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4426 pixelshader_update_resource_types(shader, args->super.tex_types);
4428 if (!string_buffer_init(&buffer))
4430 ERR("Failed to initialize shader buffer.\n");
4431 return 0;
4434 ret = shader_arb_generate_pshader(shader, gl_info, &buffer, args,
4435 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4436 string_buffer_free(&buffer);
4437 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4439 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4442 static inline BOOL vs_args_equal(const struct arb_vs_compile_args *stored, const struct arb_vs_compile_args *new,
4443 const DWORD use_map, BOOL skip_int) {
4444 if((stored->super.swizzle_map & use_map) != new->super.swizzle_map) return FALSE;
4445 if(stored->super.clip_enabled != new->super.clip_enabled) return FALSE;
4446 if(stored->super.fog_src != new->super.fog_src) return FALSE;
4447 if(stored->clip.boolclip_compare != new->clip.boolclip_compare) return FALSE;
4448 if(stored->ps_signature != new->ps_signature) return FALSE;
4449 if(stored->vertex.samplers_compare != new->vertex.samplers_compare) return FALSE;
4450 if(skip_int) return TRUE;
4452 return !memcmp(stored->loop_ctrl, new->loop_ctrl, sizeof(stored->loop_ctrl));
4455 static struct arb_vs_compiled_shader *find_arb_vshader(struct wined3d_shader *shader,
4456 const struct wined3d_gl_info *gl_info, DWORD use_map, const struct arb_vs_compile_args *args,
4457 const struct wined3d_shader_signature *ps_input_sig)
4459 UINT i;
4460 DWORD new_size;
4461 struct arb_vs_compiled_shader *new_array;
4462 struct wined3d_string_buffer buffer;
4463 struct arb_vshader_private *shader_data;
4464 GLuint ret;
4466 if (!shader->backend_data)
4468 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4470 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4471 shader_data = shader->backend_data;
4473 if ((gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
4474 && reg_maps->min_rel_offset <= reg_maps->max_rel_offset)
4476 if (reg_maps->max_rel_offset - reg_maps->min_rel_offset > 127)
4478 FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
4479 FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
4480 FIXME("Min: %u, Max: %u.\n", reg_maps->min_rel_offset, reg_maps->max_rel_offset);
4482 else if (reg_maps->max_rel_offset - reg_maps->min_rel_offset > 63)
4483 shader_data->rel_offset = reg_maps->min_rel_offset + 63;
4484 else if (reg_maps->max_rel_offset > 63)
4485 shader_data->rel_offset = reg_maps->min_rel_offset;
4488 shader_data = shader->backend_data;
4490 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4491 * so a linear search is more performant than a hashmap or a binary search
4492 * (cache coherency etc)
4494 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4495 if (vs_args_equal(&shader_data->gl_shaders[i].args, args,
4496 use_map, gl_info->supported[NV_VERTEX_PROGRAM2_OPTION]))
4498 return &shader_data->gl_shaders[i];
4502 TRACE("No matching GL shader found, compiling a new shader\n");
4504 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4505 if (shader_data->num_gl_shaders)
4507 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4508 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4509 new_size * sizeof(*shader_data->gl_shaders));
4510 } else {
4511 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4512 new_size = 1;
4515 if(!new_array) {
4516 ERR("Out of memory\n");
4517 return 0;
4519 shader_data->gl_shaders = new_array;
4520 shader_data->shader_array_size = new_size;
4523 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4525 if (!string_buffer_init(&buffer))
4527 ERR("Failed to initialize shader buffer.\n");
4528 return 0;
4531 ret = shader_arb_generate_vshader(shader, gl_info, &buffer, args,
4532 &shader_data->gl_shaders[shader_data->num_gl_shaders],
4533 ps_input_sig);
4534 string_buffer_free(&buffer);
4535 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4537 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4540 static void find_arb_ps_compile_args(const struct wined3d_state *state,
4541 const struct wined3d_context *context, const struct wined3d_shader *shader,
4542 struct arb_ps_compile_args *args)
4544 const struct wined3d_gl_info *gl_info = context->gl_info;
4545 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
4546 int i;
4547 WORD int_skip;
4549 find_ps_compile_args(state, shader, context->stream_info.position_transformed, &args->super, context);
4551 /* This forces all local boolean constants to 1 to make them stateblock independent */
4552 args->bools = shader->reg_maps.local_bool_consts;
4554 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
4556 if (state->ps_consts_b[i])
4557 args->bools |= ( 1u << i);
4560 /* Only enable the clip plane emulation KIL if at least one clipplane is enabled. The KIL instruction
4561 * is quite expensive because it forces the driver to disable early Z discards. It is cheaper to
4562 * duplicate the shader than have a no-op KIL instruction in every shader
4564 if (!d3d_info->vs_clipping && use_vs(state)
4565 && state->render_states[WINED3D_RS_CLIPPING]
4566 && state->render_states[WINED3D_RS_CLIPPLANEENABLE])
4567 args->clip = 1;
4568 else
4569 args->clip = 0;
4571 /* Skip if unused or local, or supported natively */
4572 int_skip = ~shader->reg_maps.integer_constants | shader->reg_maps.local_int_consts;
4573 if (int_skip == 0xffff || gl_info->supported[NV_FRAGMENT_PROGRAM_OPTION])
4575 memset(args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4576 return;
4579 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
4581 if (int_skip & (1u << i))
4583 args->loop_ctrl[i][0] = 0;
4584 args->loop_ctrl[i][1] = 0;
4585 args->loop_ctrl[i][2] = 0;
4587 else
4589 args->loop_ctrl[i][0] = state->ps_consts_i[i].x;
4590 args->loop_ctrl[i][1] = state->ps_consts_i[i].y;
4591 args->loop_ctrl[i][2] = state->ps_consts_i[i].z;
4596 static void find_arb_vs_compile_args(const struct wined3d_state *state,
4597 const struct wined3d_context *context, const struct wined3d_shader *shader,
4598 struct arb_vs_compile_args *args)
4600 const struct wined3d_device *device = shader->device;
4601 const struct wined3d_adapter *adapter = device->adapter;
4602 const struct wined3d_gl_info *gl_info = context->gl_info;
4603 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
4604 int i;
4605 WORD int_skip;
4607 find_vs_compile_args(state, shader, context->stream_info.swizzle_map, &args->super, d3d_info);
4609 args->clip.boolclip_compare = 0;
4610 if (use_ps(state))
4612 const struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
4613 const struct arb_pshader_private *shader_priv = ps->backend_data;
4614 args->ps_signature = shader_priv->input_signature_idx;
4616 args->clip.boolclip.clip_texcoord = shader_priv->clipplane_emulation + 1;
4618 else
4620 args->ps_signature = ~0;
4621 if (!d3d_info->vs_clipping && adapter->fragment_pipe == &arbfp_fragment_pipeline)
4622 args->clip.boolclip.clip_texcoord = ffp_clip_emul(context) ? d3d_info->limits.ffp_blend_stages : 0;
4623 /* Otherwise: Setting boolclip_compare set clip_texcoord to 0 */
4626 if (args->clip.boolclip.clip_texcoord)
4628 if (state->render_states[WINED3D_RS_CLIPPING])
4629 args->clip.boolclip.clipplane_mask = (unsigned char)state->render_states[WINED3D_RS_CLIPPLANEENABLE];
4630 /* clipplane_mask was set to 0 by setting boolclip_compare to 0 */
4633 /* This forces all local boolean constants to 1 to make them stateblock independent */
4634 args->clip.boolclip.bools = shader->reg_maps.local_bool_consts;
4635 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
4636 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
4638 if (state->vs_consts_b[i])
4639 args->clip.boolclip.bools |= (1u << i);
4642 args->vertex.samplers[0] = context->tex_unit_map[MAX_FRAGMENT_SAMPLERS + 0];
4643 args->vertex.samplers[1] = context->tex_unit_map[MAX_FRAGMENT_SAMPLERS + 1];
4644 args->vertex.samplers[2] = context->tex_unit_map[MAX_FRAGMENT_SAMPLERS + 2];
4645 args->vertex.samplers[3] = 0;
4647 /* Skip if unused or local */
4648 int_skip = ~shader->reg_maps.integer_constants | shader->reg_maps.local_int_consts;
4649 /* This is about flow control, not clipping. */
4650 if (int_skip == 0xffff || gl_info->supported[NV_VERTEX_PROGRAM2_OPTION])
4652 memset(args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4653 return;
4656 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
4658 if (int_skip & (1u << i))
4660 args->loop_ctrl[i][0] = 0;
4661 args->loop_ctrl[i][1] = 0;
4662 args->loop_ctrl[i][2] = 0;
4664 else
4666 args->loop_ctrl[i][0] = state->vs_consts_i[i].x;
4667 args->loop_ctrl[i][1] = state->vs_consts_i[i].y;
4668 args->loop_ctrl[i][2] = state->vs_consts_i[i].z;
4673 /* Context activation is done by the caller. */
4674 static void shader_arb_select(void *shader_priv, struct wined3d_context *context,
4675 const struct wined3d_state *state)
4677 struct shader_arb_priv *priv = shader_priv;
4678 const struct wined3d_gl_info *gl_info = context->gl_info;
4679 int i;
4681 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
4682 if (use_ps(state))
4684 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
4685 struct arb_ps_compile_args compile_args;
4686 struct arb_ps_compiled_shader *compiled;
4688 TRACE("Using pixel shader %p.\n", ps);
4689 find_arb_ps_compile_args(state, context, ps, &compile_args);
4690 compiled = find_arb_pshader(ps, &compile_args);
4691 priv->current_fprogram_id = compiled->prgId;
4692 priv->compiled_fprog = compiled;
4694 /* Bind the fragment program */
4695 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4696 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
4698 if (!priv->use_arbfp_fixed_func)
4699 priv->fragment_pipe->enable_extension(gl_info, FALSE);
4701 /* Enable OpenGL fragment programs. */
4702 gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_PROGRAM_ARB);
4703 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
4705 TRACE("Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", priv->current_fprogram_id);
4707 /* Pixel Shader 1.x constants are clamped to [-1;1], Pixel Shader 2.0 constants are not. If switching between
4708 * a 1.x and newer shader, reload the first 8 constants
4710 if (priv->last_ps_const_clamped != ((struct arb_pshader_private *)ps->backend_data)->clamp_consts)
4712 priv->last_ps_const_clamped = ((struct arb_pshader_private *)ps->backend_data)->clamp_consts;
4713 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, 8);
4714 for(i = 0; i < 8; i++)
4716 priv->pshader_const_dirty[i] = 1;
4718 /* Also takes care of loading local constants */
4719 shader_arb_load_constants_internal(shader_priv, context, state, TRUE, FALSE, TRUE);
4721 else
4723 UINT rt_height = state->fb->render_targets[0]->height;
4724 shader_arb_ps_local_constants(compiled, context, state, rt_height);
4727 /* Force constant reloading for the NP2 fixup (see comment in shader_glsl_select for more info) */
4728 if (compiled->np2fixup_info.super.active)
4729 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
4731 if (ps->load_local_constsF)
4732 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
4734 else
4736 if (gl_info->supported[ARB_FRAGMENT_PROGRAM] && !priv->use_arbfp_fixed_func)
4738 /* Disable only if we're not using arbfp fixed function fragment
4739 * processing. If this is used, keep GL_FRAGMENT_PROGRAM_ARB
4740 * enabled, and the fixed function pipeline will bind the fixed
4741 * function replacement shader. */
4742 gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_PROGRAM_ARB);
4743 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4744 priv->current_fprogram_id = 0;
4746 priv->fragment_pipe->enable_extension(gl_info, TRUE);
4749 if (use_vs(state))
4751 struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
4752 struct arb_vs_compile_args compile_args;
4753 struct arb_vs_compiled_shader *compiled;
4754 const struct wined3d_shader_signature *ps_input_sig;
4756 TRACE("Using vertex shader %p\n", vs);
4757 find_arb_vs_compile_args(state, context, vs, &compile_args);
4759 /* Instead of searching for the signature in the signature list, read the one from the
4760 * current pixel shader. It's maybe not the shader where the signature came from, but it
4761 * is the same signature and faster to find. */
4762 if (compile_args.ps_signature == ~0U)
4763 ps_input_sig = NULL;
4764 else
4765 ps_input_sig = &state->shader[WINED3D_SHADER_TYPE_PIXEL]->input_signature;
4767 compiled = find_arb_vshader(vs, context->gl_info, context->stream_info.use_map,
4768 &compile_args, ps_input_sig);
4769 priv->current_vprogram_id = compiled->prgId;
4770 priv->compiled_vprog = compiled;
4772 /* Bind the vertex program */
4773 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4774 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
4776 priv->vertex_pipe->vp_enable(gl_info, FALSE);
4778 /* Enable OpenGL vertex programs */
4779 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_ARB);
4780 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
4781 TRACE("Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", priv->current_vprogram_id);
4782 shader_arb_vs_local_constants(compiled, context, state);
4784 if(priv->last_vs_color_unclamp != compiled->need_color_unclamp) {
4785 priv->last_vs_color_unclamp = compiled->need_color_unclamp;
4787 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4789 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, !compiled->need_color_unclamp));
4790 checkGLcall("glClampColorARB");
4791 } else {
4792 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4796 if (vs->load_local_constsF)
4797 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
4799 else
4801 if (gl_info->supported[ARB_VERTEX_PROGRAM])
4803 priv->current_vprogram_id = 0;
4804 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_ARB);
4805 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4807 priv->vertex_pipe->vp_enable(gl_info, TRUE);
4812 /* Context activation is done by the caller. */
4813 static void shader_arb_disable(void *shader_priv, struct wined3d_context *context)
4815 const struct wined3d_gl_info *gl_info = context->gl_info;
4816 struct shader_arb_priv *priv = shader_priv;
4818 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
4820 gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_PROGRAM_ARB);
4821 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4822 priv->current_fprogram_id = 0;
4824 priv->fragment_pipe->enable_extension(gl_info, FALSE);
4826 if (gl_info->supported[ARB_VERTEX_PROGRAM])
4828 priv->current_vprogram_id = 0;
4829 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_ARB);
4830 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4832 priv->vertex_pipe->vp_enable(gl_info, FALSE);
4834 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT] && priv->last_vs_color_unclamp)
4836 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
4837 checkGLcall("glClampColorARB");
4838 priv->last_vs_color_unclamp = FALSE;
4841 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
4842 | (1u << WINED3D_SHADER_TYPE_VERTEX)
4843 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
4844 | (1u << WINED3D_SHADER_TYPE_HULL)
4845 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
4846 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
4849 /* Context activation is done by the caller. */
4850 static void shader_arb_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
4851 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
4853 const float mask[] = {0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy};
4854 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
4855 struct shader_arb_priv *priv = shader_priv;
4856 GLuint *blt_fprogram;
4858 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
4859 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
4860 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_ARB);
4862 blt_fprogram = masked ? &priv->depth_blt_fprogram_id_masked[tex_type] : &priv->depth_blt_fprogram_id_full[tex_type];
4863 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type, masked);
4864 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
4865 if (masked) GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, mask));
4866 gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_PROGRAM_ARB);
4869 /* Context activation is done by the caller. */
4870 static void shader_arb_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
4872 struct shader_arb_priv *priv = shader_priv;
4874 if (priv->current_vprogram_id) {
4875 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4876 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
4878 TRACE("Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB.\n", priv->current_vprogram_id);
4880 else
4882 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_ARB);
4883 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4886 if (priv->current_fprogram_id) {
4887 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4888 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
4890 TRACE("Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB.\n", priv->current_fprogram_id);
4892 else if(!priv->use_arbfp_fixed_func)
4894 gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_PROGRAM_ARB);
4895 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4899 static void shader_arb_destroy(struct wined3d_shader *shader)
4901 struct wined3d_device *device = shader->device;
4902 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4904 if (shader_is_pshader_version(shader->reg_maps.shader_version.type))
4906 struct arb_pshader_private *shader_data = shader->backend_data;
4907 UINT i;
4909 if(!shader_data) return; /* This can happen if a shader was never compiled */
4911 if (shader_data->num_gl_shaders)
4913 struct wined3d_context *context = context_acquire(device, NULL);
4915 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4917 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4918 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4921 context_release(context);
4924 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4925 HeapFree(GetProcessHeap(), 0, shader_data);
4926 shader->backend_data = NULL;
4928 else
4930 struct arb_vshader_private *shader_data = shader->backend_data;
4931 UINT i;
4933 if(!shader_data) return; /* This can happen if a shader was never compiled */
4935 if (shader_data->num_gl_shaders)
4937 struct wined3d_context *context = context_acquire(device, NULL);
4939 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4941 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4942 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4945 context_release(context);
4948 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4949 HeapFree(GetProcessHeap(), 0, shader_data);
4950 shader->backend_data = NULL;
4954 static int sig_tree_compare(const void *key, const struct wine_rb_entry *entry)
4956 struct ps_signature *e = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4957 return compare_sig(key, &e->sig);
4960 static HRESULT shader_arb_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
4961 const struct fragment_pipeline *fragment_pipe)
4963 struct shader_arb_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv));
4964 struct fragment_caps fragment_caps;
4965 void *vertex_priv, *fragment_priv;
4966 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4968 if (!(vertex_priv = vertex_pipe->vp_alloc(&arb_program_shader_backend, priv)))
4970 ERR("Failed to initialize vertex pipe.\n");
4971 HeapFree(GetProcessHeap(), 0, priv);
4972 return E_FAIL;
4975 if (!(fragment_priv = fragment_pipe->alloc_private(&arb_program_shader_backend, priv)))
4977 ERR("Failed to initialize fragment pipe.\n");
4978 vertex_pipe->vp_free(device);
4979 HeapFree(GetProcessHeap(), 0, priv);
4980 return E_FAIL;
4983 memset(priv->vshader_const_dirty, 1,
4984 sizeof(*priv->vshader_const_dirty) * d3d_info->limits.vs_uniform_count);
4985 memset(priv->pshader_const_dirty, 1,
4986 sizeof(*priv->pshader_const_dirty) * d3d_info->limits.ps_uniform_count);
4988 wine_rb_init(&priv->signature_tree, sig_tree_compare);
4990 priv->vertex_pipe = vertex_pipe;
4991 priv->fragment_pipe = fragment_pipe;
4992 fragment_pipe->get_caps(&device->adapter->gl_info, &fragment_caps);
4993 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
4995 device->vertex_priv = vertex_priv;
4996 device->fragment_priv = fragment_priv;
4997 device->shader_priv = priv;
4999 return WINED3D_OK;
5002 static void release_signature(struct wine_rb_entry *entry, void *context)
5004 struct ps_signature *sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
5005 unsigned int i;
5007 for (i = 0; i < sig->sig.element_count; ++i)
5009 HeapFree(GetProcessHeap(), 0, (char *)sig->sig.elements[i].semantic_name);
5011 HeapFree(GetProcessHeap(), 0, sig->sig.elements);
5012 HeapFree(GetProcessHeap(), 0, sig);
5015 /* Context activation is done by the caller. */
5016 static void shader_arb_free(struct wined3d_device *device)
5018 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5019 struct shader_arb_priv *priv = device->shader_priv;
5020 int i;
5022 if (priv->depth_blt_vprogram_id)
5023 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
5025 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
5027 if (priv->depth_blt_fprogram_id_full[i])
5029 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id_full[i]));
5031 if (priv->depth_blt_fprogram_id_masked[i])
5033 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id_masked[i]));
5037 wine_rb_destroy(&priv->signature_tree, release_signature, NULL);
5038 priv->fragment_pipe->free_private(device);
5039 priv->vertex_pipe->vp_free(device);
5040 HeapFree(GetProcessHeap(), 0, device->shader_priv);
5043 static BOOL shader_arb_allocate_context_data(struct wined3d_context *context)
5045 return TRUE;
5048 static void shader_arb_free_context_data(struct wined3d_context *context)
5050 struct shader_arb_priv *priv;
5052 if (!context->swapchain)
5053 return;
5055 priv = context->swapchain->device->shader_priv;
5056 if (priv->last_context == context)
5057 priv->last_context = NULL;
5060 static void shader_arb_init_context_state(struct wined3d_context *context) {}
5062 static void shader_arb_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
5064 if (gl_info->supported[ARB_VERTEX_PROGRAM])
5066 DWORD vs_consts;
5067 UINT vs_version;
5069 /* 96 is the minimum allowed value of MAX_PROGRAM_ENV_PARAMETERS_ARB
5070 * for vertex programs. If the native limit is less than that it's
5071 * not very useful, and e.g. Mesa swrast returns 0, probably to
5072 * indicate it's a software implementation. */
5073 if (gl_info->limits.arb_vs_native_constants < 96)
5074 vs_consts = gl_info->limits.arb_vs_float_constants;
5075 else
5076 vs_consts = min(gl_info->limits.arb_vs_float_constants, gl_info->limits.arb_vs_native_constants);
5078 if (gl_info->supported[NV_VERTEX_PROGRAM3])
5080 vs_version = 3;
5081 TRACE("Hardware vertex shader version 3.0 enabled (NV_VERTEX_PROGRAM3)\n");
5083 else if (vs_consts >= 256)
5085 /* Shader Model 2.0 requires at least 256 vertex shader constants */
5086 vs_version = 2;
5087 TRACE("Hardware vertex shader version 2.0 enabled (ARB_PROGRAM)\n");
5089 else
5091 vs_version = 1;
5092 TRACE("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
5094 caps->vs_version = min(wined3d_settings.max_sm_vs, vs_version);
5095 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, vs_consts);
5097 else
5099 caps->vs_version = 0;
5100 caps->vs_uniform_count = 0;
5103 caps->hs_version = 0;
5104 caps->ds_version = 0;
5105 caps->gs_version = 0;
5106 caps->cs_version = 0;
5108 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
5110 DWORD ps_consts;
5111 UINT ps_version;
5113 /* Similar as above for vertex programs, but the minimum for fragment
5114 * programs is 24. */
5115 if (gl_info->limits.arb_ps_native_constants < 24)
5116 ps_consts = gl_info->limits.arb_ps_float_constants;
5117 else
5118 ps_consts = min(gl_info->limits.arb_ps_float_constants, gl_info->limits.arb_ps_native_constants);
5120 if (gl_info->supported[NV_FRAGMENT_PROGRAM2])
5122 ps_version = 3;
5123 TRACE("Hardware pixel shader version 3.0 enabled (NV_FRAGMENT_PROGRAM2)\n");
5125 else if (ps_consts >= 32)
5127 /* Shader Model 2.0 requires at least 32 pixel shader constants */
5128 ps_version = 2;
5129 TRACE("Hardware pixel shader version 2.0 enabled (ARB_PROGRAM)\n");
5131 else
5133 ps_version = 1;
5134 TRACE("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
5136 caps->ps_version = min(wined3d_settings.max_sm_ps, ps_version);
5137 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, ps_consts);
5138 caps->ps_1x_max_value = 8.0f;
5140 else
5142 caps->ps_version = 0;
5143 caps->ps_uniform_count = 0;
5144 caps->ps_1x_max_value = 0.0f;
5147 caps->varying_count = 0;
5148 caps->wined3d_caps = WINED3D_SHADER_CAP_SRGB_WRITE;
5149 if (use_nv_clip(gl_info))
5150 caps->wined3d_caps |= WINED3D_SHADER_CAP_VS_CLIPPING;
5153 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
5155 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
5157 TRACE("Checking support for color_fixup:\n");
5158 dump_color_fixup_desc(fixup);
5161 /* We support everything except complex conversions. */
5162 if (!is_complex_fixup(fixup))
5164 TRACE("[OK]\n");
5165 return TRUE;
5168 TRACE("[FAILED]\n");
5169 return FALSE;
5172 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction *ins) {
5173 DWORD shift;
5174 char write_mask[20], regstr[50];
5175 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5176 BOOL is_color = FALSE;
5177 const struct wined3d_shader_dst_param *dst;
5179 if (!ins->dst_count) return;
5181 dst = &ins->dst[0];
5182 shift = dst->shift;
5183 if (!shift) return; /* Saturate alone is handled by the instructions */
5185 shader_arb_get_write_mask(ins, dst, write_mask);
5186 shader_arb_get_register_name(ins, &dst->reg, regstr, &is_color);
5188 /* Generate a line that does the output modifier computation
5189 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
5190 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
5192 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins),
5193 regstr, write_mask, regstr, shift_tab[shift]);
5196 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
5198 /* WINED3DSIH_ABS */ shader_hw_map2gl,
5199 /* WINED3DSIH_ADD */ shader_hw_map2gl,
5200 /* WINED3DSIH_AND */ NULL,
5201 /* WINED3DSIH_BEM */ pshader_hw_bem,
5202 /* WINED3DSIH_BFI */ NULL,
5203 /* WINED3DSIH_BFREV */ NULL,
5204 /* WINED3DSIH_BREAK */ shader_hw_break,
5205 /* WINED3DSIH_BREAKC */ shader_hw_breakc,
5206 /* WINED3DSIH_BREAKP */ NULL,
5207 /* WINED3DSIH_BUFINFO */ NULL,
5208 /* WINED3DSIH_CALL */ shader_hw_call,
5209 /* WINED3DSIH_CALLNZ */ NULL,
5210 /* WINED3DSIH_CASE */ NULL,
5211 /* WINED3DSIH_CMP */ pshader_hw_cmp,
5212 /* WINED3DSIH_CND */ pshader_hw_cnd,
5213 /* WINED3DSIH_CONTINUE */ NULL,
5214 /* WINED3DSIH_CRS */ shader_hw_map2gl,
5215 /* WINED3DSIH_CUT */ NULL,
5216 /* WINED3DSIH_CUT_STREAM */ NULL,
5217 /* WINED3DSIH_DCL */ shader_hw_nop,
5218 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_hw_nop,
5219 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ NULL,
5220 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
5221 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
5222 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
5223 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ NULL,
5224 /* WINED3DSIH_DCL_INPUT */ NULL,
5225 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
5226 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_hw_nop,
5227 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
5228 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
5229 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
5230 /* WINED3DSIH_DCL_INPUT_SGV */ NULL,
5231 /* WINED3DSIH_DCL_INPUT_SIV */ NULL,
5232 /* WINED3DSIH_DCL_OUTPUT */ NULL,
5233 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
5234 /* WINED3DSIH_DCL_OUTPUT_SIV */ NULL,
5235 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_hw_nop,
5236 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
5237 /* WINED3DSIH_DCL_SAMPLER */ NULL,
5238 /* WINED3DSIH_DCL_STREAM */ NULL,
5239 /* WINED3DSIH_DCL_TEMPS */ NULL,
5240 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
5241 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
5242 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
5243 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
5244 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
5245 /* WINED3DSIH_DCL_THREAD_GROUP */ NULL,
5246 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
5247 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_hw_nop,
5248 /* WINED3DSIH_DEF */ shader_hw_nop,
5249 /* WINED3DSIH_DEFAULT */ NULL,
5250 /* WINED3DSIH_DEFB */ shader_hw_nop,
5251 /* WINED3DSIH_DEFI */ shader_hw_nop,
5252 /* WINED3DSIH_DIV */ NULL,
5253 /* WINED3DSIH_DP2 */ NULL,
5254 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
5255 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
5256 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
5257 /* WINED3DSIH_DST */ shader_hw_map2gl,
5258 /* WINED3DSIH_DSX */ shader_hw_map2gl,
5259 /* WINED3DSIH_DSX_COARSE */ NULL,
5260 /* WINED3DSIH_DSX_FINE */ NULL,
5261 /* WINED3DSIH_DSY */ shader_hw_dsy,
5262 /* WINED3DSIH_DSY_COARSE */ NULL,
5263 /* WINED3DSIH_DSY_FINE */ NULL,
5264 /* WINED3DSIH_ELSE */ shader_hw_else,
5265 /* WINED3DSIH_EMIT */ NULL,
5266 /* WINED3DSIH_EMIT_STREAM */ NULL,
5267 /* WINED3DSIH_ENDIF */ shader_hw_endif,
5268 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop,
5269 /* WINED3DSIH_ENDREP */ shader_hw_endrep,
5270 /* WINED3DSIH_ENDSWITCH */ NULL,
5271 /* WINED3DSIH_EQ */ NULL,
5272 /* WINED3DSIH_EXP */ shader_hw_scalar_op,
5273 /* WINED3DSIH_EXPP */ shader_hw_scalar_op,
5274 /* WINED3DSIH_FRC */ shader_hw_map2gl,
5275 /* WINED3DSIH_FTOI */ NULL,
5276 /* WINED3DSIH_FTOU */ NULL,
5277 /* WINED3DSIH_GATHER4 */ NULL,
5278 /* WINED3DSIH_GATHER4_C */ NULL,
5279 /* WINED3DSIH_GE */ NULL,
5280 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
5281 /* WINED3DSIH_HS_DECLS */ NULL,
5282 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
5283 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
5284 /* WINED3DSIH_IADD */ NULL,
5285 /* WINED3DSIH_IEQ */ NULL,
5286 /* WINED3DSIH_IF */ NULL /* Hardcoded into the shader */,
5287 /* WINED3DSIH_IFC */ shader_hw_ifc,
5288 /* WINED3DSIH_IGE */ NULL,
5289 /* WINED3DSIH_ILT */ NULL,
5290 /* WINED3DSIH_IMAD */ NULL,
5291 /* WINED3DSIH_IMAX */ NULL,
5292 /* WINED3DSIH_IMIN */ NULL,
5293 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
5294 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
5295 /* WINED3DSIH_IMUL */ NULL,
5296 /* WINED3DSIH_INE */ NULL,
5297 /* WINED3DSIH_INEG */ NULL,
5298 /* WINED3DSIH_ISHL */ NULL,
5299 /* WINED3DSIH_ISHR */ NULL,
5300 /* WINED3DSIH_ITOF */ NULL,
5301 /* WINED3DSIH_LABEL */ shader_hw_label,
5302 /* WINED3DSIH_LD */ NULL,
5303 /* WINED3DSIH_LD2DMS */ NULL,
5304 /* WINED3DSIH_LD_RAW */ NULL,
5305 /* WINED3DSIH_LD_STRUCTURED */ NULL,
5306 /* WINED3DSIH_LD_UAV_TYPED */ NULL,
5307 /* WINED3DSIH_LIT */ shader_hw_map2gl,
5308 /* WINED3DSIH_LOD */ NULL,
5309 /* WINED3DSIH_LOG */ shader_hw_scalar_op,
5310 /* WINED3DSIH_LOGP */ shader_hw_scalar_op,
5311 /* WINED3DSIH_LOOP */ shader_hw_loop,
5312 /* WINED3DSIH_LRP */ shader_hw_lrp,
5313 /* WINED3DSIH_LT */ NULL,
5314 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
5315 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
5316 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
5317 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
5318 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
5319 /* WINED3DSIH_MAD */ shader_hw_map2gl,
5320 /* WINED3DSIH_MAX */ shader_hw_map2gl,
5321 /* WINED3DSIH_MIN */ shader_hw_map2gl,
5322 /* WINED3DSIH_MOV */ shader_hw_mov,
5323 /* WINED3DSIH_MOVA */ shader_hw_mov,
5324 /* WINED3DSIH_MOVC */ NULL,
5325 /* WINED3DSIH_MUL */ shader_hw_map2gl,
5326 /* WINED3DSIH_NE */ NULL,
5327 /* WINED3DSIH_NOP */ shader_hw_nop,
5328 /* WINED3DSIH_NOT */ NULL,
5329 /* WINED3DSIH_NRM */ shader_hw_nrm,
5330 /* WINED3DSIH_OR */ NULL,
5331 /* WINED3DSIH_PHASE */ shader_hw_nop,
5332 /* WINED3DSIH_POW */ shader_hw_pow,
5333 /* WINED3DSIH_RCP */ shader_hw_scalar_op,
5334 /* WINED3DSIH_REP */ shader_hw_rep,
5335 /* WINED3DSIH_RESINFO */ NULL,
5336 /* WINED3DSIH_RET */ shader_hw_ret,
5337 /* WINED3DSIH_ROUND_NE */ NULL,
5338 /* WINED3DSIH_ROUND_NI */ NULL,
5339 /* WINED3DSIH_ROUND_PI */ NULL,
5340 /* WINED3DSIH_ROUND_Z */ NULL,
5341 /* WINED3DSIH_RSQ */ shader_hw_scalar_op,
5342 /* WINED3DSIH_SAMPLE */ NULL,
5343 /* WINED3DSIH_SAMPLE_B */ NULL,
5344 /* WINED3DSIH_SAMPLE_C */ NULL,
5345 /* WINED3DSIH_SAMPLE_C_LZ */ NULL,
5346 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
5347 /* WINED3DSIH_SAMPLE_INFO */ NULL,
5348 /* WINED3DSIH_SAMPLE_LOD */ NULL,
5349 /* WINED3DSIH_SAMPLE_POS */ NULL,
5350 /* WINED3DSIH_SETP */ NULL,
5351 /* WINED3DSIH_SGE */ shader_hw_map2gl,
5352 /* WINED3DSIH_SGN */ shader_hw_sgn,
5353 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
5354 /* WINED3DSIH_SLT */ shader_hw_map2gl,
5355 /* WINED3DSIH_SQRT */ NULL,
5356 /* WINED3DSIH_STORE_RAW */ NULL,
5357 /* WINED3DSIH_STORE_STRUCTURED */ NULL,
5358 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
5359 /* WINED3DSIH_SUB */ shader_hw_map2gl,
5360 /* WINED3DSIH_SWAPC */ NULL,
5361 /* WINED3DSIH_SWITCH */ NULL,
5362 /* WINED3DSIH_TEX */ pshader_hw_tex,
5363 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
5364 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
5365 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
5366 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
5367 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
5368 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
5369 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
5370 /* WINED3DSIH_TEXLDD */ shader_hw_texldd,
5371 /* WINED3DSIH_TEXLDL */ shader_hw_texldl,
5372 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
5373 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
5374 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
5375 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
5376 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5377 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
5378 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
5379 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
5380 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
5381 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
5382 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
5383 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
5384 /* WINED3DSIH_UBFE */ NULL,
5385 /* WINED3DSIH_UDIV */ NULL,
5386 /* WINED3DSIH_UGE */ NULL,
5387 /* WINED3DSIH_ULT */ NULL,
5388 /* WINED3DSIH_UMAX */ NULL,
5389 /* WINED3DSIH_UMIN */ NULL,
5390 /* WINED3DSIH_USHR */ NULL,
5391 /* WINED3DSIH_UTOF */ NULL,
5392 /* WINED3DSIH_XOR */ NULL,
5395 static BOOL get_bool_const(const struct wined3d_shader_instruction *ins,
5396 const struct wined3d_shader *shader, DWORD idx)
5398 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5399 BOOL vshader = shader_is_vshader_version(reg_maps->shader_version.type);
5400 const struct wined3d_shader_lconst *constant;
5401 WORD bools = 0;
5402 WORD flag = (1u << idx);
5403 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5405 if (reg_maps->local_bool_consts & flag)
5407 /* What good is an if(bool) with a hardcoded local constant? I don't know, but handle it */
5408 LIST_FOR_EACH_ENTRY(constant, &shader->constantsB, struct wined3d_shader_lconst, entry)
5410 if (constant->idx == idx)
5412 return constant->value[0];
5415 ERR("Local constant not found\n");
5416 return FALSE;
5418 else
5420 if(vshader) bools = priv->cur_vs_args->clip.boolclip.bools;
5421 else bools = priv->cur_ps_args->bools;
5422 return bools & flag;
5426 static void get_loop_control_const(const struct wined3d_shader_instruction *ins,
5427 const struct wined3d_shader *shader, UINT idx, struct wined3d_shader_loop_control *loop_control)
5429 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5430 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5432 /* Integer constants can either be a local constant, or they can be stored in the shader
5433 * type specific compile args. */
5434 if (reg_maps->local_int_consts & (1u << idx))
5436 const struct wined3d_shader_lconst *constant;
5438 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5440 if (constant->idx == idx)
5442 loop_control->count = constant->value[0];
5443 loop_control->start = constant->value[1];
5444 /* Step is signed. */
5445 loop_control->step = (int)constant->value[2];
5446 return;
5449 /* If this happens the flag was set incorrectly */
5450 ERR("Local constant not found\n");
5451 loop_control->count = 0;
5452 loop_control->start = 0;
5453 loop_control->step = 0;
5454 return;
5457 switch (reg_maps->shader_version.type)
5459 case WINED3D_SHADER_TYPE_VERTEX:
5460 /* Count and aL start value are unsigned */
5461 loop_control->count = priv->cur_vs_args->loop_ctrl[idx][0];
5462 loop_control->start = priv->cur_vs_args->loop_ctrl[idx][1];
5463 /* Step is signed. */
5464 loop_control->step = ((char)priv->cur_vs_args->loop_ctrl[idx][2]);
5465 break;
5467 case WINED3D_SHADER_TYPE_PIXEL:
5468 loop_control->count = priv->cur_ps_args->loop_ctrl[idx][0];
5469 loop_control->start = priv->cur_ps_args->loop_ctrl[idx][1];
5470 loop_control->step = ((char)priv->cur_ps_args->loop_ctrl[idx][2]);
5471 break;
5473 default:
5474 FIXME("Unhandled shader type %#x.\n", reg_maps->shader_version.type);
5475 break;
5479 static void record_instruction(struct list *list, const struct wined3d_shader_instruction *ins)
5481 unsigned int i;
5482 struct wined3d_shader_dst_param *dst_param;
5483 struct wined3d_shader_src_param *src_param = NULL, *rel_addr;
5484 struct recorded_instruction *rec = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rec));
5485 if(!rec)
5487 ERR("Out of memory\n");
5488 return;
5491 rec->ins = *ins;
5492 dst_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param));
5493 if(!dst_param) goto free;
5494 *dst_param = *ins->dst;
5495 if (ins->dst->reg.idx[0].rel_addr)
5497 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr));
5498 if (!rel_addr)
5499 goto free;
5500 *rel_addr = *ins->dst->reg.idx[0].rel_addr;
5501 dst_param->reg.idx[0].rel_addr = rel_addr;
5503 rec->ins.dst = dst_param;
5505 if (!(src_param = wined3d_calloc(ins->src_count, sizeof(*src_param))))
5506 goto free;
5507 for (i = 0; i < ins->src_count; ++i)
5509 src_param[i] = ins->src[i];
5510 if (ins->src[i].reg.idx[0].rel_addr)
5512 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr));
5513 if (!rel_addr)
5514 goto free;
5515 *rel_addr = *ins->src[i].reg.idx[0].rel_addr;
5516 src_param[i].reg.idx[0].rel_addr = rel_addr;
5519 rec->ins.src = src_param;
5520 list_add_tail(list, &rec->entry);
5521 return;
5523 free:
5524 ERR("Out of memory\n");
5525 if(dst_param)
5527 HeapFree(GetProcessHeap(), 0, (void *)dst_param->reg.idx[0].rel_addr);
5528 HeapFree(GetProcessHeap(), 0, dst_param);
5530 if(src_param)
5532 for(i = 0; i < ins->src_count; i++)
5534 HeapFree(GetProcessHeap(), 0, (void *)src_param[i].reg.idx[0].rel_addr);
5536 HeapFree(GetProcessHeap(), 0, src_param);
5538 HeapFree(GetProcessHeap(), 0, rec);
5541 static void free_recorded_instruction(struct list *list)
5543 struct recorded_instruction *rec_ins, *entry2;
5544 unsigned int i;
5546 LIST_FOR_EACH_ENTRY_SAFE(rec_ins, entry2, list, struct recorded_instruction, entry)
5548 list_remove(&rec_ins->entry);
5549 if (rec_ins->ins.dst)
5551 HeapFree(GetProcessHeap(), 0, (void *)rec_ins->ins.dst->reg.idx[0].rel_addr);
5552 HeapFree(GetProcessHeap(), 0, (void *)rec_ins->ins.dst);
5554 if (rec_ins->ins.src)
5556 for (i = 0; i < rec_ins->ins.src_count; ++i)
5558 HeapFree(GetProcessHeap(), 0, (void *)rec_ins->ins.src[i].reg.idx[0].rel_addr);
5560 HeapFree(GetProcessHeap(), 0, (void *)rec_ins->ins.src);
5562 HeapFree(GetProcessHeap(), 0, rec_ins);
5566 static void pop_control_frame(const struct wined3d_shader_instruction *ins)
5568 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5569 struct control_frame *control_frame;
5571 if (ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
5573 struct list *e = list_head(&priv->control_frames);
5574 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5575 list_remove(&control_frame->entry);
5576 HeapFree(GetProcessHeap(), 0, control_frame);
5577 priv->loop_depth--;
5579 else if (ins->handler_idx == WINED3DSIH_ENDIF)
5581 /* Non-ifc ENDIFs were already handled previously. */
5582 struct list *e = list_head(&priv->control_frames);
5583 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5584 list_remove(&control_frame->entry);
5585 HeapFree(GetProcessHeap(), 0, control_frame);
5589 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction *ins) {
5590 SHADER_HANDLER hw_fct;
5591 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5592 const struct wined3d_shader *shader = ins->ctx->shader;
5593 struct control_frame *control_frame;
5594 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5595 BOOL bool_const;
5597 if(ins->handler_idx == WINED3DSIH_LOOP || ins->handler_idx == WINED3DSIH_REP)
5599 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
5600 list_add_head(&priv->control_frames, &control_frame->entry);
5602 if(ins->handler_idx == WINED3DSIH_LOOP) control_frame->type = LOOP;
5603 if(ins->handler_idx == WINED3DSIH_REP) control_frame->type = REP;
5605 if(priv->target_version >= NV2)
5607 control_frame->no.loop = priv->num_loops++;
5608 priv->loop_depth++;
5610 else
5612 /* Don't bother recording when we're in a not used if branch */
5613 if(priv->muted)
5615 return;
5618 if(!priv->recording)
5620 list_init(&priv->record);
5621 priv->recording = TRUE;
5622 control_frame->outer_loop = TRUE;
5623 get_loop_control_const(ins, shader, ins->src[0].reg.idx[0].offset, &control_frame->loop_control);
5624 return; /* Instruction is handled */
5626 /* Record this loop in the outer loop's recording */
5629 else if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
5631 if(priv->target_version >= NV2)
5633 /* Nothing to do. The control frame is popped after the HW instr handler */
5635 else
5637 struct list *e = list_head(&priv->control_frames);
5638 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5639 list_remove(&control_frame->entry);
5641 if(control_frame->outer_loop)
5643 unsigned int iteration;
5644 int aL = 0;
5645 struct list copy;
5647 /* Turn off recording before playback */
5648 priv->recording = FALSE;
5650 /* Move the recorded instructions to a separate list and get them out of the private data
5651 * structure. If there are nested loops, the shader_arb_handle_instruction below will
5652 * be recorded again, thus priv->record might be overwritten
5654 list_init(&copy);
5655 list_move_tail(&copy, &priv->record);
5656 list_init(&priv->record);
5658 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
5660 shader_addline(buffer, "#unrolling loop: %u iterations, aL=%u, inc %d\n",
5661 control_frame->loop_control.count, control_frame->loop_control.start,
5662 control_frame->loop_control.step);
5663 aL = control_frame->loop_control.start;
5665 else
5667 shader_addline(buffer, "#unrolling rep: %u iterations\n", control_frame->loop_control.count);
5670 for (iteration = 0; iteration < control_frame->loop_control.count; ++iteration)
5672 struct recorded_instruction *rec_ins;
5673 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
5675 priv->aL = aL;
5676 shader_addline(buffer, "#Iteration %u, aL=%d\n", iteration, aL);
5678 else
5680 shader_addline(buffer, "#Iteration %u\n", iteration);
5683 LIST_FOR_EACH_ENTRY(rec_ins, &copy, struct recorded_instruction, entry)
5685 shader_arb_handle_instruction(&rec_ins->ins);
5688 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
5690 aL += control_frame->loop_control.step;
5693 shader_addline(buffer, "#end loop/rep\n");
5695 free_recorded_instruction(&copy);
5696 HeapFree(GetProcessHeap(), 0, control_frame);
5697 return; /* Instruction is handled */
5699 else
5701 /* This is a nested loop. Proceed to the normal recording function */
5702 HeapFree(GetProcessHeap(), 0, control_frame);
5707 if(priv->recording)
5709 record_instruction(&priv->record, ins);
5710 return;
5713 /* boolean if */
5714 if(ins->handler_idx == WINED3DSIH_IF)
5716 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
5717 list_add_head(&priv->control_frames, &control_frame->entry);
5718 control_frame->type = IF;
5720 bool_const = get_bool_const(ins, shader, ins->src[0].reg.idx[0].offset);
5721 if (ins->src[0].modifiers == WINED3DSPSM_NOT)
5722 bool_const = !bool_const;
5723 if (!priv->muted && !bool_const)
5725 shader_addline(buffer, "#if(FALSE){\n");
5726 priv->muted = TRUE;
5727 control_frame->muting = TRUE;
5729 else shader_addline(buffer, "#if(TRUE) {\n");
5731 return; /* Instruction is handled */
5733 else if(ins->handler_idx == WINED3DSIH_IFC)
5735 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
5736 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
5737 control_frame->type = IFC;
5738 control_frame->no.ifc = priv->num_ifcs++;
5739 list_add_head(&priv->control_frames, &control_frame->entry);
5741 else if(ins->handler_idx == WINED3DSIH_ELSE)
5743 struct list *e = list_head(&priv->control_frames);
5744 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5746 if(control_frame->type == IF)
5748 shader_addline(buffer, "#} else {\n");
5749 if(!priv->muted && !control_frame->muting)
5751 priv->muted = TRUE;
5752 control_frame->muting = TRUE;
5754 else if(control_frame->muting) priv->muted = FALSE;
5755 return; /* Instruction is handled. */
5757 /* In case of an ifc, generate a HW shader instruction */
5758 if (control_frame->type != IFC)
5759 ERR("Control frame does not match.\n");
5761 else if(ins->handler_idx == WINED3DSIH_ENDIF)
5763 struct list *e = list_head(&priv->control_frames);
5764 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5766 if(control_frame->type == IF)
5768 shader_addline(buffer, "#} endif\n");
5769 if(control_frame->muting) priv->muted = FALSE;
5770 list_remove(&control_frame->entry);
5771 HeapFree(GetProcessHeap(), 0, control_frame);
5772 return; /* Instruction is handled */
5774 /* In case of an ifc, generate a HW shader instruction */
5775 if (control_frame->type != IFC)
5776 ERR("Control frame does not match.\n");
5779 if(priv->muted)
5781 pop_control_frame(ins);
5782 return;
5785 /* Select handler */
5786 hw_fct = shader_arb_instruction_handler_table[ins->handler_idx];
5788 /* Unhandled opcode */
5789 if (!hw_fct)
5791 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5792 return;
5794 hw_fct(ins);
5796 pop_control_frame(ins);
5798 shader_arb_add_instruction_modifiers(ins);
5801 static BOOL shader_arb_has_ffp_proj_control(void *shader_priv)
5803 struct shader_arb_priv *priv = shader_priv;
5805 return priv->ffp_proj_control;
5808 const struct wined3d_shader_backend_ops arb_program_shader_backend =
5810 shader_arb_handle_instruction,
5811 shader_arb_select,
5812 shader_arb_disable,
5813 shader_arb_select_depth_blt,
5814 shader_arb_deselect_depth_blt,
5815 shader_arb_update_float_vertex_constants,
5816 shader_arb_update_float_pixel_constants,
5817 shader_arb_load_constants,
5818 shader_arb_destroy,
5819 shader_arb_alloc,
5820 shader_arb_free,
5821 shader_arb_allocate_context_data,
5822 shader_arb_free_context_data,
5823 shader_arb_init_context_state,
5824 shader_arb_get_caps,
5825 shader_arb_color_fixup_supported,
5826 shader_arb_has_ffp_proj_control,
5829 /* ARB_fragment_program fixed function pipeline replacement definitions */
5830 #define ARB_FFP_CONST_TFACTOR 0
5831 #define ARB_FFP_CONST_COLOR_KEY_LOW ((ARB_FFP_CONST_TFACTOR) + 1)
5832 #define ARB_FFP_CONST_COLOR_KEY_HIGH ((ARB_FFP_CONST_COLOR_KEY_LOW) + 1)
5833 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_COLOR_KEY_HIGH) + 1)
5834 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
5835 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
5836 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
5838 struct arbfp_ffp_desc
5840 struct ffp_frag_desc parent;
5841 GLuint shader;
5844 /* Context activation is done by the caller. */
5845 static void arbfp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
5847 if (enable)
5849 gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_PROGRAM_ARB);
5850 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
5852 else
5854 gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_PROGRAM_ARB);
5855 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
5859 static void *arbfp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
5861 struct shader_arb_priv *priv;
5863 /* Share private data between the shader backend and the pipeline
5864 * replacement, if both are the arb implementation. This is needed to
5865 * figure out whether ARBfp should be disabled if no pixel shader is bound
5866 * or not. */
5867 if (shader_backend == &arb_program_shader_backend)
5868 priv = shader_priv;
5869 else if (!(priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv))))
5870 return NULL;
5872 wine_rb_init(&priv->fragment_shaders, wined3d_ffp_frag_program_key_compare);
5873 priv->use_arbfp_fixed_func = TRUE;
5875 return priv;
5878 /* Context activation is done by the caller. */
5879 static void arbfp_free_ffpshader(struct wine_rb_entry *entry, void *context)
5881 const struct wined3d_gl_info *gl_info = context;
5882 struct arbfp_ffp_desc *entry_arb = WINE_RB_ENTRY_VALUE(entry, struct arbfp_ffp_desc, parent.entry);
5884 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
5885 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
5886 HeapFree(GetProcessHeap(), 0, entry_arb);
5889 /* Context activation is done by the caller. */
5890 static void arbfp_free(struct wined3d_device *device)
5892 struct shader_arb_priv *priv = device->fragment_priv;
5894 wine_rb_destroy(&priv->fragment_shaders, arbfp_free_ffpshader, &device->adapter->gl_info);
5895 priv->use_arbfp_fixed_func = FALSE;
5897 if (device->shader_backend != &arb_program_shader_backend)
5899 HeapFree(GetProcessHeap(), 0, device->fragment_priv);
5903 static void arbfp_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
5905 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
5906 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
5907 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
5908 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
5909 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
5910 WINED3DTEXOPCAPS_SELECTARG1 |
5911 WINED3DTEXOPCAPS_SELECTARG2 |
5912 WINED3DTEXOPCAPS_MODULATE4X |
5913 WINED3DTEXOPCAPS_MODULATE2X |
5914 WINED3DTEXOPCAPS_MODULATE |
5915 WINED3DTEXOPCAPS_ADDSIGNED2X |
5916 WINED3DTEXOPCAPS_ADDSIGNED |
5917 WINED3DTEXOPCAPS_ADD |
5918 WINED3DTEXOPCAPS_SUBTRACT |
5919 WINED3DTEXOPCAPS_ADDSMOOTH |
5920 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
5921 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
5922 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
5923 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
5924 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
5925 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
5926 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5927 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
5928 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
5929 WINED3DTEXOPCAPS_DOTPRODUCT3 |
5930 WINED3DTEXOPCAPS_MULTIPLYADD |
5931 WINED3DTEXOPCAPS_LERP |
5932 WINED3DTEXOPCAPS_BUMPENVMAP |
5933 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
5935 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
5937 caps->MaxTextureBlendStages = 8;
5938 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
5941 static DWORD arbfp_get_emul_mask(const struct wined3d_gl_info *gl_info)
5943 return GL_EXT_EMUL_ARB_MULTITEXTURE | GL_EXT_EMUL_EXT_FOG_COORD;
5946 static void state_texfactor_arbfp(struct wined3d_context *context,
5947 const struct wined3d_state *state, DWORD state_id)
5949 struct wined3d_device *device = context->swapchain->device;
5950 const struct wined3d_gl_info *gl_info = context->gl_info;
5951 struct wined3d_color color;
5953 if (device->shader_backend == &arb_program_shader_backend)
5955 struct shader_arb_priv *priv;
5957 /* Don't load the parameter if we're using an arbfp pixel shader,
5958 * otherwise we'll overwrite application provided constants. */
5959 if (use_ps(state))
5960 return;
5962 priv = device->shader_priv;
5963 priv->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
5964 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
5967 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
5968 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, &color.r));
5969 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, &color.r)");
5972 static void state_tss_constant_arbfp(struct wined3d_context *context,
5973 const struct wined3d_state *state, DWORD state_id)
5975 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5976 struct wined3d_device *device = context->swapchain->device;
5977 const struct wined3d_gl_info *gl_info = context->gl_info;
5978 struct wined3d_color color;
5980 if (device->shader_backend == &arb_program_shader_backend)
5982 struct shader_arb_priv *priv;
5984 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5985 * application provided constants.
5987 if (use_ps(state))
5988 return;
5990 priv = device->shader_priv;
5991 priv->pshader_const_dirty[ARB_FFP_CONST_CONSTANT(stage)] = 1;
5992 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, ARB_FFP_CONST_CONSTANT(stage) + 1);
5995 wined3d_color_from_d3dcolor(&color, state->texture_states[stage][WINED3D_TSS_CONSTANT]);
5996 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_CONSTANT(stage), &color.r));
5997 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_CONSTANT(stage), &color.r)");
6000 static void state_arb_specularenable(struct wined3d_context *context,
6001 const struct wined3d_state *state, DWORD state_id)
6003 struct wined3d_device *device = context->swapchain->device;
6004 const struct wined3d_gl_info *gl_info = context->gl_info;
6005 float col[4];
6007 if (device->shader_backend == &arb_program_shader_backend)
6009 struct shader_arb_priv *priv;
6011 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
6012 * application provided constants.
6014 if (use_ps(state))
6015 return;
6017 priv = device->shader_priv;
6018 priv->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
6019 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
6022 if (state->render_states[WINED3D_RS_SPECULARENABLE])
6024 /* The specular color has no alpha */
6025 col[0] = 1.0f; col[1] = 1.0f;
6026 col[2] = 1.0f; col[3] = 0.0f;
6027 } else {
6028 col[0] = 0.0f; col[1] = 0.0f;
6029 col[2] = 0.0f; col[3] = 0.0f;
6031 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
6032 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
6035 static void set_bumpmat_arbfp(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6037 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
6038 struct wined3d_device *device = context->swapchain->device;
6039 const struct wined3d_gl_info *gl_info = context->gl_info;
6040 float mat[2][2];
6042 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6044 if (device->shader_backend == &arb_program_shader_backend)
6046 struct shader_arb_priv *priv = device->shader_priv;
6048 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants. */
6049 if (use_ps(state))
6050 return;
6052 priv->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
6053 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
6056 mat[0][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT00]);
6057 mat[0][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT01]);
6058 mat[1][0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT10]);
6059 mat[1][1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_MAT11]);
6061 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
6062 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
6065 static void tex_bumpenvlum_arbfp(struct wined3d_context *context,
6066 const struct wined3d_state *state, DWORD state_id)
6068 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
6069 struct wined3d_device *device = context->swapchain->device;
6070 const struct wined3d_gl_info *gl_info = context->gl_info;
6071 float param[4];
6073 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6075 if (device->shader_backend == &arb_program_shader_backend)
6077 struct shader_arb_priv *priv = device->shader_priv;
6079 /* Exit now, don't set the luminance below, otherwise we may overwrite pixel shader constants. */
6080 if (use_ps(state))
6081 return;
6083 priv->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
6084 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
6087 param[0] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_LSCALE]);
6088 param[1] = *((float *)&state->texture_states[stage][WINED3D_TSS_BUMPENV_LOFFSET]);
6089 param[2] = 0.0f;
6090 param[3] = 0.0f;
6092 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
6093 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
6096 static void alpha_test_arbfp(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6098 const struct wined3d_gl_info *gl_info = context->gl_info;
6099 int glParm;
6100 float ref;
6102 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
6104 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
6106 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
6107 checkGLcall("glEnable GL_ALPHA_TEST");
6109 else
6111 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
6112 checkGLcall("glDisable GL_ALPHA_TEST");
6113 return;
6116 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
6117 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
6119 if (glParm)
6121 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
6122 checkGLcall("glAlphaFunc");
6126 static void color_key_arbfp(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6128 struct wined3d_device *device = context->swapchain->device;
6129 const struct wined3d_gl_info *gl_info = context->gl_info;
6130 struct wined3d_color float_key[2];
6131 const struct wined3d_texture *texture = state->textures[0];
6133 if (!texture)
6134 return;
6136 if (device->shader_backend == &arb_program_shader_backend)
6138 struct shader_arb_priv *priv;
6140 /* Don't load the parameter if we're using an arbfp pixel shader,
6141 * otherwise we'll overwrite application provided constants. */
6142 if (use_ps(state))
6143 return;
6145 priv = device->shader_priv;
6146 priv->pshader_const_dirty[ARB_FFP_CONST_COLOR_KEY_LOW] = 1;
6147 priv->pshader_const_dirty[ARB_FFP_CONST_COLOR_KEY_HIGH] = 1;
6148 priv->highest_dirty_ps_const = max(priv->highest_dirty_ps_const, ARB_FFP_CONST_COLOR_KEY_HIGH + 1);
6151 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
6153 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_COLOR_KEY_LOW, &float_key[0].r));
6154 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_COLOR_KEY_LOW, &float_key[0].r)");
6155 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_COLOR_KEY_HIGH, &float_key[1].r));
6156 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_COLOR_KEY_HIGH, &float_key[1].r)");
6159 static const char *get_argreg(struct wined3d_string_buffer *buffer, DWORD argnum, unsigned int stage, DWORD arg)
6161 const char *ret;
6163 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
6165 switch(arg & WINED3DTA_SELECTMASK) {
6166 case WINED3DTA_DIFFUSE:
6167 ret = "fragment.color.primary"; break;
6169 case WINED3DTA_CURRENT:
6170 ret = "ret";
6171 break;
6173 case WINED3DTA_TEXTURE:
6174 switch(stage) {
6175 case 0: ret = "tex0"; break;
6176 case 1: ret = "tex1"; break;
6177 case 2: ret = "tex2"; break;
6178 case 3: ret = "tex3"; break;
6179 case 4: ret = "tex4"; break;
6180 case 5: ret = "tex5"; break;
6181 case 6: ret = "tex6"; break;
6182 case 7: ret = "tex7"; break;
6183 default: ret = "unknown texture";
6185 break;
6187 case WINED3DTA_TFACTOR:
6188 ret = "tfactor"; break;
6190 case WINED3DTA_SPECULAR:
6191 ret = "fragment.color.secondary"; break;
6193 case WINED3DTA_TEMP:
6194 ret = "tempreg"; break;
6196 case WINED3DTA_CONSTANT:
6197 switch(stage) {
6198 case 0: ret = "const0"; break;
6199 case 1: ret = "const1"; break;
6200 case 2: ret = "const2"; break;
6201 case 3: ret = "const3"; break;
6202 case 4: ret = "const4"; break;
6203 case 5: ret = "const5"; break;
6204 case 6: ret = "const6"; break;
6205 case 7: ret = "const7"; break;
6206 default: ret = "unknown constant";
6208 break;
6210 default:
6211 return "unknown";
6214 if(arg & WINED3DTA_COMPLEMENT) {
6215 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
6216 if(argnum == 0) ret = "arg0";
6217 if(argnum == 1) ret = "arg1";
6218 if(argnum == 2) ret = "arg2";
6220 if(arg & WINED3DTA_ALPHAREPLICATE) {
6221 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
6222 if(argnum == 0) ret = "arg0";
6223 if(argnum == 1) ret = "arg1";
6224 if(argnum == 2) ret = "arg2";
6226 return ret;
6229 static void gen_ffp_instr(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6230 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6232 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6233 unsigned int mul = 1;
6235 if(color && alpha) dstmask = "";
6236 else if(color) dstmask = ".xyz";
6237 else dstmask = ".w";
6239 if(dst == tempreg) dstreg = "tempreg";
6240 else dstreg = "ret";
6242 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
6243 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
6244 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
6246 switch (op)
6248 case WINED3D_TOP_DISABLE:
6249 break;
6251 case WINED3D_TOP_SELECT_ARG2:
6252 arg1 = arg2;
6253 /* FALLTHROUGH */
6254 case WINED3D_TOP_SELECT_ARG1:
6255 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
6256 break;
6258 case WINED3D_TOP_MODULATE_4X:
6259 mul = 2;
6260 /* FALLTHROUGH */
6261 case WINED3D_TOP_MODULATE_2X:
6262 mul *= 2;
6263 /* FALLTHROUGH */
6264 case WINED3D_TOP_MODULATE:
6265 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
6266 break;
6268 case WINED3D_TOP_ADD_SIGNED_2X:
6269 mul = 2;
6270 /* FALLTHROUGH */
6271 case WINED3D_TOP_ADD_SIGNED:
6272 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
6273 arg2 = "arg2";
6274 /* FALLTHROUGH */
6275 case WINED3D_TOP_ADD:
6276 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
6277 break;
6279 case WINED3D_TOP_SUBTRACT:
6280 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
6281 break;
6283 case WINED3D_TOP_ADD_SMOOTH:
6284 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
6285 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
6286 break;
6288 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6289 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
6290 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
6291 break;
6292 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6293 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
6294 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
6295 break;
6296 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6297 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
6298 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
6299 break;
6300 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6301 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6302 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
6303 break;
6305 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6306 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
6307 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
6308 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
6309 break;
6311 /* D3DTOP_PREMODULATE ???? */
6313 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6314 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
6315 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
6316 break;
6317 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6318 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
6319 break;
6320 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6321 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
6322 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
6323 break;
6324 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6325 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
6326 break;
6328 case WINED3D_TOP_DOTPRODUCT3:
6329 mul = 4;
6330 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
6331 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
6332 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
6333 break;
6335 case WINED3D_TOP_MULTIPLY_ADD:
6336 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
6337 break;
6339 case WINED3D_TOP_LERP:
6340 /* The msdn is not quite right here */
6341 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
6342 break;
6344 case WINED3D_TOP_BUMPENVMAP:
6345 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6346 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
6347 break;
6349 default:
6350 FIXME("Unhandled texture op %08x\n", op);
6353 if (mul == 2)
6354 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", dstreg, dstmask, dstreg);
6355 else if (mul == 4)
6356 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", dstreg, dstmask, dstreg);
6359 static const char *arbfp_texture_target(enum wined3d_gl_resource_type type)
6361 switch(type)
6363 case WINED3D_GL_RES_TYPE_TEX_1D:
6364 return "1D";
6365 case WINED3D_GL_RES_TYPE_TEX_2D:
6366 return "2D";
6367 case WINED3D_GL_RES_TYPE_TEX_3D:
6368 return "3D";
6369 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6370 return "CUBE";
6371 case WINED3D_GL_RES_TYPE_TEX_RECT:
6372 return "RECT";
6373 default:
6374 return "unexpected_resource_type";
6378 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6380 BYTE tex_read = 0, bump_used = 0, luminance_used = 0, constant_used = 0;
6381 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6382 unsigned int stage, lowest_disabled_stage;
6383 struct wined3d_string_buffer buffer;
6384 struct color_fixup_masks masks;
6385 BOOL custom_linear_fog = FALSE;
6386 const char *textype, *instr;
6387 DWORD arg0, arg1, arg2;
6388 char colorcor_dst[8];
6389 BOOL op_equal;
6390 GLuint ret;
6392 if (!string_buffer_init(&buffer))
6394 ERR("Failed to initialize shader buffer.\n");
6395 return 0;
6398 shader_addline(&buffer, "!!ARBfp1.0\n");
6400 if (settings->color_key_enabled)
6402 shader_addline(&buffer, "PARAM color_key_low = program.env[%u];\n", ARB_FFP_CONST_COLOR_KEY_LOW);
6403 shader_addline(&buffer, "PARAM color_key_high = program.env[%u];\n", ARB_FFP_CONST_COLOR_KEY_HIGH);
6404 tex_read |= 1;
6407 /* Find out which textures are read */
6408 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6410 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6411 break;
6413 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6414 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6415 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6417 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6418 tex_read |= 1u << stage;
6419 if (settings->op[stage].dst == tempreg)
6420 tempreg_used = TRUE;
6421 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6422 tempreg_used = TRUE;
6423 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6424 tfactor_used = TRUE;
6425 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6426 constant_used |= 1u << stage;
6428 switch (settings->op[stage].cop)
6430 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6431 luminance_used |= 1u << stage;
6432 /* fall through */
6433 case WINED3D_TOP_BUMPENVMAP:
6434 bump_used |= 1u << stage;
6435 /* fall through */
6436 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6437 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6438 tex_read |= 1u << stage;
6439 break;
6441 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6442 tfactor_used = TRUE;
6443 break;
6445 default:
6446 break;
6449 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6450 continue;
6452 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6453 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6454 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6456 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6457 tex_read |= 1u << stage;
6458 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6459 tempreg_used = TRUE;
6460 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6461 tfactor_used = TRUE;
6462 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6463 constant_used |= 1u << stage;
6465 lowest_disabled_stage = stage;
6467 switch (settings->fog)
6469 case WINED3D_FFP_PS_FOG_OFF: break;
6470 case WINED3D_FFP_PS_FOG_LINEAR:
6471 if (gl_info->quirks & WINED3D_QUIRK_BROKEN_ARB_FOG)
6473 custom_linear_fog = TRUE;
6474 break;
6476 shader_addline(&buffer, "OPTION ARB_fog_linear;\n");
6477 break;
6479 case WINED3D_FFP_PS_FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
6480 case WINED3D_FFP_PS_FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
6481 default: FIXME("Unexpected fog setting %d\n", settings->fog);
6484 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
6485 shader_addline(&buffer, "TEMP TMP;\n");
6486 shader_addline(&buffer, "TEMP ret;\n");
6487 if (tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
6488 shader_addline(&buffer, "TEMP arg0;\n");
6489 shader_addline(&buffer, "TEMP arg1;\n");
6490 shader_addline(&buffer, "TEMP arg2;\n");
6491 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6493 if (constant_used & (1u << stage))
6494 shader_addline(&buffer, "PARAM const%u = program.env[%u];\n", stage, ARB_FFP_CONST_CONSTANT(stage));
6496 if (!(tex_read & (1u << stage)))
6497 continue;
6499 shader_addline(&buffer, "TEMP tex%u;\n", stage);
6501 if (!(bump_used & (1u << stage)))
6502 continue;
6503 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
6505 if (!(luminance_used & (1u << stage)))
6506 continue;
6507 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
6509 if (tfactor_used)
6510 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
6511 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
6513 if (settings->sRGB_write)
6515 shader_addline(&buffer, "PARAM srgb_consts0 = ");
6516 shader_arb_append_imm_vec4(&buffer, wined3d_srgb_const0);
6517 shader_addline(&buffer, ";\n");
6518 shader_addline(&buffer, "PARAM srgb_consts1 = ");
6519 shader_arb_append_imm_vec4(&buffer, wined3d_srgb_const1);
6520 shader_addline(&buffer, ";\n");
6523 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6524 shader_addline(&buffer, "KIL fragment.texcoord[7];\n");
6526 if (tempreg_used || settings->sRGB_write)
6527 shader_addline(&buffer, "MOV tempreg, 0.0;\n");
6529 /* Generate texture sampling instructions */
6530 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6532 if (!(tex_read & (1u << stage)))
6533 continue;
6535 textype = arbfp_texture_target(settings->op[stage].tex_type);
6537 if(settings->op[stage].projected == proj_none) {
6538 instr = "TEX";
6539 } else if(settings->op[stage].projected == proj_count4 ||
6540 settings->op[stage].projected == proj_count3) {
6541 instr = "TXP";
6542 } else {
6543 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6544 instr = "TXP";
6547 if (stage > 0
6548 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6549 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6551 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
6552 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
6553 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
6554 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
6556 /* with projective textures, texbem only divides the static texture coord, not the displacement,
6557 * so multiply the displacement with the dividing parameter before passing it to TXP
6559 if (settings->op[stage].projected != proj_none) {
6560 if(settings->op[stage].projected == proj_count4) {
6561 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
6562 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
6563 } else {
6564 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
6565 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
6567 } else {
6568 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
6571 shader_addline(&buffer, "%s tex%u, ret, texture[%u], %s;\n",
6572 instr, stage, stage, textype);
6573 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6575 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
6576 stage - 1, stage - 1, stage - 1);
6577 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
6579 } else if(settings->op[stage].projected == proj_count3) {
6580 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
6581 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
6582 shader_addline(&buffer, "%s tex%u, ret, texture[%u], %s;\n",
6583 instr, stage, stage, textype);
6584 } else {
6585 shader_addline(&buffer, "%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
6586 instr, stage, stage, stage, textype);
6589 sprintf(colorcor_dst, "tex%u", stage);
6590 masks = calc_color_correction(settings->op[stage].color_fixup, WINED3DSP_WRITEMASK_ALL);
6591 gen_color_correction(&buffer, colorcor_dst, colorcor_dst, "const.x", "const.y",
6592 settings->op[stage].color_fixup, masks);
6595 if (settings->color_key_enabled)
6597 shader_addline(&buffer, "SLT TMP, tex0, color_key_low;\n"); /* below low key */
6598 shader_addline(&buffer, "SGE ret, tex0, color_key_high;\n"); /* above high key */
6599 shader_addline(&buffer, "ADD TMP, TMP, ret;\n"); /* or */
6600 shader_addline(&buffer, "DP4 TMP.b, TMP, TMP;\n"); /* on any channel */
6601 shader_addline(&buffer, "SGE TMP, -TMP.b, 0.0;\n"); /* logical not */
6602 shader_addline(&buffer, "KIL -TMP;\n"); /* discard if true */
6605 shader_addline(&buffer, "MOV ret, fragment.color.primary;\n");
6607 /* Generate the main shader */
6608 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6610 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6611 break;
6613 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6614 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6615 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6616 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6617 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6618 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6619 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6620 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6621 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6622 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6623 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6624 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6625 else
6626 op_equal = settings->op[stage].aop == settings->op[stage].cop
6627 && settings->op[stage].carg0 == settings->op[stage].aarg0
6628 && settings->op[stage].carg1 == settings->op[stage].aarg1
6629 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6631 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6633 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6634 settings->op[stage].cop, settings->op[stage].carg0,
6635 settings->op[stage].carg1, settings->op[stage].carg2);
6637 else if (op_equal)
6639 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6640 settings->op[stage].cop, settings->op[stage].carg0,
6641 settings->op[stage].carg1, settings->op[stage].carg2);
6643 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6644 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6646 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6647 settings->op[stage].cop, settings->op[stage].carg0,
6648 settings->op[stage].carg1, settings->op[stage].carg2);
6649 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6650 settings->op[stage].aop, settings->op[stage].aarg0,
6651 settings->op[stage].aarg1, settings->op[stage].aarg2);
6655 if (settings->sRGB_write || custom_linear_fog)
6657 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, ret;\n");
6658 if (settings->sRGB_write)
6659 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE);
6660 if (custom_linear_fog)
6661 arbfp_add_linear_fog(&buffer, "ret", "arg0");
6662 shader_addline(&buffer, "MOV result.color, ret;\n");
6664 else
6666 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, ret;\n");
6669 /* Footer */
6670 shader_addline(&buffer, "END\n");
6672 /* Generate the shader */
6673 GL_EXTCALL(glGenProgramsARB(1, &ret));
6674 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
6675 shader_arb_compile(gl_info, GL_FRAGMENT_PROGRAM_ARB, buffer.buffer);
6677 string_buffer_free(&buffer);
6678 return ret;
6681 static void fragment_prog_arbfp(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6683 const struct wined3d_device *device = context->swapchain->device;
6684 const struct wined3d_gl_info *gl_info = context->gl_info;
6685 struct shader_arb_priv *priv = device->fragment_priv;
6686 BOOL use_pshader = use_ps(state);
6687 struct ffp_frag_settings settings;
6688 const struct arbfp_ffp_desc *desc;
6689 unsigned int i;
6691 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
6693 if (isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
6695 if (!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader)
6697 /* Reload fixed function constants since they collide with the
6698 * pixel shader constants. */
6699 for (i = 0; i < MAX_TEXTURES; ++i)
6701 set_bumpmat_arbfp(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_BUMPENV_MAT00));
6702 state_tss_constant_arbfp(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_CONSTANT));
6704 state_texfactor_arbfp(context, state, STATE_RENDER(WINED3D_RS_TEXTUREFACTOR));
6705 state_arb_specularenable(context, state, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
6706 color_key_arbfp(context, state, STATE_COLOR_KEY);
6708 else if (use_pshader)
6710 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
6712 return;
6715 if (!use_pshader)
6717 /* Find or create a shader implementing the fixed function pipeline
6718 * settings, then activate it. */
6719 gen_ffp_frag_op(context, state, &settings, FALSE);
6720 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
6721 if (!desc)
6723 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
6724 if (!new_desc)
6726 ERR("Out of memory\n");
6727 return;
6730 new_desc->parent.settings = settings;
6731 new_desc->shader = gen_arbfp_ffp_shader(&settings, gl_info);
6732 add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
6733 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
6734 desc = new_desc;
6737 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active (however, note the
6738 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
6739 * deactivate it.
6741 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
6742 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
6743 priv->current_fprogram_id = desc->shader;
6745 if (device->shader_backend == &arb_program_shader_backend && context->last_was_pshader)
6747 /* Reload fixed function constants since they collide with the
6748 * pixel shader constants. */
6749 for (i = 0; i < MAX_TEXTURES; ++i)
6751 set_bumpmat_arbfp(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_BUMPENV_MAT00));
6752 state_tss_constant_arbfp(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_CONSTANT));
6754 state_texfactor_arbfp(context, state, STATE_RENDER(WINED3D_RS_TEXTUREFACTOR));
6755 state_arb_specularenable(context, state, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
6756 color_key_arbfp(context, state, STATE_COLOR_KEY);
6758 context->last_was_pshader = FALSE;
6760 else if (!context->last_was_pshader)
6762 if (device->shader_backend == &arb_program_shader_backend)
6763 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6764 context->last_was_pshader = TRUE;
6767 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
6770 /* We can't link the fog states to the fragment state directly since the
6771 * vertex pipeline links them to FOGENABLE. A different linking in different
6772 * pipeline parts can't be expressed in the combined state table, so we need
6773 * to handle that with a forwarding function. The other invisible side effect
6774 * is that changing the fog start and fog end (which links to FOGENABLE in
6775 * vertex) results in the fragment_prog_arbfp function being called because
6776 * FOGENABLE is dirty, which calls this function here. */
6777 static void state_arbfp_fog(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6779 enum fogsource new_source;
6780 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
6781 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
6783 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
6785 if (!isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL)))
6786 fragment_prog_arbfp(context, state, state_id);
6788 if (!state->render_states[WINED3D_RS_FOGENABLE])
6789 return;
6791 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
6793 if (use_vs(state))
6795 new_source = FOGSOURCE_VS;
6797 else
6799 if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
6800 new_source = FOGSOURCE_COORD;
6801 else
6802 new_source = FOGSOURCE_FFP;
6805 else
6807 new_source = FOGSOURCE_FFP;
6810 if (new_source != context->fog_source || fogstart == fogend)
6812 context->fog_source = new_source;
6813 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
6817 static void textransform(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6819 if (!isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL)))
6820 fragment_prog_arbfp(context, state, state_id);
6823 static const struct StateEntryTemplate arbfp_fragmentstate_template[] =
6825 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), { STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), state_texfactor_arbfp }, WINED3D_GL_EXT_NONE },
6826 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6827 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6828 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6829 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6830 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6831 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6832 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6833 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6834 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6835 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6836 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6837 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6838 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6839 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6840 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6841 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6842 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6843 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6844 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6845 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6846 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6847 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6848 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6849 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6850 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6851 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6852 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6853 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6854 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6855 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6856 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6857 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6858 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6859 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6860 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6861 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6862 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6863 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6864 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6865 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6866 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6867 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6868 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6869 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6870 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6871 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6872 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6873 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6874 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6875 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6876 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6877 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6878 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6879 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6880 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6881 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6882 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6883 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6884 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6885 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6886 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6887 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6888 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6889 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6890 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6891 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6892 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6893 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6894 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6895 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6896 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6897 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6898 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6899 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6900 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6901 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6902 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6903 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6904 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6905 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6906 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6907 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6908 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6909 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6910 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6911 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6912 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6913 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6914 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6915 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6916 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6917 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6918 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6919 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6920 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6921 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6922 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6923 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6924 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6925 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6926 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6927 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6928 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6929 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6930 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6931 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6932 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6933 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6934 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6935 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6936 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6937 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6938 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6939 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6940 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6941 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6942 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6943 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6944 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6945 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LOFFSET), { STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6946 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6947 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
6948 {STATE_RENDER(WINED3D_RS_ALPHAREF), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
6949 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), { STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), alpha_test_arbfp }, WINED3D_GL_EXT_NONE },
6950 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6951 {STATE_COLOR_KEY, { STATE_COLOR_KEY, color_key_arbfp }, WINED3D_GL_EXT_NONE },
6952 {STATE_RENDER(WINED3D_RS_FOGENABLE), { STATE_RENDER(WINED3D_RS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
6953 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), { STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6954 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), { STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6955 {STATE_RENDER(WINED3D_RS_FOGSTART), { STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6956 {STATE_RENDER(WINED3D_RS_FOGEND), { STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
6957 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), { STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB },
6958 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), { STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
6959 {STATE_RENDER(WINED3D_RS_FOGCOLOR), { STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
6960 {STATE_RENDER(WINED3D_RS_FOGDENSITY), { STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
6961 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6962 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6963 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6964 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6965 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6966 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6967 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6968 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), textransform}, WINED3D_GL_EXT_NONE },
6969 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6970 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6971 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6972 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6973 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6974 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6975 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6976 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), { STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), state_tss_constant_arbfp}, WINED3D_GL_EXT_NONE },
6977 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), { STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_arb_specularenable}, WINED3D_GL_EXT_NONE },
6978 {STATE_RENDER(WINED3D_RS_SHADEMODE), { STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
6979 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE },
6982 static BOOL arbfp_alloc_context_data(struct wined3d_context *context)
6984 return TRUE;
6987 static void arbfp_free_context_data(struct wined3d_context *context)
6991 const struct fragment_pipeline arbfp_fragment_pipeline = {
6992 arbfp_enable,
6993 arbfp_get_caps,
6994 arbfp_get_emul_mask,
6995 arbfp_alloc,
6996 arbfp_free,
6997 arbfp_alloc_context_data,
6998 arbfp_free_context_data,
6999 shader_arb_color_fixup_supported,
7000 arbfp_fragmentstate_template,
7003 struct arbfp_blit_type
7005 enum complex_fixup fixup : 4;
7006 enum wined3d_gl_resource_type res_type : 3;
7007 DWORD use_color_key : 1;
7008 DWORD padding : 24;
7011 struct arbfp_blit_desc
7013 GLuint shader;
7014 struct arbfp_blit_type type;
7015 struct wine_rb_entry entry;
7018 #define ARBFP_BLIT_PARAM_SIZE 0
7019 #define ARBFP_BLIT_PARAM_COLOR_KEY_LOW 1
7020 #define ARBFP_BLIT_PARAM_COLOR_KEY_HIGH 2
7022 struct arbfp_blit_priv
7024 struct wine_rb_tree shaders;
7025 GLuint palette_texture;
7028 static int arbfp_blit_type_compare(const void *key, const struct wine_rb_entry *entry)
7030 const struct arbfp_blit_type *ka = key;
7031 const struct arbfp_blit_type *kb = &WINE_RB_ENTRY_VALUE(entry, const struct arbfp_blit_desc, entry)->type;
7033 return memcmp(ka, kb, sizeof(*ka));
7036 /* Context activation is done by the caller. */
7037 static void arbfp_free_blit_shader(struct wine_rb_entry *entry, void *context)
7039 const struct wined3d_gl_info *gl_info = context;
7040 struct arbfp_blit_desc *entry_arb = WINE_RB_ENTRY_VALUE(entry, struct arbfp_blit_desc, entry);
7042 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
7043 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
7044 HeapFree(GetProcessHeap(), 0, entry_arb);
7047 static HRESULT arbfp_blit_alloc(struct wined3d_device *device)
7049 struct arbfp_blit_priv *priv;
7051 if (!(priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv))))
7052 return E_OUTOFMEMORY;
7054 wine_rb_init(&priv->shaders, arbfp_blit_type_compare);
7056 device->blit_priv = priv;
7058 return WINED3D_OK;
7061 /* Context activation is done by the caller. */
7062 static void arbfp_blit_free(struct wined3d_device *device)
7064 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7065 struct arbfp_blit_priv *priv = device->blit_priv;
7067 wine_rb_destroy(&priv->shaders, arbfp_free_blit_shader, &device->adapter->gl_info);
7068 checkGLcall("Delete blit programs");
7070 if (priv->palette_texture)
7071 gl_info->gl_ops.gl.p_glDeleteTextures(1, &priv->palette_texture);
7073 HeapFree(GetProcessHeap(), 0, device->blit_priv);
7074 device->blit_priv = NULL;
7077 static BOOL gen_planar_yuv_read(struct wined3d_string_buffer *buffer, const struct arbfp_blit_type *type,
7078 char *luminance)
7080 char chroma;
7081 const char *tex, *texinstr = "TXP";
7083 if (type->fixup == COMPLEX_FIXUP_UYVY)
7085 chroma = 'x';
7086 *luminance = 'w';
7088 else
7090 chroma = 'w';
7091 *luminance = 'x';
7094 tex = arbfp_texture_target(type->res_type);
7095 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_RECT)
7096 texinstr = "TEX";
7098 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
7099 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
7100 * filtering when we sample the texture.
7102 * These are the rules for reading the chroma:
7104 * Even pixel: Cr
7105 * Even pixel: U
7106 * Odd pixel: V
7108 * So we have to get the sampling x position in non-normalized coordinates in integers
7110 if (type->res_type != WINED3D_GL_RES_TYPE_TEX_RECT)
7112 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
7113 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
7115 else
7117 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
7119 /* We must not allow filtering between pixel x and x+1, this would mix U and V
7120 * Vertical filtering is ok. However, bear in mind that the pixel center is at
7121 * 0.5, so add 0.5.
7123 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
7124 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
7126 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
7127 * even and odd pixels respectively
7129 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
7130 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
7132 /* Sample Pixel 1 */
7133 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
7135 /* Put the value into either of the chroma values */
7136 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
7137 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
7138 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
7139 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
7141 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
7142 * the pixel right to the current one. Otherwise, sample the left pixel.
7143 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
7145 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
7146 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
7147 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
7149 /* Put the value into the other chroma */
7150 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
7151 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
7152 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
7153 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
7155 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
7156 * the current one and lerp the two U and V values
7159 /* This gives the correctly filtered luminance value */
7160 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
7162 return TRUE;
7165 static BOOL gen_yv12_read(struct wined3d_string_buffer *buffer, const struct arbfp_blit_type *type,
7166 char *luminance)
7168 const char *tex;
7169 static const float yv12_coef[]
7170 = {2.0f / 3.0f, 1.0f / 6.0f, (2.0f / 3.0f) + (1.0f / 6.0f), 1.0f / 3.0f};
7172 tex = arbfp_texture_target(type->res_type);
7174 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
7175 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
7176 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
7177 * pitch of the luminance plane, the packing into the gl texture is a bit
7178 * unfortunate. If the whole texture is interpreted as luminance data it looks
7179 * approximately like this:
7181 * +----------------------------------+----
7182 * | |
7183 * | |
7184 * | |
7185 * | |
7186 * | | 2
7187 * | LUMINANCE | -
7188 * | | 3
7189 * | |
7190 * | |
7191 * | |
7192 * | |
7193 * +----------------+-----------------+----
7194 * | | |
7195 * | V even rows | V odd rows |
7196 * | | | 1
7197 * +----------------+------------------ -
7198 * | | | 3
7199 * | U even rows | U odd rows |
7200 * | | |
7201 * +----------------+-----------------+----
7202 * | | |
7203 * | 0.5 | 0.5 |
7205 * So it appears as if there are 4 chroma images, but in fact the odd rows
7206 * in the chroma images are in the same row as the even ones. So it is
7207 * kinda tricky to read
7209 * When reading from rectangle textures, keep in mind that the input y coordinates
7210 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
7212 shader_addline(buffer, "PARAM yv12_coef = ");
7213 shader_arb_append_imm_vec4(buffer, yv12_coef);
7214 shader_addline(buffer, ";\n");
7216 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
7217 /* the chroma planes have only half the width */
7218 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
7220 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
7221 * the coordinate. Also read the right side of the image when reading odd lines
7223 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
7224 * bleeding
7226 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_2D)
7229 shader_addline(buffer, "RCP chroma.w, size.y;\n");
7231 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
7233 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
7234 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
7236 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
7237 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
7238 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
7239 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
7240 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
7242 /* clamp, keep the half pixel origin in mind */
7243 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
7244 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
7245 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
7246 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
7248 else
7250 /* Read from [size - size+size/4] */
7251 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
7252 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
7254 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
7255 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
7256 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
7257 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
7258 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
7259 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
7261 /* Make sure to read exactly from the pixel center */
7262 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
7263 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
7265 /* Clamp */
7266 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
7267 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
7268 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
7269 shader_addline(buffer, "ADD temp.y, size.y, coef.y;\n");
7270 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
7272 /* Read the texture, put the result into the output register */
7273 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
7274 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
7276 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
7277 * No need to clamp because we're just reusing the already clamped value from above
7279 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_2D)
7280 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
7281 else
7282 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
7283 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
7284 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
7286 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
7287 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
7288 * values due to filtering
7290 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
7291 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_2D)
7293 /* Multiply the y coordinate by 2/3 and clamp it */
7294 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
7295 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
7296 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
7297 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
7299 else
7301 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
7302 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
7303 * is bigger
7305 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
7306 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
7307 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
7309 *luminance = 'a';
7311 return TRUE;
7314 static BOOL gen_nv12_read(struct wined3d_string_buffer *buffer, const struct arbfp_blit_type *type,
7315 char *luminance)
7317 const char *tex;
7318 static const float nv12_coef[]
7319 = {2.0f / 3.0f, 1.0f / 3.0f, 1.0f, 1.0f};
7321 tex = arbfp_texture_target(type->res_type);
7323 /* NV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
7324 * sized plane where each component is an UV pair. So the effective
7325 * bitdepth is 12 bits per pixel If the whole texture is interpreted as luminance
7326 * data it looks approximately like this:
7328 * +----------------------------------+----
7329 * | |
7330 * | |
7331 * | |
7332 * | |
7333 * | | 2
7334 * | LUMINANCE | -
7335 * | | 3
7336 * | |
7337 * | |
7338 * | |
7339 * | |
7340 * +----------------------------------+----
7341 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
7342 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
7343 * | | 1
7344 * | | -
7345 * | | 3
7346 * | |
7347 * | |
7348 * +----------------------------------+----
7350 * When reading from rectangle textures, keep in mind that the input y coordinates
7351 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height. */
7353 shader_addline(buffer, "PARAM nv12_coef = ");
7354 shader_arb_append_imm_vec4(buffer, nv12_coef);
7355 shader_addline(buffer, ";\n");
7357 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
7358 /* We only have half the number of chroma pixels. */
7359 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
7361 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_2D)
7363 shader_addline(buffer, "RCP chroma.w, size.x;\n");
7364 shader_addline(buffer, "RCP chroma.z, size.y;\n");
7366 shader_addline(buffer, "MAD texcrd.y, texcrd.y, nv12_coef.y, nv12_coef.x;\n");
7368 /* We must not allow filtering horizontally, this would mix U and V.
7369 * Vertical filtering is ok. However, bear in mind that the pixel center is at
7370 * 0.5, so add 0.5. */
7372 /* Convert to non-normalized coordinates so we can find the
7373 * individual pixel. */
7374 shader_addline(buffer, "MUL texcrd.x, texcrd.x, size.x;\n");
7375 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
7376 /* Multiply by 2 since chroma components are stored in UV pixel pairs,
7377 * add 0.5 to hit the center of the pixel. */
7378 shader_addline(buffer, "MAD texcrd.x, texcrd.x, coef.z, coef.y;\n");
7380 /* Convert back to normalized coordinates. */
7381 shader_addline(buffer, "MUL texcrd.x, texcrd.x, chroma.w;\n");
7383 /* Clamp, keep the half pixel origin in mind. */
7384 shader_addline(buffer, "MAD temp.y, coef.y, chroma.z, nv12_coef.x;\n");
7385 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
7386 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.z, nv12_coef.z;\n");
7387 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
7389 else
7391 /* Read from [size - size+size/2] */
7392 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.y, size.y;\n");
7394 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
7395 /* Multiply by 2 since chroma components are stored in UV pixel pairs,
7396 * add 0.5 to hit the center of the pixel. */
7397 shader_addline(buffer, "MAD texcrd.x, texcrd.x, coef.z, coef.y;\n");
7399 /* Clamp */
7400 shader_addline(buffer, "MAD temp.y, size.y, coef.y, size.y;\n");
7401 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
7402 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
7403 shader_addline(buffer, "ADD temp.y, size.y, coef.y;\n");
7404 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
7406 /* Read the texture, put the result into the output register. */
7407 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
7408 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
7410 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_2D)
7412 /* Add 1/size.x */
7413 shader_addline(buffer, "ADD texcrd.x, texcrd.x, chroma.w;\n");
7415 else
7417 /* Add 1 */
7418 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.x;\n");
7420 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
7421 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
7423 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
7424 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
7425 * values due to filtering. */
7426 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
7427 if (type->res_type == WINED3D_GL_RES_TYPE_TEX_2D)
7429 /* Multiply the y coordinate by 2/3 and clamp it */
7430 shader_addline(buffer, "MUL texcrd.y, texcrd.y, nv12_coef.x;\n");
7431 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, nv12_coef.x;\n");
7432 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
7433 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
7435 else
7437 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
7438 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
7439 * is bigger
7441 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
7442 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
7443 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
7445 *luminance = 'a';
7447 return TRUE;
7450 /* Context activation is done by the caller. */
7451 static GLuint gen_p8_shader(struct arbfp_blit_priv *priv,
7452 const struct wined3d_gl_info *gl_info, const struct arbfp_blit_type *type)
7454 GLuint shader;
7455 struct wined3d_string_buffer buffer;
7456 const char *tex_target = arbfp_texture_target(type->res_type);
7458 /* This should not happen because we only use this conversion for
7459 * present blits which don't use color keying. */
7460 if (type->use_color_key)
7461 FIXME("Implement P8 color keying.\n");
7463 /* Shader header */
7464 if (!string_buffer_init(&buffer))
7466 ERR("Failed to initialize shader buffer.\n");
7467 return 0;
7470 GL_EXTCALL(glGenProgramsARB(1, &shader));
7471 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
7472 if (!shader)
7474 string_buffer_free(&buffer);
7475 return 0;
7478 shader_addline(&buffer, "!!ARBfp1.0\n");
7479 shader_addline(&buffer, "TEMP index;\n");
7481 /* { 255/256, 0.5/255*255/256, 0, 0 } */
7482 shader_addline(&buffer, "PARAM constants = { 0.996, 0.00195, 0, 0 };\n");
7484 /* The alpha-component contains the palette index */
7485 shader_addline(&buffer, "TEX index, fragment.texcoord[0], texture[0], %s;\n", tex_target);
7487 /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
7488 shader_addline(&buffer, "MAD index.a, index.a, constants.x, constants.y;\n");
7490 /* Use the alpha-component as an index in the palette to get the final color */
7491 shader_addline(&buffer, "TEX result.color, index.a, texture[1], 1D;\n");
7492 shader_addline(&buffer, "END\n");
7494 shader_arb_compile(gl_info, GL_FRAGMENT_PROGRAM_ARB, buffer.buffer);
7496 string_buffer_free(&buffer);
7498 return shader;
7501 /* Context activation is done by the caller. */
7502 static void upload_palette(const struct wined3d_texture *texture, struct wined3d_context *context)
7504 const struct wined3d_palette *palette = texture->swapchain ? texture->swapchain->palette : NULL;
7505 struct wined3d_device *device = texture->resource.device;
7506 const struct wined3d_gl_info *gl_info = context->gl_info;
7507 struct arbfp_blit_priv *priv = device->blit_priv;
7509 if (!priv->palette_texture)
7510 gl_info->gl_ops.gl.p_glGenTextures(1, &priv->palette_texture);
7512 GL_EXTCALL(glActiveTexture(GL_TEXTURE1));
7513 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, priv->palette_texture);
7515 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
7517 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
7518 /* Make sure we have discrete color levels. */
7519 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
7520 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
7521 /* TODO: avoid unneeded uploads in the future by adding some SFLAG_PALETTE_DIRTY mechanism */
7522 if (palette)
7524 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
7525 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
7527 else
7529 static const DWORD black;
7530 FIXME("P8 surface loaded without a palette.\n");
7531 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
7532 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
7535 /* Switch back to unit 0 in which the 2D texture will be stored. */
7536 context_active_texture(context, gl_info, 0);
7539 /* Context activation is done by the caller. */
7540 static GLuint gen_yuv_shader(struct arbfp_blit_priv *priv, const struct wined3d_gl_info *gl_info,
7541 const struct arbfp_blit_type *type)
7543 GLuint shader;
7544 struct wined3d_string_buffer buffer;
7545 char luminance_component;
7547 if (type->use_color_key)
7548 FIXME("Implement YUV color keying.\n");
7550 /* Shader header */
7551 if (!string_buffer_init(&buffer))
7553 ERR("Failed to initialize shader buffer.\n");
7554 return 0;
7557 GL_EXTCALL(glGenProgramsARB(1, &shader));
7558 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
7559 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
7560 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
7561 if (!shader)
7563 string_buffer_free(&buffer);
7564 return 0;
7567 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
7568 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
7569 * two chroma(U and V) values. Each macropixel has two luminance values, one for
7570 * each single pixel it contains, and one U and one V value shared between both
7571 * pixels.
7573 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
7574 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
7575 * take the format into account when generating the read swizzles
7577 * Reading the Y value is straightforward - just sample the texture. The hardware
7578 * takes care of filtering in the horizontal and vertical direction.
7580 * Reading the U and V values is harder. We have to avoid filtering horizontally,
7581 * because that would mix the U and V values of one pixel or two adjacent pixels.
7582 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
7583 * regardless of the filtering setting. Vertical filtering works automatically
7584 * though - the U and V values of two rows are mixed nicely.
7586 * Apart of avoiding filtering issues, the code has to know which value it just
7587 * read, and where it can find the other one. To determine this, it checks if
7588 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
7590 * Handling horizontal filtering of U and V values requires reading a 2nd pair
7591 * of pixels, extracting U and V and mixing them. This is not implemented yet.
7593 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
7594 * with width / 2. This way one read gives all 3 values, finding U and V is easy
7595 * in an unfiltered situation. Finding the luminance on the other hand requires
7596 * finding out if it is an odd or even pixel. The real drawback of this approach
7597 * is filtering. This would have to be emulated completely in the shader, reading
7598 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
7599 * vertically. Beyond that it would require adjustments to the texture handling
7600 * code to deal with the width scaling
7602 shader_addline(&buffer, "!!ARBfp1.0\n");
7603 shader_addline(&buffer, "TEMP luminance;\n");
7604 shader_addline(&buffer, "TEMP temp;\n");
7605 shader_addline(&buffer, "TEMP chroma;\n");
7606 shader_addline(&buffer, "TEMP texcrd;\n");
7607 shader_addline(&buffer, "TEMP texcrd2;\n");
7608 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
7609 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
7610 shader_addline(&buffer, "PARAM size = program.local[%u];\n", ARBFP_BLIT_PARAM_SIZE);
7612 switch (type->fixup)
7614 case COMPLEX_FIXUP_UYVY:
7615 case COMPLEX_FIXUP_YUY2:
7616 if (!gen_planar_yuv_read(&buffer, type, &luminance_component))
7618 string_buffer_free(&buffer);
7619 return 0;
7621 break;
7623 case COMPLEX_FIXUP_YV12:
7624 if (!gen_yv12_read(&buffer, type, &luminance_component))
7626 string_buffer_free(&buffer);
7627 return 0;
7629 break;
7631 case COMPLEX_FIXUP_NV12:
7632 if (!gen_nv12_read(&buffer, type, &luminance_component))
7634 string_buffer_free(&buffer);
7635 return 0;
7637 break;
7639 default:
7640 FIXME("Unsupported YUV fixup %#x\n", type->fixup);
7641 string_buffer_free(&buffer);
7642 return 0;
7645 /* Calculate the final result. Formula is taken from
7646 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
7647 * ranges from -0.5 to 0.5
7649 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
7651 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
7652 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
7653 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
7654 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
7655 shader_addline(&buffer, "END\n");
7657 shader_arb_compile(gl_info, GL_FRAGMENT_PROGRAM_ARB, buffer.buffer);
7659 string_buffer_free(&buffer);
7661 return shader;
7664 /* Context activation is done by the caller. */
7665 static GLuint arbfp_gen_plain_shader(struct arbfp_blit_priv *priv,
7666 const struct wined3d_gl_info *gl_info, const struct arbfp_blit_type *type)
7668 GLuint shader;
7669 struct wined3d_string_buffer buffer;
7670 const char *tex_target = arbfp_texture_target(type->res_type);
7672 /* Shader header */
7673 if (!string_buffer_init(&buffer))
7675 ERR("Failed to initialize shader buffer.\n");
7676 return 0;
7679 GL_EXTCALL(glGenProgramsARB(1, &shader));
7680 if (!shader)
7682 string_buffer_free(&buffer);
7683 return 0;
7685 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
7687 shader_addline(&buffer, "!!ARBfp1.0\n");
7689 if (type->use_color_key)
7691 shader_addline(&buffer, "TEMP color;\n");
7692 shader_addline(&buffer, "TEMP less, greater;\n");
7693 shader_addline(&buffer, "PARAM color_key_low = program.local[%u];\n", ARBFP_BLIT_PARAM_COLOR_KEY_LOW);
7694 shader_addline(&buffer, "PARAM color_key_high = program.local[%u];\n", ARBFP_BLIT_PARAM_COLOR_KEY_HIGH);
7695 shader_addline(&buffer, "TEX color, fragment.texcoord[0], texture[0], %s;\n", tex_target);
7696 shader_addline(&buffer, "SLT less, color, color_key_low;\n"); /* below low key */
7697 shader_addline(&buffer, "SGE greater, color, color_key_high;\n"); /* above high key */
7698 shader_addline(&buffer, "ADD less, less, greater;\n"); /* or */
7699 shader_addline(&buffer, "DP4 less.b, less, less;\n"); /* on any channel */
7700 shader_addline(&buffer, "SGE less, -less.b, 0.0;\n"); /* logical not */
7701 shader_addline(&buffer, "KIL -less;\n"); /* discard if true */
7702 shader_addline(&buffer, "MOV result.color, color;\n");
7704 else
7706 shader_addline(&buffer, "TEX result.color, fragment.texcoord[0], texture[0], %s;\n", tex_target);
7709 shader_addline(&buffer, "END\n");
7711 shader_arb_compile(gl_info, GL_FRAGMENT_PROGRAM_ARB, buffer.buffer);
7713 string_buffer_free(&buffer);
7715 return shader;
7718 /* Context activation is done by the caller. */
7719 static HRESULT arbfp_blit_set(void *blit_priv, struct wined3d_context *context, const struct wined3d_surface *surface,
7720 const struct wined3d_color_key *color_key)
7722 const struct wined3d_texture *texture = surface->container;
7723 struct arbfp_blit_priv *priv = blit_priv;
7724 enum complex_fixup fixup;
7725 const struct wined3d_gl_info *gl_info = context->gl_info;
7726 struct wine_rb_entry *entry;
7727 struct arbfp_blit_type type;
7728 struct arbfp_blit_desc *desc;
7729 struct wined3d_color float_color_key[2];
7730 struct wined3d_vec4 size;
7731 GLuint shader;
7733 size.x = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
7734 size.y = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
7735 size.z = 1.0f;
7736 size.w = 1.0f;
7738 if (is_complex_fixup(texture->resource.format->color_fixup))
7739 fixup = get_complex_fixup(texture->resource.format->color_fixup);
7740 else
7741 fixup = COMPLEX_FIXUP_NONE;
7743 switch (texture->target)
7745 case GL_TEXTURE_1D:
7746 type.res_type = WINED3D_GL_RES_TYPE_TEX_1D;
7747 break;
7749 case GL_TEXTURE_2D:
7750 type.res_type = WINED3D_GL_RES_TYPE_TEX_2D;
7751 break;
7753 case GL_TEXTURE_3D:
7754 type.res_type = WINED3D_GL_RES_TYPE_TEX_3D;
7755 break;
7757 case GL_TEXTURE_CUBE_MAP_ARB:
7758 type.res_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
7759 break;
7761 case GL_TEXTURE_RECTANGLE_ARB:
7762 type.res_type = WINED3D_GL_RES_TYPE_TEX_RECT;
7763 break;
7765 default:
7766 ERR("Unexpected GL texture type %#x.\n", texture->target);
7767 type.res_type = WINED3D_GL_RES_TYPE_TEX_2D;
7769 type.fixup = fixup;
7770 type.use_color_key = !!color_key;
7771 type.padding = 0;
7773 entry = wine_rb_get(&priv->shaders, &type);
7774 if (entry)
7776 desc = WINE_RB_ENTRY_VALUE(entry, struct arbfp_blit_desc, entry);
7777 shader = desc->shader;
7779 else
7781 switch (fixup)
7783 case COMPLEX_FIXUP_NONE:
7784 if (!is_identity_fixup(texture->resource.format->color_fixup))
7785 FIXME("Implement support for sign or swizzle fixups.\n");
7786 shader = arbfp_gen_plain_shader(priv, gl_info, &type);
7787 break;
7789 case COMPLEX_FIXUP_P8:
7790 shader = gen_p8_shader(priv, gl_info, &type);
7791 break;
7793 case COMPLEX_FIXUP_YUY2:
7794 case COMPLEX_FIXUP_UYVY:
7795 case COMPLEX_FIXUP_YV12:
7796 case COMPLEX_FIXUP_NV12:
7797 shader = gen_yuv_shader(priv, gl_info, &type);
7798 break;
7801 if (!shader)
7803 FIXME("Unsupported complex fixup %#x, not setting a shader\n", fixup);
7804 return E_NOTIMPL;
7807 desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*desc));
7808 if (!desc)
7809 goto err_out;
7811 desc->type = type;
7812 desc->shader = shader;
7813 if (wine_rb_put(&priv->shaders, &desc->type, &desc->entry) == -1)
7815 err_out:
7816 ERR("Out of memory\n");
7817 GL_EXTCALL(glDeleteProgramsARB(1, &shader));
7818 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader))");
7819 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0));
7820 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0)");
7821 HeapFree(GetProcessHeap(), 0, desc);
7822 return E_OUTOFMEMORY;
7826 if (fixup == COMPLEX_FIXUP_P8)
7827 upload_palette(texture, context);
7829 gl_info->gl_ops.gl.p_glEnable(GL_FRAGMENT_PROGRAM_ARB);
7830 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
7831 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
7832 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
7833 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARBFP_BLIT_PARAM_SIZE, &size.x));
7834 checkGLcall("glProgramLocalParameter4fvARB");
7835 if (type.use_color_key)
7837 wined3d_format_get_float_color_key(texture->resource.format, color_key, float_color_key);
7838 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
7839 ARBFP_BLIT_PARAM_COLOR_KEY_LOW, &float_color_key[0].r));
7840 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
7841 ARBFP_BLIT_PARAM_COLOR_KEY_HIGH, &float_color_key[1].r));
7842 checkGLcall("glProgramLocalParameter4fvARB");
7845 return WINED3D_OK;
7848 /* Context activation is done by the caller. */
7849 static void arbfp_blit_unset(const struct wined3d_gl_info *gl_info)
7851 gl_info->gl_ops.gl.p_glDisable(GL_FRAGMENT_PROGRAM_ARB);
7852 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
7855 static BOOL arbfp_blit_supported(const struct wined3d_gl_info *gl_info,
7856 const struct wined3d_d3d_info *d3d_info, enum wined3d_blit_op blit_op,
7857 const RECT *src_rect, DWORD src_usage, enum wined3d_pool src_pool, const struct wined3d_format *src_format,
7858 const RECT *dst_rect, DWORD dst_usage, enum wined3d_pool dst_pool, const struct wined3d_format *dst_format)
7860 enum complex_fixup src_fixup;
7862 if (!gl_info->supported[ARB_FRAGMENT_PROGRAM])
7863 return FALSE;
7865 switch (blit_op)
7867 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
7868 if (!d3d_info->shader_color_key)
7870 /* The conversion modifies the alpha channel so the color key might no longer match. */
7871 TRACE("Color keying not supported with converted textures.\n");
7872 return FALSE;
7874 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
7875 case WINED3D_BLIT_OP_COLOR_BLIT:
7876 break;
7878 default:
7879 TRACE("Unsupported blit_op=%d\n", blit_op);
7880 return FALSE;
7883 if (src_pool == WINED3D_POOL_SYSTEM_MEM || dst_pool == WINED3D_POOL_SYSTEM_MEM)
7884 return FALSE;
7886 src_fixup = get_complex_fixup(src_format->color_fixup);
7887 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7889 TRACE("Checking support for fixup:\n");
7890 dump_color_fixup_desc(src_format->color_fixup);
7893 if (!is_identity_fixup(dst_format->color_fixup))
7895 TRACE("Destination fixups are not supported\n");
7896 return FALSE;
7899 if (is_identity_fixup(src_format->color_fixup))
7901 TRACE("[OK]\n");
7902 return TRUE;
7905 /* We only support YUV conversions. */
7906 if (!is_complex_fixup(src_format->color_fixup))
7908 TRACE("[FAILED]\n");
7909 return FALSE;
7912 switch(src_fixup)
7914 case COMPLEX_FIXUP_YUY2:
7915 case COMPLEX_FIXUP_UYVY:
7916 case COMPLEX_FIXUP_YV12:
7917 case COMPLEX_FIXUP_NV12:
7918 case COMPLEX_FIXUP_P8:
7919 TRACE("[OK]\n");
7920 return TRUE;
7922 default:
7923 FIXME("Unsupported YUV fixup %#x\n", src_fixup);
7924 TRACE("[FAILED]\n");
7925 return FALSE;
7929 static void arbfp_blit_surface(struct wined3d_device *device, enum wined3d_blit_op op, DWORD filter,
7930 struct wined3d_surface *src_surface, const RECT *src_rect_in,
7931 struct wined3d_surface *dst_surface, const RECT *dst_rect_in,
7932 const struct wined3d_color_key *color_key)
7934 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
7935 struct wined3d_texture *src_texture = src_surface->container;
7936 struct wined3d_texture *dst_texture = dst_surface->container;
7937 struct wined3d_context *context;
7938 RECT src_rect = *src_rect_in;
7939 RECT dst_rect = *dst_rect_in;
7940 struct wined3d_color_key alpha_test_key;
7942 /* Activate the destination context, set it up for blitting */
7943 context = context_acquire(device, dst_surface);
7945 /* Now load the surface */
7946 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
7947 && (surface_get_sub_resource(src_surface)->locations
7948 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE))
7949 == WINED3D_LOCATION_DRAWABLE
7950 && !wined3d_resource_is_offscreen(&src_texture->resource))
7952 /* Without FBO blits transferring from the drawable to the texture is
7953 * expensive, because we have to flip the data in sysmem. Since we can
7954 * flip in the blitter, we don't actually need that flip anyway. So we
7955 * use the surface's texture as scratch texture, and flip the source
7956 * rectangle instead. */
7957 surface_load_fb_texture(src_surface, FALSE, context);
7959 src_rect.top = wined3d_texture_get_level_height(src_texture, src_surface->texture_level) - src_rect.top;
7960 src_rect.bottom = wined3d_texture_get_level_height(src_texture, src_surface->texture_level) - src_rect.bottom;
7962 else
7963 wined3d_texture_load(src_texture, context, FALSE);
7965 context_apply_blit_state(context, device);
7967 if (!wined3d_resource_is_offscreen(&dst_texture->resource))
7968 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
7970 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
7972 const struct wined3d_format *fmt = src_texture->resource.format;
7973 alpha_test_key.color_space_low_value = 0;
7974 alpha_test_key.color_space_high_value = ~(((1u << fmt->alpha_size) - 1) << fmt->alpha_offset);
7975 color_key = &alpha_test_key;
7978 arbfp_blit_set(device->blit_priv, context, src_surface, color_key);
7980 /* Draw a textured quad */
7981 draw_textured_quad(src_surface, context, &src_rect, &dst_rect, filter);
7983 /* Leave the opengl state valid for blitting */
7984 arbfp_blit_unset(context->gl_info);
7986 if (wined3d_settings.strict_draw_ordering
7987 || (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture)))
7988 context->gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
7990 context_release(context);
7992 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_texture->resource.draw_binding);
7993 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_texture->resource.draw_binding);
7996 static HRESULT arbfp_blit_color_fill(struct wined3d_device *device, struct wined3d_rendertarget_view *view,
7997 const RECT *rect, const struct wined3d_color *color)
7999 FIXME("Color filling not implemented by arbfp_blit\n");
8000 return WINED3DERR_INVALIDCALL;
8003 static HRESULT arbfp_blit_depth_fill(struct wined3d_device *device, struct wined3d_rendertarget_view *view,
8004 const RECT *rect, DWORD clear_flags, float depth, DWORD stencil)
8006 FIXME("Depth/stencil filling not implemented by arbfp_blit.\n");
8007 return WINED3DERR_INVALIDCALL;
8010 const struct blit_shader arbfp_blit = {
8011 arbfp_blit_alloc,
8012 arbfp_blit_free,
8013 arbfp_blit_set,
8014 arbfp_blit_unset,
8015 arbfp_blit_supported,
8016 arbfp_blit_color_fill,
8017 arbfp_blit_depth_fill,
8018 arbfp_blit_surface,