6.5 kBSource Map (JSON)View Raw
1{"version":3,"file":"UniformGroup.js","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":["Buffer","BUFFER_TYPE"],"mappings":";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