Add multithreaded upload to dynamic texture on Vulkan [Core feature] (#4028)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Adrian Cojocaru <adrian.cojocaru92@yahoo.com>
This commit is contained in:
@@ -35,11 +35,13 @@ class ShaderRegistry;
|
||||
|
||||
class Drawable;
|
||||
class DrawableBuilder;
|
||||
class DynamicTexture;
|
||||
class ShaderProgramBase;
|
||||
class Texture2D;
|
||||
class VertexAttributeArray;
|
||||
|
||||
using DrawablePtr = std::shared_ptr<Drawable>;
|
||||
using DynamicTexturePtr = std::shared_ptr<DynamicTexture>;
|
||||
using ShaderProgramBasePtr = std::shared_ptr<ShaderProgramBase>;
|
||||
using Texture2DPtr = std::shared_ptr<Texture2D>;
|
||||
using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
|
||||
@@ -132,6 +134,9 @@ public:
|
||||
/// Create a texture
|
||||
virtual Texture2DPtr createTexture2D() = 0;
|
||||
|
||||
/// Create a dynamic texture
|
||||
virtual DynamicTexturePtr createDynamicTexture(Size size, TexturePixelType pixelType) = 0;
|
||||
|
||||
/// Create a render target
|
||||
virtual RenderTargetPtr createRenderTarget(const Size size, const TextureChannelDataType type) = 0;
|
||||
|
||||
|
||||
@@ -10,12 +10,11 @@
|
||||
#include <mutex>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
namespace gfx {
|
||||
|
||||
class Context;
|
||||
class Texture2D;
|
||||
using Texture2DPtr = std::shared_ptr<gfx::Texture2D>;
|
||||
using Texture2DPtr = std::shared_ptr<Texture2D>;
|
||||
|
||||
class TextureHandle {
|
||||
public:
|
||||
@@ -46,14 +45,13 @@ private:
|
||||
class DynamicTexture {
|
||||
public:
|
||||
DynamicTexture(Context& context, Size size, TexturePixelType pixelType);
|
||||
~DynamicTexture() = default;
|
||||
virtual ~DynamicTexture() = default;
|
||||
|
||||
const Texture2DPtr& getTexture() const;
|
||||
TexturePixelType getPixelFormat() const;
|
||||
bool isEmpty() const;
|
||||
|
||||
std::optional<TextureHandle> reserveSize(const Size& size, int32_t uniqueId);
|
||||
void uploadImage(const uint8_t* pixelData, TextureHandle& texHandle);
|
||||
|
||||
template <typename Image>
|
||||
std::optional<TextureHandle> addImage(const Image& image, int32_t uniqueId = -1) {
|
||||
@@ -61,22 +59,16 @@ public:
|
||||
}
|
||||
std::optional<TextureHandle> addImage(const uint8_t* pixelData, const Size& imageSize, int32_t uniqueId = -1);
|
||||
|
||||
void uploadDeferredImages();
|
||||
void removeTexture(const TextureHandle& texHandle);
|
||||
virtual void uploadImage(const uint8_t* pixelData, TextureHandle& texHandle);
|
||||
virtual void uploadDeferredImages() {};
|
||||
virtual bool removeTexture(const TextureHandle& texHandle);
|
||||
|
||||
using ImagesToUpload = std::unordered_map<TextureHandle, std::unique_ptr<uint8_t[]>, TextureHandle::Hasher>;
|
||||
|
||||
private:
|
||||
Texture2DPtr texture;
|
||||
protected:
|
||||
mapbox::ShelfPack shelfPack;
|
||||
Texture2DPtr texture;
|
||||
int numTextures = 0;
|
||||
bool deferredCreation = false;
|
||||
ImagesToUpload imagesToUpload;
|
||||
std::mutex mutex;
|
||||
};
|
||||
|
||||
#define MLN_DEFER_UPLOAD_ON_RENDER_THREAD \
|
||||
(MLN_RENDER_BACKEND_OPENGL || MLN_RENDER_BACKEND_VULKAN || MLN_RENDER_BACKEND_WEBGPU)
|
||||
|
||||
} // namespace gfx
|
||||
} // namespace mbgl
|
||||
|
||||
27
include/mbgl/gl/dynamic_texture.hpp
Normal file
27
include/mbgl/gl/dynamic_texture.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/dynamic_texture.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace gl {
|
||||
|
||||
class Context;
|
||||
|
||||
class DynamicTexture : public gfx::DynamicTexture {
|
||||
public:
|
||||
DynamicTexture(Context& context, Size size, gfx::TexturePixelType pixelType);
|
||||
|
||||
void uploadImage(const uint8_t* pixelData, gfx::TextureHandle& texHandle) override;
|
||||
void uploadDeferredImages() override;
|
||||
bool removeTexture(const gfx::TextureHandle& texHandle) override;
|
||||
|
||||
using ImagesToUpload =
|
||||
std::unordered_map<gfx::TextureHandle, std::unique_ptr<uint8_t[]>, gfx::TextureHandle::Hasher>;
|
||||
|
||||
private:
|
||||
bool deferredCreation = false;
|
||||
ImagesToUpload imagesToUpload;
|
||||
};
|
||||
|
||||
} // namespace gl
|
||||
} // namespace mbgl
|
||||
@@ -98,6 +98,8 @@ public:
|
||||
|
||||
gfx::Texture2DPtr createTexture2D() override;
|
||||
|
||||
gfx::DynamicTexturePtr createDynamicTexture(Size size, gfx::TexturePixelType pixelType) override;
|
||||
|
||||
RenderTargetPtr createRenderTarget(const Size size, const gfx::TextureChannelDataType type) override;
|
||||
|
||||
void resetState(gfx::DepthMode depthMode, gfx::ColorMode colorMode) override;
|
||||
|
||||
18
include/mbgl/mtl/dynamic_texture.hpp
Normal file
18
include/mbgl/mtl/dynamic_texture.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/dynamic_texture.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace mtl {
|
||||
|
||||
class Context;
|
||||
|
||||
class DynamicTexture : public gfx::DynamicTexture {
|
||||
public:
|
||||
DynamicTexture(Context& context, Size size, gfx::TexturePixelType pixelType);
|
||||
|
||||
void uploadImage(const uint8_t* pixelData, gfx::TextureHandle& texHandle) override;
|
||||
};
|
||||
|
||||
} // namespace mtl
|
||||
} // namespace mbgl
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <mbgl/gfx/context.hpp>
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/containers.hpp>
|
||||
#include <mbgl/vulkan/dynamic_texture.hpp>
|
||||
#include <mbgl/vulkan/uniform_buffer.hpp>
|
||||
#include <mbgl/vulkan/renderer_backend.hpp>
|
||||
#include <mbgl/vulkan/pipeline.hpp>
|
||||
@@ -35,7 +36,6 @@ struct ClipUBO;
|
||||
namespace vulkan {
|
||||
|
||||
class RenderPass;
|
||||
class RendererBackend;
|
||||
class ShaderProgram;
|
||||
class VertexBufferResource;
|
||||
class Texture2D;
|
||||
@@ -44,6 +44,9 @@ using UniqueShaderProgram = std::unique_ptr<ShaderProgram>;
|
||||
using UniqueVertexBufferResource = std::unique_ptr<VertexBufferResource>;
|
||||
using UniqueUniformBufferArray = std::unique_ptr<gfx::UniformBufferArray>;
|
||||
|
||||
using DeletionTask = std::function<void(Context&)>;
|
||||
using DeletionQueue = std::vector<DeletionTask>;
|
||||
|
||||
class Context final : public gfx::Context {
|
||||
public:
|
||||
Context(RendererBackend&);
|
||||
@@ -89,6 +92,8 @@ public:
|
||||
|
||||
gfx::Texture2DPtr createTexture2D() override;
|
||||
|
||||
gfx::DynamicTexturePtr createDynamicTexture(Size size, gfx::TexturePixelType pixelType) override;
|
||||
|
||||
RenderTargetPtr createRenderTarget(const Size size, const gfx::TextureChannelDataType type) override;
|
||||
|
||||
void resetState(gfx::DepthMode, gfx::ColorMode) override {}
|
||||
@@ -143,8 +148,10 @@ public:
|
||||
const vk::UniquePipelineLayout& getPushConstantPipelineLayout();
|
||||
|
||||
uint8_t getCurrentFrameResourceIndex() const { return frameResourceIndex; }
|
||||
void enqueueDeletion(std::function<void(Context&)>&& function);
|
||||
void submitOneTimeCommand(const std::function<void(const vk::UniqueCommandBuffer&)>& function) const;
|
||||
void enqueueDeletion(DeletionTask&& function);
|
||||
void submitOneTimeCommand(const std::function<void(const vk::UniqueCommandBuffer&)>& function);
|
||||
void submitOneTimeCommand(const vk::UniqueCommandPool& commandPool,
|
||||
const std::function<void(const vk::UniqueCommandBuffer&)>& function);
|
||||
|
||||
void requestSurfaceUpdate(bool useDelay = true);
|
||||
|
||||
@@ -153,7 +160,7 @@ private:
|
||||
vk::UniqueCommandBuffer commandBuffer;
|
||||
vk::UniqueFence flightFrameFence;
|
||||
|
||||
std::vector<std::function<void(Context&)>> deletionQueue;
|
||||
DeletionQueue deletionQueue;
|
||||
|
||||
FrameResources(vk::UniqueCommandBuffer& cb, vk::UniqueFence&& flight)
|
||||
: commandBuffer(std::move(cb)),
|
||||
@@ -201,6 +208,10 @@ private:
|
||||
|
||||
PipelineInfo pipelineInfo;
|
||||
} clipping;
|
||||
|
||||
#if DYNAMIC_TEXTURE_VULKAN_MULTITHREADED_UPLOAD
|
||||
std::mutex graphicsQueueSubmitMutex;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace vulkan
|
||||
|
||||
42
include/mbgl/vulkan/dynamic_texture.hpp
Normal file
42
include/mbgl/vulkan/dynamic_texture.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/dynamic_texture.hpp>
|
||||
#include <mbgl/vulkan/texture2d.hpp>
|
||||
|
||||
#define DYNAMIC_TEXTURE_VULKAN_MULTITHREADED_UPLOAD 1
|
||||
|
||||
namespace mbgl {
|
||||
namespace vulkan {
|
||||
|
||||
class Context;
|
||||
struct BufferAllocation;
|
||||
using UniqueBufferAllocation = std::unique_ptr<BufferAllocation>;
|
||||
|
||||
class DynamicTexture : public gfx::DynamicTexture {
|
||||
public:
|
||||
DynamicTexture(Context& context, Size size, gfx::TexturePixelType pixelType);
|
||||
|
||||
void uploadImage(const uint8_t* pixelData, gfx::TextureHandle& texHandle) override;
|
||||
void uploadDeferredImages() override;
|
||||
bool removeTexture(const gfx::TextureHandle& texHandle) override;
|
||||
|
||||
#if DYNAMIC_TEXTURE_VULKAN_MULTITHREADED_UPLOAD
|
||||
using TexturesToBlit = std::unordered_map<gfx::TextureHandle, gfx::Texture2DPtr, gfx::TextureHandle::Hasher>;
|
||||
#else
|
||||
using TextureBuffersToUpload =
|
||||
std::unordered_map<gfx::TextureHandle, UniqueBufferAllocation, gfx::TextureHandle::Hasher>;
|
||||
#endif
|
||||
|
||||
private:
|
||||
Context& context;
|
||||
|
||||
#if DYNAMIC_TEXTURE_VULKAN_MULTITHREADED_UPLOAD
|
||||
TexturesToBlit texturesToBlit;
|
||||
vk::UniqueCommandPool commandPool;
|
||||
#else
|
||||
TextureBuffersToUpload textureBuffersToUpload;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace vulkan
|
||||
} // namespace mbgl
|
||||
@@ -41,6 +41,7 @@ enum class Texture2DUsage {
|
||||
ShaderInput,
|
||||
Attachment,
|
||||
Read,
|
||||
Blit,
|
||||
};
|
||||
|
||||
class Texture2D : public gfx::Texture2D {
|
||||
@@ -72,7 +73,8 @@ public:
|
||||
const Size& size,
|
||||
uint16_t xOffset,
|
||||
uint16_t yOffset,
|
||||
const vk::UniqueCommandBuffer& buffer) noexcept;
|
||||
const vk::UniqueCommandBuffer& buffer,
|
||||
std::vector<std::function<void(Context&)>>* deletionQueue = nullptr) noexcept;
|
||||
|
||||
bool needsUpload() const noexcept override { return !!imageData; };
|
||||
|
||||
@@ -83,7 +85,7 @@ public:
|
||||
const vk::Image& getVulkanImage() const { return imageAllocation->image; }
|
||||
const vk::Sampler& getVulkanSampler();
|
||||
|
||||
void copyImage(vk::Image image);
|
||||
void copyImage(vk::Image image, Size imageSize, uint16_t xOffset = 0, uint16_t yOffset = 0);
|
||||
std::shared_ptr<PremultipliedImage> readImage();
|
||||
|
||||
private:
|
||||
@@ -97,7 +99,8 @@ private:
|
||||
void destroyTexture();
|
||||
void destroySampler();
|
||||
|
||||
void transitionToTransferLayout(const vk::UniqueCommandBuffer&);
|
||||
void transitionToTransferWriteLayout(const vk::UniqueCommandBuffer&);
|
||||
void transitionToTransferReadLayout(const vk::UniqueCommandBuffer&);
|
||||
void transitionToShaderReadLayout(const vk::UniqueCommandBuffer&);
|
||||
void transitionToGeneralLayout(const vk::UniqueCommandBuffer&);
|
||||
|
||||
@@ -123,6 +126,8 @@ private:
|
||||
vk::Sampler sampler{};
|
||||
|
||||
Texture2DUsage textureUsage{Texture2DUsage::ShaderInput};
|
||||
|
||||
friend class DynamicTexture;
|
||||
};
|
||||
|
||||
} // namespace vulkan
|
||||
|
||||
@@ -57,6 +57,7 @@ public:
|
||||
TileLayerGroupPtr createTileLayerGroup(int32_t layerIndex, std::size_t initialCapacity, std::string name) override;
|
||||
LayerGroupPtr createLayerGroup(int32_t layerIndex, std::size_t initialCapacity, std::string name) override;
|
||||
gfx::Texture2DPtr createTexture2D() override;
|
||||
gfx::DynamicTexturePtr createDynamicTexture(Size size, gfx::TexturePixelType pixelType) override;
|
||||
RenderTargetPtr createRenderTarget(const Size size, const gfx::TextureChannelDataType type) override;
|
||||
|
||||
// State management
|
||||
|
||||
27
include/mbgl/webgpu/dynamic_texture.hpp
Normal file
27
include/mbgl/webgpu/dynamic_texture.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/dynamic_texture.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace webgpu {
|
||||
|
||||
class Context;
|
||||
|
||||
class DynamicTexture : public gfx::DynamicTexture {
|
||||
public:
|
||||
DynamicTexture(Context& context, Size size, gfx::TexturePixelType pixelType);
|
||||
|
||||
void uploadImage(const uint8_t* pixelData, gfx::TextureHandle& texHandle) override;
|
||||
void uploadDeferredImages() override;
|
||||
bool removeTexture(const gfx::TextureHandle& texHandle) override;
|
||||
|
||||
using ImagesToUpload =
|
||||
std::unordered_map<gfx::TextureHandle, std::unique_ptr<uint8_t[]>, gfx::TextureHandle::Hasher>;
|
||||
|
||||
private:
|
||||
bool deferredCreation = false;
|
||||
ImagesToUpload imagesToUpload;
|
||||
};
|
||||
|
||||
} // namespace webgpu
|
||||
} // namespace mbgl
|
||||
Reference in New Issue
Block a user