Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] add IndexedPrimitives/Drawables object to encapsulate primitiv…
Browse files Browse the repository at this point in the history
…e construction
  • Loading branch information
kkaefer committed Jul 11, 2017
1 parent cbdfabb commit 2d36983
Show file tree
Hide file tree
Showing 23 changed files with 370 additions and 94 deletions.
2 changes: 2 additions & 0 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ set(MBGL_CORE_FILES
src/mbgl/renderer/cross_faded_property_evaluator.cpp
src/mbgl/renderer/cross_faded_property_evaluator.hpp
src/mbgl/renderer/data_driven_property_evaluator.hpp
src/mbgl/renderer/drawable.hpp
src/mbgl/renderer/frame_history.cpp
src/mbgl/renderer/frame_history.hpp
src/mbgl/renderer/group_by_layout.cpp
Expand All @@ -171,6 +172,7 @@ set(MBGL_CORE_FILES
src/mbgl/renderer/image_atlas.hpp
src/mbgl/renderer/image_manager.cpp
src/mbgl/renderer/image_manager.hpp
src/mbgl/renderer/indexed_primitives.hpp
src/mbgl/renderer/paint_parameters.hpp
src/mbgl/renderer/paint_property_binder.hpp
src/mbgl/renderer/paint_property_statistics.hpp
Expand Down
1 change: 1 addition & 0 deletions cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(MBGL_TEST_FILES
# gl
test/gl/bucket.test.cpp
test/gl/object.test.cpp
test/gl/primitives.test.cpp

# include/mbgl
test/include/mbgl/test.hpp
Expand Down
26 changes: 26 additions & 0 deletions src/mbgl/gl/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,32 @@ const std::size_t Vertex<A1, A2, A3, A4, A5>::attributeOffsets[5] = {
offsetof(VertexType, a5)
};

template <class A1>
bool operator==(const Vertex<A1>& lhs, const Vertex<A1>& rhs) {
return std::tie(lhs.a1) == std::tie(rhs.a1);
}

template <class A1, class A2>
bool operator==(const Vertex<A1, A2>& lhs, const Vertex<A1, A2>& rhs) {
return std::tie(lhs.a1, lhs.a2) == std::tie(rhs.a1, rhs.a2);
}

template <class A1, class A2, class A3>
bool operator==(const Vertex<A1, A2, A3>& lhs, const Vertex<A1, A2, A3>& rhs) {
return std::tie(lhs.a1, lhs.a2, lhs.a3) == std::tie(rhs.a1, rhs.a2, rhs.a3);
}

template <class A1, class A2, class A3, class A4>
bool operator==(const Vertex<A1, A2, A3, A4>& lhs, const Vertex<A1, A2, A3, A4>& rhs) {
return std::tie(lhs.a1, lhs.a2, lhs.a3, lhs.a4) == std::tie(rhs.a1, rhs.a2, rhs.a3, rhs.a4);
}

template <class A1, class A2, class A3, class A4, class A5>
bool operator==(const Vertex<A1, A2, A3, A4, A5>& lhs, const Vertex<A1, A2, A3, A4, A5>& rhs) {
return std::tie(lhs.a1, lhs.a2, lhs.a3, lhs.a4, lhs.a5) ==
std::tie(rhs.a1, rhs.a2, rhs.a3, rhs.a4, rhs.a5);
}

} // namespace detail

AttributeLocation bindAttributeLocation(ProgramID, AttributeLocation, const char * name);
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/gl/index_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class IndexVector {
bool empty() const { return v.empty(); }
void clear() { v.clear(); }
const uint16_t* data() const { return v.data(); }
const std::vector<uint16_t>& vector() const { return v; }

private:
std::vector<uint16_t> v;
Expand Down
7 changes: 0 additions & 7 deletions src/mbgl/gl/segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ using SegmentInfoVector = std::vector<SegmentInfo>;
template <class Attributes>
class Segment {
public:
Segment(std::size_t vertexOffset,
std::size_t indexOffset,
std::size_t vertexLength = 0,
std::size_t indexLength = 0)
: info(vertexOffset, indexOffset, vertexLength, indexLength) {
}

Segment(SegmentInfo info_)
: info(std::move(info_)) {
}
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/gl/vertex_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class VertexVector {
bool empty() const { return v.empty(); }
void clear() { v.clear(); }
const Vertex* data() const { return v.data(); }
const std::vector<Vertex>& vector() const { return v; }

private:
std::vector<Vertex> v;
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/layout/symbol_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ void SymbolLayout::addSymbol(Buffer& buffer,
}

if (buffer.segments.empty() || buffer.segments.back().info.vertexLength + vertexLength > std::numeric_limits<uint16_t>::max()) {
buffer.segments.emplace_back(buffer.vertices.vertexSize(), buffer.triangles.indexSize());
buffer.segments.emplace_back(gl::SegmentInfo{ buffer.vertices.vertexSize(), buffer.triangles.indexSize() });
}

// We're generating triangle fans, so we always start with the first
Expand Down Expand Up @@ -640,7 +640,7 @@ void SymbolLayout::addToDebugBuffers(CollisionTile& collisionTile, SymbolBucket&
static constexpr std::size_t indexLength = 8;

if (collisionBox.segments.empty() || collisionBox.segments.back().info.vertexLength + vertexLength > std::numeric_limits<uint16_t>::max()) {
collisionBox.segments.emplace_back(collisionBox.vertices.vertexSize(), collisionBox.lines.indexSize());
collisionBox.segments.emplace_back(gl::SegmentInfo{ collisionBox.vertices.vertexSize(), collisionBox.lines.indexSize() });
}

auto& segment = collisionBox.segments.back();
Expand Down
9 changes: 9 additions & 0 deletions src/mbgl/programs/debug_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class DebugProgram : public Program<
{
public:
using Program::Program;

static LayoutVertex layoutVertex(Point<int16_t> p) {
return LayoutVertex {
{{
p.x,
p.y
}}
};
}
};

using DebugLayoutVertex = DebugProgram::LayoutVertex;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/buckets/circle_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void CircleBucket::addFeature(const GeometryTileFeature& feature,

if (segments.empty() || segments.back().info.vertexLength + vertexLength > std::numeric_limits<uint16_t>::max()) {
// Move to a new segments because the old one can't hold the geometry.
segments.emplace_back(vertices.vertexSize(), triangles.indexSize());
segments.emplace_back(gl::SegmentInfo{ vertices.vertexSize(), triangles.indexSize() });
}

// this geometry will be of the Point type, and we'll derive
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/buckets/debug_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ DebugBucket::DebugBucket(const OverscaledTileID& id,
addText(expiresText, 50, baseline + 200, 5);
}

segments.emplace_back(0, 0, vertices.vertexSize(), indices.indexSize());
segments.emplace_back(gl::SegmentInfo{ 0, 0, vertices.vertexSize(), indices.indexSize() });

vertexBuffer = context.createVertexBuffer(std::move(vertices));
indexBuffer = context.createIndexBuffer(std::move(indices));
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/renderer/buckets/fill_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void FillBucket::addFeature(const GeometryTileFeature& feature,
continue;

if (lineSegments.empty() || lineSegments.back().info.vertexLength + nVertices > std::numeric_limits<uint16_t>::max()) {
lineSegments.emplace_back(vertices.vertexSize(), lines.indexSize());
lineSegments.emplace_back(gl::SegmentInfo{ vertices.vertexSize(), lines.indexSize() });
}

auto& lineSegment = lineSegments.back();
Expand All @@ -87,7 +87,7 @@ void FillBucket::addFeature(const GeometryTileFeature& feature,
assert(nIndicies % 3 == 0);

if (triangleSegments.empty() || triangleSegments.back().info.vertexLength + totalVertices > std::numeric_limits<uint16_t>::max()) {
triangleSegments.emplace_back(startVertices, triangles.indexSize());
triangleSegments.emplace_back(gl::SegmentInfo{ startVertices, triangles.indexSize() });
}

auto& triangleSegment = triangleSegments.back();
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/buckets/fill_extrusion_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void FillExtrusionBucket::addFeature(const GeometryTileFeature& feature,
if (triangleSegments.empty() ||
triangleSegments.back().info.vertexLength + (5 * (totalVertices - 1) + 1) >
std::numeric_limits<uint16_t>::max()) {
triangleSegments.emplace_back(startVertices, triangles.indexSize());
triangleSegments.emplace_back(gl::SegmentInfo{ startVertices, triangles.indexSize() });
}

auto& triangleSegment = triangleSegments.back();
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/buckets/line_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void LineBucket::addGeometry(const GeometryCoordinates& coordinates, FeatureType
const std::size_t vertexCount = endVertex - startVertex;

if (segments.empty() || segments.back().info.vertexLength + vertexCount > std::numeric_limits<uint16_t>::max()) {
segments.emplace_back(startVertex, triangles.indexSize());
segments.emplace_back(gl::SegmentInfo{ startVertex, triangles.indexSize() });
}

auto& segment = segments.back();
Expand Down
28 changes: 28 additions & 0 deletions src/mbgl/renderer/drawable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <mbgl/gl/vertex_buffer.hpp>
#include <mbgl/gl/index_buffer.hpp>
#include <mbgl/gl/segment.hpp>

namespace mbgl {

template <class DrawMode, class LayoutVertex, class AttributeLayout>
class IndexedPrimitives;

template <class DrawMode, class LayoutVertex, class Attributes>
class Drawable {
public:
Drawable(gl::Context& context,
IndexedPrimitives<DrawMode, LayoutVertex, Attributes>&& primitives)
: vertices(context.createVertexBuffer(std::move(primitives.vertices))),
indices(context.createIndexBuffer(std::move(primitives.indices))),
segments(primitives.segmentInfo.begin(), primitives.segmentInfo.end()) {
primitives = {};
}

const gl::VertexBuffer<LayoutVertex> vertices;
const gl::IndexBuffer<DrawMode> indices;
const gl::SegmentVector<Attributes> segments;
};

} // namespace mbgl
78 changes: 78 additions & 0 deletions src/mbgl/renderer/indexed_primitives.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include <mbgl/gl/vertex_buffer.hpp>
#include <mbgl/gl/index_buffer.hpp>
#include <mbgl/gl/segment.hpp>

#include <stdexcept>

namespace mbgl {

template <class DrawMode, class LayoutVertex, class AttributeLayout>
class Drawable;

template <class DrawMode, class LayoutVertex, class AttributeLayout>
class IndexedPrimitives {
public:
void add(std::initializer_list<LayoutVertex> v,
std::initializer_list<std::array<uint16_t, DrawMode::bufferGroupSize>> i) {
// Check that all vertices fit into the current segment, if not, create a new one.
if (segmentInfo.empty() || segmentInfo.back().vertexLength + v.size() > std::numeric_limits<uint16_t>::max()) {
segmentInfo.emplace_back(vertices.vertexSize(), indices.indexSize());
}

auto& segment = segmentInfo.back();
const uint16_t offset = segment.vertexLength;

for (const auto& primitive : i) {
for (uint16_t index : primitive) {
// Check that the index references a vertex supplied in this list and that it is not
// out of bounds.
if (index >= v.size()) {
throw std::out_of_range("primitive contains indices outside its vertex group");
}
}

// Insert all indices into the list.
addIndices(primitive, offset, std::make_index_sequence<DrawMode::bufferGroupSize>());
}

// Insert all vertices into the list.
for (const auto& vertex : v) {
vertices.emplace_back(vertex);
}

// Update the current segment statistics.
segment.vertexLength += v.size();
segment.indexLength += i.size() * DrawMode::bufferGroupSize;
}

// Accessors for test suite.
const gl::VertexVector<LayoutVertex>& getVertices() const {
return vertices;
}

const gl::IndexVector<DrawMode>& getIndices() const {
return indices;
}

const gl::SegmentInfoVector& getSegmentInfo() const {
return segmentInfo;
}

private:
// Helper function for expanding primitives.
template <std::size_t N, std::size_t... I>
void addIndices(std::array<uint16_t, N> primitive, const uint16_t offset, std::index_sequence<I...>) {
// Adds the current offset to the indices.
indices.emplace_back((std::get<I>(primitive) + offset)...);
}

private:
friend class Drawable<DrawMode, LayoutVertex, AttributeLayout>;
gl::VertexVector<LayoutVertex> vertices;
gl::IndexVector<DrawMode> indices;
gl::SegmentInfoVector segmentInfo;
};

} // namespace mbgl
Loading

0 comments on commit 2d36983

Please sign in to comment.