Skip to content

Commit

Permalink
feat: support bc7 (#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoLei1990 authored Jul 18, 2023
1 parent 5ff9f8e commit a84eb5c
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/base/Constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/texture/enums/TextureFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 15 additions & 12 deletions packages/loader/src/ktx2/KTX2Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import {
TextureFormat,
resourceLoader
} from "@galacean/engine-core";
import { BinomialLLCTranscoder } from "./transcoder/BinomialLLCTranscoder";
import { MathUtil } from "@galacean/engine-math";
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 { 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> {
Expand All @@ -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]
};

Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions packages/loader/src/ktx2/KTX2TargetFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 10 additions & 2 deletions packages/rhi-webgl/src/GLCapability.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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) ||
Expand All @@ -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;
}
Expand Down Expand Up @@ -159,6 +165,7 @@ export class GLCapability {
pvrtc_webkit,
s3tc,
s3tc_webkit,
bptc,

textureFloat,
textureHalfFloat,
Expand Down Expand Up @@ -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));
}

/**
Expand Down
11 changes: 8 additions & 3 deletions packages/rhi-webgl/src/GLTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion packages/rhi-webgl/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a84eb5c

Please sign in to comment.