First cut at Drawable fundamentals (#983)

Co-authored-by: mwilsnd <mwilsnd@gmail.com>
Co-authored-by: Alex Cristici <alex.cristici@gmail.com>
This commit is contained in:
Tim Sylvester
2023-04-17 09:58:57 -07:00
committed by Bart Louwers
parent 42afe4189c
commit 14b53e54ec
60 changed files with 1904 additions and 87 deletions

View File

@@ -0,0 +1,72 @@
#pragma once
#include <mbgl/util/identity.hpp>
#include <mbgl/gfx/vertex_attribute.hpp>
#include <cstdint>
#include <memory>
#include <vector>
namespace mbgl {
class PaintParameters;
namespace gfx {
class DrawableTweaker;
using DrawPriority = int64_t;
using DrawableTweakerPtr = std::shared_ptr<DrawableTweaker>;
class Drawable {
protected:
Drawable() {
}
public:
virtual ~Drawable() = default;
const util::SimpleIdentity& getId() const { return uniqueID; }
/// Draw the drawable
virtual void draw(const PaintParameters &) const = 0;
/// Which shader to use when rendering this drawable
//util::SimpleIdentity getShaderID() const { return shaderID; }
//void setShaderID(util::SimpleIdentity value) { shaderID = value; }
const std::string& getShaderID() const { return shaderID; }
void setShaderID(std::string value) { shaderID = std::move(value); }
/// DrawPriority determines the drawing order
DrawPriority getDrawPriority() const { return drawPriority; }
void setDrawPriority(DrawPriority value) { drawPriority = value; }
/// Get the vertex attributes that override default values in the shader program
virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0;
virtual void setVertexAttributes(const gfx::VertexAttributeArray&) = 0;
virtual void setVertexAttributes(gfx::VertexAttributeArray&&) = 0;
virtual std::vector<std::uint16_t>& getIndexData() const = 0;
/// Attach a tweaker to be run on this drawable for each frame
void addTweaker(DrawableTweakerPtr tweaker) { tweakers.emplace_back(std::move(tweaker)); }
template <typename TIter>
void addTweakers(TIter beg, TIter end) { tweakers.insert(tweakers.end(), beg, end); }
/// Get the tweakers attached to this drawable
std::vector<DrawableTweakerPtr> getTweakers() const { return tweakers; }
protected:
util::SimpleIdentity uniqueID;
//util::SimpleIdentity shaderID = util::SimpleIdentity::Empty;
std::string shaderID;
DrawPriority drawPriority = 0;
std::vector<DrawableTweakerPtr> tweakers;
};
using DrawablePtr = std::shared_ptr<Drawable>;
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,99 @@
#pragma once
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/gfx/vertex_attribute.hpp>
#include <memory>
#include <vector>
namespace mbgl {
class Color;
namespace gfx {
class DrawableTweaker;
using DrawableTweakerPtr = std::shared_ptr<DrawableTweaker>;
/**
Base class for drawable builders, which construct Drawables from primitives
*/
class DrawableBuilder {
protected:
DrawableBuilder();
public:
virtual ~DrawableBuilder();
/// Get the drawable we're currently working on, if any
DrawablePtr getCurrentDrawable(bool createIfNone);
/// Close the current drawable, using a new one for any further work
void flush();
/// Get all the completed drawables
const std::vector<DrawablePtr>& getDrawables() const { return drawables; }
/// Get all the completed drawables and release ownership
std::vector<DrawablePtr> clearDrawables() const { return std::move(drawables); }
/// Get the ID of the drawable we're currently working on, if any
util::SimpleIdentity getDrawableId();
/// Get the draw priority assigned to generated drawables
DrawPriority getDrawPriority() const;
/// Set the draw priority assigned to generated drawables
void setDrawPriority(DrawPriority);
/// Set the draw priority on all drawables including those already generated
void resetDrawPriority(DrawPriority);
/// The color used for emitted vertexes
const Color& getColor() const;
void setColor(const Color& value);
/// Which shader to use when rendering emitted drawables
//util::SimpleIdentity getShaderID() const { return shaderID; }
//void setShaderID(util::SimpleIdentity value) { shaderID = value; }
const std::string& getShaderID() const { return shaderID; }
void setShaderID(std::string value) { shaderID = std::move(value); }
/// Get the vertex attributes that override default values in the shader program
virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0;
/// Add a triangle
void addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
/// Add another triangle based on the previous two points
void appendTriangle(int16_t x0, int16_t y0);
/// Add a rectangle consisting of two triangles
void addQuad(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/// Add a tweaker to be attached to each emitted drawable
void addTweaker(DrawableTweakerPtr tweaker) { tweakers.emplace_back(std::move(tweaker)); }
protected:
/// Create an instance of the appropriate drawable type
virtual DrawablePtr createDrawable() const = 0;
/// Setup the SDK-specific aspects after all the values are present
virtual void init() = 0;
protected:
DrawPriority drawPriority = 0;
//util::SimpleIdentity shaderID = util::SimpleIdentity::Empty;
std::string shaderID;
DrawablePtr currentDrawable;
std::vector<DrawablePtr> drawables;
std::vector<DrawableTweakerPtr> tweakers;
struct Impl;
std::unique_ptr<Impl> impl;
};
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,29 @@
#pragma once
#include <memory>
namespace mbgl {
class PaintParameters;
namespace gfx {
class Drawable;
/**
Base class for drawable tweakers, which manipulate drawables per frame
*/
class DrawableTweaker {
protected:
DrawableTweaker() = default;
public:
virtual ~DrawableTweaker() = default;
virtual void execute(Drawable&, const PaintParameters&) = 0;
};
using DrawableTweakerPtr = std::shared_ptr<DrawableTweaker>;
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,65 @@
#pragma once
#include <cstdint>
namespace mbgl {
namespace gfx {
enum class PrimitiveType : uint8_t {
Point,
Line,
Triangle,
};
enum class DrawModeType : uint8_t {
Points,
Lines,
LineLoop,
LineStrip,
Triangles,
TriangleStrip,
TriangleFan,
};
enum class AttributeDataType : uint8_t {
Byte,
Byte2,
Byte3,
Byte4,
UByte,
UByte2,
UByte3,
UByte4,
Short,
Short2,
Short3,
Short4,
UShort,
UShort2,
UShort3,
UShort4,
Int,
Int2,
Int3,
Int4,
UInt,
UInt2,
UInt3,
UInt4,
Float,
Float2,
Float3,
Float4,
Invalid = 255,
};
} // namespace gfx
} // namespace mbgl

View File

@@ -11,7 +11,7 @@ class Shader;
// Assert that a type is a valid shader for downcasting.
// A valid shader must:
// * Inherit gfx::Shader
// * Declare a public, unique type name (string_view T::Name)
// * Have properly called DECLARE_SHADER_TYPEINFO in the class body
// * Be a final class
template <typename T>
inline constexpr bool is_shader_v = std::is_base_of_v<gfx::Shader, T> &&

View File

@@ -0,0 +1,142 @@
#pragma once
#include <mbgl/gfx/gfx_types.hpp>
#include <array>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>
namespace mbgl {
using mat2 = std::array<double, 2*2>;
using mat3 = std::array<double, 3*3>;
using mat4 = std::array<double, 4*4>;
namespace gfx {
// static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes");
// static_assert(std::is_standard_layout_v<Type>, "vertex type must use standard layout");
// static_assert(I < Descriptor::data.count, "attribute index must be in range");
class VertexAttributeArray;
class VertexAttribute {
public:
using float2 = std::array<float, 2>;
using float3 = std::array<float, 3>;
using matf2 = std::array<float, 2*2>;
using matf3 = std::array<float, 3*3>;
using matf4 = std::array<float, 4*4>;
using int2 = std::array<std::int32_t, 2>;
using int3 = std::array<std::int32_t, 3>;
using int4 = std::array<std::int32_t, 4>;
using ElementType = std::variant<std::int32_t, int2, int3, int4, float, float2, float3, matf2, matf4>;
// Can only be created by VertexAttributeArray implementations
protected:
VertexAttribute(int index_, AttributeDataType dataType_, std::size_t count_)
: index(index_),
dataType(dataType_),
items(count_) {
}
VertexAttribute(const VertexAttribute &) = default;
VertexAttribute(VertexAttribute&& other)
: index(other.index),
dataType(other.dataType),
items(std::move(other.items)) {
}
public:
~VertexAttribute() = default;
int getIndex() const { return index; }
std::size_t getCount() const { return items.size(); }
AttributeDataType getDataType() const { return dataType; }
const ElementType& get(std::size_t i) const { return items[i]; }
template <typename T>
const ElementType& set(std::size_t i, T value) {
dirty = true; // need to rebuild the raw data next time
return items[i] = value;
}
protected:
VertexAttribute& operator=(const VertexAttribute&) = default;
VertexAttribute& operator=(const VertexAttribute&& other) {
index = other.index;
dataType = other.dataType;
items = std::move(other.items);
return *this;
}
protected:
int index;
/// indicates that a value has changed and any cached result should be discarded
mutable bool dirty = true;
AttributeDataType dataType;
std::vector<ElementType> items;
};
/// Stores a collection of vertex attributes by name
class VertexAttributeArray {
public:
using AttributeMap = std::unordered_map<std::string, std::unique_ptr<VertexAttribute>>;
VertexAttributeArray(int initCapacity = 10);
VertexAttributeArray(VertexAttributeArray &&);
VertexAttributeArray(const VertexAttributeArray&);
virtual ~VertexAttributeArray() = default;
std::size_t size() const { return attrs.size(); }
/// 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* get(const std::string& name) const;
/// 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);
/// 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);
using ResolveDelegate = std::function<void(const std::string&, const VertexAttribute&, const VertexAttribute*)>;
/// Call the provided delegate with each value, providing the override if one exists.
void resolve(const VertexAttributeArray& overrides, ResolveDelegate) const;
VertexAttributeArray& operator=(VertexAttributeArray &&);
VertexAttributeArray& operator=(const VertexAttributeArray&);
protected:
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>()));
if (result.second) {
result.first->second = std::move(attr);
return result.first->second.get();
} else {
return nullptr;
}
}
virtual std::unique_ptr<VertexAttribute> create(int index, AttributeDataType dataType, std::size_t count) = 0;
virtual std::unique_ptr<VertexAttribute> copy(const VertexAttribute& attr) = 0;
protected:
AttributeMap attrs;
};
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,57 @@
#pragma once
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/gl/vertex_attribute_gl.hpp>
#include <memory>
namespace mbgl {
namespace gfx {
class IndexBuffer;
class VertexBufferResource;
using UniqueVertexBufferResource = std::unique_ptr<gfx::VertexBufferResource>;
} // namespace gfx
namespace gl {
class VertexArray;
class DrawableGL : public gfx::Drawable {
public:
DrawableGL();
virtual ~DrawableGL();
void draw(const PaintParameters &) const override;
void setVertData(std::vector<std::uint8_t> vertexData_, std::vector<uint16_t> indexes_);
const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; }
void setVertexAttributes(const gfx::VertexAttributeArray& value) override {
vertexAttributes = static_cast<const VertexAttributeArrayGL&>(value);
}
void setVertexAttributes(gfx::VertexAttributeArray&& value) override {
vertexAttributes = std::move(static_cast<VertexAttributeArrayGL&&>(value));
}
const gl::VertexArray& getVertexArray() const;
void setVertexArray(gl::VertexArray&&, gfx::UniqueVertexBufferResource&&, gfx::IndexBuffer&&);
std::vector<std::uint16_t>& getIndexData() const override;
protected:
class Impl;
const std::unique_ptr<Impl> impl;
// For testing only.
DrawableGL(std::unique_ptr<Impl>);
private:
VertexAttributeArrayGL vertexAttributes;
};
} // namespace gl
} // namespace mbgl

View File

@@ -0,0 +1,33 @@
#pragma once
#include <mbgl/gfx/drawable_builder.hpp>
#include <mbgl/gl/vertex_attribute_gl.hpp>
namespace mbgl {
namespace gl {
/**
Base class for OpenGL-specific drawable builders.
*/
class DrawableGLBuilder final : public gfx::DrawableBuilder {
public:
DrawableGLBuilder() {
}
virtual ~DrawableGLBuilder() = default;
const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; }
void setVertexAttributes(const VertexAttributeArrayGL& value) { vertexAttributes = value; }
void setVertexAttributes(VertexAttributeArrayGL&& value) { vertexAttributes = std::move(value); }
protected:
gfx::DrawablePtr createDrawable() const override;
/// Setup the SDK-specific aspects after all the values are present
void init() override;
private:
VertexAttributeArrayGL vertexAttributes;
};
} // namespace gl
} // namespace mbgl

View File

@@ -0,0 +1,27 @@
#pragma once
#include <mbgl/gfx/drawable_tweaker.hpp>
namespace mbgl {
namespace gfx {
class Drawable;
}
namespace gl {
/**
OpenGL-specific drawable tweaker
*/
class DrawableGLTweaker : public gfx::DrawableTweaker {
public:
DrawableGLTweaker() = default;
public:
virtual ~DrawableGLTweaker() = default;
virtual void execute(gfx::Drawable&, const PaintParameters&) override;
};
} // namespace gl
} // namespace mbgl

View File

@@ -0,0 +1,71 @@
#pragma once
#include <mbgl/gfx/vertex_attribute.hpp>
#include <mbgl/platform/gl_functions.hpp>
namespace mbgl {
namespace gl {
class VertexAttributeArrayGL;
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(const VertexAttributeGL& other)
: VertexAttribute(other),
glType(other.glType) {
}
VertexAttributeGL(VertexAttributeGL&& other)
: VertexAttribute(std::move(other)),
glType(other.glType) {
}
public:
platform::GLenum getGLType() const { return glType; }
void setGLType(platform::GLenum value) { glType = value; }
bool getNormalized() const { return normalized; }
void setNormalized(bool value) { normalized = value; }
std::size_t getStride() const { return stride; }
const std::vector<uint8_t>& getRaw() const;
private:
platform::GLenum glType = 0;
bool normalized = false;
mutable int stride = 0;
mutable std::vector<uint8_t> rawData;
};
/// Stores a collection of vertex attributes by name
class VertexAttributeArrayGL final : public gfx::VertexAttributeArray {
public:
VertexAttributeArrayGL(int initCapacity = 10) : VertexAttributeArray(initCapacity) { }
VertexAttributeArrayGL(VertexAttributeArray &&other) : VertexAttributeArray(std::move(other)) { }
VertexAttributeArrayGL(const VertexAttributeArray& other) : VertexAttributeArray(other) { }
~VertexAttributeArrayGL() = default;
VertexAttributeArrayGL& operator=(VertexAttributeArrayGL &&other) {
VertexAttributeArray::operator=(std::move(other));
return *this;
}
VertexAttributeArrayGL& operator=(const VertexAttributeArrayGL& other) {
VertexAttributeArray::operator=(other);
return *this;
}
private:
std::unique_ptr<gfx::VertexAttribute> create(int index, gfx::AttributeDataType dataType, std::size_t count) override {
return std::unique_ptr<gfx::VertexAttribute>(new VertexAttributeGL(index, dataType, count));
}
std::unique_ptr<gfx::VertexAttribute> copy(const gfx::VertexAttribute& attr) override {
return std::unique_ptr<gfx::VertexAttribute>(new VertexAttributeGL(static_cast<const VertexAttributeGL&>(attr)));
}
};
} // namespace gfx
} // namespace mbgl

View File

View File

View File

@@ -0,0 +1,72 @@
#pragma once
#include <mbgl/util/identity.hpp>
#include <memory>
#include <vector>
namespace mbgl {
class RenderOrchestrator;
namespace gfx {
class Drawable;
using DrawablePtr = std::shared_ptr<Drawable>;
} // namespace gfx
/**
Base class for changes to be made to the scene
*/
class ChangeRequest {
protected:
ChangeRequest() = default;
public:
virtual ~ChangeRequest() = default;
virtual void execute(RenderOrchestrator &) = 0;
protected:
};
using ChangeRequestPtr = std::shared_ptr<ChangeRequest>;
using UniqueChangeRequest = std::unique_ptr<ChangeRequest>;
using UniqueChangeRequestVec = std::vector<UniqueChangeRequest>;
/**
Add a new drawable to the scene
*/
class AddDrawableRequest : public ChangeRequest {
public:
AddDrawableRequest(gfx::DrawablePtr drawable_)
: drawable(std::move(drawable_)) {
}
AddDrawableRequest(AddDrawableRequest&& other)
: drawable(std::move(other.drawable)) {
}
void execute(RenderOrchestrator &) override;
protected:
gfx::DrawablePtr drawable;
};
/**
Remove a drawable from the scene
*/
class RemoveDrawableRequest : public ChangeRequest {
public:
RemoveDrawableRequest(util::SimpleIdentity drawableId_) : drawableId(drawableId_) { }
RemoveDrawableRequest(const RemoveDrawableRequest& other) : drawableId(other.drawableId) { }
void execute(RenderOrchestrator &) override;
protected:
util::SimpleIdentity drawableId;
};
} // namespace mbgl

View File

@@ -107,6 +107,11 @@ public:
*/
const std::vector<PlacedSymbolData>& getPlacedSymbolsData() const;
/**
Update the layers
*/
void update(const std::shared_ptr<UpdateParameters>&);
// Memory
void reduceMemoryUse();
void clearData();

View File

@@ -1,5 +1,7 @@
#pragma once
#include <mbgl/gfx/drawable.hpp>
#include <memory>
namespace mbgl {
@@ -25,6 +27,8 @@ public:
/// Coalescing updates is up to the implementer
virtual void update(std::shared_ptr<UpdateParameters>) = 0;
protected:
};
} // namespace mbgl

View File

@@ -0,0 +1,34 @@
#pragma once
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/vertex_attribute_gl.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
namespace mbgl {
namespace gl {
class ShaderProgramGL final : public gfx::ShaderProgramBase {
public:
ShaderProgramGL(UniqueProgram&& glProgram_);
ShaderProgramGL(ShaderProgramGL&& other);
virtual ~ShaderProgramGL() noexcept = default;
static constexpr std::string_view Name{"GenericGLShader"};
const std::string_view typeName() const noexcept override { return Name; }
static std::shared_ptr<ShaderProgramGL> create(
Context&,
std::string_view name,
std::string_view vertexSource,
std::string_view fragmentSource) noexcept(false);
const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; }
protected:
UniqueProgram glProgram;
VertexAttributeArrayGL vertexAttributes;
};
} // namespace gl
} // namespace mbgl

View File

@@ -0,0 +1,26 @@
#pragma once
#include <mbgl/shaders/shader_program_base.hpp>
namespace mbgl {
namespace mtl {
class ShaderProgramMTL : public gfx::ShaderProgramBase {
public:
ShaderProgramMTL() : ShaderProgramBase() { }
virtual ~ShaderProgramMTL() = default;
static constexpr std::string_view Name{"GenericMTLShader"};
const std::string_view typeName() const noexcept override { return Name; }
//bool compile(std::string_view vert, std::string_view frag);
// Load compiled Metal functions from the default library in a bundle
//bool load(NSBundle*, std::string_view vertFuncName, std::string_view fragFuncName);
// Load compiled Metal functions from a specific library
//bool load(id<MTLLibrary>, std::string_view vertFuncName, std::string_view fragFuncName);
protected:
};
} // namespace mtl
} // namespace mbgl

View File

@@ -0,0 +1,30 @@
#pragma once
#include <mbgl/gfx/shader.hpp>
#include <mbgl/gfx/vertex_attribute.hpp>
#include <mbgl/util/identity.hpp>
#include <string>
namespace mbgl {
namespace gfx {
class ShaderProgramBase : public gfx::Shader {
protected:
ShaderProgramBase() { }
ShaderProgramBase(ShaderProgramBase&&) { }
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& getVertexAttributes() const = 0;
protected:
util::SimpleIdentity shaderProgramID;
};
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,51 @@
#pragma once
#include <cstdint>
namespace mbgl {
namespace util {
/**
A simple unique identifier
*/
class SimpleIdentity final {
private:
constexpr SimpleIdentity(std::int64_t id) : uniqueID(id) { }
public:
SimpleIdentity();
SimpleIdentity(const SimpleIdentity&) = default;
~SimpleIdentity() = default;
static const SimpleIdentity Empty;
bool operator<(const SimpleIdentity& other) const { return uniqueID < other.uniqueID; }
bool operator==(const SimpleIdentity& other) const { return uniqueID == other.uniqueID; }
bool operator!=(const SimpleIdentity& other) const { return uniqueID != other.uniqueID; }
operator bool() const { return uniqueID != emptyID; }
bool operator!() const { return uniqueID == emptyID; }
private:
static constexpr std::int64_t emptyID = 0;
std::int64_t uniqueID;
};
/**
Base class for objects that inherit an automatically-assigned unique identity
*/
class SimpleIdentifiable {
protected:
SimpleIdentifiable() = default;
SimpleIdentifiable(const SimpleIdentifiable&) = default;
virtual ~SimpleIdentifiable() = default;
const util::SimpleIdentity& getId() const { return uniqueID; }
protected:
util::SimpleIdentity uniqueID;
};
} // namespace util
} // namespace mbgl