vk/0.210.0: Rework allocation to use the new pAllocator's
[mesa.git] / src / vulkan / anv_query.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 VkResult anv_CreateQueryPool(
33 VkDevice _device,
34 const VkQueryPoolCreateInfo* pCreateInfo,
35 const VkAllocationCallbacks* pAllocator,
36 VkQueryPool* pQueryPool)
37 {
38 ANV_FROM_HANDLE(anv_device, device, _device);
39 struct anv_query_pool *pool;
40 VkResult result;
41 size_t size;
42
43 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
44
45 switch (pCreateInfo->queryType) {
46 case VK_QUERY_TYPE_OCCLUSION:
47 break;
48 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
49 return VK_UNSUPPORTED;
50 default:
51 unreachable("");
52 }
53
54 pool = anv_alloc2(&device->alloc, pAllocator, sizeof(*pool), 8,
55 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
56 if (pool == NULL)
57 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
58
59 size = pCreateInfo->entryCount * sizeof(struct anv_query_pool_slot);
60 result = anv_bo_init_new(&pool->bo, device, size);
61 if (result != VK_SUCCESS)
62 goto fail;
63
64 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0, size);
65
66 *pQueryPool = anv_query_pool_to_handle(pool);
67
68 return VK_SUCCESS;
69
70 fail:
71 anv_free2(&device->alloc, pAllocator, pool);
72
73 return result;
74 }
75
76 void anv_DestroyQueryPool(
77 VkDevice _device,
78 VkQueryPool _pool,
79 const VkAllocationCallbacks* pAllocator)
80 {
81 ANV_FROM_HANDLE(anv_device, device, _device);
82 ANV_FROM_HANDLE(anv_query_pool, pool, _pool);
83
84 anv_gem_munmap(pool->bo.map, pool->bo.size);
85 anv_gem_close(device, pool->bo.gem_handle);
86 anv_free2(&device->alloc, pAllocator, pool);
87 }
88
89 VkResult anv_GetQueryPoolResults(
90 VkDevice _device,
91 VkQueryPool queryPool,
92 uint32_t startQuery,
93 uint32_t queryCount,
94 size_t* pDataSize,
95 void* pData,
96 VkQueryResultFlags flags)
97 {
98 ANV_FROM_HANDLE(anv_device, device, _device);
99 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
100 struct anv_query_pool_slot *slot = pool->bo.map;
101 int64_t timeout = INT64_MAX;
102 uint32_t *dst32 = pData;
103 uint64_t *dst64 = pData;
104 uint64_t result;
105 int ret;
106
107 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
108 /* Where is the availabilty info supposed to go? */
109 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
110 return VK_UNSUPPORTED;
111 }
112
113 assert(pool->type == VK_QUERY_TYPE_OCCLUSION);
114
115 if (flags & VK_QUERY_RESULT_64_BIT)
116 *pDataSize = queryCount * sizeof(uint64_t);
117 else
118 *pDataSize = queryCount * sizeof(uint32_t);
119
120 if (pData == NULL)
121 return VK_SUCCESS;
122
123 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
124 ret = anv_gem_wait(device, pool->bo.gem_handle, &timeout);
125 if (ret == -1) {
126 /* We don't know the real error. */
127 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
128 "gem_wait failed %m");
129 }
130 }
131
132 for (uint32_t i = 0; i < queryCount; i++) {
133 result = slot[startQuery + i].end - slot[startQuery + i].begin;
134 if (flags & VK_QUERY_RESULT_64_BIT) {
135 *dst64++ = result;
136 } else {
137 if (result > UINT32_MAX)
138 result = UINT32_MAX;
139 *dst32++ = result;
140 }
141 }
142
143 return VK_SUCCESS;
144 }
145
146 void anv_CmdResetQueryPool(
147 VkCommandBuffer commandBuffer,
148 VkQueryPool queryPool,
149 uint32_t startQuery,
150 uint32_t queryCount)
151 {
152 stub();
153 }