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
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
)
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
;
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
);
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
);
108 swapchain_cleanup(swapchain
);
109 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
110 HeapFree(GetProcessHeap(), 0, swapchain
);
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
)
126 window
= swapchain
->device_window
;
127 if (window
== swapchain
->win_handle
)
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
)
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
);
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
;
155 SetRect(&s
, 0, 0, swapchain
->desc
.backbuffer_width
,
156 swapchain
->desc
.backbuffer_height
);
162 GetClientRect(swapchain
->win_handle
, &d
);
166 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
167 dst_rect
, dst_window_override
, flags
);
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
);
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
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. */
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
)
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
));
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
)
262 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
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
);
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
)
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
);
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
;
304 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
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
;
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");
354 struct wined3d_device
*device
= swapchain
->device
;
355 struct wined3d_context
*context2
;
356 float tex_left
= src_rect
->left
;
357 float tex_top
= src_rect
->top
;
358 float tex_right
= src_rect
->right
;
359 float tex_bottom
= src_rect
->bottom
;
361 context2
= context_acquire(device
, back_buffer
);
362 context_apply_blit_state(context2
, device
);
364 if (back_buffer
->container
->flags
& WINED3D_TEXTURE_NORMALIZED_COORDS
)
366 tex_left
/= back_buffer
->pow2Width
;
367 tex_right
/= back_buffer
->pow2Width
;
368 tex_top
/= back_buffer
->pow2Height
;
369 tex_bottom
/= back_buffer
->pow2Height
;
372 if (is_complex_fixup(texture
->resource
.format
->color_fixup
))
373 gl_filter
= GL_NEAREST
;
375 context_apply_fbo_state_blit(context2
, GL_FRAMEBUFFER
, front_buffer
, NULL
, WINED3D_LOCATION_DRAWABLE
);
376 context_bind_texture(context2
, back_buffer
->texture_target
, texture
->texture_rgb
.name
);
378 /* Set up the texture. The surface is not in a wined3d_texture
379 * container, so there are no D3D texture settings to dirtify. */
380 device
->blitter
->set_shader(device
->blit_priv
, context2
, back_buffer
, NULL
);
381 gl_info
->gl_ops
.gl
.p_glTexParameteri(back_buffer
->texture_target
, GL_TEXTURE_MIN_FILTER
, gl_filter
);
382 gl_info
->gl_ops
.gl
.p_glTexParameteri(back_buffer
->texture_target
, GL_TEXTURE_MAG_FILTER
, gl_filter
);
384 context_set_draw_buffer(context
, GL_BACK
);
386 /* Set the viewport to the destination rectandle, disable any projection
387 * transformation set up by context_apply_blit_state(), and draw a
388 * (-1,-1)-(1,1) quad.
390 * Back up viewport and matrix to avoid breaking last_was_blit
392 * Note that context_apply_blit_state() set up viewport and ortho to
393 * match the surface size - we want the GL drawable(=window) size. */
394 gl_info
->gl_ops
.gl
.p_glPushAttrib(GL_VIEWPORT_BIT
);
395 gl_info
->gl_ops
.gl
.p_glViewport(dst_rect
->left
, win_h
- dst_rect
->bottom
,
396 dst_rect
->right
, win_h
- dst_rect
->top
);
397 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
398 gl_info
->gl_ops
.gl
.p_glPushMatrix();
399 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
401 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
403 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_bottom
);
404 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, -1);
407 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_top
);
408 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, 1);
411 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_top
);
412 gl_info
->gl_ops
.gl
.p_glVertex2i(1, 1);
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
;
437 struct wined3d_surface
*surface
, *surface_prev
;
439 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
441 if (swapchain
->desc
.backbuffer_count
< 2 || !swapchain
->render_to_fbo
)
444 texture_prev
= swapchain
->back_buffers
[0];
445 surface_prev
= texture_prev
->sub_resources
[0].u
.surface
;
447 /* Back buffer 0 is already in the draw binding. */
448 tex0
= texture_prev
->texture_rgb
;
449 rb0
= surface_prev
->rb_multisample
;
450 locations0
= texture_prev
->sub_resources
[0].locations
;
452 for (i
= 1; i
< swapchain
->desc
.backbuffer_count
; ++i
)
454 texture
= swapchain
->back_buffers
[i
];
455 sub_resource
= &texture
->sub_resources
[0];
456 surface
= sub_resource
->u
.surface
;
458 if (!(sub_resource
->locations
& supported_locations
))
459 surface_load_location(surface
, context
, texture
->resource
.draw_binding
);
461 texture_prev
->texture_rgb
= texture
->texture_rgb
;
462 surface_prev
->rb_multisample
= surface
->rb_multisample
;
464 wined3d_texture_validate_location(texture_prev
, 0, sub_resource
->locations
& supported_locations
);
465 wined3d_texture_invalidate_location(texture_prev
, 0, ~(sub_resource
->locations
& supported_locations
));
467 texture_prev
= texture
;
468 surface_prev
= surface
;
471 texture_prev
->texture_rgb
= tex0
;
472 surface_prev
->rb_multisample
= rb0
;
474 wined3d_texture_validate_location(texture_prev
, 0, locations0
& supported_locations
);
475 wined3d_texture_invalidate_location(texture_prev
, 0, ~(locations0
& supported_locations
));
477 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
480 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
481 const RECT
*src_rect
, const RECT
*dst_rect
, DWORD flags
)
483 struct wined3d_surface
*back_buffer
= surface_from_resource(
484 wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
485 const struct wined3d_fb_state
*fb
= &swapchain
->device
->fb
;
486 const struct wined3d_gl_info
*gl_info
;
487 struct wined3d_texture
*logo_texture
;
488 struct wined3d_context
*context
;
491 context
= context_acquire(swapchain
->device
, back_buffer
);
494 context_release(context
);
495 WARN("Invalid context, skipping present.\n");
499 gl_info
= context
->gl_info
;
501 if ((logo_texture
= swapchain
->device
->logo_texture
))
503 RECT rect
= {0, 0, logo_texture
->resource
.width
, logo_texture
->resource
.height
};
505 /* Blit the logo into the upper left corner of the drawable. */
506 wined3d_texture_blt(swapchain
->back_buffers
[0], 0, &rect
, logo_texture
, 0, &rect
,
507 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
510 if (swapchain
->device
->bCursorVisible
&& swapchain
->device
->cursor_texture
511 && !swapchain
->device
->hardwareCursor
)
515 swapchain
->device
->xScreenSpace
- swapchain
->device
->xHotSpot
,
516 swapchain
->device
->yScreenSpace
- swapchain
->device
->yHotSpot
,
517 swapchain
->device
->xScreenSpace
+ swapchain
->device
->cursorWidth
- swapchain
->device
->xHotSpot
,
518 swapchain
->device
->yScreenSpace
+ swapchain
->device
->cursorHeight
- swapchain
->device
->yHotSpot
,
523 swapchain
->device
->cursor_texture
->resource
.width
,
524 swapchain
->device
->cursor_texture
->resource
.height
526 const RECT clip_rect
= {0, 0,
527 swapchain
->back_buffers
[0]->resource
.width
,
528 swapchain
->back_buffers
[0]->resource
.height
};
530 TRACE("Rendering the software cursor.\n");
532 if (swapchain
->desc
.windowed
)
533 MapWindowPoints(NULL
, swapchain
->win_handle
, (POINT
*)&dst_rect
, 2);
534 if (wined3d_clip_blit(&clip_rect
, &dst_rect
, &src_rect
))
535 wined3d_texture_blt(swapchain
->back_buffers
[0], 0, &dst_rect
,
536 swapchain
->device
->cursor_texture
, 0, &src_rect
,
537 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
540 TRACE("Presenting HDC %p.\n", context
->hdc
);
542 if (!(render_to_fbo
= swapchain
->render_to_fbo
)
543 && (src_rect
->left
|| src_rect
->top
544 || src_rect
->right
!= swapchain
->desc
.backbuffer_width
545 || src_rect
->bottom
!= swapchain
->desc
.backbuffer_height
546 || dst_rect
->left
|| dst_rect
->top
547 || dst_rect
->right
!= swapchain
->desc
.backbuffer_width
548 || dst_rect
->bottom
!= swapchain
->desc
.backbuffer_height
))
549 render_to_fbo
= TRUE
;
551 /* Rendering to a window of different size, presenting partial rectangles,
552 * or rendering to a different window needs help from FBO_blit or a textured
553 * draw. Render the swapchain to a FBO in the future.
555 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
556 * all these issues - this fails if the window is smaller than the backbuffer.
558 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
560 surface_load_location(back_buffer
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
561 wined3d_texture_invalidate_location(back_buffer
->container
, 0, WINED3D_LOCATION_DRAWABLE
);
562 swapchain
->render_to_fbo
= TRUE
;
563 swapchain_update_draw_bindings(swapchain
);
567 surface_load_location(back_buffer
, context
, back_buffer
->container
->resource
.draw_binding
);
570 if (swapchain
->render_to_fbo
)
572 static unsigned int once
;
574 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
&& !once
++)
575 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
577 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
580 if (swapchain
->num_contexts
> 1)
581 gl_info
->gl_ops
.gl
.p_glFinish();
583 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
584 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context
->hdc
);
586 wined3d_swapchain_rotate(swapchain
, context
);
588 TRACE("SwapBuffers called, Starting new frame\n");
592 DWORD time
= GetTickCount();
595 /* every 1.5 seconds */
596 if (time
- swapchain
->prev_time
> 1500)
598 TRACE_(fps
)("%p @ approx %.2ffps\n",
599 swapchain
, 1000.0 * swapchain
->frames
/ (time
- swapchain
->prev_time
));
600 swapchain
->prev_time
= time
;
601 swapchain
->frames
= 0;
605 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
606 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
607 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
608 * and INTEXTURE copies can keep their old content if they have any defined content.
609 * If the swapeffect is COPY, the content remains the same.
611 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
612 * up to date and hope WGL flipped front and back buffers and read this data into
613 * the FBO. Don't bother about this for now. */
615 if (fb
->depth_stencil
)
617 struct wined3d_surface
*ds
= wined3d_rendertarget_view_get_surface(fb
->depth_stencil
);
619 if (ds
&& (swapchain
->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
620 || ds
->flags
& SFLAG_DISCARD
))
622 surface_modify_ds_location(ds
, WINED3D_LOCATION_DISCARDED
,
623 fb
->depth_stencil
->width
, fb
->depth_stencil
->height
);
624 if (ds
== swapchain
->device
->onscreen_depth_stencil
)
626 wined3d_texture_decref(swapchain
->device
->onscreen_depth_stencil
->container
);
627 swapchain
->device
->onscreen_depth_stencil
= NULL
;
632 context_release(context
);
635 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
637 struct wined3d_surface
*surface
;
638 struct wined3d_context
*context
;
640 surface
= swapchain
->front_buffer
->sub_resources
[0].u
.surface
;
641 context
= context_acquire(swapchain
->device
, surface
);
642 surface_load_location(surface
, context
, surface
->container
->resource
.draw_binding
);
643 context_release(context
);
644 SetRectEmpty(&swapchain
->front_buffer_update
);
647 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
649 swapchain_gl_present
,
650 swapchain_gl_frontbuffer_updated
,
653 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
655 struct wined3d_surface
*front
;
656 POINT offset
= {0, 0};
661 TRACE("swapchain %p.\n", swapchain
);
663 front
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
664 if (swapchain
->palette
)
665 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->hDC
);
667 if (front
->resource
.map_count
)
668 ERR("Trying to blit a mapped surface.\n");
670 TRACE("Copying surface %p to screen.\n", front
);
672 surface_load_location(front
, NULL
, WINED3D_LOCATION_DIB
);
675 window
= swapchain
->win_handle
;
676 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
678 /* Front buffer coordinates are screen coordinates. Map them to the
679 * destination window if not fullscreened. */
680 if (swapchain
->desc
.windowed
)
681 ClientToScreen(window
, &offset
);
683 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
686 draw_rect
.right
= front
->resource
.width
;
688 draw_rect
.bottom
= front
->resource
.height
;
689 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
691 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
692 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
693 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
694 ReleaseDC(window
, dst_dc
);
696 SetRectEmpty(&swapchain
->front_buffer_update
);
699 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
700 const RECT
*src_rect
, const RECT
*dst_rect
, DWORD flags
)
702 struct wined3d_surface
*front
, *back
;
704 front
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
705 back
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
711 front
->hDC
= back
->hDC
;
715 /* Flip the DIBsection. */
718 tmp
= front
->dib
.DIBsection
;
719 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
720 back
->dib
.DIBsection
= tmp
;
723 /* Flip the surface data. */
727 tmp
= front
->dib
.bitmap_data
;
728 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
729 back
->dib
.bitmap_data
= tmp
;
731 if (front
->resource
.heap_memory
)
732 ERR("GDI Surface %p has heap memory allocated.\n", front
);
734 if (back
->resource
.heap_memory
)
735 ERR("GDI Surface %p has heap memory allocated.\n", back
);
741 static LONG prev_time
, frames
;
742 DWORD time
= GetTickCount();
746 /* every 1.5 seconds */
747 if (time
- prev_time
> 1500)
749 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
755 SetRect(&swapchain
->front_buffer_update
, 0, 0,
756 swapchain
->front_buffer
->resource
.width
,
757 swapchain
->front_buffer
->resource
.height
);
758 swapchain_gdi_frontbuffer_updated(swapchain
);
761 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
763 swapchain_gdi_present
,
764 swapchain_gdi_frontbuffer_updated
,
767 static void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
771 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
774 if (!swapchain
->desc
.backbuffer_count
)
776 TRACE("Single buffered rendering.\n");
777 swapchain
->render_to_fbo
= FALSE
;
781 GetClientRect(swapchain
->win_handle
, &client_rect
);
783 TRACE("Backbuffer %ux%u, window %ux%u.\n",
784 swapchain
->desc
.backbuffer_width
,
785 swapchain
->desc
.backbuffer_height
,
786 client_rect
.right
, client_rect
.bottom
);
787 TRACE("Multisample type %#x, quality %#x.\n",
788 swapchain
->desc
.multisample_type
,
789 swapchain
->desc
.multisample_quality
);
791 if (!wined3d_settings
.always_offscreen
&& !swapchain
->desc
.multisample_type
792 && swapchain
->desc
.backbuffer_width
== client_rect
.right
793 && swapchain
->desc
.backbuffer_height
== client_rect
.bottom
)
795 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
796 swapchain
->render_to_fbo
= FALSE
;
800 TRACE("Rendering to FBO.\n");
801 swapchain
->render_to_fbo
= TRUE
;
804 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
805 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, DWORD
*quality
)
807 const struct wined3d_gl_info
*gl_info
;
808 const struct wined3d_format
*format
;
809 enum wined3d_multisample_type t
;
811 if (wined3d_settings
.sample_count
== ~0u)
814 gl_info
= &swapchain
->device
->adapter
->gl_info
;
815 if (!(format
= wined3d_get_format(gl_info
, format_id
)))
818 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
819 while (!(format
->multisample_types
& 1u << (t
- 1)))
821 TRACE("Using sample count %u.\n", t
);
826 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
827 struct wined3d_swapchain_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
829 const struct wined3d_adapter
*adapter
= device
->adapter
;
830 struct wined3d_resource_desc texture_desc
;
831 BOOL displaymode_set
= FALSE
;
837 if (desc
->backbuffer_count
> 1)
839 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
840 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
843 if (device
->wined3d
->flags
& WINED3D_NO3D
)
844 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
846 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
848 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
850 swapchain
->device
= device
;
851 swapchain
->parent
= parent
;
852 swapchain
->parent_ops
= parent_ops
;
854 swapchain
->win_handle
= window
;
855 swapchain
->device_window
= window
;
857 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
,
858 adapter
->ordinal
, &swapchain
->original_mode
, NULL
)))
860 ERR("Failed to get current display mode, hr %#x.\n", hr
);
864 GetClientRect(window
, &client_rect
);
866 && (!desc
->backbuffer_width
|| !desc
->backbuffer_height
867 || desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
))
870 if (!desc
->backbuffer_width
)
872 desc
->backbuffer_width
= client_rect
.right
;
873 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
876 if (!desc
->backbuffer_height
)
878 desc
->backbuffer_height
= client_rect
.bottom
;
879 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
882 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
884 desc
->backbuffer_format
= swapchain
->original_mode
.format_id
;
885 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->original_mode
.format_id
));
888 swapchain
->desc
= *desc
;
889 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->desc
.backbuffer_format
,
890 &swapchain
->desc
.multisample_type
, &swapchain
->desc
.multisample_quality
);
891 swapchain_update_render_to_fbo(swapchain
);
893 TRACE("Creating front buffer.\n");
895 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
896 texture_desc
.format
= swapchain
->desc
.backbuffer_format
;
897 texture_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
898 texture_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
899 texture_desc
.usage
= 0;
900 texture_desc
.pool
= WINED3D_POOL_DEFAULT
;
901 texture_desc
.width
= swapchain
->desc
.backbuffer_width
;
902 texture_desc
.height
= swapchain
->desc
.backbuffer_height
;
903 texture_desc
.depth
= 1;
904 texture_desc
.size
= 0;
906 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
907 parent
, &texture_desc
, &swapchain
->front_buffer
)))
909 WARN("Failed to create front buffer, hr %#x.\n", hr
);
913 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
914 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
916 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
917 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
920 /* MSDN says we're only allowed a single fullscreen swapchain per device,
921 * so we should really check to see if there is a fullscreen swapchain
922 * already. Does a single head count as full screen? */
926 /* Change the display settings */
927 swapchain
->d3d_mode
.width
= desc
->backbuffer_width
;
928 swapchain
->d3d_mode
.height
= desc
->backbuffer_height
;
929 swapchain
->d3d_mode
.format_id
= desc
->backbuffer_format
;
930 swapchain
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
931 swapchain
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
933 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, adapter
->ordinal
, &swapchain
->d3d_mode
)))
935 WARN("Failed to set display mode, hr %#x.\n", hr
);
938 displaymode_set
= TRUE
;
941 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
943 static const enum wined3d_format_id formats
[] =
945 WINED3DFMT_D24_UNORM_S8_UINT
,
946 WINED3DFMT_D32_UNORM
,
947 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
948 WINED3DFMT_D16_UNORM
,
949 WINED3DFMT_S1_UINT_D15_UNORM
952 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
954 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
955 if (!swapchain
->context
)
957 ERR("Failed to create the context array.\n");
961 swapchain
->num_contexts
= 1;
963 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
964 * You are able to add a depth + stencil surface at a later stage when you need it.
965 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
966 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
967 * context, need torecreate shaders, textures and other resources.
969 * The context manager already takes care of the state problem and for the other tasks code from Reset
970 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
971 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
972 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
973 * issue needs to be fixed. */
974 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
976 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
977 swapchain
->context
[0] = context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
);
978 if (swapchain
->context
[0]) break;
979 TRACE("Depth stencil format %s is not supported, trying next format\n",
980 debug_d3dformat(formats
[i
]));
983 if (!swapchain
->context
[0])
985 WARN("Failed to create context.\n");
986 hr
= WINED3DERR_NOTAVAILABLE
;
990 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
991 && (!desc
->enable_auto_depth_stencil
992 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
994 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
996 context_release(swapchain
->context
[0]);
999 if (swapchain
->desc
.backbuffer_count
> 0)
1001 swapchain
->back_buffers
= HeapAlloc(GetProcessHeap(), 0,
1002 sizeof(*swapchain
->back_buffers
) * swapchain
->desc
.backbuffer_count
);
1003 if (!swapchain
->back_buffers
)
1005 ERR("Failed to allocate backbuffer array memory.\n");
1010 texture_desc
.usage
|= WINED3DUSAGE_RENDERTARGET
;
1011 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1013 TRACE("Creating back buffer %u.\n", i
);
1014 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1015 parent
, &texture_desc
, &swapchain
->back_buffers
[i
])))
1017 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1018 swapchain
->desc
.backbuffer_count
= i
;
1021 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1025 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1026 if (desc
->enable_auto_depth_stencil
&& !(device
->wined3d
->flags
& WINED3D_NO3D
))
1028 TRACE("Creating depth/stencil buffer.\n");
1029 if (!device
->auto_depth_stencil_view
)
1031 struct wined3d_texture
*ds
;
1032 struct wined3d_rendertarget_view_desc desc
;
1034 texture_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
1035 texture_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
1037 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1038 device
->device_parent
, &texture_desc
, &ds
)))
1040 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1044 desc
.format_id
= ds
->resource
.format
->id
;
1045 desc
.u
.texture
.level_idx
= 0;
1046 desc
.u
.texture
.layer_idx
= 0;
1047 desc
.u
.texture
.layer_count
= 1;
1048 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1049 &device
->auto_depth_stencil_view
);
1050 wined3d_texture_decref(ds
);
1053 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1059 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1064 if (displaymode_set
)
1066 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1067 adapter
->ordinal
, &swapchain
->original_mode
)))
1068 ERR("Failed to restore display mode.\n");
1072 if (swapchain
->back_buffers
)
1074 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1076 if (swapchain
->back_buffers
[i
])
1078 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1079 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1082 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1085 if (swapchain
->context
)
1087 if (swapchain
->context
[0])
1089 context_release(swapchain
->context
[0]);
1090 context_destroy(device
, swapchain
->context
[0]);
1091 swapchain
->num_contexts
= 0;
1093 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1096 if (swapchain
->front_buffer
)
1098 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1099 wined3d_texture_decref(swapchain
->front_buffer
);
1105 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
, struct wined3d_swapchain_desc
*desc
,
1106 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_swapchain
**swapchain
)
1108 struct wined3d_swapchain
*object
;
1111 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1112 device
, desc
, parent
, parent_ops
, swapchain
);
1114 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1116 return E_OUTOFMEMORY
;
1118 hr
= swapchain_init(object
, device
, desc
, parent
, parent_ops
);
1121 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1122 HeapFree(GetProcessHeap(), 0, object
);
1126 TRACE("Created swapchain %p.\n", object
);
1127 *swapchain
= object
;
1132 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1134 struct wined3d_context
**newArray
;
1135 struct wined3d_context
*ctx
;
1137 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1139 if (!(ctx
= context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
)))
1141 ERR("Failed to create a new context for the swapchain\n");
1144 context_release(ctx
);
1146 newArray
= HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray
) * (swapchain
->num_contexts
+ 1));
1148 ERR("Out of memory when trying to allocate a new context array\n");
1149 context_destroy(swapchain
->device
, ctx
);
1152 memcpy(newArray
, swapchain
->context
, sizeof(*newArray
) * swapchain
->num_contexts
);
1153 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1154 newArray
[swapchain
->num_contexts
] = ctx
;
1155 swapchain
->context
= newArray
;
1156 swapchain
->num_contexts
++;
1158 TRACE("Returning context %p\n", ctx
);
1162 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1166 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1168 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1170 swapchain
->num_contexts
= 0;
1173 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1175 DWORD tid
= GetCurrentThreadId();
1178 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1180 if (swapchain
->context
[i
]->tid
== tid
)
1181 return swapchain
->context
[i
];
1184 /* Create a new context for the thread */
1185 return swapchain_create_context(swapchain
);
1188 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1190 if (!swapchain
->backup_dc
)
1192 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1194 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1195 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1197 ERR("Failed to create a window.\n");
1201 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1203 ERR("Failed to get a DC.\n");
1204 DestroyWindow(swapchain
->backup_wnd
);
1205 swapchain
->backup_wnd
= NULL
;
1210 return swapchain
->backup_dc
;
1213 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1217 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1219 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1221 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1225 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1227 struct wined3d_device
*device
= swapchain
->device
;
1228 BOOL filter_messages
= device
->filter_messages
;
1230 /* This code is not protected by the wined3d mutex, so it may run while
1231 * wined3d_device_reset is active. Testing on Windows shows that changing
1232 * focus during resets and resetting during focus change events causes
1233 * the application to crash with an invalid memory access. */
1235 device
->filter_messages
= !(device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
);
1239 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1241 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1242 * the window but leaves the size untouched, d3d9 sets the size on an
1243 * invisible window, generates messages but doesn't change the window
1244 * properties. The implementation follows d3d9.
1246 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1247 * resume drawing after a focus loss. */
1248 SetWindowPos(swapchain
->device_window
, NULL
, 0, 0,
1249 swapchain
->desc
.backbuffer_width
, swapchain
->desc
.backbuffer_height
,
1250 SWP_NOACTIVATE
| SWP_NOZORDER
);
1253 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1255 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1256 device
->adapter
->ordinal
, &swapchain
->d3d_mode
)))
1257 ERR("Failed to set display mode.\n");
1262 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1263 device
->adapter
->ordinal
, NULL
)))
1264 ERR("Failed to set display mode.\n");
1266 swapchain
->reapply_mode
= TRUE
;
1268 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
)
1269 && IsWindowVisible(swapchain
->device_window
))
1270 ShowWindow(swapchain
->device_window
, SW_MINIMIZE
);
1273 device
->filter_messages
= filter_messages
;
1276 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1277 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1278 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1280 BOOL update_desc
= FALSE
;
1282 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1283 "multisample_type %#x, multisample_quality %#x.\n",
1284 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1285 multisample_type
, multisample_quality
);
1287 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1289 if (buffer_count
&& buffer_count
!= swapchain
->desc
.backbuffer_count
)
1290 FIXME("Cannot change the back buffer count yet.\n");
1292 if (!width
|| !height
)
1294 /* The application is requesting that either the swapchain width or
1295 * height be set to the corresponding dimension in the window's
1300 if (!swapchain
->desc
.windowed
)
1301 return WINED3DERR_INVALIDCALL
;
1303 if (!GetClientRect(swapchain
->device_window
, &client_rect
))
1305 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1306 return WINED3DERR_INVALIDCALL
;
1310 width
= client_rect
.right
;
1313 height
= client_rect
.bottom
;
1316 if (width
!= swapchain
->desc
.backbuffer_width
1317 || height
!= swapchain
->desc
.backbuffer_height
)
1319 swapchain
->desc
.backbuffer_width
= width
;
1320 swapchain
->desc
.backbuffer_height
= height
;
1324 if (format_id
== WINED3DFMT_UNKNOWN
)
1326 if (!swapchain
->desc
.windowed
)
1327 return WINED3DERR_INVALIDCALL
;
1328 format_id
= swapchain
->original_mode
.format_id
;
1331 if (format_id
!= swapchain
->desc
.backbuffer_format
)
1333 swapchain
->desc
.backbuffer_format
= format_id
;
1337 if (multisample_type
!= swapchain
->desc
.multisample_type
1338 || multisample_quality
!= swapchain
->desc
.multisample_quality
)
1340 swapchain
->desc
.multisample_type
= multisample_type
;
1341 swapchain
->desc
.multisample_quality
= multisample_quality
;
1350 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, swapchain
->desc
.backbuffer_width
,
1351 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1352 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1355 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1357 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], swapchain
->desc
.backbuffer_width
,
1358 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1359 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1364 swapchain_update_render_to_fbo(swapchain
);
1365 swapchain_update_draw_bindings(swapchain
);