mesa/src/broadcom/common/v3d_util.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

326 lines
11 KiB
C
Raw Normal View History

/*
* Copyright © 2021 Raspberry Pi Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "v3d_util.h"
broadcom/common: add tile alloc block size macros and sizing helper Add V3D_TILE_ALLOC_INITIAL_BLOCK_SIZE = 128 and V3D_TILE_ALLOC_OVERFLOW_BLOCK_SIZE = 64 to v3d_limits.h. Corresponding _ENUM macros provide the 2-bit hardware encoding for the TILE_BINNING_MODE_CFG packets. The previous implicit 64B initial blocks were too small: a single draw call emits ~88 bytes of per-tile BCL state, immediately overflowing into continuation blocks. 128B initial blocks avoid the first continuation allocation for simple single-draw passes. Add v3d_tile_alloc_sizes() to v3d_util with the full tile alloc BO and TSDA sizing logic. This uses the 128B initial blocks and tile_alloc becomes proportional to the number of draws and size of the initial blocks allocation with the cap of the previous fixed allocation. So jobs with 0 or 1 drawcalls (blits/fills) reduce their headroom dramatically. The draw-proportional formula replaces a flat 512 KB continuation pool: headroom = MIN2((tiles_size * draw_count) / 2, 512 KB) Benchmarked on RPi5 (V3D 7.1) against GfxBench GL tests and apitrace replays at 1080p. Tile-alloc memory reduction versus the flat 512 KB headroom (taking into account 256kb kernel alloc per OOM): GfxBench (5 benchmarks): -45% to -70% reduction, OOM at or below baseline Apitrace (19 traces): -4% to -77% reduction on 20/24 traces No FPS regressions observed on any workload. Reviewed-by: Maíra Canal <mcanal@igalia.com> Reviewed-by: Iago Toral Quiroga <itoral@igalia.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40554>
2026-03-19 10:37:40 +01:00
#include "v3d_limits.h"
#include "util/macros.h"
broadcom/common: add tile alloc block size macros and sizing helper Add V3D_TILE_ALLOC_INITIAL_BLOCK_SIZE = 128 and V3D_TILE_ALLOC_OVERFLOW_BLOCK_SIZE = 64 to v3d_limits.h. Corresponding _ENUM macros provide the 2-bit hardware encoding for the TILE_BINNING_MODE_CFG packets. The previous implicit 64B initial blocks were too small: a single draw call emits ~88 bytes of per-tile BCL state, immediately overflowing into continuation blocks. 128B initial blocks avoid the first continuation allocation for simple single-draw passes. Add v3d_tile_alloc_sizes() to v3d_util with the full tile alloc BO and TSDA sizing logic. This uses the 128B initial blocks and tile_alloc becomes proportional to the number of draws and size of the initial blocks allocation with the cap of the previous fixed allocation. So jobs with 0 or 1 drawcalls (blits/fills) reduce their headroom dramatically. The draw-proportional formula replaces a flat 512 KB continuation pool: headroom = MIN2((tiles_size * draw_count) / 2, 512 KB) Benchmarked on RPi5 (V3D 7.1) against GfxBench GL tests and apitrace replays at 1080p. Tile-alloc memory reduction versus the flat 512 KB headroom (taking into account 256kb kernel alloc per OOM): GfxBench (5 benchmarks): -45% to -70% reduction, OOM at or below baseline Apitrace (19 traces): -4% to -77% reduction on 20/24 traces No FPS regressions observed on any workload. Reviewed-by: Maíra Canal <mcanal@igalia.com> Reviewed-by: Iago Toral Quiroga <itoral@igalia.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40554>
2026-03-19 10:37:40 +01:00
#include "util/u_math.h"
/* Choose a number of workgroups per supergroup that maximizes
* lane occupancy. We can pack up to 16 workgroups into a supergroup.
*/
uint32_t
v3d_csd_choose_workgroups_per_supergroup(struct v3d_device_info *devinfo,
bool can_use_supergroups,
bool has_tsy_barrier,
uint32_t threads,
uint32_t num_wgs,
uint32_t wg_size)
{
/* FIXME: Some subgroups may restrict supergroup packing. For now,
* if the shader has subgroups, we only allow the ones that support
* supergroup packing.
*/
if (!can_use_supergroups)
return 1;
/* If the workgroup size is a multiple of 16 (elements per batch),
* the lane occupancy is already maximized.
*/
if (wg_size % 16 == 0)
return 1;
/* Compute maximum number of batches in a supergroup for this workgroup size.
* Each batch is 16 elements, and we can have up to 16 work groups in a
* supergroup:
*
* max_batches_per_sg = (wg_size * max_wgs_per_sg) / elements_per_batch
* since max_wgs_per_sg = 16 and elements_per_batch = 16, we get:
* max_batches_per_sg = wg_size
*/
uint32_t max_batches_per_sg = wg_size;
/* QPU threads will stall at TSY barriers until the entire supergroup
* reaches the barrier. Limit the supergroup size to half the QPU threads
* available, so we can have at least 2 supergroups executing in parallel
* and we don't stall all our QPU threads when a supergroup hits a barrier.
*/
uint32_t max_wgs_per_sg = 16;
if (has_tsy_barrier) {
uint32_t max_qpu_threads = devinfo->qpu_count * threads;
max_batches_per_sg = MIN2(max_batches_per_sg, max_qpu_threads / 2);
max_wgs_per_sg = max_batches_per_sg * 16 / wg_size;
}
uint32_t best_wgs_per_sg = 1;
uint32_t best_unused_lanes = 16;
for (uint32_t wgs_per_sg = 1; wgs_per_sg <= max_wgs_per_sg; wgs_per_sg++) {
/* Don't try to pack more workgroups per supergroup than the total amount
* of workgroups dispatched.
*/
if (wgs_per_sg > num_wgs)
return best_wgs_per_sg;
/* Compute wasted lines for this configuration and keep track of the
* config with less waste.
*/
uint32_t unused_lanes = (16 - ((wgs_per_sg * wg_size) % 16)) & 0x0f;
if (unused_lanes == 0)
return wgs_per_sg;
if (unused_lanes < best_unused_lanes) {
best_wgs_per_sg = wgs_per_sg;
best_unused_lanes = unused_lanes;
}
}
return best_wgs_per_sg;
}
#define V3D71_TLB_COLOR_SIZE (16 * 1024)
#define V3D71_TLB_DETPH_SIZE (16 * 1024)
#define V3D71_TLB_AUX_DETPH_SIZE (8 * 1024)
static bool
tile_size_valid(uint32_t pixel_count, uint32_t color_bpp, uint32_t depth_bpp)
{
/* First, we check if we can fit this tile size allocating the depth
* TLB memory to color.
*/
if (pixel_count * depth_bpp <= V3D71_TLB_AUX_DETPH_SIZE &&
pixel_count * color_bpp <= V3D71_TLB_COLOR_SIZE + V3D71_TLB_DETPH_SIZE) {
return true;
}
/* Otherwise the tile must fit in the main TLB buffers */
return pixel_count * depth_bpp <= V3D71_TLB_DETPH_SIZE &&
pixel_count * color_bpp <= V3D71_TLB_COLOR_SIZE;
}
void
v3d_choose_tile_size(const struct v3d_device_info *devinfo,
uint32_t color_attachment_count,
/* V3D 4.x max internal bpp of all RTs */
uint32_t max_internal_bpp,
/* V3D 7.x accumulated bpp for all RTs (in bytes) */
uint32_t total_color_bpp,
bool msaa,
bool double_buffer,
uint32_t *width,
uint32_t *height)
{
static const uint8_t tile_sizes[] = {
64, 64,
64, 32,
32, 32,
32, 16,
16, 16,
16, 8,
8, 8
};
uint32_t idx = 0;
if (devinfo->ver >= 71) {
/* In V3D 7.x, we use the actual bpp used by color attachments to compute
* the tile size instead of the maximum bpp. This may allow us to choose a
* larger tile size than we would in 4.x in scenarios with multiple RTs
* with different bpps.
*
* Also, the TLB has an auxiliary buffer of 8KB that will be automatically
* used for depth instead of the main 16KB depth TLB buffer when the depth
* tile fits in the auxiliary buffer, allowing the hardware to allocate
* the 16KB from the main depth TLB to the color TLB. If we can do that,
* then we are effectively doubling the memory we have for color and we
* can also select a larger tile size. This is necessary to support
* the most expensive configuration: 8x128bpp RTs + MSAA.
*
* FIXME: the docs state that depth TLB memory can be used for color
* if depth testing is not used by setting the 'depth disable' bit in the
* rendering configuration. However, this comes with a requirement that
* occlussion queries must not be active. We need to clarify if this means
* active at the point at which we emit a tile rendering configuration
* item, meaning that the we have a query spanning a full render pass
* (this is something we can tell before we emit the rendering
* configuration item) or active in the subpass for which we are enabling
* the bit (which we can't tell until later, when we record commands for
* the subpass). If it is the latter, then we cannot use this feature.
*/
const uint32_t color_bpp = total_color_bpp * (msaa ? 4 : 1);
const uint32_t depth_bpp = 4 * (msaa ? 4 : 1);
do {
const uint32_t tile_w = tile_sizes[idx * 2];
const uint32_t tile_h = tile_sizes[idx * 2 + 1];
if (tile_size_valid(tile_w * tile_h, color_bpp, depth_bpp))
break;
idx++;
} while (idx < ARRAY_SIZE(tile_sizes) / 2);
if (double_buffer)
idx += 1;
} else {
/* On V3D 4.x tile size is selected based on the number of RTs, the
* maximum bpp across all of them and whether 4x MSAA is used.
*/
if (color_attachment_count > 4)
idx += 3;
else if (color_attachment_count > 2)
idx += 2;
else if (color_attachment_count > 1)
idx += 1;
/* MSAA and double-buffer are mutually exclusive */
assert(!msaa || !double_buffer);
if (msaa)
idx += 2;
else if (double_buffer)
idx += 1;
idx += max_internal_bpp;
}
assert(idx < ARRAY_SIZE(tile_sizes) / 2);
*width = tile_sizes[idx * 2];
*height = tile_sizes[idx * 2 + 1];
}
/* Translates a pipe swizzle to the swizzle values used in the
* TEXTURE_SHADER_STATE packet.
*/
uint32_t
v3d_translate_pipe_swizzle(enum pipe_swizzle swizzle)
{
switch (swizzle) {
case PIPE_SWIZZLE_0:
return 0;
case PIPE_SWIZZLE_1:
return 1;
case PIPE_SWIZZLE_X:
case PIPE_SWIZZLE_Y:
case PIPE_SWIZZLE_Z:
case PIPE_SWIZZLE_W:
return 2 + swizzle;
default:
build: avoid redefining unreachable() which is standard in C23 In the C23 standard unreachable() is now a predefined function-like macro in <stddef.h> See https://android.googlesource.com/platform/bionic/+/HEAD/docs/c23.md#is-now-a-predefined-function_like-macro-in And this causes build errors when building for C23: ----------------------------------------------------------------------- In file included from ../src/util/log.h:30, from ../src/util/log.c:30: ../src/util/macros.h:123:9: warning: "unreachable" redefined 123 | #define unreachable(str) \ | ^~~~~~~~~~~ In file included from ../src/util/macros.h:31: /usr/lib/gcc/x86_64-linux-gnu/14/include/stddef.h:456:9: note: this is the location of the previous definition 456 | #define unreachable() (__builtin_unreachable ()) | ^~~~~~~~~~~ ----------------------------------------------------------------------- So don't redefine it with the same name, but use the name UNREACHABLE() to also signify it's a macro. Using a different name also makes sense because the behavior of the macro was extending the one of __builtin_unreachable() anyway, and it also had a different signature, accepting one argument, compared to the standard unreachable() with no arguments. This change improves the chances of building mesa with the C23 standard, which for instance is the default in recent AOSP versions. All the instances of the macro, including the definition, were updated with the following command line: git grep -l '[^_]unreachable(' -- "src/**" | sort | uniq | \ while read file; \ do \ sed -e 's/\([^_]\)unreachable(/\1UNREACHABLE(/g' -i "$file"; \ done && \ sed -e 's/#undef unreachable/#undef UNREACHABLE/g' -i src/intel/isl/isl_aux_info.c Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
2025-07-23 09:17:35 +02:00
UNREACHABLE("unknown swizzle");
}
}
/* Translates a pipe primitive type to a hw value we can use in the various
* draw packets.
*/
uint32_t
v3d_hw_prim_type(enum mesa_prim prim_type)
{
switch (prim_type) {
case MESA_PRIM_POINTS:
case MESA_PRIM_LINES:
case MESA_PRIM_LINE_LOOP:
case MESA_PRIM_LINE_STRIP:
case MESA_PRIM_TRIANGLES:
case MESA_PRIM_TRIANGLE_STRIP:
case MESA_PRIM_TRIANGLE_FAN:
return prim_type;
case MESA_PRIM_LINES_ADJACENCY:
case MESA_PRIM_LINE_STRIP_ADJACENCY:
case MESA_PRIM_TRIANGLES_ADJACENCY:
case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
return 8 + (prim_type - MESA_PRIM_LINES_ADJACENCY);
default:
build: avoid redefining unreachable() which is standard in C23 In the C23 standard unreachable() is now a predefined function-like macro in <stddef.h> See https://android.googlesource.com/platform/bionic/+/HEAD/docs/c23.md#is-now-a-predefined-function_like-macro-in And this causes build errors when building for C23: ----------------------------------------------------------------------- In file included from ../src/util/log.h:30, from ../src/util/log.c:30: ../src/util/macros.h:123:9: warning: "unreachable" redefined 123 | #define unreachable(str) \ | ^~~~~~~~~~~ In file included from ../src/util/macros.h:31: /usr/lib/gcc/x86_64-linux-gnu/14/include/stddef.h:456:9: note: this is the location of the previous definition 456 | #define unreachable() (__builtin_unreachable ()) | ^~~~~~~~~~~ ----------------------------------------------------------------------- So don't redefine it with the same name, but use the name UNREACHABLE() to also signify it's a macro. Using a different name also makes sense because the behavior of the macro was extending the one of __builtin_unreachable() anyway, and it also had a different signature, accepting one argument, compared to the standard unreachable() with no arguments. This change improves the chances of building mesa with the C23 standard, which for instance is the default in recent AOSP versions. All the instances of the macro, including the definition, were updated with the following command line: git grep -l '[^_]unreachable(' -- "src/**" | sort | uniq | \ while read file; \ do \ sed -e 's/\([^_]\)unreachable(/\1UNREACHABLE(/g' -i "$file"; \ done && \ sed -e 's/#undef unreachable/#undef UNREACHABLE/g' -i src/intel/isl/isl_aux_info.c Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
2025-07-23 09:17:35 +02:00
UNREACHABLE("Unsupported primitive type");
}
}
uint32_t
v3d_internal_bpp_words(uint32_t internal_bpp)
{
switch (internal_bpp) {
case 0 /* V3D_INTERNAL_BPP_32 */:
return 1;
case 1 /* V3D_INTERNAL_BPP_64 */:
return 2;
case 2 /* V3D_INTERNAL_BPP_128 */:
return 4;
default:
build: avoid redefining unreachable() which is standard in C23 In the C23 standard unreachable() is now a predefined function-like macro in <stddef.h> See https://android.googlesource.com/platform/bionic/+/HEAD/docs/c23.md#is-now-a-predefined-function_like-macro-in And this causes build errors when building for C23: ----------------------------------------------------------------------- In file included from ../src/util/log.h:30, from ../src/util/log.c:30: ../src/util/macros.h:123:9: warning: "unreachable" redefined 123 | #define unreachable(str) \ | ^~~~~~~~~~~ In file included from ../src/util/macros.h:31: /usr/lib/gcc/x86_64-linux-gnu/14/include/stddef.h:456:9: note: this is the location of the previous definition 456 | #define unreachable() (__builtin_unreachable ()) | ^~~~~~~~~~~ ----------------------------------------------------------------------- So don't redefine it with the same name, but use the name UNREACHABLE() to also signify it's a macro. Using a different name also makes sense because the behavior of the macro was extending the one of __builtin_unreachable() anyway, and it also had a different signature, accepting one argument, compared to the standard unreachable() with no arguments. This change improves the chances of building mesa with the C23 standard, which for instance is the default in recent AOSP versions. All the instances of the macro, including the definition, were updated with the following command line: git grep -l '[^_]unreachable(' -- "src/**" | sort | uniq | \ while read file; \ do \ sed -e 's/\([^_]\)unreachable(/\1UNREACHABLE(/g' -i "$file"; \ done && \ sed -e 's/#undef unreachable/#undef UNREACHABLE/g' -i src/intel/isl/isl_aux_info.c Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
2025-07-23 09:17:35 +02:00
UNREACHABLE("Unsupported internal BPP");
}
}
broadcom/common: add tile alloc block size macros and sizing helper Add V3D_TILE_ALLOC_INITIAL_BLOCK_SIZE = 128 and V3D_TILE_ALLOC_OVERFLOW_BLOCK_SIZE = 64 to v3d_limits.h. Corresponding _ENUM macros provide the 2-bit hardware encoding for the TILE_BINNING_MODE_CFG packets. The previous implicit 64B initial blocks were too small: a single draw call emits ~88 bytes of per-tile BCL state, immediately overflowing into continuation blocks. 128B initial blocks avoid the first continuation allocation for simple single-draw passes. Add v3d_tile_alloc_sizes() to v3d_util with the full tile alloc BO and TSDA sizing logic. This uses the 128B initial blocks and tile_alloc becomes proportional to the number of draws and size of the initial blocks allocation with the cap of the previous fixed allocation. So jobs with 0 or 1 drawcalls (blits/fills) reduce their headroom dramatically. The draw-proportional formula replaces a flat 512 KB continuation pool: headroom = MIN2((tiles_size * draw_count) / 2, 512 KB) Benchmarked on RPi5 (V3D 7.1) against GfxBench GL tests and apitrace replays at 1080p. Tile-alloc memory reduction versus the flat 512 KB headroom (taking into account 256kb kernel alloc per OOM): GfxBench (5 benchmarks): -45% to -70% reduction, OOM at or below baseline Apitrace (19 traces): -4% to -77% reduction on 20/24 traces No FPS regressions observed on any workload. Reviewed-by: Maíra Canal <mcanal@igalia.com> Reviewed-by: Iago Toral Quiroga <itoral@igalia.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40554>
2026-03-19 10:37:40 +01:00
void
v3d_tile_alloc_sizes(uint32_t layers,
uint32_t tiles_x,
uint32_t tiles_y,
uint32_t draws,
uint32_t page_size,
uint32_t *tile_alloc_size,
uint32_t *tile_state_size)
{
assert(layers > 0);
/* The PTB will request the tile alloc initial size per tile at start
* of tile binning. The size must match the initial block size
* configured in the TILE_BINNING_MODE_CFG packet.
*/
uint32_t tiles_size =
layers * tiles_x * tiles_y * V3D_TILE_ALLOC_INITIAL_BLOCK_SIZE;
/* The PTB allocates in aligned 4k chunks after the initial setup. */
uint32_t alloc_size = align(tiles_size, 4096);
/* Include the first two chunk allocations that the PTB does so that
* we definitely clear the OOM condition before triggering one (the HW
* won't trigger OOM during the first allocations).
*/
alloc_size += 8192;
/* Pre-allocate a continuation pool so the GPU rarely has to stall
* waiting for the kernel OOM handler. Each draw call writes per-tile
* BCL state (primitives, uniforms, shader records) whose size scales
* with both the number of tiles and the number of draws. Use the
* product (tiles_size * draws) / 2 as an estimate, capped at 512 KB
* to avoid over-allocating on high draw-count scenes. Align the
* total to page_size.
* The formula assumes the initial block size of 128B, so if it is
* changed it needs to be adjusted.
*/
STATIC_ASSERT(V3D_TILE_ALLOC_INITIAL_BLOCK_SIZE == 128);
alloc_size += MIN2((tiles_size * draws) / 2, 512 * 1024);
alloc_size = align(alloc_size, page_size);
*tile_alloc_size = alloc_size;
*tile_state_size = layers * tiles_x * tiles_y * 256;
}
uint32_t
v3d_compute_rt_row_row_stride_128_bits(uint32_t tile_width,
uint32_t bpp)
{
/* stride in multiples of 128 bits, and covers 2 rows. This is the
* reason we divide by 2 instead of 4, as we divide number of 32-bit
* words per row by 2.
*/
return (tile_width * bpp) / 2;
}