From 16ee2f22ebcee95e0263bc00a1b785f425f8e5b3 Mon Sep 17 00:00:00 2001 From: Joshua Staub <18756667+Carbonyte@users.noreply.github.com> Date: Sun, 30 Jun 2024 16:57:53 -0400 Subject: [PATCH] Check device texture size limits in RenderingDevice::texture_create --- servers/rendering/rendering_device.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/servers/rendering/rendering_device.cpp b/servers/rendering/rendering_device.cpp index 0761af92609..426ab122982 100644 --- a/servers/rendering/rendering_device.cpp +++ b/servers/rendering/rendering_device.cpp @@ -747,9 +747,10 @@ RID RenderingDevice::texture_create(const TextureFormat &p_format, const Texture if (format.texture_type == TEXTURE_TYPE_1D_ARRAY || format.texture_type == TEXTURE_TYPE_2D_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) { ERR_FAIL_COND_V_MSG(format.array_layers < 1, RID(), - "Amount of layers must be equal or greater than 1 for arrays and cubemaps."); + "Number of layers must be equal or greater than 1 for arrays and cubemaps."); ERR_FAIL_COND_V_MSG((format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) && (format.array_layers % 6) != 0, RID(), "Cubemap and cubemap array textures must provide a layer number that is multiple of 6"); + ERR_FAIL_COND_V_MSG(format.array_layers > driver->limit_get(LIMIT_MAX_TEXTURE_ARRAY_LAYERS), RID(), "Number of layers exceeds device maximum."); } else { format.array_layers = 1; } @@ -761,6 +762,28 @@ RID RenderingDevice::texture_create(const TextureFormat &p_format, const Texture format.height = format.texture_type != TEXTURE_TYPE_1D && format.texture_type != TEXTURE_TYPE_1D_ARRAY ? format.height : 1; format.depth = format.texture_type == TEXTURE_TYPE_3D ? format.depth : 1; + uint64_t size_max = 0; + switch (format.texture_type) { + case TEXTURE_TYPE_1D: + case TEXTURE_TYPE_1D_ARRAY: + size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_1D); + break; + case TEXTURE_TYPE_2D: + case TEXTURE_TYPE_2D_ARRAY: + size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_2D); + break; + case TEXTURE_TYPE_CUBE: + case TEXTURE_TYPE_CUBE_ARRAY: + size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_CUBE); + break; + case TEXTURE_TYPE_3D: + size_max = driver->limit_get(LIMIT_MAX_TEXTURE_SIZE_3D); + break; + case TEXTURE_TYPE_MAX: + break; + } + ERR_FAIL_COND_V_MSG(format.width > size_max || format.height > size_max || format.depth > size_max, RID(), "Texture dimensions exceed device maximum."); + uint32_t required_mipmaps = get_image_required_mipmaps(format.width, format.height, format.depth); ERR_FAIL_COND_V_MSG(required_mipmaps < format.mipmaps, RID(),