blob: 5f65ed6e2a16af18314533bd3c5a6e3d96353319 [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
37 intel_query_destroy(query);
38}
39
Tony Barbour426b9052015-06-24 16:06:58 -060040static VkResult query_get_memory_requirements(struct intel_base *base,
41 VkMemoryRequirements* pRequirements)
Chia-I Wue18ff1b2014-08-07 13:38:51 +080042{
43 struct intel_query *query = intel_query_from_base(base);
Chia-I Wue18ff1b2014-08-07 13:38:51 +080044
Mark Lobodzinski72346292015-07-02 16:49:40 -060045 pRequirements->size = query->slot_stride * query->slot_count;
46 pRequirements->alignment = 64;
47 pRequirements->memoryTypeBits = (1 << INTEL_MEMORY_TYPE_COUNT) - 1;
Chia-I Wue18ff1b2014-08-07 13:38:51 +080048
Tony Barbour426b9052015-06-24 16:06:58 -060049 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +080050}
51
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -060052static void query_init_pipeline_statistics(
53 struct intel_dev *dev,
54 const VkQueryPoolCreateInfo *info,
55 struct intel_query *query)
56{
57 /*
58 * Note: order defined by Vulkan spec.
59 */
60 const uint32_t regs[][2] = {
61 {VK_QUERY_PIPELINE_STATISTIC_IA_PRIMITIVES_BIT, GEN6_REG_IA_PRIMITIVES_COUNT},
62 {VK_QUERY_PIPELINE_STATISTIC_VS_INVOCATIONS_BIT, GEN6_REG_VS_INVOCATION_COUNT},
63 {VK_QUERY_PIPELINE_STATISTIC_GS_INVOCATIONS_BIT, GEN6_REG_GS_INVOCATION_COUNT},
64 {VK_QUERY_PIPELINE_STATISTIC_GS_PRIMITIVES_BIT, GEN6_REG_GS_PRIMITIVES_COUNT},
65 {VK_QUERY_PIPELINE_STATISTIC_C_INVOCATIONS_BIT, GEN6_REG_CL_INVOCATION_COUNT},
66 {VK_QUERY_PIPELINE_STATISTIC_C_PRIMITIVES_BIT, GEN6_REG_CL_PRIMITIVES_COUNT},
67 {VK_QUERY_PIPELINE_STATISTIC_FS_INVOCATIONS_BIT, GEN6_REG_PS_INVOCATION_COUNT},
68 {VK_QUERY_PIPELINE_STATISTIC_TCS_PATCHES_BIT, (intel_gpu_gen(dev->gpu) >= INTEL_GEN(7)) ? GEN7_REG_HS_INVOCATION_COUNT : 0},
69 {VK_QUERY_PIPELINE_STATISTIC_TES_INVOCATIONS_BIT, (intel_gpu_gen(dev->gpu) >= INTEL_GEN(7)) ? GEN7_REG_DS_INVOCATION_COUNT : 0},
70 {VK_QUERY_PIPELINE_STATISTIC_CS_INVOCATIONS_BIT, 0}
71 };
72 STATIC_ASSERT(ARRAY_SIZE(regs) < 32);
73 uint32_t i;
74 uint32_t reg_count = 0;
75
76 /*
77 * Only query registers indicated via pipeline statistics flags.
78 * If HW does not support a flag, fill value with 0.
79 */
80 for (i=0; i < ARRAY_SIZE(regs); i++) {
81 if ((regs[i][0] & info->pipelineStatistics)) {
82 query->regs[reg_count] = regs[i][1];
83 reg_count++;
84 }
85 }
86
87 query->reg_count = reg_count;
88 query->slot_stride = u_align(reg_count * sizeof(uint64_t) * 2, 64);
89}
90
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060091VkResult intel_query_create(struct intel_dev *dev,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -060092 const VkQueryPoolCreateInfo *info,
93 struct intel_query **query_ret)
Chia-I Wue18ff1b2014-08-07 13:38:51 +080094{
95 struct intel_query *query;
96
Chia-I Wu545c2e12015-02-22 13:19:54 +080097 query = (struct intel_query *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060098 sizeof(*query), dev->base.dbg, VK_OBJECT_TYPE_QUEUE,
Chia-I Wu545c2e12015-02-22 13:19:54 +080099 info, 0);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800100 if (!query)
Tony Barbour8205d902015-04-16 15:59:00 -0600101 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800102
103 query->type = info->queryType;
104 query->slot_count = info->slots;
105
Chia-I Wu659650f2014-08-07 14:11:49 +0800106 /*
107 * For each query type, the GPU will be asked to write the values of some
108 * registers to a buffer before and after a sequence of commands. We will
109 * compare the differences to get the query results.
110 */
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800111 switch (info->queryType) {
Tony Barbour8205d902015-04-16 15:59:00 -0600112 case VK_QUERY_TYPE_OCCLUSION:
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800113 query->slot_stride = u_align(sizeof(uint64_t) * 2, 64);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800114 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600115 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600116 query_init_pipeline_statistics(dev, info, query);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800117 break;
118 default:
119 break;
120 }
121
122 if (!query->slot_stride) {
123 intel_query_destroy(query);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600124 return VK_ERROR_INVALID_VALUE;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800125 }
126
Tony Barbour426b9052015-06-24 16:06:58 -0600127 query->obj.base.get_memory_requirements = query_get_memory_requirements;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800128 query->obj.destroy = query_destroy;
129
130 *query_ret = query;
131
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600132 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800133}
134
135void intel_query_destroy(struct intel_query *query)
136{
137 intel_base_destroy(&query->obj.base);
138}
139
Chia-I Wu659650f2014-08-07 14:11:49 +0800140static void
141query_process_occlusion(const struct intel_query *query,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600142 uint32_t count, const uint8_t *raw,
Chia-I Wu659650f2014-08-07 14:11:49 +0800143 uint64_t *results)
144{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600145 uint32_t i;
Chia-I Wu659650f2014-08-07 14:11:49 +0800146
147 for (i = 0; i < count; i++) {
148 const uint32_t *pair = (const uint32_t *) raw;
149
150 results[i] = pair[1] - pair[0];
151 raw += query->slot_stride;
152 }
153}
154
155static void
156query_process_pipeline_statistics(const struct intel_query *query,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600157 uint32_t count, const uint8_t *raw,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600158 void *results)
Chia-I Wu659650f2014-08-07 14:11:49 +0800159{
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600160 const uint32_t num_regs = query->reg_count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600161 uint32_t i, j;
Chia-I Wu659650f2014-08-07 14:11:49 +0800162
163 for (i = 0; i < count; i++) {
164 const uint64_t *before = (const uint64_t *) raw;
165 const uint64_t *after = before + num_regs;
166 uint64_t *dst = (uint64_t *) (results + i);
167
168 for (j = 0; j < num_regs; j++)
169 dst[j] = after[j] - before[j];
170
171 raw += query->slot_stride;
172 }
173}
174
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600175VkResult intel_query_get_results(struct intel_query *query,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600176 uint32_t slot_start, uint32_t slot_count,
177 void *results)
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800178{
179 const uint8_t *ptr;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800180
181 if (!query->obj.mem)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600182 return VK_ERROR_MEMORY_NOT_BOUND;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800183
184 if (intel_mem_is_busy(query->obj.mem))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600185 return VK_NOT_READY;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800186
187 ptr = (const uint8_t *) intel_mem_map_sync(query->obj.mem, false);
188 if (!ptr)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600189 return VK_ERROR_MEMORY_MAP_FAILED;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800190
191 ptr += query->obj.offset + query->slot_stride * slot_start;
192
193 switch (query->type) {
Tony Barbour8205d902015-04-16 15:59:00 -0600194 case VK_QUERY_TYPE_OCCLUSION:
Chia-I Wu659650f2014-08-07 14:11:49 +0800195 query_process_occlusion(query, slot_count, ptr, results);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800196 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600197 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Chia-I Wu659650f2014-08-07 14:11:49 +0800198 query_process_pipeline_statistics(query, slot_count, ptr, results);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800199 break;
200 default:
201 assert(0);
202 break;
203 }
204
205 intel_mem_unmap(query->obj.mem);
206
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600207 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800208}
209
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600210ICD_EXPORT VkResult VKAPI vkCreateQueryPool(
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600211 VkDevice device,
212 const VkQueryPoolCreateInfo* pCreateInfo,
213 VkQueryPool* pQueryPool)
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800214{
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800215 struct intel_dev *dev = intel_dev(device);
216
217 return intel_query_create(dev, pCreateInfo,
218 (struct intel_query **) pQueryPool);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800219}
220
Tony Barbourde4124d2015-07-03 10:33:54 -0600221ICD_EXPORT VkResult VKAPI vkDestroyQueryPool(
222 VkDevice device,
223 VkQueryPool queryPool)
224
225 {
226 struct intel_obj *obj = intel_obj(queryPool.handle);
227
228 obj->destroy(obj);
229 return VK_SUCCESS;
230 }
231
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600232ICD_EXPORT VkResult VKAPI vkGetQueryPoolResults(
Mike Stroyan230e6252015-04-17 12:36:38 -0600233 VkDevice device,
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600234 VkQueryPool queryPool,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600235 uint32_t startQuery,
236 uint32_t queryCount,
237 size_t* pDataSize,
Tony Barbour8205d902015-04-16 15:59:00 -0600238 void* pData,
239 VkQueryResultFlags flags)
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800240{
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800241 struct intel_query *query = intel_query(queryPool);
242
243 switch (query->type) {
Tony Barbour8205d902015-04-16 15:59:00 -0600244 case VK_QUERY_TYPE_OCCLUSION:
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800245 *pDataSize = sizeof(uint64_t) * queryCount;
246 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600247 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Courtney Goeltzenleuchter29862812015-04-16 09:13:59 -0600248 *pDataSize = query->slot_stride * queryCount;
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800249 break;
250 default:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600251 return VK_ERROR_INVALID_HANDLE;
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800252 break;
253 }
254
255 if (pData)
256 return intel_query_get_results(query, startQuery, queryCount, pData);
257 else
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600258 return VK_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800259}