Drawable Renderer (#1570), rework attributes for fill drawables

This commit is contained in:
Tim Sylvester
2023-05-15 15:02:41 -07:00
committed by Bart Louwers
parent cec22b8572
commit fffbfcfbb5
12 changed files with 198 additions and 45 deletions

View File

@@ -69,6 +69,7 @@ public:
void setColor(const Color& value);
enum class ColorMode {
None,
PerDrawable,
PerVertex
};
@@ -89,6 +90,10 @@ public:
/// Set the name given to new drawables
void setDrawableName(std::string value) { drawableName = std::move(value); }
/// The attribute names for automatic attributes (vertex/position and color, if color mode is not `None`)
void setVertexAttrName(std::string value) { vertexAttrName = std::move(value); }
void setColorAttrName(std::string value) { colorAttrName = std::move(value); }
/// Set the matrix applied to new drawables
void setMatrix(mat4 value) { matrix = value; }
@@ -100,8 +105,11 @@ public:
/// Add a rectangle consisting of two triangles
void addQuad(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/// Add already-set-up triangles directly
/// A set of attribute values to be added for each vertex
void setVertexAttributes(const VertexAttributeArray&);
void setVertexAttributes(VertexAttributeArray&&);
/// Add already-set-up triangles directly
void addTriangles(const std::vector<std::array<int16_t, 2>>& vertices,
std::size_t vertexOffset,
std::size_t vertexLength,
@@ -122,6 +130,8 @@ protected:
protected:
std::string name;
std::string drawableName;
std::string vertexAttrName;
std::string colorAttrName;
mbgl::RenderPass renderPass;
DrawPriority drawPriority = 0;
int32_t layerIndex = -1;
@@ -132,6 +142,7 @@ protected:
std::vector<UniqueDrawable> drawables;
std::vector<DrawableTweakerPtr> tweakers;
ColorMode colorMode = ColorMode::PerVertex;
VertexAttributeArray vertexAttrs;
struct Impl;
std::unique_ptr<Impl> impl;

View File

@@ -36,8 +36,6 @@ public:
using ElementType = std::variant<std::int32_t, int2, int3, int4, float, float2, float3, float4, matf3, matf4>;
// Can only be created by VertexAttributeArray implementations
protected:
VertexAttribute(int index_, AttributeDataType dataType_, int size_, std::size_t count_, std::size_t stride_)
: index(index_),
size(size_),
@@ -87,6 +85,14 @@ public:
}
return items[i] = value;
}
template <>
const ElementType& set(std::size_t i, const ElementType& value) {
if (items.size() < i + 1) {
items.resize(std::max(items.size(), i + 1));
}
setDirty();
return items[i] = value;
}
void clear() {
if (!items.empty()) {
@@ -132,7 +138,7 @@ public:
VertexAttributeArray(int initCapacity = 10);
VertexAttributeArray(VertexAttributeArray&&);
// Would need to use the virtual assignment operator
VertexAttributeArray(const VertexAttributeArray&) = delete;
VertexAttributeArray(const VertexAttributeArray&);
virtual ~VertexAttributeArray() = default;
/// Number of elements
@@ -182,6 +188,18 @@ public:
attrs.begin(), attrs.end(), [](const auto& kv) { return kv.second && kv.second->isDirty(); });
}
void clear();
/// Do something with each attribute
void observeAttributes(const std::function<void(const std::string&, VertexAttribute&)>& f) {
std::for_each(attrs.begin(), attrs.end(), [&](const auto& kv) {
if (kv.second) { f(kv.first, *kv.second); }});
}
void observeAttributes(const std::function<void(const std::string&, const VertexAttribute&)>& f) const {
std::for_each(attrs.begin(), attrs.end(), [&](const auto& kv) {
if (kv.second) { f(kv.first, *kv.second); }});
}
using ResolveDelegate =
std::function<void(const std::string&, const VertexAttribute&, const std::unique_ptr<VertexAttribute>&)>;
/// Call the provided delegate with each value, providing the override if one exists.
@@ -190,6 +208,10 @@ public:
VertexAttributeArray& operator=(VertexAttributeArray&&);
VertexAttributeArray& operator=(const VertexAttributeArray&);
virtual std::unique_ptr<VertexAttributeArray> clone() const {
return std::make_unique<VertexAttributeArray>(*this);
}
protected:
const std::unique_ptr<VertexAttribute>& add(std::string name, std::unique_ptr<VertexAttribute>&& attr) {
const auto result = attrs.insert(std::make_pair(std::move(name), std::unique_ptr<VertexAttribute>()));
@@ -204,8 +226,12 @@ protected:
virtual std::unique_ptr<VertexAttribute> create(int index,
AttributeDataType dataType,
int size,
std::size_t count) = 0;
virtual std::unique_ptr<VertexAttribute> copy(const VertexAttribute& attr) = 0;
std::size_t count) const {
return std::make_unique<VertexAttribute>(index, dataType, size, count, size*count);
}
virtual std::unique_ptr<VertexAttribute> copy(const gfx::VertexAttribute& attr) const {
return std::make_unique<VertexAttribute>(attr);
}
protected:
AttributeMap attrs;

View File

@@ -51,7 +51,8 @@ public:
: VertexAttributeArray(initCapacity) {}
VertexAttributeArrayGL(VertexAttributeArrayGL&& other)
: VertexAttributeArray(std::move(other)) {}
VertexAttributeArrayGL(const VertexAttributeArrayGL&) = delete;
VertexAttributeArrayGL(const VertexAttributeArrayGL& other)
: VertexAttributeArray(other) {}
VertexAttributeArrayGL& operator=(VertexAttributeArrayGL&& other) {
VertexAttributeArray::operator=(std::move(other));
@@ -62,14 +63,18 @@ public:
return *this;
}
std::unique_ptr<VertexAttributeArray> clone() const override {
return std::make_unique<VertexAttributeArrayGL>(*this);
}
private:
std::unique_ptr<gfx::VertexAttribute> create(int index,
gfx::AttributeDataType dataType,
int size,
std::size_t count) override {
std::size_t count) const override {
return std::unique_ptr<gfx::VertexAttribute>(new VertexAttributeGL(index, dataType, size, count));
}
std::unique_ptr<gfx::VertexAttribute> copy(const gfx::VertexAttribute& attr) override {
std::unique_ptr<gfx::VertexAttribute> copy(const gfx::VertexAttribute& attr) const override {
return std::unique_ptr<gfx::VertexAttribute>(
new VertexAttributeGL(static_cast<const VertexAttributeGL&>(attr)));
}

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-05-12T17:51:34.322Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-15T21:08:38.312Z by timsylvester using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -11,21 +11,62 @@ template <>
struct ShaderSource<BuiltIn::FillShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillShader";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
layout (std140) uniform DrawableUBO {
mat4 u_matrix;
};
#ifndef HAS_UNIFORM_u_color
uniform lowp float u_color_t;
layout (location = 1) in highp vec4 a_color;
out highp vec4 color;
#else
uniform highp vec4 u_color;
#endif
#ifndef HAS_UNIFORM_u_opacity
uniform lowp float u_opacity_t;
layout (location = 2) in lowp vec2 a_opacity;
out lowp float opacity;
#else
uniform lowp float u_opacity;
#endif
void main() {
#ifndef HAS_UNIFORM_u_color
color = unpack_mix_color(a_color, u_color_t);
#else
highp vec4 color = u_color;
#endif
#ifndef HAS_UNIFORM_u_opacity
opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
#else
lowp float opacity = u_opacity;
#endif
gl_Position = u_matrix * vec4(a_pos, 0, 1);
}
)";
static constexpr const char* fragment = R"(layout (std140) uniform BackgroundLayerUBO {
vec4 u_color;
float u_opacity;
};
static constexpr const char* fragment = R"(#ifndef HAS_UNIFORM_u_color
in highp vec4 color;
#else
uniform highp vec4 u_color;
#endif
#ifndef HAS_UNIFORM_u_opacity
in lowp float opacity;
#else
uniform lowp float u_opacity;
#endif
void main() {
fragColor = u_color * u_opacity;
#ifdef HAS_UNIFORM_u_color
highp vec4 color = u_color;
#endif
#ifdef HAS_UNIFORM_u_opacity
lowp float opacity = u_opacity;
#endif
fragColor = color * opacity;
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif