From 6b2b68e69186129111b736b18751724dce906ef0 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 25 Oct 2016 03:36:23 -0700 Subject: [PATCH] trying to add counters for the default FBO --- interface/resources/qml/Stats.qml | 3 +++ interface/src/ui/Stats.cpp | 3 +++ interface/src/ui/Stats.h | 2 ++ libraries/gl/src/gl/Context.cpp | 23 +++++++++++++++++++++++ libraries/gl/src/gl/Context.h | 11 ++++++++++- libraries/gl/src/gl/OffscreenGLCanvas.cpp | 21 +++++++++++++++++++-- libraries/gl/src/gl/OffscreenGLCanvas.h | 2 ++ 7 files changed, 62 insertions(+), 3 deletions(-) diff --git a/interface/resources/qml/Stats.qml b/interface/resources/qml/Stats.qml index 7ca527ad3f..d8333112ce 100644 --- a/interface/resources/qml/Stats.qml +++ b/interface/resources/qml/Stats.qml @@ -228,6 +228,9 @@ Item { StatText { text: " Count: " + root.gpuTextures; } + StatText { + text: "GL Context FBO Memory: " + root.glContextFBOMemory + " MB"; + } StatText { text: "QML Texture Memory: " + root.qmlTextureMemory + " MB"; } diff --git a/interface/src/ui/Stats.cpp b/interface/src/ui/Stats.cpp index 9c7d66e0e2..b0c5113c84 100644 --- a/interface/src/ui/Stats.cpp +++ b/interface/src/ui/Stats.cpp @@ -25,6 +25,8 @@ #include #include +#include + #include "BandwidthRecorder.h" #include "Menu.h" #include "Util.h" @@ -287,6 +289,7 @@ void Stats::updateStats(bool force) { STAT_UPDATE(gpuBuffers, (int)gpu::Context::getBufferGPUCount()); STAT_UPDATE(gpuTextures, (int)gpu::Context::getTextureGPUCount()); STAT_UPDATE(gpuTexturesSparse, (int)gpu::Context::getTextureGPUSparseCount()); + STAT_UPDATE(glContextFBOMemory, (int)BYTES_TO_MB(gl::Context::getDefaultFBOMemoryUsage())); STAT_UPDATE(qmlTextureMemory, (int)BYTES_TO_MB(OffscreenQmlSurface::getUsedTextureMemory())); STAT_UPDATE(gpuTextureMemory, (int)BYTES_TO_MB(gpu::Texture::getTextureGPUMemoryUsage())); STAT_UPDATE(gpuTextureVirtualMemory, (int)BYTES_TO_MB(gpu::Texture::getTextureGPUVirtualMemoryUsage())); diff --git a/interface/src/ui/Stats.h b/interface/src/ui/Stats.h index 97f047c3d5..964920210c 100644 --- a/interface/src/ui/Stats.h +++ b/interface/src/ui/Stats.h @@ -90,6 +90,7 @@ class Stats : public QQuickItem { STATS_PROPERTY(int, gpuBuffers, 0) STATS_PROPERTY(int, gpuTextures, 0) STATS_PROPERTY(int, gpuTexturesSparse, 0) + STATS_PROPERTY(int, glContextFBOMemory, 0) STATS_PROPERTY(int, qmlTextureMemory, 0) STATS_PROPERTY(int, gpuTextureMemory, 0) STATS_PROPERTY(int, gpuTextureVirtualMemory, 0) @@ -182,6 +183,7 @@ signals: void localInternalChanged(); void localLeavesChanged(); void timingStatsChanged(); + void glContextFBOMemoryChanged(); void qmlTextureMemoryChanged(); void gpuBuffersChanged(); void gpuTexturesChanged(); diff --git a/libraries/gl/src/gl/Context.cpp b/libraries/gl/src/gl/Context.cpp index 5b8a1a2fa1..fda457f94a 100644 --- a/libraries/gl/src/gl/Context.cpp +++ b/libraries/gl/src/gl/Context.cpp @@ -40,6 +40,27 @@ static bool enableDebugLogger = QProcessEnvironment::systemEnvironment().contain using namespace gl; + +std::atomic Context::_defaultFBOMemoryUsage { 0 }; + +size_t Context::getDefaultFBOMemoryUsage() { return _defaultFBOMemoryUsage.load(); } + +size_t Context::evalMemoryUsage(uint32_t width, uint32_t height) { + return width * height * 4; +} + +void Context::updateDefaultFBOMemoryUsage(size_t prevFBOSize, size_t newFBOSize) { + if (prevFBOSize == newFBOSize) { + return; + } + if (newFBOSize > prevFBOSize) { + _defaultFBOMemoryUsage.fetch_add(newFBOSize - prevFBOSize); + } else { + _defaultFBOMemoryUsage.fetch_sub(prevFBOSize - newFBOSize); + } +} + + Context* Context::PRIMARY = nullptr; Context::Context() {} @@ -277,6 +298,8 @@ void OffscreenContext::create() { _window->setSurfaceType(QSurface::OpenGLSurface); _window->create(); setWindow(_window); + QSize windowSize = _window->size() * _window->devicePixelRatio(); + qCDebug(glLogging) << "New Offscreen GLContext, window size = " << windowSize.width() << " , " << windowSize.height(); QGuiApplication::processEvents(); } Parent::create(); diff --git a/libraries/gl/src/gl/Context.h b/libraries/gl/src/gl/Context.h index 6dc859f222..783b634f75 100644 --- a/libraries/gl/src/gl/Context.h +++ b/libraries/gl/src/gl/Context.h @@ -11,6 +11,7 @@ #include #include +#include #if defined(Q_OS_WIN) #include @@ -23,7 +24,7 @@ class QThread; namespace gl { -class Context { + class Context { protected: QWindow* _window { nullptr }; static Context* PRIMARY; @@ -57,6 +58,13 @@ class Context { virtual void create(); QOpenGLContext* qglContext(); void moveToThread(QThread* thread); + + static size_t getDefaultFBOMemoryUsage(); + static size_t evalMemoryUsage(uint32_t width, uint32_t height); + static void updateDefaultFBOMemoryUsage(size_t prevFBOSize, size_t newFBOSize); + + private: + static std::atomic _defaultFBOMemoryUsage; }; class OffscreenContext : public Context { @@ -67,6 +75,7 @@ class Context { virtual ~OffscreenContext(); void create() override; }; + } #endif // hifi_gpu_GPUConfig_h diff --git a/libraries/gl/src/gl/OffscreenGLCanvas.cpp b/libraries/gl/src/gl/OffscreenGLCanvas.cpp index dbd338bc34..ac12f95c25 100644 --- a/libraries/gl/src/gl/OffscreenGLCanvas.cpp +++ b/libraries/gl/src/gl/OffscreenGLCanvas.cpp @@ -17,11 +17,16 @@ #include #include +#include "Context.h" #include "GLHelpers.h" #include "GLLogging.h" -OffscreenGLCanvas::OffscreenGLCanvas() : _context(new QOpenGLContext), _offscreenSurface(new QOffscreenSurface){ +OffscreenGLCanvas::OffscreenGLCanvas() : + _context(new QOpenGLContext), + _offscreenSurface(new QOffscreenSurface), + _offscreenSurfaceCurrentMemoryUsage(0) +{ } OffscreenGLCanvas::~OffscreenGLCanvas() { @@ -30,6 +35,8 @@ OffscreenGLCanvas::~OffscreenGLCanvas() { delete _context; _context = nullptr; + gl::Context::updateDefaultFBOMemoryUsage(_offscreenSurfaceCurrentMemoryUsage, 0); + _offscreenSurface->destroy(); delete _offscreenSurface; _offscreenSurface = nullptr; @@ -53,10 +60,18 @@ bool OffscreenGLCanvas::create(QOpenGLContext* sharedContext) { return false; } +void OffscreenGLCanvas::updateMemoryCounter() { + if (_offscreenSurface) { + auto newSize =_offscreenSurface->size(); + auto newMemSize = gl::Context::evalMemoryUsage(newSize.width(), newSize.height()); + gl::Context::updateDefaultFBOMemoryUsage(_offscreenSurfaceCurrentMemoryUsage, newMemSize); + _offscreenSurfaceCurrentMemoryUsage = newMemSize; + } +} + bool OffscreenGLCanvas::makeCurrent() { bool result = _context->makeCurrent(_offscreenSurface); Q_ASSERT(result); - std::call_once(_reportOnce, [this]{ qCDebug(glLogging) << "GL Version: " << QString((const char*) glGetString(GL_VERSION)); qCDebug(glLogging) << "GL Shader Language Version: " << QString((const char*) glGetString(GL_SHADING_LANGUAGE_VERSION)); @@ -64,6 +79,8 @@ bool OffscreenGLCanvas::makeCurrent() { qCDebug(glLogging) << "GL Renderer: " << QString((const char*) glGetString(GL_RENDERER)); }); + updateMemoryCounter(); + return result; } diff --git a/libraries/gl/src/gl/OffscreenGLCanvas.h b/libraries/gl/src/gl/OffscreenGLCanvas.h index 583aa7c60f..be7d8986f6 100644 --- a/libraries/gl/src/gl/OffscreenGLCanvas.h +++ b/libraries/gl/src/gl/OffscreenGLCanvas.h @@ -36,6 +36,8 @@ protected: std::once_flag _reportOnce; QOpenGLContext* _context{ nullptr }; QOffscreenSurface* _offscreenSurface{ nullptr }; + size_t _offscreenSurfaceCurrentMemoryUsage { 0 }; + void updateMemoryCounter(); }; #endif // hifi_OffscreenGLCanvas_h