diff options
Diffstat (limited to 'src/mbgl/gfx')
-rw-r--r-- | src/mbgl/gfx/context.hpp | 43 | ||||
-rw-r--r-- | src/mbgl/gfx/texture.hpp | 33 |
2 files changed, 76 insertions, 0 deletions
diff --git a/src/mbgl/gfx/context.hpp b/src/mbgl/gfx/context.hpp index b57cfcb65c..05a87eeeac 100644 --- a/src/mbgl/gfx/context.hpp +++ b/src/mbgl/gfx/context.hpp @@ -4,6 +4,7 @@ #include <mbgl/gfx/vertex_buffer.hpp> #include <mbgl/gfx/index_vector.hpp> #include <mbgl/gfx/index_buffer.hpp> +#include <mbgl/gfx/texture.hpp> #include <mbgl/gfx/types.hpp> namespace mbgl { @@ -56,6 +57,48 @@ protected: createIndexBufferResource(const void* data, std::size_t size, const BufferUsageType) = 0; virtual void updateIndexBufferResource(const IndexBufferResource&, const void* data, std::size_t size) = 0; + +public: + // Create a texture from an image with data. + template <typename Image> + Texture createTexture(const Image& image, + uint8_t unit = 0, + TextureChannelDataType type = TextureChannelDataType::UnsignedByte) { + auto format = image.channels == 4 ? TexturePixelType::RGBA : TexturePixelType::Alpha; + return { image.size, + createTextureResource(image.size, image.data.get(), format, unit, type) }; + } + + // Creates an empty texture with the specified dimensions. + Texture createTexture(const Size size, + TexturePixelType format = TexturePixelType::RGBA, + uint8_t unit = 0, + TextureChannelDataType type = TextureChannelDataType::UnsignedByte) { + return { size, createTextureResource(size, nullptr, format, unit, type) }; + } + + template <typename Image> + void updateTexture(Texture& texture, + const Image& image, + uint8_t unit = 0, + TextureChannelDataType type = TextureChannelDataType::UnsignedByte) { + auto format = image.channels == 4 ? TexturePixelType::RGBA : TexturePixelType::Alpha; + updateTextureResource(*texture.resource, image.size, image.data.get(), format, unit, type); + texture.size = image.size; + } + + virtual void bindTexture(Texture&, + uint8_t unit = 0, + TextureFilterType = TextureFilterType::Nearest, + TextureMipMapType = TextureMipMapType::No, + TextureWrapType wrapX = TextureWrapType::Clamp, + TextureWrapType wrapY = TextureWrapType::Clamp) = 0; + +protected: + virtual std::unique_ptr<const TextureResource> createTextureResource( + Size, const void* data, TexturePixelType, uint8_t unit, TextureChannelDataType) = 0; + virtual void updateTextureResource(const TextureResource&, Size, const void* data, + TexturePixelType, uint8_t unit, TextureChannelDataType) = 0; }; } // namespace gfx diff --git a/src/mbgl/gfx/texture.hpp b/src/mbgl/gfx/texture.hpp new file mode 100644 index 0000000000..ca5ab5d5d5 --- /dev/null +++ b/src/mbgl/gfx/texture.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include <mbgl/gfx/types.hpp> +#include <mbgl/util/size.hpp> + +#include <memory> + +namespace mbgl { +namespace gfx { + +class TextureResource { +protected: + TextureResource() = default; +public: + virtual ~TextureResource() = default; +}; + +class Texture { +public: + Texture(Size size_, std::unique_ptr<const TextureResource>&& resource_) + : size(std::move(size_)), resource(std::move(resource_)) { + } + + Size size; + TextureFilterType filter = TextureFilterType::Nearest; + TextureMipMapType mipmap = TextureMipMapType::No; + TextureWrapType wrapX = TextureWrapType::Clamp; + TextureWrapType wrapY = TextureWrapType::Clamp; + std::unique_ptr<const TextureResource> resource; +}; + +} // namespace gfx +} // namespace mbgl |