Debian Codegraph @

src/asahi/vulkan/hk_cmd_clear.c

raw
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 "util/format/u_formats.h"
8 #include "util/macros.h"
9 #include "util/u_math.h"
10 #include "vulkan/vulkan_core.h"
11 #include "hk_cmd_buffer.h"
12 #include "layout.h"
13
14 #include "hk_device.h"
15 #include "hk_entrypoints.h"
16 #include "hk_image.h"
17 #include "hk_image_view.h"
18 #include "hk_physical_device.h"
19
20 #include "libagx_dgc.h"
21 #include "libagx_shaders.h"
22
23 void
24 hk_clear_image(struct hk_cmd_buffer *cmd, struct hk_image *image,
25 enum pipe_format view_format, const uint32_t *clear_value,
26 const VkImageSubresourceRange *range, bool whole_3d)
27 {
28 const uint32_t level_count =
29 vk_image_subresource_level_count(&image->vk, range);
30
31 bool z = (range->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
32 bool s = (range->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
33
34 unsigned first_plane = (s && !z) ? 1 : 0;
35 unsigned last_plane = s ? 1 : 0;
36
37 if (image->plane_count == 1) {
38 first_plane = 0;
39 last_plane = 0;
40 }
41
42 for (unsigned plane = first_plane; plane <= last_plane; ++plane) {
43 struct ail_layout *layout = &image->planes[plane].layout;
44 perf_debug(cmd, "Image clear (%scompressed)",
45 layout->compressed ? "" : "un");
46
47 for (uint32_t l = 0; l < level_count; l++) {
48 const uint32_t level = range->baseMipLevel + l;
49
50 const VkExtent3D level_extent =
51 vk_image_mip_level_extent(&image->vk, level);
52
53 uint32_t base_array_layer, layer_count;
54 if (image->vk.image_type == VK_IMAGE_TYPE_3D && whole_3d) {
55 base_array_layer = 0;
56 layer_count = level_extent.depth;
57 } else {
58 base_array_layer = range->baseArrayLayer;
59 layer_count = vk_image_subresource_layer_count(&image->vk, range);
60 }
61
62 enum pipe_format format = view_format ? view_format : layout->format;
63 bool stencil = format == PIPE_FORMAT_S8_UINT;
64
65 if (stencil) {
66 format = PIPE_FORMAT_R8_UINT;
67 } else if (format == PIPE_FORMAT_Z16_UNORM) {
68 format = PIPE_FORMAT_R16_UNORM;
69 } else if (format == PIPE_FORMAT_Z32_FLOAT) {
70 format = PIPE_FORMAT_R32_FLOAT;
71 }
72
73 uint32_t c[4];
74 util_format_pack_rgba(format, c, &clear_value[stencil ? 1 : 0], 1);
75
76 unsigned blocksize_B = util_format_get_blocksize(format);
77 assert(util_is_power_of_two_nonzero(blocksize_B) && blocksize_B <= 16);
78
79 /* Splat out to 128-bit */
80 uint8_t *bytes = (uint8_t *)c;
81 for (unsigned i = 1; i < 16; ++i) {
82 bytes[i] = bytes[i % blocksize_B];
83 }
84
85 uint64_t address =
86 image->planes[plane].addr +
87 ail_get_layer_level_B(layout, base_array_layer, level);
88
89 uint32_t size = ail_get_level_size_B(layout, level);
90
91 assert((layout->layer_stride_B % 16) == 0 && "aligned");
92 uint32_t layer_stride_uint4 = layout->layer_stride_B / 16;
93
94 if (ail_is_level_logically_compressed(layout, level)) {
95 assert((layout->compression_layer_stride_B % 16) == 0 && "aligned");
96 uint32_t meta_layer_stride_tl =
97 layout->compression_layer_stride_B / 8;
98
99 uint64_t meta_addr =
100 image->planes[plane].addr + layout->metadata_offset_B +
101 (base_array_layer * layout->compression_layer_stride_B) +
102 layout->level_offsets_compressed_B[level];
103
104 uint32_t word = (uint32_t)ail_tile_mode_solid(format);
105
106 struct agx_grid grid =
107 agx_3d(ail_metadata_width_tl(layout, level),
108 ail_metadata_height_tl(layout, level), layer_count);
109
110 struct ail_tile tilesize = layout->tilesize_el[level];
111
112 libagx_fast_clear(cmd, grid, AGX_BARRIER_ALL, meta_addr, address,
113 grid.count[0], grid.count[1], tilesize.width_el,
114 tilesize.height_el, meta_layer_stride_tl,
115 layer_stride_uint4, util_logbase2(blocksize_B),
116 util_logbase2(layout->sample_count_sa), c[0],
117 c[1], c[2], c[3], word);
118 } else {
119 libagx_fill_uint4(cmd, agx_2d(DIV_ROUND_UP(size, 16), layer_count),
120 AGX_BARRIER_ALL, address, layer_stride_uint4,
121 c[0], c[1], c[2], c[3]);
122 }
123 }
124 }
125 }
126
127 VKAPI_ATTR void VKAPI_CALL
128 hk_CmdClearColorImage(VkCommandBuffer commandBuffer, VkImage _image,
129 VkImageLayout imageLayout,
130 const VkClearColorValue *pColor, uint32_t rangeCount,
131 const VkImageSubresourceRange *pRanges)
132 {
133 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
134 VK_FROM_HANDLE(hk_image, image, _image);
135
136 for (uint32_t r = 0; r < rangeCount; r++) {
137 hk_clear_image(cmd, image, PIPE_FORMAT_NONE, pColor->uint32, &pRanges[r],
138 true /* whole 3D */);
139 }
140 }
141
142 VKAPI_ATTR void VKAPI_CALL
143 hk_CmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage _image,
144 VkImageLayout imageLayout,
145 const VkClearDepthStencilValue *pDepthStencil,
146 uint32_t rangeCount,
147 const VkImageSubresourceRange *pRanges)
148 {
149 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
150 VK_FROM_HANDLE(hk_image, image, _image);
151
152 for (uint32_t r = 0; r < rangeCount; r++) {
153 uint32_t colour[4] = {fui(pDepthStencil->depth), pDepthStencil->stencil};
154 hk_clear_image(cmd, image, PIPE_FORMAT_NONE, colour, &pRanges[r],
155 true /* whole 3D */);
156 }
157 }