]> git.tdb.fi Git - libs/gl.git/commitdiff
Add an asynchronous version of Texture2D::sub_image
authorMikko Rasa <tdb@tdb.fi>
Tue, 28 Dec 2021 10:03:58 +0000 (12:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 28 Dec 2021 14:07:14 +0000 (16:07 +0200)
source/backends/opengl/texture2d_backend.cpp
source/backends/opengl/texture2d_backend.h
source/backends/vulkan/texture2d_backend.cpp
source/backends/vulkan/texture2d_backend.h
source/core/texture2d.cpp
source/core/texture2d.h

index ed10f6c192739196cd56f9baa5bd2d54479aaebc..ab0c6d8cfb48ccf484da7d596fe8adf84ec07cf1 100644 (file)
@@ -119,6 +119,47 @@ void OpenGLTexture2D::unload()
 }
 
 
+OpenGLTexture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other):
+       pixel_buffer(other.pixel_buffer)
+{
+       other.pixel_buffer = 0;
+}
+
+OpenGLTexture2D::AsyncTransfer &OpenGLTexture2D::AsyncTransfer::operator=(AsyncTransfer &&other)
+{
+       delete pixel_buffer;
+       pixel_buffer = other.pixel_buffer;
+       other.pixel_buffer = 0;
+
+       return *this;
+}
+
+OpenGLTexture2D::AsyncTransfer::~AsyncTransfer()
+{
+       delete pixel_buffer;
+}
+
+void *OpenGLTexture2D::AsyncTransfer::allocate()
+{
+       const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
+
+       pixel_buffer = new Buffer;
+       pixel_buffer->storage(self.data_size, STREAMING);
+       return pixel_buffer->map();
+}
+
+void OpenGLTexture2D::AsyncTransfer::finalize()
+{
+       const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
+
+       pixel_buffer->unmap();
+
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer->id);
+       self.texture->OpenGLTexture2D::sub_image(self.level, self.x, self.y, self.width, self.height, 0);
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+}
+
+
 OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
        texture(t),
        io(i)
