pixi.js
Version:
<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">
1 lines • 9.75 kB
Source Map (JSON)
{"version":3,"file":"UniformGroup.mjs","sources":["../../../../../src/rendering/renderers/shared/shader/UniformGroup.ts"],"sourcesContent":["import { uid } from '../../../../utils/data/uid';\nimport { createIdFromString } from '../utils/createIdFromString';\nimport { UNIFORM_TYPES_MAP, UNIFORM_TYPES_VALUES, type UniformData } from './types';\nimport { getDefaultUniformValue } from './utils/getDefaultUniformValue';\n\nimport type { BindResource } from '../../gpu/shader/BindResource';\nimport type { Buffer } from '../buffer/Buffer';\n\ntype FLOPS<T = UniformData> = T extends { value: infer V } ? V : never;\n\n/**\n * Extracts the value type from a uniform data object.\n * @internal\n */\nexport type ExtractUniformObject<T = Record<string, UniformData>> = {\n [K in keyof T]: FLOPS<T[K]>;\n};\n\n/**\n * Uniform group options\n * @category rendering\n * @advanced\n */\nexport type UniformGroupOptions = {\n /**\n * if true the UniformGroup is handled as an Uniform buffer object.\n * This is the only way WebGPU can work with uniforms. WebGL2 can also use this.\n * So don't set to true if you want to use WebGPU :D\n */\n ubo?: boolean;\n /** if true, then you are responsible for when the data is uploaded to the GPU by calling `update()` */\n isStatic?: boolean;\n};\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. This is the default mode\n * for WebGL rendering.\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 * This is the only way uniforms can be used with WebGPU.\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 * When declaring your uniform options, you ust parse in the value and the type of the uniform.\n * The types correspond to the WebGPU types\n *\n Uniforms can be modified via the classes 'uniforms' property. It will contain all the uniforms declared in the constructor.\n *\n * ```ts\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 UniformGroup({\n * uCoolMatrix: {value:new Matrix(), type: 'mat4<f32>'},\n * uFloatyMcFloatFace: {value:23, type: 'f32'},\n * }}\n *\n * // modify the data\n * myCoolData.uniforms.uFloatyMcFloatFace = 42;\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 * @category rendering\n * @advanced\n */\nexport class UniformGroup<UNIFORMS extends { [key: string]: UniformData } = any> implements BindResource\n{\n /** The default options used by the uniform group. */\n public static defaultOptions: UniformGroupOptions = {\n /** if true the UniformGroup is handled as an Uniform buffer object. */\n ubo: false,\n /** if true, then you are responsible for when the data is uploaded to the GPU by calling `update()` */\n isStatic: false,\n };\n\n /**\n * used internally to know if a uniform group was used in the last render pass\n * @internal\n */\n public _touched = 0;\n\n /** a unique id for this uniform group used through the renderer */\n public readonly uid: number = uid('uniform');\n /**\n * a resource type, used to identify how to handle it when its in a bind group / shader resource\n * @internal\n */\n public _resourceType = 'uniformGroup';\n /**\n * the resource id used internally by the renderer to build bind group keys\n * @internal\n */\n public _resourceId = uid('resource');\n /** the structures of the uniform group */\n public uniformStructures: UNIFORMS;\n /** the uniforms as an easily accessible map of properties */\n public uniforms: ExtractUniformObject<UNIFORMS>;\n /** true if it should be used as a uniform buffer object */\n public ubo: boolean;\n /** an underlying buffer that will be uploaded to the GPU when using this UniformGroup */\n public buffer?: Buffer;\n /**\n * if true, then you are responsible for when the data is uploaded to the GPU.\n * otherwise, the data is reuploaded each frame.\n */\n public isStatic: boolean;\n /** used ito identify if this is a uniform group */\n public readonly isUniformGroup = true;\n /**\n * used to flag if this Uniform groups data is different from what it has stored in its buffer / on the GPU\n * @internal\n */\n public _dirtyId = 0;\n /**\n * a signature string generated for internal use\n * @internal\n */\n public readonly _signature: number;\n\n // implementing the interface - UniformGroup are not destroyed\n public readonly destroyed = false;\n\n /**\n * Create a new Uniform group\n * @param uniformStructures - The structures of the uniform group\n * @param options - The optional parameters of this uniform group\n */\n constructor(uniformStructures: UNIFORMS, options?: UniformGroupOptions)\n {\n options = { ...UniformGroup.defaultOptions, ...options };\n\n this.uniformStructures = uniformStructures;\n\n const uniforms = {} as ExtractUniformObject<UNIFORMS>;\n\n for (const i in uniformStructures)\n {\n const uniformData = uniformStructures[i] as UniformData;\n\n uniformData.name = i;\n uniformData.size = uniformData.size ?? 1;\n\n if (!UNIFORM_TYPES_MAP[uniformData.type])\n {\n const arrayMatch = uniformData.type.match(/^array<(\\w+(?:<\\w+>)?),\\s*(\\d+)>$/);\n\n if (arrayMatch)\n {\n const [, innerType, size] = arrayMatch;\n\n throw new Error(\n `Uniform type ${uniformData.type} is not supported. Use type: '${innerType}', size: ${size} instead.`\n );\n }\n\n // eslint-disable-next-line max-len\n throw new Error(`Uniform type ${uniformData.type} is not supported. Supported uniform types are: ${UNIFORM_TYPES_VALUES.join(', ')}`);\n }\n\n uniformData.value ??= getDefaultUniformValue(uniformData.type, uniformData.size);\n\n uniforms[i] = uniformData.value as ExtractUniformObject<UNIFORMS>[keyof UNIFORMS];\n }\n\n this.uniforms = uniforms;\n\n this._dirtyId = 1;\n this.ubo = options.ubo;\n this.isStatic = options.isStatic;\n\n this._signature = createIdFromString(Object.keys(uniforms).map(\n (i) => `${i}-${(uniformStructures[i as keyof typeof uniformStructures] as UniformData).type}`\n ).join('-'), 'uniform-group');\n }\n\n /** Call this if you want the uniform groups data to be uploaded to the GPU only useful if `isStatic` is true. */\n public update(): void\n {\n this._dirtyId++;\n // dispatch...\n }\n}\n"],"names":[],"mappings":";;;;;;AAuFO,MAAM,aAAA,GAAN,MAAM,aAAA,CACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA6DI,WAAA,CAAY,mBAA6B,OAAA,EACzC;AAjDA;AAAA;AAAA;AAAA;AAAA,IAAA,IAAA,CAAO,QAAA,GAAW,CAAA;AAGlB;AAAA,IAAA,IAAA,CAAgB,GAAA,GAAc,IAAI,SAAS,CAAA;AAK3C;AAAA;AAAA;AAAA;AAAA,IAAA,IAAA,CAAO,aAAA,GAAgB,cAAA;AAKvB;AAAA;AAAA;AAAA;AAAA,IAAA,IAAA,CAAO,WAAA,GAAc,IAAI,UAAU,CAAA;AAenC;AAAA,IAAA,IAAA,CAAgB,cAAA,GAAiB,IAAA;AAKjC;AAAA;AAAA;AAAA;AAAA,IAAA,IAAA,CAAO,QAAA,GAAW,CAAA;AAQlB;AAAA,IAAA,IAAA,CAAgB,SAAA,GAAY,KAAA;AASxB,IAAA,OAAA,GAAU,EAAE,GAAG,aAAA,CAAa,cAAA,EAAgB,GAAG,OAAA,EAAQ;AAEvD,IAAA,IAAA,CAAK,iBAAA,GAAoB,iBAAA;AAEzB,IAAA,MAAM,WAAW,EAAC;AAElB,IAAA,KAAA,MAAW,KAAK,iBAAA,EAChB;AACI,MAAA,MAAM,WAAA,GAAc,kBAAkB,CAAC,CAAA;AAEvC,MAAA,WAAA,CAAY,IAAA,GAAO,CAAA;AACnB,MAAA,WAAA,CAAY,IAAA,GAAO,YAAY,IAAA,IAAQ,CAAA;AAEvC,MAAA,IAAI,CAAC,iBAAA,CAAkB,WAAA,CAAY,IAAI,CAAA,EACvC;AACI,QAAA,MAAM,UAAA,GAAa,WAAA,CAAY,IAAA,CAAK,KAAA,CAAM,mCAAmC,CAAA;AAE7E,QAAA,IAAI,UAAA,EACJ;AAC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