yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
9c8fab83a
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 separate debug info is generated when requested and contains the 15// correct instructions. 16 17SLANG_UNIT_TEST (separateDebug ) 18{ 19// Source for a basic slang shader to compile to spirv. 20const char * userSourceBody = R"( 21struct TestType 22{ 23float memberA; 24float3 memberB; 25RWStructuredBuffer<float> memberC; 26float getValue() 27{ 28return memberA; 29} 30} 31RWStructuredBuffer<float> result; 32void main() 33{ 34TestType t; 35t.memberA = 1.0; 36t.memberB = float3(1, 2, 3); 37t.memberC = result; 38var val = t.getValue(); 39result[0] = val + t.memberB.x; 40} 41)" ; 42String userSource = userSourceBody ; 43ComPtr < slang::IGlobalSession > globalSession ; 44SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 45 46// Setup the target descriptor. 47 slang::TargetDesc targetDesc = {}; 48targetDesc .format = SLANG_SPIRV_ASM ; 49targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 50 51// Setup the session descriptor. 52 slang::SessionDesc sessionDesc = {}; 53sessionDesc .targetCount = 1 ; 54sessionDesc .targets = & targetDesc ; 55 56// Set the compile options. 57 slang::CompilerOptionEntry compilerOptions [2 ]; 58compilerOptions [0 ].name = slang::CompilerOptionName ::DebugInformation ; 59compilerOptions [0 ].value .kind = slang::CompilerOptionValueKind ::Int ; 60compilerOptions [0 ].value .intValue0 = 2 ; 61 62compilerOptions [1 ].name = slang::CompilerOptionName ::EmitSeparateDebug ; 63compilerOptions [1 ].value .kind = slang::CompilerOptionValueKind ::Int ; 64compilerOptions [1 ].value .intValue0 = 1 ; 65 66sessionDesc .compilerOptionEntries = compilerOptions ; 67sessionDesc .compilerOptionEntryCount = 2 ; 68 69ComPtr < slang::ISession > session ; 70SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 71 72// Compile the module. 73ComPtr < slang::IBlob > diagnosticBlob ; 74auto module = session -> loadModuleFromSourceString ( 75"m" , 76"m.slang" , 77userSourceBody , 78diagnosticBlob .writeRef ()); 79SLANG_CHECK (module != nullptr ); 80 81ComPtr < slang::IEntryPoint > entryPoint ; 82module -> findAndCheckEntryPoint ( 83"main" , 84SLANG_STAGE_COMPUTE , 85entryPoint .writeRef (), 86diagnosticBlob .writeRef ()); 87SLANG_CHECK (entryPoint != nullptr ); 88 89ComPtr < slang::IComponentType > compositeProgram ; 90 slang::IComponentType * components []= {module ,entryPoint .get ()}; 91session -> createCompositeComponentType ( 92components , 932 , 94compositeProgram .writeRef (), 95diagnosticBlob .writeRef ()); 96SLANG_CHECK (compositeProgram != nullptr ); 97 98ComPtr < slang::IComponentType > linkedProgram ; 99compositeProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 100SLANG_CHECK (linkedProgram != nullptr ); 101 102// Use getEntryPointCompileResult to get the base and debug spirv, and metadata 103// containing the debug build identifier. 104ComPtr < slang::IBlob > code ; 105ComPtr < slang::IBlob > debugCode ; 106ComPtr < slang::IMetadata > metadata ; 107const char * debugBuildIdentifier = nullptr ; 108 109// IComponentType2 is a separate interface from IComponentType, so we need 110// to query for it, and then use it to get the results. 111ComPtr < slang::ICompileResult > compileResult ; 112ComPtr < slang::IComponentType2 > linkedProgram2 ; 113SLANG_CHECK ( 114linkedProgram -> queryInterface (SLANG_IID_PPV_ARGS (linkedProgram2 .writeRef ()))== SLANG_OK ); 115auto result = linkedProgram2 -> getEntryPointCompileResult ( 1160 , 1170 , 118compileResult .writeRef (), 119diagnosticBlob .writeRef ()); 120SLANG_CHECK (result == SLANG_OK ); 121SLANG_CHECK (compileResult != nullptr ); 122SLANG_CHECK (compileResult -> getItemCount ()== 2 ); 123SLANG_CHECK (compileResult -> getItemData (0 ,code .writeRef ())== SLANG_OK ); 124SLANG_CHECK (compileResult -> getItemData (1 ,debugCode .writeRef ())== SLANG_OK ); 125SLANG_CHECK (compileResult -> getMetadata (metadata .writeRef ())== SLANG_OK ); 126 127debugBuildIdentifier = metadata -> getDebugBuildIdentifier (); 128SLANG_CHECK (debugBuildIdentifier != nullptr ); 129 130// Get the data for the stripped SPIRV. 131// This is already verified by the separate-debug.slang test but we 132// check it here to again to verify that the API is working. 133String codeString = static_cast < const char *> (code -> getBufferPointer ()); 134 135// Verify that the code contains DebugBuildIdentifier instruction 136// and the correct hash. 137SLANG_CHECK (codeString .indexOf ("DebugBuildIdentifier" )!= -1 ); 138SLANG_CHECK (codeString .indexOf (debugBuildIdentifier )!= -1 ); 139 140// Verify that it does not contain any other debug instructions. 141SLANG_CHECK (codeString .indexOf ("DebugExpression" )== -1 ); 142SLANG_CHECK (codeString .indexOf ("DebugTypeMember" )== -1 ); 143SLANG_CHECK (codeString .indexOf ("DebugScope" )== -1 ); 144SLANG_CHECK (codeString .indexOf ("DebugLine" )== -1 ); 145 146// Get the data for the non-stripped debug SPIRV and verify 147// that it contains the correct debug instructions. 148String debugCodeString = static_cast < const char *> (debugCode -> getBufferPointer ()); 149SLANG_CHECK (debugCodeString .indexOf ("DebugBuildIdentifier" )!= -1 ); 150SLANG_CHECK (debugCodeString .indexOf (debugBuildIdentifier )!= -1 ); 151SLANG_CHECK (debugCodeString .indexOf ("DebugExpression" )!= -1 ); 152SLANG_CHECK (debugCodeString .indexOf ("DebugFunctionDefinition" )!= -1 ); 153SLANG_CHECK (debugCodeString .indexOf ("DebugScope" )!= -1 ); 154SLANG_CHECK (debugCodeString .indexOf ("DebugLine" )!= -1 ); 155}