Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ddraw / viewport.c
blob6e0725e604e7bd2c3c37b6a23e0a3c749b92b654
1 /* Direct3D Viewport
2 * Copyright (c) 1998 Lionel ULMER
3 * Copyright (c) 2006-2007 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DViewport2.
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
29 /*****************************************************************************
30 * Helper functions
31 *****************************************************************************/
33 /*****************************************************************************
34 * viewport_activate
36 * activates the viewport using IDirect3DDevice7::SetViewport
38 *****************************************************************************/
39 void viewport_activate(IDirect3DViewportImpl* This, BOOL ignore_lights) {
40 IDirect3DLightImpl* light;
41 D3DVIEWPORT7 vp;
43 if (!ignore_lights) {
44 /* Activate all the lights associated with this context */
45 light = This->lights;
47 while (light)
49 light_activate(light);
50 light = light->next;
54 /* And copy the values in the structure used by the device */
55 if (This->use_vp2)
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;
64 else
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;
74 /* And also set the viewport */
75 IDirect3DDevice7_SetViewport((IDirect3DDevice7 *)This->active_device, &vp);
78 /*****************************************************************************
79 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
81 * Writes viewport information to TRACE
83 *****************************************************************************/
84 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
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);
98 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
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);
112 /*****************************************************************************
113 * IUnknown Methods.
114 *****************************************************************************/
116 /*****************************************************************************
117 * IDirect3DViewport3::QueryInterface
119 * A normal QueryInterface. Can query all interface versions and the
120 * IUnknown interface. The VTables of the different versions
121 * are equal
123 * Params:
124 * refiid: Interface id queried for
125 * obj: Address to write the interface pointer to
127 * Returns:
128 * S_OK on success.
129 * E_NOINTERFACE if the requested interface wasn't found
131 *****************************************************************************/
132 static HRESULT WINAPI IDirect3DViewportImpl_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
134 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
136 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
137 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
138 || IsEqualGUID(&IID_IDirect3DViewport, riid)
139 || IsEqualGUID(&IID_IUnknown, riid))
141 IDirect3DViewport3_AddRef(iface);
142 *object = iface;
143 return S_OK;
146 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
148 *object = NULL;
149 return E_NOINTERFACE;
152 /*****************************************************************************
153 * IDirect3DViewport3::AddRef
155 * Increases the refcount.
157 * Returns:
158 * The new refcount
160 *****************************************************************************/
161 static ULONG WINAPI
162 IDirect3DViewportImpl_AddRef(IDirect3DViewport3 *iface)
164 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
165 ULONG ref = InterlockedIncrement(&This->ref);
167 TRACE("%p increasing refcount to %u.\n", This, ref);
169 return ref;
172 /*****************************************************************************
173 * IDirect3DViewport3::Release
175 * Reduces the refcount. If it falls to 0, the interface is released
177 * Returns:
178 * The new refcount
180 *****************************************************************************/
181 static ULONG WINAPI
182 IDirect3DViewportImpl_Release(IDirect3DViewport3 *iface)
184 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
185 ULONG ref = InterlockedDecrement(&This->ref);
187 TRACE("%p decreasing refcount to %u.\n", This, ref);
189 if (!ref) {
190 HeapFree(GetProcessHeap(), 0, This);
191 return 0;
193 return ref;
196 /*****************************************************************************
197 * IDirect3DViewport Methods.
198 *****************************************************************************/
200 /*****************************************************************************
201 * IDirect3DViewport3::Initialize
203 * No-op initialization.
205 * Params:
206 * Direct3D: The direct3D device this viewport is assigned to
208 * Returns:
209 * DDERR_ALREADYINITIALIZED
211 *****************************************************************************/
212 static HRESULT WINAPI
213 IDirect3DViewportImpl_Initialize(IDirect3DViewport3 *iface,
214 IDirect3D *Direct3D)
216 TRACE("iface %p, d3d %p.\n", iface, Direct3D);
218 return DDERR_ALREADYINITIALIZED;
221 /*****************************************************************************
222 * IDirect3DViewport3::GetViewport
224 * Returns the viewport data assigned to this viewport interface
226 * Params:
227 * Data: Address to store the data
229 * Returns:
230 * D3D_OK on success
231 * DDERR_INVALIDPARAMS if Data is NULL
233 *****************************************************************************/
234 static HRESULT WINAPI
235 IDirect3DViewportImpl_GetViewport(IDirect3DViewport3 *iface,
236 D3DVIEWPORT *lpData)
238 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
239 DWORD dwSize;
241 TRACE("iface %p, data %p.\n", iface, lpData);
243 EnterCriticalSection(&ddraw_cs);
244 dwSize = lpData->dwSize;
245 memset(lpData, 0, dwSize);
246 if (!This->use_vp2)
247 memcpy(lpData, &(This->viewports.vp1), dwSize);
248 else {
249 D3DVIEWPORT vp1;
250 vp1.dwSize = sizeof(vp1);
251 vp1.dwX = This->viewports.vp2.dwX;
252 vp1.dwY = This->viewports.vp2.dwY;
253 vp1.dwWidth = This->viewports.vp2.dwWidth;
254 vp1.dwHeight = This->viewports.vp2.dwHeight;
255 vp1.dvMaxX = 0.0;
256 vp1.dvMaxY = 0.0;
257 vp1.dvScaleX = 0.0;
258 vp1.dvScaleY = 0.0;
259 vp1.dvMinZ = This->viewports.vp2.dvMinZ;
260 vp1.dvMaxZ = This->viewports.vp2.dvMaxZ;
261 memcpy(lpData, &vp1, dwSize);
264 if (TRACE_ON(ddraw))
266 TRACE(" returning D3DVIEWPORT :\n");
267 _dump_D3DVIEWPORT(lpData);
269 LeaveCriticalSection(&ddraw_cs);
271 return DD_OK;
274 /*****************************************************************************
275 * IDirect3DViewport3::SetViewport
277 * Sets the viewport information for this interface
279 * Params:
280 * lpData: Viewport to set
282 * Returns:
283 * D3D_OK on success
284 * DDERR_INVALIDPARAMS if Data is NULL
286 *****************************************************************************/
287 static HRESULT WINAPI
288 IDirect3DViewportImpl_SetViewport(IDirect3DViewport3 *iface,
289 D3DVIEWPORT *lpData)
291 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
292 LPDIRECT3DVIEWPORT3 current_viewport;
294 TRACE("iface %p, data %p.\n", iface, lpData);
296 if (TRACE_ON(ddraw))
298 TRACE(" getting D3DVIEWPORT :\n");
299 _dump_D3DVIEWPORT(lpData);
302 EnterCriticalSection(&ddraw_cs);
303 This->use_vp2 = 0;
304 memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
305 memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
307 /* Tests on two games show that these values are never used properly so override
308 them with proper ones :-)
310 This->viewports.vp1.dvMinZ = 0.0;
311 This->viewports.vp1.dvMaxZ = 1.0;
313 if (This->active_device) {
314 IDirect3DDevice3 *d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
315 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
316 if (current_viewport)
318 if ((IDirect3DViewportImpl *)current_viewport == This) viewport_activate(This, FALSE);
319 IDirect3DViewport3_Release(current_viewport);
322 LeaveCriticalSection(&ddraw_cs);
324 return DD_OK;
327 /*****************************************************************************
328 * IDirect3DViewport3::TransformVertices
330 * Transforms vertices by the transformation matrix.
332 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
333 * so it's tempting to forward it to there. However, there are some
334 * tiny differences. First, the lpOffscreen flag that is reported back,
335 * then there is the homogeneous vertex that is generated. Also there's a lack
336 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
337 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
338 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
339 * reused.
341 * Params:
342 * dwVertexCount: The number of vertices to be transformed
343 * lpData: Pointer to the vertex data
344 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
345 * lpOffScreen: Set to the clipping plane clipping the vertex, if only one
346 * vertex is transformed and clipping is on. 0 otherwise
348 * Returns:
349 * D3D_OK on success
350 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
351 * DDERR_INVALIDPARAMS if no clipping flag is specified
353 *****************************************************************************/
354 static HRESULT WINAPI
355 IDirect3DViewportImpl_TransformVertices(IDirect3DViewport3 *iface,
356 DWORD dwVertexCount,
357 D3DTRANSFORMDATA *lpData,
358 DWORD dwFlags,
359 DWORD *lpOffScreen)
361 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
362 D3DMATRIX view_mat, world_mat, proj_mat, mat;
363 float *in;
364 float *out;
365 float x, y, z, w;
366 unsigned int i;
367 D3DVIEWPORT vp = This->viewports.vp1;
368 D3DHVERTEX *outH;
370 TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
371 iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
373 /* Tests on windows show that Windows crashes when this occurs,
374 * so don't return the (intuitive) return value
375 if(!This->active_device)
377 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
378 return D3DERR_VIEWPORTHASNODEVICE;
382 if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
384 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
385 return DDERR_INVALIDPARAMS;
389 EnterCriticalSection(&ddraw_cs);
390 IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
391 D3DTRANSFORMSTATE_VIEW,
392 (WINED3DMATRIX*) &view_mat);
394 IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
395 D3DTRANSFORMSTATE_PROJECTION,
396 (WINED3DMATRIX*) &proj_mat);
398 IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
399 WINED3DTS_WORLDMATRIX(0),
400 (WINED3DMATRIX*) &world_mat);
401 multiply_matrix(&mat,&view_mat,&world_mat);
402 multiply_matrix(&mat,&proj_mat,&mat);
404 in = lpData->lpIn;
405 out = lpData->lpOut;
406 outH = lpData->lpHOut;
407 for(i = 0; i < dwVertexCount; i++)
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);
414 if(dwFlags & D3DTRANSFORM_CLIPPED)
416 /* If clipping is enabled, Windows assumes that outH is
417 * a valid pointer
419 outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
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;
435 if(outH[i].dwFlags)
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.
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;
452 w = 1 / w;
453 x *= w; y *= w; z *= w;
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);
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.
468 * If clipping is requested, Windows assumes that the offscreen
469 * param is a valid pointer.
471 if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
473 *lpOffScreen = outH[0].dwFlags;
475 else if(*lpOffScreen)
477 *lpOffScreen = 0;
479 LeaveCriticalSection(&ddraw_cs);
481 TRACE("All done\n");
482 return DD_OK;
485 /*****************************************************************************
486 * IDirect3DViewport3::LightElements
488 * The DirectX 5.0 sdk says that it's not implemented
490 * Params:
493 * Returns:
494 * DDERR_UNSUPPORTED
496 *****************************************************************************/
497 static HRESULT WINAPI
498 IDirect3DViewportImpl_LightElements(IDirect3DViewport3 *iface,
499 DWORD dwElementCount,
500 LPD3DLIGHTDATA lpData)
502 TRACE("iface %p, element_count %u, data %p.\n", iface, dwElementCount, lpData);
504 return DDERR_UNSUPPORTED;
507 /*****************************************************************************
508 * IDirect3DViewport3::SetBackground
510 * Sets tje background material
512 * Params:
513 * hMat: Handle from a IDirect3DMaterial interface
515 * Returns:
516 * D3D_OK on success
518 *****************************************************************************/
519 static HRESULT WINAPI
520 IDirect3DViewportImpl_SetBackground(IDirect3DViewport3 *iface,
521 D3DMATERIALHANDLE hMat)
523 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
524 IDirect3DMaterialImpl *m;
526 TRACE("iface %p, material %#x.\n", iface, hMat);
528 EnterCriticalSection(&ddraw_cs);
530 if (!hMat)
532 This->background = NULL;
533 TRACE("Setting background to NULL\n");
534 LeaveCriticalSection(&ddraw_cs);
535 return D3D_OK;
538 m = ddraw_get_object(&This->ddraw->d3ddevice->handle_table, hMat - 1, DDRAW_HANDLE_MATERIAL);
539 if (!m)
541 WARN("Invalid material handle.\n");
542 LeaveCriticalSection(&ddraw_cs);
543 return DDERR_INVALIDPARAMS;
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;
551 LeaveCriticalSection(&ddraw_cs);
552 return D3D_OK;
555 /*****************************************************************************
556 * IDirect3DViewport3::GetBackground
558 * Returns the material handle assigned to the background of the viewport
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
564 * Returns:
565 * D3D_OK
567 *****************************************************************************/
568 static HRESULT WINAPI
569 IDirect3DViewportImpl_GetBackground(IDirect3DViewport3 *iface,
570 D3DMATERIALHANDLE *lphMat,
571 BOOL *lpValid)
573 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
575 TRACE("iface %p, material %p, valid %p.\n", iface, lphMat, lpValid);
577 EnterCriticalSection(&ddraw_cs);
578 if(lpValid)
580 *lpValid = This->background != NULL;
582 if(lphMat)
584 if(This->background)
586 *lphMat = This->background->Handle;
588 else
590 *lphMat = 0;
593 LeaveCriticalSection(&ddraw_cs);
595 return D3D_OK;
598 /*****************************************************************************
599 * IDirect3DViewport3::SetBackgroundDepth
601 * Sets a surface that represents the background depth. It's contents are
602 * used to set the depth buffer in IDirect3DViewport3::Clear
604 * Params:
605 * lpDDSurface: Surface to set
607 * Returns: D3D_OK, because it's a stub
609 *****************************************************************************/
610 static HRESULT WINAPI
611 IDirect3DViewportImpl_SetBackgroundDepth(IDirect3DViewport3 *iface,
612 IDirectDrawSurface *lpDDSurface)
614 FIXME("iface %p, surface %p stub!\n", iface, lpDDSurface);
616 return D3D_OK;
619 /*****************************************************************************
620 * IDirect3DViewport3::GetBackgroundDepth
622 * Returns the surface that represents the depth field
624 * Params:
625 * lplpDDSurface: Address to store the interface pointer
626 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
628 * Returns:
629 * D3D_OK, because it's a stub
630 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
632 *****************************************************************************/
633 static HRESULT WINAPI
634 IDirect3DViewportImpl_GetBackgroundDepth(IDirect3DViewport3 *iface,
635 IDirectDrawSurface **lplpDDSurface,
636 LPBOOL lpValid)
638 FIXME("iface %p, surface %p, valid %p stub!\n", iface, lplpDDSurface, lpValid);
640 return DD_OK;
643 /*****************************************************************************
644 * IDirect3DViewport3::Clear
646 * Clears the render target and / or the z buffer
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
654 * Returns:
655 * D3D_OK on success
656 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
657 * The return value of IDirect3DDevice7::Clear
659 *****************************************************************************/
660 static HRESULT WINAPI IDirect3DViewportImpl_Clear(IDirect3DViewport3 *iface,
661 DWORD dwCount, D3DRECT *lpRects, DWORD dwFlags)
663 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
664 DWORD color = 0x00000000;
665 HRESULT hr;
666 LPDIRECT3DVIEWPORT3 current_viewport;
667 IDirect3DDevice3 *d3d_device3;
669 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, dwCount, lpRects, dwFlags);
671 if (This->active_device == NULL) {
672 ERR(" Trying to clear a viewport not attached to a device !\n");
673 return D3DERR_VIEWPORTHASNODEVICE;
675 d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
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");
682 else
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);
691 /* Need to temporarily activate viewport to clear it. Previously active one will be restored
692 afterwards. */
693 viewport_activate(This, TRUE);
695 hr = IDirect3DDevice7_Clear((IDirect3DDevice7 *)This->active_device, dwCount, lpRects,
696 dwFlags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
698 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
699 if(current_viewport) {
700 IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)current_viewport;
701 viewport_activate(vp, TRUE);
702 IDirect3DViewport3_Release(current_viewport);
705 LeaveCriticalSection(&ddraw_cs);
706 return hr;
709 /*****************************************************************************
710 * IDirect3DViewport3::AddLight
712 * Adds an light to the viewport
714 * Params:
715 * lpDirect3DLight: Interface of the light to add
717 * Returns:
718 * D3D_OK on success
719 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
720 * DDERR_INVALIDPARAMS if there are 8 lights or more
722 *****************************************************************************/
723 static HRESULT WINAPI
724 IDirect3DViewportImpl_AddLight(IDirect3DViewport3 *iface,
725 IDirect3DLight *lpDirect3DLight)
727 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
728 IDirect3DLightImpl *lpDirect3DLightImpl = (IDirect3DLightImpl *)lpDirect3DLight;
729 DWORD i = 0;
730 DWORD map = This->map_lights;
732 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
734 EnterCriticalSection(&ddraw_cs);
735 if (This->num_lights >= 8)
737 LeaveCriticalSection(&ddraw_cs);
738 return DDERR_INVALIDPARAMS;
741 /* Find a light number and update both light and viewports objects accordingly */
742 while (map & 1)
744 map >>= 1;
745 ++i;
747 lpDirect3DLightImpl->dwLightIndex = i;
748 This->num_lights++;
749 This->map_lights |= 1<<i;
751 /* Add the light in the 'linked' chain */
752 lpDirect3DLightImpl->next = This->lights;
753 This->lights = lpDirect3DLightImpl;
754 IDirect3DLight_AddRef(lpDirect3DLight);
756 /* Attach the light to the viewport */
757 lpDirect3DLightImpl->active_viewport = This;
759 /* If active, activate the light */
760 if (This->active_device)
761 light_activate(lpDirect3DLightImpl);
763 LeaveCriticalSection(&ddraw_cs);
764 return D3D_OK;
767 /*****************************************************************************
768 * IDirect3DViewport3::DeleteLight
770 * Deletes a light from the viewports' light list
772 * Params:
773 * lpDirect3DLight: Light to delete
775 * Returns:
776 * D3D_OK on success
777 * DDERR_INVALIDPARAMS if the light wasn't found
779 *****************************************************************************/
780 static HRESULT WINAPI
781 IDirect3DViewportImpl_DeleteLight(IDirect3DViewport3 *iface,
782 IDirect3DLight *lpDirect3DLight)
784 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
785 IDirect3DLightImpl *lpDirect3DLightImpl = (IDirect3DLightImpl *)lpDirect3DLight;
786 IDirect3DLightImpl *cur_light, *prev_light = NULL;
788 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
790 EnterCriticalSection(&ddraw_cs);
791 cur_light = This->lights;
792 while (cur_light != NULL) {
793 if (cur_light == lpDirect3DLightImpl)
795 light_deactivate(lpDirect3DLightImpl);
796 if (!prev_light) This->lights = cur_light->next;
797 else prev_light->next = cur_light->next;
798 /* Detach the light from the viewport. */
799 cur_light->active_viewport = NULL;
800 IDirect3DLight_Release((IDirect3DLight *)cur_light);
801 --This->num_lights;
802 This->map_lights &= ~(1 << lpDirect3DLightImpl->dwLightIndex);
803 LeaveCriticalSection(&ddraw_cs);
804 return D3D_OK;
806 prev_light = cur_light;
807 cur_light = cur_light->next;
809 LeaveCriticalSection(&ddraw_cs);
811 return DDERR_INVALIDPARAMS;
814 /*****************************************************************************
815 * IDirect3DViewport::NextLight
817 * Enumerates the lights associated with the viewport
819 * Params:
820 * lpDirect3DLight: Light to start with
821 * lplpDirect3DLight: Address to store the successor to
823 * Returns:
824 * D3D_OK, because it's a stub
826 *****************************************************************************/
827 static HRESULT WINAPI
828 IDirect3DViewportImpl_NextLight(IDirect3DViewport3 *iface,
829 IDirect3DLight *lpDirect3DLight,
830 IDirect3DLight **lplpDirect3DLight,
831 DWORD dwFlags)
833 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
834 IDirect3DLightImpl *cur_light, *prev_light = NULL;
836 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
837 iface, lpDirect3DLight, lplpDirect3DLight, dwFlags);
839 if (!lplpDirect3DLight)
840 return DDERR_INVALIDPARAMS;
842 *lplpDirect3DLight = NULL;
844 EnterCriticalSection(&ddraw_cs);
846 cur_light = This->lights;
848 switch (dwFlags) {
849 case D3DNEXT_NEXT:
850 if (!lpDirect3DLight) {
851 LeaveCriticalSection(&ddraw_cs);
852 return DDERR_INVALIDPARAMS;
854 while (cur_light != NULL) {
855 if (cur_light == (IDirect3DLightImpl *)lpDirect3DLight) {
856 *lplpDirect3DLight = (IDirect3DLight*)cur_light->next;
857 break;
859 cur_light = cur_light->next;
861 break;
862 case D3DNEXT_HEAD:
863 *lplpDirect3DLight = (IDirect3DLight*)This->lights;
864 break;
865 case D3DNEXT_TAIL:
866 while (cur_light != NULL) {
867 prev_light = cur_light;
868 cur_light = cur_light->next;
870 *lplpDirect3DLight = (IDirect3DLight*)prev_light;
871 break;
872 default:
873 ERR("Unknown flag %d\n", dwFlags);
874 break;
877 if (*lplpDirect3DLight)
878 IDirect3DLight_AddRef(*lplpDirect3DLight);
880 LeaveCriticalSection(&ddraw_cs);
882 return *lplpDirect3DLight ? D3D_OK : DDERR_INVALIDPARAMS;
885 /*****************************************************************************
886 * IDirect3DViewport2 Methods.
887 *****************************************************************************/
889 /*****************************************************************************
890 * IDirect3DViewport3::GetViewport2
892 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
893 * Similar to IDirect3DViewport3::GetViewport
895 * Params:
896 * lpData: Pointer to the structure to fill
898 * Returns:
899 * D3D_OK on success
900 * DDERR_INVALIDPARAMS if the viewport was set with
901 * IDirect3DViewport3::SetViewport
902 * DDERR_INVALIDPARAMS if Data is NULL
904 *****************************************************************************/
905 static HRESULT WINAPI
906 IDirect3DViewportImpl_GetViewport2(IDirect3DViewport3 *iface,
907 D3DVIEWPORT2 *lpData)
909 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
910 DWORD dwSize;
912 TRACE("iface %p, data %p.\n", iface, lpData);
914 EnterCriticalSection(&ddraw_cs);
915 dwSize = lpData->dwSize;
916 memset(lpData, 0, dwSize);
917 if (This->use_vp2)
918 memcpy(lpData, &(This->viewports.vp2), dwSize);
919 else {
920 D3DVIEWPORT2 vp2;
921 vp2.dwSize = sizeof(vp2);
922 vp2.dwX = This->viewports.vp1.dwX;
923 vp2.dwY = This->viewports.vp1.dwY;
924 vp2.dwWidth = This->viewports.vp1.dwWidth;
925 vp2.dwHeight = This->viewports.vp1.dwHeight;
926 vp2.dvClipX = 0.0;
927 vp2.dvClipY = 0.0;
928 vp2.dvClipWidth = 0.0;
929 vp2.dvClipHeight = 0.0;
930 vp2.dvMinZ = This->viewports.vp1.dvMinZ;
931 vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
932 memcpy(lpData, &vp2, dwSize);
935 if (TRACE_ON(ddraw))
937 TRACE(" returning D3DVIEWPORT2 :\n");
938 _dump_D3DVIEWPORT2(lpData);
941 LeaveCriticalSection(&ddraw_cs);
942 return D3D_OK;
945 /*****************************************************************************
946 * IDirect3DViewport3::SetViewport2
948 * Sets the viewport from a D3DVIEWPORT2 structure
950 * Params:
951 * lpData: Viewport to set
953 * Returns:
954 * D3D_OK on success
956 *****************************************************************************/
957 static HRESULT WINAPI
958 IDirect3DViewportImpl_SetViewport2(IDirect3DViewport3 *iface,
959 D3DVIEWPORT2 *lpData)
961 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
962 LPDIRECT3DVIEWPORT3 current_viewport;
964 TRACE("iface %p, data %p.\n", iface, lpData);
966 if (TRACE_ON(ddraw))
968 TRACE(" getting D3DVIEWPORT2 :\n");
969 _dump_D3DVIEWPORT2(lpData);
972 EnterCriticalSection(&ddraw_cs);
973 This->use_vp2 = 1;
974 memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
975 memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
977 if (This->active_device) {
978 IDirect3DDevice3 *d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
979 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
980 if (current_viewport)
982 if ((IDirect3DViewportImpl *)current_viewport == This) viewport_activate(This, FALSE);
983 IDirect3DViewport3_Release(current_viewport);
986 LeaveCriticalSection(&ddraw_cs);
988 return D3D_OK;
991 /*****************************************************************************
992 * IDirect3DViewport3 Methods.
993 *****************************************************************************/
995 /*****************************************************************************
996 * IDirect3DViewport3::SetBackgroundDepth2
998 * Sets a IDirectDrawSurface4 surface as the background depth surface
1000 * Params:
1001 * lpDDS: Surface to set
1003 * Returns:
1004 * D3D_OK, because it's stub
1006 *****************************************************************************/
1007 static HRESULT WINAPI
1008 IDirect3DViewportImpl_SetBackgroundDepth2(IDirect3DViewport3 *iface,
1009 IDirectDrawSurface4 *lpDDS)
1011 FIXME("iface %p, surface %p stub!\n", iface, lpDDS);
1013 return D3D_OK;
1016 /*****************************************************************************
1017 * IDirect3DViewport3::GetBackgroundDepth2
1019 * Returns the IDirect3DSurface4 interface to the background depth surface
1021 * Params:
1022 * lplpDDS: Address to store the interface pointer at
1023 * lpValid: Set to true if a surface is assigned
1025 * Returns:
1026 * D3D_OK because it's a stub
1028 *****************************************************************************/
1029 static HRESULT WINAPI
1030 IDirect3DViewportImpl_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1031 IDirectDrawSurface4 **lplpDDS,
1032 BOOL *lpValid)
1034 FIXME("iface %p, surface %p, valid %p stub!\n", iface, lplpDDS, lpValid);
1036 return D3D_OK;
1039 /*****************************************************************************
1040 * IDirect3DViewport3::Clear2
1042 * Another clearing method
1044 * Params:
1045 * Count: Number of rectangles to clear
1046 * Rects: Rectangle array to clear
1047 * Flags: Some flags :)
1048 * Color: Color to fill the render target with
1049 * Z: Value to fill the depth buffer with
1050 * Stencil: Value to fill the stencil bits with
1052 * Returns:
1054 *****************************************************************************/
1055 static HRESULT WINAPI
1056 IDirect3DViewportImpl_Clear2(IDirect3DViewport3 *iface,
1057 DWORD dwCount,
1058 LPD3DRECT lpRects,
1059 DWORD dwFlags,
1060 DWORD dwColor,
1061 D3DVALUE dvZ,
1062 DWORD dwStencil)
1064 IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
1065 HRESULT hr;
1066 LPDIRECT3DVIEWPORT3 current_viewport;
1067 IDirect3DDevice3 *d3d_device3;
1069 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1070 iface, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
1072 EnterCriticalSection(&ddraw_cs);
1073 if (This->active_device == NULL) {
1074 ERR(" Trying to clear a viewport not attached to a device !\n");
1075 LeaveCriticalSection(&ddraw_cs);
1076 return D3DERR_VIEWPORTHASNODEVICE;
1078 d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
1079 /* Need to temporarily activate viewport to clear it. Previously active
1080 * one will be restored afterwards. */
1081 viewport_activate(This, TRUE);
1083 hr = IDirect3DDevice7_Clear((IDirect3DDevice7 *)This->active_device,
1084 dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
1085 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
1086 if(current_viewport) {
1087 IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)current_viewport;
1088 viewport_activate(vp, TRUE);
1089 IDirect3DViewport3_Release(current_viewport);
1091 LeaveCriticalSection(&ddraw_cs);
1092 return hr;
1095 /*****************************************************************************
1096 * The VTable
1097 *****************************************************************************/
1099 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1101 /*** IUnknown Methods ***/
1102 IDirect3DViewportImpl_QueryInterface,
1103 IDirect3DViewportImpl_AddRef,
1104 IDirect3DViewportImpl_Release,
1105 /*** IDirect3DViewport Methods */
1106 IDirect3DViewportImpl_Initialize,
1107 IDirect3DViewportImpl_GetViewport,
1108 IDirect3DViewportImpl_SetViewport,
1109 IDirect3DViewportImpl_TransformVertices,
1110 IDirect3DViewportImpl_LightElements,
1111 IDirect3DViewportImpl_SetBackground,
1112 IDirect3DViewportImpl_GetBackground,
1113 IDirect3DViewportImpl_SetBackgroundDepth,
1114 IDirect3DViewportImpl_GetBackgroundDepth,
1115 IDirect3DViewportImpl_Clear,
1116 IDirect3DViewportImpl_AddLight,
1117 IDirect3DViewportImpl_DeleteLight,
1118 IDirect3DViewportImpl_NextLight,
1119 /*** IDirect3DViewport2 Methods ***/
1120 IDirect3DViewportImpl_GetViewport2,
1121 IDirect3DViewportImpl_SetViewport2,
1122 /*** IDirect3DViewport3 Methods ***/
1123 IDirect3DViewportImpl_SetBackgroundDepth2,
1124 IDirect3DViewportImpl_GetBackgroundDepth2,
1125 IDirect3DViewportImpl_Clear2,
1128 void d3d_viewport_init(IDirect3DViewportImpl *viewport, IDirectDrawImpl *ddraw)
1130 viewport->lpVtbl = &d3d_viewport_vtbl;
1131 viewport->ref = 1;
1132 viewport->ddraw = ddraw;
1133 viewport->use_vp2 = 0xff;