Debian Codegraph @

src/asahi/vulkan/hk_query_pool.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_query_pool.h"
8
9 #include "agx_compile.h"
10 #include "agx_pack.h"
11 #include "hk_buffer.h"
12 #include "hk_cmd_buffer.h"
13 #include "hk_device.h"
14 #include "hk_entrypoints.h"
15 #include "hk_shader.h"
16
17 #include "libagx_dgc.h"
18 #include "libagx_shaders.h"
19 #include "vk_common_entrypoints.h"
20
21 #include "asahi/lib/agx_bo.h"
22 #include "asahi/libagx/libagx.h"
23 #include "asahi/libagx/query.h"
24 #include "compiler/nir/nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 #include "drm-uapi/asahi_drm.h"
28 #include "util/os_time.h"
29 #include "util/u_dynarray.h"
30 #include "vulkan/vulkan_core.h"
31
32 struct hk_query_report {
33 uint64_t value;
34 };
35
36 static inline bool
37 hk_has_available(const struct hk_query_pool *pool)
38 {
39 return pool->vk.query_type != VK_QUERY_TYPE_TIMESTAMP;
40 }
41
42 static uint16_t *
43 hk_pool_oq_index_ptr(const struct hk_query_pool *pool)
44 {
45 return agx_bo_map(pool->bo) + pool->query_start;
46 }
47
48 static uint32_t
49 hk_reports_per_query(struct hk_query_pool *pool)
50 {
51 switch (pool->vk.query_type) {
52 case VK_QUERY_TYPE_OCCLUSION:
53 case VK_QUERY_TYPE_TIMESTAMP:
54 case VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT:
55 return 1;
56 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
57 return util_bitcount(pool->vk.pipeline_statistics);
58 case VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT:
59 // Primitives succeeded and primitives needed
60 return 2;
61 default:
62 UNREACHABLE("Unsupported query type");
63 }
64 }
65
66 static void
67 hk_flush_if_timestamp(struct hk_cmd_buffer *cmd, struct hk_query_pool *pool)
68 {
69 /* There might not be a barrier between the timestamp write and the copy
70 * otherwise but we need one to give the CPU a chance to write the timestamp.
71 * This could maybe optimized.
72 */
73 if (pool->vk.query_type == VK_QUERY_TYPE_TIMESTAMP) {
74 perf_debug(cmd, "Flushing for timestamp copy");
75 hk_cmd_buffer_end_graphics(cmd);
76 hk_cmd_buffer_end_compute(cmd);
77 }
78 }
79
80 static uint64_t
81 hk_query_available_addr(struct hk_query_pool *pool, uint32_t query)
82 {
83 assert(hk_has_available(pool));
84 assert(query < pool->vk.query_count);
85 return pool->bo->va->addr + query * sizeof(uint32_t);
86 }
87
88 static uint32_t *
89 hk_query_available_map(struct hk_query_pool *pool, uint32_t query)
90 {
91 assert(hk_has_available(pool));
92 assert(query < pool->vk.query_count);
93 return (uint32_t *)agx_bo_map(pool->bo) + query;
94 }
95
96 static uint64_t
97 hk_query_offset(struct hk_query_pool *pool, uint32_t query)
98 {
99 assert(query < pool->vk.query_count);
100 return pool->query_start + query * pool->query_stride;
101 }
102
103 static uint64_t
104 hk_query_report_addr(struct hk_device *dev, struct hk_query_pool *pool,
105 uint32_t query)
106 {
107 if (pool->oq_queries) {
108 uint16_t *oq_index = hk_pool_oq_index_ptr(pool);
109 return dev->occlusion_queries.bo->va->addr +
110 (oq_index[query] * sizeof(uint64_t));
111 } else {
112 return pool->bo->va->addr + hk_query_offset(pool, query);
113 }
114 }
115
116 static struct hk_query_report *
117 hk_query_report_map(struct hk_device *dev, struct hk_query_pool *pool,
118 uint32_t query)
119 {
120 if (pool->oq_queries) {
121 uint64_t *queries = (uint64_t *)agx_bo_map(dev->occlusion_queries.bo);
122 uint16_t *oq_index = hk_pool_oq_index_ptr(pool);
123
124 return (struct hk_query_report *)&queries[oq_index[query]];
125 } else {
126 return (void *)((char *)agx_bo_map(pool->bo) +
127 hk_query_offset(pool, query));
128 }
129 }
130
131 void
132 hk_dispatch_imm_writes(struct hk_cmd_buffer *cmd, struct hk_cs *cs)
133 {
134 /* As soon as we mark a query available, it needs to be available system
135 * wide, otherwise a CPU-side get result can query. As such, we cache flush
136 * before and then let coherency works its magic. Without this barrier, we
137 * get flakes in
138 *
139 * dEQP-VK.query_pool.occlusion_query.get_results_conservative_size_64_wait_query_without_availability_draw_triangles_discard
140 */
141 struct hk_device *dev = hk_cmd_buffer_device(cmd);
142 hk_cdm_cache_flush(dev, cs);
143
144 perf_debug(cmd, "Queued writes");
145
146 uint64_t params =
147 hk_pool_upload(cmd, cs->imm_writes.data, cs->imm_writes.size, 16);
148
149 uint32_t count =
150 util_dynarray_num_elements(&cs->imm_writes, struct libagx_imm_write);
151 assert(count > 0);
152
153 libagx_write_u32s(cmd, agx_1d(count), AGX_BARRIER_ALL | AGX_POSTGFX, params);
154 }
155
156 void
157 hk_queue_write(struct hk_cmd_buffer *cmd, uint64_t address, uint32_t value,
158 bool after_gfx)
159 {
160 struct hk_cs *cs = hk_cmd_buffer_get_cs_general(
161 cmd, after_gfx ? &cmd->current_cs.post_gfx : &cmd->current_cs.cs, true);
162 if (!cs)
163 return;
164
165 /* TODO: Generalize this mechanism suitably */
166 if (after_gfx) {
167 struct libagx_imm_write imm = {.address = address, .value = value};
168
169 if (!cs->imm_writes.data) {
170 cs->imm_writes = UTIL_DYNARRAY_INIT;
171 }
172
173 util_dynarray_append(&cs->imm_writes, imm);
174 return;
175 }
176
177 /* As soon as we mark a query available, it needs to be available system
178 * wide, otherwise a CPU-side get result can query. As such, we cache flush
179 * before and then let coherency works its magic. Without this barrier, we
180 * get flakes in
181 *
182 * dEQP-VK.query_pool.occlusion_query.get_results_conservative_size_64_wait_query_without_availability_draw_triangles_discard
183 */
184 struct hk_device *dev = hk_cmd_buffer_device(cmd);
185 hk_cdm_cache_flush(dev, cs);
186
187 perf_debug(cmd, "Queued write");
188 libagx_write_u32(cmd, agx_1d(1), AGX_BARRIER_ALL, address, value);
189 }
190
191 static void
192 emit_zero_queries(struct hk_cmd_buffer *cmd, struct hk_query_pool *pool,
193 uint32_t first_index, uint32_t num_queries,
194 bool set_available)
195 {
196 struct hk_device *dev = hk_cmd_buffer_device(cmd);
197 perf_debug(cmd, "Query pool zero");
198
199 struct libagx_reset_query_args info = {
200 .availability = hk_has_available(pool) ? pool->bo->va->addr : 0,
201 .results = pool->oq_queries ? dev->occlusion_queries.bo->va->addr
202 : pool->bo->va->addr + pool->query_start,
203 .oq_index = pool->oq_queries ? pool->bo->va->addr + pool->query_start : 0,
204
205 .first_query = first_index,
206 .reports_per_query = hk_reports_per_query(pool),
207 .set_available = set_available,
208 };
209
210 libagx_reset_query_struct(cmd, agx_1d(num_queries), AGX_BARRIER_ALL, info);
211 }
212
213 static void
214 host_zero_queries(struct hk_device *dev, struct hk_query_pool *pool,
215 uint32_t first_index, uint32_t num_queries,
216 bool set_available)
217 {
218 for (uint32_t i = 0; i < num_queries; i++) {
219 struct hk_query_report *reports =
220 hk_query_report_map(dev, pool, first_index + i);
221
222 uint64_t value = 0;
223 if (hk_has_available(pool)) {
224 uint32_t *available = hk_query_available_map(pool, first_index + i);
225 *available = set_available;
226 } else {
227 value = set_available ? 0 : LIBAGX_QUERY_UNAVAILABLE;
228 }
229
230 for (unsigned j = 0; j < hk_reports_per_query(pool); ++j) {
231 reports[j].value = value;
232 }
233 }
234 }
235
236 VKAPI_ATTR VkResult VKAPI_CALL
237 hk_CreateQueryPool(VkDevice device, const VkQueryPoolCreateInfo *pCreateInfo,
238 const VkAllocationCallbacks *pAllocator,
239 VkQueryPool *pQueryPool)
240 {
241 VK_FROM_HANDLE(hk_device, dev, device);
242 struct hk_query_pool *pool;
243
244 bool occlusion = pCreateInfo->queryType == VK_QUERY_TYPE_OCCLUSION;
245 bool timestamp = pCreateInfo->queryType == VK_QUERY_TYPE_TIMESTAMP;
246 unsigned occlusion_queries = occlusion ? pCreateInfo->queryCount : 0;
247
248 pool =
249 vk_query_pool_create(&dev->vk, pCreateInfo, pAllocator, sizeof(*pool));
250 if (!pool)
251 return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
252
253 /* We place the availability first and then data */
254 pool->query_start = 0;
255 if (hk_has_available(pool)) {
256 pool->query_start = align(pool->vk.query_count * sizeof(uint32_t),
257 sizeof(struct hk_query_report));
258 }
259
260 uint32_t reports_per_query = hk_reports_per_query(pool);
261 pool->query_stride = reports_per_query * sizeof(struct hk_query_report);
262
263 if (pool->vk.query_count > 0) {
264 uint32_t bo_size = pool->query_start;
265
266 /* For occlusion queries, we stick the query index remapping here */
267 if (occlusion_queries)
268 bo_size += sizeof(uint16_t) * pool->vk.query_count;
269 else
270 bo_size += pool->query_stride * pool->vk.query_count;
271
272 /* The kernel requires that timestamp buffers are SHARED */
273 enum agx_bo_flags flags = AGX_BO_WRITEBACK;
274 if (timestamp)
275 flags |= AGX_BO_SHARED;
276
277 pool->bo = agx_bo_create(&dev->dev, bo_size, 0, flags, "Query pool");
278 if (!pool->bo) {
279 hk_DestroyQueryPool(device, hk_query_pool_to_handle(pool), pAllocator);
280 return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
281 }
282
283 /* Timestamp buffers must be explicitly bound as such before we can use
284 * them.
285 */
286 if (timestamp) {
287 int ret = agx_bind_timestamps(&dev->dev, pool->bo, &pool->handle);
288 if (ret) {
289 hk_DestroyQueryPool(device, hk_query_pool_to_handle(pool),
290 pAllocator);
291 return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
292 }
293
294 assert(pool->handle && "handles are nonzero");
295 }
296 }
297
298 uint16_t *oq_index = hk_pool_oq_index_ptr(pool);
299
300 for (unsigned i = 0; i < occlusion_queries; ++i) {
301 uint64_t zero = 0;
302 unsigned index;
303
304 VkResult result = hk_descriptor_table_add(
305 dev, &dev->occlusion_queries, &zero, sizeof(uint64_t), &index);
306
307 if (result != VK_SUCCESS) {
308 hk_DestroyQueryPool(device, hk_query_pool_to_handle(pool), pAllocator);
309 return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
310 }
311
312 /* We increment as we go so we can clean up properly if we run out */
313 assert(pool->oq_queries < occlusion_queries);
314 oq_index[pool->oq_queries++] = index;
315 }
316
317 if (pCreateInfo->flags & VK_QUERY_POOL_CREATE_RESET_BIT_KHR)
318 host_zero_queries(dev, pool, 0, pool->vk.query_count, false);
319
320 *pQueryPool = hk_query_pool_to_handle(pool);
321
322 return VK_SUCCESS;
323 }
324
325 VKAPI_ATTR void VKAPI_CALL
326 hk_DestroyQueryPool(VkDevice device, VkQueryPool queryPool,
327 const VkAllocationCallbacks *pAllocator)
328 {
329 VK_FROM_HANDLE(hk_device, dev, device);
330 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
331
332 if (!pool)
333 return;
334
335 uint16_t *oq_index = hk_pool_oq_index_ptr(pool);
336
337 for (unsigned i = 0; i < pool->oq_queries; ++i) {
338 hk_descriptor_table_remove(dev, &dev->occlusion_queries, oq_index[i]);
339 }
340
341 if (pool->handle)
342 dev->dev.ops.bo_unbind_object(&dev->dev, pool->handle);
343
344 agx_bo_unreference(&dev->dev, pool->bo);
345 vk_query_pool_destroy(&dev->vk, pAllocator, &pool->vk);
346 }
347
348 VKAPI_ATTR void VKAPI_CALL
349 hk_ResetQueryPool(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery,
350 uint32_t queryCount)
351 {
352 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
353 VK_FROM_HANDLE(hk_device, dev, device);
354
355 host_zero_queries(dev, pool, firstQuery, queryCount, false);
356 }
357
358 VKAPI_ATTR void VKAPI_CALL
359 hk_CmdResetQueryPool(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
360 uint32_t firstQuery, uint32_t queryCount)
361 {
362 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
363 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
364
365 hk_flush_if_timestamp(cmd, pool);
366
367 perf_debug(cmd, "Reset query pool");
368 emit_zero_queries(cmd, pool, firstQuery, queryCount, false);
369 }
370
371 VKAPI_ATTR void VKAPI_CALL
372 hk_CmdWriteTimestamp2(VkCommandBuffer commandBuffer,
373 VkPipelineStageFlags2 stage, VkQueryPool queryPool,
374 uint32_t query)
375 {
376 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
377 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
378 struct hk_device *dev = hk_cmd_buffer_device(cmd);
379
380 uint64_t report_addr = hk_query_report_addr(dev, pool, query);
381 bool after_gfx = cmd->current_cs.gfx != NULL;
382
383 /* When writing timestamps for compute, we split the control stream at each
384 * write. This ensures we never need to copy compute timestamps, which would
385 * require an extra control stream anyway. Unlike graphics, splitting compute
386 * control streams is inexpensive so there's not a strong performance reason
387 * to do otherwise. Finally, batching multiple timestamp writes (like we do
388 * for graphics) would destroy the ability to profile individual compute
389 * dispatches. While that's allowed by the Vulkan spec, it's pretty mean to
390 * apps. So.. don't do that.
391 */
392 if (!after_gfx && cmd->current_cs.cs &&
393 cmd->current_cs.cs->timestamp.end.addr) {
394
395 perf_debug(cmd, "Splitting for compute timestamp");
396 hk_cmd_buffer_end_compute(cmd);
397 }
398
399 struct hk_cs *cs = hk_cmd_buffer_get_cs_general(
400 cmd, after_gfx ? &cmd->current_cs.gfx : &cmd->current_cs.cs, true);
401 if (!cs)
402 return;
403
404 if (cs->timestamp.end.addr) {
405 assert(after_gfx && "compute is handled above");
406
407 libagx_copy_timestamp(cmd, agx_1d(1), AGX_BARRIER_ALL | AGX_POSTGFX,
408 report_addr, cs->timestamp.end.addr);
409 } else {
410 cs->timestamp.end = (struct agx_timestamp_req){
411 .addr = report_addr,
412 .handle = pool->handle,
413 .offset_B = hk_query_offset(pool, query),
414 };
415 }
416
417 /* From the Vulkan spec:
418 *
419 * "If vkCmdWriteTimestamp2 is called while executing a render pass
420 * instance that has multiview enabled, the timestamp uses N consecutive
421 * query indices in the query pool (starting at query) where N is the
422 * number of bits set in the view mask of the subpass the command is
423 * executed in. The resulting query values are determined by an
424 * implementation-dependent choice of one of the following behaviors:"
425 *
426 * In our case, only the first query is used, so we emit zeros for the
427 * remaining queries, as described in the first behavior listed in the
428 * Vulkan spec:
429 *
430 * "The first query is a timestamp value and (if more than one bit is set
431 * in the view mask) zero is written to the remaining queries."
432 */
433 if (cmd->state.gfx.render.view_mask != 0) {
434 const uint32_t num_queries =
435 util_bitcount(cmd->state.gfx.render.view_mask);
436 if (num_queries > 1)
437 emit_zero_queries(cmd, pool, query + 1, num_queries - 1, true);
438 }
439 }
440
441 static void
442 hk_cmd_begin_end_query(struct hk_cmd_buffer *cmd, struct hk_query_pool *pool,
443 uint32_t query, uint32_t index,
444 VkQueryControlFlags flags, bool end)
445 {
446 struct hk_device *dev = hk_cmd_buffer_device(cmd);
447 bool graphics = false;
448
449 switch (pool->vk.query_type) {
450 case VK_QUERY_TYPE_OCCLUSION: {
451 assert(query < pool->oq_queries);
452
453 if (end) {
454 cmd->state.gfx.occlusion.mode = AGX_VISIBILITY_MODE_NONE;
455 } else {
456 cmd->state.gfx.occlusion.mode = flags & VK_QUERY_CONTROL_PRECISE_BIT
457 ? AGX_VISIBILITY_MODE_COUNTING
458 : AGX_VISIBILITY_MODE_BOOLEAN;
459 }
460
461 uint16_t *oq_index = hk_pool_oq_index_ptr(pool);
462 cmd->state.gfx.occlusion.index = oq_index[query];
463 cmd->state.gfx.dirty |= HK_DIRTY_OCCLUSION;
464 graphics = true;
465 break;
466 }
467
468 case VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT: {
469 uint64_t addr = hk_query_report_addr(dev, pool, query);
470 cmd->state.gfx.xfb_query[index] = end ? 0 : addr;
471 break;
472 }
473
474 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
475 struct hk_root_descriptor_table *root = &cmd->state.gfx.descriptors.root;
476 cmd->state.gfx.descriptors.root_dirty = true;
477
478 root->draw.pipeline_stats = hk_query_report_addr(dev, pool, query);
479 root->draw.pipeline_stats_flags = pool->vk.pipeline_statistics;
480
481 /* XXX: I don't think is correct... when does the query become available
482 * exactly?
483 */
484 graphics = pool->vk.pipeline_statistics &
485 ~VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
486 break;
487 }
488
489 default:
490 UNREACHABLE("Unsupported query type");
491 }
492
493 /* We need to set available=1 after the graphics work finishes. */
494 if (end) {
495 perf_debug(cmd, "Query ending, type %u", pool->vk.query_type);
496 hk_queue_write(cmd, hk_query_available_addr(pool, query), 1, graphics);
497 }
498 }
499
500 VKAPI_ATTR void VKAPI_CALL
501 hk_CmdBeginQueryIndexedEXT(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
502 uint32_t query, VkQueryControlFlags flags,
503 uint32_t index)
504 {
505 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
506 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
507
508 hk_cmd_begin_end_query(cmd, pool, query, index, flags, false);
509 }
510
511 VKAPI_ATTR void VKAPI_CALL
512 hk_CmdEndQueryIndexedEXT(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
513 uint32_t query, uint32_t index)
514 {
515 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
516 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
517
518 hk_cmd_begin_end_query(cmd, pool, query, index, 0, true);
519
520 /* From the Vulkan spec:
521 *
522 * "If queries are used while executing a render pass instance that has
523 * multiview enabled, the query uses N consecutive query indices in
524 * the query pool (starting at query) where N is the number of bits set
525 * in the view mask in the subpass the query is used in. How the
526 * numerical results of the query are distributed among the queries is
527 * implementation-dependent."
528 *
529 * In our case, only the first query is used, so we emit zeros for the
530 * remaining queries.
531 */
532 if (cmd->state.gfx.render.view_mask != 0) {
533 const uint32_t num_queries =
534 util_bitcount(cmd->state.gfx.render.view_mask);
535 if (num_queries > 1) {
536 perf_debug(cmd, "Multiview query zeroing");
537 emit_zero_queries(cmd, pool, query + 1, num_queries - 1, true);
538 }
539 }
540 }
541
542 static bool
543 hk_query_is_available(struct hk_device *dev, struct hk_query_pool *pool,
544 uint32_t query)
545 {
546 if (hk_has_available(pool)) {
547 uint32_t *available = hk_query_available_map(pool, query);
548 return p_atomic_read(available) != 0;
549 } else {
550 const struct hk_query_report *report =
551 hk_query_report_map(dev, pool, query);
552
553 return report->value != LIBAGX_QUERY_UNAVAILABLE;
554 }
555 }
556
557 #define HK_QUERY_TIMEOUT 2000000000ull
558
559 static VkResult
560 hk_query_wait_for_available(struct hk_device *dev, struct hk_query_pool *pool,
561 uint32_t query)
562 {
563 uint64_t abs_timeout_ns = os_time_get_absolute_timeout(HK_QUERY_TIMEOUT);
564
565 while (os_time_get_nano() < abs_timeout_ns) {
566 if (hk_query_is_available(dev, pool, query))
567 return VK_SUCCESS;
568
569 VkResult status = vk_device_check_status(&dev->vk);
570 if (status != VK_SUCCESS)
571 return status;
572 }
573
574 return vk_device_set_lost(&dev->vk, "query timeout");
575 }
576
577 static void
578 cpu_write_query_result(void *dst, uint32_t idx, VkQueryResultFlags flags,
579 uint64_t result)
580 {
581 if (flags & VK_QUERY_RESULT_64_BIT) {
582 uint64_t *dst64 = dst;
583 dst64[idx] = result;
584 } else {
585 uint32_t *dst32 = dst;
586 dst32[idx] = result;
587 }
588 }
589
590 VKAPI_ATTR VkResult VKAPI_CALL
591 hk_GetQueryPoolResults(VkDevice device, VkQueryPool queryPool,
592 uint32_t firstQuery, uint32_t queryCount,
593 size_t dataSize, void *pData, VkDeviceSize stride,
594 VkQueryResultFlags flags)
595 {
596 VK_FROM_HANDLE(hk_device, dev, device);
597 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
598
599 if (vk_device_is_lost(&dev->vk))
600 return VK_ERROR_DEVICE_LOST;
601
602 VkResult status = VK_SUCCESS;
603 for (uint32_t i = 0; i < queryCount; i++) {
604 const uint32_t query = firstQuery + i;
605
606 bool available = hk_query_is_available(dev, pool, query);
607
608 if (!available && (flags & VK_QUERY_RESULT_WAIT_BIT)) {
609 status = hk_query_wait_for_available(dev, pool, query);
610 if (status != VK_SUCCESS)
611 return status;
612
613 available = true;
614 }
615
616 bool write_results = available || (flags & VK_QUERY_RESULT_PARTIAL_BIT);
617
618 const struct hk_query_report *src = hk_query_report_map(dev, pool, query);
619 assert(i * stride < dataSize);
620 void *dst = (char *)pData + i * stride;
621
622 uint32_t reports = hk_reports_per_query(pool);
623 if (write_results) {
624 for (uint32_t j = 0; j < reports; j++) {
625 cpu_write_query_result(dst, j, flags, src[j].value);
626 }
627 }
628
629 if (!write_results)
630 status = VK_NOT_READY;
631
632 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT)
633 cpu_write_query_result(dst, reports, flags, available);
634 }
635
636 return status;
637 }
638
639 VKAPI_ATTR void VKAPI_CALL
640 hk_CmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
641 uint32_t firstQuery, uint32_t queryCount,
642 VkBuffer dstBuffer, VkDeviceSize dstOffset,
643 VkDeviceSize stride, VkQueryResultFlags flags)
644 {
645 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
646 VK_FROM_HANDLE(hk_query_pool, pool, queryPool);
647 VK_FROM_HANDLE(hk_buffer, dst_buffer, dstBuffer);
648
649 struct hk_device *dev = hk_cmd_buffer_device(cmd);
650 hk_flush_if_timestamp(cmd, pool);
651
652 perf_debug(cmd, "Query pool copy");
653
654 struct libagx_copy_query_args info = {
655 .availability = hk_has_available(pool) ? pool->bo->va->addr : 0,
656 .results = pool->oq_queries ? dev->occlusion_queries.bo->va->addr
657 : pool->bo->va->addr + pool->query_start,
658 .oq_index = pool->oq_queries ? pool->bo->va->addr + pool->query_start : 0,
659
660 .first_query = firstQuery,
661 .dst_addr = hk_buffer_address_rw(dst_buffer, dstOffset),
662 .dst_stride = stride,
663 .reports_per_query = hk_reports_per_query(pool),
664 .flags = flags,
665 };
666
667 libagx_copy_query_struct(cmd, agx_1d(queryCount), AGX_BARRIER_ALL, info);
668 }