| 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 "asahi/compiler/agx_nir_texture.h" |
| 8 |
#include "util/format/u_format.h" |
| 9 |
#include "util/format/u_formats.h" |
| 10 |
#include "util/u_math.h" |
| 11 |
#include "vulkan/vulkan_core.h" |
| 12 |
#include "agx_pack.h" |
| 13 |
#include "hk_buffer.h" |
| 14 |
#include "hk_cmd_buffer.h" |
| 15 |
#include "hk_device.h" |
| 16 |
#include "hk_entrypoints.h" |
| 17 |
#include "hk_image.h" |
| 18 |
#include "hk_physical_device.h" |
| 19 |
|
| 20 |
#include "layout.h" |
| 21 |
#include "libagx_dgc.h" |
| 22 |
#include "libagx_shaders.h" |
| 23 |
#include "nir_builder.h" |
| 24 |
#include "nir_builder_opcodes.h" |
| 25 |
#include "nir_format_convert.h" |
| 26 |
#include "shader_enums.h" |
| 27 |
#include "vk_enum_to_str.h" |
| 28 |
#include "vk_format.h" |
| 29 |
#include "vk_meta.h" |
| 30 |
#include "vk_pipeline.h" |
| 31 |
|
| 32 |
/* For block based blit kernels, we hardcode the maximum tile size which we can |
| 33 |
* always achieve. This simplifies our life. |
| 34 |
*/ |
| 35 |
#define TILE_WIDTH 32 |
| 36 |
#define TILE_HEIGHT 32 |
| 37 |
|
| 38 |
static VkResult |
| 39 |
hk_cmd_bind_map_buffer(struct vk_command_buffer *vk_cmd, |
| 40 |
struct vk_meta_device *meta, VkBuffer _buffer, |
| 41 |
void **map_out) |
| 42 |
{ |
| 43 |
struct hk_cmd_buffer *cmd = container_of(vk_cmd, struct hk_cmd_buffer, vk); |
| 44 |
VK_FROM_HANDLE(hk_buffer, buffer, _buffer); |
| 45 |
|
| 46 |
assert(buffer->vk.size < UINT_MAX); |
| 47 |
struct agx_ptr T = hk_pool_alloc(cmd, buffer->vk.size, 16); |
| 48 |
if (unlikely(T.cpu == NULL)) |
| 49 |
return VK_ERROR_OUT_OF_POOL_MEMORY; |
| 50 |
|
| 51 |
assert(buffer->vk.device_address == 0); |
| 52 |
buffer->vk.device_address = T.gpu; |
| 53 |
*map_out = T.cpu; |
| 54 |
return VK_SUCCESS; |
| 55 |
} |
| 56 |
|
| 57 |
VkResult |
| 58 |
hk_device_init_meta(struct hk_device *dev) |
| 59 |
{ |
| 60 |
VkResult result = vk_meta_device_init(&dev->vk, &dev->meta); |
| 61 |
if (result != VK_SUCCESS) |
| 62 |
return result; |
| 63 |
|
| 64 |
dev->meta.use_gs_for_layer = false; |
| 65 |
dev->meta.use_stencil_export = true; |
| 66 |
dev->meta.use_rect_list_pipeline = true; |
| 67 |
dev->meta.cmd_bind_map_buffer = hk_cmd_bind_map_buffer; |
| 68 |
dev->meta.max_bind_map_buffer_size_B = 64 * 1024; |
| 69 |
|
| 70 |
for (unsigned i = 0; i < VK_META_BUFFER_CHUNK_SIZE_COUNT; ++i) { |
| 71 |
dev->meta.buffer_access.optimal_wg_size[i] = 64; |
| 72 |
} |
| 73 |
|
| 74 |
return VK_SUCCESS; |
| 75 |
} |
| 76 |
|
| 77 |
void |
| 78 |
hk_device_finish_meta(struct hk_device *dev) |
| 79 |
{ |
| 80 |
vk_meta_device_finish(&dev->vk, &dev->meta); |
| 81 |
} |
| 82 |
|
| 83 |
struct hk_meta_save { |
| 84 |
struct vk_vertex_input_state _dynamic_vi; |
| 85 |
struct vk_sample_locations_state _dynamic_sl; |
| 86 |
struct vk_dynamic_graphics_state dynamic; |
| 87 |
struct hk_api_shader *shaders[MESA_SHADER_MESH + 1]; |
| 88 |
struct hk_addr_range vb0; |
| 89 |
struct hk_descriptor_set *desc0; |
| 90 |
bool has_push_desc0; |
| 91 |
enum agx_visibility_mode occlusion; |
| 92 |
struct hk_push_descriptor_set push_desc0; |
| 93 |
VkQueryPipelineStatisticFlags pipeline_stats_flags; |
| 94 |
uint8_t push[HK_MAX_PUSH_SIZE]; |
| 95 |
}; |
| 96 |
|
| 97 |
static void |
| 98 |
hk_meta_begin(struct hk_cmd_buffer *cmd, struct hk_meta_save *save, |
| 99 |
VkPipelineBindPoint bind_point) |
| 100 |
{ |
| 101 |
struct hk_descriptor_state *desc = hk_get_descriptors_state(cmd, bind_point); |
| 102 |
|
| 103 |
if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 104 |
save->dynamic = cmd->vk.dynamic_graphics_state; |
| 105 |
save->_dynamic_vi = cmd->state.gfx._dynamic_vi; |
| 106 |
save->_dynamic_sl = cmd->state.gfx._dynamic_sl; |
| 107 |
|
| 108 |
static_assert(sizeof(cmd->state.gfx.shaders) == sizeof(save->shaders)); |
| 109 |
memcpy(save->shaders, cmd->state.gfx.shaders, sizeof(save->shaders)); |
| 110 |
|
| 111 |
/* Pause queries */ |
| 112 |
save->occlusion = cmd->state.gfx.occlusion.mode; |
| 113 |
cmd->state.gfx.occlusion.mode = AGX_VISIBILITY_MODE_NONE; |
| 114 |
cmd->state.gfx.dirty |= HK_DIRTY_OCCLUSION; |
| 115 |
|
| 116 |
save->pipeline_stats_flags = desc->root.draw.pipeline_stats_flags; |
| 117 |
desc->root.draw.pipeline_stats_flags = 0; |
| 118 |
desc->root_dirty = true; |
| 119 |
} else { |
| 120 |
save->shaders[MESA_SHADER_COMPUTE] = cmd->state.cs.shader; |
| 121 |
} |
| 122 |
|
| 123 |
save->vb0 = cmd->state.gfx.vb[0]; |
| 124 |
|
| 125 |
save->desc0 = desc->sets[0]; |
| 126 |
save->has_push_desc0 = desc->push[0]; |
| 127 |
if (save->has_push_desc0) |
| 128 |
save->push_desc0 = *desc->push[0]; |
| 129 |
|
| 130 |
static_assert(sizeof(save->push) == sizeof(desc->root.push)); |
| 131 |
memcpy(save->push, desc->root.push, sizeof(save->push)); |
| 132 |
|
| 133 |
cmd->in_meta = true; |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
hk_meta_init_render(struct hk_cmd_buffer *cmd, |
| 138 |
struct vk_meta_rendering_info *info) |
| 139 |
{ |
| 140 |
const struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 141 |
|
| 142 |
*info = (struct vk_meta_rendering_info){ |
| 143 |
.samples = MAX2(render->tilebuffer.nr_samples, 1), |
| 144 |
.view_mask = render->view_mask, |
| 145 |
.color_attachment_count = render->color_att_count, |
| 146 |
.depth_attachment_format = render->depth_att.vk_format, |
| 147 |
.stencil_attachment_format = render->stencil_att.vk_format, |
| 148 |
}; |
| 149 |
for (uint32_t a = 0; a < render->color_att_count; a++) { |
| 150 |
info->color_attachment_formats[a] = render->color_att[a].vk_format; |
| 151 |
info->color_attachment_write_masks[a] = |
| 152 |
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | |
| 153 |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
hk_meta_end(struct hk_cmd_buffer *cmd, struct hk_meta_save *save, |
| 159 |
VkPipelineBindPoint bind_point) |
| 160 |
{ |
| 161 |
struct hk_descriptor_state *desc = hk_get_descriptors_state(cmd, bind_point); |
| 162 |
desc->root_dirty = true; |
| 163 |
|
| 164 |
if (save->desc0) { |
| 165 |
desc->sets[0] = save->desc0; |
| 166 |
desc->root.sets[0] = hk_descriptor_set_addr(save->desc0); |
| 167 |
desc->sets_dirty |= BITFIELD_BIT(0); |
| 168 |
desc->push_dirty &= ~BITFIELD_BIT(0); |
| 169 |
} else if (save->has_push_desc0) { |
| 170 |
*desc->push[0] = save->push_desc0; |
| 171 |
desc->push_dirty |= BITFIELD_BIT(0); |
| 172 |
} |
| 173 |
|
| 174 |
if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 175 |
/* Restore the dynamic state */ |
| 176 |
assert(save->dynamic.vi == &cmd->state.gfx._dynamic_vi); |
| 177 |
assert(save->dynamic.ms.sample_locations == &cmd->state.gfx._dynamic_sl); |
| 178 |
cmd->vk.dynamic_graphics_state = save->dynamic; |
| 179 |
cmd->state.gfx._dynamic_vi = save->_dynamic_vi; |
| 180 |
cmd->state.gfx._dynamic_sl = save->_dynamic_sl; |
| 181 |
memcpy(cmd->vk.dynamic_graphics_state.dirty, |
| 182 |
cmd->vk.dynamic_graphics_state.set, |
| 183 |
sizeof(cmd->vk.dynamic_graphics_state.set)); |
| 184 |
|
| 185 |
for (uint32_t stage = 0; stage < ARRAY_SIZE(save->shaders); stage++) { |
| 186 |
hk_cmd_bind_graphics_shader(cmd, stage, save->shaders[stage]); |
| 187 |
} |
| 188 |
|
| 189 |
hk_cmd_bind_vertex_buffer(cmd, 0, save->vb0); |
| 190 |
|
| 191 |
/* Restore queries */ |
| 192 |
cmd->state.gfx.occlusion.mode = save->occlusion; |
| 193 |
cmd->state.gfx.dirty |= HK_DIRTY_OCCLUSION; |
| 194 |
|
| 195 |
desc->root.draw.pipeline_stats_flags = save->pipeline_stats_flags; |
| 196 |
desc->root_dirty = true; |
| 197 |
} else { |
| 198 |
hk_cmd_bind_compute_shader(cmd, save->shaders[MESA_SHADER_COMPUTE]); |
| 199 |
} |
| 200 |
|
| 201 |
memcpy(desc->root.push, save->push, sizeof(save->push)); |
| 202 |
cmd->in_meta = false; |
| 203 |
} |
| 204 |
|
| 205 |
#define BINDING_OUTPUT 0 |
| 206 |
#define BINDING_INPUT 1 |
| 207 |
|
| 208 |
static VkFormat |
| 209 |
aspect_format(VkFormat fmt, VkImageAspectFlags aspect) |
| 210 |
{ |
| 211 |
bool depth = (aspect & VK_IMAGE_ASPECT_DEPTH_BIT); |
| 212 |
bool stencil = (aspect & VK_IMAGE_ASPECT_STENCIL_BIT); |
| 213 |
|
| 214 |
assert(!(depth && stencil)); |
| 215 |
|
| 216 |
switch (fmt) { |
| 217 |
case VK_FORMAT_D32_SFLOAT: |
| 218 |
return VK_FORMAT_R32_SFLOAT; |
| 219 |
case VK_FORMAT_D32_SFLOAT_S8_UINT: |
| 220 |
return stencil ? VK_FORMAT_R8_UINT : VK_FORMAT_R32_SFLOAT; |
| 221 |
case VK_FORMAT_D16_UNORM: |
| 222 |
return VK_FORMAT_R16_UNORM; |
| 223 |
case VK_FORMAT_S8_UINT: |
| 224 |
return VK_FORMAT_R8_UINT; |
| 225 |
case VK_FORMAT_D16_UNORM_S8_UINT: |
| 226 |
return stencil ? VK_FORMAT_R8_UINT : VK_FORMAT_R16_UNORM; |
| 227 |
default: |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
const struct vk_format_ycbcr_info *ycbcr_info = |
| 232 |
vk_format_get_ycbcr_info(fmt); |
| 233 |
|
| 234 |
if (ycbcr_info) { |
| 235 |
switch (aspect) { |
| 236 |
case VK_IMAGE_ASPECT_PLANE_0_BIT: |
| 237 |
return ycbcr_info->planes[0].format; |
| 238 |
case VK_IMAGE_ASPECT_PLANE_1_BIT: |
| 239 |
return ycbcr_info->planes[1].format; |
| 240 |
case VK_IMAGE_ASPECT_PLANE_2_BIT: |
| 241 |
return ycbcr_info->planes[2].format; |
| 242 |
default: |
| 243 |
UNREACHABLE("invalid ycbcr aspect"); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return fmt; |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Canonicalize formats to simplify the copies. The returned format must in the |
| 252 |
* same compression class, and should roundtrip lossless (minifloat formats are |
| 253 |
* the unfortunate exception). |
| 254 |
*/ |
| 255 |
static enum pipe_format |
| 256 |
canonical_format_pipe(enum pipe_format fmt) |
| 257 |
{ |
| 258 |
assert(ail_is_valid_pixel_format(fmt)); |
| 259 |
|
| 260 |
if (util_format_is_compressed(fmt)) { |
| 261 |
unsigned size_B = util_format_get_blocksize(fmt); |
| 262 |
assert(size_B == 8 || size_B == 16); |
| 263 |
|
| 264 |
return size_B == 16 ? PIPE_FORMAT_R32G32B32A32_UINT |
| 265 |
: PIPE_FORMAT_R32G32_UINT; |
| 266 |
} |
| 267 |
|
| 268 |
#define CASE(x, y) [AGX_CHANNELS_##x] = PIPE_FORMAT_##y |
| 269 |
/* clang-format off */ |
| 270 |
static enum pipe_format map[] = { |
| 271 |
CASE(R8, R8_UINT), |
| 272 |
CASE(R16, R16_UNORM /* XXX: Hack for Z16 copies */), |
| 273 |
CASE(R8G8, R8G8_UINT), |
| 274 |
CASE(R5G6B5, R5G6B5_UNORM), |
| 275 |
CASE(R4G4B4A4, R4G4B4A4_UNORM), |
| 276 |
CASE(A1R5G5B5, A1R5G5B5_UNORM), |
| 277 |
CASE(R5G5B5A1, B5G5R5A1_UNORM), |
| 278 |
CASE(R32, R32_UINT), |
| 279 |
CASE(R16G16, R16G16_UINT), |
| 280 |
CASE(R11G11B10, R11G11B10_FLOAT), |
| 281 |
CASE(R10G10B10A2, R10G10B10A2_UNORM), |
| 282 |
CASE(R9G9B9E5, R9G9B9E5_FLOAT), |
| 283 |
CASE(R8G8B8A8, R8G8B8A8_UINT), |
| 284 |
CASE(R32G32, R32G32_UINT), |
| 285 |
CASE(R16G16B16A16, R16G16B16A16_UINT), |
| 286 |
CASE(R32G32B32A32, R32G32B32A32_UINT), |
| 287 |
}; |
| 288 |
/* clang-format on */ |
| 289 |
#undef CASE |
| 290 |
|
| 291 |
enum agx_channels channels = ail_pixel_format[fmt].channels; |
| 292 |
assert(channels < ARRAY_SIZE(map) && "all valid channels handled"); |
| 293 |
assert(map[channels] != PIPE_FORMAT_NONE && "all valid channels handled"); |
| 294 |
return map[channels]; |
| 295 |
} |
| 296 |
|
| 297 |
static VkFormat |
| 298 |
canonical_format(VkFormat fmt) |
| 299 |
{ |
| 300 |
return vk_format_from_pipe_format( |
| 301 |
canonical_format_pipe(hk_format_to_pipe_format(fmt))); |
| 302 |
} |
| 303 |
|
| 304 |
enum copy_type { |
| 305 |
BUF2IMG, |
| 306 |
IMG2BUF, |
| 307 |
IMG2IMG, |
| 308 |
}; |
| 309 |
|
| 310 |
struct vk_meta_push_data { |
| 311 |
uint64_t buffer; |
| 312 |
|
| 313 |
uint32_t row_extent; |
| 314 |
uint32_t slice_or_layer_extent; |
| 315 |
|
| 316 |
int32_t src_offset_el[4]; |
| 317 |
int32_t dst_offset_el[4]; |
| 318 |
uint32_t grid_el[3]; |
| 319 |
} PACKED; |
| 320 |
|
| 321 |
#define get_push(b, name) \ |
| 322 |
nir_load_push_constant( \ |
| 323 |
b, 1, sizeof(((struct vk_meta_push_data *)0)->name) * 8, \ |
| 324 |
nir_imm_int(b, offsetof(struct vk_meta_push_data, name))) |
| 325 |
|
| 326 |
struct vk_meta_image_copy_key { |
| 327 |
enum vk_meta_object_key_type key_type; |
| 328 |
enum copy_type type; |
| 329 |
enum pipe_format src_format, dst_format; |
| 330 |
unsigned block_size; |
| 331 |
unsigned nr_samples; |
| 332 |
bool block_based; |
| 333 |
}; |
| 334 |
|
| 335 |
static nir_def * |
| 336 |
linearize_coords(nir_builder *b, nir_def *coord, |
| 337 |
const struct vk_meta_image_copy_key *key) |
| 338 |
{ |
| 339 |
assert(key->nr_samples == 1 && "buffer<-->image copies not multisampled"); |
| 340 |
|
| 341 |
nir_def *row_extent = get_push(b, row_extent); |
| 342 |
nir_def *slice_or_layer_extent = get_push(b, slice_or_layer_extent); |
| 343 |
nir_def *x = nir_channel(b, coord, 0); |
| 344 |
nir_def *y = nir_channel(b, coord, 1); |
| 345 |
nir_def *z_or_layer = nir_channel(b, coord, 2); |
| 346 |
|
| 347 |
nir_def *v = nir_imul_imm(b, x, key->block_size); |
| 348 |
|
| 349 |
v = nir_iadd(b, v, nir_imul(b, y, row_extent)); |
| 350 |
v = nir_iadd(b, v, nir_imul(b, z_or_layer, slice_or_layer_extent)); |
| 351 |
|
| 352 |
return nir_udiv_imm(b, v, key->block_size); |
| 353 |
} |
| 354 |
|
| 355 |
static bool |
| 356 |
is_format_native(enum pipe_format format) |
| 357 |
{ |
| 358 |
switch (format) { |
| 359 |
case PIPE_FORMAT_R8_UINT: |
| 360 |
case PIPE_FORMAT_R8G8_UINT: |
| 361 |
case PIPE_FORMAT_R32_UINT: |
| 362 |
case PIPE_FORMAT_R32G32_UINT: |
| 363 |
case PIPE_FORMAT_R16G16_UINT: |
| 364 |
case PIPE_FORMAT_R16_UNORM: |
| 365 |
/* TODO: debug me .. why do these fail */ |
| 366 |
return false; |
| 367 |
case PIPE_FORMAT_R11G11B10_FLOAT: |
| 368 |
case PIPE_FORMAT_R9G9B9E5_FLOAT: |
| 369 |
case PIPE_FORMAT_R16G16B16A16_UINT: |
| 370 |
case PIPE_FORMAT_R32G32B32A32_UINT: |
| 371 |
case PIPE_FORMAT_R8G8B8A8_UINT: |
| 372 |
case PIPE_FORMAT_R10G10B10A2_UNORM: |
| 373 |
return true; |
| 374 |
case PIPE_FORMAT_R5G6B5_UNORM: |
| 375 |
case PIPE_FORMAT_R4G4B4A4_UNORM: |
| 376 |
case PIPE_FORMAT_A1R5G5B5_UNORM: |
| 377 |
case PIPE_FORMAT_B5G5R5A1_UNORM: |
| 378 |
return false; |
| 379 |
default: |
| 380 |
UNREACHABLE("expected canonical"); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
static nir_def * |
| 385 |
load_store_formatted(nir_builder *b, nir_def *base, nir_def *index, |
| 386 |
nir_def *value, enum pipe_format format) |
| 387 |
{ |
| 388 |
if (util_format_is_depth_or_stencil(format)) |
| 389 |
format = canonical_format_pipe(format); |
| 390 |
|
| 391 |
if (is_format_native(format)) { |
| 392 |
enum pipe_format isa = ail_pixel_format[format].renderable; |
| 393 |
unsigned isa_size = util_format_get_blocksize(isa); |
| 394 |
unsigned isa_components = util_format_get_blocksize(format) / isa_size; |
| 395 |
unsigned shift = util_logbase2(isa_components); |
| 396 |
|
| 397 |
if (value) { |
| 398 |
nir_store_agx(b, value, base, index, .format = isa, .base = shift); |
| 399 |
} else { |
| 400 |
return nir_load_agx(b, 4, 32, base, index, .format = isa, |
| 401 |
.base = shift); |
| 402 |
} |
| 403 |
} else { |
| 404 |
unsigned blocksize_B = util_format_get_blocksize(format); |
| 405 |
nir_def *addr = |
| 406 |
nir_iadd(b, base, nir_imul_imm(b, nir_u2u64(b, index), blocksize_B)); |
| 407 |
|
| 408 |
if (value) { |
| 409 |
nir_def *raw = nir_format_pack_rgba(b, format, value); |
| 410 |
|
| 411 |
if (blocksize_B <= 4) { |
| 412 |
assert(raw->num_components == 1); |
| 413 |
raw = nir_u2uN(b, raw, blocksize_B * 8); |
| 414 |
} else { |
| 415 |
assert(raw->bit_size == 32); |
| 416 |
raw = nir_trim_vector(b, raw, blocksize_B / 4); |
| 417 |
} |
| 418 |
|
| 419 |
nir_store_global(b, raw, addr, .align_mul = blocksize_B); |
| 420 |
} else { |
| 421 |
nir_def *raw = nir_load_global(b, DIV_ROUND_UP(blocksize_B, 4), |
| 422 |
MIN2(32, blocksize_B * 8), addr, |
| 423 |
.align_mul = blocksize_B); |
| 424 |
|
| 425 |
return nir_format_unpack_rgba(b, raw, format); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
static nir_shader * |
| 433 |
build_image_copy_shader(const struct vk_meta_image_copy_key *key) |
| 434 |
{ |
| 435 |
nir_builder build = |
| 436 |
nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL, "hk-meta-copy"); |
| 437 |
|
| 438 |
nir_builder *b = &build; |
| 439 |
b->shader->info.workgroup_size[0] = TILE_WIDTH; |
| 440 |
b->shader->info.workgroup_size[1] = TILE_HEIGHT; |
| 441 |
|
| 442 |
bool src_is_buf = key->type == BUF2IMG; |
| 443 |
bool dst_is_buf = key->type == IMG2BUF; |
| 444 |
|
| 445 |
bool msaa = key->nr_samples > 1; |
| 446 |
enum glsl_sampler_dim dim_2d = |
| 447 |
msaa ? GLSL_SAMPLER_DIM_MS : GLSL_SAMPLER_DIM_2D; |
| 448 |
enum glsl_sampler_dim dim_src = src_is_buf ? GLSL_SAMPLER_DIM_BUF : dim_2d; |
| 449 |
enum glsl_sampler_dim dim_dst = dst_is_buf ? GLSL_SAMPLER_DIM_BUF : dim_2d; |
| 450 |
|
| 451 |
const struct glsl_type *texture_type = |
| 452 |
glsl_sampler_type(dim_src, false, !src_is_buf, GLSL_TYPE_UINT); |
| 453 |
|
| 454 |
const struct glsl_type *image_type = |
| 455 |
glsl_image_type(dim_dst, !dst_is_buf, GLSL_TYPE_UINT); |
| 456 |
|
| 457 |
nir_variable *texture = |
| 458 |
nir_variable_create(b->shader, nir_var_uniform, texture_type, "source"); |
| 459 |
nir_variable *image = |
| 460 |
nir_variable_create(b->shader, nir_var_image, image_type, "dest"); |
| 461 |
|
| 462 |
image->data.descriptor_set = 0; |
| 463 |
image->data.binding = BINDING_OUTPUT; |
| 464 |
image->data.access = ACCESS_NON_READABLE; |
| 465 |
|
| 466 |
texture->data.descriptor_set = 0; |
| 467 |
texture->data.binding = BINDING_INPUT; |
| 468 |
|
| 469 |
/* Grab the offset vectors */ |
| 470 |
nir_def *src_offset_el = nir_load_push_constant( |
| 471 |
b, 3, 32, |
| 472 |
nir_imm_int(b, offsetof(struct vk_meta_push_data, src_offset_el))); |
| 473 |
|
| 474 |
nir_def *dst_offset_el = nir_load_push_constant( |
| 475 |
b, 3, 32, |
| 476 |
nir_imm_int(b, offsetof(struct vk_meta_push_data, dst_offset_el))); |
| 477 |
|
| 478 |
nir_def *grid_2d_el = nir_load_push_constant( |
| 479 |
b, 2, 32, nir_imm_int(b, offsetof(struct vk_meta_push_data, grid_el))); |
| 480 |
|
| 481 |
/* We're done setting up variables, do the copy */ |
| 482 |
nir_def *coord = nir_load_global_invocation_id(b, 32); |
| 483 |
|
| 484 |
/* The destination format is already canonical, convert to an ISA format */ |
| 485 |
enum pipe_format isa_format = PIPE_FORMAT_NONE; |
| 486 |
if (key->block_based) { |
| 487 |
enum pipe_format pipe = canonical_format_pipe(key->dst_format); |
| 488 |
isa_format = ail_pixel_format[pipe].renderable; |
| 489 |
assert(isa_format != PIPE_FORMAT_NONE); |
| 490 |
} |
| 491 |
|
| 492 |
nir_def *local_offset = nir_imm_int(b, 0); |
| 493 |
nir_def *lid = nir_trim_vector(b, nir_load_local_invocation_id(b), 2); |
| 494 |
lid = nir_u2u16(b, lid); |
| 495 |
|
| 496 |
nir_def *src_coord = src_is_buf ? coord : nir_iadd(b, coord, src_offset_el); |
| 497 |
nir_def *dst_coord = dst_is_buf ? coord : nir_iadd(b, coord, dst_offset_el); |
| 498 |
|
| 499 |
nir_def *image_deref = &nir_build_deref_var(b, image)->def; |
| 500 |
|
| 501 |
nir_def *coord_2d_el = nir_trim_vector(b, coord, 2); |
| 502 |
nir_def *in_bounds; |
| 503 |
if (key->block_based) { |
| 504 |
nir_def *offset_in_block_el = |
| 505 |
nir_umod_imm(b, nir_trim_vector(b, dst_offset_el, 2), TILE_WIDTH); |
| 506 |
|
| 507 |
dst_coord = |
| 508 |
nir_vector_insert_imm(b, nir_isub(b, dst_coord, offset_in_block_el), |
| 509 |
nir_channel(b, dst_coord, 2), 2); |
| 510 |
|
| 511 |
src_coord = |
| 512 |
nir_vector_insert_imm(b, nir_isub(b, src_coord, offset_in_block_el), |
| 513 |
nir_channel(b, src_coord, 2), 2); |
| 514 |
|
| 515 |
in_bounds = nir_uge(b, coord_2d_el, offset_in_block_el); |
| 516 |
in_bounds = nir_iand( |
| 517 |
b, in_bounds, |
| 518 |
nir_ult(b, coord_2d_el, nir_iadd(b, offset_in_block_el, grid_2d_el))); |
| 519 |
} else { |
| 520 |
in_bounds = nir_ult(b, coord_2d_el, grid_2d_el); |
| 521 |
} |
| 522 |
|
| 523 |
/* Special case handle buffer indexing */ |
| 524 |
if (dst_is_buf) { |
| 525 |
assert(!key->block_based); |
| 526 |
dst_coord = linearize_coords(b, dst_coord, key); |
| 527 |
} else if (src_is_buf) { |
| 528 |
src_coord = linearize_coords(b, src_coord, key); |
| 529 |
} |
| 530 |
|
| 531 |
for (unsigned s = 0; s < key->nr_samples; ++s) { |
| 532 |
nir_def *ms_index = nir_imm_int(b, s); |
| 533 |
nir_def *value1 = NULL, *value2 = NULL; |
| 534 |
|
| 535 |
nir_push_if(b, nir_ball(b, in_bounds)); |
| 536 |
{ |
| 537 |
/* Copy formatted texel from texture to storage image */ |
| 538 |
nir_deref_instr *deref = nir_build_deref_var(b, texture); |
| 539 |
|
| 540 |
if (src_is_buf) { |
| 541 |
value1 = load_store_formatted(b, get_push(b, buffer), src_coord, |
| 542 |
NULL, key->dst_format); |
| 543 |
} else { |
| 544 |
value1 = nir_txf(b, src_coord, .texture_deref = deref, |
| 545 |
.ms_index = msaa ? ms_index : NULL); |
| 546 |
nir_def_as_tex(value1)->backend_flags = AGX_TEXTURE_FLAG_NO_CLAMP; |
| 547 |
|
| 548 |
/* Munge according to the implicit conversions so we get a bit copy */ |
| 549 |
if (key->src_format != key->dst_format) { |
| 550 |
nir_def *packed = |
| 551 |
nir_format_pack_rgba(b, key->src_format, value1); |
| 552 |
|
| 553 |
value1 = nir_format_unpack_rgba(b, packed, key->dst_format); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
if (dst_is_buf) { |
| 558 |
load_store_formatted(b, get_push(b, buffer), dst_coord, value1, |
| 559 |
key->dst_format); |
| 560 |
} else if (!key->block_based) { |
| 561 |
nir_image_deref_store(b, image_deref, nir_pad_vec4(b, dst_coord), |
| 562 |
ms_index, value1, nir_imm_int(b, 0), |
| 563 |
.image_dim = dim_dst, |
| 564 |
.image_array = !dst_is_buf); |
| 565 |
} |
| 566 |
} |
| 567 |
nir_push_else(b, NULL); |
| 568 |
if (key->block_based) { |
| 569 |
/* Copy back the existing destination content */ |
| 570 |
value2 = nir_image_deref_load(b, 4, 32, image_deref, |
| 571 |
nir_pad_vec4(b, dst_coord), ms_index, |
| 572 |
nir_imm_int(b, 0), .image_dim = dim_dst, |
| 573 |
.image_array = !dst_is_buf); |
| 574 |
} |
| 575 |
nir_pop_if(b, NULL); |
| 576 |
|
| 577 |
if (key->block_based) { |
| 578 |
/* Must define the phi first so we validate. */ |
| 579 |
nir_def *phi = nir_if_phi(b, value1, value2); |
| 580 |
nir_def *mask = nir_imm_intN_t(b, 1 << s, 16); |
| 581 |
|
| 582 |
nir_store_local_pixel_agx(b, phi, mask, lid, .base = 0, |
| 583 |
.write_mask = 0xf, .format = isa_format, |
| 584 |
.explicit_coord = true); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (key->block_based) { |
| 589 |
assert(!dst_is_buf); |
| 590 |
|
| 591 |
nir_barrier(b, .execution_scope = SCOPE_WORKGROUP); |
| 592 |
|
| 593 |
nir_push_if(b, nir_ball(b, nir_ieq_imm(b, lid, 0))); |
| 594 |
{ |
| 595 |
nir_image_deref_store_block_agx( |
| 596 |
b, image_deref, local_offset, dst_coord, .format = isa_format, |
| 597 |
.image_dim = dim_2d, .image_array = true, .explicit_coord = true); |
| 598 |
} |
| 599 |
nir_pop_if(b, NULL); |
| 600 |
b->shader->info.cs.image_block_size_per_thread_agx = |
| 601 |
util_format_get_blocksize(key->dst_format); |
| 602 |
} |
| 603 |
|
| 604 |
return b->shader; |
| 605 |
} |
| 606 |
|
| 607 |
static VkResult |
| 608 |
get_image_copy_descriptor_set_layout(struct vk_device *device, |
| 609 |
struct vk_meta_device *meta, |
| 610 |
VkDescriptorSetLayout *layout_out, |
| 611 |
enum copy_type type) |
| 612 |
{ |
| 613 |
const char *keys[] = { |
| 614 |
[IMG2BUF] = "vk-meta-copy-image-to-buffer-descriptor-set-layout", |
| 615 |
[BUF2IMG] = "vk-meta-copy-buffer-to-image-descriptor-set-layout", |
| 616 |
[IMG2IMG] = "vk-meta-copy-image-to-image-descriptor-set-layout", |
| 617 |
}; |
| 618 |
|
| 619 |
VkDescriptorSetLayout from_cache = vk_meta_lookup_descriptor_set_layout( |
| 620 |
meta, keys[type], strlen(keys[type])); |
| 621 |
if (from_cache != VK_NULL_HANDLE) { |
| 622 |
*layout_out = from_cache; |
| 623 |
return VK_SUCCESS; |
| 624 |
} |
| 625 |
|
| 626 |
const VkDescriptorSetLayoutBinding bindings[] = { |
| 627 |
{ |
| 628 |
.binding = BINDING_OUTPUT, |
| 629 |
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, |
| 630 |
.descriptorCount = 1, |
| 631 |
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, |
| 632 |
}, |
| 633 |
{ |
| 634 |
.binding = BINDING_INPUT, |
| 635 |
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, |
| 636 |
.descriptorCount = 1, |
| 637 |
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, |
| 638 |
}, |
| 639 |
}; |
| 640 |
|
| 641 |
const VkDescriptorSetLayoutCreateInfo info = { |
| 642 |
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
| 643 |
.bindingCount = ARRAY_SIZE(bindings), |
| 644 |
.pBindings = bindings, |
| 645 |
}; |
| 646 |
|
| 647 |
return vk_meta_create_descriptor_set_layout(device, meta, &info, keys[type], |
| 648 |
strlen(keys[type]), layout_out); |
| 649 |
} |
| 650 |
|
| 651 |
static VkResult |
| 652 |
get_image_copy_pipeline_layout(struct vk_device *device, |
| 653 |
struct vk_meta_device *meta, |
| 654 |
struct vk_meta_image_copy_key *key, |
| 655 |
VkDescriptorSetLayout set_layout, |
| 656 |
VkPipelineLayout *layout_out, |
| 657 |
enum copy_type type) |
| 658 |
{ |
| 659 |
const char *keys[] = { |
| 660 |
[IMG2BUF] = "vk-meta-copy-image-to-buffer-pipeline-layout", |
| 661 |
[BUF2IMG] = "vk-meta-copy-buffer-to-image-pipeline-layout", |
| 662 |
[IMG2IMG] = "vk-meta-copy-image-to-image-pipeline-layout", |
| 663 |
}; |
| 664 |
|
| 665 |
VkPipelineLayout from_cache = |
| 666 |
vk_meta_lookup_pipeline_layout(meta, keys[type], strlen(keys[type])); |
| 667 |
if (from_cache != VK_NULL_HANDLE) { |
| 668 |
*layout_out = from_cache; |
| 669 |
return VK_SUCCESS; |
| 670 |
} |
| 671 |
|
| 672 |
VkPipelineLayoutCreateInfo info = { |
| 673 |
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 674 |
.setLayoutCount = 1, |
| 675 |
.pSetLayouts = &set_layout, |
| 676 |
}; |
| 677 |
|
| 678 |
const VkPushConstantRange push_range = { |
| 679 |
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, |
| 680 |
.offset = 0, |
| 681 |
.size = sizeof(struct vk_meta_push_data), |
| 682 |
}; |
| 683 |
|
| 684 |
info.pushConstantRangeCount = 1; |
| 685 |
info.pPushConstantRanges = &push_range; |
| 686 |
|
| 687 |
return vk_meta_create_pipeline_layout(device, meta, &info, keys[type], |
| 688 |
strlen(keys[type]), layout_out); |
| 689 |
} |
| 690 |
|
| 691 |
static VkResult |
| 692 |
get_image_copy_pipeline(struct vk_device *device, struct vk_meta_device *meta, |
| 693 |
const struct vk_meta_image_copy_key *key, |
| 694 |
VkPipelineLayout layout, VkPipeline *pipeline_out) |
| 695 |
{ |
| 696 |
VkPipeline from_cache = vk_meta_lookup_pipeline(meta, key, sizeof(*key)); |
| 697 |
if (from_cache != VK_NULL_HANDLE) { |
| 698 |
*pipeline_out = from_cache; |
| 699 |
return VK_SUCCESS; |
| 700 |
} |
| 701 |
|
| 702 |
const VkPipelineShaderStageNirCreateInfoMESA nir_info = { |
| 703 |
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_NIR_CREATE_INFO_MESA, |
| 704 |
.nir = build_image_copy_shader(key), |
| 705 |
}; |
| 706 |
const VkPipelineShaderStageCreateInfo cs_info = { |
| 707 |
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, |
| 708 |
.pNext = &nir_info, |
| 709 |
.stage = VK_SHADER_STAGE_COMPUTE_BIT, |
| 710 |
.pName = "main", |
| 711 |
}; |
| 712 |
|
| 713 |
const VkComputePipelineCreateInfo info = { |
| 714 |
.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, |
| 715 |
.stage = cs_info, |
| 716 |
.layout = layout, |
| 717 |
}; |
| 718 |
|
| 719 |
VkResult result = vk_meta_create_compute_pipeline( |
| 720 |
device, meta, &info, key, sizeof(*key), pipeline_out); |
| 721 |
ralloc_free(nir_info.nir); |
| 722 |
|
| 723 |
return result; |
| 724 |
} |
| 725 |
|
| 726 |
static void |
| 727 |
hk_meta_copy_image_to_buffer2(struct vk_command_buffer *cmd, |
| 728 |
struct vk_meta_device *meta, |
| 729 |
const VkCopyImageToBufferInfo2 *pCopyBufferInfo) |
| 730 |
{ |
| 731 |
VK_FROM_HANDLE(vk_image, image, pCopyBufferInfo->srcImage); |
| 732 |
VK_FROM_HANDLE(vk_image, src_image, pCopyBufferInfo->srcImage); |
| 733 |
VK_FROM_HANDLE(hk_buffer, buffer, pCopyBufferInfo->dstBuffer); |
| 734 |
|
| 735 |
struct vk_device *device = cmd->base.device; |
| 736 |
const struct vk_device_dispatch_table *disp = &device->dispatch_table; |
| 737 |
|
| 738 |
VkResult result; |
| 739 |
|
| 740 |
VkDescriptorSetLayout set_layout; |
| 741 |
result = |
| 742 |
get_image_copy_descriptor_set_layout(device, meta, &set_layout, IMG2BUF); |
| 743 |
if (unlikely(result != VK_SUCCESS)) { |
| 744 |
vk_command_buffer_set_error(cmd, result); |
| 745 |
return; |
| 746 |
} |
| 747 |
|
| 748 |
bool per_layer = |
| 749 |
util_format_is_compressed(hk_format_to_pipe_format(image->format)); |
| 750 |
|
| 751 |
for (unsigned i = 0; i < pCopyBufferInfo->regionCount; ++i) { |
| 752 |
const VkBufferImageCopy2 *region = &pCopyBufferInfo->pRegions[i]; |
| 753 |
|
| 754 |
unsigned layers = MAX2(region->imageExtent.depth, |
| 755 |
vk_image_subresource_layer_count( |
| 756 |
src_image, ®ion->imageSubresource)); |
| 757 |
unsigned layer_iters = per_layer ? layers : 1; |
| 758 |
|
| 759 |
for (unsigned layer_offs = 0; layer_offs < layer_iters; ++layer_offs) { |
| 760 |
|
| 761 |
VkImageAspectFlags aspect = region->imageSubresource.aspectMask; |
| 762 |
VkFormat aspect_fmt = aspect_format(image->format, aspect); |
| 763 |
VkFormat canonical = canonical_format(aspect_fmt); |
| 764 |
|
| 765 |
uint32_t blocksize_B = |
| 766 |
util_format_get_blocksize(hk_format_to_pipe_format(canonical)); |
| 767 |
|
| 768 |
enum pipe_format p_format = hk_format_to_pipe_format(image->format); |
| 769 |
|
| 770 |
unsigned row_extent = util_format_get_nblocksx( |
| 771 |
p_format, MAX2(region->bufferRowLength, |
| 772 |
region->imageExtent.width)) * |
| 773 |
blocksize_B; |
| 774 |
unsigned slice_extent = |
| 775 |
util_format_get_nblocksy( |
| 776 |
p_format, |
| 777 |
MAX2(region->bufferImageHeight, region->imageExtent.height)) * |
| 778 |
row_extent; |
| 779 |
unsigned layer_extent = |
| 780 |
util_format_get_nblocksz(p_format, region->imageExtent.depth) * |
| 781 |
slice_extent; |
| 782 |
|
| 783 |
bool is_3d = region->imageExtent.depth > 1; |
| 784 |
|
| 785 |
struct vk_meta_image_copy_key key = { |
| 786 |
.key_type = VK_META_OBJECT_KEY_COPY_IMAGE_TO_BUFFER, |
| 787 |
.type = IMG2BUF, |
| 788 |
.block_size = blocksize_B, |
| 789 |
.nr_samples = image->samples, |
| 790 |
.src_format = hk_format_to_pipe_format(canonical), |
| 791 |
.dst_format = hk_format_to_pipe_format(canonical), |
| 792 |
}; |
| 793 |
|
| 794 |
VkPipelineLayout pipeline_layout; |
| 795 |
result = get_image_copy_pipeline_layout(device, meta, &key, set_layout, |
| 796 |
&pipeline_layout, false); |
| 797 |
if (unlikely(result != VK_SUCCESS)) { |
| 798 |
vk_command_buffer_set_error(cmd, result); |
| 799 |
return; |
| 800 |
} |
| 801 |
|
| 802 |
VkImageView src_view; |
| 803 |
const VkImageViewUsageCreateInfo src_view_usage = { |
| 804 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, |
| 805 |
.usage = VK_IMAGE_USAGE_SAMPLED_BIT, |
| 806 |
}; |
| 807 |
const VkImageViewCreateInfo src_view_info = { |
| 808 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 809 |
.flags = VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA, |
| 810 |
.pNext = &src_view_usage, |
| 811 |
.image = pCopyBufferInfo->srcImage, |
| 812 |
.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY, |
| 813 |
.format = canonical, |
| 814 |
.subresourceRange = |
| 815 |
{ |
| 816 |
.aspectMask = region->imageSubresource.aspectMask, |
| 817 |
.baseMipLevel = region->imageSubresource.mipLevel, |
| 818 |
.baseArrayLayer = |
| 819 |
MAX2(region->imageOffset.z, |
| 820 |
region->imageSubresource.baseArrayLayer) + |
| 821 |
layer_offs, |
| 822 |
.layerCount = per_layer ? 1 : layers, |
| 823 |
.levelCount = 1, |
| 824 |
}, |
| 825 |
}; |
| 826 |
|
| 827 |
result = |
| 828 |
vk_meta_create_image_view(cmd, meta, &src_view_info, &src_view); |
| 829 |
if (unlikely(result != VK_SUCCESS)) { |
| 830 |
vk_command_buffer_set_error(cmd, result); |
| 831 |
return; |
| 832 |
} |
| 833 |
|
| 834 |
VkDescriptorImageInfo src_info = { |
| 835 |
.imageLayout = pCopyBufferInfo->srcImageLayout, |
| 836 |
.imageView = src_view, |
| 837 |
}; |
| 838 |
|
| 839 |
VkWriteDescriptorSet desc_write = { |
| 840 |
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
| 841 |
.dstSet = 0, |
| 842 |
.dstBinding = BINDING_INPUT, |
| 843 |
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, |
| 844 |
.descriptorCount = 1, |
| 845 |
.pImageInfo = &src_info, |
| 846 |
}; |
| 847 |
|
| 848 |
disp->CmdPushDescriptorSetKHR(vk_command_buffer_to_handle(cmd), |
| 849 |
VK_PIPELINE_BIND_POINT_COMPUTE, |
| 850 |
pipeline_layout, 0, 1, &desc_write); |
| 851 |
|
| 852 |
VkPipeline pipeline; |
| 853 |
result = get_image_copy_pipeline(device, meta, &key, pipeline_layout, |
| 854 |
&pipeline); |
| 855 |
if (unlikely(result != VK_SUCCESS)) { |
| 856 |
vk_command_buffer_set_error(cmd, result); |
| 857 |
return; |
| 858 |
} |
| 859 |
|
| 860 |
disp->CmdBindPipeline(vk_command_buffer_to_handle(cmd), |
| 861 |
VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); |
| 862 |
|
| 863 |
enum pipe_format p_src_fmt = |
| 864 |
hk_format_to_pipe_format(src_image->format); |
| 865 |
|
| 866 |
struct vk_meta_push_data push = { |
| 867 |
.buffer = hk_buffer_address_rw(buffer, region->bufferOffset), |
| 868 |
.row_extent = row_extent, |
| 869 |
.slice_or_layer_extent = is_3d ? slice_extent : layer_extent, |
| 870 |
|
| 871 |
.src_offset_el[0] = |
| 872 |
util_format_get_nblocksx(p_src_fmt, region->imageOffset.x), |
| 873 |
.src_offset_el[1] = |
| 874 |
util_format_get_nblocksy(p_src_fmt, region->imageOffset.y), |
| 875 |
|
| 876 |
.grid_el[0] = |
| 877 |
util_format_get_nblocksx(p_format, region->imageExtent.width), |
| 878 |
.grid_el[1] = |
| 879 |
util_format_get_nblocksy(p_format, region->imageExtent.height), |
| 880 |
.grid_el[2] = per_layer ? 1 : layers, |
| 881 |
}; |
| 882 |
|
| 883 |
push.buffer += push.slice_or_layer_extent * layer_offs; |
| 884 |
|
| 885 |
disp->CmdPushConstants(vk_command_buffer_to_handle(cmd), |
| 886 |
pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, |
| 887 |
sizeof(push), &push); |
| 888 |
|
| 889 |
disp->CmdDispatch(vk_command_buffer_to_handle(cmd), |
| 890 |
DIV_ROUND_UP(push.grid_el[0], 32), |
| 891 |
DIV_ROUND_UP(push.grid_el[1], 32), push.grid_el[2]); |
| 892 |
} |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
static void |
| 897 |
hk_meta_dispatch_to_image(struct vk_command_buffer *cmd, |
| 898 |
const struct vk_device_dispatch_table *disp, |
| 899 |
VkPipelineLayout pipeline_layout, |
| 900 |
struct vk_meta_push_data *push, VkOffset3D offset, |
| 901 |
VkExtent3D extent, bool per_layer, unsigned layers, |
| 902 |
enum pipe_format p_dst_fmt, enum pipe_format p_format) |
| 903 |
{ |
| 904 |
push->dst_offset_el[0] = util_format_get_nblocksx(p_dst_fmt, offset.x); |
| 905 |
push->dst_offset_el[1] = util_format_get_nblocksy(p_dst_fmt, offset.y); |
| 906 |
push->dst_offset_el[2] = 0; |
| 907 |
|
| 908 |
push->grid_el[0] = util_format_get_nblocksx(p_format, extent.width); |
| 909 |
push->grid_el[1] = util_format_get_nblocksy(p_format, extent.height); |
| 910 |
push->grid_el[2] = per_layer ? 1 : layers; |
| 911 |
|
| 912 |
unsigned w_el = util_format_get_nblocksx(p_format, extent.width); |
| 913 |
unsigned h_el = util_format_get_nblocksy(p_format, extent.height); |
| 914 |
|
| 915 |
/* Expand the grid so destinations are in tiles */ |
| 916 |
unsigned expanded_x0 = push->dst_offset_el[0] & ~(TILE_WIDTH - 1); |
| 917 |
unsigned expanded_y0 = push->dst_offset_el[1] & ~(TILE_HEIGHT - 1); |
| 918 |
unsigned expanded_x1 = align(push->dst_offset_el[0] + w_el, TILE_WIDTH); |
| 919 |
unsigned expanded_y1 = align(push->dst_offset_el[1] + h_el, TILE_HEIGHT); |
| 920 |
|
| 921 |
/* TODO: clamp to the destination size to save some redundant threads? */ |
| 922 |
|
| 923 |
disp->CmdPushConstants(vk_command_buffer_to_handle(cmd), pipeline_layout, |
| 924 |
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(*push), push); |
| 925 |
|
| 926 |
disp->CmdDispatch(vk_command_buffer_to_handle(cmd), |
| 927 |
(expanded_x1 - expanded_x0) / TILE_WIDTH, |
| 928 |
(expanded_y1 - expanded_y0) / TILE_HEIGHT, |
| 929 |
push->grid_el[2]); |
| 930 |
} |
| 931 |
|
| 932 |
static void |
| 933 |
hk_meta_copy_buffer_to_image2(struct vk_command_buffer *cmd, |
| 934 |
struct vk_meta_device *meta, |
| 935 |
const struct VkCopyBufferToImageInfo2 *info) |
| 936 |
{ |
| 937 |
VK_FROM_HANDLE(vk_image, image, info->dstImage); |
| 938 |
VK_FROM_HANDLE(hk_buffer, buffer, info->srcBuffer); |
| 939 |
|
| 940 |
struct vk_device *device = cmd->base.device; |
| 941 |
const struct vk_device_dispatch_table *disp = &device->dispatch_table; |
| 942 |
|
| 943 |
VkDescriptorSetLayout set_layout; |
| 944 |
VkResult result = |
| 945 |
get_image_copy_descriptor_set_layout(device, meta, &set_layout, BUF2IMG); |
| 946 |
if (unlikely(result != VK_SUCCESS)) { |
| 947 |
vk_command_buffer_set_error(cmd, result); |
| 948 |
return; |
| 949 |
} |
| 950 |
|
| 951 |
bool per_layer = |
| 952 |
util_format_is_compressed(hk_format_to_pipe_format(image->format)); |
| 953 |
|
| 954 |
for (unsigned r = 0; r < info->regionCount; ++r) { |
| 955 |
const VkBufferImageCopy2 *region = &info->pRegions[r]; |
| 956 |
|
| 957 |
unsigned layers = MAX2( |
| 958 |
region->imageExtent.depth, |
| 959 |
vk_image_subresource_layer_count(image, ®ion->imageSubresource)); |
| 960 |
unsigned layer_iters = per_layer ? layers : 1; |
| 961 |
|
| 962 |
for (unsigned layer_offs = 0; layer_offs < layer_iters; ++layer_offs) { |
| 963 |
VkImageAspectFlags aspect = region->imageSubresource.aspectMask; |
| 964 |
VkFormat aspect_fmt = aspect_format(image->format, aspect); |
| 965 |
VkFormat canonical = canonical_format(aspect_fmt); |
| 966 |
enum pipe_format p_format = hk_format_to_pipe_format(aspect_fmt); |
| 967 |
uint32_t blocksize_B = util_format_get_blocksize(p_format); |
| 968 |
bool is_3d = region->imageExtent.depth > 1; |
| 969 |
|
| 970 |
struct vk_meta_image_copy_key key = { |
| 971 |
.key_type = VK_META_OBJECT_KEY_COPY_IMAGE_TO_BUFFER, |
| 972 |
.type = BUF2IMG, |
| 973 |
.block_size = blocksize_B, |
| 974 |
.nr_samples = image->samples, |
| 975 |
.src_format = hk_format_to_pipe_format(canonical), |
| 976 |
.dst_format = canonical_format_pipe( |
| 977 |
hk_format_to_pipe_format(aspect_format(image->format, aspect))), |
| 978 |
|
| 979 |
/* TODO: MSAA path */ |
| 980 |
.block_based = |
| 981 |
(image->image_type != VK_IMAGE_TYPE_1D) && image->samples == 1, |
| 982 |
}; |
| 983 |
|
| 984 |
VkPipelineLayout pipeline_layout; |
| 985 |
result = get_image_copy_pipeline_layout(device, meta, &key, set_layout, |
| 986 |
&pipeline_layout, true); |
| 987 |
if (unlikely(result != VK_SUCCESS)) { |
| 988 |
vk_command_buffer_set_error(cmd, result); |
| 989 |
return; |
| 990 |
} |
| 991 |
|
| 992 |
unsigned row_extent = util_format_get_nblocksx( |
| 993 |
p_format, MAX2(region->bufferRowLength, |
| 994 |
region->imageExtent.width)) * |
| 995 |
blocksize_B; |
| 996 |
unsigned slice_extent = |
| 997 |
util_format_get_nblocksy( |
| 998 |
p_format, |
| 999 |
MAX2(region->bufferImageHeight, region->imageExtent.height)) * |
| 1000 |
row_extent; |
| 1001 |
unsigned layer_extent = |
| 1002 |
util_format_get_nblocksz(p_format, region->imageExtent.depth) * |
| 1003 |
slice_extent; |
| 1004 |
|
| 1005 |
VkImageView dst_view; |
| 1006 |
const VkImageViewUsageCreateInfo dst_view_usage = { |
| 1007 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, |
| 1008 |
.usage = VK_IMAGE_USAGE_STORAGE_BIT, |
| 1009 |
}; |
| 1010 |
const VkImageViewCreateInfo dst_view_info = { |
| 1011 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 1012 |
.flags = VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA, |
| 1013 |
.pNext = &dst_view_usage, |
| 1014 |
.image = info->dstImage, |
| 1015 |
.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY, |
| 1016 |
.format = canonical, |
| 1017 |
.subresourceRange = |
| 1018 |
{ |
| 1019 |
.aspectMask = region->imageSubresource.aspectMask, |
| 1020 |
.baseMipLevel = region->imageSubresource.mipLevel, |
| 1021 |
.baseArrayLayer = |
| 1022 |
MAX2(region->imageOffset.z, |
| 1023 |
region->imageSubresource.baseArrayLayer) + |
| 1024 |
layer_offs, |
| 1025 |
.layerCount = per_layer ? 1 : layers, |
| 1026 |
.levelCount = 1, |
| 1027 |
}, |
| 1028 |
}; |
| 1029 |
|
| 1030 |
result = |
| 1031 |
vk_meta_create_image_view(cmd, meta, &dst_view_info, &dst_view); |
| 1032 |
if (unlikely(result != VK_SUCCESS)) { |
| 1033 |
vk_command_buffer_set_error(cmd, result); |
| 1034 |
return; |
| 1035 |
} |
| 1036 |
|
| 1037 |
const VkDescriptorImageInfo dst_info = { |
| 1038 |
.imageView = dst_view, |
| 1039 |
.imageLayout = info->dstImageLayout, |
| 1040 |
}; |
| 1041 |
|
| 1042 |
VkWriteDescriptorSet desc_write = { |
| 1043 |
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
| 1044 |
.dstSet = 0, |
| 1045 |
.dstBinding = BINDING_OUTPUT, |
| 1046 |
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, |
| 1047 |
.descriptorCount = 1, |
| 1048 |
.pImageInfo = &dst_info, |
| 1049 |
}; |
| 1050 |
|
| 1051 |
disp->CmdPushDescriptorSetKHR(vk_command_buffer_to_handle(cmd), |
| 1052 |
VK_PIPELINE_BIND_POINT_COMPUTE, |
| 1053 |
pipeline_layout, 0, 1, &desc_write); |
| 1054 |
|
| 1055 |
VkPipeline pipeline; |
| 1056 |
result = get_image_copy_pipeline(device, meta, &key, pipeline_layout, |
| 1057 |
&pipeline); |
| 1058 |
if (unlikely(result != VK_SUCCESS)) { |
| 1059 |
vk_command_buffer_set_error(cmd, result); |
| 1060 |
return; |
| 1061 |
} |
| 1062 |
|
| 1063 |
disp->CmdBindPipeline(vk_command_buffer_to_handle(cmd), |
| 1064 |
VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); |
| 1065 |
|
| 1066 |
struct vk_meta_push_data push = { |
| 1067 |
.buffer = hk_buffer_address_ro(buffer, region->bufferOffset), |
| 1068 |
.row_extent = row_extent, |
| 1069 |
.slice_or_layer_extent = is_3d ? slice_extent : layer_extent, |
| 1070 |
}; |
| 1071 |
|
| 1072 |
push.buffer += push.slice_or_layer_extent * layer_offs; |
| 1073 |
|
| 1074 |
hk_meta_dispatch_to_image(cmd, disp, pipeline_layout, &push, |
| 1075 |
region->imageOffset, region->imageExtent, |
| 1076 |
per_layer, layers, p_format, p_format); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
static void |
| 1082 |
hk_meta_copy_image2(struct vk_command_buffer *cmd, struct vk_meta_device *meta, |
| 1083 |
const struct VkCopyImageInfo2 *info) |
| 1084 |
{ |
| 1085 |
VK_FROM_HANDLE(vk_image, src_image, info->srcImage); |
| 1086 |
VK_FROM_HANDLE(vk_image, dst_image, info->dstImage); |
| 1087 |
|
| 1088 |
struct vk_device *device = cmd->base.device; |
| 1089 |
const struct vk_device_dispatch_table *disp = &device->dispatch_table; |
| 1090 |
|
| 1091 |
VkDescriptorSetLayout set_layout; |
| 1092 |
VkResult result = |
| 1093 |
get_image_copy_descriptor_set_layout(device, meta, &set_layout, BUF2IMG); |
| 1094 |
if (unlikely(result != VK_SUCCESS)) { |
| 1095 |
vk_command_buffer_set_error(cmd, result); |
| 1096 |
return; |
| 1097 |
} |
| 1098 |
|
| 1099 |
bool per_layer = |
| 1100 |
util_format_is_compressed(hk_format_to_pipe_format(src_image->format)) || |
| 1101 |
util_format_is_compressed(hk_format_to_pipe_format(dst_image->format)); |
| 1102 |
|
| 1103 |
for (unsigned r = 0; r < info->regionCount; ++r) { |
| 1104 |
const VkImageCopy2 *region = &info->pRegions[r]; |
| 1105 |
|
| 1106 |
unsigned layers = MAX2( |
| 1107 |
vk_image_subresource_layer_count(src_image, ®ion->srcSubresource), |
| 1108 |
region->extent.depth); |
| 1109 |
unsigned layer_iters = per_layer ? layers : 1; |
| 1110 |
|
| 1111 |
for (unsigned layer_offs = 0; layer_offs < layer_iters; ++layer_offs) { |
| 1112 |
u_foreach_bit(aspect, region->srcSubresource.aspectMask) { |
| 1113 |
/* We use the source format throughout for consistent scaling with |
| 1114 |
* compressed<-->uncompressed copies, where the extents are defined |
| 1115 |
* to follow the source. |
| 1116 |
*/ |
| 1117 |
VkFormat aspect_fmt = aspect_format(src_image->format, 1 << aspect); |
| 1118 |
VkFormat canonical = canonical_format(aspect_fmt); |
| 1119 |
uint32_t blocksize_B = |
| 1120 |
util_format_get_blocksize(hk_format_to_pipe_format(canonical)); |
| 1121 |
|
| 1122 |
bool use_dst_aspect_mask = |
| 1123 |
vk_format_get_ycbcr_info(dst_image->format) || |
| 1124 |
vk_format_get_ycbcr_info(src_image->format) || |
| 1125 |
(vk_format_is_depth_or_stencil(dst_image->format) && |
| 1126 |
!vk_format_is_depth_or_stencil(src_image->format)); |
| 1127 |
|
| 1128 |
VkImageAspectFlagBits dst_aspect_mask = |
| 1129 |
use_dst_aspect_mask ? region->dstSubresource.aspectMask |
| 1130 |
: (1 << aspect); |
| 1131 |
|
| 1132 |
struct vk_meta_image_copy_key key = { |
| 1133 |
.key_type = VK_META_OBJECT_KEY_COPY_IMAGE_TO_BUFFER, |
| 1134 |
.type = IMG2IMG, |
| 1135 |
.block_size = blocksize_B, |
| 1136 |
.nr_samples = dst_image->samples, |
| 1137 |
.src_format = hk_format_to_pipe_format(canonical), |
| 1138 |
.dst_format = canonical_format_pipe(hk_format_to_pipe_format( |
| 1139 |
aspect_format(dst_image->format, dst_aspect_mask))), |
| 1140 |
|
| 1141 |
/* TODO: MSAA path */ |
| 1142 |
.block_based = (dst_image->image_type != VK_IMAGE_TYPE_1D) && |
| 1143 |
dst_image->samples == 1, |
| 1144 |
}; |
| 1145 |
|
| 1146 |
assert(key.nr_samples == src_image->samples); |
| 1147 |
|
| 1148 |
VkPipelineLayout pipeline_layout; |
| 1149 |
result = get_image_copy_pipeline_layout( |
| 1150 |
device, meta, &key, set_layout, &pipeline_layout, true); |
| 1151 |
if (unlikely(result != VK_SUCCESS)) { |
| 1152 |
vk_command_buffer_set_error(cmd, result); |
| 1153 |
return; |
| 1154 |
} |
| 1155 |
|
| 1156 |
VkWriteDescriptorSet desc_writes[2]; |
| 1157 |
|
| 1158 |
VkImageView src_view; |
| 1159 |
const VkImageViewUsageCreateInfo src_view_usage = { |
| 1160 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, |
| 1161 |
.usage = VK_IMAGE_USAGE_SAMPLED_BIT, |
| 1162 |
}; |
| 1163 |
const VkImageViewCreateInfo src_view_info = { |
| 1164 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 1165 |
.flags = VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA, |
| 1166 |
.pNext = &src_view_usage, |
| 1167 |
.image = info->srcImage, |
| 1168 |
.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY, |
| 1169 |
.format = canonical, |
| 1170 |
.subresourceRange = |
| 1171 |
{ |
| 1172 |
.aspectMask = |
| 1173 |
region->srcSubresource.aspectMask & (1 << aspect), |
| 1174 |
.baseMipLevel = region->srcSubresource.mipLevel, |
| 1175 |
.baseArrayLayer = |
| 1176 |
MAX2(region->srcOffset.z, |
| 1177 |
region->srcSubresource.baseArrayLayer) + |
| 1178 |
layer_offs, |
| 1179 |
.layerCount = per_layer ? 1 : layers, |
| 1180 |
.levelCount = 1, |
| 1181 |
}, |
| 1182 |
}; |
| 1183 |
|
| 1184 |
result = |
| 1185 |
vk_meta_create_image_view(cmd, meta, &src_view_info, &src_view); |
| 1186 |
if (unlikely(result != VK_SUCCESS)) { |
| 1187 |
vk_command_buffer_set_error(cmd, result); |
| 1188 |
return; |
| 1189 |
} |
| 1190 |
|
| 1191 |
VkDescriptorImageInfo src_info = { |
| 1192 |
.imageLayout = info->srcImageLayout, |
| 1193 |
.imageView = src_view, |
| 1194 |
}; |
| 1195 |
|
| 1196 |
VkImageView dst_view; |
| 1197 |
const VkImageViewUsageCreateInfo dst_view_usage = { |
| 1198 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, |
| 1199 |
.usage = VK_IMAGE_USAGE_STORAGE_BIT, |
| 1200 |
}; |
| 1201 |
const VkImageViewCreateInfo dst_view_info = { |
| 1202 |
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 1203 |
.flags = VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA, |
| 1204 |
.pNext = &dst_view_usage, |
| 1205 |
.image = info->dstImage, |
| 1206 |
.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY, |
| 1207 |
.format = vk_format_from_pipe_format(key.dst_format), |
| 1208 |
.subresourceRange = |
| 1209 |
{ |
| 1210 |
.aspectMask = dst_aspect_mask, |
| 1211 |
.baseMipLevel = region->dstSubresource.mipLevel, |
| 1212 |
.baseArrayLayer = |
| 1213 |
MAX2(region->dstOffset.z, |
| 1214 |
region->dstSubresource.baseArrayLayer) + |
| 1215 |
layer_offs, |
| 1216 |
.layerCount = per_layer ? 1 : layers, |
| 1217 |
.levelCount = 1, |
| 1218 |
}, |
| 1219 |
}; |
| 1220 |
|
| 1221 |
result = |
| 1222 |
vk_meta_create_image_view(cmd, meta, &dst_view_info, &dst_view); |
| 1223 |
if (unlikely(result != VK_SUCCESS)) { |
| 1224 |
vk_command_buffer_set_error(cmd, result); |
| 1225 |
return; |
| 1226 |
} |
| 1227 |
|
| 1228 |
const VkDescriptorImageInfo dst_info = { |
| 1229 |
.imageView = dst_view, |
| 1230 |
.imageLayout = info->dstImageLayout, |
| 1231 |
}; |
| 1232 |
|
| 1233 |
desc_writes[0] = (VkWriteDescriptorSet){ |
| 1234 |
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
| 1235 |
.dstSet = 0, |
| 1236 |
.dstBinding = BINDING_OUTPUT, |
| 1237 |
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, |
| 1238 |
.descriptorCount = 1, |
| 1239 |
.pImageInfo = &dst_info, |
| 1240 |
}; |
| 1241 |
|
| 1242 |
desc_writes[1] = (VkWriteDescriptorSet){ |
| 1243 |
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
| 1244 |
.dstSet = 0, |
| 1245 |
.dstBinding = BINDING_INPUT, |
| 1246 |
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, |
| 1247 |
.descriptorCount = 1, |
| 1248 |
.pImageInfo = &src_info, |
| 1249 |
}; |
| 1250 |
|
| 1251 |
disp->CmdPushDescriptorSetKHR( |
| 1252 |
vk_command_buffer_to_handle(cmd), VK_PIPELINE_BIND_POINT_COMPUTE, |
| 1253 |
pipeline_layout, 0, ARRAY_SIZE(desc_writes), desc_writes); |
| 1254 |
|
| 1255 |
VkPipeline pipeline; |
| 1256 |
result = get_image_copy_pipeline(device, meta, &key, |
| 1257 |
pipeline_layout, &pipeline); |
| 1258 |
if (unlikely(result != VK_SUCCESS)) { |
| 1259 |
vk_command_buffer_set_error(cmd, result); |
| 1260 |
return; |
| 1261 |
} |
| 1262 |
|
| 1263 |
disp->CmdBindPipeline(vk_command_buffer_to_handle(cmd), |
| 1264 |
VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); |
| 1265 |
|
| 1266 |
enum pipe_format p_src_fmt = |
| 1267 |
hk_format_to_pipe_format(src_image->format); |
| 1268 |
enum pipe_format p_dst_fmt = |
| 1269 |
hk_format_to_pipe_format(dst_image->format); |
| 1270 |
enum pipe_format p_format = hk_format_to_pipe_format(aspect_fmt); |
| 1271 |
|
| 1272 |
struct vk_meta_push_data push = { |
| 1273 |
.src_offset_el[0] = |
| 1274 |
util_format_get_nblocksx(p_src_fmt, region->srcOffset.x), |
| 1275 |
.src_offset_el[1] = |
| 1276 |
util_format_get_nblocksy(p_src_fmt, region->srcOffset.y), |
| 1277 |
}; |
| 1278 |
|
| 1279 |
hk_meta_dispatch_to_image(cmd, disp, pipeline_layout, &push, |
| 1280 |
region->dstOffset, region->extent, |
| 1281 |
per_layer, layers, p_dst_fmt, p_format); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
static inline struct vk_meta_copy_image_properties |
| 1288 |
hk_meta_copy_get_image_properties(struct hk_image *img) |
| 1289 |
{ |
| 1290 |
struct vk_meta_copy_image_properties props = {.tile_size = {16, 16, 1}}; |
| 1291 |
|
| 1292 |
if (!vk_format_is_depth_or_stencil(img->vk.format)) { |
| 1293 |
props.color.view_format = img->vk.format; |
| 1294 |
} else { |
| 1295 |
switch (img->vk.format) { |
| 1296 |
case VK_FORMAT_S8_UINT: |
| 1297 |
props.stencil.view_format = VK_FORMAT_R8_UINT; |
| 1298 |
props.stencil.component_mask = BITFIELD_MASK(1); |
| 1299 |
break; |
| 1300 |
case VK_FORMAT_D32_SFLOAT_S8_UINT: |
| 1301 |
props.depth.view_format = VK_FORMAT_R32G32_UINT; |
| 1302 |
props.depth.component_mask = BITFIELD_BIT(0); |
| 1303 |
props.stencil.view_format = VK_FORMAT_R32G32_UINT; |
| 1304 |
props.stencil.component_mask = BITFIELD_BIT(1); |
| 1305 |
break; |
| 1306 |
case VK_FORMAT_D16_UNORM: |
| 1307 |
props.depth.view_format = VK_FORMAT_R16_UINT; |
| 1308 |
props.depth.component_mask = BITFIELD_BIT(0); |
| 1309 |
break; |
| 1310 |
case VK_FORMAT_D32_SFLOAT: |
| 1311 |
props.depth.view_format = VK_FORMAT_R32_UINT; |
| 1312 |
props.depth.component_mask = BITFIELD_BIT(0); |
| 1313 |
break; |
| 1314 |
default: |
| 1315 |
UNREACHABLE("Invalid ZS format"); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
return props; |
| 1320 |
} |
| 1321 |
|
| 1322 |
VKAPI_ATTR void VKAPI_CALL |
| 1323 |
hk_CmdBlitImage2(VkCommandBuffer commandBuffer, |
| 1324 |
const VkBlitImageInfo2 *pBlitImageInfo) |
| 1325 |
{ |
| 1326 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1327 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1328 |
perf_debug(cmd, "Blit image"); |
| 1329 |
|
| 1330 |
struct hk_meta_save save; |
| 1331 |
hk_meta_begin(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1332 |
vk_meta_blit_image2(&cmd->vk, &dev->meta, pBlitImageInfo); |
| 1333 |
hk_meta_end(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1334 |
} |
| 1335 |
|
| 1336 |
VKAPI_ATTR void VKAPI_CALL |
| 1337 |
hk_CmdResolveImage2(VkCommandBuffer commandBuffer, |
| 1338 |
const VkResolveImageInfo2 *pResolveImageInfo) |
| 1339 |
{ |
| 1340 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1341 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1342 |
perf_debug(cmd, "Resolve"); |
| 1343 |
|
| 1344 |
struct hk_meta_save save; |
| 1345 |
hk_meta_begin(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1346 |
vk_meta_resolve_image2(&cmd->vk, &dev->meta, pResolveImageInfo); |
| 1347 |
hk_meta_end(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1348 |
} |
| 1349 |
|
| 1350 |
void |
| 1351 |
hk_meta_resolve_rendering(struct hk_cmd_buffer *cmd, |
| 1352 |
const VkRenderingInfo *pRenderingInfo) |
| 1353 |
{ |
| 1354 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1355 |
|
| 1356 |
struct hk_meta_save save; |
| 1357 |
hk_meta_begin(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1358 |
vk_meta_resolve_rendering(&cmd->vk, &dev->meta, pRenderingInfo); |
| 1359 |
hk_meta_end(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1360 |
} |
| 1361 |
|
| 1362 |
static void |
| 1363 |
hk_cmd_copy(struct hk_cmd_buffer *cmd, uint64_t dst, uint64_t src, size_t size) |
| 1364 |
{ |
| 1365 |
/* Use vectorized copies for as much of the buffer as possible. This requires |
| 1366 |
* that dst, src, and size are all properly aligned. Failing to check for |
| 1367 |
* alignment on the buffers causes subtle and hard-to-debug issues! |
| 1368 |
*/ |
| 1369 |
if (size >= 16 && (dst & 0xf) == 0 && (src & 0xf) == 0) { |
| 1370 |
unsigned uint4s = size / 16; |
| 1371 |
unsigned bytes = uint4s * 16; |
| 1372 |
|
| 1373 |
libagx_copy_uint4(cmd, agx_1d(uint4s), AGX_BARRIER_ALL, dst, src); |
| 1374 |
|
| 1375 |
dst += bytes; |
| 1376 |
src += bytes; |
| 1377 |
size -= bytes; |
| 1378 |
} |
| 1379 |
|
| 1380 |
if (size) { |
| 1381 |
libagx_copy_uchar(cmd, agx_1d(size), AGX_BARRIER_ALL, dst, src); |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
VKAPI_ATTR void VKAPI_CALL |
| 1386 |
hk_CmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *info) |
| 1387 |
{ |
| 1388 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1389 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1390 |
|
| 1391 |
for (unsigned i = 0; i < info->regionCount; i++) { |
| 1392 |
const VkBufferCopy2 *region = &info->pRegions[i]; |
| 1393 |
VK_FROM_HANDLE(hk_buffer, src_buffer, info->srcBuffer); |
| 1394 |
uint64_t src = hk_buffer_address_ro(src_buffer, region->srcOffset); |
| 1395 |
uint64_t dst = vk_meta_buffer_address(&dev->vk, info->dstBuffer, |
| 1396 |
region->dstOffset, region->size); |
| 1397 |
hk_cmd_copy(cmd, dst, src, region->size); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
static bool |
| 1402 |
hk_copy_requires_gfx(struct hk_image *img) |
| 1403 |
{ |
| 1404 |
return img->vk.samples > 1 && img->planes[0].layout.compressed; |
| 1405 |
} |
| 1406 |
|
| 1407 |
static bool |
| 1408 |
hk_bind_point(bool gfx) |
| 1409 |
{ |
| 1410 |
return gfx ? VK_PIPELINE_BIND_POINT_GRAPHICS |
| 1411 |
: VK_PIPELINE_BIND_POINT_COMPUTE; |
| 1412 |
} |
| 1413 |
|
| 1414 |
VKAPI_ATTR void VKAPI_CALL |
| 1415 |
hk_CmdCopyBufferToImage2(VkCommandBuffer commandBuffer, |
| 1416 |
const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) |
| 1417 |
{ |
| 1418 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1419 |
VK_FROM_HANDLE(hk_image, dst_image, pCopyBufferToImageInfo->dstImage); |
| 1420 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1421 |
|
| 1422 |
bool gfx = hk_copy_requires_gfx(dst_image); |
| 1423 |
VkPipelineBindPoint bind_point = hk_bind_point(gfx); |
| 1424 |
|
| 1425 |
struct hk_meta_save save; |
| 1426 |
hk_meta_begin(cmd, &save, bind_point); |
| 1427 |
|
| 1428 |
if (gfx) { |
| 1429 |
struct vk_meta_copy_image_properties dst_props = |
| 1430 |
hk_meta_copy_get_image_properties(dst_image); |
| 1431 |
|
| 1432 |
vk_meta_copy_buffer_to_image(&cmd->vk, &dev->meta, pCopyBufferToImageInfo, |
| 1433 |
&dst_props, bind_point); |
| 1434 |
} else { |
| 1435 |
hk_meta_copy_buffer_to_image2(&cmd->vk, &dev->meta, |
| 1436 |
pCopyBufferToImageInfo); |
| 1437 |
} |
| 1438 |
|
| 1439 |
hk_meta_end(cmd, &save, bind_point); |
| 1440 |
} |
| 1441 |
|
| 1442 |
VKAPI_ATTR void VKAPI_CALL |
| 1443 |
hk_CmdCopyImageToBuffer2(VkCommandBuffer commandBuffer, |
| 1444 |
const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) |
| 1445 |
{ |
| 1446 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1447 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1448 |
|
| 1449 |
struct hk_meta_save save; |
| 1450 |
hk_meta_begin(cmd, &save, VK_PIPELINE_BIND_POINT_COMPUTE); |
| 1451 |
hk_meta_copy_image_to_buffer2(&cmd->vk, &dev->meta, pCopyImageToBufferInfo); |
| 1452 |
hk_meta_end(cmd, &save, VK_PIPELINE_BIND_POINT_COMPUTE); |
| 1453 |
} |
| 1454 |
|
| 1455 |
VKAPI_ATTR void VKAPI_CALL |
| 1456 |
hk_CmdCopyImage2(VkCommandBuffer commandBuffer, |
| 1457 |
const VkCopyImageInfo2 *pCopyImageInfo) |
| 1458 |
{ |
| 1459 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1460 |
VK_FROM_HANDLE(hk_image, src_image, pCopyImageInfo->srcImage); |
| 1461 |
VK_FROM_HANDLE(hk_image, dst_image, pCopyImageInfo->dstImage); |
| 1462 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1463 |
bool gfx = hk_copy_requires_gfx(dst_image); |
| 1464 |
VkPipelineBindPoint bind_point = hk_bind_point(gfx); |
| 1465 |
|
| 1466 |
struct hk_meta_save save; |
| 1467 |
hk_meta_begin(cmd, &save, bind_point); |
| 1468 |
|
| 1469 |
if (gfx) { |
| 1470 |
struct vk_meta_copy_image_properties src_props = |
| 1471 |
hk_meta_copy_get_image_properties(src_image); |
| 1472 |
struct vk_meta_copy_image_properties dst_props = |
| 1473 |
hk_meta_copy_get_image_properties(dst_image); |
| 1474 |
|
| 1475 |
vk_meta_copy_image(&cmd->vk, &dev->meta, pCopyImageInfo, &src_props, |
| 1476 |
&dst_props, bind_point); |
| 1477 |
} else { |
| 1478 |
hk_meta_copy_image2(&cmd->vk, &dev->meta, pCopyImageInfo); |
| 1479 |
} |
| 1480 |
|
| 1481 |
hk_meta_end(cmd, &save, bind_point); |
| 1482 |
} |
| 1483 |
|
| 1484 |
VKAPI_ATTR void VKAPI_CALL |
| 1485 |
hk_CmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, |
| 1486 |
VkDeviceSize dstOffset, VkDeviceSize dstRange, uint32_t data) |
| 1487 |
{ |
| 1488 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1489 |
VK_FROM_HANDLE(hk_buffer, buffer, dstBuffer); |
| 1490 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1491 |
|
| 1492 |
size_t range = vk_buffer_range(&buffer->vk, dstOffset, dstRange); |
| 1493 |
uint64_t addr = |
| 1494 |
vk_meta_buffer_address(&dev->vk, dstBuffer, dstOffset, dstRange); |
| 1495 |
|
| 1496 |
if (util_is_aligned(addr, 16) && util_is_aligned(range, 16)) { |
| 1497 |
libagx_fill_uint4(cmd, agx_2d(range / 16, 1), AGX_BARRIER_ALL, |
| 1498 |
addr, 0, data, data, data, data); |
| 1499 |
} else { |
| 1500 |
libagx_fill(cmd, agx_1d(range / 4), AGX_BARRIER_ALL, addr, data); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
VKAPI_ATTR void VKAPI_CALL |
| 1505 |
hk_CmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, |
| 1506 |
VkDeviceSize dstOffset, VkDeviceSize dstRange, |
| 1507 |
const void *pData) |
| 1508 |
{ |
| 1509 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1510 |
VK_FROM_HANDLE(hk_buffer, buffer, dstBuffer); |
| 1511 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1512 |
size_t range = vk_buffer_range(&buffer->vk, dstOffset, dstRange); |
| 1513 |
|
| 1514 |
hk_cmd_copy(cmd, |
| 1515 |
vk_meta_buffer_address(&dev->vk, dstBuffer, dstOffset, dstRange), |
| 1516 |
hk_pool_upload(cmd, pData, range, 4), range); |
| 1517 |
} |
| 1518 |
|
| 1519 |
VKAPI_ATTR void VKAPI_CALL |
| 1520 |
hk_CmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount, |
| 1521 |
const VkClearAttachment *pAttachments, |
| 1522 |
uint32_t rectCount, const VkClearRect *pRects) |
| 1523 |
{ |
| 1524 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 1525 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1526 |
|
| 1527 |
struct vk_meta_rendering_info render_info; |
| 1528 |
hk_meta_init_render(cmd, &render_info); |
| 1529 |
|
| 1530 |
struct hk_meta_save save; |
| 1531 |
hk_meta_begin(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1532 |
vk_meta_clear_attachments(&cmd->vk, &dev->meta, &render_info, |
| 1533 |
attachmentCount, pAttachments, rectCount, pRects); |
| 1534 |
hk_meta_end(cmd, &save, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1535 |
} |