diff options
| author | hachem <im@hachem.wtf> | 2025-08-22 00:22:12 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-22 00:22:12 +0200 |
| commit | 0d4f5f665f2e4b735fb4f978acc6c8d4a16fcb5d (patch) | |
| tree | dad79333f7026e19732fae0e35ca9dff8ea7ade4 /src/platform/opengl | |
| parent | d8ba57f6d45b9467520377ffcc1f453154d77430 (diff) | |
feat: frame exporting
Diffstat (limited to 'src/platform/opengl')
| -rw-r--r-- | src/platform/opengl/opengl_framebuffer.cpp | 237 | ||||
| -rw-r--r-- | src/platform/opengl/opengl_framebuffer.h | 35 | ||||
| -rw-r--r-- | src/platform/opengl/opengl_renderer_api.cpp | 6 | ||||
| -rw-r--r-- | src/platform/opengl/opengl_renderer_api.h | 4 |
4 files changed, 282 insertions, 0 deletions
diff --git a/src/platform/opengl/opengl_framebuffer.cpp b/src/platform/opengl/opengl_framebuffer.cpp new file mode 100644 index 0000000..c5b8fe2 --- /dev/null +++ b/src/platform/opengl/opengl_framebuffer.cpp @@ -0,0 +1,237 @@ +#include "opengl_framebuffer.h" +#include "core/log.h" + +#include <glad/glad.h> + +namespace Donut +{ + namespace Utils + { + static GLenum texture_target(bool multisampled) + { + return multisampled ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; + } + + static void bind_texture(bool multisampled, uint32_t id) + { + glBindTexture(texture_target(multisampled), id); + } + + static void create_textures(bool multisampled, uint32_t* out_id, uint32_t count) + { + glCreateTextures(texture_target(multisampled), count, out_id); + } + + static void attach_color_texture(uint32_t id, int samples, GLenum internal_format, GLenum format, uint32_t width, uint32_t height, int index) + { + bool multisampled = samples > 1; + if (multisampled) + { + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internal_format, width, height, GL_FALSE); + } + else + { + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, texture_target(multisampled), id, 0); + } + + static void attach_depth_texture(uint32_t id, int samples, GLenum format, GLenum attachment_type, uint32_t width, uint32_t height) + { + bool multisampled = samples > 1; + if (multisampled) + { + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, format, width, height, GL_FALSE); + } + else + { + glTexStorage2D(GL_TEXTURE_2D, 1, format, width, height); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + + glFramebufferTexture2D(GL_FRAMEBUFFER, attachment_type, texture_target(multisampled), id, 0); + } + + static bool is_depth_format(FramebufferTextureFormat format) + { + switch (format) + { + case FramebufferTextureFormat::DEPTH24STENCIL8: return true; + } + return false; + } + + static GLenum donut_fb_texture_format_to_gl(FramebufferTextureFormat format) + { + switch (format) + { + case FramebufferTextureFormat::RGBA8: return GL_RGBA8; + case FramebufferTextureFormat::RED_INTEGER: return GL_RED_INTEGER; + } + + return 0; + } + } + + OpenGLFramebuffer::OpenGLFramebuffer(const FramebufferSpecification& spec) + : m_specification(spec) + { + for (auto spec : m_specification.attachments.attachments) + { + if (!Utils::is_depth_format(spec.texture_format)) + m_color_attachment_specifications.emplace_back(spec); + else + m_depth_attachment_specification = spec; + } + + invalidate(); + } + + OpenGLFramebuffer::~OpenGLFramebuffer() + { + glDeleteFramebuffers(1, &m_renderer_id); + glDeleteTextures(static_cast<GLsizei>(m_color_attachments.size()), m_color_attachments.data()); + glDeleteTextures(1, &m_depth_attachment); + } + + void OpenGLFramebuffer::invalidate() + { + if (m_renderer_id) + { + glDeleteFramebuffers(1, &m_renderer_id); + glDeleteTextures(static_cast<GLsizei>(m_color_attachments.size()), m_color_attachments.data()); + glDeleteTextures(1, &m_depth_attachment); + + m_color_attachments.clear(); + m_depth_attachment = 0; + } + + glCreateFramebuffers(1, &m_renderer_id); + glBindFramebuffer(GL_FRAMEBUFFER, m_renderer_id); + + bool multisample = m_specification.Samples > 1; + + if (m_color_attachment_specifications.size()) + { + m_color_attachments.resize(m_color_attachment_specifications.size()); + Utils::create_textures(multisample, m_color_attachments.data(), static_cast<uint32_t>(m_color_attachments.size())); + + for (size_t i = 0; i < m_color_attachments.size(); i++) + { + Utils::bind_texture(multisample, m_color_attachments[i]); + switch (m_color_attachment_specifications[i].texture_format) + { + case FramebufferTextureFormat::RGBA8: + Utils::attach_color_texture(m_color_attachments[i], m_specification.Samples, GL_RGBA8, GL_RGBA, m_specification.Width, m_specification.Height, static_cast<int>(i)); + break; + case FramebufferTextureFormat::RED_INTEGER: + Utils::attach_color_texture(m_color_attachments[i], m_specification.Samples, GL_R32I, GL_RED_INTEGER, m_specification.Width, m_specification.Height, static_cast<int>(i)); + break; + } + } + } + + if (m_depth_attachment_specification.texture_format != FramebufferTextureFormat::None) + { + Utils::create_textures(multisample, &m_depth_attachment, 1); + Utils::bind_texture(multisample, m_depth_attachment); + switch (m_depth_attachment_specification.texture_format) + { + case FramebufferTextureFormat::DEPTH24STENCIL8: + Utils::attach_depth_texture(m_depth_attachment, m_specification.Samples, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL_ATTACHMENT, m_specification.Width, m_specification.Height); + break; + } + } + + if (m_color_attachments.size() > 1) + { + GLenum buffers[4] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(static_cast<GLsizei>(m_color_attachments.size()), buffers); + } + else if (m_color_attachments.empty()) + glDrawBuffer(GL_NONE); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + { + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + switch (status) + { + case GL_FRAMEBUFFER_UNDEFINED: + DONUT_ERROR("Framebuffer is undefined"); + break; + case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: + DONUT_ERROR("Framebuffer has incomplete attachment"); + break; + case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: + DONUT_ERROR("Framebuffer is missing attachment"); + break; + case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: + DONUT_ERROR("Framebuffer has incomplete draw buffer"); + break; + case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: + DONUT_ERROR("Framebuffer has incomplete read buffer"); + break; + case GL_FRAMEBUFFER_UNSUPPORTED: + DONUT_ERROR("Framebuffer format is unsupported"); + break; + case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: + DONUT_ERROR("Framebuffer has incomplete multisample"); + break; + case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: + DONUT_ERROR("Framebuffer has incomplete layer targets"); + break; + default: + DONUT_ERROR("Framebuffer is incomplete (unknown error: {})", status); + break; + } + } + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + + void OpenGLFramebuffer::bind() + { + glBindFramebuffer(GL_FRAMEBUFFER, m_renderer_id); + glViewport(0, 0, m_specification.Width, m_specification.Height); + } + + void OpenGLFramebuffer::unbind() + { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + + void OpenGLFramebuffer::resize(uint32_t width, uint32_t height) + { + m_specification.Width = width; + m_specification.Height = height; + + invalidate(); + } + + int OpenGLFramebuffer::read_pixel(uint32_t attachment_index, int x, int y) + { + glReadBuffer(GL_COLOR_ATTACHMENT0 + attachment_index); + int pixel_data; + glReadPixels(x, y, 1, 1, GL_RED_INTEGER, GL_INT, &pixel_data); + return pixel_data; + } + + void OpenGLFramebuffer::clear_attachment(uint32_t attachment_index, int value) + { + auto& spec = m_color_attachment_specifications[attachment_index]; + glClearTexImage(m_color_attachments[attachment_index], 0, + Utils::donut_fb_texture_format_to_gl(spec.texture_format), GL_INT, &value); + } +}; diff --git a/src/platform/opengl/opengl_framebuffer.h b/src/platform/opengl/opengl_framebuffer.h new file mode 100644 index 0000000..7b07ddd --- /dev/null +++ b/src/platform/opengl/opengl_framebuffer.h @@ -0,0 +1,35 @@ +#pragma once + +#include "rendering/framebuffer.h" + +namespace Donut +{ + class OpenGLFramebuffer : public Framebuffer + { + public: + OpenGLFramebuffer(const FramebufferSpecification& spec); + virtual ~OpenGLFramebuffer(); + + void invalidate(); + + virtual void bind() override; + virtual void unbind() override; + + virtual void resize(uint32_t width, uint32_t height) override; + virtual int read_pixel(uint32_t attachment_index, int x, int y) override; + + virtual void clear_attachment(uint32_t attachment_index, int value) override; + virtual uint32_t get_color_attachment_renderer_id(uint32_t index = 0) const override { return m_color_attachments[index]; } + + virtual const FramebufferSpecification& get_specification() const override { return m_specification; } + private: + uint32_t m_renderer_id = 0; + FramebufferSpecification m_specification; + + std::vector<FramebufferTextureSpecification> m_color_attachment_specifications; + FramebufferTextureSpecification m_depth_attachment_specification = FramebufferTextureFormat::None; + + std::vector<uint32_t> m_color_attachments; + uint32_t m_depth_attachment = 0; + }; +}; diff --git a/src/platform/opengl/opengl_renderer_api.cpp b/src/platform/opengl/opengl_renderer_api.cpp index bfb79ac..0012911 100644 --- a/src/platform/opengl/opengl_renderer_api.cpp +++ b/src/platform/opengl/opengl_renderer_api.cpp @@ -106,4 +106,10 @@ namespace Donut glBindImageTexture(slot, texture_id, 0, GL_FALSE, 0, read_only ? GL_READ_ONLY : GL_WRITE_ONLY, GL_RGBA8); } + + void OpenGLRendererAPI::read_pixels(uint32_t x, uint32_t y, uint32_t width, uint32_t height, + uint32_t format, uint32_t type, void* pixels) + { + glReadPixels(x, y, width, height, format, type, pixels); + } }; diff --git a/src/platform/opengl/opengl_renderer_api.h b/src/platform/opengl/opengl_renderer_api.h index 45e6a7b..51985e0 100644 --- a/src/platform/opengl/opengl_renderer_api.h +++ b/src/platform/opengl/opengl_renderer_api.h @@ -36,5 +36,9 @@ namespace Donut virtual void bind_image_texture(uint32_t texture_id, uint32_t slot = 0, bool read_only = false) override; + virtual void read_pixels(uint32_t x, uint32_t y, + uint32_t width, uint32_t height, + uint32_t format, uint32_t type, + void* pixels) override; }; }; |
