winex11: Added missing release_win_data() to create_foreign_window().
[wine.git] / dlls / wined3d / swapchain.c
blob62827c92cf0ec99da716a5b0229180bdb494f255
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 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 HRESULT hr;
33 UINT i;
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_swapchain(swapchain->front_buffer, 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->desc.backbuffer_count;
53 while (i--)
55 surface_set_swapchain(swapchain->back_buffers[i], 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->desc.windowed && swapchain->desc.auto_restore_display_mode)
77 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
78 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
79 ERR("Failed to restore display mode, hr %#x.\n", hr);
82 if (swapchain->backup_dc)
84 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
86 ReleaseDC(swapchain->backup_wnd, swapchain->backup_dc);
87 DestroyWindow(swapchain->backup_wnd);
91 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
93 ULONG refcount = InterlockedIncrement(&swapchain->ref);
95 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
97 return refcount;
100 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedDecrement(&swapchain->ref);
104 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
106 if (!refcount)
108 swapchain_cleanup(swapchain);
109 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
110 HeapFree(GetProcessHeap(), 0, swapchain);
113 return refcount;
116 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
118 TRACE("swapchain %p.\n", swapchain);
120 return swapchain->parent;
123 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
125 if (!window)
126 window = swapchain->device_window;
127 if (window == swapchain->win_handle)
128 return;
130 TRACE("Setting swapchain %p window from %p to %p.\n",
131 swapchain, swapchain->win_handle, window);
132 swapchain->win_handle = window;
135 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
136 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
137 const RGNDATA *dirty_region, DWORD flags)
139 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
140 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
141 dst_window_override, dirty_region, flags);
143 if (flags)
144 FIXME("Ignoring flags %#x.\n", flags);
146 if (!swapchain->back_buffers)
148 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
149 return WINED3DERR_INVALIDCALL;
152 wined3d_swapchain_set_window(swapchain, dst_window_override);
154 swapchain->swapchain_ops->swapchain_present(swapchain, src_rect, dst_rect, dirty_region, flags);
156 return WINED3D_OK;
159 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
160 struct wined3d_surface *dst_surface)
162 struct wined3d_surface *src_surface;
163 RECT src_rect, dst_rect;
165 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
167 src_surface = swapchain->front_buffer;
168 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
169 dst_rect = src_rect;
171 if (swapchain->desc.windowed)
173 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
174 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
175 wine_dbgstr_rect(&dst_rect));
178 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
181 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
182 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
184 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
185 swapchain, back_buffer_idx, type);
187 /* Return invalid if there is no backbuffer array, otherwise it will
188 * crash when ddraw is used (there swapchain->back_buffers is always
189 * NULL). We need this because this function is called from
190 * stateblock_init_default_state() to get the default scissorrect
191 * dimensions. */
192 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
194 WARN("Invalid back buffer index.\n");
195 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
196 * here in wined3d to avoid problems in other libs. */
197 return NULL;
200 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
202 return swapchain->back_buffers[back_buffer_idx];
205 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
206 struct wined3d_raster_status *raster_status)
208 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
210 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
211 swapchain->device->adapter->ordinal, raster_status);
214 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
215 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
217 HRESULT hr;
219 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
221 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
222 swapchain->device->adapter->ordinal, mode, rotation);
224 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
225 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
227 return hr;
230 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
232 TRACE("swapchain %p.\n", swapchain);
234 return swapchain->device;
237 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
238 struct wined3d_swapchain_desc *desc)
240 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
242 *desc = swapchain->desc;
245 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
246 DWORD flags, const struct wined3d_gamma_ramp *ramp)
248 HDC dc;
250 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
252 if (flags)
253 FIXME("Ignoring flags %#x.\n", flags);
255 dc = GetDC(swapchain->device_window);
256 SetDeviceGammaRamp(dc, (void *)ramp);
257 ReleaseDC(swapchain->device_window, dc);
259 return WINED3D_OK;
262 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
263 struct wined3d_gamma_ramp *ramp)
265 HDC dc;
267 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
269 dc = GetDC(swapchain->device_window);
270 GetDeviceGammaRamp(dc, ramp);
271 ReleaseDC(swapchain->device_window, dc);
273 return WINED3D_OK;
276 /* A GL context is provided by the caller */
277 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
278 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
280 struct wined3d_surface *backbuffer = swapchain->back_buffers[0];
281 UINT src_w = src_rect->right - src_rect->left;
282 UINT src_h = src_rect->bottom - src_rect->top;
283 GLenum gl_filter;
284 const struct wined3d_gl_info *gl_info = context->gl_info;
285 RECT win_rect;
286 UINT win_h;
288 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
289 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
291 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
292 gl_filter = GL_NEAREST;
293 else
294 gl_filter = GL_LINEAR;
296 GetClientRect(swapchain->win_handle, &win_rect);
297 win_h = win_rect.bottom - win_rect.top;
299 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
301 DWORD location = SFLAG_INTEXTURE;
303 if (backbuffer->resource.multisample_type)
305 location = SFLAG_INRB_RESOLVED;
306 surface_load_location(backbuffer, location, NULL);
309 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
310 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
311 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
313 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
314 context_set_draw_buffer(context, GL_BACK);
315 context_invalidate_state(context, STATE_FRAMEBUFFER);
317 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
318 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
319 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
320 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
321 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
323 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
324 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
326 /* Note that the texture is upside down */
327 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
328 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
329 GL_COLOR_BUFFER_BIT, gl_filter);
330 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
332 else
334 struct wined3d_device *device = swapchain->device;
335 struct wined3d_context *context2;
336 float tex_left = src_rect->left;
337 float tex_top = src_rect->top;
338 float tex_right = src_rect->right;
339 float tex_bottom = src_rect->bottom;
341 context2 = context_acquire(device, swapchain->back_buffers[0]);
342 context_apply_blit_state(context2, device);
344 if (backbuffer->flags & SFLAG_NORMCOORD)
346 tex_left /= src_w;
347 tex_right /= src_w;
348 tex_top /= src_h;
349 tex_bottom /= src_h;
352 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
353 gl_filter = GL_NEAREST;
355 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, swapchain->front_buffer, NULL, SFLAG_INDRAWABLE);
356 context_bind_texture(context2, backbuffer->texture_target, backbuffer->texture_name);
358 /* Set up the texture. The surface is not in a wined3d_texture
359 * container, so there are no D3D texture settings to dirtify. */
360 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
361 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
362 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
364 context_set_draw_buffer(context, GL_BACK);
366 /* Set the viewport to the destination rectandle, disable any projection
367 * transformation set up by context_apply_blit_state(), and draw a
368 * (-1,-1)-(1,1) quad.
370 * Back up viewport and matrix to avoid breaking last_was_blit
372 * Note that context_apply_blit_state() set up viewport and ortho to
373 * match the surface size - we want the GL drawable(=window) size. */
374 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
375 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
376 dst_rect->right, win_h - dst_rect->top);
377 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
378 gl_info->gl_ops.gl.p_glPushMatrix();
379 gl_info->gl_ops.gl.p_glLoadIdentity();
381 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
382 /* bottom left */
383 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
384 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
386 /* top left */
387 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
388 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
390 /* top right */
391 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
392 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
394 /* bottom right */
395 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
396 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
397 gl_info->gl_ops.gl.p_glEnd();
399 gl_info->gl_ops.gl.p_glPopMatrix();
400 gl_info->gl_ops.gl.p_glPopAttrib();
402 device->blitter->unset_shader(context->gl_info);
403 checkGLcall("Swapchain present blit(manual)\n");
405 context_release(context2);
409 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
410 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
412 struct wined3d_surface *back_buffer = swapchain->back_buffers[0];
413 const struct wined3d_fb_state *fb = &swapchain->device->fb;
414 const struct wined3d_gl_info *gl_info;
415 struct wined3d_context *context;
416 RECT src_rect, dst_rect;
417 BOOL render_to_fbo;
419 context = context_acquire(swapchain->device, back_buffer);
420 if (!context->valid)
422 context_release(context);
423 WARN("Invalid context, skipping present.\n");
424 return;
427 gl_info = context->gl_info;
429 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
430 if (swapchain->device->bCursorVisible &&
431 swapchain->device->cursorTexture &&
432 !swapchain->device->hardwareCursor)
434 struct wined3d_surface cursor;
435 RECT destRect =
437 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
438 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
439 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
440 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
442 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
443 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
444 * the application because we are only supposed to copy the information out. Using a fake surface
445 * allows us to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
447 memset(&cursor, 0, sizeof(cursor));
448 cursor.resource.ref = 1;
449 cursor.resource.device = swapchain->device;
450 cursor.resource.pool = WINED3D_POOL_SCRATCH;
451 cursor.resource.format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
452 cursor.resource.type = WINED3D_RTYPE_SURFACE;
453 cursor.texture_name = swapchain->device->cursorTexture;
454 cursor.texture_target = GL_TEXTURE_2D;
455 cursor.texture_level = 0;
456 cursor.resource.width = swapchain->device->cursorWidth;
457 cursor.resource.height = swapchain->device->cursorHeight;
458 /* The cursor must have pow2 sizes */
459 cursor.pow2Width = cursor.resource.width;
460 cursor.pow2Height = cursor.resource.height;
461 /* The surface is in the texture */
462 cursor.flags |= SFLAG_INTEXTURE;
463 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
464 * which is exactly what we want :-)
466 if (swapchain->desc.windowed)
467 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
468 wined3d_surface_blt(back_buffer, &destRect, &cursor, NULL, WINEDDBLT_KEYSRC,
469 NULL, WINED3D_TEXF_POINT);
472 if (swapchain->device->logo_surface)
474 struct wined3d_surface *src_surface = swapchain->device->logo_surface;
475 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
477 /* Blit the logo into the upper left corner of the drawable. */
478 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_KEYSRC,
479 NULL, WINED3D_TEXF_POINT);
482 TRACE("Presenting HDC %p.\n", context->hdc);
484 render_to_fbo = swapchain->render_to_fbo;
486 if (src_rect_in)
488 src_rect = *src_rect_in;
489 if (!render_to_fbo && (src_rect.left || src_rect.top
490 || src_rect.right != swapchain->desc.backbuffer_width
491 || src_rect.bottom != swapchain->desc.backbuffer_height))
493 render_to_fbo = TRUE;
496 else
498 src_rect.left = 0;
499 src_rect.top = 0;
500 src_rect.right = swapchain->desc.backbuffer_width;
501 src_rect.bottom = swapchain->desc.backbuffer_height;
504 if (dst_rect_in)
505 dst_rect = *dst_rect_in;
506 else
507 GetClientRect(swapchain->win_handle, &dst_rect);
509 if (!render_to_fbo && (dst_rect.left || dst_rect.top
510 || dst_rect.right != swapchain->desc.backbuffer_width
511 || dst_rect.bottom != swapchain->desc.backbuffer_height))
512 render_to_fbo = TRUE;
514 /* Rendering to a window of different size, presenting partial rectangles,
515 * or rendering to a different window needs help from FBO_blit or a textured
516 * draw. Render the swapchain to a FBO in the future.
518 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
519 * all these issues - this fails if the window is smaller than the backbuffer.
521 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
523 surface_load_location(back_buffer, SFLAG_INTEXTURE, NULL);
524 surface_invalidate_location(back_buffer, SFLAG_INDRAWABLE);
525 swapchain->render_to_fbo = TRUE;
526 swapchain_update_draw_bindings(swapchain);
528 else
530 surface_load_location(back_buffer, back_buffer->draw_binding, NULL);
533 if (swapchain->render_to_fbo)
535 /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
536 * window size mismatch is impossible(fullscreen) and src and dst rectangles are
537 * not allowed(they need the COPY swapeffect)
539 * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
540 * the swap. */
541 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
542 FIXME("Render-to-fbo with WINED3D_SWAP_EFFECT_FLIP\n");
544 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
547 if (swapchain->num_contexts > 1)
548 gl_info->gl_ops.gl.p_glFinish();
550 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
551 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
553 TRACE("SwapBuffers called, Starting new frame\n");
554 /* FPS support */
555 if (TRACE_ON(fps))
557 DWORD time = GetTickCount();
558 ++swapchain->frames;
560 /* every 1.5 seconds */
561 if (time - swapchain->prev_time > 1500)
563 TRACE_(fps)("%p @ approx %.2ffps\n",
564 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
565 swapchain->prev_time = time;
566 swapchain->frames = 0;
570 /* This is disabled, but the code left in for debug purposes.
572 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
573 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
574 * The Debug runtime does the same on Windows. However, a few games do not redraw the
575 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
577 * Tests show that the content of the back buffer after a discard flip is indeed not
578 * reliable, so no game can depend on the exact content. However, it resembles the
579 * old contents in some way, for example by showing fragments at other locations. In
580 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
581 * gets a dark background image. If we clear it with a bright ugly color, the game's
582 * bug shows up much more than it does on Windows, and the players see single pixels
583 * with wrong colors.
584 * (The Max Payne bug has been confirmed on Windows with the debug runtime) */
585 if (FALSE && swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
587 static const struct wined3d_color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
589 TRACE("Clearing the color buffer with cyan color\n");
591 wined3d_device_clear(swapchain->device, 0, NULL,
592 WINED3DCLEAR_TARGET, &cyan, 1.0f, 0);
595 if (!swapchain->render_to_fbo && ((swapchain->front_buffer->flags & SFLAG_INSYSMEM)
596 || (back_buffer->flags & SFLAG_INSYSMEM)))
598 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
599 * Doesn't work with render_to_fbo because we're not flipping
601 struct wined3d_surface *front = swapchain->front_buffer;
603 if (front->resource.size == back_buffer->resource.size)
605 flip_surface(front, back_buffer);
607 /* Tell the front buffer surface that is has been modified. However,
608 * the other locations were preserved during that, so keep the flags.
609 * This serves to update the emulated overlay, if any. */
610 surface_validate_location(front, SFLAG_INDRAWABLE);
612 else
614 surface_validate_location(front, SFLAG_INDRAWABLE);
615 surface_invalidate_location(front, ~SFLAG_INDRAWABLE);
616 surface_validate_location(back_buffer, SFLAG_INDRAWABLE);
617 surface_invalidate_location(back_buffer, ~SFLAG_INDRAWABLE);
620 else
622 surface_validate_location(swapchain->front_buffer, SFLAG_INDRAWABLE);
623 surface_invalidate_location(swapchain->front_buffer, ~SFLAG_INDRAWABLE);
624 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
625 * and INTEXTURE copies can keep their old content if they have any defined content.
626 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
627 * the texture / sysmem copy needs to be reloaded from the drawable
629 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
631 surface_validate_location(back_buffer, back_buffer->draw_binding);
632 surface_invalidate_location(back_buffer, ~back_buffer->draw_binding);
636 if (fb->depth_stencil)
638 if (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
639 || fb->depth_stencil->flags & SFLAG_DISCARD)
641 surface_modify_ds_location(fb->depth_stencil, SFLAG_DISCARDED,
642 fb->depth_stencil->resource.width,
643 fb->depth_stencil->resource.height);
644 if (fb->depth_stencil == swapchain->device->onscreen_depth_stencil)
646 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
647 swapchain->device->onscreen_depth_stencil = NULL;
652 context_release(context);
655 static const struct wined3d_swapchain_ops swapchain_gl_ops =
657 swapchain_gl_present,
660 /* Helper function that blits the front buffer contents to the target window. */
661 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
663 const struct wined3d_surface *front;
664 POINT offset = {0, 0};
665 HDC src_dc, dst_dc;
666 RECT draw_rect;
667 HWND window;
669 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
671 front = swapchain->front_buffer;
672 if (!(front->resource.usage & WINED3DUSAGE_RENDERTARGET))
673 return;
675 if (front->resource.map_count)
676 ERR("Trying to blit a mapped surface.\n");
678 TRACE("Copying surface %p to screen.\n", front);
680 src_dc = front->hDC;
681 window = swapchain->win_handle;
682 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
684 /* Front buffer coordinates are screen coordinates. Map them to the
685 * destination window if not fullscreened. */
686 if (swapchain->desc.windowed)
687 ClientToScreen(window, &offset);
689 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
691 draw_rect.left = 0;
692 draw_rect.right = front->resource.width;
693 draw_rect.top = 0;
694 draw_rect.bottom = front->resource.height;
696 if (rect)
697 IntersectRect(&draw_rect, &draw_rect, rect);
699 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
700 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
701 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
702 ReleaseDC(window, dst_dc);
705 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
706 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
708 struct wined3d_surface *front, *back;
710 front = swapchain->front_buffer;
711 back = swapchain->back_buffers[0];
713 /* Flip the DC. */
715 HDC tmp;
716 tmp = front->hDC;
717 front->hDC = back->hDC;
718 back->hDC = tmp;
721 /* Flip the DIBsection. */
723 HBITMAP tmp;
724 tmp = front->dib.DIBsection;
725 front->dib.DIBsection = back->dib.DIBsection;
726 back->dib.DIBsection = tmp;
729 /* Flip the surface data. */
731 void *tmp;
733 tmp = front->dib.bitmap_data;
734 front->dib.bitmap_data = back->dib.bitmap_data;
735 back->dib.bitmap_data = tmp;
737 tmp = front->resource.allocatedMemory;
738 front->resource.allocatedMemory = back->resource.allocatedMemory;
739 back->resource.allocatedMemory = tmp;
741 if (front->resource.heap_memory)
742 ERR("GDI Surface %p has heap memory allocated.\n", front);
744 if (back->resource.heap_memory)
745 ERR("GDI Surface %p has heap memory allocated.\n", back);
748 /* FPS support */
749 if (TRACE_ON(fps))
751 static LONG prev_time, frames;
752 DWORD time = GetTickCount();
754 ++frames;
756 /* every 1.5 seconds */
757 if (time - prev_time > 1500)
759 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
760 prev_time = time;
761 frames = 0;
765 x11_copy_to_screen(swapchain, NULL);
768 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
770 swapchain_gdi_present,
773 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
775 RECT client_rect;
777 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
778 return;
780 if (!swapchain->desc.backbuffer_count)
782 TRACE("Single buffered rendering.\n");
783 swapchain->render_to_fbo = FALSE;
784 return;
787 GetClientRect(swapchain->win_handle, &client_rect);
789 TRACE("Backbuffer %ux%u, window %ux%u.\n",
790 swapchain->desc.backbuffer_width,
791 swapchain->desc.backbuffer_height,
792 client_rect.right, client_rect.bottom);
793 TRACE("Multisample type %#x, quality %#x.\n",
794 swapchain->desc.multisample_type,
795 swapchain->desc.multisample_quality);
797 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
798 && swapchain->desc.backbuffer_width == client_rect.right
799 && swapchain->desc.backbuffer_height == client_rect.bottom)
801 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
802 swapchain->render_to_fbo = FALSE;
803 return;
806 TRACE("Rendering to FBO.\n");
807 swapchain->render_to_fbo = TRUE;
810 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
811 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
813 const struct wined3d_adapter *adapter = device->adapter;
814 struct wined3d_resource_desc surface_desc;
815 BOOL displaymode_set = FALSE;
816 RECT client_rect;
817 HWND window;
818 HRESULT hr;
819 UINT i;
821 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
823 FIXME("The application requested %u back buffers, this is not supported.\n",
824 desc->backbuffer_count);
825 return WINED3DERR_INVALIDCALL;
828 if (desc->backbuffer_count > 1)
830 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
831 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
834 if (device->wined3d->flags & WINED3D_NO3D)
835 swapchain->swapchain_ops = &swapchain_gdi_ops;
836 else
837 swapchain->swapchain_ops = &swapchain_gl_ops;
839 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
841 swapchain->device = device;
842 swapchain->parent = parent;
843 swapchain->parent_ops = parent_ops;
844 swapchain->ref = 1;
845 swapchain->win_handle = window;
846 swapchain->device_window = window;
848 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
849 adapter->ordinal, &swapchain->original_mode, NULL)))
851 ERR("Failed to get current display mode, hr %#x.\n", hr);
852 goto err;
855 GetClientRect(window, &client_rect);
856 if (desc->windowed
857 && (!desc->backbuffer_width || !desc->backbuffer_height
858 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
861 if (!desc->backbuffer_width)
863 desc->backbuffer_width = client_rect.right;
864 TRACE("Updating width to %u.\n", desc->backbuffer_width);
867 if (!desc->backbuffer_height)
869 desc->backbuffer_height = client_rect.bottom;
870 TRACE("Updating height to %u.\n", desc->backbuffer_height);
873 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
875 desc->backbuffer_format = swapchain->original_mode.format_id;
876 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
879 swapchain->desc = *desc;
880 swapchain_update_render_to_fbo(swapchain);
882 TRACE("Creating front buffer.\n");
884 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
885 surface_desc.format = swapchain->desc.backbuffer_format;
886 surface_desc.multisample_type = swapchain->desc.multisample_type;
887 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
888 surface_desc.usage = 0;
889 surface_desc.pool = WINED3D_POOL_DEFAULT;
890 surface_desc.width = swapchain->desc.backbuffer_width;
891 surface_desc.height = swapchain->desc.backbuffer_height;
892 surface_desc.depth = 1;
893 surface_desc.size = 0;
895 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
896 parent, &surface_desc, &swapchain->front_buffer)))
898 WARN("Failed to create front buffer, hr %#x.\n", hr);
899 goto err;
902 surface_set_swapchain(swapchain->front_buffer, swapchain);
903 if (!(device->wined3d->flags & WINED3D_NO3D))
905 surface_validate_location(swapchain->front_buffer, SFLAG_INDRAWABLE);
906 surface_invalidate_location(swapchain->front_buffer, ~SFLAG_INDRAWABLE);
909 /* MSDN says we're only allowed a single fullscreen swapchain per device,
910 * so we should really check to see if there is a fullscreen swapchain
911 * already. Does a single head count as full screen? */
913 if (!desc->windowed)
915 struct wined3d_display_mode mode;
917 /* Change the display settings */
918 mode.width = desc->backbuffer_width;
919 mode.height = desc->backbuffer_height;
920 mode.format_id = desc->backbuffer_format;
921 mode.refresh_rate = desc->refresh_rate;
922 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
924 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &mode)))
926 WARN("Failed to set display mode, hr %#x.\n", hr);
927 goto err;
929 displaymode_set = TRUE;
932 if (!(device->wined3d->flags & WINED3D_NO3D))
934 static const enum wined3d_format_id formats[] =
936 WINED3DFMT_D24_UNORM_S8_UINT,
937 WINED3DFMT_D32_UNORM,
938 WINED3DFMT_R24_UNORM_X8_TYPELESS,
939 WINED3DFMT_D16_UNORM,
940 WINED3DFMT_S1_UINT_D15_UNORM
943 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
945 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
946 if (!swapchain->context)
948 ERR("Failed to create the context array.\n");
949 hr = E_OUTOFMEMORY;
950 goto err;
952 swapchain->num_contexts = 1;
954 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
955 * You are able to add a depth + stencil surface at a later stage when you need it.
956 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
957 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
958 * context, need torecreate shaders, textures and other resources.
960 * The context manager already takes care of the state problem and for the other tasks code from Reset
961 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
962 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
963 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
964 * issue needs to be fixed. */
965 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
967 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
968 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
969 if (swapchain->context[0]) break;
970 TRACE("Depth stencil format %s is not supported, trying next format\n",
971 debug_d3dformat(formats[i]));
974 if (!swapchain->context[0])
976 WARN("Failed to create context.\n");
977 hr = WINED3DERR_NOTAVAILABLE;
978 goto err;
981 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
982 && (!desc->enable_auto_depth_stencil
983 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
985 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
987 context_release(swapchain->context[0]);
990 if (swapchain->desc.backbuffer_count > 0)
992 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
993 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
994 if (!swapchain->back_buffers)
996 ERR("Failed to allocate backbuffer array memory.\n");
997 hr = E_OUTOFMEMORY;
998 goto err;
1001 surface_desc.usage |= WINED3DUSAGE_RENDERTARGET;
1002 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1004 TRACE("Creating back buffer %u.\n", i);
1005 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1006 parent, &surface_desc, &swapchain->back_buffers[i])))
1008 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1009 goto err;
1011 surface_set_swapchain(swapchain->back_buffers[i], swapchain);
1015 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1016 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1018 TRACE("Creating depth/stencil buffer.\n");
1019 if (!device->auto_depth_stencil)
1021 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
1022 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1024 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
1025 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
1027 WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
1028 goto err;
1033 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1035 return WINED3D_OK;
1037 err:
1038 if (displaymode_set)
1040 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1041 adapter->ordinal, &swapchain->original_mode)))
1042 ERR("Failed to restore display mode.\n");
1043 ClipCursor(NULL);
1046 if (swapchain->back_buffers)
1048 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1050 if (swapchain->back_buffers[i])
1052 surface_set_swapchain(swapchain->back_buffers[i], NULL);
1053 wined3d_surface_decref(swapchain->back_buffers[i]);
1056 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1059 if (swapchain->context)
1061 if (swapchain->context[0])
1063 context_release(swapchain->context[0]);
1064 context_destroy(device, swapchain->context[0]);
1065 swapchain->num_contexts = 0;
1067 HeapFree(GetProcessHeap(), 0, swapchain->context);
1070 if (swapchain->front_buffer)
1072 surface_set_swapchain(swapchain->front_buffer, NULL);
1073 wined3d_surface_decref(swapchain->front_buffer);
1076 return hr;
1079 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1080 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1082 struct wined3d_swapchain *object;
1083 HRESULT hr;
1085 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1086 device, desc, parent, parent_ops, swapchain);
1088 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1089 if (!object)
1090 return E_OUTOFMEMORY;
1092 hr = swapchain_init(object, device, desc, parent, parent_ops);
1093 if (FAILED(hr))
1095 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1096 HeapFree(GetProcessHeap(), 0, object);
1097 return hr;
1100 TRACE("Created swapchain %p.\n", object);
1101 *swapchain = object;
1103 return WINED3D_OK;
1106 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1108 struct wined3d_context **newArray;
1109 struct wined3d_context *ctx;
1111 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1113 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1115 ERR("Failed to create a new context for the swapchain\n");
1116 return NULL;
1118 context_release(ctx);
1120 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1121 if(!newArray) {
1122 ERR("Out of memory when trying to allocate a new context array\n");
1123 context_destroy(swapchain->device, ctx);
1124 return NULL;
1126 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1127 HeapFree(GetProcessHeap(), 0, swapchain->context);
1128 newArray[swapchain->num_contexts] = ctx;
1129 swapchain->context = newArray;
1130 swapchain->num_contexts++;
1132 TRACE("Returning context %p\n", ctx);
1133 return ctx;
1136 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1138 unsigned int i;
1140 for (i = 0; i < swapchain->num_contexts; ++i)
1142 context_destroy(swapchain->device, swapchain->context[i]);
1144 swapchain->num_contexts = 0;
1147 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1149 DWORD tid = GetCurrentThreadId();
1150 unsigned int i;
1152 for (i = 0; i < swapchain->num_contexts; ++i)
1154 if (swapchain->context[i]->tid == tid)
1155 return swapchain->context[i];
1158 /* Create a new context for the thread */
1159 return swapchain_create_context(swapchain);
1162 void get_drawable_size_swapchain(const struct wined3d_context *context, UINT *width, UINT *height)
1164 /* The drawable size of an onscreen drawable is the surface size.
1165 * (Actually: The window size, but the surface is created in window size) */
1166 *width = context->current_rt->resource.width;
1167 *height = context->current_rt->resource.height;
1170 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1172 if (!swapchain->backup_dc)
1174 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1176 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1177 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1179 ERR("Failed to create a window.\n");
1180 return NULL;
1183 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1185 ERR("Failed to get a DC.\n");
1186 DestroyWindow(swapchain->backup_wnd);
1187 swapchain->backup_wnd = NULL;
1188 return NULL;
1192 return swapchain->backup_dc;
1195 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1197 UINT i;
1199 surface_update_draw_binding(swapchain->front_buffer);
1201 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1203 surface_update_draw_binding(swapchain->back_buffers[i]);