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