Skip to content

Commit

Permalink
Use derived commands to enable log depth and disable when using an or…
Browse files Browse the repository at this point in the history
…thographic projection.
  • Loading branch information
bagnell committed Feb 16, 2018
1 parent 7c46e11 commit e331d4c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
3 changes: 0 additions & 3 deletions Source/Renderer/ShaderSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ define([
precision highp float;\n\
#else\n\
precision mediump float;\n\
#endif\n\n\
#ifdef GL_EXT_frag_depth\n\
#extension GL_EXT_frag_depth : enable\n\
#endif\n\n';
}

Expand Down
1 change: 1 addition & 0 deletions Source/Scene/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -3229,6 +3229,7 @@ define([
Cartesian3.clone(camera.right, result.right);
Matrix4.clone(camera._transform, result.transform);
result._transformChanged = true;
result.frustum = camera.frustum;

return result;
};
Expand Down
69 changes: 64 additions & 5 deletions Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ define([
this._cameraClone = Camera.clone(camera);
this._screenSpaceCameraController = new ScreenSpaceCameraController(this);
this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL);
this._frustumChanged = true;

// Keeps track of the state of a frame. FrameState is the state across
// the primitives of the scene. This state is for internally keeping track
Expand Down Expand Up @@ -1431,9 +1432,20 @@ define([
}

var derivedCommands = command.derivedCommands;
if (command.dirty && defined(derivedCommands)) {
if ((scene._frustumChanged || command.dirty) && defined(derivedCommands)) {
command.dirty = false;

var frustum = scene.camera.frustum;
var useLogDepth = scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum);
if (useLogDepth) {
derivedCommands.logDepth = createLogDepthCommand(command, context, derivedCommands.logDepth);
command = derivedCommands.logDepth.logDepthCommand;
}

if (scene.frameState.passes.pick) {
return;
}

if (shadowsEnabled && (command.receiveShadows || command.castShadows)) {
derivedCommands.shadows = ShadowMap.createDerivedCommands(shadowMaps, lightShadowMaps, command, shadowsDirty, context, derivedCommands.shadows);
}
Expand Down Expand Up @@ -1540,9 +1552,7 @@ define([
command.debugOverlappingFrustums = 0;
}

if (!scene.frameState.passes.pick) {
updateDerivedCommands(scene, command);
}
updateDerivedCommands(scene, command);

var frustumCommandsList = scene._frustumCommandsList;
var length = frustumCommandsList.length;
Expand Down Expand Up @@ -1871,6 +1881,8 @@ define([
command.derivedCommands.shadows.receiveCommand.execute(context, passState);
} else if (scene.frameState.passes.depth && defined(command.derivedCommands.depth)) {
command.derivedCommands.depth.depthOnlyCommand.execute(context, passState);
} else if (scene._logDepthBuffer && defined(command.derivedCommands.logDepth)) {
command.derivedCommands.logDepth.logDepthCommand.execute(context, passState);
} else {
command.execute(context, passState);
}
Expand Down Expand Up @@ -3067,8 +3079,9 @@ define([
tryAndCatchError(this, time, update);
this._postUpdate.raiseEvent(this, time);

this._frustumChanged = this._camera !== this._cameraClone.frustum;
var cameraChanged = checkForCameraUpdates(this);
var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || (this.mode === SceneMode.MORPHING);
var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || (this.mode === SceneMode.MORPHING);
if (!shouldRender && defined(this.maximumRenderTimeChange) && defined(this._lastRenderTime)) {
var difference = Math.abs(JulianDate.secondsDifference(this._lastRenderTime, time));
shouldRender = shouldRender || difference > this.maximumRenderTimeChange;
Expand Down Expand Up @@ -3370,6 +3383,52 @@ define([
return result;
}

function getLogDepthShaderProgram(context, shaderProgram) {
var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth');
if (!defined(shader)) {
var attributeLocations = shaderProgram._attributeLocations;
var fs = shaderProgram.fragmentShaderSource.clone();
fs.defines = defined(fs.defines) ? fs.defines.slice(0) : [];
fs.defines.push('LOG_DEPTH');

var extension =
'#ifdef GL_EXT_frag_depth \n' +
'#extension GL_EXT_frag_depth : enable \n' +
'#endif \n';
fs.sources.push(extension);

shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'logDepth', {
vertexShaderSource : shaderProgram.vertexShaderSource,
fragmentShaderSource : fs,
attributeLocations : attributeLocations
});
}

return shader;
}

function createLogDepthCommand(command, context, result) {
if (!defined(result)) {
result = {};
}

var shader;
if (defined(result.logDepthCommand)) {
shader = result.logDepthCommand.shaderProgram;
}

result.logDepthCommand = DrawCommand.shallowClone(command, result.logDepthCommand);

if (!defined(shader) || result.shaderProgramId !== command.shaderProgram.id) {
result.logDepthCommand.shaderProgram = getLogDepthShaderProgram(context, command.shaderProgram);
result.shaderProgramId = command.shaderProgram.id;
} else {
result.logDepthCommand.shaderProgram = shader;
}

return result;
}

function renderTranslucentDepthForPick(scene, drawingBufferPosition) {
// PERFORMANCE_IDEA: render translucent only and merge with the previous frame
var context = scene._context;
Expand Down
2 changes: 1 addition & 1 deletion Source/Shaders/Builtin/Functions/logDepth.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
void czm_logDepth(float w)
{
#ifdef GL_EXT_frag_depth
#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH)
gl_FragDepthEXT = -log(w * czm_currentFrustum.x) / log( czm_currentFrustum.y / czm_currentFrustum.x);
#endif
}

0 comments on commit e331d4c

Please sign in to comment.