{"version":3,"file":"UniformGroup.mjs","sources":["../../src/shader/UniformGroup.ts"],"sourcesContent":["import { BUFFER_TYPE } from '@pixi/constants';\nimport { Buffer } from '../geometry/Buffer';\n\nimport type { Dict } from '@pixi/utils';\nimport type { UniformsSyncCallback } from './utils';\n\nlet UID = 0;\n\n/**\n * Uniform group holds uniform map and some ID's for work\n *\n * `UniformGroup` has two modes:\n *\n * 1: Normal mode\n * Normal mode will upload the uniforms with individual function calls as required\n *\n * 2: Uniform buffer mode\n * This mode will treat the uniforms as a uniform buffer. You can pass in either a buffer that you manually handle, or\n * or a generic object that PixiJS will automatically map to a buffer for you.\n * For maximum benefits, make Ubo UniformGroups static, and only update them each frame.\n *\n * Rules of UBOs:\n * - UBOs only work with WebGL2, so make sure you have a fallback!\n * - Only floats are supported (including vec[2,3,4], mat[2,3,4])\n * - Samplers cannot be used in ubo's (a GPU limitation)\n * - You must ensure that the object you pass in exactly matches in the shader ubo structure.\n * Otherwise, weirdness will ensue!\n * - The name of the ubo object added to the group must match exactly the name of the ubo in the shader.\n *\n * ```glsl\n * // UBO in shader:\n * uniform myCoolData { // Declaring a UBO...\n *     mat4 uCoolMatrix;\n *     float uFloatyMcFloatFace;\n * };\n * ```\n *\n * ```js\n * // A new Uniform Buffer Object...\n * const myCoolData = new UniformBufferGroup({\n *     uCoolMatrix: new Matrix(),\n *     uFloatyMcFloatFace: 23,\n * }}\n *\n * // Build a shader...\n * const shader = Shader.from(srcVert, srcFrag, {\n *     myCoolData // Name matches the UBO name in the shader. Will be processed accordingly.\n * })\n *\n *  ```\n * @memberof PIXI\n */\nexport class UniformGroup<LAYOUT = Dict<any>>\n{\n    /**\n     * Uniform values\n     * @member {object}\n     */\n    public readonly uniforms: LAYOUT;\n\n    /**\n     * Its a group and not a single uniforms.\n     * @default true\n     */\n    public readonly group: boolean;\n\n    /**\n     * unique id\n     * @protected\n     */\n    public id: number;\n    syncUniforms: Dict<UniformsSyncCallback>;\n\n    /**\n     * Dirty version\n     * @protected\n     */\n    dirtyId: number;\n\n    /** Flag for if uniforms wont be changed after creation. */\n    static: boolean;\n\n    /** Flags whether this group is treated like a uniform buffer object. */\n    ubo: boolean;\n    buffer?: Buffer;\n    autoManage: boolean;\n\n    /**\n     * @param {object | Buffer} [uniforms] - Custom uniforms to use to augment the built-in ones. Or a pixi buffer.\n     * @param isStatic - Uniforms wont be changed after creation.\n     * @param isUbo - If true, will treat this uniform group as a uniform buffer object.\n     */\n    constructor(uniforms: LAYOUT | Buffer, isStatic?: boolean, isUbo?: boolean)\n    {\n        this.group = true;\n\n        // lets generate this when the shader ?\n        this.syncUniforms = {};\n        this.dirtyId = 0;\n        this.id = UID++;\n        this.static = !!isStatic;\n        this.ubo = !!isUbo;\n\n        if (uniforms instanceof Buffer)\n        {\n            this.buffer = uniforms;\n            this.buffer.type = BUFFER_TYPE.UNIFORM_BUFFER;\n            this.autoManage = false;\n            this.ubo = true;\n        }\n        else\n        {\n            this.uniforms = uniforms;\n\n            if (this.ubo)\n            {\n                this.buffer = new Buffer(new Float32Array(1));\n                this.buffer.type = BUFFER_TYPE.UNIFORM_BUFFER;\n                this.autoManage = true;\n            }\n        }\n    }\n\n    update(): void\n    {\n        this.dirtyId++;\n\n        if (!this.autoManage && this.buffer)\n        {\n            this.buffer.update();\n        }\n    }\n\n    add(name: string, uniforms: Dict<any>, _static?: boolean): void\n    {\n        if (!this.ubo)\n        {\n            (this.uniforms as any)[name] = new UniformGroup(uniforms, _static);\n        }\n        else\n        {\n            // eslint-disable-next-line max-len\n            throw new Error('[UniformGroup] uniform groups in ubo mode cannot be modified, or have uniform groups nested in them');\n        }\n    }\n\n    static from(uniforms: Dict<any> | Buffer, _static?: boolean, _ubo?: boolean): UniformGroup\n    {\n        return new UniformGroup(uniforms, _static, _ubo);\n    }\n\n    /**\n     * A short hand function for creating a static UBO UniformGroup.\n     * @param uniforms - the ubo item\n     * @param _static - should this be updated each time it is used? defaults to true here!\n     */\n    static uboFrom(uniforms: Dict<any> | Buffer, _static?: boolean): UniformGroup\n    {\n        return new UniformGroup(uniforms, _static ?? true, true);\n    }\n}\n"],"names":[],"mappings":";;AAMA,IAAI,MAAM;AA8CH,MAAM,aACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuCI,YAAY,UAA2B,UAAoB,OAC3D;AACS,SAAA,QAAQ,IAGb,KAAK,eAAe,CACpB,GAAA,KAAK,UAAU,GACf,KAAK,KAAK,OACV,KAAK,SAAS,CAAC,CAAC,UAChB,KAAK,MAAM,CAAC,CAAC,OAET,oBAAoB,UAEpB,KAAK,SAAS,UACd,KAAK,OAAO,OAAO,YAAY,gBAC/B,KAAK,aAAa,IAClB,KAAK,MAAM,OAIX,KAAK,WAAW,UAEZ,KAAK,QAEL,KAAK,SAAS,IAAI,OAAO,IAAI,aAAa,CAAC,CAAC,GAC5C,KAAK,OAAO,OAAO,YAAY,gBAC/B,KAAK,aAAa;AAAA,EAG9B;AAAA,EAEA,SACA;AACS,SAAA,WAED,CAAC,KAAK,cAAc,KAAK,UAEzB,KAAK,OAAO;EAEpB;AAAA,EAEA,IAAI,MAAc,UAAqB,SACvC;AACI,QAAI,CAAC,KAAK;AAEL,WAAK,SAAiB,IAAI,IAAI,IAAI,aAAa,UAAU,OAAO;AAAA;AAK3D,YAAA,IAAI,MAAM,qGAAqG;AAAA,EAE7H;AAAA,EAEA,OAAO,KAAK,UAA8B,SAAmB,MAC7D;AACI,WAAO,IAAI,aAAa,UAAU,SAAS,IAAI;AAAA,EACnD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,QAAQ,UAA8B,SAC7C;AACI,WAAO,IAAI,aAAa,UAAU,WAAW,IAAM,EAAI;AAAA,EAC3D;AACJ;"}