wined3d: Use pure integer vertex attributes for SM4+ shaders.
[wine.git] / dlls / wined3d / swapchain.c
blob0df7023190db971f58bba5f0c411f512fb85badb
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
30 static void swapchain_cleanup(struct wined3d_swapchain *swapchain)
32 HRESULT hr;
33 UINT i;
35 TRACE("Destroying swapchain %p.\n", swapchain);
37 wined3d_swapchain_set_gamma_ramp(swapchain, 0, &swapchain->orig_gamma);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain->front_buffer)
43 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
44 if (wined3d_texture_decref(swapchain->front_buffer))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain->front_buffer);
46 swapchain->front_buffer = NULL;
49 if (swapchain->back_buffers)
51 i = swapchain->desc.backbuffer_count;
53 while (i--)
55 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
56 if (wined3d_texture_decref(swapchain->back_buffers[i]))
57 WARN("Something's still holding back buffer %u (%p).\n", i, swapchain->back_buffers[i]);
59 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
60 swapchain->back_buffers = NULL;
63 for (i = 0; i < swapchain->num_contexts; ++i)
65 context_destroy(swapchain->device, swapchain->context[i]);
67 HeapFree(GetProcessHeap(), 0, swapchain->context);
69 /* Restore the screen resolution if we rendered in fullscreen.
70 * This will restore the screen resolution to what it was before creating
71 * the swapchain. In case of d3d8 and d3d9 this will be the original
72 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
73 * sets the resolution before starting up Direct3D, thus orig_width and
74 * orig_height will be equal to the modes in the presentation params. */
75 if (!swapchain->desc.windowed && swapchain->desc.auto_restore_display_mode)
77 if (FAILED(hr = wined3d_set_adapter_display_mode(swapchain->device->wined3d,
78 swapchain->device->adapter->ordinal, &swapchain->original_mode)))
79 ERR("Failed to restore display mode, hr %#x.\n", hr);
82 if (swapchain->backup_dc)
84 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain->backup_wnd, swapchain->backup_dc);
86 wined3d_release_dc(swapchain->backup_wnd, swapchain->backup_dc);
87 DestroyWindow(swapchain->backup_wnd);
91 ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
93 ULONG refcount = InterlockedIncrement(&swapchain->ref);
95 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
97 return refcount;
100 ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
102 ULONG refcount = InterlockedDecrement(&swapchain->ref);
104 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
106 if (!refcount)
108 swapchain_cleanup(swapchain);
109 swapchain->parent_ops->wined3d_object_destroyed(swapchain->parent);
110 HeapFree(GetProcessHeap(), 0, swapchain);
113 return refcount;
116 void * CDECL wined3d_swapchain_get_parent(const struct wined3d_swapchain *swapchain)
118 TRACE("swapchain %p.\n", swapchain);
120 return swapchain->parent;
123 void CDECL wined3d_swapchain_set_window(struct wined3d_swapchain *swapchain, HWND window)
125 if (!window)
126 window = swapchain->device_window;
127 if (window == swapchain->win_handle)
128 return;
130 TRACE("Setting swapchain %p window from %p to %p.\n",
131 swapchain, swapchain->win_handle, window);
132 swapchain->win_handle = window;
135 HRESULT CDECL wined3d_swapchain_present(struct wined3d_swapchain *swapchain,
136 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, DWORD flags)
138 RECT s, d;
140 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
141 swapchain, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
142 dst_window_override, flags);
144 if (flags)
145 FIXME("Ignoring flags %#x.\n", flags);
147 if (!swapchain->back_buffers)
149 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
150 return WINED3DERR_INVALIDCALL;
153 if (!src_rect)
155 SetRect(&s, 0, 0, swapchain->desc.backbuffer_width,
156 swapchain->desc.backbuffer_height);
157 src_rect = &s;
160 if (!dst_rect)
162 GetClientRect(swapchain->win_handle, &d);
163 dst_rect = &d;
166 wined3d_cs_emit_present(swapchain->device->cs, swapchain, src_rect,
167 dst_rect, dst_window_override, flags);
169 return WINED3D_OK;
172 HRESULT CDECL wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain *swapchain,
173 struct wined3d_texture *dst_texture, unsigned int sub_resource_idx)
175 RECT src_rect, dst_rect;
177 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain, dst_texture, sub_resource_idx);
179 SetRect(&src_rect, 0, 0, swapchain->front_buffer->resource.width, swapchain->front_buffer->resource.height);
180 dst_rect = src_rect;
182 if (swapchain->desc.windowed)
184 MapWindowPoints(swapchain->win_handle, NULL, (POINT *)&dst_rect, 2);
185 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
186 wine_dbgstr_rect(&dst_rect));
189 return wined3d_texture_blt(dst_texture, sub_resource_idx, &dst_rect,
190 swapchain->front_buffer, 0, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
193 struct wined3d_texture * CDECL wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain *swapchain,
194 UINT back_buffer_idx)
196 TRACE("swapchain %p, back_buffer_idx %u.\n",
197 swapchain, back_buffer_idx);
199 /* Return invalid if there is no backbuffer array, otherwise it will
200 * crash when ddraw is used (there swapchain->back_buffers is always
201 * NULL). We need this because this function is called from
202 * stateblock_init_default_state() to get the default scissorrect
203 * dimensions. */
204 if (!swapchain->back_buffers || back_buffer_idx >= swapchain->desc.backbuffer_count)
206 WARN("Invalid back buffer index.\n");
207 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
208 * here in wined3d to avoid problems in other libs. */
209 return NULL;
212 TRACE("Returning back buffer %p.\n", swapchain->back_buffers[back_buffer_idx]);
214 return swapchain->back_buffers[back_buffer_idx];
217 HRESULT CDECL wined3d_swapchain_get_raster_status(const struct wined3d_swapchain *swapchain,
218 struct wined3d_raster_status *raster_status)
220 TRACE("swapchain %p, raster_status %p.\n", swapchain, raster_status);
222 return wined3d_get_adapter_raster_status(swapchain->device->wined3d,
223 swapchain->device->adapter->ordinal, raster_status);
226 HRESULT CDECL wined3d_swapchain_get_display_mode(const struct wined3d_swapchain *swapchain,
227 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
229 HRESULT hr;
231 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain, mode, rotation);
233 hr = wined3d_get_adapter_display_mode(swapchain->device->wined3d,
234 swapchain->device->adapter->ordinal, mode, rotation);
236 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
237 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
239 return hr;
242 struct wined3d_device * CDECL wined3d_swapchain_get_device(const struct wined3d_swapchain *swapchain)
244 TRACE("swapchain %p.\n", swapchain);
246 return swapchain->device;
249 void CDECL wined3d_swapchain_get_desc(const struct wined3d_swapchain *swapchain,
250 struct wined3d_swapchain_desc *desc)
252 TRACE("swapchain %p, desc %p.\n", swapchain, desc);
254 *desc = swapchain->desc;
257 HRESULT CDECL wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain *swapchain,
258 DWORD flags, const struct wined3d_gamma_ramp *ramp)
260 HDC dc;
262 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain, flags, ramp);
264 if (flags)
265 FIXME("Ignoring flags %#x.\n", flags);
267 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
268 SetDeviceGammaRamp(dc, (void *)ramp);
269 ReleaseDC(swapchain->device_window, dc);
271 return WINED3D_OK;
274 void CDECL wined3d_swapchain_set_palette(struct wined3d_swapchain *swapchain, struct wined3d_palette *palette)
276 TRACE("swapchain %p, palette %p.\n", swapchain, palette);
277 swapchain->palette = palette;
280 HRESULT CDECL wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain *swapchain,
281 struct wined3d_gamma_ramp *ramp)
283 HDC dc;
285 TRACE("swapchain %p, ramp %p.\n", swapchain, ramp);
287 dc = GetDCEx(swapchain->device_window, 0, DCX_USESTYLE | DCX_CACHE);
288 GetDeviceGammaRamp(dc, ramp);
289 ReleaseDC(swapchain->device_window, dc);
291 return WINED3D_OK;
294 /* A GL context is provided by the caller */
295 static void swapchain_blit(const struct wined3d_swapchain *swapchain,
296 struct wined3d_context *context, const RECT *src_rect, const RECT *dst_rect)
298 struct wined3d_texture *texture = swapchain->back_buffers[0];
299 struct wined3d_surface *back_buffer = texture->sub_resources[0].u.surface;
300 struct wined3d_surface *front_buffer = swapchain->front_buffer->sub_resources[0].u.surface;
301 UINT src_w = src_rect->right - src_rect->left;
302 UINT src_h = src_rect->bottom - src_rect->top;
303 GLenum gl_filter;
304 const struct wined3d_gl_info *gl_info = context->gl_info;
305 RECT win_rect;
306 UINT win_h;
308 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
309 swapchain, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
311 if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
312 gl_filter = GL_NEAREST;
313 else
314 gl_filter = GL_LINEAR;
316 GetClientRect(swapchain->win_handle, &win_rect);
317 win_h = win_rect.bottom - win_rect.top;
319 if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(texture->resource.format->color_fixup))
321 DWORD location = WINED3D_LOCATION_TEXTURE_RGB;
323 if (texture->resource.multisample_type)
325 location = WINED3D_LOCATION_RB_RESOLVED;
326 surface_load_location(back_buffer, context, location);
329 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, back_buffer, NULL, location);
330 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
331 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
333 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
334 context_set_draw_buffer(context, GL_BACK);
335 context_invalidate_state(context, STATE_FRAMEBUFFER);
337 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
338 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
339 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
340 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
341 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
343 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
344 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
346 /* Note that the texture is upside down */
347 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
348 dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
349 GL_COLOR_BUFFER_BIT, gl_filter);
350 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
352 else
354 struct wined3d_device *device = swapchain->device;
355 struct wined3d_context *context2;
356 float tex_left = src_rect->left;
357 float tex_top = src_rect->top;
358 float tex_right = src_rect->right;
359 float tex_bottom = src_rect->bottom;
361 context2 = context_acquire(device, back_buffer);
362 context_apply_blit_state(context2, device);
364 if (texture->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
366 tex_left /= texture->pow2_width;
367 tex_right /= texture->pow2_width;
368 tex_top /= texture->pow2_height;
369 tex_bottom /= texture->pow2_height;
372 if (is_complex_fixup(texture->resource.format->color_fixup))
373 gl_filter = GL_NEAREST;
375 context_apply_fbo_state_blit(context2, GL_FRAMEBUFFER, front_buffer, NULL, WINED3D_LOCATION_DRAWABLE);
376 context_bind_texture(context2, back_buffer->texture_target, texture->texture_rgb.name);
378 /* Set up the texture. The surface is not in a wined3d_texture
379 * container, so there are no D3D texture settings to dirtify. */
380 device->blitter->set_shader(device->blit_priv, context2, back_buffer, NULL);
381 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
382 gl_info->gl_ops.gl.p_glTexParameteri(back_buffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
384 context_set_draw_buffer(context, GL_BACK);
386 /* Set the viewport to the destination rectandle, disable any projection
387 * transformation set up by context_apply_blit_state(), and draw a
388 * (-1,-1)-(1,1) quad.
390 * Back up viewport and matrix to avoid breaking last_was_blit
392 * Note that context_apply_blit_state() set up viewport and ortho to
393 * match the surface size - we want the GL drawable(=window) size. */
394 gl_info->gl_ops.gl.p_glPushAttrib(GL_VIEWPORT_BIT);
395 gl_info->gl_ops.gl.p_glViewport(dst_rect->left, win_h - dst_rect->bottom,
396 dst_rect->right, win_h - dst_rect->top);
397 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
398 gl_info->gl_ops.gl.p_glPushMatrix();
399 gl_info->gl_ops.gl.p_glLoadIdentity();
401 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
402 /* bottom left */
403 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_bottom);
404 gl_info->gl_ops.gl.p_glVertex2i(-1, -1);
406 /* top left */
407 gl_info->gl_ops.gl.p_glTexCoord2f(tex_left, tex_top);
408 gl_info->gl_ops.gl.p_glVertex2i(-1, 1);
410 /* top right */
411 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_top);
412 gl_info->gl_ops.gl.p_glVertex2i(1, 1);
414 /* bottom right */
415 gl_info->gl_ops.gl.p_glTexCoord2f(tex_right, tex_bottom);
416 gl_info->gl_ops.gl.p_glVertex2i(1, -1);
417 gl_info->gl_ops.gl.p_glEnd();
419 gl_info->gl_ops.gl.p_glPopMatrix();
420 gl_info->gl_ops.gl.p_glPopAttrib();
422 device->blitter->unset_shader(context->gl_info);
423 checkGLcall("Swapchain present blit(manual)\n");
425 context_release(context2);
429 /* Context activation is done by the caller. */
430 static void wined3d_swapchain_rotate(struct wined3d_swapchain *swapchain, struct wined3d_context *context)
432 struct wined3d_texture_sub_resource *sub_resource;
433 struct wined3d_texture *texture, *texture_prev;
434 struct gl_texture tex0;
435 GLuint rb0;
436 DWORD locations0;
437 unsigned int i;
438 static const DWORD supported_locations = WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_RB_MULTISAMPLE;
440 if (swapchain->desc.backbuffer_count < 2 || !swapchain->render_to_fbo)
441 return;
443 texture_prev = swapchain->back_buffers[0];
445 /* Back buffer 0 is already in the draw binding. */
446 tex0 = texture_prev->texture_rgb;
447 rb0 = texture_prev->rb_multisample;
448 locations0 = texture_prev->sub_resources[0].locations;
450 for (i = 1; i < swapchain->desc.backbuffer_count; ++i)
452 texture = swapchain->back_buffers[i];
453 sub_resource = &texture->sub_resources[0];
455 if (!(sub_resource->locations & supported_locations))
456 surface_load_location(sub_resource->u.surface, context, texture->resource.draw_binding);
458 texture_prev->texture_rgb = texture->texture_rgb;
459 texture_prev->rb_multisample = texture->rb_multisample;
461 wined3d_texture_validate_location(texture_prev, 0, sub_resource->locations & supported_locations);
462 wined3d_texture_invalidate_location(texture_prev, 0, ~(sub_resource->locations & supported_locations));
464 texture_prev = texture;
467 texture_prev->texture_rgb = tex0;
468 texture_prev->rb_multisample = rb0;
470 wined3d_texture_validate_location(texture_prev, 0, locations0 & supported_locations);
471 wined3d_texture_invalidate_location(texture_prev, 0, ~(locations0 & supported_locations));
473 device_invalidate_state(swapchain->device, STATE_FRAMEBUFFER);
476 static void swapchain_gl_present(struct wined3d_swapchain *swapchain,
477 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
479 struct wined3d_surface *back_buffer = swapchain->back_buffers[0]->sub_resources[0].u.surface;
480 const struct wined3d_fb_state *fb = &swapchain->device->fb;
481 const struct wined3d_gl_info *gl_info;
482 struct wined3d_texture *logo_texture;
483 struct wined3d_context *context;
484 BOOL render_to_fbo;
486 context = context_acquire(swapchain->device, back_buffer);
487 if (!context->valid)
489 context_release(context);
490 WARN("Invalid context, skipping present.\n");
491 return;
494 gl_info = context->gl_info;
496 if ((logo_texture = swapchain->device->logo_texture))
498 RECT rect = {0, 0, logo_texture->resource.width, logo_texture->resource.height};
500 /* Blit the logo into the upper left corner of the drawable. */
501 wined3d_texture_blt(swapchain->back_buffers[0], 0, &rect, logo_texture, 0, &rect,
502 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
505 if (swapchain->device->bCursorVisible && swapchain->device->cursor_texture
506 && !swapchain->device->hardwareCursor)
508 RECT dst_rect =
510 swapchain->device->xScreenSpace - swapchain->device->xHotSpot,
511 swapchain->device->yScreenSpace - swapchain->device->yHotSpot,
512 swapchain->device->xScreenSpace + swapchain->device->cursorWidth - swapchain->device->xHotSpot,
513 swapchain->device->yScreenSpace + swapchain->device->cursorHeight - swapchain->device->yHotSpot,
515 RECT src_rect =
517 0, 0,
518 swapchain->device->cursor_texture->resource.width,
519 swapchain->device->cursor_texture->resource.height
521 const RECT clip_rect = {0, 0,
522 swapchain->back_buffers[0]->resource.width,
523 swapchain->back_buffers[0]->resource.height};
525 TRACE("Rendering the software cursor.\n");
527 if (swapchain->desc.windowed)
528 MapWindowPoints(NULL, swapchain->win_handle, (POINT *)&dst_rect, 2);
529 if (wined3d_clip_blit(&clip_rect, &dst_rect, &src_rect))
530 wined3d_texture_blt(swapchain->back_buffers[0], 0, &dst_rect,
531 swapchain->device->cursor_texture, 0, &src_rect,
532 WINED3D_BLT_ALPHA_TEST, NULL, WINED3D_TEXF_POINT);
535 TRACE("Presenting HDC %p.\n", context->hdc);
537 if (!(render_to_fbo = swapchain->render_to_fbo)
538 && (src_rect->left || src_rect->top
539 || src_rect->right != swapchain->desc.backbuffer_width
540 || src_rect->bottom != swapchain->desc.backbuffer_height
541 || dst_rect->left || dst_rect->top
542 || dst_rect->right != swapchain->desc.backbuffer_width
543 || dst_rect->bottom != swapchain->desc.backbuffer_height))
544 render_to_fbo = TRUE;
546 /* Rendering to a window of different size, presenting partial rectangles,
547 * or rendering to a different window needs help from FBO_blit or a textured
548 * draw. Render the swapchain to a FBO in the future.
550 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
551 * all these issues - this fails if the window is smaller than the backbuffer.
553 if (!swapchain->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
555 surface_load_location(back_buffer, context, WINED3D_LOCATION_TEXTURE_RGB);
556 wined3d_texture_invalidate_location(back_buffer->container, 0, WINED3D_LOCATION_DRAWABLE);
557 swapchain->render_to_fbo = TRUE;
558 swapchain_update_draw_bindings(swapchain);
560 else
562 surface_load_location(back_buffer, context, back_buffer->container->resource.draw_binding);
565 if (swapchain->render_to_fbo)
567 static unsigned int once;
569 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_FLIP && !once++)
570 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
572 swapchain_blit(swapchain, context, src_rect, dst_rect);
575 if (swapchain->num_contexts > 1)
576 gl_info->gl_ops.gl.p_glFinish();
578 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
579 gl_info->gl_ops.wgl.p_wglSwapBuffers(context->hdc);
581 wined3d_swapchain_rotate(swapchain, context);
583 TRACE("SwapBuffers called, Starting new frame\n");
584 /* FPS support */
585 if (TRACE_ON(fps))
587 DWORD time = GetTickCount();
588 ++swapchain->frames;
590 /* every 1.5 seconds */
591 if (time - swapchain->prev_time > 1500)
593 TRACE_(fps)("%p @ approx %.2ffps\n",
594 swapchain, 1000.0 * swapchain->frames / (time - swapchain->prev_time));
595 swapchain->prev_time = time;
596 swapchain->frames = 0;
600 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
601 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
602 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
603 * and INTEXTURE copies can keep their old content if they have any defined content.
604 * If the swapeffect is COPY, the content remains the same.
606 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
607 * up to date and hope WGL flipped front and back buffers and read this data into
608 * the FBO. Don't bother about this for now. */
609 if (swapchain->desc.swap_effect == WINED3D_SWAP_EFFECT_DISCARD)
610 wined3d_texture_validate_location(swapchain->back_buffers[swapchain->desc.backbuffer_count - 1],
611 0, WINED3D_LOCATION_DISCARDED);
613 if (fb->depth_stencil)
615 struct wined3d_surface *ds = wined3d_rendertarget_view_get_surface(fb->depth_stencil);
617 if (ds && (swapchain->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
618 || ds->container->flags & WINED3D_TEXTURE_DISCARD))
620 surface_modify_ds_location(ds, WINED3D_LOCATION_DISCARDED,
621 fb->depth_stencil->width, fb->depth_stencil->height);
622 if (ds == swapchain->device->onscreen_depth_stencil)
624 wined3d_texture_decref(swapchain->device->onscreen_depth_stencil->container);
625 swapchain->device->onscreen_depth_stencil = NULL;
630 context_release(context);
633 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain *swapchain)
635 struct wined3d_surface *surface;
636 struct wined3d_context *context;
638 surface = swapchain->front_buffer->sub_resources[0].u.surface;
639 context = context_acquire(swapchain->device, surface);
640 surface_load_location(surface, context, surface->container->resource.draw_binding);
641 context_release(context);
642 SetRectEmpty(&swapchain->front_buffer_update);
645 static const struct wined3d_swapchain_ops swapchain_gl_ops =
647 swapchain_gl_present,
648 swapchain_gl_frontbuffer_updated,
651 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain *swapchain)
653 struct wined3d_surface *front;
654 POINT offset = {0, 0};
655 HDC src_dc, dst_dc;
656 RECT draw_rect;
657 HWND window;
659 TRACE("swapchain %p.\n", swapchain);
661 front = swapchain->front_buffer->sub_resources[0].u.surface;
662 if (swapchain->palette)
663 wined3d_palette_apply_to_dc(swapchain->palette, front->dc);
665 if (front->container->resource.map_count)
666 ERR("Trying to blit a mapped surface.\n");
668 TRACE("Copying surface %p to screen.\n", front);
670 src_dc = front->dc;
671 window = swapchain->win_handle;
672 dst_dc = GetDCEx(window, 0, DCX_CLIPSIBLINGS | DCX_CACHE);
674 /* Front buffer coordinates are screen coordinates. Map them to the
675 * destination window if not fullscreened. */
676 if (swapchain->desc.windowed)
677 ClientToScreen(window, &offset);
679 TRACE("offset %s.\n", wine_dbgstr_point(&offset));
681 draw_rect.left = 0;
682 draw_rect.right = swapchain->front_buffer->resource.width;
683 draw_rect.top = 0;
684 draw_rect.bottom = swapchain->front_buffer->resource.height;
685 IntersectRect(&draw_rect, &draw_rect, &swapchain->front_buffer_update);
687 BitBlt(dst_dc, draw_rect.left - offset.x, draw_rect.top - offset.y,
688 draw_rect.right - draw_rect.left, draw_rect.bottom - draw_rect.top,
689 src_dc, draw_rect.left, draw_rect.top, SRCCOPY);
690 ReleaseDC(window, dst_dc);
692 SetRectEmpty(&swapchain->front_buffer_update);
695 static void swapchain_gdi_present(struct wined3d_swapchain *swapchain,
696 const RECT *src_rect, const RECT *dst_rect, DWORD flags)
698 struct wined3d_surface *front, *back;
699 HBITMAP bitmap;
700 void *data;
701 HDC dc;
703 front = swapchain->front_buffer->sub_resources[0].u.surface;
704 back = swapchain->back_buffers[0]->sub_resources[0].u.surface;
706 /* Flip the surface data. */
707 dc = front->dc;
708 bitmap = front->bitmap;
709 data = front->container->resource.heap_memory;
711 front->dc = back->dc;
712 front->bitmap = back->bitmap;
713 front->container->resource.heap_memory = back->container->resource.heap_memory;
715 back->dc = dc;
716 back->bitmap = bitmap;
717 back->container->resource.heap_memory = data;
719 /* FPS support */
720 if (TRACE_ON(fps))
722 static LONG prev_time, frames;
723 DWORD time = GetTickCount();
725 ++frames;
727 /* every 1.5 seconds */
728 if (time - prev_time > 1500)
730 TRACE_(fps)("@ approx %.2ffps\n", 1000.0 * frames / (time - prev_time));
731 prev_time = time;
732 frames = 0;
736 SetRect(&swapchain->front_buffer_update, 0, 0,
737 swapchain->front_buffer->resource.width,
738 swapchain->front_buffer->resource.height);
739 swapchain_gdi_frontbuffer_updated(swapchain);
742 static const struct wined3d_swapchain_ops swapchain_gdi_ops =
744 swapchain_gdi_present,
745 swapchain_gdi_frontbuffer_updated,
748 static void swapchain_update_render_to_fbo(struct wined3d_swapchain *swapchain)
750 RECT client_rect;
752 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
753 return;
755 if (!swapchain->desc.backbuffer_count)
757 TRACE("Single buffered rendering.\n");
758 swapchain->render_to_fbo = FALSE;
759 return;
762 GetClientRect(swapchain->win_handle, &client_rect);
764 TRACE("Backbuffer %ux%u, window %ux%u.\n",
765 swapchain->desc.backbuffer_width,
766 swapchain->desc.backbuffer_height,
767 client_rect.right, client_rect.bottom);
768 TRACE("Multisample type %#x, quality %#x.\n",
769 swapchain->desc.multisample_type,
770 swapchain->desc.multisample_quality);
772 if (!wined3d_settings.always_offscreen && !swapchain->desc.multisample_type
773 && swapchain->desc.backbuffer_width == client_rect.right
774 && swapchain->desc.backbuffer_height == client_rect.bottom)
776 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
777 swapchain->render_to_fbo = FALSE;
778 return;
781 TRACE("Rendering to FBO.\n");
782 swapchain->render_to_fbo = TRUE;
785 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain *swapchain,
786 enum wined3d_format_id format_id, enum wined3d_multisample_type *type, DWORD *quality)
788 const struct wined3d_gl_info *gl_info;
789 const struct wined3d_format *format;
790 enum wined3d_multisample_type t;
792 if (wined3d_settings.sample_count == ~0u)
793 return;
795 gl_info = &swapchain->device->adapter->gl_info;
796 if (!(format = wined3d_get_format(gl_info, format_id)))
797 return;
799 if ((t = min(wined3d_settings.sample_count, gl_info->limits.samples)))
800 while (!(format->multisample_types & 1u << (t - 1)))
801 ++t;
802 TRACE("Using sample count %u.\n", t);
803 *type = t;
804 *quality = 0;
807 static HRESULT swapchain_init(struct wined3d_swapchain *swapchain, struct wined3d_device *device,
808 struct wined3d_swapchain_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
810 const struct wined3d_adapter *adapter = device->adapter;
811 struct wined3d_resource_desc texture_desc;
812 BOOL displaymode_set = FALSE;
813 RECT client_rect;
814 HWND window;
815 HRESULT hr;
816 UINT i;
818 if (desc->backbuffer_count > 1)
820 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
821 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
824 if (device->wined3d->flags & WINED3D_NO3D)
825 swapchain->swapchain_ops = &swapchain_gdi_ops;
826 else
827 swapchain->swapchain_ops = &swapchain_gl_ops;
829 window = desc->device_window ? desc->device_window : device->create_parms.focus_window;
831 swapchain->device = device;
832 swapchain->parent = parent;
833 swapchain->parent_ops = parent_ops;
834 swapchain->ref = 1;
835 swapchain->win_handle = window;
836 swapchain->device_window = window;
838 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d,
839 adapter->ordinal, &swapchain->original_mode, NULL)))
841 ERR("Failed to get current display mode, hr %#x.\n", hr);
842 goto err;
845 GetClientRect(window, &client_rect);
846 if (desc->windowed
847 && (!desc->backbuffer_width || !desc->backbuffer_height
848 || desc->backbuffer_format == WINED3DFMT_UNKNOWN))
851 if (!desc->backbuffer_width)
853 desc->backbuffer_width = client_rect.right;
854 TRACE("Updating width to %u.\n", desc->backbuffer_width);
857 if (!desc->backbuffer_height)
859 desc->backbuffer_height = client_rect.bottom;
860 TRACE("Updating height to %u.\n", desc->backbuffer_height);
863 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
865 desc->backbuffer_format = swapchain->original_mode.format_id;
866 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->original_mode.format_id));
869 swapchain->desc = *desc;
870 wined3d_swapchain_apply_sample_count_override(swapchain, swapchain->desc.backbuffer_format,
871 &swapchain->desc.multisample_type, &swapchain->desc.multisample_quality);
872 swapchain_update_render_to_fbo(swapchain);
874 TRACE("Creating front buffer.\n");
876 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
877 texture_desc.format = swapchain->desc.backbuffer_format;
878 texture_desc.multisample_type = swapchain->desc.multisample_type;
879 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
880 texture_desc.usage = 0;
881 texture_desc.pool = WINED3D_POOL_DEFAULT;
882 texture_desc.width = swapchain->desc.backbuffer_width;
883 texture_desc.height = swapchain->desc.backbuffer_height;
884 texture_desc.depth = 1;
885 texture_desc.size = 0;
887 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
888 parent, &texture_desc, &swapchain->front_buffer)))
890 WARN("Failed to create front buffer, hr %#x.\n", hr);
891 goto err;
894 wined3d_texture_set_swapchain(swapchain->front_buffer, swapchain);
895 if (!(device->wined3d->flags & WINED3D_NO3D))
897 wined3d_texture_validate_location(swapchain->front_buffer, 0, WINED3D_LOCATION_DRAWABLE);
898 wined3d_texture_invalidate_location(swapchain->front_buffer, 0, ~WINED3D_LOCATION_DRAWABLE);
901 /* MSDN says we're only allowed a single fullscreen swapchain per device,
902 * so we should really check to see if there is a fullscreen swapchain
903 * already. Does a single head count as full screen? */
905 if (!desc->windowed)
907 /* Change the display settings */
908 swapchain->d3d_mode.width = desc->backbuffer_width;
909 swapchain->d3d_mode.height = desc->backbuffer_height;
910 swapchain->d3d_mode.format_id = desc->backbuffer_format;
911 swapchain->d3d_mode.refresh_rate = desc->refresh_rate;
912 swapchain->d3d_mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
914 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, adapter->ordinal, &swapchain->d3d_mode)))
916 WARN("Failed to set display mode, hr %#x.\n", hr);
917 goto err;
919 displaymode_set = TRUE;
922 if (!(device->wined3d->flags & WINED3D_NO3D))
924 static const enum wined3d_format_id formats[] =
926 WINED3DFMT_D24_UNORM_S8_UINT,
927 WINED3DFMT_D32_UNORM,
928 WINED3DFMT_R24_UNORM_X8_TYPELESS,
929 WINED3DFMT_D16_UNORM,
930 WINED3DFMT_S1_UINT_D15_UNORM
933 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
935 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
936 if (!swapchain->context)
938 ERR("Failed to create the context array.\n");
939 hr = E_OUTOFMEMORY;
940 goto err;
942 swapchain->num_contexts = 1;
944 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
945 * You are able to add a depth + stencil surface at a later stage when you need it.
946 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
947 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
948 * context, need torecreate shaders, textures and other resources.
950 * The context manager already takes care of the state problem and for the other tasks code from Reset
951 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
952 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
953 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
954 * issue needs to be fixed. */
955 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
957 swapchain->ds_format = wined3d_get_format(gl_info, formats[i]);
958 swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
959 if (swapchain->context[0]) break;
960 TRACE("Depth stencil format %s is not supported, trying next format\n",
961 debug_d3dformat(formats[i]));
964 if (!swapchain->context[0])
966 WARN("Failed to create context.\n");
967 hr = WINED3DERR_NOTAVAILABLE;
968 goto err;
971 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
972 && (!desc->enable_auto_depth_stencil
973 || swapchain->desc.auto_depth_stencil_format != swapchain->ds_format->id))
975 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
977 context_release(swapchain->context[0]);
978 swapchain_update_swap_interval(swapchain);
981 if (swapchain->desc.backbuffer_count > 0)
983 if (!(swapchain->back_buffers = wined3d_calloc(swapchain->desc.backbuffer_count,
984 sizeof(*swapchain->back_buffers))))
986 ERR("Failed to allocate backbuffer array memory.\n");
987 hr = E_OUTOFMEMORY;
988 goto err;
991 texture_desc.usage |= WINED3DUSAGE_RENDERTARGET;
992 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
994 TRACE("Creating back buffer %u.\n", i);
995 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
996 parent, &texture_desc, &swapchain->back_buffers[i])))
998 WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
999 swapchain->desc.backbuffer_count = i;
1000 goto err;
1002 wined3d_texture_set_swapchain(swapchain->back_buffers[i], swapchain);
1006 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1007 if (desc->enable_auto_depth_stencil && !(device->wined3d->flags & WINED3D_NO3D))
1009 TRACE("Creating depth/stencil buffer.\n");
1010 if (!device->auto_depth_stencil_view)
1012 struct wined3d_texture *ds;
1013 struct wined3d_rendertarget_view_desc desc;
1015 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
1016 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
1018 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
1019 device->device_parent, &texture_desc, &ds)))
1021 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
1022 goto err;
1025 desc.format_id = ds->resource.format->id;
1026 desc.u.texture.level_idx = 0;
1027 desc.u.texture.layer_idx = 0;
1028 desc.u.texture.layer_count = 1;
1029 hr = wined3d_rendertarget_view_create(&desc, &ds->resource, NULL, &wined3d_null_parent_ops,
1030 &device->auto_depth_stencil_view);
1031 wined3d_texture_decref(ds);
1032 if (FAILED(hr))
1034 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1035 goto err;
1040 wined3d_swapchain_get_gamma_ramp(swapchain, &swapchain->orig_gamma);
1042 return WINED3D_OK;
1044 err:
1045 if (displaymode_set)
1047 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1048 adapter->ordinal, &swapchain->original_mode)))
1049 ERR("Failed to restore display mode.\n");
1050 ClipCursor(NULL);
1053 if (swapchain->back_buffers)
1055 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1057 if (swapchain->back_buffers[i])
1059 wined3d_texture_set_swapchain(swapchain->back_buffers[i], NULL);
1060 wined3d_texture_decref(swapchain->back_buffers[i]);
1063 HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
1066 if (swapchain->context)
1068 if (swapchain->context[0])
1070 context_release(swapchain->context[0]);
1071 context_destroy(device, swapchain->context[0]);
1072 swapchain->num_contexts = 0;
1074 HeapFree(GetProcessHeap(), 0, swapchain->context);
1077 if (swapchain->front_buffer)
1079 wined3d_texture_set_swapchain(swapchain->front_buffer, NULL);
1080 wined3d_texture_decref(swapchain->front_buffer);
1083 return hr;
1086 HRESULT CDECL wined3d_swapchain_create(struct wined3d_device *device, struct wined3d_swapchain_desc *desc,
1087 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_swapchain **swapchain)
1089 struct wined3d_swapchain *object;
1090 HRESULT hr;
1092 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1093 device, desc, parent, parent_ops, swapchain);
1095 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1096 if (!object)
1097 return E_OUTOFMEMORY;
1099 hr = swapchain_init(object, device, desc, parent, parent_ops);
1100 if (FAILED(hr))
1102 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
1103 HeapFree(GetProcessHeap(), 0, object);
1104 return hr;
1107 TRACE("Created swapchain %p.\n", object);
1108 *swapchain = object;
1110 return WINED3D_OK;
1113 static struct wined3d_context *swapchain_create_context(struct wined3d_swapchain *swapchain)
1115 struct wined3d_context **ctx_array;
1116 struct wined3d_context *ctx;
1118 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain, GetCurrentThreadId());
1120 if (!(ctx = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format)))
1122 ERR("Failed to create a new context for the swapchain\n");
1123 return NULL;
1125 context_release(ctx);
1127 if (!(ctx_array = wined3d_calloc(swapchain->num_contexts + 1, sizeof(*ctx_array))))
1129 ERR("Out of memory when trying to allocate a new context array\n");
1130 context_destroy(swapchain->device, ctx);
1131 return NULL;
1133 memcpy(ctx_array, swapchain->context, sizeof(*ctx_array) * swapchain->num_contexts);
1134 HeapFree(GetProcessHeap(), 0, swapchain->context);
1135 ctx_array[swapchain->num_contexts] = ctx;
1136 swapchain->context = ctx_array;
1137 swapchain->num_contexts++;
1139 TRACE("Returning context %p\n", ctx);
1140 return ctx;
1143 void swapchain_destroy_contexts(struct wined3d_swapchain *swapchain)
1145 unsigned int i;
1147 for (i = 0; i < swapchain->num_contexts; ++i)
1149 context_destroy(swapchain->device, swapchain->context[i]);
1151 swapchain->num_contexts = 0;
1154 struct wined3d_context *swapchain_get_context(struct wined3d_swapchain *swapchain)
1156 DWORD tid = GetCurrentThreadId();
1157 unsigned int i;
1159 for (i = 0; i < swapchain->num_contexts; ++i)
1161 if (swapchain->context[i]->tid == tid)
1162 return swapchain->context[i];
1165 /* Create a new context for the thread */
1166 return swapchain_create_context(swapchain);
1169 HDC swapchain_get_backup_dc(struct wined3d_swapchain *swapchain)
1171 if (!swapchain->backup_dc)
1173 TRACE("Creating the backup window for swapchain %p.\n", swapchain);
1175 if (!(swapchain->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
1176 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
1178 ERR("Failed to create a window.\n");
1179 return NULL;
1182 if (!(swapchain->backup_dc = GetDC(swapchain->backup_wnd)))
1184 ERR("Failed to get a DC.\n");
1185 DestroyWindow(swapchain->backup_wnd);
1186 swapchain->backup_wnd = NULL;
1187 return NULL;
1191 return swapchain->backup_dc;
1194 void swapchain_update_draw_bindings(struct wined3d_swapchain *swapchain)
1196 UINT i;
1198 wined3d_resource_update_draw_binding(&swapchain->front_buffer->resource);
1200 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1202 wined3d_resource_update_draw_binding(&swapchain->back_buffers[i]->resource);
1206 void swapchain_update_swap_interval(struct wined3d_swapchain *swapchain)
1208 const struct wined3d_gl_info *gl_info;
1209 struct wined3d_context *context;
1210 int swap_interval;
1212 context = context_acquire(swapchain->device, swapchain->front_buffer->sub_resources[0].u.surface);
1213 gl_info = context->gl_info;
1215 switch (swapchain->desc.swap_interval)
1217 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1218 swap_interval = 0;
1219 break;
1220 case WINED3DPRESENT_INTERVAL_DEFAULT:
1221 case WINED3DPRESENT_INTERVAL_ONE:
1222 swap_interval = 1;
1223 break;
1224 case WINED3DPRESENT_INTERVAL_TWO:
1225 swap_interval = 2;
1226 break;
1227 case WINED3DPRESENT_INTERVAL_THREE:
1228 swap_interval = 3;
1229 break;
1230 case WINED3DPRESENT_INTERVAL_FOUR:
1231 swap_interval = 4;
1232 break;
1233 default:
1234 FIXME("Unhandled present interval %#x.\n", swapchain->desc.swap_interval);
1235 swap_interval = 1;
1238 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1240 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1241 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1242 swap_interval, context, GetLastError());
1245 context_release(context);
1248 void wined3d_swapchain_activate(struct wined3d_swapchain *swapchain, BOOL activate)
1250 struct wined3d_device *device = swapchain->device;
1251 BOOL filter_messages = device->filter_messages;
1253 /* This code is not protected by the wined3d mutex, so it may run while
1254 * wined3d_device_reset is active. Testing on Windows shows that changing
1255 * focus during resets and resetting during focus change events causes
1256 * the application to crash with an invalid memory access. */
1258 device->filter_messages = !(device->wined3d->flags & WINED3D_FOCUS_MESSAGES);
1260 if (activate)
1262 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES))
1264 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1265 * the window but leaves the size untouched, d3d9 sets the size on an
1266 * invisible window, generates messages but doesn't change the window
1267 * properties. The implementation follows d3d9.
1269 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1270 * resume drawing after a focus loss. */
1271 SetWindowPos(swapchain->device_window, NULL, 0, 0,
1272 swapchain->desc.backbuffer_width, swapchain->desc.backbuffer_height,
1273 SWP_NOACTIVATE | SWP_NOZORDER);
1276 if (device->wined3d->flags & WINED3D_RESTORE_MODE_ON_ACTIVATE)
1278 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1279 device->adapter->ordinal, &swapchain->d3d_mode)))
1280 ERR("Failed to set display mode.\n");
1283 else
1285 if (FAILED(wined3d_set_adapter_display_mode(device->wined3d,
1286 device->adapter->ordinal, NULL)))
1287 ERR("Failed to set display mode.\n");
1289 swapchain->reapply_mode = TRUE;
1291 if (!(device->create_parms.flags & WINED3DCREATE_NOWINDOWCHANGES)
1292 && IsWindowVisible(swapchain->device_window))
1293 ShowWindow(swapchain->device_window, SW_MINIMIZE);
1296 device->filter_messages = filter_messages;
1299 HRESULT CDECL wined3d_swapchain_resize_buffers(struct wined3d_swapchain *swapchain, unsigned int buffer_count,
1300 unsigned int width, unsigned int height, enum wined3d_format_id format_id,
1301 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality)
1303 BOOL update_desc = FALSE;
1305 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1306 "multisample_type %#x, multisample_quality %#x.\n",
1307 swapchain, buffer_count, width, height, debug_d3dformat(format_id),
1308 multisample_type, multisample_quality);
1310 wined3d_swapchain_apply_sample_count_override(swapchain, format_id, &multisample_type, &multisample_quality);
1312 if (buffer_count && buffer_count != swapchain->desc.backbuffer_count)
1313 FIXME("Cannot change the back buffer count yet.\n");
1315 if (!width || !height)
1317 /* The application is requesting that either the swapchain width or
1318 * height be set to the corresponding dimension in the window's
1319 * client rect. */
1321 RECT client_rect;
1323 if (!swapchain->desc.windowed)
1324 return WINED3DERR_INVALIDCALL;
1326 if (!GetClientRect(swapchain->device_window, &client_rect))
1328 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1329 return WINED3DERR_INVALIDCALL;
1332 if (!width)
1333 width = client_rect.right;
1335 if (!height)
1336 height = client_rect.bottom;
1339 if (width != swapchain->desc.backbuffer_width
1340 || height != swapchain->desc.backbuffer_height)
1342 swapchain->desc.backbuffer_width = width;
1343 swapchain->desc.backbuffer_height = height;
1344 update_desc = TRUE;
1347 if (format_id == WINED3DFMT_UNKNOWN)
1349 if (!swapchain->desc.windowed)
1350 return WINED3DERR_INVALIDCALL;
1351 format_id = swapchain->original_mode.format_id;
1354 if (format_id != swapchain->desc.backbuffer_format)
1356 swapchain->desc.backbuffer_format = format_id;
1357 update_desc = TRUE;
1360 if (multisample_type != swapchain->desc.multisample_type
1361 || multisample_quality != swapchain->desc.multisample_quality)
1363 swapchain->desc.multisample_type = multisample_type;
1364 swapchain->desc.multisample_quality = multisample_quality;
1365 update_desc = TRUE;
1368 if (update_desc)
1370 HRESULT hr;
1371 UINT i;
1373 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
1374 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1375 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1376 return hr;
1378 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
1380 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
1381 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
1382 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
1383 return hr;
1387 swapchain_update_render_to_fbo(swapchain);
1388 swapchain_update_draw_bindings(swapchain);
1390 return WINED3D_OK;