lavapipe: clean-ups in lvp_GetQueryPoolResults()
authorBrian Paul <brianp@vmware.com>
Wed, 5 Apr 2023 15:42:33 +0000 (09:42 -0600)
committerMarge Bot <emma+marge@anholt.net>
Tue, 25 Apr 2023 02:09:26 +0000 (02:09 +0000)
Simplify the code which puts query results into the destination
buffer:
* Use a uint64 or uint32 pointer instead of uint8 to write the results
  to the buffer for less casting and simplifient pointer incrementing.
* Use MIN2() macro to be more concise.
And fix some indentation.

Signed-off-by: Brian Paul <brianp@vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22467>

src/gallium/frontends/lavapipe/lvp_query.c

index aa987b8..26059d0 100644 (file)
@@ -52,8 +52,10 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_CreateQueryPool(
    default:
       return VK_ERROR_FEATURE_NOT_PRESENT;
    }
+
    struct lvp_query_pool *pool;
-   uint32_t pool_size = sizeof(*pool) + pCreateInfo->queryCount * sizeof(struct pipe_query *);
+   size_t pool_size = sizeof(*pool)
+      + pCreateInfo->queryCount * sizeof(struct pipe_query *);
 
    pool = vk_zalloc2(&device->vk.alloc, pAllocator,
                     pool_size, 8,
@@ -107,91 +109,76 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_GetQueryPoolResults(
    device->vk.dispatch_table.DeviceWaitIdle(_device);
 
    for (unsigned i = firstQuery; i < firstQuery + queryCount; i++) {
-      uint8_t *dptr = (uint8_t *)((char *)pData + (stride * (i - firstQuery)));
+      uint8_t *dest = (uint8_t *)((char *)pData + (stride * (i - firstQuery)));
       union pipe_query_result result;
       bool ready = false;
+
       if (pool->queries[i]) {
-        ready = device->queue.ctx->get_query_result(device->queue.ctx,
-                                                    pool->queries[i],
-                                                    (flags & VK_QUERY_RESULT_WAIT_BIT),
-                                                    &result);
+         ready = device->queue.ctx->get_query_result(device->queue.ctx,
+                                                     pool->queries[i],
+                                                     (flags & VK_QUERY_RESULT_WAIT_BIT),
+                                                     &result);
       } else {
-        result.u64 = 0;
+         result.u64 = 0;
       }
 
       if (!ready && !(flags & VK_QUERY_RESULT_PARTIAL_BIT))
           vk_result = VK_NOT_READY;
+
       if (flags & VK_QUERY_RESULT_64_BIT) {
+         uint64_t *dest64 = (uint64_t *) dest;
          if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
             if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) {
                uint32_t mask = pool->pipeline_stats;
-               uint64_t *pstats = (uint64_t *)&result.pipeline_statistics;
+               const uint64_t *pstats = result.pipeline_statistics.counters;
                while (mask) {
                   uint32_t i = u_bit_scan(&mask);
-
-                  *(uint64_t *)dptr = pstats[i];
-                  dptr += 8;
+                  *dest64++ = pstats[i];
                }
             } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
-               *(uint64_t *)dptr = result.so_statistics.num_primitives_written;
-               dptr += 8;
-               *(uint64_t *)dptr = result.so_statistics.primitives_storage_needed;
-               dptr += 8;
+               *dest64++ = result.so_statistics.num_primitives_written;
+               *dest64++ = result.so_statistics.primitives_storage_needed;
             } else {
-               *(uint64_t *)dptr = result.u64;
-               dptr += 8;
+               *dest64++ = result.u64;
             }
          } else {
-            if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT)
-               dptr += 16;
-            else
-               dptr += 8;
+            if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
+               dest64 += 2; // 16 bytes
+            } else {
+               dest64 += 1; // 8 bytes
+            }
+         }
+         if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
+            *dest64 = ready;
          }
-
       } else {
+         uint32_t *dest32 = (uint32_t *) dest;
          if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
             if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) {
                uint32_t mask = pool->pipeline_stats;
-               uint64_t *pstats = (uint64_t *)&result.pipeline_statistics;
+               const uint64_t *pstats = result.pipeline_statistics.counters;
                while (mask) {
                   uint32_t i = u_bit_scan(&mask);
-
-                  if (pstats[i] > UINT32_MAX)
-                     *(uint32_t *)dptr = UINT32_MAX;
-                  else
-                     *(uint32_t *)dptr = pstats[i];
-                  dptr += 4;
+                  *dest32++ = (uint32_t) MIN2(pstats[i], UINT32_MAX);
                }
             } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
-               if (result.so_statistics.num_primitives_written > UINT32_MAX)
-                  *(uint32_t *)dptr = UINT32_MAX;
-               else
-                  *(uint32_t *)dptr = (uint32_t)result.so_statistics.num_primitives_written;
-               dptr += 4;
-               if (result.so_statistics.primitives_storage_needed > UINT32_MAX)
-                  *(uint32_t *)dptr = UINT32_MAX;
-               else
-                  *(uint32_t *)dptr = (uint32_t)result.so_statistics.primitives_storage_needed;
-               dptr += 4;
+               *dest32++ = (uint32_t)
+                  MIN2(result.so_statistics.num_primitives_written, UINT32_MAX);
+               *dest32++ = (uint32_t)
+                  MIN2(result.so_statistics.primitives_storage_needed, UINT32_MAX);
             } else {
-               if (result.u64 > UINT32_MAX)
-                  *(uint32_t *)dptr = UINT32_MAX;
-               else
-                  *(uint32_t *)dptr = result.u32;
-               dptr += 4;
+               *dest32++ = (uint32_t) MIN2(result.u64, UINT32_MAX);
             }
-         } else
-            if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT)
-               dptr += 8;
-            else
-               dptr += 4;
-      }
-
-      if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
-        if (flags & VK_QUERY_RESULT_64_BIT)
-           *(uint64_t *)dptr = ready;
-        else
-           *(uint32_t *)dptr = ready;
+         } else {
+            if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
+               dest32 += 2;  // 8 bytes
+            } else {
+               dest32 += 1;  // 4 bytes
+            }
+         }
+         if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
+            *dest32 = ready;
+         }
       }
    }
    return vk_result;