Debian Codegraph @

src/kosmickrisp/vulkan/kk_queue.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_queue.h"
9 #include "kk_buffer.h"
10 #include "kk_cmd_buffer.h"
11 #include "kk_device.h"
12 #include "kk_encoder.h"
13 #include "kk_physical_device.h"
14 #include "kk_sync.h"
15
16 #include "kosmickrisp/bridge/mtl_bridge.h"
17
18 #include "vk_cmd_queue.h"
19
20 static VkResult
21 kk_queue_submit(struct vk_queue *vk_queue, struct vk_queue_submit *submit)
22 {
23 struct kk_queue *queue = container_of(vk_queue, struct kk_queue, vk);
24 struct kk_device *dev = kk_queue_device(queue);
25
26 if (vk_queue_is_lost(&queue->vk))
27 return VK_ERROR_DEVICE_LOST;
28
29 struct kk_encoder *encoder;
30 VkResult result = kk_encoder_init(dev->mtl_handle, queue, &encoder);
31 if (result != VK_SUCCESS)
32 return result;
33
34 /* Chain with previous sumbission */
35 if (queue->wait_fence) {
36 util_dynarray_append(&encoder->main.fences, queue->wait_fence);
37 encoder->main.wait_fence = true;
38 }
39
40 for (struct vk_sync_wait *wait = submit->waits,
41 *end = submit->waits + submit->wait_count;
42 wait != end; ++wait) {
43 struct kk_sync_timeline *sync =
44 container_of(wait->sync, struct kk_sync_timeline, base);
45 mtl_encode_wait_for_event(encoder->main.cmd_buffer, sync->mtl_handle,
46 wait->wait_value);
47 }
48
49 for (uint32_t i = 0; i < submit->command_buffer_count; ++i) {
50 struct kk_cmd_buffer *cmd_buffer =
51 container_of(submit->command_buffers[i], struct kk_cmd_buffer, vk);
52 cmd_buffer->encoder = encoder;
53 /* TODO_KOSMICKRISP We need to release command buffer resources here for
54 * the following case: User records command buffers once and then reuses
55 * them multiple times. The resource release should be done at
56 * vkBeginCommandBuffer, but because we are recording all commands to
57 * later execute them at queue submission, the recording does not record
58 * the begin/end commands and jumps straight to the actual commands. */
59 kk_cmd_release_resources(dev, cmd_buffer);
60
61 vk_cmd_queue_execute(&cmd_buffer->vk.cmd_queue,
62 kk_cmd_buffer_to_handle(cmd_buffer),
63 &dev->vk.dispatch_table);
64 kk_encoder_end(cmd_buffer);
65 cmd_buffer->encoder = NULL;
66 }
67
68 for (uint32_t i = 0u; i < submit->signal_count; ++i) {
69 struct vk_sync_signal *signal = &submit->signals[i];
70 struct kk_sync_timeline *sync =
71 container_of(signal->sync, struct kk_sync_timeline, base);
72 mtl_encode_signal_event(encoder->main.cmd_buffer, sync->mtl_handle,
73 signal->signal_value);
74 }
75
76 /* Ensure any changes to residency are propagated before we submit any work.
77 * All resources should have been allocated before submission. Otherwise,
78 * users are playing with fire. */
79 kk_device_make_resources_resident(dev);
80
81 /* Steal the last fence to chain with the next submission */
82 if (util_dynarray_num_elements(&encoder->main.fences, mtl_fence *) > 0)
83 queue->wait_fence = util_dynarray_pop(&encoder->main.fences, mtl_fence *);
84 kk_encoder_submit(encoder);
85
86 return VK_SUCCESS;
87 }
88
89 VkResult
90 kk_queue_init(struct kk_device *dev, struct kk_queue *queue,
91 const VkDeviceQueueCreateInfo *pCreateInfo,
92 uint32_t index_in_family)
93 {
94 VkResult result;
95
96 result = vk_queue_init(&queue->vk, &dev->vk, pCreateInfo, index_in_family);
97 if (result != VK_SUCCESS)
98 return result;
99
100 queue->main.mtl_handle =
101 mtl_new_command_queue(dev->mtl_handle, KK_MAX_CMD_BUFFERS);
102 queue->pre_gfx.mtl_handle =
103 mtl_new_command_queue(dev->mtl_handle, KK_MAX_CMD_BUFFERS);
104
105 queue->vk.driver_submit = kk_queue_submit;
106
107 return VK_SUCCESS;
108 }
109
110 void
111 kk_queue_finish(struct kk_device *dev, struct kk_queue *queue)
112 {
113 if (queue->wait_fence)
114 mtl_release(queue->wait_fence);
115 mtl_release(queue->pre_gfx.mtl_handle);
116 mtl_release(queue->main.mtl_handle);
117 vk_queue_finish(&queue->vk);
118 }