wined3d: Mask out writes to unused render targets.
[wine.git] / dlls / wined3d / arb_program_shader.c
blobcefb6e5567cec0df27181abe4b5a7e48c11754c2
1 /*
2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
12 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
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"
32 #include <math.h>
33 #include <stdio.h>
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
42 /* Extract a line. Note that this modifies the source string. */
43 static char *get_line(char **ptr)
45 char *p, *q;
47 p = *ptr;
48 if (!(q = strstr(p, "\n")))
50 if (!*p) return NULL;
51 *ptr += strlen(p);
52 return p;
54 *q = '\0';
55 *ptr = q + 1;
57 return p;
60 static void shader_arb_dump_program_source(const char *source)
62 ULONG source_size;
63 char *ptr, *line, *tmp;
65 source_size = strlen(source) + 1;
66 tmp = HeapAlloc(GetProcessHeap(), 0, source_size);
67 if (!tmp)
69 ERR("Failed to allocate %u bytes for shader source.\n", source_size);
70 return;
72 memcpy(tmp, source, source_size);
74 ptr = tmp;
75 while ((line = get_line(&ptr))) FIXME(" %s\n", line);
76 FIXME("\n");
78 HeapFree(GetProcessHeap(), 0, tmp);
81 enum arb_helper_value
83 ARB_ZERO,
84 ARB_ONE,
85 ARB_TWO,
86 ARB_0001,
87 ARB_EPS,
89 ARB_VS_REL_OFFSET
92 static const char *arb_get_helper_value(enum wined3d_shader_type shader, enum arb_helper_value value)
94 if (shader == WINED3D_SHADER_TYPE_GEOMETRY)
96 ERR("Geometry shaders are unsupported\n");
97 return "bad";
100 if (shader == WINED3D_SHADER_TYPE_PIXEL)
102 switch (value)
104 case ARB_ZERO: return "ps_helper_const.x";
105 case ARB_ONE: return "ps_helper_const.y";
106 case ARB_TWO: return "coefmul.x";
107 case ARB_0001: return "ps_helper_const.xxxy";
108 case ARB_EPS: return "ps_helper_const.z";
109 default: break;
112 else
114 switch (value)
116 case ARB_ZERO: return "helper_const.x";
117 case ARB_ONE: return "helper_const.y";
118 case ARB_TWO: return "helper_const.z";
119 case ARB_EPS: return "helper_const.w";
120 case ARB_0001: return "helper_const.xxxy";
121 case ARB_VS_REL_OFFSET: return "rel_addr_const.y";
124 FIXME("Unmanaged %s shader helper constant requested: %u\n",
125 shader == WINED3D_SHADER_TYPE_PIXEL ? "pixel" : "vertex", value);
126 switch (value)
128 case ARB_ZERO: return "0.0";
129 case ARB_ONE: return "1.0";
130 case ARB_TWO: return "2.0";
131 case ARB_0001: return "{0.0, 0.0, 0.0, 1.0}";
132 case ARB_EPS: return "1e-8";
133 default: return "bad";
137 static inline BOOL ffp_clip_emul(const struct wined3d_state *state)
139 return state->lowest_disabled_stage < 7;
142 /* ARB_program_shader private data */
144 struct control_frame
146 struct list entry;
147 enum
150 IFC,
151 LOOP,
153 } type;
154 BOOL muting;
155 BOOL outer_loop;
156 union
158 unsigned int loop;
159 unsigned int ifc;
160 } no;
161 struct wined3d_shader_loop_control loop_control;
162 BOOL had_else;
165 struct arb_ps_np2fixup_info
167 struct ps_np2fixup_info super;
168 /* For ARB we need a offset value:
169 * With both GLSL and ARB mode the NP2 fixup information (the texture dimensions) are stored in a
170 * consecutive way (GLSL uses a uniform array). Since ARB doesn't know the notion of a "standalone"
171 * array we need an offset to the index inside the program local parameter array. */
172 UINT offset;
175 struct arb_ps_compile_args
177 struct ps_compile_args super;
178 WORD bools;
179 WORD clip; /* only a boolean, use a WORD for alignment */
180 unsigned char loop_ctrl[MAX_CONST_I][3];
183 struct stb_const_desc
185 unsigned char texunit;
186 UINT const_num;
189 struct arb_ps_compiled_shader
191 struct arb_ps_compile_args args;
192 struct arb_ps_np2fixup_info np2fixup_info;
193 struct stb_const_desc bumpenvmatconst[MAX_TEXTURES];
194 struct stb_const_desc luminanceconst[MAX_TEXTURES];
195 UINT int_consts[MAX_CONST_I];
196 GLuint prgId;
197 UINT ycorrection;
198 unsigned char numbumpenvmatconsts;
199 char num_int_consts;
202 struct arb_vs_compile_args
204 struct vs_compile_args super;
205 union
207 struct
209 WORD bools;
210 unsigned char clip_texcoord;
211 unsigned char clipplane_mask;
212 } boolclip;
213 DWORD boolclip_compare;
214 } clip;
215 DWORD ps_signature;
216 union
218 unsigned char samplers[4];
219 DWORD samplers_compare;
220 } vertex;
221 unsigned char loop_ctrl[MAX_CONST_I][3];
224 struct arb_vs_compiled_shader
226 struct arb_vs_compile_args args;
227 GLuint prgId;
228 UINT int_consts[MAX_CONST_I];
229 char num_int_consts;
230 char need_color_unclamp;
231 UINT pos_fixup;
234 struct recorded_instruction
236 struct wined3d_shader_instruction ins;
237 struct list entry;
240 struct shader_arb_ctx_priv
242 char addr_reg[20];
243 enum
245 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
246 ARB,
247 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
248 NV2,
249 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
251 } target_version;
253 const struct arb_vs_compile_args *cur_vs_args;
254 const struct arb_ps_compile_args *cur_ps_args;
255 const struct arb_ps_compiled_shader *compiled_fprog;
256 const struct arb_vs_compiled_shader *compiled_vprog;
257 struct arb_ps_np2fixup_info *cur_np2fixup_info;
258 struct list control_frames;
259 struct list record;
260 BOOL recording;
261 BOOL muted;
262 unsigned int num_loops, loop_depth, num_ifcs;
263 int aL;
265 unsigned int vs_clipplanes;
266 BOOL footer_written;
267 BOOL in_main_func;
269 /* For 3.0 vertex shaders */
270 const char *vs_output[MAX_REG_OUTPUT];
271 /* For 2.x and earlier vertex shaders */
272 const char *texcrd_output[8], *color_output[2], *fog_output;
274 /* 3.0 pshader input for compatibility with fixed function */
275 const char *ps_input[MAX_REG_INPUT];
278 struct ps_signature
280 struct wined3d_shader_signature_element *sig;
281 DWORD idx;
282 struct wine_rb_entry entry;
285 struct arb_pshader_private {
286 struct arb_ps_compiled_shader *gl_shaders;
287 UINT num_gl_shaders, shader_array_size;
288 BOOL has_signature_idx;
289 DWORD input_signature_idx;
290 DWORD clipplane_emulation;
291 BOOL clamp_consts;
294 struct arb_vshader_private {
295 struct arb_vs_compiled_shader *gl_shaders;
296 UINT num_gl_shaders, shader_array_size;
297 UINT rel_offset;
300 struct shader_arb_priv
302 GLuint current_vprogram_id;
303 GLuint current_fprogram_id;
304 const struct arb_ps_compiled_shader *compiled_fprog;
305 const struct arb_vs_compiled_shader *compiled_vprog;
306 GLuint depth_blt_vprogram_id;
307 GLuint depth_blt_fprogram_id_full[tex_type_count];
308 GLuint depth_blt_fprogram_id_masked[tex_type_count];
309 BOOL use_arbfp_fixed_func;
310 struct wine_rb_tree fragment_shaders;
311 BOOL last_ps_const_clamped;
312 BOOL last_vs_color_unclamp;
314 struct wine_rb_tree signature_tree;
315 DWORD ps_sig_number;
318 /* GL locking for state handlers is done by the caller. */
319 static BOOL need_rel_addr_const(const struct arb_vshader_private *shader_data,
320 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info)
322 if (shader_data->rel_offset) return TRUE;
323 if (!reg_maps->usesmova) return FALSE;
324 return !gl_info->supported[NV_VERTEX_PROGRAM2_OPTION];
327 /* Returns TRUE if result.clip from GL_NV_vertex_program2 should be used and FALSE otherwise */
328 static inline BOOL use_nv_clip(const struct wined3d_gl_info *gl_info)
330 return gl_info->supported[NV_VERTEX_PROGRAM2_OPTION]
331 && !(gl_info->quirks & WINED3D_QUIRK_NV_CLIP_BROKEN);
334 static BOOL need_helper_const(const struct arb_vshader_private *shader_data,
335 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info)
337 if (need_rel_addr_const(shader_data, reg_maps, gl_info)) return TRUE;
338 if (!gl_info->supported[NV_VERTEX_PROGRAM]) return TRUE; /* Need to init colors. */
339 if (gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT) return TRUE; /* Load the immval offset. */
340 if (gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) return TRUE; /* Have to init texcoords. */
341 if (!use_nv_clip(gl_info)) return TRUE; /* Init the clip texcoord */
342 if (reg_maps->usesnrm) return TRUE; /* 0.0 */
343 if (reg_maps->usespow) return TRUE; /* EPS, 0.0 and 1.0 */
344 return FALSE;
347 static unsigned int reserved_vs_const(const struct arb_vshader_private *shader_data,
348 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info)
350 unsigned int ret = 1;
351 /* We use one PARAM for the pos fixup, and in some cases one to load
352 * some immediate values into the shader. */
353 if (need_helper_const(shader_data, reg_maps, gl_info)) ++ret;
354 if (need_rel_addr_const(shader_data, reg_maps, gl_info)) ++ret;
355 return ret;
358 /* Loads floating point constants into the currently set ARB_vertex/fragment_program.
359 * When constant_list == NULL, it will load all the constants.
361 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
362 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
364 /* GL locking is done by the caller */
365 static unsigned int shader_arb_load_constantsF(struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
366 GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
368 local_constant* lconst;
369 DWORD i, j;
370 unsigned int ret;
372 if (TRACE_ON(d3d_constants))
374 for(i = 0; i < max_constants; i++) {
375 if(!dirty_consts[i]) continue;
376 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
377 constants[i * 4 + 0], constants[i * 4 + 1],
378 constants[i * 4 + 2], constants[i * 4 + 3]);
382 i = 0;
384 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
385 if (target_type == GL_FRAGMENT_PROGRAM_ARB && shader->reg_maps.shader_version.major == 1)
387 float lcl_const[4];
388 /* ps 1.x supports only 8 constants, clamp only those. When switching between 1.x and higher
389 * shaders, the first 8 constants are marked dirty for reload
391 for(; i < min(8, max_constants); i++) {
392 if(!dirty_consts[i]) continue;
393 dirty_consts[i] = 0;
395 j = 4 * i;
396 if (constants[j + 0] > 1.0f) lcl_const[0] = 1.0f;
397 else if (constants[j + 0] < -1.0f) lcl_const[0] = -1.0f;
398 else lcl_const[0] = constants[j + 0];
400 if (constants[j + 1] > 1.0f) lcl_const[1] = 1.0f;
401 else if (constants[j + 1] < -1.0f) lcl_const[1] = -1.0f;
402 else lcl_const[1] = constants[j + 1];
404 if (constants[j + 2] > 1.0f) lcl_const[2] = 1.0f;
405 else if (constants[j + 2] < -1.0f) lcl_const[2] = -1.0f;
406 else lcl_const[2] = constants[j + 2];
408 if (constants[j + 3] > 1.0f) lcl_const[3] = 1.0f;
409 else if (constants[j + 3] < -1.0f) lcl_const[3] = -1.0f;
410 else lcl_const[3] = constants[j + 3];
412 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
415 /* If further constants are dirty, reload them without clamping.
417 * The alternative is not to touch them, but then we cannot reset the dirty constant count
418 * to zero. That's bad for apps that only use PS 1.x shaders, because in that case the code
419 * above would always re-check the first 8 constants since max_constant remains at the init
420 * value
424 if (gl_info->supported[EXT_GPU_PROGRAM_PARAMETERS])
426 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
427 * or just reloading *all* constants at once
429 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, i, max_constants, constants + (i * 4)));
431 for(; i < max_constants; i++) {
432 if(!dirty_consts[i]) continue;
434 /* Find the next block of dirty constants */
435 dirty_consts[i] = 0;
436 j = i;
437 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
438 dirty_consts[i] = 0;
441 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
443 } else {
444 for(; i < max_constants; i++) {
445 if(dirty_consts[i]) {
446 dirty_consts[i] = 0;
447 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
451 checkGLcall("glProgramEnvParameter4fvARB()");
453 /* Load immediate constants */
454 if (shader->load_local_constsF)
456 if (TRACE_ON(d3d_shader))
458 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
460 GLfloat* values = (GLfloat*)lconst->value;
461 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
462 values[0], values[1], values[2], values[3]);
465 /* Immediate constants are clamped for 1.X shaders at loading times */
466 ret = 0;
467 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
469 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
470 ret = max(ret, lconst->idx + 1);
471 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
473 checkGLcall("glProgramEnvParameter4fvARB()");
474 return ret; /* The loaded immediate constants need reloading for the next shader */
475 } else {
476 return 0; /* No constants are dirty now */
481 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
483 static void shader_arb_load_np2fixup_constants(void *shader_priv,
484 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
486 const struct shader_arb_priv * priv = shader_priv;
488 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
489 if (!use_ps(state)) return;
491 if (priv->compiled_fprog && priv->compiled_fprog->np2fixup_info.super.active) {
492 const struct arb_ps_np2fixup_info* const fixup = &priv->compiled_fprog->np2fixup_info;
493 UINT i;
494 WORD active = fixup->super.active;
495 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
497 for (i = 0; active; active >>= 1, ++i)
499 const struct wined3d_texture *tex = state->textures[i];
500 const unsigned char idx = fixup->super.idx[i];
501 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
503 if (!(active & 1)) continue;
505 if (!tex) {
506 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
507 continue;
510 if (idx % 2)
512 tex_dim[2] = tex->pow2_matrix[0];
513 tex_dim[3] = tex->pow2_matrix[5];
515 else
517 tex_dim[0] = tex->pow2_matrix[0];
518 tex_dim[1] = tex->pow2_matrix[5];
522 for (i = 0; i < fixup->super.num_consts; ++i) {
523 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
524 fixup->offset + i, &np2fixup_constants[i * 4]));
529 /* GL locking is done by the caller. */
530 static void shader_arb_ps_local_constants(const struct arb_ps_compiled_shader *gl_shader,
531 const struct wined3d_context *context, const struct wined3d_state *state, UINT rt_height)
533 const struct wined3d_gl_info *gl_info = context->gl_info;
534 unsigned char i;
536 for(i = 0; i < gl_shader->numbumpenvmatconsts; i++)
538 int texunit = gl_shader->bumpenvmatconst[i].texunit;
540 /* The state manager takes care that this function is always called if the bump env matrix changes */
541 const float *data = (const float *)&state->texture_states[texunit][WINED3DTSS_BUMPENVMAT00];
542 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
543 gl_shader->bumpenvmatconst[i].const_num, data));
545 if (gl_shader->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED)
547 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
548 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
549 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
550 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
552 const float *scale = (const float *)&state->texture_states[texunit][WINED3DTSS_BUMPENVLSCALE];
553 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
554 gl_shader->luminanceconst[i].const_num, scale));
557 checkGLcall("Load bumpmap consts");
559 if(gl_shader->ycorrection != WINED3D_CONST_NUM_UNUSED)
561 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
562 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
563 * ycorrection.z: 1.0
564 * ycorrection.w: 0.0
566 float val[4];
567 val[0] = context->render_offscreen ? 0.0f : (float) rt_height;
568 val[1] = context->render_offscreen ? 1.0f : -1.0f;
569 val[2] = 1.0f;
570 val[3] = 0.0f;
571 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->ycorrection, val));
572 checkGLcall("y correction loading");
575 if (!gl_shader->num_int_consts) return;
577 for(i = 0; i < MAX_CONST_I; i++)
579 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
581 float val[4];
582 val[0] = (float)state->ps_consts_i[4 * i];
583 val[1] = (float)state->ps_consts_i[4 * i + 1];
584 val[2] = (float)state->ps_consts_i[4 * i + 2];
585 val[3] = -1.0f;
587 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->int_consts[i], val));
590 checkGLcall("Load ps int consts");
593 /* GL locking is done by the caller. */
594 static void shader_arb_vs_local_constants(const struct arb_vs_compiled_shader *gl_shader,
595 const struct wined3d_context *context, const struct wined3d_state *state)
597 const struct wined3d_gl_info *gl_info = context->gl_info;
598 float position_fixup[4];
599 unsigned char i;
601 /* Upload the position fixup */
602 shader_get_position_fixup(context, state, position_fixup);
603 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->pos_fixup, position_fixup));
605 if (!gl_shader->num_int_consts) return;
607 for(i = 0; i < MAX_CONST_I; i++)
609 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
611 float val[4];
612 val[0] = (float)state->vs_consts_i[4 * i];
613 val[1] = (float)state->vs_consts_i[4 * i + 1];
614 val[2] = (float)state->vs_consts_i[4 * i + 2];
615 val[3] = -1.0f;
617 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->int_consts[i], val));
620 checkGLcall("Load vs int consts");
624 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
626 * We only support float constants in ARB at the moment, so don't
627 * worry about the Integers or Booleans
629 /* GL locking is done by the caller (state handler) */
630 static void shader_arb_load_constants(const struct wined3d_context *context, char usePixelShader, char useVertexShader)
632 struct wined3d_device *device = context->swapchain->device;
633 struct wined3d_stateblock *stateBlock = device->stateBlock;
634 const struct wined3d_gl_info *gl_info = context->gl_info;
635 struct shader_arb_priv *priv = device->shader_priv;
637 if (useVertexShader)
639 struct wined3d_shader *vshader = stateBlock->state.vertex_shader;
640 const struct arb_vs_compiled_shader *gl_shader = priv->compiled_vprog;
642 /* Load DirectX 9 float constants for vertex shader */
643 device->highest_dirty_vs_const = shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
644 device->highest_dirty_vs_const, stateBlock->state.vs_consts_f, context->vshader_const_dirty);
645 shader_arb_vs_local_constants(gl_shader, context, &stateBlock->state);
648 if (usePixelShader)
650 struct wined3d_shader *pshader = stateBlock->state.pixel_shader;
651 const struct arb_ps_compiled_shader *gl_shader = priv->compiled_fprog;
652 UINT rt_height = device->fb.render_targets[0]->resource.height;
654 /* Load DirectX 9 float constants for pixel shader */
655 device->highest_dirty_ps_const = shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
656 device->highest_dirty_ps_const, stateBlock->state.ps_consts_f, context->pshader_const_dirty);
657 shader_arb_ps_local_constants(gl_shader, context, &stateBlock->state, rt_height);
661 static void shader_arb_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
663 struct wined3d_context *context = context_get_current();
665 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
666 * context. On a context switch the old context will be fully dirtified */
667 if (!context || context->swapchain->device != device) return;
669 memset(context->vshader_const_dirty + start, 1, sizeof(*context->vshader_const_dirty) * count);
670 device->highest_dirty_vs_const = max(device->highest_dirty_vs_const, start + count);
673 static void shader_arb_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
675 struct wined3d_context *context = context_get_current();
677 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
678 * context. On a context switch the old context will be fully dirtified */
679 if (!context || context->swapchain->device != device) return;
681 memset(context->pshader_const_dirty + start, 1, sizeof(*context->pshader_const_dirty) * count);
682 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, start + count);
685 static DWORD *local_const_mapping(const struct wined3d_shader *shader)
687 DWORD *ret;
688 DWORD idx = 0;
689 const local_constant *lconst;
691 if (shader->load_local_constsF || list_empty(&shader->constantsF))
692 return NULL;
694 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * shader->limits.constant_float);
695 if (!ret)
697 ERR("Out of memory\n");
698 return NULL;
701 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
703 ret[lconst->idx] = idx++;
705 return ret;
708 /* Generate the variable & register declarations for the ARB_vertex_program output target */
709 static DWORD shader_generate_arb_declarations(struct wined3d_shader *shader,
710 const struct wined3d_shader_reg_maps *reg_maps, struct wined3d_shader_buffer *buffer,
711 const struct wined3d_gl_info *gl_info, DWORD *lconst_map,
712 DWORD *num_clipplanes, struct shader_arb_ctx_priv *ctx)
714 DWORD i, next_local = 0;
715 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
716 unsigned max_constantsF;
717 const local_constant *lconst;
718 DWORD map;
720 /* In pixel shaders, all private constants are program local, we don't need anything
721 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
722 * If we need a private constant the GL implementation will squeeze it in somewhere
724 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
725 * immediate values. The posFixup is loaded using program.env for now, so always
726 * subtract one from the number of constants. If the shader uses indirect addressing,
727 * account for the helper const too because we have to declare all availabke d3d constants
728 * and don't know which are actually used.
730 if (pshader)
732 max_constantsF = gl_info->limits.arb_ps_native_constants;
733 /* 24 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value. */
734 if (max_constantsF < 24)
735 max_constantsF = gl_info->limits.arb_ps_float_constants;
737 else
739 const struct arb_vshader_private *shader_data = shader->backend_data;
740 max_constantsF = gl_info->limits.arb_vs_native_constants;
741 /* 96 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value.
742 * Also prevents max_constantsF from becoming less than 0 and
743 * wrapping . */
744 if (max_constantsF < 96)
745 max_constantsF = gl_info->limits.arb_vs_float_constants;
747 if (reg_maps->usesrelconstF)
749 DWORD highest_constf = 0, clip_limit;
751 max_constantsF -= reserved_vs_const(shader_data, reg_maps, gl_info);
752 max_constantsF -= count_bits(reg_maps->integer_constants);
754 for (i = 0; i < shader->limits.constant_float; ++i)
756 DWORD idx = i >> 5;
757 DWORD shift = i & 0x1f;
758 if(reg_maps->constf[idx] & (1 << shift)) highest_constf = i;
761 if(use_nv_clip(gl_info) && ctx->target_version >= NV2)
763 if(ctx->cur_vs_args->super.clip_enabled)
764 clip_limit = gl_info->limits.clipplanes;
765 else
766 clip_limit = 0;
768 else
770 unsigned int mask = ctx->cur_vs_args->clip.boolclip.clipplane_mask;
771 clip_limit = min(count_bits(mask), 4);
773 *num_clipplanes = min(clip_limit, max_constantsF - highest_constf - 1);
774 max_constantsF -= *num_clipplanes;
775 if(*num_clipplanes < clip_limit)
777 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes, gl_info->limits.clipplanes);
780 else
782 if (ctx->target_version >= NV2) *num_clipplanes = gl_info->limits.clipplanes;
783 else *num_clipplanes = min(gl_info->limits.clipplanes, 4);
787 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
789 if (map & 1) shader_addline(buffer, "TEMP R%u;\n", i);
792 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
794 if (map & 1) shader_addline(buffer, "ADDRESS A%u;\n", i);
797 if (pshader && reg_maps->shader_version.major == 1 && reg_maps->shader_version.minor <= 3)
799 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
801 if (map & 1) shader_addline(buffer, "TEMP T%u;\n", i);
805 /* Load local constants using the program-local space,
806 * this avoids reloading them each time the shader is used
808 if (lconst_map)
810 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
812 shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
813 lconst_map[lconst->idx]);
814 next_local = max(next_local, lconst_map[lconst->idx] + 1);
818 /* After subtracting privately used constants from the hardware limit(they are loaded as
819 * local constants), make sure the shader doesn't violate the env constant limit
821 if(pshader)
823 max_constantsF = min(max_constantsF, gl_info->limits.arb_ps_float_constants);
825 else
827 max_constantsF = min(max_constantsF, gl_info->limits.arb_vs_float_constants);
830 /* Avoid declaring more constants than needed */
831 max_constantsF = min(max_constantsF, shader->limits.constant_float);
833 /* we use the array-based constants array if the local constants are marked for loading,
834 * because then we use indirect addressing, or when the local constant list is empty,
835 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
836 * local constants do not declare the loaded constants as an array because ARB compilers usually
837 * do not optimize unused constants away
839 if (reg_maps->usesrelconstF)
841 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
842 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
843 max_constantsF, max_constantsF - 1);
844 } else {
845 for(i = 0; i < max_constantsF; i++) {
846 DWORD idx, mask;
847 idx = i >> 5;
848 mask = 1 << (i & 0x1f);
849 if (!shader_constant_is_local(shader, i) && (reg_maps->constf[idx] & mask))
851 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
856 return next_local;
859 static const char * const shift_tab[] = {
860 "dummy", /* 0 (none) */
861 "coefmul.x", /* 1 (x2) */
862 "coefmul.y", /* 2 (x4) */
863 "coefmul.z", /* 3 (x8) */
864 "coefmul.w", /* 4 (x16) */
865 "dummy", /* 5 (x32) */
866 "dummy", /* 6 (x64) */
867 "dummy", /* 7 (x128) */
868 "dummy", /* 8 (d256) */
869 "dummy", /* 9 (d128) */
870 "dummy", /* 10 (d64) */
871 "dummy", /* 11 (d32) */
872 "coefdiv.w", /* 12 (d16) */
873 "coefdiv.z", /* 13 (d8) */
874 "coefdiv.y", /* 14 (d4) */
875 "coefdiv.x" /* 15 (d2) */
878 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction *ins,
879 const struct wined3d_shader_dst_param *dst, char *write_mask)
881 char *ptr = write_mask;
883 if (dst->write_mask != WINED3DSP_WRITEMASK_ALL)
885 *ptr++ = '.';
886 if (dst->write_mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
887 if (dst->write_mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
888 if (dst->write_mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
889 if (dst->write_mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
892 *ptr = '\0';
895 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param *param, BOOL fixup, char *swizzle_str)
897 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
898 * but addressed as "rgba". To fix this we need to swap the register's x
899 * and z components. */
900 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
901 char *ptr = swizzle_str;
903 /* swizzle bits fields: wwzzyyxx */
904 DWORD swizzle = param->swizzle;
905 DWORD swizzle_x = swizzle & 0x03;
906 DWORD swizzle_y = (swizzle >> 2) & 0x03;
907 DWORD swizzle_z = (swizzle >> 4) & 0x03;
908 DWORD swizzle_w = (swizzle >> 6) & 0x03;
910 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
911 * generate a swizzle string. Unless we need to our own swizzling. */
912 if (swizzle != WINED3DSP_NOSWIZZLE || fixup)
914 *ptr++ = '.';
915 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
916 *ptr++ = swizzle_chars[swizzle_x];
917 } else {
918 *ptr++ = swizzle_chars[swizzle_x];
919 *ptr++ = swizzle_chars[swizzle_y];
920 *ptr++ = swizzle_chars[swizzle_z];
921 *ptr++ = swizzle_chars[swizzle_w];
925 *ptr = '\0';
928 static void shader_arb_request_a0(const struct wined3d_shader_instruction *ins, const char *src)
930 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
931 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
933 if (!strcmp(priv->addr_reg, src)) return;
935 strcpy(priv->addr_reg, src);
936 shader_addline(buffer, "ARL A0.x, %s;\n", src);
939 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
940 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr);
942 static void shader_arb_get_register_name(const struct wined3d_shader_instruction *ins,
943 const struct wined3d_shader_register *reg, char *register_name, BOOL *is_color)
945 /* oPos, oFog and oPts in D3D */
946 static const char * const rastout_reg_names[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
947 struct wined3d_shader *shader = ins->ctx->shader;
948 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
949 BOOL pshader = shader_is_pshader_version(reg_maps->shader_version.type);
950 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
952 *is_color = FALSE;
954 switch (reg->type)
956 case WINED3DSPR_TEMP:
957 sprintf(register_name, "R%u", reg->idx);
958 break;
960 case WINED3DSPR_INPUT:
961 if (pshader)
963 if (reg_maps->shader_version.major < 3)
965 if (!reg->idx) strcpy(register_name, "fragment.color.primary");
966 else strcpy(register_name, "fragment.color.secondary");
968 else
970 if(reg->rel_addr)
972 char rel_reg[50];
973 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
975 if (!strcmp(rel_reg, "**aL_emul**"))
977 DWORD idx = ctx->aL + reg->idx;
978 if(idx < MAX_REG_INPUT)
980 strcpy(register_name, ctx->ps_input[idx]);
982 else
984 ERR("Pixel shader input register out of bounds: %u\n", idx);
985 sprintf(register_name, "out_of_bounds_%u", idx);
988 else if (reg_maps->input_registers & 0x0300)
990 /* There are two ways basically:
992 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
993 * That means trouble if the loop also contains a breakc or if the control values
994 * aren't local constants.
995 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
996 * source dynamically. The trouble is that we cannot simply read aL.y because it
997 * is an ADDRESS register. We could however push it, load .zw with a value and use
998 * ADAC to load the condition code register and pop it again afterwards
1000 FIXME("Relative input register addressing with more than 8 registers\n");
1002 /* This is better than nothing for now */
1003 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
1005 else if(ctx->cur_ps_args->super.vp_mode != vertexshader)
1007 /* This is problematic because we'd have to consult the ctx->ps_input strings
1008 * for where to find the varying. Some may be "0.0", others can be texcoords or
1009 * colors. This needs either a pipeline replacement to make the vertex shader feed
1010 * proper varyings, or loop unrolling
1012 * For now use the texcoords and hope for the best
1014 FIXME("Non-vertex shader varying input with indirect addressing\n");
1015 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
1017 else
1019 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
1020 * pulls GL_NV_fragment_program2 in
1022 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
1025 else
1027 if(reg->idx < MAX_REG_INPUT)
1029 strcpy(register_name, ctx->ps_input[reg->idx]);
1031 else
1033 ERR("Pixel shader input register out of bounds: %u\n", reg->idx);
1034 sprintf(register_name, "out_of_bounds_%u", reg->idx);
1039 else
1041 if (ctx->cur_vs_args->super.swizzle_map & (1 << reg->idx)) *is_color = TRUE;
1042 sprintf(register_name, "vertex.attrib[%u]", reg->idx);
1044 break;
1046 case WINED3DSPR_CONST:
1047 if (!pshader && reg->rel_addr)
1049 const struct arb_vshader_private *shader_data = shader->backend_data;
1050 UINT rel_offset = shader_data->rel_offset;
1051 BOOL aL = FALSE;
1052 char rel_reg[50];
1053 if (reg_maps->shader_version.major < 2)
1055 sprintf(rel_reg, "A0.x");
1056 } else {
1057 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
1058 if(ctx->target_version == ARB) {
1059 if (!strcmp(rel_reg, "**aL_emul**"))
1061 aL = TRUE;
1062 } else {
1063 shader_arb_request_a0(ins, rel_reg);
1064 sprintf(rel_reg, "A0.x");
1068 if(aL)
1069 sprintf(register_name, "C[%u]", ctx->aL + reg->idx);
1070 else if (reg->idx >= rel_offset)
1071 sprintf(register_name, "C[%s + %u]", rel_reg, reg->idx - rel_offset);
1072 else
1073 sprintf(register_name, "C[%s - %u]", rel_reg, rel_offset - reg->idx);
1075 else
1077 if (reg_maps->usesrelconstF)
1078 sprintf(register_name, "C[%u]", reg->idx);
1079 else
1080 sprintf(register_name, "C%u", reg->idx);
1082 break;
1084 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1085 if (pshader)
1087 if (reg_maps->shader_version.major == 1
1088 && reg_maps->shader_version.minor <= 3)
1090 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
1091 * and as source to most instructions. For some instructions it is the texcoord
1092 * input. Those instructions know about the special use
1094 sprintf(register_name, "T%u", reg->idx);
1095 } else {
1096 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
1097 sprintf(register_name, "fragment.texcoord[%u]", reg->idx);
1100 else
1102 if (reg_maps->shader_version.major == 1 || ctx->target_version >= NV2)
1104 sprintf(register_name, "A%u", reg->idx);
1106 else
1108 sprintf(register_name, "A%u_SHADOW", reg->idx);
1111 break;
1113 case WINED3DSPR_COLOROUT:
1114 if (ctx->cur_ps_args->super.srgb_correction && !reg->idx)
1116 strcpy(register_name, "TMP_COLOR");
1118 else
1120 if(ctx->cur_ps_args->super.srgb_correction) FIXME("sRGB correction on higher render targets\n");
1121 if (reg_maps->rt_mask > 1)
1123 sprintf(register_name, "result.color[%u]", reg->idx);
1125 else
1127 strcpy(register_name, "result.color");
1130 break;
1132 case WINED3DSPR_RASTOUT:
1133 if(reg->idx == 1) sprintf(register_name, "%s", ctx->fog_output);
1134 else sprintf(register_name, "%s", rastout_reg_names[reg->idx]);
1135 break;
1137 case WINED3DSPR_DEPTHOUT:
1138 strcpy(register_name, "result.depth");
1139 break;
1141 case WINED3DSPR_ATTROUT:
1142 /* case WINED3DSPR_OUTPUT: */
1143 if (pshader) sprintf(register_name, "oD[%u]", reg->idx);
1144 else strcpy(register_name, ctx->color_output[reg->idx]);
1145 break;
1147 case WINED3DSPR_TEXCRDOUT:
1148 if (pshader)
1150 sprintf(register_name, "oT[%u]", reg->idx);
1152 else
1154 if (reg_maps->shader_version.major < 3)
1156 strcpy(register_name, ctx->texcrd_output[reg->idx]);
1158 else
1160 strcpy(register_name, ctx->vs_output[reg->idx]);
1163 break;
1165 case WINED3DSPR_LOOP:
1166 if(ctx->target_version >= NV2)
1168 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
1169 if(pshader) sprintf(register_name, "A0.x");
1170 else sprintf(register_name, "aL.y");
1172 else
1174 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
1175 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
1176 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
1177 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
1178 * indexing
1180 sprintf(register_name, "**aL_emul**");
1183 break;
1185 case WINED3DSPR_CONSTINT:
1186 sprintf(register_name, "I%u", reg->idx);
1187 break;
1189 case WINED3DSPR_MISCTYPE:
1190 if (!reg->idx)
1192 sprintf(register_name, "vpos");
1194 else if(reg->idx == 1)
1196 sprintf(register_name, "fragment.facing.x");
1198 else
1200 FIXME("Unknown MISCTYPE register index %u\n", reg->idx);
1202 break;
1204 default:
1205 FIXME("Unhandled register type %#x[%u]\n", reg->type, reg->idx);
1206 sprintf(register_name, "unrecognized_register[%u]", reg->idx);
1207 break;
1211 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction *ins,
1212 const struct wined3d_shader_dst_param *wined3d_dst, char *str)
1214 char register_name[255];
1215 char write_mask[6];
1216 BOOL is_color;
1218 shader_arb_get_register_name(ins, &wined3d_dst->reg, register_name, &is_color);
1219 strcpy(str, register_name);
1221 shader_arb_get_write_mask(ins, wined3d_dst, write_mask);
1222 strcat(str, write_mask);
1225 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
1227 switch(channel_source)
1229 case CHANNEL_SOURCE_ZERO: return "0";
1230 case CHANNEL_SOURCE_ONE: return "1";
1231 case CHANNEL_SOURCE_X: return "x";
1232 case CHANNEL_SOURCE_Y: return "y";
1233 case CHANNEL_SOURCE_Z: return "z";
1234 case CHANNEL_SOURCE_W: return "w";
1235 default:
1236 FIXME("Unhandled channel source %#x\n", channel_source);
1237 return "undefined";
1241 static void gen_color_correction(struct wined3d_shader_buffer *buffer, const char *reg,
1242 DWORD dst_mask, const char *one, const char *two, struct color_fixup_desc fixup)
1244 DWORD mask;
1246 if (is_complex_fixup(fixup))
1248 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
1249 FIXME("Complex fixup (%#x) not supported\n", complex_fixup);
1250 return;
1253 mask = 0;
1254 if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1255 if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1256 if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1257 if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1258 mask &= dst_mask;
1260 if (mask)
1262 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
1263 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
1264 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
1267 mask = 0;
1268 if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
1269 if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
1270 if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
1271 if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
1272 mask &= dst_mask;
1274 if (mask)
1276 char reg_mask[6];
1277 char *ptr = reg_mask;
1279 if (mask != WINED3DSP_WRITEMASK_ALL)
1281 *ptr++ = '.';
1282 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1283 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1284 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1285 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1287 *ptr = '\0';
1289 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
1293 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction *ins)
1295 DWORD mod;
1296 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1297 if (!ins->dst_count) return "";
1299 mod = ins->dst[0].modifiers;
1301 /* Silently ignore PARTIALPRECISION if its not supported */
1302 if(priv->target_version == ARB) mod &= ~WINED3DSPDM_PARTIALPRECISION;
1304 if(mod & WINED3DSPDM_MSAMPCENTROID)
1306 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1307 mod &= ~WINED3DSPDM_MSAMPCENTROID;
1310 switch(mod)
1312 case WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION:
1313 return "H_SAT";
1315 case WINED3DSPDM_SATURATE:
1316 return "_SAT";
1318 case WINED3DSPDM_PARTIALPRECISION:
1319 return "H";
1321 case 0:
1322 return "";
1324 default:
1325 FIXME("Unknown modifiers 0x%08x\n", mod);
1326 return "";
1330 #define TEX_PROJ 0x1
1331 #define TEX_BIAS 0x2
1332 #define TEX_LOD 0x4
1333 #define TEX_DERIV 0x10
1335 static void shader_hw_sample(const struct wined3d_shader_instruction *ins, DWORD sampler_idx,
1336 const char *dst_str, const char *coord_reg, WORD flags, const char *dsx, const char *dsy)
1338 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1339 DWORD sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
1340 const struct wined3d_texture *texture;
1341 const char *tex_type;
1342 BOOL np2_fixup = FALSE;
1343 struct wined3d_shader *shader = ins->ctx->shader;
1344 struct wined3d_device *device = shader->device;
1345 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1346 const char *mod;
1347 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
1349 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1350 if(!pshader) sampler_idx += MAX_FRAGMENT_SAMPLERS;
1352 switch(sampler_type) {
1353 case WINED3DSTT_1D:
1354 tex_type = "1D";
1355 break;
1357 case WINED3DSTT_2D:
1358 texture = device->stateBlock->state.textures[sampler_idx];
1359 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1361 tex_type = "RECT";
1362 } else {
1363 tex_type = "2D";
1365 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1367 if (priv->cur_np2fixup_info->super.active & (1 << sampler_idx))
1369 if (flags) FIXME("Only ordinary sampling from NP2 textures is supported.\n");
1370 else np2_fixup = TRUE;
1373 break;
1375 case WINED3DSTT_VOLUME:
1376 tex_type = "3D";
1377 break;
1379 case WINED3DSTT_CUBE:
1380 tex_type = "CUBE";
1381 break;
1383 default:
1384 ERR("Unexpected texture type %d\n", sampler_type);
1385 tex_type = "";
1388 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1389 * so don't use shader_arb_get_modifier
1391 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) mod = "_SAT";
1392 else mod = "";
1394 /* Fragment samplers always have indentity mapping */
1395 if(sampler_idx >= MAX_FRAGMENT_SAMPLERS)
1397 sampler_idx = priv->cur_vs_args->vertex.samplers[sampler_idx - MAX_FRAGMENT_SAMPLERS];
1400 if (flags & TEX_DERIV)
1402 if(flags & TEX_PROJ) FIXME("Projected texture sampling with custom derivatives\n");
1403 if(flags & TEX_BIAS) FIXME("Biased texture sampling with custom derivatives\n");
1404 shader_addline(buffer, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg,
1405 dsx, dsy,sampler_idx, tex_type);
1407 else if(flags & TEX_LOD)
1409 if(flags & TEX_PROJ) FIXME("Projected texture sampling with explicit lod\n");
1410 if(flags & TEX_BIAS) FIXME("Biased texture sampling with explicit lod\n");
1411 shader_addline(buffer, "TXL%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg,
1412 sampler_idx, tex_type);
1414 else if (flags & TEX_BIAS)
1416 /* Shouldn't be possible, but let's check for it */
1417 if(flags & TEX_PROJ) FIXME("Biased and Projected texture sampling\n");
1418 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1419 shader_addline(buffer, "TXB%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1421 else if (flags & TEX_PROJ)
1423 shader_addline(buffer, "TXP%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1425 else
1427 if (np2_fixup)
1429 const unsigned char idx = priv->cur_np2fixup_info->super.idx[sampler_idx];
1430 shader_addline(buffer, "MUL TA, np2fixup[%u].%s, %s;\n", idx >> 1,
1431 (idx % 2) ? "zwxy" : "xyzw", coord_reg);
1433 shader_addline(buffer, "TEX%s %s, TA, texture[%u], %s;\n", mod, dst_str, sampler_idx, tex_type);
1435 else
1436 shader_addline(buffer, "TEX%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1439 if (pshader)
1441 gen_color_correction(buffer, dst_str, ins->dst[0].write_mask,
1442 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL, ARB_ONE),
1443 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL, ARB_TWO),
1444 priv->cur_ps_args->super.color_fixup[sampler_idx]);
1448 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
1449 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr)
1451 /* Generate a line that does the input modifier computation and return the input register to use */
1452 BOOL is_color = FALSE;
1453 char regstr[256];
1454 char swzstr[20];
1455 int insert_line;
1456 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1457 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1458 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
1459 const char *two = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_TWO);
1461 /* Assume a new line will be added */
1462 insert_line = 1;
1464 /* Get register name */
1465 shader_arb_get_register_name(ins, &src->reg, regstr, &is_color);
1466 shader_arb_get_swizzle(src, is_color, swzstr);
1468 switch (src->modifiers)
1470 case WINED3DSPSM_NONE:
1471 sprintf(outregstr, "%s%s", regstr, swzstr);
1472 insert_line = 0;
1473 break;
1474 case WINED3DSPSM_NEG:
1475 sprintf(outregstr, "-%s%s", regstr, swzstr);
1476 insert_line = 0;
1477 break;
1478 case WINED3DSPSM_BIAS:
1479 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
1480 break;
1481 case WINED3DSPSM_BIASNEG:
1482 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
1483 break;
1484 case WINED3DSPSM_SIGN:
1485 shader_addline(buffer, "MAD T%c, %s, %s, -%s;\n", 'A' + tmpreg, regstr, two, one);
1486 break;
1487 case WINED3DSPSM_SIGNNEG:
1488 shader_addline(buffer, "MAD T%c, %s, -%s, %s;\n", 'A' + tmpreg, regstr, two, one);
1489 break;
1490 case WINED3DSPSM_COMP:
1491 shader_addline(buffer, "SUB T%c, %s, %s;\n", 'A' + tmpreg, one, regstr);
1492 break;
1493 case WINED3DSPSM_X2:
1494 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
1495 break;
1496 case WINED3DSPSM_X2NEG:
1497 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
1498 break;
1499 case WINED3DSPSM_DZ:
1500 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
1501 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1502 break;
1503 case WINED3DSPSM_DW:
1504 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
1505 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1506 break;
1507 case WINED3DSPSM_ABS:
1508 if(ctx->target_version >= NV2) {
1509 sprintf(outregstr, "|%s%s|", regstr, swzstr);
1510 insert_line = 0;
1511 } else {
1512 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1514 break;
1515 case WINED3DSPSM_ABSNEG:
1516 if(ctx->target_version >= NV2) {
1517 sprintf(outregstr, "-|%s%s|", regstr, swzstr);
1518 } else {
1519 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1520 sprintf(outregstr, "-T%c%s", 'A' + tmpreg, swzstr);
1522 insert_line = 0;
1523 break;
1524 default:
1525 sprintf(outregstr, "%s%s", regstr, swzstr);
1526 insert_line = 0;
1529 /* Return modified or original register, with swizzle */
1530 if (insert_line)
1531 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
1534 static void pshader_hw_bem(const struct wined3d_shader_instruction *ins)
1536 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1537 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1538 char dst_name[50];
1539 char src_name[2][50];
1540 DWORD sampler_code = dst->reg.idx;
1542 shader_arb_get_dst_param(ins, dst, dst_name);
1544 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1546 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1547 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1548 * temps is done.
1550 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1551 shader_addline(buffer, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
1552 shader_addline(buffer, "DP3 TC.r, TA, %s;\n", src_name[1]);
1553 shader_addline(buffer, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
1554 shader_addline(buffer, "DP3 TC.g, TA, %s;\n", src_name[1]);
1556 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1557 shader_addline(buffer, "ADD %s, %s, TC;\n", dst_name, src_name[0]);
1560 static DWORD negate_modifiers(DWORD mod, char *extra_char)
1562 *extra_char = ' ';
1563 switch(mod)
1565 case WINED3DSPSM_NONE: return WINED3DSPSM_NEG;
1566 case WINED3DSPSM_NEG: return WINED3DSPSM_NONE;
1567 case WINED3DSPSM_BIAS: return WINED3DSPSM_BIASNEG;
1568 case WINED3DSPSM_BIASNEG: return WINED3DSPSM_BIAS;
1569 case WINED3DSPSM_SIGN: return WINED3DSPSM_SIGNNEG;
1570 case WINED3DSPSM_SIGNNEG: return WINED3DSPSM_SIGN;
1571 case WINED3DSPSM_COMP: *extra_char = '-'; return WINED3DSPSM_COMP;
1572 case WINED3DSPSM_X2: return WINED3DSPSM_X2NEG;
1573 case WINED3DSPSM_X2NEG: return WINED3DSPSM_X2;
1574 case WINED3DSPSM_DZ: *extra_char = '-'; return WINED3DSPSM_DZ;
1575 case WINED3DSPSM_DW: *extra_char = '-'; return WINED3DSPSM_DW;
1576 case WINED3DSPSM_ABS: return WINED3DSPSM_ABSNEG;
1577 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
1579 FIXME("Unknown modifier %u\n", mod);
1580 return mod;
1583 static void pshader_hw_cnd(const struct wined3d_shader_instruction *ins)
1585 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1586 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1587 char dst_name[50];
1588 char src_name[3][50];
1589 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1590 ins->ctx->reg_maps->shader_version.minor);
1591 BOOL is_color;
1593 shader_arb_get_dst_param(ins, dst, dst_name);
1594 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1596 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
1597 if (shader_version <= WINED3D_SHADER_VERSION(1, 3) && ins->coissue)
1599 shader_addline(buffer, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[1]);
1600 } else {
1601 struct wined3d_shader_src_param src0_copy = ins->src[0];
1602 char extra_neg;
1604 /* src0 may have a negate srcmod set, so we can't blindly add "-" to the name */
1605 src0_copy.modifiers = negate_modifiers(src0_copy.modifiers, &extra_neg);
1607 shader_arb_get_src_param(ins, &src0_copy, 0, src_name[0]);
1608 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1609 shader_addline(buffer, "ADD TA, %c%s, coefdiv.x;\n", extra_neg, src_name[0]);
1610 /* No modifiers supported on CMP */
1611 shader_addline(buffer, "CMP %s, TA, %s, %s;\n", dst_name, src_name[1], src_name[2]);
1613 /* _SAT on CMP doesn't make much sense, but it is not a pure NOP */
1614 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
1616 shader_arb_get_register_name(ins, &dst->reg, src_name[0], &is_color);
1617 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, dst_name);
1622 static void pshader_hw_cmp(const struct wined3d_shader_instruction *ins)
1624 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1625 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1626 char dst_name[50];
1627 char src_name[3][50];
1628 BOOL is_color;
1630 shader_arb_get_dst_param(ins, dst, dst_name);
1632 /* Generate input register names (with modifiers) */
1633 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1634 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1635 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1637 /* No modifiers are supported on CMP */
1638 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
1639 src_name[0], src_name[2], src_name[1]);
1641 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
1643 shader_arb_get_register_name(ins, &dst->reg, src_name[0], &is_color);
1644 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name[0]);
1648 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1649 * dst = dot2(src0, src1) + src2 */
1650 static void pshader_hw_dp2add(const struct wined3d_shader_instruction *ins)
1652 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1653 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1654 char dst_name[50];
1655 char src_name[3][50];
1656 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1658 shader_arb_get_dst_param(ins, dst, dst_name);
1659 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1660 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1662 if(ctx->target_version >= NV3)
1664 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1665 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1666 shader_addline(buffer, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1667 dst_name, src_name[0], src_name[1], src_name[2]);
1669 else if(ctx->target_version >= NV2)
1671 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1672 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1673 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1674 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1676 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1678 * .xyxy and other swizzles that we could get with this are not valid in
1679 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1681 struct wined3d_shader_src_param tmp_param = ins->src[1];
1682 DWORD swizzle = tmp_param.swizzle & 0xf; /* Selects .xy */
1683 tmp_param.swizzle = swizzle | (swizzle << 4); /* Creates .xyxy */
1685 shader_arb_get_src_param(ins, &tmp_param, 1, src_name[1]);
1687 shader_addline(buffer, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1688 dst_name, src_name[2], src_name[0], src_name[1]);
1690 else
1692 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1693 /* 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]
1694 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1696 shader_addline(buffer, "MOV TA, %s;\n", src_name[0]);
1697 shader_addline(buffer, "MOV TA.z, 0.0;\n");
1698 shader_addline(buffer, "DP3 TA, TA, %s;\n", src_name[1]);
1699 shader_addline(buffer, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[2]);
1703 /* Map the opcode 1-to-1 to the GL code */
1704 static void shader_hw_map2gl(const struct wined3d_shader_instruction *ins)
1706 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1707 const char *instruction;
1708 char arguments[256], dst_str[50];
1709 unsigned int i;
1710 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1712 switch (ins->handler_idx)
1714 case WINED3DSIH_ABS: instruction = "ABS"; break;
1715 case WINED3DSIH_ADD: instruction = "ADD"; break;
1716 case WINED3DSIH_CRS: instruction = "XPD"; break;
1717 case WINED3DSIH_DP3: instruction = "DP3"; break;
1718 case WINED3DSIH_DP4: instruction = "DP4"; break;
1719 case WINED3DSIH_DST: instruction = "DST"; break;
1720 case WINED3DSIH_FRC: instruction = "FRC"; break;
1721 case WINED3DSIH_LIT: instruction = "LIT"; break;
1722 case WINED3DSIH_LRP: instruction = "LRP"; break;
1723 case WINED3DSIH_MAD: instruction = "MAD"; break;
1724 case WINED3DSIH_MAX: instruction = "MAX"; break;
1725 case WINED3DSIH_MIN: instruction = "MIN"; break;
1726 case WINED3DSIH_MOV: instruction = "MOV"; break;
1727 case WINED3DSIH_MUL: instruction = "MUL"; break;
1728 case WINED3DSIH_SGE: instruction = "SGE"; break;
1729 case WINED3DSIH_SLT: instruction = "SLT"; break;
1730 case WINED3DSIH_SUB: instruction = "SUB"; break;
1731 case WINED3DSIH_MOVA:instruction = "ARR"; break;
1732 case WINED3DSIH_DSX: instruction = "DDX"; break;
1733 default: instruction = "";
1734 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1735 break;
1738 /* Note that shader_arb_add_dst_param() adds spaces. */
1739 arguments[0] = '\0';
1740 shader_arb_get_dst_param(ins, dst, dst_str);
1741 for (i = 0; i < ins->src_count; ++i)
1743 char operand[100];
1744 strcat(arguments, ", ");
1745 shader_arb_get_src_param(ins, &ins->src[i], i, operand);
1746 strcat(arguments, operand);
1748 shader_addline(buffer, "%s%s %s%s;\n", instruction, shader_arb_get_modifier(ins), dst_str, arguments);
1751 static void shader_hw_nop(const struct wined3d_shader_instruction *ins)
1753 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1754 shader_addline(buffer, "NOP;\n");
1757 static void shader_hw_mov(const struct wined3d_shader_instruction *ins)
1759 struct wined3d_shader *shader = ins->ctx->shader;
1760 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1761 BOOL pshader = shader_is_pshader_version(reg_maps->shader_version.type);
1762 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1763 const char *zero = arb_get_helper_value(reg_maps->shader_version.type, ARB_ZERO);
1764 const char *one = arb_get_helper_value(reg_maps->shader_version.type, ARB_ONE);
1765 const char *two = arb_get_helper_value(reg_maps->shader_version.type, ARB_TWO);
1767 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1768 char src0_param[256];
1770 if (ins->handler_idx == WINED3DSIH_MOVA)
1772 const struct arb_vshader_private *shader_data = shader->backend_data;
1773 char write_mask[6];
1774 const char *offset = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_VS_REL_OFFSET);
1776 if(ctx->target_version >= NV2) {
1777 shader_hw_map2gl(ins);
1778 return;
1780 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1781 shader_arb_get_write_mask(ins, &ins->dst[0], write_mask);
1783 /* This implements the mova formula used in GLSL. The first two instructions
1784 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1785 * in this case:
1786 * mova A0.x, 0.0
1788 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1790 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1791 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1793 shader_addline(buffer, "SGE A0_SHADOW%s, %s, %s;\n", write_mask, src0_param, zero);
1794 shader_addline(buffer, "MAD A0_SHADOW%s, A0_SHADOW, %s, -%s;\n", write_mask, two, one);
1796 shader_addline(buffer, "ABS TA%s, %s;\n", write_mask, src0_param);
1797 shader_addline(buffer, "ADD TA%s, TA, rel_addr_const.x;\n", write_mask);
1798 shader_addline(buffer, "FLR TA%s, TA;\n", write_mask);
1799 if (shader_data->rel_offset)
1801 shader_addline(buffer, "ADD TA%s, TA, %s;\n", write_mask, offset);
1803 shader_addline(buffer, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask);
1805 ((struct shader_arb_ctx_priv *)ins->ctx->backend_data)->addr_reg[0] = '\0';
1807 else if (reg_maps->shader_version.major == 1
1808 && !shader_is_pshader_version(reg_maps->shader_version.type)
1809 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1811 const struct arb_vshader_private *shader_data = shader->backend_data;
1812 src0_param[0] = '\0';
1814 if (shader_data->rel_offset)
1816 const char *offset = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_VS_REL_OFFSET);
1817 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1818 shader_addline(buffer, "ADD TA.x, %s, %s;\n", src0_param, offset);
1819 shader_addline(buffer, "ARL A0.x, TA.x;\n");
1821 else
1823 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1824 * with more than one component. Thus replicate the first source argument over all
1825 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1826 struct wined3d_shader_src_param tmp_src = ins->src[0];
1827 tmp_src.swizzle = (tmp_src.swizzle & 0x3) * 0x55;
1828 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1829 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1832 else if (ins->dst[0].reg.type == WINED3DSPR_COLOROUT && !ins->dst[0].reg.idx && pshader)
1834 if (ctx->cur_ps_args->super.srgb_correction && shader->u.ps.color0_mov)
1836 shader_addline(buffer, "#mov handled in srgb write code\n");
1837 return;
1839 shader_hw_map2gl(ins);
1841 else
1843 shader_hw_map2gl(ins);
1847 static void pshader_hw_texkill(const struct wined3d_shader_instruction *ins)
1849 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1850 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1851 char reg_dest[40];
1853 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1854 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1856 shader_arb_get_dst_param(ins, dst, reg_dest);
1858 if (ins->ctx->reg_maps->shader_version.major >= 2)
1860 const char *kilsrc = "TA";
1861 BOOL is_color;
1863 shader_arb_get_register_name(ins, &dst->reg, reg_dest, &is_color);
1864 if(dst->write_mask == WINED3DSP_WRITEMASK_ALL)
1866 kilsrc = reg_dest;
1868 else
1870 /* Sigh. KIL doesn't support swizzles/writemasks. KIL passes a writemask, but ".xy" for example
1871 * is not valid as a swizzle in ARB (needs ".xyyy"). Use SWZ to load the register properly, and set
1872 * masked out components to 0(won't kill)
1874 char x = '0', y = '0', z = '0', w = '0';
1875 if(dst->write_mask & WINED3DSP_WRITEMASK_0) x = 'x';
1876 if(dst->write_mask & WINED3DSP_WRITEMASK_1) y = 'y';
1877 if(dst->write_mask & WINED3DSP_WRITEMASK_2) z = 'z';
1878 if(dst->write_mask & WINED3DSP_WRITEMASK_3) w = 'w';
1879 shader_addline(buffer, "SWZ TA, %s, %c, %c, %c, %c;\n", reg_dest, x, y, z, w);
1881 shader_addline(buffer, "KIL %s;\n", kilsrc);
1882 } else {
1883 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1884 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1886 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1887 * or pass in any temporary register(in shader phase 2)
1889 if(ins->ctx->reg_maps->shader_version.minor <= 3) {
1890 sprintf(reg_dest, "fragment.texcoord[%u]", dst->reg.idx);
1891 } else {
1892 shader_arb_get_dst_param(ins, dst, reg_dest);
1894 shader_addline(buffer, "SWZ TA, %s, x, y, z, 1;\n", reg_dest);
1895 shader_addline(buffer, "KIL TA;\n");
1899 static void pshader_hw_tex(const struct wined3d_shader_instruction *ins)
1901 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1902 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1903 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1904 ins->ctx->reg_maps->shader_version.minor);
1905 struct wined3d_shader_src_param src;
1907 char reg_dest[40];
1908 char reg_coord[40];
1909 DWORD reg_sampler_code;
1910 WORD myflags = 0;
1912 /* All versions have a destination register */
1913 shader_arb_get_dst_param(ins, dst, reg_dest);
1915 /* 1.0-1.4: Use destination register number as texture code.
1916 2.0+: Use provided sampler number as texure code. */
1917 if (shader_version < WINED3D_SHADER_VERSION(2,0))
1918 reg_sampler_code = dst->reg.idx;
1919 else
1920 reg_sampler_code = ins->src[1].reg.idx;
1922 /* 1.0-1.3: Use the texcoord varying.
1923 1.4+: Use provided coordinate source register. */
1924 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1925 sprintf(reg_coord, "fragment.texcoord[%u]", reg_sampler_code);
1926 else {
1927 /* TEX is the only instruction that can handle DW and DZ natively */
1928 src = ins->src[0];
1929 if(src.modifiers == WINED3DSPSM_DW) src.modifiers = WINED3DSPSM_NONE;
1930 if(src.modifiers == WINED3DSPSM_DZ) src.modifiers = WINED3DSPSM_NONE;
1931 shader_arb_get_src_param(ins, &src, 0, reg_coord);
1934 /* projection flag:
1935 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1936 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1937 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1939 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1941 DWORD flags = 0;
1942 if (reg_sampler_code < MAX_TEXTURES)
1943 flags = priv->cur_ps_args->super.tex_transform >> reg_sampler_code * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
1944 if (flags & WINED3D_PSARGS_PROJECTED)
1945 myflags |= TEX_PROJ;
1947 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
1949 DWORD src_mod = ins->src[0].modifiers;
1950 if (src_mod == WINED3DSPSM_DZ) {
1951 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1952 * varying register, so we need a temp reg
1954 shader_addline(ins->ctx->buffer, "SWZ TA, %s, x, y, z, z;\n", reg_coord);
1955 strcpy(reg_coord, "TA");
1956 myflags |= TEX_PROJ;
1957 } else if(src_mod == WINED3DSPSM_DW) {
1958 myflags |= TEX_PROJ;
1960 } else {
1961 if (ins->flags & WINED3DSI_TEXLD_PROJECT) myflags |= TEX_PROJ;
1962 if (ins->flags & WINED3DSI_TEXLD_BIAS) myflags |= TEX_BIAS;
1964 shader_hw_sample(ins, reg_sampler_code, reg_dest, reg_coord, myflags, NULL, NULL);
1967 static void pshader_hw_texcoord(const struct wined3d_shader_instruction *ins)
1969 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1970 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1971 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1972 ins->ctx->reg_maps->shader_version.minor);
1973 char dst_str[50];
1975 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1977 DWORD reg = dst->reg.idx;
1979 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1980 shader_addline(buffer, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str, reg);
1981 } else {
1982 char reg_src[40];
1984 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src);
1985 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1986 shader_addline(buffer, "MOV %s, %s;\n", dst_str, reg_src);
1990 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction *ins)
1992 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1993 DWORD flags = 0;
1995 DWORD reg1 = ins->dst[0].reg.idx;
1996 char dst_str[50];
1997 char src_str[50];
1999 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
2000 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2001 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
2002 /* Move .x first in case src_str is "TA" */
2003 shader_addline(buffer, "MOV TA.y, %s.x;\n", src_str);
2004 shader_addline(buffer, "MOV TA.x, %s.w;\n", src_str);
2005 if (reg1 < MAX_TEXTURES)
2007 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2008 flags = priv->cur_ps_args->super.tex_transform >> reg1 * WINED3D_PSARGS_TEXTRANSFORM_SHIFT;
2010 shader_hw_sample(ins, reg1, dst_str, "TA", flags & WINED3D_PSARGS_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2013 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction *ins)
2015 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2017 DWORD reg1 = ins->dst[0].reg.idx;
2018 char dst_str[50];
2019 char src_str[50];
2021 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
2022 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2023 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
2024 shader_addline(buffer, "MOV TA.x, %s.y;\n", src_str);
2025 shader_addline(buffer, "MOV TA.y, %s.z;\n", src_str);
2026 shader_hw_sample(ins, reg1, dst_str, "TA", 0, NULL, NULL);
2029 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction *ins)
2031 DWORD reg1 = ins->dst[0].reg.idx;
2032 char dst_str[50];
2033 char src_str[50];
2035 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
2036 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2037 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
2038 shader_hw_sample(ins, reg1, dst_str, src_str, 0, NULL, NULL);
2041 static void pshader_hw_texbem(const struct wined3d_shader_instruction *ins)
2043 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2044 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2045 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2046 char reg_coord[40], dst_reg[50], src_reg[50];
2047 DWORD reg_dest_code;
2049 /* All versions have a destination register. The Tx where the texture coordinates come
2050 * from is the varying incarnation of the texture register
2052 reg_dest_code = dst->reg.idx;
2053 shader_arb_get_dst_param(ins, &ins->dst[0], dst_reg);
2054 shader_arb_get_src_param(ins, &ins->src[0], 0, src_reg);
2055 sprintf(reg_coord, "fragment.texcoord[%u]", reg_dest_code);
2057 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
2058 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
2060 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
2061 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
2063 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
2064 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
2065 * extension.
2067 shader_addline(buffer, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
2068 shader_addline(buffer, "DP3 TA.x, TB, %s;\n", src_reg);
2069 shader_addline(buffer, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
2070 shader_addline(buffer, "DP3 TA.y, TB, %s;\n", src_reg);
2072 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2073 * so we can't let the GL handle this.
2075 if ((priv->cur_ps_args->super.tex_transform >> reg_dest_code * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
2076 & WINED3D_PSARGS_PROJECTED)
2078 shader_addline(buffer, "RCP TB.w, %s.w;\n", reg_coord);
2079 shader_addline(buffer, "MUL TB.xy, %s, TB.w;\n", reg_coord);
2080 shader_addline(buffer, "ADD TA.xy, TA, TB;\n");
2081 } else {
2082 shader_addline(buffer, "ADD TA.xy, TA, %s;\n", reg_coord);
2085 shader_hw_sample(ins, reg_dest_code, dst_reg, "TA", 0, NULL, NULL);
2087 if (ins->handler_idx == WINED3DSIH_TEXBEML)
2089 /* No src swizzles are allowed, so this is ok */
2090 shader_addline(buffer, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
2091 src_reg, reg_dest_code, reg_dest_code);
2092 shader_addline(buffer, "MUL %s, %s, TA;\n", dst_reg, dst_reg);
2096 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction *ins)
2098 DWORD reg = ins->dst[0].reg.idx;
2099 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2100 char src0_name[50], dst_name[50];
2101 BOOL is_color;
2102 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
2104 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2105 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
2106 * T<reg+1> register. Use this register to store the calculated vector
2108 tmp_reg.idx = reg + 1;
2109 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
2110 shader_addline(buffer, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
2113 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction *ins)
2115 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2116 DWORD flags;
2117 DWORD reg = ins->dst[0].reg.idx;
2118 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2119 char dst_str[50];
2120 char src0_name[50];
2121 char dst_reg[50];
2122 BOOL is_color;
2124 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
2125 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2127 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2128 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2129 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2130 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2131 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2134 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction *ins)
2136 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2137 DWORD reg = ins->dst[0].reg.idx;
2138 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2139 char src0_name[50], dst_name[50];
2140 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
2141 BOOL is_color;
2143 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
2144 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
2145 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
2147 tmp_reg.idx = reg + 2 - tex_mx->current_row;
2148 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
2150 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2151 shader_addline(buffer, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
2152 dst_name, 'x' + tex_mx->current_row, reg, src0_name);
2153 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
2156 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction *ins)
2158 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2159 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2160 DWORD flags;
2161 DWORD reg = ins->dst[0].reg.idx;
2162 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2163 char dst_str[50];
2164 char src0_name[50], dst_name[50];
2165 BOOL is_color;
2167 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2168 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2169 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
2171 /* Sample the texture using the calculated coordinates */
2172 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2173 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2174 shader_hw_sample(ins, reg, dst_str, dst_name, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2175 tex_mx->current_row = 0;
2178 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2180 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2181 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2182 DWORD flags;
2183 DWORD reg = ins->dst[0].reg.idx;
2184 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2185 char dst_str[50];
2186 char src0_name[50];
2187 char dst_reg[50];
2188 BOOL is_color;
2190 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
2191 * components for temporary data storage
2193 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2194 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2195 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2197 /* Construct the eye-ray vector from w coordinates */
2198 shader_addline(buffer, "MOV TB.x, fragment.texcoord[%u].w;\n", tex_mx->texcoord_w[0]);
2199 shader_addline(buffer, "MOV TB.y, fragment.texcoord[%u].w;\n", tex_mx->texcoord_w[1]);
2200 shader_addline(buffer, "MOV TB.z, fragment.texcoord[%u].w;\n", reg);
2202 /* Calculate reflection vector
2204 shader_addline(buffer, "DP3 %s.w, %s, TB;\n", dst_reg, dst_reg);
2205 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
2206 shader_addline(buffer, "DP3 TB.w, %s, %s;\n", dst_reg, dst_reg);
2207 shader_addline(buffer, "RCP TB.w, TB.w;\n");
2208 shader_addline(buffer, "MUL %s.w, %s.w, TB.w;\n", dst_reg, dst_reg);
2209 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2210 shader_addline(buffer, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg, dst_reg);
2212 /* Sample the texture using the calculated coordinates */
2213 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2214 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2215 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2216 tex_mx->current_row = 0;
2219 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction *ins)
2221 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2222 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
2223 DWORD flags;
2224 DWORD reg = ins->dst[0].reg.idx;
2225 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2226 char dst_str[50];
2227 char src0_name[50];
2228 char src1_name[50];
2229 char dst_reg[50];
2230 BOOL is_color;
2232 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2233 shader_arb_get_src_param(ins, &ins->src[0], 1, src1_name);
2234 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2235 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
2236 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2238 /* Calculate reflection vector.
2240 * dot(N, E)
2241 * dst_reg.xyz = 2 * --------- * N - E
2242 * dot(N, N)
2244 * Which normalizes the normal vector
2246 shader_addline(buffer, "DP3 %s.w, %s, %s;\n", dst_reg, dst_reg, src1_name);
2247 shader_addline(buffer, "DP3 TC.w, %s, %s;\n", dst_reg, dst_reg);
2248 shader_addline(buffer, "RCP TC.w, TC.w;\n");
2249 shader_addline(buffer, "MUL %s.w, %s.w, TC.w;\n", dst_reg, dst_reg);
2250 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2251 shader_addline(buffer, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg, dst_reg, src1_name);
2253 /* Sample the texture using the calculated coordinates */
2254 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2255 flags = reg < MAX_TEXTURES ? priv->cur_ps_args->super.tex_transform >> reg * WINED3D_PSARGS_TEXTRANSFORM_SHIFT : 0;
2256 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2257 tex_mx->current_row = 0;
2260 static void pshader_hw_texdepth(const struct wined3d_shader_instruction *ins)
2262 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2263 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2264 char dst_name[50];
2265 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2266 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2268 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
2269 * which is essentially an input, is the destination register because it is the first
2270 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
2271 * here(writemasks/swizzles are not valid on texdepth)
2273 shader_arb_get_dst_param(ins, dst, dst_name);
2275 /* According to the msdn, the source register(must be r5) is unusable after
2276 * the texdepth instruction, so we're free to modify it
2278 shader_addline(buffer, "MIN %s.y, %s.y, %s;\n", dst_name, dst_name, one);
2280 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2281 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2282 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2284 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2285 shader_addline(buffer, "MUL TA.x, %s.x, %s.y;\n", dst_name, dst_name);
2286 shader_addline(buffer, "MIN TA.x, TA.x, %s;\n", one);
2287 shader_addline(buffer, "MAX result.depth, TA.x, %s;\n", zero);
2290 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
2291 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2292 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2293 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction *ins)
2295 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2296 DWORD sampler_idx = ins->dst[0].reg.idx;
2297 char src0[50];
2298 char dst_str[50];
2300 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2301 shader_addline(buffer, "MOV TB, 0.0;\n");
2302 shader_addline(buffer, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx, src0);
2304 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2305 shader_hw_sample(ins, sampler_idx, dst_str, "TB", 0 /* Only one coord, can't be projected */, NULL, NULL);
2308 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2309 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2310 static void pshader_hw_texdp3(const struct wined3d_shader_instruction *ins)
2312 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2313 char src0[50];
2314 char dst_str[50];
2315 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2317 /* Handle output register */
2318 shader_arb_get_dst_param(ins, dst, dst_str);
2319 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2320 shader_addline(buffer, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str, dst->reg.idx, src0);
2323 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2324 * Perform the 3rd row of a 3x3 matrix multiply */
2325 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction *ins)
2327 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2328 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2329 char dst_str[50], dst_name[50];
2330 char src0[50];
2331 BOOL is_color;
2333 shader_arb_get_dst_param(ins, dst, dst_str);
2334 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2335 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2336 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx, src0);
2337 shader_addline(buffer, "MOV %s, %s;\n", dst_str, dst_name);
2340 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2341 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2342 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2343 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2345 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction *ins)
2347 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2348 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2349 char src0[50], dst_name[50];
2350 BOOL is_color;
2351 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2352 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2354 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2355 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2356 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx, src0);
2358 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2359 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2360 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2362 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2363 shader_addline(buffer, "MUL %s.x, %s.x, %s.y;\n", dst_name, dst_name, dst_name);
2364 shader_addline(buffer, "MIN %s.x, %s.x, %s;\n", dst_name, dst_name, one);
2365 shader_addline(buffer, "MAX result.depth, %s.x, %s;\n", dst_name, zero);
2368 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2369 Vertex/Pixel shaders to ARB_vertex_program codes */
2370 static void shader_hw_mnxn(const struct wined3d_shader_instruction *ins)
2372 int i;
2373 int nComponents = 0;
2374 struct wined3d_shader_dst_param tmp_dst = {{0}};
2375 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2376 struct wined3d_shader_instruction tmp_ins;
2378 memset(&tmp_ins, 0, sizeof(tmp_ins));
2380 /* Set constants for the temporary argument */
2381 tmp_ins.ctx = ins->ctx;
2382 tmp_ins.dst_count = 1;
2383 tmp_ins.dst = &tmp_dst;
2384 tmp_ins.src_count = 2;
2385 tmp_ins.src = tmp_src;
2387 switch(ins->handler_idx)
2389 case WINED3DSIH_M4x4:
2390 nComponents = 4;
2391 tmp_ins.handler_idx = WINED3DSIH_DP4;
2392 break;
2393 case WINED3DSIH_M4x3:
2394 nComponents = 3;
2395 tmp_ins.handler_idx = WINED3DSIH_DP4;
2396 break;
2397 case WINED3DSIH_M3x4:
2398 nComponents = 4;
2399 tmp_ins.handler_idx = WINED3DSIH_DP3;
2400 break;
2401 case WINED3DSIH_M3x3:
2402 nComponents = 3;
2403 tmp_ins.handler_idx = WINED3DSIH_DP3;
2404 break;
2405 case WINED3DSIH_M3x2:
2406 nComponents = 2;
2407 tmp_ins.handler_idx = WINED3DSIH_DP3;
2408 break;
2409 default:
2410 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
2411 break;
2414 tmp_dst = ins->dst[0];
2415 tmp_src[0] = ins->src[0];
2416 tmp_src[1] = ins->src[1];
2417 for (i = 0; i < nComponents; i++) {
2418 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2419 shader_hw_map2gl(&tmp_ins);
2420 ++tmp_src[1].reg.idx;
2424 static void shader_hw_rcp(const struct wined3d_shader_instruction *ins)
2426 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2428 char dst[50];
2429 char src[50];
2431 shader_arb_get_dst_param(ins, &ins->dst[0], dst); /* Destination */
2432 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2433 if (ins->src[0].swizzle == WINED3DSP_NOSWIZZLE)
2435 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2436 * .w is used
2438 strcat(src, ".w");
2441 shader_addline(buffer, "RCP%s %s, %s;\n", shader_arb_get_modifier(ins), dst, src);
2444 static void shader_hw_scalar_op(const struct wined3d_shader_instruction *ins)
2446 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2447 const char *instruction;
2449 char dst[50];
2450 char src[50];
2452 switch(ins->handler_idx)
2454 case WINED3DSIH_RSQ: instruction = "RSQ"; break;
2455 case WINED3DSIH_RCP: instruction = "RCP"; break;
2456 case WINED3DSIH_EXP: instruction = "EX2"; break;
2457 case WINED3DSIH_EXPP: instruction = "EXP"; break;
2458 default: instruction = "";
2459 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
2460 break;
2463 shader_arb_get_dst_param(ins, &ins->dst[0], dst); /* Destination */
2464 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2465 if (ins->src[0].swizzle == WINED3DSP_NOSWIZZLE)
2467 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2468 * .w is used
2470 strcat(src, ".w");
2473 shader_addline(buffer, "%s%s %s, %s;\n", instruction, shader_arb_get_modifier(ins), dst, src);
2476 static void shader_hw_nrm(const struct wined3d_shader_instruction *ins)
2478 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2479 char dst_name[50];
2480 char src_name[50];
2481 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2482 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
2483 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2485 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2486 shader_arb_get_src_param(ins, &ins->src[0], 1 /* Use TB */, src_name);
2488 /* In D3D, NRM of a vector with length zero returns zero. Catch this situation, as
2489 * otherwise NRM or RSQ would return NaN */
2490 if(pshader && priv->target_version >= NV3)
2492 /* GL_NV_fragment_program2's NRM needs protection against length zero vectors too
2494 * TODO: Find out if DP3+NRM+MOV is really faster than DP3+RSQ+MUL
2496 shader_addline(buffer, "DP3C TA, %s, %s;\n", src_name, src_name);
2497 shader_addline(buffer, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name);
2498 shader_addline(buffer, "MOV %s (EQ), %s;\n", dst_name, zero);
2500 else if(priv->target_version >= NV2)
2502 shader_addline(buffer, "DP3C TA.x, %s, %s;\n", src_name, src_name);
2503 shader_addline(buffer, "RSQ TA.x (NE), TA.x;\n");
2504 shader_addline(buffer, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins), dst_name,
2505 src_name);
2507 else
2509 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2511 shader_addline(buffer, "DP3 TA.x, %s, %s;\n", src_name, src_name);
2512 /* Pass any non-zero value to RSQ if the input vector has a length of zero. The
2513 * RSQ result doesn't matter, as long as multiplying it by 0 returns 0.
2515 shader_addline(buffer, "SGE TA.y, -TA.x, %s;\n", zero);
2516 shader_addline(buffer, "MAD TA.x, %s, TA.y, TA.x;\n", one);
2518 shader_addline(buffer, "RSQ TA.x, TA.x;\n");
2519 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2520 shader_addline(buffer, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins), dst_name,
2521 src_name);
2525 static void shader_hw_lrp(const struct wined3d_shader_instruction *ins)
2527 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2528 char dst_name[50];
2529 char src_name[3][50];
2531 /* ARB_fragment_program has a convenient LRP instruction */
2532 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2533 shader_hw_map2gl(ins);
2534 return;
2537 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2538 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
2539 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
2540 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
2542 shader_addline(buffer, "SUB TA, %s, %s;\n", src_name[1], src_name[2]);
2543 shader_addline(buffer, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins),
2544 dst_name, src_name[0], src_name[2]);
2547 static void shader_hw_sincos(const struct wined3d_shader_instruction *ins)
2549 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2550 * must contain fixed constants. So we need a separate function to filter those constants and
2551 * can't use map2gl
2553 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2554 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2555 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2556 char dst_name[50];
2557 char src_name0[50], src_name1[50], src_name2[50];
2558 BOOL is_color;
2560 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2561 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2562 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2563 /* No modifiers are supported on SCS */
2564 shader_addline(buffer, "SCS %s, %s;\n", dst_name, src_name0);
2566 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
2568 shader_arb_get_register_name(ins, &dst->reg, src_name0, &is_color);
2569 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name0);
2571 } else if(priv->target_version >= NV2) {
2572 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2574 /* Sincos writemask must be .x, .y or .xy */
2575 if(dst->write_mask & WINED3DSP_WRITEMASK_0)
2576 shader_addline(buffer, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2577 if(dst->write_mask & WINED3DSP_WRITEMASK_1)
2578 shader_addline(buffer, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2579 } else {
2580 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2581 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2583 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2584 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2586 * The constants we get are:
2588 * +1 +1, -1 -1 +1 +1 -1 -1
2589 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2590 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2592 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2594 * (x/2)^2 = x^2 / 4
2595 * (x/2)^3 = x^3 / 8
2596 * (x/2)^4 = x^4 / 16
2597 * (x/2)^5 = x^5 / 32
2598 * etc
2600 * To get the final result:
2601 * sin(x) = 2 * sin(x/2) * cos(x/2)
2602 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2603 * (from sin(x+y) and cos(x+y) rules)
2605 * As per MSDN, dst.z is undefined after the operation, and so is
2606 * dst.x and dst.y if they're masked out by the writemask. Ie
2607 * sincos dst.y, src1, c0, c1
2608 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2609 * vsa.exe also stops with an error if the dest register is the same register as the source
2610 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2611 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2613 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2614 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name2);
2615 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2617 shader_addline(buffer, "MUL %s.x, %s, %s;\n", dst_name, src_name0, src_name0); /* x ^ 2 */
2618 shader_addline(buffer, "MUL TA.y, %s.x, %s;\n", dst_name, src_name0); /* x ^ 3 */
2619 shader_addline(buffer, "MUL %s.y, TA.y, %s;\n", dst_name, src_name0); /* x ^ 4 */
2620 shader_addline(buffer, "MUL TA.z, %s.y, %s;\n", dst_name, src_name0); /* x ^ 5 */
2621 shader_addline(buffer, "MUL %s.z, TA.z, %s;\n", dst_name, src_name0); /* x ^ 6 */
2622 shader_addline(buffer, "MUL TA.w, %s.z, %s;\n", dst_name, src_name0); /* x ^ 7 */
2624 /* sin(x/2)
2626 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2627 * properly merge that with MULs in the code above?
2628 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2629 * we can merge the sine and cosine MAD rows to calculate them together.
2631 shader_addline(buffer, "MUL TA.x, %s, %s.w;\n", src_name0, src_name2); /* x^1, +1/(1!*2) */
2632 shader_addline(buffer, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2); /* -1/(3!*8) */
2633 shader_addline(buffer, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1); /* +1/(5!*32) */
2634 shader_addline(buffer, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1); /* -1/(7!*128) */
2636 /* cos(x/2) */
2637 shader_addline(buffer, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name, src_name2, src_name2); /* -1/(2!*4), +1.0 */
2638 shader_addline(buffer, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name, src_name1); /* +1/(4!*16) */
2639 shader_addline(buffer, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name, src_name1); /* -1/(6!*64) */
2641 if(dst->write_mask & WINED3DSP_WRITEMASK_0) {
2642 /* cos x */
2643 shader_addline(buffer, "MUL TA.z, TA.y, TA.y;\n");
2644 shader_addline(buffer, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name);
2646 if(dst->write_mask & WINED3DSP_WRITEMASK_1) {
2647 /* sin x */
2648 shader_addline(buffer, "MUL %s.y, TA.x, TA.y;\n", dst_name);
2649 shader_addline(buffer, "ADD %s.y, %s.y, %s.y;\n", dst_name, dst_name, dst_name);
2654 static void shader_hw_sgn(const struct wined3d_shader_instruction *ins)
2656 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2657 char dst_name[50];
2658 char src_name[50];
2659 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
2661 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2662 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2664 /* SGN is only valid in vertex shaders */
2665 if(ctx->target_version >= NV2) {
2666 shader_addline(buffer, "SSG%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name);
2667 return;
2670 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2671 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2673 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) {
2674 shader_addline(buffer, "SLT %s, -%s, %s;\n", dst_name, src_name, src_name);
2675 } else {
2676 /* src contains TA? Write to the dest first. This won't overwrite our destination.
2677 * Then use TA, and calculate the final result
2679 * Not reading from TA? Store the first result in TA to avoid overwriting the
2680 * destination if src reg = dst reg
2682 if(strstr(src_name, "TA"))
2684 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2685 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2686 shader_addline(buffer, "ADD %s, %s, -TA;\n", dst_name, dst_name);
2688 else
2690 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2691 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2692 shader_addline(buffer, "ADD %s, TA, -%s;\n", dst_name, dst_name);
2697 static void shader_hw_dsy(const struct wined3d_shader_instruction *ins)
2699 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2700 char src[50];
2701 char dst[50];
2702 char dst_name[50];
2703 BOOL is_color;
2705 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2706 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2707 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2709 shader_addline(buffer, "DDY %s, %s;\n", dst, src);
2710 shader_addline(buffer, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins), dst, dst_name);
2713 static DWORD abs_modifier(DWORD mod, BOOL *need_abs)
2715 *need_abs = FALSE;
2717 switch(mod)
2719 case WINED3DSPSM_NONE: return WINED3DSPSM_ABS;
2720 case WINED3DSPSM_NEG: return WINED3DSPSM_ABS;
2721 case WINED3DSPSM_BIAS: *need_abs = TRUE; return WINED3DSPSM_BIAS;
2722 case WINED3DSPSM_BIASNEG: *need_abs = TRUE; return WINED3DSPSM_BIASNEG;
2723 case WINED3DSPSM_SIGN: *need_abs = TRUE; return WINED3DSPSM_SIGN;
2724 case WINED3DSPSM_SIGNNEG: *need_abs = TRUE; return WINED3DSPSM_SIGNNEG;
2725 case WINED3DSPSM_COMP: *need_abs = TRUE; return WINED3DSPSM_COMP;
2726 case WINED3DSPSM_X2: *need_abs = TRUE; return WINED3DSPSM_X2;
2727 case WINED3DSPSM_X2NEG: *need_abs = TRUE; return WINED3DSPSM_X2NEG;
2728 case WINED3DSPSM_DZ: *need_abs = TRUE; return WINED3DSPSM_DZ;
2729 case WINED3DSPSM_DW: *need_abs = TRUE; return WINED3DSPSM_DW;
2730 case WINED3DSPSM_ABS: return WINED3DSPSM_ABS;
2731 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
2733 FIXME("Unknown modifier %u\n", mod);
2734 return mod;
2737 static void shader_hw_log(const struct wined3d_shader_instruction *ins)
2739 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2740 char src0[50], dst[50];
2741 struct wined3d_shader_src_param src0_copy = ins->src[0];
2742 BOOL need_abs = FALSE;
2743 const char *instr;
2745 switch(ins->handler_idx)
2747 case WINED3DSIH_LOG: instr = "LG2"; break;
2748 case WINED3DSIH_LOGP: instr = "LOG"; break;
2749 default:
2750 ERR("Unexpected instruction %d\n", ins->handler_idx);
2751 return;
2754 /* LOG and LOGP operate on the absolute value of the input */
2755 src0_copy.modifiers = abs_modifier(src0_copy.modifiers, &need_abs);
2757 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2758 shader_arb_get_src_param(ins, &src0_copy, 0, src0);
2760 if(need_abs)
2762 shader_addline(buffer, "ABS TA, %s;\n", src0);
2763 shader_addline(buffer, "%s%s %s, TA;\n", instr, shader_arb_get_modifier(ins), dst);
2765 else
2767 shader_addline(buffer, "%s%s %s, %s;\n", instr, shader_arb_get_modifier(ins), dst, src0);
2771 static void shader_hw_pow(const struct wined3d_shader_instruction *ins)
2773 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2774 char src0[50], src1[50], dst[50];
2775 struct wined3d_shader_src_param src0_copy = ins->src[0];
2776 BOOL need_abs = FALSE;
2777 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2778 const char *one = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ONE);
2780 /* POW operates on the absolute value of the input */
2781 src0_copy.modifiers = abs_modifier(src0_copy.modifiers, &need_abs);
2783 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2784 shader_arb_get_src_param(ins, &src0_copy, 0, src0);
2785 shader_arb_get_src_param(ins, &ins->src[1], 1, src1);
2787 if (need_abs)
2788 shader_addline(buffer, "ABS TA.x, %s;\n", src0);
2789 else
2790 shader_addline(buffer, "MOV TA.x, %s;\n", src0);
2792 if (priv->target_version >= NV2)
2794 shader_addline(buffer, "MOVC TA.y, %s;\n", src1);
2795 shader_addline(buffer, "POW%s %s, TA.x, TA.y;\n", shader_arb_get_modifier(ins), dst);
2796 shader_addline(buffer, "MOV %s (EQ.y), %s;\n", dst, one);
2798 else
2800 const char *zero = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_ZERO);
2801 const char *flt_eps = arb_get_helper_value(ins->ctx->reg_maps->shader_version.type, ARB_EPS);
2803 shader_addline(buffer, "ABS TA.y, %s;\n", src1);
2804 shader_addline(buffer, "SGE TA.y, -TA.y, %s;\n", zero);
2805 /* Possibly add flt_eps to avoid getting float special values */
2806 shader_addline(buffer, "MAD TA.z, TA.y, %s, %s;\n", flt_eps, src1);
2807 shader_addline(buffer, "POW%s TA.x, TA.x, TA.z;\n", shader_arb_get_modifier(ins));
2808 shader_addline(buffer, "MAD TA.x, -TA.x, TA.y, TA.x;\n");
2809 shader_addline(buffer, "MAD %s, TA.y, %s, TA.x;\n", dst, one);
2813 static void shader_hw_loop(const struct wined3d_shader_instruction *ins)
2815 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2816 char src_name[50];
2817 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2819 /* src0 is aL */
2820 shader_arb_get_src_param(ins, &ins->src[1], 0, src_name);
2822 if(vshader)
2824 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2825 struct list *e = list_head(&priv->control_frames);
2826 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2828 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2829 /* The constant loader makes sure to load -1 into iX.w */
2830 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2831 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->no.loop);
2832 shader_addline(buffer, "loop_%u_start:\n", control_frame->no.loop);
2834 else
2836 shader_addline(buffer, "LOOP %s;\n", src_name);
2840 static void shader_hw_rep(const struct wined3d_shader_instruction *ins)
2842 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2843 char src_name[50];
2844 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2846 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2848 /* The constant loader makes sure to load -1 into iX.w */
2849 if(vshader)
2851 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2852 struct list *e = list_head(&priv->control_frames);
2853 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2855 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2857 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2858 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->no.loop);
2859 shader_addline(buffer, "loop_%u_start:\n", control_frame->no.loop);
2861 else
2863 shader_addline(buffer, "REP %s;\n", src_name);
2867 static void shader_hw_endloop(const struct wined3d_shader_instruction *ins)
2869 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2870 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2872 if(vshader)
2874 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2875 struct list *e = list_head(&priv->control_frames);
2876 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2878 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2879 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->no.loop);
2880 shader_addline(buffer, "loop_%u_end:\n", control_frame->no.loop);
2882 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2884 else
2886 shader_addline(buffer, "ENDLOOP;\n");
2890 static void shader_hw_endrep(const struct wined3d_shader_instruction *ins)
2892 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2893 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2895 if(vshader)
2897 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2898 struct list *e = list_head(&priv->control_frames);
2899 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2901 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2902 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->no.loop);
2903 shader_addline(buffer, "loop_%u_end:\n", control_frame->no.loop);
2905 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2907 else
2909 shader_addline(buffer, "ENDREP;\n");
2913 static const struct control_frame *find_last_loop(const struct shader_arb_ctx_priv *priv)
2915 struct control_frame *control_frame;
2917 LIST_FOR_EACH_ENTRY(control_frame, &priv->control_frames, struct control_frame, entry)
2919 if(control_frame->type == LOOP || control_frame->type == REP) return control_frame;
2921 ERR("Could not find loop for break\n");
2922 return NULL;
2925 static void shader_hw_break(const struct wined3d_shader_instruction *ins)
2927 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2928 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2929 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2931 if(vshader)
2933 shader_addline(buffer, "BRA loop_%u_end;\n", control_frame->no.loop);
2935 else
2937 shader_addline(buffer, "BRK;\n");
2941 static const char *get_compare(COMPARISON_TYPE flags)
2943 switch (flags)
2945 case COMPARISON_GT: return "GT";
2946 case COMPARISON_EQ: return "EQ";
2947 case COMPARISON_GE: return "GE";
2948 case COMPARISON_LT: return "LT";
2949 case COMPARISON_NE: return "NE";
2950 case COMPARISON_LE: return "LE";
2951 default:
2952 FIXME("Unrecognized comparison value: %u\n", flags);
2953 return "(\?\?)";
2957 static COMPARISON_TYPE invert_compare(COMPARISON_TYPE flags)
2959 switch (flags)
2961 case COMPARISON_GT: return COMPARISON_LE;
2962 case COMPARISON_EQ: return COMPARISON_NE;
2963 case COMPARISON_GE: return COMPARISON_LT;
2964 case COMPARISON_LT: return COMPARISON_GE;
2965 case COMPARISON_NE: return COMPARISON_EQ;
2966 case COMPARISON_LE: return COMPARISON_GT;
2967 default:
2968 FIXME("Unrecognized comparison value: %u\n", flags);
2969 return -1;
2973 static void shader_hw_breakc(const struct wined3d_shader_instruction *ins)
2975 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2976 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2977 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2978 char src_name0[50];
2979 char src_name1[50];
2980 const char *comp = get_compare(ins->flags);
2982 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2983 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2985 if(vshader)
2987 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
2988 * away the subtraction result
2990 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2991 shader_addline(buffer, "BRA loop_%u_end (%s.x);\n", control_frame->no.loop, comp);
2993 else
2995 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2996 shader_addline(buffer, "BRK (%s.x);\n", comp);
3000 static void shader_hw_ifc(const struct wined3d_shader_instruction *ins)
3002 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3003 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3004 struct list *e = list_head(&priv->control_frames);
3005 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
3006 const char *comp;
3007 char src_name0[50];
3008 char src_name1[50];
3009 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3011 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
3012 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
3014 if(vshader)
3016 /* Invert the flag. We jump to the else label if the condition is NOT true */
3017 comp = get_compare(invert_compare(ins->flags));
3018 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
3019 shader_addline(buffer, "BRA ifc_%u_else (%s.x);\n", control_frame->no.ifc, comp);
3021 else
3023 comp = get_compare(ins->flags);
3024 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
3025 shader_addline(buffer, "IF %s.x;\n", comp);
3029 static void shader_hw_else(const struct wined3d_shader_instruction *ins)
3031 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3032 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3033 struct list *e = list_head(&priv->control_frames);
3034 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
3035 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3037 if(vshader)
3039 shader_addline(buffer, "BRA ifc_%u_endif;\n", control_frame->no.ifc);
3040 shader_addline(buffer, "ifc_%u_else:\n", control_frame->no.ifc);
3041 control_frame->had_else = TRUE;
3043 else
3045 shader_addline(buffer, "ELSE;\n");
3049 static void shader_hw_endif(const struct wined3d_shader_instruction *ins)
3051 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3052 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3053 struct list *e = list_head(&priv->control_frames);
3054 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
3055 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3057 if(vshader)
3059 if(control_frame->had_else)
3061 shader_addline(buffer, "ifc_%u_endif:\n", control_frame->no.ifc);
3063 else
3065 shader_addline(buffer, "#No else branch. else is endif\n");
3066 shader_addline(buffer, "ifc_%u_else:\n", control_frame->no.ifc);
3069 else
3071 shader_addline(buffer, "ENDIF;\n");
3075 static void shader_hw_texldd(const struct wined3d_shader_instruction *ins)
3077 DWORD sampler_idx = ins->src[1].reg.idx;
3078 char reg_dest[40];
3079 char reg_src[3][40];
3080 WORD flags = TEX_DERIV;
3082 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
3083 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src[0]);
3084 shader_arb_get_src_param(ins, &ins->src[2], 1, reg_src[1]);
3085 shader_arb_get_src_param(ins, &ins->src[3], 2, reg_src[2]);
3087 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
3088 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
3090 shader_hw_sample(ins, sampler_idx, reg_dest, reg_src[0], flags, reg_src[1], reg_src[2]);
3093 static void shader_hw_texldl(const struct wined3d_shader_instruction *ins)
3095 DWORD sampler_idx = ins->src[1].reg.idx;
3096 char reg_dest[40];
3097 char reg_coord[40];
3098 WORD flags = TEX_LOD;
3100 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
3101 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_coord);
3103 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
3104 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
3106 shader_hw_sample(ins, sampler_idx, reg_dest, reg_coord, flags, NULL, NULL);
3109 static void shader_hw_label(const struct wined3d_shader_instruction *ins)
3111 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3112 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3114 priv->in_main_func = FALSE;
3115 /* Call instructions activate the NV extensions, not labels and rets. If there is an uncalled
3116 * subroutine, don't generate a label that will make GL complain
3118 if(priv->target_version == ARB) return;
3120 shader_addline(buffer, "l%u:\n", ins->src[0].reg.idx);
3123 static void vshader_add_footer(struct shader_arb_ctx_priv *priv_ctx,
3124 const struct arb_vshader_private *shader_data, const struct arb_vs_compile_args *args,
3125 const struct wined3d_shader_reg_maps *reg_maps, const struct wined3d_gl_info *gl_info,
3126 struct wined3d_shader_buffer *buffer)
3128 unsigned int i;
3130 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
3131 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
3132 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
3133 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
3135 if (args->super.fog_src == VS_FOG_Z)
3136 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
3137 else if (!reg_maps->fog)
3138 /* posFixup.x is always 1.0, so we can safely use it */
3139 shader_addline(buffer, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
3141 /* Clipplanes are always stored without y inversion */
3142 if (use_nv_clip(gl_info) && priv_ctx->target_version >= NV2)
3144 if (args->super.clip_enabled)
3146 for (i = 0; i < priv_ctx->vs_clipplanes; i++)
3148 shader_addline(buffer, "DP4 result.clip[%u].x, TMP_OUT, state.clip[%u].plane;\n", i, i);
3152 else if (args->clip.boolclip.clip_texcoord)
3154 unsigned int cur_clip = 0;
3155 char component[4] = {'x', 'y', 'z', 'w'};
3156 const char *zero = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_ZERO);
3158 for (i = 0; i < gl_info->limits.clipplanes; ++i)
3160 if (args->clip.boolclip.clipplane_mask & (1 << i))
3162 shader_addline(buffer, "DP4 TA.%c, TMP_OUT, state.clip[%u].plane;\n",
3163 component[cur_clip++], i);
3166 switch (cur_clip)
3168 case 0:
3169 shader_addline(buffer, "MOV TA, %s;\n", zero);
3170 break;
3171 case 1:
3172 shader_addline(buffer, "MOV TA.yzw, %s;\n", zero);
3173 break;
3174 case 2:
3175 shader_addline(buffer, "MOV TA.zw, %s;\n", zero);
3176 break;
3177 case 3:
3178 shader_addline(buffer, "MOV TA.w, %s;\n", zero);
3179 break;
3181 shader_addline(buffer, "MOV result.texcoord[%u], TA;\n",
3182 args->clip.boolclip.clip_texcoord - 1);
3185 /* Write the final position.
3187 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3188 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3189 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3190 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
3192 shader_addline(buffer, "MUL TA, posFixup, TMP_OUT.w;\n");
3193 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
3194 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
3196 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3197 * and the glsl equivalent
3199 if (need_helper_const(shader_data, reg_maps, gl_info))
3201 const char *two = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_TWO);
3202 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, %s, -TMP_OUT.w;\n", two);
3204 else
3206 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
3207 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
3210 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
3212 priv_ctx->footer_written = TRUE;
3215 static void shader_hw_ret(const struct wined3d_shader_instruction *ins)
3217 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3218 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
3219 struct wined3d_shader *shader = ins->ctx->shader;
3220 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
3222 if(priv->target_version == ARB) return;
3224 if(vshader)
3226 if (priv->in_main_func) vshader_add_footer(priv, shader->backend_data,
3227 priv->cur_vs_args, ins->ctx->reg_maps, ins->ctx->gl_info, buffer);
3230 shader_addline(buffer, "RET;\n");
3233 static void shader_hw_call(const struct wined3d_shader_instruction *ins)
3235 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3236 shader_addline(buffer, "CAL l%u;\n", ins->src[0].reg.idx);
3239 /* GL locking is done by the caller */
3240 static GLuint create_arb_blt_vertex_program(const struct wined3d_gl_info *gl_info)
3242 GLuint program_id = 0;
3243 GLint pos;
3245 const char *blt_vprogram =
3246 "!!ARBvp1.0\n"
3247 "PARAM c[1] = { { 1, 0.5 } };\n"
3248 "MOV result.position, vertex.position;\n"
3249 "MOV result.color, c[0].x;\n"
3250 "MOV result.texcoord[0], vertex.texcoord[0];\n"
3251 "END\n";
3253 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3254 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
3255 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3256 strlen(blt_vprogram), blt_vprogram));
3257 checkGLcall("glProgramStringARB()");
3259 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3260 if (pos != -1)
3262 FIXME("Vertex program error at position %d: %s\n\n", pos,
3263 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3264 shader_arb_dump_program_source(blt_vprogram);
3266 else
3268 GLint native;
3270 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3271 checkGLcall("glGetProgramivARB()");
3272 if (!native) WARN("Program exceeds native resource limits.\n");
3275 return program_id;
3278 /* GL locking is done by the caller */
3279 static GLuint create_arb_blt_fragment_program(const struct wined3d_gl_info *gl_info,
3280 enum tex_types tex_type, BOOL masked)
3282 GLuint program_id = 0;
3283 const char *fprogram;
3284 GLint pos;
3286 static const char * const blt_fprograms_full[tex_type_count] =
3288 /* tex_1d */
3289 NULL,
3290 /* tex_2d */
3291 "!!ARBfp1.0\n"
3292 "TEMP R0;\n"
3293 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3294 "MOV result.depth.z, R0.x;\n"
3295 "END\n",
3296 /* tex_3d */
3297 NULL,
3298 /* tex_cube */
3299 "!!ARBfp1.0\n"
3300 "TEMP R0;\n"
3301 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3302 "MOV result.depth.z, R0.x;\n"
3303 "END\n",
3304 /* tex_rect */
3305 "!!ARBfp1.0\n"
3306 "TEMP R0;\n"
3307 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3308 "MOV result.depth.z, R0.x;\n"
3309 "END\n",
3312 static const char * const blt_fprograms_masked[tex_type_count] =
3314 /* tex_1d */
3315 NULL,
3316 /* tex_2d */
3317 "!!ARBfp1.0\n"
3318 "PARAM mask = program.local[0];\n"
3319 "TEMP R0;\n"
3320 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3321 "MUL R0.x, R0.x, R0.y;\n"
3322 "KIL -R0.x;\n"
3323 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3324 "MOV result.depth.z, R0.x;\n"
3325 "END\n",
3326 /* tex_3d */
3327 NULL,
3328 /* tex_cube */
3329 "!!ARBfp1.0\n"
3330 "PARAM mask = program.local[0];\n"
3331 "TEMP R0;\n"
3332 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3333 "MUL R0.x, R0.x, R0.y;\n"
3334 "KIL -R0.x;\n"
3335 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3336 "MOV result.depth.z, R0.x;\n"
3337 "END\n",
3338 /* tex_rect */
3339 "!!ARBfp1.0\n"
3340 "PARAM mask = program.local[0];\n"
3341 "TEMP R0;\n"
3342 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3343 "MUL R0.x, R0.x, R0.y;\n"
3344 "KIL -R0.x;\n"
3345 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3346 "MOV result.depth.z, R0.x;\n"
3347 "END\n",
3350 fprogram = masked ? blt_fprograms_masked[tex_type] : blt_fprograms_full[tex_type];
3351 if (!fprogram)
3353 FIXME("tex_type %#x not supported, falling back to tex_2d\n", tex_type);
3354 tex_type = tex_2d;
3355 fprogram = masked ? blt_fprograms_masked[tex_type] : blt_fprograms_full[tex_type];
3358 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3359 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
3360 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fprogram), fprogram));
3361 checkGLcall("glProgramStringARB()");
3363 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3364 if (pos != -1)
3366 FIXME("Fragment program error at position %d: %s\n\n", pos,
3367 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3368 shader_arb_dump_program_source(fprogram);
3370 else
3372 GLint native;
3374 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3375 checkGLcall("glGetProgramivARB()");
3376 if (!native) WARN("Program exceeds native resource limits.\n");
3379 return program_id;
3382 static void arbfp_add_sRGB_correction(struct wined3d_shader_buffer *buffer, const char *fragcolor,
3383 const char *tmp1, const char *tmp2, const char *tmp3, const char *tmp4, BOOL condcode)
3385 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
3387 if(condcode)
3389 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
3390 shader_addline(buffer, "SUBC %s, %s.x, srgb_consts1.y;\n", tmp1, fragcolor);
3391 /* Calculate the > 0.0031308 case */
3392 shader_addline(buffer, "POW %s.x (GE), %s.x, srgb_consts1.z;\n", fragcolor, fragcolor);
3393 shader_addline(buffer, "POW %s.y (GE), %s.y, srgb_consts1.z;\n", fragcolor, fragcolor);
3394 shader_addline(buffer, "POW %s.z (GE), %s.z, srgb_consts1.z;\n", fragcolor, fragcolor);
3395 shader_addline(buffer, "MUL %s.xyz (GE), %s, srgb_consts1.w;\n", fragcolor, fragcolor);
3396 shader_addline(buffer, "SUB %s.xyz (GE), %s, srgb_consts2.x;\n", fragcolor, fragcolor);
3397 /* Calculate the < case */
3398 shader_addline(buffer, "MUL %s.xyz (LT), srgb_consts1.x, %s;\n", fragcolor, fragcolor);
3400 else
3402 /* Calculate the > 0.0031308 case */
3403 shader_addline(buffer, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1, fragcolor);
3404 shader_addline(buffer, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1, fragcolor);
3405 shader_addline(buffer, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1, fragcolor);
3406 shader_addline(buffer, "MUL %s, %s, srgb_consts1.w;\n", tmp1, tmp1);
3407 shader_addline(buffer, "SUB %s, %s, srgb_consts2.x;\n", tmp1, tmp1);
3408 /* Calculate the < case */
3409 shader_addline(buffer, "MUL %s, srgb_consts1.x, %s;\n", tmp2, fragcolor);
3410 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
3411 shader_addline(buffer, "SLT %s, srgb_consts1.y, %s;\n", tmp3, fragcolor);
3412 shader_addline(buffer, "SGE %s, srgb_consts1.y, %s;\n", tmp4, fragcolor);
3413 /* Store the components > 0.0031308 in the destination */
3414 shader_addline(buffer, "MUL %s.xyz, %s, %s;\n", fragcolor, tmp1, tmp3);
3415 /* Add the components that are < 0.0031308 */
3416 shader_addline(buffer, "MAD %s.xyz, %s, %s, %s;\n", fragcolor, tmp2, tmp4, fragcolor);
3417 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
3418 * result.color writes(.rgb first, then .a), or handle overwriting already written
3419 * components. The assembler uses a temporary register in this case, which is usually
3420 * not allocated from one of our registers that were used earlier.
3423 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
3426 static const DWORD *find_loop_control_values(const struct wined3d_shader *shader, DWORD idx)
3428 const local_constant *constant;
3430 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, local_constant, entry)
3432 if (constant->idx == idx)
3434 return constant->value;
3437 return NULL;
3440 static void init_ps_input(const struct wined3d_shader *shader,
3441 const struct arb_ps_compile_args *args, struct shader_arb_ctx_priv *priv)
3443 static const char * const texcoords[8] =
3445 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
3446 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
3448 unsigned int i;
3449 const struct wined3d_shader_signature_element *sig = shader->input_signature;
3450 const char *semantic_name;
3451 DWORD semantic_idx;
3453 switch(args->super.vp_mode)
3455 case pretransformed:
3456 case fixedfunction:
3457 /* The pixelshader has to collect the varyings on its own. In any case properly load
3458 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
3459 * other attribs to 0.0.
3461 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
3462 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
3463 * load the texcoord attrib pointers to match the pixel shader signature
3465 for(i = 0; i < MAX_REG_INPUT; i++)
3467 semantic_name = sig[i].semantic_name;
3468 semantic_idx = sig[i].semantic_idx;
3469 if (!semantic_name) continue;
3471 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3473 if (!semantic_idx) priv->ps_input[i] = "fragment.color.primary";
3474 else if(semantic_idx == 1) priv->ps_input[i] = "fragment.color.secondary";
3475 else priv->ps_input[i] = "0.0";
3477 else if(args->super.vp_mode == fixedfunction)
3479 priv->ps_input[i] = "0.0";
3481 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3483 if(semantic_idx < 8) priv->ps_input[i] = texcoords[semantic_idx];
3484 else priv->ps_input[i] = "0.0";
3486 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3488 if (!semantic_idx) priv->ps_input[i] = "fragment.fogcoord";
3489 else priv->ps_input[i] = "0.0";
3491 else
3493 priv->ps_input[i] = "0.0";
3496 TRACE("v%u, semantic %s%u is %s\n", i, semantic_name, semantic_idx, priv->ps_input[i]);
3498 break;
3500 case vertexshader:
3501 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
3502 * fragment.color
3504 for(i = 0; i < 8; i++)
3506 priv->ps_input[i] = texcoords[i];
3508 priv->ps_input[8] = "fragment.color.primary";
3509 priv->ps_input[9] = "fragment.color.secondary";
3510 break;
3514 /* GL locking is done by the caller */
3515 static GLuint shader_arb_generate_pshader(struct wined3d_shader *shader,
3516 const struct wined3d_gl_info *gl_info, struct wined3d_shader_buffer *buffer,
3517 const struct arb_ps_compile_args *args, struct arb_ps_compiled_shader *compiled)
3519 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3520 const DWORD *function = shader->function;
3521 const local_constant *lconst;
3522 GLuint retval;
3523 char fragcolor[16];
3524 DWORD *lconst_map = local_const_mapping(shader), next_local;
3525 struct shader_arb_ctx_priv priv_ctx;
3526 BOOL dcl_td = FALSE;
3527 BOOL want_nv_prog = FALSE;
3528 struct arb_pshader_private *shader_priv = shader->backend_data;
3529 GLint errPos;
3530 DWORD map;
3532 char srgbtmp[4][4];
3533 unsigned int i, found = 0;
3535 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
3537 if (!(map & 1)
3538 || (shader->u.ps.color0_mov && i == shader->u.ps.color0_reg)
3539 || (reg_maps->shader_version.major < 2 && !i))
3540 continue;
3542 sprintf(srgbtmp[found], "R%u", i);
3543 ++found;
3544 if (found == 4) break;
3547 switch(found) {
3548 case 0:
3549 sprintf(srgbtmp[0], "TA");
3550 sprintf(srgbtmp[1], "TB");
3551 sprintf(srgbtmp[2], "TC");
3552 sprintf(srgbtmp[3], "TD");
3553 dcl_td = TRUE;
3554 break;
3555 case 1:
3556 sprintf(srgbtmp[1], "TA");
3557 sprintf(srgbtmp[2], "TB");
3558 sprintf(srgbtmp[3], "TC");
3559 break;
3560 case 2:
3561 sprintf(srgbtmp[2], "TA");
3562 sprintf(srgbtmp[3], "TB");
3563 break;
3564 case 3:
3565 sprintf(srgbtmp[3], "TA");
3566 break;
3567 case 4:
3568 break;
3571 /* Create the hw ARB shader */
3572 memset(&priv_ctx, 0, sizeof(priv_ctx));
3573 priv_ctx.cur_ps_args = args;
3574 priv_ctx.compiled_fprog = compiled;
3575 priv_ctx.cur_np2fixup_info = &compiled->np2fixup_info;
3576 init_ps_input(shader, args, &priv_ctx);
3577 list_init(&priv_ctx.control_frames);
3579 /* Avoid enabling NV_fragment_program* if we do not need it.
3581 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
3582 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
3583 * is faster than what we gain from using higher native instructions. There are some things though
3584 * that cannot be emulated. In that case enable the extensions.
3585 * If the extension is enabled, instruction handlers that support both ways will use it.
3587 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
3588 * So enable the best we can get.
3590 if(reg_maps->usesdsx || reg_maps->usesdsy || reg_maps->loop_depth > 0 || reg_maps->usestexldd ||
3591 reg_maps->usestexldl || reg_maps->usesfacing || reg_maps->usesifc || reg_maps->usescall)
3593 want_nv_prog = TRUE;
3596 shader_addline(buffer, "!!ARBfp1.0\n");
3597 if (want_nv_prog && gl_info->supported[NV_FRAGMENT_PROGRAM2])
3599 shader_addline(buffer, "OPTION NV_fragment_program2;\n");
3600 priv_ctx.target_version = NV3;
3602 else if (want_nv_prog && gl_info->supported[NV_FRAGMENT_PROGRAM_OPTION])
3604 shader_addline(buffer, "OPTION NV_fragment_program;\n");
3605 priv_ctx.target_version = NV2;
3606 } else {
3607 if(want_nv_prog)
3609 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
3610 * limits properly
3612 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
3613 ERR("Try GLSL\n");
3615 priv_ctx.target_version = ARB;
3618 if (reg_maps->rt_mask > 1)
3620 shader_addline(buffer, "OPTION ARB_draw_buffers;\n");
3623 if (reg_maps->shader_version.major < 3)
3625 switch(args->super.fog) {
3626 case FOG_OFF:
3627 break;
3628 case FOG_LINEAR:
3629 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
3630 break;
3631 case FOG_EXP:
3632 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
3633 break;
3634 case FOG_EXP2:
3635 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
3636 break;
3640 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
3641 * unused temps away(but occupies them for the whole shader if they're used once). Always
3642 * declaring them avoids tricky bookkeeping work
3644 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
3645 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
3646 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
3647 if(dcl_td) shader_addline(buffer, "TEMP TD;\n"); /* Used for sRGB writing */
3648 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
3649 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
3650 shader_addline(buffer, "PARAM ps_helper_const = { 0.0, 1.0, %1.10f, 0.0 };\n", eps);
3652 if (reg_maps->shader_version.major < 2)
3654 strcpy(fragcolor, "R0");
3656 else
3658 if (args->super.srgb_correction)
3660 if (shader->u.ps.color0_mov)
3662 sprintf(fragcolor, "R%u", shader->u.ps.color0_reg);
3664 else
3666 shader_addline(buffer, "TEMP TMP_COLOR;\n");
3667 strcpy(fragcolor, "TMP_COLOR");
3669 } else {
3670 strcpy(fragcolor, "result.color");
3674 if(args->super.srgb_correction) {
3675 shader_addline(buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
3676 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
3677 shader_addline(buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
3678 srgb_sub_high, 0.0, 0.0, 0.0);
3681 /* Base Declarations */
3682 next_local = shader_generate_arb_declarations(shader, reg_maps,
3683 buffer, gl_info, lconst_map, NULL, &priv_ctx);
3685 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
3687 unsigned char bump_const;
3689 if (!(map & 1)) continue;
3691 bump_const = compiled->numbumpenvmatconsts;
3692 compiled->bumpenvmatconst[bump_const].const_num = WINED3D_CONST_NUM_UNUSED;
3693 compiled->bumpenvmatconst[bump_const].texunit = i;
3694 compiled->luminanceconst[bump_const].const_num = WINED3D_CONST_NUM_UNUSED;
3695 compiled->luminanceconst[bump_const].texunit = i;
3697 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3698 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3699 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3700 * textures due to conditional NP2 restrictions)
3702 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3703 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3704 * their location is shader dependent anyway and they cannot be loaded globally.
3706 compiled->bumpenvmatconst[bump_const].const_num = next_local++;
3707 shader_addline(buffer, "PARAM bumpenvmat%d = program.local[%d];\n",
3708 i, compiled->bumpenvmatconst[bump_const].const_num);
3709 compiled->numbumpenvmatconsts = bump_const + 1;
3711 if (!(reg_maps->luminanceparams & (1 << i))) continue;
3713 compiled->luminanceconst[bump_const].const_num = next_local++;
3714 shader_addline(buffer, "PARAM luminance%d = program.local[%d];\n",
3715 i, compiled->luminanceconst[bump_const].const_num);
3718 for(i = 0; i < MAX_CONST_I; i++)
3720 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
3721 if (reg_maps->integer_constants & (1 << i) && priv_ctx.target_version >= NV2)
3723 const DWORD *control_values = find_loop_control_values(shader, i);
3725 if(control_values)
3727 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
3728 control_values[0], control_values[1], control_values[2]);
3730 else
3732 compiled->int_consts[i] = next_local;
3733 compiled->num_int_consts++;
3734 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
3739 if(reg_maps->vpos || reg_maps->usesdsy)
3741 compiled->ycorrection = next_local;
3742 shader_addline(buffer, "PARAM ycorrection = program.local[%u];\n", next_local++);
3744 if(reg_maps->vpos)
3746 shader_addline(buffer, "TEMP vpos;\n");
3747 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3748 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3749 * ycorrection.z: 1.0
3750 * ycorrection.w: 0.0
3752 shader_addline(buffer, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3753 shader_addline(buffer, "FLR vpos.xy, vpos;\n");
3756 else
3758 compiled->ycorrection = WINED3D_CONST_NUM_UNUSED;
3761 /* Load constants to fixup NP2 texcoords if there are still free constants left:
3762 * Constants (texture dimensions) for the NP2 fixup are loaded as local program parameters. This will consume
3763 * at most 8 (MAX_FRAGMENT_SAMPLERS / 2) parameters, which is highly unlikely, since the application had to
3764 * use 16 NP2 textures at the same time. In case that we run out of constants the fixup is simply not
3765 * applied / activated. This will probably result in wrong rendering of the texture, but will save us from
3766 * shader compilation errors and the subsequent errors when drawing with this shader. */
3767 if (priv_ctx.cur_ps_args->super.np2_fixup) {
3768 unsigned char cur_fixup_sampler = 0;
3770 struct arb_ps_np2fixup_info* const fixup = priv_ctx.cur_np2fixup_info;
3771 const WORD map = priv_ctx.cur_ps_args->super.np2_fixup;
3772 const UINT max_lconsts = gl_info->limits.arb_ps_local_constants;
3774 fixup->offset = next_local;
3775 fixup->super.active = 0;
3777 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
3778 if (!(map & (1 << i))) continue;
3780 if (fixup->offset + (cur_fixup_sampler >> 1) < max_lconsts) {
3781 fixup->super.active |= (1 << i);
3782 fixup->super.idx[i] = cur_fixup_sampler++;
3783 } else {
3784 FIXME("No free constant found to load NP2 fixup data into shader. "
3785 "Sampling from this texture will probably look wrong.\n");
3786 break;
3790 fixup->super.num_consts = (cur_fixup_sampler + 1) >> 1;
3791 if (fixup->super.num_consts) {
3792 shader_addline(buffer, "PARAM np2fixup[%u] = { program.env[%u..%u] };\n",
3793 fixup->super.num_consts, fixup->offset, fixup->super.num_consts + fixup->offset - 1);
3796 next_local += fixup->super.num_consts;
3799 if (shader_priv->clipplane_emulation != ~0U && args->clip)
3801 shader_addline(buffer, "KIL fragment.texcoord[%u];\n", shader_priv->clipplane_emulation);
3804 /* Base Shader Body */
3805 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
3807 if(args->super.srgb_correction) {
3808 arbfp_add_sRGB_correction(buffer, fragcolor, srgbtmp[0], srgbtmp[1], srgbtmp[2], srgbtmp[3],
3809 priv_ctx.target_version >= NV2);
3812 if(strcmp(fragcolor, "result.color")) {
3813 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
3815 shader_addline(buffer, "END\n");
3817 /* TODO: change to resource.glObjectHandle or something like that */
3818 GL_EXTCALL(glGenProgramsARB(1, &retval));
3820 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
3821 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
3823 TRACE("Created hw pixel shader, prg=%d\n", retval);
3824 /* Create the program and check for errors */
3825 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3826 buffer->bsize, buffer->buffer));
3827 checkGLcall("glProgramStringARB()");
3829 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
3830 if (errPos != -1)
3832 FIXME("HW PixelShader Error at position %d: %s\n\n",
3833 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3834 shader_arb_dump_program_source(buffer->buffer);
3835 retval = 0;
3837 else
3839 GLint native;
3841 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
3842 checkGLcall("glGetProgramivARB()");
3843 if (!native) WARN("Program exceeds native resource limits.\n");
3846 /* Load immediate constants */
3847 if (lconst_map)
3849 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
3851 const float *value = (const float *)lconst->value;
3852 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst_map[lconst->idx], value));
3853 checkGLcall("glProgramLocalParameter4fvARB");
3855 HeapFree(GetProcessHeap(), 0, lconst_map);
3858 return retval;
3861 static int compare_sig(const struct wined3d_shader_signature_element *sig1, const struct wined3d_shader_signature_element *sig2)
3863 unsigned int i;
3864 int ret;
3866 for(i = 0; i < MAX_REG_INPUT; i++)
3868 if (!sig1[i].semantic_name || !sig2[i].semantic_name)
3870 /* Compare pointers, not contents. One string is NULL(element does not exist), the other one is not NULL */
3871 if(sig1[i].semantic_name != sig2[i].semantic_name) return sig1[i].semantic_name < sig2[i].semantic_name ? -1 : 1;
3872 continue;
3875 if ((ret = strcmp(sig1[i].semantic_name, sig2[i].semantic_name))) return ret;
3876 if(sig1[i].semantic_idx != sig2[i].semantic_idx) return sig1[i].semantic_idx < sig2[i].semantic_idx ? -1 : 1;
3877 if(sig1[i].sysval_semantic != sig2[i].sysval_semantic) return sig1[i].sysval_semantic < sig2[i].sysval_semantic ? -1 : 1;
3878 if(sig1[i].component_type != sig2[i].component_type) return sig1[i].component_type < sig2[i].component_type ? -1 : 1;
3879 if(sig1[i].register_idx != sig2[i].register_idx) return sig1[i].register_idx < sig2[i].register_idx ? -1 : 1;
3880 if(sig1[i].mask != sig2->mask) return sig1[i].mask < sig2[i].mask ? -1 : 1;
3882 return 0;
3885 static struct wined3d_shader_signature_element *clone_sig(const struct wined3d_shader_signature_element *sig)
3887 struct wined3d_shader_signature_element *new;
3888 int i;
3889 char *name;
3891 new = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*new) * MAX_REG_INPUT);
3892 for(i = 0; i < MAX_REG_INPUT; i++)
3894 if (!sig[i].semantic_name) continue;
3896 new[i] = sig[i];
3897 /* Clone the semantic string */
3898 name = HeapAlloc(GetProcessHeap(), 0, strlen(sig[i].semantic_name) + 1);
3899 strcpy(name, sig[i].semantic_name);
3900 new[i].semantic_name = name;
3902 return new;
3905 static DWORD find_input_signature(struct shader_arb_priv *priv, const struct wined3d_shader_signature_element *sig)
3907 struct wine_rb_entry *entry = wine_rb_get(&priv->signature_tree, sig);
3908 struct ps_signature *found_sig;
3910 if (entry)
3912 found_sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
3913 TRACE("Found existing signature %u\n", found_sig->idx);
3914 return found_sig->idx;
3916 found_sig = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*sig));
3917 found_sig->sig = clone_sig(sig);
3918 found_sig->idx = priv->ps_sig_number++;
3919 TRACE("New signature stored and assigned number %u\n", found_sig->idx);
3920 if(wine_rb_put(&priv->signature_tree, sig, &found_sig->entry) == -1)
3922 ERR("Failed to insert program entry.\n");
3924 return found_sig->idx;
3927 static void init_output_registers(struct wined3d_shader *shader, DWORD sig_num,
3928 struct shader_arb_ctx_priv *priv_ctx, struct arb_vs_compiled_shader *compiled)
3930 unsigned int i, j;
3931 static const char * const texcoords[8] =
3933 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3934 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
3936 struct wined3d_device *device = shader->device;
3937 const struct wined3d_shader_signature_element *sig;
3938 const char *semantic_name;
3939 DWORD semantic_idx, reg_idx;
3941 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
3942 * and varying 9 to result.color.secondary
3944 static const char * const decl_idx_to_string[MAX_REG_INPUT] =
3946 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3947 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]",
3948 "result.color.primary", "result.color.secondary"
3951 if(sig_num == ~0)
3953 TRACE("Pixel shader uses builtin varyings\n");
3954 /* Map builtins to builtins */
3955 for(i = 0; i < 8; i++)
3957 priv_ctx->texcrd_output[i] = texcoords[i];
3959 priv_ctx->color_output[0] = "result.color.primary";
3960 priv_ctx->color_output[1] = "result.color.secondary";
3961 priv_ctx->fog_output = "result.fogcoord";
3963 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
3964 for (i = 0; i < (sizeof(shader->output_signature) / sizeof(*shader->output_signature)); ++i)
3966 semantic_name = shader->output_signature[i].semantic_name;
3967 if (!semantic_name) continue;
3969 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3971 TRACE("o%u is TMP_OUT\n", i);
3972 if (!shader->output_signature[i].semantic_idx) priv_ctx->vs_output[i] = "TMP_OUT";
3973 else priv_ctx->vs_output[i] = "TA";
3975 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3977 TRACE("o%u is result.pointsize\n", i);
3978 if (!shader->output_signature[i].semantic_idx) priv_ctx->vs_output[i] = "result.pointsize";
3979 else priv_ctx->vs_output[i] = "TA";
3981 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3983 TRACE("o%u is result.color.?, idx %u\n", i, shader->output_signature[i].semantic_idx);
3984 if (!shader->output_signature[i].semantic_idx)
3985 priv_ctx->vs_output[i] = "result.color.primary";
3986 else if (shader->output_signature[i].semantic_idx == 1)
3987 priv_ctx->vs_output[i] = "result.color.secondary";
3988 else priv_ctx->vs_output[i] = "TA";
3990 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3992 TRACE("o%u is %s\n", i, texcoords[shader->output_signature[i].semantic_idx]);
3993 if (shader->output_signature[i].semantic_idx >= 8) priv_ctx->vs_output[i] = "TA";
3994 else priv_ctx->vs_output[i] = texcoords[shader->output_signature[i].semantic_idx];
3996 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3998 TRACE("o%u is result.fogcoord\n", i);
3999 if (shader->output_signature[i].semantic_idx > 0) priv_ctx->vs_output[i] = "TA";
4000 else priv_ctx->vs_output[i] = "result.fogcoord";
4002 else
4004 priv_ctx->vs_output[i] = "TA";
4007 return;
4010 /* Instead of searching for the signature in the signature list, read the one from the current pixel shader.
4011 * Its maybe not the shader where the signature came from, but it is the same signature and faster to find
4013 sig = device->stateBlock->state.pixel_shader->input_signature;
4014 TRACE("Pixel shader uses declared varyings\n");
4016 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
4017 for(i = 0; i < 8; i++)
4019 priv_ctx->texcrd_output[i] = "TA";
4021 priv_ctx->color_output[0] = "TA";
4022 priv_ctx->color_output[1] = "TA";
4023 priv_ctx->fog_output = "TA";
4025 for(i = 0; i < MAX_REG_INPUT; i++)
4027 semantic_name = sig[i].semantic_name;
4028 semantic_idx = sig[i].semantic_idx;
4029 reg_idx = sig[i].register_idx;
4030 if (!semantic_name) continue;
4032 /* If a declared input register is not written by builtin arguments, don't write to it.
4033 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
4035 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
4036 * to TMP_OUT in any case
4038 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
4040 if(semantic_idx < 8) priv_ctx->texcrd_output[semantic_idx] = decl_idx_to_string[reg_idx];
4042 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
4044 if(semantic_idx < 2) priv_ctx->color_output[semantic_idx] = decl_idx_to_string[reg_idx];
4046 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
4048 if (!semantic_idx) priv_ctx->fog_output = decl_idx_to_string[reg_idx];
4050 else
4052 continue;
4055 if (!strcmp(decl_idx_to_string[reg_idx], "result.color.primary")
4056 || !strcmp(decl_idx_to_string[reg_idx], "result.color.secondary"))
4058 compiled->need_color_unclamp = TRUE;
4062 /* Map declared to declared */
4063 for (i = 0; i < (sizeof(shader->output_signature) / sizeof(*shader->output_signature)); ++i)
4065 /* Write unread output to TA to throw them away */
4066 priv_ctx->vs_output[i] = "TA";
4067 semantic_name = shader->output_signature[i].semantic_name;
4068 if (!semantic_name) continue;
4070 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION)
4071 && !shader->output_signature[i].semantic_idx)
4073 priv_ctx->vs_output[i] = "TMP_OUT";
4074 continue;
4076 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE)
4077 && !shader->output_signature[i].semantic_idx)
4079 priv_ctx->vs_output[i] = "result.pointsize";
4080 continue;
4083 for(j = 0; j < MAX_REG_INPUT; j++)
4085 if (!sig[j].semantic_name) continue;
4087 if (!strcmp(sig[j].semantic_name, semantic_name)
4088 && sig[j].semantic_idx == shader->output_signature[i].semantic_idx)
4090 priv_ctx->vs_output[i] = decl_idx_to_string[sig[j].register_idx];
4092 if (!strcmp(priv_ctx->vs_output[i], "result.color.primary")
4093 || !strcmp(priv_ctx->vs_output[i], "result.color.secondary"))
4095 compiled->need_color_unclamp = TRUE;
4102 /* GL locking is done by the caller */
4103 static GLuint shader_arb_generate_vshader(struct wined3d_shader *shader,
4104 const struct wined3d_gl_info *gl_info, struct wined3d_shader_buffer *buffer,
4105 const struct arb_vs_compile_args *args, struct arb_vs_compiled_shader *compiled)
4107 const struct arb_vshader_private *shader_data = shader->backend_data;
4108 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4109 const DWORD *function = shader->function;
4110 const local_constant *lconst;
4111 GLuint ret;
4112 DWORD next_local, *lconst_map = local_const_mapping(shader);
4113 struct shader_arb_ctx_priv priv_ctx;
4114 unsigned int i;
4115 GLint errPos;
4117 memset(&priv_ctx, 0, sizeof(priv_ctx));
4118 priv_ctx.cur_vs_args = args;
4119 list_init(&priv_ctx.control_frames);
4120 init_output_registers(shader, args->ps_signature, &priv_ctx, compiled);
4122 /* Create the hw ARB shader */
4123 shader_addline(buffer, "!!ARBvp1.0\n");
4125 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
4126 * mesurable performance penalty, and we can always make use of it for clipplanes.
4128 if (gl_info->supported[NV_VERTEX_PROGRAM3])
4130 shader_addline(buffer, "OPTION NV_vertex_program3;\n");
4131 priv_ctx.target_version = NV3;
4132 shader_addline(buffer, "ADDRESS aL;\n");
4134 else if (gl_info->supported[NV_VERTEX_PROGRAM2_OPTION])
4136 shader_addline(buffer, "OPTION NV_vertex_program2;\n");
4137 priv_ctx.target_version = NV2;
4138 shader_addline(buffer, "ADDRESS aL;\n");
4139 } else {
4140 priv_ctx.target_version = ARB;
4143 shader_addline(buffer, "TEMP TMP_OUT;\n");
4144 if (need_helper_const(shader_data, reg_maps, gl_info))
4146 shader_addline(buffer, "PARAM helper_const = { 0.0, 1.0, 2.0, %1.10f};\n", eps);
4148 if (need_rel_addr_const(shader_data, reg_maps, gl_info))
4150 shader_addline(buffer, "PARAM rel_addr_const = { 0.5, %d.0, 0.0, 0.0 };\n", shader_data->rel_offset);
4151 shader_addline(buffer, "TEMP A0_SHADOW;\n");
4154 shader_addline(buffer, "TEMP TA;\n");
4155 shader_addline(buffer, "TEMP TB;\n");
4157 /* Base Declarations */
4158 next_local = shader_generate_arb_declarations(shader, reg_maps, buffer,
4159 gl_info, lconst_map, &priv_ctx.vs_clipplanes, &priv_ctx);
4161 for(i = 0; i < MAX_CONST_I; i++)
4163 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
4164 if(reg_maps->integer_constants & (1 << i) && priv_ctx.target_version >= NV2)
4166 const DWORD *control_values = find_loop_control_values(shader, i);
4168 if(control_values)
4170 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
4171 control_values[0], control_values[1], control_values[2]);
4173 else
4175 compiled->int_consts[i] = next_local;
4176 compiled->num_int_consts++;
4177 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
4182 /* We need a constant to fixup the final position */
4183 shader_addline(buffer, "PARAM posFixup = program.local[%u];\n", next_local);
4184 compiled->pos_fixup = next_local++;
4186 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
4187 * for output parameters. D3D in theory does not do that either, but some applications depend on a
4188 * proper initialization of the secondary color, and programs using the fixed function pipeline without
4189 * a replacement shader depend on the texcoord.w being set properly.
4191 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
4192 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
4193 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
4194 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
4195 * this can eat a number of instructions, so skip it unless this cap is set as well
4197 if (!gl_info->supported[NV_VERTEX_PROGRAM])
4199 struct wined3d_device *device = shader->device;
4200 const char *color_init = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_0001);
4201 shader_addline(buffer, "MOV result.color.secondary, %s;\n", color_init);
4203 if (gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W && !device->frag_pipe->ffp_proj_control)
4205 int i;
4206 const char *one = arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX, ARB_ONE);
4207 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++)
4209 if (reg_maps->texcoord_mask[i] && reg_maps->texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL)
4210 shader_addline(buffer, "MOV result.texcoord[%u].w, %s\n", i, one);
4215 /* The shader starts with the main function */
4216 priv_ctx.in_main_func = TRUE;
4217 /* Base Shader Body */
4218 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4220 if (!priv_ctx.footer_written) vshader_add_footer(&priv_ctx,
4221 shader_data, args, reg_maps, gl_info, buffer);
4223 shader_addline(buffer, "END\n");
4225 /* TODO: change to resource.glObjectHandle or something like that */
4226 GL_EXTCALL(glGenProgramsARB(1, &ret));
4228 TRACE("Creating a hw vertex shader, prg=%d\n", ret);
4229 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, ret));
4231 TRACE("Created hw vertex shader, prg=%d\n", ret);
4232 /* Create the program and check for errors */
4233 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
4234 buffer->bsize, buffer->buffer));
4235 checkGLcall("glProgramStringARB()");
4237 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
4238 if (errPos != -1)
4240 FIXME("HW VertexShader Error at position %d: %s\n\n",
4241 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
4242 shader_arb_dump_program_source(buffer->buffer);
4243 ret = -1;
4245 else
4247 GLint native;
4249 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
4250 checkGLcall("glGetProgramivARB()");
4251 if (!native) WARN("Program exceeds native resource limits.\n");
4253 /* Load immediate constants */
4254 if (lconst_map)
4256 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
4258 const float *value = (const float *)lconst->value;
4259 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst_map[lconst->idx], value));
4263 HeapFree(GetProcessHeap(), 0, lconst_map);
4265 return ret;
4268 /* GL locking is done by the caller */
4269 static struct arb_ps_compiled_shader *find_arb_pshader(struct wined3d_shader *shader,
4270 const struct arb_ps_compile_args *args)
4272 struct wined3d_device *device = shader->device;
4273 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4274 UINT i;
4275 DWORD new_size;
4276 struct arb_ps_compiled_shader *new_array;
4277 struct wined3d_shader_buffer buffer;
4278 struct arb_pshader_private *shader_data;
4279 GLuint ret;
4281 if (!shader->backend_data)
4283 struct shader_arb_priv *priv = device->shader_priv;
4285 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4286 shader_data = shader->backend_data;
4287 shader_data->clamp_consts = shader->reg_maps.shader_version.major == 1;
4289 if (shader->reg_maps.shader_version.major < 3)
4290 shader_data->input_signature_idx = ~0;
4291 else
4292 shader_data->input_signature_idx = find_input_signature(priv, shader->input_signature);
4294 shader_data->has_signature_idx = TRUE;
4295 TRACE("Shader got assigned input signature index %u\n", shader_data->input_signature_idx);
4297 if (!device->vs_clipping)
4298 shader_data->clipplane_emulation = shader_find_free_input_register(&shader->reg_maps,
4299 gl_info->limits.texture_stages - 1);
4300 else
4301 shader_data->clipplane_emulation = ~0U;
4303 shader_data = shader->backend_data;
4305 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4306 * so a linear search is more performant than a hashmap or a binary search
4307 * (cache coherency etc)
4309 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4311 if (!memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)))
4312 return &shader_data->gl_shaders[i];
4315 TRACE("No matching GL shader found, compiling a new shader\n");
4316 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4317 if (shader_data->num_gl_shaders)
4319 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4320 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4321 new_size * sizeof(*shader_data->gl_shaders));
4322 } else {
4323 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4324 new_size = 1;
4327 if(!new_array) {
4328 ERR("Out of memory\n");
4329 return 0;
4331 shader_data->gl_shaders = new_array;
4332 shader_data->shader_array_size = new_size;
4335 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4337 pixelshader_update_samplers(&shader->reg_maps, device->stateBlock->state.textures);
4339 if (!shader_buffer_init(&buffer))
4341 ERR("Failed to initialize shader buffer.\n");
4342 return 0;
4345 ret = shader_arb_generate_pshader(shader, gl_info, &buffer, args,
4346 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4347 shader_buffer_free(&buffer);
4348 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4350 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4353 static inline BOOL vs_args_equal(const struct arb_vs_compile_args *stored, const struct arb_vs_compile_args *new,
4354 const DWORD use_map, BOOL skip_int) {
4355 if((stored->super.swizzle_map & use_map) != new->super.swizzle_map) return FALSE;
4356 if(stored->super.clip_enabled != new->super.clip_enabled) return FALSE;
4357 if(stored->super.fog_src != new->super.fog_src) return FALSE;
4358 if(stored->clip.boolclip_compare != new->clip.boolclip_compare) return FALSE;
4359 if(stored->ps_signature != new->ps_signature) return FALSE;
4360 if(stored->vertex.samplers_compare != new->vertex.samplers_compare) return FALSE;
4361 if(skip_int) return TRUE;
4363 return !memcmp(stored->loop_ctrl, new->loop_ctrl, sizeof(stored->loop_ctrl));
4366 static struct arb_vs_compiled_shader *find_arb_vshader(struct wined3d_shader *shader,
4367 const struct arb_vs_compile_args *args)
4369 struct wined3d_device *device = shader->device;
4370 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4371 DWORD use_map = device->strided_streams.use_map;
4372 UINT i;
4373 DWORD new_size;
4374 struct arb_vs_compiled_shader *new_array;
4375 struct wined3d_shader_buffer buffer;
4376 struct arb_vshader_private *shader_data;
4377 GLuint ret;
4379 if (!shader->backend_data)
4381 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4383 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4384 shader_data = shader->backend_data;
4386 if ((gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
4387 && reg_maps->min_rel_offset <= reg_maps->max_rel_offset)
4389 if (reg_maps->max_rel_offset - reg_maps->min_rel_offset > 127)
4391 FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
4392 FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
4393 FIXME("Min: %u, Max: %u.\n", reg_maps->min_rel_offset, reg_maps->max_rel_offset);
4395 else if (reg_maps->max_rel_offset - reg_maps->min_rel_offset > 63)
4396 shader_data->rel_offset = reg_maps->min_rel_offset + 63;
4397 else if (reg_maps->max_rel_offset > 63)
4398 shader_data->rel_offset = reg_maps->min_rel_offset;
4401 shader_data = shader->backend_data;
4403 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4404 * so a linear search is more performant than a hashmap or a binary search
4405 * (cache coherency etc)
4407 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4408 if (vs_args_equal(&shader_data->gl_shaders[i].args, args,
4409 use_map, gl_info->supported[NV_VERTEX_PROGRAM2_OPTION]))
4411 return &shader_data->gl_shaders[i];
4415 TRACE("No matching GL shader found, compiling a new shader\n");
4417 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4418 if (shader_data->num_gl_shaders)
4420 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4421 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4422 new_size * sizeof(*shader_data->gl_shaders));
4423 } else {
4424 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4425 new_size = 1;
4428 if(!new_array) {
4429 ERR("Out of memory\n");
4430 return 0;
4432 shader_data->gl_shaders = new_array;
4433 shader_data->shader_array_size = new_size;
4436 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4438 if (!shader_buffer_init(&buffer))
4440 ERR("Failed to initialize shader buffer.\n");
4441 return 0;
4444 ret = shader_arb_generate_vshader(shader, gl_info, &buffer, args,
4445 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4446 shader_buffer_free(&buffer);
4447 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4449 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4452 static void find_arb_ps_compile_args(const struct wined3d_state *state,
4453 const struct wined3d_shader *shader, struct arb_ps_compile_args *args)
4455 struct wined3d_device *device = shader->device;
4456 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4457 int i;
4458 WORD int_skip;
4460 find_ps_compile_args(state, shader, &args->super);
4462 /* This forces all local boolean constants to 1 to make them stateblock independent */
4463 args->bools = shader->reg_maps.local_bool_consts;
4465 for(i = 0; i < MAX_CONST_B; i++)
4467 if (state->ps_consts_b[i])
4468 args->bools |= ( 1 << i);
4471 /* Only enable the clip plane emulation KIL if at least one clipplane is enabled. The KIL instruction
4472 * is quite expensive because it forces the driver to disable early Z discards. It is cheaper to
4473 * duplicate the shader than have a no-op KIL instruction in every shader
4475 if (!device->vs_clipping && use_vs(state)
4476 && state->render_states[WINED3DRS_CLIPPING]
4477 && state->render_states[WINED3DRS_CLIPPLANEENABLE])
4478 args->clip = 1;
4479 else
4480 args->clip = 0;
4482 /* Skip if unused or local, or supported natively */
4483 int_skip = ~shader->reg_maps.integer_constants | shader->reg_maps.local_int_consts;
4484 if (int_skip == 0xffff || gl_info->supported[NV_FRAGMENT_PROGRAM_OPTION])
4486 memset(&args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4487 return;
4490 for(i = 0; i < MAX_CONST_I; i++)
4492 if(int_skip & (1 << i))
4494 args->loop_ctrl[i][0] = 0;
4495 args->loop_ctrl[i][1] = 0;
4496 args->loop_ctrl[i][2] = 0;
4498 else
4500 args->loop_ctrl[i][0] = state->ps_consts_i[i * 4];
4501 args->loop_ctrl[i][1] = state->ps_consts_i[i * 4 + 1];
4502 args->loop_ctrl[i][2] = state->ps_consts_i[i * 4 + 2];
4507 static void find_arb_vs_compile_args(const struct wined3d_state *state,
4508 const struct wined3d_shader *shader, struct arb_vs_compile_args *args)
4510 struct wined3d_device *device = shader->device;
4511 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4512 int i;
4513 WORD int_skip;
4515 find_vs_compile_args(state, shader, &args->super);
4517 args->clip.boolclip_compare = 0;
4518 if (use_ps(state))
4520 const struct wined3d_shader *ps = state->pixel_shader;
4521 const struct arb_pshader_private *shader_priv = ps->backend_data;
4522 args->ps_signature = shader_priv->input_signature_idx;
4524 args->clip.boolclip.clip_texcoord = shader_priv->clipplane_emulation + 1;
4526 else
4528 args->ps_signature = ~0;
4529 if (!device->vs_clipping)
4531 args->clip.boolclip.clip_texcoord = ffp_clip_emul(state) ? gl_info->limits.texture_stages : 0;
4533 /* Otherwise: Setting boolclip_compare set clip_texcoord to 0 */
4536 if (args->clip.boolclip.clip_texcoord)
4538 if (state->render_states[WINED3DRS_CLIPPING])
4539 args->clip.boolclip.clipplane_mask = (unsigned char)state->render_states[WINED3DRS_CLIPPLANEENABLE];
4540 /* clipplane_mask was set to 0 by setting boolclip_compare to 0 */
4543 /* This forces all local boolean constants to 1 to make them stateblock independent */
4544 args->clip.boolclip.bools = shader->reg_maps.local_bool_consts;
4545 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
4546 for(i = 0; i < MAX_CONST_B; i++)
4548 if (state->vs_consts_b[i])
4549 args->clip.boolclip.bools |= ( 1 << i);
4552 args->vertex.samplers[0] = device->texUnitMap[MAX_FRAGMENT_SAMPLERS + 0];
4553 args->vertex.samplers[1] = device->texUnitMap[MAX_FRAGMENT_SAMPLERS + 1];
4554 args->vertex.samplers[2] = device->texUnitMap[MAX_FRAGMENT_SAMPLERS + 2];
4555 args->vertex.samplers[3] = 0;
4557 /* Skip if unused or local */
4558 int_skip = ~shader->reg_maps.integer_constants | shader->reg_maps.local_int_consts;
4559 /* This is about flow control, not clipping. */
4560 if (int_skip == 0xffff || gl_info->supported[NV_VERTEX_PROGRAM2_OPTION])
4562 memset(&args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4563 return;
4566 for(i = 0; i < MAX_CONST_I; i++)
4568 if(int_skip & (1 << i))
4570 args->loop_ctrl[i][0] = 0;
4571 args->loop_ctrl[i][1] = 0;
4572 args->loop_ctrl[i][2] = 0;
4574 else
4576 args->loop_ctrl[i][0] = state->vs_consts_i[i * 4];
4577 args->loop_ctrl[i][1] = state->vs_consts_i[i * 4 + 1];
4578 args->loop_ctrl[i][2] = state->vs_consts_i[i * 4 + 2];
4583 /* GL locking is done by the caller */
4584 static void shader_arb_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4586 struct wined3d_device *device = context->swapchain->device;
4587 struct shader_arb_priv *priv = device->shader_priv;
4588 const struct wined3d_gl_info *gl_info = context->gl_info;
4589 const struct wined3d_state *state = &device->stateBlock->state;
4590 int i;
4592 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
4593 if (usePS)
4595 struct wined3d_shader *ps = state->pixel_shader;
4596 struct arb_ps_compile_args compile_args;
4597 struct arb_ps_compiled_shader *compiled;
4599 TRACE("Using pixel shader %p.\n", ps);
4600 find_arb_ps_compile_args(state, ps, &compile_args);
4601 compiled = find_arb_pshader(ps, &compile_args);
4602 priv->current_fprogram_id = compiled->prgId;
4603 priv->compiled_fprog = compiled;
4605 /* Bind the fragment program */
4606 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4607 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
4609 if(!priv->use_arbfp_fixed_func) {
4610 /* Enable OpenGL fragment programs */
4611 glEnable(GL_FRAGMENT_PROGRAM_ARB);
4612 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
4614 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n",
4615 device, priv->current_fprogram_id);
4617 /* Pixel Shader 1.x constants are clamped to [-1;1], Pixel Shader 2.0 constants are not. If switching between
4618 * a 1.x and newer shader, reload the first 8 constants
4620 if (priv->last_ps_const_clamped != ((struct arb_pshader_private *)ps->backend_data)->clamp_consts)
4622 priv->last_ps_const_clamped = ((struct arb_pshader_private *)ps->backend_data)->clamp_consts;
4623 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, 8);
4624 for(i = 0; i < 8; i++)
4626 context->pshader_const_dirty[i] = 1;
4628 /* Also takes care of loading local constants */
4629 shader_arb_load_constants(context, TRUE, FALSE);
4631 else
4633 UINT rt_height = device->fb.render_targets[0]->resource.height;
4634 shader_arb_ps_local_constants(compiled, context, state, rt_height);
4637 /* Force constant reloading for the NP2 fixup (see comment in shader_glsl_select for more info) */
4638 if (compiled->np2fixup_info.super.active)
4639 shader_arb_load_np2fixup_constants(priv, gl_info, state);
4641 else if (gl_info->supported[ARB_FRAGMENT_PROGRAM] && !priv->use_arbfp_fixed_func)
4643 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
4644 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
4645 * replacement shader
4647 glDisable(GL_FRAGMENT_PROGRAM_ARB);
4648 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4649 priv->current_fprogram_id = 0;
4652 if (useVS)
4654 struct wined3d_shader *vs = state->vertex_shader;
4655 struct arb_vs_compile_args compile_args;
4656 struct arb_vs_compiled_shader *compiled;
4658 TRACE("Using vertex shader %p\n", vs);
4659 find_arb_vs_compile_args(state, vs, &compile_args);
4660 compiled = find_arb_vshader(vs, &compile_args);
4661 priv->current_vprogram_id = compiled->prgId;
4662 priv->compiled_vprog = compiled;
4664 /* Bind the vertex program */
4665 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4666 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
4668 /* Enable OpenGL vertex programs */
4669 glEnable(GL_VERTEX_PROGRAM_ARB);
4670 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
4671 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", device, priv->current_vprogram_id);
4672 shader_arb_vs_local_constants(compiled, context, state);
4674 if(priv->last_vs_color_unclamp != compiled->need_color_unclamp) {
4675 priv->last_vs_color_unclamp = compiled->need_color_unclamp;
4677 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4679 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, !compiled->need_color_unclamp));
4680 checkGLcall("glClampColorARB");
4681 } else {
4682 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4686 else if (gl_info->supported[ARB_VERTEX_PROGRAM])
4688 priv->current_vprogram_id = 0;
4689 glDisable(GL_VERTEX_PROGRAM_ARB);
4690 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4694 /* GL locking is done by the caller */
4695 static void shader_arb_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
4696 enum tex_types tex_type, const SIZE *ds_mask_size)
4698 const float mask[] = {0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy};
4699 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
4700 struct shader_arb_priv *priv = shader_priv;
4701 GLuint *blt_fprogram;
4703 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
4704 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
4705 glEnable(GL_VERTEX_PROGRAM_ARB);
4707 blt_fprogram = masked ? &priv->depth_blt_fprogram_id_masked[tex_type] : &priv->depth_blt_fprogram_id_full[tex_type];
4708 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type, masked);
4709 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
4710 if (masked) GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, mask));
4711 glEnable(GL_FRAGMENT_PROGRAM_ARB);
4714 /* GL locking is done by the caller */
4715 static void shader_arb_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
4717 struct shader_arb_priv *priv = shader_priv;
4719 if (priv->current_vprogram_id) {
4720 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4721 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
4723 TRACE("Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB.\n", priv->current_vprogram_id);
4725 else
4727 glDisable(GL_VERTEX_PROGRAM_ARB);
4728 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4731 if (priv->current_fprogram_id) {
4732 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4733 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
4735 TRACE("Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB.\n", priv->current_fprogram_id);
4737 else if(!priv->use_arbfp_fixed_func)
4739 glDisable(GL_FRAGMENT_PROGRAM_ARB);
4740 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4744 static void shader_arb_destroy(struct wined3d_shader *shader)
4746 struct wined3d_device *device = shader->device;
4747 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4749 if (shader_is_pshader_version(shader->reg_maps.shader_version.type))
4751 struct arb_pshader_private *shader_data = shader->backend_data;
4752 UINT i;
4754 if(!shader_data) return; /* This can happen if a shader was never compiled */
4756 if (shader_data->num_gl_shaders)
4758 struct wined3d_context *context = context_acquire(device, NULL);
4760 ENTER_GL();
4761 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4763 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4764 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4766 LEAVE_GL();
4768 context_release(context);
4771 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4772 HeapFree(GetProcessHeap(), 0, shader_data);
4773 shader->backend_data = NULL;
4775 else
4777 struct arb_vshader_private *shader_data = shader->backend_data;
4778 UINT i;
4780 if(!shader_data) return; /* This can happen if a shader was never compiled */
4782 if (shader_data->num_gl_shaders)
4784 struct wined3d_context *context = context_acquire(device, NULL);
4786 ENTER_GL();
4787 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4789 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4790 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4792 LEAVE_GL();
4794 context_release(context);
4797 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4798 HeapFree(GetProcessHeap(), 0, shader_data);
4799 shader->backend_data = NULL;
4803 static int sig_tree_compare(const void *key, const struct wine_rb_entry *entry)
4805 struct ps_signature *e = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4806 return compare_sig(key, e->sig);
4809 static const struct wine_rb_functions sig_tree_functions =
4811 wined3d_rb_alloc,
4812 wined3d_rb_realloc,
4813 wined3d_rb_free,
4814 sig_tree_compare
4817 static HRESULT shader_arb_alloc(struct wined3d_device *device)
4819 struct shader_arb_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv));
4820 if(wine_rb_init(&priv->signature_tree, &sig_tree_functions) == -1)
4822 ERR("RB tree init failed\n");
4823 HeapFree(GetProcessHeap(), 0, priv);
4824 return E_OUTOFMEMORY;
4826 device->shader_priv = priv;
4827 return WINED3D_OK;
4830 static void release_signature(struct wine_rb_entry *entry, void *context)
4832 struct ps_signature *sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4833 int i;
4834 for(i = 0; i < MAX_REG_INPUT; i++)
4836 HeapFree(GetProcessHeap(), 0, (char *) sig->sig[i].semantic_name);
4838 HeapFree(GetProcessHeap(), 0, sig->sig);
4839 HeapFree(GetProcessHeap(), 0, sig);
4842 /* Context activation is done by the caller. */
4843 static void shader_arb_free(struct wined3d_device *device)
4845 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4846 struct shader_arb_priv *priv = device->shader_priv;
4847 int i;
4849 ENTER_GL();
4850 if(priv->depth_blt_vprogram_id) {
4851 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
4853 for (i = 0; i < tex_type_count; ++i)
4855 if (priv->depth_blt_fprogram_id_full[i])
4857 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id_full[i]));
4859 if (priv->depth_blt_fprogram_id_masked[i])
4861 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id_masked[i]));
4864 LEAVE_GL();
4866 wine_rb_destroy(&priv->signature_tree, release_signature, NULL);
4867 HeapFree(GetProcessHeap(), 0, device->shader_priv);
4870 static BOOL shader_arb_dirty_const(void)
4872 return TRUE;
4875 static void shader_arb_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
4877 if (gl_info->supported[ARB_VERTEX_PROGRAM])
4879 DWORD vs_consts;
4881 /* 96 is the minimum allowed value of MAX_PROGRAM_ENV_PARAMETERS_ARB
4882 * for vertex programs. If the native limit is less than that it's
4883 * not very useful, and e.g. Mesa swrast returns 0, probably to
4884 * indicate it's a software implementation. */
4885 if (gl_info->limits.arb_vs_native_constants < 96)
4886 vs_consts = gl_info->limits.arb_vs_float_constants;
4887 else
4888 vs_consts = min(gl_info->limits.arb_vs_float_constants, gl_info->limits.arb_vs_native_constants);
4890 if (gl_info->supported[NV_VERTEX_PROGRAM3])
4892 caps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4893 TRACE_(d3d_caps)("Hardware vertex shader version 3.0 enabled (NV_VERTEX_PROGRAM3)\n");
4895 else if (vs_consts >= 256)
4897 /* Shader Model 2.0 requires at least 256 vertex shader constants */
4898 caps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4899 TRACE_(d3d_caps)("Hardware vertex shader version 2.0 enabled (ARB_PROGRAM)\n");
4901 else
4903 caps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
4904 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
4906 caps->MaxVertexShaderConst = vs_consts;
4908 else
4910 caps->VertexShaderVersion = 0;
4911 caps->MaxVertexShaderConst = 0;
4914 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
4916 DWORD ps_consts;
4918 /* Similar as above for vertex programs, but the minimum for fragment
4919 * programs is 24. */
4920 if (gl_info->limits.arb_ps_native_constants < 24)
4921 ps_consts = gl_info->limits.arb_ps_float_constants;
4922 else
4923 ps_consts = min(gl_info->limits.arb_ps_float_constants, gl_info->limits.arb_ps_native_constants);
4925 if (gl_info->supported[NV_FRAGMENT_PROGRAM2])
4927 caps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4928 TRACE_(d3d_caps)("Hardware pixel shader version 3.0 enabled (NV_FRAGMENT_PROGRAM2)\n");
4930 else if (ps_consts >= 32)
4932 /* Shader Model 2.0 requires at least 32 pixel shader constants */
4933 caps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4934 TRACE_(d3d_caps)("Hardware pixel shader version 2.0 enabled (ARB_PROGRAM)\n");
4936 else
4938 caps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
4939 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
4941 caps->PixelShader1xMaxValue = 8.0f;
4942 caps->MaxPixelShaderConst = ps_consts;
4944 else
4946 caps->PixelShaderVersion = 0;
4947 caps->PixelShader1xMaxValue = 0.0f;
4948 caps->MaxPixelShaderConst = 0;
4951 caps->VSClipping = use_nv_clip(gl_info);
4954 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
4956 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4958 TRACE("Checking support for color_fixup:\n");
4959 dump_color_fixup_desc(fixup);
4962 /* We support everything except complex conversions. */
4963 if (!is_complex_fixup(fixup))
4965 TRACE("[OK]\n");
4966 return TRUE;
4969 TRACE("[FAILED]\n");
4970 return FALSE;
4973 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction *ins) {
4974 DWORD shift;
4975 char write_mask[20], regstr[50];
4976 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4977 BOOL is_color = FALSE;
4978 const struct wined3d_shader_dst_param *dst;
4980 if (!ins->dst_count) return;
4982 dst = &ins->dst[0];
4983 shift = dst->shift;
4984 if (!shift) return; /* Saturate alone is handled by the instructions */
4986 shader_arb_get_write_mask(ins, dst, write_mask);
4987 shader_arb_get_register_name(ins, &dst->reg, regstr, &is_color);
4989 /* Generate a line that does the output modifier computation
4990 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
4991 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
4993 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins),
4994 regstr, write_mask, regstr, shift_tab[shift]);
4997 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4999 /* WINED3DSIH_ABS */ shader_hw_map2gl,
5000 /* WINED3DSIH_ADD */ shader_hw_map2gl,
5001 /* WINED3DSIH_AND */ NULL,
5002 /* WINED3DSIH_BEM */ pshader_hw_bem,
5003 /* WINED3DSIH_BREAK */ shader_hw_break,
5004 /* WINED3DSIH_BREAKC */ shader_hw_breakc,
5005 /* WINED3DSIH_BREAKP */ NULL,
5006 /* WINED3DSIH_CALL */ shader_hw_call,
5007 /* WINED3DSIH_CALLNZ */ NULL,
5008 /* WINED3DSIH_CMP */ pshader_hw_cmp,
5009 /* WINED3DSIH_CND */ pshader_hw_cnd,
5010 /* WINED3DSIH_CRS */ shader_hw_map2gl,
5011 /* WINED3DSIH_CUT */ NULL,
5012 /* WINED3DSIH_DCL */ NULL,
5013 /* WINED3DSIH_DEF */ NULL,
5014 /* WINED3DSIH_DEFB */ NULL,
5015 /* WINED3DSIH_DEFI */ NULL,
5016 /* WINED3DSIH_DIV */ NULL,
5017 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
5018 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
5019 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
5020 /* WINED3DSIH_DST */ shader_hw_map2gl,
5021 /* WINED3DSIH_DSX */ shader_hw_map2gl,
5022 /* WINED3DSIH_DSY */ shader_hw_dsy,
5023 /* WINED3DSIH_ELSE */ shader_hw_else,
5024 /* WINED3DSIH_EMIT */ NULL,
5025 /* WINED3DSIH_ENDIF */ shader_hw_endif,
5026 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop,
5027 /* WINED3DSIH_ENDREP */ shader_hw_endrep,
5028 /* WINED3DSIH_EXP */ shader_hw_scalar_op,
5029 /* WINED3DSIH_EXPP */ shader_hw_scalar_op,
5030 /* WINED3DSIH_FRC */ shader_hw_map2gl,
5031 /* WINED3DSIH_FTOI */ NULL,
5032 /* WINED3DSIH_IADD */ NULL,
5033 /* WINED3DSIH_IEQ */ NULL,
5034 /* WINED3DSIH_IF */ NULL /* Hardcoded into the shader */,
5035 /* WINED3DSIH_IFC */ shader_hw_ifc,
5036 /* WINED3DSIH_IGE */ NULL,
5037 /* WINED3DSIH_IMUL */ NULL,
5038 /* WINED3DSIH_ITOF */ NULL,
5039 /* WINED3DSIH_LABEL */ shader_hw_label,
5040 /* WINED3DSIH_LD */ NULL,
5041 /* WINED3DSIH_LIT */ shader_hw_map2gl,
5042 /* WINED3DSIH_LOG */ shader_hw_log,
5043 /* WINED3DSIH_LOGP */ shader_hw_log,
5044 /* WINED3DSIH_LOOP */ shader_hw_loop,
5045 /* WINED3DSIH_LRP */ shader_hw_lrp,
5046 /* WINED3DSIH_LT */ NULL,
5047 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
5048 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
5049 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
5050 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
5051 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
5052 /* WINED3DSIH_MAD */ shader_hw_map2gl,
5053 /* WINED3DSIH_MAX */ shader_hw_map2gl,
5054 /* WINED3DSIH_MIN */ shader_hw_map2gl,
5055 /* WINED3DSIH_MOV */ shader_hw_mov,
5056 /* WINED3DSIH_MOVA */ shader_hw_mov,
5057 /* WINED3DSIH_MOVC */ NULL,
5058 /* WINED3DSIH_MUL */ shader_hw_map2gl,
5059 /* WINED3DSIH_NOP */ shader_hw_nop,
5060 /* WINED3DSIH_NRM */ shader_hw_nrm,
5061 /* WINED3DSIH_PHASE */ NULL,
5062 /* WINED3DSIH_POW */ shader_hw_pow,
5063 /* WINED3DSIH_RCP */ shader_hw_rcp,
5064 /* WINED3DSIH_REP */ shader_hw_rep,
5065 /* WINED3DSIH_RET */ shader_hw_ret,
5066 /* WINED3DSIH_RSQ */ shader_hw_scalar_op,
5067 /* WINED3DSIH_SAMPLE */ NULL,
5068 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
5069 /* WINED3DSIH_SAMPLE_LOD */ NULL,
5070 /* WINED3DSIH_SETP */ NULL,
5071 /* WINED3DSIH_SGE */ shader_hw_map2gl,
5072 /* WINED3DSIH_SGN */ shader_hw_sgn,
5073 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
5074 /* WINED3DSIH_SLT */ shader_hw_map2gl,
5075 /* WINED3DSIH_SQRT */ NULL,
5076 /* WINED3DSIH_SUB */ shader_hw_map2gl,
5077 /* WINED3DSIH_TEX */ pshader_hw_tex,
5078 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
5079 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
5080 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
5081 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
5082 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
5083 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
5084 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
5085 /* WINED3DSIH_TEXLDD */ shader_hw_texldd,
5086 /* WINED3DSIH_TEXLDL */ shader_hw_texldl,
5087 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
5088 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
5089 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
5090 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
5091 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5092 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
5093 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
5094 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
5095 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
5096 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
5097 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
5098 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
5099 /* WINED3DSIH_UTOF */ NULL,
5102 static BOOL get_bool_const(const struct wined3d_shader_instruction *ins,
5103 const struct wined3d_shader *shader, DWORD idx)
5105 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5106 BOOL vshader = shader_is_vshader_version(reg_maps->shader_version.type);
5107 WORD bools = 0;
5108 WORD flag = (1 << idx);
5109 const local_constant *constant;
5110 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5112 if (reg_maps->local_bool_consts & flag)
5114 /* What good is a if(bool) with a hardcoded local constant? I don't know, but handle it */
5115 LIST_FOR_EACH_ENTRY(constant, &shader->constantsB, local_constant, entry)
5117 if (constant->idx == idx)
5119 return constant->value[0];
5122 ERR("Local constant not found\n");
5123 return FALSE;
5125 else
5127 if(vshader) bools = priv->cur_vs_args->clip.boolclip.bools;
5128 else bools = priv->cur_ps_args->bools;
5129 return bools & flag;
5133 static void get_loop_control_const(const struct wined3d_shader_instruction *ins,
5134 const struct wined3d_shader *shader, UINT idx, struct wined3d_shader_loop_control *loop_control)
5136 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5137 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5139 /* Integer constants can either be a local constant, or they can be stored in the shader
5140 * type specific compile args. */
5141 if (reg_maps->local_int_consts & (1 << idx))
5143 const local_constant *constant;
5145 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, local_constant, entry)
5147 if (constant->idx == idx)
5149 loop_control->count = constant->value[0];
5150 loop_control->start = constant->value[1];
5151 /* Step is signed. */
5152 loop_control->step = (int)constant->value[2];
5153 return;
5156 /* If this happens the flag was set incorrectly */
5157 ERR("Local constant not found\n");
5158 loop_control->count = 0;
5159 loop_control->start = 0;
5160 loop_control->step = 0;
5161 return;
5164 switch (reg_maps->shader_version.type)
5166 case WINED3D_SHADER_TYPE_VERTEX:
5167 /* Count and aL start value are unsigned */
5168 loop_control->count = priv->cur_vs_args->loop_ctrl[idx][0];
5169 loop_control->start = priv->cur_vs_args->loop_ctrl[idx][1];
5170 /* Step is signed. */
5171 loop_control->step = ((char)priv->cur_vs_args->loop_ctrl[idx][2]);
5172 break;
5174 case WINED3D_SHADER_TYPE_PIXEL:
5175 loop_control->count = priv->cur_ps_args->loop_ctrl[idx][0];
5176 loop_control->start = priv->cur_ps_args->loop_ctrl[idx][1];
5177 loop_control->step = ((char)priv->cur_ps_args->loop_ctrl[idx][2]);
5178 break;
5180 default:
5181 FIXME("Unhandled shader type %#x.\n", reg_maps->shader_version.type);
5182 break;
5186 static void record_instruction(struct list *list, const struct wined3d_shader_instruction *ins)
5188 unsigned int i;
5189 struct wined3d_shader_dst_param *dst_param = NULL;
5190 struct wined3d_shader_src_param *src_param = NULL, *rel_addr = NULL;
5191 struct recorded_instruction *rec = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rec));
5192 if(!rec)
5194 ERR("Out of memory\n");
5195 return;
5198 rec->ins = *ins;
5199 dst_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param));
5200 if(!dst_param) goto free;
5201 *dst_param = *ins->dst;
5202 if(ins->dst->reg.rel_addr)
5204 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param->reg.rel_addr));
5205 if(!rel_addr) goto free;
5206 *rel_addr = *ins->dst->reg.rel_addr;
5207 dst_param->reg.rel_addr = rel_addr;
5209 rec->ins.dst = dst_param;
5211 src_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*src_param) * ins->src_count);
5212 if(!src_param) goto free;
5213 for(i = 0; i < ins->src_count; i++)
5215 src_param[i] = ins->src[i];
5216 if(ins->src[i].reg.rel_addr)
5218 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr));
5219 if(!rel_addr) goto free;
5220 *rel_addr = *ins->src[i].reg.rel_addr;
5221 src_param[i].reg.rel_addr = rel_addr;
5224 rec->ins.src = src_param;
5225 list_add_tail(list, &rec->entry);
5226 return;
5228 free:
5229 ERR("Out of memory\n");
5230 if(dst_param)
5232 HeapFree(GetProcessHeap(), 0, (void *) dst_param->reg.rel_addr);
5233 HeapFree(GetProcessHeap(), 0, dst_param);
5235 if(src_param)
5237 for(i = 0; i < ins->src_count; i++)
5239 HeapFree(GetProcessHeap(), 0, (void *) src_param[i].reg.rel_addr);
5241 HeapFree(GetProcessHeap(), 0, src_param);
5243 HeapFree(GetProcessHeap(), 0, rec);
5246 static void free_recorded_instruction(struct list *list)
5248 struct recorded_instruction *rec_ins, *entry2;
5249 unsigned int i;
5251 LIST_FOR_EACH_ENTRY_SAFE(rec_ins, entry2, list, struct recorded_instruction, entry)
5253 list_remove(&rec_ins->entry);
5254 if(rec_ins->ins.dst)
5256 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.dst->reg.rel_addr);
5257 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.dst);
5259 if(rec_ins->ins.src)
5261 for(i = 0; i < rec_ins->ins.src_count; i++)
5263 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.src[i].reg.rel_addr);
5265 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.src);
5267 HeapFree(GetProcessHeap(), 0, rec_ins);
5271 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction *ins) {
5272 SHADER_HANDLER hw_fct;
5273 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
5274 struct wined3d_shader *shader = ins->ctx->shader;
5275 struct control_frame *control_frame;
5276 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
5277 BOOL bool_const;
5279 if(ins->handler_idx == WINED3DSIH_LOOP || ins->handler_idx == WINED3DSIH_REP)
5281 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
5282 list_add_head(&priv->control_frames, &control_frame->entry);
5284 if(ins->handler_idx == WINED3DSIH_LOOP) control_frame->type = LOOP;
5285 if(ins->handler_idx == WINED3DSIH_REP) control_frame->type = REP;
5287 if(priv->target_version >= NV2)
5289 control_frame->no.loop = priv->num_loops++;
5290 priv->loop_depth++;
5292 else
5294 /* Don't bother recording when we're in a not used if branch */
5295 if(priv->muted)
5297 return;
5300 if(!priv->recording)
5302 list_init(&priv->record);
5303 priv->recording = TRUE;
5304 control_frame->outer_loop = TRUE;
5305 get_loop_control_const(ins, shader, ins->src[0].reg.idx, &control_frame->loop_control);
5306 return; /* Instruction is handled */
5308 /* Record this loop in the outer loop's recording */
5311 else if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
5313 if(priv->target_version >= NV2)
5315 /* Nothing to do. The control frame is popped after the HW instr handler */
5317 else
5319 struct list *e = list_head(&priv->control_frames);
5320 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5321 list_remove(&control_frame->entry);
5323 if(control_frame->outer_loop)
5325 unsigned int iteration;
5326 int aL = 0;
5327 struct list copy;
5329 /* Turn off recording before playback */
5330 priv->recording = FALSE;
5332 /* Move the recorded instructions to a separate list and get them out of the private data
5333 * structure. If there are nested loops, the shader_arb_handle_instruction below will
5334 * be recorded again, thus priv->record might be overwritten
5336 list_init(&copy);
5337 list_move_tail(&copy, &priv->record);
5338 list_init(&priv->record);
5340 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
5342 shader_addline(buffer, "#unrolling loop: %u iterations, aL=%u, inc %d\n",
5343 control_frame->loop_control.count, control_frame->loop_control.start,
5344 control_frame->loop_control.step);
5345 aL = control_frame->loop_control.start;
5347 else
5349 shader_addline(buffer, "#unrolling rep: %u iterations\n", control_frame->loop_control.count);
5352 for (iteration = 0; iteration < control_frame->loop_control.count; ++iteration)
5354 struct recorded_instruction *rec_ins;
5355 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
5357 priv->aL = aL;
5358 shader_addline(buffer, "#Iteration %u, aL=%d\n", iteration, aL);
5360 else
5362 shader_addline(buffer, "#Iteration %u\n", iteration);
5365 LIST_FOR_EACH_ENTRY(rec_ins, &copy, struct recorded_instruction, entry)
5367 shader_arb_handle_instruction(&rec_ins->ins);
5370 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
5372 aL += control_frame->loop_control.step;
5375 shader_addline(buffer, "#end loop/rep\n");
5377 free_recorded_instruction(&copy);
5378 HeapFree(GetProcessHeap(), 0, control_frame);
5379 return; /* Instruction is handled */
5381 else
5383 /* This is a nested loop. Proceed to the normal recording function */
5384 HeapFree(GetProcessHeap(), 0, control_frame);
5389 if(priv->recording)
5391 record_instruction(&priv->record, ins);
5392 return;
5395 /* boolean if */
5396 if(ins->handler_idx == WINED3DSIH_IF)
5398 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
5399 list_add_head(&priv->control_frames, &control_frame->entry);
5400 control_frame->type = IF;
5402 bool_const = get_bool_const(ins, shader, ins->src[0].reg.idx);
5403 if(ins->src[0].modifiers == WINED3DSPSM_NOT) bool_const = !bool_const;
5404 if (!priv->muted && !bool_const)
5406 shader_addline(buffer, "#if(FALSE){\n");
5407 priv->muted = TRUE;
5408 control_frame->muting = TRUE;
5410 else shader_addline(buffer, "#if(TRUE) {\n");
5412 return; /* Instruction is handled */
5414 else if(ins->handler_idx == WINED3DSIH_IFC)
5416 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
5417 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
5418 control_frame->type = IFC;
5419 control_frame->no.ifc = priv->num_ifcs++;
5420 list_add_head(&priv->control_frames, &control_frame->entry);
5422 else if(ins->handler_idx == WINED3DSIH_ELSE)
5424 struct list *e = list_head(&priv->control_frames);
5425 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5427 if(control_frame->type == IF)
5429 shader_addline(buffer, "#} else {\n");
5430 if(!priv->muted && !control_frame->muting)
5432 priv->muted = TRUE;
5433 control_frame->muting = TRUE;
5435 else if(control_frame->muting) priv->muted = FALSE;
5436 return; /* Instruction is handled. */
5438 /* In case of an ifc, generate a HW shader instruction */
5440 else if(ins->handler_idx == WINED3DSIH_ENDIF)
5442 struct list *e = list_head(&priv->control_frames);
5443 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5445 if(control_frame->type == IF)
5447 shader_addline(buffer, "#} endif\n");
5448 if(control_frame->muting) priv->muted = FALSE;
5449 list_remove(&control_frame->entry);
5450 HeapFree(GetProcessHeap(), 0, control_frame);
5451 return; /* Instruction is handled */
5455 if(priv->muted) return;
5457 /* Select handler */
5458 hw_fct = shader_arb_instruction_handler_table[ins->handler_idx];
5460 /* Unhandled opcode */
5461 if (!hw_fct)
5463 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5464 return;
5466 hw_fct(ins);
5468 if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
5470 struct list *e = list_head(&priv->control_frames);
5471 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5472 list_remove(&control_frame->entry);
5473 HeapFree(GetProcessHeap(), 0, control_frame);
5474 priv->loop_depth--;
5476 else if(ins->handler_idx == WINED3DSIH_ENDIF)
5478 /* Non-ifc ENDIFs don't reach that place because of the return in the if block above */
5479 struct list *e = list_head(&priv->control_frames);
5480 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5481 list_remove(&control_frame->entry);
5482 HeapFree(GetProcessHeap(), 0, control_frame);
5486 shader_arb_add_instruction_modifiers(ins);
5489 const struct wined3d_shader_backend_ops arb_program_shader_backend =
5491 shader_arb_handle_instruction,
5492 shader_arb_select,
5493 shader_arb_select_depth_blt,
5494 shader_arb_deselect_depth_blt,
5495 shader_arb_update_float_vertex_constants,
5496 shader_arb_update_float_pixel_constants,
5497 shader_arb_load_constants,
5498 shader_arb_load_np2fixup_constants,
5499 shader_arb_destroy,
5500 shader_arb_alloc,
5501 shader_arb_free,
5502 shader_arb_dirty_const,
5503 shader_arb_get_caps,
5504 shader_arb_color_fixup_supported,
5507 /* ARB_fragment_program fixed function pipeline replacement definitions */
5508 #define ARB_FFP_CONST_TFACTOR 0
5509 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
5510 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
5511 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
5512 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
5514 struct arbfp_ffp_desc
5516 struct ffp_frag_desc parent;
5517 GLuint shader;
5518 unsigned int num_textures_used;
5521 /* Context activation is done by the caller. */
5522 static void arbfp_enable(BOOL enable)
5524 ENTER_GL();
5525 if(enable) {
5526 glEnable(GL_FRAGMENT_PROGRAM_ARB);
5527 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
5528 } else {
5529 glDisable(GL_FRAGMENT_PROGRAM_ARB);
5530 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
5532 LEAVE_GL();
5535 static HRESULT arbfp_alloc(struct wined3d_device *device)
5537 struct shader_arb_priv *priv;
5538 /* Share private data between the shader backend and the pipeline replacement, if both
5539 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
5540 * if no pixel shader is bound or not
5542 if (device->shader_backend == &arb_program_shader_backend)
5544 device->fragment_priv = device->shader_priv;
5546 else
5548 device->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
5549 if (!device->fragment_priv) return E_OUTOFMEMORY;
5551 priv = device->fragment_priv;
5552 if (wine_rb_init(&priv->fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
5554 ERR("Failed to initialize rbtree.\n");
5555 HeapFree(GetProcessHeap(), 0, device->fragment_priv);
5556 return E_OUTOFMEMORY;
5558 priv->use_arbfp_fixed_func = TRUE;
5559 return WINED3D_OK;
5562 /* Context activation is done by the caller. */
5563 static void arbfp_free_ffpshader(struct wine_rb_entry *entry, void *context)
5565 const struct wined3d_gl_info *gl_info = context;
5566 struct arbfp_ffp_desc *entry_arb = WINE_RB_ENTRY_VALUE(entry, struct arbfp_ffp_desc, parent.entry);
5568 ENTER_GL();
5569 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
5570 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
5571 HeapFree(GetProcessHeap(), 0, entry_arb);
5572 LEAVE_GL();
5575 /* Context activation is done by the caller. */
5576 static void arbfp_free(struct wined3d_device *device)
5578 struct shader_arb_priv *priv = device->fragment_priv;
5580 wine_rb_destroy(&priv->fragment_shaders, arbfp_free_ffpshader, &device->adapter->gl_info);
5581 priv->use_arbfp_fixed_func = FALSE;
5583 if (device->shader_backend != &arb_program_shader_backend)
5585 HeapFree(GetProcessHeap(), 0, device->fragment_priv);
5589 static void arbfp_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
5591 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
5592 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
5593 WINED3DTEXOPCAPS_SELECTARG1 |
5594 WINED3DTEXOPCAPS_SELECTARG2 |
5595 WINED3DTEXOPCAPS_MODULATE4X |
5596 WINED3DTEXOPCAPS_MODULATE2X |
5597 WINED3DTEXOPCAPS_MODULATE |
5598 WINED3DTEXOPCAPS_ADDSIGNED2X |
5599 WINED3DTEXOPCAPS_ADDSIGNED |
5600 WINED3DTEXOPCAPS_ADD |
5601 WINED3DTEXOPCAPS_SUBTRACT |
5602 WINED3DTEXOPCAPS_ADDSMOOTH |
5603 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
5604 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
5605 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
5606 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
5607 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
5608 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
5609 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5610 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
5611 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
5612 WINED3DTEXOPCAPS_DOTPRODUCT3 |
5613 WINED3DTEXOPCAPS_MULTIPLYADD |
5614 WINED3DTEXOPCAPS_LERP |
5615 WINED3DTEXOPCAPS_BUMPENVMAP |
5616 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
5618 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
5620 caps->MaxTextureBlendStages = 8;
5621 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
5624 static void state_texfactor_arbfp(DWORD state_id,
5625 struct wined3d_stateblock *stateblock, struct wined3d_context *context)
5627 const struct wined3d_gl_info *gl_info = context->gl_info;
5628 const struct wined3d_state *state = &stateblock->state;
5629 struct wined3d_device *device = stateblock->device;
5630 float col[4];
5632 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5633 * application provided constants
5635 if (device->shader_backend == &arb_program_shader_backend)
5637 if (use_ps(state)) return;
5639 context->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
5640 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
5643 D3DCOLORTOGLFLOAT4(state->render_states[WINED3DRS_TEXTUREFACTOR], col);
5644 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
5645 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
5649 static void state_arb_specularenable(DWORD state_id,
5650 struct wined3d_stateblock *stateblock, struct wined3d_context *context)
5652 const struct wined3d_gl_info *gl_info = context->gl_info;
5653 const struct wined3d_state *state = &stateblock->state;
5654 struct wined3d_device *device = stateblock->device;
5655 float col[4];
5657 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5658 * application provided constants
5660 if (device->shader_backend == &arb_program_shader_backend)
5662 if (use_ps(state)) return;
5664 context->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
5665 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
5668 if (state->render_states[WINED3DRS_SPECULARENABLE])
5670 /* The specular color has no alpha */
5671 col[0] = 1.0f; col[1] = 1.0f;
5672 col[2] = 1.0f; col[3] = 0.0f;
5673 } else {
5674 col[0] = 0.0f; col[1] = 0.0f;
5675 col[2] = 0.0f; col[3] = 0.0f;
5677 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
5678 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
5681 static void set_bumpmat_arbfp(DWORD state_id, struct wined3d_stateblock *stateblock, struct wined3d_context *context)
5683 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5684 const struct wined3d_gl_info *gl_info = context->gl_info;
5685 const struct wined3d_state *state = &stateblock->state;
5686 struct wined3d_device *device = stateblock->device;
5687 float mat[2][2];
5689 if (use_ps(state))
5691 if (stage && (state->pixel_shader->reg_maps.bumpmat & (1 << stage)))
5693 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
5694 * anyway
5696 if (!isStateDirty(context, STATE_PIXELSHADERCONSTANT))
5697 stateblock_apply_state(STATE_PIXELSHADERCONSTANT, stateblock, context);
5700 if(device->shader_backend == &arb_program_shader_backend) {
5701 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5702 return;
5704 } else if(device->shader_backend == &arb_program_shader_backend) {
5705 context->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
5706 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
5709 mat[0][0] = *((float *)&state->texture_states[stage][WINED3DTSS_BUMPENVMAT00]);
5710 mat[0][1] = *((float *)&state->texture_states[stage][WINED3DTSS_BUMPENVMAT01]);
5711 mat[1][0] = *((float *)&state->texture_states[stage][WINED3DTSS_BUMPENVMAT10]);
5712 mat[1][1] = *((float *)&state->texture_states[stage][WINED3DTSS_BUMPENVMAT11]);
5714 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
5715 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
5718 static void tex_bumpenvlum_arbfp(DWORD state_id,
5719 struct wined3d_stateblock *stateblock, struct wined3d_context *context)
5721 DWORD stage = (state_id - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5722 const struct wined3d_gl_info *gl_info = context->gl_info;
5723 const struct wined3d_state *state = &stateblock->state;
5724 struct wined3d_device *device = stateblock->device;
5725 float param[4];
5727 if (use_ps(state))
5729 if (stage && (state->pixel_shader->reg_maps.luminanceparams & (1 << stage)))
5731 /* The pixel shader has to know the luminance offset. Do a constants update if it
5732 * isn't scheduled anyway
5734 if (!isStateDirty(context, STATE_PIXELSHADERCONSTANT))
5735 stateblock_apply_state(STATE_PIXELSHADERCONSTANT, stateblock, context);
5738 if(device->shader_backend == &arb_program_shader_backend) {
5739 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5740 return;
5742 } else if(device->shader_backend == &arb_program_shader_backend) {
5743 context->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
5744 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
5747 param[0] = *((float *)&state->texture_states[stage][WINED3DTSS_BUMPENVLSCALE]);
5748 param[1] = *((float *)&state->texture_states[stage][WINED3DTSS_BUMPENVLOFFSET]);
5749 param[2] = 0.0f;
5750 param[3] = 0.0f;
5752 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
5753 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
5756 static const char *get_argreg(struct wined3d_shader_buffer *buffer, DWORD argnum, unsigned int stage, DWORD arg)
5758 const char *ret;
5760 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
5762 switch(arg & WINED3DTA_SELECTMASK) {
5763 case WINED3DTA_DIFFUSE:
5764 ret = "fragment.color.primary"; break;
5766 case WINED3DTA_CURRENT:
5767 if (!stage) ret = "fragment.color.primary";
5768 else ret = "ret";
5769 break;
5771 case WINED3DTA_TEXTURE:
5772 switch(stage) {
5773 case 0: ret = "tex0"; break;
5774 case 1: ret = "tex1"; break;
5775 case 2: ret = "tex2"; break;
5776 case 3: ret = "tex3"; break;
5777 case 4: ret = "tex4"; break;
5778 case 5: ret = "tex5"; break;
5779 case 6: ret = "tex6"; break;
5780 case 7: ret = "tex7"; break;
5781 default: ret = "unknown texture";
5783 break;
5785 case WINED3DTA_TFACTOR:
5786 ret = "tfactor"; break;
5788 case WINED3DTA_SPECULAR:
5789 ret = "fragment.color.secondary"; break;
5791 case WINED3DTA_TEMP:
5792 ret = "tempreg"; break;
5794 case WINED3DTA_CONSTANT:
5795 FIXME("Implement perstage constants\n");
5796 switch(stage) {
5797 case 0: ret = "const0"; break;
5798 case 1: ret = "const1"; break;
5799 case 2: ret = "const2"; break;
5800 case 3: ret = "const3"; break;
5801 case 4: ret = "const4"; break;
5802 case 5: ret = "const5"; break;
5803 case 6: ret = "const6"; break;
5804 case 7: ret = "const7"; break;
5805 default: ret = "unknown constant";
5807 break;
5809 default:
5810 return "unknown";
5813 if(arg & WINED3DTA_COMPLEMENT) {
5814 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
5815 if(argnum == 0) ret = "arg0";
5816 if(argnum == 1) ret = "arg1";
5817 if(argnum == 2) ret = "arg2";
5819 if(arg & WINED3DTA_ALPHAREPLICATE) {
5820 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
5821 if(argnum == 0) ret = "arg0";
5822 if(argnum == 1) ret = "arg1";
5823 if(argnum == 2) ret = "arg2";
5825 return ret;
5828 static void gen_ffp_instr(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5829 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5831 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5832 unsigned int mul = 1;
5833 BOOL mul_final_dest = FALSE;
5835 if(color && alpha) dstmask = "";
5836 else if(color) dstmask = ".xyz";
5837 else dstmask = ".w";
5839 if(dst == tempreg) dstreg = "tempreg";
5840 else dstreg = "ret";
5842 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
5843 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
5844 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
5846 switch(op) {
5847 case WINED3DTOP_DISABLE:
5848 if (!stage) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
5849 break;
5851 case WINED3DTOP_SELECTARG2:
5852 arg1 = arg2;
5853 /* FALLTHROUGH */
5854 case WINED3DTOP_SELECTARG1:
5855 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
5856 break;
5858 case WINED3DTOP_MODULATE4X:
5859 mul = 2;
5860 /* FALLTHROUGH */
5861 case WINED3DTOP_MODULATE2X:
5862 mul *= 2;
5863 if (!strcmp(dstreg, "result.color"))
5865 dstreg = "ret";
5866 mul_final_dest = TRUE;
5868 /* FALLTHROUGH */
5869 case WINED3DTOP_MODULATE:
5870 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5871 break;
5873 case WINED3DTOP_ADDSIGNED2X:
5874 mul = 2;
5875 if (!strcmp(dstreg, "result.color"))
5877 dstreg = "ret";
5878 mul_final_dest = TRUE;
5880 /* FALLTHROUGH */
5881 case WINED3DTOP_ADDSIGNED:
5882 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
5883 arg2 = "arg2";
5884 /* FALLTHROUGH */
5885 case WINED3DTOP_ADD:
5886 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5887 break;
5889 case WINED3DTOP_SUBTRACT:
5890 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5891 break;
5893 case WINED3DTOP_ADDSMOOTH:
5894 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
5895 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
5896 break;
5898 case WINED3DTOP_BLENDCURRENTALPHA:
5899 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
5900 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5901 break;
5902 case WINED3DTOP_BLENDFACTORALPHA:
5903 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
5904 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5905 break;
5906 case WINED3DTOP_BLENDTEXTUREALPHA:
5907 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
5908 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5909 break;
5910 case WINED3DTOP_BLENDDIFFUSEALPHA:
5911 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5912 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5913 break;
5915 case WINED3DTOP_BLENDTEXTUREALPHAPM:
5916 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
5917 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
5918 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
5919 break;
5921 /* D3DTOP_PREMODULATE ???? */
5923 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
5924 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
5925 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
5926 break;
5927 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
5928 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
5929 break;
5930 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
5931 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
5932 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
5933 break;
5934 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
5935 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
5936 break;
5938 case WINED3DTOP_DOTPRODUCT3:
5939 mul = 4;
5940 if (!strcmp(dstreg, "result.color"))
5942 dstreg = "ret";
5943 mul_final_dest = TRUE;
5945 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
5946 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
5947 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
5948 break;
5950 case WINED3DTOP_MULTIPLYADD:
5951 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
5952 break;
5954 case WINED3DTOP_LERP:
5955 /* The msdn is not quite right here */
5956 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5957 break;
5959 case WINED3DTOP_BUMPENVMAP:
5960 case WINED3DTOP_BUMPENVMAPLUMINANCE:
5961 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
5962 break;
5964 default:
5965 FIXME("Unhandled texture op %08x\n", op);
5968 if(mul == 2) {
5969 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
5970 } else if(mul == 4) {
5971 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
5975 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, struct wined3d_stateblock *stateblock)
5977 const struct wined3d_gl_info *gl_info = &stateblock->device->adapter->gl_info;
5978 unsigned int stage;
5979 struct wined3d_shader_buffer buffer;
5980 BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5981 BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5982 BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5983 const char *textype;
5984 const char *instr, *sat;
5985 char colorcor_dst[8];
5986 GLuint ret;
5987 DWORD arg0, arg1, arg2;
5988 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5989 BOOL op_equal;
5990 const char *final_combiner_src = "ret";
5991 GLint pos;
5993 /* Find out which textures are read */
5994 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5995 if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
5996 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5997 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5998 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5999 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
6000 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
6001 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
6003 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
6004 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
6005 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
6006 bump_used[stage] = TRUE;
6007 tex_read[stage] = TRUE;
6009 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
6010 bump_used[stage] = TRUE;
6011 tex_read[stage] = TRUE;
6012 luminance_used[stage] = TRUE;
6013 } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
6014 tfactor_used = TRUE;
6017 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
6018 tfactor_used = TRUE;
6021 if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
6022 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
6023 tempreg_used = TRUE;
6026 if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
6027 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6028 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6029 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6030 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
6031 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
6032 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
6034 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
6035 tempreg_used = TRUE;
6037 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
6038 tfactor_used = TRUE;
6042 /* Shader header */
6043 if (!shader_buffer_init(&buffer))
6045 ERR("Failed to initialize shader buffer.\n");
6046 return 0;
6049 shader_addline(&buffer, "!!ARBfp1.0\n");
6051 switch(settings->fog) {
6052 case FOG_OFF: break;
6053 case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
6054 case FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
6055 case FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
6056 default: FIXME("Unexpected fog setting %d\n", settings->fog);
6059 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
6060 shader_addline(&buffer, "TEMP TMP;\n");
6061 shader_addline(&buffer, "TEMP ret;\n");
6062 if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
6063 shader_addline(&buffer, "TEMP arg0;\n");
6064 shader_addline(&buffer, "TEMP arg1;\n");
6065 shader_addline(&buffer, "TEMP arg2;\n");
6066 for(stage = 0; stage < MAX_TEXTURES; stage++) {
6067 if(!tex_read[stage]) continue;
6068 shader_addline(&buffer, "TEMP tex%u;\n", stage);
6069 if(!bump_used[stage]) continue;
6070 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
6071 if(!luminance_used[stage]) continue;
6072 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
6074 if(tfactor_used) {
6075 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
6077 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
6079 if(settings->sRGB_write) {
6080 shader_addline(&buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
6081 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
6082 shader_addline(&buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
6083 srgb_sub_high, 0.0, 0.0, 0.0);
6086 if (ffp_clip_emul(&stateblock->state) && settings->emul_clipplanes)
6087 shader_addline(&buffer, "KIL fragment.texcoord[7];\n");
6089 /* Generate texture sampling instructions) */
6090 for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
6091 if(!tex_read[stage]) continue;
6093 switch(settings->op[stage].tex_type) {
6094 case tex_1d: textype = "1D"; break;
6095 case tex_2d: textype = "2D"; break;
6096 case tex_3d: textype = "3D"; break;
6097 case tex_cube: textype = "CUBE"; break;
6098 case tex_rect: textype = "RECT"; break;
6099 default: textype = "unexpected_textype"; break;
6102 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
6103 settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
6104 sat = "";
6105 } else {
6106 sat = "_SAT";
6109 if(settings->op[stage].projected == proj_none) {
6110 instr = "TEX";
6111 } else if(settings->op[stage].projected == proj_count4 ||
6112 settings->op[stage].projected == proj_count3) {
6113 instr = "TXP";
6114 } else {
6115 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6116 instr = "TXP";
6119 if(stage > 0 &&
6120 (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
6121 settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
6122 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
6123 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
6124 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
6125 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
6127 /* with projective textures, texbem only divides the static texture coord, not the displacement,
6128 * so multiply the displacement with the dividing parameter before passing it to TXP
6130 if (settings->op[stage].projected != proj_none) {
6131 if(settings->op[stage].projected == proj_count4) {
6132 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
6133 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
6134 } else {
6135 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
6136 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
6138 } else {
6139 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
6142 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
6143 instr, sat, stage, stage, textype);
6144 if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
6145 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
6146 stage - 1, stage - 1, stage - 1);
6147 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
6149 } else if(settings->op[stage].projected == proj_count3) {
6150 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
6151 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
6152 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
6153 instr, sat, stage, stage, textype);
6154 } else {
6155 shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
6156 instr, sat, stage, stage, stage, textype);
6159 sprintf(colorcor_dst, "tex%u", stage);
6160 gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
6161 settings->op[stage].color_fixup);
6164 /* Generate the main shader */
6165 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6167 if (settings->op[stage].cop == WINED3DTOP_DISABLE)
6169 if (!stage) final_combiner_src = "fragment.color.primary";
6170 break;
6173 if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
6174 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
6175 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6176 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
6177 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
6178 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6179 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
6180 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
6181 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6182 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
6183 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
6184 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6185 } else {
6186 op_equal = settings->op[stage].aop == settings->op[stage].cop &&
6187 settings->op[stage].carg0 == settings->op[stage].aarg0 &&
6188 settings->op[stage].carg1 == settings->op[stage].aarg1 &&
6189 settings->op[stage].carg2 == settings->op[stage].aarg2;
6192 if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
6193 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6194 settings->op[stage].cop, settings->op[stage].carg0,
6195 settings->op[stage].carg1, settings->op[stage].carg2);
6196 if (!stage)
6197 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
6199 else if (op_equal)
6201 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6202 settings->op[stage].cop, settings->op[stage].carg0,
6203 settings->op[stage].carg1, settings->op[stage].carg2);
6204 } else {
6205 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6206 settings->op[stage].cop, settings->op[stage].carg0,
6207 settings->op[stage].carg1, settings->op[stage].carg2);
6208 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6209 settings->op[stage].aop, settings->op[stage].aarg0,
6210 settings->op[stage].aarg1, settings->op[stage].aarg2);
6214 if(settings->sRGB_write) {
6215 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
6216 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE);
6217 shader_addline(&buffer, "MOV result.color, ret;\n");
6218 } else {
6219 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
6222 /* Footer */
6223 shader_addline(&buffer, "END\n");
6225 /* Generate the shader */
6226 GL_EXTCALL(glGenProgramsARB(1, &ret));
6227 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
6228 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
6229 strlen(buffer.buffer), buffer.buffer));
6230 checkGLcall("glProgramStringARB()");
6232 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
6233 if (pos != -1)
6235 FIXME("Fragment program error at position %d: %s\n\n", pos,
6236 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
6237 shader_arb_dump_program_source(buffer.buffer);
6239 else
6241 GLint native;
6243 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
6244 checkGLcall("glGetProgramivARB()");
6245 if (!native) WARN("Program exceeds native resource limits.\n");
6248 shader_buffer_free(&buffer);
6249 return ret;
6252 static void fragment_prog_arbfp(DWORD state_id, struct wined3d_stateblock *stateblock, struct wined3d_context *context)
6254 const struct wined3d_gl_info *gl_info = context->gl_info;
6255 const struct wined3d_state *state = &stateblock->state;
6256 struct wined3d_device *device = stateblock->device;
6257 struct shader_arb_priv *priv = device->fragment_priv;
6258 BOOL use_vshader = use_vs(state);
6259 BOOL use_pshader = use_ps(state);
6260 struct ffp_frag_settings settings;
6261 const struct arbfp_ffp_desc *desc;
6262 unsigned int i;
6264 TRACE("state_id %#x, stateblock %p, context %p\n", state_id, stateblock, context);
6266 if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
6267 if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
6268 /* Reload fixed function constants since they collide with the pixel shader constants */
6269 for(i = 0; i < MAX_TEXTURES; i++) {
6270 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
6272 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
6273 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
6274 } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
6275 device->shader_backend->shader_select(context, use_pshader, use_vshader);
6277 return;
6280 if(!use_pshader) {
6281 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
6282 gen_ffp_frag_op(stateblock, &settings, FALSE);
6283 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
6284 if(!desc) {
6285 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
6286 if (!new_desc)
6288 ERR("Out of memory\n");
6289 return;
6291 new_desc->num_textures_used = 0;
6292 for (i = 0; i < gl_info->limits.texture_stages; ++i)
6294 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
6295 new_desc->num_textures_used = i;
6298 memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
6299 new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
6300 add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
6301 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
6302 desc = new_desc;
6305 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
6306 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
6307 * deactivate it.
6309 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
6310 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
6311 priv->current_fprogram_id = desc->shader;
6313 if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
6314 /* Reload fixed function constants since they collide with the pixel shader constants */
6315 for(i = 0; i < MAX_TEXTURES; i++) {
6316 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
6318 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
6319 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
6321 context->last_was_pshader = FALSE;
6322 } else {
6323 context->last_was_pshader = TRUE;
6326 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
6327 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
6328 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
6329 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
6330 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
6332 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
6333 * shader handler
6335 if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
6336 device->shader_backend->shader_select(context, use_pshader, use_vshader);
6338 if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader))
6339 stateblock_apply_state(STATE_VERTEXSHADERCONSTANT, stateblock, context);
6341 if (use_pshader) stateblock_apply_state(STATE_PIXELSHADERCONSTANT, stateblock, context);
6344 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
6345 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
6346 * state table, so we need to handle that with a forwarding function. The other invisible side effect
6347 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
6348 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
6350 static void state_arbfp_fog(DWORD state_id, struct wined3d_stateblock *stateblock, struct wined3d_context *context)
6352 const struct wined3d_state *state = &stateblock->state;
6353 enum fogsource new_source;
6355 TRACE("state_id %#x, stateblock %p, context %p\n", state_id, stateblock, context);
6357 if(!isStateDirty(context, STATE_PIXELSHADER)) {
6358 fragment_prog_arbfp(state_id, stateblock, context);
6361 if (!state->render_states[WINED3DRS_FOGENABLE]) return;
6363 if (state->render_states[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
6365 if (use_vs(state))
6367 new_source = FOGSOURCE_VS;
6369 else
6371 if (state->render_states[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || context->last_was_rhw)
6372 new_source = FOGSOURCE_COORD;
6373 else
6374 new_source = FOGSOURCE_FFP;
6376 } else {
6377 new_source = FOGSOURCE_FFP;
6379 if(new_source != context->fog_source) {
6380 context->fog_source = new_source;
6381 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART), stateblock, context);
6385 static void textransform(DWORD state, struct wined3d_stateblock *stateblock, struct wined3d_context *context)
6387 if (!isStateDirty(context, STATE_PIXELSHADER))
6388 fragment_prog_arbfp(state, stateblock, context);
6391 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
6392 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR), state_texfactor_arbfp }, WINED3D_GL_EXT_NONE },
6393 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6394 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6395 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6396 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6397 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6398 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6399 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6400 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6401 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6402 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6403 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6404 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6405 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6406 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6407 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6408 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6409 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6410 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6411 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6412 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6413 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6414 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6415 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6416 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6417 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6418 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6419 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6420 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6421 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6422 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6423 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6424 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6425 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6426 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6427 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6428 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6429 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6430 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6431 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6432 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6433 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6434 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6435 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6436 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6437 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6438 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6439 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6440 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6441 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6442 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6443 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6444 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6445 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6446 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6447 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6448 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6449 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6450 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6451 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6452 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6453 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6454 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6455 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6456 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6457 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6458 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6459 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6460 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6461 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6462 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6463 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6464 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6465 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6466 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6467 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6468 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6469 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6470 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6471 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6472 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6473 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6474 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6475 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6476 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6477 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6478 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6479 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6480 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6481 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6482 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6483 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6484 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6485 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6486 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6487 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6488 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6489 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6490 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6491 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6492 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6493 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6494 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6495 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6496 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6497 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6498 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6499 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6500 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6501 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6502 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6503 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6504 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6505 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6506 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6507 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
6508 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6509 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6510 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), NULL }, WINED3D_GL_EXT_NONE },
6511 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
6512 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), NULL }, WINED3D_GL_EXT_NONE },
6513 {STATE_PIXELSHADER, { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6514 {STATE_RENDER(WINED3DRS_FOGENABLE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
6515 {STATE_RENDER(WINED3DRS_FOGTABLEMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6516 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6517 {STATE_RENDER(WINED3DRS_FOGSTART), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6518 {STATE_RENDER(WINED3DRS_FOGEND), { STATE_RENDER(WINED3DRS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
6519 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE), { STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6520 {STATE_RENDER(WINED3DRS_FOGCOLOR), { STATE_RENDER(WINED3DRS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
6521 {STATE_RENDER(WINED3DRS_FOGDENSITY), { STATE_RENDER(WINED3DRS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
6522 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6523 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6524 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6525 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6526 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6527 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6528 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6529 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6530 {STATE_RENDER(WINED3DRS_SPECULARENABLE), { STATE_RENDER(WINED3DRS_SPECULARENABLE), state_arb_specularenable}, WINED3D_GL_EXT_NONE },
6531 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE },
6534 const struct fragment_pipeline arbfp_fragment_pipeline = {
6535 arbfp_enable,
6536 arbfp_get_caps,
6537 arbfp_alloc,
6538 arbfp_free,
6539 shader_arb_color_fixup_supported,
6540 arbfp_fragmentstate_template,
6541 TRUE /* We can disable projected textures */
6544 struct arbfp_blit_priv {
6545 GLenum yuy2_rect_shader, yuy2_2d_shader;
6546 GLenum uyvy_rect_shader, uyvy_2d_shader;
6547 GLenum yv12_rect_shader, yv12_2d_shader;
6548 GLenum p8_rect_shader, p8_2d_shader;
6549 GLuint palette_texture;
6552 static HRESULT arbfp_blit_alloc(struct wined3d_device *device)
6554 device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
6555 if(!device->blit_priv) {
6556 ERR("Out of memory\n");
6557 return E_OUTOFMEMORY;
6559 return WINED3D_OK;
6562 /* Context activation is done by the caller. */
6563 static void arbfp_blit_free(struct wined3d_device *device)
6565 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6566 struct arbfp_blit_priv *priv = device->blit_priv;
6568 ENTER_GL();
6569 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
6570 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
6571 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
6572 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
6573 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
6574 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
6575 GL_EXTCALL(glDeleteProgramsARB(1, &priv->p8_rect_shader));
6576 GL_EXTCALL(glDeleteProgramsARB(1, &priv->p8_2d_shader));
6577 checkGLcall("Delete yuv and p8 programs");
6579 if(priv->palette_texture) glDeleteTextures(1, &priv->palette_texture);
6580 LEAVE_GL();
6582 HeapFree(GetProcessHeap(), 0, device->blit_priv);
6583 device->blit_priv = NULL;
6586 static BOOL gen_planar_yuv_read(struct wined3d_shader_buffer *buffer, enum complex_fixup fixup,
6587 GLenum textype, char *luminance)
6589 char chroma;
6590 const char *tex, *texinstr;
6592 if (fixup == COMPLEX_FIXUP_UYVY) {
6593 chroma = 'x';
6594 *luminance = 'w';
6595 } else {
6596 chroma = 'w';
6597 *luminance = 'x';
6599 switch(textype) {
6600 case GL_TEXTURE_2D: tex = "2D"; texinstr = "TXP"; break;
6601 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; texinstr = "TEX"; break;
6602 default:
6603 /* This is more tricky than just replacing the texture type - we have to navigate
6604 * properly in the texture to find the correct chroma values
6606 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
6607 return FALSE;
6610 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
6611 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
6612 * filtering when we sample the texture.
6614 * These are the rules for reading the chroma:
6616 * Even pixel: Cr
6617 * Even pixel: U
6618 * Odd pixel: V
6620 * So we have to get the sampling x position in non-normalized coordinates in integers
6622 if(textype != GL_TEXTURE_RECTANGLE_ARB) {
6623 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
6624 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
6625 } else {
6626 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6628 /* We must not allow filtering between pixel x and x+1, this would mix U and V
6629 * Vertical filtering is ok. However, bear in mind that the pixel center is at
6630 * 0.5, so add 0.5.
6632 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
6633 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
6635 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
6636 * even and odd pixels respectively
6638 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
6639 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
6641 /* Sample Pixel 1 */
6642 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
6644 /* Put the value into either of the chroma values */
6645 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
6646 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
6647 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
6648 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
6650 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
6651 * the pixel right to the current one. Otherwise, sample the left pixel.
6652 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
6654 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
6655 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
6656 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
6658 /* Put the value into the other chroma */
6659 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
6660 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
6661 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
6662 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
6664 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
6665 * the current one and lerp the two U and V values
6668 /* This gives the correctly filtered luminance value */
6669 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
6671 return TRUE;
6674 static BOOL gen_yv12_read(struct wined3d_shader_buffer *buffer, GLenum textype, char *luminance)
6676 const char *tex;
6678 switch(textype) {
6679 case GL_TEXTURE_2D: tex = "2D"; break;
6680 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; break;
6681 default:
6682 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
6683 return FALSE;
6686 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
6687 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
6688 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
6689 * pitch of the luminance plane, the packing into the gl texture is a bit
6690 * unfortunate. If the whole texture is interpreted as luminance data it looks
6691 * approximately like this:
6693 * +----------------------------------+----
6694 * | |
6695 * | |
6696 * | |
6697 * | |
6698 * | | 2
6699 * | LUMINANCE | -
6700 * | | 3
6701 * | |
6702 * | |
6703 * | |
6704 * | |
6705 * +----------------+-----------------+----
6706 * | | |
6707 * | U even rows | U odd rows |
6708 * | | | 1
6709 * +----------------+------------------ -
6710 * | | | 3
6711 * | V even rows | V odd rows |
6712 * | | |
6713 * +----------------+-----------------+----
6714 * | | |
6715 * | 0.5 | 0.5 |
6717 * So it appears as if there are 4 chroma images, but in fact the odd rows
6718 * in the chroma images are in the same row as the even ones. So its is
6719 * kinda tricky to read
6721 * When reading from rectangle textures, keep in mind that the input y coordinates
6722 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
6724 shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
6725 2.0f / 3.0f, 1.0f / 6.0f, (2.0f / 3.0f) + (1.0f / 6.0f), 1.0f / 3.0f);
6727 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6728 /* the chroma planes have only half the width */
6729 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
6731 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
6732 * the coordinate. Also read the right side of the image when reading odd lines
6734 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
6735 * bleeding
6737 if(textype == GL_TEXTURE_2D) {
6739 shader_addline(buffer, "RCP chroma.w, size.y;\n");
6741 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
6743 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
6744 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
6746 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6747 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6748 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
6749 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6750 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6752 /* clamp, keep the half pixel origin in mind */
6753 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
6754 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
6755 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
6756 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6757 } else {
6758 /* Read from [size - size+size/4] */
6759 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
6760 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
6762 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6763 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6764 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
6765 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6766 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
6767 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6769 /* Make sure to read exactly from the pixel center */
6770 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
6771 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
6773 /* Clamp */
6774 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
6775 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
6776 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6777 shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
6778 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
6780 /* Read the texture, put the result into the output register */
6781 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
6782 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
6784 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
6785 * No need to clamp because we're just reusing the already clamped value from above
6787 if(textype == GL_TEXTURE_2D) {
6788 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
6789 } else {
6790 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
6792 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
6793 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
6795 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
6796 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
6797 * values due to filtering
6799 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6800 if(textype == GL_TEXTURE_2D) {
6801 /* Multiply the y coordinate by 2/3 and clamp it */
6802 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
6803 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
6804 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6805 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
6806 } else {
6807 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
6808 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
6809 * is bigger
6811 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
6812 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
6813 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
6815 *luminance = 'a';
6817 return TRUE;
6820 static GLuint gen_p8_shader(struct arbfp_blit_priv *priv,
6821 const struct wined3d_gl_info *gl_info, GLenum textype)
6823 GLenum shader;
6824 struct wined3d_shader_buffer buffer;
6825 GLint pos;
6827 /* Shader header */
6828 if (!shader_buffer_init(&buffer))
6830 ERR("Failed to initialize shader buffer.\n");
6831 return 0;
6834 ENTER_GL();
6835 GL_EXTCALL(glGenProgramsARB(1, &shader));
6836 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
6837 LEAVE_GL();
6838 if(!shader) {
6839 shader_buffer_free(&buffer);
6840 return 0;
6843 shader_addline(&buffer, "!!ARBfp1.0\n");
6844 shader_addline(&buffer, "TEMP index;\n");
6846 /* { 255/256, 0.5/255*255/256, 0, 0 } */
6847 shader_addline(&buffer, "PARAM constants = { 0.996, 0.00195, 0, 0 };\n");
6849 /* The alpha-component contains the palette index */
6850 if(textype == GL_TEXTURE_RECTANGLE_ARB)
6851 shader_addline(&buffer, "TXP index, fragment.texcoord[0], texture[0], RECT;\n");
6852 else
6853 shader_addline(&buffer, "TEX index, fragment.texcoord[0], texture[0], 2D;\n");
6855 /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
6856 shader_addline(&buffer, "MAD index.a, index.a, constants.x, constants.y;\n");
6858 /* Use the alpha-component as an index in the palette to get the final color */
6859 shader_addline(&buffer, "TEX result.color, index.a, texture[1], 1D;\n");
6860 shader_addline(&buffer, "END\n");
6862 ENTER_GL();
6863 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
6864 strlen(buffer.buffer), buffer.buffer));
6865 checkGLcall("glProgramStringARB()");
6867 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
6868 if (pos != -1)
6870 FIXME("Fragment program error at position %d: %s\n\n", pos,
6871 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
6872 shader_arb_dump_program_source(buffer.buffer);
6875 if (textype == GL_TEXTURE_RECTANGLE_ARB)
6876 priv->p8_rect_shader = shader;
6877 else
6878 priv->p8_2d_shader = shader;
6880 shader_buffer_free(&buffer);
6881 LEAVE_GL();
6883 return shader;
6886 /* Context activation is done by the caller. */
6887 static void upload_palette(struct wined3d_surface *surface)
6889 BYTE table[256][4];
6890 struct wined3d_device *device = surface->resource.device;
6891 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6892 struct arbfp_blit_priv *priv = device->blit_priv;
6893 BOOL colorkey = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
6895 d3dfmt_p8_init_palette(surface, table, colorkey);
6897 ENTER_GL();
6898 if (!priv->palette_texture)
6899 glGenTextures(1, &priv->palette_texture);
6901 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
6902 glBindTexture(GL_TEXTURE_1D, priv->palette_texture);
6904 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
6906 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
6907 /* Make sure we have discrete color levels. */
6908 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
6909 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
6910 /* Upload the palette */
6911 /* TODO: avoid unneeed uploads in the future by adding some SFLAG_PALETTE_DIRTY mechanism */
6912 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table);
6914 /* Switch back to unit 0 in which the 2D texture will be stored. */
6915 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
6916 LEAVE_GL();
6919 /* Context activation is done by the caller. */
6920 static GLuint gen_yuv_shader(struct arbfp_blit_priv *priv, const struct wined3d_gl_info *gl_info,
6921 enum complex_fixup yuv_fixup, GLenum textype)
6923 GLenum shader;
6924 struct wined3d_shader_buffer buffer;
6925 char luminance_component;
6926 GLint pos;
6928 /* Shader header */
6929 if (!shader_buffer_init(&buffer))
6931 ERR("Failed to initialize shader buffer.\n");
6932 return 0;
6935 ENTER_GL();
6936 GL_EXTCALL(glGenProgramsARB(1, &shader));
6937 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
6938 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
6939 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6940 LEAVE_GL();
6941 if(!shader) {
6942 shader_buffer_free(&buffer);
6943 return 0;
6946 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
6947 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
6948 * two chroma(U and V) values. Each macropixel has two luminance values, one for
6949 * each single pixel it contains, and one U and one V value shared between both
6950 * pixels.
6952 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
6953 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
6954 * take the format into account when generating the read swizzles
6956 * Reading the Y value is straightforward - just sample the texture. The hardware
6957 * takes care of filtering in the horizontal and vertical direction.
6959 * Reading the U and V values is harder. We have to avoid filtering horizontally,
6960 * because that would mix the U and V values of one pixel or two adjacent pixels.
6961 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
6962 * regardless of the filtering setting. Vertical filtering works automatically
6963 * though - the U and V values of two rows are mixed nicely.
6965 * Appart of avoiding filtering issues, the code has to know which value it just
6966 * read, and where it can find the other one. To determine this, it checks if
6967 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
6969 * Handling horizontal filtering of U and V values requires reading a 2nd pair
6970 * of pixels, extracting U and V and mixing them. This is not implemented yet.
6972 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
6973 * with width / 2. This way one read gives all 3 values, finding U and V is easy
6974 * in an unfiltered situation. Finding the luminance on the other hand requires
6975 * finding out if it is an odd or even pixel. The real drawback of this approach
6976 * is filtering. This would have to be emulated completely in the shader, reading
6977 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
6978 * vertically. Beyond that it would require adjustments to the texture handling
6979 * code to deal with the width scaling
6981 shader_addline(&buffer, "!!ARBfp1.0\n");
6982 shader_addline(&buffer, "TEMP luminance;\n");
6983 shader_addline(&buffer, "TEMP temp;\n");
6984 shader_addline(&buffer, "TEMP chroma;\n");
6985 shader_addline(&buffer, "TEMP texcrd;\n");
6986 shader_addline(&buffer, "TEMP texcrd2;\n");
6987 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
6988 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
6989 shader_addline(&buffer, "PARAM size = program.local[0];\n");
6991 switch (yuv_fixup)
6993 case COMPLEX_FIXUP_UYVY:
6994 case COMPLEX_FIXUP_YUY2:
6995 if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
6997 shader_buffer_free(&buffer);
6998 return 0;
7000 break;
7002 case COMPLEX_FIXUP_YV12:
7003 if (!gen_yv12_read(&buffer, textype, &luminance_component))
7005 shader_buffer_free(&buffer);
7006 return 0;
7008 break;
7010 default:
7011 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
7012 shader_buffer_free(&buffer);
7013 return 0;
7016 /* Calculate the final result. Formula is taken from
7017 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
7018 * ranges from -0.5 to 0.5
7020 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
7022 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
7023 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
7024 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
7025 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
7026 shader_addline(&buffer, "END\n");
7028 ENTER_GL();
7029 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
7030 strlen(buffer.buffer), buffer.buffer));
7031 checkGLcall("glProgramStringARB()");
7033 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
7034 if (pos != -1)
7036 FIXME("Fragment program error at position %d: %s\n\n", pos,
7037 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
7038 shader_arb_dump_program_source(buffer.buffer);
7040 else
7042 GLint native;
7044 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &native));
7045 checkGLcall("glGetProgramivARB()");
7046 if (!native) WARN("Program exceeds native resource limits.\n");
7049 shader_buffer_free(&buffer);
7050 LEAVE_GL();
7052 switch (yuv_fixup)
7054 case COMPLEX_FIXUP_YUY2:
7055 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
7056 else priv->yuy2_2d_shader = shader;
7057 break;
7059 case COMPLEX_FIXUP_UYVY:
7060 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
7061 else priv->uyvy_2d_shader = shader;
7062 break;
7064 case COMPLEX_FIXUP_YV12:
7065 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
7066 else priv->yv12_2d_shader = shader;
7067 break;
7068 default:
7069 ERR("Unsupported complex fixup: %d\n", yuv_fixup);
7072 return shader;
7075 /* Context activation is done by the caller. */
7076 static HRESULT arbfp_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, struct wined3d_surface *surface)
7078 GLenum shader;
7079 float size[4] = {(float) surface->pow2Width, (float) surface->pow2Height, 1.0f, 1.0f};
7080 struct arbfp_blit_priv *priv = blit_priv;
7081 enum complex_fixup fixup;
7082 GLenum textype = surface->texture_target;
7084 if (!is_complex_fixup(surface->resource.format->color_fixup))
7086 TRACE("Fixup:\n");
7087 dump_color_fixup_desc(surface->resource.format->color_fixup);
7088 /* Don't bother setting up a shader for unconverted formats */
7089 ENTER_GL();
7090 glEnable(textype);
7091 checkGLcall("glEnable(textype)");
7092 LEAVE_GL();
7093 return WINED3D_OK;
7096 fixup = get_complex_fixup(surface->resource.format->color_fixup);
7098 switch(fixup)
7100 case COMPLEX_FIXUP_YUY2:
7101 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
7102 break;
7104 case COMPLEX_FIXUP_UYVY:
7105 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
7106 break;
7108 case COMPLEX_FIXUP_YV12:
7109 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
7110 break;
7112 case COMPLEX_FIXUP_P8:
7113 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->p8_rect_shader : priv->p8_2d_shader;
7114 if (!shader) shader = gen_p8_shader(priv, gl_info, textype);
7116 upload_palette(surface);
7117 break;
7119 default:
7120 FIXME("Unsupported complex fixup %#x, not setting a shader\n", fixup);
7121 ENTER_GL();
7122 glEnable(textype);
7123 checkGLcall("glEnable(textype)");
7124 LEAVE_GL();
7125 return E_NOTIMPL;
7128 if (!shader) shader = gen_yuv_shader(priv, gl_info, fixup, textype);
7130 ENTER_GL();
7131 glEnable(GL_FRAGMENT_PROGRAM_ARB);
7132 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
7133 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
7134 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
7135 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
7136 checkGLcall("glProgramLocalParameter4fvARB");
7137 LEAVE_GL();
7139 return WINED3D_OK;
7142 /* Context activation is done by the caller. */
7143 static void arbfp_blit_unset(const struct wined3d_gl_info *gl_info)
7145 ENTER_GL();
7146 glDisable(GL_FRAGMENT_PROGRAM_ARB);
7147 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
7148 glDisable(GL_TEXTURE_2D);
7149 checkGLcall("glDisable(GL_TEXTURE_2D)");
7150 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
7152 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
7153 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
7155 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7157 glDisable(GL_TEXTURE_RECTANGLE_ARB);
7158 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
7160 LEAVE_GL();
7163 static BOOL arbfp_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
7164 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
7165 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
7167 enum complex_fixup src_fixup;
7169 if (!gl_info->supported[ARB_FRAGMENT_PROGRAM])
7170 return FALSE;
7172 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT)
7174 TRACE("Unsupported blit_op=%d\n", blit_op);
7175 return FALSE;
7178 src_fixup = get_complex_fixup(src_format->color_fixup);
7179 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7181 TRACE("Checking support for fixup:\n");
7182 dump_color_fixup_desc(src_format->color_fixup);
7185 if (!is_identity_fixup(dst_format->color_fixup))
7187 TRACE("Destination fixups are not supported\n");
7188 return FALSE;
7191 if (is_identity_fixup(src_format->color_fixup))
7193 TRACE("[OK]\n");
7194 return TRUE;
7197 /* We only support YUV conversions. */
7198 if (!is_complex_fixup(src_format->color_fixup))
7200 TRACE("[FAILED]\n");
7201 return FALSE;
7204 switch(src_fixup)
7206 case COMPLEX_FIXUP_YUY2:
7207 case COMPLEX_FIXUP_UYVY:
7208 case COMPLEX_FIXUP_YV12:
7209 case COMPLEX_FIXUP_P8:
7210 TRACE("[OK]\n");
7211 return TRUE;
7213 default:
7214 FIXME("Unsupported YUV fixup %#x\n", src_fixup);
7215 TRACE("[FAILED]\n");
7216 return FALSE;
7220 HRESULT arbfp_blit_surface(struct wined3d_device *device, struct wined3d_surface *src_surface, const RECT *src_rect,
7221 struct wined3d_surface *dst_surface, const RECT *dst_rect_in, enum wined3d_blit_op blit_op, DWORD Filter)
7223 struct wined3d_context *context;
7224 RECT dst_rect = *dst_rect_in;
7226 /* Now load the surface */
7227 surface_internal_preload(src_surface, SRGB_RGB);
7229 /* Activate the destination context, set it up for blitting */
7230 context = context_acquire(device, dst_surface);
7231 context_apply_blit_state(context, device);
7233 if (!surface_is_offscreen(dst_surface))
7234 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
7236 arbfp_blit_set(device->blit_priv, context->gl_info, src_surface);
7238 ENTER_GL();
7240 /* Draw a textured quad */
7241 draw_textured_quad(src_surface, src_rect, &dst_rect, Filter);
7243 LEAVE_GL();
7245 /* Leave the opengl state valid for blitting */
7246 arbfp_blit_unset(context->gl_info);
7248 if (wined3d_settings.strict_draw_ordering
7249 || (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
7250 && (dst_surface->container.u.swapchain->front_buffer == dst_surface)))
7251 wglFlush(); /* Flush to ensure ordering across contexts. */
7253 context_release(context);
7255 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
7256 return WINED3D_OK;
7259 /* Do not call while under the GL lock. */
7260 static HRESULT arbfp_blit_color_fill(struct wined3d_device *device, struct wined3d_surface *dst_surface,
7261 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
7263 FIXME("Color filling not implemented by arbfp_blit\n");
7264 return WINED3DERR_INVALIDCALL;
7267 /* Do not call while under the GL lock. */
7268 static HRESULT arbfp_blit_depth_fill(struct wined3d_device *device,
7269 struct wined3d_surface *surface, const RECT *rect, float depth)
7271 FIXME("Depth filling not implemented by arbfp_blit.\n");
7272 return WINED3DERR_INVALIDCALL;
7275 const struct blit_shader arbfp_blit = {
7276 arbfp_blit_alloc,
7277 arbfp_blit_free,
7278 arbfp_blit_set,
7279 arbfp_blit_unset,
7280 arbfp_blit_supported,
7281 arbfp_blit_color_fill,
7282 arbfp_blit_depth_fill,