Skip to content

Commit a9c8de1

Browse files
committed
optimize: GPU compressed texture load logic
优化压缩纹理加载流程
1 parent f75d3e4 commit a9c8de1

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/layaAir/laya/RenderEngine/RenderEnum/TextureFormat.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { RenderCapable } from './RenderCapable';
2+
13
/**
24
* 纹理格式
35
*/
@@ -72,4 +74,41 @@ export enum TextureFormat {
7274
KTXTEXTURE = -1,
7375
/**pvr图片 */
7476
PVRTEXTURE = -2
75-
}
77+
}
78+
79+
/** 通过纹理格式获取压缩纹理类型 */
80+
export function getCompressTextureRenderCapable(format: TextureFormat): RenderCapable | null {
81+
switch (format) {
82+
case TextureFormat.ASTC4x4:
83+
case TextureFormat.ASTC4x4SRGB:
84+
case TextureFormat.ASTC6x6:
85+
case TextureFormat.ASTC6x6SRGB:
86+
case TextureFormat.ASTC8x8:
87+
case TextureFormat.ASTC8x8SRGB:
88+
case TextureFormat.ASTC10x10:
89+
case TextureFormat.ASTC10x10SRGB:
90+
case TextureFormat.ASTC12x12:
91+
case TextureFormat.ASTC12x12SRGB:
92+
return RenderCapable.COMPRESS_TEXTURE_ASTC;
93+
case TextureFormat.DXT1:
94+
case TextureFormat.DXT3:
95+
case TextureFormat.DXT5:
96+
return RenderCapable.COMPRESS_TEXTURE_S3TC;
97+
case TextureFormat.PVRTCRGB_2BPPV:
98+
case TextureFormat.PVRTCRGBA_2BPPV:
99+
case TextureFormat.PVRTCRGB_4BPPV:
100+
case TextureFormat.PVRTCRGBA_4BPPV:
101+
return RenderCapable.COMPRESS_TEXTURE_PVRTC;
102+
case TextureFormat.ETC2RGB:
103+
case TextureFormat.ETC2RGBA:
104+
case TextureFormat.ETC2SRGB_Alpha8:
105+
case TextureFormat.ETC2SRGB:
106+
case TextureFormat.ETC2RGB_Alpha1:
107+
case TextureFormat.ETC2SRGB_Alpha1:
108+
return RenderCapable.COMPRESS_TEXTURE_ETC;
109+
case TextureFormat.ETC1RGB:
110+
return RenderCapable.COMPRESS_TEXTURE_ETC1;
111+
default:
112+
return null;
113+
}
114+
}

src/layaAir/laya/loaders/TextureLoader.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { KTXTextureInfo } from "../RenderEngine/KTXTextureInfo";
66
import { TextureDimension } from "../RenderEngine/RenderEnum/TextureDimension";
77
import { ClassUtils } from "../utils/ClassUtils";
88
import { BaseTexture } from "../resource/BaseTexture";
9-
import { TextureFormat } from "../RenderEngine/RenderEnum/TextureFormat";
9+
import { getCompressTextureRenderCapable, TextureFormat } from "../RenderEngine/RenderEnum/TextureFormat";
1010
import { Browser } from "../utils/Browser";
1111
import { AssetDb } from "../resource/AssetDb";
1212
import { Resource } from "../resource/Resource";
@@ -57,15 +57,31 @@ export class Texture2DLoader implements IResourceLoader {
5757
let ext = task.ext;
5858
let url = task.url;
5959
if (meta) {
60-
let platform = Browser.platform;
61-
let fileIndex = meta.platforms?.[platform] || 0;
62-
let fileInfo = meta.files?.[fileIndex] || {};
60+
const RGBA = { format: TextureFormat.R8G8B8A8, file: null as string, ext: null as string };
61+
let fileInfo = RGBA;
62+
63+
if (meta.platforms && meta.files) {
64+
if (Browser.platform in meta.platforms) {
65+
const fileIndex = meta.platforms[Browser.platform];
66+
fileInfo = meta.files[fileIndex];
67+
}
68+
let capable = getCompressTextureRenderCapable(fileInfo.format);
69+
if (capable && !LayaGL.renderEngine.getCapable(capable)) { // 当前环境是不支持 meta 中设置的压缩纹理格式
70+
const fallback = (meta.files as (typeof fileInfo)[]).find(f => {
71+
// 找到第一个支持的压缩纹理格式
72+
const c = getCompressTextureRenderCapable(f.format);
73+
return LayaGL.renderEngine.getCapable(c);
74+
});
75+
fileInfo = fallback || RGBA;
76+
}
77+
}
78+
6379
if (fileInfo.file) {
6480
url = AssetDb.inst.getSubAssetURL(url, task.uuid, fileInfo.file, fileInfo.ext);
6581
ext = fileInfo.ext;
6682
}
6783

68-
constructParams = [0, 0, fileInfo.format ?? 1, meta.mipmap, meta.readWrite, meta.sRGB];
84+
constructParams = [0, 0, fileInfo.format, meta.mipmap, meta.readWrite, meta.sRGB];
6985
propertyParams = {
7086
wrapModeU: meta.wrapMode,
7187
wrapModeV: meta.wrapMode,
@@ -293,4 +309,4 @@ const videoFormats = ["mp4", "webm"];
293309
Loader.registerLoader(["tga", "tif", "tiff", "png", "jpg", "jpeg", "webp", "rendertexture", ...videoFormats, ...compressedFormats], TextureLoader, Loader.IMAGE, true);
294310
Loader.registerLoader([], Texture2DLoader, Loader.TEXTURE2D, true);
295311
Loader.registerLoader(["rendertexture"], RenderTextureLoader, Loader.TEXTURE2D, true);
296-
Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);
312+
Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);

0 commit comments

Comments
 (0)