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
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
29 void wined3d_swapchain_cleanup(struct wined3d_swapchain
*swapchain
)
34 TRACE("Destroying swapchain %p.\n", swapchain
);
36 wined3d_swapchain_state_cleanup(&swapchain
->state
);
37 wined3d_swapchain_set_gamma_ramp(swapchain
, 0, &swapchain
->orig_gamma
);
39 /* Release the swapchain's draw buffers. Make sure swapchain->back_buffers[0]
40 * is the last buffer to be destroyed, FindContext() depends on that. */
41 if (swapchain
->front_buffer
)
43 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
44 if (wined3d_texture_decref(swapchain
->front_buffer
))
45 WARN("Something's still holding the front buffer (%p).\n", swapchain
->front_buffer
);
46 swapchain
->front_buffer
= NULL
;
49 if (swapchain
->back_buffers
)
51 i
= swapchain
->state
.desc
.backbuffer_count
;
55 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
56 if (wined3d_texture_decref(swapchain
->back_buffers
[i
]))
57 WARN("Something's still holding back buffer %u (%p).\n", i
, swapchain
->back_buffers
[i
]);
59 heap_free(swapchain
->back_buffers
);
60 swapchain
->back_buffers
= NULL
;
63 /* Restore the screen resolution if we rendered in fullscreen.
64 * This will restore the screen resolution to what it was before creating
65 * the swapchain. In case of d3d8 and d3d9 this will be the original
66 * desktop resolution. In case of d3d7 this will be a NOP because ddraw
67 * sets the resolution before starting up Direct3D, thus orig_width and
68 * orig_height will be equal to the modes in the presentation params. */
69 if (!swapchain
->state
.desc
.windowed
)
71 if (swapchain
->state
.desc
.auto_restore_display_mode
)
73 if (FAILED(hr
= wined3d_restore_display_modes(swapchain
->device
->wined3d
)))
74 ERR("Failed to restore display mode, hr %#x.\n", hr
);
76 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
78 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
,
79 swapchain
->state
.device_window
, &swapchain
->state
.original_window_rect
);
80 wined3d_device_release_focus_window(swapchain
->device
);
85 wined3d_swapchain_state_restore_from_fullscreen(&swapchain
->state
, swapchain
->state
.device_window
, NULL
);
90 static void wined3d_swapchain_gl_destroy_object(void *object
)
92 wined3d_swapchain_gl_destroy_contexts(object
);
95 void wined3d_swapchain_gl_cleanup(struct wined3d_swapchain_gl
*swapchain_gl
)
97 struct wined3d_cs
*cs
= swapchain_gl
->s
.device
->cs
;
99 wined3d_swapchain_cleanup(&swapchain_gl
->s
);
101 wined3d_cs_destroy_object(cs
, wined3d_swapchain_gl_destroy_object
, swapchain_gl
);
102 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
104 if (swapchain_gl
->backup_dc
)
106 TRACE("Destroying backup wined3d window %p, dc %p.\n", swapchain_gl
->backup_wnd
, swapchain_gl
->backup_dc
);
108 wined3d_release_dc(swapchain_gl
->backup_wnd
, swapchain_gl
->backup_dc
);
109 DestroyWindow(swapchain_gl
->backup_wnd
);
113 static void wined3d_swapchain_vk_destroy_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
115 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
116 const struct wined3d_vk_info
*vk_info
;
120 TRACE("swapchain_vk %p.\n", swapchain_vk
);
122 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
124 if ((vr
= VK_CALL(vkQueueWaitIdle(device_vk
->vk_queue
))) < 0)
125 ERR("Failed to wait on queue, vr %s.\n", wined3d_debug_vkresult(vr
));
126 heap_free(swapchain_vk
->vk_images
);
127 for (i
= 0; i
< swapchain_vk
->image_count
; ++i
)
129 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
130 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
132 heap_free(swapchain_vk
->vk_semaphores
);
133 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, NULL
));
134 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, swapchain_vk
->vk_surface
, NULL
));
137 static void wined3d_swapchain_vk_destroy_object(void *object
)
139 wined3d_swapchain_vk_destroy_vulkan_swapchain(object
);
142 void wined3d_swapchain_vk_cleanup(struct wined3d_swapchain_vk
*swapchain_vk
)
144 struct wined3d_cs
*cs
= swapchain_vk
->s
.device
->cs
;
146 wined3d_cs_destroy_object(cs
, wined3d_swapchain_vk_destroy_object
, swapchain_vk
);
147 wined3d_cs_finish(cs
, WINED3D_CS_QUEUE_DEFAULT
);
149 wined3d_swapchain_cleanup(&swapchain_vk
->s
);
152 ULONG CDECL
wined3d_swapchain_incref(struct wined3d_swapchain
*swapchain
)
154 ULONG refcount
= InterlockedIncrement(&swapchain
->ref
);
156 TRACE("%p increasing refcount to %u.\n", swapchain
, refcount
);
161 ULONG CDECL
wined3d_swapchain_decref(struct wined3d_swapchain
*swapchain
)
163 ULONG refcount
= InterlockedDecrement(&swapchain
->ref
);
165 TRACE("%p decreasing refcount to %u.\n", swapchain
, refcount
);
169 struct wined3d_device
*device
;
171 wined3d_mutex_lock();
173 device
= swapchain
->device
;
174 if (device
->swapchain_count
&& device
->swapchains
[0] == swapchain
)
175 wined3d_device_uninit_3d(device
);
176 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
178 swapchain
->parent_ops
->wined3d_object_destroyed(swapchain
->parent
);
179 swapchain
->device
->adapter
->adapter_ops
->adapter_destroy_swapchain(swapchain
);
181 wined3d_mutex_unlock();
187 void * CDECL
wined3d_swapchain_get_parent(const struct wined3d_swapchain
*swapchain
)
189 TRACE("swapchain %p.\n", swapchain
);
191 return swapchain
->parent
;
194 void CDECL
wined3d_swapchain_set_window(struct wined3d_swapchain
*swapchain
, HWND window
)
197 window
= swapchain
->state
.device_window
;
198 if (window
== swapchain
->win_handle
)
201 TRACE("Setting swapchain %p window from %p to %p.\n",
202 swapchain
, swapchain
->win_handle
, window
);
204 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
206 swapchain
->win_handle
= window
;
209 HRESULT CDECL
wined3d_swapchain_present(struct wined3d_swapchain
*swapchain
,
210 const RECT
*src_rect
, const RECT
*dst_rect
, HWND dst_window_override
,
211 unsigned int swap_interval
, DWORD flags
)
215 TRACE("swapchain %p, src_rect %s, dst_rect %s, dst_window_override %p, swap_interval %u, flags %#x.\n",
216 swapchain
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
217 dst_window_override
, swap_interval
, flags
);
220 FIXME("Ignoring flags %#x.\n", flags
);
222 wined3d_mutex_lock();
224 if (!swapchain
->back_buffers
)
226 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL.\n");
227 wined3d_mutex_unlock();
228 return WINED3DERR_INVALIDCALL
;
233 SetRect(&s
, 0, 0, swapchain
->state
.desc
.backbuffer_width
,
234 swapchain
->state
.desc
.backbuffer_height
);
240 GetClientRect(swapchain
->win_handle
, &d
);
244 wined3d_cs_emit_present(swapchain
->device
->cs
, swapchain
, src_rect
,
245 dst_rect
, dst_window_override
, swap_interval
, flags
);
247 wined3d_mutex_unlock();
252 HRESULT CDECL
wined3d_swapchain_get_front_buffer_data(const struct wined3d_swapchain
*swapchain
,
253 struct wined3d_texture
*dst_texture
, unsigned int sub_resource_idx
)
255 RECT src_rect
, dst_rect
;
257 TRACE("swapchain %p, dst_texture %p, sub_resource_idx %u.\n", swapchain
, dst_texture
, sub_resource_idx
);
259 SetRect(&src_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
, swapchain
->front_buffer
->resource
.height
);
262 if (swapchain
->state
.desc
.windowed
)
264 MapWindowPoints(swapchain
->win_handle
, NULL
, (POINT
*)&dst_rect
, 2);
265 FIXME("Using destination rect %s in windowed mode, this is likely wrong.\n",
266 wine_dbgstr_rect(&dst_rect
));
269 return wined3d_texture_blt(dst_texture
, sub_resource_idx
, &dst_rect
,
270 swapchain
->front_buffer
, 0, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
273 struct wined3d_texture
* CDECL
wined3d_swapchain_get_back_buffer(const struct wined3d_swapchain
*swapchain
,
274 UINT back_buffer_idx
)
276 TRACE("swapchain %p, back_buffer_idx %u.\n",
277 swapchain
, back_buffer_idx
);
279 /* Return invalid if there is no backbuffer array, otherwise it will
280 * crash when ddraw is used (there swapchain->back_buffers is always
281 * NULL). We need this because this function is called from
282 * stateblock_init_default_state() to get the default scissorrect
284 if (!swapchain
->back_buffers
|| back_buffer_idx
>= swapchain
->state
.desc
.backbuffer_count
)
286 WARN("Invalid back buffer index.\n");
287 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it
288 * here in wined3d to avoid problems in other libs. */
292 TRACE("Returning back buffer %p.\n", swapchain
->back_buffers
[back_buffer_idx
]);
294 return swapchain
->back_buffers
[back_buffer_idx
];
297 struct wined3d_output
* wined3d_swapchain_get_output(const struct wined3d_swapchain
*swapchain
)
299 TRACE("swapchain %p.\n", swapchain
);
301 return swapchain
->state
.desc
.output
;
304 HRESULT CDECL
wined3d_swapchain_get_raster_status(const struct wined3d_swapchain
*swapchain
,
305 struct wined3d_raster_status
*raster_status
)
307 struct wined3d_output
*output
;
309 TRACE("swapchain %p, raster_status %p.\n", swapchain
, raster_status
);
311 output
= wined3d_swapchain_get_output(swapchain
);
314 ERR("Failed to get output from swapchain %p.\n", swapchain
);
318 return wined3d_output_get_raster_status(output
, raster_status
);
321 struct wined3d_swapchain_state
* CDECL
wined3d_swapchain_get_state(struct wined3d_swapchain
*swapchain
)
323 return &swapchain
->state
;
326 HRESULT CDECL
wined3d_swapchain_get_display_mode(const struct wined3d_swapchain
*swapchain
,
327 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
329 struct wined3d_output
*output
;
332 TRACE("swapchain %p, mode %p, rotation %p.\n", swapchain
, mode
, rotation
);
334 if (!(output
= wined3d_swapchain_get_output(swapchain
)))
336 ERR("Failed to get output from swapchain %p.\n", swapchain
);
340 hr
= wined3d_output_get_display_mode(output
, mode
, rotation
);
342 TRACE("Returning w %u, h %u, refresh rate %u, format %s.\n",
343 mode
->width
, mode
->height
, mode
->refresh_rate
, debug_d3dformat(mode
->format_id
));
348 struct wined3d_device
* CDECL
wined3d_swapchain_get_device(const struct wined3d_swapchain
*swapchain
)
350 TRACE("swapchain %p.\n", swapchain
);
352 return swapchain
->device
;
355 void CDECL
wined3d_swapchain_get_desc(const struct wined3d_swapchain
*swapchain
,
356 struct wined3d_swapchain_desc
*desc
)
358 TRACE("swapchain %p, desc %p.\n", swapchain
, desc
);
360 *desc
= swapchain
->state
.desc
;
363 HRESULT CDECL
wined3d_swapchain_set_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
364 DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
368 TRACE("swapchain %p, flags %#x, ramp %p.\n", swapchain
, flags
, ramp
);
371 FIXME("Ignoring flags %#x.\n", flags
);
373 dc
= GetDCEx(swapchain
->state
.device_window
, 0, DCX_USESTYLE
| DCX_CACHE
);
374 SetDeviceGammaRamp(dc
, (void *)ramp
);
375 ReleaseDC(swapchain
->state
.device_window
, dc
);
380 void CDECL
wined3d_swapchain_set_palette(struct wined3d_swapchain
*swapchain
, struct wined3d_palette
*palette
)
382 TRACE("swapchain %p, palette %p.\n", swapchain
, palette
);
384 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
386 swapchain
->palette
= palette
;
389 HRESULT CDECL
wined3d_swapchain_get_gamma_ramp(const struct wined3d_swapchain
*swapchain
,
390 struct wined3d_gamma_ramp
*ramp
)
394 TRACE("swapchain %p, ramp %p.\n", swapchain
, ramp
);
396 dc
= GetDCEx(swapchain
->state
.device_window
, 0, DCX_USESTYLE
| DCX_CACHE
);
397 GetDeviceGammaRamp(dc
, ramp
);
398 ReleaseDC(swapchain
->state
.device_window
, dc
);
403 /* The is a fallback for cases where we e.g. can't create a GL context or
404 * Vulkan swapchain for the swapchain window. */
405 static void swapchain_blit_gdi(struct wined3d_swapchain
*swapchain
,
406 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
408 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
409 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
410 D3DKMT_CREATEDCFROMMEMORY create_desc
;
411 const struct wined3d_format
*format
;
412 unsigned int row_pitch
, slice_pitch
;
417 static unsigned int once
;
419 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
420 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
423 FIXME("Using GDI present.\n");
425 format
= back_buffer
->resource
.format
;
426 if (!format
->ddi_format
)
428 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
432 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_SYSMEM
);
433 wined3d_texture_get_pitch(back_buffer
, 0, &row_pitch
, &slice_pitch
);
435 create_desc
.pMemory
= back_buffer
->resource
.heap_memory
;
436 create_desc
.Format
= format
->ddi_format
;
437 create_desc
.Width
= wined3d_texture_get_level_width(back_buffer
, 0);
438 create_desc
.Height
= wined3d_texture_get_level_height(back_buffer
, 0);
439 create_desc
.Pitch
= row_pitch
;
440 create_desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
441 create_desc
.pColorTable
= NULL
;
443 status
= D3DKMTCreateDCFromMemory(&create_desc
);
444 DeleteDC(create_desc
.hDeviceDc
);
447 WARN("Failed to create DC, status %#x.\n", status
);
451 src_dc
= create_desc
.hDc
;
452 bitmap
= create_desc
.hBitmap
;
454 TRACE("Created source DC %p, bitmap %p for backbuffer %p.\n", src_dc
, bitmap
, back_buffer
);
456 if (!(dst_dc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
457 ERR("Failed to get destination DC.\n");
459 if (!BitBlt(dst_dc
, dst_rect
->left
, dst_rect
->top
, dst_rect
->right
- dst_rect
->left
,
460 dst_rect
->bottom
- dst_rect
->top
, src_dc
, src_rect
->left
, src_rect
->top
, SRCCOPY
))
461 ERR("Failed to blit.\n");
463 ReleaseDC(swapchain
->win_handle
, dst_dc
);
464 destroy_desc
.hDc
= src_dc
;
465 destroy_desc
.hBitmap
= bitmap
;
466 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
467 ERR("Failed to destroy src dc, status %#x.\n", status
);
470 /* A GL context is provided by the caller */
471 static void swapchain_blit(const struct wined3d_swapchain
*swapchain
,
472 struct wined3d_context
*context
, const RECT
*src_rect
, const RECT
*dst_rect
)
474 struct wined3d_texture
*texture
= swapchain
->back_buffers
[0];
475 struct wined3d_device
*device
= swapchain
->device
;
476 enum wined3d_texture_filter_type filter
;
479 TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
480 swapchain
, context
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
));
482 if ((src_rect
->right
- src_rect
->left
== dst_rect
->right
- dst_rect
->left
483 && src_rect
->bottom
- src_rect
->top
== dst_rect
->bottom
- dst_rect
->top
)
484 || is_complex_fixup(texture
->resource
.format
->color_fixup
))
485 filter
= WINED3D_TEXF_NONE
;
487 filter
= WINED3D_TEXF_LINEAR
;
489 location
= WINED3D_LOCATION_TEXTURE_RGB
;
490 if (texture
->resource
.multisample_type
)
491 location
= WINED3D_LOCATION_RB_RESOLVED
;
493 wined3d_texture_validate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
494 device
->blitter
->ops
->blitter_blit(device
->blitter
, WINED3D_BLIT_OP_COLOR_BLIT
, context
, texture
, 0,
495 location
, src_rect
, texture
, 0, WINED3D_LOCATION_DRAWABLE
, dst_rect
, NULL
, filter
);
496 wined3d_texture_invalidate_location(texture
, 0, WINED3D_LOCATION_DRAWABLE
);
499 static void swapchain_gl_set_swap_interval(struct wined3d_swapchain
*swapchain
,
500 struct wined3d_context_gl
*context_gl
, unsigned int swap_interval
)
502 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
504 swap_interval
= swap_interval
<= 4 ? swap_interval
: 1;
505 if (swapchain
->swap_interval
== swap_interval
)
508 swapchain
->swap_interval
= swap_interval
;
510 if (!gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
513 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
515 ERR("Failed to set swap interval %u for context %p, last error %#x.\n",
516 swap_interval
, context_gl
, GetLastError());
520 /* Context activation is done by the caller. */
521 static void wined3d_swapchain_gl_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context
*context
)
523 struct wined3d_texture_sub_resource
*sub_resource
;
524 struct wined3d_texture_gl
*texture
, *texture_prev
;
525 struct gl_texture tex0
;
529 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
531 if (swapchain
->state
.desc
.backbuffer_count
< 2 || !swapchain
->render_to_fbo
)
534 texture_prev
= wined3d_texture_gl(swapchain
->back_buffers
[0]);
536 /* Back buffer 0 is already in the draw binding. */
537 tex0
= texture_prev
->texture_rgb
;
538 rb0
= texture_prev
->rb_multisample
;
539 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
541 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
543 texture
= wined3d_texture_gl(swapchain
->back_buffers
[i
]);
544 sub_resource
= &texture
->t
.sub_resources
[0];
546 if (!(sub_resource
->locations
& supported_locations
))
547 wined3d_texture_load_location(&texture
->t
, 0, context
, texture
->t
.resource
.draw_binding
);
549 texture_prev
->texture_rgb
= texture
->texture_rgb
;
550 texture_prev
->rb_multisample
= texture
->rb_multisample
;
552 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
553 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
555 texture_prev
= texture
;
558 texture_prev
->texture_rgb
= tex0
;
559 texture_prev
->rb_multisample
= rb0
;
561 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
562 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
564 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
567 static void swapchain_gl_present(struct wined3d_swapchain
*swapchain
,
568 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, DWORD flags
)
570 struct wined3d_swapchain_gl
*swapchain_gl
= wined3d_swapchain_gl(swapchain
);
571 const struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
572 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
573 const struct wined3d_gl_info
*gl_info
;
574 struct wined3d_context_gl
*context_gl
;
575 struct wined3d_context
*context
;
578 context
= context_acquire(swapchain
->device
, swapchain
->front_buffer
, 0);
579 context_gl
= wined3d_context_gl(context
);
580 if (!context_gl
->valid
)
582 context_release(context
);
583 WARN("Invalid context, skipping present.\n");
587 gl_info
= context_gl
->gl_info
;
589 swapchain_gl_set_swap_interval(swapchain
, context_gl
, swap_interval
);
591 TRACE("Presenting DC %p.\n", context_gl
->dc
);
593 if (context_gl
->dc
== swapchain_gl
->backup_dc
)
594 swapchain_blit_gdi(swapchain
, context
, src_rect
, dst_rect
);
596 if (!(render_to_fbo
= swapchain
->render_to_fbo
)
597 && (src_rect
->left
|| src_rect
->top
598 || src_rect
->right
!= desc
->backbuffer_width
599 || src_rect
->bottom
!= desc
->backbuffer_height
600 || dst_rect
->left
|| dst_rect
->top
601 || dst_rect
->right
!= desc
->backbuffer_width
602 || dst_rect
->bottom
!= desc
->backbuffer_height
))
603 render_to_fbo
= TRUE
;
605 /* Rendering to a window of different size, presenting partial rectangles,
606 * or rendering to a different window needs help from FBO_blit or a textured
607 * draw. Render the swapchain to a FBO in the future.
609 * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
610 * all these issues - this fails if the window is smaller than the backbuffer.
612 if (!swapchain
->render_to_fbo
&& render_to_fbo
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
614 wined3d_texture_load_location(back_buffer
, 0, context
, WINED3D_LOCATION_TEXTURE_RGB
);
615 wined3d_texture_invalidate_location(back_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
616 swapchain
->render_to_fbo
= TRUE
;
617 swapchain_update_draw_bindings(swapchain
);
621 wined3d_texture_load_location(back_buffer
, 0, context
, back_buffer
->resource
.draw_binding
);
624 if (swapchain
->render_to_fbo
)
625 swapchain_blit(swapchain
, context
, src_rect
, dst_rect
);
627 if (swapchain_gl
->context_count
> 1)
628 gl_info
->gl_ops
.gl
.p_glFinish();
630 /* call wglSwapBuffers through the gl table to avoid confusing the Steam overlay */
631 gl_info
->gl_ops
.wgl
.p_wglSwapBuffers(context_gl
->dc
);
632 wined3d_context_gl_submit_command_fence(context_gl
);
634 wined3d_swapchain_gl_rotate(swapchain
, context
);
636 TRACE("SwapBuffers called, Starting new frame\n");
638 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
639 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
641 context_release(context
);
644 static void swapchain_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
646 struct wined3d_texture
*front_buffer
= swapchain
->front_buffer
;
647 struct wined3d_context
*context
;
649 context
= context_acquire(swapchain
->device
, front_buffer
, 0);
650 wined3d_texture_load_location(front_buffer
, 0, context
, front_buffer
->resource
.draw_binding
);
651 context_release(context
);
652 SetRectEmpty(&swapchain
->front_buffer_update
);
655 static const struct wined3d_swapchain_ops swapchain_gl_ops
=
657 swapchain_gl_present
,
658 swapchain_frontbuffer_updated
,
661 static bool wined3d_swapchain_vk_present_mode_supported(struct wined3d_swapchain_vk
*swapchain_vk
,
662 VkPresentModeKHR vk_present_mode
)
664 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
665 const struct wined3d_vk_info
*vk_info
;
666 struct wined3d_adapter_vk
*adapter_vk
;
667 VkPhysicalDevice vk_physical_device
;
668 VkPresentModeKHR
*vk_modes
;
669 bool supported
= false;
673 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
674 vk_physical_device
= adapter_vk
->physical_device
;
675 vk_info
= &adapter_vk
->vk_info
;
677 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
678 swapchain_vk
->vk_surface
, &count
, NULL
))) < 0)
680 ERR("Failed to get supported present mode count, vr %s.\n", wined3d_debug_vkresult(vr
));
684 if (!(vk_modes
= heap_calloc(count
, sizeof(*vk_modes
))))
687 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device
,
688 swapchain_vk
->vk_surface
, &count
, vk_modes
))) < 0)
690 ERR("Failed to get supported present modes, vr %s.\n", wined3d_debug_vkresult(vr
));
694 for (i
= 0; i
< count
; ++i
)
696 if (vk_modes
[i
] == vk_present_mode
)
708 static VkFormat
get_swapchain_fallback_format(VkFormat vk_format
)
712 case VK_FORMAT_R8G8B8A8_SRGB
:
713 return VK_FORMAT_B8G8R8A8_SRGB
;
714 case VK_FORMAT_R8G8B8A8_UNORM
:
715 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
716 case VK_FORMAT_R16G16B16A16_SFLOAT
:
717 return VK_FORMAT_B8G8R8A8_UNORM
;
719 WARN("Unhandled format %#x.\n", vk_format
);
720 return VK_FORMAT_UNDEFINED
;
724 static VkFormat
wined3d_swapchain_vk_select_vk_format(struct wined3d_swapchain_vk
*swapchain_vk
,
725 VkSurfaceKHR vk_surface
)
727 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
728 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
729 const struct wined3d_vk_info
*vk_info
;
730 struct wined3d_adapter_vk
*adapter_vk
;
731 const struct wined3d_format
*format
;
732 VkPhysicalDevice vk_physical_device
;
733 VkSurfaceFormatKHR
*vk_formats
;
734 uint32_t format_count
, i
;
738 adapter_vk
= wined3d_adapter_vk(device_vk
->d
.adapter
);
739 vk_physical_device
= adapter_vk
->physical_device
;
740 vk_info
= &adapter_vk
->vk_info
;
742 if ((format
= wined3d_get_format(&adapter_vk
->a
, desc
->backbuffer_format
, WINED3D_BIND_RENDER_TARGET
)))
743 vk_format
= wined3d_format_vk(format
)->vk_format
;
745 vk_format
= VK_FORMAT_B8G8R8A8_UNORM
;
747 vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
, vk_surface
, &format_count
, NULL
));
748 if (vr
< 0 || !format_count
)
750 WARN("Failed to get supported surface format count, vr %s.\n", wined3d_debug_vkresult(vr
));
751 return VK_FORMAT_UNDEFINED
;
754 if (!(vk_formats
= heap_calloc(format_count
, sizeof(*vk_formats
))))
755 return VK_FORMAT_UNDEFINED
;
757 if ((vr
= VK_CALL(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device
,
758 vk_surface
, &format_count
, vk_formats
))) < 0)
760 WARN("Failed to get supported surface formats, vr %s.\n", wined3d_debug_vkresult(vr
));
761 heap_free(vk_formats
);
762 return VK_FORMAT_UNDEFINED
;
765 for (i
= 0; i
< format_count
; ++i
)
767 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
770 if (i
== format_count
)
772 /* Try to create a swapchain with format conversion. */
773 vk_format
= get_swapchain_fallback_format(vk_format
);
774 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
775 for (i
= 0; i
< format_count
; ++i
)
777 if (vk_formats
[i
].format
== vk_format
&& vk_formats
[i
].colorSpace
== VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
)
781 heap_free(vk_formats
);
782 if (i
== format_count
)
784 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_d3dformat(desc
->backbuffer_format
));
785 return VK_FORMAT_UNDEFINED
;
788 TRACE("Using Vulkan swapchain format %#x.\n", vk_format
);
793 static bool wined3d_swapchain_vk_create_vulkan_swapchain_images(struct wined3d_swapchain_vk
*swapchain_vk
,
794 VkSwapchainKHR vk_swapchain
)
796 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
797 const struct wined3d_vk_info
*vk_info
;
798 VkSemaphoreCreateInfo semaphore_info
;
799 uint32_t image_count
, i
;
802 vk_info
= &wined3d_adapter_vk(device_vk
->d
.adapter
)->vk_info
;
804 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
, vk_swapchain
, &image_count
, NULL
))) < 0)
806 ERR("Failed to get image count, vr %s\n", wined3d_debug_vkresult(vr
));
810 if (!(swapchain_vk
->vk_images
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_images
))))
812 ERR("Failed to allocate images array.\n");
816 if ((vr
= VK_CALL(vkGetSwapchainImagesKHR(device_vk
->vk_device
,
817 vk_swapchain
, &image_count
, swapchain_vk
->vk_images
))) < 0)
819 ERR("Failed to get swapchain images, vr %s.\n", wined3d_debug_vkresult(vr
));
820 heap_free(swapchain_vk
->vk_images
);
824 if (!(swapchain_vk
->vk_semaphores
= heap_calloc(image_count
, sizeof(*swapchain_vk
->vk_semaphores
))))
826 ERR("Failed to allocate semaphores array.\n");
827 heap_free(swapchain_vk
->vk_images
);
831 semaphore_info
.sType
= VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
;
832 semaphore_info
.pNext
= NULL
;
833 semaphore_info
.flags
= 0;
834 for (i
= 0; i
< image_count
; ++i
)
836 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
837 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].available
))) < 0)
839 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
843 if ((vr
= VK_CALL(vkCreateSemaphore(device_vk
->vk_device
,
844 &semaphore_info
, NULL
, &swapchain_vk
->vk_semaphores
[i
].presentable
))) < 0)
846 ERR("Failed to create semaphore, vr %s.\n", wined3d_debug_vkresult(vr
));
850 swapchain_vk
->image_count
= image_count
;
855 for (i
= 0; i
< image_count
; ++i
)
857 if (swapchain_vk
->vk_semaphores
[i
].available
)
858 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].available
, NULL
));
859 if (swapchain_vk
->vk_semaphores
[i
].presentable
)
860 VK_CALL(vkDestroySemaphore(device_vk
->vk_device
, swapchain_vk
->vk_semaphores
[i
].presentable
, NULL
));
862 heap_free(swapchain_vk
->vk_semaphores
);
863 heap_free(swapchain_vk
->vk_images
);
867 static HRESULT
wined3d_swapchain_vk_create_vulkan_swapchain(struct wined3d_swapchain_vk
*swapchain_vk
)
869 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
870 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
871 VkSwapchainCreateInfoKHR vk_swapchain_desc
;
872 VkWin32SurfaceCreateInfoKHR surface_desc
;
873 unsigned int width
, height
, image_count
;
874 const struct wined3d_vk_info
*vk_info
;
875 VkSurfaceCapabilitiesKHR surface_caps
;
876 struct wined3d_adapter_vk
*adapter_vk
;
877 VkPresentModeKHR vk_present_mode
;
878 VkSwapchainKHR vk_swapchain
;
879 VkImageUsageFlags usage
;
880 VkSurfaceKHR vk_surface
;
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 width
= desc
->backbuffer_width
;
931 if (width
< surface_caps
.minImageExtent
.width
)
932 width
= surface_caps
.minImageExtent
.width
;
933 else if (width
> surface_caps
.maxImageExtent
.width
)
934 width
= surface_caps
.maxImageExtent
.width
;
936 height
= desc
->backbuffer_height
;
937 if (height
< surface_caps
.minImageExtent
.height
)
938 height
= surface_caps
.minImageExtent
.height
;
939 else if (height
> surface_caps
.maxImageExtent
.height
)
940 height
= surface_caps
.maxImageExtent
.height
;
942 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
943 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
944 desc
->backbuffer_width
, desc
->backbuffer_height
,
945 surface_caps
.minImageExtent
.width
, surface_caps
.maxImageExtent
.width
,
946 surface_caps
.minImageExtent
.height
, surface_caps
.maxImageExtent
.height
);
948 usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
949 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
;
950 usage
|= surface_caps
.supportedUsageFlags
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
;
951 if (!(usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) || !(usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
))
952 WARN("Transfer not supported for swapchain images.\n");
954 if (!(surface_caps
.supportedCompositeAlpha
& VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
))
956 FIXME("Unsupported alpha mode, %#x.\n", surface_caps
.supportedCompositeAlpha
);
960 vk_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
961 if (!swapchain_vk
->s
.swap_interval
)
963 if (wined3d_swapchain_vk_present_mode_supported(swapchain_vk
, VK_PRESENT_MODE_IMMEDIATE_KHR
))
964 vk_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
966 FIXME("Unsupported swap interval %u.\n", swapchain_vk
->s
.swap_interval
);
969 vk_swapchain_desc
.sType
= VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
;
970 vk_swapchain_desc
.pNext
= NULL
;
971 vk_swapchain_desc
.flags
= 0;
972 vk_swapchain_desc
.surface
= vk_surface
;
973 vk_swapchain_desc
.minImageCount
= image_count
;
974 vk_swapchain_desc
.imageFormat
= vk_format
;
975 vk_swapchain_desc
.imageColorSpace
= VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
;
976 vk_swapchain_desc
.imageExtent
.width
= width
;
977 vk_swapchain_desc
.imageExtent
.height
= height
;
978 vk_swapchain_desc
.imageArrayLayers
= 1;
979 vk_swapchain_desc
.imageUsage
= usage
;
980 vk_swapchain_desc
.imageSharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
981 vk_swapchain_desc
.queueFamilyIndexCount
= 0;
982 vk_swapchain_desc
.pQueueFamilyIndices
= NULL
;
983 vk_swapchain_desc
.preTransform
= surface_caps
.currentTransform
;
984 vk_swapchain_desc
.compositeAlpha
= VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
;
985 vk_swapchain_desc
.presentMode
= vk_present_mode
;
986 vk_swapchain_desc
.clipped
= VK_TRUE
;
987 vk_swapchain_desc
.oldSwapchain
= VK_NULL_HANDLE
;
988 if ((vr
= VK_CALL(vkCreateSwapchainKHR(device_vk
->vk_device
, &vk_swapchain_desc
, NULL
, &vk_swapchain
))) < 0)
990 ERR("Failed to create Vulkan swapchain, vr %s.\n", wined3d_debug_vkresult(vr
));
993 swapchain_vk
->vk_swapchain
= vk_swapchain
;
995 if (!wined3d_swapchain_vk_create_vulkan_swapchain_images(swapchain_vk
, vk_swapchain
))
997 VK_CALL(vkDestroySwapchainKHR(device_vk
->vk_device
, vk_swapchain
, NULL
));
1001 swapchain_vk
->width
= width
;
1002 swapchain_vk
->height
= height
;
1007 VK_CALL(vkDestroySurfaceKHR(vk_info
->instance
, vk_surface
, NULL
));
1011 static HRESULT
wined3d_swapchain_vk_recreate(struct wined3d_swapchain_vk
*swapchain_vk
)
1013 TRACE("swapchain_vk %p.\n", swapchain_vk
);
1015 wined3d_swapchain_vk_destroy_vulkan_swapchain(swapchain_vk
);
1017 return wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
);
1020 static void wined3d_swapchain_vk_set_swap_interval(struct wined3d_swapchain_vk
*swapchain_vk
,
1021 unsigned int swap_interval
)
1023 if (swap_interval
> 1)
1025 if (swap_interval
<= 4)
1026 FIXME("Unsupported swap interval %u.\n", swap_interval
);
1030 if (swapchain_vk
->s
.swap_interval
== swap_interval
)
1033 swapchain_vk
->s
.swap_interval
= swap_interval
;
1034 wined3d_swapchain_vk_recreate(swapchain_vk
);
1037 static VkResult
wined3d_swapchain_vk_blit(struct wined3d_swapchain_vk
*swapchain_vk
,
1038 struct wined3d_context_vk
*context_vk
, const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
)
1040 struct wined3d_texture_vk
*back_buffer_vk
= wined3d_texture_vk(swapchain_vk
->s
.back_buffers
[0]);
1041 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(swapchain_vk
->s
.device
);
1042 const struct wined3d_swapchain_desc
*desc
= &swapchain_vk
->s
.state
.desc
;
1043 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1044 VkCommandBuffer vk_command_buffer
;
1045 VkImageSubresourceRange vk_range
;
1046 VkPresentInfoKHR present_desc
;
1047 unsigned int present_idx
;
1048 VkImageLayout vk_layout
;
1055 static const VkPipelineStageFlags wait_stage
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
;
1057 TRACE("swapchain_vk %p, context_vk %p, src_rect %s, dst_rect %s, swap_interval %u.\n",
1058 swapchain_vk
, context_vk
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
), swap_interval
);
1060 wined3d_swapchain_vk_set_swap_interval(swapchain_vk
, swap_interval
);
1062 present_idx
= swapchain_vk
->current
++ % swapchain_vk
->image_count
;
1063 wined3d_context_vk_wait_command_buffer(context_vk
, swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
);
1064 if ((vr
= VK_CALL(vkAcquireNextImageKHR(device_vk
->vk_device
, swapchain_vk
->vk_swapchain
, UINT64_MAX
,
1065 swapchain_vk
->vk_semaphores
[present_idx
].available
, VK_NULL_HANDLE
, &image_idx
))) < 0)
1067 WARN("Failed to acquire image, vr %s.\n", wined3d_debug_vkresult(vr
));
1071 if (dst_rect
->right
> swapchain_vk
->width
|| dst_rect
->bottom
> swapchain_vk
->height
)
1073 dst_rect_tmp
= *dst_rect
;
1074 if (dst_rect
->right
> swapchain_vk
->width
)
1075 dst_rect_tmp
.right
= swapchain_vk
->width
;
1076 if (dst_rect
->bottom
> swapchain_vk
->height
)
1077 dst_rect_tmp
.bottom
= swapchain_vk
->height
;
1078 dst_rect
= &dst_rect_tmp
;
1080 filter
= src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
1081 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
1082 ? VK_FILTER_LINEAR
: VK_FILTER_NEAREST
;
1083 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
1085 wined3d_context_vk_end_current_render_pass(context_vk
);
1087 vk_range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1088 vk_range
.baseMipLevel
= 0;
1089 vk_range
.levelCount
= 1;
1090 vk_range
.baseArrayLayer
= 0;
1091 vk_range
.layerCount
= 1;
1093 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1094 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1095 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1096 VK_ACCESS_TRANSFER_READ_BIT
,
1097 back_buffer_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1098 back_buffer_vk
->image
.vk_image
, &vk_range
);
1100 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1101 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1102 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
1103 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1104 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1106 blit
.srcSubresource
.aspectMask
= vk_range
.aspectMask
;
1107 blit
.srcSubresource
.mipLevel
= vk_range
.baseMipLevel
;
1108 blit
.srcSubresource
.baseArrayLayer
= vk_range
.baseArrayLayer
;
1109 blit
.srcSubresource
.layerCount
= vk_range
.layerCount
;
1110 blit
.srcOffsets
[0].x
= src_rect
->left
;
1111 blit
.srcOffsets
[0].y
= src_rect
->top
;
1112 blit
.srcOffsets
[0].z
= 0;
1113 blit
.srcOffsets
[1].x
= src_rect
->right
;
1114 blit
.srcOffsets
[1].y
= src_rect
->bottom
;
1115 blit
.srcOffsets
[1].z
= 1;
1116 blit
.dstSubresource
= blit
.srcSubresource
;
1117 blit
.dstOffsets
[0].x
= dst_rect
->left
;
1118 blit
.dstOffsets
[0].y
= dst_rect
->top
;
1119 blit
.dstOffsets
[0].z
= 0;
1120 blit
.dstOffsets
[1].x
= dst_rect
->right
;
1121 blit
.dstOffsets
[1].y
= dst_rect
->bottom
;
1122 blit
.dstOffsets
[1].z
= 1;
1123 VK_CALL(vkCmdBlitImage(vk_command_buffer
,
1124 back_buffer_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1125 swapchain_vk
->vk_images
[image_idx
], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1128 wined3d_context_vk_reference_texture(context_vk
, back_buffer_vk
);
1129 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1130 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
,
1131 VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
1132 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
1133 swapchain_vk
->vk_images
[image_idx
], &vk_range
);
1135 if (desc
->swap_effect
== WINED3D_SWAP_EFFECT_DISCARD
|| desc
->swap_effect
== WINED3D_SWAP_EFFECT_FLIP_DISCARD
)
1136 vk_layout
= VK_IMAGE_LAYOUT_UNDEFINED
;
1138 vk_layout
= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
;
1139 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1140 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1141 VK_ACCESS_TRANSFER_READ_BIT
,
1142 vk_access_mask_from_bind_flags(back_buffer_vk
->t
.resource
.bind_flags
),
1143 vk_layout
, back_buffer_vk
->layout
,
1144 back_buffer_vk
->image
.vk_image
, &vk_range
);
1145 back_buffer_vk
->bind_mask
= 0;
1147 swapchain_vk
->vk_semaphores
[present_idx
].command_buffer_id
= context_vk
->current_command_buffer
.id
;
1148 wined3d_context_vk_submit_command_buffer(context_vk
,
1149 1, &swapchain_vk
->vk_semaphores
[present_idx
].available
, &wait_stage
,
1150 1, &swapchain_vk
->vk_semaphores
[present_idx
].presentable
);
1152 present_desc
.sType
= VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
;
1153 present_desc
.pNext
= NULL
;
1154 present_desc
.waitSemaphoreCount
= 1;
1155 present_desc
.pWaitSemaphores
= &swapchain_vk
->vk_semaphores
[present_idx
].presentable
;
1156 present_desc
.swapchainCount
= 1;
1157 present_desc
.pSwapchains
= &swapchain_vk
->vk_swapchain
;
1158 present_desc
.pImageIndices
= &image_idx
;
1159 present_desc
.pResults
= NULL
;
1160 if ((vr
= VK_CALL(vkQueuePresentKHR(device_vk
->vk_queue
, &present_desc
))))
1161 WARN("Present returned vr %s.\n", wined3d_debug_vkresult(vr
));
1165 static void wined3d_swapchain_vk_rotate(struct wined3d_swapchain
*swapchain
, struct wined3d_context_vk
*context_vk
)
1167 struct wined3d_texture_sub_resource
*sub_resource
;
1168 struct wined3d_texture_vk
*texture
, *texture_prev
;
1169 struct wined3d_image_vk image0
;
1170 VkDescriptorImageInfo vk_info0
;
1171 VkImageLayout vk_layout0
;
1175 static const DWORD supported_locations
= WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_RB_MULTISAMPLE
;
1177 if (swapchain
->state
.desc
.backbuffer_count
< 2)
1180 texture_prev
= wined3d_texture_vk(swapchain
->back_buffers
[0]);
1182 /* Back buffer 0 is already in the draw binding. */
1183 image0
= texture_prev
->image
;
1184 vk_layout0
= texture_prev
->layout
;
1185 vk_info0
= texture_prev
->default_image_info
;
1186 locations0
= texture_prev
->t
.sub_resources
[0].locations
;
1188 for (i
= 1; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1190 texture
= wined3d_texture_vk(swapchain
->back_buffers
[i
]);
1191 sub_resource
= &texture
->t
.sub_resources
[0];
1193 if (!(sub_resource
->locations
& supported_locations
))
1194 wined3d_texture_load_location(&texture
->t
, 0, &context_vk
->c
, texture
->t
.resource
.draw_binding
);
1196 texture_prev
->image
= texture
->image
;
1197 texture_prev
->layout
= texture
->layout
;
1198 texture_prev
->default_image_info
= texture
->default_image_info
;
1200 wined3d_texture_validate_location(&texture_prev
->t
, 0, sub_resource
->locations
& supported_locations
);
1201 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(sub_resource
->locations
& supported_locations
));
1203 texture_prev
= texture
;
1206 texture_prev
->image
= image0
;
1207 texture_prev
->layout
= vk_layout0
;
1208 texture_prev
->default_image_info
= vk_info0
;
1210 wined3d_texture_validate_location(&texture_prev
->t
, 0, locations0
& supported_locations
);
1211 wined3d_texture_invalidate_location(&texture_prev
->t
, 0, ~(locations0
& supported_locations
));
1213 device_invalidate_state(swapchain
->device
, STATE_FRAMEBUFFER
);
1216 static void swapchain_vk_present(struct wined3d_swapchain
*swapchain
, const RECT
*src_rect
,
1217 const RECT
*dst_rect
, unsigned int swap_interval
, uint32_t flags
)
1219 struct wined3d_swapchain_vk
*swapchain_vk
= wined3d_swapchain_vk(swapchain
);
1220 struct wined3d_texture
*back_buffer
= swapchain
->back_buffers
[0];
1221 struct wined3d_context_vk
*context_vk
;
1225 context_vk
= wined3d_context_vk(context_acquire(swapchain
->device
, back_buffer
, 0));
1227 wined3d_texture_load_location(back_buffer
, 0, &context_vk
->c
, back_buffer
->resource
.draw_binding
);
1229 if (swapchain_vk
->vk_swapchain
)
1231 if ((vr
= wined3d_swapchain_vk_blit(swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1233 if (vr
== VK_ERROR_OUT_OF_DATE_KHR
|| vr
== VK_SUBOPTIMAL_KHR
)
1235 if (FAILED(hr
= wined3d_swapchain_vk_recreate(swapchain_vk
)))
1236 ERR("Failed to recreate swapchain, hr %#x.\n", hr
);
1237 else if (vr
== VK_ERROR_OUT_OF_DATE_KHR
&& (vr
= wined3d_swapchain_vk_blit(
1238 swapchain_vk
, context_vk
, src_rect
, dst_rect
, swap_interval
)))
1239 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1243 ERR("Failed to blit image, vr %s.\n", wined3d_debug_vkresult(vr
));
1249 swapchain_blit_gdi(swapchain
, &context_vk
->c
, src_rect
, dst_rect
);
1252 wined3d_swapchain_vk_rotate(swapchain
, context_vk
);
1254 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1255 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1257 TRACE("Starting new frame.\n");
1259 context_release(&context_vk
->c
);
1262 static const struct wined3d_swapchain_ops swapchain_vk_ops
=
1264 swapchain_vk_present
,
1265 swapchain_frontbuffer_updated
,
1268 static void swapchain_gdi_frontbuffer_updated(struct wined3d_swapchain
*swapchain
)
1270 struct wined3d_dc_info
*front
;
1271 POINT offset
= {0, 0};
1276 TRACE("swapchain %p.\n", swapchain
);
1278 front
= &swapchain
->front_buffer
->dc_info
[0];
1279 if (swapchain
->palette
)
1280 wined3d_palette_apply_to_dc(swapchain
->palette
, front
->dc
);
1282 if (swapchain
->front_buffer
->resource
.map_count
)
1283 ERR("Trying to blit a mapped surface.\n");
1285 TRACE("Copying surface %p to screen.\n", front
);
1288 window
= swapchain
->win_handle
;
1289 dst_dc
= GetDCEx(window
, 0, DCX_CLIPSIBLINGS
| DCX_CACHE
);
1291 /* Front buffer coordinates are screen coordinates. Map them to the
1292 * destination window if not fullscreened. */
1293 if (swapchain
->state
.desc
.windowed
)
1294 ClientToScreen(window
, &offset
);
1296 TRACE("offset %s.\n", wine_dbgstr_point(&offset
));
1298 SetRect(&draw_rect
, 0, 0, swapchain
->front_buffer
->resource
.width
,
1299 swapchain
->front_buffer
->resource
.height
);
1300 IntersectRect(&draw_rect
, &draw_rect
, &swapchain
->front_buffer_update
);
1302 BitBlt(dst_dc
, draw_rect
.left
- offset
.x
, draw_rect
.top
- offset
.y
,
1303 draw_rect
.right
- draw_rect
.left
, draw_rect
.bottom
- draw_rect
.top
,
1304 src_dc
, draw_rect
.left
, draw_rect
.top
, SRCCOPY
);
1305 ReleaseDC(window
, dst_dc
);
1307 SetRectEmpty(&swapchain
->front_buffer_update
);
1310 static void swapchain_gdi_present(struct wined3d_swapchain
*swapchain
,
1311 const RECT
*src_rect
, const RECT
*dst_rect
, unsigned int swap_interval
, DWORD flags
)
1313 struct wined3d_dc_info
*front
, *back
;
1318 front
= &swapchain
->front_buffer
->dc_info
[0];
1319 back
= &swapchain
->back_buffers
[0]->dc_info
[0];
1321 /* Flip the surface data. */
1323 bitmap
= front
->bitmap
;
1324 data
= swapchain
->front_buffer
->resource
.heap_memory
;
1326 front
->dc
= back
->dc
;
1327 front
->bitmap
= back
->bitmap
;
1328 swapchain
->front_buffer
->resource
.heap_memory
= swapchain
->back_buffers
[0]->resource
.heap_memory
;
1331 back
->bitmap
= bitmap
;
1332 swapchain
->back_buffers
[0]->resource
.heap_memory
= data
;
1334 SetRect(&swapchain
->front_buffer_update
, 0, 0,
1335 swapchain
->front_buffer
->resource
.width
,
1336 swapchain
->front_buffer
->resource
.height
);
1337 swapchain_gdi_frontbuffer_updated(swapchain
);
1340 static const struct wined3d_swapchain_ops swapchain_no3d_ops
=
1342 swapchain_gdi_present
,
1343 swapchain_gdi_frontbuffer_updated
,
1346 static void swapchain_update_render_to_fbo(struct wined3d_swapchain
*swapchain
)
1348 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
1351 if (!swapchain
->state
.desc
.backbuffer_count
)
1353 TRACE("Single buffered rendering.\n");
1354 swapchain
->render_to_fbo
= FALSE
;
1358 TRACE("Rendering to FBO.\n");
1359 swapchain
->render_to_fbo
= TRUE
;
1362 static void wined3d_swapchain_apply_sample_count_override(const struct wined3d_swapchain
*swapchain
,
1363 enum wined3d_format_id format_id
, enum wined3d_multisample_type
*type
, DWORD
*quality
)
1365 const struct wined3d_adapter
*adapter
;
1366 const struct wined3d_gl_info
*gl_info
;
1367 const struct wined3d_format
*format
;
1368 enum wined3d_multisample_type t
;
1370 if (wined3d_settings
.sample_count
== ~0u)
1373 adapter
= swapchain
->device
->adapter
;
1374 gl_info
= &adapter
->gl_info
;
1375 if (!(format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
)))
1378 if ((t
= min(wined3d_settings
.sample_count
, gl_info
->limits
.samples
)))
1379 while (!(format
->multisample_types
& 1u << (t
- 1)))
1381 TRACE("Using sample count %u.\n", t
);
1386 void swapchain_set_max_frame_latency(struct wined3d_swapchain
*swapchain
, const struct wined3d_device
*device
)
1388 /* Subtract 1 for the implicit OpenGL latency. */
1389 swapchain
->max_frame_latency
= device
->max_frame_latency
>= 2 ? device
->max_frame_latency
- 1 : 1;
1392 static enum wined3d_format_id
adapter_format_from_backbuffer_format(const struct wined3d_adapter
*adapter
,
1393 enum wined3d_format_id format_id
)
1395 const struct wined3d_format
*backbuffer_format
;
1397 backbuffer_format
= wined3d_get_format(adapter
, format_id
, WINED3D_BIND_RENDER_TARGET
);
1398 return pixelformat_for_depth(backbuffer_format
->byte_count
* CHAR_BIT
);
1401 static HRESULT
wined3d_swapchain_state_init(struct wined3d_swapchain_state
*state
,
1402 const struct wined3d_swapchain_desc
*desc
, HWND window
, struct wined3d
*wined3d
,
1403 struct wined3d_swapchain_state_parent
*parent
)
1407 state
->desc
= *desc
;
1409 if (FAILED(hr
= wined3d_output_get_display_mode(desc
->output
, &state
->original_mode
, NULL
)))
1411 ERR("Failed to get current display mode, hr %#x.\n", hr
);
1415 if (!desc
->windowed
)
1417 if (desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1419 state
->d3d_mode
.width
= desc
->backbuffer_width
;
1420 state
->d3d_mode
.height
= desc
->backbuffer_height
;
1421 state
->d3d_mode
.format_id
= adapter_format_from_backbuffer_format(desc
->output
->adapter
,
1422 desc
->backbuffer_format
);
1423 state
->d3d_mode
.refresh_rate
= desc
->refresh_rate
;
1424 state
->d3d_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
1428 state
->d3d_mode
= state
->original_mode
;
1432 GetWindowRect(window
, &state
->original_window_rect
);
1433 state
->wined3d
= wined3d
;
1434 state
->device_window
= window
;
1435 state
->parent
= parent
;
1437 if (desc
->flags
& WINED3D_SWAPCHAIN_REGISTER_STATE
)
1438 wined3d_swapchain_state_register(state
);
1443 static HRESULT
wined3d_swapchain_init(struct wined3d_swapchain
*swapchain
, struct wined3d_device
*device
,
1444 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1445 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1446 const struct wined3d_swapchain_ops
*swapchain_ops
)
1448 struct wined3d_resource_desc texture_desc
;
1449 struct wined3d_output_desc output_desc
;
1450 BOOL displaymode_set
= FALSE
;
1451 DWORD texture_flags
= 0;
1452 HRESULT hr
= E_FAIL
;
1457 wined3d_mutex_lock();
1459 if (desc
->backbuffer_count
> 1)
1461 FIXME("The application requested more than one back buffer, this is not properly supported.\n"
1462 "Please configure the application to use double buffering (1 back buffer) if possible.\n");
1465 if (desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
1466 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
1467 && desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
1468 FIXME("Unimplemented swap effect %#x.\n", desc
->swap_effect
);
1470 window
= desc
->device_window
? desc
->device_window
: device
->create_parms
.focus_window
;
1471 if (FAILED(hr
= wined3d_swapchain_state_init(&swapchain
->state
, desc
, window
, device
->wined3d
, state_parent
)))
1473 ERR("Failed to initialise swapchain state, hr %#x.\n", hr
);
1474 wined3d_mutex_unlock();
1478 swapchain
->swapchain_ops
= swapchain_ops
;
1479 swapchain
->device
= device
;
1480 swapchain
->parent
= parent
;
1481 swapchain
->parent_ops
= parent_ops
;
1483 swapchain
->win_handle
= window
;
1484 swapchain
->swap_interval
= WINED3D_SWAP_INTERVAL_DEFAULT
;
1485 swapchain_set_max_frame_latency(swapchain
, device
);
1487 GetClientRect(window
, &client_rect
);
1490 TRACE("Client rect %s.\n", wine_dbgstr_rect(&client_rect
));
1492 if (!desc
->backbuffer_width
)
1494 desc
->backbuffer_width
= client_rect
.right
? client_rect
.right
: 8;
1495 TRACE("Updating width to %u.\n", desc
->backbuffer_width
);
1497 if (!desc
->backbuffer_height
)
1499 desc
->backbuffer_height
= client_rect
.bottom
? client_rect
.bottom
: 8;
1500 TRACE("Updating height to %u.\n", desc
->backbuffer_height
);
1503 if (desc
->backbuffer_format
== WINED3DFMT_UNKNOWN
)
1505 desc
->backbuffer_format
= swapchain
->state
.original_mode
.format_id
;
1506 TRACE("Updating format to %s.\n", debug_d3dformat(swapchain
->state
.original_mode
.format_id
));
1511 if (FAILED(hr
= wined3d_output_get_desc(desc
->output
, &output_desc
)))
1513 ERR("Failed to get output description, hr %#x.\n", hr
);
1517 wined3d_swapchain_state_setup_fullscreen(&swapchain
->state
, window
,
1518 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, desc
->backbuffer_width
,
1519 desc
->backbuffer_height
);
1521 swapchain
->state
.desc
= *desc
;
1522 wined3d_swapchain_apply_sample_count_override(swapchain
, swapchain
->state
.desc
.backbuffer_format
,
1523 &swapchain
->state
.desc
.multisample_type
, &swapchain
->state
.desc
.multisample_quality
);
1524 swapchain_update_render_to_fbo(swapchain
);
1526 TRACE("Creating front buffer.\n");
1528 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1529 texture_desc
.format
= swapchain
->state
.desc
.backbuffer_format
;
1530 texture_desc
.multisample_type
= swapchain
->state
.desc
.multisample_type
;
1531 texture_desc
.multisample_quality
= swapchain
->state
.desc
.multisample_quality
;
1532 texture_desc
.usage
= 0;
1533 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1534 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1535 texture_desc
.bind_flags
= 0;
1536 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1537 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1539 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1540 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
)
1541 texture_desc
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
1542 texture_desc
.width
= swapchain
->state
.desc
.backbuffer_width
;
1543 texture_desc
.height
= swapchain
->state
.desc
.backbuffer_height
;
1544 texture_desc
.depth
= 1;
1545 texture_desc
.size
= 0;
1547 if (swapchain
->state
.desc
.flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
1548 texture_flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
1550 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1551 parent
, &texture_desc
, texture_flags
, &swapchain
->front_buffer
)))
1553 WARN("Failed to create front buffer, hr %#x.\n", hr
);
1557 wined3d_texture_set_swapchain(swapchain
->front_buffer
, swapchain
);
1558 if (!(device
->wined3d
->flags
& WINED3D_NO3D
))
1560 wined3d_texture_validate_location(swapchain
->front_buffer
, 0, WINED3D_LOCATION_DRAWABLE
);
1561 wined3d_texture_invalidate_location(swapchain
->front_buffer
, 0, ~WINED3D_LOCATION_DRAWABLE
);
1564 /* MSDN says we're only allowed a single fullscreen swapchain per device,
1565 * so we should really check to see if there is a fullscreen swapchain
1566 * already. Does a single head count as full screen? */
1567 if (!desc
->windowed
&& desc
->flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
1569 /* Change the display settings */
1570 if (FAILED(hr
= wined3d_output_set_display_mode(desc
->output
,
1571 &swapchain
->state
.d3d_mode
)))
1573 WARN("Failed to set display mode, hr %#x.\n", hr
);
1576 displaymode_set
= TRUE
;
1579 if (swapchain
->state
.desc
.backbuffer_count
> 0)
1581 if (!(swapchain
->back_buffers
= heap_calloc(swapchain
->state
.desc
.backbuffer_count
,
1582 sizeof(*swapchain
->back_buffers
))))
1584 ERR("Failed to allocate backbuffer array memory.\n");
1589 texture_desc
.bind_flags
= swapchain
->state
.desc
.backbuffer_bind_flags
;
1590 texture_desc
.usage
= 0;
1591 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1592 texture_desc
.usage
|= WINED3DUSAGE_OWNDC
;
1593 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1595 TRACE("Creating back buffer %u.\n", i
);
1596 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1597 parent
, &texture_desc
, texture_flags
, &swapchain
->back_buffers
[i
])))
1599 WARN("Failed to create back buffer %u, hr %#x.\n", i
, hr
);
1600 swapchain
->state
.desc
.backbuffer_count
= i
;
1603 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], swapchain
);
1607 /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
1608 if (desc
->enable_auto_depth_stencil
)
1610 TRACE("Creating depth/stencil buffer.\n");
1611 if (!device
->auto_depth_stencil_view
)
1613 struct wined3d_view_desc desc
;
1614 struct wined3d_texture
*ds
;
1616 texture_desc
.format
= swapchain
->state
.desc
.auto_depth_stencil_format
;
1617 texture_desc
.usage
= 0;
1618 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
1619 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1620 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_CPU
;
1622 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
1624 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
1625 device
->device_parent
, &texture_desc
, 0, &ds
)))
1627 WARN("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
1631 desc
.format_id
= ds
->resource
.format
->id
;
1633 desc
.u
.texture
.level_idx
= 0;
1634 desc
.u
.texture
.level_count
= 1;
1635 desc
.u
.texture
.layer_idx
= 0;
1636 desc
.u
.texture
.layer_count
= 1;
1637 hr
= wined3d_rendertarget_view_create(&desc
, &ds
->resource
, NULL
, &wined3d_null_parent_ops
,
1638 &device
->auto_depth_stencil_view
);
1639 wined3d_texture_decref(ds
);
1642 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1648 wined3d_swapchain_get_gamma_ramp(swapchain
, &swapchain
->orig_gamma
);
1650 wined3d_mutex_unlock();
1655 if (displaymode_set
)
1657 if (FAILED(wined3d_restore_display_modes(device
->wined3d
)))
1658 ERR("Failed to restore display mode.\n");
1661 if (swapchain
->back_buffers
)
1663 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1665 if (swapchain
->back_buffers
[i
])
1667 wined3d_texture_set_swapchain(swapchain
->back_buffers
[i
], NULL
);
1668 wined3d_texture_decref(swapchain
->back_buffers
[i
]);
1671 heap_free(swapchain
->back_buffers
);
1674 if (swapchain
->front_buffer
)
1676 wined3d_texture_set_swapchain(swapchain
->front_buffer
, NULL
);
1677 wined3d_texture_decref(swapchain
->front_buffer
);
1680 wined3d_swapchain_state_cleanup(&swapchain
->state
);
1681 wined3d_mutex_unlock();
1686 HRESULT
wined3d_swapchain_no3d_init(struct wined3d_swapchain
*swapchain_no3d
, struct wined3d_device
*device
,
1687 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1688 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1690 TRACE("swapchain_no3d %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1691 swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
);
1693 return wined3d_swapchain_init(swapchain_no3d
, device
, desc
, state_parent
, parent
, parent_ops
,
1694 &swapchain_no3d_ops
);
1697 HRESULT
wined3d_swapchain_gl_init(struct wined3d_swapchain_gl
*swapchain_gl
, struct wined3d_device
*device
,
1698 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1699 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1703 TRACE("swapchain_gl %p, device %p, desc %p, state_parent %p, parent %p, parent_ops %p.\n",
1704 swapchain_gl
, device
, desc
, state_parent
, parent
, parent_ops
);
1706 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_gl
->s
, device
, desc
, state_parent
, parent
,
1707 parent_ops
, &swapchain_gl_ops
)))
1709 /* Cleanup any context that may have been created for the swapchain. */
1710 wined3d_cs_destroy_object(device
->cs
, wined3d_swapchain_gl_destroy_object
, swapchain_gl
);
1711 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1717 HRESULT
wined3d_swapchain_vk_init(struct wined3d_swapchain_vk
*swapchain_vk
, struct wined3d_device
*device
,
1718 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1719 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1723 TRACE("swapchain_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
1724 swapchain_vk
, device
, desc
, parent
, parent_ops
);
1726 if (FAILED(hr
= wined3d_swapchain_init(&swapchain_vk
->s
, device
, desc
, state_parent
, parent
,
1727 parent_ops
, &swapchain_vk_ops
)))
1730 if (swapchain_vk
->s
.win_handle
== GetDesktopWindow())
1732 WARN("Creating a desktop window swapchain.\n");
1736 if (FAILED(hr
= wined3d_swapchain_vk_create_vulkan_swapchain(swapchain_vk
)))
1737 WARN("Failed to create a Vulkan swapchain, hr %#x.\n", hr
);
1742 HRESULT CDECL
wined3d_swapchain_create(struct wined3d_device
*device
,
1743 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain_state_parent
*state_parent
,
1744 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1745 struct wined3d_swapchain
**swapchain
)
1747 struct wined3d_swapchain
*object
;
1750 if (FAILED(hr
= device
->adapter
->adapter_ops
->adapter_create_swapchain(device
,
1751 desc
, state_parent
, parent
, parent_ops
, &object
)))
1754 if (desc
->flags
& WINED3D_SWAPCHAIN_IMPLICIT
)
1756 wined3d_mutex_lock();
1757 if (FAILED(hr
= wined3d_device_set_implicit_swapchain(device
, object
)))
1759 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1760 device
->adapter
->adapter_ops
->adapter_destroy_swapchain(object
);
1761 wined3d_mutex_unlock();
1764 wined3d_mutex_unlock();
1767 *swapchain
= object
;
1772 static struct wined3d_context_gl
*wined3d_swapchain_gl_create_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1774 struct wined3d_device
*device
= swapchain_gl
->s
.device
;
1775 struct wined3d_context_gl
*context_gl
;
1777 TRACE("Creating a new context for swapchain %p, thread %u.\n", swapchain_gl
, GetCurrentThreadId());
1779 wined3d_from_cs(device
->cs
);
1781 if (!(context_gl
= heap_alloc_zero(sizeof(*context_gl
))))
1783 ERR("Failed to allocate context memory.\n");
1787 if (FAILED(wined3d_context_gl_init(context_gl
, swapchain_gl
)))
1789 WARN("Failed to initialise context.\n");
1790 heap_free(context_gl
);
1794 if (!device_context_add(device
, &context_gl
->c
))
1796 ERR("Failed to add the newly created context to the context list.\n");
1797 wined3d_context_gl_destroy(context_gl
);
1801 TRACE("Created context %p.\n", context_gl
);
1803 context_release(&context_gl
->c
);
1805 if (!wined3d_array_reserve((void **)&swapchain_gl
->contexts
, &swapchain_gl
->contexts_size
,
1806 swapchain_gl
->context_count
+ 1, sizeof(*swapchain_gl
->contexts
)))
1808 ERR("Failed to allocate new context array memory.\n");
1809 wined3d_context_gl_destroy(context_gl
);
1812 swapchain_gl
->contexts
[swapchain_gl
->context_count
++] = context_gl
;
1817 void wined3d_swapchain_gl_destroy_contexts(struct wined3d_swapchain_gl
*swapchain_gl
)
1821 TRACE("swapchain_gl %p.\n", swapchain_gl
);
1823 for (i
= 0; i
< swapchain_gl
->context_count
; ++i
)
1825 wined3d_context_gl_destroy(swapchain_gl
->contexts
[i
]);
1827 heap_free(swapchain_gl
->contexts
);
1828 swapchain_gl
->contexts_size
= 0;
1829 swapchain_gl
->context_count
= 0;
1830 swapchain_gl
->contexts
= NULL
;
1833 struct wined3d_context_gl
*wined3d_swapchain_gl_get_context(struct wined3d_swapchain_gl
*swapchain_gl
)
1835 DWORD tid
= GetCurrentThreadId();
1838 for (i
= 0; i
< swapchain_gl
->context_count
; ++i
)
1840 if (swapchain_gl
->contexts
[i
]->tid
== tid
)
1841 return swapchain_gl
->contexts
[i
];
1844 /* Create a new context for the thread. */
1845 return wined3d_swapchain_gl_create_context(swapchain_gl
);
1848 HDC
wined3d_swapchain_gl_get_backup_dc(struct wined3d_swapchain_gl
*swapchain_gl
)
1850 if (!swapchain_gl
->backup_dc
)
1852 TRACE("Creating the backup window for swapchain %p.\n", swapchain_gl
);
1854 if (!(swapchain_gl
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
1855 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
)))
1857 ERR("Failed to create a window.\n");
1861 if (!(swapchain_gl
->backup_dc
= GetDC(swapchain_gl
->backup_wnd
)))
1863 ERR("Failed to get a DC.\n");
1864 DestroyWindow(swapchain_gl
->backup_wnd
);
1865 swapchain_gl
->backup_wnd
= NULL
;
1870 return swapchain_gl
->backup_dc
;
1873 void swapchain_update_draw_bindings(struct wined3d_swapchain
*swapchain
)
1877 wined3d_resource_update_draw_binding(&swapchain
->front_buffer
->resource
);
1879 for (i
= 0; i
< swapchain
->state
.desc
.backbuffer_count
; ++i
)
1881 wined3d_resource_update_draw_binding(&swapchain
->back_buffers
[i
]->resource
);
1885 void wined3d_swapchain_activate(struct wined3d_swapchain
*swapchain
, BOOL activate
)
1887 struct wined3d_device
*device
= swapchain
->device
;
1888 HWND window
= swapchain
->state
.device_window
;
1889 struct wined3d_output_desc output_desc
;
1890 unsigned int screensaver_active
;
1891 struct wined3d_output
*output
;
1892 BOOL focus_messages
, filter
;
1895 /* This code is not protected by the wined3d mutex, so it may run while
1896 * wined3d_device_reset is active. Testing on Windows shows that changing
1897 * focus during resets and resetting during focus change events causes
1898 * the application to crash with an invalid memory access. */
1900 if (!(focus_messages
= device
->wined3d
->flags
& WINED3D_FOCUS_MESSAGES
))
1901 filter
= wined3d_filter_messages(window
, TRUE
);
1905 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
1906 if ((device
->restore_screensaver
= !!screensaver_active
))
1907 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
1909 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
))
1911 /* The d3d versions do not agree on the exact messages here. D3d8 restores
1912 * the window but leaves the size untouched, d3d9 sets the size on an
1913 * invisible window, generates messages but doesn't change the window
1914 * properties. The implementation follows d3d9.
1916 * Guild Wars 1 wants a WINDOWPOSCHANGED message on the device window to
1917 * resume drawing after a focus loss. */
1918 output
= wined3d_swapchain_get_output(swapchain
);
1921 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1925 if (SUCCEEDED(hr
= wined3d_output_get_desc(output
, &output_desc
)))
1926 SetWindowPos(window
, NULL
, output_desc
.desktop_rect
.left
,
1927 output_desc
.desktop_rect
.top
, swapchain
->state
.desc
.backbuffer_width
,
1928 swapchain
->state
.desc
.backbuffer_height
, SWP_NOACTIVATE
| SWP_NOZORDER
);
1930 ERR("Failed to get output description, hr %#x.\n", hr
);
1933 if (device
->wined3d
->flags
& WINED3D_RESTORE_MODE_ON_ACTIVATE
)
1935 output
= wined3d_swapchain_get_output(swapchain
);
1938 ERR("Failed to get output from swapchain %p.\n", swapchain
);
1942 if (FAILED(hr
= wined3d_output_set_display_mode(output
,
1943 &swapchain
->state
.d3d_mode
)))
1944 ERR("Failed to set display mode, hr %#x.\n", hr
);
1947 if (swapchain
== device
->swapchains
[0])
1948 device
->device_parent
->ops
->activate(device
->device_parent
, TRUE
);
1952 if (device
->restore_screensaver
)
1954 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
1955 device
->restore_screensaver
= FALSE
;
1958 if (FAILED(hr
= wined3d_restore_display_modes(device
->wined3d
)))
1959 ERR("Failed to restore display modes, hr %#x.\n", hr
);
1961 swapchain
->reapply_mode
= TRUE
;
1963 /* Some DDraw apps (Deus Ex: GOTY, and presumably all UT 1 based games) destroy the device
1964 * during window minimization. Do our housekeeping now, as the device may not exist after
1965 * the ShowWindow call.
1967 * In d3d9, the device is marked lost after the window is minimized. If we find an app
1968 * that needs this behavior (e.g. because it calls TestCooperativeLevel in the window proc)
1969 * we'll have to control this via a create flag. Note that the device and swapchain are not
1970 * safe to access after the ShowWindow call. */
1971 if (swapchain
== device
->swapchains
[0])
1972 device
->device_parent
->ops
->activate(device
->device_parent
, FALSE
);
1974 if (!(device
->create_parms
.flags
& WINED3DCREATE_NOWINDOWCHANGES
) && IsWindowVisible(window
))
1975 ShowWindow(window
, SW_MINIMIZE
);
1978 if (!focus_messages
)
1979 wined3d_filter_messages(window
, filter
);
1982 HRESULT CDECL
wined3d_swapchain_resize_buffers(struct wined3d_swapchain
*swapchain
, unsigned int buffer_count
,
1983 unsigned int width
, unsigned int height
, enum wined3d_format_id format_id
,
1984 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
)
1986 struct wined3d_swapchain_desc
*desc
= &swapchain
->state
.desc
;
1987 BOOL update_desc
= FALSE
;
1989 TRACE("swapchain %p, buffer_count %u, width %u, height %u, format %s, "
1990 "multisample_type %#x, multisample_quality %#x.\n",
1991 swapchain
, buffer_count
, width
, height
, debug_d3dformat(format_id
),
1992 multisample_type
, multisample_quality
);
1994 wined3d_swapchain_apply_sample_count_override(swapchain
, format_id
, &multisample_type
, &multisample_quality
);
1996 if (buffer_count
&& buffer_count
!= desc
->backbuffer_count
)
1997 FIXME("Cannot change the back buffer count yet.\n");
1999 wined3d_cs_finish(swapchain
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
2001 if (!width
|| !height
)
2003 /* The application is requesting that either the swapchain width or
2004 * height be set to the corresponding dimension in the window's
2009 if (!desc
->windowed
)
2010 return WINED3DERR_INVALIDCALL
;
2012 if (!GetClientRect(swapchain
->state
.device_window
, &client_rect
))
2014 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
2015 return WINED3DERR_INVALIDCALL
;
2019 width
= client_rect
.right
;
2022 height
= client_rect
.bottom
;
2025 if (width
!= desc
->backbuffer_width
|| height
!= desc
->backbuffer_height
)
2027 desc
->backbuffer_width
= width
;
2028 desc
->backbuffer_height
= height
;
2032 if (format_id
== WINED3DFMT_UNKNOWN
)
2034 if (!desc
->windowed
)
2035 return WINED3DERR_INVALIDCALL
;
2036 format_id
= swapchain
->state
.original_mode
.format_id
;
2039 if (format_id
!= desc
->backbuffer_format
)
2041 desc
->backbuffer_format
= format_id
;
2045 if (multisample_type
!= desc
->multisample_type
2046 || multisample_quality
!= desc
->multisample_quality
)
2048 desc
->multisample_type
= multisample_type
;
2049 desc
->multisample_quality
= multisample_quality
;
2058 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->front_buffer
, 0, desc
->backbuffer_width
,
2059 desc
->backbuffer_height
, desc
->backbuffer_format
,
2060 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2063 for (i
= 0; i
< desc
->backbuffer_count
; ++i
)
2065 if (FAILED(hr
= wined3d_texture_update_desc(swapchain
->back_buffers
[i
], 0, desc
->backbuffer_width
,
2066 desc
->backbuffer_height
, desc
->backbuffer_format
,
2067 desc
->multisample_type
, desc
->multisample_quality
, NULL
, 0)))
2072 swapchain_update_render_to_fbo(swapchain
);
2073 swapchain_update_draw_bindings(swapchain
);
2078 static HRESULT
wined3d_swapchain_state_set_display_mode(struct wined3d_swapchain_state
*state
,
2079 struct wined3d_output
*output
, struct wined3d_display_mode
*mode
)
2083 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_USE_CLOSEST_MATCHING_MODE
)
2085 if (FAILED(hr
= wined3d_output_find_closest_matching_mode(output
, mode
)))
2087 WARN("Failed to find closest matching mode, hr %#x.\n", hr
);
2091 if (output
!= state
->desc
.output
)
2093 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2095 WARN("Failed to restore display modes, hr %#x.\n", hr
);
2099 if (FAILED(hr
= wined3d_output_get_display_mode(output
, &state
->original_mode
, NULL
)))
2101 WARN("Failed to get current display mode, hr %#x.\n", hr
);
2106 if (FAILED(hr
= wined3d_output_set_display_mode(output
, mode
)))
2108 WARN("Failed to set display mode, hr %#x.\n", hr
);
2109 return WINED3DERR_INVALIDCALL
;
2115 HRESULT CDECL
wined3d_swapchain_state_resize_target(struct wined3d_swapchain_state
*state
,
2116 const struct wined3d_display_mode
*mode
)
2118 struct wined3d_display_mode actual_mode
;
2119 struct wined3d_output_desc output_desc
;
2120 RECT original_window_rect
, window_rect
;
2121 int x
, y
, width
, height
;
2125 TRACE("state %p, mode %p.\n", state
, mode
);
2127 wined3d_mutex_lock();
2129 window
= state
->device_window
;
2131 if (state
->desc
.windowed
)
2133 SetRect(&window_rect
, 0, 0, mode
->width
, mode
->height
);
2134 AdjustWindowRectEx(&window_rect
,
2135 GetWindowLongW(window
, GWL_STYLE
), FALSE
,
2136 GetWindowLongW(window
, GWL_EXSTYLE
));
2137 GetWindowRect(window
, &original_window_rect
);
2139 x
= original_window_rect
.left
;
2140 y
= original_window_rect
.top
;
2141 width
= window_rect
.right
- window_rect
.left
;
2142 height
= window_rect
.bottom
- window_rect
.top
;
2146 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2148 actual_mode
= *mode
;
2149 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, state
->desc
.output
,
2152 ERR("Failed to set display mode, hr %#x.\n", hr
);
2153 wined3d_mutex_unlock();
2158 if (FAILED(hr
= wined3d_output_get_desc(state
->desc
.output
, &output_desc
)))
2160 ERR("Failed to get output description, hr %#x.\n", hr
);
2161 wined3d_mutex_unlock();
2165 x
= output_desc
.desktop_rect
.left
;
2166 y
= output_desc
.desktop_rect
.top
;
2167 width
= output_desc
.desktop_rect
.right
- output_desc
.desktop_rect
.left
;
2168 height
= output_desc
.desktop_rect
.bottom
- output_desc
.desktop_rect
.top
;
2171 wined3d_mutex_unlock();
2173 MoveWindow(window
, x
, y
, width
, height
, TRUE
);
2178 static LONG
fullscreen_style(LONG style
)
2180 /* Make sure the window is managed, otherwise we won't get keyboard input. */
2181 style
|= WS_POPUP
| WS_SYSMENU
;
2182 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
2187 static LONG
fullscreen_exstyle(LONG exstyle
)
2189 /* Filter out window decorations. */
2190 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
2195 HRESULT
wined3d_swapchain_state_setup_fullscreen(struct wined3d_swapchain_state
*state
,
2196 HWND window
, int x
, int y
, int width
, int height
)
2198 unsigned int window_pos_flags
= SWP_FRAMECHANGED
| SWP_NOACTIVATE
;
2199 LONG style
, exstyle
;
2202 TRACE("Setting up window %p for fullscreen mode.\n", window
);
2204 if (!IsWindow(window
))
2206 WARN("%p is not a valid window.\n", window
);
2207 return WINED3DERR_NOTAVAILABLE
;
2210 if (state
->style
|| state
->exstyle
)
2212 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
2213 window
, state
->style
, state
->exstyle
);
2216 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
)
2217 window_pos_flags
|= SWP_NOZORDER
;
2219 window_pos_flags
|= SWP_SHOWWINDOW
;
2221 state
->style
= GetWindowLongW(window
, GWL_STYLE
);
2222 state
->exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2224 style
= fullscreen_style(state
->style
);
2225 exstyle
= fullscreen_exstyle(state
->exstyle
);
2227 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
2228 state
->style
, state
->exstyle
, style
, exstyle
);
2230 filter
= wined3d_filter_messages(window
, TRUE
);
2232 SetWindowLongW(window
, GWL_STYLE
, style
);
2233 SetWindowLongW(window
, GWL_EXSTYLE
, exstyle
);
2234 SetWindowPos(window
, HWND_TOPMOST
, x
, y
, width
, height
, window_pos_flags
);
2236 wined3d_filter_messages(window
, filter
);
2241 void wined3d_swapchain_state_restore_from_fullscreen(struct wined3d_swapchain_state
*state
,
2242 HWND window
, const RECT
*window_rect
)
2244 unsigned int window_pos_flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
2245 HWND window_pos_after
= NULL
;
2246 LONG style
, exstyle
;
2250 if (!state
->style
&& !state
->exstyle
)
2253 if ((state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE
)
2254 && !(state
->desc
.flags
& WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES
))
2256 window_pos_after
= (state
->exstyle
& WS_EX_TOPMOST
) ? HWND_TOPMOST
: HWND_NOTOPMOST
;
2257 window_pos_flags
|= (state
->style
& WS_VISIBLE
) ? SWP_SHOWWINDOW
: SWP_HIDEWINDOW
;
2258 window_pos_flags
&= ~SWP_NOZORDER
;
2261 style
= GetWindowLongW(window
, GWL_STYLE
);
2262 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
2264 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
2265 * application, and we want to ignore them in the test below, since it's
2266 * not the application's fault that they changed. Additionally, we want to
2267 * preserve the current status of these flags (i.e. don't restore them) to
2268 * more closely emulate the behavior of Direct3D, which leaves these flags
2269 * alone when returning to windowed mode. */
2270 state
->style
^= (state
->style
^ style
) & WS_VISIBLE
;
2271 state
->exstyle
^= (state
->exstyle
^ exstyle
) & WS_EX_TOPMOST
;
2273 TRACE("Restoring window style of window %p to %08x, %08x.\n",
2274 window
, state
->style
, state
->exstyle
);
2276 filter
= wined3d_filter_messages(window
, TRUE
);
2278 /* Only restore the style if the application didn't modify it during the
2279 * fullscreen phase. Some applications change it before calling Reset()
2280 * when switching between windowed and fullscreen modes (HL2), some
2281 * depend on the original style (Eve Online). */
2282 if (style
== fullscreen_style(state
->style
) && exstyle
== fullscreen_exstyle(state
->exstyle
))
2284 SetWindowLongW(window
, GWL_STYLE
, state
->style
);
2285 SetWindowLongW(window
, GWL_EXSTYLE
, state
->exstyle
);
2289 rect
= *window_rect
;
2291 window_pos_flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
2292 SetWindowPos(window
, window_pos_after
, rect
.left
, rect
.top
,
2293 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, window_pos_flags
);
2295 wined3d_filter_messages(window
, filter
);
2297 /* Delete the old values. */
2302 HRESULT CDECL
wined3d_swapchain_state_set_fullscreen(struct wined3d_swapchain_state
*state
,
2303 const struct wined3d_swapchain_desc
*swapchain_desc
,
2304 const struct wined3d_display_mode
*mode
)
2306 struct wined3d_display_mode actual_mode
;
2307 struct wined3d_output_desc output_desc
;
2308 BOOL windowed
= state
->desc
.windowed
;
2311 TRACE("state %p, swapchain_desc %p, mode %p.\n", state
, swapchain_desc
, mode
);
2313 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
)
2317 actual_mode
= *mode
;
2318 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2324 if (!swapchain_desc
->windowed
)
2326 actual_mode
.width
= swapchain_desc
->backbuffer_width
;
2327 actual_mode
.height
= swapchain_desc
->backbuffer_height
;
2328 actual_mode
.refresh_rate
= swapchain_desc
->refresh_rate
;
2329 actual_mode
.format_id
= adapter_format_from_backbuffer_format(swapchain_desc
->output
->adapter
,
2330 swapchain_desc
->backbuffer_format
);
2331 actual_mode
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
2332 if (FAILED(hr
= wined3d_swapchain_state_set_display_mode(state
, swapchain_desc
->output
,
2338 if (FAILED(hr
= wined3d_restore_display_modes(state
->wined3d
)))
2340 WARN("Failed to restore display modes for all outputs, hr %#x.\n", hr
);
2349 WARN("WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH is not set, ignoring mode.\n");
2351 if (FAILED(hr
= wined3d_output_get_display_mode(swapchain_desc
->output
, &actual_mode
,
2354 ERR("Failed to get display mode, hr %#x.\n", hr
);
2355 return WINED3DERR_INVALIDCALL
;
2359 if (!swapchain_desc
->windowed
)
2361 unsigned int width
= actual_mode
.width
;
2362 unsigned int height
= actual_mode
.height
;
2364 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
2366 ERR("Failed to get output description, hr %#x.\n", hr
);
2370 if (state
->desc
.windowed
)
2372 /* Switch from windowed to fullscreen */
2373 if (FAILED(hr
= wined3d_swapchain_state_setup_fullscreen(state
, state
->device_window
,
2374 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
, height
)))
2379 HWND window
= state
->device_window
;
2382 /* Fullscreen -> fullscreen mode change */
2383 filter
= wined3d_filter_messages(window
, TRUE
);
2384 MoveWindow(window
, output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
, width
,
2386 ShowWindow(window
, SW_SHOW
);
2387 wined3d_filter_messages(window
, filter
);
2389 state
->d3d_mode
= actual_mode
;
2391 else if (!state
->desc
.windowed
)
2393 /* Fullscreen -> windowed switch */
2394 RECT
*window_rect
= NULL
;
2395 if (state
->desc
.flags
& WINED3D_SWAPCHAIN_RESTORE_WINDOW_RECT
)
2396 window_rect
= &state
->original_window_rect
;
2397 wined3d_swapchain_state_restore_from_fullscreen(state
, state
->device_window
, window_rect
);
2400 state
->desc
.output
= swapchain_desc
->output
;
2401 state
->desc
.windowed
= swapchain_desc
->windowed
;
2403 if (windowed
!= state
->desc
.windowed
)
2404 state
->parent
->ops
->windowed_state_changed(state
->parent
, state
->desc
.windowed
);
2409 BOOL CDECL
wined3d_swapchain_state_is_windowed(const struct wined3d_swapchain_state
*state
)
2411 TRACE("state %p.\n", state
);
2413 return state
->desc
.windowed
;
2416 void CDECL
wined3d_swapchain_state_destroy(struct wined3d_swapchain_state
*state
)
2418 wined3d_swapchain_state_cleanup(state
);
2422 HRESULT CDECL
wined3d_swapchain_state_create(const struct wined3d_swapchain_desc
*desc
,
2423 HWND window
, struct wined3d
*wined3d
, struct wined3d_swapchain_state_parent
*state_parent
,
2424 struct wined3d_swapchain_state
**state
)
2426 struct wined3d_swapchain_state
*s
;
2429 TRACE("desc %p, window %p, wined3d %p, state %p.\n", desc
, window
, wined3d
, state
);
2431 if (!(s
= heap_alloc_zero(sizeof(*s
))))
2432 return E_OUTOFMEMORY
;
2434 if (FAILED(hr
= wined3d_swapchain_state_init(s
, desc
, window
, wined3d
, state_parent
)))