Revert "libwine: Move string functions to libwine_port."
[wine.git] / dlls / ddraw / viewport.c
blobe7e44635114ff0a89c119c842208b3069d50c04e
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 if (This->version == DDRAW_VIEWPORT_VERSION_NONE)
73 TRACE("Viewport data was not set.\n");
74 return;
77 /* And copy the values in the structure used by the device */
78 if (This->version == DDRAW_VIEWPORT_VERSION_2)
80 vp.dwX = This->viewports.vp2.dwX;
81 vp.dwY = This->viewports.vp2.dwY;
82 vp.dwHeight = This->viewports.vp2.dwHeight;
83 vp.dwWidth = This->viewports.vp2.dwWidth;
84 vp.dvMinZ = 0.0f;
85 vp.dvMaxZ = 1.0f;
87 scale.x = 2.0f / This->viewports.vp2.dvClipWidth;
88 scale.y = 2.0f / This->viewports.vp2.dvClipHeight;
89 scale.z = 1.0f / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
90 offset.x = -2.0f * This->viewports.vp2.dvClipX / This->viewports.vp2.dvClipWidth - 1.0f;
91 offset.y = -2.0f * This->viewports.vp2.dvClipY / This->viewports.vp2.dvClipHeight + 1.0f;
92 offset.z = -This->viewports.vp2.dvMinZ / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
94 else
96 vp.dwX = This->viewports.vp1.dwX;
97 vp.dwY = This->viewports.vp1.dwY;
98 vp.dwHeight = This->viewports.vp1.dwHeight;
99 vp.dwWidth = This->viewports.vp1.dwWidth;
100 vp.dvMinZ = 0.0f;
101 vp.dvMaxZ = 1.0f;
103 scale.x = 2.0f * This->viewports.vp1.dvScaleX / This->viewports.vp1.dwWidth;
104 scale.y = 2.0f * This->viewports.vp1.dvScaleY / This->viewports.vp1.dwHeight;
105 scale.z = 1.0f;
106 offset.x = 0.0f;
107 offset.y = 0.0f;
108 offset.z = 0.0f;
111 update_clip_space(This->active_device, &scale, &offset);
112 IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
115 /*****************************************************************************
116 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
118 * Writes viewport information to TRACE
120 *****************************************************************************/
121 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
123 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
124 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
125 TRACE(" - dwWidth = %d dwHeight = %d\n",
126 lpvp->dwWidth, lpvp->dwHeight);
127 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
128 lpvp->dvScaleX, lpvp->dvScaleY);
129 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
130 lpvp->dvMaxX, lpvp->dvMaxY);
131 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
132 lpvp->dvMinZ, lpvp->dvMaxZ);
135 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
137 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
138 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
139 TRACE(" - dwWidth = %d dwHeight = %d\n",
140 lpvp->dwWidth, lpvp->dwHeight);
141 TRACE(" - dvClipX = %f dvClipY = %f\n",
142 lpvp->dvClipX, lpvp->dvClipY);
143 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
144 lpvp->dvClipWidth, lpvp->dvClipHeight);
145 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
146 lpvp->dvMinZ, lpvp->dvMaxZ);
149 static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
151 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
154 /*****************************************************************************
155 * IUnknown Methods.
156 *****************************************************************************/
158 /*****************************************************************************
159 * IDirect3DViewport3::QueryInterface
161 * A normal QueryInterface. Can query all interface versions and the
162 * IUnknown interface. The VTables of the different versions
163 * are equal
165 * Params:
166 * refiid: Interface id queried for
167 * obj: Address to write the interface pointer to
169 * Returns:
170 * S_OK on success.
171 * E_NOINTERFACE if the requested interface wasn't found
173 *****************************************************************************/
174 static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
176 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
178 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
179 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
180 || IsEqualGUID(&IID_IDirect3DViewport, riid)
181 || IsEqualGUID(&IID_IUnknown, riid))
183 IDirect3DViewport3_AddRef(iface);
184 *object = iface;
185 return S_OK;
188 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
190 *object = NULL;
191 return E_NOINTERFACE;
194 /*****************************************************************************
195 * IDirect3DViewport3::AddRef
197 * Increases the refcount.
199 * Returns:
200 * The new refcount
202 *****************************************************************************/
203 static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
205 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
206 ULONG ref = InterlockedIncrement(&viewport->ref);
208 TRACE("%p increasing refcount to %u.\n", viewport, ref);
210 return ref;
213 /*****************************************************************************
214 * IDirect3DViewport3::Release
216 * Reduces the refcount. If it falls to 0, the interface is released
218 * Returns:
219 * The new refcount
221 *****************************************************************************/
222 static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
224 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
225 ULONG ref = InterlockedDecrement(&viewport->ref);
227 TRACE("%p decreasing refcount to %u.\n", viewport, ref);
229 if (!ref)
230 heap_free(viewport);
232 return ref;
235 /*****************************************************************************
236 * IDirect3DViewport Methods.
237 *****************************************************************************/
239 /*****************************************************************************
240 * IDirect3DViewport3::Initialize
242 * No-op initialization.
244 * Params:
245 * Direct3D: The direct3D device this viewport is assigned to
247 * Returns:
248 * DDERR_ALREADYINITIALIZED
250 *****************************************************************************/
251 static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
253 TRACE("iface %p, d3d %p.\n", iface, d3d);
255 return DDERR_ALREADYINITIALIZED;
258 static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *vp)
260 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
261 DWORD size;
263 TRACE("iface %p, vp %p.\n", iface, vp);
265 if (!vp)
266 return DDERR_INVALIDPARAMS;
268 if (viewport->version == DDRAW_VIEWPORT_VERSION_NONE)
270 WARN("Viewport data was not set.\n");
271 return D3DERR_VIEWPORTDATANOTSET;
274 wined3d_mutex_lock();
276 size = vp->dwSize;
277 if (viewport->version == DDRAW_VIEWPORT_VERSION_1)
279 memcpy(vp, &viewport->viewports.vp1, size);
281 else
283 D3DVIEWPORT vp1;
285 vp1.dwSize = sizeof(vp1);
286 vp1.dwX = viewport->viewports.vp2.dwX;
287 vp1.dwY = viewport->viewports.vp2.dwY;
288 vp1.dwWidth = viewport->viewports.vp2.dwWidth;
289 vp1.dwHeight = viewport->viewports.vp2.dwHeight;
290 vp1.dvMaxX = 0.0f;
291 vp1.dvMaxY = 0.0f;
292 vp1.dvScaleX = 0.0f;
293 vp1.dvScaleY = 0.0f;
294 vp1.dvMinZ = viewport->viewports.vp2.dvMinZ;
295 vp1.dvMaxZ = viewport->viewports.vp2.dvMaxZ;
296 memcpy(vp, &vp1, size);
299 if (TRACE_ON(ddraw))
301 TRACE(" returning D3DVIEWPORT :\n");
302 _dump_D3DVIEWPORT(vp);
305 wined3d_mutex_unlock();
307 return D3D_OK;
310 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *vp)
312 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
313 struct d3d_device *device = viewport->active_device;
314 struct wined3d_sub_resource_desc rt_desc;
315 struct wined3d_rendertarget_view *rtv;
316 IDirect3DViewport3 *current_viewport;
317 struct ddraw_surface *surface;
319 TRACE("iface %p, vp %p.\n", iface, vp);
321 if (!vp)
322 return DDERR_INVALIDPARAMS;
324 if (vp->dwSize != sizeof(*vp))
326 WARN("Invalid D3DVIEWPORT size %u.\n", vp->dwSize);
327 return DDERR_INVALIDPARAMS;
330 if (TRACE_ON(ddraw))
332 TRACE(" getting D3DVIEWPORT :\n");
333 _dump_D3DVIEWPORT(vp);
336 if (!device)
338 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
339 return D3DERR_VIEWPORTHASNODEVICE;
342 wined3d_mutex_lock();
344 if (device->version > 1)
346 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
348 wined3d_mutex_unlock();
349 return DDERR_INVALIDCAPS;
351 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
352 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
354 if (vp->dwX > rt_desc.width || vp->dwWidth > rt_desc.width - vp->dwX
355 || vp->dwY > rt_desc.height || vp->dwHeight > rt_desc.height - vp->dwY)
357 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
358 wined3d_mutex_unlock();
359 return DDERR_INVALIDPARAMS;
363 viewport->version = DDRAW_VIEWPORT_VERSION_1;
364 viewport->viewports.vp1 = *vp;
366 /* Empirical testing on a couple of d3d1 games showed that these values
367 * should be ignored. */
368 viewport->viewports.vp1.dvMinZ = 0.0f;
369 viewport->viewports.vp1.dvMaxZ = 1.0f;
371 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device->IDirect3DDevice3_iface, &current_viewport)))
373 if (current_viewport == iface)
374 viewport_activate(viewport, FALSE);
375 IDirect3DViewport3_Release(current_viewport);
378 wined3d_mutex_unlock();
380 return D3D_OK;
383 /*****************************************************************************
384 * IDirect3DViewport3::TransformVertices
386 * Transforms vertices by the transformation matrix.
388 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
389 * so it's tempting to forward it to there. However, there are some
390 * tiny differences. First, the lpOffscreen flag that is reported back,
391 * then there is the homogeneous vertex that is generated. Also there's a lack
392 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
393 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
394 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
395 * reused.
397 * Params:
398 * dwVertexCount: The number of vertices to be transformed
399 * data: Pointer to the vertex input / output data.
400 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
401 * offscreen: Logical AND of the planes that clipped the vertices if clipping
402 * is on. 0 if clipping is off.
404 * Returns:
405 * D3D_OK on success
406 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
407 * DDERR_INVALIDPARAMS if no clipping flag is specified
409 *****************************************************************************/
410 struct transform_vertices_vertex
412 float x, y, z, w; /* w is unused in input data. */
413 struct
415 DWORD p[4];
416 } payload;
419 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
420 DWORD dwVertexCount, D3DTRANSFORMDATA *data, DWORD dwFlags, DWORD *offscreen)
422 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
423 D3DVIEWPORT vp = viewport->viewports.vp1;
424 D3DMATRIX view_mat, world_mat, proj_mat, mat;
425 struct transform_vertices_vertex *in, *out;
426 float x, y, z, w;
427 unsigned int i;
428 D3DHVERTEX *outH;
429 struct d3d_device *device = viewport->active_device;
430 BOOL activate = device->current_viewport != viewport;
432 TRACE("iface %p, vertex_count %u, data %p, flags %#x, offscreen %p.\n",
433 iface, dwVertexCount, data, dwFlags, offscreen);
435 /* Tests on windows show that Windows crashes when this occurs,
436 * so don't return the (intuitive) return value
437 if (!device)
439 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
440 return D3DERR_VIEWPORTHASNODEVICE;
444 if (!data || data->dwSize != sizeof(*data))
446 WARN("Transform data is NULL or size is incorrect, returning DDERR_INVALIDPARAMS\n");
447 return DDERR_INVALIDPARAMS;
449 if (!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
451 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
452 return DDERR_INVALIDPARAMS;
455 wined3d_mutex_lock();
456 if (activate)
457 viewport_activate(viewport, TRUE);
459 wined3d_device_get_transform(device->wined3d_device,
460 D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
461 wined3d_device_get_transform(device->wined3d_device,
462 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
463 wined3d_device_get_transform(device->wined3d_device,
464 WINED3D_TS_PROJECTION, (struct wined3d_matrix *)&proj_mat);
465 multiply_matrix(&mat, &view_mat, &world_mat);
466 multiply_matrix(&mat, &proj_mat, &mat);
468 /* The pointer is not tested against NULL on Windows. */
469 if (dwFlags & D3DTRANSFORM_CLIPPED)
470 *offscreen = ~0U;
471 else
472 *offscreen = 0;
474 outH = data->lpHOut;
475 for(i = 0; i < dwVertexCount; i++)
477 in = (struct transform_vertices_vertex *)((char *)data->lpIn + data->dwInSize * i);
478 out = (struct transform_vertices_vertex *)((char *)data->lpOut + data->dwOutSize * i);
480 x = (in->x * mat._11) + (in->y * mat._21) + (in->z * mat._31) + mat._41;
481 y = (in->x * mat._12) + (in->y * mat._22) + (in->z * mat._32) + mat._42;
482 z = (in->x * mat._13) + (in->y * mat._23) + (in->z * mat._33) + mat._43;
483 w = (in->x * mat._14) + (in->y * mat._24) + (in->z * mat._34) + mat._44;
485 if(dwFlags & D3DTRANSFORM_CLIPPED)
487 /* If clipping is enabled, Windows assumes that outH is
488 * a valid pointer. */
489 outH[i].u1.hx = (x - device->legacy_clipspace._41 * w) / device->legacy_clipspace._11;
490 outH[i].u2.hy = (y - device->legacy_clipspace._42 * w) / device->legacy_clipspace._22;
491 outH[i].u3.hz = (z - device->legacy_clipspace._43 * w) / device->legacy_clipspace._33;
493 outH[i].dwFlags = 0;
494 if (x > w)
495 outH[i].dwFlags |= D3DCLIP_RIGHT;
496 if (x < -w)
497 outH[i].dwFlags |= D3DCLIP_LEFT;
498 if (y > w)
499 outH[i].dwFlags |= D3DCLIP_TOP;
500 if (y < -w)
501 outH[i].dwFlags |= D3DCLIP_BOTTOM;
502 if (z < 0.0f)
503 outH[i].dwFlags |= D3DCLIP_FRONT;
504 if (z > w)
505 outH[i].dwFlags |= D3DCLIP_BACK;
507 *offscreen &= outH[i].dwFlags;
509 if(outH[i].dwFlags)
511 /* Looks like native just drops the vertex, leaves whatever data
512 * it has in the output buffer and goes on with the next vertex.
513 * The exact scheme hasn't been figured out yet, but windows
514 * definitely writes something there.
516 out->x = x;
517 out->y = y;
518 out->z = z;
519 out->w = w;
520 continue;
524 w = 1 / w;
525 x *= w; y *= w; z *= w;
527 out->x = (x + 1.0f) * vp.dwWidth * 0.5 + vp.dwX;
528 out->y = (-y + 1.0f) * vp.dwHeight * 0.5 + vp.dwY;
529 out->z = z;
530 out->w = w;
531 out->payload = in->payload;
534 if (activate && device->current_viewport)
535 viewport_activate(device->current_viewport, TRUE);
537 wined3d_mutex_unlock();
539 TRACE("All done\n");
540 return DD_OK;
543 /*****************************************************************************
544 * IDirect3DViewport3::LightElements
546 * The DirectX 5.0 sdk says that it's not implemented
548 * Params:
551 * Returns:
552 * DDERR_UNSUPPORTED
554 *****************************************************************************/
555 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
556 DWORD element_count, D3DLIGHTDATA *data)
558 TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
560 return DDERR_UNSUPPORTED;
563 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
565 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
566 struct d3d_material *m;
568 TRACE("iface %p, material %#x.\n", iface, material);
570 wined3d_mutex_lock();
572 if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
574 WARN("Invalid material handle %#x.\n", material);
575 wined3d_mutex_unlock();
576 return DDERR_INVALIDPARAMS;
579 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
580 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
581 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
582 viewport->background = m;
584 wined3d_mutex_unlock();
586 return D3D_OK;
589 /*****************************************************************************
590 * IDirect3DViewport3::GetBackground
592 * Returns the material handle assigned to the background of the viewport
594 * Params:
595 * lphMat: Address to store the handle
596 * lpValid: is set to FALSE if no background is set, TRUE if one is set
598 * Returns:
599 * D3D_OK
601 *****************************************************************************/
602 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
603 D3DMATERIALHANDLE *material, BOOL *valid)
605 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
607 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
609 wined3d_mutex_lock();
610 if (valid)
611 *valid = !!viewport->background;
612 if (material)
613 *material = viewport->background ? viewport->background->Handle : 0;
614 wined3d_mutex_unlock();
616 return D3D_OK;
619 /*****************************************************************************
620 * IDirect3DViewport3::SetBackgroundDepth
622 * Sets a surface that represents the background depth. Its contents are
623 * used to set the depth buffer in IDirect3DViewport3::Clear
625 * Params:
626 * lpDDSurface: Surface to set
628 * Returns: D3D_OK, because it's a stub
630 *****************************************************************************/
631 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
633 FIXME("iface %p, surface %p stub!\n", iface, surface);
635 return D3D_OK;
638 /*****************************************************************************
639 * IDirect3DViewport3::GetBackgroundDepth
641 * Returns the surface that represents the depth field
643 * Params:
644 * lplpDDSurface: Address to store the interface pointer
645 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
647 * Returns:
648 * D3D_OK, because it's a stub
649 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
651 *****************************************************************************/
652 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
653 IDirectDrawSurface **surface, BOOL *valid)
655 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
657 return DD_OK;
660 /*****************************************************************************
661 * IDirect3DViewport3::Clear
663 * Clears the render target and / or the z buffer
665 * Params:
666 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
667 * cleared
668 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
669 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
671 * Returns:
672 * D3D_OK on success
673 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
674 * The return value of IDirect3DDevice7::Clear
676 *****************************************************************************/
677 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
678 DWORD rect_count, D3DRECT *rects, DWORD flags)
680 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
681 DWORD color = 0x00000000;
682 HRESULT hr;
683 IDirect3DViewport3 *current_viewport;
684 IDirect3DDevice3 *d3d_device3;
686 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
688 if (!rects || !rect_count)
690 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
691 return D3D_OK;
694 if (This->active_device == NULL) {
695 ERR(" Trying to clear a viewport not attached to a device!\n");
696 return D3DERR_VIEWPORTHASNODEVICE;
698 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
700 wined3d_mutex_lock();
702 if (flags & D3DCLEAR_TARGET)
704 if (!This->background)
705 WARN("No background material set.\n");
706 else
707 color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
708 This->background->mat.u.diffuse.u2.g,
709 This->background->mat.u.diffuse.u3.b,
710 This->background->mat.u.diffuse.u4.a);
713 /* Need to temporarily activate the viewport to clear it. The previously
714 * active one will be restored afterwards. */
715 viewport_activate(This, TRUE);
717 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
718 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
720 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
722 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
723 viewport_activate(vp, TRUE);
724 IDirect3DViewport3_Release(current_viewport);
727 wined3d_mutex_unlock();
729 return hr;
732 /*****************************************************************************
733 * IDirect3DViewport3::AddLight
735 * Adds an light to the viewport
737 * Params:
738 * lpDirect3DLight: Interface of the light to add
740 * Returns:
741 * D3D_OK on success
742 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
743 * DDERR_INVALIDPARAMS if there are 8 lights or more
745 *****************************************************************************/
746 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
748 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
749 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
750 DWORD i = 0;
751 DWORD map = This->map_lights;
753 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
755 wined3d_mutex_lock();
757 if (This->num_lights >= 8)
759 wined3d_mutex_unlock();
760 return DDERR_INVALIDPARAMS;
763 if (light_impl->active_viewport)
765 wined3d_mutex_unlock();
766 WARN("Light %p is active in viewport %p.\n", light_impl, light_impl->active_viewport);
767 return D3DERR_LIGHTHASVIEWPORT;
770 /* Find a light number and update both light and viewports objects accordingly */
771 while (map & 1)
773 map >>= 1;
774 ++i;
776 light_impl->dwLightIndex = i;
777 This->num_lights++;
778 This->map_lights |= 1<<i;
780 /* Add the light in the 'linked' chain */
781 list_add_head(&This->light_list, &light_impl->entry);
782 IDirect3DLight_AddRef(lpDirect3DLight);
784 /* Attach the light to the viewport */
785 light_impl->active_viewport = This;
787 /* If active, activate the light */
788 if (This->active_device && light_impl->light.dwFlags & D3DLIGHT_ACTIVE)
790 /* Disable the flag so that light_activate actually does its job. */
791 light_impl->light.dwFlags &= ~D3DLIGHT_ACTIVE;
792 light_activate(light_impl);
795 wined3d_mutex_unlock();
797 return D3D_OK;
800 /*****************************************************************************
801 * IDirect3DViewport3::DeleteLight
803 * Deletes a light from the viewports' light list
805 * Params:
806 * lpDirect3DLight: Light to delete
808 * Returns:
809 * D3D_OK on success
810 * DDERR_INVALIDPARAMS if the light wasn't found
812 *****************************************************************************/
813 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
815 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
816 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
818 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
820 wined3d_mutex_lock();
822 if (l->active_viewport != viewport)
824 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
825 wined3d_mutex_unlock();
826 return DDERR_INVALIDPARAMS;
829 light_deactivate(l);
830 list_remove(&l->entry);
831 l->active_viewport = NULL;
832 IDirect3DLight_Release(lpDirect3DLight);
833 --viewport->num_lights;
834 viewport->map_lights &= ~(1 << l->dwLightIndex);
836 wined3d_mutex_unlock();
838 return D3D_OK;
841 /*****************************************************************************
842 * IDirect3DViewport::NextLight
844 * Enumerates the lights associated with the viewport
846 * Params:
847 * lpDirect3DLight: Light to start with
848 * lplpDirect3DLight: Address to store the successor to
850 * Returns:
851 * D3D_OK, because it's a stub
853 *****************************************************************************/
854 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
855 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
857 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
858 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
859 struct list *entry;
860 HRESULT hr;
862 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
863 iface, lpDirect3DLight, lplpDirect3DLight, flags);
865 if (!lplpDirect3DLight)
866 return DDERR_INVALIDPARAMS;
868 wined3d_mutex_lock();
870 switch (flags)
872 case D3DNEXT_NEXT:
873 if (!l || l->active_viewport != viewport)
875 if (l)
876 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
877 entry = NULL;
879 else
880 entry = list_next(&viewport->light_list, &l->entry);
881 break;
883 case D3DNEXT_HEAD:
884 entry = list_head(&viewport->light_list);
885 break;
887 case D3DNEXT_TAIL:
888 entry = list_tail(&viewport->light_list);
889 break;
891 default:
892 entry = NULL;
893 WARN("Invalid flags %#x.\n", flags);
894 break;
897 if (entry)
899 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
900 IDirect3DLight_AddRef(*lplpDirect3DLight);
901 hr = D3D_OK;
903 else
905 *lplpDirect3DLight = NULL;
906 hr = DDERR_INVALIDPARAMS;
909 wined3d_mutex_unlock();
911 return hr;
914 /*****************************************************************************
915 * IDirect3DViewport2 Methods.
916 *****************************************************************************/
918 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *vp)
920 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
921 DWORD size;
923 TRACE("iface %p, vp %p.\n", iface, vp);
925 if (!vp)
926 return DDERR_INVALIDPARAMS;
928 if (viewport->version == DDRAW_VIEWPORT_VERSION_NONE)
930 WARN("Viewport data was not set.\n");
931 return D3DERR_VIEWPORTDATANOTSET;
934 wined3d_mutex_lock();
935 size = vp->dwSize;
936 if (viewport->version == DDRAW_VIEWPORT_VERSION_2)
938 memcpy(vp, &viewport->viewports.vp2, size);
940 else
942 D3DVIEWPORT2 vp2;
944 vp2.dwSize = sizeof(vp2);
945 vp2.dwX = viewport->viewports.vp1.dwX;
946 vp2.dwY = viewport->viewports.vp1.dwY;
947 vp2.dwWidth = viewport->viewports.vp1.dwWidth;
948 vp2.dwHeight = viewport->viewports.vp1.dwHeight;
949 vp2.dvClipX = 0.0f;
950 vp2.dvClipY = 0.0f;
951 vp2.dvClipWidth = 0.0f;
952 vp2.dvClipHeight = 0.0f;
953 vp2.dvMinZ = viewport->viewports.vp1.dvMinZ;
954 vp2.dvMaxZ = viewport->viewports.vp1.dvMaxZ;
955 memcpy(vp, &vp2, size);
958 if (TRACE_ON(ddraw))
960 TRACE(" returning D3DVIEWPORT2 :\n");
961 _dump_D3DVIEWPORT2(vp);
964 wined3d_mutex_unlock();
966 return D3D_OK;
969 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *vp)
971 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
972 struct d3d_device *device = viewport->active_device;
973 struct wined3d_sub_resource_desc rt_desc;
974 struct wined3d_rendertarget_view *rtv;
975 IDirect3DViewport3 *current_viewport;
976 struct ddraw_surface *surface;
978 TRACE("iface %p, vp %p.\n", iface, vp);
980 if (!vp)
981 return DDERR_INVALIDPARAMS;
983 if (vp->dwSize != sizeof(*vp))
985 WARN("Invalid D3DVIEWPORT2 size %u.\n", vp->dwSize);
986 return DDERR_INVALIDPARAMS;
989 if (TRACE_ON(ddraw))
991 TRACE(" getting D3DVIEWPORT2 :\n");
992 _dump_D3DVIEWPORT2(vp);
995 if (!device)
997 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
998 return D3DERR_VIEWPORTHASNODEVICE;
1001 wined3d_mutex_lock();
1003 if (device->version > 1)
1005 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1007 wined3d_mutex_unlock();
1008 return DDERR_INVALIDCAPS;
1010 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1011 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1013 if (vp->dwX > rt_desc.width || vp->dwWidth > rt_desc.width - vp->dwX
1014 || vp->dwY > rt_desc.height || vp->dwHeight > rt_desc.height - vp->dwY)
1016 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
1017 wined3d_mutex_unlock();
1018 return DDERR_INVALIDPARAMS;
1022 viewport->version = DDRAW_VIEWPORT_VERSION_2;
1023 viewport->viewports.vp2 = *vp;
1025 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device->IDirect3DDevice3_iface, &current_viewport)))
1027 if (current_viewport == iface)
1028 viewport_activate(viewport, FALSE);
1029 IDirect3DViewport3_Release(current_viewport);
1032 wined3d_mutex_unlock();
1034 return D3D_OK;
1037 /*****************************************************************************
1038 * IDirect3DViewport3 Methods.
1039 *****************************************************************************/
1041 /*****************************************************************************
1042 * IDirect3DViewport3::SetBackgroundDepth2
1044 * Sets a IDirectDrawSurface4 surface as the background depth surface
1046 * Params:
1047 * lpDDS: Surface to set
1049 * Returns:
1050 * D3D_OK, because it's stub
1052 *****************************************************************************/
1053 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
1054 IDirectDrawSurface4 *surface)
1056 FIXME("iface %p, surface %p stub!\n", iface, surface);
1058 return D3D_OK;
1061 /*****************************************************************************
1062 * IDirect3DViewport3::GetBackgroundDepth2
1064 * Returns the IDirect3DSurface4 interface to the background depth surface
1066 * Params:
1067 * lplpDDS: Address to store the interface pointer at
1068 * lpValid: Set to true if a surface is assigned
1070 * Returns:
1071 * D3D_OK because it's a stub
1073 *****************************************************************************/
1074 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1075 IDirectDrawSurface4 **surface, BOOL *valid)
1077 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1079 return D3D_OK;
1082 /*****************************************************************************
1083 * IDirect3DViewport3::Clear2
1085 * Another clearing method
1087 * Params:
1088 * Count: Number of rectangles to clear
1089 * Rects: Rectangle array to clear
1090 * Flags: Some flags :)
1091 * Color: Color to fill the render target with
1092 * Z: Value to fill the depth buffer with
1093 * Stencil: Value to fill the stencil bits with
1095 * Returns:
1097 *****************************************************************************/
1098 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1099 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1101 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1102 HRESULT hr;
1103 IDirect3DViewport3 *current_viewport;
1104 IDirect3DDevice3 *d3d_device3;
1106 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1107 iface, rect_count, rects, flags, color, depth, stencil);
1109 if (!rects || !rect_count)
1111 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
1112 return D3D_OK;
1115 wined3d_mutex_lock();
1117 if (!viewport->active_device)
1119 WARN("Trying to clear a viewport not attached to a device.\n");
1120 wined3d_mutex_unlock();
1121 return D3DERR_VIEWPORTHASNODEVICE;
1123 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1124 /* Need to temporarily activate viewport to clear it. Previously active
1125 * one will be restored afterwards. */
1126 viewport_activate(viewport, TRUE);
1128 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1129 rect_count, rects, flags, color, depth, stencil);
1130 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1132 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1133 viewport_activate(vp, TRUE);
1134 IDirect3DViewport3_Release(current_viewport);
1137 wined3d_mutex_unlock();
1139 return hr;
1142 /*****************************************************************************
1143 * The VTable
1144 *****************************************************************************/
1146 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1148 /*** IUnknown Methods ***/
1149 d3d_viewport_QueryInterface,
1150 d3d_viewport_AddRef,
1151 d3d_viewport_Release,
1152 /*** IDirect3DViewport Methods */
1153 d3d_viewport_Initialize,
1154 d3d_viewport_GetViewport,
1155 d3d_viewport_SetViewport,
1156 d3d_viewport_TransformVertices,
1157 d3d_viewport_LightElements,
1158 d3d_viewport_SetBackground,
1159 d3d_viewport_GetBackground,
1160 d3d_viewport_SetBackgroundDepth,
1161 d3d_viewport_GetBackgroundDepth,
1162 d3d_viewport_Clear,
1163 d3d_viewport_AddLight,
1164 d3d_viewport_DeleteLight,
1165 d3d_viewport_NextLight,
1166 /*** IDirect3DViewport2 Methods ***/
1167 d3d_viewport_GetViewport2,
1168 d3d_viewport_SetViewport2,
1169 /*** IDirect3DViewport3 Methods ***/
1170 d3d_viewport_SetBackgroundDepth2,
1171 d3d_viewport_GetBackgroundDepth2,
1172 d3d_viewport_Clear2,
1175 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1177 if (!iface) return NULL;
1178 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1179 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1182 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1184 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1185 if (!iface) return NULL;
1186 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1187 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1190 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1192 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1193 if (!iface) return NULL;
1194 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1195 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1198 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1200 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1201 viewport->ref = 1;
1202 viewport->ddraw = ddraw;
1203 viewport->version = DDRAW_VIEWPORT_VERSION_NONE;
1204 list_init(&viewport->light_list);