Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/CoreTextureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export class CoreTextureManager extends EventEmitter {
this.stage.txMemManager.criticalCleanupRequested === true
) {
// we're at a critical memory threshold, don't upload textures
this.enqueueUploadTexture(texture);
texture.setState('failed');
return;
}

Expand Down
59 changes: 21 additions & 38 deletions src/core/TextureMemoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ export class TextureMemoryManager {

// Skip textures that are in transitional states - we only want to clean up
// textures that are in a stable state (loaded, failed, or freed)
if (Texture.TRANSITIONAL_TEXTURE_STATES.includes(texture.state)) {
if (
texture.state === 'initial' ||
Texture.TRANSITIONAL_TEXTURE_STATES.includes(texture.state)
) {
continue;
}

Expand Down Expand Up @@ -303,48 +306,28 @@ export class TextureMemoryManager {
// Free non-renderable textures until we reach the target threshold
const memTarget = critical ? this.criticalThreshold : this.targetThreshold;

// sort by renderability
const filteredAndSortedTextures: Texture[] = [];
const textures = [...this.loadedTextures.keys()];
for (let i = 0; i < textures.length; i++) {
const texture = textures[i];
if (texture === undefined) {
continue;
}

if (
texture.type === TextureType.image ||
texture.type === TextureType.noise ||
texture.type === TextureType.renderToTexture
) {
if (texture.renderable === true) {
filteredAndSortedTextures.push(texture);
} else {
filteredAndSortedTextures.unshift(texture);
}
}
}
// Filter for textures that are candidates for cleanup
// note: This is an expensive operation, so we only do it in deep cleanup
const cleanupCandidates = [...this.loadedTextures.keys()].filter(
(texture) => {
return (
(texture.type === TextureType.image ||
texture.type === TextureType.noise ||
texture.type === TextureType.renderToTexture) &&
texture.renderable === false &&
texture.preventCleanup === false &&
texture.state !== 'initial' &&
!Texture.TRANSITIONAL_TEXTURE_STATES.includes(texture.state)
);
},
);

while (this.memUsed >= memTarget && filteredAndSortedTextures.length > 0) {
const texture = filteredAndSortedTextures.shift();
while (this.memUsed >= memTarget && cleanupCandidates.length > 0) {
const texture = cleanupCandidates.shift();
if (texture === undefined) {
continue;
}

if (texture.preventCleanup === true) {
continue;
}

if (texture.renderable === true) {
break;
}

// Skip textures that are in transitional states - we only want to clean up
// textures that are in a stable state (loaded, failed, or freed)
if (Texture.TRANSITIONAL_TEXTURE_STATES.includes(texture.state)) {
break;
}

this.destroyTexture(texture);
}
}
Expand Down