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 API supports discovering previously checked translation unit in the same 15// FrontEndCompileRequest. 16SLANG_UNIT_TEST (translationUnitImport ) 17{ 18// Source for the first translation unit. 19const char * generatedSource = "public int f() {" 20" return 5;" 21"};" ; 22 23// Source for the a file that imports the first translation unit. 24// The import should succeed and `f` should be visible to this module. 25const char * fileSource = 26R"( 27import generatedUnit; 28 29public int g(){ return f(); } 30)" ; 31 32// Source for a module that transitively uses the generated source via a file. 33const char * userSourceBody = R"( 34[shader("compute")] 35[numthreads(4,1,1)] 36void computeMain( 37uint3 sv_dispatchThreadID : SV_DispatchThreadID, 38uniform RWStructuredBuffer<int> buffer) 39{ 40buffer[sv_dispatchThreadID.x] = g(); 41})" ; 42 43auto moduleName = "moduleG" + String (Process ::getId ()); 44String userSource = "import " + moduleName + ";\n" + userSourceBody ; 45auto session = spCreateSession (); 46auto request = spCreateCompileRequest (session ); 47 48File ::writeAllText (moduleName + ".slang" ,fileSource ); 49 50spAddCodeGenTarget (request ,SLANG_HLSL ); 51int generatedTranslationUnitIndex = 52spAddTranslationUnit (request ,SLANG_SOURCE_LANGUAGE_SLANG ,"generatedUnit" ); 53spAddTranslationUnitSourceString ( 54request , 55generatedTranslationUnitIndex , 56"generatedFile" , 57generatedSource ); 58 59int entryPointTranslationUnitIndex = 60spAddTranslationUnit (request ,SLANG_SOURCE_LANGUAGE_SLANG ,"userUnit" ); 61spAddTranslationUnitSourceString ( 62request , 63entryPointTranslationUnitIndex , 64"userFile" , 65userSource .getUnownedSlice ().begin ()); 66spAddEntryPoint (request ,entryPointTranslationUnitIndex ,"computeMain" ,SLANG_STAGE_COMPUTE ); 67 68auto compileResult = spCompile (request ); 69SLANG_CHECK (compileResult == SLANG_OK ); 70 71Slang ::ComPtr < ISlangBlob > outBlob ; 72spGetEntryPointCodeBlob (request ,0 ,0 ,outBlob .writeRef ()); 73SLANG_CHECK (outBlob && outBlob -> getBufferSize ()!= 0 ); 74 75spDestroyCompileRequest (request ); 76spDestroySession (session ); 77File ::remove (moduleName + ".slang" ); 78}