ddraw: Convert VB lock flags to wined3d flags.
[wine] / dlls / ddraw / viewport.c
1 /* Direct3D Viewport
2  * Copyright (c) 1998 Lionel ULMER
3  * Copyright (c) 2006-2007 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, BOOL ignore_lights) {
58     IDirect3DLightImpl* light;
59     D3DVIEWPORT7 vp;
60
61     if (!ignore_lights) {
62         /* Activate all the lights associated with this context */
63         light = This->lights;
64
65         while (light != NULL) {
66             light->activate(light);
67             light = light->next;
68         }
69     }
70
71     /* And copy the values in the structure used by the device */
72     if (This->use_vp2) {
73         vp.dwX = This->viewports.vp2.dwX;
74         vp.dwY = This->viewports.vp2.dwY;
75         vp.dwHeight = This->viewports.vp2.dwHeight;
76         vp.dwWidth = This->viewports.vp2.dwWidth;
77         vp.dvMinZ = This->viewports.vp2.dvMinZ;
78         vp.dvMaxZ = This->viewports.vp2.dvMaxZ;
79     } else {
80         vp.dwX = This->viewports.vp1.dwX;
81         vp.dwY = This->viewports.vp1.dwY;
82         vp.dwHeight = This->viewports.vp1.dwHeight;
83         vp.dwWidth = This->viewports.vp1.dwWidth;
84         vp.dvMinZ = This->viewports.vp1.dvMinZ;
85         vp.dvMaxZ = This->viewports.vp1.dvMaxZ;
86     }
87
88     /* And also set the viewport */
89     IDirect3DDevice7_SetViewport((IDirect3DDevice7 *)This->active_device, &vp);
90 }
91
92 /*****************************************************************************
93  * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
94  *
95  * Writes viewport information to TRACE
96  *
97  *****************************************************************************/
98 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
99 {
100     TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
101           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
102     TRACE("    - dwWidth = %d   dwHeight = %d\n",
103           lpvp->dwWidth, lpvp->dwHeight);
104     TRACE("    - dvScaleX = %f   dvScaleY = %f\n",
105           lpvp->dvScaleX, lpvp->dvScaleY);
106     TRACE("    - dvMaxX = %f   dvMaxY = %f\n",
107           lpvp->dvMaxX, lpvp->dvMaxY);
108     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
109           lpvp->dvMinZ, lpvp->dvMaxZ);
110 }
111
112 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
113 {
114     TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
115           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
116     TRACE("    - dwWidth = %d   dwHeight = %d\n",
117           lpvp->dwWidth, lpvp->dwHeight);
118     TRACE("    - dvClipX = %f   dvClipY = %f\n",
119           lpvp->dvClipX, lpvp->dvClipY);
120     TRACE("    - dvClipWidth = %f   dvClipHeight = %f\n",
121           lpvp->dvClipWidth, lpvp->dvClipHeight);
122     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
123           lpvp->dvMinZ, lpvp->dvMaxZ);
124 }
125
126 /*****************************************************************************
127  * IUnknown Methods.
128  *****************************************************************************/
129
130 /*****************************************************************************
131  * IDirect3DViewport3::QueryInterface
132  *
133  * A normal QueryInterface. Can query all interface versions and the
134  * IUnknown interface. The VTables of the different versions
135  * are equal
136  *
137  * Params:
138  *  refiid: Interface id queried for
139  *  obj: Address to write the interface pointer to
140  *
141  * Returns:
142  *  S_OK on success.
143  *  E_NOINTERFACE if the requested interface wasn't found
144  *
145  *****************************************************************************/
146 static HRESULT WINAPI
147 IDirect3DViewportImpl_QueryInterface(IDirect3DViewport3 *iface,
148                                      REFIID riid,
149                                      void **obp)
150 {
151     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), obp);
152
153     *obp = NULL;
154
155     if ( IsEqualGUID(&IID_IUnknown,  riid) ||
156          IsEqualGUID(&IID_IDirect3DViewport, riid) ||
157          IsEqualGUID(&IID_IDirect3DViewport2, riid) ||
158          IsEqualGUID(&IID_IDirect3DViewport3, riid) ) {
159         IDirect3DViewport3_AddRef(iface);
160         *obp = iface;
161         TRACE("  Creating IDirect3DViewport1/2/3 interface %p\n", *obp);
162         return S_OK;
163     }
164     FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
165     return E_NOINTERFACE;
166 }
167
168 /*****************************************************************************
169  * IDirect3DViewport3::AddRef
170  *
171  * Increases the refcount.
172  *
173  * Returns:
174  *  The new refcount
175  *
176  *****************************************************************************/
177 static ULONG WINAPI
178 IDirect3DViewportImpl_AddRef(IDirect3DViewport3 *iface)
179 {
180     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
181     ULONG ref = InterlockedIncrement(&This->ref);
182
183     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
184
185     return ref;
186 }
187
188 /*****************************************************************************
189  * IDirect3DViewport3::Release
190  *
191  * Reduces the refcount. If it falls to 0, the interface is released
192  *
193  * Returns:
194  *  The new refcount
195  *
196  *****************************************************************************/
197 static ULONG WINAPI
198 IDirect3DViewportImpl_Release(IDirect3DViewport3 *iface)
199 {
200     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
201     ULONG ref = InterlockedDecrement(&This->ref);
202
203     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
204
205     if (!ref) {
206         HeapFree(GetProcessHeap(), 0, This);
207         return 0;
208     }
209     return ref;
210 }
211
212 /*****************************************************************************
213  * IDirect3DViewport Methods.
214  *****************************************************************************/
215
216 /*****************************************************************************
217  * IDirect3DViewport3::Initialize
218  *
219  * No-op initialization.
220  *
221  * Params:
222  *  Direct3D: The direct3D device this viewport is assigned to
223  *
224  * Returns:
225  *  DDERR_ALREADYINITIALIZED
226  *
227  *****************************************************************************/
228 static HRESULT WINAPI
229 IDirect3DViewportImpl_Initialize(IDirect3DViewport3 *iface,
230                                  IDirect3D *Direct3D)
231 {
232     TRACE("(%p)->(%p) no-op...\n", iface, Direct3D);
233     return DDERR_ALREADYINITIALIZED;
234 }
235
236 /*****************************************************************************
237  * IDirect3DViewport3::GetViewport
238  *
239  * Returns the viewport data assigned to this viewport interface
240  *
241  * Params:
242  *  Data: Address to store the data
243  *
244  * Returns:
245  *  D3D_OK on success
246  *  DDERR_INVALIDPARAMS if Data is NULL
247  *
248  *****************************************************************************/
249 static HRESULT WINAPI
250 IDirect3DViewportImpl_GetViewport(IDirect3DViewport3 *iface,
251                                   D3DVIEWPORT *lpData)
252 {
253     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
254     DWORD dwSize;
255     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
256
257     EnterCriticalSection(&ddraw_cs);
258     if (This->use_vp2 != 0) {
259         ERR("  Requesting to get a D3DVIEWPORT struct where a D3DVIEWPORT2 was set !\n");
260         LeaveCriticalSection(&ddraw_cs);
261         return DDERR_INVALIDPARAMS;
262     }
263     dwSize = lpData->dwSize;
264     memset(lpData, 0, dwSize);
265     memcpy(lpData, &(This->viewports.vp1), dwSize);
266
267     if (TRACE_ON(d3d7)) {
268         TRACE("  returning D3DVIEWPORT :\n");
269         _dump_D3DVIEWPORT(lpData);
270     }
271     LeaveCriticalSection(&ddraw_cs);
272
273     return DD_OK;
274 }
275
276 /*****************************************************************************
277  * IDirect3DViewport3::SetViewport
278  *
279  * Sets the viewport information for this interface
280  *
281  * Params:
282  *  lpData: Viewport to set
283  *
284  * Returns:
285  *  D3D_OK on success
286  *  DDERR_INVALIDPARAMS if Data is NULL
287  *
288  *****************************************************************************/
289 static HRESULT WINAPI
290 IDirect3DViewportImpl_SetViewport(IDirect3DViewport3 *iface,
291                                   D3DVIEWPORT *lpData)
292 {
293     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
294     LPDIRECT3DVIEWPORT3 current_viewport;
295     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
296
297     if (TRACE_ON(d3d7)) {
298         TRACE("  getting D3DVIEWPORT :\n");
299         _dump_D3DVIEWPORT(lpData);
300     }
301
302     EnterCriticalSection(&ddraw_cs);
303     This->use_vp2 = 0;
304     memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
305     memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
306
307     /* Tests on two games show that these values are never used properly so override
308        them with proper ones :-)
309     */
310     This->viewports.vp1.dvMinZ = 0.0;
311     This->viewports.vp1.dvMaxZ = 1.0;
312
313     if (This->active_device) {
314         IDirect3DDevice3 *d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
315         IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
316         if (current_viewport) {
317             if ((IDirect3DViewportImpl *)current_viewport == This) This->activate(This, FALSE);
318             IDirect3DViewport3_Release(current_viewport);
319         }
320     }
321     LeaveCriticalSection(&ddraw_cs);
322
323     return DD_OK;
324 }
325
326 /*****************************************************************************
327  * IDirect3DViewport3::TransformVertices
328  *
329  * Transforms vertices by the transformation matrix.
330  *
331  * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
332  * so it's tempting to forward it to there. However, there are some
333  * tiny differences. First, the lpOffscreen flag that is reported back,
334  * then there is the homogeneous vertex that is generated. Also there's a lack
335  * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
336  * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
337  * ProcessVertices doesn't pay of in terms of wrapper code needed and code
338  * reused.
339  *
340  * Params:
341  *  dwVertexCount: The number of vertices to be transformed
342  *  lpData: Pointer to the vertex data
343  *  dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
344  *  lpOffScreen: Set to the clipping plane clipping the vertex, if only one
345  *               vertex is transformed and clipping is on. 0 otherwise
346  *
347  * Returns:
348  *  D3D_OK on success
349  *  D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
350  *  DDERR_INVALIDPARAMS if no clipping flag is specified
351  *
352  *****************************************************************************/
353 static HRESULT WINAPI
354 IDirect3DViewportImpl_TransformVertices(IDirect3DViewport3 *iface,
355                                         DWORD dwVertexCount,
356                                         D3DTRANSFORMDATA *lpData,
357                                         DWORD dwFlags,
358                                         DWORD *lpOffScreen)
359 {
360     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
361     D3DMATRIX view_mat, world_mat, proj_mat, mat;
362     float *in;
363     float *out;
364     float x, y, z, w;
365     unsigned int i;
366     D3DVIEWPORT vp = This->viewports.vp1;
367     D3DHVERTEX *outH;
368     TRACE("(%p)->(%08x,%p,%08x,%p)\n", This, dwVertexCount, lpData, dwFlags, lpOffScreen);
369
370     /* Tests on windows show that Windows crashes when this occurs,
371      * so don't return the (intuitive) return value
372     if(!This->active_device)
373     {
374         WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
375         return D3DERR_VIEWPORTHASNODEVICE;
376     }
377      */
378
379     if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
380     {
381         WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
382         return DDERR_INVALIDPARAMS;
383     }
384
385
386     EnterCriticalSection(&ddraw_cs);
387     IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
388                                 D3DTRANSFORMSTATE_VIEW,
389                                 (WINED3DMATRIX*) &view_mat);
390
391     IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
392                                 D3DTRANSFORMSTATE_PROJECTION,
393                                 (WINED3DMATRIX*) &proj_mat);
394
395     IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
396                                 WINED3DTS_WORLDMATRIX(0),
397                                 (WINED3DMATRIX*) &world_mat);
398     multiply_matrix(&mat,&view_mat,&world_mat);
399     multiply_matrix(&mat,&proj_mat,&mat);
400
401     in = lpData->lpIn;
402     out = lpData->lpOut;
403     outH = lpData->lpHOut;
404     for(i = 0; i < dwVertexCount; i++)
405     {
406         x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + (1.0 * mat._41);
407         y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + (1.0 * mat._42);
408         z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + (1.0 * mat._43);
409         w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + (1.0 * mat._44);
410
411         if(dwFlags & D3DTRANSFORM_CLIPPED)
412         {
413             /* If clipping is enabled, Windows assumes that outH is
414              * a valid pointer
415              */
416             outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
417
418             outH[i].dwFlags = 0;
419             if(x * vp.dvScaleX > ((float) vp.dwWidth * 0.5))
420                 outH[i].dwFlags |= D3DCLIP_RIGHT;
421             if(x * vp.dvScaleX <= -((float) vp.dwWidth) * 0.5)
422                 outH[i].dwFlags |= D3DCLIP_LEFT;
423             if(y * vp.dvScaleY > ((float) vp.dwHeight * 0.5))
424                 outH[i].dwFlags |= D3DCLIP_TOP;
425             if(y * vp.dvScaleY <= -((float) vp.dwHeight) * 0.5)
426                 outH[i].dwFlags |= D3DCLIP_BOTTOM;
427             if(z < 0.0)
428                 outH[i].dwFlags |= D3DCLIP_FRONT;
429             if(z > 1.0)
430                 outH[i].dwFlags |= D3DCLIP_BACK;
431
432             if(outH[i].dwFlags)
433             {
434                 /* Looks like native just drops the vertex, leaves whatever data
435                  * it has in the output buffer and goes on with the next vertex.
436                  * The exact scheme hasn't been figured out yet, but windows
437                  * definitely writes something there.
438                  */
439                 out[0] = x;
440                 out[1] = y;
441                 out[2] = z;
442                 out[3] = w;
443                 in = (float *) ((char *) in + lpData->dwInSize);
444                 out = (float *) ((char *) out + lpData->dwOutSize);
445                 continue;
446             }
447         }
448
449         w = 1 / w;
450         x *= w; y *= w; z *= w;
451
452         out[0] = vp.dwWidth / 2 + vp.dwX + x * vp.dvScaleX;
453         out[1] = vp.dwHeight / 2 + vp.dwY - y * vp.dvScaleY;
454         out[2] = z;
455         out[3] = w;
456         in = (float *) ((char *) in + lpData->dwInSize);
457         out = (float *) ((char *) out + lpData->dwOutSize);
458     }
459
460     /* According to the d3d test, the offscreen flag is set only
461      * if exactly one vertex is transformed. Its not documented,
462      * but the test shows that the lpOffscreen flag is set to the
463      * flag combination of clipping planes that clips the vertex.
464      *
465      * If clipping is requested, Windows assumes that the offscreen
466      * param is a valid pointer.
467      */
468     if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
469     {
470         *lpOffScreen = outH[0].dwFlags;
471     }
472     else if(*lpOffScreen)
473     {
474         *lpOffScreen = 0;
475     }
476     LeaveCriticalSection(&ddraw_cs);
477
478     TRACE("All done\n");
479     return DD_OK;
480 }
481
482 /*****************************************************************************
483  * IDirect3DViewport3::LightElements
484  *
485  * The DirectX 5.0 sdk says that it's not implemented
486  *
487  * Params:
488  *  ?
489  *
490  * Returns:
491  *  DDERR_UNSUPPORTED
492  *
493  *****************************************************************************/
494 static HRESULT WINAPI
495 IDirect3DViewportImpl_LightElements(IDirect3DViewport3 *iface,
496                                     DWORD dwElementCount,
497                                     LPD3DLIGHTDATA lpData)
498 {
499     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
500     TRACE("(%p)->(%08x,%p): Unimplemented!\n", This, dwElementCount, lpData);
501     return DDERR_UNSUPPORTED;
502 }
503
504 /*****************************************************************************
505  * IDirect3DViewport3::SetBackground
506  *
507  * Sets tje background material
508  *
509  * Params:
510  *  hMat: Handle from a IDirect3DMaterial interface
511  *
512  * Returns:
513  *  D3D_OK on success
514  *
515  *****************************************************************************/
516 static HRESULT WINAPI
517 IDirect3DViewportImpl_SetBackground(IDirect3DViewport3 *iface,
518                                     D3DMATERIALHANDLE hMat)
519 {
520     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
521     TRACE("(%p)->(%d)\n", This, hMat);
522
523     EnterCriticalSection(&ddraw_cs);
524     if(hMat && hMat > This->ddraw->d3ddevice->numHandles)
525     {
526         WARN("Specified Handle %d out of range\n", hMat);
527         LeaveCriticalSection(&ddraw_cs);
528         return DDERR_INVALIDPARAMS;
529     }
530     else if(hMat && This->ddraw->d3ddevice->Handles[hMat - 1].type != DDrawHandle_Material)
531     {
532         WARN("Handle %d is not a material handle\n", hMat);
533         LeaveCriticalSection(&ddraw_cs);
534         return DDERR_INVALIDPARAMS;
535     }
536
537     if(hMat)
538     {
539         This->background = This->ddraw->d3ddevice->Handles[hMat - 1].ptr;
540         TRACE(" setting background color : %f %f %f %f\n",
541               This->background->mat.u.diffuse.u1.r,
542               This->background->mat.u.diffuse.u2.g,
543               This->background->mat.u.diffuse.u3.b,
544               This->background->mat.u.diffuse.u4.a);
545     }
546     else
547     {
548         This->background = NULL;
549         TRACE("Setting background to NULL\n");
550     }
551
552     LeaveCriticalSection(&ddraw_cs);
553     return D3D_OK;
554 }
555
556 /*****************************************************************************
557  * IDirect3DViewport3::GetBackground
558  *
559  * Returns the material handle assigned to the background of the viewport
560  *
561  * Params:
562  *  lphMat: Address to store the handle
563  *  lpValid: is set to FALSE if no background is set, TRUE if one is set
564  *
565  * Returns:
566  *  D3D_OK
567  *
568  *****************************************************************************/
569 static HRESULT WINAPI
570 IDirect3DViewportImpl_GetBackground(IDirect3DViewport3 *iface,
571                                     D3DMATERIALHANDLE *lphMat,
572                                     BOOL *lpValid)
573 {
574     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
575     TRACE("(%p)->(%p,%p)\n", This, lphMat, lpValid);
576
577     EnterCriticalSection(&ddraw_cs);
578     if(lpValid)
579     {
580         *lpValid = This->background != NULL;
581     }
582     if(lphMat)
583     {
584         if(This->background)
585         {
586             *lphMat = This->background->Handle;
587         }
588         else
589         {
590             *lphMat = 0;
591         }
592     }
593     LeaveCriticalSection(&ddraw_cs);
594
595     return D3D_OK;
596 }
597
598 /*****************************************************************************
599  * IDirect3DViewport3::SetBackgroundDepth
600  *
601  * Sets a surface that represents the background depth. It's contents are
602  * used to set the depth buffer in IDirect3DViewport3::Clear
603  *
604  * Params:
605  *  lpDDSurface: Surface to set
606  *
607  * Returns: D3D_OK, because it's a stub
608  *
609  *****************************************************************************/
610 static HRESULT WINAPI
611 IDirect3DViewportImpl_SetBackgroundDepth(IDirect3DViewport3 *iface,
612                                          IDirectDrawSurface *lpDDSurface)
613 {
614     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
615     FIXME("(%p)->(%p): stub!\n", This, lpDDSurface);
616     return D3D_OK;
617 }
618
619 /*****************************************************************************
620  * IDirect3DViewport3::GetBackgroundDepth
621  *
622  * Returns the surface that represents the depth field
623  *
624  * Params:
625  *  lplpDDSurface: Address to store the interface pointer
626  *  lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
627  *
628  * Returns:
629  *  D3D_OK, because it's a stub
630  *  (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
631  *
632  *****************************************************************************/
633 static HRESULT WINAPI
634 IDirect3DViewportImpl_GetBackgroundDepth(IDirect3DViewport3 *iface,
635                                          IDirectDrawSurface **lplpDDSurface,
636                                          LPBOOL lpValid)
637 {
638     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
639     FIXME("(%p)->(%p,%p): stub!\n", This, lplpDDSurface, lpValid);
640     return DD_OK;
641 }
642
643 /*****************************************************************************
644  * IDirect3DViewport3::Clear
645  *
646  * Clears the render target and / or the z buffer
647  *
648  * Params:
649  *  dwCount: The amount of rectangles to clear. If 0, the whole buffer is
650  *           cleared
651  *  lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
652  *  dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
653  *
654  * Returns:
655  *  D3D_OK on success
656  *  D3DERR_VIEWPORTHASNODEVICE if there's no active device
657  *  The return value of IDirect3DDevice7::Clear
658  *
659  *****************************************************************************/
660 static HRESULT WINAPI IDirect3DViewportImpl_Clear(IDirect3DViewport3 *iface,
661         DWORD dwCount, D3DRECT *lpRects, DWORD dwFlags)
662 {
663     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
664     DWORD color = 0x00000000;
665     HRESULT hr;
666     LPDIRECT3DVIEWPORT3 current_viewport;
667     IDirect3DDevice3 *d3d_device3;
668
669     TRACE("(%p/%p)->(%08x,%p,%08x)\n", This, iface, dwCount, lpRects, dwFlags);
670     if (This->active_device == NULL) {
671         ERR(" Trying to clear a viewport not attached to a device !\n");
672         return D3DERR_VIEWPORTHASNODEVICE;
673     }
674     d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
675
676     EnterCriticalSection(&ddraw_cs);
677     if (dwFlags & D3DCLEAR_TARGET) {
678         if (This->background == NULL) {
679             ERR(" Trying to clear the color buffer without background material !\n");
680         }
681         else
682         {
683             color = ((int)((This->background->mat.u.diffuse.u1.r) * 255) << 16)
684                     | ((int) ((This->background->mat.u.diffuse.u2.g) * 255) <<  8)
685                     | ((int) ((This->background->mat.u.diffuse.u3.b) * 255) <<  0)
686                     | ((int) ((This->background->mat.u.diffuse.u4.a) * 255) << 24);
687         }
688     }
689
690     /* Need to temporarily activate viewport to clear it. Previously active one will be restored
691         afterwards. */
692     This->activate(This, TRUE);
693
694     hr = IDirect3DDevice7_Clear((IDirect3DDevice7 *)This->active_device, dwCount, lpRects,
695             dwFlags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
696
697     IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
698     if(current_viewport) {
699         IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)current_viewport;
700         vp->activate(vp, TRUE);
701         IDirect3DViewport3_Release(current_viewport);
702     }
703
704     LeaveCriticalSection(&ddraw_cs);
705     return hr;
706 }
707
708 /*****************************************************************************
709  * IDirect3DViewport3::AddLight
710  *
711  * Adds an light to the viewport
712  *
713  * Params:
714  *  lpDirect3DLight: Interface of the light to add
715  *
716  * Returns:
717  *  D3D_OK on success
718  *  DDERR_INVALIDPARAMS if Direct3DLight is NULL
719  *  DDERR_INVALIDPARAMS if there are 8 lights or more
720  *
721  *****************************************************************************/
722 static HRESULT WINAPI
723 IDirect3DViewportImpl_AddLight(IDirect3DViewport3 *iface,
724                                IDirect3DLight *lpDirect3DLight)
725 {
726     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
727     IDirect3DLightImpl *lpDirect3DLightImpl = (IDirect3DLightImpl *)lpDirect3DLight;
728     DWORD i = 0;
729     DWORD map = This->map_lights;
730
731     TRACE("(%p)->(%p)\n", This, lpDirect3DLight);
732
733     EnterCriticalSection(&ddraw_cs);
734     if (This->num_lights >= 8)
735     {
736         LeaveCriticalSection(&ddraw_cs);
737         return DDERR_INVALIDPARAMS;
738     }
739
740     /* Find a light number and update both light and viewports objects accordingly */
741     while(map&1) {
742         map>>=1;
743         i++;
744     }
745     lpDirect3DLightImpl->dwLightIndex = i;
746     This->num_lights++;
747     This->map_lights |= 1<<i;
748
749     /* Add the light in the 'linked' chain */
750     lpDirect3DLightImpl->next = This->lights;
751     This->lights = lpDirect3DLightImpl;
752     IDirect3DLight_AddRef(lpDirect3DLight);
753
754     /* Attach the light to the viewport */
755     lpDirect3DLightImpl->active_viewport = This;
756
757     /* If active, activate the light */
758     if (This->active_device != NULL) {
759         lpDirect3DLightImpl->activate(lpDirect3DLightImpl);
760     }
761
762     LeaveCriticalSection(&ddraw_cs);
763     return D3D_OK;
764 }
765
766 /*****************************************************************************
767  * IDirect3DViewport3::DeleteLight
768  *
769  * Deletes a light from the viewports' light list
770  *
771  * Params:
772  *  lpDirect3DLight: Light to delete
773  *
774  * Returns:
775  *  D3D_OK on success
776  *  DDERR_INVALIDPARAMS if the light wasn't found
777  *
778  *****************************************************************************/
779 static HRESULT WINAPI
780 IDirect3DViewportImpl_DeleteLight(IDirect3DViewport3 *iface,
781                                   IDirect3DLight *lpDirect3DLight)
782 {
783     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
784     IDirect3DLightImpl *lpDirect3DLightImpl = (IDirect3DLightImpl *)lpDirect3DLight;
785     IDirect3DLightImpl *cur_light, *prev_light = NULL;
786
787     TRACE("(%p)->(%p)\n", This, lpDirect3DLight);
788
789     EnterCriticalSection(&ddraw_cs);
790     cur_light = This->lights;
791     while (cur_light != NULL) {
792         if (cur_light == lpDirect3DLightImpl) {
793             lpDirect3DLightImpl->desactivate(lpDirect3DLightImpl);
794             if (prev_light == NULL) This->lights = cur_light->next;
795             else prev_light->next = cur_light->next;
796             /* Detach the light to the viewport */
797             cur_light->active_viewport = NULL;
798             IDirect3DLight_Release( (IDirect3DLight *)cur_light );
799             This->num_lights--;
800             This->map_lights &= ~(1<<lpDirect3DLightImpl->dwLightIndex);
801             LeaveCriticalSection(&ddraw_cs);
802             return D3D_OK;
803         }
804         prev_light = cur_light;
805         cur_light = cur_light->next;
806     }
807     LeaveCriticalSection(&ddraw_cs);
808
809     return DDERR_INVALIDPARAMS;
810 }
811
812 /*****************************************************************************
813  * IDirect3DViewport::NextLight
814  *
815  * Enumerates the lights associated with the viewport
816  *
817  * Params:
818  *  lpDirect3DLight: Light to start with
819  *  lplpDirect3DLight: Address to store the successor to
820  *
821  * Returns:
822  *  D3D_OK, because it's a stub
823  *
824  *****************************************************************************/
825 static HRESULT WINAPI
826 IDirect3DViewportImpl_NextLight(IDirect3DViewport3 *iface,
827                                 IDirect3DLight *lpDirect3DLight,
828                                 IDirect3DLight **lplpDirect3DLight,
829                                 DWORD dwFlags)
830 {
831     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
832     IDirect3DLightImpl *cur_light, *prev_light = NULL;
833
834     TRACE("(%p)->(%p,%p,%08x)\n", This, lpDirect3DLight, lplpDirect3DLight, dwFlags);
835
836     if (!lplpDirect3DLight)
837         return DDERR_INVALIDPARAMS;
838
839     *lplpDirect3DLight = NULL;
840
841     EnterCriticalSection(&ddraw_cs);
842
843     cur_light = This->lights;
844
845     switch (dwFlags) {
846         case D3DNEXT_NEXT:
847             if (!lpDirect3DLight) {
848                 LeaveCriticalSection(&ddraw_cs);
849                 return DDERR_INVALIDPARAMS;
850             }
851             while (cur_light != NULL) {
852                 if (cur_light == (IDirect3DLightImpl *)lpDirect3DLight) {
853                     *lplpDirect3DLight = (IDirect3DLight*)cur_light->next;
854                     break;
855                 }
856                 cur_light = cur_light->next;
857             }
858             break;
859         case D3DNEXT_HEAD:
860             *lplpDirect3DLight = (IDirect3DLight*)This->lights;
861             break;
862         case D3DNEXT_TAIL:
863             while (cur_light != NULL) {
864                 prev_light = cur_light;
865                 cur_light = cur_light->next;
866             }
867             *lplpDirect3DLight = (IDirect3DLight*)prev_light;
868             break;
869         default:
870             ERR("Unknown flag %d\n", dwFlags);
871             break;
872     }
873
874     if (*lplpDirect3DLight)
875         IDirect3DLight_AddRef(*lplpDirect3DLight);
876
877     LeaveCriticalSection(&ddraw_cs);
878
879     return *lplpDirect3DLight ? D3D_OK : DDERR_INVALIDPARAMS;
880 }
881
882 /*****************************************************************************
883  * IDirect3DViewport2 Methods.
884  *****************************************************************************/
885
886 /*****************************************************************************
887  * IDirect3DViewport3::GetViewport2
888  *
889  * Returns the currently set viewport in a D3DVIEWPORT2 structure.
890  * Similar to IDirect3DViewport3::GetViewport
891  *
892  * Params:
893  *  lpData: Pointer to the structure to fill
894  *
895  * Returns:
896  *  D3D_OK on success
897  *  DDERR_INVALIDPARAMS if the viewport was set with
898  *                      IDirect3DViewport3::SetViewport
899  *  DDERR_INVALIDPARAMS if Data is NULL
900  *
901  *****************************************************************************/
902 static HRESULT WINAPI
903 IDirect3DViewportImpl_GetViewport2(IDirect3DViewport3 *iface,
904                                    D3DVIEWPORT2 *lpData)
905 {
906     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
907     DWORD dwSize;
908     TRACE("(%p)->(%p)\n", This, lpData);
909
910     EnterCriticalSection(&ddraw_cs);
911     if (This->use_vp2 != 1) {
912         ERR("  Requesting to get a D3DVIEWPORT2 struct where a D3DVIEWPORT was set !\n");
913         LeaveCriticalSection(&ddraw_cs);
914         return DDERR_INVALIDPARAMS;
915     }
916     dwSize = lpData->dwSize;
917     memset(lpData, 0, dwSize);
918     memcpy(lpData, &(This->viewports.vp2), dwSize);
919
920     if (TRACE_ON(d3d7)) {
921         TRACE("  returning D3DVIEWPORT2 :\n");
922         _dump_D3DVIEWPORT2(lpData);
923     }
924
925     LeaveCriticalSection(&ddraw_cs);
926     return D3D_OK;
927 }
928
929 /*****************************************************************************
930  * IDirect3DViewport3::SetViewport2
931  *
932  * Sets the viewport from a D3DVIEWPORT2 structure
933  *
934  * Params:
935  *  lpData: Viewport to set
936  *
937  * Returns:
938  *  D3D_OK on success
939  *
940  *****************************************************************************/
941 static HRESULT WINAPI
942 IDirect3DViewportImpl_SetViewport2(IDirect3DViewport3 *iface,
943                                    D3DVIEWPORT2 *lpData)
944 {
945     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
946     LPDIRECT3DVIEWPORT3 current_viewport;
947     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
948
949     if (TRACE_ON(d3d7)) {
950         TRACE("  getting D3DVIEWPORT2 :\n");
951         _dump_D3DVIEWPORT2(lpData);
952     }
953
954     EnterCriticalSection(&ddraw_cs);
955     This->use_vp2 = 1;
956     memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
957     memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
958
959     if (This->active_device) {
960         IDirect3DDevice3 *d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
961         IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
962         if (current_viewport) {
963             if ((IDirect3DViewportImpl *)current_viewport == This) This->activate(This, FALSE);
964             IDirect3DViewport3_Release(current_viewport);
965         }
966     }
967     LeaveCriticalSection(&ddraw_cs);
968
969     return D3D_OK;
970 }
971
972 /*****************************************************************************
973  * IDirect3DViewport3 Methods.
974  *****************************************************************************/
975
976 /*****************************************************************************
977  * IDirect3DViewport3::SetBackgroundDepth2
978  *
979  * Sets a IDirectDrawSurface4 surface as the background depth surface
980  *
981  * Params:
982  *  lpDDS: Surface to set
983  *
984  * Returns:
985  *  D3D_OK, because it's stub
986  *
987  *****************************************************************************/
988 static HRESULT WINAPI
989 IDirect3DViewportImpl_SetBackgroundDepth2(IDirect3DViewport3 *iface,
990                                           IDirectDrawSurface4 *lpDDS)
991 {
992     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
993     FIXME("(%p)->(%p): stub!\n", This, lpDDS);
994     return D3D_OK;
995 }
996
997 /*****************************************************************************
998  * IDirect3DViewport3::GetBackgroundDepth2
999  *
1000  * Returns the IDirect3DSurface4 interface to the background depth surface
1001  *
1002  * Params:
1003  *  lplpDDS: Address to store the interface pointer at
1004  *  lpValid: Set to true if a surface is assigned
1005  *
1006  * Returns:
1007  *  D3D_OK because it's a stub
1008  *
1009  *****************************************************************************/
1010 static HRESULT WINAPI
1011 IDirect3DViewportImpl_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1012                                           IDirectDrawSurface4 **lplpDDS,
1013                                           BOOL *lpValid)
1014 {
1015     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
1016     FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDS, lpValid);
1017     return D3D_OK;
1018 }
1019
1020 /*****************************************************************************
1021  * IDirect3DViewport3::Clear2
1022  *
1023  * Another clearing method
1024  *
1025  * Params:
1026  *  Count: Number of rectangles to clear
1027  *  Rects: Rectangle array to clear
1028  *  Flags: Some flags :)
1029  *  Color: Color to fill the render target with
1030  *  Z: Value to fill the depth buffer with
1031  *  Stencil: Value to fill the stencil bits with
1032  *
1033  * Returns:
1034  *
1035  *****************************************************************************/
1036 static HRESULT WINAPI
1037 IDirect3DViewportImpl_Clear2(IDirect3DViewport3 *iface,
1038                              DWORD dwCount,
1039                              LPD3DRECT lpRects,
1040                              DWORD dwFlags,
1041                              DWORD dwColor,
1042                              D3DVALUE dvZ,
1043                              DWORD dwStencil)
1044 {
1045     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
1046     HRESULT hr;
1047     LPDIRECT3DVIEWPORT3 current_viewport;
1048     IDirect3DDevice3 *d3d_device3;
1049     TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x)\n", This, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
1050
1051     EnterCriticalSection(&ddraw_cs);
1052     if (This->active_device == NULL) {
1053         ERR(" Trying to clear a viewport not attached to a device !\n");
1054         LeaveCriticalSection(&ddraw_cs);
1055         return D3DERR_VIEWPORTHASNODEVICE;
1056     }
1057     d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
1058     /* Need to temporarily activate viewport to clear it. Previously active one will be restored
1059         afterwards. */
1060     This->activate(This, TRUE);
1061
1062     hr = IDirect3DDevice7_Clear((IDirect3DDevice7 *)This->active_device,
1063             dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
1064     IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
1065     if(current_viewport) {
1066         IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)current_viewport;
1067         vp->activate(vp, TRUE);
1068         IDirect3DViewport3_Release(current_viewport);
1069     }
1070     LeaveCriticalSection(&ddraw_cs);
1071     return hr;
1072 }
1073
1074 /*****************************************************************************
1075  * The VTable
1076  *****************************************************************************/
1077
1078 const IDirect3DViewport3Vtbl IDirect3DViewport3_Vtbl =
1079 {
1080     /*** IUnknown Methods ***/
1081     IDirect3DViewportImpl_QueryInterface,
1082     IDirect3DViewportImpl_AddRef,
1083     IDirect3DViewportImpl_Release,
1084     /*** IDirect3DViewport Methods */
1085     IDirect3DViewportImpl_Initialize,
1086     IDirect3DViewportImpl_GetViewport,
1087     IDirect3DViewportImpl_SetViewport,
1088     IDirect3DViewportImpl_TransformVertices,
1089     IDirect3DViewportImpl_LightElements,
1090     IDirect3DViewportImpl_SetBackground,
1091     IDirect3DViewportImpl_GetBackground,
1092     IDirect3DViewportImpl_SetBackgroundDepth,
1093     IDirect3DViewportImpl_GetBackgroundDepth,
1094     IDirect3DViewportImpl_Clear,
1095     IDirect3DViewportImpl_AddLight,
1096     IDirect3DViewportImpl_DeleteLight,
1097     IDirect3DViewportImpl_NextLight,
1098     /*** IDirect3DViewport2 Methods ***/
1099     IDirect3DViewportImpl_GetViewport2,
1100     IDirect3DViewportImpl_SetViewport2,
1101     /*** IDirect3DViewport3 Methods ***/
1102     IDirect3DViewportImpl_SetBackgroundDepth2,
1103     IDirect3DViewportImpl_GetBackgroundDepth2,
1104     IDirect3DViewportImpl_Clear2,
1105 };