yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
1// unit-test-translation-unit-import.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 IComponentType::getTargetCode API supports 15// compiling a program with multiple entrypoints and retrieving a single 16// compiled module that contains all the entrypoints. 17// 18SLANG_UNIT_TEST (getTargetCode ) 19{ 20// Source for a module that contains an undecorated entrypoint. 21const char * userSourceBody = R"( 22[shader("fragment")] 23float4 fragMain(float4 pos:SV_Position) : SV_Target 24{ 25return pos; 26} 27[shader("vertex")] 28float4 vertMain(float4 pos) : SV_Position 29{ 30return pos; 31} 32)" ; 33 34String userSource = userSourceBody ; 35ComPtr < slang::IGlobalSession > globalSession ; 36SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 37 slang::TargetDesc targetDesc = {}; 38// Request SPIR-V disassembly so we can check the content. 39targetDesc .format = SLANG_SPIRV_ASM ; 40targetDesc .profile = globalSession -> findProfile ("sm_5_0" ); 41 slang::SessionDesc sessionDesc = {}; 42sessionDesc .targetCount = 1 ; 43sessionDesc .targets = & targetDesc ; 44 45ComPtr < slang::ISession > session ; 46SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 47 48ComPtr < slang::IBlob > diagnosticBlob ; 49auto module = session -> loadModuleFromSourceString ( 50"m" , 51"m.slang" , 52userSourceBody , 53diagnosticBlob .writeRef ()); 54SLANG_CHECK (module != nullptr ); 55 56ComPtr < slang::IComponentType > linkedProgram ; 57module -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 58SLANG_CHECK (linkedProgram != nullptr ); 59 60ComPtr < slang::IBlob > code ; 61linkedProgram -> getTargetCode (0 ,code .writeRef (),diagnosticBlob .writeRef ()); 62SLANG_CHECK (code != nullptr ); 63 64SLANG_CHECK (code -> getBufferSize ()!= 0 ); 65 66UnownedStringSlice resultStr = UnownedStringSlice ((char * )code -> getBufferPointer ()); 67 68// Make sure the spirv disassembly contains both entrypoint names. 69SLANG_CHECK (resultStr .indexOf (toSlice ("fragMain" ))!= -1 ); 70SLANG_CHECK (resultStr .indexOf (toSlice ("vertMain" ))!= -1 ); 71}