| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. and Red Hat Inc. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "util/os_time.h" |
| 7 |
|
| 8 |
#include "libpan_dgc.h" |
| 9 |
#include "nir_builder.h" |
| 10 |
|
| 11 |
#include "vk_log.h" |
| 12 |
#include "vk_meta.h" |
| 13 |
#include "vk_pipeline.h" |
| 14 |
|
| 15 |
#include "genxml/gen_macros.h" |
| 16 |
|
| 17 |
#include "panvk_buffer.h" |
| 18 |
#include "panvk_cmd_buffer.h" |
| 19 |
#include "panvk_cmd_meta.h" |
| 20 |
#include "panvk_cmd_precomp.h" |
| 21 |
#include "panvk_device.h" |
| 22 |
#include "panvk_entrypoints.h" |
| 23 |
#include "panvk_macros.h" |
| 24 |
#include "panvk_query_pool.h" |
| 25 |
|
| 26 |
#include "libpan.h" |
| 27 |
|
| 28 |
static nir_def * |
| 29 |
panvk_nir_query_report_dev_addr(nir_builder *b, nir_def *pool_addr, |
| 30 |
nir_def *query_stride, nir_def *query) |
| 31 |
{ |
| 32 |
return nir_iadd(b, pool_addr, nir_umul_2x32_64(b, query, query_stride)); |
| 33 |
} |
| 34 |
|
| 35 |
static nir_def * |
| 36 |
panvk_nir_available_dev_addr(nir_builder *b, nir_def *available_addr, |
| 37 |
nir_def *query) |
| 38 |
{ |
| 39 |
nir_def *offset = nir_imul_imm(b, query, sizeof(uint32_t)); |
| 40 |
return nir_iadd(b, available_addr, nir_u2u64(b, offset)); |
| 41 |
} |
| 42 |
|
| 43 |
static void |
| 44 |
panvk_emit_write_job(struct panvk_cmd_buffer *cmd, struct panvk_batch *batch, |
| 45 |
enum mali_write_value_type type, uint64_t addr, |
| 46 |
uint64_t value) |
| 47 |
{ |
| 48 |
struct pan_ptr job = |
| 49 |
pan_pool_alloc_desc(&cmd->desc_pool.base, WRITE_VALUE_JOB); |
| 50 |
|
| 51 |
pan_section_pack(job.cpu, WRITE_VALUE_JOB, PAYLOAD, payload) { |
| 52 |
payload.type = type; |
| 53 |
payload.address = addr; |
| 54 |
payload.immediate_value = value; |
| 55 |
}; |
| 56 |
|
| 57 |
pan_jc_add_job(&batch->vtc_jc, MALI_JOB_TYPE_WRITE_VALUE, true, false, 0, 0, |
| 58 |
&job, false); |
| 59 |
util_dynarray_append(&batch->jobs, job.cpu); |
| 60 |
} |
| 61 |
|
| 62 |
static struct panvk_batch * |
| 63 |
open_batch(struct panvk_cmd_buffer *cmd, bool *had_batch) |
| 64 |
{ |
| 65 |
bool res = cmd->cur_batch != NULL; |
| 66 |
|
| 67 |
if (!res) |
| 68 |
panvk_per_arch(cmd_open_batch)(cmd); |
| 69 |
|
| 70 |
*had_batch = res; |
| 71 |
|
| 72 |
return cmd->cur_batch; |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
close_batch(struct panvk_cmd_buffer *cmd, bool had_batch) |
| 77 |
{ |
| 78 |
if (!had_batch) |
| 79 |
panvk_per_arch(cmd_close_batch)(cmd); |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
panvk_emit_clear_queries(struct panvk_cmd_buffer *cmd, |
| 84 |
struct panvk_query_pool *pool, bool availaible, |
| 85 |
uint32_t first_query, uint32_t query_count) |
| 86 |
{ |
| 87 |
const struct panlib_clear_query_result_args push = { |
| 88 |
.pool_addr = panvk_priv_mem_dev_addr(pool->mem), |
| 89 |
.available_addr = panvk_priv_mem_dev_addr(pool->available_mem), |
| 90 |
.query_stride = pool->query_stride, |
| 91 |
.first_query = first_query, |
| 92 |
.query_count = query_count, |
| 93 |
.report_count = pool->reports_per_query, |
| 94 |
.availaible_value = availaible, |
| 95 |
}; |
| 96 |
|
| 97 |
bool had_batch; |
| 98 |
open_batch(cmd, &had_batch); |
| 99 |
struct panvk_precomp_ctx precomp_ctx = panvk_per_arch(precomp_cs)(cmd); |
| 100 |
panlib_clear_query_result_struct(&precomp_ctx, panlib_1d(query_count), |
| 101 |
PANLIB_BARRIER_NONE, push); |
| 102 |
close_batch(cmd, had_batch); |
| 103 |
} |
| 104 |
|
| 105 |
VKAPI_ATTR void VKAPI_CALL |
| 106 |
panvk_per_arch(CmdResetQueryPool)(VkCommandBuffer commandBuffer, |
| 107 |
VkQueryPool queryPool, uint32_t firstQuery, |
| 108 |
uint32_t queryCount) |
| 109 |
{ |
| 110 |
VK_FROM_HANDLE(panvk_cmd_buffer, cmd, commandBuffer); |
| 111 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 112 |
|
| 113 |
if (queryCount == 0) |
| 114 |
return; |
| 115 |
|
| 116 |
panvk_emit_clear_queries(cmd, pool, false, firstQuery, queryCount); |
| 117 |
} |
| 118 |
|
| 119 |
VKAPI_ATTR void VKAPI_CALL |
| 120 |
panvk_per_arch(CmdWriteTimestamp2)(VkCommandBuffer commandBuffer, |
| 121 |
VkPipelineStageFlags2 stage, |
| 122 |
VkQueryPool queryPool, uint32_t query) |
| 123 |
{ |
| 124 |
UNUSED VK_FROM_HANDLE(panvk_cmd_buffer, cmd, commandBuffer); |
| 125 |
UNUSED VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 126 |
|
| 127 |
panvk_stub(); |
| 128 |
} |
| 129 |
|
| 130 |
VKAPI_ATTR void VKAPI_CALL |
| 131 |
panvk_per_arch(CmdBeginQueryIndexedEXT)(VkCommandBuffer commandBuffer, |
| 132 |
VkQueryPool queryPool, uint32_t query, |
| 133 |
VkQueryControlFlags flags, |
| 134 |
uint32_t index) |
| 135 |
{ |
| 136 |
VK_FROM_HANDLE(panvk_cmd_buffer, cmd, commandBuffer); |
| 137 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 138 |
|
| 139 |
/* TODO: transform feedback */ |
| 140 |
assert(index == 0); |
| 141 |
|
| 142 |
bool had_batch; |
| 143 |
struct panvk_batch *batch = open_batch(cmd, &had_batch); |
| 144 |
uint64_t report_addr = panvk_query_report_dev_addr(pool, query); |
| 145 |
|
| 146 |
switch (pool->vk.query_type) { |
| 147 |
case VK_QUERY_TYPE_OCCLUSION: { |
| 148 |
cmd->state.gfx.occlusion_query.ptr = report_addr; |
| 149 |
cmd->state.gfx.occlusion_query.mode = flags & VK_QUERY_CONTROL_PRECISE_BIT |
| 150 |
? MALI_OCCLUSION_MODE_COUNTER |
| 151 |
: MALI_OCCLUSION_MODE_PREDICATE; |
| 152 |
gfx_state_set_dirty(cmd, OQ); |
| 153 |
|
| 154 |
/* From the Vulkan spec: |
| 155 |
* |
| 156 |
* "When an occlusion query begins, the count of passing samples |
| 157 |
* always starts at zero." |
| 158 |
* |
| 159 |
*/ |
| 160 |
for (unsigned i = 0; i < pool->reports_per_query; i++) { |
| 161 |
panvk_emit_write_job( |
| 162 |
cmd, batch, MALI_WRITE_VALUE_TYPE_IMMEDIATE_64, |
| 163 |
report_addr + i * sizeof(struct panvk_query_report), 0); |
| 164 |
} |
| 165 |
break; |
| 166 |
} |
| 167 |
default: |
| 168 |
UNREACHABLE("Unsupported query type"); |
| 169 |
} |
| 170 |
|
| 171 |
close_batch(cmd, had_batch); |
| 172 |
} |
| 173 |
|
| 174 |
VKAPI_ATTR void VKAPI_CALL |
| 175 |
panvk_per_arch(CmdEndQueryIndexedEXT)(VkCommandBuffer commandBuffer, |
| 176 |
VkQueryPool queryPool, uint32_t query, |
| 177 |
uint32_t index) |
| 178 |
{ |
| 179 |
VK_FROM_HANDLE(panvk_cmd_buffer, cmd, commandBuffer); |
| 180 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 181 |
|
| 182 |
/* TODO: transform feedback */ |
| 183 |
assert(index == 0); |
| 184 |
|
| 185 |
bool end_sync = cmd->cur_batch != NULL; |
| 186 |
|
| 187 |
/* Close to ensure we are sync and flush caches */ |
| 188 |
if (end_sync) |
| 189 |
panvk_per_arch(cmd_close_batch)(cmd); |
| 190 |
|
| 191 |
bool had_batch; |
| 192 |
struct panvk_batch *batch = open_batch(cmd, &had_batch); |
| 193 |
had_batch |= end_sync; |
| 194 |
|
| 195 |
switch (pool->vk.query_type) { |
| 196 |
case VK_QUERY_TYPE_OCCLUSION: { |
| 197 |
cmd->state.gfx.occlusion_query.ptr = 0; |
| 198 |
cmd->state.gfx.occlusion_query.mode = MALI_OCCLUSION_MODE_DISABLED; |
| 199 |
gfx_state_set_dirty(cmd, OQ); |
| 200 |
break; |
| 201 |
} |
| 202 |
default: |
| 203 |
UNREACHABLE("Unsupported query type"); |
| 204 |
} |
| 205 |
|
| 206 |
uint64_t available_addr = panvk_query_available_dev_addr(pool, query); |
| 207 |
panvk_emit_write_job(cmd, batch, MALI_WRITE_VALUE_TYPE_IMMEDIATE_32, |
| 208 |
available_addr, 1); |
| 209 |
|
| 210 |
close_batch(cmd, had_batch); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
panvk_meta_copy_query_pool_results(struct panvk_cmd_buffer *cmd, |
| 215 |
struct panvk_query_pool *pool, |
| 216 |
uint32_t first_query, uint32_t query_count, |
| 217 |
uint64_t dst_addr, uint64_t dst_stride, |
| 218 |
VkQueryResultFlags flags) |
| 219 |
{ |
| 220 |
const struct panlib_copy_query_result_args push = { |
| 221 |
.pool_addr = panvk_priv_mem_dev_addr(pool->mem), |
| 222 |
.available_addr = panvk_priv_mem_dev_addr(pool->available_mem), |
| 223 |
.query_stride = pool->query_stride, |
| 224 |
.first_query = first_query, |
| 225 |
.query_count = query_count, |
| 226 |
.dst_addr = dst_addr, |
| 227 |
.dst_stride = dst_stride, |
| 228 |
.query_type = pool->vk.query_type, |
| 229 |
.flags = flags, |
| 230 |
.report_count = pool->reports_per_query, |
| 231 |
}; |
| 232 |
|
| 233 |
bool had_batch; |
| 234 |
open_batch(cmd, &had_batch); |
| 235 |
struct panvk_precomp_ctx precomp_ctx = panvk_per_arch(precomp_cs)(cmd); |
| 236 |
panlib_copy_query_result_struct(&precomp_ctx, panlib_1d(query_count), |
| 237 |
PANLIB_BARRIER_NONE, push); |
| 238 |
close_batch(cmd, had_batch); |
| 239 |
} |
| 240 |
|
| 241 |
VKAPI_ATTR void VKAPI_CALL |
| 242 |
panvk_per_arch(CmdCopyQueryPoolResults)( |
| 243 |
VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, |
| 244 |
uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset, |
| 245 |
VkDeviceSize stride, VkQueryResultFlags flags) |
| 246 |
{ |
| 247 |
VK_FROM_HANDLE(panvk_cmd_buffer, cmd, commandBuffer); |
| 248 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 249 |
VK_FROM_HANDLE(panvk_buffer, dst_buffer, dstBuffer); |
| 250 |
|
| 251 |
/* XXX: Do we really need that barrier when EndQuery already handle it? */ |
| 252 |
if ((flags & VK_QUERY_RESULT_WAIT_BIT) && cmd->cur_batch != NULL) { |
| 253 |
close_batch(cmd, true); |
| 254 |
} |
| 255 |
|
| 256 |
uint64_t dst_addr = panvk_buffer_gpu_ptr(dst_buffer, dstOffset); |
| 257 |
panvk_meta_copy_query_pool_results(cmd, pool, firstQuery, queryCount, |
| 258 |
dst_addr, stride, flags); |
| 259 |
} |