Skip to content

Commit

Permalink
Merge branch 'fix-rastersource-cache' into scene-refactor-base
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek committed Dec 24, 2018
2 parents e2debbd + 90bf623 commit 70c0ac8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
46 changes: 24 additions & 22 deletions core/src/data/rasterSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "tile/tile.h"
#include "tile/tileTask.h"
#include "util/mapProjection.h"
#include "platform.h"
#include "log.h"

namespace Tangram {

Expand Down Expand Up @@ -77,7 +77,6 @@ RasterSource::RasterSource(const std::string& _name, std::unique_ptr<DataSource>
TextureOptions _options, TileSource::ZoomOptions _zoomOptions)
: TileSource(_name, std::move(_sources), _zoomOptions),
m_texOptions(_options) {
m_textures = std::make_shared<Cache>();

m_emptyTexture = std::make_shared<Texture>(m_texOptions);
}
Expand All @@ -90,13 +89,7 @@ std::shared_ptr<Texture> RasterSource::createTexture(TileID _tile, const std::ve
auto data = reinterpret_cast<const uint8_t*>(_rawTileData.data());
auto length = _rawTileData.size();

std::shared_ptr<Texture> texture(new Texture(data, length, m_texOptions),
[c = std::weak_ptr<Cache>(m_textures), _tile](auto t) {
if (auto cache = c.lock()) { cache->erase(_tile); }
delete t;
});

return texture;
return std::make_shared<Texture>(data, length, m_texOptions);
}

void RasterSource::loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) {
Expand Down Expand Up @@ -143,9 +136,9 @@ std::shared_ptr<TileTask> RasterSource::createTask(TileID _tileId, int _subTask)
// First try existing textures cache
TileID id(_tileId.x, _tileId.y, _tileId.z);

auto texIt = m_textures->find(id);
if (texIt != m_textures->end()) {
task->m_texture = texIt->second.lock();
auto texIt = m_textures.find(id);
if (texIt != m_textures.end()) {
task->m_texture = texIt->second;

if (task->m_texture) {
// No more loading needed.
Expand All @@ -157,20 +150,29 @@ std::shared_ptr<TileTask> RasterSource::createTask(TileID _tileId, int _subTask)
return task;
}

Raster RasterSource::getRaster(const TileTask& _task) {
const auto& taskTileID = _task.tileId();
TileID id(taskTileID.x, taskTileID.y, taskTileID.z);
Raster RasterSource::getRaster(const RasterTileTask& _task) {
const auto& tileId = _task.tileId();
TileID id(tileId.x, tileId.y, tileId.z);

auto texIt = m_textures->find(id);
if (texIt != m_textures->end()) {
auto&& texture = texIt->second.lock();
return { id, texture };
if (_task.m_texture == m_emptyTexture) {
return Raster{id, m_emptyTexture};
}

auto& task = static_cast<const RasterTileTask&>(_task);
m_textures->emplace(id, task.m_texture);
auto entry = m_textures.emplace(id, _task.m_texture);
std::shared_ptr<Texture> texture = entry.first->second;
LOGD("Cache: add %d/%d/%d - reused: %d", id.x, id.y, id.z, !entry.second);

// Remove textures that are only held by cache
for(auto it = m_textures.begin(), end = m_textures.end(); it != end; ) {
if (it->second.use_count() == 1) {
LOGD("Cache: remove %d/%d/%d", it->first.x, it->first.y, it->first.z);
it = m_textures.erase(it);
} else {
++it;
}
}

return { id, task.m_texture };
return Raster{id, texture};
}

}
6 changes: 3 additions & 3 deletions core/src/data/rasterSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class RasterTileTask;

class RasterSource : public TileSource {

using Cache = std::map<TileID, std::weak_ptr<Texture>>;
std::shared_ptr<Cache> m_textures;
using Cache = std::map<TileID, std::shared_ptr<Texture>>;
Cache m_textures;

TextureOptions m_texOptions;

Expand All @@ -42,7 +42,7 @@ class RasterSource : public TileSource {

std::shared_ptr<Texture> createTexture(TileID _tile, const std::vector<char>& _rawTileData);

Raster getRaster(const TileTask& _task);
Raster getRaster(const RasterTileTask& _task);

};

Expand Down
41 changes: 20 additions & 21 deletions core/src/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,33 +438,32 @@ bool Style::draw(RenderState& rs, const Tile& _tile) {
bool styleMeshDrawn = true;
TileID tileID = _tile.getID();

if (hasRasters() && !_tile.rasters().empty()) {
if (hasRasters()) {
UniformTextureArray textureIndexUniform;
UniformArray2f rasterSizeUniform;
UniformArray3f rasterOffsetsUniform;

for (auto& raster : _tile.rasters()) {
if (raster.isValid()) {
auto& texture = raster.texture;
auto texUnit = rs.nextAvailableTextureUnit();
texture->bind(rs, texUnit);

textureIndexUniform.slots.push_back(texUnit);
rasterSizeUniform.push_back({texture->width(), texture->height()});

if (tileID.z > raster.tileID.z) {
float dz = tileID.z - raster.tileID.z;
float dz2 = powf(2.f, dz);

rasterOffsetsUniform.push_back({
fmodf(tileID.x, dz2) / dz2,
(dz2 - 1.f - fmodf(tileID.y, dz2)) / dz2,
1.f / dz2
});
} else {
rasterOffsetsUniform.push_back({0, 0, 1});
}

auto& texture = raster.texture;
auto texUnit = rs.nextAvailableTextureUnit();
texture->bind(rs, texUnit);

textureIndexUniform.slots.push_back(texUnit);
rasterSizeUniform.push_back({texture->width(), texture->height()});

float x = 0.f;
float y = 0.f;
float z = 1.f;

if (tileID.z > raster.tileID.z) {
float dz = tileID.z - raster.tileID.z;
float dz2 = powf(2.f, dz);
x = fmodf(tileID.x, dz2) / dz2;
y = (dz2 - 1.f - fmodf(tileID.y, dz2)) / dz2;
z = 1.f / dz2;
}
rasterOffsetsUniform.emplace_back(x, y, z);
}

m_shaderProgram->setUniformi(rs, m_mainUniforms.uRasters, textureIndexUniform);
Expand Down

0 comments on commit 70c0ac8

Please sign in to comment.