mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
fix(render-texture): use GPU max texture size instead of 2048. (#385)
* Changed the GeometryRenderer and DebugRenderer from using intrusive calls to registering callbacks (custom pipeline). * Changed the GeometryRenderer and DebugRenderer from using intrusive calls to registering callbacks. (old pipeline) * CI error: Function parameter names cannot be omitted. * Move the devil string to macro * fix(mesh): hoose index format by max index value in createMesh * perf(pipeline): reduce render queue sort to two packed key compares (native platform). * fix CI error caused by native/cocos/base/Utils.h * fix(scene): LOD screen usage distance truncated to int (native platform) * fix: prevent accessor recursion in SkinnedMeshBatchRenderer. * fix: destroy index buffer in MeshRenderData.clear() to prevent GPU leak. * fix: use GPU max texture size for render texture resizing.
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<uint32_t> 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<float>(width), 1.F, 2048.F));
|
||||
_height = std::floor(clampf(static_cast<float>(height), 1.F, 2048.F));
|
||||
const auto maxTextureSize = getMaxRenderTextureSize();
|
||||
const auto maxSize = static_cast<float>(
|
||||
maxTextureSize.has_value() ? maxTextureSize.value() : FALLBACK_MAX_RENDER_TEXTURE_SIZE);
|
||||
_width = static_cast<uint32_t>(std::floor(clampf(static_cast<float>(width), 1.F, maxSize)));
|
||||
_height = static_cast<uint32_t>(std::floor(clampf(static_cast<float>(height), 1.F, maxSize)));
|
||||
if (_window != nullptr) {
|
||||
_window->resize(_width, _height);
|
||||
}
|
||||
@@ -146,7 +168,15 @@ void RenderTexture::initDefault(const ccstd::optional<ccstd::string> &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<uint8_t> RenderTexture::readPixels(uint32_t x, uint32_t y, uint32_t width, uint32_t height) const {
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user