Skip to content

Commit

Permalink
fix compile with new compute
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed May 4, 2024
1 parent f16a55f commit 7eb5927
Show file tree
Hide file tree
Showing 17 changed files with 641 additions and 334 deletions.
1 change: 0 additions & 1 deletion src/base/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void ProgressiveIntegrator::Instance::_render_one_camera(

auto pixel_count = resolution.x * resolution.y;
sampler()->reset(command_buffer, resolution, pixel_count, spp);
command_buffer << pipeline().printer().reset();
command_buffer << compute::synchronize();

LUISA_INFO(
Expand Down
5 changes: 2 additions & 3 deletions src/base/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace luisa::render {
inline Pipeline::Pipeline(Device &device) noexcept
: _device{device},
_bindless_array{device.create_bindless_array(bindless_array_capacity)},
_general_buffer_arena{luisa::make_unique<BufferArena>(device, 16_M)},
_printer{luisa::make_unique<compute::Printer>(device)} {}
_general_buffer_arena{luisa::make_unique<BufferArena>(device, 16_M)} {}

Pipeline::~Pipeline() noexcept = default;

Expand Down Expand Up @@ -50,7 +49,7 @@ luisa::unique_ptr<Pipeline> Pipeline::create(Device &device, Stream &stream, con
pipeline->_differentiation =
luisa::make_unique<Differentiation>(*pipeline, stream);
}
stream << pipeline->printer().reset();
stream << synchronize();
auto initial_time = std::numeric_limits<float>::max();
for (auto c : scene.cameras()) {
if (c->shutter_span().x < initial_time) {
Expand Down
4 changes: 0 additions & 4 deletions src/base/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ using compute::Image;
using compute::Mesh;
using compute::PixelStorage;
using compute::Polymorphic;
using compute::Printer;
using compute::Ray;
using compute::Resource;
using compute::Triangle;
Expand Down Expand Up @@ -96,7 +95,6 @@ class Pipeline {
Buffer<float4x4> _transform_matrix_buffer;
luisa::unordered_map<luisa::string, uint> _named_ids;
// other things
luisa::unique_ptr<Printer> _printer;
bool _any_dynamic_transforms{false};

public:
Expand Down Expand Up @@ -220,8 +218,6 @@ class Pipeline {
void render(Stream &stream) noexcept;
luisa::vector<void *> render_diff(Stream &stream, luisa::vector<Buffer<float>> &grads) noexcept;
luisa::vector<void*> render_with_return(Stream &stream) noexcept;
[[nodiscard]] auto &printer() noexcept { return *_printer; }
[[nodiscard]] auto &printer() const noexcept { return *_printer; }
[[nodiscard]] uint named_id(luisa::string_view name) const noexcept;
template<typename T, typename I>
[[nodiscard]] auto buffer(I &&i) const noexcept { return _bindless_array->buffer<T>(std::forward<I>(i)); }
Expand Down
137 changes: 103 additions & 34 deletions src/films/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Created by Mike Smith on 2023/6/14.
//

#include <imgui.h>

#include <runtime/image.h>
#include <runtime/swapchain.h>

#include <gui/window.h>
#include <gui/imgui_window.h>

#include <base/film.h>
#include <base/scene.h>
Expand Down Expand Up @@ -96,15 +98,20 @@ class DisplayInstance final : public Film::Instance {

private:
luisa::unique_ptr<Film::Instance> _base;
luisa::unique_ptr<Window> _window;
luisa::unique_ptr<ImGuiWindow> _window;
Image<float> _framebuffer;
Swapchain _swapchain;
Shader2D<> _blit;
Shader2D<int, bool, float> _blit;
Shader2D<Image<float>> _clear;
Clock _clock;
mutable double _last_frame_time;
Stream *_stream{};
ImTextureID _background{};
mutable double _last_frame_time{};
mutable int _tone_mapping{};
mutable float _exposure{};
mutable int _background_fit{};

private:
[[nodiscard]] auto _tone_mapping_uncharted2(Expr<float3> color) noexcept {
[[nodiscard]] static auto _tone_mapping_uncharted2(Expr<float3> color) noexcept {
static constexpr auto a = 0.15f;
static constexpr auto b = 0.50f;
static constexpr auto c = 0.10f;
Expand All @@ -117,25 +124,27 @@ class DisplayInstance final : public Film::Instance {
};
return op(1.6f * color) / op(white);
}
[[nodiscard]] auto _tone_mapping_aces(Expr<float3> color) noexcept {
[[nodiscard]] static auto _tone_mapping_aces(Expr<float3> color) noexcept {
constexpr auto a = 2.51f;
constexpr auto b = 0.03f;
constexpr auto c = 2.43f;
constexpr auto d = 0.59f;
constexpr auto e = 0.14f;
return (color * (a * color + b)) / (color * (c * color + d) + e);
}
[[nodiscard]] auto _linear_to_srgb(Expr<float3> color) noexcept {
[[nodiscard]] static auto _linear_to_srgb(Expr<float3> color) noexcept {
return ite(color <= .0031308f,
color * 12.92f,
1.055f * pow(color, 1.f / 2.4f) - .055f);
}

public:
DisplayInstance(const Pipeline &pipeline, const Film *film,
DisplayInstance(const Pipeline &pipeline, const Display *film,
luisa::unique_ptr<Film::Instance> base) noexcept
: Film::Instance{pipeline, film},
_base{std::move(base)} {}
_base{std::move(base)},
_tone_mapping{luisa::to_underlying(film->tone_mapping())},
_exposure{film->exposure()} {}

[[nodiscard]] Film::Accumulation read(Expr<uint2> pixel) const noexcept override {
return _base->read(pixel);
Expand All @@ -146,25 +155,37 @@ class DisplayInstance final : public Film::Instance {
auto &&device = pipeline().device();
auto size = node()->resolution();
if (!_window) {
_window = luisa::make_unique<Window>("Display", size);
auto d = node<Display>();
_swapchain = device.create_swapchain(
_window->native_handle(), *command_buffer.stream(),
size, d->hdr(), d->vsync(), d->back_buffers());
_framebuffer = device.create_image<float>(
_swapchain.backend_storage(), size);
_blit = device.compile<2>([&] {
_stream = command_buffer.stream();
auto window_size = size;
while (std::max(window_size.x, window_size.y) > 2048u) {
window_size /= 2u;
}
_window = luisa::make_unique<ImGuiWindow>(
device, *command_buffer.stream(), "Display",
ImGuiWindow::Config{
.size = window_size,
.vsync = d->vsync(),
.hdr = d->hdr(),
.back_buffers = d->back_buffers()});
_framebuffer = device.create_image<float>(_window->swapchain().backend_storage(), size);
_background = reinterpret_cast<ImTextureID>(_window->register_texture(_framebuffer, TextureSampler::linear_point_zero()));
_blit = device.compile<2>([base = _base.get(), &framebuffer = _framebuffer](Int tonemapping, Bool ldr, Float scale) noexcept {
auto p = dispatch_id().xy();
auto color = _base->read(p).average * std::exp2(d->exposure());
switch (d->tone_mapping()) {
case Display::ToneMapping::NONE: break;
case Display::ToneMapping::UNCHARTED2: color = _tone_mapping_uncharted2(color); break;
case Display::ToneMapping::ACES: color = _tone_mapping_aces(color); break;
}
if (_framebuffer.storage() == PixelStorage::BYTE4) {// LDR
auto color = base->read(p).average * scale;
$switch (tonemapping) {
$case (static_cast<int>(Display::ToneMapping::NONE)) {};
$case (static_cast<int>(Display::ToneMapping::UNCHARTED2)) { color = _tone_mapping_uncharted2(color); };
$case (static_cast<int>(Display::ToneMapping::ACES)) { color = _tone_mapping_aces(color); };
$default { unreachable(); };
};
$if (ldr) {// LDR
color = _linear_to_srgb(color);
}
_framebuffer->write(p, make_float4(color, 1.f));
};
framebuffer->write(p, make_float4(color, 1.f));
});
_clear = device.compile<2>([](ImageFloat image) noexcept {
image.write(dispatch_id().xy(), make_float4(0.f));
});
}
_last_frame_time = _clock.toc();
Expand All @@ -179,27 +200,75 @@ class DisplayInstance final : public Film::Instance {
}

void release() noexcept override {
while (_window && !_window->should_close()) {
_window->poll_events();
}
_window = nullptr;
_framebuffer = {};
_swapchain = {};
_window.reset();
_base->release();
}

private:
[[nodiscard]] float2 _compute_background_size(const ImGuiViewport *viewport, int fit) const noexcept {
auto frame_size = make_float2(_framebuffer.size());
auto viewport_size = make_float2(viewport->Size.x, viewport->Size.y);
switch (fit) {
case 0: {// aspect fill
auto aspect = frame_size.x / frame_size.y;
auto viewport_aspect = viewport_size.x / viewport_size.y;
auto ratio = aspect < viewport_aspect ? viewport_size.x / frame_size.x : viewport_size.y / frame_size.y;
return frame_size * ratio;
}
case 1: {// aspect fit
auto aspect = frame_size.x / frame_size.y;
auto viewport_aspect = viewport_size.x / viewport_size.y;
auto ratio = aspect > viewport_aspect ? viewport_size.x / frame_size.x : viewport_size.y / frame_size.y;
return frame_size * ratio;
}
case 2: {// stretch
return viewport_size;
}
default: break;
}
return viewport_size;
}

void _display() const noexcept {
auto scale = luisa::exp2(_exposure);
auto is_ldr = _window->framebuffer().storage() != PixelStorage::FLOAT4;
auto size = _framebuffer.size();
*_stream << _blit(_tone_mapping, is_ldr, scale).dispatch(size);
auto viewport = ImGui::GetMainViewport();
auto bg_size = _compute_background_size(viewport, _background_fit);
auto p_min = make_float2(viewport->Pos.x, viewport->Pos.y) +
.5f * (make_float2(viewport->Size.x, viewport->Size.y) - bg_size);
ImGui::GetBackgroundDrawList()->AddImage(_background,
ImVec2{p_min.x, p_min.y},
ImVec2{p_min.x + bg_size.x, p_min.y + bg_size.y});
ImGui::Begin("Console", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
{
ImGui::Text("Display FPS: %.2f", ImGui::GetIO().Framerate);
ImGui::SliderFloat("Exposure", &_exposure, -10.f, 10.f);
constexpr const char *const tone_mapping_names[] = {"None", "Uncharted2", "ACES"};
ImGui::Combo("Tone Mapping", &_tone_mapping, tone_mapping_names, std::size(tone_mapping_names));
constexpr const char *const fit_names[] = {"Fill", "Fit", "Stretch"};
ImGui::Combo("Background Fit", &_background_fit, fit_names, std::size(fit_names));
}
ImGui::End();
}

bool show(CommandBuffer &command_buffer) const noexcept override {
LUISA_ASSERT(command_buffer.stream() == _stream, "Command buffer stream mismatch.");
auto interval = 1. / node<Display>()->target_fps();
if (auto current_time = _clock.toc();
current_time - _last_frame_time >= interval) {
_last_frame_time = current_time;
_window->poll_events();
if (_window->should_close()) {
command_buffer << synchronize();
exit(0);// FIXME: exit gracefully
}
command_buffer << _blit().dispatch(node()->resolution())
<< _swapchain.present(_framebuffer);
command_buffer << commit();
_window->prepare_frame();
*_stream << _clear(_window->framebuffer()).dispatch(_window->framebuffer().size());
_display();
_window->render_frame();
return true;
}
return false;
Expand Down
22 changes: 11 additions & 11 deletions src/integrators/mega_replay_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("Li_1spp forward: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("Li_1spp forward: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif

Expand All @@ -236,7 +236,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("miss and break: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("miss and break: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
}
Expand All @@ -251,7 +251,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("hit light: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("hit light: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
};
Expand Down Expand Up @@ -314,7 +314,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("direct lighted: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("direct lighted: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
};
Expand Down Expand Up @@ -349,7 +349,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("done: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("done: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
};
Expand Down Expand Up @@ -407,7 +407,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("Li_1spp backward start: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("Li_1spp backward start: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif

Expand All @@ -426,7 +426,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("miss and break: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("miss and break: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
}
Expand All @@ -445,7 +445,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("after -hit: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("after -hit: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
};
Expand Down Expand Up @@ -510,9 +510,9 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(
#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
auto Li_variation = weight * eval.f * light_sample.eval.L;
pipeline().printer().info("direct lighting Li_variation = ({}, {}, {})",
pipeline().device_log("direct lighting Li_variation = ({}, {}, {})",
Li_variation[0u], Li_variation[1u], Li_variation[2u]);
pipeline().printer().info("after -direct: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("after -direct: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
closure->backward(wo, wi, d_loss * weight * light_sample.eval.L);
Expand Down Expand Up @@ -559,7 +559,7 @@ void MegakernelReplayDiffInstance::_render_one_camera_backward(

#ifdef LUISA_RENDER_PATH_REPLAY_DEBUG
$if(all(pixel_id == pixel_checked)) {
pipeline().printer().info("should be 0: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
pipeline().device_log("should be 0: Li = ({}, {}, {})", Li[0u], Li[1u], Li[2u]);
};
#endif
};
Expand Down
Loading

0 comments on commit 7eb5927

Please sign in to comment.