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 IModule::findAndCheckEntryPoint API supports discovering 15// entrypoints without a [shader] attribute. 16 17SLANG_UNIT_TEST (genericInterfaceConformance ) 18{ 19// Source for a module that contains an undecorated entrypoint. 20const char * userSourceBody = R"( 21public interface ITestInterface<Real : IFloat> { 22Real sample(); 23} 24 25struct TestInterfaceImpl<Real : IFloat> : ITestInterface<Real> { 26Real sample() { 27return x; 28} 29Real x; 30} 31 32//TEST_INPUT: set data = new StructuredBuffer<ITestInterface<float> >[new TestInterfaceImpl<float>{1.0}]; 33StructuredBuffer<ITestInterface<float>> data; 34 35//TEST_INPUT: set outputBuffer = out ubuffer(data=[0 0 0 0], stride=4); 36RWStructuredBuffer<int> outputBuffer; 37 38//TEST_INPUT: type_conformance TestInterfaceImpl<float>:ITestInterface<float> = 3 39 40[numthreads(1, 1, 1)] 41void computeMain() 42{ 43let obj = data[0]; 44// CHECK: 1 45outputBuffer[0] = int(obj.sample()); 46} 47)" ; 48 49ComPtr < slang::IGlobalSession > globalSession ; 50SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 51 slang::TargetDesc targetDesc = {}; 52targetDesc .format = SLANG_HLSL ; 53 54 slang::SessionDesc sessionDesc = {}; 55sessionDesc .targetCount = 1 ; 56sessionDesc .targets = & targetDesc ; 57sessionDesc .allowGLSLSyntax = true; 58 59ComPtr < slang::ISession > session ; 60SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 61 62ComPtr < slang::IBlob > diagnosticBlob ; 63auto module = session -> loadModuleFromSourceString ( 64"m" , 65"m.slang" , 66userSourceBody , 67diagnosticBlob .writeRef ()); 68SLANG_CHECK (module != nullptr ); 69 70ComPtr < slang::IEntryPoint > entryPoint ; 71module -> findAndCheckEntryPoint ( 72"computeMain" , 73SLANG_STAGE_COMPUTE , 74entryPoint .writeRef (), 75diagnosticBlob .writeRef ()); 76SLANG_CHECK (entryPoint != nullptr ); 77 78ComPtr < slang::IComponentType > compositeProgram ; 79 slang::IComponentType * components []= {module ,entryPoint .get ()}; 80session -> createCompositeComponentType ( 81components , 822 , 83compositeProgram .writeRef (), 84diagnosticBlob .writeRef ()); 85SLANG_CHECK (compositeProgram != nullptr ); 86 87ComPtr < slang::ITypeConformance > typeConformance ; 88auto result = session -> createTypeConformanceComponentType ( 89compositeProgram -> getLayout ()-> findTypeByName ("TestInterfaceImpl<float>" ), 90compositeProgram -> getLayout ()-> findTypeByName ("ITestInterface<float>" ), 91typeConformance .writeRef (), 923 , 93diagnosticBlob .writeRef ()); 94SLANG_CHECK (result == SLANG_OK ); 95SLANG_CHECK (typeConformance != nullptr ); 96 97ComPtr < slang::IComponentType > compositeProgram2 ; 98 slang::IComponentType * components2 []= {compositeProgram .get (),typeConformance .get ()}; 99session -> createCompositeComponentType ( 100components2 , 1012 , 102compositeProgram2 .writeRef (), 103diagnosticBlob .writeRef ()); 104 105ComPtr < slang::IComponentType > linkedProgram ; 106compositeProgram2 -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 107SLANG_CHECK (linkedProgram != nullptr ); 108 109ComPtr < slang::IBlob > code ; 110linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 111SLANG_CHECK (code != nullptr ); 112 113auto codeSrc = UnownedStringSlice ((const char * )code -> getBufferPointer ()); 114SLANG_CHECK (codeSrc .indexOf (toSlice ("computeMain" ))!= -1 ); 115}