summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/drawable.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/gl/drawable.hpp')
-rw-r--r--src/mbgl/gl/drawable.hpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/mbgl/gl/drawable.hpp b/src/mbgl/gl/drawable.hpp
new file mode 100644
index 0000000000..4f2f11e2f1
--- /dev/null
+++ b/src/mbgl/gl/drawable.hpp
@@ -0,0 +1,124 @@
+#pragma once
+
+#include <mbgl/gl/types.hpp>
+#include <mbgl/gl/draw_mode.hpp>
+#include <mbgl/gl/depth_mode.hpp>
+#include <mbgl/gl/stencil_mode.hpp>
+#include <mbgl/gl/color_mode.hpp>
+#include <mbgl/gl/vertex_buffer.hpp>
+#include <mbgl/gl/index_buffer.hpp>
+#include <mbgl/gl/attribute.hpp>
+#include <mbgl/gl/uniform.hpp>
+#include <mbgl/gl/segment.hpp>
+
+#include <cstddef>
+#include <functional>
+#include <limits>
+#include <vector>
+
+namespace mbgl {
+namespace gl {
+
+template <class Vertex>
+class UnindexedVertices {
+public:
+ using VertexType = Vertex;
+
+ UnindexedVertices(DrawMode drawMode_, const VertexBuffer<Vertex>& vertexBuffer_)
+ : drawMode(std::move(drawMode_)),
+ vertexBuffer(vertexBuffer_.buffer),
+ segments({{ 0, 0, vertexBuffer_.vertexCount, 0 }}) {}
+
+ DrawMode drawMode;
+ gl::BufferID vertexBuffer;
+ static constexpr std::size_t vertexSize = sizeof(Vertex);
+ static constexpr gl::BufferID indexBuffer = 0;
+ static constexpr std::size_t primitiveSize = 0;
+ std::vector<Segment> segments;
+};
+
+template <class DrawMode, class Vertex, class...Args>
+auto Unindexed(const VertexBuffer<Vertex>& vertexBuffer,
+ Args&&... drawModeArguments) {
+ return UnindexedVertices<Vertex>(
+ DrawMode { std::forward<Args>(drawModeArguments)... },
+ vertexBuffer);
+}
+
+template <class Vertex, class Primitive>
+class SegmentedVertices {
+public:
+ using VertexType = Vertex;
+
+ SegmentedVertices(DrawMode drawMode_,
+ const VertexBuffer<Vertex>& vertexBuffer_,
+ const IndexBuffer<Primitive>& indexBuffer_,
+ const std::vector<Segment>& segments_)
+ : drawMode(std::move(drawMode_)),
+ vertexBuffer(vertexBuffer_.buffer),
+ indexBuffer(indexBuffer_.buffer),
+ segments(segments_) {}
+
+ DrawMode drawMode;
+ gl::BufferID vertexBuffer;
+ static constexpr std::size_t vertexSize = sizeof(Vertex);
+ gl::BufferID indexBuffer;
+ static constexpr std::size_t primitiveSize = sizeof(Primitive);
+ const std::vector<Segment>& segments;
+};
+
+template <class DrawMode, class Vertex, class Primitive, class...Args>
+auto Segmented(const VertexBuffer<Vertex>& vertexBuffer,
+ const IndexBuffer<Primitive>& indexBuffer,
+ const std::vector<Segment>& segments,
+ Args&&... drawModeArguments) {
+ static_assert(std::is_same<typename Primitive::DrawMode, DrawMode>::value, "primitive mode mismatch");
+ return SegmentedVertices<Vertex, Primitive>(
+ DrawMode { std::forward<Args>(drawModeArguments)... },
+ vertexBuffer,
+ indexBuffer,
+ segments);
+}
+
+class Drawable {
+public:
+ template <class Shader, class Subject>
+ Drawable(DepthMode depthMode_,
+ StencilMode stencilMode_,
+ ColorMode colorMode_,
+ Shader& shader,
+ typename Shader::UniformsType::Values&& uniformValues,
+ const Subject& subject)
+ : drawMode(subject.drawMode),
+ depthMode(std::move(depthMode_)),
+ stencilMode(std::move(stencilMode_)),
+ colorMode(std::move(colorMode_)),
+ program(shader.getID()),
+ vertexBuffer(subject.vertexBuffer),
+ vertexSize(subject.vertexSize),
+ indexBuffer(subject.indexBuffer),
+ primitiveSize(subject.primitiveSize),
+ segments(subject.segments),
+ bindUniforms(Shader::UniformsType::binder(shader.uniformsState, std::move(uniformValues))),
+ attributeBindings(AttributeBindings<Shader, typename Subject::VertexType>()(shader))
+ {
+ static_assert(std::is_standard_layout<typename Subject::VertexType>::value, "vertex type must use standard layout");
+ static_assert(std::is_same<typename Shader::VertexType, typename Subject::VertexType>::value, "vertex type mismatch");
+ }
+
+ DrawMode drawMode;
+ DepthMode depthMode;
+ StencilMode stencilMode;
+ ColorMode colorMode;
+ gl::ProgramID program;
+ gl::BufferID vertexBuffer;
+ std::size_t vertexSize;
+ gl::BufferID indexBuffer;
+ std::size_t primitiveSize;
+ const std::vector<Segment>& segments;
+ std::function<void ()> bindUniforms;
+ std::vector<AttributeBinding> attributeBindings;
+};
+
+} // namespace gl
+} // namespace mbgl