Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/shapecraft. flo-bit.dev/shapecraft
Something went wrong. Try again.
2.0 kB · 124 lines
JavaScript
at commit da69385e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125let _id = 0;
class WebGLShaderCache {
constructor() {
this.shaderCache = new Map(); this.materialCache = new Map();
}
update( material ) {
const vertexShader = material.vertexShader; const fragmentShader = material.fragmentShader;
const vertexShaderStage = this._getShaderStage( vertexShader ); const fragmentShaderStage = this._getShaderStage( fragmentShader );
const materialShaders = this._getShaderCacheForMaterial( material );
if ( materialShaders.has( vertexShaderStage ) === false ) {
materialShaders.add( vertexShaderStage ); vertexShaderStage.usedTimes ++;
}
if ( materialShaders.has( fragmentShaderStage ) === false ) {
materialShaders.add( fragmentShaderStage ); fragmentShaderStage.usedTimes ++;
}
return this;
}
remove( material ) {
const materialShaders = this.materialCache.get( material );
for ( const shaderStage of materialShaders ) {
shaderStage.usedTimes --;
if ( shaderStage.usedTimes === 0 ) this.shaderCache.delete( shaderStage.code );
}
this.materialCache.delete( material );
return this;
}
getVertexShaderID( material ) {
return this._getShaderStage( material.vertexShader ).id;
}
getFragmentShaderID( material ) {
return this._getShaderStage( material.fragmentShader ).id;
}
dispose() {
this.shaderCache.clear(); this.materialCache.clear();
}
_getShaderCacheForMaterial( material ) {
const cache = this.materialCache; let set = cache.get( material );
if ( set === undefined ) {
set = new Set(); cache.set( material, set );
}
return set;
}
_getShaderStage( code ) {
const cache = this.shaderCache; let stage = cache.get( code );
if ( stage === undefined ) {
stage = new WebGLShaderStage( code ); cache.set( code, stage );
}
return stage;
}
}
class WebGLShaderStage {
constructor( code ) {
this.id = _id ++;
this.code = code; this.usedTimes = 0;
}
}
export { WebGLShaderCache };