Skip to content

Commit 2464be8

Browse files
committed
Better handled resources of canceled commands
1 parent 084d81d commit 2464be8

File tree

1 file changed

+63
-50
lines changed

1 file changed

+63
-50
lines changed

packages/engine/Source/Scene/DynamicEnvironmentMapManager.js

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function DynamicEnvironmentMapManager(options) {
7777
this._sphericalHarmonicCoefficientsDirty = false;
7878

7979
this._shouldRegenerateShaders = false;
80+
this._shouldReset = false;
8081

8182
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
8283

@@ -251,7 +252,7 @@ Object.defineProperties(DynamicEnvironmentMapManager.prototype, {
251252
}
252253

253254
this._position = Cartesian3.clone(value, this._position);
254-
this.reset();
255+
this._shouldReset = true;
255256
},
256257
},
257258

@@ -345,7 +346,7 @@ DynamicEnvironmentMapManager._updateCommandQueue = (frameState) => {
345346
DynamicEnvironmentMapManager._activeComputeCommandCount <
346347
DynamicEnvironmentMapManager._maximumComputeCommandCount
347348
) {
348-
if (command.canceled) {
349+
if (command.owner.isDestroyed() || command.canceled) {
349350
command = DynamicEnvironmentMapManager._nextFrameCommandQueue.shift();
350351
continue;
351352
}
@@ -402,7 +403,6 @@ DynamicEnvironmentMapManager.prototype.reset = function () {
402403
for (let i = 0; i < length; ++i) {
403404
if (defined(this._radianceMapComputeCommands[i])) {
404405
this._radianceMapComputeCommands[i].canceled = true;
405-
DynamicEnvironmentMapManager._activeComputeCommandCount--;
406406
}
407407
this._radianceMapComputeCommands[i] = undefined;
408408
}
@@ -411,19 +411,19 @@ DynamicEnvironmentMapManager.prototype.reset = function () {
411411
for (let i = 0; i < length; ++i) {
412412
if (defined(this._convolutionComputeCommands[i])) {
413413
this._convolutionComputeCommands[i].canceled = true;
414-
DynamicEnvironmentMapManager._activeComputeCommandCount--;
415414
}
416415
this._convolutionComputeCommands[i] = undefined;
417416
}
418417

419418
if (defined(this._irradianceComputeCommand)) {
420419
this._irradianceComputeCommand.canceled = true;
421-
DynamicEnvironmentMapManager._activeComputeCommandCount--;
422420
this._irradianceComputeCommand = undefined;
423421
}
424422

425423
this._radianceMapDirty = true;
426424
this._radianceCommandsDirty = true;
425+
this._convolutionsCommandsDirty = false;
426+
this._irradianceCommandDirty = false;
427427
};
428428

429429
const scratchPackedAtmosphere = new Cartesian3();
@@ -576,33 +576,34 @@ function updateRadianceMap(manager, frameState) {
576576
},
577577
},
578578
owner: manager,
579-
postExecute: () => {
580-
const commands = manager._radianceMapComputeCommands;
581-
if (!defined(commands[index])) {
582-
// This command was cancelled
583-
return;
584-
}
585-
commands[index] = undefined;
586-
587-
const framebuffer = new Framebuffer({
588-
context: context,
589-
colorTextures: [manager._radianceMapTextures[index]],
590-
});
591-
592-
// Copy the output texture into the corresponding cubemap face
593-
framebuffer._bind();
594-
manager._radianceCubeMap[face].copyFromFramebuffer();
595-
framebuffer._unBind();
596-
framebuffer.destroy();
597-
579+
});
580+
command.postExecute = () => {
581+
if (manager.isDestroyed() || command.canceled) {
598582
DynamicEnvironmentMapManager._activeComputeCommandCount--;
583+
return;
584+
}
599585

600-
if (!commands.some(defined)) {
601-
manager._convolutionsCommandsDirty = true;
602-
manager._shouldRegenerateShaders = true;
603-
}
604-
},
605-
});
586+
const commands = manager._radianceMapComputeCommands;
587+
commands[index] = undefined;
588+
589+
const framebuffer = new Framebuffer({
590+
context: context,
591+
colorTextures: [manager._radianceMapTextures[index]],
592+
});
593+
594+
// Copy the output texture into the corresponding cubemap face
595+
framebuffer._bind();
596+
manager._radianceCubeMap[face].copyFromFramebuffer();
597+
framebuffer._unBind();
598+
framebuffer.destroy();
599+
600+
DynamicEnvironmentMapManager._activeComputeCommandCount--;
601+
602+
if (!commands.some(defined)) {
603+
manager._convolutionsCommandsDirty = true;
604+
manager._shouldRegenerateShaders = true;
605+
}
606+
};
606607

