feat: support bc7 (#1649)

This commit is contained in:
ChenMo
2023-07-18 11:57:45 +08:00
committed by GitHub
parent 5ff9f8e7cf
commit a84eb5c3fd
8 changed files with 69 additions and 36 deletions

View File

@@ -120,6 +120,8 @@ export enum GLCapabilityType {
// atc = "WEBGL_compressed_texture_atc",
// s3tc_srgb = "WEBGL_compressed_texture_s3tc_srgb"
bptc = "EXT_texture_compression_bptc",
WEBGL_lose_context = "WEBGL_lose_context"
}

View File

@@ -21,10 +21,12 @@ export enum TextureFormat {
/** RGBA format, 32 bits per channel. */
R32G32B32A32,
/** RGB compressed format. */
DXT1,
/** RGBA compressed format. */
DXT5,
/** RGB compressed format, 4 bits per pixel. */
BC1,
/** RGBA compressed format, 8 bits per pixel. */
BC3,
/** RGB(A) compressed format, 128 bits per 4x4 pixel block. */
BC7,
/** RGB compressed format, 4 bits per pixel. */
ETC1_RGB,
/** RGB compressed format, 4 bits per pixel. */
@@ -67,5 +69,10 @@ export enum TextureFormat {
/** 16-bit depth + 8-bit stencil format. */
Depth24Stencil8,
/** 32-bit depth + 8-bit stencil format. */
Depth32Stencil8
Depth32Stencil8,
/** @deprecated Use `TextureFormat.BC1` instead. */
DXT1,
/** @deprecated Use `TextureFormat.BC3` instead. */
DXT5
}

View File

@@ -79,9 +79,11 @@ function getEngineFormat(internalFormat: GLint): TextureFormat {
// case GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT:
// break;
case GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT:
return TextureFormat.DXT1;
return TextureFormat.BC1;
case GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT:
return TextureFormat.DXT5;
return TextureFormat.BC3;
case GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT:
return TextureFormat.BC7;
case GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL:
return TextureFormat.ETC1_RGB;
case GLCompressedTextureInternalFormat.RGB8_ETC2:

View File

@@ -15,13 +15,12 @@ import {
TextureFormat,
resourceLoader
} from "@galacean/engine-core";
import { BinomialLLCTranscoder } from "./transcoder/BinomialLLCTranscoder";
import { KTX2Container } from "./KTX2Container";
import { KhronosTranscoder } from "./transcoder/KhronosTranscoder";
import { KTX2TargetFormat } from "./KTX2TargetFormat";
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { MathUtil } from "@galacean/engine-math";
import { KTX2Container } from "./KTX2Container";
import { KTX2TargetFormat } from "./KTX2TargetFormat";
import { TranscodeResult } from "./transcoder/AbstractTranscoder";
import { BinomialLLCTranscoder } from "./transcoder/BinomialLLCTranscoder";
import { KhronosTranscoder } from "./transcoder/KhronosTranscoder";
@resourceLoader(AssetType.KTX, ["ktx2"])
export class KTX2Loader extends Loader<Texture2D | TextureCube> {
@@ -30,7 +29,8 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
private static _supportedMap = {
[KTX2TargetFormat.ASTC]: [GLCapabilityType.astc],
[KTX2TargetFormat.ETC]: [GLCapabilityType.etc],
[KTX2TargetFormat.DXT]: [GLCapabilityType.s3tc],
[KTX2TargetFormat.BC7]: [GLCapabilityType.bptc],
[KTX2TargetFormat.BC1_BC3]: [GLCapabilityType.s3tc],
[KTX2TargetFormat.PVRTC]: [GLCapabilityType.pvrtc, GLCapabilityType.pvrtc_webkit]
};
@@ -61,12 +61,12 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
ktx2Container.pixelWidth !== ktx2Container.pixelHeight)
) {
Logger.warn("PVRTC image need power of 2 and width===height, downgrade to RGBA8");
return KTX2TargetFormat.RGBA8;
return KTX2TargetFormat.R8G8B8A8;
}
if (targetFormat === null) {
Logger.warn("Can't support any compressed texture, downgrade to RGBA8");
return KTX2TargetFormat.RGBA8;
return KTX2TargetFormat.R8G8B8A8;
}
// TODO support bc7: https://github.com/galacean/engine/issues/1371
return targetFormat;
@@ -77,7 +77,8 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
formatPriorities: KTX2TargetFormat[] = [
KTX2TargetFormat.ASTC,
KTX2TargetFormat.ETC,
KTX2TargetFormat.DXT,
KTX2TargetFormat.BC7,
KTX2TargetFormat.BC1_BC3,
KTX2TargetFormat.PVRTC
]
): KTX2TargetFormat | null {
@@ -174,11 +175,13 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
return TextureFormat.ASTC_4x4;
case KTX2TargetFormat.ETC:
return hasAlpha ? TextureFormat.ETC2_RGBA8 : TextureFormat.ETC2_RGB;
case KTX2TargetFormat.DXT:
return hasAlpha ? TextureFormat.DXT5 : TextureFormat.DXT1;
case KTX2TargetFormat.BC7:
return TextureFormat.BC7;
case KTX2TargetFormat.BC1_BC3:
return hasAlpha ? TextureFormat.BC1 : TextureFormat.BC3;
case KTX2TargetFormat.PVRTC:
return hasAlpha ? TextureFormat.PVRTC_RGBA4 : TextureFormat.PVRTC_RGB4;
case KTX2TargetFormat.RGBA8:
case KTX2TargetFormat.R8G8B8A8:
return TextureFormat.R8G8B8A8;
}
}
@@ -194,7 +197,7 @@ export interface KTX2Params {
declare module "@galacean/engine-core" {
interface EngineConfiguration {
/** ktx2 options */
/** KTX2 loader options. */
ktx2Loader?: {
/** Worker count for transcoder, default is 4. */
workerCount?: number;

View File

@@ -4,18 +4,18 @@
export enum KTX2TargetFormat {
/** RGB(A) compressed format, 128 bits per 4x4 pixel block. */
ASTC,
/** Unsupported format. */
/** RGB(A) compressed format, 128 bits per 4x4 pixel block. */
BC7,
/** RGB(A) compressed format. */
DXT,
/** RGB(A) compressed format. */
/** RGB(A) compressed format, 4 bits per pixel if no alpha channel, 8 bits per pixel if has alpha channel. */
BC1_BC3,
/** RGB(A) compressed format, 4 bits per pixel. */
PVRTC,
/** RGB(A) compressed format. */
/** RGB(A) compressed format, 4 bits per pixel if no alpha channel, 8 bits per pixel if has alpha channel. */
ETC,
/** R8 format*/
/** R format, 8 bits per pixel. */
R8,
/** RG8 format*/
RG8,
/** RGBA8 format*/
RGBA8
/** RG format, 16 bits per pixel. */
R8G8,
/** RGBA format, 32 bits per pixel. */
R8G8B8A8
}

View File

@@ -1,6 +1,6 @@
import { GLCapabilityType } from "@galacean/engine-core";
import { GLCompressedTextureInternalFormat } from "./type";
import { WebGLGraphicDevice } from "./WebGLGraphicDevice";
import { GLCompressedTextureInternalFormat } from "./type";
type extensionKey = string;
@@ -111,7 +111,11 @@ export class GLCapability {
RGBA_PVRTC_2BPPV1_IMG,
// s3tc
RGB_S3TC_DXT1_EXT,
RGBA_S3TC_DXT5_EXT
RGBA_S3TC_DXT5_EXT,
// bptc
RGBA_BPTC_UNORM_EXT,
RGB_BPTC_UNSIGNED_FLOAT_EXT
} = GLCompressedTextureInternalFormat;
if (
(internalType >= RGBA_ASTC_4X4_KHR && RGBA_ASTC_12X12_KHR <= RGBA_ASTC_12X12_KHR) ||
@@ -126,6 +130,8 @@ export class GLCapability {
return this.canIUse(GLCapabilityType.pvrtc);
} else if (internalType >= RGB_S3TC_DXT1_EXT && internalType <= RGBA_S3TC_DXT5_EXT) {
return this.canIUse(GLCapabilityType.s3tc);
} else if (internalType >= RGBA_BPTC_UNORM_EXT && internalType <= RGB_BPTC_UNSIGNED_FLOAT_EXT) {
return this.canIUse(GLCapabilityType.bptc);
}
return false;
}
@@ -159,6 +165,7 @@ export class GLCapability {
pvrtc_webkit,
s3tc,
s3tc_webkit,
bptc,
textureFloat,
textureHalfFloat,
@@ -197,6 +204,7 @@ export class GLCapability {
cap.set(etc1, !!(requireExtension(etc1) || requireExtension(etc1_webkit)));
cap.set(pvrtc, !!(requireExtension(pvrtc) || requireExtension(pvrtc_webkit)));
cap.set(s3tc, !!(requireExtension(s3tc) || requireExtension(s3tc_webkit)));
cap.set(bptc, !!requireExtension(bptc));
}
/**

View File

@@ -11,8 +11,8 @@ import {
TextureUsage,
TextureWrapMode
} from "@galacean/engine-core";
import { GLCompressedTextureInternalFormat, TextureFormatDetail } from "./type";
import { WebGLGraphicDevice } from "./WebGLGraphicDevice";
import { GLCompressedTextureInternalFormat, TextureFormatDetail } from "./type";
/**
* Texture in WebGL platform.
@@ -96,16 +96,21 @@ export class GLTexture implements IPlatformTexture {
dataType: gl.FLOAT,
isCompressed: false
};
case TextureFormat.DXT1:
case TextureFormat.BC1:
return {
internalFormat: GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
isCompressed: true
};
case TextureFormat.DXT5:
case TextureFormat.BC3:
return {
internalFormat: GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT,
isCompressed: true
};
case TextureFormat.BC7:
return {
internalFormat: GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT,
isCompressed: true
};
case TextureFormat.ETC1_RGB:
return {
internalFormat: GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL,

View File

@@ -110,5 +110,11 @@ export enum GLCompressedTextureInternalFormat {
RGB_S3TC_DXT1_EXT = 0x83f0,
RGBA_S3TC_DXT1_EXT = 0x83f1,
RGBA_S3TC_DXT3_EXT = 0x83f2,
RGBA_S3TC_DXT5_EXT = 0x83f3
RGBA_S3TC_DXT5_EXT = 0x83f3,
// bptc
RGBA_BPTC_UNORM_EXT = 0x8e8c,
SRGB_ALPHA_BPTC_UNORM_EXT = 0x8e8d,
RGB_BPTC_SIGNED_FLOAT_EXT = 0x8e8e,
RGB_BPTC_UNSIGNED_FLOAT_EXT = 0x8e8f
}