diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index 594f3a44c..916b320c0 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -38,6 +38,7 @@ import { provideUnboundDraw } from '../rendererOf.js'; import { listenerList, toCartesian, uncaught } from './common.js'; import { createGpuIcons, GPU_ICON_CAPABILITIES } from './gpuIcons.js'; import { createModelSets } from './drawModels.js'; +import { materialTexture } from './internals.js'; import { createMeshSets } from './drawMeshes.js'; import { createQuadSets } from './drawQuads.js'; import { createCesiumFields } from './drawFields.js'; @@ -3727,7 +3728,7 @@ export function createCesiumDraw({ function dropPendingTexture(image) { const texture = image.texture; if (!texture || texture.isDestroyed()) return; - if (image.material._textures?.image === texture) return; + if (materialTexture(image.material, 'image') === texture) return; texture.destroy(); image.texture = null; } diff --git a/src/render/cesium/internals.js b/src/render/cesium/internals.js index 80a33f8b4..f6053f896 100644 --- a/src/render/cesium/internals.js +++ b/src/render/cesium/internals.js @@ -205,3 +205,16 @@ export function creditDisplayContents(display) { logo: display?._currentCesiumCredit, }; } + +/** + * The texture a material holds for an image uniform. A material takes a + * texture handed to a uniform (and destroys it when it takes the next one, + * or is destroyed) only when it next updates; `draw.js` destroys one handed + * over but never taken, so it does not leak. + * @param {object} material Cesium Material. + * @param {string} uniform The uniform's name. + * @returns {object|undefined} Cesium Texture. + */ +export function materialTexture(material, uniform) { + return material?._textures?.[uniform]; +} diff --git a/src/render/cesium/internals.test.mjs b/src/render/cesium/internals.test.mjs index d454272aa..99cedeeee 100644 --- a/src/render/cesium/internals.test.mjs +++ b/src/render/cesium/internals.test.mjs @@ -15,6 +15,7 @@ import { defaultView, lastRenderTime, lastSeenCamera, + materialTexture, onEngineWorkCompleted, pickOffscreenView, renderPending, @@ -59,6 +60,7 @@ test('every private field the app reaches is still declared by the installed Ces [ENGINE, 'Scene/CreditDisplay.js', '_staticCredits'], [ENGINE, 'Scene/CreditDisplay.js', '_currentFrameCredits'], [ENGINE, 'Scene/CreditDisplay.js', '_currentCesiumCredit'], + [ENGINE, 'Scene/Material.js', '_textures'], ]; const missing = fields.filter( ([root, file, field]) => @@ -84,6 +86,14 @@ test('every private field the app reaches is still declared by the installed Ces /\nTaskProcessor\.taskCompletedEvent = /, ); assert.match(read('Scene/FrameState.js'), /this\.afterRender = \[\]/); + // A material takes a texture handed to a uniform only as it updates, + // destroying the one it held: one handed over and replaced first is + // no one's (`materialTexture`, draw.js's `dropPendingTexture`). + assert.match( + read('Scene/Material.js'), + /uniformValue instanceof Texture && uniformValue !== texture\) \{[\s\S]{0,200}?tmp\.destroy\(\);[\s\S]{0,40}?material\._textures\[uniformId\] = uniformValue;/, + 'Material takes a uniform texture on update', + ); // What makes them matter: each completion redraws the scene. const scene = read('Scene/Scene.js'); // Every tick empties the after-render queue, outside its draw. @@ -136,6 +146,12 @@ test('the helpers read and write exactly those fields', () => { const clone = {}; assert.equal(lastSeenCamera({ _view: { _cameraClone: clone } }), clone); assert.equal(lastSeenCamera({}), undefined); + const texture = {}; + assert.equal( + materialTexture({ _textures: { image: texture } }, 'image'), + texture, + ); + assert.equal(materialTexture({}, 'image'), undefined); const error = new Error('404'); assert.equal(billboardLoadError({ loadError: error }), error); const gl = {};