Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 3 | * |
| 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 Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include "dev.h" |
| 29 | #include "mem.h" |
| 30 | #include "query.h" |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 31 | #include "genhw/genhw.h" |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 32 | |
| 33 | static void query_destroy(struct intel_obj *obj) |
| 34 | { |
| 35 | struct intel_query *query = intel_query_from_obj(obj); |
| 36 | |
Tony Barbour | 2094dc7 | 2015-07-09 15:26:32 -0600 | [diff] [blame] | 37 | intel_mem_free(obj->mem); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 38 | intel_query_destroy(query); |
| 39 | } |
| 40 | |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 41 | static 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 Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 80 | VkResult intel_query_create(struct intel_dev *dev, |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 81 | const VkQueryPoolCreateInfo *info, |
| 82 | struct intel_query **query_ret) |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 83 | { |
| 84 | struct intel_query *query; |
| 85 | |
Chia-I Wu | 545c2e1 | 2015-02-22 13:19:54 +0800 | [diff] [blame] | 86 | query = (struct intel_query *) intel_base_create(&dev->base.handle, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 87 | sizeof(*query), dev->base.dbg, VK_OBJECT_TYPE_QUEUE, |
Chia-I Wu | 545c2e1 | 2015-02-22 13:19:54 +0800 | [diff] [blame] | 88 | info, 0); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 89 | if (!query) |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 90 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 91 | |
| 92 | query->type = info->queryType; |
| 93 | query->slot_count = info->slots; |
| 94 | |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 95 | /* |
| 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 Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 100 | switch (info->queryType) { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 101 | case VK_QUERY_TYPE_OCCLUSION: |
Chia-I Wu | 759fa2e | 2014-08-30 18:44:47 +0800 | [diff] [blame] | 102 | query->slot_stride = u_align(sizeof(uint64_t) * 2, 64); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 103 | break; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 104 | case VK_QUERY_TYPE_PIPELINE_STATISTICS: |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 105 | query_init_pipeline_statistics(dev, info, query); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | if (!query->slot_stride) { |
| 112 | intel_query_destroy(query); |
Courtney Goeltzenleuchter | a54b76a | 2015-09-04 13:39:59 -0600 | [diff] [blame] | 113 | /* TODOVV: Move test to validation layer */ |
| 114 | // return VK_ERROR_INVALID_VALUE; |
| 115 | return VK_ERROR_UNKNOWN; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Tony Barbour | 2094dc7 | 2015-07-09 15:26:32 -0600 | [diff] [blame] | 118 | VkMemoryAllocInfo mem_reqs; |
| 119 | mem_reqs.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 120 | mem_reqs.allocationSize = query->slot_stride * query->slot_count; |
| 121 | mem_reqs.pNext = NULL; |
| 122 | mem_reqs.memoryTypeIndex = 0; |
| 123 | intel_mem_alloc(dev, &mem_reqs, &query->obj.mem); |
| 124 | |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 125 | query->obj.destroy = query_destroy; |
| 126 | |
| 127 | *query_ret = query; |
| 128 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 129 | return VK_SUCCESS; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void intel_query_destroy(struct intel_query *query) |
| 133 | { |
| 134 | intel_base_destroy(&query->obj.base); |
| 135 | } |
| 136 | |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 137 | static void |
| 138 | query_process_occlusion(const struct intel_query *query, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 139 | uint32_t count, const uint8_t *raw, |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 140 | uint64_t *results) |
| 141 | { |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 142 | uint32_t i; |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 143 | |
| 144 | for (i = 0; i < count; i++) { |
| 145 | const uint32_t *pair = (const uint32_t *) raw; |
| 146 | |
| 147 | results[i] = pair[1] - pair[0]; |
| 148 | raw += query->slot_stride; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | query_process_pipeline_statistics(const struct intel_query *query, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 154 | uint32_t count, const uint8_t *raw, |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 155 | void *results) |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 156 | { |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 157 | const uint32_t num_regs = query->reg_count; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 158 | uint32_t i, j; |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 159 | |
| 160 | for (i = 0; i < count; i++) { |
| 161 | const uint64_t *before = (const uint64_t *) raw; |
| 162 | const uint64_t *after = before + num_regs; |
| 163 | uint64_t *dst = (uint64_t *) (results + i); |
| 164 | |
| 165 | for (j = 0; j < num_regs; j++) |
| 166 | dst[j] = after[j] - before[j]; |
| 167 | |
| 168 | raw += query->slot_stride; |
| 169 | } |
| 170 | } |
| 171 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 172 | VkResult intel_query_get_results(struct intel_query *query, |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 173 | uint32_t slot_start, uint32_t slot_count, |
| 174 | void *results) |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 175 | { |
| 176 | const uint8_t *ptr; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 177 | |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 178 | if (intel_mem_is_busy(query->obj.mem)) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 179 | return VK_NOT_READY; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 180 | |
| 181 | ptr = (const uint8_t *) intel_mem_map_sync(query->obj.mem, false); |
| 182 | if (!ptr) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 183 | return VK_ERROR_MEMORY_MAP_FAILED; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 184 | |
| 185 | ptr += query->obj.offset + query->slot_stride * slot_start; |
| 186 | |
| 187 | switch (query->type) { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 188 | case VK_QUERY_TYPE_OCCLUSION: |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 189 | query_process_occlusion(query, slot_count, ptr, results); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 190 | break; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 191 | case VK_QUERY_TYPE_PIPELINE_STATISTICS: |
Chia-I Wu | 659650f | 2014-08-07 14:11:49 +0800 | [diff] [blame] | 192 | query_process_pipeline_statistics(query, slot_count, ptr, results); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 193 | break; |
| 194 | default: |
| 195 | assert(0); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | intel_mem_unmap(query->obj.mem); |
| 200 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 201 | return VK_SUCCESS; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 204 | ICD_EXPORT VkResult VKAPI vkCreateQueryPool( |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 205 | VkDevice device, |
| 206 | const VkQueryPoolCreateInfo* pCreateInfo, |
| 207 | VkQueryPool* pQueryPool) |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 208 | { |
Chia-I Wu | f667a6a | 2014-08-07 14:15:01 +0800 | [diff] [blame] | 209 | struct intel_dev *dev = intel_dev(device); |
| 210 | |
| 211 | return intel_query_create(dev, pCreateInfo, |
| 212 | (struct intel_query **) pQueryPool); |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 213 | } |
| 214 | |
Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame^] | 215 | ICD_EXPORT void VKAPI vkDestroyQueryPool( |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 216 | VkDevice device, |
| 217 | VkQueryPool queryPool) |
| 218 | |
| 219 | { |
| 220 | struct intel_obj *obj = intel_obj(queryPool.handle); |
| 221 | |
| 222 | obj->destroy(obj); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 223 | } |
| 224 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 225 | ICD_EXPORT VkResult VKAPI vkGetQueryPoolResults( |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 226 | VkDevice device, |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 227 | VkQueryPool queryPool, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 228 | uint32_t startQuery, |
| 229 | uint32_t queryCount, |
| 230 | size_t* pDataSize, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 231 | void* pData, |
| 232 | VkQueryResultFlags flags) |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 233 | { |
Chia-I Wu | f667a6a | 2014-08-07 14:15:01 +0800 | [diff] [blame] | 234 | struct intel_query *query = intel_query(queryPool); |
| 235 | |
| 236 | switch (query->type) { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 237 | case VK_QUERY_TYPE_OCCLUSION: |
Chia-I Wu | f667a6a | 2014-08-07 14:15:01 +0800 | [diff] [blame] | 238 | *pDataSize = sizeof(uint64_t) * queryCount; |
| 239 | break; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 240 | case VK_QUERY_TYPE_PIPELINE_STATISTICS: |
Courtney Goeltzenleuchter | 2986281 | 2015-04-16 09:13:59 -0600 | [diff] [blame] | 241 | *pDataSize = query->slot_stride * queryCount; |
Chia-I Wu | f667a6a | 2014-08-07 14:15:01 +0800 | [diff] [blame] | 242 | break; |
| 243 | default: |
Courtney Goeltzenleuchter | a54b76a | 2015-09-04 13:39:59 -0600 | [diff] [blame] | 244 | /* TODOVV: Move test to validation layer */ |
| 245 | // return VK_ERROR_INVALID_HANDLE; |
| 246 | return VK_ERROR_UNKNOWN; |
Chia-I Wu | f667a6a | 2014-08-07 14:15:01 +0800 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | |
| 250 | if (pData) |
| 251 | return intel_query_get_results(query, startQuery, queryCount, pData); |
| 252 | else |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 253 | return VK_SUCCESS; |
Chia-I Wu | e18ff1b | 2014-08-07 13:38:51 +0800 | [diff] [blame] | 254 | } |