Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback loop #54

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Cool/Exporter/ExporterU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace Cool::ExporterU {

void export_image(img::Size size, Time time, Time delta_time, Polaroid polaroid, std::filesystem::path const& file_path)
void export_image(img::Size size, Time time, Time delta_time, Polaroid const& polaroid, std::filesystem::path const& file_path)
{
polaroid.render(time, delta_time, size);
ImageU::save_png(file_path, polaroid.render_target.download_pixels());
polaroid.render(size, time, delta_time);
ImageU::save_png(file_path, polaroid.texture().download_pixels());
}

} // namespace Cool::ExporterU
2 changes: 1 addition & 1 deletion src/Cool/Exporter/ExporterU.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Cool::ExporterU {
*
* @param file_path The name of the file that you want to create
*/
void export_image(img::Size size, Time time, Time delta_time, Polaroid polaroid, std::filesystem::path const& file_path);
void export_image(img::Size size, Time time, Time delta_time, Polaroid const& polaroid, std::filesystem::path const& file_path);

} // namespace Cool::ExporterU
8 changes: 4 additions & 4 deletions src/Cool/Exporter/VideoExportProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ VideoExportProcess::VideoExportProcess(VideoExportParams const& params, TimeSpee
_thread_pool.start();
}

bool VideoExportProcess::update(Polaroid polaroid)
bool VideoExportProcess::update(Polaroid const& polaroid)
{
if (_should_stop_asap || _nb_frames_which_finished_exporting.load() == _total_nb_of_frames_in_sequence)
return true; // The export is finished
Expand Down Expand Up @@ -100,13 +100,13 @@ void VideoExportProcess::imgui(std::function<void()> const& extra_widgets)
extra_widgets();
}

