智慧水务管理系统 - 精河县供水工程综合管理平台

Sync.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Check from "../Core/Check.js";
  2. import destroyObject from "../Core/destroyObject.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Frozen from "../Core/Frozen.js";
  5. import RuntimeError from "../Core/RuntimeError.js";
  6. import WebGLConstants from "../Core/WebGLConstants.js";
  7. /**
  8. * The WebGLSync interface is part of the WebGL 2 API and is used to synchronize activities between the GPU and the application.
  9. *
  10. * @param {object} options Object with the following properties:
  11. * @param {Context} context
  12. *
  13. * @exception {DeveloperError} A WebGL 2 context is required to use Sync operations.
  14. *
  15. * @private
  16. * @constructor
  17. */
  18. function Sync(options) {
  19. options = options ?? Frozen.EMPTY_OBJECT;
  20. const context = options.context;
  21. //>>includeStart('debug', pragmas.debug);
  22. Check.defined("options.context", context);
  23. //>>includeEnd('debug');
  24. if (!context._webgl2) {
  25. throw new DeveloperError(
  26. "A WebGL 2 context is required to use Sync operations.",
  27. );
  28. }
  29. const gl = context._gl;
  30. const sync = gl.fenceSync(WebGLConstants.SYNC_GPU_COMMANDS_COMPLETE, 0);
  31. this._gl = gl;
  32. this._sync = sync;
  33. }
  34. Sync.create = function (options) {
  35. return new Sync(options);
  36. };
  37. /**
  38. * Query the sync status of this Sync object.
  39. *
  40. * @returns {number} Returns a WebGLConstants indicating the status of the sync object (WebGLConstants.SIGNALED or WebGLConstants.UNSIGNALED).
  41. *
  42. * @private
  43. */
  44. Sync.prototype.getStatus = function () {
  45. const status = this._gl.getSyncParameter(
  46. this._sync,
  47. WebGLConstants.SYNC_STATUS,
  48. );
  49. return status;
  50. };
  51. Sync.prototype.isDestroyed = function () {
  52. return false;
  53. };
  54. Sync.prototype.destroy = function () {
  55. this._gl.deleteSync(this._sync);
  56. return destroyObject(this);
  57. };
  58. /**
  59. * Incremantally polls the status of the Sync object until signaled then resolves.
  60. * Usually polling should be done once per frame.
  61. *
  62. * @example
  63. * try {
  64. * await sync.waitForSignal(function (next) {
  65. * setTimeout(next, 100);
  66. * });
  67. *} catch (e) {
  68. * throw "Signal timeout";
  69. *} finally {
  70. * sync.destroy();
  71. *}
  72. *
  73. * @param {function} scheduleFunction Function for scheduling the next poll. Receives a callback as its only parameter.
  74. * @param {number} [ttl=10] Max number of iterations to poll until timeout.
  75. *
  76. * @exception {RuntimeError} Wait for signal timeout.
  77. */
  78. Sync.prototype.waitForSignal = async function (scheduleFunction, ttl) {
  79. const self = this;
  80. ttl = ttl ?? 10;
  81. function waitForSignal0(resolve, reject, ttl) {
  82. return () => {
  83. const syncStatus = self.getStatus();
  84. const signaled = syncStatus === WebGLConstants.SIGNALED;
  85. if (signaled) {
  86. resolve();
  87. } else if (ttl <= 0) {
  88. reject(new RuntimeError("Wait for signal timeout"));
  89. } else {
  90. scheduleFunction(waitForSignal0(resolve, reject, ttl - 1));
  91. }
  92. };
  93. }
  94. return new Promise((resolve, reject) => {
  95. scheduleFunction(waitForSignal0(resolve, reject, ttl));
  96. });
  97. };
  98. export default Sync;