wshom.ocx: Implement get_Item() for IWshEnvironment.
[wine.git] / dlls / ddraw / viewport.c
bloba1126ecd1e02de77813db426eab04d6b9d01ddb2
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 static void update_clip_space(struct d3d_device *device,
34 struct wined3d_vec3 *scale, struct wined3d_vec3 *offset)
36 D3DMATRIX clip_space =
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,
43 D3DMATRIX projection;
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;
51 /*****************************************************************************
52 * viewport_activate
54 * activates the viewport using IDirect3DDevice7::SetViewport
56 *****************************************************************************/
57 void viewport_activate(struct d3d_viewport *This, BOOL ignore_lights)
59 struct wined3d_vec3 scale, offset;
60 D3DVIEWPORT7 vp;
62 if (!ignore_lights)
64 struct d3d_light *light;
66 /* Activate all the lights associated with this context */
67 LIST_FOR_EACH_ENTRY(light, &This->light_list, struct d3d_light, entry)
69 light_activate(light);
73 /* And copy the values in the structure used by the device */
74 if (This->use_vp2)
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;
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);
90 else
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;
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;
107 update_clip_space(This->active_device, &scale, &offset);
108 IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
111 /*****************************************************************************
112 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
114 * Writes viewport information to TRACE
116 *****************************************************************************/
117 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
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);
131 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
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);
145 static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
147 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
150 /*****************************************************************************
151 * IUnknown Methods.
152 *****************************************************************************/
154 /*****************************************************************************
155 * IDirect3DViewport3::QueryInterface
157 * A normal QueryInterface. Can query all interface versions and the
158 * IUnknown interface. The VTables of the different versions
159 * are equal
161 * Params:
162 * refiid: Interface id queried for
163 * obj: Address to write the interface pointer to
165 * Returns:
166 * S_OK on success.
167 * E_NOINTERFACE if the requested interface wasn't found
169 *****************************************************************************/
170 static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
172 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
174 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
175 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
176 || IsEqualGUID(&IID_IDirect3DViewport, riid)
177 || IsEqualGUID(&IID_IUnknown, riid))
179 IDirect3DViewport3_AddRef(iface);
180 *object = iface;
181 return S_OK;
184 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
186 *object = NULL;
187 return E_NOINTERFACE;
190 /*****************************************************************************
191 * IDirect3DViewport3::AddRef
193 * Increases the refcount.
195 * Returns:
196 * The new refcount
198 *****************************************************************************/
199 static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
201 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
202 ULONG ref = InterlockedIncrement(&viewport->ref);
204 TRACE("%p increasing refcount to %u.\n", viewport, ref);
206 return ref;
209 /*****************************************************************************
210 * IDirect3DViewport3::Release
212 * Reduces the refcount. If it falls to 0, the interface is released
214 * Returns:
215 * The new refcount
217 *****************************************************************************/
218 static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
220 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
221 ULONG ref = InterlockedDecrement(&viewport->ref);
223 TRACE("%p decreasing refcount to %u.\n", viewport, ref);
225 if (!ref)
226 HeapFree(GetProcessHeap(), 0, viewport);
228 return ref;
231 /*****************************************************************************
232 * IDirect3DViewport Methods.
233 *****************************************************************************/
235 /*****************************************************************************
236 * IDirect3DViewport3::Initialize
238 * No-op initialization.
240 * Params:
241 * Direct3D: The direct3D device this viewport is assigned to
243 * Returns:
244 * DDERR_ALREADYINITIALIZED
246 *****************************************************************************/
247 static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
249 TRACE("iface %p, d3d %p.\n", iface, d3d);
251 return DDERR_ALREADYINITIALIZED;
254 /*****************************************************************************
255 * IDirect3DViewport3::GetViewport
257 * Returns the viewport data assigned to this viewport interface
259 * Params:
260 * Data: Address to store the data
262 * Returns:
263 * D3D_OK on success
264 * DDERR_INVALIDPARAMS if Data is NULL
266 *****************************************************************************/
267 static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
269 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
270 DWORD dwSize;
272 TRACE("iface %p, data %p.\n", iface, lpData);
274 wined3d_mutex_lock();
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);
296 if (TRACE_ON(ddraw))
298 TRACE(" returning D3DVIEWPORT :\n");
299 _dump_D3DVIEWPORT(lpData);
302 wined3d_mutex_unlock();
304 return DD_OK;
307 /*****************************************************************************
308 * IDirect3DViewport3::SetViewport
310 * Sets the viewport information for this interface
312 * Params:
313 * lpData: Viewport to set
315 * Returns:
316 * D3D_OK on success
317 * DDERR_INVALIDPARAMS if Data is NULL
319 *****************************************************************************/
320 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
322 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
323 IDirect3DViewport3 *current_viewport;
325 TRACE("iface %p, data %p.\n", iface, lpData);
327 if (TRACE_ON(ddraw))
329 TRACE(" getting D3DVIEWPORT :\n");
330 _dump_D3DVIEWPORT(lpData);
333 wined3d_mutex_lock();
335 This->use_vp2 = 0;
336 memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
337 memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
339 /* Tests on two games show that these values are never used properly so override
340 them with proper ones :-)
342 This->viewports.vp1.dvMinZ = 0.0;
343 This->viewports.vp1.dvMaxZ = 1.0;
345 if (This->active_device)
347 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
348 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
350 if (current_viewport == iface) viewport_activate(This, FALSE);
351 IDirect3DViewport3_Release(current_viewport);
355 wined3d_mutex_unlock();
357 return DD_OK;
360 /*****************************************************************************
361 * IDirect3DViewport3::TransformVertices
363 * Transforms vertices by the transformation matrix.
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.
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
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
386 *****************************************************************************/
387 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
388 DWORD dwVertexCount, D3DTRANSFORMDATA *lpData, DWORD dwFlags, DWORD *lpOffScreen)
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;
399 TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
400 iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
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)
406 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
407 return D3DERR_VIEWPORTHASNODEVICE;
411 if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
413 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
414 return DDERR_INVALIDPARAMS;
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);
426 in = lpData->lpIn;
427 out = lpData->lpOut;
428 outH = lpData->lpHOut;
429 for(i = 0; i < dwVertexCount; i++)
431 x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + mat._41;
432 y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + mat._42;
433 z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + mat._43;
434 w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + mat._44;
436 if(dwFlags & D3DTRANSFORM_CLIPPED)
438 /* If clipping is enabled, Windows assumes that outH is
439 * a valid pointer
441 outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
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;
457 if(outH[i].dwFlags)
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.
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;
474 w = 1 / w;
475 x *= w; y *= w; z *= w;
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);
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.
490 * If clipping is requested, Windows assumes that the offscreen
491 * param is a valid pointer.
493 if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
495 *lpOffScreen = outH[0].dwFlags;
497 else if(*lpOffScreen)
499 *lpOffScreen = 0;
501 wined3d_mutex_unlock();
503 TRACE("All done\n");
504 return DD_OK;
507 /*****************************************************************************
508 * IDirect3DViewport3::LightElements
510 * The DirectX 5.0 sdk says that it's not implemented
512 * Params:
515 * Returns:
516 * DDERR_UNSUPPORTED
518 *****************************************************************************/
519 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
520 DWORD element_count, D3DLIGHTDATA *data)
522 TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
524 return DDERR_UNSUPPORTED;
527 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
529 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
530 struct d3d_material *m;
532 TRACE("iface %p, material %#x.\n", iface, material);
534 wined3d_mutex_lock();
536 if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
538 WARN("Invalid material handle %#x.\n", material);
539 wined3d_mutex_unlock();
540 return DDERR_INVALIDPARAMS;
543 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
544 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
545 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
546 viewport->background = m;
548 wined3d_mutex_unlock();
550 return D3D_OK;
553 /*****************************************************************************
554 * IDirect3DViewport3::GetBackground
556 * Returns the material handle assigned to the background of the viewport
558 * Params:
559 * lphMat: Address to store the handle
560 * lpValid: is set to FALSE if no background is set, TRUE if one is set
562 * Returns:
563 * D3D_OK
565 *****************************************************************************/
566 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
567 D3DMATERIALHANDLE *material, BOOL *valid)
569 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
571 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
573 wined3d_mutex_lock();
574 if (valid)
575 *valid = !!viewport->background;
576 if (material)
577 *material = viewport->background ? viewport->background->Handle : 0;
578 wined3d_mutex_unlock();
580 return D3D_OK;
583 /*****************************************************************************
584 * IDirect3DViewport3::SetBackgroundDepth
586 * Sets a surface that represents the background depth. It's contents are
587 * used to set the depth buffer in IDirect3DViewport3::Clear
589 * Params:
590 * lpDDSurface: Surface to set
592 * Returns: D3D_OK, because it's a stub
594 *****************************************************************************/
595 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
597 FIXME("iface %p, surface %p stub!\n", iface, surface);
599 return D3D_OK;
602 /*****************************************************************************
603 * IDirect3DViewport3::GetBackgroundDepth
605 * Returns the surface that represents the depth field
607 * Params:
608 * lplpDDSurface: Address to store the interface pointer
609 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
611 * Returns:
612 * D3D_OK, because it's a stub
613 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
615 *****************************************************************************/
616 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
617 IDirectDrawSurface **surface, BOOL *valid)
619 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
621 return DD_OK;
624 /*****************************************************************************
625 * IDirect3DViewport3::Clear
627 * Clears the render target and / or the z buffer
629 * Params:
630 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
631 * cleared
632 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
633 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
635 * Returns:
636 * D3D_OK on success
637 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
638 * The return value of IDirect3DDevice7::Clear
640 *****************************************************************************/
641 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
642 DWORD rect_count, D3DRECT *rects, DWORD flags)
644 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
645 DWORD color = 0x00000000;
646 HRESULT hr;
647 IDirect3DViewport3 *current_viewport;
648 IDirect3DDevice3 *d3d_device3;
650 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
652 if (!rects || !rect_count)
654 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
655 return D3D_OK;
658 if (This->active_device == NULL) {
659 ERR(" Trying to clear a viewport not attached to a device!\n");
660 return D3DERR_VIEWPORTHASNODEVICE;
662 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
664 wined3d_mutex_lock();
666 if (flags & D3DCLEAR_TARGET)
668 if (!This->background)
669 WARN("No background material set.\n");
670 else
671 color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
672 This->background->mat.u.diffuse.u2.g,
673 This->background->mat.u.diffuse.u3.b,
674 This->background->mat.u.diffuse.u4.a);
677 /* Need to temporarily activate viewport to clear it. Previously active one will be restored
678 afterwards. */
679 viewport_activate(This, TRUE);
681 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
682 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
684 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
686 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
687 viewport_activate(vp, TRUE);
688 IDirect3DViewport3_Release(current_viewport);
691 wined3d_mutex_unlock();
693 return hr;
696 /*****************************************************************************
697 * IDirect3DViewport3::AddLight
699 * Adds an light to the viewport
701 * Params:
702 * lpDirect3DLight: Interface of the light to add
704 * Returns:
705 * D3D_OK on success
706 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
707 * DDERR_INVALIDPARAMS if there are 8 lights or more
709 *****************************************************************************/
710 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
712 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
713 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
714 DWORD i = 0;
715 DWORD map = This->map_lights;
717 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
719 wined3d_mutex_lock();
721 if (This->num_lights >= 8)
723 wined3d_mutex_unlock();
724 return DDERR_INVALIDPARAMS;
727 /* Find a light number and update both light and viewports objects accordingly */
728 while (map & 1)
730 map >>= 1;
731 ++i;
733 light_impl->dwLightIndex = i;
734 This->num_lights++;
735 This->map_lights |= 1<<i;
737 /* Add the light in the 'linked' chain */
738 list_add_head(&This->light_list, &light_impl->entry);
739 IDirect3DLight_AddRef(lpDirect3DLight);
741 /* Attach the light to the viewport */
742 light_impl->active_viewport = This;
744 /* If active, activate the light */
745 if (This->active_device)
746 light_activate(light_impl);
748 wined3d_mutex_unlock();
750 return D3D_OK;
753 /*****************************************************************************
754 * IDirect3DViewport3::DeleteLight
756 * Deletes a light from the viewports' light list
758 * Params:
759 * lpDirect3DLight: Light to delete
761 * Returns:
762 * D3D_OK on success
763 * DDERR_INVALIDPARAMS if the light wasn't found
765 *****************************************************************************/
766 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
768 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
769 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
771 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
773 wined3d_mutex_lock();
775 if (l->active_viewport != viewport)
777 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
778 wined3d_mutex_unlock();
779 return DDERR_INVALIDPARAMS;
782 light_deactivate(l);
783 list_remove(&l->entry);
784 l->active_viewport = NULL;
785 IDirect3DLight_Release(lpDirect3DLight);
786 --viewport->num_lights;
787 viewport->map_lights &= ~(1 << l->dwLightIndex);
789 wined3d_mutex_unlock();
791 return D3D_OK;
794 /*****************************************************************************
795 * IDirect3DViewport::NextLight
797 * Enumerates the lights associated with the viewport
799 * Params:
800 * lpDirect3DLight: Light to start with
801 * lplpDirect3DLight: Address to store the successor to
803 * Returns:
804 * D3D_OK, because it's a stub
806 *****************************************************************************/
807 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
808 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
810 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
811 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
812 struct list *entry;
813 HRESULT hr;
815 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
816 iface, lpDirect3DLight, lplpDirect3DLight, flags);
818 if (!lplpDirect3DLight)
819 return DDERR_INVALIDPARAMS;
821 wined3d_mutex_lock();
823 switch (flags)
825 case D3DNEXT_NEXT:
826 if (!l || l->active_viewport != viewport)
828 if (l)
829 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
830 entry = NULL;
832 else
833 entry = list_next(&viewport->light_list, &l->entry);
834 break;
836 case D3DNEXT_HEAD:
837 entry = list_head(&viewport->light_list);
838 break;
840 case D3DNEXT_TAIL:
841 entry = list_tail(&viewport->light_list);
842 break;
844 default:
845 entry = NULL;
846 WARN("Invalid flags %#x.\n", flags);
847 break;
850 if (entry)
852 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
853 IDirect3DLight_AddRef(*lplpDirect3DLight);
854 hr = D3D_OK;
856 else
858 *lplpDirect3DLight = NULL;
859 hr = DDERR_INVALIDPARAMS;
862 wined3d_mutex_unlock();
864 return hr;
867 /*****************************************************************************
868 * IDirect3DViewport2 Methods.
869 *****************************************************************************/
871 /*****************************************************************************
872 * IDirect3DViewport3::GetViewport2
874 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
875 * Similar to IDirect3DViewport3::GetViewport
877 * Params:
878 * lpData: Pointer to the structure to fill
880 * Returns:
881 * D3D_OK on success
882 * DDERR_INVALIDPARAMS if the viewport was set with
883 * IDirect3DViewport3::SetViewport
884 * DDERR_INVALIDPARAMS if Data is NULL
886 *****************************************************************************/
887 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
889 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
890 DWORD dwSize;
892 TRACE("iface %p, data %p.\n", iface, lpData);
894 wined3d_mutex_lock();
895 dwSize = lpData->dwSize;
896 memset(lpData, 0, dwSize);
897 if (This->use_vp2)
898 memcpy(lpData, &(This->viewports.vp2), dwSize);
899 else {
900 D3DVIEWPORT2 vp2;
901 vp2.dwSize = sizeof(vp2);
902 vp2.dwX = This->viewports.vp1.dwX;
903 vp2.dwY = This->viewports.vp1.dwY;
904 vp2.dwWidth = This->viewports.vp1.dwWidth;
905 vp2.dwHeight = This->viewports.vp1.dwHeight;
906 vp2.dvClipX = 0.0;
907 vp2.dvClipY = 0.0;
908 vp2.dvClipWidth = 0.0;
909 vp2.dvClipHeight = 0.0;
910 vp2.dvMinZ = This->viewports.vp1.dvMinZ;
911 vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
912 memcpy(lpData, &vp2, dwSize);
915 if (TRACE_ON(ddraw))
917 TRACE(" returning D3DVIEWPORT2 :\n");
918 _dump_D3DVIEWPORT2(lpData);
921 wined3d_mutex_unlock();
923 return D3D_OK;
926 /*****************************************************************************
927 * IDirect3DViewport3::SetViewport2
929 * Sets the viewport from a D3DVIEWPORT2 structure
931 * Params:
932 * lpData: Viewport to set
934 * Returns:
935 * D3D_OK on success
937 *****************************************************************************/
938 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
940 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
941 IDirect3DViewport3 *current_viewport;
943 TRACE("iface %p, data %p.\n", iface, lpData);
945 if (TRACE_ON(ddraw))
947 TRACE(" getting D3DVIEWPORT2 :\n");
948 _dump_D3DVIEWPORT2(lpData);
951 wined3d_mutex_lock();
953 This->use_vp2 = 1;
954 memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
955 memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
957 if (This->active_device)
959 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
960 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
962 if (current_viewport == iface) viewport_activate(This, FALSE);
963 IDirect3DViewport3_Release(current_viewport);
967 wined3d_mutex_unlock();
969 return D3D_OK;
972 /*****************************************************************************
973 * IDirect3DViewport3 Methods.
974 *****************************************************************************/
976 /*****************************************************************************
977 * IDirect3DViewport3::SetBackgroundDepth2
979 * Sets a IDirectDrawSurface4 surface as the background depth surface
981 * Params:
982 * lpDDS: Surface to set
984 * Returns:
985 * D3D_OK, because it's stub
987 *****************************************************************************/
988 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
989 IDirectDrawSurface4 *surface)
991 FIXME("iface %p, surface %p stub!\n", iface, surface);
993 return D3D_OK;
996 /*****************************************************************************
997 * IDirect3DViewport3::GetBackgroundDepth2
999 * Returns the IDirect3DSurface4 interface to the background depth surface
1001 * Params:
1002 * lplpDDS: Address to store the interface pointer at
1003 * lpValid: Set to true if a surface is assigned
1005 * Returns:
1006 * D3D_OK because it's a stub
1008 *****************************************************************************/
1009 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1010 IDirectDrawSurface4 **surface, BOOL *valid)
1012 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1014 return D3D_OK;
1017 /*****************************************************************************
1018 * IDirect3DViewport3::Clear2
1020 * Another clearing method
1022 * Params:
1023 * Count: Number of rectangles to clear
1024 * Rects: Rectangle array to clear
1025 * Flags: Some flags :)
1026 * Color: Color to fill the render target with
1027 * Z: Value to fill the depth buffer with
1028 * Stencil: Value to fill the stencil bits with
1030 * Returns:
1032 *****************************************************************************/
1033 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1034 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1036 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1037 HRESULT hr;
1038 IDirect3DViewport3 *current_viewport;
1039 IDirect3DDevice3 *d3d_device3;
1041 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1042 iface, rect_count, rects, flags, color, depth, stencil);
1044 if (!rects || !rect_count)
1046 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
1047 return D3D_OK;
1050 wined3d_mutex_lock();
1052 if (!viewport->active_device)
1054 WARN("Trying to clear a viewport not attached to a device.\n");
1055 wined3d_mutex_unlock();
1056 return D3DERR_VIEWPORTHASNODEVICE;
1058 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1059 /* Need to temporarily activate viewport to clear it. Previously active
1060 * one will be restored afterwards. */
1061 viewport_activate(viewport, TRUE);
1063 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1064 rect_count, rects, flags, color, depth, stencil);
1065 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1067 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1068 viewport_activate(vp, TRUE);
1069 IDirect3DViewport3_Release(current_viewport);
1072 wined3d_mutex_unlock();
1074 return hr;
1077 /*****************************************************************************
1078 * The VTable
1079 *****************************************************************************/
1081 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1083 /*** IUnknown Methods ***/
1084 d3d_viewport_QueryInterface,
1085 d3d_viewport_AddRef,
1086 d3d_viewport_Release,
1087 /*** IDirect3DViewport Methods */
1088 d3d_viewport_Initialize,
1089 d3d_viewport_GetViewport,
1090 d3d_viewport_SetViewport,
1091 d3d_viewport_TransformVertices,
1092 d3d_viewport_LightElements,
1093 d3d_viewport_SetBackground,
1094 d3d_viewport_GetBackground,
1095 d3d_viewport_SetBackgroundDepth,
1096 d3d_viewport_GetBackgroundDepth,
1097 d3d_viewport_Clear,
1098 d3d_viewport_AddLight,
1099 d3d_viewport_DeleteLight,
1100 d3d_viewport_NextLight,
1101 /*** IDirect3DViewport2 Methods ***/
1102 d3d_viewport_GetViewport2,
1103 d3d_viewport_SetViewport2,
1104 /*** IDirect3DViewport3 Methods ***/
1105 d3d_viewport_SetBackgroundDepth2,
1106 d3d_viewport_GetBackgroundDepth2,
1107 d3d_viewport_Clear2,
1110 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1112 if (!iface) return NULL;
1113 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1114 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1117 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1119 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1120 if (!iface) return NULL;
1121 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1122 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1125 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1127 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1128 if (!iface) return NULL;
1129 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1130 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1133 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1135 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1136 viewport->ref = 1;
1137 viewport->ddraw = ddraw;
1138 viewport->use_vp2 = 0xff;
1139 list_init(&viewport->light_list);