All files / engine/Source/Scene TranslucentTileClassification.js

26.08% Statements 48/184
5.12% Branches 4/78
35% Functions 7/20
25.96% Lines 47/181

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515                                        1x                 1377x     1377x     1377x   1377x     1377x   1377x   1377x 1377x 1377x 1377x   1377x         1377x           1377x   1377x 1377x 1377x 1377x 1377x 1377x   1377x 1377x     1x                   12296x           1345x   1345x             1345x 1345x 1345x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 1x               1x 1x 1x   1x                   1x 1x                                                                     1x                                                                                 1x                                                                                             1x 2281x     1x       1x 1345x 1345x   1345x           1345x         1345x        
import BoundingRectangle from "../Core/BoundingRectangle.js";
import Color from "../Core/Color.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import PixelFormat from "../Core/PixelFormat.js";
import ClearCommand from "../Renderer/ClearCommand.js";
import DrawCommand from "../Renderer/DrawCommand.js";
import FramebufferManager from "../Renderer/FramebufferManager.js";
import Pass from "../Renderer/Pass.js";
import PixelDatatype from "../Renderer/PixelDatatype.js";
import RenderState from "../Renderer/RenderState.js";
import Sampler from "../Renderer/Sampler.js";
import ShaderSource from "../Renderer/ShaderSource.js";
import Texture from "../Renderer/Texture.js";
import CompareAndPackTranslucentDepth from "../Shaders/CompareAndPackTranslucentDepth.js";
import CompositeTranslucentClassification from "../Shaders/PostProcessStages/CompositeTranslucentClassification.js";
import BlendingState from "./BlendingState.js";
import StencilConstants from "./StencilConstants.js";
import StencilFunction from "./StencilFunction.js";
 
const debugShowPackedDepth = false;
 
/**
 * Handles buffers, drawing, and deriving commands needed for classifying translucent 3D Tiles.
 * Uses a depth texture, so classification on translucent 3D Tiles is not available in Internet Explorer.
 *
 * @private
 */
function TranslucentTileClassification(context) {
  this._drawClassificationFBO = new FramebufferManager({
    createDepthAttachments: false,
  });
  this._accumulationFBO = new FramebufferManager({
    createDepthAttachments: false,
  });
  this._packFBO = new FramebufferManager();
 
  this._opaqueDepthStencilTexture = undefined;
 
  // Reference to either colorTexture or accumulationTexture
  this._textureToComposite = undefined;
 
  this._translucentDepthStencilTexture = undefined;
 
  this._packDepthCommand = undefined;
  this._accumulateCommand = undefined;
  this._compositeCommand = undefined;
  this._copyCommand = undefined;
 
  this._clearColorCommand = new ClearCommand({
    color: new Color(0.0, 0.0, 0.0, 0.0),
    owner: this,
  });
 
  this._clearDepthStencilCommand = new ClearCommand({
    depth: 1.0,
    stencil: 0,
    owner: this,
  });
 
  this._supported = context.depthTexture;
 
  this._viewport = new BoundingRectangle();
  this._rsDepth = undefined;
  this._rsAccumulate = undefined;
  this._rsComp = undefined;
  this._useScissorTest = undefined;
  this._scissorRectangle = undefined;
 
  this._hasTranslucentDepth = false;
  this._frustumsDrawn = 0;
}
 
Object.defineProperties(TranslucentTileClassification.prototype, {
  /**
   * Gets whether or not translucent depth was rendered.
   * @memberof TranslucentTileClassification.prototype
   *
   * @type {boolean}
   * @readonly
   */
  hasTranslucentDepth: {
    get: function () {
      return this._hasTranslucentDepth;
    },
  },
});
 
function destroyTextures(transpClass) {
  transpClass._textureToComposite = undefined;
 
  transpClass._translucentDepthStencilTexture =
    transpClass._translucentDepthStencilTexture &&
    !transpClass._translucentDepthStencilTexture.isDestroyed() &&
    transpClass._translucentDepthStencilTexture.destroy();
}
 
function destroyFramebuffers(transpClass) {
  transpClass._drawClassificationFBO.destroy();
  transpClass._accumulationFBO.destroy();
  transpClass._packFBO.destroy();
}
 
