Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / ddraw / viewport.c
blob6a4713af4624dfcf9542254c67ad6ca576fb19f9
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;
44 HRESULT hr;
46 multiply_matrix(&projection, &clip_space, &device->legacy_projection);
47 hr = wined3d_device_set_transform(device->wined3d_device,
48 WINED3D_TS_PROJECTION, (struct wined3d_matrix *)&projection);
49 if (SUCCEEDED(hr))
50 device->legacy_clipspace = clip_space;
53 /*****************************************************************************
54 * viewport_activate
56 * activates the viewport using IDirect3DDevice7::SetViewport
58 *****************************************************************************/
59 void viewport_activate(struct d3d_viewport *This, BOOL ignore_lights)
61 struct wined3d_vec3 scale, offset;
62 D3DVIEWPORT7 vp;
64 if (!ignore_lights)
66 struct d3d_light *light;
68 /* Activate all the lights associated with this context */
69 LIST_FOR_EACH_ENTRY(light, &This->light_list, struct d3d_light, entry)
71 light_activate(light);
75 /* And copy the values in the structure used by the device */
76 if (This->use_vp2)
78 vp.dwX = This->viewports.vp2.dwX;
79 vp.dwY = This->viewports.vp2.dwY;
80 vp.dwHeight = This->viewports.vp2.dwHeight;
81 vp.dwWidth = This->viewports.vp2.dwWidth;
82 vp.dvMinZ = 0.0f;
83 vp.dvMaxZ = 1.0f;
85 scale.x = 2.0f / This->viewports.vp2.dvClipWidth;
86 scale.y = 2.0f / This->viewports.vp2.dvClipHeight;
87 scale.z = 1.0f / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
88 offset.x = -2.0f * This->viewports.vp2.dvClipX / This->viewports.vp2.dvClipWidth - 1.0f;
89 offset.y = -2.0f * This->viewports.vp2.dvClipY / This->viewports.vp2.dvClipHeight + 1.0f;
90 offset.z = -This->viewports.vp2.dvMinZ / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
92 else
94 vp.dwX = This->viewports.vp1.dwX;
95 vp.dwY = This->viewports.vp1.dwY;
96 vp.dwHeight = This->viewports.vp1.dwHeight;
97 vp.dwWidth = This->viewports.vp1.dwWidth;
98 vp.dvMinZ = 0.0f;
99 vp.dvMaxZ = 1.0f;
101 scale.x = 2.0f * This->viewports.vp1.dvScaleX / This->viewports.vp1.dwWidth;
102 scale.y = 2.0f * This->viewports.vp1.dvScaleY / This->viewports.vp1.dwHeight;
103 scale.z = 1.0f;
104 offset.x = 0.0f;
105 offset.y = 0.0f;
106 offset.z = 0.0f;
109 update_clip_space(This->active_device, &scale, &offset);
110 IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
113 /*****************************************************************************
114 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
116 * Writes viewport information to TRACE
118 *****************************************************************************/
119 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
121 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
122 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
123 TRACE(" - dwWidth = %d dwHeight = %d\n",
124 lpvp->dwWidth, lpvp->dwHeight);
125 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
126 lpvp->dvScaleX, lpvp->dvScaleY);
127 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
128 lpvp->dvMaxX, lpvp->dvMaxY);
129 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
130 lpvp->dvMinZ, lpvp->dvMaxZ);
133 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
135 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
136 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
137 TRACE(" - dwWidth = %d dwHeight = %d\n",
138 lpvp->dwWidth, lpvp->dwHeight);
139 TRACE(" - dvClipX = %f dvClipY = %f\n",
140 lpvp->dvClipX, lpvp->dvClipY);
141 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
142 lpvp->dvClipWidth, lpvp->dvClipHeight);
143 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
144 lpvp->dvMinZ, lpvp->dvMaxZ);
147 static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
149 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
152 /*****************************************************************************
153 * IUnknown Methods.
154 *****************************************************************************/
156 /*****************************************************************************
157 * IDirect3DViewport3::QueryInterface
159 * A normal QueryInterface. Can query all interface versions and the
160 * IUnknown interface. The VTables of the different versions
161 * are equal
163 * Params:
164 * refiid: Interface id queried for
165 * obj: Address to write the interface pointer to
167 * Returns:
168 * S_OK on success.
169 * E_NOINTERFACE if the requested interface wasn't found
171 *****************************************************************************/
172 static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
174 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
176 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
177 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
178 || IsEqualGUID(&IID_IDirect3DViewport, riid)
179 || IsEqualGUID(&IID_IUnknown, riid))
181 IDirect3DViewport3_AddRef(iface);
182 *object = iface;
183 return S_OK;
186 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
188 *object = NULL;
189 return E_NOINTERFACE;
192 /*****************************************************************************
193 * IDirect3DViewport3::AddRef
195 * Increases the refcount.
197 * Returns:
198 * The new refcount
200 *****************************************************************************/
201 static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
203 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
204 ULONG ref = InterlockedIncrement(&viewport->ref);
206 TRACE("%p increasing refcount to %u.\n", viewport, ref);
208 return ref;
211 /*****************************************************************************
212 * IDirect3DViewport3::Release
214 * Reduces the refcount. If it falls to 0, the interface is released
216 * Returns:
217 * The new refcount
219 *****************************************************************************/
220 static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
222 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
223 ULONG ref = InterlockedDecrement(&viewport->ref);
225 TRACE("%p decreasing refcount to %u.\n", viewport, ref);
227 if (!ref)
228 HeapFree(GetProcessHeap(), 0, viewport);
230 return ref;
233 /*****************************************************************************
234 * IDirect3DViewport Methods.
235 *****************************************************************************/
237 /*****************************************************************************
238 * IDirect3DViewport3::Initialize
240 * No-op initialization.
242 * Params:
243 * Direct3D: The direct3D device this viewport is assigned to
245 * Returns:
246 * DDERR_ALREADYINITIALIZED
248 *****************************************************************************/
249 static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
251 TRACE("iface %p, d3d %p.\n", iface, d3d);
253 return DDERR_ALREADYINITIALIZED;
256 /*****************************************************************************
257 * IDirect3DViewport3::GetViewport
259 * Returns the viewport data assigned to this viewport interface
261 * Params:
262 * Data: Address to store the data
264 * Returns:
265 * D3D_OK on success
266 * DDERR_INVALIDPARAMS if Data is NULL
268 *****************************************************************************/
269 static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
271 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
272 DWORD dwSize;
274 TRACE("iface %p, data %p.\n", iface, lpData);
276 wined3d_mutex_lock();
278 dwSize = lpData->dwSize;
279 memset(lpData, 0, dwSize);
280 if (!This->use_vp2)
281 memcpy(lpData, &(This->viewports.vp1), dwSize);
282 else {
283 D3DVIEWPORT vp1;
284 vp1.dwSize = sizeof(vp1);
285 vp1.dwX = This->viewports.vp2.dwX;
286 vp1.dwY = This->viewports.vp2.dwY;
287 vp1.dwWidth = This->viewports.vp2.dwWidth;
288 vp1.dwHeight = This->viewports.vp2.dwHeight;
289 vp1.dvMaxX = 0.0;
290 vp1.dvMaxY = 0.0;
291 vp1.dvScaleX = 0.0;
292 vp1.dvScaleY = 0.0;
293 vp1.dvMinZ = This->viewports.vp2.dvMinZ;
294 vp1.dvMaxZ = This->viewports.vp2.dvMaxZ;
295 memcpy(lpData, &vp1, dwSize);
298 if (TRACE_ON(ddraw))
300 TRACE(" returning D3DVIEWPORT :\n");
301 _dump_D3DVIEWPORT(lpData);
304 wined3d_mutex_unlock();
306 return DD_OK;
309 /*****************************************************************************
310 * IDirect3DViewport3::SetViewport
312 * Sets the viewport information for this interface
314 * Params:
315 * lpData: Viewport to set
317 * Returns:
318 * D3D_OK on success
319 * DDERR_INVALIDPARAMS if Data is NULL
321 *****************************************************************************/
322 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
324 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
325 IDirect3DViewport3 *current_viewport;
327 TRACE("iface %p, data %p.\n", iface, lpData);
329 if (TRACE_ON(ddraw))
331 TRACE(" getting D3DVIEWPORT :\n");
332 _dump_D3DVIEWPORT(lpData);
335 wined3d_mutex_lock();
337 This->use_vp2 = 0;
338 memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
339 memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
341 /* Tests on two games show that these values are never used properly so override
342 them with proper ones :-)
344 This->viewports.vp1.dvMinZ = 0.0;
345 This->viewports.vp1.dvMaxZ = 1.0;
347 if (This->active_device) {
348 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
349 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
350 if (current_viewport)
352 if (current_viewport == iface) viewport_activate(This, FALSE);
353 IDirect3DViewport3_Release(current_viewport);
357 wined3d_mutex_unlock();
359 return DD_OK;
362 /*****************************************************************************
363 * IDirect3DViewport3::TransformVertices
365 * Transforms vertices by the transformation matrix.
367 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
368 * so it's tempting to forward it to there. However, there are some
369 * tiny differences. First, the lpOffscreen flag that is reported back,
370 * then there is the homogeneous vertex that is generated. Also there's a lack
371 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
372 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
373 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
374 * reused.
376 * Params:
377 * dwVertexCount: The number of vertices to be transformed
378 * lpData: Pointer to the vertex data
379 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
380 * lpOffScreen: Set to the clipping plane clipping the vertex, if only one
381 * vertex is transformed and clipping is on. 0 otherwise
383 * Returns:
384 * D3D_OK on success
385 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
386 * DDERR_INVALIDPARAMS if no clipping flag is specified
388 *****************************************************************************/
389 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
390 DWORD dwVertexCount, D3DTRANSFORMDATA *lpData, DWORD dwFlags, DWORD *lpOffScreen)
392 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
393 D3DVIEWPORT vp = viewport->viewports.vp1;
394 D3DMATRIX view_mat, world_mat, mat;
395 float *in;
396 float *out;
397 float x, y, z, w;
398 unsigned int i;
399 D3DHVERTEX *outH;
401 TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
402 iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
404 /* Tests on windows show that Windows crashes when this occurs,
405 * so don't return the (intuitive) return value
406 if (!viewport->active_device)
408 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
409 return D3DERR_VIEWPORTHASNODEVICE;
413 if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
415 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
416 return DDERR_INVALIDPARAMS;
420 wined3d_mutex_lock();
421 wined3d_device_get_transform(viewport->active_device->wined3d_device,
422 D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
423 wined3d_device_get_transform(viewport->active_device->wined3d_device,
424 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
425 multiply_matrix(&mat, &view_mat, &world_mat);
426 multiply_matrix(&mat, &viewport->active_device->legacy_projection, &mat);
428 in = lpData->lpIn;
429 out = lpData->lpOut;
430 outH = lpData->lpHOut;
431 for(i = 0; i < dwVertexCount; i++)
433 x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + (1.0 * mat._41);
434 y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + (1.0 * mat._42);
435 z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + (1.0 * mat._43);
436 w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + (1.0 * mat._44);
438 if(dwFlags & D3DTRANSFORM_CLIPPED)
440 /* If clipping is enabled, Windows assumes that outH is
441 * a valid pointer
443 outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
445 outH[i].dwFlags = 0;
446 if(x * vp.dvScaleX > ((float) vp.dwWidth * 0.5))
447 outH[i].dwFlags |= D3DCLIP_RIGHT;
448 if(x * vp.dvScaleX <= -((float) vp.dwWidth) * 0.5)
449 outH[i].dwFlags |= D3DCLIP_LEFT;
450 if(y * vp.dvScaleY > ((float) vp.dwHeight * 0.5))
451 outH[i].dwFlags |= D3DCLIP_TOP;
452 if(y * vp.dvScaleY <= -((float) vp.dwHeight) * 0.5)
453 outH[i].dwFlags |= D3DCLIP_BOTTOM;
454 if(z < 0.0)
455 outH[i].dwFlags |= D3DCLIP_FRONT;
456 if(z > 1.0)
457 outH[i].dwFlags |= D3DCLIP_BACK;
459 if(outH[i].dwFlags)
461 /* Looks like native just drops the vertex, leaves whatever data
462 * it has in the output buffer and goes on with the next vertex.
463 * The exact scheme hasn't been figured out yet, but windows
464 * definitely writes something there.
466 out[0] = x;
467 out[1] = y;
468 out[2] = z;
469 out[3] = w;
470 in = (float *) ((char *) in + lpData->dwInSize);
471 out = (float *) ((char *) out + lpData->dwOutSize);
472 continue;
476 w = 1 / w;
477 x *= w; y *= w; z *= w;
479 out[0] = vp.dwWidth / 2 + vp.dwX + x * vp.dvScaleX;
480 out[1] = vp.dwHeight / 2 + vp.dwY - y * vp.dvScaleY;
481 out[2] = z;
482 out[3] = w;
483 in = (float *) ((char *) in + lpData->dwInSize);
484 out = (float *) ((char *) out + lpData->dwOutSize);
487 /* According to the d3d test, the offscreen flag is set only
488 * if exactly one vertex is transformed. Its not documented,
489 * but the test shows that the lpOffscreen flag is set to the
490 * flag combination of clipping planes that clips the vertex.
492 * If clipping is requested, Windows assumes that the offscreen
493 * param is a valid pointer.
495 if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
497 *lpOffScreen = outH[0].dwFlags;
499 else if(*lpOffScreen)
501 *lpOffScreen = 0;
503 wined3d_mutex_unlock();
505 TRACE("All done\n");
506 return DD_OK;
509 /*****************************************************************************
510 * IDirect3DViewport3::LightElements
512 * The DirectX 5.0 sdk says that it's not implemented
514 * Params:
517 * Returns:
518 * DDERR_UNSUPPORTED
520 *****************************************************************************/
521 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
522 DWORD element_count, D3DLIGHTDATA *data)
524 TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
526 return DDERR_UNSUPPORTED;
529 /*****************************************************************************
530 * IDirect3DViewport3::SetBackground
532 * Sets the background material
534 * Params:
535 * hMat: Handle from a IDirect3DMaterial interface
537 * Returns:
538 * D3D_OK on success
540 *****************************************************************************/
541 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE hMat)
543 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
544 struct d3d_material *m;
546 TRACE("iface %p, material %#x.\n", iface, hMat);
548 wined3d_mutex_lock();
550 if (!hMat)
552 viewport->background = NULL;
553 TRACE("Setting background to NULL\n");
554 wined3d_mutex_unlock();
555 return D3D_OK;
558 m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, hMat - 1, DDRAW_HANDLE_MATERIAL);
559 if (!m)
561 WARN("Invalid material handle.\n");
562 wined3d_mutex_unlock();
563 return DDERR_INVALIDPARAMS;
566 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
567 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
568 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
569 viewport->background = m;
571 wined3d_mutex_unlock();
573 return D3D_OK;
576 /*****************************************************************************
577 * IDirect3DViewport3::GetBackground
579 * Returns the material handle assigned to the background of the viewport
581 * Params:
582 * lphMat: Address to store the handle
583 * lpValid: is set to FALSE if no background is set, TRUE if one is set
585 * Returns:
586 * D3D_OK
588 *****************************************************************************/
589 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
590 D3DMATERIALHANDLE *material, BOOL *valid)
592 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
594 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
596 wined3d_mutex_lock();
597 if (valid)
598 *valid = !!viewport->background;
599 if (material)
600 *material = viewport->background ? viewport->background->Handle : 0;
601 wined3d_mutex_unlock();
603 return D3D_OK;
606 /*****************************************************************************
607 * IDirect3DViewport3::SetBackgroundDepth
609 * Sets a surface that represents the background depth. It's contents are
610 * used to set the depth buffer in IDirect3DViewport3::Clear
612 * Params:
613 * lpDDSurface: Surface to set
615 * Returns: D3D_OK, because it's a stub
617 *****************************************************************************/
618 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
620 FIXME("iface %p, surface %p stub!\n", iface, surface);
622 return D3D_OK;
625 /*****************************************************************************
626 * IDirect3DViewport3::GetBackgroundDepth
628 * Returns the surface that represents the depth field
630 * Params:
631 * lplpDDSurface: Address to store the interface pointer
632 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
634 * Returns:
635 * D3D_OK, because it's a stub
636 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
638 *****************************************************************************/
639 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
640 IDirectDrawSurface **surface, BOOL *valid)
642 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
644 return DD_OK;
647 /*****************************************************************************
648 * IDirect3DViewport3::Clear
650 * Clears the render target and / or the z buffer
652 * Params:
653 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
654 * cleared
655 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
656 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
658 * Returns:
659 * D3D_OK on success
660 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
661 * The return value of IDirect3DDevice7::Clear
663 *****************************************************************************/
664 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
665 DWORD rect_count, D3DRECT *rects, DWORD flags)
667 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
668 DWORD color = 0x00000000;
669 HRESULT hr;
670 IDirect3DViewport3 *current_viewport;
671 IDirect3DDevice3 *d3d_device3;
673 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
675 if (This->active_device == NULL) {
676 ERR(" Trying to clear a viewport not attached to a device !\n");
677 return D3DERR_VIEWPORTHASNODEVICE;
679 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
681 wined3d_mutex_lock();
683 if (flags & D3DCLEAR_TARGET)
685 if (This->background == NULL) {
686 ERR(" Trying to clear the color buffer without background material !\n");
688 else
690 color = ((int)((This->background->mat.u.diffuse.u1.r) * 255) << 16)
691 | ((int) ((This->background->mat.u.diffuse.u2.g) * 255) << 8)
692 | ((int) ((This->background->mat.u.diffuse.u3.b) * 255) << 0)
693 | ((int) ((This->background->mat.u.diffuse.u4.a) * 255) << 24);
697 /* Need to temporarily activate viewport to clear it. Previously active one will be restored
698 afterwards. */
699 viewport_activate(This, TRUE);
701 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
702 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
704 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
705 if (current_viewport)
707 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
708 viewport_activate(vp, TRUE);
709 IDirect3DViewport3_Release(current_viewport);
712 wined3d_mutex_unlock();
714 return hr;
717 /*****************************************************************************
718 * IDirect3DViewport3::AddLight
720 * Adds an light to the viewport
722 * Params:
723 * lpDirect3DLight: Interface of the light to add
725 * Returns:
726 * D3D_OK on success
727 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
728 * DDERR_INVALIDPARAMS if there are 8 lights or more
730 *****************************************************************************/
731 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
733 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
734 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
735 DWORD i = 0;
736 DWORD map = This->map_lights;
738 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
740 wined3d_mutex_lock();
742 if (This->num_lights >= 8)
744 wined3d_mutex_unlock();
745 return DDERR_INVALIDPARAMS;
748 /* Find a light number and update both light and viewports objects accordingly */
749 while (map & 1)
751 map >>= 1;
752 ++i;
754 light_impl->dwLightIndex = i;
755 This->num_lights++;
756 This->map_lights |= 1<<i;
758 /* Add the light in the 'linked' chain */
759 list_add_head(&This->light_list, &light_impl->entry);
760 IDirect3DLight_AddRef(lpDirect3DLight);
762 /* Attach the light to the viewport */
763 light_impl->active_viewport = This;
765 /* If active, activate the light */
766 if (This->active_device)
767 light_activate(light_impl);
769 wined3d_mutex_unlock();
771 return D3D_OK;
774 /*****************************************************************************
775 * IDirect3DViewport3::DeleteLight
777 * Deletes a light from the viewports' light list
779 * Params:
780 * lpDirect3DLight: Light to delete
782 * Returns:
783 * D3D_OK on success
784 * DDERR_INVALIDPARAMS if the light wasn't found
786 *****************************************************************************/
787 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
789 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
790 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
792 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
794 wined3d_mutex_lock();
796 if (l->active_viewport != viewport)
798 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
799 wined3d_mutex_unlock();
800 return DDERR_INVALIDPARAMS;
803 light_deactivate(l);
804 list_remove(&l->entry);
805 l->active_viewport = NULL;
806 IDirect3DLight_Release(lpDirect3DLight);
807 --viewport->num_lights;
808 viewport->map_lights &= ~(1 << l->dwLightIndex);
810 wined3d_mutex_unlock();
812 return D3D_OK;
815 /*****************************************************************************
816 * IDirect3DViewport::NextLight
818 * Enumerates the lights associated with the viewport
820 * Params:
821 * lpDirect3DLight: Light to start with
822 * lplpDirect3DLight: Address to store the successor to
824 * Returns:
825 * D3D_OK, because it's a stub
827 *****************************************************************************/
828 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
829 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
831 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
832 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
833 struct list *entry;
834 HRESULT hr;
836 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
837 iface, lpDirect3DLight, lplpDirect3DLight, flags);
839 if (!lplpDirect3DLight)
840 return DDERR_INVALIDPARAMS;
842 wined3d_mutex_lock();
844 switch (flags)
846 case D3DNEXT_NEXT:
847 if (!l || l->active_viewport != viewport)
849 if (l)
850 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
851 entry = NULL;
853 else
854 entry = list_next(&viewport->light_list, &l->entry);
855 break;
857 case D3DNEXT_HEAD:
858 entry = list_head(&viewport->light_list);
859 break;
861 case D3DNEXT_TAIL:
862 entry = list_tail(&viewport->light_list);
863 break;
865 default:
866 entry = NULL;
867 WARN("Invalid flags %#x.\n", flags);
868 break;
871 if (entry)
873 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
874 IDirect3DLight_AddRef(*lplpDirect3DLight);
875 hr = D3D_OK;
877 else
879 *lplpDirect3DLight = NULL;
880 hr = DDERR_INVALIDPARAMS;
883 wined3d_mutex_unlock();
885 return hr;
888 /*****************************************************************************
889 * IDirect3DViewport2 Methods.
890 *****************************************************************************/
892 /*****************************************************************************
893 * IDirect3DViewport3::GetViewport2
895 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
896 * Similar to IDirect3DViewport3::GetViewport
898 * Params:
899 * lpData: Pointer to the structure to fill
901 * Returns:
902 * D3D_OK on success
903 * DDERR_INVALIDPARAMS if the viewport was set with
904 * IDirect3DViewport3::SetViewport
905 * DDERR_INVALIDPARAMS if Data is NULL
907 *****************************************************************************/
908 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
910 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
911 DWORD dwSize;
913 TRACE("iface %p, data %p.\n", iface, lpData);
915 wined3d_mutex_lock();
916 dwSize = lpData->dwSize;
917 memset(lpData, 0, dwSize);
918 if (This->use_vp2)
919 memcpy(lpData, &(This->viewports.vp2), dwSize);
920 else {
921 D3DVIEWPORT2 vp2;
922 vp2.dwSize = sizeof(vp2);
923 vp2.dwX = This->viewports.vp1.dwX;
924 vp2.dwY = This->viewports.vp1.dwY;
925 vp2.dwWidth = This->viewports.vp1.dwWidth;
926 vp2.dwHeight = This->viewports.vp1.dwHeight;
927 vp2.dvClipX = 0.0;
928 vp2.dvClipY = 0.0;
929 vp2.dvClipWidth = 0.0;
930 vp2.dvClipHeight = 0.0;
931 vp2.dvMinZ = This->viewports.vp1.dvMinZ;
932 vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
933 memcpy(lpData, &vp2, dwSize);
936 if (TRACE_ON(ddraw))
938 TRACE(" returning D3DVIEWPORT2 :\n");
939 _dump_D3DVIEWPORT2(lpData);
942 wined3d_mutex_unlock();
944 return D3D_OK;
947 /*****************************************************************************
948 * IDirect3DViewport3::SetViewport2
950 * Sets the viewport from a D3DVIEWPORT2 structure
952 * Params:
953 * lpData: Viewport to set
955 * Returns:
956 * D3D_OK on success
958 *****************************************************************************/
959 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
961 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
962 IDirect3DViewport3 *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 wined3d_mutex_lock();
974 This->use_vp2 = 1;
975 memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
976 memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
978 if (This->active_device) {
979 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
980 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
981 if (current_viewport)
983 if (current_viewport == iface) viewport_activate(This, FALSE);
984 IDirect3DViewport3_Release(current_viewport);
988 wined3d_mutex_unlock();
990 return D3D_OK;
993 /*****************************************************************************
994 * IDirect3DViewport3 Methods.
995 *****************************************************************************/
997 /*****************************************************************************
998 * IDirect3DViewport3::SetBackgroundDepth2
1000 * Sets a IDirectDrawSurface4 surface as the background depth surface
1002 * Params:
1003 * lpDDS: Surface to set
1005 * Returns:
1006 * D3D_OK, because it's stub
1008 *****************************************************************************/
1009 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
1010 IDirectDrawSurface4 *surface)
1012 FIXME("iface %p, surface %p stub!\n", iface, surface);
1014 return D3D_OK;
1017 /*****************************************************************************
1018 * IDirect3DViewport3::GetBackgroundDepth2
1020 * Returns the IDirect3DSurface4 interface to the background depth surface
1022 * Params:
1023 * lplpDDS: Address to store the interface pointer at
1024 * lpValid: Set to true if a surface is assigned
1026 * Returns:
1027 * D3D_OK because it's a stub
1029 *****************************************************************************/
1030 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1031 IDirectDrawSurface4 **surface, BOOL *valid)
1033 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1035 return D3D_OK;
1038 /*****************************************************************************
1039 * IDirect3DViewport3::Clear2
1041 * Another clearing method
1043 * Params:
1044 * Count: Number of rectangles to clear
1045 * Rects: Rectangle array to clear
1046 * Flags: Some flags :)
1047 * Color: Color to fill the render target with
1048 * Z: Value to fill the depth buffer with
1049 * Stencil: Value to fill the stencil bits with
1051 * Returns:
1053 *****************************************************************************/
1054 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1055 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1057 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1058 HRESULT hr;
1059 IDirect3DViewport3 *current_viewport;
1060 IDirect3DDevice3 *d3d_device3;
1062 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1063 iface, rect_count, rects, flags, color, depth, stencil);
1065 wined3d_mutex_lock();
1067 if (!viewport->active_device)
1069 WARN("Trying to clear a viewport not attached to a device.\n");
1070 wined3d_mutex_unlock();
1071 return D3DERR_VIEWPORTHASNODEVICE;
1073 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1074 /* Need to temporarily activate viewport to clear it. Previously active
1075 * one will be restored afterwards. */
1076 viewport_activate(viewport, TRUE);
1078 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1079 rect_count, rects, flags, color, depth, stencil);
1080 IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
1081 if (current_viewport)
1083 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1084 viewport_activate(vp, TRUE);
1085 IDirect3DViewport3_Release(current_viewport);
1088 wined3d_mutex_unlock();
1090 return hr;
1093 /*****************************************************************************
1094 * The VTable
1095 *****************************************************************************/
1097 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1099 /*** IUnknown Methods ***/
1100 d3d_viewport_QueryInterface,
1101 d3d_viewport_AddRef,
1102 d3d_viewport_Release,
1103 /*** IDirect3DViewport Methods */
1104 d3d_viewport_Initialize,
1105 d3d_viewport_GetViewport,
1106 d3d_viewport_SetViewport,
1107 d3d_viewport_TransformVertices,
1108 d3d_viewport_LightElements,
1109 d3d_viewport_SetBackground,
1110 d3d_viewport_GetBackground,
1111 d3d_viewport_SetBackgroundDepth,
1112 d3d_viewport_GetBackgroundDepth,
1113 d3d_viewport_Clear,
1114 d3d_viewport_AddLight,
1115 d3d_viewport_DeleteLight,
1116 d3d_viewport_NextLight,
1117 /*** IDirect3DViewport2 Methods ***/
1118 d3d_viewport_GetViewport2,
1119 d3d_viewport_SetViewport2,
1120 /*** IDirect3DViewport3 Methods ***/
1121 d3d_viewport_SetBackgroundDepth2,
1122 d3d_viewport_GetBackgroundDepth2,
1123 d3d_viewport_Clear2,
1126 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1128 if (!iface) return NULL;
1129 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1130 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1133 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1135 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1136 if (!iface) return NULL;
1137 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1138 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1141 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1143 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1144 if (!iface) return NULL;
1145 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1146 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1149 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1151 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1152 viewport->ref = 1;
1153 viewport->ddraw = ddraw;
1154 viewport->use_vp2 = 0xff;
1155 list_init(&viewport->light_list);