diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2019-03-06 18:26:32 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2019-03-12 11:03:54 +0100 |
commit | a7f151ab487d656340d4ace415abacf9e8cecbf9 (patch) | |
tree | 49d4ed12f2cbb8a256f4acbd6603d955f22907a2 /src/mbgl/gl | |
parent | 2a25270298f358f815b22c87ece74fd3f37a42b5 (diff) | |
download | qtlocation-mapboxgl-a7f151ab487d656340d4ace415abacf9e8cecbf9.tar.gz |
[core] move Texture to the gfx namespace
Diffstat (limited to 'src/mbgl/gl')
-rw-r--r-- | src/mbgl/gl/context.cpp | 69 | ||||
-rw-r--r-- | src/mbgl/gl/context.hpp | 55 | ||||
-rw-r--r-- | src/mbgl/gl/texture.hpp | 33 | ||||
-rw-r--r-- | src/mbgl/gl/texture_resource.hpp | 18 | ||||
-rw-r--r-- | src/mbgl/gl/types.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/gl/value.hpp | 2 |
6 files changed, 72 insertions, 107 deletions
diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index c8236e0b0a..2758e236e4 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -2,6 +2,7 @@ #include <mbgl/gl/enum.hpp> #include <mbgl/gl/vertex_buffer_resource.hpp> #include <mbgl/gl/index_buffer_resource.hpp> +#include <mbgl/gl/texture_resource.hpp> #include <mbgl/gl/debugging_extension.hpp> #include <mbgl/gl/vertex_array_extension.hpp> #include <mbgl/gl/program_binary_extension.hpp> @@ -266,7 +267,7 @@ void Context::updateIndexBufferResource(const gfx::IndexBufferResource& resource } -UniqueTexture Context::createTexture() { +UniqueTexture Context::createUniqueTexture() { if (pooledTextures.empty()) { pooledTextures.resize(TextureMax); MBGL_CHECK_ERROR(glGenTextures(TextureMax, pooledTextures.data())); @@ -468,83 +469,91 @@ Framebuffer Context::createFramebuffer(const Renderbuffer<RenderbufferType::RGBA } Framebuffer -Context::createFramebuffer(const Texture& color, +Context::createFramebuffer(const gfx::Texture& color, const Renderbuffer<RenderbufferType::DepthStencil>& depthStencil) { if (color.size != depthStencil.size) { throw std::runtime_error("Renderbuffer size mismatch"); } auto fbo = createFramebuffer(); bindFramebuffer = fbo; - MBGL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - color.texture, 0)); + MBGL_CHECK_ERROR(glFramebufferTexture2D( + GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + reinterpret_cast<const gl::TextureResource&>(*color.resource).texture, 0)); bindDepthStencilRenderbuffer(depthStencil); checkFramebuffer(); return { color.size, std::move(fbo) }; } -Framebuffer Context::createFramebuffer(const Texture& color) { +Framebuffer Context::createFramebuffer(const gfx::Texture& color) { auto fbo = createFramebuffer(); bindFramebuffer = fbo; - MBGL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - color.texture, 0)); + MBGL_CHECK_ERROR(glFramebufferTexture2D( + GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + reinterpret_cast<const gl::TextureResource&>(*color.resource).texture, 0)); checkFramebuffer(); return { color.size, std::move(fbo) }; } Framebuffer -Context::createFramebuffer(const Texture& color, +Context::createFramebuffer(const gfx::Texture& color, const Renderbuffer<RenderbufferType::DepthComponent>& depthTarget) { if (color.size != depthTarget.size) { throw std::runtime_error("Renderbuffer size mismatch"); } auto fbo = createFramebuffer(); bindFramebuffer = fbo; - MBGL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color.texture, 0)); - MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthTarget.renderbuffer)); + MBGL_CHECK_ERROR(glFramebufferTexture2D( + GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + reinterpret_cast<const gl::TextureResource&>(*color.resource).texture, 0)); + MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, + depthTarget.renderbuffer)); checkFramebuffer(); return { depthTarget.size, std::move(fbo) }; } -UniqueTexture Context::createTexture(const Size size, - const void* data, - gfx::TexturePixelType format, - TextureUnit unit, - gfx::TextureChannelDataType type) { - auto obj = createTexture(); +std::unique_ptr<const gfx::TextureResource> +Context::createTextureResource(const Size size, + const void* data, + gfx::TexturePixelType format, + uint8_t unit, + gfx::TextureChannelDataType type) { + auto obj = createUniqueTexture(); + std::unique_ptr<const gfx::TextureResource> resource = std::make_unique<gl::TextureResource>(std::move(obj)); pixelStoreUnpack = { 1 }; - updateTexture(obj, size, data, format, unit, type); + updateTextureResource(*resource, size, data, format, unit, type); // We are using clamp to edge here since OpenGL ES doesn't allow GL_REPEAT on NPOT textures. // We use those when the pixelRatio isn't a power of two, e.g. on iPhone 6 Plus. MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); - return obj; + return resource; } -void Context::updateTexture(TextureID id, - const Size size, - const void* data, - gfx::TexturePixelType format, - TextureUnit unit, - gfx::TextureChannelDataType type) { +void Context::updateTextureResource(const gfx::TextureResource& resource, + const Size size, + const void* data, + gfx::TexturePixelType format, + uint8_t unit, + gfx::TextureChannelDataType type) { activeTextureUnit = unit; - texture[unit] = id; + texture[unit] = reinterpret_cast<const gl::TextureResource&>(resource).texture; MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, Enum<gfx::TexturePixelType>::to(format), size.width, size.height, 0, Enum<gfx::TexturePixelType>::to(format), Enum<gfx::TextureChannelDataType>::to(type), data)); } -void Context::bindTexture(Texture& obj, - TextureUnit unit, +void Context::bindTexture(gfx::Texture& obj, + uint8_t unit, gfx::TextureFilterType filter, gfx::TextureMipMapType mipmap, gfx::TextureWrapType wrapX, gfx::TextureWrapType wrapY) { + TextureID id = reinterpret_cast<const gl::TextureResource&>(*obj.resource).texture; if (filter != obj.filter || mipmap != obj.mipmap || wrapX != obj.wrapX || wrapY != obj.wrapY) { activeTextureUnit = unit; - texture[unit] = obj.texture; + texture[unit] = id; if (filter != obj.filter || mipmap != obj.mipmap) { MBGL_CHECK_ERROR(glTexParameteri( @@ -571,11 +580,11 @@ void Context::bindTexture(Texture& obj, wrapY == gfx::TextureWrapType::Clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT)); obj.wrapY = wrapY; } - } else if (texture[unit] != obj.texture) { + } else if (texture[unit] != id) { // We are checking first to avoid setting the active texture without a subsequent // texture bind. activeTextureUnit = unit; - texture[unit] = obj.texture; + texture[unit] = id; } } diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index 8b7b9346b3..1200e3b49a 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -5,7 +5,6 @@ #include <mbgl/gl/object.hpp> #include <mbgl/gl/state.hpp> #include <mbgl/gl/value.hpp> -#include <mbgl/gl/texture.hpp> #include <mbgl/gl/renderbuffer.hpp> #include <mbgl/gl/framebuffer.hpp> #include <mbgl/gl/vertex_array.hpp> @@ -52,7 +51,7 @@ public: UniqueProgram createProgram(BinaryProgramFormat binaryFormat, const std::string& binaryProgram); void verifyProgramLinkage(ProgramID); void linkProgram(ProgramID); - UniqueTexture createTexture(); + UniqueTexture createUniqueTexture(); VertexArray createVertexArray(); #if MBGL_HAS_BINARY_PROGRAMS @@ -74,10 +73,10 @@ public: Framebuffer createFramebuffer(const Renderbuffer<RenderbufferType::RGBA>&, const Renderbuffer<RenderbufferType::DepthStencil>&); Framebuffer createFramebuffer(const Renderbuffer<RenderbufferType::RGBA>&); - Framebuffer createFramebuffer(const Texture&, + Framebuffer createFramebuffer(const gfx::Texture&, const Renderbuffer<RenderbufferType::DepthStencil>&); - Framebuffer createFramebuffer(const Texture&); - Framebuffer createFramebuffer(const Texture&, + Framebuffer createFramebuffer(const gfx::Texture&); + Framebuffer createFramebuffer(const gfx::Texture&, const Renderbuffer<RenderbufferType::DepthComponent>&); template <typename Image, @@ -89,6 +88,13 @@ public: return { size, readFramebuffer(size, format, flip) }; } + void bindTexture(gfx::Texture&, + uint8_t unit = 0, + gfx::TextureFilterType = gfx::TextureFilterType::Nearest, + gfx::TextureMipMapType = gfx::TextureMipMapType::No, + gfx::TextureWrapType wrapX = gfx::TextureWrapType::Clamp, + gfx::TextureWrapType wrapY = gfx::TextureWrapType::Clamp) override; + #if not MBGL_USE_GLES2 template <typename Image> void drawPixels(const Image& image) { @@ -97,40 +103,6 @@ public: } #endif // MBGL_USE_GLES2 - // Create a texture from an image with data. - template <typename Image> - Texture createTexture(const Image& image, - TextureUnit unit = 0, - gfx::TextureChannelDataType type = gfx::TextureChannelDataType::UnsignedByte) { - auto format = image.channels == 4 ? gfx::TexturePixelType::RGBA : gfx::TexturePixelType::Alpha; - return { image.size, createTexture(image.size, image.data.get(), format, unit, type) }; - } - - template <typename Image> - void updateTexture(Texture& obj, - const Image& image, - TextureUnit unit = 0, - gfx::TextureChannelDataType type = gfx::TextureChannelDataType::UnsignedByte) { - auto format = image.channels == 4 ? gfx::TexturePixelType::RGBA : gfx::TexturePixelType::Alpha; - updateTexture(obj.texture.get(), image.size, image.data.get(), format, unit, type); - obj.size = image.size; - } - - // Creates an empty texture with the specified dimensions. - Texture createTexture(const Size size, - gfx::TexturePixelType format = gfx::TexturePixelType::RGBA, - TextureUnit unit = 0, - gfx::TextureChannelDataType type = gfx::TextureChannelDataType::UnsignedByte) { - return { size, createTexture(size, nullptr, format, unit, type) }; - } - - void bindTexture(Texture&, - TextureUnit = 0, - gfx::TextureFilterType = gfx::TextureFilterType::Nearest, - gfx::TextureMipMapType = gfx::TextureMipMapType::No, - gfx::TextureWrapType wrapX = gfx::TextureWrapType::Clamp, - gfx::TextureWrapType wrapY = gfx::TextureWrapType::Clamp); - void clear(optional<mbgl::Color> color, optional<float> depth, optional<int32_t> stencil); @@ -248,8 +220,9 @@ private: std::unique_ptr<const gfx::IndexBufferResource> createIndexBufferResource(const void* data, std::size_t size, const gfx::BufferUsageType) override; void updateIndexBufferResource(const gfx::IndexBufferResource&, const void* data, std::size_t size) override; - UniqueTexture createTexture(Size size, const void* data, gfx::TexturePixelType, TextureUnit, gfx::TextureChannelDataType); - void updateTexture(TextureID, Size size, const void* data, gfx::TexturePixelType, TextureUnit, gfx::TextureChannelDataType); + std::unique_ptr<const gfx::TextureResource> createTextureResource(Size, const void* data, gfx::TexturePixelType, uint8_t, gfx::TextureChannelDataType) override; + void updateTextureResource(const gfx::TextureResource&, Size, const void* data, gfx::TexturePixelType, uint8_t, gfx::TextureChannelDataType) override; + UniqueFramebuffer createFramebuffer(); UniqueRenderbuffer createRenderbuffer(RenderbufferType, Size size); std::unique_ptr<uint8_t[]> readFramebuffer(Size, gfx::TexturePixelType, bool flip); diff --git a/src/mbgl/gl/texture.hpp b/src/mbgl/gl/texture.hpp deleted file mode 100644 index 1b85ac6ebc..0000000000 --- a/src/mbgl/gl/texture.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <mbgl/gfx/types.hpp> -#include <mbgl/gl/object.hpp> -#include <mbgl/util/size.hpp> - -namespace mbgl { -namespace gl { - -class Texture { -public: - Texture(Size size_, UniqueTexture texture_, - gfx::TextureFilterType filter_ = gfx::TextureFilterType::Nearest, - gfx::TextureMipMapType mipmap_ = gfx::TextureMipMapType::No, - gfx::TextureWrapType wrapX_ = gfx::TextureWrapType::Clamp, - gfx::TextureWrapType wrapY_ = gfx::TextureWrapType::Clamp) - : size(std::move(size_)), - texture(std::move(texture_)), - filter(filter_), - mipmap(mipmap_), - wrapX(wrapX_), - wrapY(wrapY_) {} - - Size size; - UniqueTexture texture; - gfx::TextureFilterType filter; - gfx::TextureMipMapType mipmap; - gfx::TextureWrapType wrapX; - gfx::TextureWrapType wrapY; -}; - -} // namespace gl -} // namespace mbgl diff --git a/src/mbgl/gl/texture_resource.hpp b/src/mbgl/gl/texture_resource.hpp new file mode 100644 index 0000000000..4803f9b7f4 --- /dev/null +++ b/src/mbgl/gl/texture_resource.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <mbgl/gfx/texture.hpp> +#include <mbgl/gl/object.hpp> + +namespace mbgl { +namespace gl { + +class TextureResource : public gfx::TextureResource { +public: + TextureResource(UniqueTexture&& texture_) : texture(std::move(texture_)) { + } + + UniqueTexture texture; +}; + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/gl/types.hpp b/src/mbgl/gl/types.hpp index ab6b72656b..5cb486f0c9 100644 --- a/src/mbgl/gl/types.hpp +++ b/src/mbgl/gl/types.hpp @@ -25,8 +25,6 @@ using AttributeLocation = uint32_t; // "silently ignored". using UniformLocation = int32_t; -using TextureUnit = uint8_t; - enum class ShaderType : uint32_t { Vertex = 0x8B31, Fragment = 0x8B30 diff --git a/src/mbgl/gl/value.hpp b/src/mbgl/gl/value.hpp index 9c01039b99..15812a49bd 100644 --- a/src/mbgl/gl/value.hpp +++ b/src/mbgl/gl/value.hpp @@ -168,7 +168,7 @@ struct LineWidth { }; struct ActiveTextureUnit { - using Type = TextureUnit; + using Type = uint8_t; static const constexpr Type Default = 0; static void Set(const Type&); static Type Get(); |