function updateTextures(transpClass, context, width, height) {
  destroyTextures(transpClass);
 
  transpClass._translucentDepthStencilTexture = new Texture({
    context: context,
    width: width,
    height: height,
    pixelFormat: PixelFormat.DEPTH_STENCIL,
    pixelDatatype: PixelDatatype.UNSIGNED_INT_24_8,
    sampler: Sampler.NEAREST,
  });
}
 
function updateFramebuffers(transpClass, context, width, height) {
  destroyFramebuffers(transpClass);
  transpClass._drawClassificationFBO.setDepthStencilTexture(
    transpClass._translucentDepthStencilTexture,
  );
  transpClass._drawClassificationFBO.update(context, width, height);
 
  transpClass._accumulationFBO.setDepthStencilTexture(
    transpClass._translucentDepthStencilTexture,
  );
  transpClass._accumulationFBO.update(context, width, height);
 
  transpClass._packFBO.update(context, width, height);
}
 
function updateResources(
  transpClass,
  context,
  passState,
  globeDepthStencilTexture,
) {
  if (!transpClass.isSupported()) {
    return;
  }
 
  transpClass._opaqueDepthStencilTexture = globeDepthStencilTexture;
 
  const width = transpClass._opaqueDepthStencilTexture.width;
  const height = transpClass._opaqueDepthStencilTexture.height;
  if (transpClass._drawClassificationFBO.isDirty(width, height)) {
    updateTextures(transpClass, context, width, height);
    updateFramebuffers(transpClass, context, width, height);
  }
 
  let fs;
  let uniformMap;
 
  if (!defined(transpClass._packDepthCommand)) {
    fs = new ShaderSource({
      sources: [CompareAndPackTranslucentDepth],
    });
 
    uniformMap = {
      u_opaqueDepthTexture: function () {
        return transpClass._opaqueDepthStencilTexture;
      },
      u_translucentDepthTexture: function () {
        return transpClass._translucentDepthStencilTexture;
      },
    };
 
    transpClass._packDepthCommand = context.createViewportQuadCommand(fs, {
      uniformMap: uniformMap,
      owner: transpClass,
    });
  }
 
  if (!defined(transpClass._compositeCommand)) {
    fs = new ShaderSource({
      sources: [CompositeTranslucentClassification],
    });
 
    uniformMap = {
      colorTexture: function () {
        return transpClass._textureToComposite;
      },
    };
 
    if (debugShowPackedDepth) {
      fs.defines = ["DEBUG_SHOW_DEPTH"];
      uniformMap.u_packedTranslucentDepth = function () {
        return transpClass._packFBO.getColorTexture();
      };
    }
 
    transpClass._compositeCommand = context.createViewportQuadCommand(fs, {
      uniformMap: uniformMap,
      owner: transpClass,
    });
 
    const compositeCommand = transpClass._compositeCommand;
    const compositeProgram = compositeCommand.shaderProgram;
    const compositePickProgram = context.shaderCache.createDerivedShaderProgram(
      compositeProgram,
      "pick",
      {
        vertexShaderSource: compositeProgram.vertexShaderSource,
        fragmentShaderSource: new ShaderSource({
          sources: fs.sources,
          defines: ["PICK"],
        }),
        attributeLocations: compositeProgram._attributeLocations,
      },
    );
    const compositePickCommand = DrawCommand.shallowClone(compositeCommand);
    compositePickCommand.shaderProgram = compositePickProgram;
    compositeCommand.derivedCommands.pick = compositePickCommand;
  }
 
  if (!defined(transpClass._copyCommand)) {
    fs = new ShaderSource({
      sources: [CompositeTranslucentClassification],
    });
 
    uniformMap = {
      colorTexture: function () {
        return transpClass._drawClassificationFBO.getColorTexture();
      },
    };
 
    transpClass._copyCommand = context.createViewportQuadCommand(fs, {
      uniformMap: uniformMap,
      owner: transpClass,
    });
  }
 
  if (!defined(transpClass._accumulateCommand)) {
    fs = new ShaderSource({
      sources: [CompositeTranslucentClassification],
    });
 
    uniformMap = {
      colorTexture: function () {
        return transpClass._drawClassificationFBO.getColorTexture();
      },
    };
 
    transpClass._accumulateCommand = context.createViewportQuadCommand(fs, {
      uniformMap: uniformMap,
      owner: transpClass,
    });
  }
 
  transpClass._viewport.width = width;
  transpClass._viewport.height = height;
 
  const useScissorTest = !BoundingRectangle.equals(
    transpClass._viewport,
    passState.viewport,
  );
  let updateScissor = useScissorTest !== transpClass._useScissorTest;
  transpClass._useScissorTest = useScissorTest;
 
  if (
    !BoundingRectangle.equals(transpClass._scissorRectangle, passState.viewport)
  ) {
    transpClass._scissorRectangle = BoundingRectangle.clone(
      passState.viewport,
      transpClass._scissorRectangle,
    );
    updateScissor = true;
  }
 
  if (
    !defined(transpClass._rsDepth) ||
    !BoundingRectangle.equals(
      transpClass._viewport,
      transpClass._rsDepth.viewport,
    ) ||
    updateScissor
  ) {
    transpClass._rsDepth = RenderState.fromCache({
      viewport: transpClass._viewport,
      scissorTest: {
        enabled: transpClass._useScissorTest,
        rectangle: transpClass._scissorRectangle,
      },
    });
  }
 
  if (defined(transpClass._packDepthCommand)) {
    transpClass._packDepthCommand.renderState = transpClass._rsDepth;
  }
 
  if (
    !defined(transpClass._rsAccumulate) ||
    !BoundingRectangle.equals(
      transpClass._viewport,
      transpClass._rsAccumulate.viewport,
    ) ||
    updateScissor
  ) {
    transpClass._rsAccumulate = RenderState.fromCache({
      viewport: transpClass._viewport,
      scissorTest: {
        enabled: transpClass._useScissorTest,
        rectangle: transpClass._scissorRectangle,
      },
      stencilTest: {
        enabled: true,
        frontFunction: StencilFunction.EQUAL,
        reference: StencilConstants.CESIUM_3D_TILE_MASK,
      },
    });
  }
 
  if (defined(transpClass._accumulateCommand)) {
    transpClass._accumulateCommand.renderState = transpClass._rsAccumulate;
  }
 
  if (
    !defined(transpClass._rsComp) ||
    !BoundingRectangle.equals(
      transpClass._viewport,
      transpClass._rsComp.viewport,
    ) ||
    updateScissor
  ) {
    transpClass._rsComp = RenderState.fromCache({
      viewport: transpClass._viewport,
      scissorTest: {
        enabled: transpClass._useScissorTest,
        rectangle: transpClass._scissorRectangle,
      },
      blending: BlendingState.ALPHA_BLEND,
    });
  }
 
  if (defined(transpClass._compositeCommand)) {
    transpClass._compositeCommand.renderState = transpClass._rsComp;
    transpClass._compositeCommand.derivedCommands.pick.renderState =
      transpClass._rsComp;
  }
}
 
