yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
dcdebc1a7
master
1// unit-test-generic-entrypoint.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 the compilation API for compiling a specialized generic entrypoint. 15 16SLANG_UNIT_TEST (genericEntryPointCompile ) 17{ 18const char * userSourceBody = R"( 19interface I { int getValue(); } 20struct X : I { int getValue() { return 100; } } 21float4 vertMain<T:I, int n, each U>(uniform T o) { 22return float4(o.getValue(), countof(U), n, 1); 23} 24)" ; 25ComPtr < slang::IGlobalSession > globalSession ; 26SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 27 slang::TargetDesc targetDesc = {}; 28targetDesc .format = SLANG_GLSL ; 29 slang::SessionDesc sessionDesc = {}; 30sessionDesc .targetCount = 1 ; 31sessionDesc .targets = & targetDesc ; 32ComPtr < slang::ISession > session ; 33SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 34 35ComPtr < slang::IBlob > diagnosticBlob ; 36auto module = session -> loadModuleFromSourceString ( 37"m" , 38"m.slang" , 39userSourceBody , 40diagnosticBlob .writeRef ()); 41SLANG_CHECK (module != nullptr ); 42 43// Test 1: Using findAndCheckEntryPoint to supply arguments in string form. 44 { 45ComPtr < slang::IEntryPoint > entryPoint ; 46module -> findAndCheckEntryPoint ( 47"vertMain<X, 7, int, float>" , 48SLANG_STAGE_VERTEX , 49entryPoint .writeRef (), 50diagnosticBlob .writeRef ()); 51SLANG_CHECK_ABORT (entryPoint != nullptr ); 52 slang::IComponentType * componentTypes [2 ]= {module ,entryPoint .get ()}; 53ComPtr < slang::IComponentType > composedProgram ; 54session -> createCompositeComponentType ( 55componentTypes , 562 , 57composedProgram .writeRef (), 58diagnosticBlob .writeRef ()); 59 60ComPtr < slang::IComponentType > linkedProgram ; 61composedProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 62 63ComPtr < slang::IBlob > code ; 64linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 65 66SLANG_CHECK ( 67UnownedStringSlice ((char * )code -> getBufferPointer ()) 68 .indexOf (toSlice ("vec4(float(X_getValue_0()), 2.0, 7.0, 1.0)" ))!= -1 ); 69 } 70 71// Test 2: Using `specialize` to supply arguments structurally with reflection types. 72 { 73ComPtr < slang::IEntryPoint > entryPoint ; 74module -> findAndCheckEntryPoint ( 75"vertMain" , 76SLANG_STAGE_VERTEX , 77entryPoint .writeRef (), 78diagnosticBlob .writeRef ()); 79SLANG_CHECK_ABORT (entryPoint != nullptr ); 80ComPtr < slang::IComponentType > specializedEntryPoint ; 81 slang::SpecializationArg args []= { 82 slang::SpecializationArg ::fromType (module -> getLayout ()-> findTypeByName ("X" )), 83 slang::SpecializationArg ::fromExpr ("8" ), 84 slang::SpecializationArg ::fromType (module -> getLayout ()-> findTypeByName ("int" )), 85 slang::SpecializationArg ::fromType (module -> getLayout ()-> findTypeByName ("float" ))}; 86 87entryPoint -> specialize (args ,4 ,specializedEntryPoint .writeRef (),nullptr ); 88SLANG_CHECK_ABORT (specializedEntryPoint != nullptr ); 89 slang::IComponentType * componentTypes [2 ]= {module ,specializedEntryPoint .get ()}; 90ComPtr < slang::IComponentType > composedProgram ; 91session -> createCompositeComponentType ( 92componentTypes , 932 , 94composedProgram .writeRef (), 95diagnosticBlob .writeRef ()); 96 97ComPtr < slang::IComponentType > linkedProgram ; 98composedProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 99 100ComPtr < slang::IBlob > code ; 101linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 102 103SLANG_CHECK ( 104UnownedStringSlice ((char * )code -> getBufferPointer ()) 105 .indexOf (toSlice ("vec4(float(X_getValue_0()), 2.0, 8.0, 1.0)" ))!= -1 ); 106 } 107 108// Test 3: corner case: specialize variadic param with 0 types. 109 { 110ComPtr < slang::IEntryPoint > entryPoint ; 111module -> findAndCheckEntryPoint ( 112"vertMain" , 113SLANG_STAGE_VERTEX , 114entryPoint .writeRef (), 115diagnosticBlob .writeRef ()); 116SLANG_CHECK_ABORT (entryPoint != nullptr ); 117ComPtr < slang::IComponentType > specializedEntryPoint ; 118 slang::SpecializationArg args []= { 119 slang::SpecializationArg ::fromType (module -> getLayout ()-> findTypeByName ("X" )), 120 slang::SpecializationArg ::fromExpr ("8" )}; 121 122entryPoint -> specialize (args ,2 ,specializedEntryPoint .writeRef (),nullptr ); 123SLANG_CHECK_ABORT (specializedEntryPoint != nullptr ); 124 slang::IComponentType * componentTypes [2 ]= {module ,specializedEntryPoint .get ()}; 125ComPtr < slang::IComponentType > composedProgram ; 126session -> createCompositeComponentType ( 127componentTypes , 1282 , 129composedProgram .writeRef (), 130diagnosticBlob .writeRef ()); 131 132ComPtr < slang::IComponentType > linkedProgram ; 133composedProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 134 135ComPtr < slang::IBlob > code ; 136linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 137 138SLANG_CHECK ( 139UnownedStringSlice ((char * )code -> getBufferPointer ()) 140 .indexOf (toSlice ("vec4(float(X_getValue_0()), 0.0, 8.0, 1.0)" ))!= -1 ); 141 } 142}