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.3 KiB71 linesraw
1// unit-test-translation-unit-import.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 IComponentType::getTargetCode API supports
15// compiling a program with multiple entrypoints and retrieving a single
16// compiled module that contains all the entrypoints.
17//
18SLANG_UNIT_TEST(getTargetCode)
19{
20    // Source for a module that contains an undecorated entrypoint.
21    const char* userSourceBody = R"(
22        [shader("fragment")]
23        float4 fragMain(float4 pos:SV_Position) : SV_Target
24        {
25            return pos;
26        }
27        [shader("vertex")]
28        float4 vertMain(float4 pos) : SV_Position
29        {
30            return pos;
31        }
32        )";
33
34    String userSource = userSourceBody;
35    ComPtr<slang::IGlobalSession> globalSession;
36    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
37    slang::TargetDesc targetDesc = {};
38    // Request SPIR-V disassembly so we can check the content.
39    targetDesc.format = SLANG_SPIRV_ASM;
40    targetDesc.profile = globalSession->findProfile("sm_5_0");
41    slang::SessionDesc sessionDesc = {};
42    sessionDesc.targetCount = 1;
43    sessionDesc.targets = &targetDesc;
44
45    ComPtr<slang::ISession> session;
46    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
47
48    ComPtr<slang::IBlob> diagnosticBlob;
49    auto module = session->loadModuleFromSourceString(
50        "m",
51        "m.slang",
52        userSourceBody,
53        diagnosticBlob.writeRef());
54    SLANG_CHECK(module != nullptr);
55
56    ComPtr<slang::IComponentType> linkedProgram;
57    module->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
58    SLANG_CHECK(linkedProgram != nullptr);
59
60    ComPtr<slang::IBlob> code;
61    linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef());
62    SLANG_CHECK(code != nullptr);
63
64    SLANG_CHECK(code->getBufferSize() != 0);
65
66    UnownedStringSlice resultStr = UnownedStringSlice((char*)code->getBufferPointer());
67
68    // Make sure the spirv disassembly contains both entrypoint names.
69    SLANG_CHECK(resultStr.indexOf(toSlice("fragMain")) != -1);
70    SLANG_CHECK(resultStr.indexOf(toSlice("vertMain")) != -1);
71}