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

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

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

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

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

    Slang::ComPtr<gfx::IDevice> gDevice;

    Slang::ComPtr<gfx::ISwapchain> gSwapchain;
    Slang::ComPtr<gfx::IFramebufferLayout> gFramebufferLayout;
    Slang::List<Slang::ComPtr<gfx::IFramebuffer>> gFramebuffers;
    Slang::List<Slang::ComPtr<gfx::ITransientResourceHeap>> gTransientHeaps;
    Slang::ComPtr<gfx::IRenderPassLayout> gRenderPass;
    Slang::ComPtr<gfx::ICommandQueue> gQueue;

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

    void createFramebuffers(
        uint32_t width,
        uint32_t height,
        gfx::Format colorFormat,
        uint32_t frameBufferCount);
    void createSwapchainFramebuffers();
    void createOfflineFramebuffers();

    void mainLoop();

    Slang::ComPtr<gfx::IResourceView> createTextureFromFile(
        Slang::String fileName,
        int& textureWidth,
        int& textureHeight);
    virtual void windowSizeChanged();

protected:
    virtual void renderFrame(int framebufferIndex) = 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());
    }
}

void initDebugCallback();

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

    TApp app;

    app.parseOption(argc, argv);
    if (SLANG_FAILED(app.initialize()))
    {
        return -1;
    }

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

    app.finalize();
    return 0;
}