TranslucentTileClassification.prototype.executeTranslucentCommands = function (
  scene,
  executeCommand,
  passState,
  commands,
  globeDepthStencilTexture,
) {
  // Check for translucent commands that should be classified
  const useLogDepth = scene.frameState.useLogDepth;
  const context = scene.context;
  const framebuffer = passState.framebuffer;
 
  for (let i = 0; i < commands.length; ++i) {
    let command = commands[i];
    command = useLogDepth ? command.derivedCommands.logDepth.command : command;
 
    if (command.depthForTranslucentClassification) {
      this._hasTranslucentDepth = true;
      break;
    }
  }
 
  Eif (!this._hasTranslucentDepth) {
    return;
  }
 
  updateResources(this, context, passState, globeDepthStencilTexture);
 
  // Get translucent depth
  passState.framebuffer = this._drawClassificationFBO.framebuffer;
 
  // Clear depth for multifrustum
  this._clearDepthStencilCommand.execute(context, passState);
 
  for (let i = 0; i < commands.length; ++i) {
    let command = commands[i];
    command = useLogDepth ? command.derivedCommands.logDepth.command : command;
 
    if (!command.depthForTranslucentClassification) {
      continue;
    }
 
    // Depth-only commands are created for all translucent 3D Tiles commands
    const depthOnlyCommand = command.derivedCommands.depth.depthOnlyCommand;
    executeCommand(depthOnlyCommand, scene, passState);
  }
 
  this._frustumsDrawn += this._hasTranslucentDepth ? 1 : 0;
 
  // Pack depth if any translucent depth commands were performed
  if (this._hasTranslucentDepth) {
    passState.framebuffer = this._packFBO.framebuffer;
    this._packDepthCommand.execute(context, passState);
  }
 
  passState.framebuffer = framebuffer;
};
 
