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
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.
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.
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
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
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)
41 # define TRACE_VSVECTOR(name)
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)
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);
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);
70 static ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
71 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
73 TRACE("(%p) : Releasing from %d\n", This, This->ref);
74 ref = InterlockedDecrement(&This->ref);
76 /* SetPixelShader does not AddRef. If the bound pixel shader is destroyed, the pointer in the stateblock remains
77 * unchanged. Drawing again will most likely crash, even on windows. A problem can occur if the application creates
78 * a new pixel shader which resides at the same address. Then SetPixelShader will think it is a NOP change, and won't
81 * Do NOT call GetPixelShader here. This will addRef and cause a recursion. And do NOT set the pixel shader to NULL,
82 * Windows does not do that(Although no test exists since they'd crash randomly)
84 if(iface == ((IWineD3DDeviceImpl *) This->baseShader.device)->stateBlock->pixelShader) {
85 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *) This->baseShader.device, STATE_PIXELSHADER);
88 if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
89 struct list *linked_programs = &This->baseShader.linked_programs;
91 TRACE("Deleting linked programs\n");
92 if (linked_programs->next) {
93 struct glsl_shader_prog_link *entry, *entry2;
94 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
95 delete_glsl_program_entry(This->baseShader.device, entry);
99 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
100 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
101 checkGLcall("glDeleteObjectARB");
103 shader_delete_constant_list(&This->baseShader.constantsF);
104 shader_delete_constant_list(&This->baseShader.constantsB);
105 shader_delete_constant_list(&This->baseShader.constantsI);
106 HeapFree(GetProcessHeap(), 0, This);
111 /* *******************************************
112 IWineD3DPixelShader IWineD3DPixelShader parts follow
113 ******************************************* */
115 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
116 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
118 *parent = This->parent;
119 IUnknown_AddRef(*parent);
120 TRACE("(%p) : returning %p\n", This, *parent);
124 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
125 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
126 IWineD3DDevice_AddRef(This->baseShader.device);
127 *pDevice = This->baseShader.device;
128 TRACE("(%p) returning %p\n", This, *pDevice);
133 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
134 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
135 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
138 *pSizeOfData = This->baseShader.functionLength;
141 if (*pSizeOfData < This->baseShader.functionLength) {
142 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
143 * than the required size we should write the required size and
144 * return D3DERR_MOREDATA. That's not actually true. */
145 return WINED3DERR_INVALIDCALL;
147 if (NULL == This->baseShader.function) { /* no function defined */
148 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
149 (*(DWORD **) pData) = NULL;
151 if (This->baseShader.functionLength == 0) {
154 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
155 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
160 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
162 {WINED3DSIO_NOP, "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
163 {WINED3DSIO_MOV, "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
164 {WINED3DSIO_ADD, "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
165 {WINED3DSIO_SUB, "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
166 {WINED3DSIO_MAD, "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
167 {WINED3DSIO_MUL, "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
168 {WINED3DSIO_RCP, "rcp", "RCP", 1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
169 {WINED3DSIO_RSQ, "rsq", "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
170 {WINED3DSIO_DP3, "dp3", "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
171 {WINED3DSIO_DP4, "dp4", "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
172 {WINED3DSIO_MIN, "min", "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
173 {WINED3DSIO_MAX, "max", "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
174 {WINED3DSIO_SLT, "slt", "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
175 {WINED3DSIO_SGE, "sge", "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
176 {WINED3DSIO_ABS, "abs", "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
177 {WINED3DSIO_EXP, "exp", "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
178 {WINED3DSIO_LOG, "log", "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
179 {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
180 {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
181 {WINED3DSIO_DST, "dst", "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
182 {WINED3DSIO_LRP, "lrp", "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
183 {WINED3DSIO_FRC, "frc", "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
184 {WINED3DSIO_CND, "cnd", NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
185 {WINED3DSIO_CMP, "cmp", NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
186 {WINED3DSIO_POW, "pow", "POW", 1, 3, NULL, shader_glsl_pow, 0, 0},
187 {WINED3DSIO_CRS, "crs", "XPS", 1, 3, NULL, shader_glsl_cross, 0, 0},
188 /* TODO: xyz normalise can be performed as VS_ARB using one temporary register,
191 MUL vec.xyz, vec, tmp;
192 but I think this is better because it accounts for w properly.
197 {WINED3DSIO_NRM, "nrm", NULL, 1, 2, NULL, shader_glsl_map2gl, 0, 0},
198 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 4, NULL, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
199 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 2, NULL, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
200 /* TODO: dp2add can be made out of multiple instuctions */
201 {WINED3DSIO_DP2ADD, "dp2add", GLNAME_REQUIRE_GLSL, 1, 4, NULL, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
203 {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
204 {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
205 {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
206 {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
207 {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, NULL, shader_glsl_mnxn, 0, 0},
208 /* Register declarations */
209 {WINED3DSIO_DCL, "dcl", NULL, 0, 2, NULL, NULL, 0, 0},
210 /* Flow control - requires GLSL or software shaders */
211 {WINED3DSIO_REP , "rep", NULL, 0, 1, NULL, shader_glsl_rep, WINED3DPS_VERSION(2,1), -1},
212 {WINED3DSIO_ENDREP, "endrep", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
213 {WINED3DSIO_IF, "if", NULL, 0, 1, NULL, shader_glsl_if, WINED3DPS_VERSION(2,1), -1},
214 {WINED3DSIO_IFC, "ifc", NULL, 0, 2, NULL, shader_glsl_ifc, WINED3DPS_VERSION(2,1), -1},
215 {WINED3DSIO_ELSE, "else", NULL, 0, 0, NULL, shader_glsl_else, WINED3DPS_VERSION(2,1), -1},
216 {WINED3DSIO_ENDIF, "endif", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
217 {WINED3DSIO_BREAK, "break", NULL, 0, 0, NULL, shader_glsl_break, WINED3DPS_VERSION(2,1), -1},
218 {WINED3DSIO_BREAKC, "breakc", NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
219 {WINED3DSIO_BREAKP, "breakp", GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
220 {WINED3DSIO_CALL, "call", NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
221 {WINED3DSIO_CALLNZ, "callnz", NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
222 {WINED3DSIO_LOOP, "loop", NULL, 0, 2, NULL, shader_glsl_loop, WINED3DPS_VERSION(3,0), -1},
223 {WINED3DSIO_RET, "ret", NULL, 0, 0, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
224 {WINED3DSIO_ENDLOOP, "endloop", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(3,0), -1},
225 {WINED3DSIO_LABEL, "label", NULL, 0, 1, NULL, shader_glsl_label, WINED3DPS_VERSION(2,1), -1},
226 /* Constant definitions */
227 {WINED3DSIO_DEF, "def", "undefined", 1, 5, NULL, NULL, 0, 0},
228 {WINED3DSIO_DEFB, "defb", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
229 {WINED3DSIO_DEFI, "defi", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
231 {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
232 {WINED3DSIO_TEXCOORD, "texcrd", "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
233 {WINED3DSIO_TEXKILL, "texkill", "KIL", 1, 1, pshader_hw_map2gl, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
234 {WINED3DSIO_TEX, "tex", "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
235 {WINED3DSIO_TEX, "texld", "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
236 {WINED3DSIO_TEX, "texld", "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
237 {WINED3DSIO_TEXBEM, "texbem", "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
238 {WINED3DSIO_TEXBEML, "texbeml", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
239 {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
240 {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
241 {WINED3DSIO_TEXREG2RGB, "texreg2rgb", GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
242 {WINED3DSIO_TEXM3x2PAD, "texm3x2pad", "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
243 {WINED3DSIO_TEXM3x2TEX, "texm3x2tex", "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
244 {WINED3DSIO_TEXM3x3PAD, "texm3x3pad", "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
245 {WINED3DSIO_TEXM3x3DIFF, "texm3x3diff", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
246 {WINED3DSIO_TEXM3x3SPEC, "texm3x3spec", "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
247 {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec", "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
248 {WINED3DSIO_TEXM3x3TEX, "texm3x3tex", "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
249 {WINED3DSIO_TEXDP3TEX, "texdp3tex", GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
250 {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
251 {WINED3DSIO_TEXDP3, "texdp3", GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
252 {WINED3DSIO_TEXM3x3, "texm3x3", GLNAME_REQUIRE_GLSL, 1, 2, NULL, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
253 {WINED3DSIO_TEXDEPTH, "texdepth", GLNAME_REQUIRE_GLSL, 1, 1, NULL, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
254 {WINED3DSIO_BEM, "bem", "undefined", 1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
255 {WINED3DSIO_DSX, "dsx", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
256 {WINED3DSIO_DSY, "dsy", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
257 {WINED3DSIO_TEXLDD, "texldd", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
258 {WINED3DSIO_SETP, "setp", GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
259 {WINED3DSIO_TEXLDL, "texldl", NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
260 {WINED3DSIO_PHASE, "phase", GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
261 {0, NULL, NULL, 0, 0, NULL, NULL, 0, 0}
264 static void pshader_set_limits(
265 IWineD3DPixelShaderImpl *This) {
267 This->baseShader.limits.attributes = 0;
268 This->baseShader.limits.address = 0;
269 This->baseShader.limits.packed_output = 0;
271 switch (This->baseShader.hex_version) {
272 case WINED3DPS_VERSION(1,0):
273 case WINED3DPS_VERSION(1,1):
274 case WINED3DPS_VERSION(1,2):
275 case WINED3DPS_VERSION(1,3):
276 This->baseShader.limits.temporary = 2;
277 This->baseShader.limits.constant_float = 8;
278 This->baseShader.limits.constant_int = 0;
279 This->baseShader.limits.constant_bool = 0;
280 This->baseShader.limits.texcoord = 4;
281 This->baseShader.limits.sampler = 4;
282 This->baseShader.limits.packed_input = 0;
283 This->baseShader.limits.label = 0;
286 case WINED3DPS_VERSION(1,4):
287 This->baseShader.limits.temporary = 6;
288 This->baseShader.limits.constant_float = 8;
289 This->baseShader.limits.constant_int = 0;
290 This->baseShader.limits.constant_bool = 0;
291 This->baseShader.limits.texcoord = 6;
292 This->baseShader.limits.sampler = 6;
293 This->baseShader.limits.packed_input = 0;
294 This->baseShader.limits.label = 0;
297 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
298 case WINED3DPS_VERSION(2,0):
299 This->baseShader.limits.temporary = 32;
300 This->baseShader.limits.constant_float = 32;
301 This->baseShader.limits.constant_int = 16;
302 This->baseShader.limits.constant_bool = 16;
303 This->baseShader.limits.texcoord = 8;
304 This->baseShader.limits.sampler = 16;
305 This->baseShader.limits.packed_input = 0;
308 case WINED3DPS_VERSION(2,1):
309 This->baseShader.limits.temporary = 32;
310 This->baseShader.limits.constant_float = 32;
311 This->baseShader.limits.constant_int = 16;
312 This->baseShader.limits.constant_bool = 16;
313 This->baseShader.limits.texcoord = 8;
314 This->baseShader.limits.sampler = 16;
315 This->baseShader.limits.packed_input = 0;
316 This->baseShader.limits.label = 16;
319 case WINED3DPS_VERSION(3,0):
320 This->baseShader.limits.temporary = 32;
321 This->baseShader.limits.constant_float = 224;
322 This->baseShader.limits.constant_int = 16;
323 This->baseShader.limits.constant_bool = 16;
324 This->baseShader.limits.texcoord = 0;
325 This->baseShader.limits.sampler = 16;
326 This->baseShader.limits.packed_input = 12;
327 This->baseShader.limits.label = 16; /* FIXME: 2048 */
330 default: This->baseShader.limits.temporary = 32;
331 This->baseShader.limits.constant_float = 32;
332 This->baseShader.limits.constant_int = 16;
333 This->baseShader.limits.constant_bool = 16;
334 This->baseShader.limits.texcoord = 8;
335 This->baseShader.limits.sampler = 16;
336 This->baseShader.limits.packed_input = 0;
337 This->baseShader.limits.label = 0;
338 FIXME("Unrecognized pixel shader version %#x\n",
339 This->baseShader.hex_version);
343 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
344 or GLSL and send it to the card */
345 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
346 IWineD3DPixelShader *iface,
347 shader_reg_maps* reg_maps,
348 CONST DWORD *pFunction) {
350 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
351 SHADER_BUFFER buffer;
353 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
354 it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
355 if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
356 HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
357 This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
358 This->fixupVertexBufferSize = PGMSIZE;
359 This->fixupVertexBuffer[0] = 0;
361 buffer.buffer = This->device->fixupVertexBuffer;
363 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
367 buffer.newline = TRUE;
369 if (This->baseShader.shader_mode == SHADER_GLSL) {
371 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
372 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
374 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
375 shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
378 /* Base Declarations */
379 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
381 /* Pack 3.0 inputs */
382 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0))
383 pshader_glsl_input_pack(&buffer, This->semantics_in);
385 /* Base Shader Body */
386 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
388 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
389 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
390 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
391 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
392 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
394 shader_addline(&buffer, "gl_FragColor = R0;\n");
397 /* Pixel shader < 3.0 do not replace the fog stage.
398 * This implements linear fog computation and blending.
399 * TODO: non linear fog
400 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
401 * -1/(e-s) and e/(e-s) respectively.
403 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
404 shader_addline(&buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
405 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
406 shader_addline(&buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
408 shader_addline(&buffer, "gl_FragColor.xyz = mix(gl_Fog.color.xyz, gl_FragColor.xyz, Fog);\n");
411 shader_addline(&buffer, "}\n");
413 TRACE("Compiling shader object %u\n", shader_obj);
414 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
415 GL_EXTCALL(glCompileShaderARB(shader_obj));
416 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
418 /* Store the shader object */
419 This->baseShader.prgId = shader_obj;
421 } else if (This->baseShader.shader_mode == SHADER_ARB) {
422 /* Create the hw ARB shader */
423 shader_addline(&buffer, "!!ARBfp1.0\n");
425 shader_addline(&buffer, "TEMP TMP;\n"); /* Used in matrix ops */
426 shader_addline(&buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
427 shader_addline(&buffer, "TEMP TA;\n"); /* Used for modifiers */
428 shader_addline(&buffer, "TEMP TB;\n"); /* Used for modifiers */
429 shader_addline(&buffer, "TEMP TC;\n"); /* Used for modifiers */
430 shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
431 shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
432 shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
434 /* Base Declarations */
435 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
437 /* We need two variables for fog blending */
438 shader_addline(&buffer, "TEMP TMP_FOG;\n");
439 if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
440 shader_addline(&buffer, "TEMP TMP_COLOR;\n");
443 /* Base Shader Body */
444 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
446 /* calculate fog and blend it
447 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
448 * -1/(e-s) and e/(e-s) respectively.
450 shader_addline(&buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
451 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
452 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, R0, state.fog.color;\n");
453 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
455 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
456 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
459 shader_addline(&buffer, "END\n");
461 /* TODO: change to resource.glObjectHandle or something like that */
462 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
464 TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
465 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
467 TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
468 /* Create the program and check for errors */
469 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
470 buffer.bsize, buffer.buffer));
472 if (glGetError() == GL_INVALID_OPERATION) {
474 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
475 FIXME("HW PixelShader Error at position %d: %s\n",
476 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
477 This->baseShader.prgId = -1;
481 This->needsbumpmat = reg_maps->bumpmat;
483 #if 1 /* if were using the data buffer of device then we don't need to free it */
484 HeapFree(GetProcessHeap(), 0, buffer.buffer);
488 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
490 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
491 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
493 TRACE("(%p) : pFunction %p\n", iface, pFunction);
495 /* First pass: trace shader */
496 shader_trace_init((IWineD3DBaseShader*) This, pFunction);
497 pshader_set_limits(This);
499 /* Initialize immediate constant lists */
500 list_init(&This->baseShader.constantsF);
501 list_init(&This->baseShader.constantsB);
502 list_init(&This->baseShader.constantsI);
504 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
505 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
508 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
509 memset(reg_maps, 0, sizeof(shader_reg_maps));
510 hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
511 This->semantics_in, NULL, pFunction, NULL);
512 if (FAILED(hr)) return hr;
513 /* FIXME: validate reg_maps against OpenGL */
516 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
518 TRACE("(%p) : Copying the function\n", This);
519 if (NULL != pFunction) {
522 function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
523 if (!function) return E_OUTOFMEMORY;
524 memcpy(function, pFunction, This->baseShader.functionLength);
525 This->baseShader.function = function;
527 This->baseShader.function = NULL;
533 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
535 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
536 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
537 CONST DWORD *function = This->baseShader.function;
539 TRACE("(%p) : function %p\n", iface, function);
541 /* We're already compiled. */
542 if (This->baseShader.is_compiled) return WINED3D_OK;
544 /* We don't need to compile */
546 This->baseShader.is_compiled = TRUE;
550 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
551 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
554 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
555 memset(reg_maps, 0, sizeof(shader_reg_maps));
556 hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
557 This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
558 if (FAILED(hr)) return hr;
559 /* FIXME: validate reg_maps against OpenGL */
562 /* Generate the HW shader */
563 TRACE("(%p) : Generating hardware program\n", This);
564 IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
566 This->baseShader.is_compiled = TRUE;
571 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
573 /*** IUnknown methods ***/
574 IWineD3DPixelShaderImpl_QueryInterface,
575 IWineD3DPixelShaderImpl_AddRef,
576 IWineD3DPixelShaderImpl_Release,
577 /*** IWineD3DBase methods ***/
578 IWineD3DPixelShaderImpl_GetParent,
579 /*** IWineD3DBaseShader methods ***/
580 IWineD3DPixelShaderImpl_SetFunction,
581 IWineD3DPixelShaderImpl_CompileShader,
582 /*** IWineD3DPixelShader methods ***/
583 IWineD3DPixelShaderImpl_GetDevice,
584 IWineD3DPixelShaderImpl_GetFunction