123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import getTimestamp from "../Core/getTimestamp.js";
- import JobType from "./JobType.js";
- function JobTypeBudget(total) {
-
- this._total = total;
-
- this.usedThisFrame = 0.0;
-
- this.stolenFromMeThisFrame = 0.0;
-
- this.starvedThisFrame = false;
-
- this.starvedLastFrame = false;
- }
- Object.defineProperties(JobTypeBudget.prototype, {
- total: {
- get: function () {
- return this._total;
- },
- },
- });
- function JobScheduler(budgets) {
-
- if (defined(budgets) && budgets.length !== JobType.NUMBER_OF_JOB_TYPES) {
- throw new DeveloperError(
- "A budget must be specified for each job type; budgets.length should equal JobType.NUMBER_OF_JOB_TYPES."
- );
- }
-
-
- var jobBudgets = new Array(JobType.NUMBER_OF_JOB_TYPES);
- jobBudgets[JobType.TEXTURE] = new JobTypeBudget(
- defined(budgets) ? budgets[JobType.TEXTURE] : 10.0
- );
-
- jobBudgets[JobType.PROGRAM] = new JobTypeBudget(
- defined(budgets) ? budgets[JobType.PROGRAM] : 10.0
- );
- jobBudgets[JobType.BUFFER] = new JobTypeBudget(
- defined(budgets) ? budgets[JobType.BUFFER] : 30.0
- );
- var length = jobBudgets.length;
- var i;
- var totalBudget = 0.0;
- for (i = 0; i < length; ++i) {
- totalBudget += jobBudgets[i].total;
- }
- var executedThisFrame = new Array(length);
- for (i = 0; i < length; ++i) {
- executedThisFrame[i] = false;
- }
- this._totalBudget = totalBudget;
- this._totalUsedThisFrame = 0.0;
- this._budgets = jobBudgets;
- this._executedThisFrame = executedThisFrame;
- }
- JobScheduler.getTimestamp = getTimestamp;
- Object.defineProperties(JobScheduler.prototype, {
- totalBudget: {
- get: function () {
- return this._totalBudget;
- },
- },
- });
- JobScheduler.prototype.disableThisFrame = function () {
-
- this._totalUsedThisFrame = this._totalBudget;
- };
- JobScheduler.prototype.resetBudgets = function () {
- var budgets = this._budgets;
- var length = budgets.length;
- for (var i = 0; i < length; ++i) {
- var budget = budgets[i];
- budget.starvedLastFrame = budget.starvedThisFrame;
- budget.starvedThisFrame = false;
- budget.usedThisFrame = 0.0;
- budget.stolenFromMeThisFrame = 0.0;
- }
- this._totalUsedThisFrame = 0.0;
- };
- JobScheduler.prototype.execute = function (job, jobType) {
- var budgets = this._budgets;
- var budget = budgets[jobType];
-
- var progressThisFrame = this._executedThisFrame[jobType];
- if (this._totalUsedThisFrame >= this._totalBudget && progressThisFrame) {
-
- budget.starvedThisFrame = true;
- return false;
- }
- var stolenBudget;
- if (budget.usedThisFrame + budget.stolenFromMeThisFrame >= budget.total) {
-
- var length = budgets.length;
- var i;
- for (i = 0; i < length; ++i) {
- stolenBudget = budgets[i];
-
- if (
- stolenBudget.usedThisFrame + stolenBudget.stolenFromMeThisFrame <
- stolenBudget.total &&
- !stolenBudget.starvedLastFrame
- ) {
- break;
- }
- }
- if (i === length && progressThisFrame) {
-
-
- return false;
- }
- if (progressThisFrame) {
-
-
- budget.starvedThisFrame = true;
- }
- }
- var startTime = JobScheduler.getTimestamp();
- job.execute();
- var duration = JobScheduler.getTimestamp() - startTime;
-
-
- this._totalUsedThisFrame += duration;
- if (stolenBudget) {
- stolenBudget.stolenFromMeThisFrame += duration;
- } else {
- budget.usedThisFrame += duration;
- }
- this._executedThisFrame[jobType] = true;
- return true;
- };
- export default JobScheduler;
|