| 1 |
/* |
| 2 |
* Copyright (C) 2019-2022 Collabora, Ltd. |
| 3 |
* Copyright (C) 2018-2019 Alyssa Rosenzweig |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "pan_layout.h" |
| 8 |
#include "util/log.h" |
| 9 |
#include "util/macros.h" |
| 10 |
#include "util/u_math.h" |
| 11 |
#include "pan_afbc.h" |
| 12 |
#include "pan_afrc.h" |
| 13 |
#include "pan_image.h" |
| 14 |
#include "pan_mod.h" |
| 15 |
#include "pan_props.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* Computes sizes for checksumming, which is 8 bytes per 16x16 tile. |
| 19 |
* Checksumming is believed to be a CRC variant (CRC64 based on the size?). |
| 20 |
* This feature is also known as "transaction elimination". |
| 21 |
* CRC values are prefetched by 32x32 (64x64 on v12+) regions so size needs to |
| 22 |
* be aligned. |
| 23 |
*/ |
| 24 |
|
| 25 |
#define CHECKSUM_TILE_WIDTH 16 |
| 26 |
#define CHECKSUM_TILE_HEIGHT 16 |
| 27 |
#define CHECKSUM_BYTES_PER_TILE 8 |
| 28 |
|
| 29 |
static void |
| 30 |
init_slice_crc_info(unsigned arch, struct pan_image_slice_layout *slice, |
| 31 |
unsigned width_px, unsigned height_px, uint64_t offset_B) |
| 32 |
{ |
| 33 |
unsigned checksum_region_size_px = pan_meta_tile_size(arch); |
| 34 |
unsigned checksum_x_tile_per_region = |
| 35 |
(checksum_region_size_px / CHECKSUM_TILE_WIDTH); |
| 36 |
unsigned checksum_y_tile_per_region = |
| 37 |
(checksum_region_size_px / CHECKSUM_TILE_HEIGHT); |
| 38 |
|
| 39 |
unsigned tile_count_x = checksum_x_tile_per_region * |
| 40 |
DIV_ROUND_UP(width_px, checksum_region_size_px); |
| 41 |
unsigned tile_count_y = checksum_y_tile_per_region * |
| 42 |
DIV_ROUND_UP(height_px, checksum_region_size_px); |
| 43 |
|
| 44 |
slice->crc.offset_B = offset_B; |
| 45 |
slice->crc.stride_B = tile_count_x * CHECKSUM_BYTES_PER_TILE; |
| 46 |
slice->crc.size_B = slice->crc.stride_B * tile_count_y; |
| 47 |
} |
| 48 |
|
| 49 |
static struct pan_image_extent |
| 50 |
get_mip_level_extent(const struct pan_image_props *props, unsigned plane_idx, |
| 51 |
unsigned mip_level) |
| 52 |
{ |
| 53 |
return (struct pan_image_extent){ |
| 54 |
.width = u_minify(util_format_get_plane_width(props->format, plane_idx, |
| 55 |
props->extent_px.width), |
| 56 |
mip_level), |
| 57 |
.height = u_minify(util_format_get_plane_height(props->format, plane_idx, |
| 58 |
props->extent_px.height), |
| 59 |
mip_level), |
| 60 |
.depth = u_minify(props->extent_px.depth, mip_level), |
| 61 |
}; |
| 62 |
} |
| 63 |
|
| 64 |
bool |
| 65 |
pan_image_layout_init( |
| 66 |
unsigned arch, struct pan_image *image, unsigned plane_idx, |
| 67 |
const struct pan_image_layout_constraints *explicit_layout_constraints) |
| 68 |
{ |
| 69 |
assert(image->mod_handler); |
| 70 |
assert(plane_idx < ARRAY_SIZE(image->planes) && |
| 71 |
plane_idx < util_format_get_num_planes(image->props.format)); |
| 72 |
assert(image->planes[plane_idx]); |
| 73 |
|
| 74 |
const struct pan_mod_handler *mod_handler = image->mod_handler; |
| 75 |
const struct pan_image_props *props = &image->props; |
| 76 |
struct pan_image_layout *layout = &image->planes[plane_idx]->layout; |
| 77 |
|
| 78 |
/* Use explicit layout only when wsi_row_pitch_B is non-zero */ |
| 79 |
struct pan_image_layout_constraints layout_constraints = {0}; |
| 80 |
if (explicit_layout_constraints) |
| 81 |
layout_constraints = *explicit_layout_constraints; |
| 82 |
|
| 83 |
const bool use_explicit_layout = layout_constraints.wsi_row_pitch_B != 0; |
| 84 |
|
| 85 |
/* Explicit stride only work with non-mipmap, non-array, single-sample |
| 86 |
* 2D image without CRC. |
| 87 |
*/ |
| 88 |
if (use_explicit_layout && |
| 89 |
(props->extent_px.depth > 1 || props->nr_samples > 1 || |
| 90 |
props->array_size > 1 || props->dim != MALI_TEXTURE_DIMENSION_2D || |
| 91 |
props->nr_slices > 1 || props->crc)) |
| 92 |
return false; |
| 93 |
|
| 94 |
if (plane_idx >= util_format_get_num_planes(props->format)) |
| 95 |
return false; |
| 96 |
|
| 97 |
/* Init plane layout data. */ |
| 98 |
if (mod_handler->init_plane_layout && |
| 99 |
!mod_handler->init_plane_layout(props, plane_idx, layout)) |
| 100 |
return false; |
| 101 |
|
| 102 |
/* MSAA is implemented as a 3D texture with z corresponding to the |
| 103 |
* sample #, horrifyingly enough */ |
| 104 |
|
| 105 |
assert(props->extent_px.depth == 1 || props->nr_samples == 1); |
| 106 |
|
| 107 |
/* Make sure the extent/sample_count is not zero. */ |
| 108 |
assert(props->extent_px.width && props->extent_px.height && |
| 109 |
props->extent_px.depth && props->nr_samples); |
| 110 |
|
| 111 |
struct pan_image_extent mip_extent_px = { |
| 112 |
.width = util_format_get_plane_width(props->format, plane_idx, |
| 113 |
props->extent_px.width), |
| 114 |
.height = util_format_get_plane_height(props->format, plane_idx, |
| 115 |
props->extent_px.height), |
| 116 |
.depth = props->extent_px.depth, |
| 117 |
}; |
| 118 |
|
| 119 |
for (unsigned l = 0; l < props->nr_slices; ++l) { |
| 120 |
struct pan_image_slice_layout *slayout = &layout->slices[l]; |
| 121 |
|
| 122 |
if (!mod_handler->init_slice_layout(props, plane_idx, mip_extent_px, |
| 123 |
&layout_constraints, slayout)) |
| 124 |
return false; |
| 125 |
|
| 126 |
layout_constraints.offset_B += slayout->size_B; |
| 127 |
|
| 128 |
/* Add a checksum region if necessary */ |
| 129 |
if (props->crc) { |
| 130 |
init_slice_crc_info(arch, slayout, mip_extent_px.width, |
| 131 |
mip_extent_px.height, layout_constraints.offset_B); |
| 132 |
layout_constraints.offset_B += slayout->crc.size_B; |
| 133 |
slayout->size_B += slayout->crc.size_B; |
| 134 |
} |
| 135 |
|
| 136 |
mip_extent_px.width = u_minify(mip_extent_px.width, 1); |
| 137 |
mip_extent_px.height = u_minify(mip_extent_px.height, 1); |
| 138 |
mip_extent_px.depth = u_minify(mip_extent_px.depth, 1); |
| 139 |
} |
| 140 |
|
| 141 |
/* Arrays and cubemaps have the entire miptree duplicated */ |
| 142 |
layout->array_stride_B = |
| 143 |
ALIGN_POT(layout_constraints.offset_B - layout->slices[0].offset_B, 64); |
| 144 |
|
| 145 |
if (use_explicit_layout) { |
| 146 |
layout->data_size_B = |
| 147 |
layout_constraints.offset_B - explicit_layout_constraints->offset_B; |
| 148 |
} else { |
| 149 |
/* Native images start from offset 0, and the planar plane offset has |
| 150 |
* been at least 4K page aligned below. So the base level slice offset |
| 151 |
* should always be the same with the plane offset. |
| 152 |
*/ |
| 153 |
assert(!explicit_layout_constraints || |
| 154 |
explicit_layout_constraints->offset_B == |
| 155 |
layout->slices[0].offset_B); |
| 156 |
layout->data_size_B = ALIGN_POT( |
| 157 |
(uint64_t)layout->array_stride_B * (uint64_t)props->array_size, 4096); |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |