From b09f265880a3a932441091b0f3822b06c112c595 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 30 Sep 2021 14:50:19 +0300 Subject: [PATCH] Refactor uploading texture data from a buffer This allows getting rid of Buffer::get_id() and using a friend declaration instead. --- source/core/buffer.h | 4 +--- source/core/texture2d.cpp | 19 ++++++++++++------- source/core/texture2d.h | 10 ++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/source/core/buffer.h b/source/core/buffer.h index db0ff025..0fb2cde1 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -26,6 +26,7 @@ UniformBlock classes contain built-in support for buffers. class Buffer { friend class PipelineState; + friend class Texture2D; friend class VertexSetup; private: @@ -38,9 +39,6 @@ public: Buffer(); ~Buffer(); - /** Returns the OpenGL ID of the buffer. For internal use only. */ - unsigned get_id() const { return id; } - /** Defines the storage size of the buffer. Must be called before data can be uploaded. Storage cannot be changed once set. */ void storage(unsigned); diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index 703b719a..6edbba0d 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -113,15 +113,17 @@ void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht } } -void Texture2D::image(const Graphics::Image &img, unsigned lv) +void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const Buffer &buffer, unsigned offset) { - image(img, lv, false); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer.id); + sub_image(level, x, y, wd, ht, reinterpret_cast(offset)); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } -void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer) +void Texture2D::image(const Graphics::Image &img, unsigned lv) { storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv); - image(0, from_buffer ? 0 : img.get_pixels()); + image(0, img.get_pixels()); } unsigned Texture2D::get_n_levels() const @@ -248,9 +250,12 @@ bool Texture2D::AsyncLoader::process() if(!texture.id) texture.generate_id(); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id()); - texture.image(image, 0, true); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + + unsigned w = image.get_width(); + unsigned h = image.get_height(); + texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h); + texture.sub_image(0, 0, 0, w, h, pixel_buffer, 0); + if(texture.auto_gen_mipmap) texture.generate_mipmap(); } diff --git a/source/core/texture2d.h b/source/core/texture2d.h index eba553b0..58552f11 100644 --- a/source/core/texture2d.h +++ b/source/core/texture2d.h @@ -9,6 +9,8 @@ namespace Msp { namespace GL { +class Buffer; + /** Two-dimensional texture. Consists of an array of texels in the shape of a rectangle. Texture coordinate have a range of [0, 1]. Coordinates outside of @@ -58,6 +60,10 @@ public: and the update region must be fully inside the texture. */ void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data); +private: + void sub_image(unsigned, int, int, unsigned, unsigned, const Buffer &, unsigned); + +public: /** Updates the contents of the entire texture from an image. If storage has not been defined, it will be set to match the image. Otherwise the image must match the defined storage. */ @@ -65,10 +71,6 @@ public: using Texture::image; -private: - void image(const Graphics::Image &, unsigned, bool); - -public: unsigned get_width() const { return width; } unsigned get_height() const { return height; } -- 2.43.0