ddraw: Do not crash when Clipper is NULL.
[wine] / dlls / ddraw / viewport.c
1 /* Direct3D Viewport
2  * Copyright (c) 1998 Lionel ULMER
3  * Copyright (c) 2006 Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DViewport2.
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
47 /*****************************************************************************
48  * Helper functions
49  *****************************************************************************/
50
51 /*****************************************************************************
52  * viewport_activate
53  *
54  * activates the viewport using IDirect3DDevice7::SetViewport
55  *
56  *****************************************************************************/
57 void viewport_activate(IDirect3DViewportImpl* This) {
58     IDirect3DLightImpl* light;
59     D3DVIEWPORT7 vp;
60     
61     /* Activate all the lights associated with this context */
62     light = This->lights;
63
64     while (light != NULL) {
65         light->activate(light);
66         light = light->next;
67     }
68
69     /* And copy the values in the structure used by the device */
70     if (This->use_vp2) {
71         vp.dwX = This->viewports.vp2.dwX;
72         vp.dwY = This->viewports.vp2.dwY;
73         vp.dwHeight = This->viewports.vp2.dwHeight;
74         vp.dwWidth = This->viewports.vp2.dwWidth;
75         vp.dvMinZ = This->viewports.vp2.dvMinZ;
76         vp.dvMaxZ = This->viewports.vp2.dvMaxZ;
77     } else {
78         vp.dwX = This->viewports.vp1.dwX;
79         vp.dwY = This->viewports.vp1.dwY;
80         vp.dwHeight = This->viewports.vp1.dwHeight;
81         vp.dwWidth = This->viewports.vp1.dwWidth;
82         vp.dvMinZ = This->viewports.vp1.dvMinZ;
83         vp.dvMaxZ = This->viewports.vp1.dvMaxZ;
84     }
85     
86     /* And also set the viewport */
87     IDirect3DDevice7_SetViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice7), &vp);
88 }
89
90 /*****************************************************************************
91  * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
92  *
93  * Writes viewport information to TRACE
94  *
95  *****************************************************************************/
96 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
97 {
98     TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
99           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
100     TRACE("    - dwWidth = %d   dwHeight = %d\n",
101           lpvp->dwWidth, lpvp->dwHeight);
102     TRACE("    - dvScaleX = %f   dvScaleY = %f\n",
103           lpvp->dvScaleX, lpvp->dvScaleY);
104     TRACE("    - dvMaxX = %f   dvMaxY = %f\n",
105           lpvp->dvMaxX, lpvp->dvMaxY);
106     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
107           lpvp->dvMinZ, lpvp->dvMaxZ);
108 }
109
110 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
111 {
112     TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
113           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
114     TRACE("    - dwWidth = %d   dwHeight = %d\n",
115           lpvp->dwWidth, lpvp->dwHeight);
116     TRACE("    - dvClipX = %f   dvClipY = %f\n",
117           lpvp->dvClipX, lpvp->dvClipY);
118     TRACE("    - dvClipWidth = %f   dvClipHeight = %f\n",
119           lpvp->dvClipWidth, lpvp->dvClipHeight);
120     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
121           lpvp->dvMinZ, lpvp->dvMaxZ);
122 }
123
124 /*****************************************************************************
125  * IUnknown Methods.
126  *****************************************************************************/
127
128 /*****************************************************************************
129  * IDirect3DViewport3::QueryInterface
130  *
131  * A normal QueryInterface. Can query all interface versions and the
132  * IUnknown interface. The VTables of the different versions
133  * are equal
134  *
135  * Params:
136  *  refiid: Interface id queried for
137  *  obj: Address to write the interface pointer to
138  *
139  * Returns:
140  *  S_OK on success.
141  *  E_NOINTERFACE if the requested interface wasn't found
142  *
143  *****************************************************************************/
144 static HRESULT WINAPI
145 IDirect3DViewportImpl_QueryInterface(IDirect3DViewport3 *iface,
146                                      REFIID riid,
147                                      void **obp)
148 {
149     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
150     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
151
152     *obp = NULL;
153
154     if ( IsEqualGUID(&IID_IUnknown,  riid) ||
155          IsEqualGUID(&IID_IDirect3DViewport, riid) ||
156          IsEqualGUID(&IID_IDirect3DViewport2, riid) ||
157          IsEqualGUID(&IID_IDirect3DViewport3, riid) ) {
158         IDirect3DViewport3_AddRef(ICOM_INTERFACE(This, IDirect3DViewport3));
159         *obp = ICOM_INTERFACE(This, IDirect3DViewport3);
160         TRACE("  Creating IDirect3DViewport1/2/3 interface %p\n", *obp);
161         return S_OK;
162     }
163     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
164     return E_NOINTERFACE;
165 }
166
167 /*****************************************************************************
168  * IDirect3DViewport3::AddRef
169  *
170  * Increases the refcount.
171  *
172  * Returns:
173  *  The new refcount
174  *
175  *****************************************************************************/
176 static ULONG WINAPI
177 IDirect3DViewportImpl_AddRef(IDirect3DViewport3 *iface)
178 {
179     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
180     ULONG ref = InterlockedIncrement(&This->ref);
181
182     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
183
184     return ref;
185 }
186
187 /*****************************************************************************
188  * IDirect3DViewport3::Release
189  *
190  * Reduces the refcount. If it falls to 0, the interface is released
191  *
192  * Returns:
193  *  The new refcount
194  *
195  *****************************************************************************/
196 static ULONG WINAPI
197 IDirect3DViewportImpl_Release(IDirect3DViewport3 *iface)
198 {
199     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
200     ULONG ref = InterlockedDecrement(&This->ref);
201
202     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
203
204     if (!ref) {
205         HeapFree(GetProcessHeap(), 0, This);
206         return 0;
207     }
208     return ref;
209 }
210
211 /*****************************************************************************
212  * IDirect3DViewport Methods.
213  *****************************************************************************/
214
215 /*****************************************************************************
216  * IDirect3DViewport3::Initialize
217  *
218  * No-op initialization.
219  *
220  * Params:
221  *  Direct3D: The direct3D device this viewport is assigned to
222  *
223  * Returns:
224  *  DDERR_ALREADYINITIALIZED
225  *
226  *****************************************************************************/
227 static HRESULT WINAPI
228 IDirect3DViewportImpl_Initialize(IDirect3DViewport3 *iface,
229                                  IDirect3D *Direct3D)
230 {
231     TRACE("(%p)->(%p) no-op...\n", iface, Direct3D);
232     return DDERR_ALREADYINITIALIZED;
233 }
234
235 /*****************************************************************************
236  * IDirect3DViewport3::GetViewport
237  *
238  * Returns the viewport data assigned to this viewport interface
239  *
240  * Params:
241  *  Data: Address to store the data
242  *
243  * Returns:
244  *  D3D_OK on success
245  *  DDERR_INVALIDPARAMS if Data is NULL
246  *
247  *****************************************************************************/
248 static HRESULT WINAPI
249 IDirect3DViewportImpl_GetViewport(IDirect3DViewport3 *iface,
250                                   D3DVIEWPORT *lpData)
251 {
252     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
253     DWORD dwSize;
254     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
255     if (This->use_vp2 != 0) {
256         ERR("  Requesting to get a D3DVIEWPORT struct where a D3DVIEWPORT2 was set !\n");
257         return DDERR_INVALIDPARAMS;
258     }
259     dwSize = lpData->dwSize;
260     memset(lpData, 0, dwSize);
261     memcpy(lpData, &(This->viewports.vp1), dwSize);
262
263     if (TRACE_ON(d3d7)) {
264         TRACE("  returning D3DVIEWPORT :\n");
265         _dump_D3DVIEWPORT(lpData);
266     }
267     
268     return DD_OK;
269 }
270
271 /*****************************************************************************
272  * IDirect3DViewport3::SetViewport
273  *
274  * Sets the viewport information for this interface
275  *
276  * Params:
277  *  lpData: Viewport to set
278  *
279  * Returns:
280  *  D3D_OK on success
281  *  DDERR_INVALIDPARAMS if Data is NULL
282  *
283  *****************************************************************************/
284 static HRESULT WINAPI
285 IDirect3DViewportImpl_SetViewport(IDirect3DViewport3 *iface,
286                                   D3DVIEWPORT *lpData)
287 {
288     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
289     LPDIRECT3DVIEWPORT3 current_viewport;
290     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
291
292     if (TRACE_ON(d3d7)) {
293         TRACE("  getting D3DVIEWPORT :\n");
294         _dump_D3DVIEWPORT(lpData);
295     }
296
297     This->use_vp2 = 0;
298     memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
299     memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
300
301     /* Tests on two games show that these values are never used properly so override
302        them with proper ones :-)
303     */
304     This->viewports.vp1.dvMinZ = 0.0;
305     This->viewports.vp1.dvMaxZ = 1.0;
306
307     if (This->active_device) {
308       IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice3), &current_viewport);
309       if (ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, current_viewport) == This)
310           This->activate(This);
311       if(current_viewport) IDirect3DViewport3_Release(current_viewport);
312     }
313
314     return DD_OK;
315 }
316
317 /*****************************************************************************
318  * IDirect3DViewport3::TransformVertices
319  *
320  * Transforms vertices by the transformation matrix.
321  *
322  * Params:
323  *  dwVertexCount: The number of vertices to be transformed
324  *  lpData: Pointer to the vertex data
325  *  dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
326  *  lpOffScreen: Is set to nonzero if all vertices are off-screen
327  *
328  * Returns:
329  *  D3D_OK because it's a stub
330  *
331  *****************************************************************************/
332 static HRESULT WINAPI
333 IDirect3DViewportImpl_TransformVertices(IDirect3DViewport3 *iface,
334                                         DWORD dwVertexCount,
335                                         D3DTRANSFORMDATA *lpData,
336                                         DWORD dwFlags,
337                                         DWORD *lpOffScreen)
338 {
339     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
340     FIXME("(%p)->(%08x,%p,%08x,%p): stub!\n", This, dwVertexCount, lpData, dwFlags, lpOffScreen);
341     if (lpOffScreen)
342         *lpOffScreen = 0;
343     return DD_OK;
344 }
345
346 /*****************************************************************************
347  * IDirect3DViewport3::LightElements
348  *
349  * The DirectX 5.0 sdk says that it's not implemented
350  *
351  * Params:
352  *  ?
353  *
354  * Returns:
355  *  DDERR_UNSUPPORTED
356  *
357  *****************************************************************************/
358 static HRESULT WINAPI
359 IDirect3DViewportImpl_LightElements(IDirect3DViewport3 *iface,
360                                     DWORD dwElementCount,
361                                     LPD3DLIGHTDATA lpData)
362 {
363     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
364     TRACE("(%p)->(%08x,%p): Unimplemented!\n", This, dwElementCount, lpData);
365     return DDERR_UNSUPPORTED;
366 }
367
368 /*****************************************************************************
369  * IDirect3DViewport3::SetBackground
370  *
371  * Sets tje background material
372  *
373  * Params:
374  *  hMat: Handle from a IDirect3DMaterial interface
375  *
376  * Returns:
377  *  D3D_OK on success
378  *
379  *****************************************************************************/
380 static HRESULT WINAPI
381 IDirect3DViewportImpl_SetBackground(IDirect3DViewport3 *iface,
382                                     D3DMATERIALHANDLE hMat)
383 {
384     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
385     TRACE("(%p)->(%d)\n", This, (DWORD) hMat);
386
387     if(hMat && hMat > This->ddraw->d3ddevice->numHandles)
388     {
389         WARN("Specified Handle %d out of range\n", hMat);
390         return DDERR_INVALIDPARAMS;
391     }
392     else if(hMat && This->ddraw->d3ddevice->Handles[hMat - 1].type != DDrawHandle_Material)
393     {
394         WARN("Handle %d is not a material handle\n", hMat);
395         return DDERR_INVALIDPARAMS;
396     }
397
398     if(hMat)
399     {
400         This->background = (IDirect3DMaterialImpl *) This->ddraw->d3ddevice->Handles[hMat - 1].ptr;
401         TRACE(" setting background color : %f %f %f %f\n",
402               This->background->mat.u.diffuse.u1.r,
403               This->background->mat.u.diffuse.u2.g,
404               This->background->mat.u.diffuse.u3.b,
405               This->background->mat.u.diffuse.u4.a);
406     }
407     else
408     {
409         This->background = NULL;
410         TRACE("Setting background to NULL\n");
411     }
412
413     return D3D_OK;
414 }
415
416 /*****************************************************************************
417  * IDirect3DViewport3::GetBackground
418  *
419  * Returns the material handle assigned to the background of the viewport
420  *
421  * Params:
422  *  lphMat: Address to store the handle
423  *  lpValid: is set to FALSE if no background is set, TRUE if one is set
424  *
425  * Returns:
426  *  D3D_OK
427  *
428  *****************************************************************************/
429 static HRESULT WINAPI
430 IDirect3DViewportImpl_GetBackground(IDirect3DViewport3 *iface,
431                                     D3DMATERIALHANDLE *lphMat,
432                                     BOOL *lpValid)
433 {
434     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
435     TRACE("(%p)->(%p,%p)\n", This, lphMat, lpValid);
436
437     if(lpValid)
438     {
439         *lpValid = This->background != NULL;
440     }
441     if(lphMat)
442     {
443         if(This->background)
444         {
445             *lphMat = This->background->Handle;
446         }
447         else
448         {
449             *lphMat = 0;
450         }
451     }
452
453     return D3D_OK;
454 }
455
456 /*****************************************************************************
457  * IDirect3DViewport3::SetBackgroundDepth
458  *
459  * Sets a surface that represents the background depth. It's contents are
460  * used to set the depth buffer in IDirect3DViewport3::Clear
461  *
462  * Params:
463  *  lpDDSurface: Surface to set
464  *
465  * Returns: D3D_OK, because it's a stub
466  *
467  *****************************************************************************/
468 static HRESULT WINAPI
469 IDirect3DViewportImpl_SetBackgroundDepth(IDirect3DViewport3 *iface,
470                                          IDirectDrawSurface *lpDDSurface)
471 {
472     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
473     FIXME("(%p)->(%p): stub!\n", This, lpDDSurface);
474     return D3D_OK;
475 }
476
477 /*****************************************************************************
478  * IDirect3DViewport3::GetBackgroundDepth
479  *
480  * Returns the surface that represents the depth field
481  *
482  * Params:
483  *  lplpDDSurface: Address to store the interface pointer
484  *  lpValid: Set to TRUE if a depth is asigned, FALSE otherwise
485  *
486  * Returns:
487  *  D3D_OK, because it's a stub
488  *  (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
489  *
490  *****************************************************************************/
491 static HRESULT WINAPI
492 IDirect3DViewportImpl_GetBackgroundDepth(IDirect3DViewport3 *iface,
493                                          IDirectDrawSurface **lplpDDSurface,
494                                          LPBOOL lpValid)
495 {
496     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
497     FIXME("(%p)->(%p,%p): stub!\n", This, lplpDDSurface, lpValid);
498     return DD_OK;
499 }
500
501 /*****************************************************************************
502  * IDirect3DViewport3::Clear
503  *
504  * Clears the render target and / or the z buffer
505  *
506  * Params:
507  *  dwCount: The amount of rectangles to clear. If 0, the whole buffer is
508  *           cleared
509  *  lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
510  *  dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
511  *
512  * Returns:
513  *  D3D_OK on success
514  *  D3DERR_VIEWPORTHASNODEVICE if there's no active device
515  *  The return value of IDirect3DDevice7::Clear
516  *
517  *****************************************************************************/
518 static HRESULT WINAPI
519 IDirect3DViewportImpl_Clear(IDirect3DViewport3 *iface,
520                             DWORD dwCount, 
521                             D3DRECT *lpRects,
522                             DWORD dwFlags)
523 {
524     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
525     DWORD color = 0x00000000;
526     
527     TRACE("(%p/%p)->(%08x,%p,%08x)\n", This, iface, dwCount, lpRects, dwFlags);
528     if (This->active_device == NULL) {
529         ERR(" Trying to clear a viewport not attached to a device !\n");
530         return D3DERR_VIEWPORTHASNODEVICE;
531     }
532     if (dwFlags & D3DCLEAR_TARGET) {
533         if (This->background == NULL) {
534             ERR(" Trying to clear the color buffer without background material !\n");
535         } else {
536             color = 
537               ((int) ((This->background->mat.u.diffuse.u1.r) * 255) << 16) |
538               ((int) ((This->background->mat.u.diffuse.u2.g) * 255) <<  8) |
539               ((int) ((This->background->mat.u.diffuse.u3.b) * 255) <<  0) |
540               ((int) ((This->background->mat.u.diffuse.u4.a) * 255) << 24);
541         }
542     }
543
544     return IDirect3DDevice7_Clear(ICOM_INTERFACE(This->active_device, IDirect3DDevice7),
545                                   dwCount,
546                                   lpRects,
547                                   dwFlags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET),
548                                   color,
549                                   1.0,
550                                   0x00000000);
551 }
552
553 /*****************************************************************************
554  * IDirect3DViewport3::AddLight
555  *
556  * Adds an light to the viewport
557  *
558  * Params:
559  *  lpDirect3DLight: Interface of the light to add
560  *
561  * Returns:
562  *  D3D_OK on success
563  *  DDERR_INVALIDPARAMS if Direct3DLight is NULL
564  *  DDERR_INVALIDPARAMS if there are 8 lights or more
565  *
566  *****************************************************************************/
567 static HRESULT WINAPI
568 IDirect3DViewportImpl_AddLight(IDirect3DViewport3 *iface,
569                                IDirect3DLight *lpDirect3DLight)
570 {
571     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
572     IDirect3DLightImpl *lpDirect3DLightImpl = ICOM_OBJECT(IDirect3DLightImpl, IDirect3DLight, lpDirect3DLight);
573     DWORD i = 0;
574     DWORD map = This->map_lights;
575     
576     TRACE("(%p)->(%p)\n", This, lpDirect3DLight);
577     
578     if (This->num_lights >= 8)
579         return DDERR_INVALIDPARAMS;
580
581     /* Find a light number and update both light and viewports objects accordingly */
582     while(map&1) {
583         map>>=1;
584         i++;
585     }
586     lpDirect3DLightImpl->dwLightIndex = i;
587     This->num_lights++;
588     This->map_lights |= 1<<i;
589
590     /* Add the light in the 'linked' chain */
591     lpDirect3DLightImpl->next = This->lights;
592     This->lights = lpDirect3DLightImpl;
593
594     /* Attach the light to the viewport */
595     lpDirect3DLightImpl->active_viewport = This;
596     
597     /* If active, activate the light */
598     if (This->active_device != NULL) {
599         lpDirect3DLightImpl->activate(lpDirect3DLightImpl);
600     }
601     
602     return D3D_OK;
603 }
604
605 /*****************************************************************************
606  * IDirect3DViewport3::DeleteLight
607  *
608  * Deletes a light from the viewports' light list
609  *
610  * Params:
611  *  lpDirect3DLight: Light to delete
612  *
613  * Returns:
614  *  D3D_OK on success
615  *  DDERR_INVALIDPARAMS if the light wasn't found
616  *
617  *****************************************************************************/
618 static HRESULT WINAPI
619 IDirect3DViewportImpl_DeleteLight(IDirect3DViewport3 *iface,
620                                   IDirect3DLight *lpDirect3DLight)
621 {
622     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
623     IDirect3DLightImpl *lpDirect3DLightImpl = ICOM_OBJECT(IDirect3DLightImpl, IDirect3DLight, lpDirect3DLight);
624     IDirect3DLightImpl *cur_light, *prev_light = NULL;
625     
626     TRACE("(%p)->(%p)\n", This, lpDirect3DLight);
627     cur_light = This->lights;
628     while (cur_light != NULL) {
629         if (cur_light == lpDirect3DLightImpl) {
630             lpDirect3DLightImpl->desactivate(lpDirect3DLightImpl);
631             if (prev_light == NULL) This->lights = cur_light->next;
632             else prev_light->next = cur_light->next;
633             /* Detach the light to the viewport */
634             cur_light->active_viewport = NULL;
635             This->num_lights--;
636             This->map_lights &= ~(1<<lpDirect3DLightImpl->dwLightIndex);
637             return D3D_OK;
638         }
639         prev_light = cur_light;
640         cur_light = cur_light->next;
641     }
642     return DDERR_INVALIDPARAMS;
643 }
644
645 /*****************************************************************************
646  * IDirect3DViewport::NextLight
647  *
648  * Enumerates the lights associated with the viewport
649  *
650  * Params:
651  *  lpDirect3DLight: Light to start with
652  *  lplpDirect3DLight: Address to store the successor to
653  *
654  * Returns:
655  *  D3D_OK, because it's a stub
656  *
657  *****************************************************************************/
658 static HRESULT WINAPI
659 IDirect3DViewportImpl_NextLight(IDirect3DViewport3 *iface,
660                                 IDirect3DLight *lpDirect3DLight,
661                                 IDirect3DLight **lplpDirect3DLight,
662                                 DWORD dwFlags)
663 {
664     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
665     FIXME("(%p)->(%p,%p,%08x): stub!\n", This, lpDirect3DLight, lplpDirect3DLight, dwFlags);
666     return D3D_OK;
667 }
668
669 /*****************************************************************************
670  * IDirect3DViewport2 Methods.
671  *****************************************************************************/
672
673 /*****************************************************************************
674  * IDirect3DViewport3::GetViewport2
675  *
676  * Returns the currently set viewport in a D3DVIEWPORT2 structure.
677  * Similar to IDirect3DViewport3::GetViewport
678  *
679  * Params:
680  *  lpData: Pointer to the structure to fill
681  *
682  * Returns:
683  *  D3D_OK on success
684  *  DDERR_INVALIDPARAMS if the viewport was set with
685  *                      IDirect3DViewport3::SetViewport
686  *  DDERR_INVALIDPARAMS if Data is NULL
687  *
688  *****************************************************************************/
689 static HRESULT WINAPI
690 IDirect3DViewportImpl_GetViewport2(IDirect3DViewport3 *iface,
691                                    D3DVIEWPORT2 *lpData)
692 {
693     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
694     DWORD dwSize;
695     TRACE("(%p)->(%p)\n", This, lpData);
696     if (This->use_vp2 != 1) {
697         ERR("  Requesting to get a D3DVIEWPORT2 struct where a D3DVIEWPORT was set !\n");
698         return DDERR_INVALIDPARAMS;
699     }
700     dwSize = lpData->dwSize;
701     memset(lpData, 0, dwSize);
702     memcpy(lpData, &(This->viewports.vp2), dwSize);
703
704     if (TRACE_ON(d3d7)) {
705         TRACE("  returning D3DVIEWPORT2 :\n");
706         _dump_D3DVIEWPORT2(lpData);
707     }
708     
709     return D3D_OK;
710 }
711
712 /*****************************************************************************
713  * IDirect3DViewport3::SetViewport2
714  *
715  * Sets the viewport from a D3DVIEWPORT2 structure
716  *
717  * Params:
718  *  lpData: Viewport to set
719  *
720  * Returns:
721  *  D3D_OK on success
722  *
723  *****************************************************************************/
724 static HRESULT WINAPI
725 IDirect3DViewportImpl_SetViewport2(IDirect3DViewport3 *iface,
726                                    D3DVIEWPORT2 *lpData)
727 {
728     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
729     LPDIRECT3DVIEWPORT3 current_viewport;
730     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
731
732     if (TRACE_ON(d3d7)) {
733         TRACE("  getting D3DVIEWPORT2 :\n");
734         _dump_D3DVIEWPORT2(lpData);
735     }
736
737     This->use_vp2 = 1;
738     memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
739     memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
740
741     if (This->active_device) {
742       IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice3), &current_viewport);
743       if (ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, current_viewport) == This)
744         This->activate(This);
745       IDirect3DViewport3_Release(current_viewport);
746     }
747
748     return D3D_OK;
749 }
750
751 /*****************************************************************************
752  * IDirect3DViewport3 Methods.
753  *****************************************************************************/
754
755 /*****************************************************************************
756  * IDirect3DViewport3::SetBackgroundDepth2
757  *
758  * Sets a IDirectDrawSurface4 surface as the background depth surface
759  *
760  * Params:
761  *  lpDDS: Surface to set
762  *
763  * Returns:
764  *  D3D_OK, because it's stub
765  *
766  *****************************************************************************/
767 static HRESULT WINAPI
768 IDirect3DViewportImpl_SetBackgroundDepth2(IDirect3DViewport3 *iface,
769                                           IDirectDrawSurface4 *lpDDS)
770 {
771     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
772     FIXME("(%p)->(%p): stub!\n", This, lpDDS);
773     return D3D_OK;
774 }
775
776 /*****************************************************************************
777  * IDirect3DViewport3::GetBackgroundDepth2
778  *
779  * Returns the IDirect3DSurface4 interface to the background depth surface
780  *
781  * Params:
782  *  lplpDDS: Address to store the interface pointer at
783  *  lpValid: Set to true if a surface is assigned
784  *
785  * Returns:
786  *  D3D_OK because it's a stub
787  *
788  *****************************************************************************/
789 static HRESULT WINAPI
790 IDirect3DViewportImpl_GetBackgroundDepth2(IDirect3DViewport3 *iface,
791                                           IDirectDrawSurface4 **lplpDDS,
792                                           BOOL *lpValid)
793 {
794     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
795     FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDS, lpValid);
796     return D3D_OK;
797 }
798
799 /*****************************************************************************
800  * IDirect3DViewport3::Clear2
801  *
802  * Another clearing method
803  *
804  * Params:
805  *  Count: Number of rectangles to clear
806  *  Rects: Rectangle array to clear
807  *  Flags: Some flags :)
808  *  Color: Color to fill the render target with
809  *  Z: Value to fill the depth buffer with
810  *  Stencil: Value to fill the stencil bits with
811  *
812  * Returns:
813  *
814  *****************************************************************************/
815 static HRESULT WINAPI
816 IDirect3DViewportImpl_Clear2(IDirect3DViewport3 *iface,
817                              DWORD dwCount,
818                              LPD3DRECT lpRects,
819                              DWORD dwFlags,
820                              DWORD dwColor,
821                              D3DVALUE dvZ,
822                              DWORD dwStencil)
823 {
824     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
825     TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x)\n", This, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
826     if (This->active_device == NULL) {
827         ERR(" Trying to clear a viewport not attached to a device !\n");
828         return D3DERR_VIEWPORTHASNODEVICE;
829     }
830     return IDirect3DDevice7_Clear(ICOM_INTERFACE(This->active_device, IDirect3DDevice7),
831                                   dwCount,
832                                   lpRects,
833                                   dwFlags,
834                                   dwColor,
835                                   dvZ,
836                                   dwStencil);
837 }
838
839 /*****************************************************************************
840  * The VTable
841  *****************************************************************************/
842
843 const IDirect3DViewport3Vtbl IDirect3DViewport3_Vtbl =
844 {
845     /*** IUnknown Methods ***/
846     IDirect3DViewportImpl_QueryInterface,
847     IDirect3DViewportImpl_AddRef,
848     IDirect3DViewportImpl_Release,
849     /*** IDirect3DViewport Methods */
850     IDirect3DViewportImpl_Initialize,
851     IDirect3DViewportImpl_GetViewport,
852     IDirect3DViewportImpl_SetViewport,
853     IDirect3DViewportImpl_TransformVertices,
854     IDirect3DViewportImpl_LightElements,
855     IDirect3DViewportImpl_SetBackground,
856     IDirect3DViewportImpl_GetBackground,
857     IDirect3DViewportImpl_SetBackgroundDepth,
858     IDirect3DViewportImpl_GetBackgroundDepth,
859     IDirect3DViewportImpl_Clear,
860     IDirect3DViewportImpl_AddLight,
861     IDirect3DViewportImpl_DeleteLight,
862     IDirect3DViewportImpl_NextLight,
863     /*** IDirect3DViewport2 Methods ***/
864     IDirect3DViewportImpl_GetViewport2,
865     IDirect3DViewportImpl_SetViewport2,
866     /*** IDirect3DViewport3 Methods ***/
867     IDirect3DViewportImpl_SetBackgroundDepth2,
868     IDirect3DViewportImpl_GetBackgroundDepth2,
869     IDirect3DViewportImpl_Clear2,
870 };