From 0d4f5f665f2e4b735fb4f978acc6c8d4a16fcb5d Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 22 Aug 2025 00:22:12 +0200 Subject: feat: frame exporting --- src/platform/opengl/opengl_framebuffer.cpp | 237 +++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 src/platform/opengl/opengl_framebuffer.cpp (limited to 'src/platform/opengl/opengl_framebuffer.cpp') 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 + +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(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(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(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(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(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(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); + } +}; -- cgit v1.3