All files / engine/Source/Core Tipsify.js

99.28% Statements 139/140
94.82% Branches 55/58
100% Functions 4/4
99.26% Lines 135/136

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                                    1x                                           1x 10x 10x 10x 10x     10x 1x       9x     9x 1x   8x 1x   7x 1x         6x 3x 3x 3x 3x 49170x 4418x   49170x 49170x         6x 6x 8914x       6x 6x 49410x 13084x 13084x       6x                                             1x 9x 9x 9x 9x         22x   24909x 24909x   24909x 17x       5x 4504x 1x 1x   4503x   4x                       3703x   3703x 3703x 3703x 24909x 24909x 15987x 15987x           15671x   15987x 7219x 7219x     24909x   3703x 22x   3681x       9x 2x       7x     7x 1x   6x 1x   5x 1x         4x 4x 4x 4x 4x 3x   1x 111x 23x   111x 111x   1x     1x       4x   4x 4508x           4x 4x 4x 8303x 8303x 8303x 8303x 8303x 8303x 8303x 8303x       4x     4x 4x     4x 4x     4x 4x 4x 4x 4x 8303x       4x 3703x 3703x 3703x 3703x 21997x 21997x 8303x 8303x 8303x   24909x 24909x 24909x     24909x 24909x     24909x 24909x 24909x 4779x 4779x   24909x       3703x                     4x      
import Frozen from "./Frozen.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
 
/**
 * Encapsulates an algorithm to optimize triangles for the post
 * vertex-shader cache.  This is based on the 2007 SIGGRAPH paper
 * 'Fast Triangle Reordering for Vertex Locality and Reduced Overdraw.'
 * The runtime is linear but several passes are made.
 *
 * @namespace Tipsify
 *
 * @see <a href='http://gfx.cs.princeton.edu/pubs/Sander_2007_%3ETR/tipsy.pdf'>
 * Fast Triangle Reordering for Vertex Locality and Reduced Overdraw</a>
 * by Sander, Nehab, and Barczak
 *
 * @private
 */
const Tipsify = {};
 
/**
 * Calculates the average cache miss ratio (ACMR) for a given set of indices.
 *
 * @param {object} options Object with the following properties:
 * @param {number[]} options.indices Lists triads of numbers corresponding to the indices of the vertices
 *                        in the vertex buffer that define the geometry's triangles.
 * @param {number} [options.maximumIndex] The maximum value of the elements in <code>args.indices</code>.
 *                                     If not supplied, this value will be computed.
 * @param {number} [options.cacheSize=24] The number of vertices that can be stored in the cache at any one time.
 * @returns {number} The average cache miss ratio (ACMR).
 *
 * @exception {DeveloperError} indices length must be a multiple of three.
 * @exception {DeveloperError} cacheSize must be greater than two.
 *
 * @example
 * const indices = [0, 1, 2, 3, 4, 5];
 * const maxIndex = 5;
 * const cacheSize = 3;
 * const acmr = Cesium.Tipsify.calculateACMR({indices : indices, maxIndex : maxIndex, cacheSize : cacheSize});
 */
Tipsify.calculateACMR = function (options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const indices = options.indices;
  let maximumIndex = options.maximumIndex;
  const cacheSize = options.cacheSize ?? 24;
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(indices)) {
    throw new DeveloperError("indices is required.");
  }
  //>>includeEnd('debug');
 
  const numIndices = indices.length;
 
  //>>includeStart('debug', pragmas.debug);
  if (numIndices < 3 || numIndices % 3 !== 0) {
    throw new DeveloperError("indices length must be a multiple of three.");
  }
  if (maximumIndex <= 0) {
    throw new DeveloperError("maximumIndex must be greater than zero.");
  }
  if (cacheSize < 3) {
    throw new DeveloperError("cacheSize must be greater than two.");
  }
  //>>includeEnd('debug');
 
  // Compute the maximumIndex if not given
  if (!defined(maximumIndex)) {
    maximumIndex = 0;
    let currentIndex = 0;
    let intoIndices = indices[currentIndex];
    while (currentIndex < numIndices) {
      if (intoIndices > maximumIndex) {
        maximumIndex = intoIndices;
      }
      ++currentIndex;
      intoIndices = indices[currentIndex];
    }
  }
 
  // Vertex time stamps
  const vertexTimeStamps = [];
  for (let i = 0; i < maximumIndex + 1; i++) {
    vertexTimeStamps[i] = 0;
  }
 
  // Cache processing
  let s = cacheSize + 1;
  for (let j = 0; j < numIndices; ++j) {
    if (s - vertexTimeStamps[indices[j]] > cacheSize) {
      vertexTimeStamps[indices[j]] = s;
      ++s;
    }
  }
 
  return (s - cacheSize + 1) / (numIndices / 3);
};
 
/**
 * Optimizes triangles for the post-vertex shader cache.
 *
 * @param {object} options Object with the following properties:
 * @param {number[]} options.indices Lists triads of numbers corresponding to the indices of the vertices
 *                        in the vertex buffer that define the geometry's triangles.
 * @param {number} [options.maximumIndex] The maximum value of the elements in <code>args.indices</code>.
 *                                     If not supplied, this value will be computed.
 * @param {number} [options.cacheSize=24] The number of vertices that can be stored in the cache at any one time.
 * @returns {number[]} A list of the input indices in an optimized order.
 *
 * @exception {DeveloperError} indices length must be a multiple of three.
 * @exception {DeveloperError} cacheSize must be greater than two.
 *
 * @example
 * const indices = [0, 1, 2, 3, 4, 5];
 * const maxIndex = 5;
 * const cacheSize = 3;
 * const reorderedIndices = Cesium.Tipsify.tipsify({indices : indices, maxIndex : maxIndex, cacheSize : cacheSize});
 */
