2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
24 #include "wined3d_gl.h"
25 #include "wined3d_vk.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
30 void wined3d_swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
35 TRACE("Destroying swapchain %p.\n", swapchain
);
37 wined3d_swapchain_state_cleanup(&swapchain
->state
);
38 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
40 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
41 * is the last buffer to be destroyed, FindContext() depends on that. */
42 if (swapchain
->front_buffer
)
44 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
45 if (wined3d_texture_decref(swapchain
->front_buffer
))
46 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
47 swapchain
->front_buffer
= NULL
;
50 if (swapchain
->back_buffers
)
52 i
= swapchain
->state
.desc
.backbuffer_count
;
56 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
57 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
58 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
60 heap_free(swapchain
->back_buffers
);
61 swapchain
->back_buffers
= NULL
;
64 /* Restore the screen resolution if we rendered in fullscreen.
65 * This will restore the screen resolution to what it was before creating
66 * the swapchain. In case of d3d8 and d3d9 this will be the original
67 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
68 * sets the resolution before starting up Direct3D, thus orig_width and
69 * orig_height will be equal to the modes in the presentation params. */
70 if (!swapchain
->state
.desc
.windowed
)
72 if (swapchain
->state
.desc
.auto_restore_display_mode
)
74 if (FAILED(hr
= wined3d_restore_display_modes(swapchain
->device
->wined3d
)))
75 ERR("Failed to restore display mode, hr %#lx.\n", hr
);
77 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
79 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
,
80 swapchain
->state
.device_window
, &swapchain
->state
.original_window_rect
);
81 wined3d_device_release_focus_window(swapchain
->device
);
86 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
, swapchain
->state
.device_window
, NULL
);
91 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl
*swapchain_gl
)
93 wined3d_swapchain_cleanup(&swapchain_gl
->s
);
96 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
98 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
99 const struct wined3d_vk_info
*vk_info
;
103 TRACE("swapchain_vk %p.\n", swapchain_vk
);
105 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
107 if ((vr
= VK_CALL(vkQueueWaitIdle(device_vk
->vk_queue
))) < 0)
108 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr
));
109 heap_free(swapchain_vk
->vk_images
);
110 for (i
= 0; i
< swapchain_vk
->image_count
; ++i
)
112 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
113 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
115 heap_free(swapchain_vk
->vk_semaphores
);
116 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, NULL
));
117 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, swapchain_vk
->vk_surface
, NULL
));
120 static void wined3d_swapchain_vk_destroy_object(void *object
)
122 wined3d_swapchain_vk_destroy_vulkan_swapchain(object
);
125 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk
*swapchain_vk
)
127 struct wined3d_cs
*cs
= swapchain_vk
->s
.device
->cs
;
129 wined3d_cs_destroy_object(cs
, wined3d_swapchain_vk_destroy_object
, swapchain_vk
);
130 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
132 wined3d_swapchain_cleanup(&swapchain_vk
->s
);
135 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
137 unsigned int refcount
= InterlockedIncrement(&swapchain
->ref
);
139 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
144 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
146 unsigned int refcount
= InterlockedDecrement(&swapchain
->ref
);
148 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
152 struct wined3d_device
*device
;
154 wined3d_mutex_lock();
156 device
= swapchain
->device
;
157 if (device
->swapchain_count
&& device
->swapchains
[0] == swapchain
)
158 wined3d_device_uninit_3d(device
);
159 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
161 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
162 swapchain
->device
->adapter
->adapter_ops
->adapter_destroy_swapchain(swapchain
);
164 wined3d_mutex_unlock();
170 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
172 TRACE("swapchain %p.\n", swapchain
);
174 return swapchain
->parent
;
177 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
180 window
= swapchain
->state
.device_window
;
181 if (window
== swapchain
->win_handle
)
184 TRACE("Setting swapchain %p window from %p to %p.\n",
185 swapchain
, swapchain
->win_handle
, window
);
187 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
189 swapchain
->win_handle
= window
;
192 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
193 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
194 unsigned int swap_interval
, uint32_t flags
)
198 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
199 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
200 dst_window_override
, swap_interval
, flags
);
203 FIXME("Ignoring flags %#x.\n", flags
);
205 wined3d_mutex_lock();
207 if (!swapchain
->back_buffers
)
209 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
210 wined3d_mutex_unlock();
211 return WINED3DERR_INVALIDCALL
;
216 SetRect(&s
, 0, 0, swapchain
->state
.desc
.backbuffer_width
,
217 swapchain
->state
.desc
.backbuffer_height
);
223 GetClientRect(swapchain
->win_handle
, &d
);
227 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
228 dst_rect
, dst_window_override
, swap_interval
, flags
);
230 wined3d_mutex_unlock();
235 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
236 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
238 RECT src_rect
, dst_rect
;
240 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
242 SetRect(&src_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
, swapchain
->front_buffer
->resource
.height
);
245 if (swapchain
->state
.desc
.windowed
)
247 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
248 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
249 wine_dbgstr_rect(&dst_rect
));
252 return wined3d_device_context_blt(&swapchain
->device
->cs
->c
, dst_texture
, sub_resource_idx
, &dst_rect
,
253 swapchain
->front_buffer
, 0, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
256 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
257 UINT back_buffer_idx
)
259 TRACE("swapchain %p, back_buffer_idx %u.\n",
260 swapchain
, back_buffer_idx
);
262 /* Return invalid if there is no backbuffer array, otherwise it will
263 * crash when ddraw is used (there swapchain->back_buffers is always
264 * NULL). We need this because this function is called from
265 * stateblock_init_default_state() to get the default scissorrect
267 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->state
.desc
.backbuffer_count
)
269 WARN("Invalid back buffer index.\n");
270 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
271 * here in wined3d to avoid problems in other libs. */
275 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
277 return swapchain
->back_buffers
[back_buffer_idx
];
280 struct wined3d_texture
* CDECL
wined3d_swapchain_get_front_buffer(const struct wined3d_swapchain
*swapchain
)
282 TRACE("swapchain %p.\n", swapchain
);
284 return swapchain
->front_buffer
;
287 struct wined3d_output
* wined3d_swapchain_get_output(const struct wined3d_swapchain
*swapchain
)
289 TRACE("swapchain %p.\n", swapchain
);
291 return swapchain
->state
.desc
.output
;
294 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
295 struct wined3d_raster_status
*raster_status
)
297 struct wined3d_output
*output
;
299 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
301 output
= wined3d_swapchain_get_output(swapchain
);
304 ERR("Failed to get output from swapchain %p.\n", swapchain
);
308 return wined3d_output_get_raster_status(output
, raster_status
);
311 struct wined3d_swapchain_state
* CDECL
wined3d_swapchain_get_state(struct wined3d_swapchain
*swapchain
)
313 return &swapchain
->state
;
316 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
317 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
319 struct wined3d_output
*output
;
322 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
324 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
326 ERR("Failed to get output from swapchain %p.\n", swapchain
);
330 hr
= wined3d_output_get_display_mode(output
, mode
, rotation
);
332 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
333 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
338 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
340 TRACE("swapchain %p.\n", swapchain
);
342 return swapchain
->device
;
345 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
346 struct wined3d_swapchain_desc
*desc
)
348 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
350 *desc
= swapchain
->state
.desc
;
353 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
354 uint32_t flags
, const struct wined3d_gamma_ramp
*ramp
)
356 struct wined3d_output
*output
;
358 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
361 FIXME("Ignoring flags %#x.\n", flags
);
363 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
365 ERR("Failed to get output from swapchain %p.\n", swapchain
);
369 return wined3d_output_set_gamma_ramp(output
, ramp
);
372 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
374 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
376 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
378 swapchain
->palette
= palette
;
381 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
382 struct wined3d_gamma_ramp
*ramp
)
384 struct wined3d_output
*output
;
386 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
388 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
390 ERR("Failed to get output from swapchain %p.\n", swapchain
);
394 return wined3d_output_get_gamma_ramp(output
, ramp
);
397 /* The is a fallback for cases where we e.g. can't create a GL context or
398 * Vulkan swapchain for the swapchain window. */
399 static void swapchain_blit_gdi(struct wined3d_swapchain
*swapchain
,
400 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
402 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
403 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
404 D3DKMT_CREATEDCFROMMEMORY create_desc
;
405 const struct wined3d_format
*format
;
406 unsigned int row_pitch
, slice_pitch
;
411 static unsigned int once
;
413 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
414 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
417 FIXME("Using GDI present.\n");
419 format
= back_buffer
->resource
.format
;
420 if (!format
->ddi_format
)
422 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
426 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_SYSMEM
);
427 wined3d_texture_get_pitch(back_buffer
, 0, &row_pitch
, &slice_pitch
);
429 create_desc
.pMemory
= back_buffer
->resource
.heap_memory
;
430 create_desc
.Format
= format
->ddi_format
;
431 create_desc
.Width
= wined3d_texture_get_level_width(back_buffer
, 0);
432 create_desc
.Height
= wined3d_texture_get_level_height(back_buffer
, 0);
433 create_desc
.Pitch
= row_pitch
;
434 create_desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
435 create_desc
.pColorTable
= NULL
;
437 status
= D3DKMTCreateDCFromMemory(&create_desc
);
438 DeleteDC(create_desc
.hDeviceDc
);
441 WARN("Failed to create DC, status %#lx.\n", status
);
445 src_dc
= create_desc
.hDc
;
446 bitmap
= create_desc
.hBitmap
;
448 TRACE("Created source DC %p, bitmap %p for backbuffer %p.\n", src_dc
, bitmap
, back_buffer
);
450 if (!(dst_dc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
451 ERR("Failed to get destination DC.\n");
453 if (!StretchBlt(dst_dc
, dst_rect
->left
, dst_rect
->top
, dst_rect
->right
- dst_rect
->left
,
454 dst_rect
->bottom
- dst_rect
->top
, src_dc
, src_rect
->left
, src_rect
->top
,
455 src_rect
->right
- src_rect
->left
, src_rect
->bottom
- src_rect
->top
, SRCCOPY
))
456 ERR("Failed to blit.\n");
458 ReleaseDC(swapchain
->win_handle
, dst_dc
);
459 destroy_desc
.hDc
= src_dc
;
460 destroy_desc
.hBitmap
= bitmap
;
461 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
462 ERR("Failed to destroy src dc, status %#lx.\n", status
);
465 /* A GL context is provided by the caller */
466 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
467 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
469 struct wined3d_texture
*texture
= swapchain
->back_buffers
[0];
470 struct wined3d_device
*device
= swapchain
->device
;
471 enum wined3d_texture_filter_type filter
;
474 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
475 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
477 if ((src_rect
->right
- src_rect
->left
== dst_rect
->right
- dst_rect
->left
478 && src_rect
->bottom
- src_rect
->top
== dst_rect
->bottom
- dst_rect
->top
)
479 || is_complex_fixup(texture
->resource
.format
->color_fixup
))
480 filter
= WINED3D_TEXF_NONE
;
482 filter
= WINED3D_TEXF_LINEAR
;
484 location
= WINED3D_LOCATION_TEXTURE_RGB
;
485 if (texture
->resource
.multisample_type
)
486 location
= WINED3D_LOCATION_RB_RESOLVED
;
488 wined3d_texture_validate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
489 device
->blitter
->ops
->blitter_blit(device
->blitter
, WINED3D_BLIT_OP_COLOR_BLIT
, context
, texture
, 0,
490 location
, src_rect
, texture
, 0, WINED3D_LOCATION_DRAWABLE
, dst_rect
, NULL
, filter
, NULL
);
491 wined3d_texture_invalidate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
494 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain
*swapchain
,
495 struct wined3d_context_gl
*context_gl
, unsigned int swap_interval
)
497 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
499 swap_interval
= swap_interval
<= 4 ? swap_interval
: 1;
500 if (swapchain
->swap_interval
== swap_interval
)
503 swapchain
->swap_interval
= swap_interval
;
505 if (!gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
508 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
510 ERR("Failed to set swap interval %u for context %p, last error %#lx.\n",
511 swap_interval
, context_gl
, GetLastError());
515 /* Context activation is done by the caller. */
516 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
518 struct wined3d_texture_sub_resource
*sub_resource
;
519 struct wined3d_texture_gl
*texture
, *texture_prev
;
520 struct gl_texture tex0
;
524 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
526 if (swapchain
->state
.desc
.backbuffer_count
< 2 || wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
529 texture_prev
= wined3d_texture_gl(swapchain
->back_buffers
[0]);
531 /* Back buffer 0 is already in the draw binding. */
532 tex0
= texture_prev
->texture_rgb
;
533 rb0
= texture_prev
->rb_multisample
;
534 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
536 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
538 texture
= wined3d_texture_gl(swapchain
->back_buffers
[i
]);
539 sub_resource
= &texture
->t
.sub_resources
[0];
541 if (!(sub_resource
->locations
& supported_locations
))
542 wined3d_texture_load_location(&texture
->t
, 0, context
, texture
->t
.resource
.draw_binding
);
544 texture_prev
->texture_rgb
= texture
->texture_rgb
;
545 texture_prev
->rb_multisample
= texture
->rb_multisample
;
547 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
548 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
550 texture_prev
= texture
;
553 texture_prev
->texture_rgb
= tex0
;
554 texture_prev
->rb_multisample
= rb0
;
556 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
557 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
559 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
562 static bool swapchain_present_is_partial_copy(struct wined3d_swapchain
*swapchain
, const RECT
*dst_rect
)
564 enum wined3d_swap_effect swap_effect
= swapchain
->state
.desc
.swap_effect
;
568 if (swap_effect
!= WINED3D_SWAP_EFFECT_COPY
&& swap_effect
!= WINED3D_SWAP_EFFECT_COPY_VSYNC
)
571 GetClientRect(swapchain
->win_handle
, &client_rect
);
573 t
= client_rect
.right
- client_rect
.left
;
574 if ((dst_rect
->left
&& dst_rect
->right
) || abs(dst_rect
->right
- dst_rect
->left
) != t
)
576 t
= client_rect
.bottom
- client_rect
.top
;
577 if ((dst_rect
->top
&& dst_rect
->bottom
) || abs(dst_rect
->bottom
- dst_rect
->top
) != t
)
583 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
584 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
586 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
587 const struct wined3d_pixel_format
*pixel_format
;
588 const struct wined3d_gl_info
*gl_info
;
589 struct wined3d_context_gl
*context_gl
;
590 struct wined3d_context
*context
;
592 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
, 0);
593 context_gl
= wined3d_context_gl(context
);
594 if (!context_gl
->valid
)
596 context_release(context
);
597 WARN("Invalid context, skipping present.\n");
601 TRACE("Presenting DC %p.\n", context_gl
->dc
);
603 pixel_format
= &wined3d_adapter_gl(swapchain
->device
->adapter
)->pixel_formats
[context_gl
->pixel_format
];
604 if (context_gl
->dc
== wined3d_device_gl(swapchain
->device
)->backup_dc
605 || (pixel_format
->swap_method
!= WGL_SWAP_COPY_ARB
606 && swapchain_present_is_partial_copy(swapchain
, dst_rect
)))
608 swapchain_blit_gdi(swapchain
, context
, src_rect
, dst_rect
);
612 gl_info
= context_gl
->gl_info
;
614 swapchain_gl_set_swap_interval(swapchain
, context_gl
, swap_interval
);
616 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
618 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
619 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
621 if (swapchain
->device
->context_count
> 1)
623 WARN_(d3d_perf
)("Multiple contexts, calling glFinish() to enforce ordering.\n");
624 gl_info
->gl_ops
.gl
.p_glFinish();
627 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
628 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context_gl
->dc
);
631 if (context
->d3d_info
->fences
)
632 wined3d_context_gl_submit_command_fence(context_gl
);
634 wined3d_swapchain_gl_rotate(swapchain
, context
);
636 TRACE("SwapBuffers called, Starting new frame\n");
638 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
639 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
641 context_release(context
);
644 static void swapchain_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
646 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
647 struct wined3d_context
*context
;
649 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
650 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
651 context_release(context
);
652 SetRectEmpty(&swapchain
->front_buffer_update
);
655 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
657 swapchain_gl_present
,
658 swapchain_frontbuffer_updated
,
661 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk
*swapchain_vk
,
662 VkPresentModeKHR vk_present_mode
)
664 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
665 const struct wined3d_vk_info
*vk_info
;
666 struct wined3d_adapter_vk
*adapter_vk
;
667 VkPhysicalDevice vk_physical_device
;
668 VkPresentModeKHR
*vk_modes
;
669 bool supported
= false;
673 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
674 vk_physical_device
= adapter_vk
->physical_device
;
675 vk_info
= &adapter_vk
->vk_info
;
677 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
678 swapchain_vk
->vk_surface
, &count
, NULL
))) < 0)
680 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr
));
684 if (!(vk_modes
= heap_calloc(count
, sizeof(*vk_modes
))))
687 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
688 swapchain_vk
->vk_surface
, &count
, vk_modes
))) < 0)
690 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr
));
694 for (i
= 0; i
< count
; ++i
)
696 if (vk_modes
[i
] == vk_present_mode
)
708 static VkFormat
get_swapchain_fallback_format(VkFormat vk_format
)
712 case VK_FORMAT_R8G8B8A8_SRGB
:
713 return VK_FORMAT_B8G8R8A8_SRGB
;
714 case VK_FORMAT_R8G8B8A8_UNORM
:
715 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
716 case VK_FORMAT_R16G16B16A16_SFLOAT
:
717 return VK_FORMAT_B8G8R8A8_UNORM
;
719 WARN("Unhandled format %#x.\n", vk_format
);
720 return VK_FORMAT_UNDEFINED
;
724 static VkFormat
wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk
*swapchain_vk
,
725 VkSurfaceKHR vk_surface
)
727 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
728 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
729 const struct wined3d_vk_info
*vk_info
;
730 struct wined3d_adapter_vk
*adapter_vk
;
731 const struct wined3d_format
*format
;
732 VkPhysicalDevice vk_physical_device
;
733 VkSurfaceFormatKHR
*vk_formats
;
734 uint32_t format_count
, i
;
738 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
739 vk_physical_device
= adapter_vk
->physical_device
;
740 vk_info
= &adapter_vk
->vk_info
;
742 if ((format
= wined3d_get_format(&adapter_vk
->a
, desc
->backbuffer_format
, WINED3D_BIND_RENDER_TARGET
)))
743 vk_format
= wined3d_format_vk(format
)->vk_format
;
745 vk_format
= VK_FORMAT_B8G8R8A8_UNORM
;
747 vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
, vk_surface
, &format_count
, NULL
));
748 if (vr
< 0 || !format_count
)
750 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr
));
751 return VK_FORMAT_UNDEFINED
;
754 if (!(vk_formats
= heap_calloc(format_count
, sizeof(*vk_formats
))))
755 return VK_FORMAT_UNDEFINED
;
757 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
,
758 vk_surface
, &format_count
, vk_formats
))) < 0)
760 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr
));
761 heap_free(vk_formats
);
762 return VK_FORMAT_UNDEFINED
;
765 for (i
= 0; i
< format_count
; ++i
)
767 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
770 if (i
== format_count
)
772 /* Try to create a swapchain with format conversion. */
773 vk_format
= get_swapchain_fallback_format(vk_format
);
774 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
775 for (i
= 0; i
< format_count
; ++i
)
777 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
781 heap_free(vk_formats
);
782 if (i
== format_count
)
784 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
785 return VK_FORMAT_UNDEFINED
;
788 TRACE("Using Vulkan swapchain format %#x.\n", vk_format
);
793 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk
*swapchain_vk
,
794 VkSwapchainKHR vk_swapchain
)
796 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
797 const struct wined3d_vk_info
*vk_info
;
798 VkSemaphoreCreateInfo semaphore_info
;
799 uint32_t image_count
, i
;
802 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
804 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
, vk_swapchain
, &image_count
, NULL
))) < 0)
806 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr
));
810 if (!(swapchain_vk
->vk_images
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_images
))))
812 ERR("Failed to allocate images array.\n");
816 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
,
817 vk_swapchain
, &image_count
, swapchain_vk
->vk_images
))) < 0)
819 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr
));
820 heap_free(swapchain_vk
->vk_images
);
824 if (!(swapchain_vk
->vk_semaphores
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_semaphores
))))
826 ERR("Failed to allocate semaphores array.\n");
827 heap_free(swapchain_vk
->vk_images
);
831 semaphore_info
.sType
= VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
;
832 semaphore_info
.pNext
= NULL
;
833 semaphore_info
.flags
= 0;
834 for (i
= 0; i
< image_count
; ++i
)
836 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
837 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].available
))) < 0)
839 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
843 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
844 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].presentable
))) < 0)
846 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
850 swapchain_vk
->image_count
= image_count
;
855 for (i
= 0; i
< image_count
; ++i
)
857 if (swapchain_vk
->vk_semaphores
[i
].available
)
858 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
859 if (swapchain_vk
->vk_semaphores
[i
].presentable
)
860 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
862 heap_free(swapchain_vk
->vk_semaphores
);
863 heap_free(swapchain_vk
->vk_images
);
867 static HRESULT
wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
869 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
870 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
871 VkSwapchainCreateInfoKHR vk_swapchain_desc
;
872 VkWin32SurfaceCreateInfoKHR surface_desc
;
873 unsigned int width
, height
, image_count
;
874 const struct wined3d_vk_info
*vk_info
;
875 VkSurfaceCapabilitiesKHR surface_caps
;
876 struct wined3d_adapter_vk
*adapter_vk
;
877 VkPresentModeKHR vk_present_mode
;
878 VkSwapchainKHR vk_swapchain
;
879 VkImageUsageFlags usage
;
880 VkSurfaceKHR vk_surface
;
886 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
887 vk_info
= &adapter_vk
->vk_info
;
889 surface_desc
.sType
= VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR
;
890 surface_desc
.pNext
= NULL
;
891 surface_desc
.flags
= 0;
892 surface_desc
.hinstance
= (HINSTANCE
)GetWindowLongPtrW(swapchain_vk
->s
.win_handle
, GWLP_HINSTANCE
);
893 surface_desc
.hwnd
= swapchain_vk
->s
.win_handle
;
894 if ((vr
= VK_CALL(vkCreateWin32SurfaceKHR(vk_info
->instance
, &surface_desc
, NULL
, &vk_surface
))) < 0)
896 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr
));
899 swapchain_vk
->vk_surface
= vk_surface
;
901 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk
->physical_device
,
902 device_vk
->vk_queue_family_index
, vk_surface
, &supported
))) < 0 || !supported
)
904 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr
));
908 if ((vk_format
= wined3d_swapchain_vk_select_vk_format(swapchain_vk
, vk_surface
)) == VK_FORMAT_UNDEFINED
)
910 ERR("Failed to select swapchain format.\n");
914 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk
->physical_device
,
915 swapchain_vk
->vk_surface
, &surface_caps
))) < 0)
917 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr
));
921 image_count
= desc
->backbuffer_count
;
922 if (image_count
< surface_caps
.minImageCount
)
923 image_count
= surface_caps
.minImageCount
;
924 else if (surface_caps
.maxImageCount
&& image_count
> surface_caps
.maxImageCount
)
925 image_count
= surface_caps
.maxImageCount
;
927 if (image_count
!= desc
->backbuffer_count
)
928 WARN("Image count %u is not supported (%u-%u).\n", desc
->backbuffer_count
,
929 surface_caps
.minImageCount
, surface_caps
.maxImageCount
);
931 GetClientRect(swapchain_vk
->s
.win_handle
, &client_rect
);
933 width
= client_rect
.right
- client_rect
.left
;
934 if (width
< surface_caps
.minImageExtent
.width
)
935 width
= surface_caps
.minImageExtent
.width
;
936 else if (width
> surface_caps
.maxImageExtent
.width
)
937 width
= surface_caps
.maxImageExtent
.width
;
939 height
= client_rect
.bottom
- client_rect
.top
;
940 if (height
< surface_caps
.minImageExtent
.height
)
941 height
= surface_caps
.minImageExtent
.height
;
942 else if (height
> surface_caps
.maxImageExtent
.height
)
943 height
= surface_caps
.maxImageExtent
.height
;
945 if (width
!= client_rect
.right
- client_rect
.left
|| height
!= client_rect
.bottom
- client_rect
.top
)
946 WARN("Swapchain dimensions %lux%lu are not supported (%u-%u x %u-%u).\n",
947 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
948 surface_caps
.minImageExtent
.width
, surface_caps
.maxImageExtent
.width
,
949 surface_caps
.minImageExtent
.height
, surface_caps
.maxImageExtent
.height
);
951 usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
952 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
953 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
954 if (!(usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) || !(usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
))
955 WARN("Transfer not supported for swapchain images.\n");
957 if (!(surface_caps
.supportedCompositeAlpha
& VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
))
959 FIXME("Unsupported alpha mode, %#x.\n", surface_caps
.supportedCompositeAlpha
);
963 vk_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
964 if (!swapchain_vk
->s
.swap_interval
)
966 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk
, VK_PRESENT_MODE_IMMEDIATE_KHR
))
967 vk_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
969 FIXME("Unsupported swap interval %u.\n", swapchain_vk
->s
.swap_interval
);
972 vk_swapchain_desc
.sType
= VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
;
973 vk_swapchain_desc
.pNext
= NULL
;
974 vk_swapchain_desc
.flags
= 0;
975 vk_swapchain_desc
.surface
= vk_surface
;
976 vk_swapchain_desc
.minImageCount
= image_count
;
977 vk_swapchain_desc
.imageFormat
= vk_format
;
978 vk_swapchain_desc
.imageColorSpace
= VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
;
979 vk_swapchain_desc
.imageExtent
.width
= width
;
980 vk_swapchain_desc
.imageExtent
.height
= height
;
981 vk_swapchain_desc
.imageArrayLayers
= 1;
982 vk_swapchain_desc
.imageUsage
= usage
;
983 vk_swapchain_desc
.imageSharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
984 vk_swapchain_desc
.queueFamilyIndexCount
= 0;
985 vk_swapchain_desc
.pQueueFamilyIndices
= NULL
;
986 vk_swapchain_desc
.preTransform
= surface_caps
.currentTransform
;
987 vk_swapchain_desc
.compositeAlpha
= VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
988 vk_swapchain_desc
.presentMode
= vk_present_mode
;
989 vk_swapchain_desc
.clipped
= VK_TRUE
;
990 vk_swapchain_desc
.oldSwapchain
= VK_NULL_HANDLE
;
991 if ((vr
= VK_CALL(vkCreateSwapchainKHR(device_vk
->vk_device
, &vk_swapchain_desc
, NULL
, &vk_swapchain
))) < 0)
993 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr
));
996 swapchain_vk
->vk_swapchain
= vk_swapchain
;
998 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk
, vk_swapchain
))
1000 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, vk_swapchain
, NULL
));
1004 swapchain_vk
->width
= width
;
1005 swapchain_vk
->height
= height
;
1010 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, vk_surface
, NULL
));
1014 static HRESULT
wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk
*swapchain_vk
)
1016 TRACE("swapchain_vk %p.\n", swapchain_vk
);
1018 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk
);
1020 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
);
1023 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk
*swapchain_vk
,
1024 unsigned int swap_interval
)
1026 if (swap_interval
> 1)
1028 if (swap_interval
<= 4)
1029 FIXME("Unsupported swap interval %u.\n", swap_interval
);
1033 if (swapchain_vk
->s
.swap_interval
== swap_interval
)
1036 swapchain_vk
->s
.swap_interval
= swap_interval
;
1037 wined3d_swapchain_vk_recreate(swapchain_vk
);
1040 static VkResult
wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk
*swapchain_vk
,
1041 struct wined3d_context_vk
*context_vk
, const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
)
1043 struct wined3d_texture_vk
*back_buffer_vk
= wined3d_texture_vk(swapchain_vk
->s
.back_buffers
[0]);
1044 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
1045 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
1046 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1047 VkCommandBuffer vk_command_buffer
;
1048 VkImageSubresourceRange vk_range
;
1049 VkPresentInfoKHR present_desc
;
1050 unsigned int present_idx
;
1051 VkImageLayout vk_layout
;
1058 static const VkPipelineStageFlags wait_stage
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
;
1060 TRACE("swapchain_vk %p, context_vk %p, src_rect %s, dst_rect %s, swap_interval %u.\n",
1061 swapchain_vk
, context_vk
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
), swap_interval
);
1063 wined3d_swapchain_vk_set_swap_interval(swapchain_vk
, swap_interval
);
1065 present_idx
= swapchain_vk
->current
++ % swapchain_vk
->image_count
;
1066 wined3d_context_vk_wait_command_buffer(context_vk
, swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
);
1067 if ((vr
= VK_CALL(vkAcquireNextImageKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, UINT64_MAX
,
1068 swapchain_vk
->vk_semaphores
[present_idx
].available
, VK_NULL_HANDLE
, &image_idx
))) < 0)
1070 WARN("Failed to acquire image, vr %s.\n", wined3d_debug_vkresult(vr
));
1074 if (dst_rect
->right
> swapchain_vk
->width
|| dst_rect
->bottom
> swapchain_vk
->height
)
1076 dst_rect_tmp
= *dst_rect
;
1077 if (dst_rect
->right
> swapchain_vk
->width
)
1078 dst_rect_tmp
.right
= swapchain_vk
->width
;
1079 if (dst_rect
->bottom
> swapchain_vk
->height
)
1080 dst_rect_tmp
.bottom
= swapchain_vk
->height
;
1081 dst_rect
= &dst_rect_tmp
;
1083 filter
= src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
1084 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
1085 ? VK_FILTER_LINEAR
: VK_FILTER_NEAREST
;
1086 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
1088 wined3d_context_vk_end_current_render_pass(context_vk
);
1090 vk_range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1091 vk_range
.baseMipLevel
= 0;
1092 vk_range
.levelCount
= 1;
1093 vk_range
.baseArrayLayer
= 0;
1094 vk_range
.layerCount
= 1;
1096 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1097 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1098 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1099 VK_ACCESS_TRANSFER_READ_BIT
,
1100 back_buffer_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1101 back_buffer_vk
->image
.vk_image
, &vk_range
);
1103 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1104 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1105 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
1106 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1107 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1109 blit
.srcSubresource
.aspectMask
= vk_range
.aspectMask
;
1110 blit
.srcSubresource
.mipLevel
= vk_range
.baseMipLevel
;
1111 blit
.srcSubresource
.baseArrayLayer
= vk_range
.baseArrayLayer
;
1112 blit
.srcSubresource
.layerCount
= vk_range
.layerCount
;
1113 blit
.srcOffsets
[0].x
= src_rect
->left
;
1114 blit
.srcOffsets
[0].y
= src_rect
->top
;
1115 blit
.srcOffsets
[0].z
= 0;
1116 blit
.srcOffsets
[1].x
= src_rect
->right
;
1117 blit
.srcOffsets
[1].y
= src_rect
->bottom
;
1118 blit
.srcOffsets
[1].z
= 1;
1119 blit
.dstSubresource
= blit
.srcSubresource
;
1120 blit
.dstOffsets
[0].x
= dst_rect
->left
;
1121 blit
.dstOffsets
[0].y
= dst_rect
->top
;
1122 blit
.dstOffsets
[0].z
= 0;
1123 blit
.dstOffsets
[1].x
= dst_rect
->right
;
1124 blit
.dstOffsets
[1].y
= dst_rect
->bottom
;
1125 blit
.dstOffsets
[1].z
= 1;
1126 VK_CALL(vkCmdBlitImage(vk_command_buffer
,
1127 back_buffer_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1128 swapchain_vk
->vk_images
[image_idx
], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1131 wined3d_context_vk_reference_texture(context_vk
, back_buffer_vk
);
1132 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1133 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
1134 VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
1135 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
1136 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1138 if (desc
->swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
|| desc
->swap_effect
== WINED3D_SWAP_EFFECT_FLIP_DISCARD
)
1139 vk_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
1141 vk_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
1142 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1143 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1144 VK_ACCESS_TRANSFER_READ_BIT
,
1145 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1146 vk_layout
, back_buffer_vk
->layout
,
1147 back_buffer_vk
->image
.vk_image
, &vk_range
);
1148 back_buffer_vk
->bind_mask
= 0;
1150 swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
= context_vk
->current_command_buffer
.id
;
1151 wined3d_context_vk_submit_command_buffer(context_vk
,
1152 1, &swapchain_vk
->vk_semaphores
[present_idx
].available
, &wait_stage
,
1153 1, &swapchain_vk
->vk_semaphores
[present_idx
].presentable
);
1155 present_desc
.sType
= VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
;
1156 present_desc
.pNext
= NULL
;
1157 present_desc
.waitSemaphoreCount
= 1;
1158 present_desc
.pWaitSemaphores
= &swapchain_vk
->vk_semaphores
[present_idx
].presentable
;
1159 present_desc
.swapchainCount
= 1;
1160 present_desc
.pSwapchains
= &swapchain_vk
->vk_swapchain
;
1161 present_desc
.pImageIndices
= &image_idx
;
1162 present_desc
.pResults
= NULL
;
1163 if ((vr
= VK_CALL(vkQueuePresentKHR(device_vk
->vk_queue
, &present_desc
))))
1164 WARN("Present returned vr %s.\n", wined3d_debug_vkresult(vr
));
1168 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context_vk
*context_vk
)
1170 struct wined3d_texture_sub_resource
*sub_resource
;
1171 struct wined3d_texture_vk
*texture
, *texture_prev
;
1172 struct wined3d_image_vk image0
;
1173 VkDescriptorImageInfo vk_info0
;
1174 VkImageLayout vk_layout0
;
1175 uint32_t bind_mask0
;
1179 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
1181 if (swapchain
->state
.desc
.backbuffer_count
< 2)
1184 texture_prev
= wined3d_texture_vk(swapchain
->back_buffers
[0]);
1186 /* Back buffer 0 is already in the draw binding. */
1187 image0
= texture_prev
->image
;
1188 vk_layout0
= texture_prev
->layout
;
1189 bind_mask0
= texture_prev
->bind_mask
;
1190 vk_info0
= texture_prev
->default_image_info
;
1191 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
1193 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1195 texture
= wined3d_texture_vk(swapchain
->back_buffers
[i
]);
1196 sub_resource
= &texture
->t
.sub_resources
[0];
1198 if (!(sub_resource
->locations
& supported_locations
))
1199 wined3d_texture_load_location(&texture
->t
, 0, &context_vk
->c
, texture
->t
.resource
.draw_binding
);
1201 texture_prev
->image
= texture
->image
;
1202 texture_prev
->layout
= texture
->layout
;
1203 texture_prev
->bind_mask
= texture
->bind_mask
;
1204 texture_prev
->default_image_info
= texture
->default_image_info
;
1206 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
1207 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
1209 texture_prev
= texture
;
1212 texture_prev
->image
= image0
;
1213 texture_prev
->layout
= vk_layout0
;
1214 texture_prev
->bind_mask
= bind_mask0
;
1215 texture_prev
->default_image_info
= vk_info0
;
1217 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
1218 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
1220 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
1223 static void swapchain_vk_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect
,
1224 const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1226 struct wined3d_swapchain_vk
*swapchain_vk
= wined3d_swapchain_vk(swapchain
);
1227 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
1228 struct wined3d_context_vk
*context_vk
;
1232 context_vk
= wined3d_context_vk(context_acquire(swapchain
->device
, back_buffer
, 0));
1234 if (!swapchain_vk
->vk_swapchain
|| swapchain_present_is_partial_copy(swapchain
, dst_rect
))
1236 swapchain_blit_gdi(swapchain
, &context_vk
->c
, src_rect
, dst_rect
);
1240 wined3d_texture_load_location(back_buffer
, 0, &context_vk
->c
, back_buffer
->resource
.draw_binding
);
1242 if ((vr
= wined3d_swapchain_vk_blit(swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1244 if (vr
== VK_ERROR_OUT_OF_DATE_KHR
|| vr
== VK_SUBOPTIMAL_KHR
)
1246 if (FAILED(hr
= wined3d_swapchain_vk_recreate(swapchain_vk
)))
1247 ERR("Failed to recreate swapchain, hr %#lx.\n", hr
);
1248 else if (vr
== VK_ERROR_OUT_OF_DATE_KHR
&& (vr
= wined3d_swapchain_vk_blit(
1249 swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1250 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1254 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1259 wined3d_swapchain_vk_rotate(swapchain
, context_vk
);
1261 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1262 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1264 TRACE("Starting new frame.\n");
1266 context_release(&context_vk
->c
);
1269 static const struct wined3d_swapchain_ops swapchain_vk_ops
=
1271 swapchain_vk_present
,
1272 swapchain_frontbuffer_updated
,
1275 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
1277 struct wined3d_dc_info
*front
;
1278 POINT offset
= {0, 0};
1283 TRACE("swapchain %p.\n", swapchain
);
1285 front
= &swapchain
->front_buffer
->dc_info
[0];
1286 if (swapchain
->palette
)
1287 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
1289 if (swapchain
->front_buffer
->resource
.map_count
)
1290 ERR("Trying to blit a mapped surface.\n");
1292 TRACE("Copying surface %p to screen.\n", front
);
1295 window
= swapchain
->win_handle
;
1296 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
1298 /* Front buffer coordinates are screen coordinates. Map them to the
1299 * destination window if not fullscreened. */
1300 if (swapchain
->state
.desc
.windowed
)
1301 ClientToScreen(window
, &offset
);
1303 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
1305 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
1306 swapchain
->front_buffer
->resource
.height
);
1307 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
1309 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
1310 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
1311 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
1312 ReleaseDC(window
, dst_dc
);
1314 SetRectEmpty(&swapchain
->front_buffer_update
);
1317 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
1318 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1320 struct wined3d_dc_info
*front
, *back
;
1325 front
= &swapchain
->front_buffer
->dc_info
[0];
1326 back
= &swapchain
->back_buffers
[0]->dc_info
[0];
1328 /* Flip the surface data. */
1330 bitmap
= front
->bitmap
;
1331 data
= swapchain
->front_buffer
->resource
.heap_memory
;
1333 front
->dc
= back
->dc
;
1334 front
->bitmap
= back
->bitmap
;
1335 swapchain
->front_buffer
->resource
.heap_memory
= swapchain
->back_buffers
[0]->resource
.heap_memory
;
1338 back
->bitmap
= bitmap
;
1339 swapchain
->back_buffers
[0]->resource
.heap_memory
= data
;
1341 SetRect(&swapchain
->front_buffer_update
, 0, 0,
1342 swapchain
->front_buffer
->resource
.width
,
1343 swapchain
->front_buffer
->resource
.height
);
1344 swapchain_gdi_frontbuffer_updated(swapchain
);
1347 static const struct wined3d_swapchain_ops swapchain_no3d_ops
=
1349 swapchain_gdi_present
,
1350 swapchain_gdi_frontbuffer_updated
,
1353 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
1354 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, unsigned int *quality
)
1356 const struct wined3d_adapter
*adapter
;
1357 const struct wined3d_format
*format
;
1358 enum wined3d_multisample_type t
;
1360 if (wined3d_settings
.sample_count
== ~0u)
1363 adapter
= swapchain
->device
->adapter
;
1364 if (!(format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
)))
1367 if ((t
= min(wined3d_settings
.sample_count
, adapter
->d3d_info
.limits
.sample_count
)))
1368 while (!(format
->multisample_types
& 1u << (t
- 1)))
1370 TRACE("Using sample count %u.\n", t
);
1375 void swapchain_set_max_frame_latency(struct wined3d_swapchain
*swapchain
, const struct wined3d_device
*device
)
1377 /* Subtract 1 for the implicit OpenGL latency. */
1378 swapchain
->max_frame_latency
= device
->max_frame_latency
>= 2 ? device
->max_frame_latency
- 1 : 1;
1381 static enum wined3d_format_id
adapter_format_from_backbuffer_format(const struct wined3d_adapter
*adapter
,
1382 enum wined3d_format_id format_id
)
1384 const struct wined3d_format
*backbuffer_format
;
1386 backbuffer_format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
);
1387 return pixelformat_for_depth(backbuffer_format
->byte_count
* CHAR_BIT
);
1390 static HRESULT
wined3d_swapchain_state_init(struct wined3d_swapchain_state
*state
,
1391 const struct wined3d_swapchain_desc
*desc
, HWND window
, struct wined3d
*wined3d
,
1392 struct wined3d_swapchain_state_parent
*parent
)
1396 state
->desc
= *desc
;
1398 if (FAILED(hr
= wined3d_output_get_display_mode(desc
->output
, &state
->original_mode
, NULL
)))
1400 ERR("Failed to get current display mode, hr %#lx.\n", hr
);
1404 if (state
->desc
.windowed
)
1408 GetClientRect(window
, &client_rect
);
1409 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect
));
1411 if (!state
->desc
.backbuffer_width
)
1413 state
->desc
.backbuffer_width
= client_rect
.right
? client_rect
.right
: 8;
1414 TRACE("Updating width to %u.\n", state
->desc
.backbuffer_width
);
1416 if (!state
->desc
.backbuffer_height
)
1418 state
->desc
.backbuffer_height
= client_rect
.bottom
? client_rect
.bottom
: 8;
1419 TRACE("Updating height to %u.\n", state
->desc
.backbuffer_height
);
1422 if (state
->desc
.backbuffer_format
== WINED3DFMT_UNKNOWN
)
1424 state
->desc
.backbuffer_format
= state
->original_mode
.format_id
;
1425 TRACE("Updating format to %s.\n", debug_d3dformat(state
->original_mode
.format_id
));
1430 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1432 state
->d3d_mode
.width
= desc
->backbuffer_width
;
1433 state
->d3d_mode
.height
= desc
->backbuffer_height
;
1434 state
->d3d_mode
.format_id
= adapter_format_from_backbuffer_format(desc
->output
->adapter
,
1435 desc
->backbuffer_format
);
1436 state
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1437 state
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1441 state
->d3d_mode
= state
->original_mode
;
1445 GetWindowRect(window
, &state
->original_window_rect
);
1446 state
->wined3d
= wined3d
;
1447 state
->device_window
= window
;
1448 state
->desc
.device_window
= window
;
1449 state
->parent
= parent
;
1451 if (desc
->flags
& WINED3D_SWAPCHAIN_REGISTER_STATE
)
1452 wined3d_swapchain_state_register(state
);
1457 static HRESULT
swapchain_create_texture(struct wined3d_swapchain
*swapchain
,
1458 bool front
, bool depth
, struct wined3d_texture
**texture
)
1460 const struct wined3d_swapchain_desc
*swapchain_desc
= &swapchain
->state
.desc
;
1461 struct wined3d_device
*device
= swapchain
->device
;
1462 struct wined3d_resource_desc texture_desc
;
1463 uint32_t texture_flags
= 0;
1466 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1467 texture_desc
.format
= depth
? swapchain_desc
->auto_depth_stencil_format
: swapchain_desc
->backbuffer_format
;
1468 texture_desc
.multisample_type
= swapchain_desc
->multisample_type
;
1469 texture_desc
.multisample_quality
= swapchain_desc
->multisample_quality
;
1470 texture_desc
.usage
= 0;
1471 if (!depth
&& (device
->wined3d
->flags
& WINED3D_NO3D
))
1472 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1473 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1474 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1476 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1477 if (!depth
&& (swapchain_desc
->flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
))
1478 texture_desc
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1479 texture_desc
.width
= swapchain_desc
->backbuffer_width
;
1480 texture_desc
.height
= swapchain_desc
->backbuffer_height
;
1481 texture_desc
.depth
= 1;
1482 texture_desc
.size
= 0;
1485 texture_desc
.bind_flags
= 0;
1487 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
1489 texture_desc
.bind_flags
= swapchain_desc
->backbuffer_bind_flags
;
1491 if (swapchain_desc
->flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
1492 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
1494 if (FAILED(hr
= wined3d_texture_create(device
, &texture_desc
, 1, 1,
1495 texture_flags
, NULL
, NULL
, &wined3d_null_parent_ops
, texture
)))
1497 WARN("Failed to create texture, hr %#lx.\n", hr
);
1502 wined3d_texture_set_swapchain(*texture
, swapchain
);
1507 static HRESULT
wined3d_swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
1508 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1509 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1510 const struct wined3d_swapchain_ops
*swapchain_ops
)
1512 struct wined3d_output_desc output_desc
;
1513 BOOL displaymode_set
= FALSE
;
1514 HRESULT hr
= E_FAIL
;
1518 wined3d_mutex_lock();
1520 if (desc
->backbuffer_count
> 1)
1522 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1523 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1526 if (desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
1527 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
1528 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
1529 FIXME("Unimplemented swap effect %#x.\n", desc
->swap_effect
);
1531 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
1532 TRACE("Using target window %p.\n", window
);
1534 if (FAILED(hr
= wined3d_swapchain_state_init(&swapchain
->state
, desc
, window
, device
->wined3d
, state_parent
)))
1536 ERR("Failed to initialise swapchain state, hr %#lx.\n", hr
);
1537 wined3d_mutex_unlock();
1541 swapchain
->swapchain_ops
= swapchain_ops
;
1542 swapchain
->device
= device
;
1543 swapchain
->parent
= parent
;
1544 swapchain
->parent_ops
= parent_ops
;
1546 swapchain
->win_handle
= window
;
1547 swapchain
->swap_interval
= WINED3D_SWAP_INTERVAL_DEFAULT
;
1548 swapchain_set_max_frame_latency(swapchain
, device
);
1550 if (!swapchain
->state
.desc
.windowed
)
1552 if (FAILED(hr
= wined3d_output_get_desc(desc
->output
, &output_desc
)))
1554 ERR("Failed to get output description, hr %#lx.\n", hr
);
1558 wined3d_swapchain_state_setup_fullscreen(&swapchain
->state
, window
,
1559 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, desc
->backbuffer_width
,
1560 desc
->backbuffer_height
);
1562 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->state
.desc
.backbuffer_format
,
1563 &swapchain
->state
.desc
.multisample_type
, &swapchain
->state
.desc
.multisample_quality
);
1565 TRACE("Creating front buffer.\n");
1567 if (FAILED(hr
= swapchain_create_texture(swapchain
, true, false, &swapchain
->front_buffer
)))
1569 WARN("Failed to create front buffer, hr %#lx.\n", hr
);
1573 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1575 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1576 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1579 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1580 * so we should really check to see if there is a fullscreen swapchain
1581 * already. Does a single head count as full screen? */
1582 if (!desc
->windowed
&& desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1584 /* Change the display settings */
1585 if (FAILED(hr
= wined3d_output_set_display_mode(desc
->output
,
1586 &swapchain
->state
.d3d_mode
)))
1588 WARN("Failed to set display mode, hr %#lx.\n", hr
);
1591 displaymode_set
= TRUE
;
1594 if (swapchain
->state
.desc
.backbuffer_count
> 0)
1596 if (!(swapchain
->back_buffers
= heap_calloc(swapchain
->state
.desc
.backbuffer_count
,
1597 sizeof(*swapchain
->back_buffers
))))
1599 ERR("Failed to allocate backbuffer array memory.\n");
1604 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1606 TRACE("Creating back buffer %u.\n", i
);
1607 if (FAILED(hr
= swapchain_create_texture(swapchain
, false, false, &swapchain
->back_buffers
[i
])))
1609 WARN("Failed to create back buffer %u, hr %#lx.\n", i
, hr
);
1610 swapchain
->state
.desc
.backbuffer_count
= i
;
1616 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1617 if (desc
->enable_auto_depth_stencil
)
1619 TRACE("Creating depth/stencil buffer.\n");
1620 if (!device
->auto_depth_stencil_view
)
1622 struct wined3d_view_desc desc
;
1623 struct wined3d_texture
*ds
;
1625 if (FAILED(hr
= swapchain_create_texture(swapchain
, false, true, &ds
)))
1627 WARN("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr
);
1631 desc
.format_id
= ds
->resource
.format
->id
;
1633 desc
.u
.texture
.level_idx
= 0;
1634 desc
.u
.texture
.level_count
= 1;
1635 desc
.u
.texture
.layer_idx
= 0;
1636 desc
.u
.texture
.layer_count
= 1;
1637 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1638 &device
->auto_depth_stencil_view
);
1639 wined3d_texture_decref(ds
);
1642 ERR("Failed to create rendertarget view, hr %#lx.\n", hr
);
1648 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1650 wined3d_mutex_unlock();
1655 if (displaymode_set
)
1657 if (FAILED(wined3d_restore_display_modes(device
->wined3d
)))
1658 ERR("Failed to restore display mode.\n");
1661 if (swapchain
->back_buffers
)
1663 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1665 if (swapchain
->back_buffers
[i
])
1667 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1668 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1671 heap_free(swapchain
->back_buffers
);
1674 if (swapchain
->front_buffer
)
1676 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1677 wined3d_texture_decref(swapchain
->front_buffer
);
1680 wined3d_swapchain_state_cleanup(&swapchain
->state
);
1681 wined3d_mutex_unlock();
1686 HRESULT
wined3d_swapchain_no3d_init(struct wined3d_swapchain
*swapchain_no3d
, struct wined3d_device
*device
,
1687 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1688 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1690 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1691 swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
);
1693 return wined3d_swapchain_init(swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
,
1694 &swapchain_no3d_ops
);
1697 HRESULT
wined3d_swapchain_gl_init(struct wined3d_swapchain_gl
*swapchain_gl
, struct wined3d_device
*device
,
1698 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1699 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1701 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1702 swapchain_gl
, device
, desc
, state_parent
, parent
, parent_ops
);
1704 return wined3d_swapchain_init(&swapchain_gl
->s
, device
, desc
, state_parent
, parent
,
1705 parent_ops
, &swapchain_gl_ops
);
1708 HRESULT
wined3d_swapchain_vk_init(struct wined3d_swapchain_vk
*swapchain_vk
, struct wined3d_device
*device
,
1709 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1710 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1714 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1715 swapchain_vk
, device
, desc
, parent
, parent_ops
);
1717 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_vk
->s
, device
, desc
, state_parent
, parent
,
1718 parent_ops
, &swapchain_vk_ops
)))
1721 if (swapchain_vk
->s
.win_handle
== GetDesktopWindow())
1723 WARN("Creating a desktop window swapchain.\n");
1727 if (FAILED(hr
= wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
)))
1728 WARN("Failed to create a Vulkan swapchain, hr %#lx.\n", hr
);
1733 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1734 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1735 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1736 struct wined3d_swapchain
**swapchain
)
1738 struct wined3d_swapchain
*object
;
1741 if (FAILED(hr
= device
->adapter
->adapter_ops
->adapter_create_swapchain(device
,
1742 desc
, state_parent
, parent
, parent_ops
, &object
)))
1745 if (desc
->flags
& WINED3D_SWAPCHAIN_IMPLICIT
)
1747 wined3d_mutex_lock();
1748 if (FAILED(hr
= wined3d_device_set_implicit_swapchain(device
, object
)))
1750 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1751 device
->adapter
->adapter_ops
->adapter_destroy_swapchain(object
);
1752 wined3d_mutex_unlock();
1755 wined3d_mutex_unlock();
1758 *swapchain
= object
;
1763 static struct wined3d_context_gl
*wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1765 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1766 struct wined3d_context_gl
*context_gl
;
1768 TRACE("Creating a new context for swapchain %p, thread %lu.\n", swapchain_gl
, GetCurrentThreadId());
1770 wined3d_from_cs(device
->cs
);
1772 if (!(context_gl
= heap_alloc_zero(sizeof(*context_gl
))))
1774 ERR("Failed to allocate context memory.\n");
1778 if (FAILED(wined3d_context_gl_init(context_gl
, swapchain_gl
)))
1780 WARN("Failed to initialise context.\n");
1781 heap_free(context_gl
);
1785 if (!device_context_add(device
, &context_gl
->c
))
1787 ERR("Failed to add the newly created context to the context list.\n");
1788 wined3d_context_gl_destroy(context_gl
);
1792 TRACE("Created context %p.\n", context_gl
);
1794 context_release(&context_gl
->c
);
1799 struct wined3d_context_gl
*wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1801 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1802 DWORD tid
= GetCurrentThreadId();
1805 for (i
= 0; i
< device
->context_count
; ++i
)
1807 if (wined3d_context_gl(device
->contexts
[i
])->tid
== tid
)
1808 return wined3d_context_gl(device
->contexts
[i
]);
1811 /* Create a new context for the thread. */
1812 return wined3d_swapchain_gl_create_context(swapchain_gl
);
1815 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1819 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1821 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1823 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1827 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1829 struct wined3d_device
*device
= swapchain
->device
;
1830 HWND window
= swapchain
->state
.device_window
;
1831 struct wined3d_output_desc output_desc
;
1832 unsigned int screensaver_active
;
1833 struct wined3d_output
*output
;
1834 BOOL focus_messages
, filter
;
1837 /* This code is not protected by the wined3d mutex, so it may run while
1838 * wined3d_device_reset is active. Testing on Windows shows that changing
1839 * focus during resets and resetting during focus change events causes
1840 * the application to crash with an invalid memory access. */
1842 if (!(focus_messages
= device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
))
1843 filter
= wined3d_filter_messages(window
, TRUE
);
1847 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
1848 if ((device
->restore_screensaver
= !!screensaver_active
))
1849 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
1851 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1853 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1854 * the window but leaves the size untouched, d3d9 sets the size on an
1855 * invisible window, generates messages but doesn't change the window
1856 * properties. The implementation follows d3d9.
1858 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1859 * resume drawing after a focus loss. */
1860 output
= wined3d_swapchain_get_output(swapchain
);
1863 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1867 if (SUCCEEDED(hr
= wined3d_output_get_desc(output
, &output_desc
)))
1868 SetWindowPos(window
, NULL
, output_desc
.desktop_rect
.left
,
1869 output_desc
.desktop_rect
.top
, swapchain
->state
.desc
.backbuffer_width
,
1870 swapchain
->state
.desc
.backbuffer_height
, SWP_NOACTIVATE
| SWP_NOZORDER
);
1872 ERR("Failed to get output description, hr %#lx.\n", hr
);
1875 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1877 output
= wined3d_swapchain_get_output(swapchain
);
1880 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1884 if (FAILED(hr
= wined3d_output_set_display_mode(output
,
1885 &swapchain
->state
.d3d_mode
)))
1886 ERR("Failed to set display mode, hr %#lx.\n", hr
);
1889 if (swapchain
== device
->swapchains
[0])
1890 device
->device_parent
->ops
->activate(device
->device_parent
, TRUE
);
1894 if (device
->restore_screensaver
)
1896 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
1897 device
->restore_screensaver
= FALSE
;
1900 if (FAILED(hr
= wined3d_restore_display_modes(device
->wined3d
)))
1901 ERR("Failed to restore display modes, hr %#lx.\n", hr
);
1903 swapchain
->reapply_mode
= TRUE
;
1905 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1906 * during window minimization. Do our housekeeping now, as the device may not exist after
1907 * the ShowWindow call.
1909 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1910 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1911 * we'll have to control this via a create flag. Note that the device and swapchain are not
1912 * safe to access after the ShowWindow call. */
1913 if (swapchain
== device
->swapchains
[0])
1914 device
->device_parent
->ops
->activate(device
->device_parent
, FALSE
);
1916 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
) && IsWindowVisible(window
))
1917 ShowWindow(window
, SW_MINIMIZE
);
1920 if (!focus_messages
)
1921 wined3d_filter_messages(window
, filter
);
1924 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1925 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1926 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1928 struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
1929 bool recreate
= false;
1931 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1932 "multisample_type %#x, multisample_quality %#x.\n",
1933 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1934 multisample_type
, multisample_quality
);
1936 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1938 if (buffer_count
&& buffer_count
!= desc
->backbuffer_count
)
1939 FIXME("Cannot change the back buffer count yet.\n");
1941 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1943 if (!width
|| !height
)
1947 /* The application is requesting that either the swapchain width or
1948 * height be set to the corresponding dimension in the window's
1951 if (!GetClientRect(swapchain
->state
.device_window
, &client_rect
))
1953 ERR("Failed to get client rect, last error %#lx.\n", GetLastError());
1954 return WINED3DERR_INVALIDCALL
;
1958 width
= client_rect
.right
;
1961 height
= client_rect
.bottom
;
1964 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
1966 desc
->backbuffer_width
= width
;
1967 desc
->backbuffer_height
= height
;
1971 if (format_id
== WINED3DFMT_UNKNOWN
)
1973 if (!desc
->windowed
)
1974 return WINED3DERR_INVALIDCALL
;
1975 format_id
= swapchain
->state
.original_mode
.format_id
;
1978 if (format_id
!= desc
->backbuffer_format
)
1980 desc
->backbuffer_format
= format_id
;
1984 if (multisample_type
!= desc
->multisample_type
1985 || multisample_quality
!= desc
->multisample_quality
)
1987 desc
->multisample_type
= multisample_type
;
1988 desc
->multisample_quality
= multisample_quality
;
1994 struct wined3d_texture
*new_texture
;
1998 TRACE("Recreating swapchain textures.\n");
2000 if (FAILED(hr
= swapchain_create_texture(swapchain
, true, false, &new_texture
)))
2002 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
2003 if (wined3d_texture_decref(swapchain
->front_buffer
))
2004 ERR("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
2005 swapchain
->front_buffer
= new_texture
;
2007 if (!(swapchain
->device
->wined3d
->flags
& WINED3D_NO3D
))
2009 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
2010 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
2013 for (i
= 0; i
< desc
->backbuffer_count
; ++i
)
2015 if (FAILED(hr
= swapchain_create_texture(swapchain
, false, false, &new_texture
)))
2017 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
2018 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
2019 ERR("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
2020 swapchain
->back_buffers
[i
] = new_texture
;
2024 swapchain_update_draw_bindings(swapchain
);
2029 static HRESULT
wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state
*state
,
2030 struct wined3d_output
*output
, struct wined3d_display_mode
*mode
)
2034 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
2036 if (FAILED(hr
= wined3d_output_find_closest_matching_mode(output
, mode
)))
2038 WARN("Failed to find closest matching mode, hr %#lx.\n", hr
);
2042 if (output
!= state
->desc
.output
)
2044 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2046 WARN("Failed to restore display modes, hr %#lx.\n", hr
);
2050 if (FAILED(hr
= wined3d_output_get_display_mode(output
, &state
->original_mode
, NULL
)))
2052 WARN("Failed to get current display mode, hr %#lx.\n", hr
);
2057 if (FAILED(hr
= wined3d_output_set_display_mode(output
, mode
)))
2059 WARN("Failed to set display mode, hr %#lx.\n", hr
);
2060 return WINED3DERR_INVALIDCALL
;
2066 HRESULT CDECL
wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state
*state
,
2067 const struct wined3d_display_mode
*mode
)
2069 struct wined3d_display_mode actual_mode
;
2070 struct wined3d_output_desc output_desc
;
2071 RECT original_window_rect
, window_rect
;
2072 int x
, y
, width
, height
;
2076 TRACE("state %p, mode %p.\n", state
, mode
);
2078 wined3d_mutex_lock();
2080 window
= state
->device_window
;
2082 if (state
->desc
.windowed
)
2084 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
2085 AdjustWindowRectEx(&window_rect
,
2086 GetWindowLongW(window
, GWL_STYLE
), FALSE
,
2087 GetWindowLongW(window
, GWL_EXSTYLE
));
2088 GetWindowRect(window
, &original_window_rect
);
2090 x
= original_window_rect
.left
;
2091 y
= original_window_rect
.top
;
2092 width
= window_rect
.right
- window_rect
.left
;
2093 height
= window_rect
.bottom
- window_rect
.top
;
2097 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2099 actual_mode
= *mode
;
2100 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, state
->desc
.output
,
2103 ERR("Failed to set display mode, hr %#lx.\n", hr
);
2104 wined3d_mutex_unlock();
2109 if (FAILED(hr
= wined3d_output_get_desc(state
->desc
.output
, &output_desc
)))
2111 ERR("Failed to get output description, hr %#lx.\n", hr
);
2112 wined3d_mutex_unlock();
2116 x
= output_desc
.desktop_rect
.left
;
2117 y
= output_desc
.desktop_rect
.top
;
2118 width
= output_desc
.desktop_rect
.right
- output_desc
.desktop_rect
.left
;
2119 height
= output_desc
.desktop_rect
.bottom
- output_desc
.desktop_rect
.top
;
2122 wined3d_mutex_unlock();
2124 MoveWindow(window
, x
, y
, width
, height
, TRUE
);
2129 static LONG
fullscreen_style(LONG style
)
2131 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2132 style
|= WS_POPUP
| WS_SYSMENU
;
2133 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
2138 static LONG
fullscreen_exstyle(LONG exstyle
)
2140 /* Filter out window decorations. */
2141 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
2146 struct wined3d_window_state
2149 HWND window_pos_after
;
2150 LONG style
, exstyle
;
2151 int x
, y
, width
, height
;
2154 bool register_topmost_timer
;
2155 bool set_topmost_timer
;
2158 #define WINED3D_WINDOW_TOPMOST_TIMER_ID 0x4242
2160 static void CALLBACK
topmost_timer_proc(HWND hwnd
, UINT msg
, UINT_PTR id
, DWORD time
)
2162 if (!(GetWindowLongW(hwnd
, GWL_EXSTYLE
) & WS_EX_TOPMOST
))
2163 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0, SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
2165 KillTimer(hwnd
, WINED3D_WINDOW_TOPMOST_TIMER_ID
);
2168 static DWORD WINAPI
set_window_state_thread(void *ctx
)
2170 struct wined3d_window_state
*s
= ctx
;
2173 filter
= wined3d_filter_messages(s
->window
, TRUE
);
2177 SetWindowLongW(s
->window
, GWL_STYLE
, s
->style
);
2178 SetWindowLongW(s
->window
, GWL_EXSTYLE
, s
->exstyle
);
2180 SetWindowPos(s
->window
, s
->window_pos_after
, s
->x
, s
->y
, s
->width
, s
->height
, s
->flags
);
2182 wined3d_filter_messages(s
->window
, filter
);
2189 static void set_window_state(struct wined3d_window_state
*s
)
2191 static const UINT timeout
= 1500;
2192 DWORD window_tid
= GetWindowThreadProcessId(s
->window
, NULL
);
2193 DWORD tid
= GetCurrentThreadId();
2196 TRACE("Window %p belongs to thread %#lx.\n", s
->window
, window_tid
);
2197 /* If the window belongs to a different thread, modifying the style and/or
2198 * position can potentially deadlock if that thread isn't processing
2200 if (window_tid
== tid
)
2202 set_window_state_thread(s
);
2204 /* Deus Ex: Game of the Year Edition removes WS_EX_TOPMOST after changing resolutions in
2205 * exclusive fullscreen mode. Tests show that WS_EX_TOPMOST will be restored when a ~1.5s
2206 * timer times out */
2207 if (s
->register_topmost_timer
)
2209 if (s
->set_topmost_timer
)
2210 SetTimer(s
->window
, WINED3D_WINDOW_TOPMOST_TIMER_ID
, timeout
, topmost_timer_proc
);
2212 KillTimer(s
->window
, WINED3D_WINDOW_TOPMOST_TIMER_ID
);
2215 else if ((thread
= CreateThread(NULL
, 0, set_window_state_thread
, s
, 0, NULL
)))
2217 SetThreadDescription(thread
, L
"wined3d_set_window_state");
2218 CloseHandle(thread
);
2222 ERR("Failed to create thread.\n");
2226 HRESULT
wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state
*state
,
2227 HWND window
, int x
, int y
, int width
, int height
)
2229 struct wined3d_window_state
*s
;
2231 TRACE("Setting up window %p for fullscreen mode.\n", window
);
2233 if (!IsWindow(window
))
2235 WARN("%p is not a valid window.\n", window
);
2236 return WINED3DERR_NOTAVAILABLE
;
2239 if (!(s
= heap_alloc(sizeof(*s
))))
2240 return E_OUTOFMEMORY
;
2242 s
->window_pos_after
= HWND_TOPMOST
;
2248 if (state
->style
|| state
->exstyle
)
2250 ERR("Changing the window style for window %p, but another style (%08lx, %08lx) is already stored.\n",
2251 window
, state
->style
, state
->exstyle
);
2254 s
->flags
= SWP_FRAMECHANGED
| SWP_NOACTIVATE
;
2255 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
)
2256 s
->flags
|= SWP_NOZORDER
;
2258 s
->flags
|= SWP_SHOWWINDOW
;
2260 state
->style
= GetWindowLongW(window
, GWL_STYLE
);
2261 state
->exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2263 s
->style
= fullscreen_style(state
->style
);
2264 s
->exstyle
= fullscreen_exstyle(state
->exstyle
);
2265 s
->set_style
= true;
2266 s
->register_topmost_timer
= !!(state
->desc
.flags
& WINED3D_SWAPCHAIN_REGISTER_TOPMOST_TIMER
);
2267 s
->set_topmost_timer
= true;
2269 TRACE("Old style was %08lx, %08lx, setting to %08lx, %08lx.\n",
2270 state
->style
, state
->exstyle
, s
->style
, s
->exstyle
);
2272 set_window_state(s
);
2276 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state
*state
,
2277 HWND window
, const RECT
*window_rect
)
2279 struct wined3d_window_state
*s
;
2280 LONG style
, exstyle
;
2282 if (!state
->style
&& !state
->exstyle
)
2285 if (!(s
= heap_alloc(sizeof(*s
))))
2289 s
->window_pos_after
= NULL
;
2290 s
->flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
2292 if ((state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE
)
2293 && !(state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
))
2295 s
->window_pos_after
= (state
->exstyle
& WS_EX_TOPMOST
) ? HWND_TOPMOST
: HWND_NOTOPMOST
;
2296 s
->flags
|= (state
->style
& WS_VISIBLE
) ? SWP_SHOWWINDOW
: SWP_HIDEWINDOW
;
2297 s
->flags
&= ~SWP_NOZORDER
;
2300 style
= GetWindowLongW(window
, GWL_STYLE
);
2301 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2303 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2304 * application, and we want to ignore them in the test below, since it's
2305 * not the application's fault that they changed. Additionally, we want to
2306 * preserve the current status of these flags (i.e. don't restore them) to
2307 * more closely emulate the behavior of Direct3D, which leaves these flags
2308 * alone when returning to windowed mode. */
2309 state
->style
^= (state
->style
^ style
) & WS_VISIBLE
;
2310 state
->exstyle
^= (state
->exstyle
^ exstyle
) & WS_EX_TOPMOST
;
2312 TRACE("Restoring window style of window %p to %08lx, %08lx.\n",
2313 window
, state
->style
, state
->exstyle
);
2315 s
->style
= state
->style
;
2316 s
->exstyle
= state
->exstyle
;
2317 /* Only restore the style if the application didn't modify it during the
2318 * fullscreen phase. Some applications change it before calling Reset()
2319 * when switching between windowed and fullscreen modes (HL2), some
2320 * depend on the original style (Eve Online). */
2321 s
->set_style
= style
== fullscreen_style(state
->style
) && exstyle
== fullscreen_exstyle(state
->exstyle
);
2322 s
->register_topmost_timer
= !!(state
->desc
.flags
& WINED3D_SWAPCHAIN_REGISTER_TOPMOST_TIMER
);
2323 s
->set_topmost_timer
= false;
2327 s
->x
= window_rect
->left
;
2328 s
->y
= window_rect
->top
;
2329 s
->width
= window_rect
->right
- window_rect
->left
;
2330 s
->height
= window_rect
->bottom
- window_rect
->top
;
2334 s
->x
= s
->y
= s
->width
= s
->height
= 0;
2335 s
->flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
2338 set_window_state(s
);
2340 /* Delete the old values. */
2345 HRESULT CDECL
wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state
*state
,
2346 const struct wined3d_swapchain_desc
*swapchain_desc
,
2347 const struct wined3d_display_mode
*mode
)
2349 struct wined3d_display_mode actual_mode
;
2350 struct wined3d_output_desc output_desc
;
2351 BOOL windowed
= state
->desc
.windowed
;
2354 TRACE("state %p, swapchain_desc %p, mode %p.\n", state
, swapchain_desc
, mode
);
2356 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2360 actual_mode
= *mode
;
2361 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2367 if (!swapchain_desc
->windowed
)
2369 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
2370 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
2371 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
2372 actual_mode
.format_id
= adapter_format_from_backbuffer_format(swapchain_desc
->output
->adapter
,
2373 swapchain_desc
->backbuffer_format
);
2374 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
2375 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2381 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2383 WARN("Failed to restore display modes for all outputs, hr %#lx.\n", hr
);
2392 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2394 if (FAILED(hr
= wined3d_output_get_display_mode(swapchain_desc
->output
, &actual_mode
,
2397 ERR("Failed to get display mode, hr %#lx.\n", hr
);
2398 return WINED3DERR_INVALIDCALL
;
2402 if (!swapchain_desc
->windowed
)
2404 unsigned int width
= actual_mode
.width
;
2405 unsigned int height
= actual_mode
.height
;
2407 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
2409 ERR("Failed to get output description, hr %#lx.\n", hr
);
2413 if (state
->desc
.windowed
)
2415 /* Switch from windowed to fullscreen */
2416 if (FAILED(hr
= wined3d_swapchain_state_setup_fullscreen(state
, state
->device_window
,
2417 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
, height
)))
2422 HWND window
= state
->device_window
;
2425 /* Fullscreen -> fullscreen mode change */
2426 filter
= wined3d_filter_messages(window
, TRUE
);
2427 MoveWindow(window
, output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
,
2429 ShowWindow(window
, SW_SHOW
);
2430 wined3d_filter_messages(window
, filter
);
2432 state
->d3d_mode
= actual_mode
;
2434 else if (!state
->desc
.windowed
)
2436 /* Fullscreen -> windowed switch */
2437 RECT
*window_rect
= NULL
;
2438 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
2439 window_rect
= &state
->original_window_rect
;
2440 wined3d_swapchain_state_restore_from_fullscreen(state
, state
->device_window
, window_rect
);
2443 state
->desc
.output
= swapchain_desc
->output
;
2444 state
->desc
.windowed
= swapchain_desc
->windowed
;
2446 if (windowed
!= state
->desc
.windowed
)
2447 state
->parent
->ops
->windowed_state_changed(state
->parent
, state
->desc
.windowed
);
2452 BOOL CDECL
wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state
*state
)
2454 TRACE("state %p.\n", state
);
2456 return state
->desc
.windowed
;
2459 void CDECL
wined3d_swapchain_state_get_size(const struct wined3d_swapchain_state
*state
,
2460 unsigned int *width
, unsigned int *height
)
2462 TRACE("state %p.\n", state
);
2464 *width
= state
->desc
.backbuffer_width
;
2465 *height
= state
->desc
.backbuffer_height
;
2468 void CDECL
wined3d_swapchain_state_destroy(struct wined3d_swapchain_state
*state
)
2470 wined3d_swapchain_state_cleanup(state
);
2474 HRESULT CDECL
wined3d_swapchain_state_create(const struct wined3d_swapchain_desc
*desc
,
2475 HWND window
, struct wined3d
*wined3d
, struct wined3d_swapchain_state_parent
*state_parent
,
2476 struct wined3d_swapchain_state
**state
)
2478 struct wined3d_swapchain_state
*s
;
2481 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc
, window
, wined3d
, state
);
2483 if (!(s
= heap_alloc_zero(sizeof(*s
))))
2484 return E_OUTOFMEMORY
;
2486 if (FAILED(hr
= wined3d_swapchain_state_init(s
, desc
, window
, wined3d
, state_parent
)))