yum-mirror/slang

Making it easier to work with shaders

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

Jay KwakHandle debug-layer messages in a separate channel (#7988)aefd1e3e0

master
4.2 KiB107 linesraw
1#pragma once
2
3#include "core/slang-render-api-util.h"
4#include "slang.h"
5
6enum class TestResult
7{
8    // NOTE! Must keep in order such that combine is meaningful. That is larger values are higher
9    // precident - and a series of tests that has lots of passes and a fail, is still a fail
10    // overall.
11    Ignored,
12    Pass,
13    ExpectedFail,
14    Fail,
15};
16
17enum class TestMessageType
18{
19    Info,        ///< General info (may not be shown depending on verbosity setting)
20    TestFailure, ///< Describes how a test failure took place
21    RunError,    ///< Describes an error that caused a test not to actually correctly run
22};
23
24class ITestReporter
25{
26public:
27    virtual SLANG_NO_THROW void SLANG_MCALL startTest(const char* testName) = 0;
28    virtual SLANG_NO_THROW void SLANG_MCALL addResult(TestResult result) = 0;
29    virtual SLANG_NO_THROW void SLANG_MCALL
30    addResultWithLocation(TestResult result, const char* testText, const char* file, int line) = 0;
31    virtual SLANG_NO_THROW void SLANG_MCALL
32    addResultWithLocation(bool testSucceeded, const char* testText, const char* file, int line) = 0;
33    virtual SLANG_NO_THROW void SLANG_MCALL addExecutionTime(double time) = 0;
34    virtual SLANG_NO_THROW void SLANG_MCALL message(TestMessageType type, const char* message) = 0;
35    virtual SLANG_NO_THROW void SLANG_MCALL endTest() = 0;
36};
37
38ITestReporter* getTestReporter();
39
40namespace rhi
41{
42class IDebugCallback;
43}
44
45struct UnitTestContext
46{
47    slang::IGlobalSession* slangGlobalSession;
48    const char* workDirectory;
49    const char* executableDirectory;
50    Slang::RenderApiFlags enabledApis;
51    bool enableDebugLayers;
52    rhi::IDebugCallback* debugCallback = nullptr;
53};
54
55typedef void (*UnitTestFunc)(UnitTestContext*);
56
57class IUnitTestModule
58{
59public:
60    virtual SLANG_NO_THROW SlangInt SLANG_MCALL getTestCount() = 0;
61    virtual SLANG_NO_THROW const char* SLANG_MCALL getTestName(SlangInt index) = 0;
62    virtual SLANG_NO_THROW UnitTestFunc SLANG_MCALL getTestFunc(SlangInt index) = 0;
63    virtual SLANG_NO_THROW void SLANG_MCALL setTestReporter(ITestReporter* reporter) = 0;
64    virtual SLANG_NO_THROW void SLANG_MCALL destroy() = 0;
65};
66
67class UnitTestRegisterHelper
68{
69public:
70    UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc);
71};
72
73class AbortTestException
74{
75};
76
77typedef IUnitTestModule* (*UnitTestGetModuleFunc)();
78
79#define SLANG_UNIT_TEST(name)                                    \
80    void _##name##_impl(UnitTestContext* unitTestContext);       \
81    void name(UnitTestContext* unitTestContext)                  \
82    {                                                            \
83        try                                                      \
84        {                                                        \
85            _##name##_impl(unitTestContext);                     \
86        }                                                        \
87        catch (AbortTestException&)                              \
88        {                                                        \
89        }                                                        \
90    }                                                            \
91    UnitTestRegisterHelper _##name##RegisterHelper(#name, name); \
92    void _##name##_impl(UnitTestContext* unitTestContext)
93
94#define SLANG_CHECK(x) getTestReporter()->addResultWithLocation((x), #x, __FILE__, __LINE__);
95#define SLANG_CHECK_ABORT(x)                                                                   \
96    {                                                                                          \
97        bool _slang_check_result = (x);                                                        \
98        getTestReporter()->addResultWithLocation(_slang_check_result, #x, __FILE__, __LINE__); \
99        if (!_slang_check_result)                                                              \
100            throw AbortTestException();                                                        \
101    }
102#define SLANG_IGNORE_TEST                              \
103    getTestReporter()->addResult(TestResult::Ignored); \
104    throw AbortTestException();
105#define SLANG_CHECK_MSG(condition, message) \
106    getTestReporter()                       \
107        ->addResultWithLocation((condition), #condition " " message, __FILE__, __LINE__)