@awayjs/stage
Version:
Stage for AwayJS
212 lines (211 loc) • 8.01 kB
JavaScript
import { RegisterPool } from './RegisterPool';
import { ShaderRegisterElement } from './ShaderRegisterElement';
/**
* ShaderRegister Cache provides the usage management system for all registers during shading compilers.
*/
var ShaderRegisterCache = /** @class */ (function () {
/**
* Create a new ShaderRegisterCache object.
*
* @param profile The compatibility profile used by the renderer.
*/
function ShaderRegisterCache(profile) {
this._numUsedVertexConstants = 0;
this._numUsedFragmentConstants = 0;
this._numUsedStreams = 0;
this._numUsedTextures = 0;
this._numUsedVaryings = 0;
this._profile = profile;
this.reset();
}
/**
* Resets all registers.
*/
ShaderRegisterCache.prototype.reset = function () {
this._fragmentTempCache = new RegisterPool('ft', 8, false);
this._vertexTempCache = new RegisterPool('vt', 8, false);
this._varyingCache = new RegisterPool('v', 8);
this._textureCache = new RegisterPool('fs', 8);
this._vertexAttributesCache = new RegisterPool('va', 8);
this._fragmentConstantsCache = new RegisterPool('fc', 28);
this._vertexConstantsCache = new RegisterPool('vc', 128);
this._fragmentOutputRegister = new ShaderRegisterElement('oc', -1);
this._vertexOutputRegister = new ShaderRegisterElement('op', -1);
this._numUsedVertexConstants = 0;
this._numUsedStreams = 0;
this._numUsedTextures = 0;
this._numUsedVaryings = 0;
this._numUsedFragmentConstants = 0;
};
/**
* Disposes all resources used.
*/
ShaderRegisterCache.prototype.dispose = function () {
this._fragmentTempCache.dispose();
this._vertexTempCache.dispose();
this._varyingCache.dispose();
this._fragmentConstantsCache.dispose();
this._vertexAttributesCache.dispose();
this._fragmentTempCache = null;
this._vertexTempCache = null;
this._varyingCache = null;
this._fragmentConstantsCache = null;
this._vertexAttributesCache = null;
this._fragmentOutputRegister = null;
this._vertexOutputRegister = null;
};
/**
* Marks a fragment temporary register as used, so it cannot be retrieved. The register won't be able to be used until removeUsage
* has been called usageCount times again.
* @param register The register to mark as used.
* @param usageCount The amount of usages to add.
*/
ShaderRegisterCache.prototype.addFragmentTempUsages = function (register, usageCount) {
this._fragmentTempCache.addUsage(register, usageCount);
};
/**
* Removes a usage from a fragment temporary register. When usages reach 0, the register is freed again.
* @param register The register for which to remove a usage.
*/
ShaderRegisterCache.prototype.removeFragmentTempUsage = function (register) {
this._fragmentTempCache.removeUsage(register);
};
/**
* Marks a vertex temporary register as used, so it cannot be retrieved. The register won't be able to be used
* until removeUsage has been called usageCount times again.
* @param register The register to mark as used.
* @param usageCount The amount of usages to add.
*/
ShaderRegisterCache.prototype.addVertexTempUsages = function (register, usageCount) {
this._vertexTempCache.addUsage(register, usageCount);
};
/**
* Removes a usage from a vertex temporary register. When usages reach 0, the register is freed again.
* @param register The register for which to remove a usage.
*/
ShaderRegisterCache.prototype.removeVertexTempUsage = function (register) {
this._vertexTempCache.removeUsage(register);
};
/**
* Retrieve an entire fragment temporary register that's still available. The register won't be able to be used until removeUsage
* has been called usageCount times again.
*/
ShaderRegisterCache.prototype.getFreeFragmentVectorTemp = function () {
return this._fragmentTempCache.requestFreeVectorReg();
};
/**
* Retrieve a single component from a fragment temporary register that's still available.
*/
ShaderRegisterCache.prototype.getFreeFragmentSingleTemp = function () {
return this._fragmentTempCache.requestFreeRegComponent();
};
/**
* Retrieve an available varying register
*/
ShaderRegisterCache.prototype.getFreeVarying = function () {
++this._numUsedVaryings;
return this._varyingCache.requestFreeVectorReg();
};
/**
* Retrieve an available fragment constant register
*/
ShaderRegisterCache.prototype.getFreeFragmentConstant = function () {
++this._numUsedFragmentConstants;
return this._fragmentConstantsCache.requestFreeVectorReg();
};
/**
* Retrieve an available vertex constant register
*/
ShaderRegisterCache.prototype.getFreeVertexConstant = function () {
++this._numUsedVertexConstants;
return this._vertexConstantsCache.requestFreeVectorReg();
};
/**
* Retrieve an entire vertex temporary register that's still available.
*/
ShaderRegisterCache.prototype.getFreeVertexVectorTemp = function () {
return this._vertexTempCache.requestFreeVectorReg();
};
/**
* Retrieve a single component from a vertex temporary register that's still available.
*/
ShaderRegisterCache.prototype.getFreeVertexSingleTemp = function () {
return this._vertexTempCache.requestFreeRegComponent();
};
/**
* Retrieve an available vertex attribute register
*/
ShaderRegisterCache.prototype.getFreeVertexAttribute = function () {
++this._numUsedStreams;
return this._vertexAttributesCache.requestFreeVectorReg();
};
/**
* Retrieve an available texture register
*/
ShaderRegisterCache.prototype.getFreeTextureReg = function () {
++this._numUsedTextures;
return this._textureCache.requestFreeVectorReg();
};
Object.defineProperty(ShaderRegisterCache.prototype, "fragmentOutputRegister", {
/**
* The fragment output register.
*/
get: function () {
return this._fragmentOutputRegister;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ShaderRegisterCache.prototype, "numUsedVertexConstants", {
/**
* The amount of used vertex constant registers.
*/
get: function () {
return this._numUsedVertexConstants;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ShaderRegisterCache.prototype, "numUsedFragmentConstants", {
/**
* The amount of used fragment constant registers.
*/
get: function () {
return this._numUsedFragmentConstants;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ShaderRegisterCache.prototype, "numUsedStreams", {
/**
* The amount of used vertex streams.
*/
get: function () {
return this._numUsedStreams;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ShaderRegisterCache.prototype, "numUsedTextures", {
/**
* The amount of used texture slots.
*/
get: function () {
return this._numUsedTextures;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ShaderRegisterCache.prototype, "numUsedVaryings", {
/**
* The amount of used varying registers.
*/
get: function () {
return this._numUsedVaryings;
},
enumerable: false,
configurable: true
});
return ShaderRegisterCache;
}());
export { ShaderRegisterCache };