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 if (context
->d3d_info
->fences
)
637 wined3d_context_gl_submit_command_fence(context_gl
);
639 wined3d_swapchain_gl_rotate(swapchain
, context
);
641 TRACE("SwapBuffers called, Starting new frame\n");
643 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
644 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
646 context_release(context
);
649 static void swapchain_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
651 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
652 struct wined3d_context
*context
;
654 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
655 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
656 context_release(context
);
657 SetRectEmpty(&swapchain
->front_buffer_update
);
660 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
662 swapchain_gl_present
,
663 swapchain_frontbuffer_updated
,
666 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk
*swapchain_vk
,
667 VkPresentModeKHR vk_present_mode
)
669 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
670 const struct wined3d_vk_info
*vk_info
;
671 struct wined3d_adapter_vk
*adapter_vk
;
672 VkPhysicalDevice vk_physical_device
;
673 VkPresentModeKHR
*vk_modes
;
674 bool supported
= false;
678 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
679 vk_physical_device
= adapter_vk
->physical_device
;
680 vk_info
= &adapter_vk
->vk_info
;
682 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
683 swapchain_vk
->vk_surface
, &count
, NULL
))) < 0)
685 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr
));
689 if (!(vk_modes
= heap_calloc(count
, sizeof(*vk_modes
))))
692 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
693 swapchain_vk
->vk_surface
, &count
, vk_modes
))) < 0)
695 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr
));
699 for (i
= 0; i
< count
; ++i
)
701 if (vk_modes
[i
] == vk_present_mode
)
713 static VkFormat
get_swapchain_fallback_format(VkFormat vk_format
)
717 case VK_FORMAT_R8G8B8A8_SRGB
:
718 return VK_FORMAT_B8G8R8A8_SRGB
;
719 case VK_FORMAT_R8G8B8A8_UNORM
:
720 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
721 case VK_FORMAT_R16G16B16A16_SFLOAT
:
722 return VK_FORMAT_B8G8R8A8_UNORM
;
724 WARN("Unhandled format %#x.\n", vk_format
);
725 return VK_FORMAT_UNDEFINED
;
729 static VkFormat
wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk
*swapchain_vk
,
730 VkSurfaceKHR vk_surface
)
732 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
733 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
734 const struct wined3d_vk_info
*vk_info
;
735 struct wined3d_adapter_vk
*adapter_vk
;
736 const struct wined3d_format
*format
;
737 VkPhysicalDevice vk_physical_device
;
738 VkSurfaceFormatKHR
*vk_formats
;
739 uint32_t format_count
, i
;
743 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
744 vk_physical_device
= adapter_vk
->physical_device
;
745 vk_info
= &adapter_vk
->vk_info
;
747 if ((format
= wined3d_get_format(&adapter_vk
->a
, desc
->backbuffer_format
, WINED3D_BIND_RENDER_TARGET
)))
748 vk_format
= wined3d_format_vk(format
)->vk_format
;
750 vk_format
= VK_FORMAT_B8G8R8A8_UNORM
;
752 vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
, vk_surface
, &format_count
, NULL
));
753 if (vr
< 0 || !format_count
)
755 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr
));
756 return VK_FORMAT_UNDEFINED
;
759 if (!(vk_formats
= heap_calloc(format_count
, sizeof(*vk_formats
))))
760 return VK_FORMAT_UNDEFINED
;
762 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
,
763 vk_surface
, &format_count
, vk_formats
))) < 0)
765 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr
));
766 heap_free(vk_formats
);
767 return VK_FORMAT_UNDEFINED
;
770 for (i
= 0; i
< format_count
; ++i
)
772 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
775 if (i
== format_count
)
777 /* Try to create a swapchain with format conversion. */
778 vk_format
= get_swapchain_fallback_format(vk_format
);
779 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
780 for (i
= 0; i
< format_count
; ++i
)
782 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
786 heap_free(vk_formats
);
787 if (i
== format_count
)
789 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
790 return VK_FORMAT_UNDEFINED
;
793 TRACE("Using Vulkan swapchain format %#x.\n", vk_format
);
798 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk
*swapchain_vk
,
799 VkSwapchainKHR vk_swapchain
)
801 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
802 const struct wined3d_vk_info
*vk_info
;
803 VkSemaphoreCreateInfo semaphore_info
;
804 uint32_t image_count
, i
;
807 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
809 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
, vk_swapchain
, &image_count
, NULL
))) < 0)
811 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr
));
815 if (!(swapchain_vk
->vk_images
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_images
))))
817 ERR("Failed to allocate images array.\n");
821 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
,
822 vk_swapchain
, &image_count
, swapchain_vk
->vk_images
))) < 0)
824 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr
));
825 heap_free(swapchain_vk
->vk_images
);
829 if (!(swapchain_vk
->vk_semaphores
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_semaphores
))))
831 ERR("Failed to allocate semaphores array.\n");
832 heap_free(swapchain_vk
->vk_images
);
836 semaphore_info
.sType
= VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
;
837 semaphore_info
.pNext
= NULL
;
838 semaphore_info
.flags
= 0;
839 for (i
= 0; i
< image_count
; ++i
)
841 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
842 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].available
))) < 0)
844 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
848 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
849 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].presentable
))) < 0)
851 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
855 swapchain_vk
->image_count
= image_count
;
860 for (i
= 0; i
< image_count
; ++i
)
862 if (swapchain_vk
->vk_semaphores
[i
].available
)
863 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
864 if (swapchain_vk
->vk_semaphores
[i
].presentable
)
865 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
867 heap_free(swapchain_vk
->vk_semaphores
);
868 heap_free(swapchain_vk
->vk_images
);
872 static HRESULT
wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
874 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
875 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
876 VkSwapchainCreateInfoKHR vk_swapchain_desc
;
877 VkWin32SurfaceCreateInfoKHR surface_desc
;
878 unsigned int width
, height
, image_count
;
879 const struct wined3d_vk_info
*vk_info
;
880 VkSurfaceCapabilitiesKHR surface_caps
;
881 struct wined3d_adapter_vk
*adapter_vk
;
882 VkPresentModeKHR vk_present_mode
;
883 VkSwapchainKHR vk_swapchain
;
884 VkImageUsageFlags usage
;
885 VkSurfaceKHR vk_surface
;
891 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
892 vk_info
= &adapter_vk
->vk_info
;
894 surface_desc
.sType
= VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR
;
895 surface_desc
.pNext
= NULL
;
896 surface_desc
.flags
= 0;
897 surface_desc
.hinstance
= (HINSTANCE
)GetWindowLongPtrW(swapchain_vk
->s
.win_handle
, GWLP_HINSTANCE
);
898 surface_desc
.hwnd
= swapchain_vk
->s
.win_handle
;
899 if ((vr
= VK_CALL(vkCreateWin32SurfaceKHR(vk_info
->instance
, &surface_desc
, NULL
, &vk_surface
))) < 0)
901 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr
));
904 swapchain_vk
->vk_surface
= vk_surface
;
906 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk
->physical_device
,
907 device_vk
->vk_queue_family_index
, vk_surface
, &supported
))) < 0 || !supported
)
909 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr
));
913 if ((vk_format
= wined3d_swapchain_vk_select_vk_format(swapchain_vk
, vk_surface
)) == VK_FORMAT_UNDEFINED
)
915 ERR("Failed to select swapchain format.\n");
919 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk
->physical_device
,
920 swapchain_vk
->vk_surface
, &surface_caps
))) < 0)
922 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr
));
926 image_count
= desc
->backbuffer_count
;
927 if (image_count
< surface_caps
.minImageCount
)
928 image_count
= surface_caps
.minImageCount
;
929 else if (surface_caps
.maxImageCount
&& image_count
> surface_caps
.maxImageCount
)
930 image_count
= surface_caps
.maxImageCount
;
932 if (image_count
!= desc
->backbuffer_count
)
933 WARN("Image count %u is not supported (%u-%u).\n", desc
->backbuffer_count
,
934 surface_caps
.minImageCount
, surface_caps
.maxImageCount
);
936 GetClientRect(swapchain_vk
->s
.win_handle
, &client_rect
);
938 width
= client_rect
.right
- client_rect
.left
;
939 if (width
< surface_caps
.minImageExtent
.width
)
940 width
= surface_caps
.minImageExtent
.width
;
941 else if (width
> surface_caps
.maxImageExtent
.width
)
942 width
= surface_caps
.maxImageExtent
.width
;
944 height
= client_rect
.bottom
- client_rect
.top
;
945 if (height
< surface_caps
.minImageExtent
.height
)
946 height
= surface_caps
.minImageExtent
.height
;
947 else if (height
> surface_caps
.maxImageExtent
.height
)
948 height
= surface_caps
.maxImageExtent
.height
;
950 if (width
!= client_rect
.right
- client_rect
.left
|| height
!= client_rect
.bottom
- client_rect
.top
)
951 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
952 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
953 surface_caps
.minImageExtent
.width
, surface_caps
.maxImageExtent
.width
,
954 surface_caps
.minImageExtent
.height
, surface_caps
.maxImageExtent
.height
);
956 usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
957 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
958 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
959 if (!(usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) || !(usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
))
960 WARN("Transfer not supported for swapchain images.\n");
962 if (!(surface_caps
.supportedCompositeAlpha
& VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
))
964 FIXME("Unsupported alpha mode, %#x.\n", surface_caps
.supportedCompositeAlpha
);
968 vk_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
969 if (!swapchain_vk
->s
.swap_interval
)
971 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk
, VK_PRESENT_MODE_IMMEDIATE_KHR
))
972 vk_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
974 FIXME("Unsupported swap interval %u.\n", swapchain_vk
->s
.swap_interval
);
977 vk_swapchain_desc
.sType
= VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
;
978 vk_swapchain_desc
.pNext
= NULL
;
979 vk_swapchain_desc
.flags
= 0;
980 vk_swapchain_desc
.surface
= vk_surface
;
981 vk_swapchain_desc
.minImageCount
= image_count
;
982 vk_swapchain_desc
.imageFormat
= vk_format
;
983 vk_swapchain_desc
.imageColorSpace
= VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
;
984 vk_swapchain_desc
.imageExtent
.width
= width
;
985 vk_swapchain_desc
.imageExtent
.height
= height
;
986 vk_swapchain_desc
.imageArrayLayers
= 1;
987 vk_swapchain_desc
.imageUsage
= usage
;
988 vk_swapchain_desc
.imageSharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
989 vk_swapchain_desc
.queueFamilyIndexCount
= 0;
990 vk_swapchain_desc
.pQueueFamilyIndices
= NULL
;
991 vk_swapchain_desc
.preTransform
= surface_caps
.currentTransform
;
992 vk_swapchain_desc
.compositeAlpha
= VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
993 vk_swapchain_desc
.presentMode
= vk_present_mode
;
994 vk_swapchain_desc
.clipped
= VK_TRUE
;
995 vk_swapchain_desc
.oldSwapchain
= VK_NULL_HANDLE
;
996 if ((vr
= VK_CALL(vkCreateSwapchainKHR(device_vk
->vk_device
, &vk_swapchain_desc
, NULL
, &vk_swapchain
))) < 0)
998 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr
));
1001 swapchain_vk
->vk_swapchain
= vk_swapchain
;
1003 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk
, vk_swapchain
))
1005 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, vk_swapchain
, NULL
));
1009 swapchain_vk
->width
= width
;
1010 swapchain_vk
->height
= height
;
1015 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, vk_surface
, NULL
));
1019 static HRESULT
wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk
*swapchain_vk
)
1021 TRACE("swapchain_vk %p.\n", swapchain_vk
);
1023 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk
);
1025 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
);
1028 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk
*swapchain_vk
,
1029 unsigned int swap_interval
)
1031 if (swap_interval
> 1)
1033 if (swap_interval
<= 4)
1034 FIXME("Unsupported swap interval %u.\n", swap_interval
);
1038 if (swapchain_vk
->s
.swap_interval
== swap_interval
)
1041 swapchain_vk
->s
.swap_interval
= swap_interval
;
1042 wined3d_swapchain_vk_recreate(swapchain_vk
);
1045 static VkResult
wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk
*swapchain_vk
,
1046 struct wined3d_context_vk
*context_vk
, const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
)
1048 struct wined3d_texture_vk
*back_buffer_vk
= wined3d_texture_vk(swapchain_vk
->s
.back_buffers
[0]);
1049 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
1050 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
1051 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1052 VkCommandBuffer vk_command_buffer
;
1053 VkImageSubresourceRange vk_range
;
1054 VkPresentInfoKHR present_desc
;
1055 unsigned int present_idx
;
1056 VkImageLayout vk_layout
;
1063 static const VkPipelineStageFlags wait_stage
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
;
1065 TRACE("swapchain_vk %p, context_vk %p, src_rect %s, dst_rect %s, swap_interval %u.\n",
1066 swapchain_vk
, context_vk
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
), swap_interval
);
1068 wined3d_swapchain_vk_set_swap_interval(swapchain_vk
, swap_interval
);
1070 present_idx
= swapchain_vk
->current
++ % swapchain_vk
->image_count
;
1071 wined3d_context_vk_wait_command_buffer(context_vk
, swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
);
1072 if ((vr
= VK_CALL(vkAcquireNextImageKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, UINT64_MAX
,
1073 swapchain_vk
->vk_semaphores
[present_idx
].available
, VK_NULL_HANDLE
, &image_idx
))) < 0)
1075 WARN("Failed to acquire image, vr %s.\n", wined3d_debug_vkresult(vr
));
1079 if (dst_rect
->right
> swapchain_vk
->width
|| dst_rect
->bottom
> swapchain_vk
->height
)
1081 dst_rect_tmp
= *dst_rect
;
1082 if (dst_rect
->right
> swapchain_vk
->width
)
1083 dst_rect_tmp
.right
= swapchain_vk
->width
;
1084 if (dst_rect
->bottom
> swapchain_vk
->height
)
1085 dst_rect_tmp
.bottom
= swapchain_vk
->height
;
1086 dst_rect
= &dst_rect_tmp
;
1088 filter
= src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
1089 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
1090 ? VK_FILTER_LINEAR
: VK_FILTER_NEAREST
;
1091 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
1093 wined3d_context_vk_end_current_render_pass(context_vk
);
1095 vk_range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1096 vk_range
.baseMipLevel
= 0;
1097 vk_range
.levelCount
= 1;
1098 vk_range
.baseArrayLayer
= 0;
1099 vk_range
.layerCount
= 1;
1101 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1102 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1103 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1104 VK_ACCESS_TRANSFER_READ_BIT
,
1105 back_buffer_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1106 back_buffer_vk
->image
.vk_image
, &vk_range
);
1108 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1109 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1110 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
1111 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1112 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1114 blit
.srcSubresource
.aspectMask
= vk_range
.aspectMask
;
1115 blit
.srcSubresource
.mipLevel
= vk_range
.baseMipLevel
;
1116 blit
.srcSubresource
.baseArrayLayer
= vk_range
.baseArrayLayer
;
1117 blit
.srcSubresource
.layerCount
= vk_range
.layerCount
;
1118 blit
.srcOffsets
[0].x
= src_rect
->left
;
1119 blit
.srcOffsets
[0].y
= src_rect
->top
;
1120 blit
.srcOffsets
[0].z
= 0;
1121 blit
.srcOffsets
[1].x
= src_rect
->right
;
1122 blit
.srcOffsets
[1].y
= src_rect
->bottom
;
1123 blit
.srcOffsets
[1].z
= 1;
1124 blit
.dstSubresource
= blit
.srcSubresource
;
1125 blit
.dstOffsets
[0].x
= dst_rect
->left
;
1126 blit
.dstOffsets
[0].y
= dst_rect
->top
;
1127 blit
.dstOffsets
[0].z
= 0;
1128 blit
.dstOffsets
[1].x
= dst_rect
->right
;
1129 blit
.dstOffsets
[1].y
= dst_rect
->bottom
;
1130 blit
.dstOffsets
[1].z
= 1;
1131 VK_CALL(vkCmdBlitImage(vk_command_buffer
,
1132 back_buffer_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1133 swapchain_vk
->vk_images
[image_idx
], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1136 wined3d_context_vk_reference_texture(context_vk
, back_buffer_vk
);
1137 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1138 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
1139 VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
1140 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
1141 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1143 if (desc
->swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
|| desc
->swap_effect
== WINED3D_SWAP_EFFECT_FLIP_DISCARD
)
1144 vk_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
1146 vk_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
1147 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1148 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1149 VK_ACCESS_TRANSFER_READ_BIT
,
1150 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1151 vk_layout
, back_buffer_vk
->layout
,
1152 back_buffer_vk
->image
.vk_image
, &vk_range
);
1153 back_buffer_vk
->bind_mask
= 0;
1155 swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
= context_vk
->current_command_buffer
.id
;
1156 wined3d_context_vk_submit_command_buffer(context_vk
,
1157 1, &swapchain_vk
->vk_semaphores
[present_idx
].available
, &wait_stage
,
1158 1, &swapchain_vk
->vk_semaphores
[present_idx
].presentable
);
1160 present_desc
.sType
= VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
;
1161 present_desc
.pNext
= NULL
;
1162 present_desc
.waitSemaphoreCount
= 1;
1163 present_desc
.pWaitSemaphores
= &swapchain_vk
->vk_semaphores
[present_idx
].presentable
;
1164 present_desc
.swapchainCount
= 1;
1165 present_desc
.pSwapchains
= &swapchain_vk
->vk_swapchain
;
1166 present_desc
.pImageIndices
= &image_idx
;
1167 present_desc
.pResults
= NULL
;
1168 if ((vr
= VK_CALL(vkQueuePresentKHR(device_vk
->vk_queue
, &present_desc
))))
1169 WARN("Present returned vr %s.\n", wined3d_debug_vkresult(vr
));
1173 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context_vk
*context_vk
)
1175 struct wined3d_texture_sub_resource
*sub_resource
;
1176 struct wined3d_texture_vk
*texture
, *texture_prev
;
1177 struct wined3d_image_vk image0
;
1178 VkDescriptorImageInfo vk_info0
;
1179 VkImageLayout vk_layout0
;
1183 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
1185 if (swapchain
->state
.desc
.backbuffer_count
< 2)
1188 texture_prev
= wined3d_texture_vk(swapchain
->back_buffers
[0]);
1190 /* Back buffer 0 is already in the draw binding. */
1191 image0
= texture_prev
->image
;
1192 vk_layout0
= texture_prev
->layout
;
1193 vk_info0
= texture_prev
->default_image_info
;
1194 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
1196 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1198 texture
= wined3d_texture_vk(swapchain
->back_buffers
[i
]);
1199 sub_resource
= &texture
->t
.sub_resources
[0];
1201 if (!(sub_resource
->locations
& supported_locations
))
1202 wined3d_texture_load_location(&texture
->t
, 0, &context_vk
->c
, texture
->t
.resource
.draw_binding
);
1204 texture_prev
->image
= texture
->image
;
1205 texture_prev
->layout
= texture
->layout
;
1206 texture_prev
->default_image_info
= texture
->default_image_info
;
1208 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
1209 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
1211 texture_prev
= texture
;
1214 texture_prev
->image
= image0
;
1215 texture_prev
->layout
= vk_layout0
;
1216 texture_prev
->default_image_info
= vk_info0
;
1218 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
1219 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
1221 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
1224 static void swapchain_vk_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect
,
1225 const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1227 struct wined3d_swapchain_vk
*swapchain_vk
= wined3d_swapchain_vk(swapchain
);
1228 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
1229 struct wined3d_context_vk
*context_vk
;
1233 context_vk
= wined3d_context_vk(context_acquire(swapchain
->device
, back_buffer
, 0));
1235 if (!swapchain_vk
->vk_swapchain
|| swapchain_present_is_partial_copy(swapchain
, dst_rect
))
1237 swapchain_blit_gdi(swapchain
, &context_vk
->c
, src_rect
, dst_rect
);
1241 wined3d_texture_load_location(back_buffer
, 0, &context_vk
->c
, back_buffer
->resource
.draw_binding
);
1243 if ((vr
= wined3d_swapchain_vk_blit(swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1245 if (vr
== VK_ERROR_OUT_OF_DATE_KHR
|| vr
== VK_SUBOPTIMAL_KHR
)
1247 if (FAILED(hr
= wined3d_swapchain_vk_recreate(swapchain_vk
)))
1248 ERR("Failed to recreate swapchain, hr %#x.\n", hr
);
1249 else if (vr
== VK_ERROR_OUT_OF_DATE_KHR
&& (vr
= wined3d_swapchain_vk_blit(
1250 swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1251 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1255 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1260 wined3d_swapchain_vk_rotate(swapchain
, context_vk
);
1262 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1263 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1265 TRACE("Starting new frame.\n");
1267 context_release(&context_vk
->c
);
1270 static const struct wined3d_swapchain_ops swapchain_vk_ops
=
1272 swapchain_vk_present
,
1273 swapchain_frontbuffer_updated
,
1276 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
1278 struct wined3d_dc_info
*front
;
1279 POINT offset
= {0, 0};
1284 TRACE("swapchain %p.\n", swapchain
);
1286 front
= &swapchain
->front_buffer
->dc_info
[0];
1287 if (swapchain
->palette
)
1288 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
1290 if (swapchain
->front_buffer
->resource
.map_count
)
1291 ERR("Trying to blit a mapped surface.\n");
1293 TRACE("Copying surface %p to screen.\n", front
);
1296 window
= swapchain
->win_handle
;
1297 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
1299 /* Front buffer coordinates are screen coordinates. Map them to the
1300 * destination window if not fullscreened. */
1301 if (swapchain
->state
.desc
.windowed
)
1302 ClientToScreen(window
, &offset
);
1304 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
1306 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
1307 swapchain
->front_buffer
->resource
.height
);
1308 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
1310 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
1311 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
1312 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
1313 ReleaseDC(window
, dst_dc
);
1315 SetRectEmpty(&swapchain
->front_buffer_update
);
1318 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
1319 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, DWORD flags
)
1321 struct wined3d_dc_info
*front
, *back
;
1326 front
= &swapchain
->front_buffer
->dc_info
[0];
1327 back
= &swapchain
->back_buffers
[0]->dc_info
[0];
1329 /* Flip the surface data. */
1331 bitmap
= front
->bitmap
;
1332 data
= swapchain
->front_buffer
->resource
.heap_memory
;
1334 front
->dc
= back
->dc
;
1335 front
->bitmap
= back
->bitmap
;
1336 swapchain
->front_buffer
->resource
.heap_memory
= swapchain
->back_buffers
[0]->resource
.heap_memory
;
1339 back
->bitmap
= bitmap
;
1340 swapchain
->back_buffers
[0]->resource
.heap_memory
= data
;
1342 SetRect(&swapchain
->front_buffer_update
, 0, 0,
1343 swapchain
->front_buffer
->resource
.width
,
1344 swapchain
->front_buffer
->resource
.height
);
1345 swapchain_gdi_frontbuffer_updated(swapchain
);
1348 static const struct wined3d_swapchain_ops swapchain_no3d_ops
=
1350 swapchain_gdi_present
,
1351 swapchain_gdi_frontbuffer_updated
,
1354 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
1355 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, DWORD
*quality
)
1357 const struct wined3d_adapter
*adapter
;
1358 const struct wined3d_gl_info
*gl_info
;
1359 const struct wined3d_format
*format
;
1360 enum wined3d_multisample_type t
;
1362 if (wined3d_settings
.sample_count
== ~0u)
1365 adapter
= swapchain
->device
->adapter
;
1366 gl_info
= &adapter
->gl_info
;
1367 if (!(format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
)))
1370 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
1371 while (!(format
->multisample_types
& 1u << (t
- 1)))
1373 TRACE("Using sample count %u.\n", t
);
1378 void swapchain_set_max_frame_latency(struct wined3d_swapchain
*swapchain
, const struct wined3d_device
*device
)
1380 /* Subtract 1 for the implicit OpenGL latency. */
1381 swapchain
->max_frame_latency
= device
->max_frame_latency
>= 2 ? device
->max_frame_latency
- 1 : 1;
1384 static enum wined3d_format_id
adapter_format_from_backbuffer_format(const struct wined3d_adapter
*adapter
,
1385 enum wined3d_format_id format_id
)
1387 const struct wined3d_format
*backbuffer_format
;
1389 backbuffer_format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
);
1390 return pixelformat_for_depth(backbuffer_format
->byte_count
* CHAR_BIT
);
1393 static HRESULT
wined3d_swapchain_state_init(struct wined3d_swapchain_state
*state
,
1394 const struct wined3d_swapchain_desc
*desc
, HWND window
, struct wined3d
*wined3d
,
1395 struct wined3d_swapchain_state_parent
*parent
)
1399 state
->desc
= *desc
;
1401 if (FAILED(hr
= wined3d_output_get_display_mode(desc
->output
, &state
->original_mode
, NULL
)))
1403 ERR("Failed to get current display mode, hr %#x.\n", hr
);
1407 if (!desc
->windowed
)
1409 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1411 state
->d3d_mode
.width
= desc
->backbuffer_width
;
1412 state
->d3d_mode
.height
= desc
->backbuffer_height
;
1413 state
->d3d_mode
.format_id
= adapter_format_from_backbuffer_format(desc
->output
->adapter
,
1414 desc
->backbuffer_format
);
1415 state
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1416 state
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1420 state
->d3d_mode
= state
->original_mode
;
1424 GetWindowRect(window
, &state
->original_window_rect
);
1425 state
->wined3d
= wined3d
;
1426 state
->device_window
= window
;
1427 state
->parent
= parent
;
1429 if (desc
->flags
& WINED3D_SWAPCHAIN_REGISTER_STATE
)
1430 wined3d_swapchain_state_register(state
);
1435 static HRESULT
wined3d_swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
1436 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1437 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1438 const struct wined3d_swapchain_ops
*swapchain_ops
)
1440 struct wined3d_resource_desc texture_desc
;
1441 struct wined3d_output_desc output_desc
;
1442 BOOL displaymode_set
= FALSE
;
1443 DWORD texture_flags
= 0;
1444 HRESULT hr
= E_FAIL
;
1449 wined3d_mutex_lock();
1451 if (desc
->backbuffer_count
> 1)
1453 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1454 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1457 if (desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
1458 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
1459 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
1460 FIXME("Unimplemented swap effect %#x.\n", desc
->swap_effect
);
1462 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
1463 if (FAILED(hr
= wined3d_swapchain_state_init(&swapchain
->state
, desc
, window
, device
->wined3d
, state_parent
)))
1465 ERR("Failed to initialise swapchain state, hr %#x.\n", hr
);
1466 wined3d_mutex_unlock();
1470 swapchain
->swapchain_ops
= swapchain_ops
;
1471 swapchain
->device
= device
;
1472 swapchain
->parent
= parent
;
1473 swapchain
->parent_ops
= parent_ops
;
1475 swapchain
->win_handle
= window
;
1476 swapchain
->swap_interval
= WINED3D_SWAP_INTERVAL_DEFAULT
;
1477 swapchain_set_max_frame_latency(swapchain
, device
);
1479 GetClientRect(window
, &client_rect
);
1482 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect
));
1484 if (!desc
->backbuffer_width
)
1486 desc
->backbuffer_width
= client_rect
.right
? client_rect
.right
: 8;
1487 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
1489 if (!desc
->backbuffer_height
)
1491 desc
->backbuffer_height
= client_rect
.bottom
? client_rect
.bottom
: 8;
1492 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
1495 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
1497 desc
->backbuffer_format
= swapchain
->state
.original_mode
.format_id
;
1498 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->state
.original_mode
.format_id
));
1503 if (FAILED(hr
= wined3d_output_get_desc(desc
->output
, &output_desc
)))
1505 ERR("Failed to get output description, hr %#x.\n", hr
);
1509 wined3d_swapchain_state_setup_fullscreen(&swapchain
->state
, window
,
1510 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, desc
->backbuffer_width
,
1511 desc
->backbuffer_height
);
1513 swapchain
->state
.desc
= *desc
;
1514 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->state
.desc
.backbuffer_format
,
1515 &swapchain
->state
.desc
.multisample_type
, &swapchain
->state
.desc
.multisample_quality
);
1517 TRACE("Creating front buffer.\n");
1519 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1520 texture_desc
.format
= swapchain
->state
.desc
.backbuffer_format
;
1521 texture_desc
.multisample_type
= swapchain
->state
.desc
.multisample_type
;
1522 texture_desc
.multisample_quality
= swapchain
->state
.desc
.multisample_quality
;
1523 texture_desc
.usage
= 0;
1524 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1525 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1526 texture_desc
.bind_flags
= 0;
1527 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1528 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1530 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1531 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
)
1532 texture_desc
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1533 texture_desc
.width
= swapchain
->state
.desc
.backbuffer_width
;
1534 texture_desc
.height
= swapchain
->state
.desc
.backbuffer_height
;
1535 texture_desc
.depth
= 1;
1536 texture_desc
.size
= 0;
1538 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
1539 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
1541 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1542 parent
, &texture_desc
, texture_flags
, &swapchain
->front_buffer
)))
1544 WARN("Failed to create front buffer, hr %#x.\n", hr
);
1548 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
1549 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1551 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1552 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1555 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1556 * so we should really check to see if there is a fullscreen swapchain
1557 * already. Does a single head count as full screen? */
1558 if (!desc
->windowed
&& desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1560 /* Change the display settings */
1561 if (FAILED(hr
= wined3d_output_set_display_mode(desc
->output
,
1562 &swapchain
->state
.d3d_mode
)))
1564 WARN("Failed to set display mode, hr %#x.\n", hr
);
1567 displaymode_set
= TRUE
;
1570 if (swapchain
->state
.desc
.backbuffer_count
> 0)
1572 if (!(swapchain
->back_buffers
= heap_calloc(swapchain
->state
.desc
.backbuffer_count
,
1573 sizeof(*swapchain
->back_buffers
))))
1575 ERR("Failed to allocate backbuffer array memory.\n");
1580 texture_desc
.bind_flags
= swapchain
->state
.desc
.backbuffer_bind_flags
;
1581 texture_desc
.usage
= 0;
1582 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1583 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1584 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1586 TRACE("Creating back buffer %u.\n", i
);
1587 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1588 parent
, &texture_desc
, texture_flags
, &swapchain
->back_buffers
[i
])))
1590 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1591 swapchain
->state
.desc
.backbuffer_count
= i
;
1594 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1598 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1599 if (desc
->enable_auto_depth_stencil
)
1601 TRACE("Creating depth/stencil buffer.\n");
1602 if (!device
->auto_depth_stencil_view
)
1604 struct wined3d_view_desc desc
;
1605 struct wined3d_texture
*ds
;
1607 texture_desc
.format
= swapchain
->state
.desc
.auto_depth_stencil_format
;
1608 texture_desc
.usage
= 0;
1609 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
1610 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1611 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1613 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1615 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1616 device
->device_parent
, &texture_desc
, 0, &ds
)))
1618 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1622 desc
.format_id
= ds
->resource
.format
->id
;
1624 desc
.u
.texture
.level_idx
= 0;
1625 desc
.u
.texture
.level_count
= 1;
1626 desc
.u
.texture
.layer_idx
= 0;
1627 desc
.u
.texture
.layer_count
= 1;
1628 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1629 &device
->auto_depth_stencil_view
);
1630 wined3d_texture_decref(ds
);
1633 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1639 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1641 wined3d_mutex_unlock();
1646 if (displaymode_set
)
1648 if (FAILED(wined3d_restore_display_modes(device
->wined3d
)))
1649 ERR("Failed to restore display mode.\n");
1652 if (swapchain
->back_buffers
)
1654 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1656 if (swapchain
->back_buffers
[i
])
1658 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1659 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1662 heap_free(swapchain
->back_buffers
);
1665 if (swapchain
->front_buffer
)
1667 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1668 wined3d_texture_decref(swapchain
->front_buffer
);
1671 wined3d_swapchain_state_cleanup(&swapchain
->state
);
1672 wined3d_mutex_unlock();
1677 HRESULT
wined3d_swapchain_no3d_init(struct wined3d_swapchain
*swapchain_no3d
, struct wined3d_device
*device
,
1678 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1679 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1681 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1682 swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
);
1684 return wined3d_swapchain_init(swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
,
1685 &swapchain_no3d_ops
);
1688 HRESULT
wined3d_swapchain_gl_init(struct wined3d_swapchain_gl
*swapchain_gl
, struct wined3d_device
*device
,
1689 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1690 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1694 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1695 swapchain_gl
, device
, desc
, state_parent
, parent
, parent_ops
);
1697 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_gl
->s
, device
, desc
, state_parent
, parent
,
1698 parent_ops
, &swapchain_gl_ops
)))
1700 /* Cleanup any context that may have been created for the swapchain. */
1701 wined3d_cs_destroy_object(device
->cs
, wined3d_swapchain_gl_destroy_object
, swapchain_gl
);
1702 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1708 HRESULT
wined3d_swapchain_vk_init(struct wined3d_swapchain_vk
*swapchain_vk
, struct wined3d_device
*device
,
1709 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 %#x.\n", hr
);
1733 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1734 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 %u.\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
);
1796 if (!wined3d_array_reserve((void **)&swapchain_gl
->contexts
, &swapchain_gl
->contexts_size
,
1797 swapchain_gl
->context_count
+ 1, sizeof(*swapchain_gl
->contexts
)))
1799 ERR("Failed to allocate new context array memory.\n");
1800 wined3d_context_gl_destroy(context_gl
);
1803 swapchain_gl
->contexts
[swapchain_gl
->context_count
++] = context_gl
;
1808 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl
*swapchain_gl
)
1812 TRACE("swapchain_gl %p.\n", swapchain_gl
);
1814 for (i
= 0; i
< swapchain_gl
->context_count
; ++i
)
1816 wined3d_context_gl_destroy(swapchain_gl
->contexts
[i
]);
1818 heap_free(swapchain_gl
->contexts
);
1819 swapchain_gl
->contexts_size
= 0;
1820 swapchain_gl
->context_count
= 0;
1821 swapchain_gl
->contexts
= NULL
;
1824 struct wined3d_context_gl
*wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1826 DWORD tid
= GetCurrentThreadId();
1829 for (i
= 0; i
< swapchain_gl
->context_count
; ++i
)
1831 if (swapchain_gl
->contexts
[i
]->tid
== tid
)
1832 return swapchain_gl
->contexts
[i
];
1835 /* Create a new context for the thread. */
1836 return wined3d_swapchain_gl_create_context(swapchain_gl
);
1839 HDC
wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl
*swapchain_gl
)
1841 if (!swapchain_gl
->backup_dc
)
1843 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl
);
1845 if (!(swapchain_gl
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1846 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1848 ERR("Failed to create a window.\n");
1852 if (!(swapchain_gl
->backup_dc
= GetDC(swapchain_gl
->backup_wnd
)))
1854 ERR("Failed to get a DC.\n");
1855 DestroyWindow(swapchain_gl
->backup_wnd
);
1856 swapchain_gl
->backup_wnd
= NULL
;
1861 return swapchain_gl
->backup_dc
;
1864 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1868 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1870 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1872 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1876 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1878 struct wined3d_device
*device
= swapchain
->device
;
1879 HWND window
= swapchain
->state
.device_window
;
1880 struct wined3d_output_desc output_desc
;
1881 unsigned int screensaver_active
;
1882 struct wined3d_output
*output
;
1883 BOOL focus_messages
, filter
;
1886 /* This code is not protected by the wined3d mutex, so it may run while
1887 * wined3d_device_reset is active. Testing on Windows shows that changing
1888 * focus during resets and resetting during focus change events causes
1889 * the application to crash with an invalid memory access. */
1891 if (!(focus_messages
= device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
))
1892 filter
= wined3d_filter_messages(window
, TRUE
);
1896 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
1897 if ((device
->restore_screensaver
= !!screensaver_active
))
1898 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
1900 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1902 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1903 * the window but leaves the size untouched, d3d9 sets the size on an
1904 * invisible window, generates messages but doesn't change the window
1905 * properties. The implementation follows d3d9.
1907 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1908 * resume drawing after a focus loss. */
1909 output
= wined3d_swapchain_get_output(swapchain
);
1912 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1916 if (SUCCEEDED(hr
= wined3d_output_get_desc(output
, &output_desc
)))
1917 SetWindowPos(window
, NULL
, output_desc
.desktop_rect
.left
,
1918 output_desc
.desktop_rect
.top
, swapchain
->state
.desc
.backbuffer_width
,
1919 swapchain
->state
.desc
.backbuffer_height
, SWP_NOACTIVATE
| SWP_NOZORDER
);
1921 ERR("Failed to get output description, hr %#x.\n", hr
);
1924 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1926 output
= wined3d_swapchain_get_output(swapchain
);
1929 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1933 if (FAILED(hr
= wined3d_output_set_display_mode(output
,
1934 &swapchain
->state
.d3d_mode
)))
1935 ERR("Failed to set display mode, hr %#x.\n", hr
);
1938 if (swapchain
== device
->swapchains
[0])
1939 device
->device_parent
->ops
->activate(device
->device_parent
, TRUE
);
1943 if (device
->restore_screensaver
)
1945 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
1946 device
->restore_screensaver
= FALSE
;
1949 if (FAILED(hr
= wined3d_restore_display_modes(device
->wined3d
)))
1950 ERR("Failed to restore display modes, hr %#x.\n", hr
);
1952 swapchain
->reapply_mode
= TRUE
;
1954 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1955 * during window minimization. Do our housekeeping now, as the device may not exist after
1956 * the ShowWindow call.
1958 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1959 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1960 * we'll have to control this via a create flag. Note that the device and swapchain are not
1961 * safe to access after the ShowWindow call. */
1962 if (swapchain
== device
->swapchains
[0])
1963 device
->device_parent
->ops
->activate(device
->device_parent
, FALSE
);
1965 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
) && IsWindowVisible(window
))
1966 ShowWindow(window
, SW_MINIMIZE
);
1969 if (!focus_messages
)
1970 wined3d_filter_messages(window
, filter
);
1973 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1974 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1975 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1977 struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
1978 BOOL update_desc
= FALSE
;
1980 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1981 "multisample_type %#x, multisample_quality %#x.\n",
1982 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1983 multisample_type
, multisample_quality
);
1985 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1987 if (buffer_count
&& buffer_count
!= desc
->backbuffer_count
)
1988 FIXME("Cannot change the back buffer count yet.\n");
1990 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1992 if (!width
|| !height
)
1996 /* The application is requesting that either the swapchain width or
1997 * height be set to the corresponding dimension in the window's
2000 if (!GetClientRect(swapchain
->state
.device_window
, &client_rect
))
2002 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
2003 return WINED3DERR_INVALIDCALL
;
2007 width
= client_rect
.right
;
2010 height
= client_rect
.bottom
;
2013 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
2015 desc
->backbuffer_width
= width
;
2016 desc
->backbuffer_height
= height
;
2020 if (format_id
== WINED3DFMT_UNKNOWN
)
2022 if (!desc
->windowed
)
2023 return WINED3DERR_INVALIDCALL
;
2024 format_id
= swapchain
->state
.original_mode
.format_id
;
2027 if (format_id
!= desc
->backbuffer_format
)
2029 desc
->backbuffer_format
= format_id
;
2033 if (multisample_type
!= desc
->multisample_type
2034 || multisample_quality
!= desc
->multisample_quality
)
2036 desc
->multisample_type
= multisample_type
;
2037 desc
->multisample_quality
= multisample_quality
;
2046 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, 0, desc
->backbuffer_width
,
2047 desc
->backbuffer_height
, desc
->backbuffer_format
,
2048 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2051 for (i
= 0; i
< desc
->backbuffer_count
; ++i
)
2053 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], 0, desc
->backbuffer_width
,
2054 desc
->backbuffer_height
, desc
->backbuffer_format
,
2055 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2060 swapchain_update_draw_bindings(swapchain
);
2065 static HRESULT
wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state
*state
,
2066 struct wined3d_output
*output
, struct wined3d_display_mode
*mode
)
2070 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
2072 if (FAILED(hr
= wined3d_output_find_closest_matching_mode(output
, mode
)))
2074 WARN("Failed to find closest matching mode, hr %#x.\n", hr
);
2078 if (output
!= state
->desc
.output
)
2080 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2082 WARN("Failed to restore display modes, hr %#x.\n", hr
);
2086 if (FAILED(hr
= wined3d_output_get_display_mode(output
, &state
->original_mode
, NULL
)))
2088 WARN("Failed to get current display mode, hr %#x.\n", hr
);
2093 if (FAILED(hr
= wined3d_output_set_display_mode(output
, mode
)))
2095 WARN("Failed to set display mode, hr %#x.\n", hr
);
2096 return WINED3DERR_INVALIDCALL
;
2102 HRESULT CDECL
wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state
*state
,
2103 const struct wined3d_display_mode
*mode
)
2105 struct wined3d_display_mode actual_mode
;
2106 struct wined3d_output_desc output_desc
;
2107 RECT original_window_rect
, window_rect
;
2108 int x
, y
, width
, height
;
2112 TRACE("state %p, mode %p.\n", state
, mode
);
2114 wined3d_mutex_lock();
2116 window
= state
->device_window
;
2118 if (state
->desc
.windowed
)
2120 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
2121 AdjustWindowRectEx(&window_rect
,
2122 GetWindowLongW(window
, GWL_STYLE
), FALSE
,
2123 GetWindowLongW(window
, GWL_EXSTYLE
));
2124 GetWindowRect(window
, &original_window_rect
);
2126 x
= original_window_rect
.left
;
2127 y
= original_window_rect
.top
;
2128 width
= window_rect
.right
- window_rect
.left
;
2129 height
= window_rect
.bottom
- window_rect
.top
;
2133 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2135 actual_mode
= *mode
;
2136 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, state
->desc
.output
,
2139 ERR("Failed to set display mode, hr %#x.\n", hr
);
2140 wined3d_mutex_unlock();
2145 if (FAILED(hr
= wined3d_output_get_desc(state
->desc
.output
, &output_desc
)))
2147 ERR("Failed to get output description, hr %#x.\n", hr
);
2148 wined3d_mutex_unlock();
2152 x
= output_desc
.desktop_rect
.left
;
2153 y
= output_desc
.desktop_rect
.top
;
2154 width
= output_desc
.desktop_rect
.right
- output_desc
.desktop_rect
.left
;
2155 height
= output_desc
.desktop_rect
.bottom
- output_desc
.desktop_rect
.top
;
2158 wined3d_mutex_unlock();
2160 MoveWindow(window
, x
, y
, width
, height
, TRUE
);
2165 static LONG
fullscreen_style(LONG style
)
2167 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2168 style
|= WS_POPUP
| WS_SYSMENU
;
2169 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
2174 static LONG
fullscreen_exstyle(LONG exstyle
)
2176 /* Filter out window decorations. */
2177 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
2182 struct wined3d_window_state
2185 HWND window_pos_after
;
2186 LONG style
, exstyle
;
2187 int x
, y
, width
, height
;
2192 static DWORD WINAPI
wined3d_set_window_state(void *ctx
)
2194 struct wined3d_window_state
*s
= ctx
;
2197 filter
= wined3d_filter_messages(s
->window
, TRUE
);
2201 SetWindowLongW(s
->window
, GWL_STYLE
, s
->style
);
2202 SetWindowLongW(s
->window
, GWL_EXSTYLE
, s
->exstyle
);
2204 SetWindowPos(s
->window
, s
->window_pos_after
, s
->x
, s
->y
, s
->width
, s
->height
, s
->flags
);
2206 wined3d_filter_messages(s
->window
, filter
);
2213 HRESULT
wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state
*state
,
2214 HWND window
, int x
, int y
, int width
, int height
)
2216 struct wined3d_window_state
*s
;
2217 DWORD window_tid
, tid
;
2220 TRACE("Setting up window %p for fullscreen mode.\n", window
);
2222 if (!IsWindow(window
))
2224 WARN("%p is not a valid window.\n", window
);
2225 return WINED3DERR_NOTAVAILABLE
;
2228 if (!(s
= heap_alloc(sizeof(*s
))))
2229 return E_OUTOFMEMORY
;
2231 s
->window_pos_after
= HWND_TOPMOST
;
2237 if (state
->style
|| state
->exstyle
)
2239 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
2240 window
, state
->style
, state
->exstyle
);
2243 s
->flags
= SWP_FRAMECHANGED
| SWP_NOACTIVATE
;
2244 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
)
2245 s
->flags
|= SWP_NOZORDER
;
2247 s
->flags
|= SWP_SHOWWINDOW
;
2249 state
->style
= GetWindowLongW(window
, GWL_STYLE
);
2250 state
->exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2252 s
->style
= fullscreen_style(state
->style
);
2253 s
->exstyle
= fullscreen_exstyle(state
->exstyle
);
2254 s
->set_style
= true;
2256 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
2257 state
->style
, state
->exstyle
, s
->style
, s
->exstyle
);
2259 window_tid
= GetWindowThreadProcessId(window
, NULL
);
2260 tid
= GetCurrentThreadId();
2262 TRACE("Window %p belongs to thread %#x.\n", window
, window_tid
);
2263 /* If the window belongs to a different thread, modifying the style and/or
2264 * position can potentially deadlock if that thread isn't processing
2266 if (window_tid
== tid
)
2267 wined3d_set_window_state(s
);
2268 else if (!(thread
= CreateThread(NULL
, 0, wined3d_set_window_state
, s
, 0, NULL
)))
2269 ERR("Failed to create thread.\n");
2271 CloseHandle(thread
);
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 DWORD window_tid
, tid
;
2281 LONG style
, exstyle
;
2284 if (!state
->style
&& !state
->exstyle
)
2287 if (!(s
= heap_alloc(sizeof(*s
))))
2291 s
->window_pos_after
= NULL
;
2292 s
->flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
2294 if ((state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE
)
2295 && !(state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
))
2297 s
->window_pos_after
= (state
->exstyle
& WS_EX_TOPMOST
) ? HWND_TOPMOST
: HWND_NOTOPMOST
;
2298 s
->flags
|= (state
->style
& WS_VISIBLE
) ? SWP_SHOWWINDOW
: SWP_HIDEWINDOW
;
2299 s
->flags
&= ~SWP_NOZORDER
;
2302 style
= GetWindowLongW(window
, GWL_STYLE
);
2303 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2305 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2306 * application, and we want to ignore them in the test below, since it's
2307 * not the application's fault that they changed. Additionally, we want to
2308 * preserve the current status of these flags (i.e. don't restore them) to
2309 * more closely emulate the behavior of Direct3D, which leaves these flags
2310 * alone when returning to windowed mode. */
2311 state
->style
^= (state
->style
^ style
) & WS_VISIBLE
;
2312 state
->exstyle
^= (state
->exstyle
^ exstyle
) & WS_EX_TOPMOST
;
2314 TRACE("Restoring window style of window %p to %08x, %08x.\n",
2315 window
, state
->style
, state
->exstyle
);
2317 s
->style
= state
->style
;
2318 s
->exstyle
= state
->exstyle
;
2319 /* Only restore the style if the application didn't modify it during the
2320 * fullscreen phase. Some applications change it before calling Reset()
2321 * when switching between windowed and fullscreen modes (HL2), some
2322 * depend on the original style (Eve Online). */
2323 s
->set_style
= style
== fullscreen_style(state
->style
) && exstyle
== fullscreen_exstyle(state
->exstyle
);
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 window_tid
= GetWindowThreadProcessId(window
, NULL
);
2339 tid
= GetCurrentThreadId();
2341 TRACE("Window %p belongs to thread %#x.\n", window
, window_tid
);
2342 /* If the window belongs to a different thread, modifying the style and/or
2343 * position can potentially deadlock if that thread isn't processing
2345 if (window_tid
== tid
)
2346 wined3d_set_window_state(s
);
2347 else if (!(thread
= CreateThread(NULL
, 0, wined3d_set_window_state
, s
, 0, NULL
)))
2348 ERR("Failed to create thread.\n");
2350 CloseHandle(thread
);
2352 /* Delete the old values. */
2357 HRESULT CDECL
wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state
*state
,
2358 const struct wined3d_swapchain_desc
*swapchain_desc
,
2359 const struct wined3d_display_mode
*mode
)
2361 struct wined3d_display_mode actual_mode
;
2362 struct wined3d_output_desc output_desc
;
2363 BOOL windowed
= state
->desc
.windowed
;
2366 TRACE("state %p, swapchain_desc %p, mode %p.\n", state
, swapchain_desc
, mode
);
2368 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2372 actual_mode
= *mode
;
2373 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2379 if (!swapchain_desc
->windowed
)
2381 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
2382 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
2383 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
2384 actual_mode
.format_id
= adapter_format_from_backbuffer_format(swapchain_desc
->output
->adapter
,
2385 swapchain_desc
->backbuffer_format
);
2386 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
2387 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2393 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2395 WARN("Failed to restore display modes for all outputs, hr %#x.\n", hr
);
2404 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2406 if (FAILED(hr
= wined3d_output_get_display_mode(swapchain_desc
->output
, &actual_mode
,
2409 ERR("Failed to get display mode, hr %#x.\n", hr
);
2410 return WINED3DERR_INVALIDCALL
;
2414 if (!swapchain_desc
->windowed
)
2416 unsigned int width
= actual_mode
.width
;
2417 unsigned int height
= actual_mode
.height
;
2419 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
2421 ERR("Failed to get output description, hr %#x.\n", hr
);
2425 if (state
->desc
.windowed
)
2427 /* Switch from windowed to fullscreen */
2428 if (FAILED(hr
= wined3d_swapchain_state_setup_fullscreen(state
, state
->device_window
,
2429 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
, height
)))
2434 HWND window
= state
->device_window
;
2437 /* Fullscreen -> fullscreen mode change */
2438 filter
= wined3d_filter_messages(window
, TRUE
);
2439 MoveWindow(window
, output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
,
2441 ShowWindow(window
, SW_SHOW
);
2442 wined3d_filter_messages(window
, filter
);
2444 state
->d3d_mode
= actual_mode
;
2446 else if (!state
->desc
.windowed
)
2448 /* Fullscreen -> windowed switch */
2449 RECT
*window_rect
= NULL
;
2450 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
2451 window_rect
= &state
->original_window_rect
;
2452 wined3d_swapchain_state_restore_from_fullscreen(state
, state
->device_window
, window_rect
);
2455 state
->desc
.output
= swapchain_desc
->output
;
2456 state
->desc
.windowed
= swapchain_desc
->windowed
;
2458 if (windowed
!= state
->desc
.windowed
)
2459 state
->parent
->ops
->windowed_state_changed(state
->parent
, state
->desc
.windowed
);
2464 BOOL CDECL
wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state
*state
)
2466 TRACE("state %p.\n", state
);
2468 return state
->desc
.windowed
;
2471 void CDECL
wined3d_swapchain_state_destroy(struct wined3d_swapchain_state
*state
)
2473 wined3d_swapchain_state_cleanup(state
);
2477 HRESULT CDECL
wined3d_swapchain_state_create(const struct wined3d_swapchain_desc
*desc
,
2478 HWND window
, struct wined3d
*wined3d
, struct wined3d_swapchain_state_parent
*state_parent
,
2479 struct wined3d_swapchain_state
**state
)
2481 struct wined3d_swapchain_state
*s
;
2484 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc
, window
, wined3d
, state
);
2486 if (!(s
= heap_alloc_zero(sizeof(*s
))))
2487 return E_OUTOFMEMORY
;
2489 if (FAILED(hr
= wined3d_swapchain_state_init(s
, desc
, window
, wined3d
, state_parent
)))