summaryrefslogtreecommitdiff
path: root/source/slang-capture-replay/slang-global-session.cpp
blob: b8dfd7d40f95f83c33b13f541b0d543bf9dfa691 (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
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
#include "slang-global-session.h"
#include "slang-session.h"
#include "slang-filesystem.h"
#include "../slang/slang-compiler.h"
#include "capture_utility.h"

namespace SlangCapture
{
    // constructor is called in slang_createGlobalSession
    GlobalSessionCapture::GlobalSessionCapture(slang::IGlobalSession* session):
        m_actualGlobalSession(session)
    {
        SLANG_CAPTURE_ASSERT(m_actualGlobalSession != nullptr);

        m_globalSessionHandle = reinterpret_cast<SlangCapture::AddressFormat>(m_actualGlobalSession.get());
        m_captureManager = std::make_unique<CaptureManager>(m_globalSessionHandle);

        // We will use the address of the global session as the filename for the capture manager
        // to make it unique for each global session.
        // capture slang::createGlobalSession
        ParameterEncoder* encoder = m_captureManager->beginMethodCapture(ApiCallId::ICreateGlobalSession, g_globalFunctionHandle);
        encoder->encodeAddress(m_actualGlobalSession);
        m_captureManager->endMethodCapture();
    }

    GlobalSessionCapture::~GlobalSessionCapture()
    {
        m_actualGlobalSession->release();
    }

    ISlangUnknown* GlobalSessionCapture::getInterface(const Guid& guid)
    {
        if(guid == ISlangUnknown::getTypeGuid() || guid == IGlobalSession::getTypeGuid())
            return asExternal(this);
        return nullptr;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::createSession(slang::SessionDesc const&  desc, slang::ISession** outSession)
    {
        setLogLevel();
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        slang::ISession* actualSession = nullptr;

        ParameterEncoder* encoder{};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_createSession, m_globalSessionHandle);
            encoder->encodeStruct(desc);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->createSession(desc, &actualSession);

        {   // capture output
            encoder->encodeAddress(actualSession);
            m_captureManager->endMethodCaptureAppendOutput();
        }

        if (actualSession != nullptr)
        {
            // reset the file system to our capture file system. After createSession() call,
            // the Linkage will set to user provided file system or slang default file system.
            // We need to reset it to our capture file system
            Slang::Linkage* linkage = static_cast<Linkage*>(actualSession);
            FileSystemCapture* fileSystemCapture = new FileSystemCapture(linkage->getFileSystemExt(), m_captureManager.get());

            Slang::ComPtr<FileSystemCapture> resultFileSystemCapture(fileSystemCapture);
            linkage->setFileSystem(resultFileSystemCapture.detach());

            SessionCapture* sessionCapture = new SessionCapture(actualSession, m_captureManager.get());
            Slang::ComPtr<SessionCapture> result(sessionCapture);
            *outSession = result.detach();
        }

        return res;
    }

    SLANG_NO_THROW SlangProfileID SLANG_MCALL GlobalSessionCapture::findProfile(char const* name)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_findProfile, m_globalSessionHandle);
            encoder->encodeString(name);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangProfileID profileId = m_actualGlobalSession->findProfile(name);
        return profileId;
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::setDownstreamCompilerPath(SlangPassThrough passThrough, char const* path)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_setDownstreamCompilerPath, m_globalSessionHandle);
            encoder->encodeEnumValue(passThrough);
            encoder->encodeString(path);
            m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->setDownstreamCompilerPath(passThrough, path);
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::setDownstreamCompilerPrelude(SlangPassThrough inPassThrough, char const* prelude)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_setDownstreamCompilerPrelude, m_globalSessionHandle);
            encoder->encodeEnumValue(inPassThrough);
            encoder->encodeString(prelude);
            m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->setDownstreamCompilerPrelude(inPassThrough, prelude);
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::getDownstreamCompilerPrelude(SlangPassThrough inPassThrough, ISlangBlob** outPrelude)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_getDownstreamCompilerPrelude, m_globalSessionHandle);
            encoder->encodeEnumValue(inPassThrough);
            encoder = m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->getDownstreamCompilerPrelude(inPassThrough, outPrelude);

        {
            encoder->encodeAddress(*outPrelude);
            m_captureManager->endMethodCaptureAppendOutput();
        }
    }

    SLANG_NO_THROW const char* SLANG_MCALL GlobalSessionCapture::getBuildTagString()
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        // No need to capture this function. It's just a query function and it won't impact the internal state.
        const char* resStr = m_actualGlobalSession->getBuildTagString();
        return resStr;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::setDefaultDownstreamCompiler(SlangSourceLanguage sourceLanguage, SlangPassThrough defaultCompiler)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_setDefaultDownstreamCompiler, m_globalSessionHandle);
            encoder->encodeEnumValue(sourceLanguage);
            encoder->encodeEnumValue(defaultCompiler);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->setDefaultDownstreamCompiler(sourceLanguage, defaultCompiler);
        return res;
    }

    SLANG_NO_THROW SlangPassThrough SLANG_MCALL GlobalSessionCapture::getDefaultDownstreamCompiler(SlangSourceLanguage sourceLanguage)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_getDefaultDownstreamCompiler, m_globalSessionHandle);
            encoder->encodeEnumValue(sourceLanguage);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangPassThrough passThrough = m_actualGlobalSession->getDefaultDownstreamCompiler(sourceLanguage);
        return passThrough;
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::setLanguagePrelude(SlangSourceLanguage inSourceLanguage, char const* prelude)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_setLanguagePrelude, m_globalSessionHandle);
            encoder->encodeEnumValue(inSourceLanguage);
            encoder->encodeString(prelude);
            encoder = m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->setLanguagePrelude(inSourceLanguage, prelude);
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::getLanguagePrelude(SlangSourceLanguage inSourceLanguage, ISlangBlob** outPrelude)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_getLanguagePrelude, m_globalSessionHandle);
            encoder->encodeEnumValue(inSourceLanguage);
            encoder = m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->getLanguagePrelude(inSourceLanguage, outPrelude);

        {
            encoder->encodeAddress(*outPrelude);
            m_captureManager->endMethodCaptureAppendOutput();
        }
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::createCompileRequest(slang::ICompileRequest** outCompileRequest)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_createCompileRequest, m_globalSessionHandle);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->createCompileRequest(outCompileRequest);

        {
            encoder->encodeAddress(*outCompileRequest);
            m_captureManager->endMethodCaptureAppendOutput();
        }

        return res;
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::addBuiltins(char const* sourcePath, char const* sourceString)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_addBuiltins, m_globalSessionHandle);
            encoder->encodeString(sourcePath);
            encoder->encodeString(sourceString);
            encoder = m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->addBuiltins(sourcePath, sourceString);
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::setSharedLibraryLoader(ISlangSharedLibraryLoader* loader)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        // TODO: Not sure if we need to capture this function. Because this functions is something like the file system
        // override, it's provided by user code. So capturing it makes no sense. The only way is to wrapper this interface
        // by our own implementation, and capture it there.
        m_actualGlobalSession->setSharedLibraryLoader(loader);
    }

    SLANG_NO_THROW ISlangSharedLibraryLoader* SLANG_MCALL GlobalSessionCapture::getSharedLibraryLoader()
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_getSharedLibraryLoader, m_globalSessionHandle);
            encoder = m_captureManager->endMethodCapture();
        }

        ISlangSharedLibraryLoader* loader = m_actualGlobalSession->getSharedLibraryLoader();

        {
            encoder->encodeAddress(loader);
            m_captureManager->endMethodCaptureAppendOutput();
        }
        return loader;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::checkCompileTargetSupport(SlangCompileTarget target)
    {
        // No need to capture this function. It's just a query function and it won't impact the internal state.
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        SlangResult res = m_actualGlobalSession->checkCompileTargetSupport(target);
        return res;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::checkPassThroughSupport(SlangPassThrough passThrough)
    {
        // No need to capture this function. It's just a query function and it won't impact the internal state.
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        SlangResult res = m_actualGlobalSession->checkPassThroughSupport(passThrough);
        return res;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::compileStdLib(slang::CompileStdLibFlags flags)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_compileStdLib, m_globalSessionHandle);
            encoder->encodeEnumValue(flags);
            m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->compileStdLib(flags);
        return res;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::loadStdLib(const void* stdLib, size_t stdLibSizeInBytes)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_loadStdLib, m_globalSessionHandle);
            encoder->encodePointer(stdLib, false, stdLibSizeInBytes);
            m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->loadStdLib(stdLib, stdLibSizeInBytes);
        return res;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::saveStdLib(SlangArchiveType archiveType, ISlangBlob** outBlob)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_saveStdLib, m_globalSessionHandle);
            encoder->encodeEnumValue(archiveType);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->saveStdLib(archiveType, outBlob);

        {
            encoder->encodeAddress(*outBlob);
            m_captureManager->endMethodCaptureAppendOutput();
        }
        return res;
    }

    SLANG_NO_THROW SlangCapabilityID SLANG_MCALL GlobalSessionCapture::findCapability(char const* name)
    {
        // No need to capture this function. It's just a query function and it won't impact the internal state.
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        SlangCapabilityID capId = m_actualGlobalSession->findCapability(name);
        return capId;
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::setDownstreamCompilerForTransition(SlangCompileTarget source, SlangCompileTarget target, SlangPassThrough compiler)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_setDownstreamCompilerForTransition, m_globalSessionHandle);
            encoder->encodeEnumValue(source);
            encoder->encodeEnumValue(target);
            encoder->encodeEnumValue(compiler);
            m_captureManager->endMethodCapture();
        }

        m_actualGlobalSession->setDownstreamCompilerForTransition(source, target, compiler);
    }

    SLANG_NO_THROW SlangPassThrough SLANG_MCALL GlobalSessionCapture::getDownstreamCompilerForTransition(SlangCompileTarget source, SlangCompileTarget target)
    {
        // No need to capture this function. It's just a query function and it won't impact the internal state.
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        SlangPassThrough passThrough = m_actualGlobalSession->getDownstreamCompilerForTransition(source, target);
        return passThrough;
    }

    SLANG_NO_THROW void SLANG_MCALL GlobalSessionCapture::getCompilerElapsedTime(double* outTotalTime, double* outDownstreamTime)
    {
        // No need to capture this function. It's just a query function and it won't impact the internal state.
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);
        m_actualGlobalSession->getCompilerElapsedTime(outTotalTime, outDownstreamTime);
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::setSPIRVCoreGrammar(char const* jsonPath)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_setSPIRVCoreGrammar, m_globalSessionHandle);
            encoder->encodeString(jsonPath);
            m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->setSPIRVCoreGrammar(jsonPath);
        return res;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::parseCommandLineArguments(
        int argc, const char* const* argv, slang::SessionDesc* outSessionDesc, ISlangUnknown** outAllocation)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_parseCommandLineArguments, m_globalSessionHandle);
            encoder->encodeStringArray(argv, argc);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->parseCommandLineArguments(argc, argv, outSessionDesc, outAllocation);

        {
            encoder->encodeStruct(*outSessionDesc);
            encoder->encodeAddress(*outAllocation);
            m_captureManager->endMethodCaptureAppendOutput();
        }
        return res;
    }

    SLANG_NO_THROW SlangResult SLANG_MCALL GlobalSessionCapture::getSessionDescDigest(slang::SessionDesc* sessionDesc, ISlangBlob** outBlob)
    {
        slangCaptureLog(LogLevel::Verbose, "%p: %s\n", m_actualGlobalSession.get(), __PRETTY_FUNCTION__);

        ParameterEncoder* encoder {};
        {
            encoder = m_captureManager->beginMethodCapture(ApiCallId::IGlobalSession_getSessionDescDigest, m_globalSessionHandle);
            encoder->encodeStruct(*sessionDesc);
            encoder = m_captureManager->endMethodCapture();
        }

        SlangResult res = m_actualGlobalSession->getSessionDescDigest(sessionDesc, outBlob);

        {
            encoder->encodeAddress(*outBlob);
            m_captureManager->endMethodCaptureAppendOutput();
        }
        return res;
    }
}