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 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 case VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT:
44 pipeq = PIPE_QUERY_SO_STATISTICS;
45 break;
46 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
47 pipeq = PIPE_QUERY_PIPELINE_STATISTICS;
48 break;
49 case VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT:
50 pipeq = PIPE_QUERY_PRIMITIVES_GENERATED;
51 break;
52 default:
53 return VK_ERROR_FEATURE_NOT_PRESENT;
54 }
55 struct lvp_query_pool *pool;
56 uint32_t pool_size = sizeof(*pool) + pCreateInfo->queryCount * sizeof(struct pipe_query *);
57
58 pool = vk_zalloc2(&device->vk.alloc, pAllocator,
59 pool_size, 8,
60 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
61 if (!pool)
62 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
63
64 vk_object_base_init(&device->vk, &pool->base,
65 VK_OBJECT_TYPE_QUERY_POOL);
66 pool->type = pCreateInfo->queryType;
67 pool->count = pCreateInfo->queryCount;
68 pool->base_type = pipeq;
69 pool->pipeline_stats = pCreateInfo->pipelineStatistics;
70
71 *pQueryPool = lvp_query_pool_to_handle(pool);
72 return VK_SUCCESS;
73 }
74
lvp_DestroyQueryPool(VkDevice _device,VkQueryPool _pool,const VkAllocationCallbacks * pAllocator)75 VKAPI_ATTR void VKAPI_CALL lvp_DestroyQueryPool(
76 VkDevice _device,
77 VkQueryPool _pool,
78 const VkAllocationCallbacks* pAllocator)
79 {
80 LVP_FROM_HANDLE(lvp_device, device, _device);
81 LVP_FROM_HANDLE(lvp_query_pool, pool, _pool);
82
83 if (!pool)
84 return;
85
86 for (unsigned i = 0; i < pool->count; i++)
87 if (pool->queries[i])
88 device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[i]);
89 vk_object_base_finish(&pool->base);
90 vk_free2(&device->vk.alloc, pAllocator, pool);
91 }
92
lvp_GetQueryPoolResults(VkDevice _device,VkQueryPool queryPool,uint32_t firstQuery,uint32_t queryCount,size_t dataSize,void * pData,VkDeviceSize stride,VkQueryResultFlags flags)93 VKAPI_ATTR VkResult VKAPI_CALL lvp_GetQueryPoolResults(
94 VkDevice _device,
95 VkQueryPool queryPool,
96 uint32_t firstQuery,
97 uint32_t queryCount,
98 size_t dataSize,
99 void* pData,
100 VkDeviceSize stride,
101 VkQueryResultFlags flags)
102 {
103 LVP_FROM_HANDLE(lvp_device, device, _device);
104 LVP_FROM_HANDLE(lvp_query_pool, pool, queryPool);
105 VkResult vk_result = VK_SUCCESS;
106
107 device->vk.dispatch_table.DeviceWaitIdle(_device);
108
109 for (unsigned i = firstQuery; i < firstQuery + queryCount; i++) {
110 uint8_t *dptr = (uint8_t *)((char *)pData + (stride * (i - firstQuery)));
111 union pipe_query_result result;
112 bool ready = false;
113 if (pool->queries[i]) {
114 ready = device->queue.ctx->get_query_result(device->queue.ctx,
115 pool->queries[i],
116 (flags & VK_QUERY_RESULT_WAIT_BIT),
117 &result);
118 } else {
119 result.u64 = 0;
120 }
121
122 if (!ready && !(flags & VK_QUERY_RESULT_PARTIAL_BIT))
123 vk_result = VK_NOT_READY;
124 if (flags & VK_QUERY_RESULT_64_BIT) {
125 if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
126 if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) {
127 uint32_t mask = pool->pipeline_stats;
128 uint64_t *pstats = (uint64_t *)&result.pipeline_statistics;
129 while (mask) {
130 uint32_t i = u_bit_scan(&mask);
131
132 *(uint64_t *)dptr = pstats[i];
133 dptr += 8;
134 }
135 } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
136 *(uint64_t *)dptr = result.so_statistics.num_primitives_written;
137 dptr += 8;
138 *(uint64_t *)dptr = result.so_statistics.primitives_storage_needed;
139 dptr += 8;
140 } else {
141 *(uint64_t *)dptr = result.u64;
142 dptr += 8;
143 }
144 } else {
145 if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT)
146 dptr += 16;
147 else
148 dptr += 8;
149 }
150
151 } else {
152 if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) {
153 if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) {
154 uint32_t mask = pool->pipeline_stats;
155 uint64_t *pstats = (uint64_t *)&result.pipeline_statistics;
156 while (mask) {
157 uint32_t i = u_bit_scan(&mask);
158
159 if (pstats[i] > UINT32_MAX)
160 *(uint32_t *)dptr = UINT32_MAX;
161 else
162 *(uint32_t *)dptr = pstats[i];
163 dptr += 4;
164 }
165 } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) {
166 if (result.so_statistics.num_primitives_written > UINT32_MAX)
167 *(uint32_t *)dptr = UINT32_MAX;
168 else
169 *(uint32_t *)dptr = (uint32_t)result.so_statistics.num_primitives_written;
170 dptr += 4;
171 if (result.so_statistics.primitives_storage_needed > UINT32_MAX)
172 *(uint32_t *)dptr = UINT32_MAX;
173 else
174 *(uint32_t *)dptr = (uint32_t)result.so_statistics.primitives_storage_needed;
175 dptr += 4;
176 } else {
177 if (result.u64 > UINT32_MAX)
178 *(uint32_t *)dptr = UINT32_MAX;
179 else
180 *(uint32_t *)dptr = result.u32;
181 dptr += 4;
182 }
183 } else
184 if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT)
185 dptr += 8;
186 else
187 dptr += 4;
188 }
189
190 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
191 if (flags & VK_QUERY_RESULT_64_BIT)
192 *(uint64_t *)dptr = ready;
193 else
194 *(uint32_t *)dptr = ready;
195 }
196 }
197 return vk_result;
198 }
199
lvp_ResetQueryPool(VkDevice _device,VkQueryPool queryPool,uint32_t firstQuery,uint32_t queryCount)200 VKAPI_ATTR void VKAPI_CALL lvp_ResetQueryPool(
201 VkDevice _device,
202 VkQueryPool queryPool,
203 uint32_t firstQuery,
204 uint32_t queryCount)
205 {
206 LVP_FROM_HANDLE(lvp_device, device, _device);
207 LVP_FROM_HANDLE(lvp_query_pool, pool, queryPool);
208
209 for (uint32_t i = 0; i < queryCount; i++) {
210 uint32_t idx = i + firstQuery;
211
212 if (pool->queries[idx]) {
213 device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[idx]);
214 pool->queries[idx] = NULL;
215 }
216 }
217 }
218