Skip to content

Commit

Permalink
Fix ktx2 offset is not correct (#1820)
Browse files Browse the repository at this point in the history
* fix: offset is not correct
  • Loading branch information
gz65555 authored Oct 17, 2023
1 parent d7f6945 commit f95cc90
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/loader/src/ktx2/KTX2Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class KTX2Container {
};
}

const endpointsByteOffset = sgdByteOffset + sgdReader.offset;
const endpointsByteOffset = sgdByteOffset + sgdReader.position;
const selectorsByteOffset = endpointsByteOffset + endpointsByteLength;
const tablesByteOffset = selectorsByteOffset + selectorsByteLength;
const extendedByteOffset = tablesByteOffset + tablesByteLength;
Expand Down
46 changes: 17 additions & 29 deletions packages/loader/src/ktx2/KTX2Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Loader,
Logger,
ResourceManager,
Texture,
Texture2D,
TextureCube,
TextureCubeFace,
Expand All @@ -27,6 +26,13 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
private static _isBinomialInit: boolean = false;
private static _binomialLLCTranscoder: BinomialLLCTranscoder;
private static _khronosTranscoder: KhronosTranscoder;
private static _priorityFormats: KTX2TargetFormat[] = [
KTX2TargetFormat.BC7,
KTX2TargetFormat.ASTC,
KTX2TargetFormat.BC1_BC3,
KTX2TargetFormat.ETC,
KTX2TargetFormat.PVRTC
];
private static _supportedMap = {
[KTX2TargetFormat.ASTC]: [GLCapabilityType.astc],
[KTX2TargetFormat.ETC]: [GLCapabilityType.etc],
Expand All @@ -49,7 +55,7 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
/** @internal */
static _parseBuffer(buffer: Uint8Array, engine: Engine, params?: KTX2Params) {
const ktx2Container = new KTX2Container(buffer);
const formatPriorities = params?.priorityFormats;
const formatPriorities = params?.priorityFormats ?? KTX2Loader._priorityFormats;
const targetFormat = KTX2Loader._decideTargetFormat(engine, ktx2Container, formatPriorities);
let transcodeResultPromise: Promise<TranscodeResult>;
if (KTX2Loader._isBinomialInit || !KhronosTranscoder.transcoderMap[targetFormat] || !ktx2Container.isUASTC) {
Expand Down Expand Up @@ -127,16 +133,7 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
return targetFormat;
}

private static _detectSupportedFormat(
renderer: any,
priorityFormats: KTX2TargetFormat[] = [
KTX2TargetFormat.ASTC,
KTX2TargetFormat.ETC,
KTX2TargetFormat.BC7,
KTX2TargetFormat.BC1_BC3,
KTX2TargetFormat.PVRTC
]
): KTX2TargetFormat | null {
private static _detectSupportedFormat(renderer: any, priorityFormats: KTX2TargetFormat[]): KTX2TargetFormat | null {
for (let i = 0; i < priorityFormats.length; i++) {
const capabilities = this._supportedMap[priorityFormats[i]];
for (let j = 0; j < capabilities.length; j++) {
Expand Down Expand Up @@ -181,6 +178,7 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
override initialize(engine: Engine, configuration: EngineConfiguration): Promise<void> {
if (configuration.ktx2Loader) {
const options = configuration.ktx2Loader;
if (options.priorityFormats) KTX2Loader._priorityFormats = options.priorityFormats;
if (this._isKhronosSupported(options.priorityFormats, engine) && options.workerCount !== 0) {
return KTX2Loader._getKhronosTranscoder(options.workerCount).init();
} else {
Expand All @@ -205,7 +203,7 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
}

private _isKhronosSupported(
priorityFormats: KTX2TargetFormat[] | KTX2TargetFormat[][] = [
priorityFormats: KTX2TargetFormat[] = [
KTX2TargetFormat.ASTC,
KTX2TargetFormat.ETC,
KTX2TargetFormat.BC7,
Expand All @@ -215,27 +213,17 @@ export class KTX2Loader extends Loader<Texture2D | TextureCube> {
],
engine: any
): boolean {
const supportedList = new Array<KTX2TargetFormat>();
if (Array.isArray(priorityFormats[0])) {
for (let i = 0; i < priorityFormats.length; i++) {
supportedList.push(
KTX2Loader._detectSupportedFormat(engine._hardwareRenderer, <KTX2TargetFormat[]>priorityFormats[i])
);
}
} else {
supportedList.push(
KTX2Loader._detectSupportedFormat(engine._hardwareRenderer, <KTX2TargetFormat[]>priorityFormats)
);
}
return supportedList.every((format) => KhronosTranscoder.transcoderMap[format]);
return !!KhronosTranscoder.transcoderMap[
KTX2Loader._detectSupportedFormat(engine._hardwareRenderer, priorityFormats)
];
}
}

/**
* KTX2 loader params interface.
*/
export interface KTX2Params {
/** Priority transcoding format queue, default is ASTC/ETC/DXT/PVRTC/RGBA8. */
/** Priority transcoding format queue which is preferred options, default is BC7/ASTC/BC3_BC1/ETC/PVRTC/R8G8B8A8. */
priorityFormats: KTX2TargetFormat[];
}

Expand All @@ -245,8 +233,8 @@ declare module "@galacean/engine-core" {
ktx2Loader?: {
/** Worker count for transcoder, default is 4. */
workerCount?: number;
/** Pre-initialization according to the priority transcoding format queue, default is ASTC/ETC/DXT/PVRTC/RGBA8. */
priorityFormats?: KTX2TargetFormat[] | KTX2TargetFormat[][];
/** Global transcoding format queue which will be used if not specified in per-instance param, default is BC7/ASTC/BC3_BC1/ETC/PVRTC/R8G8B8A8. */
priorityFormats?: KTX2TargetFormat[];
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class MeshDecoder {
// @ts-ignore Vector3 is not compatible with {x: number, y: number, z: number}.
encodedMeshData.bounds && modelMesh.bounds.copyFrom(encodedMeshData.bounds);

const offset = Math.ceil(bufferReader.offset / 4) * 4;
const offset = Math.ceil(bufferReader.position / 4) * 4;
const buffer = bufferReader.data.buffer;
const byteOffset = bufferReader.data.byteOffset;

Expand Down

0 comments on commit f95cc90

Please sign in to comment.