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