From a9c8de19f6db7948adb50ee69694b8abe04ca952 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Sun, 23 Mar 2025 22:27:59 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20GPU=20compressed=20texture=20load?= =?UTF-8?q?=20logic=20=E4=BC=98=E5=8C=96=E5=8E=8B=E7=BC=A9=E7=BA=B9?= =?UTF-8?q?=E7=90=86=E5=8A=A0=E8=BD=BD=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RenderEngine/RenderEnum/TextureFormat.ts | 41 ++++++++++++++++++- src/layaAir/laya/loaders/TextureLoader.ts | 28 ++++++++++--- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/layaAir/laya/RenderEngine/RenderEnum/TextureFormat.ts b/src/layaAir/laya/RenderEngine/RenderEnum/TextureFormat.ts index c2b9962f63..6b9d45d05c 100644 --- a/src/layaAir/laya/RenderEngine/RenderEnum/TextureFormat.ts +++ b/src/layaAir/laya/RenderEngine/RenderEnum/TextureFormat.ts @@ -1,3 +1,5 @@ +import { RenderCapable } from './RenderCapable'; + /** * 纹理格式 */ @@ -72,4 +74,41 @@ export enum TextureFormat { KTXTEXTURE = -1, /**pvr图片 */ PVRTEXTURE = -2 -} \ No newline at end of file +} + +/** 通过纹理格式获取压缩纹理类型 */ +export function getCompressTextureRenderCapable(format: TextureFormat): RenderCapable | null { + switch (format) { + case TextureFormat.ASTC4x4: + case TextureFormat.ASTC4x4SRGB: + case TextureFormat.ASTC6x6: + case TextureFormat.ASTC6x6SRGB: + case TextureFormat.ASTC8x8: + case TextureFormat.ASTC8x8SRGB: + case TextureFormat.ASTC10x10: + case TextureFormat.ASTC10x10SRGB: + case TextureFormat.ASTC12x12: + case TextureFormat.ASTC12x12SRGB: + return RenderCapable.COMPRESS_TEXTURE_ASTC; + case TextureFormat.DXT1: + case TextureFormat.DXT3: + case TextureFormat.DXT5: + return RenderCapable.COMPRESS_TEXTURE_S3TC; + case TextureFormat.PVRTCRGB_2BPPV: + case TextureFormat.PVRTCRGBA_2BPPV: + case TextureFormat.PVRTCRGB_4BPPV: + case TextureFormat.PVRTCRGBA_4BPPV: + return RenderCapable.COMPRESS_TEXTURE_PVRTC; + case TextureFormat.ETC2RGB: + case TextureFormat.ETC2RGBA: + case TextureFormat.ETC2SRGB_Alpha8: + case TextureFormat.ETC2SRGB: + case TextureFormat.ETC2RGB_Alpha1: + case TextureFormat.ETC2SRGB_Alpha1: + return RenderCapable.COMPRESS_TEXTURE_ETC; + case TextureFormat.ETC1RGB: + return RenderCapable.COMPRESS_TEXTURE_ETC1; + default: + return null; + } +} diff --git a/src/layaAir/laya/loaders/TextureLoader.ts b/src/layaAir/laya/loaders/TextureLoader.ts index a26a869bc9..c092e8dab9 100644 --- a/src/layaAir/laya/loaders/TextureLoader.ts +++ b/src/layaAir/laya/loaders/TextureLoader.ts @@ -6,7 +6,7 @@ import { KTXTextureInfo } from "../RenderEngine/KTXTextureInfo"; import { TextureDimension } from "../RenderEngine/RenderEnum/TextureDimension"; import { ClassUtils } from "../utils/ClassUtils"; import { BaseTexture } from "../resource/BaseTexture"; -import { TextureFormat } from "../RenderEngine/RenderEnum/TextureFormat"; +import { getCompressTextureRenderCapable, TextureFormat } from "../RenderEngine/RenderEnum/TextureFormat"; import { Browser } from "../utils/Browser"; import { AssetDb } from "../resource/AssetDb"; import { Resource } from "../resource/Resource"; @@ -57,15 +57,31 @@ export class Texture2DLoader implements IResourceLoader { let ext = task.ext; let url = task.url; if (meta) { - let platform = Browser.platform; - let fileIndex = meta.platforms?.[platform] || 0; - let fileInfo = meta.files?.[fileIndex] || {}; + const RGBA = { format: TextureFormat.R8G8B8A8, file: null as string, ext: null as string }; + let fileInfo = RGBA; + + if (meta.platforms && meta.files) { + if (Browser.platform in meta.platforms) { + const fileIndex = meta.platforms[Browser.platform]; + fileInfo = meta.files[fileIndex]; + } + let capable = getCompressTextureRenderCapable(fileInfo.format); + if (capable && !LayaGL.renderEngine.getCapable(capable)) { // 当前环境是不支持 meta 中设置的压缩纹理格式 + const fallback = (meta.files as (typeof fileInfo)[]).find(f => { + // 找到第一个支持的压缩纹理格式 + const c = getCompressTextureRenderCapable(f.format); + return LayaGL.renderEngine.getCapable(c); + }); + fileInfo = fallback || RGBA; + } + } + if (fileInfo.file) { url = AssetDb.inst.getSubAssetURL(url, task.uuid, fileInfo.file, fileInfo.ext); ext = fileInfo.ext; } - constructParams = [0, 0, fileInfo.format ?? 1, meta.mipmap, meta.readWrite, meta.sRGB]; + constructParams = [0, 0, fileInfo.format, meta.mipmap, meta.readWrite, meta.sRGB]; propertyParams = { wrapModeU: meta.wrapMode, wrapModeV: meta.wrapMode, @@ -293,4 +309,4 @@ const videoFormats = ["mp4", "webm"]; Loader.registerLoader(["tga", "tif", "tiff", "png", "jpg", "jpeg", "webp", "rendertexture", ...videoFormats, ...compressedFormats], TextureLoader, Loader.IMAGE, true); Loader.registerLoader([], Texture2DLoader, Loader.TEXTURE2D, true); Loader.registerLoader(["rendertexture"], RenderTextureLoader, Loader.TEXTURE2D, true); -Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D); \ No newline at end of file +Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);