From 507d3e5a39605c620d4b9d7aa2c1f5e9ed85c656 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 25 Oct 2016 12:28:07 -0700 Subject: [PATCH] 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() {