This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] add IndexedPrimitives/Drawables object to encapsulate primitiv…
…e construction
- Loading branch information
Showing
23 changed files
with
370 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.