vk: Map uncached, coherent memory as write-combine
[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 uint32_t slot_size;
42 uint64_t size;
43
44 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
45
46 switch (pCreateInfo->queryType) {
47 case VK_QUERY_TYPE_OCCLUSION:
48 slot_size = sizeof(struct anv_query_pool_slot);
49 break;
50 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
51 return VK_ERROR_INCOMPATIBLE_DRIVER;
52 case VK_QUERY_TYPE_TIMESTAMP:
53 slot_size = sizeof(uint64_t);
54 break;
55 default:
56 assert(!"Invalid query type");
57 }
58
59 pool = anv_alloc2(&device->alloc, pAllocator, sizeof(*pool), 8,
60 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
61 if (pool == NULL)
62 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
63
64 pool->type = pCreateInfo->queryType;
65 pool->slots = pCreateInfo->entryCount;
66
67 size = pCreateInfo->entryCount * slot_size;
68 result = anv_bo_init_new(&pool->bo, device, size);
69 if (result != VK_SUCCESS)
70 goto fail;
71
72 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0, size, 0);
73
74 *pQueryPool = anv_query_pool_to_handle(pool);
75
76 return VK_SUCCESS;
77
78 fail:
79 anv_free2(&device->alloc, pAllocator, pool);
80
81 return result;
82 }
83
84 void anv_DestroyQueryPool(
85 VkDevice _device,
86 VkQueryPool _pool,
87 const VkAllocationCallbacks* pAllocator)
88 {
89 ANV_FROM_HANDLE(anv_device, device, _device);
90 ANV_FROM_HANDLE(anv_query_pool, pool, _pool);
91
92 anv_gem_munmap(pool->bo.map, pool->bo.size);
93 anv_gem_close(device, pool->bo.gem_handle);
94 anv_free2(&device->alloc, pAllocator, pool);
95 }
96
97 VkResult anv_GetQueryPoolResults(
98 VkDevice _device,
99 VkQueryPool queryPool,
100 uint32_t startQuery,
101 uint32_t queryCount,
102 size_t dataSize,
103 void* pData,
104 VkDeviceSize stride,
105 VkQueryResultFlags flags)
106 {
107 ANV_FROM_HANDLE(anv_device, device, _device);
108 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
109 int64_t timeout = INT64_MAX;
110 uint64_t result;
111 int ret;
112
113 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
114 /* Where is the availabilty info supposed to go? */
115 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
116 return VK_ERROR_INCOMPATIBLE_DRIVER;
117 }
118
119 assert(pool->type == VK_QUERY_TYPE_OCCLUSION ||
120 pool->type == VK_QUERY_TYPE_TIMESTAMP);
121
122 if (pData == NULL)
123 return VK_SUCCESS;
124
125 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
126 ret = anv_gem_wait(device, pool->bo.gem_handle, &timeout);
127 if (ret == -1) {
128 /* We don't know the real error. */
129 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
130 "gem_wait failed %m");
131 }
132 }
133
134 void *data_end = pData + dataSize;
135
136 for (uint32_t i = 0; i < queryCount; i++) {
137 switch (pool->type) {
138 case VK_QUERY_TYPE_OCCLUSION: {
139 struct anv_query_pool_slot *slot = pool->bo.map;
140 result = slot[startQuery + i].end - slot[startQuery + i].begin;
141 break;
142 }
143 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
144 /* Not yet implemented */
145 break;
146 case VK_QUERY_TYPE_TIMESTAMP: {
147 uint64_t *slot = pool->bo.map;
148 result = slot[startQuery + i];
149 break;
150 }
151 default:
152 assert(!"Invalid query type");
153 }
154
155 if (flags & VK_QUERY_RESULT_64_BIT) {
156 *(uint64_t *)pData = result;
157 } else {
158 if (result > UINT32_MAX)
159 result = UINT32_MAX;
160 *(uint32_t *)pData = result;
161 }
162 pData += stride;
163 if (pData >= data_end)
164 break;
165 }
166
167 return VK_SUCCESS;
168 }
169
170 void anv_CmdResetQueryPool(
171 VkCommandBuffer commandBuffer,
172 VkQueryPool queryPool,
173 uint32_t startQuery,
174 uint32_t queryCount)
175 {
176 stub();
177 }