|
| 1 | +// unit-test-module-ptr.cpp |
| 2 | + |
| 3 | +#include "core/slang-memory-file-system.h" |
| 4 | +#include "slang-com-ptr.h" |
| 5 | +#include "slang.h" |
| 6 | +#include "unit-test/slang-unit-test.h" |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +using namespace Slang; |
| 12 | + |
| 13 | +SLANG_UNIT_TEST(modulePtr) |
| 14 | +{ |
| 15 | + const char* testModuleSource = R"( |
| 16 | + module test_module; |
| 17 | +
|
| 18 | + public void atomicFunc(__ref Atomic<int> ptr) { |
| 19 | + ptr.add(1); |
| 20 | + } |
| 21 | + )"; |
| 22 | + |
| 23 | + const char* testSource = R"( |
| 24 | + import "test_module"; |
| 25 | +
|
| 26 | + RWStructuredBuffer<Atomic<int>> input0; |
| 27 | +
|
| 28 | + [shader("compute")] |
| 29 | + [numthreads(1,1,1)] |
| 30 | + void computeMain(uint3 workGroup : SV_GroupID) |
| 31 | + { |
| 32 | + atomicFunc(input0[0]); |
| 33 | + } |
| 34 | + )"; |
| 35 | + ComPtr<ISlangMutableFileSystem> memoryFileSystem = |
| 36 | + ComPtr<ISlangMutableFileSystem>(new Slang::MemoryFileSystem()); |
| 37 | + |
| 38 | + ComPtr<slang::IGlobalSession> globalSession; |
| 39 | + SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); |
| 40 | + slang::TargetDesc targetDesc = {}; |
| 41 | + targetDesc.format = SLANG_SPIRV; |
| 42 | + targetDesc.profile = globalSession->findProfile("spirv_1_5"); |
| 43 | + slang::SessionDesc sessionDesc = {}; |
| 44 | + sessionDesc.targetCount = 1; |
| 45 | + sessionDesc.targets = &targetDesc; |
| 46 | + sessionDesc.compilerOptionEntryCount = 0; |
| 47 | + sessionDesc.fileSystem = memoryFileSystem; |
| 48 | + |
| 49 | + // Precompile test_module to file. |
| 50 | + { |
| 51 | + ComPtr<slang::ISession> session; |
| 52 | + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); |
| 53 | + |
| 54 | + ComPtr<slang::IBlob> diagnosticBlob; |
| 55 | + auto module = session->loadModuleFromSourceString( |
| 56 | + "test_module", |
| 57 | + "test_module.slang", |
| 58 | + testModuleSource, |
| 59 | + diagnosticBlob.writeRef()); |
| 60 | + SLANG_CHECK(module != nullptr); |
| 61 | + |
| 62 | + ComPtr<slang::IBlob> moduleBlob; |
| 63 | + module->serialize(moduleBlob.writeRef()); |
| 64 | + memoryFileSystem->saveFile( |
| 65 | + "test_module.slang-module", |
| 66 | + moduleBlob->getBufferPointer(), |
| 67 | + moduleBlob->getBufferSize()); |
| 68 | + } |
| 69 | + |
| 70 | + // compile test. |
| 71 | + { |
| 72 | + ComPtr<slang::ISession> session; |
| 73 | + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); |
| 74 | + |
| 75 | + ComPtr<slang::IBlob> diagnosticBlob; |
| 76 | + auto module = session->loadModuleFromSourceString( |
| 77 | + "test", |
| 78 | + "test.slang", |
| 79 | + testSource, |
| 80 | + diagnosticBlob.writeRef()); |
| 81 | + SLANG_CHECK(module != nullptr); |
| 82 | + |
| 83 | + ComPtr<slang::IComponentType> linkedProgram; |
| 84 | + module->link(linkedProgram.writeRef()); |
| 85 | + |
| 86 | + ComPtr<slang::IBlob> code; |
| 87 | + |
| 88 | + linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef()); |
| 89 | + |
| 90 | + SLANG_CHECK(code->getBufferSize() > 0); |
| 91 | + } |
| 92 | +} |
0 commit comments