TranslucentTileClassification.prototype.executeClassificationCommands =
  function (scene, executeCommand, passState, frustumCommands) {
    if (!this._hasTranslucentDepth) {
      return;
    }
 
    const context = scene.context;
    const uniformState = context.uniformState;
    const framebuffer = passState.framebuffer;
 
    passState.framebuffer = this._accumulationFBO.framebuffer;
    this._accumulateCommand.execute(context, passState);
 
    passState.framebuffer = this._drawClassificationFBO.framebuffer;
    if (this._frustumsDrawn > 1) {
      this._clearColorCommand.execute(context, passState);
    }
 
    uniformState.updatePass(Pass.CESIUM_3D_TILE_CLASSIFICATION);
    const swapGlobeDepth = uniformState.globeDepthTexture;
    uniformState.globeDepthTexture = this._packFBO.getColorTexture();
    const commands =
      frustumCommands.commands[Pass.CESIUM_3D_TILE_CLASSIFICATION];
    const length = frustumCommands.indices[Pass.CESIUM_3D_TILE_CLASSIFICATION];
    for (let i = 0; i < length; ++i) {
      executeCommand(commands[i], scene, passState);
    }
 
    uniformState.globeDepthTexture = swapGlobeDepth;
    passState.framebuffer = framebuffer;
 
    if (this._frustumsDrawn === 1) {
      return;
    }
 
    passState.framebuffer = this._accumulationFBO.framebuffer;
    this._accumulateCommand.execute(context, passState);
 
    passState.framebuffer = framebuffer;
  };
 
TranslucentTileClassification.prototype.execute = function (scene, passState) {
  if (!this._hasTranslucentDepth) {
    return;
  }
  if (this._frustumsDrawn === 1) {
    this._textureToComposite = this._drawClassificationFBO.getColorTexture();
  } else {
    this._textureToComposite = this._accumulationFBO.getColorTexture();
  }
 
  const command = scene.frameState.passes.pick
    ? this._compositeCommand.derivedCommands.pick
    : this._compositeCommand;
  command.execute(scene.context, passState);
 
  clear(this, scene, passState);
};
 
function clear(translucentTileClassification, scene, passState) {
  if (!translucentTileClassification._hasTranslucentDepth) {
    return;
  }
 
  const framebuffer = passState.framebuffer;
 
  passState.framebuffer =
    translucentTileClassification._drawClassificationFBO.framebuffer;
  translucentTileClassification._clearColorCommand.execute(
    scene._context,
    passState,
  );
 
  passState.framebuffer = framebuffer;
 
  if (translucentTileClassification._frustumsDrawn > 1) {
    passState.framebuffer =
      translucentTileClassification._accumulationFBO.framebuffer;
    translucentTileClassification._clearColorCommand.execute(
      scene._context,
      passState,
    );
  }
 
  translucentTileClassification._hasTranslucentDepth = false;
  translucentTileClassification._frustumsDrawn = 0;
}
 
TranslucentTileClassification.prototype.isSupported = function () {
  return this._supported;
};
 
TranslucentTileClassification.prototype.isDestroyed = function () {
  return false;
};
 
TranslucentTileClassification.prototype.destroy = function () {
  destroyTextures(this);
  destroyFramebuffers(this);
 
  Iif (defined(this._compositeCommand)) {
    this._compositeCommand.shaderProgram =
      this._compositeCommand.shaderProgram &&
      this._compositeCommand.shaderProgram.destroy();
  }
 
  Iif (defined(this._packDepthCommand)) {
    this._packDepthCommand.shaderProgram =
      this._packDepthCommand.shaderProgram &&
      this._packDepthCommand.shaderProgram.destroy();
  }
  return destroyObject(this);
};
 
export default TranslucentTileClassification;