yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
1// unit-test-find-entrypoint-nested.cpp 2 3#include "../../source/core/slang-io.h" 4#include "../../source/core/slang-process.h" 5#include "slang-com-ptr.h" 6#include "slang.h" 7#include "unit-test/slang-unit-test.h" 8 9#include <stdio.h> 10#include <stdlib.h> 11 12using namespace Slang ; 13 14// Test that the IModule::findAndCheckEntryPoint API works with modules that 15// defines two entrypoints, where one entrypoint calls the other. 16 17SLANG_UNIT_TEST (findEntryPointNested ) 18{ 19// Source for a module that contains an undecorated entrypoint. 20const char * userSourceBody = R"( 21[shader("raygeneration")] 22void inner() 23{ 24AllMemoryBarrier(); 25} 26[shader("raygeneration")] 27void outer() 28{ 29inner(); 30} 31)" ; 32 33auto moduleName = "moduleG" + String (Process ::getId ()); 34String userSource = "import " + moduleName + ";\n" + userSourceBody ; 35ComPtr < slang::IGlobalSession > globalSession ; 36SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 37 slang::TargetDesc targetDesc = {}; 38targetDesc .format = SLANG_SPIRV ; 39targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 40 slang::SessionDesc sessionDesc = {}; 41sessionDesc .targetCount = 1 ; 42sessionDesc .targets = & targetDesc ; 43sessionDesc .compilerOptionEntryCount = 1 ; 44 slang::CompilerOptionEntry compilerOptionEntry = {}; 45compilerOptionEntry .name = slang::CompilerOptionName ::EmitSpirvViaGLSL ; 46compilerOptionEntry .value .kind = slang::CompilerOptionValueKind ::Int ; 47compilerOptionEntry .value .intValue0 = 1 ; 48sessionDesc .compilerOptionEntries = & compilerOptionEntry ; 49 50ComPtr < slang::ISession > session ; 51SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 52 53ComPtr < slang::IBlob > diagnosticBlob ; 54auto module = session -> loadModuleFromSourceString ( 55"m" , 56"m.slang" , 57userSourceBody , 58diagnosticBlob .writeRef ()); 59SLANG_CHECK (module != nullptr ); 60 61ComPtr < slang::IEntryPoint > entryPoint ; 62module -> findAndCheckEntryPoint ( 63"outer" , 64SLANG_STAGE_RAY_GENERATION , 65entryPoint .writeRef (), 66diagnosticBlob .writeRef ()); 67SLANG_CHECK (entryPoint != nullptr ); 68 69ComPtr < slang::IComponentType > compositeProgram ; 70 slang::IComponentType * components []= {module ,entryPoint .get ()}; 71session -> createCompositeComponentType ( 72components , 732 , 74compositeProgram .writeRef (), 75diagnosticBlob .writeRef ()); 76SLANG_CHECK (compositeProgram != nullptr ); 77 78ComPtr < slang::IComponentType > linkedProgram ; 79compositeProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 80SLANG_CHECK (linkedProgram != nullptr ); 81 82ComPtr < slang::IBlob > code ; 83linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 84SLANG_CHECK (code != nullptr ); 85SLANG_CHECK (code -> getBufferSize ()!= 0 ); 86}