yum-mirror/slang

Making it easier to work with shaders

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

Jay KwakAdd command-line arguments to examples (#7835)13dd01489

master
3.8 KiB162 linesraw
1#pragma once
2
3#include "core/slang-basic.h"
4#include "core/slang-io.h"
5#include "platform/window.h"
6#include "slang-rhi.h"
7#include "test-base.h"
8
9#ifdef _WIN32
10void _Win32OutputDebugString(const char* str);
11#endif
12
13#define SLANG_STRINGIFY(x) #x
14#define SLANG_EXPAND_STRINGIFY(x) SLANG_STRINGIFY(x)
15
16#ifdef _WIN32
17#define EXAMPLE_MAIN(innerMain)                                   \
18    extern const char* const g_logFileName =                      \
19        "log-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \
20    PLATFORM_UI_MAIN(innerMain);
21
22#else
23#define EXAMPLE_MAIN(innerMain) PLATFORM_UI_MAIN(innerMain)
24#endif // _WIN32
25
26static const float kIdentity[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
27
28struct WindowedAppBase : public TestBase
29{
30protected:
31    static const int kSwapchainImageCount = 2;
32
33    Slang::RefPtr<platform::Window> gWindow;
34    uint32_t windowWidth;
35    uint32_t windowHeight;
36
37    Slang::ComPtr<rhi::IDevice> gDevice;
38    Slang::ComPtr<rhi::ICommandQueue> gQueue;
39    Slang::ComPtr<rhi::ISurface> gSurface;
40
41    Slang::List<Slang::ComPtr<rhi::ITexture>> gOfflineTextures;
42
43    Slang::Result initializeBase(
44        const char* title,
45        int width,
46        int height,
47        rhi::DeviceType deviceType = rhi::DeviceType::Default);
48
49    void mainLoop();
50
51    Slang::ComPtr<rhi::ITextureView> createTextureFromFile(
52        Slang::String fileName,
53        int& textureWidth,
54        int& textureHeight);
55
56    void createOfflineTextures();
57
58    virtual void windowSizeChanged();
59
60protected:
61    virtual void renderFrame(rhi::ITexture* texture) = 0;
62
63public:
64    platform::Window* getWindow() { return gWindow.Ptr(); }
65    virtual void finalize() { gQueue->waitOnHost(); }
66    void offlineRender();
67};
68
69struct ExampleResources
70{
71    Slang::String baseDir;
72
73    ExampleResources(const Slang::String& dir)
74        : baseDir(dir)
75    {
76    }
77
78    Slang::String resolveResource(const char* fileName) const
79    {
80        static const Slang::List<Slang::String> directories{
81            "examples",
82            "../examples",
83            "../../examples",
84        };
85
86        for (const Slang::String& dir : directories)
87        {
88            Slang::StringBuilder pathSb;
89            pathSb << dir << "/" << baseDir << "/" << fileName;
90            if (Slang::File::exists(pathSb.getBuffer()))
91                return pathSb.toString();
92        }
93
94        return fileName;
95    }
96};
97
98int64_t getCurrentTime();
99int64_t getTimerFrequency();
100
101template<typename... TArgs>
102inline void reportError(const char* format, TArgs... args)
103{
104    printf(format, std::forward<TArgs>(args)...);
105#ifdef _WIN32
106    char buffer[4096];
107    sprintf_s(buffer, format, std::forward<TArgs>(args)...);
108    _Win32OutputDebugString(buffer);
109#endif
110}
111
112template<typename... TArgs>
113inline void log(const char* format, TArgs... args)
114{
115    reportError(format, args...);
116}
117
118// Many Slang API functions return detailed diagnostic information
119// (error messages, warnings, etc.) as a "blob" of data, or return
120// a null blob pointer instead if there were no issues.
121//
122// For convenience, we define a subroutine that will dump the information
123// in a diagnostic blob if one is produced, and skip it otherwise.
124//
125inline void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob)
126{
127    if (diagnosticsBlob != nullptr)
128    {
129        reportError("%s", (const char*)diagnosticsBlob->getBufferPointer());
130    }
131}
132
133template<typename TApp>
134int innerMain(int argc, char** argv)
135{
136    TApp app;
137
138    app.parseOption(argc, argv);
139
140    if (app.shouldShowHelp())
141    {
142        app.printUsage(argc > 0 ? argv[0] : "example");
143        return 0;
144    }
145
146    if (SLANG_FAILED(app.initialize()))
147    {
148        return -1;
149    }
150
151    if (!app.isTestMode())
152    {
153        platform::Application::run(app.getWindow());
154    }
155    else
156    {
157        app.offlineRender();
158    }
159
160    app.finalize();
161    return 0;
162}