shell32/tests: Use a different name for the return value.
[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 #define GLNAME_REQUIRE_GLSL  ((const char *)1)
38
39 static HRESULT  WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj) {
40     TRACE("iface %p, riid %s, ppobj %p\n", iface, debugstr_guid(riid), ppobj);
41
42     if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
43             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
44             || IsEqualGUID(riid, &IID_IWineD3DBase)
45             || IsEqualGUID(riid, &IID_IUnknown))
46     {
47         IUnknown_AddRef(iface);
48         *ppobj = iface;
49         return S_OK;
50     }
51
52     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
53
54     *ppobj = NULL;
55     return E_NOINTERFACE;
56 }
57
58 static ULONG  WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
59     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
60     ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
61
62     TRACE("%p increasing refcount to %u\n", This, refcount);
63
64     return refcount;
65 }
66
67 static ULONG  WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
68     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
69     ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
70
71     TRACE("%p decreasing refcount to %u\n", This, refcount);
72
73     if (!refcount)
74     {
75         shader_cleanup((IWineD3DBaseShader *)iface);
76         HeapFree(GetProcessHeap(), 0, This);
77     }
78
79     return refcount;
80 }
81
82 /* *******************************************
83    IWineD3DPixelShader IWineD3DPixelShader parts follow
84    ******************************************* */
85
86 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
87     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
88
89     *parent = This->parent;
90     IUnknown_AddRef(*parent);
91     TRACE("(%p) : returning %p\n", This, *parent);
92     return WINED3D_OK;
93 }
94
95 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
96     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
97     IWineD3DDevice_AddRef(This->baseShader.device);
98     *pDevice = This->baseShader.device;
99     TRACE("(%p) returning %p\n", This, *pDevice);
100     return WINED3D_OK;
101 }
102
103
104 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
105   IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
106   TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
107
108   if (NULL == pData) {
109     *pSizeOfData = This->baseShader.functionLength;
110     return WINED3D_OK;
111   }
112   if (*pSizeOfData < This->baseShader.functionLength) {
113     /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
114      * than the required size we should write the required size and
115      * return D3DERR_MOREDATA. That's not actually true. */
116     return WINED3DERR_INVALIDCALL;
117   }
118
119   TRACE("(%p) : GetFunction copying to %p\n", This, pData);
120   memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
121
122   return WINED3D_OK;
123 }
124
125 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
126     /* Arithmetic */
127     {WINED3DSIO_NOP,          "nop",          0, 0, WINED3DSIH_NOP,          0,                      0                     },
128     {WINED3DSIO_MOV,          "mov",          1, 2, WINED3DSIH_MOV,          0,                      0                     },
129     {WINED3DSIO_ADD,          "add",          1, 3, WINED3DSIH_ADD,          0,                      0                     },
130     {WINED3DSIO_SUB,          "sub",          1, 3, WINED3DSIH_SUB,          0,                      0                     },
131     {WINED3DSIO_MAD,          "mad",          1, 4, WINED3DSIH_MAD,          0,                      0                     },
132     {WINED3DSIO_MUL,          "mul",          1, 3, WINED3DSIH_MUL,          0,                      0                     },
133     {WINED3DSIO_RCP,          "rcp",          1, 2, WINED3DSIH_RCP,          0,                      0                     },
134     {WINED3DSIO_RSQ,          "rsq",          1, 2, WINED3DSIH_RSQ,          0,                      0                     },
135     {WINED3DSIO_DP3,          "dp3",          1, 3, WINED3DSIH_DP3,          0,                      0                     },
136     {WINED3DSIO_DP4,          "dp4",          1, 3, WINED3DSIH_DP4,          0,                      0                     },
137     {WINED3DSIO_MIN,          "min",          1, 3, WINED3DSIH_MIN,          0,                      0                     },
138     {WINED3DSIO_MAX,          "max",          1, 3, WINED3DSIH_MAX,          0,                      0                     },
139     {WINED3DSIO_SLT,          "slt",          1, 3, WINED3DSIH_SLT,          0,                      0                     },
140     {WINED3DSIO_SGE,          "sge",          1, 3, WINED3DSIH_SGE,          0,                      0                     },
141     {WINED3DSIO_ABS,          "abs",          1, 2, WINED3DSIH_ABS,          0,                      0                     },
142     {WINED3DSIO_EXP,          "exp",          1, 2, WINED3DSIH_EXP,          0,                      0                     },
143     {WINED3DSIO_LOG,          "log",          1, 2, WINED3DSIH_LOG,          0,                      0                     },
144     {WINED3DSIO_EXPP,         "expp",         1, 2, WINED3DSIH_EXPP,         0,                      0                     },
145     {WINED3DSIO_LOGP,         "logp",         1, 2, WINED3DSIH_LOGP,         0,                      0                     },
146     {WINED3DSIO_DST,          "dst",          1, 3, WINED3DSIH_DST,          0,                      0                     },
147     {WINED3DSIO_LRP,          "lrp",          1, 4, WINED3DSIH_LRP,          0,                      0                     },
148     {WINED3DSIO_FRC,          "frc",          1, 2, WINED3DSIH_FRC,          0,                      0                     },
149     {WINED3DSIO_CND,          "cnd",          1, 4, WINED3DSIH_CND,          WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
150     {WINED3DSIO_CMP,          "cmp",          1, 4, WINED3DSIH_CMP,          WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
151     {WINED3DSIO_POW,          "pow",          1, 3, WINED3DSIH_POW,          0,                      0                     },
152     {WINED3DSIO_CRS,          "crs",          1, 3, WINED3DSIH_CRS,          0,                      0                     },
153     {WINED3DSIO_NRM,          "nrm",          1, 2, WINED3DSIH_NRM,          0,                      0                     },
154     {WINED3DSIO_SINCOS,       "sincos",       1, 4, WINED3DSIH_SINCOS,       WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
155     {WINED3DSIO_SINCOS,       "sincos",       1, 2, WINED3DSIH_SINCOS,       WINED3DPS_VERSION(3,0), -1                    },
156     {WINED3DSIO_DP2ADD,       "dp2add",       1, 4, WINED3DSIH_DP2ADD,       WINED3DPS_VERSION(2,0), -1                    },
157     /* Matrix */
158     {WINED3DSIO_M4x4,         "m4x4",         1, 3, WINED3DSIH_M4x4,         0,                      0                     },
159     {WINED3DSIO_M4x3,         "m4x3",         1, 3, WINED3DSIH_M4x3,         0,                      0                     },
160     {WINED3DSIO_M3x4,         "m3x4",         1, 3, WINED3DSIH_M3x4,         0,                      0                     },
161     {WINED3DSIO_M3x3,         "m3x3",         1, 3, WINED3DSIH_M3x3,         0,                      0                     },
162     {WINED3DSIO_M3x2,         "m3x2",         1, 3, WINED3DSIH_M3x2,         0,                      0                     },
163     /* Register declarations */
164     {WINED3DSIO_DCL,          "dcl",          0, 2, WINED3DSIH_DCL,          0,                      0                     },
165     /* Flow control - requires GLSL or software shaders */
166     {WINED3DSIO_REP ,         "rep",          0, 1, WINED3DSIH_REP,          WINED3DPS_VERSION(2,1), -1                    },
167     {WINED3DSIO_ENDREP,       "endrep",       0, 0, WINED3DSIH_ENDREP,       WINED3DPS_VERSION(2,1), -1                    },
168     {WINED3DSIO_IF,           "if",           0, 1, WINED3DSIH_IF,           WINED3DPS_VERSION(2,1), -1                    },
169     {WINED3DSIO_IFC,          "ifc",          0, 2, WINED3DSIH_IFC,          WINED3DPS_VERSION(2,1), -1                    },
170     {WINED3DSIO_ELSE,         "else",         0, 0, WINED3DSIH_ELSE,         WINED3DPS_VERSION(2,1), -1                    },
171     {WINED3DSIO_ENDIF,        "endif",        0, 0, WINED3DSIH_ENDIF,        WINED3DPS_VERSION(2,1), -1                    },
172     {WINED3DSIO_BREAK,        "break",        0, 0, WINED3DSIH_BREAK,        WINED3DPS_VERSION(2,1), -1                    },
173     {WINED3DSIO_BREAKC,       "breakc",       0, 2, WINED3DSIH_BREAKC,       WINED3DPS_VERSION(2,1), -1                    },
174     {WINED3DSIO_BREAKP,       "breakp",       0, 1, WINED3DSIH_BREAKP,       0,                      0                     },
175     {WINED3DSIO_CALL,         "call",         0, 1, WINED3DSIH_CALL,         WINED3DPS_VERSION(2,1), -1                    },
176     {WINED3DSIO_CALLNZ,       "callnz",       0, 2, WINED3DSIH_CALLNZ,       WINED3DPS_VERSION(2,1), -1                    },
177     {WINED3DSIO_LOOP,         "loop",         0, 2, WINED3DSIH_LOOP,         WINED3DPS_VERSION(3,0), -1                    },
178     {WINED3DSIO_RET,          "ret",          0, 0, WINED3DSIH_RET,          WINED3DPS_VERSION(2,1), -1                    },
179     {WINED3DSIO_ENDLOOP,      "endloop",      0, 0, WINED3DSIH_ENDLOOP,      WINED3DPS_VERSION(3,0), -1                    },
180     {WINED3DSIO_LABEL,        "label",        0, 1, WINED3DSIH_LABEL,        WINED3DPS_VERSION(2,1), -1                    },
181     /* Constant definitions */
182     {WINED3DSIO_DEF,          "def",          1, 5, WINED3DSIH_DEF,          0,                      0                     },
183     {WINED3DSIO_DEFB,         "defb",         1, 2, WINED3DSIH_DEFB,         0,                      0                     },
184     {WINED3DSIO_DEFI,         "defi",         1, 5, WINED3DSIH_DEFI,         0,                      0                     },
185     /* Texture */
186     {WINED3DSIO_TEXCOORD,     "texcoord",     1, 1, WINED3DSIH_TEXCOORD,     0,                      WINED3DPS_VERSION(1,3)},
187     {WINED3DSIO_TEXCOORD,     "texcrd",       1, 2, WINED3DSIH_TEXCOORD,     WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
188     {WINED3DSIO_TEXKILL,      "texkill",      1, 1, WINED3DSIH_TEXKILL,      WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
189     {WINED3DSIO_TEX,          "tex",          1, 1, WINED3DSIH_TEX,          0,                      WINED3DPS_VERSION(1,3)},
190     {WINED3DSIO_TEX,          "texld",        1, 2, WINED3DSIH_TEX,          WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
191     {WINED3DSIO_TEX,          "texld",        1, 3, WINED3DSIH_TEX,          WINED3DPS_VERSION(2,0), -1                    },
192     {WINED3DSIO_TEXBEM,       "texbem",       1, 2, WINED3DSIH_TEXBEM,       0,                      WINED3DPS_VERSION(1,3)},
193     {WINED3DSIO_TEXBEML,      "texbeml",      1, 2, WINED3DSIH_TEXBEML,      WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
194     {WINED3DSIO_TEXREG2AR,    "texreg2ar",    1, 2, WINED3DSIH_TEXREG2AR,    WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
195     {WINED3DSIO_TEXREG2GB,    "texreg2gb",    1, 2, WINED3DSIH_TEXREG2GB,    WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
196     {WINED3DSIO_TEXREG2RGB,   "texreg2rgb",   1, 2, WINED3DSIH_TEXREG2RGB,   WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
197     {WINED3DSIO_TEXM3x2PAD,   "texm3x2pad",   1, 2, WINED3DSIH_TEXM3x2PAD,   WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
198     {WINED3DSIO_TEXM3x2TEX,   "texm3x2tex",   1, 2, WINED3DSIH_TEXM3x2TEX,   WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
199     {WINED3DSIO_TEXM3x3PAD,   "texm3x3pad",   1, 2, WINED3DSIH_TEXM3x3PAD,   WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
200     {WINED3DSIO_TEXM3x3DIFF,  "texm3x3diff",  1, 2, WINED3DSIH_TEXM3x3DIFF,  WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
201     {WINED3DSIO_TEXM3x3SPEC,  "texm3x3spec",  1, 3, WINED3DSIH_TEXM3x3SPEC,  WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
202     {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec", 1, 2, WINED3DSIH_TEXM3x3VSPEC, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
203     {WINED3DSIO_TEXM3x3TEX,   "texm3x3tex",   1, 2, WINED3DSIH_TEXM3x3TEX,   WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
204     {WINED3DSIO_TEXDP3TEX,    "texdp3tex",    1, 2, WINED3DSIH_TEXDP3TEX,    WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
205     {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", 1, 2, WINED3DSIH_TEXM3x2DEPTH, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
206     {WINED3DSIO_TEXDP3,       "texdp3",       1, 2, WINED3DSIH_TEXDP3,       WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
207     {WINED3DSIO_TEXM3x3,      "texm3x3",      1, 2, WINED3DSIH_TEXM3x3,      WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
208     {WINED3DSIO_TEXDEPTH,     "texdepth",     1, 1, WINED3DSIH_TEXDEPTH,     WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
209     {WINED3DSIO_BEM,          "bem",          1, 3, WINED3DSIH_BEM,          WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
210     {WINED3DSIO_DSX,          "dsx",          1, 2, WINED3DSIH_DSX,          WINED3DPS_VERSION(2,1), -1                    },
211     {WINED3DSIO_DSY,          "dsy",          1, 2, WINED3DSIH_DSY,          WINED3DPS_VERSION(2,1), -1                    },
212     {WINED3DSIO_TEXLDD,       "texldd",       1, 5, WINED3DSIH_TEXLDD,       WINED3DPS_VERSION(2,1), -1                    },
213     {WINED3DSIO_SETP,         "setp",         1, 3, WINED3DSIH_SETP,         0,                      0                     },
214     {WINED3DSIO_TEXLDL,       "texldl",       1, 3, WINED3DSIH_TEXLDL,       WINED3DPS_VERSION(3,0), -1                    },
215     {WINED3DSIO_PHASE,        "phase",        0, 0, WINED3DSIH_PHASE,        0,                      0                     },
216     {0,                       NULL,           0, 0, 0,                       0,                      0                     }
217 };
218
219 static void pshader_set_limits(
220       IWineD3DPixelShaderImpl *This) { 
221
222       This->baseShader.limits.attributes = 0;
223       This->baseShader.limits.address = 0;
224       This->baseShader.limits.packed_output = 0;
225
226       switch (This->baseShader.reg_maps.shader_version)
227       {
228           case WINED3DPS_VERSION(1,0):
229           case WINED3DPS_VERSION(1,1):
230           case WINED3DPS_VERSION(1,2):
231           case WINED3DPS_VERSION(1,3): 
232                    This->baseShader.limits.temporary = 2;
233                    This->baseShader.limits.constant_float = 8;
234                    This->baseShader.limits.constant_int = 0;
235                    This->baseShader.limits.constant_bool = 0;
236                    This->baseShader.limits.texcoord = 4;
237                    This->baseShader.limits.sampler = 4;
238                    This->baseShader.limits.packed_input = 0;
239                    This->baseShader.limits.label = 0;
240                    break;
241
242           case WINED3DPS_VERSION(1,4):
243                    This->baseShader.limits.temporary = 6;
244                    This->baseShader.limits.constant_float = 8;
245                    This->baseShader.limits.constant_int = 0;
246                    This->baseShader.limits.constant_bool = 0;
247                    This->baseShader.limits.texcoord = 6;
248                    This->baseShader.limits.sampler = 6;
249                    This->baseShader.limits.packed_input = 0;
250                    This->baseShader.limits.label = 0;
251                    break;
252                
253           /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */ 
254           case WINED3DPS_VERSION(2,0):
255                    This->baseShader.limits.temporary = 32;
256                    This->baseShader.limits.constant_float = 32;
257                    This->baseShader.limits.constant_int = 16;
258                    This->baseShader.limits.constant_bool = 16;
259                    This->baseShader.limits.texcoord = 8;
260                    This->baseShader.limits.sampler = 16;
261                    This->baseShader.limits.packed_input = 0;
262                    break;
263
264           case WINED3DPS_VERSION(2,1):
265                    This->baseShader.limits.temporary = 32;
266                    This->baseShader.limits.constant_float = 32;
267                    This->baseShader.limits.constant_int = 16;
268                    This->baseShader.limits.constant_bool = 16;
269                    This->baseShader.limits.texcoord = 8;
270                    This->baseShader.limits.sampler = 16;
271                    This->baseShader.limits.packed_input = 0;
272                    This->baseShader.limits.label = 16;
273                    break;
274
275           case WINED3DPS_VERSION(3,0):
276                    This->baseShader.limits.temporary = 32;
277                    This->baseShader.limits.constant_float = 224;
278                    This->baseShader.limits.constant_int = 16;
279                    This->baseShader.limits.constant_bool = 16;
280                    This->baseShader.limits.texcoord = 0;
281                    This->baseShader.limits.sampler = 16;
282                    This->baseShader.limits.packed_input = 12;
283                    This->baseShader.limits.label = 16; /* FIXME: 2048 */
284                    break;
285
286           default: This->baseShader.limits.temporary = 32;
287                    This->baseShader.limits.constant_float = 32;
288                    This->baseShader.limits.constant_int = 16;
289                    This->baseShader.limits.constant_bool = 16;
290                    This->baseShader.limits.texcoord = 8;
291                    This->baseShader.limits.sampler = 16;
292                    This->baseShader.limits.packed_input = 0;
293                    This->baseShader.limits.label = 0;
294                    FIXME("Unrecognized pixel shader version %#x\n",
295                            This->baseShader.reg_maps.shader_version);
296       }
297 }
298
299 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
300
301     IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
302     IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
303     unsigned int i, highest_reg_used = 0, num_regs_used = 0;
304     shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
305     HRESULT hr;
306
307     TRACE("(%p) : pFunction %p\n", iface, pFunction);
308
309     /* First pass: trace shader */
310     if (TRACE_ON(d3d_shader)) shader_trace_init(pFunction, This->baseShader.shader_ins);
311
312     /* Initialize immediate constant lists */
313     list_init(&This->baseShader.constantsF);
314     list_init(&This->baseShader.constantsB);
315     list_init(&This->baseShader.constantsI);
316
317     /* Second pass: figure out which registers are used, what the semantics are, etc.. */
318     memset(reg_maps, 0, sizeof(shader_reg_maps));
319     hr = shader_get_registers_used((IWineD3DBaseShader *)This, reg_maps, This->semantics_in, NULL, pFunction);
320     if (FAILED(hr)) return hr;
321
322     pshader_set_limits(This);
323
324     for (i = 0; i < MAX_REG_INPUT; ++i)
325     {
326         if (This->input_reg_used[i])
327         {
328             ++num_regs_used;
329             highest_reg_used = i;
330         }
331     }
332
333     /* Don't do any register mapping magic if it is not needed, or if we can't
334      * achieve anything anyway */
335     if (highest_reg_used < (GL_LIMITS(glsl_varyings) / 4)
336             || num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
337     {
338         if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
339         {
340             /* This happens with relative addressing. The input mapper function
341              * warns about this if the higher registers are declared too, so
342              * don't write a FIXME here */
343             WARN("More varying registers used than supported\n");
344         }
345
346         for (i = 0; i < MAX_REG_INPUT; ++i)
347         {
348             This->input_reg_map[i] = i;
349         }
350
351         This->declared_in_count = highest_reg_used + 1;
352     }
353     else
354     {
355         This->declared_in_count = 0;
356         for (i = 0; i < MAX_REG_INPUT; ++i)
357         {
358             if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
359             else This->input_reg_map[i] = -1;
360         }
361     }
362
363     This->baseShader.load_local_constsF = FALSE;
364
365     This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
366
367     TRACE("(%p) : Copying the function\n", This);
368
369     This->baseShader.function = HeapAlloc(GetProcessHeap(), 0, This->baseShader.functionLength);
370     if (!This->baseShader.function) return E_OUTOFMEMORY;
371     memcpy(This->baseShader.function, pFunction, This->baseShader.functionLength);
372
373     return WINED3D_OK;
374 }
375
376 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
377 {
378     DWORD shader_version = reg_maps->shader_version;
379     DWORD *samplers = reg_maps->samplers;
380     unsigned int i;
381
382     if (WINED3DSHADER_VERSION_MAJOR(shader_version) != 1) return;
383
384     for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
385     {
386         /* We don't sample from this sampler */
387         if (!samplers[i]) continue;
388
389         if (!textures[i])
390         {
391             ERR("No texture bound to sampler %u, using 2D\n", i);
392             samplers[i] = (0x1 << 31) | WINED3DSTT_2D;
393             continue;
394         }
395
396         switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
397         {
398             case GL_TEXTURE_RECTANGLE_ARB:
399             case GL_TEXTURE_2D:
400                 /* We have to select between texture rectangles and 2D textures later because 2.0 and
401                  * 3.0 shaders only have WINED3DSTT_2D as well */
402                 samplers[i] = (1 << 31) | WINED3DSTT_2D;
403                 break;
404
405             case GL_TEXTURE_3D:
406                 samplers[i] = (1 << 31) | WINED3DSTT_VOLUME;
407                 break;
408
409             case GL_TEXTURE_CUBE_MAP_ARB:
410                 samplers[i] = (1 << 31) | WINED3DSTT_CUBE;
411                 break;
412
413             default:
414                 FIXME("Unrecognized texture type %#x, using 2D\n",
415                         IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
416                 samplers[i] = (0x1 << 31) | WINED3DSTT_2D;
417         }
418     }
419 }
420
421 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
422 {
423     CONST DWORD *function = This->baseShader.function;
424     GLuint retval;
425     SHADER_BUFFER buffer;
426     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
427
428     TRACE("(%p) : function %p\n", This, function);
429
430     pixelshader_update_samplers(&This->baseShader.reg_maps,
431             ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
432
433     /* Reset fields tracking stateblock values being hardcoded in the shader */
434     This->baseShader.num_sampled_samplers = 0;
435
436     /* Generate the HW shader */
437     TRACE("(%p) : Generating hardware program\n", This);
438     shader_buffer_init(&buffer);
439     retval = device->shader_backend->shader_generate_pshader((IWineD3DPixelShader *)This, &buffer, args);
440     shader_buffer_free(&buffer);
441
442     This->baseShader.is_compiled = TRUE;
443
444     return retval;
445 }
446
447 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
448 {
449     /*** IUnknown methods ***/
450     IWineD3DPixelShaderImpl_QueryInterface,
451     IWineD3DPixelShaderImpl_AddRef,
452     IWineD3DPixelShaderImpl_Release,
453     /*** IWineD3DBase methods ***/
454     IWineD3DPixelShaderImpl_GetParent,
455     /*** IWineD3DBaseShader methods ***/
456     IWineD3DPixelShaderImpl_SetFunction,
457     /*** IWineD3DPixelShader methods ***/
458     IWineD3DPixelShaderImpl_GetDevice,
459     IWineD3DPixelShaderImpl_GetFunction
460 };
461
462 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
463     UINT i, sampler;
464     IWineD3DBaseTextureImpl *tex;
465
466     memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
467     args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
468
469     for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
470         sampler = shader->baseShader.sampled_samplers[i];
471         tex = (IWineD3DBaseTextureImpl *) stateblock->textures[sampler];
472         if(!tex) {
473             args->color_fixup[sampler] = COLOR_FIXUP_IDENTITY;
474             continue;
475         }
476         args->color_fixup[sampler] = tex->baseTexture.shader_color_fixup;
477     }
478     if (shader->baseShader.reg_maps.shader_version >= WINED3DPS_VERSION(3,0))
479     {
480         if(((IWineD3DDeviceImpl *) shader->baseShader.device)->strided_streams.u.s.position_transformed) {
481             args->vp_mode = pretransformed;
482         }
483         else if (use_vs(stateblock))
484         {
485             args->vp_mode = vertexshader;
486         } else {
487             args->vp_mode = fixedfunction;
488         }
489         args->fog = FOG_OFF;
490     } else {
491         args->vp_mode = vertexshader;
492         if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
493             switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
494                 case WINED3DFOG_NONE:
495                     if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.u.s.position_transformed
496                             || use_vs(stateblock))
497                     {
498                         args->fog = FOG_LINEAR;
499                         break;
500                     }
501                     switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
502                         case WINED3DFOG_NONE: /* Drop through */
503                         case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
504                         case WINED3DFOG_EXP:    args->fog = FOG_EXP;    break;
505                         case WINED3DFOG_EXP2:   args->fog = FOG_EXP2;   break;
506                     }
507                     break;
508
509                 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
510                 case WINED3DFOG_EXP:    args->fog = FOG_EXP;    break;
511                 case WINED3DFOG_EXP2:   args->fog = FOG_EXP2;   break;
512             }
513         } else {
514             args->fog = FOG_OFF;
515         }
516     }
517 }
518
519 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
520 {
521     UINT i;
522     struct ps_compiled_shader *old_array;
523
524     /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
525      * so a linear search is more performant than a hashmap
526      */
527     for(i = 0; i < shader->num_gl_shaders; i++) {
528         if(memcmp(&shader->gl_shaders[i].args, args, sizeof(*args)) == 0) {
529             return shader->gl_shaders[i].prgId;
530         }
531     }
532
533     TRACE("No matching GL shader found, compiling a new shader\n");
534     old_array = shader->gl_shaders;
535     if(old_array) {
536         shader->gl_shaders = HeapReAlloc(GetProcessHeap(), 0, old_array,
537                                          (shader->num_gl_shaders + 1) * sizeof(*shader->gl_shaders));
538     } else {
539         shader->gl_shaders = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
540     }
541
542     if(!shader->gl_shaders) {
543         ERR("Out of memory\n");
544         return 0;
545     }
546
547     shader->gl_shaders[shader->num_gl_shaders].args = *args;
548     shader->gl_shaders[shader->num_gl_shaders].prgId = pixelshader_compile(shader, args);
549     return shader->gl_shaders[shader->num_gl_shaders++].prgId;
550 }