All files / engine/Source/Scene GaussianSplatSorter.js

85.71% Statements 18/21
75% Branches 6/8
60% Functions 3/5
85.71% Lines 18/21

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                        1x         1x 1x 1x 1x 9x 1x       1x         1x 1x                   1x     9x                       1x 9x 9x       9x 4x     5x            
import defined from "../Core/defined.js";
import FeatureDetection from "../Core/FeatureDetection.js";
import RuntimeError from "../Core/RuntimeError.js";
import TaskProcessor from "../Core/TaskProcessor.js";
 
/** * A sorter for Gaussian splats that uses a task processor to handle sorting in parallel.
 * This class is responsible for initializing the task processor and scheduling sorting tasks.
 * * @constructor
 * @private
 */
function GaussianSplatSorter() {}
 
GaussianSplatSorter._maxSortingConcurrency = Math.max(
  FeatureDetection.hardwareConcurrency - 1,
  1,
);
 
GaussianSplatSorter._sorterTaskProcessor = undefined;
GaussianSplatSorter._taskProcessorReady = false;
GaussianSplatSorter._error = undefined;
GaussianSplatSorter._getSorterTaskProcessor = function () {
  if (!defined(GaussianSplatSorter._sorterTaskProcessor)) {
    const processor = new TaskProcessor(
      "gaussianSplatSorter",
      GaussianSplatSorter._maxSortingConcurrency,
    );
    processor
      .initWebAssemblyModule({
        wasmBinaryFile: "ThirdParty/wasm_splats_bg.wasm",
      })
      .then(function (result) {
        if (result) {
          GaussianSplatSorter._taskProcessorReady = true;
        } else E{
          GaussianSplatSorter._error = new RuntimeError(
            "Gaussian splat sorter could not be initialized.",
          );
        }
      })
      .catch((error) => {
        GaussianSplatSorter._error = error;
      });
    GaussianSplatSorter._sorterTaskProcessor = processor;
  }
 
  return GaussianSplatSorter._sorterTaskProcessor;
};
/**
 * Sorts Gaussian splats using a radix sort algorithm. Sorted by distance from the camera.
 * A new list of indexes is returned, which can be used to render the splats in the correct order.
 *
 * @param {object} parameters - The parameters for sorting Gaussian splat indexes.
 * @param {object} parameters.primitive - The primitive containing positions and modelView matrices.
 * @returns {Promise|undefined} A promise that resolves to the sorted indexes or undefined if the task cannot be scheduled.
 * @exception {RuntimeError} Sorter could not be initialized.
 * @private
 */
GaussianSplatSorter.radixSortIndexes = function (parameters) {
  const sorterTaskProcessor = GaussianSplatSorter._getSorterTaskProcessor();
  Iif (defined(GaussianSplatSorter._error)) {
    throw GaussianSplatSorter._error;
  }
 
  if (!GaussianSplatSorter._taskProcessorReady) {
    return;
  }
 
  return sorterTaskProcessor.scheduleTask(parameters, [
    parameters.primitive.positions.buffer,
  ]);
};
 
export default GaussianSplatSorter;