From 3843bf1ce5adda7aef4a26e770f54cad7e63d640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 17 Feb 2014 16:35:12 +0100 Subject: [PATCH] better allocator defaults --- common.gypi | 2 +- include/llmr/renderer/fill_bucket.hpp | 10 +++- src/map/tile.cpp | 15 ++--- src/renderer/fill_bucket.cpp | 86 ++++++++++++--------------- 4 files changed, 56 insertions(+), 57 deletions(-) diff --git a/common.gypi b/common.gypi index 8cf3887fa..304799578 100644 --- a/common.gypi +++ b/common.gypi @@ -6,7 +6,7 @@ 'CLANG_CXX_LANGUAGE_STANDARD':'c++11', 'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0', }, - 'cppflags':['-std=c++11'], + 'cxxflags':['-std=c++11'], 'configurations': { 'Debug': { 'cflags': [ '-g', '-O0', '-I<(boost_root)/include' ], diff --git a/include/llmr/renderer/fill_bucket.hpp b/include/llmr/renderer/fill_bucket.hpp index b9b045bee..4360e9715 100644 --- a/include/llmr/renderer/fill_bucket.hpp +++ b/include/llmr/renderer/fill_bucket.hpp @@ -28,6 +28,12 @@ struct Coordinate; struct pbf; class FillBucket : public Bucket { + + static void *alloc(void *data, unsigned int size); + static void *realloc(void *data, void *ptr, unsigned int size); + static void free(void *userData, void *ptr); + + typedef ElementGroup triangle_group_type; typedef ElementGroup line_group_type; public: @@ -52,6 +58,7 @@ public: const BucketGeometryDescription geom_desc; private: + TESSalloc *allocator; TESStesselator *tesselator; std::shared_ptr vertexBuffer; @@ -66,10 +73,11 @@ private: std::vector triangleGroups; std::vector lineGroups; + std::vector line; bool hasVertices = false; static const int vertexSize = 2; - static const int stride = sizeof(TESSreal) * vertexSize; + static const int stride = sizeof(TESSreal) *vertexSize; static const int vertices_per_group = 3; }; diff --git a/src/map/tile.cpp b/src/map/tile.cpp index ef9084b0d..9c85da946 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -173,7 +173,7 @@ std::shared_ptr Tile::createBucket(const VectorTile& tile, const BucketD std::shared_ptr Tile::createFillBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc) { std::shared_ptr bucket = std::make_shared(fillVertexBuffer, triangleElementsBuffer, lineElementsBuffer, bucket_desc); - auto init_start = std::chrono::high_resolution_clock::now(); + // auto init_start = std::chrono::high_resolution_clock::now(); FilteredVectorTileLayer filtered_layer(layer, bucket_desc); for (pbf feature : filtered_layer) { @@ -181,16 +181,17 @@ std::shared_ptr Tile::createFillBucket(const VectorTileLayer& layer, con pbf geometry_pbf = feature.message(); if (geometry_pbf) { bucket->addGeometry(geometry_pbf); + bucket->tessellate(); } } } - auto init_end = std::chrono::high_resolution_clock::now(); - auto init = (double)std::chrono::duration_cast(init_end - init_start).count() / 1000.0; - auto tess_start = std::chrono::high_resolution_clock::now(); - bucket->tessellate(); - auto tess_end = std::chrono::high_resolution_clock::now(); - auto tess = (double)std::chrono::duration_cast(tess_end - tess_start).count() / 1000.0; + // auto init_end = std::chrono::high_resolution_clock::now(); + // auto init = (double)std::chrono::duration_cast(init_end - init_start).count() / 1000.0; + + // auto tess_start = std::chrono::high_resolution_clock::now(); + // auto tess_end = std::chrono::high_resolution_clock::now(); + // auto tess = (double)std::chrono::duration_cast(tess_end - tess_start).count() / 1000.0; // fprintf(stderr, "%s/%s init: %f tess: %f\n", bucket_desc.source_layer.c_str(), bucket_desc.source_field.c_str(), init, tess); diff --git a/src/renderer/fill_bucket.cpp b/src/renderer/fill_bucket.cpp index 630b571ef..d63c28144 100644 --- a/src/renderer/fill_bucket.cpp +++ b/src/renderer/fill_bucket.cpp @@ -10,48 +10,44 @@ #include - -void *customHeapAlloc(void *userData, unsigned int size) { - // fprintf(stderr, "malloc %d\n", size); - return malloc(size); -} - -void *customHeapRealloc(void *userData, void *ptr, unsigned int size) { - // fprintf(stderr, "realloc %p %d\n", ptr, size); - return realloc(ptr, size); -} - -void customHeapFree(void *userData, void *ptr) { - // fprintf(stderr, "free %p\n", ptr); - free(ptr); -} - -static TESSalloc defaultAlloc = { - &customHeapAlloc, - &customHeapRealloc, - &customHeapFree, - nullptr, // userData - 512, // meshEdgeBucketSize - 512, // meshVertexBucketSize - 256, // meshFaceBucketSize - 512, // dictNodeBucketSize - 256, // regionBucketSize - 128, // extraVertices allocated for the priority queue. -}; - - #include struct geometry_too_long_exception : std::exception {}; using namespace llmr; + + +void *FillBucket::alloc(void *, unsigned int size) { + return ::malloc(size); +} + +void *FillBucket::realloc(void *, void *ptr, unsigned int size) { + return ::realloc(ptr, size); +} + +void FillBucket::free(void *, void *ptr) { + ::free(ptr); +} + FillBucket::FillBucket(const std::shared_ptr& vertexBuffer, const std::shared_ptr& triangleElementsBuffer, const std::shared_ptr& lineElementsBuffer, const BucketDescription& bucket_desc) : geom_desc(bucket_desc.geometry), - tesselator(tessNewTess(&defaultAlloc)), + allocator(new TESSalloc { + &alloc, + &realloc, + &free, + nullptr, // userData + 64, // meshEdgeBucketSize + 64, // meshVertexBucketSize + 32, // meshFaceBucketSize + 64, // dictNodeBucketSize + 8, // regionBucketSize + 128, // extraVertices allocated for the priority queue. + }), + tesselator(tessNewTess(allocator)), vertexBuffer(vertexBuffer), triangleElementsBuffer(triangleElementsBuffer), lineElementsBuffer(lineElementsBuffer), @@ -65,38 +61,32 @@ FillBucket::~FillBucket() { if (tesselator) { tessDeleteTess(tesselator); } + if (allocator) { + delete allocator; + } } void FillBucket::addGeometry(pbf& geom) { - std::vector line; Geometry::command cmd; - std::vector line2; - Coordinate coord; Geometry geometry(geom); int32_t x, y; while ((cmd = geometry.next(x, y)) != Geometry::end) { if (cmd == Geometry::move_to) { if (line.size()) { - // addGeometry(line); + tessAddContour(tesselator, vertexSize, line.data(), stride, line.size() / vertexSize); line.clear(); - } - if (line2.size()) { - tessAddContour(tesselator, vertexSize, line2.data(), stride, line2.size() / vertexSize); - line2.clear(); hasVertices = true; } } - line.emplace_back(x, y); - line2.push_back(x); - line2.push_back(y); + line.push_back(x); + line.push_back(y); } + if (line.size()) { - // addGeometry(line); - } - if (line2.size()) { - tessAddContour(tesselator, vertexSize, line2.data(), stride, line2.size() / vertexSize); + tessAddContour(tesselator, vertexSize, line.data(), stride, line.size() / vertexSize); + line.clear(); hasVertices = true; } } @@ -135,9 +125,9 @@ void FillBucket::tessellate() { } // Add vertices and create reverse index mapping for drawing the outline. - std::map contour_vertices; + // std::map contour_vertices; for (int i = 0; i < vertex_count; ++i) { - contour_vertices[vertex_indices[i]] = i; + // contour_vertices[vertex_indices[i]] = i; vertexBuffer->add(vertices[i * 2], vertices[i * 2 + 1]); }