yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Gangzheng Tongfix the break to make sure only valid data will be accessed (#7148)49667272a

master
3.2 KiB114 linesraw
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{
19    const char* userSourceBody = R"(
20// shader.slang
21
22struct PushConstantCompute
23{
24  uint64_t bufferAddress;
25  uint     numVertices;
26};
27
28struct Vertex
29{
30  float3 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{
41  uint index = threadIdx.x;
42
43  if(index >= pushConst.numVertices)
44   return;
45
46  Vertex* vertices = (Vertex*)pushConst.bufferAddress;
47 
48  float angle = (index + 1) * 2.3f;
49
50  float3 vertex = vertices[index].position;
51
52  float cosAngle = cos(angle);
53  float sinAngle = sin(angle);
54  float3x3 rotationMatrix = float3x3(
55    cosAngle, -sinAngle, 0.0,
56    sinAngle,  cosAngle, 0.0,
57         0.0,       0.0, 1.0
58  );
59
60  float3 rotatedVertex = mul(rotationMatrix, vertex);
61
62  vertices[index].position = rotatedVertex;
63}
64        )";
65    ComPtr<slang::IGlobalSession> globalSession;
66    SlangGlobalSessionDesc globalDesc = {};
67    globalDesc.enableGLSL = false;
68    SLANG_CHECK(slang_createGlobalSession2(&globalDesc, globalSession.writeRef()) == SLANG_OK);
69    slang::TargetDesc targetDesc = {};
70    targetDesc.format = SLANG_SPIRV;
71    targetDesc.profile = globalSession->findProfile("spirv_1_5");
72    slang::SessionDesc sessionDesc = {};
73    sessionDesc.targetCount = 1;
74    sessionDesc.targets = &targetDesc;
75
76    auto start = platform::PerformanceCounter::now();
77    for (int pass = 0; pass < 100; pass++)
78    {
79        ComPtr<slang::ISession> session;
80        SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
81
82        ComPtr<slang::IBlob> diagnosticBlob;
83        auto module = session->loadModuleFromSourceString(
84            "m",
85            "m.slang",
86            userSourceBody,
87            diagnosticBlob.writeRef());
88        SLANG_CHECK(module != nullptr);
89
90        ComPtr<slang::IEntryPoint> entryPoint;
91        SlangResult res = module->findAndCheckEntryPoint(
92            "main",
93            SLANG_STAGE_COMPUTE,
94            entryPoint.writeRef(),
95            diagnosticBlob.writeRef());
96        SLANG_CHECK(res == SLANG_OK);
97
98        slang::IComponentType* componentTypes[2] = {module, entryPoint.get()};
99        ComPtr<slang::IComponentType> composedProgram;
100        session->createCompositeComponentType(
101            componentTypes,
102            2,
103            composedProgram.writeRef(),
104            diagnosticBlob.writeRef());
105
106        ComPtr<slang::IComponentType> linkedProgram;
107        composedProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
108
109        ComPtr<slang::IBlob> code;
110        linkedProgram->getEntryPointCode(0, 0, code.writeRef(), diagnosticBlob.writeRef());
111    }
112    auto time = platform::PerformanceCounter::getElapsedTimeInSeconds(start);
113    getTestReporter()->addExecutionTime(time);
114}