yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f4449d937
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 (findAndCheckEntryPoint ) 18{ 19// Source for a module that contains an undecorated entrypoint. 20const char * userSourceBody = R"( 21float4 fragMain(float4 pos:SV_Position) : SV_Target 22{ 23return pos; 24} 25)" ; 26 27auto moduleName = "moduleG" + String (Process ::getId ()); 28String userSource = "import " + moduleName + ";\n" + userSourceBody ; 29ComPtr < slang::IGlobalSession > globalSession ; 30SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 31 slang::TargetDesc targetDesc = {}; 32targetDesc .format = SLANG_SPIRV ; 33targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 34 slang::SessionDesc sessionDesc = {}; 35sessionDesc .targetCount = 1 ; 36sessionDesc .targets = & targetDesc ; 37ComPtr < slang::ISession > session ; 38SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 39 40ComPtr < slang::IBlob > diagnosticBlob ; 41auto module = session -> loadModuleFromSourceString ( 42"m" , 43"m.slang" , 44userSourceBody , 45diagnosticBlob .writeRef ()); 46SLANG_CHECK (module != nullptr ); 47 48ComPtr < slang::IEntryPoint > entryPoint ; 49module -> findAndCheckEntryPoint ( 50"fragMain" , 51SLANG_STAGE_FRAGMENT , 52entryPoint .writeRef (), 53diagnosticBlob .writeRef ()); 54SLANG_CHECK (entryPoint != nullptr ); 55 56ComPtr < slang::IComponentType > compositeProgram ; 57 slang::IComponentType * components []= {module ,entryPoint .get ()}; 58session -> createCompositeComponentType ( 59components , 602 , 61compositeProgram .writeRef (), 62diagnosticBlob .writeRef ()); 63SLANG_CHECK (compositeProgram != nullptr ); 64 65ComPtr < slang::IComponentType > linkedProgram ; 66compositeProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 67SLANG_CHECK (linkedProgram != nullptr ); 68 69ComPtr < slang::IBlob > code ; 70linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 71SLANG_CHECK (code != nullptr ); 72SLANG_CHECK (code -> getBufferSize ()!= 0 ); 73} 74 75// This test reproduces issue #6507, where it was noticed that compilation of 76// tests/compute/simple.slang for PTX target generates invalid code. 77// TODO: Remove this when issue #4760 is resolved, because at that point 78// tests/compute/simple.slang should cover the same issue. 79SLANG_UNIT_TEST (cudaCodeGenBug ) 80{ 81// We need the CUDA backend for this test 82if (!SLANG_SUCCEEDED ( 83unitTestContext -> slangGlobalSession -> checkPassThroughSupport (SLANG_PASS_THROUGH_NVRTC ))) 84 { 85SLANG_IGNORE_TEST ; 86 } 87 88// Source for a module that contains an undecorated entrypoint. 89const char * userSourceBody = R"( 90RWStructuredBuffer<float> outputBuffer; 91 92[numthreads(4, 1, 1)] 93void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 94{ 95outputBuffer[dispatchThreadID.x] = float(dispatchThreadID.x); 96} 97)" ; 98 99auto moduleName = "moduleG" + String (Process ::getId ()); 100String userSource = "import " + moduleName + ";\n" + userSourceBody ; 101ComPtr < slang::IGlobalSession > globalSession ; 102SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 103 slang::TargetDesc targetDesc = {}; 104targetDesc .format = SLANG_PTX ; 105 slang::SessionDesc sessionDesc = {}; 106sessionDesc .targetCount = 1 ; 107sessionDesc .targets = & targetDesc ; 108ComPtr < slang::ISession > session ; 109SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 110 111ComPtr < slang::IBlob > diagnosticBlob ; 112auto module = session -> loadModuleFromSourceString ( 113"m" , 114"m.slang" , 115userSourceBody , 116diagnosticBlob .writeRef ()); 117SLANG_CHECK (module != nullptr ); 118 119ComPtr < slang::IEntryPoint > entryPoint ; 120module -> findAndCheckEntryPoint ( 121"computeMain" , 122SLANG_STAGE_COMPUTE , 123entryPoint .writeRef (), 124diagnosticBlob .writeRef ()); 125SLANG_CHECK (entryPoint != nullptr ); 126 127ComPtr < slang::IComponentType > compositeProgram ; 128 slang::IComponentType * components []= {module ,entryPoint .get ()}; 129session -> createCompositeComponentType ( 130components , 1312 , 132compositeProgram .writeRef (), 133diagnosticBlob .writeRef ()); 134SLANG_CHECK (compositeProgram != nullptr ); 135 136ComPtr < slang::IComponentType > linkedProgram ; 137compositeProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 138SLANG_CHECK (linkedProgram != nullptr ); 139 140ComPtr < slang::IBlob > code ; 141auto res = linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 142SLANG_CHECK (res == SLANG_OK ); 143SLANG_CHECK (code != nullptr && code -> getBufferSize ()!= 0 ); 144}