2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
12 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
13 * Copyright 2009 Henri Verbeet for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants
);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps
);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
42 #define GLINFO_LOCATION (*gl_info)
44 /* GL locking for state handlers is done by the caller. */
45 static BOOL
need_mova_const(IWineD3DBaseShader
*shader
, const WineD3D_GL_Info
*gl_info
) {
46 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) shader
;
47 if(!This
->baseShader
.reg_maps
.usesmova
) return FALSE
;
48 return !GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION
);
51 static BOOL
need_helper_const(const WineD3D_GL_Info
*gl_info
) {
52 if(!GL_SUPPORT(NV_VERTEX_PROGRAM
) || /* Need to init colors */
53 gl_info
->arb_vs_offset_limit
|| /* Have to init texcoords */
54 gl_info
->set_texcoord_w
) { /* Load the immval offset */
60 static unsigned int reserved_vs_const(IWineD3DBaseShader
*shader
, const WineD3D_GL_Info
*gl_info
) {
62 /* We use one PARAM for the pos fixup, and in some cases one to load
63 * some immediate values into the shader
65 if(need_helper_const(gl_info
)) ret
++;
66 if(need_mova_const(shader
, gl_info
)) ret
++;
70 static inline BOOL
ffp_clip_emul(IWineD3DStateBlockImpl
*stateblock
)
72 return stateblock
->lowest_disabled_stage
< 7;
75 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
76 * so upload them above that
78 #define ARB_SHADER_PRIVCONST_BASE (GL_LIMITS(vshader_constantsF) - 1)
79 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
81 /* ARB_program_shader private data */
99 DWORD loop_control
[3];
103 struct arb_ps_compile_args
105 struct ps_compile_args super
;
106 DWORD bools
; /* WORD is enough, use DWORD for alignment */
107 unsigned char loop_ctrl
[MAX_CONST_I
][3];
110 struct stb_const_desc
112 unsigned char texunit
;
116 struct arb_ps_compiled_shader
118 struct arb_ps_compile_args args
;
120 struct stb_const_desc bumpenvmatconst
[MAX_TEXTURES
];
121 unsigned char numbumpenvmatconsts
;
122 struct stb_const_desc luminanceconst
[MAX_TEXTURES
];
123 UINT int_consts
[MAX_CONST_I
];
128 struct arb_vs_compile_args
130 struct vs_compile_args super
;
136 char clip_control
[2];
138 DWORD boolclip_compare
;
143 unsigned char vertex_samplers
[4];
144 DWORD vertex_samplers_compare
;
146 unsigned char loop_ctrl
[MAX_CONST_I
][3];
149 struct arb_vs_compiled_shader
151 struct arb_vs_compile_args args
;
153 UINT int_consts
[MAX_CONST_I
];
157 struct recorded_instruction
159 struct wined3d_shader_instruction ins
;
163 struct shader_arb_ctx_priv
168 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
170 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
172 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
176 const struct arb_vs_compile_args
*cur_vs_args
;
177 const struct arb_ps_compile_args
*cur_ps_args
;
178 const struct arb_ps_compiled_shader
*compiled_fprog
;
179 const struct arb_vs_compiled_shader
*compiled_vprog
;
180 struct list control_frames
;
184 unsigned int num_loops
, loop_depth
, num_ifcs
;
187 /* For 3.0 vertex shaders */
188 const char *vs_output
[MAX_REG_OUTPUT
];
189 /* For 2.x and earlier vertex shaders */
190 const char *texcrd_output
[8], *color_output
[2], *fog_output
;
192 /* 3.0 pshader input for compatibility with fixed function */
193 const char *ps_input
[MAX_REG_INPUT
];
198 struct wined3d_shader_signature_element
*sig
;
200 struct wine_rb_entry entry
;
203 struct arb_pshader_private
{
204 struct arb_ps_compiled_shader
*gl_shaders
;
205 UINT num_gl_shaders
, shader_array_size
;
206 BOOL has_signature_idx
;
207 DWORD input_signature_idx
;
208 DWORD clipplane_emulation
;
211 struct arb_vshader_private
{
212 struct arb_vs_compiled_shader
*gl_shaders
;
213 UINT num_gl_shaders
, shader_array_size
;
216 struct shader_arb_priv
218 GLuint current_vprogram_id
;
219 GLuint current_fprogram_id
;
220 const struct arb_ps_compiled_shader
*compiled_fprog
;
221 const struct arb_vs_compiled_shader
*compiled_vprog
;
222 GLuint depth_blt_vprogram_id
;
223 GLuint depth_blt_fprogram_id
[tex_type_count
];
224 BOOL use_arbfp_fixed_func
;
225 struct wine_rb_tree fragment_shaders
;
227 struct wine_rb_tree signature_tree
;
231 /********************************************************
232 * ARB_[vertex/fragment]_program helper functions follow
233 ********************************************************/
236 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
237 * When constant_list == NULL, it will load all the constants.
239 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
240 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
242 /* GL locking is done by the caller */
243 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl
* This
, const WineD3D_GL_Info
*gl_info
,
244 GLuint target_type
, unsigned int max_constants
, const float *constants
, char *dirty_consts
)
246 local_constant
* lconst
;
250 if (TRACE_ON(d3d_shader
)) {
251 for(i
= 0; i
< max_constants
; i
++) {
252 if(!dirty_consts
[i
]) continue;
253 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
254 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
255 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
258 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
259 if (target_type
== GL_FRAGMENT_PROGRAM_ARB
&& This
->baseShader
.reg_maps
.shader_version
.major
== 1)
262 for(i
= 0; i
< max_constants
; i
++) {
263 if(!dirty_consts
[i
]) continue;
267 if(constants
[j
+ 0] > 1.0) lcl_const
[0] = 1.0;
268 else if(constants
[j
+ 0] < -1.0) lcl_const
[0] = -1.0;
269 else lcl_const
[0] = constants
[j
+ 0];
271 if(constants
[j
+ 1] > 1.0) lcl_const
[1] = 1.0;
272 else if(constants
[j
+ 1] < -1.0) lcl_const
[1] = -1.0;
273 else lcl_const
[1] = constants
[j
+ 1];
275 if(constants
[j
+ 2] > 1.0) lcl_const
[2] = 1.0;
276 else if(constants
[j
+ 2] < -1.0) lcl_const
[2] = -1.0;
277 else lcl_const
[2] = constants
[j
+ 2];
279 if(constants
[j
+ 3] > 1.0) lcl_const
[3] = 1.0;
280 else if(constants
[j
+ 3] < -1.0) lcl_const
[3] = -1.0;
281 else lcl_const
[3] = constants
[j
+ 3];
283 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, lcl_const
));
286 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS
)) {
287 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
288 * or just reloading *all* constants at once
290 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
292 for(i
= 0; i
< max_constants
; i
++) {
293 if(!dirty_consts
[i
]) continue;
295 /* Find the next block of dirty constants */
298 for(i
++; (i
< max_constants
) && dirty_consts
[i
]; i
++) {
302 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type
, j
, i
- j
, constants
+ (j
* 4)));
305 for(i
= 0; i
< max_constants
; i
++) {
306 if(dirty_consts
[i
]) {
308 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, i
, constants
+ (i
* 4)));
313 checkGLcall("glProgramEnvParameter4fvARB()");
315 /* Load immediate constants */
316 if(This
->baseShader
.load_local_constsF
) {
317 if (TRACE_ON(d3d_shader
)) {
318 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
319 GLfloat
* values
= (GLfloat
*)lconst
->value
;
320 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
321 values
[0], values
[1], values
[2], values
[3]);
324 /* Immediate constants are clamped for 1.X shaders at loading times */
326 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
327 dirty_consts
[lconst
->idx
] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
328 ret
= max(ret
, lconst
->idx
+ 1);
329 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type
, lconst
->idx
, (GLfloat
*)lconst
->value
));
331 checkGLcall("glProgramEnvParameter4fvARB()");
332 return ret
; /* The loaded immediate constants need reloading for the next shader */
334 return 0; /* No constants are dirty now */
339 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
341 static void shader_arb_load_np2fixup_constants(
342 IWineD3DDevice
* device
,
344 char useVertexShader
) {
345 /* not implemented */
348 static inline void shader_arb_ps_local_constants(IWineD3DDeviceImpl
* deviceImpl
)
350 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
351 const WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
353 struct shader_arb_priv
*priv
= deviceImpl
->shader_priv
;
354 const struct arb_ps_compiled_shader
*gl_shader
= priv
->compiled_fprog
;
356 for(i
= 0; i
< gl_shader
->numbumpenvmatconsts
; i
++)
358 int texunit
= gl_shader
->bumpenvmatconst
[i
].texunit
;
360 /* The state manager takes care that this function is always called if the bump env matrix changes */
361 const float *data
= (const float *)&stateBlock
->textureState
[texunit
][WINED3DTSS_BUMPENVMAT00
];
362 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->bumpenvmatconst
[i
].const_num
, data
));
364 if (gl_shader
->luminanceconst
[i
].const_num
!= WINED3D_CONST_NUM_UNUSED
)
366 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
367 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
368 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
369 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
371 const float *scale
= (const float *)&stateBlock
->textureState
[texunit
][WINED3DTSS_BUMPENVLSCALE
];
372 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->luminanceconst
[i
].const_num
, scale
));
375 checkGLcall("Load bumpmap consts\n");
377 if(gl_shader
->ycorrection
!= WINED3D_CONST_NUM_UNUSED
)
379 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
380 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
385 val
[0] = deviceImpl
->render_offscreen
? 0.0 : ((IWineD3DSurfaceImpl
*) deviceImpl
->render_targets
[0])->currentDesc
.Height
;
386 val
[1] = deviceImpl
->render_offscreen
? 1.0 : -1.0;
389 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->ycorrection
, val
));
390 checkGLcall("y correction loading\n");
393 if(gl_shader
->num_int_consts
== 0) return;
395 for(i
= 0; i
< MAX_CONST_I
; i
++)
397 if(gl_shader
->int_consts
[i
] != WINED3D_CONST_NUM_UNUSED
)
400 val
[0] = stateBlock
->pixelShaderConstantI
[4 * i
];
401 val
[1] = stateBlock
->pixelShaderConstantI
[4 * i
+ 1];
402 val
[2] = stateBlock
->pixelShaderConstantI
[4 * i
+ 2];
405 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, gl_shader
->int_consts
[i
], val
));
408 checkGLcall("Load ps int consts\n");
411 static inline void shader_arb_vs_local_constants(IWineD3DDeviceImpl
* deviceImpl
)
413 IWineD3DStateBlockImpl
* stateBlock
;
414 const WineD3D_GL_Info
*gl_info
;
416 struct shader_arb_priv
*priv
= deviceImpl
->shader_priv
;
417 const struct arb_vs_compiled_shader
*gl_shader
= priv
->compiled_vprog
;
419 if(gl_shader
->num_int_consts
== 0) return;
421 gl_info
= &deviceImpl
->adapter
->gl_info
;
422 stateBlock
= deviceImpl
->stateBlock
;
424 for(i
= 0; i
< MAX_CONST_I
; i
++)
426 if(gl_shader
->int_consts
[i
] != WINED3D_CONST_NUM_UNUSED
)
429 val
[0] = stateBlock
->vertexShaderConstantI
[4 * i
];
430 val
[1] = stateBlock
->vertexShaderConstantI
[4 * i
+ 1];
431 val
[2] = stateBlock
->vertexShaderConstantI
[4 * i
+ 2];
434 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, gl_shader
->int_consts
[i
], val
));
437 checkGLcall("Load vs int consts\n");
441 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
443 * We only support float constants in ARB at the moment, so don't
444 * worry about the Integers or Booleans
446 /* GL locking is done by the caller (state handler) */
447 static void shader_arb_load_constants(
448 IWineD3DDevice
* device
,
450 char useVertexShader
) {
452 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
453 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
454 const WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
456 if (useVertexShader
) {
457 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
459 /* Load DirectX 9 float constants for vertex shader */
460 deviceImpl
->highest_dirty_vs_const
= shader_arb_load_constantsF(
461 vshader
, gl_info
, GL_VERTEX_PROGRAM_ARB
,
462 deviceImpl
->highest_dirty_vs_const
,
463 stateBlock
->vertexShaderConstantF
,
464 deviceImpl
->activeContext
->vshader_const_dirty
);
466 /* Upload the position fixup */
467 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, ARB_SHADER_PRIVCONST_POS
, deviceImpl
->posFixup
));
469 shader_arb_vs_local_constants(deviceImpl
);
472 if (usePixelShader
) {
473 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
475 /* Load DirectX 9 float constants for pixel shader */
476 deviceImpl
->highest_dirty_ps_const
= shader_arb_load_constantsF(
477 pshader
, gl_info
, GL_FRAGMENT_PROGRAM_ARB
,
478 deviceImpl
->highest_dirty_ps_const
,
479 stateBlock
->pixelShaderConstantF
,
480 deviceImpl
->activeContext
->pshader_const_dirty
);
481 shader_arb_ps_local_constants(deviceImpl
);
485 static void shader_arb_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
487 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
489 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
490 * context. On a context switch the old context will be fully dirtified */
491 memset(This
->activeContext
->vshader_const_dirty
+ start
, 1,
492 sizeof(*This
->activeContext
->vshader_const_dirty
) * count
);
493 This
->highest_dirty_vs_const
= max(This
->highest_dirty_vs_const
, start
+ count
+ 1);
496 static void shader_arb_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
498 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
500 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
501 * context. On a context switch the old context will be fully dirtified */
502 memset(This
->activeContext
->pshader_const_dirty
+ start
, 1,
503 sizeof(*This
->activeContext
->pshader_const_dirty
) * count
);
504 This
->highest_dirty_ps_const
= max(This
->highest_dirty_ps_const
, start
+ count
+ 1);
507 static DWORD
*local_const_mapping(IWineD3DBaseShaderImpl
*This
)
511 const local_constant
*lconst
;
513 if(This
->baseShader
.load_local_constsF
|| list_empty(&This
->baseShader
.constantsF
)) return NULL
;
515 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
) * This
->baseShader
.limits
.constant_float
);
517 ERR("Out of memory\n");
521 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
522 ret
[lconst
->idx
] = idx
++;
527 /* Generate the variable & register declarations for the ARB_vertex_program output target */
528 static DWORD
shader_generate_arb_declarations(IWineD3DBaseShader
*iface
, const shader_reg_maps
*reg_maps
,
529 SHADER_BUFFER
*buffer
, const WineD3D_GL_Info
*gl_info
, DWORD
*lconst_map
, DWORD
*num_clipplanes
,
530 struct shader_arb_ctx_priv
*ctx
)
532 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
533 DWORD i
, next_local
= 0;
534 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
535 unsigned max_constantsF
;
536 const local_constant
*lconst
;
538 /* In pixel shaders, all private constants are program local, we don't need anything
539 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
540 * If we need a private constant the GL implementation will squeeze it in somewhere
542 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
543 * immediate values. The posFixup is loaded using program.env for now, so always
544 * subtract one from the number of constants. If the shader uses indirect addressing,
545 * account for the helper const too because we have to declare all availabke d3d constants
546 * and don't know which are actually used.
549 max_constantsF
= GL_LIMITS(pshader_constantsF
);
551 if(This
->baseShader
.reg_maps
.usesrelconstF
) {
552 DWORD highest_constf
= 0, clip_limit
;
553 max_constantsF
= GL_LIMITS(vshader_constantsF
) - reserved_vs_const(iface
, gl_info
);
554 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.integer_constants
);
556 for(i
= 0; i
< This
->baseShader
.limits
.constant_float
; i
++)
559 DWORD shift
= i
& 0x1f;
560 if(reg_maps
->constf
[idx
] & (1 << shift
)) highest_constf
= i
;
563 clip_limit
= GL_LIMITS(clipplanes
);
564 if(ctx
->target_version
== ARB
) clip_limit
= min(clip_limit
, 4);
565 *num_clipplanes
= min(clip_limit
, max_constantsF
- highest_constf
- 1);
566 max_constantsF
-= *num_clipplanes
;
567 if(*num_clipplanes
< clip_limit
)
569 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes
, GL_LIMITS(clipplanes
));
574 if(ctx
->target_version
>= NV2
) *num_clipplanes
= GL_LIMITS(clipplanes
);
575 else *num_clipplanes
= min(GL_LIMITS(clipplanes
), 4);
576 max_constantsF
= GL_LIMITS(vshader_constantsF
) - 1;
580 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
581 if (reg_maps
->temporary
[i
])
582 shader_addline(buffer
, "TEMP R%u;\n", i
);
585 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
586 if (reg_maps
->address
[i
])
587 shader_addline(buffer
, "ADDRESS A%d;\n", i
);
590 if(pshader
&& reg_maps
->shader_version
.major
== 1 && reg_maps
->shader_version
.minor
<= 3) {
591 for(i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
592 if (reg_maps
->texcoord
[i
] && pshader
)
593 shader_addline(buffer
,"TEMP T%u;\n", i
);
597 /* Load local constants using the program-local space,
598 * this avoids reloading them each time the shader is used
601 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
602 shader_addline(buffer
, "PARAM C%u = program.local[%u];\n", lconst
->idx
,
603 lconst_map
[lconst
->idx
]);
604 next_local
= max(next_local
, lconst_map
[lconst
->idx
] + 1);
608 /* we use the array-based constants array if the local constants are marked for loading,
609 * because then we use indirect addressing, or when the local constant list is empty,
610 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
611 * local constants do not declare the loaded constants as an array because ARB compilers usually
612 * do not optimize unused constants away
614 if(This
->baseShader
.reg_maps
.usesrelconstF
) {
615 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
616 shader_addline(buffer
, "PARAM C[%d] = { program.env[0..%d] };\n",
617 max_constantsF
, max_constantsF
- 1);
619 for(i
= 0; i
< max_constantsF
; i
++) {
622 mask
= 1 << (i
& 0x1f);
623 if(!shader_constant_is_local(This
, i
) && (This
->baseShader
.reg_maps
.constf
[idx
] & mask
)) {
624 shader_addline(buffer
, "PARAM C%d = program.env[%d];\n",i
, i
);
632 static const char * const shift_tab
[] = {
633 "dummy", /* 0 (none) */
634 "coefmul.x", /* 1 (x2) */
635 "coefmul.y", /* 2 (x4) */
636 "coefmul.z", /* 3 (x8) */
637 "coefmul.w", /* 4 (x16) */
638 "dummy", /* 5 (x32) */
639 "dummy", /* 6 (x64) */
640 "dummy", /* 7 (x128) */
641 "dummy", /* 8 (d256) */
642 "dummy", /* 9 (d128) */
643 "dummy", /* 10 (d64) */
644 "dummy", /* 11 (d32) */
645 "coefdiv.w", /* 12 (d16) */
646 "coefdiv.z", /* 13 (d8) */
647 "coefdiv.y", /* 14 (d4) */
648 "coefdiv.x" /* 15 (d2) */
651 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction
*ins
,
652 const struct wined3d_shader_dst_param
*dst
, char *write_mask
)
654 char *ptr
= write_mask
;
656 if (dst
->write_mask
!= WINED3DSP_WRITEMASK_ALL
)
659 if (dst
->write_mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
660 if (dst
->write_mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
661 if (dst
->write_mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
662 if (dst
->write_mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
668 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param
*param
, BOOL fixup
, char *swizzle_str
)
670 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
671 * but addressed as "rgba". To fix this we need to swap the register's x
672 * and z components. */
673 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
674 char *ptr
= swizzle_str
;
676 /* swizzle bits fields: wwzzyyxx */
677 DWORD swizzle
= param
->swizzle
;
678 DWORD swizzle_x
= swizzle
& 0x03;
679 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
680 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
681 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
683 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
684 * generate a swizzle string. Unless we need to our own swizzling. */
685 if (swizzle
!= WINED3DSP_NOSWIZZLE
|| fixup
)
688 if (swizzle_x
== swizzle_y
&& swizzle_x
== swizzle_z
&& swizzle_x
== swizzle_w
) {
689 *ptr
++ = swizzle_chars
[swizzle_x
];
691 *ptr
++ = swizzle_chars
[swizzle_x
];
692 *ptr
++ = swizzle_chars
[swizzle_y
];
693 *ptr
++ = swizzle_chars
[swizzle_z
];
694 *ptr
++ = swizzle_chars
[swizzle_w
];
701 static void shader_arb_request_a0(const struct wined3d_shader_instruction
*ins
, const char *src
)
703 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
704 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
706 if(strcmp(priv
->addr_reg
, src
) == 0) return;
708 strcpy(priv
->addr_reg
, src
);
709 shader_addline(buffer
, "ARL A0.x, %s;\n", src
);
712 static void shader_arb_get_src_param(const struct wined3d_shader_instruction
*ins
,
713 const struct wined3d_shader_src_param
*src
, unsigned int tmpreg
, char *outregstr
);
715 static void shader_arb_get_register_name(const struct wined3d_shader_instruction
*ins
,
716 const struct wined3d_shader_register
*reg
, char *register_name
, BOOL
*is_color
)
718 /* oPos, oFog and oPts in D3D */
719 static const char * const rastout_reg_names
[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
720 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
721 BOOL pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
722 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
728 case WINED3DSPR_TEMP
:
729 sprintf(register_name
, "R%u", reg
->idx
);
732 case WINED3DSPR_INPUT
:
735 if(This
->baseShader
.reg_maps
.shader_version
.major
< 3)
737 if (reg
->idx
== 0) strcpy(register_name
, "fragment.color.primary");
738 else strcpy(register_name
, "fragment.color.secondary");
745 shader_arb_get_src_param(ins
, reg
->rel_addr
, 0, rel_reg
);
747 if(strcmp(rel_reg
, "**aL_emul**") == 0)
749 DWORD idx
= ctx
->aL
+ reg
->idx
;
750 if(idx
< MAX_REG_INPUT
)
752 strcpy(register_name
, ctx
->ps_input
[idx
]);
756 ERR("Pixel shader input register out of bounds: %u\n", idx
);
757 sprintf(register_name
, "out_of_bounds_%u", idx
);
760 else if(This
->baseShader
.reg_maps
.input_registers
& 0x0300)
762 /* There are two ways basically:
764 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
765 * That means trouble if the loop also contains a breakc or if the control values
766 * aren't local constants.
767 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
768 * source dynamically. The trouble is that we cannot simply read aL.y because it
769 * is an ADDRESS register. We could however push it, load .zw with a value and use
770 * ADAC to load the condition code register and pop it again afterwards
772 FIXME("Relative input register addressing with more than 8 registers\n");
774 /* This is better than nothing for now */
775 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
777 else if(ctx
->cur_ps_args
->super
.vp_mode
!= vertexshader
)
779 /* This is problematic because we'd have to consult the ctx->ps_input strings
780 * for where to find the varying. Some may be "0.0", others can be texcoords or
781 * colors. This needs either a pipeline replacement to make the vertex shader feed
782 * proper varyings, or loop unrolling
784 * For now use the texcoords and hope for the best
786 FIXME("Non-vertex shader varying input with indirect addressing\n");
787 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
791 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
792 * pulls GL_NV_fragment_program2 in
794 sprintf(register_name
, "fragment.texcoord[%s + %u]", rel_reg
, reg
->idx
);
799 if(reg
->idx
< MAX_REG_INPUT
)
801 strcpy(register_name
, ctx
->ps_input
[reg
->idx
]);
805 ERR("Pixel shader input register out of bounds: %u\n", reg
->idx
);
806 sprintf(register_name
, "out_of_bounds_%u", reg
->idx
);
813 if (ctx
->cur_vs_args
->super
.swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
814 sprintf(register_name
, "vertex.attrib[%u]", reg
->idx
);
818 case WINED3DSPR_CONST
:
819 if (!pshader
&& reg
->rel_addr
)
823 UINT rel_offset
= ((IWineD3DVertexShaderImpl
*)This
)->rel_offset
;
824 if(This
->baseShader
.reg_maps
.shader_version
.major
< 2) {
825 sprintf(rel_reg
, "A0.x");
827 shader_arb_get_src_param(ins
, reg
->rel_addr
, 0, rel_reg
);
828 if(ctx
->target_version
== ARB
) {
829 if(strcmp(rel_reg
, "**aL_emul**") == 0) {
832 shader_arb_request_a0(ins
, rel_reg
);
833 sprintf(rel_reg
, "A0.x");
838 sprintf(register_name
, "C[%u]", ctx
->aL
+ reg
->idx
);
839 else if (reg
->idx
>= rel_offset
)
840 sprintf(register_name
, "C[%s + %u]", rel_reg
, reg
->idx
- rel_offset
);
842 sprintf(register_name
, "C[%s - %u]", rel_reg
, -reg
->idx
+ rel_offset
);
846 if (This
->baseShader
.reg_maps
.usesrelconstF
)
847 sprintf(register_name
, "C[%u]", reg
->idx
);
849 sprintf(register_name
, "C%u", reg
->idx
);
853 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
855 if(This
->baseShader
.reg_maps
.shader_version
.major
== 1 &&
856 This
->baseShader
.reg_maps
.shader_version
.minor
<= 3) {
857 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
858 * and as source to most instructions. For some instructions it is the texcoord
859 * input. Those instructions know about the special use
861 sprintf(register_name
, "T%u", reg
->idx
);
863 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
864 sprintf(register_name
, "fragment.texcoord[%u]", reg
->idx
);
869 if(This
->baseShader
.reg_maps
.shader_version
.major
== 1 || ctx
->target_version
>= NV2
)
871 sprintf(register_name
, "A%u", reg
->idx
);
875 sprintf(register_name
, "A%u_SHADOW", reg
->idx
);
880 case WINED3DSPR_COLOROUT
:
883 if(ctx
->cur_ps_args
->super
.srgb_correction
)
885 strcpy(register_name
, "TMP_COLOR");
889 strcpy(register_name
, "result.color");
894 /* TODO: See GL_ARB_draw_buffers */
895 FIXME("Unsupported write to render target %u\n", reg
->idx
);
896 sprintf(register_name
, "unsupported_register");
900 case WINED3DSPR_RASTOUT
:
901 if(reg
->idx
== 1) sprintf(register_name
, "%s", ctx
->fog_output
);
902 else sprintf(register_name
, "%s", rastout_reg_names
[reg
->idx
]);
905 case WINED3DSPR_DEPTHOUT
:
906 strcpy(register_name
, "result.depth");
909 case WINED3DSPR_ATTROUT
:
910 /* case WINED3DSPR_OUTPUT: */
911 if (pshader
) sprintf(register_name
, "oD[%u]", reg
->idx
);
912 else strcpy(register_name
, ctx
->color_output
[reg
->idx
]);
915 case WINED3DSPR_TEXCRDOUT
:
918 sprintf(register_name
, "oT[%u]", reg
->idx
);
922 if(This
->baseShader
.reg_maps
.shader_version
.major
< 3)
924 sprintf(register_name
, ctx
->texcrd_output
[reg
->idx
]);
928 sprintf(register_name
, ctx
->vs_output
[reg
->idx
]);
933 case WINED3DSPR_LOOP
:
934 if(ctx
->target_version
>= NV2
)
936 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
937 if(pshader
) sprintf(register_name
, "A0.x");
938 else sprintf(register_name
, "aL.y");
942 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
943 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
944 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
945 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
948 sprintf(register_name
, "**aL_emul**");
953 case WINED3DSPR_CONSTINT
:
954 sprintf(register_name
, "I%u", reg
->idx
);
957 case WINED3DSPR_MISCTYPE
:
960 sprintf(register_name
, "vpos");
962 else if(reg
->idx
== 1)
964 sprintf(register_name
, "fragment.facing.x");
968 FIXME("Unknown MISCTYPE register index %u\n", reg
->idx
);
973 FIXME("Unhandled register type %#x[%u]\n", reg
->type
, reg
->idx
);
974 sprintf(register_name
, "unrecognized_register[%u]", reg
->idx
);
979 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction
*ins
,
980 const struct wined3d_shader_dst_param
*wined3d_dst
, char *str
)
982 char register_name
[255];
986 shader_arb_get_register_name(ins
, &wined3d_dst
->reg
, register_name
, &is_color
);
987 strcpy(str
, register_name
);
989 shader_arb_get_write_mask(ins
, wined3d_dst
, write_mask
);
990 strcat(str
, write_mask
);
993 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source
)
995 switch(channel_source
)
997 case CHANNEL_SOURCE_ZERO
: return "0";
998 case CHANNEL_SOURCE_ONE
: return "1";
999 case CHANNEL_SOURCE_X
: return "x";
1000 case CHANNEL_SOURCE_Y
: return "y";
1001 case CHANNEL_SOURCE_Z
: return "z";
1002 case CHANNEL_SOURCE_W
: return "w";
1004 FIXME("Unhandled channel source %#x\n", channel_source
);
1009 static void gen_color_correction(SHADER_BUFFER
*buffer
, const char *reg
, DWORD dst_mask
,
1010 const char *one
, const char *two
, struct color_fixup_desc fixup
)
1014 if (is_yuv_fixup(fixup
))
1016 enum yuv_fixup yuv_fixup
= get_yuv_fixup(fixup
);
1017 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup
);
1022 if (fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1023 if (fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1024 if (fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1025 if (fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1030 shader_addline(buffer
, "SWZ %s, %s, %s, %s, %s, %s;\n", reg
, reg
,
1031 shader_arb_get_fixup_swizzle(fixup
.x_source
), shader_arb_get_fixup_swizzle(fixup
.y_source
),
1032 shader_arb_get_fixup_swizzle(fixup
.z_source
), shader_arb_get_fixup_swizzle(fixup
.w_source
));
1036 if (fixup
.x_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_0
;
1037 if (fixup
.y_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_1
;
1038 if (fixup
.z_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_2
;
1039 if (fixup
.w_sign_fixup
) mask
|= WINED3DSP_WRITEMASK_3
;
1045 char *ptr
= reg_mask
;
1047 if (mask
!= WINED3DSP_WRITEMASK_ALL
)
1050 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
1051 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
1052 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
1053 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
1057 shader_addline(buffer
, "MAD %s%s, %s, %s, -%s;\n", reg
, reg_mask
, reg
, two
, one
);
1061 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction
*ins
)
1064 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1065 if (!ins
->dst_count
) return "";
1067 mod
= ins
->dst
[0].modifiers
;
1069 /* Silently ignore PARTIALPRECISION if its not supported */
1070 if(priv
->target_version
== ARB
) mod
&= ~WINED3DSPDM_PARTIALPRECISION
;
1072 if(mod
& WINED3DSPDM_MSAMPCENTROID
)
1074 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1075 mod
&= ~WINED3DSPDM_MSAMPCENTROID
;
1080 case WINED3DSPDM_SATURATE
| WINED3DSPDM_PARTIALPRECISION
:
1083 case WINED3DSPDM_SATURATE
:
1086 case WINED3DSPDM_PARTIALPRECISION
:
1093 FIXME("Unknown modifiers 0x%08x\n", mod
);
1098 #define TEX_PROJ 0x1
1099 #define TEX_BIAS 0x2
1101 #define TEX_DERIV 0x10
1103 static void shader_hw_sample(const struct wined3d_shader_instruction
*ins
, DWORD sampler_idx
,
1104 const char *dst_str
, const char *coord_reg
, WORD flags
, const char *dsx
, const char *dsy
)
1106 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1107 DWORD sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
1108 const char *tex_type
;
1109 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1110 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1111 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1113 BOOL pshader
= shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
1115 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1116 if(!pshader
) sampler_idx
+= MAX_FRAGMENT_SAMPLERS
;
1118 switch(sampler_type
) {
1124 if(device
->stateBlock
->textures
[sampler_idx
] &&
1125 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
1130 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
1132 if(priv
->cur_ps_args
->super
.np2_fixup
& (1 << sampler_idx
))
1134 FIXME("NP2 texcoord fixup is currently not implemented in ARB mode (use GLSL instead).\n");
1139 case WINED3DSTT_VOLUME
:
1143 case WINED3DSTT_CUBE
:
1148 ERR("Unexpected texture type %d\n", sampler_type
);
1152 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1153 * so don't use shader_arb_get_modifier
1155 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
) mod
= "_SAT";
1158 /* Fragment samplers always have indentity mapping */
1159 if(sampler_idx
>= MAX_FRAGMENT_SAMPLERS
)
1161 sampler_idx
= priv
->cur_vs_args
->vertex_samplers
[sampler_idx
- MAX_FRAGMENT_SAMPLERS
];
1164 if (flags
& TEX_DERIV
)
1166 if(flags
& TEX_PROJ
) FIXME("Projected texture sampling with custom derivates\n");
1167 if(flags
& TEX_BIAS
) FIXME("Biased texture sampling with custom derivates\n");
1168 shader_addline(buffer
, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
,
1169 dsx
, dsy
,sampler_idx
, tex_type
);
1171 else if(flags
& TEX_LOD
)
1173 if(flags
& TEX_PROJ
) FIXME("Projected texture sampling with explicit lod\n");
1174 if(flags
& TEX_BIAS
) FIXME("Biased texture sampling with explicit lod\n");
1175 shader_addline(buffer
, "TXL%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
,
1176 sampler_idx
, tex_type
);
1178 else if (flags
& TEX_BIAS
)
1180 /* Shouldn't be possible, but let's check for it */
1181 if(flags
& TEX_PROJ
) FIXME("Biased and Projected texture sampling\n");
1182 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1183 shader_addline(buffer
, "TXB%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1185 else if (flags
& TEX_PROJ
)
1187 shader_addline(buffer
, "TXP%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1191 shader_addline(buffer
, "TEX%s %s, %s, texture[%u], %s;\n", mod
, dst_str
, coord_reg
, sampler_idx
, tex_type
);
1196 gen_color_correction(buffer
, dst_str
, ins
->dst
[0].write_mask
,
1197 "one", "coefmul.x", priv
->cur_ps_args
->super
.color_fixup
[sampler_idx
]);
1201 static void shader_arb_get_src_param(const struct wined3d_shader_instruction
*ins
,
1202 const struct wined3d_shader_src_param
*src
, unsigned int tmpreg
, char *outregstr
)
1204 /* Generate a line that does the input modifier computation and return the input register to use */
1205 BOOL is_color
= FALSE
;
1209 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1210 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1212 /* Assume a new line will be added */
1215 /* Get register name */
1216 shader_arb_get_register_name(ins
, &src
->reg
, regstr
, &is_color
);
1217 shader_arb_get_swizzle(src
, is_color
, swzstr
);
1219 switch (src
->modifiers
)
1221 case WINED3DSPSM_NONE
:
1222 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
1225 case WINED3DSPSM_NEG
:
1226 sprintf(outregstr
, "-%s%s", regstr
, swzstr
);
1229 case WINED3DSPSM_BIAS
:
1230 shader_addline(buffer
, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg
, regstr
);
1232 case WINED3DSPSM_BIASNEG
:
1233 shader_addline(buffer
, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg
, regstr
);
1235 case WINED3DSPSM_SIGN
:
1236 shader_addline(buffer
, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg
, regstr
);
1238 case WINED3DSPSM_SIGNNEG
:
1239 shader_addline(buffer
, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg
, regstr
);
1241 case WINED3DSPSM_COMP
:
1242 shader_addline(buffer
, "SUB T%c, one.x, %s;\n", 'A' + tmpreg
, regstr
);
1244 case WINED3DSPSM_X2
:
1245 shader_addline(buffer
, "ADD T%c, %s, %s;\n", 'A' + tmpreg
, regstr
, regstr
);
1247 case WINED3DSPSM_X2NEG
:
1248 shader_addline(buffer
, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg
, regstr
, regstr
);
1250 case WINED3DSPSM_DZ
:
1251 shader_addline(buffer
, "RCP T%c, %s.z;\n", 'A' + tmpreg
, regstr
);
1252 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
1254 case WINED3DSPSM_DW
:
1255 shader_addline(buffer
, "RCP T%c, %s.w;\n", 'A' + tmpreg
, regstr
);
1256 shader_addline(buffer
, "MUL T%c, %s, T%c;\n", 'A' + tmpreg
, regstr
, 'A' + tmpreg
);
1258 case WINED3DSPSM_ABS
:
1259 if(ctx
->target_version
>= NV2
) {
1260 sprintf(outregstr
, "|%s%s|", regstr
, swzstr
);
1263 shader_addline(buffer
, "ABS T%c, %s;\n", 'A' + tmpreg
, regstr
);
1266 case WINED3DSPSM_ABSNEG
:
1267 if(ctx
->target_version
>= NV2
) {
1268 sprintf(outregstr
, "-|%s%s|", regstr
, swzstr
);
1270 shader_addline(buffer
, "ABS T%c, %s;\n", 'A' + tmpreg
, regstr
);
1271 sprintf(outregstr
, "-T%c%s", 'A' + tmpreg
, swzstr
);
1276 sprintf(outregstr
, "%s%s", regstr
, swzstr
);
1280 /* Return modified or original register, with swizzle */
1282 sprintf(outregstr
, "T%c%s", 'A' + tmpreg
, swzstr
);
1285 static void pshader_hw_bem(const struct wined3d_shader_instruction
*ins
)
1287 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1288 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1290 char src_name
[2][50];
1291 DWORD sampler_code
= dst
->reg
.idx
;
1293 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1295 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1297 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1298 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1301 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1302 shader_addline(buffer
, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code
);
1303 shader_addline(buffer
, "DP3 TC.r, TA, %s;\n", src_name
[1]);
1304 shader_addline(buffer
, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code
);
1305 shader_addline(buffer
, "DP3 TC.g, TA, %s;\n", src_name
[1]);
1307 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1308 shader_addline(buffer
, "ADD %s, %s, TC;\n", dst_name
, src_name
[0]);
1311 static void pshader_hw_cnd(const struct wined3d_shader_instruction
*ins
)
1313 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1314 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1316 char src_name
[3][50];
1317 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1318 ins
->ctx
->reg_maps
->shader_version
.minor
);
1321 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1322 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1324 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
1325 if (shader_version
<= WINED3D_SHADER_VERSION(1, 3) && ins
->coissue
)
1327 shader_addline(buffer
, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
[1]);
1329 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1330 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1331 shader_addline(buffer
, "ADD TA, -%s, coefdiv.x;\n", src_name
[0]);
1332 /* No modifiers supported on CMP */
1333 shader_addline(buffer
, "CMP %s, TA, %s, %s;\n", dst_name
, src_name
[1], src_name
[2]);
1335 /* _SAT on CMP doesn't make much sense, but it is not a pure NOP */
1336 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
1338 shader_arb_get_register_name(ins
, &dst
->reg
, src_name
[0], &is_color
);
1339 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, dst_name
);
1344 static void pshader_hw_cmp(const struct wined3d_shader_instruction
*ins
)
1346 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1347 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1349 char src_name
[3][50];
1352 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1354 /* Generate input register names (with modifiers) */
1355 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1356 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1357 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1359 /* No modifiers are supported on CMP */
1360 shader_addline(buffer
, "CMP %s, %s, %s, %s;\n", dst_name
,
1361 src_name
[0], src_name
[2], src_name
[1]);
1363 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
1365 shader_arb_get_register_name(ins
, &dst
->reg
, src_name
[0], &is_color
);
1366 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, src_name
[0]);
1370 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1371 * dst = dot2(src0, src1) + src2 */
1372 static void pshader_hw_dp2add(const struct wined3d_shader_instruction
*ins
)
1374 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1375 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1377 char src_name
[3][50];
1378 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1380 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1381 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
1382 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
1384 if(ctx
->target_version
>= NV3
)
1386 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1387 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1388 shader_addline(buffer
, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins
),
1389 dst_name
, src_name
[0], src_name
[1], src_name
[2]);
1391 else if(ctx
->target_version
>= NV2
)
1393 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1394 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1395 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1396 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1398 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1400 * .xyxy and other swizzles that we could get with this are not valid in
1401 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1403 struct wined3d_shader_src_param tmp_param
= ins
->src
[1];
1404 DWORD swizzle
= tmp_param
.swizzle
& 0xf; /* Selects .xy */
1405 tmp_param
.swizzle
= swizzle
| (swizzle
<< 4); /* Creates .xyxy */
1407 shader_arb_get_src_param(ins
, &tmp_param
, 1, src_name
[1]);
1409 shader_addline(buffer
, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins
),
1410 dst_name
, src_name
[2], src_name
[0], src_name
[1]);
1414 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
1415 /* 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]
1416 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1418 shader_addline(buffer
, "MOV TA, %s;\n", src_name
[0]);
1419 shader_addline(buffer
, "MOV TA.z, 0.0;\n");
1420 shader_addline(buffer
, "DP3 TA, TA, %s;\n", src_name
[1]);
1421 shader_addline(buffer
, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
[2]);
1425 /* Map the opcode 1-to-1 to the GL code */
1426 static void shader_hw_map2gl(const struct wined3d_shader_instruction
*ins
)
1428 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1429 const char *instruction
;
1430 char arguments
[256], dst_str
[50];
1432 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1434 switch (ins
->handler_idx
)
1436 case WINED3DSIH_ABS
: instruction
= "ABS"; break;
1437 case WINED3DSIH_ADD
: instruction
= "ADD"; break;
1438 case WINED3DSIH_CRS
: instruction
= "XPD"; break;
1439 case WINED3DSIH_DP3
: instruction
= "DP3"; break;
1440 case WINED3DSIH_DP4
: instruction
= "DP4"; break;
1441 case WINED3DSIH_DST
: instruction
= "DST"; break;
1442 case WINED3DSIH_EXP
: instruction
= "EX2"; break;
1443 case WINED3DSIH_EXPP
: instruction
= "EXP"; break;
1444 case WINED3DSIH_FRC
: instruction
= "FRC"; break;
1445 case WINED3DSIH_LIT
: instruction
= "LIT"; break;
1446 case WINED3DSIH_LOG
: instruction
= "LG2"; break;
1447 case WINED3DSIH_LOGP
: instruction
= "LOG"; break;
1448 case WINED3DSIH_LRP
: instruction
= "LRP"; break;
1449 case WINED3DSIH_MAD
: instruction
= "MAD"; break;
1450 case WINED3DSIH_MAX
: instruction
= "MAX"; break;
1451 case WINED3DSIH_MIN
: instruction
= "MIN"; break;
1452 case WINED3DSIH_MOV
: instruction
= "MOV"; break;
1453 case WINED3DSIH_MUL
: instruction
= "MUL"; break;
1454 case WINED3DSIH_POW
: instruction
= "POW"; break;
1455 case WINED3DSIH_SGE
: instruction
= "SGE"; break;
1456 case WINED3DSIH_SLT
: instruction
= "SLT"; break;
1457 case WINED3DSIH_SUB
: instruction
= "SUB"; break;
1458 case WINED3DSIH_MOVA
:instruction
= "ARR"; break;
1459 case WINED3DSIH_SGN
: instruction
= "SSG"; break;
1460 case WINED3DSIH_DSX
: instruction
= "DDX"; break;
1461 default: instruction
= "";
1462 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
1466 /* Note that shader_arb_add_dst_param() adds spaces. */
1467 arguments
[0] = '\0';
1468 shader_arb_get_dst_param(ins
, dst
, dst_str
);
1469 for (i
= 0; i
< ins
->src_count
; ++i
)
1472 strcat(arguments
, ", ");
1473 shader_arb_get_src_param(ins
, &ins
->src
[i
], i
, operand
);
1474 strcat(arguments
, operand
);
1476 shader_addline(buffer
, "%s%s %s%s;\n", instruction
, shader_arb_get_modifier(ins
), dst_str
, arguments
);
1479 static void shader_hw_nop(const struct wined3d_shader_instruction
*ins
)
1481 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1482 shader_addline(buffer
, "NOP;\n");
1485 static void shader_hw_mov(const struct wined3d_shader_instruction
*ins
)
1487 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1488 BOOL pshader
= shader_is_pshader_version(shader
->baseShader
.reg_maps
.shader_version
.type
);
1489 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
1491 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1492 char src0_param
[256];
1494 if(ins
->handler_idx
== WINED3DSIH_MOVA
) {
1495 struct wined3d_shader_src_param tmp_src
= ins
->src
[0];
1498 if(ctx
->target_version
>= NV2
) {
1499 shader_hw_map2gl(ins
);
1502 tmp_src
.swizzle
= (tmp_src
.swizzle
& 0x3) * 0x55;
1503 shader_arb_get_src_param(ins
, &tmp_src
, 0, src0_param
);
1504 shader_arb_get_write_mask(ins
, &ins
->dst
[0], write_mask
);
1506 /* This implements the mova formula used in GLSL. The first two instructions
1507 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1511 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1513 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1514 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1516 shader_addline(buffer
, "SGE A0_SHADOW%s, %s, mova_const.y;\n", write_mask
, src0_param
);
1517 shader_addline(buffer
, "MAD A0_SHADOW%s, A0_SHADOW, mova_const.z, -mova_const.w;\n", write_mask
);
1519 shader_addline(buffer
, "ABS TA%s, %s;\n", write_mask
, src0_param
);
1520 shader_addline(buffer
, "ADD TA%s, TA, mova_const.x;\n", write_mask
);
1521 shader_addline(buffer
, "FLR TA%s, TA;\n", write_mask
);
1522 shader_addline(buffer
, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask
);
1524 ((struct shader_arb_ctx_priv
*)ins
->ctx
->backend_data
)->addr_reg
[0] = '\0';
1525 } else if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
1526 && !shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)
1527 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
1529 src0_param
[0] = '\0';
1530 if (((IWineD3DVertexShaderImpl
*)shader
)->rel_offset
)
1532 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_param
);
1533 shader_addline(buffer
, "ADD TA.x, %s, helper_const.z;\n", src0_param
);
1534 shader_addline(buffer
, "ARL A0.x, TA.x;\n");
1538 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1539 * with more than one component. Thus replicate the first source argument over all
1540 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1541 struct wined3d_shader_src_param tmp_src
= ins
->src
[0];
1542 tmp_src
.swizzle
= (tmp_src
.swizzle
& 0x3) * 0x55;
1543 shader_arb_get_src_param(ins
, &tmp_src
, 0, src0_param
);
1544 shader_addline(buffer
, "ARL A0.x, %s;\n", src0_param
);
1547 else if(ins
->dst
[0].reg
.type
== WINED3DSPR_COLOROUT
&& ins
->dst
[0].reg
.idx
== 0 && pshader
)
1549 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) shader
;
1550 if(ctx
->cur_ps_args
->super
.srgb_correction
&& ps
->color0_mov
)
1552 shader_addline(buffer
, "#mov handled in srgb write code\n");
1555 shader_hw_map2gl(ins
);
1559 shader_hw_map2gl(ins
);
1563 static void pshader_hw_texkill(const struct wined3d_shader_instruction
*ins
)
1565 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1566 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1569 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1570 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1572 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1574 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
1576 /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1577 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1578 shader_addline(buffer
, "KIL %s;\n", reg_dest
);
1580 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1581 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1583 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1584 * or pass in any temporary register(in shader phase 2)
1586 if(ins
->ctx
->reg_maps
->shader_version
.minor
<= 3) {
1587 sprintf(reg_dest
, "fragment.texcoord[%u]", dst
->reg
.idx
);
1589 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1591 shader_addline(buffer
, "SWZ TA, %s, x, y, z, 1;\n", reg_dest
);
1592 shader_addline(buffer
, "KIL TA;\n");
1596 static void pshader_hw_tex(const struct wined3d_shader_instruction
*ins
)
1598 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1599 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1600 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1601 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1602 ins
->ctx
->reg_maps
->shader_version
.minor
);
1603 struct wined3d_shader_src_param src
;
1607 DWORD reg_sampler_code
;
1610 /* All versions have a destination register */
1611 shader_arb_get_dst_param(ins
, dst
, reg_dest
);
1613 /* 1.0-1.4: Use destination register number as texture code.
1614 2.0+: Use provided sampler number as texure code. */
1615 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
1616 reg_sampler_code
= dst
->reg
.idx
;
1618 reg_sampler_code
= ins
->src
[1].reg
.idx
;
1620 /* 1.0-1.3: Use the texcoord varying.
1621 1.4+: Use provided coordinate source register. */
1622 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1623 sprintf(reg_coord
, "fragment.texcoord[%u]", reg_sampler_code
);
1625 /* TEX is the only instruction that can handle DW and DZ natively */
1627 if(src
.modifiers
== WINED3DSPSM_DW
) src
.modifiers
= WINED3DSPSM_NONE
;
1628 if(src
.modifiers
== WINED3DSPSM_DZ
) src
.modifiers
= WINED3DSPSM_NONE
;
1629 shader_arb_get_src_param(ins
, &src
, 0, reg_coord
);
1633 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1634 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1635 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1637 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1640 if(reg_sampler_code
< MAX_TEXTURES
) {
1641 flags
= deviceImpl
->stateBlock
->textureState
[reg_sampler_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1643 if (flags
& WINED3DTTFF_PROJECTED
) {
1644 myflags
|= TEX_PROJ
;
1647 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
1649 DWORD src_mod
= ins
->src
[0].modifiers
;
1650 if (src_mod
== WINED3DSPSM_DZ
) {
1651 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1652 * varying register, so we need a temp reg
1654 shader_addline(ins
->ctx
->buffer
, "SWZ TA, %s, x, y, z, z;\n", reg_coord
);
1655 strcpy(reg_coord
, "TA");
1656 myflags
|= TEX_PROJ
;
1657 } else if(src_mod
== WINED3DSPSM_DW
) {
1658 myflags
|= TEX_PROJ
;
1661 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) myflags
|= TEX_PROJ
;
1662 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) myflags
|= TEX_BIAS
;
1664 shader_hw_sample(ins
, reg_sampler_code
, reg_dest
, reg_coord
, myflags
, NULL
, NULL
);
1667 static void pshader_hw_texcoord(const struct wined3d_shader_instruction
*ins
)
1669 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1670 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1671 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
1672 ins
->ctx
->reg_maps
->shader_version
.minor
);
1675 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
1677 DWORD reg
= dst
->reg
.idx
;
1679 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1680 shader_addline(buffer
, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str
, reg
);
1684 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_src
);
1685 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1686 shader_addline(buffer
, "MOV %s, %s;\n", dst_str
, reg_src
);
1690 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction
*ins
)
1692 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1693 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1694 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1697 DWORD reg1
= ins
->dst
[0].reg
.idx
;
1701 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
1702 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1703 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
1704 /* Move .x first in case src_str is "TA" */
1705 shader_addline(buffer
, "MOV TA.y, %s.x;\n", src_str
);
1706 shader_addline(buffer
, "MOV TA.x, %s.w;\n", src_str
);
1707 flags
= reg1
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg1
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1708 shader_hw_sample(ins
, reg1
, dst_str
, "TA", flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
1711 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction
*ins
)
1713 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1715 DWORD reg1
= ins
->dst
[0].reg
.idx
;
1719 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
1720 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1721 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
1722 shader_addline(buffer
, "MOV TA.x, %s.y;\n", src_str
);
1723 shader_addline(buffer
, "MOV TA.y, %s.z;\n", src_str
);
1724 shader_hw_sample(ins
, reg1
, dst_str
, "TA", 0, NULL
, NULL
);
1727 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
1729 DWORD reg1
= ins
->dst
[0].reg
.idx
;
1733 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
1734 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1735 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_str
);
1736 shader_hw_sample(ins
, reg1
, dst_str
, src_str
, 0, NULL
, NULL
);
1739 static void pshader_hw_texbem(const struct wined3d_shader_instruction
*ins
)
1741 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1742 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1743 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1744 char reg_coord
[40], dst_reg
[50], src_reg
[50];
1745 DWORD reg_dest_code
;
1747 /* All versions have a destination register. The Tx where the texture coordinates come
1748 * from is the varying incarnation of the texture register
1750 reg_dest_code
= dst
->reg
.idx
;
1751 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_reg
);
1752 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_reg
);
1753 sprintf(reg_coord
, "fragment.texcoord[%u]", reg_dest_code
);
1755 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1756 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
1758 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
1759 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
1761 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
1762 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
1765 shader_addline(buffer
, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code
);
1766 shader_addline(buffer
, "DP3 TA.x, TB, %s;\n", src_reg
);
1767 shader_addline(buffer
, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code
);
1768 shader_addline(buffer
, "DP3 TA.y, TB, %s;\n", src_reg
);
1770 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1771 * so we can't let the GL handle this.
1773 if (((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->stateBlock
->textureState
[reg_dest_code
][WINED3DTSS_TEXTURETRANSFORMFLAGS
]
1774 & WINED3DTTFF_PROJECTED
) {
1775 shader_addline(buffer
, "RCP TB.w, %s.w;\n", reg_coord
);
1776 shader_addline(buffer
, "MUL TB.xy, %s, TB.w;\n", reg_coord
);
1777 shader_addline(buffer
, "ADD TA.xy, TA, TB;\n");
1779 shader_addline(buffer
, "ADD TA.xy, TA, %s;\n", reg_coord
);
1782 shader_hw_sample(ins
, reg_dest_code
, dst_reg
, "TA", 0, NULL
, NULL
);
1784 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
1786 /* No src swizzles are allowed, so this is ok */
1787 shader_addline(buffer
, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
1788 src_reg
, reg_dest_code
, reg_dest_code
);
1789 shader_addline(buffer
, "MUL %s, %s, TA;\n", dst_reg
, dst_reg
);
1793 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
1795 DWORD reg
= ins
->dst
[0].reg
.idx
;
1796 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1797 char src0_name
[50], dst_name
[50];
1799 struct wined3d_shader_register tmp_reg
= ins
->dst
[0].reg
;
1801 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
1802 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
1803 * T<reg+1> register. Use this register to store the calculated vector
1805 tmp_reg
.idx
= reg
+ 1;
1806 shader_arb_get_register_name(ins
, &tmp_reg
, dst_name
, &is_color
);
1807 shader_addline(buffer
, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name
, reg
, src0_name
);
1810 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
1812 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1813 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1815 DWORD reg
= ins
->dst
[0].reg
.idx
;
1816 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1822 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
1823 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
1825 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1826 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
1827 shader_addline(buffer
, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
1828 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1829 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
1832 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
1834 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1835 DWORD reg
= ins
->dst
[0].reg
.idx
;
1836 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1837 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1838 char src0_name
[50], dst_name
[50];
1839 struct wined3d_shader_register tmp_reg
= ins
->dst
[0].reg
;
1842 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
1843 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
1844 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
1846 tmp_reg
.idx
= reg
+ 2 - current_state
->current_row
;
1847 shader_arb_get_register_name(ins
, &tmp_reg
, dst_name
, &is_color
);
1849 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
1850 shader_addline(buffer
, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
1851 dst_name
, 'x' + current_state
->current_row
, reg
, src0_name
);
1852 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
1855 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
1857 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1858 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1860 DWORD reg
= ins
->dst
[0].reg
.idx
;
1861 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1862 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1864 char src0_name
[50], dst_name
[50];
1867 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
1868 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
1869 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name
, reg
, src0_name
);
1871 /* Sample the texture using the calculated coordinates */
1872 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1873 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1874 shader_hw_sample(ins
, reg
, dst_str
, dst_name
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
1875 current_state
->current_row
= 0;
1878 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
1880 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1881 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1883 DWORD reg
= ins
->dst
[0].reg
.idx
;
1884 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1885 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1891 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
1892 * components for temporary data storage
1894 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
1895 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
1896 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
1898 /* Construct the eye-ray vector from w coordinates */
1899 shader_addline(buffer
, "MOV TB.x, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[0]);
1900 shader_addline(buffer
, "MOV TB.y, fragment.texcoord[%u].w;\n", current_state
->texcoord_w
[1]);
1901 shader_addline(buffer
, "MOV TB.z, fragment.texcoord[%u].w;\n", reg
);
1903 /* Calculate reflection vector
1905 shader_addline(buffer
, "DP3 %s.w, %s, TB;\n", dst_reg
, dst_reg
);
1906 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
1907 shader_addline(buffer
, "DP3 TB.w, %s, %s;\n", dst_reg
, dst_reg
);
1908 shader_addline(buffer
, "RCP TB.w, TB.w;\n");
1909 shader_addline(buffer
, "MUL %s.w, %s.w, TB.w;\n", dst_reg
, dst_reg
);
1910 shader_addline(buffer
, "MUL %s, %s.w, %s;\n", dst_reg
, dst_reg
, dst_reg
);
1911 shader_addline(buffer
, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg
, dst_reg
);
1913 /* Sample the texture using the calculated coordinates */
1914 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1915 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1916 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
1917 current_state
->current_row
= 0;
1920 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
1922 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1923 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1925 DWORD reg
= ins
->dst
[0].reg
.idx
;
1926 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1927 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1934 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0_name
);
1935 shader_arb_get_src_param(ins
, &ins
->src
[0], 1, src1_name
);
1936 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_reg
, &is_color
);
1937 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
1938 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg
, reg
, src0_name
);
1940 /* Calculate reflection vector.
1943 * dst_reg.xyz = 2 * --------- * N - E
1946 * Which normalizes the normal vector
1948 shader_addline(buffer
, "DP3 %s.w, %s, %s;\n", dst_reg
, dst_reg
, src1_name
);
1949 shader_addline(buffer
, "DP3 TC.w, %s, %s;\n", dst_reg
, dst_reg
);
1950 shader_addline(buffer
, "RCP TC.w, TC.w;\n");
1951 shader_addline(buffer
, "MUL %s.w, %s.w, TC.w;\n", dst_reg
, dst_reg
);
1952 shader_addline(buffer
, "MUL %s, %s.w, %s;\n", dst_reg
, dst_reg
, dst_reg
);
1953 shader_addline(buffer
, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg
, dst_reg
, src1_name
);
1955 /* Sample the texture using the calculated coordinates */
1956 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
1957 flags
= reg
< MAX_TEXTURES
? deviceImpl
->stateBlock
->textureState
[reg
][WINED3DTSS_TEXTURETRANSFORMFLAGS
] : 0;
1958 shader_hw_sample(ins
, reg
, dst_str
, dst_reg
, flags
& WINED3DTTFF_PROJECTED
? TEX_PROJ
: 0, NULL
, NULL
);
1959 current_state
->current_row
= 0;
1962 static void pshader_hw_texdepth(const struct wined3d_shader_instruction
*ins
)
1964 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
1965 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1968 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1969 * which is essentially an input, is the destination register because it is the first
1970 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1971 * here(writemasks/swizzles are not valid on texdepth)
1973 shader_arb_get_dst_param(ins
, dst
, dst_name
);
1975 /* According to the msdn, the source register(must be r5) is unusable after
1976 * the texdepth instruction, so we're free to modify it
1978 shader_addline(buffer
, "MIN %s.y, %s.y, one.y;\n", dst_name
, dst_name
);
1980 /* How to deal with the special case dst_name.g == 0? if r != 0, then
1981 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1982 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1984 shader_addline(buffer
, "RCP %s.y, %s.y;\n", dst_name
, dst_name
);
1985 shader_addline(buffer
, "MUL TA.x, %s.x, %s.y;\n", dst_name
, dst_name
);
1986 shader_addline(buffer
, "MIN TA.x, TA.x, one.x;\n");
1987 shader_addline(buffer
, "MAX result.depth, TA.x, 0.0;\n");
1990 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1991 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1992 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1993 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction
*ins
)
1995 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1996 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
2000 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2001 shader_addline(buffer
, "MOV TB, 0.0;\n");
2002 shader_addline(buffer
, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx
, src0
);
2004 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_str
);
2005 shader_hw_sample(ins
, sampler_idx
, dst_str
, "TB", 0 /* Only one coord, can't be projected */, NULL
, NULL
);
2008 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2009 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2010 static void pshader_hw_texdp3(const struct wined3d_shader_instruction
*ins
)
2012 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2015 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2017 /* Handle output register */
2018 shader_arb_get_dst_param(ins
, dst
, dst_str
);
2019 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2020 shader_addline(buffer
, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str
, dst
->reg
.idx
, src0
);
2023 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2024 * Perform the 3rd row of a 3x3 matrix multiply */
2025 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction
*ins
)
2027 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2028 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2029 char dst_str
[50], dst_name
[50];
2033 shader_arb_get_dst_param(ins
, dst
, dst_str
);
2034 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2035 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2036 shader_addline(buffer
, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name
, dst
->reg
.idx
, src0
);
2037 shader_addline(buffer
, "MOV %s, %s;\n", dst_str
, dst_name
);
2040 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2041 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2042 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2043 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2045 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
2047 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2048 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2049 char src0
[50], dst_name
[50];
2052 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src0
);
2053 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2054 shader_addline(buffer
, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name
, dst
->reg
.idx
, src0
);
2056 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2057 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2058 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2060 shader_addline(buffer
, "RCP %s.y, %s.y;\n", dst_name
, dst_name
);
2061 shader_addline(buffer
, "MUL %s.x, %s.x, %s.y;\n", dst_name
, dst_name
, dst_name
);
2062 shader_addline(buffer
, "MIN %s.x, %s.x, one.x;\n", dst_name
, dst_name
);
2063 shader_addline(buffer
, "MAX result.depth, %s.x, 0.0;\n", dst_name
);
2066 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2067 Vertex/Pixel shaders to ARB_vertex_program codes */
2068 static void shader_hw_mnxn(const struct wined3d_shader_instruction
*ins
)
2071 int nComponents
= 0;
2072 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2073 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2074 struct wined3d_shader_instruction tmp_ins
;
2076 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2078 /* Set constants for the temporary argument */
2079 tmp_ins
.ctx
= ins
->ctx
;
2080 tmp_ins
.dst_count
= 1;
2081 tmp_ins
.dst
= &tmp_dst
;
2082 tmp_ins
.src_count
= 2;
2083 tmp_ins
.src
= tmp_src
;
2085 switch(ins
->handler_idx
)
2087 case WINED3DSIH_M4x4
:
2089 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2091 case WINED3DSIH_M4x3
:
2093 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2095 case WINED3DSIH_M3x4
:
2097 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2099 case WINED3DSIH_M3x3
:
2101 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2103 case WINED3DSIH_M3x2
:
2105 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2108 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
2112 tmp_dst
= ins
->dst
[0];
2113 tmp_src
[0] = ins
->src
[0];
2114 tmp_src
[1] = ins
->src
[1];
2115 for (i
= 0; i
< nComponents
; i
++) {
2116 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2117 shader_hw_map2gl(&tmp_ins
);
2118 ++tmp_src
[1].reg
.idx
;
2122 static void shader_hw_rsq_rcp(const struct wined3d_shader_instruction
*ins
)
2124 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2125 const char *instruction
;
2130 switch(ins
->handler_idx
)
2132 case WINED3DSIH_RSQ
: instruction
= "RSQ"; break;
2133 case WINED3DSIH_RCP
: instruction
= "RCP"; break;
2134 default: instruction
= "";
2135 FIXME("Unhandled opcode %#x\n", ins
->handler_idx
);
2139 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
); /* Destination */
2140 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2141 if (ins
->src
[0].swizzle
== WINED3DSP_NOSWIZZLE
)
2143 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2149 shader_addline(buffer
, "%s%s %s, %s;\n", instruction
, shader_arb_get_modifier(ins
), dst
, src
);
2152 static void shader_hw_nrm(const struct wined3d_shader_instruction
*ins
)
2154 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2157 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2158 BOOL pshader
= shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2160 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2161 shader_arb_get_src_param(ins
, &ins
->src
[0], 1 /* Use TB */, src_name
);
2163 if(pshader
&& priv
->target_version
>= NV3
)
2165 shader_addline(buffer
, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name
);
2169 shader_addline(buffer
, "DP3 TA, %s, %s;\n", src_name
, src_name
);
2170 shader_addline(buffer
, "RSQ TA, TA.x;\n");
2171 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2172 shader_addline(buffer
, "MUL%s %s, %s, TA;\n", shader_arb_get_modifier(ins
), dst_name
,
2177 static void shader_hw_lrp(const struct wined3d_shader_instruction
*ins
)
2179 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2181 char src_name
[3][50];
2183 /* ARB_fragment_program has a convenient LRP instruction */
2184 if(shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)) {
2185 shader_hw_map2gl(ins
);
2189 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2190 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
[0]);
2191 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name
[1]);
2192 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name
[2]);
2194 shader_addline(buffer
, "SUB TA, %s, %s;\n", src_name
[1], src_name
[2]);
2195 shader_addline(buffer
, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins
),
2196 dst_name
, src_name
[0], src_name
[2]);
2199 static void shader_hw_sincos(const struct wined3d_shader_instruction
*ins
)
2201 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2202 * must contain fixed constants. So we need a separate function to filter those constants and
2205 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2206 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2207 const struct wined3d_shader_dst_param
*dst
= &ins
->dst
[0];
2209 char src_name0
[50], src_name1
[50], src_name2
[50];
2212 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2213 if(shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)) {
2214 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2215 /* No modifiers are supported on SCS */
2216 shader_addline(buffer
, "SCS %s, %s;\n", dst_name
, src_name0
);
2218 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
)
2220 shader_arb_get_register_name(ins
, &dst
->reg
, src_name0
, &is_color
);
2221 shader_addline(buffer
, "MOV_SAT %s, %s;\n", dst_name
, src_name0
);
2223 } else if(priv
->target_version
>= NV2
) {
2224 shader_arb_get_register_name(ins
, &dst
->reg
, dst_name
, &is_color
);
2226 /* Sincos writemask must be .x, .y or .xy */
2227 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
)
2228 shader_addline(buffer
, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name0
);
2229 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
)
2230 shader_addline(buffer
, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins
), dst_name
, src_name0
);
2232 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2233 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2235 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2236 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2238 * The constants we get are:
2240 * +1 +1, -1 -1 +1 +1 -1 -1
2241 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2242 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2244 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2248 * (x/2)^4 = x^4 / 16
2249 * (x/2)^5 = x^5 / 32
2252 * To get the final result:
2253 * sin(x) = 2 * sin(x/2) * cos(x/2)
2254 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2255 * (from sin(x+y) and cos(x+y) rules)
2257 * As per MSDN, dst.z is undefined after the operation, and so is
2258 * dst.x and dst.y if they're masked out by the writemask. Ie
2259 * sincos dst.y, src1, c0, c1
2260 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2261 * vsa.exe also stops with an error if the dest register is the same register as the source
2262 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2263 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2265 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2266 shader_arb_get_src_param(ins
, &ins
->src
[2], 2, src_name2
);
2267 shader_arb_get_register_name(ins
, &dst
->reg
, dst_name
, &is_color
);
2269 shader_addline(buffer
, "MUL %s.x, %s, %s;\n", dst_name
, src_name0
, src_name0
); /* x ^ 2 */
2270 shader_addline(buffer
, "MUL TA.y, %s.x, %s;\n", dst_name
, src_name0
); /* x ^ 3 */
2271 shader_addline(buffer
, "MUL %s.y, TA.y, %s;\n", dst_name
, src_name0
); /* x ^ 4 */
2272 shader_addline(buffer
, "MUL TA.z, %s.y, %s;\n", dst_name
, src_name0
); /* x ^ 5 */
2273 shader_addline(buffer
, "MUL %s.z, TA.z, %s;\n", dst_name
, src_name0
); /* x ^ 6 */
2274 shader_addline(buffer
, "MUL TA.w, %s.z, %s;\n", dst_name
, src_name0
); /* x ^ 7 */
2278 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2279 * properly merge that with MULs in the code above?
2280 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2281 * we can merge the sine and cosine MAD rows to calculate them together.
2283 shader_addline(buffer
, "MUL TA.x, %s, %s.w;\n", src_name0
, src_name2
); /* x^1, +1/(1!*2) */
2284 shader_addline(buffer
, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2
); /* -1/(3!*8) */
2285 shader_addline(buffer
, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1
); /* +1/(5!*32) */
2286 shader_addline(buffer
, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1
); /* -1/(7!*128) */
2289 shader_addline(buffer
, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name
, src_name2
, src_name2
); /* -1/(2!*4), +1.0 */
2290 shader_addline(buffer
, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name
, src_name1
); /* +1/(4!*16) */
2291 shader_addline(buffer
, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name
, src_name1
); /* -1/(6!*64) */
2293 if(dst
->write_mask
& WINED3DSP_WRITEMASK_0
) {
2295 shader_addline(buffer
, "MUL TA.z, TA.y, TA.y;\n");
2296 shader_addline(buffer
, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name
);
2298 if(dst
->write_mask
& WINED3DSP_WRITEMASK_1
) {
2300 shader_addline(buffer
, "MUL %s.y, TA.x, TA.y;\n", dst_name
);
2301 shader_addline(buffer
, "ADD %s.y, %s.y, %s.y;\n", dst_name
, dst_name
, dst_name
);
2306 /* GL locking is done by the caller */
2307 static void shader_hw_sgn(const struct wined3d_shader_instruction
*ins
)
2309 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2312 struct shader_arb_ctx_priv
*ctx
= ins
->ctx
->backend_data
;
2314 /* SGN is only valid in vertex shaders */
2315 if(ctx
->target_version
== NV2
) {
2316 shader_hw_map2gl(ins
);
2319 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst_name
);
2320 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
);
2322 FIXME("Emulated SGN untested\n");
2323 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2324 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2326 if(ins
->dst
[0].modifiers
& WINED3DSPDM_SATURATE
) {
2327 shader_addline(buffer
, "SLT %s, -%s, %s;\n", dst_name
, src_name
, src_name
);
2329 shader_addline(buffer
, "SLT TB, -%s, %s;\n", src_name
, src_name
);
2330 shader_addline(buffer
, "SLT TC, %s, -%s;\n", src_name
, src_name
);
2331 shader_addline(buffer
, "ADD %s, TB, -TC;\n", dst_name
);
2335 static void shader_hw_dsy(const struct wined3d_shader_instruction
*ins
)
2337 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2343 shader_arb_get_dst_param(ins
, &ins
->dst
[0], dst
);
2344 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src
);
2345 shader_arb_get_register_name(ins
, &ins
->dst
[0].reg
, dst_name
, &is_color
);
2347 shader_addline(buffer
, "DDY %s, %s;\n", dst
, src
);
2348 shader_addline(buffer
, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins
), dst
, dst_name
);
2351 static void shader_hw_loop(const struct wined3d_shader_instruction
*ins
)
2353 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2355 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2358 shader_arb_get_src_param(ins
, &ins
->src
[1], 0, src_name
);
2362 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2363 struct list
*e
= list_head(&priv
->control_frames
);
2364 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2366 if(priv
->loop_depth
> 1) shader_addline(buffer
, "PUSHA aL;\n");
2367 /* The constant loader makes sure to load -1 into iX.w */
2368 shader_addline(buffer
, "ARLC aL, %s.xywz;\n", src_name
);
2369 shader_addline(buffer
, "BRA loop_%u_end (LE.x);\n", control_frame
->loop_no
);
2370 shader_addline(buffer
, "loop_%u_start:\n", control_frame
->loop_no
);
2374 shader_addline(buffer
, "LOOP %s;\n", src_name
);
2378 static void shader_hw_rep(const struct wined3d_shader_instruction
*ins
)
2380 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2382 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2384 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name
);
2386 /* The constant loader makes sure to load -1 into iX.w */
2389 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2390 struct list
*e
= list_head(&priv
->control_frames
);
2391 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2393 if(priv
->loop_depth
> 1) shader_addline(buffer
, "PUSHA aL;\n");
2395 shader_addline(buffer
, "ARLC aL, %s.xywz;\n", src_name
);
2396 shader_addline(buffer
, "BRA loop_%u_end (LE.x);\n", control_frame
->loop_no
);
2397 shader_addline(buffer
, "loop_%u_start:\n", control_frame
->loop_no
);
2401 shader_addline(buffer
, "REP %s;\n", src_name
);
2405 static void shader_hw_endloop(const struct wined3d_shader_instruction
*ins
)
2407 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2408 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2412 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2413 struct list
*e
= list_head(&priv
->control_frames
);
2414 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2416 shader_addline(buffer
, "ARAC aL.xy, aL;\n");
2417 shader_addline(buffer
, "BRA loop_%u_start (GT.x);\n", control_frame
->loop_no
);
2418 shader_addline(buffer
, "loop_%u_end:\n", control_frame
->loop_no
);
2420 if(priv
->loop_depth
> 1) shader_addline(buffer
, "POPA aL;\n");
2424 shader_addline(buffer
, "ENDLOOP;\n");
2428 static void shader_hw_endrep(const struct wined3d_shader_instruction
*ins
)
2430 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2431 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2435 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2436 struct list
*e
= list_head(&priv
->control_frames
);
2437 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2439 shader_addline(buffer
, "ARAC aL.xy, aL;\n");
2440 shader_addline(buffer
, "BRA loop_%u_start (GT.x);\n", control_frame
->loop_no
);
2441 shader_addline(buffer
, "loop_%u_end:\n", control_frame
->loop_no
);
2443 if(priv
->loop_depth
> 1) shader_addline(buffer
, "POPA aL;\n");
2447 shader_addline(buffer
, "ENDREP;\n");
2451 static const struct control_frame
*find_last_loop(const struct shader_arb_ctx_priv
*priv
)
2453 struct control_frame
*control_frame
;
2455 LIST_FOR_EACH_ENTRY(control_frame
, &priv
->control_frames
, struct control_frame
, entry
)
2457 if(control_frame
->type
== LOOP
|| control_frame
->type
== REP
) return control_frame
;
2459 ERR("Could not find loop for break\n");
2463 static void shader_hw_break(const struct wined3d_shader_instruction
*ins
)
2465 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2466 const struct control_frame
*control_frame
= find_last_loop(ins
->ctx
->backend_data
);
2467 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2471 shader_addline(buffer
, "BRA loop_%u_end;\n", control_frame
->loop_no
);
2475 shader_addline(buffer
, "BRK;\n");
2479 static void shader_hw_breakc(const struct wined3d_shader_instruction
*ins
)
2481 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2482 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2483 const struct control_frame
*control_frame
= find_last_loop(ins
->ctx
->backend_data
);
2488 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2489 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2493 case COMPARISON_GT
: comp
= "GT"; break;
2494 case COMPARISON_EQ
: comp
= "EQ"; break;
2495 case COMPARISON_GE
: comp
= "GE"; break;
2496 case COMPARISON_LT
: comp
= "LT"; break;
2497 case COMPARISON_NE
: comp
= "NE"; break;
2498 case COMPARISON_LE
: comp
= "LE"; break;
2500 FIXME("Unrecognized comparison value: %u\n", ins
->flags
);
2506 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
2507 * away the subtraction result
2509 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2510 shader_addline(buffer
, "BRA loop_%u_end (%s.x);\n", control_frame
->loop_no
, comp
);
2514 shader_addline(buffer
, "SUBC CC, %s, %s;\n", src_name0
, src_name1
);
2515 shader_addline(buffer
, "BRK (%s.x);\n", comp
);
2519 static void shader_hw_ifc(const struct wined3d_shader_instruction
*ins
)
2521 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2522 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2523 struct list
*e
= list_head(&priv
->control_frames
);
2524 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2528 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2530 /* Invert the flag. We jump to the else label if the condition is NOT true */
2533 case COMPARISON_GT
: comp
= "LE"; break;
2534 case COMPARISON_EQ
: comp
= "NE"; break;
2535 case COMPARISON_GE
: comp
= "LT"; break;
2536 case COMPARISON_LT
: comp
= "GE"; break;
2537 case COMPARISON_NE
: comp
= "EQ"; break;
2538 case COMPARISON_LE
: comp
= "GT"; break;
2540 FIXME("Unrecognized comparison value: %u\n", ins
->flags
);
2544 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, src_name0
);
2545 shader_arb_get_src_param(ins
, &ins
->src
[1], 1, src_name1
);
2549 shader_addline(buffer
, "SUBC TA, %s, %s;\n", src_name0
, src_name1
);
2550 shader_addline(buffer
, "BRA ifc_%u_endif (%s.x);\n", control_frame
->ifc_no
, comp
);
2554 shader_addline(buffer
, "SUBC CC, %s, %s;\n", src_name0
, src_name1
);
2555 shader_addline(buffer
, "IF %s.x;\n", comp
);
2559 static void shader_hw_else(const struct wined3d_shader_instruction
*ins
)
2561 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2562 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2563 struct list
*e
= list_head(&priv
->control_frames
);
2564 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2565 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2569 shader_addline(buffer
, "BRA ifc_%u_endif;\n", control_frame
->ifc_no
);
2570 shader_addline(buffer
, "ifc_%u_else:\n", control_frame
->ifc_no
);
2571 control_frame
->had_else
= TRUE
;
2575 shader_addline(buffer
, "ELSE;\n");
2579 static void shader_hw_endif(const struct wined3d_shader_instruction
*ins
)
2581 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2582 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2583 struct list
*e
= list_head(&priv
->control_frames
);
2584 struct control_frame
*control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
2585 BOOL vshader
= shader_is_vshader_version(ins
->ctx
->reg_maps
->shader_version
.type
);
2589 if(control_frame
->had_else
)
2591 shader_addline(buffer
, "ifc_%u_endif:\n", control_frame
->ifc_no
);
2595 shader_addline(buffer
, "#No else branch. else is endif\n");
2596 shader_addline(buffer
, "ifc_%u_else:\n", control_frame
->ifc_no
);
2601 shader_addline(buffer
, "ENDIF;\n");
2605 static void shader_hw_texldd(const struct wined3d_shader_instruction
*ins
)
2607 DWORD sampler_idx
= ins
->src
[1].reg
.idx
;
2609 char reg_src
[3][40];
2610 DWORD flags
= TEX_DERIV
;
2612 shader_arb_get_dst_param(ins
, &ins
->dst
[0], reg_dest
);
2613 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_src
[0]);
2614 shader_arb_get_src_param(ins
, &ins
->src
[2], 1, reg_src
[1]);
2615 shader_arb_get_src_param(ins
, &ins
->src
[3], 2, reg_src
[2]);
2617 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) flags
|= TEX_PROJ
;
2618 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) flags
|= TEX_BIAS
;
2620 shader_hw_sample(ins
, sampler_idx
, reg_dest
, reg_src
[0], flags
, reg_src
[1], reg_src
[2]);
2623 static void shader_hw_texldl(const struct wined3d_shader_instruction
*ins
)
2625 DWORD sampler_idx
= ins
->src
[1].reg
.idx
;
2628 DWORD flags
= TEX_LOD
;
2630 shader_arb_get_dst_param(ins
, &ins
->dst
[0], reg_dest
);
2631 shader_arb_get_src_param(ins
, &ins
->src
[0], 0, reg_coord
);
2633 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
) flags
|= TEX_PROJ
;
2634 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
) flags
|= TEX_BIAS
;
2636 shader_hw_sample(ins
, sampler_idx
, reg_dest
, reg_coord
, flags
, NULL
, NULL
);
2639 static GLuint
create_arb_blt_vertex_program(const WineD3D_GL_Info
*gl_info
)
2641 GLuint program_id
= 0;
2642 const char *blt_vprogram
=
2644 "PARAM c[1] = { { 1, 0.5 } };\n"
2645 "MOV result.position, vertex.position;\n"
2646 "MOV result.color, c[0].x;\n"
2647 "MOV result.texcoord[0], vertex.texcoord[0];\n"
2650 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
2651 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, program_id
));
2652 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_vprogram
), blt_vprogram
));
2654 if (glGetError() == GL_INVALID_OPERATION
) {
2656 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
2657 FIXME("Vertex program error at position %d: %s\n", pos
,
2658 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
2664 /* GL locking is done by the caller */
2665 static GLuint
create_arb_blt_fragment_program(const WineD3D_GL_Info
*gl_info
, enum tex_types tex_type
)
2667 GLuint program_id
= 0;
2668 static const char * const blt_fprograms
[tex_type_count
] =
2675 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
2676 "MOV result.depth.z, R0.x;\n"
2683 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
2684 "MOV result.depth.z, R0.x;\n"
2689 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
2690 "MOV result.depth.z, R0.x;\n"
2694 if (!blt_fprograms
[tex_type
])
2696 FIXME("tex_type %#x not supported\n", tex_type
);
2700 GL_EXTCALL(glGenProgramsARB(1, &program_id
));
2701 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, program_id
));
2702 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(blt_fprograms
[tex_type
]), blt_fprograms
[tex_type
]));
2704 if (glGetError() == GL_INVALID_OPERATION
) {
2706 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
2707 FIXME("Fragment program error at position %d: %s\n", pos
,
2708 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
2714 static void arbfp_add_sRGB_correction(SHADER_BUFFER
*buffer
, const char *fragcolor
, const char *tmp1
,
2715 const char *tmp2
, const char *tmp3
, const char *tmp4
, BOOL condcode
) {
2716 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
2720 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
2721 shader_addline(buffer
, "SUBC %s, %s.x, srgb_consts1.y;\n", tmp1
, fragcolor
);
2722 /* Calculate the > 0.0031308 case */
2723 shader_addline(buffer
, "POW %s.x (GE), %s.x, srgb_consts1.z;\n", fragcolor
, fragcolor
);
2724 shader_addline(buffer
, "POW %s.y (GE), %s.y, srgb_consts1.z;\n", fragcolor
, fragcolor
);
2725 shader_addline(buffer
, "POW %s.z (GE), %s.z, srgb_consts1.z;\n", fragcolor
, fragcolor
);
2726 shader_addline(buffer
, "MUL %s.xyz (GE), %s, srgb_consts1.w;\n", fragcolor
, fragcolor
);
2727 shader_addline(buffer
, "SUB %s.xyz (GE), %s, srgb_consts2.x;\n", fragcolor
, fragcolor
);
2728 /* Calculate the < case */
2729 shader_addline(buffer
, "MUL %s.xyz (LT), srgb_consts1.x, %s;\n", fragcolor
, fragcolor
);
2733 /* Calculate the > 0.0031308 case */
2734 shader_addline(buffer
, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1
, fragcolor
);
2735 shader_addline(buffer
, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1
, fragcolor
);
2736 shader_addline(buffer
, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1
, fragcolor
);
2737 shader_addline(buffer
, "MUL %s, %s, srgb_consts1.w;\n", tmp1
, tmp1
);
2738 shader_addline(buffer
, "SUB %s, %s, srgb_consts2.x;\n", tmp1
, tmp1
);
2739 /* Calculate the < case */
2740 shader_addline(buffer
, "MUL %s, srgb_consts1.x, %s;\n", tmp2
, fragcolor
);
2741 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
2742 shader_addline(buffer
, "SLT %s, srgb_consts1.y, %s;\n", tmp3
, fragcolor
);
2743 shader_addline(buffer
, "SGE %s, srgb_consts1.y, %s;\n", tmp4
, fragcolor
);
2744 /* Store the components > 0.0031308 in the destination */
2745 shader_addline(buffer
, "MUL %s.xyz, %s, %s;\n", fragcolor
, tmp1
, tmp3
);
2746 /* Add the components that are < 0.0031308 */
2747 shader_addline(buffer
, "MAD %s.xyz, %s, %s, %s;\n", fragcolor
, tmp2
, tmp4
, fragcolor
);
2748 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
2749 * result.color writes(.rgb first, then .a), or handle overwriting already written
2750 * components. The assembler uses a temporary register in this case, which is usually
2751 * not allocated from one of our registers that were used earlier.
2754 shader_addline(buffer
, "MOV result.color, %s;\n", fragcolor
);
2755 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
2758 static const DWORD
*find_loop_control_values(IWineD3DBaseShaderImpl
*This
, DWORD idx
)
2760 const local_constant
*constant
;
2762 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsI
, local_constant
, entry
)
2764 if (constant
->idx
== idx
)
2766 return constant
->value
;
2772 static void init_ps_input(const IWineD3DPixelShaderImpl
*This
, const struct arb_ps_compile_args
*args
,
2773 struct shader_arb_ctx_priv
*priv
)
2775 const char *texcoords
[8] =
2777 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
2778 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
2781 const struct wined3d_shader_signature_element
*sig
= This
->input_signature
;
2782 const char *semantic_name
;
2785 switch(args
->super
.vp_mode
)
2787 case pretransformed
:
2789 /* The pixelshader has to collect the varyings on its own. In any case properly load
2790 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
2791 * other attribs to 0.0.
2793 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
2794 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
2795 * load the texcoord attrib pointers to match the pixel shader signature
2797 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
2799 semantic_name
= sig
[i
].semantic_name
;
2800 semantic_idx
= sig
[i
].semantic_idx
;
2801 if(semantic_name
== NULL
) continue;
2803 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
2805 if(semantic_idx
== 0) priv
->ps_input
[i
] = "fragment.color.primary";
2806 else if(semantic_idx
== 1) priv
->ps_input
[i
] = "fragment.color.secondary";
2807 else priv
->ps_input
[i
] = "0.0";
2809 else if(args
->super
.vp_mode
== fixedfunction
)
2811 priv
->ps_input
[i
] = "0.0";
2813 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
2815 if(semantic_idx
< 8) priv
->ps_input
[i
] = texcoords
[semantic_idx
];
2816 else priv
->ps_input
[i
] = "0.0";
2818 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
2820 if(semantic_idx
== 0) priv
->ps_input
[i
] = "fragment.fogcoord";
2821 else priv
->ps_input
[i
] = "0.0";
2825 priv
->ps_input
[i
] = "0.0";
2828 TRACE("v%u, semantic %s%u is %s\n", i
, semantic_name
, semantic_idx
, priv
->ps_input
[i
]);
2833 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
2836 for(i
= 0; i
< 8; i
++)
2838 priv
->ps_input
[i
] = texcoords
[i
];
2840 priv
->ps_input
[8] = "fragment.color.primary";
2841 priv
->ps_input
[9] = "fragment.color.secondary";
2846 /* GL locking is done by the caller */
2847 static GLuint
shader_arb_generate_pshader(IWineD3DPixelShaderImpl
*This
,
2848 SHADER_BUFFER
*buffer
, const struct arb_ps_compile_args
*args
, struct arb_ps_compiled_shader
*compiled
)
2850 const shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
2851 CONST DWORD
*function
= This
->baseShader
.function
;
2852 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
2853 const local_constant
*lconst
;
2856 DWORD
*lconst_map
= local_const_mapping((IWineD3DBaseShaderImpl
*) This
), next_local
, cur
;
2857 struct shader_arb_ctx_priv priv_ctx
;
2858 BOOL dcl_tmp
= args
->super
.srgb_correction
, dcl_td
= FALSE
;
2859 BOOL want_nv_prog
= FALSE
;
2860 struct arb_pshader_private
*shader_priv
= This
->backend_priv
;
2863 unsigned int i
, found
= 0;
2865 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
2867 /* Don't overwrite the color source */
2868 if(This
->color0_mov
&& i
== This
->color0_reg
) continue;
2869 else if(reg_maps
->shader_version
.major
< 2 && i
== 0) continue;
2871 if(reg_maps
->temporary
[i
]) {
2872 sprintf(srgbtmp
[found
], "R%u", i
);
2874 if(found
== 4) break;
2879 case 4: dcl_tmp
= FALSE
; break;
2881 sprintf(srgbtmp
[0], "TA");
2882 sprintf(srgbtmp
[1], "TB");
2883 sprintf(srgbtmp
[2], "TC");
2884 sprintf(srgbtmp
[3], "TD");
2888 sprintf(srgbtmp
[1], "TA");
2889 sprintf(srgbtmp
[2], "TB");
2890 sprintf(srgbtmp
[3], "TC");
2893 sprintf(srgbtmp
[2], "TA");
2894 sprintf(srgbtmp
[3], "TB");
2897 sprintf(srgbtmp
[3], "TA");
2901 /* Create the hw ARB shader */
2902 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
2903 priv_ctx
.cur_ps_args
= args
;
2904 priv_ctx
.compiled_fprog
= compiled
;
2905 init_ps_input(This
, args
, &priv_ctx
);
2906 list_init(&priv_ctx
.control_frames
);
2908 /* Avoid enabling NV_fragment_program* if we do not need it.
2910 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
2911 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
2912 * is faster than what we gain from using higher native instructions. There are some things though
2913 * that cannot be emulated. In that case enable the extensions.
2914 * If the extension is enabled, instruction handlers that support both ways will use it.
2916 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
2917 * So enable the best we can get.
2919 if(reg_maps
->usesdsx
|| reg_maps
->usesdsy
|| reg_maps
->loop_depth
> 0 || reg_maps
->usestexldd
||
2920 reg_maps
->usestexldl
|| reg_maps
->usesfacing
)
2922 want_nv_prog
= TRUE
;
2925 shader_addline(buffer
, "!!ARBfp1.0\n");
2926 if(want_nv_prog
&& GL_SUPPORT(NV_FRAGMENT_PROGRAM2
)) {
2927 shader_addline(buffer
, "OPTION NV_fragment_program2;\n");
2928 priv_ctx
.target_version
= NV3
;
2929 } else if(want_nv_prog
&& GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION
)) {
2930 shader_addline(buffer
, "OPTION NV_fragment_program;\n");
2931 priv_ctx
.target_version
= NV2
;
2935 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
2938 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
2941 priv_ctx
.target_version
= ARB
;
2944 if (reg_maps
->shader_version
.major
< 3)
2946 switch(args
->super
.fog
) {
2950 shader_addline(buffer
, "OPTION ARB_fog_linear;\n");
2953 shader_addline(buffer
, "OPTION ARB_fog_exp;\n");
2956 shader_addline(buffer
, "OPTION ARB_fog_exp2;\n");
2961 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
2962 * unused temps away(but occupies them for the whole shader if they're used once). Always
2963 * declaring them avoids tricky bookkeeping work
2965 shader_addline(buffer
, "TEMP TA;\n"); /* Used for modifiers */
2966 shader_addline(buffer
, "TEMP TB;\n"); /* Used for modifiers */
2967 shader_addline(buffer
, "TEMP TC;\n"); /* Used for modifiers */
2968 if(dcl_td
) shader_addline(buffer
, "TEMP TD;\n"); /* Used for sRGB writing */
2969 shader_addline(buffer
, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
2970 shader_addline(buffer
, "PARAM coefmul = { 2, 4, 8, 16 };\n");
2971 shader_addline(buffer
, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
2973 if (reg_maps
->shader_version
.major
< 2)
2975 strcpy(fragcolor
, "R0");
2977 if(args
->super
.srgb_correction
) {
2978 if(This
->color0_mov
) {
2979 sprintf(fragcolor
, "R%u", This
->color0_reg
);
2981 shader_addline(buffer
, "TEMP TMP_COLOR;\n");
2982 strcpy(fragcolor
, "TMP_COLOR");
2985 strcpy(fragcolor
, "result.color");
2989 if(args
->super
.srgb_correction
) {
2990 shader_addline(buffer
, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
2991 srgb_mul_low
, srgb_cmp
, srgb_pow
, srgb_mul_high
);
2992 shader_addline(buffer
, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
2993 srgb_sub_high
, 0.0, 0.0, 0.0);
2996 /* Base Declarations */
2997 next_local
= shader_generate_arb_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
,
2998 lconst_map
, NULL
, &priv_ctx
);
3000 for(i
= 0; i
< (sizeof(reg_maps
->bumpmat
) / sizeof(reg_maps
->bumpmat
[0])); i
++) {
3001 if(!reg_maps
->bumpmat
[i
]) continue;
3003 cur
= compiled
->numbumpenvmatconsts
;
3004 compiled
->bumpenvmatconst
[cur
].const_num
= WINED3D_CONST_NUM_UNUSED
;
3005 compiled
->bumpenvmatconst
[cur
].texunit
= i
;
3006 compiled
->luminanceconst
[cur
].const_num
= WINED3D_CONST_NUM_UNUSED
;
3007 compiled
->luminanceconst
[cur
].texunit
= i
;
3009 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3010 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3011 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3012 * textures due to conditional NP2 restrictions)
3014 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3015 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3016 * their location is shader dependent anyway and they cannot be loaded globally.
3018 compiled
->bumpenvmatconst
[cur
].const_num
= next_local
++;
3019 shader_addline(buffer
, "PARAM bumpenvmat%d = program.local[%d];\n",
3020 i
, compiled
->bumpenvmatconst
[cur
].const_num
);
3021 compiled
->numbumpenvmatconsts
= cur
+ 1;
3023 if(!reg_maps
->luminanceparams
[i
]) continue;
3025 compiled
->luminanceconst
[cur
].const_num
= next_local
++;
3026 shader_addline(buffer
, "PARAM luminance%d = program.local[%d];\n",
3027 i
, compiled
->luminanceconst
[cur
].const_num
);
3030 for(i
= 0; i
< MAX_CONST_I
; i
++)
3032 compiled
->int_consts
[i
] = WINED3D_CONST_NUM_UNUSED
;
3033 if (reg_maps
->integer_constants
& (1 << i
) && priv_ctx
.target_version
>= NV2
)
3035 const DWORD
*control_values
= find_loop_control_values((IWineD3DBaseShaderImpl
*) This
, i
);
3039 shader_addline(buffer
, "PARAM I%u = {%u, %u, %u, -1};\n", i
,
3040 control_values
[0], control_values
[1], control_values
[2]);
3044 compiled
->int_consts
[i
] = next_local
;
3045 compiled
->num_int_consts
++;
3046 shader_addline(buffer
, "PARAM I%u = program.local[%u];\n", i
, next_local
++);
3051 if(reg_maps
->vpos
|| reg_maps
->usesdsy
)
3053 compiled
->ycorrection
= next_local
;
3054 shader_addline(buffer
, "PARAM ycorrection = program.local[%u];\n", next_local
++);
3058 shader_addline(buffer
, "TEMP vpos;\n");
3059 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3060 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3061 * ycorrection.z: 1.0
3062 * ycorrection.w: 0.0
3064 shader_addline(buffer
, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3065 shader_addline(buffer
, "FLR vpos.xy, vpos;\n");
3070 compiled
->ycorrection
= WINED3D_CONST_NUM_UNUSED
;
3073 if(shader_priv
->clipplane_emulation
)
3075 shader_addline(buffer
, "KIL fragment.texcoord[%u];\n", shader_priv
->clipplane_emulation
- 1);
3078 /* Base Shader Body */
3079 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
3081 if(args
->super
.srgb_correction
) {
3082 arbfp_add_sRGB_correction(buffer
, fragcolor
, srgbtmp
[0], srgbtmp
[1], srgbtmp
[2], srgbtmp
[3],
3083 priv_ctx
.target_version
>= NV2
);
3084 } else if(reg_maps
->shader_version
.major
< 2) {
3085 shader_addline(buffer
, "MOV result.color, %s;\n", fragcolor
);
3087 shader_addline(buffer
, "END\n");
3089 /* TODO: change to resource.glObjectHandle or something like that */
3090 GL_EXTCALL(glGenProgramsARB(1, &retval
));
3092 TRACE("Creating a hw pixel shader, prg=%d\n", retval
);
3093 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, retval
));
3095 TRACE("Created hw pixel shader, prg=%d\n", retval
);
3096 /* Create the program and check for errors */
3097 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
3098 buffer
->bsize
, buffer
->buffer
));
3100 if (glGetError() == GL_INVALID_OPERATION
) {
3102 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
3103 FIXME("HW PixelShader Error at position %d: %s\n",
3104 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3108 /* Load immediate constants */
3110 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
3111 const float *value
= (const float *)lconst
->value
;
3112 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, lconst_map
[lconst
->idx
], value
));
3113 checkGLcall("glProgramLocalParameter4fvARB");
3115 HeapFree(GetProcessHeap(), 0, lconst_map
);
3121 static int compare_sig(const struct wined3d_shader_signature_element
*sig1
, const struct wined3d_shader_signature_element
*sig2
)
3126 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3128 if(sig1
[i
].semantic_name
== NULL
|| sig2
[i
].semantic_name
== NULL
)
3130 /* Compare pointers, not contents. One string is NULL(element does not exist), the other one is not NULL */
3131 if(sig1
[i
].semantic_name
!= sig2
[i
].semantic_name
) return sig1
[i
].semantic_name
< sig2
[i
].semantic_name
? -1 : 1;
3135 ret
= strcmp(sig1
[i
].semantic_name
, sig2
[i
].semantic_name
);
3136 if(ret
!= 0) return ret
;
3137 if(sig1
[i
].semantic_idx
!= sig2
[i
].semantic_idx
) return sig1
[i
].semantic_idx
< sig2
[i
].semantic_idx
? -1 : 1;
3138 if(sig1
[i
].sysval_semantic
!= sig2
[i
].sysval_semantic
) return sig1
[i
].sysval_semantic
< sig2
[i
].sysval_semantic
? -1 : 1;
3139 if(sig1
[i
].component_type
!= sig2
[i
].component_type
) return sig1
[i
].sysval_semantic
< sig2
[i
].component_type
? -1 : 1;
3140 if(sig1
[i
].register_idx
!= sig2
[i
].register_idx
) return sig1
[i
].register_idx
< sig2
[i
].register_idx
? -1 : 1;
3141 if(sig1
[i
].mask
!= sig2
->mask
) return sig1
[i
].mask
< sig2
[i
].mask
? -1 : 1;
3146 static struct wined3d_shader_signature_element
*clone_sig(const struct wined3d_shader_signature_element
*sig
)
3148 struct wined3d_shader_signature_element
*new;
3152 new = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*new) * MAX_REG_INPUT
);
3153 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3155 if(sig
[i
].semantic_name
== NULL
)
3161 /* Clone the semantic string */
3162 name
= HeapAlloc(GetProcessHeap(), 0, strlen(sig
[i
].semantic_name
) + 1);
3163 strcpy(name
, sig
[i
].semantic_name
);
3164 new[i
].semantic_name
= name
;
3169 static DWORD
find_input_signature(struct shader_arb_priv
*priv
, const struct wined3d_shader_signature_element
*sig
)
3171 struct wine_rb_entry
*entry
= wine_rb_get(&priv
->signature_tree
, sig
);
3172 struct ps_signature
*found_sig
;
3176 found_sig
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
3177 TRACE("Found existing signature %u\n", found_sig
->idx
);
3178 return found_sig
->idx
;
3180 found_sig
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*sig
));
3181 found_sig
->sig
= clone_sig(sig
);
3182 found_sig
->idx
= priv
->ps_sig_number
++;
3183 TRACE("New signature stored and assigned number %u\n", found_sig
->idx
);
3184 if(wine_rb_put(&priv
->signature_tree
, sig
, &found_sig
->entry
) == -1)
3186 ERR("Failed to insert program entry.\n");
3188 return found_sig
->idx
;
3191 static void init_output_registers(IWineD3DVertexShaderImpl
*shader
, DWORD sig_num
, struct shader_arb_ctx_priv
*priv_ctx
)
3194 static const char *texcoords
[8] =
3196 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3197 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
3199 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
3200 const struct wined3d_shader_signature_element
*sig
;
3201 const char *semantic_name
;
3202 DWORD semantic_idx
, reg_idx
;
3204 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
3205 * and varying 9 to result.color.secondary
3207 const char *decl_idx_to_string
[MAX_REG_INPUT
] =
3209 texcoords
[0], texcoords
[1], texcoords
[2], texcoords
[3],
3210 texcoords
[4], texcoords
[5], texcoords
[6], texcoords
[7],
3211 "result.color.primary", "result.color.secondary"
3216 TRACE("Pixel shader uses builtin varyings\n");
3217 /* Map builtins to builtins */
3218 for(i
= 0; i
< 8; i
++)
3220 priv_ctx
->texcrd_output
[i
] = texcoords
[i
];
3222 priv_ctx
->color_output
[0] = "result.color.primary";
3223 priv_ctx
->color_output
[1] = "result.color.secondary";
3224 priv_ctx
->fog_output
= "result.fogcoord";
3226 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
3227 for(i
= 0; i
< (sizeof(shader
->output_signature
) / sizeof(*shader
->output_signature
)); i
++)
3229 semantic_name
= shader
->output_signature
[i
].semantic_name
;
3230 if(semantic_name
== NULL
) continue;
3232 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3234 TRACE("o%u is TMP_OUT\n", i
);
3235 if(shader
->output_signature
[i
].semantic_idx
== 0) priv_ctx
->vs_output
[i
] = "TMP_OUT";
3236 else priv_ctx
->vs_output
[i
] = "TA";
3238 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3240 TRACE("o%u is result.pointsize\n", i
);
3241 if(shader
->output_signature
[i
].semantic_idx
== 0) priv_ctx
->vs_output
[i
] = "result.pointsize";
3242 else priv_ctx
->vs_output
[i
] = "TA";
3244 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3246 TRACE("o%u is result.color.?, idx %u\n", i
, shader
->output_signature
[i
].semantic_idx
);
3247 if(shader
->output_signature
[i
].semantic_idx
== 0) priv_ctx
->vs_output
[i
] = "result.color.primary";
3248 else if(shader
->output_signature
[i
].semantic_idx
== 1) priv_ctx
->vs_output
[i
] = "result.color.secondary";
3249 else priv_ctx
->vs_output
[i
] = "TA";
3251 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3253 TRACE("o%u is %s\n", i
, texcoords
[shader
->output_signature
[i
].semantic_idx
]);
3254 if(shader
->output_signature
[i
].semantic_idx
>= 8) priv_ctx
->vs_output
[i
] = "TA";
3255 else priv_ctx
->vs_output
[i
] = texcoords
[shader
->output_signature
[i
].semantic_idx
];
3257 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3259 TRACE("o%u is result.fogcoord\n", i
);
3260 if(shader
->output_signature
[i
].semantic_idx
> 0) priv_ctx
->vs_output
[i
] = "TA";
3261 else priv_ctx
->vs_output
[i
] = "result.fogcoord";
3265 priv_ctx
->vs_output
[i
] = "TA";
3271 /* Instead of searching for the signature in the signature list, read the one from the current pixel shader.
3272 * Its maybe not the shader where the signature came from, but it is the same signature and faster to find
3274 sig
= ((IWineD3DPixelShaderImpl
*)device
->stateBlock
->pixelShader
)->input_signature
;
3275 TRACE("Pixel shader uses declared varyings\n");
3277 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
3278 for(i
= 0; i
< 8; i
++)
3280 priv_ctx
->texcrd_output
[i
] = "TA";
3282 priv_ctx
->color_output
[0] = "TA";
3283 priv_ctx
->color_output
[1] = "TA";
3284 priv_ctx
->fog_output
= "TA";
3286 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
3288 semantic_name
= sig
[i
].semantic_name
;
3289 semantic_idx
= sig
[i
].semantic_idx
;
3290 reg_idx
= sig
[i
].register_idx
;
3291 if(semantic_name
== NULL
) continue;
3293 /* If a declared input register is not written by builtin arguments, don't write to it.
3294 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
3296 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
3297 * to TMP_OUT in any case
3299 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3301 if(semantic_idx
< 8) priv_ctx
->texcrd_output
[semantic_idx
] = decl_idx_to_string
[reg_idx
];
3303 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3305 if(semantic_idx
< 2) priv_ctx
->color_output
[semantic_idx
] = decl_idx_to_string
[reg_idx
];
3307 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3309 if(semantic_idx
== 0) priv_ctx
->fog_output
= decl_idx_to_string
[reg_idx
];
3313 /* Map declared to declared */
3314 for(i
= 0; i
< (sizeof(shader
->output_signature
) / sizeof(*shader
->output_signature
)); i
++)
3316 /* Write unread output to TA to throw them away */
3317 priv_ctx
->vs_output
[i
] = "TA";
3318 semantic_name
= shader
->output_signature
[i
].semantic_name
;
3319 if(semantic_name
== NULL
)
3324 if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
) &&
3325 shader
->output_signature
[i
].semantic_idx
== 0)
3327 priv_ctx
->vs_output
[i
] = "TMP_OUT";
3330 else if(shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
) &&
3331 shader
->output_signature
[i
].semantic_idx
== 0)
3333 priv_ctx
->vs_output
[i
] = "result.pointsize";
3337 for(j
= 0; j
< MAX_REG_INPUT
; j
++)
3339 if(sig
[j
].semantic_name
== NULL
)
3344 if(strcmp(sig
[j
].semantic_name
, semantic_name
) == 0 &&
3345 sig
[j
].semantic_idx
== shader
->output_signature
[i
].semantic_idx
)
3347 priv_ctx
->vs_output
[i
] = decl_idx_to_string
[sig
[j
].register_idx
];
3353 /* GL locking is done by the caller */
3354 static GLuint
shader_arb_generate_vshader(IWineD3DVertexShaderImpl
*This
,
3355 SHADER_BUFFER
*buffer
, const struct arb_vs_compile_args
*args
, struct arb_vs_compiled_shader
*compiled
)
3357 const shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
3358 CONST DWORD
*function
= This
->baseShader
.function
;
3359 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
3360 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
3361 const local_constant
*lconst
;
3363 DWORD next_local
, *lconst_map
= local_const_mapping((IWineD3DBaseShaderImpl
*) This
);
3364 struct shader_arb_ctx_priv priv_ctx
;
3366 DWORD num_clipplanes
= 0;
3368 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
3369 priv_ctx
.cur_vs_args
= args
;
3370 list_init(&priv_ctx
.control_frames
);
3371 init_output_registers(This
, args
->ps_signature
, &priv_ctx
);
3373 /* Create the hw ARB shader */
3374 shader_addline(buffer
, "!!ARBvp1.0\n");
3376 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
3377 * mesurable performance penalty, and we can always make use of it for clipplanes.
3379 if(GL_SUPPORT(NV_VERTEX_PROGRAM3
)) {
3380 shader_addline(buffer
, "OPTION NV_vertex_program3;\n");
3381 priv_ctx
.target_version
= NV3
;
3382 shader_addline(buffer
, "ADDRESS aL;\n");
3383 } else if(GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION
)) {
3384 shader_addline(buffer
, "OPTION NV_vertex_program2;\n");
3385 priv_ctx
.target_version
= NV2
;
3386 shader_addline(buffer
, "ADDRESS aL;\n");
3388 priv_ctx
.target_version
= ARB
;
3391 shader_addline(buffer
, "TEMP TMP_OUT;\n");
3392 if(need_helper_const(gl_info
)) {
3393 shader_addline(buffer
, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This
->rel_offset
);
3395 if(need_mova_const((IWineD3DBaseShader
*) This
, gl_info
)) {
3396 shader_addline(buffer
, "PARAM mova_const = { 0.5, 0.0, 2.0, 1.0 };\n");
3397 shader_addline(buffer
, "TEMP A0_SHADOW;\n");
3400 shader_addline(buffer
, "TEMP TA;\n");
3402 /* Base Declarations */
3403 next_local
= shader_generate_arb_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
,
3404 lconst_map
, &num_clipplanes
, &priv_ctx
);
3406 for(i
= 0; i
< MAX_CONST_I
; i
++)
3408 compiled
->int_consts
[i
] = WINED3D_CONST_NUM_UNUSED
;
3409 if(reg_maps
->integer_constants
& (1 << i
) && priv_ctx
.target_version
>= NV2
)
3411 const DWORD
*control_values
= find_loop_control_values((IWineD3DBaseShaderImpl
*) This
, i
);
3415 shader_addline(buffer
, "PARAM I%u = {%u, %u, %u, -1};\n", i
,
3416 control_values
[0], control_values
[1], control_values
[2]);
3420 compiled
->int_consts
[i
] = next_local
;
3421 compiled
->num_int_consts
++;
3422 shader_addline(buffer
, "PARAM I%u = program.local[%u];\n", i
, next_local
++);
3427 /* We need a constant to fixup the final position */
3428 shader_addline(buffer
, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS
);
3430 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
3431 * for output parameters. D3D in theory does not do that either, but some applications depend on a
3432 * proper initialization of the secondary color, and programs using the fixed function pipeline without
3433 * a replacement shader depend on the texcoord.w being set properly.
3435 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
3436 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
3437 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
3438 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
3439 * this can eat a number of instructions, so skip it unless this cap is set as well
3441 if(!GL_SUPPORT(NV_VERTEX_PROGRAM
)) {
3442 shader_addline(buffer
, "MOV result.color.secondary, -helper_const.wwwy;\n");
3444 if((GLINFO_LOCATION
).set_texcoord_w
&& !device
->frag_pipe
->ffp_proj_control
) {
3446 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
3447 if(This
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
3448 This
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3449 shader_addline(buffer
, "MOV result.texcoord[%u].w, -helper_const.y;\n", i
);
3455 /* Base Shader Body */
3456 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
3458 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
3459 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
3460 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
3461 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
3463 if(args
->super
.fog_src
== VS_FOG_Z
) {
3464 shader_addline(buffer
, "MOV result.fogcoord, TMP_OUT.z;\n");
3465 } else if (!reg_maps
->fog
) {
3466 /* posFixup.x is always 1.0, so we can savely use it */
3467 shader_addline(buffer
, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
3470 /* Write the final position.
3472 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3473 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3474 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3475 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
3477 shader_addline(buffer
, "MUL TA, posFixup, TMP_OUT.w;\n");
3478 shader_addline(buffer
, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
3479 shader_addline(buffer
, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
3481 if(priv_ctx
.target_version
>= NV2
)
3483 for(i
= 0; i
< num_clipplanes
; i
++)
3485 shader_addline(buffer
, "DP4 result.clip[%u].x, TMP_OUT, state.clip[%u].plane;\n", i
, i
);
3488 else if(args
->boolclip
.clip_control
[0])
3490 unsigned int cur_clip
= 0;
3491 char component
[4] = {'x', 'y', 'z', 'w'};
3493 for(i
= 0; i
< GL_LIMITS(clipplanes
); i
++)
3495 if(args
->boolclip
.clip_control
[1] & (1 << i
))
3497 shader_addline(buffer
, "DP4 TA.%c, TMP_OUT, state.clip[%u].plane;\n",
3498 component
[cur_clip
++], i
);
3504 shader_addline(buffer
, "MOV TA, -helper_const.w;\n");
3507 shader_addline(buffer
, "MOV TA.yzw, -helper_const.w;\n");
3510 shader_addline(buffer
, "MOV TA.zw, -helper_const.w;\n");
3513 shader_addline(buffer
, "MOV TA.w, -helper_const.w;\n");
3516 shader_addline(buffer
, "MOV result.texcoord[%u], TA;\n",
3517 args
->boolclip
.clip_control
[0] - 1);
3520 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3521 * and the glsl equivalent
3523 if(need_helper_const(gl_info
)) {
3524 shader_addline(buffer
, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
3526 shader_addline(buffer
, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
3527 shader_addline(buffer
, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
3530 shader_addline(buffer
, "MOV result.position, TMP_OUT;\n");
3532 shader_addline(buffer
, "END\n");
3534 /* TODO: change to resource.glObjectHandle or something like that */
3535 GL_EXTCALL(glGenProgramsARB(1, &ret
));
3537 TRACE("Creating a hw vertex shader, prg=%d\n", ret
);
3538 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, ret
));
3540 TRACE("Created hw vertex shader, prg=%d\n", ret
);
3541 /* Create the program and check for errors */
3542 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
3543 buffer
->bsize
, buffer
->buffer
));
3545 if (glGetError() == GL_INVALID_OPERATION
) {
3547 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errPos
);
3548 FIXME("HW VertexShader Error at position %d: %s\n",
3549 errPos
, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
3552 /* Load immediate constants */
3554 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
3555 const float *value
= (const float *)lconst
->value
;
3556 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB
, lconst_map
[lconst
->idx
], value
));
3560 HeapFree(GetProcessHeap(), 0, lconst_map
);
3565 /* GL locking is done by the caller */
3566 static struct arb_ps_compiled_shader
*find_arb_pshader(IWineD3DPixelShaderImpl
*shader
, const struct arb_ps_compile_args
*args
)
3570 struct arb_ps_compiled_shader
*new_array
;
3571 SHADER_BUFFER buffer
;
3572 struct arb_pshader_private
*shader_data
;
3575 if(!shader
->backend_priv
) {
3576 shader
->backend_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
3578 shader_data
= shader
->backend_priv
;
3580 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
3581 * so a linear search is more performant than a hashmap or a binary search
3582 * (cache coherency etc)
3584 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
3585 if(memcmp(&shader_data
->gl_shaders
[i
].args
, args
, sizeof(*args
)) == 0) {
3586 return &shader_data
->gl_shaders
[i
];
3590 TRACE("No matching GL shader found, compiling a new shader\n");
3591 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
3592 if (shader_data
->num_gl_shaders
)
3594 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
3595 new_array
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, shader_data
->gl_shaders
,
3596 new_size
* sizeof(*shader_data
->gl_shaders
));
3598 new_array
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
->gl_shaders
));
3603 ERR("Out of memory\n");
3606 shader_data
->gl_shaders
= new_array
;
3607 shader_data
->shader_array_size
= new_size
;
3610 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
3612 pixelshader_update_samplers(&shader
->baseShader
.reg_maps
,
3613 ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->stateBlock
->textures
);
3615 shader_buffer_init(&buffer
);
3616 ret
= shader_arb_generate_pshader(shader
, &buffer
, args
,
3617 &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
]);
3618 shader_buffer_free(&buffer
);
3619 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].prgId
= ret
;
3621 return &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++];
3624 static inline BOOL
vs_args_equal(const struct arb_vs_compile_args
*stored
, const struct arb_vs_compile_args
*new,
3625 const DWORD use_map
, BOOL skip_int
) {
3626 if((stored
->super
.swizzle_map
& use_map
) != new->super
.swizzle_map
) return FALSE
;
3627 if(stored
->super
.fog_src
!= new->super
.fog_src
) return FALSE
;
3628 if(stored
->boolclip_compare
!= new->boolclip_compare
) return FALSE
;
3629 if(stored
->ps_signature
!= new->ps_signature
) return FALSE
;
3630 if(stored
->vertex_samplers_compare
!= new->vertex_samplers_compare
) return FALSE
;
3631 if(skip_int
) return TRUE
;
3633 return memcmp(stored
->loop_ctrl
, new->loop_ctrl
, sizeof(stored
->loop_ctrl
)) == 0;
3636 static struct arb_vs_compiled_shader
*find_arb_vshader(IWineD3DVertexShaderImpl
*shader
, const struct arb_vs_compile_args
*args
)
3640 struct arb_vs_compiled_shader
*new_array
;
3641 DWORD use_map
= ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.use_map
;
3642 SHADER_BUFFER buffer
;
3643 struct arb_vshader_private
*shader_data
;
3645 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->adapter
->gl_info
;
3647 if(!shader
->backend_priv
) {
3648 shader
->backend_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
3650 shader_data
= shader
->backend_priv
;
3652 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
3653 * so a linear search is more performant than a hashmap or a binary search
3654 * (cache coherency etc)
3656 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
3657 if(vs_args_equal(&shader_data
->gl_shaders
[i
].args
, args
, use_map
, GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION
))) {
3658 return &shader_data
->gl_shaders
[i
];
3662 TRACE("No matching GL shader found, compiling a new shader\n");
3664 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
3665 if (shader_data
->num_gl_shaders
)
3667 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
3668 new_array
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, shader_data
->gl_shaders
,
3669 new_size
* sizeof(*shader_data
->gl_shaders
));
3671 new_array
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
->gl_shaders
));
3676 ERR("Out of memory\n");
3679 shader_data
->gl_shaders
= new_array
;
3680 shader_data
->shader_array_size
= new_size
;
3683 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
3685 shader_buffer_init(&buffer
);
3686 ret
= shader_arb_generate_vshader(shader
, &buffer
, args
,
3687 &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
]);
3688 shader_buffer_free(&buffer
);
3689 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].prgId
= ret
;
3691 return &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++];
3694 static inline void find_arb_ps_compile_args(IWineD3DPixelShaderImpl
*shader
, IWineD3DStateBlockImpl
*stateblock
,
3695 struct arb_ps_compile_args
*args
)
3699 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->adapter
->gl_info
;
3700 find_ps_compile_args(shader
, stateblock
, &args
->super
);
3702 /* This forces all local boolean constants to 1 to make them stateblock independent */
3703 args
->bools
= shader
->baseShader
.reg_maps
.local_bool_consts
;
3705 for(i
= 0; i
< MAX_CONST_B
; i
++)
3707 if(stateblock
->pixelShaderConstantB
[i
]) args
->bools
|= ( 1 << i
);
3710 /* Skip if unused or local, or supported natively */
3711 int_skip
= ~shader
->baseShader
.reg_maps
.integer_constants
| shader
->baseShader
.reg_maps
.local_int_consts
;
3712 if(int_skip
== 0xffff || GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION
))
3714 memset(&args
->loop_ctrl
, 0, sizeof(args
->loop_ctrl
));
3718 for(i
= 0; i
< MAX_CONST_I
; i
++)
3720 if(int_skip
& (1 << i
))
3722 args
->loop_ctrl
[i
][0] = 0;
3723 args
->loop_ctrl
[i
][1] = 0;
3724 args
->loop_ctrl
[i
][2] = 0;
3728 args
->loop_ctrl
[i
][0] = stateblock
->pixelShaderConstantI
[i
* 4];
3729 args
->loop_ctrl
[i
][1] = stateblock
->pixelShaderConstantI
[i
* 4 + 1];
3730 args
->loop_ctrl
[i
][2] = stateblock
->pixelShaderConstantI
[i
* 4 + 2];
3735 static inline void find_arb_vs_compile_args(IWineD3DVertexShaderImpl
*shader
, IWineD3DStateBlockImpl
*stateblock
,
3736 struct arb_vs_compile_args
*args
)
3740 IWineD3DDeviceImpl
*dev
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
3741 const WineD3D_GL_Info
*gl_info
= &dev
->adapter
->gl_info
;
3742 find_vs_compile_args(shader
, stateblock
, &args
->super
);
3744 /* This forces all local boolean constants to 1 to make them stateblock independent */
3745 args
->boolclip
.bools
= shader
->baseShader
.reg_maps
.local_bool_consts
;
3747 if(use_ps(stateblock
))
3749 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) stateblock
->pixelShader
;
3750 struct arb_pshader_private
*shader_priv
= ps
->backend_priv
;
3751 args
->ps_signature
= shader_priv
->input_signature_idx
;
3753 args
->boolclip
.clip_control
[0] = shader_priv
->clipplane_emulation
;
3757 args
->ps_signature
= ~0;
3758 args
->boolclip
.clip_control
[0] = ffp_clip_emul(stateblock
) ? GL_LIMITS(texture_stages
) : 0;
3761 if(args
->boolclip
.clip_control
[0])
3763 if(stateblock
->renderState
[WINED3DRS_CLIPPING
])
3765 args
->boolclip
.clip_control
[1] = stateblock
->renderState
[WINED3DRS_CLIPPLANEENABLE
];
3769 args
->boolclip
.clip_control
[1] = 0;
3773 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
3774 for(i
= 0; i
< MAX_CONST_B
; i
++)
3776 if(stateblock
->vertexShaderConstantB
[i
]) args
->boolclip
.bools
|= ( 1 << i
);
3779 args
->vertex_samplers
[0] = dev
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 0];
3780 args
->vertex_samplers
[1] = dev
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 1];
3781 args
->vertex_samplers
[2] = dev
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ 2];
3782 args
->vertex_samplers
[3] = 0;
3784 /* Skip if unused or local */
3785 int_skip
= ~shader
->baseShader
.reg_maps
.integer_constants
| shader
->baseShader
.reg_maps
.local_int_consts
;
3786 if(int_skip
== 0xffff || GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION
))
3788 memset(&args
->loop_ctrl
, 0, sizeof(args
->loop_ctrl
));
3792 for(i
= 0; i
< MAX_CONST_I
; i
++)
3794 if(int_skip
& (1 << i
))
3796 args
->loop_ctrl
[i
][0] = 0;
3797 args
->loop_ctrl
[i
][1] = 0;
3798 args
->loop_ctrl
[i
][2] = 0;
3802 args
->loop_ctrl
[i
][0] = stateblock
->vertexShaderConstantI
[i
* 4];
3803 args
->loop_ctrl
[i
][1] = stateblock
->vertexShaderConstantI
[i
* 4 + 1];
3804 args
->loop_ctrl
[i
][2] = stateblock
->vertexShaderConstantI
[i
* 4 + 2];
3809 static void find_clip_texcoord(IWineD3DPixelShaderImpl
*ps
, const WineD3D_GL_Info
*gl_info
)
3811 struct arb_pshader_private
*shader_priv
= ps
->backend_priv
;
3814 /* See if we can use fragment.texcoord[7] for clipplane emulation
3816 * Don't do this if it is not supported, or fragment.texcoord[7] is used
3818 if(ps
->baseShader
.reg_maps
.shader_version
.major
< 3)
3820 for(i
= GL_LIMITS(texture_stages
); i
> 0; i
--)
3822 if(!ps
->baseShader
.reg_maps
.texcoord
[i
- 1])
3824 shader_priv
->clipplane_emulation
= i
;
3831 for(i
= GL_LIMITS(texture_stages
); i
> 0; i
--)
3833 if(!ps
->baseShader
.reg_maps
.input_registers
& (1 << (i
- 1)))
3835 shader_priv
->clipplane_emulation
= i
;
3842 /* GL locking is done by the caller */
3843 static void shader_arb_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
3844 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3845 struct shader_arb_priv
*priv
= This
->shader_priv
;
3846 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3848 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
3850 struct arb_ps_compile_args compile_args
;
3851 struct arb_ps_compiled_shader
*compiled
;
3852 struct arb_pshader_private
*shader_priv
;
3853 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) This
->stateBlock
->pixelShader
;
3855 TRACE("Using pixel shader %p\n", This
->stateBlock
->pixelShader
);
3856 find_arb_ps_compile_args(ps
, This
->stateBlock
, &compile_args
);
3857 compiled
= find_arb_pshader(ps
, &compile_args
);
3858 priv
->current_fprogram_id
= compiled
->prgId
;
3859 priv
->compiled_fprog
= compiled
;
3861 shader_priv
= ps
->backend_priv
;
3862 if(!shader_priv
->has_signature_idx
)
3864 if(ps
->baseShader
.reg_maps
.shader_version
.major
< 3) shader_priv
->input_signature_idx
= ~0;
3865 else shader_priv
->input_signature_idx
= find_input_signature(priv
, ps
->input_signature
);
3867 shader_priv
->has_signature_idx
= TRUE
;
3868 TRACE("Shader got assigned input signature index %u\n", shader_priv
->input_signature_idx
);
3870 if(!This
->vs_clipping
) find_clip_texcoord(ps
, gl_info
);
3873 /* Bind the fragment program */
3874 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
3875 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
3877 if(!priv
->use_arbfp_fixed_func
) {
3878 /* Enable OpenGL fragment programs */
3879 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
3880 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
3882 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
3884 shader_arb_ps_local_constants(This
);
3885 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
) && !priv
->use_arbfp_fixed_func
) {
3886 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
3887 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
3888 * replacement shader
3890 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
3891 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3892 priv
->current_fprogram_id
= 0;
3896 struct arb_vs_compile_args compile_args
;
3897 struct arb_vs_compiled_shader
*compiled
;
3899 TRACE("Using vertex shader %p\n", This
->stateBlock
->vertexShader
);
3900 find_arb_vs_compile_args((IWineD3DVertexShaderImpl
*) This
->stateBlock
->vertexShader
, This
->stateBlock
, &compile_args
);
3901 compiled
= find_arb_vshader((IWineD3DVertexShaderImpl
*) This
->stateBlock
->vertexShader
, &compile_args
);
3902 priv
->current_vprogram_id
= compiled
->prgId
;
3903 priv
->compiled_vprog
= compiled
;
3905 /* Bind the vertex program */
3906 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
3907 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
3909 /* Enable OpenGL vertex programs */
3910 glEnable(GL_VERTEX_PROGRAM_ARB
);
3911 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
3912 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
3913 shader_arb_vs_local_constants(This
);
3914 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM
)) {
3915 priv
->current_vprogram_id
= 0;
3916 glDisable(GL_VERTEX_PROGRAM_ARB
);
3917 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
3921 /* GL locking is done by the caller */
3922 static void shader_arb_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {
3923 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3924 struct shader_arb_priv
*priv
= This
->shader_priv
;
3925 GLuint
*blt_fprogram
= &priv
->depth_blt_fprogram_id
[tex_type
];
3926 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3928 if (!priv
->depth_blt_vprogram_id
) priv
->depth_blt_vprogram_id
= create_arb_blt_vertex_program(gl_info
);
3929 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->depth_blt_vprogram_id
));
3930 glEnable(GL_VERTEX_PROGRAM_ARB
);
3932 if (!*blt_fprogram
) *blt_fprogram
= create_arb_blt_fragment_program(gl_info
, tex_type
);
3933 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, *blt_fprogram
));
3934 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
3937 /* GL locking is done by the caller */
3938 static void shader_arb_deselect_depth_blt(IWineD3DDevice
*iface
) {
3939 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3940 struct shader_arb_priv
*priv
= This
->shader_priv
;
3941 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3943 if (priv
->current_vprogram_id
) {
3944 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, priv
->current_vprogram_id
));
3945 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
3947 glEnable(GL_VERTEX_PROGRAM_ARB
);
3948 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
3950 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This
, priv
->current_vprogram_id
);
3952 glDisable(GL_VERTEX_PROGRAM_ARB
);
3953 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
3956 if (priv
->current_fprogram_id
) {
3957 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, priv
->current_fprogram_id
));
3958 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
3960 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
3961 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
3963 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This
, priv
->current_fprogram_id
);
3965 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
3966 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3970 static void shader_arb_destroy(IWineD3DBaseShader
*iface
) {
3971 IWineD3DBaseShaderImpl
*baseShader
= (IWineD3DBaseShaderImpl
*) iface
;
3972 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)baseShader
->baseShader
.device
;
3973 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
3975 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
3977 if (shader_is_pshader_version(baseShader
->baseShader
.reg_maps
.shader_version
.type
))
3979 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*) iface
;
3980 struct arb_pshader_private
*shader_data
= This
->backend_priv
;
3983 if(!shader_data
) return; /* This can happen if a shader was never compiled */
3985 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
3986 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data
->gl_shaders
[i
].prgId
));
3987 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
3990 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
3991 HeapFree(GetProcessHeap(), 0, shader_data
);
3992 This
->backend_priv
= NULL
;
3994 IWineD3DVertexShaderImpl
*This
= (IWineD3DVertexShaderImpl
*) iface
;
3995 struct arb_vshader_private
*shader_data
= This
->backend_priv
;
3998 if(!shader_data
) return; /* This can happen if a shader was never compiled */
4000 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4001 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data
->gl_shaders
[i
].prgId
));
4002 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4005 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4006 HeapFree(GetProcessHeap(), 0, shader_data
);
4007 This
->backend_priv
= NULL
;
4011 static int sig_tree_compare(const void *key
, const struct wine_rb_entry
*entry
)
4013 struct ps_signature
*e
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
4014 return compare_sig(key
, e
->sig
);
4017 struct wine_rb_functions sig_tree_functions
=
4025 static HRESULT
shader_arb_alloc(IWineD3DDevice
*iface
) {
4026 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4027 struct shader_arb_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*priv
));
4028 if(wine_rb_init(&priv
->signature_tree
, &sig_tree_functions
) == -1)
4030 ERR("RB tree init failed\n");
4031 HeapFree(GetProcessHeap(), 0, priv
);
4032 return E_OUTOFMEMORY
;
4034 This
->shader_priv
= priv
;
4038 static void release_signature(struct wine_rb_entry
*entry
, void *context
)
4040 struct ps_signature
*sig
= WINE_RB_ENTRY_VALUE(entry
, struct ps_signature
, entry
);
4042 for(i
= 0; i
< MAX_REG_INPUT
; i
++)
4044 HeapFree(GetProcessHeap(), 0, (char *) sig
->sig
[i
].semantic_name
);
4046 HeapFree(GetProcessHeap(), 0, sig
->sig
);
4047 HeapFree(GetProcessHeap(), 0, sig
);
4050 static void shader_arb_free(IWineD3DDevice
*iface
) {
4051 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4052 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4053 struct shader_arb_priv
*priv
= This
->shader_priv
;
4057 if(priv
->depth_blt_vprogram_id
) {
4058 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_vprogram_id
));
4060 for (i
= 0; i
< tex_type_count
; ++i
) {
4061 if (priv
->depth_blt_fprogram_id
[i
]) {
4062 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->depth_blt_fprogram_id
[i
]));
4067 wine_rb_destroy(&priv
->signature_tree
, release_signature
, NULL
);
4068 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
4071 static BOOL
shader_arb_dirty_const(IWineD3DDevice
*iface
) {
4075 static void shader_arb_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
)
4077 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
4078 * then overwrite the shader specific ones
4080 none_shader_backend
.shader_get_caps(devtype
, gl_info
, pCaps
);
4082 if(GL_SUPPORT(ARB_VERTEX_PROGRAM
)) {
4083 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(1,1);
4084 TRACE_(d3d_caps
)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
4085 pCaps
->MaxVertexShaderConst
= GL_LIMITS(vshader_constantsF
) - 1;
4088 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM
)) {
4089 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(1,4);
4090 pCaps
->PixelShader1xMaxValue
= 8.0;
4091 TRACE_(d3d_caps
)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
4092 pCaps
->MaxPixelShaderConst
= GL_LIMITS(pshader_constantsF
);
4095 pCaps
->VSClipping
= GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION
);
4098 static BOOL
shader_arb_color_fixup_supported(struct color_fixup_desc fixup
)
4100 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
4102 TRACE("Checking support for color_fixup:\n");
4103 dump_color_fixup_desc(fixup
);
4106 /* We support everything except YUV conversions. */
4107 if (!is_yuv_fixup(fixup
))
4113 TRACE("[FAILED]\n");
4117 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
) {
4119 char write_mask
[20], regstr
[50];
4120 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
4121 BOOL is_color
= FALSE
;
4122 const struct wined3d_shader_dst_param
*dst
;
4124 if (!ins
->dst_count
) return;
4128 if(shift
== 0) return; /* Saturate alone is handled by the instructions */
4130 shader_arb_get_write_mask(ins
, dst
, write_mask
);
4131 shader_arb_get_register_name(ins
, &dst
->reg
, regstr
, &is_color
);
4133 /* Generate a line that does the output modifier computation
4134 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
4135 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
4137 shader_addline(buffer
, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins
),
4138 regstr
, write_mask
, regstr
, shift_tab
[shift
]);
4141 static const SHADER_HANDLER shader_arb_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
4143 /* WINED3DSIH_ABS */ shader_hw_map2gl
,
4144 /* WINED3DSIH_ADD */ shader_hw_map2gl
,
4145 /* WINED3DSIH_BEM */ pshader_hw_bem
,
4146 /* WINED3DSIH_BREAK */ shader_hw_break
,
4147 /* WINED3DSIH_BREAKC */ shader_hw_breakc
,
4148 /* WINED3DSIH_BREAKP */ NULL
,
4149 /* WINED3DSIH_CALL */ NULL
,
4150 /* WINED3DSIH_CALLNZ */ NULL
,
4151 /* WINED3DSIH_CMP */ pshader_hw_cmp
,
4152 /* WINED3DSIH_CND */ pshader_hw_cnd
,
4153 /* WINED3DSIH_CRS */ shader_hw_map2gl
,
4154 /* WINED3DSIH_DCL */ NULL
,
4155 /* WINED3DSIH_DEF */ NULL
,
4156 /* WINED3DSIH_DEFB */ NULL
,
4157 /* WINED3DSIH_DEFI */ NULL
,
4158 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add
,
4159 /* WINED3DSIH_DP3 */ shader_hw_map2gl
,
4160 /* WINED3DSIH_DP4 */ shader_hw_map2gl
,
4161 /* WINED3DSIH_DST */ shader_hw_map2gl
,
4162 /* WINED3DSIH_DSX */ shader_hw_map2gl
,
4163 /* WINED3DSIH_DSY */ shader_hw_dsy
,
4164 /* WINED3DSIH_ELSE */ shader_hw_else
,
4165 /* WINED3DSIH_ENDIF */ shader_hw_endif
,
4166 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop
,
4167 /* WINED3DSIH_ENDREP */ shader_hw_endrep
,
4168 /* WINED3DSIH_EXP */ shader_hw_map2gl
,
4169 /* WINED3DSIH_EXPP */ shader_hw_map2gl
,
4170 /* WINED3DSIH_FRC */ shader_hw_map2gl
,
4171 /* WINED3DSIH_IF */ NULL
/* Hardcoded into the shader */,
4172 /* WINED3DSIH_IFC */ shader_hw_ifc
,
4173 /* WINED3DSIH_LABEL */ NULL
,
4174 /* WINED3DSIH_LIT */ shader_hw_map2gl
,
4175 /* WINED3DSIH_LOG */ shader_hw_map2gl
,
4176 /* WINED3DSIH_LOGP */ shader_hw_map2gl
,
4177 /* WINED3DSIH_LOOP */ shader_hw_loop
,
4178 /* WINED3DSIH_LRP */ shader_hw_lrp
,
4179 /* WINED3DSIH_M3x2 */ shader_hw_mnxn
,
4180 /* WINED3DSIH_M3x3 */ shader_hw_mnxn
,
4181 /* WINED3DSIH_M3x4 */ shader_hw_mnxn
,
4182 /* WINED3DSIH_M4x3 */ shader_hw_mnxn
,
4183 /* WINED3DSIH_M4x4 */ shader_hw_mnxn
,
4184 /* WINED3DSIH_MAD */ shader_hw_map2gl
,
4185 /* WINED3DSIH_MAX */ shader_hw_map2gl
,
4186 /* WINED3DSIH_MIN */ shader_hw_map2gl
,
4187 /* WINED3DSIH_MOV */ shader_hw_mov
,
4188 /* WINED3DSIH_MOVA */ shader_hw_mov
,
4189 /* WINED3DSIH_MUL */ shader_hw_map2gl
,
4190 /* WINED3DSIH_NOP */ shader_hw_nop
,
4191 /* WINED3DSIH_NRM */ shader_hw_nrm
,
4192 /* WINED3DSIH_PHASE */ NULL
,
4193 /* WINED3DSIH_POW */ shader_hw_map2gl
,
4194 /* WINED3DSIH_RCP */ shader_hw_rsq_rcp
,
4195 /* WINED3DSIH_REP */ shader_hw_rep
,
4196 /* WINED3DSIH_RET */ NULL
,
4197 /* WINED3DSIH_RSQ */ shader_hw_rsq_rcp
,
4198 /* WINED3DSIH_SETP */ NULL
,
4199 /* WINED3DSIH_SGE */ shader_hw_map2gl
,
4200 /* WINED3DSIH_SGN */ shader_hw_sgn
,
4201 /* WINED3DSIH_SINCOS */ shader_hw_sincos
,
4202 /* WINED3DSIH_SLT */ shader_hw_map2gl
,
4203 /* WINED3DSIH_SUB */ shader_hw_map2gl
,
4204 /* WINED3DSIH_TEX */ pshader_hw_tex
,
4205 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem
,
4206 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem
,
4207 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord
,
4208 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth
,
4209 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3
,
4210 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex
,
4211 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill
,
4212 /* WINED3DSIH_TEXLDD */ shader_hw_texldd
,
4213 /* WINED3DSIH_TEXLDL */ shader_hw_texldl
,
4214 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth
,
4215 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad
,
4216 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex
,
4217 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3
,
4218 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
4219 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad
,
4220 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec
,
4221 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex
,
4222 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec
,
4223 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar
,
4224 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb
,
4225 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb
,
4228 static inline BOOL
get_bool_const(const struct wined3d_shader_instruction
*ins
, IWineD3DBaseShaderImpl
*This
, DWORD idx
)
4230 BOOL vshader
= shader_is_vshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
4232 WORD flag
= (1 << idx
);
4233 const local_constant
*constant
;
4234 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4236 if(This
->baseShader
.reg_maps
.local_bool_consts
& flag
)
4238 /* What good is a if(bool) with a hardcoded local constant? I don't know, but handle it */
4239 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsB
, local_constant
, entry
)
4241 if (constant
->idx
== idx
)
4243 return constant
->value
[0];
4246 ERR("Local constant not found\n");
4251 if(vshader
) bools
= priv
->cur_vs_args
->boolclip
.bools
;
4252 else bools
= priv
->cur_ps_args
->bools
;
4253 return bools
& flag
;
4257 static inline void get_int_const(const struct wined3d_shader_instruction
*ins
, IWineD3DBaseShaderImpl
*This
, DWORD idx
, int *ret
)
4259 BOOL vshader
= shader_is_vshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
4260 WORD flag
= (1 << idx
);
4261 const local_constant
*constant
;
4262 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4264 /* Integer constants can either be a local constant, or they can be stored in the shader
4265 * type specific compile args
4267 if(This
->baseShader
.reg_maps
.local_int_consts
& flag
)
4269 LIST_FOR_EACH_ENTRY(constant
, &This
->baseShader
.constantsI
, local_constant
, entry
)
4271 if (constant
->idx
== idx
)
4273 ret
[0] = constant
->value
[0];
4274 ret
[1] = constant
->value
[1];
4275 /* Step / stride is signed */
4276 ret
[2] = (int) constant
->value
[2];
4280 /* If this happens the flag was set incorrectly */
4281 ERR("Local constant not found\n");
4291 /* Count and aL start value are unsigned */
4292 ret
[0] = priv
->cur_vs_args
->loop_ctrl
[idx
][0];
4293 ret
[1] = priv
->cur_vs_args
->loop_ctrl
[idx
][1];
4294 /* The step/stride is signed */
4295 ret
[2] = ((char) priv
->cur_vs_args
->loop_ctrl
[idx
][2]);
4299 ret
[0] = priv
->cur_ps_args
->loop_ctrl
[idx
][0];
4300 ret
[1] = priv
->cur_ps_args
->loop_ctrl
[idx
][1];
4301 ret
[2] = ((char) priv
->cur_ps_args
->loop_ctrl
[idx
][2]);
4307 static void record_instruction(struct list
*list
, const struct wined3d_shader_instruction
*ins
)
4310 struct wined3d_shader_dst_param
*dst_param
= NULL
;
4311 struct wined3d_shader_src_param
*src_param
= NULL
, *rel_addr
= NULL
;
4312 struct recorded_instruction
*rec
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*rec
));
4315 ERR("Out of memory\n");
4320 dst_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param
));
4321 if(!dst_param
) goto free
;
4322 *dst_param
= *ins
->dst
;
4323 if(ins
->dst
->reg
.rel_addr
)
4325 rel_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param
->reg
.rel_addr
));
4326 if(!rel_addr
) goto free
;
4327 *rel_addr
= *ins
->dst
->reg
.rel_addr
;
4328 dst_param
->reg
.rel_addr
= rel_addr
;
4330 rec
->ins
.dst
= dst_param
;
4332 src_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*src_param
) * ins
->src_count
);
4333 if(!src_param
) goto free
;
4334 for(i
= 0; i
< ins
->src_count
; i
++)
4336 src_param
[i
] = ins
->src
[i
];
4337 if(ins
->src
[i
].reg
.rel_addr
)
4339 rel_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr
));
4340 if(!rel_addr
) goto free
;
4341 *rel_addr
= *ins
->src
[i
].reg
.rel_addr
;
4342 src_param
[i
].reg
.rel_addr
= rel_addr
;
4345 rec
->ins
.src
= src_param
;
4346 list_add_tail(list
, &rec
->entry
);
4350 ERR("Out of memory\n");
4353 HeapFree(GetProcessHeap(), 0, (void *) dst_param
->reg
.rel_addr
);
4354 HeapFree(GetProcessHeap(), 0, dst_param
);
4358 for(i
= 0; i
< ins
->src_count
; i
++)
4360 HeapFree(GetProcessHeap(), 0, (void *) src_param
[i
].reg
.rel_addr
);
4362 HeapFree(GetProcessHeap(), 0, src_param
);
4364 HeapFree(GetProcessHeap(), 0, rec
);
4367 static void free_recorded_instruction(struct list
*list
)
4369 struct recorded_instruction
*rec_ins
, *entry2
;
4372 LIST_FOR_EACH_ENTRY_SAFE(rec_ins
, entry2
, list
, struct recorded_instruction
, entry
)
4374 list_remove(&rec_ins
->entry
);
4375 if(rec_ins
->ins
.dst
)
4377 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.dst
->reg
.rel_addr
);
4378 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.dst
);
4380 if(rec_ins
->ins
.src
)
4382 for(i
= 0; i
< rec_ins
->ins
.src_count
; i
++)
4384 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.src
[i
].reg
.rel_addr
);
4386 HeapFree(GetProcessHeap(), 0, (void *) rec_ins
->ins
.src
);
4388 HeapFree(GetProcessHeap(), 0, rec_ins
);
4392 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
4393 SHADER_HANDLER hw_fct
;
4394 struct shader_arb_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4395 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
4396 struct control_frame
*control_frame
;
4397 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
4399 if(ins
->handler_idx
== WINED3DSIH_LOOP
|| ins
->handler_idx
== WINED3DSIH_REP
)
4401 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
4402 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
4404 if(ins
->handler_idx
== WINED3DSIH_LOOP
) control_frame
->type
= LOOP
;
4405 if(ins
->handler_idx
== WINED3DSIH_REP
) control_frame
->type
= REP
;
4407 if(priv
->target_version
>= NV2
)
4409 control_frame
->loop_no
= priv
->num_loops
++;
4414 /* Don't bother recording when we're in a not used if branch */
4420 if(!priv
->recording
)
4422 int control_values
[3];
4423 get_int_const(ins
, This
, ins
->src
[0].reg
.idx
, control_values
);
4424 list_init(&priv
->record
);
4425 priv
->recording
= TRUE
;
4426 control_frame
->outer_loop
= TRUE
;
4427 control_frame
->loop_control
[0] = control_values
[0];
4428 control_frame
->loop_control
[1] = control_values
[1];
4429 control_frame
->loop_control
[2] = control_values
[2];
4430 return; /* Instruction is handled */
4432 /* Record this loop in the outer loop's recording */
4435 else if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
|| ins
->handler_idx
== WINED3DSIH_ENDREP
)
4437 if(priv
->target_version
>= NV2
)
4439 /* Nothing to do. The control frame is popped after the HW instr handler */
4443 struct list
*e
= list_head(&priv
->control_frames
);
4444 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
4445 list_remove(&control_frame
->entry
);
4447 if(control_frame
->outer_loop
)
4449 int iteration
, aL
= 0;
4452 /* Turn off recording before playback */
4453 priv
->recording
= FALSE
;
4455 /* Move the recorded instructions to a separate list and get them out of the private data
4456 * structure. If there are nested loops, the shader_arb_handle_instruction below will
4457 * be recorded again, thus priv->record might be overwritten
4460 list_move_tail(©
, &priv
->record
);
4461 list_init(&priv
->record
);
4463 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4465 shader_addline(buffer
, "#unrolling loop: %d iterations, aL=%d, inc %d\n",
4466 control_frame
->loop_control
[0], control_frame
->loop_control
[1],
4467 control_frame
->loop_control
[2]);
4468 aL
= control_frame
->loop_control
[1];
4472 shader_addline(buffer
, "#unrolling rep: %d iterations\n", control_frame
->loop_control
[0]);
4475 for(iteration
= 0; iteration
< control_frame
->loop_control
[0]; iteration
++)
4477 struct recorded_instruction
*rec_ins
;
4478 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4481 shader_addline(buffer
, "#Iteration %d, aL=%d\n", iteration
, aL
);
4485 shader_addline(buffer
, "#Iteration %d\n", iteration
);
4488 LIST_FOR_EACH_ENTRY(rec_ins
, ©
, struct recorded_instruction
, entry
)
4490 shader_arb_handle_instruction(&rec_ins
->ins
);
4493 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4495 aL
+= control_frame
->loop_control
[2];
4498 shader_addline(buffer
, "#end loop/rep\n");
4500 free_recorded_instruction(©
);
4501 HeapFree(GetProcessHeap(), 0, control_frame
);
4502 return; /* Instruction is handled */
4506 /* This is a nested loop. Proceed to the normal recording function */
4507 HeapFree(GetProcessHeap(), 0, control_frame
);
4514 record_instruction(&priv
->record
, ins
);
4519 if(ins
->handler_idx
== WINED3DSIH_IF
)
4521 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
4522 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
4523 control_frame
->type
= IF
;
4525 if(!priv
->muted
&& get_bool_const(ins
, This
, ins
->src
[0].reg
.idx
) == FALSE
)
4527 shader_addline(buffer
, "#if(FALSE){\n");
4529 control_frame
->muting
= TRUE
;
4531 else shader_addline(buffer
, "#if(TRUE) {\n");
4533 return; /* Instruction is handled */
4535 else if(ins
->handler_idx
== WINED3DSIH_IFC
)
4537 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
4538 control_frame
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*control_frame
));
4539 control_frame
->type
= IFC
;
4540 control_frame
->ifc_no
= priv
->num_ifcs
++;
4541 list_add_head(&priv
->control_frames
, &control_frame
->entry
);
4543 else if(ins
->handler_idx
== WINED3DSIH_ELSE
)
4545 struct list
*e
= list_head(&priv
->control_frames
);
4546 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
4548 if(control_frame
->type
== IF
)
4550 shader_addline(buffer
, "#} else {\n");
4551 if(!priv
->muted
&& !control_frame
->muting
)
4554 control_frame
->muting
= TRUE
;
4556 else if(control_frame
->muting
) priv
->muted
= FALSE
;
4557 return; /* Instruction is handled. */
4559 /* In case of an ifc, generate a HW shader instruction */
4561 else if(ins
->handler_idx
== WINED3DSIH_ENDIF
)
4563 struct list
*e
= list_head(&priv
->control_frames
);
4564 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
4566 if(control_frame
->type
== IF
)
4568 shader_addline(buffer
, "#} endif\n");
4569 if(control_frame
->muting
) priv
->muted
= FALSE
;
4570 list_remove(&control_frame
->entry
);
4571 HeapFree(GetProcessHeap(), 0, control_frame
);
4572 return; /* Instruction is handled */
4576 if(priv
->muted
) return;
4578 /* Select handler */
4579 hw_fct
= shader_arb_instruction_handler_table
[ins
->handler_idx
];
4581 /* Unhandled opcode */
4584 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
4589 if(ins
->handler_idx
== WINED3DSIH_ENDLOOP
|| ins
->handler_idx
== WINED3DSIH_ENDREP
)
4591 struct list
*e
= list_head(&priv
->control_frames
);
4592 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
4593 list_remove(&control_frame
->entry
);
4594 HeapFree(GetProcessHeap(), 0, control_frame
);
4597 else if(ins
->handler_idx
== WINED3DSIH_ENDIF
)
4599 /* Non-ifc ENDIFs don't reach that place because of the return in the if block above */
4600 struct list
*e
= list_head(&priv
->control_frames
);
4601 control_frame
= LIST_ENTRY(e
, struct control_frame
, entry
);
4602 list_remove(&control_frame
->entry
);
4603 HeapFree(GetProcessHeap(), 0, control_frame
);
4607 shader_arb_add_instruction_modifiers(ins
);
4610 const shader_backend_t arb_program_shader_backend
= {
4611 shader_arb_handle_instruction
,
4613 shader_arb_select_depth_blt
,
4614 shader_arb_deselect_depth_blt
,
4615 shader_arb_update_float_vertex_constants
,
4616 shader_arb_update_float_pixel_constants
,
4617 shader_arb_load_constants
,
4618 shader_arb_load_np2fixup_constants
,
4622 shader_arb_dirty_const
,
4623 shader_arb_get_caps
,
4624 shader_arb_color_fixup_supported
,
4627 /* ARB_fragment_program fixed function pipeline replacement definitions */
4628 #define ARB_FFP_CONST_TFACTOR 0
4629 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
4630 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
4631 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
4632 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
4634 struct arbfp_ffp_desc
4636 struct ffp_frag_desc parent
;
4638 unsigned int num_textures_used
;
4641 static void arbfp_enable(IWineD3DDevice
*iface
, BOOL enable
) {
4644 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
4645 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
4647 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
4648 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4653 static HRESULT
arbfp_alloc(IWineD3DDevice
*iface
) {
4654 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
4655 struct shader_arb_priv
*priv
;
4656 /* Share private data between the shader backend and the pipeline replacement, if both
4657 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
4658 * if no pixel shader is bound or not
4660 if(This
->shader_backend
== &arb_program_shader_backend
) {
4661 This
->fragment_priv
= This
->shader_priv
;
4663 This
->fragment_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_arb_priv
));
4664 if(!This
->fragment_priv
) return E_OUTOFMEMORY
;
4666 priv
= This
->fragment_priv
;
4667 if (wine_rb_init(&priv
->fragment_shaders
, &wined3d_ffp_frag_program_rb_functions
) == -1)
4669 ERR("Failed to initialize rbtree.\n");
4670 HeapFree(GetProcessHeap(), 0, This
->fragment_priv
);
4671 return E_OUTOFMEMORY
;
4673 priv
->use_arbfp_fixed_func
= TRUE
;
4677 static void arbfp_free_ffpshader(struct wine_rb_entry
*entry
, void *context
)
4679 const WineD3D_GL_Info
*gl_info
= context
;
4680 struct arbfp_ffp_desc
*entry_arb
= WINE_RB_ENTRY_VALUE(entry
, struct arbfp_ffp_desc
, parent
.entry
);
4683 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb
->shader
));
4684 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
4685 HeapFree(GetProcessHeap(), 0, entry_arb
);
4689 static void arbfp_free(IWineD3DDevice
*iface
) {
4690 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
4691 struct shader_arb_priv
*priv
= This
->fragment_priv
;
4693 wine_rb_destroy(&priv
->fragment_shaders
, arbfp_free_ffpshader
, &This
->adapter
->gl_info
);
4694 priv
->use_arbfp_fixed_func
= FALSE
;
4696 if(This
->shader_backend
!= &arb_program_shader_backend
) {
4697 HeapFree(GetProcessHeap(), 0, This
->fragment_priv
);
4701 static void arbfp_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct fragment_caps
*caps
)
4703 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
|
4704 WINED3DTEXOPCAPS_SELECTARG1
|
4705 WINED3DTEXOPCAPS_SELECTARG2
|
4706 WINED3DTEXOPCAPS_MODULATE4X
|
4707 WINED3DTEXOPCAPS_MODULATE2X
|
4708 WINED3DTEXOPCAPS_MODULATE
|
4709 WINED3DTEXOPCAPS_ADDSIGNED2X
|
4710 WINED3DTEXOPCAPS_ADDSIGNED
|
4711 WINED3DTEXOPCAPS_ADD
|
4712 WINED3DTEXOPCAPS_SUBTRACT
|
4713 WINED3DTEXOPCAPS_ADDSMOOTH
|
4714 WINED3DTEXOPCAPS_BLENDCURRENTALPHA
|
4715 WINED3DTEXOPCAPS_BLENDFACTORALPHA
|
4716 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
|
4717 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
|
4718 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
|
4719 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
|
4720 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
|
4721 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
|
4722 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
|
4723 WINED3DTEXOPCAPS_DOTPRODUCT3
|
4724 WINED3DTEXOPCAPS_MULTIPLYADD
|
4725 WINED3DTEXOPCAPS_LERP
|
4726 WINED3DTEXOPCAPS_BUMPENVMAP
|
4727 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
4729 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
4731 caps
->MaxTextureBlendStages
= 8;
4732 caps
->MaxSimultaneousTextures
= min(GL_LIMITS(fragment_samplers
), 8);
4734 caps
->PrimitiveMiscCaps
|= WINED3DPMISCCAPS_TSSARGTEMP
;
4736 #undef GLINFO_LOCATION
4738 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
4739 static void state_texfactor_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
4741 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
4743 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
4744 * application provided constants
4746 if(device
->shader_backend
== &arb_program_shader_backend
) {
4747 if (use_ps(stateblock
)) return;
4749 device
= stateblock
->wineD3DDevice
;
4750 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_TFACTOR
] = 1;
4751 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_TFACTOR
+ 1);
4754 D3DCOLORTOGLFLOAT4(stateblock
->renderState
[WINED3DRS_TEXTUREFACTOR
], col
);
4755 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_TFACTOR
, col
));
4756 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
4760 static void state_arb_specularenable(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
4762 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
4764 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
4765 * application provided constants
4767 if(device
->shader_backend
== &arb_program_shader_backend
) {
4768 if (use_ps(stateblock
)) return;
4770 device
= stateblock
->wineD3DDevice
;
4771 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_SPECULAR_ENABLE
] = 1;
4772 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_SPECULAR_ENABLE
+ 1);
4775 if(stateblock
->renderState
[WINED3DRS_SPECULARENABLE
]) {
4776 /* The specular color has no alpha */
4777 col
[0] = 1.0; col
[1] = 1.0;
4778 col
[2] = 1.0; col
[3] = 0.0;
4780 col
[0] = 0.0; col
[1] = 0.0;
4781 col
[2] = 0.0; col
[3] = 0.0;
4783 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_SPECULAR_ENABLE
, col
));
4784 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
4787 static void set_bumpmat_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
4788 DWORD stage
= (state
- STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE
+ 1);
4789 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
4792 if (use_ps(stateblock
))
4795 ((IWineD3DPixelShaderImpl
*) stateblock
->pixelShader
)->baseShader
.reg_maps
.bumpmat
[stage
]) {
4796 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
4799 if(!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
)) {
4800 device
->StateTable
[STATE_PIXELSHADERCONSTANT
].apply(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
4804 if(device
->shader_backend
== &arb_program_shader_backend
) {
4805 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
4808 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
4809 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_BUMPMAT(stage
)] = 1;
4810 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_BUMPMAT(stage
) + 1);
4813 mat
[0][0] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT00
]);
4814 mat
[0][1] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT01
]);
4815 mat
[1][0] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT10
]);
4816 mat
[1][1] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVMAT11
]);
4818 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_BUMPMAT(stage
), &mat
[0][0]));
4819 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
4822 static void tex_bumpenvlum_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
4823 DWORD stage
= (state
- STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE
+ 1);
4824 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
4827 if (use_ps(stateblock
))
4830 ((IWineD3DPixelShaderImpl
*) stateblock
->pixelShader
)->baseShader
.reg_maps
.luminanceparams
[stage
]) {
4831 /* The pixel shader has to know the luminance offset. Do a constants update if it
4832 * isn't scheduled anyway
4834 if(!isStateDirty(context
, STATE_PIXELSHADERCONSTANT
)) {
4835 device
->StateTable
[STATE_PIXELSHADERCONSTANT
].apply(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
4839 if(device
->shader_backend
== &arb_program_shader_backend
) {
4840 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
4843 } else if(device
->shader_backend
== &arb_program_shader_backend
) {
4844 device
->activeContext
->pshader_const_dirty
[ARB_FFP_CONST_LUMINANCE(stage
)] = 1;
4845 device
->highest_dirty_ps_const
= max(device
->highest_dirty_ps_const
, ARB_FFP_CONST_LUMINANCE(stage
) + 1);
4848 param
[0] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
]);
4849 param
[1] = *((float *) &stateblock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
]);
4853 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, ARB_FFP_CONST_LUMINANCE(stage
), param
));
4854 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
4857 static const char *get_argreg(SHADER_BUFFER
*buffer
, DWORD argnum
, unsigned int stage
, DWORD arg
) {
4860 if(arg
== ARG_UNUSED
) return "unused"; /* This is the marker for unused registers */
4862 switch(arg
& WINED3DTA_SELECTMASK
) {
4863 case WINED3DTA_DIFFUSE
:
4864 ret
= "fragment.color.primary"; break;
4866 case WINED3DTA_CURRENT
:
4867 if(stage
== 0) ret
= "fragment.color.primary";
4871 case WINED3DTA_TEXTURE
:
4873 case 0: ret
= "tex0"; break;
4874 case 1: ret
= "tex1"; break;
4875 case 2: ret
= "tex2"; break;
4876 case 3: ret
= "tex3"; break;
4877 case 4: ret
= "tex4"; break;
4878 case 5: ret
= "tex5"; break;
4879 case 6: ret
= "tex6"; break;
4880 case 7: ret
= "tex7"; break;
4881 default: ret
= "unknown texture";
4885 case WINED3DTA_TFACTOR
:
4886 ret
= "tfactor"; break;
4888 case WINED3DTA_SPECULAR
:
4889 ret
= "fragment.color.secondary"; break;
4891 case WINED3DTA_TEMP
:
4892 ret
= "tempreg"; break;
4894 case WINED3DTA_CONSTANT
:
4895 FIXME("Implement perstage constants\n");
4897 case 0: ret
= "const0"; break;
4898 case 1: ret
= "const1"; break;
4899 case 2: ret
= "const2"; break;
4900 case 3: ret
= "const3"; break;
4901 case 4: ret
= "const4"; break;
4902 case 5: ret
= "const5"; break;
4903 case 6: ret
= "const6"; break;
4904 case 7: ret
= "const7"; break;
4905 default: ret
= "unknown constant";
4913 if(arg
& WINED3DTA_COMPLEMENT
) {
4914 shader_addline(buffer
, "SUB arg%u, const.x, %s;\n", argnum
, ret
);
4915 if(argnum
== 0) ret
= "arg0";
4916 if(argnum
== 1) ret
= "arg1";
4917 if(argnum
== 2) ret
= "arg2";
4919 if(arg
& WINED3DTA_ALPHAREPLICATE
) {
4920 shader_addline(buffer
, "MOV arg%u, %s.w;\n", argnum
, ret
);
4921 if(argnum
== 0) ret
= "arg0";
4922 if(argnum
== 1) ret
= "arg1";
4923 if(argnum
== 2) ret
= "arg2";
4928 static void gen_ffp_instr(SHADER_BUFFER
*buffer
, unsigned int stage
, BOOL color
, BOOL alpha
,
4929 DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
) {
4930 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
4931 unsigned int mul
= 1;
4932 BOOL mul_final_dest
= FALSE
;
4934 if(color
&& alpha
) dstmask
= "";
4935 else if(color
) dstmask
= ".xyz";
4936 else dstmask
= ".w";
4938 if(dst
== tempreg
) dstreg
= "tempreg";
4939 else dstreg
= "ret";
4941 arg0
= get_argreg(buffer
, 0, stage
, dw_arg0
);
4942 arg1
= get_argreg(buffer
, 1, stage
, dw_arg1
);
4943 arg2
= get_argreg(buffer
, 2, stage
, dw_arg2
);
4946 case WINED3DTOP_DISABLE
:
4947 if(stage
== 0) shader_addline(buffer
, "MOV %s%s, fragment.color.primary;\n", dstreg
, dstmask
);
4950 case WINED3DTOP_SELECTARG2
:
4952 case WINED3DTOP_SELECTARG1
:
4953 shader_addline(buffer
, "MOV %s%s, %s;\n", dstreg
, dstmask
, arg1
);
4956 case WINED3DTOP_MODULATE4X
:
4958 case WINED3DTOP_MODULATE2X
:
4960 if(strcmp(dstreg
, "result.color") == 0) {
4962 mul_final_dest
= TRUE
;
4964 case WINED3DTOP_MODULATE
:
4965 shader_addline(buffer
, "MUL %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
4968 case WINED3DTOP_ADDSIGNED2X
:
4970 if(strcmp(dstreg
, "result.color") == 0) {
4972 mul_final_dest
= TRUE
;
4974 case WINED3DTOP_ADDSIGNED
:
4975 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
4977 case WINED3DTOP_ADD
:
4978 shader_addline(buffer
, "ADD_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
4981 case WINED3DTOP_SUBTRACT
:
4982 shader_addline(buffer
, "SUB_SAT %s%s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
);
4985 case WINED3DTOP_ADDSMOOTH
:
4986 shader_addline(buffer
, "SUB arg1, const.x, %s;\n", arg1
);
4987 shader_addline(buffer
, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
4990 case WINED3DTOP_BLENDCURRENTALPHA
:
4991 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
4992 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
4994 case WINED3DTOP_BLENDFACTORALPHA
:
4995 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
4996 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
4998 case WINED3DTOP_BLENDTEXTUREALPHA
:
4999 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5000 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5002 case WINED3DTOP_BLENDDIFFUSEALPHA
:
5003 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
5004 shader_addline(buffer
, "LRP %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5007 case WINED3DTOP_BLENDTEXTUREALPHAPM
:
5008 arg0
= get_argreg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
5009 shader_addline(buffer
, "SUB arg0.w, const.x, %s.w;\n", arg0
);
5010 shader_addline(buffer
, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5013 /* D3DTOP_PREMODULATE ???? */
5015 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR
:
5016 shader_addline(buffer
, "SUB arg0.w, const.x, %s;\n", arg1
);
5017 shader_addline(buffer
, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg
, dstmask
, arg2
, arg1
);
5019 case WINED3DTOP_MODULATEALPHA_ADDCOLOR
:
5020 shader_addline(buffer
, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
5022 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA
:
5023 shader_addline(buffer
, "SUB arg0, const.x, %s;\n", arg1
);
5024 shader_addline(buffer
, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg
, dstmask
, arg2
, arg1
);
5026 case WINED3DTOP_MODULATECOLOR_ADDALPHA
:
5027 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg
, dstmask
, arg1
, arg2
, arg1
);
5030 case WINED3DTOP_DOTPRODUCT3
:
5032 if(strcmp(dstreg
, "result.color") == 0) {
5034 mul_final_dest
= TRUE
;
5036 shader_addline(buffer
, "SUB arg1, %s, const.w;\n", arg1
);
5037 shader_addline(buffer
, "SUB arg2, %s, const.w;\n", arg2
);
5038 shader_addline(buffer
, "DP3_SAT %s%s, arg1, arg2;\n", dstreg
, dstmask
);
5041 case WINED3DTOP_MULTIPLYADD
:
5042 shader_addline(buffer
, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg1
, arg2
, arg0
);
5045 case WINED3DTOP_LERP
:
5046 /* The msdn is not quite right here */
5047 shader_addline(buffer
, "LRP %s%s, %s, %s, %s;\n", dstreg
, dstmask
, arg0
, arg1
, arg2
);
5050 case WINED3DTOP_BUMPENVMAP
:
5051 case WINED3DTOP_BUMPENVMAPLUMINANCE
:
5052 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
5056 FIXME("Unhandled texture op %08x\n", op
);
5060 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
5061 } else if(mul
== 4) {
5062 shader_addline(buffer
, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest
? "result.color" : dstreg
, dstmask
, dstreg
);
5066 /* The stateblock is passed for GLINFO_LOCATION */
5067 static GLuint
gen_arbfp_ffp_shader(const struct ffp_frag_settings
*settings
, IWineD3DStateBlockImpl
*stateblock
)
5070 SHADER_BUFFER buffer
;
5071 BOOL tex_read
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5072 BOOL bump_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5073 BOOL luminance_used
[MAX_TEXTURES
] = {FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
};
5074 const char *textype
;
5075 const char *instr
, *sat
;
5076 char colorcor_dst
[8];
5078 DWORD arg0
, arg1
, arg2
;
5079 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
5081 const char *final_combiner_src
= "ret";
5082 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
5084 /* Find out which textures are read */
5085 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
5086 if(settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
) break;
5087 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
5088 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
5089 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
5090 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5091 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5092 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5094 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHA
) tex_read
[stage
] = TRUE
;
5095 if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDTEXTUREALPHAPM
) tex_read
[stage
] = TRUE
;
5096 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
) {
5097 bump_used
[stage
] = TRUE
;
5098 tex_read
[stage
] = TRUE
;
5100 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
5101 bump_used
[stage
] = TRUE
;
5102 tex_read
[stage
] = TRUE
;
5103 luminance_used
[stage
] = TRUE
;
5104 } else if(settings
->op
[stage
].cop
== WINED3DTOP_BLENDFACTORALPHA
) {
5105 tfactor_used
= TRUE
;
5108 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
5109 tfactor_used
= TRUE
;
5112 if(settings
->op
[stage
].dst
== tempreg
) tempreg_used
= TRUE
;
5113 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
5114 tempreg_used
= TRUE
;
5117 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) continue;
5118 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
5119 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
5120 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
5121 if(arg0
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5122 if(arg1
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5123 if(arg2
== WINED3DTA_TEXTURE
) tex_read
[stage
] = TRUE
;
5125 if(arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
) {
5126 tempreg_used
= TRUE
;
5128 if(arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
) {
5129 tfactor_used
= TRUE
;
5134 shader_buffer_init(&buffer
);
5136 shader_addline(&buffer
, "!!ARBfp1.0\n");
5138 switch(settings
->fog
) {
5139 case FOG_OFF
: break;
5140 case FOG_LINEAR
: shader_addline(&buffer
, "OPTION ARB_fog_linear;\n"); break;
5141 case FOG_EXP
: shader_addline(&buffer
, "OPTION ARB_fog_exp;\n"); break;
5142 case FOG_EXP2
: shader_addline(&buffer
, "OPTION ARB_fog_exp2;\n"); break;
5143 default: FIXME("Unexpected fog setting %d\n", settings
->fog
);
5146 shader_addline(&buffer
, "PARAM const = {1, 2, 4, 0.5};\n");
5147 shader_addline(&buffer
, "TEMP TMP;\n");
5148 shader_addline(&buffer
, "TEMP ret;\n");
5149 if(tempreg_used
|| settings
->sRGB_write
) shader_addline(&buffer
, "TEMP tempreg;\n");
5150 shader_addline(&buffer
, "TEMP arg0;\n");
5151 shader_addline(&buffer
, "TEMP arg1;\n");
5152 shader_addline(&buffer
, "TEMP arg2;\n");
5153 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
5154 if(!tex_read
[stage
]) continue;
5155 shader_addline(&buffer
, "TEMP tex%u;\n", stage
);
5156 if(!bump_used
[stage
]) continue;
5157 shader_addline(&buffer
, "PARAM bumpmat%u = program.env[%u];\n", stage
, ARB_FFP_CONST_BUMPMAT(stage
));
5158 if(!luminance_used
[stage
]) continue;
5159 shader_addline(&buffer
, "PARAM luminance%u = program.env[%u];\n", stage
, ARB_FFP_CONST_LUMINANCE(stage
));
5162 shader_addline(&buffer
, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR
);
5164 shader_addline(&buffer
, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE
);
5166 if(settings
->sRGB_write
) {
5167 shader_addline(&buffer
, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
5168 srgb_mul_low
, srgb_cmp
, srgb_pow
, srgb_mul_high
);
5169 shader_addline(&buffer
, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
5170 srgb_sub_high
, 0.0, 0.0, 0.0);
5173 if(ffp_clip_emul(stateblock
) && device
->vs_clipping
) shader_addline(&buffer
, "KIL fragment.texcoord[7];\n");
5175 /* Generate texture sampling instructions) */
5176 for(stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3DTOP_DISABLE
; stage
++) {
5177 if(!tex_read
[stage
]) continue;
5179 switch(settings
->op
[stage
].tex_type
) {
5180 case tex_1d
: textype
= "1D"; break;
5181 case tex_2d
: textype
= "2D"; break;
5182 case tex_3d
: textype
= "3D"; break;
5183 case tex_cube
: textype
= "CUBE"; break;
5184 case tex_rect
: textype
= "RECT"; break;
5185 default: textype
= "unexpected_textype"; break;
5188 if(settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAP
||
5189 settings
->op
[stage
].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
5195 if(settings
->op
[stage
].projected
== proj_none
) {
5197 } else if(settings
->op
[stage
].projected
== proj_count4
||
5198 settings
->op
[stage
].projected
== proj_count3
) {
5201 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
5206 (settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAP
||
5207 settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
)) {
5208 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage
- 1);
5209 shader_addline(&buffer
, "DP3 ret.x, arg1, tex%u;\n", stage
- 1);
5210 shader_addline(&buffer
, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage
- 1);
5211 shader_addline(&buffer
, "DP3 ret.y, arg1, tex%u;\n", stage
- 1);
5213 /* with projective textures, texbem only divides the static texture coord, not the displacement,
5214 * so multiply the displacement with the dividing parameter before passing it to TXP
5216 if (settings
->op
[stage
].projected
!= proj_none
) {
5217 if(settings
->op
[stage
].projected
== proj_count4
) {
5218 shader_addline(&buffer
, "MOV ret.w, fragment.texcoord[%u].w;\n", stage
);
5219 shader_addline(&buffer
, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage
, stage
);
5221 shader_addline(&buffer
, "MOV ret.w, fragment.texcoord[%u].z;\n", stage
);
5222 shader_addline(&buffer
, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage
, stage
);
5225 shader_addline(&buffer
, "ADD ret, ret, fragment.texcoord[%u];\n", stage
);
5228 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
5229 instr
, sat
, stage
, stage
, textype
);
5230 if(settings
->op
[stage
- 1].cop
== WINED3DTOP_BUMPENVMAPLUMINANCE
) {
5231 shader_addline(&buffer
, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
5232 stage
- 1, stage
- 1, stage
- 1);
5233 shader_addline(&buffer
, "MUL tex%u, tex%u, ret.x;\n", stage
, stage
);
5235 } else if(settings
->op
[stage
].projected
== proj_count3
) {
5236 shader_addline(&buffer
, "MOV ret, fragment.texcoord[%u];\n", stage
);
5237 shader_addline(&buffer
, "MOV ret.w, ret.z;\n");
5238 shader_addline(&buffer
, "%s%s tex%u, ret, texture[%u], %s;\n",
5239 instr
, sat
, stage
, stage
, textype
);
5241 shader_addline(&buffer
, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
5242 instr
, sat
, stage
, stage
, stage
, textype
);
5245 sprintf(colorcor_dst
, "tex%u", stage
);
5246 gen_color_correction(&buffer
, colorcor_dst
, WINED3DSP_WRITEMASK_ALL
, "const.x", "const.y",
5247 settings
->op
[stage
].color_fixup
);
5250 /* Generate the main shader */
5251 for(stage
= 0; stage
< MAX_TEXTURES
; stage
++) {
5252 if(settings
->op
[stage
].cop
== WINED3DTOP_DISABLE
) {
5254 final_combiner_src
= "fragment.color.primary";
5259 if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
5260 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
5261 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
5262 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG1
&&
5263 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
5264 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
5265 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
5266 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG1
) {
5267 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
5268 } else if(settings
->op
[stage
].cop
== WINED3DTOP_SELECTARG2
&&
5269 settings
->op
[stage
].aop
== WINED3DTOP_SELECTARG2
) {
5270 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
5272 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
&&
5273 settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
&&
5274 settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
&&
5275 settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
5278 if(settings
->op
[stage
].aop
== WINED3DTOP_DISABLE
) {
5279 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
5280 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
5281 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
5283 shader_addline(&buffer
, "MOV ret.w, fragment.color.primary.w;\n");
5285 } else if(op_equal
) {
5286 gen_ffp_instr(&buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
5287 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
5288 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
5290 gen_ffp_instr(&buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
5291 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
5292 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
5293 gen_ffp_instr(&buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
5294 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
5295 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
5299 if(settings
->sRGB_write
) {
5300 shader_addline(&buffer
, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
5301 arbfp_add_sRGB_correction(&buffer
, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE
);
5303 shader_addline(&buffer
, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src
);
5307 shader_addline(&buffer
, "END\n");
5309 /* Generate the shader */
5310 GL_EXTCALL(glGenProgramsARB(1, &ret
));
5311 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
));
5312 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(buffer
.buffer
), buffer
.buffer
));
5314 if (glGetError() == GL_INVALID_OPERATION
) {
5316 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
5317 FIXME("Fragment program error at position %d: %s\n", pos
,
5318 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
5320 shader_buffer_free(&buffer
);
5324 static void fragment_prog_arbfp(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
5325 IWineD3DDeviceImpl
*device
= stateblock
->wineD3DDevice
;
5326 struct shader_arb_priv
*priv
= device
->fragment_priv
;
5327 BOOL use_pshader
= use_ps(stateblock
);
5328 BOOL use_vshader
= use_vs(stateblock
);
5329 struct ffp_frag_settings settings
;
5330 const struct arbfp_ffp_desc
*desc
;
5333 TRACE("state %#x, stateblock %p, context %p\n", state
, stateblock
, context
);
5335 if(isStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
))) {
5336 if(!use_pshader
&& device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
5337 /* Reload fixed function constants since they collide with the pixel shader constants */
5338 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
5339 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
5341 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
5342 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
5343 } else if(use_pshader
&& !isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
5344 device
->shader_backend
->shader_select((IWineD3DDevice
*)stateblock
->wineD3DDevice
, use_pshader
, use_vshader
);
5350 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
5351 gen_ffp_frag_op(stateblock
, &settings
, FALSE
);
5352 desc
= (const struct arbfp_ffp_desc
*)find_ffp_frag_shader(&priv
->fragment_shaders
, &settings
);
5354 struct arbfp_ffp_desc
*new_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc
));
5357 ERR("Out of memory\n");
5360 new_desc
->num_textures_used
= 0;
5361 for(i
= 0; i
< GL_LIMITS(texture_stages
); i
++) {
5362 if(settings
.op
[i
].cop
== WINED3DTOP_DISABLE
) break;
5363 new_desc
->num_textures_used
= i
;
5366 memcpy(&new_desc
->parent
.settings
, &settings
, sizeof(settings
));
5367 new_desc
->shader
= gen_arbfp_ffp_shader(&settings
, stateblock
);
5368 add_ffp_frag_shader(&priv
->fragment_shaders
, &new_desc
->parent
);
5369 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc
);
5373 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
5374 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
5377 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, desc
->shader
));
5378 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
5379 priv
->current_fprogram_id
= desc
->shader
;
5381 if(device
->shader_backend
== &arb_program_shader_backend
&& context
->last_was_pshader
) {
5382 /* Reload fixed function constants since they collide with the pixel shader constants */
5383 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
5384 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i
, WINED3DTSS_BUMPENVMAT00
), stateblock
, context
);
5386 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), stateblock
, context
);
5387 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE
), stateblock
, context
);
5389 context
->last_was_pshader
= FALSE
;
5391 context
->last_was_pshader
= TRUE
;
5394 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
5395 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
5396 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
5397 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
5398 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
5400 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
5403 if(!isStateDirty(context
, device
->StateTable
[STATE_VSHADER
].representative
)) {
5404 device
->shader_backend
->shader_select((IWineD3DDevice
*)stateblock
->wineD3DDevice
, use_pshader
, use_vshader
);
5406 if (!isStateDirty(context
, STATE_VERTEXSHADERCONSTANT
) && (use_vshader
|| use_pshader
)) {
5407 device
->StateTable
[STATE_VERTEXSHADERCONSTANT
].apply(STATE_VERTEXSHADERCONSTANT
, stateblock
, context
);
5411 device
->StateTable
[STATE_PIXELSHADERCONSTANT
].apply(STATE_PIXELSHADERCONSTANT
, stateblock
, context
);
5415 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
5416 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
5417 * state table, so we need to handle that with a forwarding function. The other invisible side effect
5418 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
5419 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
5421 static void state_arbfp_fog(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
5422 enum fogsource new_source
;
5424 TRACE("state %#x, stateblock %p, context %p\n", state
, stateblock
, context
);
5426 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
5427 fragment_prog_arbfp(state
, stateblock
, context
);
5430 if(!stateblock
->renderState
[WINED3DRS_FOGENABLE
]) return;
5432 if(stateblock
->renderState
[WINED3DRS_FOGTABLEMODE
] == WINED3DFOG_NONE
) {
5433 if(use_vs(stateblock
)) {
5434 new_source
= FOGSOURCE_VS
;
5436 if(stateblock
->renderState
[WINED3DRS_FOGVERTEXMODE
] == WINED3DFOG_NONE
|| context
->last_was_rhw
) {
5437 new_source
= FOGSOURCE_COORD
;
5439 new_source
= FOGSOURCE_FFP
;
5443 new_source
= FOGSOURCE_FFP
;
5445 if(new_source
!= context
->fog_source
) {
5446 context
->fog_source
= new_source
;
5447 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART
), stateblock
, context
);
5451 static void textransform(DWORD state
, IWineD3DStateBlockImpl
*stateblock
, WineD3DContext
*context
) {
5452 if(!isStateDirty(context
, STATE_PIXELSHADER
)) {
5453 fragment_prog_arbfp(state
, stateblock
, context
);
5457 #undef GLINFO_LOCATION
5459 static const struct StateEntryTemplate arbfp_fragmentstate_template
[] = {
5460 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR
), state_texfactor_arbfp
}, WINED3D_GL_EXT_NONE
},
5461 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5462 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5463 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5464 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5465 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5466 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5467 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5468 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5469 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5470 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5471 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5472 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5473 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5474 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5475 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5476 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5477 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5478 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5479 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5480 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5481 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5482 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5483 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5484 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5485 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5486 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5487 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5488 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5489 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5490 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5491 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5492 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5493 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5494 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5495 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5496 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5497 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5498 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5499 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5500 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5501 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5502 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5503 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5504 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5505 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5506 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5507 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5508 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5509 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5510 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5511 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5512 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5513 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5514 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5515 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5516 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5517 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5518 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5519 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5520 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5521 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5522 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5523 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5524 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5525 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5526 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5527 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5528 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5529 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5530 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5531 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5532 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5533 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5534 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5535 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5536 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5537 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5538 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5539 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5540 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5541 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5542 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5543 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5544 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5545 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5546 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5547 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5548 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5549 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5550 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5551 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5552 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5553 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5554 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5555 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5556 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5557 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5558 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5559 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5560 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5561 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5562 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5563 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5564 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5565 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5566 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5567 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5568 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5569 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5570 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5571 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5572 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5573 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5574 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5575 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5576 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5577 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5578 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00
), set_bumpmat_arbfp
}, WINED3D_GL_EXT_NONE
},
5579 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5580 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET
), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE
), tex_bumpenvlum_arbfp
}, WINED3D_GL_EXT_NONE
},
5581 {STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5582 {STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5583 {STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5584 {STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5585 {STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5586 {STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5587 {STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5588 {STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim
}, WINED3D_GL_EXT_NONE
},
5589 {STATE_PIXELSHADER
, { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5590 {STATE_RENDER(WINED3DRS_FOGENABLE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, WINED3D_GL_EXT_NONE
},
5591 {STATE_RENDER(WINED3DRS_FOGTABLEMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, WINED3D_GL_EXT_NONE
},
5592 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE
), { STATE_RENDER(WINED3DRS_FOGENABLE
), state_arbfp_fog
}, WINED3D_GL_EXT_NONE
},
5593 {STATE_RENDER(WINED3DRS_FOGSTART
), { STATE_RENDER(WINED3DRS_FOGSTART
), state_fogstartend
}, WINED3D_GL_EXT_NONE
},
5594 {STATE_RENDER(WINED3DRS_FOGEND
), { STATE_RENDER(WINED3DRS_FOGSTART
), state_fogstartend
}, WINED3D_GL_EXT_NONE
},
5595 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), { STATE_PIXELSHADER
, fragment_prog_arbfp
}, WINED3D_GL_EXT_NONE
},
5596 {STATE_RENDER(WINED3DRS_FOGCOLOR
), { STATE_RENDER(WINED3DRS_FOGCOLOR
), state_fogcolor
}, WINED3D_GL_EXT_NONE
},
5597 {STATE_RENDER(WINED3DRS_FOGDENSITY
), { STATE_RENDER(WINED3DRS_FOGDENSITY
), state_fogdensity
}, WINED3D_GL_EXT_NONE
},
5598 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5599 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5600 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5601 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5602 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5603 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5604 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5605 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS
),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS
), textransform
}, WINED3D_GL_EXT_NONE
},
5606 {STATE_RENDER(WINED3DRS_SPECULARENABLE
), { STATE_RENDER(WINED3DRS_SPECULARENABLE
), state_arb_specularenable
}, WINED3D_GL_EXT_NONE
},
5607 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE
},
5610 const struct fragment_pipeline arbfp_fragment_pipeline
= {
5615 shader_arb_color_fixup_supported
,
5616 arbfp_fragmentstate_template
,
5617 TRUE
/* We can disable projected textures */
5620 #define GLINFO_LOCATION device->adapter->gl_info
5622 struct arbfp_blit_priv
{
5623 GLenum yuy2_rect_shader
, yuy2_2d_shader
;
5624 GLenum uyvy_rect_shader
, uyvy_2d_shader
;
5625 GLenum yv12_rect_shader
, yv12_2d_shader
;
5628 static HRESULT
arbfp_blit_alloc(IWineD3DDevice
*iface
) {
5629 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
5630 device
->blit_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct arbfp_blit_priv
));
5631 if(!device
->blit_priv
) {
5632 ERR("Out of memory\n");
5633 return E_OUTOFMEMORY
;
5637 static void arbfp_blit_free(IWineD3DDevice
*iface
) {
5638 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
5639 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
5642 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_rect_shader
));
5643 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yuy2_2d_shader
));
5644 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_rect_shader
));
5645 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->uyvy_2d_shader
));
5646 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_rect_shader
));
5647 GL_EXTCALL(glDeleteProgramsARB(1, &priv
->yv12_2d_shader
));
5648 checkGLcall("Delete yuv programs\n");
5652 static BOOL
gen_planar_yuv_read(SHADER_BUFFER
*buffer
, enum yuv_fixup yuv_fixup
, GLenum textype
, char *luminance
)
5655 const char *tex
, *texinstr
;
5657 if (yuv_fixup
== YUV_FIXUP_UYVY
) {
5665 case GL_TEXTURE_2D
: tex
= "2D"; texinstr
= "TXP"; break;
5666 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; texinstr
= "TEX"; break;
5668 /* This is more tricky than just replacing the texture type - we have to navigate
5669 * properly in the texture to find the correct chroma values
5671 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
5675 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
5676 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
5677 * filtering when we sample the texture.
5679 * These are the rules for reading the chroma:
5685 * So we have to get the sampling x position in non-normalized coordinates in integers
5687 if(textype
!= GL_TEXTURE_RECTANGLE_ARB
) {
5688 shader_addline(buffer
, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
5689 shader_addline(buffer
, "MOV texcrd.w, size.x;\n");
5691 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
5693 /* We must not allow filtering between pixel x and x+1, this would mix U and V
5694 * Vertical filtering is ok. However, bear in mind that the pixel center is at
5697 shader_addline(buffer
, "FLR texcrd.x, texcrd.x;\n");
5698 shader_addline(buffer
, "ADD texcrd.x, texcrd.x, coef.y;\n");
5700 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
5701 * even and odd pixels respectively
5703 shader_addline(buffer
, "MUL texcrd2, texcrd, coef.y;\n");
5704 shader_addline(buffer
, "FRC texcrd2, texcrd2;\n");
5706 /* Sample Pixel 1 */
5707 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
5709 /* Put the value into either of the chroma values */
5710 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
5711 shader_addline(buffer
, "MUL chroma.x, luminance.%c, temp.x;\n", chroma
);
5712 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
5713 shader_addline(buffer
, "MUL chroma.y, luminance.%c, temp.x;\n", chroma
);
5715 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
5716 * the pixel right to the current one. Otherwise, sample the left pixel.
5717 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
5719 shader_addline(buffer
, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
5720 shader_addline(buffer
, "ADD texcrd.x, texcrd, temp.x;\n");
5721 shader_addline(buffer
, "%s luminance, texcrd, texture[0], %s;\n", texinstr
, tex
);
5723 /* Put the value into the other chroma */
5724 shader_addline(buffer
, "SGE temp.x, texcrd2.x, coef.y;\n");
5725 shader_addline(buffer
, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma
);
5726 shader_addline(buffer
, "SLT temp.x, texcrd2.x, coef.y;\n");
5727 shader_addline(buffer
, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma
);
5729 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
5730 * the current one and lerp the two U and V values
5733 /* This gives the correctly filtered luminance value */
5734 shader_addline(buffer
, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex
);
5739 static BOOL
gen_yv12_read(SHADER_BUFFER
*buffer
, GLenum textype
, char *luminance
)
5744 case GL_TEXTURE_2D
: tex
= "2D"; break;
5745 case GL_TEXTURE_RECTANGLE_ARB
: tex
= "RECT"; break;
5747 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
5751 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
5752 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
5753 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
5754 * pitch of the luminance plane, the packing into the gl texture is a bit
5755 * unfortunate. If the whole texture is interpreted as luminance data it looks
5756 * approximately like this:
5758 * +----------------------------------+----
5770 * +----------------+-----------------+----
5772 * | U even rows | U odd rows |
5774 * +----------------+------------------ -
5776 * | V even rows | V odd rows |
5778 * +----------------+-----------------+----
5782 * So it appears as if there are 4 chroma images, but in fact the odd rows
5783 * in the chroma images are in the same row as the even ones. So its is
5784 * kinda tricky to read
5786 * When reading from rectangle textures, keep in mind that the input y coordinates
5787 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
5789 shader_addline(buffer
, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
5790 2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
5792 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
5793 /* the chroma planes have only half the width */
5794 shader_addline(buffer
, "MUL texcrd.x, texcrd.x, coef.y;\n");
5796 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
5797 * the coordinate. Also read the right side of the image when reading odd lines
5799 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
5802 if(textype
== GL_TEXTURE_2D
) {
5804 shader_addline(buffer
, "RCP chroma.w, size.y;\n");
5806 shader_addline(buffer
, "MUL texcrd2.y, texcrd.y, size.y;\n");
5808 shader_addline(buffer
, "FLR texcrd2.y, texcrd2.y;\n");
5809 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
5811 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
5812 shader_addline(buffer
, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
5813 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
5814 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
5815 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
5817 /* clamp, keep the half pixel origin in mind */
5818 shader_addline(buffer
, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
5819 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
5820 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
5821 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
5823 /* Read from [size - size+size/4] */
5824 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
5825 shader_addline(buffer
, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
5827 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
5828 shader_addline(buffer
, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
5829 shader_addline(buffer
, "FRC texcrd2.x, texcrd2.x;\n");
5830 shader_addline(buffer
, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
5831 shader_addline(buffer
, "MUL texcrd2.x, texcrd2.x, size.x;\n");
5832 shader_addline(buffer
, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
5834 /* Make sure to read exactly from the pixel center */
5835 shader_addline(buffer
, "FLR texcrd.y, texcrd.y;\n");
5836 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, coef.y;\n");
5839 shader_addline(buffer
, "MAD temp.y, size.y, coef.w, size.y;\n");
5840 shader_addline(buffer
, "ADD temp.y, temp.y, -coef.y;\n");
5841 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
5842 shader_addline(buffer
, "ADD temp.y, size.y, -coef.y;\n");
5843 shader_addline(buffer
, "MAX texcrd.y, temp.y, texcrd.y;\n");
5845 /* Read the texture, put the result into the output register */
5846 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
5847 shader_addline(buffer
, "MOV chroma.x, temp.w;\n");
5849 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
5850 * No need to clamp because we're just reusing the already clamped value from above
5852 if(textype
== GL_TEXTURE_2D
) {
5853 shader_addline(buffer
, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
5855 shader_addline(buffer
, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
5857 shader_addline(buffer
, "TEX temp, texcrd, texture[0], %s;\n", tex
);
5858 shader_addline(buffer
, "MOV chroma.y, temp.w;\n");
5860 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
5861 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
5862 * values due to filtering
5864 shader_addline(buffer
, "MOV texcrd, fragment.texcoord[0];\n");
5865 if(textype
== GL_TEXTURE_2D
) {
5866 /* Multiply the y coordinate by 2/3 and clamp it */
5867 shader_addline(buffer
, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
5868 shader_addline(buffer
, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
5869 shader_addline(buffer
, "MIN texcrd.y, temp.y, texcrd.y;\n");
5870 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
5872 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
5873 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
5876 shader_addline(buffer
, "ADD temp.x, size.y, -coef.y;\n");
5877 shader_addline(buffer
, "MIN texcrd.y, texcrd.y, size.x;\n");
5878 shader_addline(buffer
, "TEX luminance, texcrd, texture[0], %s;\n", tex
);
5885 static GLuint
gen_yuv_shader(IWineD3DDeviceImpl
*device
, enum yuv_fixup yuv_fixup
, GLenum textype
)
5888 SHADER_BUFFER buffer
;
5889 char luminance_component
;
5890 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
5893 shader_buffer_init(&buffer
);
5896 GL_EXTCALL(glGenProgramsARB(1, &shader
));
5897 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
5898 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
5899 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
5902 shader_buffer_free(&buffer
);
5906 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
5907 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
5908 * two chroma(U and V) values. Each macropixel has two luminance values, one for
5909 * each single pixel it contains, and one U and one V value shared between both
5912 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
5913 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
5914 * take the format into account when generating the read swizzles
5916 * Reading the Y value is straightforward - just sample the texture. The hardware
5917 * takes care of filtering in the horizontal and vertical direction.
5919 * Reading the U and V values is harder. We have to avoid filtering horizontally,
5920 * because that would mix the U and V values of one pixel or two adjacent pixels.
5921 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
5922 * regardless of the filtering setting. Vertical filtering works automatically
5923 * though - the U and V values of two rows are mixed nicely.
5925 * Appart of avoiding filtering issues, the code has to know which value it just
5926 * read, and where it can find the other one. To determine this, it checks if
5927 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
5929 * Handling horizontal filtering of U and V values requires reading a 2nd pair
5930 * of pixels, extracting U and V and mixing them. This is not implemented yet.
5932 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
5933 * with width / 2. This way one read gives all 3 values, finding U and V is easy
5934 * in an unfiltered situation. Finding the luminance on the other hand requires
5935 * finding out if it is an odd or even pixel. The real drawback of this approach
5936 * is filtering. This would have to be emulated completely in the shader, reading
5937 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
5938 * vertically. Beyond that it would require adjustments to the texture handling
5939 * code to deal with the width scaling
5941 shader_addline(&buffer
, "!!ARBfp1.0\n");
5942 shader_addline(&buffer
, "TEMP luminance;\n");
5943 shader_addline(&buffer
, "TEMP temp;\n");
5944 shader_addline(&buffer
, "TEMP chroma;\n");
5945 shader_addline(&buffer
, "TEMP texcrd;\n");
5946 shader_addline(&buffer
, "TEMP texcrd2;\n");
5947 shader_addline(&buffer
, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
5948 shader_addline(&buffer
, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
5949 shader_addline(&buffer
, "PARAM size = program.local[0];\n");
5953 case YUV_FIXUP_UYVY
:
5954 case YUV_FIXUP_YUY2
:
5955 if (!gen_planar_yuv_read(&buffer
, yuv_fixup
, textype
, &luminance_component
))
5957 shader_buffer_free(&buffer
);
5962 case YUV_FIXUP_YV12
:
5963 if (!gen_yv12_read(&buffer
, textype
, &luminance_component
))
5965 shader_buffer_free(&buffer
);
5971 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup
);
5972 shader_buffer_free(&buffer
);
5976 /* Calculate the final result. Formula is taken from
5977 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
5978 * ranges from -0.5 to 0.5
5980 shader_addline(&buffer
, "SUB chroma.xy, chroma, coef.y;\n");
5982 shader_addline(&buffer
, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component
);
5983 shader_addline(&buffer
, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component
);
5984 shader_addline(&buffer
, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
5985 shader_addline(&buffer
, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component
);
5986 shader_addline(&buffer
, "END\n");
5989 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(buffer
.buffer
), buffer
.buffer
));
5991 if (glGetError() == GL_INVALID_OPERATION
) {
5993 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &pos
);
5994 FIXME("Fragment program error at position %d: %s\n", pos
,
5995 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
)));
5997 shader_buffer_free(&buffer
);
6002 case YUV_FIXUP_YUY2
:
6003 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->yuy2_rect_shader
= shader
;
6004 else priv
->yuy2_2d_shader
= shader
;
6007 case YUV_FIXUP_UYVY
:
6008 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->uyvy_rect_shader
= shader
;
6009 else priv
->uyvy_2d_shader
= shader
;
6012 case YUV_FIXUP_YV12
:
6013 if (textype
== GL_TEXTURE_RECTANGLE_ARB
) priv
->yv12_rect_shader
= shader
;
6014 else priv
->yv12_2d_shader
= shader
;
6021 static HRESULT
arbfp_blit_set(IWineD3DDevice
*iface
, const struct GlPixelFormatDesc
*format_desc
,
6022 GLenum textype
, UINT width
, UINT height
)
6025 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
6026 float size
[4] = {width
, height
, 1, 1};
6027 struct arbfp_blit_priv
*priv
= device
->blit_priv
;
6028 enum yuv_fixup yuv_fixup
;
6030 if (!is_yuv_fixup(format_desc
->color_fixup
))
6033 dump_color_fixup_desc(format_desc
->color_fixup
);
6034 /* Don't bother setting up a shader for unconverted formats */
6037 checkGLcall("glEnable(textype)");
6042 yuv_fixup
= get_yuv_fixup(format_desc
->color_fixup
);
6046 case YUV_FIXUP_YUY2
:
6047 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->yuy2_rect_shader
: priv
->yuy2_2d_shader
;
6050 case YUV_FIXUP_UYVY
:
6051 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->uyvy_rect_shader
: priv
->uyvy_2d_shader
;
6054 case YUV_FIXUP_YV12
:
6055 shader
= textype
== GL_TEXTURE_RECTANGLE_ARB
? priv
->yv12_rect_shader
: priv
->yv12_2d_shader
;
6059 FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup
);
6062 checkGLcall("glEnable(textype)");
6067 if (!shader
) shader
= gen_yuv_shader(device
, yuv_fixup
, textype
);
6070 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
6071 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
6072 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, shader
));
6073 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6074 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0, size
));
6075 checkGLcall("glProgramLocalParameter4fvARB");
6081 static void arbfp_blit_unset(IWineD3DDevice
*iface
) {
6082 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
6085 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
6086 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
6087 glDisable(GL_TEXTURE_2D
);
6088 checkGLcall("glDisable(GL_TEXTURE_2D)");
6089 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
6090 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
6091 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6093 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
6094 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
6095 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6100 static BOOL
arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup
)
6102 enum yuv_fixup yuv_fixup
;
6104 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
6106 TRACE("Checking support for fixup:\n");
6107 dump_color_fixup_desc(fixup
);
6110 if (is_identity_fixup(fixup
))
6116 /* We only support YUV conversions. */
6117 if (!is_yuv_fixup(fixup
))
6119 TRACE("[FAILED]\n");
6123 yuv_fixup
= get_yuv_fixup(fixup
);
6126 case YUV_FIXUP_YUY2
:
6127 case YUV_FIXUP_UYVY
:
6128 case YUV_FIXUP_YV12
:
6133 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup
);
6134 TRACE("[FAILED]\n");
6139 const struct blit_shader arbfp_blit
= {
6144 arbfp_blit_color_fixup_supported
,
6147 #undef GLINFO_LOCATION