push ca931951dc5f3f050707cce013d2412130f45f11
[wine/hacks.git] / dlls / wined3d / arb_program_shader.c
blobcdc5b10e399403620fdbc166a82d5140127c1adf
1 /*
2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
11 * Copyright 2006 Henri Verbeet
12 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
13 * Copyright 2009 Henri Verbeet for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "config.h"
32 #include <math.h>
33 #include <stdio.h>
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
42 #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->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT /* Load the immval offset. */
54 || gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) /* Have to init texcoords. */
56 return TRUE;
58 return FALSE;
61 static unsigned int reserved_vs_const(IWineD3DBaseShader *shader, const WineD3D_GL_Info *gl_info) {
62 unsigned int ret = 1;
63 /* We use one PARAM for the pos fixup, and in some cases one to load
64 * some immediate values into the shader
66 if(need_helper_const(gl_info)) ret++;
67 if(need_mova_const(shader, gl_info)) ret++;
68 return ret;
71 static inline BOOL ffp_clip_emul(IWineD3DStateBlockImpl *stateblock)
73 return stateblock->lowest_disabled_stage < 7;
76 /* Returns TRUE if result.clip from GL_NV_vertex_program2 should be used and FALSE otherwise */
77 static inline BOOL use_nv_clip(const WineD3D_GL_Info *gl_info)
79 return GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION);
82 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
83 * so upload them above that
85 #define ARB_SHADER_PRIVCONST_BASE (GL_LIMITS(vshader_constantsF) - 1)
86 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
88 /* ARB_program_shader private data */
90 struct loop_control
92 unsigned int count;
93 unsigned int start;
94 int step;
97 struct control_frame
99 struct list entry;
100 enum
103 IFC,
104 LOOP,
106 } type;
107 BOOL muting;
108 BOOL outer_loop;
109 union
111 unsigned int loop_no;
112 unsigned int ifc_no;
114 struct loop_control loop_control;
115 BOOL had_else;
118 struct arb_ps_np2fixup_info
120 struct ps_np2fixup_info super;
121 /* For ARB we need a offset value:
122 * With both GLSL and ARB mode the NP2 fixup information (the texture dimensions) are stored in a
123 * consecutive way (GLSL uses a uniform array). Since ARB doesn't know the notion of a "standalone"
124 * array we need an offset to the index inside the program local parameter array. */
125 UINT offset;
128 struct arb_ps_compile_args
130 struct ps_compile_args super;
131 DWORD bools; /* WORD is enough, use DWORD for alignment */
132 unsigned char loop_ctrl[MAX_CONST_I][3];
135 struct stb_const_desc
137 unsigned char texunit;
138 UINT const_num;
141 struct arb_ps_compiled_shader
143 struct arb_ps_compile_args args;
144 struct arb_ps_np2fixup_info np2fixup_info;
145 struct stb_const_desc bumpenvmatconst[MAX_TEXTURES];
146 struct stb_const_desc luminanceconst[MAX_TEXTURES];
147 UINT int_consts[MAX_CONST_I];
148 GLuint prgId;
149 UINT ycorrection;
150 unsigned char numbumpenvmatconsts;
151 char num_int_consts;
154 struct arb_vs_compile_args
156 struct vs_compile_args super;
157 union
159 struct
161 WORD bools;
162 char clip_control[2];
163 } boolclip;
164 DWORD boolclip_compare;
166 DWORD ps_signature;
167 union
169 unsigned char vertex_samplers[4];
170 DWORD vertex_samplers_compare;
172 unsigned char loop_ctrl[MAX_CONST_I][3];
175 struct arb_vs_compiled_shader
177 struct arb_vs_compile_args args;
178 GLuint prgId;
179 UINT int_consts[MAX_CONST_I];
180 char num_int_consts;
181 char need_color_unclamp;
182 UINT pos_fixup;
185 struct recorded_instruction
187 struct wined3d_shader_instruction ins;
188 struct list entry;
191 struct shader_arb_ctx_priv
193 char addr_reg[20];
194 enum
196 /* plain GL_ARB_vertex_program or GL_ARB_fragment_program */
197 ARB,
198 /* GL_NV_vertex_progam2_option or GL_NV_fragment_program_option */
199 NV2,
200 /* GL_NV_vertex_program3 or GL_NV_fragment_program2 */
202 } target_version;
204 const struct arb_vs_compile_args *cur_vs_args;
205 const struct arb_ps_compile_args *cur_ps_args;
206 const struct arb_ps_compiled_shader *compiled_fprog;
207 const struct arb_vs_compiled_shader *compiled_vprog;
208 struct arb_ps_np2fixup_info *cur_np2fixup_info;
209 struct list control_frames;
210 struct list record;
211 BOOL recording;
212 BOOL muted;
213 unsigned int num_loops, loop_depth, num_ifcs;
214 int aL;
216 unsigned int vs_clipplanes;
217 BOOL footer_written;
218 BOOL in_main_func;
220 /* For 3.0 vertex shaders */
221 const char *vs_output[MAX_REG_OUTPUT];
222 /* For 2.x and earlier vertex shaders */
223 const char *texcrd_output[8], *color_output[2], *fog_output;
225 /* 3.0 pshader input for compatibility with fixed function */
226 const char *ps_input[MAX_REG_INPUT];
229 struct ps_signature
231 struct wined3d_shader_signature_element *sig;
232 DWORD idx;
233 struct wine_rb_entry entry;
236 struct arb_pshader_private {
237 struct arb_ps_compiled_shader *gl_shaders;
238 UINT num_gl_shaders, shader_array_size;
239 BOOL has_signature_idx;
240 DWORD input_signature_idx;
241 DWORD clipplane_emulation;
242 BOOL clamp_consts;
245 struct arb_vshader_private {
246 struct arb_vs_compiled_shader *gl_shaders;
247 UINT num_gl_shaders, shader_array_size;
250 struct shader_arb_priv
252 GLuint current_vprogram_id;
253 GLuint current_fprogram_id;
254 const struct arb_ps_compiled_shader *compiled_fprog;
255 const struct arb_vs_compiled_shader *compiled_vprog;
256 GLuint depth_blt_vprogram_id;
257 GLuint depth_blt_fprogram_id[tex_type_count];
258 BOOL use_arbfp_fixed_func;
259 struct wine_rb_tree fragment_shaders;
260 BOOL last_ps_const_clamped;
261 BOOL last_vs_color_unclamp;
263 struct wine_rb_tree signature_tree;
264 DWORD ps_sig_number;
267 /********************************************************
268 * ARB_[vertex/fragment]_program helper functions follow
269 ********************************************************/
271 /**
272 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
273 * When constant_list == NULL, it will load all the constants.
275 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
276 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
278 /* GL locking is done by the caller */
279 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, const WineD3D_GL_Info *gl_info,
280 GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
282 local_constant* lconst;
283 DWORD i, j;
284 unsigned int ret;
286 if (TRACE_ON(d3d_shader)) {
287 for(i = 0; i < max_constants; i++) {
288 if(!dirty_consts[i]) continue;
289 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
290 constants[i * 4 + 0], constants[i * 4 + 1],
291 constants[i * 4 + 2], constants[i * 4 + 3]);
294 /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
295 if (target_type == GL_FRAGMENT_PROGRAM_ARB && This->baseShader.reg_maps.shader_version.major == 1)
297 float lcl_const[4];
298 for(i = 0; i < max_constants; i++) {
299 if(!dirty_consts[i]) continue;
300 dirty_consts[i] = 0;
302 j = 4 * i;
303 if(constants[j + 0] > 1.0) lcl_const[0] = 1.0;
304 else if(constants[j + 0] < -1.0) lcl_const[0] = -1.0;
305 else lcl_const[0] = constants[j + 0];
307 if(constants[j + 1] > 1.0) lcl_const[1] = 1.0;
308 else if(constants[j + 1] < -1.0) lcl_const[1] = -1.0;
309 else lcl_const[1] = constants[j + 1];
311 if(constants[j + 2] > 1.0) lcl_const[2] = 1.0;
312 else if(constants[j + 2] < -1.0) lcl_const[2] = -1.0;
313 else lcl_const[2] = constants[j + 2];
315 if(constants[j + 3] > 1.0) lcl_const[3] = 1.0;
316 else if(constants[j + 3] < -1.0) lcl_const[3] = -1.0;
317 else lcl_const[3] = constants[j + 3];
319 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
321 } else {
322 if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
323 /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
324 * or just reloading *all* constants at once
326 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
328 for(i = 0; i < max_constants; i++) {
329 if(!dirty_consts[i]) continue;
331 /* Find the next block of dirty constants */
332 dirty_consts[i] = 0;
333 j = i;
334 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
335 dirty_consts[i] = 0;
338 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
340 } else {
341 for(i = 0; i < max_constants; i++) {
342 if(dirty_consts[i]) {
343 dirty_consts[i] = 0;
344 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
349 checkGLcall("glProgramEnvParameter4fvARB()");
351 /* Load immediate constants */
352 if(This->baseShader.load_local_constsF) {
353 if (TRACE_ON(d3d_shader)) {
354 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
355 GLfloat* values = (GLfloat*)lconst->value;
356 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
357 values[0], values[1], values[2], values[3]);
360 /* Immediate constants are clamped for 1.X shaders at loading times */
361 ret = 0;
362 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
363 dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
364 ret = max(ret, lconst->idx + 1);
365 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
367 checkGLcall("glProgramEnvParameter4fvARB()");
368 return ret; /* The loaded immediate constants need reloading for the next shader */
369 } else {
370 return 0; /* No constants are dirty now */
375 * Loads the texture dimensions for NP2 fixup into the currently set ARB_[vertex/fragment]_programs.
377 static void shader_arb_load_np2fixup_constants(
378 IWineD3DDevice* device,
379 char usePixelShader,
380 char useVertexShader) {
382 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl *) device;
383 const struct shader_arb_priv* const priv = (const struct shader_arb_priv *) deviceImpl->shader_priv;
384 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
385 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
387 if (!usePixelShader) {
388 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
389 return;
392 if (priv->compiled_fprog && priv->compiled_fprog->np2fixup_info.super.active) {
393 const struct arb_ps_np2fixup_info* const fixup = &priv->compiled_fprog->np2fixup_info;
394 UINT i;
395 WORD active = fixup->super.active;
396 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
398 for (i = 0; active; active >>= 1, ++i) {
399 const unsigned char idx = fixup->super.idx[i];
400 const IWineD3DTextureImpl* const tex = (const IWineD3DTextureImpl*) stateBlock->textures[i];
401 GLfloat* tex_dim = &np2fixup_constants[(idx >> 1) * 4];
403 if (!(active & 1)) continue;
405 if (!tex) {
406 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
407 continue;
410 if (idx % 2) {
411 tex_dim[2] = tex->baseTexture.pow2Matrix[0]; tex_dim[3] = tex->baseTexture.pow2Matrix[5];
412 } else {
413 tex_dim[0] = tex->baseTexture.pow2Matrix[0]; tex_dim[1] = tex->baseTexture.pow2Matrix[5];
417 for (i = 0; i < fixup->super.num_consts; ++i) {
418 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB,
419 fixup->offset + i, &np2fixup_constants[i * 4]));
424 /* GL locking is done by the caller. */
425 static inline void shader_arb_ps_local_constants(IWineD3DDeviceImpl* deviceImpl)
427 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
428 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
429 unsigned char i;
430 struct shader_arb_priv *priv = deviceImpl->shader_priv;
431 const struct arb_ps_compiled_shader *gl_shader = priv->compiled_fprog;
433 for(i = 0; i < gl_shader->numbumpenvmatconsts; i++)
435 int texunit = gl_shader->bumpenvmatconst[i].texunit;
437 /* The state manager takes care that this function is always called if the bump env matrix changes */
438 const float *data = (const float *)&stateBlock->textureState[texunit][WINED3DTSS_BUMPENVMAT00];
439 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->bumpenvmatconst[i].const_num, data));
441 if (gl_shader->luminanceconst[i].const_num != WINED3D_CONST_NUM_UNUSED)
443 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
444 * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
445 * don't care about them. The pointers are valid for sure because the stateblock is bigger.
446 * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
448 const float *scale = (const float *)&stateBlock->textureState[texunit][WINED3DTSS_BUMPENVLSCALE];
449 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->luminanceconst[i].const_num, scale));
452 checkGLcall("Load bumpmap consts\n");
454 if(gl_shader->ycorrection != WINED3D_CONST_NUM_UNUSED)
456 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
457 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
458 * ycorrection.z: 1.0
459 * ycorrection.w: 0.0
461 float val[4];
462 val[0] = deviceImpl->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
463 val[1] = deviceImpl->render_offscreen ? 1.0 : -1.0;
464 val[2] = 1.0;
465 val[3] = 0.0;
466 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->ycorrection, val));
467 checkGLcall("y correction loading\n");
470 if(gl_shader->num_int_consts == 0) return;
472 for(i = 0; i < MAX_CONST_I; i++)
474 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
476 float val[4];
477 val[0] = stateBlock->pixelShaderConstantI[4 * i];
478 val[1] = stateBlock->pixelShaderConstantI[4 * i + 1];
479 val[2] = stateBlock->pixelShaderConstantI[4 * i + 2];
480 val[3] = -1.0;
482 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader->int_consts[i], val));
485 checkGLcall("Load ps int consts\n");
488 /* GL locking is done by the caller. */
489 static inline void shader_arb_vs_local_constants(IWineD3DDeviceImpl* deviceImpl)
491 IWineD3DStateBlockImpl* stateBlock;
492 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
493 unsigned char i;
494 struct shader_arb_priv *priv = deviceImpl->shader_priv;
495 const struct arb_vs_compiled_shader *gl_shader = priv->compiled_vprog;
497 /* Upload the position fixup */
498 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->pos_fixup, deviceImpl->posFixup));
500 if(gl_shader->num_int_consts == 0) return;
502 stateBlock = deviceImpl->stateBlock;
504 for(i = 0; i < MAX_CONST_I; i++)
506 if(gl_shader->int_consts[i] != WINED3D_CONST_NUM_UNUSED)
508 float val[4];
509 val[0] = stateBlock->vertexShaderConstantI[4 * i];
510 val[1] = stateBlock->vertexShaderConstantI[4 * i + 1];
511 val[2] = stateBlock->vertexShaderConstantI[4 * i + 2];
512 val[3] = -1.0;
514 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, gl_shader->int_consts[i], val));
517 checkGLcall("Load vs int consts\n");
521 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
523 * We only support float constants in ARB at the moment, so don't
524 * worry about the Integers or Booleans
526 /* GL locking is done by the caller (state handler) */
527 static void shader_arb_load_constants(
528 IWineD3DDevice* device,
529 char usePixelShader,
530 char useVertexShader) {
532 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
533 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
534 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
536 if (useVertexShader) {
537 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
539 /* Load DirectX 9 float constants for vertex shader */
540 deviceImpl->highest_dirty_vs_const = shader_arb_load_constantsF(
541 vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
542 deviceImpl->highest_dirty_vs_const,
543 stateBlock->vertexShaderConstantF,
544 deviceImpl->activeContext->vshader_const_dirty);
546 shader_arb_vs_local_constants(deviceImpl);
549 if (usePixelShader) {
550 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
552 /* Load DirectX 9 float constants for pixel shader */
553 deviceImpl->highest_dirty_ps_const = shader_arb_load_constantsF(
554 pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
555 deviceImpl->highest_dirty_ps_const,
556 stateBlock->pixelShaderConstantF,
557 deviceImpl->activeContext->pshader_const_dirty);
558 shader_arb_ps_local_constants(deviceImpl);
562 static void shader_arb_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
564 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
566 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
567 * context. On a context switch the old context will be fully dirtified */
568 memset(This->activeContext->vshader_const_dirty + start, 1,
569 sizeof(*This->activeContext->vshader_const_dirty) * count);
570 This->highest_dirty_vs_const = max(This->highest_dirty_vs_const, start + count);
573 static void shader_arb_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
575 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
577 /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
578 * context. On a context switch the old context will be fully dirtified */
579 memset(This->activeContext->pshader_const_dirty + start, 1,
580 sizeof(*This->activeContext->pshader_const_dirty) * count);
581 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, start + count);
584 static DWORD *local_const_mapping(IWineD3DBaseShaderImpl *This)
586 DWORD *ret;
587 DWORD idx = 0;
588 const local_constant *lconst;
590 if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) return NULL;
592 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * This->baseShader.limits.constant_float);
593 if(!ret) {
594 ERR("Out of memory\n");
595 return NULL;
598 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
599 ret[lconst->idx] = idx++;
601 return ret;
604 /* Generate the variable & register declarations for the ARB_vertex_program output target */
605 static DWORD shader_generate_arb_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
606 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info, DWORD *lconst_map, DWORD *num_clipplanes,
607 struct shader_arb_ctx_priv *ctx)
609 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
610 DWORD i, next_local = 0;
611 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
612 unsigned max_constantsF;
613 const local_constant *lconst;
615 /* In pixel shaders, all private constants are program local, we don't need anything
616 * from program.env. Thus we can advertise the full set of constants in pixel shaders.
617 * If we need a private constant the GL implementation will squeeze it in somewhere
619 * With vertex shaders we need the posFixup and on some GL implementations 4 helper
620 * immediate values. The posFixup is loaded using program.env for now, so always
621 * subtract one from the number of constants. If the shader uses indirect addressing,
622 * account for the helper const too because we have to declare all availabke d3d constants
623 * and don't know which are actually used.
625 if(pshader) {
626 max_constantsF = GL_LIMITS(pshader_constantsF);
627 } else {
628 if(This->baseShader.reg_maps.usesrelconstF) {
629 DWORD highest_constf = 0, clip_limit;
630 max_constantsF = GL_LIMITS(vshader_constantsF) - reserved_vs_const(iface, gl_info);
631 max_constantsF -= count_bits(This->baseShader.reg_maps.integer_constants);
633 for(i = 0; i < This->baseShader.limits.constant_float; i++)
635 DWORD idx = i >> 5;
636 DWORD shift = i & 0x1f;
637 if(reg_maps->constf[idx] & (1 << shift)) highest_constf = i;
640 clip_limit = GL_LIMITS(clipplanes);
641 if(ctx->target_version == ARB) clip_limit = min(clip_limit, 4);
642 *num_clipplanes = min(clip_limit, max_constantsF - highest_constf - 1);
643 max_constantsF -= *num_clipplanes;
644 if(*num_clipplanes < clip_limit)
646 WARN("Only %u clipplanes out of %u enabled\n", *num_clipplanes, GL_LIMITS(clipplanes));
649 else
651 if(ctx->target_version >= NV2) *num_clipplanes = GL_LIMITS(clipplanes);
652 else *num_clipplanes = min(GL_LIMITS(clipplanes), 4);
653 max_constantsF = GL_LIMITS(vshader_constantsF);
657 for(i = 0; i < This->baseShader.limits.temporary; i++) {
658 if (reg_maps->temporary[i])
659 shader_addline(buffer, "TEMP R%u;\n", i);
662 for (i = 0; i < This->baseShader.limits.address; i++) {
663 if (reg_maps->address[i])
664 shader_addline(buffer, "ADDRESS A%d;\n", i);
667 if(pshader && reg_maps->shader_version.major == 1 && reg_maps->shader_version.minor <= 3) {
668 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
669 if (reg_maps->texcoord[i] && pshader)
670 shader_addline(buffer,"TEMP T%u;\n", i);
674 /* Load local constants using the program-local space,
675 * this avoids reloading them each time the shader is used
677 if(lconst_map) {
678 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
679 shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
680 lconst_map[lconst->idx]);
681 next_local = max(next_local, lconst_map[lconst->idx] + 1);
685 /* we use the array-based constants array if the local constants are marked for loading,
686 * because then we use indirect addressing, or when the local constant list is empty,
687 * because then we don't know if we're using indirect addressing or not. If we're hardcoding
688 * local constants do not declare the loaded constants as an array because ARB compilers usually
689 * do not optimize unused constants away
691 if(This->baseShader.reg_maps.usesrelconstF) {
692 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
693 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
694 max_constantsF, max_constantsF - 1);
695 } else {
696 for(i = 0; i < max_constantsF; i++) {
697 DWORD idx, mask;
698 idx = i >> 5;
699 mask = 1 << (i & 0x1f);
700 if(!shader_constant_is_local(This, i) && (This->baseShader.reg_maps.constf[idx] & mask)) {
701 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
706 return next_local;
709 static const char * const shift_tab[] = {
710 "dummy", /* 0 (none) */
711 "coefmul.x", /* 1 (x2) */
712 "coefmul.y", /* 2 (x4) */
713 "coefmul.z", /* 3 (x8) */
714 "coefmul.w", /* 4 (x16) */
715 "dummy", /* 5 (x32) */
716 "dummy", /* 6 (x64) */
717 "dummy", /* 7 (x128) */
718 "dummy", /* 8 (d256) */
719 "dummy", /* 9 (d128) */
720 "dummy", /* 10 (d64) */
721 "dummy", /* 11 (d32) */
722 "coefdiv.w", /* 12 (d16) */
723 "coefdiv.z", /* 13 (d8) */
724 "coefdiv.y", /* 14 (d4) */
725 "coefdiv.x" /* 15 (d2) */
728 static void shader_arb_get_write_mask(const struct wined3d_shader_instruction *ins,
729 const struct wined3d_shader_dst_param *dst, char *write_mask)
731 char *ptr = write_mask;
733 if (dst->write_mask != WINED3DSP_WRITEMASK_ALL)
735 *ptr++ = '.';
736 if (dst->write_mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
737 if (dst->write_mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
738 if (dst->write_mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
739 if (dst->write_mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
742 *ptr = '\0';
745 static void shader_arb_get_swizzle(const struct wined3d_shader_src_param *param, BOOL fixup, char *swizzle_str)
747 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
748 * but addressed as "rgba". To fix this we need to swap the register's x
749 * and z components. */
750 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
751 char *ptr = swizzle_str;
753 /* swizzle bits fields: wwzzyyxx */
754 DWORD swizzle = param->swizzle;
755 DWORD swizzle_x = swizzle & 0x03;
756 DWORD swizzle_y = (swizzle >> 2) & 0x03;
757 DWORD swizzle_z = (swizzle >> 4) & 0x03;
758 DWORD swizzle_w = (swizzle >> 6) & 0x03;
760 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
761 * generate a swizzle string. Unless we need to our own swizzling. */
762 if (swizzle != WINED3DSP_NOSWIZZLE || fixup)
764 *ptr++ = '.';
765 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
766 *ptr++ = swizzle_chars[swizzle_x];
767 } else {
768 *ptr++ = swizzle_chars[swizzle_x];
769 *ptr++ = swizzle_chars[swizzle_y];
770 *ptr++ = swizzle_chars[swizzle_z];
771 *ptr++ = swizzle_chars[swizzle_w];
775 *ptr = '\0';
778 static void shader_arb_request_a0(const struct wined3d_shader_instruction *ins, const char *src)
780 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
781 SHADER_BUFFER *buffer = ins->ctx->buffer;
783 if(strcmp(priv->addr_reg, src) == 0) return;
785 strcpy(priv->addr_reg, src);
786 shader_addline(buffer, "ARL A0.x, %s;\n", src);
789 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
790 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr);
792 static void shader_arb_get_register_name(const struct wined3d_shader_instruction *ins,
793 const struct wined3d_shader_register *reg, char *register_name, BOOL *is_color)
795 /* oPos, oFog and oPts in D3D */
796 static const char * const rastout_reg_names[] = {"TMP_OUT", "result.fogcoord", "result.pointsize"};
797 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
798 BOOL pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
799 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
801 *is_color = FALSE;
803 switch (reg->type)
805 case WINED3DSPR_TEMP:
806 sprintf(register_name, "R%u", reg->idx);
807 break;
809 case WINED3DSPR_INPUT:
810 if (pshader)
812 if(This->baseShader.reg_maps.shader_version.major < 3)
814 if (reg->idx == 0) strcpy(register_name, "fragment.color.primary");
815 else strcpy(register_name, "fragment.color.secondary");
817 else
819 if(reg->rel_addr)
821 char rel_reg[50];
822 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
824 if(strcmp(rel_reg, "**aL_emul**") == 0)
826 DWORD idx = ctx->aL + reg->idx;
827 if(idx < MAX_REG_INPUT)
829 strcpy(register_name, ctx->ps_input[idx]);
831 else
833 ERR("Pixel shader input register out of bounds: %u\n", idx);
834 sprintf(register_name, "out_of_bounds_%u", idx);
837 else if(This->baseShader.reg_maps.input_registers & 0x0300)
839 /* There are two ways basically:
841 * 1) Use the unrolling code that is used for loop emulation and unroll the loop.
842 * That means trouble if the loop also contains a breakc or if the control values
843 * aren't local constants.
844 * 2) Generate an if block that checks if aL.y < 8, == 8 or == 9 and selects the
845 * source dynamically. The trouble is that we cannot simply read aL.y because it
846 * is an ADDRESS register. We could however push it, load .zw with a value and use
847 * ADAC to load the condition code register and pop it again afterwards
849 FIXME("Relative input register addressing with more than 8 registers\n");
851 /* This is better than nothing for now */
852 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
854 else if(ctx->cur_ps_args->super.vp_mode != vertexshader)
856 /* This is problematic because we'd have to consult the ctx->ps_input strings
857 * for where to find the varying. Some may be "0.0", others can be texcoords or
858 * colors. This needs either a pipeline replacement to make the vertex shader feed
859 * proper varyings, or loop unrolling
861 * For now use the texcoords and hope for the best
863 FIXME("Non-vertex shader varying input with indirect addressing\n");
864 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
866 else
868 /* D3D supports indirect addressing only with aL in loop registers. The loop instruction
869 * pulls GL_NV_fragment_program2 in
871 sprintf(register_name, "fragment.texcoord[%s + %u]", rel_reg, reg->idx);
874 else
876 if(reg->idx < MAX_REG_INPUT)
878 strcpy(register_name, ctx->ps_input[reg->idx]);
880 else
882 ERR("Pixel shader input register out of bounds: %u\n", reg->idx);
883 sprintf(register_name, "out_of_bounds_%u", reg->idx);
888 else
890 if (ctx->cur_vs_args->super.swizzle_map & (1 << reg->idx)) *is_color = TRUE;
891 sprintf(register_name, "vertex.attrib[%u]", reg->idx);
893 break;
895 case WINED3DSPR_CONST:
896 if (!pshader && reg->rel_addr)
898 BOOL aL = FALSE;
899 char rel_reg[50];
900 UINT rel_offset = ((IWineD3DVertexShaderImpl *)This)->rel_offset;
901 if(This->baseShader.reg_maps.shader_version.major < 2) {
902 sprintf(rel_reg, "A0.x");
903 } else {
904 shader_arb_get_src_param(ins, reg->rel_addr, 0, rel_reg);
905 if(ctx->target_version == ARB) {
906 if(strcmp(rel_reg, "**aL_emul**") == 0) {
907 aL = TRUE;
908 } else {
909 shader_arb_request_a0(ins, rel_reg);
910 sprintf(rel_reg, "A0.x");
914 if(aL)
915 sprintf(register_name, "C[%u]", ctx->aL + reg->idx);
916 else if (reg->idx >= rel_offset)
917 sprintf(register_name, "C[%s + %u]", rel_reg, reg->idx - rel_offset);
918 else
919 sprintf(register_name, "C[%s - %u]", rel_reg, -reg->idx + rel_offset);
921 else
923 if (This->baseShader.reg_maps.usesrelconstF)
924 sprintf(register_name, "C[%u]", reg->idx);
925 else
926 sprintf(register_name, "C%u", reg->idx);
928 break;
930 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
931 if (pshader) {
932 if(This->baseShader.reg_maps.shader_version.major == 1 &&
933 This->baseShader.reg_maps.shader_version.minor <= 3) {
934 /* In ps <= 1.3, Tx is a temporary register as destination to all instructions,
935 * and as source to most instructions. For some instructions it is the texcoord
936 * input. Those instructions know about the special use
938 sprintf(register_name, "T%u", reg->idx);
939 } else {
940 /* in ps 1.4 and 2.x Tx is always a (read-only) varying */
941 sprintf(register_name, "fragment.texcoord[%u]", reg->idx);
944 else
946 if(This->baseShader.reg_maps.shader_version.major == 1 || ctx->target_version >= NV2)
948 sprintf(register_name, "A%u", reg->idx);
950 else
952 sprintf(register_name, "A%u_SHADOW", reg->idx);
955 break;
957 case WINED3DSPR_COLOROUT:
958 if(ctx->cur_ps_args->super.srgb_correction && reg->idx == 0)
960 strcpy(register_name, "TMP_COLOR");
962 else
964 if(ctx->cur_ps_args->super.srgb_correction) FIXME("sRGB correction on higher render targets\n");
965 if(This->baseShader.reg_maps.highest_render_target > 0)
967 sprintf(register_name, "result.color[%u]", reg->idx);
969 else
971 strcpy(register_name, "result.color");
974 break;
976 case WINED3DSPR_RASTOUT:
977 if(reg->idx == 1) sprintf(register_name, "%s", ctx->fog_output);
978 else sprintf(register_name, "%s", rastout_reg_names[reg->idx]);
979 break;
981 case WINED3DSPR_DEPTHOUT:
982 strcpy(register_name, "result.depth");
983 break;
985 case WINED3DSPR_ATTROUT:
986 /* case WINED3DSPR_OUTPUT: */
987 if (pshader) sprintf(register_name, "oD[%u]", reg->idx);
988 else strcpy(register_name, ctx->color_output[reg->idx]);
989 break;
991 case WINED3DSPR_TEXCRDOUT:
992 if (pshader)
994 sprintf(register_name, "oT[%u]", reg->idx);
996 else
998 if(This->baseShader.reg_maps.shader_version.major < 3)
1000 strcpy(register_name, ctx->texcrd_output[reg->idx]);
1002 else
1004 strcpy(register_name, ctx->vs_output[reg->idx]);
1007 break;
1009 case WINED3DSPR_LOOP:
1010 if(ctx->target_version >= NV2)
1012 /* Pshader has an implicitly declared loop index counter A0.x that cannot be renamed */
1013 if(pshader) sprintf(register_name, "A0.x");
1014 else sprintf(register_name, "aL.y");
1016 else
1018 /* Unfortunately this code cannot return the value of ctx->aL here. An immediate value
1019 * would be valid, but if aL is used for indexing(its only use), there's likely an offset,
1020 * thus the result would be something like C[15 + 30], which is not valid in the ARB program
1021 * grammar. So return a marker for the emulated aL and intercept it in constant and varying
1022 * indexing
1024 sprintf(register_name, "**aL_emul**");
1027 break;
1029 case WINED3DSPR_CONSTINT:
1030 sprintf(register_name, "I%u", reg->idx);
1031 break;
1033 case WINED3DSPR_MISCTYPE:
1034 if(reg->idx == 0)
1036 sprintf(register_name, "vpos");
1038 else if(reg->idx == 1)
1040 sprintf(register_name, "fragment.facing.x");
1042 else
1044 FIXME("Unknown MISCTYPE register index %u\n", reg->idx);
1046 break;
1048 default:
1049 FIXME("Unhandled register type %#x[%u]\n", reg->type, reg->idx);
1050 sprintf(register_name, "unrecognized_register[%u]", reg->idx);
1051 break;
1055 static void shader_arb_get_dst_param(const struct wined3d_shader_instruction *ins,
1056 const struct wined3d_shader_dst_param *wined3d_dst, char *str)
1058 char register_name[255];
1059 char write_mask[6];
1060 BOOL is_color;
1062 shader_arb_get_register_name(ins, &wined3d_dst->reg, register_name, &is_color);
1063 strcpy(str, register_name);
1065 shader_arb_get_write_mask(ins, wined3d_dst, write_mask);
1066 strcat(str, write_mask);
1069 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
1071 switch(channel_source)
1073 case CHANNEL_SOURCE_ZERO: return "0";
1074 case CHANNEL_SOURCE_ONE: return "1";
1075 case CHANNEL_SOURCE_X: return "x";
1076 case CHANNEL_SOURCE_Y: return "y";
1077 case CHANNEL_SOURCE_Z: return "z";
1078 case CHANNEL_SOURCE_W: return "w";
1079 default:
1080 FIXME("Unhandled channel source %#x\n", channel_source);
1081 return "undefined";
1085 static void gen_color_correction(SHADER_BUFFER *buffer, const char *reg, DWORD dst_mask,
1086 const char *one, const char *two, struct color_fixup_desc fixup)
1088 DWORD mask;
1090 if (is_yuv_fixup(fixup))
1092 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1093 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1094 return;
1097 mask = 0;
1098 if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1099 if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1100 if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1101 if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1102 mask &= dst_mask;
1104 if (mask)
1106 shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
1107 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
1108 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
1111 mask = 0;
1112 if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
1113 if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
1114 if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
1115 if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
1116 mask &= dst_mask;
1118 if (mask)
1120 char reg_mask[6];
1121 char *ptr = reg_mask;
1123 if (mask != WINED3DSP_WRITEMASK_ALL)
1125 *ptr++ = '.';
1126 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1127 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1128 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1129 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1131 *ptr = '\0';
1133 shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
1137 static const char *shader_arb_get_modifier(const struct wined3d_shader_instruction *ins)
1139 DWORD mod;
1140 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1141 if (!ins->dst_count) return "";
1143 mod = ins->dst[0].modifiers;
1145 /* Silently ignore PARTIALPRECISION if its not supported */
1146 if(priv->target_version == ARB) mod &= ~WINED3DSPDM_PARTIALPRECISION;
1148 if(mod & WINED3DSPDM_MSAMPCENTROID)
1150 FIXME("Unhandled modifier WINED3DSPDM_MSAMPCENTROID\n");
1151 mod &= ~WINED3DSPDM_MSAMPCENTROID;
1154 switch(mod)
1156 case WINED3DSPDM_SATURATE | WINED3DSPDM_PARTIALPRECISION:
1157 return "H_SAT";
1159 case WINED3DSPDM_SATURATE:
1160 return "_SAT";
1162 case WINED3DSPDM_PARTIALPRECISION:
1163 return "H";
1165 case 0:
1166 return "";
1168 default:
1169 FIXME("Unknown modifiers 0x%08x\n", mod);
1170 return "";
1174 #define TEX_PROJ 0x1
1175 #define TEX_BIAS 0x2
1176 #define TEX_LOD 0x4
1177 #define TEX_DERIV 0x10
1179 static void shader_hw_sample(const struct wined3d_shader_instruction *ins, DWORD sampler_idx,
1180 const char *dst_str, const char *coord_reg, WORD flags, const char *dsx, const char *dsy)
1182 SHADER_BUFFER *buffer = ins->ctx->buffer;
1183 DWORD sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
1184 const char *tex_type;
1185 BOOL np2_fixup = FALSE;
1186 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1187 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
1188 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
1189 const char *mod;
1190 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
1192 /* D3D vertex shader sampler IDs are vertex samplers(0-3), not global d3d samplers */
1193 if(!pshader) sampler_idx += MAX_FRAGMENT_SAMPLERS;
1195 switch(sampler_type) {
1196 case WINED3DSTT_1D:
1197 tex_type = "1D";
1198 break;
1200 case WINED3DSTT_2D:
1201 if(device->stateBlock->textures[sampler_idx] &&
1202 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
1203 tex_type = "RECT";
1204 } else {
1205 tex_type = "2D";
1207 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1209 if (priv->cur_np2fixup_info->super.active & (1 << sampler_idx))
1211 if (flags) FIXME("Only ordinary sampling from NP2 textures is supported.\n");
1212 else np2_fixup = TRUE;
1215 break;
1217 case WINED3DSTT_VOLUME:
1218 tex_type = "3D";
1219 break;
1221 case WINED3DSTT_CUBE:
1222 tex_type = "CUBE";
1223 break;
1225 default:
1226 ERR("Unexpected texture type %d\n", sampler_type);
1227 tex_type = "";
1230 /* TEX, TXL, TXD and TXP do not support the "H" modifier,
1231 * so don't use shader_arb_get_modifier
1233 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) mod = "_SAT";
1234 else mod = "";
1236 /* Fragment samplers always have indentity mapping */
1237 if(sampler_idx >= MAX_FRAGMENT_SAMPLERS)
1239 sampler_idx = priv->cur_vs_args->vertex_samplers[sampler_idx - MAX_FRAGMENT_SAMPLERS];
1242 if (flags & TEX_DERIV)
1244 if(flags & TEX_PROJ) FIXME("Projected texture sampling with custom derivates\n");
1245 if(flags & TEX_BIAS) FIXME("Biased texture sampling with custom derivates\n");
1246 shader_addline(buffer, "TXD%s %s, %s, %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg,
1247 dsx, dsy,sampler_idx, tex_type);
1249 else if(flags & TEX_LOD)
1251 if(flags & TEX_PROJ) FIXME("Projected texture sampling with explicit lod\n");
1252 if(flags & TEX_BIAS) FIXME("Biased texture sampling with explicit lod\n");
1253 shader_addline(buffer, "TXL%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg,
1254 sampler_idx, tex_type);
1256 else if (flags & TEX_BIAS)
1258 /* Shouldn't be possible, but let's check for it */
1259 if(flags & TEX_PROJ) FIXME("Biased and Projected texture sampling\n");
1260 /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
1261 shader_addline(buffer, "TXB%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1263 else if (flags & TEX_PROJ)
1265 shader_addline(buffer, "TXP%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1267 else
1269 if (np2_fixup)
1271 const unsigned char idx = priv->cur_np2fixup_info->super.idx[sampler_idx];
1272 shader_addline(buffer, "MUL TA, np2fixup[%u].%s, %s;\n", idx >> 1,
1273 (idx % 2) ? "zwxy" : "xyzw", coord_reg);
1275 shader_addline(buffer, "TEX%s %s, TA, texture[%u], %s;\n", mod, dst_str, sampler_idx, tex_type);
1277 else
1278 shader_addline(buffer, "TEX%s %s, %s, texture[%u], %s;\n", mod, dst_str, coord_reg, sampler_idx, tex_type);
1281 if (pshader)
1283 gen_color_correction(buffer, dst_str, ins->dst[0].write_mask,
1284 "one", "coefmul.x", priv->cur_ps_args->super.color_fixup[sampler_idx]);
1288 static void shader_arb_get_src_param(const struct wined3d_shader_instruction *ins,
1289 const struct wined3d_shader_src_param *src, unsigned int tmpreg, char *outregstr)
1291 /* Generate a line that does the input modifier computation and return the input register to use */
1292 BOOL is_color = FALSE;
1293 char regstr[256];
1294 char swzstr[20];
1295 int insert_line;
1296 SHADER_BUFFER *buffer = ins->ctx->buffer;
1297 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1299 /* Assume a new line will be added */
1300 insert_line = 1;
1302 /* Get register name */
1303 shader_arb_get_register_name(ins, &src->reg, regstr, &is_color);
1304 shader_arb_get_swizzle(src, is_color, swzstr);
1306 switch (src->modifiers)
1308 case WINED3DSPSM_NONE:
1309 sprintf(outregstr, "%s%s", regstr, swzstr);
1310 insert_line = 0;
1311 break;
1312 case WINED3DSPSM_NEG:
1313 sprintf(outregstr, "-%s%s", regstr, swzstr);
1314 insert_line = 0;
1315 break;
1316 case WINED3DSPSM_BIAS:
1317 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
1318 break;
1319 case WINED3DSPSM_BIASNEG:
1320 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
1321 break;
1322 case WINED3DSPSM_SIGN:
1323 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
1324 break;
1325 case WINED3DSPSM_SIGNNEG:
1326 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
1327 break;
1328 case WINED3DSPSM_COMP:
1329 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
1330 break;
1331 case WINED3DSPSM_X2:
1332 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
1333 break;
1334 case WINED3DSPSM_X2NEG:
1335 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
1336 break;
1337 case WINED3DSPSM_DZ:
1338 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
1339 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1340 break;
1341 case WINED3DSPSM_DW:
1342 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
1343 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
1344 break;
1345 case WINED3DSPSM_ABS:
1346 if(ctx->target_version >= NV2) {
1347 sprintf(outregstr, "|%s%s|", regstr, swzstr);
1348 insert_line = 0;
1349 } else {
1350 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1352 break;
1353 case WINED3DSPSM_ABSNEG:
1354 if(ctx->target_version >= NV2) {
1355 sprintf(outregstr, "-|%s%s|", regstr, swzstr);
1356 } else {
1357 shader_addline(buffer, "ABS T%c, %s;\n", 'A' + tmpreg, regstr);
1358 sprintf(outregstr, "-T%c%s", 'A' + tmpreg, swzstr);
1360 insert_line = 0;
1361 break;
1362 default:
1363 sprintf(outregstr, "%s%s", regstr, swzstr);
1364 insert_line = 0;
1367 /* Return modified or original register, with swizzle */
1368 if (insert_line)
1369 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
1372 static void pshader_hw_bem(const struct wined3d_shader_instruction *ins)
1374 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1375 SHADER_BUFFER *buffer = ins->ctx->buffer;
1376 char dst_name[50];
1377 char src_name[2][50];
1378 DWORD sampler_code = dst->reg.idx;
1380 shader_arb_get_dst_param(ins, dst, dst_name);
1382 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1384 * Keep in mind that src_name[1] can be "TB" and src_name[0] can be "TA" because modifiers like _x2 are valid
1385 * with bem. So delay loading the first parameter until after the perturbation calculation which needs two
1386 * temps is done.
1388 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1389 shader_addline(buffer, "SWZ TA, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
1390 shader_addline(buffer, "DP3 TC.r, TA, %s;\n", src_name[1]);
1391 shader_addline(buffer, "SWZ TA, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
1392 shader_addline(buffer, "DP3 TC.g, TA, %s;\n", src_name[1]);
1394 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1395 shader_addline(buffer, "ADD %s, %s, TC;\n", dst_name, src_name[0]);
1398 static DWORD negate_modifiers(DWORD mod, char *extra_char)
1400 *extra_char = ' ';
1401 switch(mod)
1403 case WINED3DSPSM_NONE: return WINED3DSPSM_NEG;
1404 case WINED3DSPSM_NEG: return WINED3DSPSM_NONE;
1405 case WINED3DSPSM_BIAS: return WINED3DSPSM_BIASNEG;
1406 case WINED3DSPSM_BIASNEG: return WINED3DSPSM_BIAS;
1407 case WINED3DSPSM_SIGN: return WINED3DSPSM_SIGNNEG;
1408 case WINED3DSPSM_SIGNNEG: return WINED3DSPSM_SIGN;
1409 case WINED3DSPSM_COMP: *extra_char = '-'; return WINED3DSPSM_COMP;
1410 case WINED3DSPSM_X2: return WINED3DSPSM_X2NEG;
1411 case WINED3DSPSM_X2NEG: return WINED3DSPSM_X2;
1412 case WINED3DSPSM_DZ: *extra_char = '-'; return WINED3DSPSM_DZ;
1413 case WINED3DSPSM_DW: *extra_char = '-'; return WINED3DSPSM_DW;
1414 case WINED3DSPSM_ABS: return WINED3DSPSM_ABSNEG;
1415 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
1417 FIXME("Unknown modifier %u\n", mod);
1418 return mod;
1421 static void pshader_hw_cnd(const struct wined3d_shader_instruction *ins)
1423 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1424 SHADER_BUFFER *buffer = ins->ctx->buffer;
1425 char dst_name[50];
1426 char src_name[3][50];
1427 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1428 ins->ctx->reg_maps->shader_version.minor);
1429 BOOL is_color;
1431 shader_arb_get_dst_param(ins, dst, dst_name);
1432 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1434 /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
1435 if (shader_version <= WINED3D_SHADER_VERSION(1, 3) && ins->coissue)
1437 shader_addline(buffer, "MOV%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[1]);
1438 } else {
1439 struct wined3d_shader_src_param src0_copy = ins->src[0];
1440 char extra_neg;
1442 /* src0 may have a negate srcmod set, so we can't blindly add "-" to the name */
1443 src0_copy.modifiers = negate_modifiers(src0_copy.modifiers, &extra_neg);
1445 shader_arb_get_src_param(ins, &src0_copy, 0, src_name[0]);
1446 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1447 shader_addline(buffer, "ADD TA, %c%s, coefdiv.x;\n", extra_neg, src_name[0]);
1448 /* No modifiers supported on CMP */
1449 shader_addline(buffer, "CMP %s, TA, %s, %s;\n", dst_name, src_name[1], src_name[2]);
1451 /* _SAT on CMP doesn't make much sense, but it is not a pure NOP */
1452 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
1454 shader_arb_get_register_name(ins, &dst->reg, src_name[0], &is_color);
1455 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, dst_name);
1460 static void pshader_hw_cmp(const struct wined3d_shader_instruction *ins)
1462 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1463 SHADER_BUFFER *buffer = ins->ctx->buffer;
1464 char dst_name[50];
1465 char src_name[3][50];
1466 BOOL is_color;
1468 shader_arb_get_dst_param(ins, dst, dst_name);
1470 /* Generate input register names (with modifiers) */
1471 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1472 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1473 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1475 /* No modifiers are supported on CMP */
1476 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
1477 src_name[0], src_name[2], src_name[1]);
1479 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
1481 shader_arb_get_register_name(ins, &dst->reg, src_name[0], &is_color);
1482 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name[0]);
1486 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
1487 * dst = dot2(src0, src1) + src2 */
1488 static void pshader_hw_dp2add(const struct wined3d_shader_instruction *ins)
1490 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1491 SHADER_BUFFER *buffer = ins->ctx->buffer;
1492 char dst_name[50];
1493 char src_name[3][50];
1494 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1496 shader_arb_get_dst_param(ins, dst, dst_name);
1497 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
1498 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
1500 if(ctx->target_version >= NV3)
1502 /* GL_NV_fragment_program2 has a 1:1 matching instruction */
1503 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1504 shader_addline(buffer, "DP2A%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1505 dst_name, src_name[0], src_name[1], src_name[2]);
1507 else if(ctx->target_version >= NV2)
1509 /* dst.x = src2.?, src0.x, src1.x + src0.y * src1.y
1510 * dst.y = src2.?, src0.x, src1.z + src0.y * src1.w
1511 * dst.z = src2.?, src0.x, src1.x + src0.y * src1.y
1512 * dst.z = src2.?, src0.x, src1.z + src0.y * src1.w
1514 * Make sure that src1.zw = src1.xy, then we get a classic dp2add
1516 * .xyxy and other swizzles that we could get with this are not valid in
1517 * plain ARBfp, but luckily the NV extension grammar lifts this limitation.
1519 struct wined3d_shader_src_param tmp_param = ins->src[1];
1520 DWORD swizzle = tmp_param.swizzle & 0xf; /* Selects .xy */
1521 tmp_param.swizzle = swizzle | (swizzle << 4); /* Creates .xyxy */
1523 shader_arb_get_src_param(ins, &tmp_param, 1, src_name[1]);
1525 shader_addline(buffer, "X2D%s %s, %s, %s, %s;\n", shader_arb_get_modifier(ins),
1526 dst_name, src_name[2], src_name[0], src_name[1]);
1528 else
1530 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
1531 /* 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]
1532 * src_name[0] can be TA, but TA is a private temp for modifiers, so it is save to overwrite
1534 shader_addline(buffer, "MOV TA, %s;\n", src_name[0]);
1535 shader_addline(buffer, "MOV TA.z, 0.0;\n");
1536 shader_addline(buffer, "DP3 TA, TA, %s;\n", src_name[1]);
1537 shader_addline(buffer, "ADD%s %s, TA, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name[2]);
1541 /* Map the opcode 1-to-1 to the GL code */
1542 static void shader_hw_map2gl(const struct wined3d_shader_instruction *ins)
1544 SHADER_BUFFER *buffer = ins->ctx->buffer;
1545 const char *instruction;
1546 char arguments[256], dst_str[50];
1547 unsigned int i;
1548 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1550 switch (ins->handler_idx)
1552 case WINED3DSIH_ABS: instruction = "ABS"; break;
1553 case WINED3DSIH_ADD: instruction = "ADD"; break;
1554 case WINED3DSIH_CRS: instruction = "XPD"; break;
1555 case WINED3DSIH_DP3: instruction = "DP3"; break;
1556 case WINED3DSIH_DP4: instruction = "DP4"; break;
1557 case WINED3DSIH_DST: instruction = "DST"; break;
1558 case WINED3DSIH_EXP: instruction = "EX2"; break;
1559 case WINED3DSIH_EXPP: instruction = "EXP"; break;
1560 case WINED3DSIH_FRC: instruction = "FRC"; break;
1561 case WINED3DSIH_LIT: instruction = "LIT"; break;
1562 case WINED3DSIH_LRP: instruction = "LRP"; break;
1563 case WINED3DSIH_MAD: instruction = "MAD"; break;
1564 case WINED3DSIH_MAX: instruction = "MAX"; break;
1565 case WINED3DSIH_MIN: instruction = "MIN"; break;
1566 case WINED3DSIH_MOV: instruction = "MOV"; break;
1567 case WINED3DSIH_MUL: instruction = "MUL"; break;
1568 case WINED3DSIH_SGE: instruction = "SGE"; break;
1569 case WINED3DSIH_SLT: instruction = "SLT"; break;
1570 case WINED3DSIH_SUB: instruction = "SUB"; break;
1571 case WINED3DSIH_MOVA:instruction = "ARR"; break;
1572 case WINED3DSIH_SGN: instruction = "SSG"; break;
1573 case WINED3DSIH_DSX: instruction = "DDX"; break;
1574 default: instruction = "";
1575 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
1576 break;
1579 /* Note that shader_arb_add_dst_param() adds spaces. */
1580 arguments[0] = '\0';
1581 shader_arb_get_dst_param(ins, dst, dst_str);
1582 for (i = 0; i < ins->src_count; ++i)
1584 char operand[100];
1585 strcat(arguments, ", ");
1586 shader_arb_get_src_param(ins, &ins->src[i], i, operand);
1587 strcat(arguments, operand);
1589 shader_addline(buffer, "%s%s %s%s;\n", instruction, shader_arb_get_modifier(ins), dst_str, arguments);
1592 static void shader_hw_nop(const struct wined3d_shader_instruction *ins)
1594 SHADER_BUFFER *buffer = ins->ctx->buffer;
1595 shader_addline(buffer, "NOP;\n");
1598 static void shader_hw_mov(const struct wined3d_shader_instruction *ins)
1600 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1601 BOOL pshader = shader_is_pshader_version(shader->baseShader.reg_maps.shader_version.type);
1602 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
1604 SHADER_BUFFER *buffer = ins->ctx->buffer;
1605 char src0_param[256];
1607 if(ins->handler_idx == WINED3DSIH_MOVA) {
1608 struct wined3d_shader_src_param tmp_src = ins->src[0];
1609 char write_mask[6];
1611 if(ctx->target_version >= NV2) {
1612 shader_hw_map2gl(ins);
1613 return;
1615 tmp_src.swizzle = (tmp_src.swizzle & 0x3) * 0x55;
1616 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1617 shader_arb_get_write_mask(ins, &ins->dst[0], write_mask);
1619 /* This implements the mova formula used in GLSL. The first two instructions
1620 * prepare the sign() part. Note that it is fine to have my_sign(0.0) = 1.0
1621 * in this case:
1622 * mova A0.x, 0.0
1624 * A0.x = arl(floor(abs(0.0) + 0.5) * 1.0) = floor(0.5) = 0.0 since arl does a floor
1626 * The ARL is performed when A0 is used - the requested component is read from A0_SHADOW into
1627 * A0.x. We can use the overwritten component of A0_shadow as temporary storage for the sign.
1629 shader_addline(buffer, "SGE A0_SHADOW%s, %s, mova_const.y;\n", write_mask, src0_param);
1630 shader_addline(buffer, "MAD A0_SHADOW%s, A0_SHADOW, mova_const.z, -mova_const.w;\n", write_mask);
1632 shader_addline(buffer, "ABS TA%s, %s;\n", write_mask, src0_param);
1633 shader_addline(buffer, "ADD TA%s, TA, mova_const.x;\n", write_mask);
1634 shader_addline(buffer, "FLR TA%s, TA;\n", write_mask);
1635 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1637 shader_addline(buffer, "ADD TA%s, TA, helper_const.z;\n", write_mask);
1639 shader_addline(buffer, "MUL A0_SHADOW%s, TA, A0_SHADOW;\n", write_mask);
1641 ((struct shader_arb_ctx_priv *)ins->ctx->backend_data)->addr_reg[0] = '\0';
1642 } else if (ins->ctx->reg_maps->shader_version.major == 1
1643 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
1644 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1646 src0_param[0] = '\0';
1647 if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1649 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_param);
1650 shader_addline(buffer, "ADD TA.x, %s, helper_const.z;\n", src0_param);
1651 shader_addline(buffer, "ARL A0.x, TA.x;\n");
1653 else
1655 /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1656 * with more than one component. Thus replicate the first source argument over all
1657 * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc) */
1658 struct wined3d_shader_src_param tmp_src = ins->src[0];
1659 tmp_src.swizzle = (tmp_src.swizzle & 0x3) * 0x55;
1660 shader_arb_get_src_param(ins, &tmp_src, 0, src0_param);
1661 shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1664 else if(ins->dst[0].reg.type == WINED3DSPR_COLOROUT && ins->dst[0].reg.idx == 0 && pshader)
1666 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) shader;
1667 if(ctx->cur_ps_args->super.srgb_correction && ps->color0_mov)
1669 shader_addline(buffer, "#mov handled in srgb write code\n");
1670 return;
1672 shader_hw_map2gl(ins);
1674 else
1676 shader_hw_map2gl(ins);
1680 static void pshader_hw_texkill(const struct wined3d_shader_instruction *ins)
1682 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1683 SHADER_BUFFER *buffer = ins->ctx->buffer;
1684 char reg_dest[40];
1686 /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1687 * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1689 shader_arb_get_dst_param(ins, dst, reg_dest);
1691 if (ins->ctx->reg_maps->shader_version.major >= 2)
1693 const char *kilsrc = "TA";
1694 BOOL is_color;
1696 shader_arb_get_register_name(ins, &dst->reg, reg_dest, &is_color);
1697 if(dst->write_mask == WINED3DSP_WRITEMASK_ALL)
1699 kilsrc = reg_dest;
1701 else
1703 /* Sigh. KIL doesn't support swizzles/writemasks. KIL passes a writemask, but ".xy" for example
1704 * is not valid as a swizzle in ARB (needs ".xyyy"). Use SWZ to load the register properly, and set
1705 * masked out components to 0(won't kill)
1707 char x = '0', y = '0', z = '0', w = '0';
1708 if(dst->write_mask & WINED3DSP_WRITEMASK_0) x = 'x';
1709 if(dst->write_mask & WINED3DSP_WRITEMASK_1) y = 'y';
1710 if(dst->write_mask & WINED3DSP_WRITEMASK_2) z = 'z';
1711 if(dst->write_mask & WINED3DSP_WRITEMASK_3) w = 'w';
1712 shader_addline(buffer, "SWZ TA, %s, %c, %c, %c, %c;\n", reg_dest, x, y, z, w);
1714 shader_addline(buffer, "KIL %s;\n", kilsrc);
1715 } else {
1716 /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1717 * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1719 * ps_1_3 shaders use the texcoord incarnation of the Tx register. ps_1_4 shaders can use the same,
1720 * or pass in any temporary register(in shader phase 2)
1722 if(ins->ctx->reg_maps->shader_version.minor <= 3) {
1723 sprintf(reg_dest, "fragment.texcoord[%u]", dst->reg.idx);
1724 } else {
1725 shader_arb_get_dst_param(ins, dst, reg_dest);
1727 shader_addline(buffer, "SWZ TA, %s, x, y, z, 1;\n", reg_dest);
1728 shader_addline(buffer, "KIL TA;\n");
1732 static void pshader_hw_tex(const struct wined3d_shader_instruction *ins)
1734 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1735 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1736 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1737 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1738 ins->ctx->reg_maps->shader_version.minor);
1739 struct wined3d_shader_src_param src;
1741 char reg_dest[40];
1742 char reg_coord[40];
1743 DWORD reg_sampler_code;
1744 DWORD myflags = 0;
1746 /* All versions have a destination register */
1747 shader_arb_get_dst_param(ins, dst, reg_dest);
1749 /* 1.0-1.4: Use destination register number as texture code.
1750 2.0+: Use provided sampler number as texure code. */
1751 if (shader_version < WINED3D_SHADER_VERSION(2,0))
1752 reg_sampler_code = dst->reg.idx;
1753 else
1754 reg_sampler_code = ins->src[1].reg.idx;
1756 /* 1.0-1.3: Use the texcoord varying.
1757 1.4+: Use provided coordinate source register. */
1758 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1759 sprintf(reg_coord, "fragment.texcoord[%u]", reg_sampler_code);
1760 else {
1761 /* TEX is the only instruction that can handle DW and DZ natively */
1762 src = ins->src[0];
1763 if(src.modifiers == WINED3DSPSM_DW) src.modifiers = WINED3DSPSM_NONE;
1764 if(src.modifiers == WINED3DSPSM_DZ) src.modifiers = WINED3DSPSM_NONE;
1765 shader_arb_get_src_param(ins, &src, 0, reg_coord);
1768 /* projection flag:
1769 * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1770 * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1771 * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1773 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1775 DWORD flags = 0;
1776 if(reg_sampler_code < MAX_TEXTURES) {
1777 flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1779 if (flags & WINED3DTTFF_PROJECTED) {
1780 myflags |= TEX_PROJ;
1783 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
1785 DWORD src_mod = ins->src[0].modifiers;
1786 if (src_mod == WINED3DSPSM_DZ) {
1787 /* TXP cannot handle DZ natively, so move the z coordinate to .w. reg_coord is a read-only
1788 * varying register, so we need a temp reg
1790 shader_addline(ins->ctx->buffer, "SWZ TA, %s, x, y, z, z;\n", reg_coord);
1791 strcpy(reg_coord, "TA");
1792 myflags |= TEX_PROJ;
1793 } else if(src_mod == WINED3DSPSM_DW) {
1794 myflags |= TEX_PROJ;
1796 } else {
1797 if (ins->flags & WINED3DSI_TEXLD_PROJECT) myflags |= TEX_PROJ;
1798 if (ins->flags & WINED3DSI_TEXLD_BIAS) myflags |= TEX_BIAS;
1800 shader_hw_sample(ins, reg_sampler_code, reg_dest, reg_coord, myflags, NULL, NULL);
1803 static void pshader_hw_texcoord(const struct wined3d_shader_instruction *ins)
1805 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1806 SHADER_BUFFER *buffer = ins->ctx->buffer;
1807 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
1808 ins->ctx->reg_maps->shader_version.minor);
1809 char dst_str[50];
1811 if (shader_version < WINED3D_SHADER_VERSION(1,4))
1813 DWORD reg = dst->reg.idx;
1815 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1816 shader_addline(buffer, "MOV_SAT %s, fragment.texcoord[%u];\n", dst_str, reg);
1817 } else {
1818 char reg_src[40];
1820 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src);
1821 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1822 shader_addline(buffer, "MOV %s, %s;\n", dst_str, reg_src);
1826 static void pshader_hw_texreg2ar(const struct wined3d_shader_instruction *ins)
1828 SHADER_BUFFER *buffer = ins->ctx->buffer;
1829 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1830 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1831 DWORD flags;
1833 DWORD reg1 = ins->dst[0].reg.idx;
1834 char dst_str[50];
1835 char src_str[50];
1837 /* Note that texreg2ar treats Tx as a temporary register, not as a varying */
1838 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1839 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1840 /* Move .x first in case src_str is "TA" */
1841 shader_addline(buffer, "MOV TA.y, %s.x;\n", src_str);
1842 shader_addline(buffer, "MOV TA.x, %s.w;\n", src_str);
1843 flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1844 shader_hw_sample(ins, reg1, dst_str, "TA", flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
1847 static void pshader_hw_texreg2gb(const struct wined3d_shader_instruction *ins)
1849 SHADER_BUFFER *buffer = ins->ctx->buffer;
1851 DWORD reg1 = ins->dst[0].reg.idx;
1852 char dst_str[50];
1853 char src_str[50];
1855 /* Note that texreg2gb treats Tx as a temporary register, not as a varying */
1856 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1857 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1858 shader_addline(buffer, "MOV TA.x, %s.y;\n", src_str);
1859 shader_addline(buffer, "MOV TA.y, %s.z;\n", src_str);
1860 shader_hw_sample(ins, reg1, dst_str, "TA", 0, NULL, NULL);
1863 static void pshader_hw_texreg2rgb(const struct wined3d_shader_instruction *ins)
1865 DWORD reg1 = ins->dst[0].reg.idx;
1866 char dst_str[50];
1867 char src_str[50];
1869 /* Note that texreg2rg treats Tx as a temporary register, not as a varying */
1870 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1871 shader_arb_get_src_param(ins, &ins->src[0], 0, src_str);
1872 shader_hw_sample(ins, reg1, dst_str, src_str, 0, NULL, NULL);
1875 static void pshader_hw_texbem(const struct wined3d_shader_instruction *ins)
1877 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1878 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
1879 SHADER_BUFFER *buffer = ins->ctx->buffer;
1880 char reg_coord[40], dst_reg[50], src_reg[50];
1881 DWORD reg_dest_code;
1883 /* All versions have a destination register. The Tx where the texture coordinates come
1884 * from is the varying incarnation of the texture register
1886 reg_dest_code = dst->reg.idx;
1887 shader_arb_get_dst_param(ins, &ins->dst[0], dst_reg);
1888 shader_arb_get_src_param(ins, &ins->src[0], 0, src_reg);
1889 sprintf(reg_coord, "fragment.texcoord[%u]", reg_dest_code);
1891 /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed
1892 * The Tx in which the perturbation map is stored is the tempreg incarnation of the texture register
1894 * GL_NV_fragment_program_option could handle this in one instruction via X2D:
1895 * X2D TA.xy, fragment.texcoord, T%u, bumpenvmat%u.xzyw
1897 * However, the NV extensions are never enabled for <= 2.0 shaders because of the performance penalty that
1898 * comes with it, and texbem is an 1.x only instruction. No 1.x instruction forces us to enable the NV
1899 * extension.
1901 shader_addline(buffer, "SWZ TB, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1902 shader_addline(buffer, "DP3 TA.x, TB, %s;\n", src_reg);
1903 shader_addline(buffer, "SWZ TB, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1904 shader_addline(buffer, "DP3 TA.y, TB, %s;\n", src_reg);
1906 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1907 * so we can't let the GL handle this.
1909 if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1910 & WINED3DTTFF_PROJECTED) {
1911 shader_addline(buffer, "RCP TB.w, %s.w;\n", reg_coord);
1912 shader_addline(buffer, "MUL TB.xy, %s, TB.w;\n", reg_coord);
1913 shader_addline(buffer, "ADD TA.xy, TA, TB;\n");
1914 } else {
1915 shader_addline(buffer, "ADD TA.xy, TA, %s;\n", reg_coord);
1918 shader_hw_sample(ins, reg_dest_code, dst_reg, "TA", 0, NULL, NULL);
1920 if (ins->handler_idx == WINED3DSIH_TEXBEML)
1922 /* No src swizzles are allowed, so this is ok */
1923 shader_addline(buffer, "MAD TA, %s.z, luminance%d.x, luminance%d.y;\n",
1924 src_reg, reg_dest_code, reg_dest_code);
1925 shader_addline(buffer, "MUL %s, %s, TA;\n", dst_reg, dst_reg);
1929 static void pshader_hw_texm3x2pad(const struct wined3d_shader_instruction *ins)
1931 DWORD reg = ins->dst[0].reg.idx;
1932 SHADER_BUFFER *buffer = ins->ctx->buffer;
1933 char src0_name[50], dst_name[50];
1934 BOOL is_color;
1935 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
1937 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1938 /* The next instruction will be a texm3x2tex or texm3x2depth that writes to the uninitialized
1939 * T<reg+1> register. Use this register to store the calculated vector
1941 tmp_reg.idx = reg + 1;
1942 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
1943 shader_addline(buffer, "DP3 %s.x, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
1946 static void pshader_hw_texm3x2tex(const struct wined3d_shader_instruction *ins)
1948 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1949 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1950 DWORD flags;
1951 DWORD reg = ins->dst[0].reg.idx;
1952 SHADER_BUFFER *buffer = ins->ctx->buffer;
1953 char dst_str[50];
1954 char src0_name[50];
1955 char dst_reg[50];
1956 BOOL is_color;
1958 /* We know that we're writing to the uninitialized T<reg> register, so use it for temporary storage */
1959 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
1961 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
1962 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1963 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
1964 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1965 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
1968 static void pshader_hw_texm3x3pad(const struct wined3d_shader_instruction *ins)
1970 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1971 DWORD reg = ins->dst[0].reg.idx;
1972 SHADER_BUFFER *buffer = ins->ctx->buffer;
1973 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1974 char src0_name[50], dst_name[50];
1975 struct wined3d_shader_register tmp_reg = ins->dst[0].reg;
1976 BOOL is_color;
1978 /* There are always 2 texm3x3pad instructions followed by one texm3x3[tex,vspec, ...] instruction, with
1979 * incrementing ins->dst[0].register_idx numbers. So the pad instruction already knows the final destination
1980 * register, and this register is uninitialized(otherwise the assembler complains that it is 'redeclared')
1982 tmp_reg.idx = reg + 2 - current_state->current_row;
1983 shader_arb_get_register_name(ins, &tmp_reg, dst_name, &is_color);
1985 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
1986 shader_addline(buffer, "DP3 %s.%c, fragment.texcoord[%u], %s;\n",
1987 dst_name, 'x' + current_state->current_row, reg, src0_name);
1988 current_state->texcoord_w[current_state->current_row++] = reg;
1991 static void pshader_hw_texm3x3tex(const struct wined3d_shader_instruction *ins)
1993 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1994 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1995 DWORD flags;
1996 DWORD reg = ins->dst[0].reg.idx;
1997 SHADER_BUFFER *buffer = ins->ctx->buffer;
1998 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1999 char dst_str[50];
2000 char src0_name[50], dst_name[50];
2001 BOOL is_color;
2003 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2004 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2005 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, reg, src0_name);
2007 /* Sample the texture using the calculated coordinates */
2008 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2009 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
2010 shader_hw_sample(ins, reg, dst_str, dst_name, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2011 current_state->current_row = 0;
2014 static void pshader_hw_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2016 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2017 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2018 DWORD flags;
2019 DWORD reg = ins->dst[0].reg.idx;
2020 SHADER_BUFFER *buffer = ins->ctx->buffer;
2021 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2022 char dst_str[50];
2023 char src0_name[50];
2024 char dst_reg[8];
2025 BOOL is_color;
2027 /* Get the dst reg without writemask strings. We know this register is uninitialized, so we can use all
2028 * components for temporary data storage
2030 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2031 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2032 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2034 /* Construct the eye-ray vector from w coordinates */
2035 shader_addline(buffer, "MOV TB.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
2036 shader_addline(buffer, "MOV TB.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
2037 shader_addline(buffer, "MOV TB.z, fragment.texcoord[%u].w;\n", reg);
2039 /* Calculate reflection vector
2041 shader_addline(buffer, "DP3 %s.w, %s, TB;\n", dst_reg, dst_reg);
2042 /* The .w is ignored when sampling, so I can use TB.w to calculate dot(N, N) */
2043 shader_addline(buffer, "DP3 TB.w, %s, %s;\n", dst_reg, dst_reg);
2044 shader_addline(buffer, "RCP TB.w, TB.w;\n");
2045 shader_addline(buffer, "MUL %s.w, %s.w, TB.w;\n", dst_reg, dst_reg);
2046 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2047 shader_addline(buffer, "MAD %s, coefmul.x, %s, -TB;\n", dst_reg, dst_reg);
2049 /* Sample the texture using the calculated coordinates */
2050 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2051 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
2052 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2053 current_state->current_row = 0;
2056 static void pshader_hw_texm3x3spec(const struct wined3d_shader_instruction *ins)
2058 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2059 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2060 DWORD flags;
2061 DWORD reg = ins->dst[0].reg.idx;
2062 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2063 SHADER_BUFFER *buffer = ins->ctx->buffer;
2064 char dst_str[50];
2065 char src0_name[50];
2066 char src1_name[50];
2067 char dst_reg[8];
2068 BOOL is_color;
2070 shader_arb_get_src_param(ins, &ins->src[0], 0, src0_name);
2071 shader_arb_get_src_param(ins, &ins->src[0], 1, src1_name);
2072 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_reg, &is_color);
2073 /* Note: dst_reg.xy is input here, generated by two texm3x3pad instructions */
2074 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_reg, reg, src0_name);
2076 /* Calculate reflection vector.
2078 * dot(N, E)
2079 * dst_reg.xyz = 2 * --------- * N - E
2080 * dot(N, N)
2082 * Which normalizes the normal vector
2084 shader_addline(buffer, "DP3 %s.w, %s, %s;\n", dst_reg, dst_reg, src1_name);
2085 shader_addline(buffer, "DP3 TC.w, %s, %s;\n", dst_reg, dst_reg);
2086 shader_addline(buffer, "RCP TC.w, TC.w;\n");
2087 shader_addline(buffer, "MUL %s.w, %s.w, TC.w;\n", dst_reg, dst_reg);
2088 shader_addline(buffer, "MUL %s, %s.w, %s;\n", dst_reg, dst_reg, dst_reg);
2089 shader_addline(buffer, "MAD %s, coefmul.x, %s, -%s;\n", dst_reg, dst_reg, src1_name);
2091 /* Sample the texture using the calculated coordinates */
2092 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2093 flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
2094 shader_hw_sample(ins, reg, dst_str, dst_reg, flags & WINED3DTTFF_PROJECTED ? TEX_PROJ : 0, NULL, NULL);
2095 current_state->current_row = 0;
2098 static void pshader_hw_texdepth(const struct wined3d_shader_instruction *ins)
2100 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2101 SHADER_BUFFER *buffer = ins->ctx->buffer;
2102 char dst_name[50];
2104 /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
2105 * which is essentially an input, is the destination register because it is the first
2106 * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
2107 * here(writemasks/swizzles are not valid on texdepth)
2109 shader_arb_get_dst_param(ins, dst, dst_name);
2111 /* According to the msdn, the source register(must be r5) is unusable after
2112 * the texdepth instruction, so we're free to modify it
2114 shader_addline(buffer, "MIN %s.y, %s.y, one.y;\n", dst_name, dst_name);
2116 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2117 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2118 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2120 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2121 shader_addline(buffer, "MUL TA.x, %s.x, %s.y;\n", dst_name, dst_name);
2122 shader_addline(buffer, "MIN TA.x, TA.x, one.x;\n");
2123 shader_addline(buffer, "MAX result.depth, TA.x, 0.0;\n");
2126 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
2127 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2128 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2129 static void pshader_hw_texdp3tex(const struct wined3d_shader_instruction *ins)
2131 SHADER_BUFFER *buffer = ins->ctx->buffer;
2132 DWORD sampler_idx = ins->dst[0].reg.idx;
2133 char src0[50];
2134 char dst_str[50];
2136 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2137 shader_addline(buffer, "MOV TB, 0.0;\n");
2138 shader_addline(buffer, "DP3 TB.x, fragment.texcoord[%u], %s;\n", sampler_idx, src0);
2140 shader_arb_get_dst_param(ins, &ins->dst[0], dst_str);
2141 shader_hw_sample(ins, sampler_idx, dst_str, "TB", 0 /* Only one coord, can't be projected */, NULL, NULL);
2144 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
2145 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2146 static void pshader_hw_texdp3(const struct wined3d_shader_instruction *ins)
2148 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2149 char src0[50];
2150 char dst_str[50];
2151 SHADER_BUFFER *buffer = ins->ctx->buffer;
2153 /* Handle output register */
2154 shader_arb_get_dst_param(ins, dst, dst_str);
2155 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2156 shader_addline(buffer, "DP3 %s, fragment.texcoord[%u], %s;\n", dst_str, dst->reg.idx, src0);
2159 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
2160 * Perform the 3rd row of a 3x3 matrix multiply */
2161 static void pshader_hw_texm3x3(const struct wined3d_shader_instruction *ins)
2163 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2164 SHADER_BUFFER *buffer = ins->ctx->buffer;
2165 char dst_str[50], dst_name[50];
2166 char src0[50];
2167 BOOL is_color;
2169 shader_arb_get_dst_param(ins, dst, dst_str);
2170 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2171 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2172 shader_addline(buffer, "DP3 %s.z, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx, src0);
2173 shader_addline(buffer, "MOV %s, %s;\n", dst_str, dst_name);
2176 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
2177 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2178 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2179 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2181 static void pshader_hw_texm3x2depth(const struct wined3d_shader_instruction *ins)
2183 SHADER_BUFFER *buffer = ins->ctx->buffer;
2184 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2185 char src0[50], dst_name[50];
2186 BOOL is_color;
2188 shader_arb_get_src_param(ins, &ins->src[0], 0, src0);
2189 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2190 shader_addline(buffer, "DP3 %s.y, fragment.texcoord[%u], %s;\n", dst_name, dst->reg.idx, src0);
2192 /* How to deal with the special case dst_name.g == 0? if r != 0, then
2193 * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
2194 * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
2196 shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
2197 shader_addline(buffer, "MUL %s.x, %s.x, %s.y;\n", dst_name, dst_name, dst_name);
2198 shader_addline(buffer, "MIN %s.x, %s.x, one.x;\n", dst_name, dst_name);
2199 shader_addline(buffer, "MAX result.depth, %s.x, 0.0;\n", dst_name);
2202 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2203 Vertex/Pixel shaders to ARB_vertex_program codes */
2204 static void shader_hw_mnxn(const struct wined3d_shader_instruction *ins)
2206 int i;
2207 int nComponents = 0;
2208 struct wined3d_shader_dst_param tmp_dst = {{0}};
2209 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2210 struct wined3d_shader_instruction tmp_ins;
2212 memset(&tmp_ins, 0, sizeof(tmp_ins));
2214 /* Set constants for the temporary argument */
2215 tmp_ins.ctx = ins->ctx;
2216 tmp_ins.dst_count = 1;
2217 tmp_ins.dst = &tmp_dst;
2218 tmp_ins.src_count = 2;
2219 tmp_ins.src = tmp_src;
2221 switch(ins->handler_idx)
2223 case WINED3DSIH_M4x4:
2224 nComponents = 4;
2225 tmp_ins.handler_idx = WINED3DSIH_DP4;
2226 break;
2227 case WINED3DSIH_M4x3:
2228 nComponents = 3;
2229 tmp_ins.handler_idx = WINED3DSIH_DP4;
2230 break;
2231 case WINED3DSIH_M3x4:
2232 nComponents = 4;
2233 tmp_ins.handler_idx = WINED3DSIH_DP3;
2234 break;
2235 case WINED3DSIH_M3x3:
2236 nComponents = 3;
2237 tmp_ins.handler_idx = WINED3DSIH_DP3;
2238 break;
2239 case WINED3DSIH_M3x2:
2240 nComponents = 2;
2241 tmp_ins.handler_idx = WINED3DSIH_DP3;
2242 break;
2243 default:
2244 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
2245 break;
2248 tmp_dst = ins->dst[0];
2249 tmp_src[0] = ins->src[0];
2250 tmp_src[1] = ins->src[1];
2251 for (i = 0; i < nComponents; i++) {
2252 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2253 shader_hw_map2gl(&tmp_ins);
2254 ++tmp_src[1].reg.idx;
2258 static void shader_hw_rsq_rcp(const struct wined3d_shader_instruction *ins)
2260 SHADER_BUFFER *buffer = ins->ctx->buffer;
2261 const char *instruction;
2263 char dst[50];
2264 char src[50];
2266 switch(ins->handler_idx)
2268 case WINED3DSIH_RSQ: instruction = "RSQ"; break;
2269 case WINED3DSIH_RCP: instruction = "RCP"; break;
2270 default: instruction = "";
2271 FIXME("Unhandled opcode %#x\n", ins->handler_idx);
2272 break;
2275 shader_arb_get_dst_param(ins, &ins->dst[0], dst); /* Destination */
2276 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2277 if (ins->src[0].swizzle == WINED3DSP_NOSWIZZLE)
2279 /* Dx sdk says .x is used if no swizzle is given, but our test shows that
2280 * .w is used
2282 strcat(src, ".w");
2285 shader_addline(buffer, "%s%s %s, %s;\n", instruction, shader_arb_get_modifier(ins), dst, src);
2288 static void shader_hw_nrm(const struct wined3d_shader_instruction *ins)
2290 SHADER_BUFFER *buffer = ins->ctx->buffer;
2291 char dst_name[50];
2292 char src_name[50];
2293 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2294 BOOL pshader = shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type);
2296 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2297 shader_arb_get_src_param(ins, &ins->src[0], 1 /* Use TB */, src_name);
2299 if(pshader && priv->target_version >= NV3)
2301 shader_addline(buffer, "NRM%s %s, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name);
2303 else
2305 shader_addline(buffer, "DP3 TA, %s, %s;\n", src_name, src_name);
2306 shader_addline(buffer, "RSQ TA, TA.x;\n");
2307 /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
2308 shader_addline(buffer, "MUL%s %s, %s, TA;\n", shader_arb_get_modifier(ins), dst_name,
2309 src_name);
2313 static void shader_hw_lrp(const struct wined3d_shader_instruction *ins)
2315 SHADER_BUFFER *buffer = ins->ctx->buffer;
2316 char dst_name[50];
2317 char src_name[3][50];
2319 /* ARB_fragment_program has a convenient LRP instruction */
2320 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2321 shader_hw_map2gl(ins);
2322 return;
2325 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2326 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name[0]);
2327 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name[1]);
2328 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name[2]);
2330 shader_addline(buffer, "SUB TA, %s, %s;\n", src_name[1], src_name[2]);
2331 shader_addline(buffer, "MAD%s %s, %s, TA, %s;\n", shader_arb_get_modifier(ins),
2332 dst_name, src_name[0], src_name[2]);
2335 static void shader_hw_sincos(const struct wined3d_shader_instruction *ins)
2337 /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
2338 * must contain fixed constants. So we need a separate function to filter those constants and
2339 * can't use map2gl
2341 SHADER_BUFFER *buffer = ins->ctx->buffer;
2342 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2343 const struct wined3d_shader_dst_param *dst = &ins->dst[0];
2344 char dst_name[50];
2345 char src_name0[50], src_name1[50], src_name2[50];
2346 BOOL is_color;
2348 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2349 if(shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)) {
2350 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2351 /* No modifiers are supported on SCS */
2352 shader_addline(buffer, "SCS %s, %s;\n", dst_name, src_name0);
2354 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE)
2356 shader_arb_get_register_name(ins, &dst->reg, src_name0, &is_color);
2357 shader_addline(buffer, "MOV_SAT %s, %s;\n", dst_name, src_name0);
2359 } else if(priv->target_version >= NV2) {
2360 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2362 /* Sincos writemask must be .x, .y or .xy */
2363 if(dst->write_mask & WINED3DSP_WRITEMASK_0)
2364 shader_addline(buffer, "COS%s %s.x, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2365 if(dst->write_mask & WINED3DSP_WRITEMASK_1)
2366 shader_addline(buffer, "SIN%s %s.y, %s;\n", shader_arb_get_modifier(ins), dst_name, src_name0);
2367 } else {
2368 /* Approximate sine and cosine with a taylor series, as per math textbook. The application passes 8
2369 * helper constants(D3DSINCOSCONST1 and D3DSINCOSCONST2) in src1 and src2.
2371 * sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
2372 * cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
2374 * The constants we get are:
2376 * +1 +1, -1 -1 +1 +1 -1 -1
2377 * ---- , ---- , ---- , ----- , ----- , ----- , ------
2378 * 1!*2 2!*4 3!*8 4!*16 5!*32 6!*64 7!*128
2380 * If used with x^2, x^3, x^4 etc they calculate sin(x/2) and cos(x/2):
2382 * (x/2)^2 = x^2 / 4
2383 * (x/2)^3 = x^3 / 8
2384 * (x/2)^4 = x^4 / 16
2385 * (x/2)^5 = x^5 / 32
2386 * etc
2388 * To get the final result:
2389 * sin(x) = 2 * sin(x/2) * cos(x/2)
2390 * cos(x) = cos(x/2)^2 - sin(x/2)^2
2391 * (from sin(x+y) and cos(x+y) rules)
2393 * As per MSDN, dst.z is undefined after the operation, and so is
2394 * dst.x and dst.y if they're masked out by the writemask. Ie
2395 * sincos dst.y, src1, c0, c1
2396 * returns the sine in dst.y. dst.x and dst.z are undefined, dst.w is not touched. The assembler
2397 * vsa.exe also stops with an error if the dest register is the same register as the source
2398 * register. This means we can use dest.xyz as temporary storage. The assembler vsa.exe output also
2399 * indicates that sincos consumes 8 instruction slots in vs_2_0(and, strangely, in vs_3_0).
2401 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2402 shader_arb_get_src_param(ins, &ins->src[2], 2, src_name2);
2403 shader_arb_get_register_name(ins, &dst->reg, dst_name, &is_color);
2405 shader_addline(buffer, "MUL %s.x, %s, %s;\n", dst_name, src_name0, src_name0); /* x ^ 2 */
2406 shader_addline(buffer, "MUL TA.y, %s.x, %s;\n", dst_name, src_name0); /* x ^ 3 */
2407 shader_addline(buffer, "MUL %s.y, TA.y, %s;\n", dst_name, src_name0); /* x ^ 4 */
2408 shader_addline(buffer, "MUL TA.z, %s.y, %s;\n", dst_name, src_name0); /* x ^ 5 */
2409 shader_addline(buffer, "MUL %s.z, TA.z, %s;\n", dst_name, src_name0); /* x ^ 6 */
2410 shader_addline(buffer, "MUL TA.w, %s.z, %s;\n", dst_name, src_name0); /* x ^ 7 */
2412 /* sin(x/2)
2414 * Unfortunately we don't get the constants in a DP4-capable form. Is there a way to
2415 * properly merge that with MULs in the code above?
2416 * The swizzles .yz and xw however fit into the .yzxw swizzle added to ps_2_0. Maybe
2417 * we can merge the sine and cosine MAD rows to calculate them together.
2419 shader_addline(buffer, "MUL TA.x, %s, %s.w;\n", src_name0, src_name2); /* x^1, +1/(1!*2) */
2420 shader_addline(buffer, "MAD TA.x, TA.y, %s.x, TA.x;\n", src_name2); /* -1/(3!*8) */
2421 shader_addline(buffer, "MAD TA.x, TA.z, %s.w, TA.x;\n", src_name1); /* +1/(5!*32) */
2422 shader_addline(buffer, "MAD TA.x, TA.w, %s.x, TA.x;\n", src_name1); /* -1/(7!*128) */
2424 /* cos(x/2) */
2425 shader_addline(buffer, "MAD TA.y, %s.x, %s.y, %s.z;\n", dst_name, src_name2, src_name2); /* -1/(2!*4), +1.0 */
2426 shader_addline(buffer, "MAD TA.y, %s.y, %s.z, TA.y;\n", dst_name, src_name1); /* +1/(4!*16) */
2427 shader_addline(buffer, "MAD TA.y, %s.z, %s.y, TA.y;\n", dst_name, src_name1); /* -1/(6!*64) */
2429 if(dst->write_mask & WINED3DSP_WRITEMASK_0) {
2430 /* cos x */
2431 shader_addline(buffer, "MUL TA.z, TA.y, TA.y;\n");
2432 shader_addline(buffer, "MAD %s.x, -TA.x, TA.x, TA.z;\n", dst_name);
2434 if(dst->write_mask & WINED3DSP_WRITEMASK_1) {
2435 /* sin x */
2436 shader_addline(buffer, "MUL %s.y, TA.x, TA.y;\n", dst_name);
2437 shader_addline(buffer, "ADD %s.y, %s.y, %s.y;\n", dst_name, dst_name, dst_name);
2442 static void shader_hw_sgn(const struct wined3d_shader_instruction *ins)
2444 SHADER_BUFFER *buffer = ins->ctx->buffer;
2445 char dst_name[50];
2446 char src_name[50];
2447 struct shader_arb_ctx_priv *ctx = ins->ctx->backend_data;
2449 /* SGN is only valid in vertex shaders */
2450 if(ctx->target_version == NV2) {
2451 shader_hw_map2gl(ins);
2452 return;
2454 shader_arb_get_dst_param(ins, &ins->dst[0], dst_name);
2455 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2457 /* If SRC > 0.0, -SRC < SRC = TRUE, otherwise false.
2458 * if SRC < 0.0, SRC < -SRC = TRUE. If neither is true, src = 0.0
2460 if(ins->dst[0].modifiers & WINED3DSPDM_SATURATE) {
2461 shader_addline(buffer, "SLT %s, -%s, %s;\n", dst_name, src_name, src_name);
2462 } else {
2463 /* src contains TA? Write to the dest first. This won't overwrite our destination.
2464 * Then use TA, and calculate the final result
2466 * Not reading from TA? Store the first result in TA to avoid overwriting the
2467 * destination if src reg = dst reg
2469 if(strstr(src_name, "TA"))
2471 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2472 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2473 shader_addline(buffer, "ADD %s, %s, -TA;\n", dst_name, dst_name);
2475 else
2477 shader_addline(buffer, "SLT TA, -%s, %s;\n", src_name, src_name);
2478 shader_addline(buffer, "SLT %s, %s, -%s;\n", dst_name, src_name, src_name);
2479 shader_addline(buffer, "ADD %s, TA, -%s;\n", dst_name, dst_name);
2484 static void shader_hw_dsy(const struct wined3d_shader_instruction *ins)
2486 SHADER_BUFFER *buffer = ins->ctx->buffer;
2487 char src[50];
2488 char dst[50];
2489 char dst_name[50];
2490 BOOL is_color;
2492 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2493 shader_arb_get_src_param(ins, &ins->src[0], 0, src);
2494 shader_arb_get_register_name(ins, &ins->dst[0].reg, dst_name, &is_color);
2496 shader_addline(buffer, "DDY %s, %s;\n", dst, src);
2497 shader_addline(buffer, "MUL%s %s, %s, ycorrection.y;\n", shader_arb_get_modifier(ins), dst, dst_name);
2500 static DWORD abs_modifier(DWORD mod, BOOL *need_abs)
2502 *need_abs = FALSE;
2504 switch(mod)
2506 case WINED3DSPSM_NONE: return WINED3DSPSM_ABS;
2507 case WINED3DSPSM_NEG: return WINED3DSPSM_ABS;
2508 case WINED3DSPSM_BIAS: *need_abs = TRUE; return WINED3DSPSM_BIAS;
2509 case WINED3DSPSM_BIASNEG: *need_abs = TRUE; return WINED3DSPSM_BIASNEG;
2510 case WINED3DSPSM_SIGN: *need_abs = TRUE; return WINED3DSPSM_SIGN;
2511 case WINED3DSPSM_SIGNNEG: *need_abs = TRUE; return WINED3DSPSM_SIGNNEG;
2512 case WINED3DSPSM_COMP: *need_abs = TRUE; return WINED3DSPSM_COMP;
2513 case WINED3DSPSM_X2: *need_abs = TRUE; return WINED3DSPSM_X2;
2514 case WINED3DSPSM_X2NEG: *need_abs = TRUE; return WINED3DSPSM_X2NEG;
2515 case WINED3DSPSM_DZ: *need_abs = TRUE; return WINED3DSPSM_DZ;
2516 case WINED3DSPSM_DW: *need_abs = TRUE; return WINED3DSPSM_DW;
2517 case WINED3DSPSM_ABS: return WINED3DSPSM_ABS;
2518 case WINED3DSPSM_ABSNEG: return WINED3DSPSM_ABS;
2520 FIXME("Unknown modifier %u\n", mod);
2521 return mod;
2524 static void shader_hw_log_pow(const struct wined3d_shader_instruction *ins)
2526 SHADER_BUFFER *buffer = ins->ctx->buffer;
2527 char src0[50], src1[50], dst[50];
2528 struct wined3d_shader_src_param src0_copy = ins->src[0];
2529 BOOL need_abs = FALSE;
2530 const char *instr;
2531 BOOL arg2 = FALSE;
2533 switch(ins->handler_idx)
2535 case WINED3DSIH_LOG: instr = "LG2"; break;
2536 case WINED3DSIH_LOGP: instr = "LOG"; break;
2537 case WINED3DSIH_POW: instr = "POW"; arg2 = TRUE; break;
2538 default:
2539 ERR("Unexpected instruction %d\n", ins->handler_idx);
2540 return;
2543 /* LOG, LOGP and POW operate on the absolute value of the input */
2544 src0_copy.modifiers = abs_modifier(src0_copy.modifiers, &need_abs);
2546 shader_arb_get_dst_param(ins, &ins->dst[0], dst);
2547 shader_arb_get_src_param(ins, &src0_copy, 0, src0);
2548 if(arg2) shader_arb_get_src_param(ins, &ins->src[1], 1, src1);
2550 if(need_abs)
2552 shader_addline(buffer, "ABS TA, %s;\n", src0);
2553 if(arg2)
2555 shader_addline(buffer, "%s%s %s, TA, %s;\n", instr, shader_arb_get_modifier(ins), dst, src1);
2557 else
2559 shader_addline(buffer, "%s%s %s, TA;\n", instr, shader_arb_get_modifier(ins), dst);
2562 else if(arg2)
2564 shader_addline(buffer, "%s%s %s, %s, %s;\n", instr, shader_arb_get_modifier(ins), dst, src0, src1);
2566 else
2568 shader_addline(buffer, "%s%s %s, %s;\n", instr, shader_arb_get_modifier(ins), dst, src0);
2572 static void shader_hw_loop(const struct wined3d_shader_instruction *ins)
2574 SHADER_BUFFER *buffer = ins->ctx->buffer;
2575 char src_name[50];
2576 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2578 /* src0 is aL */
2579 shader_arb_get_src_param(ins, &ins->src[1], 0, src_name);
2581 if(vshader)
2583 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2584 struct list *e = list_head(&priv->control_frames);
2585 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2587 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2588 /* The constant loader makes sure to load -1 into iX.w */
2589 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2590 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->loop_no);
2591 shader_addline(buffer, "loop_%u_start:\n", control_frame->loop_no);
2593 else
2595 shader_addline(buffer, "LOOP %s;\n", src_name);
2599 static void shader_hw_rep(const struct wined3d_shader_instruction *ins)
2601 SHADER_BUFFER *buffer = ins->ctx->buffer;
2602 char src_name[50];
2603 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2605 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name);
2607 /* The constant loader makes sure to load -1 into iX.w */
2608 if(vshader)
2610 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2611 struct list *e = list_head(&priv->control_frames);
2612 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2614 if(priv->loop_depth > 1) shader_addline(buffer, "PUSHA aL;\n");
2616 shader_addline(buffer, "ARLC aL, %s.xywz;\n", src_name);
2617 shader_addline(buffer, "BRA loop_%u_end (LE.x);\n", control_frame->loop_no);
2618 shader_addline(buffer, "loop_%u_start:\n", control_frame->loop_no);
2620 else
2622 shader_addline(buffer, "REP %s;\n", src_name);
2626 static void shader_hw_endloop(const struct wined3d_shader_instruction *ins)
2628 SHADER_BUFFER *buffer = ins->ctx->buffer;
2629 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2631 if(vshader)
2633 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2634 struct list *e = list_head(&priv->control_frames);
2635 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2637 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2638 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->loop_no);
2639 shader_addline(buffer, "loop_%u_end:\n", control_frame->loop_no);
2641 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2643 else
2645 shader_addline(buffer, "ENDLOOP;\n");
2649 static void shader_hw_endrep(const struct wined3d_shader_instruction *ins)
2651 SHADER_BUFFER *buffer = ins->ctx->buffer;
2652 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2654 if(vshader)
2656 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2657 struct list *e = list_head(&priv->control_frames);
2658 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2660 shader_addline(buffer, "ARAC aL.xy, aL;\n");
2661 shader_addline(buffer, "BRA loop_%u_start (GT.x);\n", control_frame->loop_no);
2662 shader_addline(buffer, "loop_%u_end:\n", control_frame->loop_no);
2664 if(priv->loop_depth > 1) shader_addline(buffer, "POPA aL;\n");
2666 else
2668 shader_addline(buffer, "ENDREP;\n");
2672 static const struct control_frame *find_last_loop(const struct shader_arb_ctx_priv *priv)
2674 struct control_frame *control_frame;
2676 LIST_FOR_EACH_ENTRY(control_frame, &priv->control_frames, struct control_frame, entry)
2678 if(control_frame->type == LOOP || control_frame->type == REP) return control_frame;
2680 ERR("Could not find loop for break\n");
2681 return NULL;
2684 static void shader_hw_break(const struct wined3d_shader_instruction *ins)
2686 SHADER_BUFFER *buffer = ins->ctx->buffer;
2687 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2688 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2690 if(vshader)
2692 shader_addline(buffer, "BRA loop_%u_end;\n", control_frame->loop_no);
2694 else
2696 shader_addline(buffer, "BRK;\n");
2700 static const char *get_compare(COMPARISON_TYPE flags)
2702 switch (flags)
2704 case COMPARISON_GT: return "GT";
2705 case COMPARISON_EQ: return "EQ";
2706 case COMPARISON_GE: return "GE";
2707 case COMPARISON_LT: return "LT";
2708 case COMPARISON_NE: return "NE";
2709 case COMPARISON_LE: return "LE";
2710 default:
2711 FIXME("Unrecognized comparison value: %u\n", flags);
2712 return "(\?\?)";
2716 static COMPARISON_TYPE invert_compare(COMPARISON_TYPE flags)
2718 switch (flags)
2720 case COMPARISON_GT: return COMPARISON_LE;
2721 case COMPARISON_EQ: return COMPARISON_NE;
2722 case COMPARISON_GE: return COMPARISON_LT;
2723 case COMPARISON_LT: return COMPARISON_GE;
2724 case COMPARISON_NE: return COMPARISON_EQ;
2725 case COMPARISON_LE: return COMPARISON_GT;
2726 default:
2727 FIXME("Unrecognized comparison value: %u\n", flags);
2728 return -1;
2732 static void shader_hw_breakc(const struct wined3d_shader_instruction *ins)
2734 SHADER_BUFFER *buffer = ins->ctx->buffer;
2735 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2736 const struct control_frame *control_frame = find_last_loop(ins->ctx->backend_data);
2737 char src_name0[50];
2738 char src_name1[50];
2739 const char *comp = get_compare(ins->flags);
2741 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2742 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2744 if(vshader)
2746 /* SUBC CC, src0, src1" works only in pixel shaders, so use TA to throw
2747 * away the subtraction result
2749 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2750 shader_addline(buffer, "BRA loop_%u_end (%s.x);\n", control_frame->loop_no, comp);
2752 else
2754 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2755 shader_addline(buffer, "BRK (%s.x);\n", comp);
2759 static void shader_hw_ifc(const struct wined3d_shader_instruction *ins)
2761 SHADER_BUFFER *buffer = ins->ctx->buffer;
2762 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2763 struct list *e = list_head(&priv->control_frames);
2764 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2765 const char *comp;
2766 char src_name0[50];
2767 char src_name1[50];
2768 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2770 shader_arb_get_src_param(ins, &ins->src[0], 0, src_name0);
2771 shader_arb_get_src_param(ins, &ins->src[1], 1, src_name1);
2773 if(vshader)
2775 /* Invert the flag. We jump to the else label if the condition is NOT true */
2776 comp = get_compare(invert_compare(ins->flags));
2777 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2778 shader_addline(buffer, "BRA ifc_%u_else (%s.x);\n", control_frame->ifc_no, comp);
2780 else
2782 comp = get_compare(ins->flags);
2783 shader_addline(buffer, "SUBC TA, %s, %s;\n", src_name0, src_name1);
2784 shader_addline(buffer, "IF %s.x;\n", comp);
2788 static void shader_hw_else(const struct wined3d_shader_instruction *ins)
2790 SHADER_BUFFER *buffer = ins->ctx->buffer;
2791 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2792 struct list *e = list_head(&priv->control_frames);
2793 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2794 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2796 if(vshader)
2798 shader_addline(buffer, "BRA ifc_%u_endif;\n", control_frame->ifc_no);
2799 shader_addline(buffer, "ifc_%u_else:\n", control_frame->ifc_no);
2800 control_frame->had_else = TRUE;
2802 else
2804 shader_addline(buffer, "ELSE;\n");
2808 static void shader_hw_endif(const struct wined3d_shader_instruction *ins)
2810 SHADER_BUFFER *buffer = ins->ctx->buffer;
2811 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2812 struct list *e = list_head(&priv->control_frames);
2813 struct control_frame *control_frame = LIST_ENTRY(e, struct control_frame, entry);
2814 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2816 if(vshader)
2818 if(control_frame->had_else)
2820 shader_addline(buffer, "ifc_%u_endif:\n", control_frame->ifc_no);
2822 else
2824 shader_addline(buffer, "#No else branch. else is endif\n");
2825 shader_addline(buffer, "ifc_%u_else:\n", control_frame->ifc_no);
2828 else
2830 shader_addline(buffer, "ENDIF;\n");
2834 static void shader_hw_texldd(const struct wined3d_shader_instruction *ins)
2836 DWORD sampler_idx = ins->src[1].reg.idx;
2837 char reg_dest[40];
2838 char reg_src[3][40];
2839 DWORD flags = TEX_DERIV;
2841 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
2842 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_src[0]);
2843 shader_arb_get_src_param(ins, &ins->src[2], 1, reg_src[1]);
2844 shader_arb_get_src_param(ins, &ins->src[3], 2, reg_src[2]);
2846 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
2847 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
2849 shader_hw_sample(ins, sampler_idx, reg_dest, reg_src[0], flags, reg_src[1], reg_src[2]);
2852 static void shader_hw_texldl(const struct wined3d_shader_instruction *ins)
2854 DWORD sampler_idx = ins->src[1].reg.idx;
2855 char reg_dest[40];
2856 char reg_coord[40];
2857 DWORD flags = TEX_LOD;
2859 shader_arb_get_dst_param(ins, &ins->dst[0], reg_dest);
2860 shader_arb_get_src_param(ins, &ins->src[0], 0, reg_coord);
2862 if (ins->flags & WINED3DSI_TEXLD_PROJECT) flags |= TEX_PROJ;
2863 if (ins->flags & WINED3DSI_TEXLD_BIAS) flags |= TEX_BIAS;
2865 shader_hw_sample(ins, sampler_idx, reg_dest, reg_coord, flags, NULL, NULL);
2868 static void shader_hw_label(const struct wined3d_shader_instruction *ins)
2870 SHADER_BUFFER *buffer = ins->ctx->buffer;
2871 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2873 priv->in_main_func = FALSE;
2874 /* Call instructions activate the NV extensions, not labels and rets. If there is an uncalled
2875 * subroutine, don't generate a label that will make GL complain
2877 if(priv->target_version == ARB) return;
2879 shader_addline(buffer, "l%u:\n", ins->src[0].reg.idx);
2882 static void vshader_add_footer(IWineD3DVertexShaderImpl *This, SHADER_BUFFER *buffer, const struct arb_vs_compile_args *args,
2883 struct shader_arb_ctx_priv *priv_ctx)
2885 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
2886 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
2887 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
2888 unsigned int i;
2890 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
2891 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
2892 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
2893 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
2895 if(args->super.fog_src == VS_FOG_Z) {
2896 shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2897 } else if (!reg_maps->fog) {
2898 /* posFixup.x is always 1.0, so we can savely use it */
2899 shader_addline(buffer, "ADD result.fogcoord, posFixup.x, -posFixup.x;\n");
2902 /* Write the final position.
2904 * OpenGL coordinates specify the center of the pixel while d3d coords specify
2905 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2906 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2907 * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2909 shader_addline(buffer, "MUL TA, posFixup, TMP_OUT.w;\n");
2910 shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TA.z;\n");
2911 shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TA.w;\n");
2913 if(use_nv_clip(gl_info) && priv_ctx->target_version >= NV2)
2915 for(i = 0; i < priv_ctx->vs_clipplanes; i++)
2917 shader_addline(buffer, "DP4 result.clip[%u].x, TMP_OUT, state.clip[%u].plane;\n", i, i);
2920 else if(args->boolclip.clip_control[0])
2922 unsigned int cur_clip = 0;
2923 char component[4] = {'x', 'y', 'z', 'w'};
2925 for(i = 0; i < GL_LIMITS(clipplanes); i++)
2927 if(args->boolclip.clip_control[1] & (1 << i))
2929 shader_addline(buffer, "DP4 TA.%c, TMP_OUT, state.clip[%u].plane;\n",
2930 component[cur_clip++], i);
2933 switch(cur_clip)
2935 case 0:
2936 shader_addline(buffer, "MOV TA, -helper_const.w;\n");
2937 break;
2938 case 1:
2939 shader_addline(buffer, "MOV TA.yzw, -helper_const.w;\n");
2940 break;
2941 case 2:
2942 shader_addline(buffer, "MOV TA.zw, -helper_const.w;\n");
2943 break;
2944 case 3:
2945 shader_addline(buffer, "MOV TA.w, -helper_const.w;\n");
2946 break;
2948 shader_addline(buffer, "MOV result.texcoord[%u], TA;\n",
2949 args->boolclip.clip_control[0] - 1);
2952 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2953 * and the glsl equivalent
2955 if(need_helper_const(gl_info)) {
2956 shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2957 } else {
2958 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, TMP_OUT.z;\n");
2959 shader_addline(buffer, "ADD TMP_OUT.z, TMP_OUT.z, -TMP_OUT.w;\n");
2962 shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2964 priv_ctx->footer_written = TRUE;
2967 static void shader_hw_ret(const struct wined3d_shader_instruction *ins)
2969 SHADER_BUFFER *buffer = ins->ctx->buffer;
2970 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
2971 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *) ins->ctx->shader;
2972 BOOL vshader = shader_is_vshader_version(ins->ctx->reg_maps->shader_version.type);
2974 if(priv->target_version == ARB) return;
2976 if(vshader)
2978 if(priv->in_main_func) vshader_add_footer((IWineD3DVertexShaderImpl *) shader, buffer, priv->cur_vs_args, priv);
2981 shader_addline(buffer, "RET;\n");
2984 static void shader_hw_call(const struct wined3d_shader_instruction *ins)
2986 SHADER_BUFFER *buffer = ins->ctx->buffer;
2987 shader_addline(buffer, "CAL l%u;\n", ins->src[0].reg.idx);
2990 /* GL locking is done by the caller */
2991 static GLuint create_arb_blt_vertex_program(const WineD3D_GL_Info *gl_info)
2993 GLuint program_id = 0;
2994 const char *blt_vprogram =
2995 "!!ARBvp1.0\n"
2996 "PARAM c[1] = { { 1, 0.5 } };\n"
2997 "MOV result.position, vertex.position;\n"
2998 "MOV result.color, c[0].x;\n"
2999 "MOV result.texcoord[0], vertex.texcoord[0];\n"
3000 "END\n";
3002 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3003 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
3004 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
3006 if (glGetError() == GL_INVALID_OPERATION) {
3007 GLint pos;
3008 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3009 FIXME("Vertex program error at position %d: %s\n", pos,
3010 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3013 return program_id;
3016 /* GL locking is done by the caller */
3017 static GLuint create_arb_blt_fragment_program(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3019 GLuint program_id = 0;
3020 static const char * const blt_fprograms[tex_type_count] =
3022 /* tex_1d */
3023 NULL,
3024 /* tex_2d */
3025 "!!ARBfp1.0\n"
3026 "TEMP R0;\n"
3027 "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
3028 "MOV result.depth.z, R0.x;\n"
3029 "END\n",
3030 /* tex_3d */
3031 NULL,
3032 /* tex_cube */
3033 "!!ARBfp1.0\n"
3034 "TEMP R0;\n"
3035 "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
3036 "MOV result.depth.z, R0.x;\n"
3037 "END\n",
3038 /* tex_rect */
3039 "!!ARBfp1.0\n"
3040 "TEMP R0;\n"
3041 "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
3042 "MOV result.depth.z, R0.x;\n"
3043 "END\n",
3046 if (!blt_fprograms[tex_type])
3048 FIXME("tex_type %#x not supported\n", tex_type);
3049 tex_type = tex_2d;
3052 GL_EXTCALL(glGenProgramsARB(1, &program_id));
3053 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
3054 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprograms[tex_type]), blt_fprograms[tex_type]));
3056 if (glGetError() == GL_INVALID_OPERATION) {
3057 GLint pos;
3058 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3059 FIXME("Fragment program error at position %d: %s\n", pos,
3060 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3063 return program_id;
3066 static void arbfp_add_sRGB_correction(SHADER_BUFFER *buffer, const char *fragcolor, const char *tmp1,
3067 const char *tmp2, const char *tmp3, const char *tmp4, BOOL condcode) {
3068 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
3070 if(condcode)
3072 /* Sigh. MOVC CC doesn't work, so use one of the temps as dummy dest */
3073 shader_addline(buffer, "SUBC %s, %s.x, srgb_consts1.y;\n", tmp1, fragcolor);
3074 /* Calculate the > 0.0031308 case */
3075 shader_addline(buffer, "POW %s.x (GE), %s.x, srgb_consts1.z;\n", fragcolor, fragcolor);
3076 shader_addline(buffer, "POW %s.y (GE), %s.y, srgb_consts1.z;\n", fragcolor, fragcolor);
3077 shader_addline(buffer, "POW %s.z (GE), %s.z, srgb_consts1.z;\n", fragcolor, fragcolor);
3078 shader_addline(buffer, "MUL %s.xyz (GE), %s, srgb_consts1.w;\n", fragcolor, fragcolor);
3079 shader_addline(buffer, "SUB %s.xyz (GE), %s, srgb_consts2.x;\n", fragcolor, fragcolor);
3080 /* Calculate the < case */
3081 shader_addline(buffer, "MUL %s.xyz (LT), srgb_consts1.x, %s;\n", fragcolor, fragcolor);
3083 else
3085 /* Calculate the > 0.0031308 case */
3086 shader_addline(buffer, "POW %s.x, %s.x, srgb_consts1.z;\n", tmp1, fragcolor);
3087 shader_addline(buffer, "POW %s.y, %s.y, srgb_consts1.z;\n", tmp1, fragcolor);
3088 shader_addline(buffer, "POW %s.z, %s.z, srgb_consts1.z;\n", tmp1, fragcolor);
3089 shader_addline(buffer, "MUL %s, %s, srgb_consts1.w;\n", tmp1, tmp1);
3090 shader_addline(buffer, "SUB %s, %s, srgb_consts2.x;\n", tmp1, tmp1);
3091 /* Calculate the < case */
3092 shader_addline(buffer, "MUL %s, srgb_consts1.x, %s;\n", tmp2, fragcolor);
3093 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
3094 shader_addline(buffer, "SLT %s, srgb_consts1.y, %s;\n", tmp3, fragcolor);
3095 shader_addline(buffer, "SGE %s, srgb_consts1.y, %s;\n", tmp4, fragcolor);
3096 /* Store the components > 0.0031308 in the destination */
3097 shader_addline(buffer, "MUL %s.xyz, %s, %s;\n", fragcolor, tmp1, tmp3);
3098 /* Add the components that are < 0.0031308 */
3099 shader_addline(buffer, "MAD %s.xyz, %s, %s, %s;\n", fragcolor, tmp2, tmp4, fragcolor);
3100 /* Move everything into result.color at once. Nvidia hardware cannot handle partial
3101 * result.color writes(.rgb first, then .a), or handle overwriting already written
3102 * components. The assembler uses a temporary register in this case, which is usually
3103 * not allocated from one of our registers that were used earlier.
3106 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
3107 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
3110 static const DWORD *find_loop_control_values(IWineD3DBaseShaderImpl *This, DWORD idx)
3112 const local_constant *constant;
3114 LIST_FOR_EACH_ENTRY(constant, &This->baseShader.constantsI, local_constant, entry)
3116 if (constant->idx == idx)
3118 return constant->value;
3121 return NULL;
3124 static void init_ps_input(const IWineD3DPixelShaderImpl *This, const struct arb_ps_compile_args *args,
3125 struct shader_arb_ctx_priv *priv)
3127 const char *texcoords[8] =
3129 "fragment.texcoord[0]", "fragment.texcoord[1]", "fragment.texcoord[2]", "fragment.texcoord[3]",
3130 "fragment.texcoord[4]", "fragment.texcoord[5]", "fragment.texcoord[6]", "fragment.texcoord[7]"
3132 unsigned int i;
3133 const struct wined3d_shader_signature_element *sig = This->input_signature;
3134 const char *semantic_name;
3135 DWORD semantic_idx;
3137 switch(args->super.vp_mode)
3139 case pretransformed:
3140 case fixedfunction:
3141 /* The pixelshader has to collect the varyings on its own. In any case properly load
3142 * color0 and color1. In the case of pretransformed vertices also load texcoords. Set
3143 * other attribs to 0.0.
3145 * For fixedfunction this behavior is correct, according to the tests. For pretransformed
3146 * we'd either need a replacement shader that can load other attribs like BINORMAL, or
3147 * load the texcoord attrib pointers to match the pixel shader signature
3149 for(i = 0; i < MAX_REG_INPUT; i++)
3151 semantic_name = sig[i].semantic_name;
3152 semantic_idx = sig[i].semantic_idx;
3153 if(semantic_name == NULL) continue;
3155 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3157 if(semantic_idx == 0) priv->ps_input[i] = "fragment.color.primary";
3158 else if(semantic_idx == 1) priv->ps_input[i] = "fragment.color.secondary";
3159 else priv->ps_input[i] = "0.0";
3161 else if(args->super.vp_mode == fixedfunction)
3163 priv->ps_input[i] = "0.0";
3165 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3167 if(semantic_idx < 8) priv->ps_input[i] = texcoords[semantic_idx];
3168 else priv->ps_input[i] = "0.0";
3170 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3172 if(semantic_idx == 0) priv->ps_input[i] = "fragment.fogcoord";
3173 else priv->ps_input[i] = "0.0";
3175 else
3177 priv->ps_input[i] = "0.0";
3180 TRACE("v%u, semantic %s%u is %s\n", i, semantic_name, semantic_idx, priv->ps_input[i]);
3182 break;
3184 case vertexshader:
3185 /* That one is easy. The vertex shaders provide v0-v7 in fragment.texcoord and v8 and v9 in
3186 * fragment.color
3188 for(i = 0; i < 8; i++)
3190 priv->ps_input[i] = texcoords[i];
3192 priv->ps_input[8] = "fragment.color.primary";
3193 priv->ps_input[9] = "fragment.color.secondary";
3194 break;
3198 /* GL locking is done by the caller */
3199 static GLuint shader_arb_generate_pshader(IWineD3DPixelShaderImpl *This,
3200 SHADER_BUFFER *buffer, const struct arb_ps_compile_args *args, struct arb_ps_compiled_shader *compiled)
3202 const shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3203 CONST DWORD *function = This->baseShader.function;
3204 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3205 const local_constant *lconst;
3206 GLuint retval;
3207 char fragcolor[16];
3208 DWORD *lconst_map = local_const_mapping((IWineD3DBaseShaderImpl *) This), next_local, cur;
3209 struct shader_arb_ctx_priv priv_ctx;
3210 BOOL dcl_tmp = args->super.srgb_correction, dcl_td = FALSE;
3211 BOOL want_nv_prog = FALSE;
3212 struct arb_pshader_private *shader_priv = This->backend_priv;
3214 char srgbtmp[4][4];
3215 unsigned int i, found = 0;
3217 for(i = 0; i < This->baseShader.limits.temporary; i++) {
3219 /* Don't overwrite the color source */
3220 if(This->color0_mov && i == This->color0_reg) continue;
3221 else if(reg_maps->shader_version.major < 2 && i == 0) continue;
3223 if(reg_maps->temporary[i]) {
3224 sprintf(srgbtmp[found], "R%u", i);
3225 found++;
3226 if(found == 4) break;
3230 switch(found) {
3231 case 4: dcl_tmp = FALSE; break;
3232 case 0:
3233 sprintf(srgbtmp[0], "TA");
3234 sprintf(srgbtmp[1], "TB");
3235 sprintf(srgbtmp[2], "TC");
3236 sprintf(srgbtmp[3], "TD");
3237 dcl_td = TRUE;
3238 break;
3239 case 1:
3240 sprintf(srgbtmp[1], "TA");
3241 sprintf(srgbtmp[2], "TB");
3242 sprintf(srgbtmp[3], "TC");
3243 break;
3244 case 2:
3245 sprintf(srgbtmp[2], "TA");
3246 sprintf(srgbtmp[3], "TB");
3247 break;
3248 case 3:
3249 sprintf(srgbtmp[3], "TA");
3250 break;
3253 /* Create the hw ARB shader */
3254 memset(&priv_ctx, 0, sizeof(priv_ctx));
3255 priv_ctx.cur_ps_args = args;
3256 priv_ctx.compiled_fprog = compiled;
3257 priv_ctx.cur_np2fixup_info = &compiled->np2fixup_info;
3258 init_ps_input(This, args, &priv_ctx);
3259 list_init(&priv_ctx.control_frames);
3261 /* Avoid enabling NV_fragment_program* if we do not need it.
3263 * Enabling GL_NV_fragment_program_option causes the driver to occupy a temporary register,
3264 * and it slows down the shader execution noticeably(about 5%). Usually our instruction emulation
3265 * is faster than what we gain from using higher native instructions. There are some things though
3266 * that cannot be emulated. In that case enable the extensions.
3267 * If the extension is enabled, instruction handlers that support both ways will use it.
3269 * Testing shows no performance difference between OPTION NV_fragment_program2 and NV_fragment_program.
3270 * So enable the best we can get.
3272 if(reg_maps->usesdsx || reg_maps->usesdsy || reg_maps->loop_depth > 0 || reg_maps->usestexldd ||
3273 reg_maps->usestexldl || reg_maps->usesfacing || reg_maps->usesifc || reg_maps->usescall)
3275 want_nv_prog = TRUE;
3278 shader_addline(buffer, "!!ARBfp1.0\n");
3279 if(want_nv_prog && GL_SUPPORT(NV_FRAGMENT_PROGRAM2)) {
3280 shader_addline(buffer, "OPTION NV_fragment_program2;\n");
3281 priv_ctx.target_version = NV3;
3282 } else if(want_nv_prog && GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION)) {
3283 shader_addline(buffer, "OPTION NV_fragment_program;\n");
3284 priv_ctx.target_version = NV2;
3285 } else {
3286 if(want_nv_prog)
3288 /* This is an error - either we're advertising the wrong shader version, or aren't enforcing some
3289 * limits properly
3291 ERR("The shader requires instructions that are not available in plain GL_ARB_fragment_program\n");
3292 ERR("Try GLSL\n");
3294 priv_ctx.target_version = ARB;
3297 if(This->baseShader.reg_maps.highest_render_target > 0)
3299 shader_addline(buffer, "OPTION ARB_draw_buffers;\n");
3302 if (reg_maps->shader_version.major < 3)
3304 switch(args->super.fog) {
3305 case FOG_OFF:
3306 break;
3307 case FOG_LINEAR:
3308 shader_addline(buffer, "OPTION ARB_fog_linear;\n");
3309 break;
3310 case FOG_EXP:
3311 shader_addline(buffer, "OPTION ARB_fog_exp;\n");
3312 break;
3313 case FOG_EXP2:
3314 shader_addline(buffer, "OPTION ARB_fog_exp2;\n");
3315 break;
3319 /* For now always declare the temps. At least the Nvidia assembler optimizes completely
3320 * unused temps away(but occupies them for the whole shader if they're used once). Always
3321 * declaring them avoids tricky bookkeeping work
3323 shader_addline(buffer, "TEMP TA;\n"); /* Used for modifiers */
3324 shader_addline(buffer, "TEMP TB;\n"); /* Used for modifiers */
3325 shader_addline(buffer, "TEMP TC;\n"); /* Used for modifiers */
3326 if(dcl_td) shader_addline(buffer, "TEMP TD;\n"); /* Used for sRGB writing */
3327 shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
3328 shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
3329 shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
3331 if (reg_maps->shader_version.major < 2)
3333 strcpy(fragcolor, "R0");
3334 } else {
3335 if(args->super.srgb_correction) {
3336 if(This->color0_mov) {
3337 sprintf(fragcolor, "R%u", This->color0_reg);
3338 } else {
3339 shader_addline(buffer, "TEMP TMP_COLOR;\n");
3340 strcpy(fragcolor, "TMP_COLOR");
3342 } else {
3343 strcpy(fragcolor, "result.color");
3347 if(args->super.srgb_correction) {
3348 shader_addline(buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
3349 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
3350 shader_addline(buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
3351 srgb_sub_high, 0.0, 0.0, 0.0);
3354 /* Base Declarations */
3355 next_local = shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION,
3356 lconst_map, NULL, &priv_ctx);
3358 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
3359 if(!reg_maps->bumpmat[i]) continue;
3361 cur = compiled->numbumpenvmatconsts;
3362 compiled->bumpenvmatconst[cur].const_num = WINED3D_CONST_NUM_UNUSED;
3363 compiled->bumpenvmatconst[cur].texunit = i;
3364 compiled->luminanceconst[cur].const_num = WINED3D_CONST_NUM_UNUSED;
3365 compiled->luminanceconst[cur].texunit = i;
3367 /* We can fit the constants into the constant limit for sure because texbem, texbeml, bem and beml are only supported
3368 * in 1.x shaders, and GL_ARB_fragment_program has a constant limit of 24 constants. So in the worst case we're loading
3369 * 8 shader constants, 8 bump matrices and 8 luminance parameters and are perfectly fine. (No NP2 fixup on bumpmapped
3370 * textures due to conditional NP2 restrictions)
3372 * Use local constants to load the bump env parameters, not program.env. This avoids collisions with d3d constants of
3373 * shaders in newer shader models. Since the bump env parameters have to share their space with NP2 fixup constants,
3374 * their location is shader dependent anyway and they cannot be loaded globally.
3376 compiled->bumpenvmatconst[cur].const_num = next_local++;
3377 shader_addline(buffer, "PARAM bumpenvmat%d = program.local[%d];\n",
3378 i, compiled->bumpenvmatconst[cur].const_num);
3379 compiled->numbumpenvmatconsts = cur + 1;
3381 if(!reg_maps->luminanceparams[i]) continue;
3383 compiled->luminanceconst[cur].const_num = next_local++;
3384 shader_addline(buffer, "PARAM luminance%d = program.local[%d];\n",
3385 i, compiled->luminanceconst[cur].const_num);
3388 for(i = 0; i < MAX_CONST_I; i++)
3390 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
3391 if (reg_maps->integer_constants & (1 << i) && priv_ctx.target_version >= NV2)
3393 const DWORD *control_values = find_loop_control_values((IWineD3DBaseShaderImpl *) This, i);
3395 if(control_values)
3397 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
3398 control_values[0], control_values[1], control_values[2]);
3400 else
3402 compiled->int_consts[i] = next_local;
3403 compiled->num_int_consts++;
3404 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
3409 if(reg_maps->vpos || reg_maps->usesdsy)
3411 compiled->ycorrection = next_local;
3412 shader_addline(buffer, "PARAM ycorrection = program.local[%u];\n", next_local++);
3414 if(reg_maps->vpos)
3416 shader_addline(buffer, "TEMP vpos;\n");
3417 /* ycorrection.x: Backbuffer height(onscreen) or 0(offscreen).
3418 * ycorrection.y: -1.0(onscreen), 1.0(offscreen)
3419 * ycorrection.z: 1.0
3420 * ycorrection.w: 0.0
3422 shader_addline(buffer, "MAD vpos, fragment.position, ycorrection.zyww, ycorrection.wxww;\n");
3423 shader_addline(buffer, "FLR vpos.xy, vpos;\n");
3426 else
3428 compiled->ycorrection = WINED3D_CONST_NUM_UNUSED;
3431 /* Load constants to fixup NP2 texcoords if there are still free constants left:
3432 * Constants (texture dimensions) for the NP2 fixup are loaded as local program parameters. This will consume
3433 * at most 8 (MAX_FRAGMENT_SAMPLERS / 2) parameters, which is highly unlikely, since the application had to
3434 * use 16 NP2 textures at the same time. In case that we run out of constants the fixup is simply not
3435 * applied / activated. This will probably result in wrong rendering of the texture, but will save us from
3436 * shader compilation errors and the subsequent errors when drawing with this shader. */
3437 if (priv_ctx.cur_ps_args->super.np2_fixup) {
3439 struct arb_ps_np2fixup_info* const fixup = priv_ctx.cur_np2fixup_info;
3440 const WORD map = priv_ctx.cur_ps_args->super.np2_fixup;
3441 const UINT max_lconsts = gl_info->ps_arb_max_local_constants;
3443 fixup->offset = next_local;
3444 fixup->super.active = 0;
3446 cur = 0;
3447 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
3448 if (!(map & (1 << i))) continue;
3450 if (fixup->offset + (cur >> 1) < max_lconsts) {
3451 fixup->super.active |= (1 << i);
3452 fixup->super.idx[i] = cur++;
3453 } else {
3454 FIXME("No free constant found to load NP2 fixup data into shader. "
3455 "Sampling from this texture will probably look wrong.\n");
3456 break;
3460 fixup->super.num_consts = (cur + 1) >> 1;
3461 if (fixup->super.num_consts) {
3462 shader_addline(buffer, "PARAM np2fixup[%u] = { program.env[%u..%u] };\n",
3463 fixup->super.num_consts, fixup->offset, fixup->super.num_consts + fixup->offset - 1);
3466 next_local += fixup->super.num_consts;
3469 if(shader_priv->clipplane_emulation)
3471 shader_addline(buffer, "KIL fragment.texcoord[%u];\n", shader_priv->clipplane_emulation - 1);
3474 /* Base Shader Body */
3475 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
3477 if(args->super.srgb_correction) {
3478 arbfp_add_sRGB_correction(buffer, fragcolor, srgbtmp[0], srgbtmp[1], srgbtmp[2], srgbtmp[3],
3479 priv_ctx.target_version >= NV2);
3480 } else if(reg_maps->shader_version.major < 2) {
3481 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
3483 shader_addline(buffer, "END\n");
3485 /* TODO: change to resource.glObjectHandle or something like that */
3486 GL_EXTCALL(glGenProgramsARB(1, &retval));
3488 TRACE("Creating a hw pixel shader, prg=%d\n", retval);
3489 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
3491 TRACE("Created hw pixel shader, prg=%d\n", retval);
3492 /* Create the program and check for errors */
3493 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3494 buffer->bsize, buffer->buffer));
3496 if (glGetError() == GL_INVALID_OPERATION) {
3497 GLint errPos;
3498 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
3499 FIXME("HW PixelShader Error at position %d: %s\n",
3500 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3501 retval = 0;
3504 /* Load immediate constants */
3505 if(lconst_map) {
3506 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
3507 const float *value = (const float *)lconst->value;
3508 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst_map[lconst->idx], value));
3509 checkGLcall("glProgramLocalParameter4fvARB");
3511 HeapFree(GetProcessHeap(), 0, lconst_map);
3514 return retval;
3517 static int compare_sig(const struct wined3d_shader_signature_element *sig1, const struct wined3d_shader_signature_element *sig2)
3519 unsigned int i;
3520 int ret;
3522 for(i = 0; i < MAX_REG_INPUT; i++)
3524 if(sig1[i].semantic_name == NULL || sig2[i].semantic_name == NULL)
3526 /* Compare pointers, not contents. One string is NULL(element does not exist), the other one is not NULL */
3527 if(sig1[i].semantic_name != sig2[i].semantic_name) return sig1[i].semantic_name < sig2[i].semantic_name ? -1 : 1;
3528 continue;
3531 ret = strcmp(sig1[i].semantic_name, sig2[i].semantic_name);
3532 if(ret != 0) return ret;
3533 if(sig1[i].semantic_idx != sig2[i].semantic_idx) return sig1[i].semantic_idx < sig2[i].semantic_idx ? -1 : 1;
3534 if(sig1[i].sysval_semantic != sig2[i].sysval_semantic) return sig1[i].sysval_semantic < sig2[i].sysval_semantic ? -1 : 1;
3535 if(sig1[i].component_type != sig2[i].component_type) return sig1[i].sysval_semantic < sig2[i].component_type ? -1 : 1;
3536 if(sig1[i].register_idx != sig2[i].register_idx) return sig1[i].register_idx < sig2[i].register_idx ? -1 : 1;
3537 if(sig1[i].mask != sig2->mask) return sig1[i].mask < sig2[i].mask ? -1 : 1;
3539 return 0;
3542 static struct wined3d_shader_signature_element *clone_sig(const struct wined3d_shader_signature_element *sig)
3544 struct wined3d_shader_signature_element *new;
3545 int i;
3546 char *name;
3548 new = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*new) * MAX_REG_INPUT);
3549 for(i = 0; i < MAX_REG_INPUT; i++)
3551 if(sig[i].semantic_name == NULL)
3553 continue;
3556 new[i] = sig[i];
3557 /* Clone the semantic string */
3558 name = HeapAlloc(GetProcessHeap(), 0, strlen(sig[i].semantic_name) + 1);
3559 strcpy(name, sig[i].semantic_name);
3560 new[i].semantic_name = name;
3562 return new;
3565 static DWORD find_input_signature(struct shader_arb_priv *priv, const struct wined3d_shader_signature_element *sig)
3567 struct wine_rb_entry *entry = wine_rb_get(&priv->signature_tree, sig);
3568 struct ps_signature *found_sig;
3570 if(entry != NULL)
3572 found_sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
3573 TRACE("Found existing signature %u\n", found_sig->idx);
3574 return found_sig->idx;
3576 found_sig = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*sig));
3577 found_sig->sig = clone_sig(sig);
3578 found_sig->idx = priv->ps_sig_number++;
3579 TRACE("New signature stored and assigned number %u\n", found_sig->idx);
3580 if(wine_rb_put(&priv->signature_tree, sig, &found_sig->entry) == -1)
3582 ERR("Failed to insert program entry.\n");
3584 return found_sig->idx;
3587 static void init_output_registers(IWineD3DVertexShaderImpl *shader, DWORD sig_num, struct shader_arb_ctx_priv *priv_ctx,
3588 struct arb_vs_compiled_shader *compiled)
3590 unsigned int i, j;
3591 static const char *texcoords[8] =
3593 "result.texcoord[0]", "result.texcoord[1]", "result.texcoord[2]", "result.texcoord[3]",
3594 "result.texcoord[4]", "result.texcoord[5]", "result.texcoord[6]", "result.texcoord[7]"
3596 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) shader->baseShader.device;
3597 const struct wined3d_shader_signature_element *sig;
3598 const char *semantic_name;
3599 DWORD semantic_idx, reg_idx;
3601 /* Write generic input varyings 0 to 7 to result.texcoord[], varying 8 to result.color.primary
3602 * and varying 9 to result.color.secondary
3604 const char *decl_idx_to_string[MAX_REG_INPUT] =
3606 texcoords[0], texcoords[1], texcoords[2], texcoords[3],
3607 texcoords[4], texcoords[5], texcoords[6], texcoords[7],
3608 "result.color.primary", "result.color.secondary"
3611 if(sig_num == ~0)
3613 TRACE("Pixel shader uses builtin varyings\n");
3614 /* Map builtins to builtins */
3615 for(i = 0; i < 8; i++)
3617 priv_ctx->texcrd_output[i] = texcoords[i];
3619 priv_ctx->color_output[0] = "result.color.primary";
3620 priv_ctx->color_output[1] = "result.color.secondary";
3621 priv_ctx->fog_output = "result.fogcoord";
3623 /* Map declared regs to builtins. Use "TA" to /dev/null unread output */
3624 for(i = 0; i < (sizeof(shader->output_signature) / sizeof(*shader->output_signature)); i++)
3626 semantic_name = shader->output_signature[i].semantic_name;
3627 if(semantic_name == NULL) continue;
3629 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3631 TRACE("o%u is TMP_OUT\n", i);
3632 if(shader->output_signature[i].semantic_idx == 0) priv_ctx->vs_output[i] = "TMP_OUT";
3633 else priv_ctx->vs_output[i] = "TA";
3635 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3637 TRACE("o%u is result.pointsize\n", i);
3638 if(shader->output_signature[i].semantic_idx == 0) priv_ctx->vs_output[i] = "result.pointsize";
3639 else priv_ctx->vs_output[i] = "TA";
3641 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3643 TRACE("o%u is result.color.?, idx %u\n", i, shader->output_signature[i].semantic_idx);
3644 if(shader->output_signature[i].semantic_idx == 0) priv_ctx->vs_output[i] = "result.color.primary";
3645 else if(shader->output_signature[i].semantic_idx == 1) priv_ctx->vs_output[i] = "result.color.secondary";
3646 else priv_ctx->vs_output[i] = "TA";
3648 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3650 TRACE("o%u is %s\n", i, texcoords[shader->output_signature[i].semantic_idx]);
3651 if(shader->output_signature[i].semantic_idx >= 8) priv_ctx->vs_output[i] = "TA";
3652 else priv_ctx->vs_output[i] = texcoords[shader->output_signature[i].semantic_idx];
3654 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3656 TRACE("o%u is result.fogcoord\n", i);
3657 if(shader->output_signature[i].semantic_idx > 0) priv_ctx->vs_output[i] = "TA";
3658 else priv_ctx->vs_output[i] = "result.fogcoord";
3660 else
3662 priv_ctx->vs_output[i] = "TA";
3665 return;
3668 /* Instead of searching for the signature in the signature list, read the one from the current pixel shader.
3669 * Its maybe not the shader where the signature came from, but it is the same signature and faster to find
3671 sig = ((IWineD3DPixelShaderImpl *)device->stateBlock->pixelShader)->input_signature;
3672 TRACE("Pixel shader uses declared varyings\n");
3674 /* Map builtin to declared. /dev/null the results by default to the TA temp reg */
3675 for(i = 0; i < 8; i++)
3677 priv_ctx->texcrd_output[i] = "TA";
3679 priv_ctx->color_output[0] = "TA";
3680 priv_ctx->color_output[1] = "TA";
3681 priv_ctx->fog_output = "TA";
3683 for(i = 0; i < MAX_REG_INPUT; i++)
3685 semantic_name = sig[i].semantic_name;
3686 semantic_idx = sig[i].semantic_idx;
3687 reg_idx = sig[i].register_idx;
3688 if(semantic_name == NULL) continue;
3690 /* If a declared input register is not written by builtin arguments, don't write to it.
3691 * GL_NV_vertex_program makes sure the input defaults to 0.0, which is correct with D3D
3693 * Don't care about POSITION and PSIZE here - this is a builtin vertex shader, position goes
3694 * to TMP_OUT in any case
3696 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3698 if(semantic_idx < 8) priv_ctx->texcrd_output[semantic_idx] = decl_idx_to_string[reg_idx];
3700 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3702 if(semantic_idx < 2) priv_ctx->color_output[semantic_idx] = decl_idx_to_string[reg_idx];
3704 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3706 if(semantic_idx == 0) priv_ctx->fog_output = decl_idx_to_string[reg_idx];
3708 else
3710 continue;
3713 if(strcmp(decl_idx_to_string[reg_idx], "result.color.primary") == 0 ||
3714 strcmp(decl_idx_to_string[reg_idx], "result.color.secondary") == 0)
3716 compiled->need_color_unclamp = TRUE;
3720 /* Map declared to declared */
3721 for(i = 0; i < (sizeof(shader->output_signature) / sizeof(*shader->output_signature)); i++)
3723 /* Write unread output to TA to throw them away */
3724 priv_ctx->vs_output[i] = "TA";
3725 semantic_name = shader->output_signature[i].semantic_name;
3726 if(semantic_name == NULL)
3728 continue;
3731 if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION) &&
3732 shader->output_signature[i].semantic_idx == 0)
3734 priv_ctx->vs_output[i] = "TMP_OUT";
3735 continue;
3737 else if(shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE) &&
3738 shader->output_signature[i].semantic_idx == 0)
3740 priv_ctx->vs_output[i] = "result.pointsize";
3741 continue;
3744 for(j = 0; j < MAX_REG_INPUT; j++)
3746 if(sig[j].semantic_name == NULL)
3748 continue;
3751 if(strcmp(sig[j].semantic_name, semantic_name) == 0 &&
3752 sig[j].semantic_idx == shader->output_signature[i].semantic_idx)
3754 priv_ctx->vs_output[i] = decl_idx_to_string[sig[j].register_idx];
3756 if(strcmp(priv_ctx->vs_output[i], "result.color.primary") == 0 ||
3757 strcmp(priv_ctx->vs_output[i], "result.color.secondary") == 0)
3759 compiled->need_color_unclamp = TRUE;
3766 /* GL locking is done by the caller */
3767 static GLuint shader_arb_generate_vshader(IWineD3DVertexShaderImpl *This,
3768 SHADER_BUFFER *buffer, const struct arb_vs_compile_args *args, struct arb_vs_compiled_shader *compiled)
3770 const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3771 CONST DWORD *function = This->baseShader.function;
3772 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3773 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3774 const local_constant *lconst;
3775 GLuint ret;
3776 DWORD next_local, *lconst_map = local_const_mapping((IWineD3DBaseShaderImpl *) This);
3777 struct shader_arb_ctx_priv priv_ctx;
3778 unsigned int i;
3780 memset(&priv_ctx, 0, sizeof(priv_ctx));
3781 priv_ctx.cur_vs_args = args;
3782 list_init(&priv_ctx.control_frames);
3783 init_output_registers(This, args->ps_signature, &priv_ctx, compiled);
3785 /* Create the hw ARB shader */
3786 shader_addline(buffer, "!!ARBvp1.0\n");
3788 /* Always enable the NV extension if available. Unlike fragment shaders, there is no
3789 * mesurable performance penalty, and we can always make use of it for clipplanes.
3791 if(GL_SUPPORT(NV_VERTEX_PROGRAM3)) {
3792 shader_addline(buffer, "OPTION NV_vertex_program3;\n");
3793 priv_ctx.target_version = NV3;
3794 shader_addline(buffer, "ADDRESS aL;\n");
3795 } else if(GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION)) {
3796 shader_addline(buffer, "OPTION NV_vertex_program2;\n");
3797 priv_ctx.target_version = NV2;
3798 shader_addline(buffer, "ADDRESS aL;\n");
3799 } else {
3800 priv_ctx.target_version = ARB;
3803 shader_addline(buffer, "TEMP TMP_OUT;\n");
3804 if(need_helper_const(gl_info)) {
3805 shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
3807 if(need_mova_const((IWineD3DBaseShader *) This, gl_info)) {
3808 shader_addline(buffer, "PARAM mova_const = { 0.5, 0.0, 2.0, 1.0 };\n");
3809 shader_addline(buffer, "TEMP A0_SHADOW;\n");
3812 shader_addline(buffer, "TEMP TA;\n");
3814 /* Base Declarations */
3815 next_local = shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION,
3816 lconst_map, &priv_ctx.vs_clipplanes, &priv_ctx);
3818 for(i = 0; i < MAX_CONST_I; i++)
3820 compiled->int_consts[i] = WINED3D_CONST_NUM_UNUSED;
3821 if(reg_maps->integer_constants & (1 << i) && priv_ctx.target_version >= NV2)
3823 const DWORD *control_values = find_loop_control_values((IWineD3DBaseShaderImpl *) This, i);
3825 if(control_values)
3827 shader_addline(buffer, "PARAM I%u = {%u, %u, %u, -1};\n", i,
3828 control_values[0], control_values[1], control_values[2]);
3830 else
3832 compiled->int_consts[i] = next_local;
3833 compiled->num_int_consts++;
3834 shader_addline(buffer, "PARAM I%u = program.local[%u];\n", i, next_local++);
3839 /* We need a constant to fixup the final position */
3840 shader_addline(buffer, "PARAM posFixup = program.local[%u];\n", next_local);
3841 compiled->pos_fixup = next_local++;
3843 /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
3844 * for output parameters. D3D in theory does not do that either, but some applications depend on a
3845 * proper initialization of the secondary color, and programs using the fixed function pipeline without
3846 * a replacement shader depend on the texcoord.w being set properly.
3848 * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
3849 * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
3850 * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
3851 * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
3852 * this can eat a number of instructions, so skip it unless this cap is set as well
3854 if(!GL_SUPPORT(NV_VERTEX_PROGRAM)) {
3855 shader_addline(buffer, "MOV result.color.secondary, -helper_const.wwwy;\n");
3857 if ((GLINFO_LOCATION).quirks & WINED3D_QUIRK_SET_TEXCOORD_W && !device->frag_pipe->ffp_proj_control)
3859 int i;
3860 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3861 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3862 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3863 shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
3869 /* The shader starts with the main function */
3870 priv_ctx.in_main_func = TRUE;
3871 /* Base Shader Body */
3872 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
3874 if(!priv_ctx.footer_written) vshader_add_footer(This, buffer, args, &priv_ctx);
3876 shader_addline(buffer, "END\n");
3878 /* TODO: change to resource.glObjectHandle or something like that */
3879 GL_EXTCALL(glGenProgramsARB(1, &ret));
3881 TRACE("Creating a hw vertex shader, prg=%d\n", ret);
3882 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, ret));
3884 TRACE("Created hw vertex shader, prg=%d\n", ret);
3885 /* Create the program and check for errors */
3886 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
3887 buffer->bsize, buffer->buffer));
3889 if (glGetError() == GL_INVALID_OPERATION) {
3890 GLint errPos;
3891 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
3892 FIXME("HW VertexShader Error at position %d: %s\n",
3893 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3894 ret = -1;
3895 } else {
3896 /* Load immediate constants */
3897 if(lconst_map) {
3898 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
3899 const float *value = (const float *)lconst->value;
3900 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst_map[lconst->idx], value));
3904 HeapFree(GetProcessHeap(), 0, lconst_map);
3906 return ret;
3909 static void find_clip_texcoord(IWineD3DPixelShaderImpl *ps)
3911 struct arb_pshader_private *shader_priv = ps->backend_priv;
3912 int i;
3913 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)ps->baseShader.device)->adapter->gl_info;
3915 /* See if we can use fragment.texcoord[7] for clipplane emulation
3917 * Don't do this if it is not supported, or fragment.texcoord[7] is used
3919 if(ps->baseShader.reg_maps.shader_version.major < 3)
3921 for(i = GL_LIMITS(texture_stages); i > 0; i--)
3923 if(!ps->baseShader.reg_maps.texcoord[i - 1])
3925 shader_priv->clipplane_emulation = i;
3926 return;
3929 WARN("Did not find a free clip reg(2.0)\n");
3931 else
3933 for(i = GL_LIMITS(texture_stages); i > 0; i--)
3935 if(!(ps->baseShader.reg_maps.input_registers & (1 << (i - 1))))
3937 shader_priv->clipplane_emulation = i;
3938 return;
3941 WARN("Did not find a free clip reg(3.0)\n");
3945 /* GL locking is done by the caller */
3946 static struct arb_ps_compiled_shader *find_arb_pshader(IWineD3DPixelShaderImpl *shader, const struct arb_ps_compile_args *args)
3948 UINT i;
3949 DWORD new_size;
3950 struct arb_ps_compiled_shader *new_array;
3951 SHADER_BUFFER buffer;
3952 struct arb_pshader_private *shader_data;
3953 GLuint ret;
3955 if(!shader->backend_priv) {
3956 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) shader->baseShader.device;
3957 struct shader_arb_priv *priv = device->shader_priv;
3959 shader->backend_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
3960 shader_data = shader->backend_priv;
3961 shader_data->clamp_consts = shader->baseShader.reg_maps.shader_version.major == 1;
3963 if(shader->baseShader.reg_maps.shader_version.major < 3) shader_data->input_signature_idx = ~0;
3964 else shader_data->input_signature_idx = find_input_signature(priv, shader->input_signature);
3966 shader_data->has_signature_idx = TRUE;
3967 TRACE("Shader got assigned input signature index %u\n", shader_data->input_signature_idx);
3969 if(!device->vs_clipping) find_clip_texcoord(shader);
3971 shader_data = shader->backend_priv;
3973 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
3974 * so a linear search is more performant than a hashmap or a binary search
3975 * (cache coherency etc)
3977 for(i = 0; i < shader_data->num_gl_shaders; i++) {
3978 if(memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)) == 0) {
3979 return &shader_data->gl_shaders[i];
3983 TRACE("No matching GL shader found, compiling a new shader\n");
3984 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
3985 if (shader_data->num_gl_shaders)
3987 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
3988 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
3989 new_size * sizeof(*shader_data->gl_shaders));
3990 } else {
3991 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
3992 new_size = 1;
3995 if(!new_array) {
3996 ERR("Out of memory\n");
3997 return 0;
3999 shader_data->gl_shaders = new_array;
4000 shader_data->shader_array_size = new_size;
4003 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4005 pixelshader_update_samplers(&shader->baseShader.reg_maps,
4006 ((IWineD3DDeviceImpl *)shader->baseShader.device)->stateBlock->textures);
4008 shader_buffer_init(&buffer);
4009 ret = shader_arb_generate_pshader(shader, &buffer, args,
4010 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4011 shader_buffer_free(&buffer);
4012 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4014 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4017 static inline BOOL vs_args_equal(const struct arb_vs_compile_args *stored, const struct arb_vs_compile_args *new,
4018 const DWORD use_map, BOOL skip_int) {
4019 if((stored->super.swizzle_map & use_map) != new->super.swizzle_map) return FALSE;
4020 if(stored->super.fog_src != new->super.fog_src) return FALSE;
4021 if(stored->boolclip_compare != new->boolclip_compare) return FALSE;
4022 if(stored->ps_signature != new->ps_signature) return FALSE;
4023 if(stored->vertex_samplers_compare != new->vertex_samplers_compare) return FALSE;
4024 if(skip_int) return TRUE;
4026 return memcmp(stored->loop_ctrl, new->loop_ctrl, sizeof(stored->loop_ctrl)) == 0;
4029 static struct arb_vs_compiled_shader *find_arb_vshader(IWineD3DVertexShaderImpl *shader, const struct arb_vs_compile_args *args)
4031 UINT i;
4032 DWORD new_size;
4033 struct arb_vs_compiled_shader *new_array;
4034 DWORD use_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.use_map;
4035 SHADER_BUFFER buffer;
4036 struct arb_vshader_private *shader_data;
4037 GLuint ret;
4038 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)shader->baseShader.device)->adapter->gl_info;
4040 if(!shader->backend_priv) {
4041 shader->backend_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4043 shader_data = shader->backend_priv;
4045 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4046 * so a linear search is more performant than a hashmap or a binary search
4047 * (cache coherency etc)
4049 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4050 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map, GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION))) {
4051 return &shader_data->gl_shaders[i];
4055 TRACE("No matching GL shader found, compiling a new shader\n");
4057 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4058 if (shader_data->num_gl_shaders)
4060 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4061 new_array = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, shader_data->gl_shaders,
4062 new_size * sizeof(*shader_data->gl_shaders));
4063 } else {
4064 new_array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data->gl_shaders));
4065 new_size = 1;
4068 if(!new_array) {
4069 ERR("Out of memory\n");
4070 return 0;
4072 shader_data->gl_shaders = new_array;
4073 shader_data->shader_array_size = new_size;
4076 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4078 shader_buffer_init(&buffer);
4079 ret = shader_arb_generate_vshader(shader, &buffer, args,
4080 &shader_data->gl_shaders[shader_data->num_gl_shaders]);
4081 shader_buffer_free(&buffer);
4082 shader_data->gl_shaders[shader_data->num_gl_shaders].prgId = ret;
4084 return &shader_data->gl_shaders[shader_data->num_gl_shaders++];
4087 static inline void find_arb_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock,
4088 struct arb_ps_compile_args *args)
4090 int i;
4091 WORD int_skip;
4092 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)shader->baseShader.device)->adapter->gl_info;
4093 find_ps_compile_args(shader, stateblock, &args->super);
4095 /* This forces all local boolean constants to 1 to make them stateblock independent */
4096 args->bools = shader->baseShader.reg_maps.local_bool_consts;
4098 for(i = 0; i < MAX_CONST_B; i++)
4100 if(stateblock->pixelShaderConstantB[i]) args->bools |= ( 1 << i);
4103 /* Skip if unused or local, or supported natively */
4104 int_skip = ~shader->baseShader.reg_maps.integer_constants | shader->baseShader.reg_maps.local_int_consts;
4105 if(int_skip == 0xffff || GL_SUPPORT(NV_FRAGMENT_PROGRAM_OPTION))
4107 memset(&args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4108 return;
4111 for(i = 0; i < MAX_CONST_I; i++)
4113 if(int_skip & (1 << i))
4115 args->loop_ctrl[i][0] = 0;
4116 args->loop_ctrl[i][1] = 0;
4117 args->loop_ctrl[i][2] = 0;
4119 else
4121 args->loop_ctrl[i][0] = stateblock->pixelShaderConstantI[i * 4];
4122 args->loop_ctrl[i][1] = stateblock->pixelShaderConstantI[i * 4 + 1];
4123 args->loop_ctrl[i][2] = stateblock->pixelShaderConstantI[i * 4 + 2];
4128 static inline void find_arb_vs_compile_args(IWineD3DVertexShaderImpl *shader, IWineD3DStateBlockImpl *stateblock,
4129 struct arb_vs_compile_args *args)
4131 int i;
4132 WORD int_skip;
4133 IWineD3DDeviceImpl *dev = (IWineD3DDeviceImpl *)shader->baseShader.device;
4134 const WineD3D_GL_Info *gl_info = &dev->adapter->gl_info;
4135 find_vs_compile_args(shader, stateblock, &args->super);
4137 args->boolclip_compare = 0;
4138 if(use_ps(stateblock))
4140 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) stateblock->pixelShader;
4141 struct arb_pshader_private *shader_priv = ps->backend_priv;
4142 args->ps_signature = shader_priv->input_signature_idx;
4144 args->boolclip.clip_control[0] = shader_priv->clipplane_emulation;
4146 else
4148 args->ps_signature = ~0;
4149 if(!dev->vs_clipping)
4151 args->boolclip.clip_control[0] = ffp_clip_emul(stateblock) ? GL_LIMITS(texture_stages) : 0;
4153 /* Otherwise: Setting boolclip_compare set clip_control[0] to 0 */
4156 if(args->boolclip.clip_control[0])
4158 if(stateblock->renderState[WINED3DRS_CLIPPING])
4160 args->boolclip.clip_control[1] = stateblock->renderState[WINED3DRS_CLIPPLANEENABLE];
4162 /* clip_control[1] was set to 0 by setting boolclip_compare to 0 */
4165 /* This forces all local boolean constants to 1 to make them stateblock independent */
4166 args->boolclip.bools = shader->baseShader.reg_maps.local_bool_consts;
4167 /* TODO: Figure out if it would be better to store bool constants as bitmasks in the stateblock */
4168 for(i = 0; i < MAX_CONST_B; i++)
4170 if(stateblock->vertexShaderConstantB[i]) args->boolclip.bools |= ( 1 << i);
4173 args->vertex_samplers[0] = dev->texUnitMap[MAX_FRAGMENT_SAMPLERS + 0];
4174 args->vertex_samplers[1] = dev->texUnitMap[MAX_FRAGMENT_SAMPLERS + 1];
4175 args->vertex_samplers[2] = dev->texUnitMap[MAX_FRAGMENT_SAMPLERS + 2];
4176 args->vertex_samplers[3] = 0;
4178 /* Skip if unused or local */
4179 int_skip = ~shader->baseShader.reg_maps.integer_constants | shader->baseShader.reg_maps.local_int_consts;
4180 if(int_skip == 0xffff || GL_SUPPORT(NV_VERTEX_PROGRAM2_OPTION)) /* This is about flow control, not clipping */
4182 memset(&args->loop_ctrl, 0, sizeof(args->loop_ctrl));
4183 return;
4186 for(i = 0; i < MAX_CONST_I; i++)
4188 if(int_skip & (1 << i))
4190 args->loop_ctrl[i][0] = 0;
4191 args->loop_ctrl[i][1] = 0;
4192 args->loop_ctrl[i][2] = 0;
4194 else
4196 args->loop_ctrl[i][0] = stateblock->vertexShaderConstantI[i * 4];
4197 args->loop_ctrl[i][1] = stateblock->vertexShaderConstantI[i * 4 + 1];
4198 args->loop_ctrl[i][2] = stateblock->vertexShaderConstantI[i * 4 + 2];
4203 /* GL locking is done by the caller */
4204 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
4205 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4206 struct shader_arb_priv *priv = This->shader_priv;
4207 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
4208 int i;
4210 /* Deal with pixel shaders first so the vertex shader arg function has the input signature ready */
4211 if (usePS) {
4212 struct arb_ps_compile_args compile_args;
4213 struct arb_ps_compiled_shader *compiled;
4214 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader;
4216 TRACE("Using pixel shader %p\n", This->stateBlock->pixelShader);
4217 find_arb_ps_compile_args(ps, This->stateBlock, &compile_args);
4218 compiled = find_arb_pshader(ps, &compile_args);
4219 priv->current_fprogram_id = compiled->prgId;
4220 priv->compiled_fprog = compiled;
4222 /* Bind the fragment program */
4223 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4224 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id);");
4226 if(!priv->use_arbfp_fixed_func) {
4227 /* Enable OpenGL fragment programs */
4228 glEnable(GL_FRAGMENT_PROGRAM_ARB);
4229 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
4231 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
4233 /* Pixel Shader 1.x constants are clamped to [-1;1], Pixel Shader 2.0 constants are not. If switching between
4234 * a 1.x and newer shader, reload the first 8 constants
4236 if(priv->last_ps_const_clamped != ((struct arb_pshader_private *) ps->backend_priv)->clamp_consts)
4238 priv->last_ps_const_clamped = ((struct arb_pshader_private *) ps->backend_priv)->clamp_consts;
4239 This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, 8);
4240 for(i = 0; i < 8; i++)
4242 This->activeContext->pshader_const_dirty[i] = 1;
4244 /* Also takes care of loading local constants */
4245 shader_arb_load_constants(iface, TRUE, FALSE);
4247 else
4249 shader_arb_ps_local_constants(This);
4252 /* Force constant reloading for the NP2 fixup (see comment in shader_glsl_select for more info) */
4253 if (compiled->np2fixup_info.super.active) This->shader_backend->shader_load_np2fixup_constants(iface, usePS, useVS);
4254 } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && !priv->use_arbfp_fixed_func) {
4255 /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
4256 * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
4257 * replacement shader
4259 glDisable(GL_FRAGMENT_PROGRAM_ARB);
4260 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4261 priv->current_fprogram_id = 0;
4264 if (useVS) {
4265 struct arb_vs_compile_args compile_args;
4266 struct arb_vs_compiled_shader *compiled;
4267 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) This->stateBlock->vertexShader;
4269 TRACE("Using vertex shader %p\n", This->stateBlock->vertexShader);
4270 find_arb_vs_compile_args(vs, This->stateBlock, &compile_args);
4271 compiled = find_arb_vshader(vs, &compile_args);
4272 priv->current_vprogram_id = compiled->prgId;
4273 priv->compiled_vprog = compiled;
4275 /* Bind the vertex program */
4276 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4277 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id);");
4279 /* Enable OpenGL vertex programs */
4280 glEnable(GL_VERTEX_PROGRAM_ARB);
4281 checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
4282 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
4283 shader_arb_vs_local_constants(This);
4285 if(priv->last_vs_color_unclamp != compiled->need_color_unclamp) {
4286 priv->last_vs_color_unclamp = compiled->need_color_unclamp;
4288 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
4289 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, !compiled->need_color_unclamp));
4290 checkGLcall("glClampColorARB");
4291 } else {
4292 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4295 } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
4296 priv->current_vprogram_id = 0;
4297 glDisable(GL_VERTEX_PROGRAM_ARB);
4298 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4302 /* GL locking is done by the caller */
4303 static void shader_arb_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
4304 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4305 struct shader_arb_priv *priv = This->shader_priv;
4306 GLuint *blt_fprogram = &priv->depth_blt_fprogram_id[tex_type];
4307 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
4309 if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
4310 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
4311 glEnable(GL_VERTEX_PROGRAM_ARB);
4313 if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type);
4314 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
4315 glEnable(GL_FRAGMENT_PROGRAM_ARB);
4318 /* GL locking is done by the caller */
4319 static void shader_arb_deselect_depth_blt(IWineD3DDevice *iface) {
4320 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4321 struct shader_arb_priv *priv = This->shader_priv;
4322 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
4324 if (priv->current_vprogram_id) {
4325 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
4326 checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
4328 TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
4329 } else {
4330 glDisable(GL_VERTEX_PROGRAM_ARB);
4331 checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
4334 if (priv->current_fprogram_id) {
4335 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
4336 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
4338 TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
4339 } else if(!priv->use_arbfp_fixed_func) {
4340 glDisable(GL_FRAGMENT_PROGRAM_ARB);
4341 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
4345 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
4346 IWineD3DBaseShaderImpl *baseShader = (IWineD3DBaseShaderImpl *) iface;
4347 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)baseShader->baseShader.device;
4348 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
4350 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4352 if (shader_is_pshader_version(baseShader->baseShader.reg_maps.shader_version.type))
4354 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
4355 struct arb_pshader_private *shader_data = This->backend_priv;
4356 UINT i;
4358 if(!shader_data) return; /* This can happen if a shader was never compiled */
4359 ENTER_GL();
4360 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4361 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4362 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4364 LEAVE_GL();
4365 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4366 HeapFree(GetProcessHeap(), 0, shader_data);
4367 This->backend_priv = NULL;
4368 } else {
4369 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *) iface;
4370 struct arb_vshader_private *shader_data = This->backend_priv;
4371 UINT i;
4373 if(!shader_data) return; /* This can happen if a shader was never compiled */
4374 ENTER_GL();
4375 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4376 GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId));
4377 checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &shader_data->gl_shaders[i].prgId))");
4379 LEAVE_GL();
4380 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4381 HeapFree(GetProcessHeap(), 0, shader_data);
4382 This->backend_priv = NULL;
4386 static int sig_tree_compare(const void *key, const struct wine_rb_entry *entry)
4388 struct ps_signature *e = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4389 return compare_sig(key, e->sig);
4392 struct wine_rb_functions sig_tree_functions =
4394 wined3d_rb_alloc,
4395 wined3d_rb_realloc,
4396 wined3d_rb_free,
4397 sig_tree_compare
4400 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
4401 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4402 struct shader_arb_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*priv));
4403 if(wine_rb_init(&priv->signature_tree, &sig_tree_functions) == -1)
4405 ERR("RB tree init failed\n");
4406 HeapFree(GetProcessHeap(), 0, priv);
4407 return E_OUTOFMEMORY;
4409 This->shader_priv = priv;
4410 return WINED3D_OK;
4413 static void release_signature(struct wine_rb_entry *entry, void *context)
4415 struct ps_signature *sig = WINE_RB_ENTRY_VALUE(entry, struct ps_signature, entry);
4416 int i;
4417 for(i = 0; i < MAX_REG_INPUT; i++)
4419 HeapFree(GetProcessHeap(), 0, (char *) sig->sig[i].semantic_name);
4421 HeapFree(GetProcessHeap(), 0, sig->sig);
4422 HeapFree(GetProcessHeap(), 0, sig);
4425 /* Context activation is done by the caller. */
4426 static void shader_arb_free(IWineD3DDevice *iface) {
4427 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4428 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
4429 struct shader_arb_priv *priv = This->shader_priv;
4430 int i;
4432 ENTER_GL();
4433 if(priv->depth_blt_vprogram_id) {
4434 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
4436 for (i = 0; i < tex_type_count; ++i) {
4437 if (priv->depth_blt_fprogram_id[i]) {
4438 GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id[i]));
4441 LEAVE_GL();
4443 wine_rb_destroy(&priv->signature_tree, release_signature, NULL);
4444 HeapFree(GetProcessHeap(), 0, This->shader_priv);
4447 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
4448 return TRUE;
4451 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
4453 /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
4454 * then overwrite the shader specific ones
4456 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
4458 if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
4459 if(GL_SUPPORT(NV_VERTEX_PROGRAM3))
4461 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4462 TRACE_(d3d_caps)("Hardware vertex shader version 3.0 enabled (NV_VERTEX_PROGRAM3)\n");
4464 else if(GL_LIMITS(vshader_constantsF) >= 256)
4466 /* Shader Model 2.0 requires at least 256 vertex shader constants */
4467 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4468 TRACE_(d3d_caps)("Hardware vertex shader version 2.0 enabled (ARB_PROGRAM)\n");
4470 else
4472 pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
4473 TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
4475 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
4478 if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
4479 if(GL_SUPPORT(NV_FRAGMENT_PROGRAM2))
4481 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4482 TRACE_(d3d_caps)("Hardware pixel shader version 3.0 enabled (NV_FRAGMENT_PROGRAM2)\n");
4484 else if(GL_LIMITS(vshader_constantsF) >= 32)
4486 /* Shader Model 2.0 requires at least 32 pixel shader constants */
4487 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4488 TRACE_(d3d_caps)("Hardware pixel shader version 2.0 enabled (ARB_PROGRAM)\n");
4490 else
4492 pCaps->PixelShaderVersion = WINED3DPS_VERSION(1,4);
4493 TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
4495 pCaps->PixelShader1xMaxValue = 8.0;
4496 pCaps->MaxPixelShaderConst = GL_LIMITS(pshader_constantsF);
4499 pCaps->VSClipping = use_nv_clip(gl_info);
4502 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
4504 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4506 TRACE("Checking support for color_fixup:\n");
4507 dump_color_fixup_desc(fixup);
4510 /* We support everything except YUV conversions. */
4511 if (!is_yuv_fixup(fixup))
4513 TRACE("[OK]\n");
4514 return TRUE;
4517 TRACE("[FAILED]\n");
4518 return FALSE;
4521 static void shader_arb_add_instruction_modifiers(const struct wined3d_shader_instruction *ins) {
4522 DWORD shift;
4523 char write_mask[20], regstr[50];
4524 SHADER_BUFFER *buffer = ins->ctx->buffer;
4525 BOOL is_color = FALSE;
4526 const struct wined3d_shader_dst_param *dst;
4528 if (!ins->dst_count) return;
4530 dst = &ins->dst[0];
4531 shift = dst->shift;
4532 if(shift == 0) return; /* Saturate alone is handled by the instructions */
4534 shader_arb_get_write_mask(ins, dst, write_mask);
4535 shader_arb_get_register_name(ins, &dst->reg, regstr, &is_color);
4537 /* Generate a line that does the output modifier computation
4538 * FIXME: _SAT vs shift? _SAT alone is already handled in the instructions, if this
4539 * maps problems in e.g. _d4_sat modify shader_arb_get_modifier
4541 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", shader_arb_get_modifier(ins),
4542 regstr, write_mask, regstr, shift_tab[shift]);
4545 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4547 /* WINED3DSIH_ABS */ shader_hw_map2gl,
4548 /* WINED3DSIH_ADD */ shader_hw_map2gl,
4549 /* WINED3DSIH_BEM */ pshader_hw_bem,
4550 /* WINED3DSIH_BREAK */ shader_hw_break,
4551 /* WINED3DSIH_BREAKC */ shader_hw_breakc,
4552 /* WINED3DSIH_BREAKP */ NULL,
4553 /* WINED3DSIH_CALL */ shader_hw_call,
4554 /* WINED3DSIH_CALLNZ */ NULL,
4555 /* WINED3DSIH_CMP */ pshader_hw_cmp,
4556 /* WINED3DSIH_CND */ pshader_hw_cnd,
4557 /* WINED3DSIH_CRS */ shader_hw_map2gl,
4558 /* WINED3DSIH_DCL */ NULL,
4559 /* WINED3DSIH_DEF */ NULL,
4560 /* WINED3DSIH_DEFB */ NULL,
4561 /* WINED3DSIH_DEFI */ NULL,
4562 /* WINED3DSIH_DP2ADD */ pshader_hw_dp2add,
4563 /* WINED3DSIH_DP3 */ shader_hw_map2gl,
4564 /* WINED3DSIH_DP4 */ shader_hw_map2gl,
4565 /* WINED3DSIH_DST */ shader_hw_map2gl,
4566 /* WINED3DSIH_DSX */ shader_hw_map2gl,
4567 /* WINED3DSIH_DSY */ shader_hw_dsy,
4568 /* WINED3DSIH_ELSE */ shader_hw_else,
4569 /* WINED3DSIH_ENDIF */ shader_hw_endif,
4570 /* WINED3DSIH_ENDLOOP */ shader_hw_endloop,
4571 /* WINED3DSIH_ENDREP */ shader_hw_endrep,
4572 /* WINED3DSIH_EXP */ shader_hw_map2gl,
4573 /* WINED3DSIH_EXPP */ shader_hw_map2gl,
4574 /* WINED3DSIH_FRC */ shader_hw_map2gl,
4575 /* WINED3DSIH_IF */ NULL /* Hardcoded into the shader */,
4576 /* WINED3DSIH_IFC */ shader_hw_ifc,
4577 /* WINED3DSIH_LABEL */ shader_hw_label,
4578 /* WINED3DSIH_LIT */ shader_hw_map2gl,
4579 /* WINED3DSIH_LOG */ shader_hw_log_pow,
4580 /* WINED3DSIH_LOGP */ shader_hw_log_pow,
4581 /* WINED3DSIH_LOOP */ shader_hw_loop,
4582 /* WINED3DSIH_LRP */ shader_hw_lrp,
4583 /* WINED3DSIH_M3x2 */ shader_hw_mnxn,
4584 /* WINED3DSIH_M3x3 */ shader_hw_mnxn,
4585 /* WINED3DSIH_M3x4 */ shader_hw_mnxn,
4586 /* WINED3DSIH_M4x3 */ shader_hw_mnxn,
4587 /* WINED3DSIH_M4x4 */ shader_hw_mnxn,
4588 /* WINED3DSIH_MAD */ shader_hw_map2gl,
4589 /* WINED3DSIH_MAX */ shader_hw_map2gl,
4590 /* WINED3DSIH_MIN */ shader_hw_map2gl,
4591 /* WINED3DSIH_MOV */ shader_hw_mov,
4592 /* WINED3DSIH_MOVA */ shader_hw_mov,
4593 /* WINED3DSIH_MUL */ shader_hw_map2gl,
4594 /* WINED3DSIH_NOP */ shader_hw_nop,
4595 /* WINED3DSIH_NRM */ shader_hw_nrm,
4596 /* WINED3DSIH_PHASE */ NULL,
4597 /* WINED3DSIH_POW */ shader_hw_log_pow,
4598 /* WINED3DSIH_RCP */ shader_hw_rsq_rcp,
4599 /* WINED3DSIH_REP */ shader_hw_rep,
4600 /* WINED3DSIH_RET */ shader_hw_ret,
4601 /* WINED3DSIH_RSQ */ shader_hw_rsq_rcp,
4602 /* WINED3DSIH_SETP */ NULL,
4603 /* WINED3DSIH_SGE */ shader_hw_map2gl,
4604 /* WINED3DSIH_SGN */ shader_hw_sgn,
4605 /* WINED3DSIH_SINCOS */ shader_hw_sincos,
4606 /* WINED3DSIH_SLT */ shader_hw_map2gl,
4607 /* WINED3DSIH_SUB */ shader_hw_map2gl,
4608 /* WINED3DSIH_TEX */ pshader_hw_tex,
4609 /* WINED3DSIH_TEXBEM */ pshader_hw_texbem,
4610 /* WINED3DSIH_TEXBEML */ pshader_hw_texbem,
4611 /* WINED3DSIH_TEXCOORD */ pshader_hw_texcoord,
4612 /* WINED3DSIH_TEXDEPTH */ pshader_hw_texdepth,
4613 /* WINED3DSIH_TEXDP3 */ pshader_hw_texdp3,
4614 /* WINED3DSIH_TEXDP3TEX */ pshader_hw_texdp3tex,
4615 /* WINED3DSIH_TEXKILL */ pshader_hw_texkill,
4616 /* WINED3DSIH_TEXLDD */ shader_hw_texldd,
4617 /* WINED3DSIH_TEXLDL */ shader_hw_texldl,
4618 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_hw_texm3x2depth,
4619 /* WINED3DSIH_TEXM3x2PAD */ pshader_hw_texm3x2pad,
4620 /* WINED3DSIH_TEXM3x2TEX */ pshader_hw_texm3x2tex,
4621 /* WINED3DSIH_TEXM3x3 */ pshader_hw_texm3x3,
4622 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
4623 /* WINED3DSIH_TEXM3x3PAD */ pshader_hw_texm3x3pad,
4624 /* WINED3DSIH_TEXM3x3SPEC */ pshader_hw_texm3x3spec,
4625 /* WINED3DSIH_TEXM3x3TEX */ pshader_hw_texm3x3tex,
4626 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_hw_texm3x3vspec,
4627 /* WINED3DSIH_TEXREG2AR */ pshader_hw_texreg2ar,
4628 /* WINED3DSIH_TEXREG2GB */ pshader_hw_texreg2gb,
4629 /* WINED3DSIH_TEXREG2RGB */ pshader_hw_texreg2rgb,
4632 static inline BOOL get_bool_const(const struct wined3d_shader_instruction *ins, IWineD3DBaseShaderImpl *This, DWORD idx)
4634 BOOL vshader = shader_is_vshader_version(This->baseShader.reg_maps.shader_version.type);
4635 WORD bools = 0;
4636 WORD flag = (1 << idx);
4637 const local_constant *constant;
4638 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
4640 if(This->baseShader.reg_maps.local_bool_consts & flag)
4642 /* What good is a if(bool) with a hardcoded local constant? I don't know, but handle it */
4643 LIST_FOR_EACH_ENTRY(constant, &This->baseShader.constantsB, local_constant, entry)
4645 if (constant->idx == idx)
4647 return constant->value[0];
4650 ERR("Local constant not found\n");
4651 return FALSE;
4653 else
4655 if(vshader) bools = priv->cur_vs_args->boolclip.bools;
4656 else bools = priv->cur_ps_args->bools;
4657 return bools & flag;
4661 static void get_loop_control_const(const struct wined3d_shader_instruction *ins,
4662 IWineD3DBaseShaderImpl *This, UINT idx, struct loop_control *loop_control)
4664 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
4666 /* Integer constants can either be a local constant, or they can be stored in the shader
4667 * type specific compile args. */
4668 if (This->baseShader.reg_maps.local_int_consts & (1 << idx))
4670 const local_constant *constant;
4672 LIST_FOR_EACH_ENTRY(constant, &This->baseShader.constantsI, local_constant, entry)
4674 if (constant->idx == idx)
4676 loop_control->count = constant->value[0];
4677 loop_control->start = constant->value[1];
4678 /* Step is signed. */
4679 loop_control->step = (int)constant->value[2];
4680 return;
4683 /* If this happens the flag was set incorrectly */
4684 ERR("Local constant not found\n");
4685 loop_control->count = 0;
4686 loop_control->start = 0;
4687 loop_control->step = 0;
4688 return;
4691 switch (This->baseShader.reg_maps.shader_version.type)
4693 case WINED3D_SHADER_TYPE_VERTEX:
4694 /* Count and aL start value are unsigned */
4695 loop_control->count = priv->cur_vs_args->loop_ctrl[idx][0];
4696 loop_control->start = priv->cur_vs_args->loop_ctrl[idx][1];
4697 /* Step is signed. */
4698 loop_control->step = ((char)priv->cur_vs_args->loop_ctrl[idx][2]);
4699 break;
4701 case WINED3D_SHADER_TYPE_PIXEL:
4702 loop_control->count = priv->cur_ps_args->loop_ctrl[idx][0];
4703 loop_control->start = priv->cur_ps_args->loop_ctrl[idx][1];
4704 loop_control->step = ((char)priv->cur_ps_args->loop_ctrl[idx][2]);
4705 break;
4707 default:
4708 FIXME("Unhandled shader type %#x.\n", This->baseShader.reg_maps.shader_version.type);
4709 break;
4713 static void record_instruction(struct list *list, const struct wined3d_shader_instruction *ins)
4715 unsigned int i;
4716 struct wined3d_shader_dst_param *dst_param = NULL;
4717 struct wined3d_shader_src_param *src_param = NULL, *rel_addr = NULL;
4718 struct recorded_instruction *rec = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rec));
4719 if(!rec)
4721 ERR("Out of memory\n");
4722 return;
4725 rec->ins = *ins;
4726 dst_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param));
4727 if(!dst_param) goto free;
4728 *dst_param = *ins->dst;
4729 if(ins->dst->reg.rel_addr)
4731 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*dst_param->reg.rel_addr));
4732 if(!rel_addr) goto free;
4733 *rel_addr = *ins->dst->reg.rel_addr;
4734 dst_param->reg.rel_addr = rel_addr;
4736 rec->ins.dst = dst_param;
4738 src_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*src_param) * ins->src_count);
4739 if(!src_param) goto free;
4740 for(i = 0; i < ins->src_count; i++)
4742 src_param[i] = ins->src[i];
4743 if(ins->src[i].reg.rel_addr)
4745 rel_addr = HeapAlloc(GetProcessHeap(), 0, sizeof(*rel_addr));
4746 if(!rel_addr) goto free;
4747 *rel_addr = *ins->src[i].reg.rel_addr;
4748 src_param[i].reg.rel_addr = rel_addr;
4751 rec->ins.src = src_param;
4752 list_add_tail(list, &rec->entry);
4753 return;
4755 free:
4756 ERR("Out of memory\n");
4757 if(dst_param)
4759 HeapFree(GetProcessHeap(), 0, (void *) dst_param->reg.rel_addr);
4760 HeapFree(GetProcessHeap(), 0, dst_param);
4762 if(src_param)
4764 for(i = 0; i < ins->src_count; i++)
4766 HeapFree(GetProcessHeap(), 0, (void *) src_param[i].reg.rel_addr);
4768 HeapFree(GetProcessHeap(), 0, src_param);
4770 HeapFree(GetProcessHeap(), 0, rec);
4773 static void free_recorded_instruction(struct list *list)
4775 struct recorded_instruction *rec_ins, *entry2;
4776 unsigned int i;
4778 LIST_FOR_EACH_ENTRY_SAFE(rec_ins, entry2, list, struct recorded_instruction, entry)
4780 list_remove(&rec_ins->entry);
4781 if(rec_ins->ins.dst)
4783 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.dst->reg.rel_addr);
4784 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.dst);
4786 if(rec_ins->ins.src)
4788 for(i = 0; i < rec_ins->ins.src_count; i++)
4790 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.src[i].reg.rel_addr);
4792 HeapFree(GetProcessHeap(), 0, (void *) rec_ins->ins.src);
4794 HeapFree(GetProcessHeap(), 0, rec_ins);
4798 static void shader_arb_handle_instruction(const struct wined3d_shader_instruction *ins) {
4799 SHADER_HANDLER hw_fct;
4800 struct shader_arb_ctx_priv *priv = ins->ctx->backend_data;
4801 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
4802 struct control_frame *control_frame;
4803 SHADER_BUFFER *buffer = ins->ctx->buffer;
4804 BOOL bool_const;
4806 if(ins->handler_idx == WINED3DSIH_LOOP || ins->handler_idx == WINED3DSIH_REP)
4808 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
4809 list_add_head(&priv->control_frames, &control_frame->entry);
4811 if(ins->handler_idx == WINED3DSIH_LOOP) control_frame->type = LOOP;
4812 if(ins->handler_idx == WINED3DSIH_REP) control_frame->type = REP;
4814 if(priv->target_version >= NV2)
4816 control_frame->loop_no = priv->num_loops++;
4817 priv->loop_depth++;
4819 else
4821 /* Don't bother recording when we're in a not used if branch */
4822 if(priv->muted)
4824 return;
4827 if(!priv->recording)
4829 list_init(&priv->record);
4830 priv->recording = TRUE;
4831 control_frame->outer_loop = TRUE;
4832 get_loop_control_const(ins, This, ins->src[0].reg.idx, &control_frame->loop_control);
4833 return; /* Instruction is handled */
4835 /* Record this loop in the outer loop's recording */
4838 else if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
4840 if(priv->target_version >= NV2)
4842 /* Nothing to do. The control frame is popped after the HW instr handler */
4844 else
4846 struct list *e = list_head(&priv->control_frames);
4847 control_frame = LIST_ENTRY(e, struct control_frame, entry);
4848 list_remove(&control_frame->entry);
4850 if(control_frame->outer_loop)
4852 int iteration, aL = 0;
4853 struct list copy;
4855 /* Turn off recording before playback */
4856 priv->recording = FALSE;
4858 /* Move the recorded instructions to a separate list and get them out of the private data
4859 * structure. If there are nested loops, the shader_arb_handle_instruction below will
4860 * be recorded again, thus priv->record might be overwritten
4862 list_init(&copy);
4863 list_move_tail(&copy, &priv->record);
4864 list_init(&priv->record);
4866 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
4868 shader_addline(buffer, "#unrolling loop: %u iterations, aL=%u, inc %d\n",
4869 control_frame->loop_control.count, control_frame->loop_control.start,
4870 control_frame->loop_control.step);
4871 aL = control_frame->loop_control.start;
4873 else
4875 shader_addline(buffer, "#unrolling rep: %u iterations\n", control_frame->loop_control.count);
4878 for (iteration = 0; iteration < control_frame->loop_control.count; ++iteration)
4880 struct recorded_instruction *rec_ins;
4881 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
4883 priv->aL = aL;
4884 shader_addline(buffer, "#Iteration %d, aL=%d\n", iteration, aL);
4886 else
4888 shader_addline(buffer, "#Iteration %d\n", iteration);
4891 LIST_FOR_EACH_ENTRY(rec_ins, &copy, struct recorded_instruction, entry)
4893 shader_arb_handle_instruction(&rec_ins->ins);
4896 if(ins->handler_idx == WINED3DSIH_ENDLOOP)
4898 aL += control_frame->loop_control.step;
4901 shader_addline(buffer, "#end loop/rep\n");
4903 free_recorded_instruction(&copy);
4904 HeapFree(GetProcessHeap(), 0, control_frame);
4905 return; /* Instruction is handled */
4907 else
4909 /* This is a nested loop. Proceed to the normal recording function */
4910 HeapFree(GetProcessHeap(), 0, control_frame);
4915 if(priv->recording)
4917 record_instruction(&priv->record, ins);
4918 return;
4921 /* boolean if */
4922 if(ins->handler_idx == WINED3DSIH_IF)
4924 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
4925 list_add_head(&priv->control_frames, &control_frame->entry);
4926 control_frame->type = IF;
4928 bool_const = get_bool_const(ins, This, ins->src[0].reg.idx);
4929 if(ins->src[0].modifiers == WINED3DSPSM_NOT) bool_const = !bool_const;
4930 if(!priv->muted && bool_const == FALSE)
4932 shader_addline(buffer, "#if(FALSE){\n");
4933 priv->muted = TRUE;
4934 control_frame->muting = TRUE;
4936 else shader_addline(buffer, "#if(TRUE) {\n");
4938 return; /* Instruction is handled */
4940 else if(ins->handler_idx == WINED3DSIH_IFC)
4942 /* IF(bool) and if_cond(a, b) use the same ELSE and ENDIF tokens */
4943 control_frame = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*control_frame));
4944 control_frame->type = IFC;
4945 control_frame->ifc_no = priv->num_ifcs++;
4946 list_add_head(&priv->control_frames, &control_frame->entry);
4948 else if(ins->handler_idx == WINED3DSIH_ELSE)
4950 struct list *e = list_head(&priv->control_frames);
4951 control_frame = LIST_ENTRY(e, struct control_frame, entry);
4953 if(control_frame->type == IF)
4955 shader_addline(buffer, "#} else {\n");
4956 if(!priv->muted && !control_frame->muting)
4958 priv->muted = TRUE;
4959 control_frame->muting = TRUE;
4961 else if(control_frame->muting) priv->muted = FALSE;
4962 return; /* Instruction is handled. */
4964 /* In case of an ifc, generate a HW shader instruction */
4966 else if(ins->handler_idx == WINED3DSIH_ENDIF)
4968 struct list *e = list_head(&priv->control_frames);
4969 control_frame = LIST_ENTRY(e, struct control_frame, entry);
4971 if(control_frame->type == IF)
4973 shader_addline(buffer, "#} endif\n");
4974 if(control_frame->muting) priv->muted = FALSE;
4975 list_remove(&control_frame->entry);
4976 HeapFree(GetProcessHeap(), 0, control_frame);
4977 return; /* Instruction is handled */
4981 if(priv->muted) return;
4983 /* Select handler */
4984 hw_fct = shader_arb_instruction_handler_table[ins->handler_idx];
4986 /* Unhandled opcode */
4987 if (!hw_fct)
4989 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
4990 return;
4992 hw_fct(ins);
4994 if(ins->handler_idx == WINED3DSIH_ENDLOOP || ins->handler_idx == WINED3DSIH_ENDREP)
4996 struct list *e = list_head(&priv->control_frames);
4997 control_frame = LIST_ENTRY(e, struct control_frame, entry);
4998 list_remove(&control_frame->entry);
4999 HeapFree(GetProcessHeap(), 0, control_frame);
5000 priv->loop_depth--;
5002 else if(ins->handler_idx == WINED3DSIH_ENDIF)
5004 /* Non-ifc ENDIFs don't reach that place because of the return in the if block above */
5005 struct list *e = list_head(&priv->control_frames);
5006 control_frame = LIST_ENTRY(e, struct control_frame, entry);
5007 list_remove(&control_frame->entry);
5008 HeapFree(GetProcessHeap(), 0, control_frame);
5012 shader_arb_add_instruction_modifiers(ins);
5015 const shader_backend_t arb_program_shader_backend = {
5016 shader_arb_handle_instruction,
5017 shader_arb_select,
5018 shader_arb_select_depth_blt,
5019 shader_arb_deselect_depth_blt,
5020 shader_arb_update_float_vertex_constants,
5021 shader_arb_update_float_pixel_constants,
5022 shader_arb_load_constants,
5023 shader_arb_load_np2fixup_constants,
5024 shader_arb_destroy,
5025 shader_arb_alloc,
5026 shader_arb_free,
5027 shader_arb_dirty_const,
5028 shader_arb_get_caps,
5029 shader_arb_color_fixup_supported,
5032 /* ARB_fragment_program fixed function pipeline replacement definitions */
5033 #define ARB_FFP_CONST_TFACTOR 0
5034 #define ARB_FFP_CONST_SPECULAR_ENABLE ((ARB_FFP_CONST_TFACTOR) + 1)
5035 #define ARB_FFP_CONST_CONSTANT(i) ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
5036 #define ARB_FFP_CONST_BUMPMAT(i) ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
5037 #define ARB_FFP_CONST_LUMINANCE(i) ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
5039 struct arbfp_ffp_desc
5041 struct ffp_frag_desc parent;
5042 GLuint shader;
5043 unsigned int num_textures_used;
5046 /* Context activation is done by the caller. */
5047 static void arbfp_enable(IWineD3DDevice *iface, BOOL enable) {
5048 ENTER_GL();
5049 if(enable) {
5050 glEnable(GL_FRAGMENT_PROGRAM_ARB);
5051 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
5052 } else {
5053 glDisable(GL_FRAGMENT_PROGRAM_ARB);
5054 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
5056 LEAVE_GL();
5059 static HRESULT arbfp_alloc(IWineD3DDevice *iface) {
5060 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
5061 struct shader_arb_priv *priv;
5062 /* Share private data between the shader backend and the pipeline replacement, if both
5063 * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
5064 * if no pixel shader is bound or not
5066 if(This->shader_backend == &arb_program_shader_backend) {
5067 This->fragment_priv = This->shader_priv;
5068 } else {
5069 This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
5070 if(!This->fragment_priv) return E_OUTOFMEMORY;
5072 priv = This->fragment_priv;
5073 if (wine_rb_init(&priv->fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
5075 ERR("Failed to initialize rbtree.\n");
5076 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
5077 return E_OUTOFMEMORY;
5079 priv->use_arbfp_fixed_func = TRUE;
5080 return WINED3D_OK;
5083 /* Context activation is done by the caller. */
5084 static void arbfp_free_ffpshader(struct wine_rb_entry *entry, void *context)
5086 const WineD3D_GL_Info *gl_info = context;
5087 struct arbfp_ffp_desc *entry_arb = WINE_RB_ENTRY_VALUE(entry, struct arbfp_ffp_desc, parent.entry);
5089 ENTER_GL();
5090 GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
5091 checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
5092 HeapFree(GetProcessHeap(), 0, entry_arb);
5093 LEAVE_GL();
5096 /* Context activation is done by the caller. */
5097 static void arbfp_free(IWineD3DDevice *iface) {
5098 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
5099 struct shader_arb_priv *priv = This->fragment_priv;
5101 wine_rb_destroy(&priv->fragment_shaders, arbfp_free_ffpshader, &This->adapter->gl_info);
5102 priv->use_arbfp_fixed_func = FALSE;
5104 if(This->shader_backend != &arb_program_shader_backend) {
5105 HeapFree(GetProcessHeap(), 0, This->fragment_priv);
5109 static void arbfp_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps)
5111 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE |
5112 WINED3DTEXOPCAPS_SELECTARG1 |
5113 WINED3DTEXOPCAPS_SELECTARG2 |
5114 WINED3DTEXOPCAPS_MODULATE4X |
5115 WINED3DTEXOPCAPS_MODULATE2X |
5116 WINED3DTEXOPCAPS_MODULATE |
5117 WINED3DTEXOPCAPS_ADDSIGNED2X |
5118 WINED3DTEXOPCAPS_ADDSIGNED |
5119 WINED3DTEXOPCAPS_ADD |
5120 WINED3DTEXOPCAPS_SUBTRACT |
5121 WINED3DTEXOPCAPS_ADDSMOOTH |
5122 WINED3DTEXOPCAPS_BLENDCURRENTALPHA |
5123 WINED3DTEXOPCAPS_BLENDFACTORALPHA |
5124 WINED3DTEXOPCAPS_BLENDTEXTUREALPHA |
5125 WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA |
5126 WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
5127 WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
5128 WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5129 WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
5130 WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
5131 WINED3DTEXOPCAPS_DOTPRODUCT3 |
5132 WINED3DTEXOPCAPS_MULTIPLYADD |
5133 WINED3DTEXOPCAPS_LERP |
5134 WINED3DTEXOPCAPS_BUMPENVMAP |
5135 WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
5137 /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
5139 caps->MaxTextureBlendStages = 8;
5140 caps->MaxSimultaneousTextures = min(GL_LIMITS(fragment_samplers), 8);
5142 caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
5144 #undef GLINFO_LOCATION
5146 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
5147 static void state_texfactor_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5148 float col[4];
5149 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5151 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5152 * application provided constants
5154 if(device->shader_backend == &arb_program_shader_backend) {
5155 if (use_ps(stateblock)) return;
5157 device = stateblock->wineD3DDevice;
5158 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
5159 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
5162 D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
5163 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
5164 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
5168 static void state_arb_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5169 float col[4];
5170 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5172 /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
5173 * application provided constants
5175 if(device->shader_backend == &arb_program_shader_backend) {
5176 if (use_ps(stateblock)) return;
5178 device = stateblock->wineD3DDevice;
5179 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
5180 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
5183 if(stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
5184 /* The specular color has no alpha */
5185 col[0] = 1.0; col[1] = 1.0;
5186 col[2] = 1.0; col[3] = 0.0;
5187 } else {
5188 col[0] = 0.0; col[1] = 0.0;
5189 col[2] = 0.0; col[3] = 0.0;
5191 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
5192 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
5195 static void set_bumpmat_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5196 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5197 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5198 float mat[2][2];
5200 if (use_ps(stateblock))
5202 if(stage != 0 &&
5203 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.bumpmat[stage]) {
5204 /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
5205 * anyway
5207 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
5208 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
5212 if(device->shader_backend == &arb_program_shader_backend) {
5213 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5214 return;
5216 } else if(device->shader_backend == &arb_program_shader_backend) {
5217 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
5218 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
5221 mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
5222 mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
5223 mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
5224 mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
5226 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
5227 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
5230 static void tex_bumpenvlum_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5231 DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5232 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5233 float param[4];
5235 if (use_ps(stateblock))
5237 if(stage != 0 &&
5238 ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.luminanceparams[stage]) {
5239 /* The pixel shader has to know the luminance offset. Do a constants update if it
5240 * isn't scheduled anyway
5242 if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
5243 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
5247 if(device->shader_backend == &arb_program_shader_backend) {
5248 /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
5249 return;
5251 } else if(device->shader_backend == &arb_program_shader_backend) {
5252 device->activeContext->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
5253 device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
5256 param[0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLSCALE]);
5257 param[1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET]);
5258 param[2] = 0.0;
5259 param[3] = 0.0;
5261 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
5262 checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
5265 static const char *get_argreg(SHADER_BUFFER *buffer, DWORD argnum, unsigned int stage, DWORD arg) {
5266 const char *ret;
5268 if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
5270 switch(arg & WINED3DTA_SELECTMASK) {
5271 case WINED3DTA_DIFFUSE:
5272 ret = "fragment.color.primary"; break;
5274 case WINED3DTA_CURRENT:
5275 if(stage == 0) ret = "fragment.color.primary";
5276 else ret = "ret";
5277 break;
5279 case WINED3DTA_TEXTURE:
5280 switch(stage) {
5281 case 0: ret = "tex0"; break;
5282 case 1: ret = "tex1"; break;
5283 case 2: ret = "tex2"; break;
5284 case 3: ret = "tex3"; break;
5285 case 4: ret = "tex4"; break;
5286 case 5: ret = "tex5"; break;
5287 case 6: ret = "tex6"; break;
5288 case 7: ret = "tex7"; break;
5289 default: ret = "unknown texture";
5291 break;
5293 case WINED3DTA_TFACTOR:
5294 ret = "tfactor"; break;
5296 case WINED3DTA_SPECULAR:
5297 ret = "fragment.color.secondary"; break;
5299 case WINED3DTA_TEMP:
5300 ret = "tempreg"; break;
5302 case WINED3DTA_CONSTANT:
5303 FIXME("Implement perstage constants\n");
5304 switch(stage) {
5305 case 0: ret = "const0"; break;
5306 case 1: ret = "const1"; break;
5307 case 2: ret = "const2"; break;
5308 case 3: ret = "const3"; break;
5309 case 4: ret = "const4"; break;
5310 case 5: ret = "const5"; break;
5311 case 6: ret = "const6"; break;
5312 case 7: ret = "const7"; break;
5313 default: ret = "unknown constant";
5315 break;
5317 default:
5318 return "unknown";
5321 if(arg & WINED3DTA_COMPLEMENT) {
5322 shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
5323 if(argnum == 0) ret = "arg0";
5324 if(argnum == 1) ret = "arg1";
5325 if(argnum == 2) ret = "arg2";
5327 if(arg & WINED3DTA_ALPHAREPLICATE) {
5328 shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
5329 if(argnum == 0) ret = "arg0";
5330 if(argnum == 1) ret = "arg1";
5331 if(argnum == 2) ret = "arg2";
5333 return ret;
5336 static void gen_ffp_instr(SHADER_BUFFER *buffer, unsigned int stage, BOOL color, BOOL alpha,
5337 DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2) {
5338 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5339 unsigned int mul = 1;
5340 BOOL mul_final_dest = FALSE;
5342 if(color && alpha) dstmask = "";
5343 else if(color) dstmask = ".xyz";
5344 else dstmask = ".w";
5346 if(dst == tempreg) dstreg = "tempreg";
5347 else dstreg = "ret";
5349 arg0 = get_argreg(buffer, 0, stage, dw_arg0);
5350 arg1 = get_argreg(buffer, 1, stage, dw_arg1);
5351 arg2 = get_argreg(buffer, 2, stage, dw_arg2);
5353 switch(op) {
5354 case WINED3DTOP_DISABLE:
5355 if(stage == 0) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
5356 break;
5358 case WINED3DTOP_SELECTARG2:
5359 arg1 = arg2;
5360 case WINED3DTOP_SELECTARG1:
5361 shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
5362 break;
5364 case WINED3DTOP_MODULATE4X:
5365 mul = 2;
5366 case WINED3DTOP_MODULATE2X:
5367 mul *= 2;
5368 if(strcmp(dstreg, "result.color") == 0) {
5369 dstreg = "ret";
5370 mul_final_dest = TRUE;
5372 case WINED3DTOP_MODULATE:
5373 shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5374 break;
5376 case WINED3DTOP_ADDSIGNED2X:
5377 mul = 2;
5378 if(strcmp(dstreg, "result.color") == 0) {
5379 dstreg = "ret";
5380 mul_final_dest = TRUE;
5382 case WINED3DTOP_ADDSIGNED:
5383 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
5384 arg2 = "arg2";
5385 case WINED3DTOP_ADD:
5386 shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5387 break;
5389 case WINED3DTOP_SUBTRACT:
5390 shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
5391 break;
5393 case WINED3DTOP_ADDSMOOTH:
5394 shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
5395 shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
5396 break;
5398 case WINED3DTOP_BLENDCURRENTALPHA:
5399 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
5400 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5401 break;
5402 case WINED3DTOP_BLENDFACTORALPHA:
5403 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
5404 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5405 break;
5406 case WINED3DTOP_BLENDTEXTUREALPHA:
5407 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
5408 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5409 break;
5410 case WINED3DTOP_BLENDDIFFUSEALPHA:
5411 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5412 shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5413 break;
5415 case WINED3DTOP_BLENDTEXTUREALPHAPM:
5416 arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
5417 shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
5418 shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
5419 break;
5421 /* D3DTOP_PREMODULATE ???? */
5423 case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
5424 shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
5425 shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
5426 break;
5427 case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
5428 shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
5429 break;
5430 case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
5431 shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
5432 shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
5433 break;
5434 case WINED3DTOP_MODULATECOLOR_ADDALPHA:
5435 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
5436 break;
5438 case WINED3DTOP_DOTPRODUCT3:
5439 mul = 4;
5440 if(strcmp(dstreg, "result.color") == 0) {
5441 dstreg = "ret";
5442 mul_final_dest = TRUE;
5444 shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
5445 shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
5446 shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
5447 break;
5449 case WINED3DTOP_MULTIPLYADD:
5450 shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
5451 break;
5453 case WINED3DTOP_LERP:
5454 /* The msdn is not quite right here */
5455 shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
5456 break;
5458 case WINED3DTOP_BUMPENVMAP:
5459 case WINED3DTOP_BUMPENVMAPLUMINANCE:
5460 /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
5461 break;
5463 default:
5464 FIXME("Unhandled texture op %08x\n", op);
5467 if(mul == 2) {
5468 shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
5469 } else if(mul == 4) {
5470 shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
5474 /* The stateblock is passed for GLINFO_LOCATION */
5475 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, IWineD3DStateBlockImpl *stateblock)
5477 unsigned int stage;
5478 SHADER_BUFFER buffer;
5479 BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5480 BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5481 BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
5482 const char *textype;
5483 const char *instr, *sat;
5484 char colorcor_dst[8];
5485 GLuint ret;
5486 DWORD arg0, arg1, arg2;
5487 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5488 BOOL op_equal;
5489 const char *final_combiner_src = "ret";
5491 /* Find out which textures are read */
5492 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5493 if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
5494 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5495 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5496 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5497 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5498 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5499 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5501 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
5502 if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
5503 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
5504 bump_used[stage] = TRUE;
5505 tex_read[stage] = TRUE;
5507 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
5508 bump_used[stage] = TRUE;
5509 tex_read[stage] = TRUE;
5510 luminance_used[stage] = TRUE;
5511 } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
5512 tfactor_used = TRUE;
5515 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
5516 tfactor_used = TRUE;
5519 if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
5520 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
5521 tempreg_used = TRUE;
5524 if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
5525 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5526 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5527 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5528 if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5529 if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5530 if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
5532 if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
5533 tempreg_used = TRUE;
5535 if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
5536 tfactor_used = TRUE;
5540 /* Shader header */
5541 shader_buffer_init(&buffer);
5543 shader_addline(&buffer, "!!ARBfp1.0\n");
5545 switch(settings->fog) {
5546 case FOG_OFF: break;
5547 case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
5548 case FOG_EXP: shader_addline(&buffer, "OPTION ARB_fog_exp;\n"); break;
5549 case FOG_EXP2: shader_addline(&buffer, "OPTION ARB_fog_exp2;\n"); break;
5550 default: FIXME("Unexpected fog setting %d\n", settings->fog);
5553 shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
5554 shader_addline(&buffer, "TEMP TMP;\n");
5555 shader_addline(&buffer, "TEMP ret;\n");
5556 if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
5557 shader_addline(&buffer, "TEMP arg0;\n");
5558 shader_addline(&buffer, "TEMP arg1;\n");
5559 shader_addline(&buffer, "TEMP arg2;\n");
5560 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5561 if(!tex_read[stage]) continue;
5562 shader_addline(&buffer, "TEMP tex%u;\n", stage);
5563 if(!bump_used[stage]) continue;
5564 shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
5565 if(!luminance_used[stage]) continue;
5566 shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
5568 if(tfactor_used) {
5569 shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
5571 shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
5573 if(settings->sRGB_write) {
5574 shader_addline(&buffer, "PARAM srgb_consts1 = {%f, %f, %f, %f};\n",
5575 srgb_mul_low, srgb_cmp, srgb_pow, srgb_mul_high);
5576 shader_addline(&buffer, "PARAM srgb_consts2 = {%f, %f, %f, %f};\n",
5577 srgb_sub_high, 0.0, 0.0, 0.0);
5580 if(ffp_clip_emul(stateblock) && settings->emul_clipplanes) shader_addline(&buffer, "KIL fragment.texcoord[7];\n");
5582 /* Generate texture sampling instructions) */
5583 for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
5584 if(!tex_read[stage]) continue;
5586 switch(settings->op[stage].tex_type) {
5587 case tex_1d: textype = "1D"; break;
5588 case tex_2d: textype = "2D"; break;
5589 case tex_3d: textype = "3D"; break;
5590 case tex_cube: textype = "CUBE"; break;
5591 case tex_rect: textype = "RECT"; break;
5592 default: textype = "unexpected_textype"; break;
5595 if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
5596 settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
5597 sat = "";
5598 } else {
5599 sat = "_SAT";
5602 if(settings->op[stage].projected == proj_none) {
5603 instr = "TEX";
5604 } else if(settings->op[stage].projected == proj_count4 ||
5605 settings->op[stage].projected == proj_count3) {
5606 instr = "TXP";
5607 } else {
5608 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5609 instr = "TXP";
5612 if(stage > 0 &&
5613 (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
5614 settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
5615 shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
5616 shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
5617 shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
5618 shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
5620 /* with projective textures, texbem only divides the static texture coord, not the displacement,
5621 * so multiply the displacement with the dividing parameter before passing it to TXP
5623 if (settings->op[stage].projected != proj_none) {
5624 if(settings->op[stage].projected == proj_count4) {
5625 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
5626 shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
5627 } else {
5628 shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
5629 shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
5631 } else {
5632 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
5635 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
5636 instr, sat, stage, stage, textype);
5637 if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
5638 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
5639 stage - 1, stage - 1, stage - 1);
5640 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
5642 } else if(settings->op[stage].projected == proj_count3) {
5643 shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
5644 shader_addline(&buffer, "MOV ret.w, ret.z;\n");
5645 shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
5646 instr, sat, stage, stage, textype);
5647 } else {
5648 shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
5649 instr, sat, stage, stage, stage, textype);
5652 sprintf(colorcor_dst, "tex%u", stage);
5653 gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
5654 settings->op[stage].color_fixup);
5657 /* Generate the main shader */
5658 for(stage = 0; stage < MAX_TEXTURES; stage++) {
5659 if(settings->op[stage].cop == WINED3DTOP_DISABLE) {
5660 if(stage == 0) {
5661 final_combiner_src = "fragment.color.primary";
5663 break;
5666 if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
5667 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
5668 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5669 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
5670 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
5671 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5672 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
5673 settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
5674 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5675 } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
5676 settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
5677 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5678 } else {
5679 op_equal = settings->op[stage].aop == settings->op[stage].cop &&
5680 settings->op[stage].carg0 == settings->op[stage].aarg0 &&
5681 settings->op[stage].carg1 == settings->op[stage].aarg1 &&
5682 settings->op[stage].carg2 == settings->op[stage].aarg2;
5685 if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
5686 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5687 settings->op[stage].cop, settings->op[stage].carg0,
5688 settings->op[stage].carg1, settings->op[stage].carg2);
5689 if(stage == 0) {
5690 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
5692 } else if(op_equal) {
5693 gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5694 settings->op[stage].cop, settings->op[stage].carg0,
5695 settings->op[stage].carg1, settings->op[stage].carg2);
5696 } else {
5697 gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5698 settings->op[stage].cop, settings->op[stage].carg0,
5699 settings->op[stage].carg1, settings->op[stage].carg2);
5700 gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5701 settings->op[stage].aop, settings->op[stage].aarg0,
5702 settings->op[stage].aarg1, settings->op[stage].aarg2);
5706 if(settings->sRGB_write) {
5707 shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
5708 arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg", FALSE);
5709 } else {
5710 shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
5713 /* Footer */
5714 shader_addline(&buffer, "END\n");
5716 /* Generate the shader */
5717 GL_EXTCALL(glGenProgramsARB(1, &ret));
5718 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
5719 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
5721 if (glGetError() == GL_INVALID_OPERATION) {
5722 GLint pos;
5723 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
5724 FIXME("Fragment program error at position %d: %s\n", pos,
5725 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
5727 shader_buffer_free(&buffer);
5728 return ret;
5731 static void fragment_prog_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5732 IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
5733 struct shader_arb_priv *priv = device->fragment_priv;
5734 BOOL use_pshader = use_ps(stateblock);
5735 BOOL use_vshader = use_vs(stateblock);
5736 struct ffp_frag_settings settings;
5737 const struct arbfp_ffp_desc *desc;
5738 unsigned int i;
5740 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
5742 if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
5743 if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
5744 /* Reload fixed function constants since they collide with the pixel shader constants */
5745 for(i = 0; i < MAX_TEXTURES; i++) {
5746 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
5748 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
5749 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
5750 } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
5751 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
5753 return;
5756 if(!use_pshader) {
5757 /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
5758 gen_ffp_frag_op(stateblock, &settings, FALSE);
5759 desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(&priv->fragment_shaders, &settings);
5760 if(!desc) {
5761 struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
5762 if (!new_desc)
5764 ERR("Out of memory\n");
5765 return;
5767 new_desc->num_textures_used = 0;
5768 for(i = 0; i < GL_LIMITS(texture_stages); i++) {
5769 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
5770 new_desc->num_textures_used = i;
5773 memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
5774 new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
5775 add_ffp_frag_shader(&priv->fragment_shaders, &new_desc->parent);
5776 TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
5777 desc = new_desc;
5780 /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
5781 * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
5782 * deactivate it.
5784 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
5785 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
5786 priv->current_fprogram_id = desc->shader;
5788 if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
5789 /* Reload fixed function constants since they collide with the pixel shader constants */
5790 for(i = 0; i < MAX_TEXTURES; i++) {
5791 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
5793 state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
5794 state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
5796 context->last_was_pshader = FALSE;
5797 } else {
5798 context->last_was_pshader = TRUE;
5801 /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
5802 * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
5803 * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
5804 * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
5805 * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
5807 * Don't call shader_select if the vertex shader is dirty, because it will be called later on by the vertex
5808 * shader handler
5810 if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
5811 device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
5813 if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
5814 device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
5817 if(use_pshader) {
5818 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
5822 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
5823 * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
5824 * state table, so we need to handle that with a forwarding function. The other invisible side effect
5825 * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
5826 * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
5828 static void state_arbfp_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5829 enum fogsource new_source;
5831 TRACE("state %#x, stateblock %p, context %p\n", state, stateblock, context);
5833 if(!isStateDirty(context, STATE_PIXELSHADER)) {
5834 fragment_prog_arbfp(state, stateblock, context);
5837 if(!stateblock->renderState[WINED3DRS_FOGENABLE]) return;
5839 if(stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
5840 if(use_vs(stateblock)) {
5841 new_source = FOGSOURCE_VS;
5842 } else {
5843 if(stateblock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || context->last_was_rhw) {
5844 new_source = FOGSOURCE_COORD;
5845 } else {
5846 new_source = FOGSOURCE_FFP;
5849 } else {
5850 new_source = FOGSOURCE_FFP;
5852 if(new_source != context->fog_source) {
5853 context->fog_source = new_source;
5854 state_fogstartend(STATE_RENDER(WINED3DRS_FOGSTART), stateblock, context);
5858 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
5859 if(!isStateDirty(context, STATE_PIXELSHADER)) {
5860 fragment_prog_arbfp(state, stateblock, context);
5864 #undef GLINFO_LOCATION
5866 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
5867 {STATE_RENDER(WINED3DRS_TEXTUREFACTOR), { STATE_RENDER(WINED3DRS_TEXTUREFACTOR), state_texfactor_arbfp }, WINED3D_GL_EXT_NONE },
5868 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5869 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5870 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5871 {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5872 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5873 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5874 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5875 {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5876 {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5877 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5878 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5879 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5880 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5881 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5882 {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5883 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5884 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5885 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5886 {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5887 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5888 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5889 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5890 {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5891 {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5892 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5893 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5894 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5895 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5896 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5897 {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5898 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5899 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5900 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5901 {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5902 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5903 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5904 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5905 {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5906 {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5907 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5908 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5909 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5910 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5911 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5912 {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5913 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5914 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5915 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5916 {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5917 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5918 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5919 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5920 {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5921 {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5922 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5923 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5924 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5925 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5926 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5927 {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5928 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5929 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5930 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5931 {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5932 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5933 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5934 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5935 {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5936 {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5937 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5938 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5939 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5940 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5941 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5942 {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5943 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5944 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5945 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5946 {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5947 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5948 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5949 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5950 {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5951 {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5952 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5953 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5954 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5955 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5956 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5957 {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5958 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5959 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5960 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5961 {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5962 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5963 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5964 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5965 {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5966 {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5967 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5968 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5969 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5970 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5971 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5972 {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5973 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5974 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5975 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5976 {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5977 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5978 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5979 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5980 {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5981 {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5982 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5983 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5984 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5985 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00), set_bumpmat_arbfp }, WINED3D_GL_EXT_NONE },
5986 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5987 {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET), { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE), tex_bumpenvlum_arbfp }, WINED3D_GL_EXT_NONE },
5988 {STATE_SAMPLER(0), { STATE_SAMPLER(0), sampler_texdim }, WINED3D_GL_EXT_NONE },
5989 {STATE_SAMPLER(1), { STATE_SAMPLER(1), sampler_texdim }, WINED3D_GL_EXT_NONE },
5990 {STATE_SAMPLER(2), { STATE_SAMPLER(2), sampler_texdim }, WINED3D_GL_EXT_NONE },
5991 {STATE_SAMPLER(3), { STATE_SAMPLER(3), sampler_texdim }, WINED3D_GL_EXT_NONE },
5992 {STATE_SAMPLER(4), { STATE_SAMPLER(4), sampler_texdim }, WINED3D_GL_EXT_NONE },
5993 {STATE_SAMPLER(5), { STATE_SAMPLER(5), sampler_texdim }, WINED3D_GL_EXT_NONE },
5994 {STATE_SAMPLER(6), { STATE_SAMPLER(6), sampler_texdim }, WINED3D_GL_EXT_NONE },
5995 {STATE_SAMPLER(7), { STATE_SAMPLER(7), sampler_texdim }, WINED3D_GL_EXT_NONE },
5996 {STATE_PIXELSHADER, { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
5997 {STATE_RENDER(WINED3DRS_FOGENABLE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
5998 {STATE_RENDER(WINED3DRS_FOGTABLEMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
5999 {STATE_RENDER(WINED3DRS_FOGVERTEXMODE), { STATE_RENDER(WINED3DRS_FOGENABLE), state_arbfp_fog }, WINED3D_GL_EXT_NONE },
6000 {STATE_RENDER(WINED3DRS_FOGSTART), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6001 {STATE_RENDER(WINED3DRS_FOGEND), { STATE_RENDER(WINED3DRS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6002 {STATE_RENDER(WINED3DRS_SRGBWRITEENABLE), { STATE_PIXELSHADER, fragment_prog_arbfp }, WINED3D_GL_EXT_NONE },
6003 {STATE_RENDER(WINED3DRS_FOGCOLOR), { STATE_RENDER(WINED3DRS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
6004 {STATE_RENDER(WINED3DRS_FOGDENSITY), { STATE_RENDER(WINED3DRS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
6005 {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6006 {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6007 {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6008 {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6009 {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6010 {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6011 {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6012 {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform }, WINED3D_GL_EXT_NONE },
6013 {STATE_RENDER(WINED3DRS_SPECULARENABLE), { STATE_RENDER(WINED3DRS_SPECULARENABLE), state_arb_specularenable}, WINED3D_GL_EXT_NONE },
6014 {0 /* Terminate */, { 0, 0 }, WINED3D_GL_EXT_NONE },
6017 const struct fragment_pipeline arbfp_fragment_pipeline = {
6018 arbfp_enable,
6019 arbfp_get_caps,
6020 arbfp_alloc,
6021 arbfp_free,
6022 shader_arb_color_fixup_supported,
6023 arbfp_fragmentstate_template,
6024 TRUE /* We can disable projected textures */
6027 #define GLINFO_LOCATION device->adapter->gl_info
6029 struct arbfp_blit_priv {
6030 GLenum yuy2_rect_shader, yuy2_2d_shader;
6031 GLenum uyvy_rect_shader, uyvy_2d_shader;
6032 GLenum yv12_rect_shader, yv12_2d_shader;
6035 static HRESULT arbfp_blit_alloc(IWineD3DDevice *iface) {
6036 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6037 device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
6038 if(!device->blit_priv) {
6039 ERR("Out of memory\n");
6040 return E_OUTOFMEMORY;
6042 return WINED3D_OK;
6045 /* Context activation is done by the caller. */
6046 static void arbfp_blit_free(IWineD3DDevice *iface) {
6047 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6048 struct arbfp_blit_priv *priv = device->blit_priv;
6050 ENTER_GL();
6051 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
6052 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
6053 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
6054 GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
6055 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
6056 GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
6057 checkGLcall("Delete yuv programs\n");
6058 LEAVE_GL();
6061 static BOOL gen_planar_yuv_read(SHADER_BUFFER *buffer, enum yuv_fixup yuv_fixup, GLenum textype, char *luminance)
6063 char chroma;
6064 const char *tex, *texinstr;
6066 if (yuv_fixup == YUV_FIXUP_UYVY) {
6067 chroma = 'x';
6068 *luminance = 'w';
6069 } else {
6070 chroma = 'w';
6071 *luminance = 'x';
6073 switch(textype) {
6074 case GL_TEXTURE_2D: tex = "2D"; texinstr = "TXP"; break;
6075 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; texinstr = "TEX"; break;
6076 default:
6077 /* This is more tricky than just replacing the texture type - we have to navigate
6078 * properly in the texture to find the correct chroma values
6080 FIXME("Implement yuv correction for non-2d, non-rect textures\n");
6081 return FALSE;
6084 /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
6085 * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
6086 * filtering when we sample the texture.
6088 * These are the rules for reading the chroma:
6090 * Even pixel: Cr
6091 * Even pixel: U
6092 * Odd pixel: V
6094 * So we have to get the sampling x position in non-normalized coordinates in integers
6096 if(textype != GL_TEXTURE_RECTANGLE_ARB) {
6097 shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
6098 shader_addline(buffer, "MOV texcrd.w, size.x;\n");
6099 } else {
6100 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6102 /* We must not allow filtering between pixel x and x+1, this would mix U and V
6103 * Vertical filtering is ok. However, bear in mind that the pixel center is at
6104 * 0.5, so add 0.5.
6106 shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
6107 shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
6109 /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
6110 * even and odd pixels respectively
6112 shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
6113 shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
6115 /* Sample Pixel 1 */
6116 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
6118 /* Put the value into either of the chroma values */
6119 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
6120 shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
6121 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
6122 shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
6124 /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
6125 * the pixel right to the current one. Otherwise, sample the left pixel.
6126 * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
6128 shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
6129 shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
6130 shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
6132 /* Put the value into the other chroma */
6133 shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
6134 shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
6135 shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
6136 shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
6138 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
6139 * the current one and lerp the two U and V values
6142 /* This gives the correctly filtered luminance value */
6143 shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
6145 return TRUE;
6148 static BOOL gen_yv12_read(SHADER_BUFFER *buffer, GLenum textype, char *luminance)
6150 const char *tex;
6152 switch(textype) {
6153 case GL_TEXTURE_2D: tex = "2D"; break;
6154 case GL_TEXTURE_RECTANGLE_ARB: tex = "RECT"; break;
6155 default:
6156 FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
6157 return FALSE;
6160 /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
6161 * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
6162 * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
6163 * pitch of the luminance plane, the packing into the gl texture is a bit
6164 * unfortunate. If the whole texture is interpreted as luminance data it looks
6165 * approximately like this:
6167 * +----------------------------------+----
6168 * | |
6169 * | |
6170 * | |
6171 * | |
6172 * | | 2
6173 * | LUMINANCE | -
6174 * | | 3
6175 * | |
6176 * | |
6177 * | |
6178 * | |
6179 * +----------------+-----------------+----
6180 * | | |
6181 * | U even rows | U odd rows |
6182 * | | | 1
6183 * +----------------+------------------ -
6184 * | | | 3
6185 * | V even rows | V odd rows |
6186 * | | |
6187 * +----------------+-----------------+----
6188 * | | |
6189 * | 0.5 | 0.5 |
6191 * So it appears as if there are 4 chroma images, but in fact the odd rows
6192 * in the chroma images are in the same row as the even ones. So its is
6193 * kinda tricky to read
6195 * When reading from rectangle textures, keep in mind that the input y coordinates
6196 * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
6198 shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
6199 2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
6201 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6202 /* the chroma planes have only half the width */
6203 shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
6205 /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
6206 * the coordinate. Also read the right side of the image when reading odd lines
6208 * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
6209 * bleeding
6211 if(textype == GL_TEXTURE_2D) {
6213 shader_addline(buffer, "RCP chroma.w, size.y;\n");
6215 shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
6217 shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
6218 shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
6220 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6221 shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6222 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
6223 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6224 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6226 /* clamp, keep the half pixel origin in mind */
6227 shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
6228 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
6229 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
6230 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6231 } else {
6232 /* Read from [size - size+size/4] */
6233 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
6234 shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
6236 /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
6237 shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
6238 shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
6239 shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
6240 shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
6241 shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
6243 /* Make sure to read exactly from the pixel center */
6244 shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
6245 shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
6247 /* Clamp */
6248 shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
6249 shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
6250 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6251 shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
6252 shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
6254 /* Read the texture, put the result into the output register */
6255 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
6256 shader_addline(buffer, "MOV chroma.x, temp.w;\n");
6258 /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
6259 * No need to clamp because we're just reusing the already clamped value from above
6261 if(textype == GL_TEXTURE_2D) {
6262 shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
6263 } else {
6264 shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
6266 shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
6267 shader_addline(buffer, "MOV chroma.y, temp.w;\n");
6269 /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
6270 * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
6271 * values due to filtering
6273 shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
6274 if(textype == GL_TEXTURE_2D) {
6275 /* Multiply the y coordinate by 2/3 and clamp it */
6276 shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
6277 shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
6278 shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
6279 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
6280 } else {
6281 /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
6282 * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
6283 * is bigger
6285 shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
6286 shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
6287 shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
6289 *luminance = 'a';
6291 return TRUE;
6294 /* Context activation is done by the caller. */
6295 static GLuint gen_yuv_shader(IWineD3DDeviceImpl *device, enum yuv_fixup yuv_fixup, GLenum textype)
6297 GLenum shader;
6298 SHADER_BUFFER buffer;
6299 char luminance_component;
6300 struct arbfp_blit_priv *priv = device->blit_priv;
6302 /* Shader header */
6303 shader_buffer_init(&buffer);
6305 ENTER_GL();
6306 GL_EXTCALL(glGenProgramsARB(1, &shader));
6307 checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
6308 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
6309 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6310 LEAVE_GL();
6311 if(!shader) {
6312 shader_buffer_free(&buffer);
6313 return 0;
6316 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
6317 * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
6318 * two chroma(U and V) values. Each macropixel has two luminance values, one for
6319 * each single pixel it contains, and one U and one V value shared between both
6320 * pixels.
6322 * The data is loaded into an A8L8 texture. With YUY2, the luminance component
6323 * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
6324 * take the format into account when generating the read swizzles
6326 * Reading the Y value is straightforward - just sample the texture. The hardware
6327 * takes care of filtering in the horizontal and vertical direction.
6329 * Reading the U and V values is harder. We have to avoid filtering horizontally,
6330 * because that would mix the U and V values of one pixel or two adjacent pixels.
6331 * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
6332 * regardless of the filtering setting. Vertical filtering works automatically
6333 * though - the U and V values of two rows are mixed nicely.
6335 * Appart of avoiding filtering issues, the code has to know which value it just
6336 * read, and where it can find the other one. To determine this, it checks if
6337 * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
6339 * Handling horizontal filtering of U and V values requires reading a 2nd pair
6340 * of pixels, extracting U and V and mixing them. This is not implemented yet.
6342 * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
6343 * with width / 2. This way one read gives all 3 values, finding U and V is easy
6344 * in an unfiltered situation. Finding the luminance on the other hand requires
6345 * finding out if it is an odd or even pixel. The real drawback of this approach
6346 * is filtering. This would have to be emulated completely in the shader, reading
6347 * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
6348 * vertically. Beyond that it would require adjustments to the texture handling
6349 * code to deal with the width scaling
6351 shader_addline(&buffer, "!!ARBfp1.0\n");
6352 shader_addline(&buffer, "TEMP luminance;\n");
6353 shader_addline(&buffer, "TEMP temp;\n");
6354 shader_addline(&buffer, "TEMP chroma;\n");
6355 shader_addline(&buffer, "TEMP texcrd;\n");
6356 shader_addline(&buffer, "TEMP texcrd2;\n");
6357 shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
6358 shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
6359 shader_addline(&buffer, "PARAM size = program.local[0];\n");
6361 switch (yuv_fixup)
6363 case YUV_FIXUP_UYVY:
6364 case YUV_FIXUP_YUY2:
6365 if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
6367 shader_buffer_free(&buffer);
6368 return 0;
6370 break;
6372 case YUV_FIXUP_YV12:
6373 if (!gen_yv12_read(&buffer, textype, &luminance_component))
6375 shader_buffer_free(&buffer);
6376 return 0;
6378 break;
6380 default:
6381 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
6382 shader_buffer_free(&buffer);
6383 return 0;
6386 /* Calculate the final result. Formula is taken from
6387 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
6388 * ranges from -0.5 to 0.5
6390 shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
6392 shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
6393 shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
6394 shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
6395 shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
6396 shader_addline(&buffer, "END\n");
6398 ENTER_GL();
6399 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
6401 if (glGetError() == GL_INVALID_OPERATION) {
6402 GLint pos;
6403 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
6404 FIXME("Fragment program error at position %d: %s\n", pos,
6405 debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
6407 shader_buffer_free(&buffer);
6408 LEAVE_GL();
6410 switch (yuv_fixup)
6412 case YUV_FIXUP_YUY2:
6413 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
6414 else priv->yuy2_2d_shader = shader;
6415 break;
6417 case YUV_FIXUP_UYVY:
6418 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
6419 else priv->uyvy_2d_shader = shader;
6420 break;
6422 case YUV_FIXUP_YV12:
6423 if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
6424 else priv->yv12_2d_shader = shader;
6425 break;
6428 return shader;
6431 /* Context activation is done by the caller. */
6432 static HRESULT arbfp_blit_set(IWineD3DDevice *iface, const struct GlPixelFormatDesc *format_desc,
6433 GLenum textype, UINT width, UINT height)
6435 GLenum shader;
6436 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6437 float size[4] = {width, height, 1, 1};
6438 struct arbfp_blit_priv *priv = device->blit_priv;
6439 enum yuv_fixup yuv_fixup;
6441 if (!is_yuv_fixup(format_desc->color_fixup))
6443 TRACE("Fixup:\n");
6444 dump_color_fixup_desc(format_desc->color_fixup);
6445 /* Don't bother setting up a shader for unconverted formats */
6446 ENTER_GL();
6447 glEnable(textype);
6448 checkGLcall("glEnable(textype)");
6449 LEAVE_GL();
6450 return WINED3D_OK;
6453 yuv_fixup = get_yuv_fixup(format_desc->color_fixup);
6455 switch(yuv_fixup)
6457 case YUV_FIXUP_YUY2:
6458 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
6459 break;
6461 case YUV_FIXUP_UYVY:
6462 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
6463 break;
6465 case YUV_FIXUP_YV12:
6466 shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
6467 break;
6469 default:
6470 FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup);
6471 ENTER_GL();
6472 glEnable(textype);
6473 checkGLcall("glEnable(textype)");
6474 LEAVE_GL();
6475 return E_NOTIMPL;
6478 if (!shader) shader = gen_yuv_shader(device, yuv_fixup, textype);
6480 ENTER_GL();
6481 glEnable(GL_FRAGMENT_PROGRAM_ARB);
6482 checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
6483 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
6484 checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
6485 GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
6486 checkGLcall("glProgramLocalParameter4fvARB");
6487 LEAVE_GL();
6489 return WINED3D_OK;
6492 /* Context activation is done by the caller. */
6493 static void arbfp_blit_unset(IWineD3DDevice *iface) {
6494 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
6496 ENTER_GL();
6497 glDisable(GL_FRAGMENT_PROGRAM_ARB);
6498 checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
6499 glDisable(GL_TEXTURE_2D);
6500 checkGLcall("glDisable(GL_TEXTURE_2D)");
6501 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
6502 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6503 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6505 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
6506 glDisable(GL_TEXTURE_RECTANGLE_ARB);
6507 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6509 LEAVE_GL();
6512 static BOOL arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup)
6514 enum yuv_fixup yuv_fixup;
6516 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6518 TRACE("Checking support for fixup:\n");
6519 dump_color_fixup_desc(fixup);
6522 if (is_identity_fixup(fixup))
6524 TRACE("[OK]\n");
6525 return TRUE;
6528 /* We only support YUV conversions. */
6529 if (!is_yuv_fixup(fixup))
6531 TRACE("[FAILED]\n");
6532 return FALSE;
6535 yuv_fixup = get_yuv_fixup(fixup);
6536 switch(yuv_fixup)
6538 case YUV_FIXUP_YUY2:
6539 case YUV_FIXUP_UYVY:
6540 case YUV_FIXUP_YV12:
6541 TRACE("[OK]\n");
6542 return TRUE;
6544 default:
6545 FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
6546 TRACE("[FAILED]\n");
6547 return FALSE;
6551 const struct blit_shader arbfp_blit = {
6552 arbfp_blit_alloc,
6553 arbfp_blit_free,
6554 arbfp_blit_set,
6555 arbfp_blit_unset,
6556 arbfp_blit_color_fixup_supported,
6559 #undef GLINFO_LOCATION