@luma.gl/webgpu 9.2.6 → 9.3.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/adapter/helpers/get-bind-group.d.ts.map +1 -1
  2. package/dist/adapter/helpers/get-bind-group.js +11 -5
  3. package/dist/adapter/helpers/get-bind-group.js.map +1 -1
  4. package/dist/adapter/resources/webgpu-buffer.d.ts +7 -0
  5. package/dist/adapter/resources/webgpu-buffer.d.ts.map +1 -1
  6. package/dist/adapter/resources/webgpu-buffer.js +39 -12
  7. package/dist/adapter/resources/webgpu-buffer.js.map +1 -1
  8. package/dist/adapter/resources/webgpu-fence.d.ts +13 -0
  9. package/dist/adapter/resources/webgpu-fence.d.ts.map +1 -0
  10. package/dist/adapter/resources/webgpu-fence.js +25 -0
  11. package/dist/adapter/resources/webgpu-fence.js.map +1 -0
  12. package/dist/adapter/resources/webgpu-pipeline-layout.d.ts.map +1 -1
  13. package/dist/adapter/resources/webgpu-pipeline-layout.js +1 -2
  14. package/dist/adapter/resources/webgpu-pipeline-layout.js.map +1 -1
  15. package/dist/adapter/resources/webgpu-texture.d.ts +12 -3
  16. package/dist/adapter/resources/webgpu-texture.d.ts.map +1 -1
  17. package/dist/adapter/resources/webgpu-texture.js +143 -26
  18. package/dist/adapter/resources/webgpu-texture.js.map +1 -1
  19. package/dist/adapter/webgpu-adapter.d.ts.map +1 -1
  20. package/dist/adapter/webgpu-adapter.js +34 -34
  21. package/dist/adapter/webgpu-adapter.js.map +1 -1
  22. package/dist/adapter/webgpu-canvas-context.d.ts +4 -3
  23. package/dist/adapter/webgpu-canvas-context.d.ts.map +1 -1
  24. package/dist/adapter/webgpu-canvas-context.js +22 -21
  25. package/dist/adapter/webgpu-canvas-context.js.map +1 -1
  26. package/dist/adapter/webgpu-device.d.ts +3 -1
  27. package/dist/adapter/webgpu-device.d.ts.map +1 -1
  28. package/dist/adapter/webgpu-device.js +14 -3
  29. package/dist/adapter/webgpu-device.js.map +1 -1
  30. package/dist/dist.dev.js +6485 -293
  31. package/dist/dist.min.js +10 -6
  32. package/dist/index.cjs +398 -114
  33. package/dist/index.cjs.map +4 -4
  34. package/dist/index.d.ts +2 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +2 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/wgsl/get-shader-layout-wgsl.d.ts +8 -0
  39. package/dist/wgsl/get-shader-layout-wgsl.d.ts.map +1 -0
  40. package/dist/wgsl/get-shader-layout-wgsl.js +136 -0
  41. package/dist/wgsl/get-shader-layout-wgsl.js.map +1 -0
  42. package/package.json +6 -5
  43. package/src/adapter/helpers/get-bind-group.ts +18 -5
  44. package/src/adapter/resources/webgpu-buffer.ts +43 -12
  45. package/src/adapter/resources/webgpu-fence.ts +30 -0
  46. package/src/adapter/resources/webgpu-pipeline-layout.ts +1 -2
  47. package/src/adapter/resources/webgpu-texture.ts +202 -88
  48. package/src/adapter/webgpu-adapter.ts +40 -42
  49. package/src/adapter/webgpu-canvas-context.ts +25 -23
  50. package/src/adapter/webgpu-device.ts +19 -4
  51. package/src/index.ts +3 -0
  52. package/src/wgsl/get-shader-layout-wgsl.ts +156 -0
