| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* Copyright 2024 Alyssa Rosenzweig |
| 4 |
* Copyright 2022-2023 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "libagx/query.h" |
| 8 |
#include "vulkan/vulkan_core.h" |
| 9 |
#include "agx_helpers.h" |
| 10 |
#include "agx_linker.h" |
| 11 |
#include "agx_pack.h" |
| 12 |
#include "agx_scratch.h" |
| 13 |
#include "agx_tilebuffer.h" |
| 14 |
#include "hk_buffer.h" |
| 15 |
#include "hk_cmd_buffer.h" |
| 16 |
#include "hk_descriptor_set.h" |
| 17 |
#include "hk_device.h" |
| 18 |
#include "hk_entrypoints.h" |
| 19 |
#include "hk_physical_device.h" |
| 20 |
#include "hk_shader.h" |
| 21 |
#include "libagx_dgc.h" |
| 22 |
#include "libagx_shaders.h" |
| 23 |
#include "pool.h" |
| 24 |
|
| 25 |
void |
| 26 |
hk_cmd_buffer_begin_compute(struct hk_cmd_buffer *cmd, |
| 27 |
const VkCommandBufferBeginInfo *pBeginInfo) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
void |
| 32 |
hk_cmd_invalidate_compute_state(struct hk_cmd_buffer *cmd) |
| 33 |
{ |
| 34 |
memset(&cmd->state.cs, 0, sizeof(cmd->state.cs)); |
| 35 |
} |
| 36 |
|
| 37 |
void |
| 38 |
hk_cmd_bind_compute_shader(struct hk_cmd_buffer *cmd, |
| 39 |
struct hk_api_shader *shader) |
| 40 |
{ |
| 41 |
cmd->state.cs.shader = shader; |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
hk_cdm_cache_flush(struct hk_device *dev, struct hk_cs *cs) |
| 46 |
{ |
| 47 |
assert(cs->type == HK_CS_CDM); |
| 48 |
assert(cs->current + AGX_CDM_BARRIER_LENGTH < cs->end && |
| 49 |
"caller must ensure space"); |
| 50 |
|
| 51 |
cs->current = agx_cdm_barrier(cs->current, dev->dev.chip); |
| 52 |
cs->stats.flushes++; |
| 53 |
} |
| 54 |
|
| 55 |
void |
| 56 |
hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs, |
| 57 |
struct agx_cdm_launch_word_0_packed launch, |
| 58 |
uint32_t usc, struct agx_grid grid, |
| 59 |
struct agx_workgroup wg) |
| 60 |
{ |
| 61 |
hk_ensure_cs_has_space(cs->cmd, cs, 0x2000 /* TODO */); |
| 62 |
cs->stats.cmds++; |
| 63 |
|
| 64 |
cs->current = |
| 65 |
agx_cdm_launch(cs->current, dev->dev.chip, grid, wg, launch, usc); |
| 66 |
|
| 67 |
hk_cdm_cache_flush(dev, cs); |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs, |
| 72 |
struct agx_shader_info *info, uint32_t usc, |
| 73 |
struct agx_grid grid, struct agx_workgroup local_size) |
| 74 |
{ |
| 75 |
struct agx_cdm_launch_word_0_packed launch; |
| 76 |
agx_pack(&launch, CDM_LAUNCH_WORD_0, cfg) { |
| 77 |
cfg.texture_state_register_count = info->texture_state_count; |
| 78 |
cfg.sampler_state_register_count = |
| 79 |
agx_translate_sampler_state_count(info->sampler_state_count, false); |
| 80 |
cfg.uniform_register_count = info->push_count; |
| 81 |
cfg.preshader_register_count = info->nr_preamble_gprs; |
| 82 |
} |
| 83 |
|
| 84 |
hk_dispatch_with_usc_launch(dev, cs, launch, usc, grid, local_size); |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
dispatch(struct hk_cmd_buffer *cmd, struct agx_grid grid) |
| 89 |
{ |
| 90 |
struct hk_shader *s = hk_only_variant(cmd->state.cs.shader); |
| 91 |
if (agx_is_shader_empty(&s->b)) |
| 92 |
return; |
| 93 |
|
| 94 |
struct hk_cs *cs = hk_cmd_buffer_get_cs(cmd, true /* compute */); |
| 95 |
if (!cs) |
| 96 |
return; |
| 97 |
|
| 98 |
struct agx_workgroup local_size = |
| 99 |
agx_workgroup(s->b.info.workgroup_size[0], s->b.info.workgroup_size[1], |
| 100 |
s->b.info.workgroup_size[2]); |
| 101 |
|
| 102 |
uint64_t stat = hk_pipeline_stat_addr( |
| 103 |
cmd, VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT); |
| 104 |
|
| 105 |
if (hk_stat_enabled(stat)) { |
| 106 |
perf_debug(cmd, "CS invocation statistic"); |
| 107 |
uint64_t grid = cmd->state.cs.descriptors.root.cs.group_count_addr; |
| 108 |
|
| 109 |
libagx_increment_cs_invocations(cmd, agx_1d(1), AGX_BARRIER_ALL, grid, |
| 110 |
stat, agx_workgroup_threads(local_size)); |
| 111 |
} |
| 112 |
|
| 113 |
if (!agx_is_indirect(grid)) { |
| 114 |
grid.count[0] *= local_size.x; |
| 115 |
grid.count[1] *= local_size.y; |
| 116 |
grid.count[2] *= local_size.z; |
| 117 |
} |
| 118 |
|
| 119 |
hk_dispatch_with_local_size(cmd, cs, s, grid, local_size); |
| 120 |
cs->stats.calls++; |
| 121 |
} |
| 122 |
|
| 123 |
VKAPI_ATTR void VKAPI_CALL |
| 124 |
hk_CmdDispatchBase(VkCommandBuffer commandBuffer, uint32_t baseGroupX, |
| 125 |
uint32_t baseGroupY, uint32_t baseGroupZ, |
| 126 |
uint32_t groupCountX, uint32_t groupCountY, |
| 127 |
uint32_t groupCountZ) |
| 128 |
{ |
| 129 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 130 |
struct hk_descriptor_state *desc = &cmd->state.cs.descriptors; |
| 131 |
if (desc->push_dirty) |
| 132 |
hk_cmd_buffer_flush_push_descriptors(cmd, desc); |
| 133 |
|
| 134 |
desc->root.cs.base_group[0] = baseGroupX; |
| 135 |
desc->root.cs.base_group[1] = baseGroupY; |
| 136 |
desc->root.cs.base_group[2] = baseGroupZ; |
| 137 |
|
| 138 |
/* We don't want to key the shader to whether we're indirectly dispatching, |
| 139 |
* so treat everything as indirect. |
| 140 |
*/ |
| 141 |
VkDispatchIndirectCommand group_count = { |
| 142 |
.x = groupCountX, |
| 143 |
.y = groupCountY, |
| 144 |
.z = groupCountZ, |
| 145 |
}; |
| 146 |
|
| 147 |
desc->root.cs.group_count_addr = |
| 148 |
hk_pool_upload(cmd, &group_count, sizeof(group_count), 8); |
| 149 |
|
| 150 |
dispatch(cmd, agx_3d(groupCountX, groupCountY, groupCountZ)); |
| 151 |
} |
| 152 |
|
| 153 |
VKAPI_ATTR void VKAPI_CALL |
| 154 |
hk_CmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 155 |
VkDeviceSize offset) |
| 156 |
{ |
| 157 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 158 |
VK_FROM_HANDLE(hk_buffer, buffer, _buffer); |
| 159 |
struct hk_descriptor_state *desc = &cmd->state.cs.descriptors; |
| 160 |
if (desc->push_dirty) |
| 161 |
hk_cmd_buffer_flush_push_descriptors(cmd, desc); |
| 162 |
|
| 163 |
desc->root.cs.base_group[0] = 0; |
| 164 |
desc->root.cs.base_group[1] = 0; |
| 165 |
desc->root.cs.base_group[2] = 0; |
| 166 |
|
| 167 |
uint64_t dispatch_addr = hk_buffer_address(buffer, offset, true); |
| 168 |
assert(dispatch_addr != 0); |
| 169 |
|
| 170 |
desc->root.cs.group_count_addr = dispatch_addr; |
| 171 |
dispatch(cmd, agx_grid_indirect(dispatch_addr)); |
| 172 |
} |