{"name":"wgsl_reflect","version":"1.0.8","description":"WGSL Parser and Reflection library","author":{"name":"Brendan Duncan"},"license":"MIT","module":"wgsl_reflect.module.js","main":"wgsl_reflect.node.js","types":"types/index.d.ts","repository":{"type":"git","url":"git+https://github.com/brendan-duncan/wgsl_reflect.git"},"homepage":"https://github.com/brendan-duncan/wgsl_reflect","bugs":{"url":"https://github.com/brendan-duncan/wgsl_reflect/issues"},"sideEffects":false,"directories":{"test":"test"},"scripts":{"build":"rollup -c rollup.config.js","watch":"npm run watch:js","watch:js":"rollup -c rollup.config.js --watch"},"keywords":["wgsl_reflect","webgpu","wgsl","javascript","html5"],"devDependencies":{"@rollup/plugin-typescript":"^11.1.2","rollup":"^2.60.0","tslib":"^2.6.1","typescript":"^5.3.3"},"jspm":{"files":["package.json","LICENSE.md","README.md","wgsl_reflect.module.js","wgsl_reflect.node.js","types"]},"_id":"wgsl_reflect@1.0.8","gitHead":"3df90ac9301613458bc6a0d19e6f7824092a1297","_nodeVersion":"21.4.0","_npmVersion":"10.2.5","dist":{"integrity":"sha512-0kdpA5H3SF2CMeCBijYqQz+ZT+uW310nJORFX8QPFOvrkfSGNPHFDE7aGPCUnsuDi1kzpj+9SEFIhqjf9iHGSQ==","shasum":"7f6940ae9a49d5a125f3782ce710139357441945","tarball":"http://registry-npm.ywwl.com/wgsl_reflect/download/wgsl_reflect-1.0.8.tgz","fileCount":10,"unpackedSize":339372,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDHOeZbyCbfxazamULlprNnJBpULAZJBjNWZ5YZ0Hv9XgIgIuxpVGqoDOik2edwY4fGxOw5/MrHRPalHjS3RespVcg="}],"size":52319,"noattachment":false,"key":"/wgsl_reflect/-/wgsl_reflect-1.0.8.tgz"},"_npmUser":{"name":"brendanduncan","email":"brendanduncan@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/wgsl_reflect_1.0.8_1721151617921_0.6826883441803402"},"_hasShrinkwrap":false,"readme":"# WebGPU Shading Language Reflection Library\r\n\r\nA WebGPU Shading Language parser and reflection library for Typescript and Javascript.\r\n\r\n**wgsl_reflect** can parse a WGSL shader and analyze its contents, providing information about the shader. It can determine the bind group layout of the shader, resource bindings, uniform buffers, the members of a uniform buffer, their names, types, sizes, offsets into the buffer.\r\n\r\n## Usage\r\n\r\nFrom NPM\r\n```\r\nnpm install wgsl_reflect\r\n```\r\n\r\nThe _wgsl_reflect.module.js_ file is a self-contained roll-up of the library that can be included in your project and imported with:\r\n\r\n```javascript\r\nimport { WgslReflect } from \"wgsl_reflect/wgsl_reflect.module.js\";\r\nconst reflect = new WgslReflect(shader_code);\r\n```\r\n\r\n## Example\r\n\r\n[WGSL Reflect Example](https://brendan-duncan.github.io/wgsl_reflect/example.html)\r\n\r\n## Documentation\r\n\r\n```javascript\r\nclass WgslReflect {\r\n  /// All top-level uniform vars in the shader.\r\n  uniforms: Array<VariableInfo>;\r\n  /// All top-level storage vars in the shader, including storage buffers and textures.\r\n  storage: Array<VariableInfo>;\r\n  /// All top-level texture vars in the shader;\r\n  textures: Array<VariableInfo>;\r\n  // All top-level sampler vars in the shader.\r\n  samplers: Array<VariableInfo>;\r\n  /// All top-level type aliases in the shader.\r\n  aliases: Array<AliasInfo>;\r\n  /// All top-level overrides in the shader.\r\n  overrides: Array<OverrideInfo> = [];\r\n  /// All top-level structs in the shader.\r\n  structs: Array<StructInfo>;\r\n  /// All entry functions in the shader: vertex, fragment, and/or compute.\r\n  entry: EntryFunctions;\r\n  /// All functions in the shader, including entry functions.\r\n  functions: Array<FunctionInfo>;\r\n\r\n  // Find a resource by its group and binding.\r\n  findResource(group: number, binding: number): VariableInfo | null;\r\n\r\n  // Get the bind groups used by the shader, bindGroups[group][binding].\r\n  getBindGroups(): Array<Array<VariableInfo>>;\r\n}\r\n\r\nenum ResourceType {\r\n  Uniform,\r\n  Storage,\r\n  Texture,\r\n  Sampler,\r\n  StorageTexture\r\n}\r\n\r\nclass VariableInfo {\r\n  name: string; // The name of the variable\r\n  type: TypeInfo; // The type of the variable\r\n  group: number; // The binding group of the variable\r\n  binding: number; // The binding index of the variable\r\n  resourceType: ResourceType; // The resource type of the variable\r\n  access: string; // \"\", read, write, read_write\r\n\r\n  get isArray(): boolean; // True if it's an array type\r\n  get isStruct(): boolean;  // True if it's a struct type\r\n  get isTemplate(): boolean; // True if it's a template type\r\n  get size(): number; // Size of the data, in bytes\r\n  get align(): number; // The alignment size if it's a struct, otherwise 0\r\n  get members(): Array<MemberInfo> | null; // The list of members if it's a struct, otherwise null\r\n  get format(): TypeInfo | null; // The format if it's a template or array, otherwise null\r\n  get count(): number; // The array size if it's an array, otherwise 0\r\n  get stride(): number; // The array stride if it's an array, otherwise 0\r\n}\r\n\r\nclass TypeInfo {\r\n  name: string;\r\n  size: number; // Size of the data, in bytes\r\n\r\n  get isArray(): boolean;\r\n  get isStruct(): boolean;\r\n  get isTemplate(): boolean;\r\n}\r\n\r\nclass StructInfo extends TypeInfo {\r\n  members: Array<MemberInfo>;\r\n  align: number;\r\n  startLine: number;\r\n  endLine: number;\r\n  inUse: boolean; // true if the struct is used by a uniform, storage, or directly or indirectly by an entry function.\r\n}\r\n\r\nclass ArrayInfo extends TypeInfo {\r\n  format: TypeInfo;\r\n  count: number;\r\n  stride: number;\r\n}\r\n\r\nclass TemplateInfo extends TypeInfo {\r\n  format: TypeInfo;\r\n  access: string; // \"\", read, write, read_write\r\n}\r\n\r\nclass MemberInfo {\r\n  name: string;\r\n  type: TypeInfo;\r\n  offset: number;\r\n  size: number;\r\n\r\n  get isArray(): boolean;\r\n  get isStruct(): boolean;\r\n  get isTemplate(): boolean;\r\n  get align(): number;\r\n  get members(): Array<MemberInfo> | null;\r\n  get format(): TypeInfo | null;\r\n  get count(): number;\r\n  get stride(): number;\r\n}\r\n\r\nclass AliasInfo {\r\n  name: string;\r\n  type: TypeInfo;\r\n}\r\n\r\nclass EntryFunctions {\r\n  vertex: Array<FunctionInfo>;\r\n  fragment: Array<FunctionInfo>;\r\n  compute: Array<FunctionInfo>;\r\n}\r\n\r\nclass FunctionInfo {\r\n  name: string;\r\n  stage: string | null;\r\n  inputs: Array<InputInfo>;\r\n  outputs: Array<OutputInfo>;\r\n  resources: Array<VariableInfo>;\r\n  startLine: number;\r\n  endLine: number;\r\n  inUse: boolean;  // true if called directly or indirectly by an entry function.\r\n  calls: Set<FunctionInfo>; // All custom functions called directly by this function.\r\n}\r\n\r\nclass InputInfo {\r\n  name: string;\r\n  type: TypeInfo | null;\r\n  locationType: string;\r\n  location: number | string;\r\n  interpolation: string | null;\r\n}\r\n\r\nclass OutputInfo {\r\n  name: string;\r\n  type: TypeInfo | null;\r\n  locationType: string;\r\n  location: number | string;\r\n}\r\n\r\nclass OverrideInfo {\r\n  name: string;\r\n  type: TypeInfo | null;\r\n  id: number;\r\n}\r\n```\r\n\r\n## Examples\r\n\r\nCalculate the bind group information in the shader:\r\n\r\n```javascript\r\nimport { WgslReflect } from \"./wgsl_reflect.module.js\";\r\n\r\nconst shader = `\r\nstruct ViewUniforms {\r\n    viewProjection: mat4x4<f32>\r\n}\r\n\r\nstruct ModelUniforms {\r\n    model: mat4x4<f32>,\r\n    color: vec4<f32>,\r\n    intensity: f32\r\n}\r\n\r\n@binding(0) @group(0) var<uniform> viewUniforms: ViewUniforms;\r\n@binding(1) @group(0) var<uniform> modelUniforms: ModelUniforms;\r\n@binding(2) @group(0) var u_sampler: sampler;\r\n@binding(3) @group(0) var u_texture: texture_2d<f32>;\r\n\r\nstruct VertexInput {\r\n    @location(0) a_position: vec3<f32>,\r\n    @location(1) a_normal: vec3<f32>,\r\n    @location(2) a_color: vec4<f32>,\r\n    @location(3) a_uv: vec2<f32>\r\n}\r\n\r\nstruct VertexOutput {\r\n    @builtin(position) Position: vec4<f32>,\r\n    @location(0) v_position: vec4<f32>,\r\n    @location(1) v_normal: vec3<f32>,\r\n    @location(2) v_color: vec4<f32>,\r\n    @location(3) v_uv: vec2<f32>\r\n}\r\n\r\n@vertex\r\nfn main(input: VertexInput) -> VertexOutput {\r\n    var output: VertexOutput;\r\n    output.Position = viewUniforms.viewProjection * modelUniforms.model * vec4<f32>(input.a_position, 1.0);\r\n    output.v_position = output.Position;\r\n    output.v_normal = input.a_normal;\r\n    output.v_color = input.a_color * modelUniforms.color * modelUniforms.intensity;\r\n    output.v_uv = input.a_uv;\r\n    return output;\r\n}`;\r\n\r\nconst reflect = new WgslReflect(shader);\r\n\r\nconsole.log(reflect.functions.length); // 1\r\nconsole.log(reflect.structs.length); // 4\r\nconsole.log(reflect.uniforms.length); // 2\r\n\r\n// Shader entry points\r\nconsole.log(reflect.entry.vertex.length); // 1, there is 1 vertex entry function.\r\nconsole.log(reflect.entry.fragment.length); // 0, there are no fragment entry functions.\r\nconsole.log(reflect.entry.compute.length); // 0, there are no compute entry functions.\r\n\r\nconsole.log(reflect.entry.vertex[0].name); // \"main\", the name of the vertex entry function.\r\n\r\nconsole.log(reflect.entry.vertex[0].resources.length); // 2, main uses modelUniforms and viewUniforms resource bindings.\r\nconsole.log(reflect.entry.vertex[0].resources[0].name); // viewUniforms\r\nconsole.log(reflect.entry.vertex[0].resources[1].name); // modelUniforms\r\n\r\n// Vertex shader inputs\r\nconsole.log(reflect.entry.vertex[0].inputs.length); // 4, inputs to \"main\"\r\nconsole.log(reflect.entry.vertex[0].inputs[0].name); // \"a_position\"\r\nconsole.log(reflect.entry.vertex[0].inputs[0].location); // 0\r\nconsole.log(reflect.entry.vertex[0].inputs[0].locationType); // \"location\" (can be \"builtin\")\r\nconsole.log(reflect.entry.vertex[0].inputs[0].type.name); // \"vec3\"\r\nconsole.log(reflect.entry.vertex[0].inputs[0].type.format.name); // \"f32\"\r\n\r\n// Gather the bind groups used by the shader.\r\nconst groups = reflect.getBindGroups();\r\nconsole.log(groups.length); // 1\r\nconsole.log(groups[0].length); // 4, bindings in group(0)\r\n\r\nconsole.log(groups[0][1].resourceType); // ResourceType.Uniform, the type of resource at group(0) binding(1)\r\nconsole.log(groups[0][1].size); // 108, the size of the uniform buffer.\r\nconsole.log(groups[0][1].members.length); // 3, members in ModelUniforms.\r\nconsole.log(groups[0][1].members[0].name); // \"model\", the name of the first member in the uniform buffer.\r\nconsole.log(groups[0][1].members[0].offset); // 0, the offset of 'model' in the uniform buffer.\r\nconsole.log(groups[0][1].members[0].size); // 64, the size of 'model'.\r\nconsole.log(groups[0][1].members[0].type.name); // \"mat4x4\", the type of 'model'.\r\nconsole.log(groups[0][1].members[0].type.format.name); // \"f32\", the format of the mat4x4.\r\n\r\nconsole.log(groups[0][2].resourceType); // ResourceType.Sampler\r\n\r\nconsole.log(groups[0][3].resourceType); // ResourceType.Texture\r\nconsole.log(groups[0][3].type.name); // \"texture_2d\"\r\nconsole.log(groups[0][3].type.format.name); // \"f32\"\r\n```\r\n\r\n---\r\n\r\nCalculate the member information for a uniform buffer block:\r\n\r\n```javascript\r\nimport { WgslReflect } from \"./wgsl_reflect.module.js\";\r\n\r\n// WgslReflect can calculate the size and offset for members of a uniform buffer block.\r\n\r\nconst shader = `\r\nstruct A {                                     //             align(8)  size(32)\r\n    u: f32,                                    // offset(0)   align(4)  size(4)\r\n    v: f32,                                    // offset(4)   align(4)  size(4)\r\n    w: vec2<f32>,                              // offset(8)   align(8)  size(8)\r\n    @size(16) x: f32                          // offset(16)  align(4)  size(16)\r\n}\r\n\r\nstruct B {                                     //             align(16) size(208)\r\n    a: vec2<f32>,                              // offset(0)   align(8)  size(8)\r\n    // -- implicit member alignment padding -- // offset(8)             size(8)\r\n    b: vec3<f32>,                              // offset(16)  align(16) size(12)\r\n    c: f32,                                    // offset(28)  align(4)  size(4)\r\n    d: f32,                                    // offset(32)  align(4)  size(4)\r\n    // -- implicit member alignment padding -- // offset(36)            size(12)\r\n    @align(16) e: A,                           // offset(48)  align(16) size(32)\r\n    f: vec3<f32>,                              // offset(80)  align(16) size(12)\r\n    // -- implicit member alignment padding -- // offset(92)            size(4)\r\n    g: @stride(32) array<A, 3>,                // offset(96)  align(8)  size(96)\r\n    h: i32,                                    // offset(192) align(4)  size(4)\r\n    // -- implicit struct size padding --      // offset(196)           size(12)\r\n}\r\n\r\n@group(0) @binding(0)\r\nvar<uniform> uniform_buffer: B;`;\r\n\r\nconst reflect = new WgslReflect(shader);\r\n\r\nconst u = reflect.uniforms[0];\r\nconsole.log(u.size); // 208, the size of the uniform buffer in bytes\r\nconsole.log(u.group); // 0\r\nconsole.log(u.binding); // 0\r\nconsole.log(u.members.length); // 7, members in B\r\nconsole.log(u.members[0].name); // \"a\"\r\nconsole.log(u.members[0].offset); // 0, the offset of 'a' in the buffer\r\nconsole.log(u.members[0].size); // 8, the size of 'a' in bytes\r\nconsole.log(u.members[0].type.name); // \"vec2\", the type of 'a'\r\nconsole.log(u.members[0].type.format.name); // \"f32\", the format of the vec2.\r\n\r\nconsole.log(u.members[4].name); // \"e\"\r\nconsole.log(u.members[4].offset); // 48, the offset of 'e' in the buffer\r\nconsole.log(u.members[4].size); // 32, the size of 'e' in the buffer\r\n```\r\n","publish_time":1721151618177,"maintainers":[{"name":"jimblackler","email":"jimblackler@gmail.com"},{"name":"brendanduncan","email":"brendanduncan@gmail.com"}],"_cnpm_publish_time":1721151618177,"dist-tags":{"latest":"1.6.0"}}