Debian Codegraph @

src/kosmickrisp/vulkan/kk_cmd_draw.c

raw symbols SCIP
1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * Copyright 2024 Valve Corporation
4 * Copyright 2024 Alyssa Rosenzweig
5 * Copyright 2025 LunarG, Inc.
6 * Copyright 2025 Google LLC
7 * SPDX-License-Identifier: MIT
8 */
9
10 #include "kk_entrypoints.h"
11
12 #include "kk_buffer.h"
13 #include "kk_cmd_buffer.h"
14 #include "kk_encoder.h"
15 #include "kk_format.h"
16 #include "kk_image_view.h"
17 #include "kk_query_pool.h"
18
19 #include "kosmickrisp/bridge/mtl_bridge.h"
20 #include "kosmickrisp/bridge/vk_to_mtl_map.h"
21
22 #include "vulkan/util/vk_format.h"
23
24 static void
25 kk_cmd_buffer_dirty_render_pass(struct kk_cmd_buffer *cmd)
26 {
27 struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state;
28
29 /* These depend on color attachment count */
30 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_COLOR_WRITE_ENABLES);
31 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_BLEND_ENABLES);
32 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_BLEND_EQUATIONS);
33 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_WRITE_MASKS);
34
35 /* These depend on the depth/stencil format */
36 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_DEPTH_TEST_ENABLE);
37 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_DEPTH_WRITE_ENABLE);
38 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_DEPTH_BOUNDS_TEST_ENABLE);
39 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_STENCIL_TEST_ENABLE);
40
41 /* This may depend on render targets for ESO */
42 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_MS_RASTERIZATION_SAMPLES);
43
44 /* This may depend on render targets */
45 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_COLOR_ATTACHMENT_MAP);
46 }
47
48 static void
49 kk_attachment_init(struct kk_attachment *att,
50 const VkRenderingAttachmentInfo *info)
51 {
52 if (info == NULL || info->imageView == VK_NULL_HANDLE) {
53 *att = (struct kk_attachment){
54 .iview = NULL,
55 };
56 return;
57 }
58
59 VK_FROM_HANDLE(kk_image_view, iview, info->imageView);
60 *att = (struct kk_attachment){
61 .vk_format = iview->vk.format,
62 .iview = iview,
63 };
64
65 if (info->resolveMode != VK_RESOLVE_MODE_NONE) {
66 VK_FROM_HANDLE(kk_image_view, res_iview, info->resolveImageView);
67 att->resolve_mode = info->resolveMode;
68 att->resolve_iview = res_iview;
69 }
70
71 att->store_op = info->storeOp;
72 }
73
74 VKAPI_ATTR void VKAPI_CALL
75 kk_GetRenderingAreaGranularityKHR(
76 VkDevice device, const VkRenderingAreaInfoKHR *pRenderingAreaInfo,
77 VkExtent2D *pGranularity)
78 {
79 *pGranularity = (VkExtent2D){.width = 1, .height = 1};
80 }
81
82 static void
83 kk_merge_render_iview(VkExtent2D *extent, struct kk_image_view *iview)
84 {
85 if (iview) {
86 /* TODO: is this right for ycbcr? */
87 unsigned level = iview->vk.base_mip_level;
88 unsigned width = u_minify(iview->vk.image->extent.width, level);
89 unsigned height = u_minify(iview->vk.image->extent.height, level);
90
91 extent->width = MAX2(extent->width, width);
92 extent->height = MAX2(extent->height, height);
93 }
94 }
95
96 static void
97 kk_fill_common_attachment_description(
98 mtl_render_pass_attachment_descriptor *descriptor,
99 const struct kk_image_view *iview, const VkRenderingAttachmentInfo *info,
100 bool force_attachment_load)
101 {
102 assert(iview->plane_count ==
103 1); /* TODO_KOSMICKRISP Handle multiplanar images? */
104 mtl_render_pass_attachment_descriptor_set_texture(
105 descriptor, iview->planes[0].mtl_handle_render);
106 mtl_render_pass_attachment_descriptor_set_level(descriptor,
107 iview->vk.base_mip_level);
108 mtl_render_pass_attachment_descriptor_set_slice(descriptor,
109 iview->vk.base_array_layer);
110 enum mtl_load_action load_action =
111 force_attachment_load
112 ? MTL_LOAD_ACTION_LOAD
113 : vk_attachment_load_op_to_mtl_load_action(info->loadOp);
114
115 /* TODO_KOSMICKRISP Need to tackle issue #14344 */
116 if (load_action == MTL_LOAD_ACTION_DONT_CARE)
117 load_action = MTL_LOAD_ACTION_LOAD;
118 mtl_render_pass_attachment_descriptor_set_load_action(descriptor,
119 load_action);
120 /* We need to force attachment store to correctly handle situations where the
121 * attachment is written to in a subpass, and later read from in the next one
122 * with the store operation being something else than store. The other reason
123 * being that we break renderpasses when a pipeline barrier is used, so we
124 * need to not loose the information of the attachment when we restart it. */
125 enum mtl_store_action store_action = MTL_STORE_ACTION_STORE;
126 mtl_render_pass_attachment_descriptor_set_store_action(descriptor,
127 store_action);
128 }
129
130 static struct mtl_clear_color
131 vk_clear_color_value_to_mtl_clear_color(union VkClearColorValue color,
132 enum pipe_format format)
133 {
134 struct mtl_clear_color value;
135 if (util_format_is_pure_sint(format)) {
136 value.red = color.int32[0];
137 value.green = color.int32[1];
138 value.blue = color.int32[2];
139 value.alpha = color.int32[3];
140 } else if (util_format_is_pure_uint(format)) {
141 value.red = color.uint32[0];
142 value.green = color.uint32[1];
143 value.blue = color.uint32[2];
144 value.alpha = color.uint32[3];
145 } else {
146 value.red = color.float32[0];
147 value.green = color.float32[1];
148 value.blue = color.float32[2];
149 value.alpha = color.float32[3];
150 }
151
152 /* Apply swizzle to color since Metal does not allow swizzle for renderable
153 * textures, but we need to support that for formats like
154 * VK_FORMAT_B4G4R4A4_UNORM_PACK16 */
155 const struct kk_va_format *supported_format = kk_get_va_format(format);
156 struct mtl_clear_color swizzled_color;
157 for (uint32_t i = 0u; i < 4; ++i)
158 swizzled_color.channel[supported_format->unswizzle.channels[i]] =
159 value.channel[i];
160
161 return swizzled_color;
162 }
163
164 VKAPI_ATTR void VKAPI_CALL
165 kk_CmdBeginRendering(VkCommandBuffer commandBuffer,
166 const VkRenderingInfo *pRenderingInfo)
167 {
168 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
169 struct kk_device *dev = kk_cmd_buffer_device(cmd);
170
171 struct kk_rendering_state *render = &cmd->state.gfx.render;
172
173 memset(render, 0, sizeof(*render));
174
175 render->flags = pRenderingInfo->flags;
176 render->area = pRenderingInfo->renderArea;
177 render->view_mask = pRenderingInfo->viewMask;
178 render->layer_count = pRenderingInfo->layerCount;
179 render->samples = 0;
180 render->color_att_count = pRenderingInfo->colorAttachmentCount;
181
182 const uint32_t layer_count = render->view_mask
183 ? util_last_bit(render->view_mask)
184 : render->layer_count;
185
186 VkExtent2D framebuffer_extent = {.width = 0u, .height = 0u};
187 bool does_any_attachment_clear = false;
188 for (uint32_t i = 0; i < render->color_att_count; i++) {
189 kk_attachment_init(&render->color_att[i],
190 &pRenderingInfo->pColorAttachments[i]);
191 kk_merge_render_iview(&framebuffer_extent, render->color_att[i].iview);
192 does_any_attachment_clear |=
193 (pRenderingInfo->pColorAttachments[i].loadOp ==
194 VK_ATTACHMENT_LOAD_OP_CLEAR);
195 }
196 if (pRenderingInfo->pDepthAttachment)
197 does_any_attachment_clear |= (pRenderingInfo->pDepthAttachment->loadOp ==
198 VK_ATTACHMENT_LOAD_OP_CLEAR);
199 if (pRenderingInfo->pStencilAttachment)
200 does_any_attachment_clear |=
201 (pRenderingInfo->pStencilAttachment->loadOp ==
202 VK_ATTACHMENT_LOAD_OP_CLEAR);
203
204 kk_attachment_init(&render->depth_att, pRenderingInfo->pDepthAttachment);
205 kk_attachment_init(&render->stencil_att, pRenderingInfo->pStencilAttachment);
206 kk_merge_render_iview(&framebuffer_extent,
207 render->depth_att.iview ?: render->stencil_att.iview);
208
209 const VkRenderingFragmentShadingRateAttachmentInfoKHR *fsr_att_info =
210 vk_find_struct_const(pRenderingInfo->pNext,
211 RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_INFO_KHR);
212 if (fsr_att_info != NULL && fsr_att_info->imageView != VK_NULL_HANDLE) {
213 VK_FROM_HANDLE(kk_image_view, iview, fsr_att_info->imageView);
214 render->fsr_att = (struct kk_attachment){
215 .vk_format = iview->vk.format,
216 .iview = iview,
217 .store_op = VK_ATTACHMENT_STORE_OP_NONE,
218 };
219 }
220
221 const VkRenderingAttachmentLocationInfoKHR ral_info = {
222 .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_LOCATION_INFO_KHR,
223 .colorAttachmentCount = pRenderingInfo->colorAttachmentCount,
224 };
225 vk_cmd_set_rendering_attachment_locations(&cmd->vk, &ral_info);
226
227 kk_cmd_buffer_dirty_render_pass(cmd);
228 mtl_render_pass_descriptor *pass_descriptor =
229 mtl_new_render_pass_descriptor();
230
231 /* Framebufferless rendering, need to set pass_descriptors
232 * renderTargetWidth/Height to non-0 values and defaultRasterSampleCount.
233 * However, since the sample count will only be known at first pipeline bind,
234 * we need to delay the start of the pass until then since Metal will ignore
235 * bound pipeline's sample count. */
236 bool no_framebuffer =
237 framebuffer_extent.width == 0u && framebuffer_extent.height == 0u;
238 if (no_framebuffer) {
239 framebuffer_extent.width = render->area.extent.width;
240 framebuffer_extent.height = render->area.extent.height;
241 mtl_render_pass_descriptor_set_render_target_width(
242 pass_descriptor, framebuffer_extent.width);
243 mtl_render_pass_descriptor_set_render_target_height(
244 pass_descriptor, framebuffer_extent.height);
245 mtl_render_pass_descriptor_set_default_raster_sample_count(
246 pass_descriptor, 1u);
247 }
248
249 /* Check if we are rendering to the whole framebuffer. Required to understand
250 * if we need to load to avoid clearing all attachment when loading.
251 */
252 bool is_whole_framebuffer =
253 framebuffer_extent.width == render->area.extent.width &&
254 framebuffer_extent.height == render->area.extent.height &&
255 render->area.offset.x == 0u && render->area.offset.y == 0u &&
256 (render->view_mask == 0u ||
257 render->view_mask == BITFIELD64_MASK(render->layer_count));
258
259 /* Understand if the render area is tile aligned so we know if we actually
260 * need to load the tile to not lose information. */
261 uint32_t tile_alignment = 31u;
262 bool is_tile_aligned = !(render->area.offset.x & tile_alignment) &&
263 !(render->area.offset.y & tile_alignment) &&
264 !(render->area.extent.width & tile_alignment) &&
265 !(render->area.extent.height & tile_alignment);
266
267 /* Rendering to the whole framebuffer */
268 is_tile_aligned |= is_whole_framebuffer;
269
270 /* There are 3 cases where we need to force a load instead of using the user
271 * defined load operation:
272 * 1. Render area is not tile aligned
273 * 2. Load operation is clear but doesn't render to the whole attachment
274 * 3. Resuming renderpass
275 */
276 bool force_attachment_load =
277 !is_tile_aligned ||
278 (!is_whole_framebuffer && does_any_attachment_clear) ||
279 (render->flags & VK_RENDERING_RESUMING_BIT);
280
281 for (uint32_t i = 0; i < render->color_att_count; i++) {
282 const struct kk_image_view *iview = render->color_att[i].iview;
283 if (!iview)
284 continue;
285
286 assert(iview->plane_count ==
287 1); /* TODO_KOSMICKRISP Handle multiplanar images? */
288 const struct kk_image *image =
289 container_of(iview->vk.image, struct kk_image, vk);
290 render->samples = image->vk.samples;
291
292 mtl_render_pass_attachment_descriptor *attachment_descriptor =
293 mtl_render_pass_descriptor_get_color_attachment(pass_descriptor, i);
294 kk_fill_common_attachment_description(
295 attachment_descriptor, iview, &pRenderingInfo->pColorAttachments[i],
296 force_attachment_load);
297 struct mtl_clear_color clear_color =
298 vk_clear_color_value_to_mtl_clear_color(
299 pRenderingInfo->pColorAttachments[i].clearValue.color,
300 iview->planes[0].format);
301 mtl_render_pass_attachment_descriptor_set_clear_color(
302 attachment_descriptor, clear_color);
303 }
304
305 if (render->depth_att.iview) {
306 const struct kk_image_view *iview = render->depth_att.iview;
307 const struct kk_image *image =
308 container_of(iview->vk.image, struct kk_image, vk);
309 render->samples = image->vk.samples;
310
311 mtl_render_pass_attachment_descriptor *attachment_descriptor =
312 mtl_render_pass_descriptor_get_depth_attachment(pass_descriptor);
313 kk_fill_common_attachment_description(
314 attachment_descriptor, render->depth_att.iview,
315 pRenderingInfo->pDepthAttachment, force_attachment_load);
316
317 /* clearValue.depthStencil.depth could have invalid values such as NaN
318 * which will trigger a Metal validation error. Ensure we only use this
319 * value if the attachment is actually cleared. */
320 if (pRenderingInfo->pDepthAttachment->loadOp ==
321 VK_ATTACHMENT_LOAD_OP_CLEAR)
322 mtl_render_pass_attachment_descriptor_set_clear_depth(
323 attachment_descriptor,
324 pRenderingInfo->pDepthAttachment->clearValue.depthStencil.depth);
325 }
326 if (render->stencil_att.iview) {
327 const struct kk_image_view *iview = render->stencil_att.iview;
328 const struct kk_image *image =
329 container_of(iview->vk.image, struct kk_image, vk);
330 render->samples = image->vk.samples;
331
332 mtl_render_pass_attachment_descriptor *attachment_descriptor =
333 mtl_render_pass_descriptor_get_stencil_attachment(pass_descriptor);
334 kk_fill_common_attachment_description(
335 attachment_descriptor, render->stencil_att.iview,
336 pRenderingInfo->pStencilAttachment, force_attachment_load);
337 mtl_render_pass_attachment_descriptor_set_clear_stencil(
338 attachment_descriptor,
339 pRenderingInfo->pStencilAttachment->clearValue.depthStencil.stencil);
340 }
341
342 /* Render targets are always arrays */
343 mtl_render_pass_descriptor_set_render_target_array_length(
344 pass_descriptor, layer_count ? layer_count : 1u);
345
346 /* Set global visibility buffer */
347 mtl_render_pass_descriptor_set_visibility_buffer(
348 pass_descriptor, dev->occlusion_queries.bo->map);
349
350 // TODO_KOSMICKRISP Fragment shading rate support goes here if Metal supports
351 // it
352
353 /* Rendering with no attachments requires pushing the start of the render
354 * pass to first pipeline binding to know sample count. */
355 if (!no_framebuffer)
356 kk_encoder_start_render(cmd, pass_descriptor, render->view_mask);
357
358 /* Store descriptor in case we need to restart the pass at pipeline barrier,
359 * but force loads */
360 for (uint32_t i = 0; i < render->color_att_count; i++) {
361 const struct kk_image_view *iview = render->color_att[i].iview;
362 if (!iview)
363 continue;
364 mtl_render_pass_attachment_descriptor *attachment_descriptor =
365 mtl_render_pass_descriptor_get_color_attachment(pass_descriptor, i);
366 mtl_render_pass_attachment_descriptor_set_load_action(
367 attachment_descriptor, MTL_LOAD_ACTION_LOAD);
368 }
369 if (render->depth_att.iview) {
370 mtl_render_pass_attachment_descriptor *attachment_descriptor =
371 mtl_render_pass_descriptor_get_depth_attachment(pass_descriptor);
372 mtl_render_pass_attachment_descriptor_set_load_action(
373 attachment_descriptor, MTL_LOAD_ACTION_LOAD);
374 }
375 if (render->stencil_att.iview) {
376 mtl_render_pass_attachment_descriptor *attachment_descriptor =
377 mtl_render_pass_descriptor_get_stencil_attachment(pass_descriptor);
378 mtl_render_pass_attachment_descriptor_set_load_action(
379 attachment_descriptor, MTL_LOAD_ACTION_LOAD);
380 }
381 cmd->state.gfx.render_pass_descriptor = pass_descriptor;
382 cmd->state.gfx.need_to_start_render_pass = no_framebuffer;
383
384 kk_cmd_buffer_dirty_all_gfx(cmd);
385
386 if (render->flags & VK_RENDERING_RESUMING_BIT)
387 return;
388
389 /* Clear attachments if we forced a load and there's a clear */
390 if (!force_attachment_load || !does_any_attachment_clear)
391 return;
392
393 uint32_t clear_count = 0;
394 VkClearAttachment clear_att[KK_MAX_RTS + 1];
395 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) {
396 const VkRenderingAttachmentInfo *att_info =
397 &pRenderingInfo->pColorAttachments[i];
398 if (att_info->imageView == VK_NULL_HANDLE ||
399 att_info->loadOp != VK_ATTACHMENT_LOAD_OP_CLEAR)
400 continue;
401
402 clear_att[clear_count++] = (VkClearAttachment){
403 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
404 .colorAttachment = i,
405 .clearValue = att_info->clearValue,
406 };
407 }
408
409 clear_att[clear_count] = (VkClearAttachment){
410 .aspectMask = 0,
411 };
412 if (pRenderingInfo->pDepthAttachment != NULL &&
413 pRenderingInfo->pDepthAttachment->imageView != VK_NULL_HANDLE &&
414 pRenderingInfo->pDepthAttachment->loadOp ==
415 VK_ATTACHMENT_LOAD_OP_CLEAR) {
416 clear_att[clear_count].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
417 clear_att[clear_count].clearValue.depthStencil.depth =
418 pRenderingInfo->pDepthAttachment->clearValue.depthStencil.depth;
419 }
420 if (pRenderingInfo->pStencilAttachment != NULL &&
421 pRenderingInfo->pStencilAttachment->imageView != VK_NULL_HANDLE &&
422 pRenderingInfo->pStencilAttachment->loadOp ==
423 VK_ATTACHMENT_LOAD_OP_CLEAR) {
424 clear_att[clear_count].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
425 clear_att[clear_count].clearValue.depthStencil.stencil =
426 pRenderingInfo->pStencilAttachment->clearValue.depthStencil.stencil;
427 }
428 if (clear_att[clear_count].aspectMask != 0)
429 clear_count++;
430
431 if (clear_count > 0) {
432 const VkClearRect clear_rect = {
433 .rect = render->area,
434 .baseArrayLayer = 0,
435 .layerCount = render->view_mask ? 1 : render->layer_count,
436 };
437
438 kk_CmdClearAttachments(kk_cmd_buffer_to_handle(cmd), clear_count,
439 clear_att, 1, &clear_rect);
440 }
441 }
442
443 VKAPI_ATTR void VKAPI_CALL
444 kk_CmdEndRendering(VkCommandBuffer commandBuffer)
445 {
446 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
447 struct kk_rendering_state *render = &cmd->state.gfx.render;
448 bool need_resolve = false;
449
450 /* Translate render state back to VK for meta */
451 VkRenderingAttachmentInfo vk_color_att[KK_MAX_RTS];
452 for (uint32_t i = 0; i < render->color_att_count; i++) {
453 if (render->color_att[i].resolve_mode != VK_RESOLVE_MODE_NONE)
454 need_resolve = true;
455
456 vk_color_att[i] = (VkRenderingAttachmentInfo){
457 .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
458 .imageView = kk_image_view_to_handle(render->color_att[i].iview),
459 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
460 .resolveMode = render->color_att[i].resolve_mode,
461 .resolveImageView =
462 kk_image_view_to_handle(render->color_att[i].resolve_iview),
463 .resolveImageLayout = VK_IMAGE_LAYOUT_GENERAL,
464 };
465 }
466
467 const VkRenderingAttachmentInfo vk_depth_att = {
468 .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
469 .imageView = kk_image_view_to_handle(render->depth_att.iview),
470 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
471 .resolveMode = render->depth_att.resolve_mode,
472 .resolveImageView =
473 kk_image_view_to_handle(render->depth_att.resolve_iview),
474 .resolveImageLayout = VK_IMAGE_LAYOUT_GENERAL,
475 };
476 if (render->depth_att.resolve_mode != VK_RESOLVE_MODE_NONE)
477 need_resolve = true;
478
479 const VkRenderingAttachmentInfo vk_stencil_att = {
480 .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
481 .imageView = kk_image_view_to_handle(render->stencil_att.iview),
482 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
483 .resolveMode = render->stencil_att.resolve_mode,
484 .resolveImageView =
485 kk_image_view_to_handle(render->stencil_att.resolve_iview),
486 .resolveImageLayout = VK_IMAGE_LAYOUT_GENERAL,
487 };
488 if (render->stencil_att.resolve_mode != VK_RESOLVE_MODE_NONE)
489 need_resolve = true;
490
491 const VkRenderingInfo vk_render = {
492 .sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
493 .renderArea = render->area,
494 .layerCount = render->layer_count,
495 .viewMask = render->view_mask,
496 .colorAttachmentCount = render->color_att_count,
497 .pColorAttachments = vk_color_att,
498 .pDepthAttachment = &vk_depth_att,
499 .pStencilAttachment = &vk_stencil_att,
500 };
501
502 /* Clean up previous encoder */
503 kk_encoder_signal_fence_and_end(cmd);
504 mtl_release(cmd->state.gfx.render_pass_descriptor);
505 cmd->state.gfx.render_pass_descriptor = NULL;
506
507 if (render->flags & VK_RENDERING_SUSPENDING_BIT)
508 need_resolve = false;
509
510 memset(render, 0, sizeof(*render));
511
512 if (need_resolve) {
513 kk_meta_resolve_rendering(cmd, &vk_render);
514 }
515 }
516
517 VKAPI_ATTR void VKAPI_CALL
518 kk_CmdBindIndexBuffer2KHR(VkCommandBuffer commandBuffer, VkBuffer _buffer,
519 VkDeviceSize offset, VkDeviceSize size,
520 VkIndexType indexType)
521 {
522 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
523 VK_FROM_HANDLE(kk_buffer, buffer, _buffer);
524
525 cmd->state.gfx.index.handle = buffer->mtl_handle;
526 cmd->state.gfx.index.size = size;
527 cmd->state.gfx.index.offset = offset;
528 cmd->state.gfx.index.bytes_per_index = vk_index_type_to_bytes(indexType);
529 cmd->state.gfx.index.restart = vk_index_to_restart(indexType);
530 }
531
532 VKAPI_ATTR void VKAPI_CALL
533 kk_CmdBindVertexBuffers2(VkCommandBuffer commandBuffer, uint32_t firstBinding,
534 uint32_t bindingCount, const VkBuffer *pBuffers,
535 const VkDeviceSize *pOffsets,
536 const VkDeviceSize *pSizes,
537 const VkDeviceSize *pStrides)
538 {
539 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
540
541 if (pStrides) {
542 vk_cmd_set_vertex_binding_strides(&cmd->vk, firstBinding, bindingCount,
543 pStrides);
544 }
545
546 for (uint32_t i = 0; i < bindingCount; i++) {
547 VK_FROM_HANDLE(kk_buffer, buffer, pBuffers[i]);
548 uint32_t idx = firstBinding + i;
549 uint64_t size = pSizes ? pSizes[i] : VK_WHOLE_SIZE;
550 const struct kk_addr_range addr_range =
551 kk_buffer_addr_range(buffer, pOffsets[i], size);
552 cmd->state.gfx.vb.addr_range[idx] = addr_range;
553 cmd->state.gfx.vb.handles[idx] = buffer->mtl_handle;
554 cmd->state.gfx.dirty |= KK_DIRTY_VB;
555 }
556 }
557
558 static void
559 kk_flush_vp_state(struct kk_cmd_buffer *cmd)
560 {
561 const struct vk_dynamic_graphics_state *dyn =
562 &cmd->vk.dynamic_graphics_state;
563
564 /* We always need at least 1 viewport for the hardware. With rasterizer
565 * discard the app may not supply any, but we can just program garbage.
566 */
567 unsigned count = MAX2(dyn->vp.scissor_count, 1);
568
569 /* Need to clamp scissor rectangles to render area, otherwise Metal doesn't
570 * like it */
571 struct mtl_scissor_rect rects[KK_MAX_VIEWPORTS] = {0};
572 VkOffset2D origin = cmd->state.gfx.render.area.offset;
573 VkOffset2D end = {.x = origin.x + cmd->state.gfx.render.area.extent.width,
574 .y = origin.y + cmd->state.gfx.render.area.extent.height};
575 for (uint32_t i = 0; i < dyn->vp.scissor_count; i++) {
576 const VkRect2D *rect = &dyn->vp.scissors[i];
577
578 size_t x0 = CLAMP(rect->offset.x, origin.x, end.x);
579 size_t x1 = CLAMP(rect->offset.x + rect->extent.width, origin.x, end.x);
580 size_t y0 = CLAMP(rect->offset.y, origin.y, end.y);
581 size_t y1 = CLAMP(rect->offset.y + rect->extent.height, origin.y, end.y);
582 size_t minx = MIN2(x0, x1);
583 size_t miny = MIN2(y0, y1);
584 size_t maxx = MAX2(x0, x1);
585 size_t maxy = MAX2(y0, y1);
586 rects[i].x = minx;
587 rects[i].y = miny;
588 rects[i].width = maxx - minx;
589 rects[i].height = maxy - miny;
590 }
591
592 mtl_set_scissor_rects(kk_render_encoder(cmd), rects, count);
593
594 count = MAX2(dyn->vp.viewport_count, 1);
595
596 struct mtl_viewport viewports[KK_MAX_VIEWPORTS] = {0};
597
598 /* NDC in Metal is pointing downwards. Vulkan is pointing upwards. Account
599 * for that here */
600 for (uint32_t i = 0; i < dyn->vp.viewport_count; i++) {
601 const VkViewport *vp = &dyn->vp.viewports[i];
602
603 viewports[i].originX = vp->x;
604 viewports[i].originY = vp->y + vp->height;
605 viewports[i].width = vp->width;
606 viewports[i].height = -vp->height;
607
608 viewports[i].znear = vp->minDepth;
609 viewports[i].zfar = vp->maxDepth;
610 }
611
612 mtl_set_viewports(kk_render_encoder(cmd), viewports, count);
613 }
614
615 static inline uint32_t
616 kk_calculate_vbo_clamp(uint64_t vbuf, uint64_t sink, enum pipe_format format,
617 uint32_t size_B, uint32_t stride_B, uint32_t offset_B,
618 uint64_t *vbuf_out)
619 {
620 unsigned elsize_B = util_format_get_blocksize(format);
621 unsigned subtracted_B = offset_B + elsize_B;
622
623 /* If at least one index is valid, determine the max. Otherwise, direct reads
624 * to zero.
625 */
626 if (size_B >= subtracted_B) {
627 *vbuf_out = vbuf + offset_B;
628
629 /* If stride is zero, do not clamp, everything is valid. */
630 if (stride_B)
631 return ((size_B - subtracted_B) / stride_B);
632 else
633 return UINT32_MAX;
634 } else {
635 *vbuf_out = sink;
636 return 0;
637 }
638 }
639
640 static void
641 set_empty_scissor(mtl_render_encoder *enc)
642 {
643 struct mtl_scissor_rect rect = {.x = 0u, .y = 0u, .width = 0u, .height = 0u};
644 mtl_set_scissor_rects(enc, &rect, 1);
645 }
646
647 #define IS_DIRTY(bit) BITSET_TEST(dyn->dirty, MESA_VK_DYNAMIC_##bit)
648 #define IS_SHADER_DIRTY(bit) \
649 (cmd->state.dirty_shaders & BITFIELD_BIT(MESA_SHADER_##bit))
650
651 static void
652 kk_flush_pipeline(struct kk_cmd_buffer *cmd)
653 {
654 struct kk_device *device = kk_cmd_buffer_device(cmd);
655 mtl_render_encoder *enc = kk_render_encoder(cmd);
656 struct kk_graphics_state *gfx = &cmd->state.gfx;
657 struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state;
658
659 /* Depth/stencil state may be dynamic, handle it as part of the pipeline. */
660 if (cmd->state.gfx.is_depth_stencil_dynamic &&
661 (cmd->state.gfx.render.depth_att.vk_format != VK_FORMAT_UNDEFINED ||
662 cmd->state.gfx.render.stencil_att.vk_format != VK_FORMAT_UNDEFINED) &&
663 (IS_DIRTY(DS_DEPTH_TEST_ENABLE) | IS_DIRTY(DS_DEPTH_WRITE_ENABLE) |
664 IS_DIRTY(DS_DEPTH_COMPARE_OP) | IS_DIRTY(DS_STENCIL_TEST_ENABLE) |
665 IS_DIRTY(DS_STENCIL_OP) | IS_DIRTY(DS_STENCIL_COMPARE_MASK) |
666 IS_DIRTY(DS_STENCIL_WRITE_MASK))) {
667 kk_cmd_release_dynamic_ds_state(cmd);
668
669 bool has_depth = dyn->rp.attachments & MESA_VK_RP_ATTACHMENT_DEPTH_BIT;
670 bool has_stencil =
671 dyn->rp.attachments & MESA_VK_RP_ATTACHMENT_STENCIL_BIT;
672 gfx->depth_stencil_state = kk_compile_depth_stencil_state(
673 device, &dyn->ds, has_depth, has_stencil);
674 mtl_set_depth_stencil_state(enc, gfx->depth_stencil_state);
675 }
676
677 if (IS_SHADER_DIRTY(VERTEX)) {
678 struct kk_shader *vs = cmd->state.shaders[MESA_SHADER_VERTEX];
679 mtl_render_set_pipeline_state(enc, vs->pipeline.gfx.handle);
680 if (gfx->depth_stencil_state)
681 mtl_set_depth_stencil_state(enc, gfx->depth_stencil_state);
682 }
683 }
684
685 static void
686 kk_flush_dynamic_state(struct kk_cmd_buffer *cmd)
687 {
688 struct kk_graphics_state *gfx = &cmd->state.gfx;
689 struct kk_descriptor_state *desc = &gfx->descriptors;
690 struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state;
691 mtl_render_encoder *enc = kk_render_encoder(cmd);
692
693 if (IS_DIRTY(VI_BINDING_STRIDES)) {
694 u_foreach_bit(ndx, dyn->vi->bindings_valid) {
695 desc->root.draw.buffer_strides[ndx] = dyn->vi_binding_strides[ndx];
696 }
697 desc->root_dirty = true;
698 }
699
700 if (IS_DIRTY(RS_RASTERIZER_DISCARD_ENABLE)) {
701 if (dyn->rs.rasterizer_discard_enable) {
702 set_empty_scissor(enc);
703 } else {
704 /* Enforce setting the correct scissors */
705 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_VP_VIEWPORT_COUNT);
706 }
707 }
708
709 if (IS_DIRTY(RS_CULL_MODE)) {
710 gfx->is_cull_front_and_back =
711 dyn->rs.cull_mode == VK_CULL_MODE_FRONT_AND_BACK;
712 if (gfx->is_cull_front_and_back) {
713 set_empty_scissor(enc);
714 } else {
715 mtl_set_cull_mode(enc,
716 vk_front_face_to_mtl_cull_mode(dyn->rs.cull_mode));
717 /* Enforce setting the correct scissors */
718 BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_VP_VIEWPORT_COUNT);
719 }
720 }
721
722 /* We enable raster discard by setting scissor to size (0, 0) */
723 if (!(dyn->rs.rasterizer_discard_enable || gfx->is_cull_front_and_back) &&
724 (IS_DIRTY(VP_VIEWPORT_COUNT) || IS_DIRTY(VP_VIEWPORTS) ||
725 IS_DIRTY(VP_SCISSOR_COUNT) || IS_DIRTY(VP_SCISSORS)))
726 kk_flush_vp_state(cmd);
727
728 if (IS_DIRTY(RS_FRONT_FACE)) {
729 mtl_set_front_face_winding(
730 enc, vk_front_face_to_mtl_winding(
731 cmd->vk.dynamic_graphics_state.rs.front_face));
732 }
733
734 if (IS_DIRTY(RS_DEPTH_BIAS_FACTORS) || IS_DIRTY(RS_DEPTH_BIAS_ENABLE)) {
735 if (dyn->rs.depth_bias.enable)
736 mtl_set_depth_bias(enc, dyn->rs.depth_bias.constant_factor,
737 dyn->rs.depth_bias.slope_factor,
738 dyn->rs.depth_bias.clamp);
739 else
740 mtl_set_depth_bias(enc, 0.0f, 0.0f, 0.0f);
741 }
742
743 if (IS_DIRTY(RS_DEPTH_CLAMP_ENABLE)) {
744 enum mtl_depth_clip_mode mode = dyn->rs.depth_clamp_enable
745 ? MTL_DEPTH_CLIP_MODE_CLAMP
746 : MTL_DEPTH_CLIP_MODE_CLIP;
747 mtl_set_depth_clip_mode(enc, mode);
748 }
749
750 if (IS_DIRTY(DS_STENCIL_REFERENCE))
751 mtl_set_stencil_references(
752 enc, cmd->vk.dynamic_graphics_state.ds.stencil.front.reference,
753 cmd->vk.dynamic_graphics_state.ds.stencil.back.reference);
754
755 if (IS_DIRTY(CB_BLEND_CONSTANTS)) {
756 static_assert(sizeof(desc->root.draw.blend_constant) ==
757 sizeof(dyn->cb.blend_constants),
758 "common size");
759
760 memcpy(desc->root.draw.blend_constant, dyn->cb.blend_constants,
761 sizeof(dyn->cb.blend_constants));
762 desc->root_dirty = true;
763 }
764
765 if (IS_DIRTY(VI) || IS_DIRTY(VI_BINDINGS_VALID) ||
766 IS_DIRTY(VI_BINDING_STRIDES) || gfx->dirty & KK_DIRTY_VB) {
767 struct kk_shader *vs = cmd->state.shaders[MESA_SHADER_VERTEX];
768 unsigned slot = 0;
769 cmd->state.gfx.vb.max_vertices = 0u;
770 u_foreach_bit(i, vs->info.vs.attribs_read) {
771 if (dyn->vi->attributes_valid & BITFIELD_BIT(i)) {
772 struct vk_vertex_attribute_state attr = dyn->vi->attributes[i];
773 struct kk_addr_range vb = gfx->vb.addr_range[attr.binding];
774
775 desc->root.draw.attrib_clamps[slot] = kk_calculate_vbo_clamp(
776 vb.addr, 0, vk_format_to_pipe_format(attr.format), vb.range,
777 dyn->vi_binding_strides[attr.binding], attr.offset,
778 &desc->root.draw.attrib_base[slot]);
779 desc->root.draw.buffer_strides[attr.binding] =
780 dyn->vi_binding_strides[attr.binding];
781
782 cmd->state.gfx.vb.max_vertices =
783 MAX2(vb.range / dyn->vi_binding_strides[attr.binding],
784 cmd->state.gfx.vb.max_vertices);
785 }
786 slot++;
787 }
788 desc->root_dirty = true;
789 }
790
791 if (desc->root_dirty)
792 kk_upload_descriptor_root(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS);
793
794 struct kk_bo *root_buffer = desc->root.root_buffer;
795 if (root_buffer) {
796 mtl_set_vertex_buffer(enc, root_buffer->map, 0, 0);
797 mtl_set_fragment_buffer(enc, root_buffer->map, 0, 0);
798 }
799
800 if (gfx->dirty & KK_DIRTY_OCCLUSION) {
801 mtl_set_visibility_result_mode(enc, gfx->occlusion.mode,
802 gfx->occlusion.index * sizeof(uint64_t));
803 }
804 }
805
806 static void
807 kk_flush_gfx_state(struct kk_cmd_buffer *cmd)
808 {
809 struct kk_graphics_state *gfx = &cmd->state.gfx;
810 struct kk_descriptor_state *desc = &gfx->descriptors;
811
812 kk_flush_pipeline(cmd);
813
814 if (desc->push_dirty)
815 kk_cmd_buffer_flush_push_descriptors(cmd, desc);
816
817 kk_flush_dynamic_state(cmd);
818
819 cmd->state.dirty_shaders = 0u;
820 cmd->state.gfx.dirty = 0u;
821 vk_dynamic_graphics_state_clear_dirty(&cmd->vk.dynamic_graphics_state);
822 }
823 #undef IS_SHADER_DIRTY
824 #undef IS_DIRTY
825
826 struct kk_draw_data {
827 union {
828 /* Vertex/index count and instance count. */
829 uint32_t count[2];
830 mtl_buffer *indirect_buffer;
831 };
832 mtl_buffer *index_buffer;
833 uint64_t index_buffer_offset;
834 uint64_t indirect_buffer_offset;
835 uint32_t index_buffer_range_B;
836 uint32_t first_index;
837 uint32_t first_vertex;
838 uint32_t first_instance;
839 enum mesa_prim prim;
840 uint8_t index_size;
841 bool indirect;
842 bool indexed;
843 bool restart;
844 };
845
846 /* Unrolling will always be done through indirect rendering, so if this is
847 * called from non-indirect calls, we will fake it. */
848 static struct kk_draw_data
849 kk_unroll_geometry(struct kk_cmd_buffer *cmd, struct kk_draw_data data,
850 bool promote_index_type)
851 {
852 if (!data.indirect) {
853 if (data.indexed) {
854 VkDrawIndexedIndirectCommand draw = {
855 .indexCount = data.count[0],
856 .instanceCount = data.count[1],
857 .firstIndex = data.first_index,
858 .vertexOffset = data.first_vertex,
859 .firstInstance = data.first_instance,
860 };
861
862 data.indirect_buffer =
863 kk_pool_upload(cmd, &draw, sizeof(draw), 4u).handle;
864 data.indirect = true;
865 } else {
866 VkDrawIndirectCommand draw = {
867 .vertexCount = data.count[0],
868 .instanceCount = data.count[1],
869 .firstVertex = data.first_vertex,
870 .firstInstance = data.first_instance,
871 };
872 data.indirect_buffer =
873 kk_pool_upload(cmd, &draw, sizeof(draw), 4u).handle;
874 data.indirect = true;
875 }
876 }
877
878 uint32_t el_count = cmd->state.gfx.vb.max_vertices;
879 if (data.indexed) {
880 el_count =
881 (mtl_buffer_get_length(data.index_buffer) - data.index_buffer_offset) /
882 data.index_size;
883 }
884
885 uint32_t decomposed_index_count =
886 u_decomposed_prims_for_vertices(data.prim, el_count) *
887 mesa_vertices_per_prim(data.prim);
888 uint32_t el_size_B = 4u;
889 uint32_t index_buffer_size_B = decomposed_index_count * el_size_B;
890 uint32_t buffer_size_B =
891 sizeof(VkDrawIndexedIndirectCommand) + index_buffer_size_B;
892 struct kk_bo *index_buffer =
893 kk_cmd_allocate_buffer(cmd, buffer_size_B, el_size_B);
894
895 if (!index_buffer)
896 return data;
897
898 struct libkk_unroll_geometry_and_restart_args info = {
899 .index_buffer = mtl_buffer_get_gpu_address(data.index_buffer) +
900 data.index_buffer_offset,
901 .out_ptr = index_buffer->gpu + sizeof(VkDrawIndexedIndirectCommand),
902 .in_draw = mtl_buffer_get_gpu_address(data.indirect_buffer) +
903 data.indirect_buffer_offset,
904 .out_draw = index_buffer->gpu,
905 .restart_index =
906 promote_index_type ? UINT32_MAX : cmd->state.gfx.index.restart,
907 .index_buffer_size_el = data.index_buffer_range_B,
908 .in_el_size_B = data.index_size,
909 .out_el_size_B = el_size_B,
910 .flatshade_first = true,
911 .mode = data.prim,
912 };
913
914 struct mtl_size grid = {1, 1, 1};
915 libkk_unroll_geometry_and_restart_struct(cmd, grid, true, info);
916
917 data.indirect_buffer = index_buffer->map;
918 data.index_buffer = index_buffer->map;
919 data.index_buffer_offset = sizeof(VkDrawIndexedIndirectCommand);
920 data.indirect_buffer_offset = 0u;
921 data.index_buffer_range_B = index_buffer_size_B;
922 data.first_index = 0u;
923 data.prim = u_decomposed_prim(data.prim);
924 data.index_size = el_size_B;
925 data.indirect = true;
926 data.indexed = true;
927 data.restart = false;
928 return data;
929 }
930
931 static enum mtl_primitive_type
932 mesa_prim_to_mtl_primitive_type(enum mesa_prim prim)
933 {
934 switch (prim) {
935 case MESA_PRIM_POINTS:
936 return MTL_PRIMITIVE_TYPE_POINT;
937 case MESA_PRIM_LINES:
938 return MTL_PRIMITIVE_TYPE_LINE;
939 case MESA_PRIM_LINE_STRIP:
940 return MTL_PRIMITIVE_TYPE_LINE_STRIP;
941 case MESA_PRIM_TRIANGLES:
942 return MTL_PRIMITIVE_TYPE_TRIANGLE;
943 case MESA_PRIM_TRIANGLE_STRIP:
944 return MTL_PRIMITIVE_TYPE_TRIANGLE_STRIP;
945 default:
946 UNREACHABLE("Unsupported primitive type");
947 }
948 }
949
950 static void
951 kk_dispatch_draw(struct kk_cmd_buffer *cmd, struct kk_draw_data data)
952 {
953 mtl_render_encoder *enc = kk_render_encoder(cmd);
954 enum mtl_primitive_type primitive_type =
955 mesa_prim_to_mtl_primitive_type(data.prim);
956 if (data.indirect) {
957 if (data.indexed) {
958 enum mtl_index_type index_type =
959 index_size_in_bytes_to_mtl_index_type(data.index_size);
960 mtl_draw_indexed_primitives_indirect(
961 enc, primitive_type, index_type, data.index_buffer,
962 data.index_buffer_offset, data.indirect_buffer,
963 data.indirect_buffer_offset);
964 } else {
965 mtl_draw_primitives_indirect(enc, primitive_type, data.indirect_buffer,
966 data.indirect_buffer_offset);
967 }
968 } else {
969 if (data.indexed) {
970 enum mtl_index_type index_type =
971 index_size_in_bytes_to_mtl_index_type(data.index_size);
972 uint32_t index_buffer_offset =
973 data.first_index * data.index_size + data.index_buffer_offset;
974
975 mtl_render_encoder *enc = kk_render_encoder(cmd);
976 mtl_draw_indexed_primitives(enc, primitive_type, data.count[0],
977 index_type, cmd->state.gfx.index.handle,
978 index_buffer_offset, data.count[1],
979 data.first_vertex, data.first_instance);
980 } else {
981 mtl_draw_primitives(enc, primitive_type, data.first_vertex,
982 data.count[0], data.count[1], data.first_instance);
983 }
984 }
985 }
986
987 static bool
988 requires_index_promotion(struct kk_cmd_buffer *cmd)
989 {
990 const struct vk_dynamic_graphics_state *dyn =
991 &cmd->vk.dynamic_graphics_state;
992 switch (dyn->ia.primitive_topology) {
993 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
994 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
995 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
996 return (!dyn->ia.primitive_restart_enable &&
997 cmd->state.gfx.index.bytes_per_index < sizeof(uint32_t));
998 default:
999 return false;
1000 }
1001 }
1002
1003 static void
1004 kk_draw(struct kk_cmd_buffer *cmd, struct kk_draw_data data)
1005 {
1006 /* Filter trivial calls. */
1007 if (!data.indirect && (data.count[0] == 0u || data.count[1] == 0u))
1008 return;
1009
1010 kk_flush_gfx_state(cmd);
1011
1012 /* If the restart bool is set, it means that primitive restart is disabled
1013 * but index type is not uint32_t which requires promoting the type to
1014 * uint32_t since we cannot disable primitive restart in Metal. */
1015 bool promote_index_type = requires_index_promotion(cmd);
1016
1017 /* We always need to unroll triangle fans. */
1018 data.restart = (data.prim == MESA_PRIM_TRIANGLE_FAN);
1019
1020 if (promote_index_type || data.restart)
1021 data = kk_unroll_geometry(cmd, data, promote_index_type);
1022
1023 kk_dispatch_draw(cmd, data);
1024 }
1025
1026 VKAPI_ATTR void VKAPI_CALL
1027 kk_CmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount,
1028 uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
1029 {
1030 /* Metal validation dislikes empty calls */
1031 if (instanceCount == 0 || vertexCount == 0)
1032 return;
1033
1034 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
1035
1036 const struct vk_dynamic_graphics_state *dyn =
1037 &cmd->vk.dynamic_graphics_state;
1038
1039 struct kk_draw_data data = {
1040 .count[0] = vertexCount,
1041 .count[1] = instanceCount,
1042 .first_vertex = firstVertex,
1043 .first_instance = firstInstance,
1044 .prim = vk_topology_to_mesa(dyn->ia.primitive_topology),
1045 };
1046
1047 kk_draw(cmd, data);
1048 }
1049
1050 VKAPI_ATTR void VKAPI_CALL
1051 kk_CmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount,
1052 uint32_t instanceCount, uint32_t firstIndex,
1053 int32_t vertexOffset, uint32_t firstInstance)
1054 {
1055 /* Metal validation dislikes empty calls */
1056 if (instanceCount == 0 || indexCount == 0)
1057 return;
1058
1059 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
1060
1061 const struct vk_dynamic_graphics_state *dyn =
1062 &cmd->vk.dynamic_graphics_state;
1063
1064 struct kk_draw_data data = {
1065 .count[0] = indexCount,
1066 .count[1] = instanceCount,
1067 .index_buffer = cmd->state.gfx.index.handle,
1068 .index_buffer_offset = cmd->state.gfx.index.offset,
1069 .index_buffer_range_B =
1070 cmd->state.gfx.index.size - cmd->state.gfx.index.offset,
1071 .first_index = firstIndex,
1072 .first_vertex = vertexOffset,
1073 .first_instance = firstInstance,
1074 .prim = vk_topology_to_mesa(dyn->ia.primitive_topology),
1075 .index_size = cmd->state.gfx.index.bytes_per_index,
1076 .indexed = true,
1077 };
1078
1079 kk_draw(cmd, data);
1080 }
1081
1082 VKAPI_ATTR void VKAPI_CALL
1083 kk_CmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer _buffer,
1084 VkDeviceSize offset, uint32_t drawCount, uint32_t stride)
1085 {
1086 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
1087 VK_FROM_HANDLE(kk_buffer, buffer, _buffer);
1088
1089 const struct vk_dynamic_graphics_state *dyn =
1090 &cmd->vk.dynamic_graphics_state;
1091
1092 for (uint32_t i = 0u; i < drawCount; ++i, offset += stride) {
1093 /* TODO_KOSMICKRISP
1094 * Move this to a separate buffer from the root so we don't have to upload
1095 * it every single loop. Pass it to the kk_draw call as a parameter that
1096 * will later be uploaded.
1097 */
1098 cmd->state.gfx.descriptors.root_dirty = true;
1099 cmd->state.gfx.descriptors.root.draw.draw_id = i;
1100
1101 struct kk_draw_data data = {
1102 .indirect_buffer = buffer->mtl_handle,
1103 .indirect_buffer_offset = offset,
1104 .prim = vk_topology_to_mesa(dyn->ia.primitive_topology),
1105 .indirect = true,
1106 };
1107
1108 kk_draw(cmd, data);
1109 }
1110 /* TODO_KOSMICKRISP Remove once above is done */
1111 cmd->state.gfx.descriptors.root_dirty = true;
1112 cmd->state.gfx.descriptors.root.draw.draw_id = 0;
1113 }
1114
1115 VKAPI_ATTR void VKAPI_CALL
1116 kk_CmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer _buffer,
1117 VkDeviceSize offset, uint32_t drawCount,
1118 uint32_t stride)
1119 {
1120 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
1121 VK_FROM_HANDLE(kk_buffer, buffer, _buffer);
1122
1123 const struct vk_dynamic_graphics_state *dyn =
1124 &cmd->vk.dynamic_graphics_state;
1125
1126 for (uint32_t i = 0u; i < drawCount; ++i, offset += stride) {
1127 /* TODO_KOSMICKRISP
1128 * Move this to a separate buffer from the root so we don't have to upload
1129 * it every single loop. Pass it to the kk_draw call as a parameter that
1130 * will later be uploaded.
1131 */
1132 cmd->state.gfx.descriptors.root_dirty = true;
1133 cmd->state.gfx.descriptors.root.draw.draw_id = i;
1134
1135 struct kk_draw_data data = {
1136 .indirect_buffer = buffer->mtl_handle,
1137 .index_buffer = cmd->state.gfx.index.handle,
1138 .indirect_buffer_offset = offset,
1139 .index_buffer_offset = cmd->state.gfx.index.offset,
1140 .index_buffer_range_B =
1141 cmd->state.gfx.index.size - cmd->state.gfx.index.offset,
1142 .prim = vk_topology_to_mesa(dyn->ia.primitive_topology),
1143 .index_size = cmd->state.gfx.index.bytes_per_index,
1144 .indirect = true,
1145 .indexed = true,
1146 };
1147
1148 kk_draw(cmd, data);
1149 }
1150 /* TODO_KOSMICKRISP Remove once above is done */
1151 cmd->state.gfx.descriptors.root_dirty = true;
1152 cmd->state.gfx.descriptors.root.draw.draw_id = 0;
1153 }
1154
1155 VKAPI_ATTR void VKAPI_CALL
1156 kk_CmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer _buffer,
1157 VkDeviceSize offset, VkBuffer countBuffer,
1158 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
1159 uint32_t stride)
1160 {
1161 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
1162 VK_FROM_HANDLE(kk_buffer, buffer, _buffer);
1163 VK_FROM_HANDLE(kk_buffer, count_buffer, countBuffer);
1164
1165 assert((stride % 4) == 0 && "aligned");
1166
1167 const struct vk_dynamic_graphics_state *dyn =
1168 &cmd->vk.dynamic_graphics_state;
1169
1170 size_t out_stride = sizeof(uint32_t) * 4;
1171 struct kk_bo *patched =
1172 kk_cmd_allocate_buffer(cmd, out_stride * maxDrawCount, 4);
1173 uint64_t in = vk_buffer_address(&buffer->vk, offset);
1174 uint64_t count_addr =
1175 vk_buffer_address(&count_buffer->vk, countBufferOffset);
1176
1177 struct mtl_size grid = {maxDrawCount, 1u, 1u};
1178 libkk_predicate_indirect(cmd, grid, true, patched->gpu, in, count_addr,
1179 stride / 4, false);
1180
1181 for (unsigned i = 0; i < maxDrawCount; ++i) {
1182 /* TODO_KOSMICKRISP
1183 * Move this to a separate buffer from the root so we don't have to upload
1184 * it every single loop. Pass it to the kk_draw call as a parameter that
1185 * will later be uploaded.
1186 */
1187 cmd->state.gfx.descriptors.root_dirty = true;
1188 cmd->state.gfx.descriptors.root.draw.draw_id = i;
1189
1190 struct kk_draw_data data = {
1191 .indirect_buffer = patched->map,
1192 .indirect_buffer_offset = out_stride * i,
1193 .prim = vk_topology_to_mesa(dyn->ia.primitive_topology),
1194 .indirect = true,
1195 };
1196
1197 kk_draw(cmd, data);
1198 }
1199 /* TODO_KOSMICKRISP Remove once above is done */
1200 cmd->state.gfx.descriptors.root_dirty = true;
1201 cmd->state.gfx.descriptors.root.draw.draw_id = 0;
1202 }
1203
1204 VKAPI_ATTR void VKAPI_CALL
1205 kk_CmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer _buffer,
1206 VkDeviceSize offset, VkBuffer countBuffer,
1207 VkDeviceSize countBufferOffset,
1208 uint32_t maxDrawCount, uint32_t stride)
1209 {
1210 VK_FROM_HANDLE(kk_cmd_buffer, cmd, commandBuffer);
1211 VK_FROM_HANDLE(kk_buffer, buffer, _buffer);
1212 VK_FROM_HANDLE(kk_buffer, count_buffer, countBuffer);
1213
1214 assert((stride % 4) == 0 && "aligned");
1215
1216 const struct vk_dynamic_graphics_state *dyn =
1217 &cmd->vk.dynamic_graphics_state;
1218
1219 size_t out_stride = sizeof(uint32_t) * 5;
1220 struct kk_bo *patched =
1221 kk_cmd_allocate_buffer(cmd, out_stride * maxDrawCount, 4);
1222 uint64_t in = vk_buffer_address(&buffer->vk, offset);
1223 uint64_t count_addr =
1224 vk_buffer_address(&count_buffer->vk, countBufferOffset);
1225
1226 struct mtl_size grid = {maxDrawCount, 1u, 1u};
1227 libkk_predicate_indirect(cmd, grid, true, patched->gpu, in, count_addr,
1228 stride / 4, true);
1229
1230 for (unsigned i = 0; i < maxDrawCount; ++i) {
1231 /* TODO_KOSMICKRISP
1232 * Move this to a separate buffer from the root so we don't have to upload
1233 * it every single loop. Pass it to the kk_draw call as a parameter that
1234 * will later be uploaded.
1235 */
1236 cmd->state.gfx.descriptors.root_dirty = true;
1237 cmd->state.gfx.descriptors.root.draw.draw_id = i;
1238
1239 struct kk_draw_data data = {
1240 .indirect_buffer = patched->map,
1241 .index_buffer = cmd->state.gfx.index.handle,
1242 .indirect_buffer_offset = out_stride * i,
1243 .index_buffer_offset = cmd->state.gfx.index.offset,
1244 .index_buffer_range_B =
1245 cmd->state.gfx.index.size - cmd->state.gfx.index.offset,
1246 .prim = vk_topology_to_mesa(dyn->ia.primitive_topology),
1247 .index_size = cmd->state.gfx.index.bytes_per_index,
1248 .indirect = true,
1249 .indexed = true,
1250 };
1251
1252 kk_draw(cmd, data);
1253 }
1254 /* TODO_KOSMICKRISP Remove once above is done */
1255 cmd->state.gfx.descriptors.root_dirty = true;
1256 cmd->state.gfx.descriptors.root.draw.draw_id = 0;
1257 }