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
);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
28 void wined3d_swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
33 TRACE("Destroying swapchain %p.\n", swapchain
);
35 wined3d_swapchain_state_cleanup(&swapchain
->state
);
36 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
38 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
39 * is the last buffer to be destroyed, FindContext() depends on that. */
40 if (swapchain
->front_buffer
)
42 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
43 if (wined3d_texture_decref(swapchain
->front_buffer
))
44 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
45 swapchain
->front_buffer
= NULL
;
48 if (swapchain
->back_buffers
)
50 i
= swapchain
->state
.desc
.backbuffer_count
;
54 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
55 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
56 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
58 heap_free(swapchain
->back_buffers
);
59 swapchain
->back_buffers
= NULL
;
62 /* Restore the screen resolution if we rendered in fullscreen.
63 * This will restore the screen resolution to what it was before creating
64 * the swapchain. In case of d3d8 and d3d9 this will be the original
65 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
66 * sets the resolution before starting up Direct3D, thus orig_width and
67 * orig_height will be equal to the modes in the presentation params. */
68 if (!swapchain
->state
.desc
.windowed
)
70 if (swapchain
->state
.desc
.auto_restore_display_mode
)
72 if (FAILED(hr
= wined3d_restore_display_modes(swapchain
->device
->wined3d
)))
73 ERR("Failed to restore display mode, hr %#lx.\n", hr
);
75 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
77 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
,
78 swapchain
->state
.device_window
, &swapchain
->state
.original_window_rect
);
79 wined3d_device_release_focus_window(swapchain
->device
);
84 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
, swapchain
->state
.device_window
, NULL
);
89 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl
*swapchain_gl
)
91 wined3d_swapchain_cleanup(&swapchain_gl
->s
);
94 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
96 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
97 const struct wined3d_vk_info
*vk_info
;
101 TRACE("swapchain_vk %p.\n", swapchain_vk
);
103 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
105 if ((vr
= VK_CALL(vkQueueWaitIdle(device_vk
->vk_queue
))) < 0)
106 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr
));
107 heap_free(swapchain_vk
->vk_images
);
108 for (i
= 0; i
< swapchain_vk
->image_count
; ++i
)
110 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
111 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
113 heap_free(swapchain_vk
->vk_semaphores
);
114 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, NULL
));
115 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, swapchain_vk
->vk_surface
, NULL
));
118 static void wined3d_swapchain_vk_destroy_object(void *object
)
120 wined3d_swapchain_vk_destroy_vulkan_swapchain(object
);
123 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk
*swapchain_vk
)
125 struct wined3d_cs
*cs
= swapchain_vk
->s
.device
->cs
;
127 wined3d_cs_destroy_object(cs
, wined3d_swapchain_vk_destroy_object
, swapchain_vk
);
128 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
130 wined3d_swapchain_cleanup(&swapchain_vk
->s
);
133 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
135 unsigned int refcount
= InterlockedIncrement(&swapchain
->ref
);
137 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
142 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
144 unsigned int refcount
= InterlockedDecrement(&swapchain
->ref
);
146 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
150 struct wined3d_device
*device
;
152 wined3d_mutex_lock();
154 device
= swapchain
->device
;
155 if (device
->swapchain_count
&& device
->swapchains
[0] == swapchain
)
156 wined3d_device_uninit_3d(device
);
157 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
159 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
160 swapchain
->device
->adapter
->adapter_ops
->adapter_destroy_swapchain(swapchain
);
162 wined3d_mutex_unlock();
168 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
170 TRACE("swapchain %p.\n", swapchain
);
172 return swapchain
->parent
;
175 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
178 window
= swapchain
->state
.device_window
;
179 if (window
== swapchain
->win_handle
)
182 TRACE("Setting swapchain %p window from %p to %p.\n",
183 swapchain
, swapchain
->win_handle
, window
);
185 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
187 swapchain
->win_handle
= window
;
190 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
191 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
192 unsigned int swap_interval
, uint32_t flags
)
196 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
197 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
198 dst_window_override
, swap_interval
, flags
);
201 FIXME("Ignoring flags %#x.\n", flags
);
203 wined3d_mutex_lock();
205 if (!swapchain
->back_buffers
)
207 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
208 wined3d_mutex_unlock();
209 return WINED3DERR_INVALIDCALL
;
214 SetRect(&s
, 0, 0, swapchain
->state
.desc
.backbuffer_width
,
215 swapchain
->state
.desc
.backbuffer_height
);
221 GetClientRect(swapchain
->win_handle
, &d
);
225 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
226 dst_rect
, dst_window_override
, swap_interval
, flags
);
228 wined3d_mutex_unlock();
233 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
234 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
236 RECT src_rect
, dst_rect
;
238 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
240 SetRect(&src_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
, swapchain
->front_buffer
->resource
.height
);
243 if (swapchain
->state
.desc
.windowed
)
245 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
246 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
247 wine_dbgstr_rect(&dst_rect
));
250 return wined3d_device_context_blt(&swapchain
->device
->cs
->c
, dst_texture
, sub_resource_idx
, &dst_rect
,
251 swapchain
->front_buffer
, 0, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
254 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
255 UINT back_buffer_idx
)
257 TRACE("swapchain %p, back_buffer_idx %u.\n",
258 swapchain
, back_buffer_idx
);
260 /* Return invalid if there is no backbuffer array, otherwise it will
261 * crash when ddraw is used (there swapchain->back_buffers is always
262 * NULL). We need this because this function is called from
263 * stateblock_init_default_state() to get the default scissorrect
265 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->state
.desc
.backbuffer_count
)
267 WARN("Invalid back buffer index.\n");
268 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
269 * here in wined3d to avoid problems in other libs. */
273 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
275 return swapchain
->back_buffers
[back_buffer_idx
];
278 struct wined3d_texture
* CDECL
wined3d_swapchain_get_front_buffer(const struct wined3d_swapchain
*swapchain
)
280 TRACE("swapchain %p.\n", swapchain
);
282 return swapchain
->front_buffer
;
285 struct wined3d_output
* wined3d_swapchain_get_output(const struct wined3d_swapchain
*swapchain
)
287 TRACE("swapchain %p.\n", swapchain
);
289 return swapchain
->state
.desc
.output
;
292 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
293 struct wined3d_raster_status
*raster_status
)
295 struct wined3d_output
*output
;
297 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
299 output
= wined3d_swapchain_get_output(swapchain
);
302 ERR("Failed to get output from swapchain %p.\n", swapchain
);
306 return wined3d_output_get_raster_status(output
, raster_status
);
309 struct wined3d_swapchain_state
* CDECL
wined3d_swapchain_get_state(struct wined3d_swapchain
*swapchain
)
311 return &swapchain
->state
;
314 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
315 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
317 struct wined3d_output
*output
;
320 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
322 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
324 ERR("Failed to get output from swapchain %p.\n", swapchain
);
328 hr
= wined3d_output_get_display_mode(output
, mode
, rotation
);
330 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
331 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
336 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
338 TRACE("swapchain %p.\n", swapchain
);
340 return swapchain
->device
;
343 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
344 struct wined3d_swapchain_desc
*desc
)
346 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
348 *desc
= swapchain
->state
.desc
;
351 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
352 uint32_t flags
, const struct wined3d_gamma_ramp
*ramp
)
354 struct wined3d_output
*output
;
356 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
359 FIXME("Ignoring flags %#x.\n", flags
);
361 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
363 ERR("Failed to get output from swapchain %p.\n", swapchain
);
367 return wined3d_output_set_gamma_ramp(output
, ramp
);
370 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
372 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
374 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
376 swapchain
->palette
= palette
;
379 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
380 struct wined3d_gamma_ramp
*ramp
)
382 struct wined3d_output
*output
;
384 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
386 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
388 ERR("Failed to get output from swapchain %p.\n", swapchain
);
392 return wined3d_output_get_gamma_ramp(output
, ramp
);
395 /* The is a fallback for cases where we e.g. can't create a GL context or
396 * Vulkan swapchain for the swapchain window. */
397 static void swapchain_blit_gdi(struct wined3d_swapchain
*swapchain
,
398 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
400 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
401 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
402 D3DKMT_CREATEDCFROMMEMORY create_desc
;
403 const struct wined3d_format
*format
;
404 unsigned int row_pitch
, slice_pitch
;
409 static unsigned int once
;
411 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
412 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
415 FIXME("Using GDI present.\n");
417 format
= back_buffer
->resource
.format
;
418 if (!format
->ddi_format
)
420 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
424 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_SYSMEM
);
425 wined3d_texture_get_pitch(back_buffer
, 0, &row_pitch
, &slice_pitch
);
427 create_desc
.pMemory
= back_buffer
->resource
.heap_memory
;
428 create_desc
.Format
= format
->ddi_format
;
429 create_desc
.Width
= wined3d_texture_get_level_width(back_buffer
, 0);
430 create_desc
.Height
= wined3d_texture_get_level_height(back_buffer
, 0);
431 create_desc
.Pitch
= row_pitch
;
432 create_desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
433 create_desc
.pColorTable
= NULL
;
435 status
= D3DKMTCreateDCFromMemory(&create_desc
);
436 DeleteDC(create_desc
.hDeviceDc
);
439 WARN("Failed to create DC, status %#lx.\n", status
);
443 src_dc
= create_desc
.hDc
;
444 bitmap
= create_desc
.hBitmap
;
446 TRACE("Created source DC %p, bitmap %p for backbuffer %p.\n", src_dc
, bitmap
, back_buffer
);
448 if (!(dst_dc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
449 ERR("Failed to get destination DC.\n");
451 if (!StretchBlt(dst_dc
, dst_rect
->left
, dst_rect
->top
, dst_rect
->right
- dst_rect
->left
,
452 dst_rect
->bottom
- dst_rect
->top
, src_dc
, src_rect
->left
, src_rect
->top
,
453 src_rect
->right
- src_rect
->left
, src_rect
->bottom
- src_rect
->top
, SRCCOPY
))
454 ERR("Failed to blit.\n");
456 ReleaseDC(swapchain
->win_handle
, dst_dc
);
457 destroy_desc
.hDc
= src_dc
;
458 destroy_desc
.hBitmap
= bitmap
;
459 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
460 ERR("Failed to destroy src dc, status %#lx.\n", status
);
463 /* A GL context is provided by the caller */
464 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
465 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
467 struct wined3d_texture
*texture
= swapchain
->back_buffers
[0];
468 struct wined3d_device
*device
= swapchain
->device
;
469 enum wined3d_texture_filter_type filter
;
472 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
473 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
475 if ((src_rect
->right
- src_rect
->left
== dst_rect
->right
- dst_rect
->left
476 && src_rect
->bottom
- src_rect
->top
== dst_rect
->bottom
- dst_rect
->top
)
477 || is_complex_fixup(texture
->resource
.format
->color_fixup
))
478 filter
= WINED3D_TEXF_NONE
;
480 filter
= WINED3D_TEXF_LINEAR
;
482 location
= WINED3D_LOCATION_TEXTURE_RGB
;
483 if (texture
->resource
.multisample_type
)
484 location
= WINED3D_LOCATION_RB_RESOLVED
;
486 wined3d_texture_validate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
487 device
->blitter
->ops
->blitter_blit(device
->blitter
, WINED3D_BLIT_OP_COLOR_BLIT
, context
, texture
, 0,
488 location
, src_rect
, texture
, 0, WINED3D_LOCATION_DRAWABLE
, dst_rect
, NULL
, filter
, NULL
);
489 wined3d_texture_invalidate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
492 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain
*swapchain
,
493 struct wined3d_context_gl
*context_gl
, unsigned int swap_interval
)
495 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
497 swap_interval
= swap_interval
<= 4 ? swap_interval
: 1;
498 if (swapchain
->swap_interval
== swap_interval
)
501 swapchain
->swap_interval
= swap_interval
;
503 if (!gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
506 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
508 ERR("Failed to set swap interval %u for context %p, last error %#lx.\n",
509 swap_interval
, context_gl
, GetLastError());
513 /* Context activation is done by the caller. */
514 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
516 struct wined3d_texture_sub_resource
*sub_resource
;
517 struct wined3d_texture_gl
*texture
, *texture_prev
;
518 struct gl_texture tex0
;
522 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
524 if (swapchain
->state
.desc
.backbuffer_count
< 2 || wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
527 texture_prev
= wined3d_texture_gl(swapchain
->back_buffers
[0]);
529 /* Back buffer 0 is already in the draw binding. */
530 tex0
= texture_prev
->texture_rgb
;
531 rb0
= texture_prev
->rb_multisample
;
532 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
534 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
536 texture
= wined3d_texture_gl(swapchain
->back_buffers
[i
]);
537 sub_resource
= &texture
->t
.sub_resources
[0];
539 if (!(sub_resource
->locations
& supported_locations
))
540 wined3d_texture_load_location(&texture
->t
, 0, context
, texture
->t
.resource
.draw_binding
);
542 texture_prev
->texture_rgb
= texture
->texture_rgb
;
543 texture_prev
->rb_multisample
= texture
->rb_multisample
;
545 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
546 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
548 texture_prev
= texture
;
551 texture_prev
->texture_rgb
= tex0
;
552 texture_prev
->rb_multisample
= rb0
;
554 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
555 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
557 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
560 static bool swapchain_present_is_partial_copy(struct wined3d_swapchain
*swapchain
, const RECT
*dst_rect
)
562 enum wined3d_swap_effect swap_effect
= swapchain
->state
.desc
.swap_effect
;
566 if (swap_effect
!= WINED3D_SWAP_EFFECT_COPY
&& swap_effect
!= WINED3D_SWAP_EFFECT_COPY_VSYNC
)
569 GetClientRect(swapchain
->win_handle
, &client_rect
);
571 t
= client_rect
.right
- client_rect
.left
;
572 if ((dst_rect
->left
&& dst_rect
->right
) || abs(dst_rect
->right
- dst_rect
->left
) != t
)
574 t
= client_rect
.bottom
- client_rect
.top
;
575 if ((dst_rect
->top
&& dst_rect
->bottom
) || abs(dst_rect
->bottom
- dst_rect
->top
) != t
)
581 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
582 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
584 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
585 const struct wined3d_pixel_format
*pixel_format
;
586 const struct wined3d_gl_info
*gl_info
;
587 struct wined3d_context_gl
*context_gl
;
588 struct wined3d_context
*context
;
590 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
, 0);
591 context_gl
= wined3d_context_gl(context
);
592 if (!context_gl
->valid
)
594 context_release(context
);
595 WARN("Invalid context, skipping present.\n");
599 TRACE("Presenting DC %p.\n", context_gl
->dc
);
601 pixel_format
= &wined3d_adapter_gl(swapchain
->device
->adapter
)->pixel_formats
[context_gl
->pixel_format
];
602 if (context_gl
->dc
== wined3d_device_gl(swapchain
->device
)->backup_dc
603 || (pixel_format
->swap_method
!= WGL_SWAP_COPY_ARB
604 && swapchain_present_is_partial_copy(swapchain
, dst_rect
)))
606 swapchain_blit_gdi(swapchain
, context
, src_rect
, dst_rect
);
610 gl_info
= context_gl
->gl_info
;
612 swapchain_gl_set_swap_interval(swapchain
, context_gl
, swap_interval
);
614 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
616 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
617 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
619 if (swapchain
->device
->context_count
> 1)
621 WARN_(d3d_perf
)("Multiple contexts, calling glFinish() to enforce ordering.\n");
622 gl_info
->gl_ops
.gl
.p_glFinish();
625 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
626 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context_gl
->dc
);
629 if (context
->d3d_info
->fences
)
630 wined3d_context_gl_submit_command_fence(context_gl
);
632 wined3d_swapchain_gl_rotate(swapchain
, context
);
634 TRACE("SwapBuffers called, Starting new frame\n");
636 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
637 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
639 context_release(context
);
642 static void swapchain_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
644 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
645 struct wined3d_context
*context
;
647 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
648 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
649 context_release(context
);
650 SetRectEmpty(&swapchain
->front_buffer_update
);
653 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
655 swapchain_gl_present
,
656 swapchain_frontbuffer_updated
,
659 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk
*swapchain_vk
,
660 VkPresentModeKHR vk_present_mode
)
662 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
663 const struct wined3d_vk_info
*vk_info
;
664 struct wined3d_adapter_vk
*adapter_vk
;
665 VkPhysicalDevice vk_physical_device
;
666 VkPresentModeKHR
*vk_modes
;
667 bool supported
= false;
671 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
672 vk_physical_device
= adapter_vk
->physical_device
;
673 vk_info
= &adapter_vk
->vk_info
;
675 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
676 swapchain_vk
->vk_surface
, &count
, NULL
))) < 0)
678 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr
));
682 if (!(vk_modes
= heap_calloc(count
, sizeof(*vk_modes
))))
685 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
686 swapchain_vk
->vk_surface
, &count
, vk_modes
))) < 0)
688 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr
));
692 for (i
= 0; i
< count
; ++i
)
694 if (vk_modes
[i
] == vk_present_mode
)
706 static VkFormat
get_swapchain_fallback_format(VkFormat vk_format
)
710 case VK_FORMAT_R8G8B8A8_SRGB
:
711 return VK_FORMAT_B8G8R8A8_SRGB
;
712 case VK_FORMAT_R8G8B8A8_UNORM
:
713 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
714 case VK_FORMAT_R16G16B16A16_SFLOAT
:
715 return VK_FORMAT_B8G8R8A8_UNORM
;
717 WARN("Unhandled format %#x.\n", vk_format
);
718 return VK_FORMAT_UNDEFINED
;
722 static VkFormat
wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk
*swapchain_vk
,
723 VkSurfaceKHR vk_surface
)
725 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
726 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
727 const struct wined3d_vk_info
*vk_info
;
728 struct wined3d_adapter_vk
*adapter_vk
;
729 const struct wined3d_format
*format
;
730 VkPhysicalDevice vk_physical_device
;
731 VkSurfaceFormatKHR
*vk_formats
;
732 uint32_t format_count
, i
;
736 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
737 vk_physical_device
= adapter_vk
->physical_device
;
738 vk_info
= &adapter_vk
->vk_info
;
740 if ((format
= wined3d_get_format(&adapter_vk
->a
, desc
->backbuffer_format
, WINED3D_BIND_RENDER_TARGET
)))
741 vk_format
= wined3d_format_vk(format
)->vk_format
;
743 vk_format
= VK_FORMAT_B8G8R8A8_UNORM
;
745 vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
, vk_surface
, &format_count
, NULL
));
746 if (vr
< 0 || !format_count
)
748 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr
));
749 return VK_FORMAT_UNDEFINED
;
752 if (!(vk_formats
= heap_calloc(format_count
, sizeof(*vk_formats
))))
753 return VK_FORMAT_UNDEFINED
;
755 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
,
756 vk_surface
, &format_count
, vk_formats
))) < 0)
758 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr
));
759 heap_free(vk_formats
);
760 return VK_FORMAT_UNDEFINED
;
763 for (i
= 0; i
< format_count
; ++i
)
765 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
768 if (i
== format_count
)
770 /* Try to create a swapchain with format conversion. */
771 vk_format
= get_swapchain_fallback_format(vk_format
);
772 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
773 for (i
= 0; i
< format_count
; ++i
)
775 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
779 heap_free(vk_formats
);
780 if (i
== format_count
)
782 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
783 return VK_FORMAT_UNDEFINED
;
786 TRACE("Using Vulkan swapchain format %#x.\n", vk_format
);
791 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk
*swapchain_vk
,
792 VkSwapchainKHR vk_swapchain
)
794 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
795 const struct wined3d_vk_info
*vk_info
;
796 VkSemaphoreCreateInfo semaphore_info
;
797 uint32_t image_count
, i
;
800 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
802 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
, vk_swapchain
, &image_count
, NULL
))) < 0)
804 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr
));
808 if (!(swapchain_vk
->vk_images
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_images
))))
810 ERR("Failed to allocate images array.\n");
814 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
,
815 vk_swapchain
, &image_count
, swapchain_vk
->vk_images
))) < 0)
817 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr
));
818 heap_free(swapchain_vk
->vk_images
);
822 if (!(swapchain_vk
->vk_semaphores
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_semaphores
))))
824 ERR("Failed to allocate semaphores array.\n");
825 heap_free(swapchain_vk
->vk_images
);
829 semaphore_info
.sType
= VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
;
830 semaphore_info
.pNext
= NULL
;
831 semaphore_info
.flags
= 0;
832 for (i
= 0; i
< image_count
; ++i
)
834 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
835 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].available
))) < 0)
837 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
841 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
842 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].presentable
))) < 0)
844 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
848 swapchain_vk
->image_count
= image_count
;
853 for (i
= 0; i
< image_count
; ++i
)
855 if (swapchain_vk
->vk_semaphores
[i
].available
)
856 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
857 if (swapchain_vk
->vk_semaphores
[i
].presentable
)
858 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
860 heap_free(swapchain_vk
->vk_semaphores
);
861 heap_free(swapchain_vk
->vk_images
);
865 static HRESULT
wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
867 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
868 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
869 VkSwapchainCreateInfoKHR vk_swapchain_desc
;
870 VkWin32SurfaceCreateInfoKHR surface_desc
;
871 unsigned int width
, height
, image_count
;
872 const struct wined3d_vk_info
*vk_info
;
873 VkSurfaceCapabilitiesKHR surface_caps
;
874 struct wined3d_adapter_vk
*adapter_vk
;
875 VkPresentModeKHR vk_present_mode
;
876 VkSwapchainKHR vk_swapchain
;
877 VkImageUsageFlags usage
;
878 VkSurfaceKHR vk_surface
;
884 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
885 vk_info
= &adapter_vk
->vk_info
;
887 surface_desc
.sType
= VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR
;
888 surface_desc
.pNext
= NULL
;
889 surface_desc
.flags
= 0;
890 surface_desc
.hinstance
= (HINSTANCE
)GetWindowLongPtrW(swapchain_vk
->s
.win_handle
, GWLP_HINSTANCE
);
891 surface_desc
.hwnd
= swapchain_vk
->s
.win_handle
;
892 if ((vr
= VK_CALL(vkCreateWin32SurfaceKHR(vk_info
->instance
, &surface_desc
, NULL
, &vk_surface
))) < 0)
894 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr
));
897 swapchain_vk
->vk_surface
= vk_surface
;
899 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk
->physical_device
,
900 device_vk
->vk_queue_family_index
, vk_surface
, &supported
))) < 0 || !supported
)
902 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr
));
906 if ((vk_format
= wined3d_swapchain_vk_select_vk_format(swapchain_vk
, vk_surface
)) == VK_FORMAT_UNDEFINED
)
908 ERR("Failed to select swapchain format.\n");
912 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk
->physical_device
,
913 swapchain_vk
->vk_surface
, &surface_caps
))) < 0)
915 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr
));
919 image_count
= desc
->backbuffer_count
;
920 if (image_count
< surface_caps
.minImageCount
)
921 image_count
= surface_caps
.minImageCount
;
922 else if (surface_caps
.maxImageCount
&& image_count
> surface_caps
.maxImageCount
)
923 image_count
= surface_caps
.maxImageCount
;
925 if (image_count
!= desc
->backbuffer_count
)
926 WARN("Image count %u is not supported (%u-%u).\n", desc
->backbuffer_count
,
927 surface_caps
.minImageCount
, surface_caps
.maxImageCount
);
929 GetClientRect(swapchain_vk
->s
.win_handle
, &client_rect
);
931 width
= client_rect
.right
- client_rect
.left
;
932 if (width
< surface_caps
.minImageExtent
.width
)
933 width
= surface_caps
.minImageExtent
.width
;
934 else if (width
> surface_caps
.maxImageExtent
.width
)
935 width
= surface_caps
.maxImageExtent
.width
;
937 height
= client_rect
.bottom
- client_rect
.top
;
938 if (height
< surface_caps
.minImageExtent
.height
)
939 height
= surface_caps
.minImageExtent
.height
;
940 else if (height
> surface_caps
.maxImageExtent
.height
)
941 height
= surface_caps
.maxImageExtent
.height
;
943 if (width
!= client_rect
.right
- client_rect
.left
|| height
!= client_rect
.bottom
- client_rect
.top
)
944 WARN("Swapchain dimensions %lux%lu are not supported (%u-%u x %u-%u).\n",
945 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
946 surface_caps
.minImageExtent
.width
, surface_caps
.maxImageExtent
.width
,
947 surface_caps
.minImageExtent
.height
, surface_caps
.maxImageExtent
.height
);
949 usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
950 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
951 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
952 if (!(usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) || !(usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
))
953 WARN("Transfer not supported for swapchain images.\n");
955 if (!(surface_caps
.supportedCompositeAlpha
& VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
))
957 FIXME("Unsupported alpha mode, %#x.\n", surface_caps
.supportedCompositeAlpha
);
961 vk_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
962 if (!swapchain_vk
->s
.swap_interval
)
964 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk
, VK_PRESENT_MODE_IMMEDIATE_KHR
))
965 vk_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
967 FIXME("Unsupported swap interval %u.\n", swapchain_vk
->s
.swap_interval
);
970 vk_swapchain_desc
.sType
= VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
;
971 vk_swapchain_desc
.pNext
= NULL
;
972 vk_swapchain_desc
.flags
= 0;
973 vk_swapchain_desc
.surface
= vk_surface
;
974 vk_swapchain_desc
.minImageCount
= image_count
;
975 vk_swapchain_desc
.imageFormat
= vk_format
;
976 vk_swapchain_desc
.imageColorSpace
= VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
;
977 vk_swapchain_desc
.imageExtent
.width
= width
;
978 vk_swapchain_desc
.imageExtent
.height
= height
;
979 vk_swapchain_desc
.imageArrayLayers
= 1;
980 vk_swapchain_desc
.imageUsage
= usage
;
981 vk_swapchain_desc
.imageSharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
982 vk_swapchain_desc
.queueFamilyIndexCount
= 0;
983 vk_swapchain_desc
.pQueueFamilyIndices
= NULL
;
984 vk_swapchain_desc
.preTransform
= surface_caps
.currentTransform
;
985 vk_swapchain_desc
.compositeAlpha
= VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
986 vk_swapchain_desc
.presentMode
= vk_present_mode
;
987 vk_swapchain_desc
.clipped
= VK_TRUE
;
988 vk_swapchain_desc
.oldSwapchain
= VK_NULL_HANDLE
;
989 if ((vr
= VK_CALL(vkCreateSwapchainKHR(device_vk
->vk_device
, &vk_swapchain_desc
, NULL
, &vk_swapchain
))) < 0)
991 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr
));
994 swapchain_vk
->vk_swapchain
= vk_swapchain
;
996 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk
, vk_swapchain
))
998 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, vk_swapchain
, NULL
));
1002 swapchain_vk
->width
= width
;
1003 swapchain_vk
->height
= height
;
1008 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, vk_surface
, NULL
));
1012 static HRESULT
wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk
*swapchain_vk
)
1014 TRACE("swapchain_vk %p.\n", swapchain_vk
);
1016 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk
);
1018 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
);
1021 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk
*swapchain_vk
,
1022 unsigned int swap_interval
)
1024 if (swap_interval
> 1)
1026 if (swap_interval
<= 4)
1027 FIXME("Unsupported swap interval %u.\n", swap_interval
);
1031 if (swapchain_vk
->s
.swap_interval
== swap_interval
)
1034 swapchain_vk
->s
.swap_interval
= swap_interval
;
1035 wined3d_swapchain_vk_recreate(swapchain_vk
);
1038 static VkResult
wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk
*swapchain_vk
,
1039 struct wined3d_context_vk
*context_vk
, const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
)
1041 struct wined3d_texture_vk
*back_buffer_vk
= wined3d_texture_vk(swapchain_vk
->s
.back_buffers
[0]);
1042 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
1043 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
1044 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1045 VkCommandBuffer vk_command_buffer
;
1046 VkImageSubresourceRange vk_range
;
1047 VkPresentInfoKHR present_desc
;
1048 unsigned int present_idx
;
1049 VkImageLayout vk_layout
;
1056 static const VkPipelineStageFlags wait_stage
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
;
1058 TRACE("swapchain_vk %p, context_vk %p, src_rect %s, dst_rect %s, swap_interval %u.\n",
1059 swapchain_vk
, context_vk
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
), swap_interval
);
1061 wined3d_swapchain_vk_set_swap_interval(swapchain_vk
, swap_interval
);
1063 present_idx
= swapchain_vk
->current
++ % swapchain_vk
->image_count
;
1064 wined3d_context_vk_wait_command_buffer(context_vk
, swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
);
1065 if ((vr
= VK_CALL(vkAcquireNextImageKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, UINT64_MAX
,
1066 swapchain_vk
->vk_semaphores
[present_idx
].available
, VK_NULL_HANDLE
, &image_idx
))) < 0)
1068 WARN("Failed to acquire image, vr %s.\n", wined3d_debug_vkresult(vr
));
1072 if (dst_rect
->right
> swapchain_vk
->width
|| dst_rect
->bottom
> swapchain_vk
->height
)
1074 dst_rect_tmp
= *dst_rect
;
1075 if (dst_rect
->right
> swapchain_vk
->width
)
1076 dst_rect_tmp
.right
= swapchain_vk
->width
;
1077 if (dst_rect
->bottom
> swapchain_vk
->height
)
1078 dst_rect_tmp
.bottom
= swapchain_vk
->height
;
1079 dst_rect
= &dst_rect_tmp
;
1081 filter
= src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
1082 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
1083 ? VK_FILTER_LINEAR
: VK_FILTER_NEAREST
;
1084 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
1086 wined3d_context_vk_end_current_render_pass(context_vk
);
1088 vk_range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1089 vk_range
.baseMipLevel
= 0;
1090 vk_range
.levelCount
= 1;
1091 vk_range
.baseArrayLayer
= 0;
1092 vk_range
.layerCount
= 1;
1094 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1095 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1096 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1097 VK_ACCESS_TRANSFER_READ_BIT
,
1098 back_buffer_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1099 back_buffer_vk
->image
.vk_image
, &vk_range
);
1101 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1102 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1103 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
1104 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1105 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1107 blit
.srcSubresource
.aspectMask
= vk_range
.aspectMask
;
1108 blit
.srcSubresource
.mipLevel
= vk_range
.baseMipLevel
;
1109 blit
.srcSubresource
.baseArrayLayer
= vk_range
.baseArrayLayer
;
1110 blit
.srcSubresource
.layerCount
= vk_range
.layerCount
;
1111 blit
.srcOffsets
[0].x
= src_rect
->left
;
1112 blit
.srcOffsets
[0].y
= src_rect
->top
;
1113 blit
.srcOffsets
[0].z
= 0;
1114 blit
.srcOffsets
[1].x
= src_rect
->right
;
1115 blit
.srcOffsets
[1].y
= src_rect
->bottom
;
1116 blit
.srcOffsets
[1].z
= 1;
1117 blit
.dstSubresource
= blit
.srcSubresource
;
1118 blit
.dstOffsets
[0].x
= dst_rect
->left
;
1119 blit
.dstOffsets
[0].y
= dst_rect
->top
;
1120 blit
.dstOffsets
[0].z
= 0;
1121 blit
.dstOffsets
[1].x
= dst_rect
->right
;
1122 blit
.dstOffsets
[1].y
= dst_rect
->bottom
;
1123 blit
.dstOffsets
[1].z
= 1;
1124 VK_CALL(vkCmdBlitImage(vk_command_buffer
,
1125 back_buffer_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1126 swapchain_vk
->vk_images
[image_idx
], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1129 wined3d_context_vk_reference_texture(context_vk
, back_buffer_vk
);
1130 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1131 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
1132 VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
1133 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
1134 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1136 if (desc
->swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
|| desc
->swap_effect
== WINED3D_SWAP_EFFECT_FLIP_DISCARD
)
1137 vk_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
1139 vk_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
1140 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1141 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1142 VK_ACCESS_TRANSFER_READ_BIT
,
1143 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1144 vk_layout
, back_buffer_vk
->layout
,
1145 back_buffer_vk
->image
.vk_image
, &vk_range
);
1146 back_buffer_vk
->bind_mask
= 0;
1148 swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
= context_vk
->current_command_buffer
.id
;
1149 wined3d_context_vk_submit_command_buffer(context_vk
,
1150 1, &swapchain_vk
->vk_semaphores
[present_idx
].available
, &wait_stage
,
1151 1, &swapchain_vk
->vk_semaphores
[present_idx
].presentable
);
1153 present_desc
.sType
= VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
;
1154 present_desc
.pNext
= NULL
;
1155 present_desc
.waitSemaphoreCount
= 1;
1156 present_desc
.pWaitSemaphores
= &swapchain_vk
->vk_semaphores
[present_idx
].presentable
;
1157 present_desc
.swapchainCount
= 1;
1158 present_desc
.pSwapchains
= &swapchain_vk
->vk_swapchain
;
1159 present_desc
.pImageIndices
= &image_idx
;
1160 present_desc
.pResults
= NULL
;
1161 if ((vr
= VK_CALL(vkQueuePresentKHR(device_vk
->vk_queue
, &present_desc
))))
1162 WARN("Present returned vr %s.\n", wined3d_debug_vkresult(vr
));
1166 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context_vk
*context_vk
)
1168 struct wined3d_texture_sub_resource
*sub_resource
;
1169 struct wined3d_texture_vk
*texture
, *texture_prev
;
1170 struct wined3d_image_vk image0
;
1171 VkDescriptorImageInfo vk_info0
;
1172 VkImageLayout vk_layout0
;
1173 uint32_t bind_mask0
;
1177 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
1179 if (swapchain
->state
.desc
.backbuffer_count
< 2)
1182 texture_prev
= wined3d_texture_vk(swapchain
->back_buffers
[0]);
1184 /* Back buffer 0 is already in the draw binding. */
1185 image0
= texture_prev
->image
;
1186 vk_layout0
= texture_prev
->layout
;
1187 bind_mask0
= texture_prev
->bind_mask
;
1188 vk_info0
= texture_prev
->default_image_info
;
1189 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
1191 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1193 texture
= wined3d_texture_vk(swapchain
->back_buffers
[i
]);
1194 sub_resource
= &texture
->t
.sub_resources
[0];
1196 if (!(sub_resource
->locations
& supported_locations
))
1197 wined3d_texture_load_location(&texture
->t
, 0, &context_vk
->c
, texture
->t
.resource
.draw_binding
);
1199 texture_prev
->image
= texture
->image
;
1200 texture_prev
->layout
= texture
->layout
;
1201 texture_prev
->bind_mask
= texture
->bind_mask
;
1202 texture_prev
->default_image_info
= texture
->default_image_info
;
1204 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
1205 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
1207 texture_prev
= texture
;
1210 texture_prev
->image
= image0
;
1211 texture_prev
->layout
= vk_layout0
;
1212 texture_prev
->bind_mask
= bind_mask0
;
1213 texture_prev
->default_image_info
= vk_info0
;
1215 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
1216 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
1218 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
1221 static void swapchain_vk_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect
,
1222 const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1224 struct wined3d_swapchain_vk
*swapchain_vk
= wined3d_swapchain_vk(swapchain
);
1225 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
1226 struct wined3d_context_vk
*context_vk
;
1230 context_vk
= wined3d_context_vk(context_acquire(swapchain
->device
, back_buffer
, 0));
1232 if (!swapchain_vk
->vk_swapchain
|| swapchain_present_is_partial_copy(swapchain
, dst_rect
))
1234 swapchain_blit_gdi(swapchain
, &context_vk
->c
, src_rect
, dst_rect
);
1238 wined3d_texture_load_location(back_buffer
, 0, &context_vk
->c
, back_buffer
->resource
.draw_binding
);
1240 if ((vr
= wined3d_swapchain_vk_blit(swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1242 if (vr
== VK_ERROR_OUT_OF_DATE_KHR
|| vr
== VK_SUBOPTIMAL_KHR
)
1244 if (FAILED(hr
= wined3d_swapchain_vk_recreate(swapchain_vk
)))
1245 ERR("Failed to recreate swapchain, hr %#lx.\n", hr
);
1246 else if (vr
== VK_ERROR_OUT_OF_DATE_KHR
&& (vr
= wined3d_swapchain_vk_blit(
1247 swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1248 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1252 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1257 wined3d_swapchain_vk_rotate(swapchain
, context_vk
);
1259 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1260 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1262 TRACE("Starting new frame.\n");
1264 context_release(&context_vk
->c
);
1267 static const struct wined3d_swapchain_ops swapchain_vk_ops
=
1269 swapchain_vk_present
,
1270 swapchain_frontbuffer_updated
,
1273 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
1275 struct wined3d_dc_info
*front
;
1276 POINT offset
= {0, 0};
1281 TRACE("swapchain %p.\n", swapchain
);
1283 front
= &swapchain
->front_buffer
->dc_info
[0];
1284 if (swapchain
->palette
)
1285 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
1287 if (swapchain
->front_buffer
->resource
.map_count
)
1288 ERR("Trying to blit a mapped surface.\n");
1290 TRACE("Copying surface %p to screen.\n", front
);
1293 window
= swapchain
->win_handle
;
1294 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
1296 /* Front buffer coordinates are screen coordinates. Map them to the
1297 * destination window if not fullscreened. */
1298 if (swapchain
->state
.desc
.windowed
)
1299 ClientToScreen(window
, &offset
);
1301 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
1303 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
1304 swapchain
->front_buffer
->resource
.height
);
1305 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
1307 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
1308 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
1309 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
1310 ReleaseDC(window
, dst_dc
);
1312 SetRectEmpty(&swapchain
->front_buffer_update
);
1315 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
1316 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1318 struct wined3d_dc_info
*front
, *back
;
1323 front
= &swapchain
->front_buffer
->dc_info
[0];
1324 back
= &swapchain
->back_buffers
[0]->dc_info
[0];
1326 /* Flip the surface data. */
1328 bitmap
= front
->bitmap
;
1329 data
= swapchain
->front_buffer
->resource
.heap_memory
;
1331 front
->dc
= back
->dc
;
1332 front
->bitmap
= back
->bitmap
;
1333 swapchain
->front_buffer
->resource
.heap_memory
= swapchain
->back_buffers
[0]->resource
.heap_memory
;
1336 back
->bitmap
= bitmap
;
1337 swapchain
->back_buffers
[0]->resource
.heap_memory
= data
;
1339 SetRect(&swapchain
->front_buffer_update
, 0, 0,
1340 swapchain
->front_buffer
->resource
.width
,
1341 swapchain
->front_buffer
->resource
.height
);
1342 swapchain_gdi_frontbuffer_updated(swapchain
);
1345 static const struct wined3d_swapchain_ops swapchain_no3d_ops
=
1347 swapchain_gdi_present
,
1348 swapchain_gdi_frontbuffer_updated
,
1351 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
1352 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, unsigned int *quality
)
1354 const struct wined3d_adapter
*adapter
;
1355 const struct wined3d_gl_info
*gl_info
;
1356 const struct wined3d_format
*format
;
1357 enum wined3d_multisample_type t
;
1359 if (wined3d_settings
.sample_count
== ~0u)
1362 adapter
= swapchain
->device
->adapter
;
1363 gl_info
= &adapter
->gl_info
;
1364 if (!(format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
)))
1367 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
1368 while (!(format
->multisample_types
& 1u << (t
- 1)))
1370 TRACE("Using sample count %u.\n", t
);
1375 void swapchain_set_max_frame_latency(struct wined3d_swapchain
*swapchain
, const struct wined3d_device
*device
)
1377 /* Subtract 1 for the implicit OpenGL latency. */
1378 swapchain
->max_frame_latency
= device
->max_frame_latency
>= 2 ? device
->max_frame_latency
- 1 : 1;
1381 static enum wined3d_format_id
adapter_format_from_backbuffer_format(const struct wined3d_adapter
*adapter
,
1382 enum wined3d_format_id format_id
)
1384 const struct wined3d_format
*backbuffer_format
;
1386 backbuffer_format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
);
1387 return pixelformat_for_depth(backbuffer_format
->byte_count
* CHAR_BIT
);
1390 static HRESULT
wined3d_swapchain_state_init(struct wined3d_swapchain_state
*state
,
1391 const struct wined3d_swapchain_desc
*desc
, HWND window
, struct wined3d
*wined3d
,
1392 struct wined3d_swapchain_state_parent
*parent
)
1396 state
->desc
= *desc
;
1398 if (FAILED(hr
= wined3d_output_get_display_mode(desc
->output
, &state
->original_mode
, NULL
)))
1400 ERR("Failed to get current display mode, hr %#lx.\n", hr
);
1404 if (!desc
->windowed
)
1406 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1408 state
->d3d_mode
.width
= desc
->backbuffer_width
;
1409 state
->d3d_mode
.height
= desc
->backbuffer_height
;
1410 state
->d3d_mode
.format_id
= adapter_format_from_backbuffer_format(desc
->output
->adapter
,
1411 desc
->backbuffer_format
);
1412 state
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1413 state
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1417 state
->d3d_mode
= state
->original_mode
;
1421 GetWindowRect(window
, &state
->original_window_rect
);
1422 state
->wined3d
= wined3d
;
1423 state
->device_window
= window
;
1424 state
->parent
= parent
;
1426 if (desc
->flags
& WINED3D_SWAPCHAIN_REGISTER_STATE
)
1427 wined3d_swapchain_state_register(state
);
1432 static HRESULT
wined3d_swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
1433 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1434 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1435 const struct wined3d_swapchain_ops
*swapchain_ops
)
1437 struct wined3d_resource_desc texture_desc
;
1438 struct wined3d_output_desc output_desc
;
1439 BOOL displaymode_set
= FALSE
;
1440 DWORD texture_flags
= 0;
1441 HRESULT hr
= E_FAIL
;
1446 wined3d_mutex_lock();
1448 if (desc
->backbuffer_count
> 1)
1450 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1451 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1454 if (desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
1455 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
1456 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
1457 FIXME("Unimplemented swap effect %#x.\n", desc
->swap_effect
);
1459 if (!desc
->device_window
)
1461 TRACE("Updating device_window to %p.\n", device
->create_parms
.focus_window
);
1462 desc
->device_window
= device
->create_parms
.focus_window
;
1464 window
= desc
->device_window
;
1466 if (FAILED(hr
= wined3d_swapchain_state_init(&swapchain
->state
, desc
, window
, device
->wined3d
, state_parent
)))
1468 ERR("Failed to initialise swapchain state, hr %#lx.\n", hr
);
1469 wined3d_mutex_unlock();
1473 swapchain
->swapchain_ops
= swapchain_ops
;
1474 swapchain
->device
= device
;
1475 swapchain
->parent
= parent
;
1476 swapchain
->parent_ops
= parent_ops
;
1478 swapchain
->win_handle
= window
;
1479 swapchain
->swap_interval
= WINED3D_SWAP_INTERVAL_DEFAULT
;
1480 swapchain_set_max_frame_latency(swapchain
, device
);
1482 GetClientRect(window
, &client_rect
);
1485 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect
));
1487 if (!desc
->backbuffer_width
)
1489 desc
->backbuffer_width
= client_rect
.right
? client_rect
.right
: 8;
1490 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
1492 if (!desc
->backbuffer_height
)
1494 desc
->backbuffer_height
= client_rect
.bottom
? client_rect
.bottom
: 8;
1495 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
1498 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
1500 desc
->backbuffer_format
= swapchain
->state
.original_mode
.format_id
;
1501 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->state
.original_mode
.format_id
));
1506 if (FAILED(hr
= wined3d_output_get_desc(desc
->output
, &output_desc
)))
1508 ERR("Failed to get output description, hr %#lx.\n", hr
);
1512 wined3d_swapchain_state_setup_fullscreen(&swapchain
->state
, window
,
1513 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, desc
->backbuffer_width
,
1514 desc
->backbuffer_height
);
1516 swapchain
->state
.desc
= *desc
;
1517 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->state
.desc
.backbuffer_format
,
1518 &swapchain
->state
.desc
.multisample_type
, &swapchain
->state
.desc
.multisample_quality
);
1520 TRACE("Creating front buffer.\n");
1522 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1523 texture_desc
.format
= swapchain
->state
.desc
.backbuffer_format
;
1524 texture_desc
.multisample_type
= swapchain
->state
.desc
.multisample_type
;
1525 texture_desc
.multisample_quality
= swapchain
->state
.desc
.multisample_quality
;
1526 texture_desc
.usage
= 0;
1527 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1528 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1529 texture_desc
.bind_flags
= 0;
1530 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1531 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1533 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1534 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
)
1535 texture_desc
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1536 texture_desc
.width
= swapchain
->state
.desc
.backbuffer_width
;
1537 texture_desc
.height
= swapchain
->state
.desc
.backbuffer_height
;
1538 texture_desc
.depth
= 1;
1539 texture_desc
.size
= 0;
1541 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
1542 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
1544 if (FAILED(hr
= wined3d_texture_create(device
, &texture_desc
, 1, 1, texture_flags
,
1545 NULL
, NULL
, &wined3d_null_parent_ops
, &swapchain
->front_buffer
)))
1547 WARN("Failed to create front buffer, hr %#lx.\n", hr
);
1551 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
1552 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1554 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1555 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1558 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1559 * so we should really check to see if there is a fullscreen swapchain
1560 * already. Does a single head count as full screen? */
1561 if (!desc
->windowed
&& desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1563 /* Change the display settings */
1564 if (FAILED(hr
= wined3d_output_set_display_mode(desc
->output
,
1565 &swapchain
->state
.d3d_mode
)))
1567 WARN("Failed to set display mode, hr %#lx.\n", hr
);
1570 displaymode_set
= TRUE
;
1573 if (swapchain
->state
.desc
.backbuffer_count
> 0)
1575 if (!(swapchain
->back_buffers
= heap_calloc(swapchain
->state
.desc
.backbuffer_count
,
1576 sizeof(*swapchain
->back_buffers
))))
1578 ERR("Failed to allocate backbuffer array memory.\n");
1583 texture_desc
.bind_flags
= swapchain
->state
.desc
.backbuffer_bind_flags
;
1584 texture_desc
.usage
= 0;
1585 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1586 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1587 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1589 TRACE("Creating back buffer %u.\n", i
);
1590 if (FAILED(hr
= wined3d_texture_create(device
, &texture_desc
, 1, 1, texture_flags
,
1591 NULL
, NULL
, &wined3d_null_parent_ops
, &swapchain
->back_buffers
[i
])))
1593 WARN("Failed to create back buffer %u, hr %#lx.\n", i
, hr
);
1594 swapchain
->state
.desc
.backbuffer_count
= i
;
1597 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1601 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1602 if (desc
->enable_auto_depth_stencil
)
1604 TRACE("Creating depth/stencil buffer.\n");
1605 if (!device
->auto_depth_stencil_view
)
1607 struct wined3d_view_desc desc
;
1608 struct wined3d_texture
*ds
;
1610 texture_desc
.format
= swapchain
->state
.desc
.auto_depth_stencil_format
;
1611 texture_desc
.usage
= 0;
1612 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
1613 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1614 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1616 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1618 if (FAILED(hr
= wined3d_texture_create(device
, &texture_desc
, 1, 1, 0,
1619 NULL
, NULL
, &wined3d_null_parent_ops
, &ds
)))
1621 WARN("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr
);
1625 desc
.format_id
= ds
->resource
.format
->id
;
1627 desc
.u
.texture
.level_idx
= 0;
1628 desc
.u
.texture
.level_count
= 1;
1629 desc
.u
.texture
.layer_idx
= 0;
1630 desc
.u
.texture
.layer_count
= 1;
1631 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1632 &device
->auto_depth_stencil_view
);
1633 wined3d_texture_decref(ds
);
1636 ERR("Failed to create rendertarget view, hr %#lx.\n", hr
);
1642 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1644 wined3d_mutex_unlock();
1649 if (displaymode_set
)
1651 if (FAILED(wined3d_restore_display_modes(device
->wined3d
)))
1652 ERR("Failed to restore display mode.\n");
1655 if (swapchain
->back_buffers
)
1657 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1659 if (swapchain
->back_buffers
[i
])
1661 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1662 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1665 heap_free(swapchain
->back_buffers
);
1668 if (swapchain
->front_buffer
)
1670 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1671 wined3d_texture_decref(swapchain
->front_buffer
);
1674 wined3d_swapchain_state_cleanup(&swapchain
->state
);
1675 wined3d_mutex_unlock();
1680 HRESULT
wined3d_swapchain_no3d_init(struct wined3d_swapchain
*swapchain_no3d
, struct wined3d_device
*device
,
1681 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1682 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1684 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1685 swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
);
1687 return wined3d_swapchain_init(swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
,
1688 &swapchain_no3d_ops
);
1691 HRESULT
wined3d_swapchain_gl_init(struct wined3d_swapchain_gl
*swapchain_gl
, struct wined3d_device
*device
,
1692 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1693 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1695 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1696 swapchain_gl
, device
, desc
, state_parent
, parent
, parent_ops
);
1698 return wined3d_swapchain_init(&swapchain_gl
->s
, device
, desc
, state_parent
, parent
,
1699 parent_ops
, &swapchain_gl_ops
);
1702 HRESULT
wined3d_swapchain_vk_init(struct wined3d_swapchain_vk
*swapchain_vk
, struct wined3d_device
*device
,
1703 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1704 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1708 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1709 swapchain_vk
, device
, desc
, parent
, parent_ops
);
1711 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_vk
->s
, device
, desc
, state_parent
, parent
,
1712 parent_ops
, &swapchain_vk_ops
)))
1715 if (swapchain_vk
->s
.win_handle
== GetDesktopWindow())
1717 WARN("Creating a desktop window swapchain.\n");
1721 if (FAILED(hr
= wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
)))
1722 WARN("Failed to create a Vulkan swapchain, hr %#lx.\n", hr
);
1727 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1728 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1729 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1730 struct wined3d_swapchain
**swapchain
)
1732 struct wined3d_swapchain
*object
;
1735 if (FAILED(hr
= device
->adapter
->adapter_ops
->adapter_create_swapchain(device
,
1736 desc
, state_parent
, parent
, parent_ops
, &object
)))
1739 if (desc
->flags
& WINED3D_SWAPCHAIN_IMPLICIT
)
1741 wined3d_mutex_lock();
1742 if (FAILED(hr
= wined3d_device_set_implicit_swapchain(device
, object
)))
1744 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1745 device
->adapter
->adapter_ops
->adapter_destroy_swapchain(object
);
1746 wined3d_mutex_unlock();
1749 wined3d_mutex_unlock();
1752 *swapchain
= object
;
1757 static struct wined3d_context_gl
*wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1759 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1760 struct wined3d_context_gl
*context_gl
;
1762 TRACE("Creating a new context for swapchain %p, thread %lu.\n", swapchain_gl
, GetCurrentThreadId());
1764 wined3d_from_cs(device
->cs
);
1766 if (!(context_gl
= heap_alloc_zero(sizeof(*context_gl
))))
1768 ERR("Failed to allocate context memory.\n");
1772 if (FAILED(wined3d_context_gl_init(context_gl
, swapchain_gl
)))
1774 WARN("Failed to initialise context.\n");
1775 heap_free(context_gl
);
1779 if (!device_context_add(device
, &context_gl
->c
))
1781 ERR("Failed to add the newly created context to the context list.\n");
1782 wined3d_context_gl_destroy(context_gl
);
1786 TRACE("Created context %p.\n", context_gl
);
1788 context_release(&context_gl
->c
);
1793 struct wined3d_context_gl
*wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1795 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1796 DWORD tid
= GetCurrentThreadId();
1799 for (i
= 0; i
< device
->context_count
; ++i
)
1801 if (wined3d_context_gl(device
->contexts
[i
])->tid
== tid
)
1802 return wined3d_context_gl(device
->contexts
[i
]);
1805 /* Create a new context for the thread. */
1806 return wined3d_swapchain_gl_create_context(swapchain_gl
);
1809 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1813 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1815 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1817 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1821 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1823 struct wined3d_device
*device
= swapchain
->device
;
1824 HWND window
= swapchain
->state
.device_window
;
1825 struct wined3d_output_desc output_desc
;
1826 unsigned int screensaver_active
;
1827 struct wined3d_output
*output
;
1828 BOOL focus_messages
, filter
;
1831 /* This code is not protected by the wined3d mutex, so it may run while
1832 * wined3d_device_reset is active. Testing on Windows shows that changing
1833 * focus during resets and resetting during focus change events causes
1834 * the application to crash with an invalid memory access. */
1836 if (!(focus_messages
= device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
))
1837 filter
= wined3d_filter_messages(window
, TRUE
);
1841 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
1842 if ((device
->restore_screensaver
= !!screensaver_active
))
1843 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
1845 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1847 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1848 * the window but leaves the size untouched, d3d9 sets the size on an
1849 * invisible window, generates messages but doesn't change the window
1850 * properties. The implementation follows d3d9.
1852 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1853 * resume drawing after a focus loss. */
1854 output
= wined3d_swapchain_get_output(swapchain
);
1857 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1861 if (SUCCEEDED(hr
= wined3d_output_get_desc(output
, &output_desc
)))
1862 SetWindowPos(window
, NULL
, output_desc
.desktop_rect
.left
,
1863 output_desc
.desktop_rect
.top
, swapchain
->state
.desc
.backbuffer_width
,
1864 swapchain
->state
.desc
.backbuffer_height
, SWP_NOACTIVATE
| SWP_NOZORDER
);
1866 ERR("Failed to get output description, hr %#lx.\n", hr
);
1869 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1871 output
= wined3d_swapchain_get_output(swapchain
);
1874 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1878 if (FAILED(hr
= wined3d_output_set_display_mode(output
,
1879 &swapchain
->state
.d3d_mode
)))
1880 ERR("Failed to set display mode, hr %#lx.\n", hr
);
1883 if (swapchain
== device
->swapchains
[0])
1884 device
->device_parent
->ops
->activate(device
->device_parent
, TRUE
);
1888 if (device
->restore_screensaver
)
1890 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
1891 device
->restore_screensaver
= FALSE
;
1894 if (FAILED(hr
= wined3d_restore_display_modes(device
->wined3d
)))
1895 ERR("Failed to restore display modes, hr %#lx.\n", hr
);
1897 swapchain
->reapply_mode
= TRUE
;
1899 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1900 * during window minimization. Do our housekeeping now, as the device may not exist after
1901 * the ShowWindow call.
1903 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1904 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1905 * we'll have to control this via a create flag. Note that the device and swapchain are not
1906 * safe to access after the ShowWindow call. */
1907 if (swapchain
== device
->swapchains
[0])
1908 device
->device_parent
->ops
->activate(device
->device_parent
, FALSE
);
1910 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
) && IsWindowVisible(window
))
1911 ShowWindow(window
, SW_MINIMIZE
);
1914 if (!focus_messages
)
1915 wined3d_filter_messages(window
, filter
);
1918 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1919 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1920 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1922 struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
1923 BOOL update_desc
= FALSE
;
1925 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1926 "multisample_type %#x, multisample_quality %#x.\n",
1927 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1928 multisample_type
, multisample_quality
);
1930 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1932 if (buffer_count
&& buffer_count
!= desc
->backbuffer_count
)
1933 FIXME("Cannot change the back buffer count yet.\n");
1935 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1937 if (!width
|| !height
)
1941 /* The application is requesting that either the swapchain width or
1942 * height be set to the corresponding dimension in the window's
1945 if (!GetClientRect(swapchain
->state
.device_window
, &client_rect
))
1947 ERR("Failed to get client rect, last error %#lx.\n", GetLastError());
1948 return WINED3DERR_INVALIDCALL
;
1952 width
= client_rect
.right
;
1955 height
= client_rect
.bottom
;
1958 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
1960 desc
->backbuffer_width
= width
;
1961 desc
->backbuffer_height
= height
;
1965 if (format_id
== WINED3DFMT_UNKNOWN
)
1967 if (!desc
->windowed
)
1968 return WINED3DERR_INVALIDCALL
;
1969 format_id
= swapchain
->state
.original_mode
.format_id
;
1972 if (format_id
!= desc
->backbuffer_format
)
1974 desc
->backbuffer_format
= format_id
;
1978 if (multisample_type
!= desc
->multisample_type
1979 || multisample_quality
!= desc
->multisample_quality
)
1981 desc
->multisample_type
= multisample_type
;
1982 desc
->multisample_quality
= multisample_quality
;
1991 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, 0, desc
->backbuffer_width
,
1992 desc
->backbuffer_height
, desc
->backbuffer_format
,
1993 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
1996 for (i
= 0; i
< desc
->backbuffer_count
; ++i
)
1998 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], 0, desc
->backbuffer_width
,
1999 desc
->backbuffer_height
, desc
->backbuffer_format
,
2000 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2005 swapchain_update_draw_bindings(swapchain
);
2010 static HRESULT
wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state
*state
,
2011 struct wined3d_output
*output
, struct wined3d_display_mode
*mode
)
2015 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
2017 if (FAILED(hr
= wined3d_output_find_closest_matching_mode(output
, mode
)))
2019 WARN("Failed to find closest matching mode, hr %#lx.\n", hr
);
2023 if (output
!= state
->desc
.output
)
2025 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2027 WARN("Failed to restore display modes, hr %#lx.\n", hr
);
2031 if (FAILED(hr
= wined3d_output_get_display_mode(output
, &state
->original_mode
, NULL
)))
2033 WARN("Failed to get current display mode, hr %#lx.\n", hr
);
2038 if (FAILED(hr
= wined3d_output_set_display_mode(output
, mode
)))
2040 WARN("Failed to set display mode, hr %#lx.\n", hr
);
2041 return WINED3DERR_INVALIDCALL
;
2047 HRESULT CDECL
wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state
*state
,
2048 const struct wined3d_display_mode
*mode
)
2050 struct wined3d_display_mode actual_mode
;
2051 struct wined3d_output_desc output_desc
;
2052 RECT original_window_rect
, window_rect
;
2053 int x
, y
, width
, height
;
2057 TRACE("state %p, mode %p.\n", state
, mode
);
2059 wined3d_mutex_lock();
2061 window
= state
->device_window
;
2063 if (state
->desc
.windowed
)
2065 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
2066 AdjustWindowRectEx(&window_rect
,
2067 GetWindowLongW(window
, GWL_STYLE
), FALSE
,
2068 GetWindowLongW(window
, GWL_EXSTYLE
));
2069 GetWindowRect(window
, &original_window_rect
);
2071 x
= original_window_rect
.left
;
2072 y
= original_window_rect
.top
;
2073 width
= window_rect
.right
- window_rect
.left
;
2074 height
= window_rect
.bottom
- window_rect
.top
;
2078 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2080 actual_mode
= *mode
;
2081 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, state
->desc
.output
,
2084 ERR("Failed to set display mode, hr %#lx.\n", hr
);
2085 wined3d_mutex_unlock();
2090 if (FAILED(hr
= wined3d_output_get_desc(state
->desc
.output
, &output_desc
)))
2092 ERR("Failed to get output description, hr %#lx.\n", hr
);
2093 wined3d_mutex_unlock();
2097 x
= output_desc
.desktop_rect
.left
;
2098 y
= output_desc
.desktop_rect
.top
;
2099 width
= output_desc
.desktop_rect
.right
- output_desc
.desktop_rect
.left
;
2100 height
= output_desc
.desktop_rect
.bottom
- output_desc
.desktop_rect
.top
;
2103 wined3d_mutex_unlock();
2105 MoveWindow(window
, x
, y
, width
, height
, TRUE
);
2110 static LONG
fullscreen_style(LONG style
)
2112 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2113 style
|= WS_POPUP
| WS_SYSMENU
;
2114 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
2119 static LONG
fullscreen_exstyle(LONG exstyle
)
2121 /* Filter out window decorations. */
2122 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
2127 struct wined3d_window_state
2130 HWND window_pos_after
;
2131 LONG style
, exstyle
;
2132 int x
, y
, width
, height
;
2137 static DWORD WINAPI
set_window_state_thread(void *ctx
)
2139 struct wined3d_window_state
*s
= ctx
;
2142 filter
= wined3d_filter_messages(s
->window
, TRUE
);
2146 SetWindowLongW(s
->window
, GWL_STYLE
, s
->style
);
2147 SetWindowLongW(s
->window
, GWL_EXSTYLE
, s
->exstyle
);
2149 SetWindowPos(s
->window
, s
->window_pos_after
, s
->x
, s
->y
, s
->width
, s
->height
, s
->flags
);
2151 wined3d_filter_messages(s
->window
, filter
);
2158 static void set_window_state(struct wined3d_window_state
*s
)
2160 DWORD window_tid
= GetWindowThreadProcessId(s
->window
, NULL
);
2161 DWORD tid
= GetCurrentThreadId();
2164 TRACE("Window %p belongs to thread %#lx.\n", s
->window
, window_tid
);
2165 /* If the window belongs to a different thread, modifying the style and/or
2166 * position can potentially deadlock if that thread isn't processing
2168 if (window_tid
== tid
)
2170 set_window_state_thread(s
);
2172 else if ((thread
= CreateThread(NULL
, 0, set_window_state_thread
, s
, 0, NULL
)))
2174 SetThreadDescription(thread
, L
"wined3d_set_window_state");
2175 CloseHandle(thread
);
2179 ERR("Failed to create thread.\n");
2183 HRESULT
wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state
*state
,
2184 HWND window
, int x
, int y
, int width
, int height
)
2186 struct wined3d_window_state
*s
;
2188 TRACE("Setting up window %p for fullscreen mode.\n", window
);
2190 if (!IsWindow(window
))
2192 WARN("%p is not a valid window.\n", window
);
2193 return WINED3DERR_NOTAVAILABLE
;
2196 if (!(s
= heap_alloc(sizeof(*s
))))
2197 return E_OUTOFMEMORY
;
2199 s
->window_pos_after
= HWND_TOPMOST
;
2205 if (state
->style
|| state
->exstyle
)
2207 ERR("Changing the window style for window %p, but another style (%08lx, %08lx) is already stored.\n",
2208 window
, state
->style
, state
->exstyle
);
2211 s
->flags
= SWP_FRAMECHANGED
| SWP_NOACTIVATE
;
2212 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
)
2213 s
->flags
|= SWP_NOZORDER
;
2215 s
->flags
|= SWP_SHOWWINDOW
;
2217 state
->style
= GetWindowLongW(window
, GWL_STYLE
);
2218 state
->exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2220 s
->style
= fullscreen_style(state
->style
);
2221 s
->exstyle
= fullscreen_exstyle(state
->exstyle
);
2222 s
->set_style
= true;
2224 TRACE("Old style was %08lx, %08lx, setting to %08lx, %08lx.\n",
2225 state
->style
, state
->exstyle
, s
->style
, s
->exstyle
);
2227 set_window_state(s
);
2231 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state
*state
,
2232 HWND window
, const RECT
*window_rect
)
2234 struct wined3d_window_state
*s
;
2235 LONG style
, exstyle
;
2237 if (!state
->style
&& !state
->exstyle
)
2240 if (!(s
= heap_alloc(sizeof(*s
))))
2244 s
->window_pos_after
= NULL
;
2245 s
->flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
2247 if ((state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE
)
2248 && !(state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
))
2250 s
->window_pos_after
= (state
->exstyle
& WS_EX_TOPMOST
) ? HWND_TOPMOST
: HWND_NOTOPMOST
;
2251 s
->flags
|= (state
->style
& WS_VISIBLE
) ? SWP_SHOWWINDOW
: SWP_HIDEWINDOW
;
2252 s
->flags
&= ~SWP_NOZORDER
;
2255 style
= GetWindowLongW(window
, GWL_STYLE
);
2256 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2258 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2259 * application, and we want to ignore them in the test below, since it's
2260 * not the application's fault that they changed. Additionally, we want to
2261 * preserve the current status of these flags (i.e. don't restore them) to
2262 * more closely emulate the behavior of Direct3D, which leaves these flags
2263 * alone when returning to windowed mode. */
2264 state
->style
^= (state
->style
^ style
) & WS_VISIBLE
;
2265 state
->exstyle
^= (state
->exstyle
^ exstyle
) & WS_EX_TOPMOST
;
2267 TRACE("Restoring window style of window %p to %08lx, %08lx.\n",
2268 window
, state
->style
, state
->exstyle
);
2270 s
->style
= state
->style
;
2271 s
->exstyle
= state
->exstyle
;
2272 /* Only restore the style if the application didn't modify it during the
2273 * fullscreen phase. Some applications change it before calling Reset()
2274 * when switching between windowed and fullscreen modes (HL2), some
2275 * depend on the original style (Eve Online). */
2276 s
->set_style
= style
== fullscreen_style(state
->style
) && exstyle
== fullscreen_exstyle(state
->exstyle
);
2280 s
->x
= window_rect
->left
;
2281 s
->y
= window_rect
->top
;
2282 s
->width
= window_rect
->right
- window_rect
->left
;
2283 s
->height
= window_rect
->bottom
- window_rect
->top
;
2287 s
->x
= s
->y
= s
->width
= s
->height
= 0;
2288 s
->flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
2291 set_window_state(s
);
2293 /* Delete the old values. */
2298 HRESULT CDECL
wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state
*state
,
2299 const struct wined3d_swapchain_desc
*swapchain_desc
,
2300 const struct wined3d_display_mode
*mode
)
2302 struct wined3d_display_mode actual_mode
;
2303 struct wined3d_output_desc output_desc
;
2304 BOOL windowed
= state
->desc
.windowed
;
2307 TRACE("state %p, swapchain_desc %p, mode %p.\n", state
, swapchain_desc
, mode
);
2309 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2313 actual_mode
= *mode
;
2314 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2320 if (!swapchain_desc
->windowed
)
2322 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
2323 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
2324 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
2325 actual_mode
.format_id
= adapter_format_from_backbuffer_format(swapchain_desc
->output
->adapter
,
2326 swapchain_desc
->backbuffer_format
);
2327 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
2328 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2334 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2336 WARN("Failed to restore display modes for all outputs, hr %#lx.\n", hr
);
2345 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2347 if (FAILED(hr
= wined3d_output_get_display_mode(swapchain_desc
->output
, &actual_mode
,
2350 ERR("Failed to get display mode, hr %#lx.\n", hr
);
2351 return WINED3DERR_INVALIDCALL
;
2355 if (!swapchain_desc
->windowed
)
2357 unsigned int width
= actual_mode
.width
;
2358 unsigned int height
= actual_mode
.height
;
2360 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
2362 ERR("Failed to get output description, hr %#lx.\n", hr
);
2366 if (state
->desc
.windowed
)
2368 /* Switch from windowed to fullscreen */
2369 if (FAILED(hr
= wined3d_swapchain_state_setup_fullscreen(state
, state
->device_window
,
2370 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
, height
)))
2375 HWND window
= state
->device_window
;
2378 /* Fullscreen -> fullscreen mode change */
2379 filter
= wined3d_filter_messages(window
, TRUE
);
2380 MoveWindow(window
, output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
,
2382 ShowWindow(window
, SW_SHOW
);
2383 wined3d_filter_messages(window
, filter
);
2385 state
->d3d_mode
= actual_mode
;
2387 else if (!state
->desc
.windowed
)
2389 /* Fullscreen -> windowed switch */
2390 RECT
*window_rect
= NULL
;
2391 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
2392 window_rect
= &state
->original_window_rect
;
2393 wined3d_swapchain_state_restore_from_fullscreen(state
, state
->device_window
, window_rect
);
2396 state
->desc
.output
= swapchain_desc
->output
;
2397 state
->desc
.windowed
= swapchain_desc
->windowed
;
2399 if (windowed
!= state
->desc
.windowed
)
2400 state
->parent
->ops
->windowed_state_changed(state
->parent
, state
->desc
.windowed
);
2405 BOOL CDECL
wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state
*state
)
2407 TRACE("state %p.\n", state
);
2409 return state
->desc
.windowed
;
2412 void CDECL
wined3d_swapchain_state_destroy(struct wined3d_swapchain_state
*state
)
2414 wined3d_swapchain_state_cleanup(state
);
2418 HRESULT CDECL
wined3d_swapchain_state_create(const struct wined3d_swapchain_desc
*desc
,
2419 HWND window
, struct wined3d
*wined3d
, struct wined3d_swapchain_state_parent
*state_parent
,
2420 struct wined3d_swapchain_state
**state
)
2422 struct wined3d_swapchain_state
*s
;
2425 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc
, window
, wined3d
, state
);
2427 if (!(s
= heap_alloc_zero(sizeof(*s
))))
2428 return E_OUTOFMEMORY
;
2430 if (FAILED(hr
= wined3d_swapchain_state_init(s
, desc
, window
, wined3d
, state_parent
)))