Skip to content

⬅️ Back to Table of Contents

📄 WebGLShaderCache.js

📊 Analysis Summary

Metric Count
🔧 Functions 7
🧱 Classes 2
📊 Variables & Constants 5

📚 Table of Contents

🛠️ File Location:

📂 src/renderers/webgl/WebGLShaderCache.js

Variables & Constants

Name Type Kind Value Exported
_id number let/var 0
vertexShader any let/var material.vertexShader
fragmentShader any let/var material.fragmentShader
cache Map<any, any> let/var this.materialCache
cache Map<any, any> let/var this.shaderCache

Functions

WebGLShaderCache.update(material: any): this

Parameters:

  • material any

Returns: this

Calls:

  • this._getShaderStage
  • this._getShaderCacheForMaterial
  • materialShaders.has
  • materialShaders.add
Code
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;

    }

WebGLShaderCache.remove(material: any): this

Parameters:

  • material any

Returns: this

Calls:

  • this.materialCache.get
  • this.shaderCache.delete
  • this.materialCache.delete
Code
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;

    }

WebGLShaderCache.getVertexShaderID(material: any): any

Parameters:

  • material any

Returns: any

Calls:

  • this._getShaderStage
Code
getVertexShaderID( material ) {

        return this._getShaderStage( material.vertexShader ).id;

    }

WebGLShaderCache.getFragmentShaderID(material: any): any

Parameters:

  • material any

Returns: any

Calls:

  • this._getShaderStage
Code
getFragmentShaderID( material ) {

        return this._getShaderStage( material.fragmentShader ).id;

    }

WebGLShaderCache.dispose(): void

Returns: void

Calls:

  • this.shaderCache.clear
  • this.materialCache.clear
Code
dispose() {

        this.shaderCache.clear();
        this.materialCache.clear();

    }

WebGLShaderCache._getShaderCacheForMaterial(material: any): any

Parameters:

  • material any

Returns: any

Calls:

  • cache.get
  • cache.set
Code
_getShaderCacheForMaterial( material ) {

        const cache = this.materialCache;
        let set = cache.get( material );

        if ( set === undefined ) {

            set = new Set();
            cache.set( material, set );

        }

        return set;

    }

WebGLShaderCache._getShaderStage(code: any): any

Parameters:

  • code any

Returns: any

Calls:

  • cache.get
  • cache.set
Code
_getShaderStage( code ) {

        const cache = this.shaderCache;
        let stage = cache.get( code );

        if ( stage === undefined ) {

            stage = new WebGLShaderStage( code );
            cache.set( code, stage );

        }

        return stage;

    }

Classes

WebGLShaderCache

Class Code
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;

    }

}

Methods

update(material: any): this
Code
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: any): this
Code
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: any): any
Code
getVertexShaderID( material ) {

        return this._getShaderStage( material.vertexShader ).id;

    }
getFragmentShaderID(material: any): any
Code
getFragmentShaderID( material ) {

        return this._getShaderStage( material.fragmentShader ).id;

    }
dispose(): void
Code
dispose() {

        this.shaderCache.clear();
        this.materialCache.clear();

    }
_getShaderCacheForMaterial(material: any): any
Code
_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: any): any
Code
_getShaderStage( code ) {

        const cache = this.shaderCache;
        let stage = cache.get( code );

        if ( stage === undefined ) {

            stage = new WebGLShaderStage( code );
            cache.set( code, stage );

        }

        return stage;

    }

WebGLShaderStage

Class Code
class WebGLShaderStage {

    constructor( code ) {

        this.id = _id ++;

        this.code = code;
        this.usedTimes = 0;

    }

}