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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
// unit-test-com-host-callable.cpp
#include "../../source/core/slang-byte-encode-util.h"
#include "../../source/core/slang-list.h"
#include "slang-com-helper.h"
#include "slang-com-ptr.h"
#include "slang.h"
#include "unit-test/slang-unit-test.h"
#include <stdio.h>
#include <stdlib.h>
namespace
{ // anonymous
// Slang namespace is used for elements support code (like core) which we use here
// for ComPtr<> and TestToolUtil
using namespace Slang;
// For the moment we have to explicitly write the Slang COM interface in C++ code. It *MUST* match
// the interface in the slang source
// As it stands all interfaces need to derive from ISlangUnknown (or IUnknown).
class IDoThings : public ISlangUnknown
{
public:
virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) = 0;
virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) = 0;
};
class ICountGood : public ISlangUnknown
{
public:
virtual SLANG_NO_THROW int SLANG_MCALL nextCount() = 0;
};
static int _calcHash(const char* in)
{
int hash = 0;
for (; *in; ++in)
{
// A very poor hash function
hash = hash * 13 + *in;
}
return hash;
}
class DoThings : public IDoThings
{
public:
// We don't need queryInterface for this impl, or ref counting
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE
{
return SLANG_E_NOT_IMPLEMENTED;
}
virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
// IDoThings
virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE
{
return a + b + 1;
}
virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE
{
return (int)_calcHash(in);
}
};
class CountGood : public ICountGood
{
public:
// We don't need queryInterface for this impl, or ref counting
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE
{
return SLANG_E_NOT_IMPLEMENTED;
}
virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
// ICountGood
virtual SLANG_NO_THROW int SLANG_MCALL nextCount() SLANG_OVERRIDE { return m_count++; }
int m_count = 0;
};
struct ComTestContext
{
ComTestContext(UnitTestContext* context)
: m_unitTestContext(context)
{
slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession;
m_defaultCppCompiler =
slangSession->getDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP);
m_hostHostCallableCompiler = slangSession->getDownstreamCompilerForTransition(
SLANG_CPP_SOURCE,
SLANG_HOST_HOST_CALLABLE);
m_shaderHostCallableCompiler = slangSession->getDownstreamCompilerForTransition(
SLANG_CPP_SOURCE,
SLANG_SHADER_HOST_CALLABLE);
}
SlangResult runTests()
{
slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession;
// TODO(JS):
// Care is needed around this in normal testing. `slang-llvm` is whatever was asked for for
// when premake was built when the target is specified. Otherwise it is the `default` which
// is typically 64 bit during development.
//
// On CI we should be okay, because it should download the correct `slang-llvm` for the
// build (as it packages up with it). But for normal development, that can easily not be the
// case (for example changing to 32 bit build in VS is a problem).
//
// Make sure to run
//
// ```
// premake --arch=x86 --deps=true
// ```
//
// for the actual target/arch(!)
const bool hasLlvm =
SLANG_SUCCEEDED(slangSession->checkPassThroughSupport(SLANG_PASS_THROUGH_LLVM));
SlangPassThrough cppCompiler = SLANG_PASS_THROUGH_NONE;
{
const SlangPassThrough cppCompilers[] = {
SLANG_PASS_THROUGH_VISUAL_STUDIO,
SLANG_PASS_THROUGH_GCC,
SLANG_PASS_THROUGH_CLANG,
};
// Do we have a C++ compiler
for (const auto compiler : cppCompilers)
{
if (SLANG_SUCCEEDED(slangSession->checkPassThroughSupport(compiler)))
{
cppCompiler = compiler;
break;
}
}
}
// If we have an *actual* C++ compile rtest on that first
if (cppCompiler != SLANG_PASS_THROUGH_NONE)
{
slangSession->setDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP, cppCompiler);
slangSession->setDownstreamCompilerForTransition(
SLANG_CPP_SOURCE,
SLANG_SHADER_HOST_CALLABLE,
cppCompiler);
slangSession->setDownstreamCompilerForTransition(
SLANG_CPP_SOURCE,
SLANG_HOST_HOST_CALLABLE,
cppCompiler);
SLANG_RETURN_ON_FAIL(_runTest());
}
// Reset the compiler that's used for host-callable
_reset();
// If we have Llvm it is the default host callable compiler
if (hasLlvm)
{
// Should run via slang-llvm
SLANG_RETURN_ON_FAIL(_runTest());
}
return SLANG_OK;
}
void _reset()
{
slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession;
slangSession->setDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP, m_defaultCppCompiler);
slangSession->setDownstreamCompilerForTransition(
SLANG_CPP_SOURCE,
SLANG_SHADER_HOST_CALLABLE,
m_shaderHostCallableCompiler);
slangSession->setDownstreamCompilerForTransition(
SLANG_CPP_SOURCE,
SLANG_HOST_HOST_CALLABLE,
m_hostHostCallableCompiler);
}
~ComTestContext() { _reset(); }
SlangResult _runTest();
UnitTestContext* m_unitTestContext;
SlangPassThrough m_defaultCppCompiler;
SlangPassThrough m_hostHostCallableCompiler;
SlangPassThrough m_shaderHostCallableCompiler;
};
SlangResult ComTestContext::_runTest()
{
slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession;
// Create a compile request
Slang::ComPtr<slang::ICompileRequest> request;
SLANG_ALLOW_DEPRECATED_BEGIN
SLANG_RETURN_ON_FAIL(slangSession->createCompileRequest(request.writeRef()));
SLANG_ALLOW_DEPRECATED_END
// We want to compile to 'HOST_CALLABLE' here such that we can execute the Slang code.
//
// Note that it is possible to use HOST_HOST_CALLABLE, but this currently only works with
// 'regular' C++ compilers not with `slang-llvm`.
const int targetIndex = request->addCodeGenTarget(SLANG_SHADER_HOST_CALLABLE);
// Set the target flag to indicate that we want to compile all into a library.
request->setTargetFlags(targetIndex, SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM);
request->setOptimizationLevel(SLANG_OPTIMIZATION_LEVEL_NONE);
request->setDebugInfoLevel(SLANG_DEBUG_INFO_LEVEL_STANDARD);
// Add the translation unit
const int translationUnitIndex =
request->addTranslationUnit(SLANG_SOURCE_LANGUAGE_SLANG, nullptr);
// Set the source file for the translation unit
request->addTranslationUnitSourceFile(
translationUnitIndex,
"tools/slang-unit-test/unit-test-com-host-callable.slang");
const SlangResult compileRes = request->compile();
// Even if there were no errors that forced compilation to fail, the
// compiler may have produced "diagnostic" output such as warnings.
// We will go ahead and print that output here.
//
if (auto diagnostics = request->getDiagnosticOutput())
{
printf("%s", diagnostics);
}
// Get the 'shared library' (note that this doesn't necessarily have to be implemented as a
// shared library it's just an interface to executable code).
ComPtr<ISlangSharedLibrary> sharedLibrary;
SLANG_RETURN_ON_FAIL(request->getTargetHostCallable(0, sharedLibrary.writeRef()));
{
typedef const char* (*Func)(const char*);
Func func = (Func)sharedLibrary->findFuncByName("getString");
if (!func)
{
return SLANG_FAIL;
}
String text = "Hello World!";
String returnedText = func(text.getBuffer());
SLANG_CHECK(text == returnedText);
}
{
typedef int (*Func)(const char* text, IDoThings* doThings);
Func func = (Func)sharedLibrary->findFuncByName("calcHash");
if (!func)
{
return SLANG_FAIL;
}
DoThings doThings;
String text("Hello");
const int hash = func(text.getBuffer(), &doThings);
SLANG_CHECK(hash == _calcHash(text.getBuffer()));
}
// Check accessing a global
{
typedef void (*SetFunc)(int v);
typedef int (*GetFunc)();
const auto setGlobal = (SetFunc)sharedLibrary->findFuncByName("setGlobal");
const auto getGlobal = (GetFunc)sharedLibrary->findFuncByName("getGlobal");
if (setGlobal == nullptr || getGlobal == nullptr)
{
return SLANG_FAIL;
}
// In the slang source it is set a default value
SLANG_CHECK(getGlobal() == 10);
for (Index i = 0; i < 10; ++i)
{
setGlobal(int(i));
SLANG_CHECK(getGlobal() == i);
}
}
// Check using a global interface
{
typedef void (*SetCounterFunc)(ICountGood* counter);
typedef int (*NextCountFunc)();
const auto setCounter = (SetCounterFunc)sharedLibrary->findFuncByName("setCounter");
const auto nextCount = (NextCountFunc)sharedLibrary->findFuncByName("nextCount");
if (setCounter == nullptr || nextCount == nullptr)
{
return SLANG_FAIL;
}
CountGood counter;
ICountGood* counterIntf = &counter;
setCounter(counterIntf);
auto counterPtr = (ICountGood**)sharedLibrary->findSymbolAddressByName("globalCounter");
SLANG_CHECK(counterPtr);
if (!counterPtr)
{
return SLANG_FAIL;
}
for (Index i = 0; i < 10; ++i)
{
SLANG_CHECK(*counterPtr == &counter);
const auto v = nextCount();
SLANG_CHECK(v == i);
}
}
return SLANG_OK;
}
} // namespace
SLANG_UNIT_TEST(comHostCallable)
{
#if SLANG_PTR_IS_32 && !SLANG_MICROSOFT_FAMILY
// TODO(JS):
// We can't currently run this test reliably on targets other than windows
// Visual Studio DownstreamCompiler has support for 32 bit builds
// Other targets generally build for the native environment which is almost always 64 bit,
// and it requires other features to build/test 32 bit binaries on such systems.
//
// So we disable for any 32 bit non MS target for now
return;
#endif
ComTestContext context(unitTestContext);
const auto result = context.runTests();
SLANG_CHECK(SLANG_SUCCEEDED(result));
}
|