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
21 #include "wine/port.h"
23 #include "ddraw_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
27 /*****************************************************************************
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
,
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 /*****************************************************************************
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
;
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 */
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
;
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
);
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
;
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
;
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 /*****************************************************************************
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
160 * refiid: Interface id queried for
161 * obj: Address to write the interface pointer to
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
);
182 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
185 return E_NOINTERFACE
;
188 /*****************************************************************************
189 * IDirect3DViewport3::AddRef
191 * Increases the 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
);
207 /*****************************************************************************
208 * IDirect3DViewport3::Release
210 * Reduces the refcount. If it falls to 0, the interface is released
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
);
229 /*****************************************************************************
230 * IDirect3DViewport Methods.
231 *****************************************************************************/
233 /*****************************************************************************
234 * IDirect3DViewport3::Initialize
236 * No-op initialization.
239 * Direct3D: The direct3D device this viewport is assigned to
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 static HRESULT WINAPI
d3d_viewport_GetViewport(IDirect3DViewport3
*iface
, D3DVIEWPORT
*vp
)
254 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
257 TRACE("iface %p, vp %p.\n", iface
, vp
);
260 return DDERR_INVALIDPARAMS
;
262 wined3d_mutex_lock();
265 if (!viewport
->use_vp2
)
267 memcpy(vp
, &viewport
->viewports
.vp1
, size
);
273 vp1
.dwSize
= sizeof(vp1
);
274 vp1
.dwX
= viewport
->viewports
.vp2
.dwX
;
275 vp1
.dwY
= viewport
->viewports
.vp2
.dwY
;
276 vp1
.dwWidth
= viewport
->viewports
.vp2
.dwWidth
;
277 vp1
.dwHeight
= viewport
->viewports
.vp2
.dwHeight
;
282 vp1
.dvMinZ
= viewport
->viewports
.vp2
.dvMinZ
;
283 vp1
.dvMaxZ
= viewport
->viewports
.vp2
.dvMaxZ
;
284 memcpy(vp
, &vp1
, size
);
289 TRACE(" returning D3DVIEWPORT :\n");
290 _dump_D3DVIEWPORT(vp
);
293 wined3d_mutex_unlock();
298 static HRESULT WINAPI
d3d_viewport_SetViewport(IDirect3DViewport3
*iface
, D3DVIEWPORT
*vp
)
300 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
301 struct d3d_device
*device
= viewport
->active_device
;
302 struct wined3d_sub_resource_desc rt_desc
;
303 struct wined3d_rendertarget_view
*rtv
;
304 IDirect3DViewport3
*current_viewport
;
305 struct ddraw_surface
*surface
;
307 TRACE("iface %p, vp %p.\n", iface
, vp
);
310 return DDERR_INVALIDPARAMS
;
314 TRACE(" getting D3DVIEWPORT :\n");
315 _dump_D3DVIEWPORT(vp
);
320 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
321 return D3DERR_VIEWPORTHASNODEVICE
;
324 wined3d_mutex_lock();
326 if (device
->version
> 1)
328 if (!(rtv
= wined3d_device_get_rendertarget_view(device
->wined3d_device
, 0)))
330 wined3d_mutex_unlock();
331 return DDERR_INVALIDCAPS
;
333 surface
= wined3d_rendertarget_view_get_sub_resource_parent(rtv
);
334 wined3d_texture_get_sub_resource_desc(surface
->wined3d_texture
, surface
->sub_resource_idx
, &rt_desc
);
336 if (vp
->dwX
> rt_desc
.width
|| vp
->dwWidth
> rt_desc
.width
- vp
->dwX
337 || vp
->dwY
> rt_desc
.height
|| vp
->dwHeight
> rt_desc
.height
- vp
->dwY
)
339 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
340 wined3d_mutex_unlock();
341 return DDERR_INVALIDPARAMS
;
345 viewport
->use_vp2
= 0;
346 memset(&viewport
->viewports
.vp1
, 0, sizeof(viewport
->viewports
.vp1
));
347 memcpy(&viewport
->viewports
.vp1
, vp
, vp
->dwSize
);
349 /* Empirical testing on a couple of d3d1 games showed that these values
350 * should be ignored. */
351 viewport
->viewports
.vp1
.dvMinZ
= 0.0;
352 viewport
->viewports
.vp1
.dvMaxZ
= 1.0;
354 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device
->IDirect3DDevice3_iface
, ¤t_viewport
)))
356 if (current_viewport
== iface
)
357 viewport_activate(viewport
, FALSE
);
358 IDirect3DViewport3_Release(current_viewport
);
361 wined3d_mutex_unlock();
366 /*****************************************************************************
367 * IDirect3DViewport3::TransformVertices
369 * Transforms vertices by the transformation matrix.
371 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
372 * so it's tempting to forward it to there. However, there are some
373 * tiny differences. First, the lpOffscreen flag that is reported back,
374 * then there is the homogeneous vertex that is generated. Also there's a lack
375 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
376 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
377 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
381 * dwVertexCount: The number of vertices to be transformed
382 * data: Pointer to the vertex input / output data.
383 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
384 * offscreen: Logical AND of the planes that clipped the vertices if clipping
385 * is on. 0 if clipping is off.
389 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
390 * DDERR_INVALIDPARAMS if no clipping flag is specified
392 *****************************************************************************/
393 struct transform_vertices_vertex
395 float x
, y
, z
, w
; /* w is unused in input data. */
402 static HRESULT WINAPI
d3d_viewport_TransformVertices(IDirect3DViewport3
*iface
,
403 DWORD dwVertexCount
, D3DTRANSFORMDATA
*data
, DWORD dwFlags
, DWORD
*offscreen
)
405 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
406 D3DVIEWPORT vp
= viewport
->viewports
.vp1
;
407 D3DMATRIX view_mat
, world_mat
, proj_mat
, mat
;
408 struct transform_vertices_vertex
*in
, *out
;
412 struct d3d_device
*device
= viewport
->active_device
;
413 BOOL activate
= device
->current_viewport
!= viewport
;
415 TRACE("iface %p, vertex_count %u, data %p, flags %#x, offscreen %p.\n",
416 iface
, dwVertexCount
, data
, dwFlags
, offscreen
);
418 /* Tests on windows show that Windows crashes when this occurs,
419 * so don't return the (intuitive) return value
422 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
423 return D3DERR_VIEWPORTHASNODEVICE;
427 if (!data
|| data
->dwSize
!= sizeof(*data
))
429 WARN("Transform data is NULL or size is incorrect, returning DDERR_INVALIDPARAMS\n");
430 return DDERR_INVALIDPARAMS
;
432 if (!(dwFlags
& (D3DTRANSFORM_UNCLIPPED
| D3DTRANSFORM_CLIPPED
)))
434 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
435 return DDERR_INVALIDPARAMS
;
438 wined3d_mutex_lock();
440 viewport_activate(viewport
, TRUE
);
442 wined3d_device_get_transform(device
->wined3d_device
,
443 D3DTRANSFORMSTATE_VIEW
, (struct wined3d_matrix
*)&view_mat
);
444 wined3d_device_get_transform(device
->wined3d_device
,
445 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix
*)&world_mat
);
446 wined3d_device_get_transform(device
->wined3d_device
,
447 WINED3D_TS_PROJECTION
, (struct wined3d_matrix
*)&proj_mat
);
448 multiply_matrix(&mat
, &view_mat
, &world_mat
);
449 multiply_matrix(&mat
, &proj_mat
, &mat
);
451 /* The pointer is not tested against NULL on Windows. */
452 if (dwFlags
& D3DTRANSFORM_CLIPPED
)
458 for(i
= 0; i
< dwVertexCount
; i
++)
460 in
= (struct transform_vertices_vertex
*)((char *)data
->lpIn
+ data
->dwInSize
* i
);
461 out
= (struct transform_vertices_vertex
*)((char *)data
->lpOut
+ data
->dwOutSize
* i
);
463 x
= (in
->x
* mat
._11
) + (in
->y
* mat
._21
) + (in
->z
* mat
._31
) + mat
._41
;
464 y
= (in
->x
* mat
._12
) + (in
->y
* mat
._22
) + (in
->z
* mat
._32
) + mat
._42
;
465 z
= (in
->x
* mat
._13
) + (in
->y
* mat
._23
) + (in
->z
* mat
._33
) + mat
._43
;
466 w
= (in
->x
* mat
._14
) + (in
->y
* mat
._24
) + (in
->z
* mat
._34
) + mat
._44
;
468 if(dwFlags
& D3DTRANSFORM_CLIPPED
)
470 /* If clipping is enabled, Windows assumes that outH is
471 * a valid pointer. */
472 outH
[i
].u1
.hx
= (x
- device
->legacy_clipspace
._41
* w
) / device
->legacy_clipspace
._11
;
473 outH
[i
].u2
.hy
= (y
- device
->legacy_clipspace
._42
* w
) / device
->legacy_clipspace
._22
;
474 outH
[i
].u3
.hz
= (z
- device
->legacy_clipspace
._43
* w
) / device
->legacy_clipspace
._33
;
478 outH
[i
].dwFlags
|= D3DCLIP_RIGHT
;
480 outH
[i
].dwFlags
|= D3DCLIP_LEFT
;
482 outH
[i
].dwFlags
|= D3DCLIP_TOP
;
484 outH
[i
].dwFlags
|= D3DCLIP_BOTTOM
;
486 outH
[i
].dwFlags
|= D3DCLIP_FRONT
;
488 outH
[i
].dwFlags
|= D3DCLIP_BACK
;
490 *offscreen
&= outH
[i
].dwFlags
;
494 /* Looks like native just drops the vertex, leaves whatever data
495 * it has in the output buffer and goes on with the next vertex.
496 * The exact scheme hasn't been figured out yet, but windows
497 * definitely writes something there.
508 x
*= w
; y
*= w
; z
*= w
;
510 out
->x
= (x
+ 1.0f
) * vp
.dwWidth
* 0.5 + vp
.dwX
;
511 out
->y
= (-y
+ 1.0f
) * vp
.dwHeight
* 0.5 + vp
.dwY
;
514 out
->payload
= in
->payload
;
517 if (activate
&& device
->current_viewport
)
518 viewport_activate(device
->current_viewport
, TRUE
);
520 wined3d_mutex_unlock();
526 /*****************************************************************************
527 * IDirect3DViewport3::LightElements
529 * The DirectX 5.0 sdk says that it's not implemented
537 *****************************************************************************/
538 static HRESULT WINAPI
d3d_viewport_LightElements(IDirect3DViewport3
*iface
,
539 DWORD element_count
, D3DLIGHTDATA
*data
)
541 TRACE("iface %p, element_count %u, data %p.\n", iface
, element_count
, data
);
543 return DDERR_UNSUPPORTED
;
546 static HRESULT WINAPI
d3d_viewport_SetBackground(IDirect3DViewport3
*iface
, D3DMATERIALHANDLE material
)
548 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
549 struct d3d_material
*m
;
551 TRACE("iface %p, material %#x.\n", iface
, material
);
553 wined3d_mutex_lock();
555 if (!(m
= ddraw_get_object(&viewport
->ddraw
->d3ddevice
->handle_table
, material
- 1, DDRAW_HANDLE_MATERIAL
)))
557 WARN("Invalid material handle %#x.\n", material
);
558 wined3d_mutex_unlock();
559 return DDERR_INVALIDPARAMS
;
562 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
563 m
->mat
.u
.diffuse
.u1
.r
, m
->mat
.u
.diffuse
.u2
.g
,
564 m
->mat
.u
.diffuse
.u3
.b
, m
->mat
.u
.diffuse
.u4
.a
);
565 viewport
->background
= m
;
567 wined3d_mutex_unlock();
572 /*****************************************************************************
573 * IDirect3DViewport3::GetBackground
575 * Returns the material handle assigned to the background of the viewport
578 * lphMat: Address to store the handle
579 * lpValid: is set to FALSE if no background is set, TRUE if one is set
584 *****************************************************************************/
585 static HRESULT WINAPI
d3d_viewport_GetBackground(IDirect3DViewport3
*iface
,
586 D3DMATERIALHANDLE
*material
, BOOL
*valid
)
588 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
590 TRACE("iface %p, material %p, valid %p.\n", iface
, material
, valid
);
592 wined3d_mutex_lock();
594 *valid
= !!viewport
->background
;
596 *material
= viewport
->background
? viewport
->background
->Handle
: 0;
597 wined3d_mutex_unlock();
602 /*****************************************************************************
603 * IDirect3DViewport3::SetBackgroundDepth
605 * Sets a surface that represents the background depth. Its contents are
606 * used to set the depth buffer in IDirect3DViewport3::Clear
609 * lpDDSurface: Surface to set
611 * Returns: D3D_OK, because it's a stub
613 *****************************************************************************/
614 static HRESULT WINAPI
d3d_viewport_SetBackgroundDepth(IDirect3DViewport3
*iface
, IDirectDrawSurface
*surface
)
616 FIXME("iface %p, surface %p stub!\n", iface
, surface
);
621 /*****************************************************************************
622 * IDirect3DViewport3::GetBackgroundDepth
624 * Returns the surface that represents the depth field
627 * lplpDDSurface: Address to store the interface pointer
628 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
631 * D3D_OK, because it's a stub
632 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
634 *****************************************************************************/
635 static HRESULT WINAPI
d3d_viewport_GetBackgroundDepth(IDirect3DViewport3
*iface
,
636 IDirectDrawSurface
**surface
, BOOL
*valid
)
638 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, surface
, valid
);
643 /*****************************************************************************
644 * IDirect3DViewport3::Clear
646 * Clears the render target and / or the z buffer
649 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
651 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
652 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
656 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
657 * The return value of IDirect3DDevice7::Clear
659 *****************************************************************************/
660 static HRESULT WINAPI
d3d_viewport_Clear(IDirect3DViewport3
*iface
,
661 DWORD rect_count
, D3DRECT
*rects
, DWORD flags
)
663 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
664 DWORD color
= 0x00000000;
666 IDirect3DViewport3
*current_viewport
;
667 IDirect3DDevice3
*d3d_device3
;
669 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface
, rect_count
, rects
, flags
);
671 if (!rects
|| !rect_count
)
673 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count
, rects
);
677 if (This
->active_device
== NULL
) {
678 ERR(" Trying to clear a viewport not attached to a device!\n");
679 return D3DERR_VIEWPORTHASNODEVICE
;
681 d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
683 wined3d_mutex_lock();
685 if (flags
& D3DCLEAR_TARGET
)
687 if (!This
->background
)
688 WARN("No background material set.\n");
690 color
= D3DRGBA(This
->background
->mat
.u
.diffuse
.u1
.r
,
691 This
->background
->mat
.u
.diffuse
.u2
.g
,
692 This
->background
->mat
.u
.diffuse
.u3
.b
,
693 This
->background
->mat
.u
.diffuse
.u4
.a
);
696 /* Need to temporarily activate the viewport to clear it. The previously
697 * active one will be restored afterwards. */
698 viewport_activate(This
, TRUE
);
700 hr
= IDirect3DDevice7_Clear(&This
->active_device
->IDirect3DDevice7_iface
, rect_count
, rects
,
701 flags
& (D3DCLEAR_ZBUFFER
| D3DCLEAR_TARGET
), color
, 1.0, 0x00000000);
703 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
705 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
706 viewport_activate(vp
, TRUE
);
707 IDirect3DViewport3_Release(current_viewport
);
710 wined3d_mutex_unlock();
715 /*****************************************************************************
716 * IDirect3DViewport3::AddLight
718 * Adds an light to the viewport
721 * lpDirect3DLight: Interface of the light to add
725 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
726 * DDERR_INVALIDPARAMS if there are 8 lights or more
728 *****************************************************************************/
729 static HRESULT WINAPI
d3d_viewport_AddLight(IDirect3DViewport3
*iface
, IDirect3DLight
*lpDirect3DLight
)
731 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
732 struct d3d_light
*light_impl
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
734 DWORD map
= This
->map_lights
;
736 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
738 wined3d_mutex_lock();
740 if (This
->num_lights
>= 8)
742 wined3d_mutex_unlock();
743 return DDERR_INVALIDPARAMS
;
746 /* Find a light number and update both light and viewports objects accordingly */
752 light_impl
->dwLightIndex
= i
;
754 This
->map_lights
|= 1<<i
;
756 /* Add the light in the 'linked' chain */
757 list_add_head(&This
->light_list
, &light_impl
->entry
);
758 IDirect3DLight_AddRef(lpDirect3DLight
);
760 /* Attach the light to the viewport */
761 light_impl
->active_viewport
= This
;
763 /* If active, activate the light */
764 if (This
->active_device
&& light_impl
->light
.dwFlags
& D3DLIGHT_ACTIVE
)
766 /* Disable the flag so that light_activate actually does its job. */
767 light_impl
->light
.dwFlags
&= ~D3DLIGHT_ACTIVE
;
768 light_activate(light_impl
);
771 wined3d_mutex_unlock();
776 /*****************************************************************************
777 * IDirect3DViewport3::DeleteLight
779 * Deletes a light from the viewports' light list
782 * lpDirect3DLight: Light to delete
786 * DDERR_INVALIDPARAMS if the light wasn't found
788 *****************************************************************************/
789 static HRESULT WINAPI
d3d_viewport_DeleteLight(IDirect3DViewport3
*iface
, IDirect3DLight
*lpDirect3DLight
)
791 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
792 struct d3d_light
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
794 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
796 wined3d_mutex_lock();
798 if (l
->active_viewport
!= viewport
)
800 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
801 wined3d_mutex_unlock();
802 return DDERR_INVALIDPARAMS
;
806 list_remove(&l
->entry
);
807 l
->active_viewport
= NULL
;
808 IDirect3DLight_Release(lpDirect3DLight
);
809 --viewport
->num_lights
;
810 viewport
->map_lights
&= ~(1 << l
->dwLightIndex
);
812 wined3d_mutex_unlock();
817 /*****************************************************************************
818 * IDirect3DViewport::NextLight
820 * Enumerates the lights associated with the viewport
823 * lpDirect3DLight: Light to start with
824 * lplpDirect3DLight: Address to store the successor to
827 * D3D_OK, because it's a stub
829 *****************************************************************************/
830 static HRESULT WINAPI
d3d_viewport_NextLight(IDirect3DViewport3
*iface
,
831 IDirect3DLight
*lpDirect3DLight
, IDirect3DLight
**lplpDirect3DLight
, DWORD flags
)
833 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
834 struct d3d_light
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
838 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
839 iface
, lpDirect3DLight
, lplpDirect3DLight
, flags
);
841 if (!lplpDirect3DLight
)
842 return DDERR_INVALIDPARAMS
;
844 wined3d_mutex_lock();
849 if (!l
|| l
->active_viewport
!= viewport
)
852 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
856 entry
= list_next(&viewport
->light_list
, &l
->entry
);
860 entry
= list_head(&viewport
->light_list
);
864 entry
= list_tail(&viewport
->light_list
);
869 WARN("Invalid flags %#x.\n", flags
);
875 *lplpDirect3DLight
= (IDirect3DLight
*)LIST_ENTRY(entry
, struct d3d_light
, entry
);
876 IDirect3DLight_AddRef(*lplpDirect3DLight
);
881 *lplpDirect3DLight
= NULL
;
882 hr
= DDERR_INVALIDPARAMS
;
885 wined3d_mutex_unlock();
890 /*****************************************************************************
891 * IDirect3DViewport2 Methods.
892 *****************************************************************************/
894 static HRESULT WINAPI
d3d_viewport_GetViewport2(IDirect3DViewport3
*iface
, D3DVIEWPORT2
*vp
)
896 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
899 TRACE("iface %p, vp %p.\n", iface
, vp
);
902 return DDERR_INVALIDPARAMS
;
904 wined3d_mutex_lock();
906 if (viewport
->use_vp2
)
908 memcpy(vp
, &viewport
->viewports
.vp2
, size
);
914 vp2
.dwSize
= sizeof(vp2
);
915 vp2
.dwX
= viewport
->viewports
.vp1
.dwX
;
916 vp2
.dwY
= viewport
->viewports
.vp1
.dwY
;
917 vp2
.dwWidth
= viewport
->viewports
.vp1
.dwWidth
;
918 vp2
.dwHeight
= viewport
->viewports
.vp1
.dwHeight
;
921 vp2
.dvClipWidth
= 0.0;
922 vp2
.dvClipHeight
= 0.0;
923 vp2
.dvMinZ
= viewport
->viewports
.vp1
.dvMinZ
;
924 vp2
.dvMaxZ
= viewport
->viewports
.vp1
.dvMaxZ
;
925 memcpy(vp
, &vp2
, size
);
930 TRACE(" returning D3DVIEWPORT2 :\n");
931 _dump_D3DVIEWPORT2(vp
);
934 wined3d_mutex_unlock();
939 static HRESULT WINAPI
d3d_viewport_SetViewport2(IDirect3DViewport3
*iface
, D3DVIEWPORT2
*vp
)
941 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
942 struct d3d_device
*device
= viewport
->active_device
;
943 struct wined3d_sub_resource_desc rt_desc
;
944 struct wined3d_rendertarget_view
*rtv
;
945 IDirect3DViewport3
*current_viewport
;
946 struct ddraw_surface
*surface
;
948 TRACE("iface %p, vp %p.\n", iface
, vp
);
951 return DDERR_INVALIDPARAMS
;
955 TRACE(" getting D3DVIEWPORT2 :\n");
956 _dump_D3DVIEWPORT2(vp
);
961 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
962 return D3DERR_VIEWPORTHASNODEVICE
;
965 wined3d_mutex_lock();
967 if (device
->version
> 1)
969 if (!(rtv
= wined3d_device_get_rendertarget_view(device
->wined3d_device
, 0)))
971 wined3d_mutex_unlock();
972 return DDERR_INVALIDCAPS
;
974 surface
= wined3d_rendertarget_view_get_sub_resource_parent(rtv
);
975 wined3d_texture_get_sub_resource_desc(surface
->wined3d_texture
, surface
->sub_resource_idx
, &rt_desc
);
977 if (vp
->dwX
> rt_desc
.width
|| vp
->dwWidth
> rt_desc
.width
- vp
->dwX
978 || vp
->dwY
> rt_desc
.height
|| vp
->dwHeight
> rt_desc
.height
- vp
->dwY
)
980 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
981 wined3d_mutex_unlock();
982 return DDERR_INVALIDPARAMS
;
986 viewport
->use_vp2
= 1;
987 memset(&viewport
->viewports
.vp2
, 0, sizeof(viewport
->viewports
.vp2
));
988 memcpy(&viewport
->viewports
.vp2
, vp
, vp
->dwSize
);
990 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device
->IDirect3DDevice3_iface
, ¤t_viewport
)))
992 if (current_viewport
== iface
)
993 viewport_activate(viewport
, FALSE
);
994 IDirect3DViewport3_Release(current_viewport
);
997 wined3d_mutex_unlock();
1002 /*****************************************************************************
1003 * IDirect3DViewport3 Methods.
1004 *****************************************************************************/
1006 /*****************************************************************************
1007 * IDirect3DViewport3::SetBackgroundDepth2
1009 * Sets a IDirectDrawSurface4 surface as the background depth surface
1012 * lpDDS: Surface to set
1015 * D3D_OK, because it's stub
1017 *****************************************************************************/
1018 static HRESULT WINAPI
d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3
*iface
,
1019 IDirectDrawSurface4
*surface
)
1021 FIXME("iface %p, surface %p stub!\n", iface
, surface
);
1026 /*****************************************************************************
1027 * IDirect3DViewport3::GetBackgroundDepth2
1029 * Returns the IDirect3DSurface4 interface to the background depth surface
1032 * lplpDDS: Address to store the interface pointer at
1033 * lpValid: Set to true if a surface is assigned
1036 * D3D_OK because it's a stub
1038 *****************************************************************************/
1039 static HRESULT WINAPI
d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3
*iface
,
1040 IDirectDrawSurface4
**surface
, BOOL
*valid
)
1042 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, surface
, valid
);
1047 /*****************************************************************************
1048 * IDirect3DViewport3::Clear2
1050 * Another clearing method
1053 * Count: Number of rectangles to clear
1054 * Rects: Rectangle array to clear
1055 * Flags: Some flags :)
1056 * Color: Color to fill the render target with
1057 * Z: Value to fill the depth buffer with
1058 * Stencil: Value to fill the stencil bits with
1062 *****************************************************************************/
1063 static HRESULT WINAPI
d3d_viewport_Clear2(IDirect3DViewport3
*iface
, DWORD rect_count
,
1064 D3DRECT
*rects
, DWORD flags
, DWORD color
, D3DVALUE depth
, DWORD stencil
)
1066 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
1068 IDirect3DViewport3
*current_viewport
;
1069 IDirect3DDevice3
*d3d_device3
;
1071 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1072 iface
, rect_count
, rects
, flags
, color
, depth
, stencil
);
1074 if (!rects
|| !rect_count
)
1076 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count
, rects
);
1080 wined3d_mutex_lock();
1082 if (!viewport
->active_device
)
1084 WARN("Trying to clear a viewport not attached to a device.\n");
1085 wined3d_mutex_unlock();
1086 return D3DERR_VIEWPORTHASNODEVICE
;
1088 d3d_device3
= &viewport
->active_device
->IDirect3DDevice3_iface
;
1089 /* Need to temporarily activate viewport to clear it. Previously active
1090 * one will be restored afterwards. */
1091 viewport_activate(viewport
, TRUE
);
1093 hr
= IDirect3DDevice7_Clear(&viewport
->active_device
->IDirect3DDevice7_iface
,
1094 rect_count
, rects
, flags
, color
, depth
, stencil
);
1095 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
1097 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
1098 viewport_activate(vp
, TRUE
);
1099 IDirect3DViewport3_Release(current_viewport
);
1102 wined3d_mutex_unlock();
1107 /*****************************************************************************
1109 *****************************************************************************/
1111 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl
=
1113 /*** IUnknown Methods ***/
1114 d3d_viewport_QueryInterface
,
1115 d3d_viewport_AddRef
,
1116 d3d_viewport_Release
,
1117 /*** IDirect3DViewport Methods */
1118 d3d_viewport_Initialize
,
1119 d3d_viewport_GetViewport
,
1120 d3d_viewport_SetViewport
,
1121 d3d_viewport_TransformVertices
,
1122 d3d_viewport_LightElements
,
1123 d3d_viewport_SetBackground
,
1124 d3d_viewport_GetBackground
,
1125 d3d_viewport_SetBackgroundDepth
,
1126 d3d_viewport_GetBackgroundDepth
,
1128 d3d_viewport_AddLight
,
1129 d3d_viewport_DeleteLight
,
1130 d3d_viewport_NextLight
,
1131 /*** IDirect3DViewport2 Methods ***/
1132 d3d_viewport_GetViewport2
,
1133 d3d_viewport_SetViewport2
,
1134 /*** IDirect3DViewport3 Methods ***/
1135 d3d_viewport_SetBackgroundDepth2
,
1136 d3d_viewport_GetBackgroundDepth2
,
1137 d3d_viewport_Clear2
,
1140 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
1142 if (!iface
) return NULL
;
1143 assert(iface
->lpVtbl
== &d3d_viewport_vtbl
);
1144 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1147 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2
*iface
)
1149 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1150 if (!iface
) return NULL
;
1151 assert(iface
->lpVtbl
== (IDirect3DViewport2Vtbl
*)&d3d_viewport_vtbl
);
1152 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1155 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport(IDirect3DViewport
*iface
)
1157 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1158 if (!iface
) return NULL
;
1159 assert(iface
->lpVtbl
== (IDirect3DViewportVtbl
*)&d3d_viewport_vtbl
);
1160 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1163 void d3d_viewport_init(struct d3d_viewport
*viewport
, struct ddraw
*ddraw
)
1165 viewport
->IDirect3DViewport3_iface
.lpVtbl
= &d3d_viewport_vtbl
;
1167 viewport
->ddraw
= ddraw
;
1168 viewport
->use_vp2
= 0xff;
1169 list_init(&viewport
->light_list
);