wined3d: Add mapping for MOV instructions in shader_hw_map2gl.
[wine] / dlls / wined3d / arb_program_shader.c
1 /*
2  * Pixel and vertex shaders implementation using ARB_vertex_program
3  * and ARB_fragment_program GL extensions.
4  *
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  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include "config.h"
30
31 #include <math.h>
32 #include <stdio.h>
33
34 #include "wined3d_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d);
40
41 #define GLINFO_LOCATION      (*gl_info)
42
43 /* Internally used shader constants. Applications can use constants 0 to GL_LIMITS(vshader_constantsF) - 1,
44  * so upload them above that
45  */
46 #define ARB_SHADER_PRIVCONST_BASE GL_LIMITS(vshader_constantsF)
47 #define ARB_SHADER_PRIVCONST_POS ARB_SHADER_PRIVCONST_BASE + 0
48
49 /* ARB_program_shader private data */
50 struct shader_arb_priv {
51     GLuint                  current_vprogram_id;
52     GLuint                  current_fprogram_id;
53     GLuint                  depth_blt_vprogram_id;
54     GLuint                  depth_blt_fprogram_id[tex_type_count];
55     BOOL                    use_arbfp_fixed_func;
56     struct hash_table_t     *fragment_shaders;
57 };
58
59 /********************************************************
60  * ARB_[vertex/fragment]_program helper functions follow
61  ********************************************************/
62
63 /** 
64  * Loads floating point constants into the currently set ARB_vertex/fragment_program.
65  * When constant_list == NULL, it will load all the constants.
66  *  
67  * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
68  *  or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
69  */
70 static unsigned int shader_arb_load_constantsF(IWineD3DBaseShaderImpl* This, const WineD3D_GL_Info *gl_info,
71         GLuint target_type, unsigned int max_constants, const float *constants, char *dirty_consts)
72 {
73     DWORD shader_version = This->baseShader.reg_maps.shader_version;
74     local_constant* lconst;
75     DWORD i, j;
76     unsigned int ret;
77
78     if (TRACE_ON(d3d_shader)) {
79         for(i = 0; i < max_constants; i++) {
80             if(!dirty_consts[i]) continue;
81             TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
82                         constants[i * 4 + 0], constants[i * 4 + 1],
83                         constants[i * 4 + 2], constants[i * 4 + 3]);
84         }
85     }
86     /* In 1.X pixel shaders constants are implicitly clamped in the range [-1;1] */
87     if (target_type == GL_FRAGMENT_PROGRAM_ARB && WINED3DSHADER_VERSION_MAJOR(shader_version) == 1)
88     {
89         float lcl_const[4];
90         for(i = 0; i < max_constants; i++) {
91             if(!dirty_consts[i]) continue;
92             dirty_consts[i] = 0;
93
94             j = 4 * i;
95             if(constants[j + 0] > 1.0) lcl_const[0] = 1.0;
96             else if(constants[j + 0] < -1.0) lcl_const[0] = -1.0;
97             else lcl_const[0] = constants[j + 0];
98
99             if(constants[j + 1] > 1.0) lcl_const[1] = 1.0;
100             else if(constants[j + 1] < -1.0) lcl_const[1] = -1.0;
101             else lcl_const[1] = constants[j + 1];
102
103             if(constants[j + 2] > 1.0) lcl_const[2] = 1.0;
104             else if(constants[j + 2] < -1.0) lcl_const[2] = -1.0;
105             else lcl_const[2] = constants[j + 2];
106
107             if(constants[j + 3] > 1.0) lcl_const[3] = 1.0;
108             else if(constants[j + 3] < -1.0) lcl_const[3] = -1.0;
109             else lcl_const[3] = constants[j + 3];
110
111             GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, lcl_const));
112         }
113     } else {
114         if(GL_SUPPORT(EXT_GPU_PROGRAM_PARAMETERS)) {
115             /* TODO: Benchmark if we're better of with finding the dirty constants ourselves,
116              * or just reloading *all* constants at once
117              *
118             GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, 0, max_constants, constants));
119              */
120             for(i = 0; i < max_constants; i++) {
121                 if(!dirty_consts[i]) continue;
122
123                 /* Find the next block of dirty constants */
124                 dirty_consts[i] = 0;
125                 j = i;
126                 for(i++; (i < max_constants) && dirty_consts[i]; i++) {
127                     dirty_consts[i] = 0;
128                 }
129
130                 GL_EXTCALL(glProgramEnvParameters4fvEXT(target_type, j, i - j, constants + (j * 4)));
131             }
132         } else {
133             for(i = 0; i < max_constants; i++) {
134                 if(dirty_consts[i]) {
135                     dirty_consts[i] = 0;
136                     GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, constants + (i * 4)));
137                 }
138             }
139         }
140     }
141     checkGLcall("glProgramEnvParameter4fvARB()");
142
143     /* Load immediate constants */
144     if(This->baseShader.load_local_constsF) {
145         if (TRACE_ON(d3d_shader)) {
146             LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
147                 GLfloat* values = (GLfloat*)lconst->value;
148                 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
149                         values[0], values[1], values[2], values[3]);
150             }
151         }
152         /* Immediate constants are clamped for 1.X shaders at loading times */
153         ret = 0;
154         LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
155             dirty_consts[lconst->idx] = 1; /* Dirtify so the non-immediate constant overwrites it next time */
156             ret = max(ret, lconst->idx + 1);
157             GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, lconst->idx, (GLfloat*)lconst->value));
158         }
159         checkGLcall("glProgramEnvParameter4fvARB()");
160         return ret; /* The loaded immediate constants need reloading for the next shader */
161     } else {
162         return 0; /* No constants are dirty now */
163     }
164 }
165
166 /**
167  * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
168  * 
169  * We only support float constants in ARB at the moment, so don't 
170  * worry about the Integers or Booleans
171  */
172 static void shader_arb_load_constants(
173     IWineD3DDevice* device,
174     char usePixelShader,
175     char useVertexShader) {
176    
177     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device; 
178     IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
179     const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
180     unsigned char i;
181
182     if (useVertexShader) {
183         IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
184
185         /* Load DirectX 9 float constants for vertex shader */
186         deviceImpl->highest_dirty_vs_const = shader_arb_load_constantsF(
187                 vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
188                 deviceImpl->highest_dirty_vs_const,
189                 stateBlock->vertexShaderConstantF,
190                 deviceImpl->activeContext->vshader_const_dirty);
191
192         /* Upload the position fixup */
193         GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, ARB_SHADER_PRIVCONST_POS, deviceImpl->posFixup));
194     }
195
196     if (usePixelShader) {
197
198         IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
199         IWineD3DPixelShaderImpl *psi = (IWineD3DPixelShaderImpl *) pshader;
200
201         /* Load DirectX 9 float constants for pixel shader */
202         deviceImpl->highest_dirty_ps_const = shader_arb_load_constantsF(
203                 pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
204                 deviceImpl->highest_dirty_ps_const,
205                 stateBlock->pixelShaderConstantF,
206                 deviceImpl->activeContext->pshader_const_dirty);
207
208         for(i = 0; i < psi->numbumpenvmatconsts; i++) {
209             /* The state manager takes care that this function is always called if the bump env matrix changes
210              */
211             const float *data = (const float *)&stateBlock->textureState[(int) psi->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
212             GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->bumpenvmatconst[i].const_num, data));
213             deviceImpl->activeContext->pshader_const_dirty[psi->bumpenvmatconst[i].const_num] = 1;
214
215             if(psi->luminanceconst[i].const_num != -1) {
216                 /* WINED3DTSS_BUMPENVLSCALE and WINED3DTSS_BUMPENVLOFFSET are next to each other.
217                  * point gl to the scale, and load 4 floats. x = scale, y = offset, z and w are junk, we
218                  * don't care about them. The pointers are valid for sure because the stateblock is bigger.
219                  * (they're WINED3DTSS_TEXTURETRANSFORMFLAGS and WINED3DTSS_ADDRESSW, so most likely 0 or NaN
220                  */
221                 const float *scale = (const float *)&stateBlock->textureState[(int) psi->luminanceconst[i].texunit][WINED3DTSS_BUMPENVLSCALE];
222                 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, psi->luminanceconst[i].const_num, scale));
223                 deviceImpl->activeContext->pshader_const_dirty[psi->luminanceconst[i].const_num] = 1;
224             }
225         }
226     }
227 }
228
229 static void shader_arb_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
230 {
231     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
232
233     /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
234      * context. On a context switch the old context will be fully dirtified */
235     memset(This->activeContext->vshader_const_dirty + start, 1,
236             sizeof(*This->activeContext->vshader_const_dirty) * count);
237     This->highest_dirty_vs_const = max(This->highest_dirty_vs_const, start + count + 1);
238 }
239
240 static void shader_arb_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
241 {
242     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
243
244     /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
245      * context. On a context switch the old context will be fully dirtified */
246     memset(This->activeContext->pshader_const_dirty + start, 1,
247             sizeof(*This->activeContext->pshader_const_dirty) * count);
248     This->highest_dirty_ps_const = max(This->highest_dirty_ps_const, start + count + 1);
249 }
250
251 /* Generate the variable & register declarations for the ARB_vertex_program output target */
252 static void shader_generate_arb_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
253         SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info)
254 {
255     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
256     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
257     DWORD i, cur;
258     char pshader = shader_is_pshader_version(reg_maps->shader_version);
259     unsigned max_constantsF = min(This->baseShader.limits.constant_float, 
260             (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
261     UINT extra_constants_needed = 0;
262     const local_constant *lconst;
263
264     /* Temporary Output register */
265     shader_addline(buffer, "TEMP TMP_OUT;\n");
266
267     for(i = 0; i < This->baseShader.limits.temporary; i++) {
268         if (reg_maps->temporary[i])
269             shader_addline(buffer, "TEMP R%u;\n", i);
270     }
271
272     for (i = 0; i < This->baseShader.limits.address; i++) {
273         if (reg_maps->address[i])
274             shader_addline(buffer, "ADDRESS A%d;\n", i);
275     }
276
277     for(i = 0; i < This->baseShader.limits.texcoord; i++) {
278         if (reg_maps->texcoord[i])
279             shader_addline(buffer,"TEMP T%u;\n", i);
280     }
281
282     /* Texture coordinate registers must be pre-loaded */
283     for (i = 0; i < This->baseShader.limits.texcoord; i++) {
284         if (reg_maps->texcoord[i])
285             shader_addline(buffer, "MOV T%u, fragment.texcoord[%u];\n", i, i);
286     }
287
288     for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
289         IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) This;
290         if(!reg_maps->bumpmat[i]) continue;
291
292         cur = ps->numbumpenvmatconsts;
293         ps->bumpenvmatconst[cur].const_num = -1;
294         ps->bumpenvmatconst[cur].texunit = i;
295         ps->luminanceconst[cur].const_num = -1;
296         ps->luminanceconst[cur].texunit = i;
297
298         /* If the shader does not use all available constants, use the next free constant to load the bump mapping environment matrix from
299          * the stateblock into the shader. If no constant is available don't load, texbem will then just sample the texture without applying
300          * bump mapping.
301          */
302         if(max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF)) {
303             ps->bumpenvmatconst[cur].const_num = max_constantsF + extra_constants_needed;
304             shader_addline(buffer, "PARAM bumpenvmat%d = program.env[%d];\n",
305                            i, ps->bumpenvmatconst[cur].const_num);
306             extra_constants_needed++;
307
308             if(reg_maps->luminanceparams && max_constantsF + extra_constants_needed < GL_LIMITS(pshader_constantsF)) {
309                 ((IWineD3DPixelShaderImpl *)This)->luminanceconst[cur].const_num = max_constantsF + extra_constants_needed;
310                 shader_addline(buffer, "PARAM luminance%d = program.env[%d];\n",
311                                i, ps->luminanceconst[cur].const_num);
312                 extra_constants_needed++;
313             } else if(reg_maps->luminanceparams) {
314                 FIXME("No free constant to load the luminance parameters\n");
315             }
316         } else {
317             FIXME("No free constant found to load environment bump mapping matrix into the shader. texbem instruction will not apply bump mapping\n");
318         }
319
320         ps->numbumpenvmatconsts = cur + 1;
321     }
322
323     if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] && pshader) {
324         shader_addline(buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
325                         srgb_mul_low, srgb_mul_low, srgb_mul_low);
326         shader_addline(buffer, "PARAM srgb_comparison =  {%f, %f, %f, %f};\n",
327                         srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
328         shader_addline(buffer, "PARAM srgb_pow =  {%f, %f, %f, 1.0};\n",
329                        srgb_pow, srgb_pow, srgb_pow);
330         shader_addline(buffer, "PARAM srgb_mul_hi =  {%f, %f, %f, 1.0};\n",
331                        srgb_mul_high, srgb_mul_high, srgb_mul_high);
332         shader_addline(buffer, "PARAM srgb_sub_hi =  {%f, %f, %f, 0.0};\n",
333                        srgb_sub_high, srgb_sub_high, srgb_sub_high);
334     }
335
336     /* Load local constants using the program-local space,
337      * this avoids reloading them each time the shader is used
338      */
339     if(!This->baseShader.load_local_constsF) {
340         LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
341             shader_addline(buffer, "PARAM C%u = program.local[%u];\n", lconst->idx,
342                            lconst->idx);
343         }
344     }
345
346     /* we use the array-based constants array if the local constants are marked for loading,
347      * because then we use indirect addressing, or when the local constant list is empty,
348      * because then we don't know if we're using indirect addressing or not. If we're hardcoding
349      * local constants do not declare the loaded constants as an array because ARB compilers usually
350      * do not optimize unused constants away
351      */
352     if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
353         /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
354         shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
355                     max_constantsF, max_constantsF - 1);
356     } else {
357         for(i = 0; i < max_constantsF; i++) {
358             if(!shader_constant_is_local(This, i)) {
359                 shader_addline(buffer, "PARAM C%d = program.env[%d];\n",i, i);
360             }
361         }
362     }
363 }
364
365 static const char * const shift_tab[] = {
366     "dummy",     /*  0 (none) */
367     "coefmul.x", /*  1 (x2)   */
368     "coefmul.y", /*  2 (x4)   */
369     "coefmul.z", /*  3 (x8)   */
370     "coefmul.w", /*  4 (x16)  */
371     "dummy",     /*  5 (x32)  */
372     "dummy",     /*  6 (x64)  */
373     "dummy",     /*  7 (x128) */
374     "dummy",     /*  8 (d256) */
375     "dummy",     /*  9 (d128) */
376     "dummy",     /* 10 (d64)  */
377     "dummy",     /* 11 (d32)  */
378     "coefdiv.w", /* 12 (d16)  */
379     "coefdiv.z", /* 13 (d8)   */
380     "coefdiv.y", /* 14 (d4)   */
381     "coefdiv.x"  /* 15 (d2)   */
382 };
383
384 static void shader_arb_get_write_mask(const SHADER_OPCODE_ARG *arg, const DWORD param, char *write_mask)
385 {
386     char *ptr = write_mask;
387     char vshader = shader_is_vshader_version(arg->reg_maps->shader_version);
388
389     if(vshader && shader_get_regtype(param) == WINED3DSPR_ADDR) {
390         *ptr++ = '.';
391         *ptr++ = 'x';
392     } else if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
393         *ptr++ = '.';
394         if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
395         if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
396         if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
397         if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
398     }
399
400     *ptr = '\0';
401 }
402
403 static void shader_arb_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
404     /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
405      * but addressed as "rgba". To fix this we need to swap the register's x
406      * and z components. */
407     const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
408     char *ptr = swizzle_str;
409
410     /* swizzle bits fields: wwzzyyxx */
411     DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
412     DWORD swizzle_x = swizzle & 0x03;
413     DWORD swizzle_y = (swizzle >> 2) & 0x03;
414     DWORD swizzle_z = (swizzle >> 4) & 0x03;
415     DWORD swizzle_w = (swizzle >> 6) & 0x03;
416
417     /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
418      * generate a swizzle string. Unless we need to our own swizzling. */
419     if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
420         *ptr++ = '.';
421         if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
422             *ptr++ = swizzle_chars[swizzle_x];
423         } else {
424             *ptr++ = swizzle_chars[swizzle_x];
425             *ptr++ = swizzle_chars[swizzle_y];
426             *ptr++ = swizzle_chars[swizzle_z];
427             *ptr++ = swizzle_chars[swizzle_w];
428         }
429     }
430
431     *ptr = '\0';
432 }
433
434 static void pshader_get_register_name(IWineD3DBaseShader* iface,
435     const DWORD param, char* regstr) {
436
437     DWORD reg = param & WINED3DSP_REGNUM_MASK;
438     DWORD regtype = shader_get_regtype(param);
439     IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
440
441     switch (regtype) {
442     case WINED3DSPR_TEMP:
443         sprintf(regstr, "R%u", reg);
444     break;
445     case WINED3DSPR_INPUT:
446         if (reg==0) {
447             strcpy(regstr, "fragment.color.primary");
448         } else {
449             strcpy(regstr, "fragment.color.secondary");
450         }
451     break;
452     case WINED3DSPR_CONST:
453         if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
454             sprintf(regstr, "C[%u]", reg);
455         } else {
456             sprintf(regstr, "C%u", reg);
457         }
458     break;
459     case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
460         sprintf(regstr,"T%u", reg);
461     break;
462     case WINED3DSPR_COLOROUT:
463         if (reg == 0)
464             sprintf(regstr, "TMP_COLOR");
465         else {
466             /* TODO: See GL_ARB_draw_buffers */
467             FIXME("Unsupported write to render target %u\n", reg);
468             sprintf(regstr, "unsupported_register");
469         }
470     break;
471     case WINED3DSPR_DEPTHOUT:
472         sprintf(regstr, "result.depth");
473     break;
474     case WINED3DSPR_ATTROUT:
475         sprintf(regstr, "oD[%u]", reg);
476     break;
477     case WINED3DSPR_TEXCRDOUT:
478         sprintf(regstr, "oT[%u]", reg);
479     break;
480     default:
481         FIXME("Unhandled register name Type(%d)\n", regtype);
482         sprintf(regstr, "unrecognized_register");
483     break;
484     }
485 }
486
487 /* TODO: merge with pixel shader */
488 static void vshader_program_add_param(const SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine)
489 {
490   IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
491
492   /* oPos, oFog and oPts in D3D */
493   static const char * const hwrastout_reg_names[] = { "TMP_OUT", "result.fogcoord", "result.pointsize" };
494
495   DWORD reg = param & WINED3DSP_REGNUM_MASK;
496   DWORD regtype = shader_get_regtype(param);
497   char  tmpReg[255];
498   BOOL is_color = FALSE;
499
500   if ((param & WINED3DSP_SRCMOD_MASK) == WINED3DSPSM_NEG) {
501       strcat(hwLine, " -");
502   } else {
503       strcat(hwLine, " ");
504   }
505
506   switch (regtype) {
507   case WINED3DSPR_TEMP:
508     sprintf(tmpReg, "R%u", reg);
509     strcat(hwLine, tmpReg);
510     break;
511   case WINED3DSPR_INPUT:
512
513     if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
514         is_color = TRUE;
515
516     sprintf(tmpReg, "vertex.attrib[%u]", reg);
517     strcat(hwLine, tmpReg);
518     break;
519   case WINED3DSPR_CONST:
520       if(param & WINED3DSHADER_ADDRMODE_RELATIVE) {
521           if(reg >= This->rel_offset) {
522               sprintf(tmpReg, "C[A0.x + %u]", reg - This->rel_offset);
523           } else {
524               sprintf(tmpReg, "C[A0.x - %u]", -reg + This->rel_offset);
525           }
526       } else {
527           if(This->baseShader.load_local_constsF || list_empty(&This->baseShader.constantsF)) {
528               sprintf(tmpReg, "C[%u]", reg);
529           } else {
530               sprintf(tmpReg, "C%u", reg);
531           }
532       }
533     strcat(hwLine, tmpReg);
534     break;
535   case WINED3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
536     sprintf(tmpReg, "A%u", reg);
537     strcat(hwLine, tmpReg);
538     break;
539   case WINED3DSPR_RASTOUT:
540     sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
541     strcat(hwLine, tmpReg);
542     break;
543   case WINED3DSPR_ATTROUT:
544     if (reg==0) {
545        strcat(hwLine, "result.color.primary");
546     } else {
547        strcat(hwLine, "result.color.secondary");
548     }
549     break;
550   case WINED3DSPR_TEXCRDOUT:
551     sprintf(tmpReg, "result.texcoord[%u]", reg);
552     strcat(hwLine, tmpReg);
553     break;
554   default:
555     FIXME("Unknown reg type %d %d\n", regtype, reg);
556     strcat(hwLine, "unrecognized_register");
557     break;
558   }
559
560   if (!is_input) {
561     char write_mask[6];
562     shader_arb_get_write_mask(arg, param, write_mask);
563     strcat(hwLine, write_mask);
564   } else {
565     char swizzle[6];
566     shader_arb_get_swizzle(param, is_color, swizzle);
567     strcat(hwLine, swizzle);
568   }
569 }
570
571 static void shader_hw_sample(const SHADER_OPCODE_ARG *arg, DWORD sampler_idx, const char *dst_str,
572         const char *coord_reg, BOOL projected, BOOL bias)
573 {
574     SHADER_BUFFER* buffer = arg->buffer;
575     DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
576     const char *tex_type;
577
578     switch(sampler_type) {
579         case WINED3DSTT_1D:
580             tex_type = "1D";
581             break;
582
583         case WINED3DSTT_2D:
584         {
585             IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) arg->shader;
586             IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
587             if(device->stateBlock->textures[sampler_idx] &&
588                IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
589                 tex_type = "RECT";
590             } else {
591                 tex_type = "2D";
592             }
593             break;
594         }
595
596         case WINED3DSTT_VOLUME:
597             tex_type = "3D";
598             break;
599
600         case WINED3DSTT_CUBE:
601             tex_type = "CUBE";
602             break;
603
604         default:
605             ERR("Unexpected texture type %d\n", sampler_type);
606             tex_type = "";
607     }
608
609     if (bias) {
610         /* Shouldn't be possible, but let's check for it */
611         if(projected) FIXME("Biased and Projected texture sampling\n");
612         /* TXB takes the 4th component of the source vector automatically, as d3d. Nothing more to do */
613         shader_addline(buffer, "TXB %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
614     } else if (projected) {
615         shader_addline(buffer, "TXP %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
616     } else {
617         shader_addline(buffer, "TEX %s, %s, texture[%u], %s;\n", dst_str, coord_reg, sampler_idx, tex_type);
618     }
619 }
620
621 static const char *shader_arb_get_fixup_swizzle(enum fixup_channel_source channel_source)
622 {
623     switch(channel_source)
624     {
625         case CHANNEL_SOURCE_ZERO: return "0";
626         case CHANNEL_SOURCE_ONE: return "1";
627         case CHANNEL_SOURCE_X: return "x";
628         case CHANNEL_SOURCE_Y: return "y";
629         case CHANNEL_SOURCE_Z: return "z";
630         case CHANNEL_SOURCE_W: return "w";
631         default:
632             FIXME("Unhandled channel source %#x\n", channel_source);
633             return "undefined";
634     }
635 }
636
637 static void gen_color_correction(SHADER_BUFFER *buffer, const char *reg, DWORD dst_mask,
638         const char *one, const char *two, struct color_fixup_desc fixup)
639 {
640     DWORD mask;
641
642     if (is_yuv_fixup(fixup))
643     {
644         enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
645         FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
646         return;
647     }
648
649     mask = 0;
650     if (fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
651     if (fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
652     if (fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
653     if (fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
654     mask &= dst_mask;
655
656     if (mask)
657     {
658         shader_addline(buffer, "SWZ %s, %s, %s, %s, %s, %s;\n", reg, reg,
659                 shader_arb_get_fixup_swizzle(fixup.x_source), shader_arb_get_fixup_swizzle(fixup.y_source),
660                 shader_arb_get_fixup_swizzle(fixup.z_source), shader_arb_get_fixup_swizzle(fixup.w_source));
661     }
662
663     mask = 0;
664     if (fixup.x_sign_fixup) mask |= WINED3DSP_WRITEMASK_0;
665     if (fixup.y_sign_fixup) mask |= WINED3DSP_WRITEMASK_1;
666     if (fixup.z_sign_fixup) mask |= WINED3DSP_WRITEMASK_2;
667     if (fixup.w_sign_fixup) mask |= WINED3DSP_WRITEMASK_3;
668     mask &= dst_mask;
669
670     if (mask)
671     {
672         char reg_mask[6];
673         char *ptr = reg_mask;
674
675         if (mask != WINED3DSP_WRITEMASK_ALL)
676         {
677             *ptr++ = '.';
678             if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
679             if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
680             if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
681             if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
682         }
683         *ptr = '\0';
684
685         shader_addline(buffer, "MAD %s%s, %s, %s, -%s;\n", reg, reg_mask, reg, two, one);
686     }
687 }
688
689 static void shader_arb_color_correction(const struct SHADER_OPCODE_ARG* arg, struct color_fixup_desc fixup)
690 {
691     char reg[256];
692     pshader_get_register_name(arg->shader, arg->dst, reg);
693     gen_color_correction(arg->buffer, reg, arg->dst & WINED3DSP_WRITEMASK_ALL, "one", "coefmul.x", fixup);
694 }
695
696 static void pshader_gen_input_modifier_line (
697     IWineD3DBaseShader *iface,
698     SHADER_BUFFER* buffer,
699     const DWORD instr,
700     int tmpreg,
701     char *outregstr) {
702
703     /* Generate a line that does the input modifier computation and return the input register to use */
704     char regstr[256];
705     char swzstr[20];
706     int insert_line;
707
708     /* Assume a new line will be added */
709     insert_line = 1;
710
711     /* Get register name */
712     pshader_get_register_name(iface, instr, regstr);
713     shader_arb_get_swizzle(instr, FALSE, swzstr);
714
715     switch (instr & WINED3DSP_SRCMOD_MASK) {
716     case WINED3DSPSM_NONE:
717         sprintf(outregstr, "%s%s", regstr, swzstr);
718         insert_line = 0;
719         break;
720     case WINED3DSPSM_NEG:
721         sprintf(outregstr, "-%s%s", regstr, swzstr);
722         insert_line = 0;
723         break;
724     case WINED3DSPSM_BIAS:
725         shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
726         break;
727     case WINED3DSPSM_BIASNEG:
728         shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
729         break;
730     case WINED3DSPSM_SIGN:
731         shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
732         break;
733     case WINED3DSPSM_SIGNNEG:
734         shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
735         break;
736     case WINED3DSPSM_COMP:
737         shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
738         break;
739     case WINED3DSPSM_X2:
740         shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
741         break;
742     case WINED3DSPSM_X2NEG:
743         shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
744         break;
745     case WINED3DSPSM_DZ:
746         shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
747         shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
748         break;
749     case WINED3DSPSM_DW:
750         shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
751         shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
752         break;
753     default:
754         sprintf(outregstr, "%s%s", regstr, swzstr);
755         insert_line = 0;
756     }
757
758     /* Return modified or original register, with swizzle */
759     if (insert_line)
760         sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
761 }
762
763 static inline void pshader_gen_output_modifier_line(SHADER_BUFFER *buffer, int saturate, const char *write_mask,
764         int shift, const char *regstr)
765 {
766     /* Generate a line that does the output modifier computation */
767     shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
768         regstr, write_mask, regstr, shift_tab[shift]);
769 }
770
771 static void pshader_hw_bem(const SHADER_OPCODE_ARG *arg)
772 {
773     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
774
775     SHADER_BUFFER* buffer = arg->buffer;
776     char dst_name[50];
777     char src_name[2][50];
778     char dst_wmask[20];
779     DWORD sampler_code = arg->dst & WINED3DSP_REGNUM_MASK;
780     BOOL has_bumpmat = FALSE;
781     int i;
782
783     for(i = 0; i < This->numbumpenvmatconsts; i++) {
784         if(This->bumpenvmatconst[i].const_num != -1 && This->bumpenvmatconst[i].texunit == sampler_code) {
785             has_bumpmat = TRUE;
786             break;
787         }
788     }
789
790     pshader_get_register_name(arg->shader, arg->dst, dst_name);
791     shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
792     strcat(dst_name, dst_wmask);
793
794     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
795     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
796
797     if(has_bumpmat) {
798         /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
799         shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", sampler_code);
800         shader_addline(buffer, "DP3 TMP.r, TMP2, %s;\n", src_name[1]);
801         shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", sampler_code);
802         shader_addline(buffer, "DP3 TMP.g, TMP2, %s;\n", src_name[1]);
803
804         shader_addline(buffer, "ADD %s, %s, TMP;\n", dst_name, src_name[0]);
805     } else {
806         shader_addline(buffer, "MOV %s, %s;\n", dst_name, src_name[0]);
807     }
808 }
809
810 static void pshader_hw_cnd(const SHADER_OPCODE_ARG *arg)
811 {
812     SHADER_BUFFER* buffer = arg->buffer;
813     char dst_wmask[20];
814     char dst_name[50];
815     char src_name[3][50];
816     BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
817     DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
818
819     /* FIXME: support output modifiers */
820
821     /* Handle output register */
822     pshader_get_register_name(arg->shader, arg->dst, dst_name);
823     shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
824
825     /* Generate input register names (with modifiers) */
826     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
827     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
828     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
829
830     /* The coissue flag changes the semantic of the cnd instruction in <= 1.3 shaders */
831     if (arg->reg_maps->shader_version <= WINED3DPS_VERSION(1, 3) && arg->opcode_token & WINED3DSI_COISSUE)
832     {
833         shader_addline(buffer, "MOV%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1]);
834     } else {
835         shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
836         shader_addline(buffer, "CMP%s %s%s, TMP, %s, %s;\n",
837                                 sat ? "_SAT" : "", dst_name, dst_wmask, src_name[1], src_name[2]);
838     }
839     if (shift != 0)
840         pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
841 }
842
843 static void pshader_hw_cmp(const SHADER_OPCODE_ARG *arg)
844 {
845     SHADER_BUFFER* buffer = arg->buffer;
846     char dst_wmask[20];
847     char dst_name[50];
848     char src_name[3][50];
849     DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
850     BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
851
852     /* FIXME: support output modifiers */
853
854     /* Handle output register */
855     pshader_get_register_name(arg->shader, arg->dst, dst_name);
856     shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
857
858     /* Generate input register names (with modifiers) */
859     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
860     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
861     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
862
863     shader_addline(buffer, "CMP%s %s%s, %s, %s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
864                    src_name[0], src_name[2], src_name[1]);
865
866     if (shift != 0)
867         pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
868 }
869
870 /** Process the WINED3DSIO_DP2ADD instruction in ARB.
871  * dst = dot2(src0, src1) + src2 */
872 static void pshader_hw_dp2add(const SHADER_OPCODE_ARG *arg)
873 {
874     SHADER_BUFFER* buffer = arg->buffer;
875     char dst_wmask[20];
876     char dst_name[50];
877     char src_name[3][50];
878     DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
879     BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
880
881     pshader_get_register_name(arg->shader, arg->dst, dst_name);
882     shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
883
884     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name[0]);
885     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[1], 1, src_name[1]);
886     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[2], 2, src_name[2]);
887
888     /* Emulate a DP2 with a DP3 and 0.0 */
889     shader_addline(buffer, "MOV TMP, %s;\n", src_name[0]);
890     shader_addline(buffer, "MOV TMP.z, 0.0;\n");
891     shader_addline(buffer, "DP3 TMP2, TMP, %s;\n", src_name[1]);
892     shader_addline(buffer, "ADD%s %s%s, TMP2, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask, src_name[2]);
893
894     if (shift != 0)
895         pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
896 }
897
898 /* Map the opcode 1-to-1 to the GL code */
899 static void shader_hw_map2gl(const SHADER_OPCODE_ARG *arg)
900 {
901     CONST SHADER_OPCODE* curOpcode = arg->opcode;
902     SHADER_BUFFER* buffer = arg->buffer;
903     DWORD dst = arg->dst;
904     const DWORD *src = arg->src;
905     const char *instruction;
906     char arguments[256];
907     unsigned int i;
908
909     switch (curOpcode->opcode)
910     {
911         case WINED3DSIO_ABS: instruction = "ABS"; break;
912         case WINED3DSIO_ADD: instruction = "ADD"; break;
913         case WINED3DSIO_CRS: instruction = "XPD"; break;
914         case WINED3DSIO_DP3: instruction = "DP3"; break;
915         case WINED3DSIO_DP4: instruction = "DP4"; break;
916         case WINED3DSIO_DST: instruction = "DST"; break;
917         case WINED3DSIO_EXP: instruction = "EX2"; break;
918         case WINED3DSIO_EXPP: instruction = "EXP"; break;
919         case WINED3DSIO_FRC: instruction = "FRC"; break;
920         case WINED3DSIO_LIT: instruction = "LIT"; break;
921         case WINED3DSIO_LOG: instruction = "LG2"; break;
922         case WINED3DSIO_LOGP: instruction = "LOG"; break;
923         case WINED3DSIO_LRP: instruction = "LRP"; break;
924         case WINED3DSIO_MAD: instruction = "MAD"; break;
925         case WINED3DSIO_MAX: instruction = "MAX"; break;
926         case WINED3DSIO_MIN: instruction = "MIN"; break;
927         case WINED3DSIO_MOV: instruction = "MOV"; break;
928         case WINED3DSIO_MUL: instruction = "MUL"; break;
929         case WINED3DSIO_NOP: instruction = "NOP"; break;
930         case WINED3DSIO_POW: instruction = "POW"; break;
931         case WINED3DSIO_SGE: instruction = "SGE"; break;
932         case WINED3DSIO_SLT: instruction = "SLT"; break;
933         case WINED3DSIO_SUB: instruction = "SUB"; break;
934         default: instruction = "";
935             FIXME("Unhandled opcode %s\n", curOpcode->name);
936             break;
937     }
938
939     if (shader_is_pshader_version(arg->reg_maps->shader_version))
940     {
941         /* Output token related */
942         char output_rname[256];
943         char output_wmask[20];
944         char operands[4][100];
945         BOOL saturate = FALSE;
946         BOOL centroid = FALSE;
947         BOOL partialprecision = FALSE;
948         const char *modifier;
949         DWORD shift;
950
951         if (!curOpcode->num_params)
952         {
953             ERR("Opcode \"%s\" has no parameters\n", curOpcode->name);
954             return;
955         }
956
957         /* Process modifiers */
958         if (dst & WINED3DSP_DSTMOD_MASK)
959         {
960             DWORD mask = dst & WINED3DSP_DSTMOD_MASK;
961
962             saturate = mask & WINED3DSPDM_SATURATE;
963             centroid = mask & WINED3DSPDM_MSAMPCENTROID;
964             partialprecision = mask & WINED3DSPDM_PARTIALPRECISION;
965             mask &= ~(WINED3DSPDM_MSAMPCENTROID | WINED3DSPDM_PARTIALPRECISION | WINED3DSPDM_SATURATE);
966             if (mask)
967                 FIXME("Unrecognized modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
968
969             if (centroid)
970                 FIXME("Unhandled modifier(%#x)\n", mask >> WINED3DSP_DSTMOD_SHIFT);
971         }
972         shift = (dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
973         modifier = (saturate && !shift) ? "_SAT" : "";
974
975         /* Generate input register names (with modifiers) */
976         for (i = 1; i < curOpcode->num_params; ++i)
977             pshader_gen_input_modifier_line(arg->shader, buffer, src[i-1], i-1, operands[i]);
978
979         /* Handle output register */
980         pshader_get_register_name(arg->shader, dst, output_rname);
981         strcpy(operands[0], output_rname);
982         shader_arb_get_write_mask(arg, dst, output_wmask);
983         strcat(operands[0], output_wmask);
984
985         arguments[0] = '\0';
986         strcat(arguments, operands[0]);
987         for (i = 1; i < curOpcode->num_params; i++)
988         {
989             strcat(arguments, ", ");
990             strcat(arguments, operands[i]);
991         }
992         shader_addline(buffer, "%s%s %s;\n", instruction, modifier, arguments);
993
994         /* A shift requires another line. */
995         if (shift) pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
996     } else {
997         /* Note that vshader_program_add_param() adds spaces. */
998
999         arguments[0] = '\0';
1000         if (curOpcode->num_params > 0)
1001         {
1002             vshader_program_add_param(arg, dst, FALSE, arguments);
1003             for (i = 1; i < curOpcode->num_params; ++i)
1004             {
1005                 strcat(arguments, ",");
1006                 vshader_program_add_param(arg, src[i-1], TRUE, arguments);
1007             }
1008         }
1009         shader_addline(buffer, "%s%s;\n", instruction, arguments);
1010     }
1011 }
1012
1013 static void shader_hw_mov(const SHADER_OPCODE_ARG *arg)
1014 {
1015     IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl*)arg->shader;
1016
1017     if ((WINED3DSHADER_VERSION_MAJOR(arg->reg_maps->shader_version) == 1
1018             && !shader_is_pshader_version(arg->reg_maps->shader_version)
1019             && shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)
1020             || arg->opcode->opcode == WINED3DSIO_MOVA)
1021     {
1022         SHADER_BUFFER *buffer = arg->buffer;
1023         char src0_param[256];
1024
1025         if (arg->opcode->opcode == WINED3DSIO_MOVA)
1026             FIXME("mova should round\n");
1027
1028         src0_param[0] = '\0';
1029         if (((IWineD3DVertexShaderImpl *)shader)->rel_offset)
1030         {
1031             vshader_program_add_param(arg, arg->src[0], TRUE, src0_param);
1032             shader_addline(buffer, "ADD TMP.x, %s, helper_const.z;\n", src0_param);
1033             shader_addline(buffer, "ARL A0.x, TMP.x;\n");
1034         }
1035         else
1036         {
1037             /* Apple's ARB_vertex_program implementation does not accept an ARL source argument
1038              * with more than one component. Thus replicate the first source argument over all
1039              * 4 components. For example, .xyzw -> .x (or better: .xxxx), .zwxy -> .z, etc)
1040              */
1041             DWORD parm = arg->src[0] & ~(WINED3DVS_SWIZZLE_MASK);
1042             if((arg->src[0] & WINED3DVS_X_W) == WINED3DVS_X_W)
1043                 parm |= WINED3DVS_X_W | WINED3DVS_Y_W | WINED3DVS_Z_W | WINED3DVS_W_W;
1044             else if((arg->src[0] & WINED3DVS_X_Z) == WINED3DVS_X_Z)
1045                 parm |= WINED3DVS_X_Z | WINED3DVS_Y_Z | WINED3DVS_Z_Z | WINED3DVS_W_Z;
1046             else if((arg->src[0] & WINED3DVS_X_Y) == WINED3DVS_X_Y)
1047                 parm |= WINED3DVS_X_Y | WINED3DVS_Y_Y | WINED3DVS_Z_Y | WINED3DVS_W_Y;
1048             else if((arg->src[0] & WINED3DVS_X_X) == WINED3DVS_X_X)
1049                 parm |= WINED3DVS_X_X | WINED3DVS_Y_X | WINED3DVS_Z_X | WINED3DVS_W_X;
1050             vshader_program_add_param(arg, parm, TRUE, src0_param);
1051             shader_addline(buffer, "ARL A0.x, %s;\n", src0_param);
1052         }
1053     }
1054     else
1055     {
1056         shader_hw_map2gl(arg);
1057     }
1058 }
1059
1060 static void pshader_hw_texkill(const SHADER_OPCODE_ARG *arg)
1061 {
1062     DWORD shader_version = arg->reg_maps->shader_version;
1063     SHADER_BUFFER* buffer = arg->buffer;
1064     char reg_dest[40];
1065
1066     /* No swizzles are allowed in d3d's texkill. PS 1.x ignores the 4th component as documented,
1067      * but >= 2.0 honors it(undocumented, but tested by the d3d9 testsuit)
1068      */
1069     pshader_get_register_name(arg->shader, arg->dst, reg_dest);
1070
1071     if (shader_version >= WINED3DPS_VERSION(2,0))
1072     {
1073         /* The arb backend doesn't claim ps 2.0 support, but try to eat what the app feeds to us */
1074         shader_addline(buffer, "KIL %s;\n", reg_dest);
1075     } else {
1076         /* ARB fp doesn't like swizzles on the parameter of the KIL instruction. To mask the 4th component,
1077          * copy the register into our general purpose TMP variable, overwrite .w and pass TMP to KIL
1078          */
1079         shader_addline(buffer, "MOV TMP, %s;\n", reg_dest);
1080         shader_addline(buffer, "MOV TMP.w, one.w;\n");
1081         shader_addline(buffer, "KIL TMP;\n");
1082     }
1083 }
1084
1085 static void pshader_hw_tex(const SHADER_OPCODE_ARG *arg)
1086 {
1087     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1088     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1089
1090     DWORD dst = arg->dst;
1091     const DWORD *src = arg->src;
1092     SHADER_BUFFER* buffer = arg->buffer;
1093     DWORD shader_version = arg->reg_maps->shader_version;
1094     BOOL projected = FALSE, bias = FALSE;
1095
1096     char reg_dest[40];
1097     char reg_coord[40];
1098     DWORD reg_dest_code;
1099     DWORD reg_sampler_code;
1100
1101     /* All versions have a destination register */
1102     reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
1103     pshader_get_register_name(arg->shader, dst, reg_dest);
1104
1105     /* 1.0-1.3: Use destination register as coordinate source.
1106        1.4+: Use provided coordinate source register. */
1107    if (shader_version < WINED3DPS_VERSION(1,4))
1108       strcpy(reg_coord, reg_dest);
1109    else
1110       pshader_gen_input_modifier_line(arg->shader, buffer, src[0], 0, reg_coord);
1111
1112   /* 1.0-1.4: Use destination register number as texture code.
1113      2.0+: Use provided sampler number as texure code. */
1114   if (shader_version < WINED3DPS_VERSION(2,0))
1115      reg_sampler_code = reg_dest_code;
1116   else
1117      reg_sampler_code = src[1] & WINED3DSP_REGNUM_MASK;
1118
1119   /* projection flag:
1120    * 1.1, 1.2, 1.3: Use WINED3DTSS_TEXTURETRANSFORMFLAGS
1121    * 1.4: Use WINED3DSPSM_DZ or WINED3DSPSM_DW on src[0]
1122    * 2.0+: Use WINED3DSI_TEXLD_PROJECT on the opcode
1123    */
1124   if (shader_version < WINED3DPS_VERSION(1,4))
1125   {
1126       DWORD flags = 0;
1127       if(reg_sampler_code < MAX_TEXTURES) {
1128         flags = deviceImpl->stateBlock->textureState[reg_sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1129       }
1130       if (flags & WINED3DTTFF_PROJECTED) {
1131           projected = TRUE;
1132       }
1133   }
1134   else if (shader_version < WINED3DPS_VERSION(2,0))
1135   {
1136       DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1137       if (src_mod == WINED3DSPSM_DZ) {
1138           projected = TRUE;
1139       } else if(src_mod == WINED3DSPSM_DW) {
1140           projected = TRUE;
1141       }
1142   } else {
1143       if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1144           projected = TRUE;
1145       }
1146       if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
1147           bias = TRUE;
1148       }
1149   }
1150   shader_hw_sample(arg, reg_sampler_code, reg_dest, reg_coord, projected, bias);
1151 }
1152
1153 static void pshader_hw_texcoord(const SHADER_OPCODE_ARG *arg)
1154 {
1155     DWORD dst = arg->dst;
1156     SHADER_BUFFER* buffer = arg->buffer;
1157
1158     char tmp[20];
1159     shader_arb_get_write_mask(arg, dst, tmp);
1160     if (arg->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
1161     {
1162         DWORD reg = dst & WINED3DSP_REGNUM_MASK;
1163         shader_addline(buffer, "MOV_SAT T%u%s, fragment.texcoord[%u];\n", reg, tmp, reg);
1164     } else {
1165         DWORD reg1 = dst & WINED3DSP_REGNUM_MASK;
1166         char reg_src[40];
1167
1168         pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, reg_src);
1169         shader_addline(buffer, "MOV R%u%s, %s;\n", reg1, tmp, reg_src);
1170    }
1171 }
1172
1173 static void pshader_hw_texreg2ar(const SHADER_OPCODE_ARG *arg)
1174 {
1175      SHADER_BUFFER* buffer = arg->buffer;
1176      IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1177      IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1178      DWORD flags;
1179
1180      DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1181      char dst_str[8];
1182      char src_str[50];
1183
1184      sprintf(dst_str, "T%u", reg1);
1185      pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1186      shader_addline(buffer, "MOV TMP.x, %s.w;\n", src_str);
1187      shader_addline(buffer, "MOV TMP.y, %s.x;\n", src_str);
1188      flags = reg1 < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg1][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1189      shader_hw_sample(arg, reg1, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1190 }
1191
1192 static void pshader_hw_texreg2gb(const SHADER_OPCODE_ARG *arg)
1193 {
1194      SHADER_BUFFER* buffer = arg->buffer;
1195
1196      DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1197      char dst_str[8];
1198      char src_str[50];
1199
1200      sprintf(dst_str, "T%u", reg1);
1201      pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1202      shader_addline(buffer, "MOV TMP.x, %s.y;\n", src_str);
1203      shader_addline(buffer, "MOV TMP.y, %s.z;\n", src_str);
1204      shader_hw_sample(arg, reg1, dst_str, "TMP", FALSE, FALSE);
1205 }
1206
1207 static void pshader_hw_texreg2rgb(const SHADER_OPCODE_ARG *arg)
1208 {
1209     SHADER_BUFFER* buffer = arg->buffer;
1210     DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1211     char dst_str[8];
1212     char src_str[50];
1213
1214     sprintf(dst_str, "T%u", reg1);
1215     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_str);
1216     shader_hw_sample(arg, reg1, dst_str, src_str, FALSE, FALSE);
1217 }
1218
1219 static void pshader_hw_texbem(const SHADER_OPCODE_ARG *arg)
1220 {
1221     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1222     BOOL has_bumpmat = FALSE;
1223     BOOL has_luminance = FALSE;
1224     int i;
1225
1226     DWORD dst = arg->dst;
1227     DWORD src = arg->src[0] & WINED3DSP_REGNUM_MASK;
1228     SHADER_BUFFER* buffer = arg->buffer;
1229
1230     char reg_coord[40];
1231     DWORD reg_dest_code;
1232
1233     /* All versions have a destination register */
1234     reg_dest_code = dst & WINED3DSP_REGNUM_MASK;
1235     /* Can directly use the name because texbem is only valid for <= 1.3 shaders */
1236     pshader_get_register_name(arg->shader, dst, reg_coord);
1237
1238     for(i = 0; i < This->numbumpenvmatconsts; i++) {
1239         if(This->bumpenvmatconst[i].const_num != -1 && reg_dest_code == This->bumpenvmatconst[i].texunit) {
1240             has_bumpmat = TRUE;
1241             break;
1242         }
1243     }
1244     for(i = 0; i < This->numbumpenvmatconsts; i++) {
1245         if(This->luminanceconst[i].const_num != -1 && reg_dest_code == This->luminanceconst[i].texunit) {
1246             has_luminance = TRUE;
1247             break;
1248         }
1249     }
1250
1251     if(has_bumpmat) {
1252         /* Sampling the perturbation map in Tsrc was done already, including the signedness correction if needed */
1253
1254         shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, x, z, 0, 0;\n", reg_dest_code);
1255         shader_addline(buffer, "DP3 TMP.x, TMP2, T%u;\n", src);
1256         shader_addline(buffer, "SWZ TMP2, bumpenvmat%d, y, w, 0, 0;\n", reg_dest_code);
1257         shader_addline(buffer, "DP3 TMP.y, TMP2, T%u;\n", src);
1258
1259         /* with projective textures, texbem only divides the static texture coord, not the displacement,
1260          * so we can't let the GL handle this.
1261          */
1262         if (((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS]
1263               & WINED3DTTFF_PROJECTED) {
1264             shader_addline(buffer, "RCP TMP2.w, %s.w;\n", reg_coord);
1265             shader_addline(buffer, "MUL TMP2.xy, %s, TMP2.w;\n", reg_coord);
1266             shader_addline(buffer, "ADD TMP.xy, TMP, TMP2;\n");
1267         } else {
1268             shader_addline(buffer, "ADD TMP.xy, TMP, %s;\n", reg_coord);
1269         }
1270
1271         shader_hw_sample(arg, reg_dest_code, reg_coord, "TMP", FALSE, FALSE);
1272
1273         if(arg->opcode->opcode == WINED3DSIO_TEXBEML && has_luminance) {
1274             shader_addline(buffer, "MAD TMP, T%u.z, luminance%d.x, luminance%d.y;\n",
1275                            src, reg_dest_code, reg_dest_code);
1276             shader_addline(buffer, "MUL %s, %s, TMP;\n", reg_coord, reg_coord);
1277         }
1278
1279     } else {
1280         DWORD tf;
1281         if(reg_dest_code < MAX_TEXTURES) {
1282             tf = ((IWineD3DDeviceImpl*) This->baseShader.device)->stateBlock->textureState[reg_dest_code][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1283         } else {
1284             tf = 0;
1285         }
1286         /* Without a bump matrix loaded, just sample with the unmodified coordinates */
1287         shader_hw_sample(arg, reg_dest_code, reg_coord, reg_coord, tf & WINED3DTTFF_PROJECTED, FALSE);
1288     }
1289 }
1290
1291 static void pshader_hw_texm3x2pad(const SHADER_OPCODE_ARG *arg)
1292 {
1293     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1294     SHADER_BUFFER* buffer = arg->buffer;
1295     char src0_name[50];
1296
1297     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1298     shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", reg, src0_name);
1299 }
1300
1301 static void pshader_hw_texm3x2tex(const SHADER_OPCODE_ARG *arg)
1302 {
1303     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1304     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1305     DWORD flags;
1306     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1307     SHADER_BUFFER* buffer = arg->buffer;
1308     char dst_str[8];
1309     char src0_name[50];
1310
1311     sprintf(dst_str, "T%u", reg);
1312     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1313     shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", reg, src0_name);
1314     flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1315     shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1316 }
1317
1318 static void pshader_hw_texm3x3pad(const SHADER_OPCODE_ARG *arg)
1319 {
1320     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1321     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1322     SHADER_BUFFER* buffer = arg->buffer;
1323     SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1324     char src0_name[50];
1325
1326     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1327     shader_addline(buffer, "DP3 TMP.%c, T%u, %s;\n", 'x' + current_state->current_row, reg, src0_name);
1328     current_state->texcoord_w[current_state->current_row++] = reg;
1329 }
1330
1331 static void pshader_hw_texm3x3tex(const SHADER_OPCODE_ARG *arg)
1332 {
1333     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1334     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1335     DWORD flags;
1336     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1337     SHADER_BUFFER* buffer = arg->buffer;
1338     SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1339     char dst_str[8];
1340     char src0_name[50];
1341
1342     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1343     shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1344
1345     /* Sample the texture using the calculated coordinates */
1346     sprintf(dst_str, "T%u", reg);
1347     flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1348     shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1349     current_state->current_row = 0;
1350 }
1351
1352 static void pshader_hw_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
1353 {
1354     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1355     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1356     DWORD flags;
1357     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1358     SHADER_BUFFER* buffer = arg->buffer;
1359     SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1360     char dst_str[8];
1361     char src0_name[50];
1362
1363     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1364     shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1365
1366     /* Construct the eye-ray vector from w coordinates */
1367     shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%u].w;\n", current_state->texcoord_w[0]);
1368     shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%u].w;\n", current_state->texcoord_w[1]);
1369     shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%u].w;\n", reg);
1370
1371     /* Calculate reflection vector
1372      */
1373     shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
1374     /* The .w is ignored when sampling, so I can use TMP2.w to calculate dot(N, N) */
1375     shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1376     shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1377     shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1378     shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1379     shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
1380
1381     /* Sample the texture using the calculated coordinates */
1382     sprintf(dst_str, "T%u", reg);
1383     flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1384     shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1385     current_state->current_row = 0;
1386 }
1387
1388 static void pshader_hw_texm3x3spec(const SHADER_OPCODE_ARG *arg)
1389 {
1390     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1391     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1392     DWORD flags;
1393     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1394     DWORD reg3 = arg->src[1] & WINED3DSP_REGNUM_MASK;
1395     SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1396     SHADER_BUFFER* buffer = arg->buffer;
1397     char dst_str[8];
1398     char src0_name[50];
1399
1400     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0_name);
1401     shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", reg, src0_name);
1402
1403     /* Calculate reflection vector.
1404      *
1405      *               dot(N, E)
1406      * TMP.xyz = 2 * --------- * N - E
1407      *               dot(N, N)
1408      *
1409      * Which normalizes the normal vector
1410      */
1411     shader_addline(buffer, "DP3 TMP.w, TMP, C[%u];\n", reg3);
1412     shader_addline(buffer, "DP3 TMP2.w, TMP, TMP;\n");
1413     shader_addline(buffer, "RCP TMP2.w, TMP2.w;\n");
1414     shader_addline(buffer, "MUL TMP.w, TMP.w, TMP2.w;\n");
1415     shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
1416     shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%u];\n", reg3);
1417
1418     /* Sample the texture using the calculated coordinates */
1419     sprintf(dst_str, "T%u", reg);
1420     flags = reg < MAX_TEXTURES ? deviceImpl->stateBlock->textureState[reg][WINED3DTSS_TEXTURETRANSFORMFLAGS] : 0;
1421     shader_hw_sample(arg, reg, dst_str, "TMP", flags & WINED3DTTFF_PROJECTED, FALSE);
1422     current_state->current_row = 0;
1423 }
1424
1425 static void pshader_hw_texdepth(const SHADER_OPCODE_ARG *arg)
1426 {
1427     SHADER_BUFFER* buffer = arg->buffer;
1428     char dst_name[50];
1429
1430     /* texdepth has an implicit destination, the fragment depth value. It's only parameter,
1431      * which is essentially an input, is the destination register because it is the first
1432      * parameter. According to the msdn, this must be register r5, but let's keep it more flexible
1433      * here
1434      */
1435     pshader_get_register_name(arg->shader, arg->dst, dst_name);
1436
1437     /* According to the msdn, the source register(must be r5) is unusable after
1438      * the texdepth instruction, so we're free to modify it
1439      */
1440     shader_addline(buffer, "MIN %s.y, %s.y, one.y;\n", dst_name, dst_name);
1441
1442     /* How to deal with the special case dst_name.g == 0? if r != 0, then
1443      * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1444      * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1445      */
1446     shader_addline(buffer, "RCP %s.y, %s.y;\n", dst_name, dst_name);
1447     shader_addline(buffer, "MUL TMP.x, %s.x, %s.y;\n", dst_name, dst_name);
1448     shader_addline(buffer, "MIN TMP.x, TMP.x, one.x;\n");
1449     shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1450 }
1451
1452 /** Process the WINED3DSIO_TEXDP3TEX instruction in ARB:
1453  * Take a 3-component dot product of the TexCoord[dstreg] and src,
1454  * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1455 static void pshader_hw_texdp3tex(const SHADER_OPCODE_ARG *arg)
1456 {
1457     SHADER_BUFFER* buffer = arg->buffer;
1458     DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1459     char src0[50];
1460     char dst_str[8];
1461
1462     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1463     shader_addline(buffer, "MOV TMP, 0.0;\n");
1464     shader_addline(buffer, "DP3 TMP.x, T%u, %s;\n", sampler_idx, src0);
1465
1466     sprintf(dst_str, "T%u", sampler_idx);
1467     shader_hw_sample(arg, sampler_idx, dst_str, "TMP", FALSE /* Only one coord, can't be projected */, FALSE);
1468 }
1469
1470 /** Process the WINED3DSIO_TEXDP3 instruction in ARB:
1471  * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1472 static void pshader_hw_texdp3(const SHADER_OPCODE_ARG *arg)
1473 {
1474     char src0[50];
1475     char dst_str[50];
1476     char dst_mask[6];
1477     DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1478     SHADER_BUFFER* buffer = arg->buffer;
1479
1480     /* Handle output register */
1481     pshader_get_register_name(arg->shader, arg->dst, dst_str);
1482     shader_arb_get_write_mask(arg, arg->dst, dst_mask);
1483
1484     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1485     shader_addline(buffer, "DP3 %s%s, T%u, %s;\n", dst_str, dst_mask, dstreg, src0);
1486
1487     /* TODO: Handle output modifiers */
1488 }
1489
1490 /** Process the WINED3DSIO_TEXM3X3 instruction in ARB
1491  * Perform the 3rd row of a 3x3 matrix multiply */
1492 static void pshader_hw_texm3x3(const SHADER_OPCODE_ARG *arg)
1493 {
1494     SHADER_BUFFER* buffer = arg->buffer;
1495     char dst_str[50];
1496     char dst_mask[6];
1497     char src0[50];
1498     DWORD dst_reg = arg->dst & WINED3DSP_REGNUM_MASK;
1499
1500     pshader_get_register_name(arg->shader, arg->dst, dst_str);
1501     shader_arb_get_write_mask(arg, arg->dst, dst_mask);
1502
1503     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1504     shader_addline(buffer, "DP3 TMP.z, T%u, %s;\n", dst_reg, src0);
1505     shader_addline(buffer, "MOV %s%s, TMP;\n", dst_str, dst_mask);
1506
1507     /* TODO: Handle output modifiers */
1508 }
1509
1510 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in ARB:
1511  * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1512  * Calculate tmp0.y = TexCoord[dstreg] . src.xyz;  (tmp0.x has already been calculated)
1513  * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1514  */
1515 static void pshader_hw_texm3x2depth(const SHADER_OPCODE_ARG *arg)
1516 {
1517     SHADER_BUFFER* buffer = arg->buffer;
1518     DWORD dst_reg = arg->dst & WINED3DSP_REGNUM_MASK;
1519     char src0[50];
1520
1521     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src0);
1522     shader_addline(buffer, "DP3 TMP.y, T%u, %s;\n", dst_reg, src0);
1523
1524     /* How to deal with the special case dst_name.g == 0? if r != 0, then
1525      * the r * (1 / 0) will give infinity, which is clamped to 1.0, the correct
1526      * result. But if r = 0.0, then 0 * inf = 0, which is incorrect.
1527      */
1528     shader_addline(buffer, "RCP TMP.y, TMP.y;\n");
1529     shader_addline(buffer, "MUL TMP.x, TMP.x, TMP.y;\n");
1530     shader_addline(buffer, "MIN TMP.x, TMP.x, one.x;\n");
1531     shader_addline(buffer, "MAX result.depth, TMP.x, 0.0;\n");
1532 }
1533
1534 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1535     Vertex/Pixel shaders to ARB_vertex_program codes */
1536 static void shader_hw_mnxn(const SHADER_OPCODE_ARG *arg)
1537 {
1538     int i;
1539     int nComponents = 0;
1540     SHADER_OPCODE_ARG tmpArg;
1541     IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1542     const SHADER_OPCODE *opcode_table = shader->baseShader.shader_ins;
1543     DWORD shader_version = arg->reg_maps->shader_version;
1544
1545     memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1546
1547     /* Set constants for the temporary argument */
1548     tmpArg.shader      = arg->shader;
1549     tmpArg.buffer      = arg->buffer;
1550     tmpArg.src[0]      = arg->src[0];
1551     tmpArg.src_addr[0] = arg->src_addr[0];
1552     tmpArg.src_addr[1] = arg->src_addr[1];
1553     tmpArg.reg_maps = arg->reg_maps;
1554
1555     switch(arg->opcode->opcode) {
1556     case WINED3DSIO_M4x4:
1557         nComponents = 4;
1558         tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1559         break;
1560     case WINED3DSIO_M4x3:
1561         nComponents = 3;
1562         tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1563         break;
1564     case WINED3DSIO_M3x4:
1565         nComponents = 4;
1566         tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1567         break;
1568     case WINED3DSIO_M3x3:
1569         nComponents = 3;
1570         tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1571         break;
1572     case WINED3DSIO_M3x2:
1573         nComponents = 2;
1574         tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1575         break;
1576     default:
1577         break;
1578     }
1579
1580     for (i = 0; i < nComponents; i++) {
1581         tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1582         tmpArg.src[1] = arg->src[1]+i;
1583         shader_hw_map2gl(&tmpArg);
1584     }
1585 }
1586
1587 static void vshader_hw_rsq_rcp(const SHADER_OPCODE_ARG *arg)
1588 {
1589     CONST SHADER_OPCODE* curOpcode = arg->opcode;
1590     SHADER_BUFFER* buffer = arg->buffer;
1591     DWORD dst = arg->dst;
1592     DWORD src = arg->src[0];
1593     DWORD swizzle = (src & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1594     const char *instruction;
1595
1596     char tmpLine[256];
1597
1598     switch(curOpcode->opcode)
1599     {
1600         case WINED3DSIO_RSQ: instruction = "RSQ"; break;
1601         case WINED3DSIO_RCP: instruction = "RCP"; break;
1602         default: instruction = "";
1603             FIXME("Unhandled opcode %s\n", curOpcode->name);
1604             break;
1605     }
1606
1607     strcpy(tmpLine, instruction);
1608     vshader_program_add_param(arg, dst, FALSE, tmpLine); /* Destination */
1609     strcat(tmpLine, ",");
1610     vshader_program_add_param(arg, src, TRUE, tmpLine);
1611     if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) == swizzle) {
1612         /* Dx sdk says .x is used if no swizzle is given, but our test shows that
1613          * .w is used
1614          */
1615         strcat(tmpLine, ".w");
1616     }
1617
1618     shader_addline(buffer, "%s;\n", tmpLine);
1619 }
1620
1621 static void shader_hw_nrm(const SHADER_OPCODE_ARG *arg)
1622 {
1623     SHADER_BUFFER* buffer = arg->buffer;
1624     char dst_name[50];
1625     char src_name[50];
1626     char dst_wmask[20];
1627     DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1628     BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1629
1630     pshader_get_register_name(arg->shader, arg->dst, dst_name);
1631     shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1632
1633     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name);
1634     shader_addline(buffer, "DP3 TMP, %s, %s;\n", src_name, src_name);
1635     shader_addline(buffer, "RSQ TMP, TMP.x;\n");
1636     /* dst.w = src[0].w * 1 / (src.x^2 + src.y^2 + src.z^2)^(1/2) according to msdn*/
1637     shader_addline(buffer, "MUL%s %s%s, %s, TMP;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1638                    src_name);
1639
1640     if (shift != 0)
1641         pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1642 }
1643
1644 static void shader_hw_sincos(const SHADER_OPCODE_ARG *arg)
1645 {
1646     /* This instruction exists in ARB, but the d3d instruction takes two extra parameters which
1647      * must contain fixed constants. So we need a separate function to filter those constants and
1648      * can't use map2gl
1649      */
1650     SHADER_BUFFER* buffer = arg->buffer;
1651     char dst_name[50];
1652     char src_name[50];
1653     char dst_wmask[20];
1654     DWORD shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1655     BOOL sat = (arg->dst & WINED3DSP_DSTMOD_MASK) & WINED3DSPDM_SATURATE;
1656
1657     pshader_get_register_name(arg->shader, arg->dst, dst_name);
1658     shader_arb_get_write_mask(arg, arg->dst, dst_wmask);
1659
1660     pshader_gen_input_modifier_line(arg->shader, buffer, arg->src[0], 0, src_name);
1661     shader_addline(buffer, "SCS%s %s%s, %s;\n", sat ? "_SAT" : "", dst_name, dst_wmask,
1662                    src_name);
1663
1664     if (shift != 0)
1665         pshader_gen_output_modifier_line(buffer, FALSE, dst_wmask, shift, dst_name);
1666
1667 }
1668
1669 static GLuint create_arb_blt_vertex_program(const WineD3D_GL_Info *gl_info)
1670 {
1671     GLuint program_id = 0;
1672     const char *blt_vprogram =
1673         "!!ARBvp1.0\n"
1674         "PARAM c[1] = { { 1, 0.5 } };\n"
1675         "MOV result.position, vertex.position;\n"
1676         "MOV result.color, c[0].x;\n"
1677         "MOV result.texcoord[0], vertex.texcoord[0];\n"
1678         "END\n";
1679
1680     GL_EXTCALL(glGenProgramsARB(1, &program_id));
1681     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, program_id));
1682     GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_vprogram), blt_vprogram));
1683
1684     if (glGetError() == GL_INVALID_OPERATION) {
1685         GLint pos;
1686         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1687         FIXME("Vertex program error at position %d: %s\n", pos,
1688             debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1689     }
1690
1691     return program_id;
1692 }
1693
1694 static GLuint create_arb_blt_fragment_program(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
1695 {
1696     GLuint program_id = 0;
1697     static const char * const blt_fprograms[tex_type_count] =
1698     {
1699         /* tex_1d */
1700         NULL,
1701         /* tex_2d */
1702         "!!ARBfp1.0\n"
1703         "TEMP R0;\n"
1704         "TEX R0.x, fragment.texcoord[0], texture[0], 2D;\n"
1705         "MOV result.depth.z, R0.x;\n"
1706         "END\n",
1707         /* tex_3d */
1708         NULL,
1709         /* tex_cube */
1710         "!!ARBfp1.0\n"
1711         "TEMP R0;\n"
1712         "TEX R0.x, fragment.texcoord[0], texture[0], CUBE;\n"
1713         "MOV result.depth.z, R0.x;\n"
1714         "END\n",
1715         /* tex_rect */
1716         "!!ARBfp1.0\n"
1717         "TEMP R0;\n"
1718         "TEX R0.x, fragment.texcoord[0], texture[0], RECT;\n"
1719         "MOV result.depth.z, R0.x;\n"
1720         "END\n",
1721     };
1722
1723     if (!blt_fprograms[tex_type])
1724     {
1725         FIXME("tex_type %#x not supported\n", tex_type);
1726         tex_type = tex_2d;
1727     }
1728
1729     GL_EXTCALL(glGenProgramsARB(1, &program_id));
1730     GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id));
1731     GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(blt_fprograms[tex_type]), blt_fprograms[tex_type]));
1732
1733     if (glGetError() == GL_INVALID_OPERATION) {
1734         GLint pos;
1735         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
1736         FIXME("Fragment program error at position %d: %s\n", pos,
1737             debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
1738     }
1739
1740     return program_id;
1741 }
1742
1743 static void shader_arb_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1744     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1745     struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1746     const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1747
1748     if (useVS) {
1749         TRACE("Using vertex shader\n");
1750         IWineD3DVertexShaderImpl_CompileShader(This->stateBlock->vertexShader);
1751
1752         priv->current_vprogram_id = ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->prgId;
1753
1754         /* Bind the vertex program */
1755         GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1756         checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1757
1758         /* Enable OpenGL vertex programs */
1759         glEnable(GL_VERTEX_PROGRAM_ARB);
1760         checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1761         TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1762     } else if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
1763         priv->current_vprogram_id = 0;
1764         glDisable(GL_VERTEX_PROGRAM_ARB);
1765         checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1766     }
1767
1768     if (usePS) {
1769         struct ps_compile_args compile_args;
1770         TRACE("Using pixel shader\n");
1771         find_ps_compile_args((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader, This->stateBlock, &compile_args);
1772         priv->current_fprogram_id = find_gl_pshader((IWineD3DPixelShaderImpl *) This->stateBlock->pixelShader,
1773                                                     &compile_args);
1774
1775         /* Bind the fragment program */
1776         GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1777         checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1778
1779         if(!priv->use_arbfp_fixed_func) {
1780             /* Enable OpenGL fragment programs */
1781             glEnable(GL_FRAGMENT_PROGRAM_ARB);
1782             checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1783         }
1784         TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1785     } else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && !priv->use_arbfp_fixed_func) {
1786         /* Disable only if we're not using arbfp fixed function fragment processing. If this is used,
1787          * keep GL_FRAGMENT_PROGRAM_ARB enabled, and the fixed function pipeline will bind the fixed function
1788          * replacement shader
1789          */
1790         glDisable(GL_FRAGMENT_PROGRAM_ARB);
1791         checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1792         priv->current_fprogram_id = 0;
1793     }
1794 }
1795
1796 static void shader_arb_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
1797     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1798     struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1799     GLuint *blt_fprogram = &priv->depth_blt_fprogram_id[tex_type];
1800     const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1801
1802     if (!priv->depth_blt_vprogram_id) priv->depth_blt_vprogram_id = create_arb_blt_vertex_program(gl_info);
1803     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->depth_blt_vprogram_id));
1804     glEnable(GL_VERTEX_PROGRAM_ARB);
1805
1806     if (!*blt_fprogram) *blt_fprogram = create_arb_blt_fragment_program(gl_info, tex_type);
1807     GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, *blt_fprogram));
1808     glEnable(GL_FRAGMENT_PROGRAM_ARB);
1809 }
1810
1811 static void shader_arb_deselect_depth_blt(IWineD3DDevice *iface) {
1812     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1813     struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1814     const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1815
1816     if (priv->current_vprogram_id) {
1817         GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, priv->current_vprogram_id));
1818         checkGLcall("glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexShader->prgId);");
1819
1820         glEnable(GL_VERTEX_PROGRAM_ARB);
1821         checkGLcall("glEnable(GL_VERTEX_PROGRAM_ARB);");
1822
1823         TRACE("(%p) : Bound vertex program %u and enabled GL_VERTEX_PROGRAM_ARB\n", This, priv->current_vprogram_id);
1824     } else {
1825         glDisable(GL_VERTEX_PROGRAM_ARB);
1826         checkGLcall("glDisable(GL_VERTEX_PROGRAM_ARB)");
1827     }
1828
1829     if (priv->current_fprogram_id) {
1830         GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, priv->current_fprogram_id));
1831         checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pixelShader->prgId);");
1832
1833         glEnable(GL_FRAGMENT_PROGRAM_ARB);
1834         checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB);");
1835
1836         TRACE("(%p) : Bound fragment program %u and enabled GL_FRAGMENT_PROGRAM_ARB\n", This, priv->current_fprogram_id);
1837     } else {
1838         glDisable(GL_FRAGMENT_PROGRAM_ARB);
1839         checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
1840     }
1841 }
1842
1843 static void shader_arb_destroy(IWineD3DBaseShader *iface) {
1844     IWineD3DBaseShaderImpl *baseShader = (IWineD3DBaseShaderImpl *) iface;
1845     const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)baseShader->baseShader.device)->adapter->gl_info;
1846
1847     if (shader_is_pshader_version(baseShader->baseShader.reg_maps.shader_version))
1848     {
1849         IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
1850         UINT i;
1851
1852         ENTER_GL();
1853         for(i = 0; i < This->num_gl_shaders; i++) {
1854             GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId));
1855             checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->gl_shaders[i].prgId))");
1856         }
1857         LEAVE_GL();
1858         HeapFree(GetProcessHeap(), 0, This->gl_shaders);
1859         This->gl_shaders = NULL;
1860         This->num_gl_shaders = 0;
1861     } else {
1862         IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *) iface;
1863
1864         ENTER_GL();
1865         GL_EXTCALL(glDeleteProgramsARB(1, &This->prgId));
1866         checkGLcall("GL_EXTCALL(glDeleteProgramsARB(1, &This->prgId))");
1867         ((IWineD3DVertexShaderImpl *) This)->prgId = 0;
1868         LEAVE_GL();
1869     }
1870     baseShader->baseShader.is_compiled = FALSE;
1871 }
1872
1873 static HRESULT shader_arb_alloc(IWineD3DDevice *iface) {
1874     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1875     This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
1876     return WINED3D_OK;
1877 }
1878
1879 static void shader_arb_free(IWineD3DDevice *iface) {
1880     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1881     const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
1882     struct shader_arb_priv *priv = (struct shader_arb_priv *) This->shader_priv;
1883     int i;
1884
1885     if(priv->depth_blt_vprogram_id) {
1886         GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_vprogram_id));
1887     }
1888     for (i = 0; i < tex_type_count; ++i) {
1889         if (priv->depth_blt_fprogram_id[i]) {
1890             GL_EXTCALL(glDeleteProgramsARB(1, &priv->depth_blt_fprogram_id[i]));
1891         }
1892     }
1893
1894     HeapFree(GetProcessHeap(), 0, This->shader_priv);
1895 }
1896
1897 static BOOL shader_arb_dirty_const(IWineD3DDevice *iface) {
1898     return TRUE;
1899 }
1900
1901 static void arbfp_add_sRGB_correction(SHADER_BUFFER *buffer, const char *fragcolor, const char *tmp1,
1902                                       const char *tmp2, const char *tmp3, const char *tmp4) {
1903     /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
1904
1905     /* Calculate the > 0.0031308 case */
1906     shader_addline(buffer, "POW %s.x, %s.x, srgb_pow.x;\n", tmp1, fragcolor);
1907     shader_addline(buffer, "POW %s.y, %s.y, srgb_pow.y;\n", tmp1, fragcolor);
1908     shader_addline(buffer, "POW %s.z, %s.z, srgb_pow.z;\n", tmp1, fragcolor);
1909     shader_addline(buffer, "MUL %s, %s, srgb_mul_hi;\n", tmp1, tmp1);
1910     shader_addline(buffer, "SUB %s, %s, srgb_sub_hi;\n", tmp1, tmp1);
1911     /* Calculate the < case */
1912     shader_addline(buffer, "MUL %s, srgb_mul_low, %s;\n", tmp2, fragcolor);
1913     /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
1914     shader_addline(buffer, "SLT %s, srgb_comparison, %s;\n", tmp3, fragcolor);
1915     shader_addline(buffer, "SGE %s, srgb_comparison, %s;\n", tmp4, fragcolor);
1916     /* Store the components > 0.0031308 in the destination */
1917     shader_addline(buffer, "MUL %s, %s, %s;\n", fragcolor, tmp1, tmp3);
1918     /* Add the components that are < 0.0031308 */
1919     shader_addline(buffer, "MAD result.color.xyz, %s, %s, %s;\n", tmp2, tmp4, fragcolor);
1920     /* [0.0;1.0] clamping. Not needed, this is done implicitly */
1921 }
1922
1923 static GLuint shader_arb_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
1924     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
1925     const shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
1926     CONST DWORD *function = This->baseShader.function;
1927     DWORD shader_version = reg_maps->shader_version;
1928     const char *fragcolor;
1929     const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
1930     const local_constant *lconst;
1931     GLuint retval;
1932
1933     /*  Create the hw ARB shader */
1934     shader_addline(buffer, "!!ARBfp1.0\n");
1935
1936     shader_addline(buffer, "TEMP TMP;\n");     /* Used in matrix ops */
1937     shader_addline(buffer, "TEMP TMP2;\n");    /* Used in matrix ops */
1938     shader_addline(buffer, "TEMP TA;\n");      /* Used for modifiers */
1939     shader_addline(buffer, "TEMP TB;\n");      /* Used for modifiers */
1940     shader_addline(buffer, "TEMP TC;\n");      /* Used for modifiers */
1941     shader_addline(buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
1942     shader_addline(buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
1943     shader_addline(buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
1944
1945     /* Base Declarations */
1946     shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
1947
1948     /* We need two variables for fog blending */
1949     if(args->fog != FOG_OFF) shader_addline(buffer, "TEMP TMP_FOG;\n");
1950     if (shader_version >= WINED3DPS_VERSION(2,0)) shader_addline(buffer, "TEMP TMP_COLOR;\n");
1951
1952     /* Base Shader Body */
1953     shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
1954
1955     if (shader_version < WINED3DPS_VERSION(2,0)) {
1956         fragcolor = "R0";
1957     } else {
1958         fragcolor = "TMP_COLOR";
1959     }
1960     if(args->srgb_correction) {
1961         arbfp_add_sRGB_correction(buffer, fragcolor, "TMP", "TMP2", "TA", "TB");
1962     }
1963     if (shader_version < WINED3DPS_VERSION(3,0)) {
1964         /* calculate fog and blend it
1965          * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
1966          * -1/(e-s) and e/(e-s) respectively.
1967          */
1968         switch(args->fog) {
1969             case FOG_OFF:
1970                 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
1971                 break;
1972             case FOG_LINEAR:
1973                 shader_addline(buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
1974                 shader_addline(buffer, "LRP result.color.xyz, TMP_FOG.x, %s, state.fog.color;\n", fragcolor);
1975                 shader_addline(buffer, "MOV result.color.w, %s.w;\n", fragcolor);
1976                 break;
1977             case FOG_EXP:
1978                 FIXME("Implement EXP fog in ARB\n");
1979                 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
1980                 break;
1981             case FOG_EXP2:
1982                 FIXME("Implement EXP2 fog in ARB\n");
1983                 shader_addline(buffer, "MOV result.color, %s;\n", fragcolor);
1984                 break;
1985         }
1986     }
1987
1988     shader_addline(buffer, "END\n");
1989
1990     /* TODO: change to resource.glObjectHandle or something like that */
1991     GL_EXTCALL(glGenProgramsARB(1, &retval));
1992
1993     TRACE("Creating a hw pixel shader, prg=%d\n", retval);
1994     GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, retval));
1995
1996     TRACE("Created hw pixel shader, prg=%d\n", retval);
1997     /* Create the program and check for errors */
1998     GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1999                buffer->bsize, buffer->buffer));
2000
2001     if (glGetError() == GL_INVALID_OPERATION) {
2002         GLint errPos;
2003         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2004         FIXME("HW PixelShader Error at position %d: %s\n",
2005               errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2006         retval = 0;
2007     }
2008
2009     /* Load immediate constants */
2010     if(!This->baseShader.load_local_constsF) {
2011         LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2012             const float *value = (const float *)lconst->value;
2013             GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, lconst->idx, value));
2014             checkGLcall("glProgramLocalParameter4fvARB");
2015         }
2016     }
2017
2018     return retval;
2019 }
2020
2021 static void shader_arb_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
2022     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
2023     const shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
2024     CONST DWORD *function = This->baseShader.function;
2025     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
2026     const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
2027     const local_constant *lconst;
2028
2029     /*  Create the hw ARB shader */
2030     shader_addline(buffer, "!!ARBvp1.0\n");
2031     shader_addline(buffer, "PARAM helper_const = { 2.0, -1.0, %d.0, 0.0 };\n", This->rel_offset);
2032
2033     /* Mesa supports only 95 constants */
2034     if (GL_VEND(MESA) || GL_VEND(WINE))
2035         This->baseShader.limits.constant_float =
2036                 min(95, This->baseShader.limits.constant_float);
2037
2038     shader_addline(buffer, "TEMP TMP;\n");
2039
2040     /* Base Declarations */
2041     shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
2042
2043     /* We need a constant to fixup the final position */
2044     shader_addline(buffer, "PARAM posFixup = program.env[%d];\n", ARB_SHADER_PRIVCONST_POS);
2045
2046     /* Initialize output parameters. GL_ARB_vertex_program does not require special initialization values
2047      * for output parameters. D3D in theory does not do that either, but some applications depend on a
2048      * proper initialization of the secondary color, and programs using the fixed function pipeline without
2049      * a replacement shader depend on the texcoord.w being set properly.
2050      *
2051      * GL_NV_vertex_program defines that all output values are initialized to {0.0, 0.0, 0.0, 1.0}. This
2052      * assertion is in effect even when using GL_ARB_vertex_program without any NV specific additions. So
2053      * skip this if NV_vertex_program is supported. Otherwise, initialize the secondary color. For the tex-
2054      * coords, we have a flag in the opengl caps. Many cards do not require the texcoord being set, and
2055      * this can eat a number of instructions, so skip it unless this cap is set as well
2056      */
2057     if(!GL_SUPPORT(NV_VERTEX_PROGRAM)) {
2058         shader_addline(buffer, "MOV result.color.secondary, -helper_const.wwwy;\n");
2059
2060         if((GLINFO_LOCATION).set_texcoord_w && !device->frag_pipe->ffp_proj_control) {
2061             int i;
2062             for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2063                 if(This->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2064                 This->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2065                     shader_addline(buffer, "MOV result.texcoord[%u].w, -helper_const.y;\n", i);
2066                 }
2067             }
2068         }
2069     }
2070
2071     /* Base Shader Body */
2072     shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
2073
2074     /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
2075     if (!reg_maps->fog)
2076         shader_addline(buffer, "MOV result.fogcoord, TMP_OUT.z;\n");
2077
2078     /* Write the final position.
2079      *
2080      * OpenGL coordinates specify the center of the pixel while d3d coords specify
2081      * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
2082      * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
2083      * contains 1.0 to allow a mad, but arb vs swizzles are too restricted for that.
2084      */
2085     shader_addline(buffer, "MUL TMP, posFixup, TMP_OUT.w;\n");
2086     shader_addline(buffer, "ADD TMP_OUT.x, TMP_OUT.x, TMP.z;\n");
2087     shader_addline(buffer, "MAD TMP_OUT.y, TMP_OUT.y, posFixup.y, TMP.w;\n");
2088
2089     /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
2090      * and the glsl equivalent
2091      */
2092     shader_addline(buffer, "MAD TMP_OUT.z, TMP_OUT.z, helper_const.x, -TMP_OUT.w;\n");
2093
2094     shader_addline(buffer, "MOV result.position, TMP_OUT;\n");
2095
2096     shader_addline(buffer, "END\n");
2097
2098     /* TODO: change to resource.glObjectHandle or something like that */
2099     GL_EXTCALL(glGenProgramsARB(1, &This->prgId));
2100
2101     TRACE("Creating a hw vertex shader, prg=%d\n", This->prgId);
2102     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, This->prgId));
2103
2104     TRACE("Created hw vertex shader, prg=%d\n", This->prgId);
2105     /* Create the program and check for errors */
2106     GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2107                buffer->bsize, buffer->buffer));
2108
2109     if (glGetError() == GL_INVALID_OPERATION) {
2110         GLint errPos;
2111         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2112         FIXME("HW VertexShader Error at position %d: %s\n",
2113               errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2114         This->prgId = -1;
2115     }
2116
2117     /* Load immediate constants */
2118     if(!This->baseShader.load_local_constsF) {
2119         LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
2120             const float *value = (const float *)lconst->value;
2121             GL_EXTCALL(glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, lconst->idx, value));
2122         }
2123     }
2124 }
2125
2126 static void shader_arb_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
2127 {
2128     /* We don't have an ARB fixed function pipeline yet, so let the none backend set its caps,
2129      * then overwrite the shader specific ones
2130      */
2131     none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
2132
2133     if(GL_SUPPORT(ARB_VERTEX_PROGRAM)) {
2134         pCaps->VertexShaderVersion = WINED3DVS_VERSION(1,1);
2135         TRACE_(d3d_caps)("Hardware vertex shader version 1.1 enabled (ARB_PROGRAM)\n");
2136         pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
2137     }
2138
2139     if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
2140         pCaps->PixelShaderVersion    = WINED3DPS_VERSION(1,4);
2141         pCaps->PixelShader1xMaxValue = 8.0;
2142         TRACE_(d3d_caps)("Hardware pixel shader version 1.4 enabled (ARB_PROGRAM)\n");
2143     }
2144 }
2145
2146 static BOOL shader_arb_color_fixup_supported(struct color_fixup_desc fixup)
2147 {
2148     if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
2149     {
2150         TRACE("Checking support for color_fixup:\n");
2151         dump_color_fixup_desc(fixup);
2152     }
2153
2154     /* We support everything except YUV conversions. */
2155     if (!is_yuv_fixup(fixup))
2156     {
2157         TRACE("[OK]\n");
2158         return TRUE;
2159     }
2160
2161     TRACE("[FAILED]\n");
2162     return FALSE;
2163 }
2164
2165 static const SHADER_HANDLER shader_arb_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
2166 {
2167     /* WINED3DSIH_ABS           */ shader_hw_map2gl,
2168     /* WINED3DSIH_ADD           */ shader_hw_map2gl,
2169     /* WINED3DSIH_BEM           */ pshader_hw_bem,
2170     /* WINED3DSIH_BREAK         */ NULL,
2171     /* WINED3DSIH_BREAKC        */ NULL,
2172     /* WINED3DSIH_BREAKP        */ NULL,
2173     /* WINED3DSIH_CALL          */ NULL,
2174     /* WINED3DSIH_CALLNZ        */ NULL,
2175     /* WINED3DSIH_CMP           */ pshader_hw_cmp,
2176     /* WINED3DSIH_CND           */ pshader_hw_cnd,
2177     /* WINED3DSIH_CRS           */ shader_hw_map2gl,
2178     /* WINED3DSIH_DCL           */ NULL,
2179     /* WINED3DSIH_DEF           */ NULL,
2180     /* WINED3DSIH_DEFB          */ NULL,
2181     /* WINED3DSIH_DEFI          */ NULL,
2182     /* WINED3DSIH_DP2ADD        */ pshader_hw_dp2add,
2183     /* WINED3DSIH_DP3           */ shader_hw_map2gl,
2184     /* WINED3DSIH_DP4           */ shader_hw_map2gl,
2185     /* WINED3DSIH_DST           */ shader_hw_map2gl,
2186     /* WINED3DSIH_DSX           */ NULL,
2187     /* WINED3DSIH_DSY           */ NULL,
2188     /* WINED3DSIH_ELSE          */ NULL,
2189     /* WINED3DSIH_ENDIF         */ NULL,
2190     /* WINED3DSIH_ENDLOOP       */ NULL,
2191     /* WINED3DSIH_ENDREP        */ NULL,
2192     /* WINED3DSIH_EXP           */ shader_hw_map2gl,
2193     /* WINED3DSIH_EXPP          */ shader_hw_map2gl,
2194     /* WINED3DSIH_FRC           */ shader_hw_map2gl,
2195     /* WINED3DSIH_IF            */ NULL,
2196     /* WINED3DSIH_IFC           */ NULL,
2197     /* WINED3DSIH_LABEL         */ NULL,
2198     /* WINED3DSIH_LIT           */ shader_hw_map2gl,
2199     /* WINED3DSIH_LOG           */ shader_hw_map2gl,
2200     /* WINED3DSIH_LOGP          */ shader_hw_map2gl,
2201     /* WINED3DSIH_LOOP          */ NULL,
2202     /* WINED3DSIH_LRP           */ shader_hw_map2gl,
2203     /* WINED3DSIH_M3x2          */ shader_hw_mnxn,
2204     /* WINED3DSIH_M3x3          */ shader_hw_mnxn,
2205     /* WINED3DSIH_M3x4          */ shader_hw_mnxn,
2206     /* WINED3DSIH_M4x3          */ shader_hw_mnxn,
2207     /* WINED3DSIH_M4x4          */ shader_hw_mnxn,
2208     /* WINED3DSIH_MAD           */ shader_hw_map2gl,
2209     /* WINED3DSIH_MAX           */ shader_hw_map2gl,
2210     /* WINED3DSIH_MIN           */ shader_hw_map2gl,
2211     /* WINED3DSIH_MOV           */ shader_hw_mov,
2212     /* WINED3DSIH_MOVA          */ shader_hw_mov,
2213     /* WINED3DSIH_MUL           */ shader_hw_map2gl,
2214     /* WINED3DSIH_NOP           */ shader_hw_map2gl,
2215     /* WINED3DSIH_NRM           */ shader_hw_nrm,
2216     /* WINED3DSIH_PHASE         */ NULL,
2217     /* WINED3DSIH_POW           */ shader_hw_map2gl,
2218     /* WINED3DSIH_RCP           */ vshader_hw_rsq_rcp,
2219     /* WINED3DSIH_REP           */ NULL,
2220     /* WINED3DSIH_RET           */ NULL,
2221     /* WINED3DSIH_RSQ           */ vshader_hw_rsq_rcp,
2222     /* WINED3DSIH_SETP          */ NULL,
2223     /* WINED3DSIH_SGE           */ shader_hw_map2gl,
2224     /* WINED3DSIH_SGN           */ NULL,
2225     /* WINED3DSIH_SINCOS        */ shader_hw_sincos,
2226     /* WINED3DSIH_SLT           */ shader_hw_map2gl,
2227     /* WINED3DSIH_SUB           */ shader_hw_map2gl,
2228     /* WINED3DSIH_TEX           */ pshader_hw_tex,
2229     /* WINED3DSIH_TEXBEM        */ pshader_hw_texbem,
2230     /* WINED3DSIH_TEXBEML       */ pshader_hw_texbem,
2231     /* WINED3DSIH_TEXCOORD      */ pshader_hw_texcoord,
2232     /* WINED3DSIH_TEXDEPTH      */ pshader_hw_texdepth,
2233     /* WINED3DSIH_TEXDP3        */ pshader_hw_texdp3,
2234     /* WINED3DSIH_TEXDP3TEX     */ pshader_hw_texdp3tex,
2235     /* WINED3DSIH_TEXKILL       */ pshader_hw_texkill,
2236     /* WINED3DSIH_TEXLDD        */ NULL,
2237     /* WINED3DSIH_TEXLDL        */ NULL,
2238     /* WINED3DSIH_TEXM3x2DEPTH  */ pshader_hw_texm3x2depth,
2239     /* WINED3DSIH_TEXM3x2PAD    */ pshader_hw_texm3x2pad,
2240     /* WINED3DSIH_TEXM3x2TEX    */ pshader_hw_texm3x2tex,
2241     /* WINED3DSIH_TEXM3x3       */ pshader_hw_texm3x3,
2242     /* WINED3DSIH_TEXM3x3DIFF   */ NULL,
2243     /* WINED3DSIH_TEXM3x3PAD    */ pshader_hw_texm3x3pad,
2244     /* WINED3DSIH_TEXM3x3SPEC   */ pshader_hw_texm3x3spec,
2245     /* WINED3DSIH_TEXM3x3TEX    */ pshader_hw_texm3x3tex,
2246     /* WINED3DSIH_TEXM3x3VSPEC  */ pshader_hw_texm3x3vspec,
2247     /* WINED3DSIH_TEXREG2AR     */ pshader_hw_texreg2ar,
2248     /* WINED3DSIH_TEXREG2GB     */ pshader_hw_texreg2gb,
2249     /* WINED3DSIH_TEXREG2RGB    */ pshader_hw_texreg2rgb,
2250 };
2251
2252 const shader_backend_t arb_program_shader_backend = {
2253     shader_arb_instruction_handler_table,
2254     shader_arb_select,
2255     shader_arb_select_depth_blt,
2256     shader_arb_deselect_depth_blt,
2257     shader_arb_update_float_vertex_constants,
2258     shader_arb_update_float_pixel_constants,
2259     shader_arb_load_constants,
2260     shader_arb_color_correction,
2261     shader_arb_destroy,
2262     shader_arb_alloc,
2263     shader_arb_free,
2264     shader_arb_dirty_const,
2265     shader_arb_generate_pshader,
2266     shader_arb_generate_vshader,
2267     shader_arb_get_caps,
2268     shader_arb_color_fixup_supported,
2269 };
2270
2271 /* ARB_fragment_program fixed function pipeline replacement definitions */
2272 #define ARB_FFP_CONST_TFACTOR           0
2273 #define ARB_FFP_CONST_SPECULAR_ENABLE   ((ARB_FFP_CONST_TFACTOR) + 1)
2274 #define ARB_FFP_CONST_CONSTANT(i)       ((ARB_FFP_CONST_SPECULAR_ENABLE) + 1 + i)
2275 #define ARB_FFP_CONST_BUMPMAT(i)        ((ARB_FFP_CONST_CONSTANT(7)) + 1 + i)
2276 #define ARB_FFP_CONST_LUMINANCE(i)      ((ARB_FFP_CONST_BUMPMAT(7)) + 1 + i)
2277
2278 struct arbfp_ffp_desc
2279 {
2280     struct ffp_frag_desc parent;
2281     GLuint shader;
2282     unsigned int num_textures_used;
2283 };
2284
2285 static void arbfp_enable(IWineD3DDevice *iface, BOOL enable) {
2286     if(enable) {
2287         glEnable(GL_FRAGMENT_PROGRAM_ARB);
2288         checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
2289     } else {
2290         glDisable(GL_FRAGMENT_PROGRAM_ARB);
2291         checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
2292     }
2293 }
2294
2295 static HRESULT arbfp_alloc(IWineD3DDevice *iface) {
2296     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2297     struct shader_arb_priv *priv;
2298     /* Share private data between the shader backend and the pipeline replacement, if both
2299      * are the arb implementation. This is needed to figure out whether ARBfp should be disabled
2300      * if no pixel shader is bound or not
2301      */
2302     if(This->shader_backend == &arb_program_shader_backend) {
2303         This->fragment_priv = This->shader_priv;
2304     } else {
2305         This->fragment_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_arb_priv));
2306         if(!This->fragment_priv) return E_OUTOFMEMORY;
2307     }
2308     priv = (struct shader_arb_priv *) This->fragment_priv;
2309     priv->fragment_shaders = hash_table_create(ffp_frag_program_key_hash, ffp_frag_program_key_compare);
2310     priv->use_arbfp_fixed_func = TRUE;
2311     return WINED3D_OK;
2312 }
2313
2314 static void arbfp_free_ffpshader(void *value, void *gli) {
2315     const WineD3D_GL_Info *gl_info = gli;
2316     struct arbfp_ffp_desc *entry_arb = value;
2317
2318     ENTER_GL();
2319     GL_EXTCALL(glDeleteProgramsARB(1, &entry_arb->shader));
2320     checkGLcall("glDeleteProgramsARB(1, &entry_arb->shader)");
2321     HeapFree(GetProcessHeap(), 0, entry_arb);
2322     LEAVE_GL();
2323 }
2324
2325 static void arbfp_free(IWineD3DDevice *iface) {
2326     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
2327     struct shader_arb_priv *priv = (struct shader_arb_priv *) This->fragment_priv;
2328
2329     hash_table_destroy(priv->fragment_shaders, arbfp_free_ffpshader, &This->adapter->gl_info);
2330     priv->use_arbfp_fixed_func = FALSE;
2331
2332     if(This->shader_backend != &arb_program_shader_backend) {
2333         HeapFree(GetProcessHeap(), 0, This->fragment_priv);
2334     }
2335 }
2336
2337 static void arbfp_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct fragment_caps *caps)
2338 {
2339     caps->TextureOpCaps =  WINED3DTEXOPCAPS_DISABLE                     |
2340                            WINED3DTEXOPCAPS_SELECTARG1                  |
2341                            WINED3DTEXOPCAPS_SELECTARG2                  |
2342                            WINED3DTEXOPCAPS_MODULATE4X                  |
2343                            WINED3DTEXOPCAPS_MODULATE2X                  |
2344                            WINED3DTEXOPCAPS_MODULATE                    |
2345                            WINED3DTEXOPCAPS_ADDSIGNED2X                 |
2346                            WINED3DTEXOPCAPS_ADDSIGNED                   |
2347                            WINED3DTEXOPCAPS_ADD                         |
2348                            WINED3DTEXOPCAPS_SUBTRACT                    |
2349                            WINED3DTEXOPCAPS_ADDSMOOTH                   |
2350                            WINED3DTEXOPCAPS_BLENDCURRENTALPHA           |
2351                            WINED3DTEXOPCAPS_BLENDFACTORALPHA            |
2352                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHA           |
2353                            WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA           |
2354                            WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM         |
2355                            WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR      |
2356                            WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA      |
2357                            WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA   |
2358                            WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR   |
2359                            WINED3DTEXOPCAPS_DOTPRODUCT3                 |
2360                            WINED3DTEXOPCAPS_MULTIPLYADD                 |
2361                            WINED3DTEXOPCAPS_LERP                        |
2362                            WINED3DTEXOPCAPS_BUMPENVMAP                  |
2363                            WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
2364
2365     /* TODO: Implement WINED3DTEXOPCAPS_PREMODULATE */
2366
2367     caps->MaxTextureBlendStages   = 8;
2368     caps->MaxSimultaneousTextures = min(GL_LIMITS(fragment_samplers), 8);
2369
2370     caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_TSSARGTEMP;
2371 }
2372 #undef GLINFO_LOCATION
2373
2374 #define GLINFO_LOCATION stateblock->wineD3DDevice->adapter->gl_info
2375 static void state_texfactor_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2376     float col[4];
2377     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2378
2379     /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2380      * application provided constants
2381      */
2382     if(device->shader_backend == &arb_program_shader_backend) {
2383         if (use_ps(stateblock)) return;
2384
2385         device = stateblock->wineD3DDevice;
2386         device->activeContext->pshader_const_dirty[ARB_FFP_CONST_TFACTOR] = 1;
2387         device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_TFACTOR + 1);
2388     }
2389
2390     D3DCOLORTOGLFLOAT4(stateblock->renderState[WINED3DRS_TEXTUREFACTOR], col);
2391     GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col));
2392     checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_TFACTOR, col)");
2393
2394 }
2395
2396 static void state_arb_specularenable(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2397     float col[4];
2398     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2399
2400     /* Don't load the parameter if we're using an arbfp pixel shader, otherwise we'll overwrite
2401      * application provided constants
2402      */
2403     if(device->shader_backend == &arb_program_shader_backend) {
2404         if (use_ps(stateblock)) return;
2405
2406         device = stateblock->wineD3DDevice;
2407         device->activeContext->pshader_const_dirty[ARB_FFP_CONST_SPECULAR_ENABLE] = 1;
2408         device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_SPECULAR_ENABLE + 1);
2409     }
2410
2411     if(stateblock->renderState[WINED3DRS_SPECULARENABLE]) {
2412         /* The specular color has no alpha */
2413         col[0] = 1.0; col[1] = 1.0;
2414         col[2] = 1.0; col[3] = 0.0;
2415     } else {
2416         col[0] = 0.0; col[1] = 0.0;
2417         col[2] = 0.0; col[3] = 0.0;
2418     }
2419     GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col));
2420     checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_SPECULAR_ENABLE, col)");
2421 }
2422
2423 static void set_bumpmat_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2424     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
2425     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2426     float mat[2][2];
2427
2428     if (use_ps(stateblock))
2429     {
2430         if(stage != 0 &&
2431            ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.bumpmat[stage]) {
2432             /* The pixel shader has to know the bump env matrix. Do a constants update if it isn't scheduled
2433              * anyway
2434              */
2435             if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2436                 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2437             }
2438         }
2439
2440         if(device->shader_backend == &arb_program_shader_backend) {
2441             /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2442             return;
2443         }
2444     } else if(device->shader_backend == &arb_program_shader_backend) {
2445         device->activeContext->pshader_const_dirty[ARB_FFP_CONST_BUMPMAT(stage)] = 1;
2446         device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_BUMPMAT(stage) + 1);
2447     }
2448
2449     mat[0][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT00]);
2450     mat[0][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT01]);
2451     mat[1][0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT10]);
2452     mat[1][1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVMAT11]);
2453
2454     GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0]));
2455     checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_BUMPMAT(stage), &mat[0][0])");
2456 }
2457
2458 static void tex_bumpenvlum_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2459     DWORD stage = (state - STATE_TEXTURESTAGE(0, 0)) / WINED3D_HIGHEST_TEXTURE_STATE;
2460     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2461     float param[4];
2462
2463     if (use_ps(stateblock))
2464     {
2465         if(stage != 0 &&
2466            ((IWineD3DPixelShaderImpl *) stateblock->pixelShader)->baseShader.reg_maps.luminanceparams[stage]) {
2467             /* The pixel shader has to know the luminance offset. Do a constants update if it
2468              * isn't scheduled anyway
2469              */
2470             if(!isStateDirty(context, STATE_PIXELSHADERCONSTANT)) {
2471                 device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
2472             }
2473         }
2474
2475         if(device->shader_backend == &arb_program_shader_backend) {
2476             /* Exit now, don't set the bumpmat below, otherwise we may overwrite pixel shader constants */
2477             return;
2478         }
2479     } else if(device->shader_backend == &arb_program_shader_backend) {
2480         device->activeContext->pshader_const_dirty[ARB_FFP_CONST_LUMINANCE(stage)] = 1;
2481         device->highest_dirty_ps_const = max(device->highest_dirty_ps_const, ARB_FFP_CONST_LUMINANCE(stage) + 1);
2482     }
2483
2484     param[0] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLSCALE]);
2485     param[1] = *((float *) &stateblock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET]);
2486     param[2] = 0.0;
2487     param[3] = 0.0;
2488
2489     GL_EXTCALL(glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param));
2490     checkGLcall("glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, ARB_FFP_CONST_LUMINANCE(stage), param)");
2491 }
2492
2493 static const char *get_argreg(SHADER_BUFFER *buffer, DWORD argnum, unsigned int stage, DWORD arg) {
2494     const char *ret;
2495
2496     if(arg == ARG_UNUSED) return "unused"; /* This is the marker for unused registers */
2497
2498     switch(arg & WINED3DTA_SELECTMASK) {
2499         case WINED3DTA_DIFFUSE:
2500             ret = "fragment.color.primary"; break;
2501
2502         case WINED3DTA_CURRENT:
2503             if(stage == 0) ret = "fragment.color.primary";
2504             else ret = "ret";
2505             break;
2506
2507         case WINED3DTA_TEXTURE:
2508             switch(stage) {
2509                 case 0: ret = "tex0"; break;
2510                 case 1: ret = "tex1"; break;
2511                 case 2: ret = "tex2"; break;
2512                 case 3: ret = "tex3"; break;
2513                 case 4: ret = "tex4"; break;
2514                 case 5: ret = "tex5"; break;
2515                 case 6: ret = "tex6"; break;
2516                 case 7: ret = "tex7"; break;
2517                 default: ret = "unknown texture";
2518             }
2519             break;
2520
2521         case WINED3DTA_TFACTOR:
2522             ret = "tfactor"; break;
2523
2524         case WINED3DTA_SPECULAR:
2525             ret = "fragment.color.secondary"; break;
2526
2527         case WINED3DTA_TEMP:
2528             ret = "tempreg"; break;
2529
2530         case WINED3DTA_CONSTANT:
2531             FIXME("Implement perstage constants\n");
2532             switch(stage) {
2533                 case 0: ret = "const0"; break;
2534                 case 1: ret = "const1"; break;
2535                 case 2: ret = "const2"; break;
2536                 case 3: ret = "const3"; break;
2537                 case 4: ret = "const4"; break;
2538                 case 5: ret = "const5"; break;
2539                 case 6: ret = "const6"; break;
2540                 case 7: ret = "const7"; break;
2541                 default: ret = "unknown constant";
2542             }
2543             break;
2544
2545         default:
2546             return "unknown";
2547     }
2548
2549     if(arg & WINED3DTA_COMPLEMENT) {
2550         shader_addline(buffer, "SUB arg%u, const.x, %s;\n", argnum, ret);
2551         if(argnum == 0) ret = "arg0";
2552         if(argnum == 1) ret = "arg1";
2553         if(argnum == 2) ret = "arg2";
2554     }
2555     if(arg & WINED3DTA_ALPHAREPLICATE) {
2556         shader_addline(buffer, "MOV arg%u, %s.w;\n", argnum, ret);
2557         if(argnum == 0) ret = "arg0";
2558         if(argnum == 1) ret = "arg1";
2559         if(argnum == 2) ret = "arg2";
2560     }
2561     return ret;
2562 }
2563
2564 static void gen_ffp_instr(SHADER_BUFFER *buffer, unsigned int stage, BOOL color, BOOL alpha,
2565                           DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2) {
2566     const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
2567     unsigned int mul = 1;
2568     BOOL mul_final_dest = FALSE;
2569
2570     if(color && alpha) dstmask = "";
2571     else if(color) dstmask = ".xyz";
2572     else dstmask = ".w";
2573
2574     if(dst == tempreg) dstreg = "tempreg";
2575     else dstreg = "ret";
2576
2577     arg0 = get_argreg(buffer, 0, stage, dw_arg0);
2578     arg1 = get_argreg(buffer, 1, stage, dw_arg1);
2579     arg2 = get_argreg(buffer, 2, stage, dw_arg2);
2580
2581     switch(op) {
2582         case WINED3DTOP_DISABLE:
2583             if(stage == 0) shader_addline(buffer, "MOV %s%s, fragment.color.primary;\n", dstreg, dstmask);
2584             break;
2585
2586         case WINED3DTOP_SELECTARG2:
2587             arg1 = arg2;
2588         case WINED3DTOP_SELECTARG1:
2589             shader_addline(buffer, "MOV %s%s, %s;\n", dstreg, dstmask, arg1);
2590             break;
2591
2592         case WINED3DTOP_MODULATE4X:
2593             mul = 2;
2594         case WINED3DTOP_MODULATE2X:
2595             mul *= 2;
2596             if(strcmp(dstreg, "result.color") == 0) {
2597                 dstreg = "ret";
2598                 mul_final_dest = TRUE;
2599             }
2600         case WINED3DTOP_MODULATE:
2601             shader_addline(buffer, "MUL %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2602             break;
2603
2604         case WINED3DTOP_ADDSIGNED2X:
2605             mul = 2;
2606             if(strcmp(dstreg, "result.color") == 0) {
2607                 dstreg = "ret";
2608                 mul_final_dest = TRUE;
2609             }
2610         case WINED3DTOP_ADDSIGNED:
2611             shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2612             arg2 = "arg2";
2613         case WINED3DTOP_ADD:
2614             shader_addline(buffer, "ADD_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2615             break;
2616
2617         case WINED3DTOP_SUBTRACT:
2618             shader_addline(buffer, "SUB_SAT %s%s, %s, %s;\n", dstreg, dstmask, arg1, arg2);
2619             break;
2620
2621         case WINED3DTOP_ADDSMOOTH:
2622             shader_addline(buffer, "SUB arg1, const.x, %s;\n", arg1);
2623             shader_addline(buffer, "MAD_SAT %s%s, arg1, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2624             break;
2625
2626         case WINED3DTOP_BLENDCURRENTALPHA:
2627             arg0 = get_argreg(buffer, 0, stage, WINED3DTA_CURRENT);
2628             shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2629             break;
2630         case WINED3DTOP_BLENDFACTORALPHA:
2631             arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TFACTOR);
2632             shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2633             break;
2634         case WINED3DTOP_BLENDTEXTUREALPHA:
2635             arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2636             shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2637             break;
2638         case WINED3DTOP_BLENDDIFFUSEALPHA:
2639             arg0 = get_argreg(buffer, 0, stage, WINED3DTA_DIFFUSE);
2640             shader_addline(buffer, "LRP %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2641             break;
2642
2643         case WINED3DTOP_BLENDTEXTUREALPHAPM:
2644             arg0 = get_argreg(buffer, 0, stage, WINED3DTA_TEXTURE);
2645             shader_addline(buffer, "SUB arg0.w, const.x, %s.w;\n", arg0);
2646             shader_addline(buffer, "MAD_SAT %s%s, %s, arg0.w, %s;\n", dstreg, dstmask, arg2, arg1);
2647             break;
2648
2649         /* D3DTOP_PREMODULATE ???? */
2650
2651         case WINED3DTOP_MODULATEINVALPHA_ADDCOLOR:
2652             shader_addline(buffer, "SUB arg0.w, const.x, %s;\n", arg1);
2653             shader_addline(buffer, "MAD_SAT %s%s, arg0.w, %s, %s;\n", dstreg, dstmask, arg2, arg1);
2654             break;
2655         case WINED3DTOP_MODULATEALPHA_ADDCOLOR:
2656             shader_addline(buffer, "MAD_SAT %s%s, %s.w, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg1);
2657             break;
2658         case WINED3DTOP_MODULATEINVCOLOR_ADDALPHA:
2659             shader_addline(buffer, "SUB arg0, const.x, %s;\n", arg1);
2660             shader_addline(buffer, "MAD_SAT %s%s, arg0, %s, %s.w;\n", dstreg, dstmask, arg2, arg1);
2661             break;
2662         case WINED3DTOP_MODULATECOLOR_ADDALPHA:
2663             shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s.w;\n", dstreg, dstmask, arg1, arg2, arg1);
2664             break;
2665
2666         case WINED3DTOP_DOTPRODUCT3:
2667             mul = 4;
2668             if(strcmp(dstreg, "result.color") == 0) {
2669                 dstreg = "ret";
2670                 mul_final_dest = TRUE;
2671             }
2672             shader_addline(buffer, "SUB arg1, %s, const.w;\n", arg1);
2673             shader_addline(buffer, "SUB arg2, %s, const.w;\n", arg2);
2674             shader_addline(buffer, "DP3_SAT %s%s, arg1, arg2;\n", dstreg, dstmask);
2675             break;
2676
2677         case WINED3DTOP_MULTIPLYADD:
2678             shader_addline(buffer, "MAD_SAT %s%s, %s, %s, %s;\n", dstreg, dstmask, arg1, arg2, arg0);
2679             break;
2680
2681         case WINED3DTOP_LERP:
2682             /* The msdn is not quite right here */
2683             shader_addline(buffer, "LRP %s%s, %s, %s, %s;\n", dstreg, dstmask, arg0, arg1, arg2);
2684             break;
2685
2686         case WINED3DTOP_BUMPENVMAP:
2687         case WINED3DTOP_BUMPENVMAPLUMINANCE:
2688             /* Those are handled in the first pass of the shader(generation pass 1 and 2) already */
2689             break;
2690
2691         default:
2692             FIXME("Unhandled texture op %08x\n", op);
2693     }
2694
2695     if(mul == 2) {
2696         shader_addline(buffer, "MUL_SAT %s%s, %s, const.y;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2697     } else if(mul == 4) {
2698         shader_addline(buffer, "MUL_SAT %s%s, %s, const.z;\n", mul_final_dest ? "result.color" : dstreg, dstmask, dstreg);
2699     }
2700 }
2701
2702 /* The stateblock is passed for GLINFO_LOCATION */
2703 static GLuint gen_arbfp_ffp_shader(const struct ffp_frag_settings *settings, IWineD3DStateBlockImpl *stateblock)
2704 {
2705     unsigned int stage;
2706     SHADER_BUFFER buffer;
2707     BOOL tex_read[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2708     BOOL bump_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2709     BOOL luminance_used[MAX_TEXTURES] = {FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
2710     const char *textype;
2711     const char *instr, *sat;
2712     char colorcor_dst[8];
2713     GLuint ret;
2714     DWORD arg0, arg1, arg2;
2715     BOOL tempreg_used = FALSE, tfactor_used = FALSE;
2716     BOOL op_equal;
2717     const char *final_combiner_src = "ret";
2718
2719     /* Find out which textures are read */
2720     for(stage = 0; stage < MAX_TEXTURES; stage++) {
2721         if(settings->op[stage].cop == WINED3DTOP_DISABLE) break;
2722         arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
2723         arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
2724         arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
2725         if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2726         if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2727         if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2728
2729         if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHA) tex_read[stage] = TRUE;
2730         if(settings->op[stage].cop == WINED3DTOP_BLENDTEXTUREALPHAPM) tex_read[stage] = TRUE;
2731         if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP) {
2732             bump_used[stage] = TRUE;
2733             tex_read[stage] = TRUE;
2734         }
2735         if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2736             bump_used[stage] = TRUE;
2737             tex_read[stage] = TRUE;
2738             luminance_used[stage] = TRUE;
2739         } else if(settings->op[stage].cop == WINED3DTOP_BLENDFACTORALPHA) {
2740             tfactor_used = TRUE;
2741         }
2742
2743         if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2744             tfactor_used = TRUE;
2745         }
2746
2747         if(settings->op[stage].dst == tempreg) tempreg_used = TRUE;
2748         if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2749             tempreg_used = TRUE;
2750         }
2751
2752         if(settings->op[stage].aop == WINED3DTOP_DISABLE) continue;
2753         arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
2754         arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
2755         arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
2756         if(arg0 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2757         if(arg1 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2758         if(arg2 == WINED3DTA_TEXTURE) tex_read[stage] = TRUE;
2759
2760         if(arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP) {
2761             tempreg_used = TRUE;
2762         }
2763         if(arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR) {
2764             tfactor_used = TRUE;
2765         }
2766     }
2767
2768     /* Shader header */
2769     shader_buffer_init(&buffer);
2770
2771     shader_addline(&buffer, "!!ARBfp1.0\n");
2772
2773     switch(settings->fog) {
2774         case FOG_OFF:                                                         break;
2775         case FOG_LINEAR: shader_addline(&buffer, "OPTION ARB_fog_linear;\n"); break;
2776         case FOG_EXP:    shader_addline(&buffer, "OPTION ARB_fog_exp;\n");    break;
2777         case FOG_EXP2:   shader_addline(&buffer, "OPTION ARB_fog_exp2;\n");   break;
2778         default: FIXME("Unexpected fog setting %d\n", settings->fog);
2779     }
2780
2781     shader_addline(&buffer, "PARAM const = {1, 2, 4, 0.5};\n");
2782     shader_addline(&buffer, "TEMP TMP;\n");
2783     shader_addline(&buffer, "TEMP ret;\n");
2784     if(tempreg_used || settings->sRGB_write) shader_addline(&buffer, "TEMP tempreg;\n");
2785     shader_addline(&buffer, "TEMP arg0;\n");
2786     shader_addline(&buffer, "TEMP arg1;\n");
2787     shader_addline(&buffer, "TEMP arg2;\n");
2788     for(stage = 0; stage < MAX_TEXTURES; stage++) {
2789         if(!tex_read[stage]) continue;
2790         shader_addline(&buffer, "TEMP tex%u;\n", stage);
2791         if(!bump_used[stage]) continue;
2792         shader_addline(&buffer, "PARAM bumpmat%u = program.env[%u];\n", stage, ARB_FFP_CONST_BUMPMAT(stage));
2793         if(!luminance_used[stage]) continue;
2794         shader_addline(&buffer, "PARAM luminance%u = program.env[%u];\n", stage, ARB_FFP_CONST_LUMINANCE(stage));
2795     }
2796     if(tfactor_used) {
2797         shader_addline(&buffer, "PARAM tfactor = program.env[%u];\n", ARB_FFP_CONST_TFACTOR);
2798     }
2799         shader_addline(&buffer, "PARAM specular_enable = program.env[%u];\n", ARB_FFP_CONST_SPECULAR_ENABLE);
2800
2801     if(settings->sRGB_write) {
2802         shader_addline(&buffer, "PARAM srgb_mul_low = {%f, %f, %f, 1.0};\n",
2803                        srgb_mul_low, srgb_mul_low, srgb_mul_low);
2804         shader_addline(&buffer, "PARAM srgb_comparison =  {%f, %f, %f, %f};\n",
2805                        srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
2806         shader_addline(&buffer, "PARAM srgb_pow =  {%f, %f, %f, 1.0};\n",
2807                        srgb_pow, srgb_pow, srgb_pow);
2808         shader_addline(&buffer, "PARAM srgb_mul_hi =  {%f, %f, %f, 1.0};\n",
2809                        srgb_mul_high, srgb_mul_high, srgb_mul_high);
2810         shader_addline(&buffer, "PARAM srgb_sub_hi =  {%f, %f, %f, 0.0};\n",
2811                        srgb_sub_high, srgb_sub_high, srgb_sub_high);
2812     }
2813
2814     /* Generate texture sampling instructions) */
2815     for(stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3DTOP_DISABLE; stage++) {
2816         if(!tex_read[stage]) continue;
2817
2818         switch(settings->op[stage].tex_type) {
2819             case tex_1d:                    textype = "1D";     break;
2820             case tex_2d:                    textype = "2D";     break;
2821             case tex_3d:                    textype = "3D";     break;
2822             case tex_cube:                  textype = "CUBE";   break;
2823             case tex_rect:                  textype = "RECT";   break;
2824             default: textype = "unexpected_textype";   break;
2825         }
2826
2827         if(settings->op[stage].cop == WINED3DTOP_BUMPENVMAP ||
2828            settings->op[stage].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2829             sat = "";
2830         } else {
2831             sat = "_SAT";
2832         }
2833
2834         if(settings->op[stage].projected == proj_none) {
2835             instr = "TEX";
2836         } else if(settings->op[stage].projected == proj_count4 ||
2837                   settings->op[stage].projected == proj_count3) {
2838             instr = "TXP";
2839         } else {
2840             FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
2841             instr = "TXP";
2842         }
2843
2844         if(stage > 0 &&
2845            (settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAP ||
2846             settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE)) {
2847             shader_addline(&buffer, "SWZ arg1, bumpmat%u, x, z, 0, 0;\n", stage - 1);
2848             shader_addline(&buffer, "DP3 ret.x, arg1, tex%u;\n", stage - 1);
2849             shader_addline(&buffer, "SWZ arg1, bumpmat%u, y, w, 0, 0;\n", stage - 1);
2850             shader_addline(&buffer, "DP3 ret.y, arg1, tex%u;\n", stage - 1);
2851
2852             /* with projective textures, texbem only divides the static texture coord, not the displacement,
2853              * so multiply the displacement with the dividing parameter before passing it to TXP
2854              */
2855             if (settings->op[stage].projected != proj_none) {
2856                 if(settings->op[stage].projected == proj_count4) {
2857                     shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].w;\n", stage);
2858                     shader_addline(&buffer, "MUL ret.xyz, ret, fragment.texcoord[%u].w, fragment.texcoord[%u];\n", stage, stage);
2859                 } else {
2860                     shader_addline(&buffer, "MOV ret.w, fragment.texcoord[%u].z;\n", stage);
2861                     shader_addline(&buffer, "MAD ret.xyz, ret, fragment.texcoord[%u].z, fragment.texcoord[%u];\n", stage, stage);
2862                 }
2863             } else {
2864                 shader_addline(&buffer, "ADD ret, ret, fragment.texcoord[%u];\n", stage);
2865             }
2866
2867             shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
2868                            instr, sat, stage, stage, textype);
2869             if(settings->op[stage - 1].cop == WINED3DTOP_BUMPENVMAPLUMINANCE) {
2870                 shader_addline(&buffer, "MAD_SAT ret.x, tex%u.z, luminance%u.x, luminance%u.y;\n",
2871                                stage - 1, stage - 1, stage - 1);
2872                 shader_addline(&buffer, "MUL tex%u, tex%u, ret.x;\n", stage, stage);
2873             }
2874         } else if(settings->op[stage].projected == proj_count3) {
2875             shader_addline(&buffer, "MOV ret, fragment.texcoord[%u];\n", stage);
2876             shader_addline(&buffer, "MOV ret.w, ret.z;\n");
2877             shader_addline(&buffer, "%s%s tex%u, ret, texture[%u], %s;\n",
2878                             instr, sat, stage, stage, textype);
2879         } else {
2880             shader_addline(&buffer, "%s%s tex%u, fragment.texcoord[%u], texture[%u], %s;\n",
2881                             instr, sat, stage, stage, stage, textype);
2882         }
2883
2884         sprintf(colorcor_dst, "tex%u", stage);
2885         gen_color_correction(&buffer, colorcor_dst, WINED3DSP_WRITEMASK_ALL, "const.x", "const.y",
2886                 settings->op[stage].color_fixup);
2887     }
2888
2889     /* Generate the main shader */
2890     for(stage = 0; stage < MAX_TEXTURES; stage++) {
2891         if(settings->op[stage].cop == WINED3DTOP_DISABLE) {
2892             if(stage == 0) {
2893                 final_combiner_src = "fragment.color.primary";
2894             }
2895             break;
2896         }
2897
2898         if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
2899            settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
2900             op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
2901         } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG1 &&
2902                   settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
2903             op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
2904         } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
2905                   settings->op[stage].aop == WINED3DTOP_SELECTARG1) {
2906             op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
2907         } else if(settings->op[stage].cop == WINED3DTOP_SELECTARG2 &&
2908                   settings->op[stage].aop == WINED3DTOP_SELECTARG2) {
2909             op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
2910         } else {
2911             op_equal = settings->op[stage].aop   == settings->op[stage].cop &&
2912                        settings->op[stage].carg0 == settings->op[stage].aarg0 &&
2913                        settings->op[stage].carg1 == settings->op[stage].aarg1 &&
2914                        settings->op[stage].carg2 == settings->op[stage].aarg2;
2915         }
2916
2917         if(settings->op[stage].aop == WINED3DTOP_DISABLE) {
2918             gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
2919                           settings->op[stage].cop, settings->op[stage].carg0,
2920                           settings->op[stage].carg1, settings->op[stage].carg2);
2921             if(stage == 0) {
2922                 shader_addline(&buffer, "MOV ret.w, fragment.color.primary.w;\n");
2923             }
2924         } else if(op_equal) {
2925             gen_ffp_instr(&buffer, stage, TRUE, TRUE, settings->op[stage].dst,
2926                           settings->op[stage].cop, settings->op[stage].carg0,
2927                           settings->op[stage].carg1, settings->op[stage].carg2);
2928         } else {
2929             gen_ffp_instr(&buffer, stage, TRUE, FALSE, settings->op[stage].dst,
2930                           settings->op[stage].cop, settings->op[stage].carg0,
2931                           settings->op[stage].carg1, settings->op[stage].carg2);
2932             gen_ffp_instr(&buffer, stage, FALSE, TRUE, settings->op[stage].dst,
2933                           settings->op[stage].aop, settings->op[stage].aarg0,
2934                           settings->op[stage].aarg1, settings->op[stage].aarg2);
2935         }
2936     }
2937
2938     if(settings->sRGB_write) {
2939         shader_addline(&buffer, "MAD ret, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
2940         arbfp_add_sRGB_correction(&buffer, "ret", "arg0", "arg1", "arg2", "tempreg");
2941         shader_addline(&buffer, "MOV result.color.w, ret.w;\n");
2942     } else {
2943         shader_addline(&buffer, "MAD result.color, fragment.color.secondary, specular_enable, %s;\n", final_combiner_src);
2944     }
2945
2946     /* Footer */
2947     shader_addline(&buffer, "END\n");
2948
2949     /* Generate the shader */
2950     GL_EXTCALL(glGenProgramsARB(1, &ret));
2951     GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret));
2952     GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
2953
2954     if (glGetError() == GL_INVALID_OPERATION) {
2955         GLint pos;
2956         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
2957         FIXME("Fragment program error at position %d: %s\n", pos,
2958               debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
2959     }
2960     shader_buffer_free(&buffer);
2961     return ret;
2962 }
2963
2964 static void fragment_prog_arbfp(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
2965     IWineD3DDeviceImpl *device = stateblock->wineD3DDevice;
2966     struct shader_arb_priv *priv = (struct shader_arb_priv *) device->fragment_priv;
2967     BOOL use_pshader = use_ps(stateblock);
2968     BOOL use_vshader = use_vs(stateblock);
2969     struct ffp_frag_settings settings;
2970     const struct arbfp_ffp_desc *desc;
2971     unsigned int i;
2972
2973     if(isStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE))) {
2974         if(!use_pshader && device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
2975             /* Reload fixed function constants since they collide with the pixel shader constants */
2976             for(i = 0; i < MAX_TEXTURES; i++) {
2977                 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
2978             }
2979             state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
2980             state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
2981         } else if(use_pshader && !isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
2982             device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
2983         }
2984         return;
2985     }
2986
2987     if(!use_pshader) {
2988         /* Find or create a shader implementing the fixed function pipeline settings, then activate it */
2989         gen_ffp_frag_op(stateblock, &settings, FALSE);
2990         desc = (const struct arbfp_ffp_desc *)find_ffp_frag_shader(priv->fragment_shaders, &settings);
2991         if(!desc) {
2992             struct arbfp_ffp_desc *new_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_desc));
2993             if (!new_desc)
2994             {
2995                 ERR("Out of memory\n");
2996                 return;
2997             }
2998             new_desc->num_textures_used = 0;
2999             for(i = 0; i < GL_LIMITS(texture_stages); i++) {
3000                 if(settings.op[i].cop == WINED3DTOP_DISABLE) break;
3001                 new_desc->num_textures_used = i;
3002             }
3003
3004             memcpy(&new_desc->parent.settings, &settings, sizeof(settings));
3005             new_desc->shader = gen_arbfp_ffp_shader(&settings, stateblock);
3006             add_ffp_frag_shader(priv->fragment_shaders, &new_desc->parent);
3007             TRACE("Allocated fixed function replacement shader descriptor %p\n", new_desc);
3008             desc = new_desc;
3009         }
3010
3011         /* Now activate the replacement program. GL_FRAGMENT_PROGRAM_ARB is already active(however, note the
3012          * comment above the shader_select call below). If e.g. GLSL is active, the shader_select call will
3013          * deactivate it.
3014          */
3015         GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader));
3016         checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, desc->shader)");
3017         priv->current_fprogram_id = desc->shader;
3018
3019         if(device->shader_backend == &arb_program_shader_backend && context->last_was_pshader) {
3020             /* Reload fixed function constants since they collide with the pixel shader constants */
3021             for(i = 0; i < MAX_TEXTURES; i++) {
3022                 set_bumpmat_arbfp(STATE_TEXTURESTAGE(i, WINED3DTSS_BUMPENVMAT00), stateblock, context);
3023             }
3024             state_texfactor_arbfp(STATE_RENDER(WINED3DRS_TEXTUREFACTOR), stateblock, context);
3025             state_arb_specularenable(STATE_RENDER(WINED3DRS_SPECULARENABLE), stateblock, context);
3026         }
3027     }
3028
3029     /* Finally, select the shader. If a pixel shader is used, it will be set and enabled by the shader backend.
3030      * If this shader backend is arbfp(most likely), then it will simply overwrite the last fixed function replace-
3031      * ment shader. If the shader backend is not ARB, it currently is important that the opengl implementation
3032      * type overwrites GL_ARB_fragment_program. This is currently the case with GLSL. If we really want to use
3033      * atifs or nvrc pixel shaders with arb fragment programs we'd have to disable GL_FRAGMENT_PROGRAM_ARB here
3034      *
3035      * Don't call shader_select if the vertex shader is dirty, because some shader backends(GLSL) need both shaders
3036      * to be compiled before activating them(needs some cleanups in the shader backend interface)
3037      */
3038     if(!isStateDirty(context, device->StateTable[STATE_VSHADER].representative)) {
3039         device->shader_backend->shader_select((IWineD3DDevice *)stateblock->wineD3DDevice, use_pshader, use_vshader);
3040
3041         if (!isStateDirty(context, STATE_VERTEXSHADERCONSTANT) && (use_vshader || use_pshader)) {
3042             device->StateTable[STATE_VERTEXSHADERCONSTANT].apply(STATE_VERTEXSHADERCONSTANT, stateblock, context);
3043         }
3044     }
3045     if(use_pshader) {
3046         device->StateTable[STATE_PIXELSHADERCONSTANT].apply(STATE_PIXELSHADERCONSTANT, stateblock, context);
3047     }
3048 }
3049
3050 /* We can't link the fog states to the fragment state directly since the vertex pipeline links them
3051  * to FOGENABLE. A different linking in different pipeline parts can't be expressed in the combined
3052  * state table, so we need to handle that with a forwarding function. The other invisible side effect
3053  * is that changing the fog start and fog end(which links to FOGENABLE in vertex) results in the
3054  * fragment_prog_arbfp function being called because FOGENABLE is dirty, which calls this function here
3055  */
3056 static void state_arbfp_fog(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3057     if(!isStateDirty(context, STATE_PIXELSHADER)) {
3058         fragment_prog_arbfp(state, stateblock, context);
3059     }
3060 }
3061
3062 static void textransform(DWORD state, IWineD3DStateBlockImpl *stateblock, WineD3DContext *context) {
3063     if(!isStateDirty(context, STATE_PIXELSHADER)) {
3064         fragment_prog_arbfp(state, stateblock, context);
3065     }
3066 }
3067
3068 #undef GLINFO_LOCATION
3069
3070 static const struct StateEntryTemplate arbfp_fragmentstate_template[] = {
3071     {STATE_RENDER(WINED3DRS_TEXTUREFACTOR),               { STATE_RENDER(WINED3DRS_TEXTUREFACTOR),              state_texfactor_arbfp   }, 0                               },
3072     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3073     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3074     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3075     {STATE_TEXTURESTAGE(0, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3076     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3077     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3078     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3079     {STATE_TEXTURESTAGE(0, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3080     {STATE_TEXTURESTAGE(0, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3081     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3082     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3083     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3084     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3085     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3086     {STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(0, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3087     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3088     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3089     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3090     {STATE_TEXTURESTAGE(1, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3091     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3092     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3093     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3094     {STATE_TEXTURESTAGE(1, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3095     {STATE_TEXTURESTAGE(1, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3096     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3097     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3098     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3099     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3100     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3101     {STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(1, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3102     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3103     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3104     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3105     {STATE_TEXTURESTAGE(2, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3106     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3107     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3108     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3109     {STATE_TEXTURESTAGE(2, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3110     {STATE_TEXTURESTAGE(2, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3111     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3112     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3113     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3114     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3115     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3116     {STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(2, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3117     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3118     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3119     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3120     {STATE_TEXTURESTAGE(3, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3121     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3122     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3123     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3124     {STATE_TEXTURESTAGE(3, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3125     {STATE_TEXTURESTAGE(3, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3126     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3127     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3128     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3129     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3130     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3131     {STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(3, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3132     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3133     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3134     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3135     {STATE_TEXTURESTAGE(4, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3136     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3137     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3138     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3139     {STATE_TEXTURESTAGE(4, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3140     {STATE_TEXTURESTAGE(4, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3141     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3142     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3143     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3144     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3145     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3146     {STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(4, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3147     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3148     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3149     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3150     {STATE_TEXTURESTAGE(5, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3151     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3152     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3153     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3154     {STATE_TEXTURESTAGE(5, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3155     {STATE_TEXTURESTAGE(5, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3156     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3157     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3158     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3159     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3160     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3161     {STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(5, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3162     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3163     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3164     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3165     {STATE_TEXTURESTAGE(6, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3166     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3167     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3168     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3169     {STATE_TEXTURESTAGE(6, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3170     {STATE_TEXTURESTAGE(6, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3171     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3172     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3173     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3174     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3175     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3176     {STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(6, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3177     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLOROP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3178     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3179     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3180     {STATE_TEXTURESTAGE(7, WINED3DTSS_COLORARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3181     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAOP),           { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3182     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG1),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3183     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG2),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3184     {STATE_TEXTURESTAGE(7, WINED3DTSS_ALPHAARG0),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3185     {STATE_TEXTURESTAGE(7, WINED3DTSS_RESULTARG),         { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3186     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3187     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT01),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3188     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT10),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3189     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT11),      { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVMAT00),     set_bumpmat_arbfp       }, 0                               },
3190     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE),     { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3191     {STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLOFFSET),    { STATE_TEXTURESTAGE(7, WINED3DTSS_BUMPENVLSCALE),    tex_bumpenvlum_arbfp    }, 0                               },
3192     { STATE_SAMPLER(0),                                   { STATE_SAMPLER(0),                                   sampler_texdim          }, 0                               },
3193     { STATE_SAMPLER(1),                                   { STATE_SAMPLER(1),                                   sampler_texdim          }, 0                               },
3194     { STATE_SAMPLER(2),                                   { STATE_SAMPLER(2),                                   sampler_texdim          }, 0                               },
3195     { STATE_SAMPLER(3),                                   { STATE_SAMPLER(3),                                   sampler_texdim          }, 0                               },
3196     { STATE_SAMPLER(4),                                   { STATE_SAMPLER(4),                                   sampler_texdim          }, 0                               },
3197     { STATE_SAMPLER(5),                                   { STATE_SAMPLER(5),                                   sampler_texdim          }, 0                               },
3198     { STATE_SAMPLER(6),                                   { STATE_SAMPLER(6),                                   sampler_texdim          }, 0                               },
3199     { STATE_SAMPLER(7),                                   { STATE_SAMPLER(7),                                   sampler_texdim          }, 0                               },
3200     { STATE_PIXELSHADER,                                  { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3201     { STATE_RENDER(WINED3DRS_FOGENABLE),                  { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_arbfp_fog         }, 0                               },
3202     { STATE_RENDER(WINED3DRS_FOGTABLEMODE),               { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_arbfp_fog         }, 0                               },
3203     { STATE_RENDER(WINED3DRS_FOGVERTEXMODE),              { STATE_RENDER(WINED3DRS_FOGENABLE),                  state_arbfp_fog         }, 0                               },
3204     { STATE_RENDER(WINED3DRS_SRGBWRITEENABLE),            { STATE_PIXELSHADER,                                  fragment_prog_arbfp     }, 0                               },
3205     {STATE_TEXTURESTAGE(0,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(0, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3206     {STATE_TEXTURESTAGE(1,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(1, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3207     {STATE_TEXTURESTAGE(2,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(2, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3208     {STATE_TEXTURESTAGE(3,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(3, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3209     {STATE_TEXTURESTAGE(4,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(4, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3210     {STATE_TEXTURESTAGE(5,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(5, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3211     {STATE_TEXTURESTAGE(6,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(6, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3212     {STATE_TEXTURESTAGE(7,WINED3DTSS_TEXTURETRANSFORMFLAGS),{STATE_TEXTURESTAGE(7, WINED3DTSS_TEXTURETRANSFORMFLAGS), textransform      }, 0                               },
3213     { STATE_RENDER(WINED3DRS_SPECULARENABLE),             { STATE_RENDER(WINED3DRS_SPECULARENABLE),             state_arb_specularenable}, 0                               },
3214     {0 /* Terminate */,                                   { 0,                                                  0                       }, 0                               },
3215 };
3216
3217 const struct fragment_pipeline arbfp_fragment_pipeline = {
3218     arbfp_enable,
3219     arbfp_get_caps,
3220     arbfp_alloc,
3221     arbfp_free,
3222     shader_arb_color_fixup_supported,
3223     arbfp_fragmentstate_template,
3224     TRUE /* We can disable projected textures */
3225 };
3226
3227 #define GLINFO_LOCATION device->adapter->gl_info
3228
3229 struct arbfp_blit_priv {
3230     GLenum yuy2_rect_shader, yuy2_2d_shader;
3231     GLenum uyvy_rect_shader, uyvy_2d_shader;
3232     GLenum yv12_rect_shader, yv12_2d_shader;
3233 };
3234
3235 static HRESULT arbfp_blit_alloc(IWineD3DDevice *iface) {
3236     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3237     device->blit_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct arbfp_blit_priv));
3238     if(!device->blit_priv) {
3239         ERR("Out of memory\n");
3240         return E_OUTOFMEMORY;
3241     }
3242     return WINED3D_OK;
3243 }
3244 static void arbfp_blit_free(IWineD3DDevice *iface) {
3245     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3246     struct arbfp_blit_priv *priv = (struct arbfp_blit_priv *) device->blit_priv;
3247
3248     ENTER_GL();
3249     GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_rect_shader));
3250     GL_EXTCALL(glDeleteProgramsARB(1, &priv->yuy2_2d_shader));
3251     GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_rect_shader));
3252     GL_EXTCALL(glDeleteProgramsARB(1, &priv->uyvy_2d_shader));
3253     GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_rect_shader));
3254     GL_EXTCALL(glDeleteProgramsARB(1, &priv->yv12_2d_shader));
3255     checkGLcall("Delete yuv programs\n");
3256     LEAVE_GL();
3257 }
3258
3259 static BOOL gen_planar_yuv_read(SHADER_BUFFER *buffer, enum yuv_fixup yuv_fixup, GLenum textype, char *luminance)
3260 {
3261     char chroma;
3262     const char *tex, *texinstr;
3263
3264     if (yuv_fixup == YUV_FIXUP_UYVY) {
3265         chroma = 'x';
3266         *luminance = 'w';
3267     } else {
3268         chroma = 'w';
3269         *luminance = 'x';
3270     }
3271     switch(textype) {
3272         case GL_TEXTURE_2D:             tex = "2D";     texinstr = "TXP"; break;
3273         case GL_TEXTURE_RECTANGLE_ARB:  tex = "RECT";   texinstr = "TEX"; break;
3274         default:
3275             /* This is more tricky than just replacing the texture type - we have to navigate
3276              * properly in the texture to find the correct chroma values
3277              */
3278             FIXME("Implement yuv correction for non-2d, non-rect textures\n");
3279             return FALSE;
3280     }
3281
3282     /* First we have to read the chroma values. This means we need at least two pixels(no filtering),
3283      * or 4 pixels(with filtering). To get the unmodified chromas, we have to rid ourselves of the
3284      * filtering when we sample the texture.
3285      *
3286      * These are the rules for reading the chroma:
3287      *
3288      * Even pixel: Cr
3289      * Even pixel: U
3290      * Odd pixel: V
3291      *
3292      * So we have to get the sampling x position in non-normalized coordinates in integers
3293      */
3294     if(textype != GL_TEXTURE_RECTANGLE_ARB) {
3295         shader_addline(buffer, "MUL texcrd.xy, fragment.texcoord[0], size.x;\n");
3296         shader_addline(buffer, "MOV texcrd.w, size.x;\n");
3297     } else {
3298         shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3299     }
3300     /* We must not allow filtering between pixel x and x+1, this would mix U and V
3301      * Vertical filtering is ok. However, bear in mind that the pixel center is at
3302      * 0.5, so add 0.5.
3303      */
3304     shader_addline(buffer, "FLR texcrd.x, texcrd.x;\n");
3305     shader_addline(buffer, "ADD texcrd.x, texcrd.x, coef.y;\n");
3306
3307     /* Divide the x coordinate by 0.5 and get the fraction. This gives 0.25 and 0.75 for the
3308      * even and odd pixels respectively
3309      */
3310     shader_addline(buffer, "MUL texcrd2, texcrd, coef.y;\n");
3311     shader_addline(buffer, "FRC texcrd2, texcrd2;\n");
3312
3313     /* Sample Pixel 1 */
3314     shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3315
3316     /* Put the value into either of the chroma values */
3317     shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3318     shader_addline(buffer, "MUL chroma.x, luminance.%c, temp.x;\n", chroma);
3319     shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3320     shader_addline(buffer, "MUL chroma.y, luminance.%c, temp.x;\n", chroma);
3321
3322     /* Sample pixel 2. If we read an even pixel(SLT above returned 1), sample
3323      * the pixel right to the current one. Otherwise, sample the left pixel.
3324      * Bias and scale the SLT result to -1;1 and add it to the texcrd.x.
3325      */
3326     shader_addline(buffer, "MAD temp.x, temp.x, coef.z, -coef.x;\n");
3327     shader_addline(buffer, "ADD texcrd.x, texcrd, temp.x;\n");
3328     shader_addline(buffer, "%s luminance, texcrd, texture[0], %s;\n", texinstr, tex);
3329
3330     /* Put the value into the other chroma */
3331     shader_addline(buffer, "SGE temp.x, texcrd2.x, coef.y;\n");
3332     shader_addline(buffer, "MAD chroma.y, luminance.%c, temp.x, chroma.y;\n", chroma);
3333     shader_addline(buffer, "SLT temp.x, texcrd2.x, coef.y;\n");
3334     shader_addline(buffer, "MAD chroma.x, luminance.%c, temp.x, chroma.x;\n", chroma);
3335
3336     /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
3337      * the current one and lerp the two U and V values
3338      */
3339
3340     /* This gives the correctly filtered luminance value */
3341     shader_addline(buffer, "TEX luminance, fragment.texcoord[0], texture[0], %s;\n", tex);
3342
3343     return TRUE;
3344 }
3345
3346 static BOOL gen_yv12_read(SHADER_BUFFER *buffer, GLenum textype, char *luminance)
3347 {
3348     const char *tex;
3349
3350     switch(textype) {
3351         case GL_TEXTURE_2D:             tex = "2D";     break;
3352         case GL_TEXTURE_RECTANGLE_ARB:  tex = "RECT";   break;
3353         default:
3354             FIXME("Implement yv12 correction for non-2d, non-rect textures\n");
3355             return FALSE;
3356     }
3357
3358     /* YV12 surfaces contain a WxH sized luminance plane, followed by a (W/2)x(H/2)
3359      * V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So the effective
3360      * bitdepth is 12 bits per pixel. Since the U and V planes have only half the
3361      * pitch of the luminance plane, the packing into the gl texture is a bit
3362      * unfortunate. If the whole texture is interpreted as luminance data it looks
3363      * approximately like this:
3364      *
3365      *        +----------------------------------+----
3366      *        |                                  |
3367      *        |                                  |
3368      *        |                                  |
3369      *        |                                  |
3370      *        |                                  |   2
3371      *        |            LUMINANCE             |   -
3372      *        |                                  |   3
3373      *        |                                  |
3374      *        |                                  |
3375      *        |                                  |
3376      *        |                                  |
3377      *        +----------------+-----------------+----
3378      *        |                |                 |
3379      *        |  U even rows   |  U odd rows     |
3380      *        |                |                 |   1
3381      *        +----------------+------------------   -
3382      *        |                |                 |   3
3383      *        |  V even rows   |  V odd rows     |
3384      *        |                |                 |
3385      *        +----------------+-----------------+----
3386      *        |                |                 |
3387      *        |     0.5        |       0.5       |
3388      *
3389      * So it appears as if there are 4 chroma images, but in fact the odd rows
3390      * in the chroma images are in the same row as the even ones. So its is
3391      * kinda tricky to read
3392      *
3393      * When reading from rectangle textures, keep in mind that the input y coordinates
3394      * go from 0 to d3d_height, whereas the opengl texture height is 1.5 * d3d_height
3395      */
3396     shader_addline(buffer, "PARAM yv12_coef = {%f, %f, %f, %f};\n",
3397                    2.0 / 3.0, 1.0 / 6.0, (2.0 / 3.0) + (1.0 / 6.0), 1.0 / 3.0);
3398
3399     shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3400     /* the chroma planes have only half the width */
3401     shader_addline(buffer, "MUL texcrd.x, texcrd.x, coef.y;\n");
3402
3403     /* The first value is between 2/3 and 5/6th of the texture's height, so scale+bias
3404      * the coordinate. Also read the right side of the image when reading odd lines
3405      *
3406      * Don't forget to clamp the y values in into the range, otherwise we'll get filtering
3407      * bleeding
3408      */
3409     if(textype == GL_TEXTURE_2D) {
3410
3411         shader_addline(buffer, "RCP chroma.w, size.y;\n");
3412
3413         shader_addline(buffer, "MUL texcrd2.y, texcrd.y, size.y;\n");
3414
3415         shader_addline(buffer, "FLR texcrd2.y, texcrd2.y;\n");
3416         shader_addline(buffer, "MAD texcrd.y, texcrd.y, yv12_coef.y, yv12_coef.x;\n");
3417
3418         /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3419         shader_addline(buffer, "ADD texcrd2.x, texcrd2.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3420         shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3421         shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3422         shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3423
3424         /* clamp, keep the half pixel origin in mind */
3425         shader_addline(buffer, "MAD temp.y, coef.y, chroma.w, yv12_coef.x;\n");
3426         shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3427         shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.z;\n");
3428         shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3429     } else {
3430         /* Read from [size - size+size/4] */
3431         shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3432         shader_addline(buffer, "MAD texcrd.y, texcrd.y, coef.w, size.y;\n");
3433
3434         /* Read odd lines from the right side(add size * 0.5 to the x coordinate */
3435         shader_addline(buffer, "ADD texcrd2.x, texcrd.y, yv12_coef.y;\n"); /* To avoid 0.5 == 0.5 comparisons */
3436         shader_addline(buffer, "FRC texcrd2.x, texcrd2.x;\n");
3437         shader_addline(buffer, "SGE texcrd2.x, texcrd2.x, coef.y;\n");
3438         shader_addline(buffer, "MUL texcrd2.x, texcrd2.x, size.x;\n");
3439         shader_addline(buffer, "MAD texcrd.x, texcrd2.x, coef.y, texcrd.x;\n");
3440
3441         /* Make sure to read exactly from the pixel center */
3442         shader_addline(buffer, "FLR texcrd.y, texcrd.y;\n");
3443         shader_addline(buffer, "ADD texcrd.y, texcrd.y, coef.y;\n");
3444
3445         /* Clamp */
3446         shader_addline(buffer, "MAD temp.y, size.y, coef.w, size.y;\n");
3447         shader_addline(buffer, "ADD temp.y, temp.y, -coef.y;\n");
3448         shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3449         shader_addline(buffer, "ADD temp.y, size.y, -coef.y;\n");
3450         shader_addline(buffer, "MAX texcrd.y, temp.y, texcrd.y;\n");
3451     }
3452     /* Read the texture, put the result into the output register */
3453     shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3454     shader_addline(buffer, "MOV chroma.x, temp.w;\n");
3455
3456     /* The other chroma value is 1/6th of the texture lower, from 5/6th to 6/6th
3457      * No need to clamp because we're just reusing the already clamped value from above
3458      */
3459     if(textype == GL_TEXTURE_2D) {
3460         shader_addline(buffer, "ADD texcrd.y, texcrd.y, yv12_coef.y;\n");
3461     } else {
3462         shader_addline(buffer, "MAD texcrd.y, size.y, coef.w, texcrd.y;\n");
3463     }
3464     shader_addline(buffer, "TEX temp, texcrd, texture[0], %s;\n", tex);
3465     shader_addline(buffer, "MOV chroma.y, temp.w;\n");
3466
3467     /* Sample the luminance value. It is in the top 2/3rd of the texture, so scale the y coordinate.
3468      * Clamp the y coordinate to prevent the chroma values from bleeding into the sampled luminance
3469      * values due to filtering
3470      */
3471     shader_addline(buffer, "MOV texcrd, fragment.texcoord[0];\n");
3472     if(textype == GL_TEXTURE_2D) {
3473         /* Multiply the y coordinate by 2/3 and clamp it */
3474         shader_addline(buffer, "MUL texcrd.y, texcrd.y, yv12_coef.x;\n");
3475         shader_addline(buffer, "MAD temp.y, -coef.y, chroma.w, yv12_coef.x;\n");
3476         shader_addline(buffer, "MIN texcrd.y, temp.y, texcrd.y;\n");
3477         shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3478     } else {
3479         /* Reading from texture_rectangles is pretty straightforward, just use the unmodified
3480          * texture coordinate. It is still a good idea to clamp it though, since the opengl texture
3481          * is bigger
3482          */
3483         shader_addline(buffer, "ADD temp.x, size.y, -coef.y;\n");
3484         shader_addline(buffer, "MIN texcrd.y, texcrd.y, size.x;\n");
3485         shader_addline(buffer, "TEX luminance, texcrd, texture[0], %s;\n", tex);
3486     }
3487     *luminance = 'a';
3488
3489     return TRUE;
3490 }
3491
3492 static GLuint gen_yuv_shader(IWineD3DDeviceImpl *device, enum yuv_fixup yuv_fixup, GLenum textype)
3493 {
3494     GLenum shader;
3495     SHADER_BUFFER buffer;
3496     char luminance_component;
3497     struct arbfp_blit_priv *priv = (struct arbfp_blit_priv *) device->blit_priv;
3498
3499     /* Shader header */
3500     shader_buffer_init(&buffer);
3501
3502     ENTER_GL();
3503     GL_EXTCALL(glGenProgramsARB(1, &shader));
3504     checkGLcall("GL_EXTCALL(glGenProgramsARB(1, &shader))");
3505     GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3506     checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3507     LEAVE_GL();
3508     if(!shader) {
3509         shader_buffer_free(&buffer);
3510         return 0;
3511     }
3512
3513     /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit macropixel,
3514      * giving effectively 16 bit per pixel. The color consists of a luminance(Y) and
3515      * two chroma(U and V) values. Each macropixel has two luminance values, one for
3516      * each single pixel it contains, and one U and one V value shared between both
3517      * pixels.
3518      *
3519      * The data is loaded into an A8L8 texture. With YUY2, the luminance component
3520      * contains the luminance and alpha the chroma. With UYVY it is vice versa. Thus
3521      * take the format into account when generating the read swizzles
3522      *
3523      * Reading the Y value is straightforward - just sample the texture. The hardware
3524      * takes care of filtering in the horizontal and vertical direction.
3525      *
3526      * Reading the U and V values is harder. We have to avoid filtering horizontally,
3527      * because that would mix the U and V values of one pixel or two adjacent pixels.
3528      * Thus floor the texture coordinate and add 0.5 to get an unfiltered read,
3529      * regardless of the filtering setting. Vertical filtering works automatically
3530      * though - the U and V values of two rows are mixed nicely.
3531      *
3532      * Appart of avoiding filtering issues, the code has to know which value it just
3533      * read, and where it can find the other one. To determine this, it checks if
3534      * it sampled an even or odd pixel, and shifts the 2nd read accordingly.
3535      *
3536      * Handling horizontal filtering of U and V values requires reading a 2nd pair
3537      * of pixels, extracting U and V and mixing them. This is not implemented yet.
3538      *
3539      * An alternative implementation idea is to load the texture as A8R8G8B8 texture,
3540      * with width / 2. This way one read gives all 3 values, finding U and V is easy
3541      * in an unfiltered situation. Finding the luminance on the other hand requires
3542      * finding out if it is an odd or even pixel. The real drawback of this approach
3543      * is filtering. This would have to be emulated completely in the shader, reading
3544      * up two 2 packed pixels in up to 2 rows and interpolating both horizontally and
3545      * vertically. Beyond that it would require adjustments to the texture handling
3546      * code to deal with the width scaling
3547      */
3548     shader_addline(&buffer, "!!ARBfp1.0\n");
3549     shader_addline(&buffer, "TEMP luminance;\n");
3550     shader_addline(&buffer, "TEMP temp;\n");
3551     shader_addline(&buffer, "TEMP chroma;\n");
3552     shader_addline(&buffer, "TEMP texcrd;\n");
3553     shader_addline(&buffer, "TEMP texcrd2;\n");
3554     shader_addline(&buffer, "PARAM coef = {1.0, 0.5, 2.0, 0.25};\n");
3555     shader_addline(&buffer, "PARAM yuv_coef = {1.403, 0.344, 0.714, 1.770};\n");
3556     shader_addline(&buffer, "PARAM size = program.local[0];\n");
3557
3558     switch (yuv_fixup)
3559     {
3560         case YUV_FIXUP_UYVY:
3561         case YUV_FIXUP_YUY2:
3562             if (!gen_planar_yuv_read(&buffer, yuv_fixup, textype, &luminance_component))
3563             {
3564                 shader_buffer_free(&buffer);
3565                 return 0;
3566             }
3567             break;
3568
3569         case YUV_FIXUP_YV12:
3570             if (!gen_yv12_read(&buffer, textype, &luminance_component))
3571             {
3572                 shader_buffer_free(&buffer);
3573                 return 0;
3574             }
3575             break;
3576
3577         default:
3578             FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3579             shader_buffer_free(&buffer);
3580             return 0;
3581     }
3582
3583     /* Calculate the final result. Formula is taken from
3584      * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
3585      * ranges from -0.5 to 0.5
3586      */
3587     shader_addline(&buffer, "SUB chroma.xy, chroma, coef.y;\n");
3588
3589     shader_addline(&buffer, "MAD result.color.x, chroma.x, yuv_coef.x, luminance.%c;\n", luminance_component);
3590     shader_addline(&buffer, "MAD temp.x, -chroma.y, yuv_coef.y, luminance.%c;\n", luminance_component);
3591     shader_addline(&buffer, "MAD result.color.y, -chroma.x, yuv_coef.z, temp.x;\n");
3592     shader_addline(&buffer, "MAD result.color.z, chroma.y, yuv_coef.w, luminance.%c;\n", luminance_component);
3593     shader_addline(&buffer, "END\n");
3594
3595     ENTER_GL();
3596     GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(buffer.buffer), buffer.buffer));
3597
3598     if (glGetError() == GL_INVALID_OPERATION) {
3599         GLint pos;
3600         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
3601         FIXME("Fragment program error at position %d: %s\n", pos,
3602               debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
3603     }
3604     shader_buffer_free(&buffer);
3605     LEAVE_GL();
3606
3607     switch (yuv_fixup)
3608     {
3609         case YUV_FIXUP_YUY2:
3610             if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yuy2_rect_shader = shader;
3611             else priv->yuy2_2d_shader = shader;
3612             break;
3613
3614         case YUV_FIXUP_UYVY:
3615             if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->uyvy_rect_shader = shader;
3616             else priv->uyvy_2d_shader = shader;
3617             break;
3618
3619         case YUV_FIXUP_YV12:
3620             if (textype == GL_TEXTURE_RECTANGLE_ARB) priv->yv12_rect_shader = shader;
3621             else priv->yv12_2d_shader = shader;
3622             break;
3623     }
3624
3625     return shader;
3626 }
3627
3628 static HRESULT arbfp_blit_set(IWineD3DDevice *iface, WINED3DFORMAT fmt, GLenum textype, UINT width, UINT height) {
3629     GLenum shader;
3630     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3631     float size[4] = {width, height, 1, 1};
3632     struct arbfp_blit_priv *priv = (struct arbfp_blit_priv *) device->blit_priv;
3633     const struct GlPixelFormatDesc *glDesc;
3634     enum yuv_fixup yuv_fixup;
3635
3636     getFormatDescEntry(fmt, &GLINFO_LOCATION, &glDesc);
3637
3638     if (!is_yuv_fixup(glDesc->color_fixup))
3639     {
3640         TRACE("Fixup:\n");
3641         dump_color_fixup_desc(glDesc->color_fixup);
3642         /* Don't bother setting up a shader for unconverted formats */
3643         ENTER_GL();
3644         glEnable(textype);
3645         checkGLcall("glEnable(textype)");
3646         LEAVE_GL();
3647         return WINED3D_OK;
3648     }
3649
3650     yuv_fixup = get_yuv_fixup(glDesc->color_fixup);
3651
3652     switch(yuv_fixup)
3653     {
3654         case YUV_FIXUP_YUY2:
3655             shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yuy2_rect_shader : priv->yuy2_2d_shader;
3656             break;
3657
3658         case YUV_FIXUP_UYVY:
3659             shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->uyvy_rect_shader : priv->uyvy_2d_shader;
3660             break;
3661
3662         case YUV_FIXUP_YV12:
3663             shader = textype == GL_TEXTURE_RECTANGLE_ARB ? priv->yv12_rect_shader : priv->yv12_2d_shader;
3664             break;
3665
3666         default:
3667             FIXME("Unsupported YUV fixup %#x, not setting a shader\n", yuv_fixup);
3668             ENTER_GL();
3669             glEnable(textype);
3670             checkGLcall("glEnable(textype)");
3671             LEAVE_GL();
3672             return E_NOTIMPL;
3673     }
3674
3675     if (!shader) shader = gen_yuv_shader(device, yuv_fixup, textype);
3676
3677     ENTER_GL();
3678     glEnable(GL_FRAGMENT_PROGRAM_ARB);
3679     checkGLcall("glEnable(GL_FRAGMENT_PROGRAM_ARB)");
3680     GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader));
3681     checkGLcall("glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader)");
3682     GL_EXTCALL(glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, size));
3683     checkGLcall("glProgramLocalParameter4fvARB");
3684     LEAVE_GL();
3685
3686     return WINED3D_OK;
3687 }
3688
3689 static void arbfp_blit_unset(IWineD3DDevice *iface) {
3690     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
3691
3692     ENTER_GL();
3693     glDisable(GL_FRAGMENT_PROGRAM_ARB);
3694     checkGLcall("glDisable(GL_FRAGMENT_PROGRAM_ARB)");
3695     glDisable(GL_TEXTURE_2D);
3696     checkGLcall("glDisable(GL_TEXTURE_2D)");
3697     if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
3698         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3699         checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3700     }
3701     if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3702         glDisable(GL_TEXTURE_RECTANGLE_ARB);
3703         checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
3704     }
3705     LEAVE_GL();
3706 }
3707
3708 static BOOL arbfp_blit_color_fixup_supported(struct color_fixup_desc fixup)
3709 {
3710     enum yuv_fixup yuv_fixup;
3711
3712     if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3713     {
3714         TRACE("Checking support for fixup:\n");
3715         dump_color_fixup_desc(fixup);
3716     }
3717
3718     if (is_identity_fixup(fixup))
3719     {
3720         TRACE("[OK]\n");
3721         return TRUE;
3722     }
3723
3724     /* We only support YUV conversions. */
3725     if (!is_yuv_fixup(fixup))
3726     {
3727         TRACE("[FAILED]\n");
3728         return FALSE;
3729     }
3730
3731     yuv_fixup = get_yuv_fixup(fixup);
3732     switch(yuv_fixup)
3733     {
3734         case YUV_FIXUP_YUY2:
3735         case YUV_FIXUP_UYVY:
3736         case YUV_FIXUP_YV12:
3737             TRACE("[OK]\n");
3738             return TRUE;
3739
3740         default:
3741             FIXME("Unsupported YUV fixup %#x\n", yuv_fixup);
3742             TRACE("[FAILED]\n");
3743             return FALSE;
3744     }
3745 }
3746
3747 const struct blit_shader arbfp_blit = {
3748     arbfp_blit_alloc,
3749     arbfp_blit_free,
3750     arbfp_blit_set,
3751     arbfp_blit_unset,
3752     arbfp_blit_color_fixup_supported,
3753 };
3754
3755 #undef GLINFO_LOCATION