From 3a5c606d206860914f771a0199eb228f3af0e05e Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Thu, 16 Jan 2025 13:17:27 +0100 Subject: [PATCH] panfrost: limit maximum texture size We don't want huge textures to take up large amounts of system memory. On a system with 1GB of RAM, this would limit us to 256 MB, which is the same memory usage as a texture of 8192 x 8192 with 4 bytes per component, which is what we currently limit max texture size to anyway. The goal here is really to allow using larger textures on systems with more memory, but that bit comes in a later patch in this series. Acked-by: Boris Brezillon Part-of: --- src/gallium/drivers/panfrost/pan_resource.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/gallium/drivers/panfrost/pan_resource.c b/src/gallium/drivers/panfrost/pan_resource.c index 5cc05862863..483d4fcc46e 100644 --- a/src/gallium/drivers/panfrost/pan_resource.c +++ b/src/gallium/drivers/panfrost/pan_resource.c @@ -706,6 +706,25 @@ panfrost_resource_set_damage_region(struct pipe_screen *screen, } } +static bool +panfrost_can_create_resource(struct pipe_screen *screen, + const struct pipe_resource *template) +{ + struct panfrost_resource tmp; + tmp.base = *template; + + panfrost_resource_setup(screen, &tmp, DRM_FORMAT_MOD_INVALID, template->format); + + uint64_t system_memory; + if (!os_get_total_physical_memory(&system_memory)) + return false; + + /* Limit maximum texture size to a quarter of the system memory, to avoid + * allocating huge textures on systems with little memory. + */ + return tmp.image.layout.data_size <= system_memory / 4; +} + struct pipe_resource * panfrost_resource_create_with_modifier(struct pipe_screen *screen, const struct pipe_resource *template, @@ -1937,6 +1956,7 @@ static const struct u_transfer_vtbl transfer_vtbl = { void panfrost_resource_screen_init(struct pipe_screen *pscreen) { + pscreen->can_create_resource = panfrost_can_create_resource; pscreen->resource_create_with_modifiers = panfrost_resource_create_with_modifiers; pscreen->resource_create = u_transfer_helper_resource_create;