yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
9972a5269
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 reflection API provides correct info about attributes. 15 16SLANG_UNIT_TEST (attributeReflection ) 17{ 18const char * userSourceBody = R"( 19public enum E 20{ 21V0, 22V1, 23}; 24 25[__AttributeUsage(_AttributeTargets.Struct)] 26public struct NormalTextureAttribute 27{ 28public E Type; 29public float x; 30}; 31 32[COM("042BE50B-CB01-4DBB-8367-3A9CDCBE2F49")] 33interface IInterface { void f(); } 34 35[NormalTexture(E.V1, 6)] 36struct TS {}; 37)" ; 38String userSource = userSourceBody ; 39ComPtr < slang::IGlobalSession > globalSession ; 40SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 41 slang::TargetDesc targetDesc = {}; 42targetDesc .format = SLANG_HLSL ; 43targetDesc .profile = globalSession -> findProfile ("sm_5_0" ); 44 slang::SessionDesc sessionDesc = {}; 45sessionDesc .targetCount = 1 ; 46sessionDesc .targets = & targetDesc ; 47ComPtr < slang::ISession > session ; 48SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 49 50ComPtr < slang::IBlob > diagnosticBlob ; 51auto module = session -> loadModuleFromSourceString ( 52"m" , 53"m.slang" , 54userSourceBody , 55diagnosticBlob .writeRef ()); 56SLANG_CHECK (module != nullptr ); 57 58auto reflection = module -> getLayout (); 59 60auto interfaceType = reflection -> findTypeByName ("IInterface" ); 61SLANG_CHECK (interfaceType != nullptr ); 62 63auto comAttribute = interfaceType -> findAttributeByName ("COM" ); 64SLANG_CHECK (comAttribute != nullptr ); 65 66size_t size = 0 ; 67auto guid = comAttribute -> getArgumentValueString (0 ,& size ); 68UnownedStringSlice stringSlice = UnownedStringSlice (guid ,size ); 69SLANG_CHECK (stringSlice == "042BE50B-CB01-4DBB-8367-3A9CDCBE2F49" ); 70 71auto testType = reflection -> findTypeByName ("TS" ); 72SLANG_CHECK (testType != nullptr ); 73 74auto normalTextureAttribute = testType -> findAttributeByName ("NormalTexture" ); 75SLANG_CHECK (normalTextureAttribute != nullptr ); 76 77int value = 0 ; 78normalTextureAttribute -> getArgumentValueInt (0 ,& value ); 79SLANG_CHECK (value == 1 ); 80 81float fvalue = 0 ; 82normalTextureAttribute -> getArgumentValueFloat (1 ,& fvalue ); 83SLANG_CHECK (fvalue == 6.0 ); 84}