2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
27 WINE_DECLARE_DEBUG_CHANNEL(fps
);
29 /* Do not call while under the GL lock. */
30 static void swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
32 struct wined3d_display_mode mode
;
35 TRACE("Destroying swapchain %p.\n", swapchain
);
37 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain
->front_buffer
)
43 surface_set_container(swapchain
->front_buffer
, WINED3D_CONTAINER_NONE
, NULL
);
44 if (wined3d_surface_decref(swapchain
->front_buffer
))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
46 swapchain
->front_buffer
= NULL
;
49 if (swapchain
->back_buffers
)
51 i
= swapchain
->presentParms
.BackBufferCount
;
55 surface_set_container(swapchain
->back_buffers
[i
], WINED3D_CONTAINER_NONE
, NULL
);
56 if (wined3d_surface_decref(swapchain
->back_buffers
[i
]))
57 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
59 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
60 swapchain
->back_buffers
= NULL
;
63 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
65 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
67 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
69 /* Restore the screen resolution if we rendered in fullscreen.
70 * This will restore the screen resolution to what it was before creating
71 * the swapchain. In case of d3d8 and d3d9 this will be the original
72 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
73 * sets the resolution before starting up Direct3D, thus orig_width and
74 * orig_height will be equal to the modes in the presentation params. */
75 if (!swapchain
->presentParms
.Windowed
&& swapchain
->presentParms
.AutoRestoreDisplayMode
)
77 mode
.width
= swapchain
->orig_width
;
78 mode
.height
= swapchain
->orig_height
;
79 mode
.refresh_rate
= 0;
80 mode
.format_id
= swapchain
->orig_fmt
;
81 wined3d_device_set_display_mode(swapchain
->device
, 0, &mode
);
84 if (swapchain
->backup_dc
)
86 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain
->backup_wnd
, swapchain
->backup_dc
);
88 ReleaseDC(swapchain
->backup_wnd
, swapchain
->backup_dc
);
89 DestroyWindow(swapchain
->backup_wnd
);
93 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
95 ULONG refcount
= InterlockedIncrement(&swapchain
->ref
);
97 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
102 /* Do not call while under the GL lock. */
103 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
105 ULONG refcount
= InterlockedDecrement(&swapchain
->ref
);
107 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
111 swapchain_cleanup(swapchain
);
112 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
113 HeapFree(GetProcessHeap(), 0, swapchain
);
119 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
121 TRACE("swapchain %p.\n", swapchain
);
123 return swapchain
->parent
;
126 HRESULT CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
129 window
= swapchain
->device_window
;
130 if (window
== swapchain
->win_handle
)
133 TRACE("Setting swapchain %p window from %p to %p.\n",
134 swapchain
, swapchain
->win_handle
, window
);
135 swapchain
->win_handle
= window
;
140 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
141 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
142 const RGNDATA
*dirty_region
, DWORD flags
)
144 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
145 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
146 dst_window_override
, dirty_region
, flags
);
148 wined3d_swapchain_set_window(swapchain
, dst_window_override
);
150 return swapchain
->swapchain_ops
->swapchain_present(swapchain
,
151 src_rect
, dst_rect
, dirty_region
, flags
);
154 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
155 struct wined3d_surface
*dst_surface
)
157 struct wined3d_surface
*src_surface
;
158 RECT src_rect
, dst_rect
;
160 TRACE("swapchain %p, dst_surface %p.\n", swapchain
, dst_surface
);
162 src_surface
= swapchain
->front_buffer
;
163 SetRect(&src_rect
, 0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
);
166 if (swapchain
->presentParms
.Windowed
)
168 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
169 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
170 wine_dbgstr_rect(&dst_rect
));
173 return wined3d_surface_blt(dst_surface
, &dst_rect
, src_surface
, &src_rect
, 0, NULL
, WINED3DTEXF_POINT
);
176 HRESULT CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
177 UINT back_buffer_idx
, WINED3DBACKBUFFER_TYPE type
, struct wined3d_surface
**back_buffer
)
179 TRACE("swapchain %p, back_buffer_idx %u, type %#x, back_buffer %p.\n",
180 swapchain
, back_buffer_idx
, type
, back_buffer
);
182 /* Return invalid if there is no backbuffer array, otherwise it will
183 * crash when ddraw is used (there swapchain->back_buffers is always
184 * NULL). We need this because this function is called from
185 * stateblock_init_default_state() to get the default scissorrect
187 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->presentParms
.BackBufferCount
)
189 WARN("Invalid back buffer index.\n");
190 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
191 * here in wined3d to avoid problems in other libs. */
193 return WINED3DERR_INVALIDCALL
;
196 *back_buffer
= swapchain
->back_buffers
[back_buffer_idx
];
198 wined3d_surface_incref(*back_buffer
);
200 TRACE("Returning back buffer %p.\n", *back_buffer
);
205 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
206 WINED3DRASTER_STATUS
*raster_status
)
209 /* No OpenGL equivalent */
212 FIXME("swapchain %p, raster_status %p stub!\n", swapchain
, raster_status
);
216 /* Obtaining the raster status is a widely implemented but optional
217 * feature. When this method returns OK StarCraft 2 expects the
218 * raster_status->InVBlank value to actually change over time. To prevent
219 * StarCraft 2 from running in an infinite loop at startup this method
220 * returns INVALIDCALL. */
221 return WINED3DERR_INVALIDCALL
;
224 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
225 struct wined3d_display_mode
*mode
)
229 TRACE("swapchain %p, mode %p.\n", swapchain
, mode
);
231 hr
= wined3d_get_adapter_display_mode(swapchain
->device
->wined3d
, swapchain
->device
->adapter
->ordinal
, mode
);
233 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
234 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
239 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
241 TRACE("swapchain %p.\n", swapchain
);
243 return swapchain
->device
;
246 HRESULT CDECL
wined3d_swapchain_get_present_parameters(const struct wined3d_swapchain
*swapchain
,
247 WINED3DPRESENT_PARAMETERS
*present_parameters
)
249 TRACE("swapchain %p, present_parameters %p.\n", swapchain
, present_parameters
);
251 *present_parameters
= swapchain
->presentParms
;
256 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
257 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
261 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
264 FIXME("Ignoring flags %#x.\n", flags
);
266 dc
= GetDC(swapchain
->device_window
);
267 SetDeviceGammaRamp(dc
, (void *)ramp
);
268 ReleaseDC(swapchain
->device_window
, dc
);
273 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
274 struct wined3d_gamma_ramp
*ramp
)
278 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
280 dc
= GetDC(swapchain
->device_window
);
281 GetDeviceGammaRamp(dc
, ramp
);
282 ReleaseDC(swapchain
->device_window
, dc
);
287 /* A GL context is provided by the caller */
288 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
289 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
291 struct wined3d_surface
*backbuffer
= swapchain
->back_buffers
[0];
292 UINT src_w
= src_rect
->right
- src_rect
->left
;
293 UINT src_h
= src_rect
->bottom
- src_rect
->top
;
295 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
299 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
300 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
302 if (src_w
== dst_rect
->right
- dst_rect
->left
&& src_h
== dst_rect
->bottom
- dst_rect
->top
)
303 gl_filter
= GL_NEAREST
;
305 gl_filter
= GL_LINEAR
;
307 GetClientRect(swapchain
->win_handle
, &win_rect
);
308 win_h
= win_rect
.bottom
- win_rect
.top
;
310 if (gl_info
->fbo_ops
.glBlitFramebuffer
&& is_identity_fixup(backbuffer
->resource
.format
->color_fixup
))
312 DWORD location
= SFLAG_INTEXTURE
;
314 if (backbuffer
->resource
.multisample_type
)
316 location
= SFLAG_INRB_RESOLVED
;
317 surface_load_location(backbuffer
, location
, NULL
);
321 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, backbuffer
, NULL
, location
);
322 glReadBuffer(GL_COLOR_ATTACHMENT0
);
323 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
325 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, swapchain
->front_buffer
, NULL
, SFLAG_INDRAWABLE
);
326 context_set_draw_buffer(context
, GL_BACK
);
327 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
329 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
330 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
));
331 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
));
332 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
));
333 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
));
335 glDisable(GL_SCISSOR_TEST
);
336 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
338 /* Note that the texture is upside down */
339 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
,
340 dst_rect
->left
, win_h
- dst_rect
->top
, dst_rect
->right
, win_h
- dst_rect
->bottom
,
341 GL_COLOR_BUFFER_BIT
, gl_filter
);
342 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
347 struct wined3d_device
*device
= swapchain
->device
;
348 struct wined3d_context
*context2
;
349 float tex_left
= src_rect
->left
;
350 float tex_top
= src_rect
->top
;
351 float tex_right
= src_rect
->right
;
352 float tex_bottom
= src_rect
->bottom
;
354 context2
= context_acquire(device
, swapchain
->back_buffers
[0]);
355 context_apply_blit_state(context2
, device
);
357 if (backbuffer
->flags
& SFLAG_NORMCOORD
)
365 if (is_complex_fixup(backbuffer
->resource
.format
->color_fixup
))
366 gl_filter
= GL_NEAREST
;
369 context_apply_fbo_state_blit(context2
, GL_FRAMEBUFFER
, swapchain
->front_buffer
, NULL
, SFLAG_INDRAWABLE
);
371 /* Set up the texture. The surface is not in a wined3d_texture
372 * container, so there are no D3D texture settings to dirtify. */
373 device
->blitter
->set_shader(device
->blit_priv
, context2
, backbuffer
);
374 glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MIN_FILTER
, gl_filter
);
375 glTexParameteri(backbuffer
->texture_target
, GL_TEXTURE_MAG_FILTER
, gl_filter
);
377 context_set_draw_buffer(context
, GL_BACK
);
379 /* Set the viewport to the destination rectandle, disable any projection
380 * transformation set up by context_apply_blit_state(), and draw a
381 * (-1,-1)-(1,1) quad.
383 * Back up viewport and matrix to avoid breaking last_was_blit
385 * Note that context_apply_blit_state() set up viewport and ortho to
386 * match the surface size - we want the GL drawable(=window) size. */
387 glPushAttrib(GL_VIEWPORT_BIT
);
388 glViewport(dst_rect
->left
, win_h
- dst_rect
->bottom
, dst_rect
->right
, win_h
- dst_rect
->top
);
389 glMatrixMode(GL_PROJECTION
);
395 glTexCoord2f(tex_left
, tex_bottom
);
399 glTexCoord2f(tex_left
, tex_top
);
403 glTexCoord2f(tex_right
, tex_top
);
407 glTexCoord2f(tex_right
, tex_bottom
);
414 device
->blitter
->unset_shader(context
->gl_info
);
415 checkGLcall("Swapchain present blit(manual)\n");
418 context_release(context2
);
422 static HRESULT
swapchain_gl_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
423 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
425 const struct wined3d_fb_state
*fb
= &swapchain
->device
->fb
;
426 const struct wined3d_gl_info
*gl_info
;
427 struct wined3d_context
*context
;
428 RECT src_rect
, dst_rect
;
431 context
= context_acquire(swapchain
->device
, swapchain
->back_buffers
[0]);
434 context_release(context
);
435 WARN("Invalid context, skipping present.\n");
439 gl_info
= context
->gl_info
;
441 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
442 if (swapchain
->device
->bCursorVisible
&&
443 swapchain
->device
->cursorTexture
&&
444 !swapchain
->device
->hardwareCursor
)
446 struct wined3d_surface cursor
;
449 swapchain
->device
->xScreenSpace
- swapchain
->device
->xHotSpot
,
450 swapchain
->device
->yScreenSpace
- swapchain
->device
->yHotSpot
,
451 swapchain
->device
->xScreenSpace
+ swapchain
->device
->cursorWidth
- swapchain
->device
->xHotSpot
,
452 swapchain
->device
->yScreenSpace
+ swapchain
->device
->cursorHeight
- swapchain
->device
->yHotSpot
,
454 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor
);
455 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
456 * the application because we are only supposed to copy the information out. Using a fake surface
457 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
459 memset(&cursor
, 0, sizeof(cursor
));
460 cursor
.resource
.ref
= 1;
461 cursor
.resource
.device
= swapchain
->device
;
462 cursor
.resource
.pool
= WINED3DPOOL_SCRATCH
;
463 cursor
.resource
.format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
464 cursor
.resource
.resourceType
= WINED3DRTYPE_SURFACE
;
465 cursor
.texture_name
= swapchain
->device
->cursorTexture
;
466 cursor
.texture_target
= GL_TEXTURE_2D
;
467 cursor
.texture_level
= 0;
468 cursor
.resource
.width
= swapchain
->device
->cursorWidth
;
469 cursor
.resource
.height
= swapchain
->device
->cursorHeight
;
470 /* The cursor must have pow2 sizes */
471 cursor
.pow2Width
= cursor
.resource
.width
;
472 cursor
.pow2Height
= cursor
.resource
.height
;
473 /* The surface is in the texture */
474 cursor
.flags
|= SFLAG_INTEXTURE
;
475 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
476 * which is exactly what we want :-)
478 if (swapchain
->presentParms
.Windowed
)
479 MapWindowPoints(NULL
, swapchain
->win_handle
, (LPPOINT
)&destRect
, 2);
480 wined3d_surface_blt(swapchain
->back_buffers
[0], &destRect
,
481 &cursor
, NULL
, WINEDDBLT_KEYSRC
, NULL
, WINED3DTEXF_POINT
);
484 if (swapchain
->device
->logo_surface
)
486 struct wined3d_surface
*src_surface
= swapchain
->device
->logo_surface
;
487 RECT rect
= {0, 0, src_surface
->resource
.width
, src_surface
->resource
.height
};
489 /* Blit the logo into the upper left corner of the drawable. */
490 wined3d_surface_blt(swapchain
->back_buffers
[0], &rect
, src_surface
, &rect
,
491 WINEDDBLT_KEYSRC
, NULL
, WINED3DTEXF_POINT
);
494 TRACE("Presenting HDC %p.\n", context
->hdc
);
496 render_to_fbo
= swapchain
->render_to_fbo
;
500 src_rect
= *src_rect_in
;
501 if (!render_to_fbo
&& (src_rect
.left
|| src_rect
.top
502 || src_rect
.right
!= swapchain
->presentParms
.BackBufferWidth
503 || src_rect
.bottom
!= swapchain
->presentParms
.BackBufferHeight
))
505 render_to_fbo
= TRUE
;
512 src_rect
.right
= swapchain
->presentParms
.BackBufferWidth
;
513 src_rect
.bottom
= swapchain
->presentParms
.BackBufferHeight
;
517 dst_rect
= *dst_rect_in
;
519 GetClientRect(swapchain
->win_handle
, &dst_rect
);
521 if (!render_to_fbo
&& (dst_rect
.left
|| dst_rect
.top
522 || dst_rect
.right
!= swapchain
->presentParms
.BackBufferWidth
523 || dst_rect
.bottom
!= swapchain
->presentParms
.BackBufferHeight
))
525 render_to_fbo
= TRUE
;
528 /* Rendering to a window of different size, presenting partial rectangles,
529 * or rendering to a different window needs help from FBO_blit or a textured
530 * draw. Render the swapchain to a FBO in the future.
532 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
533 * all these issues - this fails if the window is smaller than the backbuffer.
535 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
537 surface_load_location(swapchain
->back_buffers
[0], SFLAG_INTEXTURE
, NULL
);
538 surface_modify_location(swapchain
->back_buffers
[0], SFLAG_INDRAWABLE
, FALSE
);
539 swapchain
->render_to_fbo
= TRUE
;
540 swapchain_update_draw_bindings(swapchain
);
543 if (swapchain
->render_to_fbo
)
545 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
546 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
547 * not allowed(they need the COPY swapeffect)
549 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
551 if (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_FLIP
)
552 FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
554 swapchain_blit(swapchain
, context
, &src_rect
, &dst_rect
);
557 if (swapchain
->num_contexts
> 1)
559 SwapBuffers(context
->hdc
); /* TODO: cycle through the swapchain buffers */
561 TRACE("SwapBuffers called, Starting new frame\n");
565 DWORD time
= GetTickCount();
568 /* every 1.5 seconds */
569 if (time
- swapchain
->prev_time
> 1500)
571 TRACE_(fps
)("%p @ approx %.2ffps\n",
572 swapchain
, 1000.0 * swapchain
->frames
/ (time
- swapchain
->prev_time
));
573 swapchain
->prev_time
= time
;
574 swapchain
->frames
= 0;
578 /* This is disabled, but the code left in for debug purposes.
580 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
581 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
582 * The Debug runtime does the same on Windows. However, a few games do not redraw the
583 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
585 * Tests show that the content of the back buffer after a discard flip is indeed not
586 * reliable, so no game can depend on the exact content. However, it resembles the
587 * old contents in some way, for example by showing fragments at other locations. In
588 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
589 * gets a dark background image. If we clear it with a bright ugly color, the game's
590 * bug shows up much more than it does on Windows, and the players see single pixels
592 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
593 if (FALSE
&& swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
)
595 static const struct wined3d_color cyan
= {0.0f
, 1.0f
, 1.0f
, 1.0f
};
597 TRACE("Clearing the color buffer with cyan color\n");
599 wined3d_device_clear(swapchain
->device
, 0, NULL
,
600 WINED3DCLEAR_TARGET
, &cyan
, 1.0f
, 0);
603 if (!swapchain
->render_to_fbo
&& ((swapchain
->front_buffer
->flags
& SFLAG_INSYSMEM
)
604 || (swapchain
->back_buffers
[0]->flags
& SFLAG_INSYSMEM
)))
606 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
607 * Doesn't work with render_to_fbo because we're not flipping
609 struct wined3d_surface
*front
= swapchain
->front_buffer
;
610 struct wined3d_surface
*back
= swapchain
->back_buffers
[0];
612 if(front
->resource
.size
== back
->resource
.size
) {
614 flip_surface(front
, back
);
616 /* Tell the front buffer surface that is has been modified. However,
617 * the other locations were preserved during that, so keep the flags.
618 * This serves to update the emulated overlay, if any. */
619 fbflags
= front
->flags
;
620 surface_modify_location(front
, SFLAG_INDRAWABLE
, TRUE
);
621 front
->flags
= fbflags
;
625 surface_modify_location(front
, SFLAG_INDRAWABLE
, TRUE
);
626 surface_modify_location(back
, SFLAG_INDRAWABLE
, TRUE
);
631 surface_modify_location(swapchain
->front_buffer
, SFLAG_INDRAWABLE
, TRUE
);
632 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
633 * and INTEXTURE copies can keep their old content if they have any defined content.
634 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
635 * the texture / sysmem copy needs to be reloaded from the drawable
637 if (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_FLIP
)
638 surface_modify_location(swapchain
->back_buffers
[0], swapchain
->back_buffers
[0]->draw_binding
, TRUE
);
641 if (fb
->depth_stencil
)
643 if (swapchain
->presentParms
.Flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
644 || fb
->depth_stencil
->flags
& SFLAG_DISCARD
)
646 surface_modify_ds_location(fb
->depth_stencil
, SFLAG_DS_DISCARDED
,
647 fb
->depth_stencil
->resource
.width
,
648 fb
->depth_stencil
->resource
.height
);
649 if (fb
->depth_stencil
== swapchain
->device
->onscreen_depth_stencil
)
651 wined3d_surface_decref(swapchain
->device
->onscreen_depth_stencil
);
652 swapchain
->device
->onscreen_depth_stencil
= NULL
;
657 context_release(context
);
659 TRACE("returning\n");
663 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
665 swapchain_gl_present
,
668 /* Helper function that blits the front buffer contents to the target window. */
669 void x11_copy_to_screen(const struct wined3d_swapchain
*swapchain
, const RECT
*rect
)
671 const struct wined3d_surface
*front
;
672 POINT offset
= {0, 0};
677 TRACE("swapchain %p, rect %s.\n", swapchain
, wine_dbgstr_rect(rect
));
679 front
= swapchain
->front_buffer
;
680 if (!(front
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
))
683 if (front
->flags
& SFLAG_LOCKED
)
684 ERR("Trying to blit a mapped surface.\n");
686 TRACE("Copying surface %p to screen.\n", front
);
689 window
= swapchain
->win_handle
;
690 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
692 /* Front buffer coordinates are screen coordinates. Map them to the
693 * destination window if not fullscreened. */
694 if (swapchain
->presentParms
.Windowed
)
695 ClientToScreen(window
, &offset
);
697 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
700 draw_rect
.right
= front
->resource
.width
;
702 draw_rect
.bottom
= front
->resource
.height
;
705 IntersectRect(&draw_rect
, &draw_rect
, rect
);
707 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
708 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
709 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
710 ReleaseDC(window
, dst_dc
);
713 static HRESULT
swapchain_gdi_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect_in
,
714 const RECT
*dst_rect_in
, const RGNDATA
*dirty_region
, DWORD flags
)
716 struct wined3d_surface
*front
, *back
;
718 if (!swapchain
->back_buffers
)
720 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
721 return WINED3DERR_INVALIDCALL
;
723 front
= swapchain
->front_buffer
;
724 back
= swapchain
->back_buffers
[0];
730 front
->hDC
= back
->hDC
;
734 /* Flip the DIBsection. */
737 tmp
= front
->dib
.DIBsection
;
738 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
739 back
->dib
.DIBsection
= tmp
;
742 /* Flip the surface data. */
746 tmp
= front
->dib
.bitmap_data
;
747 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
748 back
->dib
.bitmap_data
= tmp
;
750 tmp
= front
->resource
.allocatedMemory
;
751 front
->resource
.allocatedMemory
= back
->resource
.allocatedMemory
;
752 back
->resource
.allocatedMemory
= tmp
;
754 if (front
->resource
.heapMemory
)
755 ERR("GDI Surface %p has heap memory allocated.\n", front
);
757 if (back
->resource
.heapMemory
)
758 ERR("GDI Surface %p has heap memory allocated.\n", back
);
764 static LONG prev_time
, frames
;
765 DWORD time
= GetTickCount();
769 /* every 1.5 seconds */
770 if (time
- prev_time
> 1500)
772 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
778 x11_copy_to_screen(swapchain
, NULL
);
783 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
785 swapchain_gdi_present
,
788 void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
792 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
795 if (!swapchain
->presentParms
.BackBufferCount
)
797 TRACE("Single buffered rendering.\n");
798 swapchain
->render_to_fbo
= FALSE
;
802 GetClientRect(swapchain
->win_handle
, &client_rect
);
804 TRACE("Backbuffer %ux%u, window %ux%u.\n",
805 swapchain
->presentParms
.BackBufferWidth
,
806 swapchain
->presentParms
.BackBufferHeight
,
807 client_rect
.right
, client_rect
.bottom
);
808 TRACE("Multisample type %#x, quality %#x.\n",
809 swapchain
->presentParms
.MultiSampleType
,
810 swapchain
->presentParms
.MultiSampleQuality
);
812 if (!wined3d_settings
.always_offscreen
&& !swapchain
->presentParms
.MultiSampleType
813 && swapchain
->presentParms
.BackBufferWidth
== client_rect
.right
814 && swapchain
->presentParms
.BackBufferHeight
== client_rect
.bottom
)
816 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
817 swapchain
->render_to_fbo
= FALSE
;
821 TRACE("Rendering to FBO.\n");
822 swapchain
->render_to_fbo
= TRUE
;
825 /* Do not call while under the GL lock. */
826 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, WINED3DSURFTYPE surface_type
,
827 struct wined3d_device
*device
, WINED3DPRESENT_PARAMETERS
*present_parameters
,
828 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
830 const struct wined3d_adapter
*adapter
= device
->adapter
;
831 const struct wined3d_format
*format
;
832 struct wined3d_display_mode mode
;
833 BOOL displaymode_set
= FALSE
;
839 if (present_parameters
->BackBufferCount
> WINED3DPRESENT_BACK_BUFFER_MAX
)
841 FIXME("The application requested %u back buffers, this is not supported.\n",
842 present_parameters
->BackBufferCount
);
843 return WINED3DERR_INVALIDCALL
;
846 if (present_parameters
->BackBufferCount
> 1)
848 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
849 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
852 switch (surface_type
)
855 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
859 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
863 ERR("Invalid surface type %#x.\n", surface_type
);
864 return WINED3DERR_INVALIDCALL
;
867 window
= present_parameters
->hDeviceWindow
? present_parameters
->hDeviceWindow
: device
->createParms
.hFocusWindow
;
869 swapchain
->device
= device
;
870 swapchain
->parent
= parent
;
871 swapchain
->parent_ops
= parent_ops
;
873 swapchain
->win_handle
= window
;
874 swapchain
->device_window
= window
;
876 wined3d_get_adapter_display_mode(device
->wined3d
, adapter
->ordinal
, &mode
);
877 swapchain
->orig_width
= mode
.width
;
878 swapchain
->orig_height
= mode
.height
;
879 swapchain
->orig_fmt
= mode
.format_id
;
880 format
= wined3d_get_format(&adapter
->gl_info
, mode
.format_id
);
882 GetClientRect(window
, &client_rect
);
883 if (present_parameters
->Windowed
884 && (!present_parameters
->BackBufferWidth
|| !present_parameters
->BackBufferHeight
885 || present_parameters
->BackBufferFormat
== WINED3DFMT_UNKNOWN
))
888 if (!present_parameters
->BackBufferWidth
)
890 present_parameters
->BackBufferWidth
= client_rect
.right
;
891 TRACE("Updating width to %u.\n", present_parameters
->BackBufferWidth
);
894 if (!present_parameters
->BackBufferHeight
)
896 present_parameters
->BackBufferHeight
= client_rect
.bottom
;
897 TRACE("Updating height to %u.\n", present_parameters
->BackBufferHeight
);
900 if (present_parameters
->BackBufferFormat
== WINED3DFMT_UNKNOWN
)
902 present_parameters
->BackBufferFormat
= swapchain
->orig_fmt
;
903 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->orig_fmt
));
906 swapchain
->presentParms
= *present_parameters
;
907 swapchain_update_render_to_fbo(swapchain
);
909 TRACE("Creating front buffer.\n");
910 hr
= device
->device_parent
->ops
->create_rendertarget(device
->device_parent
, parent
,
911 swapchain
->presentParms
.BackBufferWidth
, swapchain
->presentParms
.BackBufferHeight
,
912 swapchain
->presentParms
.BackBufferFormat
, swapchain
->presentParms
.MultiSampleType
,
913 swapchain
->presentParms
.MultiSampleQuality
, TRUE
/* Lockable */,
914 &swapchain
->front_buffer
);
917 WARN("Failed to create front buffer, hr %#x.\n", hr
);
921 surface_set_container(swapchain
->front_buffer
, WINED3D_CONTAINER_SWAPCHAIN
, swapchain
);
922 if (surface_type
== SURFACE_OPENGL
)
924 surface_modify_location(swapchain
->front_buffer
, SFLAG_INDRAWABLE
, TRUE
);
927 /* MSDN says we're only allowed a single fullscreen swapchain per device,
928 * so we should really check to see if there is a fullscreen swapchain
929 * already. Does a single head count as full screen? */
931 if (!present_parameters
->Windowed
)
933 struct wined3d_display_mode mode
;
935 /* Change the display settings */
936 mode
.width
= present_parameters
->BackBufferWidth
;
937 mode
.height
= present_parameters
->BackBufferHeight
;
938 mode
.format_id
= present_parameters
->BackBufferFormat
;
939 mode
.refresh_rate
= present_parameters
->FullScreen_RefreshRateInHz
;
941 hr
= wined3d_device_set_display_mode(device
, 0, &mode
);
944 WARN("Failed to set display mode, hr %#x.\n", hr
);
947 displaymode_set
= TRUE
;
950 if (surface_type
== SURFACE_OPENGL
)
952 static const enum wined3d_format_id formats
[] =
954 WINED3DFMT_D24_UNORM_S8_UINT
,
955 WINED3DFMT_D32_UNORM
,
956 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
957 WINED3DFMT_D16_UNORM
,
958 WINED3DFMT_S1_UINT_D15_UNORM
961 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
963 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
964 if (!swapchain
->context
)
966 ERR("Failed to create the context array.\n");
970 swapchain
->num_contexts
= 1;
972 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
973 * You are able to add a depth + stencil surface at a later stage when you need it.
974 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
975 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
976 * context, need torecreate shaders, textures and other resources.
978 * The context manager already takes care of the state problem and for the other tasks code from Reset
979 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
980 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
981 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
982 * issue needs to be fixed. */
983 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
985 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
986 swapchain
->context
[0] = context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
);
987 if (swapchain
->context
[0]) break;
988 TRACE("Depth stencil format %s is not supported, trying next format\n",
989 debug_d3dformat(formats
[i
]));
992 if (!swapchain
->context
[0])
994 WARN("Failed to create context.\n");
995 hr
= WINED3DERR_NOTAVAILABLE
;
999 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
1000 && (!present_parameters
->EnableAutoDepthStencil
1001 || swapchain
->presentParms
.AutoDepthStencilFormat
!= swapchain
->ds_format
->id
))
1003 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
1005 context_release(swapchain
->context
[0]);
1008 if (swapchain
->presentParms
.BackBufferCount
> 0)
1010 swapchain
->back_buffers
= HeapAlloc(GetProcessHeap(), 0,
1011 sizeof(*swapchain
->back_buffers
) * swapchain
->presentParms
.BackBufferCount
);
1012 if (!swapchain
->back_buffers
)
1014 ERR("Failed to allocate backbuffer array memory.\n");
1019 for (i
= 0; i
< swapchain
->presentParms
.BackBufferCount
; ++i
)
1021 TRACE("Creating back buffer %u.\n", i
);
1022 hr
= device
->device_parent
->ops
->create_rendertarget(device
->device_parent
, parent
,
1023 swapchain
->presentParms
.BackBufferWidth
, swapchain
->presentParms
.BackBufferHeight
,
1024 swapchain
->presentParms
.BackBufferFormat
, swapchain
->presentParms
.MultiSampleType
,
1025 swapchain
->presentParms
.MultiSampleQuality
, TRUE
/* Lockable */,
1026 &swapchain
->back_buffers
[i
]);
1029 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1033 surface_set_container(swapchain
->back_buffers
[i
], WINED3D_CONTAINER_SWAPCHAIN
, swapchain
);
1037 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1038 if (present_parameters
->EnableAutoDepthStencil
&& surface_type
== SURFACE_OPENGL
)
1040 TRACE("Creating depth/stencil buffer.\n");
1041 if (!device
->auto_depth_stencil
)
1043 hr
= device
->device_parent
->ops
->create_depth_stencil(device
->device_parent
,
1044 swapchain
->presentParms
.BackBufferWidth
, swapchain
->presentParms
.BackBufferHeight
,
1045 swapchain
->presentParms
.AutoDepthStencilFormat
, swapchain
->presentParms
.MultiSampleType
,
1046 swapchain
->presentParms
.MultiSampleQuality
, FALSE
/* FIXME: Discard */,
1047 &device
->auto_depth_stencil
);
1050 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr
);
1054 surface_set_container(device
->auto_depth_stencil
, WINED3D_CONTAINER_NONE
, NULL
);
1058 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1063 if (displaymode_set
)
1069 /* Change the display settings */
1070 memset(&devmode
, 0, sizeof(devmode
));
1071 devmode
.dmSize
= sizeof(devmode
);
1072 devmode
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
1073 devmode
.dmBitsPerPel
= format
->byte_count
* CHAR_BIT
;
1074 devmode
.dmPelsWidth
= swapchain
->orig_width
;
1075 devmode
.dmPelsHeight
= swapchain
->orig_height
;
1076 ChangeDisplaySettingsExW(adapter
->DeviceName
, &devmode
, NULL
, CDS_FULLSCREEN
, NULL
);
1079 if (swapchain
->back_buffers
)
1081 for (i
= 0; i
< swapchain
->presentParms
.BackBufferCount
; ++i
)
1083 if (swapchain
->back_buffers
[i
])
1085 surface_set_container(swapchain
->back_buffers
[i
], WINED3D_CONTAINER_NONE
, NULL
);
1086 wined3d_surface_decref(swapchain
->back_buffers
[i
]);
1089 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1092 if (swapchain
->context
)
1094 if (swapchain
->context
[0])
1096 context_release(swapchain
->context
[0]);
1097 context_destroy(device
, swapchain
->context
[0]);
1098 swapchain
->num_contexts
= 0;
1100 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1103 if (swapchain
->front_buffer
)
1105 surface_set_container(swapchain
->front_buffer
, WINED3D_CONTAINER_NONE
, NULL
);
1106 wined3d_surface_decref(swapchain
->front_buffer
);
1112 /* Do not call while under the GL lock. */
1113 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1114 WINED3DPRESENT_PARAMETERS
*present_parameters
, WINED3DSURFTYPE surface_type
,
1115 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1116 struct wined3d_swapchain
**swapchain
)
1118 struct wined3d_swapchain
*object
;
1121 TRACE("device %p, present_parameters %p, swapchain %p, parent %p, surface_type %#x.\n",
1122 device
, present_parameters
, swapchain
, parent
, surface_type
);
1124 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1127 ERR("Failed to allocate swapchain memory.\n");
1128 return E_OUTOFMEMORY
;
1131 hr
= swapchain_init(object
, surface_type
, device
, present_parameters
, parent
, parent_ops
);
1134 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1135 HeapFree(GetProcessHeap(), 0, object
);
1139 TRACE("Created swapchain %p.\n", object
);
1140 *swapchain
= object
;
1145 /* Do not call while under the GL lock. */
1146 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1148 struct wined3d_context
**newArray
;
1149 struct wined3d_context
*ctx
;
1151 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1153 if (!(ctx
= context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
)))
1155 ERR("Failed to create a new context for the swapchain\n");
1158 context_release(ctx
);
1160 newArray
= HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray
) * (swapchain
->num_contexts
+ 1));
1162 ERR("Out of memory when trying to allocate a new context array\n");
1163 context_destroy(swapchain
->device
, ctx
);
1166 memcpy(newArray
, swapchain
->context
, sizeof(*newArray
) * swapchain
->num_contexts
);
1167 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1168 newArray
[swapchain
->num_contexts
] = ctx
;
1169 swapchain
->context
= newArray
;
1170 swapchain
->num_contexts
++;
1172 TRACE("Returning context %p\n", ctx
);
1176 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1180 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1182 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1184 swapchain
->num_contexts
= 0;
1187 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1189 DWORD tid
= GetCurrentThreadId();
1192 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1194 if (swapchain
->context
[i
]->tid
== tid
)
1195 return swapchain
->context
[i
];
1198 /* Create a new context for the thread */
1199 return swapchain_create_context(swapchain
);
1202 void get_drawable_size_swapchain(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
1204 /* The drawable size of an onscreen drawable is the surface size.
1205 * (Actually: The window size, but the surface is created in window size) */
1206 *width
= context
->current_rt
->resource
.width
;
1207 *height
= context
->current_rt
->resource
.height
;
1210 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1212 if (!swapchain
->backup_dc
)
1214 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1216 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1217 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1219 ERR("Failed to create a window.\n");
1223 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1225 ERR("Failed to get a DC.\n");
1226 DestroyWindow(swapchain
->backup_wnd
);
1227 swapchain
->backup_wnd
= NULL
;
1232 return swapchain
->backup_dc
;
1235 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1239 surface_update_draw_binding(swapchain
->front_buffer
);
1241 for (i
= 0; i
< swapchain
->presentParms
.BackBufferCount
; ++i
)
1243 surface_update_draw_binding(swapchain
->back_buffers
[i
]);