gdiplus: Implement GdipGetFontUnit.
[wine] / dlls / wined3d / pixelshader.c
1 /*
2  * shaders implementation
3  *
4  * Copyright 2002-2003 Jason Edmeades
5  * Copyright 2002-2003 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  * Copyright 2006 Ivan Gyurdiev
9  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include "config.h"
27
28 #include <math.h>
29 #include <stdio.h>
30
31 #include "wined3d_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34
35 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
36
37 #if 0 /* Must not be 1 in cvs version */
38 # define PSTRACE(A) TRACE A
39 # define TRACE_VSVECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w)
40 #else
41 # define PSTRACE(A)
42 # define TRACE_VSVECTOR(name)
43 #endif
44
45 #define GLNAME_REQUIRE_GLSL  ((const char *)1)
46
47 static HRESULT  WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj) {
48     return IWineD3DBaseShaderImpl_QueryInterface((IWineD3DBaseShader *) iface, riid, ppobj);
49 }
50
51 static ULONG  WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
52     return IWineD3DBaseShaderImpl_AddRef((IWineD3DBaseShader *) iface);
53 }
54
55 static ULONG  WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
56     return IWineD3DBaseShaderImpl_Release((IWineD3DBaseShader *) iface);
57 }
58
59 /* *******************************************
60    IWineD3DPixelShader IWineD3DPixelShader parts follow
61    ******************************************* */
62
63 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
64     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
65
66     *parent = This->parent;
67     IUnknown_AddRef(*parent);
68     TRACE("(%p) : returning %p\n", This, *parent);
69     return WINED3D_OK;
70 }
71
72 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
73     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
74     IWineD3DDevice_AddRef(This->baseShader.device);
75     *pDevice = This->baseShader.device;
76     TRACE("(%p) returning %p\n", This, *pDevice);
77     return WINED3D_OK;
78 }
79
80
81 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
82   IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
83   TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
84
85   if (NULL == pData) {
86     *pSizeOfData = This->baseShader.functionLength;
87     return WINED3D_OK;
88   }
89   if (*pSizeOfData < This->baseShader.functionLength) {
90     /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
91      * than the required size we should write the required size and
92      * return D3DERR_MOREDATA. That's not actually true. */
93     return WINED3DERR_INVALIDCALL;
94   }
95   if (NULL == This->baseShader.function) { /* no function defined */
96     TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
97     (*(DWORD **) pData) = NULL;
98   } else {
99     if (This->baseShader.functionLength == 0) {
100
101     }
102     TRACE("(%p) : GetFunction copying to %p\n", This, pData);
103     memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
104   }
105   return WINED3D_OK;
106 }
107
108 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
109     /* Arithmetic */
110     {WINED3DSIO_NOP,  "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
111     {WINED3DSIO_MOV,  "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
112     {WINED3DSIO_ADD,  "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
113     {WINED3DSIO_SUB,  "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
114     {WINED3DSIO_MAD,  "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
115     {WINED3DSIO_MUL,  "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
116     {WINED3DSIO_RCP,  "rcp", "RCP",  1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
117     {WINED3DSIO_RSQ,  "rsq",  "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
118     {WINED3DSIO_DP3,  "dp3",  "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
119     {WINED3DSIO_DP4,  "dp4",  "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
120     {WINED3DSIO_MIN,  "min",  "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
121     {WINED3DSIO_MAX,  "max",  "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
122     {WINED3DSIO_SLT,  "slt",  "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
123     {WINED3DSIO_SGE,  "sge",  "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
124     {WINED3DSIO_ABS,  "abs",  "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
125     {WINED3DSIO_EXP,  "exp",  "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
126     {WINED3DSIO_LOG,  "log",  "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_log, 0, 0},
127     {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
128     {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
129     {WINED3DSIO_DST,  "dst",  "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
130     {WINED3DSIO_LRP,  "lrp",  "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
131     {WINED3DSIO_FRC,  "frc",  "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
132     {WINED3DSIO_CND,  "cnd",  NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
133     {WINED3DSIO_CMP,  "cmp",  NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
134     {WINED3DSIO_POW,  "pow",  "POW", 1, 3, pshader_hw_map2gl, shader_glsl_pow, 0, 0},
135     {WINED3DSIO_CRS,  "crs",  "XPD", 1, 3, pshader_hw_map2gl, shader_glsl_cross, 0, 0},
136     {WINED3DSIO_NRM,      "nrm",      NULL, 1, 2, shader_hw_nrm, shader_glsl_map2gl, 0, 0},
137     {WINED3DSIO_SINCOS,   "sincos",   NULL, 1, 4, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
138     {WINED3DSIO_SINCOS,   "sincos",   "SCS", 1, 2, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
139     {WINED3DSIO_DP2ADD,   "dp2add",   NULL, 1, 4, pshader_hw_dp2add, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
140     /* Matrix */
141     {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
142     {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
143     {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
144     {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
145     {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
146     /* Register declarations */
147     {WINED3DSIO_DCL,      "dcl",      NULL, 0, 2, NULL, NULL, 0, 0},
148     /* Flow control - requires GLSL or software shaders */
149     {WINED3DSIO_REP ,     "rep",      NULL, 0, 1, NULL, shader_glsl_rep,    WINED3DPS_VERSION(2,1), -1},
150     {WINED3DSIO_ENDREP,   "endrep",   NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(2,1), -1},
151     {WINED3DSIO_IF,       "if",       NULL, 0, 1, NULL, shader_glsl_if,     WINED3DPS_VERSION(2,1), -1},
152     {WINED3DSIO_IFC,      "ifc",      NULL, 0, 2, NULL, shader_glsl_ifc,    WINED3DPS_VERSION(2,1), -1},
153     {WINED3DSIO_ELSE,     "else",     NULL, 0, 0, NULL, shader_glsl_else,   WINED3DPS_VERSION(2,1), -1},
154     {WINED3DSIO_ENDIF,    "endif",    NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(2,1), -1},
155     {WINED3DSIO_BREAK,    "break",    NULL, 0, 0, NULL, shader_glsl_break,  WINED3DPS_VERSION(2,1), -1},
156     {WINED3DSIO_BREAKC,   "breakc",   NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
157     {WINED3DSIO_BREAKP,   "breakp",   GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
158     {WINED3DSIO_CALL,     "call",     NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
159     {WINED3DSIO_CALLNZ,   "callnz",   NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
160     {WINED3DSIO_LOOP,     "loop",     NULL, 0, 2, NULL, shader_glsl_loop,   WINED3DPS_VERSION(3,0), -1},
161     {WINED3DSIO_RET,      "ret",      NULL, 0, 0, NULL, NULL,               WINED3DPS_VERSION(2,1), -1},
162     {WINED3DSIO_ENDLOOP,  "endloop",  NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(3,0), -1},
163     {WINED3DSIO_LABEL,    "label",    NULL, 0, 1, NULL, shader_glsl_label,  WINED3DPS_VERSION(2,1), -1},
164     /* Constant definitions */
165     {WINED3DSIO_DEF,      "def",      "undefined",         1, 5, NULL, NULL, 0, 0},
166     {WINED3DSIO_DEFB,     "defb",     GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
167     {WINED3DSIO_DEFI,     "defi",     GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
168     /* Texture */
169     {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
170     {WINED3DSIO_TEXCOORD, "texcrd",   "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
171     {WINED3DSIO_TEXKILL,  "texkill",  "KIL",       1, 1, pshader_hw_texkill, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
172     {WINED3DSIO_TEX,      "tex",      "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
173     {WINED3DSIO_TEX,      "texld",    "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
174     {WINED3DSIO_TEX,      "texld",    "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
175     {WINED3DSIO_TEXBEM,   "texbem",   "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
176     {WINED3DSIO_TEXBEML,  "texbeml",  GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texbem, pshader_glsl_texbem, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
177     {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
178     {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
179     {WINED3DSIO_TEXREG2RGB,   "texreg2rgb",   "undefined", 1, 2, pshader_hw_texreg2rgb, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
180     {WINED3DSIO_TEXM3x2PAD,   "texm3x2pad",   "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
181     {WINED3DSIO_TEXM3x2TEX,   "texm3x2tex",   "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
182     {WINED3DSIO_TEXM3x3PAD,   "texm3x3pad",   "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
183     {WINED3DSIO_TEXM3x3DIFF,  "texm3x3diff",  GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
184     {WINED3DSIO_TEXM3x3SPEC,  "texm3x3spec",  "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
185     {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec",  "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
186     {WINED3DSIO_TEXM3x3TEX,   "texm3x3tex",   "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
187     {WINED3DSIO_TEXDP3TEX,    "texdp3tex",    NULL, 1, 2, pshader_hw_texdp3tex, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
188     {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texm3x2depth, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
189     {WINED3DSIO_TEXDP3,   "texdp3",   NULL, 1, 2, pshader_hw_texdp3, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
190     {WINED3DSIO_TEXM3x3,  "texm3x3",  NULL, 1, 2, pshader_hw_texm3x3, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
191     {WINED3DSIO_TEXDEPTH, "texdepth", NULL, 1, 1, pshader_hw_texdepth, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
192     {WINED3DSIO_BEM,      "bem",      "undefined",         1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
193     {WINED3DSIO_DSX,      "dsx",      NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
194     {WINED3DSIO_DSY,      "dsy",      NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
195     {WINED3DSIO_TEXLDD,   "texldd",   GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
196     {WINED3DSIO_SETP,     "setp",     GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
197     {WINED3DSIO_TEXLDL,   "texldl",   NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
198     {WINED3DSIO_PHASE,    "phase",    GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
199     {0,               NULL,       NULL,   0, 0, NULL,            NULL, 0, 0}
200 };
201
202 static void pshader_set_limits(
203       IWineD3DPixelShaderImpl *This) { 
204
205       This->baseShader.limits.attributes = 0;
206       This->baseShader.limits.address = 0;
207       This->baseShader.limits.packed_output = 0;
208
209       switch (This->baseShader.hex_version) {
210           case WINED3DPS_VERSION(1,0):
211           case WINED3DPS_VERSION(1,1):
212           case WINED3DPS_VERSION(1,2):
213           case WINED3DPS_VERSION(1,3): 
214                    This->baseShader.limits.temporary = 2;
215                    This->baseShader.limits.constant_float = 8;
216                    This->baseShader.limits.constant_int = 0;
217                    This->baseShader.limits.constant_bool = 0;
218                    This->baseShader.limits.texcoord = 4;
219                    This->baseShader.limits.sampler = 4;
220                    This->baseShader.limits.packed_input = 0;
221                    This->baseShader.limits.label = 0;
222                    break;
223
224           case WINED3DPS_VERSION(1,4):
225                    This->baseShader.limits.temporary = 6;
226                    This->baseShader.limits.constant_float = 8;
227                    This->baseShader.limits.constant_int = 0;
228                    This->baseShader.limits.constant_bool = 0;
229                    This->baseShader.limits.texcoord = 6;
230                    This->baseShader.limits.sampler = 6;
231                    This->baseShader.limits.packed_input = 0;
232                    This->baseShader.limits.label = 0;
233                    break;
234                
235           /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */ 
236           case WINED3DPS_VERSION(2,0):
237                    This->baseShader.limits.temporary = 32;
238                    This->baseShader.limits.constant_float = 32;
239                    This->baseShader.limits.constant_int = 16;
240                    This->baseShader.limits.constant_bool = 16;
241                    This->baseShader.limits.texcoord = 8;
242                    This->baseShader.limits.sampler = 16;
243                    This->baseShader.limits.packed_input = 0;
244                    break;
245
246           case WINED3DPS_VERSION(2,1):
247                    This->baseShader.limits.temporary = 32;
248                    This->baseShader.limits.constant_float = 32;
249                    This->baseShader.limits.constant_int = 16;
250                    This->baseShader.limits.constant_bool = 16;
251                    This->baseShader.limits.texcoord = 8;
252                    This->baseShader.limits.sampler = 16;
253                    This->baseShader.limits.packed_input = 0;
254                    This->baseShader.limits.label = 16;
255                    break;
256
257           case WINED3DPS_VERSION(3,0):
258                    This->baseShader.limits.temporary = 32;
259                    This->baseShader.limits.constant_float = 224;
260                    This->baseShader.limits.constant_int = 16;
261                    This->baseShader.limits.constant_bool = 16;
262                    This->baseShader.limits.texcoord = 0;
263                    This->baseShader.limits.sampler = 16;
264                    This->baseShader.limits.packed_input = 12;
265                    This->baseShader.limits.label = 16; /* FIXME: 2048 */
266                    break;
267
268           default: This->baseShader.limits.temporary = 32;
269                    This->baseShader.limits.constant_float = 32;
270                    This->baseShader.limits.constant_int = 16;
271                    This->baseShader.limits.constant_bool = 16;
272                    This->baseShader.limits.texcoord = 8;
273                    This->baseShader.limits.sampler = 16;
274                    This->baseShader.limits.packed_input = 0;
275                    This->baseShader.limits.label = 0;
276                    FIXME("Unrecognized pixel shader version %#x\n", 
277                        This->baseShader.hex_version);
278       }
279 }
280
281 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
282     or GLSL and send it to the card */
283 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
284     IWineD3DPixelShader *iface) {
285     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
286     SHADER_BUFFER buffer;
287
288 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
289         it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
290     if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
291         HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
292         This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
293         This->fixupVertexBufferSize = PGMSIZE;
294         This->fixupVertexBuffer[0] = 0;
295     }
296     buffer.buffer = This->device->fixupVertexBuffer;
297 #else
298     buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE); 
299 #endif
300     buffer.bsize = 0;
301     buffer.lineNo = 0;
302     buffer.newline = TRUE;
303
304     ((IWineD3DDeviceImpl *)This->baseShader.device)->shader_backend->shader_generate_pshader(iface, &buffer);
305
306 #if 1 /* if were using the data buffer of device then we don't need to free it */
307   HeapFree(GetProcessHeap(), 0, buffer.buffer);
308 #endif
309 }
310
311 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
312
313     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
314     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
315
316     TRACE("(%p) : pFunction %p\n", iface, pFunction);
317
318     /* First pass: trace shader */
319     shader_trace_init((IWineD3DBaseShader*) This, pFunction);
320     pshader_set_limits(This);
321
322     /* Initialize immediate constant lists */
323     list_init(&This->baseShader.constantsF);
324     list_init(&This->baseShader.constantsB);
325     list_init(&This->baseShader.constantsI);
326
327     if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
328         shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
329         HRESULT hr;
330         unsigned int i, j, highest_reg_used = 0, num_regs_used = 0;
331
332         /* Second pass: figure out which registers are used, what the semantics are, etc.. */
333         memset(reg_maps, 0, sizeof(shader_reg_maps));
334         hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
335             This->semantics_in, NULL, pFunction, NULL);
336         if (FAILED(hr)) return hr;
337         /* FIXME: validate reg_maps against OpenGL */
338
339         for(i = 0; i < MAX_REG_INPUT; i++) {
340             if(This->input_reg_used[i]) {
341                 num_regs_used++;
342                 highest_reg_used = i;
343             }
344         }
345
346         /* Don't do any register mapping magic if it is not needed, or if we can't
347          * achieve anything anyway
348          */
349         if(highest_reg_used < (GL_LIMITS(glsl_varyings) / 4) ||
350            num_regs_used > (GL_LIMITS(glsl_varyings) / 4) ) {
351             if(num_regs_used > (GL_LIMITS(glsl_varyings) / 4)) {
352                 /* This happens with relative addressing. The input mapper function
353                  * warns about this if the higher registers are declared too, so
354                  * don't write a FIXME here
355                  */
356                 WARN("More varying registers used than supported\n");
357             }
358
359             for(i = 0; i < MAX_REG_INPUT; i++) {
360                 This->input_reg_map[i] = i;
361             }
362             This->declared_in_count = highest_reg_used + 1;
363         } else {
364             j = 0;
365             for(i = 0; i < MAX_REG_INPUT; i++) {
366                 if(This->input_reg_used[i]) {
367                     This->input_reg_map[i] = j;
368                     j++;
369                 } else {
370                     This->input_reg_map[i] = -1;
371                 }
372             }
373             This->declared_in_count = j;
374         }
375     }
376     This->baseShader.load_local_constsF = FALSE;
377
378     This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
379
380     TRACE("(%p) : Copying the function\n", This);
381     if (NULL != pFunction) {
382         void *function;
383
384         function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
385         if (!function) return E_OUTOFMEMORY;
386         memcpy(function, pFunction, This->baseShader.functionLength);
387         This->baseShader.function = function;
388     } else {
389         This->baseShader.function = NULL;
390     }
391
392     return WINED3D_OK;
393 }
394
395 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
396
397     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
398     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
399     CONST DWORD *function = This->baseShader.function;
400     UINT i, sampler;
401     IWineD3DBaseTextureImpl *texture;
402
403     TRACE("(%p) : function %p\n", iface, function);
404
405     /* We're already compiled, but check if any of the hardcoded stateblock assumptions
406      * changed.
407      */
408     if (This->baseShader.is_compiled) {
409         char srgbenabled = deviceImpl->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
410         for(i = 0; i < This->baseShader.num_sampled_samplers; i++) {
411             sampler = This->baseShader.sampled_samplers[i];
412             texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler];
413             if(texture && texture->baseTexture.shader_conversion_group != This->baseShader.sampled_format[sampler]) {
414                 WARN("Recompiling shader %p due to format change on sampler %d\n", This, sampler);
415                 WARN("Old format group %s, new is %s\n",
416                      debug_d3dformat(This->baseShader.sampled_format[sampler]),
417                      debug_d3dformat(texture->baseTexture.shader_conversion_group));
418                 goto recompile;
419             }
420         }
421
422         /* TODO: Check projected textures */
423         /* TODO: Check texture types(2D, Cube, 3D) */
424
425         if(srgbenabled != This->srgb_enabled && This->srgb_mode_hardcoded) {
426             WARN("Recompiling shader because srgb correction is different and hardcoded\n");
427             goto recompile;
428         }
429         if(This->baseShader.reg_maps.vpos && !This->vpos_uniform) {
430             if(This->render_offscreen != deviceImpl->render_offscreen ||
431                This->height != ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height) {
432                 WARN("Recompiling shader because vpos is used, hard compiled and changed\n");
433                 goto recompile;
434             }
435         }
436         if(This->baseShader.reg_maps.usesdsy && !This->vpos_uniform) {
437             if(This->render_offscreen ? 0 : 1 != deviceImpl->render_offscreen ? 0 : 1) {
438                 WARN("Recompiling shader because dsy is used, hard compiled and render_offscreen changed\n");
439                 goto recompile;
440             }
441         }
442         if(This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
443             if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
444                 if(This->vertexprocessing != pretransformed) {
445                     WARN("Recompiling shader because pretransformed vertices are provided, which wasn't the case before\n");
446                     goto recompile;
447                 }
448             } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device) &&
449                        This->vertexprocessing != fixedfunction) {
450                 WARN("Recompiling shader because fixed function vp is in use, which wasn't the case before\n");
451                 goto recompile;
452             } else if(This->vertexprocessing != vertexshader) {
453                 WARN("Recompiling shader because vertex shaders are in use, which wasn't the case before\n");
454                 goto recompile;
455             }
456         }
457
458         return WINED3D_OK;
459
460         recompile:
461         if(This->baseShader.recompile_count > 50) {
462             FIXME("Shader %p recompiled more than 50 times\n", This);
463         } else {
464             This->baseShader.recompile_count++;
465         }
466
467         deviceImpl->shader_backend->shader_destroy((IWineD3DBaseShader *) iface);
468     }
469
470     /* We don't need to compile */
471     if (!function) {
472         This->baseShader.is_compiled = TRUE;
473         return WINED3D_OK;
474     }
475
476     if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
477         shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
478         HRESULT hr;
479
480         /* Second pass: figure out which registers are used, what the semantics are, etc.. */
481         memset(reg_maps, 0, sizeof(shader_reg_maps));
482         hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
483             This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
484         if (FAILED(hr)) return hr;
485         /* FIXME: validate reg_maps against OpenGL */
486     }
487
488     /* Reset fields tracking stateblock values being hardcoded in the shader */
489     This->baseShader.num_sampled_samplers = 0;
490
491     /* Generate the HW shader */
492     TRACE("(%p) : Generating hardware program\n", This);
493     IWineD3DPixelShaderImpl_GenerateShader(iface);
494
495     This->baseShader.is_compiled = TRUE;
496
497     return WINED3D_OK;
498 }
499
500 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
501 {
502     /*** IUnknown methods ***/
503     IWineD3DPixelShaderImpl_QueryInterface,
504     IWineD3DPixelShaderImpl_AddRef,
505     IWineD3DPixelShaderImpl_Release,
506     /*** IWineD3DBase methods ***/
507     IWineD3DPixelShaderImpl_GetParent,
508     /*** IWineD3DBaseShader methods ***/
509     IWineD3DPixelShaderImpl_SetFunction,
510     IWineD3DPixelShaderImpl_CompileShader,
511     /*** IWineD3DPixelShader methods ***/
512     IWineD3DPixelShaderImpl_GetDevice,
513     IWineD3DPixelShaderImpl_GetFunction
514 };