wined3d: Add cube array sampler support.
[wine.git] / dlls / wined3d / swapchain.c
blob56646e7b141890e4500ce7eb9d1ee0abac5ecd9f
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);
81 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
83 wined3d_device_restore_fullscreen_window(swapchain->device, swapchain->device_window,
84 &swapchain->original_window_rect);
85 wined3d_device_release_focus_window(swapchain->device);
89 if (swapchain->backup_dc)
91 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
93 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
94 DestroyWindow(swapchain->backup_wnd);
98 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
100 ULONG refcount = InterlockedIncrement(&swapchain->ref);
102 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
104 return refcount;
107 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
109 ULONG refcount = InterlockedDecrement(&swapchain->ref);
111 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
113 if (!refcount)
115 swapchain_cleanup(swapchain);
116 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
117 HeapFree(GetProcessHeap(), 0, swapchain);
120 return refcount;
123 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
125 TRACE("swapchain %p.\n", swapchain);
127 return swapchain->parent;
130 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
132 if (!window)
133 window = swapchain->device_window;
134 if (window == swapchain->win_handle)
135 return;
137 TRACE("Setting swapchain %p window from %p to %p.\n",
138 swapchain, swapchain->win_handle, window);
139 swapchain->win_handle = window;
142 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
143 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, DWORD flags)
145 RECT s, d;
147 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
148 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
149 dst_window_override, flags);
151 if (flags)
152 FIXME("Ignoring flags %#x.\n", flags);
154 if (!swapchain->back_buffers)
156 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
157 return WINED3DERR_INVALIDCALL;
160 if (!src_rect)
162 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
163 swapchain->desc.backbuffer_height);
164 src_rect = &s;
167 if (!dst_rect)
169 GetClientRect(swapchain->win_handle, &d);
170 dst_rect = &d;
173 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
174 dst_rect, dst_window_override, flags);
176 return WINED3D_OK;
179 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
180 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
182 RECT src_rect, dst_rect;
184 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
186 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
187 dst_rect = src_rect;
189 if (swapchain->desc.windowed)
191 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
192 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
193 wine_dbgstr_rect(&dst_rect));
196 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
197 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
200 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
201 UINT back_buffer_idx)
203 TRACE("swapchain %p, back_buffer_idx %u.\n",
204 swapchain, back_buffer_idx);
206 /* Return invalid if there is no backbuffer array, otherwise it will
207 * crash when ddraw is used (there swapchain->back_buffers is always
208 * NULL). We need this because this function is called from
209 * stateblock_init_default_state() to get the default scissorrect
210 * dimensions. */
211 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
213 WARN("Invalid back buffer index.\n");
214 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
215 * here in wined3d to avoid problems in other libs. */
216 return NULL;
219 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
221 return swapchain->back_buffers[back_buffer_idx];
224 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
225 struct wined3d_raster_status *raster_status)
227 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
229 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
230 swapchain->device->adapter->ordinal, raster_status);
233 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
234 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
236 HRESULT hr;
238 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
240 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
241 swapchain->device->adapter->ordinal, mode, rotation);
243 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
244 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
246 return hr;
249 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
251 TRACE("swapchain %p.\n", swapchain);
253 return swapchain->device;
256 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
257 struct wined3d_swapchain_desc *desc)
259 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
261 *desc = swapchain->desc;
264 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
265 DWORD flags, const struct wined3d_gamma_ramp *ramp)
267 HDC dc;
269 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
271 if (flags)
272 FIXME("Ignoring flags %#x.\n", flags);
274 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
275 SetDeviceGammaRamp(dc, (void *)ramp);
276 ReleaseDC(swapchain->device_window, dc);
278 return WINED3D_OK;
281 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
283 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
284 swapchain->palette = palette;
287 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
288 struct wined3d_gamma_ramp *ramp)
290 HDC dc;
292 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
294 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
295 GetDeviceGammaRamp(dc, ramp);
296 ReleaseDC(swapchain->device_window, dc);
298 return WINED3D_OK;
301 /* A GL context is provided by the caller */
302 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
303 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
305 struct wined3d_texture *texture = swapchain->back_buffers[0];
306 struct wined3d_surface *back_buffer = texture->sub_resources[0].u.surface;
307 struct wined3d_surface *front_buffer = swapchain->front_buffer->sub_resources[0].u.surface;
308 UINT src_w = src_rect->right - src_rect->left;
309 UINT src_h = src_rect->bottom - src_rect->top;
310 GLenum gl_filter;
311 const struct wined3d_gl_info *gl_info = context->gl_info;
312 RECT win_rect;
313 UINT win_h;
315 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
316 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
318 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
319 gl_filter = GL_NEAREST;
320 else
321 gl_filter = GL_LINEAR;
323 GetClientRect(swapchain->win_handle, &win_rect);
324 win_h = win_rect.bottom - win_rect.top;
326 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(texture->resource.format->color_fixup))
328 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
330 if (texture->resource.multisample_type)
332 location = WINED3D_LOCATION_RB_RESOLVED;
333 wined3d_texture_load_location(texture, 0, context, location);
336 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, back_buffer, NULL, location);
337 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
338 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
340 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
341 context_set_draw_buffer(context, GL_BACK);
342 context_invalidate_state(context, STATE_FRAMEBUFFER);
344 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
345 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
346 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
347 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
348 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
350 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
351 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
353 /* Note that the texture is upside down */
354 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
355 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
356 GL_COLOR_BUFFER_BIT, gl_filter);
357 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
359 else
361 struct wined3d_device *device = swapchain->device;
362 struct wined3d_context *context2;
363 float tex_left = src_rect->left;
364 float tex_top = src_rect->top;
365 float tex_right = src_rect->right;
366 float tex_bottom = src_rect->bottom;
368 context2 = context_acquire(device, back_buffer);
369 context_apply_blit_state(context2, device);
371 if (texture->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
373 tex_left /= texture->pow2_width;
374 tex_right /= texture->pow2_width;
375 tex_top /= texture->pow2_height;
376 tex_bottom /= texture->pow2_height;
379 if (is_complex_fixup(texture->resource.format->color_fixup))
380 gl_filter = GL_NEAREST;
382 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
383 context_bind_texture(context2, back_buffer->texture_target, texture->texture_rgb.name);
385 /* Set up the texture. The surface is not in a wined3d_texture
386 * container, so there are no D3D texture settings to dirtify. */
387 device->blitter->set_shader(device->blit_priv, context2, back_buffer, NULL);
388 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
389 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
391 context_set_draw_buffer(context, GL_BACK);
393 /* Set the viewport to the destination rectandle, disable any projection
394 * transformation set up by context_apply_blit_state(), and draw a
395 * (-1,-1)-(1,1) quad.
397 * Back up viewport and matrix to avoid breaking last_was_blit
399 * Note that context_apply_blit_state() set up viewport and ortho to
400 * match the surface size - we want the GL drawable(=window) size. */
401 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
402 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
403 dst_rect->right, win_h - dst_rect->top);
404 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
405 gl_info->gl_ops.gl.p_glPushMatrix();
406 gl_info->gl_ops.gl.p_glLoadIdentity();
408 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
409 /* bottom left */
410 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
411 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
413 /* top left */
414 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
415 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
417 /* top right */
418 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
419 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
421 /* bottom right */
422 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
423 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
424 gl_info->gl_ops.gl.p_glEnd();
426 gl_info->gl_ops.gl.p_glPopMatrix();
427 gl_info->gl_ops.gl.p_glPopAttrib();
429 device->blitter->unset_shader(context->gl_info);
430 checkGLcall("Swapchain present blit(manual)\n");
432 context_release(context2);
436 /* Context activation is done by the caller. */
437 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
439 struct wined3d_texture_sub_resource *sub_resource;
440 struct wined3d_texture *texture, *texture_prev;
441 struct gl_texture tex0;
442 GLuint rb0;
443 DWORD locations0;
444 unsigned int i;
445 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
447 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
448 return;
450 texture_prev = swapchain->back_buffers[0];
452 /* Back buffer 0 is already in the draw binding. */
453 tex0 = texture_prev->texture_rgb;
454 rb0 = texture_prev->rb_multisample;
455 locations0 = texture_prev->sub_resources[0].locations;
457 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
459 texture = swapchain->back_buffers[i];
460 sub_resource = &texture->sub_resources[0];
462 if (!(sub_resource->locations & supported_locations))
463 wined3d_texture_load_location(texture, 0, context, texture->resource.draw_binding);
465 texture_prev->texture_rgb = texture->texture_rgb;
466 texture_prev->rb_multisample = texture->rb_multisample;
468 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
469 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
471 texture_prev = texture;
474 texture_prev->texture_rgb = tex0;
475 texture_prev->rb_multisample = rb0;
477 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
478 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
480 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
483 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
484 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
486 struct wined3d_texture *back_buffer = swapchain->back_buffers[0];
487 const struct wined3d_fb_state *fb = &swapchain->device->fb;
488 const struct wined3d_gl_info *gl_info;
489 struct wined3d_texture *logo_texture;
490 struct wined3d_context *context;
491 BOOL render_to_fbo;
493 context = context_acquire(swapchain->device, back_buffer->sub_resources[0].u.surface);
494 if (!context->valid)
496 context_release(context);
497 WARN("Invalid context, skipping present.\n");
498 return;
501 gl_info = context->gl_info;
503 if ((logo_texture = swapchain->device->logo_texture))
505 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
507 /* Blit the logo into the upper left corner of the drawable. */
508 wined3d_texture_blt(back_buffer, 0, &rect, logo_texture, 0, &rect,
509 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
512 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
513 && !swapchain->device->hardwareCursor)
515 RECT dst_rect =
517 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
518 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
519 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
520 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
522 RECT src_rect =
524 0, 0,
525 swapchain->device->cursor_texture->resource.width,
526 swapchain->device->cursor_texture->resource.height
528 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
530 TRACE("Rendering the software cursor.\n");
532 if (swapchain->desc.windowed)
533 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
534 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
535 wined3d_texture_blt(back_buffer, 0, &dst_rect,
536 swapchain->device->cursor_texture, 0, &src_rect,
537 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
540 TRACE("Presenting HDC %p.\n", context->hdc);
542 if (!(render_to_fbo = swapchain->render_to_fbo)
543 && (src_rect->left || src_rect->top
544 || src_rect->right != swapchain->desc.backbuffer_width
545 || src_rect->bottom != swapchain->desc.backbuffer_height
546 || dst_rect->left || dst_rect->top
547 || dst_rect->right != swapchain->desc.backbuffer_width
548 || dst_rect->bottom != swapchain->desc.backbuffer_height))
549 render_to_fbo = TRUE;
551 /* Rendering to a window of different size, presenting partial rectangles,
552 * or rendering to a different window needs help from FBO_blit or a textured
553 * draw. Render the swapchain to a FBO in the future.
555 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
556 * all these issues - this fails if the window is smaller than the backbuffer.
558 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
560 wined3d_texture_load_location(back_buffer, 0, context, WINED3D_LOCATION_TEXTURE_RGB);
561 wined3d_texture_invalidate_location(back_buffer, 0, WINED3D_LOCATION_DRAWABLE);
562 swapchain->render_to_fbo = TRUE;
563 swapchain_update_draw_bindings(swapchain);
565 else
567 wined3d_texture_load_location(back_buffer, 0, context, back_buffer->resource.draw_binding);
570 if (swapchain->render_to_fbo)
572 static unsigned int once;
574 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
575 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
577 swapchain_blit(swapchain, context, src_rect, dst_rect);
580 if (swapchain->num_contexts > 1)
581 gl_info->gl_ops.gl.p_glFinish();
583 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
584 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
586 wined3d_swapchain_rotate(swapchain, context);
588 TRACE("SwapBuffers called, Starting new frame\n");
589 /* FPS support */
590 if (TRACE_ON(fps))
592 DWORD time = GetTickCount();
593 ++swapchain->frames;
595 /* every 1.5 seconds */
596 if (time - swapchain->prev_time > 1500)
598 TRACE_(fps)("%p @ approx %.2ffps\n",
599 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
600 swapchain->prev_time = time;
601 swapchain->frames = 0;
605 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
606 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
607 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
608 * and INTEXTURE copies can keep their old content if they have any defined content.
609 * If the swapeffect is COPY, the content remains the same.
611 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
612 * up to date and hope WGL flipped front and back buffers and read this data into
613 * the FBO. Don't bother about this for now. */
614 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
615 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
616 0, WINED3D_LOCATION_DISCARDED);
618 if (fb->depth_stencil)
620 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
622 if (ds && (swapchain->desc.flags & WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
623 || ds->container->flags & WINED3D_TEXTURE_DISCARD))
624 wined3d_texture_validate_location(ds->container,
625 fb->depth_stencil->sub_resource_idx, WINED3D_LOCATION_DISCARDED);
628 context_release(context);
631 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
633 struct wined3d_texture *front_buffer = swapchain->front_buffer;
634 struct wined3d_context *context;
636 context = context_acquire(swapchain->device, front_buffer->sub_resources[0].u.surface);
637 wined3d_texture_load_location(front_buffer, 0, context, front_buffer->resource.draw_binding);
638 context_release(context);
639 SetRectEmpty(&swapchain->front_buffer_update);
642 static const struct wined3d_swapchain_ops swapchain_gl_ops =
644 swapchain_gl_present,
645 swapchain_gl_frontbuffer_updated,
648 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
650 struct wined3d_surface *front;
651 POINT offset = {0, 0};
652 HDC src_dc, dst_dc;
653 RECT draw_rect;
654 HWND window;
656 TRACE("swapchain %p.\n", swapchain);
658 front = swapchain->front_buffer->sub_resources[0].u.surface;
659 if (swapchain->palette)
660 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
662 if (front->container->resource.map_count)
663 ERR("Trying to blit a mapped surface.\n");
665 TRACE("Copying surface %p to screen.\n", front);
667 src_dc = front->dc;
668 window = swapchain->win_handle;
669 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
671 /* Front buffer coordinates are screen coordinates. Map them to the
672 * destination window if not fullscreened. */
673 if (swapchain->desc.windowed)
674 ClientToScreen(window, &offset);
676 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
678 SetRect(&draw_rect, 0, 0, swapchain->front_buffer->resource.width,
679 swapchain->front_buffer->resource.height);
680 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
682 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
683 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
684 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
685 ReleaseDC(window, dst_dc);
687 SetRectEmpty(&swapchain->front_buffer_update);
690 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
691 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
693 struct wined3d_surface *front, *back;
694 HBITMAP bitmap;
695 void *data;
696 HDC dc;
698 front = swapchain->front_buffer->sub_resources[0].u.surface;
699 back = swapchain->back_buffers[0]->sub_resources[0].u.surface;
701 /* Flip the surface data. */
702 dc = front->dc;
703 bitmap = front->bitmap;
704 data = front->container->resource.heap_memory;
706 front->dc = back->dc;
707 front->bitmap = back->bitmap;
708 front->container->resource.heap_memory = back->container->resource.heap_memory;
710 back->dc = dc;
711 back->bitmap = bitmap;
712 back->container->resource.heap_memory = data;
714 /* FPS support */
715 if (TRACE_ON(fps))
717 static LONG prev_time, frames;
718 DWORD time = GetTickCount();
720 ++frames;
722 /* every 1.5 seconds */
723 if (time - prev_time > 1500)
725 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
726 prev_time = time;
727 frames = 0;
731 SetRect(&swapchain->front_buffer_update, 0, 0,
732 swapchain->front_buffer->resource.width,
733 swapchain->front_buffer->resource.height);
734 swapchain_gdi_frontbuffer_updated(swapchain);
737 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
739 swapchain_gdi_present,
740 swapchain_gdi_frontbuffer_updated,
743 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
745 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
746 return;
748 if (!swapchain->desc.backbuffer_count)
750 TRACE("Single buffered rendering.\n");
751 swapchain->render_to_fbo = FALSE;
752 return;
755 TRACE("Rendering to FBO.\n");
756 swapchain->render_to_fbo = TRUE;
759 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
760 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
762 const struct wined3d_gl_info *gl_info;
763 const struct wined3d_format *format;
764 enum wined3d_multisample_type t;
766 if (wined3d_settings.sample_count == ~0u)
767 return;
769 gl_info = &swapchain->device->adapter->gl_info;
770 if (!(format = wined3d_get_format(gl_info, format_id, WINED3DUSAGE_RENDERTARGET)))
771 return;
773 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
774 while (!(format->multisample_types & 1u << (t - 1)))
775 ++t;
776 TRACE("Using sample count %u.\n", t);
777 *type = t;
778 *quality = 0;
781 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
782 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
784 const struct wined3d_adapter *adapter = device->adapter;
785 struct wined3d_resource_desc texture_desc;
786 BOOL displaymode_set = FALSE;
787 DWORD texture_flags = 0;
788 RECT client_rect;
789 HWND window;
790 HRESULT hr;
791 UINT i;
793 if (desc->backbuffer_count > 1)
795 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
796 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
799 if (device->wined3d->flags & WINED3D_NO3D)
800 swapchain->swapchain_ops = &swapchain_gdi_ops;
801 else
802 swapchain->swapchain_ops = &swapchain_gl_ops;
804 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
806 swapchain->device = device;
807 swapchain->parent = parent;
808 swapchain->parent_ops = parent_ops;
809 swapchain->ref = 1;
810 swapchain->win_handle = window;
811 swapchain->device_window = window;
813 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
814 adapter->ordinal, &swapchain->original_mode, NULL)))
816 ERR("Failed to get current display mode, hr %#x.\n", hr);
817 goto err;
819 GetWindowRect(window, &swapchain->original_window_rect);
821 GetClientRect(window, &client_rect);
822 if (desc->windowed)
824 if (!desc->backbuffer_width)
826 desc->backbuffer_width = client_rect.right;
827 TRACE("Updating width to %u.\n", desc->backbuffer_width);
830 if (!desc->backbuffer_height)
832 desc->backbuffer_height = client_rect.bottom;
833 TRACE("Updating height to %u.\n", desc->backbuffer_height);
836 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
838 desc->backbuffer_format = swapchain->original_mode.format_id;
839 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
842 swapchain->desc = *desc;
843 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
844 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
845 swapchain_update_render_to_fbo(swapchain);
847 TRACE("Creating front buffer.\n");
849 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
850 texture_desc.format = swapchain->desc.backbuffer_format;
851 texture_desc.multisample_type = swapchain->desc.multisample_type;
852 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
853 texture_desc.usage = 0;
854 texture_desc.pool = WINED3D_POOL_DEFAULT;
855 texture_desc.width = swapchain->desc.backbuffer_width;
856 texture_desc.height = swapchain->desc.backbuffer_height;
857 texture_desc.depth = 1;
858 texture_desc.size = 0;
860 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
861 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
863 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
864 parent, &texture_desc, texture_flags, &swapchain->front_buffer)))
866 WARN("Failed to create front buffer, hr %#x.\n", hr);
867 goto err;
870 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
871 if (!(device->wined3d->flags & WINED3D_NO3D))
873 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
874 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
877 /* MSDN says we're only allowed a single fullscreen swapchain per device,
878 * so we should really check to see if there is a fullscreen swapchain
879 * already. Does a single head count as full screen? */
880 if (!desc->windowed)
882 if (desc->flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
884 /* Change the display settings */
885 swapchain->d3d_mode.width = desc->backbuffer_width;
886 swapchain->d3d_mode.height = desc->backbuffer_height;
887 swapchain->d3d_mode.format_id = desc->backbuffer_format;
888 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
889 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
891 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
892 adapter->ordinal, &swapchain->d3d_mode)))
894 WARN("Failed to set display mode, hr %#x.\n", hr);
895 goto err;
897 displaymode_set = TRUE;
899 else
901 swapchain->d3d_mode = swapchain->original_mode;
905 if (!(device->wined3d->flags & WINED3D_NO3D))
907 static const enum wined3d_format_id formats[] =
909 WINED3DFMT_D24_UNORM_S8_UINT,
910 WINED3DFMT_D32_UNORM,
911 WINED3DFMT_R24_UNORM_X8_TYPELESS,
912 WINED3DFMT_D16_UNORM,
913 WINED3DFMT_S1_UINT_D15_UNORM
916 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
918 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
919 if (!swapchain->context)
921 ERR("Failed to create the context array.\n");
922 hr = E_OUTOFMEMORY;
923 goto err;
925 swapchain->num_contexts = 1;
927 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
928 * You are able to add a depth + stencil surface at a later stage when you need it.
929 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
930 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
931 * context, need torecreate shaders, textures and other resources.
933 * The context manager already takes care of the state problem and for the other tasks code from Reset
934 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
935 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
936 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
937 * issue needs to be fixed. */
938 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
940 swapchain->ds_format = wined3d_get_format(gl_info, formats[i], WINED3DUSAGE_DEPTHSTENCIL);
941 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
942 if (swapchain->context[0]) break;
943 TRACE("Depth stencil format %s is not supported, trying next format\n",
944 debug_d3dformat(formats[i]));
947 if (!swapchain->context[0])
949 WARN("Failed to create context.\n");
950 hr = WINED3DERR_NOTAVAILABLE;
951 goto err;
954 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
955 && (!desc->enable_auto_depth_stencil
956 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
958 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
960 context_release(swapchain->context[0]);
961 swapchain_update_swap_interval(swapchain);
964 if (swapchain->desc.backbuffer_count > 0)
966 if (!(swapchain->back_buffers = wined3d_calloc(swapchain->desc.backbuffer_count,
967 sizeof(*swapchain->back_buffers))))
969 ERR("Failed to allocate backbuffer array memory.\n");
970 hr = E_OUTOFMEMORY;
971 goto err;
974 texture_desc.usage |= WINED3DUSAGE_RENDERTARGET;
975 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
977 TRACE("Creating back buffer %u.\n", i);
978 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
979 parent, &texture_desc, texture_flags, &swapchain->back_buffers[i])))
981 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
982 swapchain->desc.backbuffer_count = i;
983 goto err;
985 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
989 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
990 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
992 TRACE("Creating depth/stencil buffer.\n");
993 if (!device->auto_depth_stencil_view)
995 struct wined3d_view_desc desc;
996 struct wined3d_texture *ds;
998 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
999 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1001 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1002 device->device_parent, &texture_desc, texture_flags, &ds)))
1004 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1005 goto err;
1008 desc.format_id = ds->resource.format->id;
1009 desc.flags = 0;
1010 desc.u.texture.level_idx = 0;
1011 desc.u.texture.level_count = 1;
1012 desc.u.texture.layer_idx = 0;
1013 desc.u.texture.layer_count = 1;
1014 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1015 &device->auto_depth_stencil_view);
1016 wined3d_texture_decref(ds);
1017 if (FAILED(hr))
1019 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1020 goto err;
1025 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1027 return WINED3D_OK;
1029 err:
1030 if (displaymode_set)
1032 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1033 adapter->ordinal, &swapchain->original_mode)))
1034 ERR("Failed to restore display mode.\n");
1035 ClipCursor(NULL);
1038 if (swapchain->back_buffers)
1040 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1042 if (swapchain->back_buffers[i])
1044 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1045 wined3d_texture_decref(swapchain->back_buffers[i]);
1048 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1051 if (swapchain->context)
1053 if (swapchain->context[0])
1055 context_release(swapchain->context[0]);
1056 context_destroy(device, swapchain->context[0]);
1057 swapchain->num_contexts = 0;
1059 HeapFree(GetProcessHeap(), 0, swapchain->context);
1062 if (swapchain->front_buffer)
1064 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1065 wined3d_texture_decref(swapchain->front_buffer);
1068 return hr;
1071 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1072 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1074 struct wined3d_swapchain *object;
1075 HRESULT hr;
1077 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1078 device, desc, parent, parent_ops, swapchain);
1080 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1081 if (!object)
1082 return E_OUTOFMEMORY;
1084 hr = swapchain_init(object, device, desc, parent, parent_ops);
1085 if (FAILED(hr))
1087 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1088 HeapFree(GetProcessHeap(), 0, object);
1089 return hr;
1092 TRACE("Created swapchain %p.\n", object);
1093 *swapchain = object;
1095 return WINED3D_OK;
1098 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1100 struct wined3d_context **ctx_array;
1101 struct wined3d_context *ctx;
1103 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1105 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1107 ERR("Failed to create a new context for the swapchain\n");
1108 return NULL;
1110 context_release(ctx);
1112 if (!(ctx_array = wined3d_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1114 ERR("Out of memory when trying to allocate a new context array\n");
1115 context_destroy(swapchain->device, ctx);
1116 return NULL;
1118 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1119 HeapFree(GetProcessHeap(), 0, swapchain->context);
1120 ctx_array[swapchain->num_contexts] = ctx;
1121 swapchain->context = ctx_array;
1122 swapchain->num_contexts++;
1124 TRACE("Returning context %p\n", ctx);
1125 return ctx;
1128 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1130 unsigned int i;
1132 for (i = 0; i < swapchain->num_contexts; ++i)
1134 context_destroy(swapchain->device, swapchain->context[i]);
1136 swapchain->num_contexts = 0;
1139 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1141 DWORD tid = GetCurrentThreadId();
1142 unsigned int i;
1144 for (i = 0; i < swapchain->num_contexts; ++i)
1146 if (swapchain->context[i]->tid == tid)
1147 return swapchain->context[i];
1150 /* Create a new context for the thread */
1151 return swapchain_create_context(swapchain);
1154 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1156 if (!swapchain->backup_dc)
1158 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1160 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1161 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1163 ERR("Failed to create a window.\n");
1164 return NULL;
1167 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1169 ERR("Failed to get a DC.\n");
1170 DestroyWindow(swapchain->backup_wnd);
1171 swapchain->backup_wnd = NULL;
1172 return NULL;
1176 return swapchain->backup_dc;
1179 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1181 UINT i;
1183 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1185 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1187 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1191 void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1193 const struct wined3d_gl_info *gl_info;
1194 struct wined3d_context *context;
1195 int swap_interval;
1197 context = context_acquire(swapchain->device, swapchain->front_buffer->sub_resources[0].u.surface);
1198 gl_info = context->gl_info;
1200 switch (swapchain->desc.swap_interval)
1202 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1203 swap_interval = 0;
1204 break;
1205 case WINED3DPRESENT_INTERVAL_DEFAULT:
1206 case WINED3DPRESENT_INTERVAL_ONE:
1207 swap_interval = 1;
1208 break;
1209 case WINED3DPRESENT_INTERVAL_TWO:
1210 swap_interval = 2;
1211 break;
1212 case WINED3DPRESENT_INTERVAL_THREE:
1213 swap_interval = 3;
1214 break;
1215 case WINED3DPRESENT_INTERVAL_FOUR:
1216 swap_interval = 4;
1217 break;
1218 default:
1219 FIXME("Unhandled present interval %#x.\n", swapchain->desc.swap_interval);
1220 swap_interval = 1;
1223 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1225 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1226 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1227 swap_interval, context, GetLastError());
1230 context_release(context);
1233 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1235 struct wined3d_device *device = swapchain->device;
1236 BOOL filter_messages = device->filter_messages;
1238 /* This code is not protected by the wined3d mutex, so it may run while
1239 * wined3d_device_reset is active. Testing on Windows shows that changing
1240 * focus during resets and resetting during focus change events causes
1241 * the application to crash with an invalid memory access. */
1243 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1245 if (activate)
1247 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1249 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1250 * the window but leaves the size untouched, d3d9 sets the size on an
1251 * invisible window, generates messages but doesn't change the window
1252 * properties. The implementation follows d3d9.
1254 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1255 * resume drawing after a focus loss. */
1256 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1257 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1258 SWP_NOACTIVATE | SWP_NOZORDER);
1261 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1263 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1264 device->adapter->ordinal, &swapchain->d3d_mode)))
1265 ERR("Failed to set display mode.\n");
1268 else
1270 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1271 device->adapter->ordinal, NULL)))
1272 ERR("Failed to set display mode.\n");
1274 swapchain->reapply_mode = TRUE;
1276 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1277 && IsWindowVisible(swapchain->device_window))
1278 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1281 device->filter_messages = filter_messages;
1284 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1285 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1286 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1288 BOOL update_desc = FALSE;
1290 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1291 "multisample_type %#x, multisample_quality %#x.\n",
1292 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1293 multisample_type, multisample_quality);
1295 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1297 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1298 FIXME("Cannot change the back buffer count yet.\n");
1300 if (!width || !height)
1302 /* The application is requesting that either the swapchain width or
1303 * height be set to the corresponding dimension in the window's
1304 * client rect. */
1306 RECT client_rect;
1308 if (!swapchain->desc.windowed)
1309 return WINED3DERR_INVALIDCALL;
1311 if (!GetClientRect(swapchain->device_window, &client_rect))
1313 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1314 return WINED3DERR_INVALIDCALL;
1317 if (!width)
1318 width = client_rect.right;
1320 if (!height)
1321 height = client_rect.bottom;
1324 if (width != swapchain->desc.backbuffer_width
1325 || height != swapchain->desc.backbuffer_height)
1327 swapchain->desc.backbuffer_width = width;
1328 swapchain->desc.backbuffer_height = height;
1329 update_desc = TRUE;
1332 if (format_id == WINED3DFMT_UNKNOWN)
1334 if (!swapchain->desc.windowed)
1335 return WINED3DERR_INVALIDCALL;
1336 format_id = swapchain->original_mode.format_id;
1339 if (format_id != swapchain->desc.backbuffer_format)
1341 swapchain->desc.backbuffer_format = format_id;
1342 update_desc = TRUE;
1345 if (multisample_type != swapchain->desc.multisample_type
1346 || multisample_quality != swapchain->desc.multisample_quality)
1348 swapchain->desc.multisample_type = multisample_type;
1349 swapchain->desc.multisample_quality = multisample_quality;
1350 update_desc = TRUE;
1353 if (update_desc)
1355 HRESULT hr;
1356 UINT i;
1358 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1359 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1360 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1361 return hr;
1363 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1365 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1366 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1367 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1368 return hr;
1372 swapchain_update_render_to_fbo(swapchain);
1373 swapchain_update_draw_bindings(swapchain);
1375 return WINED3D_OK;
1378 static HRESULT wined3d_swapchain_set_display_mode(struct wined3d_swapchain *swapchain,
1379 struct wined3d_display_mode *mode)
1381 struct wined3d_device *device = swapchain->device;
1382 HRESULT hr;
1384 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE)
1386 if (FAILED(hr = wined3d_find_closest_matching_adapter_mode(device->wined3d,
1387 device->adapter->ordinal, mode)))
1389 WARN("Failed to find closest matching mode, hr %#x.\n", hr);
1393 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d,
1394 device->adapter->ordinal, mode)))
1396 WARN("Failed to set display mode, hr %#x.\n", hr);
1397 return WINED3DERR_INVALIDCALL;
1400 return WINED3D_OK;
1403 HRESULT CDECL wined3d_swapchain_resize_target(struct wined3d_swapchain *swapchain,
1404 const struct wined3d_display_mode *mode)
1406 struct wined3d_device *device = swapchain->device;
1407 struct wined3d_display_mode actual_mode;
1408 RECT original_window_rect, window_rect;
1409 HRESULT hr;
1411 TRACE("swapchain %p, mode %p.\n", swapchain, mode);
1413 if (swapchain->desc.windowed)
1415 SetRect(&window_rect, 0, 0, mode->width, mode->height);
1416 AdjustWindowRectEx(&window_rect,
1417 GetWindowLongW(swapchain->device_window, GWL_STYLE), FALSE,
1418 GetWindowLongW(swapchain->device_window, GWL_EXSTYLE));
1419 SetRect(&window_rect, 0, 0,
1420 window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
1421 GetWindowRect(swapchain->device_window, &original_window_rect);
1422 OffsetRect(&window_rect, original_window_rect.left, original_window_rect.top);
1424 else if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1426 actual_mode = *mode;
1427 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1428 return hr;
1429 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1431 else
1433 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1434 &actual_mode, NULL)))
1436 ERR("Failed to get display mode, hr %#x.\n", hr);
1437 return WINED3DERR_INVALIDCALL;
1440 SetRect(&window_rect, 0, 0, actual_mode.width, actual_mode.height);
1443 MoveWindow(swapchain->device_window, window_rect.left, window_rect.top,
1444 window_rect.right - window_rect.left,
1445 window_rect.bottom - window_rect.top, TRUE);
1447 return WINED3D_OK;
1450 HRESULT CDECL wined3d_swapchain_set_fullscreen(struct wined3d_swapchain *swapchain,
1451 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode)
1453 struct wined3d_device *device = swapchain->device;
1454 struct wined3d_display_mode actual_mode;
1455 HRESULT hr;
1457 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain, swapchain_desc, mode);
1459 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH)
1461 if (mode)
1463 actual_mode = *mode;
1465 else
1467 if (!swapchain_desc->windowed)
1469 actual_mode.width = swapchain_desc->backbuffer_width;
1470 actual_mode.height = swapchain_desc->backbuffer_height;
1471 actual_mode.refresh_rate = swapchain_desc->refresh_rate;
1472 actual_mode.format_id = swapchain_desc->backbuffer_format;
1473 actual_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1475 else
1477 actual_mode = swapchain->original_mode;
1481 if (FAILED(hr = wined3d_swapchain_set_display_mode(swapchain, &actual_mode)))
1482 return hr;
1484 else
1486 if (mode)
1487 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1489 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal,
1490 &actual_mode, NULL)))
1492 ERR("Failed to get display mode, hr %#x.\n", hr);
1493 return WINED3DERR_INVALIDCALL;
1497 if (!swapchain_desc->windowed)
1499 unsigned int width = actual_mode.width;
1500 unsigned int height = actual_mode.height;
1502 if (swapchain->desc.windowed)
1504 /* Switch from windowed to fullscreen */
1505 HWND focus_window = device->create_parms.focus_window;
1506 if (!focus_window)
1507 focus_window = swapchain->device_window;
1508 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
1510 ERR("Failed to acquire focus window, hr %#x.\n", hr);
1511 return hr;
1514 wined3d_device_setup_fullscreen_window(device, swapchain->device_window, width, height);
1516 else
1518 /* Fullscreen -> fullscreen mode change */
1519 BOOL filter_messages = device->filter_messages;
1520 device->filter_messages = TRUE;
1522 MoveWindow(swapchain->device_window, 0, 0, width, height, TRUE);
1524 device->filter_messages = filter_messages;
1526 swapchain->d3d_mode = actual_mode;
1528 else if (!swapchain->desc.windowed)
1530 /* Fullscreen -> windowed switch */
1531 RECT *window_rect = NULL;
1532 if (swapchain->desc.flags & WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT)
1533 window_rect = &swapchain->original_window_rect;
1534 wined3d_device_restore_fullscreen_window(device, swapchain->device_window, window_rect);
1535 wined3d_device_release_focus_window(device);
1538 swapchain->desc.windowed = swapchain_desc->windowed;
1540 return WINED3D_OK;