| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#include "compiler/libcl/libcl.h" |
| 6 |
#include "compiler/nir/nir_defines.h" |
| 7 |
#include "compiler/shader_enums.h" |
| 8 |
#include "agx_pack.h" |
| 9 |
#include "compression.h" |
| 10 |
#include "libagx_intrinsics.h" |
| 11 |
|
| 12 |
/* |
| 13 |
* Decompress in place. The metadata is updated, so other processes can read the |
| 14 |
* image with a compressed texture descriptor. |
| 15 |
* |
| 16 |
* Each workgroup processes one 16x16 tile, avoiding races. We use 32x1 |
| 17 |
* workgroups, matching the warp size, meaning each work-item must process |
| 18 |
* (16*16)/(32*1) = 8 sampels. Matching the warp size eliminates cross-warp |
| 19 |
* barriers. It also minimizes launched threads, accelerating the early exit. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* Our compiler represents a bindless handle as a uint2 of a uniform base and an |
| 23 |
* offset in bytes. Since the descriptors are all in the u0_u1 push, the former |
| 24 |
* is hardcoded and the latter is an offsetof. |
| 25 |
*/ |
| 26 |
#define HANDLE(field) \ |
| 27 |
nir_bindless_image_agx(offsetof(struct libagx_decompress_images, field), 0) |
| 28 |
|
| 29 |
/* |
| 30 |
* The metadata buffer is fully twiddled, so interleave the X/Y coordinate bits. |
| 31 |
* While dimensions are padded to powers-of-two, they are not padded to a |
| 32 |
* square. If the width is more than 2x the height or vice versa, the additional |
| 33 |
* bits are linear. So we interleave as much as possible, and then add what's |
| 34 |
* remaining. Finally, layers are strided linear and added at the end. |
| 35 |
*/ |
| 36 |
static uint |
| 37 |
index_metadata(uint3 c, uint width, uint height, uint layer_stride) |
| 38 |
{ |
| 39 |
uint major_coord = width > height ? c.x : c.y; |
| 40 |
uint minor_dim = min(width, height); |
| 41 |
|
| 42 |
uint intl_bits = util_logbase2_ceil(minor_dim); |
| 43 |
uint intl_mask = (1 << intl_bits) - 1; |
| 44 |
uint2 intl_coords = c.xy & intl_mask; |
| 45 |
|
| 46 |
return nir_interleave(intl_coords.x, intl_coords.y) + |
| 47 |
((major_coord & ~intl_mask) << intl_bits) + (layer_stride * c.z); |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* For multisampled images, a 2x2 or 1x2 group of samples form a single pixel. |
| 52 |
* The following two helpers convert a coordinate in samples into a coordinate |
| 53 |
* in pixels and a sample ID, respectively. They each assume that samples > 1. |
| 54 |
*/ |
| 55 |
static int4 |
| 56 |
decompose_px(int4 c, uint samples) |
| 57 |
{ |
| 58 |
if (samples == 4) |
| 59 |
c.xy >>= 1; |
| 60 |
else |
| 61 |
c.y >>= 1; |
| 62 |
|
| 63 |
return c; |
| 64 |
} |
| 65 |
|
| 66 |
static uint |
| 67 |
sample_id(int4 c, uint samples) |
| 68 |
{ |
| 69 |
if (samples == 4) |
| 70 |
return (c.x & 1) | ((c.y & 1) << 1); |
| 71 |
else |
| 72 |
return c.y & 1; |
| 73 |
} |
| 74 |
|
| 75 |
KERNEL(32) |
| 76 |
libagx_decompress(constant struct libagx_decompress_images *images, |
| 77 |
global uint64_t *metadata, uint64_t tile_uncompressed, |
| 78 |
uint32_t metadata_layer_stride_tl, uint16_t metadata_width_tl, |
| 79 |
uint16_t metadata_height_tl, |
| 80 |
uint log2_samples__3 /* 1x, 2x, 4x */) |
| 81 |
{ |
| 82 |
uint samples = 1 << log2_samples__3; |
| 83 |
|
| 84 |
/* Index into the metadata buffer */ |
| 85 |
uint index_tl = index_metadata(cl_group_id, metadata_width_tl, |
| 86 |
metadata_height_tl, metadata_layer_stride_tl); |
| 87 |
|
| 88 |
/* If the tile is already uncompressed, there's nothing to do. */ |
| 89 |
if (metadata[index_tl] == tile_uncompressed) |
| 90 |
return; |
| 91 |
|
| 92 |
/* Tiles are 16x16 */ |
| 93 |
uint2 coord_sa = (cl_group_id.xy * 16); |
| 94 |
uint layer = cl_group_id.z; |
| 95 |
|
| 96 |
/* Since we use a 32x1 workgroup, each work-item handles half of a row. */ |
| 97 |
uint offs_y_sa = cl_local_id.x >> 1; |
| 98 |
uint offs_x_sa = (cl_local_id.x & 1) ? 8 : 0; |
| 99 |
|
| 100 |
int2 img_coord_sa_2d = convert_int2(coord_sa) + (int2)(offs_x_sa, offs_y_sa); |
| 101 |
int4 img_coord_sa = (int4)(img_coord_sa_2d.x, img_coord_sa_2d.y, layer, 0); |
| 102 |
|
| 103 |
/* Read our half-row into registers. */ |
| 104 |
uint4 texels[8]; |
| 105 |
for (uint i = 0; i < 8; ++i) { |
| 106 |
int4 c_sa = img_coord_sa + (int4)(i, 0, 0, 0); |
| 107 |
if (samples == 1) { |
| 108 |
texels[i] = nir_bindless_image_load( |
| 109 |
HANDLE(compressed), c_sa, 0, 0, GLSL_SAMPLER_DIM_2D, true, 0, |
| 110 |
ACCESS_IN_BOUNDS, nir_type_uint32); |
| 111 |
} else { |
| 112 |
int4 dec_px = decompose_px(c_sa, samples); |
| 113 |
texels[i] = nir_bindless_image_load( |
| 114 |
HANDLE(compressed), dec_px, sample_id(c_sa, samples), 0, |
| 115 |
GLSL_SAMPLER_DIM_MS, true, 0, ACCESS_IN_BOUNDS, |
| 116 |
nir_type_uint32); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub_group_barrier(CLK_LOCAL_MEM_FENCE); |
| 121 |
|
| 122 |
/* Now that the whole tile is read, we write without racing. */ |
| 123 |
for (uint i = 0; i < 8; ++i) { |
| 124 |
int4 c_sa = img_coord_sa + (int4)(i, 0, 0, 0); |
| 125 |
if (samples == 1) { |
| 126 |
nir_bindless_image_store(HANDLE(uncompressed), c_sa, 0, texels[i], 0, |
| 127 |
GLSL_SAMPLER_DIM_2D, true, 0, |
| 128 |
ACCESS_NON_READABLE, nir_type_uint32); |
| 129 |
} else { |
| 130 |
int4 dec_px = decompose_px(c_sa, samples); |
| 131 |
|
| 132 |
nir_bindless_image_store(HANDLE(uncompressed), dec_px, |
| 133 |
sample_id(c_sa, samples), texels[i], 0, |
| 134 |
GLSL_SAMPLER_DIM_MS, true, 0, |
| 135 |
ACCESS_NON_READABLE, nir_type_uint32); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/* We've replaced the body buffer. Mark the tile as uncompressed. */ |
| 140 |
if (cl_local_id.x == 0) { |
| 141 |
metadata[index_tl] = tile_uncompressed; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static ushort2 |
| 146 |
stretched_sa_to_px(ushort2 px, uint samples) |
| 147 |
{ |
| 148 |
/* clang-format off */ |
| 149 |
switch (samples) { |
| 150 |
case 4: return (ushort2)(px.x / 2, px.y / 2); |
| 151 |
case 2: return (ushort2)(px.x, px.y / 2); |
| 152 |
case 1: return px; |
| 153 |
default: UNREACHABLE("invalid sample count"); |
| 154 |
} |
| 155 |
/* clang-format on */ |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Compute kernel to clear a compressed image to a solid colour by filling the |
| 160 |
* compression metadata to all solid and writing the colour to each tile. |
| 161 |
* |
| 162 |
* Invoked as <width in 16x16 tiles, height in 16x16 tiles, layers>. Each |
| 163 |
* invocation clears a single 16x16 compression tile. |
| 164 |
* |
| 165 |
* Despite the ALU complexity, this is twice as fast as clearing with the |
| 166 |
* 3D pipe for a few reasons. |
| 167 |
* |
| 168 |
* 1. At a kernel/firmware level, CDM submission is much faster than VDM. |
| 169 |
* 2. Far fewer threads are dispatched (1 per 16x16 tile, rather than 1 per |
| 170 |
* pixel like the background program used to implement 3D clears). |
| 171 |
* 3. No useless tilebuffer load/stores. |
| 172 |
* 4. Many CDM dispatches can share a single control stream, whereas a VDM |
| 173 |
* control stream covers only 1 render pass. |
| 174 |
* |
| 175 |
* AGX compression is not fully understood, but we know enough to clear solid |
| 176 |
* colours: |
| 177 |
* |
| 178 |
* - Metadata buffer has one byte per 8x4 subtile, grouped into 16x16 tiles with |
| 179 |
* 8 bytes of metadata. |
| 180 |
* - The 8x4 "solid mode" will read its colour from the beginning of the tile in |
| 181 |
* the body buffer, addressed as-if twiddled. To clear the whole tile, we need |
| 182 |
* (16x16) / (8x4) = 8 copies of the colour per 16x16 tile. |
| 183 |
* |
| 184 |
* Also, multisampling requires stretching the coordinates. This corresponds to |
| 185 |
* ail_effective_width_sa and ail_effective_height_sa. With multisampling, we |
| 186 |
* are invoked on tiles of 16x16 samples, so we need to squish back to tiles of |
| 187 |
* 16x8 or 8x8 pixels. |
| 188 |
*/ |
| 189 |
KERNEL(32) |
| 190 |
libagx_fast_clear(global uint2 *meta, global uint4 *body, uint width_tl, |
| 191 |
uint height_tl, uint tile_w_el, uint tile_h_el, |
| 192 |
uint meta_layer_stride_tl, uint body_layer_stride_uint4, |
| 193 |
uint blocksize_B_log2__5, uint samples_log2__3, uint a, |
| 194 |
uint b, uint c, uint d, uint solid_mode) |
| 195 |
{ |
| 196 |
uint samples = 1 << samples_log2__3; |
| 197 |
uint blocksize_B = 1 << blocksize_B_log2__5; |
| 198 |
|
| 199 |
/* Set the tile to be solid */ |
| 200 |
meta[index_metadata(cl_global_id, width_tl, height_tl, meta_layer_stride_tl)] = |
| 201 |
solid_mode; |
| 202 |
|
| 203 |
/* SWAR vectorized multiply by 16, we know this won't overflow */ |
| 204 |
ushort2 coord_el = |
| 205 |
as_ushort2(as_uint(convert_ushort2(cl_global_id.xy)) << 4); |
| 206 |
|
| 207 |
/* Calculate the offset of the tile in the body buffer */ |
| 208 |
ushort2 dim_sa = (ushort2)(width_tl, height_tl) * (ushort)16; |
| 209 |
uint aligned_width_px = |
| 210 |
align(stretched_sa_to_px(dim_sa, samples).x, tile_w_el); |
| 211 |
|
| 212 |
ushort2 coord_px = stretched_sa_to_px(coord_el, samples); |
| 213 |
uint twiddled = libagx_twiddle_coordinates(coord_px, tile_w_el, tile_h_el, |
| 214 |
aligned_width_px); |
| 215 |
|
| 216 |
/* We scale blocksize_B by # of samples to make addressing work properly and |
| 217 |
* to splat the clear colour across the samples. See the corresponding scale |
| 218 |
* in libagx_image_texel_address. |
| 219 |
* |
| 220 |
* TODO: Check that this is actually right? Passes CTS at least. |
| 221 |
*/ |
| 222 |
blocksize_B *= samples; |
| 223 |
uint body_offs = (twiddled * blocksize_B) / sizeof(uint4); |
| 224 |
body_offs += cl_global_id.z * body_layer_stride_uint4; |
| 225 |
|
| 226 |
/* Write 8 copies of the clear colour. We vectorize with 128-bit writes, |
| 227 |
* unless 8 copies would be less than 128-bit which only occurs for 8-bit |
| 228 |
* formats where we write only 64-bit. |
| 229 |
*/ |
| 230 |
if (blocksize_B == 1) { |
| 231 |
*(global uint2 *)(body + body_offs) = (uint2)(a, b); |
| 232 |
} else { |
| 233 |
for (uint i = 0; i < (8 * blocksize_B) / 16; ++i) { |
| 234 |
body[body_offs + i] = (uint4)(a, b, c, d); |
| 235 |
} |
| 236 |
} |
| 237 |
} |