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
10 * Copyright 2009 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
38 static HRESULT WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj) {
39 TRACE("iface %p, riid %s, ppobj %p\n", iface, debugstr_guid(riid), ppobj);
41 if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
42 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
43 || IsEqualGUID(riid, &IID_IWineD3DBase)
44 || IsEqualGUID(riid, &IID_IUnknown))
46 IUnknown_AddRef(iface);
51 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
57 static ULONG WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
58 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
59 ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
61 TRACE("%p increasing refcount to %u\n", This, refcount);
66 static ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
67 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
68 ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
70 TRACE("%p decreasing refcount to %u\n", This, refcount);
74 shader_cleanup((IWineD3DBaseShader *)iface);
75 This->baseShader.parent_ops->wined3d_object_destroyed(This->baseShader.parent);
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->baseShader.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 static void pshader_set_limits(IWineD3DPixelShaderImpl *This)
127 DWORD shader_version = WINED3D_SHADER_VERSION(This->baseShader.reg_maps.shader_version.major,
128 This->baseShader.reg_maps.shader_version.minor);
130 This->baseShader.limits.attributes = 0;
131 This->baseShader.limits.address = 0;
132 This->baseShader.limits.packed_output = 0;
134 switch (shader_version)
136 case WINED3D_SHADER_VERSION(1,0):
137 case WINED3D_SHADER_VERSION(1,1):
138 case WINED3D_SHADER_VERSION(1,2):
139 case WINED3D_SHADER_VERSION(1,3):
140 This->baseShader.limits.temporary = 2;
141 This->baseShader.limits.constant_float = 8;
142 This->baseShader.limits.constant_int = 0;
143 This->baseShader.limits.constant_bool = 0;
144 This->baseShader.limits.texcoord = 4;
145 This->baseShader.limits.sampler = 4;
146 This->baseShader.limits.packed_input = 0;
147 This->baseShader.limits.label = 0;
150 case WINED3D_SHADER_VERSION(1,4):
151 This->baseShader.limits.temporary = 6;
152 This->baseShader.limits.constant_float = 8;
153 This->baseShader.limits.constant_int = 0;
154 This->baseShader.limits.constant_bool = 0;
155 This->baseShader.limits.texcoord = 6;
156 This->baseShader.limits.sampler = 6;
157 This->baseShader.limits.packed_input = 0;
158 This->baseShader.limits.label = 0;
161 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
162 case WINED3D_SHADER_VERSION(2,0):
163 This->baseShader.limits.temporary = 32;
164 This->baseShader.limits.constant_float = 32;
165 This->baseShader.limits.constant_int = 16;
166 This->baseShader.limits.constant_bool = 16;
167 This->baseShader.limits.texcoord = 8;
168 This->baseShader.limits.sampler = 16;
169 This->baseShader.limits.packed_input = 0;
172 case WINED3D_SHADER_VERSION(2,1):
173 This->baseShader.limits.temporary = 32;
174 This->baseShader.limits.constant_float = 32;
175 This->baseShader.limits.constant_int = 16;
176 This->baseShader.limits.constant_bool = 16;
177 This->baseShader.limits.texcoord = 8;
178 This->baseShader.limits.sampler = 16;
179 This->baseShader.limits.packed_input = 0;
180 This->baseShader.limits.label = 16;
183 case WINED3D_SHADER_VERSION(4,0):
184 FIXME("Using 3.0 limits for 4.0 shader\n");
187 case WINED3D_SHADER_VERSION(3,0):
188 This->baseShader.limits.temporary = 32;
189 This->baseShader.limits.constant_float = 224;
190 This->baseShader.limits.constant_int = 16;
191 This->baseShader.limits.constant_bool = 16;
192 This->baseShader.limits.texcoord = 0;
193 This->baseShader.limits.sampler = 16;
194 This->baseShader.limits.packed_input = 12;
195 This->baseShader.limits.label = 16; /* FIXME: 2048 */
199 This->baseShader.limits.temporary = 32;
200 This->baseShader.limits.constant_float = 32;
201 This->baseShader.limits.constant_int = 16;
202 This->baseShader.limits.constant_bool = 16;
203 This->baseShader.limits.texcoord = 8;
204 This->baseShader.limits.sampler = 16;
205 This->baseShader.limits.packed_input = 0;
206 This->baseShader.limits.label = 0;
207 FIXME("Unrecognized pixel shader version %u.%u\n",
208 This->baseShader.reg_maps.shader_version.major,
209 This->baseShader.reg_maps.shader_version.minor);
213 static HRESULT pixelshader_set_function(IWineD3DPixelShaderImpl *shader,
214 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
216 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
217 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
218 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
219 shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
220 const struct wined3d_shader_frontend *fe;
223 TRACE("shader %p, byte_code %p, output_signature %p.\n", shader, byte_code, output_signature);
225 fe = shader_select_frontend(*byte_code);
228 FIXME("Unable to find frontend for shader.\n");
229 return WINED3DERR_INVALIDCALL;
231 shader->baseShader.frontend = fe;
232 shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
233 if (!shader->baseShader.frontend_data)
235 FIXME("Failed to initialize frontend.\n");
236 return WINED3DERR_INVALIDCALL;
239 /* First pass: trace shader */
240 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
242 /* Initialize immediate constant lists */
243 list_init(&shader->baseShader.constantsF);
244 list_init(&shader->baseShader.constantsB);
245 list_init(&shader->baseShader.constantsI);
247 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
248 hr = shader_get_registers_used((IWineD3DBaseShader *)shader, fe,
249 reg_maps, NULL, shader->input_signature, NULL,
250 byte_code, gl_info->max_pshader_constantsF);
251 if (FAILED(hr)) return hr;
253 pshader_set_limits(shader);
255 for (i = 0; i < MAX_REG_INPUT; ++i)
257 if (shader->input_reg_used[i])
260 highest_reg_used = i;
264 /* Don't do any register mapping magic if it is not needed, or if we can't
265 * achieve anything anyway */
266 if (highest_reg_used < (gl_info->max_glsl_varyings / 4)
267 || num_regs_used > (gl_info->max_glsl_varyings / 4))
269 if (num_regs_used > (gl_info->max_glsl_varyings / 4))
271 /* This happens with relative addressing. The input mapper function
272 * warns about this if the higher registers are declared too, so
273 * don't write a FIXME here */
274 WARN("More varying registers used than supported\n");
277 for (i = 0; i < MAX_REG_INPUT; ++i)
279 shader->input_reg_map[i] = i;
282 shader->declared_in_count = highest_reg_used + 1;
286 shader->declared_in_count = 0;
287 for (i = 0; i < MAX_REG_INPUT; ++i)
289 if (shader->input_reg_used[i]) shader->input_reg_map[i] = shader->declared_in_count++;
290 else shader->input_reg_map[i] = ~0U;
294 shader->baseShader.load_local_constsF = FALSE;
296 TRACE("(%p) : Copying byte code.\n", shader);
298 shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
299 if (!shader->baseShader.function) return E_OUTOFMEMORY;
300 memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
305 void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
307 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
310 if (reg_maps->shader_version.major != 1) return;
312 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
314 /* We don't sample from this sampler */
315 if (!sampler_type[i]) continue;
319 ERR("No texture bound to sampler %u, using 2D\n", i);
320 sampler_type[i] = WINED3DSTT_2D;
324 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
326 case GL_TEXTURE_RECTANGLE_ARB:
328 /* We have to select between texture rectangles and 2D textures later because 2.0 and
329 * 3.0 shaders only have WINED3DSTT_2D as well */
330 sampler_type[i] = WINED3DSTT_2D;
334 sampler_type[i] = WINED3DSTT_VOLUME;
337 case GL_TEXTURE_CUBE_MAP_ARB:
338 sampler_type[i] = WINED3DSTT_CUBE;
342 FIXME("Unrecognized texture type %#x, using 2D\n",
343 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
344 sampler_type[i] = WINED3DSTT_2D;
349 static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
351 /*** IUnknown methods ***/
352 IWineD3DPixelShaderImpl_QueryInterface,
353 IWineD3DPixelShaderImpl_AddRef,
354 IWineD3DPixelShaderImpl_Release,
355 /*** IWineD3DBase methods ***/
356 IWineD3DPixelShaderImpl_GetParent,
357 /*** IWineD3DBaseShader methods ***/
358 IWineD3DPixelShaderImpl_GetDevice,
359 IWineD3DPixelShaderImpl_GetFunction
360 /*** IWineD3DPixelShader methods ***/
363 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
365 IWineD3DBaseTextureImpl *tex;
367 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
368 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
371 for(i = 0; i < MAX_FRAGMENT_SAMPLERS; i++) {
372 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
373 tex = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
375 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
378 args->color_fixup[i] = tex->resource.format_desc->color_fixup;
380 /* Flag samplers that need NP2 texcoord fixup. */
381 if(!tex->baseTexture.pow2Matrix_identity) {
382 args->np2_fixup |= (1 << i);
385 if (shader->baseShader.reg_maps.shader_version.major >= 3)
387 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
389 args->vp_mode = pretransformed;
391 else if (use_vs(stateblock))
393 args->vp_mode = vertexshader;
395 args->vp_mode = fixedfunction;
399 args->vp_mode = vertexshader;
400 if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
401 switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
402 case WINED3DFOG_NONE:
403 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
404 || use_vs(stateblock))
406 args->fog = FOG_LINEAR;
409 switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
410 case WINED3DFOG_NONE: /* Drop through */
411 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
412 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
413 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
417 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
418 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
419 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
427 HRESULT pixelshader_init(IWineD3DPixelShaderImpl *shader, IWineD3DDeviceImpl *device,
428 const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
429 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
433 if (!byte_code) return WINED3DERR_INVALIDCALL;
435 shader->lpVtbl = &IWineD3DPixelShader_Vtbl;
436 shader_init(&shader->baseShader, device, parent, parent_ops);
438 hr = pixelshader_set_function(shader, byte_code, output_signature);
441 WARN("Failed to set function, hr %#x.\n", hr);
442 shader_cleanup((IWineD3DBaseShader *)shader);