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 static HRESULT WINAPI
IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader
*iface
, REFIID riid
, LPVOID
*ppobj
) {
37 TRACE("iface %p, riid %s, ppobj %p\n", iface
, debugstr_guid(riid
), ppobj
);
39 if (IsEqualGUID(riid
, &IID_IWineD3DPixelShader
)
40 || IsEqualGUID(riid
, &IID_IWineD3DBaseShader
)
41 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
42 || IsEqualGUID(riid
, &IID_IUnknown
))
44 IUnknown_AddRef(iface
);
49 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
55 static ULONG WINAPI
IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader
*iface
) {
56 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
57 ULONG refcount
= InterlockedIncrement(&This
->baseShader
.ref
);
59 TRACE("%p increasing refcount to %u\n", This
, refcount
);
64 static ULONG WINAPI
IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader
*iface
) {
65 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
66 ULONG refcount
= InterlockedDecrement(&This
->baseShader
.ref
);
68 TRACE("%p decreasing refcount to %u\n", This
, refcount
);
72 shader_cleanup((IWineD3DBaseShader
*)iface
);
73 This
->baseShader
.parent_ops
->wined3d_object_destroyed(This
->baseShader
.parent
);
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
->baseShader
.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 static void pshader_set_limits(IWineD3DPixelShaderImpl
*This
)
125 DWORD shader_version
= WINED3D_SHADER_VERSION(This
->baseShader
.reg_maps
.shader_version
.major
,
126 This
->baseShader
.reg_maps
.shader_version
.minor
);
128 This
->baseShader
.limits
.attributes
= 0;
129 This
->baseShader
.limits
.address
= 0;
130 This
->baseShader
.limits
.packed_output
= 0;
132 switch (shader_version
)
134 case WINED3D_SHADER_VERSION(1,0):
135 case WINED3D_SHADER_VERSION(1,1):
136 case WINED3D_SHADER_VERSION(1,2):
137 case WINED3D_SHADER_VERSION(1,3):
138 This
->baseShader
.limits
.temporary
= 2;
139 This
->baseShader
.limits
.constant_float
= 8;
140 This
->baseShader
.limits
.constant_int
= 0;
141 This
->baseShader
.limits
.constant_bool
= 0;
142 This
->baseShader
.limits
.texcoord
= 4;
143 This
->baseShader
.limits
.sampler
= 4;
144 This
->baseShader
.limits
.packed_input
= 0;
145 This
->baseShader
.limits
.label
= 0;
148 case WINED3D_SHADER_VERSION(1,4):
149 This
->baseShader
.limits
.temporary
= 6;
150 This
->baseShader
.limits
.constant_float
= 8;
151 This
->baseShader
.limits
.constant_int
= 0;
152 This
->baseShader
.limits
.constant_bool
= 0;
153 This
->baseShader
.limits
.texcoord
= 6;
154 This
->baseShader
.limits
.sampler
= 6;
155 This
->baseShader
.limits
.packed_input
= 0;
156 This
->baseShader
.limits
.label
= 0;
159 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
160 case WINED3D_SHADER_VERSION(2,0):
161 This
->baseShader
.limits
.temporary
= 32;
162 This
->baseShader
.limits
.constant_float
= 32;
163 This
->baseShader
.limits
.constant_int
= 16;
164 This
->baseShader
.limits
.constant_bool
= 16;
165 This
->baseShader
.limits
.texcoord
= 8;
166 This
->baseShader
.limits
.sampler
= 16;
167 This
->baseShader
.limits
.packed_input
= 0;
170 case WINED3D_SHADER_VERSION(2,1):
171 This
->baseShader
.limits
.temporary
= 32;
172 This
->baseShader
.limits
.constant_float
= 32;
173 This
->baseShader
.limits
.constant_int
= 16;
174 This
->baseShader
.limits
.constant_bool
= 16;
175 This
->baseShader
.limits
.texcoord
= 8;
176 This
->baseShader
.limits
.sampler
= 16;
177 This
->baseShader
.limits
.packed_input
= 0;
178 This
->baseShader
.limits
.label
= 16;
181 case WINED3D_SHADER_VERSION(4,0):
182 FIXME("Using 3.0 limits for 4.0 shader\n");
185 case WINED3D_SHADER_VERSION(3,0):
186 This
->baseShader
.limits
.temporary
= 32;
187 This
->baseShader
.limits
.constant_float
= 224;
188 This
->baseShader
.limits
.constant_int
= 16;
189 This
->baseShader
.limits
.constant_bool
= 16;
190 This
->baseShader
.limits
.texcoord
= 0;
191 This
->baseShader
.limits
.sampler
= 16;
192 This
->baseShader
.limits
.packed_input
= 12;
193 This
->baseShader
.limits
.label
= 16; /* FIXME: 2048 */
197 This
->baseShader
.limits
.temporary
= 32;
198 This
->baseShader
.limits
.constant_float
= 32;
199 This
->baseShader
.limits
.constant_int
= 16;
200 This
->baseShader
.limits
.constant_bool
= 16;
201 This
->baseShader
.limits
.texcoord
= 8;
202 This
->baseShader
.limits
.sampler
= 16;
203 This
->baseShader
.limits
.packed_input
= 0;
204 This
->baseShader
.limits
.label
= 0;
205 FIXME("Unrecognized pixel shader version %u.%u\n",
206 This
->baseShader
.reg_maps
.shader_version
.major
,
207 This
->baseShader
.reg_maps
.shader_version
.minor
);
211 static HRESULT
pixelshader_set_function(IWineD3DPixelShaderImpl
*shader
,
212 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
)
214 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
215 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
216 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
217 shader_reg_maps
*reg_maps
= &shader
->baseShader
.reg_maps
;
218 const struct wined3d_shader_frontend
*fe
;
221 TRACE("shader %p, byte_code %p, output_signature %p.\n", shader
, byte_code
, output_signature
);
223 fe
= shader_select_frontend(*byte_code
);
226 FIXME("Unable to find frontend for shader.\n");
227 return WINED3DERR_INVALIDCALL
;
229 shader
->baseShader
.frontend
= fe
;
230 shader
->baseShader
.frontend_data
= fe
->shader_init(byte_code
, output_signature
);
231 if (!shader
->baseShader
.frontend_data
)
233 FIXME("Failed to initialize frontend.\n");
234 return WINED3DERR_INVALIDCALL
;
237 /* First pass: trace shader */
238 if (TRACE_ON(d3d_shader
)) shader_trace_init(fe
, shader
->baseShader
.frontend_data
, byte_code
);
240 /* Initialize immediate constant lists */
241 list_init(&shader
->baseShader
.constantsF
);
242 list_init(&shader
->baseShader
.constantsB
);
243 list_init(&shader
->baseShader
.constantsI
);
245 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
246 hr
= shader_get_registers_used((IWineD3DBaseShader
*)shader
, fe
,
247 reg_maps
, NULL
, shader
->input_signature
, NULL
,
248 byte_code
, device
->d3d_pshader_constantF
);
249 if (FAILED(hr
)) return hr
;
251 pshader_set_limits(shader
);
253 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
255 if (shader
->input_reg_used
[i
])
258 highest_reg_used
= i
;
262 /* Don't do any register mapping magic if it is not needed, or if we can't
263 * achieve anything anyway */
264 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
265 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
267 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
269 /* This happens with relative addressing. The input mapper function
270 * warns about this if the higher registers are declared too, so
271 * don't write a FIXME here */
272 WARN("More varying registers used than supported\n");
275 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
277 shader
->input_reg_map
[i
] = i
;
280 shader
->declared_in_count
= highest_reg_used
+ 1;
284 shader
->declared_in_count
= 0;
285 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
287 if (shader
->input_reg_used
[i
]) shader
->input_reg_map
[i
] = shader
->declared_in_count
++;
288 else shader
->input_reg_map
[i
] = ~0U;
292 shader
->baseShader
.load_local_constsF
= FALSE
;
294 TRACE("(%p) : Copying byte code.\n", shader
);
296 shader
->baseShader
.function
= HeapAlloc(GetProcessHeap(), 0, shader
->baseShader
.functionLength
);
297 if (!shader
->baseShader
.function
) return E_OUTOFMEMORY
;
298 memcpy(shader
->baseShader
.function
, byte_code
, shader
->baseShader
.functionLength
);
303 void pixelshader_update_samplers(struct shader_reg_maps
*reg_maps
, IWineD3DBaseTexture
* const *textures
)
305 WINED3DSAMPLER_TEXTURE_TYPE
*sampler_type
= reg_maps
->sampler_type
;
308 if (reg_maps
->shader_version
.major
!= 1) return;
310 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
312 /* We don't sample from this sampler */
313 if (!sampler_type
[i
]) continue;
317 ERR("No texture bound to sampler %u, using 2D\n", i
);
318 sampler_type
[i
] = WINED3DSTT_2D
;
322 switch (IWineD3DBaseTexture_GetTextureDimensions(textures
[i
]))
324 case GL_TEXTURE_RECTANGLE_ARB
:
326 /* We have to select between texture rectangles and 2D textures later because 2.0 and
327 * 3.0 shaders only have WINED3DSTT_2D as well */
328 sampler_type
[i
] = WINED3DSTT_2D
;
332 sampler_type
[i
] = WINED3DSTT_VOLUME
;
335 case GL_TEXTURE_CUBE_MAP_ARB
:
336 sampler_type
[i
] = WINED3DSTT_CUBE
;
340 FIXME("Unrecognized texture type %#x, using 2D\n",
341 IWineD3DBaseTexture_GetTextureDimensions(textures
[i
]));
342 sampler_type
[i
] = WINED3DSTT_2D
;
347 static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl
=
349 /*** IUnknown methods ***/
350 IWineD3DPixelShaderImpl_QueryInterface
,
351 IWineD3DPixelShaderImpl_AddRef
,
352 IWineD3DPixelShaderImpl_Release
,
353 /*** IWineD3DBase methods ***/
354 IWineD3DPixelShaderImpl_GetParent
,
355 /*** IWineD3DBaseShader methods ***/
356 IWineD3DPixelShaderImpl_GetDevice
,
357 IWineD3DPixelShaderImpl_GetFunction
358 /*** IWineD3DPixelShader methods ***/
361 void find_ps_compile_args(IWineD3DPixelShaderImpl
*shader
, IWineD3DStateBlockImpl
*stateblock
, struct ps_compile_args
*args
) {
363 IWineD3DBaseTextureImpl
*tex
;
365 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set */
366 args
->srgb_correction
= stateblock
->renderState
[WINED3DRS_SRGBWRITEENABLE
] ? 1 : 0;
369 for(i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; i
++) {
370 if (!shader
->baseShader
.reg_maps
.sampler_type
[i
]) continue;
371 tex
= (IWineD3DBaseTextureImpl
*) stateblock
->textures
[i
];
373 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
376 args
->color_fixup
[i
] = tex
->resource
.format_desc
->color_fixup
;
378 /* Flag samplers that need NP2 texcoord fixup. */
379 if(!tex
->baseTexture
.pow2Matrix_identity
) {
380 args
->np2_fixup
|= (1 << i
);
383 if (shader
->baseShader
.reg_maps
.shader_version
.major
>= 3)
385 if (((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.position_transformed
)
387 args
->vp_mode
= pretransformed
;
389 else if (use_vs(stateblock
))
391 args
->vp_mode
= vertexshader
;
393 args
->vp_mode
= fixedfunction
;
397 args
->vp_mode
= vertexshader
;
398 if(stateblock
->renderState
[WINED3DRS_FOGENABLE
]) {
399 switch(stateblock
->renderState
[WINED3DRS_FOGTABLEMODE
]) {
400 case WINED3DFOG_NONE
:
401 if (((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.position_transformed
402 || use_vs(stateblock
))
404 args
->fog
= FOG_LINEAR
;
407 switch(stateblock
->renderState
[WINED3DRS_FOGVERTEXMODE
]) {
408 case WINED3DFOG_NONE
: /* Drop through */
409 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
410 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
411 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
415 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
416 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
417 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
425 HRESULT
pixelshader_init(IWineD3DPixelShaderImpl
*shader
, IWineD3DDeviceImpl
*device
,
426 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
427 IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
431 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
433 shader
->lpVtbl
= &IWineD3DPixelShader_Vtbl
;
434 shader_init(&shader
->baseShader
, device
, parent
, parent_ops
);
436 hr
= pixelshader_set_function(shader
, byte_code
, output_signature
);
439 WARN("Failed to set function, hr %#x.\n", hr
);
440 shader_cleanup((IWineD3DBaseShader
*)shader
);