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