diff --git a/cocos/asset/assets/render-texture.ts b/cocos/asset/assets/render-texture.ts index dffdf5a1a6..ee8767b1b4 100644 --- a/cocos/asset/assets/render-texture.ts +++ b/cocos/asset/assets/render-texture.ts @@ -53,6 +53,19 @@ const _windowInfo: IRenderWindowInfo = { renderPassInfo: passInfo, }; +// The legacy limit used only when the GPU capability is unavailable, +// e.g. in editor or during asset deserialization. +const FALLBACK_MAX_RENDER_TEXTURE_SIZE = 2048; + +/** + * @en Gets the maximum render texture size supported by the current GPU, or null if it is unknown. + * @zh 获取当前 GPU 支持的最大渲染贴图尺寸,未知时返回 null。 + */ +function getMaxRenderTextureSize (): number | null { + const maxTextureSize = deviceManager.gfxDevice?.capabilities.maxTextureSize; + return maxTextureSize && maxTextureSize > 0 ? maxTextureSize : null; +} + /** * @en Render texture is a render target for [[Camera]] or [[Canvas]] component, * the render pipeline will use its `RenderWindow` as the target of the rendering process. @@ -111,12 +124,17 @@ export class RenderTexture extends TextureBase { /** * @en Resize the render texture. * @zh 修改渲染贴图的尺寸。 - * @param width @en The pixel width to resize to, the range is from 1 to 2048. @zh 需要调整到的像素宽度,范围为 1-2048。 - * @param height @en The pixel height to resize to, the range is from 1 to 2048. @zh 需要调整到的像素高度,范围为 1-2048。 + * @param width @en The pixel width to resize to, the range is from 1 to the GPU max texture size. + * @zh 需要调整到的像素宽度,范围为 1 到当前 GPU 支持的最大贴图尺寸。 + * @param height @en The pixel height to resize to, the range is from 1 to the GPU max texture size. + * @zh 需要调整到的像素高度,范围为 1 到当前 GPU 支持的最大贴图尺寸。 + * Note: if the GPU limit is unavailable, the legacy 2048 limit is used as a fallback. + * @zh 注意:如果无法获取 GPU 上限,则回退到旧的 2048 限制。 */ public resize (width: number, height: number): void { - this._width = Math.floor(clamp(width, 1, 2048)); - this._height = Math.floor(clamp(height, 1, 2048)); + const maxSize = getMaxRenderTextureSize() ?? FALLBACK_MAX_RENDER_TEXTURE_SIZE; + this._width = Math.floor(clamp(width, 1, maxSize)); + this._height = Math.floor(clamp(height, 1, maxSize)); if (this._window) { this._window.resize(this._width, this._height); } @@ -214,7 +232,10 @@ export class RenderTexture extends TextureBase { * @deprecated Since v3.7, this is an internal engine interface and you should not call this interface under any circumstances. */ public validate (): boolean { - return this.width >= 1 && this.width <= 2048 && this.height >= 1 && this.height <= 2048; + const maxSize = getMaxRenderTextureSize(); + const validLowerBound = this.width >= 1 && this.height >= 1; + // If the GPU limit is unknown, only validate the lower bound instead of rejecting a possibly valid asset. + return validLowerBound && (maxSize === null || (this.width <= maxSize && this.height <= maxSize)); } /** diff --git a/native/cocos/core/assets/RenderTexture.cpp b/native/cocos/core/assets/RenderTexture.cpp index 564e94cb92..8209dca5c8 100644 --- a/native/cocos/core/assets/RenderTexture.cpp +++ b/native/cocos/core/assets/RenderTexture.cpp @@ -23,15 +23,34 @@ ****************************************************************************/ #include "core/assets/RenderTexture.h" +#include "base/std/optional.h" #include "core/Root.h" #include "core/platform/Debug.h" #include "core/utils/IDGenerator.h" #include "renderer/gfx-base/GFXDef-common.h" +#include "renderer/gfx-base/GFXDevice.h" #include "scene/RenderWindow.h" namespace cc { namespace { +// The legacy limit used only when the GPU capability is unavailable, +// e.g. when assets are loaded before the renderer is initialized. +constexpr uint32_t FALLBACK_MAX_RENDER_TEXTURE_SIZE = 2048; + +// Returns ccstd::nullopt when the device or its max texture size is unavailable. +ccstd::optional getMaxRenderTextureSize() { + auto *device = gfx::Device::getInstance(); + if (device == nullptr) { + return ccstd::nullopt; + } + const uint32_t maxTextureSize = device->getCapabilities().maxTextureSize; + if (maxTextureSize == 0) { + return ccstd::nullopt; + } + return maxTextureSize; +} + gfx::RenderPassInfo getDefaultRenderPassInfo(gfx::Device *device) { gfx::RenderPassInfo info; info.colorAttachments.push_back({ @@ -72,8 +91,11 @@ bool RenderTexture::destroy() { } void RenderTexture::resize(uint32_t width, uint32_t height) { - _width = std::floor(clampf(static_cast(width), 1.F, 2048.F)); - _height = std::floor(clampf(static_cast(height), 1.F, 2048.F)); + const auto maxTextureSize = getMaxRenderTextureSize(); + const auto maxSize = static_cast( + maxTextureSize.has_value() ? maxTextureSize.value() : FALLBACK_MAX_RENDER_TEXTURE_SIZE); + _width = static_cast(std::floor(clampf(static_cast(width), 1.F, maxSize))); + _height = static_cast(std::floor(clampf(static_cast(height), 1.F, maxSize))); if (_window != nullptr) { _window->resize(_width, _height); } @@ -146,7 +168,15 @@ void RenderTexture::initDefault(const ccstd::optional &uuid) { } bool RenderTexture::validate() const { - return _width >= 1 && _width <= 2048 && _height >= 1 && _height <= 2048; + if (_width == 0 || _height == 0) { + return false; + } + const auto maxTextureSize = getMaxRenderTextureSize(); + if (!maxTextureSize.has_value()) { + return true; + } + const uint32_t maxSize = maxTextureSize.value(); + return _width <= maxSize && _height <= maxSize; } ccstd::vector RenderTexture::readPixels(uint32_t x, uint32_t y, uint32_t width, uint32_t height) const { diff --git a/native/cocos/core/assets/RenderTexture.h b/native/cocos/core/assets/RenderTexture.h index 8aa19e7dfa..04db563e10 100644 --- a/native/cocos/core/assets/RenderTexture.h +++ b/native/cocos/core/assets/RenderTexture.h @@ -73,8 +73,9 @@ public: /** * @en Resize the render texture * @zh 修改渲染贴图的尺寸 - * @param width The pixel width, the range is from 1 to 2048 - * @param height The pixel height, the range is from 1 to 2048 + * The upper bound is the GPU max texture size. If the limit is unavailable, 2048 is used as a fallback. + * @param width The pixel width, the range is from 1 to the maximum texture size supported by the GPU + * @param height The pixel height, the range is from 1 to the maximum texture size supported by the GPU */ void resize(uint32_t width, uint32_t height); diff --git a/tests/assets/render-texture.test.ts b/tests/assets/render-texture.test.ts index a7d58ba6d0..210f1f1304 100644 --- a/tests/assets/render-texture.test.ts +++ b/tests/assets/render-texture.test.ts @@ -1,6 +1,7 @@ import { TextureFilter, WrapMode } from '../../cocos/asset/assets/asset-enum'; import { RenderTexture } from '../../cocos/asset/assets/render-texture'; import { Address, SamplerInfo, Filter as GFXFilter } from '../../cocos/gfx/base/define'; +import { deviceManager } from '../../cocos/gfx/device-manager'; describe('render-texture', () => { test('sampler', () => { @@ -22,6 +23,49 @@ describe('render-texture', () => { expect(rt.getGFXTexture().height).toBe(2048); }); + test('resize follows the device max texture size', () => { + const capabilities = deviceManager.gfxDevice.capabilities; + const originalMaxTextureSize = capabilities.maxTextureSize; + capabilities.maxTextureSize = 4096; + try { + const rt = new RenderTexture(); + rt.initialize({ width: 1, height: 1 }); + + rt.resize(4096, 4096); + expect(rt.width).toBe(4096); + expect(rt.height).toBe(4096); + expect(rt.getGFXTexture()!.width).toBe(4096); + + rt.resize(4097, 16384); + expect(rt.width).toBe(4096); + expect(rt.height).toBe(4096); + expect(rt.validate()).toBe(true); + } finally { + capabilities.maxTextureSize = originalMaxTextureSize; + } + }); + + test('validate only checks the upper bound when the device limit is known', () => { + const capabilities = deviceManager.gfxDevice.capabilities; + const originalMaxTextureSize = capabilities.maxTextureSize; + try { + capabilities.maxTextureSize = 0; + const unknownLimit = new RenderTexture(); + unknownLimit.initialize({ width: 4096, height: 4096 }); + expect(unknownLimit.validate()).toBe(true); + + capabilities.maxTextureSize = 4096; + const knownLimit = new RenderTexture(); + knownLimit.initialize({ width: 4096, height: 4096 }); + expect(knownLimit.validate()).toBe(true); + + knownLimit.initialize({ width: 4097, height: 4096 }); + expect(knownLimit.validate()).toBe(false); + } finally { + capabilities.maxTextureSize = originalMaxTextureSize; + } + }); + test('serialize', () => { const rt = new RenderTexture(); const rtData1 = rt._serialize(null);