2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
12 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
13 * Copyright 2009 Henri Verbeet for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants
);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps
);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
42 /* Extract a line. Note that this modifies the source string. */
43 static char *get_line(char **ptr
)
48 if (!(q
= strstr(p
, "\n")))
60 static void shader_arb_dump_program_source(const char *source
)
63 char *ptr
, *line
, *tmp
;
65 source_size
= strlen(source
) + 1;
66 tmp
= HeapAlloc(GetProcessHeap(), 0, source_size
);
69 ERR("Failed to allocate %u bytes for shader source.\n", source_size
);
72 memcpy(tmp
, source
, source_size
);
75 while ((line
= get_line(&ptr
))) FIXME(" %s\n", line
);
78 HeapFree(GetProcessHeap(), 0, tmp
);
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");
100 if (shader
== WINED3D_SHADER_TYPE_PIXEL
)
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 "helper_const.xxxy";
108 case ARB_EPS
: return "ps_helper_const.z";
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
);
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 */
161 struct wined3d_shader_loop_control loop_control
;
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. */
175 struct arb_ps_compile_args
177 struct ps_compile_args super
;
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
;
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
];
198 unsigned char numbumpenvmatconsts
;
202 struct arb_vs_compile_args
204 struct vs_compile_args super
;
210 unsigned char clip_texcoord
;
211 unsigned char clipplane_mask
;
213 DWORD boolclip_compare
;
218 unsigned char samplers
[4];
219 DWORD samplers_compare
;
221 unsigned char loop_ctrl
[MAX_CONST_I
][3];
224 struct arb_vs_compiled_shader
226 struct arb_vs_compile_args args
;
228 UINT int_consts
[MAX_CONST_I
];
230 char need_color_unclamp
;
234 struct recorded_instruction
236 struct wined3d_shader_instruction ins
;
240 struct shader_arb_ctx_priv
245 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
247 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
249 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
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
;
262 unsigned int num_loops
, loop_depth
, num_ifcs
;
265 unsigned int vs_clipplanes
;
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
];
280 struct wined3d_shader_signature_element
*sig
;
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
;
294 struct arb_vshader_private
{
295 struct arb_vs_compiled_shader
*gl_shaders
;
296 UINT num_gl_shaders
, shader_array_size
;
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
;
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
->usesrcp
) return TRUE
; /* EPS */
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
;
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(IWineD3DBaseShaderImpl
*This
, 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
;
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]);
384 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
385 if (target_type
== GL_FRAGMENT_PROGRAM_ARB
&& This
->baseShader
.reg_maps
.shader_version
.major
== 1)
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;
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
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 */
437 for(i
++; (i
< max_constants
) && dirty_consts
[i
]; i
++) {
441 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type
, j
, i
- j
, constants
+ (j
* 4)));
444 for(; i
< max_constants
; i
++) {
445 if(dirty_consts
[i
]) {
447 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, constants
+ (i
* 4)));
451 checkGLcall("glProgramEnvParameter4fvARB()");
453 /* Load immediate constants */
454 if(This
->baseShader
.load_local_constsF
) {
455 if (TRACE_ON(d3d_shader
)) {
456 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
457 GLfloat
* values
= (GLfloat
*)lconst
->value
;
458 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
459 values
[0], values
[1], values
[2], values
[3]);
462 /* Immediate constants are clamped for 1.X shaders at loading times */
464 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
465 dirty_consts
[lconst
->idx
] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
466 ret
= max(ret
, lconst
->idx
+ 1);
467 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, lconst
->idx
, (GLfloat
*)lconst
->value
));
469 checkGLcall("glProgramEnvParameter4fvARB()");
470 return ret
; /* The loaded immediate constants need reloading for the next shader */
472 return 0; /* No constants are dirty now */
477 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
479 static void shader_arb_load_np2fixup_constants(void *shader_priv
,
480 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
482 const struct shader_arb_priv
* priv
= shader_priv
;
484 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
485 if (!use_ps(state
)) return;
487 if (priv
->compiled_fprog
&& priv
->compiled_fprog
->np2fixup_info
.super
.active
) {
488 const struct arb_ps_np2fixup_info
* const fixup
= &priv
->compiled_fprog
->np2fixup_info
;
490 WORD active
= fixup
->super
.active
;
491 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
493 for (i
= 0; active
; active
>>= 1, ++i
)
495 const struct wined3d_texture
*tex
= state
->textures
[i
];
496 const unsigned char idx
= fixup
->super
.idx
[i
];
497 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
499 if (!(active
& 1)) continue;
502 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
508 tex_dim
[2] = tex
->pow2_matrix
[0];
509 tex_dim
[3] = tex
->pow2_matrix
[5];
513 tex_dim
[0] = tex
->pow2_matrix
[0];
514 tex_dim
[1] = tex
->pow2_matrix
[5];
518 for (i
= 0; i
< fixup
->super
.num_consts
; ++i
) {
519 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
,
520 fixup
->offset
+ i
, &np2fixup_constants
[i
* 4]));
525 /* GL locking is done by the caller. */
526 static void shader_arb_ps_local_constants(const struct arb_ps_compiled_shader
*gl_shader
,
527 const struct wined3d_context
*context
, const struct wined3d_state
*state
, float rt_height
)
529 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
532 for(i
= 0; i
< gl_shader
->numbumpenvmatconsts
; i
++)
534 int texunit
= gl_shader
->bumpenvmatconst
[i
].texunit
;
536 /* The state manager takes care that this function is always called if the bump env matrix changes */
537 const float *data
= (const float *)&state
->texture_states
[texunit
][WINED3DTSS_BUMPENVMAT00
];
538 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
,
539 gl_shader
->bumpenvmatconst
[i
].const_num
, data
));
541 if (gl_shader
->luminanceconst
[i
].const_num
!= WINED3D_CONST_NUM_UNUSED
)
543 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
544 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
545 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
546 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
548 const float *scale
= (const float *)&state
->texture_states
[texunit
][WINED3DTSS_BUMPENVLSCALE
];
549 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
,
550 gl_shader
->luminanceconst
[i
].const_num
, scale
));
553 checkGLcall("Load bumpmap consts");
555 if(gl_shader
->ycorrection
!= WINED3D_CONST_NUM_UNUSED
)
557 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
558 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
563 val
[0] = context
->render_offscreen
? 0.0f
: rt_height
;
564 val
[1] = context
->render_offscreen
? 1.0f
: -1.0f
;
567 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->ycorrection
, val
));
568 checkGLcall("y correction loading");
571 if (!gl_shader
->num_int_consts
) return;
573 for(i
= 0; i
< MAX_CONST_I
; i
++)
575 if(gl_shader
->int_consts
[i
] != WINED3D_CONST_NUM_UNUSED
)
578 val
[0] = (float)state
->ps_consts_i
[4 * i
];
579 val
[1] = (float)state
->ps_consts_i
[4 * i
+ 1];
580 val
[2] = (float)state
->ps_consts_i
[4 * i
+ 2];
583 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->int_consts
[i
], val
));
586 checkGLcall("Load ps int consts");
589 /* GL locking is done by the caller. */
590 static void shader_arb_vs_local_constants(const struct arb_vs_compiled_shader
*gl_shader
,
591 const struct wined3d_context
*context
, const struct wined3d_state
*state
)
593 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
594 float position_fixup
[4];
597 /* Upload the position fixup */
598 shader_get_position_fixup(context
, state
, position_fixup
);
599 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, gl_shader
->pos_fixup
, position_fixup
));
601 if (!gl_shader
->num_int_consts
) return;
603 for(i
= 0; i
< MAX_CONST_I
; i
++)
605 if(gl_shader
->int_consts
[i
] != WINED3D_CONST_NUM_UNUSED
)
608 val
[0] = (float)state
->vs_consts_i
[4 * i
];
609 val
[1] = (float)state
->vs_consts_i
[4 * i
+ 1];
610 val
[2] = (float)state
->vs_consts_i
[4 * i
+ 2];
613 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, gl_shader
->int_consts
[i
], val
));
616 checkGLcall("Load vs int consts");
620 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
622 * We only support float constants in ARB at the moment, so don't
623 * worry about the Integers or Booleans
625 /* GL locking is done by the caller (state handler) */
626 static void shader_arb_load_constants(const struct wined3d_context
*context
, char usePixelShader
, char useVertexShader
)
628 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
629 struct wined3d_stateblock
*stateBlock
= device
->stateBlock
;
630 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
631 struct shader_arb_priv
*priv
= device
->shader_priv
;
635 IWineD3DBaseShaderImpl
*vshader
= (IWineD3DBaseShaderImpl
*)stateBlock
->state
.vertex_shader
;
636 const struct arb_vs_compiled_shader
*gl_shader
= priv
->compiled_vprog
;
638 /* Load DirectX 9 float constants for vertex shader */
639 device
->highest_dirty_vs_const
= shader_arb_load_constantsF(vshader
, gl_info
, GL_VERTEX_PROGRAM_ARB
,
640 device
->highest_dirty_vs_const
, stateBlock
->state
.vs_consts_f
, context
->vshader_const_dirty
);
641 shader_arb_vs_local_constants(gl_shader
, context
, &stateBlock
->state
);
646 IWineD3DBaseShaderImpl
*pshader
= (IWineD3DBaseShaderImpl
*)stateBlock
->state
.pixel_shader
;
647 const struct arb_ps_compiled_shader
*gl_shader
= priv
->compiled_fprog
;
648 float rt_height
= device
->render_targets
[0]->resource
.height
;
650 /* Load DirectX 9 float constants for pixel shader */
651 device
->highest_dirty_ps_const
= shader_arb_load_constantsF(pshader
, gl_info
, GL_FRAGMENT_PROGRAM_ARB
,
652 device
->highest_dirty_ps_const
, stateBlock
->state
.ps_consts_f
, context
->pshader_const_dirty
);
653 shader_arb_ps_local_constants(gl_shader
, context
, &stateBlock
->state
, rt_height
);
657 static void shader_arb_update_float_vertex_constants(IWineD3DDeviceImpl
*device
, UINT start
, UINT count
)
659 struct wined3d_context
*context
= context_get_current();
661 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
662 * context. On a context switch the old context will be fully dirtified */
663 if (!context
|| context
->swapchain
->device
!= device
) return;
665 memset(context
->vshader_const_dirty
+ start
, 1, sizeof(*context
->vshader_const_dirty
) * count
);
666 device
->highest_dirty_vs_const
= max(device
->highest_dirty_vs_const
, start
+ count
);
669 static void shader_arb_update_float_pixel_constants(IWineD3DDeviceImpl
*device
, UINT start
, UINT count
)
671 struct wined3d_context
*context
= context_get_current();
673 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
674 * context. On a context switch the old context will be fully dirtified */
675 if (!context
|| context
->swapchain
->device
!= device
) return;
677 memset(context
->pshader_const_dirty
+ start
, 1, sizeof(*context
->pshader_const_dirty
) * count
);
678 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, start
+ count
);
681 static DWORD
*local_const_mapping(IWineD3DBaseShaderImpl
*This
)
685 const local_constant
*lconst
;
687 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) return NULL
;
689 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
) * This
->baseShader
.limits
.constant_float
);
691 ERR("Out of memory\n");
695 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
696 ret
[lconst
->idx
] = idx
++;
701 /* Generate the variable & register declarations for the ARB_vertex_program output target */
702 static DWORD
shader_generate_arb_declarations(IWineD3DBaseShaderImpl
*shader
,
703 const struct wined3d_shader_reg_maps
*reg_maps
, struct wined3d_shader_buffer
*buffer
,
704 const struct wined3d_gl_info
*gl_info
, DWORD
*lconst_map
,
705 DWORD
*num_clipplanes
, struct shader_arb_ctx_priv
*ctx
)
707 DWORD i
, next_local
= 0;
708 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
709 unsigned max_constantsF
;
710 const local_constant
*lconst
;
713 /* In pixel shaders, all private constants are program local, we don't need anything
714 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
715 * If we need a private constant the GL implementation will squeeze it in somewhere
717 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
718 * immediate values. The posFixup is loaded using program.env for now, so always
719 * subtract one from the number of constants. If the shader uses indirect addressing,
720 * account for the helper const too because we have to declare all availabke d3d constants
721 * and don't know which are actually used.
725 max_constantsF
= gl_info
->limits
.arb_ps_native_constants
;
726 /* 24 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value. */
727 if (max_constantsF
< 24)
728 max_constantsF
= gl_info
->limits
.arb_ps_float_constants
;
732 const struct arb_vshader_private
*shader_data
= shader
->baseShader
.backend_data
;
733 max_constantsF
= gl_info
->limits
.arb_vs_native_constants
;
734 /* 96 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value.
735 * Also prevents max_constantsF from becoming less than 0 and
737 if (max_constantsF
< 96)
738 max_constantsF
= gl_info
->limits
.arb_vs_float_constants
;
740 if (reg_maps
->usesrelconstF
)
742 DWORD highest_constf
= 0, clip_limit
;
744 max_constantsF
-= reserved_vs_const(shader_data
, reg_maps
, gl_info
);
745 max_constantsF
-= count_bits(reg_maps
->integer_constants
);
747 for (i
= 0; i
< shader
->baseShader
.limits
.constant_float
; ++i
)
750 DWORD shift
= i
& 0x1f;
751 if(reg_maps
->constf
[idx
] & (1 << shift
)) highest_constf
= i
;
754 if(use_nv_clip(gl_info
) && ctx
->target_version
>= NV2
)
756 if(ctx
->cur_vs_args
->super
.clip_enabled
)
757 clip_limit
= gl_info
->limits
.clipplanes
;
763 unsigned int mask
= ctx
->cur_vs_args
->clip
.boolclip
.clipplane_mask
;
764 clip_limit
= min(count_bits(mask
), 4);
766 *num_clipplanes
= min(clip_limit
, max_constantsF
- highest_constf
- 1);
767 max_constantsF
-= *num_clipplanes
;
768 if(*num_clipplanes
< clip_limit
)
770 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes
, gl_info
->limits
.clipplanes
);
775 if (ctx
->target_version
>= NV2
) *num_clipplanes
= gl_info
->limits
.clipplanes
;
776 else *num_clipplanes
= min(gl_info
->limits
.clipplanes
, 4);
780 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
782 if (map
& 1) shader_addline(buffer
, "TEMP R%u;\n", i
);
785 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
787 if (map
& 1) shader_addline(buffer
, "ADDRESS A%u;\n", i
);
790 if (pshader
&& reg_maps
->shader_version
.major
== 1 && reg_maps
->shader_version
.minor
<= 3)
792 for (i
= 0, map
= reg_maps
->texcoord
; map
; map
>>= 1, ++i
)
794 if (map
& 1) shader_addline(buffer
, "TEMP T%u;\n", i
);
798 /* Load local constants using the program-local space,
799 * this avoids reloading them each time the shader is used
803 LIST_FOR_EACH_ENTRY(lconst
, &shader
->baseShader
.constantsF
, local_constant
, entry
)
805 shader_addline(buffer
, "PARAM C%u = program.local[%u];\n", lconst
->idx
,
806 lconst_map
[lconst
->idx
]);
807 next_local
= max(next_local
, lconst_map
[lconst
->idx
] + 1);
811 /* After subtracting privately used constants from the hardware limit(they are loaded as
812 * local constants), make sure the shader doesn't violate the env constant limit
816 max_constantsF
= min(max_constantsF
, gl_info
->limits
.arb_ps_float_constants
);
820 max_constantsF
= min(max_constantsF
, gl_info
->limits
.arb_vs_float_constants
);
823 /* Avoid declaring more constants than needed */
824 max_constantsF
= min(max_constantsF
, shader
->baseShader
.limits
.constant_float
);
826 /* we use the array-based constants array if the local constants are marked for loading,
827 * because then we use indirect addressing, or when the local constant list is empty,
828 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
829 * local constants do not declare the loaded constants as an array because ARB compilers usually
830 * do not optimize unused constants away
832 if (reg_maps
->usesrelconstF
)
834 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
835 shader_addline(buffer
, "PARAM C[%d] = { program.env[0..%d] };\n",
836 max_constantsF
, max_constantsF
- 1);
838 for(i
= 0; i
< max_constantsF
; i
++) {
841 mask
= 1 << (i
& 0x1f);
842 if (!shader_constant_is_local(shader
, i
) && (reg_maps
->constf
[idx
] & mask
))
844 shader_addline(buffer
, "PARAM C%d = program.env[%d];\n",i
, i
);
852 static const char * const shift_tab
[] = {
853 "dummy", /* 0 (none) */
854 "coefmul.x", /* 1 (x2) */
855 "coefmul.y", /* 2 (x4) */
856 "coefmul.z", /* 3 (x8) */
857 "coefmul.w", /* 4 (x16) */
858 "dummy", /* 5 (x32) */
859 "dummy", /* 6 (x64) */
860 "dummy", /* 7 (x128) */
861 "dummy", /* 8 (d256) */
862 "dummy", /* 9 (d128) */
863 "dummy", /* 10 (d64) */
864 "dummy", /* 11 (d32) */
865 "coefdiv.w", /* 12 (d16) */
866 "coefdiv.z", /* 13 (d8) */
867 "coefdiv.y", /* 14 (d4) */
868 "coefdiv.x" /* 15 (d2) */
871 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction
*ins
,
872 const struct wined3d_shader_dst_param
*dst
, char *write_mask
)
874 char *ptr
= write_mask
;
876 if (dst
->write_mask
!= WINED3DSP_WRITEMASK_ALL
)
879 if (dst
->write_mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
880 if (dst
->write_mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
881 if (dst
->write_mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
882 if (dst
->write_mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
888 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param
*param
, BOOL fixup
, char *swizzle_str
)
890 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
891 * but addressed as "rgba". To fix this we need to swap the register's x
892 * and z components. */
893 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
894 char *ptr
= swizzle_str
;
896 /* swizzle bits fields: wwzzyyxx */
897 DWORD swizzle
= param
->swizzle
;
898 DWORD swizzle_x
= swizzle
& 0x03;
899 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
900 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
901 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
903 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
904 * generate a swizzle string. Unless we need to our own swizzling. */
905 if (swizzle
!= WINED3DSP_NOSWIZZLE
|| fixup
)
908 if (swizzle_x
== swizzle_y
&& swizzle_x
== swizzle_z
&& swizzle_x
== swizzle_w
) {
909 *ptr
++ = swizzle_chars
[swizzle_x
];
911 *ptr
++ = swizzle_chars
[swizzle_x
];
912 *ptr
++ = swizzle_chars
[swizzle_y
];
913 *ptr
++ = swizzle_chars
[swizzle_z
];
914 *ptr
++ = swizzle_chars
[swizzle_w
];
921 static void shader_arb_request_a0(const struct wined3d_shader_instruction
*ins
, const char *src
)
923 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
924 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
926 if (!strcmp(priv
->addr_reg
, src
)) return;
928 strcpy(priv
->addr_reg
, src
);
929 shader_addline(buffer
, "ARL A0.x, %s;\n", src
);
932 static void shader_arb_get_src_param(const struct wined3d_shader_instruction
*ins
,
933 const struct wined3d_shader_src_param
*src
, unsigned int tmpreg
, char *outregstr
);
935 static void shader_arb_get_register_name(const struct wined3d_shader_instruction
*ins
,
936 const struct wined3d_shader_register
*reg
, char *register_name
, BOOL
*is_color
)
938 /* oPos, oFog and oPts in D3D */
939 static const char * const rastout_reg_names
[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
940 struct IWineD3DBaseShaderImpl
*shader
= ins
->ctx
->shader
;
941 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
942 BOOL pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
943 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
949 case WINED3DSPR_TEMP
:
950 sprintf(register_name
, "R%u", reg
->idx
);
953 case WINED3DSPR_INPUT
:
956 if (reg_maps
->shader_version
.major
< 3)
958 if (!reg
->idx
) strcpy(register_name
, "fragment.color.primary");
959 else strcpy(register_name
, "fragment.color.secondary");
966 shader_arb_get_src_param(ins
, reg
->rel_addr
, 0, rel_reg
);
968 if (!strcmp(rel_reg
, "**aL_emul**"))
970 DWORD idx
= ctx
->aL
+ reg
->idx
;
971 if(idx
< MAX_REG_INPUT
)
973 strcpy(register_name
, ctx
->ps_input
[idx
]);
977 ERR("Pixel shader input register out of bounds: %u\n", idx
);
978 sprintf(register_name
, "out_of_bounds_%u", idx
);
981 else if (reg_maps
->input_registers
& 0x0300)
983 /* There are two ways basically:
985 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
986 * That means trouble if the loop also contains a breakc or if the control values
987 * aren't local constants.
988 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
989 * source dynamically. The trouble is that we cannot simply read aL.y because it
990 * is an ADDRESS register. We could however push it, load .zw with a value and use
991 * ADAC to load the condition code register and pop it again afterwards
993 FIXME("Relative input register addressing with more than 8 registers\n");
995 /* This is better than nothing for now */
996 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
998 else if(ctx
->cur_ps_args
->super
.vp_mode
!= vertexshader
)
1000 /* This is problematic because we'd have to consult the ctx->ps_input strings
1001 * for where to find the varying. Some may be "0.0", others can be texcoords or
1002 * colors. This needs either a pipeline replacement to make the vertex shader feed
1003 * proper varyings, or loop unrolling
1005 * For now use the texcoords and hope for the best
1007 FIXME("Non-vertex shader varying input with indirect addressing\n");
1008 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
1012 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
1013 * pulls GL_NV_fragment_program2 in
1015 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
1020 if(reg
->idx
< MAX_REG_INPUT
)
1022 strcpy(register_name
, ctx
->ps_input
[reg
->idx
]);
1026 ERR("Pixel shader input register out of bounds: %u\n", reg
->idx
);
1027 sprintf(register_name
, "out_of_bounds_%u", reg
->idx
);
1034 if (ctx
->cur_vs_args
->super
.swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
1035 sprintf(register_name
, "vertex.attrib[%u]", reg
->idx
);
1039 case WINED3DSPR_CONST
:
1040 if (!pshader
&& reg
->rel_addr
)
1042 const struct arb_vshader_private
*shader_data
= shader
->baseShader
.backend_data
;
1043 UINT rel_offset
= shader_data
->rel_offset
;
1046 if (reg_maps
->shader_version
.major
< 2)
1048 sprintf(rel_reg
, "A0.x");
1050 shader_arb_get_src_param(ins
, reg
->rel_addr
, 0, rel_reg
);
1051 if(ctx
->target_version
== ARB
) {
1052 if (!strcmp(rel_reg
, "**aL_emul**"))
1056 shader_arb_request_a0(ins
, rel_reg
);
1057 sprintf(rel_reg
, "A0.x");
1062 sprintf(register_name
, "C[%u]", ctx
->aL
+ reg
->idx
);
1063 else if (reg
->idx
>= rel_offset
)
1064 sprintf(register_name
, "C[%s + %u]", rel_reg
, reg
->idx
- rel_offset
);
1066 sprintf(register_name
, "C[%s - %u]", rel_reg
, rel_offset
- reg
->idx
);
1070 if (reg_maps
->usesrelconstF
)
1071 sprintf(register_name
, "C[%u]", reg
->idx
);
1073 sprintf(register_name
, "C%u", reg
->idx
);
1077 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1080 if (reg_maps
->shader_version
.major
== 1
1081 && reg_maps
->shader_version
.minor
<= 3)
1083 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
1084 * and as source to most instructions. For some instructions it is the texcoord
1085 * input. Those instructions know about the special use
1087 sprintf(register_name
, "T%u", reg
->idx
);
1089 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
1090 sprintf(register_name
, "fragment.texcoord[%u]", reg
->idx
);
1095 if (reg_maps
->shader_version
.major
== 1 || ctx
->target_version
>= NV2
)
1097 sprintf(register_name
, "A%u", reg
->idx
);
1101 sprintf(register_name
, "A%u_SHADOW", reg
->idx
);
1106 case WINED3DSPR_COLOROUT
:
1107 if (ctx
->cur_ps_args
->super
.srgb_correction
&& !reg
->idx
)
1109 strcpy(register_name
, "TMP_COLOR");
1113 if(ctx
->cur_ps_args
->super
.srgb_correction
) FIXME("sRGB correction on higher render targets\n");
1114 if (reg_maps
->highest_render_target
> 0)
1116 sprintf(register_name
, "result.color[%u]", reg
->idx
);
1120 strcpy(register_name
, "result.color");
1125 case WINED3DSPR_RASTOUT
:
1126 if(reg
->idx
== 1) sprintf(register_name
, "%s", ctx
->fog_output
);
1127 else sprintf(register_name
, "%s", rastout_reg_names
[reg
->idx
]);
1130 case WINED3DSPR_DEPTHOUT
:
1131 strcpy(register_name
, "result.depth");
1134 case WINED3DSPR_ATTROUT
:
1135 /* case WINED3DSPR_OUTPUT: */
1136 if (pshader
) sprintf(register_name
, "oD[%u]", reg
->idx
);
1137 else strcpy(register_name
, ctx
->color_output
[reg
->idx
]);
1140 case WINED3DSPR_TEXCRDOUT
:
1143 sprintf(register_name
, "oT[%u]", reg
->idx
);
1147 if (reg_maps
->shader_version
.major
< 3)
1149 strcpy(register_name
, ctx
->texcrd_output
[reg
->idx
]);
1153 strcpy(register_name
, ctx
->vs_output
[reg
->idx
]);
1158 case WINED3DSPR_LOOP
:
1159 if(ctx
->target_version
>= NV2
)
1161 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
1162 if(pshader
) sprintf(register_name
, "A0.x");
1163 else sprintf(register_name
, "aL.y");
1167 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
1168 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
1169 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
1170 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
1173 sprintf(register_name
, "**aL_emul**");
1178 case WINED3DSPR_CONSTINT
:
1179 sprintf(register_name
, "I%u", reg
->idx
);
1182 case WINED3DSPR_MISCTYPE
:
1185 sprintf(register_name
, "vpos");
1187 else if(reg
->idx
== 1)
1189 sprintf(register_name
, "fragment.facing.x");
1193 FIXME("Unknown MISCTYPE register index %u\n", reg
->idx
);
1198 FIXME("Unhandled register type %#x[%u]\n", reg
->type
, reg
->idx
);
1199 sprintf(register_name
, "unrecognized_register[%u]", reg
->idx
);
1204 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction
*ins
,
1205 const struct wined3d_shader_dst_param
*wined3d_dst
, char *str
)
1207 char register_name
[255];
1211 shader_arb_get_register_name(ins
, &wined3d_dst
->reg
, register_name
, &is_color
);
1212 strcpy(str
, register_name
);
1214 shader_arb_get_write_mask(ins
, wined3d_dst
, write_mask
);
1215 strcat(str
, write_mask
);
1218 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source
)
1220 switch(channel_source
)
1222 case CHANNEL_SOURCE_ZERO
: return "0";
1223 case CHANNEL_SOURCE_ONE
: return "1";
1224 case CHANNEL_SOURCE_X
: return "x";
1225 case CHANNEL_SOURCE_Y
: return "y";
1226 case CHANNEL_SOURCE_Z
: return "z";
1227 case CHANNEL_SOURCE_W
: return "w";
1229 FIXME("Unhandled channel source %#x\n", channel_source
);
1234 static void gen_color_correction(struct wined3d_shader_buffer
*buffer
, const char *reg
,
1235 DWORD dst_mask
, const char *one
, const char *two
, struct color_fixup_desc fixup
)
1239 if (is_complex_fixup(fixup
))
1241 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
1242 FIXME("Complex fixup (%#x) not supported\n", complex_fixup
);
1247 if (fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1248 if (fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1249 if (fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1250 if (fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1255 shader_addline(buffer
, "SWZ %s, %s, %s, %s, %s, %s;\n", reg
, reg
,
1256 shader_arb_get_fixup_swizzle(fixup
.x_source
), shader_arb_get_fixup_swizzle(fixup
.y_source
),
1257 shader_arb_get_fixup_swizzle(fixup
.z_source
), shader_arb_get_fixup_swizzle(fixup
.w_source
));
1261 if (fixup
.x_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_0
;
1262 if (fixup
.y_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_1
;
1263 if (fixup
.z_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_2
;
1264 if (fixup
.w_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_3
;
1270 char *ptr
= reg_mask
;
1272 if (mask
!= WINED3DSP_WRITEMASK_ALL
)
1275 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
1276 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
1277 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
1278 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
1282 shader_addline(buffer
, "MAD %s%s, %s, %s, -%s;\n", reg
, reg_mask
, reg
, two
, one
);
1286 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction
*ins
)
1289 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1290 if (!ins
->dst_count
) return "";
1292 mod
= ins
->dst
[0].modifiers
;
1294 /* Silently ignore PARTIALPRECISION if its not supported */
1295 if(priv
->target_version
== ARB
) mod
&= ~WINED3DSPDM_PARTIALPRECISION
;
1297 if(mod
& WINED3DSPDM_MSAMPCENTROID
)
1299 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1300 mod
&= ~WINED3DSPDM_MSAMPCENTROID
;
1305 case WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
:
1308 case WINED3DSPDM_SATURATE
:
1311 case WINED3DSPDM_PARTIALPRECISION
:
1318 FIXME("Unknown modifiers 0x%08x\n", mod
);
1323 #define TEX_PROJ 0x1
1324 #define TEX_BIAS 0x2
1326 #define TEX_DERIV 0x10
1328 static void shader_hw_sample(const struct wined3d_shader_instruction
*ins
, DWORD sampler_idx
,
1329 const char *dst_str
, const char *coord_reg
, WORD flags
, const char *dsx
, const char *dsy
)
1331 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1332 DWORD sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
1333 const struct wined3d_texture
*texture
;
1334 const char *tex_type
;
1335 BOOL np2_fixup
= FALSE
;
1336 struct IWineD3DBaseShaderImpl
*shader
= ins
->ctx
->shader
;
1337 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
1338 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1340 BOOL pshader
= shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
1342 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1343 if(!pshader
) sampler_idx
+= MAX_FRAGMENT_SAMPLERS
;
1345 switch(sampler_type
) {
1351 texture
= device
->stateBlock
->state
.textures
[sampler_idx
];
1352 if (texture
&& texture
->target
== GL_TEXTURE_RECTANGLE_ARB
)
1358 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
1360 if (priv
->cur_np2fixup_info
->super
.active
& (1 << sampler_idx
))
1362 if (flags
) FIXME("Only ordinary sampling from NP2 textures is supported.\n");
1363 else np2_fixup
= TRUE
;
1368 case WINED3DSTT_VOLUME
:
1372 case WINED3DSTT_CUBE
:
1377 ERR("Unexpected texture type %d\n", sampler_type
);
1381 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1382 * so don't use shader_arb_get_modifier
1384 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
) mod
= "_SAT";
1387 /* Fragment samplers always have indentity mapping */
1388 if(sampler_idx
>= MAX_FRAGMENT_SAMPLERS
)
1390 sampler_idx
= priv
->cur_vs_args
->vertex
.samplers
[sampler_idx
- MAX_FRAGMENT_SAMPLERS
];
1393 if (flags
& TEX_DERIV
)
1395 if(flags
& TEX_PROJ
) FIXME("Projected texture sampling with custom derivatives\n");
1396 if(flags
& TEX_BIAS
) FIXME("Biased texture sampling with custom derivatives\n");
1397 shader_addline(buffer
, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
,
1398 dsx
, dsy
,sampler_idx
, tex_type
);
1400 else if(flags
& TEX_LOD
)
1402 if(flags
& TEX_PROJ
) FIXME("Projected texture sampling with explicit lod\n");
1403 if(flags
& TEX_BIAS
) FIXME("Biased texture sampling with explicit lod\n");
1404 shader_addline(buffer
, "TXL%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
,
1405 sampler_idx
, tex_type
);
1407 else if (flags
& TEX_BIAS
)
1409 /* Shouldn't be possible, but let's check for it */
1410 if(flags
& TEX_PROJ
) FIXME("Biased and Projected texture sampling\n");
1411 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1412 shader_addline(buffer
, "TXB%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1414 else if (flags
& TEX_PROJ
)
1416 shader_addline(buffer
, "TXP%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1422 const unsigned char idx
= priv
->cur_np2fixup_info
->super
.idx
[sampler_idx
];
1423 shader_addline(buffer
, "MUL TA, np2fixup[%u].%s, %s;\n", idx
>> 1,
1424 (idx
% 2) ? "zwxy" : "xyzw", coord_reg
);
1426 shader_addline(buffer
, "TEX%s %s, TA, texture[%u], %s;\n", mod
, dst_str
, sampler_idx
, tex_type
);
1429 shader_addline(buffer
, "TEX%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1434 gen_color_correction(buffer
, dst_str
, ins
->dst
[0].write_mask
,
1435 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL
, ARB_ONE
),
1436 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL
, ARB_TWO
),
1437 priv
->cur_ps_args
->super
.color_fixup
[sampler_idx
]);
1441 static void shader_arb_get_src_param(const struct wined3d_shader_instruction
*ins
,
1442 const struct wined3d_shader_src_param
*src
, unsigned int tmpreg
, char *outregstr
)
1444 /* Generate a line that does the input modifier computation and return the input register to use */
1445 BOOL is_color
= FALSE
;
1449 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1450 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1451 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
1452 const char *two
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_TWO
);
1454 /* Assume a new line will be added */
1457 /* Get register name */
1458 shader_arb_get_register_name(ins
, &src
->reg
, regstr
, &is_color
);
1459 shader_arb_get_swizzle(src
, is_color
, swzstr
);
1461 switch (src
->modifiers
)
1463 case WINED3DSPSM_NONE
:
1464 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
1467 case WINED3DSPSM_NEG
:
1468 sprintf(outregstr
, "-%s%s", regstr
, swzstr
);
1471 case WINED3DSPSM_BIAS
:
1472 shader_addline(buffer
, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg
, regstr
);
1474 case WINED3DSPSM_BIASNEG
:
1475 shader_addline(buffer
, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg
, regstr
);
1477 case WINED3DSPSM_SIGN
:
1478 shader_addline(buffer
, "MAD T%c, %s, %s, -%s;\n", 'A' + tmpreg
, regstr
, two
, one
);
1480 case WINED3DSPSM_SIGNNEG
:
1481 shader_addline(buffer
, "MAD T%c, %s, -%s, %s;\n", 'A' + tmpreg
, regstr
, two
, one
);
1483 case WINED3DSPSM_COMP
:
1484 shader_addline(buffer
, "SUB T%c, %s, %s;\n", 'A' + tmpreg
, one
, regstr
);
1486 case WINED3DSPSM_X2
:
1487 shader_addline(buffer
, "ADD T%c, %s, %s;\n", 'A' + tmpreg
, regstr
, regstr
);
1489 case WINED3DSPSM_X2NEG
:
1490 shader_addline(buffer
, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg
, regstr
, regstr
);
1492 case WINED3DSPSM_DZ
:
1493 shader_addline(buffer
, "RCP T%c, %s.z;\n", 'A' + tmpreg
, regstr
);
1494 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
1496 case WINED3DSPSM_DW
:
1497 shader_addline(buffer
, "RCP T%c, %s.w;\n", 'A' + tmpreg
, regstr
);
1498 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
1500 case WINED3DSPSM_ABS
:
1501 if(ctx
->target_version
>= NV2
) {
1502 sprintf(outregstr
, "|%s%s|", regstr
, swzstr
);
1505 shader_addline(buffer
, "ABS T%c, %s;\n", 'A' + tmpreg
, regstr
);
1508 case WINED3DSPSM_ABSNEG
:
1509 if(ctx
->target_version
>= NV2
) {
1510 sprintf(outregstr
, "-|%s%s|", regstr
, swzstr
);
1512 shader_addline(buffer
, "ABS T%c, %s;\n", 'A' + tmpreg
, regstr
);
1513 sprintf(outregstr
, "-T%c%s", 'A' + tmpreg
, swzstr
);
1518 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
1522 /* Return modified or original register, with swizzle */
1524 sprintf(outregstr
, "T%c%s", 'A' + tmpreg
, swzstr
);
1527 static void pshader_hw_bem(const struct wined3d_shader_instruction
*ins
)
1529 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1530 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1532 char src_name
[2][50];
1533 DWORD sampler_code
= dst
->reg
.idx
;
1535 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1537 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1539 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1540 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1543 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1544 shader_addline(buffer
, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code
);
1545 shader_addline(buffer
, "DP3 TC.r, TA, %s;\n", src_name
[1]);
1546 shader_addline(buffer
, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code
);
1547 shader_addline(buffer
, "DP3 TC.g, TA, %s;\n", src_name
[1]);
1549 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1550 shader_addline(buffer
, "ADD %s, %s, TC;\n", dst_name
, src_name
[0]);
1553 static DWORD
negate_modifiers(DWORD mod
, char *extra_char
)
1558 case WINED3DSPSM_NONE
: return WINED3DSPSM_NEG
;
1559 case WINED3DSPSM_NEG
: return WINED3DSPSM_NONE
;
1560 case WINED3DSPSM_BIAS
: return WINED3DSPSM_BIASNEG
;
1561 case WINED3DSPSM_BIASNEG
: return WINED3DSPSM_BIAS
;
1562 case WINED3DSPSM_SIGN
: return WINED3DSPSM_SIGNNEG
;
1563 case WINED3DSPSM_SIGNNEG
: return WINED3DSPSM_SIGN
;
1564 case WINED3DSPSM_COMP
: *extra_char
= '-'; return WINED3DSPSM_COMP
;
1565 case WINED3DSPSM_X2
: return WINED3DSPSM_X2NEG
;
1566 case WINED3DSPSM_X2NEG
: return WINED3DSPSM_X2
;
1567 case WINED3DSPSM_DZ
: *extra_char
= '-'; return WINED3DSPSM_DZ
;
1568 case WINED3DSPSM_DW
: *extra_char
= '-'; return WINED3DSPSM_DW
;
1569 case WINED3DSPSM_ABS
: return WINED3DSPSM_ABSNEG
;
1570 case WINED3DSPSM_ABSNEG
: return WINED3DSPSM_ABS
;
1572 FIXME("Unknown modifier %u\n", mod
);
1576 static void pshader_hw_cnd(const struct wined3d_shader_instruction
*ins
)
1578 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1579 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1581 char src_name
[3][50];
1582 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1583 ins
->ctx
->reg_maps
->shader_version
.minor
);
1586 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1587 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1589 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
1590 if (shader_version
<= WINED3D_SHADER_VERSION(1, 3) && ins
->coissue
)
1592 shader_addline(buffer
, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
[1]);
1594 struct wined3d_shader_src_param src0_copy
= ins
->src
[0];
1597 /* src0 may have a negate srcmod set, so we can't blindly add "-" to the name */
1598 src0_copy
.modifiers
= negate_modifiers(src0_copy
.modifiers
, &extra_neg
);
1600 shader_arb_get_src_param(ins
, &src0_copy
, 0, src_name
[0]);
1601 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1602 shader_addline(buffer
, "ADD TA, %c%s, coefdiv.x;\n", extra_neg
, src_name
[0]);
1603 /* No modifiers supported on CMP */
1604 shader_addline(buffer
, "CMP %s, TA, %s, %s;\n", dst_name
, src_name
[1], src_name
[2]);
1606 /* _SAT on CMP doesn't make much sense, but it is not a pure NOP */
1607 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
1609 shader_arb_get_register_name(ins
, &dst
->reg
, src_name
[0], &is_color
);
1610 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, dst_name
);
1615 static void pshader_hw_cmp(const struct wined3d_shader_instruction
*ins
)
1617 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1618 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1620 char src_name
[3][50];
1623 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1625 /* Generate input register names (with modifiers) */
1626 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1627 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1628 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1630 /* No modifiers are supported on CMP */
1631 shader_addline(buffer
, "CMP %s, %s, %s, %s;\n", dst_name
,
1632 src_name
[0], src_name
[2], src_name
[1]);
1634 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
1636 shader_arb_get_register_name(ins
, &dst
->reg
, src_name
[0], &is_color
);
1637 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, src_name
[0]);
1641 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1642 * dst = dot2(src0, src1) + src2 */
1643 static void pshader_hw_dp2add(const struct wined3d_shader_instruction
*ins
)
1645 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1646 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1648 char src_name
[3][50];
1649 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1651 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1652 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1653 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1655 if(ctx
->target_version
>= NV3
)
1657 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1658 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1659 shader_addline(buffer
, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins
),
1660 dst_name
, src_name
[0], src_name
[1], src_name
[2]);
1662 else if(ctx
->target_version
>= NV2
)
1664 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1665 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1666 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1667 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1669 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1671 * .xyxy and other swizzles that we could get with this are not valid in
1672 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1674 struct wined3d_shader_src_param tmp_param
= ins
->src
[1];
1675 DWORD swizzle
= tmp_param
.swizzle
& 0xf; /* Selects .xy */
1676 tmp_param
.swizzle
= swizzle
| (swizzle
<< 4); /* Creates .xyxy */
1678 shader_arb_get_src_param(ins
, &tmp_param
, 1, src_name
[1]);
1680 shader_addline(buffer
, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins
),
1681 dst_name
, src_name
[2], src_name
[0], src_name
[1]);
1685 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1686 /* 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]
1687 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1689 shader_addline(buffer
, "MOV TA, %s;\n", src_name
[0]);
1690 shader_addline(buffer
, "MOV TA.z, 0.0;\n");
1691 shader_addline(buffer
, "DP3 TA, TA, %s;\n", src_name
[1]);
1692 shader_addline(buffer
, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
[2]);
1696 /* Map the opcode 1-to-1 to the GL code */
1697 static void shader_hw_map2gl(const struct wined3d_shader_instruction
*ins
)
1699 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1700 const char *instruction
;
1701 char arguments
[256], dst_str
[50];
1703 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1705 switch (ins
->handler_idx
)
1707 case WINED3DSIH_ABS
: instruction
= "ABS"; break;
1708 case WINED3DSIH_ADD
: instruction
= "ADD"; break;
1709 case WINED3DSIH_CRS
: instruction
= "XPD"; break;
1710 case WINED3DSIH_DP3
: instruction
= "DP3"; break;
1711 case WINED3DSIH_DP4
: instruction
= "DP4"; break;
1712 case WINED3DSIH_DST
: instruction
= "DST"; break;
1713 case WINED3DSIH_FRC
: instruction
= "FRC"; break;
1714 case WINED3DSIH_LIT
: instruction
= "LIT"; break;
1715 case WINED3DSIH_LRP
: instruction
= "LRP"; break;
1716 case WINED3DSIH_MAD
: instruction
= "MAD"; break;
1717 case WINED3DSIH_MAX
: instruction
= "MAX"; break;
1718 case WINED3DSIH_MIN
: instruction
= "MIN"; break;
1719 case WINED3DSIH_MOV
: instruction
= "MOV"; break;
1720 case WINED3DSIH_MUL
: instruction
= "MUL"; break;
1721 case WINED3DSIH_SGE
: instruction
= "SGE"; break;
1722 case WINED3DSIH_SLT
: instruction
= "SLT"; break;
1723 case WINED3DSIH_SUB
: instruction
= "SUB"; break;
1724 case WINED3DSIH_MOVA
:instruction
= "ARR"; break;
1725 case WINED3DSIH_DSX
: instruction
= "DDX"; break;
1726 default: instruction
= "";
1727 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
1731 /* Note that shader_arb_add_dst_param() adds spaces. */
1732 arguments
[0] = '\0';
1733 shader_arb_get_dst_param(ins
, dst
, dst_str
);
1734 for (i
= 0; i
< ins
->src_count
; ++i
)
1737 strcat(arguments
, ", ");
1738 shader_arb_get_src_param(ins
, &ins
->src
[i
], i
, operand
);
1739 strcat(arguments
, operand
);
1741 shader_addline(buffer
, "%s%s %s%s;\n", instruction
, shader_arb_get_modifier(ins
), dst_str
, arguments
);
1744 static void shader_hw_nop(const struct wined3d_shader_instruction
*ins
)
1746 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1747 shader_addline(buffer
, "NOP;\n");
1750 static void shader_hw_mov(const struct wined3d_shader_instruction
*ins
)
1752 struct IWineD3DBaseShaderImpl
*shader
= ins
->ctx
->shader
;
1753 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
1754 BOOL pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
1755 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1756 const char *zero
= arb_get_helper_value(reg_maps
->shader_version
.type
, ARB_ZERO
);
1757 const char *one
= arb_get_helper_value(reg_maps
->shader_version
.type
, ARB_ONE
);
1758 const char *two
= arb_get_helper_value(reg_maps
->shader_version
.type
, ARB_TWO
);
1760 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1761 char src0_param
[256];
1763 if (ins
->handler_idx
== WINED3DSIH_MOVA
)
1765 const struct arb_vshader_private
*shader_data
= shader
->baseShader
.backend_data
;
1767 const char *offset
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_VS_REL_OFFSET
);
1769 if(ctx
->target_version
>= NV2
) {
1770 shader_hw_map2gl(ins
);
1773 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_param
);
1774 shader_arb_get_write_mask(ins
, &ins
->dst
[0], write_mask
);
1776 /* This implements the mova formula used in GLSL. The first two instructions
1777 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1781 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1783 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1784 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1786 shader_addline(buffer
, "SGE A0_SHADOW%s, %s, %s;\n", write_mask
, src0_param
, zero
);
1787 shader_addline(buffer
, "MAD A0_SHADOW%s, A0_SHADOW, %s, -%s;\n", write_mask
, two
, one
);
1789 shader_addline(buffer
, "ABS TA%s, %s;\n", write_mask
, src0_param
);
1790 shader_addline(buffer
, "ADD TA%s, TA, rel_addr_const.x;\n", write_mask
);
1791 shader_addline(buffer
, "FLR TA%s, TA;\n", write_mask
);
1792 if (shader_data
->rel_offset
)
1794 shader_addline(buffer
, "ADD TA%s, TA, %s;\n", write_mask
, offset
);
1796 shader_addline(buffer
, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask
);
1798 ((struct shader_arb_ctx_priv
*)ins
->ctx
->backend_data
)->addr_reg
[0] = '\0';
1800 else if (reg_maps
->shader_version
.major
== 1
1801 && !shader_is_pshader_version(reg_maps
->shader_version
.type
)
1802 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
1804 const struct arb_vshader_private
*shader_data
= shader
->baseShader
.backend_data
;
1805 src0_param
[0] = '\0';
1807 if (shader_data
->rel_offset
)
1809 const char *offset
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_VS_REL_OFFSET
);
1810 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_param
);
1811 shader_addline(buffer
, "ADD TA.x, %s, %s;\n", src0_param
, offset
);
1812 shader_addline(buffer
, "ARL A0.x, TA.x;\n");
1816 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1817 * with more than one component. Thus replicate the first source argument over all
1818 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1819 struct wined3d_shader_src_param tmp_src
= ins
->src
[0];
1820 tmp_src
.swizzle
= (tmp_src
.swizzle
& 0x3) * 0x55;
1821 shader_arb_get_src_param(ins
, &tmp_src
, 0, src0_param
);
1822 shader_addline(buffer
, "ARL A0.x, %s;\n", src0_param
);
1825 else if (ins
->dst
[0].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
->dst
[0].reg
.idx
&& pshader
)
1827 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) shader
;
1828 if(ctx
->cur_ps_args
->super
.srgb_correction
&& ps
->color0_mov
)
1830 shader_addline(buffer
, "#mov handled in srgb write code\n");
1833 shader_hw_map2gl(ins
);
1837 shader_hw_map2gl(ins
);
1841 static void pshader_hw_texkill(const struct wined3d_shader_instruction
*ins
)
1843 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1844 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1847 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1848 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1850 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1852 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
1854 const char *kilsrc
= "TA";
1857 shader_arb_get_register_name(ins
, &dst
->reg
, reg_dest
, &is_color
);
1858 if(dst
->write_mask
== WINED3DSP_WRITEMASK_ALL
)
1864 /* Sigh. KIL doesn't support swizzles/writemasks. KIL passes a writemask, but ".xy" for example
1865 * is not valid as a swizzle in ARB (needs ".xyyy"). Use SWZ to load the register properly, and set
1866 * masked out components to 0(won't kill)
1868 char x
= '0', y
= '0', z
= '0', w
= '0';
1869 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
) x
= 'x';
1870 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
) y
= 'y';
1871 if(dst
->write_mask
& WINED3DSP_WRITEMASK_2
) z
= 'z';
1872 if(dst
->write_mask
& WINED3DSP_WRITEMASK_3
) w
= 'w';
1873 shader_addline(buffer
, "SWZ TA, %s, %c, %c, %c, %c;\n", reg_dest
, x
, y
, z
, w
);
1875 shader_addline(buffer
, "KIL %s;\n", kilsrc
);
1877 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1878 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1880 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1881 * or pass in any temporary register(in shader phase 2)
1883 if(ins
->ctx
->reg_maps
->shader_version
.minor
<= 3) {
1884 sprintf(reg_dest
, "fragment.texcoord[%u]", dst
->reg
.idx
);
1886 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1888 shader_addline(buffer
, "SWZ TA, %s, x, y, z, 1;\n", reg_dest
);
1889 shader_addline(buffer
, "KIL TA;\n");
1893 static void pshader_hw_tex(const struct wined3d_shader_instruction
*ins
)
1895 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1896 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1897 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1898 ins
->ctx
->reg_maps
->shader_version
.minor
);
1899 struct wined3d_shader_src_param src
;
1903 DWORD reg_sampler_code
;
1906 /* All versions have a destination register */
1907 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1909 /* 1.0-1.4: Use destination register number as texture code.
1910 2.0+: Use provided sampler number as texure code. */
1911 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
1912 reg_sampler_code
= dst
->reg
.idx
;
1914 reg_sampler_code
= ins
->src
[1].reg
.idx
;
1916 /* 1.0-1.3: Use the texcoord varying.
1917 1.4+: Use provided coordinate source register. */
1918 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1919 sprintf(reg_coord
, "fragment.texcoord[%u]", reg_sampler_code
);
1921 /* TEX is the only instruction that can handle DW and DZ natively */
1923 if(src
.modifiers
== WINED3DSPSM_DW
) src
.modifiers
= WINED3DSPSM_NONE
;
1924 if(src
.modifiers
== WINED3DSPSM_DZ
) src
.modifiers
= WINED3DSPSM_NONE
;
1925 shader_arb_get_src_param(ins
, &src
, 0, reg_coord
);
1929 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1930 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1931 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1933 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1936 if (reg_sampler_code
< MAX_TEXTURES
)
1937 flags
= priv
->cur_ps_args
->super
.tex_transform
>> reg_sampler_code
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
1938 if (flags
& WINED3D_PSARGS_PROJECTED
)
1939 myflags
|= TEX_PROJ
;
1941 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
1943 DWORD src_mod
= ins
->src
[0].modifiers
;
1944 if (src_mod
== WINED3DSPSM_DZ
) {
1945 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1946 * varying register, so we need a temp reg
1948 shader_addline(ins
->ctx
->buffer
, "SWZ TA, %s, x, y, z, z;\n", reg_coord
);
1949 strcpy(reg_coord
, "TA");
1950 myflags
|= TEX_PROJ
;
1951 } else if(src_mod
== WINED3DSPSM_DW
) {
1952 myflags
|= TEX_PROJ
;
1955 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) myflags
|= TEX_PROJ
;
1956 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) myflags
|= TEX_BIAS
;
1958 shader_hw_sample(ins
, reg_sampler_code
, reg_dest
, reg_coord
, myflags
, NULL
, NULL
);
1961 static void pshader_hw_texcoord(const struct wined3d_shader_instruction
*ins
)
1963 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1964 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1965 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1966 ins
->ctx
->reg_maps
->shader_version
.minor
);
1969 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1971 DWORD reg
= dst
->reg
.idx
;
1973 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1974 shader_addline(buffer
, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str
, reg
);
1978 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_src
);
1979 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1980 shader_addline(buffer
, "MOV %s, %s;\n", dst_str
, reg_src
);
1984 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction
*ins
)
1986 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1989 DWORD reg1
= ins
->dst
[0].reg
.idx
;
1993 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
1994 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1995 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
1996 /* Move .x first in case src_str is "TA" */
1997 shader_addline(buffer
, "MOV TA.y, %s.x;\n", src_str
);
1998 shader_addline(buffer
, "MOV TA.x, %s.w;\n", src_str
);
1999 if (reg1
< MAX_TEXTURES
)
2001 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2002 flags
= priv
->cur_ps_args
->super
.tex_transform
>> reg1
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
2004 shader_hw_sample(ins
, reg1
, dst_str
, "TA", flags
& WINED3D_PSARGS_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2007 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction
*ins
)
2009 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2011 DWORD reg1
= ins
->dst
[0].reg
.idx
;
2015 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
2016 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2017 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
2018 shader_addline(buffer
, "MOV TA.x, %s.y;\n", src_str
);
2019 shader_addline(buffer
, "MOV TA.y, %s.z;\n", src_str
);
2020 shader_hw_sample(ins
, reg1
, dst_str
, "TA", 0, NULL
, NULL
);
2023 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
2025 DWORD reg1
= ins
->dst
[0].reg
.idx
;
2029 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
2030 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2031 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
2032 shader_hw_sample(ins
, reg1
, dst_str
, src_str
, 0, NULL
, NULL
);
2035 static void pshader_hw_texbem(const struct wined3d_shader_instruction
*ins
)
2037 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2038 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2039 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2040 char reg_coord
[40], dst_reg
[50], src_reg
[50];
2041 DWORD reg_dest_code
;
2043 /* All versions have a destination register. The Tx where the texture coordinates come
2044 * from is the varying incarnation of the texture register
2046 reg_dest_code
= dst
->reg
.idx
;
2047 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_reg
);
2048 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_reg
);
2049 sprintf(reg_coord
, "fragment.texcoord[%u]", reg_dest_code
);
2051 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
2052 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
2054 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
2055 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
2057 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
2058 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
2061 shader_addline(buffer
, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code
);
2062 shader_addline(buffer
, "DP3 TA.x, TB, %s;\n", src_reg
);
2063 shader_addline(buffer
, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code
);
2064 shader_addline(buffer
, "DP3 TA.y, TB, %s;\n", src_reg
);
2066 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2067 * so we can't let the GL handle this.
2069 if ((priv
->cur_ps_args
->super
.tex_transform
>> reg_dest_code
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
2070 & WINED3D_PSARGS_PROJECTED
)
2072 shader_addline(buffer
, "RCP TB.w, %s.w;\n", reg_coord
);
2073 shader_addline(buffer
, "MUL TB.xy, %s, TB.w;\n", reg_coord
);
2074 shader_addline(buffer
, "ADD TA.xy, TA, TB;\n");
2076 shader_addline(buffer
, "ADD TA.xy, TA, %s;\n", reg_coord
);
2079 shader_hw_sample(ins
, reg_dest_code
, dst_reg
, "TA", 0, NULL
, NULL
);
2081 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
2083 /* No src swizzles are allowed, so this is ok */
2084 shader_addline(buffer
, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
2085 src_reg
, reg_dest_code
, reg_dest_code
);
2086 shader_addline(buffer
, "MUL %s, %s, TA;\n", dst_reg
, dst_reg
);
2090 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
2092 DWORD reg
= ins
->dst
[0].reg
.idx
;
2093 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2094 char src0_name
[50], dst_name
[50];
2096 struct wined3d_shader_register tmp_reg
= ins
->dst
[0].reg
;
2098 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2099 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
2100 * T<reg+1> register. Use this register to store the calculated vector
2102 tmp_reg
.idx
= reg
+ 1;
2103 shader_arb_get_register_name(ins
, &tmp_reg
, dst_name
, &is_color
);
2104 shader_addline(buffer
, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name
, reg
, src0_name
);
2107 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
2109 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2111 DWORD reg
= ins
->dst
[0].reg
.idx
;
2112 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2118 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
2119 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
2121 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2122 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2123 shader_addline(buffer
, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
2124 flags
= reg
< MAX_TEXTURES
? priv
->cur_ps_args
->super
.tex_transform
>> reg
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
: 0;
2125 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2128 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
2130 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
2131 DWORD reg
= ins
->dst
[0].reg
.idx
;
2132 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2133 char src0_name
[50], dst_name
[50];
2134 struct wined3d_shader_register tmp_reg
= ins
->dst
[0].reg
;
2137 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
2138 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
2139 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
2141 tmp_reg
.idx
= reg
+ 2 - tex_mx
->current_row
;
2142 shader_arb_get_register_name(ins
, &tmp_reg
, dst_name
, &is_color
);
2144 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2145 shader_addline(buffer
, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
2146 dst_name
, 'x' + tex_mx
->current_row
, reg
, src0_name
);
2147 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
2150 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
2152 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2153 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
2155 DWORD reg
= ins
->dst
[0].reg
.idx
;
2156 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2158 char src0_name
[50], dst_name
[50];
2161 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2162 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2163 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name
, reg
, src0_name
);
2165 /* Sample the texture using the calculated coordinates */
2166 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2167 flags
= reg
< MAX_TEXTURES
? priv
->cur_ps_args
->super
.tex_transform
>> reg
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
: 0;
2168 shader_hw_sample(ins
, reg
, dst_str
, dst_name
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2169 tex_mx
->current_row
= 0;
2172 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
2174 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2175 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
2177 DWORD reg
= ins
->dst
[0].reg
.idx
;
2178 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2184 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
2185 * components for temporary data storage
2187 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
2188 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2189 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
2191 /* Construct the eye-ray vector from w coordinates */
2192 shader_addline(buffer
, "MOV TB.x, fragment.texcoord[%u].w;\n", tex_mx
->texcoord_w
[0]);
2193 shader_addline(buffer
, "MOV TB.y, fragment.texcoord[%u].w;\n", tex_mx
->texcoord_w
[1]);
2194 shader_addline(buffer
, "MOV TB.z, fragment.texcoord[%u].w;\n", reg
);
2196 /* Calculate reflection vector
2198 shader_addline(buffer
, "DP3 %s.w, %s, TB;\n", dst_reg
, dst_reg
);
2199 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
2200 shader_addline(buffer
, "DP3 TB.w, %s, %s;\n", dst_reg
, dst_reg
);
2201 shader_addline(buffer
, "RCP TB.w, TB.w;\n");
2202 shader_addline(buffer
, "MUL %s.w, %s.w, TB.w;\n", dst_reg
, dst_reg
);
2203 shader_addline(buffer
, "MUL %s, %s.w, %s;\n", dst_reg
, dst_reg
, dst_reg
);
2204 shader_addline(buffer
, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg
, dst_reg
);
2206 /* Sample the texture using the calculated coordinates */
2207 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2208 flags
= reg
< MAX_TEXTURES
? priv
->cur_ps_args
->super
.tex_transform
>> reg
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
: 0;
2209 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2210 tex_mx
->current_row
= 0;
2213 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
2215 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2216 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
2218 DWORD reg
= ins
->dst
[0].reg
.idx
;
2219 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2226 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2227 shader_arb_get_src_param(ins
, &ins
->src
[0], 1, src1_name
);
2228 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
2229 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
2230 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
2232 /* Calculate reflection vector.
2235 * dst_reg.xyz = 2 * --------- * N - E
2238 * Which normalizes the normal vector
2240 shader_addline(buffer
, "DP3 %s.w, %s, %s;\n", dst_reg
, dst_reg
, src1_name
);
2241 shader_addline(buffer
, "DP3 TC.w, %s, %s;\n", dst_reg
, dst_reg
);
2242 shader_addline(buffer
, "RCP TC.w, TC.w;\n");
2243 shader_addline(buffer
, "MUL %s.w, %s.w, TC.w;\n", dst_reg
, dst_reg
);
2244 shader_addline(buffer
, "MUL %s, %s.w, %s;\n", dst_reg
, dst_reg
, dst_reg
);
2245 shader_addline(buffer
, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg
, dst_reg
, src1_name
);
2247 /* Sample the texture using the calculated coordinates */
2248 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2249 flags
= reg
< MAX_TEXTURES
? priv
->cur_ps_args
->super
.tex_transform
>> reg
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
: 0;
2250 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2251 tex_mx
->current_row
= 0;
2254 static void pshader_hw_texdepth(const struct wined3d_shader_instruction
*ins
)
2256 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2257 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2259 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2260 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
2262 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
2263 * which is essentially an input, is the destination register because it is the first
2264 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
2265 * here(writemasks/swizzles are not valid on texdepth)
2267 shader_arb_get_dst_param(ins
, dst
, dst_name
);
2269 /* According to the msdn, the source register(must be r5) is unusable after
2270 * the texdepth instruction, so we're free to modify it
2272 shader_addline(buffer
, "MIN %s.y, %s.y, %s;\n", dst_name
, dst_name
, one
);
2274 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2275 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2276 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2278 shader_addline(buffer
, "RCP %s.y, %s.y;\n", dst_name
, dst_name
);
2279 shader_addline(buffer
, "MUL TA.x, %s.x, %s.y;\n", dst_name
, dst_name
);
2280 shader_addline(buffer
, "MIN TA.x, TA.x, %s;\n", one
);
2281 shader_addline(buffer
, "MAX result.depth, TA.x, %s;\n", zero
);
2284 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
2285 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2286 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2287 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction
*ins
)
2289 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2290 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
2294 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2295 shader_addline(buffer
, "MOV TB, 0.0;\n");
2296 shader_addline(buffer
, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx
, src0
);
2298 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2299 shader_hw_sample(ins
, sampler_idx
, dst_str
, "TB", 0 /* Only one coord, can't be projected */, NULL
, NULL
);
2302 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2303 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2304 static void pshader_hw_texdp3(const struct wined3d_shader_instruction
*ins
)
2306 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2309 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2311 /* Handle output register */
2312 shader_arb_get_dst_param(ins
, dst
, dst_str
);
2313 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2314 shader_addline(buffer
, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str
, dst
->reg
.idx
, src0
);
2317 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2318 * Perform the 3rd row of a 3x3 matrix multiply */
2319 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction
*ins
)
2321 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2322 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2323 char dst_str
[50], dst_name
[50];
2327 shader_arb_get_dst_param(ins
, dst
, dst_str
);
2328 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2329 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2330 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name
, dst
->reg
.idx
, src0
);
2331 shader_addline(buffer
, "MOV %s, %s;\n", dst_str
, dst_name
);
2334 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2335 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2336 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2337 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2339 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
2341 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2342 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2343 char src0
[50], dst_name
[50];
2345 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2346 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
2348 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2349 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2350 shader_addline(buffer
, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name
, dst
->reg
.idx
, src0
);
2352 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2353 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2354 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2356 shader_addline(buffer
, "RCP %s.y, %s.y;\n", dst_name
, dst_name
);
2357 shader_addline(buffer
, "MUL %s.x, %s.x, %s.y;\n", dst_name
, dst_name
, dst_name
);
2358 shader_addline(buffer
, "MIN %s.x, %s.x, %s;\n", dst_name
, dst_name
, one
);
2359 shader_addline(buffer
, "MAX result.depth, %s.x, %s;\n", dst_name
, zero
);
2362 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2363 Vertex/Pixel shaders to ARB_vertex_program codes */
2364 static void shader_hw_mnxn(const struct wined3d_shader_instruction
*ins
)
2367 int nComponents
= 0;
2368 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2369 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2370 struct wined3d_shader_instruction tmp_ins
;
2372 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2374 /* Set constants for the temporary argument */
2375 tmp_ins
.ctx
= ins
->ctx
;
2376 tmp_ins
.dst_count
= 1;
2377 tmp_ins
.dst
= &tmp_dst
;
2378 tmp_ins
.src_count
= 2;
2379 tmp_ins
.src
= tmp_src
;
2381 switch(ins
->handler_idx
)
2383 case WINED3DSIH_M4x4
:
2385 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2387 case WINED3DSIH_M4x3
:
2389 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2391 case WINED3DSIH_M3x4
:
2393 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2395 case WINED3DSIH_M3x3
:
2397 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2399 case WINED3DSIH_M3x2
:
2401 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2404 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
2408 tmp_dst
= ins
->dst
[0];
2409 tmp_src
[0] = ins
->src
[0];
2410 tmp_src
[1] = ins
->src
[1];
2411 for (i
= 0; i
< nComponents
; i
++) {
2412 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2413 shader_hw_map2gl(&tmp_ins
);
2414 ++tmp_src
[1].reg
.idx
;
2418 static void shader_hw_rcp(const struct wined3d_shader_instruction
*ins
)
2420 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2421 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2422 const char *flt_eps
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_EPS
);
2427 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
); /* Destination */
2428 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2429 if (ins
->src
[0].swizzle
== WINED3DSP_NOSWIZZLE
)
2431 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2437 /* TODO: If the destination is readable, and not the same as the source, the destination
2438 * can be used instead of TA
2440 if (priv
->target_version
>= NV2
)
2442 shader_addline(buffer
, "MOVC TA.x, %s;\n", src
);
2443 shader_addline(buffer
, "MOV TA.x (EQ.x), %s;\n", flt_eps
);
2444 shader_addline(buffer
, "RCP%s %s, TA.x;\n", shader_arb_get_modifier(ins
), dst
);
2448 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2449 shader_addline(buffer
, "ABS TA.x, %s;\n", src
);
2450 shader_addline(buffer
, "SGE TA.y, -TA.x, %s;\n", zero
);
2451 shader_addline(buffer
, "MAD TA.x, TA.y, %s, %s;\n", flt_eps
, src
);
2452 shader_addline(buffer
, "RCP%s %s, TA.x;\n", shader_arb_get_modifier(ins
), dst
);
2456 static void shader_hw_scalar_op(const struct wined3d_shader_instruction
*ins
)
2458 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2459 const char *instruction
;
2464 switch(ins
->handler_idx
)
2466 case WINED3DSIH_RSQ
: instruction
= "RSQ"; break;
2467 case WINED3DSIH_RCP
: instruction
= "RCP"; break;
2468 case WINED3DSIH_EXP
: instruction
= "EX2"; break;
2469 case WINED3DSIH_EXPP
: instruction
= "EXP"; break;
2470 default: instruction
= "";
2471 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
2475 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
); /* Destination */
2476 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2477 if (ins
->src
[0].swizzle
== WINED3DSP_NOSWIZZLE
)
2479 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2485 shader_addline(buffer
, "%s%s %s, %s;\n", instruction
, shader_arb_get_modifier(ins
), dst
, src
);
2488 static void shader_hw_nrm(const struct wined3d_shader_instruction
*ins
)
2490 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2493 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2494 BOOL pshader
= shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2495 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2497 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2498 shader_arb_get_src_param(ins
, &ins
->src
[0], 1 /* Use TB */, src_name
);
2500 /* In D3D, NRM of a vector with length zero returns zero. Catch this situation, as
2501 * otherwise NRM or RSQ would return NaN */
2502 if(pshader
&& priv
->target_version
>= NV3
)
2504 /* GL_NV_fragment_program2's NRM needs protection against length zero vectors too
2506 * TODO: Find out if DP3+NRM+MOV is really faster than DP3+RSQ+MUL
2508 shader_addline(buffer
, "DP3C TA, %s, %s;\n", src_name
, src_name
);
2509 shader_addline(buffer
, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
);
2510 shader_addline(buffer
, "MOV %s (EQ), %s;\n", dst_name
, zero
);
2512 else if(priv
->target_version
>= NV2
)
2514 shader_addline(buffer
, "DP3C TA.x, %s, %s;\n", src_name
, src_name
);
2515 shader_addline(buffer
, "RSQ TA.x (NE), TA.x;\n");
2516 shader_addline(buffer
, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins
), dst_name
,
2521 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
2523 shader_addline(buffer
, "DP3 TA.x, %s, %s;\n", src_name
, src_name
);
2524 /* Pass any non-zero value to RSQ if the input vector has a length of zero. The
2525 * RSQ result doesn't matter, as long as multiplying it by 0 returns 0.
2527 shader_addline(buffer
, "SGE TA.y, -TA.x, %s;\n", zero
);
2528 shader_addline(buffer
, "MAD TA.x, %s, TA.y, TA.x;\n", one
);
2530 shader_addline(buffer
, "RSQ TA.x, TA.x;\n");
2531 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2532 shader_addline(buffer
, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins
), dst_name
,
2537 static void shader_hw_lrp(const struct wined3d_shader_instruction
*ins
)
2539 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2541 char src_name
[3][50];
2543 /* ARB_fragment_program has a convenient LRP instruction */
2544 if(shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)) {
2545 shader_hw_map2gl(ins
);
2549 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2550 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
2551 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
2552 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
2554 shader_addline(buffer
, "SUB TA, %s, %s;\n", src_name
[1], src_name
[2]);
2555 shader_addline(buffer
, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins
),
2556 dst_name
, src_name
[0], src_name
[2]);
2559 static void shader_hw_sincos(const struct wined3d_shader_instruction
*ins
)
2561 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2562 * must contain fixed constants. So we need a separate function to filter those constants and
2565 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2566 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2567 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2569 char src_name0
[50], src_name1
[50], src_name2
[50];
2572 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2573 if(shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)) {
2574 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2575 /* No modifiers are supported on SCS */
2576 shader_addline(buffer
, "SCS %s, %s;\n", dst_name
, src_name0
);
2578 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
2580 shader_arb_get_register_name(ins
, &dst
->reg
, src_name0
, &is_color
);
2581 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, src_name0
);
2583 } else if(priv
->target_version
>= NV2
) {
2584 shader_arb_get_register_name(ins
, &dst
->reg
, dst_name
, &is_color
);
2586 /* Sincos writemask must be .x, .y or .xy */
2587 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
)
2588 shader_addline(buffer
, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name0
);
2589 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
)
2590 shader_addline(buffer
, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name0
);
2592 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2593 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2595 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2596 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2598 * The constants we get are:
2600 * +1 +1, -1 -1 +1 +1 -1 -1
2601 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2602 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2604 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2608 * (x/2)^4 = x^4 / 16
2609 * (x/2)^5 = x^5 / 32
2612 * To get the final result:
2613 * sin(x) = 2 * sin(x/2) * cos(x/2)
2614 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2615 * (from sin(x+y) and cos(x+y) rules)
2617 * As per MSDN, dst.z is undefined after the operation, and so is
2618 * dst.x and dst.y if they're masked out by the writemask. Ie
2619 * sincos dst.y, src1, c0, c1
2620 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2621 * vsa.exe also stops with an error if the dest register is the same register as the source
2622 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2623 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2625 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2626 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name2
);
2627 shader_arb_get_register_name(ins
, &dst
->reg
, dst_name
, &is_color
);
2629 shader_addline(buffer
, "MUL %s.x, %s, %s;\n", dst_name
, src_name0
, src_name0
); /* x ^ 2 */
2630 shader_addline(buffer
, "MUL TA.y, %s.x, %s;\n", dst_name
, src_name0
); /* x ^ 3 */
2631 shader_addline(buffer
, "MUL %s.y, TA.y, %s;\n", dst_name
, src_name0
); /* x ^ 4 */
2632 shader_addline(buffer
, "MUL TA.z, %s.y, %s;\n", dst_name
, src_name0
); /* x ^ 5 */
2633 shader_addline(buffer
, "MUL %s.z, TA.z, %s;\n", dst_name
, src_name0
); /* x ^ 6 */
2634 shader_addline(buffer
, "MUL TA.w, %s.z, %s;\n", dst_name
, src_name0
); /* x ^ 7 */
2638 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2639 * properly merge that with MULs in the code above?
2640 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2641 * we can merge the sine and cosine MAD rows to calculate them together.
2643 shader_addline(buffer
, "MUL TA.x, %s, %s.w;\n", src_name0
, src_name2
); /* x^1, +1/(1!*2) */
2644 shader_addline(buffer
, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2
); /* -1/(3!*8) */
2645 shader_addline(buffer
, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1
); /* +1/(5!*32) */
2646 shader_addline(buffer
, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1
); /* -1/(7!*128) */
2649 shader_addline(buffer
, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name
, src_name2
, src_name2
); /* -1/(2!*4), +1.0 */
2650 shader_addline(buffer
, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name
, src_name1
); /* +1/(4!*16) */
2651 shader_addline(buffer
, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name
, src_name1
); /* -1/(6!*64) */
2653 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
) {
2655 shader_addline(buffer
, "MUL TA.z, TA.y, TA.y;\n");
2656 shader_addline(buffer
, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name
);
2658 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
) {
2660 shader_addline(buffer
, "MUL %s.y, TA.x, TA.y;\n", dst_name
);
2661 shader_addline(buffer
, "ADD %s.y, %s.y, %s.y;\n", dst_name
, dst_name
, dst_name
);
2666 static void shader_hw_sgn(const struct wined3d_shader_instruction
*ins
)
2668 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2671 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
2673 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2674 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
);
2676 /* SGN is only valid in vertex shaders */
2677 if(ctx
->target_version
>= NV2
) {
2678 shader_addline(buffer
, "SSG%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
);
2682 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2683 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2685 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
) {
2686 shader_addline(buffer
, "SLT %s, -%s, %s;\n", dst_name
, src_name
, src_name
);
2688 /* src contains TA? Write to the dest first. This won't overwrite our destination.
2689 * Then use TA, and calculate the final result
2691 * Not reading from TA? Store the first result in TA to avoid overwriting the
2692 * destination if src reg = dst reg
2694 if(strstr(src_name
, "TA"))
2696 shader_addline(buffer
, "SLT %s, %s, -%s;\n", dst_name
, src_name
, src_name
);
2697 shader_addline(buffer
, "SLT TA, -%s, %s;\n", src_name
, src_name
);
2698 shader_addline(buffer
, "ADD %s, %s, -TA;\n", dst_name
, dst_name
);
2702 shader_addline(buffer
, "SLT TA, -%s, %s;\n", src_name
, src_name
);
2703 shader_addline(buffer
, "SLT %s, %s, -%s;\n", dst_name
, src_name
, src_name
);
2704 shader_addline(buffer
, "ADD %s, TA, -%s;\n", dst_name
, dst_name
);
2709 static void shader_hw_dsy(const struct wined3d_shader_instruction
*ins
)
2711 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2717 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
);
2718 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2719 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2721 shader_addline(buffer
, "DDY %s, %s;\n", dst
, src
);
2722 shader_addline(buffer
, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins
), dst
, dst_name
);
2725 static DWORD
abs_modifier(DWORD mod
, BOOL
*need_abs
)
2731 case WINED3DSPSM_NONE
: return WINED3DSPSM_ABS
;
2732 case WINED3DSPSM_NEG
: return WINED3DSPSM_ABS
;
2733 case WINED3DSPSM_BIAS
: *need_abs
= TRUE
; return WINED3DSPSM_BIAS
;
2734 case WINED3DSPSM_BIASNEG
: *need_abs
= TRUE
; return WINED3DSPSM_BIASNEG
;
2735 case WINED3DSPSM_SIGN
: *need_abs
= TRUE
; return WINED3DSPSM_SIGN
;
2736 case WINED3DSPSM_SIGNNEG
: *need_abs
= TRUE
; return WINED3DSPSM_SIGNNEG
;
2737 case WINED3DSPSM_COMP
: *need_abs
= TRUE
; return WINED3DSPSM_COMP
;
2738 case WINED3DSPSM_X2
: *need_abs
= TRUE
; return WINED3DSPSM_X2
;
2739 case WINED3DSPSM_X2NEG
: *need_abs
= TRUE
; return WINED3DSPSM_X2NEG
;
2740 case WINED3DSPSM_DZ
: *need_abs
= TRUE
; return WINED3DSPSM_DZ
;
2741 case WINED3DSPSM_DW
: *need_abs
= TRUE
; return WINED3DSPSM_DW
;
2742 case WINED3DSPSM_ABS
: return WINED3DSPSM_ABS
;
2743 case WINED3DSPSM_ABSNEG
: return WINED3DSPSM_ABS
;
2745 FIXME("Unknown modifier %u\n", mod
);
2749 static void shader_hw_log_pow(const struct wined3d_shader_instruction
*ins
)
2751 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2752 char src0
[50], src1
[50], dst
[50];
2753 struct wined3d_shader_src_param src0_copy
= ins
->src
[0];
2754 BOOL need_abs
= FALSE
;
2758 switch(ins
->handler_idx
)
2760 case WINED3DSIH_LOG
: instr
= "LG2"; break;
2761 case WINED3DSIH_LOGP
: instr
= "LOG"; break;
2762 case WINED3DSIH_POW
: instr
= "POW"; arg2
= TRUE
; break;
2764 ERR("Unexpected instruction %d\n", ins
->handler_idx
);
2768 /* LOG, LOGP and POW operate on the absolute value of the input */
2769 src0_copy
.modifiers
= abs_modifier(src0_copy
.modifiers
, &need_abs
);
2771 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
);
2772 shader_arb_get_src_param(ins
, &src0_copy
, 0, src0
);
2773 if(arg2
) shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src1
);
2777 shader_addline(buffer
, "ABS TA, %s;\n", src0
);
2780 shader_addline(buffer
, "%s%s %s, TA, %s;\n", instr
, shader_arb_get_modifier(ins
), dst
, src1
);
2784 shader_addline(buffer
, "%s%s %s, TA;\n", instr
, shader_arb_get_modifier(ins
), dst
);
2789 shader_addline(buffer
, "%s%s %s, %s, %s;\n", instr
, shader_arb_get_modifier(ins
), dst
, src0
, src1
);
2793 shader_addline(buffer
, "%s%s %s, %s;\n", instr
, shader_arb_get_modifier(ins
), dst
, src0
);
2797 static void shader_hw_loop(const struct wined3d_shader_instruction
*ins
)
2799 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2801 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2804 shader_arb_get_src_param(ins
, &ins
->src
[1], 0, src_name
);
2808 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2809 struct list
*e
= list_head(&priv
->control_frames
);
2810 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2812 if(priv
->loop_depth
> 1) shader_addline(buffer
, "PUSHA aL;\n");
2813 /* The constant loader makes sure to load -1 into iX.w */
2814 shader_addline(buffer
, "ARLC aL, %s.xywz;\n", src_name
);
2815 shader_addline(buffer
, "BRA loop_%u_end (LE.x);\n", control_frame
->no
.loop
);
2816 shader_addline(buffer
, "loop_%u_start:\n", control_frame
->no
.loop
);
2820 shader_addline(buffer
, "LOOP %s;\n", src_name
);
2824 static void shader_hw_rep(const struct wined3d_shader_instruction
*ins
)
2826 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2828 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2830 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
);
2832 /* The constant loader makes sure to load -1 into iX.w */
2835 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2836 struct list
*e
= list_head(&priv
->control_frames
);
2837 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2839 if(priv
->loop_depth
> 1) shader_addline(buffer
, "PUSHA aL;\n");
2841 shader_addline(buffer
, "ARLC aL, %s.xywz;\n", src_name
);
2842 shader_addline(buffer
, "BRA loop_%u_end (LE.x);\n", control_frame
->no
.loop
);
2843 shader_addline(buffer
, "loop_%u_start:\n", control_frame
->no
.loop
);
2847 shader_addline(buffer
, "REP %s;\n", src_name
);
2851 static void shader_hw_endloop(const struct wined3d_shader_instruction
*ins
)
2853 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2854 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2858 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2859 struct list
*e
= list_head(&priv
->control_frames
);
2860 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2862 shader_addline(buffer
, "ARAC aL.xy, aL;\n");
2863 shader_addline(buffer
, "BRA loop_%u_start (GT.x);\n", control_frame
->no
.loop
);
2864 shader_addline(buffer
, "loop_%u_end:\n", control_frame
->no
.loop
);
2866 if(priv
->loop_depth
> 1) shader_addline(buffer
, "POPA aL;\n");
2870 shader_addline(buffer
, "ENDLOOP;\n");
2874 static void shader_hw_endrep(const struct wined3d_shader_instruction
*ins
)
2876 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2877 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2881 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2882 struct list
*e
= list_head(&priv
->control_frames
);
2883 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2885 shader_addline(buffer
, "ARAC aL.xy, aL;\n");
2886 shader_addline(buffer
, "BRA loop_%u_start (GT.x);\n", control_frame
->no
.loop
);
2887 shader_addline(buffer
, "loop_%u_end:\n", control_frame
->no
.loop
);
2889 if(priv
->loop_depth
> 1) shader_addline(buffer
, "POPA aL;\n");
2893 shader_addline(buffer
, "ENDREP;\n");
2897 static const struct control_frame
*find_last_loop(const struct shader_arb_ctx_priv
*priv
)
2899 struct control_frame
*control_frame
;
2901 LIST_FOR_EACH_ENTRY(control_frame
, &priv
->control_frames
, struct control_frame
, entry
)
2903 if(control_frame
->type
== LOOP
|| control_frame
->type
== REP
) return control_frame
;
2905 ERR("Could not find loop for break\n");
2909 static void shader_hw_break(const struct wined3d_shader_instruction
*ins
)
2911 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2912 const struct control_frame
*control_frame
= find_last_loop(ins
->ctx
->backend_data
);
2913 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2917 shader_addline(buffer
, "BRA loop_%u_end;\n", control_frame
->no
.loop
);
2921 shader_addline(buffer
, "BRK;\n");
2925 static const char *get_compare(COMPARISON_TYPE flags
)
2929 case COMPARISON_GT
: return "GT";
2930 case COMPARISON_EQ
: return "EQ";
2931 case COMPARISON_GE
: return "GE";
2932 case COMPARISON_LT
: return "LT";
2933 case COMPARISON_NE
: return "NE";
2934 case COMPARISON_LE
: return "LE";
2936 FIXME("Unrecognized comparison value: %u\n", flags
);
2941 static COMPARISON_TYPE
invert_compare(COMPARISON_TYPE flags
)
2945 case COMPARISON_GT
: return COMPARISON_LE
;
2946 case COMPARISON_EQ
: return COMPARISON_NE
;
2947 case COMPARISON_GE
: return COMPARISON_LT
;
2948 case COMPARISON_LT
: return COMPARISON_GE
;
2949 case COMPARISON_NE
: return COMPARISON_EQ
;
2950 case COMPARISON_LE
: return COMPARISON_GT
;
2952 FIXME("Unrecognized comparison value: %u\n", flags
);
2957 static void shader_hw_breakc(const struct wined3d_shader_instruction
*ins
)
2959 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2960 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2961 const struct control_frame
*control_frame
= find_last_loop(ins
->ctx
->backend_data
);
2964 const char *comp
= get_compare(ins
->flags
);
2966 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2967 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2971 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
2972 * away the subtraction result
2974 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2975 shader_addline(buffer
, "BRA loop_%u_end (%s.x);\n", control_frame
->no
.loop
, comp
);
2979 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2980 shader_addline(buffer
, "BRK (%s.x);\n", comp
);
2984 static void shader_hw_ifc(const struct wined3d_shader_instruction
*ins
)
2986 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2987 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2988 struct list
*e
= list_head(&priv
->control_frames
);
2989 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2993 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2995 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2996 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
3000 /* Invert the flag. We jump to the else label if the condition is NOT true */
3001 comp
= get_compare(invert_compare(ins
->flags
));
3002 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
3003 shader_addline(buffer
, "BRA ifc_%u_else (%s.x);\n", control_frame
->no
.ifc
, comp
);
3007 comp
= get_compare(ins
->flags
);
3008 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
3009 shader_addline(buffer
, "IF %s.x;\n", comp
);
3013 static void shader_hw_else(const struct wined3d_shader_instruction
*ins
)
3015 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3016 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3017 struct list
*e
= list_head(&priv
->control_frames
);
3018 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
3019 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
3023 shader_addline(buffer
, "BRA ifc_%u_endif;\n", control_frame
->no
.ifc
);
3024 shader_addline(buffer
, "ifc_%u_else:\n", control_frame
->no
.ifc
);
3025 control_frame
->had_else
= TRUE
;
3029 shader_addline(buffer
, "ELSE;\n");
3033 static void shader_hw_endif(const struct wined3d_shader_instruction
*ins
)
3035 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3036 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3037 struct list
*e
= list_head(&priv
->control_frames
);
3038 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
3039 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
3043 if(control_frame
->had_else
)
3045 shader_addline(buffer
, "ifc_%u_endif:\n", control_frame
->no
.ifc
);
3049 shader_addline(buffer
, "#No else branch. else is endif\n");
3050 shader_addline(buffer
, "ifc_%u_else:\n", control_frame
->no
.ifc
);
3055 shader_addline(buffer
, "ENDIF;\n");
3059 static void shader_hw_texldd(const struct wined3d_shader_instruction
*ins
)
3061 DWORD sampler_idx
= ins
->src
[1].reg
.idx
;
3063 char reg_src
[3][40];
3064 WORD flags
= TEX_DERIV
;
3066 shader_arb_get_dst_param(ins
, &ins
->dst
[0], reg_dest
);
3067 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_src
[0]);
3068 shader_arb_get_src_param(ins
, &ins
->src
[2], 1, reg_src
[1]);
3069 shader_arb_get_src_param(ins
, &ins
->src
[3], 2, reg_src
[2]);
3071 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) flags
|= TEX_PROJ
;
3072 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) flags
|= TEX_BIAS
;
3074 shader_hw_sample(ins
, sampler_idx
, reg_dest
, reg_src
[0], flags
, reg_src
[1], reg_src
[2]);
3077 static void shader_hw_texldl(const struct wined3d_shader_instruction
*ins
)
3079 DWORD sampler_idx
= ins
->src
[1].reg
.idx
;
3082 WORD flags
= TEX_LOD
;
3084 shader_arb_get_dst_param(ins
, &ins
->dst
[0], reg_dest
);
3085 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_coord
);
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_coord
, flags
, NULL
, NULL
);
3093 static void shader_hw_label(const struct wined3d_shader_instruction
*ins
)
3095 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3096 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3098 priv
->in_main_func
= FALSE
;
3099 /* Call instructions activate the NV extensions, not labels and rets. If there is an uncalled
3100 * subroutine, don't generate a label that will make GL complain
3102 if(priv
->target_version
== ARB
) return;
3104 shader_addline(buffer
, "l%u:\n", ins
->src
[0].reg
.idx
);
3107 static void vshader_add_footer(struct shader_arb_ctx_priv
*priv_ctx
,
3108 const struct arb_vshader_private
*shader_data
, const struct arb_vs_compile_args
*args
,
3109 const struct wined3d_shader_reg_maps
*reg_maps
, const struct wined3d_gl_info
*gl_info
,
3110 struct wined3d_shader_buffer
*buffer
)
3114 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
3115 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
3116 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
3117 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
3119 if (args
->super
.fog_src
== VS_FOG_Z
)
3120 shader_addline(buffer
, "MOV result.fogcoord, TMP_OUT.z;\n");
3121 else if (!reg_maps
->fog
)
3122 /* posFixup.x is always 1.0, so we can safely use it */
3123 shader_addline(buffer
, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
3125 /* Clipplanes are always stored without y inversion */
3126 if (use_nv_clip(gl_info
) && priv_ctx
->target_version
>= NV2
)
3128 if (args
->super
.clip_enabled
)
3130 for (i
= 0; i
< priv_ctx
->vs_clipplanes
; i
++)
3132 shader_addline(buffer
, "DP4 result.clip[%u].x, TMP_OUT, state.clip[%u].plane;\n", i
, i
);
3136 else if (args
->clip
.boolclip
.clip_texcoord
)
3138 unsigned int cur_clip
= 0;
3139 char component
[4] = {'x', 'y', 'z', 'w'};
3140 const char *zero
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_ZERO
);
3142 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
3144 if (args
->clip
.boolclip
.clipplane_mask
& (1 << i
))
3146 shader_addline(buffer
, "DP4 TA.%c, TMP_OUT, state.clip[%u].plane;\n",
3147 component
[cur_clip
++], i
);
3153 shader_addline(buffer
, "MOV TA, %s;\n", zero
);
3156 shader_addline(buffer
, "MOV TA.yzw, %s;\n", zero
);
3159 shader_addline(buffer
, "MOV TA.zw, %s;\n", zero
);
3162 shader_addline(buffer
, "MOV TA.w, %s;\n", zero
);
3165 shader_addline(buffer
, "MOV result.texcoord[%u], TA;\n",
3166 args
->clip
.boolclip
.clip_texcoord
- 1);
3169 /* Write the final position.
3171 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3172 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3173 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3174 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
3176 shader_addline(buffer
, "MUL TA, posFixup, TMP_OUT.w;\n");
3177 shader_addline(buffer
, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
3178 shader_addline(buffer
, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
3180 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3181 * and the glsl equivalent
3183 if (need_helper_const(shader_data
, reg_maps
, gl_info
))
3185 const char *two
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_TWO
);
3186 shader_addline(buffer
, "MAD TMP_OUT.z, TMP_OUT.z, %s, -TMP_OUT.w;\n", two
);
3190 shader_addline(buffer
, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
3191 shader_addline(buffer
, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
3194 shader_addline(buffer
, "MOV result.position, TMP_OUT;\n");
3196 priv_ctx
->footer_written
= TRUE
;
3199 static void shader_hw_ret(const struct wined3d_shader_instruction
*ins
)
3201 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3202 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3203 struct IWineD3DBaseShaderImpl
*shader
= ins
->ctx
->shader
;
3204 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
3206 if(priv
->target_version
== ARB
) return;
3210 if (priv
->in_main_func
) vshader_add_footer(priv
, shader
->baseShader
.backend_data
,
3211 priv
->cur_vs_args
, ins
->ctx
->reg_maps
, ins
->ctx
->gl_info
, buffer
);
3214 shader_addline(buffer
, "RET;\n");
3217 static void shader_hw_call(const struct wined3d_shader_instruction
*ins
)
3219 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3220 shader_addline(buffer
, "CAL l%u;\n", ins
->src
[0].reg
.idx
);
3223 /* GL locking is done by the caller */
3224 static GLuint
create_arb_blt_vertex_program(const struct wined3d_gl_info
*gl_info
)
3226 GLuint program_id
= 0;
3229 const char *blt_vprogram
=
3231 "PARAM c[1] = { { 1, 0.5 } };\n"
3232 "MOV result.position, vertex.position;\n"
3233 "MOV result.color, c[0].x;\n"
3234 "MOV result.texcoord[0], vertex.texcoord[0];\n"
3237 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
3238 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, program_id
));
3239 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
3240 strlen(blt_vprogram
), blt_vprogram
));
3241 checkGLcall("glProgramStringARB()");
3243 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
3246 FIXME("Vertex program error at position %d: %s\n\n", pos
,
3247 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3248 shader_arb_dump_program_source(blt_vprogram
);
3254 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
3255 checkGLcall("glGetProgramivARB()");
3256 if (!native
) WARN("Program exceeds native resource limits.\n");
3262 /* GL locking is done by the caller */
3263 static GLuint
create_arb_blt_fragment_program(const struct wined3d_gl_info
*gl_info
,
3264 enum tex_types tex_type
, BOOL masked
)
3266 GLuint program_id
= 0;
3267 const char *fprogram
;
3270 static const char * const blt_fprograms_full
[tex_type_count
] =
3277 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3278 "MOV result.depth.z, R0.x;\n"
3285 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3286 "MOV result.depth.z, R0.x;\n"
3291 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3292 "MOV result.depth.z, R0.x;\n"
3296 static const char * const blt_fprograms_masked
[tex_type_count
] =
3302 "PARAM mask = program.local[0];\n"
3304 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3305 "MUL R0.x, R0.x, R0.y;\n"
3307 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3308 "MOV result.depth.z, R0.x;\n"
3314 "PARAM mask = program.local[0];\n"
3316 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3317 "MUL R0.x, R0.x, R0.y;\n"
3319 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3320 "MOV result.depth.z, R0.x;\n"
3324 "PARAM mask = program.local[0];\n"
3326 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3327 "MUL R0.x, R0.x, R0.y;\n"
3329 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3330 "MOV result.depth.z, R0.x;\n"
3334 fprogram
= masked
? blt_fprograms_masked
[tex_type
] : blt_fprograms_full
[tex_type
];
3337 FIXME("tex_type %#x not supported, falling back to tex_2d\n", tex_type
);
3339 fprogram
= masked
? blt_fprograms_masked
[tex_type
] : blt_fprograms_full
[tex_type
];
3342 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
3343 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, program_id
));
3344 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(fprogram
), fprogram
));
3345 checkGLcall("glProgramStringARB()");
3347 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
3350 FIXME("Fragment program error at position %d: %s\n\n", pos
,
3351 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3352 shader_arb_dump_program_source(fprogram
);
3358 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
3359 checkGLcall("glGetProgramivARB()");
3360 if (!native
) WARN("Program exceeds native resource limits.\n");
3366 static void arbfp_add_sRGB_correction(struct wined3d_shader_buffer
*buffer
, const char *fragcolor
,
3367 const char *tmp1
, const char *tmp2
, const char *tmp3
, const char *tmp4
, BOOL condcode
)
3369 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
3373 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
3374 shader_addline(buffer
, "SUBC %s, %s.x, srgb_consts1.y;\n", tmp1
, fragcolor
);
3375 /* Calculate the > 0.0031308 case */
3376 shader_addline(buffer
, "POW %s.x (GE), %s.x, srgb_consts1.z;\n", fragcolor
, fragcolor
);
3377 shader_addline(buffer
, "POW %s.y (GE), %s.y, srgb_consts1.z;\n", fragcolor
, fragcolor
);
3378 shader_addline(buffer
, "POW %s.z (GE), %s.z, srgb_consts1.z;\n", fragcolor
, fragcolor
);
3379 shader_addline(buffer
, "MUL %s.xyz (GE), %s, srgb_consts1.w;\n", fragcolor
, fragcolor
);
3380 shader_addline(buffer
, "SUB %s.xyz (GE), %s, srgb_consts2.x;\n", fragcolor
, fragcolor
);
3381 /* Calculate the < case */
3382 shader_addline(buffer
, "MUL %s.xyz (LT), srgb_consts1.x, %s;\n", fragcolor
, fragcolor
);
3386 /* Calculate the > 0.0031308 case */
3387 shader_addline(buffer
, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1
, fragcolor
);
3388 shader_addline(buffer
, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1
, fragcolor
);
3389 shader_addline(buffer
, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1
, fragcolor
);
3390 shader_addline(buffer
, "MUL %s, %s, srgb_consts1.w;\n", tmp1
, tmp1
);
3391 shader_addline(buffer
, "SUB %s, %s, srgb_consts2.x;\n", tmp1
, tmp1
);
3392 /* Calculate the < case */
3393 shader_addline(buffer
, "MUL %s, srgb_consts1.x, %s;\n", tmp2
, fragcolor
);
3394 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
3395 shader_addline(buffer
, "SLT %s, srgb_consts1.y, %s;\n", tmp3
, fragcolor
);
3396 shader_addline(buffer
, "SGE %s, srgb_consts1.y, %s;\n", tmp4
, fragcolor
);
3397 /* Store the components > 0.0031308 in the destination */
3398 shader_addline(buffer
, "MUL %s.xyz, %s, %s;\n", fragcolor
, tmp1
, tmp3
);
3399 /* Add the components that are < 0.0031308 */
3400 shader_addline(buffer
, "MAD %s.xyz, %s, %s, %s;\n", fragcolor
, tmp2
, tmp4
, fragcolor
);
3401 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
3402 * result.color writes(.rgb first, then .a), or handle overwriting already written
3403 * components. The assembler uses a temporary register in this case, which is usually
3404 * not allocated from one of our registers that were used earlier.
3407 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
3410 static const DWORD
*find_loop_control_values(IWineD3DBaseShaderImpl
*This
, DWORD idx
)
3412 const local_constant
*constant
;
3414 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsI
, local_constant
, entry
)
3416 if (constant
->idx
== idx
)
3418 return constant
->value
;
3424 static void init_ps_input(const IWineD3DPixelShaderImpl
*This
, const struct arb_ps_compile_args
*args
,
3425 struct shader_arb_ctx_priv
*priv
)
3427 static const char * const texcoords
[8] =
3429 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
3430 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
3433 const struct wined3d_shader_signature_element
*sig
= This
->baseShader
.input_signature
;
3434 const char *semantic_name
;
3437 switch(args
->super
.vp_mode
)
3439 case pretransformed
:
3441 /* The pixelshader has to collect the varyings on its own. In any case properly load
3442 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
3443 * other attribs to 0.0.
3445 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
3446 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
3447 * load the texcoord attrib pointers to match the pixel shader signature
3449 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3451 semantic_name
= sig
[i
].semantic_name
;
3452 semantic_idx
= sig
[i
].semantic_idx
;
3453 if (!semantic_name
) continue;
3455 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3457 if (!semantic_idx
) priv
->ps_input
[i
] = "fragment.color.primary";
3458 else if(semantic_idx
== 1) priv
->ps_input
[i
] = "fragment.color.secondary";
3459 else priv
->ps_input
[i
] = "0.0";
3461 else if(args
->super
.vp_mode
== fixedfunction
)
3463 priv
->ps_input
[i
] = "0.0";
3465 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3467 if(semantic_idx
< 8) priv
->ps_input
[i
] = texcoords
[semantic_idx
];
3468 else priv
->ps_input
[i
] = "0.0";
3470 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3472 if (!semantic_idx
) priv
->ps_input
[i
] = "fragment.fogcoord";
3473 else priv
->ps_input
[i
] = "0.0";
3477 priv
->ps_input
[i
] = "0.0";
3480 TRACE("v%u, semantic %s%u is %s\n", i
, semantic_name
, semantic_idx
, priv
->ps_input
[i
]);
3485 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
3488 for(i
= 0; i
< 8; i
++)
3490 priv
->ps_input
[i
] = texcoords
[i
];
3492 priv
->ps_input
[8] = "fragment.color.primary";
3493 priv
->ps_input
[9] = "fragment.color.secondary";
3498 /* GL locking is done by the caller */
3499 static GLuint
shader_arb_generate_pshader(IWineD3DPixelShaderImpl
*This
,
3500 const struct wined3d_gl_info
*gl_info
, struct wined3d_shader_buffer
*buffer
,
3501 const struct arb_ps_compile_args
*args
, struct arb_ps_compiled_shader
*compiled
)
3503 const struct wined3d_shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
3504 CONST DWORD
*function
= This
->baseShader
.function
;
3505 const local_constant
*lconst
;
3508 DWORD
*lconst_map
= local_const_mapping((IWineD3DBaseShaderImpl
*) This
), next_local
, cur
;
3509 struct shader_arb_ctx_priv priv_ctx
;
3510 BOOL dcl_td
= FALSE
;
3511 BOOL want_nv_prog
= FALSE
;
3512 struct arb_pshader_private
*shader_priv
= This
->baseShader
.backend_data
;
3517 unsigned int i
, found
= 0;
3519 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
3522 || (This
->color0_mov
&& i
== This
->color0_reg
)
3523 || (reg_maps
->shader_version
.major
< 2 && !i
))
3526 sprintf(srgbtmp
[found
], "R%u", i
);
3528 if (found
== 4) break;
3533 sprintf(srgbtmp
[0], "TA");
3534 sprintf(srgbtmp
[1], "TB");
3535 sprintf(srgbtmp
[2], "TC");
3536 sprintf(srgbtmp
[3], "TD");
3540 sprintf(srgbtmp
[1], "TA");
3541 sprintf(srgbtmp
[2], "TB");
3542 sprintf(srgbtmp
[3], "TC");
3545 sprintf(srgbtmp
[2], "TA");
3546 sprintf(srgbtmp
[3], "TB");
3549 sprintf(srgbtmp
[3], "TA");
3555 /* Create the hw ARB shader */
3556 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
3557 priv_ctx
.cur_ps_args
= args
;
3558 priv_ctx
.compiled_fprog
= compiled
;
3559 priv_ctx
.cur_np2fixup_info
= &compiled
->np2fixup_info
;
3560 init_ps_input(This
, args
, &priv_ctx
);
3561 list_init(&priv_ctx
.control_frames
);
3563 /* Avoid enabling NV_fragment_program* if we do not need it.
3565 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
3566 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
3567 * is faster than what we gain from using higher native instructions. There are some things though
3568 * that cannot be emulated. In that case enable the extensions.
3569 * If the extension is enabled, instruction handlers that support both ways will use it.
3571 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
3572 * So enable the best we can get.
3574 if(reg_maps
->usesdsx
|| reg_maps
->usesdsy
|| reg_maps
->loop_depth
> 0 || reg_maps
->usestexldd
||
3575 reg_maps
->usestexldl
|| reg_maps
->usesfacing
|| reg_maps
->usesifc
|| reg_maps
->usescall
)
3577 want_nv_prog
= TRUE
;
3580 shader_addline(buffer
, "!!ARBfp1.0\n");
3581 if (want_nv_prog
&& gl_info
->supported
[NV_FRAGMENT_PROGRAM2
])
3583 shader_addline(buffer
, "OPTION NV_fragment_program2;\n");
3584 priv_ctx
.target_version
= NV3
;
3586 else if (want_nv_prog
&& gl_info
->supported
[NV_FRAGMENT_PROGRAM_OPTION
])
3588 shader_addline(buffer
, "OPTION NV_fragment_program;\n");
3589 priv_ctx
.target_version
= NV2
;
3593 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
3596 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
3599 priv_ctx
.target_version
= ARB
;
3602 if (reg_maps
->highest_render_target
> 0)
3604 shader_addline(buffer
, "OPTION ARB_draw_buffers;\n");
3607 if (reg_maps
->shader_version
.major
< 3)
3609 switch(args
->super
.fog
) {
3613 shader_addline(buffer
, "OPTION ARB_fog_linear;\n");
3616 shader_addline(buffer
, "OPTION ARB_fog_exp;\n");
3619 shader_addline(buffer
, "OPTION ARB_fog_exp2;\n");
3624 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
3625 * unused temps away(but occupies them for the whole shader if they're used once). Always
3626 * declaring them avoids tricky bookkeeping work
3628 shader_addline(buffer
, "TEMP TA;\n"); /* Used for modifiers */
3629 shader_addline(buffer
, "TEMP TB;\n"); /* Used for modifiers */
3630 shader_addline(buffer
, "TEMP TC;\n"); /* Used for modifiers */
3631 if(dcl_td
) shader_addline(buffer
, "TEMP TD;\n"); /* Used for sRGB writing */
3632 shader_addline(buffer
, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
3633 shader_addline(buffer
, "PARAM coefmul = { 2, 4, 8, 16 };\n");
3634 shader_addline(buffer
, "PARAM ps_helper_const = { 0.0, 1.0, %1.10f, 0.0 };\n", eps
);
3636 if (reg_maps
->shader_version
.major
< 2)
3638 strcpy(fragcolor
, "R0");
3640 if(args
->super
.srgb_correction
) {
3641 if(This
->color0_mov
) {
3642 sprintf(fragcolor
, "R%u", This
->color0_reg
);
3644 shader_addline(buffer
, "TEMP TMP_COLOR;\n");
3645 strcpy(fragcolor
, "TMP_COLOR");
3648 strcpy(fragcolor
, "result.color");
3652 if(args
->super
.srgb_correction
) {
3653 shader_addline(buffer
, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
3654 srgb_mul_low
, srgb_cmp
, srgb_pow
, srgb_mul_high
);
3655 shader_addline(buffer
, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
3656 srgb_sub_high
, 0.0, 0.0, 0.0);
3659 /* Base Declarations */
3660 next_local
= shader_generate_arb_declarations((IWineD3DBaseShaderImpl
*)This
,
3661 reg_maps
, buffer
, gl_info
, lconst_map
, NULL
, &priv_ctx
);
3663 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
3665 if (!(map
& 1)) continue;
3667 cur
= compiled
->numbumpenvmatconsts
;
3668 compiled
->bumpenvmatconst
[cur
].const_num
= WINED3D_CONST_NUM_UNUSED
;
3669 compiled
->bumpenvmatconst
[cur
].texunit
= i
;
3670 compiled
->luminanceconst
[cur
].const_num
= WINED3D_CONST_NUM_UNUSED
;
3671 compiled
->luminanceconst
[cur
].texunit
= i
;
3673 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3674 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3675 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3676 * textures due to conditional NP2 restrictions)
3678 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3679 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3680 * their location is shader dependent anyway and they cannot be loaded globally.
3682 compiled
->bumpenvmatconst
[cur
].const_num
= next_local
++;
3683 shader_addline(buffer
, "PARAM bumpenvmat%d = program.local[%d];\n",
3684 i
, compiled
->bumpenvmatconst
[cur
].const_num
);
3685 compiled
->numbumpenvmatconsts
= cur
+ 1;
3687 if (!(reg_maps
->luminanceparams
& (1 << i
))) continue;
3689 compiled
->luminanceconst
[cur
].const_num
= next_local
++;
3690 shader_addline(buffer
, "PARAM luminance%d = program.local[%d];\n",
3691 i
, compiled
->luminanceconst
[cur
].const_num
);
3694 for(i
= 0; i
< MAX_CONST_I
; i
++)
3696 compiled
->int_consts
[i
] = WINED3D_CONST_NUM_UNUSED
;
3697 if (reg_maps
->integer_constants
& (1 << i
) && priv_ctx
.target_version
>= NV2
)
3699 const DWORD
*control_values
= find_loop_control_values((IWineD3DBaseShaderImpl
*) This
, i
);
3703 shader_addline(buffer
, "PARAM I%u = {%u, %u, %u, -1};\n", i
,
3704 control_values
[0], control_values
[1], control_values
[2]);
3708 compiled
->int_consts
[i
] = next_local
;
3709 compiled
->num_int_consts
++;
3710 shader_addline(buffer
, "PARAM I%u = program.local[%u];\n", i
, next_local
++);
3715 if(reg_maps
->vpos
|| reg_maps
->usesdsy
)
3717 compiled
->ycorrection
= next_local
;
3718 shader_addline(buffer
, "PARAM ycorrection = program.local[%u];\n", next_local
++);
3722 shader_addline(buffer
, "TEMP vpos;\n");
3723 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3724 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3725 * ycorrection.z: 1.0
3726 * ycorrection.w: 0.0
3728 shader_addline(buffer
, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3729 shader_addline(buffer
, "FLR vpos.xy, vpos;\n");
3734 compiled
->ycorrection
= WINED3D_CONST_NUM_UNUSED
;
3737 /* Load constants to fixup NP2 texcoords if there are still free constants left:
3738 * Constants (texture dimensions) for the NP2 fixup are loaded as local program parameters. This will consume
3739 * at most 8 (MAX_FRAGMENT_SAMPLERS / 2) parameters, which is highly unlikely, since the application had to
3740 * use 16 NP2 textures at the same time. In case that we run out of constants the fixup is simply not
3741 * applied / activated. This will probably result in wrong rendering of the texture, but will save us from
3742 * shader compilation errors and the subsequent errors when drawing with this shader. */
3743 if (priv_ctx
.cur_ps_args
->super
.np2_fixup
) {
3745 struct arb_ps_np2fixup_info
* const fixup
= priv_ctx
.cur_np2fixup_info
;
3746 const WORD map
= priv_ctx
.cur_ps_args
->super
.np2_fixup
;
3747 const UINT max_lconsts
= gl_info
->limits
.arb_ps_local_constants
;
3749 fixup
->offset
= next_local
;
3750 fixup
->super
.active
= 0;
3753 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
3754 if (!(map
& (1 << i
))) continue;
3756 if (fixup
->offset
+ (cur
>> 1) < max_lconsts
) {
3757 fixup
->super
.active
|= (1 << i
);
3758 fixup
->super
.idx
[i
] = cur
++;
3760 FIXME("No free constant found to load NP2 fixup data into shader. "
3761 "Sampling from this texture will probably look wrong.\n");
3766 fixup
->super
.num_consts
= (cur
+ 1) >> 1;
3767 if (fixup
->super
.num_consts
) {
3768 shader_addline(buffer
, "PARAM np2fixup[%u] = { program.env[%u..%u] };\n",
3769 fixup
->super
.num_consts
, fixup
->offset
, fixup
->super
.num_consts
+ fixup
->offset
- 1);
3772 next_local
+= fixup
->super
.num_consts
;
3775 if (shader_priv
->clipplane_emulation
!= ~0U && args
->clip
)
3777 shader_addline(buffer
, "KIL fragment.texcoord[%u];\n", shader_priv
->clipplane_emulation
);
3780 /* Base Shader Body */
3781 shader_generate_main((IWineD3DBaseShaderImpl
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
3783 if(args
->super
.srgb_correction
) {
3784 arbfp_add_sRGB_correction(buffer
, fragcolor
, srgbtmp
[0], srgbtmp
[1], srgbtmp
[2], srgbtmp
[3],
3785 priv_ctx
.target_version
>= NV2
);
3788 if(strcmp(fragcolor
, "result.color")) {
3789 shader_addline(buffer
, "MOV result.color, %s;\n", fragcolor
);
3791 shader_addline(buffer
, "END\n");
3793 /* TODO: change to resource.glObjectHandle or something like that */
3794 GL_EXTCALL(glGenProgramsARB(1, &retval
));
3796 TRACE("Creating a hw pixel shader, prg=%d\n", retval
);
3797 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, retval
));
3799 TRACE("Created hw pixel shader, prg=%d\n", retval
);
3800 /* Create the program and check for errors */
3801 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
3802 buffer
->bsize
, buffer
->buffer
));
3803 checkGLcall("glProgramStringARB()");
3805 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
3808 FIXME("HW PixelShader Error at position %d: %s\n\n",
3809 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3810 shader_arb_dump_program_source(buffer
->buffer
);
3817 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
3818 checkGLcall("glGetProgramivARB()");
3819 if (!native
) WARN("Program exceeds native resource limits.\n");
3822 /* Load immediate constants */
3824 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
3825 const float *value
= (const float *)lconst
->value
;
3826 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, lconst_map
[lconst
->idx
], value
));
3827 checkGLcall("glProgramLocalParameter4fvARB");
3829 HeapFree(GetProcessHeap(), 0, lconst_map
);
3835 static int compare_sig(const struct wined3d_shader_signature_element
*sig1
, const struct wined3d_shader_signature_element
*sig2
)
3840 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3842 if (!sig1
[i
].semantic_name
|| !sig2
[i
].semantic_name
)
3844 /* Compare pointers, not contents. One string is NULL(element does not exist), the other one is not NULL */
3845 if(sig1
[i
].semantic_name
!= sig2
[i
].semantic_name
) return sig1
[i
].semantic_name
< sig2
[i
].semantic_name
? -1 : 1;
3849 if ((ret
= strcmp(sig1
[i
].semantic_name
, sig2
[i
].semantic_name
))) return ret
;
3850 if(sig1
[i
].semantic_idx
!= sig2
[i
].semantic_idx
) return sig1
[i
].semantic_idx
< sig2
[i
].semantic_idx
? -1 : 1;
3851 if(sig1
[i
].sysval_semantic
!= sig2
[i
].sysval_semantic
) return sig1
[i
].sysval_semantic
< sig2
[i
].sysval_semantic
? -1 : 1;
3852 if(sig1
[i
].component_type
!= sig2
[i
].component_type
) return sig1
[i
].sysval_semantic
< sig2
[i
].component_type
? -1 : 1;
3853 if(sig1
[i
].register_idx
!= sig2
[i
].register_idx
) return sig1
[i
].register_idx
< sig2
[i
].register_idx
? -1 : 1;
3854 if(sig1
[i
].mask
!= sig2
->mask
) return sig1
[i
].mask
< sig2
[i
].mask
? -1 : 1;
3859 static struct wined3d_shader_signature_element
*clone_sig(const struct wined3d_shader_signature_element
*sig
)
3861 struct wined3d_shader_signature_element
*new;
3865 new = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*new) * MAX_REG_INPUT
);
3866 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3868 if (!sig
[i
].semantic_name
) continue;
3871 /* Clone the semantic string */
3872 name
= HeapAlloc(GetProcessHeap(), 0, strlen(sig
[i
].semantic_name
) + 1);
3873 strcpy(name
, sig
[i
].semantic_name
);
3874 new[i
].semantic_name
= name
;
3879 static DWORD
find_input_signature(struct shader_arb_priv
*priv
, const struct wined3d_shader_signature_element
*sig
)
3881 struct wine_rb_entry
*entry
= wine_rb_get(&priv
->signature_tree
, sig
);
3882 struct ps_signature
*found_sig
;
3886 found_sig
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
3887 TRACE("Found existing signature %u\n", found_sig
->idx
);
3888 return found_sig
->idx
;
3890 found_sig
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*sig
));
3891 found_sig
->sig
= clone_sig(sig
);
3892 found_sig
->idx
= priv
->ps_sig_number
++;
3893 TRACE("New signature stored and assigned number %u\n", found_sig
->idx
);
3894 if(wine_rb_put(&priv
->signature_tree
, sig
, &found_sig
->entry
) == -1)
3896 ERR("Failed to insert program entry.\n");
3898 return found_sig
->idx
;
3901 static void init_output_registers(IWineD3DBaseShaderImpl
*shader
, DWORD sig_num
,
3902 struct shader_arb_ctx_priv
*priv_ctx
, struct arb_vs_compiled_shader
*compiled
)
3905 static const char * const texcoords
[8] =
3907 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3908 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
3910 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
3911 IWineD3DBaseShaderClass
*baseshader
= &shader
->baseShader
;
3912 const struct wined3d_shader_signature_element
*sig
;
3913 const char *semantic_name
;
3914 DWORD semantic_idx
, reg_idx
;
3916 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
3917 * and varying 9 to result.color.secondary
3919 static const char * const decl_idx_to_string
[MAX_REG_INPUT
] =
3921 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3922 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]",
3923 "result.color.primary", "result.color.secondary"
3928 TRACE("Pixel shader uses builtin varyings\n");
3929 /* Map builtins to builtins */
3930 for(i
= 0; i
< 8; i
++)
3932 priv_ctx
->texcrd_output
[i
] = texcoords
[i
];
3934 priv_ctx
->color_output
[0] = "result.color.primary";
3935 priv_ctx
->color_output
[1] = "result.color.secondary";
3936 priv_ctx
->fog_output
= "result.fogcoord";
3938 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
3939 for (i
= 0; i
< (sizeof(baseshader
->output_signature
) / sizeof(*baseshader
->output_signature
)); ++i
)
3941 semantic_name
= baseshader
->output_signature
[i
].semantic_name
;
3942 if (!semantic_name
) continue;
3944 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3946 TRACE("o%u is TMP_OUT\n", i
);
3947 if (!baseshader
->output_signature
[i
].semantic_idx
) priv_ctx
->vs_output
[i
] = "TMP_OUT";
3948 else priv_ctx
->vs_output
[i
] = "TA";
3950 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3952 TRACE("o%u is result.pointsize\n", i
);
3953 if (!baseshader
->output_signature
[i
].semantic_idx
) priv_ctx
->vs_output
[i
] = "result.pointsize";
3954 else priv_ctx
->vs_output
[i
] = "TA";
3956 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3958 TRACE("o%u is result.color.?, idx %u\n", i
, baseshader
->output_signature
[i
].semantic_idx
);
3959 if (!baseshader
->output_signature
[i
].semantic_idx
)
3960 priv_ctx
->vs_output
[i
] = "result.color.primary";
3961 else if (baseshader
->output_signature
[i
].semantic_idx
== 1)
3962 priv_ctx
->vs_output
[i
] = "result.color.secondary";
3963 else priv_ctx
->vs_output
[i
] = "TA";
3965 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3967 TRACE("o%u is %s\n", i
, texcoords
[baseshader
->output_signature
[i
].semantic_idx
]);
3968 if (baseshader
->output_signature
[i
].semantic_idx
>= 8) priv_ctx
->vs_output
[i
] = "TA";
3969 else priv_ctx
->vs_output
[i
] = texcoords
[baseshader
->output_signature
[i
].semantic_idx
];
3971 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3973 TRACE("o%u is result.fogcoord\n", i
);
3974 if (baseshader
->output_signature
[i
].semantic_idx
> 0) priv_ctx
->vs_output
[i
] = "TA";
3975 else priv_ctx
->vs_output
[i
] = "result.fogcoord";
3979 priv_ctx
->vs_output
[i
] = "TA";
3985 /* Instead of searching for the signature in the signature list, read the one from the current pixel shader.
3986 * Its maybe not the shader where the signature came from, but it is the same signature and faster to find
3988 sig
= device
->stateBlock
->state
.pixel_shader
->baseShader
.input_signature
;
3989 TRACE("Pixel shader uses declared varyings\n");
3991 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
3992 for(i
= 0; i
< 8; i
++)
3994 priv_ctx
->texcrd_output
[i
] = "TA";
3996 priv_ctx
->color_output
[0] = "TA";
3997 priv_ctx
->color_output
[1] = "TA";
3998 priv_ctx
->fog_output
= "TA";
4000 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
4002 semantic_name
= sig
[i
].semantic_name
;
4003 semantic_idx
= sig
[i
].semantic_idx
;
4004 reg_idx
= sig
[i
].register_idx
;
4005 if (!semantic_name
) continue;
4007 /* If a declared input register is not written by builtin arguments, don't write to it.
4008 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
4010 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
4011 * to TMP_OUT in any case
4013 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
4015 if(semantic_idx
< 8) priv_ctx
->texcrd_output
[semantic_idx
] = decl_idx_to_string
[reg_idx
];
4017 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
4019 if(semantic_idx
< 2) priv_ctx
->color_output
[semantic_idx
] = decl_idx_to_string
[reg_idx
];
4021 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
4023 if (!semantic_idx
) priv_ctx
->fog_output
= decl_idx_to_string
[reg_idx
];
4030 if (!strcmp(decl_idx_to_string
[reg_idx
], "result.color.primary")
4031 || !strcmp(decl_idx_to_string
[reg_idx
], "result.color.secondary"))
4033 compiled
->need_color_unclamp
= TRUE
;
4037 /* Map declared to declared */
4038 for (i
= 0; i
< (sizeof(baseshader
->output_signature
) / sizeof(*baseshader
->output_signature
)); ++i
)
4040 /* Write unread output to TA to throw them away */
4041 priv_ctx
->vs_output
[i
] = "TA";
4042 semantic_name
= baseshader
->output_signature
[i
].semantic_name
;
4043 if (!semantic_name
) continue;
4045 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
)
4046 && !baseshader
->output_signature
[i
].semantic_idx
)
4048 priv_ctx
->vs_output
[i
] = "TMP_OUT";
4051 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
)
4052 && !baseshader
->output_signature
[i
].semantic_idx
)
4054 priv_ctx
->vs_output
[i
] = "result.pointsize";
4058 for(j
= 0; j
< MAX_REG_INPUT
; j
++)
4060 if (!sig
[j
].semantic_name
) continue;
4062 if (!strcmp(sig
[j
].semantic_name
, semantic_name
)
4063 && sig
[j
].semantic_idx
== baseshader
->output_signature
[i
].semantic_idx
)
4065 priv_ctx
->vs_output
[i
] = decl_idx_to_string
[sig
[j
].register_idx
];
4067 if (!strcmp(priv_ctx
->vs_output
[i
], "result.color.primary")
4068 || !strcmp(priv_ctx
->vs_output
[i
], "result.color.secondary"))
4070 compiled
->need_color_unclamp
= TRUE
;
4077 /* GL locking is done by the caller */
4078 static GLuint
shader_arb_generate_vshader(IWineD3DBaseShaderImpl
*shader
,
4079 const struct wined3d_gl_info
*gl_info
, struct wined3d_shader_buffer
*buffer
,
4080 const struct arb_vs_compile_args
*args
, struct arb_vs_compiled_shader
*compiled
)
4082 const struct arb_vshader_private
*shader_data
= shader
->baseShader
.backend_data
;
4083 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->baseShader
.reg_maps
;
4084 const DWORD
*function
= shader
->baseShader
.function
;
4085 const local_constant
*lconst
;
4087 DWORD next_local
, *lconst_map
= local_const_mapping(shader
);
4088 struct shader_arb_ctx_priv priv_ctx
;
4092 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4093 priv_ctx
.cur_vs_args
= args
;
4094 list_init(&priv_ctx
.control_frames
);
4095 init_output_registers(shader
, args
->ps_signature
, &priv_ctx
, compiled
);
4097 /* Create the hw ARB shader */
4098 shader_addline(buffer
, "!!ARBvp1.0\n");
4100 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
4101 * mesurable performance penalty, and we can always make use of it for clipplanes.
4103 if (gl_info
->supported
[NV_VERTEX_PROGRAM3
])
4105 shader_addline(buffer
, "OPTION NV_vertex_program3;\n");
4106 priv_ctx
.target_version
= NV3
;
4107 shader_addline(buffer
, "ADDRESS aL;\n");
4109 else if (gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
])
4111 shader_addline(buffer
, "OPTION NV_vertex_program2;\n");
4112 priv_ctx
.target_version
= NV2
;
4113 shader_addline(buffer
, "ADDRESS aL;\n");
4115 priv_ctx
.target_version
= ARB
;
4118 shader_addline(buffer
, "TEMP TMP_OUT;\n");
4119 if (need_helper_const(shader_data
, reg_maps
, gl_info
))
4121 shader_addline(buffer
, "PARAM helper_const = { 0.0, 1.0, 2.0, %1.10f};\n", eps
);
4123 if (need_rel_addr_const(shader_data
, reg_maps
, gl_info
))
4125 shader_addline(buffer
, "PARAM rel_addr_const = { 0.5, %d.0, 0.0, 0.0 };\n", shader_data
->rel_offset
);
4126 shader_addline(buffer
, "TEMP A0_SHADOW;\n");
4129 shader_addline(buffer
, "TEMP TA;\n");
4131 /* Base Declarations */
4132 next_local
= shader_generate_arb_declarations(shader
, reg_maps
, buffer
,
4133 gl_info
, lconst_map
, &priv_ctx
.vs_clipplanes
, &priv_ctx
);
4135 for(i
= 0; i
< MAX_CONST_I
; i
++)
4137 compiled
->int_consts
[i
] = WINED3D_CONST_NUM_UNUSED
;
4138 if(reg_maps
->integer_constants
& (1 << i
) && priv_ctx
.target_version
>= NV2
)
4140 const DWORD
*control_values
= find_loop_control_values(shader
, i
);
4144 shader_addline(buffer
, "PARAM I%u = {%u, %u, %u, -1};\n", i
,
4145 control_values
[0], control_values
[1], control_values
[2]);
4149 compiled
->int_consts
[i
] = next_local
;
4150 compiled
->num_int_consts
++;
4151 shader_addline(buffer
, "PARAM I%u = program.local[%u];\n", i
, next_local
++);
4156 /* We need a constant to fixup the final position */
4157 shader_addline(buffer
, "PARAM posFixup = program.local[%u];\n", next_local
);
4158 compiled
->pos_fixup
= next_local
++;
4160 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
4161 * for output parameters. D3D in theory does not do that either, but some applications depend on a
4162 * proper initialization of the secondary color, and programs using the fixed function pipeline without
4163 * a replacement shader depend on the texcoord.w being set properly.
4165 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
4166 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
4167 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
4168 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
4169 * this can eat a number of instructions, so skip it unless this cap is set as well
4171 if (!gl_info
->supported
[NV_VERTEX_PROGRAM
])
4173 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
4174 const char *color_init
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_0001
);
4175 shader_addline(buffer
, "MOV result.color.secondary, %s;\n", color_init
);
4177 if (gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
&& !device
->frag_pipe
->ffp_proj_control
)
4180 const char *one
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_ONE
);
4181 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++)
4183 if (reg_maps
->texcoord_mask
[i
] && reg_maps
->texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
4184 shader_addline(buffer
, "MOV result.texcoord[%u].w, %s\n", i
, one
);
4189 /* The shader starts with the main function */
4190 priv_ctx
.in_main_func
= TRUE
;
4191 /* Base Shader Body */
4192 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
4194 if (!priv_ctx
.footer_written
) vshader_add_footer(&priv_ctx
,
4195 shader_data
, args
, reg_maps
, gl_info
, buffer
);
4197 shader_addline(buffer
, "END\n");
4199 /* TODO: change to resource.glObjectHandle or something like that */
4200 GL_EXTCALL(glGenProgramsARB(1, &ret
));
4202 TRACE("Creating a hw vertex shader, prg=%d\n", ret
);
4203 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, ret
));
4205 TRACE("Created hw vertex shader, prg=%d\n", ret
);
4206 /* Create the program and check for errors */
4207 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
4208 buffer
->bsize
, buffer
->buffer
));
4209 checkGLcall("glProgramStringARB()");
4211 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
4214 FIXME("HW VertexShader Error at position %d: %s\n\n",
4215 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
4216 shader_arb_dump_program_source(buffer
->buffer
);
4223 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
4224 checkGLcall("glGetProgramivARB()");
4225 if (!native
) WARN("Program exceeds native resource limits.\n");
4227 /* Load immediate constants */
4230 LIST_FOR_EACH_ENTRY(lconst
, &shader
->baseShader
.constantsF
, local_constant
, entry
)
4232 const float *value
= (const float *)lconst
->value
;
4233 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, lconst_map
[lconst
->idx
], value
));
4237 HeapFree(GetProcessHeap(), 0, lconst_map
);
4242 /* GL locking is done by the caller */
4243 static struct arb_ps_compiled_shader
*find_arb_pshader(IWineD3DPixelShaderImpl
*shader
, const struct arb_ps_compile_args
*args
)
4245 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
4246 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4249 struct arb_ps_compiled_shader
*new_array
;
4250 struct wined3d_shader_buffer buffer
;
4251 struct arb_pshader_private
*shader_data
;
4254 if (!shader
->baseShader
.backend_data
)
4256 struct shader_arb_priv
*priv
= device
->shader_priv
;
4258 shader
->baseShader
.backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4259 shader_data
= shader
->baseShader
.backend_data
;
4260 shader_data
->clamp_consts
= shader
->baseShader
.reg_maps
.shader_version
.major
== 1;
4262 if(shader
->baseShader
.reg_maps
.shader_version
.major
< 3) shader_data
->input_signature_idx
= ~0;
4263 else shader_data
->input_signature_idx
= find_input_signature(priv
, shader
->baseShader
.input_signature
);
4265 shader_data
->has_signature_idx
= TRUE
;
4266 TRACE("Shader got assigned input signature index %u\n", shader_data
->input_signature_idx
);
4268 if (!device
->vs_clipping
)
4269 shader_data
->clipplane_emulation
= shader_find_free_input_register(&shader
->baseShader
.reg_maps
,
4270 gl_info
->limits
.texture_stages
- 1);
4272 shader_data
->clipplane_emulation
= ~0U;
4274 shader_data
= shader
->baseShader
.backend_data
;
4276 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4277 * so a linear search is more performant than a hashmap or a binary search
4278 * (cache coherency etc)
4280 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4282 if (!memcmp(&shader_data
->gl_shaders
[i
].args
, args
, sizeof(*args
)))
4283 return &shader_data
->gl_shaders
[i
];
4286 TRACE("No matching GL shader found, compiling a new shader\n");
4287 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4288 if (shader_data
->num_gl_shaders
)
4290 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4291 new_array
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, shader_data
->gl_shaders
,
4292 new_size
* sizeof(*shader_data
->gl_shaders
));
4294 new_array
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
->gl_shaders
));
4299 ERR("Out of memory\n");
4302 shader_data
->gl_shaders
= new_array
;
4303 shader_data
->shader_array_size
= new_size
;
4306 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4308 pixelshader_update_samplers(&shader
->baseShader
.reg_maps
, device
->stateBlock
->state
.textures
);
4310 if (!shader_buffer_init(&buffer
))
4312 ERR("Failed to initialize shader buffer.\n");
4316 ret
= shader_arb_generate_pshader(shader
, gl_info
, &buffer
, args
,
4317 &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
]);
4318 shader_buffer_free(&buffer
);
4319 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].prgId
= ret
;
4321 return &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++];
4324 static inline BOOL
vs_args_equal(const struct arb_vs_compile_args
*stored
, const struct arb_vs_compile_args
*new,
4325 const DWORD use_map
, BOOL skip_int
) {
4326 if((stored
->super
.swizzle_map
& use_map
) != new->super
.swizzle_map
) return FALSE
;
4327 if(stored
->super
.clip_enabled
!= new->super
.clip_enabled
) return FALSE
;
4328 if(stored
->super
.fog_src
!= new->super
.fog_src
) return FALSE
;
4329 if(stored
->clip
.boolclip_compare
!= new->clip
.boolclip_compare
) return FALSE
;
4330 if(stored
->ps_signature
!= new->ps_signature
) return FALSE
;
4331 if(stored
->vertex
.samplers_compare
!= new->vertex
.samplers_compare
) return FALSE
;
4332 if(skip_int
) return TRUE
;
4334 return !memcmp(stored
->loop_ctrl
, new->loop_ctrl
, sizeof(stored
->loop_ctrl
));
4337 static struct arb_vs_compiled_shader
*find_arb_vshader(IWineD3DBaseShaderImpl
*shader
,
4338 const struct arb_vs_compile_args
*args
)
4340 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
4341 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4342 DWORD use_map
= device
->strided_streams
.use_map
;
4345 struct arb_vs_compiled_shader
*new_array
;
4346 struct wined3d_shader_buffer buffer
;
4347 struct arb_vshader_private
*shader_data
;
4350 if (!shader
->baseShader
.backend_data
)
4352 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->baseShader
.reg_maps
;
4354 shader
->baseShader
.backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4355 shader_data
= shader
->baseShader
.backend_data
;
4357 if ((gl_info
->quirks
& WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT
)
4358 && reg_maps
->min_rel_offset
<= reg_maps
->max_rel_offset
)
4360 if (reg_maps
->max_rel_offset
- reg_maps
->min_rel_offset
> 127)
4362 FIXME("The difference between the minimum and maximum relative offset is > 127.\n");
4363 FIXME("Which this OpenGL implementation does not support. Try using GLSL.\n");
4364 FIXME("Min: %u, Max: %u.\n", reg_maps
->min_rel_offset
, reg_maps
->max_rel_offset
);
4366 else if (reg_maps
->max_rel_offset
- reg_maps
->min_rel_offset
> 63)
4367 shader_data
->rel_offset
= reg_maps
->min_rel_offset
+ 63;
4368 else if (reg_maps
->max_rel_offset
> 63)
4369 shader_data
->rel_offset
= reg_maps
->min_rel_offset
;
4372 shader_data
= shader
->baseShader
.backend_data
;
4374 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4375 * so a linear search is more performant than a hashmap or a binary search
4376 * (cache coherency etc)
4378 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4379 if (vs_args_equal(&shader_data
->gl_shaders
[i
].args
, args
,
4380 use_map
, gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
]))
4382 return &shader_data
->gl_shaders
[i
];
4386 TRACE("No matching GL shader found, compiling a new shader\n");
4388 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4389 if (shader_data
->num_gl_shaders
)
4391 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4392 new_array
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, shader_data
->gl_shaders
,
4393 new_size
* sizeof(*shader_data
->gl_shaders
));
4395 new_array
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
->gl_shaders
));
4400 ERR("Out of memory\n");
4403 shader_data
->gl_shaders
= new_array
;
4404 shader_data
->shader_array_size
= new_size
;
4407 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4409 if (!shader_buffer_init(&buffer
))
4411 ERR("Failed to initialize shader buffer.\n");
4415 ret
= shader_arb_generate_vshader(shader
, gl_info
, &buffer
, args
,
4416 &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
]);
4417 shader_buffer_free(&buffer
);
4418 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].prgId
= ret
;
4420 return &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++];
4423 static void find_arb_ps_compile_args(const struct wined3d_state
*state
,
4424 IWineD3DPixelShaderImpl
*shader
, struct arb_ps_compile_args
*args
)
4426 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
4427 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4431 find_ps_compile_args(state
, shader
, &args
->super
);
4433 /* This forces all local boolean constants to 1 to make them stateblock independent */
4434 args
->bools
= shader
->baseShader
.reg_maps
.local_bool_consts
;
4436 for(i
= 0; i
< MAX_CONST_B
; i
++)
4438 if (state
->ps_consts_b
[i
])
4439 args
->bools
|= ( 1 << i
);
4442 /* Only enable the clip plane emulation KIL if at least one clipplane is enabled. The KIL instruction
4443 * is quite expensive because it forces the driver to disable early Z discards. It is cheaper to
4444 * duplicate the shader than have a no-op KIL instruction in every shader
4446 if (!device
->vs_clipping
&& use_vs(state
)
4447 && state
->render_states
[WINED3DRS_CLIPPING
]
4448 && state
->render_states
[WINED3DRS_CLIPPLANEENABLE
])
4453 /* Skip if unused or local, or supported natively */
4454 int_skip
= ~shader
->baseShader
.reg_maps
.integer_constants
| shader
->baseShader
.reg_maps
.local_int_consts
;
4455 if (int_skip
== 0xffff || gl_info
->supported
[NV_FRAGMENT_PROGRAM_OPTION
])
4457 memset(&args
->loop_ctrl
, 0, sizeof(args
->loop_ctrl
));
4461 for(i
= 0; i
< MAX_CONST_I
; i
++)
4463 if(int_skip
& (1 << i
))
4465 args
->loop_ctrl
[i
][0] = 0;
4466 args
->loop_ctrl
[i
][1] = 0;
4467 args
->loop_ctrl
[i
][2] = 0;
4471 args
->loop_ctrl
[i
][0] = state
->ps_consts_i
[i
* 4];
4472 args
->loop_ctrl
[i
][1] = state
->ps_consts_i
[i
* 4 + 1];
4473 args
->loop_ctrl
[i
][2] = state
->ps_consts_i
[i
* 4 + 2];
4478 static void find_arb_vs_compile_args(const struct wined3d_state
*state
,
4479 IWineD3DBaseShaderImpl
*shader
, struct arb_vs_compile_args
*args
)
4481 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
4482 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4486 find_vs_compile_args(state
, shader
, &args
->super
);
4488 args
->clip
.boolclip_compare
= 0;
4491 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
4492 struct arb_pshader_private
*shader_priv
= ps
->baseShader
.backend_data
;
4493 args
->ps_signature
= shader_priv
->input_signature_idx
;
4495 args
->clip
.boolclip
.clip_texcoord
= shader_priv
->clipplane_emulation
+ 1;
4499 args
->ps_signature
= ~0;
4500 if (!device
->vs_clipping
)
4502 args
->clip
.boolclip
.clip_texcoord
= ffp_clip_emul(state
) ? gl_info
->limits
.texture_stages
: 0;
4504 /* Otherwise: Setting boolclip_compare set clip_texcoord to 0 */
4507 if (args
->clip
.boolclip
.clip_texcoord
)
4509 if (state
->render_states
[WINED3DRS_CLIPPING
])
4510 args
->clip
.boolclip
.clipplane_mask
= (unsigned char)state
->render_states
[WINED3DRS_CLIPPLANEENABLE
];
4511 /* clipplane_mask was set to 0 by setting boolclip_compare to 0 */
4514 /* This forces all local boolean constants to 1 to make them stateblock independent */
4515 args
->clip
.boolclip
.bools
= shader
->baseShader
.reg_maps
.local_bool_consts
;
4516 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
4517 for(i
= 0; i
< MAX_CONST_B
; i
++)
4519 if (state
->vs_consts_b
[i
])
4520 args
->clip
.boolclip
.bools
|= ( 1 << i
);
4523 args
->vertex
.samplers
[0] = device
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 0];
4524 args
->vertex
.samplers
[1] = device
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 1];
4525 args
->vertex
.samplers
[2] = device
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 2];
4526 args
->vertex
.samplers
[3] = 0;
4528 /* Skip if unused or local */
4529 int_skip
= ~shader
->baseShader
.reg_maps
.integer_constants
| shader
->baseShader
.reg_maps
.local_int_consts
;
4530 /* This is about flow control, not clipping. */
4531 if (int_skip
== 0xffff || gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
])
4533 memset(&args
->loop_ctrl
, 0, sizeof(args
->loop_ctrl
));
4537 for(i
= 0; i
< MAX_CONST_I
; i
++)
4539 if(int_skip
& (1 << i
))
4541 args
->loop_ctrl
[i
][0] = 0;
4542 args
->loop_ctrl
[i
][1] = 0;
4543 args
->loop_ctrl
[i
][2] = 0;
4547 args
->loop_ctrl
[i
][0] = state
->vs_consts_i
[i
* 4];
4548 args
->loop_ctrl
[i
][1] = state
->vs_consts_i
[i
* 4 + 1];
4549 args
->loop_ctrl
[i
][2] = state
->vs_consts_i
[i
* 4 + 2];
4554 /* GL locking is done by the caller */
4555 static void shader_arb_select(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
)
4557 IWineD3DDeviceImpl
*This
= context
->swapchain
->device
;
4558 struct shader_arb_priv
*priv
= This
->shader_priv
;
4559 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4560 const struct wined3d_state
*state
= &This
->stateBlock
->state
;
4563 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
4566 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
4567 struct arb_ps_compile_args compile_args
;
4568 struct arb_ps_compiled_shader
*compiled
;
4570 TRACE("Using pixel shader %p.\n", ps
);
4571 find_arb_ps_compile_args(state
, ps
, &compile_args
);
4572 compiled
= find_arb_pshader(ps
, &compile_args
);
4573 priv
->current_fprogram_id
= compiled
->prgId
;
4574 priv
->compiled_fprog
= compiled
;
4576 /* Bind the fragment program */
4577 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
4578 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
4580 if(!priv
->use_arbfp_fixed_func
) {
4581 /* Enable OpenGL fragment programs */
4582 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
4583 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
4585 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
4587 /* Pixel Shader 1.x constants are clamped to [-1;1], Pixel Shader 2.0 constants are not. If switching between
4588 * a 1.x and newer shader, reload the first 8 constants
4590 if(priv
->last_ps_const_clamped
!= ((struct arb_pshader_private
*)ps
->baseShader
.backend_data
)->clamp_consts
)
4592 priv
->last_ps_const_clamped
= ((struct arb_pshader_private
*)ps
->baseShader
.backend_data
)->clamp_consts
;
4593 This
->highest_dirty_ps_const
= max(This
->highest_dirty_ps_const
, 8);
4594 for(i
= 0; i
< 8; i
++)
4596 context
->pshader_const_dirty
[i
] = 1;
4598 /* Also takes care of loading local constants */
4599 shader_arb_load_constants(context
, TRUE
, FALSE
);
4603 float rt_height
= This
->render_targets
[0]->resource
.height
;
4604 shader_arb_ps_local_constants(compiled
, context
, state
, rt_height
);
4607 /* Force constant reloading for the NP2 fixup (see comment in shader_glsl_select for more info) */
4608 if (compiled
->np2fixup_info
.super
.active
)
4609 shader_arb_load_np2fixup_constants(priv
, gl_info
, state
);
4611 else if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
] && !priv
->use_arbfp_fixed_func
)
4613 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
4614 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
4615 * replacement shader
4617 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
4618 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4619 priv
->current_fprogram_id
= 0;
4624 IWineD3DBaseShaderImpl
*vs
= state
->vertex_shader
;
4625 struct arb_vs_compile_args compile_args
;
4626 struct arb_vs_compiled_shader
*compiled
;
4628 TRACE("Using vertex shader %p\n", vs
);
4629 find_arb_vs_compile_args(state
, vs
, &compile_args
);
4630 compiled
= find_arb_vshader(vs
, &compile_args
);
4631 priv
->current_vprogram_id
= compiled
->prgId
;
4632 priv
->compiled_vprog
= compiled
;
4634 /* Bind the vertex program */
4635 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
4636 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
4638 /* Enable OpenGL vertex programs */
4639 glEnable(GL_VERTEX_PROGRAM_ARB
);
4640 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
4641 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
4642 shader_arb_vs_local_constants(compiled
, context
, state
);
4644 if(priv
->last_vs_color_unclamp
!= compiled
->need_color_unclamp
) {
4645 priv
->last_vs_color_unclamp
= compiled
->need_color_unclamp
;
4647 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
4649 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, !compiled
->need_color_unclamp
));
4650 checkGLcall("glClampColorARB");
4652 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4656 else if (gl_info
->supported
[ARB_VERTEX_PROGRAM
])
4658 priv
->current_vprogram_id
= 0;
4659 glDisable(GL_VERTEX_PROGRAM_ARB
);
4660 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4664 /* GL locking is done by the caller */
4665 static void shader_arb_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
4666 enum tex_types tex_type
, const SIZE
*ds_mask_size
)
4668 const float mask
[] = {0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
};
4669 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
4670 struct shader_arb_priv
*priv
= shader_priv
;
4671 GLuint
*blt_fprogram
;
4673 if (!priv
->depth_blt_vprogram_id
) priv
->depth_blt_vprogram_id
= create_arb_blt_vertex_program(gl_info
);
4674 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->depth_blt_vprogram_id
));
4675 glEnable(GL_VERTEX_PROGRAM_ARB
);
4677 blt_fprogram
= masked
? &priv
->depth_blt_fprogram_id_masked
[tex_type
] : &priv
->depth_blt_fprogram_id_full
[tex_type
];
4678 if (!*blt_fprogram
) *blt_fprogram
= create_arb_blt_fragment_program(gl_info
, tex_type
, masked
);
4679 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, *blt_fprogram
));
4680 if (masked
) GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0, mask
));
4681 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
4684 /* GL locking is done by the caller */
4685 static void shader_arb_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
)
4687 struct shader_arb_priv
*priv
= shader_priv
;
4689 if (priv
->current_vprogram_id
) {
4690 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
4691 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
4693 TRACE("Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB.\n", priv
->current_vprogram_id
);
4697 glDisable(GL_VERTEX_PROGRAM_ARB
);
4698 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4701 if (priv
->current_fprogram_id
) {
4702 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
4703 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
4705 TRACE("Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB.\n", priv
->current_fprogram_id
);
4707 else if(!priv
->use_arbfp_fixed_func
)
4709 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
4710 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4714 static void shader_arb_destroy(IWineD3DBaseShaderImpl
*shader
)
4716 IWineD3DDeviceImpl
*device
= shader
->baseShader
.device
;
4717 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4719 if (shader_is_pshader_version(shader
->baseShader
.reg_maps
.shader_version
.type
))
4721 struct arb_pshader_private
*shader_data
= shader
->baseShader
.backend_data
;
4724 if(!shader_data
) return; /* This can happen if a shader was never compiled */
4726 if (shader_data
->num_gl_shaders
)
4728 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4731 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4733 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data
->gl_shaders
[i
].prgId
));
4734 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4738 context_release(context
);
4741 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4742 HeapFree(GetProcessHeap(), 0, shader_data
);
4743 shader
->baseShader
.backend_data
= NULL
;
4747 struct arb_vshader_private
*shader_data
= shader
->baseShader
.backend_data
;
4750 if(!shader_data
) return; /* This can happen if a shader was never compiled */
4752 if (shader_data
->num_gl_shaders
)
4754 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4757 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4759 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data
->gl_shaders
[i
].prgId
));
4760 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4764 context_release(context
);
4767 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4768 HeapFree(GetProcessHeap(), 0, shader_data
);
4769 shader
->baseShader
.backend_data
= NULL
;
4773 static int sig_tree_compare(const void *key
, const struct wine_rb_entry
*entry
)
4775 struct ps_signature
*e
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
4776 return compare_sig(key
, e
->sig
);
4779 static const struct wine_rb_functions sig_tree_functions
=
4787 static HRESULT
shader_arb_alloc(IWineD3DDeviceImpl
*device
)
4789 struct shader_arb_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*priv
));
4790 if(wine_rb_init(&priv
->signature_tree
, &sig_tree_functions
) == -1)
4792 ERR("RB tree init failed\n");
4793 HeapFree(GetProcessHeap(), 0, priv
);
4794 return E_OUTOFMEMORY
;
4796 device
->shader_priv
= priv
;
4800 static void release_signature(struct wine_rb_entry
*entry
, void *context
)
4802 struct ps_signature
*sig
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
4804 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
4806 HeapFree(GetProcessHeap(), 0, (char *) sig
->sig
[i
].semantic_name
);
4808 HeapFree(GetProcessHeap(), 0, sig
->sig
);
4809 HeapFree(GetProcessHeap(), 0, sig
);
4812 /* Context activation is done by the caller. */
4813 static void shader_arb_free(IWineD3DDeviceImpl
*device
)
4815 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4816 struct shader_arb_priv
*priv
= device
->shader_priv
;
4820 if(priv
->depth_blt_vprogram_id
) {
4821 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_vprogram_id
));
4823 for (i
= 0; i
< tex_type_count
; ++i
)
4825 if (priv
->depth_blt_fprogram_id_full
[i
])
4827 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id_full
[i
]));
4829 if (priv
->depth_blt_fprogram_id_masked
[i
])
4831 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id_masked
[i
]));
4836 wine_rb_destroy(&priv
->signature_tree
, release_signature
, NULL
);
4837 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
4840 static BOOL
shader_arb_dirty_const(void)
4845 static void shader_arb_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*pCaps
)
4847 if (gl_info
->supported
[ARB_VERTEX_PROGRAM
])
4851 /* 96 is the minimum allowed value of MAX_PROGRAM_ENV_PARAMETERS_ARB
4852 * for vertex programs. If the native limit is less than that it's
4853 * not very useful, and e.g. Mesa swrast returns 0, probably to
4854 * indicate it's a software implementation. */
4855 if (gl_info
->limits
.arb_vs_native_constants
< 96)
4856 vs_consts
= gl_info
->limits
.arb_vs_float_constants
;
4858 vs_consts
= min(gl_info
->limits
.arb_vs_float_constants
, gl_info
->limits
.arb_vs_native_constants
);
4860 if (gl_info
->supported
[NV_VERTEX_PROGRAM3
])
4862 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
4863 TRACE_(d3d_caps
)("Hardware vertex shader version 3.0 enabled (NV_VERTEX_PROGRAM3)\n");
4865 else if (vs_consts
>= 256)
4867 /* Shader Model 2.0 requires at least 256 vertex shader constants */
4868 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
4869 TRACE_(d3d_caps
)("Hardware vertex shader version 2.0 enabled (ARB_PROGRAM)\n");
4873 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(1,1);
4874 TRACE_(d3d_caps
)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
4876 pCaps
->MaxVertexShaderConst
= vs_consts
;
4880 pCaps
->VertexShaderVersion
= 0;
4881 pCaps
->MaxVertexShaderConst
= 0;
4884 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
4888 /* Similar as above for vertex programs, but the minimum for fragment
4889 * programs is 24. */
4890 if (gl_info
->limits
.arb_ps_native_constants
< 24)
4891 ps_consts
= gl_info
->limits
.arb_ps_float_constants
;
4893 ps_consts
= min(gl_info
->limits
.arb_ps_float_constants
, gl_info
->limits
.arb_ps_native_constants
);
4895 if (gl_info
->supported
[NV_FRAGMENT_PROGRAM2
])
4897 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
4898 TRACE_(d3d_caps
)("Hardware pixel shader version 3.0 enabled (NV_FRAGMENT_PROGRAM2)\n");
4900 else if (ps_consts
>= 32)
4902 /* Shader Model 2.0 requires at least 32 pixel shader constants */
4903 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
4904 TRACE_(d3d_caps
)("Hardware pixel shader version 2.0 enabled (ARB_PROGRAM)\n");
4908 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(1,4);
4909 TRACE_(d3d_caps
)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
4911 pCaps
->PixelShader1xMaxValue
= 8.0f
;
4912 pCaps
->MaxPixelShaderConst
= ps_consts
;
4916 pCaps
->PixelShaderVersion
= 0;
4917 pCaps
->PixelShader1xMaxValue
= 0.0f
;
4918 pCaps
->MaxPixelShaderConst
= 0;
4921 pCaps
->VSClipping
= use_nv_clip(gl_info
);
4924 static BOOL
shader_arb_color_fixup_supported(struct color_fixup_desc fixup
)
4926 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
4928 TRACE("Checking support for color_fixup:\n");
4929 dump_color_fixup_desc(fixup
);
4932 /* We support everything except complex conversions. */
4933 if (!is_complex_fixup(fixup
))
4939 TRACE("[FAILED]\n");
4943 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
) {
4945 char write_mask
[20], regstr
[50];
4946 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
4947 BOOL is_color
= FALSE
;
4948 const struct wined3d_shader_dst_param
*dst
;
4950 if (!ins
->dst_count
) return;
4954 if (!shift
) return; /* Saturate alone is handled by the instructions */
4956 shader_arb_get_write_mask(ins
, dst
, write_mask
);
4957 shader_arb_get_register_name(ins
, &dst
->reg
, regstr
, &is_color
);
4959 /* Generate a line that does the output modifier computation
4960 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
4961 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
4963 shader_addline(buffer
, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins
),
4964 regstr
, write_mask
, regstr
, shift_tab
[shift
]);
4967 static const SHADER_HANDLER shader_arb_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
4969 /* WINED3DSIH_ABS */ shader_hw_map2gl
,
4970 /* WINED3DSIH_ADD */ shader_hw_map2gl
,
4971 /* WINED3DSIH_AND */ NULL
,
4972 /* WINED3DSIH_BEM */ pshader_hw_bem
,
4973 /* WINED3DSIH_BREAK */ shader_hw_break
,
4974 /* WINED3DSIH_BREAKC */ shader_hw_breakc
,
4975 /* WINED3DSIH_BREAKP */ NULL
,
4976 /* WINED3DSIH_CALL */ shader_hw_call
,
4977 /* WINED3DSIH_CALLNZ */ NULL
,
4978 /* WINED3DSIH_CMP */ pshader_hw_cmp
,
4979 /* WINED3DSIH_CND */ pshader_hw_cnd
,
4980 /* WINED3DSIH_CRS */ shader_hw_map2gl
,
4981 /* WINED3DSIH_CUT */ NULL
,
4982 /* WINED3DSIH_DCL */ NULL
,
4983 /* WINED3DSIH_DEF */ NULL
,
4984 /* WINED3DSIH_DEFB */ NULL
,
4985 /* WINED3DSIH_DEFI */ NULL
,
4986 /* WINED3DSIH_DIV */ NULL
,
4987 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add
,
4988 /* WINED3DSIH_DP3 */ shader_hw_map2gl
,
4989 /* WINED3DSIH_DP4 */ shader_hw_map2gl
,
4990 /* WINED3DSIH_DST */ shader_hw_map2gl
,
4991 /* WINED3DSIH_DSX */ shader_hw_map2gl
,
4992 /* WINED3DSIH_DSY */ shader_hw_dsy
,
4993 /* WINED3DSIH_ELSE */ shader_hw_else
,
4994 /* WINED3DSIH_EMIT */ NULL
,
4995 /* WINED3DSIH_ENDIF */ shader_hw_endif
,
4996 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop
,
4997 /* WINED3DSIH_ENDREP */ shader_hw_endrep
,
4998 /* WINED3DSIH_EXP */ shader_hw_scalar_op
,
4999 /* WINED3DSIH_EXPP */ shader_hw_scalar_op
,
5000 /* WINED3DSIH_FRC */ shader_hw_map2gl
,
5001 /* WINED3DSIH_FTOI */ NULL
,
5002 /* WINED3DSIH_IADD */ NULL
,
5003 /* WINED3DSIH_IEQ */ NULL
,
5004 /* WINED3DSIH_IF */ NULL
/* Hardcoded into the shader */,
5005 /* WINED3DSIH_IFC */ shader_hw_ifc
,
5006 /* WINED3DSIH_IGE */ NULL
,
5007 /* WINED3DSIH_IMUL */ NULL
,
5008 /* WINED3DSIH_ITOF */ NULL
,
5009 /* WINED3DSIH_LABEL */ shader_hw_label
,
5010 /* WINED3DSIH_LD */ NULL
,
5011 /* WINED3DSIH_LIT */ shader_hw_map2gl
,
5012 /* WINED3DSIH_LOG */ shader_hw_log_pow
,
5013 /* WINED3DSIH_LOGP */ shader_hw_log_pow
,
5014 /* WINED3DSIH_LOOP */ shader_hw_loop
,
5015 /* WINED3DSIH_LRP */ shader_hw_lrp
,
5016 /* WINED3DSIH_LT */ NULL
,
5017 /* WINED3DSIH_M3x2 */ shader_hw_mnxn
,
5018 /* WINED3DSIH_M3x3 */ shader_hw_mnxn
,
5019 /* WINED3DSIH_M3x4 */ shader_hw_mnxn
,
5020 /* WINED3DSIH_M4x3 */ shader_hw_mnxn
,
5021 /* WINED3DSIH_M4x4 */ shader_hw_mnxn
,
5022 /* WINED3DSIH_MAD */ shader_hw_map2gl
,
5023 /* WINED3DSIH_MAX */ shader_hw_map2gl
,
5024 /* WINED3DSIH_MIN */ shader_hw_map2gl
,
5025 /* WINED3DSIH_MOV */ shader_hw_mov
,
5026 /* WINED3DSIH_MOVA */ shader_hw_mov
,
5027 /* WINED3DSIH_MOVC */ NULL
,
5028 /* WINED3DSIH_MUL */ shader_hw_map2gl
,
5029 /* WINED3DSIH_NOP */ shader_hw_nop
,
5030 /* WINED3DSIH_NRM */ shader_hw_nrm
,
5031 /* WINED3DSIH_PHASE */ NULL
,
5032 /* WINED3DSIH_POW */ shader_hw_log_pow
,
5033 /* WINED3DSIH_RCP */ shader_hw_rcp
,
5034 /* WINED3DSIH_REP */ shader_hw_rep
,
5035 /* WINED3DSIH_RET */ shader_hw_ret
,
5036 /* WINED3DSIH_RSQ */ shader_hw_scalar_op
,
5037 /* WINED3DSIH_SAMPLE */ NULL
,
5038 /* WINED3DSIH_SAMPLE_GRAD */ NULL
,
5039 /* WINED3DSIH_SAMPLE_LOD */ NULL
,
5040 /* WINED3DSIH_SETP */ NULL
,
5041 /* WINED3DSIH_SGE */ shader_hw_map2gl
,
5042 /* WINED3DSIH_SGN */ shader_hw_sgn
,
5043 /* WINED3DSIH_SINCOS */ shader_hw_sincos
,
5044 /* WINED3DSIH_SLT */ shader_hw_map2gl
,
5045 /* WINED3DSIH_SQRT */ NULL
,
5046 /* WINED3DSIH_SUB */ shader_hw_map2gl
,
5047 /* WINED3DSIH_TEX */ pshader_hw_tex
,
5048 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem
,
5049 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem
,
5050 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord
,
5051 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth
,
5052 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3
,
5053 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex
,
5054 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill
,
5055 /* WINED3DSIH_TEXLDD */ shader_hw_texldd
,
5056 /* WINED3DSIH_TEXLDL */ shader_hw_texldl
,
5057 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth
,
5058 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad
,
5059 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex
,
5060 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3
,
5061 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
5062 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad
,
5063 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec
,
5064 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex
,
5065 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec
,
5066 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar
,
5067 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb
,
5068 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb
,
5069 /* WINED3DSIH_UTOF */ NULL
,
5072 static inline BOOL
get_bool_const(const struct wined3d_shader_instruction
*ins
, IWineD3DBaseShaderImpl
*This
, DWORD idx
)
5074 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5075 BOOL vshader
= shader_is_vshader_version(reg_maps
->shader_version
.type
);
5077 WORD flag
= (1 << idx
);
5078 const local_constant
*constant
;
5079 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5081 if (reg_maps
->local_bool_consts
& flag
)
5083 /* What good is a if(bool) with a hardcoded local constant? I don't know, but handle it */
5084 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsB
, local_constant
, entry
)
5086 if (constant
->idx
== idx
)
5088 return constant
->value
[0];
5091 ERR("Local constant not found\n");
5096 if(vshader
) bools
= priv
->cur_vs_args
->clip
.boolclip
.bools
;
5097 else bools
= priv
->cur_ps_args
->bools
;
5098 return bools
& flag
;
5102 static void get_loop_control_const(const struct wined3d_shader_instruction
*ins
,
5103 IWineD3DBaseShaderImpl
*This
, UINT idx
, struct wined3d_shader_loop_control
*loop_control
)
5105 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5106 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5108 /* Integer constants can either be a local constant, or they can be stored in the shader
5109 * type specific compile args. */
5110 if (reg_maps
->local_int_consts
& (1 << idx
))
5112 const local_constant
*constant
;
5114 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsI
, local_constant
, entry
)
5116 if (constant
->idx
== idx
)
5118 loop_control
->count
= constant
->value
[0];
5119 loop_control
->start
= constant
->value
[1];
5120 /* Step is signed. */
5121 loop_control
->step
= (int)constant
->value
[2];
5125 /* If this happens the flag was set incorrectly */
5126 ERR("Local constant not found\n");
5127 loop_control
->count
= 0;
5128 loop_control
->start
= 0;
5129 loop_control
->step
= 0;
5133 switch (reg_maps
->shader_version
.type
)
5135 case WINED3D_SHADER_TYPE_VERTEX
:
5136 /* Count and aL start value are unsigned */
5137 loop_control
->count
= priv
->cur_vs_args
->loop_ctrl
[idx
][0];
5138 loop_control
->start
= priv
->cur_vs_args
->loop_ctrl
[idx
][1];
5139 /* Step is signed. */
5140 loop_control
->step
= ((char)priv
->cur_vs_args
->loop_ctrl
[idx
][2]);
5143 case WINED3D_SHADER_TYPE_PIXEL
:
5144 loop_control
->count
= priv
->cur_ps_args
->loop_ctrl
[idx
][0];
5145 loop_control
->start
= priv
->cur_ps_args
->loop_ctrl
[idx
][1];
5146 loop_control
->step
= ((char)priv
->cur_ps_args
->loop_ctrl
[idx
][2]);
5150 FIXME("Unhandled shader type %#x.\n", reg_maps
->shader_version
.type
);
5155 static void record_instruction(struct list
*list
, const struct wined3d_shader_instruction
*ins
)
5158 struct wined3d_shader_dst_param
*dst_param
= NULL
;
5159 struct wined3d_shader_src_param
*src_param
= NULL
, *rel_addr
= NULL
;
5160 struct recorded_instruction
*rec
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*rec
));
5163 ERR("Out of memory\n");
5168 dst_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param
));
5169 if(!dst_param
) goto free
;
5170 *dst_param
= *ins
->dst
;
5171 if(ins
->dst
->reg
.rel_addr
)
5173 rel_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param
->reg
.rel_addr
));
5174 if(!rel_addr
) goto free
;
5175 *rel_addr
= *ins
->dst
->reg
.rel_addr
;
5176 dst_param
->reg
.rel_addr
= rel_addr
;
5178 rec
->ins
.dst
= dst_param
;
5180 src_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*src_param
) * ins
->src_count
);
5181 if(!src_param
) goto free
;
5182 for(i
= 0; i
< ins
->src_count
; i
++)
5184 src_param
[i
] = ins
->src
[i
];
5185 if(ins
->src
[i
].reg
.rel_addr
)
5187 rel_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr
));
5188 if(!rel_addr
) goto free
;
5189 *rel_addr
= *ins
->src
[i
].reg
.rel_addr
;
5190 src_param
[i
].reg
.rel_addr
= rel_addr
;
5193 rec
->ins
.src
= src_param
;
5194 list_add_tail(list
, &rec
->entry
);
5198 ERR("Out of memory\n");
5201 HeapFree(GetProcessHeap(), 0, (void *) dst_param
->reg
.rel_addr
);
5202 HeapFree(GetProcessHeap(), 0, dst_param
);
5206 for(i
= 0; i
< ins
->src_count
; i
++)
5208 HeapFree(GetProcessHeap(), 0, (void *) src_param
[i
].reg
.rel_addr
);
5210 HeapFree(GetProcessHeap(), 0, src_param
);
5212 HeapFree(GetProcessHeap(), 0, rec
);
5215 static void free_recorded_instruction(struct list
*list
)
5217 struct recorded_instruction
*rec_ins
, *entry2
;
5220 LIST_FOR_EACH_ENTRY_SAFE(rec_ins
, entry2
, list
, struct recorded_instruction
, entry
)
5222 list_remove(&rec_ins
->entry
);
5223 if(rec_ins
->ins
.dst
)
5225 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.dst
->reg
.rel_addr
);
5226 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.dst
);
5228 if(rec_ins
->ins
.src
)
5230 for(i
= 0; i
< rec_ins
->ins
.src_count
; i
++)
5232 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.src
[i
].reg
.rel_addr
);
5234 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.src
);
5236 HeapFree(GetProcessHeap(), 0, rec_ins
);
5240 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
5241 SHADER_HANDLER hw_fct
;
5242 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5243 struct IWineD3DBaseShaderImpl
*shader
= ins
->ctx
->shader
;
5244 struct control_frame
*control_frame
;
5245 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
5248 if(ins
->handler_idx
== WINED3DSIH_LOOP
|| ins
->handler_idx
== WINED3DSIH_REP
)
5250 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
5251 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
5253 if(ins
->handler_idx
== WINED3DSIH_LOOP
) control_frame
->type
= LOOP
;
5254 if(ins
->handler_idx
== WINED3DSIH_REP
) control_frame
->type
= REP
;
5256 if(priv
->target_version
>= NV2
)
5258 control_frame
->no
.loop
= priv
->num_loops
++;
5263 /* Don't bother recording when we're in a not used if branch */
5269 if(!priv
->recording
)
5271 list_init(&priv
->record
);
5272 priv
->recording
= TRUE
;
5273 control_frame
->outer_loop
= TRUE
;
5274 get_loop_control_const(ins
, shader
, ins
->src
[0].reg
.idx
, &control_frame
->loop_control
);
5275 return; /* Instruction is handled */
5277 /* Record this loop in the outer loop's recording */
5280 else if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
|| ins
->handler_idx
== WINED3DSIH_ENDREP
)
5282 if(priv
->target_version
>= NV2
)
5284 /* Nothing to do. The control frame is popped after the HW instr handler */
5288 struct list
*e
= list_head(&priv
->control_frames
);
5289 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5290 list_remove(&control_frame
->entry
);
5292 if(control_frame
->outer_loop
)
5294 unsigned int iteration
;
5298 /* Turn off recording before playback */
5299 priv
->recording
= FALSE
;
5301 /* Move the recorded instructions to a separate list and get them out of the private data
5302 * structure. If there are nested loops, the shader_arb_handle_instruction below will
5303 * be recorded again, thus priv->record might be overwritten
5306 list_move_tail(©
, &priv
->record
);
5307 list_init(&priv
->record
);
5309 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
5311 shader_addline(buffer
, "#unrolling loop: %u iterations, aL=%u, inc %d\n",
5312 control_frame
->loop_control
.count
, control_frame
->loop_control
.start
,
5313 control_frame
->loop_control
.step
);
5314 aL
= control_frame
->loop_control
.start
;
5318 shader_addline(buffer
, "#unrolling rep: %u iterations\n", control_frame
->loop_control
.count
);
5321 for (iteration
= 0; iteration
< control_frame
->loop_control
.count
; ++iteration
)
5323 struct recorded_instruction
*rec_ins
;
5324 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
5327 shader_addline(buffer
, "#Iteration %u, aL=%d\n", iteration
, aL
);
5331 shader_addline(buffer
, "#Iteration %u\n", iteration
);
5334 LIST_FOR_EACH_ENTRY(rec_ins
, ©
, struct recorded_instruction
, entry
)
5336 shader_arb_handle_instruction(&rec_ins
->ins
);
5339 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
5341 aL
+= control_frame
->loop_control
.step
;
5344 shader_addline(buffer
, "#end loop/rep\n");
5346 free_recorded_instruction(©
);
5347 HeapFree(GetProcessHeap(), 0, control_frame
);
5348 return; /* Instruction is handled */
5352 /* This is a nested loop. Proceed to the normal recording function */
5353 HeapFree(GetProcessHeap(), 0, control_frame
);
5360 record_instruction(&priv
->record
, ins
);
5365 if(ins
->handler_idx
== WINED3DSIH_IF
)
5367 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
5368 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
5369 control_frame
->type
= IF
;
5371 bool_const
= get_bool_const(ins
, shader
, ins
->src
[0].reg
.idx
);
5372 if(ins
->src
[0].modifiers
== WINED3DSPSM_NOT
) bool_const
= !bool_const
;
5373 if (!priv
->muted
&& !bool_const
)
5375 shader_addline(buffer
, "#if(FALSE){\n");
5377 control_frame
->muting
= TRUE
;
5379 else shader_addline(buffer
, "#if(TRUE) {\n");
5381 return; /* Instruction is handled */
5383 else if(ins
->handler_idx
== WINED3DSIH_IFC
)
5385 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
5386 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
5387 control_frame
->type
= IFC
;
5388 control_frame
->no
.ifc
= priv
->num_ifcs
++;
5389 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
5391 else if(ins
->handler_idx
== WINED3DSIH_ELSE
)
5393 struct list
*e
= list_head(&priv
->control_frames
);
5394 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5396 if(control_frame
->type
== IF
)
5398 shader_addline(buffer
, "#} else {\n");
5399 if(!priv
->muted
&& !control_frame
->muting
)
5402 control_frame
->muting
= TRUE
;
5404 else if(control_frame
->muting
) priv
->muted
= FALSE
;
5405 return; /* Instruction is handled. */
5407 /* In case of an ifc, generate a HW shader instruction */
5409 else if(ins
->handler_idx
== WINED3DSIH_ENDIF
)
5411 struct list
*e
= list_head(&priv
->control_frames
);
5412 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5414 if(control_frame
->type
== IF
)
5416 shader_addline(buffer
, "#} endif\n");
5417 if(control_frame
->muting
) priv
->muted
= FALSE
;
5418 list_remove(&control_frame
->entry
);
5419 HeapFree(GetProcessHeap(), 0, control_frame
);
5420 return; /* Instruction is handled */
5424 if(priv
->muted
) return;
5426 /* Select handler */
5427 hw_fct
= shader_arb_instruction_handler_table
[ins
->handler_idx
];
5429 /* Unhandled opcode */
5432 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
5437 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
|| ins
->handler_idx
== WINED3DSIH_ENDREP
)
5439 struct list
*e
= list_head(&priv
->control_frames
);
5440 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5441 list_remove(&control_frame
->entry
);
5442 HeapFree(GetProcessHeap(), 0, control_frame
);
5445 else if(ins
->handler_idx
== WINED3DSIH_ENDIF
)
5447 /* Non-ifc ENDIFs don't reach that place because of the return in the if block above */
5448 struct list
*e
= list_head(&priv
->control_frames
);
5449 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5450 list_remove(&control_frame
->entry
);
5451 HeapFree(GetProcessHeap(), 0, control_frame
);
5455 shader_arb_add_instruction_modifiers(ins
);
5458 const shader_backend_t arb_program_shader_backend
= {
5459 shader_arb_handle_instruction
,
5461 shader_arb_select_depth_blt
,
5462 shader_arb_deselect_depth_blt
,
5463 shader_arb_update_float_vertex_constants
,
5464 shader_arb_update_float_pixel_constants
,
5465 shader_arb_load_constants
,
5466 shader_arb_load_np2fixup_constants
,
5470 shader_arb_dirty_const
,
5471 shader_arb_get_caps
,
5472 shader_arb_color_fixup_supported
,
5475 /* ARB_fragment_program fixed function pipeline replacement definitions */
5476 #define ARB_FFP_CONST_TFACTOR 0
5477 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
5478 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
5479 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
5480 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
5482 struct arbfp_ffp_desc
5484 struct ffp_frag_desc parent
;
5486 unsigned int num_textures_used
;
5489 /* Context activation is done by the caller. */
5490 static void arbfp_enable(BOOL enable
)
5494 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
5495 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
5497 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
5498 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
5503 static HRESULT
arbfp_alloc(IWineD3DDeviceImpl
*device
)
5505 struct shader_arb_priv
*priv
;
5506 /* Share private data between the shader backend and the pipeline replacement, if both
5507 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
5508 * if no pixel shader is bound or not
5510 if (device
->shader_backend
== &arb_program_shader_backend
)
5512 device
->fragment_priv
= device
->shader_priv
;
5516 device
->fragment_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_arb_priv
));
5517 if (!device
->fragment_priv
) return E_OUTOFMEMORY
;
5519 priv
= device
->fragment_priv
;
5520 if (wine_rb_init(&priv
->fragment_shaders
, &wined3d_ffp_frag_program_rb_functions
) == -1)
5522 ERR("Failed to initialize rbtree.\n");
5523 HeapFree(GetProcessHeap(), 0, device
->fragment_priv
);
5524 return E_OUTOFMEMORY
;
5526 priv
->use_arbfp_fixed_func
= TRUE
;
5530 /* Context activation is done by the caller. */
5531 static void arbfp_free_ffpshader(struct wine_rb_entry
*entry
, void *context
)
5533 const struct wined3d_gl_info
*gl_info
= context
;
5534 struct arbfp_ffp_desc
*entry_arb
= WINE_RB_ENTRY_VALUE(entry
, struct arbfp_ffp_desc
, parent
.entry
);
5537 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb
->shader
));
5538 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
5539 HeapFree(GetProcessHeap(), 0, entry_arb
);
5543 /* Context activation is done by the caller. */
5544 static void arbfp_free(IWineD3DDeviceImpl
*device
)
5546 struct shader_arb_priv
*priv
= device
->fragment_priv
;
5548 wine_rb_destroy(&priv
->fragment_shaders
, arbfp_free_ffpshader
, &device
->adapter
->gl_info
);
5549 priv
->use_arbfp_fixed_func
= FALSE
;
5551 if (device
->shader_backend
!= &arb_program_shader_backend
)
5553 HeapFree(GetProcessHeap(), 0, device
->fragment_priv
);
5557 static void arbfp_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
5559 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
;
5560 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
|
5561 WINED3DTEXOPCAPS_SELECTARG1
|
5562 WINED3DTEXOPCAPS_SELECTARG2
|
5563 WINED3DTEXOPCAPS_MODULATE4X
|
5564 WINED3DTEXOPCAPS_MODULATE2X
|
5565 WINED3DTEXOPCAPS_MODULATE
|
5566 WINED3DTEXOPCAPS_ADDSIGNED2X
|
5567 WINED3DTEXOPCAPS_ADDSIGNED
|
5568 WINED3DTEXOPCAPS_ADD
|
5569 WINED3DTEXOPCAPS_SUBTRACT
|
5570 WINED3DTEXOPCAPS_ADDSMOOTH
|
5571 WINED3DTEXOPCAPS_BLENDCURRENTALPHA
|
5572 WINED3DTEXOPCAPS_BLENDFACTORALPHA
|
5573 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
|
5574 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
|
5575 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
|
5576 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
|
5577 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
|
5578 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
|
5579 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
|
5580 WINED3DTEXOPCAPS_DOTPRODUCT3
|
5581 WINED3DTEXOPCAPS_MULTIPLYADD
|
5582 WINED3DTEXOPCAPS_LERP
|
5583 WINED3DTEXOPCAPS_BUMPENVMAP
|
5584 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
5586 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
5588 caps
->MaxTextureBlendStages
= 8;
5589 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, 8);
5592 static void state_texfactor_arbfp(DWORD state_id
,
5593 struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
5595 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5596 const struct wined3d_state
*state
= &stateblock
->state
;
5597 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5600 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5601 * application provided constants
5603 if (device
->shader_backend
== &arb_program_shader_backend
)
5605 if (use_ps(state
)) return;
5607 context
->pshader_const_dirty
[ARB_FFP_CONST_TFACTOR
] = 1;
5608 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_TFACTOR
+ 1);
5611 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3DRS_TEXTUREFACTOR
], col
);
5612 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_TFACTOR
, col
));
5613 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
5617 static void state_arb_specularenable(DWORD state_id
,
5618 struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
5620 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5621 const struct wined3d_state
*state
= &stateblock
->state
;
5622 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5625 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5626 * application provided constants
5628 if (device
->shader_backend
== &arb_program_shader_backend
)
5630 if (use_ps(state
)) return;
5632 context
->pshader_const_dirty
[ARB_FFP_CONST_SPECULAR_ENABLE
] = 1;
5633 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_SPECULAR_ENABLE
+ 1);
5636 if (state
->render_states
[WINED3DRS_SPECULARENABLE
])
5638 /* The specular color has no alpha */
5639 col
[0] = 1.0f
; col
[1] = 1.0f
;
5640 col
[2] = 1.0f
; col
[3] = 0.0f
;
5642 col
[0] = 0.0f
; col
[1] = 0.0f
;
5643 col
[2] = 0.0f
; col
[3] = 0.0f
;
5645 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_SPECULAR_ENABLE
, col
));
5646 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
5649 static void set_bumpmat_arbfp(DWORD state_id
, struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
5651 DWORD stage
= (state_id
- STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE
+ 1);
5652 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5653 const struct wined3d_state
*state
= &stateblock
->state
;
5654 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5659 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
5660 if (stage
&& (ps
->baseShader
.reg_maps
.bumpmat
& (1 << stage
)))
5662 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
5665 if (!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
))
5666 stateblock_apply_state(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
5669 if(device
->shader_backend
== &arb_program_shader_backend
) {
5670 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5673 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
5674 context
->pshader_const_dirty
[ARB_FFP_CONST_BUMPMAT(stage
)] = 1;
5675 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_BUMPMAT(stage
) + 1);
5678 mat
[0][0] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT00
]);
5679 mat
[0][1] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT01
]);
5680 mat
[1][0] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT10
]);
5681 mat
[1][1] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT11
]);
5683 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_BUMPMAT(stage
), &mat
[0][0]));
5684 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
5687 static void tex_bumpenvlum_arbfp(DWORD state_id
,
5688 struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
5690 DWORD stage
= (state_id
- STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE
+ 1);
5691 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5692 const struct wined3d_state
*state
= &stateblock
->state
;
5693 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5698 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
5699 if (stage
&& (ps
->baseShader
.reg_maps
.luminanceparams
& (1 << stage
)))
5701 /* The pixel shader has to know the luminance offset. Do a constants update if it
5702 * isn't scheduled anyway
5704 if (!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
))
5705 stateblock_apply_state(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
5708 if(device
->shader_backend
== &arb_program_shader_backend
) {
5709 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5712 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
5713 context
->pshader_const_dirty
[ARB_FFP_CONST_LUMINANCE(stage
)] = 1;
5714 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_LUMINANCE(stage
) + 1);
5717 param
[0] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVLSCALE
]);
5718 param
[1] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVLOFFSET
]);
5722 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_LUMINANCE(stage
), param
));
5723 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
5726 static const char *get_argreg(struct wined3d_shader_buffer
*buffer
, DWORD argnum
, unsigned int stage
, DWORD arg
)
5730 if(arg
== ARG_UNUSED
) return "unused"; /* This is the marker for unused registers */
5732 switch(arg
& WINED3DTA_SELECTMASK
) {
5733 case WINED3DTA_DIFFUSE
:
5734 ret
= "fragment.color.primary"; break;
5736 case WINED3DTA_CURRENT
:
5737 if (!stage
) ret
= "fragment.color.primary";
5741 case WINED3DTA_TEXTURE
:
5743 case 0: ret
= "tex0"; break;
5744 case 1: ret
= "tex1"; break;
5745 case 2: ret
= "tex2"; break;
5746 case 3: ret
= "tex3"; break;
5747 case 4: ret
= "tex4"; break;
5748 case 5: ret
= "tex5"; break;
5749 case 6: ret
= "tex6"; break;
5750 case 7: ret
= "tex7"; break;
5751 default: ret
= "unknown texture";
5755 case WINED3DTA_TFACTOR
:
5756 ret
= "tfactor"; break;
5758 case WINED3DTA_SPECULAR
:
5759 ret
= "fragment.color.secondary"; break;
5761 case WINED3DTA_TEMP
:
5762 ret
= "tempreg"; break;
5764 case WINED3DTA_CONSTANT
:
5765 FIXME("Implement perstage constants\n");
5767 case 0: ret
= "const0"; break;
5768 case 1: ret
= "const1"; break;
5769 case 2: ret
= "const2"; break;
5770 case 3: ret
= "const3"; break;
5771 case 4: ret
= "const4"; break;
5772 case 5: ret
= "const5"; break;
5773 case 6: ret
= "const6"; break;
5774 case 7: ret
= "const7"; break;
5775 default: ret
= "unknown constant";
5783 if(arg
& WINED3DTA_COMPLEMENT
) {
5784 shader_addline(buffer
, "SUB arg%u, const.x, %s;\n", argnum
, ret
);
5785 if(argnum
== 0) ret
= "arg0";
5786 if(argnum
== 1) ret
= "arg1";
5787 if(argnum
== 2) ret
= "arg2";
5789 if(arg
& WINED3DTA_ALPHAREPLICATE
) {
5790 shader_addline(buffer
, "MOV arg%u, %s.w;\n", argnum
, ret
);
5791 if(argnum
== 0) ret
= "arg0";
5792 if(argnum
== 1) ret
= "arg1";
5793 if(argnum
== 2) ret
= "arg2";
5798 static void gen_ffp_instr(struct wined3d_shader_buffer
*buffer
, unsigned int stage
, BOOL color
,
5799 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
5801 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
5802 unsigned int mul
= 1;
5803 BOOL mul_final_dest
= FALSE
;
5805 if(color
&& alpha
) dstmask
= "";
5806 else if(color
) dstmask
= ".xyz";
5807 else dstmask
= ".w";
5809 if(dst
== tempreg
) dstreg
= "tempreg";
5810 else dstreg
= "ret";
5812 arg0
= get_argreg(buffer
, 0, stage
, dw_arg0
);
5813 arg1
= get_argreg(buffer
, 1, stage
, dw_arg1
);
5814 arg2
= get_argreg(buffer
, 2, stage
, dw_arg2
);
5817 case WINED3DTOP_DISABLE
:
5818 if (!stage
) shader_addline(buffer
, "MOV %s%s, fragment.color.primary;\n", dstreg
, dstmask
);
5821 case WINED3DTOP_SELECTARG2
:
5824 case WINED3DTOP_SELECTARG1
:
5825 shader_addline(buffer
, "MOV %s%s, %s;\n", dstreg
, dstmask
, arg1
);
5828 case WINED3DTOP_MODULATE4X
:
5831 case WINED3DTOP_MODULATE2X
:
5833 if (!strcmp(dstreg
, "result.color"))
5836 mul_final_dest
= TRUE
;
5839 case WINED3DTOP_MODULATE
:
5840 shader_addline(buffer
, "MUL %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
5843 case WINED3DTOP_ADDSIGNED2X
:
5845 if (!strcmp(dstreg
, "result.color"))
5848 mul_final_dest
= TRUE
;
5851 case WINED3DTOP_ADDSIGNED
:
5852 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
5855 case WINED3DTOP_ADD
:
5856 shader_addline(buffer
, "ADD_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
5859 case WINED3DTOP_SUBTRACT
:
5860 shader_addline(buffer
, "SUB_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
5863 case WINED3DTOP_ADDSMOOTH
:
5864 shader_addline(buffer
, "SUB arg1, const.x, %s;\n", arg1
);
5865 shader_addline(buffer
, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5868 case WINED3DTOP_BLENDCURRENTALPHA
:
5869 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
5870 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5872 case WINED3DTOP_BLENDFACTORALPHA
:
5873 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
5874 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5876 case WINED3DTOP_BLENDTEXTUREALPHA
:
5877 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5878 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5880 case WINED3DTOP_BLENDDIFFUSEALPHA
:
5881 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
5882 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5885 case WINED3DTOP_BLENDTEXTUREALPHAPM
:
5886 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5887 shader_addline(buffer
, "SUB arg0.w, const.x, %s.w;\n", arg0
);
5888 shader_addline(buffer
, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5891 /* D3DTOP_PREMODULATE ???? */
5893 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR
:
5894 shader_addline(buffer
, "SUB arg0.w, const.x, %s;\n", arg1
);
5895 shader_addline(buffer
, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5897 case WINED3DTOP_MODULATEALPHA_ADDCOLOR
:
5898 shader_addline(buffer
, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
5900 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA
:
5901 shader_addline(buffer
, "SUB arg0, const.x, %s;\n", arg1
);
5902 shader_addline(buffer
, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg
, dstmask
, arg2
, arg1
);
5904 case WINED3DTOP_MODULATECOLOR_ADDALPHA
:
5905 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
5908 case WINED3DTOP_DOTPRODUCT3
:
5910 if (!strcmp(dstreg
, "result.color"))
5913 mul_final_dest
= TRUE
;
5915 shader_addline(buffer
, "SUB arg1, %s, const.w;\n", arg1
);
5916 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
5917 shader_addline(buffer
, "DP3_SAT %s%s, arg1, arg2;\n", dstreg
, dstmask
);
5920 case WINED3DTOP_MULTIPLYADD
:
5921 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg0
);
5924 case WINED3DTOP_LERP
:
5925 /* The msdn is not quite right here */
5926 shader_addline(buffer
, "LRP %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5929 case WINED3DTOP_BUMPENVMAP
:
5930 case WINED3DTOP_BUMPENVMAPLUMINANCE
:
5931 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
5935 FIXME("Unhandled texture op %08x\n", op
);
5939 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
5940 } else if(mul
== 4) {
5941 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
5945 static GLuint
gen_arbfp_ffp_shader(const struct ffp_frag_settings
*settings
, struct wined3d_stateblock
*stateblock
)
5947 const struct wined3d_gl_info
*gl_info
= &stateblock
->device
->adapter
->gl_info
;
5949 struct wined3d_shader_buffer buffer
;
5950 BOOL tex_read
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5951 BOOL bump_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5952 BOOL luminance_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5953 const char *textype
;
5954 const char *instr
, *sat
;
5955 char colorcor_dst
[8];
5957 DWORD arg0
, arg1
, arg2
;
5958 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
5960 const char *final_combiner_src
= "ret";
5963 /* Find out which textures are read */
5964 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
5965 if(settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
) break;
5966 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
5967 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
5968 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
5969 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5970 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5971 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5973 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHA
) tex_read
[stage
] = TRUE
;
5974 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHAPM
) tex_read
[stage
] = TRUE
;
5975 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
) {
5976 bump_used
[stage
] = TRUE
;
5977 tex_read
[stage
] = TRUE
;
5979 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
5980 bump_used
[stage
] = TRUE
;
5981 tex_read
[stage
] = TRUE
;
5982 luminance_used
[stage
] = TRUE
;
5983 } else if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDFACTORALPHA
) {
5984 tfactor_used
= TRUE
;
5987 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
5988 tfactor_used
= TRUE
;
5991 if(settings
->op
[stage
].dst
== tempreg
) tempreg_used
= TRUE
;
5992 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
5993 tempreg_used
= TRUE
;
5996 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) continue;
5997 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
5998 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
5999 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
6000 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
6001 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
6002 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
6004 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
6005 tempreg_used
= TRUE
;
6007 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
6008 tfactor_used
= TRUE
;
6013 if (!shader_buffer_init(&buffer
))
6015 ERR("Failed to initialize shader buffer.\n");
6019 shader_addline(&buffer
, "!!ARBfp1.0\n");
6021 switch(settings
->fog
) {
6022 case FOG_OFF
: break;
6023 case FOG_LINEAR
: shader_addline(&buffer
, "OPTION ARB_fog_linear;\n"); break;
6024 case FOG_EXP
: shader_addline(&buffer
, "OPTION ARB_fog_exp;\n"); break;
6025 case FOG_EXP2
: shader_addline(&buffer
, "OPTION ARB_fog_exp2;\n"); break;
6026 default: FIXME("Unexpected fog setting %d\n", settings
->fog
);
6029 shader_addline(&buffer
, "PARAM const = {1, 2, 4, 0.5};\n");
6030 shader_addline(&buffer
, "TEMP TMP;\n");
6031 shader_addline(&buffer
, "TEMP ret;\n");
6032 if(tempreg_used
|| settings
->sRGB_write
) shader_addline(&buffer
, "TEMP tempreg;\n");
6033 shader_addline(&buffer
, "TEMP arg0;\n");
6034 shader_addline(&buffer
, "TEMP arg1;\n");
6035 shader_addline(&buffer
, "TEMP arg2;\n");
6036 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
6037 if(!tex_read
[stage
]) continue;
6038 shader_addline(&buffer
, "TEMP tex%u;\n", stage
);
6039 if(!bump_used
[stage
]) continue;
6040 shader_addline(&buffer
, "PARAM bumpmat%u = program.env[%u];\n", stage
, ARB_FFP_CONST_BUMPMAT(stage
));
6041 if(!luminance_used
[stage
]) continue;
6042 shader_addline(&buffer
, "PARAM luminance%u = program.env[%u];\n", stage
, ARB_FFP_CONST_LUMINANCE(stage
));
6045 shader_addline(&buffer
, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR
);
6047 shader_addline(&buffer
, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE
);
6049 if(settings
->sRGB_write
) {
6050 shader_addline(&buffer
, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
6051 srgb_mul_low
, srgb_cmp
, srgb_pow
, srgb_mul_high
);
6052 shader_addline(&buffer
, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
6053 srgb_sub_high
, 0.0, 0.0, 0.0);
6056 if (ffp_clip_emul(&stateblock
->state
) && settings
->emul_clipplanes
)
6057 shader_addline(&buffer
, "KIL fragment.texcoord[7];\n");
6059 /* Generate texture sampling instructions) */
6060 for(stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3DTOP_DISABLE
; stage
++) {
6061 if(!tex_read
[stage
]) continue;
6063 switch(settings
->op
[stage
].tex_type
) {
6064 case tex_1d
: textype
= "1D"; break;
6065 case tex_2d
: textype
= "2D"; break;
6066 case tex_3d
: textype
= "3D"; break;
6067 case tex_cube
: textype
= "CUBE"; break;
6068 case tex_rect
: textype
= "RECT"; break;
6069 default: textype
= "unexpected_textype"; break;
6072 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
||
6073 settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
6079 if(settings
->op
[stage
].projected
== proj_none
) {
6081 } else if(settings
->op
[stage
].projected
== proj_count4
||
6082 settings
->op
[stage
].projected
== proj_count3
) {
6085 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
6090 (settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAP
||
6091 settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
)) {
6092 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage
- 1);
6093 shader_addline(&buffer
, "DP3 ret.x, arg1, tex%u;\n", stage
- 1);
6094 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage
- 1);
6095 shader_addline(&buffer
, "DP3 ret.y, arg1, tex%u;\n", stage
- 1);
6097 /* with projective textures, texbem only divides the static texture coord, not the displacement,
6098 * so multiply the displacement with the dividing parameter before passing it to TXP
6100 if (settings
->op
[stage
].projected
!= proj_none
) {
6101 if(settings
->op
[stage
].projected
== proj_count4
) {
6102 shader_addline(&buffer
, "MOV ret.w, fragment.texcoord[%u].w;\n", stage
);
6103 shader_addline(&buffer
, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage
, stage
);
6105 shader_addline(&buffer
, "MOV ret.w, fragment.texcoord[%u].z;\n", stage
);
6106 shader_addline(&buffer
, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage
, stage
);
6109 shader_addline(&buffer
, "ADD ret, ret, fragment.texcoord[%u];\n", stage
);
6112 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
6113 instr
, sat
, stage
, stage
, textype
);
6114 if(settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
6115 shader_addline(&buffer
, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
6116 stage
- 1, stage
- 1, stage
- 1);
6117 shader_addline(&buffer
, "MUL tex%u, tex%u, ret.x;\n", stage
, stage
);
6119 } else if(settings
->op
[stage
].projected
== proj_count3
) {
6120 shader_addline(&buffer
, "MOV ret, fragment.texcoord[%u];\n", stage
);
6121 shader_addline(&buffer
, "MOV ret.w, ret.z;\n");
6122 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
6123 instr
, sat
, stage
, stage
, textype
);
6125 shader_addline(&buffer
, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
6126 instr
, sat
, stage
, stage
, stage
, textype
);
6129 sprintf(colorcor_dst
, "tex%u", stage
);
6130 gen_color_correction(&buffer
, colorcor_dst
, WINED3DSP_WRITEMASK_ALL
, "const.x", "const.y",
6131 settings
->op
[stage
].color_fixup
);
6134 /* Generate the main shader */
6135 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6137 if (settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
)
6139 if (!stage
) final_combiner_src
= "fragment.color.primary";
6143 if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
6144 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
6145 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
6146 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
6147 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
6148 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
6149 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
6150 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
6151 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
6152 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
6153 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
6154 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6156 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
&&
6157 settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
&&
6158 settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
&&
6159 settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6162 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) {
6163 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6164 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6165 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6167 shader_addline(&buffer
, "MOV ret.w, fragment.color.primary.w;\n");
6171 gen_ffp_instr(&buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
6172 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6173 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6175 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6176 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6177 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6178 gen_ffp_instr(&buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
6179 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
6180 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
6184 if(settings
->sRGB_write
) {
6185 shader_addline(&buffer
, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
6186 arbfp_add_sRGB_correction(&buffer
, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE
);
6187 shader_addline(&buffer
, "MOV result.color, ret;\n");
6189 shader_addline(&buffer
, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
6193 shader_addline(&buffer
, "END\n");
6195 /* Generate the shader */
6196 GL_EXTCALL(glGenProgramsARB(1, &ret
));
6197 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
));
6198 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
6199 strlen(buffer
.buffer
), buffer
.buffer
));
6200 checkGLcall("glProgramStringARB()");
6202 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
6205 FIXME("Fragment program error at position %d: %s\n\n", pos
,
6206 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
6207 shader_arb_dump_program_source(buffer
.buffer
);
6213 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
6214 checkGLcall("glGetProgramivARB()");
6215 if (!native
) WARN("Program exceeds native resource limits.\n");
6218 shader_buffer_free(&buffer
);
6222 static void fragment_prog_arbfp(DWORD state_id
, struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
6224 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6225 const struct wined3d_state
*state
= &stateblock
->state
;
6226 IWineD3DDeviceImpl
*device
= stateblock
->device
;
6227 struct shader_arb_priv
*priv
= device
->fragment_priv
;
6228 BOOL use_vshader
= use_vs(state
);
6229 BOOL use_pshader
= use_ps(state
);
6230 struct ffp_frag_settings settings
;
6231 const struct arbfp_ffp_desc
*desc
;
6234 TRACE("state_id %#x, stateblock %p, context %p\n", state_id
, stateblock
, context
);
6236 if(isStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
))) {
6237 if(!use_pshader
&& device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
6238 /* Reload fixed function constants since they collide with the pixel shader constants */
6239 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
6240 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
6242 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
6243 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
6244 } else if(use_pshader
&& !isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
6245 device
->shader_backend
->shader_select(context
, use_pshader
, use_vshader
);
6251 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
6252 gen_ffp_frag_op(stateblock
, &settings
, FALSE
);
6253 desc
= (const struct arbfp_ffp_desc
*)find_ffp_frag_shader(&priv
->fragment_shaders
, &settings
);
6255 struct arbfp_ffp_desc
*new_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc
));
6258 ERR("Out of memory\n");
6261 new_desc
->num_textures_used
= 0;
6262 for (i
= 0; i
< gl_info
->limits
.texture_stages
; ++i
)
6264 if(settings
.op
[i
].cop
== WINED3DTOP_DISABLE
) break;
6265 new_desc
->num_textures_used
= i
;
6268 memcpy(&new_desc
->parent
.settings
, &settings
, sizeof(settings
));
6269 new_desc
->shader
= gen_arbfp_ffp_shader(&settings
, stateblock
);
6270 add_ffp_frag_shader(&priv
->fragment_shaders
, &new_desc
->parent
);
6271 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc
);
6275 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
6276 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
6279 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, desc
->shader
));
6280 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
6281 priv
->current_fprogram_id
= desc
->shader
;
6283 if(device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
6284 /* Reload fixed function constants since they collide with the pixel shader constants */
6285 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
6286 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
6288 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
6289 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
6291 context
->last_was_pshader
= FALSE
;
6293 context
->last_was_pshader
= TRUE
;
6296 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
6297 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
6298 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
6299 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
6300 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
6302 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
6305 if(!isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
6306 device
->shader_backend
->shader_select(context
, use_pshader
, use_vshader
);
6308 if (!isStateDirty(context
, STATE_VERTEXSHADERCONSTANT
) && (use_vshader
|| use_pshader
))
6309 stateblock_apply_state(STATE_VERTEXSHADERCONSTANT
, stateblock
, context
);
6311 if (use_pshader
) stateblock_apply_state(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
6314 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
6315 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
6316 * state table, so we need to handle that with a forwarding function. The other invisible side effect
6317 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
6318 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
6320 static void state_arbfp_fog(DWORD state_id
, struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
6322 const struct wined3d_state
*state
= &stateblock
->state
;
6323 enum fogsource new_source
;
6325 TRACE("state_id %#x, stateblock %p, context %p\n", state_id
, stateblock
, context
);
6327 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
6328 fragment_prog_arbfp(state_id
, stateblock
, context
);
6331 if (!state
->render_states
[WINED3DRS_FOGENABLE
]) return;
6333 if (state
->render_states
[WINED3DRS_FOGTABLEMODE
] == WINED3DFOG_NONE
)
6337 new_source
= FOGSOURCE_VS
;
6341 if (state
->render_states
[WINED3DRS_FOGVERTEXMODE
] == WINED3DFOG_NONE
|| context
->last_was_rhw
)
6342 new_source
= FOGSOURCE_COORD
;
6344 new_source
= FOGSOURCE_FFP
;
6347 new_source
= FOGSOURCE_FFP
;
6349 if(new_source
!= context
->fog_source
) {
6350 context
->fog_source
= new_source
;
6351 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART
), stateblock
, context
);
6355 static void textransform(DWORD state
, struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
6357 if (!isStateDirty(context
, STATE_PIXELSHADER
))
6358 fragment_prog_arbfp(state
, stateblock
, context
);
6361 static const struct StateEntryTemplate arbfp_fragmentstate_template
[] = {
6362 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), state_texfactor_arbfp
}, WINED3D_GL_EXT_NONE
},
6363 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6364 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6365 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6366 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6367 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6368 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6369 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6370 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6371 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6372 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6373 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6374 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6375 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6376 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6377 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6378 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6379 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6380 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6381 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6382 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6383 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6384 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6385 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6386 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6387 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6388 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6389 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6390 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6391 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6392 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6393 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6394 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6395 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6396 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6397 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6398 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6399 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6400 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6401 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6402 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6403 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6404 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6405 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6406 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6407 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6408 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6409 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6410 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6411 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6412 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6413 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6414 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6415 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6416 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6417 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6418 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6419 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6420 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6421 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6422 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6423 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6424 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6425 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6426 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6427 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6428 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6429 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6430 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6431 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6432 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6433 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6434 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6435 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6436 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6437 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6438 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6439 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6440 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6441 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6442 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6443 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6444 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6445 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6446 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6447 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6448 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6449 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6450 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6451 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6452 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6453 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6454 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6455 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6456 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6457 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6458 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6459 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6460 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6461 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6462 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6463 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6464 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6465 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6466 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6467 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6468 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6469 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6470 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6471 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6472 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6473 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6474 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6475 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6476 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6477 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6478 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6479 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6480 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6481 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6482 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6483 {STATE_PIXELSHADER
, { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
6484 {STATE_RENDER(WINED3DRS_FOGENABLE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, WINED3D_GL_EXT_NONE
},
6485 {STATE_RENDER(WINED3DRS_FOGTABLEMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
6486 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
6487 {STATE_RENDER(WINED3DRS_FOGSTART
), { STATE_RENDER(WINED3DRS_FOGSTART
), state_fogstartend
}, WINED3D_GL_EXT_NONE
},
6488 {STATE_RENDER(WINED3DRS_FOGEND
), { STATE_RENDER(WINED3DRS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
6489 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6490 {STATE_RENDER(WINED3DRS_FOGCOLOR
), { STATE_RENDER(WINED3DRS_FOGCOLOR
), state_fogcolor
}, WINED3D_GL_EXT_NONE
},
6491 {STATE_RENDER(WINED3DRS_FOGDENSITY
), { STATE_RENDER(WINED3DRS_FOGDENSITY
), state_fogdensity
}, WINED3D_GL_EXT_NONE
},
6492 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6493 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6494 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6495 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6496 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6497 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6498 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6499 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6500 {STATE_RENDER(WINED3DRS_SPECULARENABLE
), { STATE_RENDER(WINED3DRS_SPECULARENABLE
), state_arb_specularenable
}, WINED3D_GL_EXT_NONE
},
6501 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE
},
6504 const struct fragment_pipeline arbfp_fragment_pipeline
= {
6509 shader_arb_color_fixup_supported
,
6510 arbfp_fragmentstate_template
,
6511 TRUE
/* We can disable projected textures */
6514 struct arbfp_blit_priv
{
6515 GLenum yuy2_rect_shader
, yuy2_2d_shader
;
6516 GLenum uyvy_rect_shader
, uyvy_2d_shader
;
6517 GLenum yv12_rect_shader
, yv12_2d_shader
;
6518 GLenum p8_rect_shader
, p8_2d_shader
;
6519 GLuint palette_texture
;
6522 static HRESULT
arbfp_blit_alloc(IWineD3DDeviceImpl
*device
)
6524 device
->blit_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct arbfp_blit_priv
));
6525 if(!device
->blit_priv
) {
6526 ERR("Out of memory\n");
6527 return E_OUTOFMEMORY
;
6532 /* Context activation is done by the caller. */
6533 static void arbfp_blit_free(IWineD3DDeviceImpl
*device
)
6535 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6536 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6539 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_rect_shader
));
6540 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_2d_shader
));
6541 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_rect_shader
));
6542 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_2d_shader
));
6543 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_rect_shader
));
6544 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_2d_shader
));
6545 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->p8_rect_shader
));
6546 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->p8_2d_shader
));
6547 checkGLcall("Delete yuv and p8 programs");
6549 if(priv
->palette_texture
) glDeleteTextures(1, &priv
->palette_texture
);
6552 HeapFree(GetProcessHeap(), 0, device
->blit_priv
);
6553 device
->blit_priv
= NULL
;
6556 static BOOL
gen_planar_yuv_read(struct wined3d_shader_buffer
*buffer
, enum complex_fixup fixup
,
6557 GLenum textype
, char *luminance
)
6560 const char *tex
, *texinstr
;
6562 if (fixup
== COMPLEX_FIXUP_UYVY
) {
6570 case GL_TEXTURE_2D
: tex
= "2D"; texinstr
= "TXP"; break;
6571 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; texinstr
= "TEX"; break;
6573 /* This is more tricky than just replacing the texture type - we have to navigate
6574 * properly in the texture to find the correct chroma values
6576 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
6580 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
6581 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
6582 * filtering when we sample the texture.
6584 * These are the rules for reading the chroma:
6590 * So we have to get the sampling x position in non-normalized coordinates in integers
6592 if(textype
!= GL_TEXTURE_RECTANGLE_ARB
) {
6593 shader_addline(buffer
, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
6594 shader_addline(buffer
, "MOV texcrd.w, size.x;\n");
6596 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
6598 /* We must not allow filtering between pixel x and x+1, this would mix U and V
6599 * Vertical filtering is ok. However, bear in mind that the pixel center is at
6602 shader_addline(buffer
, "FLR texcrd.x, texcrd.x;\n");
6603 shader_addline(buffer
, "ADD texcrd.x, texcrd.x, coef.y;\n");
6605 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
6606 * even and odd pixels respectively
6608 shader_addline(buffer
, "MUL texcrd2, texcrd, coef.y;\n");
6609 shader_addline(buffer
, "FRC texcrd2, texcrd2;\n");
6611 /* Sample Pixel 1 */
6612 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
6614 /* Put the value into either of the chroma values */
6615 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
6616 shader_addline(buffer
, "MUL chroma.x, luminance.%c, temp.x;\n", chroma
);
6617 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
6618 shader_addline(buffer
, "MUL chroma.y, luminance.%c, temp.x;\n", chroma
);
6620 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
6621 * the pixel right to the current one. Otherwise, sample the left pixel.
6622 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
6624 shader_addline(buffer
, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
6625 shader_addline(buffer
, "ADD texcrd.x, texcrd, temp.x;\n");
6626 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
6628 /* Put the value into the other chroma */
6629 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
6630 shader_addline(buffer
, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma
);
6631 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
6632 shader_addline(buffer
, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma
);
6634 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
6635 * the current one and lerp the two U and V values
6638 /* This gives the correctly filtered luminance value */
6639 shader_addline(buffer
, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex
);
6644 static BOOL
gen_yv12_read(struct wined3d_shader_buffer
*buffer
, GLenum textype
, char *luminance
)
6649 case GL_TEXTURE_2D
: tex
= "2D"; break;
6650 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; break;
6652 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
6656 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
6657 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
6658 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
6659 * pitch of the luminance plane, the packing into the gl texture is a bit
6660 * unfortunate. If the whole texture is interpreted as luminance data it looks
6661 * approximately like this:
6663 * +----------------------------------+----
6675 * +----------------+-----------------+----
6677 * | U even rows | U odd rows |
6679 * +----------------+------------------ -
6681 * | V even rows | V odd rows |
6683 * +----------------+-----------------+----
6687 * So it appears as if there are 4 chroma images, but in fact the odd rows
6688 * in the chroma images are in the same row as the even ones. So its is
6689 * kinda tricky to read
6691 * When reading from rectangle textures, keep in mind that the input y coordinates
6692 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
6694 shader_addline(buffer
, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
6695 2.0f
/ 3.0f
, 1.0f
/ 6.0f
, (2.0f
/ 3.0f
) + (1.0f
/ 6.0f
), 1.0f
/ 3.0f
);
6697 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
6698 /* the chroma planes have only half the width */
6699 shader_addline(buffer
, "MUL texcrd.x, texcrd.x, coef.y;\n");
6701 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
6702 * the coordinate. Also read the right side of the image when reading odd lines
6704 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
6707 if(textype
== GL_TEXTURE_2D
) {
6709 shader_addline(buffer
, "RCP chroma.w, size.y;\n");
6711 shader_addline(buffer
, "MUL texcrd2.y, texcrd.y, size.y;\n");
6713 shader_addline(buffer
, "FLR texcrd2.y, texcrd2.y;\n");
6714 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
6716 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6717 shader_addline(buffer
, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6718 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
6719 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6720 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6722 /* clamp, keep the half pixel origin in mind */
6723 shader_addline(buffer
, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
6724 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
6725 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
6726 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
6728 /* Read from [size - size+size/4] */
6729 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
6730 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
6732 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6733 shader_addline(buffer
, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6734 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
6735 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6736 shader_addline(buffer
, "MUL texcrd2.x, texcrd2.x, size.x;\n");
6737 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6739 /* Make sure to read exactly from the pixel center */
6740 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
6741 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, coef.y;\n");
6744 shader_addline(buffer
, "MAD temp.y, size.y, coef.w, size.y;\n");
6745 shader_addline(buffer
, "ADD temp.y, temp.y, -coef.y;\n");
6746 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
6747 shader_addline(buffer
, "ADD temp.y, size.y, -coef.y;\n");
6748 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
6750 /* Read the texture, put the result into the output register */
6751 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
6752 shader_addline(buffer
, "MOV chroma.x, temp.w;\n");
6754 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
6755 * No need to clamp because we're just reusing the already clamped value from above
6757 if(textype
== GL_TEXTURE_2D
) {
6758 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
6760 shader_addline(buffer
, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
6762 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
6763 shader_addline(buffer
, "MOV chroma.y, temp.w;\n");
6765 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
6766 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
6767 * values due to filtering
6769 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
6770 if(textype
== GL_TEXTURE_2D
) {
6771 /* Multiply the y coordinate by 2/3 and clamp it */
6772 shader_addline(buffer
, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
6773 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
6774 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
6775 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
6777 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
6778 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
6781 shader_addline(buffer
, "ADD temp.x, size.y, -coef.y;\n");
6782 shader_addline(buffer
, "MIN texcrd.y, texcrd.y, size.x;\n");
6783 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
6790 static GLuint
gen_p8_shader(struct arbfp_blit_priv
*priv
,
6791 const struct wined3d_gl_info
*gl_info
, GLenum textype
)
6794 struct wined3d_shader_buffer buffer
;
6798 if (!shader_buffer_init(&buffer
))
6800 ERR("Failed to initialize shader buffer.\n");
6805 GL_EXTCALL(glGenProgramsARB(1, &shader
));
6806 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
6809 shader_buffer_free(&buffer
);
6813 shader_addline(&buffer
, "!!ARBfp1.0\n");
6814 shader_addline(&buffer
, "TEMP index;\n");
6816 /* { 255/256, 0.5/255*255/256, 0, 0 } */
6817 shader_addline(&buffer
, "PARAM constants = { 0.996, 0.00195, 0, 0 };\n");
6819 /* The alpha-component contains the palette index */
6820 if(textype
== GL_TEXTURE_RECTANGLE_ARB
)
6821 shader_addline(&buffer
, "TXP index, fragment.texcoord[0], texture[0], RECT;\n");
6823 shader_addline(&buffer
, "TEX index, fragment.texcoord[0], texture[0], 2D;\n");
6825 /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
6826 shader_addline(&buffer
, "MAD index.a, index.a, constants.x, constants.y;\n");
6828 /* Use the alpha-component as an index in the palette to get the final color */
6829 shader_addline(&buffer
, "TEX result.color, index.a, texture[1], 1D;\n");
6830 shader_addline(&buffer
, "END\n");
6833 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
6834 strlen(buffer
.buffer
), buffer
.buffer
));
6835 checkGLcall("glProgramStringARB()");
6837 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
6840 FIXME("Fragment program error at position %d: %s\n\n", pos
,
6841 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
6842 shader_arb_dump_program_source(buffer
.buffer
);
6845 if (textype
== GL_TEXTURE_RECTANGLE_ARB
)
6846 priv
->p8_rect_shader
= shader
;
6848 priv
->p8_2d_shader
= shader
;
6850 shader_buffer_free(&buffer
);
6856 /* Context activation is done by the caller. */
6857 static void upload_palette(IWineD3DSurfaceImpl
*surface
)
6860 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
6861 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6862 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6863 BOOL colorkey
= (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
) ? TRUE
: FALSE
;
6865 d3dfmt_p8_init_palette(surface
, table
, colorkey
);
6868 if (!priv
->palette_texture
)
6869 glGenTextures(1, &priv
->palette_texture
);
6871 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1
));
6872 glBindTexture(GL_TEXTURE_1D
, priv
->palette_texture
);
6874 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
6876 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
6877 /* Make sure we have discrete color levels. */
6878 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
6879 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
6880 /* Upload the palette */
6881 /* TODO: avoid unneeed uploads in the future by adding some SFLAG_PALETTE_DIRTY mechanism */
6882 glTexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA
, 256, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, table
);
6884 /* Switch back to unit 0 in which the 2D texture will be stored. */
6885 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
));
6889 /* Context activation is done by the caller. */
6890 static GLuint
gen_yuv_shader(struct arbfp_blit_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
6891 enum complex_fixup yuv_fixup
, GLenum textype
)
6894 struct wined3d_shader_buffer buffer
;
6895 char luminance_component
;
6899 if (!shader_buffer_init(&buffer
))
6901 ERR("Failed to initialize shader buffer.\n");
6906 GL_EXTCALL(glGenProgramsARB(1, &shader
));
6907 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
6908 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
6909 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6912 shader_buffer_free(&buffer
);
6916 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
6917 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
6918 * two chroma(U and V) values. Each macropixel has two luminance values, one for
6919 * each single pixel it contains, and one U and one V value shared between both
6922 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
6923 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
6924 * take the format into account when generating the read swizzles
6926 * Reading the Y value is straightforward - just sample the texture. The hardware
6927 * takes care of filtering in the horizontal and vertical direction.
6929 * Reading the U and V values is harder. We have to avoid filtering horizontally,
6930 * because that would mix the U and V values of one pixel or two adjacent pixels.
6931 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
6932 * regardless of the filtering setting. Vertical filtering works automatically
6933 * though - the U and V values of two rows are mixed nicely.
6935 * Appart of avoiding filtering issues, the code has to know which value it just
6936 * read, and where it can find the other one. To determine this, it checks if
6937 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
6939 * Handling horizontal filtering of U and V values requires reading a 2nd pair
6940 * of pixels, extracting U and V and mixing them. This is not implemented yet.
6942 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
6943 * with width / 2. This way one read gives all 3 values, finding U and V is easy
6944 * in an unfiltered situation. Finding the luminance on the other hand requires
6945 * finding out if it is an odd or even pixel. The real drawback of this approach
6946 * is filtering. This would have to be emulated completely in the shader, reading
6947 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
6948 * vertically. Beyond that it would require adjustments to the texture handling
6949 * code to deal with the width scaling
6951 shader_addline(&buffer
, "!!ARBfp1.0\n");
6952 shader_addline(&buffer
, "TEMP luminance;\n");
6953 shader_addline(&buffer
, "TEMP temp;\n");
6954 shader_addline(&buffer
, "TEMP chroma;\n");
6955 shader_addline(&buffer
, "TEMP texcrd;\n");
6956 shader_addline(&buffer
, "TEMP texcrd2;\n");
6957 shader_addline(&buffer
, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
6958 shader_addline(&buffer
, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
6959 shader_addline(&buffer
, "PARAM size = program.local[0];\n");
6963 case COMPLEX_FIXUP_UYVY
:
6964 case COMPLEX_FIXUP_YUY2
:
6965 if (!gen_planar_yuv_read(&buffer
, yuv_fixup
, textype
, &luminance_component
))
6967 shader_buffer_free(&buffer
);
6972 case COMPLEX_FIXUP_YV12
:
6973 if (!gen_yv12_read(&buffer
, textype
, &luminance_component
))
6975 shader_buffer_free(&buffer
);
6981 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup
);
6982 shader_buffer_free(&buffer
);
6986 /* Calculate the final result. Formula is taken from
6987 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
6988 * ranges from -0.5 to 0.5
6990 shader_addline(&buffer
, "SUB chroma.xy, chroma, coef.y;\n");
6992 shader_addline(&buffer
, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component
);
6993 shader_addline(&buffer
, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component
);
6994 shader_addline(&buffer
, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
6995 shader_addline(&buffer
, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component
);
6996 shader_addline(&buffer
, "END\n");
6999 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
7000 strlen(buffer
.buffer
), buffer
.buffer
));
7001 checkGLcall("glProgramStringARB()");
7003 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
7006 FIXME("Fragment program error at position %d: %s\n\n", pos
,
7007 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
7008 shader_arb_dump_program_source(buffer
.buffer
);
7014 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
7015 checkGLcall("glGetProgramivARB()");
7016 if (!native
) WARN("Program exceeds native resource limits.\n");
7019 shader_buffer_free(&buffer
);
7024 case COMPLEX_FIXUP_YUY2
:
7025 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->yuy2_rect_shader
= shader
;
7026 else priv
->yuy2_2d_shader
= shader
;
7029 case COMPLEX_FIXUP_UYVY
:
7030 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->uyvy_rect_shader
= shader
;
7031 else priv
->uyvy_2d_shader
= shader
;
7034 case COMPLEX_FIXUP_YV12
:
7035 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->yv12_rect_shader
= shader
;
7036 else priv
->yv12_2d_shader
= shader
;
7039 ERR("Unsupported complex fixup: %d\n", yuv_fixup
);
7045 /* Context activation is done by the caller. */
7046 static HRESULT
arbfp_blit_set(void *blit_priv
, const struct wined3d_gl_info
*gl_info
, IWineD3DSurfaceImpl
*surface
)
7049 float size
[4] = {(float) surface
->pow2Width
, (float) surface
->pow2Height
, 1.0f
, 1.0f
};
7050 struct arbfp_blit_priv
*priv
= blit_priv
;
7051 enum complex_fixup fixup
;
7052 GLenum textype
= surface
->texture_target
;
7054 if (!is_complex_fixup(surface
->resource
.format
->color_fixup
))
7057 dump_color_fixup_desc(surface
->resource
.format
->color_fixup
);
7058 /* Don't bother setting up a shader for unconverted formats */
7061 checkGLcall("glEnable(textype)");
7066 fixup
= get_complex_fixup(surface
->resource
.format
->color_fixup
);
7070 case COMPLEX_FIXUP_YUY2
:
7071 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->yuy2_rect_shader
: priv
->yuy2_2d_shader
;
7074 case COMPLEX_FIXUP_UYVY
:
7075 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->uyvy_rect_shader
: priv
->uyvy_2d_shader
;
7078 case COMPLEX_FIXUP_YV12
:
7079 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->yv12_rect_shader
: priv
->yv12_2d_shader
;
7082 case COMPLEX_FIXUP_P8
:
7083 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->p8_rect_shader
: priv
->p8_2d_shader
;
7084 if (!shader
) shader
= gen_p8_shader(priv
, gl_info
, textype
);
7086 upload_palette(surface
);
7090 FIXME("Unsupported complex fixup %#x, not setting a shader\n", fixup
);
7093 checkGLcall("glEnable(textype)");
7098 if (!shader
) shader
= gen_yuv_shader(priv
, gl_info
, fixup
, textype
);
7101 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
7102 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
7103 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
7104 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
7105 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0, size
));
7106 checkGLcall("glProgramLocalParameter4fvARB");
7112 /* Context activation is done by the caller. */
7113 static void arbfp_blit_unset(const struct wined3d_gl_info
*gl_info
)
7116 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
7117 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
7118 glDisable(GL_TEXTURE_2D
);
7119 checkGLcall("glDisable(GL_TEXTURE_2D)");
7120 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
7122 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
7123 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
7125 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
7127 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
7128 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
7133 static BOOL
arbfp_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
7134 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
, const struct wined3d_format
*src_format
,
7135 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
, const struct wined3d_format
*dst_format
)
7137 enum complex_fixup src_fixup
;
7139 if (!gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
7142 if (blit_op
!= BLIT_OP_BLIT
)
7144 TRACE("Unsupported blit_op=%d\n", blit_op
);
7148 src_fixup
= get_complex_fixup(src_format
->color_fixup
);
7149 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
7151 TRACE("Checking support for fixup:\n");
7152 dump_color_fixup_desc(src_format
->color_fixup
);
7155 if (!is_identity_fixup(dst_format
->color_fixup
))
7157 TRACE("Destination fixups are not supported\n");
7161 if (is_identity_fixup(src_format
->color_fixup
))
7167 /* We only support YUV conversions. */
7168 if (!is_complex_fixup(src_format
->color_fixup
))
7170 TRACE("[FAILED]\n");
7176 case COMPLEX_FIXUP_YUY2
:
7177 case COMPLEX_FIXUP_UYVY
:
7178 case COMPLEX_FIXUP_YV12
:
7179 case COMPLEX_FIXUP_P8
:
7184 FIXME("Unsupported YUV fixup %#x\n", src_fixup
);
7185 TRACE("[FAILED]\n");
7190 HRESULT
arbfp_blit_surface(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*src_surface
, const RECT
*src_rect
,
7191 IWineD3DSurfaceImpl
*dst_surface
, const RECT
*dst_rect_in
, enum blit_operation blit_op
,
7194 IWineD3DSwapChainImpl
*dst_swapchain
;
7195 struct wined3d_context
*context
;
7196 RECT dst_rect
= *dst_rect_in
;
7198 /* Now load the surface */
7199 surface_internal_preload(src_surface
, SRGB_RGB
);
7201 /* Activate the destination context, set it up for blitting */
7202 context
= context_acquire(device
, dst_surface
);
7203 context_apply_blit_state(context
, device
);
7205 if (!surface_is_offscreen(dst_surface
))
7206 surface_translate_drawable_coords(dst_surface
, context
->win_handle
, &dst_rect
);
7208 arbfp_blit_set(device
->blit_priv
, context
->gl_info
, src_surface
);
7212 /* Draw a textured quad */
7213 draw_textured_quad(src_surface
, src_rect
, &dst_rect
, Filter
);
7217 /* Leave the opengl state valid for blitting */
7218 arbfp_blit_unset(context
->gl_info
);
7220 dst_swapchain
= dst_surface
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
7221 ? dst_surface
->container
.u
.swapchain
: NULL
;
7222 if (wined3d_settings
.strict_draw_ordering
|| (dst_swapchain
7223 && (dst_surface
== dst_swapchain
->front_buffer
7224 || dst_swapchain
->num_contexts
> 1)))
7225 wglFlush(); /* Flush to ensure ordering across contexts. */
7227 context_release(context
);
7229 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
7233 /* Do not call while under the GL lock. */
7234 static HRESULT
arbfp_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
,
7235 const RECT
*dst_rect
, const WINED3DCOLORVALUE
*color
)
7237 FIXME("Color filling not implemented by arbfp_blit\n");
7238 return WINED3DERR_INVALIDCALL
;
7241 const struct blit_shader arbfp_blit
= {
7246 arbfp_blit_supported
,
7247 arbfp_blit_color_fill