yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
49667272a
master
1// unit-test-glsl-compile.cpp 2 3#include "../../source/core/slang-io.h" 4#include "../../source/core/slang-process.h" 5#include "../../tools/platform/performance-counter.h" 6#include "slang-com-ptr.h" 7#include "slang.h" 8#include "unit-test/slang-unit-test.h" 9 10#include <stdio.h> 11#include <stdlib.h> 12 13using namespace Slang ; 14 15// Test the compilation API for cross-compiling glsl source to SPIRV. 16 17SLANG_UNIT_TEST (compileBenchmark ) 18{ 19const char * userSourceBody = R"( 20// shader.slang 21 22struct PushConstantCompute 23{ 24uint64_t bufferAddress; 25uint numVertices; 26}; 27 28struct Vertex 29{ 30float3 position; 31}; 32 33 34[[vk::push_constant]] 35ConstantBuffer<PushConstantCompute> pushConst; 36 37[shader("compute")] 38[numthreads(256, 1, 1)] 39void main(uint3 threadIdx : SV_DispatchThreadID) 40{ 41uint index = threadIdx.x; 42 43if(index >= pushConst.numVertices) 44return; 45 46Vertex* vertices = (Vertex*)pushConst.bufferAddress; 4748 float angle = (index + 1) * 2.3f; 49 50float3 vertex = vertices[index].position; 51 52float cosAngle = cos(angle); 53float sinAngle = sin(angle); 54float3x3 rotationMatrix = float3x3( 55cosAngle, -sinAngle, 0.0, 56sinAngle, cosAngle, 0.0, 570.0, 0.0, 1.0 58); 59 60float3 rotatedVertex = mul(rotationMatrix, vertex); 61 62vertices[index].position = rotatedVertex; 63} 64)" ; 65ComPtr < slang::IGlobalSession > globalSession ; 66SlangGlobalSessionDesc globalDesc = {}; 67globalDesc .enableGLSL = false; 68SLANG_CHECK (slang_createGlobalSession2 (& globalDesc ,globalSession .writeRef ())== SLANG_OK ); 69 slang::TargetDesc targetDesc = {}; 70targetDesc .format = SLANG_SPIRV ; 71targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 72 slang::SessionDesc sessionDesc = {}; 73sessionDesc .targetCount = 1 ; 74sessionDesc .targets = & targetDesc ; 75 76auto start = platform::PerformanceCounter ::now (); 77for (int pass = 0 ;pass < 100 ;pass ++ ) 78 { 79ComPtr < slang::ISession > session ; 80SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 81 82ComPtr < slang::IBlob > diagnosticBlob ; 83auto module = session -> loadModuleFromSourceString ( 84"m" , 85"m.slang" , 86userSourceBody , 87diagnosticBlob .writeRef ()); 88SLANG_CHECK (module != nullptr ); 89 90ComPtr < slang::IEntryPoint > entryPoint ; 91SlangResult res = module -> findAndCheckEntryPoint ( 92"main" , 93SLANG_STAGE_COMPUTE , 94entryPoint .writeRef (), 95diagnosticBlob .writeRef ()); 96SLANG_CHECK (res == SLANG_OK ); 97 98 slang::IComponentType * componentTypes [2 ]= {module ,entryPoint .get ()}; 99ComPtr < slang::IComponentType > composedProgram ; 100session -> createCompositeComponentType ( 101componentTypes , 1022 , 103composedProgram .writeRef (), 104diagnosticBlob .writeRef ()); 105 106ComPtr < slang::IComponentType > linkedProgram ; 107composedProgram -> link (linkedProgram .writeRef (),diagnosticBlob .writeRef ()); 108 109ComPtr < slang::IBlob > code ; 110linkedProgram -> getEntryPointCode (0 ,0 ,code .writeRef (),diagnosticBlob .writeRef ()); 111 } 112auto time = platform::PerformanceCounter ::getElapsedTimeInSeconds (start ); 113getTestReporter ()-> addExecutionTime (time ); 114}