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 "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
30 /* Do not call while under the GL lock. */
31 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
33 struct wined3d_display_mode mode;
37 TRACE("Destroying swapchain %p.\n", swapchain);
39 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
41 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
42 * is the last buffer to be destroyed, FindContext() depends on that. */
43 if (swapchain->front_buffer)
45 if (swapchain->front_buffer->container.type == WINED3D_CONTAINER_SWAPCHAIN)
46 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
47 if (wined3d_surface_decref(swapchain->front_buffer))
48 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
49 swapchain->front_buffer = NULL;
52 if (swapchain->back_buffers)
54 i = swapchain->desc.backbuffer_count;
58 if (swapchain->back_buffers[i]->container.type == WINED3D_CONTAINER_SWAPCHAIN)
59 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
60 if (wined3d_surface_decref(swapchain->back_buffers[i]))
61 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
63 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
64 swapchain->back_buffers = NULL;
67 for (i = 0; i < swapchain->num_contexts; ++i)
69 context_destroy(swapchain->device, swapchain->context[i]);
71 HeapFree(GetProcessHeap(), 0, swapchain->context);
73 /* Restore the screen resolution if we rendered in fullscreen.
74 * This will restore the screen resolution to what it was before creating
75 * the swapchain. In case of d3d8 and d3d9 this will be the original
76 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
77 * sets the resolution before starting up Direct3D, thus orig_width and
78 * orig_height will be equal to the modes in the presentation params. */
79 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
81 mode.width = swapchain->orig_width;
82 mode.height = swapchain->orig_height;
83 mode.refresh_rate = 0;
84 mode.format_id = swapchain->orig_fmt;
85 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
86 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
87 swapchain->device->adapter->ordinal, &mode)))
88 ERR("Failed to restore display mode, hr %#x.\n", hr);
91 if (swapchain->backup_dc)
93 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
95 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
96 DestroyWindow(swapchain->backup_wnd);
100 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedIncrement(&swapchain->ref);
104 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
109 /* Do not call while under the GL lock. */
110 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
112 ULONG refcount = InterlockedDecrement(&swapchain->ref);
114 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
118 swapchain_cleanup(swapchain);
119 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
120 HeapFree(GetProcessHeap(), 0, swapchain);
126 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
128 TRACE("swapchain %p.\n", swapchain);
130 return swapchain->parent;
133 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
136 window = swapchain->device_window;
137 if (window == swapchain->win_handle)
140 TRACE("Setting swapchain %p window from %p to %p.\n",
141 swapchain, swapchain->win_handle, window);
142 swapchain->win_handle = window;
145 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
146 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
147 const RGNDATA *dirty_region, DWORD flags)
149 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
150 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
151 dst_window_override, dirty_region, flags);
154 FIXME("Ignoring flags %#x.\n", flags);
156 if (!swapchain->back_buffers)
158 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
159 return WINED3DERR_INVALIDCALL;
162 wined3d_swapchain_set_window(swapchain, dst_window_override);
164 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
169 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
170 struct wined3d_surface *dst_surface)
172 struct wined3d_surface *src_surface;
173 RECT src_rect, dst_rect;
175 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
177 src_surface = swapchain->front_buffer;
178 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
181 if (swapchain->desc.windowed)
183 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
184 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
185 wine_dbgstr_rect(&dst_rect));
188 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
191 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
192 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
194 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
195 swapchain, back_buffer_idx, type);
197 /* Return invalid if there is no backbuffer array, otherwise it will
198 * crash when ddraw is used (there swapchain->back_buffers is always
199 * NULL). We need this because this function is called from
200 * stateblock_init_default_state() to get the default scissorrect
202 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
204 WARN("Invalid back buffer index.\n");
205 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
206 * here in wined3d to avoid problems in other libs. */
210 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
212 return swapchain->back_buffers[back_buffer_idx];
215 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
216 struct wined3d_raster_status *raster_status)
218 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
220 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
221 swapchain->device->adapter->ordinal, raster_status);
224 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
225 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
229 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
231 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
232 swapchain->device->adapter->ordinal, mode, rotation);
234 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
235 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
240 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
242 TRACE("swapchain %p.\n", swapchain);
244 return swapchain->device;
247 HRESULT CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
248 struct wined3d_swapchain_desc *desc)
250 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
252 *desc = swapchain->desc;
257 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
258 DWORD flags, const struct wined3d_gamma_ramp *ramp)
262 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
265 FIXME("Ignoring flags %#x.\n", flags);
267 dc = GetDC(swapchain->device_window);
268 SetDeviceGammaRamp(dc, (void *)ramp);
269 ReleaseDC(swapchain->device_window, dc);
274 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
275 struct wined3d_gamma_ramp *ramp)
279 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
281 dc = GetDC(swapchain->device_window);
282 GetDeviceGammaRamp(dc, ramp);
283 ReleaseDC(swapchain->device_window, dc);
288 /* A GL context is provided by the caller */
289 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
290 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
292 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
293 UINT src_w = src_rect->right - src_rect->left;
294 UINT src_h = src_rect->bottom - src_rect->top;
296 const struct wined3d_gl_info *gl_info = context->gl_info;
300 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
301 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
303 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
304 gl_filter = GL_NEAREST;
306 gl_filter = GL_LINEAR;
308 GetClientRect(swapchain->win_handle, &win_rect);
309 win_h = win_rect.bottom - win_rect.top;
311 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
313 DWORD location = SFLAG_INTEXTURE;
315 if (backbuffer->resource.multisample_type)
317 location = SFLAG_INRB_RESOLVED;
318 surface_load_location(backbuffer, location, NULL);
322 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
323 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
324 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
326 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
327 context_set_draw_buffer(context, GL_BACK);
328 context_invalidate_state(context, STATE_FRAMEBUFFER);
330 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
331 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
332 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
333 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
334 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
336 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
337 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
339 /* Note that the texture is upside down */
340 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
341 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
342 GL_COLOR_BUFFER_BIT, gl_filter);
343 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
348 struct wined3d_device *device = swapchain->device;
349 struct wined3d_context *context2;
350 float tex_left = src_rect->left;
351 float tex_top = src_rect->top;
352 float tex_right = src_rect->right;
353 float tex_bottom = src_rect->bottom;
355 context2 = context_acquire(device, swapchain->back_buffers[0]);
356 context_apply_blit_state(context2, device);
358 if (backbuffer->flags & SFLAG_NORMCOORD)
366 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
367 gl_filter = GL_NEAREST;
370 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
372 /* Set up the texture. The surface is not in a wined3d_texture
373 * container, so there are no D3D texture settings to dirtify. */
374 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
375 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
376 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
378 context_set_draw_buffer(context, GL_BACK);
380 /* Set the viewport to the destination rectandle, disable any projection
381 * transformation set up by context_apply_blit_state(), and draw a
382 * (-1,-1)-(1,1) quad.
384 * Back up viewport and matrix to avoid breaking last_was_blit
386 * Note that context_apply_blit_state() set up viewport and ortho to
387 * match the surface size - we want the GL drawable(=window) size. */
388 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
389 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
390 dst_rect->right, win_h - dst_rect->top);
391 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
392 gl_info->gl_ops.gl.p_glPushMatrix();
393 gl_info->gl_ops.gl.p_glLoadIdentity();
395 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
397 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
398 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
401 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
402 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
405 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
406 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
409 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
410 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
411 gl_info->gl_ops.gl.p_glEnd();
413 gl_info->gl_ops.gl.p_glPopMatrix();
414 gl_info->gl_ops.gl.p_glPopAttrib();
416 device->blitter->unset_shader(context->gl_info);
417 checkGLcall("Swapchain present blit(manual)\n");
420 context_release(context2);
424 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
425 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
427 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
428 const struct wined3d_fb_state *fb = &swapchain->device->fb;
429 const struct wined3d_gl_info *gl_info;
430 struct wined3d_context *context;
431 RECT src_rect, dst_rect;
434 context = context_acquire(swapchain->device, back_buffer);
437 context_release(context);
438 WARN("Invalid context, skipping present.\n");
442 gl_info = context->gl_info;
444 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
445 if (swapchain->device->bCursorVisible &&
446 swapchain->device->cursorTexture &&
447 !swapchain->device->hardwareCursor)
449 struct wined3d_surface cursor;
452 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
453 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
454 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
455 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
457 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
458 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
459 * the application because we are only supposed to copy the information out. Using a fake surface
460 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
462 memset(&cursor, 0, sizeof(cursor));
463 cursor.resource.ref = 1;
464 cursor.resource.device = swapchain->device;
465 cursor.resource.pool = WINED3D_POOL_SCRATCH;
466 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
467 cursor.resource.type = WINED3D_RTYPE_SURFACE;
468 cursor.texture_name = swapchain->device->cursorTexture;
469 cursor.texture_target = GL_TEXTURE_2D;
470 cursor.texture_level = 0;
471 cursor.resource.width = swapchain->device->cursorWidth;
472 cursor.resource.height = swapchain->device->cursorHeight;
473 /* The cursor must have pow2 sizes */
474 cursor.pow2Width = cursor.resource.width;
475 cursor.pow2Height = cursor.resource.height;
476 /* The surface is in the texture */
477 cursor.flags |= SFLAG_INTEXTURE;
478 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
479 * which is exactly what we want :-)
481 if (swapchain->desc.windowed)
482 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
483 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
484 NULL, WINED3D_TEXF_POINT);
487 if (swapchain->device->logo_surface)
489 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
490 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
492 /* Blit the logo into the upper left corner of the drawable. */
493 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
494 NULL, WINED3D_TEXF_POINT);
497 TRACE("Presenting HDC %p.\n", context->hdc);
499 render_to_fbo = swapchain->render_to_fbo;
503 src_rect = *src_rect_in;
504 if (!render_to_fbo && (src_rect.left || src_rect.top
505 || src_rect.right != swapchain->desc.backbuffer_width
506 || src_rect.bottom != swapchain->desc.backbuffer_height))
508 render_to_fbo = TRUE;
515 src_rect.right = swapchain->desc.backbuffer_width;
516 src_rect.bottom = swapchain->desc.backbuffer_height;
520 dst_rect = *dst_rect_in;
522 GetClientRect(swapchain->win_handle, &dst_rect);
524 if (!render_to_fbo && (dst_rect.left || dst_rect.top
525 || dst_rect.right != swapchain->desc.backbuffer_width
526 || dst_rect.bottom != swapchain->desc.backbuffer_height))
527 render_to_fbo = TRUE;
529 /* Rendering to a window of different size, presenting partial rectangles,
530 * or rendering to a different window needs help from FBO_blit or a textured
531 * draw. Render the swapchain to a FBO in the future.
533 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
534 * all these issues - this fails if the window is smaller than the backbuffer.
536 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
538 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
539 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
540 swapchain->render_to_fbo = TRUE;
541 swapchain_update_draw_bindings(swapchain);
545 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
548 if (swapchain->render_to_fbo)
550 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
551 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
552 * not allowed(they need the COPY swapeffect)
554 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
556 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
557 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
559 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
562 if (swapchain->num_contexts > 1)
563 gl_info->gl_ops.gl.p_glFinish();
564 SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
566 TRACE("SwapBuffers called, Starting new frame\n");
570 DWORD time = GetTickCount();
573 /* every 1.5 seconds */
574 if (time - swapchain->prev_time > 1500)
576 TRACE_(fps)("%p @ approx %.2ffps\n",
577 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
578 swapchain->prev_time = time;
579 swapchain->frames = 0;
583 /* This is disabled, but the code left in for debug purposes.
585 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
586 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
587 * The Debug runtime does the same on Windows. However, a few games do not redraw the
588 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
590 * Tests show that the content of the back buffer after a discard flip is indeed not
591 * reliable, so no game can depend on the exact content. However, it resembles the
592 * old contents in some way, for example by showing fragments at other locations. In
593 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
594 * gets a dark background image. If we clear it with a bright ugly color, the game's
595 * bug shows up much more than it does on Windows, and the players see single pixels
597 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
598 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
600 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
602 TRACE("Clearing the color buffer with cyan color\n");
604 wined3d_device_clear(swapchain->device, 0, NULL,
605 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
608 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
609 || (back_buffer->flags & SFLAG_INSYSMEM)))
611 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
612 * Doesn't work with render_to_fbo because we're not flipping
614 struct wined3d_surface *front = swapchain->front_buffer;
616 if (front->resource.size == back_buffer->resource.size)
619 flip_surface(front, back_buffer);
621 /* Tell the front buffer surface that is has been modified. However,
622 * the other locations were preserved during that, so keep the flags.
623 * This serves to update the emulated overlay, if any. */
624 fbflags = front->flags;
625 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
626 front->flags = fbflags;
630 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
631 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
636 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
637 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
638 * and INTEXTURE copies can keep their old content if they have any defined content.
639 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
640 * the texture / sysmem copy needs to be reloaded from the drawable
642 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
643 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
646 if (fb->depth_stencil)
648 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
649 || fb->depth_stencil->flags & SFLAG_DISCARD)
651 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
652 fb->depth_stencil->resource.width,
653 fb->depth_stencil->resource.height);
654 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
656 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
657 swapchain->device->onscreen_depth_stencil = NULL;
662 context_release(context);
665 static const struct wined3d_swapchain_ops swapchain_gl_ops =
667 swapchain_gl_present,
670 /* Helper function that blits the front buffer contents to the target window. */
671 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
673 const struct wined3d_surface *front;
674 POINT offset = {0, 0};
679 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
681 front = swapchain->front_buffer;
682 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
685 if (front->resource.map_count)
686 ERR("Trying to blit a mapped surface.\n");
688 TRACE("Copying surface %p to screen.\n", front);
691 window = swapchain->win_handle;
692 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
694 /* Front buffer coordinates are screen coordinates. Map them to the
695 * destination window if not fullscreened. */
696 if (swapchain->desc.windowed)
697 ClientToScreen(window, &offset);
699 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
702 draw_rect.right = front->resource.width;
704 draw_rect.bottom = front->resource.height;
707 IntersectRect(&draw_rect, &draw_rect, rect);
709 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
710 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
711 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
712 ReleaseDC(window, dst_dc);
715 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
716 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
718 struct wined3d_surface *front, *back;
720 front = swapchain->front_buffer;
721 back = swapchain->back_buffers[0];
727 front->hDC = back->hDC;
731 /* Flip the DIBsection. */
734 tmp = front->dib.DIBsection;
735 front->dib.DIBsection = back->dib.DIBsection;
736 back->dib.DIBsection = tmp;
739 /* Flip the surface data. */
743 tmp = front->dib.bitmap_data;
744 front->dib.bitmap_data = back->dib.bitmap_data;
745 back->dib.bitmap_data = tmp;
747 tmp = front->resource.allocatedMemory;
748 front->resource.allocatedMemory = back->resource.allocatedMemory;
749 back->resource.allocatedMemory = tmp;
751 if (front->resource.heapMemory)
752 ERR("GDI Surface %p has heap memory allocated.\n", front);
754 if (back->resource.heapMemory)
755 ERR("GDI Surface %p has heap memory allocated.\n", back);
761 static LONG prev_time, frames;
762 DWORD time = GetTickCount();
766 /* every 1.5 seconds */
767 if (time - prev_time > 1500)
769 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
775 x11_copy_to_screen(swapchain, NULL);
778 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
780 swapchain_gdi_present,
783 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
787 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
790 if (!swapchain->desc.backbuffer_count)
792 TRACE("Single buffered rendering.\n");
793 swapchain->render_to_fbo = FALSE;
797 GetClientRect(swapchain->win_handle, &client_rect);
799 TRACE("Backbuffer %ux%u, window %ux%u.\n",
800 swapchain->desc.backbuffer_width,
801 swapchain->desc.backbuffer_height,
802 client_rect.right, client_rect.bottom);
803 TRACE("Multisample type %#x, quality %#x.\n",
804 swapchain->desc.multisample_type,
805 swapchain->desc.multisample_quality);
807 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
808 && swapchain->desc.backbuffer_width == client_rect.right
809 && swapchain->desc.backbuffer_height == client_rect.bottom)
811 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
812 swapchain->render_to_fbo = FALSE;
816 TRACE("Rendering to FBO.\n");
817 swapchain->render_to_fbo = TRUE;
820 /* Do not call while under the GL lock. */
821 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, enum wined3d_surface_type surface_type,
822 struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
823 void *parent, const struct wined3d_parent_ops *parent_ops)
825 const struct wined3d_adapter *adapter = device->adapter;
826 const struct wined3d_format *format;
827 struct wined3d_display_mode mode;
828 BOOL displaymode_set = FALSE;
834 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
836 FIXME("The application requested %u back buffers, this is not supported.\n",
837 desc->backbuffer_count);
838 return WINED3DERR_INVALIDCALL;
841 if (desc->backbuffer_count > 1)
843 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
844 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
847 switch (surface_type)
849 case WINED3D_SURFACE_TYPE_GDI:
850 swapchain->swapchain_ops = &swapchain_gdi_ops;
853 case WINED3D_SURFACE_TYPE_OPENGL:
854 swapchain->swapchain_ops = &swapchain_gl_ops;
858 ERR("Invalid surface type %#x.\n", surface_type);
859 return WINED3DERR_INVALIDCALL;
862 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
864 swapchain->device = device;
865 swapchain->parent = parent;
866 swapchain->parent_ops = parent_ops;
868 swapchain->win_handle = window;
869 swapchain->device_window = window;
871 wined3d_get_adapter_display_mode(device->wined3d, adapter->ordinal, &mode, NULL);
872 swapchain->orig_width = mode.width;
873 swapchain->orig_height = mode.height;
874 swapchain->orig_fmt = mode.format_id;
875 format = wined3d_get_format(&adapter->gl_info, mode.format_id);
877 GetClientRect(window, &client_rect);
879 && (!desc->backbuffer_width || !desc->backbuffer_height
880 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
883 if (!desc->backbuffer_width)
885 desc->backbuffer_width = client_rect.right;
886 TRACE("Updating width to %u.\n", desc->backbuffer_width);
889 if (!desc->backbuffer_height)
891 desc->backbuffer_height = client_rect.bottom;
892 TRACE("Updating height to %u.\n", desc->backbuffer_height);
895 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
897 desc->backbuffer_format = swapchain->orig_fmt;
898 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
901 swapchain->desc = *desc;
902 swapchain_update_render_to_fbo(swapchain);
904 TRACE("Creating front buffer.\n");
905 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent, parent,
906 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
907 swapchain->desc.backbuffer_format, WINED3DUSAGE_RENDERTARGET,
908 swapchain->desc.multisample_type, swapchain->desc.multisample_quality,
909 &swapchain->front_buffer)))
911 WARN("Failed to create front buffer, hr %#x.\n", hr);
915 if (swapchain->front_buffer->container.type == WINED3D_CONTAINER_NONE)
916 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_SWAPCHAIN, swapchain);
917 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
918 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
920 /* MSDN says we're only allowed a single fullscreen swapchain per device,
921 * so we should really check to see if there is a fullscreen swapchain
922 * already. Does a single head count as full screen? */
926 struct wined3d_display_mode mode;
928 /* Change the display settings */
929 mode.width = desc->backbuffer_width;
930 mode.height = desc->backbuffer_height;
931 mode.format_id = desc->backbuffer_format;
932 mode.refresh_rate = desc->refresh_rate;
933 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
935 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode)))
937 WARN("Failed to set display mode, hr %#x.\n", hr);
940 displaymode_set = TRUE;
943 if (surface_type == WINED3D_SURFACE_TYPE_OPENGL)
945 static const enum wined3d_format_id formats[] =
947 WINED3DFMT_D24_UNORM_S8_UINT,
948 WINED3DFMT_D32_UNORM,
949 WINED3DFMT_R24_UNORM_X8_TYPELESS,
950 WINED3DFMT_D16_UNORM,
951 WINED3DFMT_S1_UINT_D15_UNORM
954 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
956 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
957 if (!swapchain->context)
959 ERR("Failed to create the context array.\n");
963 swapchain->num_contexts = 1;
965 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
966 * You are able to add a depth + stencil surface at a later stage when you need it.
967 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
968 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
969 * context, need torecreate shaders, textures and other resources.
971 * The context manager already takes care of the state problem and for the other tasks code from Reset
972 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
973 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
974 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
975 * issue needs to be fixed. */
976 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
978 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
979 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
980 if (swapchain->context[0]) break;
981 TRACE("Depth stencil format %s is not supported, trying next format\n",
982 debug_d3dformat(formats[i]));
985 if (!swapchain->context[0])
987 WARN("Failed to create context.\n");
988 hr = WINED3DERR_NOTAVAILABLE;
992 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
993 && (!desc->enable_auto_depth_stencil
994 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
996 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
998 context_release(swapchain->context[0]);
1001 if (swapchain->desc.backbuffer_count > 0)
1003 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1004 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1005 if (!swapchain->back_buffers)
1007 ERR("Failed to allocate backbuffer array memory.\n");
1012 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1014 TRACE("Creating back buffer %u.\n", i);
1015 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent, parent,
1016 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1017 swapchain->desc.backbuffer_format, WINED3DUSAGE_RENDERTARGET,
1018 swapchain->desc.multisample_type, swapchain->desc.multisample_quality,
1019 &swapchain->back_buffers[i])))
1021 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1024 if (swapchain->back_buffers[i]->container.type == WINED3D_CONTAINER_NONE)
1025 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_SWAPCHAIN, swapchain);
1029 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1030 if (desc->enable_auto_depth_stencil && surface_type == WINED3D_SURFACE_TYPE_OPENGL)
1032 TRACE("Creating depth/stencil buffer.\n");
1033 if (!device->auto_depth_stencil)
1035 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1036 device->device_parent, swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1037 swapchain->desc.auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
1038 swapchain->desc.multisample_type, swapchain->desc.multisample_quality,
1039 &device->auto_depth_stencil)))
1041 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1045 surface_set_container(device->auto_depth_stencil, WINED3D_CONTAINER_NONE, NULL);
1049 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1054 if (displaymode_set)
1060 /* Change the display settings */
1061 memset(&devmode, 0, sizeof(devmode));
1062 devmode.dmSize = sizeof(devmode);
1063 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1064 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1065 devmode.dmPelsWidth = swapchain->orig_width;
1066 devmode.dmPelsHeight = swapchain->orig_height;
1067 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
1070 if (swapchain->back_buffers)
1072 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1074 if (swapchain->back_buffers[i])
1076 surface_set_container(swapchain->back_buffers[i], WINED3D_CONTAINER_NONE, NULL);
1077 wined3d_surface_decref(swapchain->back_buffers[i]);
1080 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1083 if (swapchain->context)
1085 if (swapchain->context[0])
1087 context_release(swapchain->context[0]);
1088 context_destroy(device, swapchain->context[0]);
1089 swapchain->num_contexts = 0;
1091 HeapFree(GetProcessHeap(), 0, swapchain->context);
1094 if (swapchain->front_buffer)
1096 surface_set_container(swapchain->front_buffer, WINED3D_CONTAINER_NONE, NULL);
1097 wined3d_surface_decref(swapchain->front_buffer);
1103 /* Do not call while under the GL lock. */
1104 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device,
1105 struct wined3d_swapchain_desc *desc, enum wined3d_surface_type surface_type,
1106 void *parent, const struct wined3d_parent_ops *parent_ops,
1107 struct wined3d_swapchain **swapchain)
1109 struct wined3d_swapchain *object;
1112 TRACE("device %p, desc %p, swapchain %p, parent %p, surface_type %#x.\n",
1113 device, desc, swapchain, parent, surface_type);
1115 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1118 ERR("Failed to allocate swapchain memory.\n");
1119 return E_OUTOFMEMORY;
1122 hr = swapchain_init(object, surface_type, device, desc, parent, parent_ops);
1125 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1126 HeapFree(GetProcessHeap(), 0, object);
1130 TRACE("Created swapchain %p.\n", object);
1131 *swapchain = object;
1136 /* Do not call while under the GL lock. */
1137 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1139 struct wined3d_context **newArray;
1140 struct wined3d_context *ctx;
1142 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1144 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1146 ERR("Failed to create a new context for the swapchain\n");
1149 context_release(ctx);
1151 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1153 ERR("Out of memory when trying to allocate a new context array\n");
1154 context_destroy(swapchain->device, ctx);
1157 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1158 HeapFree(GetProcessHeap(), 0, swapchain->context);
1159 newArray[swapchain->num_contexts] = ctx;
1160 swapchain->context = newArray;
1161 swapchain->num_contexts++;
1163 TRACE("Returning context %p\n", ctx);
1167 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1171 for (i = 0; i < swapchain->num_contexts; ++i)
1173 context_destroy(swapchain->device, swapchain->context[i]);
1175 swapchain->num_contexts = 0;
1178 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1180 DWORD tid = GetCurrentThreadId();
1183 for (i = 0; i < swapchain->num_contexts; ++i)
1185 if (swapchain->context[i]->tid == tid)
1186 return swapchain->context[i];
1189 /* Create a new context for the thread */
1190 return swapchain_create_context(swapchain);
1193 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1195 /* The drawable size of an onscreen drawable is the surface size.
1196 * (Actually: The window size, but the surface is created in window size) */
1197 *width = context->current_rt->resource.width;
1198 *height = context->current_rt->resource.height;
1201 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1203 if (!swapchain->backup_dc)
1205 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1207 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1208 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1210 ERR("Failed to create a window.\n");
1214 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1216 ERR("Failed to get a DC.\n");
1217 DestroyWindow(swapchain->backup_wnd);
1218 swapchain->backup_wnd = NULL;
1223 return swapchain->backup_dc;
1226 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1230 surface_update_draw_binding(swapchain->front_buffer);
1232 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1234 surface_update_draw_binding(swapchain->back_buffers[i]);