yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Gangzheng TongAdd RHI Device Caching and Test Prefix Exclusion (#8448)ba8132345

master
7.5 KiB218 linesraw
1// test-context.h
2
3#ifndef TEST_CONTEXT_H_INCLUDED
4#define TEST_CONTEXT_H_INCLUDED
5
6#include "../../source/compiler-core/slang-downstream-compiler-util.h"
7#include "../../source/compiler-core/slang-downstream-compiler.h"
8#include "../../source/compiler-core/slang-json-rpc-connection.h"
9#include "../../source/core/slang-dictionary.h"
10#include "../../source/core/slang-platform.h"
11#include "../../source/core/slang-render-api-util.h"
12#include "../../source/core/slang-std-writers.h"
13#include "../../source/core/slang-string-util.h"
14#include "../../source/core/slang-test-tool-util.h"
15#include "filecheck.h"
16#include "options.h"
17#include "slang-com-ptr.h"
18
19#include <mutex>
20
21typedef uint32_t PassThroughFlags;
22struct PassThroughFlag
23{
24    enum Enum : PassThroughFlags
25    {
26        Dxc = 1 << int(SLANG_PASS_THROUGH_DXC),
27        Fxc = 1 << int(SLANG_PASS_THROUGH_FXC),
28        Glslang = 1 << int(SLANG_PASS_THROUGH_GLSLANG),
29        VisualStudio = 1 << int(SLANG_PASS_THROUGH_VISUAL_STUDIO),
30        GCC = 1 << int(SLANG_PASS_THROUGH_GCC),
31        Clang = 1 << int(SLANG_PASS_THROUGH_CLANG),
32        Generic_C_CPP = 1 << int(SLANG_PASS_THROUGH_GENERIC_C_CPP),
33        NVRTC = 1 << int(SLANG_PASS_THROUGH_NVRTC),
34        LLVM = 1 << int(SLANG_PASS_THROUGH_LLVM),
35        Metal = 1 << int(SLANG_PASS_THROUGH_METAL),
36        Tint = 1 << int(SLANG_PASS_THROUGH_TINT),
37    };
38};
39
40/// Structure that describes requirements needs to run - such as rendering APIs or
41/// back-end availability
42struct TestRequirements
43{
44
45    TestRequirements& addUsedRenderApi(Slang::RenderApiType type)
46    {
47        using namespace Slang;
48        if (type != RenderApiType::Unknown)
49        {
50            usedRenderApiFlags |= RenderApiFlags(1) << int(type);
51        }
52        return *this;
53    }
54    TestRequirements& addUsedBackEnd(SlangPassThrough type)
55    {
56        if (type != SLANG_PASS_THROUGH_NONE)
57        {
58            usedBackendFlags |= PassThroughFlags(1) << int(type);
59        }
60        return *this;
61    }
62    TestRequirements& addUsedBackends(PassThroughFlags flags)
63    {
64        usedBackendFlags |= flags;
65        return *this;
66    }
67    TestRequirements& addUsedRenderApis(Slang::RenderApiFlags flags)
68    {
69        usedRenderApiFlags |= flags;
70        return *this;
71    }
72    /// True if has this render api as used
73    bool isUsed(Slang::RenderApiType apiType) const
74    {
75        return (apiType != Slang::RenderApiType::Unknown) &&
76               ((usedRenderApiFlags & (Slang::RenderApiFlags(1) << int(apiType))) != 0);
77    }
78
79    Slang::RenderApiType explicitRenderApi =
80        Slang::RenderApiType::Unknown;            ///< The render api explicitly specified
81    PassThroughFlags usedBackendFlags = 0;        ///< Used backends
82    Slang::RenderApiFlags usedRenderApiFlags = 0; ///< Used render api flags (some might be implied)
83};
84
85struct FileTestInfo : public Slang::RefObject
86{
87};
88
89class TestContext
90{
91public:
92    typedef Slang::TestToolUtil::InnerMainFunc InnerMainFunc;
93    typedef void (*CleanDeviceCacheFunc)();
94
95    /// Get the slang session
96    SlangSession* getSession() const { return m_session; }
97
98    SlangResult init(const char* exePath);
99
100    /// Get the inner main function (from shared library)
101    InnerMainFunc getInnerMainFunc(const Slang::String& dirPath, const Slang::String& name);
102    /// Set the function for the shared library
103    void setInnerMainFunc(const Slang::String& name, InnerMainFunc func);
104
105    /// Get the device cache cleanup function (from shared library)
106    CleanDeviceCacheFunc getCleanDeviceCacheFunc(const Slang::String& name);
107
108    void setTestRequirements(TestRequirements* req);
109
110    TestRequirements* getTestRequirements() const;
111
112    /// If true tests aren't being run just the information on testing is being accumulated
113    bool isCollectingRequirements() const { return getTestRequirements() != nullptr; }
114    /// If set, then tests are executed
115    bool isExecuting() const { return getTestRequirements() == nullptr; }
116
117    /// True if a render API filter is enabled
118    bool isRenderApiFilterEnabled() const
119    {
120        return options.enabledApis != Slang::RenderApiFlag::AllOf && options.enabledApis != 0;
121    }
122
123    /// True if a test with the requiredFlags can in principal run (it may not be possible if the
124    /// API is not available though)
125    bool canRunTestWithRenderApiFlags(Slang::RenderApiFlags requiredFlags);
126
127    /// True if can run unit tests
128    bool canRunUnitTests() const { return options.apiOnly == false; }
129
130    /// Given a spawn type, return the final spawn type.
131    /// In particular we want 'Default' spawn type to vary by the environment (for example running
132    /// on test server on CI)
133    SpawnType getFinalSpawnType(SpawnType spawnType);
134
135    SpawnType getFinalSpawnType();
136
137    /// Get compiler set
138    Slang::DownstreamCompilerSet* getCompilerSet();
139    Slang::IDownstreamCompiler* getDefaultCompiler(SlangSourceLanguage sourceLanguage);
140
141    Slang::JSONRPCConnection* getOrCreateJSONRPCConnection();
142    void destroyRPCConnection();
143
144    /// Ctor
145    TestContext();
146    /// Dtor
147    ~TestContext();
148
149    Options options;
150    TestCategorySet categorySet;
151
152    /// If set then tests are not run, but their requirements are set
153
154    PassThroughFlags availableBackendFlags = 0;
155    Slang::RenderApiFlags availableRenderApiFlags = 0;
156    bool isAvailableRenderApiFlagsValid = false;
157
158    Slang::RefPtr<Slang::DownstreamCompilerSet> compilerSet;
159
160    Slang::String exeDirectoryPath;
161    Slang::String dllDirectoryPath;
162    Slang::String exePath;
163
164    /// Timeout time for communication over connection.
165    /// NOTE! If the timeout is hit, the connection will be destroyed, and then recreated.
166    /// To test it, compile the core module, if it takes too much time, the core module will be
167    /// repeatedly compiled and each time fail.
168    /// NOTE! This timeout may be altered in the ctor for a specific target, the initializatoin
169    /// value is just the default.
170    ///
171    /// TODO(JS): We could split the core module compilation from other actions, and have timeout
172    /// specific for that. To do this we could have a 'compileCoreModule' RPC method.
173    ///
174    /// Current default is 60 seconds.
175    Slang::Int connectionTimeOutInMs = 60 * 1000;
176
177    void setThreadIndex(int index);
178    void setMaxTestRunnerThreadCount(int count);
179
180    void setTestReporter(TestReporter* reporter);
181    TestReporter* getTestReporter();
182    SlangResult createLanguageServerJSONRPCConnection(Slang::RefPtr<Slang::JSONRPCConnection>& out);
183
184    std::mutex mutex;
185    Slang::RefPtr<Slang::JSONRPCConnection> m_languageServerConnection;
186
187    bool isRetry = false;
188    std::mutex mutexFailedTests;
189    Slang::List<Slang::RefPtr<FileTestInfo>> failedFileTests;
190    Slang::List<Slang::String> failedUnitTests;
191
192    Slang::IFileCheck* getFileCheck() { return m_fileCheck; };
193
194protected:
195    SlangResult _createJSONRPCConnection(Slang::RefPtr<Slang::JSONRPCConnection>& out);
196
197    SlangResult locateFileCheck();
198
199    struct SharedLibraryTool
200    {
201        Slang::ComPtr<ISlangSharedLibrary> m_sharedLibrary;
202        InnerMainFunc m_func;
203        CleanDeviceCacheFunc m_cleanDeviceCacheFunc;
204    };
205
206    Slang::List<Slang::RefPtr<Slang::JSONRPCConnection>> m_jsonRpcConnections;
207    Slang::List<TestReporter*> m_reporters;
208    Slang::List<TestRequirements*> m_testRequirements = nullptr;
209
210    Slang::ComPtr<SlangSession> m_session;
211
212    Slang::Dictionary<Slang::String, SharedLibraryTool> m_sharedLibTools;
213
214    Slang::ComPtr<ISlangSharedLibrary> m_fileCheckLibrary;
215    Slang::ComPtr<Slang::IFileCheck> m_fileCheck;
216};
217
218#endif // TEST_CONTEXT_H_INCLUDED