vallium: initial import of the vulkan frontend
[mesa.git] / src / gallium / frontends / vallium / val_query.c
1 /*
2 * Copyright © 2019 Red Hat.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "val_private.h"
25 #include "pipe/p_context.h"
26
27 VkResult val_CreateQueryPool(
28 VkDevice _device,
29 const VkQueryPoolCreateInfo* pCreateInfo,
30 const VkAllocationCallbacks* pAllocator,
31 VkQueryPool* pQueryPool)
32 {
33 VAL_FROM_HANDLE(val_device, device, _device);
34
35 enum pipe_query_type pipeq;
36 switch (pCreateInfo->queryType) {
37 case VK_QUERY_TYPE_OCCLUSION:
38 pipeq = PIPE_QUERY_OCCLUSION_COUNTER;
39 break;
40 case VK_QUERY_TYPE_TIMESTAMP:
41 pipeq = PIPE_QUERY_TIMESTAMP;
42 break;
43 default:
44 return VK_ERROR_FEATURE_NOT_PRESENT;
45 }
46 struct val_query_pool *pool;
47 uint32_t pool_size = sizeof(*pool) + pCreateInfo->queryCount * sizeof(struct pipe_query *);
48
49 pool = vk_zalloc2(&device->alloc, pAllocator,
50 pool_size, 8,
51 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
52 if (!pool)
53 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
54
55 vk_object_base_init(&device->vk, &pool->base,
56 VK_OBJECT_TYPE_QUERY_POOL);
57 pool->type = pCreateInfo->queryType;
58 pool->count = pCreateInfo->queryCount;
59 pool->base_type = pipeq;
60
61 *pQueryPool = val_query_pool_to_handle(pool);
62 return VK_SUCCESS;
63 }
64
65 void val_DestroyQueryPool(
66 VkDevice _device,
67 VkQueryPool _pool,
68 const VkAllocationCallbacks* pAllocator)
69 {
70 VAL_FROM_HANDLE(val_device, device, _device);
71 VAL_FROM_HANDLE(val_query_pool, pool, _pool);
72
73 if (!pool)
74 return;
75
76 for (unsigned i = 0; i < pool->count; i++)
77 if (pool->queries[i])
78 device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[i]);
79 vk_object_base_finish(&pool->base);
80 vk_free2(&device->alloc, pAllocator, pool);
81 }
82
83 VkResult val_GetQueryPoolResults(
84 VkDevice _device,
85 VkQueryPool queryPool,
86 uint32_t firstQuery,
87 uint32_t queryCount,
88 size_t dataSize,
89 void* pData,
90 VkDeviceSize stride,
91 VkQueryResultFlags flags)
92 {
93 VAL_FROM_HANDLE(val_device, device, _device);
94 VAL_FROM_HANDLE(val_query_pool, pool, queryPool);
95 VkResult vk_result = VK_SUCCESS;
96
97 val_DeviceWaitIdle(_device);
98
99 for (unsigned i = firstQuery; i < firstQuery + queryCount; i++) {
100 uint8_t *dptr = (uint8_t *)((char *)pData + (stride * (i - firstQuery)));
101 union pipe_query_result result;
102 bool ready = false;
103 if (pool->queries[i]) {
104 ready = device->queue.ctx->get_query_result(device->queue.ctx,
105 pool->queries[i],
106 (flags & VK_QUERY_RESULT_WAIT_BIT),
107 &result);
108 } else {
109 result.u64 = 0;
110 }
111
112 if (!ready && !(flags & VK_QUERY_RESULT_PARTIAL_BIT))
113 vk_result = VK_NOT_READY;
114 if (flags & VK_QUERY_RESULT_64_BIT) {
115 if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT))
116 *(uint64_t *)dptr = result.u64;
117 dptr += 8;
118 } else {
119 if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
120 if (result.u64 > UINT32_MAX)
121 *(uint32_t *)dptr = UINT32_MAX;
122 else
123 *(uint32_t *)dptr = result.u32;
124 }
125 dptr += 4;
126 }
127
128 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
129 if (flags & VK_QUERY_RESULT_64_BIT)
130 *(uint64_t *)dptr = ready;
131 else
132 *(uint32_t *)dptr = ready;
133 }
134 }
135 return vk_result;
136 }