hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/rendering/render_path.cpp
blob: e3ca366b7816aeefe2613b7ffb0e3e7e8f34bf7d (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "render_path.h"

#include "scene/scene.h"
#include "core/log.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <glm/gtc/matrix_transform.hpp>
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <ctime>
#include <cstdio>
#include <vector>

namespace Donut
{
    auto RenderPath::init(RHI::Device& device, const std::string& hdri_path) -> bool
    {
        m_device = &device;
        m_hdri_path = hdri_path;
        m_cubemap = device.create_cubemap_from_hdri(hdri_path);

        m_scene_renderer = create_scope<SceneRenderer>();
        m_scene_renderer->init(device);
        m_black_hole_renderer = create_scope<BlackHoleRenderer>();
        m_black_hole_renderer->init(device);
        return true;
    }

    auto RenderPath::sync_hdri(const std::string& hdri_path) -> void
    {
        if (hdri_path == m_hdri_path || !m_device) return;
        m_device->wait_idle();
        m_cubemap = m_device->create_cubemap_from_hdri(hdri_path);
        m_hdri_path = hdri_path;
    }

    auto RenderPath::render(RHI::CommandList& cmd, Scene& scene, View view,
                            int fb_width, int fb_height, bool moving, float time) -> void
    {
        const glm::vec4 clear(0.05f, 0.06f, 0.10f, 1.0f);

        if (view != View::BlackHole)
        {
            cmd.begin_render_pass(nullptr, clear);
            if (view == View::Scene)
            {
                CameraView cv;
                cv.view       = scene.scene_camera.get_view_matrix();
                cv.projection = scene.scene_camera.get_projection_matrix();
                cv.position   = scene.scene_camera.get_orbital_position();
                cv.fb_width   = fb_width; cv.fb_height = fb_height;
                m_scene_renderer->render(cmd, cv, scene.objects, scene.selected_object, m_cubemap.get());
            }
            m_device->imgui_render(cmd);
            cmd.end_render_pass();
        }
        else
        {
            GeodesicView gv;
            glm::vec3 pos, fwd;
            if (scene.sim_camera.get_camera_mode() == CameraMode::FPS)
            {
                pos = scene.sim_camera.get_position();
                fwd = scene.sim_camera.get_forward_direction();
            }
            else
            {
                pos = scene.sim_camera.get_orbital_position();
                fwd = glm::normalize(scene.sim_camera.get_orbital_target() - pos);
            }
            glm::vec3 right = glm::normalize(glm::cross(fwd, glm::vec3(0, 1, 0)));
            gv.position = pos; gv.right = right; gv.up = glm::cross(right, fwd); gv.forward = fwd;
            gv.tan_half_fov = (float)tan(glm::radians(scene.black_hole.fov_degrees * 0.5f));
            gv.aspect = (float)BlackHoleRenderer::GEO_HI_W / (float)BlackHoleRenderer::GEO_HI_H;
            gv.moving = moving;
            gv.time = time;

            m_black_hole_renderer->render_geodesic(cmd, gv, scene.black_hole, scene.objects, m_cubemap.get());
            cmd.begin_render_pass(nullptr, clear);
            m_black_hole_renderer->blit(cmd, fb_width, fb_height);
            m_device->imgui_render(cmd);
            cmd.end_render_pass();
        }
    }

    auto RenderPath::shutdown() -> void
    {
        m_scene_renderer.reset();
        m_black_hole_renderer.reset();
        m_cubemap.reset();
    }

    static auto write_pfm(const std::string& path, int w, int h, const std::vector<float>& rgba) -> bool
    {
        std::FILE* f = std::fopen(path.c_str(), "wb");
        if (!f) return false;
        std::fprintf(f, "PF\n%d %d\n-1.0\n", w, h);
        std::vector<float> rgb((size_t)w * 3);
        for (int y = h - 1; y >= 0; --y)
        {
            const float* src = &rgba[(size_t)y * w * 4];
            for (int x = 0; x < w; ++x) { rgb[x*3+0] = src[x*4+0]; rgb[x*3+1] = src[x*4+1]; rgb[x*3+2] = src[x*4+2]; }
            std::fwrite(rgb.data(), sizeof(float), (size_t)w * 3, f);
        }
        std::fclose(f);
        return true;
    }

    static auto write_csv(const std::string& path, int w, int h, const std::vector<float>& rgba) -> bool
    {
        std::FILE* f = std::fopen(path.c_str(), "wb");
        if (!f) return false;
        for (int y = 0; y < h; ++y)
        {
            for (int x = 0; x < w; ++x) std::fprintf(f, x ? ",%g" : "%g", rgba[((size_t)y * w + x) * 4]);
            std::fputc('\n', f);
        }
        std::fclose(f);
        return true;
    }

    auto RenderPath::export_frame(Scene& scene, const ExportConfig& cfg) -> int
    {
        if (!m_device) return 0;
        const int  w = std::max(cfg.width, 1), h = std::max(cfg.height, 1);
        const bool raw = cfg.format != ExportFormat::Png;
        auto target = m_device->create_render_target(
            w, h, raw ? RHI::Format::RGBA32F : RHI::Format::RGBA8, RHI::Format::None, RHI::Filter::Nearest);

        GeodesicView gv;
        glm::vec3 pos, fwd;
        if (scene.sim_camera.get_camera_mode() == CameraMode::FPS)
        { pos = scene.sim_camera.get_position(); fwd = scene.sim_camera.get_forward_direction(); }
        else
        { pos = scene.sim_camera.get_orbital_position(); fwd = glm::normalize(scene.sim_camera.get_orbital_target() - pos); }
        glm::vec3 right = glm::normalize(glm::cross(fwd, glm::vec3(0, 1, 0)));
        gv.position = pos; gv.right = right; gv.up = glm::cross(right, fwd); gv.forward = fwd;
        gv.tan_half_fov = (float)tan(glm::radians(scene.black_hole.fov_degrees * 0.5f));
        gv.aspect = (float)w / (float)h;
        gv.moving = false;
        gv.time = 0.0f;

        std::error_code ec; std::filesystem::create_directories(cfg.directory, ec);
        char stamp[32]; std::time_t t = std::time(nullptr);
        std::strftime(stamp, sizeof(stamp), "%Y%m%d_%H%M%S", std::localtime(&t));
        const char* ext = cfg.format == ExportFormat::Pfm ? "pfm" : cfg.format == ExportFormat::Csv ? "csv" : "png";

        int written = 0;
        std::vector<uint8_t> px8; std::vector<float> pxf;
        auto do_channel = [&](bool enabled, int channel, const char* name)
        {
            if (!enabled) return;
            m_device->run_offscreen([&](RHI::CommandList& cmd) {
                m_black_hole_renderer->render_export(cmd, target.get(), gv, scene.black_hole,
                                                     scene.objects, m_cubemap.get(), channel, raw);
            });
            std::string path = cfg.directory + "/donut_" + name + "_" + stamp + "." + ext;
            bool ok;
            if (cfg.format == ExportFormat::Png)
            { m_device->read_render_target(target.get(), px8); ok = stbi_write_png(path.c_str(), w, h, 4, px8.data(), w * 4) != 0; }
            else
            {
                m_device->read_render_target_float(target.get(), pxf);
                ok = (cfg.format == ExportFormat::Pfm) ? write_pfm(path, w, h, pxf) : write_csv(path, w, h, pxf);
            }
            if (ok) { DONUT_INFO("Exported {}", path); ++written; } else DONUT_ERROR("Export failed: {}", path);
        };
        do_channel(cfg.color,       0, "color");
        do_channel(cfg.redshift,    1, "redshift");
        do_channel(cfg.temperature, 2, "temperature");
        do_channel(cfg.impact,      3, "impact");
        return written;
    }
}