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