server: Make a couple of fields smaller in the apc_call_t/apc_result_t structures.
[wine] / dlls / ddraw / material.c
1 /* Direct3D Material
2  * Copyright (c) 2002 Lionel ULMER
3  * Copyright (c) 2006 Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DMaterial.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #define COBJMACROS
31 #define NONAMELESSUNION
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
38
39 #include "ddraw.h"
40 #include "d3d.h"
41
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
47
48 static void dump_material(const D3DMATERIAL *mat)
49 {
50     TRACE("  dwSize : %d\n", mat->dwSize);
51 }
52
53 /*****************************************************************************
54  * IUnknown Methods.
55  *****************************************************************************/
56
57 /*****************************************************************************
58  * IDirect3DMaterial3::QueryInterface
59  *
60  * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
61  * versions.
62  *
63  * Params:
64  *  riid: Interface id queried for
65  *  obj: Address to pass the interface pointer back
66  *
67  * Returns:
68  *  S_OK on success
69  *  E_NOINTERFACE if the requested interface wasn't found
70  *
71  *****************************************************************************/
72 static HRESULT WINAPI
73 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
74                                      REFIID riid,
75                                      LPVOID* obp)
76 {
77     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
78     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
79
80     *obp = NULL;
81
82     if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
83         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
84         *obp = iface;
85         TRACE("  Creating IUnknown interface at %p.\n", *obp);
86         return S_OK;
87     }
88     if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
89         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
90         *obp = ICOM_INTERFACE(This, IDirect3DMaterial);
91         TRACE("  Creating IDirect3DMaterial interface %p\n", *obp);
92         return S_OK;
93     }
94     if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
95         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
96         *obp = ICOM_INTERFACE(This, IDirect3DMaterial2);
97         TRACE("  Creating IDirect3DMaterial2 interface %p\n", *obp);
98         return S_OK;
99     }
100     if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
101         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
102         *obp = ICOM_INTERFACE(This, IDirect3DMaterial3);
103         TRACE("  Creating IDirect3DMaterial3 interface %p\n", *obp);
104         return S_OK;
105     }
106     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
107     return E_NOINTERFACE;
108 }
109
110 /*****************************************************************************
111  * IDirect3DMaterial3::AddRef
112  *
113  * Increases the refcount.
114  *
115  * Returns:
116  *  The new refcount
117  *
118  *****************************************************************************/
119 static ULONG WINAPI
120 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
121 {
122     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
123     ULONG ref = InterlockedIncrement(&This->ref);
124
125     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
126
127     return ref;
128 }
129
130 /*****************************************************************************
131  * IDirect3DMaterial3::Release
132  *
133  * Reduces the refcount by one. If the refcount falls to 0, the object
134  * is destroyed
135  *
136  * Returns:
137  *  The new refcount
138  *
139  *****************************************************************************/
140 static ULONG WINAPI
141 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
142 {
143     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
144     ULONG ref = InterlockedDecrement(&This->ref);
145
146     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
147
148     if (!ref)
149     {
150         if(This->Handle)
151         {
152             EnterCriticalSection(&ddraw_cs);
153             This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
154             This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
155             LeaveCriticalSection(&ddraw_cs);
156         }
157
158         HeapFree(GetProcessHeap(), 0, This);
159         return 0;
160     }
161     return ref;
162 }
163
164 /*****************************************************************************
165  * IDirect3DMaterial Methods
166  *****************************************************************************/
167
168 /*****************************************************************************
169  * IDirect3DMaterial::Initialize
170  *
171  * A no-op initialization
172  *
173  * Params:
174  *  Direct3D: Pointer to a Direct3D interface
175  *
176  * Returns:
177  *  D3D_OK
178  *
179  *****************************************************************************/
180 static HRESULT WINAPI
181 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
182                                   IDirect3D *Direct3D)
183 {
184     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
185
186     TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
187
188     return D3D_OK;
189 }
190
191 /*****************************************************************************
192  * IDirect3DMaterial::Reserve
193  *
194  * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
195  * Odd. They seem to have mixed their interfaces.
196  *
197  * Returns:
198  *  DDERR_UNSUPPORTED
199  *
200  *****************************************************************************/
201 static HRESULT WINAPI
202 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
203 {
204     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
205     TRACE("(%p)->() not implemented\n", This);
206
207     return DDERR_UNSUPPORTED;
208 }
209
210 /*****************************************************************************
211  * IDirect3DMaterial::Unreserve
212  *
213  * Not supported too
214  *
215  * Returns:
216  *  DDERR_UNSUPPORTED
217  *
218  *****************************************************************************/
219 static HRESULT WINAPI
220 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
221 {
222     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
223     TRACE("(%p)->() not implemented.\n", This);
224
225     return DDERR_UNSUPPORTED;
226 }
227
228 /*****************************************************************************
229  * IDirect3DMaterial3::SetMaterial
230  *
231  * Sets the material description
232  *
233  * Params:
234  *  Mat: Material to set
235  *
236  * Returns:
237  *  D3D_OK on success
238  *  DDERR_INVALIDPARAMS if Mat is NULL
239  *
240  *****************************************************************************/
241 static HRESULT WINAPI
242 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
243                                   D3DMATERIAL *lpMat)
244 {
245     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
246     TRACE("(%p)->(%p)\n", This, lpMat);
247     if (TRACE_ON(d3d7))
248         dump_material(lpMat);
249
250     /* Stores the material */
251     EnterCriticalSection(&ddraw_cs);
252     memset(&This->mat, 0, sizeof(This->mat));
253     memcpy(&This->mat, lpMat, lpMat->dwSize);
254     LeaveCriticalSection(&ddraw_cs);
255
256     return DD_OK;
257 }
258
259 /*****************************************************************************
260  * IDirect3DMaterial3::GetMaterial
261  *
262  * Returns the material assigned to this interface
263  *
264  * Params:
265  *  Mat: Pointer to a D3DMATERIAL structure to store the material description
266  *
267  * Returns:
268  *  D3D_OK on success
269  *  DDERR_INVALIDPARAMS if Mat is NULL
270  *
271  *****************************************************************************/
272 static HRESULT WINAPI
273 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
274                                   D3DMATERIAL *lpMat)
275 {
276     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
277     DWORD dwSize;
278     TRACE("(%p)->(%p)\n", This, lpMat);
279     if (TRACE_ON(d3d7)) {
280         TRACE("  Returning material : ");
281         dump_material(&This->mat);
282     }
283
284     /* Copies the material structure */
285     EnterCriticalSection(&ddraw_cs);
286     dwSize = lpMat->dwSize;
287     memset(lpMat, 0, dwSize);
288     memcpy(lpMat, &This->mat, dwSize);
289     LeaveCriticalSection(&ddraw_cs);
290
291     return DD_OK;
292 }
293
294 /*****************************************************************************
295  * IDirect3DMaterial3::GetHandle
296  *
297  * Returns a handle for the material interface. The handle is simply a
298  * pointer to the material implementation
299  *
300  * Params:
301  *  Direct3DDevice3: The device this handle is assigned to
302  *  Handle: Address to write the handle to
303  *
304  * Returns:
305  *  D3D_OK on success
306  *  DDERR_INVALIDPARAMS if Handle is NULL
307  *
308  *****************************************************************************/
309 static HRESULT WINAPI
310 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
311                                 IDirect3DDevice3 *lpDirect3DDevice3,
312                                 D3DMATERIALHANDLE *lpHandle)
313 {
314     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
315     IDirect3DDeviceImpl *device = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, lpDirect3DDevice3);
316     TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
317
318     EnterCriticalSection(&ddraw_cs);
319     This->active_device = device;
320     if(!This->Handle)
321     {
322         This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
323         if(!This->Handle)
324         {
325             ERR("Error creating a handle\n");
326             LeaveCriticalSection(&ddraw_cs);
327             return DDERR_INVALIDPARAMS;   /* Unchecked */
328         }
329         device->Handles[This->Handle - 1].ptr = This;
330         device->Handles[This->Handle - 1].type = DDrawHandle_Material;
331     }
332     *lpHandle = This->Handle;
333     TRACE(" returning handle %08x.\n", *lpHandle);
334     LeaveCriticalSection(&ddraw_cs);
335
336     return D3D_OK;
337 }
338
339 static HRESULT WINAPI
340 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
341                                         LPDIRECT3DDEVICE2 lpDirect3DDevice2,
342                                         LPD3DMATERIALHANDLE lpHandle)
343 {
344     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
345     return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
346                                         COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice2, IDirect3DDevice3, lpDirect3DDevice2),
347                                         lpHandle);
348 }
349
350 static HRESULT WINAPI
351 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
352                                         LPDIRECT3DDEVICE lpDirect3DDevice,
353                                         LPD3DMATERIALHANDLE lpHandle)
354 {
355     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
356     return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
357                                         COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice3, lpDirect3DDevice),
358                                         lpHandle);
359 }
360
361 static HRESULT WINAPI
362 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
363                                              REFIID riid,
364                                              LPVOID* obp)
365 {
366     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
367     return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
368                                              riid,
369                                              obp);
370 }
371
372 static HRESULT WINAPI
373 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
374                                              REFIID riid,
375                                              LPVOID* obp)
376 {
377     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
378     return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
379                                              riid,
380                                              obp);
381 }
382
383 static ULONG WINAPI
384 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
385 {
386     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
387     return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
388 }
389
390 static ULONG WINAPI
391 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
392 {
393     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
394     return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
395 }
396
397 static ULONG WINAPI
398 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
399 {
400     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
401     return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
402 }
403
404 static ULONG WINAPI
405 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
406 {
407     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
408     return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
409 }
410
411 static HRESULT WINAPI
412 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
413                                           LPD3DMATERIAL lpMat)
414 {
415     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
416     return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
417                                           lpMat);
418 }
419
420 static HRESULT WINAPI
421 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
422                                           LPD3DMATERIAL lpMat)
423 {
424     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
425     return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
426                                           lpMat);
427 }
428
429 static HRESULT WINAPI
430 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
431                                           LPD3DMATERIAL lpMat)
432 {
433     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
434     return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
435                                           lpMat);
436 }
437
438 static HRESULT WINAPI
439 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
440                                           LPD3DMATERIAL lpMat)
441 {
442     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
443     return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
444                                           lpMat);
445 }
446
447
448 /*****************************************************************************
449  * material_activate
450  *
451  * Uses IDirect3DDevice7::SetMaterial to activate the material
452  *
453  * Params:
454  *  This: Pointer to the material implementation to activate
455  *
456  *****************************************************************************/
457 void material_activate(IDirect3DMaterialImpl* This)
458 {
459     D3DMATERIAL7 d3d7mat;
460
461     TRACE("Activating material %p\n", This);
462     d3d7mat.u.diffuse = This->mat.u.diffuse;
463     d3d7mat.u1.ambient = This->mat.u1.ambient;
464     d3d7mat.u2.specular = This->mat.u2.specular;
465     d3d7mat.u3.emissive = This->mat.u3.emissive;
466     d3d7mat.u4.power = This->mat.u4.power;
467
468     IDirect3DDevice7_SetMaterial(ICOM_INTERFACE(This->active_device, IDirect3DDevice7),
469                                  &d3d7mat);
470 }
471
472 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
473 {
474     /*** IUnknown Methods ***/
475     IDirect3DMaterialImpl_QueryInterface,
476     IDirect3DMaterialImpl_AddRef,
477     IDirect3DMaterialImpl_Release,
478     /*** IDirect3DMaterial3 Methods ***/
479     IDirect3DMaterialImpl_SetMaterial,
480     IDirect3DMaterialImpl_GetMaterial,
481     IDirect3DMaterialImpl_GetHandle,
482 };
483
484 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
485 {
486     /*** IUnknown Methods ***/
487     Thunk_IDirect3DMaterialImpl_2_QueryInterface,
488     Thunk_IDirect3DMaterialImpl_2_AddRef,
489     Thunk_IDirect3DMaterialImpl_2_Release,
490     /*** IDirect3DMaterial2 Methods ***/
491     Thunk_IDirect3DMaterialImpl_2_SetMaterial,
492     Thunk_IDirect3DMaterialImpl_2_GetMaterial,
493     Thunk_IDirect3DMaterialImpl_2_GetHandle,
494 };
495
496 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
497 {
498     /*** IUnknown Methods ***/
499     Thunk_IDirect3DMaterialImpl_1_QueryInterface,
500     Thunk_IDirect3DMaterialImpl_1_AddRef,
501     Thunk_IDirect3DMaterialImpl_1_Release,
502     /*** IDirect3DMaterial1 Methods ***/
503     IDirect3DMaterialImpl_Initialize,
504     Thunk_IDirect3DMaterialImpl_1_SetMaterial,
505     Thunk_IDirect3DMaterialImpl_1_GetMaterial,
506     Thunk_IDirect3DMaterialImpl_1_GetHandle,
507     IDirect3DMaterialImpl_Reserve,
508     IDirect3DMaterialImpl_Unreserve
509 };