Rework attributes (#1048)

This commit is contained in:
Tim Sylvester
2023-04-20 15:33:27 -07:00
committed by Bart Louwers
parent f76fb98745
commit 92ce58db86
16 changed files with 206 additions and 146 deletions

View File

@@ -22,8 +22,7 @@ using DrawableTweakerPtr = std::shared_ptr<DrawableTweaker>;
class Drawable {
protected:
Drawable() {
}
Drawable() = default;
public:
virtual ~Drawable() = default;
@@ -41,13 +40,15 @@ public:
DrawPriority getDrawPriority() const { return drawPriority; }
void setDrawPriority(DrawPriority value) { drawPriority = value; }
/// Get the number of vertexes
std::size_t getVertexCount() const { return getVertexAttributes().getMaxCount(); }
/// 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;
virtual std::vector<std::uint8_t>& getVertexData() const = 0;
/// Attach a tweaker to be run on this drawable for each frame
void addTweaker(DrawableTweakerPtr tweaker) { tweakers.emplace_back(std::move(tweaker)); }
@@ -55,7 +56,7 @@ public:
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; }
const std::vector<DrawableTweakerPtr>& getTweakers() const { return tweakers; }
protected:
util::SimpleIdentity uniqueID;

View File

@@ -39,9 +39,11 @@ public:
// Can only be created by VertexAttributeArray implementations
protected:
VertexAttribute(int index_, AttributeDataType dataType_, int size_, std::size_t count_)
VertexAttribute(int index_, AttributeDataType dataType_,
int size_, std::size_t count_, std::size_t stride_)
: index(index_),
size(size_),
stride((int)stride_),
dataType(dataType_),
items(count_) {
}
@@ -59,7 +61,9 @@ public:
int getIndex() const { return index; }
void setIndex(int value) { index = value; }
int getSize() const { return size; }
std::size_t getSize() const { return size; }
std::size_t getStride() const { return stride; }
std::size_t getCount() const { return items.size(); }
AttributeDataType getDataType() const { return dataType; }
@@ -69,11 +73,15 @@ public:
template <typename T>
const ElementType& set(std::size_t i, T value) {
dirty = true; // need to rebuild the raw data next time
items.resize(std::max(items.size(), i + 1));
return items[i] = value;
}
bool getDirty() const { return dirty; }
void clearDirty() { dirty = false; }
void clearDirty() {
dirty = false;
rawData.clear();
}
protected:
VertexAttribute& operator=(const VertexAttribute&) = default;
@@ -88,12 +96,14 @@ protected:
protected:
int index;
int size;
int stride;
/// indicates that a value has changed and any cached result should be discarded
mutable bool dirty = true;
AttributeDataType dataType;
std::vector<ElementType> items;
mutable std::vector<std::uint8_t> rawData;
};
/// Stores a collection of vertex attributes by name
@@ -107,8 +117,15 @@ public:
VertexAttributeArray(const VertexAttributeArray&) = delete;
virtual ~VertexAttributeArray() = default;
/// Number of elements
std::size_t size() const { return attrs.size(); }
/// Sum of element strides, and the total size of a vertex in the buffer
std::size_t getTotalSize() const;
/// Get the largest count value of the attribute elements
std::size_t getMaxCount() 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.
@@ -117,12 +134,20 @@ 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, int size, std::size_t count);
VertexAttribute* add(std::string name,
int index = -1,
AttributeDataType = AttributeDataType::Invalid,
int size = 1,
std::size_t count = 1);
/// 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, int size, std::size_t count);
VertexAttribute* getOrAdd(std::string name,
int index = -1,
AttributeDataType = AttributeDataType::Invalid,
int size = 1,
std::size_t count = 1);
// Set a value if the element is present
template <typename T>

View File

@@ -27,22 +27,19 @@ public:
void draw(const PaintParameters &) const override;
void setVertexData(std::vector<std::uint8_t> vertexData, std::vector<uint16_t> indexes,
std::size_t indexOffset, std::size_t indexLength);
void setIndexData(std::vector<uint16_t> indexes,
std::size_t indexOffset = 0,
std::size_t indexLength = 0);
std::vector<std::uint16_t>& getIndexData() const override;
std::vector<std::uint8_t>& getVertexData() const override;
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 gfx::VertexAttributeArray& getVertexAttributes() const override;
void setVertexAttributes(const gfx::VertexAttributeArray& value) override;
void setVertexAttributes(gfx::VertexAttributeArray&& value) override;
gfx::VertexAttributeArray& mutableVertexAttributes();
const gl::VertexArray& getVertexArray() const;
void setVertexArray(gl::VertexArray&&, gfx::UniqueVertexBufferResource&&,
gfx::IndexBuffer&&, std::size_t attrOffset);
void setVertexArray(gl::VertexArray&&, gfx::UniqueVertexBufferResource&&, gfx::IndexBuffer&&);
protected:
class Impl;
@@ -52,7 +49,6 @@ protected:
DrawableGL(std::unique_ptr<Impl>);
private:
VertexAttributeArrayGL vertexAttributes;
};
} // namespace gl

View File

@@ -1,6 +1,7 @@
#pragma once
#include <mbgl/gfx/vertex_attribute.hpp>
#include <mbgl/gl/defines.hpp>
#include <mbgl/platform/gl_functions.hpp>
namespace mbgl {
@@ -13,7 +14,7 @@ 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_, /*stride_=*/0) {
}
VertexAttributeGL(const VertexAttributeGL& other)
: VertexAttribute(other),
@@ -26,19 +27,23 @@ private:
public:
platform::GLenum getGLType() const { return glType; }
void setGLType(platform::GLenum value) { glType = value; }
void setGLType(platform::GLenum 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;
std::size_t getStride() const;
const std::vector<std::uint8_t>& getRaw() const;
private:
static int getSize(platform::GLenum glType);
static int getStride(platform::GLenum glType);
static const void* getPtr(const gfx::VertexAttribute::ElementType&, platform::GLenum glType);
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