@luma.gl/shadertools 9.2.6 → 9.3.0-alpha.11
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.
- package/dist/dist.dev.js +5388 -6438
- package/dist/dist.min.js +2264 -322
- package/dist/index.cjs +3601 -566
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +12 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/glsl-utils/shader-utils.js +4 -4
- package/dist/lib/glsl-utils/shader-utils.js.map +1 -1
- package/dist/lib/preprocessor/preprocessor.d.ts.map +1 -1
- package/dist/lib/preprocessor/preprocessor.js +35 -8
- package/dist/lib/preprocessor/preprocessor.js.map +1 -1
- package/dist/lib/shader-assembler.d.ts +10 -0
- package/dist/lib/shader-assembler.d.ts.map +1 -1
- package/dist/lib/shader-assembler.js +20 -3
- package/dist/lib/shader-assembler.js.map +1 -1
- package/dist/lib/shader-assembly/assemble-shaders.d.ts +23 -2
- package/dist/lib/shader-assembly/assemble-shaders.d.ts.map +1 -1
- package/dist/lib/shader-assembly/assemble-shaders.js +265 -12
- package/dist/lib/shader-assembly/assemble-shaders.js.map +1 -1
- package/dist/lib/shader-assembly/wgsl-binding-debug.d.ts +37 -0
- package/dist/lib/shader-assembly/wgsl-binding-debug.d.ts.map +1 -0
- package/dist/lib/shader-assembly/wgsl-binding-debug.js +144 -0
- package/dist/lib/shader-assembly/wgsl-binding-debug.js.map +1 -0
- package/dist/lib/shader-assembly/wgsl-binding-scan.d.ts +19 -0
- package/dist/lib/shader-assembly/wgsl-binding-scan.d.ts.map +1 -0
- package/dist/lib/shader-assembly/wgsl-binding-scan.js +151 -0
- package/dist/lib/shader-assembly/wgsl-binding-scan.js.map +1 -0
- package/dist/lib/shader-generator/glsl/generate-glsl.js +7 -4
- package/dist/lib/shader-generator/glsl/generate-glsl.js.map +1 -1
- package/dist/lib/shader-generator/wgsl/generate-wgsl.d.ts.map +1 -1
- package/dist/lib/shader-generator/wgsl/generate-wgsl.js +3 -0
- package/dist/lib/shader-generator/wgsl/generate-wgsl.js.map +1 -1
- package/dist/lib/shader-module/shader-module-uniform-layout.d.ts +91 -0
- package/dist/lib/shader-module/shader-module-uniform-layout.d.ts.map +1 -0
- package/dist/lib/shader-module/shader-module-uniform-layout.js +253 -0
- package/dist/lib/shader-module/shader-module-uniform-layout.js.map +1 -0
- package/dist/lib/shader-module/shader-module.d.ts +12 -6
- package/dist/lib/shader-module/shader-module.d.ts.map +1 -1
- package/dist/lib/shader-module/shader-module.js.map +1 -1
- package/dist/lib/utils/assert.d.ts.map +1 -1
- package/dist/lib/utils/assert.js +3 -1
- package/dist/lib/utils/assert.js.map +1 -1
- package/dist/lib/utils/uniform-types.d.ts +11 -7
- package/dist/lib/utils/uniform-types.d.ts.map +1 -1
- package/dist/modules/engine/picking/picking.d.ts +5 -2
- package/dist/modules/engine/picking/picking.d.ts.map +1 -1
- package/dist/modules/engine/picking/picking.js +5 -2
- package/dist/modules/engine/picking/picking.js.map +1 -1
- package/dist/modules/engine/project/project.d.ts +1 -1
- package/dist/modules/engine/project/project.js +1 -1
- package/dist/modules/engine/skin/skin.d.ts +30 -0
- package/dist/modules/engine/skin/skin.d.ts.map +1 -0
- package/dist/modules/engine/skin/skin.js +86 -0
- package/dist/modules/engine/skin/skin.js.map +1 -0
- package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts +1 -0
- package/dist/modules/lighting/gouraud-material/gouraud-material.d.ts.map +1 -1
- package/dist/modules/lighting/gouraud-material/gouraud-material.js +3 -0
- package/dist/modules/lighting/gouraud-material/gouraud-material.js.map +1 -1
- package/dist/modules/lighting/ibl/ibl.d.ts +26 -0
- package/dist/modules/lighting/ibl/ibl.d.ts.map +1 -0
- package/dist/modules/lighting/ibl/ibl.js +33 -0
- package/dist/modules/lighting/ibl/ibl.js.map +1 -0
- package/dist/modules/lighting/lambert-material/lambert-material.d.ts +10 -0
- package/dist/modules/lighting/lambert-material/lambert-material.d.ts.map +1 -0
- package/dist/modules/lighting/lambert-material/lambert-material.js +33 -0
- package/dist/modules/lighting/lambert-material/lambert-material.js.map +1 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.d.ts +3 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.d.ts.map +1 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.js +60 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-glsl.js.map +1 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.d.ts +2 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.d.ts.map +1 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.js +73 -0
- package/dist/modules/lighting/lambert-material/lambert-shaders-wgsl.js.map +1 -0
- package/dist/modules/lighting/lights/lighting-glsl.d.ts +1 -1
- package/dist/modules/lighting/lights/lighting-glsl.d.ts.map +1 -1
- package/dist/modules/lighting/lights/lighting-glsl.js +44 -38
- package/dist/modules/lighting/lights/lighting-glsl.js.map +1 -1
- package/dist/modules/lighting/lights/lighting-wgsl.d.ts +1 -1
- package/dist/modules/lighting/lights/lighting-wgsl.d.ts.map +1 -1
- package/dist/modules/lighting/lights/lighting-wgsl.js +46 -18
- package/dist/modules/lighting/lights/lighting-wgsl.js.map +1 -1
- package/dist/modules/lighting/lights/lighting.d.ts +104 -62
- package/dist/modules/lighting/lights/lighting.d.ts.map +1 -1
- package/dist/modules/lighting/lights/lighting.js +107 -68
- package/dist/modules/lighting/lights/lighting.js.map +1 -1
- package/dist/modules/lighting/no-material/dirlight.d.ts +8 -3
- package/dist/modules/lighting/no-material/dirlight.d.ts.map +1 -1
- package/dist/modules/lighting/no-material/dirlight.js +5 -3
- package/dist/modules/lighting/no-material/dirlight.js.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-material-glsl.d.ts +2 -2
- package/dist/modules/lighting/pbr-material/pbr-material-glsl.d.ts.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-material-glsl.js +694 -38
- package/dist/modules/lighting/pbr-material/pbr-material-glsl.js.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts +2 -2
- package/dist/modules/lighting/pbr-material/pbr-material-wgsl.d.ts.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js +956 -109
- package/dist/modules/lighting/pbr-material/pbr-material-wgsl.js.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-material.d.ts +242 -43
- package/dist/modules/lighting/pbr-material/pbr-material.d.ts.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-material.js +178 -2
- package/dist/modules/lighting/pbr-material/pbr-material.js.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-projection.d.ts.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-projection.js +14 -2
- package/dist/modules/lighting/pbr-material/pbr-projection.js.map +1 -1
- package/dist/modules/lighting/pbr-material/pbr-scene.d.ts +40 -0
- package/dist/modules/lighting/pbr-material/pbr-scene.d.ts.map +1 -0
- package/dist/modules/lighting/pbr-material/pbr-scene.js +67 -0
- package/dist/modules/lighting/pbr-material/pbr-scene.js.map +1 -0
- package/dist/modules/lighting/phong-material/phong-material.d.ts +1 -0
- package/dist/modules/lighting/phong-material/phong-material.d.ts.map +1 -1
- package/dist/modules/lighting/phong-material/phong-material.js +4 -0
- package/dist/modules/lighting/phong-material/phong-material.js.map +1 -1
- package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts +2 -2
- package/dist/modules/lighting/phong-material/phong-shaders-glsl.d.ts.map +1 -1
- package/dist/modules/lighting/phong-material/phong-shaders-glsl.js +17 -6
- package/dist/modules/lighting/phong-material/phong-shaders-glsl.js.map +1 -1
- package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts +1 -40
- package/dist/modules/lighting/phong-material/phong-shaders-wgsl.d.ts.map +1 -1
- package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js +71 -76
- package/dist/modules/lighting/phong-material/phong-shaders-wgsl.js.map +1 -1
- package/dist/modules/math/fp64/fp64-arithmetic-glsl.d.ts +1 -1
- package/dist/modules/math/fp64/fp64-arithmetic-glsl.d.ts.map +1 -1
- package/dist/modules/math/fp64/fp64-arithmetic-glsl.js +42 -11
- package/dist/modules/math/fp64/fp64-arithmetic-glsl.js.map +1 -1
- package/dist/modules/math/fp64/fp64-arithmetic-wgsl.d.ts +2 -0
- package/dist/modules/math/fp64/fp64-arithmetic-wgsl.d.ts.map +1 -0
- package/dist/modules/math/fp64/fp64-arithmetic-wgsl.js +212 -0
- package/dist/modules/math/fp64/fp64-arithmetic-wgsl.js.map +1 -0
- package/dist/modules/math/fp64/fp64.d.ts +1 -0
- package/dist/modules/math/fp64/fp64.d.ts.map +1 -1
- package/dist/modules/math/fp64/fp64.js +8 -2
- package/dist/modules/math/fp64/fp64.js.map +1 -1
- package/dist/modules/math/random/random.d.ts +1 -1
- package/dist/modules/math/random/random.d.ts.map +1 -1
- package/dist/modules/math/random/random.js +2 -3
- package/dist/modules/math/random/random.js.map +1 -1
- package/package.json +4 -5
- package/src/index.ts +37 -6
- package/src/lib/glsl-utils/shader-utils.ts +4 -4
- package/src/lib/preprocessor/preprocessor.ts +44 -8
- package/src/lib/shader-assembler.ts +25 -3
- package/src/lib/shader-assembly/assemble-shaders.ts +506 -13
- package/src/lib/shader-assembly/wgsl-binding-debug.ts +227 -0
- package/src/lib/shader-assembly/wgsl-binding-scan.ts +228 -0
- package/src/lib/shader-generator/glsl/generate-glsl.ts +11 -5
- package/src/lib/shader-generator/wgsl/generate-wgsl.ts +6 -0
- package/src/lib/shader-module/shader-module-uniform-layout.ts +420 -0
- package/src/lib/shader-module/shader-module.ts +17 -7
- package/src/lib/utils/assert.ts +3 -1
- package/src/lib/utils/uniform-types.ts +24 -9
- package/src/modules/engine/picking/picking.ts +5 -2
- package/src/modules/engine/project/project.ts +1 -1
- package/src/modules/engine/skin/skin.ts +114 -0
- package/src/modules/lighting/gouraud-material/gouraud-material.ts +4 -0
- package/src/modules/lighting/ibl/ibl.ts +44 -0
- package/src/modules/lighting/lambert-material/lambert-material.ts +42 -0
- package/src/modules/lighting/lambert-material/lambert-shaders-glsl.ts +61 -0
- package/src/modules/lighting/lambert-material/lambert-shaders-wgsl.ts +73 -0
- package/src/modules/lighting/lights/lighting-glsl.ts +44 -38
- package/src/modules/lighting/lights/lighting-wgsl.ts +46 -18
- package/src/modules/lighting/lights/lighting.ts +198 -99
- package/src/modules/lighting/no-material/dirlight.ts +5 -3
- package/src/modules/lighting/pbr-material/pbr-material-glsl.ts +694 -38
- package/src/modules/lighting/pbr-material/pbr-material-wgsl.ts +956 -109
- package/src/modules/lighting/pbr-material/pbr-material.ts +294 -7
- package/src/modules/lighting/pbr-material/pbr-projection.ts +15 -2
- package/src/modules/lighting/pbr-material/pbr-scene.ts +91 -0
- package/src/modules/lighting/phong-material/phong-material.ts +5 -0
- package/src/modules/lighting/phong-material/phong-shaders-glsl.ts +17 -6
- package/src/modules/lighting/phong-material/phong-shaders-wgsl.ts +71 -77
- package/src/modules/math/fp64/fp64-arithmetic-glsl.ts +42 -11
- package/src/modules/math/fp64/fp64-arithmetic-wgsl.ts +212 -0
- package/src/modules/math/fp64/fp64.ts +9 -3
- package/src/modules/math/random/random.ts +2 -3
- package/dist/lib/wgsl/get-shader-layout-wgsl.d.ts +0 -8
- package/dist/lib/wgsl/get-shader-layout-wgsl.d.ts.map +0 -1
- package/dist/lib/wgsl/get-shader-layout-wgsl.js +0 -95
- package/dist/lib/wgsl/get-shader-layout-wgsl.js.map +0 -1
- package/src/lib/wgsl/get-shader-layout-wgsl.ts +0 -105
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {WGSL_BINDABLE_VARIABLE_PATTERN, maskWGSLComments} from './wgsl-binding-scan';
|
|
6
|
+
|
|
7
|
+
type ShaderBindingAssignment = {
|
|
8
|
+
moduleName: string;
|
|
9
|
+
name: string;
|
|
10
|
+
group: number;
|
|
11
|
+
location: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const WGSL_BINDING_DEBUG_REGEXES = [
|
|
15
|
+
new RegExp(
|
|
16
|
+
`@binding\\(\\s*(\\d+)\\s*\\)\\s*@group\\(\\s*(\\d+)\\s*\\)\\s*${WGSL_BINDABLE_VARIABLE_PATTERN}\\s*:\\s*([^;]+);`,
|
|
17
|
+
'g'
|
|
18
|
+
),
|
|
19
|
+
new RegExp(
|
|
20
|
+
`@group\\(\\s*(\\d+)\\s*\\)\\s*@binding\\(\\s*(\\d+)\\s*\\)\\s*${WGSL_BINDABLE_VARIABLE_PATTERN}\\s*:\\s*([^;]+);`,
|
|
21
|
+
'g'
|
|
22
|
+
)
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
/** One debug row describing a WGSL binding in the assembled shader source. */
|
|
26
|
+
export type ShaderBindingDebugRow = {
|
|
27
|
+
/** Binding name as declared in WGSL. */
|
|
28
|
+
name: string;
|
|
29
|
+
/** Bind-group index. */
|
|
30
|
+
group: number;
|
|
31
|
+
/** Binding slot within the bind group. */
|
|
32
|
+
binding: number;
|
|
33
|
+
/** Resource kind inferred from the WGSL declaration. */
|
|
34
|
+
kind:
|
|
35
|
+
| 'uniform'
|
|
36
|
+
| 'storage'
|
|
37
|
+
| 'read-only-storage'
|
|
38
|
+
| 'texture'
|
|
39
|
+
| 'sampler'
|
|
40
|
+
| 'storage-texture'
|
|
41
|
+
| 'unknown';
|
|
42
|
+
/** Whether the binding came from application WGSL or a shader module. */
|
|
43
|
+
owner: 'application' | 'module';
|
|
44
|
+
/** Shader module name when the binding was contributed by a module. */
|
|
45
|
+
moduleName?: string;
|
|
46
|
+
/** Full WGSL resource type text from the declaration. */
|
|
47
|
+
resourceType?: string;
|
|
48
|
+
/** WGSL access mode when cheaply available. */
|
|
49
|
+
access?: string;
|
|
50
|
+
/** Texture view dimension when cheaply available. */
|
|
51
|
+
viewDimension?: string;
|
|
52
|
+
/** Texture sample type when cheaply available. */
|
|
53
|
+
sampleType?: string;
|
|
54
|
+
/** Sampler kind when cheaply available. */
|
|
55
|
+
samplerKind?: string;
|
|
56
|
+
/** Whether the texture is multisampled when cheaply available. */
|
|
57
|
+
multisampled?: boolean;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Builds a stable, table-friendly binding summary from assembled WGSL source. */
|
|
61
|
+
export function getShaderBindingDebugRowsFromWGSL(
|
|
62
|
+
source: string,
|
|
63
|
+
bindingAssignments: ShaderBindingAssignment[] = []
|
|
64
|
+
): ShaderBindingDebugRow[] {
|
|
65
|
+
const maskedSource = maskWGSLComments(source);
|
|
66
|
+
const assignmentMap = new Map<string, string>();
|
|
67
|
+
for (const bindingAssignment of bindingAssignments) {
|
|
68
|
+
assignmentMap.set(
|
|
69
|
+
getBindingAssignmentKey(
|
|
70
|
+
bindingAssignment.name,
|
|
71
|
+
bindingAssignment.group,
|
|
72
|
+
bindingAssignment.location
|
|
73
|
+
),
|
|
74
|
+
bindingAssignment.moduleName
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const rows: ShaderBindingDebugRow[] = [];
|
|
79
|
+
for (const regex of WGSL_BINDING_DEBUG_REGEXES) {
|
|
80
|
+
regex.lastIndex = 0;
|
|
81
|
+
let match: RegExpExecArray | null;
|
|
82
|
+
match = regex.exec(maskedSource);
|
|
83
|
+
while (match) {
|
|
84
|
+
const isBindingFirst = regex === WGSL_BINDING_DEBUG_REGEXES[0];
|
|
85
|
+
const binding = Number(match[isBindingFirst ? 1 : 2]);
|
|
86
|
+
const group = Number(match[isBindingFirst ? 2 : 1]);
|
|
87
|
+
const accessDeclaration = match[3]?.trim();
|
|
88
|
+
const name = match[4];
|
|
89
|
+
const resourceType = match[5].trim();
|
|
90
|
+
const moduleName = assignmentMap.get(getBindingAssignmentKey(name, group, binding));
|
|
91
|
+
|
|
92
|
+
rows.push(
|
|
93
|
+
normalizeShaderBindingDebugRow({
|
|
94
|
+
name,
|
|
95
|
+
group,
|
|
96
|
+
binding,
|
|
97
|
+
owner: moduleName ? 'module' : 'application',
|
|
98
|
+
moduleName,
|
|
99
|
+
accessDeclaration,
|
|
100
|
+
resourceType
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
match = regex.exec(maskedSource);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return rows.sort((left, right) => {
|
|
108
|
+
if (left.group !== right.group) {
|
|
109
|
+
return left.group - right.group;
|
|
110
|
+
}
|
|
111
|
+
if (left.binding !== right.binding) {
|
|
112
|
+
return left.binding - right.binding;
|
|
113
|
+
}
|
|
114
|
+
return left.name.localeCompare(right.name);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function normalizeShaderBindingDebugRow(row: {
|
|
119
|
+
name: string;
|
|
120
|
+
group: number;
|
|
121
|
+
binding: number;
|
|
122
|
+
owner: 'application' | 'module';
|
|
123
|
+
moduleName?: string;
|
|
124
|
+
accessDeclaration?: string;
|
|
125
|
+
resourceType: string;
|
|
126
|
+
}): ShaderBindingDebugRow {
|
|
127
|
+
const baseRow: ShaderBindingDebugRow = {
|
|
128
|
+
name: row.name,
|
|
129
|
+
group: row.group,
|
|
130
|
+
binding: row.binding,
|
|
131
|
+
owner: row.owner,
|
|
132
|
+
kind: 'unknown',
|
|
133
|
+
moduleName: row.moduleName,
|
|
134
|
+
resourceType: row.resourceType
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
if (row.accessDeclaration) {
|
|
138
|
+
const access = row.accessDeclaration.split(',').map(value => value.trim());
|
|
139
|
+
if (access[0] === 'uniform') {
|
|
140
|
+
return {...baseRow, kind: 'uniform', access: 'uniform'};
|
|
141
|
+
}
|
|
142
|
+
if (access[0] === 'storage') {
|
|
143
|
+
const storageAccess = access[1] || 'read_write';
|
|
144
|
+
return {
|
|
145
|
+
...baseRow,
|
|
146
|
+
kind: storageAccess === 'read' ? 'read-only-storage' : 'storage',
|
|
147
|
+
access: storageAccess
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (row.resourceType === 'sampler' || row.resourceType === 'sampler_comparison') {
|
|
153
|
+
return {
|
|
154
|
+
...baseRow,
|
|
155
|
+
kind: 'sampler',
|
|
156
|
+
samplerKind: row.resourceType === 'sampler_comparison' ? 'comparison' : 'filtering'
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (row.resourceType.startsWith('texture_storage_')) {
|
|
161
|
+
return {
|
|
162
|
+
...baseRow,
|
|
163
|
+
kind: 'storage-texture',
|
|
164
|
+
access: getStorageTextureAccess(row.resourceType),
|
|
165
|
+
viewDimension: getTextureViewDimension(row.resourceType)
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (row.resourceType.startsWith('texture_')) {
|
|
170
|
+
return {
|
|
171
|
+
...baseRow,
|
|
172
|
+
kind: 'texture',
|
|
173
|
+
viewDimension: getTextureViewDimension(row.resourceType),
|
|
174
|
+
sampleType: getTextureSampleType(row.resourceType),
|
|
175
|
+
multisampled: row.resourceType.startsWith('texture_multisampled_')
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return baseRow;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function getBindingAssignmentKey(name: string, group: number, binding: number): string {
|
|
183
|
+
return `${group}:${binding}:${name}`;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function getTextureViewDimension(resourceType: string): string | undefined {
|
|
187
|
+
if (resourceType.includes('cube_array')) {
|
|
188
|
+
return 'cube-array';
|
|
189
|
+
}
|
|
190
|
+
if (resourceType.includes('2d_array')) {
|
|
191
|
+
return '2d-array';
|
|
192
|
+
}
|
|
193
|
+
if (resourceType.includes('cube')) {
|
|
194
|
+
return 'cube';
|
|
195
|
+
}
|
|
196
|
+
if (resourceType.includes('3d')) {
|
|
197
|
+
return '3d';
|
|
198
|
+
}
|
|
199
|
+
if (resourceType.includes('2d')) {
|
|
200
|
+
return '2d';
|
|
201
|
+
}
|
|
202
|
+
if (resourceType.includes('1d')) {
|
|
203
|
+
return '1d';
|
|
204
|
+
}
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function getTextureSampleType(resourceType: string): string | undefined {
|
|
209
|
+
if (resourceType.startsWith('texture_depth_')) {
|
|
210
|
+
return 'depth';
|
|
211
|
+
}
|
|
212
|
+
if (resourceType.includes('<i32>')) {
|
|
213
|
+
return 'sint';
|
|
214
|
+
}
|
|
215
|
+
if (resourceType.includes('<u32>')) {
|
|
216
|
+
return 'uint';
|
|
217
|
+
}
|
|
218
|
+
if (resourceType.includes('<f32>')) {
|
|
219
|
+
return 'float';
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function getStorageTextureAccess(resourceType: string): string | undefined {
|
|
225
|
+
const match = /,\s*([A-Za-z_][A-Za-z0-9_]*)\s*>$/.exec(resourceType);
|
|
226
|
+
return match?.[1];
|
|
227
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export const WGSL_BINDABLE_VARIABLE_PATTERN =
|
|
6
|
+
'(?:var<\\s*(uniform|storage(?:\\s*,\\s*[A-Za-z_][A-Za-z0-9_]*)?)\\s*>|var)\\s+([A-Za-z_][A-Za-z0-9_]*)';
|
|
7
|
+
const WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN = '\\s*';
|
|
8
|
+
|
|
9
|
+
export const MODULE_WGSL_BINDING_DECLARATION_REGEXES = [
|
|
10
|
+
new RegExp(
|
|
11
|
+
`@binding\\(\\s*(auto|\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}@group\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
12
|
+
'g'
|
|
13
|
+
),
|
|
14
|
+
new RegExp(
|
|
15
|
+
`@group\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}@binding\\(\\s*(auto|\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
16
|
+
'g'
|
|
17
|
+
)
|
|
18
|
+
] as const;
|
|
19
|
+
|
|
20
|
+
export const WGSL_BINDING_DECLARATION_REGEXES = [
|
|
21
|
+
new RegExp(
|
|
22
|
+
`@binding\\(\\s*(auto|\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}@group\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
23
|
+
'g'
|
|
24
|
+
),
|
|
25
|
+
new RegExp(
|
|
26
|
+
`@group\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}@binding\\(\\s*(auto|\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
27
|
+
'g'
|
|
28
|
+
)
|
|
29
|
+
] as const;
|
|
30
|
+
|
|
31
|
+
export const WGSL_EXPLICIT_BINDING_DECLARATION_REGEXES = [
|
|
32
|
+
new RegExp(
|
|
33
|
+
`@binding\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}@group\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
34
|
+
'g'
|
|
35
|
+
),
|
|
36
|
+
new RegExp(
|
|
37
|
+
`@group\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}@binding\\(\\s*(\\d+)\\s*\\)${WGSL_BINDING_DECLARATION_SEPARATOR_PATTERN}${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
38
|
+
'g'
|
|
39
|
+
)
|
|
40
|
+
] as const;
|
|
41
|
+
|
|
42
|
+
const WGSL_AUTO_BINDING_DECLARATION_REGEXES = [
|
|
43
|
+
new RegExp(
|
|
44
|
+
`@binding\\(\\s*(auto)\\s*\\)\\s*@group\\(\\s*(\\d+)\\s*\\)\\s*${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
45
|
+
'g'
|
|
46
|
+
),
|
|
47
|
+
new RegExp(
|
|
48
|
+
`@group\\(\\s*(\\d+)\\s*\\)\\s*@binding\\(\\s*(auto)\\s*\\)\\s*${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
49
|
+
'g'
|
|
50
|
+
),
|
|
51
|
+
new RegExp(
|
|
52
|
+
`@binding\\(\\s*(auto)\\s*\\)\\s*@group\\(\\s*(\\d+)\\s*\\)(?:[\\s\\n\\r]*@[A-Za-z_][^\\n\\r]*)*[\\s\\n\\r]*${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
53
|
+
'g'
|
|
54
|
+
),
|
|
55
|
+
new RegExp(
|
|
56
|
+
`@group\\(\\s*(\\d+)\\s*\\)\\s*@binding\\(\\s*(auto)\\s*\\)(?:[\\s\\n\\r]*@[A-Za-z_][^\\n\\r]*)*[\\s\\n\\r]*${WGSL_BINDABLE_VARIABLE_PATTERN}`,
|
|
57
|
+
'g'
|
|
58
|
+
)
|
|
59
|
+
] as const;
|
|
60
|
+
|
|
61
|
+
export type WGSLBindingDeclarationMatch = {
|
|
62
|
+
match: string;
|
|
63
|
+
index: number;
|
|
64
|
+
length: number;
|
|
65
|
+
bindingToken: string;
|
|
66
|
+
groupToken: string;
|
|
67
|
+
accessDeclaration?: string;
|
|
68
|
+
name: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export function maskWGSLComments(source: string): string {
|
|
72
|
+
const maskedCharacters = source.split('');
|
|
73
|
+
let index = 0;
|
|
74
|
+
let blockCommentDepth = 0;
|
|
75
|
+
let inLineComment = false;
|
|
76
|
+
let inString = false;
|
|
77
|
+
let isEscaped = false;
|
|
78
|
+
|
|
79
|
+
while (index < source.length) {
|
|
80
|
+
const character = source[index];
|
|
81
|
+
const nextCharacter = source[index + 1];
|
|
82
|
+
|
|
83
|
+
if (inString) {
|
|
84
|
+
if (isEscaped) {
|
|
85
|
+
isEscaped = false;
|
|
86
|
+
} else if (character === '\\') {
|
|
87
|
+
isEscaped = true;
|
|
88
|
+
} else if (character === '"') {
|
|
89
|
+
inString = false;
|
|
90
|
+
}
|
|
91
|
+
index++;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (inLineComment) {
|
|
96
|
+
if (character === '\n' || character === '\r') {
|
|
97
|
+
inLineComment = false;
|
|
98
|
+
} else {
|
|
99
|
+
maskedCharacters[index] = ' ';
|
|
100
|
+
}
|
|
101
|
+
index++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (blockCommentDepth > 0) {
|
|
106
|
+
if (character === '/' && nextCharacter === '*') {
|
|
107
|
+
maskedCharacters[index] = ' ';
|
|
108
|
+
maskedCharacters[index + 1] = ' ';
|
|
109
|
+
blockCommentDepth++;
|
|
110
|
+
index += 2;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (character === '*' && nextCharacter === '/') {
|
|
115
|
+
maskedCharacters[index] = ' ';
|
|
116
|
+
maskedCharacters[index + 1] = ' ';
|
|
117
|
+
blockCommentDepth--;
|
|
118
|
+
index += 2;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (character !== '\n' && character !== '\r') {
|
|
123
|
+
maskedCharacters[index] = ' ';
|
|
124
|
+
}
|
|
125
|
+
index++;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (character === '"') {
|
|
130
|
+
inString = true;
|
|
131
|
+
index++;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (character === '/' && nextCharacter === '/') {
|
|
136
|
+
maskedCharacters[index] = ' ';
|
|
137
|
+
maskedCharacters[index + 1] = ' ';
|
|
138
|
+
inLineComment = true;
|
|
139
|
+
index += 2;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (character === '/' && nextCharacter === '*') {
|
|
144
|
+
maskedCharacters[index] = ' ';
|
|
145
|
+
maskedCharacters[index + 1] = ' ';
|
|
146
|
+
blockCommentDepth = 1;
|
|
147
|
+
index += 2;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
index++;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return maskedCharacters.join('');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function getWGSLBindingDeclarationMatches(
|
|
158
|
+
source: string,
|
|
159
|
+
regexes: readonly RegExp[]
|
|
160
|
+
): WGSLBindingDeclarationMatch[] {
|
|
161
|
+
const maskedSource = maskWGSLComments(source);
|
|
162
|
+
const matches: WGSLBindingDeclarationMatch[] = [];
|
|
163
|
+
|
|
164
|
+
for (const regex of regexes) {
|
|
165
|
+
regex.lastIndex = 0;
|
|
166
|
+
let match: RegExpExecArray | null;
|
|
167
|
+
match = regex.exec(maskedSource);
|
|
168
|
+
while (match) {
|
|
169
|
+
const isBindingFirst = regex === regexes[0];
|
|
170
|
+
const index = match.index;
|
|
171
|
+
const length = match[0].length;
|
|
172
|
+
matches.push({
|
|
173
|
+
match: source.slice(index, index + length),
|
|
174
|
+
index,
|
|
175
|
+
length,
|
|
176
|
+
bindingToken: match[isBindingFirst ? 1 : 2],
|
|
177
|
+
groupToken: match[isBindingFirst ? 2 : 1],
|
|
178
|
+
accessDeclaration: match[3]?.trim(),
|
|
179
|
+
name: match[4]
|
|
180
|
+
});
|
|
181
|
+
match = regex.exec(maskedSource);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return matches.sort((left, right) => left.index - right.index);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function replaceWGSLBindingDeclarationMatches(
|
|
189
|
+
source: string,
|
|
190
|
+
regexes: readonly RegExp[],
|
|
191
|
+
replacer: (match: WGSLBindingDeclarationMatch) => string
|
|
192
|
+
): string {
|
|
193
|
+
const matches = getWGSLBindingDeclarationMatches(source, regexes);
|
|
194
|
+
if (!matches.length) {
|
|
195
|
+
return source;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let relocatedSource = '';
|
|
199
|
+
let lastIndex = 0;
|
|
200
|
+
|
|
201
|
+
for (const match of matches) {
|
|
202
|
+
relocatedSource += source.slice(lastIndex, match.index);
|
|
203
|
+
relocatedSource += replacer(match);
|
|
204
|
+
lastIndex = match.index + match.length;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
relocatedSource += source.slice(lastIndex);
|
|
208
|
+
return relocatedSource;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function hasWGSLAutoBinding(source: string): boolean {
|
|
212
|
+
return /@binding\(\s*auto\s*\)/.test(maskWGSLComments(source));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function getFirstWGSLAutoBindingDeclarationMatch(
|
|
216
|
+
source: string,
|
|
217
|
+
regexes: readonly RegExp[]
|
|
218
|
+
): WGSLBindingDeclarationMatch | undefined {
|
|
219
|
+
const autoBindingRegexes =
|
|
220
|
+
regexes === MODULE_WGSL_BINDING_DECLARATION_REGEXES ||
|
|
221
|
+
regexes === WGSL_BINDING_DECLARATION_REGEXES
|
|
222
|
+
? WGSL_AUTO_BINDING_DECLARATION_REGEXES
|
|
223
|
+
: regexes;
|
|
224
|
+
|
|
225
|
+
return getWGSLBindingDeclarationMatches(source, autoBindingRegexes).find(
|
|
226
|
+
declarationMatch => declarationMatch.bindingToken === 'auto'
|
|
227
|
+
);
|
|
228
|
+
}
|
|
@@ -21,25 +21,31 @@ function generateGLSLUniformDeclarations(
|
|
|
21
21
|
): string {
|
|
22
22
|
const glsl: string[] = [];
|
|
23
23
|
|
|
24
|
-
// => uniform UniformBlockName {
|
|
24
|
+
// => layout(std140) uniform UniformBlockName {
|
|
25
25
|
switch (options.uniforms) {
|
|
26
26
|
case 'scoped-interface-blocks':
|
|
27
27
|
case 'unscoped-interface-blocks':
|
|
28
|
-
glsl.push(`uniform ${capitalize(module.name)} {`);
|
|
28
|
+
glsl.push(`layout(std140) uniform ${capitalize(module.name)} {`);
|
|
29
29
|
break;
|
|
30
30
|
case 'uniforms':
|
|
31
31
|
// ignore
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
for (const [uniformName, uniformFormat] of Object.entries(module.uniformTypes || {})) {
|
|
35
|
-
|
|
35
|
+
if (typeof uniformFormat !== 'string') {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Composite uniform types are not supported by GLSL shader generation: ${module.name}.${uniformName}`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const glslUniformType = getGLSLUniformType(uniformFormat as UniformFormat);
|
|
36
42
|
switch (options.uniforms) {
|
|
37
43
|
case 'scoped-interface-blocks':
|
|
38
|
-
// => uniform UniformBlockName {
|
|
44
|
+
// => layout(std140) uniform UniformBlockName {
|
|
39
45
|
glsl.push(` ${glslUniformType} ${uniformName};`);
|
|
40
46
|
break;
|
|
41
47
|
case 'unscoped-interface-blocks':
|
|
42
|
-
// => uniform UniformBlockName {
|
|
48
|
+
// => layout(std140) uniform UniformBlockName {
|
|
43
49
|
glsl.push(` ${glslUniformType} ${module.name}_${uniformName};`);
|
|
44
50
|
break;
|
|
45
51
|
case 'uniforms':
|
|
@@ -23,6 +23,12 @@ export function generateWGSLUniformDeclarations(
|
|
|
23
23
|
wgsl.push(`struct ${capitalize(module.name)} {`);
|
|
24
24
|
|
|
25
25
|
for (const [uniformName, uniformFormat] of Object.entries(module?.uniformTypes || {})) {
|
|
26
|
+
if (typeof uniformFormat !== 'string') {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Composite uniform types are not supported by WGSL shader generation: ${module.name}.${uniformName}`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
const wgslUniformType = uniformFormat;
|
|
27
33
|
wgsl.push(` ${uniformName} : ${wgslUniformType};`);
|
|
28
34
|
}
|