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.19 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 // 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,aACb,CAAA;AAAA;AAA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