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.6 KiB78 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 API supports discovering previously checked translation unit in the same
15// FrontEndCompileRequest.
16SLANG_UNIT_TEST(translationUnitImport)
17{
18    // Source for the first translation unit.
19    const char* generatedSource = "public int f() {"
20                                  "   return 5;"
21                                  "};";
22
23    // Source for the a file that imports the first translation unit.
24    // The import should succeed and `f` should be visible to this module.
25    const char* fileSource =
26        R"(
27        import generatedUnit;
28
29        public int g(){ return f(); }
30        )";
31
32    // Source for a module that transitively uses the generated source via a file.
33    const char* userSourceBody = R"(
34        [shader("compute")]
35        [numthreads(4,1,1)]
36        void computeMain(
37            uint3 sv_dispatchThreadID : SV_DispatchThreadID,
38            uniform RWStructuredBuffer<int> buffer)
39        {
40            buffer[sv_dispatchThreadID.x] = g();
41        })";
42
43    auto moduleName = "moduleG" + String(Process::getId());
44    String userSource = "import " + moduleName + ";\n" + userSourceBody;
45    auto session = spCreateSession();
46    auto request = spCreateCompileRequest(session);
47
48    File::writeAllText(moduleName + ".slang", fileSource);
49
50    spAddCodeGenTarget(request, SLANG_HLSL);
51    int generatedTranslationUnitIndex =
52        spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "generatedUnit");
53    spAddTranslationUnitSourceString(
54        request,
55        generatedTranslationUnitIndex,
56        "generatedFile",
57        generatedSource);
58
59    int entryPointTranslationUnitIndex =
60        spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "userUnit");
61    spAddTranslationUnitSourceString(
62        request,
63        entryPointTranslationUnitIndex,
64        "userFile",
65        userSource.getUnownedSlice().begin());
66    spAddEntryPoint(request, entryPointTranslationUnitIndex, "computeMain", SLANG_STAGE_COMPUTE);
67
68    auto compileResult = spCompile(request);
69    SLANG_CHECK(compileResult == SLANG_OK);
70
71    Slang::ComPtr<ISlangBlob> outBlob;
72    spGetEntryPointCodeBlob(request, 0, 0, outBlob.writeRef());
73    SLANG_CHECK(outBlob && outBlob->getBufferSize() != 0);
74
75    spDestroyCompileRequest(request);
76    spDestroySession(session);
77    File::remove(moduleName + ".slang");
78}