summaryrefslogtreecommitdiff
path: root/tools/slang-unit-test/unit-test-translation-unit-import.cpp
blob: 3b7f90c00ca6de11e6059d08abf0b698bf841dbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// unit-test-translation-unit-import.cpp

#include "slang.h"

#include <stdio.h>
#include <stdlib.h>

#include "tools/unit-test/slang-unit-test.h"
#include "slang-com-ptr.h"
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-process.h"

using namespace Slang;

// Test that the API supports discovering previously checked translation unit in the same
// FrontEndCompileRequest.
SLANG_UNIT_TEST(translationUnitImport)
{
    // Source for the first translation unit.
    const char* generatedSource =
        "public int f() {"
        "   return 5;"
        "};";

    // Source for the a file that imports the first translation unit.
    // The import should succeed and `f` should be visible to this module.
    const char* fileSource =
        R"(
        import generatedUnit;

        public int g(){ return f(); }
        )";

    // Source for a module that transitively uses the generated source via a file.
    const char* userSourceBody = R"(
        [shader("compute")]
        [numthreads(4,1,1)]
        void computeMain(
            uint3 sv_dispatchThreadID : SV_DispatchThreadID,
            uniform RWStructuredBuffer<int> buffer)
        {
            buffer[sv_dispatchThreadID.x] = g();
        })";

    auto moduleName = "moduleG" + String(Process::getId());
    String userSource = "import " + moduleName + ";\n" + userSourceBody;
    auto session = spCreateSession();
    auto request = spCreateCompileRequest(session);

    File::writeAllText(moduleName + ".slang", fileSource);

    spAddCodeGenTarget(request, SLANG_HLSL);
    int generatedTranslationUnitIndex = spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "generatedUnit");
    spAddTranslationUnitSourceString(
        request, generatedTranslationUnitIndex, "generatedFile", generatedSource);

    int entryPointTranslationUnitIndex = spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "userUnit");
    spAddTranslationUnitSourceString(
        request, entryPointTranslationUnitIndex, "userFile", userSource.getUnownedSlice().begin());
    spAddEntryPoint(request, entryPointTranslationUnitIndex, "computeMain", SLANG_STAGE_COMPUTE);

    auto compileResult = spCompile(request);
    SLANG_CHECK(compileResult == SLANG_OK);

    Slang::ComPtr<ISlangBlob> outBlob;
    spGetEntryPointCodeBlob(request, 0, 0, outBlob.writeRef());
    SLANG_CHECK(outBlob && outBlob->getBufferSize() != 0);
    
    spDestroyCompileRequest(request);
    spDestroySession(session);
    File::remove(moduleName + ".slang");
}