summaryrefslogtreecommitdiff
path: root/src/app.cpp
blob: 11a219dccda93fe23acd93b662174ba34d470a08 (plain) (blame)
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
#include "imgui.h"
#include "vulkan_swapchain.h"

#include "imgui_impl_vulkan.h"
#include "argparse/argparse.hpp"
#include <cstdint>
#include <spdlog/spdlog.h>
#include <spdlog/cfg/env.h>
#include <sys/types.h>
#include <vk_mem_alloc.h>
#include <vulkan/vulkan_core.h>

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include <cstdlib>
#include <iostream>
#include <vector>

int main(int argc, char** argv) {
    spdlog::cfg::load_env_levels();
    argparse::ArgumentParser program("Iris Renderer");
    program.add_argument("width")
        .help("display width of the window")
        .scan<'i', int>();
    program.add_argument("height")
        .help("display height of the window")
        .scan<'i', int>();
    
    try {
        program.parse_args(argc, argv);
    } catch (const std::exception& err) {
        std::cerr << err.what() << std::endl;
        std::cerr << program;
        return 1;
    }

    int window_width = program.get<int>("width");
    int window_height = program.get<int>("height");

    if (!glfwInit()) {
        const char* description = nullptr;
        glfwGetError(&description);
        if (description != nullptr) {
            spdlog::critical("Failed to initialize GLFW: {}", description);
        } else {
            spdlog::critical("Failed to initialize GLFW");
        }

        glfwTerminate();
        return -1;
    }
    auto glfw_extensions = get_glfw_instance_extensions();
    std::vector<std::string> layers;
    iris::Device device(layers, glfw_extensions);

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    auto window = glfwCreateWindow(window_width, window_height, "Iris Renderer", nullptr, nullptr);
    if (window == nullptr) {
        spdlog::critical("Failed to create GLFW window");
        abort();
    }

    // load debug image
    iris::Texture2D debug_texture = device.create_texture_from_image(
        "assets/debug.png",
        VK_FORMAT_R8G8B8A8_UNORM,
        VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
        VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
        VK_IMAGE_USAGE_TRANSFER_DST_BIT,
        VmaAllocationCreateInfo {
            .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
            .usage = VMA_MEMORY_USAGE_AUTO,
        });
    // end load debug image

    auto swapchain = iris::Swapchain(window, device, window_width, window_height);
    while (!glfwWindowShouldClose(swapchain.window)) {
        glfwPollEvents();

        int display_w, display_h;
        glfwGetFramebufferSize(swapchain.window, &display_w, &display_h);
        if (display_w == 0 || display_h == 0) {
            break;
        }

        swapchain.needs_recreate |= 
            (uint32_t) display_w != swapchain.width || 
            (uint32_t) display_h != swapchain.height;
        if (swapchain.needs_recreate) {
            swapchain.resize(display_w, display_h);
            swapchain.needs_recreate = false;
        }

        ImGui_ImplVulkan_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        swapchain.start_frame();
        ImGui::NewFrame();
        ImGui::ShowDemoWindow();
        ImGui::Render();
        swapchain.display(debug_texture);
    }

    debug_texture->release();
    ImGui_ImplVulkan_Shutdown();

    glfwDestroyWindow(swapchain.window);
    glfwTerminate();
    swapchain.release();
    device.destroy();

    return 0;
}