blob: 978ffaeee3946a9a7618dac60937878585062aa1 [file] [log] [blame]
Chia-I Wue18ff1b2014-08-07 13:38:51 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wue18ff1b2014-08-07 13:38:51 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wue18ff1b2014-08-07 13:38:51 +080026 */
27
28#include "dev.h"
29#include "mem.h"
30#include "query.h"
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -060031#include "genhw/genhw.h"
Chia-I Wue18ff1b2014-08-07 13:38:51 +080032
33static void query_destroy(struct intel_obj *obj)
34{
35 struct intel_query *query = intel_query_from_obj(obj);
36
Tony Barbour2094dc72015-07-09 15:26:32 -060037 intel_mem_free(obj->mem);
Chia-I Wue18ff1b2014-08-07 13:38:51 +080038 intel_query_destroy(query);
39}
40
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -060041static void query_init_pipeline_statistics(
42 struct intel_dev *dev,
43 const VkQueryPoolCreateInfo *info,
44 struct intel_query *query)
45{
46 /*
47 * Note: order defined by Vulkan spec.
48 */
49 const uint32_t regs[][2] = {
50 {VK_QUERY_PIPELINE_STATISTIC_IA_PRIMITIVES_BIT, GEN6_REG_IA_PRIMITIVES_COUNT},
51 {VK_QUERY_PIPELINE_STATISTIC_VS_INVOCATIONS_BIT, GEN6_REG_VS_INVOCATION_COUNT},
52 {VK_QUERY_PIPELINE_STATISTIC_GS_INVOCATIONS_BIT, GEN6_REG_GS_INVOCATION_COUNT},
53 {VK_QUERY_PIPELINE_STATISTIC_GS_PRIMITIVES_BIT, GEN6_REG_GS_PRIMITIVES_COUNT},
54 {VK_QUERY_PIPELINE_STATISTIC_C_INVOCATIONS_BIT, GEN6_REG_CL_INVOCATION_COUNT},
55 {VK_QUERY_PIPELINE_STATISTIC_C_PRIMITIVES_BIT, GEN6_REG_CL_PRIMITIVES_COUNT},
56 {VK_QUERY_PIPELINE_STATISTIC_FS_INVOCATIONS_BIT, GEN6_REG_PS_INVOCATION_COUNT},
57 {VK_QUERY_PIPELINE_STATISTIC_TCS_PATCHES_BIT, (intel_gpu_gen(dev->gpu) >= INTEL_GEN(7)) ? GEN7_REG_HS_INVOCATION_COUNT : 0},
58 {VK_QUERY_PIPELINE_STATISTIC_TES_INVOCATIONS_BIT, (intel_gpu_gen(dev->gpu) >= INTEL_GEN(7)) ? GEN7_REG_DS_INVOCATION_COUNT : 0},
59 {VK_QUERY_PIPELINE_STATISTIC_CS_INVOCATIONS_BIT, 0}
60 };
61 STATIC_ASSERT(ARRAY_SIZE(regs) < 32);
62 uint32_t i;
63 uint32_t reg_count = 0;
64
65 /*
66 * Only query registers indicated via pipeline statistics flags.
67 * If HW does not support a flag, fill value with 0.
68 */
69 for (i=0; i < ARRAY_SIZE(regs); i++) {
70 if ((regs[i][0] & info->pipelineStatistics)) {
71 query->regs[reg_count] = regs[i][1];
72 reg_count++;
73 }
74 }
75
76 query->reg_count = reg_count;
77 query->slot_stride = u_align(reg_count * sizeof(uint64_t) * 2, 64);
78}
79
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060080VkResult intel_query_create(struct intel_dev *dev,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -060081 const VkQueryPoolCreateInfo *info,
82 struct intel_query **query_ret)
Chia-I Wue18ff1b2014-08-07 13:38:51 +080083{
84 struct intel_query *query;
85
Chia-I Wu545c2e12015-02-22 13:19:54 +080086 query = (struct intel_query *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060087 sizeof(*query), dev->base.dbg, VK_OBJECT_TYPE_QUEUE,
Chia-I Wu545c2e12015-02-22 13:19:54 +080088 info, 0);
Chia-I Wue18ff1b2014-08-07 13:38:51 +080089 if (!query)
Tony Barbour8205d902015-04-16 15:59:00 -060090 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wue18ff1b2014-08-07 13:38:51 +080091
92 query->type = info->queryType;
93 query->slot_count = info->slots;
94
Chia-I Wu659650f2014-08-07 14:11:49 +080095 /*
96 * For each query type, the GPU will be asked to write the values of some
97 * registers to a buffer before and after a sequence of commands. We will
98 * compare the differences to get the query results.
99 */
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800100 switch (info->queryType) {
Tony Barbour8205d902015-04-16 15:59:00 -0600101 case VK_QUERY_TYPE_OCCLUSION:
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800102 query->slot_stride = u_align(sizeof(uint64_t) * 2, 64);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800103 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600104 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600105 query_init_pipeline_statistics(dev, info, query);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800106 break;
107 default:
108 break;
109 }
110
111 if (!query->slot_stride) {
112 intel_query_destroy(query);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600113 return VK_ERROR_INVALID_VALUE;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800114 }
115
Tony Barbour2094dc72015-07-09 15:26:32 -0600116 VkMemoryAllocInfo mem_reqs;
117 mem_reqs.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
118 mem_reqs.allocationSize = query->slot_stride * query->slot_count;
119 mem_reqs.pNext = NULL;
120 mem_reqs.memoryTypeIndex = 0;
121 intel_mem_alloc(dev, &mem_reqs, &query->obj.mem);
122
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800123 query->obj.destroy = query_destroy;
124
125 *query_ret = query;
126
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600127 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800128}
129
130void intel_query_destroy(struct intel_query *query)
131{
132 intel_base_destroy(&query->obj.base);
133}
134
Chia-I Wu659650f2014-08-07 14:11:49 +0800135static void
136query_process_occlusion(const struct intel_query *query,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600137 uint32_t count, const uint8_t *raw,
Chia-I Wu659650f2014-08-07 14:11:49 +0800138 uint64_t *results)
139{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600140 uint32_t i;
Chia-I Wu659650f2014-08-07 14:11:49 +0800141
142 for (i = 0; i < count; i++) {
143 const uint32_t *pair = (const uint32_t *) raw;
144
145 results[i] = pair[1] - pair[0];
146 raw += query->slot_stride;
147 }
148}
149
150static void
151query_process_pipeline_statistics(const struct intel_query *query,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600152 uint32_t count, const uint8_t *raw,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600153 void *results)
Chia-I Wu659650f2014-08-07 14:11:49 +0800154{
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600155 const uint32_t num_regs = query->reg_count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600156 uint32_t i, j;
Chia-I Wu659650f2014-08-07 14:11:49 +0800157
158 for (i = 0; i < count; i++) {
159 const uint64_t *before = (const uint64_t *) raw;
160 const uint64_t *after = before + num_regs;
161 uint64_t *dst = (uint64_t *) (results + i);
162
163 for (j = 0; j < num_regs; j++)
164 dst[j] = after[j] - before[j];
165
166 raw += query->slot_stride;
167 }
168}
169
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600170VkResult intel_query_get_results(struct intel_query *query,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600171 uint32_t slot_start, uint32_t slot_count,
172 void *results)
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800173{
174 const uint8_t *ptr;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800175
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800176 if (intel_mem_is_busy(query->obj.mem))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600177 return VK_NOT_READY;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800178
179 ptr = (const uint8_t *) intel_mem_map_sync(query->obj.mem, false);
180 if (!ptr)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600181 return VK_ERROR_MEMORY_MAP_FAILED;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800182
183 ptr += query->obj.offset + query->slot_stride * slot_start;
184
185 switch (query->type) {
Tony Barbour8205d902015-04-16 15:59:00 -0600186 case VK_QUERY_TYPE_OCCLUSION:
Chia-I Wu659650f2014-08-07 14:11:49 +0800187 query_process_occlusion(query, slot_count, ptr, results);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800188 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600189 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Chia-I Wu659650f2014-08-07 14:11:49 +0800190 query_process_pipeline_statistics(query, slot_count, ptr, results);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800191 break;
192 default:
193 assert(0);
194 break;
195 }
196
197 intel_mem_unmap(query->obj.mem);
198
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600199 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800200}
201
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600202ICD_EXPORT VkResult VKAPI vkCreateQueryPool(
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600203 VkDevice device,
204 const VkQueryPoolCreateInfo* pCreateInfo,
205 VkQueryPool* pQueryPool)
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800206{
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800207 struct intel_dev *dev = intel_dev(device);
208
209 return intel_query_create(dev, pCreateInfo,
210 (struct intel_query **) pQueryPool);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800211}
212
Tony Barbourde4124d2015-07-03 10:33:54 -0600213ICD_EXPORT VkResult VKAPI vkDestroyQueryPool(
214 VkDevice device,
215 VkQueryPool queryPool)
216
217 {
218 struct intel_obj *obj = intel_obj(queryPool.handle);
219
220 obj->destroy(obj);
221 return VK_SUCCESS;
222 }
223
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600224ICD_EXPORT VkResult VKAPI vkGetQueryPoolResults(
Mike Stroyan230e6252015-04-17 12:36:38 -0600225 VkDevice device,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600226 VkQueryPool queryPool,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600227 uint32_t startQuery,
228 uint32_t queryCount,
229 size_t* pDataSize,
Tony Barbour8205d902015-04-16 15:59:00 -0600230 void* pData,
231 VkQueryResultFlags flags)
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800232{
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800233 struct intel_query *query = intel_query(queryPool);
234
235 switch (query->type) {
Tony Barbour8205d902015-04-16 15:59:00 -0600236 case VK_QUERY_TYPE_OCCLUSION:
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800237 *pDataSize = sizeof(uint64_t) * queryCount;
238 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600239 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600240 *pDataSize = query->slot_stride * queryCount;
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800241 break;
242 default:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600243 return VK_ERROR_INVALID_HANDLE;
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800244 break;
245 }
246
247 if (pData)
248 return intel_query_get_results(query, startQuery, queryCount, pData);
249 else
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600250 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800251}