directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / dxgi / swapchain.c
blob0f335a71cb77ab2bb07f2fc1fce73e6441ab99b6
1 /*
2 * Copyright 2008 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "dxgi_private.h"
25 #ifdef SONAME_LIBVKD3D
26 #define VK_NO_PROTOTYPES
27 #define VKD3D_NO_PROTOTYPES
28 #define VKD3D_NO_VULKAN_H
29 #define VKD3D_NO_WIN32_TYPES
30 #ifndef USE_WIN32_VULKAN
31 #define WINE_VK_HOST
32 #endif
33 #include "wine/vulkan.h"
34 #include "wine/vulkan_driver.h"
35 #include <vkd3d.h>
36 #endif
38 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
39 WINE_DECLARE_DEBUG_CHANNEL(winediag);
41 static DXGI_SWAP_EFFECT dxgi_swap_effect_from_wined3d(enum wined3d_swap_effect swap_effect)
43 switch (swap_effect)
45 case WINED3D_SWAP_EFFECT_DISCARD:
46 return DXGI_SWAP_EFFECT_DISCARD;
47 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
48 return DXGI_SWAP_EFFECT_SEQUENTIAL;
49 case WINED3D_SWAP_EFFECT_FLIP_DISCARD:
50 return DXGI_SWAP_EFFECT_FLIP_DISCARD;
51 case WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL:
52 return DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
53 default:
54 FIXME("Invalid swap effect %#x.\n", swap_effect);
55 return DXGI_SWAP_EFFECT_DISCARD;
59 static BOOL dxgi_validate_flip_swap_effect_format(DXGI_FORMAT format)
61 switch (format)
63 case DXGI_FORMAT_R16G16B16A16_FLOAT:
64 case DXGI_FORMAT_R10G10B10A2_UNORM:
65 case DXGI_FORMAT_R8G8B8A8_UNORM:
66 case DXGI_FORMAT_B8G8R8A8_UNORM:
67 return TRUE;
68 default:
69 WARN("Invalid swapchain format %#x for flip presentation model.\n", format);
70 return FALSE;
74 BOOL dxgi_validate_swapchain_desc(const DXGI_SWAP_CHAIN_DESC1 *desc)
76 unsigned int min_buffer_count;
78 switch (desc->SwapEffect)
80 case DXGI_SWAP_EFFECT_DISCARD:
81 case DXGI_SWAP_EFFECT_SEQUENTIAL:
82 min_buffer_count = 1;
83 break;
85 case DXGI_SWAP_EFFECT_FLIP_DISCARD:
86 case DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL:
87 min_buffer_count = 2;
89 if (desc->Format && !dxgi_validate_flip_swap_effect_format(desc->Format))
90 return FALSE;
92 if (desc->SampleDesc.Count != 1 || desc->SampleDesc.Quality)
94 WARN("Invalid sample desc %u, %u for swap effect %#x.\n",
95 desc->SampleDesc.Count, desc->SampleDesc.Quality, desc->SwapEffect);
96 return FALSE;
98 break;
100 default:
101 WARN("Invalid swap effect %u used.\n", desc->SwapEffect);
102 return FALSE;
105 if (desc->BufferCount < min_buffer_count || desc->BufferCount > DXGI_MAX_SWAP_CHAIN_BUFFERS)
107 WARN("BufferCount is %u.\n", desc->BufferCount);
108 return FALSE;
111 return TRUE;
114 HRESULT dxgi_get_output_from_window(IDXGIFactory *factory, HWND window, IDXGIOutput **dxgi_output)
116 unsigned int adapter_idx, output_idx;
117 DXGI_OUTPUT_DESC desc;
118 IDXGIAdapter *adapter;
119 IDXGIOutput *output;
120 HMONITOR monitor;
121 HRESULT hr;
123 if (!(monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST)))
125 WARN("Failed to get monitor from window.\n");
126 return DXGI_ERROR_INVALID_CALL;
129 for (adapter_idx = 0; SUCCEEDED(hr = IDXGIFactory_EnumAdapters(factory, adapter_idx, &adapter));
130 ++adapter_idx)
132 for (output_idx = 0; SUCCEEDED(hr = IDXGIAdapter_EnumOutputs(adapter, output_idx,
133 &output)); ++output_idx)
135 if (FAILED(hr = IDXGIOutput_GetDesc(output, &desc)))
137 WARN("Adapter %u output %u: Failed to get output desc, hr %#x.\n", adapter_idx,
138 output_idx, hr);
139 IDXGIOutput_Release(output);
140 continue;
143 if (desc.Monitor == monitor)
145 *dxgi_output = output;
146 IDXGIAdapter_Release(adapter);
147 return S_OK;
150 IDXGIOutput_Release(output);
152 IDXGIAdapter_Release(adapter);
155 if (hr != DXGI_ERROR_NOT_FOUND)
156 WARN("Failed to enumerate outputs, hr %#x.\n", hr);
158 WARN("Output could not be found.\n");
159 return DXGI_ERROR_NOT_FOUND;
162 static HRESULT dxgi_swapchain_resize_target(struct wined3d_swapchain_state *state,
163 const DXGI_MODE_DESC *target_mode_desc)
165 struct wined3d_display_mode mode;
167 if (!target_mode_desc)
169 WARN("Invalid pointer.\n");
170 return DXGI_ERROR_INVALID_CALL;
173 TRACE("Mode: %s.\n", debug_dxgi_mode(target_mode_desc));
175 if (target_mode_desc->Scaling)
176 FIXME("Ignoring scaling %#x.\n", target_mode_desc->Scaling);
178 wined3d_display_mode_from_dxgi(&mode, target_mode_desc);
180 return wined3d_swapchain_state_resize_target(state, &mode);
183 static HWND d3d11_swapchain_get_hwnd(struct d3d11_swapchain *swapchain)
185 struct wined3d_swapchain_desc wined3d_desc;
187 wined3d_mutex_lock();
188 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
189 wined3d_mutex_unlock();
191 return wined3d_desc.device_window;
194 static inline struct d3d11_swapchain *d3d11_swapchain_from_IDXGISwapChain1(IDXGISwapChain1 *iface)
196 return CONTAINING_RECORD(iface, struct d3d11_swapchain, IDXGISwapChain1_iface);
199 /* IUnknown methods */
201 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_QueryInterface(IDXGISwapChain1 *iface, REFIID riid, void **object)
203 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
205 if (IsEqualGUID(riid, &IID_IUnknown)
206 || IsEqualGUID(riid, &IID_IDXGIObject)
207 || IsEqualGUID(riid, &IID_IDXGIDeviceSubObject)
208 || IsEqualGUID(riid, &IID_IDXGISwapChain)
209 || IsEqualGUID(riid, &IID_IDXGISwapChain1))
211 IUnknown_AddRef(iface);
212 *object = iface;
213 return S_OK;
216 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
218 *object = NULL;
219 return E_NOINTERFACE;
222 static ULONG STDMETHODCALLTYPE d3d11_swapchain_AddRef(IDXGISwapChain1 *iface)
224 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
225 ULONG refcount = InterlockedIncrement(&swapchain->refcount);
227 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
229 if (refcount == 1)
230 wined3d_swapchain_incref(swapchain->wined3d_swapchain);
232 return refcount;
235 static ULONG STDMETHODCALLTYPE d3d11_swapchain_Release(IDXGISwapChain1 *iface)
237 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
238 ULONG refcount = InterlockedDecrement(&swapchain->refcount);
240 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
242 if (!refcount)
244 IWineDXGIDevice *device = swapchain->device;
245 if (swapchain->target)
247 WARN("Releasing fullscreen swapchain.\n");
248 IDXGIOutput_Release(swapchain->target);
250 if (swapchain->factory)
251 IDXGIFactory_Release(swapchain->factory);
252 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
253 if (device)
254 IWineDXGIDevice_Release(device);
257 return refcount;
260 /* IDXGIObject methods */
262 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_SetPrivateData(IDXGISwapChain1 *iface,
263 REFGUID guid, UINT data_size, const void *data)
265 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
267 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
269 return dxgi_set_private_data(&swapchain->private_store, guid, data_size, data);
272 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_SetPrivateDataInterface(IDXGISwapChain1 *iface,
273 REFGUID guid, const IUnknown *object)
275 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
277 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
279 return dxgi_set_private_data_interface(&swapchain->private_store, guid, object);
282 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetPrivateData(IDXGISwapChain1 *iface,
283 REFGUID guid, UINT *data_size, void *data)
285 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
287 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
289 return dxgi_get_private_data(&swapchain->private_store, guid, data_size, data);
292 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetParent(IDXGISwapChain1 *iface, REFIID riid, void **parent)
294 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
296 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
298 if (!swapchain->factory)
300 ERR("Implicit swapchain does not store reference to parent.\n");
301 *parent = NULL;
302 return E_NOINTERFACE;
305 return IDXGIFactory_QueryInterface(swapchain->factory, riid, parent);
308 /* IDXGIDeviceSubObject methods */
310 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetDevice(IDXGISwapChain1 *iface, REFIID riid, void **device)
312 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
314 TRACE("iface %p, riid %s, device %p.\n", iface, debugstr_guid(riid), device);
316 if (!swapchain->device)
318 ERR("Implicit swapchain does not store reference to device.\n");
319 *device = NULL;
320 return E_NOINTERFACE;
323 return IWineDXGIDevice_QueryInterface(swapchain->device, riid, device);
326 /* IDXGISwapChain1 methods */
328 static HRESULT d3d11_swapchain_present(struct d3d11_swapchain *swapchain,
329 unsigned int sync_interval, unsigned int flags)
331 if (sync_interval > 4)
333 WARN("Invalid sync interval %u.\n", sync_interval);
334 return DXGI_ERROR_INVALID_CALL;
337 if (IsIconic(d3d11_swapchain_get_hwnd(swapchain)))
338 return DXGI_STATUS_OCCLUDED;
340 if (flags & ~DXGI_PRESENT_TEST)
341 FIXME("Unimplemented flags %#x.\n", flags);
342 if (flags & DXGI_PRESENT_TEST)
344 WARN("Returning S_OK for DXGI_PRESENT_TEST.\n");
345 return S_OK;
348 return wined3d_swapchain_present(swapchain->wined3d_swapchain, NULL, NULL, NULL, sync_interval, 0);
351 static HRESULT STDMETHODCALLTYPE DECLSPEC_HOTPATCH d3d11_swapchain_Present(IDXGISwapChain1 *iface, UINT sync_interval, UINT flags)
353 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
355 TRACE("iface %p, sync_interval %u, flags %#x.\n", iface, sync_interval, flags);
357 return d3d11_swapchain_present(swapchain, sync_interval, flags);
360 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetBuffer(IDXGISwapChain1 *iface,
361 UINT buffer_idx, REFIID riid, void **surface)
363 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
364 struct wined3d_texture *texture;
365 IUnknown *parent;
366 HRESULT hr;
368 TRACE("iface %p, buffer_idx %u, riid %s, surface %p\n",
369 iface, buffer_idx, debugstr_guid(riid), surface);
371 wined3d_mutex_lock();
373 if (!(texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, buffer_idx)))
375 wined3d_mutex_unlock();
376 return DXGI_ERROR_INVALID_CALL;
379 parent = wined3d_texture_get_parent(texture);
380 hr = IUnknown_QueryInterface(parent, riid, surface);
381 wined3d_mutex_unlock();
383 return hr;
386 static HRESULT STDMETHODCALLTYPE DECLSPEC_HOTPATCH d3d11_swapchain_SetFullscreenState(IDXGISwapChain1 *iface,
387 BOOL fullscreen, IDXGIOutput *target)
389 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
390 struct wined3d_swapchain_desc swapchain_desc;
391 struct wined3d_swapchain_state *state;
392 struct dxgi_output *dxgi_output;
393 HRESULT hr;
395 TRACE("iface %p, fullscreen %#x, target %p.\n", iface, fullscreen, target);
397 if (!fullscreen && target)
399 WARN("Invalid call.\n");
400 return DXGI_ERROR_INVALID_CALL;
403 if (target)
405 IDXGIOutput_AddRef(target);
407 else if (FAILED(hr = IDXGISwapChain1_GetContainingOutput(iface, &target)))
409 WARN("Failed to get target output for swapchain, hr %#x.\n", hr);
410 return hr;
412 dxgi_output = unsafe_impl_from_IDXGIOutput(target);
414 wined3d_mutex_lock();
415 state = wined3d_swapchain_get_state(swapchain->wined3d_swapchain);
416 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &swapchain_desc);
417 swapchain_desc.output = dxgi_output->wined3d_output;
418 swapchain_desc.windowed = !fullscreen;
419 hr = wined3d_swapchain_state_set_fullscreen(state, &swapchain_desc, NULL);
420 wined3d_mutex_unlock();
421 if (FAILED(hr))
423 IDXGIOutput_Release(target);
425 return DXGI_ERROR_NOT_CURRENTLY_AVAILABLE;
428 if (!fullscreen)
430 IDXGIOutput_Release(target);
431 target = NULL;
434 if (swapchain->target)
435 IDXGIOutput_Release(swapchain->target);
436 swapchain->target = target;
438 return S_OK;
441 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetFullscreenState(IDXGISwapChain1 *iface,
442 BOOL *fullscreen, IDXGIOutput **target)
444 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
445 struct wined3d_swapchain_desc swapchain_desc;
446 HRESULT hr;
448 TRACE("iface %p, fullscreen %p, target %p.\n", iface, fullscreen, target);
450 if (fullscreen || target)
452 wined3d_mutex_lock();
453 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &swapchain_desc);
454 wined3d_mutex_unlock();
457 if (fullscreen)
458 *fullscreen = !swapchain_desc.windowed;
460 if (target)
462 if (!swapchain_desc.windowed)
464 if (!swapchain->target && FAILED(hr = IDXGISwapChain1_GetContainingOutput(iface, &swapchain->target)))
465 return hr;
467 *target = swapchain->target;
468 IDXGIOutput_AddRef(*target);
470 else
472 *target = NULL;
476 return S_OK;
479 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetDesc(IDXGISwapChain1 *iface, DXGI_SWAP_CHAIN_DESC *desc)
481 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
482 struct wined3d_swapchain_desc wined3d_desc;
484 TRACE("iface %p, desc %p.\n", iface, desc);
486 if (!desc)
488 WARN("Invalid pointer.\n");
489 return E_INVALIDARG;
492 wined3d_mutex_lock();
493 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
494 wined3d_mutex_unlock();
496 FIXME("Ignoring ScanlineOrdering and Scaling.\n");
498 desc->BufferDesc.Width = wined3d_desc.backbuffer_width;
499 desc->BufferDesc.Height = wined3d_desc.backbuffer_height;
500 desc->BufferDesc.RefreshRate.Numerator = wined3d_desc.refresh_rate;
501 desc->BufferDesc.RefreshRate.Denominator = 1;
502 desc->BufferDesc.Format = dxgi_format_from_wined3dformat(wined3d_desc.backbuffer_format);
503 desc->BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
504 desc->BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
505 dxgi_sample_desc_from_wined3d(&desc->SampleDesc,
506 wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
507 desc->BufferUsage = dxgi_usage_from_wined3d_bind_flags(wined3d_desc.backbuffer_bind_flags);
508 desc->BufferCount = wined3d_desc.backbuffer_count;
509 desc->OutputWindow = wined3d_desc.device_window;
510 desc->Windowed = wined3d_desc.windowed;
511 desc->SwapEffect = dxgi_swap_effect_from_wined3d(wined3d_desc.swap_effect);
512 desc->Flags = dxgi_swapchain_flags_from_wined3d(wined3d_desc.flags);
514 return S_OK;
517 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_ResizeBuffers(IDXGISwapChain1 *iface,
518 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
520 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
521 struct wined3d_swapchain_desc wined3d_desc;
522 struct wined3d_texture *texture;
523 IUnknown *parent;
524 unsigned int i;
525 HRESULT hr;
527 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x.\n",
528 iface, buffer_count, width, height, debug_dxgi_format(format), flags);
530 if (flags)
531 FIXME("Ignoring flags %#x.\n", flags);
533 wined3d_mutex_lock();
534 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
535 for (i = 0; i < wined3d_desc.backbuffer_count; ++i)
537 texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, i);
538 parent = wined3d_texture_get_parent(texture);
539 IUnknown_AddRef(parent);
540 if (IUnknown_Release(parent))
542 wined3d_mutex_unlock();
543 return DXGI_ERROR_INVALID_CALL;
546 if (format != DXGI_FORMAT_UNKNOWN)
547 wined3d_desc.backbuffer_format = wined3dformat_from_dxgi_format(format);
548 hr = wined3d_swapchain_resize_buffers(swapchain->wined3d_swapchain, buffer_count, width, height,
549 wined3d_desc.backbuffer_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
550 wined3d_mutex_unlock();
552 return hr;
555 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_ResizeTarget(IDXGISwapChain1 *iface,
556 const DXGI_MODE_DESC *target_mode_desc)
558 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
559 struct wined3d_swapchain_state *state;
561 TRACE("iface %p, target_mode_desc %p.\n", iface, target_mode_desc);
563 state = wined3d_swapchain_get_state(swapchain->wined3d_swapchain);
565 return dxgi_swapchain_resize_target(state, target_mode_desc);
568 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetContainingOutput(IDXGISwapChain1 *iface, IDXGIOutput **output)
570 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
571 HWND window;
573 TRACE("iface %p, output %p.\n", iface, output);
575 if (swapchain->target)
577 IDXGIOutput_AddRef(*output = swapchain->target);
578 return S_OK;
581 if (!swapchain->factory)
583 ERR("Implicit swapchain does not store a reference to factory.\n");
584 return E_NOINTERFACE;
587 window = d3d11_swapchain_get_hwnd(swapchain);
588 return dxgi_get_output_from_window(swapchain->factory, window, output);
591 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetFrameStatistics(IDXGISwapChain1 *iface,
592 DXGI_FRAME_STATISTICS *stats)
594 FIXME("iface %p, stats %p stub!\n", iface, stats);
596 return E_NOTIMPL;
599 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetLastPresentCount(IDXGISwapChain1 *iface,
600 UINT *last_present_count)
602 FIXME("iface %p, last_present_count %p stub!\n", iface, last_present_count);
604 return E_NOTIMPL;
607 /* IDXGISwapChain1 methods */
609 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetDesc1(IDXGISwapChain1 *iface, DXGI_SWAP_CHAIN_DESC1 *desc)
611 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
612 struct wined3d_swapchain_desc wined3d_desc;
614 TRACE("iface %p, desc %p.\n", iface, desc);
616 if (!desc)
618 WARN("Invalid pointer.\n");
619 return E_INVALIDARG;
622 wined3d_mutex_lock();
623 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
624 wined3d_mutex_unlock();
626 FIXME("Ignoring Stereo, Scaling and AlphaMode.\n");
628 desc->Width = wined3d_desc.backbuffer_width;
629 desc->Height = wined3d_desc.backbuffer_height;
630 desc->Format = dxgi_format_from_wined3dformat(wined3d_desc.backbuffer_format);
631 desc->Stereo = FALSE;
632 dxgi_sample_desc_from_wined3d(&desc->SampleDesc,
633 wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
634 desc->BufferUsage = dxgi_usage_from_wined3d_bind_flags(wined3d_desc.backbuffer_bind_flags);
635 desc->BufferCount = wined3d_desc.backbuffer_count;
636 desc->Scaling = DXGI_SCALING_STRETCH;
637 desc->SwapEffect = dxgi_swap_effect_from_wined3d(wined3d_desc.swap_effect);
638 desc->AlphaMode = DXGI_ALPHA_MODE_IGNORE;
639 desc->Flags = dxgi_swapchain_flags_from_wined3d(wined3d_desc.flags);
641 return S_OK;
644 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetFullscreenDesc(IDXGISwapChain1 *iface,
645 DXGI_SWAP_CHAIN_FULLSCREEN_DESC *desc)
647 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
648 struct wined3d_swapchain_desc wined3d_desc;
650 TRACE("iface %p, desc %p.\n", iface, desc);
652 if (!desc)
654 WARN("Invalid pointer.\n");
655 return E_INVALIDARG;
658 wined3d_mutex_lock();
659 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
660 wined3d_mutex_unlock();
662 FIXME("Ignoring ScanlineOrdering and Scaling.\n");
664 desc->RefreshRate.Numerator = wined3d_desc.refresh_rate;
665 desc->RefreshRate.Denominator = 1;
666 desc->ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
667 desc->Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
668 desc->Windowed = wined3d_desc.windowed;
670 return S_OK;
673 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetHwnd(IDXGISwapChain1 *iface, HWND *hwnd)
675 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
677 TRACE("iface %p, hwnd %p.\n", iface, hwnd);
679 if (!hwnd)
681 WARN("Invalid pointer.\n");
682 return DXGI_ERROR_INVALID_CALL;
685 *hwnd = d3d11_swapchain_get_hwnd(swapchain);
686 return S_OK;
689 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetCoreWindow(IDXGISwapChain1 *iface,
690 REFIID iid, void **core_window)
692 FIXME("iface %p, iid %s, core_window %p stub!\n", iface, debugstr_guid(iid), core_window);
694 if (core_window)
695 *core_window = NULL;
697 return DXGI_ERROR_INVALID_CALL;
700 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_Present1(IDXGISwapChain1 *iface,
701 UINT sync_interval, UINT flags, const DXGI_PRESENT_PARAMETERS *present_parameters)
703 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_IDXGISwapChain1(iface);
705 TRACE("iface %p, sync_interval %u, flags %#x, present_parameters %p.\n",
706 iface, sync_interval, flags, present_parameters);
708 if (present_parameters)
709 FIXME("Ignored present parameters %p.\n", present_parameters);
711 return d3d11_swapchain_present(swapchain, sync_interval, flags);
714 static BOOL STDMETHODCALLTYPE d3d11_swapchain_IsTemporaryMonoSupported(IDXGISwapChain1 *iface)
716 FIXME("iface %p stub!\n", iface);
718 return FALSE;
721 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetRestrictToOutput(IDXGISwapChain1 *iface, IDXGIOutput **output)
723 FIXME("iface %p, output %p stub!\n", iface, output);
725 if (!output)
727 WARN("Invalid pointer.\n");
728 return E_INVALIDARG;
731 *output = NULL;
732 return E_NOTIMPL;
735 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_SetBackgroundColor(IDXGISwapChain1 *iface, const DXGI_RGBA *color)
737 FIXME("iface %p, color %p stub!\n", iface, color);
739 return E_NOTIMPL;
742 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetBackgroundColor(IDXGISwapChain1 *iface, DXGI_RGBA *color)
744 FIXME("iface %p, color %p stub!\n", iface, color);
746 return E_NOTIMPL;
749 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_SetRotation(IDXGISwapChain1 *iface, DXGI_MODE_ROTATION rotation)
751 FIXME("iface %p, rotation %#x stub!\n", iface, rotation);
753 return E_NOTIMPL;
756 static HRESULT STDMETHODCALLTYPE d3d11_swapchain_GetRotation(IDXGISwapChain1 *iface, DXGI_MODE_ROTATION *rotation)
758 FIXME("iface %p, rotation %p stub!\n", iface, rotation);
760 return E_NOTIMPL;
763 static const struct IDXGISwapChain1Vtbl d3d11_swapchain_vtbl =
765 /* IUnknown methods */
766 d3d11_swapchain_QueryInterface,
767 d3d11_swapchain_AddRef,
768 d3d11_swapchain_Release,
769 /* IDXGIObject methods */
770 d3d11_swapchain_SetPrivateData,
771 d3d11_swapchain_SetPrivateDataInterface,
772 d3d11_swapchain_GetPrivateData,
773 d3d11_swapchain_GetParent,
774 /* IDXGIDeviceSubObject methods */
775 d3d11_swapchain_GetDevice,
776 /* IDXGISwapChain methods */
777 d3d11_swapchain_Present,
778 d3d11_swapchain_GetBuffer,
779 d3d11_swapchain_SetFullscreenState,
780 d3d11_swapchain_GetFullscreenState,
781 d3d11_swapchain_GetDesc,
782 d3d11_swapchain_ResizeBuffers,
783 d3d11_swapchain_ResizeTarget,
784 d3d11_swapchain_GetContainingOutput,
785 d3d11_swapchain_GetFrameStatistics,
786 d3d11_swapchain_GetLastPresentCount,
787 /* IDXGISwapChain1 methods */
788 d3d11_swapchain_GetDesc1,
789 d3d11_swapchain_GetFullscreenDesc,
790 d3d11_swapchain_GetHwnd,
791 d3d11_swapchain_GetCoreWindow,
792 d3d11_swapchain_Present1,
793 d3d11_swapchain_IsTemporaryMonoSupported,
794 d3d11_swapchain_GetRestrictToOutput,
795 d3d11_swapchain_SetBackgroundColor,
796 d3d11_swapchain_GetBackgroundColor,
797 d3d11_swapchain_SetRotation,
798 d3d11_swapchain_GetRotation,
801 static void STDMETHODCALLTYPE d3d11_swapchain_wined3d_object_released(void *parent)
803 struct d3d11_swapchain *swapchain = parent;
805 wined3d_private_store_cleanup(&swapchain->private_store);
806 heap_free(parent);
809 static const struct wined3d_parent_ops d3d11_swapchain_wined3d_parent_ops =
811 d3d11_swapchain_wined3d_object_released,
814 static inline struct d3d11_swapchain *d3d11_swapchain_from_wined3d_swapchain_state_parent(struct wined3d_swapchain_state_parent *parent)
816 return CONTAINING_RECORD(parent, struct d3d11_swapchain, state_parent);
819 static void CDECL d3d11_swapchain_windowed_state_changed(struct wined3d_swapchain_state_parent *parent,
820 BOOL windowed)
822 struct d3d11_swapchain *swapchain = d3d11_swapchain_from_wined3d_swapchain_state_parent(parent);
824 TRACE("parent %p, windowed %d.\n", parent, windowed);
826 if (windowed && swapchain->target)
828 IDXGIOutput_Release(swapchain->target);
829 swapchain->target = NULL;
833 static const struct wined3d_swapchain_state_parent_ops d3d11_swapchain_state_parent_ops =
835 d3d11_swapchain_windowed_state_changed,
838 HRESULT d3d11_swapchain_init(struct d3d11_swapchain *swapchain, struct dxgi_device *device,
839 struct wined3d_swapchain_desc *desc)
841 BOOL fullscreen;
842 HRESULT hr;
844 /* A reference to the implicit swapchain is held by the wined3d device. In
845 * order to avoid circular references we do not keep a reference to the
846 * device in the implicit swapchain. */
847 if (!(desc->flags & WINED3D_SWAPCHAIN_IMPLICIT))
849 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
850 return E_INVALIDARG;
852 if (FAILED(hr = IWineDXGIAdapter_GetParent(device->adapter, &IID_IDXGIFactory,
853 (void **)&swapchain->factory)))
855 WARN("Failed to get adapter parent, hr %#x.\n", hr);
856 return hr;
858 IWineDXGIDevice_AddRef(swapchain->device = &device->IWineDXGIDevice_iface);
860 else
862 swapchain->device = NULL;
863 swapchain->factory = NULL;
866 swapchain->IDXGISwapChain1_iface.lpVtbl = &d3d11_swapchain_vtbl;
867 swapchain->state_parent.ops = &d3d11_swapchain_state_parent_ops;
868 swapchain->refcount = 1;
869 wined3d_mutex_lock();
870 wined3d_private_store_init(&swapchain->private_store);
872 if (!desc->windowed && (!desc->backbuffer_width || !desc->backbuffer_height))
873 FIXME("Fullscreen swapchain with back buffer width/height equal to 0 not supported properly.\n");
875 fullscreen = !desc->windowed;
876 desc->windowed = TRUE;
877 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, &swapchain->state_parent,
878 swapchain, &d3d11_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
880 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
881 goto cleanup;
884 swapchain->target = NULL;
885 if (fullscreen)
887 struct wined3d_swapchain_state *state;
889 desc->windowed = FALSE;
890 state = wined3d_swapchain_get_state(swapchain->wined3d_swapchain);
892 if (FAILED(hr = IDXGISwapChain1_GetContainingOutput(&swapchain->IDXGISwapChain1_iface,
893 &swapchain->target)))
895 WARN("Failed to get target output for fullscreen swapchain, hr %#x.\n", hr);
896 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
897 goto cleanup;
900 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(state, desc, NULL)))
902 WARN("Failed to set fullscreen state, hr %#x.\n", hr);
903 IDXGIOutput_Release(swapchain->target);
904 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
905 goto cleanup;
909 wined3d_mutex_unlock();
911 return S_OK;
913 cleanup:
914 wined3d_private_store_cleanup(&swapchain->private_store);
915 wined3d_mutex_unlock();
916 if (swapchain->factory)
917 IDXGIFactory_Release(swapchain->factory);
918 if (swapchain->device)
919 IWineDXGIDevice_Release(swapchain->device);
920 return hr;
923 #ifdef SONAME_LIBVKD3D
925 #ifdef USE_WIN32_VULKAN
927 static void *load_library(const char *name)
929 return LoadLibraryA(name);
932 static void *get_library_proc(void *handle, const char *name)
934 return (void *)GetProcAddress(handle, name);
937 static void close_library(void *handle)
939 if (handle)
940 FreeLibrary(handle);
943 static PFN_vkGetInstanceProcAddr load_vulkan(void **vulkan_handle)
945 *vulkan_handle = LoadLibraryA("vulkan-1.dll");
946 return (void *)GetProcAddress(*vulkan_handle, "vkGetInstanceProcAddr");
949 #else
951 static void *load_library(const char *name)
953 return dlopen(name, RTLD_NOW);
956 static void *get_library_proc(void *handle, const char *name)
958 return dlsym(handle, name);
961 static void close_library(void *handle)
963 if (handle)
964 dlclose(handle);
967 static PFN_vkGetInstanceProcAddr load_vulkan(void **vulkan_handle)
969 const struct vulkan_funcs *vk_funcs;
970 HDC hdc;
972 *vulkan_handle = NULL;
974 hdc = GetDC(0);
975 vk_funcs = __wine_get_vulkan_driver(hdc, WINE_VULKAN_DRIVER_VERSION);
976 ReleaseDC(0, hdc);
978 if (vk_funcs)
979 return (PFN_vkGetInstanceProcAddr)vk_funcs->p_vkGetInstanceProcAddr;
981 return NULL;
984 #endif /* USE_WIN32_VULKAN */
986 static PFN_vkd3d_acquire_vk_queue vkd3d_acquire_vk_queue;
987 static PFN_vkd3d_create_image_resource vkd3d_create_image_resource;
988 static PFN_vkd3d_get_device_parent vkd3d_get_device_parent;
989 static PFN_vkd3d_get_vk_device vkd3d_get_vk_device;
990 static PFN_vkd3d_get_vk_format vkd3d_get_vk_format;
991 static PFN_vkd3d_get_vk_physical_device vkd3d_get_vk_physical_device;
992 static PFN_vkd3d_get_vk_queue_family_index vkd3d_get_vk_queue_family_index;
993 static PFN_vkd3d_instance_from_device vkd3d_instance_from_device;
994 static PFN_vkd3d_instance_get_vk_instance vkd3d_instance_get_vk_instance;
995 static PFN_vkd3d_release_vk_queue vkd3d_release_vk_queue;
996 static PFN_vkd3d_resource_decref vkd3d_resource_decref;
997 static PFN_vkd3d_resource_incref vkd3d_resource_incref;
999 struct dxgi_vk_funcs
1001 PFN_vkAcquireNextImageKHR p_vkAcquireNextImageKHR;
1002 PFN_vkAllocateCommandBuffers p_vkAllocateCommandBuffers;
1003 PFN_vkAllocateMemory p_vkAllocateMemory;
1004 PFN_vkBeginCommandBuffer p_vkBeginCommandBuffer;
1005 PFN_vkBindImageMemory p_vkBindImageMemory;
1006 PFN_vkCmdBlitImage p_vkCmdBlitImage;
1007 PFN_vkCmdPipelineBarrier p_vkCmdPipelineBarrier;
1008 PFN_vkCreateCommandPool p_vkCreateCommandPool;
1009 PFN_vkCreateFence p_vkCreateFence;
1010 PFN_vkCreateImage p_vkCreateImage;
1011 PFN_vkCreateSemaphore p_vkCreateSemaphore;
1012 PFN_vkCreateSwapchainKHR p_vkCreateSwapchainKHR;
1013 PFN_vkCreateWin32SurfaceKHR p_vkCreateWin32SurfaceKHR;
1014 PFN_vkDestroyCommandPool p_vkDestroyCommandPool;
1015 PFN_vkDestroyFence p_vkDestroyFence;
1016 PFN_vkDestroyImage p_vkDestroyImage;
1017 PFN_vkDestroySemaphore p_vkDestroySemaphore;
1018 PFN_vkDestroySurfaceKHR p_vkDestroySurfaceKHR;
1019 PFN_vkResetCommandBuffer p_vkResetCommandBuffer;
1020 PFN_vkDestroySwapchainKHR p_vkDestroySwapchainKHR;
1021 PFN_vkEndCommandBuffer p_vkEndCommandBuffer;
1022 PFN_vkFreeMemory p_vkFreeMemory;
1023 PFN_vkGetImageMemoryRequirements p_vkGetImageMemoryRequirements;
1024 PFN_vkGetInstanceProcAddr p_vkGetInstanceProcAddr;
1025 PFN_vkGetPhysicalDeviceMemoryProperties p_vkGetPhysicalDeviceMemoryProperties;
1026 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR p_vkGetPhysicalDeviceSurfaceCapabilitiesKHR;
1027 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR p_vkGetPhysicalDeviceSurfaceFormatsKHR;
1028 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR p_vkGetPhysicalDeviceSurfacePresentModesKHR;
1029 PFN_vkGetPhysicalDeviceSurfaceSupportKHR p_vkGetPhysicalDeviceSurfaceSupportKHR;
1030 PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR p_vkGetPhysicalDeviceWin32PresentationSupportKHR;
1031 PFN_vkGetSwapchainImagesKHR p_vkGetSwapchainImagesKHR;
1032 PFN_vkQueuePresentKHR p_vkQueuePresentKHR;
1033 PFN_vkQueueSubmit p_vkQueueSubmit;
1034 PFN_vkQueueWaitIdle p_vkQueueWaitIdle;
1035 PFN_vkResetFences p_vkResetFences;
1036 PFN_vkWaitForFences p_vkWaitForFences;
1038 void *vulkan_module;
1041 static HRESULT hresult_from_vk_result(VkResult vr)
1043 switch (vr)
1045 case VK_SUCCESS:
1046 return S_OK;
1047 case VK_ERROR_OUT_OF_HOST_MEMORY:
1048 case VK_ERROR_OUT_OF_DEVICE_MEMORY:
1049 return E_OUTOFMEMORY;
1050 default:
1051 FIXME("Unhandled VkResult %d.\n", vr);
1052 return E_FAIL;
1056 #define INVALID_VK_IMAGE_INDEX (~(uint32_t)0)
1058 struct d3d12_swapchain
1060 IDXGISwapChain4 IDXGISwapChain4_iface;
1061 LONG refcount;
1062 struct wined3d_private_store private_store;
1064 struct wined3d_swapchain_state *state;
1065 struct wined3d_swapchain_state_parent state_parent;
1067 VkSwapchainKHR vk_swapchain;
1068 VkSurfaceKHR vk_surface;
1069 VkFence vk_fence;
1070 VkInstance vk_instance;
1071 VkDevice vk_device;
1072 VkPhysicalDevice vk_physical_device;
1073 VkDeviceMemory vk_memory;
1074 VkCommandPool vk_cmd_pool;
1075 VkImage vk_images[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1076 VkImage vk_swapchain_images[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1077 VkCommandBuffer vk_cmd_buffers[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1078 VkSemaphore vk_semaphores[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1079 ID3D12Resource *buffers[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1080 unsigned int buffer_count;
1081 unsigned int vk_swapchain_width;
1082 unsigned int vk_swapchain_height;
1083 VkPresentModeKHR present_mode;
1085 uint32_t vk_image_index;
1086 unsigned int current_buffer_index;
1087 struct dxgi_vk_funcs vk_funcs;
1089 ID3D12CommandQueue *command_queue;
1090 ID3D12Device *device;
1091 IWineDXGIFactory *factory;
1093 HWND window;
1094 IDXGIOutput *target;
1095 DXGI_SWAP_CHAIN_DESC1 desc;
1096 DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreen_desc;
1098 ID3D12Fence *frame_latency_fence;
1099 HANDLE frame_latency_event;
1101 uint64_t frame_number;
1102 uint32_t frame_latency;
1105 static DXGI_FORMAT dxgi_format_from_vk_format(VkFormat vk_format)
1107 switch (vk_format)
1109 case VK_FORMAT_B8G8R8A8_UNORM: return DXGI_FORMAT_B8G8R8A8_UNORM;
1110 case VK_FORMAT_R8G8B8A8_UNORM: return DXGI_FORMAT_R8G8B8A8_UNORM;
1111 case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return DXGI_FORMAT_R10G10B10A2_UNORM;
1112 case VK_FORMAT_R16G16B16A16_SFLOAT: return DXGI_FORMAT_R16G16B16A16_FLOAT;
1113 default:
1114 WARN("Unhandled format %#x.\n", vk_format);
1115 return DXGI_FORMAT_UNKNOWN;
1119 static VkFormat get_swapchain_fallback_format(VkFormat vk_format)
1121 switch (vk_format)
1123 case VK_FORMAT_R8G8B8A8_UNORM:
1124 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1125 case VK_FORMAT_R16G16B16A16_SFLOAT:
1126 return VK_FORMAT_B8G8R8A8_UNORM;
1127 default:
1128 WARN("Unhandled format %#x.\n", vk_format);
1129 return VK_FORMAT_UNDEFINED;
1133 static HRESULT select_vk_format(const struct dxgi_vk_funcs *vk_funcs,
1134 VkPhysicalDevice vk_physical_device, VkSurfaceKHR vk_surface,
1135 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, VkFormat *vk_format)
1137 VkSurfaceFormatKHR *formats;
1138 uint32_t format_count;
1139 VkFormat format;
1140 unsigned int i;
1141 VkResult vr;
1143 *vk_format = VK_FORMAT_UNDEFINED;
1145 format = vkd3d_get_vk_format(swapchain_desc->Format);
1147 vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device, vk_surface, &format_count, NULL);
1148 if (vr < 0 || !format_count)
1150 WARN("Failed to get supported surface formats, vr %d.\n", vr);
1151 return DXGI_ERROR_INVALID_CALL;
1154 if (!(formats = heap_calloc(format_count, sizeof(*formats))))
1155 return E_OUTOFMEMORY;
1157 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device,
1158 vk_surface, &format_count, formats)) < 0)
1160 WARN("Failed to enumerate supported surface formats, vr %d.\n", vr);
1161 heap_free(formats);
1162 return hresult_from_vk_result(vr);
1165 for (i = 0; i < format_count; ++i)
1167 if (formats[i].format == format && formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
1168 break;
1170 if (i == format_count)
1172 /* Try to create a swapchain with format conversion. */
1173 format = get_swapchain_fallback_format(format);
1174 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_dxgi_format(swapchain_desc->Format));
1175 for (i = 0; i < format_count; ++i)
1177 if (formats[i].format == format && formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
1179 format = formats[i].format;
1180 break;
1184 heap_free(formats);
1185 if (i == format_count)
1187 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_dxgi_format(swapchain_desc->Format));
1188 return DXGI_ERROR_UNSUPPORTED;
1191 TRACE("Using Vulkan swapchain format %#x.\n", format);
1193 *vk_format = format;
1194 return S_OK;
1197 static HRESULT vk_select_memory_type(const struct dxgi_vk_funcs *vk_funcs,
1198 VkPhysicalDevice vk_physical_device, uint32_t memory_type_mask,
1199 VkMemoryPropertyFlags flags, uint32_t *memory_type_index)
1201 VkPhysicalDeviceMemoryProperties memory_properties;
1202 unsigned int i;
1204 vk_funcs->p_vkGetPhysicalDeviceMemoryProperties(vk_physical_device, &memory_properties);
1205 for (i = 0; i < memory_properties.memoryTypeCount; ++i)
1207 if (!(memory_type_mask & (1u << i)))
1208 continue;
1210 if ((memory_properties.memoryTypes[i].propertyFlags & flags) == flags)
1212 *memory_type_index = i;
1213 return S_OK;
1217 FIXME("Failed to find memory type (allowed types %#x).\n", memory_type_mask);
1218 return E_FAIL;
1221 static BOOL d3d12_swapchain_is_present_mode_supported(struct d3d12_swapchain *swapchain,
1222 VkPresentModeKHR present_mode)
1224 VkPhysicalDevice vk_physical_device = swapchain->vk_physical_device;
1225 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1226 VkPresentModeKHR *modes;
1227 uint32_t count, i;
1228 BOOL supported;
1229 VkResult vr;
1231 if (present_mode == VK_PRESENT_MODE_FIFO_KHR)
1232 return TRUE;
1234 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
1235 swapchain->vk_surface, &count, NULL)) < 0)
1237 WARN("Failed to get count of available present modes, vr %d.\n", vr);
1238 return FALSE;
1241 supported = FALSE;
1243 if (!(modes = heap_calloc(count, sizeof(*modes))))
1244 return FALSE;
1245 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
1246 swapchain->vk_surface, &count, modes)) >= 0)
1248 for (i = 0; i < count; ++i)
1250 if (modes[i] == present_mode)
1252 supported = TRUE;
1253 break;
1257 else
1259 WARN("Failed to get available present modes, vr %d.\n", vr);
1261 heap_free(modes);
1263 return supported;
1266 static BOOL d3d12_swapchain_has_user_images(struct d3d12_swapchain *swapchain)
1268 return !!swapchain->vk_images[0];
1271 static HRESULT d3d12_swapchain_create_user_buffers(struct d3d12_swapchain *swapchain, VkFormat vk_format)
1273 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1274 VkDeviceSize image_offset[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1275 VkDevice vk_device = swapchain->vk_device;
1276 VkMemoryAllocateInfo allocate_info;
1277 VkMemoryRequirements requirements;
1278 VkImageCreateInfo image_info;
1279 uint32_t memory_type_mask;
1280 VkDeviceSize memory_size;
1281 unsigned int i;
1282 VkResult vr;
1283 HRESULT hr;
1285 if (d3d12_swapchain_has_user_images(swapchain))
1286 return S_OK;
1288 memset(&image_info, 0, sizeof(image_info));
1289 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1290 image_info.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
1291 image_info.imageType = VK_IMAGE_TYPE_2D;
1292 image_info.format = vk_format;
1293 image_info.extent.width = swapchain->desc.Width;
1294 image_info.extent.height = swapchain->desc.Height;
1295 image_info.extent.depth = 1;
1296 image_info.mipLevels = 1;
1297 image_info.arrayLayers = 1;
1298 image_info.samples = VK_SAMPLE_COUNT_1_BIT;
1299 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
1300 image_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
1301 | VK_IMAGE_USAGE_TRANSFER_DST_BIT
1302 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1303 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1304 image_info.queueFamilyIndexCount = 0;
1305 image_info.pQueueFamilyIndices = NULL;
1306 image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1308 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1310 assert(swapchain->vk_images[i] == VK_NULL_HANDLE);
1311 if ((vr = vk_funcs->p_vkCreateImage(vk_device, &image_info, NULL, &swapchain->vk_images[i])) < 0)
1313 WARN("Failed to create Vulkan image, vr %d.\n", vr);
1314 swapchain->vk_images[i] = VK_NULL_HANDLE;
1315 return hresult_from_vk_result(vr);
1319 memory_size = 0;
1320 memory_type_mask = ~0u;
1321 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1323 vk_funcs->p_vkGetImageMemoryRequirements(vk_device, swapchain->vk_images[i], &requirements);
1325 TRACE("Size %s, alignment %s, memory types %#x.\n",
1326 wine_dbgstr_longlong(requirements.size), wine_dbgstr_longlong(requirements.alignment),
1327 requirements.memoryTypeBits);
1329 image_offset[i] = (memory_size + (requirements.alignment - 1)) & ~(requirements.alignment - 1);
1330 memory_size = image_offset[i] + requirements.size;
1332 memory_type_mask &= requirements.memoryTypeBits;
1335 TRACE("Allocating %s bytes for user images.\n", wine_dbgstr_longlong(memory_size));
1337 allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1338 allocate_info.pNext = NULL;
1339 allocate_info.allocationSize = memory_size;
1341 if (FAILED(hr = vk_select_memory_type(vk_funcs, swapchain->vk_physical_device,
1342 memory_type_mask, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &allocate_info.memoryTypeIndex)))
1343 return hr;
1345 assert(swapchain->vk_memory == VK_NULL_HANDLE);
1346 if ((vr = vk_funcs->p_vkAllocateMemory(vk_device, &allocate_info, NULL, &swapchain->vk_memory)) < 0)
1348 WARN("Failed to allocate device memory, vr %d.\n", vr);
1349 swapchain->vk_memory = VK_NULL_HANDLE;
1350 return hresult_from_vk_result(vr);
1353 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1355 if ((vr = vk_funcs->p_vkBindImageMemory(vk_device, swapchain->vk_images[i],
1356 swapchain->vk_memory, image_offset[i])) < 0)
1358 WARN("Failed to bind image memory, vr %d.\n", vr);
1359 return hresult_from_vk_result(vr);
1363 return S_OK;
1366 static void vk_cmd_image_barrier(const struct dxgi_vk_funcs *vk_funcs, VkCommandBuffer cmd_buffer,
1367 VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
1368 VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask,
1369 VkImageLayout old_layout, VkImageLayout new_layout, VkImage image)
1371 VkImageMemoryBarrier barrier;
1373 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1374 barrier.pNext = NULL;
1375 barrier.srcAccessMask = src_access_mask;
1376 barrier.dstAccessMask = dst_access_mask;
1377 barrier.oldLayout = old_layout;
1378 barrier.newLayout = new_layout;
1379 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1380 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1381 barrier.image = image;
1382 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1383 barrier.subresourceRange.baseMipLevel = 0;
1384 barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
1385 barrier.subresourceRange.baseArrayLayer = 0;
1386 barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
1388 vk_funcs->p_vkCmdPipelineBarrier(cmd_buffer,
1389 src_stage_mask, dst_stage_mask, 0, 0, NULL, 0, NULL, 1, &barrier);
1392 static VkResult d3d12_swapchain_record_swapchain_blit(struct d3d12_swapchain *swapchain,
1393 VkCommandBuffer vk_cmd_buffer, VkImage vk_dst_image, VkImage vk_src_image)
1395 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1396 VkCommandBufferBeginInfo begin_info;
1397 VkImageBlit blit;
1398 VkFilter filter;
1399 VkResult vr;
1401 if (swapchain->desc.Width != swapchain->vk_swapchain_width
1402 || swapchain->desc.Height != swapchain->vk_swapchain_height)
1403 filter = VK_FILTER_LINEAR;
1404 else
1405 filter = VK_FILTER_NEAREST;
1407 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1408 begin_info.pNext = NULL;
1409 begin_info.flags = 0;
1410 begin_info.pInheritanceInfo = NULL;
1412 if ((vr = vk_funcs->p_vkBeginCommandBuffer(vk_cmd_buffer, &begin_info)) < 0)
1414 WARN("Failed to begin command buffer, vr %d.\n", vr);
1415 return vr;
1418 vk_cmd_image_barrier(vk_funcs, vk_cmd_buffer,
1419 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1420 0, VK_ACCESS_TRANSFER_WRITE_BIT,
1421 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_dst_image);
1423 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1424 blit.srcSubresource.mipLevel = 0;
1425 blit.srcSubresource.baseArrayLayer = 0;
1426 blit.srcSubresource.layerCount = 1;
1427 blit.srcOffsets[0].x = 0;
1428 blit.srcOffsets[0].y = 0;
1429 blit.srcOffsets[0].z = 0;
1430 blit.srcOffsets[1].x = swapchain->desc.Width;
1431 blit.srcOffsets[1].y = swapchain->desc.Height;
1432 blit.srcOffsets[1].z = 1;
1433 blit.dstSubresource = blit.srcSubresource;
1434 blit.dstOffsets[0].x = 0;
1435 blit.dstOffsets[0].y = 0;
1436 blit.dstOffsets[0].z = 0;
1437 if (swapchain->desc.Scaling == DXGI_SCALING_NONE)
1439 blit.srcOffsets[1].x = min(swapchain->vk_swapchain_width, blit.srcOffsets[1].x);
1440 blit.srcOffsets[1].y = min(swapchain->vk_swapchain_height, blit.srcOffsets[1].y);
1441 blit.dstOffsets[1].x = blit.srcOffsets[1].x;
1442 blit.dstOffsets[1].y = blit.srcOffsets[1].y;
1444 else
1446 /* FIXME: handle DXGI_SCALING_ASPECT_RATIO_STRETCH. */
1447 blit.dstOffsets[1].x = swapchain->vk_swapchain_width;
1448 blit.dstOffsets[1].y = swapchain->vk_swapchain_height;
1450 blit.dstOffsets[1].z = 1;
1452 vk_funcs->p_vkCmdBlitImage(vk_cmd_buffer,
1453 vk_src_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1454 vk_dst_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1455 1, &blit, filter);
1457 vk_cmd_image_barrier(vk_funcs, vk_cmd_buffer,
1458 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1459 VK_ACCESS_TRANSFER_WRITE_BIT, 0,
1460 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, vk_dst_image);
1462 if ((vr = vk_funcs->p_vkEndCommandBuffer(vk_cmd_buffer)) < 0)
1463 WARN("Failed to end command buffer, vr %d.\n", vr);
1465 return vr;
1468 static HRESULT d3d12_swapchain_prepare_command_buffers(struct d3d12_swapchain *swapchain,
1469 uint32_t queue_family_index)
1471 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1472 VkDevice vk_device = swapchain->vk_device;
1473 VkCommandBufferAllocateInfo allocate_info;
1474 VkSemaphoreCreateInfo semaphore_info;
1475 VkCommandPoolCreateInfo pool_info;
1476 unsigned int i;
1477 VkResult vr;
1479 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1480 pool_info.pNext = NULL;
1481 pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
1482 pool_info.queueFamilyIndex = queue_family_index;
1484 assert(swapchain->vk_cmd_pool == VK_NULL_HANDLE);
1485 if ((vr = vk_funcs->p_vkCreateCommandPool(vk_device, &pool_info,
1486 NULL, &swapchain->vk_cmd_pool)) < 0)
1488 WARN("Failed to create command pool, vr %d.\n", vr);
1489 swapchain->vk_cmd_pool = VK_NULL_HANDLE;
1490 return hresult_from_vk_result(vr);
1493 allocate_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1494 allocate_info.pNext = NULL;
1495 allocate_info.commandPool = swapchain->vk_cmd_pool;
1496 allocate_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1497 allocate_info.commandBufferCount = swapchain->buffer_count;
1499 if ((vr = vk_funcs->p_vkAllocateCommandBuffers(vk_device, &allocate_info,
1500 swapchain->vk_cmd_buffers)) < 0)
1502 WARN("Failed to allocate command buffers, vr %d.\n", vr);
1503 return hresult_from_vk_result(vr);
1506 for (i = 0; i < swapchain->buffer_count; ++i)
1508 semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1509 semaphore_info.pNext = NULL;
1510 semaphore_info.flags = 0;
1512 assert(swapchain->vk_semaphores[i] == VK_NULL_HANDLE);
1513 if ((vr = vk_funcs->p_vkCreateSemaphore(vk_device, &semaphore_info,
1514 NULL, &swapchain->vk_semaphores[i])) < 0)
1516 WARN("Failed to create semaphore, vr %d.\n", vr);
1517 swapchain->vk_semaphores[i] = VK_NULL_HANDLE;
1518 return hresult_from_vk_result(vr);
1522 return S_OK;
1525 static HRESULT d3d12_swapchain_create_buffers(struct d3d12_swapchain *swapchain,
1526 VkFormat vk_swapchain_format, VkFormat vk_format)
1528 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1529 struct vkd3d_image_resource_create_info resource_info;
1530 VkSwapchainKHR vk_swapchain = swapchain->vk_swapchain;
1531 ID3D12CommandQueue *queue = swapchain->command_queue;
1532 VkDevice vk_device = swapchain->vk_device;
1533 ID3D12Device *device = swapchain->device;
1534 uint32_t image_count, queue_family_index;
1535 D3D12_COMMAND_QUEUE_DESC queue_desc;
1536 unsigned int i;
1537 VkResult vr;
1538 HRESULT hr;
1540 if ((vr = vk_funcs->p_vkGetSwapchainImagesKHR(vk_device, vk_swapchain, &image_count, NULL)) < 0)
1542 WARN("Failed to get Vulkan swapchain images, vr %d.\n", vr);
1543 return hresult_from_vk_result(vr);
1545 if (image_count > ARRAY_SIZE(swapchain->vk_swapchain_images))
1547 FIXME("Unsupported Vulkan swapchain image count %u.\n", image_count);
1548 return E_FAIL;
1550 swapchain->buffer_count = image_count;
1551 if ((vr = vk_funcs->p_vkGetSwapchainImagesKHR(vk_device, vk_swapchain,
1552 &image_count, swapchain->vk_swapchain_images)) < 0)
1554 WARN("Failed to get Vulkan swapchain images, vr %d.\n", vr);
1555 return hresult_from_vk_result(vr);
1558 resource_info.type = VKD3D_STRUCTURE_TYPE_IMAGE_RESOURCE_CREATE_INFO;
1559 resource_info.next = NULL;
1560 resource_info.desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
1561 resource_info.desc.Alignment = 0;
1562 resource_info.desc.Width = swapchain->desc.Width;
1563 resource_info.desc.Height = swapchain->desc.Height;
1564 resource_info.desc.DepthOrArraySize = 1;
1565 resource_info.desc.MipLevels = 1;
1566 resource_info.desc.Format = dxgi_format_from_vk_format(vk_format);
1567 resource_info.desc.SampleDesc.Count = 1;
1568 resource_info.desc.SampleDesc.Quality = 0;
1569 resource_info.desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
1570 resource_info.desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
1571 resource_info.flags = VKD3D_RESOURCE_INITIAL_STATE_TRANSITION | VKD3D_RESOURCE_PRESENT_STATE_TRANSITION;
1573 queue_desc = ID3D12CommandQueue_GetDesc(queue);
1574 if (queue_desc.Type != D3D12_COMMAND_LIST_TYPE_DIRECT)
1576 /* vkCmdBlitImage() is only supported for graphics queues. */
1577 FIXME("Swapchain blit not implemented for command queue type %#x.\n", queue_desc.Type);
1578 if (vk_swapchain_format != vk_format)
1579 return E_NOTIMPL;
1580 if (image_count != swapchain->desc.BufferCount)
1582 FIXME("Got %u swapchain images, expected %u.\n", image_count, swapchain->desc.BufferCount);
1583 return E_NOTIMPL;
1586 queue_family_index = vkd3d_get_vk_queue_family_index(queue);
1588 if (queue_desc.Type == D3D12_COMMAND_LIST_TYPE_DIRECT)
1590 TRACE("Creating user swapchain buffers.\n");
1592 if (FAILED(hr = d3d12_swapchain_create_user_buffers(swapchain, vk_format)))
1593 return hr;
1595 if (FAILED(hr = d3d12_swapchain_prepare_command_buffers(swapchain, queue_family_index)))
1596 return hr;
1599 if (swapchain->buffers[0])
1600 return S_OK;
1602 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1604 if (swapchain->vk_images[i])
1606 resource_info.vk_image = swapchain->vk_images[i];
1607 resource_info.present_state = D3D12_RESOURCE_STATE_COPY_SOURCE;
1609 else
1611 resource_info.vk_image = swapchain->vk_swapchain_images[i];
1612 resource_info.present_state = D3D12_RESOURCE_STATE_PRESENT;
1615 if (FAILED(hr = vkd3d_create_image_resource(device, &resource_info, &swapchain->buffers[i])))
1617 WARN("Failed to create vkd3d resource for Vulkan image %u, hr %#x.\n", i, hr);
1618 return hr;
1621 vkd3d_resource_incref(swapchain->buffers[i]);
1622 ID3D12Resource_Release(swapchain->buffers[i]);
1625 return S_OK;
1628 static VkResult d3d12_swapchain_acquire_next_vulkan_image(struct d3d12_swapchain *swapchain)
1630 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1631 VkDevice vk_device = swapchain->vk_device;
1632 VkFence vk_fence = swapchain->vk_fence;
1633 VkResult vr;
1635 swapchain->vk_image_index = INVALID_VK_IMAGE_INDEX;
1637 if ((vr = vk_funcs->p_vkAcquireNextImageKHR(vk_device, swapchain->vk_swapchain, UINT64_MAX,
1638 VK_NULL_HANDLE, vk_fence, &swapchain->vk_image_index)) < 0)
1640 WARN("Failed to acquire next Vulkan image, vr %d.\n", vr);
1641 return vr;
1644 if ((vr = vk_funcs->p_vkWaitForFences(vk_device, 1, &vk_fence, VK_TRUE, UINT64_MAX)) != VK_SUCCESS)
1646 ERR("Failed to wait for fence, vr %d.\n", vr);
1647 return vr;
1649 if ((vr = vk_funcs->p_vkResetFences(vk_device, 1, &vk_fence)) < 0)
1650 ERR("Failed to reset fence, vr %d.\n", vr);
1652 return vr;
1655 static VkResult d3d12_swapchain_acquire_next_back_buffer(struct d3d12_swapchain *swapchain)
1657 VkResult vr;
1659 /* If we don't have user images, we need to acquire a Vulkan image in order
1660 * to get the correct value for the current back buffer index. */
1661 if (d3d12_swapchain_has_user_images(swapchain))
1662 return VK_SUCCESS;
1664 if ((vr = d3d12_swapchain_acquire_next_vulkan_image(swapchain)) >= 0)
1665 swapchain->current_buffer_index = swapchain->vk_image_index;
1666 else
1667 ERR("Failed to acquire Vulkan image, vr %d.\n", vr);
1669 return vr;
1672 static void d3d12_swapchain_destroy_buffers(struct d3d12_swapchain *swapchain, BOOL destroy_user_buffers)
1674 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1675 VkQueue vk_queue;
1676 unsigned int i;
1678 if (swapchain->command_queue)
1680 if ((vk_queue = vkd3d_acquire_vk_queue(swapchain->command_queue)))
1682 vk_funcs->p_vkQueueWaitIdle(vk_queue);
1684 vkd3d_release_vk_queue(swapchain->command_queue);
1686 else
1688 WARN("Failed to acquire Vulkan queue.\n");
1692 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1694 if (swapchain->buffers[i] && (destroy_user_buffers || !d3d12_swapchain_has_user_images(swapchain)))
1696 vkd3d_resource_decref(swapchain->buffers[i]);
1697 swapchain->buffers[i] = NULL;
1699 if (swapchain->vk_device && destroy_user_buffers)
1701 vk_funcs->p_vkDestroyImage(swapchain->vk_device, swapchain->vk_images[i], NULL);
1702 swapchain->vk_images[i] = VK_NULL_HANDLE;
1706 if (swapchain->vk_device)
1708 for (i = 0; i < swapchain->buffer_count; ++i)
1710 vk_funcs->p_vkDestroySemaphore(swapchain->vk_device, swapchain->vk_semaphores[i], NULL);
1711 swapchain->vk_semaphores[i] = VK_NULL_HANDLE;
1713 if (destroy_user_buffers)
1715 vk_funcs->p_vkFreeMemory(swapchain->vk_device, swapchain->vk_memory, NULL);
1716 swapchain->vk_memory = VK_NULL_HANDLE;
1718 vk_funcs->p_vkDestroyCommandPool(swapchain->vk_device, swapchain->vk_cmd_pool, NULL);
1719 swapchain->vk_cmd_pool = VK_NULL_HANDLE;
1723 static HRESULT d3d12_swapchain_create_vulkan_swapchain(struct d3d12_swapchain *swapchain)
1725 VkPhysicalDevice vk_physical_device = swapchain->vk_physical_device;
1726 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1727 VkSwapchainCreateInfoKHR vk_swapchain_desc;
1728 VkDevice vk_device = swapchain->vk_device;
1729 VkFormat vk_format, vk_swapchain_format;
1730 unsigned int width, height, image_count;
1731 VkSurfaceCapabilitiesKHR surface_caps;
1732 VkSwapchainKHR vk_swapchain;
1733 VkImageUsageFlags usage;
1734 VkResult vr;
1735 HRESULT hr;
1737 if (!(vk_format = vkd3d_get_vk_format(swapchain->desc.Format)))
1739 WARN("Invalid format %#x.\n", swapchain->desc.Format);
1740 return DXGI_ERROR_INVALID_CALL;
1743 if (FAILED(hr = select_vk_format(vk_funcs, vk_physical_device,
1744 swapchain->vk_surface, &swapchain->desc, &vk_swapchain_format)))
1745 return hr;
1747 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical_device,
1748 swapchain->vk_surface, &surface_caps)) < 0)
1750 WARN("Failed to get surface capabilities, vr %d.\n", vr);
1751 return hresult_from_vk_result(vr);
1754 image_count = swapchain->desc.BufferCount;
1755 image_count = max(image_count, surface_caps.minImageCount);
1756 if (surface_caps.maxImageCount)
1757 image_count = min(image_count, surface_caps.maxImageCount);
1759 if (image_count != swapchain->desc.BufferCount)
1761 WARN("Buffer count %u is not supported (%u-%u).\n", swapchain->desc.BufferCount,
1762 surface_caps.minImageCount, surface_caps.maxImageCount);
1765 width = swapchain->desc.Width;
1766 height = swapchain->desc.Height;
1767 width = max(width, surface_caps.minImageExtent.width);
1768 width = min(width, surface_caps.maxImageExtent.width);
1769 height = max(height, surface_caps.minImageExtent.height);
1770 height = min(height, surface_caps.maxImageExtent.height);
1772 if (width != swapchain->desc.Width || height != swapchain->desc.Height)
1774 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
1775 swapchain->desc.Width, swapchain->desc.Height,
1776 surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width,
1777 surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height);
1780 TRACE("Vulkan swapchain extent %ux%u.\n", width, height);
1782 if (!(surface_caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR))
1784 FIXME("Unsupported alpha mode.\n");
1785 return DXGI_ERROR_UNSUPPORTED;
1788 usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1789 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1790 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1791 if (!(usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) || !(usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT))
1792 WARN("Transfer not supported for swapchain images.\n");
1794 vk_swapchain_desc.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
1795 vk_swapchain_desc.pNext = NULL;
1796 vk_swapchain_desc.flags = 0;
1797 vk_swapchain_desc.surface = swapchain->vk_surface;
1798 vk_swapchain_desc.minImageCount = image_count;
1799 vk_swapchain_desc.imageFormat = vk_swapchain_format;
1800 vk_swapchain_desc.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
1801 vk_swapchain_desc.imageExtent.width = width;
1802 vk_swapchain_desc.imageExtent.height = height;
1803 vk_swapchain_desc.imageArrayLayers = 1;
1804 vk_swapchain_desc.imageUsage = usage;
1805 vk_swapchain_desc.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
1806 vk_swapchain_desc.queueFamilyIndexCount = 0;
1807 vk_swapchain_desc.pQueueFamilyIndices = NULL;
1808 vk_swapchain_desc.preTransform = surface_caps.currentTransform;
1809 vk_swapchain_desc.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1810 vk_swapchain_desc.presentMode = swapchain->present_mode;
1811 vk_swapchain_desc.clipped = VK_TRUE;
1812 vk_swapchain_desc.oldSwapchain = swapchain->vk_swapchain;
1813 if ((vr = vk_funcs->p_vkCreateSwapchainKHR(vk_device, &vk_swapchain_desc, NULL, &vk_swapchain)) < 0)
1815 WARN("Failed to create Vulkan swapchain, vr %d.\n", vr);
1816 return hresult_from_vk_result(vr);
1819 if (swapchain->vk_swapchain)
1820 vk_funcs->p_vkDestroySwapchainKHR(swapchain->vk_device, swapchain->vk_swapchain, NULL);
1822 swapchain->vk_swapchain = vk_swapchain;
1823 swapchain->vk_swapchain_width = width;
1824 swapchain->vk_swapchain_height = height;
1826 swapchain->vk_image_index = INVALID_VK_IMAGE_INDEX;
1828 return d3d12_swapchain_create_buffers(swapchain, vk_swapchain_format, vk_format);
1831 static HRESULT d3d12_swapchain_recreate_vulkan_swapchain(struct d3d12_swapchain *swapchain)
1833 VkResult vr;
1834 HRESULT hr;
1836 if (SUCCEEDED(hr = d3d12_swapchain_create_vulkan_swapchain(swapchain)))
1838 vr = d3d12_swapchain_acquire_next_back_buffer(swapchain);
1839 hr = hresult_from_vk_result(vr);
1841 else
1843 ERR("Failed to recreate Vulkan swapchain, hr %#x.\n", hr);
1846 return hr;
1849 static inline struct d3d12_swapchain *d3d12_swapchain_from_IDXGISwapChain4(IDXGISwapChain4 *iface)
1851 return CONTAINING_RECORD(iface, struct d3d12_swapchain, IDXGISwapChain4_iface);
1854 /* IUnknown methods */
1856 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_QueryInterface(IDXGISwapChain4 *iface, REFIID iid, void **object)
1858 TRACE("iface %p, iid %s, object %p.\n", iface, debugstr_guid(iid), object);
1860 if (IsEqualGUID(iid, &IID_IUnknown)
1861 || IsEqualGUID(iid, &IID_IDXGIObject)
1862 || IsEqualGUID(iid, &IID_IDXGIDeviceSubObject)
1863 || IsEqualGUID(iid, &IID_IDXGISwapChain)
1864 || IsEqualGUID(iid, &IID_IDXGISwapChain1)
1865 || IsEqualGUID(iid, &IID_IDXGISwapChain2)
1866 || IsEqualGUID(iid, &IID_IDXGISwapChain3)
1867 || IsEqualGUID(iid, &IID_IDXGISwapChain4))
1869 IUnknown_AddRef(iface);
1870 *object = iface;
1871 return S_OK;
1874 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
1876 *object = NULL;
1877 return E_NOINTERFACE;
1880 static ULONG STDMETHODCALLTYPE d3d12_swapchain_AddRef(IDXGISwapChain4 *iface)
1882 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1883 ULONG refcount = InterlockedIncrement(&swapchain->refcount);
1885 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
1887 return refcount;
1890 static void d3d12_swapchain_destroy(struct d3d12_swapchain *swapchain)
1892 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1893 void *vulkan_module = vk_funcs->vulkan_module;
1895 d3d12_swapchain_destroy_buffers(swapchain, TRUE);
1897 if (swapchain->frame_latency_event)
1898 CloseHandle(swapchain->frame_latency_event);
1900 if (swapchain->frame_latency_fence)
1901 ID3D12Fence_Release(swapchain->frame_latency_fence);
1903 if (swapchain->command_queue)
1904 ID3D12CommandQueue_Release(swapchain->command_queue);
1906 wined3d_private_store_cleanup(&swapchain->private_store);
1908 if (swapchain->vk_device)
1910 vk_funcs->p_vkDestroyFence(swapchain->vk_device, swapchain->vk_fence, NULL);
1911 vk_funcs->p_vkDestroySwapchainKHR(swapchain->vk_device, swapchain->vk_swapchain, NULL);
1914 if (swapchain->vk_instance)
1915 vk_funcs->p_vkDestroySurfaceKHR(swapchain->vk_instance, swapchain->vk_surface, NULL);
1917 if (swapchain->target)
1919 WARN("Destroying fullscreen swapchain.\n");
1920 IDXGIOutput_Release(swapchain->target);
1923 if (swapchain->device)
1924 ID3D12Device_Release(swapchain->device);
1926 if (swapchain->factory)
1927 IWineDXGIFactory_Release(swapchain->factory);
1929 close_library(vulkan_module);
1931 wined3d_swapchain_state_destroy(swapchain->state);
1934 static ULONG STDMETHODCALLTYPE d3d12_swapchain_Release(IDXGISwapChain4 *iface)
1936 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1937 ULONG refcount = InterlockedDecrement(&swapchain->refcount);
1939 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
1941 if (!refcount)
1943 d3d12_swapchain_destroy(swapchain);
1944 heap_free(swapchain);
1947 return refcount;
1950 /* IDXGIObject methods */
1952 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetPrivateData(IDXGISwapChain4 *iface,
1953 REFGUID guid, UINT data_size, const void *data)
1955 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1957 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1959 return dxgi_set_private_data(&swapchain->private_store, guid, data_size, data);
1962 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetPrivateDataInterface(IDXGISwapChain4 *iface,
1963 REFGUID guid, const IUnknown *object)
1965 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1967 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
1969 return dxgi_set_private_data_interface(&swapchain->private_store, guid, object);
1972 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetPrivateData(IDXGISwapChain4 *iface,
1973 REFGUID guid, UINT *data_size, void *data)
1975 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1977 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1979 return dxgi_get_private_data(&swapchain->private_store, guid, data_size, data);
1982 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetParent(IDXGISwapChain4 *iface, REFIID iid, void **parent)
1984 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1986 TRACE("iface %p, iid %s, parent %p.\n", iface, debugstr_guid(iid), parent);
1988 return IWineDXGIFactory_QueryInterface(swapchain->factory, iid, parent);
1991 /* IDXGIDeviceSubObject methods */
1993 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetDevice(IDXGISwapChain4 *iface, REFIID iid, void **device)
1995 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
1997 TRACE("iface %p, iid %s, device %p.\n", iface, debugstr_guid(iid), device);
1999 return ID3D12Device_QueryInterface(swapchain->device, iid, device);
2002 /* IDXGISwapChain methods */
2004 static HRESULT d3d12_swapchain_set_sync_interval(struct d3d12_swapchain *swapchain,
2005 unsigned int sync_interval)
2007 VkPresentModeKHR present_mode;
2009 switch (sync_interval)
2011 case 0:
2012 present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
2013 break;
2014 default:
2015 FIXME("Unsupported sync interval %u.\n", sync_interval);
2016 case 1:
2017 present_mode = VK_PRESENT_MODE_FIFO_KHR;
2018 break;
2021 if (swapchain->present_mode == present_mode)
2022 return S_OK;
2024 if (!d3d12_swapchain_has_user_images(swapchain))
2026 FIXME("Cannot recreate swapchain without user images.\n");
2027 return S_OK;
2030 if (!d3d12_swapchain_is_present_mode_supported(swapchain, present_mode))
2032 FIXME("Vulkan present mode %#x is not supported.\n", present_mode);
2033 return S_OK;
2036 d3d12_swapchain_destroy_buffers(swapchain, FALSE);
2037 swapchain->present_mode = present_mode;
2038 return d3d12_swapchain_recreate_vulkan_swapchain(swapchain);
2041 static VkResult d3d12_swapchain_queue_present(struct d3d12_swapchain *swapchain, VkQueue vk_queue)
2043 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
2044 VkPresentInfoKHR present_info;
2045 VkSubmitInfo submit_info;
2046 VkResult vr;
2048 if (swapchain->vk_image_index == INVALID_VK_IMAGE_INDEX)
2050 if ((vr = d3d12_swapchain_acquire_next_vulkan_image(swapchain)) < 0)
2051 return vr;
2054 assert(swapchain->vk_image_index < swapchain->buffer_count);
2056 present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
2057 present_info.pNext = NULL;
2058 present_info.waitSemaphoreCount = 0;
2059 present_info.pWaitSemaphores = NULL;
2060 present_info.swapchainCount = 1;
2061 present_info.pSwapchains = &swapchain->vk_swapchain;
2062 present_info.pImageIndices = &swapchain->vk_image_index;
2063 present_info.pResults = NULL;
2065 if (d3d12_swapchain_has_user_images(swapchain))
2067 /* blit */
2068 VkCommandBuffer vk_cmd_buffer = swapchain->vk_cmd_buffers[swapchain->vk_image_index];
2069 VkImage vk_dst_image = swapchain->vk_swapchain_images[swapchain->vk_image_index];
2070 VkImage vk_src_image = swapchain->vk_images[swapchain->current_buffer_index];
2072 if ((vr = vk_funcs->p_vkResetCommandBuffer(vk_cmd_buffer, 0)) < 0)
2074 ERR("Failed to reset command buffer, vr %d.\n", vr);
2075 return vr;
2078 if ((vr = d3d12_swapchain_record_swapchain_blit(swapchain,
2079 vk_cmd_buffer, vk_dst_image, vk_src_image)) < 0 )
2080 return vr;
2082 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
2083 submit_info.pNext = NULL;
2084 submit_info.waitSemaphoreCount = 0;
2085 submit_info.pWaitSemaphores = NULL;
2086 submit_info.pWaitDstStageMask = NULL;
2087 submit_info.commandBufferCount = 1;
2088 submit_info.pCommandBuffers = &vk_cmd_buffer;
2089 submit_info.signalSemaphoreCount = 1;
2090 submit_info.pSignalSemaphores = &swapchain->vk_semaphores[swapchain->vk_image_index];
2092 if ((vr = vk_funcs->p_vkQueueSubmit(vk_queue, 1, &submit_info, VK_NULL_HANDLE)) < 0)
2094 ERR("Failed to blit swapchain buffer, vr %d.\n", vr);
2095 return vr;
2098 present_info.waitSemaphoreCount = 1;
2099 present_info.pWaitSemaphores = &swapchain->vk_semaphores[swapchain->vk_image_index];
2102 if ((vr = vk_funcs->p_vkQueuePresentKHR(vk_queue, &present_info)) >= 0)
2103 swapchain->vk_image_index = INVALID_VK_IMAGE_INDEX;
2105 return vr;
2108 static HRESULT d3d12_swapchain_present(struct d3d12_swapchain *swapchain,
2109 unsigned int sync_interval, unsigned int flags)
2111 HANDLE frame_latency_event;
2112 VkQueue vk_queue;
2113 VkResult vr;
2114 HRESULT hr;
2116 if (sync_interval > 4)
2118 WARN("Invalid sync interval %u.\n", sync_interval);
2119 return DXGI_ERROR_INVALID_CALL;
2122 if (flags & ~DXGI_PRESENT_TEST)
2123 FIXME("Unimplemented flags %#x.\n", flags);
2124 if (flags & DXGI_PRESENT_TEST)
2126 WARN("Returning S_OK for DXGI_PRESENT_TEST.\n");
2127 return S_OK;
2130 if (FAILED(hr = d3d12_swapchain_set_sync_interval(swapchain, sync_interval)))
2131 return hr;
2133 if (!(vk_queue = vkd3d_acquire_vk_queue(swapchain->command_queue)))
2135 ERR("Failed to acquire Vulkan queue.\n");
2136 return E_FAIL;
2139 vr = d3d12_swapchain_queue_present(swapchain, vk_queue);
2140 if (vr == VK_ERROR_OUT_OF_DATE_KHR)
2142 vkd3d_release_vk_queue(swapchain->command_queue);
2144 if (!d3d12_swapchain_has_user_images(swapchain))
2146 FIXME("Cannot recreate swapchain without user images.\n");
2147 return DXGI_STATUS_MODE_CHANGED;
2150 TRACE("Recreating Vulkan swapchain.\n");
2152 d3d12_swapchain_destroy_buffers(swapchain, FALSE);
2153 if (FAILED(hr = d3d12_swapchain_recreate_vulkan_swapchain(swapchain)))
2154 return hr;
2156 if (!(vk_queue = vkd3d_acquire_vk_queue(swapchain->command_queue)))
2158 ERR("Failed to acquire Vulkan queue.\n");
2159 return E_FAIL;
2162 if ((vr = d3d12_swapchain_queue_present(swapchain, vk_queue)) < 0)
2163 ERR("Failed to present after recreating swapchain, vr %d.\n", vr);
2166 vkd3d_release_vk_queue(swapchain->command_queue);
2168 if (vr < 0)
2170 ERR("Failed to queue present, vr %d.\n", vr);
2171 return hresult_from_vk_result(vr);
2174 if ((frame_latency_event = swapchain->frame_latency_event))
2176 ++swapchain->frame_number;
2178 if (FAILED(hr = ID3D12CommandQueue_Signal(swapchain->command_queue,
2179 swapchain->frame_latency_fence, swapchain->frame_number)))
2181 ERR("Failed to signal frame latency fence, hr %#x.\n", hr);
2182 return hr;
2185 if (FAILED(hr = ID3D12Fence_SetEventOnCompletion(swapchain->frame_latency_fence,
2186 swapchain->frame_number - swapchain->frame_latency, frame_latency_event)))
2188 ERR("Failed to enqueue frame latency event, hr %#x.\n", hr);
2189 return hr;
2193 swapchain->current_buffer_index = (swapchain->current_buffer_index + 1) % swapchain->desc.BufferCount;
2194 vr = d3d12_swapchain_acquire_next_back_buffer(swapchain);
2195 if (vr == VK_ERROR_OUT_OF_DATE_KHR)
2197 if (!d3d12_swapchain_has_user_images(swapchain))
2199 FIXME("Cannot recreate swapchain without user images.\n");
2200 return DXGI_STATUS_MODE_CHANGED;
2203 TRACE("Recreating Vulkan swapchain.\n");
2205 d3d12_swapchain_destroy_buffers(swapchain, FALSE);
2206 return d3d12_swapchain_recreate_vulkan_swapchain(swapchain);
2208 if (vr < 0)
2209 ERR("Failed to acquire next Vulkan image, vr %d.\n", vr);
2210 return hresult_from_vk_result(vr);
2213 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_Present(IDXGISwapChain4 *iface, UINT sync_interval, UINT flags)
2215 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2217 TRACE("iface %p, sync_interval %u, flags %#x.\n", iface, sync_interval, flags);
2219 return d3d12_swapchain_present(swapchain, sync_interval, flags);
2222 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetBuffer(IDXGISwapChain4 *iface,
2223 UINT buffer_idx, REFIID iid, void **surface)
2225 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2227 TRACE("iface %p, buffer_idx %u, iid %s, surface %p.\n",
2228 iface, buffer_idx, debugstr_guid(iid), surface);
2230 if (buffer_idx >= swapchain->desc.BufferCount)
2232 WARN("Invalid buffer index %u.\n", buffer_idx);
2233 return DXGI_ERROR_INVALID_CALL;
2236 assert(swapchain->buffers[buffer_idx]);
2237 return ID3D12Resource_QueryInterface(swapchain->buffers[buffer_idx], iid, surface);
2240 static HRESULT STDMETHODCALLTYPE DECLSPEC_HOTPATCH d3d12_swapchain_SetFullscreenState(IDXGISwapChain4 *iface,
2241 BOOL fullscreen, IDXGIOutput *target)
2243 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2244 DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc = &swapchain->fullscreen_desc;
2245 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc = &swapchain->desc;
2246 struct wined3d_swapchain_desc wined3d_desc;
2247 HWND window = swapchain->window;
2248 HRESULT hr;
2250 TRACE("iface %p, fullscreen %#x, target %p.\n", iface, fullscreen, target);
2252 if (!fullscreen && target)
2254 WARN("Invalid call.\n");
2255 return DXGI_ERROR_INVALID_CALL;
2258 if (target)
2260 IDXGIOutput_AddRef(target);
2262 else if (FAILED(hr = IDXGISwapChain4_GetContainingOutput(iface, &target)))
2264 WARN("Failed to get target output for swapchain, hr %#x.\n", hr);
2265 return hr;
2268 if (FAILED(hr = wined3d_swapchain_desc_from_dxgi(&wined3d_desc, target, window, swapchain_desc,
2269 fullscreen_desc)))
2270 goto fail;
2271 wined3d_mutex_lock();
2272 wined3d_desc.windowed = !fullscreen;
2273 hr = wined3d_swapchain_state_set_fullscreen(swapchain->state, &wined3d_desc, NULL);
2274 wined3d_mutex_unlock();
2275 if (FAILED(hr))
2276 goto fail;
2278 fullscreen_desc->Windowed = wined3d_desc.windowed;
2279 if (!fullscreen)
2281 IDXGIOutput_Release(target);
2282 target = NULL;
2285 if (swapchain->target)
2286 IDXGIOutput_Release(swapchain->target);
2287 swapchain->target = target;
2289 return S_OK;
2291 fail:
2292 IDXGIOutput_Release(target);
2294 return DXGI_ERROR_NOT_CURRENTLY_AVAILABLE;
2297 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetFullscreenState(IDXGISwapChain4 *iface,
2298 BOOL *fullscreen, IDXGIOutput **target)
2300 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2301 BOOL windowed;
2302 HRESULT hr;
2304 TRACE("iface %p, fullscreen %p, target %p.\n", iface, fullscreen, target);
2306 if (fullscreen || target)
2308 wined3d_mutex_lock();
2309 windowed = wined3d_swapchain_state_is_windowed(swapchain->state);
2310 wined3d_mutex_unlock();
2313 if (fullscreen)
2314 *fullscreen = !windowed;
2316 if (target)
2318 if (!windowed)
2320 if (!swapchain->target && FAILED(hr = IDXGISwapChain4_GetContainingOutput(iface,
2321 &swapchain->target)))
2322 return hr;
2324 *target = swapchain->target;
2325 IDXGIOutput_AddRef(*target);
2327 else
2329 *target = NULL;
2333 return S_OK;
2336 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetDesc(IDXGISwapChain4 *iface, DXGI_SWAP_CHAIN_DESC *desc)
2338 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2339 const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc = &swapchain->fullscreen_desc;
2340 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc = &swapchain->desc;
2341 BOOL windowed;
2343 TRACE("iface %p, desc %p.\n", iface, desc);
2345 if (!desc)
2347 WARN("Invalid pointer.\n");
2348 return E_INVALIDARG;
2351 wined3d_mutex_lock();
2352 windowed = wined3d_swapchain_state_is_windowed(swapchain->state);
2353 wined3d_mutex_unlock();
2355 desc->BufferDesc.Width = swapchain_desc->Width;
2356 desc->BufferDesc.Height = swapchain_desc->Height;
2357 desc->BufferDesc.RefreshRate = fullscreen_desc->RefreshRate;
2358 desc->BufferDesc.Format = swapchain_desc->Format;
2359 desc->BufferDesc.ScanlineOrdering = fullscreen_desc->ScanlineOrdering;
2360 desc->BufferDesc.Scaling = fullscreen_desc->Scaling;
2361 desc->SampleDesc = swapchain_desc->SampleDesc;
2362 desc->BufferUsage = swapchain_desc->BufferUsage;
2363 desc->BufferCount = swapchain_desc->BufferCount;
2364 desc->OutputWindow = swapchain->window;
2365 desc->Windowed = windowed;
2366 desc->SwapEffect = swapchain_desc->SwapEffect;
2367 desc->Flags = swapchain_desc->Flags;
2369 return S_OK;
2372 static HRESULT d3d12_swapchain_resize_buffers(struct d3d12_swapchain *swapchain,
2373 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
2375 DXGI_SWAP_CHAIN_DESC1 *desc, new_desc;
2376 unsigned int i;
2377 ULONG refcount;
2379 if (flags)
2380 FIXME("Ignoring flags %#x.\n", flags);
2382 for (i = 0; i < swapchain->desc.BufferCount; ++i)
2384 ID3D12Resource_AddRef(swapchain->buffers[i]);
2385 if ((refcount = ID3D12Resource_Release(swapchain->buffers[i])))
2387 WARN("Buffer %p has %u references left.\n", swapchain->buffers[i], refcount);
2388 return DXGI_ERROR_INVALID_CALL;
2392 desc = &swapchain->desc;
2393 new_desc = swapchain->desc;
2395 if (buffer_count)
2396 new_desc.BufferCount = buffer_count;
2397 if (!width || !height)
2399 RECT client_rect;
2401 if (!GetClientRect(swapchain->window, &client_rect))
2403 WARN("Failed to get client rect, last error %#x.\n", GetLastError());
2404 return DXGI_ERROR_INVALID_CALL;
2407 if (!width)
2408 width = client_rect.right;
2409 if (!height)
2410 height = client_rect.bottom;
2412 new_desc.Width = width;
2413 new_desc.Height = height;
2415 if (format)
2416 new_desc.Format = format;
2418 if (!dxgi_validate_swapchain_desc(&new_desc))
2419 return DXGI_ERROR_INVALID_CALL;
2421 if (desc->Width == new_desc.Width && desc->Height == new_desc.Height
2422 && desc->Format == new_desc.Format && desc->BufferCount == new_desc.BufferCount)
2423 return S_OK;
2425 d3d12_swapchain_destroy_buffers(swapchain, TRUE);
2426 swapchain->desc = new_desc;
2427 return d3d12_swapchain_recreate_vulkan_swapchain(swapchain);
2430 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_ResizeBuffers(IDXGISwapChain4 *iface,
2431 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
2433 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2435 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x.\n",
2436 iface, buffer_count, width, height, debug_dxgi_format(format), flags);
2438 return d3d12_swapchain_resize_buffers(swapchain, buffer_count, width, height, format, flags);
2441 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_ResizeTarget(IDXGISwapChain4 *iface,
2442 const DXGI_MODE_DESC *target_mode_desc)
2444 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2446 TRACE("iface %p, target_mode_desc %p.\n", iface, target_mode_desc);
2448 return dxgi_swapchain_resize_target(swapchain->state, target_mode_desc);
2451 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetContainingOutput(IDXGISwapChain4 *iface,
2452 IDXGIOutput **output)
2454 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2455 IUnknown *device_parent;
2456 IDXGIFactory *factory;
2457 IDXGIAdapter *adapter;
2458 HRESULT hr;
2460 TRACE("iface %p, output %p.\n", iface, output);
2462 if (swapchain->target)
2464 IDXGIOutput_AddRef(*output = swapchain->target);
2465 return S_OK;
2468 device_parent = vkd3d_get_device_parent(swapchain->device);
2470 if (FAILED(hr = IUnknown_QueryInterface(device_parent, &IID_IDXGIAdapter, (void **)&adapter)))
2472 WARN("Failed to get adapter, hr %#x.\n", hr);
2473 return hr;
2476 if (FAILED(hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory)))
2478 WARN("Failed to get factory, hr %#x.\n", hr);
2479 IDXGIAdapter_Release(adapter);
2480 return hr;
2483 hr = dxgi_get_output_from_window(factory, swapchain->window, output);
2484 IDXGIFactory_Release(factory);
2485 IDXGIAdapter_Release(adapter);
2486 return hr;
2489 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetFrameStatistics(IDXGISwapChain4 *iface,
2490 DXGI_FRAME_STATISTICS *stats)
2492 FIXME("iface %p, stats %p stub!\n", iface, stats);
2494 return E_NOTIMPL;
2497 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetLastPresentCount(IDXGISwapChain4 *iface,
2498 UINT *last_present_count)
2500 FIXME("iface %p, last_present_count %p stub!\n", iface, last_present_count);
2502 return E_NOTIMPL;
2505 /* IDXGISwapChain1 methods */
2507 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetDesc1(IDXGISwapChain4 *iface, DXGI_SWAP_CHAIN_DESC1 *desc)
2509 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2511 TRACE("iface %p, desc %p.\n", iface, desc);
2513 if (!desc)
2515 WARN("Invalid pointer.\n");
2516 return E_INVALIDARG;
2519 *desc = swapchain->desc;
2520 return S_OK;
2523 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetFullscreenDesc(IDXGISwapChain4 *iface,
2524 DXGI_SWAP_CHAIN_FULLSCREEN_DESC *desc)
2526 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2527 BOOL windowed;
2529 TRACE("iface %p, desc %p.\n", iface, desc);
2531 if (!desc)
2533 WARN("Invalid pointer.\n");
2534 return E_INVALIDARG;
2537 wined3d_mutex_lock();
2538 windowed = wined3d_swapchain_state_is_windowed(swapchain->state);
2539 wined3d_mutex_unlock();
2541 *desc = swapchain->fullscreen_desc;
2542 desc->Windowed = windowed;
2543 return S_OK;
2546 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetHwnd(IDXGISwapChain4 *iface, HWND *hwnd)
2548 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2550 TRACE("iface %p, hwnd %p.\n", iface, hwnd);
2552 if (!hwnd)
2554 WARN("Invalid pointer.\n");
2555 return DXGI_ERROR_INVALID_CALL;
2558 *hwnd = swapchain->window;
2559 return S_OK;
2562 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetCoreWindow(IDXGISwapChain4 *iface,
2563 REFIID iid, void **core_window)
2565 FIXME("iface %p, iid %s, core_window %p stub!\n", iface, debugstr_guid(iid), core_window);
2567 if (core_window)
2568 *core_window = NULL;
2570 return DXGI_ERROR_INVALID_CALL;
2573 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_Present1(IDXGISwapChain4 *iface,
2574 UINT sync_interval, UINT flags, const DXGI_PRESENT_PARAMETERS *present_parameters)
2576 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2578 TRACE("iface %p, sync_interval %u, flags %#x, present_parameters %p.\n",
2579 iface, sync_interval, flags, present_parameters);
2581 if (present_parameters)
2582 FIXME("Ignored present parameters %p.\n", present_parameters);
2584 return d3d12_swapchain_present(swapchain, sync_interval, flags);
2587 static BOOL STDMETHODCALLTYPE d3d12_swapchain_IsTemporaryMonoSupported(IDXGISwapChain4 *iface)
2589 FIXME("iface %p stub!\n", iface);
2591 return FALSE;
2594 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetRestrictToOutput(IDXGISwapChain4 *iface, IDXGIOutput **output)
2596 FIXME("iface %p, output %p stub!\n", iface, output);
2598 if (!output)
2600 WARN("Invalid pointer.\n");
2601 return E_INVALIDARG;
2604 *output = NULL;
2605 return E_NOTIMPL;
2608 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetBackgroundColor(IDXGISwapChain4 *iface, const DXGI_RGBA *color)
2610 FIXME("iface %p, color %p stub!\n", iface, color);
2612 return E_NOTIMPL;
2615 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetBackgroundColor(IDXGISwapChain4 *iface, DXGI_RGBA *color)
2617 FIXME("iface %p, color %p stub!\n", iface, color);
2619 return E_NOTIMPL;
2622 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetRotation(IDXGISwapChain4 *iface, DXGI_MODE_ROTATION rotation)
2624 FIXME("iface %p, rotation %#x stub!\n", iface, rotation);
2626 return E_NOTIMPL;
2629 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetRotation(IDXGISwapChain4 *iface, DXGI_MODE_ROTATION *rotation)
2631 FIXME("iface %p, rotation %p stub!\n", iface, rotation);
2633 return E_NOTIMPL;
2636 /* IDXGISwapChain2 methods */
2638 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetSourceSize(IDXGISwapChain4 *iface, UINT width, UINT height)
2640 FIXME("iface %p, width %u, height %u stub!\n", iface, width, height);
2642 return E_NOTIMPL;
2645 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetSourceSize(IDXGISwapChain4 *iface, UINT *width, UINT *height)
2647 FIXME("iface %p, width %p, height %p stub!\n", iface, width, height);
2649 return E_NOTIMPL;
2652 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetMaximumFrameLatency(IDXGISwapChain4 *iface, UINT max_latency)
2654 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2656 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
2658 if (!(swapchain->desc.Flags & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
2660 WARN("DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT not set for swap chain %p.\n", iface);
2661 return DXGI_ERROR_INVALID_CALL;
2664 if (!max_latency)
2666 WARN("Invalid maximum frame latency %u.\n", max_latency);
2667 return DXGI_ERROR_INVALID_CALL;
2670 swapchain->frame_latency = max_latency;
2671 return S_OK;
2674 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetMaximumFrameLatency(IDXGISwapChain4 *iface, UINT *max_latency)
2676 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2678 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
2680 if (!(swapchain->desc.Flags & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
2682 WARN("DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT not set for swap chain %p.\n", iface);
2683 return DXGI_ERROR_INVALID_CALL;
2686 *max_latency = swapchain->frame_latency;
2687 return S_OK;
2690 static HANDLE STDMETHODCALLTYPE d3d12_swapchain_GetFrameLatencyWaitableObject(IDXGISwapChain4 *iface)
2692 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2694 TRACE("iface %p.\n", iface);
2696 return swapchain->frame_latency_event;
2699 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetMatrixTransform(IDXGISwapChain4 *iface,
2700 const DXGI_MATRIX_3X2_F *matrix)
2702 FIXME("iface %p, matrix %p stub!\n", iface, matrix);
2704 return E_NOTIMPL;
2707 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetMatrixTransform(IDXGISwapChain4 *iface,
2708 DXGI_MATRIX_3X2_F *matrix)
2710 FIXME("iface %p, matrix %p stub!\n", iface, matrix);
2712 return E_NOTIMPL;
2715 /* IDXGISwapChain3 methods */
2717 static UINT STDMETHODCALLTYPE d3d12_swapchain_GetCurrentBackBufferIndex(IDXGISwapChain4 *iface)
2719 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2721 TRACE("iface %p.\n", iface);
2723 TRACE("Current back buffer index %u.\n", swapchain->current_buffer_index);
2724 assert(swapchain->current_buffer_index < swapchain->desc.BufferCount);
2725 return swapchain->current_buffer_index;
2728 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_CheckColorSpaceSupport(IDXGISwapChain4 *iface,
2729 DXGI_COLOR_SPACE_TYPE colour_space, UINT *colour_space_support)
2731 UINT support_flags = 0;
2733 FIXME("iface %p, colour_space %#x, colour_space_support %p semi-stub!\n",
2734 iface, colour_space, colour_space_support);
2736 if (!colour_space_support)
2737 return E_INVALIDARG;
2739 if (colour_space == DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709)
2740 support_flags |= DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT;
2742 *colour_space_support = support_flags;
2743 return S_OK;
2746 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetColorSpace1(IDXGISwapChain4 *iface,
2747 DXGI_COLOR_SPACE_TYPE colour_space)
2749 FIXME("iface %p, colour_space %#x semi-stub!\n", iface, colour_space);
2751 if (colour_space != DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709)
2753 WARN("Colour space %u not supported.\n", colour_space);
2754 return E_INVALIDARG;
2757 return S_OK;
2760 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_ResizeBuffers1(IDXGISwapChain4 *iface,
2761 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags,
2762 const UINT *node_mask, IUnknown * const *present_queue)
2764 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain4(iface);
2765 size_t i, count;
2767 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x, "
2768 "node_mask %p, present_queue %p.\n",
2769 iface, buffer_count, width, height, debug_dxgi_format(format), flags, node_mask, present_queue);
2771 if (!node_mask || !present_queue)
2772 return DXGI_ERROR_INVALID_CALL;
2774 count = buffer_count ? buffer_count : swapchain->desc.BufferCount;
2775 for (i = 0; i < count; ++i)
2777 if (node_mask[i] > 1 || !present_queue[i])
2778 return DXGI_ERROR_INVALID_CALL;
2779 if ((ID3D12CommandQueue*)present_queue[i] != swapchain->command_queue)
2780 FIXME("Ignoring present queue %p.\n", present_queue[i]);
2783 return d3d12_swapchain_resize_buffers(swapchain, buffer_count, width, height, format, flags);
2786 /* IDXGISwapChain4 methods */
2788 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetHDRMetaData(IDXGISwapChain4 *iface,
2789 DXGI_HDR_METADATA_TYPE type, UINT size, void *metadata)
2791 FIXME("iface %p, type %#x, size %#x, metadata %p stub!\n", iface, type, size, metadata);
2793 return E_NOTIMPL;
2796 static const struct IDXGISwapChain4Vtbl d3d12_swapchain_vtbl =
2798 /* IUnknown methods */
2799 d3d12_swapchain_QueryInterface,
2800 d3d12_swapchain_AddRef,
2801 d3d12_swapchain_Release,
2802 /* IDXGIObject methods */
2803 d3d12_swapchain_SetPrivateData,
2804 d3d12_swapchain_SetPrivateDataInterface,
2805 d3d12_swapchain_GetPrivateData,
2806 d3d12_swapchain_GetParent,
2807 /* IDXGIDeviceSubObject methods */
2808 d3d12_swapchain_GetDevice,
2809 /* IDXGISwapChain methods */
2810 d3d12_swapchain_Present,
2811 d3d12_swapchain_GetBuffer,
2812 d3d12_swapchain_SetFullscreenState,
2813 d3d12_swapchain_GetFullscreenState,
2814 d3d12_swapchain_GetDesc,
2815 d3d12_swapchain_ResizeBuffers,
2816 d3d12_swapchain_ResizeTarget,
2817 d3d12_swapchain_GetContainingOutput,
2818 d3d12_swapchain_GetFrameStatistics,
2819 d3d12_swapchain_GetLastPresentCount,
2820 /* IDXGISwapChain1 methods */
2821 d3d12_swapchain_GetDesc1,
2822 d3d12_swapchain_GetFullscreenDesc,
2823 d3d12_swapchain_GetHwnd,
2824 d3d12_swapchain_GetCoreWindow,
2825 d3d12_swapchain_Present1,
2826 d3d12_swapchain_IsTemporaryMonoSupported,
2827 d3d12_swapchain_GetRestrictToOutput,
2828 d3d12_swapchain_SetBackgroundColor,
2829 d3d12_swapchain_GetBackgroundColor,
2830 d3d12_swapchain_SetRotation,
2831 d3d12_swapchain_GetRotation,
2832 /* IDXGISwapChain2 methods */
2833 d3d12_swapchain_SetSourceSize,
2834 d3d12_swapchain_GetSourceSize,
2835 d3d12_swapchain_SetMaximumFrameLatency,
2836 d3d12_swapchain_GetMaximumFrameLatency,
2837 d3d12_swapchain_GetFrameLatencyWaitableObject,
2838 d3d12_swapchain_SetMatrixTransform,
2839 d3d12_swapchain_GetMatrixTransform,
2840 /* IDXGISwapChain3 methods */
2841 d3d12_swapchain_GetCurrentBackBufferIndex,
2842 d3d12_swapchain_CheckColorSpaceSupport,
2843 d3d12_swapchain_SetColorSpace1,
2844 d3d12_swapchain_ResizeBuffers1,
2845 /* IDXGISwapChain4 methods */
2846 d3d12_swapchain_SetHDRMetaData,
2849 static BOOL load_vkd3d_functions(void *vkd3d_handle)
2851 #define LOAD_FUNCPTR(f) if (!(f = get_library_proc(vkd3d_handle, #f))) return FALSE;
2852 LOAD_FUNCPTR(vkd3d_acquire_vk_queue)
2853 LOAD_FUNCPTR(vkd3d_create_image_resource)
2854 LOAD_FUNCPTR(vkd3d_get_device_parent)
2855 LOAD_FUNCPTR(vkd3d_get_vk_device)
2856 LOAD_FUNCPTR(vkd3d_get_vk_format)
2857 LOAD_FUNCPTR(vkd3d_get_vk_physical_device)
2858 LOAD_FUNCPTR(vkd3d_get_vk_queue_family_index)
2859 LOAD_FUNCPTR(vkd3d_instance_from_device)
2860 LOAD_FUNCPTR(vkd3d_instance_get_vk_instance)
2861 LOAD_FUNCPTR(vkd3d_release_vk_queue)
2862 LOAD_FUNCPTR(vkd3d_resource_decref)
2863 LOAD_FUNCPTR(vkd3d_resource_incref)
2864 #undef LOAD_FUNCPTR
2866 return TRUE;
2869 static void *vkd3d_handle;
2871 static BOOL WINAPI init_vkd3d_once(INIT_ONCE *once, void *param, void **context)
2873 TRACE("Loading vkd3d %s.\n", SONAME_LIBVKD3D);
2875 if (!(vkd3d_handle = load_library(SONAME_LIBVKD3D)))
2876 return FALSE;
2878 if (!load_vkd3d_functions(vkd3d_handle))
2880 ERR("Failed to load vkd3d functions.\n");
2881 close_library(vkd3d_handle);
2882 vkd3d_handle = NULL;
2883 return FALSE;
2886 return TRUE;
2889 static BOOL init_vkd3d(void)
2891 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
2892 InitOnceExecuteOnce(&init_once, init_vkd3d_once, NULL, NULL);
2893 return !!vkd3d_handle;
2896 static BOOL init_vk_funcs(struct dxgi_vk_funcs *dxgi, VkInstance vk_instance, VkDevice vk_device)
2898 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
2899 PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;
2901 dxgi->vulkan_module = NULL;
2903 if (!(vkGetInstanceProcAddr = load_vulkan(&dxgi->vulkan_module)))
2905 ERR_(winediag)("Failed to load Vulkan.\n");
2906 return FALSE;
2909 vkGetDeviceProcAddr = (void *)vkGetInstanceProcAddr(vk_instance, "vkGetDeviceProcAddr");
2911 #define LOAD_INSTANCE_PFN(name) \
2912 if (!(dxgi->p_##name = (void *)vkGetInstanceProcAddr(vk_instance, #name))) \
2914 ERR("Failed to get instance proc "#name".\n"); \
2915 close_library(dxgi->vulkan_module); \
2916 return FALSE; \
2918 LOAD_INSTANCE_PFN(vkCreateWin32SurfaceKHR)
2919 LOAD_INSTANCE_PFN(vkDestroySurfaceKHR)
2920 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceMemoryProperties)
2921 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR)
2922 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfaceFormatsKHR)
2923 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfacePresentModesKHR)
2924 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfaceSupportKHR)
2925 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceWin32PresentationSupportKHR)
2926 #undef LOAD_INSTANCE_PFN
2928 #define LOAD_DEVICE_PFN(name) \
2929 if (!(dxgi->p_##name = (void *)vkGetDeviceProcAddr(vk_device, #name))) \
2931 ERR("Failed to get device proc "#name".\n"); \
2932 close_library(dxgi->vulkan_module); \
2933 return FALSE; \
2935 LOAD_DEVICE_PFN(vkAcquireNextImageKHR)
2936 LOAD_DEVICE_PFN(vkAllocateCommandBuffers)
2937 LOAD_DEVICE_PFN(vkAllocateMemory)
2938 LOAD_DEVICE_PFN(vkBeginCommandBuffer)
2939 LOAD_DEVICE_PFN(vkBindImageMemory)
2940 LOAD_DEVICE_PFN(vkCmdBlitImage)
2941 LOAD_DEVICE_PFN(vkCmdPipelineBarrier)
2942 LOAD_DEVICE_PFN(vkCreateCommandPool)
2943 LOAD_DEVICE_PFN(vkCreateFence)
2944 LOAD_DEVICE_PFN(vkCreateImage)
2945 LOAD_DEVICE_PFN(vkCreateSemaphore)
2946 LOAD_DEVICE_PFN(vkCreateSwapchainKHR)
2947 LOAD_DEVICE_PFN(vkDestroyCommandPool)
2948 LOAD_DEVICE_PFN(vkDestroyFence)
2949 LOAD_DEVICE_PFN(vkDestroyImage)
2950 LOAD_DEVICE_PFN(vkDestroySemaphore)
2951 LOAD_DEVICE_PFN(vkDestroySwapchainKHR)
2952 LOAD_DEVICE_PFN(vkEndCommandBuffer)
2953 LOAD_DEVICE_PFN(vkFreeMemory)
2954 LOAD_DEVICE_PFN(vkResetCommandBuffer)
2955 LOAD_DEVICE_PFN(vkGetImageMemoryRequirements)
2956 LOAD_DEVICE_PFN(vkGetSwapchainImagesKHR)
2957 LOAD_DEVICE_PFN(vkQueuePresentKHR)
2958 LOAD_DEVICE_PFN(vkQueueSubmit)
2959 LOAD_DEVICE_PFN(vkQueueWaitIdle)
2960 LOAD_DEVICE_PFN(vkResetFences)
2961 LOAD_DEVICE_PFN(vkWaitForFences)
2962 #undef LOAD_DEVICE_PFN
2964 return TRUE;
2967 static inline struct d3d12_swapchain *d3d12_swapchain_from_wined3d_swapchain_state_parent(struct wined3d_swapchain_state_parent *parent)
2969 return CONTAINING_RECORD(parent, struct d3d12_swapchain, state_parent);
2972 static void CDECL d3d12_swapchain_windowed_state_changed(struct wined3d_swapchain_state_parent *parent,
2973 BOOL windowed)
2975 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_wined3d_swapchain_state_parent(parent);
2977 TRACE("parent %p, windowed %d.\n", parent, windowed);
2979 if (windowed && swapchain->target)
2981 IDXGIOutput_Release(swapchain->target);
2982 swapchain->target = NULL;
2986 static const struct wined3d_swapchain_state_parent_ops d3d12_swapchain_state_parent_ops =
2988 d3d12_swapchain_windowed_state_changed,
2991 static HRESULT d3d12_swapchain_init(struct d3d12_swapchain *swapchain, IWineDXGIFactory *factory,
2992 ID3D12Device *device, ID3D12CommandQueue *queue, HWND window,
2993 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc)
2995 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
2996 struct wined3d_swapchain_desc wined3d_desc;
2997 VkWin32SurfaceCreateInfoKHR surface_desc;
2998 VkPhysicalDevice vk_physical_device;
2999 struct dxgi_factory *dxgi_factory;
3000 VkFenceCreateInfo fence_desc;
3001 uint32_t queue_family_index;
3002 VkSurfaceKHR vk_surface;
3003 IUnknown *device_parent;
3004 VkInstance vk_instance;
3005 IDXGIAdapter *adapter;
3006 IDXGIOutput *output;
3007 VkBool32 supported;
3008 VkDevice vk_device;
3009 VkFence vk_fence;
3010 VkResult vr;
3011 HRESULT hr;
3013 if (window == GetDesktopWindow())
3015 WARN("D3D12 swapchain cannot be created on desktop window.\n");
3016 return E_ACCESSDENIED;
3019 swapchain->IDXGISwapChain4_iface.lpVtbl = &d3d12_swapchain_vtbl;
3020 swapchain->state_parent.ops = &d3d12_swapchain_state_parent_ops;
3021 swapchain->refcount = 1;
3023 swapchain->window = window;
3024 swapchain->desc = *swapchain_desc;
3025 swapchain->fullscreen_desc = *fullscreen_desc;
3027 swapchain->present_mode = VK_PRESENT_MODE_FIFO_KHR;
3029 switch (swapchain_desc->SwapEffect)
3031 case DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL:
3032 case DXGI_SWAP_EFFECT_FLIP_DISCARD:
3033 FIXME("Ignoring swap effect %#x.\n", swapchain_desc->SwapEffect);
3034 break;
3035 default:
3036 WARN("Invalid swap effect %#x.\n", swapchain_desc->SwapEffect);
3037 return DXGI_ERROR_INVALID_CALL;
3040 if (!init_vkd3d())
3042 ERR_(winediag)("libvkd3d could not be loaded.\n");
3043 return DXGI_ERROR_UNSUPPORTED;
3046 device_parent = vkd3d_get_device_parent(device);
3047 if (FAILED(hr = IUnknown_QueryInterface(device_parent, &IID_IDXGIAdapter, (void **)&adapter)))
3048 return hr;
3050 if (FAILED(hr = dxgi_get_output_from_window((IDXGIFactory *)factory, window, &output)))
3052 WARN("Failed to get output from window %p, hr %#x.\n", window, hr);
3054 /* FIXME: As wined3d only supports one output currently, even if a window is on a
3055 * non-primary output, the swapchain will use the primary output. Keep this behaviour
3056 * until all outputs are correctly enumerated. Otherwise it will create a regression
3057 * for applications that specify a device window on a non-primary output */
3058 if (FAILED(hr = IDXGIAdapter_EnumOutputs(adapter, 0, &output)))
3060 IDXGIAdapter_Release(adapter);
3061 return hr;
3064 FIXME("Using the primary output for the device window that is on a non-primary output.\n");
3066 IDXGIAdapter_Release(adapter);
3068 hr = wined3d_swapchain_desc_from_dxgi(&wined3d_desc, output, window, swapchain_desc,
3069 fullscreen_desc);
3070 if (FAILED(hr))
3072 IDXGIOutput_Release(output);
3073 return hr;
3076 dxgi_factory = unsafe_impl_from_IDXGIFactory((IDXGIFactory *)factory);
3077 if (FAILED(hr = wined3d_swapchain_state_create(&wined3d_desc, window, dxgi_factory->wined3d,
3078 &swapchain->state_parent, &swapchain->state)))
3080 IDXGIOutput_Release(output);
3081 return hr;
3084 if (!fullscreen_desc->Windowed)
3086 hr = wined3d_swapchain_state_set_fullscreen(swapchain->state, &wined3d_desc, NULL);
3087 if (FAILED(hr))
3089 wined3d_swapchain_state_destroy(swapchain->state);
3090 IDXGIOutput_Release(output);
3091 return hr;
3094 swapchain->target = output;
3096 else
3098 IDXGIOutput_Release(output);
3101 if (swapchain_desc->BufferUsage && swapchain_desc->BufferUsage != DXGI_USAGE_RENDER_TARGET_OUTPUT)
3102 FIXME("Ignoring buffer usage %#x.\n", swapchain_desc->BufferUsage);
3103 if (swapchain_desc->Scaling != DXGI_SCALING_STRETCH && swapchain_desc->Scaling != DXGI_SCALING_NONE)
3104 FIXME("Ignoring scaling %#x.\n", swapchain_desc->Scaling);
3105 if (swapchain_desc->AlphaMode && swapchain_desc->AlphaMode != DXGI_ALPHA_MODE_IGNORE)
3106 FIXME("Ignoring alpha mode %#x.\n", swapchain_desc->AlphaMode);
3107 if (swapchain_desc->Flags & ~(DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
3108 FIXME("Ignoring swapchain flags %#x.\n", swapchain_desc->Flags);
3110 if (fullscreen_desc->RefreshRate.Numerator || fullscreen_desc->RefreshRate.Denominator)
3111 FIXME("Ignoring refresh rate.\n");
3112 if (fullscreen_desc->ScanlineOrdering)
3113 FIXME("Unhandled scanline ordering %#x.\n", fullscreen_desc->ScanlineOrdering);
3114 if (fullscreen_desc->Scaling)
3115 FIXME("Unhandled mode scaling %#x.\n", fullscreen_desc->Scaling);
3117 vk_instance = vkd3d_instance_get_vk_instance(vkd3d_instance_from_device(device));
3118 vk_physical_device = vkd3d_get_vk_physical_device(device);
3119 vk_device = vkd3d_get_vk_device(device);
3121 swapchain->vk_instance = vk_instance;
3122 swapchain->vk_device = vk_device;
3123 swapchain->vk_physical_device = vk_physical_device;
3125 if (!init_vk_funcs(&swapchain->vk_funcs, vk_instance, vk_device))
3127 wined3d_swapchain_state_destroy(swapchain->state);
3128 return E_FAIL;
3131 wined3d_private_store_init(&swapchain->private_store);
3133 surface_desc.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3134 surface_desc.pNext = NULL;
3135 surface_desc.flags = 0;
3136 surface_desc.hinstance = GetModuleHandleA("dxgi.dll");
3137 surface_desc.hwnd = window;
3138 if ((vr = vk_funcs->p_vkCreateWin32SurfaceKHR(vk_instance, &surface_desc, NULL, &vk_surface)) < 0)
3140 WARN("Failed to create Vulkan surface, vr %d.\n", vr);
3141 d3d12_swapchain_destroy(swapchain);
3142 return hresult_from_vk_result(vr);
3144 swapchain->vk_surface = vk_surface;
3146 queue_family_index = vkd3d_get_vk_queue_family_index(queue);
3147 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceSupportKHR(vk_physical_device,
3148 queue_family_index, vk_surface, &supported)) < 0 || !supported)
3150 FIXME("Queue family does not support presentation, vr %d.\n", vr);
3151 d3d12_swapchain_destroy(swapchain);
3152 return DXGI_ERROR_UNSUPPORTED;
3155 ID3D12CommandQueue_AddRef(swapchain->command_queue = queue);
3156 ID3D12Device_AddRef(swapchain->device = device);
3158 if (FAILED(hr = d3d12_swapchain_create_vulkan_swapchain(swapchain)))
3160 d3d12_swapchain_destroy(swapchain);
3161 return hr;
3164 fence_desc.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
3165 fence_desc.pNext = NULL;
3166 fence_desc.flags = 0;
3167 if ((vr = vk_funcs->p_vkCreateFence(vk_device, &fence_desc, NULL, &vk_fence)) < 0)
3169 WARN("Failed to create Vulkan fence, vr %d.\n", vr);
3170 d3d12_swapchain_destroy(swapchain);
3171 return hresult_from_vk_result(vr);
3173 swapchain->vk_fence = vk_fence;
3175 swapchain->current_buffer_index = 0;
3176 if ((vr = d3d12_swapchain_acquire_next_back_buffer(swapchain)) < 0)
3178 d3d12_swapchain_destroy(swapchain);
3179 return hresult_from_vk_result(vr);
3182 if (swapchain_desc->Flags & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT)
3184 swapchain->frame_number = DXGI_MAX_SWAP_CHAIN_BUFFERS;
3185 swapchain->frame_latency = 1;
3187 if (FAILED(hr = ID3D12Device_CreateFence(device, DXGI_MAX_SWAP_CHAIN_BUFFERS,
3188 0, &IID_ID3D12Fence, (void **)&swapchain->frame_latency_fence)))
3190 WARN("Failed to create frame latency fence, hr %#x.\n", hr);
3191 d3d12_swapchain_destroy(swapchain);
3192 return hr;
3195 if (!(swapchain->frame_latency_event = CreateEventW(NULL, FALSE, TRUE, NULL)))
3197 hr = HRESULT_FROM_WIN32(GetLastError());
3198 WARN("Failed to create frame latency event, hr %#x.\n", hr);
3199 d3d12_swapchain_destroy(swapchain);
3200 return hr;
3204 IWineDXGIFactory_AddRef(swapchain->factory = factory);
3206 return S_OK;
3209 HRESULT d3d12_swapchain_create(IWineDXGIFactory *factory, ID3D12CommandQueue *queue, HWND window,
3210 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc,
3211 IDXGISwapChain1 **swapchain)
3213 DXGI_SWAP_CHAIN_FULLSCREEN_DESC default_fullscreen_desc;
3214 struct d3d12_swapchain *object;
3215 ID3D12Device *device;
3216 HRESULT hr;
3218 if (swapchain_desc->Format == DXGI_FORMAT_UNKNOWN)
3219 return DXGI_ERROR_INVALID_CALL;
3221 if (!fullscreen_desc)
3223 memset(&default_fullscreen_desc, 0, sizeof(default_fullscreen_desc));
3224 default_fullscreen_desc.Windowed = TRUE;
3225 fullscreen_desc = &default_fullscreen_desc;
3228 if (!(object = heap_alloc_zero(sizeof(*object))))
3229 return E_OUTOFMEMORY;
3231 if (FAILED(hr = ID3D12CommandQueue_GetDevice(queue, &IID_ID3D12Device, (void **)&device)))
3233 ERR("Failed to get D3D12 device, hr %#x.\n", hr);
3234 heap_free(object);
3235 return hr;
3238 hr = d3d12_swapchain_init(object, factory, device, queue, window, swapchain_desc, fullscreen_desc);
3239 ID3D12Device_Release(device);
3240 if (FAILED(hr))
3242 heap_free(object);
3243 return hr;
3246 TRACE("Created swapchain %p.\n", object);
3248 *swapchain = (IDXGISwapChain1 *)&object->IDXGISwapChain4_iface;
3250 return S_OK;
3253 #else
3255 HRESULT d3d12_swapchain_create(IWineDXGIFactory *factory, ID3D12CommandQueue *queue, HWND window,
3256 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc,
3257 IDXGISwapChain1 **swapchain)
3259 ERR_(winediag)("Wine was built without Direct3D 12 support.\n");
3260 return DXGI_ERROR_UNSUPPORTED;
3263 #endif /* SONAME_LIBVKD3D */