-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathvite.slang.js
More file actions
50 lines (36 loc) · 1.55 KB
/
vite.slang.js
File metadata and controls
50 lines (36 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { minify } from './src/index.js';
import Slang from './slang-wasm.js';
const error = ({ type, message }) =>
console.error(`${type} error:`, message);
const slang = await Slang();
const globalSession = slang.createGlobalSession();
const target = slang.getCompileTargets().find(
// 'GLSL' target is also supported, but the output
// version is 4.5, which is not valid on the web.
({ name }) => name === 'WGSL'
)?.value;
export default (mode) => ({
include: ['**/*.slang'],
defaultExtension: 'slang',
importKeywords: ['import', '__include'],
onComplete: (shader, path, session) => {
session = globalSession.createSession(target);
const module = session.loadModuleFromSource(
shader, path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.')), path
);
!module && error(slang.getLastError());
const modules = Array.from({ length: module.getDefinedEntryPointCount() }).map((_, ep) => {
const componentType = session.createCompositeComponentType(
[module.getDefinedEntryPoint(ep), 1]
);
const { name, stage } = componentType.getLayout(0).toJsonObject().entryPoints[0];
const stageId = stage === 'vertex' ? 1 : stage === 'fragment' ? 5 : 6;
return module.findAndCheckEntryPoint(name, stageId);
});
modules.unshift(module);
shader = session.createCompositeComponentType(modules).link().getTargetCode(0);
!shader.length && error(slang.getLastError());
// Minify output shader only when building for production:
return mode === "production" ? minify(shader) : shader;
}
});