d3dx9: Implement GetMaxNum* functions.
[wine.git] / dlls / wined3d / swapchain.c
blob069b0719f15468ff4ef5b615b3e2962b321391d6
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_texture *dst_texture, unsigned int sub_resource_idx)
161 struct wined3d_surface *src_surface, *dst_surface;
162 struct wined3d_resource *sub_resource;
163 RECT src_rect, dst_rect;
165 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
167 if (!(sub_resource = wined3d_texture_get_sub_resource(dst_texture, sub_resource_idx)) ||
168 sub_resource->type != WINED3D_RTYPE_SURFACE)
169 return WINED3DERR_INVALIDCALL;
171 dst_surface = surface_from_resource(sub_resource);
172 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
173 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
174 dst_rect = src_rect;
176 if (swapchain->desc.windowed)
178 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
179 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
180 wine_dbgstr_rect(&dst_rect));
183 return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
186 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
187 UINT back_buffer_idx)
189 TRACE("swapchain %p, back_buffer_idx %u.\n",
190 swapchain, back_buffer_idx);
192 /* Return invalid if there is no backbuffer array, otherwise it will
193 * crash when ddraw is used (there swapchain->back_buffers is always
194 * NULL). We need this because this function is called from
195 * stateblock_init_default_state() to get the default scissorrect
196 * dimensions. */
197 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
199 WARN("Invalid back buffer index.\n");
200 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
201 * here in wined3d to avoid problems in other libs. */
202 return NULL;
205 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
207 return swapchain->back_buffers[back_buffer_idx];
210 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
211 struct wined3d_raster_status *raster_status)
213 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
215 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
216 swapchain->device->adapter->ordinal, raster_status);
219 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
220 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
222 HRESULT hr;
224 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
226 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
227 swapchain->device->adapter->ordinal, mode, rotation);
229 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
230 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
232 return hr;
235 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
237 TRACE("swapchain %p.\n", swapchain);
239 return swapchain->device;
242 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
243 struct wined3d_swapchain_desc *desc)
245 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
247 *desc = swapchain->desc;
250 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
251 DWORD flags, const struct wined3d_gamma_ramp *ramp)
253 HDC dc;
255 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
257 if (flags)
258 FIXME("Ignoring flags %#x.\n", flags);
260 dc = GetDC(swapchain->device_window);
261 SetDeviceGammaRamp(dc, (void *)ramp);
262 ReleaseDC(swapchain->device_window, dc);
264 return WINED3D_OK;
267 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
269 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
270 swapchain->palette = palette;
273 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
274 struct wined3d_gamma_ramp *ramp)
276 HDC dc;
278 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
280 dc = GetDC(swapchain->device_window);
281 GetDeviceGammaRamp(dc, ramp);
282 ReleaseDC(swapchain->device_window, dc);
284 return WINED3D_OK;
287 /* A GL context is provided by the caller */
288 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
289 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
291 struct wined3d_surface *backbuffer = surface_from_resource(
292 wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
293 UINT src_w = src_rect->right - src_rect->left;
294 UINT src_h = src_rect->bottom - src_rect->top;
295 GLenum gl_filter;
296 const struct wined3d_gl_info *gl_info = context->gl_info;
297 RECT win_rect;
298 UINT win_h;
300 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
301 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
303 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
304 gl_filter = GL_NEAREST;
305 else
306 gl_filter = GL_LINEAR;
308 GetClientRect(swapchain->win_handle, &win_rect);
309 win_h = win_rect.bottom - win_rect.top;
311 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format->color_fixup))
313 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
315 if (backbuffer->resource.multisample_type)
317 location = WINED3D_LOCATION_RB_RESOLVED;
318 surface_load_location(backbuffer, context, location);
321 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL, location);
322 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
323 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
325 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
326 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
327 NULL, WINED3D_LOCATION_DRAWABLE);
328 context_set_draw_buffer(context, GL_BACK);
329 context_invalidate_state(context, STATE_FRAMEBUFFER);
331 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
332 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
333 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
334 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
335 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
337 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
338 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
340 /* Note that the texture is upside down */
341 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
342 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
343 GL_COLOR_BUFFER_BIT, gl_filter);
344 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
346 else
348 struct wined3d_device *device = swapchain->device;
349 struct wined3d_context *context2;
350 float tex_left = src_rect->left;
351 float tex_top = src_rect->top;
352 float tex_right = src_rect->right;
353 float tex_bottom = src_rect->bottom;
355 context2 = context_acquire(device, backbuffer);
356 context_apply_blit_state(context2, device);
358 if (backbuffer->container->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
360 tex_left /= backbuffer->pow2Width;
361 tex_right /= backbuffer->pow2Width;
362 tex_top /= backbuffer->pow2Height;
363 tex_bottom /= backbuffer->pow2Height;
366 if (is_complex_fixup(backbuffer->resource.format->color_fixup))
367 gl_filter = GL_NEAREST;
369 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER,
370 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
371 NULL, WINED3D_LOCATION_DRAWABLE);
372 context_bind_texture(context2, backbuffer->texture_target, backbuffer->container->texture_rgb.name);
374 /* Set up the texture. The surface is not in a wined3d_texture
375 * container, so there are no D3D texture settings to dirtify. */
376 device->blitter->set_shader(device->blit_priv, context2, backbuffer, NULL);
377 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
378 gl_info->gl_ops.gl.p_glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
380 context_set_draw_buffer(context, GL_BACK);
382 /* Set the viewport to the destination rectandle, disable any projection
383 * transformation set up by context_apply_blit_state(), and draw a
384 * (-1,-1)-(1,1) quad.
386 * Back up viewport and matrix to avoid breaking last_was_blit
388 * Note that context_apply_blit_state() set up viewport and ortho to
389 * match the surface size - we want the GL drawable(=window) size. */
390 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
391 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
392 dst_rect->right, win_h - dst_rect->top);
393 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
394 gl_info->gl_ops.gl.p_glPushMatrix();
395 gl_info->gl_ops.gl.p_glLoadIdentity();
397 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
398 /* bottom left */
399 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
400 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
402 /* top left */
403 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
404 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
406 /* top right */
407 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
408 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
410 /* bottom right */
411 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
412 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
413 gl_info->gl_ops.gl.p_glEnd();
415 gl_info->gl_ops.gl.p_glPopMatrix();
416 gl_info->gl_ops.gl.p_glPopAttrib();
418 device->blitter->unset_shader(context->gl_info);
419 checkGLcall("Swapchain present blit(manual)\n");
421 context_release(context2);
425 static void swapchain_gl_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
426 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
428 struct wined3d_surface *back_buffer = surface_from_resource(
429 wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
430 const struct wined3d_fb_state *fb = &swapchain->device->fb;
431 const struct wined3d_gl_info *gl_info;
432 struct wined3d_context *context;
433 struct wined3d_surface *front;
434 RECT src_rect, dst_rect;
435 BOOL render_to_fbo;
437 context = context_acquire(swapchain->device, back_buffer);
438 if (!context->valid)
440 context_release(context);
441 WARN("Invalid context, skipping present.\n");
442 return;
445 gl_info = context->gl_info;
447 if (swapchain->device->logo_texture)
449 struct wined3d_surface *src_surface = surface_from_resource(
450 wined3d_texture_get_sub_resource(swapchain->device->logo_texture, 0));
451 RECT rect = {0, 0, src_surface->resource.width, src_surface->resource.height};
453 /* Blit the logo into the upper left corner of the drawable. */
454 wined3d_surface_blt(back_buffer, &rect, src_surface, &rect, WINEDDBLT_ALPHATEST,
455 NULL, WINED3D_TEXF_POINT);
458 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
459 && !swapchain->device->hardwareCursor)
461 struct wined3d_surface *cursor = surface_from_resource(
462 wined3d_texture_get_sub_resource(swapchain->device->cursor_texture, 0));
463 RECT destRect =
465 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
466 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
467 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
468 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
470 RECT src_rect =
472 0, 0,
473 swapchain->device->cursor_texture->resource.width,
474 swapchain->device->cursor_texture->resource.height
476 const RECT clip_rect = {0, 0, back_buffer->resource.width, back_buffer->resource.height};
478 TRACE("Rendering the software cursor.\n");
480 if (swapchain->desc.windowed)
481 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&destRect, 2);
482 if (wined3d_clip_blit(&clip_rect, &destRect, &src_rect))
483 wined3d_surface_blt(back_buffer, &destRect, cursor, &src_rect, WINEDDBLT_ALPHATEST,
484 NULL, WINED3D_TEXF_POINT);
487 TRACE("Presenting HDC %p.\n", context->hdc);
489 render_to_fbo = swapchain->render_to_fbo;
491 if (src_rect_in)
493 src_rect = *src_rect_in;
494 if (!render_to_fbo && (src_rect.left || src_rect.top
495 || src_rect.right != swapchain->desc.backbuffer_width
496 || src_rect.bottom != swapchain->desc.backbuffer_height))
498 render_to_fbo = TRUE;
501 else
503 src_rect.left = 0;
504 src_rect.top = 0;
505 src_rect.right = swapchain->desc.backbuffer_width;
506 src_rect.bottom = swapchain->desc.backbuffer_height;
509 if (dst_rect_in)
510 dst_rect = *dst_rect_in;
511 else
512 GetClientRect(swapchain->win_handle, &dst_rect);
514 if (!render_to_fbo && (dst_rect.left || dst_rect.top
515 || dst_rect.right != swapchain->desc.backbuffer_width
516 || dst_rect.bottom != swapchain->desc.backbuffer_height))
517 render_to_fbo = TRUE;
519 /* Rendering to a window of different size, presenting partial rectangles,
520 * or rendering to a different window needs help from FBO_blit or a textured
521 * draw. Render the swapchain to a FBO in the future.
523 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
524 * all these issues - this fails if the window is smaller than the backbuffer.
526 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
528 surface_load_location(back_buffer, context, WINED3D_LOCATION_TEXTURE_RGB);
529 surface_invalidate_location(back_buffer, WINED3D_LOCATION_DRAWABLE);
530 swapchain->render_to_fbo = TRUE;
531 swapchain_update_draw_bindings(swapchain);
533 else
535 surface_load_location(back_buffer, context, back_buffer->container->resource.draw_binding);
538 if (swapchain->render_to_fbo)
540 static unsigned int once;
542 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
543 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
545 swapchain_blit(swapchain, context, &src_rect, &dst_rect);
548 if (swapchain->num_contexts > 1)
549 gl_info->gl_ops.gl.p_glFinish();
551 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
552 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
554 TRACE("SwapBuffers called, Starting new frame\n");
555 /* FPS support */
556 if (TRACE_ON(fps))
558 DWORD time = GetTickCount();
559 ++swapchain->frames;
561 /* every 1.5 seconds */
562 if (time - swapchain->prev_time > 1500)
564 TRACE_(fps)("%p @ approx %.2ffps\n",
565 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
566 swapchain->prev_time = time;
567 swapchain->frames = 0;
571 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
573 surface_validate_location(front, WINED3D_LOCATION_DRAWABLE);
574 surface_invalidate_location(front, ~WINED3D_LOCATION_DRAWABLE);
575 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
576 * and INTEXTURE copies can keep their old content if they have any defined content.
577 * If the swapeffect is COPY, the content remains the same.
579 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
580 * up to date and hope WGL flipped front and back buffers and read this data into
581 * the FBO. Don't bother about this for now. */
583 if (fb->depth_stencil)
585 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
587 if (ds && (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
588 || ds->flags & SFLAG_DISCARD))
590 surface_modify_ds_location(ds, WINED3D_LOCATION_DISCARDED,
591 fb->depth_stencil->width, fb->depth_stencil->height);
592 if (ds == swapchain->device->onscreen_depth_stencil)
594 wined3d_texture_decref(swapchain->device->onscreen_depth_stencil->container);
595 swapchain->device->onscreen_depth_stencil = NULL;
600 context_release(context);
603 static const struct wined3d_swapchain_ops swapchain_gl_ops =
605 swapchain_gl_present,
608 /* Helper function that blits the front buffer contents to the target window. */
609 void x11_copy_to_screen(const struct wined3d_swapchain *swapchain, const RECT *rect)
611 struct wined3d_surface *front;
612 POINT offset = {0, 0};
613 HDC src_dc, dst_dc;
614 RECT draw_rect;
615 HWND window;
617 TRACE("swapchain %p, rect %s.\n", swapchain, wine_dbgstr_rect(rect));
619 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
620 if (swapchain->palette)
621 wined3d_palette_apply_to_dc(swapchain->palette, front->hDC);
623 if (front->resource.map_count)
624 ERR("Trying to blit a mapped surface.\n");
626 TRACE("Copying surface %p to screen.\n", front);
628 surface_load_location(front, NULL, WINED3D_LOCATION_DIB);
630 src_dc = front->hDC;
631 window = swapchain->win_handle;
632 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
634 /* Front buffer coordinates are screen coordinates. Map them to the
635 * destination window if not fullscreened. */
636 if (swapchain->desc.windowed)
637 ClientToScreen(window, &offset);
639 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
641 draw_rect.left = 0;
642 draw_rect.right = front->resource.width;
643 draw_rect.top = 0;
644 draw_rect.bottom = front->resource.height;
646 if (rect)
647 IntersectRect(&draw_rect, &draw_rect, rect);
649 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
650 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
651 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
652 ReleaseDC(window, dst_dc);
655 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const RECT *src_rect_in,
656 const RECT *dst_rect_in, const RGNDATA *dirty_region, DWORD flags)
658 struct wined3d_surface *front, *back;
660 front = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
661 back = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0));
663 /* Flip the DC. */
665 HDC tmp;
666 tmp = front->hDC;
667 front->hDC = back->hDC;
668 back->hDC = tmp;
671 /* Flip the DIBsection. */
673 HBITMAP tmp;
674 tmp = front->dib.DIBsection;
675 front->dib.DIBsection = back->dib.DIBsection;
676 back->dib.DIBsection = tmp;
679 /* Flip the surface data. */
681 void *tmp;
683 tmp = front->dib.bitmap_data;
684 front->dib.bitmap_data = back->dib.bitmap_data;
685 back->dib.bitmap_data = tmp;
687 if (front->resource.heap_memory)
688 ERR("GDI Surface %p has heap memory allocated.\n", front);
690 if (back->resource.heap_memory)
691 ERR("GDI Surface %p has heap memory allocated.\n", back);
694 /* FPS support */
695 if (TRACE_ON(fps))
697 static LONG prev_time, frames;
698 DWORD time = GetTickCount();
700 ++frames;
702 /* every 1.5 seconds */
703 if (time - prev_time > 1500)
705 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
706 prev_time = time;
707 frames = 0;
711 x11_copy_to_screen(swapchain, NULL);
714 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
716 swapchain_gdi_present,
719 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
721 RECT client_rect;
723 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
724 return;
726 if (!swapchain->desc.backbuffer_count)
728 TRACE("Single buffered rendering.\n");
729 swapchain->render_to_fbo = FALSE;
730 return;
733 GetClientRect(swapchain->win_handle, &client_rect);
735 TRACE("Backbuffer %ux%u, window %ux%u.\n",
736 swapchain->desc.backbuffer_width,
737 swapchain->desc.backbuffer_height,
738 client_rect.right, client_rect.bottom);
739 TRACE("Multisample type %#x, quality %#x.\n",
740 swapchain->desc.multisample_type,
741 swapchain->desc.multisample_quality);
743 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
744 && swapchain->desc.backbuffer_width == client_rect.right
745 && swapchain->desc.backbuffer_height == client_rect.bottom)
747 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
748 swapchain->render_to_fbo = FALSE;
749 return;
752 TRACE("Rendering to FBO.\n");
753 swapchain->render_to_fbo = TRUE;
756 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
757 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
759 const struct wined3d_adapter *adapter = device->adapter;
760 struct wined3d_resource_desc texture_desc;
761 struct wined3d_surface *front_buffer;
762 BOOL displaymode_set = FALSE;
763 RECT client_rect;
764 HWND window;
765 HRESULT hr;
766 UINT i;
768 if (desc->backbuffer_count > 1)
770 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
771 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
774 if (device->wined3d->flags & WINED3D_NO3D)
775 swapchain->swapchain_ops = &swapchain_gdi_ops;
776 else
777 swapchain->swapchain_ops = &swapchain_gl_ops;
779 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
781 swapchain->device = device;
782 swapchain->parent = parent;
783 swapchain->parent_ops = parent_ops;
784 swapchain->ref = 1;
785 swapchain->win_handle = window;
786 swapchain->device_window = window;
788 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
789 adapter->ordinal, &swapchain->original_mode, NULL)))
791 ERR("Failed to get current display mode, hr %#x.\n", hr);
792 goto err;
795 GetClientRect(window, &client_rect);
796 if (desc->windowed
797 && (!desc->backbuffer_width || !desc->backbuffer_height
798 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
801 if (!desc->backbuffer_width)
803 desc->backbuffer_width = client_rect.right;
804 TRACE("Updating width to %u.\n", desc->backbuffer_width);
807 if (!desc->backbuffer_height)
809 desc->backbuffer_height = client_rect.bottom;
810 TRACE("Updating height to %u.\n", desc->backbuffer_height);
813 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
815 desc->backbuffer_format = swapchain->original_mode.format_id;
816 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
819 swapchain->desc = *desc;
820 swapchain_update_render_to_fbo(swapchain);
822 TRACE("Creating front buffer.\n");
824 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
825 texture_desc.format = swapchain->desc.backbuffer_format;
826 texture_desc.multisample_type = swapchain->desc.multisample_type;
827 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
828 texture_desc.usage = 0;
829 texture_desc.pool = WINED3D_POOL_DEFAULT;
830 texture_desc.width = swapchain->desc.backbuffer_width;
831 texture_desc.height = swapchain->desc.backbuffer_height;
832 texture_desc.depth = 1;
833 texture_desc.size = 0;
835 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
836 parent, &texture_desc, &swapchain->front_buffer)))
838 WARN("Failed to create front buffer, hr %#x.\n", hr);
839 goto err;
842 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
843 front_buffer = surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
844 if (!(device->wined3d->flags & WINED3D_NO3D))
846 surface_validate_location(front_buffer, WINED3D_LOCATION_DRAWABLE);
847 surface_invalidate_location(front_buffer, ~WINED3D_LOCATION_DRAWABLE);
850 /* MSDN says we're only allowed a single fullscreen swapchain per device,
851 * so we should really check to see if there is a fullscreen swapchain
852 * already. Does a single head count as full screen? */
854 if (!desc->windowed)
856 /* Change the display settings */
857 swapchain->d3d_mode.width = desc->backbuffer_width;
858 swapchain->d3d_mode.height = desc->backbuffer_height;
859 swapchain->d3d_mode.format_id = desc->backbuffer_format;
860 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
861 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
863 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &swapchain->d3d_mode)))
865 WARN("Failed to set display mode, hr %#x.\n", hr);
866 goto err;
868 displaymode_set = TRUE;
871 if (!(device->wined3d->flags & WINED3D_NO3D))
873 static const enum wined3d_format_id formats[] =
875 WINED3DFMT_D24_UNORM_S8_UINT,
876 WINED3DFMT_D32_UNORM,
877 WINED3DFMT_R24_UNORM_X8_TYPELESS,
878 WINED3DFMT_D16_UNORM,
879 WINED3DFMT_S1_UINT_D15_UNORM
882 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
884 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
885 if (!swapchain->context)
887 ERR("Failed to create the context array.\n");
888 hr = E_OUTOFMEMORY;
889 goto err;
891 swapchain->num_contexts = 1;
893 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
894 * You are able to add a depth + stencil surface at a later stage when you need it.
895 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
896 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
897 * context, need torecreate shaders, textures and other resources.
899 * The context manager already takes care of the state problem and for the other tasks code from Reset
900 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
901 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
902 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
903 * issue needs to be fixed. */
904 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
906 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
907 swapchain->context[0] = context_create(swapchain, front_buffer, swapchain->ds_format);
908 if (swapchain->context[0]) break;
909 TRACE("Depth stencil format %s is not supported, trying next format\n",
910 debug_d3dformat(formats[i]));
913 if (!swapchain->context[0])
915 WARN("Failed to create context.\n");
916 hr = WINED3DERR_NOTAVAILABLE;
917 goto err;
920 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
921 && (!desc->enable_auto_depth_stencil
922 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
924 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
926 context_release(swapchain->context[0]);
929 if (swapchain->desc.backbuffer_count > 0)
931 swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
932 sizeof(*swapchain->back_buffers) * swapchain->desc.backbuffer_count);
933 if (!swapchain->back_buffers)
935 ERR("Failed to allocate backbuffer array memory.\n");
936 hr = E_OUTOFMEMORY;
937 goto err;
940 texture_desc.usage |= WINED3DUSAGE_RENDERTARGET;
941 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
943 TRACE("Creating back buffer %u.\n", i);
944 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
945 parent, &texture_desc, &swapchain->back_buffers[i])))
947 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
948 swapchain->desc.backbuffer_count = i;
949 goto err;
951 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
955 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
956 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
958 TRACE("Creating depth/stencil buffer.\n");
959 if (!device->auto_depth_stencil_view)
961 struct wined3d_texture *ds;
962 struct wined3d_rendertarget_view_desc desc;
964 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
965 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
967 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
968 device->device_parent, &texture_desc, &ds)))
970 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
971 goto err;
974 desc.format_id = ds->resource.format->id;
975 desc.u.texture.level_idx = 0;
976 desc.u.texture.layer_idx = 0;
977 desc.u.texture.layer_count = 1;
978 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
979 &device->auto_depth_stencil_view);
980 wined3d_texture_decref(ds);
981 if (FAILED(hr))
983 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
984 goto err;
989 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
991 return WINED3D_OK;
993 err:
994 if (displaymode_set)
996 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
997 adapter->ordinal, &swapchain->original_mode)))
998 ERR("Failed to restore display mode.\n");
999 ClipCursor(NULL);
1002 if (swapchain->back_buffers)
1004 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1006 if (swapchain->back_buffers[i])
1008 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1009 wined3d_texture_decref(swapchain->back_buffers[i]);
1012 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1015 if (swapchain->context)
1017 if (swapchain->context[0])
1019 context_release(swapchain->context[0]);
1020 context_destroy(device, swapchain->context[0]);
1021 swapchain->num_contexts = 0;
1023 HeapFree(GetProcessHeap(), 0, swapchain->context);
1026 if (swapchain->front_buffer)
1028 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1029 wined3d_texture_decref(swapchain->front_buffer);
1032 return hr;
1035 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1036 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1038 struct wined3d_swapchain *object;
1039 HRESULT hr;
1041 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1042 device, desc, parent, parent_ops, swapchain);
1044 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1045 if (!object)
1046 return E_OUTOFMEMORY;
1048 hr = swapchain_init(object, device, desc, parent, parent_ops);
1049 if (FAILED(hr))
1051 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1052 HeapFree(GetProcessHeap(), 0, object);
1053 return hr;
1056 TRACE("Created swapchain %p.\n", object);
1057 *swapchain = object;
1059 return WINED3D_OK;
1062 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1064 struct wined3d_context **newArray;
1065 struct wined3d_context *ctx;
1067 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1069 if (!(ctx = context_create(swapchain,
1070 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)),
1071 swapchain->ds_format)))
1073 ERR("Failed to create a new context for the swapchain\n");
1074 return NULL;
1076 context_release(ctx);
1078 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * (swapchain->num_contexts + 1));
1079 if(!newArray) {
1080 ERR("Out of memory when trying to allocate a new context array\n");
1081 context_destroy(swapchain->device, ctx);
1082 return NULL;
1084 memcpy(newArray, swapchain->context, sizeof(*newArray) * swapchain->num_contexts);
1085 HeapFree(GetProcessHeap(), 0, swapchain->context);
1086 newArray[swapchain->num_contexts] = ctx;
1087 swapchain->context = newArray;
1088 swapchain->num_contexts++;
1090 TRACE("Returning context %p\n", ctx);
1091 return ctx;
1094 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1096 unsigned int i;
1098 for (i = 0; i < swapchain->num_contexts; ++i)
1100 context_destroy(swapchain->device, swapchain->context[i]);
1102 swapchain->num_contexts = 0;
1105 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1107 DWORD tid = GetCurrentThreadId();
1108 unsigned int i;
1110 for (i = 0; i < swapchain->num_contexts; ++i)
1112 if (swapchain->context[i]->tid == tid)
1113 return swapchain->context[i];
1116 /* Create a new context for the thread */
1117 return swapchain_create_context(swapchain);
1120 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1122 if (!swapchain->backup_dc)
1124 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1126 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1127 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1129 ERR("Failed to create a window.\n");
1130 return NULL;
1133 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1135 ERR("Failed to get a DC.\n");
1136 DestroyWindow(swapchain->backup_wnd);
1137 swapchain->backup_wnd = NULL;
1138 return NULL;
1142 return swapchain->backup_dc;
1145 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1147 UINT i;
1149 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1151 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1153 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1157 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1159 struct wined3d_device *device = swapchain->device;
1160 BOOL filter_messages = device->filter_messages;
1162 /* This code is not protected by the wined3d mutex, so it may run while
1163 * wined3d_device_reset is active. Testing on Windows shows that changing
1164 * focus during resets and resetting during focus change events causes
1165 * the application to crash with an invalid memory access. */
1167 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1169 if (activate)
1171 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1173 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1174 * the window but leaves the size untouched, d3d9 sets the size on an
1175 * invisible window, generates messages but doesn't change the window
1176 * properties. The implementation follows d3d9.
1178 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1179 * resume drawing after a focus loss. */
1180 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1181 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1182 SWP_NOACTIVATE | SWP_NOZORDER);
1185 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1187 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1188 device->adapter->ordinal, &swapchain->d3d_mode)))
1189 ERR("Failed to set display mode.\n");
1192 else
1194 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1195 device->adapter->ordinal, NULL)))
1196 ERR("Failed to set display mode.\n");
1198 swapchain->reapply_mode = TRUE;
1200 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1201 && IsWindowVisible(swapchain->device_window))
1202 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1205 device->filter_messages = filter_messages;
1208 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1209 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1210 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1212 BOOL update_desc = FALSE;
1214 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1215 "multisample_type %#x, multisample_quality %#x.\n",
1216 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1217 multisample_type, multisample_quality);
1219 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1220 FIXME("Cannot change the back buffer count yet.\n");
1222 if (!width || !height)
1224 /* The application is requesting that either the swapchain width or
1225 * height be set to the corresponding dimension in the window's
1226 * client rect. */
1228 RECT client_rect;
1230 if (!swapchain->desc.windowed)
1231 return WINED3DERR_INVALIDCALL;
1233 if (!GetClientRect(swapchain->device_window, &client_rect))
1235 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1236 return WINED3DERR_INVALIDCALL;
1239 if (!width)
1240 width = client_rect.right;
1242 if (!height)
1243 height = client_rect.bottom;
1246 if (width != swapchain->desc.backbuffer_width
1247 || height != swapchain->desc.backbuffer_height)
1249 swapchain->desc.backbuffer_width = width;
1250 swapchain->desc.backbuffer_height = height;
1251 update_desc = TRUE;
1254 if (format_id == WINED3DFMT_UNKNOWN)
1256 if (!swapchain->desc.windowed)
1257 return WINED3DERR_INVALIDCALL;
1258 format_id = swapchain->original_mode.format_id;
1261 if (format_id != swapchain->desc.backbuffer_format)
1263 swapchain->desc.backbuffer_format = format_id;
1264 update_desc = TRUE;
1267 if (multisample_type != swapchain->desc.multisample_type
1268 || multisample_quality != swapchain->desc.multisample_quality)
1270 swapchain->desc.multisample_type = multisample_type;
1271 swapchain->desc.multisample_quality = multisample_quality;
1272 update_desc = TRUE;
1275 if (update_desc)
1277 HRESULT hr;
1278 UINT i;
1280 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1281 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1282 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1283 return hr;
1285 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1287 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1288 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1289 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1290 return hr;
1294 swapchain_update_render_to_fbo(swapchain);
1295 swapchain_update_draw_bindings(swapchain);
1297 return WINED3D_OK;