Release 1.9.16.
[wine.git] / dlls / ddraw / viewport.c
blob000158d87d8e70a0d0153962c3c644ef9cb0d8ef
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 if (!This->use_vp2)
276 memcpy(lpData, &(This->viewports.vp1), dwSize);
277 else {
278 D3DVIEWPORT vp1;
279 vp1.dwSize = sizeof(vp1);
280 vp1.dwX = This->viewports.vp2.dwX;
281 vp1.dwY = This->viewports.vp2.dwY;
282 vp1.dwWidth = This->viewports.vp2.dwWidth;
283 vp1.dwHeight = This->viewports.vp2.dwHeight;
284 vp1.dvMaxX = 0.0;
285 vp1.dvMaxY = 0.0;
286 vp1.dvScaleX = 0.0;
287 vp1.dvScaleY = 0.0;
288 vp1.dvMinZ = This->viewports.vp2.dvMinZ;
289 vp1.dvMaxZ = This->viewports.vp2.dvMaxZ;
290 memcpy(lpData, &vp1, dwSize);
293 if (TRACE_ON(ddraw))
295 TRACE(" returning D3DVIEWPORT :\n");
296 _dump_D3DVIEWPORT(lpData);
299 wined3d_mutex_unlock();
301 return DD_OK;
304 /*****************************************************************************
305 * IDirect3DViewport3::SetViewport
307 * Sets the viewport information for this interface
309 * Params:
310 * lpData: Viewport to set
312 * Returns:
313 * D3D_OK on success
314 * DDERR_INVALIDPARAMS if Data is NULL
316 *****************************************************************************/
317 static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIEWPORT *lpData)
319 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
320 IDirect3DViewport3 *current_viewport;
322 TRACE("iface %p, data %p.\n", iface, lpData);
324 if (TRACE_ON(ddraw))
326 TRACE(" getting D3DVIEWPORT :\n");
327 _dump_D3DVIEWPORT(lpData);
330 wined3d_mutex_lock();
332 This->use_vp2 = 0;
333 memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
334 memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
336 /* Tests on two games show that these values are never used properly so override
337 them with proper ones :-)
339 This->viewports.vp1.dvMinZ = 0.0;
340 This->viewports.vp1.dvMaxZ = 1.0;
342 if (This->active_device)
344 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
345 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
347 if (current_viewport == iface) viewport_activate(This, FALSE);
348 IDirect3DViewport3_Release(current_viewport);
352 wined3d_mutex_unlock();
354 return DD_OK;
357 /*****************************************************************************
358 * IDirect3DViewport3::TransformVertices
360 * Transforms vertices by the transformation matrix.
362 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
363 * so it's tempting to forward it to there. However, there are some
364 * tiny differences. First, the lpOffscreen flag that is reported back,
365 * then there is the homogeneous vertex that is generated. Also there's a lack
366 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
367 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
368 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
369 * reused.
371 * Params:
372 * dwVertexCount: The number of vertices to be transformed
373 * lpData: Pointer to the vertex data
374 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
375 * lpOffScreen: Set to the clipping plane clipping the vertex, if only one
376 * vertex is transformed and clipping is on. 0 otherwise
378 * Returns:
379 * D3D_OK on success
380 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
381 * DDERR_INVALIDPARAMS if no clipping flag is specified
383 *****************************************************************************/
384 static HRESULT WINAPI d3d_viewport_TransformVertices(IDirect3DViewport3 *iface,
385 DWORD dwVertexCount, D3DTRANSFORMDATA *lpData, DWORD dwFlags, DWORD *lpOffScreen)
387 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
388 D3DVIEWPORT vp = viewport->viewports.vp1;
389 D3DMATRIX view_mat, world_mat, mat;
390 float *in;
391 float *out;
392 float x, y, z, w;
393 unsigned int i;
394 D3DHVERTEX *outH;
396 TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
397 iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
399 /* Tests on windows show that Windows crashes when this occurs,
400 * so don't return the (intuitive) return value
401 if (!viewport->active_device)
403 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
404 return D3DERR_VIEWPORTHASNODEVICE;
408 if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
410 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
411 return DDERR_INVALIDPARAMS;
415 wined3d_mutex_lock();
416 wined3d_device_get_transform(viewport->active_device->wined3d_device,
417 D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
418 wined3d_device_get_transform(viewport->active_device->wined3d_device,
419 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
420 multiply_matrix(&mat, &view_mat, &world_mat);
421 multiply_matrix(&mat, &viewport->active_device->legacy_projection, &mat);
423 in = lpData->lpIn;
424 out = lpData->lpOut;
425 outH = lpData->lpHOut;
426 for(i = 0; i < dwVertexCount; i++)
428 x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + mat._41;
429 y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + mat._42;
430 z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + mat._43;
431 w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + mat._44;
433 if(dwFlags & D3DTRANSFORM_CLIPPED)
435 /* If clipping is enabled, Windows assumes that outH is
436 * a valid pointer
438 outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
440 outH[i].dwFlags = 0;
441 if(x * vp.dvScaleX > ((float) vp.dwWidth * 0.5))
442 outH[i].dwFlags |= D3DCLIP_RIGHT;
443 if(x * vp.dvScaleX <= -((float) vp.dwWidth) * 0.5)
444 outH[i].dwFlags |= D3DCLIP_LEFT;
445 if(y * vp.dvScaleY > ((float) vp.dwHeight * 0.5))
446 outH[i].dwFlags |= D3DCLIP_TOP;
447 if(y * vp.dvScaleY <= -((float) vp.dwHeight) * 0.5)
448 outH[i].dwFlags |= D3DCLIP_BOTTOM;
449 if(z < 0.0)
450 outH[i].dwFlags |= D3DCLIP_FRONT;
451 if(z > 1.0)
452 outH[i].dwFlags |= D3DCLIP_BACK;
454 if(outH[i].dwFlags)
456 /* Looks like native just drops the vertex, leaves whatever data
457 * it has in the output buffer and goes on with the next vertex.
458 * The exact scheme hasn't been figured out yet, but windows
459 * definitely writes something there.
461 out[0] = x;
462 out[1] = y;
463 out[2] = z;
464 out[3] = w;
465 in = (float *) ((char *) in + lpData->dwInSize);
466 out = (float *) ((char *) out + lpData->dwOutSize);
467 continue;
471 w = 1 / w;
472 x *= w; y *= w; z *= w;
474 out[0] = vp.dwWidth / 2 + vp.dwX + x * vp.dvScaleX;
475 out[1] = vp.dwHeight / 2 + vp.dwY - y * vp.dvScaleY;
476 out[2] = z;
477 out[3] = w;
478 in = (float *) ((char *) in + lpData->dwInSize);
479 out = (float *) ((char *) out + lpData->dwOutSize);
482 /* According to the d3d test, the offscreen flag is set only
483 * if exactly one vertex is transformed. It's not documented,
484 * but the test shows that the lpOffscreen flag is set to the
485 * flag combination of clipping planes that clips the vertex.
487 * If clipping is requested, Windows assumes that the offscreen
488 * param is a valid pointer.
490 if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
492 *lpOffScreen = outH[0].dwFlags;
494 else if(*lpOffScreen)
496 *lpOffScreen = 0;
498 wined3d_mutex_unlock();
500 TRACE("All done\n");
501 return DD_OK;
504 /*****************************************************************************
505 * IDirect3DViewport3::LightElements
507 * The DirectX 5.0 sdk says that it's not implemented
509 * Params:
512 * Returns:
513 * DDERR_UNSUPPORTED
515 *****************************************************************************/
516 static HRESULT WINAPI d3d_viewport_LightElements(IDirect3DViewport3 *iface,
517 DWORD element_count, D3DLIGHTDATA *data)
519 TRACE("iface %p, element_count %u, data %p.\n", iface, element_count, data);
521 return DDERR_UNSUPPORTED;
524 static HRESULT WINAPI d3d_viewport_SetBackground(IDirect3DViewport3 *iface, D3DMATERIALHANDLE material)
526 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
527 struct d3d_material *m;
529 TRACE("iface %p, material %#x.\n", iface, material);
531 wined3d_mutex_lock();
533 if (!(m = ddraw_get_object(&viewport->ddraw->d3ddevice->handle_table, material - 1, DDRAW_HANDLE_MATERIAL)))
535 WARN("Invalid material handle %#x.\n", material);
536 wined3d_mutex_unlock();
537 return DDERR_INVALIDPARAMS;
540 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
541 m->mat.u.diffuse.u1.r, m->mat.u.diffuse.u2.g,
542 m->mat.u.diffuse.u3.b, m->mat.u.diffuse.u4.a);
543 viewport->background = m;
545 wined3d_mutex_unlock();
547 return D3D_OK;
550 /*****************************************************************************
551 * IDirect3DViewport3::GetBackground
553 * Returns the material handle assigned to the background of the viewport
555 * Params:
556 * lphMat: Address to store the handle
557 * lpValid: is set to FALSE if no background is set, TRUE if one is set
559 * Returns:
560 * D3D_OK
562 *****************************************************************************/
563 static HRESULT WINAPI d3d_viewport_GetBackground(IDirect3DViewport3 *iface,
564 D3DMATERIALHANDLE *material, BOOL *valid)
566 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
568 TRACE("iface %p, material %p, valid %p.\n", iface, material, valid);
570 wined3d_mutex_lock();
571 if (valid)
572 *valid = !!viewport->background;
573 if (material)
574 *material = viewport->background ? viewport->background->Handle : 0;
575 wined3d_mutex_unlock();
577 return D3D_OK;
580 /*****************************************************************************
581 * IDirect3DViewport3::SetBackgroundDepth
583 * Sets a surface that represents the background depth. Its contents are
584 * used to set the depth buffer in IDirect3DViewport3::Clear
586 * Params:
587 * lpDDSurface: Surface to set
589 * Returns: D3D_OK, because it's a stub
591 *****************************************************************************/
592 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth(IDirect3DViewport3 *iface, IDirectDrawSurface *surface)
594 FIXME("iface %p, surface %p stub!\n", iface, surface);
596 return D3D_OK;
599 /*****************************************************************************
600 * IDirect3DViewport3::GetBackgroundDepth
602 * Returns the surface that represents the depth field
604 * Params:
605 * lplpDDSurface: Address to store the interface pointer
606 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
608 * Returns:
609 * D3D_OK, because it's a stub
610 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
612 *****************************************************************************/
613 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth(IDirect3DViewport3 *iface,
614 IDirectDrawSurface **surface, BOOL *valid)
616 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
618 return DD_OK;
621 /*****************************************************************************
622 * IDirect3DViewport3::Clear
624 * Clears the render target and / or the z buffer
626 * Params:
627 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
628 * cleared
629 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
630 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
632 * Returns:
633 * D3D_OK on success
634 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
635 * The return value of IDirect3DDevice7::Clear
637 *****************************************************************************/
638 static HRESULT WINAPI d3d_viewport_Clear(IDirect3DViewport3 *iface,
639 DWORD rect_count, D3DRECT *rects, DWORD flags)
641 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
642 DWORD color = 0x00000000;
643 HRESULT hr;
644 IDirect3DViewport3 *current_viewport;
645 IDirect3DDevice3 *d3d_device3;
647 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface, rect_count, rects, flags);
649 if (!rects || !rect_count)
651 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
652 return D3D_OK;
655 if (This->active_device == NULL) {
656 ERR(" Trying to clear a viewport not attached to a device!\n");
657 return D3DERR_VIEWPORTHASNODEVICE;
659 d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
661 wined3d_mutex_lock();
663 if (flags & D3DCLEAR_TARGET)
665 if (!This->background)
666 WARN("No background material set.\n");
667 else
668 color = D3DRGBA(This->background->mat.u.diffuse.u1.r,
669 This->background->mat.u.diffuse.u2.g,
670 This->background->mat.u.diffuse.u3.b,
671 This->background->mat.u.diffuse.u4.a);
674 /* Need to temporarily activate the viewport to clear it. The previously
675 * active one will be restored afterwards. */
676 viewport_activate(This, TRUE);
678 hr = IDirect3DDevice7_Clear(&This->active_device->IDirect3DDevice7_iface, rect_count, rects,
679 flags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
681 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
683 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
684 viewport_activate(vp, TRUE);
685 IDirect3DViewport3_Release(current_viewport);
688 wined3d_mutex_unlock();
690 return hr;
693 /*****************************************************************************
694 * IDirect3DViewport3::AddLight
696 * Adds an light to the viewport
698 * Params:
699 * lpDirect3DLight: Interface of the light to add
701 * Returns:
702 * D3D_OK on success
703 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
704 * DDERR_INVALIDPARAMS if there are 8 lights or more
706 *****************************************************************************/
707 static HRESULT WINAPI d3d_viewport_AddLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
709 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
710 struct d3d_light *light_impl = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
711 DWORD i = 0;
712 DWORD map = This->map_lights;
714 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
716 wined3d_mutex_lock();
718 if (This->num_lights >= 8)
720 wined3d_mutex_unlock();
721 return DDERR_INVALIDPARAMS;
724 /* Find a light number and update both light and viewports objects accordingly */
725 while (map & 1)
727 map >>= 1;
728 ++i;
730 light_impl->dwLightIndex = i;
731 This->num_lights++;
732 This->map_lights |= 1<<i;
734 /* Add the light in the 'linked' chain */
735 list_add_head(&This->light_list, &light_impl->entry);
736 IDirect3DLight_AddRef(lpDirect3DLight);
738 /* Attach the light to the viewport */
739 light_impl->active_viewport = This;
741 /* If active, activate the light */
742 if (This->active_device && light_impl->light.dwFlags & D3DLIGHT_ACTIVE)
744 /* Disable the flag so that light_activate actually does its job. */
745 light_impl->light.dwFlags &= ~D3DLIGHT_ACTIVE;
746 light_activate(light_impl);
749 wined3d_mutex_unlock();
751 return D3D_OK;
754 /*****************************************************************************
755 * IDirect3DViewport3::DeleteLight
757 * Deletes a light from the viewports' light list
759 * Params:
760 * lpDirect3DLight: Light to delete
762 * Returns:
763 * D3D_OK on success
764 * DDERR_INVALIDPARAMS if the light wasn't found
766 *****************************************************************************/
767 static HRESULT WINAPI d3d_viewport_DeleteLight(IDirect3DViewport3 *iface, IDirect3DLight *lpDirect3DLight)
769 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
770 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
772 TRACE("iface %p, light %p.\n", iface, lpDirect3DLight);
774 wined3d_mutex_lock();
776 if (l->active_viewport != viewport)
778 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
779 wined3d_mutex_unlock();
780 return DDERR_INVALIDPARAMS;
783 light_deactivate(l);
784 list_remove(&l->entry);
785 l->active_viewport = NULL;
786 IDirect3DLight_Release(lpDirect3DLight);
787 --viewport->num_lights;
788 viewport->map_lights &= ~(1 << l->dwLightIndex);
790 wined3d_mutex_unlock();
792 return D3D_OK;
795 /*****************************************************************************
796 * IDirect3DViewport::NextLight
798 * Enumerates the lights associated with the viewport
800 * Params:
801 * lpDirect3DLight: Light to start with
802 * lplpDirect3DLight: Address to store the successor to
804 * Returns:
805 * D3D_OK, because it's a stub
807 *****************************************************************************/
808 static HRESULT WINAPI d3d_viewport_NextLight(IDirect3DViewport3 *iface,
809 IDirect3DLight *lpDirect3DLight, IDirect3DLight **lplpDirect3DLight, DWORD flags)
811 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
812 struct d3d_light *l = unsafe_impl_from_IDirect3DLight(lpDirect3DLight);
813 struct list *entry;
814 HRESULT hr;
816 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
817 iface, lpDirect3DLight, lplpDirect3DLight, flags);
819 if (!lplpDirect3DLight)
820 return DDERR_INVALIDPARAMS;
822 wined3d_mutex_lock();
824 switch (flags)
826 case D3DNEXT_NEXT:
827 if (!l || l->active_viewport != viewport)
829 if (l)
830 WARN("Light %p active viewport is %p.\n", l, l->active_viewport);
831 entry = NULL;
833 else
834 entry = list_next(&viewport->light_list, &l->entry);
835 break;
837 case D3DNEXT_HEAD:
838 entry = list_head(&viewport->light_list);
839 break;
841 case D3DNEXT_TAIL:
842 entry = list_tail(&viewport->light_list);
843 break;
845 default:
846 entry = NULL;
847 WARN("Invalid flags %#x.\n", flags);
848 break;
851 if (entry)
853 *lplpDirect3DLight = (IDirect3DLight *)LIST_ENTRY(entry, struct d3d_light, entry);
854 IDirect3DLight_AddRef(*lplpDirect3DLight);
855 hr = D3D_OK;
857 else
859 *lplpDirect3DLight = NULL;
860 hr = DDERR_INVALIDPARAMS;
863 wined3d_mutex_unlock();
865 return hr;
868 /*****************************************************************************
869 * IDirect3DViewport2 Methods.
870 *****************************************************************************/
872 /*****************************************************************************
873 * IDirect3DViewport3::GetViewport2
875 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
876 * Similar to IDirect3DViewport3::GetViewport
878 * Params:
879 * lpData: Pointer to the structure to fill
881 * Returns:
882 * D3D_OK on success
883 * DDERR_INVALIDPARAMS if the viewport was set with
884 * IDirect3DViewport3::SetViewport
885 * DDERR_INVALIDPARAMS if Data is NULL
887 *****************************************************************************/
888 static HRESULT WINAPI d3d_viewport_GetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
890 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
891 DWORD dwSize;
893 TRACE("iface %p, data %p.\n", iface, lpData);
895 wined3d_mutex_lock();
896 dwSize = lpData->dwSize;
897 if (This->use_vp2)
898 memcpy(lpData, &(This->viewports.vp2), dwSize);
899 else {
900 D3DVIEWPORT2 vp2;
901 vp2.dwSize = sizeof(vp2);
902 vp2.dwX = This->viewports.vp1.dwX;
903 vp2.dwY = This->viewports.vp1.dwY;
904 vp2.dwWidth = This->viewports.vp1.dwWidth;
905 vp2.dwHeight = This->viewports.vp1.dwHeight;
906 vp2.dvClipX = 0.0;
907 vp2.dvClipY = 0.0;
908 vp2.dvClipWidth = 0.0;
909 vp2.dvClipHeight = 0.0;
910 vp2.dvMinZ = This->viewports.vp1.dvMinZ;
911 vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
912 memcpy(lpData, &vp2, dwSize);
915 if (TRACE_ON(ddraw))
917 TRACE(" returning D3DVIEWPORT2 :\n");
918 _dump_D3DVIEWPORT2(lpData);
921 wined3d_mutex_unlock();
923 return D3D_OK;
926 /*****************************************************************************
927 * IDirect3DViewport3::SetViewport2
929 * Sets the viewport from a D3DVIEWPORT2 structure
931 * Params:
932 * lpData: Viewport to set
934 * Returns:
935 * D3D_OK on success
937 *****************************************************************************/
938 static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVIEWPORT2 *lpData)
940 struct d3d_viewport *This = impl_from_IDirect3DViewport3(iface);
941 IDirect3DViewport3 *current_viewport;
943 TRACE("iface %p, data %p.\n", iface, lpData);
945 if (TRACE_ON(ddraw))
947 TRACE(" getting D3DVIEWPORT2 :\n");
948 _dump_D3DVIEWPORT2(lpData);
951 wined3d_mutex_lock();
953 This->use_vp2 = 1;
954 memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
955 memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
957 if (This->active_device)
959 IDirect3DDevice3 *d3d_device3 = &This->active_device->IDirect3DDevice3_iface;
960 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
962 if (current_viewport == iface) viewport_activate(This, FALSE);
963 IDirect3DViewport3_Release(current_viewport);
967 wined3d_mutex_unlock();
969 return D3D_OK;
972 /*****************************************************************************
973 * IDirect3DViewport3 Methods.
974 *****************************************************************************/
976 /*****************************************************************************
977 * IDirect3DViewport3::SetBackgroundDepth2
979 * Sets a IDirectDrawSurface4 surface as the background depth surface
981 * Params:
982 * lpDDS: Surface to set
984 * Returns:
985 * D3D_OK, because it's stub
987 *****************************************************************************/
988 static HRESULT WINAPI d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3 *iface,
989 IDirectDrawSurface4 *surface)
991 FIXME("iface %p, surface %p stub!\n", iface, surface);
993 return D3D_OK;
996 /*****************************************************************************
997 * IDirect3DViewport3::GetBackgroundDepth2
999 * Returns the IDirect3DSurface4 interface to the background depth surface
1001 * Params:
1002 * lplpDDS: Address to store the interface pointer at
1003 * lpValid: Set to true if a surface is assigned
1005 * Returns:
1006 * D3D_OK because it's a stub
1008 *****************************************************************************/
1009 static HRESULT WINAPI d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1010 IDirectDrawSurface4 **surface, BOOL *valid)
1012 FIXME("iface %p, surface %p, valid %p stub!\n", iface, surface, valid);
1014 return D3D_OK;
1017 /*****************************************************************************
1018 * IDirect3DViewport3::Clear2
1020 * Another clearing method
1022 * Params:
1023 * Count: Number of rectangles to clear
1024 * Rects: Rectangle array to clear
1025 * Flags: Some flags :)
1026 * Color: Color to fill the render target with
1027 * Z: Value to fill the depth buffer with
1028 * Stencil: Value to fill the stencil bits with
1030 * Returns:
1032 *****************************************************************************/
1033 static HRESULT WINAPI d3d_viewport_Clear2(IDirect3DViewport3 *iface, DWORD rect_count,
1034 D3DRECT *rects, DWORD flags, DWORD color, D3DVALUE depth, DWORD stencil)
1036 struct d3d_viewport *viewport = impl_from_IDirect3DViewport3(iface);
1037 HRESULT hr;
1038 IDirect3DViewport3 *current_viewport;
1039 IDirect3DDevice3 *d3d_device3;
1041 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1042 iface, rect_count, rects, flags, color, depth, stencil);
1044 if (!rects || !rect_count)
1046 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count, rects);
1047 return D3D_OK;
1050 wined3d_mutex_lock();
1052 if (!viewport->active_device)
1054 WARN("Trying to clear a viewport not attached to a device.\n");
1055 wined3d_mutex_unlock();
1056 return D3DERR_VIEWPORTHASNODEVICE;
1058 d3d_device3 = &viewport->active_device->IDirect3DDevice3_iface;
1059 /* Need to temporarily activate viewport to clear it. Previously active
1060 * one will be restored afterwards. */
1061 viewport_activate(viewport, TRUE);
1063 hr = IDirect3DDevice7_Clear(&viewport->active_device->IDirect3DDevice7_iface,
1064 rect_count, rects, flags, color, depth, stencil);
1065 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport)))
1067 struct d3d_viewport *vp = impl_from_IDirect3DViewport3(current_viewport);
1068 viewport_activate(vp, TRUE);
1069 IDirect3DViewport3_Release(current_viewport);
1072 wined3d_mutex_unlock();
1074 return hr;
1077 /*****************************************************************************
1078 * The VTable
1079 *****************************************************************************/
1081 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl =
1083 /*** IUnknown Methods ***/
1084 d3d_viewport_QueryInterface,
1085 d3d_viewport_AddRef,
1086 d3d_viewport_Release,
1087 /*** IDirect3DViewport Methods */
1088 d3d_viewport_Initialize,
1089 d3d_viewport_GetViewport,
1090 d3d_viewport_SetViewport,
1091 d3d_viewport_TransformVertices,
1092 d3d_viewport_LightElements,
1093 d3d_viewport_SetBackground,
1094 d3d_viewport_GetBackground,
1095 d3d_viewport_SetBackgroundDepth,
1096 d3d_viewport_GetBackgroundDepth,
1097 d3d_viewport_Clear,
1098 d3d_viewport_AddLight,
1099 d3d_viewport_DeleteLight,
1100 d3d_viewport_NextLight,
1101 /*** IDirect3DViewport2 Methods ***/
1102 d3d_viewport_GetViewport2,
1103 d3d_viewport_SetViewport2,
1104 /*** IDirect3DViewport3 Methods ***/
1105 d3d_viewport_SetBackgroundDepth2,
1106 d3d_viewport_GetBackgroundDepth2,
1107 d3d_viewport_Clear2,
1110 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3 *iface)
1112 if (!iface) return NULL;
1113 assert(iface->lpVtbl == &d3d_viewport_vtbl);
1114 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1117 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2 *iface)
1119 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1120 if (!iface) return NULL;
1121 assert(iface->lpVtbl == (IDirect3DViewport2Vtbl *)&d3d_viewport_vtbl);
1122 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1125 struct d3d_viewport *unsafe_impl_from_IDirect3DViewport(IDirect3DViewport *iface)
1127 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1128 if (!iface) return NULL;
1129 assert(iface->lpVtbl == (IDirect3DViewportVtbl *)&d3d_viewport_vtbl);
1130 return CONTAINING_RECORD(iface, struct d3d_viewport, IDirect3DViewport3_iface);
1133 void d3d_viewport_init(struct d3d_viewport *viewport, struct ddraw *ddraw)
1135 viewport->IDirect3DViewport3_iface.lpVtbl = &d3d_viewport_vtbl;
1136 viewport->ref = 1;
1137 viewport->ddraw = ddraw;
1138 viewport->use_vp2 = 0xff;
1139 list_init(&viewport->light_list);