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 /*****************************************************************************
24 *****************************************************************************/
26 static void update_clip_space(struct d3d_device
*device
,
27 struct wined3d_vec3
*scale
, struct wined3d_vec3
*offset
)
29 D3DMATRIX clip_space
=
31 scale
->x
, 0.0f
, 0.0f
, 0.0f
,
32 0.0f
, scale
->y
, 0.0f
, 0.0f
,
33 0.0f
, 0.0f
, scale
->z
, 0.0f
,
34 offset
->x
, offset
->y
, offset
->z
, 1.0f
,
38 multiply_matrix(&projection
, &clip_space
, &device
->legacy_projection
);
39 wined3d_device_set_transform(device
->wined3d_device
,
40 WINED3D_TS_PROJECTION
, (struct wined3d_matrix
*)&projection
);
41 device
->legacy_clipspace
= clip_space
;
44 /*****************************************************************************
47 * activates the viewport using IDirect3DDevice7::SetViewport
49 *****************************************************************************/
50 void viewport_activate(struct d3d_viewport
*This
, BOOL ignore_lights
)
52 struct wined3d_vec3 scale
, offset
;
57 struct d3d_light
*light
;
59 /* Activate all the lights associated with this context */
60 LIST_FOR_EACH_ENTRY(light
, &This
->light_list
, struct d3d_light
, entry
)
62 light_activate(light
);
66 /* And copy the values in the structure used by the device */
69 vp
.dwX
= This
->viewports
.vp2
.dwX
;
70 vp
.dwY
= This
->viewports
.vp2
.dwY
;
71 vp
.dwHeight
= This
->viewports
.vp2
.dwHeight
;
72 vp
.dwWidth
= This
->viewports
.vp2
.dwWidth
;
76 scale
.x
= 2.0f
/ This
->viewports
.vp2
.dvClipWidth
;
77 scale
.y
= 2.0f
/ This
->viewports
.vp2
.dvClipHeight
;
78 scale
.z
= 1.0f
/ (This
->viewports
.vp2
.dvMaxZ
- This
->viewports
.vp2
.dvMinZ
);
79 offset
.x
= -2.0f
* This
->viewports
.vp2
.dvClipX
/ This
->viewports
.vp2
.dvClipWidth
- 1.0f
;
80 offset
.y
= -2.0f
* This
->viewports
.vp2
.dvClipY
/ This
->viewports
.vp2
.dvClipHeight
+ 1.0f
;
81 offset
.z
= -This
->viewports
.vp2
.dvMinZ
/ (This
->viewports
.vp2
.dvMaxZ
- This
->viewports
.vp2
.dvMinZ
);
85 vp
.dwX
= This
->viewports
.vp1
.dwX
;
86 vp
.dwY
= This
->viewports
.vp1
.dwY
;
87 vp
.dwHeight
= This
->viewports
.vp1
.dwHeight
;
88 vp
.dwWidth
= This
->viewports
.vp1
.dwWidth
;
92 scale
.x
= 2.0f
* This
->viewports
.vp1
.dvScaleX
/ This
->viewports
.vp1
.dwWidth
;
93 scale
.y
= 2.0f
* This
->viewports
.vp1
.dvScaleY
/ This
->viewports
.vp1
.dwHeight
;
100 update_clip_space(This
->active_device
, &scale
, &offset
);
101 IDirect3DDevice7_SetViewport(&This
->active_device
->IDirect3DDevice7_iface
, &vp
);
104 /*****************************************************************************
105 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
107 * Writes viewport information to TRACE
109 *****************************************************************************/
110 static void _dump_D3DVIEWPORT(const D3DVIEWPORT
*lpvp
)
112 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
113 lpvp
->dwSize
, lpvp
->dwX
, lpvp
->dwY
);
114 TRACE(" - dwWidth = %d dwHeight = %d\n",
115 lpvp
->dwWidth
, lpvp
->dwHeight
);
116 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
117 lpvp
->dvScaleX
, lpvp
->dvScaleY
);
118 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
119 lpvp
->dvMaxX
, lpvp
->dvMaxY
);
120 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
121 lpvp
->dvMinZ
, lpvp
->dvMaxZ
);
124 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2
*lpvp
)
126 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
127 lpvp
->dwSize
, lpvp
->dwX
, lpvp
->dwY
);
128 TRACE(" - dwWidth = %d dwHeight = %d\n",
129 lpvp
->dwWidth
, lpvp
->dwHeight
);
130 TRACE(" - dvClipX = %f dvClipY = %f\n",
131 lpvp
->dvClipX
, lpvp
->dvClipY
);
132 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
133 lpvp
->dvClipWidth
, lpvp
->dvClipHeight
);
134 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
135 lpvp
->dvMinZ
, lpvp
->dvMaxZ
);
138 static inline struct d3d_viewport
*impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
140 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
143 /*****************************************************************************
145 *****************************************************************************/
147 /*****************************************************************************
148 * IDirect3DViewport3::QueryInterface
150 * A normal QueryInterface. Can query all interface versions and the
151 * IUnknown interface. The VTables of the different versions
155 * refiid: Interface id queried for
156 * obj: Address to write the interface pointer to
160 * E_NOINTERFACE if the requested interface wasn't found
162 *****************************************************************************/
163 static HRESULT WINAPI
d3d_viewport_QueryInterface(IDirect3DViewport3
*iface
, REFIID riid
, void **object
)
165 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
167 if (IsEqualGUID(&IID_IDirect3DViewport3
, riid
)
168 || IsEqualGUID(&IID_IDirect3DViewport2
, riid
)
169 || IsEqualGUID(&IID_IDirect3DViewport
, riid
)
170 || IsEqualGUID(&IID_IUnknown
, riid
))
172 IDirect3DViewport3_AddRef(iface
);
177 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
180 return E_NOINTERFACE
;
183 /*****************************************************************************
184 * IDirect3DViewport3::AddRef
186 * Increases the refcount.
191 *****************************************************************************/
192 static ULONG WINAPI
d3d_viewport_AddRef(IDirect3DViewport3
*iface
)
194 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
195 ULONG ref
= InterlockedIncrement(&viewport
->ref
);
197 TRACE("%p increasing refcount to %u.\n", viewport
, ref
);
202 /*****************************************************************************
203 * IDirect3DViewport3::Release
205 * Reduces the refcount. If it falls to 0, the interface is released
210 *****************************************************************************/
211 static ULONG WINAPI
d3d_viewport_Release(IDirect3DViewport3
*iface
)
213 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
214 ULONG ref
= InterlockedDecrement(&viewport
->ref
);
216 TRACE("%p decreasing refcount to %u.\n", viewport
, ref
);
219 HeapFree(GetProcessHeap(), 0, viewport
);
224 /*****************************************************************************
225 * IDirect3DViewport Methods.
226 *****************************************************************************/
228 /*****************************************************************************
229 * IDirect3DViewport3::Initialize
231 * No-op initialization.
234 * Direct3D: The direct3D device this viewport is assigned to
237 * DDERR_ALREADYINITIALIZED
239 *****************************************************************************/
240 static HRESULT WINAPI
d3d_viewport_Initialize(IDirect3DViewport3
*iface
, IDirect3D
*d3d
)
242 TRACE("iface %p, d3d %p.\n", iface
, d3d
);
244 return DDERR_ALREADYINITIALIZED
;
247 /*****************************************************************************
248 * IDirect3DViewport3::GetViewport
250 * Returns the viewport data assigned to this viewport interface
253 * Data: Address to store the data
257 * DDERR_INVALIDPARAMS if Data is NULL
259 *****************************************************************************/
260 static HRESULT WINAPI
d3d_viewport_GetViewport(IDirect3DViewport3
*iface
, D3DVIEWPORT
*lpData
)
262 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
265 TRACE("iface %p, data %p.\n", iface
, lpData
);
267 wined3d_mutex_lock();
269 dwSize
= lpData
->dwSize
;
271 memcpy(lpData
, &(This
->viewports
.vp1
), dwSize
);
274 vp1
.dwSize
= sizeof(vp1
);
275 vp1
.dwX
= This
->viewports
.vp2
.dwX
;
276 vp1
.dwY
= This
->viewports
.vp2
.dwY
;
277 vp1
.dwWidth
= This
->viewports
.vp2
.dwWidth
;
278 vp1
.dwHeight
= This
->viewports
.vp2
.dwHeight
;
283 vp1
.dvMinZ
= This
->viewports
.vp2
.dvMinZ
;
284 vp1
.dvMaxZ
= This
->viewports
.vp2
.dvMaxZ
;
285 memcpy(lpData
, &vp1
, dwSize
);
290 TRACE(" returning D3DVIEWPORT :\n");
291 _dump_D3DVIEWPORT(lpData
);
294 wined3d_mutex_unlock();
299 /*****************************************************************************
300 * IDirect3DViewport3::SetViewport
302 * Sets the viewport information for this interface
305 * lpData: Viewport to set
309 * DDERR_INVALIDPARAMS if Data is NULL
311 *****************************************************************************/
312 static HRESULT WINAPI
d3d_viewport_SetViewport(IDirect3DViewport3
*iface
, D3DVIEWPORT
*lpData
)
314 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
315 IDirect3DViewport3
*current_viewport
;
317 TRACE("iface %p, data %p.\n", iface
, lpData
);
321 TRACE(" getting D3DVIEWPORT :\n");
322 _dump_D3DVIEWPORT(lpData
);
325 wined3d_mutex_lock();
328 memset(&(This
->viewports
.vp1
), 0, sizeof(This
->viewports
.vp1
));
329 memcpy(&(This
->viewports
.vp1
), lpData
, lpData
->dwSize
);
331 /* Tests on two games show that these values are never used properly so override
332 them with proper ones :-)
334 This
->viewports
.vp1
.dvMinZ
= 0.0;
335 This
->viewports
.vp1
.dvMaxZ
= 1.0;
337 if (This
->active_device
)
339 IDirect3DDevice3
*d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
340 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
342 if (current_viewport
== iface
) viewport_activate(This
, FALSE
);
343 IDirect3DViewport3_Release(current_viewport
);
347 wined3d_mutex_unlock();
352 /*****************************************************************************
353 * IDirect3DViewport3::TransformVertices
355 * Transforms vertices by the transformation matrix.
357 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
358 * so it's tempting to forward it to there. However, there are some
359 * tiny differences. First, the lpOffscreen flag that is reported back,
360 * then there is the homogeneous vertex that is generated. Also there's a lack
361 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
362 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
363 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
367 * dwVertexCount: The number of vertices to be transformed
368 * data: Pointer to the vertex input / output data.
369 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
370 * offscreen: Logical AND of the planes that clipped the vertices if clipping
371 * is on. 0 if clipping is off.
375 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
376 * DDERR_INVALIDPARAMS if no clipping flag is specified
378 *****************************************************************************/
379 struct transform_vertices_vertex
381 float x
, y
, z
, w
; /* w is unused in input data. */
388 static HRESULT WINAPI
d3d_viewport_TransformVertices(IDirect3DViewport3
*iface
,
389 DWORD dwVertexCount
, D3DTRANSFORMDATA
*data
, DWORD dwFlags
, DWORD
*offscreen
)
391 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
392 D3DVIEWPORT vp
= viewport
->viewports
.vp1
;
393 D3DMATRIX view_mat
, world_mat
, proj_mat
, mat
;
394 struct transform_vertices_vertex
*in
, *out
;
398 struct d3d_device
*device
= viewport
->active_device
;
399 BOOL activate
= device
->current_viewport
!= viewport
;
401 TRACE("iface %p, vertex_count %u, data %p, flags %#x, offscreen %p.\n",
402 iface
, dwVertexCount
, data
, dwFlags
, offscreen
);
404 /* Tests on windows show that Windows crashes when this occurs,
405 * so don't return the (intuitive) return value
408 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
409 return D3DERR_VIEWPORTHASNODEVICE;
413 if (!data
|| data
->dwSize
!= sizeof(*data
))
415 WARN("Transform data is NULL or size is incorrect, returning DDERR_INVALIDPARAMS\n");
416 return DDERR_INVALIDPARAMS
;
418 if (!(dwFlags
& (D3DTRANSFORM_UNCLIPPED
| D3DTRANSFORM_CLIPPED
)))
420 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
421 return DDERR_INVALIDPARAMS
;
424 wined3d_mutex_lock();
426 viewport_activate(viewport
, TRUE
);
428 wined3d_device_get_transform(device
->wined3d_device
,
429 D3DTRANSFORMSTATE_VIEW
, (struct wined3d_matrix
*)&view_mat
);
430 wined3d_device_get_transform(device
->wined3d_device
,
431 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix
*)&world_mat
);
432 wined3d_device_get_transform(device
->wined3d_device
,
433 WINED3D_TS_PROJECTION
, (struct wined3d_matrix
*)&proj_mat
);
434 multiply_matrix(&mat
, &view_mat
, &world_mat
);
435 multiply_matrix(&mat
, &proj_mat
, &mat
);
437 /* The pointer is not tested against NULL on Windows. */
438 if (dwFlags
& D3DTRANSFORM_CLIPPED
)
444 for(i
= 0; i
< dwVertexCount
; i
++)
446 in
= (struct transform_vertices_vertex
*)((char *)data
->lpIn
+ data
->dwInSize
* i
);
447 out
= (struct transform_vertices_vertex
*)((char *)data
->lpOut
+ data
->dwOutSize
* i
);
449 x
= (in
->x
* mat
._11
) + (in
->y
* mat
._21
) + (in
->z
* mat
._31
) + mat
._41
;
450 y
= (in
->x
* mat
._12
) + (in
->y
* mat
._22
) + (in
->z
* mat
._32
) + mat
._42
;
451 z
= (in
->x
* mat
._13
) + (in
->y
* mat
._23
) + (in
->z
* mat
._33
) + mat
._43
;
452 w
= (in
->x
* mat
._14
) + (in
->y
* mat
._24
) + (in
->z
* mat
._34
) + mat
._44
;
454 if(dwFlags
& D3DTRANSFORM_CLIPPED
)
456 /* If clipping is enabled, Windows assumes that outH is
457 * a valid pointer. */
458 outH
[i
].u1
.hx
= (x
- device
->legacy_clipspace
._41
* w
) / device
->legacy_clipspace
._11
;
459 outH
[i
].u2
.hy
= (y
- device
->legacy_clipspace
._42
* w
) / device
->legacy_clipspace
._22
;
460 outH
[i
].u3
.hz
= (z
- device
->legacy_clipspace
._43
* w
) / device
->legacy_clipspace
._33
;
464 outH
[i
].dwFlags
|= D3DCLIP_RIGHT
;
466 outH
[i
].dwFlags
|= D3DCLIP_LEFT
;
468 outH
[i
].dwFlags
|= D3DCLIP_TOP
;
470 outH
[i
].dwFlags
|= D3DCLIP_BOTTOM
;
472 outH
[i
].dwFlags
|= D3DCLIP_FRONT
;
474 outH
[i
].dwFlags
|= D3DCLIP_BACK
;
476 *offscreen
&= outH
[i
].dwFlags
;
480 /* Looks like native just drops the vertex, leaves whatever data
481 * it has in the output buffer and goes on with the next vertex.
482 * The exact scheme hasn't been figured out yet, but windows
483 * definitely writes something there.
494 x
*= w
; y
*= w
; z
*= w
;
496 out
->x
= (x
+ 1.0f
) * vp
.dwWidth
* 0.5 + vp
.dwX
;
497 out
->y
= (-y
+ 1.0f
) * vp
.dwHeight
* 0.5 + vp
.dwY
;
500 out
->payload
= in
->payload
;
503 if (activate
&& device
->current_viewport
)
504 viewport_activate(device
->current_viewport
, TRUE
);
506 wined3d_mutex_unlock();
512 /*****************************************************************************
513 * IDirect3DViewport3::LightElements
515 * The DirectX 5.0 sdk says that it's not implemented
523 *****************************************************************************/
524 static HRESULT WINAPI
d3d_viewport_LightElements(IDirect3DViewport3
*iface
,
525 DWORD element_count
, D3DLIGHTDATA
*data
)
527 TRACE("iface %p, element_count %u, data %p.\n", iface
, element_count
, data
);
529 return DDERR_UNSUPPORTED
;
532 static HRESULT WINAPI
d3d_viewport_SetBackground(IDirect3DViewport3
*iface
, D3DMATERIALHANDLE material
)
534 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
535 struct d3d_material
*m
;
537 TRACE("iface %p, material %#x.\n", iface
, material
);
539 wined3d_mutex_lock();
541 if (!(m
= ddraw_get_object(&viewport
->ddraw
->d3ddevice
->handle_table
, material
- 1, DDRAW_HANDLE_MATERIAL
)))
543 WARN("Invalid material handle %#x.\n", material
);
544 wined3d_mutex_unlock();
545 return DDERR_INVALIDPARAMS
;
548 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
549 m
->mat
.u
.diffuse
.u1
.r
, m
->mat
.u
.diffuse
.u2
.g
,
550 m
->mat
.u
.diffuse
.u3
.b
, m
->mat
.u
.diffuse
.u4
.a
);
551 viewport
->background
= m
;
553 wined3d_mutex_unlock();
558 /*****************************************************************************
559 * IDirect3DViewport3::GetBackground
561 * Returns the material handle assigned to the background of the viewport
564 * lphMat: Address to store the handle
565 * lpValid: is set to FALSE if no background is set, TRUE if one is set
570 *****************************************************************************/
571 static HRESULT WINAPI
d3d_viewport_GetBackground(IDirect3DViewport3
*iface
,
572 D3DMATERIALHANDLE
*material
, BOOL
*valid
)
574 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
576 TRACE("iface %p, material %p, valid %p.\n", iface
, material
, valid
);
578 wined3d_mutex_lock();
580 *valid
= !!viewport
->background
;
582 *material
= viewport
->background
? viewport
->background
->Handle
: 0;
583 wined3d_mutex_unlock();
588 /*****************************************************************************
589 * IDirect3DViewport3::SetBackgroundDepth
591 * Sets a surface that represents the background depth. Its contents are
592 * used to set the depth buffer in IDirect3DViewport3::Clear
595 * lpDDSurface: Surface to set
597 * Returns: D3D_OK, because it's a stub
599 *****************************************************************************/
600 static HRESULT WINAPI
d3d_viewport_SetBackgroundDepth(IDirect3DViewport3
*iface
, IDirectDrawSurface
*surface
)
602 FIXME("iface %p, surface %p stub!\n", iface
, surface
);
607 /*****************************************************************************
608 * IDirect3DViewport3::GetBackgroundDepth
610 * Returns the surface that represents the depth field
613 * lplpDDSurface: Address to store the interface pointer
614 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
617 * D3D_OK, because it's a stub
618 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
620 *****************************************************************************/
621 static HRESULT WINAPI
d3d_viewport_GetBackgroundDepth(IDirect3DViewport3
*iface
,
622 IDirectDrawSurface
**surface
, BOOL
*valid
)
624 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, surface
, valid
);
629 /*****************************************************************************
630 * IDirect3DViewport3::Clear
632 * Clears the render target and / or the z buffer
635 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
637 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
638 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
642 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
643 * The return value of IDirect3DDevice7::Clear
645 *****************************************************************************/
646 static HRESULT WINAPI
d3d_viewport_Clear(IDirect3DViewport3
*iface
,
647 DWORD rect_count
, D3DRECT
*rects
, DWORD flags
)
649 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
650 DWORD color
= 0x00000000;
652 IDirect3DViewport3
*current_viewport
;
653 IDirect3DDevice3
*d3d_device3
;
655 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface
, rect_count
, rects
, flags
);
657 if (!rects
|| !rect_count
)
659 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count
, rects
);
663 if (This
->active_device
== NULL
) {
664 ERR(" Trying to clear a viewport not attached to a device!\n");
665 return D3DERR_VIEWPORTHASNODEVICE
;
667 d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
669 wined3d_mutex_lock();
671 if (flags
& D3DCLEAR_TARGET
)
673 if (!This
->background
)
674 WARN("No background material set.\n");
676 color
= D3DRGBA(This
->background
->mat
.u
.diffuse
.u1
.r
,
677 This
->background
->mat
.u
.diffuse
.u2
.g
,
678 This
->background
->mat
.u
.diffuse
.u3
.b
,
679 This
->background
->mat
.u
.diffuse
.u4
.a
);
682 /* Need to temporarily activate the viewport to clear it. The previously
683 * active one will be restored afterwards. */
684 viewport_activate(This
, TRUE
);
686 hr
= IDirect3DDevice7_Clear(&This
->active_device
->IDirect3DDevice7_iface
, rect_count
, rects
,
687 flags
& (D3DCLEAR_ZBUFFER
| D3DCLEAR_TARGET
), color
, 1.0, 0x00000000);
689 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
691 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
692 viewport_activate(vp
, TRUE
);
693 IDirect3DViewport3_Release(current_viewport
);
696 wined3d_mutex_unlock();
701 /*****************************************************************************
702 * IDirect3DViewport3::AddLight
704 * Adds an light to the viewport
707 * lpDirect3DLight: Interface of the light to add
711 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
712 * DDERR_INVALIDPARAMS if there are 8 lights or more
714 *****************************************************************************/
715 static HRESULT WINAPI
d3d_viewport_AddLight(IDirect3DViewport3
*iface
, IDirect3DLight
*lpDirect3DLight
)
717 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
718 struct d3d_light
*light_impl
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
720 DWORD map
= This
->map_lights
;
722 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
724 wined3d_mutex_lock();
726 if (This
->num_lights
>= 8)
728 wined3d_mutex_unlock();
729 return DDERR_INVALIDPARAMS
;
732 /* Find a light number and update both light and viewports objects accordingly */
738 light_impl
->dwLightIndex
= i
;
740 This
->map_lights
|= 1<<i
;
742 /* Add the light in the 'linked' chain */
743 list_add_head(&This
->light_list
, &light_impl
->entry
);
744 IDirect3DLight_AddRef(lpDirect3DLight
);
746 /* Attach the light to the viewport */
747 light_impl
->active_viewport
= This
;
749 /* If active, activate the light */
750 if (This
->active_device
&& light_impl
->light
.dwFlags
& D3DLIGHT_ACTIVE
)
752 /* Disable the flag so that light_activate actually does its job. */
753 light_impl
->light
.dwFlags
&= ~D3DLIGHT_ACTIVE
;
754 light_activate(light_impl
);
757 wined3d_mutex_unlock();
762 /*****************************************************************************
763 * IDirect3DViewport3::DeleteLight
765 * Deletes a light from the viewports' light list
768 * lpDirect3DLight: Light to delete
772 * DDERR_INVALIDPARAMS if the light wasn't found
774 *****************************************************************************/
775 static HRESULT WINAPI
d3d_viewport_DeleteLight(IDirect3DViewport3
*iface
, IDirect3DLight
*lpDirect3DLight
)
777 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
778 struct d3d_light
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
780 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
782 wined3d_mutex_lock();
784 if (l
->active_viewport
!= viewport
)
786 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
787 wined3d_mutex_unlock();
788 return DDERR_INVALIDPARAMS
;
792 list_remove(&l
->entry
);
793 l
->active_viewport
= NULL
;
794 IDirect3DLight_Release(lpDirect3DLight
);
795 --viewport
->num_lights
;
796 viewport
->map_lights
&= ~(1 << l
->dwLightIndex
);
798 wined3d_mutex_unlock();
803 /*****************************************************************************
804 * IDirect3DViewport::NextLight
806 * Enumerates the lights associated with the viewport
809 * lpDirect3DLight: Light to start with
810 * lplpDirect3DLight: Address to store the successor to
813 * D3D_OK, because it's a stub
815 *****************************************************************************/
816 static HRESULT WINAPI
d3d_viewport_NextLight(IDirect3DViewport3
*iface
,
817 IDirect3DLight
*lpDirect3DLight
, IDirect3DLight
**lplpDirect3DLight
, DWORD flags
)
819 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
820 struct d3d_light
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
824 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
825 iface
, lpDirect3DLight
, lplpDirect3DLight
, flags
);
827 if (!lplpDirect3DLight
)
828 return DDERR_INVALIDPARAMS
;
830 wined3d_mutex_lock();
835 if (!l
|| l
->active_viewport
!= viewport
)
838 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
842 entry
= list_next(&viewport
->light_list
, &l
->entry
);
846 entry
= list_head(&viewport
->light_list
);
850 entry
= list_tail(&viewport
->light_list
);
855 WARN("Invalid flags %#x.\n", flags
);
861 *lplpDirect3DLight
= (IDirect3DLight
*)LIST_ENTRY(entry
, struct d3d_light
, entry
);
862 IDirect3DLight_AddRef(*lplpDirect3DLight
);
867 *lplpDirect3DLight
= NULL
;
868 hr
= DDERR_INVALIDPARAMS
;
871 wined3d_mutex_unlock();
876 /*****************************************************************************
877 * IDirect3DViewport2 Methods.
878 *****************************************************************************/
880 /*****************************************************************************
881 * IDirect3DViewport3::GetViewport2
883 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
884 * Similar to IDirect3DViewport3::GetViewport
887 * lpData: Pointer to the structure to fill
891 * DDERR_INVALIDPARAMS if the viewport was set with
892 * IDirect3DViewport3::SetViewport
893 * DDERR_INVALIDPARAMS if Data is NULL
895 *****************************************************************************/
896 static HRESULT WINAPI
d3d_viewport_GetViewport2(IDirect3DViewport3
*iface
, D3DVIEWPORT2
*lpData
)
898 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
901 TRACE("iface %p, data %p.\n", iface
, lpData
);
903 wined3d_mutex_lock();
904 dwSize
= lpData
->dwSize
;
906 memcpy(lpData
, &(This
->viewports
.vp2
), dwSize
);
909 vp2
.dwSize
= sizeof(vp2
);
910 vp2
.dwX
= This
->viewports
.vp1
.dwX
;
911 vp2
.dwY
= This
->viewports
.vp1
.dwY
;
912 vp2
.dwWidth
= This
->viewports
.vp1
.dwWidth
;
913 vp2
.dwHeight
= This
->viewports
.vp1
.dwHeight
;
916 vp2
.dvClipWidth
= 0.0;
917 vp2
.dvClipHeight
= 0.0;
918 vp2
.dvMinZ
= This
->viewports
.vp1
.dvMinZ
;
919 vp2
.dvMaxZ
= This
->viewports
.vp1
.dvMaxZ
;
920 memcpy(lpData
, &vp2
, dwSize
);
925 TRACE(" returning D3DVIEWPORT2 :\n");
926 _dump_D3DVIEWPORT2(lpData
);
929 wined3d_mutex_unlock();
934 /*****************************************************************************
935 * IDirect3DViewport3::SetViewport2
937 * Sets the viewport from a D3DVIEWPORT2 structure
940 * lpData: Viewport to set
945 *****************************************************************************/
946 static HRESULT WINAPI
d3d_viewport_SetViewport2(IDirect3DViewport3
*iface
, D3DVIEWPORT2
*lpData
)
948 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
949 IDirect3DViewport3
*current_viewport
;
951 TRACE("iface %p, data %p.\n", iface
, lpData
);
955 TRACE(" getting D3DVIEWPORT2 :\n");
956 _dump_D3DVIEWPORT2(lpData
);
959 wined3d_mutex_lock();
962 memset(&(This
->viewports
.vp2
), 0, sizeof(This
->viewports
.vp2
));
963 memcpy(&(This
->viewports
.vp2
), lpData
, lpData
->dwSize
);
965 if (This
->active_device
)
967 IDirect3DDevice3
*d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
968 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
970 if (current_viewport
== iface
) viewport_activate(This
, FALSE
);
971 IDirect3DViewport3_Release(current_viewport
);
975 wined3d_mutex_unlock();
980 /*****************************************************************************
981 * IDirect3DViewport3 Methods.
982 *****************************************************************************/
984 /*****************************************************************************
985 * IDirect3DViewport3::SetBackgroundDepth2
987 * Sets a IDirectDrawSurface4 surface as the background depth surface
990 * lpDDS: Surface to set
993 * D3D_OK, because it's stub
995 *****************************************************************************/
996 static HRESULT WINAPI
d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3
*iface
,
997 IDirectDrawSurface4
*surface
)
999 FIXME("iface %p, surface %p stub!\n", iface
, surface
);
1004 /*****************************************************************************
1005 * IDirect3DViewport3::GetBackgroundDepth2
1007 * Returns the IDirect3DSurface4 interface to the background depth surface
1010 * lplpDDS: Address to store the interface pointer at
1011 * lpValid: Set to true if a surface is assigned
1014 * D3D_OK because it's a stub
1016 *****************************************************************************/
1017 static HRESULT WINAPI
d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3
*iface
,
1018 IDirectDrawSurface4
**surface
, BOOL
*valid
)
1020 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, surface
, valid
);
1025 /*****************************************************************************
1026 * IDirect3DViewport3::Clear2
1028 * Another clearing method
1031 * Count: Number of rectangles to clear
1032 * Rects: Rectangle array to clear
1033 * Flags: Some flags :)
1034 * Color: Color to fill the render target with
1035 * Z: Value to fill the depth buffer with
1036 * Stencil: Value to fill the stencil bits with
1040 *****************************************************************************/
1041 static HRESULT WINAPI
d3d_viewport_Clear2(IDirect3DViewport3
*iface
, DWORD rect_count
,
1042 D3DRECT
*rects
, DWORD flags
, DWORD color
, D3DVALUE depth
, DWORD stencil
)
1044 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
1046 IDirect3DViewport3
*current_viewport
;
1047 IDirect3DDevice3
*d3d_device3
;
1049 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1050 iface
, rect_count
, rects
, flags
, color
, depth
, stencil
);
1052 if (!rects
|| !rect_count
)
1054 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count
, rects
);
1058 wined3d_mutex_lock();
1060 if (!viewport
->active_device
)
1062 WARN("Trying to clear a viewport not attached to a device.\n");
1063 wined3d_mutex_unlock();
1064 return D3DERR_VIEWPORTHASNODEVICE
;
1066 d3d_device3
= &viewport
->active_device
->IDirect3DDevice3_iface
;
1067 /* Need to temporarily activate viewport to clear it. Previously active
1068 * one will be restored afterwards. */
1069 viewport_activate(viewport
, TRUE
);
1071 hr
= IDirect3DDevice7_Clear(&viewport
->active_device
->IDirect3DDevice7_iface
,
1072 rect_count
, rects
, flags
, color
, depth
, stencil
);
1073 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
1075 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
1076 viewport_activate(vp
, TRUE
);
1077 IDirect3DViewport3_Release(current_viewport
);
1080 wined3d_mutex_unlock();
1085 /*****************************************************************************
1087 *****************************************************************************/
1089 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl
=
1091 /*** IUnknown Methods ***/
1092 d3d_viewport_QueryInterface
,
1093 d3d_viewport_AddRef
,
1094 d3d_viewport_Release
,
1095 /*** IDirect3DViewport Methods */
1096 d3d_viewport_Initialize
,
1097 d3d_viewport_GetViewport
,
1098 d3d_viewport_SetViewport
,
1099 d3d_viewport_TransformVertices
,
1100 d3d_viewport_LightElements
,
1101 d3d_viewport_SetBackground
,
1102 d3d_viewport_GetBackground
,
1103 d3d_viewport_SetBackgroundDepth
,
1104 d3d_viewport_GetBackgroundDepth
,
1106 d3d_viewport_AddLight
,
1107 d3d_viewport_DeleteLight
,
1108 d3d_viewport_NextLight
,
1109 /*** IDirect3DViewport2 Methods ***/
1110 d3d_viewport_GetViewport2
,
1111 d3d_viewport_SetViewport2
,
1112 /*** IDirect3DViewport3 Methods ***/
1113 d3d_viewport_SetBackgroundDepth2
,
1114 d3d_viewport_GetBackgroundDepth2
,
1115 d3d_viewport_Clear2
,
1118 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
1120 if (!iface
) return NULL
;
1121 assert(iface
->lpVtbl
== &d3d_viewport_vtbl
);
1122 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1125 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2
*iface
)
1127 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1128 if (!iface
) return NULL
;
1129 assert(iface
->lpVtbl
== (IDirect3DViewport2Vtbl
*)&d3d_viewport_vtbl
);
1130 return CONTAINING_RECORD((IDirect3DViewport3
*)iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1133 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport(IDirect3DViewport
*iface
)
1135 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1136 if (!iface
) return NULL
;
1137 assert(iface
->lpVtbl
== (IDirect3DViewportVtbl
*)&d3d_viewport_vtbl
);
1138 return CONTAINING_RECORD((IDirect3DViewport3
*)iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1141 void d3d_viewport_init(struct d3d_viewport
*viewport
, struct ddraw
*ddraw
)
1143 viewport
->IDirect3DViewport3_iface
.lpVtbl
= &d3d_viewport_vtbl
;
1145 viewport
->ddraw
= ddraw
;
1146 viewport
->use_vp2
= 0xff;
1147 list_init(&viewport
->light_list
);