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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
karimnaaji committed Jun 4, 2020
1 parent 6bd46c1 commit 95f3a98
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 52 deletions.
34 changes: 34 additions & 0 deletions platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/style/conversion/color_ramp_property_value.hpp>
#include <mbgl/style/conversion/json.hpp>

#include <mapbox/cheap_ruler.hpp>
#include <mapbox/geometry.hpp>
Expand Down Expand Up @@ -408,6 +410,38 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
case GLFW_KEY_T:
view->toggleCustomSource();
break;
case GLFW_KEY_V: {
using namespace mbgl;
using namespace mbgl::style;
auto &style = view->map->getStyle();
if (!style.getSource("line-gradient-source")) {
std::string url = "https://gist.githubusercontent.com/karimnaaji/0ea3016a825a4c2883fce309183f0c20/raw/9f67d55c3c797b8b1183dc27a26ec705ac619198/polylines.geojson";
GeoJSONOptions options;
options.lineMetrics = true;
auto source = std::make_unique<GeoJSONSource>("line-gradient-source", Immutable<GeoJSONOptions>(makeMutable<GeoJSONOptions>(options)));
source->setURL(url);
style.addSource(std::move(source));

mbgl::CameraOptions cameraOptions;
cameraOptions.center = mbgl::LatLng{38.888, -77.01866};
cameraOptions.zoom = 12.5;
cameraOptions.pitch = 0;
cameraOptions.bearing = 0;
view->map->jumpTo(cameraOptions);
}

if (!style.getLayer("line-gradient")) {
auto lineLayer = std::make_unique<LineLayer>("line-gradient", "line-gradient-source");
lineLayer->setLineWidth(PropertyValue<float>(14.0f));
std::string rawValue = R"JSON(["step",["line-progress"],"rgba(0, 0, 255, 0.1)",0.25,"red",0.6,"yellow"])JSON";
//std::string rawValue = R"JSON(["interpolate",["linear"],["line-progress"],0,"rgba(0, 0, 255, 0)",0.1,"royalblue",0.3,"cyan",0.5,"lime",0.7,"yellow",1,"red"])JSON";
conversion::Error error;
auto ramp = conversion::convertJSON<ColorRampPropertyValue>(rawValue, error);
lineLayer->setLineGradient(ramp.value());
style.addLayer(std::move(lineLayer));
}
}
break;
case GLFW_KEY_F: {
using namespace mbgl;
using namespace mbgl::style;
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Context {
static constexpr const uint32_t minimumRequiredVertexBindingCount = 8;
const uint32_t maximumVertexBindingCount;
bool supportsHalfFloatTextures = false;
static int32_t maxTextureSize;

public:
Context(Context&&) = delete;
Expand Down
9 changes: 8 additions & 1 deletion src/mbgl/gl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#include <cstring>

namespace mbgl {

namespace gfx {
int32_t Context::maxTextureSize;
} // namespace gfx

namespace gl {

using namespace platform;
Expand Down Expand Up @@ -65,7 +70,7 @@ Context::~Context() {
}
}

void Context::initializeExtensions(const std::function<gl::ProcAddress(const char*)>& getProcAddress) {
void Context::initialize(const std::function<gl::ProcAddress(const char*)>& getProcAddress) {
if (const auto* extensions =
reinterpret_cast<const char*>(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS)))) {

Expand Down Expand Up @@ -123,6 +128,8 @@ void Context::initializeExtensions(const std::function<gl::ProcAddress(const cha
Log::Warning(Event::OpenGL, "Not using Vertex Array Objects");
}
}

MBGL_CHECK_ERROR(glGetIntegerv(GL_MAX_TEXTURE_SIZE, &Context::maxTextureSize));
}

void Context::enableDebugging() {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/gl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Context final : public gfx::Context {
gfx::RenderingStats& renderingStats();
const gfx::RenderingStats& renderingStats() const override;

void initializeExtensions(const std::function<gl::ProcAddress(const char*)>&);
void initialize(const std::function<gl::ProcAddress(const char*)>&);

void enableDebugging();

Expand Down
1 change: 1 addition & 0 deletions src/mbgl/gl/defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
#define GL_VERTEX_SHADER 0x8B31
#define GL_VIEWPORT 0x0BA2
#define GL_ZERO 0
#define GL_MAX_TEXTURE_SIZE 0x0D33
#ifdef MBGL_USE_GLES2
#define GL_HALF_FLOAT 0x8D61
#else
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/gl/renderer_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RendererBackend::RendererBackend(const gfx::ContextMode contextMode_)
std::unique_ptr<gfx::Context> RendererBackend::createContext() {
auto result = std::make_unique<gl::Context>(*this);
result->enableDebugging();
result->initializeExtensions(
result->initialize(
std::bind(&RendererBackend::getExtensionFunctionPointer, this, std::placeholders::_1));
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/mbgl/programs/line_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ LineGradientProgram::LayoutUniformValues LineGradientProgram::layoutUniformValue
const RenderTile& tile,
const TransformState& state,
const std::array<float, 2>& pixelsToGLUnits,
const float pixelRatio) {
const float pixelRatio,
const float imageHeight) {
return makeValues<LineGradientProgram::LayoutUniformValues>(
properties,
tile,
state,
pixelsToGLUnits,
pixelRatio
pixelRatio,
imageHeight
);
}

Expand Down
30 changes: 28 additions & 2 deletions src/mbgl/programs/line_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ MBGL_DEFINE_UNIFORM_SCALAR(float, sdfgamma);
MBGL_DEFINE_UNIFORM_VECTOR(float, 2, patternscale_a);
MBGL_DEFINE_UNIFORM_VECTOR(float, 2, patternscale_b);
MBGL_DEFINE_UNIFORM_VECTOR(float, 2, units_to_pixels);
MBGL_DEFINE_UNIFORM_SCALAR(float, image_height);
} // namespace uniforms

namespace attributes {
MBGL_DEFINE_ATTRIBUTE(float, 1, line_progress);
MBGL_DEFINE_ATTRIBUTE(float, 1, line_clip);
MBGL_DEFINE_ATTRIBUTE(uint32_t, 1, split_index);
} // namespace attributes

using LineLayoutAttributes = TypeList<
attributes::pos_normal,
attributes::data<uint8_t, 4>>;

using LineLayoutAttributesExt = TypeList<
attributes::line_progress,
attributes::line_clip,
attributes::split_index>;

class LineProgram : public Program<
LineProgram,
gfx::PrimitiveType::Triangle,
Expand Down Expand Up @@ -165,7 +177,8 @@ class LineGradientProgram : public Program<
uniforms::matrix,
uniforms::ratio,
uniforms::units_to_pixels,
uniforms::device_pixel_ratio>,
uniforms::device_pixel_ratio,
uniforms::image_height>,
TypeList<
textures::image>,
style::LinePaintProperties>
Expand All @@ -177,11 +190,24 @@ class LineGradientProgram : public Program<
const RenderTile&,
const TransformState&,
const std::array<float, 2>& pixelsToGLUnits,
float pixelRatio);
float pixelRatio,
float imageHeight);

static AttributeBindings computeAllAttributeBindings(
const gfx::VertexBuffer<LineProgram::LayoutVertex>& layoutVertexBuffer,
const gfx::VertexBuffer<gfx::Vertex<LineLayoutAttributesExt>>& layoutVertexBufferExt,
const Binders& paintPropertyBinders,
const typename PaintProperties::PossiblyEvaluated& currentProperties) {
(void)layoutVertexBufferExt;
return gfx::AttributeBindings<LineLayoutAttributes>(layoutVertexBuffer)
//.concat(gfx::AttributeBindings<LineLayoutAttributesExt>(layoutVertexBufferExt))
.concat(paintPropertyBinders.attributeBindings(currentProperties));
}
};

using LineLayoutVertex = LineProgram::LayoutVertex;
using LineAttributes = LineProgram::AttributeList;
using LineLayoutVertexExt = gfx::Vertex<LineLayoutAttributesExt>;

class LineLayerPrograms final : public LayerTypePrograms {
public:
Expand Down
33 changes: 31 additions & 2 deletions src/mbgl/renderer/buckets/line_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LineBucket::LineBucket(LineBucket::PossiblyEvaluatedLayoutProperties layout_,
const std::map<std::string, Immutable<LayerProperties>>& layerPaintProperties,
const float zoom_,
const uint32_t overscaling_)
: layout(std::move(layout_)), zoom(zoom_), overscaling(overscaling_) {
: layout(std::move(layout_)), gradientVersion(0), maxLineLength(0.0), zoom(zoom_), overscaling(overscaling_) {
for (const auto& pair : layerPaintProperties) {
paintPropertyBinders.emplace(
std::piecewise_construct,
Expand All @@ -34,6 +34,7 @@ void LineBucket::addFeature(const GeometryTileFeature& feature,
const PatternLayerMap& patternDependencies,
std::size_t index,
const CanonicalTileID& canonical) {
lineClips.reserve(geometryCollection.size());
for (auto& line : geometryCollection) {
addGeometry(line, feature, canonical);
}
Expand Down Expand Up @@ -92,7 +93,15 @@ class LineBucket::Distances {
return (relativeTileDistance * (clipEnd - clipStart) + clipStart) * (MAX_LINE_DISTANCE - 1);
}

private:
double lineDistance(double tileDistance) const {
double relativeTileDistance = tileDistance / total;
if (std::isinf(relativeTileDistance) || std::isnan(relativeTileDistance)) {
assert(false);
relativeTileDistance = 0.0;
}
return relativeTileDistance * (clipEnd - clipStart) + clipStart;
}

double clipStart;
double clipEnd;
double total;
Expand Down Expand Up @@ -136,8 +145,10 @@ void LineBucket::addGeometry(const GeometryCoordinates& coordinates,
total_length += util::dist<double>(coordinates[i], coordinates[i + 1]);
}

maxLineLength = std::max<double>(maxLineLength, total_length);
lineDistances = Distances{
*numericValue<double>(clip_start_it->second), *numericValue<double>(clip_end_it->second), total_length};
lineClips.push_back(LineClip{ lineDistances->clipStart, lineDistances->clipEnd });
}

const LineJoinType joinType = layout.evaluate<LineJoin>(zoom, feature, canonical);
Expand Down Expand Up @@ -466,6 +477,13 @@ void LineBucket::addCurrentVertex(const GeometryCoordinate& currentCoordinate,
if (endLeft)
extrude = extrude - (util::perp(normal) * endLeft);
vertices.emplace_back(LineProgram::layoutVertex(currentCoordinate, extrude, round, false, endLeft, scaledDistance * LINE_DISTANCE_SCALE));
if (lineDistances) {
vertices2.emplace_back(LineLayoutVertexExt {
{ static_cast<float>(lineDistances->lineDistance(distance) - lineDistances->clipStart) },
{ static_cast<float>(lineDistances->clipEnd - lineDistances->clipStart) },
{ static_cast<uint32_t>(lineClips.size()) }
});
}
e3 = vertices.elements() - 1 - startVertex;
if (e1 >= 0 && e2 >= 0) {
triangleStore.emplace_back(e1, e2, e3);
Expand All @@ -477,6 +495,13 @@ void LineBucket::addCurrentVertex(const GeometryCoordinate& currentCoordinate,
if (endRight)
extrude = extrude - (util::perp(normal) * endRight);
vertices.emplace_back(LineProgram::layoutVertex(currentCoordinate, extrude, round, true, -endRight, scaledDistance * LINE_DISTANCE_SCALE));
if (lineDistances) {
vertices2.emplace_back(LineLayoutVertexExt {
{ static_cast<float>(lineDistances->lineDistance(distance) - lineDistances->clipStart) },
{ static_cast<float>(lineDistances->clipEnd - lineDistances->clipStart) },
{ static_cast<uint32_t>(lineClips.size()) }
});
}
e3 = vertices.elements() - 1 - startVertex;
if (e1 >= 0 && e2 >= 0) {
triangleStore.emplace_back(e1, e2, e3);
Expand Down Expand Up @@ -507,6 +532,7 @@ void LineBucket::addPieSliceVertex(const GeometryCoordinate& currentVertex,
}

vertices.emplace_back(LineProgram::layoutVertex(currentVertex, flippedExtrude, false, lineTurnsLeft, 0, distance * LINE_DISTANCE_SCALE));
vertices2.emplace_back(LineLayoutVertexExt{});
e3 = vertices.elements() - 1 - startVertex;
if (e1 >= 0 && e2 >= 0) {
triangleStore.emplace_back(e1, e2, e3);
Expand All @@ -521,6 +547,9 @@ void LineBucket::addPieSliceVertex(const GeometryCoordinate& currentVertex,

void LineBucket::upload(gfx::UploadPass& uploadPass) {
if (!uploaded) {
if (vertices2.elements() > 0) {
vertexBuffer2 = uploadPass.createVertexBuffer(std::move(vertices2));
}
vertexBuffer = uploadPass.createVertexBuffer(std::move(vertices));
indexBuffer = uploadPass.createIndexBuffer(std::move(triangles));
}
Expand Down
13 changes: 13 additions & 0 deletions src/mbgl/renderer/buckets/line_bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,20 @@ class LineBucket final : public Bucket {
SegmentVector<LineAttributes> segments;

optional<gfx::VertexBuffer<LineLayoutVertex>> vertexBuffer;
gfx::VertexVector<LineLayoutVertexExt> vertices2;
optional<gfx::VertexBuffer<LineLayoutVertexExt>> vertexBuffer2;
optional<gfx::Texture> gradientTexture;
optional<gfx::IndexBuffer> indexBuffer;
optional<PremultipliedImage> gradientColorRamp;

uint32_t gradientVersion;
double maxLineLength;

struct LineClip {
double start;
double end;
};
std::vector<LineClip> lineClips;

std::map<std::string, LineProgram::Binders> paintPropertyBinders;

Expand Down
Loading

0 comments on commit 95f3a98

Please sign in to comment.