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
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
29 static void dump_material(const D3DMATERIAL
*mat
)
31 TRACE(" dwSize : %d\n", mat
->dwSize
);
34 static inline IDirect3DMaterialImpl
*impl_from_IDirect3DMaterial(IDirect3DMaterial
*iface
)
36 return CONTAINING_RECORD(iface
, IDirect3DMaterialImpl
, IDirect3DMaterial_iface
);
39 static inline IDirect3DMaterialImpl
*impl_from_IDirect3DMaterial2(IDirect3DMaterial2
*iface
)
41 return CONTAINING_RECORD(iface
, IDirect3DMaterialImpl
, IDirect3DMaterial2_iface
);
44 static inline IDirect3DMaterialImpl
*impl_from_IDirect3DMaterial3(IDirect3DMaterial3
*iface
)
46 return CONTAINING_RECORD(iface
, IDirect3DMaterialImpl
, IDirect3DMaterial3_iface
);
49 /*****************************************************************************
51 *****************************************************************************/
53 /*****************************************************************************
54 * IDirect3DMaterial3::QueryInterface
56 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
60 * riid: Interface id queried for
61 * obj: Address to pass the interface pointer back
65 * E_NOINTERFACE if the requested interface wasn't found
67 *****************************************************************************/
68 static HRESULT WINAPI
IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3
*iface
, REFIID riid
,
71 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial3(iface
);
73 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), obp
);
77 if ( IsEqualGUID( &IID_IUnknown
, riid
) ) {
78 IUnknown_AddRef(iface
);
80 TRACE(" Creating IUnknown interface at %p.\n", *obp
);
83 if ( IsEqualGUID( &IID_IDirect3DMaterial
, riid
) ) {
84 IDirect3DMaterial_AddRef(&This
->IDirect3DMaterial_iface
);
85 *obp
= &This
->IDirect3DMaterial_iface
;
86 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp
);
89 if ( IsEqualGUID( &IID_IDirect3DMaterial2
, riid
) ) {
90 IDirect3DMaterial_AddRef(&This
->IDirect3DMaterial2_iface
);
91 *obp
= &This
->IDirect3DMaterial2_iface
;
92 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp
);
95 if ( IsEqualGUID( &IID_IDirect3DMaterial3
, riid
) ) {
96 IDirect3DMaterial3_AddRef(&This
->IDirect3DMaterial3_iface
);
98 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp
);
101 FIXME("(%p): interface for IID %s NOT found!\n", This
, debugstr_guid(riid
));
102 return E_NOINTERFACE
;
105 /*****************************************************************************
106 * IDirect3DMaterial3::AddRef
108 * Increases the refcount.
113 *****************************************************************************/
114 static ULONG WINAPI
IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3
*iface
)
116 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial3(iface
);
117 ULONG ref
= InterlockedIncrement(&This
->ref
);
119 TRACE("%p increasing refcount to %u.\n", This
, ref
);
124 /*****************************************************************************
125 * IDirect3DMaterial3::Release
127 * Reduces the refcount by one. If the refcount falls to 0, the object
133 *****************************************************************************/
134 static ULONG WINAPI
IDirect3DMaterialImpl_Release(IDirect3DMaterial3
*iface
)
136 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial3(iface
);
137 ULONG ref
= InterlockedDecrement(&This
->ref
);
139 TRACE("%p decreasing refcount to %u.\n", This
, ref
);
145 wined3d_mutex_lock();
146 ddraw_free_handle(&This
->ddraw
->d3ddevice
->handle_table
, This
->Handle
- 1, DDRAW_HANDLE_MATERIAL
);
147 wined3d_mutex_unlock();
150 HeapFree(GetProcessHeap(), 0, This
);
156 /*****************************************************************************
157 * IDirect3DMaterial Methods
158 *****************************************************************************/
160 /*****************************************************************************
161 * IDirect3DMaterial::Initialize
163 * A no-op initialization
166 * Direct3D: Pointer to a Direct3D interface
171 *****************************************************************************/
172 static HRESULT WINAPI
173 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial
*iface
,
176 TRACE("iface %p, d3d %p.\n", iface
, Direct3D
);
181 /*****************************************************************************
182 * IDirect3DMaterial::Reserve
184 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
185 * Odd. They seem to have mixed their interfaces.
190 *****************************************************************************/
191 static HRESULT WINAPI
192 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial
*iface
)
194 TRACE("iface %p.\n", iface
);
196 return DDERR_UNSUPPORTED
;
199 /*****************************************************************************
200 * IDirect3DMaterial::Unreserve
207 *****************************************************************************/
208 static HRESULT WINAPI
209 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial
*iface
)
211 TRACE("iface %p.\n", iface
);
213 return DDERR_UNSUPPORTED
;
216 /*****************************************************************************
217 * IDirect3DMaterial3::SetMaterial
219 * Sets the material description
222 * Mat: Material to set
226 * DDERR_INVALIDPARAMS if Mat is NULL
228 *****************************************************************************/
229 static HRESULT WINAPI
IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3
*iface
,
232 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial3(iface
);
234 TRACE("iface %p, material %p.\n", iface
, lpMat
);
236 dump_material(lpMat
);
238 /* Stores the material */
239 wined3d_mutex_lock();
240 memset(&This
->mat
, 0, sizeof(This
->mat
));
241 memcpy(&This
->mat
, lpMat
, lpMat
->dwSize
);
242 wined3d_mutex_unlock();
247 /*****************************************************************************
248 * IDirect3DMaterial3::GetMaterial
250 * Returns the material assigned to this interface
253 * Mat: Pointer to a D3DMATERIAL structure to store the material description
257 * DDERR_INVALIDPARAMS if Mat is NULL
259 *****************************************************************************/
260 static HRESULT WINAPI
IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3
*iface
,
263 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial3(iface
);
266 TRACE("iface %p, material %p.\n", iface
, lpMat
);
269 TRACE(" Returning material : ");
270 dump_material(&This
->mat
);
273 /* Copies the material structure */
274 wined3d_mutex_lock();
275 dwSize
= lpMat
->dwSize
;
276 memcpy(lpMat
, &This
->mat
, dwSize
);
277 wined3d_mutex_unlock();
282 /*****************************************************************************
283 * IDirect3DMaterial3::GetHandle
285 * Returns a handle for the material interface. The handle is simply a
286 * pointer to the material implementation
289 * Direct3DDevice3: The device this handle is assigned to
290 * Handle: Address to write the handle to
294 * DDERR_INVALIDPARAMS if Handle is NULL
296 *****************************************************************************/
297 static HRESULT WINAPI
IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3
*iface
,
298 IDirect3DDevice3
*device
, D3DMATERIALHANDLE
*handle
)
300 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial3(iface
);
301 IDirect3DDeviceImpl
*device_impl
= unsafe_impl_from_IDirect3DDevice3(device
);
303 TRACE("iface %p, device %p, handle %p.\n", iface
, device
, handle
);
305 wined3d_mutex_lock();
306 This
->active_device
= device_impl
;
309 DWORD h
= ddraw_allocate_handle(&device_impl
->handle_table
, This
, DDRAW_HANDLE_MATERIAL
);
310 if (h
== DDRAW_INVALID_HANDLE
)
312 ERR("Failed to allocate a material handle.\n");
313 wined3d_mutex_unlock();
314 return DDERR_INVALIDPARAMS
; /* Unchecked */
317 This
->Handle
= h
+ 1;
319 *handle
= This
->Handle
;
320 TRACE(" returning handle %08x.\n", *handle
);
321 wined3d_mutex_unlock();
326 static HRESULT WINAPI
IDirect3DMaterialImpl_2_GetHandle(IDirect3DMaterial2
*iface
,
327 IDirect3DDevice2
*device
, D3DMATERIALHANDLE
*handle
)
329 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial2(iface
);
330 IDirect3DDeviceImpl
*device_impl
= unsafe_impl_from_IDirect3DDevice2(device
);
332 TRACE("iface %p, device %p, handle %p.\n", iface
, device
, handle
);
334 return IDirect3DMaterial3_GetHandle(&This
->IDirect3DMaterial3_iface
, device_impl
?
335 &device_impl
->IDirect3DDevice3_iface
: NULL
, handle
);
338 static HRESULT WINAPI
IDirect3DMaterialImpl_1_GetHandle(IDirect3DMaterial
*iface
,
339 IDirect3DDevice
*device
, D3DMATERIALHANDLE
*handle
)
341 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial(iface
);
342 IDirect3DDeviceImpl
*device_impl
= unsafe_impl_from_IDirect3DDevice(device
);
344 TRACE("iface %p, device %p, handle %p.\n", iface
, device
, handle
);
346 return IDirect3DMaterial3_GetHandle(&This
->IDirect3DMaterial3_iface
, device_impl
?
347 &device_impl
->IDirect3DDevice3_iface
: NULL
, handle
);
350 static HRESULT WINAPI
IDirect3DMaterialImpl_2_QueryInterface(IDirect3DMaterial2
*iface
, REFIID riid
,
353 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial2(iface
);
355 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), obp
);
357 return IDirect3DMaterial3_QueryInterface(&This
->IDirect3DMaterial3_iface
, riid
, obp
);
360 static HRESULT WINAPI
IDirect3DMaterialImpl_1_QueryInterface(IDirect3DMaterial
*iface
, REFIID riid
,
363 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial(iface
);
365 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), obp
);
367 return IDirect3DMaterial3_QueryInterface(&This
->IDirect3DMaterial3_iface
, riid
, obp
);
370 static ULONG WINAPI
IDirect3DMaterialImpl_2_AddRef(IDirect3DMaterial2
*iface
)
372 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial2(iface
);
374 TRACE("iface %p.\n", iface
);
376 return IDirect3DMaterial3_AddRef(&This
->IDirect3DMaterial3_iface
);
379 static ULONG WINAPI
IDirect3DMaterialImpl_1_AddRef(IDirect3DMaterial
*iface
)
381 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial(iface
);
383 TRACE("iface %p.\n", iface
);
385 return IDirect3DMaterial3_AddRef(&This
->IDirect3DMaterial3_iface
);
388 static ULONG WINAPI
IDirect3DMaterialImpl_2_Release(IDirect3DMaterial2
*iface
)
390 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial2(iface
);
392 TRACE("iface %p.\n", iface
);
394 return IDirect3DMaterial3_Release(&This
->IDirect3DMaterial3_iface
);
397 static ULONG WINAPI
IDirect3DMaterialImpl_1_Release(IDirect3DMaterial
*iface
)
399 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial(iface
);
401 TRACE("iface %p.\n", iface
);
403 return IDirect3DMaterial3_Release(&This
->IDirect3DMaterial3_iface
);
406 static HRESULT WINAPI
IDirect3DMaterialImpl_2_SetMaterial(IDirect3DMaterial2
*iface
,
409 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial2(iface
);
411 TRACE("iface %p, material %p.\n", iface
, lpMat
);
413 return IDirect3DMaterial3_SetMaterial(&This
->IDirect3DMaterial3_iface
, lpMat
);
416 static HRESULT WINAPI
IDirect3DMaterialImpl_1_SetMaterial(IDirect3DMaterial
*iface
,
419 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial(iface
);
421 TRACE("iface %p, material %p.\n", iface
, lpMat
);
423 return IDirect3DMaterial3_SetMaterial(&This
->IDirect3DMaterial3_iface
, lpMat
);
426 static HRESULT WINAPI
IDirect3DMaterialImpl_2_GetMaterial(IDirect3DMaterial2
*iface
,
429 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial2(iface
);
431 TRACE("iface %p, material %p.\n", iface
, lpMat
);
433 return IDirect3DMaterial3_GetMaterial(&This
->IDirect3DMaterial3_iface
, lpMat
);
436 static HRESULT WINAPI
IDirect3DMaterialImpl_1_GetMaterial(IDirect3DMaterial
*iface
,
439 IDirect3DMaterialImpl
*This
= impl_from_IDirect3DMaterial(iface
);
441 TRACE("iface %p, material %p.\n", iface
, lpMat
);
443 return IDirect3DMaterial3_GetMaterial(&This
->IDirect3DMaterial3_iface
, lpMat
);
447 /*****************************************************************************
450 * Uses IDirect3DDevice7::SetMaterial to activate the material
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(&This
->active_device
->IDirect3DDevice7_iface
, &d3d7mat
);
470 static const struct IDirect3DMaterial3Vtbl d3d_material3_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 static const struct IDirect3DMaterial2Vtbl d3d_material2_vtbl
=
484 /*** IUnknown Methods ***/
485 IDirect3DMaterialImpl_2_QueryInterface
,
486 IDirect3DMaterialImpl_2_AddRef
,
487 IDirect3DMaterialImpl_2_Release
,
488 /*** IDirect3DMaterial2 Methods ***/
489 IDirect3DMaterialImpl_2_SetMaterial
,
490 IDirect3DMaterialImpl_2_GetMaterial
,
491 IDirect3DMaterialImpl_2_GetHandle
,
494 static const struct IDirect3DMaterialVtbl d3d_material1_vtbl
=
496 /*** IUnknown Methods ***/
497 IDirect3DMaterialImpl_1_QueryInterface
,
498 IDirect3DMaterialImpl_1_AddRef
,
499 IDirect3DMaterialImpl_1_Release
,
500 /*** IDirect3DMaterial1 Methods ***/
501 IDirect3DMaterialImpl_Initialize
,
502 IDirect3DMaterialImpl_1_SetMaterial
,
503 IDirect3DMaterialImpl_1_GetMaterial
,
504 IDirect3DMaterialImpl_1_GetHandle
,
505 IDirect3DMaterialImpl_Reserve
,
506 IDirect3DMaterialImpl_Unreserve
509 IDirect3DMaterialImpl
*d3d_material_create(IDirectDrawImpl
*ddraw
)
511 IDirect3DMaterialImpl
*material
;
513 material
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*material
));
517 material
->IDirect3DMaterial3_iface
.lpVtbl
= &d3d_material3_vtbl
;
518 material
->IDirect3DMaterial2_iface
.lpVtbl
= &d3d_material2_vtbl
;
519 material
->IDirect3DMaterial_iface
.lpVtbl
= &d3d_material1_vtbl
;
521 material
->ddraw
= ddraw
;