2 * shaders implementation
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
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.
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.
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
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
35 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
37 #define GLNAME_REQUIRE_GLSL ((const char *)1)
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
);
42 if (IsEqualGUID(riid
, &IID_IWineD3DPixelShader
)
43 || IsEqualGUID(riid
, &IID_IWineD3DBaseShader
)
44 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
45 || IsEqualGUID(riid
, &IID_IUnknown
))
47 IUnknown_AddRef(iface
);
52 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
58 static ULONG WINAPI
IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader
*iface
) {
59 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
60 ULONG refcount
= InterlockedIncrement(&This
->baseShader
.ref
);
62 TRACE("%p increasing refcount to %u\n", This
, refcount
);
67 static ULONG WINAPI
IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader
*iface
) {
68 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
69 ULONG refcount
= InterlockedDecrement(&This
->baseShader
.ref
);
71 TRACE("%p decreasing refcount to %u\n", This
, refcount
);
75 shader_cleanup((IWineD3DBaseShader
*)iface
);
76 HeapFree(GetProcessHeap(), 0, This
);
82 /* *******************************************
83 IWineD3DPixelShader IWineD3DPixelShader parts follow
84 ******************************************* */
86 static HRESULT WINAPI
IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader
*iface
, IUnknown
** parent
){
87 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
89 *parent
= This
->parent
;
90 IUnknown_AddRef(*parent
);
91 TRACE("(%p) : returning %p\n", This
, *parent
);
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
);
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
);
109 *pSizeOfData
= This
->baseShader
.functionLength
;
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
;
119 TRACE("(%p) : GetFunction copying to %p\n", This
, pData
);
120 memcpy(pData
, This
->baseShader
.function
, This
->baseShader
.functionLength
);
125 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins
[] = {
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 },
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 },
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 }
219 static void pshader_set_limits(
220 IWineD3DPixelShaderImpl
*This
) {
222 This
->baseShader
.limits
.attributes
= 0;
223 This
->baseShader
.limits
.address
= 0;
224 This
->baseShader
.limits
.packed_output
= 0;
226 switch (This
->baseShader
.reg_maps
.shader_version
)
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;
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;
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;
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;
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 */
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
);
299 static HRESULT WINAPI
IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader
*iface
, CONST DWORD
*pFunction
) {
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
;
307 TRACE("(%p) : pFunction %p\n", iface
, pFunction
);
309 /* First pass: trace shader */
310 if (TRACE_ON(d3d_shader
)) shader_trace_init(pFunction
, This
->baseShader
.shader_ins
);
312 /* Initialize immediate constant lists */
313 list_init(&This
->baseShader
.constantsF
);
314 list_init(&This
->baseShader
.constantsB
);
315 list_init(&This
->baseShader
.constantsI
);
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
;
322 pshader_set_limits(This
);
324 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
326 if (This
->input_reg_used
[i
])
329 highest_reg_used
= i
;
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))
338 if (num_regs_used
> (GL_LIMITS(glsl_varyings
) / 4))
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");
346 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
348 This
->input_reg_map
[i
] = i
;
351 This
->declared_in_count
= highest_reg_used
+ 1;
355 This
->declared_in_count
= 0;
356 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
358 if (This
->input_reg_used
[i
]) This
->input_reg_map
[i
] = This
->declared_in_count
++;
359 else This
->input_reg_map
[i
] = -1;
363 This
->baseShader
.load_local_constsF
= FALSE
;
365 This
->baseShader
.shader_mode
= deviceImpl
->ps_selected_mode
;
367 TRACE("(%p) : Copying the function\n", This
);
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
);
376 static void pixelshader_update_samplers(struct shader_reg_maps
*reg_maps
, IWineD3DBaseTexture
* const *textures
)
378 DWORD shader_version
= reg_maps
->shader_version
;
379 DWORD
*samplers
= reg_maps
->samplers
;
382 if (WINED3DSHADER_VERSION_MAJOR(shader_version
) != 1) return;
384 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
386 /* We don't sample from this sampler */
387 if (!samplers
[i
]) continue;
391 ERR("No texture bound to sampler %u, using 2D\n", i
);
392 samplers
[i
] = (0x1 << 31) | WINED3DSTT_2D
;
396 switch (IWineD3DBaseTexture_GetTextureDimensions(textures
[i
]))
398 case GL_TEXTURE_RECTANGLE_ARB
:
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
;
406 samplers
[i
] = (1 << 31) | WINED3DSTT_VOLUME
;
409 case GL_TEXTURE_CUBE_MAP_ARB
:
410 samplers
[i
] = (1 << 31) | WINED3DSTT_CUBE
;
414 FIXME("Unrecognized texture type %#x, using 2D\n",
415 IWineD3DBaseTexture_GetTextureDimensions(textures
[i
]));
416 samplers
[i
] = (0x1 << 31) | WINED3DSTT_2D
;
421 static GLuint
pixelshader_compile(IWineD3DPixelShaderImpl
*This
, const struct ps_compile_args
*args
)
423 CONST DWORD
*function
= This
->baseShader
.function
;
425 SHADER_BUFFER buffer
;
426 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
428 TRACE("(%p) : function %p\n", This
, function
);
430 pixelshader_update_samplers(&This
->baseShader
.reg_maps
,
431 ((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->stateBlock
->textures
);
433 /* Reset fields tracking stateblock values being hardcoded in the shader */
434 This
->baseShader
.num_sampled_samplers
= 0;
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
);
445 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl
=
447 /*** IUnknown methods ***/
448 IWineD3DPixelShaderImpl_QueryInterface
,
449 IWineD3DPixelShaderImpl_AddRef
,
450 IWineD3DPixelShaderImpl_Release
,
451 /*** IWineD3DBase methods ***/
452 IWineD3DPixelShaderImpl_GetParent
,
453 /*** IWineD3DBaseShader methods ***/
454 IWineD3DPixelShaderImpl_SetFunction
,
455 /*** IWineD3DPixelShader methods ***/
456 IWineD3DPixelShaderImpl_GetDevice
,
457 IWineD3DPixelShaderImpl_GetFunction
460 void find_ps_compile_args(IWineD3DPixelShaderImpl
*shader
, IWineD3DStateBlockImpl
*stateblock
, struct ps_compile_args
*args
) {
462 IWineD3DBaseTextureImpl
*tex
;
464 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set */
465 args
->srgb_correction
= stateblock
->renderState
[WINED3DRS_SRGBWRITEENABLE
] ? 1 : 0;
467 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
468 sampler
= shader
->baseShader
.sampled_samplers
[i
];
469 tex
= (IWineD3DBaseTextureImpl
*) stateblock
->textures
[sampler
];
471 args
->color_fixup
[sampler
] = COLOR_FIXUP_IDENTITY
;
474 args
->color_fixup
[sampler
] = tex
->baseTexture
.shader_color_fixup
;
476 if (shader
->baseShader
.reg_maps
.shader_version
>= WINED3DPS_VERSION(3,0))
478 if (((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.position_transformed
)
480 args
->vp_mode
= pretransformed
;
482 else if (use_vs(stateblock
))
484 args
->vp_mode
= vertexshader
;
486 args
->vp_mode
= fixedfunction
;
490 args
->vp_mode
= vertexshader
;
491 if(stateblock
->renderState
[WINED3DRS_FOGENABLE
]) {
492 switch(stateblock
->renderState
[WINED3DRS_FOGTABLEMODE
]) {
493 case WINED3DFOG_NONE
:
494 if (((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.position_transformed
495 || use_vs(stateblock
))
497 args
->fog
= FOG_LINEAR
;
500 switch(stateblock
->renderState
[WINED3DRS_FOGVERTEXMODE
]) {
501 case WINED3DFOG_NONE
: /* Drop through */
502 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
503 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
504 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
508 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
509 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
510 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
518 GLuint
find_gl_pshader(IWineD3DPixelShaderImpl
*shader
, const struct ps_compile_args
*args
)
522 struct ps_compiled_shader
*new_array
;
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 or a binary search
526 * (cache coherency etc)
528 for(i
= 0; i
< shader
->num_gl_shaders
; i
++) {
529 if(memcmp(&shader
->gl_shaders
[i
].args
, args
, sizeof(*args
)) == 0) {
530 return shader
->gl_shaders
[i
].prgId
;
534 TRACE("No matching GL shader found, compiling a new shader\n");
535 if(shader
->shader_array_size
== shader
->num_gl_shaders
) {
536 if(shader
->gl_shaders
) {
537 new_size
= shader
->shader_array_size
+ max(1, shader
->shader_array_size
/ 2);
538 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader
->gl_shaders
,
539 new_size
* sizeof(*shader
->gl_shaders
));
541 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
->gl_shaders
));
546 ERR("Out of memory\n");
549 shader
->gl_shaders
= new_array
;
550 shader
->shader_array_size
= new_size
;
553 shader
->gl_shaders
[shader
->num_gl_shaders
].args
= *args
;
554 shader
->gl_shaders
[shader
->num_gl_shaders
].prgId
= pixelshader_compile(shader
, args
);
555 return shader
->gl_shaders
[shader
->num_gl_shaders
++].prgId
;