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