yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
8000e0ede
master
1// unit-test-glsl-compile.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 cross-compiling glsl source to SPIRV. 15 16SLANG_UNIT_TEST (glslCompile ) 17{ 18const char * userSourceBody = R"( 19#version 450 core 20layout(location = 0) in vec2 aPosition; 21layout(location = 1) in vec4 aColor; 22layout(location = 0) out vec4 vColor; 23void main() { 24vColor = aColor; 25gl_Position = vec4(aPosition, 0, 1); 26} 27)" ; 28ComPtr < slang::IGlobalSession > globalSession ; 29SlangGlobalSessionDesc globalDesc = {}; 30globalDesc .enableGLSL = true; 31SLANG_CHECK (slang_createGlobalSession2 (& globalDesc ,globalSession .writeRef ())== SLANG_OK ); 32 slang::TargetDesc targetDesc = {}; 33targetDesc .format = SLANG_SPIRV ; 34targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 35 slang::SessionDesc sessionDesc = {}; 36sessionDesc .targetCount = 1 ; 37sessionDesc .targets = & targetDesc ; 38ComPtr < slang::ISession > session ; 39SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 40 41ComPtr < slang::IBlob > diagnosticBlob ; 42auto module = session -> loadModuleFromSourceString ( 43"m" , 44"m.slang" , 45userSourceBody , 46diagnosticBlob .writeRef ()); 47SLANG_CHECK (module != nullptr ); 48 49ComPtr < slang::IEntryPoint > entryPoint ; 50module -> findAndCheckEntryPoint ( 51"main" , 52SLANG_STAGE_VERTEX , 53entryPoint .writeRef (), 54diagnosticBlob .writeRef ()); 55 56 slang::IComponentType * componentTypes [2 ]= {module ,entryPoint .get ()}; 57ComPtr < slang::IComponentType > composedProgram ; 58session -> createCompositeComponentType ( 59componentTypes , 602 , 61composedProgram .writeRef (), 62diagnosticBlob .writeRef ()); 63 64ComPtr < slang::IComponentType > linkedProgram ; 65composedProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 66 67ComPtr < slang::IBlob > code ; 68linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 69 70SLANG_CHECK (code != nullptr ); 71}