wined3d: Pixel shader 1.x constants are clamped to [-1;1].
[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  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26
27 #include <math.h>
28 #include <stdio.h>
29
30 #include "wined3d_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
33
34 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
35
36 #if 0 /* Must not be 1 in cvs version */
37 # define PSTRACE(A) TRACE A
38 # define TRACE_VSVECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w)
39 #else
40 # define PSTRACE(A)
41 # define TRACE_VSVECTOR(name)
42 #endif
43
44 #define GLNAME_REQUIRE_GLSL  ((const char *)1)
45 /* *******************************************
46    IWineD3DPixelShader IUnknown parts follow
47    ******************************************* */
48 static HRESULT  WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj)
49 {
50     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
51     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
52     if (IsEqualGUID(riid, &IID_IUnknown)
53         || IsEqualGUID(riid, &IID_IWineD3DBase)
54         || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
55         || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
56         IUnknown_AddRef(iface);
57         *ppobj = This;
58         return S_OK;
59     }
60     *ppobj = NULL;
61     return E_NOINTERFACE;
62 }
63
64 static ULONG  WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
65     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
66     TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
67     return InterlockedIncrement(&This->ref);
68 }
69
70 static ULONG  WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
71     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
72     ULONG ref;
73     TRACE("(%p) : Releasing from %d\n", This, This->ref);
74     ref = InterlockedDecrement(&This->ref);
75     if (ref == 0) {
76         if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
77             struct list *linked_programs = &This->baseShader.linked_programs;
78
79             TRACE("Deleting linked programs\n");
80             if (linked_programs->next) {
81                 struct glsl_shader_prog_link *entry, *entry2;
82                 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
83                     delete_glsl_program_entry(This->baseShader.device, entry);
84                 }
85             }
86
87             TRACE("Deleting shader object %u\n", This->baseShader.prgId);
88             GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
89             checkGLcall("glDeleteObjectARB");
90         }
91         shader_delete_constant_list(&This->baseShader.constantsF);
92         shader_delete_constant_list(&This->baseShader.constantsB);
93         shader_delete_constant_list(&This->baseShader.constantsI);
94         HeapFree(GetProcessHeap(), 0, This);
95     }
96     return ref;
97 }
98
99 /* *******************************************
100    IWineD3DPixelShader IWineD3DPixelShader parts follow
101    ******************************************* */
102
103 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
104     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
105
106     *parent = This->parent;
107     IUnknown_AddRef(*parent);
108     TRACE("(%p) : returning %p\n", This, *parent);
109     return WINED3D_OK;
110 }
111
112 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
113     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
114     IWineD3DDevice_AddRef(This->baseShader.device);
115     *pDevice = This->baseShader.device;
116     TRACE("(%p) returning %p\n", This, *pDevice);
117     return WINED3D_OK;
118 }
119
120
121 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
122   IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
123   TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
124
125   if (NULL == pData) {
126     *pSizeOfData = This->baseShader.functionLength;
127     return WINED3D_OK;
128   }
129   if (*pSizeOfData < This->baseShader.functionLength) {
130     /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
131      * than the required size we should write the required size and
132      * return D3DERR_MOREDATA. That's not actually true. */
133     return WINED3DERR_INVALIDCALL;
134   }
135   if (NULL == This->baseShader.function) { /* no function defined */
136     TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
137     (*(DWORD **) pData) = NULL;
138   } else {
139     if (This->baseShader.functionLength == 0) {
140
141     }
142     TRACE("(%p) : GetFunction copying to %p\n", This, pData);
143     memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
144   }
145   return WINED3D_OK;
146 }
147
148 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
149     /* Arithmethic */
150     {WINED3DSIO_NOP,  "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
151     {WINED3DSIO_MOV,  "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
152     {WINED3DSIO_ADD,  "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
153     {WINED3DSIO_SUB,  "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
154     {WINED3DSIO_MAD,  "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
155     {WINED3DSIO_MUL,  "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
156     {WINED3DSIO_RCP,  "rcp", "RCP",  1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
157     {WINED3DSIO_RSQ,  "rsq",  "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
158     {WINED3DSIO_DP3,  "dp3",  "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
159     {WINED3DSIO_DP4,  "dp4",  "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
160     {WINED3DSIO_MIN,  "min",  "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
161     {WINED3DSIO_MAX,  "max",  "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
162     {WINED3DSIO_SLT,  "slt",  "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
163     {WINED3DSIO_SGE,  "sge",  "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
164     {WINED3DSIO_ABS,  "abs",  "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
165     {WINED3DSIO_EXP,  "exp",  "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
166     {WINED3DSIO_LOG,  "log",  "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
167     {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
168     {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
169     {WINED3DSIO_DST,  "dst",  "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
170     {WINED3DSIO_LRP,  "lrp",  "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
171     {WINED3DSIO_FRC,  "frc",  "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
172     {WINED3DSIO_CND,  "cnd",  NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
173     {WINED3DSIO_CMP,  "cmp",  NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
174     {WINED3DSIO_POW,  "pow",  "POW", 1, 3, NULL, shader_glsl_pow, 0, 0},
175     {WINED3DSIO_CRS,  "crs",  "XPS", 1, 3, NULL, shader_glsl_cross, 0, 0},
176     /* TODO: xyz normalise can be performed as VS_ARB using one temporary register,
177         DP3 tmp , vec, vec;
178         RSQ tmp, tmp.x;
179         MUL vec.xyz, vec, tmp;
180     but I think this is better because it accounts for w properly.
181         DP3 tmp , vec, vec;
182         RSQ tmp, tmp.x;
183         MUL vec, vec, tmp;
184     */
185     {WINED3DSIO_NRM,      "nrm",      NULL, 1, 2, NULL, shader_glsl_map2gl, 0, 0},
186     {WINED3DSIO_SINCOS,   "sincos",   NULL, 1, 4, NULL, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
187     {WINED3DSIO_SINCOS,   "sincos",   NULL, 1, 2, NULL, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
188     /* TODO: dp2add can be made out of multiple instuctions */
189     {WINED3DSIO_DP2ADD,   "dp2add",   GLNAME_REQUIRE_GLSL,  1, 4, NULL, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
190     /* Matrix */
191     {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
192     {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
193     {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
194     {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
195     {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
196     /* Register declarations */
197     {WINED3DSIO_DCL,      "dcl",      NULL, 0, 2, NULL, NULL, 0, 0},
198     /* Flow control - requires GLSL or software shaders */
199     {WINED3DSIO_REP ,     "rep",      NULL, 0, 1, NULL, shader_glsl_rep,    WINED3DPS_VERSION(2,1), -1},
200     {WINED3DSIO_ENDREP,   "endrep",   NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(2,1), -1},
201     {WINED3DSIO_IF,       "if",       NULL, 0, 1, NULL, shader_glsl_if,     WINED3DPS_VERSION(2,1), -1},
202     {WINED3DSIO_IFC,      "ifc",      NULL, 0, 2, NULL, shader_glsl_ifc,    WINED3DPS_VERSION(2,1), -1},
203     {WINED3DSIO_ELSE,     "else",     NULL, 0, 0, NULL, shader_glsl_else,   WINED3DPS_VERSION(2,1), -1},
204     {WINED3DSIO_ENDIF,    "endif",    NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(2,1), -1},
205     {WINED3DSIO_BREAK,    "break",    NULL, 0, 0, NULL, shader_glsl_break,  WINED3DPS_VERSION(2,1), -1},
206     {WINED3DSIO_BREAKC,   "breakc",   NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
207     {WINED3DSIO_BREAKP,   "breakp",   GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
208     {WINED3DSIO_CALL,     "call",     NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
209     {WINED3DSIO_CALLNZ,   "callnz",   NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
210     {WINED3DSIO_LOOP,     "loop",     NULL, 0, 2, NULL, shader_glsl_loop,   WINED3DPS_VERSION(3,0), -1},
211     {WINED3DSIO_RET,      "ret",      NULL, 0, 0, NULL, NULL,               WINED3DPS_VERSION(2,1), -1},
212     {WINED3DSIO_ENDLOOP,  "endloop",  NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(3,0), -1},
213     {WINED3DSIO_LABEL,    "label",    NULL, 0, 1, NULL, shader_glsl_label,  WINED3DPS_VERSION(2,1), -1},
214     /* Constant definitions */
215     {WINED3DSIO_DEF,      "def",      "undefined",         1, 5, NULL, NULL, 0, 0},
216     {WINED3DSIO_DEFB,     "defb",     GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
217     {WINED3DSIO_DEFI,     "defi",     GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
218     /* Texture */
219     {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
220     {WINED3DSIO_TEXCOORD, "texcrd",   "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
221     {WINED3DSIO_TEXKILL,  "texkill",  "KIL",       1, 1, pshader_hw_texkill, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
222     {WINED3DSIO_TEX,      "tex",      "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
223     {WINED3DSIO_TEX,      "texld",    "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
224     {WINED3DSIO_TEX,      "texld",    "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
225     {WINED3DSIO_TEXBEM,   "texbem",   "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
226     {WINED3DSIO_TEXBEML,  "texbeml",  GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texbem, pshader_glsl_texbem, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
227     {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
228     {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
229     {WINED3DSIO_TEXREG2RGB,   "texreg2rgb",   GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
230     {WINED3DSIO_TEXM3x2PAD,   "texm3x2pad",   "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
231     {WINED3DSIO_TEXM3x2TEX,   "texm3x2tex",   "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
232     {WINED3DSIO_TEXM3x3PAD,   "texm3x3pad",   "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
233     {WINED3DSIO_TEXM3x3DIFF,  "texm3x3diff",  GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
234     {WINED3DSIO_TEXM3x3SPEC,  "texm3x3spec",  "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
235     {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec",  "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
236     {WINED3DSIO_TEXM3x3TEX,   "texm3x3tex",   "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
237     {WINED3DSIO_TEXDP3TEX,    "texdp3tex",    GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
238     {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
239     {WINED3DSIO_TEXDP3,   "texdp3",   GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
240     {WINED3DSIO_TEXM3x3,  "texm3x3",  GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
241     {WINED3DSIO_TEXDEPTH, "texdepth", NULL, 1, 1, pshader_hw_texdepth, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
242     {WINED3DSIO_BEM,      "bem",      "undefined",         1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
243     {WINED3DSIO_DSX,      "dsx",      NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
244     {WINED3DSIO_DSY,      "dsy",      NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
245     {WINED3DSIO_TEXLDD,   "texldd",   GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
246     {WINED3DSIO_SETP,     "setp",     GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
247     {WINED3DSIO_TEXLDL,   "texldl",   NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
248     {WINED3DSIO_PHASE,    "phase",    GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
249     {0,               NULL,       NULL,   0, 0, NULL,            NULL, 0, 0}
250 };
251
252 static void pshader_set_limits(
253       IWineD3DPixelShaderImpl *This) { 
254
255       This->baseShader.limits.attributes = 0;
256       This->baseShader.limits.address = 0;
257       This->baseShader.limits.packed_output = 0;
258
259       switch (This->baseShader.hex_version) {
260           case WINED3DPS_VERSION(1,0):
261           case WINED3DPS_VERSION(1,1):
262           case WINED3DPS_VERSION(1,2):
263           case WINED3DPS_VERSION(1,3): 
264                    This->baseShader.limits.temporary = 2;
265                    This->baseShader.limits.constant_float = 8;
266                    This->baseShader.limits.constant_int = 0;
267                    This->baseShader.limits.constant_bool = 0;
268                    This->baseShader.limits.texcoord = 4;
269                    This->baseShader.limits.sampler = 4;
270                    This->baseShader.limits.packed_input = 0;
271                    This->baseShader.limits.label = 0;
272                    break;
273
274           case WINED3DPS_VERSION(1,4):
275                    This->baseShader.limits.temporary = 6;
276                    This->baseShader.limits.constant_float = 8;
277                    This->baseShader.limits.constant_int = 0;
278                    This->baseShader.limits.constant_bool = 0;
279                    This->baseShader.limits.texcoord = 6;
280                    This->baseShader.limits.sampler = 6;
281                    This->baseShader.limits.packed_input = 0;
282                    This->baseShader.limits.label = 0;
283                    break;
284                
285           /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */ 
286           case WINED3DPS_VERSION(2,0):
287                    This->baseShader.limits.temporary = 32;
288                    This->baseShader.limits.constant_float = 32;
289                    This->baseShader.limits.constant_int = 16;
290                    This->baseShader.limits.constant_bool = 16;
291                    This->baseShader.limits.texcoord = 8;
292                    This->baseShader.limits.sampler = 16;
293                    This->baseShader.limits.packed_input = 0;
294                    break;
295
296           case WINED3DPS_VERSION(2,1):
297                    This->baseShader.limits.temporary = 32;
298                    This->baseShader.limits.constant_float = 32;
299                    This->baseShader.limits.constant_int = 16;
300                    This->baseShader.limits.constant_bool = 16;
301                    This->baseShader.limits.texcoord = 8;
302                    This->baseShader.limits.sampler = 16;
303                    This->baseShader.limits.packed_input = 0;
304                    This->baseShader.limits.label = 16;
305                    break;
306
307           case WINED3DPS_VERSION(3,0):
308                    This->baseShader.limits.temporary = 32;
309                    This->baseShader.limits.constant_float = 224;
310                    This->baseShader.limits.constant_int = 16;
311                    This->baseShader.limits.constant_bool = 16;
312                    This->baseShader.limits.texcoord = 0;
313                    This->baseShader.limits.sampler = 16;
314                    This->baseShader.limits.packed_input = 12;
315                    This->baseShader.limits.label = 16; /* FIXME: 2048 */
316                    break;
317
318           default: This->baseShader.limits.temporary = 32;
319                    This->baseShader.limits.constant_float = 32;
320                    This->baseShader.limits.constant_int = 16;
321                    This->baseShader.limits.constant_bool = 16;
322                    This->baseShader.limits.texcoord = 8;
323                    This->baseShader.limits.sampler = 16;
324                    This->baseShader.limits.packed_input = 0;
325                    This->baseShader.limits.label = 0;
326                    FIXME("Unrecognized pixel shader version %#x\n", 
327                        This->baseShader.hex_version);
328       }
329 }
330
331 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
332     or GLSL and send it to the card */
333 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
334     IWineD3DPixelShader *iface,
335     shader_reg_maps* reg_maps,
336     CONST DWORD *pFunction) {
337
338     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
339     SHADER_BUFFER buffer;
340
341 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
342         it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
343     if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
344         HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
345         This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
346         This->fixupVertexBufferSize = PGMSIZE;
347         This->fixupVertexBuffer[0] = 0;
348     }
349     buffer.buffer = This->device->fixupVertexBuffer;
350 #else
351     buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE); 
352 #endif
353     buffer.bsize = 0;
354     buffer.lineNo = 0;
355     buffer.newline = TRUE;
356
357     if (This->baseShader.shader_mode == SHADER_GLSL) {
358
359         /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
360         GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
361
362         if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
363             shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
364         }
365
366         /* Base Declarations */
367         shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
368
369         /* Pack 3.0 inputs */
370         if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0))
371             pshader_glsl_input_pack(&buffer, This->semantics_in);
372
373         /* Base Shader Body */
374         shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
375
376         /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
377         if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
378             /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
379             if(GL_SUPPORT(ARB_DRAW_BUFFERS))
380                 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
381             else
382                 shader_addline(&buffer, "gl_FragColor = R0;\n");
383         }
384
385         /* Pixel shader < 3.0 do not replace the fog stage.
386          * This implements linear fog computation and blending.
387          * TODO: non linear fog
388          * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
389          * -1/(e-s) and e/(e-s) respectively.
390          */
391         if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
392             shader_addline(&buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
393             if(GL_SUPPORT(ARB_DRAW_BUFFERS))
394                 shader_addline(&buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
395             else
396                 shader_addline(&buffer, "gl_FragColor.xyz = mix(gl_Fog.color.xyz, gl_FragColor.xyz, Fog);\n");
397         }
398
399         shader_addline(&buffer, "}\n");
400
401         TRACE("Compiling shader object %u\n", shader_obj);
402         GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
403         GL_EXTCALL(glCompileShaderARB(shader_obj));
404         print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
405
406         /* Store the shader object */
407         This->baseShader.prgId = shader_obj;
408
409     } else if (This->baseShader.shader_mode == SHADER_ARB) {
410         /*  Create the hw ARB shader */
411         shader_addline(&buffer, "!!ARBfp1.0\n");
412
413         shader_addline(&buffer, "TEMP TMP;\n");     /* Used in matrix ops */
414         shader_addline(&buffer, "TEMP TMP2;\n");    /* Used in matrix ops */
415         shader_addline(&buffer, "TEMP TA;\n");      /* Used for modifiers */
416         shader_addline(&buffer, "TEMP TB;\n");      /* Used for modifiers */
417         shader_addline(&buffer, "TEMP TC;\n");      /* Used for modifiers */
418         shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
419         shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
420         shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
421
422         /* Base Declarations */
423         shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
424
425         /* We need two variables for fog blending */
426         shader_addline(&buffer, "TEMP TMP_FOG;\n");
427         if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
428             shader_addline(&buffer, "TEMP TMP_COLOR;\n");
429         }
430
431         /* Base Shader Body */
432         shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
433
434         /* calculate fog and blend it
435          * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
436          * -1/(e-s) and e/(e-s) respectively.
437          */
438         shader_addline(&buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
439         if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
440             shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, R0, state.fog.color;\n");
441             shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
442         } else {
443             shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
444             shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
445         }
446
447         shader_addline(&buffer, "END\n"); 
448
449         /* TODO: change to resource.glObjectHandle or something like that */
450         GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
451
452         TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
453         GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
454
455         TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
456         /* Create the program and check for errors */
457         GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
458             buffer.bsize, buffer.buffer));
459
460         if (glGetError() == GL_INVALID_OPERATION) {
461             GLint errPos;
462             glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
463             FIXME("HW PixelShader Error at position %d: %s\n",
464                   errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
465             This->baseShader.prgId = -1;
466         }
467     }
468
469     This->needsbumpmat = reg_maps->bumpmat;
470
471 #if 1 /* if were using the data buffer of device then we don't need to free it */
472   HeapFree(GetProcessHeap(), 0, buffer.buffer);
473 #endif
474 }
475
476 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
477
478     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
479     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
480
481     TRACE("(%p) : pFunction %p\n", iface, pFunction);
482
483     /* First pass: trace shader */
484     shader_trace_init((IWineD3DBaseShader*) This, pFunction);
485     pshader_set_limits(This);
486
487     /* Initialize immediate constant lists */
488     list_init(&This->baseShader.constantsF);
489     list_init(&This->baseShader.constantsB);
490     list_init(&This->baseShader.constantsI);
491
492     if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
493         shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
494         HRESULT hr;
495
496         /* Second pass: figure out which registers are used, what the semantics are, etc.. */
497         memset(reg_maps, 0, sizeof(shader_reg_maps));
498         hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
499             This->semantics_in, NULL, pFunction, NULL);
500         if (FAILED(hr)) return hr;
501         /* FIXME: validate reg_maps against OpenGL */
502     }
503
504     This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
505
506     TRACE("(%p) : Copying the function\n", This);
507     if (NULL != pFunction) {
508         void *function;
509
510         function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
511         if (!function) return E_OUTOFMEMORY;
512         memcpy(function, pFunction, This->baseShader.functionLength);
513         This->baseShader.function = function;
514     } else {
515         This->baseShader.function = NULL;
516     }
517
518     return WINED3D_OK;
519 }
520
521 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
522
523     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
524     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
525     CONST DWORD *function = This->baseShader.function;
526
527     TRACE("(%p) : function %p\n", iface, function);
528
529     /* We're already compiled. */
530     if (This->baseShader.is_compiled) return WINED3D_OK;
531
532     /* We don't need to compile */
533     if (!function) {
534         This->baseShader.is_compiled = TRUE;
535         return WINED3D_OK;
536     }
537
538     if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
539         shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
540         HRESULT hr;
541
542         /* Second pass: figure out which registers are used, what the semantics are, etc.. */
543         memset(reg_maps, 0, sizeof(shader_reg_maps));
544         hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
545             This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
546         if (FAILED(hr)) return hr;
547         /* FIXME: validate reg_maps against OpenGL */
548     }
549
550     /* Generate the HW shader */
551     TRACE("(%p) : Generating hardware program\n", This);
552     IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
553
554     This->baseShader.is_compiled = TRUE;
555
556     return WINED3D_OK;
557 }
558
559 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
560 {
561     /*** IUnknown methods ***/
562     IWineD3DPixelShaderImpl_QueryInterface,
563     IWineD3DPixelShaderImpl_AddRef,
564     IWineD3DPixelShaderImpl_Release,
565     /*** IWineD3DBase methods ***/
566     IWineD3DPixelShaderImpl_GetParent,
567     /*** IWineD3DBaseShader methods ***/
568     IWineD3DPixelShaderImpl_SetFunction,
569     IWineD3DPixelShaderImpl_CompileShader,
570     /*** IWineD3DPixelShader methods ***/
571     IWineD3DPixelShaderImpl_GetDevice,
572     IWineD3DPixelShaderImpl_GetFunction
573 };