From f16bbf970797dc1ff42901d1416b03bc045b05dd Mon Sep 17 00:00:00 2001 From: Sovanny Huy Date: Wed, 29 May 2019 11:36:23 -0400 Subject: [PATCH] implemented texture queue --- modules/base/rendering/renderablesphere.cpp | 3 +- modules/base/rendering/suntexturemanager.cpp | 71 +++++++++++--------- modules/base/rendering/suntexturemanager.h | 6 ++ 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/modules/base/rendering/renderablesphere.cpp b/modules/base/rendering/renderablesphere.cpp index 54931a1618..ad603bb9eb 100644 --- a/modules/base/rendering/renderablesphere.cpp +++ b/modules/base/rendering/renderablesphere.cpp @@ -300,8 +300,7 @@ RenderableSphere::RenderableSphere(const ghoul::Dictionary& dictionary) setRenderBin(Renderable::RenderBin::Background); } } - - _sunTexMgr = SunTextureManager(); + } bool RenderableSphere::isReady() const { diff --git a/modules/base/rendering/suntexturemanager.cpp b/modules/base/rendering/suntexturemanager.cpp index f4dc6bdb69..1668d9eb80 100644 --- a/modules/base/rendering/suntexturemanager.cpp +++ b/modules/base/rendering/suntexturemanager.cpp @@ -31,6 +31,8 @@ #include #include #include +#include + namespace { @@ -43,29 +45,29 @@ namespace openspace { SunTextureManager::SunTextureManager(){ } - void SunTextureManager::update(std::unique_ptr& texture){ std::string current = getOpenSpaceDateTime(); if(_counter == 200){ // first time std::string next = checkNextTextureId(current, 1); - LERROR(next); if(next != "Not found!"){ startDownloadTexture(next); uploadTextureFromName(next); } } + // check if there's a texture for the current timestamp (minute) else if((_activeTextureDate != current) && (_textureListGPU.find(current) != _textureListGPU.end()) ){ - LERROR(current); + LERROR("switching to texture with id: " + current); _textureListGPU[_activeTextureDate] = std::move(texture); texture = std::move(_textureListGPU[current]); _activeTextureDate = current; float dir = global::timeManager.deltaTime(); std::string next = checkNextTextureId(current, dir); - LERROR(next); - if(next != "Not found!"){ + LERROR("next is = " + next); + //do nothing if Not found is returned + if(next != "Not found!"){ startDownloadTexture(next); uploadTextureFromName(next); } @@ -121,17 +123,13 @@ SunTextureManager::SunTextureManager(){ void SunTextureManager::startDownloadTexture(std::string textureId){ std::string url = "http://localhost:3000/get/" + textureId; - LERROR(url); - std::string destinationpath = "../../../../../sync/test/" + textureId; - LERROR(destinationpath); - LERROR(absPath(destinationpath)); + std::string destinationpath = absPath("../../../../../sync/magnetograms/" + textureId); AsyncHttpFileDownload ashd = AsyncHttpFileDownload(url, destinationpath, HttpFileDownload::Overwrite::Yes); HttpRequest::RequestOptions opt = {}; opt.requestTimeoutSeconds = 0; ashd.start(opt); - LERROR("nedladdning startad"); ashd.wait(); - LERROR("efter wait"); + LERROR("Texture " + textureId + " downloaded to disk" ); } @@ -141,7 +139,6 @@ SunTextureManager::SunTextureManager(){ HttpRequest::RequestOptions opt = {}; opt.requestTimeoutSeconds = 0; mmryDld.download(opt); - LERROR("nedladdning startad"); std::string s; std::transform(mmryDld.downloadedData().begin(), mmryDld.downloadedData().end(), std::back_inserter(s), [](char c) { @@ -170,20 +167,10 @@ SunTextureManager::SunTextureManager(){ void SunTextureManager::uploadTextureFromName(std::string filename){ FitsFileReader fitsFileReader(false); - std::string dateID =""; + const auto tempBild = fitsFileReader.readImageFloat("../../../../../sync/magnetograms/" + filename); - const std::string datestring = *fitsFileReader.readHeaderValueString("DATE"); - - int magicalCounter = 0; - for (char c : datestring) { - if (std::isdigit(c)) { - if (magicalCounter >= 0 && magicalCounter < 12) { - dateID += c; - } - magicalCounter++; - } - } + std::string dateID = parseMagnetogramDate(*fitsFileReader.readHeaderValueString("DATE")); const float stdvalue = *fitsFileReader.readHeaderValueFloat("IMGRMS01"); std::vector fitsImage; @@ -191,16 +178,15 @@ SunTextureManager::SunTextureManager(){ fitsImage.push_back((c+stdvalue)/stdvalue); } - LERROR(std::to_string(fitsImage.at(100))); + LERROR("laddar upp texture till GPU med id: " + dateID); auto textureFits = std::make_unique(fitsImage.data(), glm::vec3(360, 180, 1),ghoul::opengl::Texture::Format::Red, GL_R32F,GL_FLOAT); textureFits->uploadTexture(); - if(_textureListGPU.find(dateID) != _textureListGPU.end()){ - _textureListGPU[dateID].release(); - } - + _textureQueueGPU.push(dateID); _textureListGPU[dateID] = std::move(textureFits); + + trimGPUList(); } @@ -226,6 +212,31 @@ SunTextureManager::SunTextureManager(){ return datetime; } + std::string SunTextureManager::parseMagnetogramDate(std::string name){ + + std::string dateID; + int magicalCounter = 0; + for (char c : name) { + if (std::isdigit(c)) { + if (magicalCounter >= 0 && magicalCounter < 12) { + dateID += c; + } + magicalCounter++; + } + } + return dateID; + } + + void SunTextureManager::trimGPUList(){ + if(_textureQueueGPU.size() > _maxTexturesOnGPU){ + + std::string dateId = _textureQueueGPU.front(); + _textureQueueGPU.pop(); + LERROR("popped dateId : " + dateId); + _textureListGPU.at(dateId).release(); + //BaseModule::TextureManager.release(_textureListGPU.at(dateId).get()); + _textureListGPU.erase(dateId); + } + } - } diff --git a/modules/base/rendering/suntexturemanager.h b/modules/base/rendering/suntexturemanager.h index 3a102ec963..ceaeffc9a6 100644 --- a/modules/base/rendering/suntexturemanager.h +++ b/modules/base/rendering/suntexturemanager.h @@ -29,6 +29,7 @@ #include #include +#include namespace ghoul::opengl { class Texture; @@ -52,6 +53,8 @@ private: void checkFilesInDirectory(); void uploadTexturesFromList(std::vector& filelist); void uploadTextureFromName(std::string); + void trimGPUList(); + std::string parseMagnetogramDate(std::string name); std::string checkNextTextureId(std::string current, float dir); std::string getOpenSpaceDateTime(); @@ -62,8 +65,11 @@ private: std::thread _dldthread; std::string _activeTextureDate = "NODATE"; + const unsigned int _maxTexturesOnGPU = 5; //every texture is around 250kb in size + std::vector _textureListDisk; std::unordered_map> _textureListGPU; + std::queue _textureQueueGPU; };