607608
manager._radianceMapComputeCommands[i] = command;
608609
DynamicEnvironmentMapManager._queueCommand(command, frameState);
@@ -629,13 +630,14 @@ function updateSpecularMaps(manager, frameState) {
629630
const context = frameState.context;
630631

631632
let facesCopied = 0;
632-
const getPostExecute = (index, texture, face, level) => () => {
633-
// Copy output texture to corresponding face and mipmap level
634-
const commands = manager._convolutionComputeCommands;
635-
if (!defined(commands[index]) || commands[index].canceled) {
636-
// This command was cancelled
633+
const getPostExecute = (command, index, texture, face, level) => () => {
634+
if (manager.isDestroyed() || command.canceled) {
635+
DynamicEnvironmentMapManager._activeComputeCommandCount--;
637636
return;
638637
}
638+
639+
// Copy output texture to corresponding face and mipmap level
640+
const commands = manager._convolutionComputeCommands;
639641
commands[index] = undefined;
640642

641643
radianceCubeMap.copyFace(frameState, texture, face, level);
@@ -712,8 +714,14 @@ function updateSpecularMaps(manager, frameState) {
712714
return CubeMap.getDirection(face, scratchCartesian);
713715
},
714716
},
715-
postExecute: getPostExecute(index, texture, face, level),
716717
});
718+
command.postExecute = getPostExecute(
719+
command,
720+
index,
721+
texture,
722+
face,
723+
level,
724+
);
717725
manager._convolutionComputeCommands[index] = command;
718726
DynamicEnvironmentMapManager._queueCommand(command, frameState);
719727
++index;
@@ -737,7 +745,7 @@ function updateIrradianceResources(manager, frameState) {
737745
const dimensions = irradianceTextureDimensions;
738746

739747
let texture = manager._irradianceMapTexture;
740-
if (defined(texture)) {
748+
if (defined(texture) && !texture.isDestroyed()) {
741749
texture.destroy();
742750
}
743751

@@ -761,22 +769,25 @@ function updateIrradianceResources(manager, frameState) {
761769
const command = new ComputeCommand({
762770
fragmentShaderSource: fs,
763771
outputTexture: texture,
772+
owner: manager,
764773
uniformMap: {
765774
u_radianceMap: () => manager._radianceCubeMap ?? context.defaultTexture,
766775
},
767-
postExecute: () => {
768-
if (!defined(manager._irradianceComputeCommand)) {
769-
// This command was cancelled
770-
return;
771-
}
772-
manager._irradianceTextureDirty = false;
773-
manager._irradianceComputeCommand = undefined;
774-
manager._sphericalHarmonicCoefficientsDirty = true;
775-
manager._irradianceMapFS = undefined;
776+
});
776777

778+
command.postExecute = () => {
779+
if (manager.isDestroyed() || command.canceled) {
777780
DynamicEnvironmentMapManager._activeComputeCommandCount--;
778-
},
779-
});
781+
return;
782+
}
783+
manager._irradianceTextureDirty = false;
784+
manager._irradianceComputeCommand = undefined;
785+
manager._sphericalHarmonicCoefficientsDirty = true;
786+
manager._irradianceMapFS = undefined;
787+
788+
DynamicEnvironmentMapManager._activeComputeCommandCount--;
789+
};
790+
780791
manager._irradianceComputeCommand = command;
781792
DynamicEnvironmentMapManager._queueCommand(command, frameState);
782793
manager._irradianceTextureDirty = true;
@@ -792,7 +803,7 @@ function updateSphericalHarmonicCoefficients(manager, frameState) {
792803
const context = frameState.context;
793804

794805
if (!defined(manager._irradianceMapTexture)) {
795-
// Operation was cancelled
806+
// Operation was canceled
796807
return;
797808
}
798809

@@ -864,9 +875,11 @@ DynamicEnvironmentMapManager.prototype.update = function (frameState) {
864875
this.maximumSecondsDifference,
865876
));
866877

867-
if (regenerateEnvironmentMap) {
878+
if (this._shouldReset || regenerateEnvironmentMap) {
868879
this.reset();
880+
this._shouldReset = false;
869881
this._lastTime = JulianDate.clone(frameState.time, this._lastTime);
882+
return;
870883
}
871884

872885
if (this._radianceMapDirty) {

0 commit comments

Comments
 (0)