From 867555b1c9ef51fec23ce77c682cf7d5b5a23c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 13 May 2019 17:13:31 -0700 Subject: [core] add gfx::UploadPass, split startRender into prepare and upload --- src/mbgl/gfx/upload_pass.hpp | 109 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/mbgl/gfx/upload_pass.hpp (limited to 'src/mbgl/gfx/upload_pass.hpp') diff --git a/src/mbgl/gfx/upload_pass.hpp b/src/mbgl/gfx/upload_pass.hpp new file mode 100644 index 0000000000..f0bf9d7e2d --- /dev/null +++ b/src/mbgl/gfx/upload_pass.hpp @@ -0,0 +1,109 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace mbgl { +namespace gfx { + +class UploadPass { +protected: + UploadPass() = default; + + friend class DebugGroup; + virtual void pushDebugGroup(const char* name) = 0; + virtual void popDebugGroup() = 0; + +public: + virtual ~UploadPass() = default; + UploadPass(const UploadPass&) = delete; + UploadPass& operator=(const UploadPass&) = delete; + + DebugGroup createDebugGroup(const char* name) { + return { *this, name }; + } + +public: + template + VertexBuffer + createVertexBuffer(VertexVector&& v, + const BufferUsageType usage = BufferUsageType::StaticDraw) { + return { v.elements(), createVertexBufferResource(v.data(), v.bytes(), usage) }; + } + + template + void updateVertexBuffer(VertexBuffer& buffer, VertexVector&& v) { + assert(v.elements() == buffer.elements); + updateVertexBufferResource(buffer.getResource(), v.data(), v.bytes()); + } + + template + IndexBuffer createIndexBuffer(IndexVector&& v, + const BufferUsageType usage = BufferUsageType::StaticDraw) { + return { v.elements(), createIndexBufferResource(v.data(), v.bytes(), usage) }; + } + + template + void updateIndexBuffer(IndexBuffer& buffer, IndexVector&& v) { + assert(v.elements() == buffer.elements); + updateIndexBufferResource(buffer.getResource(), v.data(), v.bytes()); + } + +protected: + virtual std::unique_ptr + createVertexBufferResource(const void* data, std::size_t size, const BufferUsageType) = 0; + virtual void + updateVertexBufferResource(VertexBufferResource&, const void* data, std::size_t size) = 0; + + virtual std::unique_ptr + createIndexBufferResource(const void* data, std::size_t size, const BufferUsageType) = 0; + virtual void + updateIndexBufferResource(IndexBufferResource&, const void* data, std::size_t size) = 0; + +public: + // Create a texture from an image with data. + template + Texture createTexture(const Image& image, + TextureChannelDataType type = TextureChannelDataType::UnsignedByte) { + auto format = image.channels == 4 ? TexturePixelType::RGBA : TexturePixelType::Alpha; + return { image.size, + createTextureResource(image.size, image.data.get(), format, type) }; + } + + template + void updateTexture(Texture& texture, + const Image& image, + TextureChannelDataType type = TextureChannelDataType::UnsignedByte) { + auto format = image.channels == 4 ? TexturePixelType::RGBA : TexturePixelType::Alpha; + updateTextureResource(texture.getResource(), image.size, image.data.get(), format, type); + texture.size = image.size; + } + + template + void updateTextureSub(Texture& texture, + const Image& image, + const uint16_t offsetX, + const uint16_t offsetY, + TextureChannelDataType type = TextureChannelDataType::UnsignedByte) { + assert(image.size.width + offsetX <= texture.size.width); + assert(image.size.height + offsetY <= texture.size.height); + auto format = image.channels == 4 ? TexturePixelType::RGBA : TexturePixelType::Alpha; + updateTextureResourceSub(texture.getResource(), offsetX, offsetY, image.size, image.data.get(), format, type); + } + +protected: + virtual std::unique_ptr createTextureResource( + Size, const void* data, TexturePixelType, TextureChannelDataType) = 0; + virtual void updateTextureResource(TextureResource&, Size, const void* data, + TexturePixelType, TextureChannelDataType) = 0; + virtual void updateTextureResourceSub(TextureResource&, uint16_t xOffset, uint16_t yOffset, Size, const void* data, + TexturePixelType, TextureChannelDataType) = 0; +}; + +} // namespace gfx +} // namespace mbgl -- cgit v1.2.1