Skip to content

Commit

Permalink
Merge pull request #6781 from AnalyticalGraphicsInc/billboard-depth-vs
Browse files Browse the repository at this point in the history
Billboard depth testing performance improvements
  • Loading branch information
bagnell authored Jul 10, 2018
2 parents 7151a21 + ce49ae9 commit 69b3e9d
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 71 deletions.
1 change: 1 addition & 0 deletions Source/Scene/Billboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ define([

this._labelDimensions = undefined;
this._labelHorizontalOrigin = undefined;
this._labelTranslate = undefined;

var image = options.image;
var imageId = options.imageId;
Expand Down
70 changes: 58 additions & 12 deletions Source/Scene/BillboardCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define([
'../Core/WebGLConstants',
'../Renderer/Buffer',
'../Renderer/BufferUsage',
'../Renderer/ContextLimits',
'../Renderer/DrawCommand',
'../Renderer/Pass',
'../Renderer/RenderState',
Expand Down Expand Up @@ -52,6 +53,7 @@ define([
WebGLConstants,
Buffer,
BufferUsage,
ContextLimits,
DrawCommand,
Pass,
RenderState,
Expand Down Expand Up @@ -101,7 +103,7 @@ define([
scaleByDistance : 6,
pixelOffsetScaleByDistance : 7,
compressedAttribute3 : 8,
textureCoordinateBounds : 9,
textureCoordinateBoundsOrLabelTranslate : 9,
a_batchId : 10
};

Expand All @@ -116,7 +118,7 @@ define([
scaleByDistance : 7,
pixelOffsetScaleByDistance : 8,
compressedAttribute3 : 9,
textureCoordinateBounds : 10,
textureCoordinateBoundsOrLabelTranslate : 10,
a_batchId : 11
};

Expand Down Expand Up @@ -744,7 +746,7 @@ define([
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[DISTANCE_DISPLAY_CONDITION_INDEX]
}, {
index : attributeLocations.textureCoordinateBounds,
index : attributeLocations.textureCoordinateBoundsOrLabelTranslate,
componentsPerAttribute : 4,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[TEXTURE_COORDINATE_BOUNDS]
Expand Down Expand Up @@ -1249,13 +1251,35 @@ define([
}
}

function writeTextureCoordinateBounds(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard) {
function writeTextureCoordinateBoundsOrLabelTranslate(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard) {
if (billboard.heightReference === HeightReference.CLAMP_TO_GROUND) {
billboardCollection._shaderClampToGround = billboardCollection._scene.context.depthTexture;
}
var i;
var writer = vafWriters[attributeLocations.textureCoordinateBounds];
var writer = vafWriters[attributeLocations.textureCoordinateBoundsOrLabelTranslate];

if (ContextLimits.maximumVertexTextureImageUnits > 0) {
//write _labelTranslate, used by depth testing in the vertex shader
var translateX = 0;
var translateY = 0;
if (defined(billboard._labelTranslate)) {
translateX = billboard._labelTranslate.x;
translateY = billboard._labelTranslate.y;
}
if (billboardCollection._instanced) {
i = billboard._index;
writer(i, translateX, translateY, 0.0, 0.0);
} else {
i = billboard._index * 4;
writer(i + 0, translateX, translateY, 0.0, 0.0);
writer(i + 1, translateX, translateY, 0.0, 0.0);
writer(i + 2, translateX, translateY, 0.0, 0.0);
writer(i + 3, translateX, translateY, 0.0, 0.0);
}
return;
}

//write texture coordinate bounds, used by depth testing in fragment shader
var minX = 0;
var minY = 0;
var width = 0;
Expand Down Expand Up @@ -1320,7 +1344,7 @@ define([
writeScaleByDistance(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard);
writePixelOffsetScaleByDistance(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard);
writeCompressedAttribute3(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard);
writeTextureCoordinateBounds(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard);
writeTextureCoordinateBoundsOrLabelTranslate(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard);
writeBatchId(billboardCollection, context, textureAtlasCoordinates, vafWriters, billboard);
}

Expand Down Expand Up @@ -1521,7 +1545,7 @@ define([
}

if (properties[IMAGE_INDEX_INDEX] || properties[POSITION_INDEX]) {
writers.push(writeTextureCoordinateBounds);
writers.push(writeTextureCoordinateBoundsOrLabelTranslate);
}

var numWriters = writers.length;
Expand Down Expand Up @@ -1632,6 +1656,8 @@ define([
var fs;
var vertDefines;

var supportVSTextureReads = ContextLimits.maximumVertexTextureImageUnits > 0;

if (blendOptionChanged ||
(this._shaderRotation !== this._compiledShaderRotation) ||
(this._shaderAlignedAxis !== this._compiledShaderAlignedAxis) ||
Expand Down Expand Up @@ -1681,7 +1707,11 @@ define([
vs.defines.push('DISABLE_DEPTH_DISTANCE');
}
if (this._shaderClampToGround) {
vs.defines.push('CLAMP_TO_GROUND');
if (supportVSTextureReads) {
vs.defines.push('VERTEX_DEPTH_CHECK');
} else {
vs.defines.push('FRAGMENT_DEPTH_CHECK');
}
}

var vectorFragDefine = defined(this._batchTable) ? 'VECTOR_TILE' : '';
Expand All @@ -1692,7 +1722,11 @@ define([
sources : [fsSource]
});
if (this._shaderClampToGround) {
fs.defines.push('CLAMP_TO_GROUND');
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
}
this._sp = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1707,7 +1741,11 @@ define([
sources : [fsSource]
});
if (this._shaderClampToGround) {
fs.defines.push('CLAMP_TO_GROUND');
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
}
this._spTranslucent = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1724,7 +1762,11 @@ define([
sources : [fsSource]
});
if (this._shaderClampToGround) {
fs.defines.push('CLAMP_TO_GROUND');
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
}
this._sp = ShaderProgram.replaceCache({
context : context,
Expand All @@ -1741,7 +1783,11 @@ define([
sources : [fsSource]
});
if (this._shaderClampToGround) {
fs.defines.push('CLAMP_TO_GROUND');
if (supportVSTextureReads) {
fs.defines.push('VERTEX_DEPTH_CHECK');
} else {
fs.defines.push('FRAGMENT_DEPTH_CHECK');
}
}
this._spTranslucent = ShaderProgram.replaceCache({
context : context,
Expand Down
18 changes: 16 additions & 2 deletions Source/Scene/LabelCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ define([
'../Core/writeTextToCanvas',
'./BillboardCollection',
'./BlendOption',
'./HeightReference',
'./HorizontalOrigin',
'./Label',
'./LabelStyle',
Expand All @@ -29,6 +30,7 @@ define([
writeTextToCanvas,
BillboardCollection,
BlendOption,
HeightReference,
HorizontalOrigin,
Label,
LabelStyle,
Expand Down Expand Up @@ -250,6 +252,7 @@ define([
collection : labelCollection
});
billboard._labelDimensions = new Cartesian2();
billboard._labelTranslate = new Cartesian2();
}
glyph.billboard = billboard;
}
Expand Down Expand Up @@ -303,7 +306,7 @@ define([
var maxGlyphDescent = Number.NEGATIVE_INFINITY;
var maxGlyphY = 0;
var numberOfLines = 1;
var glyphIndex = 0;
var glyphIndex;
var glyphLength = glyphs.length;

var backgroundBillboard = label._backgroundBillboard;
Expand Down Expand Up @@ -384,7 +387,7 @@ define([
glyph.billboard._labelHorizontalOrigin = horizontalOrigin;
}

//Compute the next x offset taking into acocunt the kerning performed
//Compute the next x offset taking into account the kerning performed
//on both the current letter as well as the next letter to be drawn
//as well as any applied scale.
if (glyphIndex < glyphLength - 1) {
Expand Down Expand Up @@ -419,6 +422,17 @@ define([
backgroundBillboard.width = totalLineWidth;
backgroundBillboard.height = totalLineHeight;
backgroundBillboard._setTranslate(glyphPixelOffset);
backgroundBillboard._labelTranslate = Cartesian2.clone(glyphPixelOffset, backgroundBillboard._labelTranslate);
}

if (label.heightReference === HeightReference.CLAMP_TO_GROUND) {
for (glyphIndex = 0; glyphIndex < glyphLength; ++glyphIndex) {
glyph = glyphs[glyphIndex];
var billboard = glyph.billboard;
if (defined(billboard)) {
billboard._labelTranslate = Cartesian2.clone(glyphPixelOffset, billboard._labelTranslate);
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Shaders/BillboardCollectionFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ varying vec2 v_textureCoordinates;
varying vec4 v_pickColor;
varying vec4 v_color;

#ifdef CLAMP_TO_GROUND
#ifdef FRAGMENT_DEPTH_CHECK
varying vec4 v_textureCoordinateBounds; // the min and max x and y values for the texture coordinates
varying vec4 v_originTextureCoordinateAndTranslate; // texture coordinate at the origin, billboard translate (used for label glyphs)
varying vec4 v_dimensionsAndImageSize; // dimensions of the bounding rectangle and the size of the image. The values will only be different for label glyphs
Expand Down Expand Up @@ -79,8 +79,8 @@ void main()

czm_writeLogDepth();

#ifdef CLAMP_TO_GROUND
if (v_eyeDepthDistanceAndApplyTranslate.x > -v_eyeDepthDistanceAndApplyTranslate.y) {
#ifdef FRAGMENT_DEPTH_CHECK
if (v_eyeDepthDistanceAndApplyTranslate.y != 0.0) {
vec2 adjustedST = v_textureCoordinates - v_textureCoordinateBounds.xy;
adjustedST = adjustedST / vec2(v_textureCoordinateBounds.z - v_textureCoordinateBounds.x, v_textureCoordinateBounds.w - v_textureCoordinateBounds.y);

Expand Down
Loading

0 comments on commit 69b3e9d

Please sign in to comment.