xref: /aosp_15_r20/external/mesa3d/src/microsoft/vulkan/dzn_wsi.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "dzn_private.h"
25 #include "vk_util.h"
26 
27 static PFN_vkVoidFunction VKAPI_PTR
dzn_wsi_proc_addr(VkPhysicalDevice physicalDevice,const char * pName)28 dzn_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
29 {
30    VK_FROM_HANDLE(dzn_physical_device, pdevice, physicalDevice);
31    return vk_instance_get_proc_addr_unchecked(pdevice->vk.instance, pName);
32 }
33 
34 static void *
dzn_wsi_get_d3d12_command_queue(VkDevice dev)35 dzn_wsi_get_d3d12_command_queue(VkDevice dev)
36 {
37    VK_FROM_HANDLE(dzn_device, device, dev);
38 
39    return device->swapchain_queue->cmdqueue;
40 }
41 
42 static VkQueue
dzn_wsi_get_blit_queue(VkDevice dev)43 dzn_wsi_get_blit_queue(VkDevice dev)
44 {
45    VK_FROM_HANDLE(dzn_device, device, dev);
46 
47    return dzn_queue_to_handle(device->swapchain_queue);
48 }
49 
50 static bool
dzn_wsi_needs_blits(VkDevice dev)51 dzn_wsi_needs_blits(VkDevice dev)
52 {
53    VK_FROM_HANDLE(dzn_device, device, dev);
54    return device->need_swapchain_blits;
55 }
56 
57 static VkResult
dzn_wsi_create_image_memory(VkDevice dev,void * resource,const VkAllocationCallbacks * alloc,VkDeviceMemory * out)58 dzn_wsi_create_image_memory(VkDevice dev, void *resource,
59                             const VkAllocationCallbacks *alloc,
60                             VkDeviceMemory *out)
61 {
62    VK_FROM_HANDLE(dzn_device, device, dev);
63 
64    struct dzn_device_memory *mem =
65       vk_zalloc2(&device->vk.alloc, alloc, sizeof(*mem), 8,
66                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
67    if (!mem)
68       return VK_ERROR_OUT_OF_HOST_MEMORY;
69 
70    vk_object_base_init(&device->vk, &mem->base, VK_OBJECT_TYPE_DEVICE_MEMORY);
71    mem->dedicated_res = resource;
72    ID3D12Resource_AddRef(mem->dedicated_res);
73 
74    *out = dzn_device_memory_to_handle(mem);
75    return VK_SUCCESS;
76 }
77 
78 void
dzn_wsi_finish(struct dzn_physical_device * physical_device)79 dzn_wsi_finish(struct dzn_physical_device *physical_device)
80 {
81    wsi_device_finish(&physical_device->wsi_device,
82                      &physical_device->vk.instance->alloc);
83 }
84 
85 VkResult
dzn_wsi_init(struct dzn_physical_device * physical_device)86 dzn_wsi_init(struct dzn_physical_device *physical_device)
87 {
88    VkResult result;
89 
90 #ifdef _WIN32
91    bool sw = false;
92 #else
93    bool sw = true;
94 #endif
95 
96    result = wsi_device_init(&physical_device->wsi_device,
97                             dzn_physical_device_to_handle(physical_device),
98                             dzn_wsi_proc_addr,
99                             &physical_device->vk.instance->alloc,
100                             -1, NULL,
101                             &(struct wsi_device_options){.sw_device = sw});
102 
103    if (result != VK_SUCCESS)
104       return result;
105 
106    physical_device->wsi_device.win32.get_d3d12_command_queue =
107       dzn_wsi_get_d3d12_command_queue;
108    physical_device->wsi_device.win32.requires_blits = dzn_wsi_needs_blits;
109    physical_device->wsi_device.get_blit_queue = dzn_wsi_get_blit_queue;
110    physical_device->wsi_device.win32.create_image_memory =
111       dzn_wsi_create_image_memory;
112    physical_device->wsi_device.supports_modifiers = false;
113    physical_device->vk.wsi_device = &physical_device->wsi_device;
114 
115    return VK_SUCCESS;
116 }
117