yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
114c9766c
master
1// unit-test-module-ptr.cpp 2 3#include "core/slang-memory-file-system.h" 4#include "slang-com-ptr.h" 5#include "slang.h" 6#include "unit-test/slang-unit-test.h" 7 8#include <stdio.h> 9#include <stdlib.h> 10 11using namespace Slang ; 12 13SLANG_UNIT_TEST (modulePtr ) 14{ 15const char * testModuleSource = R"( 16module test_module; 17 18public void atomicFunc(__ref Atomic<int> ptr) { 19ptr.add(1); 20} 21)" ; 22 23const char * testSource = R"( 24import "test_module"; 25 26RWStructuredBuffer<Atomic<int>> input0; 27 28[shader("compute")] 29[numthreads(1,1,1)] 30void computeMain(uint3 workGroup : SV_GroupID) 31{ 32atomicFunc(input0[0]); 33} 34)" ; 35ComPtr < ISlangMutableFileSystem > memoryFileSystem = 36ComPtr < ISlangMutableFileSystem > (new Slang ::MemoryFileSystem ()); 37 38ComPtr < slang::IGlobalSession > globalSession ; 39SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 40 slang::TargetDesc targetDesc = {}; 41targetDesc .format = SLANG_SPIRV ; 42targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 43 slang::SessionDesc sessionDesc = {}; 44sessionDesc .targetCount = 1 ; 45sessionDesc .targets = & targetDesc ; 46sessionDesc .compilerOptionEntryCount = 0 ; 47sessionDesc .fileSystem = memoryFileSystem ; 48 49// Precompile test_module to file. 50 { 51ComPtr < slang::ISession > session ; 52SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 53 54ComPtr < slang::IBlob > diagnosticBlob ; 55auto module = session -> loadModuleFromSourceString ( 56"test_module" , 57"test_module.slang" , 58testModuleSource , 59diagnosticBlob .writeRef ()); 60SLANG_CHECK (module != nullptr ); 61 62ComPtr < slang::IBlob > moduleBlob ; 63module -> serialize (moduleBlob .writeRef ()); 64memoryFileSystem -> saveFile ( 65"test_module.slang-module" , 66moduleBlob -> getBufferPointer (), 67moduleBlob -> getBufferSize ()); 68 } 69 70// compile test. 71 { 72ComPtr < slang::ISession > session ; 73SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 74 75ComPtr < slang::IBlob > diagnosticBlob ; 76auto module = session -> loadModuleFromSourceString ( 77"test" , 78"test.slang" , 79testSource , 80diagnosticBlob .writeRef ()); 81SLANG_CHECK (module != nullptr ); 82 83ComPtr < slang::IComponentType > linkedProgram ; 84module -> link (linkedProgram .writeRef ()); 85 86ComPtr < slang::IBlob > code ; 87 88linkedProgram -> getTargetCode (0 ,code .writeRef (),diagnosticBlob .writeRef ()); 89 90SLANG_CHECK (code -> getBufferSize ()> 0 ); 91 } 92}