From f76fb98745efdf173cce64ea26a023bcbd431711 Mon Sep 17 00:00:00 2001 From: Tim Sylvester Date: Wed, 19 Apr 2023 15:49:36 -0700 Subject: [PATCH] Drawable Renderer (#1570), Rebase on main, squash trivial commits --- include/mbgl/gfx/vertex_attribute.hpp | 14 ++++++---- include/mbgl/gl/vertex_attribute_gl.hpp | 12 ++++----- src/mbgl/gfx/drawable_builder.cpp | 2 +- src/mbgl/gfx/vertex_attribute.cpp | 5 ---- src/mbgl/gl/renderer_backend.cpp | 6 ++--- src/mbgl/gl/vertex_attribute_gl.cpp | 31 +++++++++++++---------- src/mbgl/shaders/gl/shader_program_gl.cpp | 11 ++++---- 7 files changed, 41 insertions(+), 40 deletions(-) diff --git a/include/mbgl/gfx/vertex_attribute.hpp b/include/mbgl/gfx/vertex_attribute.hpp index 46353537a..ff018f539 100644 --- a/include/mbgl/gfx/vertex_attribute.hpp +++ b/include/mbgl/gfx/vertex_attribute.hpp @@ -33,7 +33,9 @@ public: using int3 = std::array; using int4 = std::array; - using ElementType = std::variant; + using ElementType = std::variant; // Can only be created by VertexAttributeArray implementations protected: @@ -75,7 +77,7 @@ public: protected: VertexAttribute& operator=(const VertexAttribute&) = default; - VertexAttribute& operator=(const VertexAttribute&& other) { + VertexAttribute& operator=(VertexAttribute&& other) { index = other.index; size = other.size; dataType = other.dataType; @@ -101,7 +103,8 @@ public: VertexAttributeArray(int initCapacity = 10); VertexAttributeArray(VertexAttributeArray &&); - VertexAttributeArray(const VertexAttributeArray&); + // Would need to use the virtual assignment operator + VertexAttributeArray(const VertexAttributeArray&) = delete; virtual ~VertexAttributeArray() = default; std::size_t size() const { return attrs.size(); } @@ -123,10 +126,11 @@ public: // Set a value if the element is present template - const VertexAttribute::ElementType& set(const std::string& name, std::size_t i, T value) { + bool set(const std::string& name, std::size_t i, T value) { if (auto *item = get(name)) { - item->set(i, value); + return item->set(i, value); } + return false; } /// Indicates whether any values have changed diff --git a/include/mbgl/gl/vertex_attribute_gl.hpp b/include/mbgl/gl/vertex_attribute_gl.hpp index a343b90c7..58dd2ec37 100644 --- a/include/mbgl/gl/vertex_attribute_gl.hpp +++ b/include/mbgl/gl/vertex_attribute_gl.hpp @@ -13,15 +13,15 @@ class VertexAttributeGL final : public gfx::VertexAttribute { private: friend VertexAttributeArrayGL; VertexAttributeGL(int index_, gfx::AttributeDataType dataType_, int size_, std::size_t count_) - : VertexAttribute(index_, dataType_, size_, count_) { + : VertexAttribute(index_, dataType_, size_, count_) { } VertexAttributeGL(const VertexAttributeGL& other) - : VertexAttribute(other), - glType(other.glType) { + : VertexAttribute(other), + glType(other.glType) { } VertexAttributeGL(VertexAttributeGL&& other) - : VertexAttribute(std::move(other)), - glType(other.glType) { + : VertexAttribute(std::move(other)), + glType(other.glType) { } public: @@ -46,7 +46,7 @@ class VertexAttributeArrayGL final : public gfx::VertexAttributeArray { public: VertexAttributeArrayGL(int initCapacity = 10) : VertexAttributeArray(initCapacity) { } VertexAttributeArrayGL(VertexAttributeArrayGL &&other) : VertexAttributeArray(std::move(other)) { } - VertexAttributeArrayGL(const VertexAttributeArrayGL& other) : VertexAttributeArray(other) { } + VertexAttributeArrayGL(const VertexAttributeArrayGL&) = delete; ~VertexAttributeArrayGL() = default; VertexAttributeArrayGL& operator=(VertexAttributeArrayGL &&other) { diff --git a/src/mbgl/gfx/drawable_builder.cpp b/src/mbgl/gfx/drawable_builder.cpp index b37e06da1..cd18b94a6 100644 --- a/src/mbgl/gfx/drawable_builder.cpp +++ b/src/mbgl/gfx/drawable_builder.cpp @@ -26,7 +26,7 @@ DrawablePtr DrawableBuilder::getCurrentDrawable(bool createIfNone) { void DrawableBuilder::flush() { if (!impl->vertices.empty()) { - auto draw = getCurrentDrawable(/*create=*/true); + auto draw = getCurrentDrawable(/*createIfNone=*/true); currentDrawable->setDrawPriority(drawPriority); currentDrawable->setShader(shader); currentDrawable->setVertexAttributes(getVertexAttributes()); diff --git a/src/mbgl/gfx/vertex_attribute.cpp b/src/mbgl/gfx/vertex_attribute.cpp index 59da28b7c..e825336e6 100644 --- a/src/mbgl/gfx/vertex_attribute.cpp +++ b/src/mbgl/gfx/vertex_attribute.cpp @@ -11,11 +11,6 @@ VertexAttributeArray::VertexAttributeArray(VertexAttributeArray &&other) : attrs(std::move(other.attrs)) { } -VertexAttributeArray::VertexAttributeArray(const VertexAttributeArray& other) - : attrs(other.attrs.size()) { - operator=(other); -} - VertexAttributeArray& VertexAttributeArray::operator=(VertexAttributeArray &&other) { attrs = std::move(other.attrs); return *this; diff --git a/src/mbgl/gl/renderer_backend.cpp b/src/mbgl/gl/renderer_backend.cpp index 298729a42..053df098e 100644 --- a/src/mbgl/gl/renderer_backend.cpp +++ b/src/mbgl/gl/renderer_backend.cpp @@ -75,8 +75,7 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) { auto shader = shaders.get(shaderName); if (!shader) { - constexpr auto vert = R"( - #version 300 es + constexpr auto vert = R"(#version 300 es precision highp float; in vec2 a_pos; @@ -94,8 +93,7 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) { gl_Position = u_matrix * vec4(gl_Position.xy * 8192.0, 0, 1); //gl_Position = u_matrix * vec4(a_pos, 0, 1); })"; - constexpr auto frag = R"( - #version 300 es + constexpr auto frag = R"(#version 300 es precision highp float; in vec4 pos; out vec4 color; diff --git a/src/mbgl/gl/vertex_attribute_gl.cpp b/src/mbgl/gl/vertex_attribute_gl.cpp index e9bbef606..75677db37 100644 --- a/src/mbgl/gl/vertex_attribute_gl.cpp +++ b/src/mbgl/gl/vertex_attribute_gl.cpp @@ -71,22 +71,25 @@ const std::vector& VertexAttributeGL::getRaw() const { return rawData; } -struct ApplyUniform { - GLint location; - template void operator()(const T&); - - template <> void operator()(const std::int32_t&) { } - template <> void operator()(const gfx::VertexAttribute::int2&) { } - template <> void operator()(const gfx::VertexAttribute::int3&) { } - template <> void operator()(const gfx::VertexAttribute::int4&) { } - template <> void operator()(const float& ) { } - template <> void operator()(const gfx::VertexAttribute::float2& ) { } - template <> void operator()(const gfx::VertexAttribute::float3& ) { } - template <> void operator()(const gfx::VertexAttribute::float4& ) { } - template <> void operator()(const gfx::VertexAttribute::matf3& ) { } - template <> void operator()(const gfx::VertexAttribute::matf4& value) { +namespace { + template void applyUniform(GLint, const T&); + template <> void applyUniform(GLint, const std::int32_t&) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::int2&) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::int3&) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::int4&) { } + template <> void applyUniform(GLint, const float& ) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::float2& ) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::float3& ) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::float4& ) { } + template <> void applyUniform(GLint, const gfx::VertexAttribute::matf3& ) { } + template <> void applyUniform(GLint location, const gfx::VertexAttribute::matf4& value) { MBGL_CHECK_ERROR(glUniformMatrix4fv(location, 1, GL_FALSE, &value[0])); } +} + +struct ApplyUniform { + GLint location; + template void operator()(const T& value) { applyUniform(location, value); } }; void VertexAttributeArrayGL::applyUniforms(const gfx::ShaderProgramBase& shader) { diff --git a/src/mbgl/shaders/gl/shader_program_gl.cpp b/src/mbgl/shaders/gl/shader_program_gl.cpp index eed421058..047c8c3f7 100644 --- a/src/mbgl/shaders/gl/shader_program_gl.cpp +++ b/src/mbgl/shaders/gl/shader_program_gl.cpp @@ -119,13 +119,13 @@ std::shared_ptr ShaderProgramGL::create( MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count)); MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength)); - auto name = std::make_unique(maxLength); + auto name = std::vector(maxLength); for (GLint index = 0; index < count; ++index) { GLsizei length = 0; GLint size = 0; GLenum glType = 0; - MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &length, &size, &glType, name.get())); - addAttr(uniforms, name.get(), index, length, size, glType); + MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &length, &size, &glType, &name[0])); + addAttr(uniforms, &name[0], index, length, size, glType); } VertexAttributeArrayGL attrs; @@ -133,12 +133,13 @@ std::shared_ptr ShaderProgramGL::create( count = 0; MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count)); MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength)); + name.resize(maxLength); for (GLint index = 0; index < count; ++index) { GLsizei length = 0; // "number of characters actually written in name (excluding the null terminator)" GLint size = 0; // "size of the attribute variable, in units of the type returned in type" GLenum glType = 0; - MBGL_CHECK_ERROR(glGetActiveAttrib(program, index, maxLength, &length, &size, &glType, name.get())); - addAttr(attrs, name.get(), index, length, size, glType); + MBGL_CHECK_ERROR(glGetActiveAttrib(program, index, maxLength, &length, &size, &glType, &name[0])); + addAttr(attrs, &name[0], index, length, size, glType); } return std::make_shared(std::move(program), std::move(uniforms), std::move(attrs));