Tipsify.tipsify = function (options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const indices = options.indices;
  const maximumIndex = options.maximumIndex;
  const cacheSize = options.cacheSize ?? 24;
 
  let cursor;
 
  function skipDeadEnd(vertices, deadEnd, indices, maximumIndexPlusOne) {
    while (deadEnd.length >= 1) {
      // while the stack is not empty
      const d = deadEnd[deadEnd.length - 1]; // top of the stack
      deadEnd.splice(deadEnd.length - 1, 1); // pop the stack
 
      if (vertices[d].numLiveTriangles > 0) {
        return d;
      }
    }
 
    while (cursor < maximumIndexPlusOne) {
      if (vertices[cursor].numLiveTriangles > 0) {
        ++cursor;
        return cursor - 1;
      }
      ++cursor;
    }
    return -1;
  }
 
  function getNextVertex(
    indices,
    cacheSize,
    oneRing,
    vertices,
    s,
    deadEnd,
    maximumIndexPlusOne,
  ) {
    let n = -1;
    let p;
    let m = -1;
    let itOneRing = 0;
    while (itOneRing < oneRing.length) {
      const index = oneRing[itOneRing];
      if (vertices[index].numLiveTriangles) {
        p = 0;
        if (
          s -
            vertices[index].timeStamp +
            2 * vertices[index].numLiveTriangles <=
          cacheSize
        ) {
          p = s - vertices[index].timeStamp;
        }
        if (p > m || m === -1) {
          m = p;
          n = index;
        }
      }
      ++itOneRing;
    }
    if (n === -1) {
      return skipDeadEnd(vertices, deadEnd, indices, maximumIndexPlusOne);
    }
    return n;
  }
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(indices)) {
    throw new DeveloperError("indices is required.");
  }
  //>>includeEnd('debug');
 
  const numIndices = indices.length;
 
  //>>includeStart('debug', pragmas.debug);
  if (numIndices < 3 || numIndices % 3 !== 0) {
    throw new DeveloperError("indices length must be a multiple of three.");
  }
  if (maximumIndex <= 0) {
    throw new DeveloperError("maximumIndex must be greater than zero.");
  }
  if (cacheSize < 3) {
    throw new DeveloperError("cacheSize must be greater than two.");
  }
  //>>includeEnd('debug');
 
  // Determine maximum index
  let maximumIndexPlusOne = 0;
  let currentIndex = 0;
  let intoIndices = indices[currentIndex];
  const endIndex = numIndices;
  if (defined(maximumIndex)) {
    maximumIndexPlusOne = maximumIndex + 1;
  } else {
    while (currentIndex < endIndex) {
      if (intoIndices > maximumIndexPlusOne) {
        maximumIndexPlusOne = intoIndices;
      }
      ++currentIndex;
      intoIndices = indices[currentIndex];
    }
    Iif (maximumIndexPlusOne === -1) {
      return 0;
    }
    ++maximumIndexPlusOne;
  }
 
  // Vertices
  const vertices = [];
  let i;
  for (i = 0; i < maximumIndexPlusOne; i++) {
    vertices[i] = {
      numLiveTriangles: 0,
      timeStamp: 0,
      vertexTriangles: [],
    };
  }
  currentIndex = 0;
  let triangle = 0;
  while (currentIndex < endIndex) {
    vertices[indices[currentIndex]].vertexTriangles.push(triangle);
    ++vertices[indices[currentIndex]].numLiveTriangles;
    vertices[indices[currentIndex + 1]].vertexTriangles.push(triangle);
    ++vertices[indices[currentIndex + 1]].numLiveTriangles;
    vertices[indices[currentIndex + 2]].vertexTriangles.push(triangle);
    ++vertices[indices[currentIndex + 2]].numLiveTriangles;
    ++triangle;
    currentIndex += 3;
  }
 
  // Starting index
  let f = 0;
 
  // Time Stamp
  let s = cacheSize + 1;
  cursor = 1;
 
  // Process
  let oneRing = [];
  const deadEnd = []; //Stack
  let vertex;
  let intoVertices;
  let currentOutputIndex = 0;
  const outputIndices = [];
  const numTriangles = numIndices / 3;
  const triangleEmitted = [];
  for (i = 0; i < numTriangles; i++) {
    triangleEmitted[i] = false;
  }
  let index;
  let limit;
  while (f !== -1) {
    oneRing = [];
    intoVertices = vertices[f];
    limit = intoVertices.vertexTriangles.length;
    for (let k = 0; k < limit; ++k) {
      triangle = intoVertices.vertexTriangles[k];
      if (!triangleEmitted[triangle]) {
        triangleEmitted[triangle] = true;
        currentIndex = triangle + triangle + triangle;
        for (let j = 0; j < 3; ++j) {
          // Set this index as a possible next index
          index = indices[currentIndex];
          oneRing.push(index);
          deadEnd.push(index);
 
          // Output index
          outputIndices[currentOutputIndex] = index;
          ++currentOutputIndex;
 
          // Cache processing
          vertex = vertices[index];
          --vertex.numLiveTriangles;
          if (s - vertex.timeStamp > cacheSize) {
            vertex.timeStamp = s;
            ++s;
          }
          ++currentIndex;
        }
      }
    }
    f = getNextVertex(
      indices,
      cacheSize,
      oneRing,
      vertices,
      s,
      deadEnd,
      maximumIndexPlusOne,
    );
  }
 
  return outputIndices;
};
export default Tipsify;