From 58b81e3b0c245c74e2897fc82bede0ff3328472b Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 24 Oct 2016 18:34:16 -0700 Subject: [PATCH 1/5] Adding counter for the amount of memory used in texture for Framebuffers --- interface/resources/qml/Stats.qml | 3 +++ interface/src/ui/Stats.cpp | 1 + interface/src/ui/Stats.h | 2 ++ libraries/gpu-gl/src/gpu/gl/GLFramebuffer.cpp | 2 +- libraries/gpu-gl/src/gpu/gl/GLTexture.cpp | 8 +++++++- libraries/gpu/src/gpu/Context.cpp | 19 ++++++++++++++++++- libraries/gpu/src/gpu/Context.h | 4 ++++ libraries/gpu/src/gpu/Texture.cpp | 5 +++++ libraries/gpu/src/gpu/Texture.h | 1 + 9 files changed, 42 insertions(+), 3 deletions(-) diff --git a/interface/resources/qml/Stats.qml b/interface/resources/qml/Stats.qml index 6ebe3f2278..7ca527ad3f 100644 --- a/interface/resources/qml/Stats.qml +++ b/interface/resources/qml/Stats.qml @@ -215,6 +215,9 @@ Item { StatText { text: " Commited Memory: " + root.gpuTextureMemory + " MB"; } + StatText { + text: " Framebuffer Memory: " + root.gpuTextureFramebufferMemory + " MB"; + } StatText { text: " Sparse Memory: " + root.gpuTextureSparseMemory + " MB"; visible: 0 != root.gpuSparseTextureEnabled; diff --git a/interface/src/ui/Stats.cpp b/interface/src/ui/Stats.cpp index 6c301f6f0a..9c7d66e0e2 100644 --- a/interface/src/ui/Stats.cpp +++ b/interface/src/ui/Stats.cpp @@ -290,6 +290,7 @@ void Stats::updateStats(bool force) { 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())); + STAT_UPDATE(gpuTextureFramebufferMemory, (int)BYTES_TO_MB(gpu::Texture::getTextureGPUFramebufferMemoryUsage())); STAT_UPDATE(gpuTextureSparseMemory, (int)BYTES_TO_MB(gpu::Texture::getTextureGPUSparseMemoryUsage())); STAT_UPDATE(gpuSparseTextureEnabled, gpu::Texture::getEnableSparseTextures() ? 1 : 0); STAT_UPDATE(gpuFreeMemory, (int)BYTES_TO_MB(gpu::Context::getFreeGPUMemory())); diff --git a/interface/src/ui/Stats.h b/interface/src/ui/Stats.h index c9fccf02d9..97f047c3d5 100644 --- a/interface/src/ui/Stats.h +++ b/interface/src/ui/Stats.h @@ -93,6 +93,7 @@ class Stats : public QQuickItem { STATS_PROPERTY(int, qmlTextureMemory, 0) STATS_PROPERTY(int, gpuTextureMemory, 0) STATS_PROPERTY(int, gpuTextureVirtualMemory, 0) + STATS_PROPERTY(int, gpuTextureFramebufferMemory, 0) STATS_PROPERTY(int, gpuTextureSparseMemory, 0) STATS_PROPERTY(int, gpuSparseTextureEnabled, 0) STATS_PROPERTY(int, gpuFreeMemory, 0) @@ -187,6 +188,7 @@ signals: void gpuTexturesSparseChanged(); void gpuTextureMemoryChanged(); void gpuTextureVirtualMemoryChanged(); + void gpuTextureFramebufferMemoryChanged(); void gpuTextureSparseMemoryChanged(); void gpuSparseTextureEnabledChanged(); void gpuFreeMemoryChanged(); diff --git a/libraries/gpu-gl/src/gpu/gl/GLFramebuffer.cpp b/libraries/gpu-gl/src/gpu/gl/GLFramebuffer.cpp index eaca31ebdc..0ff00697ca 100644 --- a/libraries/gpu-gl/src/gpu/gl/GLFramebuffer.cpp +++ b/libraries/gpu-gl/src/gpu/gl/GLFramebuffer.cpp @@ -13,7 +13,7 @@ using namespace gpu; using namespace gpu::gl; GLFramebuffer::~GLFramebuffer() { - if (_id) { + if (_id) { auto backend = _backend.lock(); if (backend) { backend->releaseFramebuffer(_id); diff --git a/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp b/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp index ef16e3b53f..cd9807fd46 100644 --- a/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp +++ b/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp @@ -181,8 +181,11 @@ GLTexture::~GLTexture() { // the GL45Texture destructor for doing any required work tracking GPU stats backend->releaseTexture(_id, _size); } + + if (!_external && !_transferrable) { + Backend::updateTextureGPUFramebufferMemoryUsage(_size, 0); + } } - Backend::updateTextureGPUVirtualMemoryUsage(_virtualSize, 0); } void GLTexture::createTexture() { @@ -217,6 +220,9 @@ void GLTexture::withPreservedTexture(std::function f) const { } void GLTexture::setSize(GLuint size) const { + if (!_external && !_transferrable) { + Backend::updateTextureGPUFramebufferMemoryUsage(_size, size); + } Backend::updateTextureGPUMemoryUsage(_size, size); const_cast(_size) = size; } diff --git a/libraries/gpu/src/gpu/Context.cpp b/libraries/gpu/src/gpu/Context.cpp index 6e8a5e312f..ab6bba178a 100644 --- a/libraries/gpu/src/gpu/Context.cpp +++ b/libraries/gpu/src/gpu/Context.cpp @@ -170,7 +170,8 @@ std::atomic Context::_bufferGPUMemoryUsage { 0 }; std::atomic Context::_textureGPUCount{ 0 }; std::atomic Context::_textureGPUSparseCount { 0 }; std::atomic Context::_textureGPUMemoryUsage { 0 }; -std::atomic Context::_textureGPUVirtualMemoryUsage{ 0 }; +std::atomic Context::_textureGPUVirtualMemoryUsage { 0 }; +std::atomic Context::_textureGPUFramebufferMemoryUsage { 0 }; std::atomic Context::_textureGPUSparseMemoryUsage { 0 }; std::atomic Context::_textureGPUTransferCount { 0 }; @@ -262,6 +263,17 @@ void Context::updateTextureGPUVirtualMemoryUsage(Size prevObjectSize, Size newOb } } +void Context::updateTextureGPUFramebufferMemoryUsage(Size prevObjectSize, Size newObjectSize) { + if (prevObjectSize == newObjectSize) { + return; + } + if (newObjectSize > prevObjectSize) { + _textureGPUFramebufferMemoryUsage.fetch_add(newObjectSize - prevObjectSize); + } else { + _textureGPUFramebufferMemoryUsage.fetch_sub(prevObjectSize - newObjectSize); + } +} + void Context::updateTextureGPUSparseMemoryUsage(Size prevObjectSize, Size newObjectSize) { if (prevObjectSize == newObjectSize) { return; @@ -310,6 +322,10 @@ Context::Size Context::getTextureGPUVirtualMemoryUsage() { return _textureGPUVirtualMemoryUsage.load(); } +Context::Size Context::getTextureGPUFramebufferMemoryUsage() { + return _textureGPUFramebufferMemoryUsage.load(); +} + Context::Size Context::getTextureGPUSparseMemoryUsage() { return _textureGPUSparseMemoryUsage.load(); } @@ -329,6 +345,7 @@ void Backend::incrementTextureGPUSparseCount() { Context::incrementTextureGPUSpa void Backend::decrementTextureGPUSparseCount() { Context::decrementTextureGPUSparseCount(); } void Backend::updateTextureGPUMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize) { Context::updateTextureGPUMemoryUsage(prevObjectSize, newObjectSize); } void Backend::updateTextureGPUVirtualMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize) { Context::updateTextureGPUVirtualMemoryUsage(prevObjectSize, newObjectSize); } +void Backend::updateTextureGPUFramebufferMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize) { Context::updateTextureGPUFramebufferMemoryUsage(prevObjectSize, newObjectSize); } void Backend::updateTextureGPUSparseMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize) { Context::updateTextureGPUSparseMemoryUsage(prevObjectSize, newObjectSize); } void Backend::incrementTextureGPUTransferCount() { Context::incrementTextureGPUTransferCount(); } void Backend::decrementTextureGPUTransferCount() { Context::decrementTextureGPUTransferCount(); } diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index ea199db3aa..763e91b3e4 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -101,6 +101,7 @@ public: static void updateTextureGPUMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize); static void updateTextureGPUSparseMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize); static void updateTextureGPUVirtualMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize); + static void updateTextureGPUFramebufferMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize); static void incrementTextureGPUTransferCount(); static void decrementTextureGPUTransferCount(); @@ -210,6 +211,7 @@ public: static Size getFreeGPUMemory(); static Size getTextureGPUMemoryUsage(); static Size getTextureGPUVirtualMemoryUsage(); + static Size getTextureGPUFramebufferMemoryUsage(); static Size getTextureGPUSparseMemoryUsage(); static uint32_t getTextureGPUTransferCount(); @@ -249,6 +251,7 @@ protected: static void updateTextureGPUMemoryUsage(Size prevObjectSize, Size newObjectSize); static void updateTextureGPUSparseMemoryUsage(Size prevObjectSize, Size newObjectSize); static void updateTextureGPUVirtualMemoryUsage(Size prevObjectSize, Size newObjectSize); + static void updateTextureGPUFramebufferMemoryUsage(Size prevObjectSize, Size newObjectSize); static void incrementTextureGPUTransferCount(); static void decrementTextureGPUTransferCount(); @@ -264,6 +267,7 @@ protected: static std::atomic _textureGPUMemoryUsage; static std::atomic _textureGPUSparseMemoryUsage; static std::atomic _textureGPUVirtualMemoryUsage; + static std::atomic _textureGPUFramebufferMemoryUsage; static std::atomic _textureGPUTransferCount; diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 5c393d758e..3283f5a4d9 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -102,6 +102,11 @@ Texture::Size Texture::getTextureGPUVirtualMemoryUsage() { return Context::getTextureGPUVirtualMemoryUsage(); } + +Texture::Size Texture::getTextureGPUFramebufferMemoryUsage() { + return Context::getTextureGPUFramebufferMemoryUsage(); +} + Texture::Size Texture::getTextureGPUSparseMemoryUsage() { return Context::getTextureGPUSparseMemoryUsage(); } diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 881f4439a1..0f1022a0c9 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -154,6 +154,7 @@ public: static uint32_t getTextureGPUSparseCount(); static Size getTextureGPUMemoryUsage(); static Size getTextureGPUVirtualMemoryUsage(); + static Size getTextureGPUFramebufferMemoryUsage(); static Size getTextureGPUSparseMemoryUsage(); static uint32_t getTextureGPUTransferCount(); static Size getAllowedGPUMemoryUsage(); From 6b2b68e69186129111b736b18751724dce906ef0 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 25 Oct 2016 03:36:23 -0700 Subject: [PATCH 2/5] 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 From 507d3e5a39605c620d4b9d7aa2c1f5e9ed85c656 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 25 Oct 2016 12:28:07 -0700 Subject: [PATCH 3/5] IMprove the counting and namoing of the new couters the gl swapchain --- interface/resources/qml/Stats.qml | 2 +- interface/src/ui/Stats.cpp | 4 ++- interface/src/ui/Stats.h | 4 +-- libraries/gl/src/gl/Context.cpp | 43 +++++++++++++++++------ libraries/gl/src/gl/Context.h | 12 ++++--- libraries/gl/src/gl/ContextQt.cpp | 4 +++ libraries/gl/src/gl/GLHelpers.cpp | 9 +++++ libraries/gl/src/gl/GLHelpers.h | 2 ++ libraries/gl/src/gl/OffscreenGLCanvas.cpp | 16 +-------- libraries/gl/src/gl/OffscreenGLCanvas.h | 2 -- libraries/gpu-gl/src/gpu/gl/GLTexture.cpp | 1 + 11 files changed, 64 insertions(+), 35 deletions(-) diff --git a/interface/resources/qml/Stats.qml b/interface/resources/qml/Stats.qml index d8333112ce..2439aaf5c0 100644 --- a/interface/resources/qml/Stats.qml +++ b/interface/resources/qml/Stats.qml @@ -229,7 +229,7 @@ Item { text: " Count: " + root.gpuTextures; } StatText { - text: "GL Context FBO Memory: " + root.glContextFBOMemory + " MB"; + text: "GL Swapchain Memory: " + root.glContextSwapchainMemory + " MB"; } StatText { text: "QML Texture Memory: " + root.qmlTextureMemory + " MB"; diff --git a/interface/src/ui/Stats.cpp b/interface/src/ui/Stats.cpp index b0c5113c84..b1fc0ce503 100644 --- a/interface/src/ui/Stats.cpp +++ b/interface/src/ui/Stats.cpp @@ -289,7 +289,9 @@ 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(glContextSwapchainMemory, (int)BYTES_TO_MB(gl::Context::getSwapchainMemoryUsage())); + 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 964920210c..a98a99f093 100644 --- a/interface/src/ui/Stats.h +++ b/interface/src/ui/Stats.h @@ -90,7 +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, glContextSwapchainMemory, 0) STATS_PROPERTY(int, qmlTextureMemory, 0) STATS_PROPERTY(int, gpuTextureMemory, 0) STATS_PROPERTY(int, gpuTextureVirtualMemory, 0) @@ -183,7 +183,7 @@ signals: void localInternalChanged(); void localLeavesChanged(); void timingStatsChanged(); - void glContextFBOMemoryChanged(); + void glContextSwapchainMemoryChanged(); void qmlTextureMemoryChanged(); void gpuBuffersChanged(); void gpuTexturesChanged(); diff --git a/libraries/gl/src/gl/Context.cpp b/libraries/gl/src/gl/Context.cpp index fda457f94a..f748703506 100644 --- a/libraries/gl/src/gl/Context.cpp +++ b/libraries/gl/src/gl/Context.cpp @@ -41,22 +41,22 @@ static bool enableDebugLogger = QProcessEnvironment::systemEnvironment().contain using namespace gl; -std::atomic Context::_defaultFBOMemoryUsage { 0 }; +std::atomic Context::_totalSwapchainMemoryUsage { 0 }; -size_t Context::getDefaultFBOMemoryUsage() { return _defaultFBOMemoryUsage.load(); } +size_t Context::getSwapchainMemoryUsage() { return _totalSwapchainMemoryUsage.load(); } -size_t Context::evalMemoryUsage(uint32_t width, uint32_t height) { - return width * height * 4; +size_t Context::evalSurfaceMemoryUsage(uint32_t width, uint32_t height, uint32_t pixelSize) { + return width * height * pixelSize; } -void Context::updateDefaultFBOMemoryUsage(size_t prevFBOSize, size_t newFBOSize) { - if (prevFBOSize == newFBOSize) { +void Context::updateSwapchainMemoryUsage(size_t prevSize, size_t newSize) { + if (prevSize == newSize) { return; } - if (newFBOSize > prevFBOSize) { - _defaultFBOMemoryUsage.fetch_add(newFBOSize - prevFBOSize); + if (newSize > prevSize) { + _totalSwapchainMemoryUsage.fetch_add(newSize - prevSize); } else { - _defaultFBOMemoryUsage.fetch_sub(prevFBOSize - newFBOSize); + _totalSwapchainMemoryUsage.fetch_sub(prevSize - newSize); } } @@ -99,18 +99,35 @@ void Context::release() { if (PRIMARY == this) { PRIMARY = nullptr; } - } + updateSwapchainMemoryCounter(); +} Context::~Context() { release(); } +void Context::updateSwapchainMemoryCounter() { + if (_window) { + auto newSize = _window->size(); + auto newMemSize = gl::Context::evalSurfaceMemoryUsage(newSize.width(), newSize.height(), _swapchainPixelSize); + gl::Context::updateSwapchainMemoryUsage(_swapchainMemoryUsage, newMemSize); + _swapchainMemoryUsage = newMemSize; + } else { + // No window ? no more swapchain + gl::Context::updateSwapchainMemoryUsage(_swapchainMemoryUsage, 0); + _swapchainMemoryUsage = 0; + } +} + void Context::setWindow(QWindow* window) { release(); _window = window; + #ifdef Q_OS_WIN _hwnd = (HWND)window->winId(); #endif + + updateSwapchainMemoryCounter(); } #ifdef Q_OS_WIN @@ -119,6 +136,8 @@ static const char* PRIMARY_CONTEXT_PROPERTY_NAME = "com.highfidelity.gl.primaryC bool Context::makeCurrent() { BOOL result = wglMakeCurrent(_hdc, _hglrc); assert(result); + updateSwapchainMemoryCounter(); + return result; } @@ -238,6 +257,10 @@ void Context::create() { wglChoosePixelFormatARB(_hdc, &formatAttribs[0], NULL, 1, &pixelFormat, &numFormats); DescribePixelFormat(_hdc, pixelFormat, sizeof(pfd), &pfd); } + // The swap chain pixel size for swap chains is : rgba32 * double buffer + depth24stencil8 + _swapchainPixelSize = 32 * 2 + 32; + updateSwapchainMemoryCounter(); + SetPixelFormat(_hdc, pixelFormat, &pfd); { std::vector contextAttribs; diff --git a/libraries/gl/src/gl/Context.h b/libraries/gl/src/gl/Context.h index 783b634f75..d1b0d53631 100644 --- a/libraries/gl/src/gl/Context.h +++ b/libraries/gl/src/gl/Context.h @@ -59,12 +59,16 @@ namespace gl { 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); + static size_t evalSurfaceMemoryUsage(uint32_t width, uint32_t height, uint32_t pixelSize); + static size_t getSwapchainMemoryUsage(); + static void updateSwapchainMemoryUsage(size_t prevSize, size_t newSize); private: - static std::atomic _defaultFBOMemoryUsage; + static std::atomic _totalSwapchainMemoryUsage; + + size_t _swapchainMemoryUsage { 0 }; + size_t _swapchainPixelSize { 0 }; + void updateSwapchainMemoryCounter(); }; class OffscreenContext : public Context { diff --git a/libraries/gl/src/gl/ContextQt.cpp b/libraries/gl/src/gl/ContextQt.cpp index ff59b8f1e1..5021bddc7f 100644 --- a/libraries/gl/src/gl/ContextQt.cpp +++ b/libraries/gl/src/gl/ContextQt.cpp @@ -45,6 +45,7 @@ void Context::moveToThread(QThread* thread) { #ifndef Q_OS_WIN bool Context::makeCurrent() { + updateSwapchainMemoryCounter(); return _context->makeCurrent(_window); } @@ -70,6 +71,9 @@ void Context::create() { } _context->setFormat(getDefaultOpenGLSurfaceFormat()); _context->create(); + + _swapchainPixelSize = evalGLFormatSwapchainPixelSize(_context->format()); + updateSwapchainMemoryCounter(); } #endif diff --git a/libraries/gl/src/gl/GLHelpers.cpp b/libraries/gl/src/gl/GLHelpers.cpp index dde1e0ec97..e845c402be 100644 --- a/libraries/gl/src/gl/GLHelpers.cpp +++ b/libraries/gl/src/gl/GLHelpers.cpp @@ -13,6 +13,15 @@ #include +size_t evalGLFormatSwapchainPixelSize(const QSurfaceFormat& format) { + size_t pixelSize = format.redBufferSize() + format.greenBufferSize() + format.blueBufferSize() + format.alphaBufferSize(); + if (format.swapBehavior() > 0) { + pixelSize *= format.swapBehavior(); // multiply the color buffer pixel size by the actual swapchain depth + } + pixelSize += format.stencilBufferSize() + format.depthBufferSize(); + return pixelSize; +} + const QSurfaceFormat& getDefaultOpenGLSurfaceFormat() { static QSurfaceFormat format; static std::once_flag once; diff --git a/libraries/gl/src/gl/GLHelpers.h b/libraries/gl/src/gl/GLHelpers.h index 8834a718fd..daa181467d 100644 --- a/libraries/gl/src/gl/GLHelpers.h +++ b/libraries/gl/src/gl/GLHelpers.h @@ -26,6 +26,8 @@ class QGLFormat; template void setGLFormatVersion(F& format, int major = 4, int minor = 5) { format.setVersion(major, minor); } +size_t evalGLFormatSwapchainPixelSize(const QSurfaceFormat& format); + const QSurfaceFormat& getDefaultOpenGLSurfaceFormat(); QJsonObject getGLContextData(); int glVersionToInteger(QString glVersion); diff --git a/libraries/gl/src/gl/OffscreenGLCanvas.cpp b/libraries/gl/src/gl/OffscreenGLCanvas.cpp index ac12f95c25..e54846196b 100644 --- a/libraries/gl/src/gl/OffscreenGLCanvas.cpp +++ b/libraries/gl/src/gl/OffscreenGLCanvas.cpp @@ -24,8 +24,7 @@ OffscreenGLCanvas::OffscreenGLCanvas() : _context(new QOpenGLContext), - _offscreenSurface(new QOffscreenSurface), - _offscreenSurfaceCurrentMemoryUsage(0) + _offscreenSurface(new QOffscreenSurface) { } @@ -35,8 +34,6 @@ OffscreenGLCanvas::~OffscreenGLCanvas() { delete _context; _context = nullptr; - gl::Context::updateDefaultFBOMemoryUsage(_offscreenSurfaceCurrentMemoryUsage, 0); - _offscreenSurface->destroy(); delete _offscreenSurface; _offscreenSurface = nullptr; @@ -60,15 +57,6 @@ 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); @@ -79,8 +67,6 @@ 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 be7d8986f6..583aa7c60f 100644 --- a/libraries/gl/src/gl/OffscreenGLCanvas.h +++ b/libraries/gl/src/gl/OffscreenGLCanvas.h @@ -36,8 +36,6 @@ protected: std::once_flag _reportOnce; QOpenGLContext* _context{ nullptr }; QOffscreenSurface* _offscreenSurface{ nullptr }; - size_t _offscreenSurfaceCurrentMemoryUsage { 0 }; - void updateMemoryCounter(); }; #endif // hifi_OffscreenGLCanvas_h diff --git a/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp b/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp index cd9807fd46..9a19d6f309 100644 --- a/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp +++ b/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp @@ -186,6 +186,7 @@ GLTexture::~GLTexture() { Backend::updateTextureGPUFramebufferMemoryUsage(_size, 0); } } + Backend::updateTextureGPUVirtualMemoryUsage(_virtualSize, 0); } void GLTexture::createTexture() { From c586bef45216c0379929b7dedab531e57e1ed5a9 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 25 Oct 2016 12:56:26 -0700 Subject: [PATCH 4/5] Actually remove the multiplication of the color buffer cost by the swapchain length in the pixel SIze estimation since it doesn;t look true --- libraries/gl/src/gl/Context.cpp | 7 ++++--- libraries/gl/src/gl/GLHelpers.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libraries/gl/src/gl/Context.cpp b/libraries/gl/src/gl/Context.cpp index f748703506..ec1c284e99 100644 --- a/libraries/gl/src/gl/Context.cpp +++ b/libraries/gl/src/gl/Context.cpp @@ -109,7 +109,7 @@ Context::~Context() { void Context::updateSwapchainMemoryCounter() { if (_window) { auto newSize = _window->size(); - auto newMemSize = gl::Context::evalSurfaceMemoryUsage(newSize.width(), newSize.height(), _swapchainPixelSize); + auto newMemSize = gl::Context::evalSurfaceMemoryUsage(newSize.width(), newSize.height(), (uint32_t) _swapchainPixelSize); gl::Context::updateSwapchainMemoryUsage(_swapchainMemoryUsage, newMemSize); _swapchainMemoryUsage = newMemSize; } else { @@ -257,8 +257,9 @@ void Context::create() { wglChoosePixelFormatARB(_hdc, &formatAttribs[0], NULL, 1, &pixelFormat, &numFormats); DescribePixelFormat(_hdc, pixelFormat, sizeof(pfd), &pfd); } - // The swap chain pixel size for swap chains is : rgba32 * double buffer + depth24stencil8 - _swapchainPixelSize = 32 * 2 + 32; + // The swap chain pixel size for swap chains is : rgba32 + depth24stencil8 + // We don't apply the length of the swap chain into this pixelSize since it is not vsible for the Process (on windows). + _swapchainPixelSize = 32 + 32; updateSwapchainMemoryCounter(); SetPixelFormat(_hdc, pixelFormat, &pfd); diff --git a/libraries/gl/src/gl/GLHelpers.cpp b/libraries/gl/src/gl/GLHelpers.cpp index e845c402be..02c1f308de 100644 --- a/libraries/gl/src/gl/GLHelpers.cpp +++ b/libraries/gl/src/gl/GLHelpers.cpp @@ -15,9 +15,11 @@ size_t evalGLFormatSwapchainPixelSize(const QSurfaceFormat& format) { size_t pixelSize = format.redBufferSize() + format.greenBufferSize() + format.blueBufferSize() + format.alphaBufferSize(); - if (format.swapBehavior() > 0) { - pixelSize *= format.swapBehavior(); // multiply the color buffer pixel size by the actual swapchain depth - } + // We don't apply the length of the swap chain into this pixelSize since it is not vsible for the Process (on windows). + // Let s keep this here remember that: + // if (format.swapBehavior() > 0) { + // pixelSize *= format.swapBehavior(); // multiply the color buffer pixel size by the actual swapchain depth + // } pixelSize += format.stencilBufferSize() + format.depthBufferSize(); return pixelSize; } From 133b0b38fdd20d140eab6e10eb2024649dbd679b Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 25 Oct 2016 13:24:39 -0700 Subject: [PATCH 5/5] Fixing build issue on non win --- libraries/gl/src/gl/ContextQt.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/gl/src/gl/ContextQt.cpp b/libraries/gl/src/gl/ContextQt.cpp index 5021bddc7f..4d5d2c4e9f 100644 --- a/libraries/gl/src/gl/ContextQt.cpp +++ b/libraries/gl/src/gl/ContextQt.cpp @@ -15,6 +15,8 @@ #include #endif +#include "GLHelpers.h" + using namespace gl; void Context::destroyContext(QOpenGLContext* context) {