Use temporary memory arena to create unsorted list of tiles, preventing dynamic memory allocations

This commit is contained in:
Alexander Bock
2020-08-13 16:48:18 +02:00
parent 4f68f99c9d
commit cabb962f6a
5 changed files with 36 additions and 24 deletions

View File

@@ -26,13 +26,19 @@
#define __OPENSPACE_CORE___MEMORYMANAGER___H__
#include <ghoul/misc/memorypool.h>
#include <memory_resource>
namespace openspace {
class MemoryManager {
public:
ghoul::MemoryPool<8 * 1024 * 1024, false> PersistentMemory;
ghoul::MemoryPool<10 * 1024, false> TemporaryMemory;
// This should be replaced with a std::pmr::memory_resource wrapper around our own
// Memory pool. Resetting the monotoic_buffer_resource costs an allocation, that we
// can prevent
std::pmr::monotonic_buffer_resource TemporaryMemory =
std::pmr::monotonic_buffer_resource(4096);
};
} // namespace openspace

View File

@@ -37,11 +37,13 @@
#include <openspace/rendering/renderengine.h>
#include <openspace/scene/scenegraphnode.h>
#include <openspace/scene/scene.h>
#include <openspace/util/memorymanager.h>
#include <openspace/util/spicemanager.h>
#include <openspace/util/time.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/misc/memorypool.h>
#include <ghoul/misc/profiling.h>
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/textureunit.h>
@@ -49,6 +51,7 @@
#include <ghoul/systemcapabilities/openglcapabilitiescomponent.h>
#include <numeric>
#include <queue>
#include <memory_resource>
namespace {
// Global flags to modify the RenderableGlobe
@@ -258,10 +261,14 @@ const Chunk& findChunkNode(const Chunk& node, const Geodetic2& location) {
return *n;
}
std::vector<std::pair<ChunkTile, const LayerRenderSettings*>>
std::pmr::vector<std::pair<ChunkTile, const LayerRenderSettings*>>
tilesAndSettingsUnsorted(const LayerGroup& layerGroup, const TileIndex& tileIndex)
{
std::vector<std::pair<ChunkTile, const LayerRenderSettings*>> tilesAndSettings;
ZoneScoped
std::pmr::vector<std::pair<ChunkTile, const LayerRenderSettings*>> tilesAndSettings(
&global::memoryManager.TemporaryMemory
);
for (Layer* layer : layerGroup.activeLayers()) {
if (layer->tileProvider()) {
tilesAndSettings.emplace_back(
@@ -286,10 +293,9 @@ BoundingHeights boundingHeightsForChunk(const Chunk& chunk, const LayerManager&
// (that is channel 0).
const size_t HeightChannel = 0;
const LayerGroup& heightmaps = lm.layerGroup(layergroupid::GroupID::HeightLayers);
std::vector<ChunkTileSettingsPair> chunkTileSettingPairs = tilesAndSettingsUnsorted(
heightmaps,
chunk.tileIndex
);
std::pmr::vector<ChunkTileSettingsPair> chunkTileSettingPairs =
tilesAndSettingsUnsorted(heightmaps, chunk.tileIndex);
bool lastHadMissingData = true;
for (const ChunkTileSettingsPair& chunkTileSettingsPair : chunkTileSettingPairs) {
@@ -341,18 +347,14 @@ BoundingHeights boundingHeightsForChunk(const Chunk& chunk, const LayerManager&
bool colorAvailableForChunk(const Chunk& chunk, const LayerManager& lm) {
ZoneScoped
using ChunkTileSettingsPair = std::pair<ChunkTile, const LayerRenderSettings*>;
const LayerGroup& colormaps = lm.layerGroup(layergroupid::GroupID::ColorLayers);
std::vector<ChunkTileSettingsPair> chunkTileSettingPairs = tilesAndSettingsUnsorted(
colormaps,
chunk.tileIndex
);
const LayerGroup& colorLayers = lm.layerGroup(layergroupid::GroupID::ColorLayers);
for (const ChunkTileSettingsPair& chunkTileSettingsPair : chunkTileSettingPairs) {
const ChunkTile& chunkTile = chunkTileSettingsPair.first;
if (chunkTile.tile.status == Tile::Status::Unavailable) {
return false;
for (Layer* lyr : colorLayers.activeLayers()) {
if (lyr->tileProvider()) {
ChunkTile t = tileprovider::chunkTile(*lyr->tileProvider(), chunk.tileIndex);
if (t.tile.status == Tile::Status::Unavailable) {
return false;
}
}
}

View File

@@ -1404,7 +1404,7 @@ ChunkTile chunkTile(TileProvider& tp, TileIndex tileIndex, int parents, int maxP
maxParents--;
}
if (maxParents < 0) {
return ChunkTile{ Tile(), uvTransform, TileDepthTransform() };
return ChunkTile { Tile(), uvTransform, TileDepthTransform() };
}
// Step 3. Traverse 0 or more parents up the chunkTree until we find a chunk that
@@ -1413,16 +1413,16 @@ ChunkTile chunkTile(TileProvider& tp, TileIndex tileIndex, int parents, int maxP
Tile t = tile(tp, tileIndex);
if (t.status != Tile::Status::OK) {
if (--maxParents < 0) {
return ChunkTile{ Tile(), uvTransform, TileDepthTransform() };
return ChunkTile { Tile(), uvTransform, TileDepthTransform() };
}
ascendToParent(tileIndex, uvTransform);
}
else {
return ChunkTile{ std::move(t), uvTransform, TileDepthTransform() };
return ChunkTile { std::move(t), uvTransform, TileDepthTransform() };
}
}
return ChunkTile{ Tile(), uvTransform, TileDepthTransform() };
return ChunkTile { Tile(), uvTransform, TileDepthTransform() };
}

View File

@@ -66,8 +66,8 @@ void GuiMemoryComponent::render() {
ImGui::Text("%s", "Persistent Memory Pool");
renderMemoryPoolInformation(global::memoryManager.PersistentMemory);
ImGui::Text("%s", "Temporary Memory Pool");
renderMemoryPoolInformation(global::memoryManager.TemporaryMemory);
//ImGui::Text("%s", "Temporary Memory Pool");
//renderMemoryPoolInformation(global::memoryManager.TemporaryMemory);
ImGui::End();
}

View File

@@ -62,6 +62,7 @@
#include <openspace/scripting/scriptengine.h>
#include <openspace/util/camera.h>
#include <openspace/util/factorymanager.h>
#include <openspace/util/memorymanager.h>
#include <openspace/util/spicemanager.h>
#include <openspace/util/task.h>
#include <openspace/util/timemanager.h>
@@ -1059,6 +1060,9 @@ void OpenSpaceEngine::preSynchronization() {
FileSys.triggerFilesystemEvents();
// Reset the temporary, frame-based storage
global::memoryManager.TemporaryMemory.release();
if (_hasScheduledAssetLoading) {
LINFO(fmt::format("Loading asset: {}", _scheduledAssetPathToLoad));
global::profile.setIgnoreUpdates(true);