summaryrefslogtreecommitdiffstats
path: root/examples/example-base/example-base.h
blob: fbb54748af5eb51dab40eedf90e0bd29ecc6c793 (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
#pragma once

#include "core/slang-basic.h"
#include "core/slang-io.h"
#include "platform/window.h"
#include "slang-rhi.h"
#include "test-base.h"

#ifdef _WIN32
void _Win32OutputDebugString(const char* str);
#endif

#define SLANG_STRINGIFY(x) #x
#define SLANG_EXPAND_STRINGIFY(x) SLANG_STRINGIFY(x)

#ifdef _WIN32
#define EXAMPLE_MAIN(innerMain)                                   \
    extern const char* const g_logFileName =                      \
        "log-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \
    PLATFORM_UI_MAIN(innerMain);

#else
#define EXAMPLE_MAIN(innerMain) PLATFORM_UI_MAIN(innerMain)
#endif // _WIN32

static const float kIdentity[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};

struct WindowedAppBase : public TestBase
{
protected:
    static const int kSwapchainImageCount = 2;

    Slang::RefPtr<platform::Window> gWindow;
    uint32_t windowWidth;
    uint32_t windowHeight;

    Slang::ComPtr<rhi::IDevice> gDevice;
    Slang::ComPtr<rhi::ICommandQueue> gQueue;
    Slang::ComPtr<rhi::ISurface> gSurface;

    Slang::List<Slang::ComPtr<rhi::ITexture>> gOfflineTextures;

    Slang::Result initializeBase(
        const char* title,
        int width,
        int height,
        rhi::DeviceType deviceType = rhi::DeviceType::Default);

    void mainLoop();

    Slang::ComPtr<rhi::ITextureView> createTextureFromFile(
        Slang::String fileName,
        int& textureWidth,
        int& textureHeight);

    void createOfflineTextures();

    virtual void windowSizeChanged();

protected:
    virtual void renderFrame(rhi::ITexture* texture) = 0;

public:
    platform::Window* getWindow() { return gWindow.Ptr(); }
    virtual void finalize() { gQueue->waitOnHost(); }
    void offlineRender();
};

struct ExampleResources
{
    Slang::String baseDir;

    ExampleResources(const Slang::String& dir)
        : baseDir(dir)
    {
    }

    Slang::String resolveResource(const char* fileName) const
    {
        static const Slang::List<Slang::String> directories{
            "examples",
            "../examples",
            "../../examples",
        };

        for (const Slang::String& dir : directories)
        {
            Slang::StringBuilder pathSb;
            pathSb << dir << "/" << baseDir << "/" << fileName;
            if (Slang::File::exists(pathSb.getBuffer()))
                return pathSb.toString();
        }

        return fileName;
    }
};

int64_t getCurrentTime();
int64_t getTimerFrequency();

template<typename... TArgs>
inline void reportError(const char* format, TArgs... args)
{
    printf(format, std::forward<TArgs>(args)...);
#ifdef _WIN32
    char buffer[4096];
    sprintf_s(buffer, format, std::forward<TArgs>(args)...);
    _Win32OutputDebugString(buffer);
#endif
}

template<typename... TArgs>
inline void log(const char* format, TArgs... args)
{
    reportError(format, args...);
}

// Many Slang API functions return detailed diagnostic information
// (error messages, warnings, etc.) as a "blob" of data, or return
// a null blob pointer instead if there were no issues.
//
// For convenience, we define a subroutine that will dump the information
// in a diagnostic blob if one is produced, and skip it otherwise.
//
inline void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob)
{
    if (diagnosticsBlob != nullptr)
    {
        reportError("%s", (const char*)diagnosticsBlob->getBufferPointer());
    }
}

template<typename TApp>
int innerMain(int argc, char** argv)
{
    TApp app;

    app.parseOption(argc, argv);

    if (app.shouldShowHelp())
    {
        app.printUsage(argc > 0 ? argv[0] : "example");
        return 0;
    }

    if (SLANG_FAILED(app.initialize()))
    {
        return -1;
    }

    if (!app.isTestMode())
    {
        platform::Application::run(app.getWindow());
    }
    else
    {
        app.offlineRender();
    }

    app.finalize();
    return 0;
}