d3d9: Validate swap effect and backbuffer count.
[wine.git] / dlls / wined3d / swapchain.c
blob70f5d5c1247f1af1b0ba40bba25e5eb879da13db
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
30 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 HRESULT hr;
33 UINT i;
35 TRACE("Destroying swapchain %p.\n", swapchain);
37 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain->front_buffer)
43 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
44 if (wined3d_texture_decref(swapchain->front_buffer))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
46 swapchain->front_buffer = NULL;
49 if (swapchain->back_buffers)
51 i = swapchain->desc.backbuffer_count;
53 while (i--)
55 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
56 if (wined3d_texture_decref(swapchain->back_buffers[i]))
57 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
59 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
60 swapchain->back_buffers = NULL;
63 for (i = 0; i < swapchain->num_contexts; ++i)
65 context_destroy(swapchain->device, swapchain->context[i]);
67 HeapFree(GetProcessHeap(), 0, swapchain->context);
69 /* Restore the screen resolution if we rendered in fullscreen.
70 * This will restore the screen resolution to what it was before creating
71 * the swapchain. In case of d3d8 and d3d9 this will be the original
72 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
73 * sets the resolution before starting up Direct3D, thus orig_width and
74 * orig_height will be equal to the modes in the presentation params. */
75 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
77 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
78 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
79 ERR("Failed to restore display mode, hr %#x.\n", hr);
82 if (swapchain->backup_dc)
84 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
86 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
87 DestroyWindow(swapchain->backup_wnd);
91 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
93 ULONG refcount = InterlockedIncrement(&swapchain->ref);
95 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
97 return refcount;
100 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedDecrement(&swapchain->ref);
104 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
106 if (!refcount)
108 swapchain_cleanup(swapchain);
109 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
110 HeapFree(GetProcessHeap(), 0, swapchain);
113 return refcount;
116 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
118 TRACE("swapchain %p.\n", swapchain);
120 return swapchain->parent;
123 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
125 if (!window)
126 window = swapchain->device_window;
127 if (window == swapchain->win_handle)
128 return;
130 TRACE("Setting swapchain %p window from %p to %p.\n",
131 swapchain, swapchain->win_handle, window);
132 swapchain->win_handle = window;
135 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
136 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
137 const RGNDATA *dirty_region, DWORD flags)
139 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
140 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
141 dst_window_override, dirty_region, flags);
143 if (flags)
144 FIXME("Ignoring flags %#x.\n", flags);
146 if (!swapchain->back_buffers)
148 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
149 return WINED3DERR_INVALIDCALL;
152 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
153 dst_rect, dst_window_override, dirty_region, flags);
155 return WINED3D_OK;
158 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
159 struct wined3d_surface *dst_surface)
161 struct wined3d_surface *src_surface;
162 RECT src_rect, dst_rect;
164 TRACE("swapchain %p, dst_surface %p.\n", swapchain, dst_surface);
166 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
167 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
168 dst_rect = src_rect;
170 if (swapchain->desc.windowed)
172 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
173 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
174 wine_dbgstr_rect(&dst_rect));
177 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
180 struct wined3d_surface * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
181 UINT back_buffer_idx, enum wined3d_backbuffer_type type)
183 TRACE("swapchain %p, back_buffer_idx %u, type %#x.\n",
184 swapchain, back_buffer_idx, type);
186 /* Return invalid if there is no backbuffer array, otherwise it will
187 * crash when ddraw is used (there swapchain->back_buffers is always
188 * NULL). We need this because this function is called from
189 * stateblock_init_default_state() to get the default scissorrect
190 * dimensions. */
191 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
193 WARN("Invalid back buffer index.\n");
194 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
195 * here in wined3d to avoid problems in other libs. */
196 return NULL;
199 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
201 return surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[back_buffer_idx], 0));
204 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
205 struct wined3d_raster_status *raster_status)
207 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
209 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
210 swapchain->device->adapter->ordinal, raster_status);
213 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
214 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
216 HRESULT hr;
218 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
220 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
221 swapchain->device->adapter->ordinal, mode, rotation);
223 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
224 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
226 return hr;
229 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
231 TRACE("swapchain %p.\n", swapchain);
233 return swapchain->device;
236 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
237 struct wined3d_swapchain_desc *desc)
239 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
241 *desc = swapchain->desc;
244 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
245 DWORD flags, const struct wined3d_gamma_ramp *ramp)
247 HDC dc;
249 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
251 if (flags)
252 FIXME("Ignoring flags %#x.\n", flags);
254 dc = GetDC(swapchain->device_window);
255 SetDeviceGammaRamp(dc, (void *)ramp);
256 ReleaseDC(swapchain->device_window, dc);
258 return WINED3D_OK;
261 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
263 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
264 swapchain->palette = palette;
267 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
268 struct wined3d_gamma_ramp *ramp)
270 HDC dc;
272 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
274 dc = GetDC(swapchain->device_window);
275 GetDeviceGammaRamp(dc, ramp);
276 ReleaseDC(swapchain->device_window, dc);
278 return WINED3D_OK;
281 /* A GL context is provided by the caller */
282 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
283 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
285 struct wined3d_surface *backbuffer = surface_from_resource(
286 wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
287 UINT src_w = src_rect->right - src_rect->left;
288 UINT src_h = src_rect->bottom - src_rect->top;
289 GLenum gl_filter;
290 const struct wined3d_gl_info *gl_info = context->gl_info;
291 RECT win_rect;
292 UINT win_h;
294 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
295 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
297 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
298 gl_filter = GL_NEAREST;
299 else
300 gl_filter = GL_LINEAR;
302 GetClientRect(swapchain->win_handle, &win_rect);
303 win_h = win_rect.bottom - win_rect.top;
305 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
307 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
309 if (backbuffer->resource.multisample_type)
311 location = WINED3D_LOCATION_RB_RESOLVED;
312 surface_load_location(backbuffer, location);
315 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
316 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
317 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
319 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
320 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
321 NULL, WINED3D_LOCATION_DRAWABLE);
322 context_set_draw_buffer(context, GL_BACK);
323 context_invalidate_state(context, STATE_FRAMEBUFFER);
325 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
327 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
328 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
329 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
331 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
332 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
334 /* Note that the texture is upside down */
335 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
336 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
337 GL_COLOR_BUFFER_BIT, gl_filter);
338 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
340 else
342 struct wined3d_device *device = swapchain->device;
343 struct wined3d_context *context2;
344 float tex_left = src_rect->left;
345 float tex_top = src_rect->top;
346 float tex_right = src_rect->right;
347 float tex_bottom = src_rect->bottom;
349 context2 = context_acquire(device, backbuffer);
350 context_apply_blit_state(context2, device);
352 if (backbuffer->container->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
354 tex_left /= backbuffer->pow2Width;
355 tex_right /= backbuffer->pow2Width;
356 tex_top /= backbuffer->pow2Height;
357 tex_bottom /= backbuffer->pow2Height;
360 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
361 gl_filter = GL_NEAREST;
363 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER,
364 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
365 NULL, WINED3D_LOCATION_DRAWABLE);
366 context_bind_texture(context2, backbuffer->texture_target, backbuffer->container->texture_rgb.name);
368 /* Set up the texture. The surface is not in a wined3d_texture
369 * container, so there are no D3D texture settings to dirtify. */
370 device->blitter->set_shader(device->blit_priv, context2, backbuffer, NULL);
371 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
372 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
374 context_set_draw_buffer(context, GL_BACK);
376 /* Set the viewport to the destination rectandle, disable any projection
377 * transformation set up by context_apply_blit_state(), and draw a
378 * (-1,-1)-(1,1) quad.
380 * Back up viewport and matrix to avoid breaking last_was_blit
382 * Note that context_apply_blit_state() set up viewport and ortho to
383 * match the surface size - we want the GL drawable(=window) size. */
384 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
385 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
386 dst_rect->right, win_h - dst_rect->top);
387 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
388 gl_info->gl_ops.gl.p_glPushMatrix();
389 gl_info->gl_ops.gl.p_glLoadIdentity();
391 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
392 /* bottom left */
393 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
394 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
396 /* top left */
397 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
398 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
400 /* top right */
401 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
402 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
404 /* bottom right */
405 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
406 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
407 gl_info->gl_ops.gl.p_glEnd();
409 gl_info->gl_ops.gl.p_glPopMatrix();
410 gl_info->gl_ops.gl.p_glPopAttrib();
412 device->blitter->unset_shader(context->gl_info);
413 checkGLcall("Swapchain present blit(manual)\n");
415 context_release(context2);
419 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
420 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
422 struct wined3d_surface *back_buffer = surface_from_resource(
423 wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
424 const struct wined3d_fb_state *fb = &swapchain->device->fb;
425 const struct wined3d_gl_info *gl_info;
426 struct wined3d_context *context;
427 struct wined3d_surface *front;
428 RECT src_rect, dst_rect;
429 BOOL render_to_fbo;
431 context = context_acquire(swapchain->device, back_buffer);
432 if (!context->valid)
434 context_release(context);
435 WARN("Invalid context, skipping present.\n");
436 return;
439 gl_info = context->gl_info;
441 if (swapchain->device->logo_texture)
443 struct wined3d_surface *src_surface = surface_from_resource(
444 wined3d_texture_get_sub_resource(swapchain->device->logo_texture, 0));
445 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
447 /* Blit the logo into the upper left corner of the drawable. */
448 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_ALPHATEST,
449 NULL, WINED3D_TEXF_POINT);
452 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
453 && !swapchain->device->hardwareCursor)
455 struct wined3d_surface *cursor = surface_from_resource(
456 wined3d_texture_get_sub_resource(swapchain->device->cursor_texture, 0));
457 RECT destRect =
459 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
460 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
461 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
462 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
465 TRACE("Rendering the software cursor.\n");
467 if (swapchain->desc.windowed)
468 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
469 wined3d_surface_blt(back_buffer, &destRect, cursor, NULL, WINEDDBLT_ALPHATEST,
470 NULL, WINED3D_TEXF_POINT);
473 TRACE("Presenting HDC %p.\n", context->hdc);
475 render_to_fbo = swapchain->render_to_fbo;
477 if (src_rect_in)
479 src_rect = *src_rect_in;
480 if (!render_to_fbo && (src_rect.left || src_rect.top
481 || src_rect.right != swapchain->desc.backbuffer_width
482 || src_rect.bottom != swapchain->desc.backbuffer_height))
484 render_to_fbo = TRUE;
487 else
489 src_rect.left = 0;
490 src_rect.top = 0;
491 src_rect.right = swapchain->desc.backbuffer_width;
492 src_rect.bottom = swapchain->desc.backbuffer_height;
495 if (dst_rect_in)
496 dst_rect = *dst_rect_in;
497 else
498 GetClientRect(swapchain->win_handle, &dst_rect);
500 if (!render_to_fbo && (dst_rect.left || dst_rect.top
501 || dst_rect.right != swapchain->desc.backbuffer_width
502 || dst_rect.bottom != swapchain->desc.backbuffer_height))
503 render_to_fbo = TRUE;
505 /* Rendering to a window of different size, presenting partial rectangles,
506 * or rendering to a different window needs help from FBO_blit or a textured
507 * draw. Render the swapchain to a FBO in the future.
509 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
510 * all these issues - this fails if the window is smaller than the backbuffer.
512 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
514 surface_load_location(back_buffer, WINED3D_LOCATION_TEXTURE_RGB);
515 surface_invalidate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
516 swapchain->render_to_fbo = TRUE;
517 swapchain_update_draw_bindings(swapchain);
519 else
521 surface_load_location(back_buffer, back_buffer->container->resource.draw_binding);
524 if (swapchain->render_to_fbo)
526 static unsigned int once;
528 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
529 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
531 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
534 if (swapchain->num_contexts > 1)
535 gl_info->gl_ops.gl.p_glFinish();
537 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
538 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
540 TRACE("SwapBuffers called, Starting new frame\n");
541 /* FPS support */
542 if (TRACE_ON(fps))
544 DWORD time = GetTickCount();
545 ++swapchain->frames;
547 /* every 1.5 seconds */
548 if (time - swapchain->prev_time > 1500)
550 TRACE_(fps)("%p @ approx %.2ffps\n",
551 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
552 swapchain->prev_time = time;
553 swapchain->frames = 0;
557 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
558 if (!swapchain->render_to_fbo && ((front->locations & WINED3D_LOCATION_SYSMEM)
559 || (back_buffer->locations & WINED3D_LOCATION_SYSMEM)))
561 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
562 * Doesn't work with render_to_fbo because we're not flipping
565 if (front->resource.size == back_buffer->resource.size)
567 flip_surface(front, back_buffer);
569 /* Tell the front buffer surface that is has been modified. However,
570 * the other locations were preserved during that, so keep the flags.
571 * This serves to update the emulated overlay, if any. */
572 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
574 else
576 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
577 surface_invalidate_location(front, ~WINED3D_LOCATION_DRAWABLE);
578 surface_validate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
579 surface_invalidate_location(back_buffer, ~WINED3D_LOCATION_DRAWABLE);
582 else
584 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
585 surface_invalidate_location(front, ~WINED3D_LOCATION_DRAWABLE);
586 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
587 * and INTEXTURE copies can keep their old content if they have any defined content.
588 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
589 * the texture / sysmem copy needs to be reloaded from the drawable
591 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP)
593 surface_validate_location(back_buffer, back_buffer->container->resource.draw_binding);
594 surface_invalidate_location(back_buffer, ~back_buffer->container->resource.draw_binding);
598 if (fb->depth_stencil)
600 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
602 if (ds && (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
603 || ds->flags & SFLAG_DISCARD))
605 surface_modify_ds_location(ds, WINED3D_LOCATION_DISCARDED,
606 fb->depth_stencil->width, fb->depth_stencil->height);
607 if (ds == swapchain->device->onscreen_depth_stencil)
609 wined3d_surface_decref(swapchain->device->onscreen_depth_stencil);
610 swapchain->device->onscreen_depth_stencil = NULL;
615 context_release(context);
618 static const struct wined3d_swapchain_ops swapchain_gl_ops =
620 swapchain_gl_present,
623 /* Helper function that blits the front buffer contents to the target window. */
624 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
626 struct wined3d_surface *front;
627 POINT offset = {0, 0};
628 HDC src_dc, dst_dc;
629 RECT draw_rect;
630 HWND window;
632 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
634 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
635 if (swapchain->palette)
636 wined3d_palette_apply_to_dc(swapchain->palette, front->hDC);
638 if (front->resource.map_count)
639 ERR("Trying to blit a mapped surface.\n");
641 TRACE("Copying surface %p to screen.\n", front);
643 surface_load_location(front, WINED3D_LOCATION_DIB);
645 src_dc = front->hDC;
646 window = swapchain->win_handle;
647 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
649 /* Front buffer coordinates are screen coordinates. Map them to the
650 * destination window if not fullscreened. */
651 if (swapchain->desc.windowed)
652 ClientToScreen(window, &offset);
654 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
656 draw_rect.left = 0;
657 draw_rect.right = front->resource.width;
658 draw_rect.top = 0;
659 draw_rect.bottom = front->resource.height;
661 if (rect)
662 IntersectRect(&draw_rect, &draw_rect, rect);
664 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
665 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
666 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
667 ReleaseDC(window, dst_dc);
670 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
671 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
673 struct wined3d_surface *front, *back;
675 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
676 back = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
678 /* Flip the DC. */
680 HDC tmp;
681 tmp = front->hDC;
682 front->hDC = back->hDC;
683 back->hDC = tmp;
686 /* Flip the DIBsection. */
688 HBITMAP tmp;
689 tmp = front->dib.DIBsection;
690 front->dib.DIBsection = back->dib.DIBsection;
691 back->dib.DIBsection = tmp;
694 /* Flip the surface data. */
696 void *tmp;
698 tmp = front->dib.bitmap_data;
699 front->dib.bitmap_data = back->dib.bitmap_data;
700 back->dib.bitmap_data = tmp;
702 if (front->resource.heap_memory)
703 ERR("GDI Surface %p has heap memory allocated.\n", front);
705 if (back->resource.heap_memory)
706 ERR("GDI Surface %p has heap memory allocated.\n", back);
709 /* FPS support */
710 if (TRACE_ON(fps))
712 static LONG prev_time, frames;
713 DWORD time = GetTickCount();
715 ++frames;
717 /* every 1.5 seconds */
718 if (time - prev_time > 1500)
720 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
721 prev_time = time;
722 frames = 0;
726 x11_copy_to_screen(swapchain, NULL);
729 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
731 swapchain_gdi_present,
734 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
736 RECT client_rect;
738 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
739 return;
741 if (!swapchain->desc.backbuffer_count)
743 TRACE("Single buffered rendering.\n");
744 swapchain->render_to_fbo = FALSE;
745 return;
748 GetClientRect(swapchain->win_handle, &client_rect);
750 TRACE("Backbuffer %ux%u, window %ux%u.\n",
751 swapchain->desc.backbuffer_width,
752 swapchain->desc.backbuffer_height,
753 client_rect.right, client_rect.bottom);
754 TRACE("Multisample type %#x, quality %#x.\n",
755 swapchain->desc.multisample_type,
756 swapchain->desc.multisample_quality);
758 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
759 && swapchain->desc.backbuffer_width == client_rect.right
760 && swapchain->desc.backbuffer_height == client_rect.bottom)
762 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
763 swapchain->render_to_fbo = FALSE;
764 return;
767 TRACE("Rendering to FBO.\n");
768 swapchain->render_to_fbo = TRUE;
771 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
772 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
774 const struct wined3d_adapter *adapter = device->adapter;
775 struct wined3d_resource_desc surface_desc;
776 struct wined3d_surface *front_buffer;
777 BOOL displaymode_set = FALSE;
778 RECT client_rect;
779 HWND window;
780 HRESULT hr;
781 UINT i;
783 if (desc->backbuffer_count > 1)
785 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
786 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
789 if (device->wined3d->flags & WINED3D_NO3D)
790 swapchain->swapchain_ops = &swapchain_gdi_ops;
791 else
792 swapchain->swapchain_ops = &swapchain_gl_ops;
794 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
796 swapchain->device = device;
797 swapchain->parent = parent;
798 swapchain->parent_ops = parent_ops;
799 swapchain->ref = 1;
800 swapchain->win_handle = window;
801 swapchain->device_window = window;
803 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
804 adapter->ordinal, &swapchain->original_mode, NULL)))
806 ERR("Failed to get current display mode, hr %#x.\n", hr);
807 goto err;
810 GetClientRect(window, &client_rect);
811 if (desc->windowed
812 && (!desc->backbuffer_width || !desc->backbuffer_height
813 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
816 if (!desc->backbuffer_width)
818 desc->backbuffer_width = client_rect.right;
819 TRACE("Updating width to %u.\n", desc->backbuffer_width);
822 if (!desc->backbuffer_height)
824 desc->backbuffer_height = client_rect.bottom;
825 TRACE("Updating height to %u.\n", desc->backbuffer_height);
828 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
830 desc->backbuffer_format = swapchain->original_mode.format_id;
831 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
834 swapchain->desc = *desc;
835 swapchain_update_render_to_fbo(swapchain);
837 TRACE("Creating front buffer.\n");
839 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
840 surface_desc.format = swapchain->desc.backbuffer_format;
841 surface_desc.multisample_type = swapchain->desc.multisample_type;
842 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
843 surface_desc.usage = 0;
844 surface_desc.pool = WINED3D_POOL_DEFAULT;
845 surface_desc.width = swapchain->desc.backbuffer_width;
846 surface_desc.height = swapchain->desc.backbuffer_height;
847 surface_desc.depth = 1;
848 surface_desc.size = 0;
850 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
851 parent, &surface_desc, &front_buffer)))
853 WARN("Failed to create front buffer, hr %#x.\n", hr);
854 goto err;
857 swapchain->front_buffer = front_buffer->container;
858 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
859 if (!(device->wined3d->flags & WINED3D_NO3D))
861 surface_validate_location(front_buffer, WINED3D_LOCATION_DRAWABLE);
862 surface_invalidate_location(front_buffer, ~WINED3D_LOCATION_DRAWABLE);
865 /* MSDN says we're only allowed a single fullscreen swapchain per device,
866 * so we should really check to see if there is a fullscreen swapchain
867 * already. Does a single head count as full screen? */
869 if (!desc->windowed)
871 /* Change the display settings */
872 swapchain->d3d_mode.width = desc->backbuffer_width;
873 swapchain->d3d_mode.height = desc->backbuffer_height;
874 swapchain->d3d_mode.format_id = desc->backbuffer_format;
875 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
876 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
878 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &swapchain->d3d_mode)))
880 WARN("Failed to set display mode, hr %#x.\n", hr);
881 goto err;
883 displaymode_set = TRUE;
886 if (!(device->wined3d->flags & WINED3D_NO3D))
888 static const enum wined3d_format_id formats[] =
890 WINED3DFMT_D24_UNORM_S8_UINT,
891 WINED3DFMT_D32_UNORM,
892 WINED3DFMT_R24_UNORM_X8_TYPELESS,
893 WINED3DFMT_D16_UNORM,
894 WINED3DFMT_S1_UINT_D15_UNORM
897 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
899 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
900 if (!swapchain->context)
902 ERR("Failed to create the context array.\n");
903 hr = E_OUTOFMEMORY;
904 goto err;
906 swapchain->num_contexts = 1;
908 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
909 * You are able to add a depth + stencil surface at a later stage when you need it.
910 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
911 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
912 * context, need torecreate shaders, textures and other resources.
914 * The context manager already takes care of the state problem and for the other tasks code from Reset
915 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
916 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
917 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
918 * issue needs to be fixed. */
919 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
921 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
922 swapchain->context[0] = context_create(swapchain, front_buffer, swapchain->ds_format);
923 if (swapchain->context[0]) break;
924 TRACE("Depth stencil format %s is not supported, trying next format\n",
925 debug_d3dformat(formats[i]));
928 if (!swapchain->context[0])
930 WARN("Failed to create context.\n");
931 hr = WINED3DERR_NOTAVAILABLE;
932 goto err;
935 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
936 && (!desc->enable_auto_depth_stencil
937 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
939 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
941 context_release(swapchain->context[0]);
944 if (swapchain->desc.backbuffer_count > 0)
946 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
947 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
948 if (!swapchain->back_buffers)
950 ERR("Failed to allocate backbuffer array memory.\n");
951 hr = E_OUTOFMEMORY;
952 goto err;
955 surface_desc.usage |= WINED3DUSAGE_RENDERTARGET;
956 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
958 struct wined3d_surface *back_buffer;
960 TRACE("Creating back buffer %u.\n", i);
961 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
962 parent, &surface_desc, &back_buffer)))
964 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
965 swapchain->desc.backbuffer_count = i;
966 goto err;
968 swapchain->back_buffers[i] = back_buffer->container;
969 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
973 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
974 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
976 TRACE("Creating depth/stencil buffer.\n");
977 if (!device->auto_depth_stencil_view)
979 struct wined3d_surface *ds;
981 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
982 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
984 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
985 device->device_parent, &surface_desc, &ds)))
987 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
988 goto err;
991 hr = wined3d_rendertarget_view_create_from_surface(ds,
992 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
993 wined3d_surface_decref(ds);
994 if (FAILED(hr))
996 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
997 goto err;
1002 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1004 return WINED3D_OK;
1006 err:
1007 if (displaymode_set)
1009 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1010 adapter->ordinal, &swapchain->original_mode)))
1011 ERR("Failed to restore display mode.\n");
1012 ClipCursor(NULL);
1015 if (swapchain->back_buffers)
1017 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1019 if (swapchain->back_buffers[i])
1021 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1022 wined3d_texture_decref(swapchain->back_buffers[i]);
1025 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1028 if (swapchain->context)
1030 if (swapchain->context[0])
1032 context_release(swapchain->context[0]);
1033 context_destroy(device, swapchain->context[0]);
1034 swapchain->num_contexts = 0;
1036 HeapFree(GetProcessHeap(), 0, swapchain->context);
1039 if (swapchain->front_buffer)
1041 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1042 wined3d_texture_decref(swapchain->front_buffer);
1045 return hr;
1048 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1049 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1051 struct wined3d_swapchain *object;
1052 HRESULT hr;
1054 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1055 device, desc, parent, parent_ops, swapchain);
1057 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1058 if (!object)
1059 return E_OUTOFMEMORY;
1061 hr = swapchain_init(object, device, desc, parent, parent_ops);
1062 if (FAILED(hr))
1064 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1065 HeapFree(GetProcessHeap(), 0, object);
1066 return hr;
1069 TRACE("Created swapchain %p.\n", object);
1070 *swapchain = object;
1072 return WINED3D_OK;
1075 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1077 struct wined3d_context **newArray;
1078 struct wined3d_context *ctx;
1080 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1082 if (!(ctx = context_create(swapchain,
1083 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
1084 swapchain->ds_format)))
1086 ERR("Failed to create a new context for the swapchain\n");
1087 return NULL;
1089 context_release(ctx);
1091 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1092 if(!newArray) {
1093 ERR("Out of memory when trying to allocate a new context array\n");
1094 context_destroy(swapchain->device, ctx);
1095 return NULL;
1097 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1098 HeapFree(GetProcessHeap(), 0, swapchain->context);
1099 newArray[swapchain->num_contexts] = ctx;
1100 swapchain->context = newArray;
1101 swapchain->num_contexts++;
1103 TRACE("Returning context %p\n", ctx);
1104 return ctx;
1107 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1109 unsigned int i;
1111 for (i = 0; i < swapchain->num_contexts; ++i)
1113 context_destroy(swapchain->device, swapchain->context[i]);
1115 swapchain->num_contexts = 0;
1118 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1120 DWORD tid = GetCurrentThreadId();
1121 unsigned int i;
1123 for (i = 0; i < swapchain->num_contexts; ++i)
1125 if (swapchain->context[i]->tid == tid)
1126 return swapchain->context[i];
1129 /* Create a new context for the thread */
1130 return swapchain_create_context(swapchain);
1133 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1135 if (!swapchain->backup_dc)
1137 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1139 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1140 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1142 ERR("Failed to create a window.\n");
1143 return NULL;
1146 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1148 ERR("Failed to get a DC.\n");
1149 DestroyWindow(swapchain->backup_wnd);
1150 swapchain->backup_wnd = NULL;
1151 return NULL;
1155 return swapchain->backup_dc;
1158 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1160 UINT i;
1162 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1164 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1166 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1170 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1172 struct wined3d_device *device = swapchain->device;
1173 BOOL filter_messages = device->filter_messages;
1175 /* This code is not protected by the wined3d mutex, so it may run while
1176 * wined3d_device_reset is active. Testing on Windows shows that changing
1177 * focus during resets and resetting during focus change events causes
1178 * the application to crash with an invalid memory access. */
1180 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1182 if (activate)
1184 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1186 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1187 * the window but leaves the size untouched, d3d9 sets the size on an
1188 * invisible window, generates messages but doesn't change the window
1189 * properties. The implementation follows d3d9.
1191 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1192 * resume drawing after a focus loss. */
1193 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1194 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1195 SWP_NOACTIVATE | SWP_NOZORDER);
1198 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1200 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1201 device->adapter->ordinal, &swapchain->d3d_mode)))
1202 ERR("Failed to set display mode.\n");
1205 else
1207 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1208 device->adapter->ordinal, NULL)))
1209 ERR("Failed to set display mode.\n");
1211 swapchain->reapply_mode = TRUE;
1213 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1214 && IsWindowVisible(swapchain->device_window))
1215 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1218 device->filter_messages = filter_messages;
1221 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1222 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1223 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1225 BOOL update_desc = FALSE;
1227 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1228 "multisample_type %#x, multisample_quality %#x.\n",
1229 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1230 multisample_type, multisample_quality);
1232 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1233 FIXME("Cannot change the back buffer count yet.\n");
1235 if (!width || !height)
1237 /* The application is requesting that either the swapchain width or
1238 * height be set to the corresponding dimension in the window's
1239 * client rect. */
1241 RECT client_rect;
1243 if (!swapchain->desc.windowed)
1244 return WINED3DERR_INVALIDCALL;
1246 if (!GetClientRect(swapchain->device_window, &client_rect))
1248 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1249 return WINED3DERR_INVALIDCALL;
1252 if (!width)
1253 width = client_rect.right;
1255 if (!height)
1256 height = client_rect.bottom;
1259 if (width != swapchain->desc.backbuffer_width
1260 || height != swapchain->desc.backbuffer_height)
1262 swapchain->desc.backbuffer_width = width;
1263 swapchain->desc.backbuffer_height = height;
1264 update_desc = TRUE;
1267 if (format_id == WINED3DFMT_UNKNOWN)
1269 if (!swapchain->desc.windowed)
1270 return WINED3DERR_INVALIDCALL;
1271 format_id = swapchain->original_mode.format_id;
1274 if (format_id != swapchain->desc.backbuffer_format)
1276 swapchain->desc.backbuffer_format = format_id;
1277 update_desc = TRUE;
1280 if (multisample_type != swapchain->desc.multisample_type
1281 || multisample_quality != swapchain->desc.multisample_quality)
1283 swapchain->desc.multisample_type = multisample_type;
1284 swapchain->desc.multisample_quality = multisample_quality;
1285 update_desc = TRUE;
1288 if (update_desc)
1290 HRESULT hr;
1291 UINT i;
1293 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1294 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1295 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1296 return hr;
1298 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1300 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1301 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1302 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1303 return hr;
1307 swapchain_update_render_to_fbo(swapchain);
1308 swapchain_update_draw_bindings(swapchain);
1310 return WINED3D_OK;