Skip to content

Commit c7aff34

Browse files
committed
Compressed texture format is choosed by render context
1 parent ab55f97 commit c7aff34

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/layaAir/laya/RenderDriver/DriverDesign/RenderDevice/ITextureContext.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import { TextureFormat } from "../../../RenderEngine/RenderEnum/TextureFormat";
88
import { InternalRenderTarget } from "./InternalRenderTarget";
99
import { InternalTexture } from "./InternalTexture";
1010

11+
/** 压缩纹理格式 */
12+
export enum CompressedTextureFormat {
13+
S3TC = 1,
14+
ASTC = 1 << 1,
15+
ETC = 1 << 2,
16+
PVRTC = 1 << 3,
17+
};
1118

1219
export interface ITextureContext {
1320
needBitmap: boolean;
@@ -82,4 +89,10 @@ export interface ITextureContext {
8289

8390
setTexture3DSubPixelsData(texture: InternalTexture, source: ArrayBufferView, mipmapLevel: number, generateMipmap: boolean, xOffset: number, yOffset: number, zOffset: number, width: number, height: number, depth: number, premultiplyAlpha: boolean, invertY: boolean): void;
8491

85-
}
92+
/**
93+
* 当前渲染上下文支持的压缩纹理格式
94+
* @see CompressedTextureFormat
95+
* @returns CompressedTextureFormat 枚举值的组合
96+
*/
97+
readonly supportedCompressedTextureFormats?: number;
98+
}

src/layaAir/laya/RenderDriver/WebGLDriver/RenderDevice/GLTextureContext.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { RenderTargetFormat } from "../../../RenderEngine/RenderEnum/RenderTarge
99
import { TextureCompareMode } from "../../../RenderEngine/RenderEnum/TextureCompareMode";
1010
import { TextureDimension } from "../../../RenderEngine/RenderEnum/TextureDimension";
1111
import { TextureFormat } from "../../../RenderEngine/RenderEnum/TextureFormat";
12-
import { ITextureContext } from "../../DriverDesign/RenderDevice/ITextureContext";
12+
import { CompressedTextureFormat, ITextureContext } from "../../DriverDesign/RenderDevice/ITextureContext";
1313
import { WebGLEngine } from "./WebGLEngine";
1414
import { WebGLExtension } from "./WebGLEngine/GLEnum/WebGLExtension";
1515
import { GLObject } from "./WebGLEngine/GLObject";
@@ -25,8 +25,12 @@ export class GLTextureContext extends GLObject implements ITextureContext {
2525
protected _compressedTextureS3tc: any;
2626
protected _compressedTextureETC: any;
2727
protected _compressedTextureASTC: any;
28+
protected _compressedTexturePVRTC: any;
2829
protected _webgl_depth_texture: any;
2930
needBitmap: boolean;
31+
32+
readonly supportedCompressedTextureFormats: number;
33+
3034
constructor(engine: WebGLEngine) {
3135
super(engine);
3236
this.needBitmap = false;
@@ -36,9 +40,18 @@ export class GLTextureContext extends GLObject implements ITextureContext {
3640
this._compressedTextureEtc1 = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_compressed_texture_etc1)
3741
this._compressedTextureS3tc = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_compressed_texture_s3tc)
3842
this._compressedTextureETC = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_compressed_texture_etc)
39-
this._compressedTextureASTC = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_compressed_texture_astc)
43+
this._compressedTextureASTC = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_compressed_texture_astc);
44+
this._compressedTexturePVRTC = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_compressed_texture_pvrtc)
4045
this._webgl_depth_texture = this._engine._supportCapatable.getExtension(WebGLExtension.WEBGL_depth_texture);
46+
47+
let formats = 0;
48+
this._compressedTextureASTC && (formats |= CompressedTextureFormat.ASTC);
49+
this._compressedTextureS3tc && (formats |= CompressedTextureFormat.S3TC);
50+
this._compressedTextureETC && (formats |= CompressedTextureFormat.ETC);
51+
this._compressedTexturePVRTC && (formats |= CompressedTextureFormat.PVRTC);
52+
this.supportedCompressedTextureFormats = formats;
4153
}
54+
4255
createTexture3DInternal(dimension: TextureDimension, width: number, height: number, depth: number, format: TextureFormat, generateMipmap: boolean, sRGB: boolean, premultipliedAlpha: boolean): InternalTexture {
4356
return null;
4457
}
@@ -1580,4 +1593,4 @@ export class GLTextureContext extends GLObject implements ITextureContext {
15801593
// return pixels;
15811594
// }
15821595

1583-
}
1596+
}

src/layaAir/laya/loaders/TextureLoader.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ import { VideoTexture } from "../media/VideoTexture";
1616
import { LayaEnv } from "../../LayaEnv";
1717
import { LayaGL } from "../layagl/LayaGL";
1818
import { DDSTextureInfo } from "../RenderEngine/DDSTextureInfo";
19+
import { CompressedTextureFormat } from '../RenderDriver/DriverDesign/RenderDevice/ITextureContext';
1920

2021
var internalResources: Record<string, Texture2D>;
2122

2223
export class Texture2DLoader implements IResourceLoader {
24+
25+
protected static readonly CompressedTextureFormatIndex = {
26+
[CompressedTextureFormat.ASTC]: 1,
27+
[CompressedTextureFormat.S3TC]: 0,
28+
} as Record<CompressedTextureFormat, number>;
29+
2330
constructor() {
2431
if (!internalResources) {
2532
internalResources = {
@@ -57,8 +64,13 @@ export class Texture2DLoader implements IResourceLoader {
5764
let ext = task.ext;
5865
let url = task.url;
5966
if (meta) {
60-
let platform = Browser.platform;
61-
let fileIndex = meta.platforms?.[platform] || 0;
67+
let flags = LayaGL.textureContext.supportedCompressedTextureFormats || 0;
68+
if (!flags) {
69+
Loader.warnFailed("Compressed texture is not supported.");
70+
return null;
71+
}
72+
const format = [CompressedTextureFormat.ASTC, CompressedTextureFormat.S3TC].find(format => flags & format);
73+
const fileIndex = Texture2DLoader.CompressedTextureFormatIndex[format];
6274
let fileInfo = meta.files?.[fileIndex] || {};
6375
if (fileInfo.file) {
6476
url = AssetDb.inst.getSubAssetURL(url, task.uuid, fileInfo.file, fileInfo.ext);
@@ -293,4 +305,4 @@ const videoFormats = ["mp4", "webm"];
293305
Loader.registerLoader(["tga", "tif", "tiff", "png", "jpg", "jpeg", "webp", "rendertexture", ...videoFormats, ...compressedFormats], TextureLoader, Loader.IMAGE, true);
294306
Loader.registerLoader([], Texture2DLoader, Loader.TEXTURE2D, true);
295307
Loader.registerLoader(["rendertexture"], RenderTextureLoader, Loader.TEXTURE2D, true);
296-
Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);
308+
Loader.registerLoader(videoFormats, VideoTextureLoader, Loader.TEXTURE2D);

0 commit comments

Comments
 (0)