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
);
81 /* GL locking for state handlers is done by the caller. */
82 static BOOL
need_rel_addr_const(IWineD3DBaseShaderImpl
*shader
, const struct wined3d_gl_info
*gl_info
)
84 if (shader
->baseShader
.reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
86 if (((IWineD3DVertexShaderImpl
*)shader
)->rel_offset
) return TRUE
;
88 if (!shader
->baseShader
.reg_maps
.usesmova
) return FALSE
;
89 return !gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
];
92 /* Returns TRUE if result.clip from GL_NV_vertex_program2 should be used and FALSE otherwise */
93 static inline BOOL
use_nv_clip(const struct wined3d_gl_info
*gl_info
)
95 return gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
]
96 && !(gl_info
->quirks
& WINED3D_QUIRK_NV_CLIP_BROKEN
);
99 static BOOL
need_helper_const(IWineD3DBaseShaderImpl
*shader
, const struct wined3d_gl_info
*gl_info
)
101 if (need_rel_addr_const(shader
, gl_info
)) return TRUE
;
102 if (!gl_info
->supported
[NV_VERTEX_PROGRAM
]) return TRUE
; /* Need to init colors. */
103 if (gl_info
->quirks
& WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT
) return TRUE
; /* Load the immval offset. */
104 if (gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
) return TRUE
; /* Have to init texcoords. */
105 if (!use_nv_clip(gl_info
)) return TRUE
; /* Init the clip texcoord */
106 if (shader
->baseShader
.reg_maps
.usesnrm
) return TRUE
; /* 0.0 */
107 if (shader
->baseShader
.reg_maps
.usesrcp
) return TRUE
; /* EPS */
111 static unsigned int reserved_vs_const(IWineD3DBaseShaderImpl
*shader
, const struct wined3d_gl_info
*gl_info
)
113 unsigned int ret
= 1;
114 /* We use one PARAM for the pos fixup, and in some cases one to load
115 * some immediate values into the shader
117 if(need_helper_const(shader
, gl_info
)) ret
++;
118 if(need_rel_addr_const(shader
, gl_info
)) ret
++;
122 enum arb_helper_value
133 static const char *arb_get_helper_value(enum wined3d_shader_type shader
, enum arb_helper_value value
)
135 if (shader
== WINED3D_SHADER_TYPE_GEOMETRY
)
137 ERR("Geometry shaders are unsupported\n");
141 if (shader
== WINED3D_SHADER_TYPE_PIXEL
)
145 case ARB_ZERO
: return "ps_helper_const.x";
146 case ARB_ONE
: return "ps_helper_const.y";
147 case ARB_TWO
: return "coefmul.x";
148 case ARB_0001
: return "helper_const.xxxy";
149 case ARB_EPS
: return "ps_helper_const.z";
157 case ARB_ZERO
: return "helper_const.x";
158 case ARB_ONE
: return "helper_const.y";
159 case ARB_TWO
: return "helper_const.z";
160 case ARB_EPS
: return "helper_const.w";
161 case ARB_0001
: return "helper_const.xxxy";
162 case ARB_VS_REL_OFFSET
: return "rel_addr_const.y";
165 FIXME("Unmanaged %s shader helper constant requested: %u\n",
166 shader
== WINED3D_SHADER_TYPE_PIXEL
? "pixel" : "vertex", value
);
169 case ARB_ZERO
: return "0.0";
170 case ARB_ONE
: return "1.0";
171 case ARB_TWO
: return "2.0";
172 case ARB_0001
: return "{0.0, 0.0, 0.0, 1.0}";
173 case ARB_EPS
: return "1e-8";
174 default: return "bad";
178 static inline BOOL
ffp_clip_emul(const struct wined3d_state
*state
)
180 return state
->lowest_disabled_stage
< 7;
183 /* ARB_program_shader private data */
202 struct wined3d_shader_loop_control loop_control
;
206 struct arb_ps_np2fixup_info
208 struct ps_np2fixup_info super
;
209 /* For ARB we need a offset value:
210 * With both GLSL and ARB mode the NP2 fixup information (the texture dimensions) are stored in a
211 * consecutive way (GLSL uses a uniform array). Since ARB doesn't know the notion of a "standalone"
212 * array we need an offset to the index inside the program local parameter array. */
216 struct arb_ps_compile_args
218 struct ps_compile_args super
;
220 WORD clip
; /* only a boolean, use a WORD for alignment */
221 unsigned char loop_ctrl
[MAX_CONST_I
][3];
224 struct stb_const_desc
226 unsigned char texunit
;
230 struct arb_ps_compiled_shader
232 struct arb_ps_compile_args args
;
233 struct arb_ps_np2fixup_info np2fixup_info
;
234 struct stb_const_desc bumpenvmatconst
[MAX_TEXTURES
];
235 struct stb_const_desc luminanceconst
[MAX_TEXTURES
];
236 UINT int_consts
[MAX_CONST_I
];
239 unsigned char numbumpenvmatconsts
;
243 struct arb_vs_compile_args
245 struct vs_compile_args super
;
251 unsigned char clip_texcoord
;
252 unsigned char clipplane_mask
;
254 DWORD boolclip_compare
;
259 unsigned char samplers
[4];
260 DWORD samplers_compare
;
262 unsigned char loop_ctrl
[MAX_CONST_I
][3];
265 struct arb_vs_compiled_shader
267 struct arb_vs_compile_args args
;
269 UINT int_consts
[MAX_CONST_I
];
271 char need_color_unclamp
;
275 struct recorded_instruction
277 struct wined3d_shader_instruction ins
;
281 struct shader_arb_ctx_priv
286 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
288 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
290 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
294 const struct arb_vs_compile_args
*cur_vs_args
;
295 const struct arb_ps_compile_args
*cur_ps_args
;
296 const struct arb_ps_compiled_shader
*compiled_fprog
;
297 const struct arb_vs_compiled_shader
*compiled_vprog
;
298 struct arb_ps_np2fixup_info
*cur_np2fixup_info
;
299 struct list control_frames
;
303 unsigned int num_loops
, loop_depth
, num_ifcs
;
306 unsigned int vs_clipplanes
;
310 /* For 3.0 vertex shaders */
311 const char *vs_output
[MAX_REG_OUTPUT
];
312 /* For 2.x and earlier vertex shaders */
313 const char *texcrd_output
[8], *color_output
[2], *fog_output
;
315 /* 3.0 pshader input for compatibility with fixed function */
316 const char *ps_input
[MAX_REG_INPUT
];
321 struct wined3d_shader_signature_element
*sig
;
323 struct wine_rb_entry entry
;
326 struct arb_pshader_private
{
327 struct arb_ps_compiled_shader
*gl_shaders
;
328 UINT num_gl_shaders
, shader_array_size
;
329 BOOL has_signature_idx
;
330 DWORD input_signature_idx
;
331 DWORD clipplane_emulation
;
335 struct arb_vshader_private
{
336 struct arb_vs_compiled_shader
*gl_shaders
;
337 UINT num_gl_shaders
, shader_array_size
;
340 struct shader_arb_priv
342 GLuint current_vprogram_id
;
343 GLuint current_fprogram_id
;
344 const struct arb_ps_compiled_shader
*compiled_fprog
;
345 const struct arb_vs_compiled_shader
*compiled_vprog
;
346 GLuint depth_blt_vprogram_id
;
347 GLuint depth_blt_fprogram_id_full
[tex_type_count
];
348 GLuint depth_blt_fprogram_id_masked
[tex_type_count
];
349 BOOL use_arbfp_fixed_func
;
350 struct wine_rb_tree fragment_shaders
;
351 BOOL last_ps_const_clamped
;
352 BOOL last_vs_color_unclamp
;
354 struct wine_rb_tree signature_tree
;
358 /********************************************************
359 * ARB_[vertex/fragment]_program helper functions follow
360 ********************************************************/
362 /* Loads floating point constants into the currently set ARB_vertex/fragment_program.
363 * When constant_list == NULL, it will load all the constants.
365 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
366 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
368 /* GL locking is done by the caller */
369 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl
*This
, const struct wined3d_gl_info
*gl_info
,
370 GLuint target_type
, unsigned int max_constants
, const float *constants
, char *dirty_consts
)
372 local_constant
* lconst
;
376 if (TRACE_ON(d3d_constants
))
378 for(i
= 0; i
< max_constants
; i
++) {
379 if(!dirty_consts
[i
]) continue;
380 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
381 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
382 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
388 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
389 if (target_type
== GL_FRAGMENT_PROGRAM_ARB
&& This
->baseShader
.reg_maps
.shader_version
.major
== 1)
392 /* ps 1.x supports only 8 constants, clamp only those. When switching between 1.x and higher
393 * shaders, the first 8 constants are marked dirty for reload
395 for(; i
< min(8, max_constants
); i
++) {
396 if(!dirty_consts
[i
]) continue;
400 if (constants
[j
+ 0] > 1.0f
) lcl_const
[0] = 1.0f
;
401 else if (constants
[j
+ 0] < -1.0f
) lcl_const
[0] = -1.0f
;
402 else lcl_const
[0] = constants
[j
+ 0];
404 if (constants
[j
+ 1] > 1.0f
) lcl_const
[1] = 1.0f
;
405 else if (constants
[j
+ 1] < -1.0f
) lcl_const
[1] = -1.0f
;
406 else lcl_const
[1] = constants
[j
+ 1];
408 if (constants
[j
+ 2] > 1.0f
) lcl_const
[2] = 1.0f
;
409 else if (constants
[j
+ 2] < -1.0f
) lcl_const
[2] = -1.0f
;
410 else lcl_const
[2] = constants
[j
+ 2];
412 if (constants
[j
+ 3] > 1.0f
) lcl_const
[3] = 1.0f
;
413 else if (constants
[j
+ 3] < -1.0f
) lcl_const
[3] = -1.0f
;
414 else lcl_const
[3] = constants
[j
+ 3];
416 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, lcl_const
));
419 /* If further constants are dirty, reload them without clamping.
421 * The alternative is not to touch them, but then we cannot reset the dirty constant count
422 * to zero. That's bad for apps that only use PS 1.x shaders, because in that case the code
423 * above would always re-check the first 8 constants since max_constant remains at the init
428 if (gl_info
->supported
[EXT_GPU_PROGRAM_PARAMETERS
])
430 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
431 * or just reloading *all* constants at once
433 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, i, max_constants, constants + (i * 4)));
435 for(; i
< max_constants
; i
++) {
436 if(!dirty_consts
[i
]) continue;
438 /* Find the next block of dirty constants */
441 for(i
++; (i
< max_constants
) && dirty_consts
[i
]; i
++) {
445 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type
, j
, i
- j
, constants
+ (j
* 4)));
448 for(; i
< max_constants
; i
++) {
449 if(dirty_consts
[i
]) {
451 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, constants
+ (i
* 4)));
455 checkGLcall("glProgramEnvParameter4fvARB()");
457 /* Load immediate constants */
458 if(This
->baseShader
.load_local_constsF
) {
459 if (TRACE_ON(d3d_shader
)) {
460 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
461 GLfloat
* values
= (GLfloat
*)lconst
->value
;
462 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
463 values
[0], values
[1], values
[2], values
[3]);
466 /* Immediate constants are clamped for 1.X shaders at loading times */
468 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
469 dirty_consts
[lconst
->idx
] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
470 ret
= max(ret
, lconst
->idx
+ 1);
471 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, lconst
->idx
, (GLfloat
*)lconst
->value
));
473 checkGLcall("glProgramEnvParameter4fvARB()");
474 return ret
; /* The loaded immediate constants need reloading for the next shader */
476 return 0; /* No constants are dirty now */
481 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
483 static void shader_arb_load_np2fixup_constants(void *shader_priv
,
484 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
486 const struct shader_arb_priv
* priv
= shader_priv
;
488 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
489 if (!use_ps(state
)) return;
491 if (priv
->compiled_fprog
&& priv
->compiled_fprog
->np2fixup_info
.super
.active
) {
492 const struct arb_ps_np2fixup_info
* const fixup
= &priv
->compiled_fprog
->np2fixup_info
;
494 WORD active
= fixup
->super
.active
;
495 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
497 for (i
= 0; active
; active
>>= 1, ++i
)
499 const IWineD3DBaseTextureImpl
*tex
= state
->textures
[i
];
500 const unsigned char idx
= fixup
->super
.idx
[i
];
501 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
503 if (!(active
& 1)) continue;
506 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
511 tex_dim
[2] = tex
->baseTexture
.pow2Matrix
[0]; tex_dim
[3] = tex
->baseTexture
.pow2Matrix
[5];
513 tex_dim
[0] = tex
->baseTexture
.pow2Matrix
[0]; tex_dim
[1] = tex
->baseTexture
.pow2Matrix
[5];
517 for (i
= 0; i
< fixup
->super
.num_consts
; ++i
) {
518 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
,
519 fixup
->offset
+ i
, &np2fixup_constants
[i
* 4]));
524 /* GL locking is done by the caller. */
525 static void shader_arb_ps_local_constants(const struct arb_ps_compiled_shader
*gl_shader
,
526 const struct wined3d_context
*context
, const struct wined3d_state
*state
, float rt_height
)
528 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
531 for(i
= 0; i
< gl_shader
->numbumpenvmatconsts
; i
++)
533 int texunit
= gl_shader
->bumpenvmatconst
[i
].texunit
;
535 /* The state manager takes care that this function is always called if the bump env matrix changes */
536 const float *data
= (const float *)&state
->texture_states
[texunit
][WINED3DTSS_BUMPENVMAT00
];
537 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
,
538 gl_shader
->bumpenvmatconst
[i
].const_num
, data
));
540 if (gl_shader
->luminanceconst
[i
].const_num
!= WINED3D_CONST_NUM_UNUSED
)
542 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
543 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
544 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
545 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
547 const float *scale
= (const float *)&state
->texture_states
[texunit
][WINED3DTSS_BUMPENVLSCALE
];
548 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
,
549 gl_shader
->luminanceconst
[i
].const_num
, scale
));
552 checkGLcall("Load bumpmap consts");
554 if(gl_shader
->ycorrection
!= WINED3D_CONST_NUM_UNUSED
)
556 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
557 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
562 val
[0] = context
->render_offscreen
? 0.0f
: rt_height
;
563 val
[1] = context
->render_offscreen
? 1.0f
: -1.0f
;
566 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->ycorrection
, val
));
567 checkGLcall("y correction loading");
570 if (!gl_shader
->num_int_consts
) return;
572 for(i
= 0; i
< MAX_CONST_I
; i
++)
574 if(gl_shader
->int_consts
[i
] != WINED3D_CONST_NUM_UNUSED
)
577 val
[0] = (float)state
->ps_consts_i
[4 * i
];
578 val
[1] = (float)state
->ps_consts_i
[4 * i
+ 1];
579 val
[2] = (float)state
->ps_consts_i
[4 * i
+ 2];
582 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->int_consts
[i
], val
));
585 checkGLcall("Load ps int consts");
588 /* GL locking is done by the caller. */
589 static void shader_arb_vs_local_constants(const struct arb_vs_compiled_shader
*gl_shader
,
590 const struct wined3d_context
*context
, const struct wined3d_state
*state
)
592 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
593 float position_fixup
[4];
596 /* Upload the position fixup */
597 shader_get_position_fixup(context
, state
, position_fixup
);
598 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, gl_shader
->pos_fixup
, position_fixup
));
600 if (!gl_shader
->num_int_consts
) return;
602 for(i
= 0; i
< MAX_CONST_I
; i
++)
604 if(gl_shader
->int_consts
[i
] != WINED3D_CONST_NUM_UNUSED
)
607 val
[0] = (float)state
->vs_consts_i
[4 * i
];
608 val
[1] = (float)state
->vs_consts_i
[4 * i
+ 1];
609 val
[2] = (float)state
->vs_consts_i
[4 * i
+ 2];
612 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, gl_shader
->int_consts
[i
], val
));
615 checkGLcall("Load vs int consts");
619 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
621 * We only support float constants in ARB at the moment, so don't
622 * worry about the Integers or Booleans
624 /* GL locking is done by the caller (state handler) */
625 static void shader_arb_load_constants(const struct wined3d_context
*context
, char usePixelShader
, char useVertexShader
)
627 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
628 IWineD3DStateBlockImpl
* stateBlock
= device
->stateBlock
;
629 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
630 struct shader_arb_priv
*priv
= device
->shader_priv
;
634 IWineD3DBaseShaderImpl
*vshader
= (IWineD3DBaseShaderImpl
*)stateBlock
->state
.vertex_shader
;
635 const struct arb_vs_compiled_shader
*gl_shader
= priv
->compiled_vprog
;
637 /* Load DirectX 9 float constants for vertex shader */
638 device
->highest_dirty_vs_const
= shader_arb_load_constantsF(vshader
, gl_info
, GL_VERTEX_PROGRAM_ARB
,
639 device
->highest_dirty_vs_const
, stateBlock
->state
.vs_consts_f
, context
->vshader_const_dirty
);
640 shader_arb_vs_local_constants(gl_shader
, context
, &stateBlock
->state
);
645 IWineD3DBaseShaderImpl
*pshader
= (IWineD3DBaseShaderImpl
*)stateBlock
->state
.pixel_shader
;
646 const struct arb_ps_compiled_shader
*gl_shader
= priv
->compiled_fprog
;
647 float rt_height
= device
->render_targets
[0]->currentDesc
.Height
;
649 /* Load DirectX 9 float constants for pixel shader */
650 device
->highest_dirty_ps_const
= shader_arb_load_constantsF(pshader
, gl_info
, GL_FRAGMENT_PROGRAM_ARB
,
651 device
->highest_dirty_ps_const
, stateBlock
->state
.ps_consts_f
, context
->pshader_const_dirty
);
652 shader_arb_ps_local_constants(gl_shader
, context
, &stateBlock
->state
, rt_height
);
656 static void shader_arb_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
658 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
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
!= This
) return;
665 memset(context
->vshader_const_dirty
+ start
, 1, sizeof(*context
->vshader_const_dirty
) * count
);
666 This
->highest_dirty_vs_const
= max(This
->highest_dirty_vs_const
, start
+ count
);
669 static void shader_arb_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
671 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
672 struct wined3d_context
*context
= context_get_current();
674 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
675 * context. On a context switch the old context will be fully dirtified */
676 if (!context
|| context
->swapchain
->device
!= This
) return;
678 memset(context
->pshader_const_dirty
+ start
, 1, sizeof(*context
->pshader_const_dirty
) * count
);
679 This
->highest_dirty_ps_const
= max(This
->highest_dirty_ps_const
, start
+ count
);
682 static DWORD
*local_const_mapping(IWineD3DBaseShaderImpl
*This
)
686 const local_constant
*lconst
;
688 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) return NULL
;
690 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
) * This
->baseShader
.limits
.constant_float
);
692 ERR("Out of memory\n");
696 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
697 ret
[lconst
->idx
] = idx
++;
702 /* Generate the variable & register declarations for the ARB_vertex_program output target */
703 static DWORD
shader_generate_arb_declarations(IWineD3DBaseShader
*iface
, const shader_reg_maps
*reg_maps
,
704 struct wined3d_shader_buffer
*buffer
, const struct wined3d_gl_info
*gl_info
, DWORD
*lconst_map
,
705 DWORD
*num_clipplanes
, struct shader_arb_ctx_priv
*ctx
)
707 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
708 DWORD i
, next_local
= 0;
709 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
710 unsigned max_constantsF
;
711 const local_constant
*lconst
;
714 /* In pixel shaders, all private constants are program local, we don't need anything
715 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
716 * If we need a private constant the GL implementation will squeeze it in somewhere
718 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
719 * immediate values. The posFixup is loaded using program.env for now, so always
720 * subtract one from the number of constants. If the shader uses indirect addressing,
721 * account for the helper const too because we have to declare all availabke d3d constants
722 * and don't know which are actually used.
726 max_constantsF
= gl_info
->limits
.arb_ps_native_constants
;
727 /* 24 is the minimum MAX_PROGRAM_ENV_PARAMETERS_ARB value. */
728 if (max_constantsF
< 24)
729 max_constantsF
= gl_info
->limits
.arb_ps_float_constants
;
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(This
->baseShader
.reg_maps
.usesrelconstF
) {
741 DWORD highest_constf
= 0, clip_limit
;
743 max_constantsF
-= reserved_vs_const(This
, gl_info
);
744 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.integer_constants
);
746 for(i
= 0; i
< This
->baseShader
.limits
.constant_float
; i
++)
749 DWORD shift
= i
& 0x1f;
750 if(reg_maps
->constf
[idx
] & (1 << shift
)) highest_constf
= i
;
753 if(use_nv_clip(gl_info
) && ctx
->target_version
>= NV2
)
755 if(ctx
->cur_vs_args
->super
.clip_enabled
)
756 clip_limit
= gl_info
->limits
.clipplanes
;
762 unsigned int mask
= ctx
->cur_vs_args
->clip
.boolclip
.clipplane_mask
;
763 clip_limit
= min(count_bits(mask
), 4);
765 *num_clipplanes
= min(clip_limit
, max_constantsF
- highest_constf
- 1);
766 max_constantsF
-= *num_clipplanes
;
767 if(*num_clipplanes
< clip_limit
)
769 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes
, gl_info
->limits
.clipplanes
);
774 if (ctx
->target_version
>= NV2
) *num_clipplanes
= gl_info
->limits
.clipplanes
;
775 else *num_clipplanes
= min(gl_info
->limits
.clipplanes
, 4);
779 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
781 if (map
& 1) shader_addline(buffer
, "TEMP R%u;\n", i
);
784 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
786 if (map
& 1) shader_addline(buffer
, "ADDRESS A%u;\n", i
);
789 if (pshader
&& reg_maps
->shader_version
.major
== 1 && reg_maps
->shader_version
.minor
<= 3)
791 for (i
= 0, map
= reg_maps
->texcoord
; map
; map
>>= 1, ++i
)
793 if (map
& 1) shader_addline(buffer
, "TEMP T%u;\n", i
);
797 /* Load local constants using the program-local space,
798 * this avoids reloading them each time the shader is used
801 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
802 shader_addline(buffer
, "PARAM C%u = program.local[%u];\n", lconst
->idx
,
803 lconst_map
[lconst
->idx
]);
804 next_local
= max(next_local
, lconst_map
[lconst
->idx
] + 1);
808 /* After subtracting privately used constants from the hardware limit(they are loaded as
809 * local constants), make sure the shader doesn't violate the env constant limit
813 max_constantsF
= min(max_constantsF
, gl_info
->limits
.arb_ps_float_constants
);
817 max_constantsF
= min(max_constantsF
, gl_info
->limits
.arb_vs_float_constants
);
820 /* Avoid declaring more constants than needed */
821 max_constantsF
= min(max_constantsF
, This
->baseShader
.limits
.constant_float
);
823 /* we use the array-based constants array if the local constants are marked for loading,
824 * because then we use indirect addressing, or when the local constant list is empty,
825 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
826 * local constants do not declare the loaded constants as an array because ARB compilers usually
827 * do not optimize unused constants away
829 if(This
->baseShader
.reg_maps
.usesrelconstF
) {
830 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
831 shader_addline(buffer
, "PARAM C[%d] = { program.env[0..%d] };\n",
832 max_constantsF
, max_constantsF
- 1);
834 for(i
= 0; i
< max_constantsF
; i
++) {
837 mask
= 1 << (i
& 0x1f);
838 if(!shader_constant_is_local(This
, i
) && (This
->baseShader
.reg_maps
.constf
[idx
] & mask
)) {
839 shader_addline(buffer
, "PARAM C%d = program.env[%d];\n",i
, i
);
847 static const char * const shift_tab
[] = {
848 "dummy", /* 0 (none) */
849 "coefmul.x", /* 1 (x2) */
850 "coefmul.y", /* 2 (x4) */
851 "coefmul.z", /* 3 (x8) */
852 "coefmul.w", /* 4 (x16) */
853 "dummy", /* 5 (x32) */
854 "dummy", /* 6 (x64) */
855 "dummy", /* 7 (x128) */
856 "dummy", /* 8 (d256) */
857 "dummy", /* 9 (d128) */
858 "dummy", /* 10 (d64) */
859 "dummy", /* 11 (d32) */
860 "coefdiv.w", /* 12 (d16) */
861 "coefdiv.z", /* 13 (d8) */
862 "coefdiv.y", /* 14 (d4) */
863 "coefdiv.x" /* 15 (d2) */
866 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction
*ins
,
867 const struct wined3d_shader_dst_param
*dst
, char *write_mask
)
869 char *ptr
= write_mask
;
871 if (dst
->write_mask
!= WINED3DSP_WRITEMASK_ALL
)
874 if (dst
->write_mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
875 if (dst
->write_mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
876 if (dst
->write_mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
877 if (dst
->write_mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
883 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param
*param
, BOOL fixup
, char *swizzle_str
)
885 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
886 * but addressed as "rgba". To fix this we need to swap the register's x
887 * and z components. */
888 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
889 char *ptr
= swizzle_str
;
891 /* swizzle bits fields: wwzzyyxx */
892 DWORD swizzle
= param
->swizzle
;
893 DWORD swizzle_x
= swizzle
& 0x03;
894 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
895 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
896 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
898 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
899 * generate a swizzle string. Unless we need to our own swizzling. */
900 if (swizzle
!= WINED3DSP_NOSWIZZLE
|| fixup
)
903 if (swizzle_x
== swizzle_y
&& swizzle_x
== swizzle_z
&& swizzle_x
== swizzle_w
) {
904 *ptr
++ = swizzle_chars
[swizzle_x
];
906 *ptr
++ = swizzle_chars
[swizzle_x
];
907 *ptr
++ = swizzle_chars
[swizzle_y
];
908 *ptr
++ = swizzle_chars
[swizzle_z
];
909 *ptr
++ = swizzle_chars
[swizzle_w
];
916 static void shader_arb_request_a0(const struct wined3d_shader_instruction
*ins
, const char *src
)
918 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
919 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
921 if (!strcmp(priv
->addr_reg
, src
)) return;
923 strcpy(priv
->addr_reg
, src
);
924 shader_addline(buffer
, "ARL A0.x, %s;\n", src
);
927 static void shader_arb_get_src_param(const struct wined3d_shader_instruction
*ins
,
928 const struct wined3d_shader_src_param
*src
, unsigned int tmpreg
, char *outregstr
);
930 static void shader_arb_get_register_name(const struct wined3d_shader_instruction
*ins
,
931 const struct wined3d_shader_register
*reg
, char *register_name
, BOOL
*is_color
)
933 /* oPos, oFog and oPts in D3D */
934 static const char * const rastout_reg_names
[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
935 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
936 BOOL pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
937 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
943 case WINED3DSPR_TEMP
:
944 sprintf(register_name
, "R%u", reg
->idx
);
947 case WINED3DSPR_INPUT
:
950 if(This
->baseShader
.reg_maps
.shader_version
.major
< 3)
952 if (!reg
->idx
) strcpy(register_name
, "fragment.color.primary");
953 else strcpy(register_name
, "fragment.color.secondary");
960 shader_arb_get_src_param(ins
, reg
->rel_addr
, 0, rel_reg
);
962 if (!strcmp(rel_reg
, "**aL_emul**"))
964 DWORD idx
= ctx
->aL
+ reg
->idx
;
965 if(idx
< MAX_REG_INPUT
)
967 strcpy(register_name
, ctx
->ps_input
[idx
]);
971 ERR("Pixel shader input register out of bounds: %u\n", idx
);
972 sprintf(register_name
, "out_of_bounds_%u", idx
);
975 else if(This
->baseShader
.reg_maps
.input_registers
& 0x0300)
977 /* There are two ways basically:
979 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
980 * That means trouble if the loop also contains a breakc or if the control values
981 * aren't local constants.
982 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
983 * source dynamically. The trouble is that we cannot simply read aL.y because it
984 * is an ADDRESS register. We could however push it, load .zw with a value and use
985 * ADAC to load the condition code register and pop it again afterwards
987 FIXME("Relative input register addressing with more than 8 registers\n");
989 /* This is better than nothing for now */
990 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
992 else if(ctx
->cur_ps_args
->super
.vp_mode
!= vertexshader
)
994 /* This is problematic because we'd have to consult the ctx->ps_input strings
995 * for where to find the varying. Some may be "0.0", others can be texcoords or
996 * colors. This needs either a pipeline replacement to make the vertex shader feed
997 * proper varyings, or loop unrolling
999 * For now use the texcoords and hope for the best
1001 FIXME("Non-vertex shader varying input with indirect addressing\n");
1002 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
1006 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
1007 * pulls GL_NV_fragment_program2 in
1009 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
1014 if(reg
->idx
< MAX_REG_INPUT
)
1016 strcpy(register_name
, ctx
->ps_input
[reg
->idx
]);
1020 ERR("Pixel shader input register out of bounds: %u\n", reg
->idx
);
1021 sprintf(register_name
, "out_of_bounds_%u", reg
->idx
);
1028 if (ctx
->cur_vs_args
->super
.swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
1029 sprintf(register_name
, "vertex.attrib[%u]", reg
->idx
);
1033 case WINED3DSPR_CONST
:
1034 if (!pshader
&& reg
->rel_addr
)
1038 UINT rel_offset
= ((IWineD3DVertexShaderImpl
*)This
)->rel_offset
;
1039 if(This
->baseShader
.reg_maps
.shader_version
.major
< 2) {
1040 sprintf(rel_reg
, "A0.x");
1042 shader_arb_get_src_param(ins
, reg
->rel_addr
, 0, rel_reg
);
1043 if(ctx
->target_version
== ARB
) {
1044 if (!strcmp(rel_reg
, "**aL_emul**"))
1048 shader_arb_request_a0(ins
, rel_reg
);
1049 sprintf(rel_reg
, "A0.x");
1054 sprintf(register_name
, "C[%u]", ctx
->aL
+ reg
->idx
);
1055 else if (reg
->idx
>= rel_offset
)
1056 sprintf(register_name
, "C[%s + %u]", rel_reg
, reg
->idx
- rel_offset
);
1058 sprintf(register_name
, "C[%s - %u]", rel_reg
, rel_offset
- reg
->idx
);
1062 if (This
->baseShader
.reg_maps
.usesrelconstF
)
1063 sprintf(register_name
, "C[%u]", reg
->idx
);
1065 sprintf(register_name
, "C%u", reg
->idx
);
1069 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1071 if(This
->baseShader
.reg_maps
.shader_version
.major
== 1 &&
1072 This
->baseShader
.reg_maps
.shader_version
.minor
<= 3) {
1073 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
1074 * and as source to most instructions. For some instructions it is the texcoord
1075 * input. Those instructions know about the special use
1077 sprintf(register_name
, "T%u", reg
->idx
);
1079 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
1080 sprintf(register_name
, "fragment.texcoord[%u]", reg
->idx
);
1085 if(This
->baseShader
.reg_maps
.shader_version
.major
== 1 || ctx
->target_version
>= NV2
)
1087 sprintf(register_name
, "A%u", reg
->idx
);
1091 sprintf(register_name
, "A%u_SHADOW", reg
->idx
);
1096 case WINED3DSPR_COLOROUT
:
1097 if (ctx
->cur_ps_args
->super
.srgb_correction
&& !reg
->idx
)
1099 strcpy(register_name
, "TMP_COLOR");
1103 if(ctx
->cur_ps_args
->super
.srgb_correction
) FIXME("sRGB correction on higher render targets\n");
1104 if(This
->baseShader
.reg_maps
.highest_render_target
> 0)
1106 sprintf(register_name
, "result.color[%u]", reg
->idx
);
1110 strcpy(register_name
, "result.color");
1115 case WINED3DSPR_RASTOUT
:
1116 if(reg
->idx
== 1) sprintf(register_name
, "%s", ctx
->fog_output
);
1117 else sprintf(register_name
, "%s", rastout_reg_names
[reg
->idx
]);
1120 case WINED3DSPR_DEPTHOUT
:
1121 strcpy(register_name
, "result.depth");
1124 case WINED3DSPR_ATTROUT
:
1125 /* case WINED3DSPR_OUTPUT: */
1126 if (pshader
) sprintf(register_name
, "oD[%u]", reg
->idx
);
1127 else strcpy(register_name
, ctx
->color_output
[reg
->idx
]);
1130 case WINED3DSPR_TEXCRDOUT
:
1133 sprintf(register_name
, "oT[%u]", reg
->idx
);
1137 if(This
->baseShader
.reg_maps
.shader_version
.major
< 3)
1139 strcpy(register_name
, ctx
->texcrd_output
[reg
->idx
]);
1143 strcpy(register_name
, ctx
->vs_output
[reg
->idx
]);
1148 case WINED3DSPR_LOOP
:
1149 if(ctx
->target_version
>= NV2
)
1151 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
1152 if(pshader
) sprintf(register_name
, "A0.x");
1153 else sprintf(register_name
, "aL.y");
1157 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
1158 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
1159 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
1160 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
1163 sprintf(register_name
, "**aL_emul**");
1168 case WINED3DSPR_CONSTINT
:
1169 sprintf(register_name
, "I%u", reg
->idx
);
1172 case WINED3DSPR_MISCTYPE
:
1175 sprintf(register_name
, "vpos");
1177 else if(reg
->idx
== 1)
1179 sprintf(register_name
, "fragment.facing.x");
1183 FIXME("Unknown MISCTYPE register index %u\n", reg
->idx
);
1188 FIXME("Unhandled register type %#x[%u]\n", reg
->type
, reg
->idx
);
1189 sprintf(register_name
, "unrecognized_register[%u]", reg
->idx
);
1194 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction
*ins
,
1195 const struct wined3d_shader_dst_param
*wined3d_dst
, char *str
)
1197 char register_name
[255];
1201 shader_arb_get_register_name(ins
, &wined3d_dst
->reg
, register_name
, &is_color
);
1202 strcpy(str
, register_name
);
1204 shader_arb_get_write_mask(ins
, wined3d_dst
, write_mask
);
1205 strcat(str
, write_mask
);
1208 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source
)
1210 switch(channel_source
)
1212 case CHANNEL_SOURCE_ZERO
: return "0";
1213 case CHANNEL_SOURCE_ONE
: return "1";
1214 case CHANNEL_SOURCE_X
: return "x";
1215 case CHANNEL_SOURCE_Y
: return "y";
1216 case CHANNEL_SOURCE_Z
: return "z";
1217 case CHANNEL_SOURCE_W
: return "w";
1219 FIXME("Unhandled channel source %#x\n", channel_source
);
1224 static void gen_color_correction(struct wined3d_shader_buffer
*buffer
, const char *reg
,
1225 DWORD dst_mask
, const char *one
, const char *two
, struct color_fixup_desc fixup
)
1229 if (is_complex_fixup(fixup
))
1231 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
1232 FIXME("Complex fixup (%#x) not supported\n", complex_fixup
);
1237 if (fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1238 if (fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1239 if (fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1240 if (fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1245 shader_addline(buffer
, "SWZ %s, %s, %s, %s, %s, %s;\n", reg
, reg
,
1246 shader_arb_get_fixup_swizzle(fixup
.x_source
), shader_arb_get_fixup_swizzle(fixup
.y_source
),
1247 shader_arb_get_fixup_swizzle(fixup
.z_source
), shader_arb_get_fixup_swizzle(fixup
.w_source
));
1251 if (fixup
.x_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_0
;
1252 if (fixup
.y_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_1
;
1253 if (fixup
.z_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_2
;
1254 if (fixup
.w_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_3
;
1260 char *ptr
= reg_mask
;
1262 if (mask
!= WINED3DSP_WRITEMASK_ALL
)
1265 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
1266 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
1267 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
1268 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
1272 shader_addline(buffer
, "MAD %s%s, %s, %s, -%s;\n", reg
, reg_mask
, reg
, two
, one
);
1276 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction
*ins
)
1279 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1280 if (!ins
->dst_count
) return "";
1282 mod
= ins
->dst
[0].modifiers
;
1284 /* Silently ignore PARTIALPRECISION if its not supported */
1285 if(priv
->target_version
== ARB
) mod
&= ~WINED3DSPDM_PARTIALPRECISION
;
1287 if(mod
& WINED3DSPDM_MSAMPCENTROID
)
1289 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1290 mod
&= ~WINED3DSPDM_MSAMPCENTROID
;
1295 case WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
:
1298 case WINED3DSPDM_SATURATE
:
1301 case WINED3DSPDM_PARTIALPRECISION
:
1308 FIXME("Unknown modifiers 0x%08x\n", mod
);
1313 #define TEX_PROJ 0x1
1314 #define TEX_BIAS 0x2
1316 #define TEX_DERIV 0x10
1318 static void shader_hw_sample(const struct wined3d_shader_instruction
*ins
, DWORD sampler_idx
,
1319 const char *dst_str
, const char *coord_reg
, WORD flags
, const char *dsx
, const char *dsy
)
1321 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1322 DWORD sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
1323 IWineD3DBaseTextureImpl
*texture
;
1324 const char *tex_type
;
1325 BOOL np2_fixup
= FALSE
;
1326 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1327 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1328 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1330 BOOL pshader
= shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
1332 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1333 if(!pshader
) sampler_idx
+= MAX_FRAGMENT_SAMPLERS
;
1335 switch(sampler_type
) {
1341 texture
= device
->stateBlock
->state
.textures
[sampler_idx
];
1342 if (texture
&& texture
->baseTexture
.target
== GL_TEXTURE_RECTANGLE_ARB
)
1348 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
1350 if (priv
->cur_np2fixup_info
->super
.active
& (1 << sampler_idx
))
1352 if (flags
) FIXME("Only ordinary sampling from NP2 textures is supported.\n");
1353 else np2_fixup
= TRUE
;
1358 case WINED3DSTT_VOLUME
:
1362 case WINED3DSTT_CUBE
:
1367 ERR("Unexpected texture type %d\n", sampler_type
);
1371 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1372 * so don't use shader_arb_get_modifier
1374 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
) mod
= "_SAT";
1377 /* Fragment samplers always have indentity mapping */
1378 if(sampler_idx
>= MAX_FRAGMENT_SAMPLERS
)
1380 sampler_idx
= priv
->cur_vs_args
->vertex
.samplers
[sampler_idx
- MAX_FRAGMENT_SAMPLERS
];
1383 if (flags
& TEX_DERIV
)
1385 if(flags
& TEX_PROJ
) FIXME("Projected texture sampling with custom derivatives\n");
1386 if(flags
& TEX_BIAS
) FIXME("Biased texture sampling with custom derivatives\n");
1387 shader_addline(buffer
, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
,
1388 dsx
, dsy
,sampler_idx
, tex_type
);
1390 else if(flags
& TEX_LOD
)
1392 if(flags
& TEX_PROJ
) FIXME("Projected texture sampling with explicit lod\n");
1393 if(flags
& TEX_BIAS
) FIXME("Biased texture sampling with explicit lod\n");
1394 shader_addline(buffer
, "TXL%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
,
1395 sampler_idx
, tex_type
);
1397 else if (flags
& TEX_BIAS
)
1399 /* Shouldn't be possible, but let's check for it */
1400 if(flags
& TEX_PROJ
) FIXME("Biased and Projected texture sampling\n");
1401 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1402 shader_addline(buffer
, "TXB%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1404 else if (flags
& TEX_PROJ
)
1406 shader_addline(buffer
, "TXP%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1412 const unsigned char idx
= priv
->cur_np2fixup_info
->super
.idx
[sampler_idx
];
1413 shader_addline(buffer
, "MUL TA, np2fixup[%u].%s, %s;\n", idx
>> 1,
1414 (idx
% 2) ? "zwxy" : "xyzw", coord_reg
);
1416 shader_addline(buffer
, "TEX%s %s, TA, texture[%u], %s;\n", mod
, dst_str
, sampler_idx
, tex_type
);
1419 shader_addline(buffer
, "TEX%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1424 gen_color_correction(buffer
, dst_str
, ins
->dst
[0].write_mask
,
1425 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL
, ARB_ONE
),
1426 arb_get_helper_value(WINED3D_SHADER_TYPE_PIXEL
, ARB_TWO
),
1427 priv
->cur_ps_args
->super
.color_fixup
[sampler_idx
]);
1431 static void shader_arb_get_src_param(const struct wined3d_shader_instruction
*ins
,
1432 const struct wined3d_shader_src_param
*src
, unsigned int tmpreg
, char *outregstr
)
1434 /* Generate a line that does the input modifier computation and return the input register to use */
1435 BOOL is_color
= FALSE
;
1439 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1440 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1441 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
1442 const char *two
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_TWO
);
1444 /* Assume a new line will be added */
1447 /* Get register name */
1448 shader_arb_get_register_name(ins
, &src
->reg
, regstr
, &is_color
);
1449 shader_arb_get_swizzle(src
, is_color
, swzstr
);
1451 switch (src
->modifiers
)
1453 case WINED3DSPSM_NONE
:
1454 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
1457 case WINED3DSPSM_NEG
:
1458 sprintf(outregstr
, "-%s%s", regstr
, swzstr
);
1461 case WINED3DSPSM_BIAS
:
1462 shader_addline(buffer
, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg
, regstr
);
1464 case WINED3DSPSM_BIASNEG
:
1465 shader_addline(buffer
, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg
, regstr
);
1467 case WINED3DSPSM_SIGN
:
1468 shader_addline(buffer
, "MAD T%c, %s, %s, -%s;\n", 'A' + tmpreg
, regstr
, two
, one
);
1470 case WINED3DSPSM_SIGNNEG
:
1471 shader_addline(buffer
, "MAD T%c, %s, %s, %s;\n", 'A' + tmpreg
, regstr
, two
, one
);
1473 case WINED3DSPSM_COMP
:
1474 shader_addline(buffer
, "SUB T%c, %s, %s;\n", 'A' + tmpreg
, one
, regstr
);
1476 case WINED3DSPSM_X2
:
1477 shader_addline(buffer
, "ADD T%c, %s, %s;\n", 'A' + tmpreg
, regstr
, regstr
);
1479 case WINED3DSPSM_X2NEG
:
1480 shader_addline(buffer
, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg
, regstr
, regstr
);
1482 case WINED3DSPSM_DZ
:
1483 shader_addline(buffer
, "RCP T%c, %s.z;\n", 'A' + tmpreg
, regstr
);
1484 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
1486 case WINED3DSPSM_DW
:
1487 shader_addline(buffer
, "RCP T%c, %s.w;\n", 'A' + tmpreg
, regstr
);
1488 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
1490 case WINED3DSPSM_ABS
:
1491 if(ctx
->target_version
>= NV2
) {
1492 sprintf(outregstr
, "|%s%s|", regstr
, swzstr
);
1495 shader_addline(buffer
, "ABS T%c, %s;\n", 'A' + tmpreg
, regstr
);
1498 case WINED3DSPSM_ABSNEG
:
1499 if(ctx
->target_version
>= NV2
) {
1500 sprintf(outregstr
, "-|%s%s|", regstr
, swzstr
);
1502 shader_addline(buffer
, "ABS T%c, %s;\n", 'A' + tmpreg
, regstr
);
1503 sprintf(outregstr
, "-T%c%s", 'A' + tmpreg
, swzstr
);
1508 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
1512 /* Return modified or original register, with swizzle */
1514 sprintf(outregstr
, "T%c%s", 'A' + tmpreg
, swzstr
);
1517 static void pshader_hw_bem(const struct wined3d_shader_instruction
*ins
)
1519 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1520 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1522 char src_name
[2][50];
1523 DWORD sampler_code
= dst
->reg
.idx
;
1525 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1527 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1529 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1530 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1533 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1534 shader_addline(buffer
, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code
);
1535 shader_addline(buffer
, "DP3 TC.r, TA, %s;\n", src_name
[1]);
1536 shader_addline(buffer
, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code
);
1537 shader_addline(buffer
, "DP3 TC.g, TA, %s;\n", src_name
[1]);
1539 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1540 shader_addline(buffer
, "ADD %s, %s, TC;\n", dst_name
, src_name
[0]);
1543 static DWORD
negate_modifiers(DWORD mod
, char *extra_char
)
1548 case WINED3DSPSM_NONE
: return WINED3DSPSM_NEG
;
1549 case WINED3DSPSM_NEG
: return WINED3DSPSM_NONE
;
1550 case WINED3DSPSM_BIAS
: return WINED3DSPSM_BIASNEG
;
1551 case WINED3DSPSM_BIASNEG
: return WINED3DSPSM_BIAS
;
1552 case WINED3DSPSM_SIGN
: return WINED3DSPSM_SIGNNEG
;
1553 case WINED3DSPSM_SIGNNEG
: return WINED3DSPSM_SIGN
;
1554 case WINED3DSPSM_COMP
: *extra_char
= '-'; return WINED3DSPSM_COMP
;
1555 case WINED3DSPSM_X2
: return WINED3DSPSM_X2NEG
;
1556 case WINED3DSPSM_X2NEG
: return WINED3DSPSM_X2
;
1557 case WINED3DSPSM_DZ
: *extra_char
= '-'; return WINED3DSPSM_DZ
;
1558 case WINED3DSPSM_DW
: *extra_char
= '-'; return WINED3DSPSM_DW
;
1559 case WINED3DSPSM_ABS
: return WINED3DSPSM_ABSNEG
;
1560 case WINED3DSPSM_ABSNEG
: return WINED3DSPSM_ABS
;
1562 FIXME("Unknown modifier %u\n", mod
);
1566 static void pshader_hw_cnd(const struct wined3d_shader_instruction
*ins
)
1568 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1569 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1571 char src_name
[3][50];
1572 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1573 ins
->ctx
->reg_maps
->shader_version
.minor
);
1576 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1577 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1579 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
1580 if (shader_version
<= WINED3D_SHADER_VERSION(1, 3) && ins
->coissue
)
1582 shader_addline(buffer
, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
[1]);
1584 struct wined3d_shader_src_param src0_copy
= ins
->src
[0];
1587 /* src0 may have a negate srcmod set, so we can't blindly add "-" to the name */
1588 src0_copy
.modifiers
= negate_modifiers(src0_copy
.modifiers
, &extra_neg
);
1590 shader_arb_get_src_param(ins
, &src0_copy
, 0, src_name
[0]);
1591 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1592 shader_addline(buffer
, "ADD TA, %c%s, coefdiv.x;\n", extra_neg
, src_name
[0]);
1593 /* No modifiers supported on CMP */
1594 shader_addline(buffer
, "CMP %s, TA, %s, %s;\n", dst_name
, src_name
[1], src_name
[2]);
1596 /* _SAT on CMP doesn't make much sense, but it is not a pure NOP */
1597 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
1599 shader_arb_get_register_name(ins
, &dst
->reg
, src_name
[0], &is_color
);
1600 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, dst_name
);
1605 static void pshader_hw_cmp(const struct wined3d_shader_instruction
*ins
)
1607 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1608 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1610 char src_name
[3][50];
1613 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1615 /* Generate input register names (with modifiers) */
1616 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1617 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1618 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1620 /* No modifiers are supported on CMP */
1621 shader_addline(buffer
, "CMP %s, %s, %s, %s;\n", dst_name
,
1622 src_name
[0], src_name
[2], src_name
[1]);
1624 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
1626 shader_arb_get_register_name(ins
, &dst
->reg
, src_name
[0], &is_color
);
1627 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, src_name
[0]);
1631 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1632 * dst = dot2(src0, src1) + src2 */
1633 static void pshader_hw_dp2add(const struct wined3d_shader_instruction
*ins
)
1635 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1636 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1638 char src_name
[3][50];
1639 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1641 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1642 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1643 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1645 if(ctx
->target_version
>= NV3
)
1647 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1648 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1649 shader_addline(buffer
, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins
),
1650 dst_name
, src_name
[0], src_name
[1], src_name
[2]);
1652 else if(ctx
->target_version
>= NV2
)
1654 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1655 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1656 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1657 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1659 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1661 * .xyxy and other swizzles that we could get with this are not valid in
1662 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1664 struct wined3d_shader_src_param tmp_param
= ins
->src
[1];
1665 DWORD swizzle
= tmp_param
.swizzle
& 0xf; /* Selects .xy */
1666 tmp_param
.swizzle
= swizzle
| (swizzle
<< 4); /* Creates .xyxy */
1668 shader_arb_get_src_param(ins
, &tmp_param
, 1, src_name
[1]);
1670 shader_addline(buffer
, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins
),
1671 dst_name
, src_name
[2], src_name
[0], src_name
[1]);
1675 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1676 /* 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]
1677 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1679 shader_addline(buffer
, "MOV TA, %s;\n", src_name
[0]);
1680 shader_addline(buffer
, "MOV TA.z, 0.0;\n");
1681 shader_addline(buffer
, "DP3 TA, TA, %s;\n", src_name
[1]);
1682 shader_addline(buffer
, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
[2]);
1686 /* Map the opcode 1-to-1 to the GL code */
1687 static void shader_hw_map2gl(const struct wined3d_shader_instruction
*ins
)
1689 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1690 const char *instruction
;
1691 char arguments
[256], dst_str
[50];
1693 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1695 switch (ins
->handler_idx
)
1697 case WINED3DSIH_ABS
: instruction
= "ABS"; break;
1698 case WINED3DSIH_ADD
: instruction
= "ADD"; break;
1699 case WINED3DSIH_CRS
: instruction
= "XPD"; break;
1700 case WINED3DSIH_DP3
: instruction
= "DP3"; break;
1701 case WINED3DSIH_DP4
: instruction
= "DP4"; break;
1702 case WINED3DSIH_DST
: instruction
= "DST"; break;
1703 case WINED3DSIH_FRC
: instruction
= "FRC"; break;
1704 case WINED3DSIH_LIT
: instruction
= "LIT"; break;
1705 case WINED3DSIH_LRP
: instruction
= "LRP"; break;
1706 case WINED3DSIH_MAD
: instruction
= "MAD"; break;
1707 case WINED3DSIH_MAX
: instruction
= "MAX"; break;
1708 case WINED3DSIH_MIN
: instruction
= "MIN"; break;
1709 case WINED3DSIH_MOV
: instruction
= "MOV"; break;
1710 case WINED3DSIH_MUL
: instruction
= "MUL"; break;
1711 case WINED3DSIH_SGE
: instruction
= "SGE"; break;
1712 case WINED3DSIH_SLT
: instruction
= "SLT"; break;
1713 case WINED3DSIH_SUB
: instruction
= "SUB"; break;
1714 case WINED3DSIH_MOVA
:instruction
= "ARR"; break;
1715 case WINED3DSIH_DSX
: instruction
= "DDX"; break;
1716 default: instruction
= "";
1717 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
1721 /* Note that shader_arb_add_dst_param() adds spaces. */
1722 arguments
[0] = '\0';
1723 shader_arb_get_dst_param(ins
, dst
, dst_str
);
1724 for (i
= 0; i
< ins
->src_count
; ++i
)
1727 strcat(arguments
, ", ");
1728 shader_arb_get_src_param(ins
, &ins
->src
[i
], i
, operand
);
1729 strcat(arguments
, operand
);
1731 shader_addline(buffer
, "%s%s %s%s;\n", instruction
, shader_arb_get_modifier(ins
), dst_str
, arguments
);
1734 static void shader_hw_nop(const struct wined3d_shader_instruction
*ins
)
1736 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1737 shader_addline(buffer
, "NOP;\n");
1740 static void shader_hw_mov(const struct wined3d_shader_instruction
*ins
)
1742 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1743 BOOL pshader
= shader_is_pshader_version(shader
->baseShader
.reg_maps
.shader_version
.type
);
1744 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1745 const char *zero
= arb_get_helper_value(shader
->baseShader
.reg_maps
.shader_version
.type
, ARB_ZERO
);
1746 const char *one
= arb_get_helper_value(shader
->baseShader
.reg_maps
.shader_version
.type
, ARB_ONE
);
1747 const char *two
= arb_get_helper_value(shader
->baseShader
.reg_maps
.shader_version
.type
, ARB_TWO
);
1749 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1750 char src0_param
[256];
1752 if(ins
->handler_idx
== WINED3DSIH_MOVA
) {
1754 const char *offset
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_VS_REL_OFFSET
);
1756 if(ctx
->target_version
>= NV2
) {
1757 shader_hw_map2gl(ins
);
1760 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_param
);
1761 shader_arb_get_write_mask(ins
, &ins
->dst
[0], write_mask
);
1763 /* This implements the mova formula used in GLSL. The first two instructions
1764 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1768 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1770 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1771 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1773 shader_addline(buffer
, "SGE A0_SHADOW%s, %s, %s;\n", write_mask
, src0_param
, zero
);
1774 shader_addline(buffer
, "MAD A0_SHADOW%s, A0_SHADOW, %s, -%s;\n", write_mask
, two
, one
);
1776 shader_addline(buffer
, "ABS TA%s, %s;\n", write_mask
, src0_param
);
1777 shader_addline(buffer
, "ADD TA%s, TA, rel_addr_const.x;\n", write_mask
);
1778 shader_addline(buffer
, "FLR TA%s, TA;\n", write_mask
);
1779 if (((IWineD3DVertexShaderImpl
*)shader
)->rel_offset
)
1781 shader_addline(buffer
, "ADD TA%s, TA, %s;\n", write_mask
, offset
);
1783 shader_addline(buffer
, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask
);
1785 ((struct shader_arb_ctx_priv
*)ins
->ctx
->backend_data
)->addr_reg
[0] = '\0';
1786 } else if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
1787 && !shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)
1788 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
1790 src0_param
[0] = '\0';
1791 if (((IWineD3DVertexShaderImpl
*)shader
)->rel_offset
)
1793 const char *offset
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_VS_REL_OFFSET
);
1794 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_param
);
1795 shader_addline(buffer
, "ADD TA.x, %s, %s;\n", src0_param
, offset
);
1796 shader_addline(buffer
, "ARL A0.x, TA.x;\n");
1800 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1801 * with more than one component. Thus replicate the first source argument over all
1802 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1803 struct wined3d_shader_src_param tmp_src
= ins
->src
[0];
1804 tmp_src
.swizzle
= (tmp_src
.swizzle
& 0x3) * 0x55;
1805 shader_arb_get_src_param(ins
, &tmp_src
, 0, src0_param
);
1806 shader_addline(buffer
, "ARL A0.x, %s;\n", src0_param
);
1809 else if (ins
->dst
[0].reg
.type
== WINED3DSPR_COLOROUT
&& !ins
->dst
[0].reg
.idx
&& pshader
)
1811 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) shader
;
1812 if(ctx
->cur_ps_args
->super
.srgb_correction
&& ps
->color0_mov
)
1814 shader_addline(buffer
, "#mov handled in srgb write code\n");
1817 shader_hw_map2gl(ins
);
1821 shader_hw_map2gl(ins
);
1825 static void pshader_hw_texkill(const struct wined3d_shader_instruction
*ins
)
1827 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1828 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1831 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1832 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1834 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1836 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
1838 const char *kilsrc
= "TA";
1841 shader_arb_get_register_name(ins
, &dst
->reg
, reg_dest
, &is_color
);
1842 if(dst
->write_mask
== WINED3DSP_WRITEMASK_ALL
)
1848 /* Sigh. KIL doesn't support swizzles/writemasks. KIL passes a writemask, but ".xy" for example
1849 * is not valid as a swizzle in ARB (needs ".xyyy"). Use SWZ to load the register properly, and set
1850 * masked out components to 0(won't kill)
1852 char x
= '0', y
= '0', z
= '0', w
= '0';
1853 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
) x
= 'x';
1854 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
) y
= 'y';
1855 if(dst
->write_mask
& WINED3DSP_WRITEMASK_2
) z
= 'z';
1856 if(dst
->write_mask
& WINED3DSP_WRITEMASK_3
) w
= 'w';
1857 shader_addline(buffer
, "SWZ TA, %s, %c, %c, %c, %c;\n", reg_dest
, x
, y
, z
, w
);
1859 shader_addline(buffer
, "KIL %s;\n", kilsrc
);
1861 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1862 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1864 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1865 * or pass in any temporary register(in shader phase 2)
1867 if(ins
->ctx
->reg_maps
->shader_version
.minor
<= 3) {
1868 sprintf(reg_dest
, "fragment.texcoord[%u]", dst
->reg
.idx
);
1870 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1872 shader_addline(buffer
, "SWZ TA, %s, x, y, z, 1;\n", reg_dest
);
1873 shader_addline(buffer
, "KIL TA;\n");
1877 static void pshader_hw_tex(const struct wined3d_shader_instruction
*ins
)
1879 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1880 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1881 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1882 ins
->ctx
->reg_maps
->shader_version
.minor
);
1883 struct wined3d_shader_src_param src
;
1887 DWORD reg_sampler_code
;
1890 /* All versions have a destination register */
1891 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1893 /* 1.0-1.4: Use destination register number as texture code.
1894 2.0+: Use provided sampler number as texure code. */
1895 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
1896 reg_sampler_code
= dst
->reg
.idx
;
1898 reg_sampler_code
= ins
->src
[1].reg
.idx
;
1900 /* 1.0-1.3: Use the texcoord varying.
1901 1.4+: Use provided coordinate source register. */
1902 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1903 sprintf(reg_coord
, "fragment.texcoord[%u]", reg_sampler_code
);
1905 /* TEX is the only instruction that can handle DW and DZ natively */
1907 if(src
.modifiers
== WINED3DSPSM_DW
) src
.modifiers
= WINED3DSPSM_NONE
;
1908 if(src
.modifiers
== WINED3DSPSM_DZ
) src
.modifiers
= WINED3DSPSM_NONE
;
1909 shader_arb_get_src_param(ins
, &src
, 0, reg_coord
);
1913 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1914 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1915 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1917 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1920 if (reg_sampler_code
< MAX_TEXTURES
)
1921 flags
= priv
->cur_ps_args
->super
.tex_transform
>> reg_sampler_code
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
1922 if (flags
& WINED3D_PSARGS_PROJECTED
)
1923 myflags
|= TEX_PROJ
;
1925 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
1927 DWORD src_mod
= ins
->src
[0].modifiers
;
1928 if (src_mod
== WINED3DSPSM_DZ
) {
1929 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1930 * varying register, so we need a temp reg
1932 shader_addline(ins
->ctx
->buffer
, "SWZ TA, %s, x, y, z, z;\n", reg_coord
);
1933 strcpy(reg_coord
, "TA");
1934 myflags
|= TEX_PROJ
;
1935 } else if(src_mod
== WINED3DSPSM_DW
) {
1936 myflags
|= TEX_PROJ
;
1939 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) myflags
|= TEX_PROJ
;
1940 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) myflags
|= TEX_BIAS
;
1942 shader_hw_sample(ins
, reg_sampler_code
, reg_dest
, reg_coord
, myflags
, NULL
, NULL
);
1945 static void pshader_hw_texcoord(const struct wined3d_shader_instruction
*ins
)
1947 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1948 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1949 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1950 ins
->ctx
->reg_maps
->shader_version
.minor
);
1953 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1955 DWORD reg
= dst
->reg
.idx
;
1957 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1958 shader_addline(buffer
, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str
, reg
);
1962 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_src
);
1963 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1964 shader_addline(buffer
, "MOV %s, %s;\n", dst_str
, reg_src
);
1968 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction
*ins
)
1970 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1973 DWORD reg1
= ins
->dst
[0].reg
.idx
;
1977 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
1978 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1979 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
1980 /* Move .x first in case src_str is "TA" */
1981 shader_addline(buffer
, "MOV TA.y, %s.x;\n", src_str
);
1982 shader_addline(buffer
, "MOV TA.x, %s.w;\n", src_str
);
1983 if (reg1
< MAX_TEXTURES
)
1985 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1986 flags
= priv
->cur_ps_args
->super
.tex_transform
>> reg1
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
;
1988 shader_hw_sample(ins
, reg1
, dst_str
, "TA", flags
& WINED3D_PSARGS_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
1991 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction
*ins
)
1993 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
1995 DWORD reg1
= ins
->dst
[0].reg
.idx
;
1999 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
2000 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2001 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
2002 shader_addline(buffer
, "MOV TA.x, %s.y;\n", src_str
);
2003 shader_addline(buffer
, "MOV TA.y, %s.z;\n", src_str
);
2004 shader_hw_sample(ins
, reg1
, dst_str
, "TA", 0, NULL
, NULL
);
2007 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
2009 DWORD reg1
= ins
->dst
[0].reg
.idx
;
2013 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
2014 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2015 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
2016 shader_hw_sample(ins
, reg1
, dst_str
, src_str
, 0, NULL
, NULL
);
2019 static void pshader_hw_texbem(const struct wined3d_shader_instruction
*ins
)
2021 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2022 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2023 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2024 char reg_coord
[40], dst_reg
[50], src_reg
[50];
2025 DWORD reg_dest_code
;
2027 /* All versions have a destination register. The Tx where the texture coordinates come
2028 * from is the varying incarnation of the texture register
2030 reg_dest_code
= dst
->reg
.idx
;
2031 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_reg
);
2032 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_reg
);
2033 sprintf(reg_coord
, "fragment.texcoord[%u]", reg_dest_code
);
2035 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
2036 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
2038 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
2039 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
2041 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
2042 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
2045 shader_addline(buffer
, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code
);
2046 shader_addline(buffer
, "DP3 TA.x, TB, %s;\n", src_reg
);
2047 shader_addline(buffer
, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code
);
2048 shader_addline(buffer
, "DP3 TA.y, TB, %s;\n", src_reg
);
2050 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2051 * so we can't let the GL handle this.
2053 if ((priv
->cur_ps_args
->super
.tex_transform
>> reg_dest_code
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
2054 & WINED3D_PSARGS_PROJECTED
)
2056 shader_addline(buffer
, "RCP TB.w, %s.w;\n", reg_coord
);
2057 shader_addline(buffer
, "MUL TB.xy, %s, TB.w;\n", reg_coord
);
2058 shader_addline(buffer
, "ADD TA.xy, TA, TB;\n");
2060 shader_addline(buffer
, "ADD TA.xy, TA, %s;\n", reg_coord
);
2063 shader_hw_sample(ins
, reg_dest_code
, dst_reg
, "TA", 0, NULL
, NULL
);
2065 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
2067 /* No src swizzles are allowed, so this is ok */
2068 shader_addline(buffer
, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
2069 src_reg
, reg_dest_code
, reg_dest_code
);
2070 shader_addline(buffer
, "MUL %s, %s, TA;\n", dst_reg
, dst_reg
);
2074 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
2076 DWORD reg
= ins
->dst
[0].reg
.idx
;
2077 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2078 char src0_name
[50], dst_name
[50];
2080 struct wined3d_shader_register tmp_reg
= ins
->dst
[0].reg
;
2082 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2083 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
2084 * T<reg+1> register. Use this register to store the calculated vector
2086 tmp_reg
.idx
= reg
+ 1;
2087 shader_arb_get_register_name(ins
, &tmp_reg
, dst_name
, &is_color
);
2088 shader_addline(buffer
, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name
, reg
, src0_name
);
2091 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
2093 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2095 DWORD reg
= ins
->dst
[0].reg
.idx
;
2096 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2102 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
2103 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
2105 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2106 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2107 shader_addline(buffer
, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
2108 flags
= reg
< MAX_TEXTURES
? priv
->cur_ps_args
->super
.tex_transform
>> reg
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
: 0;
2109 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2112 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
2114 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2115 SHADER_PARSE_STATE
*current_state
= &shader
->baseShader
.parse_state
;
2116 DWORD reg
= ins
->dst
[0].reg
.idx
;
2117 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2118 char src0_name
[50], dst_name
[50];
2119 struct wined3d_shader_register tmp_reg
= ins
->dst
[0].reg
;
2122 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
2123 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
2124 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
2126 tmp_reg
.idx
= reg
+ 2 - current_state
->current_row
;
2127 shader_arb_get_register_name(ins
, &tmp_reg
, dst_name
, &is_color
);
2129 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2130 shader_addline(buffer
, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
2131 dst_name
, 'x' + current_state
->current_row
, reg
, src0_name
);
2132 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2135 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
2137 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2138 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2139 SHADER_PARSE_STATE
*current_state
= &shader
->baseShader
.parse_state
;
2141 DWORD reg
= ins
->dst
[0].reg
.idx
;
2142 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2144 char src0_name
[50], dst_name
[50];
2147 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2148 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2149 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name
, reg
, src0_name
);
2151 /* Sample the texture using the calculated coordinates */
2152 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2153 flags
= reg
< MAX_TEXTURES
? priv
->cur_ps_args
->super
.tex_transform
>> reg
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
: 0;
2154 shader_hw_sample(ins
, reg
, dst_str
, dst_name
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2155 current_state
->current_row
= 0;
2158 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
2160 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2161 IWineD3DDeviceImpl
*deviceImpl
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
2162 SHADER_PARSE_STATE
*current_state
= &shader
->baseShader
.parse_state
;
2164 DWORD reg
= ins
->dst
[0].reg
.idx
;
2165 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2171 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
2172 * components for temporary data storage
2174 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
2175 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2176 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
2178 /* Construct the eye-ray vector from w coordinates */
2179 shader_addline(buffer
, "MOV TB.x, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[0]);
2180 shader_addline(buffer
, "MOV TB.y, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[1]);
2181 shader_addline(buffer
, "MOV TB.z, fragment.texcoord[%u].w;\n", reg
);
2183 /* Calculate reflection vector
2185 shader_addline(buffer
, "DP3 %s.w, %s, TB;\n", dst_reg
, dst_reg
);
2186 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
2187 shader_addline(buffer
, "DP3 TB.w, %s, %s;\n", dst_reg
, dst_reg
);
2188 shader_addline(buffer
, "RCP TB.w, TB.w;\n");
2189 shader_addline(buffer
, "MUL %s.w, %s.w, TB.w;\n", dst_reg
, dst_reg
);
2190 shader_addline(buffer
, "MUL %s, %s.w, %s;\n", dst_reg
, dst_reg
, dst_reg
);
2191 shader_addline(buffer
, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg
, dst_reg
);
2193 /* Sample the texture using the calculated coordinates */
2194 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2195 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->state
.texture_states
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
2196 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2197 current_state
->current_row
= 0;
2200 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
2202 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2203 IWineD3DDeviceImpl
*deviceImpl
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
2204 SHADER_PARSE_STATE
*current_state
= &shader
->baseShader
.parse_state
;
2206 DWORD reg
= ins
->dst
[0].reg
.idx
;
2207 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2214 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
2215 shader_arb_get_src_param(ins
, &ins
->src
[0], 1, src1_name
);
2216 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
2217 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
2218 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
2220 /* Calculate reflection vector.
2223 * dst_reg.xyz = 2 * --------- * N - E
2226 * Which normalizes the normal vector
2228 shader_addline(buffer
, "DP3 %s.w, %s, %s;\n", dst_reg
, dst_reg
, src1_name
);
2229 shader_addline(buffer
, "DP3 TC.w, %s, %s;\n", dst_reg
, dst_reg
);
2230 shader_addline(buffer
, "RCP TC.w, TC.w;\n");
2231 shader_addline(buffer
, "MUL %s.w, %s.w, TC.w;\n", dst_reg
, dst_reg
);
2232 shader_addline(buffer
, "MUL %s, %s.w, %s;\n", dst_reg
, dst_reg
, dst_reg
);
2233 shader_addline(buffer
, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg
, dst_reg
, src1_name
);
2235 /* Sample the texture using the calculated coordinates */
2236 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2237 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->state
.texture_states
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
2238 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
2239 current_state
->current_row
= 0;
2242 static void pshader_hw_texdepth(const struct wined3d_shader_instruction
*ins
)
2244 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2245 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2247 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2248 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
2250 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
2251 * which is essentially an input, is the destination register because it is the first
2252 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
2253 * here(writemasks/swizzles are not valid on texdepth)
2255 shader_arb_get_dst_param(ins
, dst
, dst_name
);
2257 /* According to the msdn, the source register(must be r5) is unusable after
2258 * the texdepth instruction, so we're free to modify it
2260 shader_addline(buffer
, "MIN %s.y, %s.y, %s;\n", dst_name
, dst_name
, one
);
2262 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2263 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2264 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2266 shader_addline(buffer
, "RCP %s.y, %s.y;\n", dst_name
, dst_name
);
2267 shader_addline(buffer
, "MUL TA.x, %s.x, %s.y;\n", dst_name
, dst_name
);
2268 shader_addline(buffer
, "MIN TA.x, TA.x, %s;\n", one
);
2269 shader_addline(buffer
, "MAX result.depth, TA.x, %s;\n", zero
);
2272 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
2273 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2274 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2275 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction
*ins
)
2277 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2278 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
2282 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2283 shader_addline(buffer
, "MOV TB, 0.0;\n");
2284 shader_addline(buffer
, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx
, src0
);
2286 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2287 shader_hw_sample(ins
, sampler_idx
, dst_str
, "TB", 0 /* Only one coord, can't be projected */, NULL
, NULL
);
2290 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2291 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2292 static void pshader_hw_texdp3(const struct wined3d_shader_instruction
*ins
)
2294 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2297 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2299 /* Handle output register */
2300 shader_arb_get_dst_param(ins
, dst
, dst_str
);
2301 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2302 shader_addline(buffer
, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str
, dst
->reg
.idx
, src0
);
2305 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2306 * Perform the 3rd row of a 3x3 matrix multiply */
2307 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction
*ins
)
2309 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2310 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2311 char dst_str
[50], dst_name
[50];
2315 shader_arb_get_dst_param(ins
, dst
, dst_str
);
2316 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2317 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2318 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name
, dst
->reg
.idx
, src0
);
2319 shader_addline(buffer
, "MOV %s, %s;\n", dst_str
, dst_name
);
2322 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2323 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2324 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2325 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2327 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
2329 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2330 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2331 char src0
[50], dst_name
[50];
2333 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2334 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
2336 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2337 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2338 shader_addline(buffer
, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name
, dst
->reg
.idx
, src0
);
2340 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2341 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2342 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2344 shader_addline(buffer
, "RCP %s.y, %s.y;\n", dst_name
, dst_name
);
2345 shader_addline(buffer
, "MUL %s.x, %s.x, %s.y;\n", dst_name
, dst_name
, dst_name
);
2346 shader_addline(buffer
, "MIN %s.x, %s.x, %s;\n", dst_name
, dst_name
, one
);
2347 shader_addline(buffer
, "MAX result.depth, %s.x, %s;\n", dst_name
, zero
);
2350 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2351 Vertex/Pixel shaders to ARB_vertex_program codes */
2352 static void shader_hw_mnxn(const struct wined3d_shader_instruction
*ins
)
2355 int nComponents
= 0;
2356 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2357 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2358 struct wined3d_shader_instruction tmp_ins
;
2360 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2362 /* Set constants for the temporary argument */
2363 tmp_ins
.ctx
= ins
->ctx
;
2364 tmp_ins
.dst_count
= 1;
2365 tmp_ins
.dst
= &tmp_dst
;
2366 tmp_ins
.src_count
= 2;
2367 tmp_ins
.src
= tmp_src
;
2369 switch(ins
->handler_idx
)
2371 case WINED3DSIH_M4x4
:
2373 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2375 case WINED3DSIH_M4x3
:
2377 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2379 case WINED3DSIH_M3x4
:
2381 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2383 case WINED3DSIH_M3x3
:
2385 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2387 case WINED3DSIH_M3x2
:
2389 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2392 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
2396 tmp_dst
= ins
->dst
[0];
2397 tmp_src
[0] = ins
->src
[0];
2398 tmp_src
[1] = ins
->src
[1];
2399 for (i
= 0; i
< nComponents
; i
++) {
2400 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2401 shader_hw_map2gl(&tmp_ins
);
2402 ++tmp_src
[1].reg
.idx
;
2406 static void shader_hw_rcp(const struct wined3d_shader_instruction
*ins
)
2408 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2409 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2410 const char *flt_eps
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_EPS
);
2415 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
); /* Destination */
2416 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2417 if (ins
->src
[0].swizzle
== WINED3DSP_NOSWIZZLE
)
2419 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2425 /* TODO: If the destination is readable, and not the same as the source, the destination
2426 * can be used instead of TA
2428 if (priv
->target_version
>= NV2
)
2430 shader_addline(buffer
, "MOVC TA.x, %s;\n", src
);
2431 shader_addline(buffer
, "MOV TA.x (EQ.x), %s;\n", flt_eps
);
2432 shader_addline(buffer
, "RCP%s %s, TA.x;\n", shader_arb_get_modifier(ins
), dst
);
2436 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2437 shader_addline(buffer
, "ABS TA.x, %s;\n", src
);
2438 shader_addline(buffer
, "SGE TA.y, -TA.x, %s;\n", zero
);
2439 shader_addline(buffer
, "MAD TA.x, TA.y, %s, %s;\n", flt_eps
, src
);
2440 shader_addline(buffer
, "RCP%s %s, TA.x;\n", shader_arb_get_modifier(ins
), dst
);
2444 static void shader_hw_scalar_op(const struct wined3d_shader_instruction
*ins
)
2446 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2447 const char *instruction
;
2452 switch(ins
->handler_idx
)
2454 case WINED3DSIH_RSQ
: instruction
= "RSQ"; break;
2455 case WINED3DSIH_RCP
: instruction
= "RCP"; break;
2456 case WINED3DSIH_EXP
: instruction
= "EX2"; break;
2457 case WINED3DSIH_EXPP
: instruction
= "EXP"; break;
2458 default: instruction
= "";
2459 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
2463 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
); /* Destination */
2464 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2465 if (ins
->src
[0].swizzle
== WINED3DSP_NOSWIZZLE
)
2467 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2473 shader_addline(buffer
, "%s%s %s, %s;\n", instruction
, shader_arb_get_modifier(ins
), dst
, src
);
2476 static void shader_hw_nrm(const struct wined3d_shader_instruction
*ins
)
2478 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2481 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2482 BOOL pshader
= shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2483 const char *zero
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ZERO
);
2485 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2486 shader_arb_get_src_param(ins
, &ins
->src
[0], 1 /* Use TB */, src_name
);
2488 /* In D3D, NRM of a vector with length zero returns zero. Catch this situation, as
2489 * otherwise NRM or RSQ would return NaN */
2490 if(pshader
&& priv
->target_version
>= NV3
)
2492 /* GL_NV_fragment_program2's NRM needs protection against length zero vectors too
2494 * TODO: Find out if DP3+NRM+MOV is really faster than DP3+RSQ+MUL
2496 shader_addline(buffer
, "DP3C TA, %s, %s;\n", src_name
, src_name
);
2497 shader_addline(buffer
, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
);
2498 shader_addline(buffer
, "MOV %s (EQ), %s;\n", dst_name
, zero
);
2500 else if(priv
->target_version
>= NV2
)
2502 shader_addline(buffer
, "DP3C TA.x, %s, %s;\n", src_name
, src_name
);
2503 shader_addline(buffer
, "RSQ TA.x (NE), TA.x;\n");
2504 shader_addline(buffer
, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins
), dst_name
,
2509 const char *one
= arb_get_helper_value(ins
->ctx
->reg_maps
->shader_version
.type
, ARB_ONE
);
2511 shader_addline(buffer
, "DP3 TA.x, %s, %s;\n", src_name
, src_name
);
2512 /* Pass any non-zero value to RSQ if the input vector has a length of zero. The
2513 * RSQ result doesn't matter, as long as multiplying it by 0 returns 0.
2515 shader_addline(buffer
, "SGE TA.y, -TA.x, %s;\n", zero
);
2516 shader_addline(buffer
, "MAD TA.x, %s, TA.y, TA.x;\n", one
);
2518 shader_addline(buffer
, "RSQ TA.x, TA.x;\n");
2519 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2520 shader_addline(buffer
, "MUL%s %s, %s, TA.x;\n", shader_arb_get_modifier(ins
), dst_name
,
2525 static void shader_hw_lrp(const struct wined3d_shader_instruction
*ins
)
2527 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2529 char src_name
[3][50];
2531 /* ARB_fragment_program has a convenient LRP instruction */
2532 if(shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)) {
2533 shader_hw_map2gl(ins
);
2537 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2538 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
2539 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
2540 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
2542 shader_addline(buffer
, "SUB TA, %s, %s;\n", src_name
[1], src_name
[2]);
2543 shader_addline(buffer
, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins
),
2544 dst_name
, src_name
[0], src_name
[2]);
2547 static void shader_hw_sincos(const struct wined3d_shader_instruction
*ins
)
2549 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2550 * must contain fixed constants. So we need a separate function to filter those constants and
2553 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2554 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2555 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2557 char src_name0
[50], src_name1
[50], src_name2
[50];
2560 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2561 if(shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)) {
2562 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2563 /* No modifiers are supported on SCS */
2564 shader_addline(buffer
, "SCS %s, %s;\n", dst_name
, src_name0
);
2566 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
2568 shader_arb_get_register_name(ins
, &dst
->reg
, src_name0
, &is_color
);
2569 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, src_name0
);
2571 } else if(priv
->target_version
>= NV2
) {
2572 shader_arb_get_register_name(ins
, &dst
->reg
, dst_name
, &is_color
);
2574 /* Sincos writemask must be .x, .y or .xy */
2575 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
)
2576 shader_addline(buffer
, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name0
);
2577 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
)
2578 shader_addline(buffer
, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name0
);
2580 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2581 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2583 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2584 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2586 * The constants we get are:
2588 * +1 +1, -1 -1 +1 +1 -1 -1
2589 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2590 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2592 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2596 * (x/2)^4 = x^4 / 16
2597 * (x/2)^5 = x^5 / 32
2600 * To get the final result:
2601 * sin(x) = 2 * sin(x/2) * cos(x/2)
2602 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2603 * (from sin(x+y) and cos(x+y) rules)
2605 * As per MSDN, dst.z is undefined after the operation, and so is
2606 * dst.x and dst.y if they're masked out by the writemask. Ie
2607 * sincos dst.y, src1, c0, c1
2608 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2609 * vsa.exe also stops with an error if the dest register is the same register as the source
2610 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2611 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2613 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2614 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name2
);
2615 shader_arb_get_register_name(ins
, &dst
->reg
, dst_name
, &is_color
);
2617 shader_addline(buffer
, "MUL %s.x, %s, %s;\n", dst_name
, src_name0
, src_name0
); /* x ^ 2 */
2618 shader_addline(buffer
, "MUL TA.y, %s.x, %s;\n", dst_name
, src_name0
); /* x ^ 3 */
2619 shader_addline(buffer
, "MUL %s.y, TA.y, %s;\n", dst_name
, src_name0
); /* x ^ 4 */
2620 shader_addline(buffer
, "MUL TA.z, %s.y, %s;\n", dst_name
, src_name0
); /* x ^ 5 */
2621 shader_addline(buffer
, "MUL %s.z, TA.z, %s;\n", dst_name
, src_name0
); /* x ^ 6 */
2622 shader_addline(buffer
, "MUL TA.w, %s.z, %s;\n", dst_name
, src_name0
); /* x ^ 7 */
2626 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2627 * properly merge that with MULs in the code above?
2628 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2629 * we can merge the sine and cosine MAD rows to calculate them together.
2631 shader_addline(buffer
, "MUL TA.x, %s, %s.w;\n", src_name0
, src_name2
); /* x^1, +1/(1!*2) */
2632 shader_addline(buffer
, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2
); /* -1/(3!*8) */
2633 shader_addline(buffer
, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1
); /* +1/(5!*32) */
2634 shader_addline(buffer
, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1
); /* -1/(7!*128) */
2637 shader_addline(buffer
, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name
, src_name2
, src_name2
); /* -1/(2!*4), +1.0 */
2638 shader_addline(buffer
, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name
, src_name1
); /* +1/(4!*16) */
2639 shader_addline(buffer
, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name
, src_name1
); /* -1/(6!*64) */
2641 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
) {
2643 shader_addline(buffer
, "MUL TA.z, TA.y, TA.y;\n");
2644 shader_addline(buffer
, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name
);
2646 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
) {
2648 shader_addline(buffer
, "MUL %s.y, TA.x, TA.y;\n", dst_name
);
2649 shader_addline(buffer
, "ADD %s.y, %s.y, %s.y;\n", dst_name
, dst_name
, dst_name
);
2654 static void shader_hw_sgn(const struct wined3d_shader_instruction
*ins
)
2656 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2659 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
2661 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2662 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
);
2664 /* SGN is only valid in vertex shaders */
2665 if(ctx
->target_version
>= NV2
) {
2666 shader_addline(buffer
, "SSG%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
);
2670 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2671 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2673 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
) {
2674 shader_addline(buffer
, "SLT %s, -%s, %s;\n", dst_name
, src_name
, src_name
);
2676 /* src contains TA? Write to the dest first. This won't overwrite our destination.
2677 * Then use TA, and calculate the final result
2679 * Not reading from TA? Store the first result in TA to avoid overwriting the
2680 * destination if src reg = dst reg
2682 if(strstr(src_name
, "TA"))
2684 shader_addline(buffer
, "SLT %s, %s, -%s;\n", dst_name
, src_name
, src_name
);
2685 shader_addline(buffer
, "SLT TA, -%s, %s;\n", src_name
, src_name
);
2686 shader_addline(buffer
, "ADD %s, %s, -TA;\n", dst_name
, dst_name
);
2690 shader_addline(buffer
, "SLT TA, -%s, %s;\n", src_name
, src_name
);
2691 shader_addline(buffer
, "SLT %s, %s, -%s;\n", dst_name
, src_name
, src_name
);
2692 shader_addline(buffer
, "ADD %s, TA, -%s;\n", dst_name
, dst_name
);
2697 static void shader_hw_dsy(const struct wined3d_shader_instruction
*ins
)
2699 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2705 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
);
2706 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2707 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2709 shader_addline(buffer
, "DDY %s, %s;\n", dst
, src
);
2710 shader_addline(buffer
, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins
), dst
, dst_name
);
2713 static DWORD
abs_modifier(DWORD mod
, BOOL
*need_abs
)
2719 case WINED3DSPSM_NONE
: return WINED3DSPSM_ABS
;
2720 case WINED3DSPSM_NEG
: return WINED3DSPSM_ABS
;
2721 case WINED3DSPSM_BIAS
: *need_abs
= TRUE
; return WINED3DSPSM_BIAS
;
2722 case WINED3DSPSM_BIASNEG
: *need_abs
= TRUE
; return WINED3DSPSM_BIASNEG
;
2723 case WINED3DSPSM_SIGN
: *need_abs
= TRUE
; return WINED3DSPSM_SIGN
;
2724 case WINED3DSPSM_SIGNNEG
: *need_abs
= TRUE
; return WINED3DSPSM_SIGNNEG
;
2725 case WINED3DSPSM_COMP
: *need_abs
= TRUE
; return WINED3DSPSM_COMP
;
2726 case WINED3DSPSM_X2
: *need_abs
= TRUE
; return WINED3DSPSM_X2
;
2727 case WINED3DSPSM_X2NEG
: *need_abs
= TRUE
; return WINED3DSPSM_X2NEG
;
2728 case WINED3DSPSM_DZ
: *need_abs
= TRUE
; return WINED3DSPSM_DZ
;
2729 case WINED3DSPSM_DW
: *need_abs
= TRUE
; return WINED3DSPSM_DW
;
2730 case WINED3DSPSM_ABS
: return WINED3DSPSM_ABS
;
2731 case WINED3DSPSM_ABSNEG
: return WINED3DSPSM_ABS
;
2733 FIXME("Unknown modifier %u\n", mod
);
2737 static void shader_hw_log_pow(const struct wined3d_shader_instruction
*ins
)
2739 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2740 char src0
[50], src1
[50], dst
[50];
2741 struct wined3d_shader_src_param src0_copy
= ins
->src
[0];
2742 BOOL need_abs
= FALSE
;
2746 switch(ins
->handler_idx
)
2748 case WINED3DSIH_LOG
: instr
= "LG2"; break;
2749 case WINED3DSIH_LOGP
: instr
= "LOG"; break;
2750 case WINED3DSIH_POW
: instr
= "POW"; arg2
= TRUE
; break;
2752 ERR("Unexpected instruction %d\n", ins
->handler_idx
);
2756 /* LOG, LOGP and POW operate on the absolute value of the input */
2757 src0_copy
.modifiers
= abs_modifier(src0_copy
.modifiers
, &need_abs
);
2759 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
);
2760 shader_arb_get_src_param(ins
, &src0_copy
, 0, src0
);
2761 if(arg2
) shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src1
);
2765 shader_addline(buffer
, "ABS TA, %s;\n", src0
);
2768 shader_addline(buffer
, "%s%s %s, TA, %s;\n", instr
, shader_arb_get_modifier(ins
), dst
, src1
);
2772 shader_addline(buffer
, "%s%s %s, TA;\n", instr
, shader_arb_get_modifier(ins
), dst
);
2777 shader_addline(buffer
, "%s%s %s, %s, %s;\n", instr
, shader_arb_get_modifier(ins
), dst
, src0
, src1
);
2781 shader_addline(buffer
, "%s%s %s, %s;\n", instr
, shader_arb_get_modifier(ins
), dst
, src0
);
2785 static void shader_hw_loop(const struct wined3d_shader_instruction
*ins
)
2787 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2789 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2792 shader_arb_get_src_param(ins
, &ins
->src
[1], 0, src_name
);
2796 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2797 struct list
*e
= list_head(&priv
->control_frames
);
2798 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2800 if(priv
->loop_depth
> 1) shader_addline(buffer
, "PUSHA aL;\n");
2801 /* The constant loader makes sure to load -1 into iX.w */
2802 shader_addline(buffer
, "ARLC aL, %s.xywz;\n", src_name
);
2803 shader_addline(buffer
, "BRA loop_%u_end (LE.x);\n", control_frame
->no
.loop
);
2804 shader_addline(buffer
, "loop_%u_start:\n", control_frame
->no
.loop
);
2808 shader_addline(buffer
, "LOOP %s;\n", src_name
);
2812 static void shader_hw_rep(const struct wined3d_shader_instruction
*ins
)
2814 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2816 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2818 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
);
2820 /* The constant loader makes sure to load -1 into iX.w */
2823 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2824 struct list
*e
= list_head(&priv
->control_frames
);
2825 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2827 if(priv
->loop_depth
> 1) shader_addline(buffer
, "PUSHA aL;\n");
2829 shader_addline(buffer
, "ARLC aL, %s.xywz;\n", src_name
);
2830 shader_addline(buffer
, "BRA loop_%u_end (LE.x);\n", control_frame
->no
.loop
);
2831 shader_addline(buffer
, "loop_%u_start:\n", control_frame
->no
.loop
);
2835 shader_addline(buffer
, "REP %s;\n", src_name
);
2839 static void shader_hw_endloop(const struct wined3d_shader_instruction
*ins
)
2841 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2842 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2846 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2847 struct list
*e
= list_head(&priv
->control_frames
);
2848 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2850 shader_addline(buffer
, "ARAC aL.xy, aL;\n");
2851 shader_addline(buffer
, "BRA loop_%u_start (GT.x);\n", control_frame
->no
.loop
);
2852 shader_addline(buffer
, "loop_%u_end:\n", control_frame
->no
.loop
);
2854 if(priv
->loop_depth
> 1) shader_addline(buffer
, "POPA aL;\n");
2858 shader_addline(buffer
, "ENDLOOP;\n");
2862 static void shader_hw_endrep(const struct wined3d_shader_instruction
*ins
)
2864 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2865 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2869 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2870 struct list
*e
= list_head(&priv
->control_frames
);
2871 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2873 shader_addline(buffer
, "ARAC aL.xy, aL;\n");
2874 shader_addline(buffer
, "BRA loop_%u_start (GT.x);\n", control_frame
->no
.loop
);
2875 shader_addline(buffer
, "loop_%u_end:\n", control_frame
->no
.loop
);
2877 if(priv
->loop_depth
> 1) shader_addline(buffer
, "POPA aL;\n");
2881 shader_addline(buffer
, "ENDREP;\n");
2885 static const struct control_frame
*find_last_loop(const struct shader_arb_ctx_priv
*priv
)
2887 struct control_frame
*control_frame
;
2889 LIST_FOR_EACH_ENTRY(control_frame
, &priv
->control_frames
, struct control_frame
, entry
)
2891 if(control_frame
->type
== LOOP
|| control_frame
->type
== REP
) return control_frame
;
2893 ERR("Could not find loop for break\n");
2897 static void shader_hw_break(const struct wined3d_shader_instruction
*ins
)
2899 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2900 const struct control_frame
*control_frame
= find_last_loop(ins
->ctx
->backend_data
);
2901 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2905 shader_addline(buffer
, "BRA loop_%u_end;\n", control_frame
->no
.loop
);
2909 shader_addline(buffer
, "BRK;\n");
2913 static const char *get_compare(COMPARISON_TYPE flags
)
2917 case COMPARISON_GT
: return "GT";
2918 case COMPARISON_EQ
: return "EQ";
2919 case COMPARISON_GE
: return "GE";
2920 case COMPARISON_LT
: return "LT";
2921 case COMPARISON_NE
: return "NE";
2922 case COMPARISON_LE
: return "LE";
2924 FIXME("Unrecognized comparison value: %u\n", flags
);
2929 static COMPARISON_TYPE
invert_compare(COMPARISON_TYPE flags
)
2933 case COMPARISON_GT
: return COMPARISON_LE
;
2934 case COMPARISON_EQ
: return COMPARISON_NE
;
2935 case COMPARISON_GE
: return COMPARISON_LT
;
2936 case COMPARISON_LT
: return COMPARISON_GE
;
2937 case COMPARISON_NE
: return COMPARISON_EQ
;
2938 case COMPARISON_LE
: return COMPARISON_GT
;
2940 FIXME("Unrecognized comparison value: %u\n", flags
);
2945 static void shader_hw_breakc(const struct wined3d_shader_instruction
*ins
)
2947 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2948 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2949 const struct control_frame
*control_frame
= find_last_loop(ins
->ctx
->backend_data
);
2952 const char *comp
= get_compare(ins
->flags
);
2954 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2955 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2959 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
2960 * away the subtraction result
2962 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2963 shader_addline(buffer
, "BRA loop_%u_end (%s.x);\n", control_frame
->no
.loop
, comp
);
2967 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2968 shader_addline(buffer
, "BRK (%s.x);\n", comp
);
2972 static void shader_hw_ifc(const struct wined3d_shader_instruction
*ins
)
2974 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2975 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2976 struct list
*e
= list_head(&priv
->control_frames
);
2977 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2981 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2983 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2984 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2988 /* Invert the flag. We jump to the else label if the condition is NOT true */
2989 comp
= get_compare(invert_compare(ins
->flags
));
2990 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2991 shader_addline(buffer
, "BRA ifc_%u_else (%s.x);\n", control_frame
->no
.ifc
, comp
);
2995 comp
= get_compare(ins
->flags
);
2996 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2997 shader_addline(buffer
, "IF %s.x;\n", comp
);
3001 static void shader_hw_else(const struct wined3d_shader_instruction
*ins
)
3003 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3004 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3005 struct list
*e
= list_head(&priv
->control_frames
);
3006 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
3007 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
3011 shader_addline(buffer
, "BRA ifc_%u_endif;\n", control_frame
->no
.ifc
);
3012 shader_addline(buffer
, "ifc_%u_else:\n", control_frame
->no
.ifc
);
3013 control_frame
->had_else
= TRUE
;
3017 shader_addline(buffer
, "ELSE;\n");
3021 static void shader_hw_endif(const struct wined3d_shader_instruction
*ins
)
3023 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3024 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3025 struct list
*e
= list_head(&priv
->control_frames
);
3026 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
3027 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
3031 if(control_frame
->had_else
)
3033 shader_addline(buffer
, "ifc_%u_endif:\n", control_frame
->no
.ifc
);
3037 shader_addline(buffer
, "#No else branch. else is endif\n");
3038 shader_addline(buffer
, "ifc_%u_else:\n", control_frame
->no
.ifc
);
3043 shader_addline(buffer
, "ENDIF;\n");
3047 static void shader_hw_texldd(const struct wined3d_shader_instruction
*ins
)
3049 DWORD sampler_idx
= ins
->src
[1].reg
.idx
;
3051 char reg_src
[3][40];
3052 WORD flags
= TEX_DERIV
;
3054 shader_arb_get_dst_param(ins
, &ins
->dst
[0], reg_dest
);
3055 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_src
[0]);
3056 shader_arb_get_src_param(ins
, &ins
->src
[2], 1, reg_src
[1]);
3057 shader_arb_get_src_param(ins
, &ins
->src
[3], 2, reg_src
[2]);
3059 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) flags
|= TEX_PROJ
;
3060 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) flags
|= TEX_BIAS
;
3062 shader_hw_sample(ins
, sampler_idx
, reg_dest
, reg_src
[0], flags
, reg_src
[1], reg_src
[2]);
3065 static void shader_hw_texldl(const struct wined3d_shader_instruction
*ins
)
3067 DWORD sampler_idx
= ins
->src
[1].reg
.idx
;
3070 WORD flags
= TEX_LOD
;
3072 shader_arb_get_dst_param(ins
, &ins
->dst
[0], reg_dest
);
3073 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_coord
);
3075 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) flags
|= TEX_PROJ
;
3076 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) flags
|= TEX_BIAS
;
3078 shader_hw_sample(ins
, sampler_idx
, reg_dest
, reg_coord
, flags
, NULL
, NULL
);
3081 static void shader_hw_label(const struct wined3d_shader_instruction
*ins
)
3083 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3084 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3086 priv
->in_main_func
= FALSE
;
3087 /* Call instructions activate the NV extensions, not labels and rets. If there is an uncalled
3088 * subroutine, don't generate a label that will make GL complain
3090 if(priv
->target_version
== ARB
) return;
3092 shader_addline(buffer
, "l%u:\n", ins
->src
[0].reg
.idx
);
3095 static void vshader_add_footer(IWineD3DVertexShaderImpl
*This
, struct wined3d_shader_buffer
*buffer
,
3096 const struct arb_vs_compile_args
*args
, struct shader_arb_ctx_priv
*priv_ctx
)
3098 const shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
3099 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
3100 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
3103 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
3104 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
3105 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
3106 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
3108 if(args
->super
.fog_src
== VS_FOG_Z
) {
3109 shader_addline(buffer
, "MOV result.fogcoord, TMP_OUT.z;\n");
3110 } else if (!reg_maps
->fog
) {
3111 /* posFixup.x is always 1.0, so we can savely use it */
3112 shader_addline(buffer
, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
3115 /* Write the final position.
3117 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3118 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3119 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3120 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
3122 shader_addline(buffer
, "MUL TA, posFixup, TMP_OUT.w;\n");
3123 shader_addline(buffer
, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
3124 shader_addline(buffer
, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
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 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3170 * and the glsl equivalent
3172 if(need_helper_const((IWineD3DBaseShaderImpl
*) This
, gl_info
)) {
3173 const char *two
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_TWO
);
3174 shader_addline(buffer
, "MAD TMP_OUT.z, TMP_OUT.z, %s, -TMP_OUT.w;\n", two
);
3176 shader_addline(buffer
, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
3177 shader_addline(buffer
, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
3180 shader_addline(buffer
, "MOV result.position, TMP_OUT;\n");
3182 priv_ctx
->footer_written
= TRUE
;
3185 static void shader_hw_ret(const struct wined3d_shader_instruction
*ins
)
3187 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3188 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3189 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*) ins
->ctx
->shader
;
3190 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
3192 if(priv
->target_version
== ARB
) return;
3196 if(priv
->in_main_func
) vshader_add_footer((IWineD3DVertexShaderImpl
*) shader
, buffer
, priv
->cur_vs_args
, priv
);
3199 shader_addline(buffer
, "RET;\n");
3202 static void shader_hw_call(const struct wined3d_shader_instruction
*ins
)
3204 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3205 shader_addline(buffer
, "CAL l%u;\n", ins
->src
[0].reg
.idx
);
3208 /* GL locking is done by the caller */
3209 static GLuint
create_arb_blt_vertex_program(const struct wined3d_gl_info
*gl_info
)
3211 GLuint program_id
= 0;
3214 const char *blt_vprogram
=
3216 "PARAM c[1] = { { 1, 0.5 } };\n"
3217 "MOV result.position, vertex.position;\n"
3218 "MOV result.color, c[0].x;\n"
3219 "MOV result.texcoord[0], vertex.texcoord[0];\n"
3222 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
3223 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, program_id
));
3224 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
3225 strlen(blt_vprogram
), blt_vprogram
));
3226 checkGLcall("glProgramStringARB()");
3228 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
3231 FIXME("Vertex program error at position %d: %s\n\n", pos
,
3232 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3233 shader_arb_dump_program_source(blt_vprogram
);
3239 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
3240 checkGLcall("glGetProgramivARB()");
3241 if (!native
) WARN("Program exceeds native resource limits.\n");
3247 /* GL locking is done by the caller */
3248 static GLuint
create_arb_blt_fragment_program(const struct wined3d_gl_info
*gl_info
,
3249 enum tex_types tex_type
, BOOL masked
)
3251 GLuint program_id
= 0;
3252 const char *fprogram
;
3255 static const char * const blt_fprograms_full
[tex_type_count
] =
3262 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3263 "MOV result.depth.z, R0.x;\n"
3270 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3271 "MOV result.depth.z, R0.x;\n"
3276 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3277 "MOV result.depth.z, R0.x;\n"
3281 static const char * const blt_fprograms_masked
[tex_type_count
] =
3287 "PARAM mask = program.local[0];\n"
3289 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3290 "MUL R0.x, R0.x, R0.y;\n"
3292 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3293 "MOV result.depth.z, R0.x;\n"
3299 "PARAM mask = program.local[0];\n"
3301 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3302 "MUL R0.x, R0.x, R0.y;\n"
3304 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3305 "MOV result.depth.z, R0.x;\n"
3309 "PARAM mask = program.local[0];\n"
3311 "SLT R0.xy, fragment.position, mask.zwzw;\n"
3312 "MUL R0.x, R0.x, R0.y;\n"
3314 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3315 "MOV result.depth.z, R0.x;\n"
3319 fprogram
= masked
? blt_fprograms_masked
[tex_type
] : blt_fprograms_full
[tex_type
];
3322 FIXME("tex_type %#x not supported, falling back to tex_2d\n", tex_type
);
3324 fprogram
= masked
? blt_fprograms_masked
[tex_type
] : blt_fprograms_full
[tex_type
];
3327 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
3328 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, program_id
));
3329 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(fprogram
), fprogram
));
3330 checkGLcall("glProgramStringARB()");
3332 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
3335 FIXME("Fragment program error at position %d: %s\n\n", pos
,
3336 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3337 shader_arb_dump_program_source(fprogram
);
3343 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
3344 checkGLcall("glGetProgramivARB()");
3345 if (!native
) WARN("Program exceeds native resource limits.\n");
3351 static void arbfp_add_sRGB_correction(struct wined3d_shader_buffer
*buffer
, const char *fragcolor
,
3352 const char *tmp1
, const char *tmp2
, const char *tmp3
, const char *tmp4
, BOOL condcode
)
3354 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
3358 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
3359 shader_addline(buffer
, "SUBC %s, %s.x, srgb_consts1.y;\n", tmp1
, fragcolor
);
3360 /* Calculate the > 0.0031308 case */
3361 shader_addline(buffer
, "POW %s.x (GE), %s.x, srgb_consts1.z;\n", fragcolor
, fragcolor
);
3362 shader_addline(buffer
, "POW %s.y (GE), %s.y, srgb_consts1.z;\n", fragcolor
, fragcolor
);
3363 shader_addline(buffer
, "POW %s.z (GE), %s.z, srgb_consts1.z;\n", fragcolor
, fragcolor
);
3364 shader_addline(buffer
, "MUL %s.xyz (GE), %s, srgb_consts1.w;\n", fragcolor
, fragcolor
);
3365 shader_addline(buffer
, "SUB %s.xyz (GE), %s, srgb_consts2.x;\n", fragcolor
, fragcolor
);
3366 /* Calculate the < case */
3367 shader_addline(buffer
, "MUL %s.xyz (LT), srgb_consts1.x, %s;\n", fragcolor
, fragcolor
);
3371 /* Calculate the > 0.0031308 case */
3372 shader_addline(buffer
, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1
, fragcolor
);
3373 shader_addline(buffer
, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1
, fragcolor
);
3374 shader_addline(buffer
, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1
, fragcolor
);
3375 shader_addline(buffer
, "MUL %s, %s, srgb_consts1.w;\n", tmp1
, tmp1
);
3376 shader_addline(buffer
, "SUB %s, %s, srgb_consts2.x;\n", tmp1
, tmp1
);
3377 /* Calculate the < case */
3378 shader_addline(buffer
, "MUL %s, srgb_consts1.x, %s;\n", tmp2
, fragcolor
);
3379 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
3380 shader_addline(buffer
, "SLT %s, srgb_consts1.y, %s;\n", tmp3
, fragcolor
);
3381 shader_addline(buffer
, "SGE %s, srgb_consts1.y, %s;\n", tmp4
, fragcolor
);
3382 /* Store the components > 0.0031308 in the destination */
3383 shader_addline(buffer
, "MUL %s.xyz, %s, %s;\n", fragcolor
, tmp1
, tmp3
);
3384 /* Add the components that are < 0.0031308 */
3385 shader_addline(buffer
, "MAD %s.xyz, %s, %s, %s;\n", fragcolor
, tmp2
, tmp4
, fragcolor
);
3386 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
3387 * result.color writes(.rgb first, then .a), or handle overwriting already written
3388 * components. The assembler uses a temporary register in this case, which is usually
3389 * not allocated from one of our registers that were used earlier.
3392 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
3395 static const DWORD
*find_loop_control_values(IWineD3DBaseShaderImpl
*This
, DWORD idx
)
3397 const local_constant
*constant
;
3399 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsI
, local_constant
, entry
)
3401 if (constant
->idx
== idx
)
3403 return constant
->value
;
3409 static void init_ps_input(const IWineD3DPixelShaderImpl
*This
, const struct arb_ps_compile_args
*args
,
3410 struct shader_arb_ctx_priv
*priv
)
3412 static const char * const texcoords
[8] =
3414 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
3415 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
3418 const struct wined3d_shader_signature_element
*sig
= This
->baseShader
.input_signature
;
3419 const char *semantic_name
;
3422 switch(args
->super
.vp_mode
)
3424 case pretransformed
:
3426 /* The pixelshader has to collect the varyings on its own. In any case properly load
3427 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
3428 * other attribs to 0.0.
3430 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
3431 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
3432 * load the texcoord attrib pointers to match the pixel shader signature
3434 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3436 semantic_name
= sig
[i
].semantic_name
;
3437 semantic_idx
= sig
[i
].semantic_idx
;
3438 if (!semantic_name
) continue;
3440 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3442 if (!semantic_idx
) priv
->ps_input
[i
] = "fragment.color.primary";
3443 else if(semantic_idx
== 1) priv
->ps_input
[i
] = "fragment.color.secondary";
3444 else priv
->ps_input
[i
] = "0.0";
3446 else if(args
->super
.vp_mode
== fixedfunction
)
3448 priv
->ps_input
[i
] = "0.0";
3450 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3452 if(semantic_idx
< 8) priv
->ps_input
[i
] = texcoords
[semantic_idx
];
3453 else priv
->ps_input
[i
] = "0.0";
3455 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3457 if (!semantic_idx
) priv
->ps_input
[i
] = "fragment.fogcoord";
3458 else priv
->ps_input
[i
] = "0.0";
3462 priv
->ps_input
[i
] = "0.0";
3465 TRACE("v%u, semantic %s%u is %s\n", i
, semantic_name
, semantic_idx
, priv
->ps_input
[i
]);
3470 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
3473 for(i
= 0; i
< 8; i
++)
3475 priv
->ps_input
[i
] = texcoords
[i
];
3477 priv
->ps_input
[8] = "fragment.color.primary";
3478 priv
->ps_input
[9] = "fragment.color.secondary";
3483 /* GL locking is done by the caller */
3484 static GLuint
shader_arb_generate_pshader(IWineD3DPixelShaderImpl
*This
, struct wined3d_shader_buffer
*buffer
,
3485 const struct arb_ps_compile_args
*args
, struct arb_ps_compiled_shader
*compiled
)
3487 const shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
3488 CONST DWORD
*function
= This
->baseShader
.function
;
3489 const struct wined3d_gl_info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
3490 const local_constant
*lconst
;
3493 DWORD
*lconst_map
= local_const_mapping((IWineD3DBaseShaderImpl
*) This
), next_local
, cur
;
3494 struct shader_arb_ctx_priv priv_ctx
;
3495 BOOL dcl_td
= FALSE
;
3496 BOOL want_nv_prog
= FALSE
;
3497 struct arb_pshader_private
*shader_priv
= This
->baseShader
.backend_data
;
3502 unsigned int i
, found
= 0;
3504 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
3507 || (This
->color0_mov
&& i
== This
->color0_reg
)
3508 || (reg_maps
->shader_version
.major
< 2 && !i
))
3511 sprintf(srgbtmp
[found
], "R%u", i
);
3513 if (found
== 4) break;
3518 sprintf(srgbtmp
[0], "TA");
3519 sprintf(srgbtmp
[1], "TB");
3520 sprintf(srgbtmp
[2], "TC");
3521 sprintf(srgbtmp
[3], "TD");
3525 sprintf(srgbtmp
[1], "TA");
3526 sprintf(srgbtmp
[2], "TB");
3527 sprintf(srgbtmp
[3], "TC");
3530 sprintf(srgbtmp
[2], "TA");
3531 sprintf(srgbtmp
[3], "TB");
3534 sprintf(srgbtmp
[3], "TA");
3540 /* Create the hw ARB shader */
3541 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
3542 priv_ctx
.cur_ps_args
= args
;
3543 priv_ctx
.compiled_fprog
= compiled
;
3544 priv_ctx
.cur_np2fixup_info
= &compiled
->np2fixup_info
;
3545 init_ps_input(This
, args
, &priv_ctx
);
3546 list_init(&priv_ctx
.control_frames
);
3548 /* Avoid enabling NV_fragment_program* if we do not need it.
3550 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
3551 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
3552 * is faster than what we gain from using higher native instructions. There are some things though
3553 * that cannot be emulated. In that case enable the extensions.
3554 * If the extension is enabled, instruction handlers that support both ways will use it.
3556 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
3557 * So enable the best we can get.
3559 if(reg_maps
->usesdsx
|| reg_maps
->usesdsy
|| reg_maps
->loop_depth
> 0 || reg_maps
->usestexldd
||
3560 reg_maps
->usestexldl
|| reg_maps
->usesfacing
|| reg_maps
->usesifc
|| reg_maps
->usescall
)
3562 want_nv_prog
= TRUE
;
3565 shader_addline(buffer
, "!!ARBfp1.0\n");
3566 if (want_nv_prog
&& gl_info
->supported
[NV_FRAGMENT_PROGRAM2
])
3568 shader_addline(buffer
, "OPTION NV_fragment_program2;\n");
3569 priv_ctx
.target_version
= NV3
;
3571 else if (want_nv_prog
&& gl_info
->supported
[NV_FRAGMENT_PROGRAM_OPTION
])
3573 shader_addline(buffer
, "OPTION NV_fragment_program;\n");
3574 priv_ctx
.target_version
= NV2
;
3578 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
3581 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
3584 priv_ctx
.target_version
= ARB
;
3587 if(This
->baseShader
.reg_maps
.highest_render_target
> 0)
3589 shader_addline(buffer
, "OPTION ARB_draw_buffers;\n");
3592 if (reg_maps
->shader_version
.major
< 3)
3594 switch(args
->super
.fog
) {
3598 shader_addline(buffer
, "OPTION ARB_fog_linear;\n");
3601 shader_addline(buffer
, "OPTION ARB_fog_exp;\n");
3604 shader_addline(buffer
, "OPTION ARB_fog_exp2;\n");
3609 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
3610 * unused temps away(but occupies them for the whole shader if they're used once). Always
3611 * declaring them avoids tricky bookkeeping work
3613 shader_addline(buffer
, "TEMP TA;\n"); /* Used for modifiers */
3614 shader_addline(buffer
, "TEMP TB;\n"); /* Used for modifiers */
3615 shader_addline(buffer
, "TEMP TC;\n"); /* Used for modifiers */
3616 if(dcl_td
) shader_addline(buffer
, "TEMP TD;\n"); /* Used for sRGB writing */
3617 shader_addline(buffer
, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
3618 shader_addline(buffer
, "PARAM coefmul = { 2, 4, 8, 16 };\n");
3619 shader_addline(buffer
, "PARAM ps_helper_const = { 0.0, 1.0, %1.10f, 0.0 };\n", eps
);
3621 if (reg_maps
->shader_version
.major
< 2)
3623 strcpy(fragcolor
, "R0");
3625 if(args
->super
.srgb_correction
) {
3626 if(This
->color0_mov
) {
3627 sprintf(fragcolor
, "R%u", This
->color0_reg
);
3629 shader_addline(buffer
, "TEMP TMP_COLOR;\n");
3630 strcpy(fragcolor
, "TMP_COLOR");
3633 strcpy(fragcolor
, "result.color");
3637 if(args
->super
.srgb_correction
) {
3638 shader_addline(buffer
, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
3639 srgb_mul_low
, srgb_cmp
, srgb_pow
, srgb_mul_high
);
3640 shader_addline(buffer
, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
3641 srgb_sub_high
, 0.0, 0.0, 0.0);
3644 /* Base Declarations */
3645 next_local
= shader_generate_arb_declarations((IWineD3DBaseShader
*)This
,
3646 reg_maps
, buffer
, gl_info
, lconst_map
, NULL
, &priv_ctx
);
3648 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
3650 if (!(map
& 1)) continue;
3652 cur
= compiled
->numbumpenvmatconsts
;
3653 compiled
->bumpenvmatconst
[cur
].const_num
= WINED3D_CONST_NUM_UNUSED
;
3654 compiled
->bumpenvmatconst
[cur
].texunit
= i
;
3655 compiled
->luminanceconst
[cur
].const_num
= WINED3D_CONST_NUM_UNUSED
;
3656 compiled
->luminanceconst
[cur
].texunit
= i
;
3658 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3659 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3660 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3661 * textures due to conditional NP2 restrictions)
3663 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3664 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3665 * their location is shader dependent anyway and they cannot be loaded globally.
3667 compiled
->bumpenvmatconst
[cur
].const_num
= next_local
++;
3668 shader_addline(buffer
, "PARAM bumpenvmat%d = program.local[%d];\n",
3669 i
, compiled
->bumpenvmatconst
[cur
].const_num
);
3670 compiled
->numbumpenvmatconsts
= cur
+ 1;
3672 if (!(reg_maps
->luminanceparams
& (1 << i
))) continue;
3674 compiled
->luminanceconst
[cur
].const_num
= next_local
++;
3675 shader_addline(buffer
, "PARAM luminance%d = program.local[%d];\n",
3676 i
, compiled
->luminanceconst
[cur
].const_num
);
3679 for(i
= 0; i
< MAX_CONST_I
; i
++)
3681 compiled
->int_consts
[i
] = WINED3D_CONST_NUM_UNUSED
;
3682 if (reg_maps
->integer_constants
& (1 << i
) && priv_ctx
.target_version
>= NV2
)
3684 const DWORD
*control_values
= find_loop_control_values((IWineD3DBaseShaderImpl
*) This
, i
);
3688 shader_addline(buffer
, "PARAM I%u = {%u, %u, %u, -1};\n", i
,
3689 control_values
[0], control_values
[1], control_values
[2]);
3693 compiled
->int_consts
[i
] = next_local
;
3694 compiled
->num_int_consts
++;
3695 shader_addline(buffer
, "PARAM I%u = program.local[%u];\n", i
, next_local
++);
3700 if(reg_maps
->vpos
|| reg_maps
->usesdsy
)
3702 compiled
->ycorrection
= next_local
;
3703 shader_addline(buffer
, "PARAM ycorrection = program.local[%u];\n", next_local
++);
3707 shader_addline(buffer
, "TEMP vpos;\n");
3708 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3709 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3710 * ycorrection.z: 1.0
3711 * ycorrection.w: 0.0
3713 shader_addline(buffer
, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3714 shader_addline(buffer
, "FLR vpos.xy, vpos;\n");
3719 compiled
->ycorrection
= WINED3D_CONST_NUM_UNUSED
;
3722 /* Load constants to fixup NP2 texcoords if there are still free constants left:
3723 * Constants (texture dimensions) for the NP2 fixup are loaded as local program parameters. This will consume
3724 * at most 8 (MAX_FRAGMENT_SAMPLERS / 2) parameters, which is highly unlikely, since the application had to
3725 * use 16 NP2 textures at the same time. In case that we run out of constants the fixup is simply not
3726 * applied / activated. This will probably result in wrong rendering of the texture, but will save us from
3727 * shader compilation errors and the subsequent errors when drawing with this shader. */
3728 if (priv_ctx
.cur_ps_args
->super
.np2_fixup
) {
3730 struct arb_ps_np2fixup_info
* const fixup
= priv_ctx
.cur_np2fixup_info
;
3731 const WORD map
= priv_ctx
.cur_ps_args
->super
.np2_fixup
;
3732 const UINT max_lconsts
= gl_info
->limits
.arb_ps_local_constants
;
3734 fixup
->offset
= next_local
;
3735 fixup
->super
.active
= 0;
3738 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
3739 if (!(map
& (1 << i
))) continue;
3741 if (fixup
->offset
+ (cur
>> 1) < max_lconsts
) {
3742 fixup
->super
.active
|= (1 << i
);
3743 fixup
->super
.idx
[i
] = cur
++;
3745 FIXME("No free constant found to load NP2 fixup data into shader. "
3746 "Sampling from this texture will probably look wrong.\n");
3751 fixup
->super
.num_consts
= (cur
+ 1) >> 1;
3752 if (fixup
->super
.num_consts
) {
3753 shader_addline(buffer
, "PARAM np2fixup[%u] = { program.env[%u..%u] };\n",
3754 fixup
->super
.num_consts
, fixup
->offset
, fixup
->super
.num_consts
+ fixup
->offset
- 1);
3757 next_local
+= fixup
->super
.num_consts
;
3760 if (shader_priv
->clipplane_emulation
!= ~0U && args
->clip
)
3762 shader_addline(buffer
, "KIL fragment.texcoord[%u];\n", shader_priv
->clipplane_emulation
);
3765 /* Base Shader Body */
3766 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
3768 if(args
->super
.srgb_correction
) {
3769 arbfp_add_sRGB_correction(buffer
, fragcolor
, srgbtmp
[0], srgbtmp
[1], srgbtmp
[2], srgbtmp
[3],
3770 priv_ctx
.target_version
>= NV2
);
3773 if(strcmp(fragcolor
, "result.color")) {
3774 shader_addline(buffer
, "MOV result.color, %s;\n", fragcolor
);
3776 shader_addline(buffer
, "END\n");
3778 /* TODO: change to resource.glObjectHandle or something like that */
3779 GL_EXTCALL(glGenProgramsARB(1, &retval
));
3781 TRACE("Creating a hw pixel shader, prg=%d\n", retval
);
3782 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, retval
));
3784 TRACE("Created hw pixel shader, prg=%d\n", retval
);
3785 /* Create the program and check for errors */
3786 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
3787 buffer
->bsize
, buffer
->buffer
));
3788 checkGLcall("glProgramStringARB()");
3790 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
3793 FIXME("HW PixelShader Error at position %d: %s\n\n",
3794 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3795 shader_arb_dump_program_source(buffer
->buffer
);
3802 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
3803 checkGLcall("glGetProgramivARB()");
3804 if (!native
) WARN("Program exceeds native resource limits.\n");
3807 /* Load immediate constants */
3809 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
3810 const float *value
= (const float *)lconst
->value
;
3811 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, lconst_map
[lconst
->idx
], value
));
3812 checkGLcall("glProgramLocalParameter4fvARB");
3814 HeapFree(GetProcessHeap(), 0, lconst_map
);
3820 static int compare_sig(const struct wined3d_shader_signature_element
*sig1
, const struct wined3d_shader_signature_element
*sig2
)
3825 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3827 if (!sig1
[i
].semantic_name
|| !sig2
[i
].semantic_name
)
3829 /* Compare pointers, not contents. One string is NULL(element does not exist), the other one is not NULL */
3830 if(sig1
[i
].semantic_name
!= sig2
[i
].semantic_name
) return sig1
[i
].semantic_name
< sig2
[i
].semantic_name
? -1 : 1;
3834 if ((ret
= strcmp(sig1
[i
].semantic_name
, sig2
[i
].semantic_name
))) return ret
;
3835 if(sig1
[i
].semantic_idx
!= sig2
[i
].semantic_idx
) return sig1
[i
].semantic_idx
< sig2
[i
].semantic_idx
? -1 : 1;
3836 if(sig1
[i
].sysval_semantic
!= sig2
[i
].sysval_semantic
) return sig1
[i
].sysval_semantic
< sig2
[i
].sysval_semantic
? -1 : 1;
3837 if(sig1
[i
].component_type
!= sig2
[i
].component_type
) return sig1
[i
].sysval_semantic
< sig2
[i
].component_type
? -1 : 1;
3838 if(sig1
[i
].register_idx
!= sig2
[i
].register_idx
) return sig1
[i
].register_idx
< sig2
[i
].register_idx
? -1 : 1;
3839 if(sig1
[i
].mask
!= sig2
->mask
) return sig1
[i
].mask
< sig2
[i
].mask
? -1 : 1;
3844 static struct wined3d_shader_signature_element
*clone_sig(const struct wined3d_shader_signature_element
*sig
)
3846 struct wined3d_shader_signature_element
*new;
3850 new = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*new) * MAX_REG_INPUT
);
3851 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3853 if (!sig
[i
].semantic_name
) continue;
3856 /* Clone the semantic string */
3857 name
= HeapAlloc(GetProcessHeap(), 0, strlen(sig
[i
].semantic_name
) + 1);
3858 strcpy(name
, sig
[i
].semantic_name
);
3859 new[i
].semantic_name
= name
;
3864 static DWORD
find_input_signature(struct shader_arb_priv
*priv
, const struct wined3d_shader_signature_element
*sig
)
3866 struct wine_rb_entry
*entry
= wine_rb_get(&priv
->signature_tree
, sig
);
3867 struct ps_signature
*found_sig
;
3871 found_sig
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
3872 TRACE("Found existing signature %u\n", found_sig
->idx
);
3873 return found_sig
->idx
;
3875 found_sig
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*sig
));
3876 found_sig
->sig
= clone_sig(sig
);
3877 found_sig
->idx
= priv
->ps_sig_number
++;
3878 TRACE("New signature stored and assigned number %u\n", found_sig
->idx
);
3879 if(wine_rb_put(&priv
->signature_tree
, sig
, &found_sig
->entry
) == -1)
3881 ERR("Failed to insert program entry.\n");
3883 return found_sig
->idx
;
3886 static void init_output_registers(IWineD3DVertexShaderImpl
*shader
, DWORD sig_num
, struct shader_arb_ctx_priv
*priv_ctx
,
3887 struct arb_vs_compiled_shader
*compiled
)
3890 static const char * const texcoords
[8] =
3892 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3893 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
3895 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
3896 IWineD3DBaseShaderClass
*baseshader
= &shader
->baseShader
;
3897 const struct wined3d_shader_signature_element
*sig
;
3898 const char *semantic_name
;
3899 DWORD semantic_idx
, reg_idx
;
3901 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
3902 * and varying 9 to result.color.secondary
3904 static const char * const decl_idx_to_string
[MAX_REG_INPUT
] =
3906 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3907 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]",
3908 "result.color.primary", "result.color.secondary"
3913 TRACE("Pixel shader uses builtin varyings\n");
3914 /* Map builtins to builtins */
3915 for(i
= 0; i
< 8; i
++)
3917 priv_ctx
->texcrd_output
[i
] = texcoords
[i
];
3919 priv_ctx
->color_output
[0] = "result.color.primary";
3920 priv_ctx
->color_output
[1] = "result.color.secondary";
3921 priv_ctx
->fog_output
= "result.fogcoord";
3923 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
3924 for (i
= 0; i
< (sizeof(baseshader
->output_signature
) / sizeof(*baseshader
->output_signature
)); ++i
)
3926 semantic_name
= baseshader
->output_signature
[i
].semantic_name
;
3927 if (!semantic_name
) continue;
3929 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3931 TRACE("o%u is TMP_OUT\n", i
);
3932 if (!baseshader
->output_signature
[i
].semantic_idx
) priv_ctx
->vs_output
[i
] = "TMP_OUT";
3933 else priv_ctx
->vs_output
[i
] = "TA";
3935 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3937 TRACE("o%u is result.pointsize\n", i
);
3938 if (!baseshader
->output_signature
[i
].semantic_idx
) priv_ctx
->vs_output
[i
] = "result.pointsize";
3939 else priv_ctx
->vs_output
[i
] = "TA";
3941 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3943 TRACE("o%u is result.color.?, idx %u\n", i
, baseshader
->output_signature
[i
].semantic_idx
);
3944 if (!baseshader
->output_signature
[i
].semantic_idx
)
3945 priv_ctx
->vs_output
[i
] = "result.color.primary";
3946 else if (baseshader
->output_signature
[i
].semantic_idx
== 1)
3947 priv_ctx
->vs_output
[i
] = "result.color.secondary";
3948 else priv_ctx
->vs_output
[i
] = "TA";
3950 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3952 TRACE("o%u is %s\n", i
, texcoords
[baseshader
->output_signature
[i
].semantic_idx
]);
3953 if (baseshader
->output_signature
[i
].semantic_idx
>= 8) priv_ctx
->vs_output
[i
] = "TA";
3954 else priv_ctx
->vs_output
[i
] = texcoords
[baseshader
->output_signature
[i
].semantic_idx
];
3956 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3958 TRACE("o%u is result.fogcoord\n", i
);
3959 if (baseshader
->output_signature
[i
].semantic_idx
> 0) priv_ctx
->vs_output
[i
] = "TA";
3960 else priv_ctx
->vs_output
[i
] = "result.fogcoord";
3964 priv_ctx
->vs_output
[i
] = "TA";
3970 /* Instead of searching for the signature in the signature list, read the one from the current pixel shader.
3971 * Its maybe not the shader where the signature came from, but it is the same signature and faster to find
3973 sig
= device
->stateBlock
->state
.pixel_shader
->baseShader
.input_signature
;
3974 TRACE("Pixel shader uses declared varyings\n");
3976 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
3977 for(i
= 0; i
< 8; i
++)
3979 priv_ctx
->texcrd_output
[i
] = "TA";
3981 priv_ctx
->color_output
[0] = "TA";
3982 priv_ctx
->color_output
[1] = "TA";
3983 priv_ctx
->fog_output
= "TA";
3985 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3987 semantic_name
= sig
[i
].semantic_name
;
3988 semantic_idx
= sig
[i
].semantic_idx
;
3989 reg_idx
= sig
[i
].register_idx
;
3990 if (!semantic_name
) continue;
3992 /* If a declared input register is not written by builtin arguments, don't write to it.
3993 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
3995 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
3996 * to TMP_OUT in any case
3998 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
4000 if(semantic_idx
< 8) priv_ctx
->texcrd_output
[semantic_idx
] = decl_idx_to_string
[reg_idx
];
4002 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
4004 if(semantic_idx
< 2) priv_ctx
->color_output
[semantic_idx
] = decl_idx_to_string
[reg_idx
];
4006 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
4008 if (!semantic_idx
) priv_ctx
->fog_output
= decl_idx_to_string
[reg_idx
];
4015 if (!strcmp(decl_idx_to_string
[reg_idx
], "result.color.primary")
4016 || !strcmp(decl_idx_to_string
[reg_idx
], "result.color.secondary"))
4018 compiled
->need_color_unclamp
= TRUE
;
4022 /* Map declared to declared */
4023 for (i
= 0; i
< (sizeof(baseshader
->output_signature
) / sizeof(*baseshader
->output_signature
)); ++i
)
4025 /* Write unread output to TA to throw them away */
4026 priv_ctx
->vs_output
[i
] = "TA";
4027 semantic_name
= baseshader
->output_signature
[i
].semantic_name
;
4028 if (!semantic_name
) continue;
4030 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
)
4031 && !baseshader
->output_signature
[i
].semantic_idx
)
4033 priv_ctx
->vs_output
[i
] = "TMP_OUT";
4036 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
)
4037 && !baseshader
->output_signature
[i
].semantic_idx
)
4039 priv_ctx
->vs_output
[i
] = "result.pointsize";
4043 for(j
= 0; j
< MAX_REG_INPUT
; j
++)
4045 if (!sig
[j
].semantic_name
) continue;
4047 if (!strcmp(sig
[j
].semantic_name
, semantic_name
)
4048 && sig
[j
].semantic_idx
== baseshader
->output_signature
[i
].semantic_idx
)
4050 priv_ctx
->vs_output
[i
] = decl_idx_to_string
[sig
[j
].register_idx
];
4052 if (!strcmp(priv_ctx
->vs_output
[i
], "result.color.primary")
4053 || !strcmp(priv_ctx
->vs_output
[i
], "result.color.secondary"))
4055 compiled
->need_color_unclamp
= TRUE
;
4062 /* GL locking is done by the caller */
4063 static GLuint
shader_arb_generate_vshader(IWineD3DVertexShaderImpl
*This
, struct wined3d_shader_buffer
*buffer
,
4064 const struct arb_vs_compile_args
*args
, struct arb_vs_compiled_shader
*compiled
)
4066 const shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
4067 CONST DWORD
*function
= This
->baseShader
.function
;
4068 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
4069 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4070 const local_constant
*lconst
;
4072 DWORD next_local
, *lconst_map
= local_const_mapping((IWineD3DBaseShaderImpl
*) This
);
4073 struct shader_arb_ctx_priv priv_ctx
;
4077 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4078 priv_ctx
.cur_vs_args
= args
;
4079 list_init(&priv_ctx
.control_frames
);
4080 init_output_registers(This
, args
->ps_signature
, &priv_ctx
, compiled
);
4082 /* Create the hw ARB shader */
4083 shader_addline(buffer
, "!!ARBvp1.0\n");
4085 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
4086 * mesurable performance penalty, and we can always make use of it for clipplanes.
4088 if (gl_info
->supported
[NV_VERTEX_PROGRAM3
])
4090 shader_addline(buffer
, "OPTION NV_vertex_program3;\n");
4091 priv_ctx
.target_version
= NV3
;
4092 shader_addline(buffer
, "ADDRESS aL;\n");
4094 else if (gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
])
4096 shader_addline(buffer
, "OPTION NV_vertex_program2;\n");
4097 priv_ctx
.target_version
= NV2
;
4098 shader_addline(buffer
, "ADDRESS aL;\n");
4100 priv_ctx
.target_version
= ARB
;
4103 shader_addline(buffer
, "TEMP TMP_OUT;\n");
4104 if(need_helper_const((IWineD3DBaseShaderImpl
*) This
, gl_info
)) {
4105 shader_addline(buffer
, "PARAM helper_const = { 0.0, 1.0, 2.0, %1.10f};\n", eps
);
4107 if(need_rel_addr_const((IWineD3DBaseShaderImpl
*) This
, gl_info
)) {
4108 shader_addline(buffer
, "PARAM rel_addr_const = { 0.5, %d.0, 0.0, 0.0 };\n", This
->rel_offset
);
4109 shader_addline(buffer
, "TEMP A0_SHADOW;\n");
4112 shader_addline(buffer
, "TEMP TA;\n");
4114 /* Base Declarations */
4115 next_local
= shader_generate_arb_declarations((IWineD3DBaseShader
*)This
,
4116 reg_maps
, buffer
, gl_info
, lconst_map
, &priv_ctx
.vs_clipplanes
, &priv_ctx
);
4118 for(i
= 0; i
< MAX_CONST_I
; i
++)
4120 compiled
->int_consts
[i
] = WINED3D_CONST_NUM_UNUSED
;
4121 if(reg_maps
->integer_constants
& (1 << i
) && priv_ctx
.target_version
>= NV2
)
4123 const DWORD
*control_values
= find_loop_control_values((IWineD3DBaseShaderImpl
*) This
, i
);
4127 shader_addline(buffer
, "PARAM I%u = {%u, %u, %u, -1};\n", i
,
4128 control_values
[0], control_values
[1], control_values
[2]);
4132 compiled
->int_consts
[i
] = next_local
;
4133 compiled
->num_int_consts
++;
4134 shader_addline(buffer
, "PARAM I%u = program.local[%u];\n", i
, next_local
++);
4139 /* We need a constant to fixup the final position */
4140 shader_addline(buffer
, "PARAM posFixup = program.local[%u];\n", next_local
);
4141 compiled
->pos_fixup
= next_local
++;
4143 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
4144 * for output parameters. D3D in theory does not do that either, but some applications depend on a
4145 * proper initialization of the secondary color, and programs using the fixed function pipeline without
4146 * a replacement shader depend on the texcoord.w being set properly.
4148 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
4149 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
4150 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
4151 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
4152 * this can eat a number of instructions, so skip it unless this cap is set as well
4154 if (!gl_info
->supported
[NV_VERTEX_PROGRAM
])
4156 const char *color_init
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_0001
);
4157 shader_addline(buffer
, "MOV result.color.secondary, %s;\n", color_init
);
4159 if (gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
&& !device
->frag_pipe
->ffp_proj_control
)
4162 const char *one
= arb_get_helper_value(WINED3D_SHADER_TYPE_VERTEX
, ARB_ONE
);
4163 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++)
4165 if (This
->baseShader
.reg_maps
.texcoord_mask
[i
]
4166 && This
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
4167 shader_addline(buffer
, "MOV result.texcoord[%u].w, %s\n", i
, one
);
4172 /* The shader starts with the main function */
4173 priv_ctx
.in_main_func
= TRUE
;
4174 /* Base Shader Body */
4175 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
4177 if(!priv_ctx
.footer_written
) vshader_add_footer(This
, buffer
, args
, &priv_ctx
);
4179 shader_addline(buffer
, "END\n");
4181 /* TODO: change to resource.glObjectHandle or something like that */
4182 GL_EXTCALL(glGenProgramsARB(1, &ret
));
4184 TRACE("Creating a hw vertex shader, prg=%d\n", ret
);
4185 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, ret
));
4187 TRACE("Created hw vertex shader, prg=%d\n", ret
);
4188 /* Create the program and check for errors */
4189 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
4190 buffer
->bsize
, buffer
->buffer
));
4191 checkGLcall("glProgramStringARB()");
4193 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
4196 FIXME("HW VertexShader Error at position %d: %s\n\n",
4197 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
4198 shader_arb_dump_program_source(buffer
->buffer
);
4205 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
4206 checkGLcall("glGetProgramivARB()");
4207 if (!native
) WARN("Program exceeds native resource limits.\n");
4209 /* Load immediate constants */
4211 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
4212 const float *value
= (const float *)lconst
->value
;
4213 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, lconst_map
[lconst
->idx
], value
));
4217 HeapFree(GetProcessHeap(), 0, lconst_map
);
4222 /* GL locking is done by the caller */
4223 static struct arb_ps_compiled_shader
*find_arb_pshader(IWineD3DPixelShaderImpl
*shader
, const struct arb_ps_compile_args
*args
)
4225 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
4228 struct arb_ps_compiled_shader
*new_array
;
4229 struct wined3d_shader_buffer buffer
;
4230 struct arb_pshader_private
*shader_data
;
4233 if (!shader
->baseShader
.backend_data
)
4235 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4236 struct shader_arb_priv
*priv
= device
->shader_priv
;
4238 shader
->baseShader
.backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4239 shader_data
= shader
->baseShader
.backend_data
;
4240 shader_data
->clamp_consts
= shader
->baseShader
.reg_maps
.shader_version
.major
== 1;
4242 if(shader
->baseShader
.reg_maps
.shader_version
.major
< 3) shader_data
->input_signature_idx
= ~0;
4243 else shader_data
->input_signature_idx
= find_input_signature(priv
, shader
->baseShader
.input_signature
);
4245 shader_data
->has_signature_idx
= TRUE
;
4246 TRACE("Shader got assigned input signature index %u\n", shader_data
->input_signature_idx
);
4248 if (!device
->vs_clipping
)
4249 shader_data
->clipplane_emulation
= shader_find_free_input_register(&shader
->baseShader
.reg_maps
,
4250 gl_info
->limits
.texture_stages
- 1);
4252 shader_data
->clipplane_emulation
= ~0U;
4254 shader_data
= shader
->baseShader
.backend_data
;
4256 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4257 * so a linear search is more performant than a hashmap or a binary search
4258 * (cache coherency etc)
4260 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4262 if (!memcmp(&shader_data
->gl_shaders
[i
].args
, args
, sizeof(*args
)))
4263 return &shader_data
->gl_shaders
[i
];
4266 TRACE("No matching GL shader found, compiling a new shader\n");
4267 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4268 if (shader_data
->num_gl_shaders
)
4270 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4271 new_array
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, shader_data
->gl_shaders
,
4272 new_size
* sizeof(*shader_data
->gl_shaders
));
4274 new_array
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
->gl_shaders
));
4279 ERR("Out of memory\n");
4282 shader_data
->gl_shaders
= new_array
;
4283 shader_data
->shader_array_size
= new_size
;
4286 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4288 pixelshader_update_samplers(&shader
->baseShader
.reg_maps
,
4289 (IWineD3DBaseTexture
**)device
->stateBlock
->state
.textures
);
4291 if (!shader_buffer_init(&buffer
))
4293 ERR("Failed to initialize shader buffer.\n");
4297 ret
= shader_arb_generate_pshader(shader
, &buffer
, args
,
4298 &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
]);
4299 shader_buffer_free(&buffer
);
4300 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].prgId
= ret
;
4302 return &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++];
4305 static inline BOOL
vs_args_equal(const struct arb_vs_compile_args
*stored
, const struct arb_vs_compile_args
*new,
4306 const DWORD use_map
, BOOL skip_int
) {
4307 if((stored
->super
.swizzle_map
& use_map
) != new->super
.swizzle_map
) return FALSE
;
4308 if(stored
->super
.clip_enabled
!= new->super
.clip_enabled
) return FALSE
;
4309 if(stored
->super
.fog_src
!= new->super
.fog_src
) return FALSE
;
4310 if(stored
->clip
.boolclip_compare
!= new->clip
.boolclip_compare
) return FALSE
;
4311 if(stored
->ps_signature
!= new->ps_signature
) return FALSE
;
4312 if(stored
->vertex
.samplers_compare
!= new->vertex
.samplers_compare
) return FALSE
;
4313 if(skip_int
) return TRUE
;
4315 return !memcmp(stored
->loop_ctrl
, new->loop_ctrl
, sizeof(stored
->loop_ctrl
));
4318 static struct arb_vs_compiled_shader
*find_arb_vshader(IWineD3DVertexShaderImpl
*shader
, const struct arb_vs_compile_args
*args
)
4322 struct arb_vs_compiled_shader
*new_array
;
4323 DWORD use_map
= ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.use_map
;
4324 struct wined3d_shader_buffer buffer
;
4325 struct arb_vshader_private
*shader_data
;
4327 const struct wined3d_gl_info
*gl_info
= &((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->adapter
->gl_info
;
4329 if (!shader
->baseShader
.backend_data
)
4331 shader
->baseShader
.backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4333 shader_data
= shader
->baseShader
.backend_data
;
4335 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4336 * so a linear search is more performant than a hashmap or a binary search
4337 * (cache coherency etc)
4339 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4340 if (vs_args_equal(&shader_data
->gl_shaders
[i
].args
, args
,
4341 use_map
, gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
]))
4343 return &shader_data
->gl_shaders
[i
];
4347 TRACE("No matching GL shader found, compiling a new shader\n");
4349 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4350 if (shader_data
->num_gl_shaders
)
4352 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4353 new_array
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, shader_data
->gl_shaders
,
4354 new_size
* sizeof(*shader_data
->gl_shaders
));
4356 new_array
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
->gl_shaders
));
4361 ERR("Out of memory\n");
4364 shader_data
->gl_shaders
= new_array
;
4365 shader_data
->shader_array_size
= new_size
;
4368 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4370 if (!shader_buffer_init(&buffer
))
4372 ERR("Failed to initialize shader buffer.\n");
4376 ret
= shader_arb_generate_vshader(shader
, &buffer
, args
,
4377 &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
]);
4378 shader_buffer_free(&buffer
);
4379 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].prgId
= ret
;
4381 return &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++];
4384 static void find_arb_ps_compile_args(const struct wined3d_state
*state
,
4385 IWineD3DPixelShaderImpl
*shader
, struct arb_ps_compile_args
*args
)
4389 const struct wined3d_gl_info
*gl_info
= &((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->adapter
->gl_info
;
4391 find_ps_compile_args(state
, shader
, &args
->super
);
4393 /* This forces all local boolean constants to 1 to make them stateblock independent */
4394 args
->bools
= shader
->baseShader
.reg_maps
.local_bool_consts
;
4396 for(i
= 0; i
< MAX_CONST_B
; i
++)
4398 if (state
->ps_consts_b
[i
])
4399 args
->bools
|= ( 1 << i
);
4402 /* Only enable the clip plane emulation KIL if at least one clipplane is enabled. The KIL instruction
4403 * is quite expensive because it forces the driver to disable early Z discards. It is cheaper to
4404 * duplicate the shader than have a no-op KIL instruction in every shader
4406 if ((!((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->vs_clipping
) && use_vs(state
)
4407 && state
->render_states
[WINED3DRS_CLIPPING
]
4408 && state
->render_states
[WINED3DRS_CLIPPLANEENABLE
])
4413 /* Skip if unused or local, or supported natively */
4414 int_skip
= ~shader
->baseShader
.reg_maps
.integer_constants
| shader
->baseShader
.reg_maps
.local_int_consts
;
4415 if (int_skip
== 0xffff || gl_info
->supported
[NV_FRAGMENT_PROGRAM_OPTION
])
4417 memset(&args
->loop_ctrl
, 0, sizeof(args
->loop_ctrl
));
4421 for(i
= 0; i
< MAX_CONST_I
; i
++)
4423 if(int_skip
& (1 << i
))
4425 args
->loop_ctrl
[i
][0] = 0;
4426 args
->loop_ctrl
[i
][1] = 0;
4427 args
->loop_ctrl
[i
][2] = 0;
4431 args
->loop_ctrl
[i
][0] = state
->ps_consts_i
[i
* 4];
4432 args
->loop_ctrl
[i
][1] = state
->ps_consts_i
[i
* 4 + 1];
4433 args
->loop_ctrl
[i
][2] = state
->ps_consts_i
[i
* 4 + 2];
4438 static void find_arb_vs_compile_args(const struct wined3d_state
*state
,
4439 IWineD3DVertexShaderImpl
*shader
, struct arb_vs_compile_args
*args
)
4443 IWineD3DDeviceImpl
*dev
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
4444 const struct wined3d_gl_info
*gl_info
= &dev
->adapter
->gl_info
;
4446 find_vs_compile_args(state
, shader
, &args
->super
);
4448 args
->clip
.boolclip_compare
= 0;
4451 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
4452 struct arb_pshader_private
*shader_priv
= ps
->baseShader
.backend_data
;
4453 args
->ps_signature
= shader_priv
->input_signature_idx
;
4455 args
->clip
.boolclip
.clip_texcoord
= shader_priv
->clipplane_emulation
+ 1;
4459 args
->ps_signature
= ~0;
4460 if(!dev
->vs_clipping
)
4462 args
->clip
.boolclip
.clip_texcoord
= ffp_clip_emul(state
) ? gl_info
->limits
.texture_stages
: 0;
4464 /* Otherwise: Setting boolclip_compare set clip_texcoord to 0 */
4467 if (args
->clip
.boolclip
.clip_texcoord
)
4469 if (state
->render_states
[WINED3DRS_CLIPPING
])
4470 args
->clip
.boolclip
.clipplane_mask
= (unsigned char)state
->render_states
[WINED3DRS_CLIPPLANEENABLE
];
4471 /* clipplane_mask was set to 0 by setting boolclip_compare to 0 */
4474 /* This forces all local boolean constants to 1 to make them stateblock independent */
4475 args
->clip
.boolclip
.bools
= shader
->baseShader
.reg_maps
.local_bool_consts
;
4476 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
4477 for(i
= 0; i
< MAX_CONST_B
; i
++)
4479 if (state
->vs_consts_b
[i
])
4480 args
->clip
.boolclip
.bools
|= ( 1 << i
);
4483 args
->vertex
.samplers
[0] = dev
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 0];
4484 args
->vertex
.samplers
[1] = dev
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 1];
4485 args
->vertex
.samplers
[2] = dev
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 2];
4486 args
->vertex
.samplers
[3] = 0;
4488 /* Skip if unused or local */
4489 int_skip
= ~shader
->baseShader
.reg_maps
.integer_constants
| shader
->baseShader
.reg_maps
.local_int_consts
;
4490 /* This is about flow control, not clipping. */
4491 if (int_skip
== 0xffff || gl_info
->supported
[NV_VERTEX_PROGRAM2_OPTION
])
4493 memset(&args
->loop_ctrl
, 0, sizeof(args
->loop_ctrl
));
4497 for(i
= 0; i
< MAX_CONST_I
; i
++)
4499 if(int_skip
& (1 << i
))
4501 args
->loop_ctrl
[i
][0] = 0;
4502 args
->loop_ctrl
[i
][1] = 0;
4503 args
->loop_ctrl
[i
][2] = 0;
4507 args
->loop_ctrl
[i
][0] = state
->vs_consts_i
[i
* 4];
4508 args
->loop_ctrl
[i
][1] = state
->vs_consts_i
[i
* 4 + 1];
4509 args
->loop_ctrl
[i
][2] = state
->vs_consts_i
[i
* 4 + 2];
4514 /* GL locking is done by the caller */
4515 static void shader_arb_select(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
)
4517 IWineD3DDeviceImpl
*This
= context
->swapchain
->device
;
4518 struct shader_arb_priv
*priv
= This
->shader_priv
;
4519 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4520 const struct wined3d_state
*state
= &This
->stateBlock
->state
;
4523 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
4526 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
4527 struct arb_ps_compile_args compile_args
;
4528 struct arb_ps_compiled_shader
*compiled
;
4530 TRACE("Using pixel shader %p.\n", ps
);
4531 find_arb_ps_compile_args(state
, ps
, &compile_args
);
4532 compiled
= find_arb_pshader(ps
, &compile_args
);
4533 priv
->current_fprogram_id
= compiled
->prgId
;
4534 priv
->compiled_fprog
= compiled
;
4536 /* Bind the fragment program */
4537 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
4538 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
4540 if(!priv
->use_arbfp_fixed_func
) {
4541 /* Enable OpenGL fragment programs */
4542 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
4543 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
4545 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
4547 /* Pixel Shader 1.x constants are clamped to [-1;1], Pixel Shader 2.0 constants are not. If switching between
4548 * a 1.x and newer shader, reload the first 8 constants
4550 if(priv
->last_ps_const_clamped
!= ((struct arb_pshader_private
*)ps
->baseShader
.backend_data
)->clamp_consts
)
4552 priv
->last_ps_const_clamped
= ((struct arb_pshader_private
*)ps
->baseShader
.backend_data
)->clamp_consts
;
4553 This
->highest_dirty_ps_const
= max(This
->highest_dirty_ps_const
, 8);
4554 for(i
= 0; i
< 8; i
++)
4556 context
->pshader_const_dirty
[i
] = 1;
4558 /* Also takes care of loading local constants */
4559 shader_arb_load_constants(context
, TRUE
, FALSE
);
4563 float rt_height
= This
->render_targets
[0]->currentDesc
.Height
;
4564 shader_arb_ps_local_constants(compiled
, context
, state
, rt_height
);
4567 /* Force constant reloading for the NP2 fixup (see comment in shader_glsl_select for more info) */
4568 if (compiled
->np2fixup_info
.super
.active
)
4569 shader_arb_load_np2fixup_constants(priv
, gl_info
, state
);
4571 else if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
] && !priv
->use_arbfp_fixed_func
)
4573 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
4574 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
4575 * replacement shader
4577 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
4578 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4579 priv
->current_fprogram_id
= 0;
4584 IWineD3DVertexShaderImpl
*vs
= state
->vertex_shader
;
4585 struct arb_vs_compile_args compile_args
;
4586 struct arb_vs_compiled_shader
*compiled
;
4588 TRACE("Using vertex shader %p\n", vs
);
4589 find_arb_vs_compile_args(state
, vs
, &compile_args
);
4590 compiled
= find_arb_vshader(vs
, &compile_args
);
4591 priv
->current_vprogram_id
= compiled
->prgId
;
4592 priv
->compiled_vprog
= compiled
;
4594 /* Bind the vertex program */
4595 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
4596 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
4598 /* Enable OpenGL vertex programs */
4599 glEnable(GL_VERTEX_PROGRAM_ARB
);
4600 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
4601 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
4602 shader_arb_vs_local_constants(compiled
, context
, state
);
4604 if(priv
->last_vs_color_unclamp
!= compiled
->need_color_unclamp
) {
4605 priv
->last_vs_color_unclamp
= compiled
->need_color_unclamp
;
4607 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
4609 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, !compiled
->need_color_unclamp
));
4610 checkGLcall("glClampColorARB");
4612 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4616 else if (gl_info
->supported
[ARB_VERTEX_PROGRAM
])
4618 priv
->current_vprogram_id
= 0;
4619 glDisable(GL_VERTEX_PROGRAM_ARB
);
4620 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4624 /* GL locking is done by the caller */
4625 static void shader_arb_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
, const SIZE
*ds_mask_size
)
4627 const float mask
[] = {0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
};
4628 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4629 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
4630 struct shader_arb_priv
*priv
= This
->shader_priv
;
4631 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4632 GLuint
*blt_fprogram
;
4634 if (!priv
->depth_blt_vprogram_id
) priv
->depth_blt_vprogram_id
= create_arb_blt_vertex_program(gl_info
);
4635 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->depth_blt_vprogram_id
));
4636 glEnable(GL_VERTEX_PROGRAM_ARB
);
4638 blt_fprogram
= masked
? &priv
->depth_blt_fprogram_id_masked
[tex_type
] : &priv
->depth_blt_fprogram_id_full
[tex_type
];
4639 if (!*blt_fprogram
) *blt_fprogram
= create_arb_blt_fragment_program(gl_info
, tex_type
, masked
);
4640 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, *blt_fprogram
));
4641 if (masked
) GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0, mask
));
4642 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
4645 /* GL locking is done by the caller */
4646 static void shader_arb_deselect_depth_blt(IWineD3DDevice
*iface
) {
4647 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4648 struct shader_arb_priv
*priv
= This
->shader_priv
;
4649 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4651 if (priv
->current_vprogram_id
) {
4652 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
4653 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
4655 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
4657 glDisable(GL_VERTEX_PROGRAM_ARB
);
4658 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4661 if (priv
->current_fprogram_id
) {
4662 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
4663 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
4665 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
4666 } else if(!priv
->use_arbfp_fixed_func
) {
4667 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
4668 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4672 static void shader_arb_destroy(IWineD3DBaseShader
*iface
) {
4673 IWineD3DBaseShaderImpl
*baseShader
= (IWineD3DBaseShaderImpl
*) iface
;
4674 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)baseShader
->baseShader
.device
;
4675 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4677 if (shader_is_pshader_version(baseShader
->baseShader
.reg_maps
.shader_version
.type
))
4679 struct arb_pshader_private
*shader_data
= baseShader
->baseShader
.backend_data
;
4682 if(!shader_data
) return; /* This can happen if a shader was never compiled */
4684 if (shader_data
->num_gl_shaders
)
4686 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4689 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4691 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data
->gl_shaders
[i
].prgId
));
4692 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4696 context_release(context
);
4699 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4700 HeapFree(GetProcessHeap(), 0, shader_data
);
4701 baseShader
->baseShader
.backend_data
= NULL
;
4705 struct arb_vshader_private
*shader_data
= baseShader
->baseShader
.backend_data
;
4708 if(!shader_data
) return; /* This can happen if a shader was never compiled */
4710 if (shader_data
->num_gl_shaders
)
4712 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4715 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4717 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data
->gl_shaders
[i
].prgId
));
4718 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4722 context_release(context
);
4725 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4726 HeapFree(GetProcessHeap(), 0, shader_data
);
4727 baseShader
->baseShader
.backend_data
= NULL
;
4731 static int sig_tree_compare(const void *key
, const struct wine_rb_entry
*entry
)
4733 struct ps_signature
*e
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
4734 return compare_sig(key
, e
->sig
);
4737 static const struct wine_rb_functions sig_tree_functions
=
4745 static HRESULT
shader_arb_alloc(IWineD3DDevice
*iface
) {
4746 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4747 struct shader_arb_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*priv
));
4748 if(wine_rb_init(&priv
->signature_tree
, &sig_tree_functions
) == -1)
4750 ERR("RB tree init failed\n");
4751 HeapFree(GetProcessHeap(), 0, priv
);
4752 return E_OUTOFMEMORY
;
4754 This
->shader_priv
= priv
;
4758 static void release_signature(struct wine_rb_entry
*entry
, void *context
)
4760 struct ps_signature
*sig
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
4762 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
4764 HeapFree(GetProcessHeap(), 0, (char *) sig
->sig
[i
].semantic_name
);
4766 HeapFree(GetProcessHeap(), 0, sig
->sig
);
4767 HeapFree(GetProcessHeap(), 0, sig
);
4770 /* Context activation is done by the caller. */
4771 static void shader_arb_free(IWineD3DDevice
*iface
) {
4772 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4773 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4774 struct shader_arb_priv
*priv
= This
->shader_priv
;
4778 if(priv
->depth_blt_vprogram_id
) {
4779 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_vprogram_id
));
4781 for (i
= 0; i
< tex_type_count
; ++i
)
4783 if (priv
->depth_blt_fprogram_id_full
[i
])
4785 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id_full
[i
]));
4787 if (priv
->depth_blt_fprogram_id_masked
[i
])
4789 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id_masked
[i
]));
4794 wine_rb_destroy(&priv
->signature_tree
, release_signature
, NULL
);
4795 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
4798 static BOOL
shader_arb_dirty_const(IWineD3DDevice
*iface
) {
4802 static void shader_arb_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*pCaps
)
4804 if (gl_info
->supported
[ARB_VERTEX_PROGRAM
])
4808 /* 96 is the minimum allowed value of MAX_PROGRAM_ENV_PARAMETERS_ARB
4809 * for vertex programs. If the native limit is less than that it's
4810 * not very useful, and e.g. Mesa swrast returns 0, probably to
4811 * indicate it's a software implementation. */
4812 if (gl_info
->limits
.arb_vs_native_constants
< 96)
4813 vs_consts
= gl_info
->limits
.arb_vs_float_constants
;
4815 vs_consts
= min(gl_info
->limits
.arb_vs_float_constants
, gl_info
->limits
.arb_vs_native_constants
);
4817 if (gl_info
->supported
[NV_VERTEX_PROGRAM3
])
4819 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
4820 TRACE_(d3d_caps
)("Hardware vertex shader version 3.0 enabled (NV_VERTEX_PROGRAM3)\n");
4822 else if (vs_consts
>= 256)
4824 /* Shader Model 2.0 requires at least 256 vertex shader constants */
4825 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
4826 TRACE_(d3d_caps
)("Hardware vertex shader version 2.0 enabled (ARB_PROGRAM)\n");
4830 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(1,1);
4831 TRACE_(d3d_caps
)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
4833 pCaps
->MaxVertexShaderConst
= vs_consts
;
4837 pCaps
->VertexShaderVersion
= 0;
4838 pCaps
->MaxVertexShaderConst
= 0;
4841 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
4845 /* Similar as above for vertex programs, but the minimum for fragment
4846 * programs is 24. */
4847 if (gl_info
->limits
.arb_ps_native_constants
< 24)
4848 ps_consts
= gl_info
->limits
.arb_ps_float_constants
;
4850 ps_consts
= min(gl_info
->limits
.arb_ps_float_constants
, gl_info
->limits
.arb_ps_native_constants
);
4852 if (gl_info
->supported
[NV_FRAGMENT_PROGRAM2
])
4854 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
4855 TRACE_(d3d_caps
)("Hardware pixel shader version 3.0 enabled (NV_FRAGMENT_PROGRAM2)\n");
4857 else if (ps_consts
>= 32)
4859 /* Shader Model 2.0 requires at least 32 pixel shader constants */
4860 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
4861 TRACE_(d3d_caps
)("Hardware pixel shader version 2.0 enabled (ARB_PROGRAM)\n");
4865 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(1,4);
4866 TRACE_(d3d_caps
)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
4868 pCaps
->PixelShader1xMaxValue
= 8.0f
;
4869 pCaps
->MaxPixelShaderConst
= ps_consts
;
4873 pCaps
->PixelShaderVersion
= 0;
4874 pCaps
->PixelShader1xMaxValue
= 0.0f
;
4875 pCaps
->MaxPixelShaderConst
= 0;
4878 pCaps
->VSClipping
= use_nv_clip(gl_info
);
4881 static BOOL
shader_arb_color_fixup_supported(struct color_fixup_desc fixup
)
4883 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
4885 TRACE("Checking support for color_fixup:\n");
4886 dump_color_fixup_desc(fixup
);
4889 /* We support everything except complex conversions. */
4890 if (!is_complex_fixup(fixup
))
4896 TRACE("[FAILED]\n");
4900 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
) {
4902 char write_mask
[20], regstr
[50];
4903 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
4904 BOOL is_color
= FALSE
;
4905 const struct wined3d_shader_dst_param
*dst
;
4907 if (!ins
->dst_count
) return;
4911 if (!shift
) return; /* Saturate alone is handled by the instructions */
4913 shader_arb_get_write_mask(ins
, dst
, write_mask
);
4914 shader_arb_get_register_name(ins
, &dst
->reg
, regstr
, &is_color
);
4916 /* Generate a line that does the output modifier computation
4917 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
4918 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
4920 shader_addline(buffer
, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins
),
4921 regstr
, write_mask
, regstr
, shift_tab
[shift
]);
4924 static const SHADER_HANDLER shader_arb_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
4926 /* WINED3DSIH_ABS */ shader_hw_map2gl
,
4927 /* WINED3DSIH_ADD */ shader_hw_map2gl
,
4928 /* WINED3DSIH_AND */ NULL
,
4929 /* WINED3DSIH_BEM */ pshader_hw_bem
,
4930 /* WINED3DSIH_BREAK */ shader_hw_break
,
4931 /* WINED3DSIH_BREAKC */ shader_hw_breakc
,
4932 /* WINED3DSIH_BREAKP */ NULL
,
4933 /* WINED3DSIH_CALL */ shader_hw_call
,
4934 /* WINED3DSIH_CALLNZ */ NULL
,
4935 /* WINED3DSIH_CMP */ pshader_hw_cmp
,
4936 /* WINED3DSIH_CND */ pshader_hw_cnd
,
4937 /* WINED3DSIH_CRS */ shader_hw_map2gl
,
4938 /* WINED3DSIH_CUT */ NULL
,
4939 /* WINED3DSIH_DCL */ NULL
,
4940 /* WINED3DSIH_DEF */ NULL
,
4941 /* WINED3DSIH_DEFB */ NULL
,
4942 /* WINED3DSIH_DEFI */ NULL
,
4943 /* WINED3DSIH_DIV */ NULL
,
4944 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add
,
4945 /* WINED3DSIH_DP3 */ shader_hw_map2gl
,
4946 /* WINED3DSIH_DP4 */ shader_hw_map2gl
,
4947 /* WINED3DSIH_DST */ shader_hw_map2gl
,
4948 /* WINED3DSIH_DSX */ shader_hw_map2gl
,
4949 /* WINED3DSIH_DSY */ shader_hw_dsy
,
4950 /* WINED3DSIH_ELSE */ shader_hw_else
,
4951 /* WINED3DSIH_EMIT */ NULL
,
4952 /* WINED3DSIH_ENDIF */ shader_hw_endif
,
4953 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop
,
4954 /* WINED3DSIH_ENDREP */ shader_hw_endrep
,
4955 /* WINED3DSIH_EXP */ shader_hw_scalar_op
,
4956 /* WINED3DSIH_EXPP */ shader_hw_scalar_op
,
4957 /* WINED3DSIH_FRC */ shader_hw_map2gl
,
4958 /* WINED3DSIH_FTOI */ NULL
,
4959 /* WINED3DSIH_IADD */ NULL
,
4960 /* WINED3DSIH_IEQ */ NULL
,
4961 /* WINED3DSIH_IF */ NULL
/* Hardcoded into the shader */,
4962 /* WINED3DSIH_IFC */ shader_hw_ifc
,
4963 /* WINED3DSIH_IGE */ NULL
,
4964 /* WINED3DSIH_IMUL */ NULL
,
4965 /* WINED3DSIH_ITOF */ NULL
,
4966 /* WINED3DSIH_LABEL */ shader_hw_label
,
4967 /* WINED3DSIH_LD */ NULL
,
4968 /* WINED3DSIH_LIT */ shader_hw_map2gl
,
4969 /* WINED3DSIH_LOG */ shader_hw_log_pow
,
4970 /* WINED3DSIH_LOGP */ shader_hw_log_pow
,
4971 /* WINED3DSIH_LOOP */ shader_hw_loop
,
4972 /* WINED3DSIH_LRP */ shader_hw_lrp
,
4973 /* WINED3DSIH_LT */ NULL
,
4974 /* WINED3DSIH_M3x2 */ shader_hw_mnxn
,
4975 /* WINED3DSIH_M3x3 */ shader_hw_mnxn
,
4976 /* WINED3DSIH_M3x4 */ shader_hw_mnxn
,
4977 /* WINED3DSIH_M4x3 */ shader_hw_mnxn
,
4978 /* WINED3DSIH_M4x4 */ shader_hw_mnxn
,
4979 /* WINED3DSIH_MAD */ shader_hw_map2gl
,
4980 /* WINED3DSIH_MAX */ shader_hw_map2gl
,
4981 /* WINED3DSIH_MIN */ shader_hw_map2gl
,
4982 /* WINED3DSIH_MOV */ shader_hw_mov
,
4983 /* WINED3DSIH_MOVA */ shader_hw_mov
,
4984 /* WINED3DSIH_MOVC */ NULL
,
4985 /* WINED3DSIH_MUL */ shader_hw_map2gl
,
4986 /* WINED3DSIH_NOP */ shader_hw_nop
,
4987 /* WINED3DSIH_NRM */ shader_hw_nrm
,
4988 /* WINED3DSIH_PHASE */ NULL
,
4989 /* WINED3DSIH_POW */ shader_hw_log_pow
,
4990 /* WINED3DSIH_RCP */ shader_hw_rcp
,
4991 /* WINED3DSIH_REP */ shader_hw_rep
,
4992 /* WINED3DSIH_RET */ shader_hw_ret
,
4993 /* WINED3DSIH_RSQ */ shader_hw_scalar_op
,
4994 /* WINED3DSIH_SAMPLE */ NULL
,
4995 /* WINED3DSIH_SAMPLE_GRAD */ NULL
,
4996 /* WINED3DSIH_SAMPLE_LOD */ NULL
,
4997 /* WINED3DSIH_SETP */ NULL
,
4998 /* WINED3DSIH_SGE */ shader_hw_map2gl
,
4999 /* WINED3DSIH_SGN */ shader_hw_sgn
,
5000 /* WINED3DSIH_SINCOS */ shader_hw_sincos
,
5001 /* WINED3DSIH_SLT */ shader_hw_map2gl
,
5002 /* WINED3DSIH_SQRT */ NULL
,
5003 /* WINED3DSIH_SUB */ shader_hw_map2gl
,
5004 /* WINED3DSIH_TEX */ pshader_hw_tex
,
5005 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem
,
5006 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem
,
5007 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord
,
5008 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth
,
5009 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3
,
5010 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex
,
5011 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill
,
5012 /* WINED3DSIH_TEXLDD */ shader_hw_texldd
,
5013 /* WINED3DSIH_TEXLDL */ shader_hw_texldl
,
5014 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth
,
5015 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad
,
5016 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex
,
5017 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3
,
5018 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
5019 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad
,
5020 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec
,
5021 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex
,
5022 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec
,
5023 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar
,
5024 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb
,
5025 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb
,
5026 /* WINED3DSIH_UTOF */ NULL
,
5029 static inline BOOL
get_bool_const(const struct wined3d_shader_instruction
*ins
, IWineD3DBaseShaderImpl
*This
, DWORD idx
)
5031 BOOL vshader
= shader_is_vshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
5033 WORD flag
= (1 << idx
);
5034 const local_constant
*constant
;
5035 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5037 if(This
->baseShader
.reg_maps
.local_bool_consts
& flag
)
5039 /* What good is a if(bool) with a hardcoded local constant? I don't know, but handle it */
5040 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsB
, local_constant
, entry
)
5042 if (constant
->idx
== idx
)
5044 return constant
->value
[0];
5047 ERR("Local constant not found\n");
5052 if(vshader
) bools
= priv
->cur_vs_args
->clip
.boolclip
.bools
;
5053 else bools
= priv
->cur_ps_args
->bools
;
5054 return bools
& flag
;
5058 static void get_loop_control_const(const struct wined3d_shader_instruction
*ins
,
5059 IWineD3DBaseShaderImpl
*This
, UINT idx
, struct wined3d_shader_loop_control
*loop_control
)
5061 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5063 /* Integer constants can either be a local constant, or they can be stored in the shader
5064 * type specific compile args. */
5065 if (This
->baseShader
.reg_maps
.local_int_consts
& (1 << idx
))
5067 const local_constant
*constant
;
5069 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsI
, local_constant
, entry
)
5071 if (constant
->idx
== idx
)
5073 loop_control
->count
= constant
->value
[0];
5074 loop_control
->start
= constant
->value
[1];
5075 /* Step is signed. */
5076 loop_control
->step
= (int)constant
->value
[2];
5080 /* If this happens the flag was set incorrectly */
5081 ERR("Local constant not found\n");
5082 loop_control
->count
= 0;
5083 loop_control
->start
= 0;
5084 loop_control
->step
= 0;
5088 switch (This
->baseShader
.reg_maps
.shader_version
.type
)
5090 case WINED3D_SHADER_TYPE_VERTEX
:
5091 /* Count and aL start value are unsigned */
5092 loop_control
->count
= priv
->cur_vs_args
->loop_ctrl
[idx
][0];
5093 loop_control
->start
= priv
->cur_vs_args
->loop_ctrl
[idx
][1];
5094 /* Step is signed. */
5095 loop_control
->step
= ((char)priv
->cur_vs_args
->loop_ctrl
[idx
][2]);
5098 case WINED3D_SHADER_TYPE_PIXEL
:
5099 loop_control
->count
= priv
->cur_ps_args
->loop_ctrl
[idx
][0];
5100 loop_control
->start
= priv
->cur_ps_args
->loop_ctrl
[idx
][1];
5101 loop_control
->step
= ((char)priv
->cur_ps_args
->loop_ctrl
[idx
][2]);
5105 FIXME("Unhandled shader type %#x.\n", This
->baseShader
.reg_maps
.shader_version
.type
);
5110 static void record_instruction(struct list
*list
, const struct wined3d_shader_instruction
*ins
)
5113 struct wined3d_shader_dst_param
*dst_param
= NULL
;
5114 struct wined3d_shader_src_param
*src_param
= NULL
, *rel_addr
= NULL
;
5115 struct recorded_instruction
*rec
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*rec
));
5118 ERR("Out of memory\n");
5123 dst_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param
));
5124 if(!dst_param
) goto free
;
5125 *dst_param
= *ins
->dst
;
5126 if(ins
->dst
->reg
.rel_addr
)
5128 rel_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param
->reg
.rel_addr
));
5129 if(!rel_addr
) goto free
;
5130 *rel_addr
= *ins
->dst
->reg
.rel_addr
;
5131 dst_param
->reg
.rel_addr
= rel_addr
;
5133 rec
->ins
.dst
= dst_param
;
5135 src_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*src_param
) * ins
->src_count
);
5136 if(!src_param
) goto free
;
5137 for(i
= 0; i
< ins
->src_count
; i
++)
5139 src_param
[i
] = ins
->src
[i
];
5140 if(ins
->src
[i
].reg
.rel_addr
)
5142 rel_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr
));
5143 if(!rel_addr
) goto free
;
5144 *rel_addr
= *ins
->src
[i
].reg
.rel_addr
;
5145 src_param
[i
].reg
.rel_addr
= rel_addr
;
5148 rec
->ins
.src
= src_param
;
5149 list_add_tail(list
, &rec
->entry
);
5153 ERR("Out of memory\n");
5156 HeapFree(GetProcessHeap(), 0, (void *) dst_param
->reg
.rel_addr
);
5157 HeapFree(GetProcessHeap(), 0, dst_param
);
5161 for(i
= 0; i
< ins
->src_count
; i
++)
5163 HeapFree(GetProcessHeap(), 0, (void *) src_param
[i
].reg
.rel_addr
);
5165 HeapFree(GetProcessHeap(), 0, src_param
);
5167 HeapFree(GetProcessHeap(), 0, rec
);
5170 static void free_recorded_instruction(struct list
*list
)
5172 struct recorded_instruction
*rec_ins
, *entry2
;
5175 LIST_FOR_EACH_ENTRY_SAFE(rec_ins
, entry2
, list
, struct recorded_instruction
, entry
)
5177 list_remove(&rec_ins
->entry
);
5178 if(rec_ins
->ins
.dst
)
5180 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.dst
->reg
.rel_addr
);
5181 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.dst
);
5183 if(rec_ins
->ins
.src
)
5185 for(i
= 0; i
< rec_ins
->ins
.src_count
; i
++)
5187 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.src
[i
].reg
.rel_addr
);
5189 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.src
);
5191 HeapFree(GetProcessHeap(), 0, rec_ins
);
5195 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
5196 SHADER_HANDLER hw_fct
;
5197 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5198 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
5199 struct control_frame
*control_frame
;
5200 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
5203 if(ins
->handler_idx
== WINED3DSIH_LOOP
|| ins
->handler_idx
== WINED3DSIH_REP
)
5205 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
5206 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
5208 if(ins
->handler_idx
== WINED3DSIH_LOOP
) control_frame
->type
= LOOP
;
5209 if(ins
->handler_idx
== WINED3DSIH_REP
) control_frame
->type
= REP
;
5211 if(priv
->target_version
>= NV2
)
5213 control_frame
->no
.loop
= priv
->num_loops
++;
5218 /* Don't bother recording when we're in a not used if branch */
5224 if(!priv
->recording
)
5226 list_init(&priv
->record
);
5227 priv
->recording
= TRUE
;
5228 control_frame
->outer_loop
= TRUE
;
5229 get_loop_control_const(ins
, This
, ins
->src
[0].reg
.idx
, &control_frame
->loop_control
);
5230 return; /* Instruction is handled */
5232 /* Record this loop in the outer loop's recording */
5235 else if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
|| ins
->handler_idx
== WINED3DSIH_ENDREP
)
5237 if(priv
->target_version
>= NV2
)
5239 /* Nothing to do. The control frame is popped after the HW instr handler */
5243 struct list
*e
= list_head(&priv
->control_frames
);
5244 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5245 list_remove(&control_frame
->entry
);
5247 if(control_frame
->outer_loop
)
5249 unsigned int iteration
;
5253 /* Turn off recording before playback */
5254 priv
->recording
= FALSE
;
5256 /* Move the recorded instructions to a separate list and get them out of the private data
5257 * structure. If there are nested loops, the shader_arb_handle_instruction below will
5258 * be recorded again, thus priv->record might be overwritten
5261 list_move_tail(©
, &priv
->record
);
5262 list_init(&priv
->record
);
5264 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
5266 shader_addline(buffer
, "#unrolling loop: %u iterations, aL=%u, inc %d\n",
5267 control_frame
->loop_control
.count
, control_frame
->loop_control
.start
,
5268 control_frame
->loop_control
.step
);
5269 aL
= control_frame
->loop_control
.start
;
5273 shader_addline(buffer
, "#unrolling rep: %u iterations\n", control_frame
->loop_control
.count
);
5276 for (iteration
= 0; iteration
< control_frame
->loop_control
.count
; ++iteration
)
5278 struct recorded_instruction
*rec_ins
;
5279 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
5282 shader_addline(buffer
, "#Iteration %u, aL=%d\n", iteration
, aL
);
5286 shader_addline(buffer
, "#Iteration %u\n", iteration
);
5289 LIST_FOR_EACH_ENTRY(rec_ins
, ©
, struct recorded_instruction
, entry
)
5291 shader_arb_handle_instruction(&rec_ins
->ins
);
5294 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
5296 aL
+= control_frame
->loop_control
.step
;
5299 shader_addline(buffer
, "#end loop/rep\n");
5301 free_recorded_instruction(©
);
5302 HeapFree(GetProcessHeap(), 0, control_frame
);
5303 return; /* Instruction is handled */
5307 /* This is a nested loop. Proceed to the normal recording function */
5308 HeapFree(GetProcessHeap(), 0, control_frame
);
5315 record_instruction(&priv
->record
, ins
);
5320 if(ins
->handler_idx
== WINED3DSIH_IF
)
5322 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
5323 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
5324 control_frame
->type
= IF
;
5326 bool_const
= get_bool_const(ins
, This
, ins
->src
[0].reg
.idx
);
5327 if(ins
->src
[0].modifiers
== WINED3DSPSM_NOT
) bool_const
= !bool_const
;
5328 if (!priv
->muted
&& !bool_const
)
5330 shader_addline(buffer
, "#if(FALSE){\n");
5332 control_frame
->muting
= TRUE
;
5334 else shader_addline(buffer
, "#if(TRUE) {\n");
5336 return; /* Instruction is handled */
5338 else if(ins
->handler_idx
== WINED3DSIH_IFC
)
5340 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
5341 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
5342 control_frame
->type
= IFC
;
5343 control_frame
->no
.ifc
= priv
->num_ifcs
++;
5344 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
5346 else if(ins
->handler_idx
== WINED3DSIH_ELSE
)
5348 struct list
*e
= list_head(&priv
->control_frames
);
5349 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5351 if(control_frame
->type
== IF
)
5353 shader_addline(buffer
, "#} else {\n");
5354 if(!priv
->muted
&& !control_frame
->muting
)
5357 control_frame
->muting
= TRUE
;
5359 else if(control_frame
->muting
) priv
->muted
= FALSE
;
5360 return; /* Instruction is handled. */
5362 /* In case of an ifc, generate a HW shader instruction */
5364 else if(ins
->handler_idx
== WINED3DSIH_ENDIF
)
5366 struct list
*e
= list_head(&priv
->control_frames
);
5367 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5369 if(control_frame
->type
== IF
)
5371 shader_addline(buffer
, "#} endif\n");
5372 if(control_frame
->muting
) priv
->muted
= FALSE
;
5373 list_remove(&control_frame
->entry
);
5374 HeapFree(GetProcessHeap(), 0, control_frame
);
5375 return; /* Instruction is handled */
5379 if(priv
->muted
) return;
5381 /* Select handler */
5382 hw_fct
= shader_arb_instruction_handler_table
[ins
->handler_idx
];
5384 /* Unhandled opcode */
5387 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
5392 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
|| ins
->handler_idx
== WINED3DSIH_ENDREP
)
5394 struct list
*e
= list_head(&priv
->control_frames
);
5395 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5396 list_remove(&control_frame
->entry
);
5397 HeapFree(GetProcessHeap(), 0, control_frame
);
5400 else if(ins
->handler_idx
== WINED3DSIH_ENDIF
)
5402 /* Non-ifc ENDIFs don't reach that place because of the return in the if block above */
5403 struct list
*e
= list_head(&priv
->control_frames
);
5404 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
5405 list_remove(&control_frame
->entry
);
5406 HeapFree(GetProcessHeap(), 0, control_frame
);
5410 shader_arb_add_instruction_modifiers(ins
);
5413 const shader_backend_t arb_program_shader_backend
= {
5414 shader_arb_handle_instruction
,
5416 shader_arb_select_depth_blt
,
5417 shader_arb_deselect_depth_blt
,
5418 shader_arb_update_float_vertex_constants
,
5419 shader_arb_update_float_pixel_constants
,
5420 shader_arb_load_constants
,
5421 shader_arb_load_np2fixup_constants
,
5425 shader_arb_dirty_const
,
5426 shader_arb_get_caps
,
5427 shader_arb_color_fixup_supported
,
5430 /* ARB_fragment_program fixed function pipeline replacement definitions */
5431 #define ARB_FFP_CONST_TFACTOR 0
5432 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
5433 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
5434 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
5435 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
5437 struct arbfp_ffp_desc
5439 struct ffp_frag_desc parent
;
5441 unsigned int num_textures_used
;
5444 /* Context activation is done by the caller. */
5445 static void arbfp_enable(IWineD3DDevice
*iface
, BOOL enable
) {
5448 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
5449 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
5451 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
5452 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
5457 static HRESULT
arbfp_alloc(IWineD3DDevice
*iface
) {
5458 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
5459 struct shader_arb_priv
*priv
;
5460 /* Share private data between the shader backend and the pipeline replacement, if both
5461 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
5462 * if no pixel shader is bound or not
5464 if(This
->shader_backend
== &arb_program_shader_backend
) {
5465 This
->fragment_priv
= This
->shader_priv
;
5467 This
->fragment_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_arb_priv
));
5468 if(!This
->fragment_priv
) return E_OUTOFMEMORY
;
5470 priv
= This
->fragment_priv
;
5471 if (wine_rb_init(&priv
->fragment_shaders
, &wined3d_ffp_frag_program_rb_functions
) == -1)
5473 ERR("Failed to initialize rbtree.\n");
5474 HeapFree(GetProcessHeap(), 0, This
->fragment_priv
);
5475 return E_OUTOFMEMORY
;
5477 priv
->use_arbfp_fixed_func
= TRUE
;
5481 /* Context activation is done by the caller. */
5482 static void arbfp_free_ffpshader(struct wine_rb_entry
*entry
, void *context
)
5484 const struct wined3d_gl_info
*gl_info
= context
;
5485 struct arbfp_ffp_desc
*entry_arb
= WINE_RB_ENTRY_VALUE(entry
, struct arbfp_ffp_desc
, parent
.entry
);
5488 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb
->shader
));
5489 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
5490 HeapFree(GetProcessHeap(), 0, entry_arb
);
5494 /* Context activation is done by the caller. */
5495 static void arbfp_free(IWineD3DDevice
*iface
) {
5496 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
5497 struct shader_arb_priv
*priv
= This
->fragment_priv
;
5499 wine_rb_destroy(&priv
->fragment_shaders
, arbfp_free_ffpshader
, &This
->adapter
->gl_info
);
5500 priv
->use_arbfp_fixed_func
= FALSE
;
5502 if(This
->shader_backend
!= &arb_program_shader_backend
) {
5503 HeapFree(GetProcessHeap(), 0, This
->fragment_priv
);
5507 static void arbfp_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
5509 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
;
5510 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
|
5511 WINED3DTEXOPCAPS_SELECTARG1
|
5512 WINED3DTEXOPCAPS_SELECTARG2
|
5513 WINED3DTEXOPCAPS_MODULATE4X
|
5514 WINED3DTEXOPCAPS_MODULATE2X
|
5515 WINED3DTEXOPCAPS_MODULATE
|
5516 WINED3DTEXOPCAPS_ADDSIGNED2X
|
5517 WINED3DTEXOPCAPS_ADDSIGNED
|
5518 WINED3DTEXOPCAPS_ADD
|
5519 WINED3DTEXOPCAPS_SUBTRACT
|
5520 WINED3DTEXOPCAPS_ADDSMOOTH
|
5521 WINED3DTEXOPCAPS_BLENDCURRENTALPHA
|
5522 WINED3DTEXOPCAPS_BLENDFACTORALPHA
|
5523 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
|
5524 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
|
5525 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
|
5526 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
|
5527 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
|
5528 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
|
5529 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
|
5530 WINED3DTEXOPCAPS_DOTPRODUCT3
|
5531 WINED3DTEXOPCAPS_MULTIPLYADD
|
5532 WINED3DTEXOPCAPS_LERP
|
5533 WINED3DTEXOPCAPS_BUMPENVMAP
|
5534 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
5536 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
5538 caps
->MaxTextureBlendStages
= 8;
5539 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, 8);
5542 static void state_texfactor_arbfp(DWORD state_id
, IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
5544 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5545 const struct wined3d_state
*state
= &stateblock
->state
;
5546 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5549 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5550 * application provided constants
5552 if (device
->shader_backend
== &arb_program_shader_backend
)
5554 if (use_ps(state
)) return;
5556 context
->pshader_const_dirty
[ARB_FFP_CONST_TFACTOR
] = 1;
5557 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_TFACTOR
+ 1);
5560 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3DRS_TEXTUREFACTOR
], col
);
5561 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_TFACTOR
, col
));
5562 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
5566 static void state_arb_specularenable(DWORD state_id
,
5567 IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
5569 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5570 const struct wined3d_state
*state
= &stateblock
->state
;
5571 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5574 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5575 * application provided constants
5577 if (device
->shader_backend
== &arb_program_shader_backend
)
5579 if (use_ps(state
)) return;
5581 context
->pshader_const_dirty
[ARB_FFP_CONST_SPECULAR_ENABLE
] = 1;
5582 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_SPECULAR_ENABLE
+ 1);
5585 if (state
->render_states
[WINED3DRS_SPECULARENABLE
])
5587 /* The specular color has no alpha */
5588 col
[0] = 1.0f
; col
[1] = 1.0f
;
5589 col
[2] = 1.0f
; col
[3] = 0.0f
;
5591 col
[0] = 0.0f
; col
[1] = 0.0f
;
5592 col
[2] = 0.0f
; col
[3] = 0.0f
;
5594 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_SPECULAR_ENABLE
, col
));
5595 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
5598 static void set_bumpmat_arbfp(DWORD state_id
, IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
5600 DWORD stage
= (state_id
- STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE
+ 1);
5601 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5602 const struct wined3d_state
*state
= &stateblock
->state
;
5603 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5608 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
5609 if (stage
&& (ps
->baseShader
.reg_maps
.bumpmat
& (1 << stage
)))
5611 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
5614 if (!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
))
5615 stateblock_apply_state(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
5618 if(device
->shader_backend
== &arb_program_shader_backend
) {
5619 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5622 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
5623 context
->pshader_const_dirty
[ARB_FFP_CONST_BUMPMAT(stage
)] = 1;
5624 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_BUMPMAT(stage
) + 1);
5627 mat
[0][0] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT00
]);
5628 mat
[0][1] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT01
]);
5629 mat
[1][0] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT10
]);
5630 mat
[1][1] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVMAT11
]);
5632 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_BUMPMAT(stage
), &mat
[0][0]));
5633 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
5636 static void tex_bumpenvlum_arbfp(DWORD state_id
, IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
5638 DWORD stage
= (state_id
- STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE
+ 1);
5639 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5640 const struct wined3d_state
*state
= &stateblock
->state
;
5641 IWineD3DDeviceImpl
*device
= stateblock
->device
;
5646 IWineD3DPixelShaderImpl
*ps
= state
->pixel_shader
;
5647 if (stage
&& (ps
->baseShader
.reg_maps
.luminanceparams
& (1 << stage
)))
5649 /* The pixel shader has to know the luminance offset. Do a constants update if it
5650 * isn't scheduled anyway
5652 if (!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
))
5653 stateblock_apply_state(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
5656 if(device
->shader_backend
== &arb_program_shader_backend
) {
5657 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5660 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
5661 context
->pshader_const_dirty
[ARB_FFP_CONST_LUMINANCE(stage
)] = 1;
5662 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_LUMINANCE(stage
) + 1);
5665 param
[0] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVLSCALE
]);
5666 param
[1] = *((float *)&state
->texture_states
[stage
][WINED3DTSS_BUMPENVLOFFSET
]);
5670 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_LUMINANCE(stage
), param
));
5671 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
5674 static const char *get_argreg(struct wined3d_shader_buffer
*buffer
, DWORD argnum
, unsigned int stage
, DWORD arg
)
5678 if(arg
== ARG_UNUSED
) return "unused"; /* This is the marker for unused registers */
5680 switch(arg
& WINED3DTA_SELECTMASK
) {
5681 case WINED3DTA_DIFFUSE
:
5682 ret
= "fragment.color.primary"; break;
5684 case WINED3DTA_CURRENT
:
5685 if (!stage
) ret
= "fragment.color.primary";
5689 case WINED3DTA_TEXTURE
:
5691 case 0: ret
= "tex0"; break;
5692 case 1: ret
= "tex1"; break;
5693 case 2: ret
= "tex2"; break;
5694 case 3: ret
= "tex3"; break;
5695 case 4: ret
= "tex4"; break;
5696 case 5: ret
= "tex5"; break;
5697 case 6: ret
= "tex6"; break;
5698 case 7: ret
= "tex7"; break;
5699 default: ret
= "unknown texture";
5703 case WINED3DTA_TFACTOR
:
5704 ret
= "tfactor"; break;
5706 case WINED3DTA_SPECULAR
:
5707 ret
= "fragment.color.secondary"; break;
5709 case WINED3DTA_TEMP
:
5710 ret
= "tempreg"; break;
5712 case WINED3DTA_CONSTANT
:
5713 FIXME("Implement perstage constants\n");
5715 case 0: ret
= "const0"; break;
5716 case 1: ret
= "const1"; break;
5717 case 2: ret
= "const2"; break;
5718 case 3: ret
= "const3"; break;
5719 case 4: ret
= "const4"; break;
5720 case 5: ret
= "const5"; break;
5721 case 6: ret
= "const6"; break;
5722 case 7: ret
= "const7"; break;
5723 default: ret
= "unknown constant";
5731 if(arg
& WINED3DTA_COMPLEMENT
) {
5732 shader_addline(buffer
, "SUB arg%u, const.x, %s;\n", argnum
, ret
);
5733 if(argnum
== 0) ret
= "arg0";
5734 if(argnum
== 1) ret
= "arg1";
5735 if(argnum
== 2) ret
= "arg2";
5737 if(arg
& WINED3DTA_ALPHAREPLICATE
) {
5738 shader_addline(buffer
, "MOV arg%u, %s.w;\n", argnum
, ret
);
5739 if(argnum
== 0) ret
= "arg0";
5740 if(argnum
== 1) ret
= "arg1";
5741 if(argnum
== 2) ret
= "arg2";
5746 static void gen_ffp_instr(struct wined3d_shader_buffer
*buffer
, unsigned int stage
, BOOL color
,
5747 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
5749 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
5750 unsigned int mul
= 1;
5751 BOOL mul_final_dest
= FALSE
;
5753 if(color
&& alpha
) dstmask
= "";
5754 else if(color
) dstmask
= ".xyz";
5755 else dstmask
= ".w";
5757 if(dst
== tempreg
) dstreg
= "tempreg";
5758 else dstreg
= "ret";
5760 arg0
= get_argreg(buffer
, 0, stage
, dw_arg0
);
5761 arg1
= get_argreg(buffer
, 1, stage
, dw_arg1
);
5762 arg2
= get_argreg(buffer
, 2, stage
, dw_arg2
);
5765 case WINED3DTOP_DISABLE
:
5766 if (!stage
) shader_addline(buffer
, "MOV %s%s, fragment.color.primary;\n", dstreg
, dstmask
);
5769 case WINED3DTOP_SELECTARG2
:
5771 case WINED3DTOP_SELECTARG1
:
5772 shader_addline(buffer
, "MOV %s%s, %s;\n", dstreg
, dstmask
, arg1
);
5775 case WINED3DTOP_MODULATE4X
:
5777 case WINED3DTOP_MODULATE2X
:
5779 if (!strcmp(dstreg
, "result.color"))
5782 mul_final_dest
= TRUE
;
5784 case WINED3DTOP_MODULATE
:
5785 shader_addline(buffer
, "MUL %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
5788 case WINED3DTOP_ADDSIGNED2X
:
5790 if (!strcmp(dstreg
, "result.color"))
5793 mul_final_dest
= TRUE
;
5795 case WINED3DTOP_ADDSIGNED
:
5796 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
5798 case WINED3DTOP_ADD
:
5799 shader_addline(buffer
, "ADD_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
5802 case WINED3DTOP_SUBTRACT
:
5803 shader_addline(buffer
, "SUB_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
5806 case WINED3DTOP_ADDSMOOTH
:
5807 shader_addline(buffer
, "SUB arg1, const.x, %s;\n", arg1
);
5808 shader_addline(buffer
, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5811 case WINED3DTOP_BLENDCURRENTALPHA
:
5812 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
5813 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5815 case WINED3DTOP_BLENDFACTORALPHA
:
5816 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
5817 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5819 case WINED3DTOP_BLENDTEXTUREALPHA
:
5820 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5821 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5823 case WINED3DTOP_BLENDDIFFUSEALPHA
:
5824 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
5825 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5828 case WINED3DTOP_BLENDTEXTUREALPHAPM
:
5829 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5830 shader_addline(buffer
, "SUB arg0.w, const.x, %s.w;\n", arg0
);
5831 shader_addline(buffer
, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5834 /* D3DTOP_PREMODULATE ???? */
5836 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR
:
5837 shader_addline(buffer
, "SUB arg0.w, const.x, %s;\n", arg1
);
5838 shader_addline(buffer
, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5840 case WINED3DTOP_MODULATEALPHA_ADDCOLOR
:
5841 shader_addline(buffer
, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
5843 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA
:
5844 shader_addline(buffer
, "SUB arg0, const.x, %s;\n", arg1
);
5845 shader_addline(buffer
, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg
, dstmask
, arg2
, arg1
);
5847 case WINED3DTOP_MODULATECOLOR_ADDALPHA
:
5848 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
5851 case WINED3DTOP_DOTPRODUCT3
:
5853 if (!strcmp(dstreg
, "result.color"))
5856 mul_final_dest
= TRUE
;
5858 shader_addline(buffer
, "SUB arg1, %s, const.w;\n", arg1
);
5859 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
5860 shader_addline(buffer
, "DP3_SAT %s%s, arg1, arg2;\n", dstreg
, dstmask
);
5863 case WINED3DTOP_MULTIPLYADD
:
5864 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg0
);
5867 case WINED3DTOP_LERP
:
5868 /* The msdn is not quite right here */
5869 shader_addline(buffer
, "LRP %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5872 case WINED3DTOP_BUMPENVMAP
:
5873 case WINED3DTOP_BUMPENVMAPLUMINANCE
:
5874 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
5878 FIXME("Unhandled texture op %08x\n", op
);
5882 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
5883 } else if(mul
== 4) {
5884 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
5888 static GLuint
gen_arbfp_ffp_shader(const struct ffp_frag_settings
*settings
, IWineD3DStateBlockImpl
*stateblock
)
5890 const struct wined3d_gl_info
*gl_info
= &stateblock
->device
->adapter
->gl_info
;
5892 struct wined3d_shader_buffer buffer
;
5893 BOOL tex_read
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5894 BOOL bump_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5895 BOOL luminance_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5896 const char *textype
;
5897 const char *instr
, *sat
;
5898 char colorcor_dst
[8];
5900 DWORD arg0
, arg1
, arg2
;
5901 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
5903 const char *final_combiner_src
= "ret";
5906 /* Find out which textures are read */
5907 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
5908 if(settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
) break;
5909 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
5910 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
5911 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
5912 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5913 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5914 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5916 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHA
) tex_read
[stage
] = TRUE
;
5917 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHAPM
) tex_read
[stage
] = TRUE
;
5918 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
) {
5919 bump_used
[stage
] = TRUE
;
5920 tex_read
[stage
] = TRUE
;
5922 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
5923 bump_used
[stage
] = TRUE
;
5924 tex_read
[stage
] = TRUE
;
5925 luminance_used
[stage
] = TRUE
;
5926 } else if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDFACTORALPHA
) {
5927 tfactor_used
= TRUE
;
5930 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
5931 tfactor_used
= TRUE
;
5934 if(settings
->op
[stage
].dst
== tempreg
) tempreg_used
= TRUE
;
5935 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
5936 tempreg_used
= TRUE
;
5939 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) continue;
5940 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
5941 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
5942 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
5943 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5944 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5945 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5947 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
5948 tempreg_used
= TRUE
;
5950 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
5951 tfactor_used
= TRUE
;
5956 if (!shader_buffer_init(&buffer
))
5958 ERR("Failed to initialize shader buffer.\n");
5962 shader_addline(&buffer
, "!!ARBfp1.0\n");
5964 switch(settings
->fog
) {
5965 case FOG_OFF
: break;
5966 case FOG_LINEAR
: shader_addline(&buffer
, "OPTION ARB_fog_linear;\n"); break;
5967 case FOG_EXP
: shader_addline(&buffer
, "OPTION ARB_fog_exp;\n"); break;
5968 case FOG_EXP2
: shader_addline(&buffer
, "OPTION ARB_fog_exp2;\n"); break;
5969 default: FIXME("Unexpected fog setting %d\n", settings
->fog
);
5972 shader_addline(&buffer
, "PARAM const = {1, 2, 4, 0.5};\n");
5973 shader_addline(&buffer
, "TEMP TMP;\n");
5974 shader_addline(&buffer
, "TEMP ret;\n");
5975 if(tempreg_used
|| settings
->sRGB_write
) shader_addline(&buffer
, "TEMP tempreg;\n");
5976 shader_addline(&buffer
, "TEMP arg0;\n");
5977 shader_addline(&buffer
, "TEMP arg1;\n");
5978 shader_addline(&buffer
, "TEMP arg2;\n");
5979 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
5980 if(!tex_read
[stage
]) continue;
5981 shader_addline(&buffer
, "TEMP tex%u;\n", stage
);
5982 if(!bump_used
[stage
]) continue;
5983 shader_addline(&buffer
, "PARAM bumpmat%u = program.env[%u];\n", stage
, ARB_FFP_CONST_BUMPMAT(stage
));
5984 if(!luminance_used
[stage
]) continue;
5985 shader_addline(&buffer
, "PARAM luminance%u = program.env[%u];\n", stage
, ARB_FFP_CONST_LUMINANCE(stage
));
5988 shader_addline(&buffer
, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR
);
5990 shader_addline(&buffer
, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE
);
5992 if(settings
->sRGB_write
) {
5993 shader_addline(&buffer
, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
5994 srgb_mul_low
, srgb_cmp
, srgb_pow
, srgb_mul_high
);
5995 shader_addline(&buffer
, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
5996 srgb_sub_high
, 0.0, 0.0, 0.0);
5999 if (ffp_clip_emul(&stateblock
->state
) && settings
->emul_clipplanes
)
6000 shader_addline(&buffer
, "KIL fragment.texcoord[7];\n");
6002 /* Generate texture sampling instructions) */
6003 for(stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3DTOP_DISABLE
; stage
++) {
6004 if(!tex_read
[stage
]) continue;
6006 switch(settings
->op
[stage
].tex_type
) {
6007 case tex_1d
: textype
= "1D"; break;
6008 case tex_2d
: textype
= "2D"; break;
6009 case tex_3d
: textype
= "3D"; break;
6010 case tex_cube
: textype
= "CUBE"; break;
6011 case tex_rect
: textype
= "RECT"; break;
6012 default: textype
= "unexpected_textype"; break;
6015 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
||
6016 settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
6022 if(settings
->op
[stage
].projected
== proj_none
) {
6024 } else if(settings
->op
[stage
].projected
== proj_count4
||
6025 settings
->op
[stage
].projected
== proj_count3
) {
6028 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
6033 (settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAP
||
6034 settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
)) {
6035 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage
- 1);
6036 shader_addline(&buffer
, "DP3 ret.x, arg1, tex%u;\n", stage
- 1);
6037 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage
- 1);
6038 shader_addline(&buffer
, "DP3 ret.y, arg1, tex%u;\n", stage
- 1);
6040 /* with projective textures, texbem only divides the static texture coord, not the displacement,
6041 * so multiply the displacement with the dividing parameter before passing it to TXP
6043 if (settings
->op
[stage
].projected
!= proj_none
) {
6044 if(settings
->op
[stage
].projected
== proj_count4
) {
6045 shader_addline(&buffer
, "MOV ret.w, fragment.texcoord[%u].w;\n", stage
);
6046 shader_addline(&buffer
, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage
, stage
);
6048 shader_addline(&buffer
, "MOV ret.w, fragment.texcoord[%u].z;\n", stage
);
6049 shader_addline(&buffer
, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage
, stage
);
6052 shader_addline(&buffer
, "ADD ret, ret, fragment.texcoord[%u];\n", stage
);
6055 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
6056 instr
, sat
, stage
, stage
, textype
);
6057 if(settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
6058 shader_addline(&buffer
, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
6059 stage
- 1, stage
- 1, stage
- 1);
6060 shader_addline(&buffer
, "MUL tex%u, tex%u, ret.x;\n", stage
, stage
);
6062 } else if(settings
->op
[stage
].projected
== proj_count3
) {
6063 shader_addline(&buffer
, "MOV ret, fragment.texcoord[%u];\n", stage
);
6064 shader_addline(&buffer
, "MOV ret.w, ret.z;\n");
6065 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
6066 instr
, sat
, stage
, stage
, textype
);
6068 shader_addline(&buffer
, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
6069 instr
, sat
, stage
, stage
, stage
, textype
);
6072 sprintf(colorcor_dst
, "tex%u", stage
);
6073 gen_color_correction(&buffer
, colorcor_dst
, WINED3DSP_WRITEMASK_ALL
, "const.x", "const.y",
6074 settings
->op
[stage
].color_fixup
);
6077 /* Generate the main shader */
6078 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6080 if (settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
)
6082 if (!stage
) final_combiner_src
= "fragment.color.primary";
6086 if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
6087 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
6088 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
6089 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
6090 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
6091 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
6092 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
6093 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
6094 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
6095 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
6096 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
6097 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6099 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
&&
6100 settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
&&
6101 settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
&&
6102 settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6105 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) {
6106 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6107 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6108 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6110 shader_addline(&buffer
, "MOV ret.w, fragment.color.primary.w;\n");
6114 gen_ffp_instr(&buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
6115 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6116 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6118 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6119 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6120 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6121 gen_ffp_instr(&buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
6122 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
6123 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
6127 if(settings
->sRGB_write
) {
6128 shader_addline(&buffer
, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
6129 arbfp_add_sRGB_correction(&buffer
, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE
);
6130 shader_addline(&buffer
, "MOV result.color, ret;\n");
6132 shader_addline(&buffer
, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
6136 shader_addline(&buffer
, "END\n");
6138 /* Generate the shader */
6139 GL_EXTCALL(glGenProgramsARB(1, &ret
));
6140 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
));
6141 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
6142 strlen(buffer
.buffer
), buffer
.buffer
));
6143 checkGLcall("glProgramStringARB()");
6145 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
6148 FIXME("Fragment program error at position %d: %s\n\n", pos
,
6149 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
6150 shader_arb_dump_program_source(buffer
.buffer
);
6156 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
6157 checkGLcall("glGetProgramivARB()");
6158 if (!native
) WARN("Program exceeds native resource limits.\n");
6161 shader_buffer_free(&buffer
);
6165 static void fragment_prog_arbfp(DWORD state_id
, IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
6167 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6168 const struct wined3d_state
*state
= &stateblock
->state
;
6169 IWineD3DDeviceImpl
*device
= stateblock
->device
;
6170 struct shader_arb_priv
*priv
= device
->fragment_priv
;
6171 BOOL use_vshader
= use_vs(state
);
6172 BOOL use_pshader
= use_ps(state
);
6173 struct ffp_frag_settings settings
;
6174 const struct arbfp_ffp_desc
*desc
;
6177 TRACE("state_id %#x, stateblock %p, context %p\n", state_id
, stateblock
, context
);
6179 if(isStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
))) {
6180 if(!use_pshader
&& device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
6181 /* Reload fixed function constants since they collide with the pixel shader constants */
6182 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
6183 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
6185 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
6186 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
6187 } else if(use_pshader
&& !isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
6188 device
->shader_backend
->shader_select(context
, use_pshader
, use_vshader
);
6194 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
6195 gen_ffp_frag_op(stateblock
, &settings
, FALSE
);
6196 desc
= (const struct arbfp_ffp_desc
*)find_ffp_frag_shader(&priv
->fragment_shaders
, &settings
);
6198 struct arbfp_ffp_desc
*new_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc
));
6201 ERR("Out of memory\n");
6204 new_desc
->num_textures_used
= 0;
6205 for (i
= 0; i
< gl_info
->limits
.texture_stages
; ++i
)
6207 if(settings
.op
[i
].cop
== WINED3DTOP_DISABLE
) break;
6208 new_desc
->num_textures_used
= i
;
6211 memcpy(&new_desc
->parent
.settings
, &settings
, sizeof(settings
));
6212 new_desc
->shader
= gen_arbfp_ffp_shader(&settings
, stateblock
);
6213 add_ffp_frag_shader(&priv
->fragment_shaders
, &new_desc
->parent
);
6214 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc
);
6218 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
6219 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
6222 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, desc
->shader
));
6223 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
6224 priv
->current_fprogram_id
= desc
->shader
;
6226 if(device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
6227 /* Reload fixed function constants since they collide with the pixel shader constants */
6228 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
6229 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
6231 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
6232 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
6234 context
->last_was_pshader
= FALSE
;
6236 context
->last_was_pshader
= TRUE
;
6239 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
6240 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
6241 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
6242 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
6243 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
6245 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
6248 if(!isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
6249 device
->shader_backend
->shader_select(context
, use_pshader
, use_vshader
);
6251 if (!isStateDirty(context
, STATE_VERTEXSHADERCONSTANT
) && (use_vshader
|| use_pshader
))
6252 stateblock_apply_state(STATE_VERTEXSHADERCONSTANT
, stateblock
, context
);
6254 if (use_pshader
) stateblock_apply_state(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
6257 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
6258 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
6259 * state table, so we need to handle that with a forwarding function. The other invisible side effect
6260 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
6261 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
6263 static void state_arbfp_fog(DWORD state_id
, IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
6265 const struct wined3d_state
*state
= &stateblock
->state
;
6266 enum fogsource new_source
;
6268 TRACE("state_id %#x, stateblock %p, context %p\n", state_id
, stateblock
, context
);
6270 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
6271 fragment_prog_arbfp(state_id
, stateblock
, context
);
6274 if (!state
->render_states
[WINED3DRS_FOGENABLE
]) return;
6276 if (state
->render_states
[WINED3DRS_FOGTABLEMODE
] == WINED3DFOG_NONE
)
6280 new_source
= FOGSOURCE_VS
;
6284 if (state
->render_states
[WINED3DRS_FOGVERTEXMODE
] == WINED3DFOG_NONE
|| context
->last_was_rhw
)
6285 new_source
= FOGSOURCE_COORD
;
6287 new_source
= FOGSOURCE_FFP
;
6290 new_source
= FOGSOURCE_FFP
;
6292 if(new_source
!= context
->fog_source
) {
6293 context
->fog_source
= new_source
;
6294 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART
), stateblock
, context
);
6298 static void textransform(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, struct wined3d_context
*context
)
6300 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
6301 fragment_prog_arbfp(state
, stateblock
, context
);
6305 static const struct StateEntryTemplate arbfp_fragmentstate_template
[] = {
6306 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), state_texfactor_arbfp
}, WINED3D_GL_EXT_NONE
},
6307 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6308 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6309 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6310 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6311 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6312 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6313 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6314 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6315 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6316 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6317 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6318 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6319 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6320 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6321 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6322 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6323 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6324 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6325 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6326 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6327 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6328 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6329 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6330 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6331 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6332 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6333 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6334 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6335 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6336 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6337 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6338 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6339 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6340 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6341 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6342 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6343 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6344 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6345 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6346 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6347 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6348 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6349 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6350 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6351 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6352 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6353 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6354 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6355 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6356 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6357 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6358 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6359 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6360 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6361 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6362 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6363 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6364 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6365 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6366 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6367 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6368 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6369 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6370 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6371 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6372 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6373 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6374 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6375 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6376 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6377 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6378 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6379 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6380 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6381 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6382 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6383 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6384 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6385 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6386 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6387 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6388 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6389 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6390 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6391 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6392 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6393 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6394 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6395 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6396 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6397 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6398 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6399 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6400 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6401 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6402 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6403 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6404 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6405 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6406 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6407 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6408 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6409 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6410 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6411 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6412 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6413 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6414 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6415 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6416 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6417 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6418 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6419 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6420 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6421 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
6422 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6423 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6424 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), NULL
}, WINED3D_GL_EXT_NONE
},
6425 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
6426 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), NULL
}, WINED3D_GL_EXT_NONE
},
6427 {STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6428 {STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6429 {STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6430 {STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6431 {STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6432 {STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6433 {STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6434 {STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
6435 {STATE_PIXELSHADER
, { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
6436 {STATE_RENDER(WINED3DRS_FOGENABLE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, WINED3D_GL_EXT_NONE
},
6437 {STATE_RENDER(WINED3DRS_FOGTABLEMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
6438 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
6439 {STATE_RENDER(WINED3DRS_FOGSTART
), { STATE_RENDER(WINED3DRS_FOGSTART
), state_fogstartend
}, WINED3D_GL_EXT_NONE
},
6440 {STATE_RENDER(WINED3DRS_FOGEND
), { STATE_RENDER(WINED3DRS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
6441 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), { STATE_PIXELSHADER
, NULL
}, WINED3D_GL_EXT_NONE
},
6442 {STATE_RENDER(WINED3DRS_FOGCOLOR
), { STATE_RENDER(WINED3DRS_FOGCOLOR
), state_fogcolor
}, WINED3D_GL_EXT_NONE
},
6443 {STATE_RENDER(WINED3DRS_FOGDENSITY
), { STATE_RENDER(WINED3DRS_FOGDENSITY
), state_fogdensity
}, WINED3D_GL_EXT_NONE
},
6444 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6445 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6446 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6447 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6448 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6449 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6450 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6451 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
6452 {STATE_RENDER(WINED3DRS_SPECULARENABLE
), { STATE_RENDER(WINED3DRS_SPECULARENABLE
), state_arb_specularenable
}, WINED3D_GL_EXT_NONE
},
6453 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE
},
6456 const struct fragment_pipeline arbfp_fragment_pipeline
= {
6461 shader_arb_color_fixup_supported
,
6462 arbfp_fragmentstate_template
,
6463 TRUE
/* We can disable projected textures */
6466 struct arbfp_blit_priv
{
6467 GLenum yuy2_rect_shader
, yuy2_2d_shader
;
6468 GLenum uyvy_rect_shader
, uyvy_2d_shader
;
6469 GLenum yv12_rect_shader
, yv12_2d_shader
;
6470 GLenum p8_rect_shader
, p8_2d_shader
;
6471 GLuint palette_texture
;
6474 static HRESULT
arbfp_blit_alloc(IWineD3DDevice
*iface
) {
6475 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
6476 device
->blit_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct arbfp_blit_priv
));
6477 if(!device
->blit_priv
) {
6478 ERR("Out of memory\n");
6479 return E_OUTOFMEMORY
;
6484 /* Context activation is done by the caller. */
6485 static void arbfp_blit_free(IWineD3DDevice
*iface
) {
6486 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
6487 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6488 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6491 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_rect_shader
));
6492 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_2d_shader
));
6493 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_rect_shader
));
6494 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_2d_shader
));
6495 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_rect_shader
));
6496 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_2d_shader
));
6497 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->p8_rect_shader
));
6498 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->p8_2d_shader
));
6499 checkGLcall("Delete yuv and p8 programs");
6501 if(priv
->palette_texture
) glDeleteTextures(1, &priv
->palette_texture
);
6504 HeapFree(GetProcessHeap(), 0, device
->blit_priv
);
6505 device
->blit_priv
= NULL
;
6508 static BOOL
gen_planar_yuv_read(struct wined3d_shader_buffer
*buffer
, enum complex_fixup fixup
,
6509 GLenum textype
, char *luminance
)
6512 const char *tex
, *texinstr
;
6514 if (fixup
== COMPLEX_FIXUP_UYVY
) {
6522 case GL_TEXTURE_2D
: tex
= "2D"; texinstr
= "TXP"; break;
6523 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; texinstr
= "TEX"; break;
6525 /* This is more tricky than just replacing the texture type - we have to navigate
6526 * properly in the texture to find the correct chroma values
6528 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
6532 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
6533 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
6534 * filtering when we sample the texture.
6536 * These are the rules for reading the chroma:
6542 * So we have to get the sampling x position in non-normalized coordinates in integers
6544 if(textype
!= GL_TEXTURE_RECTANGLE_ARB
) {
6545 shader_addline(buffer
, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
6546 shader_addline(buffer
, "MOV texcrd.w, size.x;\n");
6548 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
6550 /* We must not allow filtering between pixel x and x+1, this would mix U and V
6551 * Vertical filtering is ok. However, bear in mind that the pixel center is at
6554 shader_addline(buffer
, "FLR texcrd.x, texcrd.x;\n");
6555 shader_addline(buffer
, "ADD texcrd.x, texcrd.x, coef.y;\n");
6557 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
6558 * even and odd pixels respectively
6560 shader_addline(buffer
, "MUL texcrd2, texcrd, coef.y;\n");
6561 shader_addline(buffer
, "FRC texcrd2, texcrd2;\n");
6563 /* Sample Pixel 1 */
6564 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
6566 /* Put the value into either of the chroma values */
6567 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
6568 shader_addline(buffer
, "MUL chroma.x, luminance.%c, temp.x;\n", chroma
);
6569 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
6570 shader_addline(buffer
, "MUL chroma.y, luminance.%c, temp.x;\n", chroma
);
6572 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
6573 * the pixel right to the current one. Otherwise, sample the left pixel.
6574 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
6576 shader_addline(buffer
, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
6577 shader_addline(buffer
, "ADD texcrd.x, texcrd, temp.x;\n");
6578 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
6580 /* Put the value into the other chroma */
6581 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
6582 shader_addline(buffer
, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma
);
6583 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
6584 shader_addline(buffer
, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma
);
6586 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
6587 * the current one and lerp the two U and V values
6590 /* This gives the correctly filtered luminance value */
6591 shader_addline(buffer
, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex
);
6596 static BOOL
gen_yv12_read(struct wined3d_shader_buffer
*buffer
, GLenum textype
, char *luminance
)
6601 case GL_TEXTURE_2D
: tex
= "2D"; break;
6602 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; break;
6604 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
6608 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
6609 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
6610 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
6611 * pitch of the luminance plane, the packing into the gl texture is a bit
6612 * unfortunate. If the whole texture is interpreted as luminance data it looks
6613 * approximately like this:
6615 * +----------------------------------+----
6627 * +----------------+-----------------+----
6629 * | U even rows | U odd rows |
6631 * +----------------+------------------ -
6633 * | V even rows | V odd rows |
6635 * +----------------+-----------------+----
6639 * So it appears as if there are 4 chroma images, but in fact the odd rows
6640 * in the chroma images are in the same row as the even ones. So its is
6641 * kinda tricky to read
6643 * When reading from rectangle textures, keep in mind that the input y coordinates
6644 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
6646 shader_addline(buffer
, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
6647 2.0f
/ 3.0f
, 1.0f
/ 6.0f
, (2.0f
/ 3.0f
) + (1.0f
/ 6.0f
), 1.0f
/ 3.0f
);
6649 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
6650 /* the chroma planes have only half the width */
6651 shader_addline(buffer
, "MUL texcrd.x, texcrd.x, coef.y;\n");
6653 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
6654 * the coordinate. Also read the right side of the image when reading odd lines
6656 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
6659 if(textype
== GL_TEXTURE_2D
) {
6661 shader_addline(buffer
, "RCP chroma.w, size.y;\n");
6663 shader_addline(buffer
, "MUL texcrd2.y, texcrd.y, size.y;\n");
6665 shader_addline(buffer
, "FLR texcrd2.y, texcrd2.y;\n");
6666 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
6668 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6669 shader_addline(buffer
, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6670 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
6671 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6672 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6674 /* clamp, keep the half pixel origin in mind */
6675 shader_addline(buffer
, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
6676 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
6677 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
6678 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
6680 /* Read from [size - size+size/4] */
6681 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
6682 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
6684 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6685 shader_addline(buffer
, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6686 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
6687 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6688 shader_addline(buffer
, "MUL texcrd2.x, texcrd2.x, size.x;\n");
6689 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6691 /* Make sure to read exactly from the pixel center */
6692 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
6693 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, coef.y;\n");
6696 shader_addline(buffer
, "MAD temp.y, size.y, coef.w, size.y;\n");
6697 shader_addline(buffer
, "ADD temp.y, temp.y, -coef.y;\n");
6698 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
6699 shader_addline(buffer
, "ADD temp.y, size.y, -coef.y;\n");
6700 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
6702 /* Read the texture, put the result into the output register */
6703 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
6704 shader_addline(buffer
, "MOV chroma.x, temp.w;\n");
6706 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
6707 * No need to clamp because we're just reusing the already clamped value from above
6709 if(textype
== GL_TEXTURE_2D
) {
6710 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
6712 shader_addline(buffer
, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
6714 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
6715 shader_addline(buffer
, "MOV chroma.y, temp.w;\n");
6717 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
6718 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
6719 * values due to filtering
6721 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
6722 if(textype
== GL_TEXTURE_2D
) {
6723 /* Multiply the y coordinate by 2/3 and clamp it */
6724 shader_addline(buffer
, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
6725 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
6726 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
6727 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
6729 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
6730 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
6733 shader_addline(buffer
, "ADD temp.x, size.y, -coef.y;\n");
6734 shader_addline(buffer
, "MIN texcrd.y, texcrd.y, size.x;\n");
6735 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
6742 static GLuint
gen_p8_shader(IWineD3DDeviceImpl
*device
, GLenum textype
)
6744 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6746 struct wined3d_shader_buffer buffer
;
6747 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6751 if (!shader_buffer_init(&buffer
))
6753 ERR("Failed to initialize shader buffer.\n");
6758 GL_EXTCALL(glGenProgramsARB(1, &shader
));
6759 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
6762 shader_buffer_free(&buffer
);
6766 shader_addline(&buffer
, "!!ARBfp1.0\n");
6767 shader_addline(&buffer
, "TEMP index;\n");
6769 /* { 255/256, 0.5/255*255/256, 0, 0 } */
6770 shader_addline(&buffer
, "PARAM constants = { 0.996, 0.00195, 0, 0 };\n");
6772 /* The alpha-component contains the palette index */
6773 if(textype
== GL_TEXTURE_RECTANGLE_ARB
)
6774 shader_addline(&buffer
, "TXP index, fragment.texcoord[0], texture[0], RECT;\n");
6776 shader_addline(&buffer
, "TEX index, fragment.texcoord[0], texture[0], 2D;\n");
6778 /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
6779 shader_addline(&buffer
, "MAD index.a, index.a, constants.x, constants.y;\n");
6781 /* Use the alpha-component as an index in the palette to get the final color */
6782 shader_addline(&buffer
, "TEX result.color, index.a, texture[1], 1D;\n");
6783 shader_addline(&buffer
, "END\n");
6786 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
6787 strlen(buffer
.buffer
), buffer
.buffer
));
6788 checkGLcall("glProgramStringARB()");
6790 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
6793 FIXME("Fragment program error at position %d: %s\n\n", pos
,
6794 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
6795 shader_arb_dump_program_source(buffer
.buffer
);
6798 if (textype
== GL_TEXTURE_RECTANGLE_ARB
)
6799 priv
->p8_rect_shader
= shader
;
6801 priv
->p8_2d_shader
= shader
;
6803 shader_buffer_free(&buffer
);
6809 /* Context activation is done by the caller. */
6810 static void upload_palette(IWineD3DSurfaceImpl
*surface
)
6813 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
6814 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6815 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6816 BOOL colorkey
= (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
) ? TRUE
: FALSE
;
6818 d3dfmt_p8_init_palette(surface
, table
, colorkey
);
6821 if (!priv
->palette_texture
)
6822 glGenTextures(1, &priv
->palette_texture
);
6824 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1
));
6825 glBindTexture(GL_TEXTURE_1D
, priv
->palette_texture
);
6827 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
6829 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
6830 /* Make sure we have discrete color levels. */
6831 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
6832 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
6833 /* Upload the palette */
6834 /* TODO: avoid unneeed uploads in the future by adding some SFLAG_PALETTE_DIRTY mechanism */
6835 glTexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA
, 256, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, table
);
6837 /* Switch back to unit 0 in which the 2D texture will be stored. */
6838 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
));
6842 /* Context activation is done by the caller. */
6843 static GLuint
gen_yuv_shader(IWineD3DDeviceImpl
*device
, enum complex_fixup yuv_fixup
, GLenum textype
)
6845 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
6847 struct wined3d_shader_buffer buffer
;
6848 char luminance_component
;
6849 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6853 if (!shader_buffer_init(&buffer
))
6855 ERR("Failed to initialize shader buffer.\n");
6860 GL_EXTCALL(glGenProgramsARB(1, &shader
));
6861 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
6862 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
6863 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6866 shader_buffer_free(&buffer
);
6870 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
6871 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
6872 * two chroma(U and V) values. Each macropixel has two luminance values, one for
6873 * each single pixel it contains, and one U and one V value shared between both
6876 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
6877 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
6878 * take the format into account when generating the read swizzles
6880 * Reading the Y value is straightforward - just sample the texture. The hardware
6881 * takes care of filtering in the horizontal and vertical direction.
6883 * Reading the U and V values is harder. We have to avoid filtering horizontally,
6884 * because that would mix the U and V values of one pixel or two adjacent pixels.
6885 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
6886 * regardless of the filtering setting. Vertical filtering works automatically
6887 * though - the U and V values of two rows are mixed nicely.
6889 * Appart of avoiding filtering issues, the code has to know which value it just
6890 * read, and where it can find the other one. To determine this, it checks if
6891 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
6893 * Handling horizontal filtering of U and V values requires reading a 2nd pair
6894 * of pixels, extracting U and V and mixing them. This is not implemented yet.
6896 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
6897 * with width / 2. This way one read gives all 3 values, finding U and V is easy
6898 * in an unfiltered situation. Finding the luminance on the other hand requires
6899 * finding out if it is an odd or even pixel. The real drawback of this approach
6900 * is filtering. This would have to be emulated completely in the shader, reading
6901 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
6902 * vertically. Beyond that it would require adjustments to the texture handling
6903 * code to deal with the width scaling
6905 shader_addline(&buffer
, "!!ARBfp1.0\n");
6906 shader_addline(&buffer
, "TEMP luminance;\n");
6907 shader_addline(&buffer
, "TEMP temp;\n");
6908 shader_addline(&buffer
, "TEMP chroma;\n");
6909 shader_addline(&buffer
, "TEMP texcrd;\n");
6910 shader_addline(&buffer
, "TEMP texcrd2;\n");
6911 shader_addline(&buffer
, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
6912 shader_addline(&buffer
, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
6913 shader_addline(&buffer
, "PARAM size = program.local[0];\n");
6917 case COMPLEX_FIXUP_UYVY
:
6918 case COMPLEX_FIXUP_YUY2
:
6919 if (!gen_planar_yuv_read(&buffer
, yuv_fixup
, textype
, &luminance_component
))
6921 shader_buffer_free(&buffer
);
6926 case COMPLEX_FIXUP_YV12
:
6927 if (!gen_yv12_read(&buffer
, textype
, &luminance_component
))
6929 shader_buffer_free(&buffer
);
6935 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup
);
6936 shader_buffer_free(&buffer
);
6940 /* Calculate the final result. Formula is taken from
6941 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
6942 * ranges from -0.5 to 0.5
6944 shader_addline(&buffer
, "SUB chroma.xy, chroma, coef.y;\n");
6946 shader_addline(&buffer
, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component
);
6947 shader_addline(&buffer
, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component
);
6948 shader_addline(&buffer
, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
6949 shader_addline(&buffer
, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component
);
6950 shader_addline(&buffer
, "END\n");
6953 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
6954 strlen(buffer
.buffer
), buffer
.buffer
));
6955 checkGLcall("glProgramStringARB()");
6957 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
6960 FIXME("Fragment program error at position %d: %s\n\n", pos
,
6961 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
6962 shader_arb_dump_program_source(buffer
.buffer
);
6968 GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB
, &native
));
6969 checkGLcall("glGetProgramivARB()");
6970 if (!native
) WARN("Program exceeds native resource limits.\n");
6973 shader_buffer_free(&buffer
);
6978 case COMPLEX_FIXUP_YUY2
:
6979 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->yuy2_rect_shader
= shader
;
6980 else priv
->yuy2_2d_shader
= shader
;
6983 case COMPLEX_FIXUP_UYVY
:
6984 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->uyvy_rect_shader
= shader
;
6985 else priv
->uyvy_2d_shader
= shader
;
6988 case COMPLEX_FIXUP_YV12
:
6989 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->yv12_rect_shader
= shader
;
6990 else priv
->yv12_2d_shader
= shader
;
6993 ERR("Unsupported complex fixup: %d\n", yuv_fixup
);
6999 /* Context activation is done by the caller. */
7000 static HRESULT
arbfp_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
7003 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
7004 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7005 float size
[4] = {(float) surface
->pow2Width
, (float) surface
->pow2Height
, 1.0f
, 1.0f
};
7006 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
7007 enum complex_fixup fixup
;
7008 GLenum textype
= surface
->texture_target
;
7010 if (!is_complex_fixup(surface
->resource
.format
->color_fixup
))
7013 dump_color_fixup_desc(surface
->resource
.format
->color_fixup
);
7014 /* Don't bother setting up a shader for unconverted formats */
7017 checkGLcall("glEnable(textype)");
7022 fixup
= get_complex_fixup(surface
->resource
.format
->color_fixup
);
7026 case COMPLEX_FIXUP_YUY2
:
7027 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->yuy2_rect_shader
: priv
->yuy2_2d_shader
;
7030 case COMPLEX_FIXUP_UYVY
:
7031 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->uyvy_rect_shader
: priv
->uyvy_2d_shader
;
7034 case COMPLEX_FIXUP_YV12
:
7035 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->yv12_rect_shader
: priv
->yv12_2d_shader
;
7038 case COMPLEX_FIXUP_P8
:
7039 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->p8_rect_shader
: priv
->p8_2d_shader
;
7040 if (!shader
) shader
= gen_p8_shader(device
, textype
);
7042 upload_palette(surface
);
7046 FIXME("Unsupported complex fixup %#x, not setting a shader\n", fixup
);
7049 checkGLcall("glEnable(textype)");
7054 if (!shader
) shader
= gen_yuv_shader(device
, fixup
, textype
);
7057 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
7058 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
7059 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
7060 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
7061 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0, size
));
7062 checkGLcall("glProgramLocalParameter4fvARB");
7068 /* Context activation is done by the caller. */
7069 static void arbfp_blit_unset(IWineD3DDevice
*iface
) {
7070 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
7071 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7074 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
7075 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
7076 glDisable(GL_TEXTURE_2D
);
7077 checkGLcall("glDisable(GL_TEXTURE_2D)");
7078 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
7080 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
7081 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
7083 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
7085 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
7086 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
7091 static BOOL
arbfp_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
7092 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
, const struct wined3d_format
*src_format
,
7093 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
, const struct wined3d_format
*dst_format
)
7095 enum complex_fixup src_fixup
;
7097 if (blit_op
!= BLIT_OP_BLIT
)
7099 TRACE("Unsupported blit_op=%d\n", blit_op
);
7103 src_fixup
= get_complex_fixup(src_format
->color_fixup
);
7104 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
7106 TRACE("Checking support for fixup:\n");
7107 dump_color_fixup_desc(src_format
->color_fixup
);
7110 if (!is_identity_fixup(dst_format
->color_fixup
))
7112 TRACE("Destination fixups are not supported\n");
7116 if (is_identity_fixup(src_format
->color_fixup
))
7122 /* We only support YUV conversions. */
7123 if (!is_complex_fixup(src_format
->color_fixup
))
7125 TRACE("[FAILED]\n");
7131 case COMPLEX_FIXUP_YUY2
:
7132 case COMPLEX_FIXUP_UYVY
:
7133 case COMPLEX_FIXUP_YV12
:
7134 case COMPLEX_FIXUP_P8
:
7139 FIXME("Unsupported YUV fixup %#x\n", src_fixup
);
7140 TRACE("[FAILED]\n");
7145 HRESULT
arbfp_blit_surface(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*src_surface
, const RECT
*src_rect
,
7146 IWineD3DSurfaceImpl
*dst_surface
, const RECT
*dst_rect_in
, enum blit_operation blit_op
,
7149 IWineD3DSwapChainImpl
*dst_swapchain
;
7150 struct wined3d_context
*context
;
7151 RECT dst_rect
= *dst_rect_in
;
7153 /* Now load the surface */
7154 surface_internal_preload(src_surface
, SRGB_RGB
);
7156 /* Activate the destination context, set it up for blitting */
7157 context
= context_acquire(device
, dst_surface
);
7158 context_apply_blit_state(context
, device
);
7160 /* The coordinates of the ddraw front buffer are always fullscreen ('screen coordinates',
7161 * while OpenGL coordinates are window relative.
7162 * Also beware of the origin difference(top left vs bottom left).
7163 * Also beware that the front buffer's surface size is screen width x screen height,
7164 * whereas the real gl drawable size is the size of the window. */
7165 dst_swapchain
= dst_surface
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
7166 ? dst_surface
->container
.u
.swapchain
: NULL
;
7167 if (dst_swapchain
&& dst_surface
== dst_swapchain
->front_buffer
)
7168 surface_translate_frontbuffer_coords(dst_surface
, context
->win_handle
, &dst_rect
);
7169 else if (surface_is_offscreen(dst_surface
))
7171 dst_rect
.top
= dst_surface
->currentDesc
.Height
- dst_rect
.top
;
7172 dst_rect
.bottom
= dst_surface
->currentDesc
.Height
- dst_rect
.bottom
;
7175 arbfp_blit_set((IWineD3DDevice
*)device
, src_surface
);
7179 /* Draw a textured quad */
7180 draw_textured_quad(src_surface
, src_rect
, &dst_rect
, Filter
);
7184 /* Leave the opengl state valid for blitting */
7185 arbfp_blit_unset((IWineD3DDevice
*)device
);
7187 if (wined3d_settings
.strict_draw_ordering
|| (dst_swapchain
7188 && (dst_surface
== dst_swapchain
->front_buffer
7189 || dst_swapchain
->num_contexts
> 1)))
7190 wglFlush(); /* Flush to ensure ordering across contexts. */
7192 context_release(context
);
7194 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
7198 /* Do not call while under the GL lock. */
7199 static HRESULT
arbfp_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
,
7200 const RECT
*dst_rect
, const WINED3DCOLORVALUE
*color
)
7202 FIXME("Color filling not implemented by arbfp_blit\n");
7203 return WINED3DERR_INVALIDCALL
;
7206 const struct blit_shader arbfp_blit
= {
7211 arbfp_blit_supported
,
7212 arbfp_blit_color_fill