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 D3DMATRIX 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
,
40 multiply_matrix(&projection
, &clip_space
, &device
->legacy_projection
);
41 wined3d_device_set_transform(device
->wined3d_device
,
42 WINED3D_TS_PROJECTION
, (struct wined3d_matrix
*)&projection
);
43 device
->legacy_clipspace
= clip_space
;
46 /*****************************************************************************
49 * activates the viewport using IDirect3DDevice7::SetViewport
51 *****************************************************************************/
52 void viewport_activate(struct d3d_viewport
*This
, BOOL ignore_lights
)
54 struct wined3d_vec3 scale
, offset
;
59 struct d3d_light
*light
;
61 /* Activate all the lights associated with this context */
62 LIST_FOR_EACH_ENTRY(light
, &This
->light_list
, struct d3d_light
, entry
)
64 light_activate(light
);
68 if (This
->version
== DDRAW_VIEWPORT_VERSION_NONE
)
70 TRACE("Viewport data was not set.\n");
74 /* And copy the values in the structure used by the device */
75 if (This
->version
== DDRAW_VIEWPORT_VERSION_2
)
77 vp
.dwX
= This
->viewports
.vp2
.dwX
;
78 vp
.dwY
= This
->viewports
.vp2
.dwY
;
79 vp
.dwHeight
= This
->viewports
.vp2
.dwHeight
;
80 vp
.dwWidth
= This
->viewports
.vp2
.dwWidth
;
84 scale
.x
= 2.0f
/ This
->viewports
.vp2
.dvClipWidth
;
85 scale
.y
= 2.0f
/ This
->viewports
.vp2
.dvClipHeight
;
86 scale
.z
= 1.0f
/ (This
->viewports
.vp2
.dvMaxZ
- This
->viewports
.vp2
.dvMinZ
);
87 offset
.x
= -2.0f
* This
->viewports
.vp2
.dvClipX
/ This
->viewports
.vp2
.dvClipWidth
- 1.0f
;
88 offset
.y
= -2.0f
* This
->viewports
.vp2
.dvClipY
/ This
->viewports
.vp2
.dvClipHeight
+ 1.0f
;
89 offset
.z
= -This
->viewports
.vp2
.dvMinZ
/ (This
->viewports
.vp2
.dvMaxZ
- This
->viewports
.vp2
.dvMinZ
);
93 vp
.dwX
= This
->viewports
.vp1
.dwX
;
94 vp
.dwY
= This
->viewports
.vp1
.dwY
;
95 vp
.dwHeight
= This
->viewports
.vp1
.dwHeight
;
96 vp
.dwWidth
= This
->viewports
.vp1
.dwWidth
;
100 scale
.x
= 2.0f
* This
->viewports
.vp1
.dvScaleX
/ This
->viewports
.vp1
.dwWidth
;
101 scale
.y
= 2.0f
* This
->viewports
.vp1
.dvScaleY
/ This
->viewports
.vp1
.dwHeight
;
108 update_clip_space(This
->active_device
, &scale
, &offset
);
109 IDirect3DDevice7_SetViewport(&This
->active_device
->IDirect3DDevice7_iface
, &vp
);
112 void viewport_deactivate(struct d3d_viewport
*viewport
)
114 struct d3d_light
*light
;
116 LIST_FOR_EACH_ENTRY(light
, &viewport
->light_list
, struct d3d_light
, entry
)
118 light_deactivate(light
);
122 void viewport_alloc_active_light_index(struct d3d_light
*light
)
124 struct d3d_viewport
*vp
= light
->active_viewport
;
128 TRACE("vp %p, light %p, index %u, active_lights_count %u.\n",
129 vp
, light
, light
->active_light_index
, vp
->active_lights_count
);
131 if (light
->active_light_index
)
134 if (vp
->active_lights_count
>= DDRAW_MAX_ACTIVE_LIGHTS
)
138 LIST_FOR_EACH_ENTRY(l
, &vp
->light_list
, struct d3d_light
, entry
)
140 if (l
->active_light_index
)
142 WARN("Too many active lights, viewport %p, light %p, deactivating %p.\n", vp
, light
, l
);
145 /* Recycle active lights in a FIFO way. */
146 list_remove(&light
->entry
);
147 list_add_tail(&vp
->light_list
, &light
->entry
);
153 map
= ~vp
->map_lights
;
154 assert(vp
->active_lights_count
< DDRAW_MAX_ACTIVE_LIGHTS
&& map
);
155 i
= wined3d_bit_scan(&map
);
156 light
->active_light_index
= i
+ 1;
157 ++vp
->active_lights_count
;
158 vp
->map_lights
|= 1u << i
;
161 void viewport_free_active_light_index(struct d3d_light
*light
)
163 struct d3d_viewport
*vp
= light
->active_viewport
;
165 TRACE("vp %p, light %p, index %u, active_lights_count %u, map_lights %#x.\n",
166 vp
, light
, light
->active_light_index
, vp
->active_lights_count
, vp
->map_lights
);
168 if (!light
->active_light_index
)
171 assert(vp
->map_lights
& (1u << (light
->active_light_index
- 1)));
173 --vp
->active_lights_count
;
174 vp
->map_lights
&= ~(1u << (light
->active_light_index
- 1));
175 light
->active_light_index
= 0;
178 /*****************************************************************************
179 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
181 * Writes viewport information to TRACE
183 *****************************************************************************/
184 static void _dump_D3DVIEWPORT(const D3DVIEWPORT
*lpvp
)
186 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
187 lpvp
->dwSize
, lpvp
->dwX
, lpvp
->dwY
);
188 TRACE(" - dwWidth = %d dwHeight = %d\n",
189 lpvp
->dwWidth
, lpvp
->dwHeight
);
190 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
191 lpvp
->dvScaleX
, lpvp
->dvScaleY
);
192 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
193 lpvp
->dvMaxX
, lpvp
->dvMaxY
);
194 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
195 lpvp
->dvMinZ
, lpvp
->dvMaxZ
);
198 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2
*lpvp
)
200 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
201 lpvp
->dwSize
, lpvp
->dwX
, lpvp
->dwY
);
202 TRACE(" - dwWidth = %d dwHeight = %d\n",
203 lpvp
->dwWidth
, lpvp
->dwHeight
);
204 TRACE(" - dvClipX = %f dvClipY = %f\n",
205 lpvp
->dvClipX
, lpvp
->dvClipY
);
206 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
207 lpvp
->dvClipWidth
, lpvp
->dvClipHeight
);
208 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
209 lpvp
->dvMinZ
, lpvp
->dvMaxZ
);
212 static inline struct d3d_viewport
*impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
214 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
217 /*****************************************************************************
219 *****************************************************************************/
221 /*****************************************************************************
222 * IDirect3DViewport3::QueryInterface
224 * A normal QueryInterface. Can query all interface versions and the
225 * IUnknown interface. The VTables of the different versions
229 * refiid: Interface id queried for
230 * obj: Address to write the interface pointer to
234 * E_NOINTERFACE if the requested interface wasn't found
236 *****************************************************************************/
237 static HRESULT WINAPI
d3d_viewport_QueryInterface(IDirect3DViewport3
*iface
, REFIID riid
, void **object
)
239 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
241 if (IsEqualGUID(&IID_IDirect3DViewport3
, riid
)
242 || IsEqualGUID(&IID_IDirect3DViewport2
, riid
)
243 || IsEqualGUID(&IID_IDirect3DViewport
, riid
)
244 || IsEqualGUID(&IID_IUnknown
, riid
))
246 IDirect3DViewport3_AddRef(iface
);
251 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
254 return E_NOINTERFACE
;
257 /*****************************************************************************
258 * IDirect3DViewport3::AddRef
260 * Increases the refcount.
265 *****************************************************************************/
266 static ULONG WINAPI
d3d_viewport_AddRef(IDirect3DViewport3
*iface
)
268 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
269 ULONG ref
= InterlockedIncrement(&viewport
->ref
);
271 TRACE("%p increasing refcount to %u.\n", viewport
, ref
);
276 /*****************************************************************************
277 * IDirect3DViewport3::Release
279 * Reduces the refcount. If it falls to 0, the interface is released
284 *****************************************************************************/
285 static ULONG WINAPI
d3d_viewport_Release(IDirect3DViewport3
*iface
)
287 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
288 ULONG ref
= InterlockedDecrement(&viewport
->ref
);
290 TRACE("%p decreasing refcount to %u.\n", viewport
, ref
);
298 /*****************************************************************************
299 * IDirect3DViewport Methods.
300 *****************************************************************************/
302 /*****************************************************************************
303 * IDirect3DViewport3::Initialize
305 * No-op initialization.
308 * Direct3D: The direct3D device this viewport is assigned to
311 * DDERR_ALREADYINITIALIZED
313 *****************************************************************************/
314 static HRESULT WINAPI
d3d_viewport_Initialize(IDirect3DViewport3
*iface
, IDirect3D
*d3d
)
316 TRACE("iface %p, d3d %p.\n", iface
, d3d
);
318 return DDERR_ALREADYINITIALIZED
;
321 static HRESULT WINAPI
d3d_viewport_GetViewport(IDirect3DViewport3
*iface
, D3DVIEWPORT
*vp
)
323 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
326 TRACE("iface %p, vp %p.\n", iface
, vp
);
329 return DDERR_INVALIDPARAMS
;
331 if (viewport
->version
== DDRAW_VIEWPORT_VERSION_NONE
)
333 WARN("Viewport data was not set.\n");
334 return D3DERR_VIEWPORTDATANOTSET
;
337 wined3d_mutex_lock();
340 if (viewport
->version
== DDRAW_VIEWPORT_VERSION_1
)
342 memcpy(vp
, &viewport
->viewports
.vp1
, size
);
348 vp1
.dwSize
= sizeof(vp1
);
349 vp1
.dwX
= viewport
->viewports
.vp2
.dwX
;
350 vp1
.dwY
= viewport
->viewports
.vp2
.dwY
;
351 vp1
.dwWidth
= viewport
->viewports
.vp2
.dwWidth
;
352 vp1
.dwHeight
= viewport
->viewports
.vp2
.dwHeight
;
357 vp1
.dvMinZ
= viewport
->viewports
.vp2
.dvMinZ
;
358 vp1
.dvMaxZ
= viewport
->viewports
.vp2
.dvMaxZ
;
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_get_rendertarget_view(device
->wined3d_device
, 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 D3DVIEWPORT vp
= viewport
->viewports
.vp1
;
487 D3DMATRIX view_mat
, world_mat
, proj_mat
, mat
;
488 struct transform_vertices_vertex
*in
, *out
;
492 struct d3d_device
*device
= viewport
->active_device
;
493 BOOL activate
= device
->current_viewport
!= viewport
;
495 TRACE("iface %p, vertex_count %u, data %p, flags %#x, offscreen %p.\n",
496 iface
, dwVertexCount
, data
, dwFlags
, offscreen
);
498 /* Tests on windows show that Windows crashes when this occurs,
499 * so don't return the (intuitive) return value
502 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
503 return D3DERR_VIEWPORTHASNODEVICE;
507 if (!data
|| data
->dwSize
!= sizeof(*data
))
509 WARN("Transform data is NULL or size is incorrect, returning DDERR_INVALIDPARAMS\n");
510 return DDERR_INVALIDPARAMS
;
512 if (!(dwFlags
& (D3DTRANSFORM_UNCLIPPED
| D3DTRANSFORM_CLIPPED
)))
514 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
515 return DDERR_INVALIDPARAMS
;
518 wined3d_mutex_lock();
520 viewport_activate(viewport
, TRUE
);
522 wined3d_device_get_transform(device
->wined3d_device
,
523 D3DTRANSFORMSTATE_VIEW
, (struct wined3d_matrix
*)&view_mat
);
524 wined3d_device_get_transform(device
->wined3d_device
,
525 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix
*)&world_mat
);
526 wined3d_device_get_transform(device
->wined3d_device
,
527 WINED3D_TS_PROJECTION
, (struct wined3d_matrix
*)&proj_mat
);
528 multiply_matrix(&mat
, &view_mat
, &world_mat
);
529 multiply_matrix(&mat
, &proj_mat
, &mat
);
531 /* The pointer is not tested against NULL on Windows. */
532 if (dwFlags
& D3DTRANSFORM_CLIPPED
)
538 for(i
= 0; i
< dwVertexCount
; i
++)
540 in
= (struct transform_vertices_vertex
*)((char *)data
->lpIn
+ data
->dwInSize
* i
);
541 out
= (struct transform_vertices_vertex
*)((char *)data
->lpOut
+ data
->dwOutSize
* i
);
543 x
= (in
->x
* mat
._11
) + (in
->y
* mat
._21
) + (in
->z
* mat
._31
) + mat
._41
;
544 y
= (in
->x
* mat
._12
) + (in
->y
* mat
._22
) + (in
->z
* mat
._32
) + mat
._42
;
545 z
= (in
->x
* mat
._13
) + (in
->y
* mat
._23
) + (in
->z
* mat
._33
) + mat
._43
;
546 w
= (in
->x
* mat
._14
) + (in
->y
* mat
._24
) + (in
->z
* mat
._34
) + mat
._44
;
548 if(dwFlags
& D3DTRANSFORM_CLIPPED
)
550 /* If clipping is enabled, Windows assumes that outH is
551 * a valid pointer. */
552 outH
[i
].u1
.hx
= (x
- device
->legacy_clipspace
._41
* w
) / device
->legacy_clipspace
._11
;
553 outH
[i
].u2
.hy
= (y
- device
->legacy_clipspace
._42
* w
) / device
->legacy_clipspace
._22
;
554 outH
[i
].u3
.hz
= (z
- device
->legacy_clipspace
._43
* w
) / device
->legacy_clipspace
._33
;
558 outH
[i
].dwFlags
|= D3DCLIP_RIGHT
;
560 outH
[i
].dwFlags
|= D3DCLIP_LEFT
;
562 outH
[i
].dwFlags
|= D3DCLIP_TOP
;
564 outH
[i
].dwFlags
|= D3DCLIP_BOTTOM
;
566 outH
[i
].dwFlags
|= D3DCLIP_FRONT
;
568 outH
[i
].dwFlags
|= D3DCLIP_BACK
;
570 *offscreen
&= outH
[i
].dwFlags
;
574 /* Looks like native just drops the vertex, leaves whatever data
575 * it has in the output buffer and goes on with the next vertex.
576 * The exact scheme hasn't been figured out yet, but windows
577 * definitely writes something there.
588 x
*= w
; y
*= w
; z
*= w
;
590 out
->x
= (x
+ 1.0f
) * vp
.dwWidth
* 0.5 + vp
.dwX
;
591 out
->y
= (-y
+ 1.0f
) * vp
.dwHeight
* 0.5 + vp
.dwY
;
594 out
->payload
= in
->payload
;
597 if (activate
&& device
->current_viewport
)
598 viewport_activate(device
->current_viewport
, TRUE
);
600 wined3d_mutex_unlock();
606 /*****************************************************************************
607 * IDirect3DViewport3::LightElements
609 * The DirectX 5.0 sdk says that it's not implemented
617 *****************************************************************************/
618 static HRESULT WINAPI
d3d_viewport_LightElements(IDirect3DViewport3
*iface
,
619 DWORD element_count
, D3DLIGHTDATA
*data
)
621 TRACE("iface %p, element_count %u, data %p.\n", iface
, element_count
, data
);
623 return DDERR_UNSUPPORTED
;
626 static HRESULT WINAPI
d3d_viewport_SetBackground(IDirect3DViewport3
*iface
, D3DMATERIALHANDLE material
)
628 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
629 struct d3d_material
*m
;
631 TRACE("iface %p, material %#x.\n", iface
, material
);
633 wined3d_mutex_lock();
635 if (!(m
= ddraw_get_object(&viewport
->ddraw
->d3ddevice
->handle_table
, material
- 1, DDRAW_HANDLE_MATERIAL
)))
637 WARN("Invalid material handle %#x.\n", material
);
638 wined3d_mutex_unlock();
639 return DDERR_INVALIDPARAMS
;
642 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
643 m
->mat
.u
.diffuse
.u1
.r
, m
->mat
.u
.diffuse
.u2
.g
,
644 m
->mat
.u
.diffuse
.u3
.b
, m
->mat
.u
.diffuse
.u4
.a
);
645 viewport
->background
= m
;
647 wined3d_mutex_unlock();
652 /*****************************************************************************
653 * IDirect3DViewport3::GetBackground
655 * Returns the material handle assigned to the background of the viewport
658 * lphMat: Address to store the handle
659 * lpValid: is set to FALSE if no background is set, TRUE if one is set
664 *****************************************************************************/
665 static HRESULT WINAPI
d3d_viewport_GetBackground(IDirect3DViewport3
*iface
,
666 D3DMATERIALHANDLE
*material
, BOOL
*valid
)
668 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
670 TRACE("iface %p, material %p, valid %p.\n", iface
, material
, valid
);
672 wined3d_mutex_lock();
674 *valid
= !!viewport
->background
;
676 *material
= viewport
->background
? viewport
->background
->Handle
: 0;
677 wined3d_mutex_unlock();
682 /*****************************************************************************
683 * IDirect3DViewport3::SetBackgroundDepth
685 * Sets a surface that represents the background depth. Its contents are
686 * used to set the depth buffer in IDirect3DViewport3::Clear
689 * lpDDSurface: Surface to set
691 * Returns: D3D_OK, because it's a stub
693 *****************************************************************************/
694 static HRESULT WINAPI
d3d_viewport_SetBackgroundDepth(IDirect3DViewport3
*iface
, IDirectDrawSurface
*surface
)
696 FIXME("iface %p, surface %p stub!\n", iface
, surface
);
701 /*****************************************************************************
702 * IDirect3DViewport3::GetBackgroundDepth
704 * Returns the surface that represents the depth field
707 * lplpDDSurface: Address to store the interface pointer
708 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
711 * D3D_OK, because it's a stub
712 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
714 *****************************************************************************/
715 static HRESULT WINAPI
d3d_viewport_GetBackgroundDepth(IDirect3DViewport3
*iface
,
716 IDirectDrawSurface
**surface
, BOOL
*valid
)
718 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, surface
, valid
);
723 /*****************************************************************************
724 * IDirect3DViewport3::Clear
726 * Clears the render target and / or the z buffer
729 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
731 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
732 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
736 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
737 * The return value of IDirect3DDevice7::Clear
739 *****************************************************************************/
740 static HRESULT WINAPI
d3d_viewport_Clear(IDirect3DViewport3
*iface
,
741 DWORD rect_count
, D3DRECT
*rects
, DWORD flags
)
743 struct d3d_viewport
*This
= impl_from_IDirect3DViewport3(iface
);
744 DWORD color
= 0x00000000;
746 IDirect3DViewport3
*current_viewport
;
747 IDirect3DDevice3
*d3d_device3
;
749 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface
, rect_count
, rects
, flags
);
751 if (!rects
|| !rect_count
)
753 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count
, rects
);
757 if (This
->active_device
== NULL
) {
758 ERR(" Trying to clear a viewport not attached to a device!\n");
759 return D3DERR_VIEWPORTHASNODEVICE
;
761 d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
763 wined3d_mutex_lock();
765 if (flags
& D3DCLEAR_TARGET
)
767 if (!This
->background
)
768 WARN("No background material set.\n");
770 color
= D3DRGBA(This
->background
->mat
.u
.diffuse
.u1
.r
,
771 This
->background
->mat
.u
.diffuse
.u2
.g
,
772 This
->background
->mat
.u
.diffuse
.u3
.b
,
773 This
->background
->mat
.u
.diffuse
.u4
.a
);
776 /* Need to temporarily activate the viewport to clear it. The previously
777 * active one will be restored afterwards. */
778 viewport_activate(This
, TRUE
);
780 hr
= IDirect3DDevice7_Clear(&This
->active_device
->IDirect3DDevice7_iface
, rect_count
, rects
,
781 flags
& (D3DCLEAR_ZBUFFER
| D3DCLEAR_TARGET
), color
, 1.0, 0x00000000);
783 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
785 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
786 viewport_activate(vp
, TRUE
);
787 IDirect3DViewport3_Release(current_viewport
);
790 wined3d_mutex_unlock();
795 /*****************************************************************************
796 * IDirect3DViewport3::AddLight
798 * Adds an light to the viewport
801 * lpDirect3DLight: Interface of the light to add
805 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
806 * DDERR_INVALIDPARAMS if there are 8 lights or more
808 *****************************************************************************/
809 static HRESULT WINAPI
d3d_viewport_AddLight(IDirect3DViewport3
*viewport
, IDirect3DLight
*light
)
811 struct d3d_light
*light_impl
= unsafe_impl_from_IDirect3DLight(light
);
812 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(viewport
);
814 TRACE("viewport %p, light %p.\n", viewport
, light
);
816 wined3d_mutex_lock();
818 if (light_impl
->active_viewport
)
820 wined3d_mutex_unlock();
821 WARN("Light %p is active in viewport %p.\n", light_impl
, light_impl
->active_viewport
);
822 return D3DERR_LIGHTHASVIEWPORT
;
825 light_impl
->active_viewport
= vp
;
827 /* Add the light in the 'linked' chain */
828 list_add_tail(&vp
->light_list
, &light_impl
->entry
);
829 IDirect3DLight_AddRef(light
);
831 light_activate(light_impl
);
833 wined3d_mutex_unlock();
838 /*****************************************************************************
839 * IDirect3DViewport3::DeleteLight
841 * Deletes a light from the viewports' light list
844 * lpDirect3DLight: Light to delete
848 * DDERR_INVALIDPARAMS if the light wasn't found
850 *****************************************************************************/
851 static HRESULT WINAPI
d3d_viewport_DeleteLight(IDirect3DViewport3
*iface
, IDirect3DLight
*lpDirect3DLight
)
853 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
854 struct d3d_light
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
856 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
858 wined3d_mutex_lock();
860 if (l
->active_viewport
!= viewport
)
862 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
863 wined3d_mutex_unlock();
864 return DDERR_INVALIDPARAMS
;
868 list_remove(&l
->entry
);
869 l
->active_viewport
= NULL
;
870 IDirect3DLight_Release(lpDirect3DLight
);
872 wined3d_mutex_unlock();
877 /*****************************************************************************
878 * IDirect3DViewport::NextLight
880 * Enumerates the lights associated with the viewport
883 * lpDirect3DLight: Light to start with
884 * lplpDirect3DLight: Address to store the successor to
887 * D3D_OK, because it's a stub
889 *****************************************************************************/
890 static HRESULT WINAPI
d3d_viewport_NextLight(IDirect3DViewport3
*iface
,
891 IDirect3DLight
*lpDirect3DLight
, IDirect3DLight
**lplpDirect3DLight
, DWORD flags
)
893 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
894 struct d3d_light
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
898 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
899 iface
, lpDirect3DLight
, lplpDirect3DLight
, flags
);
901 if (!lplpDirect3DLight
)
902 return DDERR_INVALIDPARAMS
;
904 wined3d_mutex_lock();
909 if (!l
|| l
->active_viewport
!= viewport
)
912 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
916 entry
= list_next(&viewport
->light_list
, &l
->entry
);
920 entry
= list_head(&viewport
->light_list
);
924 entry
= list_tail(&viewport
->light_list
);
929 WARN("Invalid flags %#x.\n", flags
);
935 *lplpDirect3DLight
= (IDirect3DLight
*)LIST_ENTRY(entry
, struct d3d_light
, entry
);
936 IDirect3DLight_AddRef(*lplpDirect3DLight
);
941 *lplpDirect3DLight
= NULL
;
942 hr
= DDERR_INVALIDPARAMS
;
945 wined3d_mutex_unlock();
950 /*****************************************************************************
951 * IDirect3DViewport2 Methods.
952 *****************************************************************************/
954 static HRESULT WINAPI
d3d_viewport_GetViewport2(IDirect3DViewport3
*iface
, D3DVIEWPORT2
*vp
)
956 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
959 TRACE("iface %p, vp %p.\n", iface
, vp
);
962 return DDERR_INVALIDPARAMS
;
964 if (viewport
->version
== DDRAW_VIEWPORT_VERSION_NONE
)
966 WARN("Viewport data was not set.\n");
967 return D3DERR_VIEWPORTDATANOTSET
;
970 wined3d_mutex_lock();
972 if (viewport
->version
== DDRAW_VIEWPORT_VERSION_2
)
974 memcpy(vp
, &viewport
->viewports
.vp2
, size
);
980 vp2
.dwSize
= sizeof(vp2
);
981 vp2
.dwX
= viewport
->viewports
.vp1
.dwX
;
982 vp2
.dwY
= viewport
->viewports
.vp1
.dwY
;
983 vp2
.dwWidth
= viewport
->viewports
.vp1
.dwWidth
;
984 vp2
.dwHeight
= viewport
->viewports
.vp1
.dwHeight
;
987 vp2
.dvClipWidth
= 0.0f
;
988 vp2
.dvClipHeight
= 0.0f
;
989 vp2
.dvMinZ
= viewport
->viewports
.vp1
.dvMinZ
;
990 vp2
.dvMaxZ
= viewport
->viewports
.vp1
.dvMaxZ
;
991 memcpy(vp
, &vp2
, size
);
996 TRACE(" returning D3DVIEWPORT2 :\n");
997 _dump_D3DVIEWPORT2(vp
);
1000 wined3d_mutex_unlock();
1005 static HRESULT WINAPI
d3d_viewport_SetViewport2(IDirect3DViewport3
*iface
, D3DVIEWPORT2
*vp
)
1007 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
1008 struct d3d_device
*device
= viewport
->active_device
;
1009 struct wined3d_sub_resource_desc rt_desc
;
1010 struct wined3d_rendertarget_view
*rtv
;
1011 IDirect3DViewport3
*current_viewport
;
1012 struct ddraw_surface
*surface
;
1014 TRACE("iface %p, vp %p.\n", iface
, vp
);
1017 return DDERR_INVALIDPARAMS
;
1019 if (vp
->dwSize
!= sizeof(*vp
))
1021 WARN("Invalid D3DVIEWPORT2 size %u.\n", vp
->dwSize
);
1022 return DDERR_INVALIDPARAMS
;
1025 if (TRACE_ON(ddraw
))
1027 TRACE(" getting D3DVIEWPORT2 :\n");
1028 _dump_D3DVIEWPORT2(vp
);
1033 WARN("Viewport not bound to a device, returning D3DERR_VIEWPORTHASNODEVICE.\n");
1034 return D3DERR_VIEWPORTHASNODEVICE
;
1037 wined3d_mutex_lock();
1039 if (device
->version
> 1)
1041 if (!(rtv
= wined3d_device_get_rendertarget_view(device
->wined3d_device
, 0)))
1043 wined3d_mutex_unlock();
1044 return DDERR_INVALIDCAPS
;
1046 surface
= wined3d_rendertarget_view_get_sub_resource_parent(rtv
);
1047 wined3d_texture_get_sub_resource_desc(surface
->wined3d_texture
, surface
->sub_resource_idx
, &rt_desc
);
1049 if (vp
->dwX
> rt_desc
.width
|| vp
->dwWidth
> rt_desc
.width
- vp
->dwX
1050 || vp
->dwY
> rt_desc
.height
|| vp
->dwHeight
> rt_desc
.height
- vp
->dwY
)
1052 WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
1053 wined3d_mutex_unlock();
1054 return DDERR_INVALIDPARAMS
;
1058 viewport
->version
= DDRAW_VIEWPORT_VERSION_2
;
1059 viewport
->viewports
.vp2
= *vp
;
1061 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(&device
->IDirect3DDevice3_iface
, ¤t_viewport
)))
1063 if (current_viewport
== iface
)
1064 viewport_activate(viewport
, FALSE
);
1065 IDirect3DViewport3_Release(current_viewport
);
1068 wined3d_mutex_unlock();
1073 /*****************************************************************************
1074 * IDirect3DViewport3 Methods.
1075 *****************************************************************************/
1077 /*****************************************************************************
1078 * IDirect3DViewport3::SetBackgroundDepth2
1080 * Sets a IDirectDrawSurface4 surface as the background depth surface
1083 * lpDDS: Surface to set
1086 * D3D_OK, because it's stub
1088 *****************************************************************************/
1089 static HRESULT WINAPI
d3d_viewport_SetBackgroundDepth2(IDirect3DViewport3
*iface
,
1090 IDirectDrawSurface4
*surface
)
1092 FIXME("iface %p, surface %p stub!\n", iface
, surface
);
1097 /*****************************************************************************
1098 * IDirect3DViewport3::GetBackgroundDepth2
1100 * Returns the IDirect3DSurface4 interface to the background depth surface
1103 * lplpDDS: Address to store the interface pointer at
1104 * lpValid: Set to true if a surface is assigned
1107 * D3D_OK because it's a stub
1109 *****************************************************************************/
1110 static HRESULT WINAPI
d3d_viewport_GetBackgroundDepth2(IDirect3DViewport3
*iface
,
1111 IDirectDrawSurface4
**surface
, BOOL
*valid
)
1113 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, surface
, valid
);
1118 /*****************************************************************************
1119 * IDirect3DViewport3::Clear2
1121 * Another clearing method
1124 * Count: Number of rectangles to clear
1125 * Rects: Rectangle array to clear
1126 * Flags: Some flags :)
1127 * Color: Color to fill the render target with
1128 * Z: Value to fill the depth buffer with
1129 * Stencil: Value to fill the stencil bits with
1133 *****************************************************************************/
1134 static HRESULT WINAPI
d3d_viewport_Clear2(IDirect3DViewport3
*iface
, DWORD rect_count
,
1135 D3DRECT
*rects
, DWORD flags
, DWORD color
, D3DVALUE depth
, DWORD stencil
)
1137 struct d3d_viewport
*viewport
= impl_from_IDirect3DViewport3(iface
);
1139 IDirect3DViewport3
*current_viewport
;
1140 IDirect3DDevice3
*d3d_device3
;
1142 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1143 iface
, rect_count
, rects
, flags
, color
, depth
, stencil
);
1145 if (!rects
|| !rect_count
)
1147 WARN("rect_count = %u, rects = %p, ignoring clear\n", rect_count
, rects
);
1151 wined3d_mutex_lock();
1153 if (!viewport
->active_device
)
1155 WARN("Trying to clear a viewport not attached to a device.\n");
1156 wined3d_mutex_unlock();
1157 return D3DERR_VIEWPORTHASNODEVICE
;
1159 d3d_device3
= &viewport
->active_device
->IDirect3DDevice3_iface
;
1160 /* Need to temporarily activate viewport to clear it. Previously active
1161 * one will be restored afterwards. */
1162 viewport_activate(viewport
, TRUE
);
1164 hr
= IDirect3DDevice7_Clear(&viewport
->active_device
->IDirect3DDevice7_iface
,
1165 rect_count
, rects
, flags
, color
, depth
, stencil
);
1166 if (SUCCEEDED(IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
)))
1168 struct d3d_viewport
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
1169 viewport_activate(vp
, TRUE
);
1170 IDirect3DViewport3_Release(current_viewport
);
1173 wined3d_mutex_unlock();
1178 /*****************************************************************************
1180 *****************************************************************************/
1182 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl
=
1184 /*** IUnknown Methods ***/
1185 d3d_viewport_QueryInterface
,
1186 d3d_viewport_AddRef
,
1187 d3d_viewport_Release
,
1188 /*** IDirect3DViewport Methods */
1189 d3d_viewport_Initialize
,
1190 d3d_viewport_GetViewport
,
1191 d3d_viewport_SetViewport
,
1192 d3d_viewport_TransformVertices
,
1193 d3d_viewport_LightElements
,
1194 d3d_viewport_SetBackground
,
1195 d3d_viewport_GetBackground
,
1196 d3d_viewport_SetBackgroundDepth
,
1197 d3d_viewport_GetBackgroundDepth
,
1199 d3d_viewport_AddLight
,
1200 d3d_viewport_DeleteLight
,
1201 d3d_viewport_NextLight
,
1202 /*** IDirect3DViewport2 Methods ***/
1203 d3d_viewport_GetViewport2
,
1204 d3d_viewport_SetViewport2
,
1205 /*** IDirect3DViewport3 Methods ***/
1206 d3d_viewport_SetBackgroundDepth2
,
1207 d3d_viewport_GetBackgroundDepth2
,
1208 d3d_viewport_Clear2
,
1211 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
1213 if (!iface
) return NULL
;
1214 assert(iface
->lpVtbl
== &d3d_viewport_vtbl
);
1215 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1218 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2
*iface
)
1220 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1221 if (!iface
) return NULL
;
1222 assert(iface
->lpVtbl
== (IDirect3DViewport2Vtbl
*)&d3d_viewport_vtbl
);
1223 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1226 struct d3d_viewport
*unsafe_impl_from_IDirect3DViewport(IDirect3DViewport
*iface
)
1228 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1229 if (!iface
) return NULL
;
1230 assert(iface
->lpVtbl
== (IDirect3DViewportVtbl
*)&d3d_viewport_vtbl
);
1231 return CONTAINING_RECORD(iface
, struct d3d_viewport
, IDirect3DViewport3_iface
);
1234 void d3d_viewport_init(struct d3d_viewport
*viewport
, struct ddraw
*ddraw
)
1236 viewport
->IDirect3DViewport3_iface
.lpVtbl
= &d3d_viewport_vtbl
;
1238 viewport
->ddraw
= ddraw
;
1239 viewport
->version
= DDRAW_VIEWPORT_VERSION_NONE
;
1240 list_init(&viewport
->light_list
);