yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
c7c481614
master
1// unit-test-function-lookup-resolution.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 14static String getTypeFullName (slang::TypeReflection * type ) 15{ 16ComPtr < ISlangBlob > blob ; 17type -> getFullName (blob .writeRef ()); 18return String ((const char * )blob -> getBufferPointer ()); 19} 20 21// Test that the reflection API provides correctly resolved lookup results. 22 23SLANG_UNIT_TEST (functionLookupResolution ) 24{ 25// Source for a module that contains an undecorated entrypoint. 26const char * userSourceBody = R"( 27public interface IBase 28{ 29public void step(inout float f); 30public void method(int x) {} 31} 32 33public struct Impl : IBase 34{ 35public void step(inout float f) 36{ 37f += 1.0f; 38} 39public override void method(int x) {} 40} 41public extension<T : IBase> T { 42public void method(int x) {} 43} 44)" ; 45 46auto moduleName = "moduleG" + String (Process ::getId ()); 47String userSource = "import " + moduleName + ";\n" + userSourceBody ; 48ComPtr < slang::IGlobalSession > globalSession ; 49SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 50 slang::TargetDesc targetDesc = {}; 51targetDesc .format = SLANG_HLSL ; 52targetDesc .profile = globalSession -> findProfile ("sm_5_0" ); 53 slang::SessionDesc sessionDesc = {}; 54sessionDesc .targetCount = 1 ; 55sessionDesc .targets = & targetDesc ; 56ComPtr < slang::ISession > session ; 57SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 58 59ComPtr < slang::IBlob > diagnosticBlob ; 60auto module = session -> loadModuleFromSourceString ( 61"m" , 62"m.slang" , 63userSourceBody , 64diagnosticBlob .writeRef ()); 65SLANG_CHECK (module != nullptr ); 66 67auto layout = module -> getLayout (); 68auto type = layout -> findTypeByName ("Impl" ); 69SLANG_CHECK_ABORT (type != nullptr ); 70 71auto func = layout -> findFunctionByNameInType (type ,"step" ); 72SLANG_CHECK_ABORT (func && !func -> isOverloaded ()); 73 74 75auto func1 = layout -> findFunctionByNameInType (type ,"method" ); 76SLANG_CHECK_ABORT (func1 -> isOverloaded ()); 77SLANG_CHECK (func1 -> getOverloadCount ()== 3 ); 78// Test that overloaded function containers return the correct name 79SLANG_CHECK (func1 -> getName ()!= nullptr ); 80SLANG_CHECK (String (func1 -> getName ())== "method" ); 81if (func1 -> isOverloaded ()) 82 { 83List < slang::FunctionReflection *> candidates ; 84for (uint32_t i = 0 ;i < func1 -> getOverloadCount ();i ++ ) 85 { 86candidates .add (func1 -> getOverload (i )); 87 } 88func1 = layout -> tryResolveOverloadedFunction ( 89 (uint32_t )candidates .getCount (), 90candidates .getBuffer ()); 91 } 92SLANG_CHECK (!func1 -> isOverloaded ()); 93SLANG_CHECK (String (func1 -> getName ())== "method" ); 94}