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
);
81 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
83 wined3d_device_restore_fullscreen_window(swapchain
->device
, swapchain
->device_window
,
84 &swapchain
->original_window_rect
);
85 wined3d_device_release_focus_window(swapchain
->device
);
89 if (swapchain
->backup_dc
)
91 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain
->backup_wnd
, swapchain
->backup_dc
);
93 wined3d_release_dc(swapchain
->backup_wnd
, swapchain
->backup_dc
);
94 DestroyWindow(swapchain
->backup_wnd
);
98 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
100 ULONG refcount
= InterlockedIncrement(&swapchain
->ref
);
102 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
107 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
109 ULONG refcount
= InterlockedDecrement(&swapchain
->ref
);
111 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
115 swapchain_cleanup(swapchain
);
116 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
117 HeapFree(GetProcessHeap(), 0, swapchain
);
123 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
125 TRACE("swapchain %p.\n", swapchain
);
127 return swapchain
->parent
;
130 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
133 window
= swapchain
->device_window
;
134 if (window
== swapchain
->win_handle
)
137 TRACE("Setting swapchain %p window from %p to %p.\n",
138 swapchain
, swapchain
->win_handle
, window
);
139 swapchain
->win_handle
= window
;
142 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
143 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
, DWORD flags
)
147 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, flags %#x.\n",
148 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
149 dst_window_override
, flags
);
152 FIXME("Ignoring flags %#x.\n", flags
);
154 if (!swapchain
->back_buffers
)
156 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
157 return WINED3DERR_INVALIDCALL
;
162 SetRect(&s
, 0, 0, swapchain
->desc
.backbuffer_width
,
163 swapchain
->desc
.backbuffer_height
);
169 GetClientRect(swapchain
->win_handle
, &d
);
173 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
174 dst_rect
, dst_window_override
, flags
);
179 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
180 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
182 RECT src_rect
, dst_rect
;
184 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
186 SetRect(&src_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
, swapchain
->front_buffer
->resource
.height
);
189 if (swapchain
->desc
.windowed
)
191 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
192 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
193 wine_dbgstr_rect(&dst_rect
));
196 return wined3d_texture_blt(dst_texture
, sub_resource_idx
, &dst_rect
,
197 swapchain
->front_buffer
, 0, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
200 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
201 UINT back_buffer_idx
)
203 TRACE("swapchain %p, back_buffer_idx %u.\n",
204 swapchain
, back_buffer_idx
);
206 /* Return invalid if there is no backbuffer array, otherwise it will
207 * crash when ddraw is used (there swapchain->back_buffers is always
208 * NULL). We need this because this function is called from
209 * stateblock_init_default_state() to get the default scissorrect
211 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->desc
.backbuffer_count
)
213 WARN("Invalid back buffer index.\n");
214 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
215 * here in wined3d to avoid problems in other libs. */
219 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
221 return swapchain
->back_buffers
[back_buffer_idx
];
224 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
225 struct wined3d_raster_status
*raster_status
)
227 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
229 return wined3d_get_adapter_raster_status(swapchain
->device
->wined3d
,
230 swapchain
->device
->adapter
->ordinal
, raster_status
);
233 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
234 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
238 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
240 hr
= wined3d_get_adapter_display_mode(swapchain
->device
->wined3d
,
241 swapchain
->device
->adapter
->ordinal
, mode
, rotation
);
243 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
244 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
249 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
251 TRACE("swapchain %p.\n", swapchain
);
253 return swapchain
->device
;
256 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
257 struct wined3d_swapchain_desc
*desc
)
259 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
261 *desc
= swapchain
->desc
;
264 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
265 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
269 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
272 FIXME("Ignoring flags %#x.\n", flags
);
274 dc
= GetDCEx(swapchain
->device_window
, 0, DCX_USESTYLE
| DCX_CACHE
);
275 SetDeviceGammaRamp(dc
, (void *)ramp
);
276 ReleaseDC(swapchain
->device_window
, dc
);
281 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
283 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
284 swapchain
->palette
= palette
;
287 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
288 struct wined3d_gamma_ramp
*ramp
)
292 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
294 dc
= GetDCEx(swapchain
->device_window
, 0, DCX_USESTYLE
| DCX_CACHE
);
295 GetDeviceGammaRamp(dc
, ramp
);
296 ReleaseDC(swapchain
->device_window
, dc
);
301 /* A GL context is provided by the caller */
302 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
303 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
305 struct wined3d_texture
*texture
= swapchain
->back_buffers
[0];
306 struct wined3d_surface
*back_buffer
= texture
->sub_resources
[0].u
.surface
;
307 struct wined3d_surface
*front_buffer
= swapchain
->front_buffer
->sub_resources
[0].u
.surface
;
308 UINT src_w
= src_rect
->right
- src_rect
->left
;
309 UINT src_h
= src_rect
->bottom
- src_rect
->top
;
311 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
315 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
316 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
318 if (src_w
== dst_rect
->right
- dst_rect
->left
&& src_h
== dst_rect
->bottom
- dst_rect
->top
)
319 gl_filter
= GL_NEAREST
;
321 gl_filter
= GL_LINEAR
;
323 GetClientRect(swapchain
->win_handle
, &win_rect
);
324 win_h
= win_rect
.bottom
- win_rect
.top
;
326 if (gl_info
->fbo_ops
.glBlitFramebuffer
&& is_identity_fixup(texture
->resource
.format
->color_fixup
))
328 DWORD location
= WINED3D_LOCATION_TEXTURE_RGB
;
330 if (texture
->resource
.multisample_type
)
332 location
= WINED3D_LOCATION_RB_RESOLVED
;
333 wined3d_texture_load_location(texture
, 0, context
, location
);
336 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, back_buffer
, NULL
, location
);
337 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_COLOR_ATTACHMENT0
);
338 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
340 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, front_buffer
, NULL
, WINED3D_LOCATION_DRAWABLE
);
341 context_set_draw_buffer(context
, GL_BACK
);
342 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
344 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
345 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
346 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
347 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
348 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
350 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
351 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
353 /* Note that the texture is upside down */
354 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
,
355 dst_rect
->left
, win_h
- dst_rect
->top
, dst_rect
->right
, win_h
- dst_rect
->bottom
,
356 GL_COLOR_BUFFER_BIT
, gl_filter
);
357 checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
361 struct wined3d_device
*device
= swapchain
->device
;
362 struct wined3d_context
*context2
;
363 float tex_left
= src_rect
->left
;
364 float tex_top
= src_rect
->top
;
365 float tex_right
= src_rect
->right
;
366 float tex_bottom
= src_rect
->bottom
;
368 context2
= context_acquire(device
, back_buffer
);
369 context_apply_blit_state(context2
, device
);
371 if (texture
->flags
& WINED3D_TEXTURE_NORMALIZED_COORDS
)
373 tex_left
/= texture
->pow2_width
;
374 tex_right
/= texture
->pow2_width
;
375 tex_top
/= texture
->pow2_height
;
376 tex_bottom
/= texture
->pow2_height
;
379 if (is_complex_fixup(texture
->resource
.format
->color_fixup
))
380 gl_filter
= GL_NEAREST
;
382 context_apply_fbo_state_blit(context2
, GL_FRAMEBUFFER
, front_buffer
, NULL
, WINED3D_LOCATION_DRAWABLE
);
383 context_bind_texture(context2
, back_buffer
->texture_target
, texture
->texture_rgb
.name
);
385 /* Set up the texture. The surface is not in a wined3d_texture
386 * container, so there are no D3D texture settings to dirtify. */
387 device
->blitter
->set_shader(device
->blit_priv
, context2
, back_buffer
, NULL
);
388 gl_info
->gl_ops
.gl
.p_glTexParameteri(back_buffer
->texture_target
, GL_TEXTURE_MIN_FILTER
, gl_filter
);
389 gl_info
->gl_ops
.gl
.p_glTexParameteri(back_buffer
->texture_target
, GL_TEXTURE_MAG_FILTER
, gl_filter
);
391 context_set_draw_buffer(context
, GL_BACK
);
393 /* Set the viewport to the destination rectandle, disable any projection
394 * transformation set up by context_apply_blit_state(), and draw a
395 * (-1,-1)-(1,1) quad.
397 * Back up viewport and matrix to avoid breaking last_was_blit
399 * Note that context_apply_blit_state() set up viewport and ortho to
400 * match the surface size - we want the GL drawable(=window) size. */
401 gl_info
->gl_ops
.gl
.p_glPushAttrib(GL_VIEWPORT_BIT
);
402 gl_info
->gl_ops
.gl
.p_glViewport(dst_rect
->left
, win_h
- dst_rect
->bottom
,
403 dst_rect
->right
, win_h
- dst_rect
->top
);
404 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
405 gl_info
->gl_ops
.gl
.p_glPushMatrix();
406 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
408 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
410 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_bottom
);
411 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, -1);
414 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_top
);
415 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, 1);
418 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_top
);
419 gl_info
->gl_ops
.gl
.p_glVertex2i(1, 1);
422 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_bottom
);
423 gl_info
->gl_ops
.gl
.p_glVertex2i(1, -1);
424 gl_info
->gl_ops
.gl
.p_glEnd();
426 gl_info
->gl_ops
.gl
.p_glPopMatrix();
427 gl_info
->gl_ops
.gl
.p_glPopAttrib();
429 device
->blitter
->unset_shader(context
->gl_info
);
430 checkGLcall("Swapchain present blit(manual)\n");
432 context_release(context2
);
436 /* Context activation is done by the caller. */
437 static void wined3d_swapchain_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
439 struct wined3d_texture_sub_resource
*sub_resource
;
440 struct wined3d_texture
*texture
, *texture_prev
;
441 struct gl_texture tex0
;
445 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
447 if (swapchain
->desc
.backbuffer_count
< 2 || !swapchain
->render_to_fbo
)
450 texture_prev
= swapchain
->back_buffers
[0];
452 /* Back buffer 0 is already in the draw binding. */
453 tex0
= texture_prev
->texture_rgb
;
454 rb0
= texture_prev
->rb_multisample
;
455 locations0
= texture_prev
->sub_resources
[0].locations
;
457 for (i
= 1; i
< swapchain
->desc
.backbuffer_count
; ++i
)
459 texture
= swapchain
->back_buffers
[i
];
460 sub_resource
= &texture
->sub_resources
[0];
462 if (!(sub_resource
->locations
& supported_locations
))
463 wined3d_texture_load_location(texture
, 0, context
, texture
->resource
.draw_binding
);
465 texture_prev
->texture_rgb
= texture
->texture_rgb
;
466 texture_prev
->rb_multisample
= texture
->rb_multisample
;
468 wined3d_texture_validate_location(texture_prev
, 0, sub_resource
->locations
& supported_locations
);
469 wined3d_texture_invalidate_location(texture_prev
, 0, ~(sub_resource
->locations
& supported_locations
));
471 texture_prev
= texture
;
474 texture_prev
->texture_rgb
= tex0
;
475 texture_prev
->rb_multisample
= rb0
;
477 wined3d_texture_validate_location(texture_prev
, 0, locations0
& supported_locations
);
478 wined3d_texture_invalidate_location(texture_prev
, 0, ~(locations0
& supported_locations
));
480 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
483 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
484 const RECT
*src_rect
, const RECT
*dst_rect
, DWORD flags
)
486 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
487 const struct wined3d_fb_state
*fb
= &swapchain
->device
->fb
;
488 const struct wined3d_gl_info
*gl_info
;
489 struct wined3d_texture
*logo_texture
;
490 struct wined3d_context
*context
;
493 context
= context_acquire(swapchain
->device
, back_buffer
->sub_resources
[0].u
.surface
);
496 context_release(context
);
497 WARN("Invalid context, skipping present.\n");
501 gl_info
= context
->gl_info
;
503 if ((logo_texture
= swapchain
->device
->logo_texture
))
505 RECT rect
= {0, 0, logo_texture
->resource
.width
, logo_texture
->resource
.height
};
507 /* Blit the logo into the upper left corner of the drawable. */
508 wined3d_texture_blt(back_buffer
, 0, &rect
, logo_texture
, 0, &rect
,
509 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
512 if (swapchain
->device
->bCursorVisible
&& swapchain
->device
->cursor_texture
513 && !swapchain
->device
->hardwareCursor
)
517 swapchain
->device
->xScreenSpace
- swapchain
->device
->xHotSpot
,
518 swapchain
->device
->yScreenSpace
- swapchain
->device
->yHotSpot
,
519 swapchain
->device
->xScreenSpace
+ swapchain
->device
->cursorWidth
- swapchain
->device
->xHotSpot
,
520 swapchain
->device
->yScreenSpace
+ swapchain
->device
->cursorHeight
- swapchain
->device
->yHotSpot
,
525 swapchain
->device
->cursor_texture
->resource
.width
,
526 swapchain
->device
->cursor_texture
->resource
.height
528 const RECT clip_rect
= {0, 0, back_buffer
->resource
.width
, back_buffer
->resource
.height
};
530 TRACE("Rendering the software cursor.\n");
532 if (swapchain
->desc
.windowed
)
533 MapWindowPoints(NULL
, swapchain
->win_handle
, (POINT
*)&dst_rect
, 2);
534 if (wined3d_clip_blit(&clip_rect
, &dst_rect
, &src_rect
))
535 wined3d_texture_blt(back_buffer
, 0, &dst_rect
,
536 swapchain
->device
->cursor_texture
, 0, &src_rect
,
537 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
540 TRACE("Presenting HDC %p.\n", context
->hdc
);
542 if (!(render_to_fbo
= swapchain
->render_to_fbo
)
543 && (src_rect
->left
|| src_rect
->top
544 || src_rect
->right
!= swapchain
->desc
.backbuffer_width
545 || src_rect
->bottom
!= swapchain
->desc
.backbuffer_height
546 || dst_rect
->left
|| dst_rect
->top
547 || dst_rect
->right
!= swapchain
->desc
.backbuffer_width
548 || dst_rect
->bottom
!= swapchain
->desc
.backbuffer_height
))
549 render_to_fbo
= TRUE
;
551 /* Rendering to a window of different size, presenting partial rectangles,
552 * or rendering to a different window needs help from FBO_blit or a textured
553 * draw. Render the swapchain to a FBO in the future.
555 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
556 * all these issues - this fails if the window is smaller than the backbuffer.
558 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
560 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_TEXTURE_RGB
);
561 wined3d_texture_invalidate_location(back_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
562 swapchain
->render_to_fbo
= TRUE
;
563 swapchain_update_draw_bindings(swapchain
);
567 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
570 if (swapchain
->render_to_fbo
)
572 static unsigned int once
;
574 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
&& !once
++)
575 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
577 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
580 if (swapchain
->num_contexts
> 1)
581 gl_info
->gl_ops
.gl
.p_glFinish();
583 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
584 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context
->hdc
);
586 wined3d_swapchain_rotate(swapchain
, context
);
588 TRACE("SwapBuffers called, Starting new frame\n");
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. */
614 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
)
615 wined3d_texture_validate_location(swapchain
->back_buffers
[swapchain
->desc
.backbuffer_count
- 1],
616 0, WINED3D_LOCATION_DISCARDED
);
618 if (fb
->depth_stencil
)
620 struct wined3d_surface
*ds
= wined3d_rendertarget_view_get_surface(fb
->depth_stencil
);
622 if (ds
&& (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
623 || ds
->container
->flags
& WINED3D_TEXTURE_DISCARD
))
625 surface_modify_ds_location(ds
, WINED3D_LOCATION_DISCARDED
,
626 fb
->depth_stencil
->width
, fb
->depth_stencil
->height
);
627 if (ds
== swapchain
->device
->onscreen_depth_stencil
)
629 wined3d_texture_decref(swapchain
->device
->onscreen_depth_stencil
->container
);
630 swapchain
->device
->onscreen_depth_stencil
= NULL
;
635 context_release(context
);
638 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
640 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
641 struct wined3d_context
*context
;
643 context
= context_acquire(swapchain
->device
, front_buffer
->sub_resources
[0].u
.surface
);
644 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
645 context_release(context
);
646 SetRectEmpty(&swapchain
->front_buffer_update
);
649 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
651 swapchain_gl_present
,
652 swapchain_gl_frontbuffer_updated
,
655 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
657 struct wined3d_surface
*front
;
658 POINT offset
= {0, 0};
663 TRACE("swapchain %p.\n", swapchain
);
665 front
= swapchain
->front_buffer
->sub_resources
[0].u
.surface
;
666 if (swapchain
->palette
)
667 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
669 if (front
->container
->resource
.map_count
)
670 ERR("Trying to blit a mapped surface.\n");
672 TRACE("Copying surface %p to screen.\n", front
);
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
));
685 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
686 swapchain
->front_buffer
->resource
.height
);
687 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
689 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
690 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
691 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
692 ReleaseDC(window
, dst_dc
);
694 SetRectEmpty(&swapchain
->front_buffer_update
);
697 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
698 const RECT
*src_rect
, const RECT
*dst_rect
, DWORD flags
)
700 struct wined3d_surface
*front
, *back
;
705 front
= swapchain
->front_buffer
->sub_resources
[0].u
.surface
;
706 back
= swapchain
->back_buffers
[0]->sub_resources
[0].u
.surface
;
708 /* Flip the surface data. */
710 bitmap
= front
->bitmap
;
711 data
= front
->container
->resource
.heap_memory
;
713 front
->dc
= back
->dc
;
714 front
->bitmap
= back
->bitmap
;
715 front
->container
->resource
.heap_memory
= back
->container
->resource
.heap_memory
;
718 back
->bitmap
= bitmap
;
719 back
->container
->resource
.heap_memory
= data
;
724 static LONG prev_time
, frames
;
725 DWORD time
= GetTickCount();
729 /* every 1.5 seconds */
730 if (time
- prev_time
> 1500)
732 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
738 SetRect(&swapchain
->front_buffer_update
, 0, 0,
739 swapchain
->front_buffer
->resource
.width
,
740 swapchain
->front_buffer
->resource
.height
);
741 swapchain_gdi_frontbuffer_updated(swapchain
);
744 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
746 swapchain_gdi_present
,
747 swapchain_gdi_frontbuffer_updated
,
750 static void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
754 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
757 if (!swapchain
->desc
.backbuffer_count
)
759 TRACE("Single buffered rendering.\n");
760 swapchain
->render_to_fbo
= FALSE
;
764 GetClientRect(swapchain
->win_handle
, &client_rect
);
766 TRACE("Backbuffer %ux%u, window %ux%u.\n",
767 swapchain
->desc
.backbuffer_width
,
768 swapchain
->desc
.backbuffer_height
,
769 client_rect
.right
, client_rect
.bottom
);
770 TRACE("Multisample type %#x, quality %#x.\n",
771 swapchain
->desc
.multisample_type
,
772 swapchain
->desc
.multisample_quality
);
774 if (!wined3d_settings
.always_offscreen
&& !swapchain
->desc
.multisample_type
775 && swapchain
->desc
.backbuffer_width
== client_rect
.right
776 && swapchain
->desc
.backbuffer_height
== client_rect
.bottom
)
778 TRACE("Backbuffer dimensions match window dimensions, rendering onscreen.\n");
779 swapchain
->render_to_fbo
= FALSE
;
783 TRACE("Rendering to FBO.\n");
784 swapchain
->render_to_fbo
= TRUE
;
787 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
788 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, DWORD
*quality
)
790 const struct wined3d_gl_info
*gl_info
;
791 const struct wined3d_format
*format
;
792 enum wined3d_multisample_type t
;
794 if (wined3d_settings
.sample_count
== ~0u)
797 gl_info
= &swapchain
->device
->adapter
->gl_info
;
798 if (!(format
= wined3d_get_format(gl_info
, format_id
)))
801 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
802 while (!(format
->multisample_types
& 1u << (t
- 1)))
804 TRACE("Using sample count %u.\n", t
);
809 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
810 struct wined3d_swapchain_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
812 const struct wined3d_adapter
*adapter
= device
->adapter
;
813 struct wined3d_resource_desc texture_desc
;
814 BOOL displaymode_set
= FALSE
;
820 if (desc
->backbuffer_count
> 1)
822 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
823 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
826 if (device
->wined3d
->flags
& WINED3D_NO3D
)
827 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
829 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
831 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
833 swapchain
->device
= device
;
834 swapchain
->parent
= parent
;
835 swapchain
->parent_ops
= parent_ops
;
837 swapchain
->win_handle
= window
;
838 swapchain
->device_window
= window
;
840 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
,
841 adapter
->ordinal
, &swapchain
->original_mode
, NULL
)))
843 ERR("Failed to get current display mode, hr %#x.\n", hr
);
846 GetWindowRect(window
, &swapchain
->original_window_rect
);
848 GetClientRect(window
, &client_rect
);
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
);
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? */
906 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
908 /* Change the display settings */
909 swapchain
->d3d_mode
.width
= desc
->backbuffer_width
;
910 swapchain
->d3d_mode
.height
= desc
->backbuffer_height
;
911 swapchain
->d3d_mode
.format_id
= desc
->backbuffer_format
;
912 swapchain
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
913 swapchain
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
915 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
,
916 adapter
->ordinal
, &swapchain
->d3d_mode
)))
918 WARN("Failed to set display mode, hr %#x.\n", hr
);
921 displaymode_set
= TRUE
;
925 swapchain
->d3d_mode
= swapchain
->original_mode
;
929 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
931 static const enum wined3d_format_id formats
[] =
933 WINED3DFMT_D24_UNORM_S8_UINT
,
934 WINED3DFMT_D32_UNORM
,
935 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
936 WINED3DFMT_D16_UNORM
,
937 WINED3DFMT_S1_UINT_D15_UNORM
940 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
942 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
943 if (!swapchain
->context
)
945 ERR("Failed to create the context array.\n");
949 swapchain
->num_contexts
= 1;
951 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
952 * You are able to add a depth + stencil surface at a later stage when you need it.
953 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
954 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
955 * context, need torecreate shaders, textures and other resources.
957 * The context manager already takes care of the state problem and for the other tasks code from Reset
958 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
959 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
960 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
961 * issue needs to be fixed. */
962 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
964 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
]);
965 swapchain
->context
[0] = context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
);
966 if (swapchain
->context
[0]) break;
967 TRACE("Depth stencil format %s is not supported, trying next format\n",
968 debug_d3dformat(formats
[i
]));
971 if (!swapchain
->context
[0])
973 WARN("Failed to create context.\n");
974 hr
= WINED3DERR_NOTAVAILABLE
;
978 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
979 && (!desc
->enable_auto_depth_stencil
980 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
982 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
984 context_release(swapchain
->context
[0]);
985 swapchain_update_swap_interval(swapchain
);
988 if (swapchain
->desc
.backbuffer_count
> 0)
990 if (!(swapchain
->back_buffers
= wined3d_calloc(swapchain
->desc
.backbuffer_count
,
991 sizeof(*swapchain
->back_buffers
))))
993 ERR("Failed to allocate backbuffer array memory.\n");
998 texture_desc
.usage
|= WINED3DUSAGE_RENDERTARGET
;
999 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1001 TRACE("Creating back buffer %u.\n", i
);
1002 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1003 parent
, &texture_desc
, &swapchain
->back_buffers
[i
])))
1005 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1006 swapchain
->desc
.backbuffer_count
= i
;
1009 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1013 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1014 if (desc
->enable_auto_depth_stencil
&& !(device
->wined3d
->flags
& WINED3D_NO3D
))
1016 TRACE("Creating depth/stencil buffer.\n");
1017 if (!device
->auto_depth_stencil_view
)
1019 struct wined3d_texture
*ds
;
1020 struct wined3d_rendertarget_view_desc desc
;
1022 texture_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
1023 texture_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
1025 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1026 device
->device_parent
, &texture_desc
, &ds
)))
1028 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1032 desc
.format_id
= ds
->resource
.format
->id
;
1033 desc
.u
.texture
.level_idx
= 0;
1034 desc
.u
.texture
.layer_idx
= 0;
1035 desc
.u
.texture
.layer_count
= 1;
1036 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1037 &device
->auto_depth_stencil_view
);
1038 wined3d_texture_decref(ds
);
1041 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1047 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1052 if (displaymode_set
)
1054 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1055 adapter
->ordinal
, &swapchain
->original_mode
)))
1056 ERR("Failed to restore display mode.\n");
1060 if (swapchain
->back_buffers
)
1062 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1064 if (swapchain
->back_buffers
[i
])
1066 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1067 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1070 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1073 if (swapchain
->context
)
1075 if (swapchain
->context
[0])
1077 context_release(swapchain
->context
[0]);
1078 context_destroy(device
, swapchain
->context
[0]);
1079 swapchain
->num_contexts
= 0;
1081 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1084 if (swapchain
->front_buffer
)
1086 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1087 wined3d_texture_decref(swapchain
->front_buffer
);
1093 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
, struct wined3d_swapchain_desc
*desc
,
1094 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_swapchain
**swapchain
)
1096 struct wined3d_swapchain
*object
;
1099 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1100 device
, desc
, parent
, parent_ops
, swapchain
);
1102 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1104 return E_OUTOFMEMORY
;
1106 hr
= swapchain_init(object
, device
, desc
, parent
, parent_ops
);
1109 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1110 HeapFree(GetProcessHeap(), 0, object
);
1114 TRACE("Created swapchain %p.\n", object
);
1115 *swapchain
= object
;
1120 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1122 struct wined3d_context
**ctx_array
;
1123 struct wined3d_context
*ctx
;
1125 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1127 if (!(ctx
= context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
)))
1129 ERR("Failed to create a new context for the swapchain\n");
1132 context_release(ctx
);
1134 if (!(ctx_array
= wined3d_calloc(swapchain
->num_contexts
+ 1, sizeof(*ctx_array
))))
1136 ERR("Out of memory when trying to allocate a new context array\n");
1137 context_destroy(swapchain
->device
, ctx
);
1140 memcpy(ctx_array
, swapchain
->context
, sizeof(*ctx_array
) * swapchain
->num_contexts
);
1141 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1142 ctx_array
[swapchain
->num_contexts
] = ctx
;
1143 swapchain
->context
= ctx_array
;
1144 swapchain
->num_contexts
++;
1146 TRACE("Returning context %p\n", ctx
);
1150 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1154 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1156 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1158 swapchain
->num_contexts
= 0;
1161 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1163 DWORD tid
= GetCurrentThreadId();
1166 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1168 if (swapchain
->context
[i
]->tid
== tid
)
1169 return swapchain
->context
[i
];
1172 /* Create a new context for the thread */
1173 return swapchain_create_context(swapchain
);
1176 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1178 if (!swapchain
->backup_dc
)
1180 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1182 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1183 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1185 ERR("Failed to create a window.\n");
1189 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1191 ERR("Failed to get a DC.\n");
1192 DestroyWindow(swapchain
->backup_wnd
);
1193 swapchain
->backup_wnd
= NULL
;
1198 return swapchain
->backup_dc
;
1201 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1205 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1207 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1209 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1213 void swapchain_update_swap_interval(struct wined3d_swapchain
*swapchain
)
1215 const struct wined3d_gl_info
*gl_info
;
1216 struct wined3d_context
*context
;
1219 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
->sub_resources
[0].u
.surface
);
1220 gl_info
= context
->gl_info
;
1222 switch (swapchain
->desc
.swap_interval
)
1224 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1227 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1228 case WINED3DPRESENT_INTERVAL_ONE
:
1231 case WINED3DPRESENT_INTERVAL_TWO
:
1234 case WINED3DPRESENT_INTERVAL_THREE
:
1237 case WINED3DPRESENT_INTERVAL_FOUR
:
1241 FIXME("Unhandled present interval %#x.\n", swapchain
->desc
.swap_interval
);
1245 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1247 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1248 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1249 swap_interval
, context
, GetLastError());
1252 context_release(context
);
1255 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1257 struct wined3d_device
*device
= swapchain
->device
;
1258 BOOL filter_messages
= device
->filter_messages
;
1260 /* This code is not protected by the wined3d mutex, so it may run while
1261 * wined3d_device_reset is active. Testing on Windows shows that changing
1262 * focus during resets and resetting during focus change events causes
1263 * the application to crash with an invalid memory access. */
1265 device
->filter_messages
= !(device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
);
1269 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1271 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1272 * the window but leaves the size untouched, d3d9 sets the size on an
1273 * invisible window, generates messages but doesn't change the window
1274 * properties. The implementation follows d3d9.
1276 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1277 * resume drawing after a focus loss. */
1278 SetWindowPos(swapchain
->device_window
, NULL
, 0, 0,
1279 swapchain
->desc
.backbuffer_width
, swapchain
->desc
.backbuffer_height
,
1280 SWP_NOACTIVATE
| SWP_NOZORDER
);
1283 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1285 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1286 device
->adapter
->ordinal
, &swapchain
->d3d_mode
)))
1287 ERR("Failed to set display mode.\n");
1292 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1293 device
->adapter
->ordinal
, NULL
)))
1294 ERR("Failed to set display mode.\n");
1296 swapchain
->reapply_mode
= TRUE
;
1298 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
)
1299 && IsWindowVisible(swapchain
->device_window
))
1300 ShowWindow(swapchain
->device_window
, SW_MINIMIZE
);
1303 device
->filter_messages
= filter_messages
;
1306 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1307 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1308 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1310 BOOL update_desc
= FALSE
;
1312 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1313 "multisample_type %#x, multisample_quality %#x.\n",
1314 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1315 multisample_type
, multisample_quality
);
1317 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1319 if (buffer_count
&& buffer_count
!= swapchain
->desc
.backbuffer_count
)
1320 FIXME("Cannot change the back buffer count yet.\n");
1322 if (!width
|| !height
)
1324 /* The application is requesting that either the swapchain width or
1325 * height be set to the corresponding dimension in the window's
1330 if (!swapchain
->desc
.windowed
)
1331 return WINED3DERR_INVALIDCALL
;
1333 if (!GetClientRect(swapchain
->device_window
, &client_rect
))
1335 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1336 return WINED3DERR_INVALIDCALL
;
1340 width
= client_rect
.right
;
1343 height
= client_rect
.bottom
;
1346 if (width
!= swapchain
->desc
.backbuffer_width
1347 || height
!= swapchain
->desc
.backbuffer_height
)
1349 swapchain
->desc
.backbuffer_width
= width
;
1350 swapchain
->desc
.backbuffer_height
= height
;
1354 if (format_id
== WINED3DFMT_UNKNOWN
)
1356 if (!swapchain
->desc
.windowed
)
1357 return WINED3DERR_INVALIDCALL
;
1358 format_id
= swapchain
->original_mode
.format_id
;
1361 if (format_id
!= swapchain
->desc
.backbuffer_format
)
1363 swapchain
->desc
.backbuffer_format
= format_id
;
1367 if (multisample_type
!= swapchain
->desc
.multisample_type
1368 || multisample_quality
!= swapchain
->desc
.multisample_quality
)
1370 swapchain
->desc
.multisample_type
= multisample_type
;
1371 swapchain
->desc
.multisample_quality
= multisample_quality
;
1380 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, swapchain
->desc
.backbuffer_width
,
1381 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1382 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1385 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1387 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], swapchain
->desc
.backbuffer_width
,
1388 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1389 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1394 swapchain_update_render_to_fbo(swapchain
);
1395 swapchain_update_draw_bindings(swapchain
);
1400 HRESULT CDECL
wined3d_swapchain_resize_target(struct wined3d_swapchain
*swapchain
,
1401 const struct wined3d_display_mode
*mode
)
1403 struct wined3d_device
*device
= swapchain
->device
;
1404 struct wined3d_display_mode current_mode
;
1405 RECT original_window_rect
, window_rect
;
1408 TRACE("swapchain %p, mode %p.\n", swapchain
, mode
);
1410 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
1412 if (swapchain
->desc
.windowed
)
1414 AdjustWindowRectEx(&window_rect
,
1415 GetWindowLongW(swapchain
->device_window
, GWL_STYLE
), FALSE
,
1416 GetWindowLongW(swapchain
->device_window
, GWL_EXSTYLE
));
1417 SetRect(&window_rect
, 0, 0,
1418 window_rect
.right
- window_rect
.left
, window_rect
.bottom
- window_rect
.top
);
1419 GetWindowRect(swapchain
->device_window
, &original_window_rect
);
1420 OffsetRect(&window_rect
, original_window_rect
.left
, original_window_rect
.top
);
1422 else if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1424 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, mode
)))
1426 WARN("Failed to set display mode, hr %#x.\n", hr
);
1427 return WINED3DERR_INVALIDCALL
;
1432 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
,
1433 ¤t_mode
, NULL
)))
1435 ERR("Failed to get display mode, hr %#x.\n", hr
);
1436 return WINED3DERR_INVALIDCALL
;
1439 SetRect(&window_rect
, 0, 0, current_mode
.width
, current_mode
.height
);
1442 MoveWindow(swapchain
->device_window
, window_rect
.left
, window_rect
.top
,
1443 window_rect
.right
- window_rect
.left
,
1444 window_rect
.bottom
- window_rect
.top
, TRUE
);
1449 HRESULT CDECL
wined3d_swapchain_set_fullscreen(struct wined3d_swapchain
*swapchain
,
1450 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
)
1452 struct wined3d_device
*device
= swapchain
->device
;
1453 struct wined3d_display_mode default_mode
;
1454 unsigned int width
, height
;
1457 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain
, swapchain_desc
, mode
);
1459 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1461 width
= swapchain_desc
->backbuffer_width
;
1462 height
= swapchain_desc
->backbuffer_height
;
1466 if (!swapchain_desc
->windowed
)
1468 default_mode
.width
= swapchain_desc
->backbuffer_width
;
1469 default_mode
.height
= swapchain_desc
->backbuffer_height
;
1470 default_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
1471 default_mode
.format_id
= swapchain_desc
->backbuffer_format
;
1472 default_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1476 default_mode
= swapchain
->original_mode
;
1478 mode
= &default_mode
;
1481 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, mode
)))
1483 WARN("Failed to set display mode, hr %#x.\n", hr
);
1484 return WINED3DERR_INVALIDCALL
;
1490 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1492 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
,
1493 &default_mode
, NULL
)))
1495 ERR("Failed to get display mode, hr %#x.\n", hr
);
1496 return WINED3DERR_INVALIDCALL
;
1498 mode
= &default_mode
;
1500 width
= mode
->width
;
1501 height
= mode
->height
;
1504 if (!swapchain_desc
->windowed
)
1506 if (swapchain
->desc
.windowed
)
1508 HWND focus_window
= device
->create_parms
.focus_window
;
1510 focus_window
= swapchain
->device_window
;
1511 if (FAILED(hr
= wined3d_device_acquire_focus_window(device
, focus_window
)))
1513 ERR("Failed to acquire focus window, hr %#x.\n", hr
);
1517 /* switch from windowed to fs */
1518 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
, width
, height
);
1522 /* Fullscreen -> fullscreen mode change */
1523 MoveWindow(swapchain
->device_window
, 0, 0, width
, height
, TRUE
);
1525 swapchain
->d3d_mode
= *mode
;
1527 else if (!swapchain
->desc
.windowed
)
1529 RECT
*window_rect
= NULL
;
1530 /* Fullscreen -> windowed switch */
1531 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
1532 window_rect
= &swapchain
->original_window_rect
;
1533 wined3d_device_restore_fullscreen_window(device
, swapchain
->device_window
, window_rect
);
1534 wined3d_device_release_focus_window(device
);
1537 swapchain
->desc
.windowed
= swapchain_desc
->windowed
;