From a646cbf9382e3568546613e6b0af4d1121f5892a Mon Sep 17 00:00:00 2001 From: Tim Sylvester Date: Wed, 19 Apr 2023 15:30:59 -0700 Subject: [PATCH] Topic/drawable 2 (#1047) uniforms --- include/mbgl/gfx/vertex_attribute.hpp | 42 ++++++++++--- include/mbgl/gl/vertex_attribute_gl.hpp | 11 ++-- include/mbgl/shaders/gl/shader_program_gl.hpp | 14 ++++- include/mbgl/shaders/shader_program_base.hpp | 29 +++++++++ src/mbgl/gfx/vertex_attribute.cpp | 24 ++++++-- src/mbgl/gl/renderer_backend.cpp | 37 ++++++------ src/mbgl/gl/vertex_attribute_gl.cpp | 39 ++++++++++++ src/mbgl/renderer/renderer_impl.cpp | 12 +++- src/mbgl/shaders/gl/shader_program_gl.cpp | 60 +++++++++++++------ 9 files changed, 209 insertions(+), 59 deletions(-) diff --git a/include/mbgl/gfx/vertex_attribute.hpp b/include/mbgl/gfx/vertex_attribute.hpp index 4ff5bb56a..46353537a 100644 --- a/include/mbgl/gfx/vertex_attribute.hpp +++ b/include/mbgl/gfx/vertex_attribute.hpp @@ -19,16 +19,13 @@ using mat4 = std::array; namespace gfx { -// static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes"); -// static_assert(std::is_standard_layout_v, "vertex type must use standard layout"); -// static_assert(I < Descriptor::data.count, "attribute index must be in range"); - -class VertexAttributeArray; +class ShaderProgramBase; class VertexAttribute { public: using float2 = std::array; using float3 = std::array; + using float4 = std::array; using matf2 = std::array; using matf3 = std::array; using matf4 = std::array; @@ -40,14 +37,16 @@ public: // Can only be created by VertexAttributeArray implementations protected: - VertexAttribute(int index_, AttributeDataType dataType_, std::size_t count_) + VertexAttribute(int index_, AttributeDataType dataType_, int size_, std::size_t count_) : index(index_), + size(size_), dataType(dataType_), items(count_) { } VertexAttribute(const VertexAttribute &) = default; VertexAttribute(VertexAttribute&& other) : index(other.index), + size(other.size), dataType(other.dataType), items(std::move(other.items)) { } @@ -56,6 +55,10 @@ public: ~VertexAttribute() = default; int getIndex() const { return index; } + void setIndex(int value) { index = value; } + + int getSize() const { return size; } + std::size_t getCount() const { return items.size(); } AttributeDataType getDataType() const { return dataType; } @@ -67,10 +70,14 @@ public: return items[i] = value; } + bool getDirty() const { return dirty; } + void clearDirty() { dirty = false; } + protected: VertexAttribute& operator=(const VertexAttribute&) = default; VertexAttribute& operator=(const VertexAttribute&& other) { index = other.index; + size = other.size; dataType = other.dataType; items = std::move(other.items); return *this; @@ -78,6 +85,7 @@ protected: protected: int index; + int size; /// indicates that a value has changed and any cached result should be discarded mutable bool dirty = true; @@ -106,17 +114,32 @@ public: /// Add a new attribute element. /// Returns a pointer to the new element on success, or null if the attribute already exists. /// The result is valid only until the next non-const method call on this class. - VertexAttribute* add(std::string name, int index, AttributeDataType, std::size_t count); + VertexAttribute* add(std::string name, int index, AttributeDataType, int size, std::size_t count); /// Add a new attribute element if it doesn't already exist. /// Returns a pointer to the new element on success, or null if the type or count conflict with an existing entry. /// The result is valid only until the next non-const method call on this class. - VertexAttribute* getOrAdd(std::string name, int index, AttributeDataType, std::size_t count); + VertexAttribute* getOrAdd(std::string name, int index, AttributeDataType, int size, std::size_t count); + + // Set a value if the element is present + template + const VertexAttribute::ElementType& set(const std::string& name, std::size_t i, T value) { + if (auto *item = get(name)) { + item->set(i, value); + } + } + + /// Indicates whether any values have changed + bool isDirty() const; + void resetDirty(); using ResolveDelegate = std::function; /// Call the provided delegate with each value, providing the override if one exists. void resolve(const VertexAttributeArray& overrides, ResolveDelegate) const; + /// Apply to the active program + virtual void applyUniforms(const ShaderProgramBase&) = 0; + VertexAttributeArray& operator=(VertexAttributeArray &&); VertexAttributeArray& operator=(const VertexAttributeArray&); @@ -131,7 +154,8 @@ protected: } } - virtual std::unique_ptr create(int index, AttributeDataType dataType, std::size_t count) = 0; + virtual std::unique_ptr create(int index, AttributeDataType dataType, + int size, std::size_t count) = 0; virtual std::unique_ptr copy(const VertexAttribute& attr) = 0; protected: diff --git a/include/mbgl/gl/vertex_attribute_gl.hpp b/include/mbgl/gl/vertex_attribute_gl.hpp index 04f50c839..a343b90c7 100644 --- a/include/mbgl/gl/vertex_attribute_gl.hpp +++ b/include/mbgl/gl/vertex_attribute_gl.hpp @@ -12,8 +12,8 @@ class VertexAttributeGL final : public gfx::VertexAttribute { // Can only be created by VertexAttributeArrayGL private: friend VertexAttributeArrayGL; - VertexAttributeGL(int index_, gfx::AttributeDataType dataType_, std::size_t count_) - : VertexAttribute(index_, dataType_, count_) { + VertexAttributeGL(int index_, gfx::AttributeDataType dataType_, int size_, std::size_t count_) + : VertexAttribute(index_, dataType_, size_, count_) { } VertexAttributeGL(const VertexAttributeGL& other) : VertexAttribute(other), @@ -58,9 +58,12 @@ public: return *this; } + void applyUniforms(const gfx::ShaderProgramBase&) override; + private: - std::unique_ptr create(int index, gfx::AttributeDataType dataType, std::size_t count) override { - return std::unique_ptr(new VertexAttributeGL(index, dataType, count)); + std::unique_ptr create(int index, gfx::AttributeDataType dataType, + int size, std::size_t count) override { + return std::unique_ptr(new VertexAttributeGL(index, dataType, size, count)); } std::unique_ptr copy(const gfx::VertexAttribute& attr) override { return std::unique_ptr(new VertexAttributeGL(static_cast(attr))); diff --git a/include/mbgl/shaders/gl/shader_program_gl.hpp b/include/mbgl/shaders/gl/shader_program_gl.hpp index 31b26443a..364365cf4 100644 --- a/include/mbgl/shaders/gl/shader_program_gl.hpp +++ b/include/mbgl/shaders/gl/shader_program_gl.hpp @@ -10,7 +10,9 @@ namespace gl { class ShaderProgramGL final : public gfx::ShaderProgramBase { public: ShaderProgramGL(UniqueProgram&& glProgram_); - ShaderProgramGL(UniqueProgram&& glProgram_, VertexAttributeArrayGL&& attributes); + ShaderProgramGL(UniqueProgram&&, + VertexAttributeArrayGL&& uniforms, + VertexAttributeArrayGL&& attributes); ShaderProgramGL(ShaderProgramGL&& other); virtual ~ShaderProgramGL() noexcept = default; @@ -23,13 +25,21 @@ public: std::string_view vertexSource, std::string_view fragmentSource) noexcept(false); + const gfx::VertexAttributeArray& getUniforms() const override { return uniforms; } + const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; } ProgramID getGLProgramID() const { return glProgram; } +protected: + gfx::VertexAttributeArray& mutableUniforms() override { return uniforms; } + + gfx::VertexAttributeArray& mutableVertexAttributes() override { return vertexAttributes; } + protected: UniqueProgram glProgram; - + + VertexAttributeArrayGL uniforms; VertexAttributeArrayGL vertexAttributes; }; diff --git a/include/mbgl/shaders/shader_program_base.hpp b/include/mbgl/shaders/shader_program_base.hpp index 431a3d78f..a472bdcd9 100644 --- a/include/mbgl/shaders/shader_program_base.hpp +++ b/include/mbgl/shaders/shader_program_base.hpp @@ -14,14 +14,43 @@ protected: ShaderProgramBase() { } ShaderProgramBase(ShaderProgramBase&&) { } + template + bool set(gfx::VertexAttributeArray& attrs, const std::string& name, std::size_t i, T value) { + auto *item = attrs.get(name); + if (item && i < item->getCount()) { + item->set(i, value); + return true; + } + return false; + } + public: virtual ~ShaderProgramBase() noexcept = default; const util::SimpleIdentity& getID() const { return shaderProgramID; } + /// Get the available vertex attributes and their default values + virtual const gfx::VertexAttributeArray& getUniforms() const = 0; + /// Get the available vertex attributes and their default values virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0; + // Set a value if the element is present + template + bool setUniform(const std::string& name, std::size_t i, T value) { + return set(mutableUniforms(), name, i, value); + } + template + bool setAttribute(const std::string& name, std::size_t i, T value) { + return set(mutableVertexAttributes(), name, i, value); + } + + void updateUniforms() { mutableUniforms().applyUniforms(*this); } + +protected: + virtual gfx::VertexAttributeArray& mutableUniforms() = 0; + virtual gfx::VertexAttributeArray& mutableVertexAttributes() = 0; + protected: util::SimpleIdentity shaderProgramID; }; diff --git a/src/mbgl/gfx/vertex_attribute.cpp b/src/mbgl/gfx/vertex_attribute.cpp index 1bf4f81a7..59da28b7c 100644 --- a/src/mbgl/gfx/vertex_attribute.cpp +++ b/src/mbgl/gfx/vertex_attribute.cpp @@ -35,10 +35,10 @@ VertexAttribute* VertexAttributeArray::get(const std::string& name) const { } VertexAttribute* VertexAttributeArray::add(std::string name, int index, - AttributeDataType dataType, std::size_t count) { + AttributeDataType dataType, int size, std::size_t count) { const auto result = attrs.insert(std::make_pair(std::move(name), std::unique_ptr())); if (result.second) { - result.first->second = create(index, dataType, count); + result.first->second = create(index, dataType, size, count); return result.first->second.get(); } else { return nullptr; @@ -46,18 +46,34 @@ VertexAttribute* VertexAttributeArray::add(std::string name, int index, } VertexAttribute* VertexAttributeArray::getOrAdd(std::string name, int index, - AttributeDataType dataType, std::size_t count) { + AttributeDataType dataType, int size, std::size_t count) { //attrs.emplace_back(std::make_unique(dataType, count)); const auto result = attrs.insert(std::make_pair(std::move(name), std::unique_ptr())); if (result.second) { - result.first->second = create(index, dataType, count); + result.first->second = create(index, dataType, size, count); } else if (result.first->second->getDataType() != dataType || + result.first->second->getSize() != size || result.first->second->getCount() != count) { return nullptr; } return result.first->second.get(); } +bool VertexAttributeArray::isDirty() const { + for (const auto& kv : attrs) { + if (kv.second->getDirty()) { + return true; + } + } + return false; +} + +void VertexAttributeArray::resetDirty() { + for (auto& kv : attrs) { + kv.second->clearDirty(); + } +} + void VertexAttributeArray::resolve(const VertexAttributeArray& overrides, ResolveDelegate delegate) const { for (auto& kv : attrs) { diff --git a/src/mbgl/gl/renderer_backend.cpp b/src/mbgl/gl/renderer_backend.cpp index 0235458eb..298729a42 100644 --- a/src/mbgl/gl/renderer_backend.cpp +++ b/src/mbgl/gl/renderer_backend.cpp @@ -70,28 +70,29 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) { gl::Context& glContext = static_cast(*context); +// "layout (location = 0) in vec2 a_pos;\nuniform mat4 u_matrix;\n\nvoid main() {\n + // gl_Position = u_matrix * vec4(a_pos, 0, 1);\n}\n", fragment = "uniform vec4 u_color;\nuniform float u_opacity;\n\nvoid main() {\n fragColor = u_color * u_opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n fragColor = vec4(1.0);\n#endif\n}\n" + auto shader = shaders.get(shaderName); if (!shader) { constexpr auto vert = R"( #version 300 es precision highp float; - uniform float a; - uniform int b; - uniform mat4 c; - layout (location = 0) in vec3 pos; - in float d; - in int e; - in mat2 f; - in mat4 g; + + in vec2 a_pos; + uniform mat4 u_matrix; + void main() { - gl_Position = vec4(d, float(e), f[0][0], a)/1.0e12; // optimized away if unused - gl_Position = vec4(pos, 1.0); + gl_Position = u_matrix * vec4(a_pos, 0, 1); + switch (gl_VertexID) { // fake it for now case 0: gl_Position = vec4(-1.0, -1.0, 1.0, 1.0); break; case 1: gl_Position = vec4( 1.0, -1.0, 1.0, 1.0); break; case 2: gl_Position = vec4( 1.0, 1.0, 1.0, 1.0); break; case 3: gl_Position = vec4(-1.0, 1.0, 1.0, 1.0); break; } + 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 @@ -106,16 +107,14 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) { // Compile shader = gl::ShaderProgramGL::create(glContext, shaderName, vert, frag); if (shader) { + // Set uniforms + //shader->setUniform("a", 0, 3.21f); + //shader->setUniform("b", 0, 4.32f); + // Set default values - if (auto *attr = shader->getVertexAttributes().get("d")) { - attr->set(0, 12.3f); - } - if (auto *attr = shader->getVertexAttributes().get("e")) { - attr->set(0, 123); - } - if (auto *attr = shader->getVertexAttributes().get("f")) { - attr->set(0, gfx::VertexAttribute::matf2{ 1.0f, 2.0f, 3.0f, 4.0f }); - } + //shader->setAttribute("d", 0, 12.3f); + //shader->setAttribute("e", 0, 123); + //shader->setAttribute("f", 0, gfx::VertexAttribute::matf2{ 1.0f, 2.0f, 3.0f, 4.0f }); // Add to the registry if (!shaders.registerShader(shader, shaderName)) { diff --git a/src/mbgl/gl/vertex_attribute_gl.cpp b/src/mbgl/gl/vertex_attribute_gl.cpp index d094cfc0e..e9bbef606 100644 --- a/src/mbgl/gl/vertex_attribute_gl.cpp +++ b/src/mbgl/gl/vertex_attribute_gl.cpp @@ -1,6 +1,8 @@ #include #include +#include +#include #include @@ -69,6 +71,43 @@ 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) { + MBGL_CHECK_ERROR(glUniformMatrix4fv(location, 1, GL_FALSE, &value[0])); + } +}; + +void VertexAttributeArrayGL::applyUniforms(const gfx::ShaderProgramBase& shader) { + const auto& glShader = static_cast(shader); + const auto program = glShader.getGLProgramID(); + + for (auto& kv : attrs) { + const auto& name = kv.first; + auto& uniform = kv.second; + + if (uniform->getDirty()) { + if (uniform->getIndex() < 0) { + const auto index = MBGL_CHECK_ERROR(glGetUniformLocation(program, name.c_str())); + uniform->setIndex(index); + } + std::visit(ApplyUniform { uniform->getIndex() }, uniform->get(0)); + uniform->clearDirty(); + } + } +} + } // namespace gfx } // namespace mbgl diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index 405557f5c..13fd9bd04 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -111,9 +112,16 @@ void Renderer::Impl::render(const RenderTree& renderTree) { tweaker->execute(drawable, parameters); } - // Generate a vertex array object for the drawable state, if necessary auto& drawableGL = static_cast(drawable); - const auto& shader = drawable.getShader(); + auto& shader = drawable.getShader(); + + // Where should this happen? + const mat4 matrix = parameters.matrixForTile({ 0, 0, 0 }); //tileID.toUnwrapped()); + + shader->setUniform("u_matrix", 0, util::convert(matrix)); + shader->updateUniforms(); + + // Generate a vertex array object for the drawable state, if necessary if (shader && !drawableGL.getVertexArray().isValid()) { const auto usage = gfx::BufferUsageType::StaticDraw; diff --git a/src/mbgl/shaders/gl/shader_program_gl.cpp b/src/mbgl/shaders/gl/shader_program_gl.cpp index 8da6b327e..eed421058 100644 --- a/src/mbgl/shaders/gl/shader_program_gl.cpp +++ b/src/mbgl/shaders/gl/shader_program_gl.cpp @@ -19,7 +19,7 @@ static gfx::AttributeDataType mapType(platform::GLenum attrType) { case GL_FLOAT_VEC4: return T::Float4; case GL_FLOAT_MAT2: return T::Float4; // does that work ? case GL_FLOAT_MAT3: - case GL_FLOAT_MAT4: return T::Invalid; + case GL_FLOAT_MAT4: return T::Float4; case GL_INT: return T::Int; case GL_INT_VEC2: return T::Int2; case GL_INT_VEC3: return T::Int3; @@ -52,24 +52,49 @@ static gfx::AttributeDataType mapType(platform::GLenum attrType) { } } +// No `AttributeDataType` for 4x4, so we have to account for it separately... +static int mapCount(const platform::GLenum attrType) { + switch (attrType) { + case GL_FLOAT_MAT3: return 3; + case GL_FLOAT_MAT4: return 4; + default: return 1; + } +} + ShaderProgramGL::ShaderProgramGL(UniqueProgram&& glProgram_) : ShaderProgramBase(), glProgram(std::move(glProgram_)) { } -ShaderProgramGL::ShaderProgramGL(UniqueProgram&& glProgram_, - VertexAttributeArrayGL&& attributes) +ShaderProgramGL::ShaderProgramGL(UniqueProgram&& program, + VertexAttributeArrayGL&& uniforms_, + VertexAttributeArrayGL&& attributes_) : ShaderProgramBase(), - glProgram(std::move(glProgram_)), - vertexAttributes(std::move(attributes)) { + glProgram(std::move(program)), + uniforms(std::move(uniforms_)), + vertexAttributes(std::move(attributes_)) { } ShaderProgramGL::ShaderProgramGL(ShaderProgramGL&& other) : ShaderProgramBase(std::forward(other)), glProgram(std::move(other.glProgram)), + uniforms(std::move(other.uniforms)), vertexAttributes(std::move(other.vertexAttributes)) { } +using namespace platform; + +static void addAttr(VertexAttributeArrayGL& attrs, const char* name, GLint index, + GLsizei length, GLint count, GLenum glType) { + const auto elementType = mapType(glType); + const auto elementCount = mapCount(glType); // number of `elementType`, hopefully temporary + if (elementType != gfx::AttributeDataType::Invalid && length > 0) { + if (auto newAttr = attrs.add(name, index, elementType, elementCount, count)) { + static_cast(newAttr)->setGLType(glType); + } + } +} + std::shared_ptr ShaderProgramGL::create( Context& context, std::string_view /*name*/, @@ -83,14 +108,14 @@ std::shared_ptr ShaderProgramGL::create( auto fragProg = context.createShader(ShaderType::Fragment, std::initializer_list{fragmentSource.data()}); auto program = context.createProgram(vertProg, fragProg, firstAttrib); - using namespace platform; - - // ES3 + // GLES3.1 //GLint numAttribs; //glGetProgramInterfaceiv(program, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &numAttribs); - GLint count; - GLint maxLength; + VertexAttributeArrayGL uniforms; + + GLint count = 0; + GLint maxLength = 0; MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count)); MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength)); @@ -98,12 +123,14 @@ std::shared_ptr ShaderProgramGL::create( for (GLint index = 0; index < count; ++index) { GLsizei length = 0; GLint size = 0; - GLenum type = 0; - MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &length, &size, &type, name.get())); + GLenum glType = 0; + MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &length, &size, &glType, name.get())); + addAttr(uniforms, name.get(), index, length, size, glType); } VertexAttributeArrayGL attrs; + count = 0; MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count)); MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength)); for (GLint index = 0; index < count; ++index) { @@ -111,15 +138,10 @@ std::shared_ptr ShaderProgramGL::create( 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())); - const auto elementType = mapType(glType); - if (elementType != gfx::AttributeDataType::Invalid && length > 0) { - if (auto newAttr = attrs.add(name.get(), index, elementType, size)) { - static_cast(newAttr)->setGLType(glType); - } - } + addAttr(attrs, name.get(), index, length, size, glType); } - return std::make_shared(std::move(program), std::move(attrs)); + return std::make_shared(std::move(program), std::move(uniforms), std::move(attrs)); } } // namespace gl