yum-mirror/slang

Making it easier to work with shaders

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

Yong HeCache and reuse glsl module. (#6152)8000e0ede

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