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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// unit-test-slang-vm.cpp
#include "core/slang-memory-file-system.h"
#include "slang-com-ptr.h"
#include "slang.h"
#include "unit-test/slang-unit-test.h"
#include <stdio.h>
#include <stdlib.h>
using namespace Slang;
SLANG_UNIT_TEST(slangVM)
{
const char* testSource = R"(
int one() { return 1; }
int sum(int x)
{
int result = 0;
for (int i = 0; i <= x; i++)
{
result += i;
}
return result + one();
}
[shader("dispatch")]
int dispatchMain(uniform int2 v, out int c)
{
int a = v.x;
int b = v.y;
int tmp = 0;
if (a > 0)
tmp = a + b;
else
tmp = b - a;
tmp += sum(b);
c = tmp;
return 100;
}
)";
// Create Slang session and compile code.
ComPtr<slang::IBlob> code;
String disasmText;
{
ComPtr<slang::IGlobalSession> globalSession;
SLANG_CHECK(
slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
slang::TargetDesc targetDesc = {};
targetDesc.format = SLANG_HOST_VM;
slang::SessionDesc sessionDesc = {};
sessionDesc.targetCount = 1;
sessionDesc.targets = &targetDesc;
sessionDesc.compilerOptionEntryCount = 0;
ComPtr<slang::ISession> session;
SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
ComPtr<slang::IBlob> diagnosticBlob;
auto module = session->loadModuleFromSourceString(
"test",
"test.slang",
testSource,
diagnosticBlob.writeRef());
SLANG_CHECK(module != nullptr);
ComPtr<slang::IComponentType> linkedProgram;
module->link(linkedProgram.writeRef());
linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK(code->getBufferSize() > 0);
ComPtr<slang::IBlob> disasmBlob;
SLANG_CHECK(slang_disassembleByteCode(code, disasmBlob.writeRef()) == SLANG_OK);
disasmText = (const char*)disasmBlob->getBufferPointer();
SLANG_CHECK(disasmText.indexOf("ret") != -1);
}
// Create a byte code runner and interpret the code.
ComPtr<slang::IByteCodeRunner> runner;
slang::ByteCodeRunnerDesc runnerDesc = {};
SLANG_CHECK(slang_createByteCodeRunner(&runnerDesc, runner.writeRef()) == SLANG_OK);
SLANG_CHECK(runner->loadModule(code) == SLANG_OK);
SLANG_CHECK(runner->selectFunctionByIndex(0) == SLANG_OK);
struct Params
{
int a;
int b;
int* result;
};
int result = 0;
Params params = {1, 2, &result};
SLANG_CHECK(runner->execute(¶ms, sizeof(params)) == SLANG_OK);
SLANG_CHECK(result == 7);
size_t returnValSize = 0;
int* returnVal = (int*)runner->getReturnValue(&returnValSize);
SLANG_CHECK(returnValSize == sizeof(int));
SLANG_CHECK(*returnVal == 100);
}
|