index af9f482cbf039c47e6bb17190bfdf7966701032a..5de1079537daf16dd08248ca1d3712844223913d 100644 (file)
@@ -11,6 +11,20 @@ class Buffer;
 class OpenGLTexture2D: public Texture
 {
 protected:
+       class AsyncTransfer: public NonCopyable
+       {
+       protected:
+               Buffer *pixel_buffer = 0;
+
+               AsyncTransfer() = default;
+               AsyncTransfer(AsyncTransfer &&);
+               AsyncTransfer &operator=(AsyncTransfer &&);
+               ~AsyncTransfer();
+
+               void *allocate();
+               void finalize();
+       };
+
        class AsyncLoader;
 
        OpenGLTexture2D();
index 7edee3b2afa7ced6a67bd3e39f8e7dd67b724c64..7c6b55f1ee94dfb0652ef76ea8adb7e75675fbbf 100644 (file)
@@ -24,33 +24,8 @@ void VulkanTexture2D::fill_image_info(void *ii) const
 
 void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
 {
-       const Texture2D &self = *static_cast<const Texture2D *>(this);
-
-       auto level_size = self.get_level_size(level);
-       bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
-
-       TransferQueue &tq = device.get_transfer_queue();
-       size_t data_size = wd*ht*get_pixel_size(storage_fmt);
-       void *staging = tq.prepare_transfer(this, false, data_size,
-               [this, level, discard](){
-                       change_layout(level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
-               },
-               [this, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
-                       const VulkanFunctions &vk = device.get_functions();
-
-                       VkBufferImageCopy region = { };
-                       region.bufferOffset = src_off;
-                       region.imageSubresource.aspectMask = get_vulkan_aspect(get_components(storage_fmt));
-                       region.imageSubresource.mipLevel = level;
-                       region.imageSubresource.baseArrayLayer = 0;
-                       region.imageSubresource.layerCount = 1;
-                       region.imageOffset = { x, y, 0 };
-                       region.imageExtent = { wd, ht, 1 };
-                       vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
-               });
-
-       stage_pixels(staging, data, wd*ht);
-       tq.finalize_transfer(staging);
+       Texture2D::AsyncTransfer transfer(*static_cast<Texture2D *>(this), level, x, y, wd, ht);
+       stage_pixels(transfer.get_address(), data, wd*ht);
 }
 
 void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
@@ -79,5 +54,46 @@ void VulkanTexture2D::unload()
 {
 }
 
+
+void *VulkanTexture2D::AsyncTransfer::allocate()
+{
+       const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
+
+       Texture2D &tex = *self.texture;
+       unsigned level = self.level;
+       int x = self.x;
+       int y = self.y;
+       unsigned wd = self.width;
+       unsigned ht = self.height;
+
+       auto level_size = tex.get_level_size(level);
+       bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
+
+       TransferQueue &tq = tex.device.get_transfer_queue();
+       return tq.prepare_transfer(&tex, false, self.data_size,
+               [&tex, level, discard](){
+                       tex.change_layout(level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
+               },
+               [&tex, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
+                       const VulkanFunctions &vk = tex.device.get_functions();
+
+                       VkBufferImageCopy region = { };
+                       region.bufferOffset = src_off;
+                       region.imageSubresource.aspectMask = get_vulkan_aspect(get_components(tex.storage_fmt));
+                       region.imageSubresource.mipLevel = level;
+                       region.imageSubresource.baseArrayLayer = 0;
+                       region.imageSubresource.layerCount = 1;
+                       region.imageOffset = { x, y, 0 };
+                       region.imageExtent = { wd, ht, 1 };
+                       vk.CmdCopyBufferToImage(cmd_buf, staging_buf, tex.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
+               });
+}
+
+void VulkanTexture2D::AsyncTransfer::finalize()
+{
+       const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
+       self.texture->device.get_transfer_queue().finalize_transfer(self.dest_addr);
+}
+
 } // namespace GL
 } // namespace Msp
index d4b7db1d934cbcdd44a33f504857cf452857715c..7686a9e0e548645c1e9b0bd0fa42ba7c11be4b86 100644 (file)
@@ -9,6 +9,17 @@ namespace GL {
 class VulkanTexture2D: public Texture
 {
 protected:
+       class AsyncTransfer: public NonCopyable
+       {
+       protected:
+               AsyncTransfer() = default;
+               AsyncTransfer(AsyncTransfer &&) { }
+               AsyncTransfer &operator=(AsyncTransfer &&) { return *this; }
+
+               void *allocate();
+               void finalize();
+       };
+
        VulkanTexture2D();
 
        virtual void fill_image_info(void *) const;
index 7ac1c5ec84f599fcef2285b94e8f5531c3631958..74071535e73f41e768f053cd5041479fe80353c3 100644 (file)
@@ -48,6 +48,11 @@ void Texture2D::sub_image(unsigned level, unsigned x, unsigned y, unsigned wd, u
        Texture2DBackend::sub_image(level, x, y, wd, ht, data);
 }
 
+Texture2D::AsyncTransfer Texture2D::sub_image_async(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht)
+{
+       return AsyncTransfer(*this, level, x, y, wd, ht);
+}
+
 void Texture2D::image(const Graphics::Image &img, unsigned lv)
 {
        storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
@@ -96,5 +101,60 @@ void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h,
        obj.storage(fmt, w, h, l);
 }
 
+
+Texture2D::AsyncTransfer::AsyncTransfer(Texture2D &t, unsigned l, unsigned x_, unsigned y_, unsigned wd, unsigned ht):
+       texture(&t),
+       level(l),
+       x(x_),
+       y(y_),
+       width(wd),
+       height(ht),
+       data_size(width*height*get_pixel_size(texture->storage_fmt)),
+       dest_addr(0)
+{
+       dest_addr = allocate();
+}
+
+Texture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other):
+       Texture2DBackend::AsyncTransfer(move(other)),
+       texture(other.texture),
+       level(other.level),
+       x(other.x),
+       y(other.y),
+       width(other.width),
+       height(other.height),
+       data_size(other.data_size),
+       dest_addr(other.dest_addr)
+{
+       other.dest_addr = 0;
+}
+
+Texture2D::AsyncTransfer &Texture2D::AsyncTransfer::operator=(AsyncTransfer &&other)
+{
+       if(dest_addr)
+               finalize();
+
+       Texture2DBackend::AsyncTransfer::operator=(move(other));
+
+       texture = other.texture;
+       level = other.level;
+       x = other.x;
+       y = other.y;
+       width = other.width;
+       height = other.height;
+       data_size = other.data_size;
+       dest_addr = other.dest_addr;
+
+       other.dest_addr = 0;
+
+       return *this;
+}
+
+Texture2D::AsyncTransfer::~AsyncTransfer()
+{
+       if(dest_addr)
+               finalize();
+}
+
 } // namespace GL
 } // namespace Msp
index 05c3283b73bd1aba1dd98f348c0c620753611114..02cdade57e30e1b19cc771b2a0df4030d3b0a3fe 100644 (file)
@@ -28,6 +28,38 @@ public:
                void storage_levels(PixelFormat, unsigned, unsigned, unsigned);
        };
 
+       /**
+       An RAII handle for asynchronously writing texel data into a texture.
+       */
+       class AsyncTransfer: public Texture2DBackend::AsyncTransfer
+       {
+               friend Texture2DBackend;
+               friend class Texture2D;
+               friend class Texture2DBackend::AsyncTransfer;
+
+       private:
+               Texture2D *texture = 0;
+               unsigned level = 0;
+               unsigned x = 0;
+               unsigned y = 0;
+               unsigned width = 0;
+               unsigned height = 0;
+               std::size_t data_size = 0;
+               void *dest_addr = 0;
+
+               AsyncTransfer(Texture2D &, unsigned, unsigned, unsigned, unsigned, unsigned);
+       public:
+               AsyncTransfer() = default;
+               AsyncTransfer(AsyncTransfer &&);
+               AsyncTransfer &operator=(AsyncTransfer &&);
+               ~AsyncTransfer();
+
+       public:
+               /** Returns an address for writing the texel data.  It should not be used
+               beyond the lifetime of the object. */
+               void *get_address() { return dest_addr; }
+       };
+
 private:
        unsigned width = 0;
        unsigned height = 0;
@@ -49,6 +81,8 @@ public:
        the region must be fully inside the selected mipmap level. */
        void sub_image(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht, const void *);
 
+       AsyncTransfer sub_image_async(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht);
+
        virtual void image(const Graphics::Image &, unsigned = 0);
 
        unsigned get_width() const { return width; }