Skip to content
Merged
Changes from all 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
108 changes: 46 additions & 62 deletions packages/engine/Source/Scene/GlobeSurfaceShaderSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import SceneMode from "./SceneMode.js";
function GlobeSurfaceShader(
numberOfDayTextures,
flags,
flags2,
material,
shaderProgram,
clippingShaderState,
clippingPolygonShaderState
clippingPolygonShaderState,
) {
this.numberOfDayTextures = numberOfDayTextures;
this.flags = flags;
this.flags2 = flags2;
this.material = material;
this.shaderProgram = shaderProgram;
this.clippingShaderState = clippingShaderState;
Expand Down Expand Up @@ -168,40 +166,40 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
// Flags beyond bit 31 use arithmetic to avoid silent wrap-around
// (x << 32 === x << 0 in JavaScript).
const flags =
sceneMode |
(applyBrightness << 2) |
(applyContrast << 3) |
(applyHue << 4) |
(applySaturation << 5) |
(applyGamma << 6) |
(applyAlpha << 7) |
(hasWaterMask << 8) |
(showReflectiveOcean << 9) |
(showOceanWaves << 10) |
(enableLighting << 11) |
(dynamicAtmosphereLighting << 12) |
(dynamicAtmosphereLightingFromSun << 13) |
(showGroundAtmosphere << 14) |
(perFragmentGroundAtmosphere << 15) |
(hasVertexNormals << 16) |
(useWebMercatorProjection << 17) |
(enableFog << 18) |
(quantization << 19) |
(applySplit << 20) |
(enableClippingPlanes << 21) |
(enableClippingPolygons << 22) |
(cartographicLimitRectangleFlag << 23) |
(imageryCutoutFlag << 24) |
(colorCorrect << 25) |
(highlightFillTile << 26) |
(colorToAlpha << 27) |
(hasGeodeticSurfaceNormals << 28) |
(hasExaggeration << 29) |
(showUndergroundColor << 30) |
(translucent << 31);

// More bit flags that don't fit in the first `flag`
const flags2 = applyDayNightAlpha | (splitTerrain << 2);
((sceneMode |
(applyBrightness << 2) |
(applyContrast << 3) |
(applyHue << 4) |
(applySaturation << 5) |
(applyGamma << 6) |
(applyAlpha << 7) |
(hasWaterMask << 8) |
(showReflectiveOcean << 9) |
(showOceanWaves << 10) |
(enableLighting << 11) |
(dynamicAtmosphereLighting << 12) |
(dynamicAtmosphereLightingFromSun << 13) |
(showGroundAtmosphere << 14) |
(perFragmentGroundAtmosphere << 15) |
(hasVertexNormals << 16) |
(useWebMercatorProjection << 17) |
(enableFog << 18) |
(quantization << 19) |
(applySplit << 20) |
(enableClippingPlanes << 21) |
(enableClippingPolygons << 22) |
(cartographicLimitRectangleFlag << 23) |
(imageryCutoutFlag << 24) |
(colorCorrect << 25) |
(highlightFillTile << 26) |
(colorToAlpha << 27) |
(hasGeodeticSurfaceNormals << 28) |
(hasExaggeration << 29) |
(showUndergroundColor << 30) |
(translucent << 31)) >>>
0) +
(applyDayNightAlpha ? 0x100000000 : 0) +
(splitTerrain ? 0x1000000000 : 0);

let currentClippingShaderState = 0;
if (defined(clippingPlanes) && clippingPlanes.length > 0) {
Expand All @@ -222,7 +220,6 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
defined(surfaceShader) &&
surfaceShader.numberOfDayTextures === numberOfDayTextures &&
surfaceShader.flags === flags &&
surfaceShader.flags2 === flags2 &&
surfaceShader.material === this.material &&
surfaceShader.clippingShaderState === currentClippingShaderState &&
surfaceShader.clippingPolygonShaderState ===
Expand All @@ -237,12 +234,7 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
shadersByFlags = this._shadersByTexturesFlags[numberOfDayTextures] = [];
}

let shadersByFlags2 = shadersByFlags[flags];
if (!defined(shadersByFlags2)) {
shadersByFlags2 = shadersByFlags[flags2] = [];
}

surfaceShader = shadersByFlags2[flags2];
surfaceShader = shadersByFlags[flags];
if (
!defined(surfaceShader) ||
surfaceShader.material !== this.material ||
Expand All @@ -257,7 +249,7 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
// Need to go before GlobeFS
if (currentClippingShaderState !== 0) {
fs.sources.unshift(
getClippingFunction(clippingPlanes, frameState.context)
getClippingFunction(clippingPlanes, frameState.context),
);
}

Expand All @@ -271,7 +263,7 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
fs.defines.push(
`TEXTURE_UNITS ${numberOfDayTextures}`,
cartographicLimitRectangleDefine,
imageryCutoutDefine
imageryCutoutDefine,
);

if (applyBrightness) {
Expand Down Expand Up @@ -369,10 +361,10 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
}

fs.defines.push(
`CLIPPING_POLYGON_REGIONS_LENGTH ${clippingPolygons.extentsCount}`
`CLIPPING_POLYGON_REGIONS_LENGTH ${clippingPolygons.extentsCount}`,
);
vs.defines.push(
`CLIPPING_POLYGON_REGIONS_LENGTH ${clippingPolygons.extentsCount}`
`CLIPPING_POLYGON_REGIONS_LENGTH ${clippingPolygons.extentsCount}`,
);
}

Expand Down Expand Up @@ -461,25 +453,22 @@ GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
attributeLocations: terrainEncoding.getAttributeLocations(),
});

surfaceShader = new GlobeSurfaceShader(
surfaceShader = shadersByFlags[flags] = new GlobeSurfaceShader(
numberOfDayTextures,
flags,
flags2,
this.material,
shader,
currentClippingShaderState,
currentClippingPolygonsShaderState
currentClippingPolygonsShaderState,
);

shadersByFlags2[flags2] = surfaceShader;
}

surfaceTile.surfaceShader = surfaceShader;
return surfaceShader.shaderProgram;
};

GlobeSurfaceShaderSet.prototype.destroy = function () {
let flags, flags2;
let flags;
let shader;

const shadersByTexturesFlags = this._shadersByTexturesFlags;
Expand All @@ -492,14 +481,9 @@ GlobeSurfaceShaderSet.prototype.destroy = function () {

for (flags in shadersByFlags) {
if (shadersByFlags.hasOwnProperty(flags)) {
const shadersByFlags2 = shadersByFlags[flags];
for (flags2 in shadersByFlags2) {
if (shadersByFlags2.hasOwnProperty(flags2)) {
shader = shadersByFlags2[flags2];
if (defined(shader)) {
shader.shaderProgram.destroy();
}
}
shader = shadersByFlags[flags];
if (defined(shader)) {
shader.shaderProgram.destroy();
}
}
}
Expand Down
Loading