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