d3d8/tests: Add a system memory miptree layout test.
[wine.git] / dlls / wined3d / swapchain.c
blobbada5fe242f15f4b6351b863ecca23268aaa29e4
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, DWORD flags)
138 RECT s, d;
140 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
141 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
142 dst_window_override, flags);
144 if (flags)
145 FIXME("Ignoring flags %#x.\n", flags);
147 if (!swapchain->back_buffers)
149 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
150 return WINED3DERR_INVALIDCALL;
153 if (!src_rect)
155 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
156 swapchain->desc.backbuffer_height);
157 src_rect = &s;
160 if (!dst_rect)
162 GetClientRect(swapchain->win_handle, &d);
163 dst_rect = &d;
166 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
167 dst_rect, dst_window_override, flags);
169 return WINED3D_OK;
172 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
173 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
175 RECT src_rect, dst_rect;
177 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
179 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
180 dst_rect = src_rect;
182 if (swapchain->desc.windowed)
184 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
185 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
186 wine_dbgstr_rect(&dst_rect));
189 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
190 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
193 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
194 UINT back_buffer_idx)
196 TRACE("swapchain %p, back_buffer_idx %u.\n",
197 swapchain, back_buffer_idx);
199 /* Return invalid if there is no backbuffer array, otherwise it will
200 * crash when ddraw is used (there swapchain->back_buffers is always
201 * NULL). We need this because this function is called from
202 * stateblock_init_default_state() to get the default scissorrect
203 * dimensions. */
204 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
206 WARN("Invalid back buffer index.\n");
207 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
208 * here in wined3d to avoid problems in other libs. */
209 return NULL;
212 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
214 return swapchain->back_buffers[back_buffer_idx];
217 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
218 struct wined3d_raster_status *raster_status)
220 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
222 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
223 swapchain->device->adapter->ordinal, raster_status);
226 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
227 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
229 HRESULT hr;
231 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
233 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
234 swapchain->device->adapter->ordinal, mode, rotation);
236 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
237 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
239 return hr;
242 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
244 TRACE("swapchain %p.\n", swapchain);
246 return swapchain->device;
249 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
250 struct wined3d_swapchain_desc *desc)
252 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
254 *desc = swapchain->desc;
257 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
258 DWORD flags, const struct wined3d_gamma_ramp *ramp)
260 HDC dc;
262 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
264 if (flags)
265 FIXME("Ignoring flags %#x.\n", flags);
267 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
268 SetDeviceGammaRamp(dc, (void *)ramp);
269 ReleaseDC(swapchain->device_window, dc);
271 return WINED3D_OK;
274 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
276 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
277 swapchain->palette = palette;
280 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
281 struct wined3d_gamma_ramp *ramp)
283 HDC dc;
285 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
287 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
288 GetDeviceGammaRamp(dc, ramp);
289 ReleaseDC(swapchain->device_window, dc);
291 return WINED3D_OK;
294 /* A GL context is provided by the caller */
295 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
296 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
298 struct wined3d_texture *texture = swapchain->back_buffers[0];
299 struct wined3d_surface *back_buffer = texture->sub_resources[0].u.surface;
300 struct wined3d_surface *front_buffer = swapchain->front_buffer->sub_resources[0].u.surface;
301 UINT src_w = src_rect->right - src_rect->left;
302 UINT src_h = src_rect->bottom - src_rect->top;
303 GLenum gl_filter;
304 const struct wined3d_gl_info *gl_info = context->gl_info;
305 RECT win_rect;
306 UINT win_h;
308 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
309 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
311 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
312 gl_filter = GL_NEAREST;
313 else
314 gl_filter = GL_LINEAR;
316 GetClientRect(swapchain->win_handle, &win_rect);
317 win_h = win_rect.bottom - win_rect.top;
319 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(texture->resource.format->color_fixup))
321 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
323 if (texture->resource.multisample_type)
325 location = WINED3D_LOCATION_RB_RESOLVED;
326 surface_load_location(back_buffer, context, location);
329 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, back_buffer, NULL, location);
330 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
331 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
333 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
334 context_set_draw_buffer(context, GL_BACK);
335 context_invalidate_state(context, STATE_FRAMEBUFFER);
337 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
338 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
339 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
340 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
341 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
343 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
344 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
346 /* Note that the texture is upside down */
347 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
348 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
349 GL_COLOR_BUFFER_BIT, gl_filter);
350 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
352 else
354 struct wined3d_device *device = swapchain->device;
355 struct wined3d_context *context2;
356 float tex_left = src_rect->left;
357 float tex_top = src_rect->top;
358 float tex_right = src_rect->right;
359 float tex_bottom = src_rect->bottom;
361 context2 = context_acquire(device, back_buffer);
362 context_apply_blit_state(context2, device);
364 if (back_buffer->container->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
366 tex_left /= back_buffer->pow2Width;
367 tex_right /= back_buffer->pow2Width;
368 tex_top /= back_buffer->pow2Height;
369 tex_bottom /= back_buffer->pow2Height;
372 if (is_complex_fixup(texture->resource.format->color_fixup))
373 gl_filter = GL_NEAREST;
375 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
376 context_bind_texture(context2, back_buffer->texture_target, texture->texture_rgb.name);
378 /* Set up the texture. The surface is not in a wined3d_texture
379 * container, so there are no D3D texture settings to dirtify. */
380 device->blitter->set_shader(device->blit_priv, context2, back_buffer, NULL);
381 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
382 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
384 context_set_draw_buffer(context, GL_BACK);
386 /* Set the viewport to the destination rectandle, disable any projection
387 * transformation set up by context_apply_blit_state(), and draw a
388 * (-1,-1)-(1,1) quad.
390 * Back up viewport and matrix to avoid breaking last_was_blit
392 * Note that context_apply_blit_state() set up viewport and ortho to
393 * match the surface size - we want the GL drawable(=window) size. */
394 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
395 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
396 dst_rect->right, win_h - dst_rect->top);
397 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
398 gl_info->gl_ops.gl.p_glPushMatrix();
399 gl_info->gl_ops.gl.p_glLoadIdentity();
401 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
402 /* bottom left */
403 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
404 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
406 /* top left */
407 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
408 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
410 /* top right */
411 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
412 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
414 /* bottom right */
415 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
416 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
417 gl_info->gl_ops.gl.p_glEnd();
419 gl_info->gl_ops.gl.p_glPopMatrix();
420 gl_info->gl_ops.gl.p_glPopAttrib();
422 device->blitter->unset_shader(context->gl_info);
423 checkGLcall("Swapchain present blit(manual)\n");
425 context_release(context2);
429 /* Context activation is done by the caller. */
430 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
432 struct wined3d_texture_sub_resource *sub_resource;
433 struct wined3d_texture *texture, *texture_prev;
434 struct gl_texture tex0;
435 GLuint rb0;
436 DWORD locations0;
437 struct wined3d_surface *surface, *surface_prev;
438 unsigned int i;
439 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
441 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
442 return;
444 texture_prev = swapchain->back_buffers[0];
445 surface_prev = texture_prev->sub_resources[0].u.surface;
447 /* Back buffer 0 is already in the draw binding. */
448 tex0 = texture_prev->texture_rgb;
449 rb0 = surface_prev->rb_multisample;
450 locations0 = texture_prev->sub_resources[0].locations;
452 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
454 texture = swapchain->back_buffers[i];
455 sub_resource = &texture->sub_resources[0];
456 surface = sub_resource->u.surface;
458 if (!(sub_resource->locations & supported_locations))
459 surface_load_location(surface, context, texture->resource.draw_binding);
461 texture_prev->texture_rgb = texture->texture_rgb;
462 surface_prev->rb_multisample = surface->rb_multisample;
464 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
465 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
467 texture_prev = texture;
468 surface_prev = surface;
471 texture_prev->texture_rgb = tex0;
472 surface_prev->rb_multisample = rb0;
474 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
475 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
477 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
480 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
481 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
483 struct wined3d_surface *back_buffer = swapchain->back_buffers[0]->sub_resources[0].u.surface;
484 const struct wined3d_fb_state *fb = &swapchain->device->fb;
485 const struct wined3d_gl_info *gl_info;
486 struct wined3d_texture *logo_texture;
487 struct wined3d_context *context;
488 BOOL render_to_fbo;
490 context = context_acquire(swapchain->device, back_buffer);
491 if (!context->valid)
493 context_release(context);
494 WARN("Invalid context, skipping present.\n");
495 return;
498 gl_info = context->gl_info;
500 if ((logo_texture = swapchain->device->logo_texture))
502 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
504 /* Blit the logo into the upper left corner of the drawable. */
505 wined3d_texture_blt(swapchain->back_buffers[0], 0, &rect, logo_texture, 0, &rect,
506 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
509 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
510 && !swapchain->device->hardwareCursor)
512 RECT dst_rect =
514 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
515 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
516 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
517 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
519 RECT src_rect =
521 0, 0,
522 swapchain->device->cursor_texture->resource.width,
523 swapchain->device->cursor_texture->resource.height
525 const RECT clip_rect = {0, 0,
526 swapchain->back_buffers[0]->resource.width,
527 swapchain->back_buffers[0]->resource.height};
529 TRACE("Rendering the software cursor.\n");
531 if (swapchain->desc.windowed)
532 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
533 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
534 wined3d_texture_blt(swapchain->back_buffers[0], 0, &dst_rect,
535 swapchain->device->cursor_texture, 0, &src_rect,
536 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
539 TRACE("Presenting HDC %p.\n", context->hdc);
541 if (!(render_to_fbo = swapchain->render_to_fbo)
542 && (src_rect->left || src_rect->top
543 || src_rect->right != swapchain->desc.backbuffer_width
544 || src_rect->bottom != swapchain->desc.backbuffer_height
545 || dst_rect->left || dst_rect->top
546 || dst_rect->right != swapchain->desc.backbuffer_width
547 || dst_rect->bottom != swapchain->desc.backbuffer_height))
548 render_to_fbo = TRUE;
550 /* Rendering to a window of different size, presenting partial rectangles,
551 * or rendering to a different window needs help from FBO_blit or a textured
552 * draw. Render the swapchain to a FBO in the future.
554 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
555 * all these issues - this fails if the window is smaller than the backbuffer.
557 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
559 surface_load_location(back_buffer, context, WINED3D_LOCATION_TEXTURE_RGB);
560 wined3d_texture_invalidate_location(back_buffer->container, 0, WINED3D_LOCATION_DRAWABLE);
561 swapchain->render_to_fbo = TRUE;
562 swapchain_update_draw_bindings(swapchain);
564 else
566 surface_load_location(back_buffer, context, back_buffer->container->resource.draw_binding);
569 if (swapchain->render_to_fbo)
571 static unsigned int once;
573 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
574 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
576 swapchain_blit(swapchain, context, src_rect, dst_rect);
579 if (swapchain->num_contexts > 1)
580 gl_info->gl_ops.gl.p_glFinish();
582 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
583 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
585 wined3d_swapchain_rotate(swapchain, context);
587 TRACE("SwapBuffers called, Starting new frame\n");
588 /* FPS support */
589 if (TRACE_ON(fps))
591 DWORD time = GetTickCount();
592 ++swapchain->frames;
594 /* every 1.5 seconds */
595 if (time - swapchain->prev_time > 1500)
597 TRACE_(fps)("%p @ approx %.2ffps\n",
598 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
599 swapchain->prev_time = time;
600 swapchain->frames = 0;
604 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
605 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
606 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
607 * and INTEXTURE copies can keep their old content if they have any defined content.
608 * If the swapeffect is COPY, the content remains the same.
610 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
611 * up to date and hope WGL flipped front and back buffers and read this data into
612 * the FBO. Don't bother about this for now. */
614 if (fb->depth_stencil)
616 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
618 if (ds && (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
619 || ds->flags & SFLAG_DISCARD))
621 surface_modify_ds_location(ds, WINED3D_LOCATION_DISCARDED,
622 fb->depth_stencil->width, fb->depth_stencil->height);
623 if (ds == swapchain->device->onscreen_depth_stencil)
625 wined3d_texture_decref(swapchain->device->onscreen_depth_stencil->container);
626 swapchain->device->onscreen_depth_stencil = NULL;
631 context_release(context);
634 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
636 struct wined3d_surface *surface;
637 struct wined3d_context *context;
639 surface = swapchain->front_buffer->sub_resources[0].u.surface;
640 context = context_acquire(swapchain->device, surface);
641 surface_load_location(surface, context, surface->container->resource.draw_binding);
642 context_release(context);
643 SetRectEmpty(&swapchain->front_buffer_update);
646 static const struct wined3d_swapchain_ops swapchain_gl_ops =
648 swapchain_gl_present,
649 swapchain_gl_frontbuffer_updated,
652 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
654 struct wined3d_surface *front;
655 POINT offset = {0, 0};
656 HDC src_dc, dst_dc;
657 RECT draw_rect;
658 HWND window;
660 TRACE("swapchain %p.\n", swapchain);
662 front = swapchain->front_buffer->sub_resources[0].u.surface;
663 if (swapchain->palette)
664 wined3d_palette_apply_to_dc(swapchain->palette, front->hDC);
666 if (front->container->resource.map_count)
667 ERR("Trying to blit a mapped surface.\n");
669 TRACE("Copying surface %p to screen.\n", front);
671 surface_load_location(front, NULL, WINED3D_LOCATION_DIB);
673 src_dc = front->hDC;
674 window = swapchain->win_handle;
675 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
677 /* Front buffer coordinates are screen coordinates. Map them to the
678 * destination window if not fullscreened. */
679 if (swapchain->desc.windowed)
680 ClientToScreen(window, &offset);
682 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
684 draw_rect.left = 0;
685 draw_rect.right = front->resource.width;
686 draw_rect.top = 0;
687 draw_rect.bottom = front->resource.height;
688 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
690 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
691 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
692 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
693 ReleaseDC(window, dst_dc);
695 SetRectEmpty(&swapchain->front_buffer_update);
698 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
699 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
701 struct wined3d_surface *front, *back;
703 front = swapchain->front_buffer->sub_resources[0].u.surface;
704 back = swapchain->back_buffers[0]->sub_resources[0].u.surface;
706 /* Flip the DC. */
708 HDC tmp;
709 tmp = front->hDC;
710 front->hDC = back->hDC;
711 back->hDC = tmp;
714 /* Flip the DIBsection. */
716 HBITMAP tmp;
717 tmp = front->dib.DIBsection;
718 front->dib.DIBsection = back->dib.DIBsection;
719 back->dib.DIBsection = tmp;
722 /* Flip the surface data. */
724 void *tmp;
726 tmp = front->dib.bitmap_data;
727 front->dib.bitmap_data = back->dib.bitmap_data;
728 back->dib.bitmap_data = tmp;
730 if (front->resource.heap_memory)
731 ERR("GDI Surface %p has heap memory allocated.\n", front);
733 if (back->resource.heap_memory)
734 ERR("GDI Surface %p has heap memory allocated.\n", back);
737 /* FPS support */
738 if (TRACE_ON(fps))
740 static LONG prev_time, frames;
741 DWORD time = GetTickCount();
743 ++frames;
745 /* every 1.5 seconds */
746 if (time - prev_time > 1500)
748 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
749 prev_time = time;
750 frames = 0;
754 SetRect(&swapchain->front_buffer_update, 0, 0,
755 swapchain->front_buffer->resource.width,
756 swapchain->front_buffer->resource.height);
757 swapchain_gdi_frontbuffer_updated(swapchain);
760 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
762 swapchain_gdi_present,
763 swapchain_gdi_frontbuffer_updated,
766 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
768 RECT client_rect;
770 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
771 return;
773 if (!swapchain->desc.backbuffer_count)
775 TRACE("Single buffered rendering.\n");
776 swapchain->render_to_fbo = FALSE;
777 return;
780 GetClientRect(swapchain->win_handle, &client_rect);
782 TRACE("Backbuffer %ux%u, window %ux%u.\n",
783 swapchain->desc.backbuffer_width,
784 swapchain->desc.backbuffer_height,
785 client_rect.right, client_rect.bottom);
786 TRACE("Multisample type %#x, quality %#x.\n",
787 swapchain->desc.multisample_type,
788 swapchain->desc.multisample_quality);
790 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
791 && swapchain->desc.backbuffer_width == client_rect.right
792 && swapchain->desc.backbuffer_height == client_rect.bottom)
794 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
795 swapchain->render_to_fbo = FALSE;
796 return;
799 TRACE("Rendering to FBO.\n");
800 swapchain->render_to_fbo = TRUE;
803 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
804 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
806 const struct wined3d_gl_info *gl_info;
807 const struct wined3d_format *format;
808 enum wined3d_multisample_type t;
810 if (wined3d_settings.sample_count == ~0u)
811 return;
813 gl_info = &swapchain->device->adapter->gl_info;
814 if (!(format = wined3d_get_format(gl_info, format_id)))
815 return;
817 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
818 while (!(format->multisample_types & 1u << (t - 1)))
819 ++t;
820 TRACE("Using sample count %u.\n", t);
821 *type = t;
822 *quality = 0;
825 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
826 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
828 const struct wined3d_adapter *adapter = device->adapter;
829 struct wined3d_resource_desc texture_desc;
830 BOOL displaymode_set = FALSE;
831 RECT client_rect;
832 HWND window;
833 HRESULT hr;
834 UINT i;
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 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
857 adapter->ordinal, &swapchain->original_mode, NULL)))
859 ERR("Failed to get current display mode, hr %#x.\n", hr);
860 goto err;
863 GetClientRect(window, &client_rect);
864 if (desc->windowed
865 && (!desc->backbuffer_width || !desc->backbuffer_height
866 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
869 if (!desc->backbuffer_width)
871 desc->backbuffer_width = client_rect.right;
872 TRACE("Updating width to %u.\n", desc->backbuffer_width);
875 if (!desc->backbuffer_height)
877 desc->backbuffer_height = client_rect.bottom;
878 TRACE("Updating height to %u.\n", desc->backbuffer_height);
881 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
883 desc->backbuffer_format = swapchain->original_mode.format_id;
884 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
887 swapchain->desc = *desc;
888 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
889 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
890 swapchain_update_render_to_fbo(swapchain);
892 TRACE("Creating front buffer.\n");
894 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
895 texture_desc.format = swapchain->desc.backbuffer_format;
896 texture_desc.multisample_type = swapchain->desc.multisample_type;
897 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
898 texture_desc.usage = 0;
899 texture_desc.pool = WINED3D_POOL_DEFAULT;
900 texture_desc.width = swapchain->desc.backbuffer_width;
901 texture_desc.height = swapchain->desc.backbuffer_height;
902 texture_desc.depth = 1;
903 texture_desc.size = 0;
905 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
906 parent, &texture_desc, &swapchain->front_buffer)))
908 WARN("Failed to create front buffer, hr %#x.\n", hr);
909 goto err;
912 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
913 if (!(device->wined3d->flags & WINED3D_NO3D))
915 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
916 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
919 /* MSDN says we're only allowed a single fullscreen swapchain per device,
920 * so we should really check to see if there is a fullscreen swapchain
921 * already. Does a single head count as full screen? */
923 if (!desc->windowed)
925 /* Change the display settings */
926 swapchain->d3d_mode.width = desc->backbuffer_width;
927 swapchain->d3d_mode.height = desc->backbuffer_height;
928 swapchain->d3d_mode.format_id = desc->backbuffer_format;
929 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
930 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
932 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &swapchain->d3d_mode)))
934 WARN("Failed to set display mode, hr %#x.\n", hr);
935 goto err;
937 displaymode_set = TRUE;
940 if (!(device->wined3d->flags & WINED3D_NO3D))
942 static const enum wined3d_format_id formats[] =
944 WINED3DFMT_D24_UNORM_S8_UINT,
945 WINED3DFMT_D32_UNORM,
946 WINED3DFMT_R24_UNORM_X8_TYPELESS,
947 WINED3DFMT_D16_UNORM,
948 WINED3DFMT_S1_UINT_D15_UNORM
951 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
953 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
954 if (!swapchain->context)
956 ERR("Failed to create the context array.\n");
957 hr = E_OUTOFMEMORY;
958 goto err;
960 swapchain->num_contexts = 1;
962 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
963 * You are able to add a depth + stencil surface at a later stage when you need it.
964 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
965 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
966 * context, need torecreate shaders, textures and other resources.
968 * The context manager already takes care of the state problem and for the other tasks code from Reset
969 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
970 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
971 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
972 * issue needs to be fixed. */
973 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
975 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
976 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
977 if (swapchain->context[0]) break;
978 TRACE("Depth stencil format %s is not supported, trying next format\n",
979 debug_d3dformat(formats[i]));
982 if (!swapchain->context[0])
984 WARN("Failed to create context.\n");
985 hr = WINED3DERR_NOTAVAILABLE;
986 goto err;
989 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
990 && (!desc->enable_auto_depth_stencil
991 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
993 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
995 context_release(swapchain->context[0]);
996 swapchain_update_swap_interval(swapchain);
999 if (swapchain->desc.backbuffer_count > 0)
1001 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
1002 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
1003 if (!swapchain->back_buffers)
1005 ERR("Failed to allocate backbuffer array memory.\n");
1006 hr = E_OUTOFMEMORY;
1007 goto err;
1010 texture_desc.usage |= WINED3DUSAGE_RENDERTARGET;
1011 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1013 TRACE("Creating back buffer %u.\n", i);
1014 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1015 parent, &texture_desc, &swapchain->back_buffers[i])))
1017 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
1018 swapchain->desc.backbuffer_count = i;
1019 goto err;
1021 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
1025 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1026 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1028 TRACE("Creating depth/stencil buffer.\n");
1029 if (!device->auto_depth_stencil_view)
1031 struct wined3d_texture *ds;
1032 struct wined3d_rendertarget_view_desc desc;
1034 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
1035 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1037 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1038 device->device_parent, &texture_desc, &ds)))
1040 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1041 goto err;
1044 desc.format_id = ds->resource.format->id;
1045 desc.u.texture.level_idx = 0;
1046 desc.u.texture.layer_idx = 0;
1047 desc.u.texture.layer_count = 1;
1048 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1049 &device->auto_depth_stencil_view);
1050 wined3d_texture_decref(ds);
1051 if (FAILED(hr))
1053 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1054 goto err;
1059 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1061 return WINED3D_OK;
1063 err:
1064 if (displaymode_set)
1066 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1067 adapter->ordinal, &swapchain->original_mode)))
1068 ERR("Failed to restore display mode.\n");
1069 ClipCursor(NULL);
1072 if (swapchain->back_buffers)
1074 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1076 if (swapchain->back_buffers[i])
1078 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1079 wined3d_texture_decref(swapchain->back_buffers[i]);
1082 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1085 if (swapchain->context)
1087 if (swapchain->context[0])
1089 context_release(swapchain->context[0]);
1090 context_destroy(device, swapchain->context[0]);
1091 swapchain->num_contexts = 0;
1093 HeapFree(GetProcessHeap(), 0, swapchain->context);
1096 if (swapchain->front_buffer)
1098 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1099 wined3d_texture_decref(swapchain->front_buffer);
1102 return hr;
1105 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1106 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1108 struct wined3d_swapchain *object;
1109 HRESULT hr;
1111 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1112 device, desc, parent, parent_ops, swapchain);
1114 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1115 if (!object)
1116 return E_OUTOFMEMORY;
1118 hr = swapchain_init(object, device, desc, parent, parent_ops);
1119 if (FAILED(hr))
1121 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1122 HeapFree(GetProcessHeap(), 0, object);
1123 return hr;
1126 TRACE("Created swapchain %p.\n", object);
1127 *swapchain = object;
1129 return WINED3D_OK;
1132 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1134 struct wined3d_context **newArray;
1135 struct wined3d_context *ctx;
1137 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1139 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1141 ERR("Failed to create a new context for the swapchain\n");
1142 return NULL;
1144 context_release(ctx);
1146 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1147 if(!newArray) {
1148 ERR("Out of memory when trying to allocate a new context array\n");
1149 context_destroy(swapchain->device, ctx);
1150 return NULL;
1152 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1153 HeapFree(GetProcessHeap(), 0, swapchain->context);
1154 newArray[swapchain->num_contexts] = ctx;
1155 swapchain->context = newArray;
1156 swapchain->num_contexts++;
1158 TRACE("Returning context %p\n", ctx);
1159 return ctx;
1162 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1164 unsigned int i;
1166 for (i = 0; i < swapchain->num_contexts; ++i)
1168 context_destroy(swapchain->device, swapchain->context[i]);
1170 swapchain->num_contexts = 0;
1173 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1175 DWORD tid = GetCurrentThreadId();
1176 unsigned int i;
1178 for (i = 0; i < swapchain->num_contexts; ++i)
1180 if (swapchain->context[i]->tid == tid)
1181 return swapchain->context[i];
1184 /* Create a new context for the thread */
1185 return swapchain_create_context(swapchain);
1188 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1190 if (!swapchain->backup_dc)
1192 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1194 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1195 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1197 ERR("Failed to create a window.\n");
1198 return NULL;
1201 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1203 ERR("Failed to get a DC.\n");
1204 DestroyWindow(swapchain->backup_wnd);
1205 swapchain->backup_wnd = NULL;
1206 return NULL;
1210 return swapchain->backup_dc;
1213 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1215 UINT i;
1217 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1219 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1221 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1225 void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1227 const struct wined3d_gl_info *gl_info;
1228 struct wined3d_context *context;
1229 int swap_interval;
1231 context = context_acquire(swapchain->device, swapchain->front_buffer->sub_resources[0].u.surface);
1232 gl_info = context->gl_info;
1234 switch (swapchain->desc.swap_interval)
1236 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1237 swap_interval = 0;
1238 break;
1239 case WINED3DPRESENT_INTERVAL_DEFAULT:
1240 case WINED3DPRESENT_INTERVAL_ONE:
1241 swap_interval = 1;
1242 break;
1243 case WINED3DPRESENT_INTERVAL_TWO:
1244 swap_interval = 2;
1245 break;
1246 case WINED3DPRESENT_INTERVAL_THREE:
1247 swap_interval = 3;
1248 break;
1249 case WINED3DPRESENT_INTERVAL_FOUR:
1250 swap_interval = 4;
1251 break;
1252 default:
1253 FIXME("Unhandled present interval %#x.\n", swapchain->desc.swap_interval);
1254 swap_interval = 1;
1257 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1259 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1260 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1261 swap_interval, context, GetLastError());
1264 context_release(context);
1267 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1269 struct wined3d_device *device = swapchain->device;
1270 BOOL filter_messages = device->filter_messages;
1272 /* This code is not protected by the wined3d mutex, so it may run while
1273 * wined3d_device_reset is active. Testing on Windows shows that changing
1274 * focus during resets and resetting during focus change events causes
1275 * the application to crash with an invalid memory access. */
1277 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1279 if (activate)
1281 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1283 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1284 * the window but leaves the size untouched, d3d9 sets the size on an
1285 * invisible window, generates messages but doesn't change the window
1286 * properties. The implementation follows d3d9.
1288 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1289 * resume drawing after a focus loss. */
1290 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1291 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1292 SWP_NOACTIVATE | SWP_NOZORDER);
1295 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1297 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1298 device->adapter->ordinal, &swapchain->d3d_mode)))
1299 ERR("Failed to set display mode.\n");
1302 else
1304 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1305 device->adapter->ordinal, NULL)))
1306 ERR("Failed to set display mode.\n");
1308 swapchain->reapply_mode = TRUE;
1310 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1311 && IsWindowVisible(swapchain->device_window))
1312 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1315 device->filter_messages = filter_messages;
1318 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1319 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1320 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1322 BOOL update_desc = FALSE;
1324 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1325 "multisample_type %#x, multisample_quality %#x.\n",
1326 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1327 multisample_type, multisample_quality);
1329 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1331 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1332 FIXME("Cannot change the back buffer count yet.\n");
1334 if (!width || !height)
1336 /* The application is requesting that either the swapchain width or
1337 * height be set to the corresponding dimension in the window's
1338 * client rect. */
1340 RECT client_rect;
1342 if (!swapchain->desc.windowed)
1343 return WINED3DERR_INVALIDCALL;
1345 if (!GetClientRect(swapchain->device_window, &client_rect))
1347 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1348 return WINED3DERR_INVALIDCALL;
1351 if (!width)
1352 width = client_rect.right;
1354 if (!height)
1355 height = client_rect.bottom;
1358 if (width != swapchain->desc.backbuffer_width
1359 || height != swapchain->desc.backbuffer_height)
1361 swapchain->desc.backbuffer_width = width;
1362 swapchain->desc.backbuffer_height = height;
1363 update_desc = TRUE;
1366 if (format_id == WINED3DFMT_UNKNOWN)
1368 if (!swapchain->desc.windowed)
1369 return WINED3DERR_INVALIDCALL;
1370 format_id = swapchain->original_mode.format_id;
1373 if (format_id != swapchain->desc.backbuffer_format)
1375 swapchain->desc.backbuffer_format = format_id;
1376 update_desc = TRUE;
1379 if (multisample_type != swapchain->desc.multisample_type
1380 || multisample_quality != swapchain->desc.multisample_quality)
1382 swapchain->desc.multisample_type = multisample_type;
1383 swapchain->desc.multisample_quality = multisample_quality;
1384 update_desc = TRUE;
1387 if (update_desc)
1389 HRESULT hr;
1390 UINT i;
1392 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1393 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1394 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1395 return hr;
1397 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1399 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1400 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1401 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1402 return hr;
1406 swapchain_update_render_to_fbo(swapchain);
1407 swapchain_update_draw_bindings(swapchain);
1409 return WINED3D_OK;