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"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
27 void wined3d_swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
32 TRACE("Destroying swapchain %p.\n", swapchain
);
34 wined3d_swapchain_state_cleanup(&swapchain
->state
);
35 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
37 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
38 * is the last buffer to be destroyed, FindContext() depends on that. */
39 if (swapchain
->front_buffer
)
41 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
42 if (wined3d_texture_decref(swapchain
->front_buffer
))
43 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
44 swapchain
->front_buffer
= NULL
;
47 if (swapchain
->back_buffers
)
49 i
= swapchain
->state
.desc
.backbuffer_count
;
53 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
54 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
55 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
57 heap_free(swapchain
->back_buffers
);
58 swapchain
->back_buffers
= NULL
;
61 /* Restore the screen resolution if we rendered in fullscreen.
62 * This will restore the screen resolution to what it was before creating
63 * the swapchain. In case of d3d8 and d3d9 this will be the original
64 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
65 * sets the resolution before starting up Direct3D, thus orig_width and
66 * orig_height will be equal to the modes in the presentation params. */
67 if (!swapchain
->state
.desc
.windowed
)
69 if (swapchain
->state
.desc
.auto_restore_display_mode
)
71 if (FAILED(hr
= wined3d_restore_display_modes(swapchain
->device
->wined3d
)))
72 ERR("Failed to restore display mode, hr %#x.\n", hr
);
74 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
76 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
,
77 swapchain
->state
.device_window
, &swapchain
->state
.original_window_rect
);
78 wined3d_device_release_focus_window(swapchain
->device
);
83 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
, swapchain
->state
.device_window
, NULL
);
88 static void wined3d_swapchain_gl_destroy_object(void *object
)
90 wined3d_swapchain_gl_destroy_contexts(object
);
93 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl
*swapchain_gl
)
95 struct wined3d_cs
*cs
= swapchain_gl
->s
.device
->cs
;
97 wined3d_swapchain_cleanup(&swapchain_gl
->s
);
99 wined3d_cs_destroy_object(cs
, wined3d_swapchain_gl_destroy_object
, swapchain_gl
);
100 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
102 if (swapchain_gl
->backup_dc
)
104 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain_gl
->backup_wnd
, swapchain_gl
->backup_dc
);
106 wined3d_release_dc(swapchain_gl
->backup_wnd
, swapchain_gl
->backup_dc
);
107 DestroyWindow(swapchain_gl
->backup_wnd
);
111 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
113 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
114 const struct wined3d_vk_info
*vk_info
;
118 TRACE("swapchain_vk %p.\n", swapchain_vk
);
120 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
122 if ((vr
= VK_CALL(vkQueueWaitIdle(device_vk
->vk_queue
))) < 0)
123 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr
));
124 heap_free(swapchain_vk
->vk_images
);
125 for (i
= 0; i
< swapchain_vk
->image_count
; ++i
)
127 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
128 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
130 heap_free(swapchain_vk
->vk_semaphores
);
131 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, NULL
));
132 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, swapchain_vk
->vk_surface
, NULL
));
135 static void wined3d_swapchain_vk_destroy_object(void *object
)
137 wined3d_swapchain_vk_destroy_vulkan_swapchain(object
);
140 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk
*swapchain_vk
)
142 struct wined3d_cs
*cs
= swapchain_vk
->s
.device
->cs
;
144 wined3d_cs_destroy_object(cs
, wined3d_swapchain_vk_destroy_object
, swapchain_vk
);
145 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
147 wined3d_swapchain_cleanup(&swapchain_vk
->s
);
150 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
152 ULONG refcount
= InterlockedIncrement(&swapchain
->ref
);
154 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
159 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
161 ULONG refcount
= InterlockedDecrement(&swapchain
->ref
);
163 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
167 struct wined3d_device
*device
;
169 wined3d_mutex_lock();
171 device
= swapchain
->device
;
172 if (device
->swapchain_count
&& device
->swapchains
[0] == swapchain
)
173 wined3d_device_uninit_3d(device
);
174 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
176 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
177 swapchain
->device
->adapter
->adapter_ops
->adapter_destroy_swapchain(swapchain
);
179 wined3d_mutex_unlock();
185 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
187 TRACE("swapchain %p.\n", swapchain
);
189 return swapchain
->parent
;
192 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
195 window
= swapchain
->state
.device_window
;
196 if (window
== swapchain
->win_handle
)
199 TRACE("Setting swapchain %p window from %p to %p.\n",
200 swapchain
, swapchain
->win_handle
, window
);
202 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
204 swapchain
->win_handle
= window
;
207 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
208 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
209 unsigned int swap_interval
, DWORD flags
)
213 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
214 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
215 dst_window_override
, swap_interval
, flags
);
218 FIXME("Ignoring flags %#x.\n", flags
);
220 wined3d_mutex_lock();
222 if (!swapchain
->back_buffers
)
224 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
225 wined3d_mutex_unlock();
226 return WINED3DERR_INVALIDCALL
;
231 SetRect(&s
, 0, 0, swapchain
->state
.desc
.backbuffer_width
,
232 swapchain
->state
.desc
.backbuffer_height
);
238 GetClientRect(swapchain
->win_handle
, &d
);
242 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
243 dst_rect
, dst_window_override
, swap_interval
, flags
);
245 wined3d_mutex_unlock();
250 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
251 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
253 RECT src_rect
, dst_rect
;
255 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
257 SetRect(&src_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
, swapchain
->front_buffer
->resource
.height
);
260 if (swapchain
->state
.desc
.windowed
)
262 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
263 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
264 wine_dbgstr_rect(&dst_rect
));
267 return wined3d_device_context_blt(&swapchain
->device
->cs
->c
, dst_texture
, sub_resource_idx
, &dst_rect
,
268 swapchain
->front_buffer
, 0, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
271 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
272 UINT back_buffer_idx
)
274 TRACE("swapchain %p, back_buffer_idx %u.\n",
275 swapchain
, back_buffer_idx
);
277 /* Return invalid if there is no backbuffer array, otherwise it will
278 * crash when ddraw is used (there swapchain->back_buffers is always
279 * NULL). We need this because this function is called from
280 * stateblock_init_default_state() to get the default scissorrect
282 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->state
.desc
.backbuffer_count
)
284 WARN("Invalid back buffer index.\n");
285 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
286 * here in wined3d to avoid problems in other libs. */
290 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
292 return swapchain
->back_buffers
[back_buffer_idx
];
295 struct wined3d_output
* wined3d_swapchain_get_output(const struct wined3d_swapchain
*swapchain
)
297 TRACE("swapchain %p.\n", swapchain
);
299 return swapchain
->state
.desc
.output
;
302 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
303 struct wined3d_raster_status
*raster_status
)
305 struct wined3d_output
*output
;
307 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
309 output
= wined3d_swapchain_get_output(swapchain
);
312 ERR("Failed to get output from swapchain %p.\n", swapchain
);
316 return wined3d_output_get_raster_status(output
, raster_status
);
319 struct wined3d_swapchain_state
* CDECL
wined3d_swapchain_get_state(struct wined3d_swapchain
*swapchain
)
321 return &swapchain
->state
;
324 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
325 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
327 struct wined3d_output
*output
;
330 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
332 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
334 ERR("Failed to get output from swapchain %p.\n", swapchain
);
338 hr
= wined3d_output_get_display_mode(output
, mode
, rotation
);
340 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
341 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
346 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
348 TRACE("swapchain %p.\n", swapchain
);
350 return swapchain
->device
;
353 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
354 struct wined3d_swapchain_desc
*desc
)
356 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
358 *desc
= swapchain
->state
.desc
;
361 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
362 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
364 struct wined3d_output
*output
;
366 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
369 FIXME("Ignoring flags %#x.\n", flags
);
371 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
373 ERR("Failed to get output from swapchain %p.\n", swapchain
);
377 return wined3d_output_set_gamma_ramp(output
, ramp
);
380 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
382 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
384 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
386 swapchain
->palette
= palette
;
389 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
390 struct wined3d_gamma_ramp
*ramp
)
392 struct wined3d_output
*output
;
394 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
396 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
398 ERR("Failed to get output from swapchain %p.\n", swapchain
);
402 return wined3d_output_get_gamma_ramp(output
, ramp
);
405 /* The is a fallback for cases where we e.g. can't create a GL context or
406 * Vulkan swapchain for the swapchain window. */
407 static void swapchain_blit_gdi(struct wined3d_swapchain
*swapchain
,
408 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
410 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
411 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
412 D3DKMT_CREATEDCFROMMEMORY create_desc
;
413 const struct wined3d_format
*format
;
414 unsigned int row_pitch
, slice_pitch
;
419 static unsigned int once
;
421 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
422 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
425 FIXME("Using GDI present.\n");
427 format
= back_buffer
->resource
.format
;
428 if (!format
->ddi_format
)
430 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
434 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_SYSMEM
);
435 wined3d_texture_get_pitch(back_buffer
, 0, &row_pitch
, &slice_pitch
);
437 create_desc
.pMemory
= back_buffer
->resource
.heap_memory
;
438 create_desc
.Format
= format
->ddi_format
;
439 create_desc
.Width
= wined3d_texture_get_level_width(back_buffer
, 0);
440 create_desc
.Height
= wined3d_texture_get_level_height(back_buffer
, 0);
441 create_desc
.Pitch
= row_pitch
;
442 create_desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
443 create_desc
.pColorTable
= NULL
;
445 status
= D3DKMTCreateDCFromMemory(&create_desc
);
446 DeleteDC(create_desc
.hDeviceDc
);
449 WARN("Failed to create DC, status %#x.\n", status
);
453 src_dc
= create_desc
.hDc
;
454 bitmap
= create_desc
.hBitmap
;
456 TRACE("Created source DC %p, bitmap %p for backbuffer %p.\n", src_dc
, bitmap
, back_buffer
);
458 if (!(dst_dc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
459 ERR("Failed to get destination DC.\n");
461 if (!StretchBlt(dst_dc
, dst_rect
->left
, dst_rect
->top
, dst_rect
->right
- dst_rect
->left
,
462 dst_rect
->bottom
- dst_rect
->top
, src_dc
, src_rect
->left
, src_rect
->top
,
463 src_rect
->right
- src_rect
->left
, src_rect
->bottom
- src_rect
->top
, SRCCOPY
))
464 ERR("Failed to blit.\n");
466 ReleaseDC(swapchain
->win_handle
, dst_dc
);
467 destroy_desc
.hDc
= src_dc
;
468 destroy_desc
.hBitmap
= bitmap
;
469 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
470 ERR("Failed to destroy src dc, status %#x.\n", status
);
473 /* A GL context is provided by the caller */
474 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
475 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
477 struct wined3d_texture
*texture
= swapchain
->back_buffers
[0];
478 struct wined3d_device
*device
= swapchain
->device
;
479 enum wined3d_texture_filter_type filter
;
482 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
483 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
485 if ((src_rect
->right
- src_rect
->left
== dst_rect
->right
- dst_rect
->left
486 && src_rect
->bottom
- src_rect
->top
== dst_rect
->bottom
- dst_rect
->top
)
487 || is_complex_fixup(texture
->resource
.format
->color_fixup
))
488 filter
= WINED3D_TEXF_NONE
;
490 filter
= WINED3D_TEXF_LINEAR
;
492 location
= WINED3D_LOCATION_TEXTURE_RGB
;
493 if (texture
->resource
.multisample_type
)
494 location
= WINED3D_LOCATION_RB_RESOLVED
;
496 wined3d_texture_validate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
497 device
->blitter
->ops
->blitter_blit(device
->blitter
, WINED3D_BLIT_OP_COLOR_BLIT
, context
, texture
, 0,
498 location
, src_rect
, texture
, 0, WINED3D_LOCATION_DRAWABLE
, dst_rect
, NULL
, filter
, NULL
);
499 wined3d_texture_invalidate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
502 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain
*swapchain
,
503 struct wined3d_context_gl
*context_gl
, unsigned int swap_interval
)
505 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
507 swap_interval
= swap_interval
<= 4 ? swap_interval
: 1;
508 if (swapchain
->swap_interval
== swap_interval
)
511 swapchain
->swap_interval
= swap_interval
;
513 if (!gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
516 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
518 ERR("Failed to set swap interval %u for context %p, last error %#x.\n",
519 swap_interval
, context_gl
, GetLastError());
523 /* Context activation is done by the caller. */
524 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
526 struct wined3d_texture_sub_resource
*sub_resource
;
527 struct wined3d_texture_gl
*texture
, *texture_prev
;
528 struct gl_texture tex0
;
532 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
534 if (swapchain
->state
.desc
.backbuffer_count
< 2 || wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
537 texture_prev
= wined3d_texture_gl(swapchain
->back_buffers
[0]);
539 /* Back buffer 0 is already in the draw binding. */
540 tex0
= texture_prev
->texture_rgb
;
541 rb0
= texture_prev
->rb_multisample
;
542 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
544 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
546 texture
= wined3d_texture_gl(swapchain
->back_buffers
[i
]);
547 sub_resource
= &texture
->t
.sub_resources
[0];
549 if (!(sub_resource
->locations
& supported_locations
))
550 wined3d_texture_load_location(&texture
->t
, 0, context
, texture
->t
.resource
.draw_binding
);
552 texture_prev
->texture_rgb
= texture
->texture_rgb
;
553 texture_prev
->rb_multisample
= texture
->rb_multisample
;
555 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
556 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
558 texture_prev
= texture
;
561 texture_prev
->texture_rgb
= tex0
;
562 texture_prev
->rb_multisample
= rb0
;
564 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
565 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
567 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
570 static bool swapchain_present_is_partial_copy(struct wined3d_swapchain
*swapchain
, const RECT
*dst_rect
)
572 enum wined3d_swap_effect swap_effect
= swapchain
->state
.desc
.swap_effect
;
576 if (swap_effect
!= WINED3D_SWAP_EFFECT_COPY
&& swap_effect
!= WINED3D_SWAP_EFFECT_COPY_VSYNC
)
579 GetClientRect(swapchain
->win_handle
, &client_rect
);
581 t
= client_rect
.right
- client_rect
.left
;
582 if ((dst_rect
->left
&& dst_rect
->right
) || abs(dst_rect
->right
- dst_rect
->left
) != t
)
584 t
= client_rect
.bottom
- client_rect
.top
;
585 if ((dst_rect
->top
&& dst_rect
->bottom
) || abs(dst_rect
->bottom
- dst_rect
->top
) != t
)
591 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
592 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, DWORD flags
)
594 struct wined3d_swapchain_gl
*swapchain_gl
= wined3d_swapchain_gl(swapchain
);
595 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
596 const struct wined3d_pixel_format
*pixel_format
;
597 const struct wined3d_gl_info
*gl_info
;
598 struct wined3d_context_gl
*context_gl
;
599 struct wined3d_context
*context
;
601 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
, 0);
602 context_gl
= wined3d_context_gl(context
);
603 if (!context_gl
->valid
)
605 context_release(context
);
606 WARN("Invalid context, skipping present.\n");
610 TRACE("Presenting DC %p.\n", context_gl
->dc
);
612 pixel_format
= &wined3d_adapter_gl(swapchain
->device
->adapter
)->pixel_formats
[context_gl
->pixel_format
];
613 if (context_gl
->dc
== swapchain_gl
->backup_dc
|| (pixel_format
->swap_method
!= WGL_SWAP_COPY_ARB
614 && swapchain_present_is_partial_copy(swapchain
, dst_rect
)))
616 swapchain_blit_gdi(swapchain
, context
, src_rect
, dst_rect
);
620 gl_info
= context_gl
->gl_info
;
622 swapchain_gl_set_swap_interval(swapchain
, context_gl
, swap_interval
);
624 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
626 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
627 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
629 if (swapchain_gl
->context_count
> 1)
630 gl_info
->gl_ops
.gl
.p_glFinish();
632 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
633 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context_gl
->dc
);
636 wined3d_context_gl_submit_command_fence(context_gl
);
638 wined3d_swapchain_gl_rotate(swapchain
, context
);
640 TRACE("SwapBuffers called, Starting new frame\n");
642 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
643 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
645 context_release(context
);
648 static void swapchain_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
650 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
651 struct wined3d_context
*context
;
653 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
654 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
655 context_release(context
);
656 SetRectEmpty(&swapchain
->front_buffer_update
);
659 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
661 swapchain_gl_present
,
662 swapchain_frontbuffer_updated
,
665 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk
*swapchain_vk
,
666 VkPresentModeKHR vk_present_mode
)
668 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
669 const struct wined3d_vk_info
*vk_info
;
670 struct wined3d_adapter_vk
*adapter_vk
;
671 VkPhysicalDevice vk_physical_device
;
672 VkPresentModeKHR
*vk_modes
;
673 bool supported
= false;
677 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
678 vk_physical_device
= adapter_vk
->physical_device
;
679 vk_info
= &adapter_vk
->vk_info
;
681 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
682 swapchain_vk
->vk_surface
, &count
, NULL
))) < 0)
684 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr
));
688 if (!(vk_modes
= heap_calloc(count
, sizeof(*vk_modes
))))
691 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
692 swapchain_vk
->vk_surface
, &count
, vk_modes
))) < 0)
694 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr
));
698 for (i
= 0; i
< count
; ++i
)
700 if (vk_modes
[i
] == vk_present_mode
)
712 static VkFormat
get_swapchain_fallback_format(VkFormat vk_format
)
716 case VK_FORMAT_R8G8B8A8_SRGB
:
717 return VK_FORMAT_B8G8R8A8_SRGB
;
718 case VK_FORMAT_R8G8B8A8_UNORM
:
719 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
720 case VK_FORMAT_R16G16B16A16_SFLOAT
:
721 return VK_FORMAT_B8G8R8A8_UNORM
;
723 WARN("Unhandled format %#x.\n", vk_format
);
724 return VK_FORMAT_UNDEFINED
;
728 static VkFormat
wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk
*swapchain_vk
,
729 VkSurfaceKHR vk_surface
)
731 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
732 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
733 const struct wined3d_vk_info
*vk_info
;
734 struct wined3d_adapter_vk
*adapter_vk
;
735 const struct wined3d_format
*format
;
736 VkPhysicalDevice vk_physical_device
;
737 VkSurfaceFormatKHR
*vk_formats
;
738 uint32_t format_count
, i
;
742 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
743 vk_physical_device
= adapter_vk
->physical_device
;
744 vk_info
= &adapter_vk
->vk_info
;
746 if ((format
= wined3d_get_format(&adapter_vk
->a
, desc
->backbuffer_format
, WINED3D_BIND_RENDER_TARGET
)))
747 vk_format
= wined3d_format_vk(format
)->vk_format
;
749 vk_format
= VK_FORMAT_B8G8R8A8_UNORM
;
751 vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
, vk_surface
, &format_count
, NULL
));
752 if (vr
< 0 || !format_count
)
754 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr
));
755 return VK_FORMAT_UNDEFINED
;
758 if (!(vk_formats
= heap_calloc(format_count
, sizeof(*vk_formats
))))
759 return VK_FORMAT_UNDEFINED
;
761 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
,
762 vk_surface
, &format_count
, vk_formats
))) < 0)
764 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr
));
765 heap_free(vk_formats
);
766 return VK_FORMAT_UNDEFINED
;
769 for (i
= 0; i
< format_count
; ++i
)
771 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
774 if (i
== format_count
)
776 /* Try to create a swapchain with format conversion. */
777 vk_format
= get_swapchain_fallback_format(vk_format
);
778 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
779 for (i
= 0; i
< format_count
; ++i
)
781 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
785 heap_free(vk_formats
);
786 if (i
== format_count
)
788 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
789 return VK_FORMAT_UNDEFINED
;
792 TRACE("Using Vulkan swapchain format %#x.\n", vk_format
);
797 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk
*swapchain_vk
,
798 VkSwapchainKHR vk_swapchain
)
800 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
801 const struct wined3d_vk_info
*vk_info
;
802 VkSemaphoreCreateInfo semaphore_info
;
803 uint32_t image_count
, i
;
806 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
808 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
, vk_swapchain
, &image_count
, NULL
))) < 0)
810 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr
));
814 if (!(swapchain_vk
->vk_images
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_images
))))
816 ERR("Failed to allocate images array.\n");
820 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
,
821 vk_swapchain
, &image_count
, swapchain_vk
->vk_images
))) < 0)
823 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr
));
824 heap_free(swapchain_vk
->vk_images
);
828 if (!(swapchain_vk
->vk_semaphores
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_semaphores
))))
830 ERR("Failed to allocate semaphores array.\n");
831 heap_free(swapchain_vk
->vk_images
);
835 semaphore_info
.sType
= VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
;
836 semaphore_info
.pNext
= NULL
;
837 semaphore_info
.flags
= 0;
838 for (i
= 0; i
< image_count
; ++i
)
840 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
841 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].available
))) < 0)
843 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
847 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
848 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].presentable
))) < 0)
850 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
854 swapchain_vk
->image_count
= image_count
;
859 for (i
= 0; i
< image_count
; ++i
)
861 if (swapchain_vk
->vk_semaphores
[i
].available
)
862 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
863 if (swapchain_vk
->vk_semaphores
[i
].presentable
)
864 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
866 heap_free(swapchain_vk
->vk_semaphores
);
867 heap_free(swapchain_vk
->vk_images
);
871 static HRESULT
wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
873 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
874 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
875 VkSwapchainCreateInfoKHR vk_swapchain_desc
;
876 VkWin32SurfaceCreateInfoKHR surface_desc
;
877 unsigned int width
, height
, image_count
;
878 const struct wined3d_vk_info
*vk_info
;
879 VkSurfaceCapabilitiesKHR surface_caps
;
880 struct wined3d_adapter_vk
*adapter_vk
;
881 VkPresentModeKHR vk_present_mode
;
882 VkSwapchainKHR vk_swapchain
;
883 VkImageUsageFlags usage
;
884 VkSurfaceKHR vk_surface
;
890 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
891 vk_info
= &adapter_vk
->vk_info
;
893 surface_desc
.sType
= VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR
;
894 surface_desc
.pNext
= NULL
;
895 surface_desc
.flags
= 0;
896 surface_desc
.hinstance
= (HINSTANCE
)GetWindowLongPtrW(swapchain_vk
->s
.win_handle
, GWLP_HINSTANCE
);
897 surface_desc
.hwnd
= swapchain_vk
->s
.win_handle
;
898 if ((vr
= VK_CALL(vkCreateWin32SurfaceKHR(vk_info
->instance
, &surface_desc
, NULL
, &vk_surface
))) < 0)
900 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr
));
903 swapchain_vk
->vk_surface
= vk_surface
;
905 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk
->physical_device
,
906 device_vk
->vk_queue_family_index
, vk_surface
, &supported
))) < 0 || !supported
)
908 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr
));
912 if ((vk_format
= wined3d_swapchain_vk_select_vk_format(swapchain_vk
, vk_surface
)) == VK_FORMAT_UNDEFINED
)
914 ERR("Failed to select swapchain format.\n");
918 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk
->physical_device
,
919 swapchain_vk
->vk_surface
, &surface_caps
))) < 0)
921 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr
));
925 image_count
= desc
->backbuffer_count
;
926 if (image_count
< surface_caps
.minImageCount
)
927 image_count
= surface_caps
.minImageCount
;
928 else if (surface_caps
.maxImageCount
&& image_count
> surface_caps
.maxImageCount
)
929 image_count
= surface_caps
.maxImageCount
;
931 if (image_count
!= desc
->backbuffer_count
)
932 WARN("Image count %u is not supported (%u-%u).\n", desc
->backbuffer_count
,
933 surface_caps
.minImageCount
, surface_caps
.maxImageCount
);
935 GetClientRect(swapchain_vk
->s
.win_handle
, &client_rect
);
937 width
= client_rect
.right
- client_rect
.left
;
938 if (width
< surface_caps
.minImageExtent
.width
)
939 width
= surface_caps
.minImageExtent
.width
;
940 else if (width
> surface_caps
.maxImageExtent
.width
)
941 width
= surface_caps
.maxImageExtent
.width
;
943 height
= client_rect
.bottom
- client_rect
.top
;
944 if (height
< surface_caps
.minImageExtent
.height
)
945 height
= surface_caps
.minImageExtent
.height
;
946 else if (height
> surface_caps
.maxImageExtent
.height
)
947 height
= surface_caps
.maxImageExtent
.height
;
949 if (width
!= client_rect
.right
- client_rect
.left
|| height
!= client_rect
.bottom
- client_rect
.top
)
950 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
951 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
952 surface_caps
.minImageExtent
.width
, surface_caps
.maxImageExtent
.width
,
953 surface_caps
.minImageExtent
.height
, surface_caps
.maxImageExtent
.height
);
955 usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
956 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
957 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
958 if (!(usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) || !(usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
))
959 WARN("Transfer not supported for swapchain images.\n");
961 if (!(surface_caps
.supportedCompositeAlpha
& VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
))
963 FIXME("Unsupported alpha mode, %#x.\n", surface_caps
.supportedCompositeAlpha
);
967 vk_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
968 if (!swapchain_vk
->s
.swap_interval
)
970 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk
, VK_PRESENT_MODE_IMMEDIATE_KHR
))
971 vk_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
973 FIXME("Unsupported swap interval %u.\n", swapchain_vk
->s
.swap_interval
);
976 vk_swapchain_desc
.sType
= VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
;
977 vk_swapchain_desc
.pNext
= NULL
;
978 vk_swapchain_desc
.flags
= 0;
979 vk_swapchain_desc
.surface
= vk_surface
;
980 vk_swapchain_desc
.minImageCount
= image_count
;
981 vk_swapchain_desc
.imageFormat
= vk_format
;
982 vk_swapchain_desc
.imageColorSpace
= VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
;
983 vk_swapchain_desc
.imageExtent
.width
= width
;
984 vk_swapchain_desc
.imageExtent
.height
= height
;
985 vk_swapchain_desc
.imageArrayLayers
= 1;
986 vk_swapchain_desc
.imageUsage
= usage
;
987 vk_swapchain_desc
.imageSharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
988 vk_swapchain_desc
.queueFamilyIndexCount
= 0;
989 vk_swapchain_desc
.pQueueFamilyIndices
= NULL
;
990 vk_swapchain_desc
.preTransform
= surface_caps
.currentTransform
;
991 vk_swapchain_desc
.compositeAlpha
= VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
992 vk_swapchain_desc
.presentMode
= vk_present_mode
;
993 vk_swapchain_desc
.clipped
= VK_TRUE
;
994 vk_swapchain_desc
.oldSwapchain
= VK_NULL_HANDLE
;
995 if ((vr
= VK_CALL(vkCreateSwapchainKHR(device_vk
->vk_device
, &vk_swapchain_desc
, NULL
, &vk_swapchain
))) < 0)
997 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr
));
1000 swapchain_vk
->vk_swapchain
= vk_swapchain
;
1002 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk
, vk_swapchain
))
1004 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, vk_swapchain
, NULL
));
1008 swapchain_vk
->width
= width
;
1009 swapchain_vk
->height
= height
;
1014 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, vk_surface
, NULL
));
1018 static HRESULT
wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk
*swapchain_vk
)
1020 TRACE("swapchain_vk %p.\n", swapchain_vk
);
1022 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk
);
1024 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
);
1027 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk
*swapchain_vk
,
1028 unsigned int swap_interval
)
1030 if (swap_interval
> 1)
1032 if (swap_interval
<= 4)
1033 FIXME("Unsupported swap interval %u.\n", swap_interval
);
1037 if (swapchain_vk
->s
.swap_interval
== swap_interval
)
1040 swapchain_vk
->s
.swap_interval
= swap_interval
;
1041 wined3d_swapchain_vk_recreate(swapchain_vk
);
1044 static VkResult
wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk
*swapchain_vk
,
1045 struct wined3d_context_vk
*context_vk
, const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
)
1047 struct wined3d_texture_vk
*back_buffer_vk
= wined3d_texture_vk(swapchain_vk
->s
.back_buffers
[0]);
1048 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
1049 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
1050 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1051 VkCommandBuffer vk_command_buffer
;
1052 VkImageSubresourceRange vk_range
;
1053 VkPresentInfoKHR present_desc
;
1054 unsigned int present_idx
;
1055 VkImageLayout vk_layout
;
1062 static const VkPipelineStageFlags wait_stage
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
;
1064 TRACE("swapchain_vk %p, context_vk %p, src_rect %s, dst_rect %s, swap_interval %u.\n",
1065 swapchain_vk
, context_vk
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
), swap_interval
);
1067 wined3d_swapchain_vk_set_swap_interval(swapchain_vk
, swap_interval
);
1069 present_idx
= swapchain_vk
->current
++ % swapchain_vk
->image_count
;
1070 wined3d_context_vk_wait_command_buffer(context_vk
, swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
);
1071 if ((vr
= VK_CALL(vkAcquireNextImageKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, UINT64_MAX
,
1072 swapchain_vk
->vk_semaphores
[present_idx
].available
, VK_NULL_HANDLE
, &image_idx
))) < 0)
1074 WARN("Failed to acquire image, vr %s.\n", wined3d_debug_vkresult(vr
));
1078 if (dst_rect
->right
> swapchain_vk
->width
|| dst_rect
->bottom
> swapchain_vk
->height
)
1080 dst_rect_tmp
= *dst_rect
;
1081 if (dst_rect
->right
> swapchain_vk
->width
)
1082 dst_rect_tmp
.right
= swapchain_vk
->width
;
1083 if (dst_rect
->bottom
> swapchain_vk
->height
)
1084 dst_rect_tmp
.bottom
= swapchain_vk
->height
;
1085 dst_rect
= &dst_rect_tmp
;
1087 filter
= src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
1088 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
1089 ? VK_FILTER_LINEAR
: VK_FILTER_NEAREST
;
1090 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
1092 wined3d_context_vk_end_current_render_pass(context_vk
);
1094 vk_range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1095 vk_range
.baseMipLevel
= 0;
1096 vk_range
.levelCount
= 1;
1097 vk_range
.baseArrayLayer
= 0;
1098 vk_range
.layerCount
= 1;
1100 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1101 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1102 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1103 VK_ACCESS_TRANSFER_READ_BIT
,
1104 back_buffer_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1105 back_buffer_vk
->image
.vk_image
, &vk_range
);
1107 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1108 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1109 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
1110 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1111 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1113 blit
.srcSubresource
.aspectMask
= vk_range
.aspectMask
;
1114 blit
.srcSubresource
.mipLevel
= vk_range
.baseMipLevel
;
1115 blit
.srcSubresource
.baseArrayLayer
= vk_range
.baseArrayLayer
;
1116 blit
.srcSubresource
.layerCount
= vk_range
.layerCount
;
1117 blit
.srcOffsets
[0].x
= src_rect
->left
;
1118 blit
.srcOffsets
[0].y
= src_rect
->top
;
1119 blit
.srcOffsets
[0].z
= 0;
1120 blit
.srcOffsets
[1].x
= src_rect
->right
;
1121 blit
.srcOffsets
[1].y
= src_rect
->bottom
;
1122 blit
.srcOffsets
[1].z
= 1;
1123 blit
.dstSubresource
= blit
.srcSubresource
;
1124 blit
.dstOffsets
[0].x
= dst_rect
->left
;
1125 blit
.dstOffsets
[0].y
= dst_rect
->top
;
1126 blit
.dstOffsets
[0].z
= 0;
1127 blit
.dstOffsets
[1].x
= dst_rect
->right
;
1128 blit
.dstOffsets
[1].y
= dst_rect
->bottom
;
1129 blit
.dstOffsets
[1].z
= 1;
1130 VK_CALL(vkCmdBlitImage(vk_command_buffer
,
1131 back_buffer_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1132 swapchain_vk
->vk_images
[image_idx
], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1135 wined3d_context_vk_reference_texture(context_vk
, back_buffer_vk
);
1136 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1137 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
1138 VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
1139 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
1140 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1142 if (desc
->swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
|| desc
->swap_effect
== WINED3D_SWAP_EFFECT_FLIP_DISCARD
)
1143 vk_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
1145 vk_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
1146 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1147 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1148 VK_ACCESS_TRANSFER_READ_BIT
,
1149 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1150 vk_layout
, back_buffer_vk
->layout
,
1151 back_buffer_vk
->image
.vk_image
, &vk_range
);
1152 back_buffer_vk
->bind_mask
= 0;
1154 swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
= context_vk
->current_command_buffer
.id
;
1155 wined3d_context_vk_submit_command_buffer(context_vk
,
1156 1, &swapchain_vk
->vk_semaphores
[present_idx
].available
, &wait_stage
,
1157 1, &swapchain_vk
->vk_semaphores
[present_idx
].presentable
);
1159 present_desc
.sType
= VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
;
1160 present_desc
.pNext
= NULL
;
1161 present_desc
.waitSemaphoreCount
= 1;
1162 present_desc
.pWaitSemaphores
= &swapchain_vk
->vk_semaphores
[present_idx
].presentable
;
1163 present_desc
.swapchainCount
= 1;
1164 present_desc
.pSwapchains
= &swapchain_vk
->vk_swapchain
;
1165 present_desc
.pImageIndices
= &image_idx
;
1166 present_desc
.pResults
= NULL
;
1167 if ((vr
= VK_CALL(vkQueuePresentKHR(device_vk
->vk_queue
, &present_desc
))))
1168 WARN("Present returned vr %s.\n", wined3d_debug_vkresult(vr
));
1172 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context_vk
*context_vk
)
1174 struct wined3d_texture_sub_resource
*sub_resource
;
1175 struct wined3d_texture_vk
*texture
, *texture_prev
;
1176 struct wined3d_image_vk image0
;
1177 VkDescriptorImageInfo vk_info0
;
1178 VkImageLayout vk_layout0
;
1182 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
1184 if (swapchain
->state
.desc
.backbuffer_count
< 2)
1187 texture_prev
= wined3d_texture_vk(swapchain
->back_buffers
[0]);
1189 /* Back buffer 0 is already in the draw binding. */
1190 image0
= texture_prev
->image
;
1191 vk_layout0
= texture_prev
->layout
;
1192 vk_info0
= texture_prev
->default_image_info
;
1193 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
1195 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1197 texture
= wined3d_texture_vk(swapchain
->back_buffers
[i
]);
1198 sub_resource
= &texture
->t
.sub_resources
[0];
1200 if (!(sub_resource
->locations
& supported_locations
))
1201 wined3d_texture_load_location(&texture
->t
, 0, &context_vk
->c
, texture
->t
.resource
.draw_binding
);
1203 texture_prev
->image
= texture
->image
;
1204 texture_prev
->layout
= texture
->layout
;
1205 texture_prev
->default_image_info
= texture
->default_image_info
;
1207 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
1208 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
1210 texture_prev
= texture
;
1213 texture_prev
->image
= image0
;
1214 texture_prev
->layout
= vk_layout0
;
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 %#x.\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
, DWORD 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
, DWORD
*quality
)
1356 const struct wined3d_adapter
*adapter
;
1357 const struct wined3d_gl_info
*gl_info
;
1358 const struct wined3d_format
*format
;
1359 enum wined3d_multisample_type t
;
1361 if (wined3d_settings
.sample_count
== ~0u)
1364 adapter
= swapchain
->device
->adapter
;
1365 gl_info
= &adapter
->gl_info
;
1366 if (!(format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
)))
1369 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
1370 while (!(format
->multisample_types
& 1u << (t
- 1)))
1372 TRACE("Using sample count %u.\n", t
);
1377 void swapchain_set_max_frame_latency(struct wined3d_swapchain
*swapchain
, const struct wined3d_device
*device
)
1379 /* Subtract 1 for the implicit OpenGL latency. */
1380 swapchain
->max_frame_latency
= device
->max_frame_latency
>= 2 ? device
->max_frame_latency
- 1 : 1;
1383 static enum wined3d_format_id
adapter_format_from_backbuffer_format(const struct wined3d_adapter
*adapter
,
1384 enum wined3d_format_id format_id
)
1386 const struct wined3d_format
*backbuffer_format
;
1388 backbuffer_format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
);
1389 return pixelformat_for_depth(backbuffer_format
->byte_count
* CHAR_BIT
);
1392 static HRESULT
wined3d_swapchain_state_init(struct wined3d_swapchain_state
*state
,
1393 const struct wined3d_swapchain_desc
*desc
, HWND window
, struct wined3d
*wined3d
,
1394 struct wined3d_swapchain_state_parent
*parent
)
1398 state
->desc
= *desc
;
1400 if (FAILED(hr
= wined3d_output_get_display_mode(desc
->output
, &state
->original_mode
, NULL
)))
1402 ERR("Failed to get current display mode, hr %#x.\n", hr
);
1406 if (!desc
->windowed
)
1408 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1410 state
->d3d_mode
.width
= desc
->backbuffer_width
;
1411 state
->d3d_mode
.height
= desc
->backbuffer_height
;
1412 state
->d3d_mode
.format_id
= adapter_format_from_backbuffer_format(desc
->output
->adapter
,
1413 desc
->backbuffer_format
);
1414 state
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1415 state
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1419 state
->d3d_mode
= state
->original_mode
;
1423 GetWindowRect(window
, &state
->original_window_rect
);
1424 state
->wined3d
= wined3d
;
1425 state
->device_window
= window
;
1426 state
->parent
= parent
;
1428 if (desc
->flags
& WINED3D_SWAPCHAIN_REGISTER_STATE
)
1429 wined3d_swapchain_state_register(state
);
1434 static HRESULT
wined3d_swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
1435 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1436 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1437 const struct wined3d_swapchain_ops
*swapchain_ops
)
1439 struct wined3d_resource_desc texture_desc
;
1440 struct wined3d_output_desc output_desc
;
1441 BOOL displaymode_set
= FALSE
;
1442 DWORD texture_flags
= 0;
1443 HRESULT hr
= E_FAIL
;
1448 wined3d_mutex_lock();
1450 if (desc
->backbuffer_count
> 1)
1452 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1453 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1456 if (desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
1457 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
1458 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
1459 FIXME("Unimplemented swap effect %#x.\n", desc
->swap_effect
);
1461 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
1462 if (FAILED(hr
= wined3d_swapchain_state_init(&swapchain
->state
, desc
, window
, device
->wined3d
, state_parent
)))
1464 ERR("Failed to initialise swapchain state, hr %#x.\n", hr
);
1465 wined3d_mutex_unlock();
1469 swapchain
->swapchain_ops
= swapchain_ops
;
1470 swapchain
->device
= device
;
1471 swapchain
->parent
= parent
;
1472 swapchain
->parent_ops
= parent_ops
;
1474 swapchain
->win_handle
= window
;
1475 swapchain
->swap_interval
= WINED3D_SWAP_INTERVAL_DEFAULT
;
1476 swapchain_set_max_frame_latency(swapchain
, device
);
1478 GetClientRect(window
, &client_rect
);
1481 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect
));
1483 if (!desc
->backbuffer_width
)
1485 desc
->backbuffer_width
= client_rect
.right
? client_rect
.right
: 8;
1486 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
1488 if (!desc
->backbuffer_height
)
1490 desc
->backbuffer_height
= client_rect
.bottom
? client_rect
.bottom
: 8;
1491 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
1494 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
1496 desc
->backbuffer_format
= swapchain
->state
.original_mode
.format_id
;
1497 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->state
.original_mode
.format_id
));
1502 if (FAILED(hr
= wined3d_output_get_desc(desc
->output
, &output_desc
)))
1504 ERR("Failed to get output description, hr %#x.\n", hr
);
1508 wined3d_swapchain_state_setup_fullscreen(&swapchain
->state
, window
,
1509 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, desc
->backbuffer_width
,
1510 desc
->backbuffer_height
);
1512 swapchain
->state
.desc
= *desc
;
1513 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->state
.desc
.backbuffer_format
,
1514 &swapchain
->state
.desc
.multisample_type
, &swapchain
->state
.desc
.multisample_quality
);
1516 TRACE("Creating front buffer.\n");
1518 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1519 texture_desc
.format
= swapchain
->state
.desc
.backbuffer_format
;
1520 texture_desc
.multisample_type
= swapchain
->state
.desc
.multisample_type
;
1521 texture_desc
.multisample_quality
= swapchain
->state
.desc
.multisample_quality
;
1522 texture_desc
.usage
= 0;
1523 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1524 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1525 texture_desc
.bind_flags
= 0;
1526 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1527 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1529 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1530 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
)
1531 texture_desc
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1532 texture_desc
.width
= swapchain
->state
.desc
.backbuffer_width
;
1533 texture_desc
.height
= swapchain
->state
.desc
.backbuffer_height
;
1534 texture_desc
.depth
= 1;
1535 texture_desc
.size
= 0;
1537 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
1538 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
1540 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1541 parent
, &texture_desc
, texture_flags
, &swapchain
->front_buffer
)))
1543 WARN("Failed to create front buffer, hr %#x.\n", hr
);
1547 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
1548 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1550 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1551 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1554 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1555 * so we should really check to see if there is a fullscreen swapchain
1556 * already. Does a single head count as full screen? */
1557 if (!desc
->windowed
&& desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1559 /* Change the display settings */
1560 if (FAILED(hr
= wined3d_output_set_display_mode(desc
->output
,
1561 &swapchain
->state
.d3d_mode
)))
1563 WARN("Failed to set display mode, hr %#x.\n", hr
);
1566 displaymode_set
= TRUE
;
1569 if (swapchain
->state
.desc
.backbuffer_count
> 0)
1571 if (!(swapchain
->back_buffers
= heap_calloc(swapchain
->state
.desc
.backbuffer_count
,
1572 sizeof(*swapchain
->back_buffers
))))
1574 ERR("Failed to allocate backbuffer array memory.\n");
1579 texture_desc
.bind_flags
= swapchain
->state
.desc
.backbuffer_bind_flags
;
1580 texture_desc
.usage
= 0;
1581 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1582 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1583 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1585 TRACE("Creating back buffer %u.\n", i
);
1586 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1587 parent
, &texture_desc
, texture_flags
, &swapchain
->back_buffers
[i
])))
1589 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1590 swapchain
->state
.desc
.backbuffer_count
= i
;
1593 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1597 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1598 if (desc
->enable_auto_depth_stencil
)
1600 TRACE("Creating depth/stencil buffer.\n");
1601 if (!device
->auto_depth_stencil_view
)
1603 struct wined3d_view_desc desc
;
1604 struct wined3d_texture
*ds
;
1606 texture_desc
.format
= swapchain
->state
.desc
.auto_depth_stencil_format
;
1607 texture_desc
.usage
= 0;
1608 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
1609 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1610 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1612 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1614 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1615 device
->device_parent
, &texture_desc
, 0, &ds
)))
1617 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1621 desc
.format_id
= ds
->resource
.format
->id
;
1623 desc
.u
.texture
.level_idx
= 0;
1624 desc
.u
.texture
.level_count
= 1;
1625 desc
.u
.texture
.layer_idx
= 0;
1626 desc
.u
.texture
.layer_count
= 1;
1627 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1628 &device
->auto_depth_stencil_view
);
1629 wined3d_texture_decref(ds
);
1632 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1638 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1640 wined3d_mutex_unlock();
1645 if (displaymode_set
)
1647 if (FAILED(wined3d_restore_display_modes(device
->wined3d
)))
1648 ERR("Failed to restore display mode.\n");
1651 if (swapchain
->back_buffers
)
1653 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1655 if (swapchain
->back_buffers
[i
])
1657 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1658 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1661 heap_free(swapchain
->back_buffers
);
1664 if (swapchain
->front_buffer
)
1666 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1667 wined3d_texture_decref(swapchain
->front_buffer
);
1670 wined3d_swapchain_state_cleanup(&swapchain
->state
);
1671 wined3d_mutex_unlock();
1676 HRESULT
wined3d_swapchain_no3d_init(struct wined3d_swapchain
*swapchain_no3d
, struct wined3d_device
*device
,
1677 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1678 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1680 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1681 swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
);
1683 return wined3d_swapchain_init(swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
,
1684 &swapchain_no3d_ops
);
1687 HRESULT
wined3d_swapchain_gl_init(struct wined3d_swapchain_gl
*swapchain_gl
, struct wined3d_device
*device
,
1688 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1689 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1693 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1694 swapchain_gl
, device
, desc
, state_parent
, parent
, parent_ops
);
1696 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_gl
->s
, device
, desc
, state_parent
, parent
,
1697 parent_ops
, &swapchain_gl_ops
)))
1699 /* Cleanup any context that may have been created for the swapchain. */
1700 wined3d_cs_destroy_object(device
->cs
, wined3d_swapchain_gl_destroy_object
, swapchain_gl
);
1701 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1707 HRESULT
wined3d_swapchain_vk_init(struct wined3d_swapchain_vk
*swapchain_vk
, struct wined3d_device
*device
,
1708 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1709 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1713 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1714 swapchain_vk
, device
, desc
, parent
, parent_ops
);
1716 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_vk
->s
, device
, desc
, state_parent
, parent
,
1717 parent_ops
, &swapchain_vk_ops
)))
1720 if (swapchain_vk
->s
.win_handle
== GetDesktopWindow())
1722 WARN("Creating a desktop window swapchain.\n");
1726 if (FAILED(hr
= wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
)))
1727 WARN("Failed to create a Vulkan swapchain, hr %#x.\n", hr
);
1732 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1733 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1734 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1735 struct wined3d_swapchain
**swapchain
)
1737 struct wined3d_swapchain
*object
;
1740 if (FAILED(hr
= device
->adapter
->adapter_ops
->adapter_create_swapchain(device
,
1741 desc
, state_parent
, parent
, parent_ops
, &object
)))
1744 if (desc
->flags
& WINED3D_SWAPCHAIN_IMPLICIT
)
1746 wined3d_mutex_lock();
1747 if (FAILED(hr
= wined3d_device_set_implicit_swapchain(device
, object
)))
1749 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1750 device
->adapter
->adapter_ops
->adapter_destroy_swapchain(object
);
1751 wined3d_mutex_unlock();
1754 wined3d_mutex_unlock();
1757 *swapchain
= object
;
1762 static struct wined3d_context_gl
*wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1764 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1765 struct wined3d_context_gl
*context_gl
;
1767 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain_gl
, GetCurrentThreadId());
1769 wined3d_from_cs(device
->cs
);
1771 if (!(context_gl
= heap_alloc_zero(sizeof(*context_gl
))))
1773 ERR("Failed to allocate context memory.\n");
1777 if (FAILED(wined3d_context_gl_init(context_gl
, swapchain_gl
)))
1779 WARN("Failed to initialise context.\n");
1780 heap_free(context_gl
);
1784 if (!device_context_add(device
, &context_gl
->c
))
1786 ERR("Failed to add the newly created context to the context list.\n");
1787 wined3d_context_gl_destroy(context_gl
);
1791 TRACE("Created context %p.\n", context_gl
);
1793 context_release(&context_gl
->c
);
1795 if (!wined3d_array_reserve((void **)&swapchain_gl
->contexts
, &swapchain_gl
->contexts_size
,
1796 swapchain_gl
->context_count
+ 1, sizeof(*swapchain_gl
->contexts
)))
1798 ERR("Failed to allocate new context array memory.\n");
1799 wined3d_context_gl_destroy(context_gl
);
1802 swapchain_gl
->contexts
[swapchain_gl
->context_count
++] = context_gl
;
1807 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl
*swapchain_gl
)
1811 TRACE("swapchain_gl %p.\n", swapchain_gl
);
1813 for (i
= 0; i
< swapchain_gl
->context_count
; ++i
)
1815 wined3d_context_gl_destroy(swapchain_gl
->contexts
[i
]);
1817 heap_free(swapchain_gl
->contexts
);
1818 swapchain_gl
->contexts_size
= 0;
1819 swapchain_gl
->context_count
= 0;
1820 swapchain_gl
->contexts
= NULL
;
1823 struct wined3d_context_gl
*wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1825 DWORD tid
= GetCurrentThreadId();
1828 for (i
= 0; i
< swapchain_gl
->context_count
; ++i
)
1830 if (swapchain_gl
->contexts
[i
]->tid
== tid
)
1831 return swapchain_gl
->contexts
[i
];
1834 /* Create a new context for the thread. */
1835 return wined3d_swapchain_gl_create_context(swapchain_gl
);
1838 HDC
wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl
*swapchain_gl
)
1840 if (!swapchain_gl
->backup_dc
)
1842 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl
);
1844 if (!(swapchain_gl
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1845 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1847 ERR("Failed to create a window.\n");
1851 if (!(swapchain_gl
->backup_dc
= GetDC(swapchain_gl
->backup_wnd
)))
1853 ERR("Failed to get a DC.\n");
1854 DestroyWindow(swapchain_gl
->backup_wnd
);
1855 swapchain_gl
->backup_wnd
= NULL
;
1860 return swapchain_gl
->backup_dc
;
1863 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1867 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1869 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1871 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1875 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1877 struct wined3d_device
*device
= swapchain
->device
;
1878 HWND window
= swapchain
->state
.device_window
;
1879 struct wined3d_output_desc output_desc
;
1880 unsigned int screensaver_active
;
1881 struct wined3d_output
*output
;
1882 BOOL focus_messages
, filter
;
1885 /* This code is not protected by the wined3d mutex, so it may run while
1886 * wined3d_device_reset is active. Testing on Windows shows that changing
1887 * focus during resets and resetting during focus change events causes
1888 * the application to crash with an invalid memory access. */
1890 if (!(focus_messages
= device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
))
1891 filter
= wined3d_filter_messages(window
, TRUE
);
1895 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
1896 if ((device
->restore_screensaver
= !!screensaver_active
))
1897 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
1899 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1901 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1902 * the window but leaves the size untouched, d3d9 sets the size on an
1903 * invisible window, generates messages but doesn't change the window
1904 * properties. The implementation follows d3d9.
1906 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1907 * resume drawing after a focus loss. */
1908 output
= wined3d_swapchain_get_output(swapchain
);
1911 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1915 if (SUCCEEDED(hr
= wined3d_output_get_desc(output
, &output_desc
)))
1916 SetWindowPos(window
, NULL
, output_desc
.desktop_rect
.left
,
1917 output_desc
.desktop_rect
.top
, swapchain
->state
.desc
.backbuffer_width
,
1918 swapchain
->state
.desc
.backbuffer_height
, SWP_NOACTIVATE
| SWP_NOZORDER
);
1920 ERR("Failed to get output description, hr %#x.\n", hr
);
1923 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1925 output
= wined3d_swapchain_get_output(swapchain
);
1928 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1932 if (FAILED(hr
= wined3d_output_set_display_mode(output
,
1933 &swapchain
->state
.d3d_mode
)))
1934 ERR("Failed to set display mode, hr %#x.\n", hr
);
1937 if (swapchain
== device
->swapchains
[0])
1938 device
->device_parent
->ops
->activate(device
->device_parent
, TRUE
);
1942 if (device
->restore_screensaver
)
1944 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
1945 device
->restore_screensaver
= FALSE
;
1948 if (FAILED(hr
= wined3d_restore_display_modes(device
->wined3d
)))
1949 ERR("Failed to restore display modes, hr %#x.\n", hr
);
1951 swapchain
->reapply_mode
= TRUE
;
1953 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1954 * during window minimization. Do our housekeeping now, as the device may not exist after
1955 * the ShowWindow call.
1957 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1958 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1959 * we'll have to control this via a create flag. Note that the device and swapchain are not
1960 * safe to access after the ShowWindow call. */
1961 if (swapchain
== device
->swapchains
[0])
1962 device
->device_parent
->ops
->activate(device
->device_parent
, FALSE
);
1964 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
) && IsWindowVisible(window
))
1965 ShowWindow(window
, SW_MINIMIZE
);
1968 if (!focus_messages
)
1969 wined3d_filter_messages(window
, filter
);
1972 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1973 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1974 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1976 struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
1977 BOOL update_desc
= FALSE
;
1979 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1980 "multisample_type %#x, multisample_quality %#x.\n",
1981 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1982 multisample_type
, multisample_quality
);
1984 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1986 if (buffer_count
&& buffer_count
!= desc
->backbuffer_count
)
1987 FIXME("Cannot change the back buffer count yet.\n");
1989 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1991 if (!width
|| !height
)
1995 /* The application is requesting that either the swapchain width or
1996 * height be set to the corresponding dimension in the window's
1999 if (!GetClientRect(swapchain
->state
.device_window
, &client_rect
))
2001 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
2002 return WINED3DERR_INVALIDCALL
;
2006 width
= client_rect
.right
;
2009 height
= client_rect
.bottom
;
2012 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
2014 desc
->backbuffer_width
= width
;
2015 desc
->backbuffer_height
= height
;
2019 if (format_id
== WINED3DFMT_UNKNOWN
)
2021 if (!desc
->windowed
)
2022 return WINED3DERR_INVALIDCALL
;
2023 format_id
= swapchain
->state
.original_mode
.format_id
;
2026 if (format_id
!= desc
->backbuffer_format
)
2028 desc
->backbuffer_format
= format_id
;
2032 if (multisample_type
!= desc
->multisample_type
2033 || multisample_quality
!= desc
->multisample_quality
)
2035 desc
->multisample_type
= multisample_type
;
2036 desc
->multisample_quality
= multisample_quality
;
2045 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, 0, desc
->backbuffer_width
,
2046 desc
->backbuffer_height
, desc
->backbuffer_format
,
2047 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2050 for (i
= 0; i
< desc
->backbuffer_count
; ++i
)
2052 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], 0, desc
->backbuffer_width
,
2053 desc
->backbuffer_height
, desc
->backbuffer_format
,
2054 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2059 swapchain_update_draw_bindings(swapchain
);
2064 static HRESULT
wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state
*state
,
2065 struct wined3d_output
*output
, struct wined3d_display_mode
*mode
)
2069 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
2071 if (FAILED(hr
= wined3d_output_find_closest_matching_mode(output
, mode
)))
2073 WARN("Failed to find closest matching mode, hr %#x.\n", hr
);
2077 if (output
!= state
->desc
.output
)
2079 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2081 WARN("Failed to restore display modes, hr %#x.\n", hr
);
2085 if (FAILED(hr
= wined3d_output_get_display_mode(output
, &state
->original_mode
, NULL
)))
2087 WARN("Failed to get current display mode, hr %#x.\n", hr
);
2092 if (FAILED(hr
= wined3d_output_set_display_mode(output
, mode
)))
2094 WARN("Failed to set display mode, hr %#x.\n", hr
);
2095 return WINED3DERR_INVALIDCALL
;
2101 HRESULT CDECL
wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state
*state
,
2102 const struct wined3d_display_mode
*mode
)
2104 struct wined3d_display_mode actual_mode
;
2105 struct wined3d_output_desc output_desc
;
2106 RECT original_window_rect
, window_rect
;
2107 int x
, y
, width
, height
;
2111 TRACE("state %p, mode %p.\n", state
, mode
);
2113 wined3d_mutex_lock();
2115 window
= state
->device_window
;
2117 if (state
->desc
.windowed
)
2119 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
2120 AdjustWindowRectEx(&window_rect
,
2121 GetWindowLongW(window
, GWL_STYLE
), FALSE
,
2122 GetWindowLongW(window
, GWL_EXSTYLE
));
2123 GetWindowRect(window
, &original_window_rect
);
2125 x
= original_window_rect
.left
;
2126 y
= original_window_rect
.top
;
2127 width
= window_rect
.right
- window_rect
.left
;
2128 height
= window_rect
.bottom
- window_rect
.top
;
2132 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2134 actual_mode
= *mode
;
2135 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, state
->desc
.output
,
2138 ERR("Failed to set display mode, hr %#x.\n", hr
);
2139 wined3d_mutex_unlock();
2144 if (FAILED(hr
= wined3d_output_get_desc(state
->desc
.output
, &output_desc
)))
2146 ERR("Failed to get output description, hr %#x.\n", hr
);
2147 wined3d_mutex_unlock();
2151 x
= output_desc
.desktop_rect
.left
;
2152 y
= output_desc
.desktop_rect
.top
;
2153 width
= output_desc
.desktop_rect
.right
- output_desc
.desktop_rect
.left
;
2154 height
= output_desc
.desktop_rect
.bottom
- output_desc
.desktop_rect
.top
;
2157 wined3d_mutex_unlock();
2159 MoveWindow(window
, x
, y
, width
, height
, TRUE
);
2164 static LONG
fullscreen_style(LONG style
)
2166 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2167 style
|= WS_POPUP
| WS_SYSMENU
;
2168 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
2173 static LONG
fullscreen_exstyle(LONG exstyle
)
2175 /* Filter out window decorations. */
2176 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
2181 struct wined3d_window_state
2184 HWND window_pos_after
;
2185 LONG style
, exstyle
;
2186 int x
, y
, width
, height
;
2191 static DWORD WINAPI
wined3d_set_window_state(void *ctx
)
2193 struct wined3d_window_state
*s
= ctx
;
2196 filter
= wined3d_filter_messages(s
->window
, TRUE
);
2200 SetWindowLongW(s
->window
, GWL_STYLE
, s
->style
);
2201 SetWindowLongW(s
->window
, GWL_EXSTYLE
, s
->exstyle
);
2203 SetWindowPos(s
->window
, s
->window_pos_after
, s
->x
, s
->y
, s
->width
, s
->height
, s
->flags
);
2205 wined3d_filter_messages(s
->window
, filter
);
2212 HRESULT
wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state
*state
,
2213 HWND window
, int x
, int y
, int width
, int height
)
2215 struct wined3d_window_state
*s
;
2216 DWORD window_tid
, tid
;
2219 TRACE("Setting up window %p for fullscreen mode.\n", window
);
2221 if (!IsWindow(window
))
2223 WARN("%p is not a valid window.\n", window
);
2224 return WINED3DERR_NOTAVAILABLE
;
2227 if (!(s
= heap_alloc(sizeof(*s
))))
2228 return E_OUTOFMEMORY
;
2230 s
->window_pos_after
= HWND_TOPMOST
;
2236 if (state
->style
|| state
->exstyle
)
2238 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
2239 window
, state
->style
, state
->exstyle
);
2242 s
->flags
= SWP_FRAMECHANGED
| SWP_NOACTIVATE
;
2243 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
)
2244 s
->flags
|= SWP_NOZORDER
;
2246 s
->flags
|= SWP_SHOWWINDOW
;
2248 state
->style
= GetWindowLongW(window
, GWL_STYLE
);
2249 state
->exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2251 s
->style
= fullscreen_style(state
->style
);
2252 s
->exstyle
= fullscreen_exstyle(state
->exstyle
);
2253 s
->set_style
= true;
2255 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
2256 state
->style
, state
->exstyle
, s
->style
, s
->exstyle
);
2258 window_tid
= GetWindowThreadProcessId(window
, NULL
);
2259 tid
= GetCurrentThreadId();
2261 TRACE("Window %p belongs to thread %#x.\n", window
, window_tid
);
2262 /* If the window belongs to a different thread, modifying the style and/or
2263 * position can potentially deadlock if that thread isn't processing
2265 if (window_tid
== tid
)
2266 wined3d_set_window_state(s
);
2267 else if (!(thread
= CreateThread(NULL
, 0, wined3d_set_window_state
, s
, 0, NULL
)))
2268 ERR("Failed to create thread.\n");
2270 CloseHandle(thread
);
2275 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state
*state
,
2276 HWND window
, const RECT
*window_rect
)
2278 struct wined3d_window_state
*s
;
2279 DWORD window_tid
, tid
;
2280 LONG style
, exstyle
;
2283 if (!state
->style
&& !state
->exstyle
)
2286 if (!(s
= heap_alloc(sizeof(*s
))))
2290 s
->window_pos_after
= NULL
;
2291 s
->flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
2293 if ((state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE
)
2294 && !(state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
))
2296 s
->window_pos_after
= (state
->exstyle
& WS_EX_TOPMOST
) ? HWND_TOPMOST
: HWND_NOTOPMOST
;
2297 s
->flags
|= (state
->style
& WS_VISIBLE
) ? SWP_SHOWWINDOW
: SWP_HIDEWINDOW
;
2298 s
->flags
&= ~SWP_NOZORDER
;
2301 style
= GetWindowLongW(window
, GWL_STYLE
);
2302 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2304 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2305 * application, and we want to ignore them in the test below, since it's
2306 * not the application's fault that they changed. Additionally, we want to
2307 * preserve the current status of these flags (i.e. don't restore them) to
2308 * more closely emulate the behavior of Direct3D, which leaves these flags
2309 * alone when returning to windowed mode. */
2310 state
->style
^= (state
->style
^ style
) & WS_VISIBLE
;
2311 state
->exstyle
^= (state
->exstyle
^ exstyle
) & WS_EX_TOPMOST
;
2313 TRACE("Restoring window style of window %p to %08x, %08x.\n",
2314 window
, state
->style
, state
->exstyle
);
2316 s
->style
= state
->style
;
2317 s
->exstyle
= state
->exstyle
;
2318 /* Only restore the style if the application didn't modify it during the
2319 * fullscreen phase. Some applications change it before calling Reset()
2320 * when switching between windowed and fullscreen modes (HL2), some
2321 * depend on the original style (Eve Online). */
2322 s
->set_style
= style
== fullscreen_style(state
->style
) && exstyle
== fullscreen_exstyle(state
->exstyle
);
2326 s
->x
= window_rect
->left
;
2327 s
->y
= window_rect
->top
;
2328 s
->width
= window_rect
->right
- window_rect
->left
;
2329 s
->height
= window_rect
->bottom
- window_rect
->top
;
2333 s
->x
= s
->y
= s
->width
= s
->height
= 0;
2334 s
->flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
2337 window_tid
= GetWindowThreadProcessId(window
, NULL
);
2338 tid
= GetCurrentThreadId();
2340 TRACE("Window %p belongs to thread %#x.\n", window
, window_tid
);
2341 /* If the window belongs to a different thread, modifying the style and/or
2342 * position can potentially deadlock if that thread isn't processing
2344 if (window_tid
== tid
)
2345 wined3d_set_window_state(s
);
2346 else if (!(thread
= CreateThread(NULL
, 0, wined3d_set_window_state
, s
, 0, NULL
)))
2347 ERR("Failed to create thread.\n");
2349 CloseHandle(thread
);
2351 /* Delete the old values. */
2356 HRESULT CDECL
wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state
*state
,
2357 const struct wined3d_swapchain_desc
*swapchain_desc
,
2358 const struct wined3d_display_mode
*mode
)
2360 struct wined3d_display_mode actual_mode
;
2361 struct wined3d_output_desc output_desc
;
2362 BOOL windowed
= state
->desc
.windowed
;
2365 TRACE("state %p, swapchain_desc %p, mode %p.\n", state
, swapchain_desc
, mode
);
2367 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2371 actual_mode
= *mode
;
2372 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2378 if (!swapchain_desc
->windowed
)
2380 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
2381 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
2382 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
2383 actual_mode
.format_id
= adapter_format_from_backbuffer_format(swapchain_desc
->output
->adapter
,
2384 swapchain_desc
->backbuffer_format
);
2385 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
2386 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2392 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2394 WARN("Failed to restore display modes for all outputs, hr %#x.\n", hr
);
2403 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2405 if (FAILED(hr
= wined3d_output_get_display_mode(swapchain_desc
->output
, &actual_mode
,
2408 ERR("Failed to get display mode, hr %#x.\n", hr
);
2409 return WINED3DERR_INVALIDCALL
;
2413 if (!swapchain_desc
->windowed
)
2415 unsigned int width
= actual_mode
.width
;
2416 unsigned int height
= actual_mode
.height
;
2418 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
2420 ERR("Failed to get output description, hr %#x.\n", hr
);
2424 if (state
->desc
.windowed
)
2426 /* Switch from windowed to fullscreen */
2427 if (FAILED(hr
= wined3d_swapchain_state_setup_fullscreen(state
, state
->device_window
,
2428 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
, height
)))
2433 HWND window
= state
->device_window
;
2436 /* Fullscreen -> fullscreen mode change */
2437 filter
= wined3d_filter_messages(window
, TRUE
);
2438 MoveWindow(window
, output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
,
2440 ShowWindow(window
, SW_SHOW
);
2441 wined3d_filter_messages(window
, filter
);
2443 state
->d3d_mode
= actual_mode
;
2445 else if (!state
->desc
.windowed
)
2447 /* Fullscreen -> windowed switch */
2448 RECT
*window_rect
= NULL
;
2449 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
2450 window_rect
= &state
->original_window_rect
;
2451 wined3d_swapchain_state_restore_from_fullscreen(state
, state
->device_window
, window_rect
);
2454 state
->desc
.output
= swapchain_desc
->output
;
2455 state
->desc
.windowed
= swapchain_desc
->windowed
;
2457 if (windowed
!= state
->desc
.windowed
)
2458 state
->parent
->ops
->windowed_state_changed(state
->parent
, state
->desc
.windowed
);
2463 BOOL CDECL
wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state
*state
)
2465 TRACE("state %p.\n", state
);
2467 return state
->desc
.windowed
;
2470 void CDECL
wined3d_swapchain_state_destroy(struct wined3d_swapchain_state
*state
)
2472 wined3d_swapchain_state_cleanup(state
);
2476 HRESULT CDECL
wined3d_swapchain_state_create(const struct wined3d_swapchain_desc
*desc
,
2477 HWND window
, struct wined3d
*wined3d
, struct wined3d_swapchain_state_parent
*state_parent
,
2478 struct wined3d_swapchain_state
**state
)
2480 struct wined3d_swapchain_state
*s
;
2483 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc
, window
, wined3d
, state
);
2485 if (!(s
= heap_alloc_zero(sizeof(*s
))))
2486 return E_OUTOFMEMORY
;
2488 if (FAILED(hr
= wined3d_swapchain_state_init(s
, desc
, window
, wined3d
, state_parent
)))