| 1 |
/* |
| 2 |
* Copyright © 2022 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "pvr_cmd_buffer.h" |
| 8 |
#include "pvr_entrypoints.h" |
| 9 |
#include "pvr_hw_pass.h" |
| 10 |
#include "pvr_macros.h" |
| 11 |
#include "pvr_pass.h" |
| 12 |
#include "pvr_query.h" |
| 13 |
|
| 14 |
void PVR_PER_ARCH(CmdResetQueryPool)(VkCommandBuffer commandBuffer, |
| 15 |
VkQueryPool queryPool, |
| 16 |
uint32_t firstQuery, |
| 17 |
uint32_t queryCount) |
| 18 |
{ |
| 19 |
VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer); |
| 20 |
struct pvr_query_info query_info; |
| 21 |
VkResult result; |
| 22 |
|
| 23 |
PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer); |
| 24 |
|
| 25 |
if (queryCount == 0) |
| 26 |
return; |
| 27 |
|
| 28 |
query_info.type = PVR_QUERY_TYPE_RESET_QUERY_POOL; |
| 29 |
|
| 30 |
query_info.reset_query_pool.query_pool = queryPool; |
| 31 |
query_info.reset_query_pool.first_query = firstQuery; |
| 32 |
query_info.reset_query_pool.query_count = queryCount; |
| 33 |
|
| 34 |
/* make the query-reset program wait for previous geom/frag, |
| 35 |
* to not overwrite them |
| 36 |
*/ |
| 37 |
result = |
| 38 |
pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT); |
| 39 |
if (result != VK_SUCCESS) |
| 40 |
return; |
| 41 |
|
| 42 |
cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){ |
| 43 |
.type = PVR_EVENT_TYPE_BARRIER, |
| 44 |
.barrier = { |
| 45 |
.wait_for_stage_mask = PVR_PIPELINE_STAGE_ALL_GRAPHICS_BITS, |
| 46 |
.wait_at_stage_mask = PVR_PIPELINE_STAGE_QUERY_BIT, |
| 47 |
}, |
| 48 |
}; |
| 49 |
|
| 50 |
/* add the query-program itself */ |
| 51 |
result = pvr_arch_add_query_program(cmd_buffer, &query_info); |
| 52 |
if (result != VK_SUCCESS) |
| 53 |
return; |
| 54 |
|
| 55 |
/* make future geom/frag wait for the query-reset program to |
| 56 |
* reset the counters to 0 |
| 57 |
*/ |
| 58 |
result = |
| 59 |
pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT); |
| 60 |
if (result != VK_SUCCESS) |
| 61 |
return; |
| 62 |
|
| 63 |
cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){ |
| 64 |
.type = PVR_EVENT_TYPE_BARRIER, |
| 65 |
.barrier = { |
| 66 |
.wait_for_stage_mask = PVR_PIPELINE_STAGE_QUERY_BIT, |
| 67 |
.wait_at_stage_mask = PVR_PIPELINE_STAGE_ALL_GRAPHICS_BITS, |
| 68 |
}, |
| 69 |
}; |
| 70 |
} |
| 71 |
|
| 72 |
void PVR_PER_ARCH(CmdCopyQueryPoolResults)(VkCommandBuffer commandBuffer, |
| 73 |
VkQueryPool queryPool, |
| 74 |
uint32_t firstQuery, |
| 75 |
uint32_t queryCount, |
| 76 |
VkBuffer dstBuffer, |
| 77 |
VkDeviceSize dstOffset, |
| 78 |
VkDeviceSize stride, |
| 79 |
VkQueryResultFlags flags) |
| 80 |
{ |
| 81 |
VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer); |
| 82 |
struct pvr_query_info query_info; |
| 83 |
VkResult result; |
| 84 |
|
| 85 |
PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer); |
| 86 |
|
| 87 |
if (queryCount == 0) |
| 88 |
return; |
| 89 |
|
| 90 |
query_info.type = PVR_QUERY_TYPE_COPY_QUERY_RESULTS; |
| 91 |
|
| 92 |
query_info.copy_query_results.query_pool = queryPool; |
| 93 |
query_info.copy_query_results.first_query = firstQuery; |
| 94 |
query_info.copy_query_results.query_count = queryCount; |
| 95 |
query_info.copy_query_results.dst_buffer = dstBuffer; |
| 96 |
query_info.copy_query_results.dst_offset = dstOffset; |
| 97 |
query_info.copy_query_results.stride = stride; |
| 98 |
query_info.copy_query_results.flags = flags; |
| 99 |
|
| 100 |
result = |
| 101 |
pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT); |
| 102 |
if (result != VK_SUCCESS) |
| 103 |
return; |
| 104 |
|
| 105 |
/* The Vulkan 1.3.231 spec says: |
| 106 |
* |
| 107 |
* "vkCmdCopyQueryPoolResults is considered to be a transfer operation, |
| 108 |
* and its writes to buffer memory must be synchronized using |
| 109 |
* VK_PIPELINE_STAGE_TRANSFER_BIT and VK_ACCESS_TRANSFER_WRITE_BIT before |
| 110 |
* using the results." |
| 111 |
* |
| 112 |
*/ |
| 113 |
/* We record barrier event sub commands to sync the compute job used for the |
| 114 |
* copy query results program with transfer jobs to prevent an overlapping |
| 115 |
* transfer job with the compute job. |
| 116 |
*/ |
| 117 |
|
| 118 |
cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){ |
| 119 |
.type = PVR_EVENT_TYPE_BARRIER, |
| 120 |
.barrier = { |
| 121 |
.wait_for_stage_mask = PVR_PIPELINE_STAGE_ALL_GRAPHICS_BITS | PVR_PIPELINE_STAGE_TRANSFER_BIT, |
| 122 |
.wait_at_stage_mask = PVR_PIPELINE_STAGE_QUERY_BIT, |
| 123 |
}, |
| 124 |
}; |
| 125 |
|
| 126 |
result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); |
| 127 |
if (result != VK_SUCCESS) |
| 128 |
return; |
| 129 |
|
| 130 |
pvr_arch_add_query_program(cmd_buffer, &query_info); |
| 131 |
|
| 132 |
result = |
| 133 |
pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT); |
| 134 |
if (result != VK_SUCCESS) |
| 135 |
return; |
| 136 |
|
| 137 |
cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){ |
| 138 |
.type = PVR_EVENT_TYPE_BARRIER, |
| 139 |
.barrier = { |
| 140 |
.wait_for_stage_mask = PVR_PIPELINE_STAGE_QUERY_BIT, |
| 141 |
.wait_at_stage_mask = PVR_PIPELINE_STAGE_ALL_GRAPHICS_BITS | PVR_PIPELINE_STAGE_TRANSFER_BIT, |
| 142 |
}, |
| 143 |
}; |
| 144 |
} |
| 145 |
|
| 146 |
static inline const uint32_t |
| 147 |
pvr_cmd_buffer_state_get_view_count(const struct pvr_cmd_buffer_state *state) |
| 148 |
{ |
| 149 |
const struct pvr_render_pass_info *render_pass_info = |
| 150 |
&state->render_pass_info; |
| 151 |
const struct pvr_sub_cmd_gfx *gfx_sub_cmd = &state->current_sub_cmd->gfx; |
| 152 |
const uint32_t hw_render_idx = gfx_sub_cmd->hw_render_idx; |
| 153 |
const struct pvr_renderpass_hwsetup_render *hw_render = |
| 154 |
pvr_arch_pass_info_get_hw_render(render_pass_info, hw_render_idx); |
| 155 |
const uint32_t view_count = util_bitcount(hw_render->view_mask); |
| 156 |
|
| 157 |
assert(state->current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS); |
| 158 |
/* hw_render view masks have 1 bit set at least. */ |
| 159 |
assert(view_count); |
| 160 |
|
| 161 |
return view_count; |
| 162 |
} |
| 163 |
|
| 164 |
void PVR_PER_ARCH(CmdBeginQuery)(VkCommandBuffer commandBuffer, |
| 165 |
VkQueryPool queryPool, |
| 166 |
uint32_t query, |
| 167 |
VkQueryControlFlags flags) |
| 168 |
{ |
| 169 |
VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer); |
| 170 |
struct pvr_cmd_buffer_state *state = &cmd_buffer->state; |
| 171 |
uint32_t view_count = 1; |
| 172 |
VK_FROM_HANDLE(pvr_query_pool, pool, queryPool); |
| 173 |
|
| 174 |
PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer); |
| 175 |
|
| 176 |
/* Occlusion queries can't be nested. */ |
| 177 |
assert(!state->vis_test_enabled); |
| 178 |
|
| 179 |
if (state->current_sub_cmd && |
| 180 |
state->current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS) { |
| 181 |
if (!state->current_sub_cmd->gfx.query_pool) { |
| 182 |
state->current_sub_cmd->gfx.query_pool = pool; |
| 183 |
} else if (state->current_sub_cmd->gfx.query_pool != pool) { |
| 184 |
VkResult result; |
| 185 |
|
| 186 |
/* Kick render. */ |
| 187 |
state->current_sub_cmd->gfx.barrier_store = true; |
| 188 |
|
| 189 |
result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); |
| 190 |
if (result != VK_SUCCESS) |
| 191 |
return; |
| 192 |
|
| 193 |
result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, |
| 194 |
PVR_SUB_CMD_TYPE_GRAPHICS); |
| 195 |
if (result != VK_SUCCESS) |
| 196 |
return; |
| 197 |
|
| 198 |
/* Use existing render setup, but load color attachments from HW |
| 199 |
* BGOBJ. |
| 200 |
*/ |
| 201 |
state->current_sub_cmd->gfx.barrier_load = true; |
| 202 |
state->current_sub_cmd->gfx.barrier_store = false; |
| 203 |
state->current_sub_cmd->gfx.query_pool = pool; |
| 204 |
} |
| 205 |
|
| 206 |
view_count = pvr_cmd_buffer_state_get_view_count(state); |
| 207 |
} |
| 208 |
|
| 209 |
state->query_pool = pool; |
| 210 |
state->vis_test_enabled = true; |
| 211 |
state->vis_reg = query; |
| 212 |
state->dirty.vis_test = true; |
| 213 |
|
| 214 |
/* Add the index to the list for this render. */ |
| 215 |
for (uint32_t i = 0; i < view_count; i++) { |
| 216 |
util_dynarray_append(&state->query_indices, query); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void PVR_PER_ARCH(CmdEndQuery)(VkCommandBuffer commandBuffer, |
| 221 |
VkQueryPool queryPool, |
| 222 |
uint32_t query) |
| 223 |
{ |
| 224 |
VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer); |
| 225 |
struct pvr_cmd_buffer_state *state = &cmd_buffer->state; |
| 226 |
|
| 227 |
PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer); |
| 228 |
|
| 229 |
state->vis_test_enabled = false; |
| 230 |
state->dirty.vis_test = true; |
| 231 |
} |