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

Commit

Permalink
[core] Use indexed rendering everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Dec 1, 2016
1 parent b1068b4 commit 54c557d
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 110 deletions.
17 changes: 5 additions & 12 deletions src/mbgl/gl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,18 +518,11 @@ void Context::draw(const Drawable& drawable) {
drawable.bindAttributes(segment.vertexOffset);
}

if (drawable.indexBuffer) {
MBGL_CHECK_ERROR(glDrawElements(
static_cast<GLenum>(primitiveType),
static_cast<GLsizei>(segment.indexLength),
GL_UNSIGNED_SHORT,
reinterpret_cast<GLvoid*>(sizeof(uint16_t) * segment.indexOffset)));
} else {
MBGL_CHECK_ERROR(glDrawArrays(
static_cast<GLenum>(primitiveType),
static_cast<GLint>(segment.vertexOffset),
static_cast<GLsizei>(segment.vertexLength)));
}
MBGL_CHECK_ERROR(glDrawElements(
static_cast<GLenum>(primitiveType),
static_cast<GLsizei>(segment.indexLength),
GL_UNSIGNED_SHORT,
reinterpret_cast<GLvoid*>(sizeof(uint16_t) * segment.indexOffset)));
}
}

Expand Down
26 changes: 0 additions & 26 deletions src/mbgl/gl/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Program {
attributesState(Attributes::state(program)),
uniformsState((context.linkProgram(program), Uniforms::state(program))) {}

// Indexed drawing.
template <class DrawMode>
void draw(Context& context,
DrawMode drawMode,
Expand All @@ -58,31 +57,6 @@ class Program {
});
}

// Unindexed drawing.
template <class DrawMode>
void draw(Context& context,
DrawMode drawMode,
DepthMode depthMode,
StencilMode stencilMode,
ColorMode colorMode,
UniformValues&& uniformValues,
const VertexBuffer<Vertex, DrawMode>& vertexBuffer,
const SegmentVector<Attributes>& segments) {
static_assert(std::is_same<Primitive, typename DrawMode::Primitive>::value, "incompatible draw mode");
context.draw({
std::move(drawMode),
std::move(depthMode),
std::move(stencilMode),
std::move(colorMode),
program,
vertexBuffer.buffer,
0,
segments,
Uniforms::binder(uniformsState, std::move(uniformValues)),
Attributes::binder(attributesState)
});
}

private:
UniqueShader vertexShader;
UniqueShader fragmentShader;
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 @@ -33,13 +33,6 @@ template <class Attributes>
class SegmentVector : public std::vector<Segment> {
public:
SegmentVector() = default;

// This constructor is for unindexed rendering. It creates a SegmentVector with a
// single segment having 0 indexes.
template <class DrawMode>
SegmentVector(const VertexBuffer<typename Attributes::Vertex, DrawMode>& buffer) {
emplace_back(0, 0, buffer.vertexCount, 0);
}
};

} // namespace gl
Expand Down
48 changes: 22 additions & 26 deletions src/mbgl/renderer/debug_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@