void VideoExportProcess::export_frame(Polaroid polaroid, std::filesystem::path const& file_path)
void VideoExportProcess::export_frame(Polaroid const& polaroid, std::filesystem::path const& file_path)
{
polaroid.render(_clock.time(), _clock.delta_time(), _size);
polaroid.render(_size, _clock.time(), _clock.delta_time());

_thread_pool.push_job(ImageExportJob{
file_path,
polaroid.render_target.download_pixels(),
polaroid.texture().download_pixels(),
_average_export_time,
_average_export_time_mutex,
_nb_frames_which_finished_exporting
Expand Down
4 changes: 2 additions & 2 deletions src/Cool/Exporter/VideoExportProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace Cool {
class VideoExportProcess {
public:
VideoExportProcess(VideoExportParams const& params, TimeSpeed time_speed, std::filesystem::path const& folder_path, img::Size size);
auto update(Polaroid polaroid) -> bool;
auto update(Polaroid const& polaroid) -> bool;
void imgui(std::function<void()> const& extra_widgets);

auto clock() const -> Clock const& { return _clock; }

private:
auto estimated_remaining_time() -> Time;
void update_time_estimate();
void export_frame(Polaroid polaroid, std::filesystem::path const& file_path);
void export_frame(Polaroid const& polaroid, std::filesystem::path const& file_path);

private:
std::filesystem::path _folder_path;
Expand Down
16 changes: 3 additions & 13 deletions src/Cool/Exporter/internal/Polaroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@
namespace Cool {

struct Polaroid {
// The target that we will render to
RenderTarget& render_target;
// The texture that we will render to
std::function<TextureRef()> texture;
// The function that renders the desired image
std::function<void(RenderTarget&, Time time, Time delta_time)> render_fn;

void render(Time time, Time delta_time)
{
render_fn(render_target, time, delta_time);
}
void render(Time time, Time delta_time, img::Size size)
{
render_target.set_size(size);
render(time, delta_time);
}
std::function<void(img::Size size, Time time, Time delta_time)> render;
};

} // namespace Cool
1 change: 1 addition & 0 deletions src/Cool/Gpu/OpenGL/Texture.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if defined(COOL_OPENGL)
#pragma once
#include <glpp/glpp.hpp>
#include "TextureRef.hpp"
#include "glpp-extended/src/Texture2D.h"
#include "img/src/Image.h"
#include "img/src/Size.h"
Expand Down
29 changes: 29 additions & 0 deletions src/Cool/Gpu/OpenGL/TextureRef.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "TextureRef.hpp"
#include "Cool/Gpu/OpenGL/copy_tex_pipeline.hpp"
#include "Cool/Gpu/RenderTarget.h"
#include "glpp/glpp.hpp"

namespace Cool {

auto TextureRef::download_pixels() const -> img::Image
{
static auto rt = RenderTarget{};
rt.set_size(size);
std::unique_ptr<uint8_t[]> data{new uint8_t[4 * width() * height()]};
rt.render([&]() {
// TODO(WebGPU) We shouldn't need to run a shader to get the texture on a framebuffer and then download its pixels
Cool::copy_tex_pipeline().shader()->bind();
Cool::copy_tex_pipeline().shader()->set_uniform_texture("tex_to_copy", id);
glDisable(GL_BLEND);
Cool::copy_tex_pipeline().draw();
glEnable(GL_BLEND);
glReadPixels(0, 0, static_cast<GLsizei>(width()), static_cast<GLsizei>(height()), GL_RGBA, GL_UNSIGNED_BYTE, data.get());
});
return img::Image{
img::Size{width(), height()},
4,
data.release()
};
}

} // namespace Cool
17 changes: 17 additions & 0 deletions src/Cool/Gpu/OpenGL/TextureRef.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace Cool {

struct TextureRef {
GLuint id{};
img::Size size{};

auto width() const { return size.width(); }
auto height() const { return size.height(); }

[[nodiscard]] auto imgui_texture_id() const -> ImTextureID { return reinterpret_cast<ImTextureID>(static_cast<uint64_t>(id)); }

auto download_pixels() const -> img::Image;
};

} // namespace Cool
38 changes: 38 additions & 0 deletions src/Cool/Gpu/OpenGL/copy_tex_pipeline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "copy_tex_pipeline.hpp"

namespace Cool {

static auto make_copy_tex_pipeline() -> Cool::OpenGL::FullscreenPipeline
{
auto instance = Cool::OpenGL::FullscreenPipeline{};
auto const err = instance.compile(R"GLSL(
#version 410
#include "_COOL_RES_/shaders/shader-utils.glsl"
out vec4 out_Color;
uniform sampler2D tex_to_copy;
void main()
{
out_Color = texture(tex_to_copy, _uv);
}
)GLSL");
err.send_error_if_any(
[&](std::string const& message) {
return Cool::Message{
.category = "Internal",
.message = "Failed to create shader to copy texture:\n" + message,
.severity = Cool::MessageSeverity::Error,
};
},
Cool::Log::ToUser::console()
);

return instance;
}

auto copy_tex_pipeline() -> Cool::OpenGL::FullscreenPipeline&
{
static auto instance = make_copy_tex_pipeline();
return instance;
}

} // namespace Cool
8 changes: 8 additions & 0 deletions src/Cool/Gpu/OpenGL/copy_tex_pipeline.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include "FullscreenPipeline.h"

namespace Cool {

auto copy_tex_pipeline() -> Cool::OpenGL::FullscreenPipeline&;

}
3 changes: 2 additions & 1 deletion src/Cool/Gpu/internal/RenderTarget_Base.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <img/img.hpp>
#include "../RenderTargetInfo.h"
#include "Cool/Gpu/OpenGL/TextureRef.hpp"

namespace Cool {

Expand All @@ -11,6 +11,7 @@ class RenderTarget_Base {
void render(typename RenderTarget_Impl::RenderFuncType render_fn);
img::Image download_pixels() const { return _impl.download_pixels(); }
ImTextureID imgui_texture_id() const { return _impl.imgui_texture_id(); }
auto texture_ref() const -> TextureRef { return _impl.texture_ref(); }
RenderTargetInfo info() const { return _impl.info(); }
img::Size current_size() const { return _impl.size(); }
img::Size desired_size() const { return _desired_size; }
Expand Down
17 changes: 10 additions & 7 deletions src/Cool/Gpu/internal/RenderTarget_ImplOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,37 @@ RenderTarget_ImplOpenGL::RenderTarget_ImplOpenGL(img::Size size)

void RenderTarget_ImplOpenGL::render(RenderFuncType render_fn)
{
_texture.bind();
_texture_fb.bind();
render_fn();
_texture.unbind();
_texture_fb.unbind();
}

RenderTargetInfo RenderTarget_ImplOpenGL::info() const
{
return RenderTargetInfo{
.viewport = {
.size = size(),
.bottom_left_corner = {0, 0}}};
.bottom_left_corner = {0, 0}
}
};
}

void RenderTarget_ImplOpenGL::resize(img::Size size)
{
_texture.setSize(size);
_texture_fb.setSize(size);
}

img::Image RenderTarget_ImplOpenGL::download_pixels() const
{
_texture.bind();
_texture_fb.bind();
std::unique_ptr<uint8_t[]> data{new uint8_t[4 * width() * height()]};
glReadPixels(0, 0, static_cast<GLsizei>(width()), static_cast<GLsizei>(height()), GL_RGBA, GL_UNSIGNED_BYTE, data.get());
_texture.unbind();
_texture_fb.unbind();
return img::Image{
img::Size{width(), height()},
4,
data.release()};
data.release()
};
}

} // namespace Cool
Expand Down
15 changes: 8 additions & 7 deletions src/Cool/Gpu/internal/RenderTarget_ImplOpenGL.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#if defined(COOL_OPENGL)

#include <img/img.hpp>
#include "../OpenGL/Texture.h"
#include "../OpenGL/TextureFB.h"
#include "../RenderTargetInfo.h"

Expand All @@ -14,19 +14,20 @@ class RenderTarget_ImplOpenGL {
void render(RenderFuncType render_fn);

RenderTargetInfo info() const;
img::Size::DataType width() const { return _texture.width(); }
img::Size::DataType height() const { return _texture.height(); }
img::Size size() const { return _texture.size(); }
img::Size::DataType width() const { return _texture_fb.width(); }
img::Size::DataType height() const { return _texture_fb.height(); }
img::Size size() const { return _texture_fb.size(); }
img::Image download_pixels() const;
GLuint texture_id() const { return _texture.textureID(); }
ImTextureID imgui_texture_id() const { return reinterpret_cast<ImTextureID>(static_cast<uint64_t>(_texture.textureID())); } // Double-cast to fix a warning : first we convert to the correct size (uint32_t -> uint64_t) then from integral type to pointer type (uint64_t -> ImTextureID)
GLuint texture_id() const { return _texture_fb.textureID(); }
auto texture_ref() const -> TextureRef { return TextureRef{texture_id(), size()}; }
ImTextureID imgui_texture_id() const { return reinterpret_cast<ImTextureID>(static_cast<uint64_t>(_texture_fb.textureID())); } // Double-cast to fix a warning : first we convert to the correct size (uint32_t -> uint64_t) then from integral type to pointer type (uint64_t -> ImTextureID)

void imgui_window();

void resize(img::Size size);

private:
TextureFB _texture;
TextureFB _texture_fb;
};

} // namespace Cool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#pragma once
#include "RenderView.h"
#include "TextureView.hpp"

namespace Cool {

class ForwardingOrRenderView : public RenderView {
class ForwardingOrTextureView : public TextureView {
public:
ForwardingOrRenderView(View const& forwarded_view, ViewCreationParams const& p)
: RenderView{p}
ForwardingOrTextureView(View const& forwarded_view, ViewCreationParams const& p)
: TextureView{p}
, _forwarded_view{forwarded_view}
{}

private:
auto get_image_texture_id() const -> ImTextureID override { return _forwarded_view.is_open()
? _forwarded_view.get_image_texture_id()
: RenderView::get_image_texture_id(); }
: TextureView::get_image_texture_id(); }
auto get_image_size() const -> img::Size override { return _forwarded_view.is_open()
? _forwarded_view.get_image_size()
: RenderView::get_image_size(); }
: TextureView::get_image_size(); }

private:
View const& _forwarded_view; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
Expand Down
21 changes: 0 additions & 21 deletions src/Cool/View/RenderView.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions src/Cool/View/RenderView.h

This file was deleted.

21 changes: 21 additions & 0 deletions src/Cool/View/TextureView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "TextureView.hpp"

namespace Cool {

auto TextureView::desired_image_size(ImageSizeConstraint const& constraint) const -> img::Size
{
if (!window_size().has_value())
return {};
return constraint.applied_to(*window_size());
}

auto TextureView::get_image_texture_id() const -> ImTextureID
{
return _texture.imgui_texture_id();
}
auto TextureView::get_image_size() const -> img::Size
{
return _texture.size;
}

} // namespace Cool
Loading
Loading