winex11: Force alpha channel to 0 when blitting to depth 32 from a lower depth.
[wine/multimedia.git] / dlls / ddraw / material.c
blobd75501ecd95b42fcef16be60b3f0fbde70a70869
1 /* Direct3D Material
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DMaterial.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #define COBJMACROS
31 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
39 #include "ddraw.h"
40 #include "d3d.h"
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
48 static void dump_material(const D3DMATERIAL *mat)
50 TRACE(" dwSize : %d\n", mat->dwSize);
53 static inline IDirect3DMaterialImpl *material_from_material1(IDirect3DMaterial *iface)
55 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial_vtbl));
58 static inline IDirect3DMaterialImpl *material_from_material2(IDirect3DMaterial2 *iface)
60 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial2_vtbl));
63 /*****************************************************************************
64 * IUnknown Methods.
65 *****************************************************************************/
67 /*****************************************************************************
68 * IDirect3DMaterial3::QueryInterface
70 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
71 * versions.
73 * Params:
74 * riid: Interface id queried for
75 * obj: Address to pass the interface pointer back
77 * Returns:
78 * S_OK on success
79 * E_NOINTERFACE if the requested interface wasn't found
81 *****************************************************************************/
82 static HRESULT WINAPI
83 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
84 REFIID riid,
85 LPVOID* obp)
87 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
88 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
90 *obp = NULL;
92 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
93 IUnknown_AddRef(iface);
94 *obp = iface;
95 TRACE(" Creating IUnknown interface at %p.\n", *obp);
96 return S_OK;
98 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
99 IDirect3DMaterial_AddRef((IDirect3DMaterial *)&This->IDirect3DMaterial_vtbl);
100 *obp = &This->IDirect3DMaterial_vtbl;
101 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
102 return S_OK;
104 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
105 IDirect3DMaterial_AddRef((IDirect3DMaterial2 *)&This->IDirect3DMaterial2_vtbl);
106 *obp = &This->IDirect3DMaterial2_vtbl;
107 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
108 return S_OK;
110 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
111 IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)This);
112 *obp = This;
113 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
114 return S_OK;
116 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
117 return E_NOINTERFACE;
120 /*****************************************************************************
121 * IDirect3DMaterial3::AddRef
123 * Increases the refcount.
125 * Returns:
126 * The new refcount
128 *****************************************************************************/
129 static ULONG WINAPI
130 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
132 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
133 ULONG ref = InterlockedIncrement(&This->ref);
135 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
137 return ref;
140 /*****************************************************************************
141 * IDirect3DMaterial3::Release
143 * Reduces the refcount by one. If the refcount falls to 0, the object
144 * is destroyed
146 * Returns:
147 * The new refcount
149 *****************************************************************************/
150 static ULONG WINAPI
151 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
153 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
154 ULONG ref = InterlockedDecrement(&This->ref);
156 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
158 if (!ref)
160 if(This->Handle)
162 EnterCriticalSection(&ddraw_cs);
163 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
164 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
165 LeaveCriticalSection(&ddraw_cs);
168 HeapFree(GetProcessHeap(), 0, This);
169 return 0;
171 return ref;
174 /*****************************************************************************
175 * IDirect3DMaterial Methods
176 *****************************************************************************/
178 /*****************************************************************************
179 * IDirect3DMaterial::Initialize
181 * A no-op initialization
183 * Params:
184 * Direct3D: Pointer to a Direct3D interface
186 * Returns:
187 * D3D_OK
189 *****************************************************************************/
190 static HRESULT WINAPI
191 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
192 IDirect3D *Direct3D)
194 IDirect3DMaterialImpl *This = material_from_material1(iface);
196 TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
198 return D3D_OK;
201 /*****************************************************************************
202 * IDirect3DMaterial::Reserve
204 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
205 * Odd. They seem to have mixed their interfaces.
207 * Returns:
208 * DDERR_UNSUPPORTED
210 *****************************************************************************/
211 static HRESULT WINAPI
212 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
214 IDirect3DMaterialImpl *This = material_from_material1(iface);
215 TRACE("(%p)->() not implemented\n", This);
217 return DDERR_UNSUPPORTED;
220 /*****************************************************************************
221 * IDirect3DMaterial::Unreserve
223 * Not supported too
225 * Returns:
226 * DDERR_UNSUPPORTED
228 *****************************************************************************/
229 static HRESULT WINAPI
230 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
232 IDirect3DMaterialImpl *This = material_from_material1(iface);
233 TRACE("(%p)->() not implemented.\n", This);
235 return DDERR_UNSUPPORTED;
238 /*****************************************************************************
239 * IDirect3DMaterial3::SetMaterial
241 * Sets the material description
243 * Params:
244 * Mat: Material to set
246 * Returns:
247 * D3D_OK on success
248 * DDERR_INVALIDPARAMS if Mat is NULL
250 *****************************************************************************/
251 static HRESULT WINAPI
252 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
253 D3DMATERIAL *lpMat)
255 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
256 TRACE("(%p)->(%p)\n", This, lpMat);
257 if (TRACE_ON(d3d7))
258 dump_material(lpMat);
260 /* Stores the material */
261 EnterCriticalSection(&ddraw_cs);
262 memset(&This->mat, 0, sizeof(This->mat));
263 memcpy(&This->mat, lpMat, lpMat->dwSize);
264 LeaveCriticalSection(&ddraw_cs);
266 return DD_OK;
269 /*****************************************************************************
270 * IDirect3DMaterial3::GetMaterial
272 * Returns the material assigned to this interface
274 * Params:
275 * Mat: Pointer to a D3DMATERIAL structure to store the material description
277 * Returns:
278 * D3D_OK on success
279 * DDERR_INVALIDPARAMS if Mat is NULL
281 *****************************************************************************/
282 static HRESULT WINAPI
283 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
284 D3DMATERIAL *lpMat)
286 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
287 DWORD dwSize;
288 TRACE("(%p)->(%p)\n", This, lpMat);
289 if (TRACE_ON(d3d7)) {
290 TRACE(" Returning material : ");
291 dump_material(&This->mat);
294 /* Copies the material structure */
295 EnterCriticalSection(&ddraw_cs);
296 dwSize = lpMat->dwSize;
297 memcpy(lpMat, &This->mat, dwSize);
298 LeaveCriticalSection(&ddraw_cs);
300 return DD_OK;
303 /*****************************************************************************
304 * IDirect3DMaterial3::GetHandle
306 * Returns a handle for the material interface. The handle is simply a
307 * pointer to the material implementation
309 * Params:
310 * Direct3DDevice3: The device this handle is assigned to
311 * Handle: Address to write the handle to
313 * Returns:
314 * D3D_OK on success
315 * DDERR_INVALIDPARAMS if Handle is NULL
317 *****************************************************************************/
318 static HRESULT WINAPI
319 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
320 IDirect3DDevice3 *lpDirect3DDevice3,
321 D3DMATERIALHANDLE *lpHandle)
323 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
324 IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
325 TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
327 EnterCriticalSection(&ddraw_cs);
328 This->active_device = device;
329 if(!This->Handle)
331 This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
332 if(!This->Handle)
334 ERR("Error creating a handle\n");
335 LeaveCriticalSection(&ddraw_cs);
336 return DDERR_INVALIDPARAMS; /* Unchecked */
338 device->Handles[This->Handle - 1].ptr = This;
339 device->Handles[This->Handle - 1].type = DDrawHandle_Material;
341 *lpHandle = This->Handle;
342 TRACE(" returning handle %08x.\n", *lpHandle);
343 LeaveCriticalSection(&ddraw_cs);
345 return D3D_OK;
348 static HRESULT WINAPI
349 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
350 LPDIRECT3DDEVICE2 lpDirect3DDevice2,
351 LPD3DMATERIALHANDLE lpHandle)
353 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
354 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material2(iface), lpDirect3DDevice2 ?
355 (IDirect3DDevice3 *)&device_from_device2(lpDirect3DDevice2)->IDirect3DDevice3_vtbl : NULL, lpHandle);
358 static HRESULT WINAPI
359 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
360 LPDIRECT3DDEVICE lpDirect3DDevice,
361 LPD3DMATERIALHANDLE lpHandle)
363 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
364 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material1(iface), lpDirect3DDevice ?
365 (IDirect3DDevice3 *)&device_from_device1(lpDirect3DDevice)->IDirect3DDevice3_vtbl : NULL, lpHandle);
368 static HRESULT WINAPI
369 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
370 REFIID riid,
371 LPVOID* obp)
373 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
374 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material2(iface), riid, obp);
377 static HRESULT WINAPI
378 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
379 REFIID riid,
380 LPVOID* obp)
382 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
383 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material1(iface), riid, obp);
386 static ULONG WINAPI
387 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
389 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
390 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material2(iface));
393 static ULONG WINAPI
394 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
396 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
397 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material1(iface));
400 static ULONG WINAPI
401 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
403 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
404 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material2(iface));
407 static ULONG WINAPI
408 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
410 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
411 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material1(iface));
414 static HRESULT WINAPI
415 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
416 LPD3DMATERIAL lpMat)
418 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
419 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
422 static HRESULT WINAPI
423 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
424 LPD3DMATERIAL lpMat)
426 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
427 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
430 static HRESULT WINAPI
431 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
432 LPD3DMATERIAL lpMat)
434 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
435 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
438 static HRESULT WINAPI
439 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
440 LPD3DMATERIAL lpMat)
442 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
443 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
447 /*****************************************************************************
448 * material_activate
450 * Uses IDirect3DDevice7::SetMaterial to activate the material
452 * Params:
453 * This: Pointer to the material implementation to activate
455 *****************************************************************************/
456 void material_activate(IDirect3DMaterialImpl* This)
458 D3DMATERIAL7 d3d7mat;
460 TRACE("Activating material %p\n", This);
461 d3d7mat.u.diffuse = This->mat.u.diffuse;
462 d3d7mat.u1.ambient = This->mat.u1.ambient;
463 d3d7mat.u2.specular = This->mat.u2.specular;
464 d3d7mat.u3.emissive = This->mat.u3.emissive;
465 d3d7mat.u4.power = This->mat.u4.power;
467 IDirect3DDevice7_SetMaterial((IDirect3DDevice7 *)This->active_device, &d3d7mat);
470 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
472 /*** IUnknown Methods ***/
473 IDirect3DMaterialImpl_QueryInterface,
474 IDirect3DMaterialImpl_AddRef,
475 IDirect3DMaterialImpl_Release,
476 /*** IDirect3DMaterial3 Methods ***/
477 IDirect3DMaterialImpl_SetMaterial,
478 IDirect3DMaterialImpl_GetMaterial,
479 IDirect3DMaterialImpl_GetHandle,
482 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
484 /*** IUnknown Methods ***/
485 Thunk_IDirect3DMaterialImpl_2_QueryInterface,
486 Thunk_IDirect3DMaterialImpl_2_AddRef,
487 Thunk_IDirect3DMaterialImpl_2_Release,
488 /*** IDirect3DMaterial2 Methods ***/
489 Thunk_IDirect3DMaterialImpl_2_SetMaterial,
490 Thunk_IDirect3DMaterialImpl_2_GetMaterial,
491 Thunk_IDirect3DMaterialImpl_2_GetHandle,
494 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
496 /*** IUnknown Methods ***/
497 Thunk_IDirect3DMaterialImpl_1_QueryInterface,
498 Thunk_IDirect3DMaterialImpl_1_AddRef,
499 Thunk_IDirect3DMaterialImpl_1_Release,
500 /*** IDirect3DMaterial1 Methods ***/
501 IDirect3DMaterialImpl_Initialize,
502 Thunk_IDirect3DMaterialImpl_1_SetMaterial,
503 Thunk_IDirect3DMaterialImpl_1_GetMaterial,
504 Thunk_IDirect3DMaterialImpl_1_GetHandle,
505 IDirect3DMaterialImpl_Reserve,
506 IDirect3DMaterialImpl_Unreserve