Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clipping planes crash when adding plane to empty collection #7168

Merged
merged 4 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 9 additions & 6 deletions Source/Scene/ClippingPlaneCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,8 @@ define([

function computeTextureResolution(pixelsNeeded, result) {
var maxSize = ContextLimits.maximumTextureSize;
var width = Math.min(pixelsNeeded, maxSize);
var height = Math.ceil(pixelsNeeded / width);
result.x = Math.max(width, 1);
result.y = Math.max(height, 1);
result.x = Math.min(pixelsNeeded, maxSize);
result.y = Math.ceil(pixelsNeeded / result.x);
return result;
}

Expand All @@ -495,7 +493,6 @@ define([
// In RGBA UNSIGNED_BYTE, A plane is a float in [0, 1) packed to RGBA and an Oct32 quantized normal,
// so 8 bits or 2 pixels in RGBA.
var pixelsNeeded = useFloatTexture ? this.length : this.length * 2;
var requiredResolution = computeTextureResolution(pixelsNeeded, textureResolutionScratch);

if (defined(clippingPlanesTexture)) {
var currentPixelCount = clippingPlanesTexture.width * clippingPlanesTexture.height;
Expand All @@ -508,10 +505,17 @@ define([
pixelsNeeded < 0.25 * currentPixelCount) {
clippingPlanesTexture.destroy();
clippingPlanesTexture = undefined;
this._clippingPlanesTexture = undefined;
}
}

// If there are no clipping planes, there's nothing to update.
if (this.length === 0) {
return;
}

if (!defined(clippingPlanesTexture)) {
var requiredResolution = computeTextureResolution(pixelsNeeded, textureResolutionScratch);
// Allocate twice as much space as needed to avoid frequent texture reallocation.
// Allocate in the Y direction, since texture may be as wide as context texture support.
requiredResolution.y *= 2;
Expand Down Expand Up @@ -572,7 +576,6 @@ define([
} else {
offsetY = Math.floor((dirtyIndex * 2) / clippingPlanesTexture.width);
offsetX = Math.floor((dirtyIndex * 2) - offsetY * clippingPlanesTexture.width);

packPlanesAsUint8(this, dirtyIndex, dirtyIndex + 1);
clippingPlanesTexture.copyFrom({
width : 2,
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/GlobeSurfaceShaderSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ define([
(imageryCutoutFlag << 21);

var currentClippingShaderState = 0;
if (defined(clippingPlanes)) {
if (defined(clippingPlanes) && clippingPlanes.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the clippingPlanes.length > 0 check is covered in GlobeSurfaceTileProvider.computeTileVisibility by a check for if the globe surface tile intersects any clipping planes. We also do this for 3D Tiles, so tiles in a tileset that aren't touched by any clipping planes won't have injected clipping code. This change doesn't seem like it will hurt beyond being a little inconsistent with what's happening elsewhere, so no change needed, just wanted you to be aware.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might not be needed here, but Model was crashing because it considered clipping planes enabled even if there weren't any, so I added this check everywhere else to be consistent.

currentClippingShaderState = enableClippingPlanes ? clippingPlanes.clippingPlanesState : 0;
}
var surfaceShader = surfaceTile.surfaceShader;
Expand Down
4 changes: 2 additions & 2 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ define([

function isClippingEnabled(model) {
var clippingPlanes = model._clippingPlanes;
return defined(clippingPlanes) && clippingPlanes.enabled;
return defined(clippingPlanes) && clippingPlanes.enabled && clippingPlanes.length !== 0;
}

/**
Expand Down Expand Up @@ -4250,7 +4250,7 @@ define([
// Regenerate shaders if ClippingPlaneCollection state changed or it was removed
var clippingPlanes = this._clippingPlanes;
var currentClippingPlanesState = 0;
if (defined(clippingPlanes) && clippingPlanes.enabled) {
if (defined(clippingPlanes) && clippingPlanes.enabled && clippingPlanes.length > 0) {
var clippingPlaneOffsetMatrix = defaultValue(this.clippingPlaneOffsetMatrix, modelMatrix);
Matrix4.multiply(context.uniformState.view3D, clippingPlaneOffsetMatrix, this._clippingPlaneModelViewMatrix);
currentClippingPlanesState = clippingPlanes.clippingPlanesState;
Expand Down
39 changes: 39 additions & 0 deletions Specs/Scene/ClippingPlaneCollectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ defineSuite([
scene.destroyForSpecs();
});

it('only creates texture when planes are added', function() {
var scene = createScene();

clippingPlanes = new ClippingPlaneCollection();
clippingPlanes.update(scene.frameState);
expect(clippingPlanes.texture).toBeUndefined();

clippingPlanes.add(planes[0]);
clippingPlanes.update(scene.frameState);

expect(clippingPlanes.texture).toBeDefined();
expect(isNaN(clippingPlanes.texture.width)).toBe(false);
expect(isNaN(clippingPlanes.texture.height)).toBe(false);
});

it('update fills the clipping plane texture with packed planes', function() {
var scene = createScene();

Expand Down Expand Up @@ -399,6 +414,30 @@ defineSuite([
scene.destroyForSpecs();
});

it('only creates texture when planes are added', function() {
var scene = createScene();

if (!ClippingPlaneCollection.useFloatTexture(scene.context)) {
// Don't fail just because float textures aren't supported
scene.destroyForSpecs();
return;
}

clippingPlanes = new ClippingPlaneCollection();
clippingPlanes.update(scene.frameState);
expect(clippingPlanes.texture).toBeUndefined();

clippingPlanes.add(planes[0]);
clippingPlanes.update(scene.frameState);

expect(clippingPlanes.texture).toBeDefined();
expect(isNaN(clippingPlanes.texture.width)).toBe(false);
expect(isNaN(clippingPlanes.texture.height)).toBe(false);

clippingPlanes.destroy();
scene.destroyForSpecs();
});

it('update fills the clipping plane texture with packed planes', function() {
var scene = createScene();

Expand Down