@@ -0,0 +1,156 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {AttributeShaderType, ShaderLayout, TextureBindingLayout, log} from '@luma.gl/core';
6
+ import {TypeInfo, VariableInfo, WgslReflect, ResourceType} from 'wgsl_reflect';
7
+
8
+ /**
9
+ * Parse a ShaderLayout from WGSL shader source code.
10
+ * @param source WGSL source code (can contain both @vertex and @fragment entry points)
11
+ * @returns
12
+ */
13
+ export function getShaderLayoutFromWGSL(source: string): ShaderLayout {
14
+ const shaderLayout: ShaderLayout = {attributes: [], bindings: []};
15
+
16
+ let parsedWGSL: WgslReflect;
17
+ try {
18
+ parsedWGSL = parseWGSL(source);
19
+ } catch (error: any) {
20
+ log.error(error.message)();
21
+ return shaderLayout;
22
+ }
23
+
24
+ for (const uniform of parsedWGSL.uniforms) {
25
+ const members = [];
26
+ // @ts-expect-error
27
+ for (const attribute of uniform.type?.members || []) {
28
+ members.push({
29
+ name: attribute.name,
30
+ type: getType(attribute.type)
31
+ });
32
+ }
33
+
34
+ shaderLayout.bindings.push({
35
+ type: 'uniform',
36
+ name: uniform.name,
37
+ group: uniform.group,
38
+ location: uniform.binding,
39
+ // @ts-expect-error TODO - unused for now but needs fixing
40
+ members
41
+ });
42
+ }
43
+
44
+ for (const texture of parsedWGSL.textures) {
45
+ const bindingDeclaration: TextureBindingLayout = {
46
+ type: 'texture',
47
+ name: texture.name,
48
+ group: texture.group,
49
+ location: texture.binding,
50
+ ...getTextureBindingFromReflect(texture)
51
+ };
52
+
53
+ shaderLayout.bindings.push(bindingDeclaration);
54
+ }
55
+
56
+ for (const sampler of parsedWGSL.samplers) {
57
+ shaderLayout.bindings.push({
58
+ type: 'sampler',
59
+ name: sampler.name,
60
+ group: sampler.group,
61
+ location: sampler.binding
62
+ });
63
+ }
64
+
65
+ const vertex = parsedWGSL.entry.vertex[0]; // "main"
66
+
67
+ // Vertex shader inputs
68
+ const attributeCount = vertex?.inputs.length || 0; // inputs to "main"
69
+ for (let i = 0; i < attributeCount; i++) {
70
+ const wgslAttribute = vertex.inputs[i];
71
+
72
+ // locationType can be "builtin"
73
+ if (wgslAttribute.locationType === 'location') {
74
+ const type = getType(wgslAttribute.type);
75
+
76
+ shaderLayout.attributes.push({
77
+ name: wgslAttribute.name,
78
+ location: Number(wgslAttribute.location),
79
+ type
80
+ });
81
+ }
82
+ }
83
+ return shaderLayout;
84
+ }
85
+
86
+ /** Get a valid shader attribute type string from a wgsl-reflect type */
87
+ function getType(type: TypeInfo | null): AttributeShaderType {
88
+ // @ts-expect-error WgslReflect type checks needed
89
+ return type?.format ? `${type.name}<${type.format.name}>` : type.name;
90
+ }
91
+
92
+ function parseWGSL(source: string): WgslReflect {
93
+ try {
94
+ return new WgslReflect(source);
95
+ } catch (error: any) {
96
+ if (error instanceof Error) {
97
+ throw error;
98
+ }
99
+ let message = 'WGSL parse error';
100
+ if (typeof error === 'object' && error?.message) {
101
+ message += `: ${error.message} `;
102
+ }
103
+ if (typeof error === 'object' && error?.token) {
104
+ message += error.token.line || '';
105
+ }
106
+ throw new Error(message, {cause: error});
107
+ }
108
+ }
109
+
110
+ function getTextureBindingFromReflect(
111
+ v: VariableInfo, // VariableInfo for a texture
112
+ opts?: {format?: GPUTextureFormat} // optional: if you know the runtime format
113
+ ): {
114
+ viewDimension: GPUTextureViewDimension;
115
+ /** @note sampleType float vs unfilterable-float cannot be determined without checking texture format and features */
116
+ sampleType: GPUTextureSampleType;
117
+ multisampled: boolean;
118
+ } {
119
+ if (v.resourceType !== ResourceType.Texture) {
120
+ throw new Error('Not a texture binding');
121
+ }
122
+
123
+ const typeName = v.type.name; // e.g. "texture_2d", "texture_cube_array", "texture_multisampled_2d"
124
+ // @ts-expect-error v.type.format is not always defined
125
+ const component = v.type.format?.name as 'f32' | 'i32' | 'u32' | undefined;
126
+
127
+ // viewDimension
128
+ const viewDimension: GPUTextureViewDimension = typeName.includes('cube_array')
129
+ ? 'cube-array'
130
+ : typeName.includes('cube')
131
+ ? 'cube'
132
+ : typeName.includes('2d_array')
133
+ ? '2d-array'
134
+ : typeName.includes('3d')
135
+ ? '3d'
136
+ : typeName.includes('1d')
137
+ ? '1d'
138
+ : '2d';
139
+
140
+ // multisampled
141
+ const multisampled = typeName === 'texture_multisampled_2d';
142
+
143
+ // sampleType
144
+ let sampleType: GPUTextureSampleType;
145
+ if (typeName.startsWith('texture_depth')) {
146
+ sampleType = 'depth';
147
+ } else if (component === 'i32') {
148
+ sampleType = 'sint';
149
+ } else if (component === 'u32') {
150
+ sampleType = 'uint';
151
+ } else {
152
+ sampleType = 'float'; // default to float
153
+ }
154
+
155
+ return {viewDimension, sampleType, multisampled};
156
+ }