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