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_GetFunction(IWineD3DPixelShader
* impl
, VOID
* pData
, UINT
* pSizeOfData
) {
94 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)impl
;
95 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This
, pData
, pSizeOfData
);
98 *pSizeOfData
= This
->baseShader
.functionLength
;
101 if (*pSizeOfData
< This
->baseShader
.functionLength
) {
102 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
103 * than the required size we should write the required size and
104 * return D3DERR_MOREDATA. That's not actually true. */
105 return WINED3DERR_INVALIDCALL
;
108 TRACE("(%p) : GetFunction copying to %p\n", This
, pData
);
109 memcpy(pData
, This
->baseShader
.function
, This
->baseShader
.functionLength
);
114 static void pshader_set_limits(IWineD3DPixelShaderImpl
*This
)
116 DWORD shader_version
= WINED3D_SHADER_VERSION(This
->baseShader
.reg_maps
.shader_version
.major
,
117 This
->baseShader
.reg_maps
.shader_version
.minor
);
119 This
->baseShader
.limits
.attributes
= 0;
120 This
->baseShader
.limits
.address
= 0;
121 This
->baseShader
.limits
.packed_output
= 0;
123 switch (shader_version
)
125 case WINED3D_SHADER_VERSION(1,0):
126 case WINED3D_SHADER_VERSION(1,1):
127 case WINED3D_SHADER_VERSION(1,2):
128 case WINED3D_SHADER_VERSION(1,3):
129 This
->baseShader
.limits
.temporary
= 2;
130 This
->baseShader
.limits
.constant_float
= 8;
131 This
->baseShader
.limits
.constant_int
= 0;
132 This
->baseShader
.limits
.constant_bool
= 0;
133 This
->baseShader
.limits
.texcoord
= 4;
134 This
->baseShader
.limits
.sampler
= 4;
135 This
->baseShader
.limits
.packed_input
= 0;
136 This
->baseShader
.limits
.label
= 0;
139 case WINED3D_SHADER_VERSION(1,4):
140 This
->baseShader
.limits
.temporary
= 6;
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
= 6;
145 This
->baseShader
.limits
.sampler
= 6;
146 This
->baseShader
.limits
.packed_input
= 0;
147 This
->baseShader
.limits
.label
= 0;
150 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
151 case WINED3D_SHADER_VERSION(2,0):
152 This
->baseShader
.limits
.temporary
= 32;
153 This
->baseShader
.limits
.constant_float
= 32;
154 This
->baseShader
.limits
.constant_int
= 16;
155 This
->baseShader
.limits
.constant_bool
= 16;
156 This
->baseShader
.limits
.texcoord
= 8;
157 This
->baseShader
.limits
.sampler
= 16;
158 This
->baseShader
.limits
.packed_input
= 0;
161 case WINED3D_SHADER_VERSION(2,1):
162 This
->baseShader
.limits
.temporary
= 32;
163 This
->baseShader
.limits
.constant_float
= 32;
164 This
->baseShader
.limits
.constant_int
= 16;
165 This
->baseShader
.limits
.constant_bool
= 16;
166 This
->baseShader
.limits
.texcoord
= 8;
167 This
->baseShader
.limits
.sampler
= 16;
168 This
->baseShader
.limits
.packed_input
= 0;
169 This
->baseShader
.limits
.label
= 16;
172 case WINED3D_SHADER_VERSION(4,0):
173 FIXME("Using 3.0 limits for 4.0 shader\n");
176 case WINED3D_SHADER_VERSION(3,0):
177 This
->baseShader
.limits
.temporary
= 32;
178 This
->baseShader
.limits
.constant_float
= 224;
179 This
->baseShader
.limits
.constant_int
= 16;
180 This
->baseShader
.limits
.constant_bool
= 16;
181 This
->baseShader
.limits
.texcoord
= 0;
182 This
->baseShader
.limits
.sampler
= 16;
183 This
->baseShader
.limits
.packed_input
= 12;
184 This
->baseShader
.limits
.label
= 16; /* FIXME: 2048 */
188 This
->baseShader
.limits
.temporary
= 32;
189 This
->baseShader
.limits
.constant_float
= 32;
190 This
->baseShader
.limits
.constant_int
= 16;
191 This
->baseShader
.limits
.constant_bool
= 16;
192 This
->baseShader
.limits
.texcoord
= 8;
193 This
->baseShader
.limits
.sampler
= 16;
194 This
->baseShader
.limits
.packed_input
= 0;
195 This
->baseShader
.limits
.label
= 0;
196 FIXME("Unrecognized pixel shader version %u.%u\n",
197 This
->baseShader
.reg_maps
.shader_version
.major
,
198 This
->baseShader
.reg_maps
.shader_version
.minor
);
202 static HRESULT
pixelshader_set_function(IWineD3DPixelShaderImpl
*shader
,
203 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
)
205 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
206 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
207 unsigned int i
, highest_reg_used
= 0, num_regs_used
= 0;
208 shader_reg_maps
*reg_maps
= &shader
->baseShader
.reg_maps
;
209 const struct wined3d_shader_frontend
*fe
;
212 TRACE("shader %p, byte_code %p, output_signature %p.\n", shader
, byte_code
, output_signature
);
214 fe
= shader_select_frontend(*byte_code
);
217 FIXME("Unable to find frontend for shader.\n");
218 return WINED3DERR_INVALIDCALL
;
220 shader
->baseShader
.frontend
= fe
;
221 shader
->baseShader
.frontend_data
= fe
->shader_init(byte_code
, output_signature
);
222 if (!shader
->baseShader
.frontend_data
)
224 FIXME("Failed to initialize frontend.\n");
225 return WINED3DERR_INVALIDCALL
;
228 /* First pass: trace shader */
229 if (TRACE_ON(d3d_shader
)) shader_trace_init(fe
, shader
->baseShader
.frontend_data
, byte_code
);
231 /* Initialize immediate constant lists */
232 list_init(&shader
->baseShader
.constantsF
);
233 list_init(&shader
->baseShader
.constantsB
);
234 list_init(&shader
->baseShader
.constantsI
);
236 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
237 hr
= shader_get_registers_used((IWineD3DBaseShader
*)shader
, fe
,
238 reg_maps
, NULL
, shader
->input_signature
, NULL
,
239 byte_code
, device
->d3d_pshader_constantF
);
240 if (FAILED(hr
)) return hr
;
242 pshader_set_limits(shader
);
244 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
246 if (shader
->input_reg_used
[i
])
249 highest_reg_used
= i
;
253 /* Don't do any register mapping magic if it is not needed, or if we can't
254 * achieve anything anyway */
255 if (highest_reg_used
< (gl_info
->limits
.glsl_varyings
/ 4)
256 || num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
258 if (num_regs_used
> (gl_info
->limits
.glsl_varyings
/ 4))
260 /* This happens with relative addressing. The input mapper function
261 * warns about this if the higher registers are declared too, so
262 * don't write a FIXME here */
263 WARN("More varying registers used than supported\n");
266 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
268 shader
->input_reg_map
[i
] = i
;
271 shader
->declared_in_count
= highest_reg_used
+ 1;
275 shader
->declared_in_count
= 0;
276 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
278 if (shader
->input_reg_used
[i
]) shader
->input_reg_map
[i
] = shader
->declared_in_count
++;
279 else shader
->input_reg_map
[i
] = ~0U;
283 shader
->baseShader
.load_local_constsF
= FALSE
;
285 TRACE("(%p) : Copying byte code.\n", shader
);
287 shader
->baseShader
.function
= HeapAlloc(GetProcessHeap(), 0, shader
->baseShader
.functionLength
);
288 if (!shader
->baseShader
.function
) return E_OUTOFMEMORY
;
289 memcpy(shader
->baseShader
.function
, byte_code
, shader
->baseShader
.functionLength
);
294 void pixelshader_update_samplers(struct shader_reg_maps
*reg_maps
, IWineD3DBaseTexture
* const *textures
)
296 WINED3DSAMPLER_TEXTURE_TYPE
*sampler_type
= reg_maps
->sampler_type
;
299 if (reg_maps
->shader_version
.major
!= 1) return;
301 for (i
= 0; i
< max(MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
); ++i
)
303 /* We don't sample from this sampler */
304 if (!sampler_type
[i
]) continue;
308 ERR("No texture bound to sampler %u, using 2D\n", i
);
309 sampler_type
[i
] = WINED3DSTT_2D
;
313 switch (IWineD3DBaseTexture_GetTextureDimensions(textures
[i
]))
315 case GL_TEXTURE_RECTANGLE_ARB
:
317 /* We have to select between texture rectangles and 2D textures later because 2.0 and
318 * 3.0 shaders only have WINED3DSTT_2D as well */
319 sampler_type
[i
] = WINED3DSTT_2D
;
323 sampler_type
[i
] = WINED3DSTT_VOLUME
;
326 case GL_TEXTURE_CUBE_MAP_ARB
:
327 sampler_type
[i
] = WINED3DSTT_CUBE
;
331 FIXME("Unrecognized texture type %#x, using 2D\n",
332 IWineD3DBaseTexture_GetTextureDimensions(textures
[i
]));
333 sampler_type
[i
] = WINED3DSTT_2D
;
338 static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl
=
340 /*** IUnknown methods ***/
341 IWineD3DPixelShaderImpl_QueryInterface
,
342 IWineD3DPixelShaderImpl_AddRef
,
343 IWineD3DPixelShaderImpl_Release
,
344 /*** IWineD3DBase methods ***/
345 IWineD3DPixelShaderImpl_GetParent
,
346 /*** IWineD3DBaseShader methods ***/
347 IWineD3DPixelShaderImpl_GetFunction
348 /*** IWineD3DPixelShader methods ***/
351 void find_ps_compile_args(IWineD3DPixelShaderImpl
*shader
, IWineD3DStateBlockImpl
*stateblock
, struct ps_compile_args
*args
) {
353 IWineD3DBaseTextureImpl
*tex
;
355 memset(args
, 0, sizeof(*args
)); /* FIXME: Make sure all bits are set */
356 args
->srgb_correction
= stateblock
->renderState
[WINED3DRS_SRGBWRITEENABLE
] ? 1 : 0;
359 for(i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; i
++) {
360 if (!shader
->baseShader
.reg_maps
.sampler_type
[i
]) continue;
361 tex
= (IWineD3DBaseTextureImpl
*) stateblock
->textures
[i
];
363 args
->color_fixup
[i
] = COLOR_FIXUP_IDENTITY
;
366 args
->color_fixup
[i
] = tex
->resource
.format_desc
->color_fixup
;
368 /* Flag samplers that need NP2 texcoord fixup. */
369 if(!tex
->baseTexture
.pow2Matrix_identity
) {
370 args
->np2_fixup
|= (1 << i
);
373 if (shader
->baseShader
.reg_maps
.shader_version
.major
>= 3)
375 if (((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.position_transformed
)
377 args
->vp_mode
= pretransformed
;
379 else if (use_vs(stateblock
))
381 args
->vp_mode
= vertexshader
;
383 args
->vp_mode
= fixedfunction
;
387 args
->vp_mode
= vertexshader
;
388 if(stateblock
->renderState
[WINED3DRS_FOGENABLE
]) {
389 switch(stateblock
->renderState
[WINED3DRS_FOGTABLEMODE
]) {
390 case WINED3DFOG_NONE
:
391 if (((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.position_transformed
392 || use_vs(stateblock
))
394 args
->fog
= FOG_LINEAR
;
397 switch(stateblock
->renderState
[WINED3DRS_FOGVERTEXMODE
]) {
398 case WINED3DFOG_NONE
: /* Drop through */
399 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
400 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
401 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
405 case WINED3DFOG_LINEAR
: args
->fog
= FOG_LINEAR
; break;
406 case WINED3DFOG_EXP
: args
->fog
= FOG_EXP
; break;
407 case WINED3DFOG_EXP2
: args
->fog
= FOG_EXP2
; break;
415 HRESULT
pixelshader_init(IWineD3DPixelShaderImpl
*shader
, IWineD3DDeviceImpl
*device
,
416 const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
,
417 IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
421 if (!byte_code
) return WINED3DERR_INVALIDCALL
;
423 shader
->lpVtbl
= &IWineD3DPixelShader_Vtbl
;
424 shader_init(&shader
->baseShader
, device
, parent
, parent_ops
);
426 hr
= pixelshader_set_function(shader
, byte_code
, output_signature
);
429 WARN("Failed to set function, hr %#x.\n", hr
);
430 shader_cleanup((IWineD3DBaseShader
*)shader
);