yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Jay KwakSmoke test WASM as a part of CI (#6969)15fce7c8f

master
4.4 KiB116 linesraw
1import * as wasmModule from '../slang-wasm.js';
2import { readFileSync } from 'fs';
3import { resolve, basename } from 'path';
4
5async function runSmokeTest() {
6    try {
7        // Get the file path from command line arguments
8        const filePath = process.argv[2];
9        const entryPointName = process.argv[3];
10        if (!filePath || !entryPointName) {
11            console.error('Please provide a path to a .slang file and an entry point name');
12            console.error('Usage: node test/smoke-test.js <path-to-slang-file> <entry-point-name>');
13            process.exit(1);
14        }
15
16        console.log(`Starting Slang WASM smoke test with file: ${filePath} and entry point: ${entryPointName}`);
17        
18        // Read the source file
19        const absolutePath = resolve(filePath);
20        const source = readFileSync(absolutePath, 'utf8');
21        const fileName = basename(filePath);
22        
23        // Load the WASM module
24        const module = await wasmModule.default();
25        console.log('WASM module loaded successfully');
26
27        // Print available compile targets
28        const targets = module.getCompileTargets();
29        if (!targets) {
30            throw new Error('Failed to get compile targets');
31        }
32        console.log('Available compile targets:', JSON.stringify(targets, null, 2));
33
34        // Find SPIRV target value
35        const spirvTarget = targets.findIndex(target => target.name.toLowerCase() === 'spirv');
36        if (spirvTarget === -1) {
37            throw new Error('SPIRV target not found in available targets');
38        }
39        console.log('Found SPIRV target at index:', spirvTarget);
40
41        // Get the actual SPIRV target value
42        const spirvTargetValue = targets[spirvTarget].value;
43        console.log('SPIRV target value:', spirvTargetValue);
44
45        // Create a global session
46        const globalSession = module.createGlobalSession();
47        if (!globalSession) {
48            throw new Error('Failed to create global session');
49        }
50        console.log('Global session created');
51
52        // Create a session with SPIRV as the target
53        const session = globalSession.createSession(spirvTargetValue);
54        if (!session) {
55            throw new Error('Failed to create session');
56        }
57        console.log('Session created with SPIRV target');
58
59        // Load the shader source
60        const module1 = session.loadModuleFromSource(source, fileName, '');
61        if (!module1) {
62            const error = module.getLastError();
63            throw new Error(`Failed to load module: ${error ? error.message : 'Unknown error'}`);
64        }
65        console.log('Shader module loaded');
66
67        // Check for compilation errors
68        const error = module.getLastError();
69        if (error && error.result !== module.SLANG_OK) {
70            throw new Error(`Compilation failed: ${error.message}`);
71        }
72        console.log('No compilation errors found');
73
74        // Try to find the entry point
75        const entryPoint = module1.findEntryPointByName(entryPointName);
76        if (!entryPoint) {
77            throw new Error(`Could not find entry point "${entryPointName}"`);
78        }
79        console.log(`Entry point "${entryPointName}" found`);
80
81        // Create and link the program
82        const program = session.createCompositeComponentType([module1]);
83        if (!program) {
84            throw new Error('Failed to create composite component type');
85        }
86        const linkedProgram = program.link();
87        if (!linkedProgram) {
88            throw new Error('Failed to link program');
89        }
90        console.log('Program created and linked successfully');
91
92        // Try to get the SPIRV code
93        console.log('\nTrying to generate SPIRV code:');
94        const spirvBinary = linkedProgram.getTargetCodeBlob(0); // 0 is the target index
95        if (!spirvBinary) {
96            throw new Error('Could not generate SPIRV binary');
97        }
98        console.log('SPIRV binary generated successfully');
99        console.log('Generated binary length:', spirvBinary.length);
100        
101        // Clean up
102        linkedProgram.delete();
103        program.delete();
104        entryPoint.delete();
105        module1.delete();
106        session.delete();
107        globalSession.delete();
108        console.log('Smoke test completed successfully');
109        process.exit(0); // Explicit success exit code
110    } catch (error) {
111        console.error('Smoke test failed:', error);
112        process.exit(1); // Error exit code
113    }
114}
115
116runSmokeTest();