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

#include "slang-gfx.h"
#include "tools/platform/window.h"
#include "source/core/slang-basic.h"
#include "source/core/slang-io.h"

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

struct WindowedAppBase
{
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 createSwapchainFramebuffers();
    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(); }
};

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, args...);
#ifdef _WIN32
    char buffer[4096];
    sprintf_s(buffer, format, 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()
{
    initDebugCallback();

    TApp app;

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

    platform::Application::run(app.getWindow());

    app.finalize();
    return 0;
}