ntoskrnl.exe: Add KeQueryActiveProcessorCountEx() function.
[wine.git] / dlls / dxgi / swapchain.c
blobf37aee933ec641a27f678a87bffa9b5cbb08f0c5
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 HRESULT d3d11_swapchain_init(struct d3d11_swapchain *swapchain, struct dxgi_device *device,
815 struct wined3d_swapchain_desc *desc)
817 BOOL fullscreen;
818 HRESULT hr;
820 /* A reference to the implicit swapchain is held by the wined3d device. In
821 * order to avoid circular references we do not keep a reference to the
822 * device in the implicit swapchain. */
823 if (!(desc->flags & WINED3D_SWAPCHAIN_IMPLICIT))
825 if (desc->backbuffer_format == WINED3DFMT_UNKNOWN)
826 return E_INVALIDARG;
828 if (FAILED(hr = IWineDXGIAdapter_GetParent(device->adapter, &IID_IDXGIFactory,
829 (void **)&swapchain->factory)))
831 WARN("Failed to get adapter parent, hr %#x.\n", hr);
832 return hr;
834 IWineDXGIDevice_AddRef(swapchain->device = &device->IWineDXGIDevice_iface);
836 else
838 swapchain->device = NULL;
839 swapchain->factory = NULL;
842 swapchain->IDXGISwapChain1_iface.lpVtbl = &d3d11_swapchain_vtbl;
843 swapchain->refcount = 1;
844 wined3d_mutex_lock();
845 wined3d_private_store_init(&swapchain->private_store);
847 if (!desc->windowed && (!desc->backbuffer_width || !desc->backbuffer_height))
848 FIXME("Fullscreen swapchain with back buffer width/height equal to 0 not supported properly.\n");
850 fullscreen = !desc->windowed;
851 desc->windowed = TRUE;
852 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
853 &d3d11_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
855 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
856 goto cleanup;
859 swapchain->target = NULL;
860 if (fullscreen)
862 struct wined3d_swapchain_state *state;
864 desc->windowed = FALSE;
865 state = wined3d_swapchain_get_state(swapchain->wined3d_swapchain);
867 if (FAILED(hr = IDXGISwapChain1_GetContainingOutput(&swapchain->IDXGISwapChain1_iface,
868 &swapchain->target)))
870 WARN("Failed to get target output for fullscreen swapchain, hr %#x.\n", hr);
871 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
872 goto cleanup;
875 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(state, desc, NULL)))
877 WARN("Failed to set fullscreen state, hr %#x.\n", hr);
878 IDXGIOutput_Release(swapchain->target);
879 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
880 goto cleanup;
884 wined3d_mutex_unlock();
886 return S_OK;
888 cleanup:
889 wined3d_private_store_cleanup(&swapchain->private_store);
890 wined3d_mutex_unlock();
891 if (swapchain->factory)
892 IDXGIFactory_Release(swapchain->factory);
893 if (swapchain->device)
894 IWineDXGIDevice_Release(swapchain->device);
895 return hr;
898 #ifdef SONAME_LIBVKD3D
900 #ifdef USE_WIN32_VULKAN
902 static void *load_library(const char *name)
904 return LoadLibraryA(name);
907 static void *get_library_proc(void *handle, const char *name)
909 return (void *)GetProcAddress(handle, name);
912 static void close_library(void *handle)
914 if (handle)
915 FreeLibrary(handle);
918 static PFN_vkGetInstanceProcAddr load_vulkan(void **vulkan_handle)
920 *vulkan_handle = LoadLibraryA("vulkan-1.dll");
921 return (void *)GetProcAddress(*vulkan_handle, "vkGetInstanceProcAddr");
924 #else
926 static void *load_library(const char *name)
928 return dlopen(name, RTLD_NOW);
931 static void *get_library_proc(void *handle, const char *name)
933 return dlsym(handle, name);
936 static void close_library(void *handle)
938 if (handle)
939 dlclose(handle);
942 static PFN_vkGetInstanceProcAddr load_vulkan(void **vulkan_handle)
944 const struct vulkan_funcs *vk_funcs;
945 HDC hdc;
947 *vulkan_handle = NULL;
949 hdc = GetDC(0);
950 vk_funcs = __wine_get_vulkan_driver(hdc, WINE_VULKAN_DRIVER_VERSION);
951 ReleaseDC(0, hdc);
953 if (vk_funcs)
954 return (PFN_vkGetInstanceProcAddr)vk_funcs->p_vkGetInstanceProcAddr;
956 return NULL;
959 #endif /* USE_WIN32_VULKAN */
961 static PFN_vkd3d_acquire_vk_queue vkd3d_acquire_vk_queue;
962 static PFN_vkd3d_create_image_resource vkd3d_create_image_resource;
963 static PFN_vkd3d_get_device_parent vkd3d_get_device_parent;
964 static PFN_vkd3d_get_vk_device vkd3d_get_vk_device;
965 static PFN_vkd3d_get_vk_format vkd3d_get_vk_format;
966 static PFN_vkd3d_get_vk_physical_device vkd3d_get_vk_physical_device;
967 static PFN_vkd3d_get_vk_queue_family_index vkd3d_get_vk_queue_family_index;
968 static PFN_vkd3d_instance_from_device vkd3d_instance_from_device;
969 static PFN_vkd3d_instance_get_vk_instance vkd3d_instance_get_vk_instance;
970 static PFN_vkd3d_release_vk_queue vkd3d_release_vk_queue;
971 static PFN_vkd3d_resource_decref vkd3d_resource_decref;
972 static PFN_vkd3d_resource_incref vkd3d_resource_incref;
974 struct dxgi_vk_funcs
976 PFN_vkAcquireNextImageKHR p_vkAcquireNextImageKHR;
977 PFN_vkAllocateCommandBuffers p_vkAllocateCommandBuffers;
978 PFN_vkAllocateMemory p_vkAllocateMemory;
979 PFN_vkBeginCommandBuffer p_vkBeginCommandBuffer;
980 PFN_vkBindImageMemory p_vkBindImageMemory;
981 PFN_vkCmdBlitImage p_vkCmdBlitImage;
982 PFN_vkCmdPipelineBarrier p_vkCmdPipelineBarrier;
983 PFN_vkCreateCommandPool p_vkCreateCommandPool;
984 PFN_vkCreateFence p_vkCreateFence;
985 PFN_vkCreateImage p_vkCreateImage;
986 PFN_vkCreateSemaphore p_vkCreateSemaphore;
987 PFN_vkCreateSwapchainKHR p_vkCreateSwapchainKHR;
988 PFN_vkCreateWin32SurfaceKHR p_vkCreateWin32SurfaceKHR;
989 PFN_vkDestroyCommandPool p_vkDestroyCommandPool;
990 PFN_vkDestroyFence p_vkDestroyFence;
991 PFN_vkDestroyImage p_vkDestroyImage;
992 PFN_vkDestroySemaphore p_vkDestroySemaphore;
993 PFN_vkDestroySurfaceKHR p_vkDestroySurfaceKHR;
994 PFN_vkResetCommandBuffer p_vkResetCommandBuffer;
995 PFN_vkDestroySwapchainKHR p_vkDestroySwapchainKHR;
996 PFN_vkEndCommandBuffer p_vkEndCommandBuffer;
997 PFN_vkFreeMemory p_vkFreeMemory;
998 PFN_vkGetImageMemoryRequirements p_vkGetImageMemoryRequirements;
999 PFN_vkGetInstanceProcAddr p_vkGetInstanceProcAddr;
1000 PFN_vkGetPhysicalDeviceMemoryProperties p_vkGetPhysicalDeviceMemoryProperties;
1001 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR p_vkGetPhysicalDeviceSurfaceCapabilitiesKHR;
1002 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR p_vkGetPhysicalDeviceSurfaceFormatsKHR;
1003 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR p_vkGetPhysicalDeviceSurfacePresentModesKHR;
1004 PFN_vkGetPhysicalDeviceSurfaceSupportKHR p_vkGetPhysicalDeviceSurfaceSupportKHR;
1005 PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR p_vkGetPhysicalDeviceWin32PresentationSupportKHR;
1006 PFN_vkGetSwapchainImagesKHR p_vkGetSwapchainImagesKHR;
1007 PFN_vkQueuePresentKHR p_vkQueuePresentKHR;
1008 PFN_vkQueueSubmit p_vkQueueSubmit;
1009 PFN_vkQueueWaitIdle p_vkQueueWaitIdle;
1010 PFN_vkResetFences p_vkResetFences;
1011 PFN_vkWaitForFences p_vkWaitForFences;
1013 void *vulkan_module;
1016 static HRESULT hresult_from_vk_result(VkResult vr)
1018 switch (vr)
1020 case VK_SUCCESS:
1021 return S_OK;
1022 case VK_ERROR_OUT_OF_HOST_MEMORY:
1023 case VK_ERROR_OUT_OF_DEVICE_MEMORY:
1024 return E_OUTOFMEMORY;
1025 default:
1026 FIXME("Unhandled VkResult %d.\n", vr);
1027 return E_FAIL;
1031 #define INVALID_VK_IMAGE_INDEX (~(uint32_t)0)
1033 struct d3d12_swapchain
1035 IDXGISwapChain3 IDXGISwapChain3_iface;
1036 LONG refcount;
1037 struct wined3d_private_store private_store;
1039 struct wined3d_swapchain_state *state;
1041 VkSwapchainKHR vk_swapchain;
1042 VkSurfaceKHR vk_surface;
1043 VkFence vk_fence;
1044 VkInstance vk_instance;
1045 VkDevice vk_device;
1046 VkPhysicalDevice vk_physical_device;
1047 VkDeviceMemory vk_memory;
1048 VkCommandPool vk_cmd_pool;
1049 VkImage vk_images[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1050 VkImage vk_swapchain_images[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1051 VkCommandBuffer vk_cmd_buffers[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1052 VkSemaphore vk_semaphores[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1053 ID3D12Resource *buffers[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1054 unsigned int buffer_count;
1055 unsigned int vk_swapchain_width;
1056 unsigned int vk_swapchain_height;
1057 VkPresentModeKHR present_mode;
1059 uint32_t vk_image_index;
1060 unsigned int current_buffer_index;
1061 struct dxgi_vk_funcs vk_funcs;
1063 ID3D12CommandQueue *command_queue;
1064 ID3D12Device *device;
1065 IWineDXGIFactory *factory;
1067 HWND window;
1068 IDXGIOutput *target;
1069 DXGI_SWAP_CHAIN_DESC1 desc;
1070 DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreen_desc;
1072 ID3D12Fence *frame_latency_fence;
1073 HANDLE frame_latency_event;
1075 uint64_t frame_number;
1076 uint32_t frame_latency;
1079 static DXGI_FORMAT dxgi_format_from_vk_format(VkFormat vk_format)
1081 switch (vk_format)
1083 case VK_FORMAT_B8G8R8A8_UNORM: return DXGI_FORMAT_B8G8R8A8_UNORM;
1084 case VK_FORMAT_R8G8B8A8_UNORM: return DXGI_FORMAT_R8G8B8A8_UNORM;
1085 case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return DXGI_FORMAT_R10G10B10A2_UNORM;
1086 case VK_FORMAT_R16G16B16A16_SFLOAT: return DXGI_FORMAT_R16G16B16A16_FLOAT;
1087 default:
1088 WARN("Unhandled format %#x.\n", vk_format);
1089 return DXGI_FORMAT_UNKNOWN;
1093 static VkFormat get_swapchain_fallback_format(VkFormat vk_format)
1095 switch (vk_format)
1097 case VK_FORMAT_R8G8B8A8_UNORM:
1098 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1099 case VK_FORMAT_R16G16B16A16_SFLOAT:
1100 return VK_FORMAT_B8G8R8A8_UNORM;
1101 default:
1102 WARN("Unhandled format %#x.\n", vk_format);
1103 return VK_FORMAT_UNDEFINED;
1107 static HRESULT select_vk_format(const struct dxgi_vk_funcs *vk_funcs,
1108 VkPhysicalDevice vk_physical_device, VkSurfaceKHR vk_surface,
1109 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, VkFormat *vk_format)
1111 VkSurfaceFormatKHR *formats;
1112 uint32_t format_count;
1113 VkFormat format;
1114 unsigned int i;
1115 VkResult vr;
1117 *vk_format = VK_FORMAT_UNDEFINED;
1119 format = vkd3d_get_vk_format(swapchain_desc->Format);
1121 vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device, vk_surface, &format_count, NULL);
1122 if (vr < 0 || !format_count)
1124 WARN("Failed to get supported surface formats, vr %d.\n", vr);
1125 return DXGI_ERROR_INVALID_CALL;
1128 if (!(formats = heap_calloc(format_count, sizeof(*formats))))
1129 return E_OUTOFMEMORY;
1131 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical_device,
1132 vk_surface, &format_count, formats)) < 0)
1134 WARN("Failed to enumerate supported surface formats, vr %d.\n", vr);
1135 heap_free(formats);
1136 return hresult_from_vk_result(vr);
1139 for (i = 0; i < format_count; ++i)
1141 if (formats[i].format == format && formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
1142 break;
1144 if (i == format_count)
1146 /* Try to create a swapchain with format conversion. */
1147 format = get_swapchain_fallback_format(format);
1148 WARN("Failed to find Vulkan swapchain format for %s.\n", debug_dxgi_format(swapchain_desc->Format));
1149 for (i = 0; i < format_count; ++i)
1151 if (formats[i].format == format && formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
1153 format = formats[i].format;
1154 break;
1158 heap_free(formats);
1159 if (i == format_count)
1161 FIXME("Failed to find Vulkan swapchain format for %s.\n", debug_dxgi_format(swapchain_desc->Format));
1162 return DXGI_ERROR_UNSUPPORTED;
1165 TRACE("Using Vulkan swapchain format %#x.\n", format);
1167 *vk_format = format;
1168 return S_OK;
1171 static HRESULT vk_select_memory_type(const struct dxgi_vk_funcs *vk_funcs,
1172 VkPhysicalDevice vk_physical_device, uint32_t memory_type_mask,
1173 VkMemoryPropertyFlags flags, uint32_t *memory_type_index)
1175 VkPhysicalDeviceMemoryProperties memory_properties;
1176 unsigned int i;
1178 vk_funcs->p_vkGetPhysicalDeviceMemoryProperties(vk_physical_device, &memory_properties);
1179 for (i = 0; i < memory_properties.memoryTypeCount; ++i)
1181 if (!(memory_type_mask & (1u << i)))
1182 continue;
1184 if ((memory_properties.memoryTypes[i].propertyFlags & flags) == flags)
1186 *memory_type_index = i;
1187 return S_OK;
1191 FIXME("Failed to find memory type (allowed types %#x).\n", memory_type_mask);
1192 return E_FAIL;
1195 static BOOL d3d12_swapchain_is_present_mode_supported(struct d3d12_swapchain *swapchain,
1196 VkPresentModeKHR present_mode)
1198 VkPhysicalDevice vk_physical_device = swapchain->vk_physical_device;
1199 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1200 VkPresentModeKHR *modes;
1201 uint32_t count, i;
1202 BOOL supported;
1203 VkResult vr;
1205 if (present_mode == VK_PRESENT_MODE_FIFO_KHR)
1206 return TRUE;
1208 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
1209 swapchain->vk_surface, &count, NULL)) < 0)
1211 WARN("Failed to get count of available present modes, vr %d.\n", vr);
1212 return FALSE;
1215 supported = FALSE;
1217 if (!(modes = heap_calloc(count, sizeof(*modes))))
1218 return FALSE;
1219 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical_device,
1220 swapchain->vk_surface, &count, modes)) >= 0)
1222 for (i = 0; i < count; ++i)
1224 if (modes[i] == present_mode)
1226 supported = TRUE;
1227 break;
1231 else
1233 WARN("Failed to get available present modes, vr %d.\n", vr);
1235 heap_free(modes);
1237 return supported;
1240 static BOOL d3d12_swapchain_has_user_images(struct d3d12_swapchain *swapchain)
1242 return !!swapchain->vk_images[0];
1245 static HRESULT d3d12_swapchain_create_user_buffers(struct d3d12_swapchain *swapchain, VkFormat vk_format)
1247 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1248 VkDeviceSize image_offset[DXGI_MAX_SWAP_CHAIN_BUFFERS];
1249 VkDevice vk_device = swapchain->vk_device;
1250 VkMemoryAllocateInfo allocate_info;
1251 VkMemoryRequirements requirements;
1252 VkImageCreateInfo image_info;
1253 uint32_t memory_type_mask;
1254 VkDeviceSize memory_size;
1255 unsigned int i;
1256 VkResult vr;
1257 HRESULT hr;
1259 if (d3d12_swapchain_has_user_images(swapchain))
1260 return S_OK;
1262 memset(&image_info, 0, sizeof(image_info));
1263 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1264 image_info.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
1265 image_info.imageType = VK_IMAGE_TYPE_2D;
1266 image_info.format = vk_format;
1267 image_info.extent.width = swapchain->desc.Width;
1268 image_info.extent.height = swapchain->desc.Height;
1269 image_info.extent.depth = 1;
1270 image_info.mipLevels = 1;
1271 image_info.arrayLayers = 1;
1272 image_info.samples = VK_SAMPLE_COUNT_1_BIT;
1273 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
1274 image_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
1275 | VK_IMAGE_USAGE_TRANSFER_DST_BIT
1276 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1277 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1278 image_info.queueFamilyIndexCount = 0;
1279 image_info.pQueueFamilyIndices = NULL;
1280 image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1282 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1284 assert(swapchain->vk_images[i] == VK_NULL_HANDLE);
1285 if ((vr = vk_funcs->p_vkCreateImage(vk_device, &image_info, NULL, &swapchain->vk_images[i])) < 0)
1287 WARN("Failed to create Vulkan image, vr %d.\n", vr);
1288 swapchain->vk_images[i] = VK_NULL_HANDLE;
1289 return hresult_from_vk_result(vr);
1293 memory_size = 0;
1294 memory_type_mask = ~0u;
1295 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1297 vk_funcs->p_vkGetImageMemoryRequirements(vk_device, swapchain->vk_images[i], &requirements);
1299 TRACE("Size %s, alignment %s, memory types %#x.\n",
1300 wine_dbgstr_longlong(requirements.size), wine_dbgstr_longlong(requirements.alignment),
1301 requirements.memoryTypeBits);
1303 image_offset[i] = (memory_size + (requirements.alignment - 1)) & ~(requirements.alignment - 1);
1304 memory_size = image_offset[i] + requirements.size;
1306 memory_type_mask &= requirements.memoryTypeBits;
1309 TRACE("Allocating %s bytes for user images.\n", wine_dbgstr_longlong(memory_size));
1311 allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1312 allocate_info.pNext = NULL;
1313 allocate_info.allocationSize = memory_size;
1315 if (FAILED(hr = vk_select_memory_type(vk_funcs, swapchain->vk_physical_device,
1316 memory_type_mask, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &allocate_info.memoryTypeIndex)))
1317 return hr;
1319 assert(swapchain->vk_memory == VK_NULL_HANDLE);
1320 if ((vr = vk_funcs->p_vkAllocateMemory(vk_device, &allocate_info, NULL, &swapchain->vk_memory)) < 0)
1322 WARN("Failed to allocate device memory, vr %d.\n", vr);
1323 swapchain->vk_memory = VK_NULL_HANDLE;
1324 return hresult_from_vk_result(vr);
1327 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1329 if ((vr = vk_funcs->p_vkBindImageMemory(vk_device, swapchain->vk_images[i],
1330 swapchain->vk_memory, image_offset[i])) < 0)
1332 WARN("Failed to bind image memory, vr %d.\n", vr);
1333 return hresult_from_vk_result(vr);
1337 return S_OK;
1340 static void vk_cmd_image_barrier(const struct dxgi_vk_funcs *vk_funcs, VkCommandBuffer cmd_buffer,
1341 VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
1342 VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask,
1343 VkImageLayout old_layout, VkImageLayout new_layout, VkImage image)
1345 VkImageMemoryBarrier barrier;
1347 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1348 barrier.pNext = NULL;
1349 barrier.srcAccessMask = src_access_mask;
1350 barrier.dstAccessMask = dst_access_mask;
1351 barrier.oldLayout = old_layout;
1352 barrier.newLayout = new_layout;
1353 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1354 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1355 barrier.image = image;
1356 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1357 barrier.subresourceRange.baseMipLevel = 0;
1358 barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
1359 barrier.subresourceRange.baseArrayLayer = 0;
1360 barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
1362 vk_funcs->p_vkCmdPipelineBarrier(cmd_buffer,
1363 src_stage_mask, dst_stage_mask, 0, 0, NULL, 0, NULL, 1, &barrier);
1366 static VkResult d3d12_swapchain_record_swapchain_blit(struct d3d12_swapchain *swapchain,
1367 VkCommandBuffer vk_cmd_buffer, VkImage vk_dst_image, VkImage vk_src_image)
1369 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1370 VkCommandBufferBeginInfo begin_info;
1371 VkImageBlit blit;
1372 VkFilter filter;
1373 VkResult vr;
1375 if (swapchain->desc.Width != swapchain->vk_swapchain_width
1376 || swapchain->desc.Height != swapchain->vk_swapchain_height)
1377 filter = VK_FILTER_LINEAR;
1378 else
1379 filter = VK_FILTER_NEAREST;
1381 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1382 begin_info.pNext = NULL;
1383 begin_info.flags = 0;
1384 begin_info.pInheritanceInfo = NULL;
1386 if ((vr = vk_funcs->p_vkBeginCommandBuffer(vk_cmd_buffer, &begin_info)) < 0)
1388 WARN("Failed to begin command buffer, vr %d.\n", vr);
1389 return vr;
1392 vk_cmd_image_barrier(vk_funcs, vk_cmd_buffer,
1393 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1394 0, VK_ACCESS_TRANSFER_WRITE_BIT,
1395 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_dst_image);
1397 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1398 blit.srcSubresource.mipLevel = 0;
1399 blit.srcSubresource.baseArrayLayer = 0;
1400 blit.srcSubresource.layerCount = 1;
1401 blit.srcOffsets[0].x = 0;
1402 blit.srcOffsets[0].y = 0;
1403 blit.srcOffsets[0].z = 0;
1404 blit.srcOffsets[1].x = swapchain->desc.Width;
1405 blit.srcOffsets[1].y = swapchain->desc.Height;
1406 blit.srcOffsets[1].z = 1;
1407 blit.dstSubresource = blit.srcSubresource;
1408 blit.dstOffsets[0].x = 0;
1409 blit.dstOffsets[0].y = 0;
1410 blit.dstOffsets[0].z = 0;
1411 if (swapchain->desc.Scaling == DXGI_SCALING_NONE)
1413 blit.srcOffsets[1].x = min(swapchain->vk_swapchain_width, blit.srcOffsets[1].x);
1414 blit.srcOffsets[1].y = min(swapchain->vk_swapchain_height, blit.srcOffsets[1].y);
1415 blit.dstOffsets[1].x = blit.srcOffsets[1].x;
1416 blit.dstOffsets[1].y = blit.srcOffsets[1].y;
1418 else
1420 /* FIXME: handle DXGI_SCALING_ASPECT_RATIO_STRETCH. */
1421 blit.dstOffsets[1].x = swapchain->vk_swapchain_width;
1422 blit.dstOffsets[1].y = swapchain->vk_swapchain_height;
1424 blit.dstOffsets[1].z = 1;
1426 vk_funcs->p_vkCmdBlitImage(vk_cmd_buffer,
1427 vk_src_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1428 vk_dst_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1429 1, &blit, filter);
1431 vk_cmd_image_barrier(vk_funcs, vk_cmd_buffer,
1432 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1433 VK_ACCESS_TRANSFER_WRITE_BIT, 0,
1434 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, vk_dst_image);
1436 if ((vr = vk_funcs->p_vkEndCommandBuffer(vk_cmd_buffer)) < 0)
1437 WARN("Failed to end command buffer, vr %d.\n", vr);
1439 return vr;
1442 static HRESULT d3d12_swapchain_prepare_command_buffers(struct d3d12_swapchain *swapchain,
1443 uint32_t queue_family_index)
1445 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1446 VkDevice vk_device = swapchain->vk_device;
1447 VkCommandBufferAllocateInfo allocate_info;
1448 VkSemaphoreCreateInfo semaphore_info;
1449 VkCommandPoolCreateInfo pool_info;
1450 unsigned int i;
1451 VkResult vr;
1453 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1454 pool_info.pNext = NULL;
1455 pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
1456 pool_info.queueFamilyIndex = queue_family_index;
1458 assert(swapchain->vk_cmd_pool == VK_NULL_HANDLE);
1459 if ((vr = vk_funcs->p_vkCreateCommandPool(vk_device, &pool_info,
1460 NULL, &swapchain->vk_cmd_pool)) < 0)
1462 WARN("Failed to create command pool, vr %d.\n", vr);
1463 swapchain->vk_cmd_pool = VK_NULL_HANDLE;
1464 return hresult_from_vk_result(vr);
1467 allocate_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1468 allocate_info.pNext = NULL;
1469 allocate_info.commandPool = swapchain->vk_cmd_pool;
1470 allocate_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1471 allocate_info.commandBufferCount = swapchain->buffer_count;
1473 if ((vr = vk_funcs->p_vkAllocateCommandBuffers(vk_device, &allocate_info,
1474 swapchain->vk_cmd_buffers)) < 0)
1476 WARN("Failed to allocate command buffers, vr %d.\n", vr);
1477 return hresult_from_vk_result(vr);
1480 for (i = 0; i < swapchain->buffer_count; ++i)
1482 semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1483 semaphore_info.pNext = NULL;
1484 semaphore_info.flags = 0;
1486 assert(swapchain->vk_semaphores[i] == VK_NULL_HANDLE);
1487 if ((vr = vk_funcs->p_vkCreateSemaphore(vk_device, &semaphore_info,
1488 NULL, &swapchain->vk_semaphores[i])) < 0)
1490 WARN("Failed to create semaphore, vr %d.\n", vr);
1491 swapchain->vk_semaphores[i] = VK_NULL_HANDLE;
1492 return hresult_from_vk_result(vr);
1496 return S_OK;
1499 static HRESULT d3d12_swapchain_create_buffers(struct d3d12_swapchain *swapchain,
1500 VkFormat vk_swapchain_format, VkFormat vk_format)
1502 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1503 struct vkd3d_image_resource_create_info resource_info;
1504 VkSwapchainKHR vk_swapchain = swapchain->vk_swapchain;
1505 ID3D12CommandQueue *queue = swapchain->command_queue;
1506 VkDevice vk_device = swapchain->vk_device;
1507 ID3D12Device *device = swapchain->device;
1508 uint32_t image_count, queue_family_index;
1509 D3D12_COMMAND_QUEUE_DESC queue_desc;
1510 unsigned int i;
1511 VkResult vr;
1512 HRESULT hr;
1514 if ((vr = vk_funcs->p_vkGetSwapchainImagesKHR(vk_device, vk_swapchain, &image_count, NULL)) < 0)
1516 WARN("Failed to get Vulkan swapchain images, vr %d.\n", vr);
1517 return hresult_from_vk_result(vr);
1519 if (image_count > ARRAY_SIZE(swapchain->vk_swapchain_images))
1521 FIXME("Unsupported Vulkan swapchain image count %u.\n", image_count);
1522 return E_FAIL;
1524 swapchain->buffer_count = image_count;
1525 if ((vr = vk_funcs->p_vkGetSwapchainImagesKHR(vk_device, vk_swapchain,
1526 &image_count, swapchain->vk_swapchain_images)) < 0)
1528 WARN("Failed to get Vulkan swapchain images, vr %d.\n", vr);
1529 return hresult_from_vk_result(vr);
1532 resource_info.type = VKD3D_STRUCTURE_TYPE_IMAGE_RESOURCE_CREATE_INFO;
1533 resource_info.next = NULL;
1534 resource_info.desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
1535 resource_info.desc.Alignment = 0;
1536 resource_info.desc.Width = swapchain->desc.Width;
1537 resource_info.desc.Height = swapchain->desc.Height;
1538 resource_info.desc.DepthOrArraySize = 1;
1539 resource_info.desc.MipLevels = 1;
1540 resource_info.desc.Format = dxgi_format_from_vk_format(vk_format);
1541 resource_info.desc.SampleDesc.Count = 1;
1542 resource_info.desc.SampleDesc.Quality = 0;
1543 resource_info.desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
1544 resource_info.desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
1545 resource_info.flags = VKD3D_RESOURCE_INITIAL_STATE_TRANSITION | VKD3D_RESOURCE_PRESENT_STATE_TRANSITION;
1547 queue_desc = ID3D12CommandQueue_GetDesc(queue);
1548 if (queue_desc.Type != D3D12_COMMAND_LIST_TYPE_DIRECT)
1550 /* vkCmdBlitImage() is only supported for graphics queues. */
1551 FIXME("Swapchain blit not implemented for command queue type %#x.\n", queue_desc.Type);
1552 if (vk_swapchain_format != vk_format)
1553 return E_NOTIMPL;
1554 if (image_count != swapchain->desc.BufferCount)
1556 FIXME("Got %u swapchain images, expected %u.\n", image_count, swapchain->desc.BufferCount);
1557 return E_NOTIMPL;
1560 queue_family_index = vkd3d_get_vk_queue_family_index(queue);
1562 if (queue_desc.Type == D3D12_COMMAND_LIST_TYPE_DIRECT)
1564 TRACE("Creating user swapchain buffers.\n");
1566 if (FAILED(hr = d3d12_swapchain_create_user_buffers(swapchain, vk_format)))
1567 return hr;
1569 if (FAILED(hr = d3d12_swapchain_prepare_command_buffers(swapchain, queue_family_index)))
1570 return hr;
1573 if (swapchain->buffers[0])
1574 return S_OK;
1576 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1578 if (swapchain->vk_images[i])
1580 resource_info.vk_image = swapchain->vk_images[i];
1581 resource_info.present_state = D3D12_RESOURCE_STATE_COPY_SOURCE;
1583 else
1585 resource_info.vk_image = swapchain->vk_swapchain_images[i];
1586 resource_info.present_state = D3D12_RESOURCE_STATE_PRESENT;
1589 if (FAILED(hr = vkd3d_create_image_resource(device, &resource_info, &swapchain->buffers[i])))
1591 WARN("Failed to create vkd3d resource for Vulkan image %u, hr %#x.\n", i, hr);
1592 return hr;
1595 vkd3d_resource_incref(swapchain->buffers[i]);
1596 ID3D12Resource_Release(swapchain->buffers[i]);
1599 return S_OK;
1602 static VkResult d3d12_swapchain_acquire_next_vulkan_image(struct d3d12_swapchain *swapchain)
1604 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1605 VkDevice vk_device = swapchain->vk_device;
1606 VkFence vk_fence = swapchain->vk_fence;
1607 VkResult vr;
1609 swapchain->vk_image_index = INVALID_VK_IMAGE_INDEX;
1611 if ((vr = vk_funcs->p_vkAcquireNextImageKHR(vk_device, swapchain->vk_swapchain, UINT64_MAX,
1612 VK_NULL_HANDLE, vk_fence, &swapchain->vk_image_index)) < 0)
1614 WARN("Failed to acquire next Vulkan image, vr %d.\n", vr);
1615 return vr;
1618 if ((vr = vk_funcs->p_vkWaitForFences(vk_device, 1, &vk_fence, VK_TRUE, UINT64_MAX)) != VK_SUCCESS)
1620 ERR("Failed to wait for fence, vr %d.\n", vr);
1621 return vr;
1623 if ((vr = vk_funcs->p_vkResetFences(vk_device, 1, &vk_fence)) < 0)
1624 ERR("Failed to reset fence, vr %d.\n", vr);
1626 return vr;
1629 static VkResult d3d12_swapchain_acquire_next_back_buffer(struct d3d12_swapchain *swapchain)
1631 VkResult vr;
1633 /* If we don't have user images, we need to acquire a Vulkan image in order
1634 * to get the correct value for the current back buffer index. */
1635 if (d3d12_swapchain_has_user_images(swapchain))
1636 return VK_SUCCESS;
1638 if ((vr = d3d12_swapchain_acquire_next_vulkan_image(swapchain)) >= 0)
1639 swapchain->current_buffer_index = swapchain->vk_image_index;
1640 else
1641 ERR("Failed to acquire Vulkan image, vr %d.\n", vr);
1643 return vr;
1646 static void d3d12_swapchain_destroy_buffers(struct d3d12_swapchain *swapchain, BOOL destroy_user_buffers)
1648 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1649 VkQueue vk_queue;
1650 unsigned int i;
1652 if (swapchain->command_queue)
1654 if ((vk_queue = vkd3d_acquire_vk_queue(swapchain->command_queue)))
1656 vk_funcs->p_vkQueueWaitIdle(vk_queue);
1658 vkd3d_release_vk_queue(swapchain->command_queue);
1660 else
1662 WARN("Failed to acquire Vulkan queue.\n");
1666 for (i = 0; i < swapchain->desc.BufferCount; ++i)
1668 if (swapchain->buffers[i] && (destroy_user_buffers || !d3d12_swapchain_has_user_images(swapchain)))
1670 vkd3d_resource_decref(swapchain->buffers[i]);
1671 swapchain->buffers[i] = NULL;
1673 if (swapchain->vk_device && destroy_user_buffers)
1675 vk_funcs->p_vkDestroyImage(swapchain->vk_device, swapchain->vk_images[i], NULL);
1676 swapchain->vk_images[i] = VK_NULL_HANDLE;
1680 if (swapchain->vk_device)
1682 for (i = 0; i < swapchain->buffer_count; ++i)
1684 vk_funcs->p_vkDestroySemaphore(swapchain->vk_device, swapchain->vk_semaphores[i], NULL);
1685 swapchain->vk_semaphores[i] = VK_NULL_HANDLE;
1687 if (destroy_user_buffers)
1689 vk_funcs->p_vkFreeMemory(swapchain->vk_device, swapchain->vk_memory, NULL);
1690 swapchain->vk_memory = VK_NULL_HANDLE;
1692 vk_funcs->p_vkDestroyCommandPool(swapchain->vk_device, swapchain->vk_cmd_pool, NULL);
1693 swapchain->vk_cmd_pool = VK_NULL_HANDLE;
1697 static HRESULT d3d12_swapchain_create_vulkan_swapchain(struct d3d12_swapchain *swapchain)
1699 VkPhysicalDevice vk_physical_device = swapchain->vk_physical_device;
1700 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1701 VkSwapchainCreateInfoKHR vk_swapchain_desc;
1702 VkDevice vk_device = swapchain->vk_device;
1703 VkFormat vk_format, vk_swapchain_format;
1704 unsigned int width, height, image_count;
1705 VkSurfaceCapabilitiesKHR surface_caps;
1706 VkSwapchainKHR vk_swapchain;
1707 VkImageUsageFlags usage;
1708 VkResult vr;
1709 HRESULT hr;
1711 if (!(vk_format = vkd3d_get_vk_format(swapchain->desc.Format)))
1713 WARN("Invalid format %#x.\n", swapchain->desc.Format);
1714 return DXGI_ERROR_INVALID_CALL;
1717 if (FAILED(hr = select_vk_format(vk_funcs, vk_physical_device,
1718 swapchain->vk_surface, &swapchain->desc, &vk_swapchain_format)))
1719 return hr;
1721 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical_device,
1722 swapchain->vk_surface, &surface_caps)) < 0)
1724 WARN("Failed to get surface capabilities, vr %d.\n", vr);
1725 return hresult_from_vk_result(vr);
1728 image_count = swapchain->desc.BufferCount;
1729 image_count = max(image_count, surface_caps.minImageCount);
1730 if (surface_caps.maxImageCount)
1731 image_count = min(image_count, surface_caps.maxImageCount);
1733 if (image_count != swapchain->desc.BufferCount)
1735 WARN("Buffer count %u is not supported (%u-%u).\n", swapchain->desc.BufferCount,
1736 surface_caps.minImageCount, surface_caps.maxImageCount);
1739 width = swapchain->desc.Width;
1740 height = swapchain->desc.Height;
1741 width = max(width, surface_caps.minImageExtent.width);
1742 width = min(width, surface_caps.maxImageExtent.width);
1743 height = max(height, surface_caps.minImageExtent.height);
1744 height = min(height, surface_caps.maxImageExtent.height);
1746 if (width != swapchain->desc.Width || height != swapchain->desc.Height)
1748 WARN("Swapchain dimensions %ux%u are not supported (%u-%u x %u-%u).\n",
1749 swapchain->desc.Width, swapchain->desc.Height,
1750 surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width,
1751 surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height);
1754 TRACE("Vulkan swapchain extent %ux%u.\n", width, height);
1756 if (!(surface_caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR))
1758 FIXME("Unsupported alpha mode.\n");
1759 return DXGI_ERROR_UNSUPPORTED;
1762 usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1763 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1764 usage |= surface_caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1765 if (!(usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) || !(usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT))
1766 WARN("Transfer not supported for swapchain images.\n");
1768 vk_swapchain_desc.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
1769 vk_swapchain_desc.pNext = NULL;
1770 vk_swapchain_desc.flags = 0;
1771 vk_swapchain_desc.surface = swapchain->vk_surface;
1772 vk_swapchain_desc.minImageCount = image_count;
1773 vk_swapchain_desc.imageFormat = vk_swapchain_format;
1774 vk_swapchain_desc.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
1775 vk_swapchain_desc.imageExtent.width = width;
1776 vk_swapchain_desc.imageExtent.height = height;
1777 vk_swapchain_desc.imageArrayLayers = 1;
1778 vk_swapchain_desc.imageUsage = usage;
1779 vk_swapchain_desc.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
1780 vk_swapchain_desc.queueFamilyIndexCount = 0;
1781 vk_swapchain_desc.pQueueFamilyIndices = NULL;
1782 vk_swapchain_desc.preTransform = surface_caps.currentTransform;
1783 vk_swapchain_desc.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1784 vk_swapchain_desc.presentMode = swapchain->present_mode;
1785 vk_swapchain_desc.clipped = VK_TRUE;
1786 vk_swapchain_desc.oldSwapchain = swapchain->vk_swapchain;
1787 if ((vr = vk_funcs->p_vkCreateSwapchainKHR(vk_device, &vk_swapchain_desc, NULL, &vk_swapchain)) < 0)
1789 WARN("Failed to create Vulkan swapchain, vr %d.\n", vr);
1790 return hresult_from_vk_result(vr);
1793 if (swapchain->vk_swapchain)
1794 vk_funcs->p_vkDestroySwapchainKHR(swapchain->vk_device, swapchain->vk_swapchain, NULL);
1796 swapchain->vk_swapchain = vk_swapchain;
1797 swapchain->vk_swapchain_width = width;
1798 swapchain->vk_swapchain_height = height;
1800 swapchain->vk_image_index = INVALID_VK_IMAGE_INDEX;
1802 return d3d12_swapchain_create_buffers(swapchain, vk_swapchain_format, vk_format);
1805 static HRESULT d3d12_swapchain_recreate_vulkan_swapchain(struct d3d12_swapchain *swapchain)
1807 VkResult vr;
1808 HRESULT hr;
1810 if (SUCCEEDED(hr = d3d12_swapchain_create_vulkan_swapchain(swapchain)))
1812 vr = d3d12_swapchain_acquire_next_back_buffer(swapchain);
1813 hr = hresult_from_vk_result(vr);
1815 else
1817 ERR("Failed to recreate Vulkan swapchain, hr %#x.\n", hr);
1820 return hr;
1823 static inline struct d3d12_swapchain *d3d12_swapchain_from_IDXGISwapChain3(IDXGISwapChain3 *iface)
1825 return CONTAINING_RECORD(iface, struct d3d12_swapchain, IDXGISwapChain3_iface);
1828 /* IUnknown methods */
1830 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_QueryInterface(IDXGISwapChain3 *iface, REFIID iid, void **object)
1832 TRACE("iface %p, iid %s, object %p.\n", iface, debugstr_guid(iid), object);
1834 if (IsEqualGUID(iid, &IID_IUnknown)
1835 || IsEqualGUID(iid, &IID_IDXGIObject)
1836 || IsEqualGUID(iid, &IID_IDXGIDeviceSubObject)
1837 || IsEqualGUID(iid, &IID_IDXGISwapChain)
1838 || IsEqualGUID(iid, &IID_IDXGISwapChain1)
1839 || IsEqualGUID(iid, &IID_IDXGISwapChain2)
1840 || IsEqualGUID(iid, &IID_IDXGISwapChain3))
1842 IUnknown_AddRef(iface);
1843 *object = iface;
1844 return S_OK;
1847 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
1849 *object = NULL;
1850 return E_NOINTERFACE;
1853 static ULONG STDMETHODCALLTYPE d3d12_swapchain_AddRef(IDXGISwapChain3 *iface)
1855 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1856 ULONG refcount = InterlockedIncrement(&swapchain->refcount);
1858 TRACE("%p increasing refcount to %u.\n", swapchain, refcount);
1860 return refcount;
1863 static void d3d12_swapchain_destroy(struct d3d12_swapchain *swapchain)
1865 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
1866 void *vulkan_module = vk_funcs->vulkan_module;
1868 d3d12_swapchain_destroy_buffers(swapchain, TRUE);
1870 if (swapchain->frame_latency_event)
1871 CloseHandle(swapchain->frame_latency_event);
1873 if (swapchain->frame_latency_fence)
1874 ID3D12Fence_Release(swapchain->frame_latency_fence);
1876 if (swapchain->command_queue)
1877 ID3D12CommandQueue_Release(swapchain->command_queue);
1879 wined3d_private_store_cleanup(&swapchain->private_store);
1881 if (swapchain->vk_device)
1883 vk_funcs->p_vkDestroyFence(swapchain->vk_device, swapchain->vk_fence, NULL);
1884 vk_funcs->p_vkDestroySwapchainKHR(swapchain->vk_device, swapchain->vk_swapchain, NULL);
1887 if (swapchain->vk_instance)
1888 vk_funcs->p_vkDestroySurfaceKHR(swapchain->vk_instance, swapchain->vk_surface, NULL);
1890 if (swapchain->target)
1892 WARN("Destroying fullscreen swapchain.\n");
1893 IDXGIOutput_Release(swapchain->target);
1896 if (swapchain->device)
1897 ID3D12Device_Release(swapchain->device);
1899 if (swapchain->factory)
1900 IWineDXGIFactory_Release(swapchain->factory);
1902 close_library(vulkan_module);
1904 wined3d_swapchain_state_destroy(swapchain->state);
1907 static ULONG STDMETHODCALLTYPE d3d12_swapchain_Release(IDXGISwapChain3 *iface)
1909 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1910 ULONG refcount = InterlockedDecrement(&swapchain->refcount);
1912 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
1914 if (!refcount)
1916 d3d12_swapchain_destroy(swapchain);
1917 heap_free(swapchain);
1920 return refcount;
1923 /* IDXGIObject methods */
1925 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetPrivateData(IDXGISwapChain3 *iface,
1926 REFGUID guid, UINT data_size, const void *data)
1928 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1930 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1932 return dxgi_set_private_data(&swapchain->private_store, guid, data_size, data);
1935 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetPrivateDataInterface(IDXGISwapChain3 *iface,
1936 REFGUID guid, const IUnknown *object)
1938 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1940 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
1942 return dxgi_set_private_data_interface(&swapchain->private_store, guid, object);
1945 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetPrivateData(IDXGISwapChain3 *iface,
1946 REFGUID guid, UINT *data_size, void *data)
1948 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1950 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1952 return dxgi_get_private_data(&swapchain->private_store, guid, data_size, data);
1955 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetParent(IDXGISwapChain3 *iface, REFIID iid, void **parent)
1957 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1959 TRACE("iface %p, iid %s, parent %p.\n", iface, debugstr_guid(iid), parent);
1961 return IWineDXGIFactory_QueryInterface(swapchain->factory, iid, parent);
1964 /* IDXGIDeviceSubObject methods */
1966 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetDevice(IDXGISwapChain3 *iface, REFIID iid, void **device)
1968 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
1970 TRACE("iface %p, iid %s, device %p.\n", iface, debugstr_guid(iid), device);
1972 return ID3D12Device_QueryInterface(swapchain->device, iid, device);
1975 /* IDXGISwapChain methods */
1977 static HRESULT d3d12_swapchain_set_sync_interval(struct d3d12_swapchain *swapchain,
1978 unsigned int sync_interval)
1980 VkPresentModeKHR present_mode;
1982 switch (sync_interval)
1984 case 0:
1985 present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
1986 break;
1987 default:
1988 FIXME("Unsupported sync interval %u.\n", sync_interval);
1989 case 1:
1990 present_mode = VK_PRESENT_MODE_FIFO_KHR;
1991 break;
1994 if (swapchain->present_mode == present_mode)
1995 return S_OK;
1997 if (!d3d12_swapchain_has_user_images(swapchain))
1999 FIXME("Cannot recreate swapchain without user images.\n");
2000 return S_OK;
2003 if (!d3d12_swapchain_is_present_mode_supported(swapchain, present_mode))
2005 FIXME("Vulkan present mode %#x is not supported.\n", present_mode);
2006 return S_OK;
2009 d3d12_swapchain_destroy_buffers(swapchain, FALSE);
2010 swapchain->present_mode = present_mode;
2011 return d3d12_swapchain_recreate_vulkan_swapchain(swapchain);
2014 static VkResult d3d12_swapchain_queue_present(struct d3d12_swapchain *swapchain, VkQueue vk_queue)
2016 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
2017 VkPresentInfoKHR present_info;
2018 VkSubmitInfo submit_info;
2019 VkResult vr;
2021 if (swapchain->vk_image_index == INVALID_VK_IMAGE_INDEX)
2023 if ((vr = d3d12_swapchain_acquire_next_vulkan_image(swapchain)) < 0)
2024 return vr;
2027 assert(swapchain->vk_image_index < swapchain->buffer_count);
2029 present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
2030 present_info.pNext = NULL;
2031 present_info.waitSemaphoreCount = 0;
2032 present_info.pWaitSemaphores = NULL;
2033 present_info.swapchainCount = 1;
2034 present_info.pSwapchains = &swapchain->vk_swapchain;
2035 present_info.pImageIndices = &swapchain->vk_image_index;
2036 present_info.pResults = NULL;
2038 if (d3d12_swapchain_has_user_images(swapchain))
2040 /* blit */
2041 VkCommandBuffer vk_cmd_buffer = swapchain->vk_cmd_buffers[swapchain->vk_image_index];
2042 VkImage vk_dst_image = swapchain->vk_swapchain_images[swapchain->vk_image_index];
2043 VkImage vk_src_image = swapchain->vk_images[swapchain->current_buffer_index];
2045 if ((vr = vk_funcs->p_vkResetCommandBuffer(vk_cmd_buffer, 0)) < 0)
2047 ERR("Failed to reset command buffer, vr %d.\n", vr);
2048 return vr;
2051 if ((vr = d3d12_swapchain_record_swapchain_blit(swapchain,
2052 vk_cmd_buffer, vk_dst_image, vk_src_image)) < 0 )
2053 return vr;
2055 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
2056 submit_info.pNext = NULL;
2057 submit_info.waitSemaphoreCount = 0;
2058 submit_info.pWaitSemaphores = NULL;
2059 submit_info.pWaitDstStageMask = NULL;
2060 submit_info.commandBufferCount = 1;
2061 submit_info.pCommandBuffers = &vk_cmd_buffer;
2062 submit_info.signalSemaphoreCount = 1;
2063 submit_info.pSignalSemaphores = &swapchain->vk_semaphores[swapchain->vk_image_index];
2065 if ((vr = vk_funcs->p_vkQueueSubmit(vk_queue, 1, &submit_info, VK_NULL_HANDLE)) < 0)
2067 ERR("Failed to blit swapchain buffer, vr %d.\n", vr);
2068 return vr;
2071 present_info.waitSemaphoreCount = 1;
2072 present_info.pWaitSemaphores = &swapchain->vk_semaphores[swapchain->vk_image_index];
2075 if ((vr = vk_funcs->p_vkQueuePresentKHR(vk_queue, &present_info)) >= 0)
2076 swapchain->vk_image_index = INVALID_VK_IMAGE_INDEX;
2078 return vr;
2081 static HRESULT d3d12_swapchain_present(struct d3d12_swapchain *swapchain,
2082 unsigned int sync_interval, unsigned int flags)
2084 HANDLE frame_latency_event;
2085 VkQueue vk_queue;
2086 VkResult vr;
2087 HRESULT hr;
2089 if (sync_interval > 4)
2091 WARN("Invalid sync interval %u.\n", sync_interval);
2092 return DXGI_ERROR_INVALID_CALL;
2095 if (flags & ~DXGI_PRESENT_TEST)
2096 FIXME("Unimplemented flags %#x.\n", flags);
2097 if (flags & DXGI_PRESENT_TEST)
2099 WARN("Returning S_OK for DXGI_PRESENT_TEST.\n");
2100 return S_OK;
2103 if (FAILED(hr = d3d12_swapchain_set_sync_interval(swapchain, sync_interval)))
2104 return hr;
2106 if (!(vk_queue = vkd3d_acquire_vk_queue(swapchain->command_queue)))
2108 ERR("Failed to acquire Vulkan queue.\n");
2109 return E_FAIL;
2112 vr = d3d12_swapchain_queue_present(swapchain, vk_queue);
2113 if (vr == VK_ERROR_OUT_OF_DATE_KHR)
2115 vkd3d_release_vk_queue(swapchain->command_queue);
2117 if (!d3d12_swapchain_has_user_images(swapchain))
2119 FIXME("Cannot recreate swapchain without user images.\n");
2120 return DXGI_STATUS_MODE_CHANGED;
2123 TRACE("Recreating Vulkan swapchain.\n");
2125 d3d12_swapchain_destroy_buffers(swapchain, FALSE);
2126 if (FAILED(hr = d3d12_swapchain_recreate_vulkan_swapchain(swapchain)))
2127 return hr;
2129 if (!(vk_queue = vkd3d_acquire_vk_queue(swapchain->command_queue)))
2131 ERR("Failed to acquire Vulkan queue.\n");
2132 return E_FAIL;
2135 if ((vr = d3d12_swapchain_queue_present(swapchain, vk_queue)) < 0)
2136 ERR("Failed to present after recreating swapchain, vr %d.\n", vr);
2139 vkd3d_release_vk_queue(swapchain->command_queue);
2141 if (vr < 0)
2143 ERR("Failed to queue present, vr %d.\n", vr);
2144 return hresult_from_vk_result(vr);
2147 if ((frame_latency_event = swapchain->frame_latency_event))
2149 ++swapchain->frame_number;
2151 if (FAILED(hr = ID3D12CommandQueue_Signal(swapchain->command_queue,
2152 swapchain->frame_latency_fence, swapchain->frame_number)))
2154 ERR("Failed to signal frame latency fence, hr %#x.\n", hr);
2155 return hr;
2158 if (FAILED(hr = ID3D12Fence_SetEventOnCompletion(swapchain->frame_latency_fence,
2159 swapchain->frame_number - swapchain->frame_latency, frame_latency_event)))
2161 ERR("Failed to enqueue frame latency event, hr %#x.\n", hr);
2162 return hr;
2166 swapchain->current_buffer_index = (swapchain->current_buffer_index + 1) % swapchain->desc.BufferCount;
2167 vr = d3d12_swapchain_acquire_next_back_buffer(swapchain);
2168 if (vr == VK_ERROR_OUT_OF_DATE_KHR)
2170 if (!d3d12_swapchain_has_user_images(swapchain))
2172 FIXME("Cannot recreate swapchain without user images.\n");
2173 return DXGI_STATUS_MODE_CHANGED;
2176 TRACE("Recreating Vulkan swapchain.\n");
2178 d3d12_swapchain_destroy_buffers(swapchain, FALSE);
2179 return d3d12_swapchain_recreate_vulkan_swapchain(swapchain);
2181 if (vr < 0)
2182 ERR("Failed to acquire next Vulkan image, vr %d.\n", vr);
2183 return hresult_from_vk_result(vr);
2186 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_Present(IDXGISwapChain3 *iface, UINT sync_interval, UINT flags)
2188 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2190 TRACE("iface %p, sync_interval %u, flags %#x.\n", iface, sync_interval, flags);
2192 return d3d12_swapchain_present(swapchain, sync_interval, flags);
2195 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetBuffer(IDXGISwapChain3 *iface,
2196 UINT buffer_idx, REFIID iid, void **surface)
2198 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2200 TRACE("iface %p, buffer_idx %u, iid %s, surface %p.\n",
2201 iface, buffer_idx, debugstr_guid(iid), surface);
2203 if (buffer_idx >= swapchain->desc.BufferCount)
2205 WARN("Invalid buffer index %u.\n", buffer_idx);
2206 return DXGI_ERROR_INVALID_CALL;
2209 assert(swapchain->buffers[buffer_idx]);
2210 return ID3D12Resource_QueryInterface(swapchain->buffers[buffer_idx], iid, surface);
2213 static HRESULT STDMETHODCALLTYPE DECLSPEC_HOTPATCH d3d12_swapchain_SetFullscreenState(IDXGISwapChain3 *iface,
2214 BOOL fullscreen, IDXGIOutput *target)
2216 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2217 DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc = &swapchain->fullscreen_desc;
2218 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc = &swapchain->desc;
2219 struct wined3d_swapchain_desc wined3d_desc;
2220 HWND window = swapchain->window;
2221 HRESULT hr;
2223 TRACE("iface %p, fullscreen %#x, target %p.\n", iface, fullscreen, target);
2225 if (!fullscreen && target)
2227 WARN("Invalid call.\n");
2228 return DXGI_ERROR_INVALID_CALL;
2231 if (target)
2233 IDXGIOutput_AddRef(target);
2235 else if (FAILED(hr = IDXGISwapChain3_GetContainingOutput(iface, &target)))
2237 WARN("Failed to get target output for swapchain, hr %#x.\n", hr);
2238 return hr;
2241 if (FAILED(hr = wined3d_swapchain_desc_from_dxgi(&wined3d_desc, target, window, swapchain_desc,
2242 fullscreen_desc)))
2243 goto fail;
2244 wined3d_mutex_lock();
2245 wined3d_desc.windowed = !fullscreen;
2246 hr = wined3d_swapchain_state_set_fullscreen(swapchain->state, &wined3d_desc, NULL);
2247 wined3d_mutex_unlock();
2248 if (FAILED(hr))
2249 goto fail;
2251 fullscreen_desc->Windowed = wined3d_desc.windowed;
2252 if (!fullscreen)
2254 IDXGIOutput_Release(target);
2255 target = NULL;
2258 if (swapchain->target)
2259 IDXGIOutput_Release(swapchain->target);
2260 swapchain->target = target;
2262 return S_OK;
2264 fail:
2265 IDXGIOutput_Release(target);
2267 return DXGI_ERROR_NOT_CURRENTLY_AVAILABLE;
2270 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetFullscreenState(IDXGISwapChain3 *iface,
2271 BOOL *fullscreen, IDXGIOutput **target)
2273 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2275 TRACE("iface %p, fullscreen %p, target %p.\n", iface, fullscreen, target);
2277 if (fullscreen)
2278 *fullscreen = !swapchain->fullscreen_desc.Windowed;
2280 if (target && (*target = swapchain->target))
2281 IDXGIOutput_AddRef(*target);
2283 return S_OK;
2286 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetDesc(IDXGISwapChain3 *iface, DXGI_SWAP_CHAIN_DESC *desc)
2288 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2289 const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc = &swapchain->fullscreen_desc;
2290 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc = &swapchain->desc;
2292 TRACE("iface %p, desc %p.\n", iface, desc);
2294 if (!desc)
2296 WARN("Invalid pointer.\n");
2297 return E_INVALIDARG;
2300 desc->BufferDesc.Width = swapchain_desc->Width;
2301 desc->BufferDesc.Height = swapchain_desc->Height;
2302 desc->BufferDesc.RefreshRate = fullscreen_desc->RefreshRate;
2303 desc->BufferDesc.Format = swapchain_desc->Format;
2304 desc->BufferDesc.ScanlineOrdering = fullscreen_desc->ScanlineOrdering;
2305 desc->BufferDesc.Scaling = fullscreen_desc->Scaling;
2306 desc->SampleDesc = swapchain_desc->SampleDesc;
2307 desc->BufferUsage = swapchain_desc->BufferUsage;
2308 desc->BufferCount = swapchain_desc->BufferCount;
2309 desc->OutputWindow = swapchain->window;
2310 desc->Windowed = fullscreen_desc->Windowed;
2311 desc->SwapEffect = swapchain_desc->SwapEffect;
2312 desc->Flags = swapchain_desc->Flags;
2314 return S_OK;
2317 static HRESULT d3d12_swapchain_resize_buffers(struct d3d12_swapchain *swapchain,
2318 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
2320 DXGI_SWAP_CHAIN_DESC1 *desc, new_desc;
2321 unsigned int i;
2322 ULONG refcount;
2324 if (flags)
2325 FIXME("Ignoring flags %#x.\n", flags);
2327 for (i = 0; i < swapchain->desc.BufferCount; ++i)
2329 ID3D12Resource_AddRef(swapchain->buffers[i]);
2330 if ((refcount = ID3D12Resource_Release(swapchain->buffers[i])))
2332 WARN("Buffer %p has %u references left.\n", swapchain->buffers[i], refcount);
2333 return DXGI_ERROR_INVALID_CALL;
2337 desc = &swapchain->desc;
2338 new_desc = swapchain->desc;
2340 if (buffer_count)
2341 new_desc.BufferCount = buffer_count;
2342 if (!width || !height)
2344 RECT client_rect;
2346 if (!GetClientRect(swapchain->window, &client_rect))
2348 WARN("Failed to get client rect, last error %#x.\n", GetLastError());
2349 return DXGI_ERROR_INVALID_CALL;
2352 if (!width)
2353 width = client_rect.right;
2354 if (!height)
2355 height = client_rect.bottom;
2357 new_desc.Width = width;
2358 new_desc.Height = height;
2360 if (format)
2361 new_desc.Format = format;
2363 if (!dxgi_validate_swapchain_desc(&new_desc))
2364 return DXGI_ERROR_INVALID_CALL;
2366 if (desc->Width == new_desc.Width && desc->Height == new_desc.Height
2367 && desc->Format == new_desc.Format && desc->BufferCount == new_desc.BufferCount)
2368 return S_OK;
2370 d3d12_swapchain_destroy_buffers(swapchain, TRUE);
2371 swapchain->desc = new_desc;
2372 return d3d12_swapchain_recreate_vulkan_swapchain(swapchain);
2375 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_ResizeBuffers(IDXGISwapChain3 *iface,
2376 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
2378 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2380 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x.\n",
2381 iface, buffer_count, width, height, debug_dxgi_format(format), flags);
2383 return d3d12_swapchain_resize_buffers(swapchain, buffer_count, width, height, format, flags);
2386 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_ResizeTarget(IDXGISwapChain3 *iface,
2387 const DXGI_MODE_DESC *target_mode_desc)
2389 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2391 TRACE("iface %p, target_mode_desc %p.\n", iface, target_mode_desc);
2393 return dxgi_swapchain_resize_target(swapchain->state, target_mode_desc);
2396 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetContainingOutput(IDXGISwapChain3 *iface,
2397 IDXGIOutput **output)
2399 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2400 IUnknown *device_parent;
2401 IDXGIFactory *factory;
2402 IDXGIAdapter *adapter;
2403 HRESULT hr;
2405 TRACE("iface %p, output %p.\n", iface, output);
2407 if (swapchain->target)
2409 IDXGIOutput_AddRef(*output = swapchain->target);
2410 return S_OK;
2413 device_parent = vkd3d_get_device_parent(swapchain->device);
2415 if (FAILED(hr = IUnknown_QueryInterface(device_parent, &IID_IDXGIAdapter, (void **)&adapter)))
2417 WARN("Failed to get adapter, hr %#x.\n", hr);
2418 return hr;
2421 if (FAILED(hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory)))
2423 WARN("Failed to get factory, hr %#x.\n", hr);
2424 IDXGIAdapter_Release(adapter);
2425 return hr;
2428 hr = dxgi_get_output_from_window(factory, swapchain->window, output);
2429 IDXGIFactory_Release(factory);
2430 IDXGIAdapter_Release(adapter);
2431 return hr;
2434 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetFrameStatistics(IDXGISwapChain3 *iface,
2435 DXGI_FRAME_STATISTICS *stats)
2437 FIXME("iface %p, stats %p stub!\n", iface, stats);
2439 return E_NOTIMPL;
2442 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetLastPresentCount(IDXGISwapChain3 *iface,
2443 UINT *last_present_count)
2445 FIXME("iface %p, last_present_count %p stub!\n", iface, last_present_count);
2447 return E_NOTIMPL;
2450 /* IDXGISwapChain1 methods */
2452 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetDesc1(IDXGISwapChain3 *iface, DXGI_SWAP_CHAIN_DESC1 *desc)
2454 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2456 TRACE("iface %p, desc %p.\n", iface, desc);
2458 if (!desc)
2460 WARN("Invalid pointer.\n");
2461 return E_INVALIDARG;
2464 *desc = swapchain->desc;
2465 return S_OK;
2468 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetFullscreenDesc(IDXGISwapChain3 *iface,
2469 DXGI_SWAP_CHAIN_FULLSCREEN_DESC *desc)
2471 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2473 TRACE("iface %p, desc %p.\n", iface, desc);
2475 if (!desc)
2477 WARN("Invalid pointer.\n");
2478 return E_INVALIDARG;
2481 *desc = swapchain->fullscreen_desc;
2482 return S_OK;
2485 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetHwnd(IDXGISwapChain3 *iface, HWND *hwnd)
2487 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2489 TRACE("iface %p, hwnd %p.\n", iface, hwnd);
2491 if (!hwnd)
2493 WARN("Invalid pointer.\n");
2494 return DXGI_ERROR_INVALID_CALL;
2497 *hwnd = swapchain->window;
2498 return S_OK;
2501 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetCoreWindow(IDXGISwapChain3 *iface,
2502 REFIID iid, void **core_window)
2504 FIXME("iface %p, iid %s, core_window %p stub!\n", iface, debugstr_guid(iid), core_window);
2506 if (core_window)
2507 *core_window = NULL;
2509 return DXGI_ERROR_INVALID_CALL;
2512 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_Present1(IDXGISwapChain3 *iface,
2513 UINT sync_interval, UINT flags, const DXGI_PRESENT_PARAMETERS *present_parameters)
2515 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2517 TRACE("iface %p, sync_interval %u, flags %#x, present_parameters %p.\n",
2518 iface, sync_interval, flags, present_parameters);
2520 if (present_parameters)
2521 FIXME("Ignored present parameters %p.\n", present_parameters);
2523 return d3d12_swapchain_present(swapchain, sync_interval, flags);
2526 static BOOL STDMETHODCALLTYPE d3d12_swapchain_IsTemporaryMonoSupported(IDXGISwapChain3 *iface)
2528 FIXME("iface %p stub!\n", iface);
2530 return FALSE;
2533 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetRestrictToOutput(IDXGISwapChain3 *iface, IDXGIOutput **output)
2535 FIXME("iface %p, output %p stub!\n", iface, output);
2537 if (!output)
2539 WARN("Invalid pointer.\n");
2540 return E_INVALIDARG;
2543 *output = NULL;
2544 return E_NOTIMPL;
2547 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetBackgroundColor(IDXGISwapChain3 *iface, const DXGI_RGBA *color)
2549 FIXME("iface %p, color %p stub!\n", iface, color);
2551 return E_NOTIMPL;
2554 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetBackgroundColor(IDXGISwapChain3 *iface, DXGI_RGBA *color)
2556 FIXME("iface %p, color %p stub!\n", iface, color);
2558 return E_NOTIMPL;
2561 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetRotation(IDXGISwapChain3 *iface, DXGI_MODE_ROTATION rotation)
2563 FIXME("iface %p, rotation %#x stub!\n", iface, rotation);
2565 return E_NOTIMPL;
2568 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetRotation(IDXGISwapChain3 *iface, DXGI_MODE_ROTATION *rotation)
2570 FIXME("iface %p, rotation %p stub!\n", iface, rotation);
2572 return E_NOTIMPL;
2575 /* IDXGISwapChain2 methods */
2577 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetSourceSize(IDXGISwapChain3 *iface, UINT width, UINT height)
2579 FIXME("iface %p, width %u, height %u stub!\n", iface, width, height);
2581 return E_NOTIMPL;
2584 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetSourceSize(IDXGISwapChain3 *iface, UINT *width, UINT *height)
2586 FIXME("iface %p, width %p, height %p stub!\n", iface, width, height);
2588 return E_NOTIMPL;
2591 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetMaximumFrameLatency(IDXGISwapChain3 *iface, UINT max_latency)
2593 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2595 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
2597 if (!(swapchain->desc.Flags & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
2599 WARN("DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT not set for swap chain %p.\n", iface);
2600 return DXGI_ERROR_INVALID_CALL;
2603 if (!max_latency)
2605 WARN("Invalid maximum frame latency %u.\n", max_latency);
2606 return DXGI_ERROR_INVALID_CALL;
2609 swapchain->frame_latency = max_latency;
2610 return S_OK;
2613 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetMaximumFrameLatency(IDXGISwapChain3 *iface, UINT *max_latency)
2615 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2617 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
2619 if (!(swapchain->desc.Flags & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
2621 WARN("DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT not set for swap chain %p.\n", iface);
2622 return DXGI_ERROR_INVALID_CALL;
2625 *max_latency = swapchain->frame_latency;
2626 return S_OK;
2629 static HANDLE STDMETHODCALLTYPE d3d12_swapchain_GetFrameLatencyWaitableObject(IDXGISwapChain3 *iface)
2631 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2633 TRACE("iface %p.\n", iface);
2635 return swapchain->frame_latency_event;
2638 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetMatrixTransform(IDXGISwapChain3 *iface,
2639 const DXGI_MATRIX_3X2_F *matrix)
2641 FIXME("iface %p, matrix %p stub!\n", iface, matrix);
2643 return E_NOTIMPL;
2646 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_GetMatrixTransform(IDXGISwapChain3 *iface,
2647 DXGI_MATRIX_3X2_F *matrix)
2649 FIXME("iface %p, matrix %p stub!\n", iface, matrix);
2651 return E_NOTIMPL;
2654 /* IDXGISwapChain3 methods */
2656 static UINT STDMETHODCALLTYPE d3d12_swapchain_GetCurrentBackBufferIndex(IDXGISwapChain3 *iface)
2658 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2660 TRACE("iface %p.\n", iface);
2662 TRACE("Current back buffer index %u.\n", swapchain->current_buffer_index);
2663 assert(swapchain->current_buffer_index < swapchain->desc.BufferCount);
2664 return swapchain->current_buffer_index;
2667 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_CheckColorSpaceSupport(IDXGISwapChain3 *iface,
2668 DXGI_COLOR_SPACE_TYPE colour_space, UINT *colour_space_support)
2670 UINT support_flags = 0;
2672 FIXME("iface %p, colour_space %#x, colour_space_support %p semi-stub!\n",
2673 iface, colour_space, colour_space_support);
2675 if (!colour_space_support)
2676 return E_INVALIDARG;
2678 if (colour_space == DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709)
2679 support_flags |= DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT;
2681 *colour_space_support = support_flags;
2682 return S_OK;
2685 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_SetColorSpace1(IDXGISwapChain3 *iface,
2686 DXGI_COLOR_SPACE_TYPE colour_space)
2688 FIXME("iface %p, colour_space %#x semi-stub!\n", iface, colour_space);
2690 if (colour_space != DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709)
2692 WARN("Colour space %u not supported.\n", colour_space);
2693 return E_INVALIDARG;
2696 return S_OK;
2699 static HRESULT STDMETHODCALLTYPE d3d12_swapchain_ResizeBuffers1(IDXGISwapChain3 *iface,
2700 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags,
2701 const UINT *node_mask, IUnknown * const *present_queue)
2703 struct d3d12_swapchain *swapchain = d3d12_swapchain_from_IDXGISwapChain3(iface);
2704 size_t i, count;
2706 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x, "
2707 "node_mask %p, present_queue %p.\n",
2708 iface, buffer_count, width, height, debug_dxgi_format(format), flags, node_mask, present_queue);
2710 if (!node_mask || !present_queue)
2711 return DXGI_ERROR_INVALID_CALL;
2713 count = buffer_count ? buffer_count : swapchain->desc.BufferCount;
2714 for (i = 0; i < count; ++i)
2716 if (node_mask[i] > 1 || !present_queue[i])
2717 return DXGI_ERROR_INVALID_CALL;
2718 if ((ID3D12CommandQueue*)present_queue[i] != swapchain->command_queue)
2719 FIXME("Ignoring present queue %p.\n", present_queue[i]);
2722 return d3d12_swapchain_resize_buffers(swapchain, buffer_count, width, height, format, flags);
2725 static const struct IDXGISwapChain3Vtbl d3d12_swapchain_vtbl =
2727 /* IUnknown methods */
2728 d3d12_swapchain_QueryInterface,
2729 d3d12_swapchain_AddRef,
2730 d3d12_swapchain_Release,
2731 /* IDXGIObject methods */
2732 d3d12_swapchain_SetPrivateData,
2733 d3d12_swapchain_SetPrivateDataInterface,
2734 d3d12_swapchain_GetPrivateData,
2735 d3d12_swapchain_GetParent,
2736 /* IDXGIDeviceSubObject methods */
2737 d3d12_swapchain_GetDevice,
2738 /* IDXGISwapChain methods */
2739 d3d12_swapchain_Present,
2740 d3d12_swapchain_GetBuffer,
2741 d3d12_swapchain_SetFullscreenState,
2742 d3d12_swapchain_GetFullscreenState,
2743 d3d12_swapchain_GetDesc,
2744 d3d12_swapchain_ResizeBuffers,
2745 d3d12_swapchain_ResizeTarget,
2746 d3d12_swapchain_GetContainingOutput,
2747 d3d12_swapchain_GetFrameStatistics,
2748 d3d12_swapchain_GetLastPresentCount,
2749 /* IDXGISwapChain1 methods */
2750 d3d12_swapchain_GetDesc1,
2751 d3d12_swapchain_GetFullscreenDesc,
2752 d3d12_swapchain_GetHwnd,
2753 d3d12_swapchain_GetCoreWindow,
2754 d3d12_swapchain_Present1,
2755 d3d12_swapchain_IsTemporaryMonoSupported,
2756 d3d12_swapchain_GetRestrictToOutput,
2757 d3d12_swapchain_SetBackgroundColor,
2758 d3d12_swapchain_GetBackgroundColor,
2759 d3d12_swapchain_SetRotation,
2760 d3d12_swapchain_GetRotation,
2761 /* IDXGISwapChain2 methods */
2762 d3d12_swapchain_SetSourceSize,
2763 d3d12_swapchain_GetSourceSize,
2764 d3d12_swapchain_SetMaximumFrameLatency,
2765 d3d12_swapchain_GetMaximumFrameLatency,
2766 d3d12_swapchain_GetFrameLatencyWaitableObject,
2767 d3d12_swapchain_SetMatrixTransform,
2768 d3d12_swapchain_GetMatrixTransform,
2769 /* IDXGISwapChain3 methods */
2770 d3d12_swapchain_GetCurrentBackBufferIndex,
2771 d3d12_swapchain_CheckColorSpaceSupport,
2772 d3d12_swapchain_SetColorSpace1,
2773 d3d12_swapchain_ResizeBuffers1,
2776 static BOOL load_vkd3d_functions(void *vkd3d_handle)
2778 #define LOAD_FUNCPTR(f) if (!(f = get_library_proc(vkd3d_handle, #f))) return FALSE;
2779 LOAD_FUNCPTR(vkd3d_acquire_vk_queue)
2780 LOAD_FUNCPTR(vkd3d_create_image_resource)
2781 LOAD_FUNCPTR(vkd3d_get_device_parent)
2782 LOAD_FUNCPTR(vkd3d_get_vk_device)
2783 LOAD_FUNCPTR(vkd3d_get_vk_format)
2784 LOAD_FUNCPTR(vkd3d_get_vk_physical_device)
2785 LOAD_FUNCPTR(vkd3d_get_vk_queue_family_index)
2786 LOAD_FUNCPTR(vkd3d_instance_from_device)
2787 LOAD_FUNCPTR(vkd3d_instance_get_vk_instance)
2788 LOAD_FUNCPTR(vkd3d_release_vk_queue)
2789 LOAD_FUNCPTR(vkd3d_resource_decref)
2790 LOAD_FUNCPTR(vkd3d_resource_incref)
2791 #undef LOAD_FUNCPTR
2793 return TRUE;
2796 static void *vkd3d_handle;
2798 static BOOL WINAPI init_vkd3d_once(INIT_ONCE *once, void *param, void **context)
2800 TRACE("Loading vkd3d %s.\n", SONAME_LIBVKD3D);
2802 if (!(vkd3d_handle = load_library(SONAME_LIBVKD3D)))
2803 return FALSE;
2805 if (!load_vkd3d_functions(vkd3d_handle))
2807 ERR("Failed to load vkd3d functions.\n");
2808 close_library(vkd3d_handle);
2809 vkd3d_handle = NULL;
2810 return FALSE;
2813 return TRUE;
2816 static BOOL init_vkd3d(void)
2818 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
2819 InitOnceExecuteOnce(&init_once, init_vkd3d_once, NULL, NULL);
2820 return !!vkd3d_handle;
2823 static BOOL init_vk_funcs(struct dxgi_vk_funcs *dxgi, VkInstance vk_instance, VkDevice vk_device)
2825 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
2826 PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;
2828 dxgi->vulkan_module = NULL;
2830 if (!(vkGetInstanceProcAddr = load_vulkan(&dxgi->vulkan_module)))
2832 ERR_(winediag)("Failed to load Vulkan.\n");
2833 return FALSE;
2836 vkGetDeviceProcAddr = (void *)vkGetInstanceProcAddr(vk_instance, "vkGetDeviceProcAddr");
2838 #define LOAD_INSTANCE_PFN(name) \
2839 if (!(dxgi->p_##name = (void *)vkGetInstanceProcAddr(vk_instance, #name))) \
2841 ERR("Failed to get instance proc "#name".\n"); \
2842 close_library(dxgi->vulkan_module); \
2843 return FALSE; \
2845 LOAD_INSTANCE_PFN(vkCreateWin32SurfaceKHR)
2846 LOAD_INSTANCE_PFN(vkDestroySurfaceKHR)
2847 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceMemoryProperties)
2848 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR)
2849 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfaceFormatsKHR)
2850 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfacePresentModesKHR)
2851 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceSurfaceSupportKHR)
2852 LOAD_INSTANCE_PFN(vkGetPhysicalDeviceWin32PresentationSupportKHR)
2853 #undef LOAD_INSTANCE_PFN
2855 #define LOAD_DEVICE_PFN(name) \
2856 if (!(dxgi->p_##name = (void *)vkGetDeviceProcAddr(vk_device, #name))) \
2858 ERR("Failed to get device proc "#name".\n"); \
2859 close_library(dxgi->vulkan_module); \
2860 return FALSE; \
2862 LOAD_DEVICE_PFN(vkAcquireNextImageKHR)
2863 LOAD_DEVICE_PFN(vkAllocateCommandBuffers)
2864 LOAD_DEVICE_PFN(vkAllocateMemory)
2865 LOAD_DEVICE_PFN(vkBeginCommandBuffer)
2866 LOAD_DEVICE_PFN(vkBindImageMemory)
2867 LOAD_DEVICE_PFN(vkCmdBlitImage)
2868 LOAD_DEVICE_PFN(vkCmdPipelineBarrier)
2869 LOAD_DEVICE_PFN(vkCreateCommandPool)
2870 LOAD_DEVICE_PFN(vkCreateFence)
2871 LOAD_DEVICE_PFN(vkCreateImage)
2872 LOAD_DEVICE_PFN(vkCreateSemaphore)
2873 LOAD_DEVICE_PFN(vkCreateSwapchainKHR)
2874 LOAD_DEVICE_PFN(vkDestroyCommandPool)
2875 LOAD_DEVICE_PFN(vkDestroyFence)
2876 LOAD_DEVICE_PFN(vkDestroyImage)
2877 LOAD_DEVICE_PFN(vkDestroySemaphore)
2878 LOAD_DEVICE_PFN(vkDestroySwapchainKHR)
2879 LOAD_DEVICE_PFN(vkEndCommandBuffer)
2880 LOAD_DEVICE_PFN(vkFreeMemory)
2881 LOAD_DEVICE_PFN(vkResetCommandBuffer)
2882 LOAD_DEVICE_PFN(vkGetImageMemoryRequirements)
2883 LOAD_DEVICE_PFN(vkGetSwapchainImagesKHR)
2884 LOAD_DEVICE_PFN(vkQueuePresentKHR)
2885 LOAD_DEVICE_PFN(vkQueueSubmit)
2886 LOAD_DEVICE_PFN(vkQueueWaitIdle)
2887 LOAD_DEVICE_PFN(vkResetFences)
2888 LOAD_DEVICE_PFN(vkWaitForFences)
2889 #undef LOAD_DEVICE_PFN
2891 return TRUE;
2894 static HRESULT d3d12_swapchain_init(struct d3d12_swapchain *swapchain, IWineDXGIFactory *factory,
2895 ID3D12Device *device, ID3D12CommandQueue *queue, HWND window,
2896 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc)
2898 const struct dxgi_vk_funcs *vk_funcs = &swapchain->vk_funcs;
2899 struct wined3d_swapchain_desc wined3d_desc;
2900 VkWin32SurfaceCreateInfoKHR surface_desc;
2901 VkPhysicalDevice vk_physical_device;
2902 VkFenceCreateInfo fence_desc;
2903 uint32_t queue_family_index;
2904 VkSurfaceKHR vk_surface;
2905 IUnknown *device_parent;
2906 VkInstance vk_instance;
2907 IDXGIAdapter *adapter;
2908 IDXGIOutput *output;
2909 VkBool32 supported;
2910 VkDevice vk_device;
2911 VkFence vk_fence;
2912 VkResult vr;
2913 HRESULT hr;
2915 if (window == GetDesktopWindow())
2917 WARN("D3D12 swapchain cannot be created on desktop window.\n");
2918 return E_ACCESSDENIED;
2921 swapchain->IDXGISwapChain3_iface.lpVtbl = &d3d12_swapchain_vtbl;
2922 swapchain->refcount = 1;
2924 swapchain->window = window;
2925 swapchain->desc = *swapchain_desc;
2926 swapchain->fullscreen_desc = *fullscreen_desc;
2928 swapchain->present_mode = VK_PRESENT_MODE_FIFO_KHR;
2930 switch (swapchain_desc->SwapEffect)
2932 case DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL:
2933 case DXGI_SWAP_EFFECT_FLIP_DISCARD:
2934 FIXME("Ignoring swap effect %#x.\n", swapchain_desc->SwapEffect);
2935 break;
2936 default:
2937 WARN("Invalid swap effect %#x.\n", swapchain_desc->SwapEffect);
2938 return DXGI_ERROR_INVALID_CALL;
2941 if (!init_vkd3d())
2943 ERR_(winediag)("libvkd3d could not be loaded.\n");
2944 return DXGI_ERROR_UNSUPPORTED;
2947 device_parent = vkd3d_get_device_parent(device);
2948 if (FAILED(hr = IUnknown_QueryInterface(device_parent, &IID_IDXGIAdapter, (void **)&adapter)))
2949 return hr;
2951 if (FAILED(hr = dxgi_get_output_from_window((IDXGIFactory *)factory, window, &output)))
2953 WARN("Failed to get output from window %p, hr %#x.\n", window, hr);
2955 /* FIXME: As wined3d only supports one output currently, even if a window is on a
2956 * non-primary output, the swapchain will use the primary output. Keep this behaviour
2957 * until all outputs are correctly enumerated. Otherwise it will create a regression
2958 * for applications that specify a device window on a non-primary output */
2959 if (FAILED(hr = IDXGIAdapter_EnumOutputs(adapter, 0, &output)))
2961 IDXGIAdapter_Release(adapter);
2962 return hr;
2965 FIXME("Using the primary output for the device window that is on a non-primary output.\n");
2967 IDXGIAdapter_Release(adapter);
2969 hr = wined3d_swapchain_desc_from_dxgi(&wined3d_desc, output, window, swapchain_desc,
2970 fullscreen_desc);
2971 IDXGIOutput_Release(output);
2972 if (FAILED(hr))
2973 return hr;
2974 if (FAILED(hr = wined3d_swapchain_state_create(&wined3d_desc, window, &swapchain->state)))
2975 return hr;
2977 if (swapchain_desc->BufferUsage && swapchain_desc->BufferUsage != DXGI_USAGE_RENDER_TARGET_OUTPUT)
2978 FIXME("Ignoring buffer usage %#x.\n", swapchain_desc->BufferUsage);
2979 if (swapchain_desc->Scaling != DXGI_SCALING_STRETCH && swapchain_desc->Scaling != DXGI_SCALING_NONE)
2980 FIXME("Ignoring scaling %#x.\n", swapchain_desc->Scaling);
2981 if (swapchain_desc->AlphaMode && swapchain_desc->AlphaMode != DXGI_ALPHA_MODE_IGNORE)
2982 FIXME("Ignoring alpha mode %#x.\n", swapchain_desc->AlphaMode);
2983 if (swapchain_desc->Flags & ~(DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
2984 FIXME("Ignoring swapchain flags %#x.\n", swapchain_desc->Flags);
2986 if (fullscreen_desc->RefreshRate.Numerator || fullscreen_desc->RefreshRate.Denominator)
2987 FIXME("Ignoring refresh rate.\n");
2988 if (fullscreen_desc->ScanlineOrdering)
2989 FIXME("Unhandled scanline ordering %#x.\n", fullscreen_desc->ScanlineOrdering);
2990 if (fullscreen_desc->Scaling)
2991 FIXME("Unhandled mode scaling %#x.\n", fullscreen_desc->Scaling);
2992 if (!fullscreen_desc->Windowed)
2993 FIXME("Fullscreen not supported yet.\n");
2995 vk_instance = vkd3d_instance_get_vk_instance(vkd3d_instance_from_device(device));
2996 vk_physical_device = vkd3d_get_vk_physical_device(device);
2997 vk_device = vkd3d_get_vk_device(device);
2999 swapchain->vk_instance = vk_instance;
3000 swapchain->vk_device = vk_device;
3001 swapchain->vk_physical_device = vk_physical_device;
3003 if (!init_vk_funcs(&swapchain->vk_funcs, vk_instance, vk_device))
3005 wined3d_swapchain_state_destroy(swapchain->state);
3006 return E_FAIL;
3009 wined3d_private_store_init(&swapchain->private_store);
3011 surface_desc.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3012 surface_desc.pNext = NULL;
3013 surface_desc.flags = 0;
3014 surface_desc.hinstance = GetModuleHandleA("dxgi.dll");
3015 surface_desc.hwnd = window;
3016 if ((vr = vk_funcs->p_vkCreateWin32SurfaceKHR(vk_instance, &surface_desc, NULL, &vk_surface)) < 0)
3018 WARN("Failed to create Vulkan surface, vr %d.\n", vr);
3019 d3d12_swapchain_destroy(swapchain);
3020 return hresult_from_vk_result(vr);
3022 swapchain->vk_surface = vk_surface;
3024 queue_family_index = vkd3d_get_vk_queue_family_index(queue);
3025 if ((vr = vk_funcs->p_vkGetPhysicalDeviceSurfaceSupportKHR(vk_physical_device,
3026 queue_family_index, vk_surface, &supported)) < 0 || !supported)
3028 FIXME("Queue family does not support presentation, vr %d.\n", vr);
3029 d3d12_swapchain_destroy(swapchain);
3030 return DXGI_ERROR_UNSUPPORTED;
3033 ID3D12CommandQueue_AddRef(swapchain->command_queue = queue);
3034 ID3D12Device_AddRef(swapchain->device = device);
3036 if (FAILED(hr = d3d12_swapchain_create_vulkan_swapchain(swapchain)))
3038 d3d12_swapchain_destroy(swapchain);
3039 return hr;
3042 fence_desc.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
3043 fence_desc.pNext = NULL;
3044 fence_desc.flags = 0;
3045 if ((vr = vk_funcs->p_vkCreateFence(vk_device, &fence_desc, NULL, &vk_fence)) < 0)
3047 WARN("Failed to create Vulkan fence, vr %d.\n", vr);
3048 d3d12_swapchain_destroy(swapchain);
3049 return hresult_from_vk_result(vr);
3051 swapchain->vk_fence = vk_fence;
3053 swapchain->current_buffer_index = 0;
3054 if ((vr = d3d12_swapchain_acquire_next_back_buffer(swapchain)) < 0)
3056 d3d12_swapchain_destroy(swapchain);
3057 return hresult_from_vk_result(vr);
3060 if (swapchain_desc->Flags & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT)
3062 swapchain->frame_number = DXGI_MAX_SWAP_CHAIN_BUFFERS;
3063 swapchain->frame_latency = 1;
3065 if (FAILED(hr = ID3D12Device_CreateFence(device, DXGI_MAX_SWAP_CHAIN_BUFFERS,
3066 0, &IID_ID3D12Fence, (void **)&swapchain->frame_latency_fence)))
3068 WARN("Failed to create frame latency fence, hr %#x.\n", hr);
3069 d3d12_swapchain_destroy(swapchain);
3070 return hr;
3073 if (!(swapchain->frame_latency_event = CreateEventW(NULL, FALSE, TRUE, NULL)))
3075 hr = HRESULT_FROM_WIN32(GetLastError());
3076 WARN("Failed to create frame latency event, hr %#x.\n", hr);
3077 d3d12_swapchain_destroy(swapchain);
3078 return hr;
3082 IWineDXGIFactory_AddRef(swapchain->factory = factory);
3084 return S_OK;
3087 HRESULT d3d12_swapchain_create(IWineDXGIFactory *factory, ID3D12CommandQueue *queue, HWND window,
3088 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc,
3089 IDXGISwapChain1 **swapchain)
3091 DXGI_SWAP_CHAIN_FULLSCREEN_DESC default_fullscreen_desc;
3092 struct d3d12_swapchain *object;
3093 ID3D12Device *device;
3094 HRESULT hr;
3096 if (swapchain_desc->Format == DXGI_FORMAT_UNKNOWN)
3097 return DXGI_ERROR_INVALID_CALL;
3099 if (!fullscreen_desc)
3101 memset(&default_fullscreen_desc, 0, sizeof(default_fullscreen_desc));
3102 default_fullscreen_desc.Windowed = TRUE;
3103 fullscreen_desc = &default_fullscreen_desc;
3106 if (!(object = heap_alloc_zero(sizeof(*object))))
3107 return E_OUTOFMEMORY;
3109 if (FAILED(hr = ID3D12CommandQueue_GetDevice(queue, &IID_ID3D12Device, (void **)&device)))
3111 ERR("Failed to get D3D12 device, hr %#x.\n", hr);
3112 heap_free(object);
3113 return hr;
3116 hr = d3d12_swapchain_init(object, factory, device, queue, window, swapchain_desc, fullscreen_desc);
3117 ID3D12Device_Release(device);
3118 if (FAILED(hr))
3120 heap_free(object);
3121 return hr;
3124 TRACE("Created swapchain %p.\n", object);
3126 *swapchain = (IDXGISwapChain1 *)&object->IDXGISwapChain3_iface;
3128 return S_OK;
3131 #else
3133 HRESULT d3d12_swapchain_create(IWineDXGIFactory *factory, ID3D12CommandQueue *queue, HWND window,
3134 const DXGI_SWAP_CHAIN_DESC1 *swapchain_desc, const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc,
3135 IDXGISwapChain1 **swapchain)
3137 ERR_(winediag)("Wine was built without Direct3D 12 support.\n");
3138 return DXGI_ERROR_UNSUPPORTED;
3141 #endif /* SONAME_LIBVKD3D */