Debian Codegraph @

src/kosmickrisp/vulkan/kk_device.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_device.h"
9
10 #include "kk_cmd_buffer.h"
11 #include "kk_entrypoints.h"
12 #include "kk_instance.h"
13 #include "kk_physical_device.h"
14 #include "kk_shader.h"
15
16 #include "kosmickrisp/bridge/mtl_bridge.h"
17
18 #include "vk_cmd_enqueue_entrypoints.h"
19 #include "vk_common_entrypoints.h"
20
21 #include "vulkan/wsi/wsi_common.h"
22 #include "vk_pipeline_cache.h"
23
24 #include <time.h>
25
26 DERIVE_HASH_TABLE(mtl_sampler_packed);
27
28 static VkResult
29 kk_init_sampler_heap(struct kk_device *dev, struct kk_sampler_heap *h)
30 {
31 h->ht = mtl_sampler_packed_table_create(NULL);
32 if (!h->ht)
33 return VK_ERROR_OUT_OF_HOST_MEMORY;
34
35 VkResult result = kk_query_table_init(dev, &h->table, 1024);
36
37 if (result != VK_SUCCESS) {
38 ralloc_free(h->ht);
39 return result;
40 }
41
42 simple_mtx_init(&h->lock, mtx_plain);
43 return VK_SUCCESS;
44 }
45
46 static void
47 kk_destroy_sampler_heap(struct kk_device *dev, struct kk_sampler_heap *h)
48 {
49 struct hash_entry *entry = _mesa_hash_table_next_entry(h->ht, NULL);
50 while (entry) {
51 struct kk_rc_sampler *sampler = (struct kk_rc_sampler *)entry->data;
52 mtl_release(sampler->handle);
53 entry = _mesa_hash_table_next_entry(h->ht, entry);
54 }
55 kk_query_table_finish(dev, &h->table);
56 ralloc_free(h->ht);
57 simple_mtx_destroy(&h->lock);
58 }
59
60 static VkResult
61 kk_sampler_heap_add_locked(struct kk_device *dev, struct kk_sampler_heap *h,
62 struct mtl_sampler_packed desc,
63 struct kk_rc_sampler **out)
64 {
65 struct hash_entry *ent = _mesa_hash_table_search(h->ht, &desc);
66 if (ent != NULL) {
67 *out = ent->data;
68
69 assert((*out)->refcount != 0);
70 (*out)->refcount++;
71
72 return VK_SUCCESS;
73 }
74
75 struct kk_rc_sampler *rc = ralloc(h->ht, struct kk_rc_sampler);
76 if (!rc)
77 return VK_ERROR_OUT_OF_HOST_MEMORY;
78
79 mtl_sampler *handle = kk_sampler_create(dev, &desc);
80 uint64_t gpu_id = mtl_sampler_get_gpu_resource_id(handle);
81
82 uint32_t index;
83 VkResult result = kk_query_table_add(dev, &h->table, gpu_id, &index);
84 if (result != VK_SUCCESS) {
85 mtl_release(handle);
86 ralloc_free(rc);
87 return result;
88 }
89
90 *rc = (struct kk_rc_sampler){
91 .key = desc,
92 .handle = handle,
93 .refcount = 1,
94 .index = index,
95 };
96
97 _mesa_hash_table_insert(h->ht, &rc->key, rc);
98 *out = rc;
99
100 return VK_SUCCESS;
101 }
102
103 VkResult
104 kk_sampler_heap_add(struct kk_device *dev, struct mtl_sampler_packed desc,
105 struct kk_rc_sampler **out)
106 {
107 struct kk_sampler_heap *h = &dev->samplers;
108
109 simple_mtx_lock(&h->lock);
110 VkResult result = kk_sampler_heap_add_locked(dev, h, desc, out);
111 simple_mtx_unlock(&h->lock);
112
113 return result;
114 }
115
116 static void
117 kk_sampler_heap_remove_locked(struct kk_device *dev, struct kk_sampler_heap *h,
118 struct kk_rc_sampler *rc)
119 {
120 assert(rc->refcount != 0);
121 rc->refcount--;
122
123 if (rc->refcount == 0) {
124 mtl_release(rc->handle);
125 kk_query_table_remove(dev, &h->table, rc->index);
126 _mesa_hash_table_remove_key(h->ht, &rc->key);
127 ralloc_free(rc);
128 }
129 }
130
131 void
132 kk_sampler_heap_remove(struct kk_device *dev, struct kk_rc_sampler *rc)
133 {
134 struct kk_sampler_heap *h = &dev->samplers;
135
136 simple_mtx_lock(&h->lock);
137 kk_sampler_heap_remove_locked(dev, h, rc);
138 simple_mtx_unlock(&h->lock);
139 }
140
141 static void
142 kk_parse_device_environment_options(struct kk_device *dev)
143 {
144 dev->gpu_capture_enabled =
145 debug_get_bool_option("MESA_KK_GPU_CAPTURE", false);
146 if (dev->gpu_capture_enabled) {
147 const char *capture_directory =
148 debug_get_option("MESA_KK_GPU_CAPTURE_DIRECTORY", NULL);
149 mtl_start_gpu_capture(dev->mtl_handle, capture_directory);
150 }
151
152 const char *list = debug_get_option("MESA_KK_DISABLE_WORKAROUNDS", "");
153 const char *all_workarounds = "all";
154 const size_t all_len = strlen(all_workarounds);
155 for (unsigned n; n = strcspn(list, ","), *list; list += MAX2(1, n)) {
156 if (n == all_len && !strncmp(list, all_workarounds, n)) {
157 dev->disabled_workarounds = UINT64_MAX;
158 break;
159 }
160
161 int index = atoi(list);
162 dev->disabled_workarounds |= BITFIELD64_BIT(index);
163 }
164 }
165
166 static VkResult
167 kk_get_timestamp(struct vk_device *device, uint64_t *timestamp)
168 {
169 struct kk_device *dev = container_of(device, struct kk_device, vk);
170
171 *timestamp = mtl_device_get_gpu_timestamp(dev->mtl_handle);
172 return VK_SUCCESS;
173 }
174
175 VKAPI_ATTR VkResult VKAPI_CALL
176 kk_CreateDevice(VkPhysicalDevice physicalDevice,
177 const VkDeviceCreateInfo *pCreateInfo,
178 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice)
179 {
180 VK_FROM_HANDLE(kk_physical_device, pdev, physicalDevice);
181 VkResult result = VK_ERROR_OUT_OF_HOST_MEMORY;
182 struct kk_device *dev;
183
184 dev = vk_zalloc2(&pdev->vk.instance->alloc, pAllocator, sizeof(*dev), 8,
185 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
186 if (!dev)
187 return vk_error(pdev, VK_ERROR_OUT_OF_HOST_MEMORY);
188
189 /* Fill the dispatch table we will expose to the users */
190 vk_device_dispatch_table_from_entrypoints(
191 &dev->exposed_dispatch_table, &vk_cmd_enqueue_device_entrypoints, true);
192 vk_device_dispatch_table_from_entrypoints(&dev->exposed_dispatch_table,
193 &kk_device_entrypoints, false);
194 vk_device_dispatch_table_from_entrypoints(&dev->exposed_dispatch_table,
195 &wsi_device_entrypoints, false);
196 vk_device_dispatch_table_from_entrypoints(
197 &dev->exposed_dispatch_table, &vk_common_device_entrypoints, false);
198
199 struct vk_device_dispatch_table dispatch_table;
200 vk_device_dispatch_table_from_entrypoints(&dispatch_table,
201 &kk_device_entrypoints, true);
202 vk_device_dispatch_table_from_entrypoints(
203 &dispatch_table, &vk_common_device_entrypoints, false);
204 vk_device_dispatch_table_from_entrypoints(&dispatch_table,
205 &wsi_device_entrypoints, false);
206
207 result = vk_device_init(&dev->vk, &pdev->vk, &dispatch_table, pCreateInfo,
208 pAllocator);
209 if (result != VK_SUCCESS)
210 goto fail_alloc;
211
212 dev->vk.shader_ops = &kk_device_shader_ops;
213 dev->mtl_handle = pdev->mtl_dev_handle;
214 dev->vk.command_buffer_ops = &kk_cmd_buffer_ops;
215 dev->vk.command_dispatch_table = &dev->vk.dispatch_table;
216 dev->vk.get_timestamp = kk_get_timestamp;
217
218 /* We need to initialize the device residency set before any bo is created. */
219 simple_mtx_init(&dev->residency_set.mutex, mtx_plain);
220 dev->residency_set.handle = mtl_new_residency_set(dev->mtl_handle);
221 if (dev->residency_set.handle == NULL)
222 goto fail_init;
223
224 result =
225 kk_queue_init(dev, &dev->queue, &pCreateInfo->pQueueCreateInfos[0], 0);
226 if (result != VK_SUCCESS)
227 goto fail_vab_memory;
228
229 result = kk_device_init_meta(dev);
230 if (result != VK_SUCCESS)
231 goto fail_mem_cache;
232
233 result = kk_query_table_init(dev, &dev->occlusion_queries,
234 KK_MAX_OCCLUSION_QUERIES);
235 if (result != VK_SUCCESS)
236 goto fail_meta;
237
238 result = kk_init_sampler_heap(dev, &dev->samplers);
239 if (result != VK_SUCCESS)
240 goto fail_query_table;
241
242 result = kk_device_init_lib(dev);
243 if (result != VK_SUCCESS)
244 goto fail_sampler_heap;
245
246 kk_parse_device_environment_options(dev);
247
248 *pDevice = kk_device_to_handle(dev);
249
250 return VK_SUCCESS;
251
252 fail_sampler_heap:
253 kk_destroy_sampler_heap(dev, &dev->samplers);
254 fail_query_table:
255 kk_query_table_finish(dev, &dev->occlusion_queries);
256 fail_meta:
257 kk_device_finish_meta(dev);
258 fail_mem_cache:
259 kk_queue_finish(dev, &dev->queue);
260 fail_vab_memory:
261 mtl_release(dev->residency_set.handle);
262 simple_mtx_destroy(&dev->residency_set.mutex);
263 fail_init:
264 vk_device_finish(&dev->vk);
265 fail_alloc:
266 vk_free(&dev->vk.alloc, dev);
267 return result;
268 }
269
270 VKAPI_ATTR void VKAPI_CALL
271 kk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
272 {
273 VK_FROM_HANDLE(kk_device, dev, _device);
274
275 if (!dev)
276 return;
277
278 /* End capture before we start releasing resources. Otherwise, Metal capture
279 * may run into issues. */
280 if (dev->gpu_capture_enabled) {
281 mtl_stop_gpu_capture();
282 }
283
284 /* Meta first since it may destroy Vulkan objects */
285 kk_device_finish_meta(dev);
286 kk_device_finish_lib(dev);
287 kk_query_table_finish(dev, &dev->occlusion_queries);
288 kk_destroy_sampler_heap(dev, &dev->samplers);
289
290 /* Release the residency set last once all BOs are released. */
291 mtl_release(dev->residency_set.handle);
292 simple_mtx_destroy(&dev->residency_set.mutex);
293
294 kk_queue_finish(dev, &dev->queue);
295 vk_device_finish(&dev->vk);
296
297 vk_free(&dev->vk.alloc, dev);
298 }
299
300 /* We need to implement this ourselves so we give the fake ones for vk_common_*
301 * to work when executing actual commands */
302 static PFN_vkVoidFunction
303 kk_device_get_proc_addr(const struct kk_device *device, const char *name)
304 {
305 if (device == NULL || name == NULL)
306 return NULL;
307
308 struct vk_instance *instance = device->vk.physical->instance;
309 return vk_device_dispatch_table_get_if_supported(
310 &device->exposed_dispatch_table, name, instance->app_info.api_version,
311 &instance->enabled_extensions, &device->vk.enabled_extensions);
312 }
313
314 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
315 kk_GetDeviceProcAddr(VkDevice _device, const char *pName)
316 {
317 VK_FROM_HANDLE(kk_device, device, _device);
318 return kk_device_get_proc_addr(device, pName);
319 }
320
321 void
322 kk_device_add_heap_to_residency_set(struct kk_device *dev, mtl_heap *heap)
323 {
324 simple_mtx_lock(&dev->residency_set.mutex);
325 mtl_residency_set_add_allocation(dev->residency_set.handle, heap);
326 simple_mtx_unlock(&dev->residency_set.mutex);
327 }
328
329 void
330 kk_device_remove_heap_from_residency_set(struct kk_device *dev, mtl_heap *heap)
331 {
332 simple_mtx_lock(&dev->residency_set.mutex);
333 mtl_residency_set_remove_allocation(dev->residency_set.handle, heap);
334 simple_mtx_unlock(&dev->residency_set.mutex);
335 }
336
337 void
338 kk_device_make_resources_resident(struct kk_device *dev)
339 {
340 simple_mtx_lock(&dev->residency_set.mutex);
341 mtl_residency_set_commit(dev->residency_set.handle);
342 mtl_residency_set_request_residency(dev->residency_set.handle);
343 simple_mtx_unlock(&dev->residency_set.mutex);
344 }