yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
af27de015
master
1// unit-test-ir-blob.cpp 2 3#include "slang-com-ptr.h" 4#include "slang.h" 5#include "unit-test/slang-unit-test.h" 6 7#include <stdio.h> 8#include <stdlib.h> 9#include <string.h> 10 11using namespace Slang ; 12 13// Test the slang_loadModuleFromIRBlob and slang_loadModuleInfoFromIRBlob functions 14SLANG_UNIT_TEST (irBlob ) 15{ 16// Test source code for creating IR data 17const char * testModuleSource = R"( 18module test_ir_module; 19 20public struct TestStruct { 21float x, y, z; 22} 23 24public void testFunction(TestStruct input) { 25// Simple function 26} 27 28public static const float PI = 3.14159; 29)" ; 30 31ComPtr < slang::IGlobalSession > globalSession ; 32SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 33 34 slang::SessionDesc sessionDesc = {}; 35sessionDesc .targetCount = 1 ; 36 slang::TargetDesc targetDesc = {}; 37targetDesc .format = SLANG_SPIRV ; 38targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 39sessionDesc .targets = & targetDesc ; 40 41ComPtr < slang::ISession > session ; 42SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 43 44// Create IR data by serializing a module 45ComPtr < ISlangBlob > irBlob ; 46 { 47ComPtr < slang::IModule > module ; 48ComPtr < ISlangBlob > diagnostics ; 49 50module = session -> loadModuleFromSourceString ( 51"test_ir_module" , 52"test_ir_module.slang" , 53testModuleSource , 54diagnostics .writeRef ()); 55 56SLANG_CHECK (module != nullptr ); 57if (diagnostics && diagnostics -> getBufferSize ()> 0 ) 58 { 59// Log diagnostics if any 60printf ( 61"Module compilation diagnostics: %.*s\n" , 62 (int )diagnostics -> getBufferSize (), 63 (const char * )diagnostics -> getBufferPointer ()); 64 } 65 66// Serialize the module to create IR data 67SLANG_CHECK (module -> serialize (irBlob .writeRef ())== SLANG_OK ); 68SLANG_CHECK (irBlob != nullptr ); 69SLANG_CHECK (irBlob -> getBufferSize ()> 0 ); 70 } 71 72// Test 1: Test slang_loadModuleFromIRBlob with valid IR data 73 { 74ComPtr < slang::IModule > loadedModule ; 75ComPtr < ISlangBlob > diagnostics ; 76 77loadedModule = slang_loadModuleFromIRBlob ( 78session , 79"test_ir_module_loaded" , 80"test_ir_module_loaded.slang" , 81irBlob -> getBufferPointer (), 82irBlob -> getBufferSize (), 83diagnostics .writeRef ()); 84 85SLANG_CHECK (loadedModule != nullptr ); 86if (diagnostics && diagnostics -> getBufferSize ()> 0 ) 87 { 88// Log diagnostics if any 89printf ( 90"IR blob loading diagnostics: %.*s\n" , 91 (int )diagnostics -> getBufferSize (), 92 (const char * )diagnostics -> getBufferPointer ()); 93 } 94 95// Verify the loaded module is valid 96SLANG_CHECK (loadedModule != nullptr ); 97 } 98 99// Test 2: Test slang_loadModuleInfoFromIRBlob with valid IR data 100 { 101SlangInt moduleVersion ; 102const char * moduleCompilerVersion ; 103const char * moduleName ; 104 105SlangResult result = slang_loadModuleInfoFromIRBlob ( 106session , 107irBlob -> getBufferPointer (), 108irBlob -> getBufferSize (), 109moduleVersion , 110moduleCompilerVersion , 111moduleName ); 112 113SLANG_CHECK (result == SLANG_OK ); 114SLANG_CHECK (moduleName != nullptr ); 115SLANG_CHECK (strcmp (moduleName ,"test_ir_module" )== 0 ); 116SLANG_CHECK (moduleCompilerVersion != nullptr ); 117SLANG_CHECK (moduleVersion >=0 ); 118 } 119 120// Test 3: Test slang_loadModuleFromIRBlob with invalid parameters 121 { 122ComPtr < slang::IModule > module ; 123ComPtr < ISlangBlob > diagnostics ; 124 125// Test with null session 126module = slang_loadModuleFromIRBlob ( 127nullptr , 128"testModule" , 129"test.slang" , 130irBlob -> getBufferPointer (), 131irBlob -> getBufferSize (), 132diagnostics .writeRef ()); 133 134SLANG_CHECK (module == nullptr ); 135 136// Test with null moduleName 137module = slang_loadModuleFromIRBlob ( 138session , 139nullptr , 140"test.slang" , 141irBlob -> getBufferPointer (), 142irBlob -> getBufferSize (), 143diagnostics .writeRef ()); 144 145SLANG_CHECK (module == nullptr ); 146 147// Test with null path 148module = slang_loadModuleFromIRBlob ( 149session , 150"testModule" , 151nullptr , 152irBlob -> getBufferPointer (), 153irBlob -> getBufferSize (), 154diagnostics .writeRef ()); 155 156SLANG_CHECK (module == nullptr ); 157 158// Test with null source 159module = slang_loadModuleFromIRBlob ( 160session , 161"testModule" , 162"test.slang" , 163nullptr , 164irBlob -> getBufferSize (), 165diagnostics .writeRef ()); 166 167SLANG_CHECK (module == nullptr ); 168 169// Test with zero size 170module = slang_loadModuleFromIRBlob ( 171session , 172"testModule" , 173"test.slang" , 174irBlob -> getBufferPointer (), 1750 , 176diagnostics .writeRef ()); 177 178SLANG_CHECK (module == nullptr ); 179 } 180 181// Test 4: Test slang_loadModuleInfoFromIRBlob with invalid parameters 182 { 183SlangInt moduleVersion ; 184const char * moduleCompilerVersion ; 185const char * moduleName ; 186 187// Test with null session 188SlangResult result = slang_loadModuleInfoFromIRBlob ( 189nullptr , 190irBlob -> getBufferPointer (), 191irBlob -> getBufferSize (), 192moduleVersion , 193moduleCompilerVersion , 194moduleName ); 195 196SLANG_CHECK (result == SLANG_E_INVALID_ARG ); 197 198// Test with null source 199result = slang_loadModuleInfoFromIRBlob ( 200session , 201nullptr , 202irBlob -> getBufferSize (), 203moduleVersion , 204moduleCompilerVersion , 205moduleName ); 206 207SLANG_CHECK (result == SLANG_E_INVALID_ARG ); 208 209// Test with zero size 210result = slang_loadModuleInfoFromIRBlob ( 211session , 212irBlob -> getBufferPointer (), 2130 , 214moduleVersion , 215moduleCompilerVersion , 216moduleName ); 217 218SLANG_CHECK (result == SLANG_E_INVALID_ARG ); 219 } 220 221// Test 5: Test with corrupted/invalid IR data 222 { 223ComPtr < slang::IModule > module ; 224ComPtr < ISlangBlob > diagnostics ; 225 226// Create some invalid data 227const char * invalidData = "This is not valid IR data" ; 228size_t invalidDataSize = strlen (invalidData ); 229 230module = slang_loadModuleFromIRBlob ( 231session , 232"testModule" , 233"test.slang" , 234invalidData , 235invalidDataSize , 236diagnostics .writeRef ()); 237 238// This might return nullptr or a module with diagnostics 239if (module == nullptr ) 240 { 241// If it failed, that's expected for invalid data 242SLANG_CHECK (true); 243 } 244else 245 { 246// If it succeeded, there should be diagnostics 247if (diagnostics && diagnostics -> getBufferSize ()> 0 ) 248 { 249SLANG_CHECK (true); 250 } 251 } 252 } 253 254// Test 6: Test slang_loadModuleInfoFromIRBlob with corrupted/invalid IR data 255 { 256SlangInt moduleVersion ; 257const char * moduleCompilerVersion ; 258const char * moduleName ; 259 260// Create some invalid data 261const char * invalidData = "This is not valid IR data" ; 262size_t invalidDataSize = strlen (invalidData ); 263 264SlangResult result = slang_loadModuleInfoFromIRBlob ( 265session , 266invalidData , 267invalidDataSize , 268moduleVersion , 269moduleCompilerVersion , 270moduleName ); 271 272// This should fail with invalid data 273SLANG_CHECK (result != SLANG_OK ); 274 } 275 276// Test 7: Test round-trip serialization and loading 277 { 278// Load the module from IR 279ComPtr < slang::IModule > loadedModule ; 280ComPtr < ISlangBlob > diagnostics ; 281 282loadedModule = slang_loadModuleFromIRBlob ( 283session , 284"test_round_trip" , 285"test_round_trip.slang" , 286irBlob -> getBufferPointer (), 287irBlob -> getBufferSize (), 288diagnostics .writeRef ()); 289 290SLANG_CHECK (loadedModule != nullptr ); 291 292if (loadedModule ) 293 { 294// Serialize the loaded module again 295ComPtr < ISlangBlob > roundTripBlob ; 296SLANG_CHECK (loadedModule -> serialize (roundTripBlob .writeRef ())== SLANG_OK ); 297SLANG_CHECK (roundTripBlob != nullptr ); 298SLANG_CHECK (roundTripBlob -> getBufferSize ()> 0 ); 299 300// Load it again 301ComPtr < slang::IModule > roundTripModule ; 302roundTripModule = slang_loadModuleFromIRBlob ( 303session , 304"test_round_trip_2" , 305"test_round_trip_2.slang" , 306roundTripBlob -> getBufferPointer (), 307roundTripBlob -> getBufferSize (), 308diagnostics .writeRef ()); 309 310SLANG_CHECK (roundTripModule != nullptr ); 311 } 312 } 313 314// Test 8: Test multiple modules with different IR data 315 { 316// Create a second module with different content 317const char * testModuleSource2 = R"( 318module test_ir_module_2; 319 320public struct AnotherStruct { 321int a, b, c; 322} 323 324public void anotherFunction(AnotherStruct input) { 325// Another function 326} 327)" ; 328 329ComPtr < slang::IModule > module2 ; 330ComPtr < ISlangBlob > diagnostics2 ; 331ComPtr < ISlangBlob > irBlob2 ; 332 333module2 = session -> loadModuleFromSourceString ( 334"test_ir_module_2" , 335"test_ir_module_2.slang" , 336testModuleSource2 , 337diagnostics2 .writeRef ()); 338 339SLANG_CHECK (module2 != nullptr ); 340SLANG_CHECK (module2 -> serialize (irBlob2 .writeRef ())== SLANG_OK ); 341 342// Load both modules 343ComPtr < slang::IModule > loadedModule1 ; 344ComPtr < slang::IModule > loadedModule2 ; 345ComPtr < ISlangBlob > diagnostics ; 346 347loadedModule1 = slang_loadModuleFromIRBlob ( 348session , 349"test_ir_module_1_loaded" , 350"test_ir_module_1_loaded.slang" , 351irBlob -> getBufferPointer (), 352irBlob -> getBufferSize (), 353diagnostics .writeRef ()); 354 355loadedModule2 = slang_loadModuleFromIRBlob ( 356session , 357"test_ir_module_2_loaded" , 358"test_ir_module_2_loaded.slang" , 359irBlob2 -> getBufferPointer (), 360irBlob2 -> getBufferSize (), 361diagnostics .writeRef ()); 362 363SLANG_CHECK (loadedModule1 != nullptr ); 364SLANG_CHECK (loadedModule2 != nullptr ); 365 366// Verify both modules loaded successfully 367SLANG_CHECK (loadedModule1 != nullptr ); 368SLANG_CHECK (loadedModule2 != nullptr ); 369 } 370}