yum-mirror/slang

Making it easier to work with shaders

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

Ellie HermaszewskaCorrect include dir for libslang (#5539)7b570feed

master
2.9 KiB86 linesraw
1// unit-test-find-entrypoint-nested.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 works with modules that
15// defines two entrypoints, where one entrypoint calls the other.
16
17SLANG_UNIT_TEST(findEntryPointNested)
18{
19    // Source for a module that contains an undecorated entrypoint.
20    const char* userSourceBody = R"(
21        [shader("raygeneration")]
22        void inner()
23        {
24            AllMemoryBarrier();
25        }
26        [shader("raygeneration")]
27        void outer()
28        {
29            inner();
30        }
31        )";
32
33    auto moduleName = "moduleG" + String(Process::getId());
34    String userSource = "import " + moduleName + ";\n" + userSourceBody;
35    ComPtr<slang::IGlobalSession> globalSession;
36    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
37    slang::TargetDesc targetDesc = {};
38    targetDesc.format = SLANG_SPIRV;
39    targetDesc.profile = globalSession->findProfile("spirv_1_5");
40    slang::SessionDesc sessionDesc = {};
41    sessionDesc.targetCount = 1;
42    sessionDesc.targets = &targetDesc;
43    sessionDesc.compilerOptionEntryCount = 1;
44    slang::CompilerOptionEntry compilerOptionEntry = {};
45    compilerOptionEntry.name = slang::CompilerOptionName::EmitSpirvViaGLSL;
46    compilerOptionEntry.value.kind = slang::CompilerOptionValueKind::Int;
47    compilerOptionEntry.value.intValue0 = 1;
48    sessionDesc.compilerOptionEntries = &compilerOptionEntry;
49
50    ComPtr<slang::ISession> session;
51    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
52
53    ComPtr<slang::IBlob> diagnosticBlob;
54    auto module = session->loadModuleFromSourceString(
55        "m",
56        "m.slang",
57        userSourceBody,
58        diagnosticBlob.writeRef());
59    SLANG_CHECK(module != nullptr);
60
61    ComPtr<slang::IEntryPoint> entryPoint;
62    module->findAndCheckEntryPoint(
63        "outer",
64        SLANG_STAGE_RAY_GENERATION,
65        entryPoint.writeRef(),
66        diagnosticBlob.writeRef());
67    SLANG_CHECK(entryPoint != nullptr);
68
69    ComPtr<slang::IComponentType> compositeProgram;
70    slang::IComponentType* components[] = {module, entryPoint.get()};
71    session->createCompositeComponentType(
72        components,
73        2,
74        compositeProgram.writeRef(),
75        diagnosticBlob.writeRef());
76    SLANG_CHECK(compositeProgram != nullptr);
77
78    ComPtr<slang::IComponentType> linkedProgram;
79    compositeProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
80    SLANG_CHECK(linkedProgram != nullptr);
81
82    ComPtr<slang::IBlob> code;
83    linkedProgram->getEntryPointCode(0, 0, code.writeRef(), diagnosticBlob.writeRef());
84    SLANG_CHECK(code != nullptr);
85    SLANG_CHECK(code->getBufferSize() != 0);
86}