wined3d: Call glReadBuffer() through the appropriate function pointer.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
blob887143f0686a41104df004bdb86bd460a4636d6e
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 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
44 if (wined3d_texture_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 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
56 if (wined3d_texture_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 wined3d_release_dc(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_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
153 dst_rect, dst_window_override, dirty_region, flags);
155 return WINED3D_OK;
158 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
159 struct wined3d_surface *dst_surface)
161 struct wined3d_surface *src_surface;
162 RECT src_rect, dst_rect;
164 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
166 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
167 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
168 dst_rect = src_rect;
170 if (swapchain->desc.windowed)
172 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
173 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
174 wine_dbgstr_rect(&dst_rect));
177 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
180 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
181 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
183 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
184 swapchain, back_buffer_idx, type);
186 /* Return invalid if there is no backbuffer array, otherwise it will
187 * crash when ddraw is used (there swapchain->back_buffers is always
188 * NULL). We need this because this function is called from
189 * stateblock_init_default_state() to get the default scissorrect
190 * dimensions. */
191 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
193 WARN("Invalid back buffer index.\n");
194 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
195 * here in wined3d to avoid problems in other libs. */
196 return NULL;
199 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
201 return surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[back_buffer_idx], 0));
204 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
205 struct wined3d_raster_status *raster_status)
207 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
209 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
210 swapchain->device->adapter->ordinal, raster_status);
213 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
214 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
216 HRESULT hr;
218 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
220 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
221 swapchain->device->adapter->ordinal, mode, rotation);
223 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
224 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
226 return hr;
229 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
231 TRACE("swapchain %p.\n", swapchain);
233 return swapchain->device;
236 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
237 struct wined3d_swapchain_desc *desc)
239 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
241 *desc = swapchain->desc;
244 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
245 DWORD flags, const struct wined3d_gamma_ramp *ramp)
247 HDC dc;
249 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
251 if (flags)
252 FIXME("Ignoring flags %#x.\n", flags);
254 dc = GetDC(swapchain->device_window);
255 SetDeviceGammaRamp(dc, (void *)ramp);
256 ReleaseDC(swapchain->device_window, dc);
258 return WINED3D_OK;
261 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
263 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
264 swapchain->palette = palette;
267 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
268 struct wined3d_gamma_ramp *ramp)
270 HDC dc;
272 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
274 dc = GetDC(swapchain->device_window);
275 GetDeviceGammaRamp(dc, ramp);
276 ReleaseDC(swapchain->device_window, dc);
278 return WINED3D_OK;
281 /* A GL context is provided by the caller */
282 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
283 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
285 struct wined3d_surface *backbuffer = surface_from_resource(
286 wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
287 UINT src_w = src_rect->right - src_rect->left;
288 UINT src_h = src_rect->bottom - src_rect->top;
289 GLenum gl_filter;
290 const struct wined3d_gl_info *gl_info = context->gl_info;
291 RECT win_rect;
292 UINT win_h;
294 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
295 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
297 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
298 gl_filter = GL_NEAREST;
299 else
300 gl_filter = GL_LINEAR;
302 GetClientRect(swapchain->win_handle, &win_rect);
303 win_h = win_rect.bottom - win_rect.top;
305 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
307 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
309 if (backbuffer->resource.multisample_type)
311 location = WINED3D_LOCATION_RB_RESOLVED;
312 surface_load_location(backbuffer, location);
315 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
316 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
317 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
319 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
320 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
321 NULL, WINED3D_LOCATION_DRAWABLE);
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, backbuffer);
350 context_apply_blit_state(context2, device);
352 if (backbuffer->container->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
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,
364 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
365 NULL, WINED3D_LOCATION_DRAWABLE);
366 context_bind_texture(context2, backbuffer->texture_target, backbuffer->container->texture_rgb.name);
368 /* Set up the texture. The surface is not in a wined3d_texture
369 * container, so there are no D3D texture settings to dirtify. */
370 device->blitter->set_shader(device->blit_priv, context2, backbuffer);
371 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
372 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
374 context_set_draw_buffer(context, GL_BACK);
376 /* Set the viewport to the destination rectandle, disable any projection
377 * transformation set up by context_apply_blit_state(), and draw a
378 * (-1,-1)-(1,1) quad.
380 * Back up viewport and matrix to avoid breaking last_was_blit
382 * Note that context_apply_blit_state() set up viewport and ortho to
383 * match the surface size - we want the GL drawable(=window) size. */
384 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
385 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
386 dst_rect->right, win_h - dst_rect->top);
387 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
388 gl_info->gl_ops.gl.p_glPushMatrix();
389 gl_info->gl_ops.gl.p_glLoadIdentity();
391 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
392 /* bottom left */
393 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
394 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
396 /* top left */
397 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
398 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
400 /* top right */
401 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
402 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
404 /* bottom right */
405 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
406 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
407 gl_info->gl_ops.gl.p_glEnd();
409 gl_info->gl_ops.gl.p_glPopMatrix();
410 gl_info->gl_ops.gl.p_glPopAttrib();
412 device->blitter->unset_shader(context->gl_info);
413 checkGLcall("Swapchain present blit(manual)\n");
415 context_release(context2);
419 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
420 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
422 struct wined3d_surface *back_buffer = surface_from_resource(
423 wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
424 const struct wined3d_fb_state *fb = &swapchain->device->fb;
425 const struct wined3d_gl_info *gl_info;
426 struct wined3d_context *context;
427 struct wined3d_surface *front;
428 RECT src_rect, dst_rect;
429 BOOL render_to_fbo;
431 context = context_acquire(swapchain->device, back_buffer);
432 if (!context->valid)
434 context_release(context);
435 WARN("Invalid context, skipping present.\n");
436 return;
439 gl_info = context->gl_info;
441 if (swapchain->device->logo_texture)
443 struct wined3d_surface *src_surface = surface_from_resource(
444 wined3d_texture_get_sub_resource(swapchain->device->logo_texture, 0));
445 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
447 /* Blit the logo into the upper left corner of the drawable. */
448 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_ALPHATEST,
449 NULL, WINED3D_TEXF_POINT);
452 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
453 && !swapchain->device->hardwareCursor)
455 struct wined3d_surface *cursor = surface_from_resource(
456 wined3d_texture_get_sub_resource(swapchain->device->cursor_texture, 0));
457 RECT destRect =
459 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
460 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
461 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
462 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
465 TRACE("Rendering the software cursor.\n");
467 if (swapchain->desc.windowed)
468 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
469 wined3d_surface_blt(back_buffer, &destRect, cursor, NULL, WINEDDBLT_ALPHATEST,
470 NULL, WINED3D_TEXF_POINT);
473 TRACE("Presenting HDC %p.\n", context->hdc);
475 render_to_fbo = swapchain->render_to_fbo;
477 if (src_rect_in)
479 src_rect = *src_rect_in;
480 if (!render_to_fbo && (src_rect.left || src_rect.top
481 || src_rect.right != swapchain->desc.backbuffer_width
482 || src_rect.bottom != swapchain->desc.backbuffer_height))
484 render_to_fbo = TRUE;
487 else
489 src_rect.left = 0;
490 src_rect.top = 0;
491 src_rect.right = swapchain->desc.backbuffer_width;
492 src_rect.bottom = swapchain->desc.backbuffer_height;
495 if (dst_rect_in)
496 dst_rect = *dst_rect_in;
497 else
498 GetClientRect(swapchain->win_handle, &dst_rect);
500 if (!render_to_fbo && (dst_rect.left || dst_rect.top
501 || dst_rect.right != swapchain->desc.backbuffer_width
502 || dst_rect.bottom != swapchain->desc.backbuffer_height))
503 render_to_fbo = TRUE;
505 /* Rendering to a window of different size, presenting partial rectangles,
506 * or rendering to a different window needs help from FBO_blit or a textured
507 * draw. Render the swapchain to a FBO in the future.
509 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
510 * all these issues - this fails if the window is smaller than the backbuffer.
512 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
514 surface_load_location(back_buffer, WINED3D_LOCATION_TEXTURE_RGB);
515 surface_invalidate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
516 swapchain->render_to_fbo = TRUE;
517 swapchain_update_draw_bindings(swapchain);
519 else
521 surface_load_location(back_buffer, back_buffer->container->resource.draw_binding);
524 if (swapchain->render_to_fbo)
526 static unsigned int once;
528 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
529 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
531 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
534 if (swapchain->num_contexts > 1)
535 gl_info->gl_ops.gl.p_glFinish();
537 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
538 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
540 TRACE("SwapBuffers called, Starting new frame\n");
541 /* FPS support */
542 if (TRACE_ON(fps))
544 DWORD time = GetTickCount();
545 ++swapchain->frames;
547 /* every 1.5 seconds */
548 if (time - swapchain->prev_time > 1500)
550 TRACE_(fps)("%p @ approx %.2ffps\n",
551 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
552 swapchain->prev_time = time;
553 swapchain->frames = 0;
557 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
558 if (!swapchain->render_to_fbo && ((front->locations & WINED3D_LOCATION_SYSMEM)
559 || (back_buffer->locations & WINED3D_LOCATION_SYSMEM)))
561 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
562 * Doesn't work with render_to_fbo because we're not flipping
565 if (front->resource.size == back_buffer->resource.size)
567 flip_surface(front, back_buffer);
569 /* Tell the front buffer surface that is has been modified. However,
570 * the other locations were preserved during that, so keep the flags.
571 * This serves to update the emulated overlay, if any. */
572 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
574 else
576 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
577 surface_invalidate_location(front, ~WINED3D_LOCATION_DRAWABLE);
578 surface_validate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
579 surface_invalidate_location(back_buffer, ~WINED3D_LOCATION_DRAWABLE);
582 else
584 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
585 surface_invalidate_location(front, ~WINED3D_LOCATION_DRAWABLE);
586 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
587 * and INTEXTURE copies can keep their old content if they have any defined content.
588 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
589 * the texture / sysmem copy needs to be reloaded from the drawable
591 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
593 surface_validate_location(back_buffer, back_buffer->container->resource.draw_binding);
594 surface_invalidate_location(back_buffer, ~back_buffer->container->resource.draw_binding);
598 if (fb->depth_stencil)
600 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
602 if (ds && (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
603 || ds->flags & SFLAG_DISCARD))
605 surface_modify_ds_location(ds, WINED3D_LOCATION_DISCARDED,
606 fb->depth_stencil->width, fb->depth_stencil->height);
607 if (ds == swapchain->device->onscreen_depth_stencil)
609 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
610 swapchain->device->onscreen_depth_stencil = NULL;
615 context_release(context);
618 static const struct wined3d_swapchain_ops swapchain_gl_ops =
620 swapchain_gl_present,
623 /* Helper function that blits the front buffer contents to the target window. */
624 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
626 struct wined3d_surface *front;
627 POINT offset = {0, 0};
628 HDC src_dc, dst_dc;
629 RECT draw_rect;
630 HWND window;
632 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
634 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
635 if (swapchain->palette)
636 wined3d_palette_apply_to_dc(swapchain->palette, front->hDC);
638 if (front->resource.map_count)
639 ERR("Trying to blit a mapped surface.\n");
641 TRACE("Copying surface %p to screen.\n", front);
643 surface_load_location(front, WINED3D_LOCATION_DIB);
645 src_dc = front->hDC;
646 window = swapchain->win_handle;
647 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
649 /* Front buffer coordinates are screen coordinates. Map them to the
650 * destination window if not fullscreened. */
651 if (swapchain->desc.windowed)
652 ClientToScreen(window, &offset);
654 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
656 draw_rect.left = 0;
657 draw_rect.right = front->resource.width;
658 draw_rect.top = 0;
659 draw_rect.bottom = front->resource.height;
661 if (rect)
662 IntersectRect(&draw_rect, &draw_rect, rect);
664 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
665 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
666 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
667 ReleaseDC(window, dst_dc);
670 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
671 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
673 struct wined3d_surface *front, *back;
675 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
676 back = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
678 /* Flip the DC. */
680 HDC tmp;
681 tmp = front->hDC;
682 front->hDC = back->hDC;
683 back->hDC = tmp;
686 /* Flip the DIBsection. */
688 HBITMAP tmp;
689 tmp = front->dib.DIBsection;
690 front->dib.DIBsection = back->dib.DIBsection;
691 back->dib.DIBsection = tmp;
694 /* Flip the surface data. */
696 void *tmp;
698 tmp = front->dib.bitmap_data;
699 front->dib.bitmap_data = back->dib.bitmap_data;
700 back->dib.bitmap_data = tmp;
702 if (front->resource.heap_memory)
703 ERR("GDI Surface %p has heap memory allocated.\n", front);
705 if (back->resource.heap_memory)
706 ERR("GDI Surface %p has heap memory allocated.\n", back);
709 /* FPS support */
710 if (TRACE_ON(fps))
712 static LONG prev_time, frames;
713 DWORD time = GetTickCount();
715 ++frames;
717 /* every 1.5 seconds */
718 if (time - prev_time > 1500)
720 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
721 prev_time = time;
722 frames = 0;
726 x11_copy_to_screen(swapchain, NULL);
729 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
731 swapchain_gdi_present,
734 void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
736 RECT client_rect;
738 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
739 return;
741 if (!swapchain->desc.backbuffer_count)
743 TRACE("Single buffered rendering.\n");
744 swapchain->render_to_fbo = FALSE;
745 return;
748 GetClientRect(swapchain->win_handle, &client_rect);
750 TRACE("Backbuffer %ux%u, window %ux%u.\n",
751 swapchain->desc.backbuffer_width,
752 swapchain->desc.backbuffer_height,
753 client_rect.right, client_rect.bottom);
754 TRACE("Multisample type %#x, quality %#x.\n",
755 swapchain->desc.multisample_type,
756 swapchain->desc.multisample_quality);
758 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
759 && swapchain->desc.backbuffer_width == client_rect.right
760 && swapchain->desc.backbuffer_height == client_rect.bottom)
762 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
763 swapchain->render_to_fbo = FALSE;
764 return;
767 TRACE("Rendering to FBO.\n");
768 swapchain->render_to_fbo = TRUE;
771 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
772 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
774 const struct wined3d_adapter *adapter = device->adapter;
775 struct wined3d_resource_desc surface_desc;
776 struct wined3d_surface *front_buffer;
777 BOOL displaymode_set = FALSE;
778 RECT client_rect;
779 HWND window;
780 HRESULT hr;
781 UINT i;
783 if (desc->backbuffer_count > WINED3DPRESENT_BACK_BUFFER_MAX)
785 FIXME("The application requested %u back buffers, this is not supported.\n",
786 desc->backbuffer_count);
787 return WINED3DERR_INVALIDCALL;
790 if (desc->backbuffer_count > 1)
792 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
793 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
796 if (device->wined3d->flags & WINED3D_NO3D)
797 swapchain->swapchain_ops = &swapchain_gdi_ops;
798 else
799 swapchain->swapchain_ops = &swapchain_gl_ops;
801 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
803 swapchain->device = device;
804 swapchain->parent = parent;
805 swapchain->parent_ops = parent_ops;
806 swapchain->ref = 1;
807 swapchain->win_handle = window;
808 swapchain->device_window = window;
810 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
811 adapter->ordinal, &swapchain->original_mode, NULL)))
813 ERR("Failed to get current display mode, hr %#x.\n", hr);
814 goto err;
817 GetClientRect(window, &client_rect);
818 if (desc->windowed
819 && (!desc->backbuffer_width || !desc->backbuffer_height
820 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
823 if (!desc->backbuffer_width)
825 desc->backbuffer_width = client_rect.right;
826 TRACE("Updating width to %u.\n", desc->backbuffer_width);
829 if (!desc->backbuffer_height)
831 desc->backbuffer_height = client_rect.bottom;
832 TRACE("Updating height to %u.\n", desc->backbuffer_height);
835 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
837 desc->backbuffer_format = swapchain->original_mode.format_id;
838 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
841 swapchain->desc = *desc;
842 swapchain_update_render_to_fbo(swapchain);
844 TRACE("Creating front buffer.\n");
846 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
847 surface_desc.format = swapchain->desc.backbuffer_format;
848 surface_desc.multisample_type = swapchain->desc.multisample_type;
849 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
850 surface_desc.usage = 0;
851 surface_desc.pool = WINED3D_POOL_DEFAULT;
852 surface_desc.width = swapchain->desc.backbuffer_width;
853 surface_desc.height = swapchain->desc.backbuffer_height;
854 surface_desc.depth = 1;
855 surface_desc.size = 0;
857 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
858 parent, &surface_desc, &front_buffer)))
860 WARN("Failed to create front buffer, hr %#x.\n", hr);
861 goto err;
864 swapchain->front_buffer = front_buffer->container;
865 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
866 if (!(device->wined3d->flags & WINED3D_NO3D))
868 surface_validate_location(front_buffer, WINED3D_LOCATION_DRAWABLE);
869 surface_invalidate_location(front_buffer, ~WINED3D_LOCATION_DRAWABLE);
872 /* MSDN says we're only allowed a single fullscreen swapchain per device,
873 * so we should really check to see if there is a fullscreen swapchain
874 * already. Does a single head count as full screen? */
876 if (!desc->windowed)
878 /* Change the display settings */
879 swapchain->d3d_mode.width = desc->backbuffer_width;
880 swapchain->d3d_mode.height = desc->backbuffer_height;
881 swapchain->d3d_mode.format_id = desc->backbuffer_format;
882 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
883 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
885 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &swapchain->d3d_mode)))
887 WARN("Failed to set display mode, hr %#x.\n", hr);
888 goto err;
890 displaymode_set = TRUE;
893 if (!(device->wined3d->flags & WINED3D_NO3D))
895 static const enum wined3d_format_id formats[] =
897 WINED3DFMT_D24_UNORM_S8_UINT,
898 WINED3DFMT_D32_UNORM,
899 WINED3DFMT_R24_UNORM_X8_TYPELESS,
900 WINED3DFMT_D16_UNORM,
901 WINED3DFMT_S1_UINT_D15_UNORM
904 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
906 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
907 if (!swapchain->context)
909 ERR("Failed to create the context array.\n");
910 hr = E_OUTOFMEMORY;
911 goto err;
913 swapchain->num_contexts = 1;
915 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
916 * You are able to add a depth + stencil surface at a later stage when you need it.
917 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
918 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
919 * context, need torecreate shaders, textures and other resources.
921 * The context manager already takes care of the state problem and for the other tasks code from Reset
922 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
923 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
924 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
925 * issue needs to be fixed. */
926 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
928 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
929 swapchain->context[0] = context_create(swapchain, front_buffer, swapchain->ds_format);
930 if (swapchain->context[0]) break;
931 TRACE("Depth stencil format %s is not supported, trying next format\n",
932 debug_d3dformat(formats[i]));
935 if (!swapchain->context[0])
937 WARN("Failed to create context.\n");
938 hr = WINED3DERR_NOTAVAILABLE;
939 goto err;
942 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
943 && (!desc->enable_auto_depth_stencil
944 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
946 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
948 context_release(swapchain->context[0]);
951 if (swapchain->desc.backbuffer_count > 0)
953 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
954 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
955 if (!swapchain->back_buffers)
957 ERR("Failed to allocate backbuffer array memory.\n");
958 hr = E_OUTOFMEMORY;
959 goto err;
962 surface_desc.usage |= WINED3DUSAGE_RENDERTARGET;
963 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
965 struct wined3d_surface *back_buffer;
967 TRACE("Creating back buffer %u.\n", i);
968 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
969 parent, &surface_desc, &back_buffer)))
971 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
972 swapchain->desc.backbuffer_count = i;
973 goto err;
975 swapchain->back_buffers[i] = back_buffer->container;
976 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
980 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
981 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
983 TRACE("Creating depth/stencil buffer.\n");
984 if (!device->auto_depth_stencil_view)
986 struct wined3d_surface *ds;
988 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
989 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
991 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
992 device->device_parent, &surface_desc, &ds)))
994 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
995 goto err;
998 hr = wined3d_rendertarget_view_create_from_surface(ds,
999 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
1000 wined3d_surface_decref(ds);
1001 if (FAILED(hr))
1003 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1004 goto err;
1009 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1011 return WINED3D_OK;
1013 err:
1014 if (displaymode_set)
1016 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1017 adapter->ordinal, &swapchain->original_mode)))
1018 ERR("Failed to restore display mode.\n");
1019 ClipCursor(NULL);
1022 if (swapchain->back_buffers)
1024 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1026 if (swapchain->back_buffers[i])
1028 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1029 wined3d_texture_decref(swapchain->back_buffers[i]);
1032 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1035 if (swapchain->context)
1037 if (swapchain->context[0])
1039 context_release(swapchain->context[0]);
1040 context_destroy(device, swapchain->context[0]);
1041 swapchain->num_contexts = 0;
1043 HeapFree(GetProcessHeap(), 0, swapchain->context);
1046 if (swapchain->front_buffer)
1048 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1049 wined3d_texture_decref(swapchain->front_buffer);
1052 return hr;
1055 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1056 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1058 struct wined3d_swapchain *object;
1059 HRESULT hr;
1061 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1062 device, desc, parent, parent_ops, swapchain);
1064 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1065 if (!object)
1066 return E_OUTOFMEMORY;
1068 hr = swapchain_init(object, device, desc, parent, parent_ops);
1069 if (FAILED(hr))
1071 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1072 HeapFree(GetProcessHeap(), 0, object);
1073 return hr;
1076 TRACE("Created swapchain %p.\n", object);
1077 *swapchain = object;
1079 return WINED3D_OK;
1082 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1084 struct wined3d_context **newArray;
1085 struct wined3d_context *ctx;
1087 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1089 if (!(ctx = context_create(swapchain,
1090 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
1091 swapchain->ds_format)))
1093 ERR("Failed to create a new context for the swapchain\n");
1094 return NULL;
1096 context_release(ctx);
1098 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1099 if(!newArray) {
1100 ERR("Out of memory when trying to allocate a new context array\n");
1101 context_destroy(swapchain->device, ctx);
1102 return NULL;
1104 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1105 HeapFree(GetProcessHeap(), 0, swapchain->context);
1106 newArray[swapchain->num_contexts] = ctx;
1107 swapchain->context = newArray;
1108 swapchain->num_contexts++;
1110 TRACE("Returning context %p\n", ctx);
1111 return ctx;
1114 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1116 unsigned int i;
1118 for (i = 0; i < swapchain->num_contexts; ++i)
1120 context_destroy(swapchain->device, swapchain->context[i]);
1122 swapchain->num_contexts = 0;
1125 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1127 DWORD tid = GetCurrentThreadId();
1128 unsigned int i;
1130 for (i = 0; i < swapchain->num_contexts; ++i)
1132 if (swapchain->context[i]->tid == tid)
1133 return swapchain->context[i];
1136 /* Create a new context for the thread */
1137 return swapchain_create_context(swapchain);
1140 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1142 if (!swapchain->backup_dc)
1144 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1146 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1147 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1149 ERR("Failed to create a window.\n");
1150 return NULL;
1153 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1155 ERR("Failed to get a DC.\n");
1156 DestroyWindow(swapchain->backup_wnd);
1157 swapchain->backup_wnd = NULL;
1158 return NULL;
1162 return swapchain->backup_dc;
1165 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1167 UINT i;
1169 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1171 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1173 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1177 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1179 struct wined3d_device *device = swapchain->device;
1180 BOOL filter_messages = device->filter_messages;
1182 /* This code is not protected by the wined3d mutex, so it may run while
1183 * wined3d_device_reset is active. Testing on Windows shows that changing
1184 * focus during resets and resetting during focus change events causes
1185 * the application to crash with an invalid memory access. */
1187 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1189 if (activate)
1191 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1193 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1194 * the window but leaves the size untouched, d3d9 sets the size on an
1195 * invisible window, generates messages but doesn't change the window
1196 * properties. The implementation follows d3d9.
1198 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1199 * resume drawing after a focus loss. */
1200 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1201 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1202 SWP_NOACTIVATE | SWP_NOZORDER);
1205 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1207 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1208 device->adapter->ordinal, &swapchain->d3d_mode)))
1209 ERR("Failed to set display mode.\n");
1212 else
1214 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1215 device->adapter->ordinal, NULL)))
1216 ERR("Failed to set display mode.\n");
1218 swapchain->reapply_mode = TRUE;
1220 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1221 && IsWindowVisible(swapchain->device_window))
1222 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1225 device->filter_messages = filter_messages;