namespace mbgl {

gl::VertexVector<FillVertex, gl::Lines>
buildTextVertices(const OverscaledTileID& id,
const bool renderable,
const bool complete,
optional<Timestamp> modified,
optional<Timestamp> expires,
MapDebugOptions debugMode) {
gl::VertexVector<FillVertex, gl::Lines> textLines;
DebugBucket::DebugBucket(const OverscaledTileID& id,
const bool renderable_,
const bool complete_,
optional<Timestamp> modified_,
optional<Timestamp> expires_,
MapDebugOptions debugMode_,
gl::Context& context)
: renderable(renderable_),
complete(complete_),
modified(std::move(modified_)),
expires(std::move(expires_)),
debugMode(debugMode_) {

gl::VertexVector<FillVertex> vertices;
gl::IndexVector<gl::Lines> indices;

auto addText = [&] (const std::string& text, double left, double baseline, double scale) {
for (uint8_t c : text) {
Expand All @@ -36,9 +43,11 @@ buildTextVertices(const OverscaledTileID& id,
int16_t(::round(baseline - glyph.data[j + 1] * scale))
};

vertices.emplace_back(FillAttributes::vertex(p));

if (prev) {
textLines.emplace_back(FillAttributes::vertex(*prev),
FillAttributes::vertex(p));
indices.emplace_back(vertices.vertexSize() - 1,
vertices.vertexSize());
}

prev = p;
Expand All @@ -65,23 +74,10 @@ buildTextVertices(const OverscaledTileID& id,
addText(expiresText, 50, baseline + 200, 5);
}

return textLines;
}
segments.emplace_back(0, 0, vertices.vertexSize(), indices.indexSize());

DebugBucket::DebugBucket(const OverscaledTileID& id,
const bool renderable_,
const bool complete_,
optional<Timestamp> modified_,
optional<Timestamp> expires_,
MapDebugOptions debugMode_,
gl::Context& context)
: renderable(renderable_),
complete(complete_),
modified(std::move(modified_)),
expires(std::move(expires_)),
debugMode(debugMode_),
vertexBuffer(context.createVertexBuffer(buildTextVertices(id, renderable_, complete_, modified_, expires_, debugMode_))),
segments(vertexBuffer) {
vertexBuffer = context.createVertexBuffer(std::move(vertices));
indexBuffer = context.createIndexBuffer(std::move(indices));
}

} // namespace mbgl
4 changes: 3 additions & 1 deletion src/mbgl/renderer/debug_bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/util/optional.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/gl/vertex_buffer.hpp>
#include <mbgl/gl/index_buffer.hpp>
#include <mbgl/programs/debug_program.hpp>

namespace mbgl {
Expand All @@ -32,8 +33,9 @@ class DebugBucket : private util::noncopyable {
const optional<Timestamp> expires;
const MapDebugOptions debugMode;

gl::VertexBuffer<DebugVertex, gl::Lines> vertexBuffer;
gl::SegmentVector<DebugAttributes> segments;
optional<gl::VertexBuffer<DebugVertex>> vertexBuffer;
optional<gl::IndexBuffer<gl::Lines>> indexBuffer;
};

} // namespace mbgl
56 changes: 31 additions & 25 deletions src/mbgl/renderer/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,34 @@ namespace mbgl {

using namespace style;

static gl::VertexVector<FillVertex, gl::Triangles> tileTriangles() {
gl::VertexVector<FillVertex, gl::Triangles> result;
result.emplace_back(
FillAttributes::vertex({ 0, 0 }),
FillAttributes::vertex({ util::EXTENT, 0 }),
FillAttributes::vertex({ 0, util::EXTENT }));
result.emplace_back(
FillAttributes::vertex({ util::EXTENT, 0 }),
FillAttributes::vertex({ 0, util::EXTENT }),
FillAttributes::vertex({ util::EXTENT, util::EXTENT }));
static gl::VertexVector<FillVertex> tileVertices() {
gl::VertexVector<FillVertex> result;
result.emplace_back(FillAttributes::vertex({ 0, 0 }));
result.emplace_back(FillAttributes::vertex({ util::EXTENT, 0 }));
result.emplace_back(FillAttributes::vertex({ 0, util::EXTENT }));
result.emplace_back(FillAttributes::vertex({ util::EXTENT, util::EXTENT }));
return result;
}

static gl::VertexVector<FillVertex, gl::LineStrip> tileLineStrip() {
gl::VertexVector<FillVertex, gl::LineStrip> result;
result.emplace_back(FillAttributes::vertex({ 0, 0 }));
result.emplace_back(FillAttributes::vertex({ util::EXTENT, 0 }));
result.emplace_back(FillAttributes::vertex({ util::EXTENT, util::EXTENT }));
result.emplace_back(FillAttributes::vertex({ 0, util::EXTENT }));
result.emplace_back(FillAttributes::vertex({ 0, 0 }));
static gl::IndexVector<gl::Triangles> tileTriangleIndices() {
gl::IndexVector<gl::Triangles> result;
result.emplace_back(0, 1, 2);
result.emplace_back(1, 2, 3);
return result;
}

static gl::VertexVector<RasterVertex, gl::TriangleStrip> rasterTriangleStrip() {
gl::VertexVector<RasterVertex, gl::TriangleStrip> result;
static gl::IndexVector<gl::LineStrip> tileLineStripIndices() {
gl::IndexVector<gl::LineStrip> result;
result.emplace_back(0);
result.emplace_back(1);
result.emplace_back(3);
result.emplace_back(2);
result.emplace_back(0);
return result;
}

static gl::VertexVector<RasterVertex> rasterVertices() {
gl::VertexVector<RasterVertex> result;
result.emplace_back(RasterProgram::vertex({ 0, 0 }, { 0, 0 }));
result.emplace_back(RasterProgram::vertex({ util::EXTENT, 0 }, { 32767, 0 }));
result.emplace_back(RasterProgram::vertex({ 0, util::EXTENT }, { 0, 32767 }));
Expand All @@ -77,12 +80,15 @@ static gl::VertexVector<RasterVertex, gl::TriangleStrip> rasterTriangleStrip() {
Painter::Painter(gl::Context& context_, const TransformState& state_, float pixelRatio)
: context(context_),
state(state_),
tileTriangleVertexBuffer(context.createVertexBuffer(tileTriangles())),
tileTriangleSegments(tileTriangleVertexBuffer),
tileBorderVertexBuffer(context.createVertexBuffer(tileLineStrip())),
tileBorderSegments(tileBorderVertexBuffer),
rasterVertexBuffer(context.createVertexBuffer(rasterTriangleStrip())),
rasterSegments(rasterVertexBuffer) {
tileVertexBuffer(context.createVertexBuffer(tileVertices())),
rasterVertexBuffer(context.createVertexBuffer(rasterVertices())),
tileTriangleIndexBuffer(context.createIndexBuffer(tileTriangleIndices())),
tileBorderIndexBuffer(context.createIndexBuffer(tileLineStripIndices())) {

tileTriangleSegments.emplace_back(0, 0, 4, 6);
tileBorderSegments.emplace_back(0, 0, 4, 5);
rasterSegments.emplace_back(0, 0, 4, 6);

#ifndef NDEBUG
gl::debugging::enable();
#endif
Expand Down
11 changes: 6 additions & 5 deletions src/mbgl/renderer/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,14 @@ class Painter : private util::noncopyable {
std::unique_ptr<Programs> overdrawPrograms;
#endif

gl::VertexBuffer<FillVertex, gl::Triangles> tileTriangleVertexBuffer;
gl::SegmentVector<FillAttributes> tileTriangleSegments;
gl::VertexBuffer<FillVertex> tileVertexBuffer;
gl::VertexBuffer<RasterVertex> rasterVertexBuffer;

gl::VertexBuffer<DebugVertex, gl::LineStrip> tileBorderVertexBuffer;
gl::SegmentVector<DebugAttributes> tileBorderSegments;
gl::IndexBuffer<gl::Triangles> tileTriangleIndexBuffer;
gl::IndexBuffer<gl::LineStrip> tileBorderIndexBuffer;

gl::VertexBuffer<RasterVertex, gl::TriangleStrip> rasterVertexBuffer;
gl::SegmentVector<FillAttributes> tileTriangleSegments;
gl::SegmentVector<DebugAttributes> tileBorderSegments;
gl::SegmentVector<RasterAttributes> rasterSegments;
};

Expand Down
6 changes: 4 additions & 2 deletions src/mbgl/renderer/painter_background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void Painter::renderBackground(PaintParameters& parameters, const BackgroundLaye
tileID,
state
),
tileTriangleVertexBuffer,
tileVertexBuffer,
tileTriangleIndexBuffer,
tileTriangleSegments
);
}
Expand All @@ -63,7 +64,8 @@ void Painter::renderBackground(PaintParameters& parameters, const BackgroundLaye
uniforms::u_outline_color::Value{ properties.get<BackgroundColor>() },
uniforms::u_world::Value{ context.viewport.getCurrentValue().size },
},
tileTriangleVertexBuffer,
tileVertexBuffer,
tileTriangleIndexBuffer,
tileTriangleSegments
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/painter_clipping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void Painter::renderClippingMask(const UnwrappedTileID& tileID, const ClipID& cl
uniforms::u_outline_color::Value{ Color {} },
uniforms::u_world::Value{ context.viewport.getCurrentValue().size },
},
tileTriangleVertexBuffer,
tileVertexBuffer,
tileTriangleIndexBuffer,
tileTriangleSegments
);
}
Expand Down
12 changes: 8 additions & 4 deletions src/mbgl/renderer/painter_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Painter::renderTileDebug(const RenderTile& renderTile) {

MBGL_DEBUG_GROUP(std::string { "debug " } + util::toString(renderTile.id));

auto draw = [&] (Color color, const auto& vertexBuffer, const auto& segments, auto drawMode) {
auto draw = [&] (Color color, const auto& vertexBuffer, const auto& indexBuffer, const auto& segments, auto drawMode) {
programs->debug.draw(
context,
drawMode,
Expand All @@ -30,6 +30,7 @@ void Painter::renderTileDebug(const RenderTile& renderTile) {
uniforms::u_color::Value{ color }
},
vertexBuffer,
indexBuffer,
segments
);
};
Expand All @@ -47,19 +48,22 @@ void Painter::renderTileDebug(const RenderTile& renderTile) {
}

draw(Color::white(),
tile.debugBucket->vertexBuffer,
*tile.debugBucket->vertexBuffer,
*tile.debugBucket->indexBuffer,
tile.debugBucket->segments,
gl::Lines { 4.0f * frame.pixelRatio });

draw(Color::black(),
tile.debugBucket->vertexBuffer,
*tile.debugBucket->vertexBuffer,
*tile.debugBucket->indexBuffer,
tile.debugBucket->segments,
gl::Lines { 2.0f * frame.pixelRatio });
}

if (frame.debugOptions & MapDebugOptions::TileBorders) {
draw(Color::red(),
tileBorderVertexBuffer,
tileVertexBuffer,
tileBorderIndexBuffer,
tileBorderSegments,
gl::LineStrip { 4.0f * frame.pixelRatio });
}
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/painter_raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Painter::renderRaster(PaintParameters& parameters,

parameters.programs.raster.draw(
context,
gl::TriangleStrip(),
gl::Triangles(),
depthModeForSublayer(0, gl::DepthMode::ReadOnly),
gl::StencilMode::disabled(),
colorModeForRenderPass(),
Expand All @@ -76,6 +76,7 @@ void Painter::renderRaster(PaintParameters& parameters,
uniforms::u_tl_parent::Value{ std::array<float, 2> {{ 0.0f, 0.0f }} },
},
rasterVertexBuffer,
tileTriangleIndexBuffer,
rasterSegments
);
}
Expand Down

0 comments on commit 54c557d

Please sign in to comment.