Debian Codegraph @

src/kosmickrisp/vulkan/kk_wsi.c

raw symbols SCIP
1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * Copyright 2025 LunarG, Inc.
4 * Copyright 2025 Google LLC
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "kk_wsi.h"
9 #include "kk_cmd_buffer.h"
10 #include "kk_device.h"
11 #include "kk_dispatch_trampolines.h"
12 #include "kk_image.h"
13 #include "kk_instance.h"
14 #include "wsi_common.h"
15
16 #include "kosmickrisp/bridge/mtl_bridge.h"
17
18 static PFN_vkVoidFunction
19 kk_instance_get_proc_addr_unchecked(const struct vk_instance *instance,
20 const char *name)
21 {
22 PFN_vkVoidFunction func;
23
24 if (instance == NULL || name == NULL)
25 return NULL;
26
27 func = vk_instance_dispatch_table_get(&instance->dispatch_table, name);
28 if (func != NULL)
29 return func;
30
31 func = vk_physical_device_dispatch_table_get(&kk_physical_device_trampolines,
32 name);
33 if (func != NULL)
34 return func;
35
36 func = vk_device_dispatch_table_get(&kk_device_trampolines, name);
37 if (func != NULL)
38 return func;
39
40 return NULL;
41 }
42
43 static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
44 kk_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
45 {
46 VK_FROM_HANDLE(kk_physical_device, pdev, physicalDevice);
47 return kk_instance_get_proc_addr_unchecked(pdev->vk.instance, pName);
48 }
49
50 static VkResult
51 kk_bind_drawable_to_vkimage(VkImage vk_image, void *drawable)
52 {
53 VK_FROM_HANDLE(kk_image, image, vk_image);
54 mtl_texture *texture = mtl_drawable_get_texture(drawable);
55
56 /* This should only be called for swapchain binding. */
57 assert(image->plane_count == 1);
58 struct kk_image_plane *plane = &image->planes[0];
59 if (plane->mtl_handle)
60 mtl_release(plane->mtl_handle);
61 if (plane->mtl_handle_array)
62 mtl_release(plane->mtl_handle_array);
63 plane->mtl_handle = mtl_retain(texture);
64 plane->mtl_handle_array = NULL;
65 plane->addr = mtl_texture_get_gpu_resource_id(texture);
66
67 return VK_SUCCESS;
68 }
69
70 static void
71 kk_encode_drawable_present(VkCommandBuffer vk_cmd, void *drawable)
72 {
73 VK_FROM_HANDLE(kk_cmd_buffer, cmd, vk_cmd);
74 mtl_retain(drawable);
75 cmd->drawable = drawable;
76 }
77
78 static struct vk_queue *
79 kk_get_blit_queue(VkDevice device)
80 {
81 /* We only have one queue, so just return that one. */
82 VK_FROM_HANDLE(kk_device, dev, device);
83 return &dev->queue.vk;
84 }
85
86 VkResult
87 kk_init_wsi(struct kk_physical_device *pdev)
88 {
89 struct wsi_device_options wsi_options = {.sw_device = false};
90 struct wsi_device *wsi = &pdev->wsi_device;
91 VkResult result =
92 wsi_device_init(wsi, kk_physical_device_to_handle(pdev), kk_wsi_proc_addr,
93 &pdev->vk.instance->alloc,
94 0u, // Not relevant for metal wsi
95 NULL, // Not relevant for metal
96 &wsi_options);
97 if (result != VK_SUCCESS)
98 return result;
99
100 wsi->metal.bind_drawable_to_vkimage = kk_bind_drawable_to_vkimage;
101 wsi->metal.encode_drawable_present = kk_encode_drawable_present;
102 wsi->get_blit_queue = kk_get_blit_queue;
103
104 pdev->vk.wsi_device = wsi;
105
106 return result;
107 }
108
109 void
110 kk_finish_wsi(struct kk_physical_device *pdev)
111 {
112 pdev->vk.wsi_device = NULL;
113 wsi_device_finish(&pdev->wsi_device, &pdev->vk.instance->alloc);
114 }