From 4a8f30bfc3bce793d2e1cb466be65cd5c1705b45 Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Mon, 23 Jun 2025 18:25:23 +0200 Subject: [PATCH 1/4] fix: update texture loading logic to handle only image textures --- src/core/CoreTextureManager.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/core/CoreTextureManager.ts b/src/core/CoreTextureManager.ts index 16e9b307..637b608d 100644 --- a/src/core/CoreTextureManager.ts +++ b/src/core/CoreTextureManager.ts @@ -413,21 +413,21 @@ export class CoreTextureManager extends EventEmitter { texture.freeTextureData(); } - // these types of textures don't need to be downloaded - // Technically the noise texture shouldn't either, but it's a special case - // and not really used in production so who cares ¯\_(ツ)_/¯ - if ( - (texture.type === TextureType.color || - texture.type === TextureType.renderToTexture) && - texture.state !== 'initial' - ) { - texture.setState('fetched'); - this.enqueueUploadTexture(texture); + texture.setState('loading'); + + // Only image textures can be downloaded, so we check the type + if (texture.type !== TextureType.image) { + texture + .getTextureData() + .then(() => { + this.enqueueUploadTexture(texture); + }) + .catch((err) => { + console.error(err); + }); return; } - texture.setState('loading'); - // prioritize the texture for immediate loading if (priority === true) { texture From 9f6ab6404388c81cf6c7ae43ac4160c8883c4dd2 Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Mon, 23 Jun 2025 18:33:02 +0200 Subject: [PATCH 2/4] fix: upload directly if not image --- src/core/CoreTextureManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/CoreTextureManager.ts b/src/core/CoreTextureManager.ts index 637b608d..13188cf1 100644 --- a/src/core/CoreTextureManager.ts +++ b/src/core/CoreTextureManager.ts @@ -420,7 +420,7 @@ export class CoreTextureManager extends EventEmitter { texture .getTextureData() .then(() => { - this.enqueueUploadTexture(texture); + this.uploadTexture(texture); }) .catch((err) => { console.error(err); From 878ced9c3e069566e056a8bc50df4851ca5abf5d Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Mon, 23 Jun 2025 19:04:20 +0200 Subject: [PATCH 3/4] feat: remove texture download queue --- src/core/CoreTextureManager.ts | 107 +++++++++------------------------ 1 file changed, 29 insertions(+), 78 deletions(-) diff --git a/src/core/CoreTextureManager.ts b/src/core/CoreTextureManager.ts index 13188cf1..7a6ade8c 100644 --- a/src/core/CoreTextureManager.ts +++ b/src/core/CoreTextureManager.ts @@ -178,7 +178,6 @@ export class CoreTextureManager extends EventEmitter { */ txConstructors: Partial = {}; - private downloadTextureSourceQueue: Array = []; private priorityQueue: Array = []; private uploadTextureQueue: Array = []; private initialized = false; @@ -229,7 +228,7 @@ export class CoreTextureManager extends EventEmitter { .then((result) => { this.initialize(result); }) - .catch((e) => { + .catch(() => { console.warn( '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.', ); @@ -290,15 +289,6 @@ export class CoreTextureManager extends EventEmitter { this.emit('initialized'); } - /** - * Enqueue a texture for downloading its source image. - */ - enqueueDownloadTextureSource(texture: Texture): void { - if (!this.downloadTextureSourceQueue.includes(texture)) { - this.downloadTextureSourceQueue.push(texture); - } - } - /** * Enqueue a texture for uploading to the GPU. * @@ -378,10 +368,7 @@ export class CoreTextureManager extends EventEmitter { } // if the texture is already being processed, don't load it again - if ( - this.downloadTextureSourceQueue.includes(texture) === true || - this.uploadTextureQueue.includes(texture) === true - ) { + if (this.uploadTextureQueue.includes(texture) === true) { return; } @@ -391,13 +378,6 @@ export class CoreTextureManager extends EventEmitter { texture.ctxTexture !== undefined && texture.ctxTexture.state === 'loading' ) { - // if the texture has texture data, queue it for upload - if (texture.textureData !== null) { - this.enqueueUploadTexture(texture); - return; - } - - // else we will have to re-download the texture texture.free(); } @@ -415,33 +395,29 @@ export class CoreTextureManager extends EventEmitter { texture.setState('loading'); - // Only image textures can be downloaded, so we check the type - if (texture.type !== TextureType.image) { - texture - .getTextureData() - .then(() => { - this.uploadTexture(texture); - }) - .catch((err) => { - console.error(err); - }); - return; - } + // Get the texture data + texture + .getTextureData() + .then(() => { + texture.setState('fetched'); - // prioritize the texture for immediate loading - if (priority === true) { - texture - .getTextureData() - .then(() => { + // For non-image textures, upload immediately + if (texture.type !== TextureType.image) { this.uploadTexture(texture); - }) - .catch((err) => { - console.error(err); - }); - } - - // enqueue the texture for download and upload - this.enqueueDownloadTextureSource(texture); + } else { + // For image textures, queue for throttled upload + // If it's a priority texture, upload it immediately + if (priority === true) { + this.uploadTexture(texture); + } else { + this.enqueueUploadTexture(texture); + } + } + }) + .catch((err) => { + console.error(err); + texture.setState('failed'); + }); } /** @@ -472,16 +448,13 @@ export class CoreTextureManager extends EventEmitter { * Check if a texture is being processed */ isProcessingTexture(texture: Texture): boolean { - return ( - this.downloadTextureSourceQueue.includes(texture) === true || - this.uploadTextureQueue.includes(texture) === true - ); + return this.uploadTextureQueue.includes(texture) === true; } /** - * Process a limited number of downloads and uploads. + * Process a limited number of uploads. * - * @param maxItems - The maximum number of items to process + * @param maxProcessingTime - The maximum processing time in milliseconds */ processSome(maxProcessingTime: number): void { if (this.initialized === false) { @@ -509,29 +482,12 @@ export class CoreTextureManager extends EventEmitter { platform.getTimeStamp() - startTime < maxProcessingTime ) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.uploadTexture(this.uploadTextureQueue.pop()!); - } - - // Process downloads - while ( - this.downloadTextureSourceQueue.length > 0 && - platform.getTimeStamp() - startTime < maxProcessingTime - ) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const texture = this.downloadTextureSourceQueue.shift()!; - texture.getTextureData().then(() => { - if (texture.state === 'fetched') { - this.enqueueUploadTexture(texture); - } - }); + this.uploadTexture(this.uploadTextureQueue.shift()!); } } public hasUpdates(): boolean { - return ( - this.downloadTextureSourceQueue.length > 0 || - this.uploadTextureQueue.length > 0 - ); + return this.uploadTextureQueue.length > 0; } /** @@ -572,16 +528,11 @@ export class CoreTextureManager extends EventEmitter { } /** - * Remove texture from the queue's + * Remove texture from the upload queue * * @param texture - The texture to remove */ removeTextureFromQueue(texture: Texture): void { - const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture); - if (downloadIndex !== -1) { - this.downloadTextureSourceQueue.splice(downloadIndex, 1); - } - const uploadIndex = this.uploadTextureQueue.indexOf(texture); if (uploadIndex !== -1) { this.uploadTextureQueue.splice(uploadIndex, 1); From df94a16966e43c3643b0f99d19c62368cbac7d84 Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Mon, 23 Jun 2025 21:37:45 +0200 Subject: [PATCH 4/4] fix: update texture state handling to set 'failed' if not fetched --- src/core/CoreTextureManager.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/CoreTextureManager.ts b/src/core/CoreTextureManager.ts index 7a6ade8c..04b3dd07 100644 --- a/src/core/CoreTextureManager.ts +++ b/src/core/CoreTextureManager.ts @@ -399,7 +399,10 @@ export class CoreTextureManager extends EventEmitter { texture .getTextureData() .then(() => { - texture.setState('fetched'); + if (texture.state !== 'fetched') { + texture.setState('failed'); + return; + } // For non-image textures, upload immediately if (texture.type !== TextureType.image) {