include: Add transact.idl to oledb.idl.
[wine.git] / dlls / ddraw / viewport.c
blob13a6a14708304a5b48a23b55f48a187cadce7688
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 "ddraw_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
24 /*****************************************************************************
25 * Helper functions
26 *****************************************************************************/
28 static void update_clip_space(struct d3d_device *device,
29 struct wined3d_vec3 *scale, struct wined3d_vec3 *offset)
31 struct wined3d_matrix clip_space =
33 scale->x, 0.0f, 0.0f, 0.0f,
34 0.0f, scale->y, 0.0f, 0.0f,
35 0.0f, 0.0f, scale->z, 0.0f,
36 offset->x, offset->y, offset->z, 1.0f,
38 struct wined3d_matrix projection;
40 multiply_matrix(&projection, &clip_space, &device->legacy_projection);
41 wined3d_stateblock_set_transform(device->state, WINED3D_TS_PROJECTION, &projection);
42 device->legacy_clipspace = clip_space;
45 /*****************************************************************************
46 * viewport_activate
48 * activates the viewport using IDirect3DDevice7::SetViewport
50 *****************************************************************************/
51 void viewport_activate(struct d3d_viewport *This, BOOL ignore_lights)
53 struct wined3d_vec3 scale, offset;
54 D3DVIEWPORT7 vp;
56 if (!ignore_lights)
58 struct d3d_light *light;
60 /* Activate all the lights associated with this context */
61 LIST_FOR_EACH_ENTRY(light, &This->light_list, struct d3d_light, entry)
63 light_activate(light);
67 if (This->version == DDRAW_VIEWPORT_VERSION_NONE)
69 TRACE("Viewport data was not set.\n");
70 return;
73 /* And copy the values in the structure used by the device */
74 if (This->version == DDRAW_VIEWPORT_VERSION_2)
76 vp.dwX = This->viewports.vp2.dwX;
77 vp.dwY = This->viewports.vp2.dwY;
78 vp.dwHeight = This->viewports.vp2.dwHeight;
79 vp.dwWidth = This->viewports.vp2.dwWidth;
80 vp.dvMinZ = 0.0f;
81 vp.dvMaxZ = 1.0f;
83 scale.x = 2.0f / This->viewports.vp2.dvClipWidth;
84 scale.y = 2.0f / This->viewports.vp2.dvClipHeight;
85 scale.z = 1.0f / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
86 offset.x = -2.0f * This->viewports.vp2.dvClipX / This->viewports.vp2.dvClipWidth - 1.0f;
87 offset.y = -2.0f * This->viewports.vp2.dvClipY / This->viewports.vp2.dvClipHeight + 1.0f;
88 offset.z = -This->viewports.vp2.dvMinZ / (This->viewports.vp2.dvMaxZ - This->viewports.vp2.dvMinZ);
90 else
92 vp.dwX = This->viewports.vp1.dwX;
93 vp.dwY = This->viewports.vp1.dwY;
94 vp.dwHeight = This->viewports.vp1.dwHeight;
95 vp.dwWidth = This->viewports.vp1.dwWidth;
96 vp.dvMinZ = 0.0f;
97 vp.dvMaxZ = 1.0f;
99 scale.x = 2.0f * This->viewports.vp1.dvScaleX / This->viewports.vp1.dwWidth;
100 scale.y = 2.0f * This->viewports.vp1.dvScaleY / This->viewports.vp1.dwHeight;
101 scale.z = 1.0f;
102 offset.x = 0.0f;
103 offset.y = 0.0f;
104 offset.z = 0.0f;
107 update_clip_space(This->active_device, &scale, &offset);
108 IDirect3DDevice7_SetViewport(&This->active_device->IDirect3DDevice7_iface, &vp);
111 void viewport_deactivate(struct d3d_viewport *viewport)
113 struct d3d_light *light;
115 LIST_FOR_EACH_ENTRY(light, &viewport->light_list, struct d3d_light, entry)
117 light_deactivate(light);
121 void viewport_alloc_active_light_index(struct d3d_light *light)
123 struct d3d_viewport *vp = light->active_viewport;
124 unsigned int i;
125 DWORD map;
127 TRACE("vp %p, light %p, index %u, active_lights_count %u.\n",
128 vp, light, light->active_light_index, vp->active_lights_count);
130 if (light->active_light_index)
131 return;
133 if (vp->active_lights_count >= DDRAW_MAX_ACTIVE_LIGHTS)
135 struct d3d_light *l;
137 LIST_FOR_EACH_ENTRY(l, &vp->light_list, struct d3d_light, entry)
139 if (l->active_light_index)
141 WARN("Too many active lights, viewport %p, light %p, deactivating %p.\n", vp, light, l);
142 light_deactivate(l);
144 /* Recycle active lights in a FIFO way. */
145 list_remove(&light->entry);
146 list_add_tail(&vp->light_list, &light->entry);
147 break;
152 map = ~vp->map_lights;
153 assert(vp->active_lights_count < DDRAW_MAX_ACTIVE_LIGHTS && map);
154 i = wined3d_bit_scan(&map);
155 light->active_light_index = i + 1;
156 ++vp->active_lights_count;
157 vp->map_lights |= 1u << i;
160 void viewport_free_active_light_index(struct d3d_light *light)
162 struct d3d_viewport *vp = light->active_viewport;
164 TRACE("vp %p, light %p, index %u, active_lights_count %u, map_lights %#x.\n",
165 vp, light, light->active_light_index, vp->active_lights_count, vp->map_lights);
167 if (!light->active_light_index)
168 return;
170 assert(vp->map_lights & (1u << (light->active_light_index - 1)));
172 --vp->active_lights_count;
173 vp->map_lights &= ~(1u << (light->active_light_index - 1));
174 light->active_light_index = 0;
177 /*****************************************************************************
178 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
180 * Writes viewport information to TRACE
182 *****************************************************************************/
183 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
185 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
186 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
187 TRACE(" - dwWidth = %d dwHeight = %d\n",
188 lpvp->dwWidth, lpvp->dwHeight);
189 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
190 lpvp->dvScaleX, lpvp->dvScaleY);
191 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
192 lpvp->dvMaxX, lpvp->dvMaxY);
193 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
194 lpvp->dvMinZ, lpvp->dvMaxZ);
197 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
199 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
200 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
201 TRACE(" - dwWidth = %d dwHeight = %d\n",
202 lpvp->dwWidth, lpvp->dwHeight);
203 TRACE(" - dvClipX = %f dvClipY = %f\n",
204 lpvp->dvClipX, lpvp->dvClipY);
205 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
206 lpvp->dvClipWidth, lpvp->dvClipHeight);
207 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
208 lpvp->dvMinZ, lpvp->dvMaxZ);
211 static inline struct d3d_viewport *impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
213 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
216 /*****************************************************************************
217 * IUnknown Methods.
218 *****************************************************************************/
220 /*****************************************************************************
221 * IDirect3DViewport3::QueryInterface
223 * A normal QueryInterface. Can query all interface versions and the
224 * IUnknown interface. The VTables of the different versions
225 * are equal
227 * Params:
228 * refiid: Interface id queried for
229 * obj: Address to write the interface pointer to
231 * Returns:
232 * S_OK on success.
233 * E_NOINTERFACE if the requested interface wasn't found
235 *****************************************************************************/
236 static HRESULT WINAPI d3d_viewport_QueryInterface(IDirect3DViewport3 *iface, REFIID riid, void **object)
238 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
240 if (IsEqualGUID(&IID_IDirect3DViewport3, riid)
241 || IsEqualGUID(&IID_IDirect3DViewport2, riid)
242 || IsEqualGUID(&IID_IDirect3DViewport, riid)
243 || IsEqualGUID(&IID_IUnknown, riid))
245 IDirect3DViewport3_AddRef(iface);
246 *object = iface;
247 return S_OK;
250 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
252 *object = NULL;
253 return E_NOINTERFACE;
256 /*****************************************************************************
257 * IDirect3DViewport3::AddRef
259 * Increases the refcount.
261 * Returns:
262 * The new refcount
264 *****************************************************************************/
265 static ULONG WINAPI d3d_viewport_AddRef(IDirect3DViewport3 *iface)
267 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
268 ULONG ref = InterlockedIncrement(&viewport->ref);
270 TRACE("%p increasing refcount to %u.\n", viewport, ref);
272 return ref;
275 /*****************************************************************************
276 * IDirect3DViewport3::Release
278 * Reduces the refcount. If it falls to 0, the interface is released
280 * Returns:
281 * The new refcount
283 *****************************************************************************/
284 static ULONG WINAPI d3d_viewport_Release(IDirect3DViewport3 *iface)
286 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
287 ULONG ref = InterlockedDecrement(&viewport->ref);
289 TRACE("%p decreasing refcount to %u.\n", viewport, ref);
291 if (!ref)
292 heap_free(viewport);
294 return ref;
297 /*****************************************************************************
298 * IDirect3DViewport Methods.
299 *****************************************************************************/
301 /*****************************************************************************
302 * IDirect3DViewport3::Initialize
304 * No-op initialization.
306 * Params:
307 * Direct3D: The direct3D device this viewport is assigned to
309 * Returns:
310 * DDERR_ALREADYINITIALIZED
312 *****************************************************************************/
313 static HRESULT WINAPI d3d_viewport_Initialize(IDirect3DViewport3 *iface, IDirect3D *d3d)
315 TRACE("iface %p, d3d %p.\n", iface, d3d);
317 return DDERR_ALREADYINITIALIZED;
320 static HRESULT WINAPI d3d_viewport_GetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *vp)
322 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
323 DWORD size;
325 TRACE("iface %p, vp %p.\n", iface, vp);
327 if (!vp)
328 return DDERR_INVALIDPARAMS;
330 if (viewport->version == DDRAW_VIEWPORT_VERSION_NONE)
332 WARN("Viewport data was not set.\n");
333 return D3DERR_VIEWPORTDATANOTSET;
336 wined3d_mutex_lock();
338 size = vp->dwSize;
339 if (viewport->version == DDRAW_VIEWPORT_VERSION_1)
341 memcpy(vp, &viewport->viewports.vp1, size);
343 else
345 D3DVIEWPORT vp1;
347 vp1.dwSize = sizeof(vp1);
348 vp1.dwX = viewport->viewports.vp2.dwX;
349 vp1.dwY = viewport->viewports.vp2.dwY;
350 vp1.dwWidth = viewport->viewports.vp2.dwWidth;
351 vp1.dwHeight = viewport->viewports.vp2.dwHeight;
353 vp1.dvScaleX = vp1.dwWidth / viewport->viewports.vp2.dvClipWidth;
354 vp1.dvScaleY = vp1.dwHeight / viewport->viewports.vp2.dvClipHeight;
355 vp1.dvMaxX = viewport->viewports.vp2.dvClipWidth + viewport->viewports.vp2.dvClipX;
356 vp1.dvMaxY = viewport->viewports.vp2.dvClipY;
357 vp1.dvMinZ = 0.0f;
358 vp1.dvMaxZ = 1.0f;
359 memcpy(vp, &vp1, size);
362 if (TRACE_ON(ddraw))
364 TRACE(" returning D3DVIEWPORT :\n");
365 _dump_D3DVIEWPORT(vp);
368 wined3d_mutex_unlock();
370 return D3D_OK;
373 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *vp)
375 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
376 struct d3d_device *device = viewport->active_device;
377 struct wined3d_sub_resource_desc rt_desc;
378 struct wined3d_rendertarget_view *rtv;
379 IDirect3DViewport3 *current_viewport;
380 struct ddraw_surface *surface;
382 TRACE("iface %p, vp %p.\n", iface, vp);
384 if (!vp)
385 return DDERR_INVALIDPARAMS;
387 if (vp->dwSize != sizeof(*vp))
389 WARN("Invalid D3DVIEWPORT size %u.\n", vp->dwSize);
390 return DDERR_INVALIDPARAMS;
393 if (TRACE_ON(ddraw))
395 TRACE(" getting D3DVIEWPORT :\n");
396 _dump_D3DVIEWPORT(vp);
399 if (!device)
401 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
402 return D3DERR_VIEWPORTHASNODEVICE;
405 wined3d_mutex_lock();
407 if (device->version > 1)
409 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
411 wined3d_mutex_unlock();
412 return DDERR_INVALIDCAPS;
414 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
415 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
417 if (!wined3d_bound_range(vp->dwX, vp->dwWidth, rt_desc.width)
418 || !wined3d_bound_range(vp->dwY, vp->dwHeight, rt_desc.height))
420 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
421 wined3d_mutex_unlock();
422 return DDERR_INVALIDPARAMS;
426 viewport->version = DDRAW_VIEWPORT_VERSION_1;
427 viewport->viewports.vp1 = *vp;
429 /* Empirical testing on a couple of d3d1 games showed that these values
430 * should be ignored. */
431 viewport->viewports.vp1.dvMinZ = 0.0f;
432 viewport->viewports.vp1.dvMaxZ = 1.0f;
434 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device->IDirect3DDevice3_iface, &current_viewport)))
436 if (current_viewport == iface)
437 viewport_activate(viewport, FALSE);
438 IDirect3DViewport3_Release(current_viewport);
441 wined3d_mutex_unlock();
443 return D3D_OK;
446 /*****************************************************************************
447 * IDirect3DViewport3::TransformVertices
449 * Transforms vertices by the transformation matrix.
451 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
452 * so it's tempting to forward it to there. However, there are some
453 * tiny differences. First, the lpOffscreen flag that is reported back,
454 * then there is the homogeneous vertex that is generated. Also there's a lack
455 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
456 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
457 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
458 * reused.
460 * Params:
461 * dwVertexCount: The number of vertices to be transformed
462 * data: Pointer to the vertex input / output data.
463 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
464 * offscreen: Logical AND of the planes that clipped the vertices if clipping
465 * is on. 0 if clipping is off.
467 * Returns:
468 * D3D_OK on success
469 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
470 * DDERR_INVALIDPARAMS if no clipping flag is specified
472 *****************************************************************************/
473 struct transform_vertices_vertex
475 float x, y, z, w; /* w is unused in input data. */
476 struct
478 DWORD p[4];
479 } payload;
482 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
483 DWORD dwVertexCount, D3DTRANSFORMDATA *data, DWORD dwFlags, DWORD *offscreen)
485 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
486 const struct wined3d_stateblock_state *state;
487 D3DVIEWPORT vp = viewport->viewports.vp1;
488 struct transform_vertices_vertex *in, *out;
489 struct wined3d_matrix mat;
490 float x, y, z, w;
491 unsigned int i;
492 D3DHVERTEX *outH;
493 struct d3d_device *device = viewport->active_device;
494 BOOL activate = device->current_viewport != viewport;
496 TRACE("iface %p, vertex_count %u, data %p, flags %#x, offscreen %p.\n",
497 iface, dwVertexCount, data, dwFlags, offscreen);
499 /* Tests on windows show that Windows crashes when this occurs,
500 * so don't return the (intuitive) return value
501 if (!device)
503 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
504 return D3DERR_VIEWPORTHASNODEVICE;
508 if (!data || data->dwSize != sizeof(*data))
510 WARN("Transform data is NULL or size is incorrect, returning DDERR_INVALIDPARAMS\n");
511 return DDERR_INVALIDPARAMS;
513 if (!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
515 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
516 return DDERR_INVALIDPARAMS;
519 wined3d_mutex_lock();
520 if (activate)
521 viewport_activate(viewport, TRUE);
523 state = device->stateblock_state;
524 multiply_matrix(&mat, &state->transforms[WINED3D_TS_VIEW], &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
525 multiply_matrix(&mat, &state->transforms[WINED3D_TS_PROJECTION], &mat);
527 /* The pointer is not tested against NULL on Windows. */
528 if (dwFlags & D3DTRANSFORM_CLIPPED)
529 *offscreen = ~0U;
530 else
531 *offscreen = 0;
533 outH = data->lpHOut;
534 for(i = 0; i < dwVertexCount; i++)
536 in = (struct transform_vertices_vertex *)((char *)data->lpIn + data->dwInSize * i);
537 out = (struct transform_vertices_vertex *)((char *)data->lpOut + data->dwOutSize * i);
539 x = (in->x * mat._11) + (in->y * mat._21) + (in->z * mat._31) + mat._41;
540 y = (in->x * mat._12) + (in->y * mat._22) + (in->z * mat._32) + mat._42;
541 z = (in->x * mat._13) + (in->y * mat._23) + (in->z * mat._33) + mat._43;
542 w = (in->x * mat._14) + (in->y * mat._24) + (in->z * mat._34) + mat._44;
544 if(dwFlags & D3DTRANSFORM_CLIPPED)
546 /* If clipping is enabled, Windows assumes that outH is
547 * a valid pointer. */
548 outH[i].u1.hx = (x - device->legacy_clipspace._41 * w) / device->legacy_clipspace._11;
549 outH[i].u2.hy = (y - device->legacy_clipspace._42 * w) / device->legacy_clipspace._22;
550 outH[i].u3.hz = (z - device->legacy_clipspace._43 * w) / device->legacy_clipspace._33;
552 outH[i].dwFlags = 0;
553 if (x > w)
554 outH[i].dwFlags |= D3DCLIP_RIGHT;
555 if (x < -w)
556 outH[i].dwFlags |= D3DCLIP_LEFT;
557 if (y > w)
558 outH[i].dwFlags |= D3DCLIP_TOP;
559 if (y < -w)
560 outH[i].dwFlags |= D3DCLIP_BOTTOM;
561 if (z < 0.0f)
562 outH[i].dwFlags |= D3DCLIP_FRONT;
563 if (z > w)
564 outH[i].dwFlags |= D3DCLIP_BACK;
566 *offscreen &= outH[i].dwFlags;
568 if(outH[i].dwFlags)
570 /* Looks like native just drops the vertex, leaves whatever data
571 * it has in the output buffer and goes on with the next vertex.
572 * The exact scheme hasn't been figured out yet, but windows
573 * definitely writes something there.
575 out->x = x;
576 out->y = y;
577 out->z = z;
578 out->w = w;
579 continue;
583 w = 1 / w;
584 x *= w; y *= w; z *= w;
586 out->x = (x + 1.0f) * vp.dwWidth * 0.5 + vp.dwX;
587 out->y = (-y + 1.0f) * vp.dwHeight * 0.5 + vp.dwY;
588 out->z = z;
589 out->w = w;
590 out->payload = in->payload;
593 if (activate && device->current_viewport)
594 viewport_activate(device->current_viewport, TRUE);
596 wined3d_mutex_unlock();
598 TRACE("All done\n");
599 return DD_OK;
602 /*****************************************************************************
603 * IDirect3DViewport3::LightElements
605 * The DirectX 5.0 sdk says that it's not implemented
607 * Params:
610 * Returns:
611 * DDERR_UNSUPPORTED
613 *****************************************************************************/
614 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
615 DWORD element_count, D3DLIGHTDATA *data)
617 TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
619 return DDERR_UNSUPPORTED;
622 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
624 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
625 struct d3d_material *m;
627 TRACE("iface %p, material %#x.\n", iface, material);
629 wined3d_mutex_lock();
631 if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
633 WARN("Invalid material handle %#x.\n", material);
634 wined3d_mutex_unlock();
635 return DDERR_INVALIDPARAMS;
638 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
639 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
640 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
641 viewport->background = m;
643 wined3d_mutex_unlock();
645 return D3D_OK;
648 /*****************************************************************************
649 * IDirect3DViewport3::GetBackground
651 * Returns the material handle assigned to the background of the viewport
653 * Params:
654 * lphMat: Address to store the handle
655 * lpValid: is set to FALSE if no background is set, TRUE if one is set
657 * Returns:
658 * D3D_OK
660 *****************************************************************************/
661 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
662 D3DMATERIALHANDLE *material, BOOL *valid)
664 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
666 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
668 wined3d_mutex_lock();
669 if (valid)
670 *valid = !!viewport->background;
671 if (material)
672 *material = viewport->background ? viewport->background->Handle : 0;
673 wined3d_mutex_unlock();
675 return D3D_OK;
678 /*****************************************************************************
679 * IDirect3DViewport3::SetBackgroundDepth
681 * Sets a surface that represents the background depth. Its contents are
682 * used to set the depth buffer in IDirect3DViewport3::Clear
684 * Params:
685 * lpDDSurface: Surface to set
687 * Returns: D3D_OK, because it's a stub
689 *****************************************************************************/
690 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
692 FIXME("iface %p, surface %p stub!\n", iface, surface);
694 return D3D_OK;
697 /*****************************************************************************
698 * IDirect3DViewport3::GetBackgroundDepth
700 * Returns the surface that represents the depth field
702 * Params:
703 * lplpDDSurface: Address to store the interface pointer
704 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
706 * Returns:
707 * D3D_OK, because it's a stub
708 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
710 *****************************************************************************/
711 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
712 IDirectDrawSurface **surface, BOOL *valid)
714 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
716 return DD_OK;
719 /*****************************************************************************
720 * IDirect3DViewport3::Clear
722 * Clears the render target and / or the z buffer
724 * Params:
725 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
726 * cleared
727 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
728 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
730 * Returns:
731 * D3D_OK on success
732 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
733 * The return value of IDirect3DDevice7::Clear
735 *****************************************************************************/
736 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
737 DWORD rect_count, D3DRECT *rects, DWORD flags)
739 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
740 DWORD color = 0x00000000;
741 HRESULT hr;
742 IDirect3DViewport3 *current_viewport;
743 IDirect3DDevice3 *d3d_device3;
745 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
747 if (!rects || !rect_count)
749 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
750 return D3D_OK;
753 if (This->active_device == NULL) {
754 ERR(" Trying to clear a viewport not attached to a device!\n");
755 return D3DERR_VIEWPORTHASNODEVICE;
757 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
759 wined3d_mutex_lock();
761 if (flags & D3DCLEAR_TARGET)
763 if (!This->background)
764 WARN("No background material set.\n");
765 else
766 color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
767 This->background->mat.u.diffuse.u2.g,
768 This->background->mat.u.diffuse.u3.b,
769 This->background->mat.u.diffuse.u4.a);
772 /* Need to temporarily activate the viewport to clear it. The previously
773 * active one will be restored afterwards. */
774 viewport_activate(This, TRUE);
776 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
777 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
779 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
781 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
782 viewport_activate(vp, TRUE);
783 IDirect3DViewport3_Release(current_viewport);
786 wined3d_mutex_unlock();
788 return hr;
791 /*****************************************************************************
792 * IDirect3DViewport3::AddLight
794 * Adds an light to the viewport
796 * Params:
797 * lpDirect3DLight: Interface of the light to add
799 * Returns:
800 * D3D_OK on success
801 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
802 * DDERR_INVALIDPARAMS if there are 8 lights or more
804 *****************************************************************************/
805 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *viewport, IDirect3DLight *light)
807 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(light);
808 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(viewport);
810 TRACE("viewport %p, light %p.\n", viewport, light);
812 wined3d_mutex_lock();
814 if (light_impl->active_viewport)
816 wined3d_mutex_unlock();
817 WARN("Light %p is active in viewport %p.\n", light_impl, light_impl->active_viewport);
818 return D3DERR_LIGHTHASVIEWPORT;
821 light_impl->active_viewport = vp;
823 /* Add the light in the 'linked' chain */
824 list_add_tail(&vp->light_list, &light_impl->entry);
825 IDirect3DLight_AddRef(light);
827 light_activate(light_impl);
829 wined3d_mutex_unlock();
831 return D3D_OK;
834 /*****************************************************************************
835 * IDirect3DViewport3::DeleteLight
837 * Deletes a light from the viewports' light list
839 * Params:
840 * lpDirect3DLight: Light to delete
842 * Returns:
843 * D3D_OK on success
844 * DDERR_INVALIDPARAMS if the light wasn't found
846 *****************************************************************************/
847 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
849 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
850 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
852 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
854 wined3d_mutex_lock();
856 if (l->active_viewport != viewport)
858 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
859 wined3d_mutex_unlock();
860 return DDERR_INVALIDPARAMS;
863 light_deactivate(l);
864 list_remove(&l->entry);
865 l->active_viewport = NULL;
866 IDirect3DLight_Release(lpDirect3DLight);
868 wined3d_mutex_unlock();
870 return D3D_OK;
873 /*****************************************************************************
874 * IDirect3DViewport::NextLight
876 * Enumerates the lights associated with the viewport
878 * Params:
879 * lpDirect3DLight: Light to start with
880 * lplpDirect3DLight: Address to store the successor to
882 * Returns:
883 * D3D_OK, because it's a stub
885 *****************************************************************************/
886 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
887 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
889 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
890 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
891 struct list *entry;
892 HRESULT hr;
894 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
895 iface, lpDirect3DLight, lplpDirect3DLight, flags);
897 if (!lplpDirect3DLight)
898 return DDERR_INVALIDPARAMS;
900 wined3d_mutex_lock();
902 switch (flags)
904 case D3DNEXT_NEXT:
905 if (!l || l->active_viewport != viewport)
907 if (l)
908 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
909 entry = NULL;
911 else
912 entry = list_next(&viewport->light_list, &l->entry);
913 break;
915 case D3DNEXT_HEAD:
916 entry = list_head(&viewport->light_list);
917 break;
919 case D3DNEXT_TAIL:
920 entry = list_tail(&viewport->light_list);
921 break;
923 default:
924 entry = NULL;
925 WARN("Invalid flags %#x.\n", flags);
926 break;
929 if (entry)
931 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
932 IDirect3DLight_AddRef(*lplpDirect3DLight);
933 hr = D3D_OK;
935 else
937 *lplpDirect3DLight = NULL;
938 hr = DDERR_INVALIDPARAMS;
941 wined3d_mutex_unlock();
943 return hr;
946 /*****************************************************************************
947 * IDirect3DViewport2 Methods.
948 *****************************************************************************/
950 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *vp)
952 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
953 DWORD size;
955 TRACE("iface %p, vp %p.\n", iface, vp);
957 if (!vp)
958 return DDERR_INVALIDPARAMS;
960 if (viewport->version == DDRAW_VIEWPORT_VERSION_NONE)
962 WARN("Viewport data was not set.\n");
963 return D3DERR_VIEWPORTDATANOTSET;
966 wined3d_mutex_lock();
967 size = vp->dwSize;
968 if (viewport->version == DDRAW_VIEWPORT_VERSION_2)
970 memcpy(vp, &viewport->viewports.vp2, size);
972 else
974 D3DVIEWPORT2 vp2;
976 vp2.dwSize = sizeof(vp2);
977 vp2.dwX = viewport->viewports.vp1.dwX;
978 vp2.dwY = viewport->viewports.vp1.dwY;
979 vp2.dwWidth = viewport->viewports.vp1.dwWidth;
980 vp2.dwHeight = viewport->viewports.vp1.dwHeight;
982 vp2.dvClipWidth = viewport->viewports.vp1.dwWidth / viewport->viewports.vp1.dvScaleX;
983 vp2.dvClipHeight = viewport->viewports.vp1.dwHeight / viewport->viewports.vp1.dvScaleY;
984 vp2.dvClipX = -vp2.dvClipWidth / 2.0f;
985 vp2.dvClipY = vp2.dvClipHeight / 2.0f;
986 vp2.dvMinZ = 0.0f;
987 vp2.dvMaxZ = 1.0f;
988 memcpy(vp, &vp2, size);
991 if (TRACE_ON(ddraw))
993 TRACE(" returning D3DVIEWPORT2 :\n");
994 _dump_D3DVIEWPORT2(vp);
997 wined3d_mutex_unlock();
999 return D3D_OK;
1002 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *vp)
1004 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1005 struct d3d_device *device = viewport->active_device;
1006 struct wined3d_sub_resource_desc rt_desc;
1007 struct wined3d_rendertarget_view *rtv;
1008 IDirect3DViewport3 *current_viewport;
1009 struct ddraw_surface *surface;
1011 TRACE("iface %p, vp %p.\n", iface, vp);
1013 if (!vp)
1014 return DDERR_INVALIDPARAMS;
1016 if (vp->dwSize != sizeof(*vp))
1018 WARN("Invalid D3DVIEWPORT2 size %u.\n", vp->dwSize);
1019 return DDERR_INVALIDPARAMS;
1022 if (TRACE_ON(ddraw))
1024 TRACE(" getting D3DVIEWPORT2 :\n");
1025 _dump_D3DVIEWPORT2(vp);
1028 if (!device)
1030 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
1031 return D3DERR_VIEWPORTHASNODEVICE;
1034 wined3d_mutex_lock();
1036 if (device->version > 1)
1038 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1040 wined3d_mutex_unlock();
1041 return DDERR_INVALIDCAPS;
1043 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1044 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1046 if (!wined3d_bound_range(vp->dwX, vp->dwWidth, rt_desc.width)
1047 || !wined3d_bound_range(vp->dwY, vp->dwHeight, rt_desc.height))
1049 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
1050 wined3d_mutex_unlock();
1051 return DDERR_INVALIDPARAMS;
1055 viewport->version = DDRAW_VIEWPORT_VERSION_2;
1056 viewport->viewports.vp2 = *vp;
1058 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device->IDirect3DDevice3_iface, &current_viewport)))
1060 if (current_viewport == iface)
1061 viewport_activate(viewport, FALSE);
1062 IDirect3DViewport3_Release(current_viewport);
1065 wined3d_mutex_unlock();
1067 return D3D_OK;
1070 /*****************************************************************************
1071 * IDirect3DViewport3 Methods.
1072 *****************************************************************************/
1074 /*****************************************************************************
1075 * IDirect3DViewport3::SetBackgroundDepth2
1077 * Sets a IDirectDrawSurface4 surface as the background depth surface
1079 * Params:
1080 * lpDDS: Surface to set
1082 * Returns:
1083 * D3D_OK, because it's stub
1085 *****************************************************************************/
1086 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
1087 IDirectDrawSurface4 *surface)
1089 FIXME("iface %p, surface %p stub!\n", iface, surface);
1091 return D3D_OK;
1094 /*****************************************************************************
1095 * IDirect3DViewport3::GetBackgroundDepth2
1097 * Returns the IDirect3DSurface4 interface to the background depth surface
1099 * Params:
1100 * lplpDDS: Address to store the interface pointer at
1101 * lpValid: Set to true if a surface is assigned
1103 * Returns:
1104 * D3D_OK because it's a stub
1106 *****************************************************************************/
1107 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1108 IDirectDrawSurface4 **surface, BOOL *valid)
1110 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1112 return D3D_OK;
1115 /*****************************************************************************
1116 * IDirect3DViewport3::Clear2
1118 * Another clearing method
1120 * Params:
1121 * Count: Number of rectangles to clear
1122 * Rects: Rectangle array to clear
1123 * Flags: Some flags :)
1124 * Color: Color to fill the render target with
1125 * Z: Value to fill the depth buffer with
1126 * Stencil: Value to fill the stencil bits with
1128 * Returns:
1130 *****************************************************************************/
1131 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1132 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1134 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1135 HRESULT hr;
1136 IDirect3DViewport3 *current_viewport;
1137 IDirect3DDevice3 *d3d_device3;
1139 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1140 iface, rect_count, rects, flags, color, depth, stencil);
1142 if (!rects || !rect_count)
1144 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
1145 return D3D_OK;
1148 wined3d_mutex_lock();
1150 if (!viewport->active_device)
1152 WARN("Trying to clear a viewport not attached to a device.\n");
1153 wined3d_mutex_unlock();
1154 return D3DERR_VIEWPORTHASNODEVICE;
1156 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1157 /* Need to temporarily activate viewport to clear it. Previously active
1158 * one will be restored afterwards. */
1159 viewport_activate(viewport, TRUE);
1161 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1162 rect_count, rects, flags, color, depth, stencil);
1163 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1165 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1166 viewport_activate(vp, TRUE);
1167 IDirect3DViewport3_Release(current_viewport);
1170 wined3d_mutex_unlock();
1172 return hr;
1175 /*****************************************************************************
1176 * The VTable
1177 *****************************************************************************/
1179 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1181 /*** IUnknown Methods ***/
1182 d3d_viewport_QueryInterface,
1183 d3d_viewport_AddRef,
1184 d3d_viewport_Release,
1185 /*** IDirect3DViewport Methods */
1186 d3d_viewport_Initialize,
1187 d3d_viewport_GetViewport,
1188 d3d_viewport_SetViewport,
1189 d3d_viewport_TransformVertices,
1190 d3d_viewport_LightElements,
1191 d3d_viewport_SetBackground,
1192 d3d_viewport_GetBackground,
1193 d3d_viewport_SetBackgroundDepth,
1194 d3d_viewport_GetBackgroundDepth,
1195 d3d_viewport_Clear,
1196 d3d_viewport_AddLight,
1197 d3d_viewport_DeleteLight,
1198 d3d_viewport_NextLight,
1199 /*** IDirect3DViewport2 Methods ***/
1200 d3d_viewport_GetViewport2,
1201 d3d_viewport_SetViewport2,
1202 /*** IDirect3DViewport3 Methods ***/
1203 d3d_viewport_SetBackgroundDepth2,
1204 d3d_viewport_GetBackgroundDepth2,
1205 d3d_viewport_Clear2,
1208 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1210 if (!iface) return NULL;
1211 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1212 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1215 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1217 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1218 if (!iface) return NULL;
1219 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1220 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1223 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1225 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1226 if (!iface) return NULL;
1227 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1228 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1231 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1233 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1234 viewport->ref = 1;
1235 viewport->ddraw = ddraw;
1236 viewport->version = DDRAW_VIEWPORT_VERSION_NONE;
1237 list_init(&viewport->light_list);