zink: merge qbo update copies when possible
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>
Fri, 24 Feb 2023 13:10:46 +0000 (08:10 -0500)
committerMarge Bot <emma+marge@anholt.net>
Mon, 6 Mar 2023 02:22:39 +0000 (02:22 +0000)
if a single query is being started and stopped frequently, update
the internal qbo with a single copy call instead of one copy per result

not actually that useful in practice because of how query pools are shared,
but could help somewhere in theory

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21581>

src/gallium/drivers/zink/zink_query.c

index 5b4599d..84c1ba4 100644 (file)
@@ -824,14 +824,25 @@ update_qbo(struct zink_context *ctx, struct zink_query *q)
    bool is_timestamp = q->type == PIPE_QUERY_TIMESTAMP;
    /* timestamp queries just write to offset 0 always */
    int num_queries = get_num_queries(q);
-   for (unsigned j = q->start_offset; j < num_starts; j++) {
-      unsigned cur_offset = q->curr_qbo->num_results * get_num_results(q) * sizeof(uint64_t);
-      for (unsigned i = 0; i < num_queries; i++) {
+   unsigned num_results = qbo->num_results;
+   for (unsigned i = 0; i < num_queries; i++) {
+      unsigned start_offset = q->start_offset;
+      while (start_offset < num_starts) {
+         unsigned num_merged_copies = 0;
+         VkQueryPool qp = starts[start_offset].vkq[i]->pool->query_pool;
+         unsigned base_id = starts[start_offset].vkq[i]->query_id;
+         /* iterate over all the starts to see how many can be merged */
+         for (unsigned j = start_offset; j < num_starts; j++, num_merged_copies++) {
+            if (starts[j].vkq[i]->pool->query_pool != qp || starts[j].vkq[i]->query_id != base_id + num_merged_copies)
+               break;
+         }
+         assert(num_merged_copies);
+         unsigned cur_offset = start_offset * get_num_results(q) * sizeof(uint64_t);
          unsigned offset = is_timestamp ? 0 : cur_offset;
-         copy_pool_results_to_buffer(ctx, q, starts[j].vkq[i]->pool->query_pool, starts[j].vkq[i]->query_id,
+         copy_pool_results_to_buffer(ctx, q, starts[start_offset].vkq[i]->pool->query_pool, starts[start_offset].vkq[i]->query_id,
                                     zink_resource(qbo->buffers[i]),
                                     offset,
-                                    1,
+                                    num_merged_copies,
                                     /*
                                        there is an implicit execution dependency from
                                        each such query command to all query commands previously submitted to the same queue. There
@@ -842,12 +853,12 @@ update_qbo(struct zink_context *ctx, struct zink_query *q)
                                     * - Chapter 18. Queries
                                     */
                                     VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
-      }
-      if (!is_timestamp) {
-         q->curr_qbo->num_results++;
-         q->start_offset++;
+         if (!is_timestamp)
+            q->curr_qbo->num_results += num_merged_copies;
+         start_offset += num_merged_copies;
       }
    }
+   q->start_offset += q->curr_qbo->num_results - num_results;
 
 
    if (is_timestamp)