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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
// unit-test-record-replay.cpp
#include "../../source/core/slang-http.h"
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-process-util.h"
#include "../../source/core/slang-random-generator.h"
#include "../../source/core/slang-string-util.h"
#include "unit-test/slang-unit-test.h"
#include <chrono>
#include <thread>
using namespace Slang;
static SlangResult createProcess(
UnitTestContext* context,
const char* processName,
const List<String>* optArgs,
RefPtr<Process>& outProcess)
{
CommandLine cmdLine;
cmdLine.setExecutableLocation(ExecutableLocation(context->executableDirectory, processName));
if (optArgs)
{
cmdLine.m_args.addRange(optArgs->getBuffer(), optArgs->getCount());
}
SLANG_RETURN_ON_FAIL(Process::create(cmdLine, Process::Flag::AttachDebugger, outProcess));
return SLANG_OK;
}
struct entryHashInfo
{
int64_t callIdx = -1;
int64_t targetIndex = -1;
int64_t entryPointIndex = -1;
String hash;
};
static SlangResult parseHashes(List<String> const& lines, List<entryHashInfo>& outHashes)
{
SlangResult res = SLANG_OK;
for (const auto& line : lines)
{
List<UnownedStringSlice> tokens;
Index skipCharacters = line.indexOf(UnownedStringSlice("[slang-record-replay]:"));
if (skipCharacters == -1)
{
skipCharacters = 0;
}
else
{
skipCharacters += strlen("[slang-record-replay]:");
}
StringUtil::split(UnownedStringSlice(line.getBuffer() + skipCharacters), ',', tokens);
if (tokens.getCount() != 4)
{
return SLANG_FAIL;
}
entryHashInfo hashInfo;
auto extractToken = [](const UnownedStringSlice& token,
const char splitChar,
UnownedStringSlice& outToken) -> SlangResult
{
List<UnownedStringSlice> subTokens;
StringUtil::split(token, splitChar, subTokens);
if (subTokens.getCount() != 2)
{
return SLANG_FAIL;
}
outToken = subTokens[1];
return SLANG_OK;
};
{
UnownedStringSlice subToken;
SLANG_RETURN_ON_FAIL(extractToken(tokens[0], ':', subToken));
int64_t outNumer = 0;
StringUtil::parseInt64(subToken, outNumer);
hashInfo.callIdx = outNumer;
}
{
UnownedStringSlice subToken;
SLANG_RETURN_ON_FAIL(extractToken(tokens[1], ':', subToken));
int64_t outNumer = 0;
StringUtil::parseInt64(subToken, outNumer);
hashInfo.entryPointIndex = outNumer;
}
{
UnownedStringSlice subToken;
SLANG_RETURN_ON_FAIL(extractToken(tokens[2], ':', subToken));
int64_t outNumer = 0;
StringUtil::parseInt64(subToken, outNumer);
hashInfo.targetIndex = outNumer;
}
{
UnownedStringSlice subToken;
SLANG_RETURN_ON_FAIL(extractToken(tokens[3], ':', subToken));
// remove the white space after ":"
hashInfo.hash = subToken.begin() + 1;
}
outHashes.add(hashInfo);
}
return res;
}
static int writeEnvironmentVariable(const char* key, const char* val)
{
#ifdef _WIN32
String var = String(key) + "=" + val;
return _putenv(var.getBuffer());
#else
return setenv(key, val, 1);
#endif
}
static bool enableRecordLayer()
{
int retCode = writeEnvironmentVariable("SLANG_RECORD_LAYER", "1");
return retCode == 0;
}
static bool disableRecordLayer()
{
int retCode = writeEnvironmentVariable("SLANG_RECORD_LAYER", "0");
return retCode == 0;
}
static bool enableLogInReplayer()
{
int retCode = writeEnvironmentVariable("SLANG_RECORD_LOG_LEVEL", "3");
return retCode == 0;
}
static bool disableLogInReplayer()
{
int retCode = writeEnvironmentVariable("SLANG_RECORD_LOG_LEVEL", "0");
return retCode == 0;
}
static void findRecordFileName(List<String>* fileNames, const String& recordDir)
{
struct Visitor : Path::Visitor
{
void accept(Path::Type type, const UnownedStringSlice& filename) SLANG_OVERRIDE
{
if (type == Path::Type::File)
{
m_fileNames->add(filename);
}
}
Visitor(List<String>* fileNames)
: m_fileNames(fileNames)
{
}
List<String>* m_fileNames;
};
Visitor visitor(fileNames);
Path::find(recordDir.getBuffer(), "*.cap", &visitor);
}
static SlangResult launchProcessAndReadStdout(
UnitTestContext* context,
const List<String>& optArgs,
const char* exampleName,
RefPtr<Process>& process,
ExecuteResult& exeRes)
{
StringBuilder msgBuilder;
SlangResult res = createProcess(context, exampleName, &optArgs, process);
if (SLANG_FAILED(res))
{
msgBuilder << "Failed to launch process of '" << exampleName << "'\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return res;
}
res = ProcessUtil::readUntilTermination(process, exeRes);
if (SLANG_FAILED(res))
{
msgBuilder << "Failed to read stdout from '" << exampleName << "'\n";
msgBuilder << "process ret code: " << exeRes.resultCode;
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return res;
}
if (exeRes.resultCode != 0)
{
msgBuilder << "'" << exampleName << "' exits with failure\n";
msgBuilder << "Process ret code: " << exeRes.resultCode << "\n";
msgBuilder << "Standard output:\n" << exeRes.standardOutput;
msgBuilder << "Standard error:\n" << exeRes.standardError;
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
if (exeRes.standardOutput.getLength() == 0)
{
msgBuilder << "No stdout found in '" << exampleName << "'\n";
msgBuilder << "Standard error: " << exeRes.standardError;
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
return SLANG_OK;
}
static SlangResult runExample(
UnitTestContext* context,
const char* exampleName,
const String& recordDir,
List<entryHashInfo>& outHashes)
{
SlangResult finalRes = SLANG_OK;
RefPtr<Process> process;
ExecuteResult exeRes;
List<String> optArgs;
optArgs.add("--test-mode");
StringBuilder msgBuilder;
SlangResult res = SLANG_OK;
// Set unique record directory for this test
writeEnvironmentVariable("SLANG_RECORD_DIRECTORY", recordDir.getBuffer());
enableRecordLayer();
res = launchProcessAndReadStdout(context, optArgs, exampleName, process, exeRes);
disableRecordLayer();
if (SLANG_FAILED(res))
{
return res;
}
List<String> hashLines;
for (auto line : LineParser(exeRes.standardOutput.getUnownedSlice()))
{
if (line.getLength() == 0)
{
continue;
}
if (line.indexOf(UnownedStringSlice("hash:")) == -1)
{
continue;
}
hashLines.add(line);
}
if (hashLines.getCount() == 0)
{
msgBuilder << "Hash value is not found for '" << exampleName << "'\n";
msgBuilder << "Process ret code: " << exeRes.resultCode << "\n";
msgBuilder << "Standard output:\n" << exeRes.standardOutput << "\n";
msgBuilder << "Standard error:\n" << exeRes.standardError << "\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
res = parseHashes(hashLines, outHashes);
if (SLANG_FAILED(res))
{
msgBuilder << "Failed to parse hash from stdout of '" << exampleName << "'\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return res;
}
return SLANG_OK;
}
static SlangResult replayExample(
UnitTestContext* context,
const String& recordDir,
List<entryHashInfo>& outHashes)
{
List<String> fileNames;
findRecordFileName(&fileNames, recordDir);
if (fileNames.getCount() == 0)
{
getTestReporter()->message(TestMessageType::TestFailure, "No record files found\n");
return SLANG_FAIL;
}
List<String> optArgs;
String recordFileName = Path::combine(recordDir, fileNames[0]);
optArgs.add(recordFileName.getBuffer());
RefPtr<Process> process;
ExecuteResult exeRes;
StringBuilder msgBuilder;
msgBuilder << "replay the test\n";
enableLogInReplayer();
SlangResult res = launchProcessAndReadStdout(context, optArgs, "slang-replay", process, exeRes);
disableLogInReplayer();
if (SLANG_FAILED(res))
{
return res;
}
List<String> hashLines;
for (auto line : LineParser(exeRes.standardOutput.getUnownedSlice()))
{
if (line.getLength() == 0)
{
continue;
}
if (line.indexOf(UnownedStringSlice("hash:")) == -1)
{
continue;
}
hashLines.add(line);
}
res = parseHashes(hashLines, outHashes);
if (SLANG_FAILED(res))
{
msgBuilder << "Failed to parse hash from stdout of 'slang-replay'\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
return SLANG_OK;
}
static SlangResult resultCompare(
List<entryHashInfo> const& expectHashes,
List<entryHashInfo> const& resultHashes)
{
if (expectHashes.getCount() == 0)
{
getTestReporter()->message(TestMessageType::TestFailure, "No hash found\n");
return SLANG_FAIL;
}
StringBuilder msgBuilder;
if (expectHashes.getCount() != resultHashes.getCount())
{
msgBuilder << "The number of hashes doesn't match, expect: " << expectHashes.getCount()
<< ", actual: " << resultHashes.getCount() << "\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
for (Index i = 0; i < expectHashes.getCount(); i++)
{
if (expectHashes[i].targetIndex != resultHashes[i].targetIndex)
{
msgBuilder << "Failed to match 'targetIndex' at index " << i << "\n";
msgBuilder << "Expect: " << expectHashes[i].targetIndex
<< ", actual: " << resultHashes[i].targetIndex << "\n";
getTestReporter()->message(
TestMessageType::TestFailure,
msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
if (expectHashes[i].entryPointIndex != resultHashes[i].entryPointIndex)
{
msgBuilder << "Failed to match 'entryPointIndex' at index " << i << "\n";
msgBuilder << "Expect: " << expectHashes[i].entryPointIndex
<< ", actual: " << resultHashes[i].entryPointIndex << "\n";
getTestReporter()->message(
TestMessageType::TestFailure,
msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
if (expectHashes[i].hash != resultHashes[i].hash)
{
msgBuilder << "Failed to match 'hash' at index " << i << "\n";
msgBuilder << "Expect: " << expectHashes[i].hash << ", actual: " << resultHashes[i].hash
<< "\n";
getTestReporter()->message(
TestMessageType::TestFailure,
msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}
}
return SLANG_OK;
}
static SlangResult cleanupRecordFiles(const String& recordDir)
{
SlangResult res = Path::removeNonEmpty(recordDir.getBuffer());
if (SLANG_FAILED(res))
{
StringBuilder msgBuilder;
msgBuilder << "Failed to remove '" << recordDir << "' directory\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
}
return res;
}
static SlangResult runTest(UnitTestContext* context, const char* testName)
{
// Create unique directory for this test to avoid conflicts
StringBuilder recordDirBuilder;
recordDirBuilder << "slang-record-" << testName;
String recordDir = recordDirBuilder.toString();
List<entryHashInfo> expectHashes;
List<entryHashInfo> resultHashes;
SlangResult res = SLANG_OK;
// Run the example to generate recording
res = runExample(context, testName, recordDir, expectHashes);
if (SLANG_SUCCEEDED(res))
{
// Replay the recording
res = replayExample(context, recordDir, resultHashes);
if (SLANG_SUCCEEDED(res))
{
// Compare results
res = resultCompare(expectHashes, resultHashes);
}
}
// Always cleanup, regardless of success or failure
cleanupRecordFiles(recordDir);
return res;
}
// Those examples all depend on the Vulkan, so we only run them on non-Apple platforms.
// In the future, we may be able to modify the examples further to remove all the render APIs
// such that it can be ran on Apple platforms.
#if !(SLANG_APPLE_FAMILY)
SLANG_UNIT_TEST(RecordReplay_cpu_hello_world)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "cpu-hello-world")));
}
SLANG_UNIT_TEST(RecordReplay_triangle)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "triangle")));
}
SLANG_UNIT_TEST(RecordReplay_ray_tracing)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "ray-tracing")));
}
// This causes a Windows Graphics driver crash.
// Temporarily disabled; issue #8022
#if 0
SLANG_UNIT_TEST(RecordReplay_ray_tracing_pipeline)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "ray-tracing-pipeline")));
}
#endif
SLANG_UNIT_TEST(RecordReplay_autodiff_texture)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "autodiff-texture")));
}
SLANG_UNIT_TEST(RecordReplay_gpu_printing)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "gpu-printing")));
}
#if 0
// These examples requires reflection API to replay, we have to disable
// it for now. "model-viewer",
SLANG_UNIT_TEST(RecordReplay_shader_object)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "shader-object")));
}
SLANG_UNIT_TEST(RecordReplay_model_viewer)
{
SLANG_CHECK(SLANG_SUCCEEDED(runTest(unitTestContext, "model-viewer")));
}
#endif
#endif
|