Skip to content

Commit

Permalink
Collision drawables shared buffers (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankarschti authored Nov 15, 2023
1 parent c68d8f2 commit 1200046
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 177 deletions.
9 changes: 8 additions & 1 deletion include/mbgl/gfx/vertex_attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class VertexAttribute {
stride(stride_),
dataType(dataType_),
items(count_) {}
VertexAttribute(int index_, AttributeDataType dataType_, std::size_t count_)
: index(index_),
stride(getStrideOf(dataType_)),
dataType(dataType_),
items(count_) {}
VertexAttribute(const VertexAttribute& other)
: index(other.index),
stride(other.stride),
Expand All @@ -105,6 +110,8 @@ class VertexAttribute {
public:
virtual ~VertexAttribute() = default;

static std::size_t getStrideOf(gfx::AttributeDataType);

/// @brief Get the index of the vertex attribute
int getIndex() const { return index; }

Expand Down Expand Up @@ -455,7 +462,7 @@ class VertexAttributeArray {
const UniqueVertexAttribute& add(const StringIdentity id, std::unique_ptr<VertexAttribute>&&);

virtual UniqueVertexAttribute create(int index, AttributeDataType dataType, std::size_t count) const {
return std::make_unique<VertexAttribute>(index, dataType, count, count);
return std::make_unique<VertexAttribute>(index, dataType, count);
}

virtual UniqueVertexAttribute copy(const gfx::VertexAttribute& attr) const {
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/gl/vertex_attribute_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VertexAttributeGL final : public gfx::VertexAttribute {
private:
friend VertexAttributeArrayGL;
VertexAttributeGL(int index_, gfx::AttributeDataType dataType_, std::size_t count_)
: VertexAttribute(index_, dataType_, count_, /*stride_=*/0) {}
: VertexAttribute(index_, dataType_, count_) {}
VertexAttributeGL(const VertexAttributeGL& other)
: VertexAttribute(other),
glType(other.glType) {}
Expand Down
4 changes: 1 addition & 3 deletions include/mbgl/mtl/vertex_attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class VertexAttribute final : public gfx::VertexAttribute {
private:
friend VertexAttributeArray;
VertexAttribute(int index_, gfx::AttributeDataType dataType_, std::size_t count_)
: gfx::VertexAttribute(index_, dataType_, count_, /*stride_=*/0) {}
: gfx::VertexAttribute(index_, dataType_, count_) {}
VertexAttribute(const VertexAttribute& other)
: gfx::VertexAttribute(other) {}
VertexAttribute(VertexAttribute&& other)
Expand All @@ -32,8 +32,6 @@ class VertexAttribute final : public gfx::VertexAttribute {
static const gfx::UniqueVertexBufferResource& getBuffer(gfx::VertexAttribute&,
UploadPass&,
const gfx::BufferUsageType);

static std::size_t getStrideOf(gfx::AttributeDataType);
};

/// Stores a collection of vertex attributes by name
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/shaders/mtl/collision_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct VertexStage {
short2 pos [[attribute(0)]];
short2 anchor_pos [[attribute(1)]];
short2 extrude [[attribute(2)]];
uchar2 placed [[attribute(3)]];
ushort2 placed [[attribute(3)]];
float2 shift [[attribute(4)]];
};
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/shaders/mtl/collision_circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct VertexStage {
short2 pos [[attribute(0)]];
short2 anchor_pos [[attribute(1)]];
short2 extrude [[attribute(2)]];
uchar2 placed [[attribute(3)]];
ushort2 placed [[attribute(3)]];
};
struct FragmentStage {
Expand Down
65 changes: 65 additions & 0 deletions src/mbgl/gfx/vertex_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,71 @@ namespace {
const UniqueVertexAttribute nullref;
} // namespace

std::size_t VertexAttribute::getStrideOf(gfx::AttributeDataType type) {
switch (type) {
case gfx::AttributeDataType::Byte:
return 1;
case gfx::AttributeDataType::Byte2:
return 2;
case gfx::AttributeDataType::Byte3:
return 3;
case gfx::AttributeDataType::Byte4:
return 4;
case gfx::AttributeDataType::UByte:
return 1;
case gfx::AttributeDataType::UByte2:
return 2;
case gfx::AttributeDataType::UByte3:
return 3;
case gfx::AttributeDataType::UByte4:
return 4;
case gfx::AttributeDataType::Short:
return 2;
case gfx::AttributeDataType::Short2:
return 4;
case gfx::AttributeDataType::Short3:
return 6;
case gfx::AttributeDataType::Short4:
return 8;
case gfx::AttributeDataType::UShort:
return 2;
case gfx::AttributeDataType::UShort2:
return 4;
case gfx::AttributeDataType::UShort3:
return 6;
case gfx::AttributeDataType::UShort4:
return 8;
case gfx::AttributeDataType::UShort8:
return 16;
case gfx::AttributeDataType::Int:
return 4;
case gfx::AttributeDataType::Int2:
return 8;
case gfx::AttributeDataType::Int3:
return 12;
case gfx::AttributeDataType::Int4:
return 16;
case gfx::AttributeDataType::UInt:
return 4;
case gfx::AttributeDataType::UInt2:
return 8;
case gfx::AttributeDataType::UInt3:
return 12;
case gfx::AttributeDataType::UInt4:
return 16;
case gfx::AttributeDataType::Float:
return 4;
case gfx::AttributeDataType::Float2:
return 8;
case gfx::AttributeDataType::Float3:
return 12;
case gfx::AttributeDataType::Float4:
return 16;
default:
return 0;
}
}

std::size_t VertexAttribute::getCount() const {
return sharedRawData ? sharedRawData->getRawCount() : items.size();
}
Expand Down
1 change: 0 additions & 1 deletion src/mbgl/mtl/drawable_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void DrawableBuilder::init() {
if (impl->rawVerticesCount) {
auto raw = impl->rawVertices;
drawable.setVertices(std::move(raw), impl->rawVerticesCount, impl->rawVerticesType);
impl->rawVerticesCount = 0;
} else {
const auto& verts = impl->vertices.vector();
constexpr auto vertSize = sizeof(std::remove_reference<decltype(verts)>::type::value_type);
Expand Down
82 changes: 9 additions & 73 deletions src/mbgl/mtl/vertex_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,30 @@
#include <mbgl/mtl/buffer_resource.hpp>
#include <mbgl/mtl/upload_pass.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/convert.hpp>

#include <cstring>
#include <sstream>

namespace mbgl {
namespace mtl {

std::size_t VertexAttribute::getStrideOf(gfx::AttributeDataType type) {
switch (type) {
case gfx::AttributeDataType::Byte:
return 1;
case gfx::AttributeDataType::Byte2:
return 2;
case gfx::AttributeDataType::Byte3:
return 3;
case gfx::AttributeDataType::Byte4:
return 4;
case gfx::AttributeDataType::UByte:
return 1;
case gfx::AttributeDataType::UByte2:
return 2;
case gfx::AttributeDataType::UByte3:
return 3;
case gfx::AttributeDataType::UByte4:
return 4;
case gfx::AttributeDataType::Short:
return 2;
case gfx::AttributeDataType::Short2:
return 4;
case gfx::AttributeDataType::Short3:
return 6;
case gfx::AttributeDataType::Short4:
return 8;
case gfx::AttributeDataType::UShort:
return 2;
case gfx::AttributeDataType::UShort2:
return 4;
case gfx::AttributeDataType::UShort3:
return 6;
case gfx::AttributeDataType::UShort4:
return 8;
case gfx::AttributeDataType::UShort8:
return 16;
case gfx::AttributeDataType::Int:
return 4;
case gfx::AttributeDataType::Int2:
return 8;
case gfx::AttributeDataType::Int3:
return 12;
case gfx::AttributeDataType::Int4:
return 16;
case gfx::AttributeDataType::UInt:
return 4;
case gfx::AttributeDataType::UInt2:
return 8;
case gfx::AttributeDataType::UInt3:
return 12;
case gfx::AttributeDataType::UInt4:
return 16;
case gfx::AttributeDataType::Float:
return 4;
case gfx::AttributeDataType::Float2:
return 8;
case gfx::AttributeDataType::Float3:
return 12;
case gfx::AttributeDataType::Float4:
return 16;
default:
assert(false);
return 0;
}
}

const gfx::UniqueVertexBufferResource& VertexAttribute::getBuffer(gfx::VertexAttribute& attrib_,
UploadPass& uploadPass,
const gfx::BufferUsageType usage) {
if (!attrib_.getBuffer()) {
auto& attrib = static_cast<VertexAttribute&>(attrib_);
if (attrib.sharedRawData) {
return uploadPass.getBuffer(attrib.sharedRawData, usage);
} else if (!attrib.rawData.empty()) {
auto buffer = uploadPass.createVertexBufferResource(
attrib.rawData.data(), attrib.rawData.size(), usage, false);
attrib.setBuffer(std::move(buffer));
attrib.setRawData({});
} else {
// TODO: vertex building
assert(false);
if (!attrib.rawData.empty()) {
auto buffer = uploadPass.createVertexBufferResource(
attrib.rawData.data(), attrib.rawData.size(), usage, false);
attrib.setBuffer(std::move(buffer));
attrib.setRawData({});
} else {
assert(false);
}
}
}
return attrib_.getBuffer();
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/programs/attributes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MBGL_DEFINE_ATTRIBUTE(int16_t, 2, anchor_pos);
MBGL_DEFINE_ATTRIBUTE(uint16_t, 2, texture_pos);
MBGL_DEFINE_ATTRIBUTE(int16_t, 4, normal_ed);
MBGL_DEFINE_ATTRIBUTE(float, 1, fade_opacity);
MBGL_DEFINE_ATTRIBUTE(uint8_t, 2, placed);
MBGL_DEFINE_ATTRIBUTE(uint16_t, 2, placed);
MBGL_DEFINE_ATTRIBUTE(uint16_t, 3, size);
MBGL_DEFINE_ATTRIBUTE(float, 1, offset);
MBGL_DEFINE_ATTRIBUTE(float, 2, shift);
Expand Down
4 changes: 1 addition & 3 deletions src/mbgl/programs/collision_box_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CollisionBoxProgram final
}

static gfx::Vertex<CollisionBoxDynamicAttributes> dynamicVertex(bool placed, bool notUsed, Point<float> shift) {
return {{{static_cast<uint8_t>(placed), static_cast<uint8_t>(notUsed)}}, {{shift.x, shift.y}}};
return {{{static_cast<uint16_t>(placed), static_cast<uint16_t>(notUsed)}}, {{shift.x, shift.y}}};
}

template <class DrawMode>
Expand Down Expand Up @@ -165,6 +165,4 @@ class CollisionCircleProgram final
}
};

using CollisionBoxVertex = CollisionBoxProgram::LayoutVertex;

} // namespace mbgl
Loading

0 comments on commit 1200046

Please sign in to comment.