2 * Copyright 2002-2003 Jason Edmeades
3 * Copyright 2002-2003 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
24 #include "wined3d_gl.h"
25 #include "wined3d_vk.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
30 void wined3d_swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
35 TRACE("Destroying swapchain %p.\n", swapchain
);
37 wined3d_swapchain_state_cleanup(&swapchain
->state
);
38 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
40 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
41 * is the last buffer to be destroyed, FindContext() depends on that. */
42 if (swapchain
->front_buffer
)
44 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
45 if (wined3d_texture_decref(swapchain
->front_buffer
))
46 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
47 swapchain
->front_buffer
= NULL
;
50 if (swapchain
->back_buffers
)
52 i
= swapchain
->state
.desc
.backbuffer_count
;
56 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
57 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
58 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
60 free(swapchain
->back_buffers
);
61 swapchain
->back_buffers
= NULL
;
64 /* Restore the screen resolution if we rendered in fullscreen.
65 * This will restore the screen resolution to what it was before creating
66 * the swapchain. In case of d3d8 and d3d9 this will be the original
67 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
68 * sets the resolution before starting up Direct3D, thus orig_width and
69 * orig_height will be equal to the modes in the presentation params. */
70 if (!swapchain
->state
.desc
.windowed
)
72 if (swapchain
->state
.desc
.auto_restore_display_mode
)
74 if (FAILED(hr
= wined3d_restore_display_modes(swapchain
->device
->wined3d
)))
75 ERR("Failed to restore display mode, hr %#lx.\n", hr
);
77 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
79 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
,
80 swapchain
->state
.device_window
, &swapchain
->state
.original_window_rect
);
81 wined3d_device_release_focus_window(swapchain
->device
);
86 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
, swapchain
->state
.device_window
, NULL
);
91 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl
*swapchain_gl
)
93 wined3d_swapchain_cleanup(&swapchain_gl
->s
);
96 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
98 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
99 const struct wined3d_vk_info
*vk_info
;
103 TRACE("swapchain_vk %p.\n", swapchain_vk
);
105 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
107 if ((vr
= VK_CALL(vkQueueWaitIdle(device_vk
->vk_queue
))) < 0)
108 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr
));
109 free(swapchain_vk
->vk_images
);
110 for (i
= 0; i
< swapchain_vk
->image_count
; ++i
)
112 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
113 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
115 free(swapchain_vk
->vk_semaphores
);
116 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, NULL
));
117 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, swapchain_vk
->vk_surface
, NULL
));
120 static void wined3d_swapchain_vk_destroy_object(void *object
)
122 wined3d_swapchain_vk_destroy_vulkan_swapchain(object
);
125 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk
*swapchain_vk
)
127 struct wined3d_cs
*cs
= swapchain_vk
->s
.device
->cs
;
129 wined3d_cs_destroy_object(cs
, wined3d_swapchain_vk_destroy_object
, swapchain_vk
);
130 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
132 wined3d_swapchain_cleanup(&swapchain_vk
->s
);
135 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
137 unsigned int refcount
= InterlockedIncrement(&swapchain
->ref
);
139 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
144 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
146 unsigned int refcount
= InterlockedDecrement(&swapchain
->ref
);
148 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
152 struct wined3d_device
*device
;
154 wined3d_mutex_lock();
156 device
= swapchain
->device
;
157 if (device
->swapchain_count
&& device
->swapchains
[0] == swapchain
)
158 wined3d_device_uninit_3d(device
);
159 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
161 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
162 swapchain
->device
->adapter
->adapter_ops
->adapter_destroy_swapchain(swapchain
);
164 wined3d_mutex_unlock();
170 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
172 TRACE("swapchain %p.\n", swapchain
);
174 return swapchain
->parent
;
177 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
180 window
= swapchain
->state
.device_window
;
181 if (window
== swapchain
->win_handle
)
184 TRACE("Setting swapchain %p window from %p to %p.\n",
185 swapchain
, swapchain
->win_handle
, window
);
187 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
189 swapchain
->win_handle
= window
;
192 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
193 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
194 unsigned int swap_interval
, uint32_t flags
)
198 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
199 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
200 dst_window_override
, swap_interval
, flags
);
203 FIXME("Ignoring flags %#x.\n", flags
);
205 wined3d_mutex_lock();
207 if (!swapchain
->back_buffers
)
209 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
210 wined3d_mutex_unlock();
211 return WINED3DERR_INVALIDCALL
;
216 SetRect(&s
, 0, 0, swapchain
->state
.desc
.backbuffer_width
,
217 swapchain
->state
.desc
.backbuffer_height
);
223 GetClientRect(swapchain
->win_handle
, &d
);
227 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
228 dst_rect
, dst_window_override
, swap_interval
, flags
);
230 wined3d_mutex_unlock();
235 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
236 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
238 RECT src_rect
, dst_rect
;
240 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
242 SetRect(&src_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
, swapchain
->front_buffer
->resource
.height
);
245 if (swapchain
->state
.desc
.windowed
)
247 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
248 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
249 wine_dbgstr_rect(&dst_rect
));
252 return wined3d_device_context_blt(&swapchain
->device
->cs
->c
, dst_texture
, sub_resource_idx
, &dst_rect
,
253 swapchain
->front_buffer
, 0, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
256 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
257 UINT back_buffer_idx
)
259 TRACE("swapchain %p, back_buffer_idx %u.\n",
260 swapchain
, back_buffer_idx
);
262 /* Return invalid if there is no backbuffer array, otherwise it will
263 * crash when ddraw is used (there swapchain->back_buffers is always
264 * NULL). We need this because this function is called from
265 * stateblock_init_default_state() to get the default scissorrect
267 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->state
.desc
.backbuffer_count
)
269 WARN("Invalid back buffer index.\n");
270 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
271 * here in wined3d to avoid problems in other libs. */
275 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
277 return swapchain
->back_buffers
[back_buffer_idx
];
280 struct wined3d_texture
* CDECL
wined3d_swapchain_get_front_buffer(const struct wined3d_swapchain
*swapchain
)
282 TRACE("swapchain %p.\n", swapchain
);
284 return swapchain
->front_buffer
;
287 struct wined3d_output
* wined3d_swapchain_get_output(const struct wined3d_swapchain
*swapchain
)
289 TRACE("swapchain %p.\n", swapchain
);
291 return swapchain
->state
.desc
.output
;
294 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
295 struct wined3d_raster_status
*raster_status
)
297 struct wined3d_output
*output
;
299 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
301 output
= wined3d_swapchain_get_output(swapchain
);
304 ERR("Failed to get output from swapchain %p.\n", swapchain
);
308 return wined3d_output_get_raster_status(output
, raster_status
);
311 struct wined3d_swapchain_state
* CDECL
wined3d_swapchain_get_state(struct wined3d_swapchain
*swapchain
)
313 return &swapchain
->state
;
316 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
317 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
319 struct wined3d_output
*output
;
322 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
324 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
326 ERR("Failed to get output from swapchain %p.\n", swapchain
);
330 hr
= wined3d_output_get_display_mode(output
, mode
, rotation
);
332 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
333 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
338 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
340 TRACE("swapchain %p.\n", swapchain
);
342 return swapchain
->device
;
345 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
346 struct wined3d_swapchain_desc
*desc
)
348 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
350 *desc
= swapchain
->state
.desc
;
353 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
354 uint32_t flags
, const struct wined3d_gamma_ramp
*ramp
)
356 struct wined3d_output
*output
;
358 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
361 FIXME("Ignoring flags %#x.\n", flags
);
363 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
365 ERR("Failed to get output from swapchain %p.\n", swapchain
);
369 return wined3d_output_set_gamma_ramp(output
, ramp
);
372 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
374 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
376 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
378 swapchain
->palette
= palette
;
381 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
382 struct wined3d_gamma_ramp
*ramp
)
384 struct wined3d_output
*output
;
386 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
388 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
390 ERR("Failed to get output from swapchain %p.\n", swapchain
);
394 return wined3d_output_get_gamma_ramp(output
, ramp
);
397 /* The is a fallback for cases where we e.g. can't create a GL context or
398 * Vulkan swapchain for the swapchain window. */
399 static void swapchain_blit_gdi(struct wined3d_swapchain
*swapchain
,
400 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
402 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
403 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
404 D3DKMT_CREATEDCFROMMEMORY create_desc
;
405 const struct wined3d_format
*format
;
406 unsigned int row_pitch
, slice_pitch
;
411 static unsigned int once
;
413 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
414 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
417 FIXME("Using GDI present.\n");
419 format
= back_buffer
->resource
.format
;
420 if (!format
->ddi_format
)
422 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
426 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_SYSMEM
);
427 wined3d_texture_get_pitch(back_buffer
, 0, &row_pitch
, &slice_pitch
);
429 create_desc
.pMemory
= back_buffer
->resource
.heap_memory
;
430 create_desc
.Format
= format
->ddi_format
;
431 create_desc
.Width
= wined3d_texture_get_level_width(back_buffer
, 0);
432 create_desc
.Height
= wined3d_texture_get_level_height(back_buffer
, 0);
433 create_desc
.Pitch
= row_pitch
;
434 create_desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
435 create_desc
.pColorTable
= NULL
;
437 status
= D3DKMTCreateDCFromMemory(&create_desc
);
438 DeleteDC(create_desc
.hDeviceDc
);
441 WARN("Failed to create DC, status %#lx.\n", status
);
445 src_dc
= create_desc
.hDc
;
446 bitmap
= create_desc
.hBitmap
;
448 TRACE("Created source DC %p, bitmap %p for backbuffer %p.\n", src_dc
, bitmap
, back_buffer
);
450 if (!(dst_dc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
451 ERR("Failed to get destination DC.\n");
453 if (!StretchBlt(dst_dc
, dst_rect
->left
, dst_rect
->top
, dst_rect
->right
- dst_rect
->left
,
454 dst_rect
->bottom
- dst_rect
->top
, src_dc
, src_rect
->left
, src_rect
->top
,
455 src_rect
->right
- src_rect
->left
, src_rect
->bottom
- src_rect
->top
, SRCCOPY
))
456 ERR("Failed to blit.\n");
458 ReleaseDC(swapchain
->win_handle
, dst_dc
);
459 destroy_desc
.hDc
= src_dc
;
460 destroy_desc
.hBitmap
= bitmap
;
461 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
462 ERR("Failed to destroy src dc, status %#lx.\n", status
);
465 /* A GL context is provided by the caller */
466 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
467 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
469 struct wined3d_texture
*texture
= swapchain
->back_buffers
[0];
470 struct wined3d_device
*device
= swapchain
->device
;
471 enum wined3d_texture_filter_type filter
;
474 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
475 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
477 if ((src_rect
->right
- src_rect
->left
== dst_rect
->right
- dst_rect
->left
478 && src_rect
->bottom
- src_rect
->top
== dst_rect
->bottom
- dst_rect
->top
)
479 || is_complex_fixup(texture
->resource
.format
->color_fixup
))
480 filter
= WINED3D_TEXF_NONE
;
482 filter
= WINED3D_TEXF_LINEAR
;
484 location
= WINED3D_LOCATION_TEXTURE_RGB
;
485 if (texture
->resource
.multisample_type
)
486 location
= WINED3D_LOCATION_RB_RESOLVED
;
488 wined3d_texture_validate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
489 device
->blitter
->ops
->blitter_blit(device
->blitter
, WINED3D_BLIT_OP_COLOR_BLIT
, context
, texture
, 0,
490 location
, src_rect
, texture
, 0, WINED3D_LOCATION_DRAWABLE
, dst_rect
, NULL
, filter
, NULL
);
491 wined3d_texture_invalidate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
494 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain
*swapchain
,
495 struct wined3d_context_gl
*context_gl
, unsigned int swap_interval
)
497 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
499 swap_interval
= swap_interval
<= 4 ? swap_interval
: 1;
500 if (swapchain
->swap_interval
== swap_interval
)
503 swapchain
->swap_interval
= swap_interval
;
505 if (!gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
508 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
510 ERR("Failed to set swap interval %u for context %p, last error %#lx.\n",
511 swap_interval
, context_gl
, GetLastError());
515 /* Context activation is done by the caller. */
516 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
518 struct wined3d_texture_sub_resource
*sub_resource
;
519 struct wined3d_texture_gl
*texture
, *texture_prev
;
520 struct gl_texture tex0
;
524 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
526 if (swapchain
->state
.desc
.backbuffer_count
< 2)
529 texture_prev
= wined3d_texture_gl(swapchain
->back_buffers
[0]);
531 /* Back buffer 0 is already in the draw binding. */
532 tex0
= texture_prev
->texture_rgb
;
533 rb0
= texture_prev
->rb_multisample
;
534 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
536 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
538 texture
= wined3d_texture_gl(swapchain
->back_buffers
[i
]);
539 sub_resource
= &texture
->t
.sub_resources
[0];
541 if (!(sub_resource
->locations
& supported_locations
))
542 wined3d_texture_load_location(&texture
->t
, 0, context
, texture
->t
.resource
.draw_binding
);
544 texture_prev
->texture_rgb
= texture
->texture_rgb
;
545 texture_prev
->rb_multisample
= texture
->rb_multisample
;
547 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
548 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
550 texture_prev
= texture
;
553 texture_prev
->texture_rgb
= tex0
;
554 texture_prev
->rb_multisample
= rb0
;
556 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
557 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
559 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
562 static bool swapchain_present_is_partial_copy(struct wined3d_swapchain
*swapchain
, const RECT
*dst_rect
)
564 enum wined3d_swap_effect swap_effect
= swapchain
->state
.desc
.swap_effect
;
568 if (swap_effect
!= WINED3D_SWAP_EFFECT_COPY
&& swap_effect
!= WINED3D_SWAP_EFFECT_COPY_VSYNC
)
571 GetClientRect(swapchain
->win_handle
, &client_rect
);
573 t
= client_rect
.right
- client_rect
.left
;
574 if ((dst_rect
->left
&& dst_rect
->right
) || abs(dst_rect
->right
- dst_rect
->left
) != t
)
576 t
= client_rect
.bottom
- client_rect
.top
;
577 if ((dst_rect
->top
&& dst_rect
->bottom
) || abs(dst_rect
->bottom
- dst_rect
->top
) != t
)
583 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
584 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
586 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
587 const struct wined3d_pixel_format
*pixel_format
;
588 const struct wined3d_gl_info
*gl_info
;
589 struct wined3d_context_gl
*context_gl
;
590 struct wined3d_context
*context
;
592 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
, 0);
593 context_gl
= wined3d_context_gl(context
);
594 if (!context_gl
->valid
)
596 context_release(context
);
597 WARN("Invalid context, skipping present.\n");
601 TRACE("Presenting DC %p.\n", context_gl
->dc
);
603 pixel_format
= &wined3d_adapter_gl(swapchain
->device
->adapter
)->pixel_formats
[context_gl
->pixel_format
];
604 if (context_gl
->dc
== wined3d_device_gl(swapchain
->device
)->backup_dc
605 || (pixel_format
->swap_method
!= WGL_SWAP_COPY_ARB
606 && swapchain_present_is_partial_copy(swapchain
, dst_rect
)))
608 swapchain_blit_gdi(swapchain
, context
, src_rect
, dst_rect
);
612 gl_info
= context_gl
->gl_info
;
614 swapchain_gl_set_swap_interval(swapchain
, context_gl
, swap_interval
);
616 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
618 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
620 if (swapchain
->device
->context_count
> 1)
622 WARN_(d3d_perf
)("Multiple contexts, calling glFinish() to enforce ordering.\n");
623 gl_info
->gl_ops
.gl
.p_glFinish();
626 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
627 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context_gl
->dc
);
630 if (context
->d3d_info
->fences
)
631 wined3d_context_gl_submit_command_fence(context_gl
);
633 wined3d_swapchain_gl_rotate(swapchain
, context
);
635 TRACE("SwapBuffers called, Starting new frame\n");
637 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
638 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
640 context_release(context
);
643 static void swapchain_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
645 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
646 struct wined3d_context
*context
;
648 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
649 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
650 context_release(context
);
651 SetRectEmpty(&swapchain
->front_buffer_update
);
654 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
656 swapchain_gl_present
,
657 swapchain_frontbuffer_updated
,
660 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk
*swapchain_vk
,
661 VkPresentModeKHR vk_present_mode
)
663 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
664 const struct wined3d_vk_info
*vk_info
;
665 struct wined3d_adapter_vk
*adapter_vk
;
666 VkPhysicalDevice vk_physical_device
;
667 VkPresentModeKHR
*vk_modes
;
668 bool supported
= false;
672 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
673 vk_physical_device
= adapter_vk
->physical_device
;
674 vk_info
= &adapter_vk
->vk_info
;
676 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
677 swapchain_vk
->vk_surface
, &count
, NULL
))) < 0)
679 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr
));
683 if (!(vk_modes
= calloc(count
, sizeof(*vk_modes
))))
686 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
687 swapchain_vk
->vk_surface
, &count
, vk_modes
))) < 0)
689 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr
));
693 for (i
= 0; i
< count
; ++i
)
695 if (vk_modes
[i
] == vk_present_mode
)
707 static VkFormat
get_swapchain_fallback_format(VkFormat vk_format
)
711 case VK_FORMAT_R8G8B8A8_SRGB
:
712 return VK_FORMAT_B8G8R8A8_SRGB
;
713 case VK_FORMAT_R8G8B8A8_UNORM
:
714 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
715 case VK_FORMAT_R16G16B16A16_SFLOAT
:
716 return VK_FORMAT_B8G8R8A8_UNORM
;
718 WARN("Unhandled format %#x.\n", vk_format
);
719 return VK_FORMAT_UNDEFINED
;
723 static VkFormat
wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk
*swapchain_vk
,
724 VkSurfaceKHR vk_surface
)
726 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
727 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
728 const struct wined3d_vk_info
*vk_info
;
729 struct wined3d_adapter_vk
*adapter_vk
;
730 const struct wined3d_format
*format
;
731 VkPhysicalDevice vk_physical_device
;
732 VkSurfaceFormatKHR
*vk_formats
;
733 uint32_t format_count
, i
;
737 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
738 vk_physical_device
= adapter_vk
->physical_device
;
739 vk_info
= &adapter_vk
->vk_info
;
741 if ((format
= wined3d_get_format(&adapter_vk
->a
, desc
->backbuffer_format
, WINED3D_BIND_RENDER_TARGET
)))
742 vk_format
= wined3d_format_vk(format
)->vk_format
;
744 vk_format
= VK_FORMAT_B8G8R8A8_UNORM
;
746 vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
, vk_surface
, &format_count
, NULL
));
747 if (vr
< 0 || !format_count
)
749 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr
));
750 return VK_FORMAT_UNDEFINED
;
753 if (!(vk_formats
= calloc(format_count
, sizeof(*vk_formats
))))
754 return VK_FORMAT_UNDEFINED
;
756 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
,
757 vk_surface
, &format_count
, vk_formats
))) < 0)
759 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr
));
761 return VK_FORMAT_UNDEFINED
;
764 for (i
= 0; i
< format_count
; ++i
)
766 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
769 if (i
== format_count
)
771 /* Try to create a swapchain with format conversion. */
772 vk_format
= get_swapchain_fallback_format(vk_format
);
773 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
774 for (i
= 0; i
< format_count
; ++i
)
776 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
781 if (i
== format_count
)
783 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
784 return VK_FORMAT_UNDEFINED
;
787 TRACE("Using Vulkan swapchain format %#x.\n", vk_format
);
792 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk
*swapchain_vk
,
793 VkSwapchainKHR vk_swapchain
)
795 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
796 const struct wined3d_vk_info
*vk_info
;
797 VkSemaphoreCreateInfo semaphore_info
;
798 uint32_t image_count
, i
;
801 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
803 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
, vk_swapchain
, &image_count
, NULL
))) < 0)
805 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr
));
809 if (!(swapchain_vk
->vk_images
= calloc(image_count
, sizeof(*swapchain_vk
->vk_images
))))
811 ERR("Failed to allocate images array.\n");
815 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
,
816 vk_swapchain
, &image_count
, swapchain_vk
->vk_images
))) < 0)
818 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr
));
819 free(swapchain_vk
->vk_images
);
823 if (!(swapchain_vk
->vk_semaphores
= calloc(image_count
, sizeof(*swapchain_vk
->vk_semaphores
))))
825 ERR("Failed to allocate semaphores array.\n");
826 free(swapchain_vk
->vk_images
);
830 semaphore_info
.sType
= VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
;
831 semaphore_info
.pNext
= NULL
;
832 semaphore_info
.flags
= 0;
833 for (i
= 0; i
< image_count
; ++i
)
835 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
836 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].available
))) < 0)
838 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
842 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
843 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].presentable
))) < 0)
845 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
849 swapchain_vk
->image_count
= image_count
;
854 for (i
= 0; i
< image_count
; ++i
)
856 if (swapchain_vk
->vk_semaphores
[i
].available
)
857 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
858 if (swapchain_vk
->vk_semaphores
[i
].presentable
)
859 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
861 free(swapchain_vk
->vk_semaphores
);
862 free(swapchain_vk
->vk_images
);
866 static HRESULT
wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
868 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
869 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
870 VkSwapchainCreateInfoKHR vk_swapchain_desc
;
871 VkWin32SurfaceCreateInfoKHR surface_desc
;
872 unsigned int width
, height
, image_count
;
873 const struct wined3d_vk_info
*vk_info
;
874 VkSurfaceCapabilitiesKHR surface_caps
;
875 struct wined3d_adapter_vk
*adapter_vk
;
876 VkPresentModeKHR vk_present_mode
;
877 VkSwapchainKHR vk_swapchain
;
878 VkImageUsageFlags usage
;
879 VkSurfaceKHR vk_surface
;
885 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
886 vk_info
= &adapter_vk
->vk_info
;
888 surface_desc
.sType
= VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR
;
889 surface_desc
.pNext
= NULL
;
890 surface_desc
.flags
= 0;
891 surface_desc
.hinstance
= (HINSTANCE
)GetWindowLongPtrW(swapchain_vk
->s
.win_handle
, GWLP_HINSTANCE
);
892 surface_desc
.hwnd
= swapchain_vk
->s
.win_handle
;
893 if ((vr
= VK_CALL(vkCreateWin32SurfaceKHR(vk_info
->instance
, &surface_desc
, NULL
, &vk_surface
))) < 0)
895 ERR("Failed to create Vulkan surface, vr %s.\n", wined3d_debug_vkresult(vr
));
898 swapchain_vk
->vk_surface
= vk_surface
;
900 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR(adapter_vk
->physical_device
,
901 device_vk
->vk_queue_family_index
, vk_surface
, &supported
))) < 0 || !supported
)
903 ERR("Queue family does not support presentation on this surface, vr %s.\n", wined3d_debug_vkresult(vr
));
907 if ((vk_format
= wined3d_swapchain_vk_select_vk_format(swapchain_vk
, vk_surface
)) == VK_FORMAT_UNDEFINED
)
909 ERR("Failed to select swapchain format.\n");
913 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(adapter_vk
->physical_device
,
914 swapchain_vk
->vk_surface
, &surface_caps
))) < 0)
916 ERR("Failed to get surface capabilities, vr %s.\n", wined3d_debug_vkresult(vr
));
920 image_count
= desc
->backbuffer_count
;
921 if (image_count
< surface_caps
.minImageCount
)
922 image_count
= surface_caps
.minImageCount
;
923 else if (surface_caps
.maxImageCount
&& image_count
> surface_caps
.maxImageCount
)
924 image_count
= surface_caps
.maxImageCount
;
926 if (image_count
!= desc
->backbuffer_count
)
927 WARN("Image count %u is not supported (%u-%u).\n", desc
->backbuffer_count
,
928 surface_caps
.minImageCount
, surface_caps
.maxImageCount
);
930 GetClientRect(swapchain_vk
->s
.win_handle
, &client_rect
);
932 width
= client_rect
.right
- client_rect
.left
;
933 if (width
< surface_caps
.minImageExtent
.width
)
934 width
= surface_caps
.minImageExtent
.width
;
935 else if (width
> surface_caps
.maxImageExtent
.width
)
936 width
= surface_caps
.maxImageExtent
.width
;
938 height
= client_rect
.bottom
- client_rect
.top
;
939 if (height
< surface_caps
.minImageExtent
.height
)
940 height
= surface_caps
.minImageExtent
.height
;
941 else if (height
> surface_caps
.maxImageExtent
.height
)
942 height
= surface_caps
.maxImageExtent
.height
;
944 if (width
!= client_rect
.right
- client_rect
.left
|| height
!= client_rect
.bottom
- client_rect
.top
)
945 WARN("Swapchain dimensions %lux%lu are not supported (%u-%u x %u-%u).\n",
946 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
947 surface_caps
.minImageExtent
.width
, surface_caps
.maxImageExtent
.width
,
948 surface_caps
.minImageExtent
.height
, surface_caps
.maxImageExtent
.height
);
950 usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
951 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
952 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
953 if (!(usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) || !(usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
))
954 WARN("Transfer not supported for swapchain images.\n");
956 if (!(surface_caps
.supportedCompositeAlpha
& VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
))
958 FIXME("Unsupported alpha mode, %#x.\n", surface_caps
.supportedCompositeAlpha
);
962 vk_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
963 if (!swapchain_vk
->s
.swap_interval
)
965 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk
, VK_PRESENT_MODE_IMMEDIATE_KHR
))
966 vk_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
968 FIXME("Unsupported swap interval %u.\n", swapchain_vk
->s
.swap_interval
);
971 vk_swapchain_desc
.sType
= VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
;
972 vk_swapchain_desc
.pNext
= NULL
;
973 vk_swapchain_desc
.flags
= 0;
974 vk_swapchain_desc
.surface
= vk_surface
;
975 vk_swapchain_desc
.minImageCount
= image_count
;
976 vk_swapchain_desc
.imageFormat
= vk_format
;
977 vk_swapchain_desc
.imageColorSpace
= VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
;
978 vk_swapchain_desc
.imageExtent
.width
= width
;
979 vk_swapchain_desc
.imageExtent
.height
= height
;
980 vk_swapchain_desc
.imageArrayLayers
= 1;
981 vk_swapchain_desc
.imageUsage
= usage
;
982 vk_swapchain_desc
.imageSharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
983 vk_swapchain_desc
.queueFamilyIndexCount
= 0;
984 vk_swapchain_desc
.pQueueFamilyIndices
= NULL
;
985 vk_swapchain_desc
.preTransform
= surface_caps
.currentTransform
;
986 vk_swapchain_desc
.compositeAlpha
= VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
987 vk_swapchain_desc
.presentMode
= vk_present_mode
;
988 vk_swapchain_desc
.clipped
= VK_TRUE
;
989 vk_swapchain_desc
.oldSwapchain
= VK_NULL_HANDLE
;
990 if ((vr
= VK_CALL(vkCreateSwapchainKHR(device_vk
->vk_device
, &vk_swapchain_desc
, NULL
, &vk_swapchain
))) < 0)
992 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr
));
995 swapchain_vk
->vk_swapchain
= vk_swapchain
;
997 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk
, vk_swapchain
))
999 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, vk_swapchain
, NULL
));
1003 swapchain_vk
->width
= width
;
1004 swapchain_vk
->height
= height
;
1009 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, vk_surface
, NULL
));
1013 static HRESULT
wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk
*swapchain_vk
)
1015 TRACE("swapchain_vk %p.\n", swapchain_vk
);
1017 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk
);
1019 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
);
1022 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk
*swapchain_vk
,
1023 unsigned int swap_interval
)
1025 if (swap_interval
> 1)
1027 if (swap_interval
<= 4)
1028 FIXME("Unsupported swap interval %u.\n", swap_interval
);
1032 if (swapchain_vk
->s
.swap_interval
== swap_interval
)
1035 swapchain_vk
->s
.swap_interval
= swap_interval
;
1036 wined3d_swapchain_vk_recreate(swapchain_vk
);
1039 static VkResult
wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk
*swapchain_vk
,
1040 struct wined3d_context_vk
*context_vk
, const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
)
1042 struct wined3d_texture_vk
*back_buffer_vk
= wined3d_texture_vk(swapchain_vk
->s
.back_buffers
[0]);
1043 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
1044 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
1045 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1046 VkCommandBuffer vk_command_buffer
;
1047 VkImageSubresourceRange vk_range
;
1048 VkPresentInfoKHR present_desc
;
1049 unsigned int present_idx
;
1050 VkImageLayout vk_layout
;
1057 static const VkPipelineStageFlags wait_stage
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
;
1059 TRACE("swapchain_vk %p, context_vk %p, src_rect %s, dst_rect %s, swap_interval %u.\n",
1060 swapchain_vk
, context_vk
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
), swap_interval
);
1062 wined3d_swapchain_vk_set_swap_interval(swapchain_vk
, swap_interval
);
1064 present_idx
= swapchain_vk
->current
++ % swapchain_vk
->image_count
;
1065 wined3d_context_vk_wait_command_buffer(context_vk
, swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
);
1066 if ((vr
= VK_CALL(vkAcquireNextImageKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, UINT64_MAX
,
1067 swapchain_vk
->vk_semaphores
[present_idx
].available
, VK_NULL_HANDLE
, &image_idx
))) < 0)
1069 WARN("Failed to acquire image, vr %s.\n", wined3d_debug_vkresult(vr
));
1073 if (dst_rect
->right
> swapchain_vk
->width
|| dst_rect
->bottom
> swapchain_vk
->height
)
1075 dst_rect_tmp
= *dst_rect
;
1076 if (dst_rect
->right
> swapchain_vk
->width
)
1077 dst_rect_tmp
.right
= swapchain_vk
->width
;
1078 if (dst_rect
->bottom
> swapchain_vk
->height
)
1079 dst_rect_tmp
.bottom
= swapchain_vk
->height
;
1080 dst_rect
= &dst_rect_tmp
;
1082 filter
= src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
1083 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
1084 ? VK_FILTER_LINEAR
: VK_FILTER_NEAREST
;
1085 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
1087 wined3d_context_vk_end_current_render_pass(context_vk
);
1089 vk_range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1090 vk_range
.baseMipLevel
= 0;
1091 vk_range
.levelCount
= 1;
1092 vk_range
.baseArrayLayer
= 0;
1093 vk_range
.layerCount
= 1;
1095 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1096 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1097 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1098 VK_ACCESS_TRANSFER_READ_BIT
,
1099 back_buffer_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1100 back_buffer_vk
->image
.vk_image
, &vk_range
);
1102 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1103 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1104 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
1105 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1106 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1108 blit
.srcSubresource
.aspectMask
= vk_range
.aspectMask
;
1109 blit
.srcSubresource
.mipLevel
= vk_range
.baseMipLevel
;
1110 blit
.srcSubresource
.baseArrayLayer
= vk_range
.baseArrayLayer
;
1111 blit
.srcSubresource
.layerCount
= vk_range
.layerCount
;
1112 blit
.srcOffsets
[0].x
= src_rect
->left
;
1113 blit
.srcOffsets
[0].y
= src_rect
->top
;
1114 blit
.srcOffsets
[0].z
= 0;
1115 blit
.srcOffsets
[1].x
= src_rect
->right
;
1116 blit
.srcOffsets
[1].y
= src_rect
->bottom
;
1117 blit
.srcOffsets
[1].z
= 1;
1118 blit
.dstSubresource
= blit
.srcSubresource
;
1119 blit
.dstOffsets
[0].x
= dst_rect
->left
;
1120 blit
.dstOffsets
[0].y
= dst_rect
->top
;
1121 blit
.dstOffsets
[0].z
= 0;
1122 blit
.dstOffsets
[1].x
= dst_rect
->right
;
1123 blit
.dstOffsets
[1].y
= dst_rect
->bottom
;
1124 blit
.dstOffsets
[1].z
= 1;
1125 VK_CALL(vkCmdBlitImage(vk_command_buffer
,
1126 back_buffer_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1127 swapchain_vk
->vk_images
[image_idx
], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1130 wined3d_context_vk_reference_texture(context_vk
, back_buffer_vk
);
1131 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1132 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
1133 VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
1134 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
1135 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1137 if (desc
->swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
|| desc
->swap_effect
== WINED3D_SWAP_EFFECT_FLIP_DISCARD
)
1138 vk_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
1140 vk_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
1141 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1142 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1143 VK_ACCESS_TRANSFER_READ_BIT
,
1144 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1145 vk_layout
, back_buffer_vk
->layout
,
1146 back_buffer_vk
->image
.vk_image
, &vk_range
);
1147 back_buffer_vk
->bind_mask
= 0;
1149 swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
= context_vk
->current_command_buffer
.id
;
1150 wined3d_context_vk_submit_command_buffer(context_vk
,
1151 1, &swapchain_vk
->vk_semaphores
[present_idx
].available
, &wait_stage
,
1152 1, &swapchain_vk
->vk_semaphores
[present_idx
].presentable
);
1154 present_desc
.sType
= VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
;
1155 present_desc
.pNext
= NULL
;
1156 present_desc
.waitSemaphoreCount
= 1;
1157 present_desc
.pWaitSemaphores
= &swapchain_vk
->vk_semaphores
[present_idx
].presentable
;
1158 present_desc
.swapchainCount
= 1;
1159 present_desc
.pSwapchains
= &swapchain_vk
->vk_swapchain
;
1160 present_desc
.pImageIndices
= &image_idx
;
1161 present_desc
.pResults
= NULL
;
1162 if ((vr
= VK_CALL(vkQueuePresentKHR(device_vk
->vk_queue
, &present_desc
))))
1163 WARN("Present returned vr %s.\n", wined3d_debug_vkresult(vr
));
1167 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context_vk
*context_vk
)
1169 struct wined3d_texture_sub_resource
*sub_resource
;
1170 struct wined3d_texture_vk
*texture
, *texture_prev
;
1171 struct wined3d_image_vk image0
;
1172 VkDescriptorImageInfo vk_info0
;
1173 VkImageLayout vk_layout0
;
1174 uint32_t bind_mask0
;
1178 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
1180 if (swapchain
->state
.desc
.backbuffer_count
< 2)
1183 texture_prev
= wined3d_texture_vk(swapchain
->back_buffers
[0]);
1185 /* Back buffer 0 is already in the draw binding. */
1186 image0
= texture_prev
->image
;
1187 vk_layout0
= texture_prev
->layout
;
1188 bind_mask0
= texture_prev
->bind_mask
;
1189 vk_info0
= texture_prev
->default_image_info
;
1190 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
1192 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1194 texture
= wined3d_texture_vk(swapchain
->back_buffers
[i
]);
1195 sub_resource
= &texture
->t
.sub_resources
[0];
1197 if (!(sub_resource
->locations
& supported_locations
))
1198 wined3d_texture_load_location(&texture
->t
, 0, &context_vk
->c
, texture
->t
.resource
.draw_binding
);
1200 texture_prev
->image
= texture
->image
;
1201 texture_prev
->layout
= texture
->layout
;
1202 texture_prev
->bind_mask
= texture
->bind_mask
;
1203 texture_prev
->default_image_info
= texture
->default_image_info
;
1205 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
1206 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
1208 texture_prev
= texture
;
1211 texture_prev
->image
= image0
;
1212 texture_prev
->layout
= vk_layout0
;
1213 texture_prev
->bind_mask
= bind_mask0
;
1214 texture_prev
->default_image_info
= vk_info0
;
1216 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
1217 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
1219 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
1222 static void swapchain_vk_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect
,
1223 const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1225 struct wined3d_swapchain_vk
*swapchain_vk
= wined3d_swapchain_vk(swapchain
);
1226 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
1227 struct wined3d_context_vk
*context_vk
;
1231 context_vk
= wined3d_context_vk(context_acquire(swapchain
->device
, back_buffer
, 0));
1233 if (!swapchain_vk
->vk_swapchain
|| swapchain_present_is_partial_copy(swapchain
, dst_rect
))
1235 swapchain_blit_gdi(swapchain
, &context_vk
->c
, src_rect
, dst_rect
);
1239 wined3d_texture_load_location(back_buffer
, 0, &context_vk
->c
, back_buffer
->resource
.draw_binding
);
1241 if ((vr
= wined3d_swapchain_vk_blit(swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1243 if (vr
== VK_ERROR_OUT_OF_DATE_KHR
|| vr
== VK_SUBOPTIMAL_KHR
)
1245 if (FAILED(hr
= wined3d_swapchain_vk_recreate(swapchain_vk
)))
1246 ERR("Failed to recreate swapchain, hr %#lx.\n", hr
);
1247 else if (vr
== VK_ERROR_OUT_OF_DATE_KHR
&& (vr
= wined3d_swapchain_vk_blit(
1248 swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1249 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1253 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1258 wined3d_swapchain_vk_rotate(swapchain
, context_vk
);
1260 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1261 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1263 TRACE("Starting new frame.\n");
1265 context_release(&context_vk
->c
);
1268 static const struct wined3d_swapchain_ops swapchain_vk_ops
=
1270 swapchain_vk_present
,
1271 swapchain_frontbuffer_updated
,
1274 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
1276 struct wined3d_dc_info
*front
;
1277 POINT offset
= {0, 0};
1282 TRACE("swapchain %p.\n", swapchain
);
1284 front
= &swapchain
->front_buffer
->dc_info
[0];
1285 if (swapchain
->palette
)
1286 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
1288 if (swapchain
->front_buffer
->resource
.map_count
)
1289 ERR("Trying to blit a mapped surface.\n");
1291 TRACE("Copying surface %p to screen.\n", front
);
1294 window
= swapchain
->win_handle
;
1295 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
1297 /* Front buffer coordinates are screen coordinates. Map them to the
1298 * destination window if not fullscreened. */
1299 if (swapchain
->state
.desc
.windowed
)
1300 ClientToScreen(window
, &offset
);
1302 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
1304 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
1305 swapchain
->front_buffer
->resource
.height
);
1306 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
1308 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
1309 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
1310 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
1311 ReleaseDC(window
, dst_dc
);
1313 SetRectEmpty(&swapchain
->front_buffer_update
);
1316 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
1317 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1319 struct wined3d_dc_info
*front
, *back
;
1324 front
= &swapchain
->front_buffer
->dc_info
[0];
1325 back
= &swapchain
->back_buffers
[0]->dc_info
[0];
1327 /* Flip the surface data. */
1329 bitmap
= front
->bitmap
;
1330 data
= swapchain
->front_buffer
->resource
.heap_memory
;
1332 front
->dc
= back
->dc
;
1333 front
->bitmap
= back
->bitmap
;
1334 swapchain
->front_buffer
->resource
.heap_memory
= swapchain
->back_buffers
[0]->resource
.heap_memory
;
1337 back
->bitmap
= bitmap
;
1338 swapchain
->back_buffers
[0]->resource
.heap_memory
= data
;
1340 SetRect(&swapchain
->front_buffer_update
, 0, 0,
1341 swapchain
->front_buffer
->resource
.width
,
1342 swapchain
->front_buffer
->resource
.height
);
1343 swapchain_gdi_frontbuffer_updated(swapchain
);
1346 static const struct wined3d_swapchain_ops swapchain_no3d_ops
=
1348 swapchain_gdi_present
,
1349 swapchain_gdi_frontbuffer_updated
,
1352 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
1353 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, unsigned int *quality
)
1355 const struct wined3d_adapter
*adapter
;
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 if (!(format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
)))
1366 if ((t
= min(wined3d_settings
.sample_count
, adapter
->d3d_info
.limits
.sample_count
)))
1367 while (!(format
->multisample_types
& 1u << (t
- 1)))
1369 TRACE("Using sample count %u.\n", t
);
1374 void swapchain_set_max_frame_latency(struct wined3d_swapchain
*swapchain
, const struct wined3d_device
*device
)
1376 /* Subtract 1 for the implicit OpenGL latency. */
1377 swapchain
->max_frame_latency
= device
->max_frame_latency
>= 2 ? device
->max_frame_latency
- 1 : 1;
1380 static enum wined3d_format_id
adapter_format_from_backbuffer_format(const struct wined3d_adapter
*adapter
,
1381 enum wined3d_format_id format_id
)
1383 const struct wined3d_format
*backbuffer_format
;
1385 backbuffer_format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
);
1386 return pixelformat_for_depth(backbuffer_format
->byte_count
* CHAR_BIT
);
1389 static HRESULT
wined3d_swapchain_state_init(struct wined3d_swapchain_state
*state
,
1390 const struct wined3d_swapchain_desc
*desc
, HWND window
, struct wined3d
*wined3d
,
1391 struct wined3d_swapchain_state_parent
*parent
)
1395 state
->desc
= *desc
;
1397 if (FAILED(hr
= wined3d_output_get_display_mode(desc
->output
, &state
->original_mode
, NULL
)))
1399 ERR("Failed to get current display mode, hr %#lx.\n", hr
);
1403 if (state
->desc
.windowed
)
1407 GetClientRect(window
, &client_rect
);
1408 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect
));
1410 if (!state
->desc
.backbuffer_width
)
1412 state
->desc
.backbuffer_width
= client_rect
.right
? client_rect
.right
: 8;
1413 TRACE("Updating width to %u.\n", state
->desc
.backbuffer_width
);
1415 if (!state
->desc
.backbuffer_height
)
1417 state
->desc
.backbuffer_height
= client_rect
.bottom
? client_rect
.bottom
: 8;
1418 TRACE("Updating height to %u.\n", state
->desc
.backbuffer_height
);
1421 if (state
->desc
.backbuffer_format
== WINED3DFMT_UNKNOWN
)
1423 state
->desc
.backbuffer_format
= state
->original_mode
.format_id
;
1424 TRACE("Updating format to %s.\n", debug_d3dformat(state
->original_mode
.format_id
));
1429 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1431 state
->d3d_mode
.width
= desc
->backbuffer_width
;
1432 state
->d3d_mode
.height
= desc
->backbuffer_height
;
1433 state
->d3d_mode
.format_id
= adapter_format_from_backbuffer_format(desc
->output
->adapter
,
1434 desc
->backbuffer_format
);
1435 state
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1436 state
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1440 state
->d3d_mode
= state
->original_mode
;
1444 GetWindowRect(window
, &state
->original_window_rect
);
1445 state
->wined3d
= wined3d
;
1446 state
->device_window
= window
;
1447 state
->desc
.device_window
= window
;
1448 state
->parent
= parent
;
1450 if (desc
->flags
& WINED3D_SWAPCHAIN_REGISTER_STATE
)
1451 wined3d_swapchain_state_register(state
);
1456 static HRESULT
swapchain_create_texture(struct wined3d_swapchain
*swapchain
,
1457 bool front
, bool depth
, struct wined3d_texture
**texture
)
1459 const struct wined3d_swapchain_desc
*swapchain_desc
= &swapchain
->state
.desc
;
1460 struct wined3d_device
*device
= swapchain
->device
;
1461 struct wined3d_resource_desc texture_desc
;
1462 uint32_t texture_flags
= 0;
1465 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1466 texture_desc
.format
= depth
? swapchain_desc
->auto_depth_stencil_format
: swapchain_desc
->backbuffer_format
;
1467 texture_desc
.multisample_type
= swapchain_desc
->multisample_type
;
1468 texture_desc
.multisample_quality
= swapchain_desc
->multisample_quality
;
1469 texture_desc
.usage
= 0;
1470 if (!depth
&& (device
->wined3d
->flags
& WINED3D_NO3D
))
1471 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1472 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1473 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1475 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1476 if (!depth
&& (swapchain_desc
->flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
))
1477 texture_desc
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1478 texture_desc
.width
= swapchain_desc
->backbuffer_width
;
1479 texture_desc
.height
= swapchain_desc
->backbuffer_height
;
1480 texture_desc
.depth
= 1;
1481 texture_desc
.size
= 0;
1484 texture_desc
.bind_flags
= 0;
1486 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
1488 texture_desc
.bind_flags
= swapchain_desc
->backbuffer_bind_flags
;
1490 if (swapchain_desc
->flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
1491 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
1493 if (FAILED(hr
= wined3d_texture_create(device
, &texture_desc
, 1, 1,
1494 texture_flags
, NULL
, NULL
, &wined3d_null_parent_ops
, texture
)))
1496 WARN("Failed to create texture, hr %#lx.\n", hr
);
1501 wined3d_texture_set_swapchain(*texture
, swapchain
);
1506 static HRESULT
wined3d_swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
1507 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1508 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1509 const struct wined3d_swapchain_ops
*swapchain_ops
)
1511 struct wined3d_output_desc output_desc
;
1512 BOOL displaymode_set
= FALSE
;
1513 HRESULT hr
= E_FAIL
;
1517 wined3d_mutex_lock();
1519 if (desc
->backbuffer_count
> 1)
1521 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1522 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1525 if (desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
1526 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
1527 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
1528 FIXME("Unimplemented swap effect %#x.\n", desc
->swap_effect
);
1530 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
1531 TRACE("Using target window %p.\n", window
);
1533 if (FAILED(hr
= wined3d_swapchain_state_init(&swapchain
->state
, desc
, window
, device
->wined3d
, state_parent
)))
1535 ERR("Failed to initialise swapchain state, hr %#lx.\n", hr
);
1536 wined3d_mutex_unlock();
1540 swapchain
->swapchain_ops
= swapchain_ops
;
1541 swapchain
->device
= device
;
1542 swapchain
->parent
= parent
;
1543 swapchain
->parent_ops
= parent_ops
;
1545 swapchain
->win_handle
= window
;
1546 swapchain
->swap_interval
= WINED3D_SWAP_INTERVAL_DEFAULT
;
1547 swapchain_set_max_frame_latency(swapchain
, device
);
1549 if (!swapchain
->state
.desc
.windowed
)
1551 if (FAILED(hr
= wined3d_output_get_desc(desc
->output
, &output_desc
)))
1553 ERR("Failed to get output description, hr %#lx.\n", hr
);
1557 wined3d_swapchain_state_setup_fullscreen(&swapchain
->state
, window
,
1558 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, desc
->backbuffer_width
,
1559 desc
->backbuffer_height
);
1561 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->state
.desc
.backbuffer_format
,
1562 &swapchain
->state
.desc
.multisample_type
, &swapchain
->state
.desc
.multisample_quality
);
1564 TRACE("Creating front buffer.\n");
1566 if (FAILED(hr
= swapchain_create_texture(swapchain
, true, false, &swapchain
->front_buffer
)))
1568 WARN("Failed to create front buffer, hr %#lx.\n", hr
);
1572 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1574 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1575 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1578 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1579 * so we should really check to see if there is a fullscreen swapchain
1580 * already. Does a single head count as full screen? */
1581 if (!desc
->windowed
&& desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1583 /* Change the display settings */
1584 if (FAILED(hr
= wined3d_output_set_display_mode(desc
->output
,
1585 &swapchain
->state
.d3d_mode
)))
1587 WARN("Failed to set display mode, hr %#lx.\n", hr
);
1590 displaymode_set
= TRUE
;
1593 if (swapchain
->state
.desc
.backbuffer_count
> 0)
1595 if (!(swapchain
->back_buffers
= calloc(swapchain
->state
.desc
.backbuffer_count
,
1596 sizeof(*swapchain
->back_buffers
))))
1598 ERR("Failed to allocate backbuffer array memory.\n");
1603 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1605 TRACE("Creating back buffer %u.\n", i
);
1606 if (FAILED(hr
= swapchain_create_texture(swapchain
, false, false, &swapchain
->back_buffers
[i
])))
1608 WARN("Failed to create back buffer %u, hr %#lx.\n", i
, hr
);
1609 swapchain
->state
.desc
.backbuffer_count
= i
;
1615 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1616 if (desc
->enable_auto_depth_stencil
)
1618 TRACE("Creating depth/stencil buffer.\n");
1619 if (!device
->auto_depth_stencil_view
)
1621 struct wined3d_view_desc desc
;
1622 struct wined3d_texture
*ds
;
1624 if (FAILED(hr
= swapchain_create_texture(swapchain
, false, true, &ds
)))
1626 WARN("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr
);
1630 desc
.format_id
= ds
->resource
.format
->id
;
1632 desc
.u
.texture
.level_idx
= 0;
1633 desc
.u
.texture
.level_count
= 1;
1634 desc
.u
.texture
.layer_idx
= 0;
1635 desc
.u
.texture
.layer_count
= 1;
1636 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1637 &device
->auto_depth_stencil_view
);
1638 wined3d_texture_decref(ds
);
1641 ERR("Failed to create rendertarget view, hr %#lx.\n", hr
);
1647 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1649 wined3d_mutex_unlock();
1654 if (displaymode_set
)
1656 if (FAILED(wined3d_restore_display_modes(device
->wined3d
)))
1657 ERR("Failed to restore display mode.\n");
1660 if (swapchain
->back_buffers
)
1662 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1664 if (swapchain
->back_buffers
[i
])
1666 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1667 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1670 free(swapchain
->back_buffers
);
1673 if (swapchain
->front_buffer
)
1675 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1676 wined3d_texture_decref(swapchain
->front_buffer
);
1679 wined3d_swapchain_state_cleanup(&swapchain
->state
);
1680 wined3d_mutex_unlock();
1685 HRESULT
wined3d_swapchain_no3d_init(struct wined3d_swapchain
*swapchain_no3d
, struct wined3d_device
*device
,
1686 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1687 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1689 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1690 swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
);
1692 return wined3d_swapchain_init(swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
,
1693 &swapchain_no3d_ops
);
1696 HRESULT
wined3d_swapchain_gl_init(struct wined3d_swapchain_gl
*swapchain_gl
, struct wined3d_device
*device
,
1697 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1698 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1700 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1701 swapchain_gl
, device
, desc
, state_parent
, parent
, parent_ops
);
1703 return wined3d_swapchain_init(&swapchain_gl
->s
, device
, desc
, state_parent
, parent
,
1704 parent_ops
, &swapchain_gl_ops
);
1707 HRESULT
wined3d_swapchain_vk_init(struct wined3d_swapchain_vk
*swapchain_vk
, struct wined3d_device
*device
,
1708 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1709 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1713 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1714 swapchain_vk
, device
, desc
, parent
, parent_ops
);
1716 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_vk
->s
, device
, desc
, state_parent
, parent
,
1717 parent_ops
, &swapchain_vk_ops
)))
1720 if (swapchain_vk
->s
.win_handle
== GetDesktopWindow())
1722 WARN("Creating a desktop window swapchain.\n");
1726 if (FAILED(hr
= wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
)))
1727 WARN("Failed to create a Vulkan swapchain, hr %#lx.\n", hr
);
1732 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1733 const struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1734 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1735 struct wined3d_swapchain
**swapchain
)
1737 struct wined3d_swapchain
*object
;
1740 if (FAILED(hr
= device
->adapter
->adapter_ops
->adapter_create_swapchain(device
,
1741 desc
, state_parent
, parent
, parent_ops
, &object
)))
1744 if (desc
->flags
& WINED3D_SWAPCHAIN_IMPLICIT
)
1746 wined3d_mutex_lock();
1747 if (FAILED(hr
= wined3d_device_set_implicit_swapchain(device
, object
)))
1749 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1750 device
->adapter
->adapter_ops
->adapter_destroy_swapchain(object
);
1751 wined3d_mutex_unlock();
1754 wined3d_mutex_unlock();
1757 *swapchain
= object
;
1762 static struct wined3d_context_gl
*wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1764 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1765 struct wined3d_context_gl
*context_gl
;
1767 TRACE("Creating a new context for swapchain %p, thread %lu.\n", swapchain_gl
, GetCurrentThreadId());
1769 wined3d_from_cs(device
->cs
);
1771 if (!(context_gl
= calloc(1, sizeof(*context_gl
))))
1773 ERR("Failed to allocate context memory.\n");
1777 if (FAILED(wined3d_context_gl_init(context_gl
, swapchain_gl
)))
1779 WARN("Failed to initialise context.\n");
1784 if (!device_context_add(device
, &context_gl
->c
))
1786 ERR("Failed to add the newly created context to the context list.\n");
1787 wined3d_context_gl_destroy(context_gl
);
1791 TRACE("Created context %p.\n", context_gl
);
1793 context_release(&context_gl
->c
);
1798 struct wined3d_context_gl
*wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1800 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1801 DWORD tid
= GetCurrentThreadId();
1804 for (i
= 0; i
< device
->context_count
; ++i
)
1806 if (wined3d_context_gl(device
->contexts
[i
])->tid
== tid
)
1807 return wined3d_context_gl(device
->contexts
[i
]);
1810 /* Create a new context for the thread. */
1811 return wined3d_swapchain_gl_create_context(swapchain_gl
);
1814 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1818 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1820 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1822 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1826 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1828 struct wined3d_device
*device
= swapchain
->device
;
1829 HWND window
= swapchain
->state
.device_window
;
1830 struct wined3d_output_desc output_desc
;
1831 unsigned int screensaver_active
;
1832 struct wined3d_output
*output
;
1833 BOOL focus_messages
, filter
;
1836 /* This code is not protected by the wined3d mutex, so it may run while
1837 * wined3d_device_reset is active. Testing on Windows shows that changing
1838 * focus during resets and resetting during focus change events causes
1839 * the application to crash with an invalid memory access. */
1841 if (!(focus_messages
= device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
))
1842 filter
= wined3d_filter_messages(window
, TRUE
);
1846 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
1847 if ((device
->restore_screensaver
= !!screensaver_active
))
1848 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
1850 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1852 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1853 * the window but leaves the size untouched, d3d9 sets the size on an
1854 * invisible window, generates messages but doesn't change the window
1855 * properties. The implementation follows d3d9.
1857 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1858 * resume drawing after a focus loss. */
1859 output
= wined3d_swapchain_get_output(swapchain
);
1862 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1866 if (SUCCEEDED(hr
= wined3d_output_get_desc(output
, &output_desc
)))
1867 SetWindowPos(window
, NULL
, output_desc
.desktop_rect
.left
,
1868 output_desc
.desktop_rect
.top
, swapchain
->state
.desc
.backbuffer_width
,
1869 swapchain
->state
.desc
.backbuffer_height
, SWP_NOACTIVATE
| SWP_NOZORDER
);
1871 ERR("Failed to get output description, hr %#lx.\n", hr
);
1874 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1876 output
= wined3d_swapchain_get_output(swapchain
);
1879 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1883 if (FAILED(hr
= wined3d_output_set_display_mode(output
,
1884 &swapchain
->state
.d3d_mode
)))
1885 ERR("Failed to set display mode, hr %#lx.\n", hr
);
1888 if (swapchain
== device
->swapchains
[0])
1889 device
->device_parent
->ops
->activate(device
->device_parent
, TRUE
);
1893 if (device
->restore_screensaver
)
1895 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
1896 device
->restore_screensaver
= FALSE
;
1899 if (FAILED(hr
= wined3d_restore_display_modes(device
->wined3d
)))
1900 ERR("Failed to restore display modes, hr %#lx.\n", hr
);
1902 swapchain
->reapply_mode
= TRUE
;
1904 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1905 * during window minimization. Do our housekeeping now, as the device may not exist after
1906 * the ShowWindow call.
1908 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1909 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1910 * we'll have to control this via a create flag. Note that the device and swapchain are not
1911 * safe to access after the ShowWindow call. */
1912 if (swapchain
== device
->swapchains
[0])
1913 device
->device_parent
->ops
->activate(device
->device_parent
, FALSE
);
1915 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
) && IsWindowVisible(window
))
1916 ShowWindow(window
, SW_MINIMIZE
);
1919 if (!focus_messages
)
1920 wined3d_filter_messages(window
, filter
);
1923 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1924 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1925 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1927 struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
1928 bool recreate
= false;
1930 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1931 "multisample_type %#x, multisample_quality %#x.\n",
1932 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1933 multisample_type
, multisample_quality
);
1935 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1937 if (buffer_count
&& buffer_count
!= desc
->backbuffer_count
)
1938 FIXME("Cannot change the back buffer count yet.\n");
1940 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1942 if (!width
|| !height
)
1946 /* The application is requesting that either the swapchain width or
1947 * height be set to the corresponding dimension in the window's
1950 if (!GetClientRect(swapchain
->state
.device_window
, &client_rect
))
1952 ERR("Failed to get client rect, last error %#lx.\n", GetLastError());
1953 return WINED3DERR_INVALIDCALL
;
1957 width
= client_rect
.right
;
1960 height
= client_rect
.bottom
;
1963 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
1965 desc
->backbuffer_width
= width
;
1966 desc
->backbuffer_height
= height
;
1970 if (format_id
== WINED3DFMT_UNKNOWN
)
1972 if (!desc
->windowed
)
1973 return WINED3DERR_INVALIDCALL
;
1974 format_id
= swapchain
->state
.original_mode
.format_id
;
1977 if (format_id
!= desc
->backbuffer_format
)
1979 desc
->backbuffer_format
= format_id
;
1983 if (multisample_type
!= desc
->multisample_type
1984 || multisample_quality
!= desc
->multisample_quality
)
1986 desc
->multisample_type
= multisample_type
;
1987 desc
->multisample_quality
= multisample_quality
;
1993 struct wined3d_texture
*new_texture
;
1997 TRACE("Recreating swapchain textures.\n");
1999 if (FAILED(hr
= swapchain_create_texture(swapchain
, true, false, &new_texture
)))
2001 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
2002 if (wined3d_texture_decref(swapchain
->front_buffer
))
2003 ERR("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
2004 swapchain
->front_buffer
= new_texture
;
2006 if (!(swapchain
->device
->wined3d
->flags
& WINED3D_NO3D
))
2008 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
2009 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
2012 for (i
= 0; i
< desc
->backbuffer_count
; ++i
)
2014 if (FAILED(hr
= swapchain_create_texture(swapchain
, false, false, &new_texture
)))
2016 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
2017 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
2018 ERR("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
2019 swapchain
->back_buffers
[i
] = new_texture
;
2023 swapchain_update_draw_bindings(swapchain
);
2028 static HRESULT
wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state
*state
,
2029 struct wined3d_output
*output
, struct wined3d_display_mode
*mode
)
2033 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
2035 if (FAILED(hr
= wined3d_output_find_closest_matching_mode(output
, mode
)))
2037 WARN("Failed to find closest matching mode, hr %#lx.\n", hr
);
2041 if (output
!= state
->desc
.output
)
2043 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2045 WARN("Failed to restore display modes, hr %#lx.\n", hr
);
2049 if (FAILED(hr
= wined3d_output_get_display_mode(output
, &state
->original_mode
, NULL
)))
2051 WARN("Failed to get current display mode, hr %#lx.\n", hr
);
2056 if (FAILED(hr
= wined3d_output_set_display_mode(output
, mode
)))
2058 WARN("Failed to set display mode, hr %#lx.\n", hr
);
2059 return WINED3DERR_INVALIDCALL
;
2065 HRESULT CDECL
wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state
*state
,
2066 const struct wined3d_display_mode
*mode
)
2068 struct wined3d_display_mode actual_mode
;
2069 struct wined3d_output_desc output_desc
;
2070 RECT original_window_rect
, window_rect
;
2071 int x
, y
, width
, height
;
2075 TRACE("state %p, mode %p.\n", state
, mode
);
2077 wined3d_mutex_lock();
2079 window
= state
->device_window
;
2081 if (state
->desc
.windowed
)
2083 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
2084 AdjustWindowRectEx(&window_rect
,
2085 GetWindowLongW(window
, GWL_STYLE
), FALSE
,
2086 GetWindowLongW(window
, GWL_EXSTYLE
));
2087 GetWindowRect(window
, &original_window_rect
);
2089 x
= original_window_rect
.left
;
2090 y
= original_window_rect
.top
;
2091 width
= window_rect
.right
- window_rect
.left
;
2092 height
= window_rect
.bottom
- window_rect
.top
;
2096 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2098 actual_mode
= *mode
;
2099 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, state
->desc
.output
,
2102 ERR("Failed to set display mode, hr %#lx.\n", hr
);
2103 wined3d_mutex_unlock();
2108 if (FAILED(hr
= wined3d_output_get_desc(state
->desc
.output
, &output_desc
)))
2110 ERR("Failed to get output description, hr %#lx.\n", hr
);
2111 wined3d_mutex_unlock();
2115 x
= output_desc
.desktop_rect
.left
;
2116 y
= output_desc
.desktop_rect
.top
;
2117 width
= output_desc
.desktop_rect
.right
- output_desc
.desktop_rect
.left
;
2118 height
= output_desc
.desktop_rect
.bottom
- output_desc
.desktop_rect
.top
;
2121 wined3d_mutex_unlock();
2123 MoveWindow(window
, x
, y
, width
, height
, TRUE
);
2128 static LONG
fullscreen_style(LONG style
)
2130 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2131 style
|= WS_POPUP
| WS_SYSMENU
;
2132 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
2137 static LONG
fullscreen_exstyle(LONG exstyle
)
2139 /* Filter out window decorations. */
2140 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
2145 struct wined3d_window_state
2148 HWND window_pos_after
;
2149 LONG style
, exstyle
;
2150 int x
, y
, width
, height
;
2153 bool register_topmost_timer
;
2154 bool set_topmost_timer
;
2157 #define WINED3D_WINDOW_TOPMOST_TIMER_ID 0x4242
2159 static void CALLBACK
topmost_timer_proc(HWND hwnd
, UINT msg
, UINT_PTR id
, DWORD time
)
2161 if (!(GetWindowLongW(hwnd
, GWL_EXSTYLE
) & WS_EX_TOPMOST
))
2162 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0, SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
2164 KillTimer(hwnd
, WINED3D_WINDOW_TOPMOST_TIMER_ID
);
2167 static DWORD WINAPI
set_window_state_thread(void *ctx
)
2169 struct wined3d_window_state
*s
= ctx
;
2172 filter
= wined3d_filter_messages(s
->window
, TRUE
);
2176 SetWindowLongW(s
->window
, GWL_STYLE
, s
->style
);
2177 SetWindowLongW(s
->window
, GWL_EXSTYLE
, s
->exstyle
);
2179 SetWindowPos(s
->window
, s
->window_pos_after
, s
->x
, s
->y
, s
->width
, s
->height
, s
->flags
);
2181 wined3d_filter_messages(s
->window
, filter
);
2188 static void set_window_state(struct wined3d_window_state
*s
)
2190 static const UINT timeout
= 1500;
2191 DWORD window_tid
= GetWindowThreadProcessId(s
->window
, NULL
);
2192 DWORD tid
= GetCurrentThreadId();
2195 TRACE("Window %p belongs to thread %#lx.\n", s
->window
, window_tid
);
2196 /* If the window belongs to a different thread, modifying the style and/or
2197 * position can potentially deadlock if that thread isn't processing
2199 if (window_tid
== tid
)
2201 /* Deus Ex: Game of the Year Edition removes WS_EX_TOPMOST after changing resolutions in
2202 * exclusive fullscreen mode. Tests show that WS_EX_TOPMOST will be restored when a ~1.5s
2203 * timer times out */
2204 if (s
->register_topmost_timer
)
2206 if (s
->set_topmost_timer
)
2207 SetTimer(s
->window
, WINED3D_WINDOW_TOPMOST_TIMER_ID
, timeout
, topmost_timer_proc
);
2209 KillTimer(s
->window
, WINED3D_WINDOW_TOPMOST_TIMER_ID
);
2212 set_window_state_thread(s
);
2214 else if ((thread
= CreateThread(NULL
, 0, set_window_state_thread
, s
, 0, NULL
)))
2216 SetThreadDescription(thread
, L
"wined3d_set_window_state");
2217 CloseHandle(thread
);
2221 ERR("Failed to create thread.\n");
2225 HRESULT
wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state
*state
,
2226 HWND window
, int x
, int y
, int width
, int height
)
2228 struct wined3d_window_state
*s
;
2230 TRACE("Setting up window %p for fullscreen mode.\n", window
);
2232 if (!IsWindow(window
))
2234 WARN("%p is not a valid window.\n", window
);
2235 return WINED3DERR_NOTAVAILABLE
;
2238 if (!(s
= malloc(sizeof(*s
))))
2239 return E_OUTOFMEMORY
;
2241 s
->window_pos_after
= HWND_TOPMOST
;
2247 if (state
->style
|| state
->exstyle
)
2249 ERR("Changing the window style for window %p, but another style (%08lx, %08lx) is already stored.\n",
2250 window
, state
->style
, state
->exstyle
);
2253 s
->flags
= SWP_FRAMECHANGED
| SWP_NOACTIVATE
;
2254 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
)
2255 s
->flags
|= SWP_NOZORDER
;
2257 s
->flags
|= SWP_SHOWWINDOW
;
2259 state
->style
= GetWindowLongW(window
, GWL_STYLE
);
2260 state
->exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2262 s
->style
= fullscreen_style(state
->style
);
2263 s
->exstyle
= fullscreen_exstyle(state
->exstyle
);
2264 s
->set_style
= true;
2265 s
->register_topmost_timer
= !!(state
->desc
.flags
& WINED3D_SWAPCHAIN_REGISTER_TOPMOST_TIMER
);
2266 s
->set_topmost_timer
= true;
2268 TRACE("Old style was %08lx, %08lx, setting to %08lx, %08lx.\n",
2269 state
->style
, state
->exstyle
, s
->style
, s
->exstyle
);
2271 set_window_state(s
);
2275 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state
*state
,
2276 HWND window
, const RECT
*window_rect
)
2278 struct wined3d_window_state
*s
;
2279 LONG style
, exstyle
;
2281 if (!state
->style
&& !state
->exstyle
)
2284 if (!(s
= malloc(sizeof(*s
))))
2288 s
->window_pos_after
= NULL
;
2289 s
->flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
2291 if ((state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE
)
2292 && !(state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
))
2294 s
->window_pos_after
= (state
->exstyle
& WS_EX_TOPMOST
) ? HWND_TOPMOST
: HWND_NOTOPMOST
;
2295 s
->flags
|= (state
->style
& WS_VISIBLE
) ? SWP_SHOWWINDOW
: SWP_HIDEWINDOW
;
2296 s
->flags
&= ~SWP_NOZORDER
;
2299 style
= GetWindowLongW(window
, GWL_STYLE
);
2300 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2302 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2303 * application, and we want to ignore them in the test below, since it's
2304 * not the application's fault that they changed. Additionally, we want to
2305 * preserve the current status of these flags (i.e. don't restore them) to
2306 * more closely emulate the behavior of Direct3D, which leaves these flags
2307 * alone when returning to windowed mode. */
2308 state
->style
^= (state
->style
^ style
) & WS_VISIBLE
;
2309 state
->exstyle
^= (state
->exstyle
^ exstyle
) & WS_EX_TOPMOST
;
2311 TRACE("Restoring window style of window %p to %08lx, %08lx.\n",
2312 window
, state
->style
, state
->exstyle
);
2314 s
->style
= state
->style
;
2315 s
->exstyle
= state
->exstyle
;
2316 /* Only restore the style if the application didn't modify it during the
2317 * fullscreen phase. Some applications change it before calling Reset()
2318 * when switching between windowed and fullscreen modes (HL2), some
2319 * depend on the original style (Eve Online). */
2320 s
->set_style
= style
== fullscreen_style(state
->style
) && exstyle
== fullscreen_exstyle(state
->exstyle
);
2321 s
->register_topmost_timer
= !!(state
->desc
.flags
& WINED3D_SWAPCHAIN_REGISTER_TOPMOST_TIMER
);
2322 s
->set_topmost_timer
= false;
2326 s
->x
= window_rect
->left
;
2327 s
->y
= window_rect
->top
;
2328 s
->width
= window_rect
->right
- window_rect
->left
;
2329 s
->height
= window_rect
->bottom
- window_rect
->top
;
2333 s
->x
= s
->y
= s
->width
= s
->height
= 0;
2334 s
->flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
2337 set_window_state(s
);
2339 /* Delete the old values. */
2344 HRESULT CDECL
wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state
*state
,
2345 const struct wined3d_swapchain_desc
*swapchain_desc
,
2346 const struct wined3d_display_mode
*mode
)
2348 struct wined3d_display_mode actual_mode
;
2349 struct wined3d_output_desc output_desc
;
2350 BOOL windowed
= state
->desc
.windowed
;
2353 TRACE("state %p, swapchain_desc %p, mode %p.\n", state
, swapchain_desc
, mode
);
2355 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2359 actual_mode
= *mode
;
2360 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2366 if (!swapchain_desc
->windowed
)
2368 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
2369 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
2370 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
2371 actual_mode
.format_id
= adapter_format_from_backbuffer_format(swapchain_desc
->output
->adapter
,
2372 swapchain_desc
->backbuffer_format
);
2373 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
2374 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2380 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2382 WARN("Failed to restore display modes for all outputs, hr %#lx.\n", hr
);
2391 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2393 if (FAILED(hr
= wined3d_output_get_display_mode(swapchain_desc
->output
, &actual_mode
,
2396 ERR("Failed to get display mode, hr %#lx.\n", hr
);
2397 return WINED3DERR_INVALIDCALL
;
2401 if (!swapchain_desc
->windowed
)
2403 unsigned int width
= actual_mode
.width
;
2404 unsigned int height
= actual_mode
.height
;
2406 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
2408 ERR("Failed to get output description, hr %#lx.\n", hr
);
2412 if (state
->desc
.windowed
)
2414 /* Switch from windowed to fullscreen */
2415 if (FAILED(hr
= wined3d_swapchain_state_setup_fullscreen(state
, state
->device_window
,
2416 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
, height
)))
2421 HWND window
= state
->device_window
;
2424 /* Fullscreen -> fullscreen mode change */
2425 filter
= wined3d_filter_messages(window
, TRUE
);
2426 MoveWindow(window
, output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
,
2428 ShowWindow(window
, SW_SHOW
);
2429 wined3d_filter_messages(window
, filter
);
2431 state
->d3d_mode
= actual_mode
;
2433 else if (!state
->desc
.windowed
)
2435 /* Fullscreen -> windowed switch */
2436 RECT
*window_rect
= NULL
;
2437 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
2438 window_rect
= &state
->original_window_rect
;
2439 wined3d_swapchain_state_restore_from_fullscreen(state
, state
->device_window
, window_rect
);
2442 state
->desc
.output
= swapchain_desc
->output
;
2443 state
->desc
.windowed
= swapchain_desc
->windowed
;
2445 if (windowed
!= state
->desc
.windowed
)
2446 state
->parent
->ops
->windowed_state_changed(state
->parent
, state
->desc
.windowed
);
2451 BOOL CDECL
wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state
*state
)
2453 TRACE("state %p.\n", state
);
2455 return state
->desc
.windowed
;
2458 void CDECL
wined3d_swapchain_state_get_size(const struct wined3d_swapchain_state
*state
,
2459 unsigned int *width
, unsigned int *height
)
2461 TRACE("state %p.\n", state
);
2463 *width
= state
->desc
.backbuffer_width
;
2464 *height
= state
->desc
.backbuffer_height
;
2467 void CDECL
wined3d_swapchain_state_destroy(struct wined3d_swapchain_state
*state
)
2469 wined3d_swapchain_state_cleanup(state
);
2473 HRESULT CDECL
wined3d_swapchain_state_create(const struct wined3d_swapchain_desc
*desc
,
2474 HWND window
, struct wined3d
*wined3d
, struct wined3d_swapchain_state_parent
*state_parent
,
2475 struct wined3d_swapchain_state
**state
)
2477 struct wined3d_swapchain_state
*s
;
2480 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc
, window
, wined3d
, state
);
2482 if (!(s
= calloc(1, sizeof(*s
))))
2483 return E_OUTOFMEMORY
;
2485 if (FAILED(hr
= wined3d_swapchain_state_init(s
, desc
, window
, wined3d
, state_parent
)))