2 *IDirect3DSwapChain9 implementation
4 *Copyright 2002-2003 Jason Edmeades
5 *Copyright 2002-2003 Raphael Junqueira
6 *Copyright 2005 Oliver Stieber
7 *Copyright 2007-2008 Stefan Dösinger for CodeWeavers
9 *This library is free software; you can redistribute it and/or
10 *modify it under the terms of the GNU Lesser General Public
11 *License as published by the Free Software Foundation; either
12 *version 2.1 of the License, or (at your option) any later version.
14 *This library is distributed in the hope that it will be useful,
15 *but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 *Lesser General Public License for more details.
19 *You should have received a copy of the GNU Lesser General Public
20 *License along with this library; if not, write to the Free Software
21 *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wined3d_private.h"
28 /*TODO: some of the additional parameters may be required to
29 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
30 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
34 WINE_DECLARE_DEBUG_CHANNEL(fps
);
36 #define GLINFO_LOCATION This->device->adapter->gl_info
38 /*IWineD3DSwapChain parts follow: */
39 static void WINAPI
IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain
*iface
)
41 IWineD3DSwapChainImpl
*This
= (IWineD3DSwapChainImpl
*)iface
;
42 WINED3DDISPLAYMODE mode
;
45 TRACE("Destroying swapchain %p\n", iface
);
47 IWineD3DSwapChain_SetGammaRamp(iface
, 0, &This
->orig_gamma
);
49 /* Release the swapchain's draw buffers. Make sure This->backBuffer[0] is
50 * the last buffer to be destroyed, FindContext() depends on that. */
51 if (This
->frontBuffer
)
53 IWineD3DSurface_SetContainer(This
->frontBuffer
, 0);
54 if (IWineD3DSurface_Release(This
->frontBuffer
))
56 WARN("(%p) Something's still holding the front buffer (%p).\n",
57 This
, This
->frontBuffer
);
59 This
->frontBuffer
= NULL
;
64 UINT i
= This
->presentParms
.BackBufferCount
;
68 IWineD3DSurface_SetContainer(This
->backBuffer
[i
], 0);
69 if (IWineD3DSurface_Release(This
->backBuffer
[i
]))
70 WARN("(%p) Something's still holding back buffer %u (%p).\n",
71 This
, i
, This
->backBuffer
[i
]);
73 HeapFree(GetProcessHeap(), 0, This
->backBuffer
);
74 This
->backBuffer
= NULL
;
77 for (i
= 0; i
< This
->num_contexts
; ++i
)
79 context_destroy(This
->device
, This
->context
[i
]);
81 /* Restore the screen resolution if we rendered in fullscreen
82 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
83 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
84 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
86 if(This
->presentParms
.Windowed
== FALSE
&& This
->presentParms
.AutoRestoreDisplayMode
) {
87 mode
.Width
= This
->orig_width
;
88 mode
.Height
= This
->orig_height
;
90 mode
.Format
= This
->orig_fmt
;
91 IWineD3DDevice_SetDisplayMode((IWineD3DDevice
*)This
->device
, 0, &mode
);
93 HeapFree(GetProcessHeap(), 0, This
->context
);
95 HeapFree(GetProcessHeap(), 0, This
);
98 /* A GL context is provided by the caller */
99 static inline void swapchain_blit(IWineD3DSwapChainImpl
*This
, struct wined3d_context
*context
)
102 IWineD3DDeviceImpl
*device
= This
->device
;
103 IWineD3DSurfaceImpl
*backbuffer
= ((IWineD3DSurfaceImpl
*) This
->backBuffer
[0]);
104 UINT w
= backbuffer
->currentDesc
.Width
;
105 UINT h
= backbuffer
->currentDesc
.Height
;
107 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
109 GetClientRect(This
->win_handle
, &window
);
110 if(w
== window
.right
&& h
== window
.bottom
) gl_filter
= GL_NEAREST
;
111 else gl_filter
= GL_LINEAR
;
113 if(gl_info
->supported
[EXT_FRAMEBUFFER_BLIT
])
116 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, &context
->src_fbo
);
117 context_attach_surface_fbo(context
, GL_READ_FRAMEBUFFER
, 0, This
->backBuffer
[0]);
118 context_attach_depth_stencil_fbo(context
, GL_READ_FRAMEBUFFER
, NULL
, FALSE
);
120 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, NULL
);
121 glDrawBuffer(GL_BACK
);
123 glDisable(GL_SCISSOR_TEST
);
124 IWineD3DDeviceImpl_MarkStateDirty(This
->device
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
126 /* Note that the texture is upside down */
127 gl_info
->fbo_ops
.glBlitFramebuffer(0, 0, w
, h
,
128 window
.left
, window
.bottom
, window
.right
, window
.top
,
129 GL_COLOR_BUFFER_BIT
, gl_filter
);
130 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
135 struct wined3d_context
*context2
;
139 float tex_bottom
= h
;
141 context2
= context_acquire(This
->device
, This
->backBuffer
[0], CTXUSAGE_BLIT
);
143 if(backbuffer
->Flags
& SFLAG_NORMCOORD
)
152 context_bind_fbo(context2
, GL_DRAW_FRAMEBUFFER
, NULL
);
154 /* Set up the texture. The surface is not in a IWineD3D*Texture container,
155 * so there are no d3d texture settings to dirtify
157 device
->blitter
->set_shader((IWineD3DDevice
*) device
, backbuffer
->resource
.format_desc
,
158 backbuffer
->texture_target
, backbuffer
->pow2Width
,
159 backbuffer
->pow2Height
);
160 glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
161 glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
163 glDrawBuffer(GL_BACK
);
165 /* Set the viewport to the destination rectandle, disable any projection
166 * transformation set up by CTXUSAGE_BLIT, and draw a (-1,-1)-(1,1) quad.
168 * Back up viewport and matrix to avoid breaking last_was_blit
170 * Note that CTXUSAGE_BLIT set up viewport and ortho to match the surface
171 * size - we want the GL drawable(=window) size.
173 glPushAttrib(GL_VIEWPORT_BIT
);
174 glViewport(window
.left
, window
.top
, window
.right
, window
.bottom
);
175 glMatrixMode(GL_PROJECTION
);
181 glTexCoord2f(tex_left
, tex_bottom
);
185 glTexCoord2f(tex_left
, tex_top
);
189 glTexCoord2f(tex_right
, tex_top
);
193 glTexCoord2f(tex_right
, tex_bottom
);
200 device
->blitter
->unset_shader((IWineD3DDevice
*) device
);
201 checkGLcall("Swapchain present blit(manual)\n");
204 context_release(context2
);
208 static HRESULT WINAPI
IWineD3DSwapChainImpl_Present(IWineD3DSwapChain
*iface
, CONST RECT
*pSourceRect
, CONST RECT
*pDestRect
, HWND hDestWindowOverride
, CONST RGNDATA
*pDirtyRegion
, DWORD dwFlags
) {
209 IWineD3DSwapChainImpl
*This
= (IWineD3DSwapChainImpl
*)iface
;
210 struct wined3d_context
*context
;
214 context
= context_acquire(This
->device
, This
->backBuffer
[0], CTXUSAGE_RESOURCELOAD
);
216 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
217 if (This
->device
->bCursorVisible
&& This
->device
->cursorTexture
)
219 IWineD3DSurfaceImpl cursor
;
222 This
->device
->xScreenSpace
- This
->device
->xHotSpot
,
223 This
->device
->yScreenSpace
- This
->device
->yHotSpot
,
224 This
->device
->xScreenSpace
+ This
->device
->cursorWidth
- This
->device
->xHotSpot
,
225 This
->device
->yScreenSpace
+ This
->device
->cursorHeight
- This
->device
->yHotSpot
,
227 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor
);
228 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
229 * the application because we are only supposed to copy the information out. Using a fake surface
230 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
232 memset(&cursor
, 0, sizeof(cursor
));
233 cursor
.lpVtbl
= &IWineD3DSurface_Vtbl
;
234 cursor
.resource
.ref
= 1;
235 cursor
.resource
.device
= This
->device
;
236 cursor
.resource
.pool
= WINED3DPOOL_SCRATCH
;
237 cursor
.resource
.format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, context
->gl_info
);
238 cursor
.resource
.resourceType
= WINED3DRTYPE_SURFACE
;
239 cursor
.texture_name
= This
->device
->cursorTexture
;
240 cursor
.texture_target
= GL_TEXTURE_2D
;
241 cursor
.texture_level
= 0;
242 cursor
.currentDesc
.Width
= This
->device
->cursorWidth
;
243 cursor
.currentDesc
.Height
= This
->device
->cursorHeight
;
244 cursor
.glRect
.left
= 0;
245 cursor
.glRect
.top
= 0;
246 cursor
.glRect
.right
= cursor
.currentDesc
.Width
;
247 cursor
.glRect
.bottom
= cursor
.currentDesc
.Height
;
248 /* The cursor must have pow2 sizes */
249 cursor
.pow2Width
= cursor
.currentDesc
.Width
;
250 cursor
.pow2Height
= cursor
.currentDesc
.Height
;
251 /* The surface is in the texture */
252 cursor
.Flags
|= SFLAG_INTEXTURE
;
253 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
254 * which is exactly what we want :-)
256 if (This
->presentParms
.Windowed
) {
257 MapWindowPoints(NULL
, This
->win_handle
, (LPPOINT
)&destRect
, 2);
259 IWineD3DSurface_Blt(This
->backBuffer
[0], &destRect
, (IWineD3DSurface
*)&cursor
,
260 NULL
, WINEDDBLT_KEYSRC
, NULL
, WINED3DTEXF_POINT
);
263 if (This
->device
->logo_surface
)
265 /* Blit the logo into the upper left corner of the drawable. */
266 IWineD3DSurface_BltFast(This
->backBuffer
[0], 0, 0, This
->device
->logo_surface
, NULL
, WINEDDBLTFAST_SRCCOLORKEY
);
269 if (pSourceRect
|| pDestRect
) FIXME("Unhandled present rects %s/%s\n", wine_dbgstr_rect(pSourceRect
), wine_dbgstr_rect(pDestRect
));
270 /* TODO: If only source rect or dest rect are supplied then clip the window to match */
271 TRACE("presetting HDC %p\n", This
->context
[0]->hdc
);
273 /* Don't call checkGLcall, as glGetError is not applicable here */
274 if (hDestWindowOverride
&& This
->win_handle
!= hDestWindowOverride
) {
275 IWineD3DSwapChain_SetDestWindowOverride(iface
, hDestWindowOverride
);
278 /* Rendering to a window of different size, presenting partial rectangles,
279 * or rendering to a different window needs help from FBO_blit or a textured
280 * draw. Render the swapchain to a FBO in the future.
282 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
283 * all these issues - this fails if the window is smaller than the backbuffer.
285 if(This
->presentParms
.Windowed
&& !This
->render_to_fbo
&&
286 wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
289 GetClientRect(This
->win_handle
, &window_size
);
290 if(window_size
.bottom
!= This
->presentParms
.BackBufferHeight
||
291 window_size
.right
!= This
->presentParms
.BackBufferWidth
)
293 TRACE("Window size changed from %ux%u to %ux%u, switching to render-to-fbo mode\n",
294 This
->presentParms
.BackBufferWidth
, This
->presentParms
.BackBufferHeight
,
295 window_size
.right
, window_size
.bottom
);
297 IWineD3DSurface_LoadLocation(This
->backBuffer
[0], SFLAG_INTEXTURE
, NULL
);
298 IWineD3DSurface_ModifyLocation(This
->backBuffer
[0], SFLAG_INDRAWABLE
, FALSE
);
299 This
->render_to_fbo
= TRUE
;
301 /* Force the context manager to update the render target configuration next draw */
302 context
->current_rt
= NULL
;
306 if(This
->render_to_fbo
)
308 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
309 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
310 * not allowed(they need the COPY swapeffect)
312 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
315 if(This
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_FLIP
)
317 FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
319 swapchain_blit(This
, context
);
322 SwapBuffers(This
->context
[0]->hdc
); /* TODO: cycle through the swapchain buffers */
324 TRACE("SwapBuffers called, Starting new frame\n");
328 DWORD time
= GetTickCount();
330 /* every 1.5 seconds */
331 if (time
- This
->prev_time
> 1500) {
332 TRACE_(fps
)("%p @ approx %.2ffps\n", This
, 1000.0*This
->frames
/(time
- This
->prev_time
));
333 This
->prev_time
= time
;
338 #if defined(FRAME_DEBUGGING)
340 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES
) {
343 FIXME("Enabling D3D Trace\n");
344 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE
, __wine_dbch_d3d
, 1);
345 #if defined(SHOW_FRAME_MAKEUP)
346 FIXME("Singe Frame snapshots Starting\n");
347 isDumpingFrames
= TRUE
;
349 glClear(GL_COLOR_BUFFER_BIT
);
353 #if defined(SINGLE_FRAME_DEBUGGING)
355 #if defined(SHOW_FRAME_MAKEUP)
356 FIXME("Singe Frame snapshots Finishing\n");
357 isDumpingFrames
= FALSE
;
359 FIXME("Singe Frame trace complete\n");
360 DeleteFileA("C:\\D3DTRACE");
361 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE
, __wine_dbch_d3d
, 0);
367 #if defined(SHOW_FRAME_MAKEUP)
368 FIXME("Single Frame snapshots Finishing\n");
369 isDumpingFrames
= FALSE
;
371 FIXME("Disabling D3D Trace\n");
372 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE
, __wine_dbch_d3d
, 0);
378 /* This is disabled, but the code left in for debug purposes.
380 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
381 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
382 * The Debug runtime does the same on Windows. However, a few games do not redraw the
383 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
385 * Tests show that the content of the back buffer after a discard flip is indeed not
386 * reliable, so no game can depend on the exact content. However, it resembles the
387 * old contents in some way, for example by showing fragments at other locations. In
388 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
389 * gets a dark background image. If we clear it with a bright ugly color, the game's
390 * bug shows up much more than it does on Windows, and the players see single pixels
392 * (The Max Payne bug has been confirmed on Windows with the debug runtime)
394 if (FALSE
&& This
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
) {
395 TRACE("Clearing the color buffer with cyan color\n");
397 IWineD3DDevice_Clear((IWineD3DDevice
*)This
->device
, 0, NULL
,
398 WINED3DCLEAR_TARGET
, 0xff00ffff, 1.0f
, 0);
401 if(!This
->render_to_fbo
&&
402 ( ((IWineD3DSurfaceImpl
*) This
->frontBuffer
)->Flags
& SFLAG_INSYSMEM
||
403 ((IWineD3DSurfaceImpl
*) This
->backBuffer
[0])->Flags
& SFLAG_INSYSMEM
) ) {
404 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
405 * Doesn't work with render_to_fbo because we're not flipping
407 IWineD3DSurfaceImpl
*front
= (IWineD3DSurfaceImpl
*) This
->frontBuffer
;
408 IWineD3DSurfaceImpl
*back
= (IWineD3DSurfaceImpl
*) This
->backBuffer
[0];
410 if(front
->resource
.size
== back
->resource
.size
) {
412 flip_surface(front
, back
);
414 /* Tell the front buffer surface that is has been modified. However,
415 * the other locations were preserved during that, so keep the flags.
416 * This serves to update the emulated overlay, if any
418 fbflags
= front
->Flags
;
419 IWineD3DSurface_ModifyLocation(This
->frontBuffer
, SFLAG_INDRAWABLE
, TRUE
);
420 front
->Flags
= fbflags
;
422 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) front
, SFLAG_INDRAWABLE
, TRUE
);
423 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) back
, SFLAG_INDRAWABLE
, TRUE
);
426 IWineD3DSurface_ModifyLocation(This
->frontBuffer
, SFLAG_INDRAWABLE
, TRUE
);
427 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
428 * and INTEXTURE copies can keep their old content if they have any defined content.
429 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
430 * the texture / sysmem copy needs to be reloaded from the drawable
432 if(This
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_FLIP
) {
433 IWineD3DSurface_ModifyLocation(This
->backBuffer
[0], SFLAG_INDRAWABLE
, TRUE
);
437 if (This
->device
->stencilBufferTarget
)
439 if (This
->presentParms
.Flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
440 || ((IWineD3DSurfaceImpl
*)This
->device
->stencilBufferTarget
)->Flags
& SFLAG_DISCARD
)
442 surface_modify_ds_location(This
->device
->stencilBufferTarget
, SFLAG_DS_DISCARDED
);
446 if (This
->presentParms
.PresentationInterval
!= WINED3DPRESENT_INTERVAL_IMMEDIATE
447 && context
->gl_info
->supported
[SGI_VIDEO_SYNC
])
449 retval
= GL_EXTCALL(glXGetVideoSyncSGI(&sync
));
451 ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval
);
454 switch(This
->presentParms
.PresentationInterval
) {
455 case WINED3DPRESENT_INTERVAL_DEFAULT
:
456 case WINED3DPRESENT_INTERVAL_ONE
:
457 if(sync
<= This
->vSyncCounter
) {
458 retval
= GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This
->vSyncCounter
));
460 This
->vSyncCounter
= sync
;
463 case WINED3DPRESENT_INTERVAL_TWO
:
464 if(sync
<= This
->vSyncCounter
+ 1) {
465 retval
= GL_EXTCALL(glXWaitVideoSyncSGI(2, This
->vSyncCounter
& 0x1, &This
->vSyncCounter
));
467 This
->vSyncCounter
= sync
;
470 case WINED3DPRESENT_INTERVAL_THREE
:
471 if(sync
<= This
->vSyncCounter
+ 2) {
472 retval
= GL_EXTCALL(glXWaitVideoSyncSGI(3, This
->vSyncCounter
% 0x3, &This
->vSyncCounter
));
474 This
->vSyncCounter
= sync
;
477 case WINED3DPRESENT_INTERVAL_FOUR
:
478 if(sync
<= This
->vSyncCounter
+ 3) {
479 retval
= GL_EXTCALL(glXWaitVideoSyncSGI(4, This
->vSyncCounter
& 0x3, &This
->vSyncCounter
));
481 This
->vSyncCounter
= sync
;
485 FIXME("Unknown presentation interval %08x\n", This
->presentParms
.PresentationInterval
);
489 context_release(context
);
491 TRACE("returning\n");
495 static HRESULT WINAPI
IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain
*iface
, HWND window
) {
496 IWineD3DSwapChainImpl
*This
= (IWineD3DSwapChainImpl
*)iface
;
497 WINED3DLOCKED_RECT r
;
500 if(window
== This
->win_handle
) return WINED3D_OK
;
502 TRACE("Performing dest override of swapchain %p from window %p to %p\n", This
, This
->win_handle
, window
);
503 if (This
->context
[0] == This
->device
->contexts
[0])
505 /* The primary context 'owns' all the opengl resources. Destroying and recreating that context requires downloading
506 * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
507 * and reload the resources
509 delete_opengl_contexts((IWineD3DDevice
*)This
->device
, iface
);
510 This
->win_handle
= window
;
511 create_primary_opengl_context((IWineD3DDevice
*)This
->device
, iface
);
515 This
->win_handle
= window
;
517 /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
518 * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
519 * So lock read only, copy the surface out, then lock with the discard flag and write back
521 IWineD3DSurface_LockRect(This
->backBuffer
[0], &r
, NULL
, WINED3DLOCK_READONLY
);
522 mem
= HeapAlloc(GetProcessHeap(), 0, r
.Pitch
* ((IWineD3DSurfaceImpl
*) This
->backBuffer
[0])->currentDesc
.Height
);
523 memcpy(mem
, r
.pBits
, r
.Pitch
* ((IWineD3DSurfaceImpl
*) This
->backBuffer
[0])->currentDesc
.Height
);
524 IWineD3DSurface_UnlockRect(This
->backBuffer
[0]);
526 context_destroy(This
->device
, This
->context
[0]);
527 This
->context
[0] = context_create(This
->device
, (IWineD3DSurfaceImpl
*)This
->frontBuffer
,
528 This
->win_handle
, FALSE
/* pbuffer */, &This
->presentParms
);
529 context_release(This
->context
[0]);
531 IWineD3DSurface_LockRect(This
->backBuffer
[0], &r
, NULL
, WINED3DLOCK_DISCARD
);
532 memcpy(r
.pBits
, mem
, r
.Pitch
* ((IWineD3DSurfaceImpl
*) This
->backBuffer
[0])->currentDesc
.Height
);
533 HeapFree(GetProcessHeap(), 0, mem
);
534 IWineD3DSurface_UnlockRect(This
->backBuffer
[0]);
539 static const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl
=
542 IWineD3DBaseSwapChainImpl_QueryInterface
,
543 IWineD3DBaseSwapChainImpl_AddRef
,
544 IWineD3DBaseSwapChainImpl_Release
,
545 /* IWineD3DSwapChain */
546 IWineD3DBaseSwapChainImpl_GetParent
,
547 IWineD3DSwapChainImpl_Destroy
,
548 IWineD3DBaseSwapChainImpl_GetDevice
,
549 IWineD3DSwapChainImpl_Present
,
550 IWineD3DSwapChainImpl_SetDestWindowOverride
,
551 IWineD3DBaseSwapChainImpl_GetFrontBufferData
,
552 IWineD3DBaseSwapChainImpl_GetBackBuffer
,
553 IWineD3DBaseSwapChainImpl_GetRasterStatus
,
554 IWineD3DBaseSwapChainImpl_GetDisplayMode
,
555 IWineD3DBaseSwapChainImpl_GetPresentParameters
,
556 IWineD3DBaseSwapChainImpl_SetGammaRamp
,
557 IWineD3DBaseSwapChainImpl_GetGammaRamp
560 static LONG
fullscreen_style(LONG style
)
562 /* Make sure the window is managed, otherwise we won't get keyboard input. */
563 style
|= WS_POPUP
| WS_SYSMENU
;
564 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
569 static LONG
fullscreen_exstyle(LONG exstyle
)
571 /* Filter out window decorations. */
572 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
577 void swapchain_setup_fullscreen_window(IWineD3DSwapChainImpl
*swapchain
, UINT w
, UINT h
)
579 IWineD3DDeviceImpl
*device
= swapchain
->device
;
580 HWND window
= swapchain
->win_handle
;
583 TRACE("Setting up window %p for fullscreen mode.\n", window
);
585 if (device
->style
|| device
->exStyle
)
587 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
588 window
, device
->style
, device
->exStyle
);
591 device
->style
= GetWindowLongW(window
, GWL_STYLE
);
592 device
->exStyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
594 style
= fullscreen_style(device
->style
);
595 exstyle
= fullscreen_exstyle(device
->exStyle
);
597 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
598 device
->style
, device
->exStyle
, style
, exstyle
);
600 SetWindowLongW(window
, GWL_STYLE
, style
);
601 SetWindowLongW(window
, GWL_EXSTYLE
, exstyle
);
602 SetWindowPos(window
, HWND_TOP
, 0, 0, w
, h
, SWP_FRAMECHANGED
| SWP_SHOWWINDOW
);
605 void swapchain_restore_fullscreen_window(IWineD3DSwapChainImpl
*swapchain
)
607 IWineD3DDeviceImpl
*device
= swapchain
->device
;
608 HWND window
= swapchain
->win_handle
;
611 if (!device
->style
&& !device
->exStyle
) return;
613 TRACE("Restoring window style of window %p to %08x, %08x.\n",
614 window
, device
->style
, device
->exStyle
);
616 style
= GetWindowLongW(window
, GWL_STYLE
);
617 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
619 /* Only restore the style if the application didn't modify it during the
620 * fullscreen phase. Some applications change it before calling Reset()
621 * when switching between windowed and fullscreen modes (HL2), some
622 * depend on the original style (Eve Online). */
623 if (style
== fullscreen_style(device
->style
) && exstyle
== fullscreen_exstyle(device
->exStyle
))
625 SetWindowLongW(window
, GWL_STYLE
, device
->style
);
626 SetWindowLongW(window
, GWL_EXSTYLE
, device
->exStyle
);
629 /* Delete the old values. */
633 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
| SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
);
637 HRESULT
swapchain_init(IWineD3DSwapChainImpl
*swapchain
, WINED3DSURFTYPE surface_type
,
638 IWineD3DDeviceImpl
*device
, WINED3DPRESENT_PARAMETERS
*present_parameters
, IUnknown
*parent
)
640 const struct wined3d_adapter
*adapter
= device
->adapter
;
641 const struct GlPixelFormatDesc
*format_desc
;
642 BOOL displaymode_set
= FALSE
;
643 WINED3DDISPLAYMODE mode
;
649 if (present_parameters
->BackBufferCount
> WINED3DPRESENT_BACK_BUFFER_MAX
)
651 FIXME("The application requested %u back buffers, this is not supported.\n",
652 present_parameters
->BackBufferCount
);
653 return WINED3DERR_INVALIDCALL
;
656 if (present_parameters
->BackBufferCount
> 1)
658 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
659 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
662 switch (surface_type
)
665 swapchain
->lpVtbl
= &IWineGDISwapChain_Vtbl
;
669 swapchain
->lpVtbl
= &IWineD3DSwapChain_Vtbl
;
672 case SURFACE_UNKNOWN
:
673 FIXME("Caller tried to create a SURFACE_UNKNOWN swapchain.\n");
674 return WINED3DERR_INVALIDCALL
;
677 window
= present_parameters
->hDeviceWindow
? present_parameters
->hDeviceWindow
: device
->createParms
.hFocusWindow
;
679 swapchain
->device
= device
;
680 swapchain
->parent
= parent
;
682 swapchain
->win_handle
= window
;
684 if (!present_parameters
->Windowed
&& window
)
686 swapchain_setup_fullscreen_window(swapchain
, present_parameters
->BackBufferWidth
,
687 present_parameters
->BackBufferHeight
);
690 IWineD3D_GetAdapterDisplayMode(device
->wined3d
, adapter
->ordinal
, &mode
);
691 swapchain
->orig_width
= mode
.Width
;
692 swapchain
->orig_height
= mode
.Height
;
693 swapchain
->orig_fmt
= mode
.Format
;
694 format_desc
= getFormatDescEntry(mode
.Format
, &adapter
->gl_info
);
696 GetClientRect(window
, &client_rect
);
697 if (present_parameters
->Windowed
698 && (!present_parameters
->BackBufferWidth
|| !present_parameters
->BackBufferHeight
699 || present_parameters
->BackBufferFormat
== WINED3DFMT_UNKNOWN
))
702 if (!present_parameters
->BackBufferWidth
)
704 present_parameters
->BackBufferWidth
= client_rect
.right
;
705 TRACE("Updating width to %u.\n", present_parameters
->BackBufferWidth
);
708 if (!present_parameters
->BackBufferHeight
)
710 present_parameters
->BackBufferHeight
= client_rect
.bottom
;
711 TRACE("Updating height to %u.\n", present_parameters
->BackBufferHeight
);
714 if (present_parameters
->BackBufferFormat
== WINED3DFMT_UNKNOWN
)
716 present_parameters
->BackBufferFormat
= swapchain
->orig_fmt
;
717 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->orig_fmt
));
720 swapchain
->presentParms
= *present_parameters
;
722 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
723 && (present_parameters
->BackBufferWidth
!= client_rect
.right
724 || present_parameters
->BackBufferHeight
!= client_rect
.bottom
))
726 TRACE("Rendering to FBO. Backbuffer %ux%u, window %ux%u.\n",
727 present_parameters
->BackBufferWidth
,
728 present_parameters
->BackBufferHeight
,
729 client_rect
.right
, client_rect
.bottom
);
730 swapchain
->render_to_fbo
= TRUE
;
733 TRACE("Creating front buffer.\n");
734 hr
= IWineD3DDeviceParent_CreateRenderTarget(device
->device_parent
, parent
,
735 swapchain
->presentParms
.BackBufferWidth
, swapchain
->presentParms
.BackBufferHeight
,
736 swapchain
->presentParms
.BackBufferFormat
, swapchain
->presentParms
.MultiSampleType
,
737 swapchain
->presentParms
.MultiSampleQuality
, TRUE
/* Lockable */, &swapchain
->frontBuffer
);
740 WARN("Failed to create front buffer, hr %#x.\n", hr
);
744 IWineD3DSurface_SetContainer(swapchain
->frontBuffer
, (IWineD3DBase
*)swapchain
);
745 ((IWineD3DSurfaceImpl
*)swapchain
->frontBuffer
)->Flags
|= SFLAG_SWAPCHAIN
;
746 if (surface_type
== SURFACE_OPENGL
)
748 IWineD3DSurface_ModifyLocation(swapchain
->frontBuffer
, SFLAG_INDRAWABLE
, TRUE
);
751 /* MSDN says we're only allowed a single fullscreen swapchain per device,
752 * so we should really check to see if there is a fullscreen swapchain
753 * already. Does a single head count as full screen? */
755 if (!present_parameters
->Windowed
)
757 WINED3DDISPLAYMODE mode
;
759 /* Change the display settings */
760 mode
.Width
= present_parameters
->BackBufferWidth
;
761 mode
.Height
= present_parameters
->BackBufferHeight
;
762 mode
.Format
= present_parameters
->BackBufferFormat
;
763 mode
.RefreshRate
= present_parameters
->FullScreen_RefreshRateInHz
;
765 hr
= IWineD3DDevice_SetDisplayMode((IWineD3DDevice
*)device
, 0, &mode
);
768 WARN("Failed to set display mode, hr %#x.\n", hr
);
771 displaymode_set
= TRUE
;
774 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(swapchain
->context
));
775 if (!swapchain
->context
)
777 ERR("Failed to create the context array.\n");
781 swapchain
->num_contexts
= 1;
783 if (surface_type
== SURFACE_OPENGL
)
785 swapchain
->context
[0] = context_create(device
, (IWineD3DSurfaceImpl
*)swapchain
->frontBuffer
,
786 window
, FALSE
/* pbuffer */, present_parameters
);
787 if (!swapchain
->context
[0])
789 WARN("Failed to create context.\n");
790 hr
= WINED3DERR_NOTAVAILABLE
;
794 swapchain
->context
[0]->render_offscreen
= swapchain
->render_to_fbo
;
798 swapchain
->context
[0] = NULL
;
801 if (swapchain
->presentParms
.BackBufferCount
> 0)
803 swapchain
->backBuffer
= HeapAlloc(GetProcessHeap(), 0,
804 sizeof(*swapchain
->backBuffer
) * swapchain
->presentParms
.BackBufferCount
);
805 if (!swapchain
->backBuffer
)
807 ERR("Failed to allocate backbuffer array memory.\n");
812 for (i
= 0; i
< swapchain
->presentParms
.BackBufferCount
; ++i
)
814 TRACE("Creating back buffer %u.\n", i
);
815 hr
= IWineD3DDeviceParent_CreateRenderTarget(device
->device_parent
, parent
,
816 swapchain
->presentParms
.BackBufferWidth
, swapchain
->presentParms
.BackBufferHeight
,
817 swapchain
->presentParms
.BackBufferFormat
, swapchain
->presentParms
.MultiSampleType
,
818 swapchain
->presentParms
.MultiSampleQuality
, TRUE
/* Lockable */, &swapchain
->backBuffer
[i
]);
821 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
825 IWineD3DSurface_SetContainer(swapchain
->backBuffer
[i
], (IWineD3DBase
*)swapchain
);
826 ((IWineD3DSurfaceImpl
*)swapchain
->backBuffer
[i
])->Flags
|= SFLAG_SWAPCHAIN
;
828 if (surface_type
== SURFACE_OPENGL
)
831 glDrawBuffer(GL_BACK
);
832 checkGLcall("glDrawBuffer(GL_BACK)");
839 /* Single buffering - draw to front buffer */
840 if (surface_type
== SURFACE_OPENGL
)
843 glDrawBuffer(GL_FRONT
);
844 checkGLcall("glDrawBuffer(GL_FRONT)");
849 if (swapchain
->context
[0]) context_release(swapchain
->context
[0]);
851 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
852 if (present_parameters
->EnableAutoDepthStencil
&& surface_type
== SURFACE_OPENGL
)
854 TRACE("Creating depth/stencil buffer.\n");
855 if (!device
->auto_depth_stencil_buffer
)
857 hr
= IWineD3DDeviceParent_CreateDepthStencilSurface(device
->device_parent
, parent
,
858 swapchain
->presentParms
.BackBufferWidth
, swapchain
->presentParms
.BackBufferHeight
,
859 swapchain
->presentParms
.AutoDepthStencilFormat
, swapchain
->presentParms
.MultiSampleType
,
860 swapchain
->presentParms
.MultiSampleQuality
, FALSE
/* FIXME: Discard */,
861 &device
->auto_depth_stencil_buffer
);
864 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr
);
868 IWineD3DSurface_SetContainer(device
->auto_depth_stencil_buffer
, NULL
);
872 IWineD3DSwapChain_GetGammaRamp((IWineD3DSwapChain
*)swapchain
, &swapchain
->orig_gamma
);
883 /* Change the display settings */
884 memset(&devmode
, 0, sizeof(devmode
));
885 devmode
.dmSize
= sizeof(devmode
);
886 devmode
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
887 devmode
.dmBitsPerPel
= format_desc
->byte_count
* 8;
888 devmode
.dmPelsWidth
= swapchain
->orig_width
;
889 devmode
.dmPelsHeight
= swapchain
->orig_height
;
890 ChangeDisplaySettingsExW(adapter
->DeviceName
, &devmode
, NULL
, CDS_FULLSCREEN
, NULL
);
893 if (swapchain
->backBuffer
)
895 for (i
= 0; i
< swapchain
->presentParms
.BackBufferCount
; ++i
)
897 if (swapchain
->backBuffer
[i
]) IWineD3DSurface_Release(swapchain
->backBuffer
[i
]);
899 HeapFree(GetProcessHeap(), 0, swapchain
->backBuffer
);
902 if (swapchain
->context
&& swapchain
->context
[0])
904 context_release(swapchain
->context
[0]);
905 context_destroy(device
, swapchain
->context
[0]);
908 if (swapchain
->frontBuffer
) IWineD3DSurface_Release(swapchain
->frontBuffer
);
913 struct wined3d_context
*swapchain_create_context_for_thread(IWineD3DSwapChain
*iface
)
915 IWineD3DSwapChainImpl
*This
= (IWineD3DSwapChainImpl
*) iface
;
916 struct wined3d_context
**newArray
;
917 struct wined3d_context
*ctx
;
919 TRACE("Creating a new context for swapchain %p, thread %d\n", This
, GetCurrentThreadId());
921 ctx
= context_create(This
->device
, (IWineD3DSurfaceImpl
*)This
->frontBuffer
,
922 This
->context
[0]->win_handle
, FALSE
/* pbuffer */, &This
->presentParms
);
925 ERR("Failed to create a new context for the swapchain\n");
928 context_release(ctx
);
930 newArray
= HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray
) * This
->num_contexts
+ 1);
932 ERR("Out of memory when trying to allocate a new context array\n");
933 context_destroy(This
->device
, ctx
);
936 memcpy(newArray
, This
->context
, sizeof(*newArray
) * This
->num_contexts
);
937 HeapFree(GetProcessHeap(), 0, This
->context
);
938 newArray
[This
->num_contexts
] = ctx
;
939 This
->context
= newArray
;
940 This
->num_contexts
++;
942 TRACE("Returning context %p\n", ctx
);
946 void get_drawable_size_swapchain(struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
948 IWineD3DSurfaceImpl
*surface
= (IWineD3DSurfaceImpl
*)context
->current_rt
;
949 /* The drawable size of an onscreen drawable is the surface size.
950 * (Actually: The window size, but the surface is created in window size) */
951 *width
= surface
->currentDesc
.Width
;
952 *height
= surface
->currentDesc
.Height
;