From b019a4a64c2a3e34e14dba401339a15cd527b0a6 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 6 May 2017 23:56:10 -0300 Subject: [PATCH] Cleaned up texture VRAM usage tracking. --HG-- branch : minor --- src/modules/graphics/Graphics.cpp | 18 ++++++++++++ src/modules/graphics/Graphics.h | 7 +++-- src/modules/graphics/Image.cpp | 1 - src/modules/graphics/Image.h | 2 -- src/modules/graphics/Texture.cpp | 12 ++++++++ src/modules/graphics/Texture.h | 12 ++++++-- src/modules/graphics/opengl/Canvas.cpp | 37 ++++++++++-------------- src/modules/graphics/opengl/Canvas.h | 2 -- src/modules/graphics/opengl/Graphics.cpp | 21 ++------------ src/modules/graphics/opengl/Graphics.h | 2 +- src/modules/graphics/opengl/Image.cpp | 12 ++++---- src/modules/graphics/opengl/OpenGL.cpp | 6 ---- src/modules/graphics/opengl/OpenGL.h | 8 ++--- 13 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index 5adc53145..30337f113 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -1113,6 +1113,24 @@ void Graphics::polygon(DrawMode mode, const float *coords, size_t count) } } +Graphics::Stats Graphics::getStats() const +{ + Stats stats; + + getAPIStats(stats.drawCalls, stats.shaderSwitches); + + if (streamBufferState.vertexCount > 0) + stats.drawCalls++; + + stats.canvasSwitches = canvasSwitchCount; + stats.canvases = Canvas::canvasCount; + stats.images = Image::imageCount; + stats.fonts = Font::fontCount; + stats.textureMemory = Texture::totalGraphicsMemory; + + return stats; +} + void Graphics::push(StackType type) { if (stackTypeStack.size() == MAX_USER_STACK_DEPTH) diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 9cba834f3..51f7ebe7b 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -27,6 +27,7 @@ #include "common/StringMap.h" #include "common/Vector.h" #include "common/Optional.h" +#include "common/int.h" #include "StreamBuffer.h" #include "vertex.h" #include "Color.h" @@ -208,7 +209,7 @@ public: int canvases; int images; int fonts; - size_t textureMemory; + int64 textureMemory; }; struct ColorMask @@ -725,7 +726,7 @@ public: /** * Returns performance-related statistics. **/ - virtual Stats getStats() const = 0; + Stats getStats() const; void push(StackType type = STACK_TRANSFORM); void pop(); @@ -855,6 +856,8 @@ protected: virtual StreamBuffer *newStreamBuffer(BufferType type, size_t size) = 0; + virtual void getAPIStats(int &drawcalls, int &shaderswitches) const = 0; + void restoreState(const DisplayState &s); void restoreStateChecked(const DisplayState &s); diff --git a/src/modules/graphics/Image.cpp b/src/modules/graphics/Image.cpp index 1e639c3b0..0ff8489a8 100644 --- a/src/modules/graphics/Image.cpp +++ b/src/modules/graphics/Image.cpp @@ -40,7 +40,6 @@ Image::Image(const Slices &data, const Settings &settings, bool validatedata) , mipmapsType(settings.mipmaps ? MIPMAPS_GENERATED : MIPMAPS_NONE) , sRGB(isGammaCorrect() && !settings.linear) , usingDefaultTexture(false) - , textureMemorySize(0) { if (validatedata && data.validate() == MIPMAPS_DATA) mipmapsType = MIPMAPS_DATA; diff --git a/src/modules/graphics/Image.h b/src/modules/graphics/Image.h index ea6274908..349e8d563 100644 --- a/src/modules/graphics/Image.h +++ b/src/modules/graphics/Image.h @@ -116,8 +116,6 @@ protected: // back to a default texture. bool usingDefaultTexture; - size_t textureMemorySize; - private: Image(const Slices &data, const Settings &settings, bool validatedata); diff --git a/src/modules/graphics/Texture.cpp b/src/modules/graphics/Texture.cpp index e51329335..377155e90 100644 --- a/src/modules/graphics/Texture.cpp +++ b/src/modules/graphics/Texture.cpp @@ -47,6 +47,7 @@ love::Type Texture::type("Texture", &Drawable::type); Texture::Filter Texture::defaultFilter; Texture::FilterMode Texture::defaultMipmapFilter = Texture::FILTER_LINEAR; float Texture::defaultMipmapSharpness = 0.0f; +int64 Texture::totalGraphicsMemory = 0; Texture::Texture(TextureType texType) : texType(texType) @@ -62,11 +63,13 @@ Texture::Texture(TextureType texType) , filter(defaultFilter) , wrap() , mipmapSharpness(defaultMipmapSharpness) + , graphicsMemorySize(0) { } Texture::~Texture() { + setGraphicsMemorySize(0); } void Texture::initQuad() @@ -75,6 +78,15 @@ void Texture::initQuad() quad.set(new Quad(v, width, height), Acquire::NORETAIN); } +void Texture::setGraphicsMemorySize(int64 bytes) +{ + totalGraphicsMemory = std::max(totalGraphicsMemory - graphicsMemorySize, 0LL); + + bytes = std::max(bytes, 0LL); + graphicsMemorySize = bytes; + totalGraphicsMemory += bytes; +} + TextureType Texture::getTextureType() const { return texType; diff --git a/src/modules/graphics/Texture.h b/src/modules/graphics/Texture.h index f997f8a21..7d5b20180 100644 --- a/src/modules/graphics/Texture.h +++ b/src/modules/graphics/Texture.h @@ -27,6 +27,7 @@ #include "common/pixelformat.h" #include "common/Exception.h" #include "common/Optional.h" +#include "common/int.h" #include "Drawable.h" #include "Quad.h" #include "vertex.h" @@ -101,13 +102,15 @@ public: WrapMode r = WRAP_CLAMP; }; - Texture(TextureType texType); - virtual ~Texture(); - static Filter defaultFilter; static FilterMode defaultMipmapFilter; static float defaultMipmapSharpness; + static int64 totalGraphicsMemory; + + Texture(TextureType texType); + virtual ~Texture(); + // Drawable. void draw(Graphics *gfx, const Matrix4 &m) override; @@ -169,6 +172,7 @@ public: protected: void initQuad(); + void setGraphicsMemorySize(int64 size); TextureType texType; @@ -194,6 +198,8 @@ protected: StrongRef quad; + int64 graphicsMemorySize; + private: static StringMap::Entry texTypeEntries[]; diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index 4908b01d8..85af00357 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -158,7 +158,6 @@ Canvas::Canvas(const Settings &settings) , texture(0) , renderbuffer(0) , actualSamples(0) - , textureMemory(0) { format = getSizedFormat(format); @@ -245,6 +244,13 @@ bool Canvas::loadVolatile() renderbuffer = 0; status = GL_FRAMEBUFFER_COMPLETE; + // getMaxRenderbufferSamples will be 0 on systems that don't support + // multisampled renderbuffers / don't export FBO multisample extensions. + actualSamples = getRequestedMSAA(); + actualSamples = std::min(actualSamples, gl.getMaxRenderbufferSamples()); + actualSamples = std::max(actualSamples, 0); + actualSamples = actualSamples == 1 ? 0 : actualSamples; + if (isReadable()) { glGenTextures(1, &texture); @@ -292,29 +298,19 @@ bool Canvas::loadVolatile() } } - // getMaxRenderbufferSamples will be 0 on systems that don't support - // multisampled renderbuffers / don't export FBO multisample extensions. - actualSamples = getRequestedMSAA(); - actualSamples = std::min(actualSamples, gl.getMaxRenderbufferSamples()); - actualSamples = std::max(actualSamples, 0); - actualSamples = actualSamples == 1 ? 0 : actualSamples; - if (!isReadable() || actualSamples > 0) createRenderbuffer(pixelWidth, pixelHeight, actualSamples, format, renderbuffer); - size_t prevmemsize = textureMemory; - - textureMemory = getPixelFormatSize(format) * pixelWidth * pixelHeight; - - if (actualSamples > 0 && isReadable()) - textureMemory += (textureMemory * actualSamples); - else if (actualSamples > 0) - textureMemory *= actualSamples; - + int64 memsize = getPixelFormatSize(format) * pixelWidth * pixelHeight; if (getMipmapCount() > 1) - textureMemory *= 1.33334; + memsize *= 1.33334; - gl.updateTextureMemorySize(prevmemsize, textureMemory); + if (actualSamples > 1 && isReadable()) + memsize += getPixelFormatSize(format) * pixelWidth * pixelHeight * actualSamples; + else if (actualSamples > 1) + memsize *= actualSamples; + + setGraphicsMemorySize(memsize); return true; } @@ -334,8 +330,7 @@ void Canvas::unloadVolatile() renderbuffer = 0; texture = 0; - gl.updateTextureMemorySize(textureMemory, 0); - textureMemory = 0; + setGraphicsMemorySize(0); } void Canvas::setFilter(const Texture::Filter &f) diff --git a/src/modules/graphics/opengl/Canvas.h b/src/modules/graphics/opengl/Canvas.h index 07e1aa4a0..71010184d 100644 --- a/src/modules/graphics/opengl/Canvas.h +++ b/src/modules/graphics/opengl/Canvas.h @@ -112,8 +112,6 @@ private: int actualSamples; - size_t textureMemory; - static SupportedFormat supportedFormats[PIXELFORMAT_MAX_ENUM]; static SupportedFormat checkedFormats[PIXELFORMAT_MAX_ENUM]; diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 7988d78f4..c7984b619 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -126,7 +126,6 @@ love::graphics::Canvas *Graphics::newCanvas(const Canvas::Settings &settings) return new Canvas(settings); } - love::graphics::Shader *Graphics::newShader(const Shader::ShaderSource &source) { return new Shader(source); @@ -1446,24 +1445,10 @@ Graphics::RendererInfo Graphics::getRendererInfo() const return info; } -Graphics::Stats Graphics::getStats() const +void Graphics::getAPIStats(int &drawcalls, int &shaderswitches) const { - int drawcalls = gl.stats.drawCalls; - - if (streamBufferState.vertexCount > 0) - drawcalls++; - - Stats stats; - - stats.drawCalls = drawcalls; - stats.canvasSwitches = canvasSwitchCount; - stats.shaderSwitches = gl.stats.shaderSwitches; - stats.canvases = Canvas::canvasCount; - stats.images = Image::imageCount; - stats.fonts = Font::fontCount; - stats.textureMemory = gl.stats.textureMemory; - - return stats; + drawcalls = gl.stats.drawCalls; + shaderswitches = gl.stats.shaderSwitches; } double Graphics::getSystemLimit(SystemLimit limittype) const diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 724f1beaf..9cf7db95a 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -126,13 +126,13 @@ public: bool isImageFormatSupported(PixelFormat format) const override; Renderer getRenderer() const override; RendererInfo getRendererInfo() const override; - Stats getStats() const override; Shader::Language getShaderLanguageTarget() const override; private: love::graphics::StreamBuffer *newStreamBuffer(BufferType type, size_t size) override; + void getAPIStats(int &drawcalls, int &shaderswitches) const override; void endPass(); void bindCachedFBO(const RenderTargets &targets); diff --git a/src/modules/graphics/opengl/Image.cpp b/src/modules/graphics/opengl/Image.cpp index 2a94a9a56..42c940e18 100644 --- a/src/modules/graphics/opengl/Image.cpp +++ b/src/modules/graphics/opengl/Image.cpp @@ -268,16 +268,15 @@ bool Image::loadVolatile() throw; } - size_t prevmemsize = textureMemorySize; - textureMemorySize = 0; + int64 memsize = 0; for (int slice = 0; slice < data.getSliceCount(0); slice++) - textureMemorySize += data.get(slice, 0)->getSize(); + memsize += data.get(slice, 0)->getSize(); if (getMipmapCount() > 1) - textureMemorySize *= 1.33334; + memsize *= 1.33334; - gl.updateTextureMemorySize(prevmemsize, textureMemorySize); + setGraphicsMemorySize(memsize); usingDefaultTexture = false; return true; @@ -291,8 +290,7 @@ void Image::unloadVolatile() gl.deleteTexture(texture); texture = 0; - gl.updateTextureMemorySize(textureMemorySize, 0); - textureMemorySize = 0; + setGraphicsMemorySize(0); } ptrdiff_t Image::getHandle() const diff --git a/src/modules/graphics/opengl/OpenGL.cpp b/src/modules/graphics/opengl/OpenGL.cpp index 5daec2ef6..158a124d2 100644 --- a/src/modules/graphics/opengl/OpenGL.cpp +++ b/src/modules/graphics/opengl/OpenGL.cpp @@ -1181,12 +1181,6 @@ float OpenGL::getMaxLODBias() const return maxLODBias; } -void OpenGL::updateTextureMemorySize(size_t oldsize, size_t newsize) -{ - int64 memsize = (int64) stats.textureMemory + ((int64) newsize - (int64) oldsize); - stats.textureMemory = (size_t) std::max(memsize, (int64) 0); -} - bool OpenGL::isCoreProfile() const { return coreProfile; diff --git a/src/modules/graphics/opengl/OpenGL.h b/src/modules/graphics/opengl/OpenGL.h index fb52686ab..b459ef8b2 100644 --- a/src/modules/graphics/opengl/OpenGL.h +++ b/src/modules/graphics/opengl/OpenGL.h @@ -114,9 +114,8 @@ public: struct Stats { - size_t textureMemory; - int drawCalls; - int shaderSwitches; + int drawCalls; + int shaderSwitches; } stats; struct Bugs @@ -366,9 +365,6 @@ public: float getMaxLODBias() const; - - void updateTextureMemorySize(size_t oldsize, size_t newsize); - /** * Gets whether the context is Core Profile OpenGL 3.2+. **/