wined3d: Pass a wined3d_resource_desc structure to device_parent_create_swapchain_sur...
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blobce54aeb8fd3e4144a2c787e4f88eab373313b8ae
1 /*
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
23 #include "config.h"
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;
34 HRESULT hr;
35 UINT i;
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 surface_set_swapchain(swapchain->front_buffer, NULL);
46 if (wined3d_surface_decref(swapchain->front_buffer))
47 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
48 swapchain->front_buffer = NULL;
51 if (swapchain->back_buffers)
53 i = swapchain->desc.backbuffer_count;
55 while (i--)
57 surface_set_swapchain(swapchain->back_buffers[i], NULL);
58 if (wined3d_surface_decref(swapchain->back_buffers[i]))
59 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
61 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
62 swapchain->back_buffers = NULL;
65 for (i = 0; i < swapchain->num_contexts; ++i)
67 context_destroy(swapchain->device, swapchain->context[i]);
69 HeapFree(GetProcessHeap(), 0, swapchain->context);
71 /* Restore the screen resolution if we rendered in fullscreen.
72 * This will restore the screen resolution to what it was before creating
73 * the swapchain. In case of d3d8 and d3d9 this will be the original
74 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
75 * sets the resolution before starting up Direct3D, thus orig_width and
76 * orig_height will be equal to the modes in the presentation params. */
77 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
79 mode.width = swapchain->orig_width;
80 mode.height = swapchain->orig_height;
81 mode.refresh_rate = 0;
82 mode.format_id = swapchain->orig_fmt;
83 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
84 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
85 swapchain->device->adapter->ordinal, &mode)))
86 ERR("Failed to restore display mode, hr %#x.\n", hr);
89 if (swapchain->backup_dc)
91 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
93 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
94 DestroyWindow(swapchain->backup_wnd);
98 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
100 ULONG refcount = InterlockedIncrement(&swapchain->ref);
102 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
104 return refcount;
107 /* Do not call while under the GL lock. */
108 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
110 ULONG refcount = InterlockedDecrement(&swapchain->ref);
112 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
114 if (!refcount)
116 swapchain_cleanup(swapchain);
117 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
118 HeapFree(GetProcessHeap(), 0, swapchain);
121 return refcount;
124 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
126 TRACE("swapchain %p.\n", swapchain);
128 return swapchain->parent;
131 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
133 if (!window)
134 window = swapchain->device_window;
135 if (window == swapchain->win_handle)
136 return;
138 TRACE("Setting swapchain %p window from %p to %p.\n",
139 swapchain, swapchain->win_handle, window);
140 swapchain->win_handle = window;
143 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
144 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
145 const RGNDATA *dirty_region, DWORD flags)
147 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
148 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
149 dst_window_override, dirty_region, flags);
151 if (flags)
152 FIXME("Ignoring flags %#x.\n", flags);
154 if (!swapchain->back_buffers)
156 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
157 return WINED3DERR_INVALIDCALL;
160 wined3d_swapchain_set_window(swapchain, dst_window_override);
162 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
164 return WINED3D_OK;
167 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
168 struct wined3d_surface *dst_surface)
170 struct wined3d_surface *src_surface;
171 RECT src_rect, dst_rect;
173 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
175 src_surface = swapchain->front_buffer;
176 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
177 dst_rect = src_rect;
179 if (swapchain->desc.windowed)
181 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
182 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
183 wine_dbgstr_rect(&dst_rect));
186 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
189 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
190 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
192 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
193 swapchain, back_buffer_idx, type);
195 /* Return invalid if there is no backbuffer array, otherwise it will
196 * crash when ddraw is used (there swapchain->back_buffers is always
197 * NULL). We need this because this function is called from
198 * stateblock_init_default_state() to get the default scissorrect
199 * dimensions. */
200 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
202 WARN("Invalid back buffer index.\n");
203 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
204 * here in wined3d to avoid problems in other libs. */
205 return NULL;
208 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
210 return swapchain->back_buffers[back_buffer_idx];
213 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
214 struct wined3d_raster_status *raster_status)
216 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
218 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
219 swapchain->device->adapter->ordinal, raster_status);
222 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
223 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
225 HRESULT hr;
227 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
229 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
230 swapchain->device->adapter->ordinal, mode, rotation);
232 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
233 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
235 return hr;
238 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
240 TRACE("swapchain %p.\n", swapchain);
242 return swapchain->device;
245 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
246 struct wined3d_swapchain_desc *desc)
248 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
250 *desc = swapchain->desc;
253 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
254 DWORD flags, const struct wined3d_gamma_ramp *ramp)
256 HDC dc;
258 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
260 if (flags)
261 FIXME("Ignoring flags %#x.\n", flags);
263 dc = GetDC(swapchain->device_window);
264 SetDeviceGammaRamp(dc, (void *)ramp);
265 ReleaseDC(swapchain->device_window, dc);
267 return WINED3D_OK;
270 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
271 struct wined3d_gamma_ramp *ramp)
273 HDC dc;
275 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
277 dc = GetDC(swapchain->device_window);
278 GetDeviceGammaRamp(dc, ramp);
279 ReleaseDC(swapchain->device_window, dc);
281 return WINED3D_OK;
284 /* A GL context is provided by the caller */
285 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
286 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
288 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
289 UINT src_w = src_rect->right - src_rect->left;
290 UINT src_h = src_rect->bottom - src_rect->top;
291 GLenum gl_filter;
292 const struct wined3d_gl_info *gl_info = context->gl_info;
293 RECT win_rect;
294 UINT win_h;
296 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
297 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
299 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
300 gl_filter = GL_NEAREST;
301 else
302 gl_filter = GL_LINEAR;
304 GetClientRect(swapchain->win_handle, &win_rect);
305 win_h = win_rect.bottom - win_rect.top;
307 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
309 DWORD location = SFLAG_INTEXTURE;
311 if (backbuffer->resource.multisample_type)
313 location = SFLAG_INRB_RESOLVED;
314 surface_load_location(backbuffer, location, NULL);
317 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
318 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
319 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
321 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
322 context_set_draw_buffer(context, GL_BACK);
323 context_invalidate_state(context, STATE_FRAMEBUFFER);
325 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
327 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
328 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
329 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
331 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
332 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
334 /* Note that the texture is upside down */
335 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
336 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
337 GL_COLOR_BUFFER_BIT, gl_filter);
338 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
340 else
342 struct wined3d_device *device = swapchain->device;
343 struct wined3d_context *context2;
344 float tex_left = src_rect->left;
345 float tex_top = src_rect->top;
346 float tex_right = src_rect->right;
347 float tex_bottom = src_rect->bottom;
349 context2 = context_acquire(device, swapchain->back_buffers[0]);
350 context_apply_blit_state(context2, device);
352 if (backbuffer->flags & SFLAG_NORMCOORD)
354 tex_left /= src_w;
355 tex_right /= src_w;
356 tex_top /= src_h;
357 tex_bottom /= src_h;
360 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
361 gl_filter = GL_NEAREST;
363 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
364 context_bind_texture(context2, backbuffer->texture_target, backbuffer->texture_name);
366 /* Set up the texture. The surface is not in a wined3d_texture
367 * container, so there are no D3D texture settings to dirtify. */
368 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
369 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
370 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
372 context_set_draw_buffer(context, GL_BACK);
374 /* Set the viewport to the destination rectandle, disable any projection
375 * transformation set up by context_apply_blit_state(), and draw a
376 * (-1,-1)-(1,1) quad.
378 * Back up viewport and matrix to avoid breaking last_was_blit
380 * Note that context_apply_blit_state() set up viewport and ortho to
381 * match the surface size - we want the GL drawable(=window) size. */
382 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
383 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
384 dst_rect->right, win_h - dst_rect->top);
385 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
386 gl_info->gl_ops.gl.p_glPushMatrix();
387 gl_info->gl_ops.gl.p_glLoadIdentity();
389 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
390 /* bottom left */
391 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
392 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
394 /* top left */
395 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
396 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
398 /* top right */
399 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
400 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
402 /* bottom right */
403 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
404 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
405 gl_info->gl_ops.gl.p_glEnd();
407 gl_info->gl_ops.gl.p_glPopMatrix();
408 gl_info->gl_ops.gl.p_glPopAttrib();
410 device->blitter->unset_shader(context->gl_info);
411 checkGLcall("Swapchain present blit(manual)\n");
413 context_release(context2);
417 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
418 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
420 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
421 const struct wined3d_fb_state *fb = &swapchain->device->fb;
422 const struct wined3d_gl_info *gl_info;
423 struct wined3d_context *context;
424 RECT src_rect, dst_rect;
425 BOOL render_to_fbo;
427 context = context_acquire(swapchain->device, back_buffer);
428 if (!context->valid)
430 context_release(context);
431 WARN("Invalid context, skipping present.\n");
432 return;
435 gl_info = context->gl_info;
437 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
438 if (swapchain->device->bCursorVisible &&
439 swapchain->device->cursorTexture &&
440 !swapchain->device->hardwareCursor)
442 struct wined3d_surface cursor;
443 RECT destRect =
445 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
446 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
447 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
448 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
450 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
451 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
452 * the application because we are only supposed to copy the information out. Using a fake surface
453 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
455 memset(&cursor, 0, sizeof(cursor));
456 cursor.resource.ref = 1;
457 cursor.resource.device = swapchain->device;
458 cursor.resource.pool = WINED3D_POOL_SCRATCH;
459 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
460 cursor.resource.type = WINED3D_RTYPE_SURFACE;
461 cursor.texture_name = swapchain->device->cursorTexture;
462 cursor.texture_target = GL_TEXTURE_2D;
463 cursor.texture_level = 0;
464 cursor.resource.width = swapchain->device->cursorWidth;
465 cursor.resource.height = swapchain->device->cursorHeight;
466 /* The cursor must have pow2 sizes */
467 cursor.pow2Width = cursor.resource.width;
468 cursor.pow2Height = cursor.resource.height;
469 /* The surface is in the texture */
470 cursor.flags |= SFLAG_INTEXTURE;
471 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
472 * which is exactly what we want :-)
474 if (swapchain->desc.windowed)
475 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
476 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
477 NULL, WINED3D_TEXF_POINT);
480 if (swapchain->device->logo_surface)
482 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
483 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
485 /* Blit the logo into the upper left corner of the drawable. */
486 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
487 NULL, WINED3D_TEXF_POINT);
490 TRACE("Presenting HDC %p.\n", context->hdc);
492 render_to_fbo = swapchain->render_to_fbo;
494 if (src_rect_in)
496 src_rect = *src_rect_in;
497 if (!render_to_fbo && (src_rect.left || src_rect.top
498 || src_rect.right != swapchain->desc.backbuffer_width
499 || src_rect.bottom != swapchain->desc.backbuffer_height))
501 render_to_fbo = TRUE;
504 else
506 src_rect.left = 0;
507 src_rect.top = 0;
508 src_rect.right = swapchain->desc.backbuffer_width;
509 src_rect.bottom = swapchain->desc.backbuffer_height;
512 if (dst_rect_in)
513 dst_rect = *dst_rect_in;
514 else
515 GetClientRect(swapchain->win_handle, &dst_rect);
517 if (!render_to_fbo && (dst_rect.left || dst_rect.top
518 || dst_rect.right != swapchain->desc.backbuffer_width
519 || dst_rect.bottom != swapchain->desc.backbuffer_height))
520 render_to_fbo = TRUE;
522 /* Rendering to a window of different size, presenting partial rectangles,
523 * or rendering to a different window needs help from FBO_blit or a textured
524 * draw. Render the swapchain to a FBO in the future.
526 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
527 * all these issues - this fails if the window is smaller than the backbuffer.
529 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
531 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
532 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, FALSE);
533 swapchain->render_to_fbo = TRUE;
534 swapchain_update_draw_bindings(swapchain);
536 else
538 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
541 if (swapchain->render_to_fbo)
543 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
544 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
545 * not allowed(they need the COPY swapeffect)
547 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
548 * the swap. */
549 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
550 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
552 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
555 if (swapchain->num_contexts > 1)
556 gl_info->gl_ops.gl.p_glFinish();
558 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
559 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
561 TRACE("SwapBuffers called, Starting new frame\n");
562 /* FPS support */
563 if (TRACE_ON(fps))
565 DWORD time = GetTickCount();
566 ++swapchain->frames;
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
591 * with wrong colors.
592 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
593 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_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 || (back_buffer->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;
611 if (front->resource.size == back_buffer->resource.size)
613 DWORD fbflags;
614 flip_surface(front, back_buffer);
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;
623 else
625 surface_modify_location(front, SFLAG_INDRAWABLE, TRUE);
626 surface_modify_location(back_buffer, SFLAG_INDRAWABLE, TRUE);
629 else
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->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
638 surface_modify_location(back_buffer, back_buffer->draw_binding, TRUE);
641 if (fb->depth_stencil)
643 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
644 || fb->depth_stencil->flags & SFLAG_DISCARD)
646 surface_modify_ds_location(fb->depth_stencil, SFLAG_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);
660 static const struct wined3d_swapchain_ops swapchain_gl_ops =
662 swapchain_gl_present,
665 /* Helper function that blits the front buffer contents to the target window. */
666 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
668 const struct wined3d_surface *front;
669 POINT offset = {0, 0};
670 HDC src_dc, dst_dc;
671 RECT draw_rect;
672 HWND window;
674 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
676 front = swapchain->front_buffer;
677 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
678 return;
680 if (front->resource.map_count)
681 ERR("Trying to blit a mapped surface.\n");
683 TRACE("Copying surface %p to screen.\n", front);
685 src_dc = front->hDC;
686 window = swapchain->win_handle;
687 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
689 /* Front buffer coordinates are screen coordinates. Map them to the
690 * destination window if not fullscreened. */
691 if (swapchain->desc.windowed)
692 ClientToScreen(window, &offset);
694 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
696 draw_rect.left = 0;
697 draw_rect.right = front->resource.width;
698 draw_rect.top = 0;
699 draw_rect.bottom = front->resource.height;
701 if (rect)
702 IntersectRect(&draw_rect, &draw_rect, rect);
704 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
705 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
706 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
707 ReleaseDC(window, dst_dc);
710 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
711 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
713 struct wined3d_surface *front, *back;
715 front = swapchain->front_buffer;
716 back = swapchain->back_buffers[0];
718 /* Flip the DC. */
720 HDC tmp;
721 tmp = front->hDC;
722 front->hDC = back->hDC;
723 back->hDC = tmp;
726 /* Flip the DIBsection. */
728 HBITMAP tmp;
729 tmp = front->dib.DIBsection;
730 front->dib.DIBsection = back->dib.DIBsection;
731 back->dib.DIBsection = tmp;
734 /* Flip the surface data. */
736 void *tmp;
738 tmp = front->dib.bitmap_data;
739 front->dib.bitmap_data = back->dib.bitmap_data;
740 back->dib.bitmap_data = tmp;
742 tmp = front->resource.allocatedMemory;
743 front->resource.allocatedMemory = back->resource.allocatedMemory;
744 back->resource.allocatedMemory = tmp;
746 if (front->resource.heapMemory)
747 ERR("GDI Surface %p has heap memory allocated.\n", front);
749 if (back->resource.heapMemory)
750 ERR("GDI Surface %p has heap memory allocated.\n", back);
753 /* FPS support */
754 if (TRACE_ON(fps))
756 static LONG prev_time, frames;
757 DWORD time = GetTickCount();
759 ++frames;
761 /* every 1.5 seconds */
762 if (time - prev_time > 1500)
764 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
765 prev_time = time;
766 frames = 0;
770 x11_copy_to_screen(swapchain, NULL);
773 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
775 swapchain_gdi_present,
778 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
780 RECT client_rect;
782 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
783 return;
785 if (!swapchain->desc.backbuffer_count)
787 TRACE("Single buffered rendering.\n");
788 swapchain->render_to_fbo = FALSE;
789 return;
792 GetClientRect(swapchain->win_handle, &client_rect);
794 TRACE("Backbuffer %ux%u, window %ux%u.\n",
795 swapchain->desc.backbuffer_width,
796 swapchain->desc.backbuffer_height,
797 client_rect.right, client_rect.bottom);
798 TRACE("Multisample type %#x, quality %#x.\n",
799 swapchain->desc.multisample_type,
800 swapchain->desc.multisample_quality);
802 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
803 && swapchain->desc.backbuffer_width == client_rect.right
804 && swapchain->desc.backbuffer_height == client_rect.bottom)
806 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
807 swapchain->render_to_fbo = FALSE;
808 return;
811 TRACE("Rendering to FBO.\n");
812 swapchain->render_to_fbo = TRUE;
815 /* Do not call while under the GL lock. */
816 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
817 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
819 const struct wined3d_adapter *adapter = device->adapter;
820 struct wined3d_resource_desc surface_desc;
821 const struct wined3d_format *format;
822 struct wined3d_display_mode mode;
823 BOOL displaymode_set = FALSE;
824 RECT client_rect;
825 HWND window;
826 HRESULT hr;
827 UINT i;
829 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
831 FIXME("The application requested %u back buffers, this is not supported.\n",
832 desc->backbuffer_count);
833 return WINED3DERR_INVALIDCALL;
836 if (desc->backbuffer_count > 1)
838 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
839 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
842 if (device->wined3d->flags & WINED3D_NO3D)
843 swapchain->swapchain_ops = &swapchain_gdi_ops;
844 else
845 swapchain->swapchain_ops = &swapchain_gl_ops;
847 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
849 swapchain->device = device;
850 swapchain->parent = parent;
851 swapchain->parent_ops = parent_ops;
852 swapchain->ref = 1;
853 swapchain->win_handle = window;
854 swapchain->device_window = window;
856 wined3d_get_adapter_display_mode(device->wined3d, adapter->ordinal, &mode, NULL);
857 swapchain->orig_width = mode.width;
858 swapchain->orig_height = mode.height;
859 swapchain->orig_fmt = mode.format_id;
860 format = wined3d_get_format(&adapter->gl_info, mode.format_id);
862 GetClientRect(window, &client_rect);
863 if (desc->windowed
864 && (!desc->backbuffer_width || !desc->backbuffer_height
865 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
868 if (!desc->backbuffer_width)
870 desc->backbuffer_width = client_rect.right;
871 TRACE("Updating width to %u.\n", desc->backbuffer_width);
874 if (!desc->backbuffer_height)
876 desc->backbuffer_height = client_rect.bottom;
877 TRACE("Updating height to %u.\n", desc->backbuffer_height);
880 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
882 desc->backbuffer_format = swapchain->orig_fmt;
883 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
886 swapchain->desc = *desc;
887 swapchain_update_render_to_fbo(swapchain);
889 TRACE("Creating front buffer.\n");
891 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
892 surface_desc.format = swapchain->desc.backbuffer_format;
893 surface_desc.multisample_type = swapchain->desc.multisample_type;
894 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
895 surface_desc.usage = WINED3DUSAGE_RENDERTARGET;
896 surface_desc.pool = WINED3D_POOL_DEFAULT;
897 surface_desc.width = swapchain->desc.backbuffer_width;
898 surface_desc.height = swapchain->desc.backbuffer_height;
899 surface_desc.depth = 1;
900 surface_desc.size = 0;
902 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
903 parent, &surface_desc, &swapchain->front_buffer)))
905 WARN("Failed to create front buffer, hr %#x.\n", hr);
906 goto err;
909 surface_set_swapchain(swapchain->front_buffer, swapchain);
910 if (!(device->wined3d->flags & WINED3D_NO3D))
911 surface_modify_location(swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
913 /* MSDN says we're only allowed a single fullscreen swapchain per device,
914 * so we should really check to see if there is a fullscreen swapchain
915 * already. Does a single head count as full screen? */
917 if (!desc->windowed)
919 struct wined3d_display_mode mode;
921 /* Change the display settings */
922 mode.width = desc->backbuffer_width;
923 mode.height = desc->backbuffer_height;
924 mode.format_id = desc->backbuffer_format;
925 mode.refresh_rate = desc->refresh_rate;
926 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
928 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode)))
930 WARN("Failed to set display mode, hr %#x.\n", hr);
931 goto err;
933 displaymode_set = TRUE;
936 if (!(device->wined3d->flags & WINED3D_NO3D))
938 static const enum wined3d_format_id formats[] =
940 WINED3DFMT_D24_UNORM_S8_UINT,
941 WINED3DFMT_D32_UNORM,
942 WINED3DFMT_R24_UNORM_X8_TYPELESS,
943 WINED3DFMT_D16_UNORM,
944 WINED3DFMT_S1_UINT_D15_UNORM
947 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
949 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
950 if (!swapchain->context)
952 ERR("Failed to create the context array.\n");
953 hr = E_OUTOFMEMORY;
954 goto err;
956 swapchain->num_contexts = 1;
958 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
959 * You are able to add a depth + stencil surface at a later stage when you need it.
960 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
961 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
962 * context, need torecreate shaders, textures and other resources.
964 * The context manager already takes care of the state problem and for the other tasks code from Reset
965 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
966 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
967 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
968 * issue needs to be fixed. */
969 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
971 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
972 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
973 if (swapchain->context[0]) break;
974 TRACE("Depth stencil format %s is not supported, trying next format\n",
975 debug_d3dformat(formats[i]));
978 if (!swapchain->context[0])
980 WARN("Failed to create context.\n");
981 hr = WINED3DERR_NOTAVAILABLE;
982 goto err;
985 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
986 && (!desc->enable_auto_depth_stencil
987 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
989 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
991 context_release(swapchain->context[0]);
994 if (swapchain->desc.backbuffer_count > 0)
996 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
997 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
998 if (!swapchain->back_buffers)
1000 ERR("Failed to allocate backbuffer array memory.\n");
1001 hr = E_OUTOFMEMORY;
1002 goto err;
1005 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1007 TRACE("Creating back buffer %u.\n", i);
1008 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1009 parent, &surface_desc, &swapchain->back_buffers[i])))
1011 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1012 goto err;
1014 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
1018 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1019 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1021 TRACE("Creating depth/stencil buffer.\n");
1022 if (!device->auto_depth_stencil)
1024 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
1025 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1027 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1028 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
1030 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1031 goto err;
1036 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1038 return WINED3D_OK;
1040 err:
1041 if (displaymode_set)
1043 DEVMODEW devmode;
1045 ClipCursor(NULL);
1047 /* Change the display settings */
1048 memset(&devmode, 0, sizeof(devmode));
1049 devmode.dmSize = sizeof(devmode);
1050 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1051 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1052 devmode.dmPelsWidth = swapchain->orig_width;
1053 devmode.dmPelsHeight = swapchain->orig_height;
1054 ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
1057 if (swapchain->back_buffers)
1059 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1061 if (swapchain->back_buffers[i])
1063 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1064 wined3d_surface_decref(swapchain->back_buffers[i]);
1067 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1070 if (swapchain->context)
1072 if (swapchain->context[0])
1074 context_release(swapchain->context[0]);
1075 context_destroy(device, swapchain->context[0]);
1076 swapchain->num_contexts = 0;
1078 HeapFree(GetProcessHeap(), 0, swapchain->context);
1081 if (swapchain->front_buffer)
1083 surface_set_swapchain(swapchain->front_buffer, NULL);
1084 wined3d_surface_decref(swapchain->front_buffer);
1087 return hr;
1090 /* Do not call while under the GL lock. */
1091 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1092 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1094 struct wined3d_swapchain *object;
1095 HRESULT hr;
1097 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1098 device, desc, parent, parent_ops, swapchain);
1100 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1101 if (!object)
1102 return E_OUTOFMEMORY;
1104 hr = swapchain_init(object, device, desc, parent, parent_ops);
1105 if (FAILED(hr))
1107 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1108 HeapFree(GetProcessHeap(), 0, object);
1109 return hr;
1112 TRACE("Created swapchain %p.\n", object);
1113 *swapchain = object;
1115 return WINED3D_OK;
1118 /* Do not call while under the GL lock. */
1119 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1121 struct wined3d_context **newArray;
1122 struct wined3d_context *ctx;
1124 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1126 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1128 ERR("Failed to create a new context for the swapchain\n");
1129 return NULL;
1131 context_release(ctx);
1133 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1134 if(!newArray) {
1135 ERR("Out of memory when trying to allocate a new context array\n");
1136 context_destroy(swapchain->device, ctx);
1137 return NULL;
1139 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1140 HeapFree(GetProcessHeap(), 0, swapchain->context);
1141 newArray[swapchain->num_contexts] = ctx;
1142 swapchain->context = newArray;
1143 swapchain->num_contexts++;
1145 TRACE("Returning context %p\n", ctx);
1146 return ctx;
1149 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1151 unsigned int i;
1153 for (i = 0; i < swapchain->num_contexts; ++i)
1155 context_destroy(swapchain->device, swapchain->context[i]);
1157 swapchain->num_contexts = 0;
1160 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1162 DWORD tid = GetCurrentThreadId();
1163 unsigned int i;
1165 for (i = 0; i < swapchain->num_contexts; ++i)
1167 if (swapchain->context[i]->tid == tid)
1168 return swapchain->context[i];
1171 /* Create a new context for the thread */
1172 return swapchain_create_context(swapchain);
1175 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1177 /* The drawable size of an onscreen drawable is the surface size.
1178 * (Actually: The window size, but the surface is created in window size) */
1179 *width = context->current_rt->resource.width;
1180 *height = context->current_rt->resource.height;
1183 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1185 if (!swapchain->backup_dc)
1187 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1189 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1190 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1192 ERR("Failed to create a window.\n");
1193 return NULL;
1196 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1198 ERR("Failed to get a DC.\n");
1199 DestroyWindow(swapchain->backup_wnd);
1200 swapchain->backup_wnd = NULL;
1201 return NULL;
1205 return swapchain->backup_dc;
1208 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1210 UINT i;
1212 surface_update_draw_binding(swapchain->front_buffer);
1214 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1216 surface_update_draw_binding(swapchain->back_buffers[i]);