reg: Do not allow combinations of /v, /ve or /va in the 'delete' function.
[wine.git] / dlls / ddraw / viewport.c
blob5ebc41e1d5e7de7c067da9a5ef24849763d678a4
1 /* Direct3D Viewport
2 * Copyright (c) 1998 Lionel ULMER
3 * Copyright (c) 2006-2007 Stefan DÖSINGER
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "ddraw_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
27 /*****************************************************************************
28 * Helper functions
29 *****************************************************************************/
31 static void update_clip_space(struct d3d_device *device,
32 struct wined3d_vec3 *scale, struct wined3d_vec3 *offset)
34 D3DMATRIX clip_space =
36 scale->x, 0.0f, 0.0f, 0.0f,
37 0.0f, scale->y, 0.0f, 0.0f,
38 0.0f, 0.0f, scale->z, 0.0f,
39 offset->x, offset->y, offset->z, 1.0f,
41 D3DMATRIX projection;
43 multiply_matrix(&projection, &clip_space, &device->legacy_projection);
44 wined3d_device_set_transform(device->wined3d_device,
45 WINED3D_TS_PROJECTION, (struct wined3d_matrix *)&projection);
46 device->legacy_clipspace = clip_space;
49 /*****************************************************************************
50 * viewport_activate
52 * activates the viewport using IDirect3DDevice7::SetViewport
54 *****************************************************************************/
55 void viewport_activate(struct d3d_viewport *This, BOOL ignore_lights)
57 struct wined3d_vec3 scale, offset;
58 D3DVIEWPORT7 vp;
60 if (!ignore_lights)
62 struct d3d_light *light;
64 /* Activate all the lights associated with this context */
65 LIST_FOR_EACH_ENTRY(light, &This->light_list, struct d3d_light, entry)
67 light_activate(light);
71 /* And copy the values in the structure used by the device */
72 if (This->use_vp2)
74 vp.dwX = This->viewports.vp2.dwX;
75 vp.dwY = This->viewports.vp2.dwY;
76 vp.dwHeight = This->viewports.vp2.dwHeight;
77 vp.dwWidth = This->viewports.vp2.dwWidth;
78 vp.dvMinZ = 0.0f;
79 vp.dvMaxZ = 1.0f;
81 scale.x = 2.0f / This->viewports.vp2.dvClipWidth;
82 scale.y = 2.0f / This->viewports.vp2.dvClipHeight;
83 scale.z = 1.0f / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
84 offset.x = -2.0f * This->viewports.vp2.dvClipX / This->viewports.vp2.dvClipWidth - 1.0f;
85 offset.y = -2.0f * This->viewports.vp2.dvClipY / This->viewports.vp2.dvClipHeight + 1.0f;
86 offset.z = -This->viewports.vp2.dvMinZ / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
88 else
90 vp.dwX = This->viewports.vp1.dwX;
91 vp.dwY = This->viewports.vp1.dwY;
92 vp.dwHeight = This->viewports.vp1.dwHeight;
93 vp.dwWidth = This->viewports.vp1.dwWidth;
94 vp.dvMinZ = 0.0f;
95 vp.dvMaxZ = 1.0f;
97 scale.x = 2.0f * This->viewports.vp1.dvScaleX / This->viewports.vp1.dwWidth;
98 scale.y = 2.0f * This->viewports.vp1.dvScaleY / This->viewports.vp1.dwHeight;
99 scale.z = 1.0f;
100 offset.x = 0.0f;
101 offset.y = 0.0f;
102 offset.z = 0.0f;
105 update_clip_space(This->active_device, &scale, &offset);
106 IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
109 /*****************************************************************************
110 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
112 * Writes viewport information to TRACE
114 *****************************************************************************/
115 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
117 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
118 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
119 TRACE(" - dwWidth = %d dwHeight = %d\n",
120 lpvp->dwWidth, lpvp->dwHeight);
121 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
122 lpvp->dvScaleX, lpvp->dvScaleY);
123 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
124 lpvp->dvMaxX, lpvp->dvMaxY);
125 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
126 lpvp->dvMinZ, lpvp->dvMaxZ);
129 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
131 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
132 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
133 TRACE(" - dwWidth = %d dwHeight = %d\n",
134 lpvp->dwWidth, lpvp->dwHeight);
135 TRACE(" - dvClipX = %f dvClipY = %f\n",
136 lpvp->dvClipX, lpvp->dvClipY);
137 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
138 lpvp->dvClipWidth, lpvp->dvClipHeight);
139 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
140 lpvp->dvMinZ, lpvp->dvMaxZ);
143 static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
145 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
148 /*****************************************************************************
149 * IUnknown Methods.
150 *****************************************************************************/
152 /*****************************************************************************
153 * IDirect3DViewport3::QueryInterface
155 * A normal QueryInterface. Can query all interface versions and the
156 * IUnknown interface. The VTables of the different versions
157 * are equal
159 * Params:
160 * refiid: Interface id queried for
161 * obj: Address to write the interface pointer to
163 * Returns:
164 * S_OK on success.
165 * E_NOINTERFACE if the requested interface wasn't found
167 *****************************************************************************/
168 static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
170 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
172 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
173 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
174 || IsEqualGUID(&IID_IDirect3DViewport, riid)
175 || IsEqualGUID(&IID_IUnknown, riid))
177 IDirect3DViewport3_AddRef(iface);
178 *object = iface;
179 return S_OK;
182 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
184 *object = NULL;
185 return E_NOINTERFACE;
188 /*****************************************************************************
189 * IDirect3DViewport3::AddRef
191 * Increases the refcount.
193 * Returns:
194 * The new refcount
196 *****************************************************************************/
197 static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
199 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
200 ULONG ref = InterlockedIncrement(&viewport->ref);
202 TRACE("%p increasing refcount to %u.\n", viewport, ref);
204 return ref;
207 /*****************************************************************************
208 * IDirect3DViewport3::Release
210 * Reduces the refcount. If it falls to 0, the interface is released
212 * Returns:
213 * The new refcount
215 *****************************************************************************/
216 static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
218 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
219 ULONG ref = InterlockedDecrement(&viewport->ref);
221 TRACE("%p decreasing refcount to %u.\n", viewport, ref);
223 if (!ref)
224 HeapFree(GetProcessHeap(), 0, viewport);
226 return ref;
229 /*****************************************************************************
230 * IDirect3DViewport Methods.
231 *****************************************************************************/
233 /*****************************************************************************
234 * IDirect3DViewport3::Initialize
236 * No-op initialization.
238 * Params:
239 * Direct3D: The direct3D device this viewport is assigned to
241 * Returns:
242 * DDERR_ALREADYINITIALIZED
244 *****************************************************************************/
245 static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
247 TRACE("iface %p, d3d %p.\n", iface, d3d);
249 return DDERR_ALREADYINITIALIZED;
252 /*****************************************************************************
253 * IDirect3DViewport3::GetViewport
255 * Returns the viewport data assigned to this viewport interface
257 * Params:
258 * Data: Address to store the data
260 * Returns:
261 * D3D_OK on success
262 * DDERR_INVALIDPARAMS if Data is NULL
264 *****************************************************************************/
265 static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
267 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
268 DWORD dwSize;
270 TRACE("iface %p, data %p.\n", iface, lpData);
272 wined3d_mutex_lock();
274 dwSize = lpData->dwSize;
275 memset(lpData, 0, dwSize);
276 if (!This->use_vp2)
277 memcpy(lpData, &(This->viewports.vp1), dwSize);
278 else {
279 D3DVIEWPORT vp1;
280 vp1.dwSize = sizeof(vp1);
281 vp1.dwX = This->viewports.vp2.dwX;
282 vp1.dwY = This->viewports.vp2.dwY;
283 vp1.dwWidth = This->viewports.vp2.dwWidth;
284 vp1.dwHeight = This->viewports.vp2.dwHeight;
285 vp1.dvMaxX = 0.0;
286 vp1.dvMaxY = 0.0;
287 vp1.dvScaleX = 0.0;
288 vp1.dvScaleY = 0.0;
289 vp1.dvMinZ = This->viewports.vp2.dvMinZ;
290 vp1.dvMaxZ = This->viewports.vp2.dvMaxZ;
291 memcpy(lpData, &vp1, dwSize);
294 if (TRACE_ON(ddraw))
296 TRACE(" returning D3DVIEWPORT :\n");
297 _dump_D3DVIEWPORT(lpData);
300 wined3d_mutex_unlock();
302 return DD_OK;
305 /*****************************************************************************
306 * IDirect3DViewport3::SetViewport
308 * Sets the viewport information for this interface
310 * Params:
311 * lpData: Viewport to set
313 * Returns:
314 * D3D_OK on success
315 * DDERR_INVALIDPARAMS if Data is NULL
317 *****************************************************************************/
318 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
320 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
321 IDirect3DViewport3 *current_viewport;
323 TRACE("iface %p, data %p.\n", iface, lpData);
325 if (TRACE_ON(ddraw))
327 TRACE(" getting D3DVIEWPORT :\n");
328 _dump_D3DVIEWPORT(lpData);
331 wined3d_mutex_lock();
333 This->use_vp2 = 0;
334 memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
335 memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
337 /* Tests on two games show that these values are never used properly so override
338 them with proper ones :-)
340 This->viewports.vp1.dvMinZ = 0.0;
341 This->viewports.vp1.dvMaxZ = 1.0;
343 if (This->active_device)
345 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
346 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
348 if (current_viewport == iface) viewport_activate(This, FALSE);
349 IDirect3DViewport3_Release(current_viewport);
353 wined3d_mutex_unlock();
355 return DD_OK;
358 /*****************************************************************************
359 * IDirect3DViewport3::TransformVertices
361 * Transforms vertices by the transformation matrix.
363 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
364 * so it's tempting to forward it to there. However, there are some
365 * tiny differences. First, the lpOffscreen flag that is reported back,
366 * then there is the homogeneous vertex that is generated. Also there's a lack
367 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
368 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
369 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
370 * reused.
372 * Params:
373 * dwVertexCount: The number of vertices to be transformed
374 * lpData: Pointer to the vertex data
375 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
376 * lpOffScreen: Set to the clipping plane clipping the vertex, if only one
377 * vertex is transformed and clipping is on. 0 otherwise
379 * Returns:
380 * D3D_OK on success
381 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
382 * DDERR_INVALIDPARAMS if no clipping flag is specified
384 *****************************************************************************/
385 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
386 DWORD dwVertexCount, D3DTRANSFORMDATA *lpData, DWORD dwFlags, DWORD *lpOffScreen)
388 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
389 D3DVIEWPORT vp = viewport->viewports.vp1;
390 D3DMATRIX view_mat, world_mat, mat;
391 float *in;
392 float *out;
393 float x, y, z, w;
394 unsigned int i;
395 D3DHVERTEX *outH;
397 TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
398 iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
400 /* Tests on windows show that Windows crashes when this occurs,
401 * so don't return the (intuitive) return value
402 if (!viewport->active_device)
404 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
405 return D3DERR_VIEWPORTHASNODEVICE;
409 if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
411 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
412 return DDERR_INVALIDPARAMS;
416 wined3d_mutex_lock();
417 wined3d_device_get_transform(viewport->active_device->wined3d_device,
418 D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
419 wined3d_device_get_transform(viewport->active_device->wined3d_device,
420 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
421 multiply_matrix(&mat, &view_mat, &world_mat);
422 multiply_matrix(&mat, &viewport->active_device->legacy_projection, &mat);
424 in = lpData->lpIn;
425 out = lpData->lpOut;
426 outH = lpData->lpHOut;
427 for(i = 0; i < dwVertexCount; i++)
429 x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + mat._41;
430 y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + mat._42;
431 z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + mat._43;
432 w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + mat._44;
434 if(dwFlags & D3DTRANSFORM_CLIPPED)
436 /* If clipping is enabled, Windows assumes that outH is
437 * a valid pointer
439 outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
441 outH[i].dwFlags = 0;
442 if(x * vp.dvScaleX > ((float) vp.dwWidth * 0.5))
443 outH[i].dwFlags |= D3DCLIP_RIGHT;
444 if(x * vp.dvScaleX <= -((float) vp.dwWidth) * 0.5)
445 outH[i].dwFlags |= D3DCLIP_LEFT;
446 if(y * vp.dvScaleY > ((float) vp.dwHeight * 0.5))
447 outH[i].dwFlags |= D3DCLIP_TOP;
448 if(y * vp.dvScaleY <= -((float) vp.dwHeight) * 0.5)
449 outH[i].dwFlags |= D3DCLIP_BOTTOM;
450 if(z < 0.0)
451 outH[i].dwFlags |= D3DCLIP_FRONT;
452 if(z > 1.0)
453 outH[i].dwFlags |= D3DCLIP_BACK;
455 if(outH[i].dwFlags)
457 /* Looks like native just drops the vertex, leaves whatever data
458 * it has in the output buffer and goes on with the next vertex.
459 * The exact scheme hasn't been figured out yet, but windows
460 * definitely writes something there.
462 out[0] = x;
463 out[1] = y;
464 out[2] = z;
465 out[3] = w;
466 in = (float *) ((char *) in + lpData->dwInSize);
467 out = (float *) ((char *) out + lpData->dwOutSize);
468 continue;
472 w = 1 / w;
473 x *= w; y *= w; z *= w;
475 out[0] = vp.dwWidth / 2 + vp.dwX + x * vp.dvScaleX;
476 out[1] = vp.dwHeight / 2 + vp.dwY - y * vp.dvScaleY;
477 out[2] = z;
478 out[3] = w;
479 in = (float *) ((char *) in + lpData->dwInSize);
480 out = (float *) ((char *) out + lpData->dwOutSize);
483 /* According to the d3d test, the offscreen flag is set only
484 * if exactly one vertex is transformed. Its not documented,
485 * but the test shows that the lpOffscreen flag is set to the
486 * flag combination of clipping planes that clips the vertex.
488 * If clipping is requested, Windows assumes that the offscreen
489 * param is a valid pointer.
491 if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
493 *lpOffScreen = outH[0].dwFlags;
495 else if(*lpOffScreen)
497 *lpOffScreen = 0;
499 wined3d_mutex_unlock();
501 TRACE("All done\n");
502 return DD_OK;
505 /*****************************************************************************
506 * IDirect3DViewport3::LightElements
508 * The DirectX 5.0 sdk says that it's not implemented
510 * Params:
513 * Returns:
514 * DDERR_UNSUPPORTED
516 *****************************************************************************/
517 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
518 DWORD element_count, D3DLIGHTDATA *data)
520 TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
522 return DDERR_UNSUPPORTED;
525 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
527 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
528 struct d3d_material *m;
530 TRACE("iface %p, material %#x.\n", iface, material);
532 wined3d_mutex_lock();
534 if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
536 WARN("Invalid material handle %#x.\n", material);
537 wined3d_mutex_unlock();
538 return DDERR_INVALIDPARAMS;
541 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
542 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
543 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
544 viewport->background = m;
546 wined3d_mutex_unlock();
548 return D3D_OK;
551 /*****************************************************************************
552 * IDirect3DViewport3::GetBackground
554 * Returns the material handle assigned to the background of the viewport
556 * Params:
557 * lphMat: Address to store the handle
558 * lpValid: is set to FALSE if no background is set, TRUE if one is set
560 * Returns:
561 * D3D_OK
563 *****************************************************************************/
564 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
565 D3DMATERIALHANDLE *material, BOOL *valid)
567 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
569 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
571 wined3d_mutex_lock();
572 if (valid)
573 *valid = !!viewport->background;
574 if (material)
575 *material = viewport->background ? viewport->background->Handle : 0;
576 wined3d_mutex_unlock();
578 return D3D_OK;
581 /*****************************************************************************
582 * IDirect3DViewport3::SetBackgroundDepth
584 * Sets a surface that represents the background depth. It's contents are
585 * used to set the depth buffer in IDirect3DViewport3::Clear
587 * Params:
588 * lpDDSurface: Surface to set
590 * Returns: D3D_OK, because it's a stub
592 *****************************************************************************/
593 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
595 FIXME("iface %p, surface %p stub!\n", iface, surface);
597 return D3D_OK;
600 /*****************************************************************************
601 * IDirect3DViewport3::GetBackgroundDepth
603 * Returns the surface that represents the depth field
605 * Params:
606 * lplpDDSurface: Address to store the interface pointer
607 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
609 * Returns:
610 * D3D_OK, because it's a stub
611 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
613 *****************************************************************************/
614 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
615 IDirectDrawSurface **surface, BOOL *valid)
617 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
619 return DD_OK;
622 /*****************************************************************************
623 * IDirect3DViewport3::Clear
625 * Clears the render target and / or the z buffer
627 * Params:
628 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
629 * cleared
630 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
631 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
633 * Returns:
634 * D3D_OK on success
635 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
636 * The return value of IDirect3DDevice7::Clear
638 *****************************************************************************/
639 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
640 DWORD rect_count, D3DRECT *rects, DWORD flags)
642 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
643 DWORD color = 0x00000000;
644 HRESULT hr;
645 IDirect3DViewport3 *current_viewport;
646 IDirect3DDevice3 *d3d_device3;
648 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
650 if (!rects || !rect_count)
652 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
653 return D3D_OK;
656 if (This->active_device == NULL) {
657 ERR(" Trying to clear a viewport not attached to a device!\n");
658 return D3DERR_VIEWPORTHASNODEVICE;
660 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
662 wined3d_mutex_lock();
664 if (flags & D3DCLEAR_TARGET)
666 if (!This->background)
667 WARN("No background material set.\n");
668 else
669 color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
670 This->background->mat.u.diffuse.u2.g,
671 This->background->mat.u.diffuse.u3.b,
672 This->background->mat.u.diffuse.u4.a);
675 /* Need to temporarily activate the viewport to clear it. The previously
676 * active one will be restored afterwards. */
677 viewport_activate(This, TRUE);
679 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
680 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
682 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
684 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
685 viewport_activate(vp, TRUE);
686 IDirect3DViewport3_Release(current_viewport);
689 wined3d_mutex_unlock();
691 return hr;
694 /*****************************************************************************
695 * IDirect3DViewport3::AddLight
697 * Adds an light to the viewport
699 * Params:
700 * lpDirect3DLight: Interface of the light to add
702 * Returns:
703 * D3D_OK on success
704 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
705 * DDERR_INVALIDPARAMS if there are 8 lights or more
707 *****************************************************************************/
708 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
710 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
711 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
712 DWORD i = 0;
713 DWORD map = This->map_lights;
715 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
717 wined3d_mutex_lock();
719 if (This->num_lights >= 8)
721 wined3d_mutex_unlock();
722 return DDERR_INVALIDPARAMS;
725 /* Find a light number and update both light and viewports objects accordingly */
726 while (map & 1)
728 map >>= 1;
729 ++i;
731 light_impl->dwLightIndex = i;
732 This->num_lights++;
733 This->map_lights |= 1<<i;
735 /* Add the light in the 'linked' chain */
736 list_add_head(&This->light_list, &light_impl->entry);
737 IDirect3DLight_AddRef(lpDirect3DLight);
739 /* Attach the light to the viewport */
740 light_impl->active_viewport = This;
742 /* If active, activate the light */
743 if (This->active_device && light_impl->light.dwFlags & D3DLIGHT_ACTIVE)
745 /* Disable the flag so that light_activate actually does its job. */
746 light_impl->light.dwFlags &= ~D3DLIGHT_ACTIVE;
747 light_activate(light_impl);
750 wined3d_mutex_unlock();
752 return D3D_OK;
755 /*****************************************************************************
756 * IDirect3DViewport3::DeleteLight
758 * Deletes a light from the viewports' light list
760 * Params:
761 * lpDirect3DLight: Light to delete
763 * Returns:
764 * D3D_OK on success
765 * DDERR_INVALIDPARAMS if the light wasn't found
767 *****************************************************************************/
768 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
770 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
771 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
773 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
775 wined3d_mutex_lock();
777 if (l->active_viewport != viewport)
779 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
780 wined3d_mutex_unlock();
781 return DDERR_INVALIDPARAMS;
784 light_deactivate(l);
785 list_remove(&l->entry);
786 l->active_viewport = NULL;
787 IDirect3DLight_Release(lpDirect3DLight);
788 --viewport->num_lights;
789 viewport->map_lights &= ~(1 << l->dwLightIndex);
791 wined3d_mutex_unlock();
793 return D3D_OK;
796 /*****************************************************************************
797 * IDirect3DViewport::NextLight
799 * Enumerates the lights associated with the viewport
801 * Params:
802 * lpDirect3DLight: Light to start with
803 * lplpDirect3DLight: Address to store the successor to
805 * Returns:
806 * D3D_OK, because it's a stub
808 *****************************************************************************/
809 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
810 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
812 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
813 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
814 struct list *entry;
815 HRESULT hr;
817 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
818 iface, lpDirect3DLight, lplpDirect3DLight, flags);
820 if (!lplpDirect3DLight)
821 return DDERR_INVALIDPARAMS;
823 wined3d_mutex_lock();
825 switch (flags)
827 case D3DNEXT_NEXT:
828 if (!l || l->active_viewport != viewport)
830 if (l)
831 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
832 entry = NULL;
834 else
835 entry = list_next(&viewport->light_list, &l->entry);
836 break;
838 case D3DNEXT_HEAD:
839 entry = list_head(&viewport->light_list);
840 break;
842 case D3DNEXT_TAIL:
843 entry = list_tail(&viewport->light_list);
844 break;
846 default:
847 entry = NULL;
848 WARN("Invalid flags %#x.\n", flags);
849 break;
852 if (entry)
854 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
855 IDirect3DLight_AddRef(*lplpDirect3DLight);
856 hr = D3D_OK;
858 else
860 *lplpDirect3DLight = NULL;
861 hr = DDERR_INVALIDPARAMS;
864 wined3d_mutex_unlock();
866 return hr;
869 /*****************************************************************************
870 * IDirect3DViewport2 Methods.
871 *****************************************************************************/
873 /*****************************************************************************
874 * IDirect3DViewport3::GetViewport2
876 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
877 * Similar to IDirect3DViewport3::GetViewport
879 * Params:
880 * lpData: Pointer to the structure to fill
882 * Returns:
883 * D3D_OK on success
884 * DDERR_INVALIDPARAMS if the viewport was set with
885 * IDirect3DViewport3::SetViewport
886 * DDERR_INVALIDPARAMS if Data is NULL
888 *****************************************************************************/
889 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
891 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
892 DWORD dwSize;
894 TRACE("iface %p, data %p.\n", iface, lpData);
896 wined3d_mutex_lock();
897 dwSize = lpData->dwSize;
898 memset(lpData, 0, dwSize);
899 if (This->use_vp2)
900 memcpy(lpData, &(This->viewports.vp2), dwSize);
901 else {
902 D3DVIEWPORT2 vp2;
903 vp2.dwSize = sizeof(vp2);
904 vp2.dwX = This->viewports.vp1.dwX;
905 vp2.dwY = This->viewports.vp1.dwY;
906 vp2.dwWidth = This->viewports.vp1.dwWidth;
907 vp2.dwHeight = This->viewports.vp1.dwHeight;
908 vp2.dvClipX = 0.0;
909 vp2.dvClipY = 0.0;
910 vp2.dvClipWidth = 0.0;
911 vp2.dvClipHeight = 0.0;
912 vp2.dvMinZ = This->viewports.vp1.dvMinZ;
913 vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
914 memcpy(lpData, &vp2, dwSize);
917 if (TRACE_ON(ddraw))
919 TRACE(" returning D3DVIEWPORT2 :\n");
920 _dump_D3DVIEWPORT2(lpData);
923 wined3d_mutex_unlock();
925 return D3D_OK;
928 /*****************************************************************************
929 * IDirect3DViewport3::SetViewport2
931 * Sets the viewport from a D3DVIEWPORT2 structure
933 * Params:
934 * lpData: Viewport to set
936 * Returns:
937 * D3D_OK on success
939 *****************************************************************************/
940 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
942 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
943 IDirect3DViewport3 *current_viewport;
945 TRACE("iface %p, data %p.\n", iface, lpData);
947 if (TRACE_ON(ddraw))
949 TRACE(" getting D3DVIEWPORT2 :\n");
950 _dump_D3DVIEWPORT2(lpData);
953 wined3d_mutex_lock();
955 This->use_vp2 = 1;
956 memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
957 memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
959 if (This->active_device)
961 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
962 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
964 if (current_viewport == iface) viewport_activate(This, FALSE);
965 IDirect3DViewport3_Release(current_viewport);
969 wined3d_mutex_unlock();
971 return D3D_OK;
974 /*****************************************************************************
975 * IDirect3DViewport3 Methods.
976 *****************************************************************************/
978 /*****************************************************************************
979 * IDirect3DViewport3::SetBackgroundDepth2
981 * Sets a IDirectDrawSurface4 surface as the background depth surface
983 * Params:
984 * lpDDS: Surface to set
986 * Returns:
987 * D3D_OK, because it's stub
989 *****************************************************************************/
990 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
991 IDirectDrawSurface4 *surface)
993 FIXME("iface %p, surface %p stub!\n", iface, surface);
995 return D3D_OK;
998 /*****************************************************************************
999 * IDirect3DViewport3::GetBackgroundDepth2
1001 * Returns the IDirect3DSurface4 interface to the background depth surface
1003 * Params:
1004 * lplpDDS: Address to store the interface pointer at
1005 * lpValid: Set to true if a surface is assigned
1007 * Returns:
1008 * D3D_OK because it's a stub
1010 *****************************************************************************/
1011 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1012 IDirectDrawSurface4 **surface, BOOL *valid)
1014 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1016 return D3D_OK;
1019 /*****************************************************************************
1020 * IDirect3DViewport3::Clear2
1022 * Another clearing method
1024 * Params:
1025 * Count: Number of rectangles to clear
1026 * Rects: Rectangle array to clear
1027 * Flags: Some flags :)
1028 * Color: Color to fill the render target with
1029 * Z: Value to fill the depth buffer with
1030 * Stencil: Value to fill the stencil bits with
1032 * Returns:
1034 *****************************************************************************/
1035 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1036 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1038 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1039 HRESULT hr;
1040 IDirect3DViewport3 *current_viewport;
1041 IDirect3DDevice3 *d3d_device3;
1043 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1044 iface, rect_count, rects, flags, color, depth, stencil);
1046 if (!rects || !rect_count)
1048 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
1049 return D3D_OK;
1052 wined3d_mutex_lock();
1054 if (!viewport->active_device)
1056 WARN("Trying to clear a viewport not attached to a device.\n");
1057 wined3d_mutex_unlock();
1058 return D3DERR_VIEWPORTHASNODEVICE;
1060 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1061 /* Need to temporarily activate viewport to clear it. Previously active
1062 * one will be restored afterwards. */
1063 viewport_activate(viewport, TRUE);
1065 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1066 rect_count, rects, flags, color, depth, stencil);
1067 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1069 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1070 viewport_activate(vp, TRUE);
1071 IDirect3DViewport3_Release(current_viewport);
1074 wined3d_mutex_unlock();
1076 return hr;
1079 /*****************************************************************************
1080 * The VTable
1081 *****************************************************************************/
1083 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1085 /*** IUnknown Methods ***/
1086 d3d_viewport_QueryInterface,
1087 d3d_viewport_AddRef,
1088 d3d_viewport_Release,
1089 /*** IDirect3DViewport Methods */
1090 d3d_viewport_Initialize,
1091 d3d_viewport_GetViewport,
1092 d3d_viewport_SetViewport,
1093 d3d_viewport_TransformVertices,
1094 d3d_viewport_LightElements,
1095 d3d_viewport_SetBackground,
1096 d3d_viewport_GetBackground,
1097 d3d_viewport_SetBackgroundDepth,
1098 d3d_viewport_GetBackgroundDepth,
1099 d3d_viewport_Clear,
1100 d3d_viewport_AddLight,
1101 d3d_viewport_DeleteLight,
1102 d3d_viewport_NextLight,
1103 /*** IDirect3DViewport2 Methods ***/
1104 d3d_viewport_GetViewport2,
1105 d3d_viewport_SetViewport2,
1106 /*** IDirect3DViewport3 Methods ***/
1107 d3d_viewport_SetBackgroundDepth2,
1108 d3d_viewport_GetBackgroundDepth2,
1109 d3d_viewport_Clear2,
1112 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1114 if (!iface) return NULL;
1115 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1116 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1119 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1121 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1122 if (!iface) return NULL;
1123 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1124 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1127 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1129 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1130 if (!iface) return NULL;
1131 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1132 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1135 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1137 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1138 viewport->ref = 1;
1139 viewport->ddraw = ddraw;
1140 viewport->use_vp2 = 0xff;
1141 list_init(&viewport->light_list);