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

Cache pickposition #5169

Merged
merged 15 commits into from
Apr 18, 2017
Merged
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Change Log
* Fixed issues with imagerySplitPosition and the international date line in 2D mode. [#5151](https://github.com/AnalyticalGraphicsInc/cesium/pull/5151)
* Fixed an issue with `TileBoundingBox` that caused the terrain to disappear in certain places [4032](https://github.com/AnalyticalGraphicsInc/cesium/issues/4032)
* `QuadtreePrimitive` now uses `frameState.afterRender` to fire `tileLoadProgressEvent` [#3450](https://github.com/AnalyticalGraphicsInc/cesium/issues/3450)
* `Scene.pickPosition` now caches results per frame to increase performance [#5117](https://github.com/AnalyticalGraphicsInc/cesium/issues/5117)

### 1.31 - 2017-03-01

Expand Down
13 changes: 13 additions & 0 deletions Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ define([
this._cameraStartFired = false;
this._cameraMovedTime = undefined;

this._pickPositionCache = {};

/**
* Exceptions occurring in <code>render</code> are always caught in order to raise the
* <code>renderError</code> event. If this property is true, the error is rethrown
Expand Down Expand Up @@ -2523,6 +2525,8 @@ define([
var scratchEyeTranslation = new Cartesian3();

function render(scene, time) {
scene._pickPositionCache = {}; //clean cache
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid doing this allocation per frame? We are really careful about not doing allocation per vertex, per command, per primitive, etc., but we even try to avoid per frame allocations.

Instead, perhaps set a flag like _pickPositionCacheDirty to true. Then in pickPositionWorldCoordinates, clear the cache and set dirty to false if it was true.


if (!defined(time)) {
time = JulianDate.now();
}
Expand Down Expand Up @@ -2954,6 +2958,12 @@ define([
}
//>>includeEnd('debug');

var cacheKey;
Copy link
Contributor

Choose a reason for hiding this comment

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

cacheKey needs to be set.


if (this._pickPositionCache.hasOwnProperty(cacheKey)){
return Cartesian3.clone(this._pickPositionCache[cacheKey], result);
}

var context = this._context;
var uniformState = context.uniformState;

Expand Down Expand Up @@ -3015,10 +3025,12 @@ define([
uniformState.update(this.frameState);
}

this._pickPositionCache[cacheKey] = Cartesian3.clone(result);
return result;
}
}

this._pickPositionCache[cacheKey] = undefined;
return undefined;
};

Expand Down Expand Up @@ -3053,6 +3065,7 @@ define([
var cart = projection.unproject(result, scratchPickPositionCartographic);
ellipsoid.cartographicToCartesian(cart, result);
}

return result;
};

Expand Down
39 changes: 39 additions & 0 deletions Specs/Scene/SceneSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defineSuite([
'Scene/Scene',
'Scene/ScreenSpaceCameraController',
'Scene/TweenCollection',
'Scene/SceneTransforms',
'Specs/createCanvas',
'Specs/createScene',
'Specs/equals',
Expand Down Expand Up @@ -72,6 +73,7 @@ defineSuite([
Scene,
ScreenSpaceCameraController,
TweenCollection,
SceneTransforms,
createCanvas,
createScene,
equals,
Expand Down Expand Up @@ -838,6 +840,43 @@ defineSuite([
});
});

it('pickPosition caches results per frame',function(){
if (!scene.pickPositionSupported) {
return;
}

var rectangle = Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0);
scene.camera.setView({ destination : rectangle });

var canvas = scene.canvas;
var windowPosition = new Cartesian2(canvas.clientWidth / 2, canvas.clientHeight / 2);
var originaltransformWindowToDrawingBuffer = SceneTransforms.transformWindowToDrawingBuffer;
spyOn(SceneTransforms, 'transformWindowToDrawingBuffer').and.callThrough();

expect(scene).toRenderAndCall(function() {
scene.pickPosition(windowPosition);
expect(SceneTransforms.transformWindowToDrawingBuffer).toHaveBeenCalled();

scene.pickPosition(windowPosition);
expect(SceneTransforms.transformWindowToDrawingBuffer.calls.count()).toEqual(1);

var rectanglePrimitive = createRectangle(rectangle);
rectanglePrimitive.appearance.material.uniforms.color = new Color(1.0, 0.0, 0.0, 1.0);

var primitives = scene.primitives;
primitives.add(rectanglePrimitive);
});

expect(scene).toRenderAndCall(function() {
scene.pickPosition(windowPosition);
expect(SceneTransforms.transformWindowToDrawingBuffer.calls.count()).toEqual(2);

scene.pickPosition(windowPosition);
expect(SceneTransforms.transformWindowToDrawingBuffer.calls.count()).toEqual(2);
SceneTransforms.transformWindowToDrawingBuffer = originaltransformWindowToDrawingBuffer;
});
});

it('pickPosition throws without windowPosition', function() {
expect(function() {
scene.pickPosition();
Expand Down