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
, texture
, 0);
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
);
390 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
391 gl_info
->gl_ops
.gl
.p_glTexParameteri(back_buffer
->texture_target
,
392 GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
394 context_set_draw_buffer(context
, GL_BACK
);
396 /* Set the viewport to the destination rectandle, disable any projection
397 * transformation set up by context_apply_blit_state(), and draw a
398 * (-1,-1)-(1,1) quad.
400 * Back up viewport and matrix to avoid breaking last_was_blit
402 * Note that context_apply_blit_state() set up viewport and ortho to
403 * match the surface size - we want the GL drawable(=window) size. */
404 gl_info
->gl_ops
.gl
.p_glPushAttrib(GL_VIEWPORT_BIT
);
405 gl_info
->gl_ops
.gl
.p_glViewport(dst_rect
->left
, win_h
- dst_rect
->bottom
,
406 dst_rect
->right
, win_h
- dst_rect
->top
);
407 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
408 gl_info
->gl_ops
.gl
.p_glPushMatrix();
409 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
411 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
413 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_bottom
);
414 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, -1);
417 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_left
, tex_top
);
418 gl_info
->gl_ops
.gl
.p_glVertex2i(-1, 1);
421 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_top
);
422 gl_info
->gl_ops
.gl
.p_glVertex2i(1, 1);
425 gl_info
->gl_ops
.gl
.p_glTexCoord2f(tex_right
, tex_bottom
);
426 gl_info
->gl_ops
.gl
.p_glVertex2i(1, -1);
427 gl_info
->gl_ops
.gl
.p_glEnd();
429 gl_info
->gl_ops
.gl
.p_glPopMatrix();
430 gl_info
->gl_ops
.gl
.p_glPopAttrib();
432 device
->blitter
->unset_shader(context
->gl_info
);
433 checkGLcall("Swapchain present blit(manual)\n");
435 context_release(context2
);
439 /* Context activation is done by the caller. */
440 static void wined3d_swapchain_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
442 struct wined3d_texture_sub_resource
*sub_resource
;
443 struct wined3d_texture
*texture
, *texture_prev
;
444 struct gl_texture tex0
;
448 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
450 if (swapchain
->desc
.backbuffer_count
< 2 || !swapchain
->render_to_fbo
)
453 texture_prev
= swapchain
->back_buffers
[0];
455 /* Back buffer 0 is already in the draw binding. */
456 tex0
= texture_prev
->texture_rgb
;
457 rb0
= texture_prev
->rb_multisample
;
458 locations0
= texture_prev
->sub_resources
[0].locations
;
460 for (i
= 1; i
< swapchain
->desc
.backbuffer_count
; ++i
)
462 texture
= swapchain
->back_buffers
[i
];
463 sub_resource
= &texture
->sub_resources
[0];
465 if (!(sub_resource
->locations
& supported_locations
))
466 wined3d_texture_load_location(texture
, 0, context
, texture
->resource
.draw_binding
);
468 texture_prev
->texture_rgb
= texture
->texture_rgb
;
469 texture_prev
->rb_multisample
= texture
->rb_multisample
;
471 wined3d_texture_validate_location(texture_prev
, 0, sub_resource
->locations
& supported_locations
);
472 wined3d_texture_invalidate_location(texture_prev
, 0, ~(sub_resource
->locations
& supported_locations
));
474 texture_prev
= texture
;
477 texture_prev
->texture_rgb
= tex0
;
478 texture_prev
->rb_multisample
= rb0
;
480 wined3d_texture_validate_location(texture_prev
, 0, locations0
& supported_locations
);
481 wined3d_texture_invalidate_location(texture_prev
, 0, ~(locations0
& supported_locations
));
483 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
486 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
487 const RECT
*src_rect
, const RECT
*dst_rect
, DWORD flags
)
489 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
490 const struct wined3d_fb_state
*fb
= &swapchain
->device
->fb
;
491 const struct wined3d_gl_info
*gl_info
;
492 struct wined3d_texture
*logo_texture
;
493 struct wined3d_context
*context
;
496 context
= context_acquire(swapchain
->device
, back_buffer
, 0);
499 context_release(context
);
500 WARN("Invalid context, skipping present.\n");
504 gl_info
= context
->gl_info
;
506 if ((logo_texture
= swapchain
->device
->logo_texture
))
508 RECT rect
= {0, 0, logo_texture
->resource
.width
, logo_texture
->resource
.height
};
510 /* Blit the logo into the upper left corner of the drawable. */
511 wined3d_texture_blt(back_buffer
, 0, &rect
, logo_texture
, 0, &rect
,
512 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
515 if (swapchain
->device
->bCursorVisible
&& swapchain
->device
->cursor_texture
516 && !swapchain
->device
->hardwareCursor
)
520 swapchain
->device
->xScreenSpace
- swapchain
->device
->xHotSpot
,
521 swapchain
->device
->yScreenSpace
- swapchain
->device
->yHotSpot
,
522 swapchain
->device
->xScreenSpace
+ swapchain
->device
->cursorWidth
- swapchain
->device
->xHotSpot
,
523 swapchain
->device
->yScreenSpace
+ swapchain
->device
->cursorHeight
- swapchain
->device
->yHotSpot
,
528 swapchain
->device
->cursor_texture
->resource
.width
,
529 swapchain
->device
->cursor_texture
->resource
.height
531 const RECT clip_rect
= {0, 0, back_buffer
->resource
.width
, back_buffer
->resource
.height
};
533 TRACE("Rendering the software cursor.\n");
535 if (swapchain
->desc
.windowed
)
536 MapWindowPoints(NULL
, swapchain
->win_handle
, (POINT
*)&dst_rect
, 2);
537 if (wined3d_clip_blit(&clip_rect
, &dst_rect
, &src_rect
))
538 wined3d_texture_blt(back_buffer
, 0, &dst_rect
,
539 swapchain
->device
->cursor_texture
, 0, &src_rect
,
540 WINED3D_BLT_ALPHA_TEST
, NULL
, WINED3D_TEXF_POINT
);
543 TRACE("Presenting HDC %p.\n", context
->hdc
);
545 if (!(render_to_fbo
= swapchain
->render_to_fbo
)
546 && (src_rect
->left
|| src_rect
->top
547 || src_rect
->right
!= swapchain
->desc
.backbuffer_width
548 || src_rect
->bottom
!= swapchain
->desc
.backbuffer_height
549 || dst_rect
->left
|| dst_rect
->top
550 || dst_rect
->right
!= swapchain
->desc
.backbuffer_width
551 || dst_rect
->bottom
!= swapchain
->desc
.backbuffer_height
))
552 render_to_fbo
= TRUE
;
554 /* Rendering to a window of different size, presenting partial rectangles,
555 * or rendering to a different window needs help from FBO_blit or a textured
556 * draw. Render the swapchain to a FBO in the future.
558 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
559 * all these issues - this fails if the window is smaller than the backbuffer.
561 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
563 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_TEXTURE_RGB
);
564 wined3d_texture_invalidate_location(back_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
565 swapchain
->render_to_fbo
= TRUE
;
566 swapchain_update_draw_bindings(swapchain
);
570 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
573 if (swapchain
->render_to_fbo
)
575 static unsigned int once
;
577 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_FLIP
&& !once
++)
578 FIXME("WINED3D_SWAP_EFFECT_FLIP not implemented.\n");
580 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
583 if (swapchain
->num_contexts
> 1)
584 gl_info
->gl_ops
.gl
.p_glFinish();
586 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
587 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context
->hdc
);
589 wined3d_swapchain_rotate(swapchain
, context
);
591 TRACE("SwapBuffers called, Starting new frame\n");
595 DWORD time
= GetTickCount();
598 /* every 1.5 seconds */
599 if (time
- swapchain
->prev_time
> 1500)
601 TRACE_(fps
)("%p @ approx %.2ffps\n",
602 swapchain
, 1000.0 * swapchain
->frames
/ (time
- swapchain
->prev_time
));
603 swapchain
->prev_time
= time
;
604 swapchain
->frames
= 0;
608 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
609 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
610 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
611 * and INTEXTURE copies can keep their old content if they have any defined content.
612 * If the swapeffect is COPY, the content remains the same.
614 * The FLIP swap effect is not implemented yet. We could mark WINED3D_LOCATION_DRAWABLE
615 * up to date and hope WGL flipped front and back buffers and read this data into
616 * the FBO. Don't bother about this for now. */
617 if (swapchain
->desc
.swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
)
618 wined3d_texture_validate_location(swapchain
->back_buffers
[swapchain
->desc
.backbuffer_count
- 1],
619 0, WINED3D_LOCATION_DISCARDED
);
621 if (fb
->depth_stencil
)
623 struct wined3d_surface
*ds
= wined3d_rendertarget_view_get_surface(fb
->depth_stencil
);
625 if (ds
&& (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_DISCARD_DEPTHSTENCIL
626 || ds
->container
->flags
& WINED3D_TEXTURE_DISCARD
))
627 wined3d_texture_validate_location(ds
->container
,
628 fb
->depth_stencil
->sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
631 context_release(context
);
634 static void swapchain_gl_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
636 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
637 struct wined3d_context
*context
;
639 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
640 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
641 context_release(context
);
642 SetRectEmpty(&swapchain
->front_buffer_update
);
645 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
647 swapchain_gl_present
,
648 swapchain_gl_frontbuffer_updated
,
651 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
653 struct wined3d_surface
*front
;
654 POINT offset
= {0, 0};
659 TRACE("swapchain %p.\n", swapchain
);
661 front
= swapchain
->front_buffer
->sub_resources
[0].u
.surface
;
662 if (swapchain
->palette
)
663 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
665 if (front
->container
->resource
.map_count
)
666 ERR("Trying to blit a mapped surface.\n");
668 TRACE("Copying surface %p to screen.\n", front
);
671 window
= swapchain
->win_handle
;
672 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
674 /* Front buffer coordinates are screen coordinates. Map them to the
675 * destination window if not fullscreened. */
676 if (swapchain
->desc
.windowed
)
677 ClientToScreen(window
, &offset
);
679 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
681 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
682 swapchain
->front_buffer
->resource
.height
);
683 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
685 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
686 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
687 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
688 ReleaseDC(window
, dst_dc
);
690 SetRectEmpty(&swapchain
->front_buffer_update
);
693 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
694 const RECT
*src_rect
, const RECT
*dst_rect
, DWORD flags
)
696 struct wined3d_surface
*front
, *back
;
701 front
= swapchain
->front_buffer
->sub_resources
[0].u
.surface
;
702 back
= swapchain
->back_buffers
[0]->sub_resources
[0].u
.surface
;
704 /* Flip the surface data. */
706 bitmap
= front
->bitmap
;
707 data
= front
->container
->resource
.heap_memory
;
709 front
->dc
= back
->dc
;
710 front
->bitmap
= back
->bitmap
;
711 front
->container
->resource
.heap_memory
= back
->container
->resource
.heap_memory
;
714 back
->bitmap
= bitmap
;
715 back
->container
->resource
.heap_memory
= data
;
720 static LONG prev_time
, frames
;
721 DWORD time
= GetTickCount();
725 /* every 1.5 seconds */
726 if (time
- prev_time
> 1500)
728 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0 * frames
/ (time
- prev_time
));
734 SetRect(&swapchain
->front_buffer_update
, 0, 0,
735 swapchain
->front_buffer
->resource
.width
,
736 swapchain
->front_buffer
->resource
.height
);
737 swapchain_gdi_frontbuffer_updated(swapchain
);
740 static const struct wined3d_swapchain_ops swapchain_gdi_ops
=
742 swapchain_gdi_present
,
743 swapchain_gdi_frontbuffer_updated
,
746 static void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
748 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
751 if (!swapchain
->desc
.backbuffer_count
)
753 TRACE("Single buffered rendering.\n");
754 swapchain
->render_to_fbo
= FALSE
;
758 TRACE("Rendering to FBO.\n");
759 swapchain
->render_to_fbo
= TRUE
;
762 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
763 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, DWORD
*quality
)
765 const struct wined3d_gl_info
*gl_info
;
766 const struct wined3d_format
*format
;
767 enum wined3d_multisample_type t
;
769 if (wined3d_settings
.sample_count
== ~0u)
772 gl_info
= &swapchain
->device
->adapter
->gl_info
;
773 if (!(format
= wined3d_get_format(gl_info
, format_id
, WINED3DUSAGE_RENDERTARGET
)))
776 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
777 while (!(format
->multisample_types
& 1u << (t
- 1)))
779 TRACE("Using sample count %u.\n", t
);
784 static HRESULT
swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
785 struct wined3d_swapchain_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
787 const struct wined3d_adapter
*adapter
= device
->adapter
;
788 struct wined3d_resource_desc texture_desc
;
789 BOOL displaymode_set
= FALSE
;
790 DWORD texture_flags
= 0;
796 if (desc
->backbuffer_count
> 1)
798 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
799 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
802 if (device
->wined3d
->flags
& WINED3D_NO3D
)
803 swapchain
->swapchain_ops
= &swapchain_gdi_ops
;
805 swapchain
->swapchain_ops
= &swapchain_gl_ops
;
807 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
809 swapchain
->device
= device
;
810 swapchain
->parent
= parent
;
811 swapchain
->parent_ops
= parent_ops
;
813 swapchain
->win_handle
= window
;
814 swapchain
->device_window
= window
;
816 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
,
817 adapter
->ordinal
, &swapchain
->original_mode
, NULL
)))
819 ERR("Failed to get current display mode, hr %#x.\n", hr
);
822 GetWindowRect(window
, &swapchain
->original_window_rect
);
824 GetClientRect(window
, &client_rect
);
827 if (!desc
->backbuffer_width
)
829 desc
->backbuffer_width
= client_rect
.right
;
830 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
833 if (!desc
->backbuffer_height
)
835 desc
->backbuffer_height
= client_rect
.bottom
;
836 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
839 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
841 desc
->backbuffer_format
= swapchain
->original_mode
.format_id
;
842 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->original_mode
.format_id
));
845 swapchain
->desc
= *desc
;
846 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->desc
.backbuffer_format
,
847 &swapchain
->desc
.multisample_type
, &swapchain
->desc
.multisample_quality
);
848 swapchain_update_render_to_fbo(swapchain
);
850 TRACE("Creating front buffer.\n");
852 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
853 texture_desc
.format
= swapchain
->desc
.backbuffer_format
;
854 texture_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
855 texture_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
856 texture_desc
.usage
= 0;
857 texture_desc
.pool
= WINED3D_POOL_DEFAULT
;
858 texture_desc
.width
= swapchain
->desc
.backbuffer_width
;
859 texture_desc
.height
= swapchain
->desc
.backbuffer_height
;
860 texture_desc
.depth
= 1;
861 texture_desc
.size
= 0;
863 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
864 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
866 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
867 parent
, &texture_desc
, texture_flags
, &swapchain
->front_buffer
)))
869 WARN("Failed to create front buffer, hr %#x.\n", hr
);
873 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
874 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
876 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
877 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
880 /* MSDN says we're only allowed a single fullscreen swapchain per device,
881 * so we should really check to see if there is a fullscreen swapchain
882 * already. Does a single head count as full screen? */
885 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
887 /* Change the display settings */
888 swapchain
->d3d_mode
.width
= desc
->backbuffer_width
;
889 swapchain
->d3d_mode
.height
= desc
->backbuffer_height
;
890 swapchain
->d3d_mode
.format_id
= desc
->backbuffer_format
;
891 swapchain
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
892 swapchain
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
894 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
,
895 adapter
->ordinal
, &swapchain
->d3d_mode
)))
897 WARN("Failed to set display mode, hr %#x.\n", hr
);
900 displaymode_set
= TRUE
;
904 swapchain
->d3d_mode
= swapchain
->original_mode
;
908 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
910 static const enum wined3d_format_id formats
[] =
912 WINED3DFMT_D24_UNORM_S8_UINT
,
913 WINED3DFMT_D32_UNORM
,
914 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
915 WINED3DFMT_D16_UNORM
,
916 WINED3DFMT_S1_UINT_D15_UNORM
919 const struct wined3d_gl_info
*gl_info
= &adapter
->gl_info
;
921 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
922 if (!swapchain
->context
)
924 ERR("Failed to create the context array.\n");
928 swapchain
->num_contexts
= 1;
930 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
931 * You are able to add a depth + stencil surface at a later stage when you need it.
932 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
933 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
934 * context, need torecreate shaders, textures and other resources.
936 * The context manager already takes care of the state problem and for the other tasks code from Reset
937 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
938 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
939 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
940 * issue needs to be fixed. */
941 for (i
= 0; i
< (sizeof(formats
) / sizeof(*formats
)); i
++)
943 swapchain
->ds_format
= wined3d_get_format(gl_info
, formats
[i
], WINED3DUSAGE_DEPTHSTENCIL
);
944 swapchain
->context
[0] = context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
);
945 if (swapchain
->context
[0]) break;
946 TRACE("Depth stencil format %s is not supported, trying next format\n",
947 debug_d3dformat(formats
[i
]));
950 if (!swapchain
->context
[0])
952 WARN("Failed to create context.\n");
953 hr
= WINED3DERR_NOTAVAILABLE
;
957 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
958 && (!desc
->enable_auto_depth_stencil
959 || swapchain
->desc
.auto_depth_stencil_format
!= swapchain
->ds_format
->id
))
961 FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
963 context_release(swapchain
->context
[0]);
964 swapchain_update_swap_interval(swapchain
);
967 if (swapchain
->desc
.backbuffer_count
> 0)
969 if (!(swapchain
->back_buffers
= wined3d_calloc(swapchain
->desc
.backbuffer_count
,
970 sizeof(*swapchain
->back_buffers
))))
972 ERR("Failed to allocate backbuffer array memory.\n");
977 texture_desc
.usage
|= WINED3DUSAGE_RENDERTARGET
;
978 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
980 TRACE("Creating back buffer %u.\n", i
);
981 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
982 parent
, &texture_desc
, texture_flags
, &swapchain
->back_buffers
[i
])))
984 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
985 swapchain
->desc
.backbuffer_count
= i
;
988 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
992 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
993 if (desc
->enable_auto_depth_stencil
&& !(device
->wined3d
->flags
& WINED3D_NO3D
))
995 TRACE("Creating depth/stencil buffer.\n");
996 if (!device
->auto_depth_stencil_view
)
998 struct wined3d_view_desc desc
;
999 struct wined3d_texture
*ds
;
1001 texture_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
1002 texture_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
1004 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1005 device
->device_parent
, &texture_desc
, texture_flags
, &ds
)))
1007 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1011 desc
.format_id
= ds
->resource
.format
->id
;
1013 desc
.u
.texture
.level_idx
= 0;
1014 desc
.u
.texture
.level_count
= 1;
1015 desc
.u
.texture
.layer_idx
= 0;
1016 desc
.u
.texture
.layer_count
= 1;
1017 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1018 &device
->auto_depth_stencil_view
);
1019 wined3d_texture_decref(ds
);
1022 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1028 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1033 if (displaymode_set
)
1035 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1036 adapter
->ordinal
, &swapchain
->original_mode
)))
1037 ERR("Failed to restore display mode.\n");
1041 if (swapchain
->back_buffers
)
1043 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1045 if (swapchain
->back_buffers
[i
])
1047 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1048 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1051 HeapFree(GetProcessHeap(), 0, swapchain
->back_buffers
);
1054 if (swapchain
->context
)
1056 if (swapchain
->context
[0])
1058 context_release(swapchain
->context
[0]);
1059 context_destroy(device
, swapchain
->context
[0]);
1060 swapchain
->num_contexts
= 0;
1062 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1065 if (swapchain
->front_buffer
)
1067 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1068 wined3d_texture_decref(swapchain
->front_buffer
);
1074 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
, struct wined3d_swapchain_desc
*desc
,
1075 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_swapchain
**swapchain
)
1077 struct wined3d_swapchain
*object
;
1080 TRACE("device %p, desc %p, parent %p, parent_ops %p, swapchain %p.\n",
1081 device
, desc
, parent
, parent_ops
, swapchain
);
1083 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1085 return E_OUTOFMEMORY
;
1087 hr
= swapchain_init(object
, device
, desc
, parent
, parent_ops
);
1090 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
1091 HeapFree(GetProcessHeap(), 0, object
);
1095 TRACE("Created swapchain %p.\n", object
);
1096 *swapchain
= object
;
1101 static struct wined3d_context
*swapchain_create_context(struct wined3d_swapchain
*swapchain
)
1103 struct wined3d_context
**ctx_array
;
1104 struct wined3d_context
*ctx
;
1106 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain
, GetCurrentThreadId());
1108 if (!(ctx
= context_create(swapchain
, swapchain
->front_buffer
, swapchain
->ds_format
)))
1110 ERR("Failed to create a new context for the swapchain\n");
1113 context_release(ctx
);
1115 if (!(ctx_array
= wined3d_calloc(swapchain
->num_contexts
+ 1, sizeof(*ctx_array
))))
1117 ERR("Out of memory when trying to allocate a new context array\n");
1118 context_destroy(swapchain
->device
, ctx
);
1121 memcpy(ctx_array
, swapchain
->context
, sizeof(*ctx_array
) * swapchain
->num_contexts
);
1122 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1123 ctx_array
[swapchain
->num_contexts
] = ctx
;
1124 swapchain
->context
= ctx_array
;
1125 swapchain
->num_contexts
++;
1127 TRACE("Returning context %p\n", ctx
);
1131 void swapchain_destroy_contexts(struct wined3d_swapchain
*swapchain
)
1135 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1137 context_destroy(swapchain
->device
, swapchain
->context
[i
]);
1139 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
1140 swapchain
->num_contexts
= 0;
1141 swapchain
->context
= NULL
;
1144 struct wined3d_context
*swapchain_get_context(struct wined3d_swapchain
*swapchain
)
1146 DWORD tid
= GetCurrentThreadId();
1149 for (i
= 0; i
< swapchain
->num_contexts
; ++i
)
1151 if (swapchain
->context
[i
]->tid
== tid
)
1152 return swapchain
->context
[i
];
1155 /* Create a new context for the thread */
1156 return swapchain_create_context(swapchain
);
1159 HDC
swapchain_get_backup_dc(struct wined3d_swapchain
*swapchain
)
1161 if (!swapchain
->backup_dc
)
1163 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
1165 if (!(swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1166 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1168 ERR("Failed to create a window.\n");
1172 if (!(swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
)))
1174 ERR("Failed to get a DC.\n");
1175 DestroyWindow(swapchain
->backup_wnd
);
1176 swapchain
->backup_wnd
= NULL
;
1181 return swapchain
->backup_dc
;
1184 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1188 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1190 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1192 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1196 void swapchain_update_swap_interval(struct wined3d_swapchain
*swapchain
)
1198 const struct wined3d_gl_info
*gl_info
;
1199 struct wined3d_context
*context
;
1202 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
, 0);
1203 gl_info
= context
->gl_info
;
1205 switch (swapchain
->desc
.swap_interval
)
1207 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1210 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1211 case WINED3DPRESENT_INTERVAL_ONE
:
1214 case WINED3DPRESENT_INTERVAL_TWO
:
1217 case WINED3DPRESENT_INTERVAL_THREE
:
1220 case WINED3DPRESENT_INTERVAL_FOUR
:
1224 FIXME("Unhandled present interval %#x.\n", swapchain
->desc
.swap_interval
);
1228 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1230 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1231 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1232 swap_interval
, context
, GetLastError());
1235 context_release(context
);
1238 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1240 struct wined3d_device
*device
= swapchain
->device
;
1241 BOOL filter_messages
= device
->filter_messages
;
1243 /* This code is not protected by the wined3d mutex, so it may run while
1244 * wined3d_device_reset is active. Testing on Windows shows that changing
1245 * focus during resets and resetting during focus change events causes
1246 * the application to crash with an invalid memory access. */
1248 device
->filter_messages
= !(device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
);
1252 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1254 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1255 * the window but leaves the size untouched, d3d9 sets the size on an
1256 * invisible window, generates messages but doesn't change the window
1257 * properties. The implementation follows d3d9.
1259 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1260 * resume drawing after a focus loss. */
1261 SetWindowPos(swapchain
->device_window
, NULL
, 0, 0,
1262 swapchain
->desc
.backbuffer_width
, swapchain
->desc
.backbuffer_height
,
1263 SWP_NOACTIVATE
| SWP_NOZORDER
);
1266 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1268 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1269 device
->adapter
->ordinal
, &swapchain
->d3d_mode
)))
1270 ERR("Failed to set display mode.\n");
1275 if (FAILED(wined3d_set_adapter_display_mode(device
->wined3d
,
1276 device
->adapter
->ordinal
, NULL
)))
1277 ERR("Failed to set display mode.\n");
1279 swapchain
->reapply_mode
= TRUE
;
1281 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
)
1282 && IsWindowVisible(swapchain
->device_window
))
1283 ShowWindow(swapchain
->device_window
, SW_MINIMIZE
);
1286 device
->filter_messages
= filter_messages
;
1289 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1290 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1291 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1293 BOOL update_desc
= FALSE
;
1295 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1296 "multisample_type %#x, multisample_quality %#x.\n",
1297 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1298 multisample_type
, multisample_quality
);
1300 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1302 if (buffer_count
&& buffer_count
!= swapchain
->desc
.backbuffer_count
)
1303 FIXME("Cannot change the back buffer count yet.\n");
1305 if (!width
|| !height
)
1307 /* The application is requesting that either the swapchain width or
1308 * height be set to the corresponding dimension in the window's
1313 if (!swapchain
->desc
.windowed
)
1314 return WINED3DERR_INVALIDCALL
;
1316 if (!GetClientRect(swapchain
->device_window
, &client_rect
))
1318 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
1319 return WINED3DERR_INVALIDCALL
;
1323 width
= client_rect
.right
;
1326 height
= client_rect
.bottom
;
1329 if (width
!= swapchain
->desc
.backbuffer_width
1330 || height
!= swapchain
->desc
.backbuffer_height
)
1332 swapchain
->desc
.backbuffer_width
= width
;
1333 swapchain
->desc
.backbuffer_height
= height
;
1337 if (format_id
== WINED3DFMT_UNKNOWN
)
1339 if (!swapchain
->desc
.windowed
)
1340 return WINED3DERR_INVALIDCALL
;
1341 format_id
= swapchain
->original_mode
.format_id
;
1344 if (format_id
!= swapchain
->desc
.backbuffer_format
)
1346 swapchain
->desc
.backbuffer_format
= format_id
;
1350 if (multisample_type
!= swapchain
->desc
.multisample_type
1351 || multisample_quality
!= swapchain
->desc
.multisample_quality
)
1353 swapchain
->desc
.multisample_type
= multisample_type
;
1354 swapchain
->desc
.multisample_quality
= multisample_quality
;
1363 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, swapchain
->desc
.backbuffer_width
,
1364 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1365 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1368 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
1370 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], swapchain
->desc
.backbuffer_width
,
1371 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
1372 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
, NULL
, 0)))
1377 swapchain_update_render_to_fbo(swapchain
);
1378 swapchain_update_draw_bindings(swapchain
);
1383 static HRESULT
wined3d_swapchain_set_display_mode(struct wined3d_swapchain
*swapchain
,
1384 struct wined3d_display_mode
*mode
)
1386 struct wined3d_device
*device
= swapchain
->device
;
1389 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
1391 if (FAILED(hr
= wined3d_find_closest_matching_adapter_mode(device
->wined3d
,
1392 device
->adapter
->ordinal
, mode
)))
1394 WARN("Failed to find closest matching mode, hr %#x.\n", hr
);
1398 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
,
1399 device
->adapter
->ordinal
, mode
)))
1401 WARN("Failed to set display mode, hr %#x.\n", hr
);
1402 return WINED3DERR_INVALIDCALL
;
1408 HRESULT CDECL
wined3d_swapchain_resize_target(struct wined3d_swapchain
*swapchain
,
1409 const struct wined3d_display_mode
*mode
)
1411 struct wined3d_device
*device
= swapchain
->device
;
1412 struct wined3d_display_mode actual_mode
;
1413 RECT original_window_rect
, window_rect
;
1416 TRACE("swapchain %p, mode %p.\n", swapchain
, mode
);
1418 if (swapchain
->desc
.windowed
)
1420 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
1421 AdjustWindowRectEx(&window_rect
,
1422 GetWindowLongW(swapchain
->device_window
, GWL_STYLE
), FALSE
,
1423 GetWindowLongW(swapchain
->device_window
, GWL_EXSTYLE
));
1424 SetRect(&window_rect
, 0, 0,
1425 window_rect
.right
- window_rect
.left
, window_rect
.bottom
- window_rect
.top
);
1426 GetWindowRect(swapchain
->device_window
, &original_window_rect
);
1427 OffsetRect(&window_rect
, original_window_rect
.left
, original_window_rect
.top
);
1429 else if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1431 actual_mode
= *mode
;
1432 if (FAILED(hr
= wined3d_swapchain_set_display_mode(swapchain
, &actual_mode
)))
1434 SetRect(&window_rect
, 0, 0, actual_mode
.width
, actual_mode
.height
);
1438 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
,
1439 &actual_mode
, NULL
)))
1441 ERR("Failed to get display mode, hr %#x.\n", hr
);
1442 return WINED3DERR_INVALIDCALL
;
1445 SetRect(&window_rect
, 0, 0, actual_mode
.width
, actual_mode
.height
);
1448 MoveWindow(swapchain
->device_window
, window_rect
.left
, window_rect
.top
,
1449 window_rect
.right
- window_rect
.left
,
1450 window_rect
.bottom
- window_rect
.top
, TRUE
);
1455 HRESULT CDECL
wined3d_swapchain_set_fullscreen(struct wined3d_swapchain
*swapchain
,
1456 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
)
1458 struct wined3d_device
*device
= swapchain
->device
;
1459 struct wined3d_display_mode actual_mode
;
1462 TRACE("swapchain %p, desc %p, mode %p.\n", swapchain
, swapchain_desc
, mode
);
1464 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1468 actual_mode
= *mode
;
1472 if (!swapchain_desc
->windowed
)
1474 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
1475 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
1476 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
1477 actual_mode
.format_id
= swapchain_desc
->backbuffer_format
;
1478 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1482 actual_mode
= swapchain
->original_mode
;
1486 if (FAILED(hr
= wined3d_swapchain_set_display_mode(swapchain
, &actual_mode
)))
1492 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
1494 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
,
1495 &actual_mode
, NULL
)))
1497 ERR("Failed to get display mode, hr %#x.\n", hr
);
1498 return WINED3DERR_INVALIDCALL
;
1502 if (!swapchain_desc
->windowed
)
1504 unsigned int width
= actual_mode
.width
;
1505 unsigned int height
= actual_mode
.height
;
1507 if (swapchain
->desc
.windowed
)
1509 /* Switch from windowed to fullscreen */
1510 HWND focus_window
= device
->create_parms
.focus_window
;
1512 focus_window
= swapchain
->device_window
;
1513 if (FAILED(hr
= wined3d_device_acquire_focus_window(device
, focus_window
)))
1515 ERR("Failed to acquire focus window, hr %#x.\n", hr
);
1519 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
, width
, height
);
1523 /* Fullscreen -> fullscreen mode change */
1524 BOOL filter_messages
= device
->filter_messages
;
1525 device
->filter_messages
= TRUE
;
1527 MoveWindow(swapchain
->device_window
, 0, 0, width
, height
, TRUE
);
1529 device
->filter_messages
= filter_messages
;
1531 swapchain
->d3d_mode
= actual_mode
;
1533 else if (!swapchain
->desc
.windowed
)
1535 /* Fullscreen -> windowed switch */
1536 RECT
*window_rect
= NULL
;
1537 if (swapchain
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
1538 window_rect
= &swapchain
->original_window_rect
;
1539 wined3d_device_restore_fullscreen_window(device
, swapchain
->device_window
, window_rect
);
1540 wined3d_device_release_focus_window(device
);
1543 swapchain
->desc
.windowed
= swapchain_desc
->windowed
;