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 "lvp_private.h"
25 #include "pipe/p_context.h"
26
lvp_CreateQueryPool(VkDevice _device,const VkQueryPoolCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkQueryPool * pQueryPool)27 VKAPI_ATTR VkResult VKAPI_CALL lvp_CreateQueryPool(
28 VkDevice _device,
29 const VkQueryPoolCreateInfo* pCreateInfo,
30 const VkAllocationCallbacks* pAllocator,
31 VkQueryPool* pQueryPool)
32 {
33 LVP_FROM_HANDLE(lvp_device, device, _device);
34
35 uint32_t query_size = sizeof(struct pipe_query *);
36 enum pipe_query_type pipeq;
37 switch (pCreateInfo->queryType) {
38 case VK_QUERY_TYPE_OCCLUSION:
39 pipeq = PIPE_QUERY_OCCLUSION_COUNTER;
40 break;
41 case VK_QUERY_TYPE_TIMESTAMP:
42 pipeq = PIPE_QUERY_TIMESTAMP;
43 break;
44 case VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT:
45 pipeq = PIPE_QUERY_SO_STATISTICS;
46 break;
47 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
48 pipeq = PIPE_QUERY_PIPELINE_STATISTICS;
49 break;
50 case VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT:
51 case VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT:
52 pipeq = PIPE_QUERY_PRIMITIVES_GENERATED;
53 break;
54 case VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR:
55 query_size = sizeof(uint64_t);
56 pipeq = LVP_QUERY_ACCELERATION_STRUCTURE_COMPACTED_SIZE;
57 break;
58 case VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SERIALIZATION_SIZE_KHR:
59 query_size = sizeof(uint64_t);
60 pipeq = LVP_QUERY_ACCELERATION_STRUCTURE_SERIALIZATION_SIZE;
61 break;
62 case VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SIZE_KHR:
63 query_size = sizeof(uint64_t);
64 pipeq = LVP_QUERY_ACCELERATION_STRUCTURE_SIZE;
65 break;
66 case VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SERIALIZATION_BOTTOM_LEVEL_POINTERS_KHR:
67 query_size = sizeof(uint64_t);
68 pipeq = LVP_QUERY_ACCELERATION_STRUCTURE_INSTANCE_COUNT;
69 break;
70 default:
71 return VK_ERROR_FEATURE_NOT_PRESENT;
72 }
73
74 struct lvp_query_pool *pool;
75 size_t pool_size = sizeof(*pool)
76 + pCreateInfo->queryCount * query_size;
77
78 pool = vk_zalloc2(&device->vk.alloc, pAllocator,
79 pool_size, 8,
80 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
81 if (!pool)
82 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
83
84 vk_object_base_init(&device->vk, &pool->base,
85 VK_OBJECT_TYPE_QUERY_POOL);
86 pool->type = pCreateInfo->queryType;
87 pool->count = pCreateInfo->queryCount;
88 pool->base_type = pipeq;
89 pool->pipeline_stats = pCreateInfo->pipelineStatistics;
90 pool->data = &pool->queries;
91
92 *pQueryPool = lvp_query_pool_to_handle(pool);
93 return VK_SUCCESS;
94 }
95
lvp_DestroyQueryPool(VkDevice _device,VkQueryPool _pool,const VkAllocationCallbacks * pAllocator)96 VKAPI_ATTR void VKAPI_CALL lvp_DestroyQueryPool(
97 VkDevice _device,
98 VkQueryPool _pool,
99 const VkAllocationCallbacks* pAllocator)
100 {
101 LVP_FROM_HANDLE(lvp_device, device, _device);
102 LVP_FROM_HANDLE(lvp_query_pool, pool, _pool);
103
104 if (!pool)
105 return;
106
107 if (pool->base_type < PIPE_QUERY_TYPES) {
108 for (unsigned i = 0; i < pool->count; i++)
109 if (pool->queries[i])
110 device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[i]);
111 }
112 vk_object_base_finish(&pool->base);
113 vk_free2(&device->vk.alloc, pAllocator, pool);
114 }
115
lvp_GetQueryPoolResults(VkDevice _device,VkQueryPool queryPool,uint32_t firstQuery,uint32_t queryCount,size_t dataSize,void * pData,VkDeviceSize stride,VkQueryResultFlags flags)116 VKAPI_ATTR VkResult VKAPI_CALL lvp_GetQueryPoolResults(
117 VkDevice _device,
118 VkQueryPool queryPool,
119 uint32_t firstQuery,
120 uint32_t queryCount,
121 size_t dataSize,
122 void* pData,
123 VkDeviceSize stride,
124 VkQueryResultFlags flags)
125 {
126 LVP_FROM_HANDLE(lvp_device, device, _device);
127 LVP_FROM_HANDLE(lvp_query_pool, pool, queryPool);
128 VkResult vk_result = VK_SUCCESS;
129
130 device->vk.dispatch_table.DeviceWaitIdle(_device);
131
132 for (unsigned i = firstQuery; i < firstQuery + queryCount; i++) {
133 uint8_t *dest = (uint8_t *)((char *)pData + (stride * (i - firstQuery)));
134 union pipe_query_result result;
135 bool ready = false;
136
137 if (pool->base_type >= PIPE_QUERY_TYPES) {
138 if (flags & VK_QUERY_RESULT_64_BIT) {
139 uint64_t *dst = (uint64_t *)dest;
140 uint64_t *src = (uint64_t *)pool->data;
141 *dst = src[i];
142 } else {
143 uint32_t *dst = (uint32_t *)dest;
144 uint64_t *src = (uint64_t *)pool->data;
145 *dst = src[i];
146 }
147 continue;
148 }
149
150 if (pool->queries[i]) {
151 ready = device->queue.ctx->get_query_result(device->queue.ctx,
152 pool->queries[i],
153 (flags & VK_QUERY_RESULT_WAIT_BIT),
154 &result);
155 } else {
156 result.u64 = 0;
157 }
158
159 if (!ready && !(flags & VK_QUERY_RESULT_PARTIAL_BIT))
160 vk_result = VK_NOT_READY;
161
162 if (flags & VK_QUERY_RESULT_64_BIT) {
163 uint64_t *dest64 = (uint64_t *) dest;
164 if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
165 if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) {
166 uint32_t mask = pool->pipeline_stats;
167 const uint64_t *pstats = result.pipeline_statistics.counters;
168 while (mask) {
169 uint32_t i = u_bit_scan(&mask);
170 *dest64++ = pstats[i];
171 }
172 } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
173 *dest64++ = result.so_statistics.num_primitives_written;
174 *dest64++ = result.so_statistics.primitives_storage_needed;
175 } else {
176 *dest64++ = result.u64;
177 }
178 } else {
179 if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
180 dest64 += 2; // 16 bytes
181 } else {
182 dest64 += 1; // 8 bytes
183 }
184 }
185 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
186 *dest64 = ready;
187 }
188 } else {
189 uint32_t *dest32 = (uint32_t *) dest;
190 if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
191 if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) {
192 uint32_t mask = pool->pipeline_stats;
193 const uint64_t *pstats = result.pipeline_statistics.counters;
194 while (mask) {
195 uint32_t i = u_bit_scan(&mask);
196 *dest32++ = (uint32_t) MIN2(pstats[i], UINT32_MAX);
197 }
198 } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
199 *dest32++ = (uint32_t)
200 MIN2(result.so_statistics.num_primitives_written, UINT32_MAX);
201 *dest32++ = (uint32_t)
202 MIN2(result.so_statistics.primitives_storage_needed, UINT32_MAX);
203 } else {
204 *dest32++ = (uint32_t) (result.u64 & UINT32_MAX);
205 }
206 } else {
207 if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
208 dest32 += 2; // 8 bytes
209 } else {
210 dest32 += 1; // 4 bytes
211 }
212 }
213 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
214 *dest32 = ready;
215 }
216 }
217 }
218 return vk_result;
219 }
220
lvp_ResetQueryPool(VkDevice _device,VkQueryPool queryPool,uint32_t firstQuery,uint32_t queryCount)221 VKAPI_ATTR void VKAPI_CALL lvp_ResetQueryPool(
222 VkDevice _device,
223 VkQueryPool queryPool,
224 uint32_t firstQuery,
225 uint32_t queryCount)
226 {
227 LVP_FROM_HANDLE(lvp_device, device, _device);
228 LVP_FROM_HANDLE(lvp_query_pool, pool, queryPool);
229
230 if (pool->base_type >= PIPE_QUERY_TYPES)
231 return;
232
233 for (uint32_t i = 0; i < queryCount; i++) {
234 uint32_t idx = i + firstQuery;
235
236 if (pool->queries[idx]) {
237 device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[idx]);
238 pool->queries[idx] = NULL;
239 }
240 }
241 }
242