import { CompileGLSL } from "../utils" ; import { SHADER_STAGE } from "../constants" ; import { Renderer } from "../Renderer" ; type ShaderCodeType = ( Uint32Array | string ) ; export interface IShaderOptions { name ? : string ; stage : SHADER_STAGE ; code : ShaderCodeType ; } export const SHADER_DEFAULT_OPTIONS = { name : null , stage : SHADER_STAGE . NONE , code : null } ; function ToShaderStageString ( stage ) { switch ( stage ) { case SHADER_STAGE . NONE : throw new Error ( `'SHADER_STAGE.NONE' is not a valid shader stage` ) ; case SHADER_STAGE . VERTEX : return "vertex" ; case SHADER_STAGE . FRAGMENT : return "fragment" ; } } export class Shader { private _name : string = null ; private _stage : SHADER_STAGE = SHADER_STAGE . NONE ; private _code : ShaderCodeType = null ; private _resource : GPUShaderModule = null ; param options public constructor ( options ? ) { options = Object . assign ( { } , SHADER_DEFAULT_OPTIONS , options ) ; this . setName ( options . name ) ; this . _stage = options . stage ; if ( typeof options . code === "string" ) { this . _code = CompileGLSL ( options . code , ToShaderStageString ( options . stage ) ) ; } else { this . _code = options . code ; } } /**
     * The shader name
     */ public getName ( ) { return this . _name ; } param value public setName ( value ) { this . _name = value ; } /**
     * Returns the shader's GLSL or SPIRV code
     */ public getCode ( ) { return this . _code ; } /**
     * Returns the shader's stage
     */ public getStage ( ) { return this . _stage ; } /**
     * The GPU shader resource
     */ public getResource ( ) { return this . _resource ; } param renderer param descriptor public create ( renderer , descriptor ) { if ( this . _resource === null ) { const device = renderer . getDevice ( ) ; const instance = device . createShaderModule ( descriptor ) ; this . _resource = instance ; } } /**
     * Destroy this Object
     */ public destroy ( ) { this . _name = null ; this . _code = null ; this . _resource = null ; } }