2 * Copyright (c) 1998 Lionel ULMER
3 * Copyright (c) 2006-2007 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DViewport2.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
29 /*****************************************************************************
31 *****************************************************************************/
33 static void update_clip_space(IDirect3DDeviceImpl
*device
,
34 struct wined3d_vec3
*scale
, struct wined3d_vec3
*offset
)
36 D3DMATRIX clip_space
=
38 scale
->x
, 0.0f
, 0.0f
, 0.0f
,
39 0.0f
, scale
->y
, 0.0f
, 0.0f
,
40 0.0f
, 0.0f
, scale
->z
, 0.0f
,
41 offset
->x
, offset
->y
, offset
->z
, 1.0f
,
46 multiply_matrix(&projection
, &clip_space
, &device
->legacy_projection
);
47 hr
= wined3d_device_set_transform(device
->wined3d_device
,
48 WINED3DTS_PROJECTION
, (struct wined3d_matrix
*)&projection
);
50 device
->legacy_clipspace
= clip_space
;
53 /*****************************************************************************
56 * activates the viewport using IDirect3DDevice7::SetViewport
58 *****************************************************************************/
59 void viewport_activate(IDirect3DViewportImpl
*This
, BOOL ignore_lights
)
61 struct wined3d_vec3 scale
, offset
;
66 IDirect3DLightImpl
*light
;
68 /* Activate all the lights associated with this context */
69 LIST_FOR_EACH_ENTRY(light
, &This
->light_list
, IDirect3DLightImpl
, entry
)
71 light_activate(light
);
75 /* And copy the values in the structure used by the device */
78 vp
.dwX
= This
->viewports
.vp2
.dwX
;
79 vp
.dwY
= This
->viewports
.vp2
.dwY
;
80 vp
.dwHeight
= This
->viewports
.vp2
.dwHeight
;
81 vp
.dwWidth
= This
->viewports
.vp2
.dwWidth
;
85 scale
.x
= 2.0f
/ This
->viewports
.vp2
.dvClipWidth
;
86 scale
.y
= 2.0f
/ This
->viewports
.vp2
.dvClipHeight
;
87 scale
.z
= 1.0f
/ (This
->viewports
.vp2
.dvMaxZ
- This
->viewports
.vp2
.dvMinZ
);
88 offset
.x
= -2.0f
* This
->viewports
.vp2
.dvClipX
/ This
->viewports
.vp2
.dvClipWidth
- 1.0f
;
89 offset
.y
= -2.0f
* This
->viewports
.vp2
.dvClipY
/ This
->viewports
.vp2
.dvClipHeight
+ 1.0f
;
90 offset
.z
= -This
->viewports
.vp2
.dvMinZ
/ (This
->viewports
.vp2
.dvMaxZ
- This
->viewports
.vp2
.dvMinZ
);
94 vp
.dwX
= This
->viewports
.vp1
.dwX
;
95 vp
.dwY
= This
->viewports
.vp1
.dwY
;
96 vp
.dwHeight
= This
->viewports
.vp1
.dwHeight
;
97 vp
.dwWidth
= This
->viewports
.vp1
.dwWidth
;
101 scale
.x
= 2.0f
* This
->viewports
.vp1
.dvScaleX
/ This
->viewports
.vp1
.dwWidth
;
102 scale
.y
= 2.0f
* This
->viewports
.vp1
.dvScaleY
/ This
->viewports
.vp1
.dwHeight
;
109 update_clip_space(This
->active_device
, &scale
, &offset
);
110 IDirect3DDevice7_SetViewport(&This
->active_device
->IDirect3DDevice7_iface
, &vp
);
113 /*****************************************************************************
114 * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
116 * Writes viewport information to TRACE
118 *****************************************************************************/
119 static void _dump_D3DVIEWPORT(const D3DVIEWPORT
*lpvp
)
121 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
122 lpvp
->dwSize
, lpvp
->dwX
, lpvp
->dwY
);
123 TRACE(" - dwWidth = %d dwHeight = %d\n",
124 lpvp
->dwWidth
, lpvp
->dwHeight
);
125 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
126 lpvp
->dvScaleX
, lpvp
->dvScaleY
);
127 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
128 lpvp
->dvMaxX
, lpvp
->dvMaxY
);
129 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
130 lpvp
->dvMinZ
, lpvp
->dvMaxZ
);
133 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2
*lpvp
)
135 TRACE(" - dwSize = %d dwX = %d dwY = %d\n",
136 lpvp
->dwSize
, lpvp
->dwX
, lpvp
->dwY
);
137 TRACE(" - dwWidth = %d dwHeight = %d\n",
138 lpvp
->dwWidth
, lpvp
->dwHeight
);
139 TRACE(" - dvClipX = %f dvClipY = %f\n",
140 lpvp
->dvClipX
, lpvp
->dvClipY
);
141 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
142 lpvp
->dvClipWidth
, lpvp
->dvClipHeight
);
143 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
144 lpvp
->dvMinZ
, lpvp
->dvMaxZ
);
147 static inline IDirect3DViewportImpl
*impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
149 return CONTAINING_RECORD(iface
, IDirect3DViewportImpl
, IDirect3DViewport3_iface
);
152 /*****************************************************************************
154 *****************************************************************************/
156 /*****************************************************************************
157 * IDirect3DViewport3::QueryInterface
159 * A normal QueryInterface. Can query all interface versions and the
160 * IUnknown interface. The VTables of the different versions
164 * refiid: Interface id queried for
165 * obj: Address to write the interface pointer to
169 * E_NOINTERFACE if the requested interface wasn't found
171 *****************************************************************************/
172 static HRESULT WINAPI
IDirect3DViewportImpl_QueryInterface(IDirect3DViewport3
*iface
, REFIID riid
, void **object
)
174 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
176 if (IsEqualGUID(&IID_IDirect3DViewport3
, riid
)
177 || IsEqualGUID(&IID_IDirect3DViewport2
, riid
)
178 || IsEqualGUID(&IID_IDirect3DViewport
, riid
)
179 || IsEqualGUID(&IID_IUnknown
, riid
))
181 IDirect3DViewport3_AddRef(iface
);
186 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
189 return E_NOINTERFACE
;
192 /*****************************************************************************
193 * IDirect3DViewport3::AddRef
195 * Increases the refcount.
200 *****************************************************************************/
202 IDirect3DViewportImpl_AddRef(IDirect3DViewport3
*iface
)
204 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
205 ULONG ref
= InterlockedIncrement(&This
->ref
);
207 TRACE("%p increasing refcount to %u.\n", This
, ref
);
212 /*****************************************************************************
213 * IDirect3DViewport3::Release
215 * Reduces the refcount. If it falls to 0, the interface is released
220 *****************************************************************************/
222 IDirect3DViewportImpl_Release(IDirect3DViewport3
*iface
)
224 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
225 ULONG ref
= InterlockedDecrement(&This
->ref
);
227 TRACE("%p decreasing refcount to %u.\n", This
, ref
);
230 HeapFree(GetProcessHeap(), 0, This
);
236 /*****************************************************************************
237 * IDirect3DViewport Methods.
238 *****************************************************************************/
240 /*****************************************************************************
241 * IDirect3DViewport3::Initialize
243 * No-op initialization.
246 * Direct3D: The direct3D device this viewport is assigned to
249 * DDERR_ALREADYINITIALIZED
251 *****************************************************************************/
252 static HRESULT WINAPI
253 IDirect3DViewportImpl_Initialize(IDirect3DViewport3
*iface
,
256 TRACE("iface %p, d3d %p.\n", iface
, Direct3D
);
258 return DDERR_ALREADYINITIALIZED
;
261 /*****************************************************************************
262 * IDirect3DViewport3::GetViewport
264 * Returns the viewport data assigned to this viewport interface
267 * Data: Address to store the data
271 * DDERR_INVALIDPARAMS if Data is NULL
273 *****************************************************************************/
274 static HRESULT WINAPI
275 IDirect3DViewportImpl_GetViewport(IDirect3DViewport3
*iface
,
278 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
281 TRACE("iface %p, data %p.\n", iface
, lpData
);
283 wined3d_mutex_lock();
285 dwSize
= lpData
->dwSize
;
286 memset(lpData
, 0, dwSize
);
288 memcpy(lpData
, &(This
->viewports
.vp1
), dwSize
);
291 vp1
.dwSize
= sizeof(vp1
);
292 vp1
.dwX
= This
->viewports
.vp2
.dwX
;
293 vp1
.dwY
= This
->viewports
.vp2
.dwY
;
294 vp1
.dwWidth
= This
->viewports
.vp2
.dwWidth
;
295 vp1
.dwHeight
= This
->viewports
.vp2
.dwHeight
;
300 vp1
.dvMinZ
= This
->viewports
.vp2
.dvMinZ
;
301 vp1
.dvMaxZ
= This
->viewports
.vp2
.dvMaxZ
;
302 memcpy(lpData
, &vp1
, dwSize
);
307 TRACE(" returning D3DVIEWPORT :\n");
308 _dump_D3DVIEWPORT(lpData
);
311 wined3d_mutex_unlock();
316 /*****************************************************************************
317 * IDirect3DViewport3::SetViewport
319 * Sets the viewport information for this interface
322 * lpData: Viewport to set
326 * DDERR_INVALIDPARAMS if Data is NULL
328 *****************************************************************************/
329 static HRESULT WINAPI
330 IDirect3DViewportImpl_SetViewport(IDirect3DViewport3
*iface
,
333 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
334 IDirect3DViewport3
*current_viewport
;
336 TRACE("iface %p, data %p.\n", iface
, lpData
);
340 TRACE(" getting D3DVIEWPORT :\n");
341 _dump_D3DVIEWPORT(lpData
);
344 wined3d_mutex_lock();
347 memset(&(This
->viewports
.vp1
), 0, sizeof(This
->viewports
.vp1
));
348 memcpy(&(This
->viewports
.vp1
), lpData
, lpData
->dwSize
);
350 /* Tests on two games show that these values are never used properly so override
351 them with proper ones :-)
353 This
->viewports
.vp1
.dvMinZ
= 0.0;
354 This
->viewports
.vp1
.dvMaxZ
= 1.0;
356 if (This
->active_device
) {
357 IDirect3DDevice3
*d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
358 IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
);
359 if (current_viewport
)
361 if (current_viewport
== iface
) viewport_activate(This
, FALSE
);
362 IDirect3DViewport3_Release(current_viewport
);
366 wined3d_mutex_unlock();
371 /*****************************************************************************
372 * IDirect3DViewport3::TransformVertices
374 * Transforms vertices by the transformation matrix.
376 * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
377 * so it's tempting to forward it to there. However, there are some
378 * tiny differences. First, the lpOffscreen flag that is reported back,
379 * then there is the homogeneous vertex that is generated. Also there's a lack
380 * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
381 * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
382 * ProcessVertices doesn't pay of in terms of wrapper code needed and code
386 * dwVertexCount: The number of vertices to be transformed
387 * lpData: Pointer to the vertex data
388 * dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
389 * lpOffScreen: Set to the clipping plane clipping the vertex, if only one
390 * vertex is transformed and clipping is on. 0 otherwise
394 * D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
395 * DDERR_INVALIDPARAMS if no clipping flag is specified
397 *****************************************************************************/
398 static HRESULT WINAPI
399 IDirect3DViewportImpl_TransformVertices(IDirect3DViewport3
*iface
,
401 D3DTRANSFORMDATA
*lpData
,
405 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
406 D3DMATRIX view_mat
, world_mat
, mat
;
411 D3DVIEWPORT vp
= This
->viewports
.vp1
;
414 TRACE("iface %p, vertex_count %u, vertex_data %p, flags %#x, clip_plane %p.\n",
415 iface
, dwVertexCount
, lpData
, dwFlags
, lpOffScreen
);
417 /* Tests on windows show that Windows crashes when this occurs,
418 * so don't return the (intuitive) return value
419 if(!This->active_device)
421 WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
422 return D3DERR_VIEWPORTHASNODEVICE;
426 if(!(dwFlags
& (D3DTRANSFORM_UNCLIPPED
| D3DTRANSFORM_CLIPPED
)))
428 WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
429 return DDERR_INVALIDPARAMS
;
433 wined3d_mutex_lock();
434 wined3d_device_get_transform(This
->active_device
->wined3d_device
,
435 D3DTRANSFORMSTATE_VIEW
, (struct wined3d_matrix
*)&view_mat
);
436 wined3d_device_get_transform(This
->active_device
->wined3d_device
,
437 WINED3DTS_WORLDMATRIX(0), (struct wined3d_matrix
*)&world_mat
);
438 multiply_matrix(&mat
, &view_mat
, &world_mat
);
439 multiply_matrix(&mat
, &This
->active_device
->legacy_projection
, &mat
);
443 outH
= lpData
->lpHOut
;
444 for(i
= 0; i
< dwVertexCount
; i
++)
446 x
= (in
[0] * mat
._11
) + (in
[1] * mat
._21
) + (in
[2] * mat
._31
) + (1.0 * mat
._41
);
447 y
= (in
[0] * mat
._12
) + (in
[1] * mat
._22
) + (in
[2] * mat
._32
) + (1.0 * mat
._42
);
448 z
= (in
[0] * mat
._13
) + (in
[1] * mat
._23
) + (in
[2] * mat
._33
) + (1.0 * mat
._43
);
449 w
= (in
[0] * mat
._14
) + (in
[1] * mat
._24
) + (in
[2] * mat
._34
) + (1.0 * mat
._44
);
451 if(dwFlags
& D3DTRANSFORM_CLIPPED
)
453 /* If clipping is enabled, Windows assumes that outH is
456 outH
[i
].u1
.hx
= x
; outH
[i
].u2
.hy
= y
; outH
[i
].u3
.hz
= z
;
459 if(x
* vp
.dvScaleX
> ((float) vp
.dwWidth
* 0.5))
460 outH
[i
].dwFlags
|= D3DCLIP_RIGHT
;
461 if(x
* vp
.dvScaleX
<= -((float) vp
.dwWidth
) * 0.5)
462 outH
[i
].dwFlags
|= D3DCLIP_LEFT
;
463 if(y
* vp
.dvScaleY
> ((float) vp
.dwHeight
* 0.5))
464 outH
[i
].dwFlags
|= D3DCLIP_TOP
;
465 if(y
* vp
.dvScaleY
<= -((float) vp
.dwHeight
) * 0.5)
466 outH
[i
].dwFlags
|= D3DCLIP_BOTTOM
;
468 outH
[i
].dwFlags
|= D3DCLIP_FRONT
;
470 outH
[i
].dwFlags
|= D3DCLIP_BACK
;
474 /* Looks like native just drops the vertex, leaves whatever data
475 * it has in the output buffer and goes on with the next vertex.
476 * The exact scheme hasn't been figured out yet, but windows
477 * definitely writes something there.
483 in
= (float *) ((char *) in
+ lpData
->dwInSize
);
484 out
= (float *) ((char *) out
+ lpData
->dwOutSize
);
490 x
*= w
; y
*= w
; z
*= w
;
492 out
[0] = vp
.dwWidth
/ 2 + vp
.dwX
+ x
* vp
.dvScaleX
;
493 out
[1] = vp
.dwHeight
/ 2 + vp
.dwY
- y
* vp
.dvScaleY
;
496 in
= (float *) ((char *) in
+ lpData
->dwInSize
);
497 out
= (float *) ((char *) out
+ lpData
->dwOutSize
);
500 /* According to the d3d test, the offscreen flag is set only
501 * if exactly one vertex is transformed. Its not documented,
502 * but the test shows that the lpOffscreen flag is set to the
503 * flag combination of clipping planes that clips the vertex.
505 * If clipping is requested, Windows assumes that the offscreen
506 * param is a valid pointer.
508 if(dwVertexCount
== 1 && dwFlags
& D3DTRANSFORM_CLIPPED
)
510 *lpOffScreen
= outH
[0].dwFlags
;
512 else if(*lpOffScreen
)
516 wined3d_mutex_unlock();
522 /*****************************************************************************
523 * IDirect3DViewport3::LightElements
525 * The DirectX 5.0 sdk says that it's not implemented
533 *****************************************************************************/
534 static HRESULT WINAPI
535 IDirect3DViewportImpl_LightElements(IDirect3DViewport3
*iface
,
536 DWORD dwElementCount
,
537 LPD3DLIGHTDATA lpData
)
539 TRACE("iface %p, element_count %u, data %p.\n", iface
, dwElementCount
, lpData
);
541 return DDERR_UNSUPPORTED
;
544 /*****************************************************************************
545 * IDirect3DViewport3::SetBackground
547 * Sets tje background material
550 * hMat: Handle from a IDirect3DMaterial interface
555 *****************************************************************************/
556 static HRESULT WINAPI
557 IDirect3DViewportImpl_SetBackground(IDirect3DViewport3
*iface
,
558 D3DMATERIALHANDLE hMat
)
560 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
561 IDirect3DMaterialImpl
*m
;
563 TRACE("iface %p, material %#x.\n", iface
, hMat
);
565 wined3d_mutex_lock();
569 This
->background
= NULL
;
570 TRACE("Setting background to NULL\n");
571 wined3d_mutex_unlock();
575 m
= ddraw_get_object(&This
->ddraw
->d3ddevice
->handle_table
, hMat
- 1, DDRAW_HANDLE_MATERIAL
);
578 WARN("Invalid material handle.\n");
579 wined3d_mutex_unlock();
580 return DDERR_INVALIDPARAMS
;
583 TRACE("Setting background color : %.8e %.8e %.8e %.8e.\n",
584 m
->mat
.u
.diffuse
.u1
.r
, m
->mat
.u
.diffuse
.u2
.g
,
585 m
->mat
.u
.diffuse
.u3
.b
, m
->mat
.u
.diffuse
.u4
.a
);
586 This
->background
= m
;
588 wined3d_mutex_unlock();
593 /*****************************************************************************
594 * IDirect3DViewport3::GetBackground
596 * Returns the material handle assigned to the background of the viewport
599 * lphMat: Address to store the handle
600 * lpValid: is set to FALSE if no background is set, TRUE if one is set
605 *****************************************************************************/
606 static HRESULT WINAPI
607 IDirect3DViewportImpl_GetBackground(IDirect3DViewport3
*iface
,
608 D3DMATERIALHANDLE
*lphMat
,
611 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
613 TRACE("iface %p, material %p, valid %p.\n", iface
, lphMat
, lpValid
);
615 wined3d_mutex_lock();
618 *lpValid
= This
->background
!= NULL
;
624 *lphMat
= This
->background
->Handle
;
631 wined3d_mutex_unlock();
636 /*****************************************************************************
637 * IDirect3DViewport3::SetBackgroundDepth
639 * Sets a surface that represents the background depth. It's contents are
640 * used to set the depth buffer in IDirect3DViewport3::Clear
643 * lpDDSurface: Surface to set
645 * Returns: D3D_OK, because it's a stub
647 *****************************************************************************/
648 static HRESULT WINAPI
649 IDirect3DViewportImpl_SetBackgroundDepth(IDirect3DViewport3
*iface
,
650 IDirectDrawSurface
*lpDDSurface
)
652 FIXME("iface %p, surface %p stub!\n", iface
, lpDDSurface
);
657 /*****************************************************************************
658 * IDirect3DViewport3::GetBackgroundDepth
660 * Returns the surface that represents the depth field
663 * lplpDDSurface: Address to store the interface pointer
664 * lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
667 * D3D_OK, because it's a stub
668 * (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
670 *****************************************************************************/
671 static HRESULT WINAPI
672 IDirect3DViewportImpl_GetBackgroundDepth(IDirect3DViewport3
*iface
,
673 IDirectDrawSurface
**lplpDDSurface
,
676 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, lplpDDSurface
, lpValid
);
681 /*****************************************************************************
682 * IDirect3DViewport3::Clear
684 * Clears the render target and / or the z buffer
687 * dwCount: The amount of rectangles to clear. If 0, the whole buffer is
689 * lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
690 * dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
694 * D3DERR_VIEWPORTHASNODEVICE if there's no active device
695 * The return value of IDirect3DDevice7::Clear
697 *****************************************************************************/
698 static HRESULT WINAPI
IDirect3DViewportImpl_Clear(IDirect3DViewport3
*iface
,
699 DWORD dwCount
, D3DRECT
*lpRects
, DWORD dwFlags
)
701 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
702 DWORD color
= 0x00000000;
704 IDirect3DViewport3
*current_viewport
;
705 IDirect3DDevice3
*d3d_device3
;
707 TRACE("iface %p, rect_count %u, rects %p, flags %#x.\n", iface
, dwCount
, lpRects
, dwFlags
);
709 if (This
->active_device
== NULL
) {
710 ERR(" Trying to clear a viewport not attached to a device !\n");
711 return D3DERR_VIEWPORTHASNODEVICE
;
713 d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
715 wined3d_mutex_lock();
717 if (dwFlags
& D3DCLEAR_TARGET
) {
718 if (This
->background
== NULL
) {
719 ERR(" Trying to clear the color buffer without background material !\n");
723 color
= ((int)((This
->background
->mat
.u
.diffuse
.u1
.r
) * 255) << 16)
724 | ((int) ((This
->background
->mat
.u
.diffuse
.u2
.g
) * 255) << 8)
725 | ((int) ((This
->background
->mat
.u
.diffuse
.u3
.b
) * 255) << 0)
726 | ((int) ((This
->background
->mat
.u
.diffuse
.u4
.a
) * 255) << 24);
730 /* Need to temporarily activate viewport to clear it. Previously active one will be restored
732 viewport_activate(This
, TRUE
);
734 hr
= IDirect3DDevice7_Clear(&This
->active_device
->IDirect3DDevice7_iface
, dwCount
, lpRects
,
735 dwFlags
& (D3DCLEAR_ZBUFFER
| D3DCLEAR_TARGET
), color
, 1.0, 0x00000000);
737 IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
);
738 if(current_viewport
) {
739 IDirect3DViewportImpl
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
740 viewport_activate(vp
, TRUE
);
741 IDirect3DViewport3_Release(current_viewport
);
744 wined3d_mutex_unlock();
749 /*****************************************************************************
750 * IDirect3DViewport3::AddLight
752 * Adds an light to the viewport
755 * lpDirect3DLight: Interface of the light to add
759 * DDERR_INVALIDPARAMS if Direct3DLight is NULL
760 * DDERR_INVALIDPARAMS if there are 8 lights or more
762 *****************************************************************************/
763 static HRESULT WINAPI
IDirect3DViewportImpl_AddLight(IDirect3DViewport3
*iface
,
764 IDirect3DLight
*lpDirect3DLight
)
766 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
767 IDirect3DLightImpl
*lpDirect3DLightImpl
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
769 DWORD map
= This
->map_lights
;
771 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
773 wined3d_mutex_lock();
775 if (This
->num_lights
>= 8)
777 wined3d_mutex_unlock();
778 return DDERR_INVALIDPARAMS
;
781 /* Find a light number and update both light and viewports objects accordingly */
787 lpDirect3DLightImpl
->dwLightIndex
= i
;
789 This
->map_lights
|= 1<<i
;
791 /* Add the light in the 'linked' chain */
792 list_add_head(&This
->light_list
, &lpDirect3DLightImpl
->entry
);
793 IDirect3DLight_AddRef(lpDirect3DLight
);
795 /* Attach the light to the viewport */
796 lpDirect3DLightImpl
->active_viewport
= This
;
798 /* If active, activate the light */
799 if (This
->active_device
)
800 light_activate(lpDirect3DLightImpl
);
802 wined3d_mutex_unlock();
807 /*****************************************************************************
808 * IDirect3DViewport3::DeleteLight
810 * Deletes a light from the viewports' light list
813 * lpDirect3DLight: Light to delete
817 * DDERR_INVALIDPARAMS if the light wasn't found
819 *****************************************************************************/
820 static HRESULT WINAPI
IDirect3DViewportImpl_DeleteLight(IDirect3DViewport3
*iface
,
821 IDirect3DLight
*lpDirect3DLight
)
823 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
824 IDirect3DLightImpl
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
826 TRACE("iface %p, light %p.\n", iface
, lpDirect3DLight
);
828 wined3d_mutex_lock();
830 if (l
->active_viewport
!= This
)
832 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
833 wined3d_mutex_unlock();
834 return DDERR_INVALIDPARAMS
;
838 list_remove(&l
->entry
);
839 l
->active_viewport
= NULL
;
840 IDirect3DLight_Release(lpDirect3DLight
);
842 This
->map_lights
&= ~(1 << l
->dwLightIndex
);
844 wined3d_mutex_unlock();
849 /*****************************************************************************
850 * IDirect3DViewport::NextLight
852 * Enumerates the lights associated with the viewport
855 * lpDirect3DLight: Light to start with
856 * lplpDirect3DLight: Address to store the successor to
859 * D3D_OK, because it's a stub
861 *****************************************************************************/
862 static HRESULT WINAPI
IDirect3DViewportImpl_NextLight(IDirect3DViewport3
*iface
,
863 IDirect3DLight
*lpDirect3DLight
, IDirect3DLight
**lplpDirect3DLight
, DWORD dwFlags
)
865 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
866 IDirect3DLightImpl
*l
= unsafe_impl_from_IDirect3DLight(lpDirect3DLight
);
870 TRACE("iface %p, light %p, next_light %p, flags %#x.\n",
871 iface
, lpDirect3DLight
, lplpDirect3DLight
, dwFlags
);
873 if (!lplpDirect3DLight
)
874 return DDERR_INVALIDPARAMS
;
876 wined3d_mutex_lock();
881 if (!l
|| l
->active_viewport
!= This
)
884 WARN("Light %p active viewport is %p.\n", l
, l
->active_viewport
);
888 entry
= list_next(&This
->light_list
, &l
->entry
);
892 entry
= list_head(&This
->light_list
);
896 entry
= list_tail(&This
->light_list
);
901 WARN("Invalid flags %#x.\n", dwFlags
);
907 *lplpDirect3DLight
= (IDirect3DLight
*)LIST_ENTRY(entry
, IDirect3DLightImpl
, entry
);
908 IDirect3DLight_AddRef(*lplpDirect3DLight
);
913 *lplpDirect3DLight
= NULL
;
914 hr
= DDERR_INVALIDPARAMS
;
917 wined3d_mutex_unlock();
922 /*****************************************************************************
923 * IDirect3DViewport2 Methods.
924 *****************************************************************************/
926 /*****************************************************************************
927 * IDirect3DViewport3::GetViewport2
929 * Returns the currently set viewport in a D3DVIEWPORT2 structure.
930 * Similar to IDirect3DViewport3::GetViewport
933 * lpData: Pointer to the structure to fill
937 * DDERR_INVALIDPARAMS if the viewport was set with
938 * IDirect3DViewport3::SetViewport
939 * DDERR_INVALIDPARAMS if Data is NULL
941 *****************************************************************************/
942 static HRESULT WINAPI
943 IDirect3DViewportImpl_GetViewport2(IDirect3DViewport3
*iface
,
944 D3DVIEWPORT2
*lpData
)
946 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
949 TRACE("iface %p, data %p.\n", iface
, lpData
);
951 wined3d_mutex_lock();
952 dwSize
= lpData
->dwSize
;
953 memset(lpData
, 0, dwSize
);
955 memcpy(lpData
, &(This
->viewports
.vp2
), dwSize
);
958 vp2
.dwSize
= sizeof(vp2
);
959 vp2
.dwX
= This
->viewports
.vp1
.dwX
;
960 vp2
.dwY
= This
->viewports
.vp1
.dwY
;
961 vp2
.dwWidth
= This
->viewports
.vp1
.dwWidth
;
962 vp2
.dwHeight
= This
->viewports
.vp1
.dwHeight
;
965 vp2
.dvClipWidth
= 0.0;
966 vp2
.dvClipHeight
= 0.0;
967 vp2
.dvMinZ
= This
->viewports
.vp1
.dvMinZ
;
968 vp2
.dvMaxZ
= This
->viewports
.vp1
.dvMaxZ
;
969 memcpy(lpData
, &vp2
, dwSize
);
974 TRACE(" returning D3DVIEWPORT2 :\n");
975 _dump_D3DVIEWPORT2(lpData
);
978 wined3d_mutex_unlock();
983 /*****************************************************************************
984 * IDirect3DViewport3::SetViewport2
986 * Sets the viewport from a D3DVIEWPORT2 structure
989 * lpData: Viewport to set
994 *****************************************************************************/
995 static HRESULT WINAPI
996 IDirect3DViewportImpl_SetViewport2(IDirect3DViewport3
*iface
,
997 D3DVIEWPORT2
*lpData
)
999 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
1000 IDirect3DViewport3
*current_viewport
;
1002 TRACE("iface %p, data %p.\n", iface
, lpData
);
1004 if (TRACE_ON(ddraw
))
1006 TRACE(" getting D3DVIEWPORT2 :\n");
1007 _dump_D3DVIEWPORT2(lpData
);
1010 wined3d_mutex_lock();
1013 memset(&(This
->viewports
.vp2
), 0, sizeof(This
->viewports
.vp2
));
1014 memcpy(&(This
->viewports
.vp2
), lpData
, lpData
->dwSize
);
1016 if (This
->active_device
) {
1017 IDirect3DDevice3
*d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
1018 IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
);
1019 if (current_viewport
)
1021 if (current_viewport
== iface
) viewport_activate(This
, FALSE
);
1022 IDirect3DViewport3_Release(current_viewport
);
1026 wined3d_mutex_unlock();
1031 /*****************************************************************************
1032 * IDirect3DViewport3 Methods.
1033 *****************************************************************************/
1035 /*****************************************************************************
1036 * IDirect3DViewport3::SetBackgroundDepth2
1038 * Sets a IDirectDrawSurface4 surface as the background depth surface
1041 * lpDDS: Surface to set
1044 * D3D_OK, because it's stub
1046 *****************************************************************************/
1047 static HRESULT WINAPI
1048 IDirect3DViewportImpl_SetBackgroundDepth2(IDirect3DViewport3
*iface
,
1049 IDirectDrawSurface4
*lpDDS
)
1051 FIXME("iface %p, surface %p stub!\n", iface
, lpDDS
);
1056 /*****************************************************************************
1057 * IDirect3DViewport3::GetBackgroundDepth2
1059 * Returns the IDirect3DSurface4 interface to the background depth surface
1062 * lplpDDS: Address to store the interface pointer at
1063 * lpValid: Set to true if a surface is assigned
1066 * D3D_OK because it's a stub
1068 *****************************************************************************/
1069 static HRESULT WINAPI
1070 IDirect3DViewportImpl_GetBackgroundDepth2(IDirect3DViewport3
*iface
,
1071 IDirectDrawSurface4
**lplpDDS
,
1074 FIXME("iface %p, surface %p, valid %p stub!\n", iface
, lplpDDS
, lpValid
);
1079 /*****************************************************************************
1080 * IDirect3DViewport3::Clear2
1082 * Another clearing method
1085 * Count: Number of rectangles to clear
1086 * Rects: Rectangle array to clear
1087 * Flags: Some flags :)
1088 * Color: Color to fill the render target with
1089 * Z: Value to fill the depth buffer with
1090 * Stencil: Value to fill the stencil bits with
1094 *****************************************************************************/
1095 static HRESULT WINAPI
1096 IDirect3DViewportImpl_Clear2(IDirect3DViewport3
*iface
,
1104 IDirect3DViewportImpl
*This
= impl_from_IDirect3DViewport3(iface
);
1106 IDirect3DViewport3
*current_viewport
;
1107 IDirect3DDevice3
*d3d_device3
;
1109 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, depth %.8e, stencil %u.\n",
1110 iface
, dwCount
, lpRects
, dwFlags
, dwColor
, dvZ
, dwStencil
);
1112 wined3d_mutex_lock();
1114 if (This
->active_device
== NULL
) {
1115 ERR(" Trying to clear a viewport not attached to a device !\n");
1116 wined3d_mutex_unlock();
1117 return D3DERR_VIEWPORTHASNODEVICE
;
1119 d3d_device3
= &This
->active_device
->IDirect3DDevice3_iface
;
1120 /* Need to temporarily activate viewport to clear it. Previously active
1121 * one will be restored afterwards. */
1122 viewport_activate(This
, TRUE
);
1124 hr
= IDirect3DDevice7_Clear(&This
->active_device
->IDirect3DDevice7_iface
,
1125 dwCount
, lpRects
, dwFlags
, dwColor
, dvZ
, dwStencil
);
1126 IDirect3DDevice3_GetCurrentViewport(d3d_device3
, ¤t_viewport
);
1127 if(current_viewport
) {
1128 IDirect3DViewportImpl
*vp
= impl_from_IDirect3DViewport3(current_viewport
);
1129 viewport_activate(vp
, TRUE
);
1130 IDirect3DViewport3_Release(current_viewport
);
1133 wined3d_mutex_unlock();
1138 /*****************************************************************************
1140 *****************************************************************************/
1142 static const struct IDirect3DViewport3Vtbl d3d_viewport_vtbl
=
1144 /*** IUnknown Methods ***/
1145 IDirect3DViewportImpl_QueryInterface
,
1146 IDirect3DViewportImpl_AddRef
,
1147 IDirect3DViewportImpl_Release
,
1148 /*** IDirect3DViewport Methods */
1149 IDirect3DViewportImpl_Initialize
,
1150 IDirect3DViewportImpl_GetViewport
,
1151 IDirect3DViewportImpl_SetViewport
,
1152 IDirect3DViewportImpl_TransformVertices
,
1153 IDirect3DViewportImpl_LightElements
,
1154 IDirect3DViewportImpl_SetBackground
,
1155 IDirect3DViewportImpl_GetBackground
,
1156 IDirect3DViewportImpl_SetBackgroundDepth
,
1157 IDirect3DViewportImpl_GetBackgroundDepth
,
1158 IDirect3DViewportImpl_Clear
,
1159 IDirect3DViewportImpl_AddLight
,
1160 IDirect3DViewportImpl_DeleteLight
,
1161 IDirect3DViewportImpl_NextLight
,
1162 /*** IDirect3DViewport2 Methods ***/
1163 IDirect3DViewportImpl_GetViewport2
,
1164 IDirect3DViewportImpl_SetViewport2
,
1165 /*** IDirect3DViewport3 Methods ***/
1166 IDirect3DViewportImpl_SetBackgroundDepth2
,
1167 IDirect3DViewportImpl_GetBackgroundDepth2
,
1168 IDirect3DViewportImpl_Clear2
,
1171 IDirect3DViewportImpl
*unsafe_impl_from_IDirect3DViewport3(IDirect3DViewport3
*iface
)
1173 if (!iface
) return NULL
;
1174 assert(iface
->lpVtbl
== &d3d_viewport_vtbl
);
1175 return CONTAINING_RECORD(iface
, IDirect3DViewportImpl
, IDirect3DViewport3_iface
);
1178 IDirect3DViewportImpl
*unsafe_impl_from_IDirect3DViewport2(IDirect3DViewport2
*iface
)
1180 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1181 if (!iface
) return NULL
;
1182 assert(iface
->lpVtbl
== (IDirect3DViewport2Vtbl
*)&d3d_viewport_vtbl
);
1183 return CONTAINING_RECORD(iface
, IDirect3DViewportImpl
, IDirect3DViewport3_iface
);
1186 IDirect3DViewportImpl
*unsafe_impl_from_IDirect3DViewport(IDirect3DViewport
*iface
)
1188 /* IDirect3DViewport and IDirect3DViewport3 use the same iface. */
1189 if (!iface
) return NULL
;
1190 assert(iface
->lpVtbl
== (IDirect3DViewportVtbl
*)&d3d_viewport_vtbl
);
1191 return CONTAINING_RECORD(iface
, IDirect3DViewportImpl
, IDirect3DViewport3_iface
);
1194 void d3d_viewport_init(IDirect3DViewportImpl
*viewport
, IDirectDrawImpl
*ddraw
)
1196 viewport
->IDirect3DViewport3_iface
.lpVtbl
= &d3d_viewport_vtbl
;
1198 viewport
->ddraw
= ddraw
;
1199 viewport
->use_vp2
= 0xff;
1200 list_init(&viewport
->light_list
);