All files / engine/Source/Renderer Sync.js

34.37% Statements 11/32
20% Branches 2/10
22.22% Functions 2/9
34.37% Lines 11/32

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                                      2x 2x     2x     2x 2x                     1x 2x                 1x             1x     1x                                                 1x                                          
import Check from "../Core/Check.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import Frozen from "../Core/Frozen.js";
import RuntimeError from "../Core/RuntimeError.js";
import WebGLConstants from "../Core/WebGLConstants.js";
 
/**
 * The WebGLSync interface is part of the WebGL 2 API and is used to synchronize activities between the GPU and the application.
 *
 * @param {object} options Object with the following properties:
 * @param {Context} context
 *
 * @exception {DeveloperError} A WebGL 2 context is required to use Sync operations.
 *
 * @private
 * @constructor
 */
function Sync(options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const context = options.context;
 
  //>>includeStart('debug', pragmas.debug);
  Check.defined("options.context", context);
  //>>includeEnd('debug');
 
  Eif (!context._webgl2) {
    throw new DeveloperError(
      "A WebGL 2 context is required to use Sync operations.",
    );
  }
 
  const gl = context._gl;
  const sync = gl.fenceSync(WebGLConstants.SYNC_GPU_COMMANDS_COMPLETE, 0);
 
  this._gl = gl;
  this._sync = sync;
}
Sync.create = function (options) {
  return new Sync(options);
};
/**
 * Query the sync status of this Sync object.
 *
 * @returns {number} Returns a WebGLConstants indicating the status of the sync object (WebGLConstants.SIGNALED or WebGLConstants.UNSIGNALED).
 *
 * @private
 */
Sync.prototype.getStatus = function () {
  const status = this._gl.getSyncParameter(
    this._sync,
    WebGLConstants.SYNC_STATUS,
  );
  return status;
};
Sync.prototype.isDestroyed = function () {
  return false;
};
Sync.prototype.destroy = function () {
  this._gl.deleteSync(this._sync);
  return destroyObject(this);
};
 
/**
 * Incremantally polls the status of the Sync object until signaled then resolves.
 * Usually polling should be done once per frame.
 *
 * @example
 * try {
 *  await sync.waitForSignal(function (next) {
 *    setTimeout(next, 100);
 *  });
 *} catch (e) {
 *  throw "Signal timeout";
 *} finally {
 *  sync.destroy();
 *}
 *
 * @param {function} scheduleFunction Function for scheduling the next poll. Receives a callback as its only parameter.
 * @param {number} [ttl=10] Max number of iterations to poll until timeout.
 *
 * @exception {RuntimeError} Wait for signal timeout.
 */
Sync.prototype.waitForSignal = async function (scheduleFunction, ttl) {
  const self = this;
  ttl = ttl ?? 10;
  function waitForSignal0(resolve, reject, ttl) {
    return () => {
      const syncStatus = self.getStatus();
      const signaled = syncStatus === WebGLConstants.SIGNALED;
      if (signaled) {
        resolve();
      } else if (ttl <= 0) {
        reject(new RuntimeError("Wait for signal timeout"));
      } else {
        scheduleFunction(waitForSignal0(resolve, reject, ttl - 1));
      }
    };
  }
  return new Promise((resolve, reject) => {
    scheduleFunction(waitForSignal0(resolve, reject, ttl));
  });
};
export default Sync;