17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2019 Red Hat. 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 217ec681f3Smrg * IN THE SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#include "lvp_private.h" 257ec681f3Smrg#include "pipe/p_context.h" 267ec681f3Smrg 277ec681f3SmrgVKAPI_ATTR VkResult VKAPI_CALL lvp_CreateQueryPool( 287ec681f3Smrg VkDevice _device, 297ec681f3Smrg const VkQueryPoolCreateInfo* pCreateInfo, 307ec681f3Smrg const VkAllocationCallbacks* pAllocator, 317ec681f3Smrg VkQueryPool* pQueryPool) 327ec681f3Smrg{ 337ec681f3Smrg LVP_FROM_HANDLE(lvp_device, device, _device); 347ec681f3Smrg 357ec681f3Smrg enum pipe_query_type pipeq; 367ec681f3Smrg switch (pCreateInfo->queryType) { 377ec681f3Smrg case VK_QUERY_TYPE_OCCLUSION: 387ec681f3Smrg pipeq = PIPE_QUERY_OCCLUSION_COUNTER; 397ec681f3Smrg break; 407ec681f3Smrg case VK_QUERY_TYPE_TIMESTAMP: 417ec681f3Smrg pipeq = PIPE_QUERY_TIMESTAMP; 427ec681f3Smrg break; 437ec681f3Smrg case VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT: 447ec681f3Smrg pipeq = PIPE_QUERY_SO_STATISTICS; 457ec681f3Smrg break; 467ec681f3Smrg case VK_QUERY_TYPE_PIPELINE_STATISTICS: 477ec681f3Smrg pipeq = PIPE_QUERY_PIPELINE_STATISTICS; 487ec681f3Smrg break; 497ec681f3Smrg default: 507ec681f3Smrg return VK_ERROR_FEATURE_NOT_PRESENT; 517ec681f3Smrg } 527ec681f3Smrg struct lvp_query_pool *pool; 537ec681f3Smrg uint32_t pool_size = sizeof(*pool) + pCreateInfo->queryCount * sizeof(struct pipe_query *); 547ec681f3Smrg 557ec681f3Smrg pool = vk_zalloc2(&device->vk.alloc, pAllocator, 567ec681f3Smrg pool_size, 8, 577ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); 587ec681f3Smrg if (!pool) 597ec681f3Smrg return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); 607ec681f3Smrg 617ec681f3Smrg vk_object_base_init(&device->vk, &pool->base, 627ec681f3Smrg VK_OBJECT_TYPE_QUERY_POOL); 637ec681f3Smrg pool->type = pCreateInfo->queryType; 647ec681f3Smrg pool->count = pCreateInfo->queryCount; 657ec681f3Smrg pool->base_type = pipeq; 667ec681f3Smrg pool->pipeline_stats = pCreateInfo->pipelineStatistics; 677ec681f3Smrg 687ec681f3Smrg *pQueryPool = lvp_query_pool_to_handle(pool); 697ec681f3Smrg return VK_SUCCESS; 707ec681f3Smrg} 717ec681f3Smrg 727ec681f3SmrgVKAPI_ATTR void VKAPI_CALL lvp_DestroyQueryPool( 737ec681f3Smrg VkDevice _device, 747ec681f3Smrg VkQueryPool _pool, 757ec681f3Smrg const VkAllocationCallbacks* pAllocator) 767ec681f3Smrg{ 777ec681f3Smrg LVP_FROM_HANDLE(lvp_device, device, _device); 787ec681f3Smrg LVP_FROM_HANDLE(lvp_query_pool, pool, _pool); 797ec681f3Smrg 807ec681f3Smrg if (!pool) 817ec681f3Smrg return; 827ec681f3Smrg 837ec681f3Smrg for (unsigned i = 0; i < pool->count; i++) 847ec681f3Smrg if (pool->queries[i]) 857ec681f3Smrg device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[i]); 867ec681f3Smrg vk_object_base_finish(&pool->base); 877ec681f3Smrg vk_free2(&device->vk.alloc, pAllocator, pool); 887ec681f3Smrg} 897ec681f3Smrg 907ec681f3SmrgVKAPI_ATTR VkResult VKAPI_CALL lvp_GetQueryPoolResults( 917ec681f3Smrg VkDevice _device, 927ec681f3Smrg VkQueryPool queryPool, 937ec681f3Smrg uint32_t firstQuery, 947ec681f3Smrg uint32_t queryCount, 957ec681f3Smrg size_t dataSize, 967ec681f3Smrg void* pData, 977ec681f3Smrg VkDeviceSize stride, 987ec681f3Smrg VkQueryResultFlags flags) 997ec681f3Smrg{ 1007ec681f3Smrg LVP_FROM_HANDLE(lvp_device, device, _device); 1017ec681f3Smrg LVP_FROM_HANDLE(lvp_query_pool, pool, queryPool); 1027ec681f3Smrg VkResult vk_result = VK_SUCCESS; 1037ec681f3Smrg 1047ec681f3Smrg lvp_DeviceWaitIdle(_device); 1057ec681f3Smrg 1067ec681f3Smrg for (unsigned i = firstQuery; i < firstQuery + queryCount; i++) { 1077ec681f3Smrg uint8_t *dptr = (uint8_t *)((char *)pData + (stride * (i - firstQuery))); 1087ec681f3Smrg union pipe_query_result result; 1097ec681f3Smrg bool ready = false; 1107ec681f3Smrg if (pool->queries[i]) { 1117ec681f3Smrg ready = device->queue.ctx->get_query_result(device->queue.ctx, 1127ec681f3Smrg pool->queries[i], 1137ec681f3Smrg (flags & VK_QUERY_RESULT_WAIT_BIT), 1147ec681f3Smrg &result); 1157ec681f3Smrg } else { 1167ec681f3Smrg result.u64 = 0; 1177ec681f3Smrg } 1187ec681f3Smrg 1197ec681f3Smrg if (!ready && !(flags & VK_QUERY_RESULT_PARTIAL_BIT)) 1207ec681f3Smrg vk_result = VK_NOT_READY; 1217ec681f3Smrg if (flags & VK_QUERY_RESULT_64_BIT) { 1227ec681f3Smrg if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) { 1237ec681f3Smrg if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) { 1247ec681f3Smrg uint32_t mask = pool->pipeline_stats; 1257ec681f3Smrg uint64_t *pstats = (uint64_t *)&result.pipeline_statistics; 1267ec681f3Smrg while (mask) { 1277ec681f3Smrg uint32_t i = u_bit_scan(&mask); 1287ec681f3Smrg 1297ec681f3Smrg *(uint64_t *)dptr = pstats[i]; 1307ec681f3Smrg dptr += 8; 1317ec681f3Smrg } 1327ec681f3Smrg } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) { 1337ec681f3Smrg *(uint64_t *)dptr = result.so_statistics.num_primitives_written; 1347ec681f3Smrg dptr += 8; 1357ec681f3Smrg *(uint64_t *)dptr = result.so_statistics.primitives_storage_needed; 1367ec681f3Smrg dptr += 8; 1377ec681f3Smrg } else { 1387ec681f3Smrg *(uint64_t *)dptr = result.u64; 1397ec681f3Smrg dptr += 8; 1407ec681f3Smrg } 1417ec681f3Smrg } else { 1427ec681f3Smrg if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) 1437ec681f3Smrg dptr += 16; 1447ec681f3Smrg else 1457ec681f3Smrg dptr += 8; 1467ec681f3Smrg } 1477ec681f3Smrg 1487ec681f3Smrg } else { 1497ec681f3Smrg if (ready || (flags & VK_QUERY_RESULT_PARTIAL_BIT)) { 1507ec681f3Smrg if (pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS) { 1517ec681f3Smrg uint32_t mask = pool->pipeline_stats; 1527ec681f3Smrg uint64_t *pstats = (uint64_t *)&result.pipeline_statistics; 1537ec681f3Smrg while (mask) { 1547ec681f3Smrg uint32_t i = u_bit_scan(&mask); 1557ec681f3Smrg 1567ec681f3Smrg if (pstats[i] > UINT32_MAX) 1577ec681f3Smrg *(uint32_t *)dptr = UINT32_MAX; 1587ec681f3Smrg else 1597ec681f3Smrg *(uint32_t *)dptr = pstats[i]; 1607ec681f3Smrg dptr += 4; 1617ec681f3Smrg } 1627ec681f3Smrg } else if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) { 1637ec681f3Smrg if (result.so_statistics.num_primitives_written > UINT32_MAX) 1647ec681f3Smrg *(uint32_t *)dptr = UINT32_MAX; 1657ec681f3Smrg else 1667ec681f3Smrg *(uint32_t *)dptr = (uint32_t)result.so_statistics.num_primitives_written; 1677ec681f3Smrg dptr += 4; 1687ec681f3Smrg if (result.so_statistics.primitives_storage_needed > UINT32_MAX) 1697ec681f3Smrg *(uint32_t *)dptr = UINT32_MAX; 1707ec681f3Smrg else 1717ec681f3Smrg *(uint32_t *)dptr = (uint32_t)result.so_statistics.primitives_storage_needed; 1727ec681f3Smrg dptr += 4; 1737ec681f3Smrg } else { 1747ec681f3Smrg if (result.u64 > UINT32_MAX) 1757ec681f3Smrg *(uint32_t *)dptr = UINT32_MAX; 1767ec681f3Smrg else 1777ec681f3Smrg *(uint32_t *)dptr = result.u32; 1787ec681f3Smrg dptr += 4; 1797ec681f3Smrg } 1807ec681f3Smrg } else 1817ec681f3Smrg if (pool->type == VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT) 1827ec681f3Smrg dptr += 8; 1837ec681f3Smrg else 1847ec681f3Smrg dptr += 4; 1857ec681f3Smrg } 1867ec681f3Smrg 1877ec681f3Smrg if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) { 1887ec681f3Smrg if (flags & VK_QUERY_RESULT_64_BIT) 1897ec681f3Smrg *(uint64_t *)dptr = ready; 1907ec681f3Smrg else 1917ec681f3Smrg *(uint32_t *)dptr = ready; 1927ec681f3Smrg } 1937ec681f3Smrg } 1947ec681f3Smrg return vk_result; 1957ec681f3Smrg} 1967ec681f3Smrg 1977ec681f3SmrgVKAPI_ATTR void VKAPI_CALL lvp_ResetQueryPool( 1987ec681f3Smrg VkDevice _device, 1997ec681f3Smrg VkQueryPool queryPool, 2007ec681f3Smrg uint32_t firstQuery, 2017ec681f3Smrg uint32_t queryCount) 2027ec681f3Smrg{ 2037ec681f3Smrg LVP_FROM_HANDLE(lvp_device, device, _device); 2047ec681f3Smrg LVP_FROM_HANDLE(lvp_query_pool, pool, queryPool); 2057ec681f3Smrg 2067ec681f3Smrg for (uint32_t i = 0; i < queryCount; i++) { 2077ec681f3Smrg uint32_t idx = i + firstQuery; 2087ec681f3Smrg 2097ec681f3Smrg if (pool->queries[idx]) { 2107ec681f3Smrg device->queue.ctx->destroy_query(device->queue.ctx, pool->queries[idx]); 2117ec681f3Smrg pool->queries[idx] = NULL; 2127ec681f3Smrg } 2137ec681f3Smrg } 2147ec681f3Smrg} 215