wined3d: Implement the varying map.
[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 void destroy_glsl_pshader(IWineD3DPixelShaderImpl *This) {
71     struct list *linked_programs = &This->baseShader.linked_programs;
72
73     TRACE("Deleting linked programs\n");
74     if (linked_programs->next) {
75         struct glsl_shader_prog_link *entry, *entry2;
76         LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
77             delete_glsl_program_entry(This->baseShader.device, entry);
78         }
79     }
80
81     TRACE("Deleting shader object %u\n", This->baseShader.prgId);
82     GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
83     checkGLcall("glDeleteObjectARB");
84 }
85
86 static ULONG  WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
87     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
88     ULONG ref;
89     TRACE("(%p) : Releasing from %d\n", This, This->ref);
90     ref = InterlockedDecrement(&This->ref);
91     if (ref == 0) {
92         if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
93             destroy_glsl_pshader(This);
94         }
95         shader_delete_constant_list(&This->baseShader.constantsF);
96         shader_delete_constant_list(&This->baseShader.constantsB);
97         shader_delete_constant_list(&This->baseShader.constantsI);
98         HeapFree(GetProcessHeap(), 0, This);
99     }
100     return ref;
101 }
102
103 /* *******************************************
104    IWineD3DPixelShader IWineD3DPixelShader parts follow
105    ******************************************* */
106
107 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
108     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
109
110     *parent = This->parent;
111     IUnknown_AddRef(*parent);
112     TRACE("(%p) : returning %p\n", This, *parent);
113     return WINED3D_OK;
114 }
115
116 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
117     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
118     IWineD3DDevice_AddRef(This->baseShader.device);
119     *pDevice = This->baseShader.device;
120     TRACE("(%p) returning %p\n", This, *pDevice);
121     return WINED3D_OK;
122 }
123
124
125 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
126   IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
127   TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
128
129   if (NULL == pData) {
130     *pSizeOfData = This->baseShader.functionLength;
131     return WINED3D_OK;
132   }
133   if (*pSizeOfData < This->baseShader.functionLength) {
134     /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
135      * than the required size we should write the required size and
136      * return D3DERR_MOREDATA. That's not actually true. */
137     return WINED3DERR_INVALIDCALL;
138   }
139   if (NULL == This->baseShader.function) { /* no function defined */
140     TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
141     (*(DWORD **) pData) = NULL;
142   } else {
143     if (This->baseShader.functionLength == 0) {
144
145     }
146     TRACE("(%p) : GetFunction copying to %p\n", This, pData);
147     memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
148   }
149   return WINED3D_OK;
150 }
151
152 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
153     /* Arithmethic */
154     {WINED3DSIO_NOP,  "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
155     {WINED3DSIO_MOV,  "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
156     {WINED3DSIO_ADD,  "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
157     {WINED3DSIO_SUB,  "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
158     {WINED3DSIO_MAD,  "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
159     {WINED3DSIO_MUL,  "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
160     {WINED3DSIO_RCP,  "rcp", "RCP",  1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
161     {WINED3DSIO_RSQ,  "rsq",  "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
162     {WINED3DSIO_DP3,  "dp3",  "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
163     {WINED3DSIO_DP4,  "dp4",  "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
164     {WINED3DSIO_MIN,  "min",  "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
165     {WINED3DSIO_MAX,  "max",  "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
166     {WINED3DSIO_SLT,  "slt",  "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
167     {WINED3DSIO_SGE,  "sge",  "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
168     {WINED3DSIO_ABS,  "abs",  "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
169     {WINED3DSIO_EXP,  "exp",  "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
170     {WINED3DSIO_LOG,  "log",  "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_log, 0, 0},
171     {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
172     {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
173     {WINED3DSIO_DST,  "dst",  "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
174     {WINED3DSIO_LRP,  "lrp",  "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
175     {WINED3DSIO_FRC,  "frc",  "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
176     {WINED3DSIO_CND,  "cnd",  NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
177     {WINED3DSIO_CMP,  "cmp",  NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
178     {WINED3DSIO_POW,  "pow",  "POW", 1, 3, pshader_hw_map2gl, shader_glsl_pow, 0, 0},
179     {WINED3DSIO_CRS,  "crs",  "XPD", 1, 3, pshader_hw_map2gl, shader_glsl_cross, 0, 0},
180     {WINED3DSIO_NRM,      "nrm",      NULL, 1, 2, shader_hw_nrm, shader_glsl_map2gl, 0, 0},
181     {WINED3DSIO_SINCOS,   "sincos",   NULL, 1, 4, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
182     {WINED3DSIO_SINCOS,   "sincos",   "SCS", 1, 2, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
183     {WINED3DSIO_DP2ADD,   "dp2add",   NULL, 1, 4, pshader_hw_dp2add, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
184     /* Matrix */
185     {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
186     {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
187     {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
188     {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
189     {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
190     /* Register declarations */
191     {WINED3DSIO_DCL,      "dcl",      NULL, 0, 2, NULL, NULL, 0, 0},
192     /* Flow control - requires GLSL or software shaders */
193     {WINED3DSIO_REP ,     "rep",      NULL, 0, 1, NULL, shader_glsl_rep,    WINED3DPS_VERSION(2,1), -1},
194     {WINED3DSIO_ENDREP,   "endrep",   NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(2,1), -1},
195     {WINED3DSIO_IF,       "if",       NULL, 0, 1, NULL, shader_glsl_if,     WINED3DPS_VERSION(2,1), -1},
196     {WINED3DSIO_IFC,      "ifc",      NULL, 0, 2, NULL, shader_glsl_ifc,    WINED3DPS_VERSION(2,1), -1},
197     {WINED3DSIO_ELSE,     "else",     NULL, 0, 0, NULL, shader_glsl_else,   WINED3DPS_VERSION(2,1), -1},
198     {WINED3DSIO_ENDIF,    "endif",    NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(2,1), -1},
199     {WINED3DSIO_BREAK,    "break",    NULL, 0, 0, NULL, shader_glsl_break,  WINED3DPS_VERSION(2,1), -1},
200     {WINED3DSIO_BREAKC,   "breakc",   NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
201     {WINED3DSIO_BREAKP,   "breakp",   GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
202     {WINED3DSIO_CALL,     "call",     NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
203     {WINED3DSIO_CALLNZ,   "callnz",   NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
204     {WINED3DSIO_LOOP,     "loop",     NULL, 0, 2, NULL, shader_glsl_loop,   WINED3DPS_VERSION(3,0), -1},
205     {WINED3DSIO_RET,      "ret",      NULL, 0, 0, NULL, NULL,               WINED3DPS_VERSION(2,1), -1},
206     {WINED3DSIO_ENDLOOP,  "endloop",  NULL, 0, 0, NULL, shader_glsl_end,    WINED3DPS_VERSION(3,0), -1},
207     {WINED3DSIO_LABEL,    "label",    NULL, 0, 1, NULL, shader_glsl_label,  WINED3DPS_VERSION(2,1), -1},
208     /* Constant definitions */
209     {WINED3DSIO_DEF,      "def",      "undefined",         1, 5, NULL, NULL, 0, 0},
210     {WINED3DSIO_DEFB,     "defb",     GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
211     {WINED3DSIO_DEFI,     "defi",     GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
212     /* Texture */
213     {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
214     {WINED3DSIO_TEXCOORD, "texcrd",   "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
215     {WINED3DSIO_TEXKILL,  "texkill",  "KIL",       1, 1, pshader_hw_texkill, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
216     {WINED3DSIO_TEX,      "tex",      "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
217     {WINED3DSIO_TEX,      "texld",    "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
218     {WINED3DSIO_TEX,      "texld",    "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
219     {WINED3DSIO_TEXBEM,   "texbem",   "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
220     {WINED3DSIO_TEXBEML,  "texbeml",  GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texbem, pshader_glsl_texbem, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
221     {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
222     {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
223     {WINED3DSIO_TEXREG2RGB,   "texreg2rgb",   "undefined", 1, 2, pshader_hw_texreg2rgb, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
224     {WINED3DSIO_TEXM3x2PAD,   "texm3x2pad",   "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
225     {WINED3DSIO_TEXM3x2TEX,   "texm3x2tex",   "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
226     {WINED3DSIO_TEXM3x3PAD,   "texm3x3pad",   "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
227     {WINED3DSIO_TEXM3x3DIFF,  "texm3x3diff",  GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
228     {WINED3DSIO_TEXM3x3SPEC,  "texm3x3spec",  "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
229     {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec",  "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
230     {WINED3DSIO_TEXM3x3TEX,   "texm3x3tex",   "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
231     {WINED3DSIO_TEXDP3TEX,    "texdp3tex",    NULL, 1, 2, pshader_hw_texdp3tex, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
232     {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texm3x2depth, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
233     {WINED3DSIO_TEXDP3,   "texdp3",   NULL, 1, 2, pshader_hw_texdp3, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
234     {WINED3DSIO_TEXM3x3,  "texm3x3",  NULL, 1, 2, pshader_hw_texm3x3, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
235     {WINED3DSIO_TEXDEPTH, "texdepth", NULL, 1, 1, pshader_hw_texdepth, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
236     {WINED3DSIO_BEM,      "bem",      "undefined",         1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
237     {WINED3DSIO_DSX,      "dsx",      NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
238     {WINED3DSIO_DSY,      "dsy",      NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
239     {WINED3DSIO_TEXLDD,   "texldd",   GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
240     {WINED3DSIO_SETP,     "setp",     GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
241     {WINED3DSIO_TEXLDL,   "texldl",   NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
242     {WINED3DSIO_PHASE,    "phase",    GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
243     {0,               NULL,       NULL,   0, 0, NULL,            NULL, 0, 0}
244 };
245
246 static void pshader_set_limits(
247       IWineD3DPixelShaderImpl *This) { 
248
249       This->baseShader.limits.attributes = 0;
250       This->baseShader.limits.address = 0;
251       This->baseShader.limits.packed_output = 0;
252
253       switch (This->baseShader.hex_version) {
254           case WINED3DPS_VERSION(1,0):
255           case WINED3DPS_VERSION(1,1):
256           case WINED3DPS_VERSION(1,2):
257           case WINED3DPS_VERSION(1,3): 
258                    This->baseShader.limits.temporary = 2;
259                    This->baseShader.limits.constant_float = 8;
260                    This->baseShader.limits.constant_int = 0;
261                    This->baseShader.limits.constant_bool = 0;
262                    This->baseShader.limits.texcoord = 4;
263                    This->baseShader.limits.sampler = 4;
264                    This->baseShader.limits.packed_input = 0;
265                    This->baseShader.limits.label = 0;
266                    break;
267
268           case WINED3DPS_VERSION(1,4):
269                    This->baseShader.limits.temporary = 6;
270                    This->baseShader.limits.constant_float = 8;
271                    This->baseShader.limits.constant_int = 0;
272                    This->baseShader.limits.constant_bool = 0;
273                    This->baseShader.limits.texcoord = 6;
274                    This->baseShader.limits.sampler = 6;
275                    This->baseShader.limits.packed_input = 0;
276                    This->baseShader.limits.label = 0;
277                    break;
278                
279           /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */ 
280           case WINED3DPS_VERSION(2,0):
281                    This->baseShader.limits.temporary = 32;
282                    This->baseShader.limits.constant_float = 32;
283                    This->baseShader.limits.constant_int = 16;
284                    This->baseShader.limits.constant_bool = 16;
285                    This->baseShader.limits.texcoord = 8;
286                    This->baseShader.limits.sampler = 16;
287                    This->baseShader.limits.packed_input = 0;
288                    break;
289
290           case WINED3DPS_VERSION(2,1):
291                    This->baseShader.limits.temporary = 32;
292                    This->baseShader.limits.constant_float = 32;
293                    This->baseShader.limits.constant_int = 16;
294                    This->baseShader.limits.constant_bool = 16;
295                    This->baseShader.limits.texcoord = 8;
296                    This->baseShader.limits.sampler = 16;
297                    This->baseShader.limits.packed_input = 0;
298                    This->baseShader.limits.label = 16;
299                    break;
300
301           case WINED3DPS_VERSION(3,0):
302                    This->baseShader.limits.temporary = 32;
303                    This->baseShader.limits.constant_float = 224;
304                    This->baseShader.limits.constant_int = 16;
305                    This->baseShader.limits.constant_bool = 16;
306                    This->baseShader.limits.texcoord = 0;
307                    This->baseShader.limits.sampler = 16;
308                    This->baseShader.limits.packed_input = 12;
309                    This->baseShader.limits.label = 16; /* FIXME: 2048 */
310                    break;
311
312           default: This->baseShader.limits.temporary = 32;
313                    This->baseShader.limits.constant_float = 32;
314                    This->baseShader.limits.constant_int = 16;
315                    This->baseShader.limits.constant_bool = 16;
316                    This->baseShader.limits.texcoord = 8;
317                    This->baseShader.limits.sampler = 16;
318                    This->baseShader.limits.packed_input = 0;
319                    This->baseShader.limits.label = 0;
320                    FIXME("Unrecognized pixel shader version %#x\n", 
321                        This->baseShader.hex_version);
322       }
323 }
324
325 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
326     or GLSL and send it to the card */
327 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
328     IWineD3DPixelShader *iface,
329     shader_reg_maps* reg_maps,
330     CONST DWORD *pFunction) {
331
332     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
333     SHADER_BUFFER buffer;
334
335 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
336         it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
337     if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
338         HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
339         This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
340         This->fixupVertexBufferSize = PGMSIZE;
341         This->fixupVertexBuffer[0] = 0;
342     }
343     buffer.buffer = This->device->fixupVertexBuffer;
344 #else
345     buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE); 
346 #endif
347     buffer.bsize = 0;
348     buffer.lineNo = 0;
349     buffer.newline = TRUE;
350
351     if (This->baseShader.shader_mode == SHADER_GLSL) {
352
353         /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
354         GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
355
356         if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
357             shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
358         }
359
360         /* Base Declarations */
361         shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
362
363         /* Pack 3.0 inputs */
364         if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0) &&
365             !use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
366
367             if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
368                 This->vertexprocessing = pretransformed;
369             } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
370                 This->vertexprocessing = fixedfunction;
371             } else {
372                 This->vertexprocessing = vertexshader;
373             }
374
375             pshader_glsl_input_pack(&buffer, This->semantics_in, iface);
376         }
377
378         /* Base Shader Body */
379         shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
380
381         /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
382         if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
383             /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
384             if(GL_SUPPORT(ARB_DRAW_BUFFERS))
385                 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
386             else
387                 shader_addline(&buffer, "gl_FragColor = R0;\n");
388         }
389
390         /* Pixel shader < 3.0 do not replace the fog stage.
391          * This implements linear fog computation and blending.
392          * TODO: non linear fog
393          * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
394          * -1/(e-s) and e/(e-s) respectively.
395          */
396         if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
397             shader_addline(&buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
398             if(GL_SUPPORT(ARB_DRAW_BUFFERS))
399                 shader_addline(&buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
400             else
401                 shader_addline(&buffer, "gl_FragColor.xyz = mix(gl_Fog.color.xyz, gl_FragColor.xyz, Fog);\n");
402         }
403         if(This->srgb_enabled) {
404             const char *fragcolor;
405
406             if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
407                 fragcolor = "gl_FragData[0]";
408             } else {
409                 fragcolor = "gl_FragColor";
410             }
411             shader_addline(&buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
412                             fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
413                             srgb_sub_high, srgb_sub_high, srgb_sub_high);
414             shader_addline(&buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
415             shader_addline(&buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
416             shader_addline(&buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
417             shader_addline(&buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
418             shader_addline(&buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
419         }
420
421         shader_addline(&buffer, "}\n");
422
423         TRACE("Compiling shader object %u\n", shader_obj);
424         GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
425         GL_EXTCALL(glCompileShaderARB(shader_obj));
426         print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
427
428         /* Store the shader object */
429         This->baseShader.prgId = shader_obj;
430
431     } else if (This->baseShader.shader_mode == SHADER_ARB) {
432         /*  Create the hw ARB shader */
433         shader_addline(&buffer, "!!ARBfp1.0\n");
434
435         shader_addline(&buffer, "TEMP TMP;\n");     /* Used in matrix ops */
436         shader_addline(&buffer, "TEMP TMP2;\n");    /* Used in matrix ops */
437         shader_addline(&buffer, "TEMP TA;\n");      /* Used for modifiers */
438         shader_addline(&buffer, "TEMP TB;\n");      /* Used for modifiers */
439         shader_addline(&buffer, "TEMP TC;\n");      /* Used for modifiers */
440         shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
441         shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
442         shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
443
444         /* Base Declarations */
445         shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
446
447         /* We need two variables for fog blending */
448         shader_addline(&buffer, "TEMP TMP_FOG;\n");
449         if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
450             shader_addline(&buffer, "TEMP TMP_COLOR;\n");
451         }
452
453         /* Base Shader Body */
454         shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
455
456         /* calculate fog and blend it
457          * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
458          * -1/(e-s) and e/(e-s) respectively.
459          */
460         shader_addline(&buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
461
462         if(This->srgb_enabled) {
463             if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
464                 shader_addline(&buffer, "LRP TMP_COLOR.rgb, TMP_FOG.x, R0, state.fog.color;\n");
465                 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
466             } else {
467                 shader_addline(&buffer, "LRP TMP_COLOR.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
468                 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
469             }
470             /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
471
472             /* Calculate the > 0.0031308 case */
473             shader_addline(&buffer, "POW TMP.x, TMP_COLOR.x, srgb_pow.x;\n");
474             shader_addline(&buffer, "POW TMP.y, TMP_COLOR.y, srgb_pow.y;\n");
475             shader_addline(&buffer, "POW TMP.z, TMP_COLOR.z, srgb_pow.z;\n");
476             shader_addline(&buffer, "MUL TMP, TMP, srgb_mul_hi;\n");
477             shader_addline(&buffer, "SUB TMP, TMP, srgb_sub_hi;\n");
478             /* Calculate the < case */
479             shader_addline(&buffer, "MUL TMP2, srgb_mul_low, TMP_COLOR;\n");
480             /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
481             shader_addline(&buffer, "SLT TA, srgb_comparison, TMP_COLOR;\n");
482             shader_addline(&buffer, "SGE TB, srgb_comparison, TMP_COLOR;\n");
483             /* Store the components > 0.0031308 in the destination */
484             shader_addline(&buffer, "MUL TMP_COLOR, TMP, TA;\n");
485             /* Add the components that are < 0.0031308 */
486             shader_addline(&buffer, "MAD result.color.xyz, TMP2, TB, TMP_COLOR;\n");
487             /* [0.0;1.0] clamping. Not needed, this is done implicitly */
488         } else {
489             if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
490                 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, R0, state.fog.color;\n");
491                 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
492             } else {
493                 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
494                 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
495             }
496         }
497
498         shader_addline(&buffer, "END\n"); 
499
500         /* TODO: change to resource.glObjectHandle or something like that */
501         GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
502
503         TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
504         GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
505
506         TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
507         /* Create the program and check for errors */
508         GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
509             buffer.bsize, buffer.buffer));
510
511         if (glGetError() == GL_INVALID_OPERATION) {
512             GLint errPos;
513             glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
514             FIXME("HW PixelShader Error at position %d: %s\n",
515                   errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
516             This->baseShader.prgId = -1;
517         }
518     }
519
520     This->needsbumpmat = reg_maps->bumpmat;
521
522 #if 1 /* if were using the data buffer of device then we don't need to free it */
523   HeapFree(GetProcessHeap(), 0, buffer.buffer);
524 #endif
525 }
526
527 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
528
529     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
530     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
531
532     TRACE("(%p) : pFunction %p\n", iface, pFunction);
533
534     /* First pass: trace shader */
535     shader_trace_init((IWineD3DBaseShader*) This, pFunction);
536     pshader_set_limits(This);
537
538     /* Initialize immediate constant lists */
539     list_init(&This->baseShader.constantsF);
540     list_init(&This->baseShader.constantsB);
541     list_init(&This->baseShader.constantsI);
542
543     if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
544         shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
545         HRESULT hr;
546         unsigned int i, j, highest_reg_used = 0, num_regs_used = 0;
547
548         /* Second pass: figure out which registers are used, what the semantics are, etc.. */
549         memset(reg_maps, 0, sizeof(shader_reg_maps));
550         hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
551             This->semantics_in, NULL, pFunction, NULL);
552         if (FAILED(hr)) return hr;
553         /* FIXME: validate reg_maps against OpenGL */
554
555         for(i = 0; i < MAX_REG_INPUT; i++) {
556             if(This->input_reg_used[i]) {
557                 num_regs_used++;
558                 highest_reg_used = i;
559             }
560         }
561
562         /* Don't do any register mapping magic if it is not needed, or if we can't
563          * achive anything anyway
564          */
565         if(highest_reg_used < (GL_LIMITS(glsl_varyings) / 4) ||
566            num_regs_used >= (GL_LIMITS(glsl_varyings) / 4) ) {
567             if(num_regs_used >= (GL_LIMITS(glsl_varyings) / 4)) {
568                 /* This happens with relative addressing. The input mapper function
569                  * warns about this if the higher registers are declared too, so
570                  * don't write a FIXME here
571                  */
572                 WARN("More varying registers used than supported\n");
573             }
574
575             for(i = 0; i < MAX_REG_INPUT; i++) {
576                 This->input_reg_map[i] = i;
577             }
578         } else {
579             j = 0;
580             for(i = 0; i < MAX_REG_INPUT; i++) {
581                 if(This->input_reg_used[i]) {
582                     This->input_reg_map[i] = j;
583                     j++;
584                 } else {
585                     This->input_reg_map[i] = -1;
586                 }
587             }
588         }
589     }
590
591     This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
592
593     TRACE("(%p) : Copying the function\n", This);
594     if (NULL != pFunction) {
595         void *function;
596
597         function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
598         if (!function) return E_OUTOFMEMORY;
599         memcpy(function, pFunction, This->baseShader.functionLength);
600         This->baseShader.function = function;
601     } else {
602         This->baseShader.function = NULL;
603     }
604
605     return WINED3D_OK;
606 }
607
608 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
609
610     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
611     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
612     CONST DWORD *function = This->baseShader.function;
613     UINT i, sampler;
614     IWineD3DBaseTextureImpl *texture;
615
616     TRACE("(%p) : function %p\n", iface, function);
617
618     /* We're already compiled, but check if any of the hardcoded stateblock assumptions
619      * changed.
620      */
621     if (This->baseShader.is_compiled) {
622         char srgbenabled = deviceImpl->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
623         for(i = 0; i < This->baseShader.num_sampled_samplers; i++) {
624             sampler = This->baseShader.sampled_samplers[i];
625             texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler];
626             if(texture && texture->baseTexture.shader_conversion_group != This->baseShader.sampled_format[sampler]) {
627                 WARN("Recompiling shader %p due to format change on sampler %d\n", This, sampler);
628                 WARN("Old format group %s, new is %s\n",
629                      debug_d3dformat(This->baseShader.sampled_format[sampler]),
630                      debug_d3dformat(texture->baseTexture.shader_conversion_group));
631                 goto recompile;
632             }
633         }
634
635         /* TODO: Check projected textures */
636         /* TODO: Check texture types(2D, Cube, 3D) */
637
638         if(srgbenabled != This->srgb_enabled && This->srgb_mode_hardcoded) {
639             WARN("Recompiling shader because srgb correction is different and hardcoded\n");
640             goto recompile;
641         }
642         if(This->baseShader.reg_maps.vpos && !This->vpos_uniform) {
643             if(This->render_offscreen != deviceImpl->render_offscreen ||
644                This->height != ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height) {
645                 WARN("Recompiling shader because vpos is used, hard compiled and changed\n");
646                 goto recompile;
647             }
648         }
649         if(This->baseShader.reg_maps.usesdsy && !This->vpos_uniform) {
650             if(This->render_offscreen ? 0 : 1 != deviceImpl->render_offscreen ? 0 : 1) {
651                 WARN("Recompiling shader because dsy is used, hard compiled and render_offscreen changed\n");
652                 goto recompile;
653             }
654         }
655         if(This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
656             if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed &&
657                  This->vertexprocessing != pretransformed) {
658                 WARN("Recompiling shader because pretransformed vertices are provided, which wasn't the case before\n");
659                 goto recompile;
660             } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device) &&
661                        This->vertexprocessing != fixedfunction) {
662                 WARN("Recompiling shader because fixed function vp is in use, which wasn't the case before\n");
663                 goto recompile;
664             } else if(This->vertexprocessing != vertexshader) {
665                 WARN("Recompiling shader because vertex shaders are in use, which wasn't the case before\n");
666                 goto recompile;
667             }
668         }
669
670         return WINED3D_OK;
671
672         recompile:
673         if(This->baseShader.recompile_count > 50) {
674             FIXME("Shader %p recompiled more than 50 times\n", This);
675         } else {
676             This->baseShader.recompile_count++;
677         }
678
679         if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
680             destroy_glsl_pshader(This);
681         }
682     }
683
684     /* We don't need to compile */
685     if (!function) {
686         This->baseShader.is_compiled = TRUE;
687         return WINED3D_OK;
688     }
689
690     if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
691         shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
692         HRESULT hr;
693
694         /* Second pass: figure out which registers are used, what the semantics are, etc.. */
695         memset(reg_maps, 0, sizeof(shader_reg_maps));
696         hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
697             This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
698         if (FAILED(hr)) return hr;
699         /* FIXME: validate reg_maps against OpenGL */
700     }
701
702     /* Reset fields tracking stateblock values being hardcoded in the shader */
703     This->baseShader.num_sampled_samplers = 0;
704
705     /* Generate the HW shader */
706     TRACE("(%p) : Generating hardware program\n", This);
707     IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
708
709     This->baseShader.is_compiled = TRUE;
710
711     return WINED3D_OK;
712 }
713
714 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
715 {
716     /*** IUnknown methods ***/
717     IWineD3DPixelShaderImpl_QueryInterface,
718     IWineD3DPixelShaderImpl_AddRef,
719     IWineD3DPixelShaderImpl_Release,
720     /*** IWineD3DBase methods ***/
721     IWineD3DPixelShaderImpl_GetParent,
722     /*** IWineD3DBaseShader methods ***/
723     IWineD3DPixelShaderImpl_SetFunction,
724     IWineD3DPixelShaderImpl_CompileShader,
725     /*** IWineD3DPixelShader methods ***/
726     IWineD3DPixelShaderImpl_GetDevice,
727     IWineD3DPixelShaderImpl_GetFunction
728 };