2021-04-29 09:23:28 +02:00
|
|
|
/*
|
2022-02-17 12:38:42 +01:00
|
|
|
* Copyright © 2021 Raspberry Pi Ltd
|
2021-04-29 09:23:28 +02:00
|
|
|
*
|
|
|
|
|
* 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"
|
2026-03-19 10:37:40 +01:00
|
|
|
#include "v3d_limits.h"
|
2021-04-29 09:23:28 +02:00
|
|
|
#include "util/macros.h"
|
2026-03-19 10:37:40 +01:00
|
|
|
#include "util/u_math.h"
|
2021-04-29 09:23:28 +02:00
|
|
|
|
|
|
|
|
/* 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,
|
2025-09-24 22:38:30 +05:30
|
|
|
bool can_use_supergroups,
|
2021-04-29 09:23:28 +02:00
|
|
|
bool has_tsy_barrier,
|
|
|
|
|
uint32_t threads,
|
|
|
|
|
uint32_t num_wgs,
|
|
|
|
|
uint32_t wg_size)
|
|
|
|
|
{
|
2025-09-24 22:38:30 +05:30
|
|
|
/* FIXME: Some subgroups may restrict supergroup packing. For now,
|
|
|
|
|
* if the shader has subgroups, we only allow the ones that support
|
|
|
|
|
* supergroup packing.
|
2021-06-22 12:34:02 +02:00
|
|
|
*/
|
2025-09-24 22:38:30 +05:30
|
|
|
if (!can_use_supergroups)
|
2021-06-22 12:34:02 +02:00
|
|
|
return 1;
|
|
|
|
|
|
2025-09-05 19:35:36 +05:30
|
|
|
/* 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;
|
|
|
|
|
|
2021-04-29 09:23:28 +02:00
|
|
|
/* 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.
|
|
|
|
|
*/
|
2025-09-05 19:35:36 +05:30
|
|
|
uint32_t max_wgs_per_sg = 16;
|
|
|
|
|
|
2021-04-29 09:23:28 +02:00
|
|
|
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);
|
2025-09-05 19:35:36 +05:30
|
|
|
max_wgs_per_sg = max_batches_per_sg * 16 / wg_size;
|
2021-04-29 09:23:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-11-15 11:09:38 +01:00
|
|
|
|
2021-11-16 11:26:17 +01:00
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 11:09:38 +01:00
|
|
|
void
|
2021-11-12 10:35:59 +01:00
|
|
|
v3d_choose_tile_size(const struct v3d_device_info *devinfo,
|
|
|
|
|
uint32_t color_attachment_count,
|
2021-11-16 11:26:17 +01:00
|
|
|
/* 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,
|
2021-11-12 10:35:59 +01:00
|
|
|
bool double_buffer,
|
2021-11-16 11:26:17 +01:00
|
|
|
uint32_t *width,
|
|
|
|
|
uint32_t *height)
|
2021-11-15 11:09:38 +01:00
|
|
|
{
|
|
|
|
|
static const uint8_t tile_sizes[] = {
|
|
|
|
|
64, 64,
|
|
|
|
|
64, 32,
|
|
|
|
|
32, 32,
|
|
|
|
|
32, 16,
|
|
|
|
|
16, 16,
|
|
|
|
|
16, 8,
|
|
|
|
|
8, 8
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
uint32_t idx = 0;
|
2021-11-12 10:35:59 +01:00
|
|
|
if (devinfo->ver >= 71) {
|
2021-11-16 11:26:17 +01:00
|
|
|
/* 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.
|
2021-11-12 10:35:59 +01:00
|
|
|
*
|
2021-11-16 11:26:17 +01:00
|
|
|
* 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.
|
2021-11-12 10:35:59 +01:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2021-11-16 11:26:17 +01:00
|
|
|
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);
|
|
|
|
|
|
2024-10-28 12:22:34 +01:00
|
|
|
if (double_buffer)
|
|
|
|
|
idx += 1;
|
2021-11-16 11:26:17 +01:00
|
|
|
} 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;
|
2021-11-12 10:35:59 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 11:09:38 +01:00
|
|
|
assert(idx < ARRAY_SIZE(tile_sizes) / 2);
|
|
|
|
|
|
|
|
|
|
*width = tile_sizes[idx * 2];
|
|
|
|
|
*height = tile_sizes[idx * 2 + 1];
|
|
|
|
|
}
|
2022-07-13 09:24:18 +02:00
|
|
|
|
|
|
|
|
/* 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:
|
2025-07-23 09:17:35 +02:00
|
|
|
UNREACHABLE("unknown swizzle");
|
2022-07-13 09:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-18 10:21:46 +02:00
|
|
|
|
|
|
|
|
/* Translates a pipe primitive type to a hw value we can use in the various
|
|
|
|
|
* draw packets.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t
|
2023-06-01 23:03:34 +08:00
|
|
|
v3d_hw_prim_type(enum mesa_prim prim_type)
|
2022-07-18 10:21:46 +02:00
|
|
|
{
|
|
|
|
|
switch (prim_type) {
|
2023-06-01 23:03:34 +08:00
|
|
|
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:
|
2022-07-18 10:21:46 +02:00
|
|
|
return prim_type;
|
|
|
|
|
|
2023-06-01 23:03:34 +08:00
|
|
|
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);
|
2022-07-18 10:21:46 +02:00
|
|
|
|
|
|
|
|
default:
|
2025-07-23 09:17:35 +02:00
|
|
|
UNREACHABLE("Unsupported primitive type");
|
2022-07-18 10:21:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-17 14:40:47 +01:00
|
|
|
|
|
|
|
|
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:
|
2025-07-23 09:17:35 +02:00
|
|
|
UNREACHABLE("Unsupported internal BPP");
|
2021-11-17 14:40:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 14:40:47 +01:00
|
|
|
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;
|
|
|
|
|
}
|