Cleaned up texture VRAM usage tracking.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-05-06 23:56:10 -03:00
parent b590edf27d
commit b019a4a64c
13 changed files with 71 additions and 69 deletions
+18
View File
@@ -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) void Graphics::push(StackType type)
{ {
if (stackTypeStack.size() == MAX_USER_STACK_DEPTH) if (stackTypeStack.size() == MAX_USER_STACK_DEPTH)
+5 -2
View File
@@ -27,6 +27,7 @@
#include "common/StringMap.h" #include "common/StringMap.h"
#include "common/Vector.h" #include "common/Vector.h"
#include "common/Optional.h" #include "common/Optional.h"
#include "common/int.h"
#include "StreamBuffer.h" #include "StreamBuffer.h"
#include "vertex.h" #include "vertex.h"
#include "Color.h" #include "Color.h"
@@ -208,7 +209,7 @@ public:
int canvases; int canvases;
int images; int images;
int fonts; int fonts;
size_t textureMemory; int64 textureMemory;
}; };
struct ColorMask struct ColorMask
@@ -725,7 +726,7 @@ public:
/** /**
* Returns performance-related statistics. * Returns performance-related statistics.
**/ **/
virtual Stats getStats() const = 0; Stats getStats() const;
void push(StackType type = STACK_TRANSFORM); void push(StackType type = STACK_TRANSFORM);
void pop(); void pop();
@@ -855,6 +856,8 @@ protected:
virtual StreamBuffer *newStreamBuffer(BufferType type, size_t size) = 0; virtual StreamBuffer *newStreamBuffer(BufferType type, size_t size) = 0;
virtual void getAPIStats(int &drawcalls, int &shaderswitches) const = 0;
void restoreState(const DisplayState &s); void restoreState(const DisplayState &s);
void restoreStateChecked(const DisplayState &s); void restoreStateChecked(const DisplayState &s);
-1
View File
@@ -40,7 +40,6 @@ Image::Image(const Slices &data, const Settings &settings, bool validatedata)
, mipmapsType(settings.mipmaps ? MIPMAPS_GENERATED : MIPMAPS_NONE) , mipmapsType(settings.mipmaps ? MIPMAPS_GENERATED : MIPMAPS_NONE)
, sRGB(isGammaCorrect() && !settings.linear) , sRGB(isGammaCorrect() && !settings.linear)
, usingDefaultTexture(false) , usingDefaultTexture(false)
, textureMemorySize(0)
{ {
if (validatedata && data.validate() == MIPMAPS_DATA) if (validatedata && data.validate() == MIPMAPS_DATA)
mipmapsType = MIPMAPS_DATA; mipmapsType = MIPMAPS_DATA;
-2
View File
@@ -116,8 +116,6 @@ protected:
// back to a default texture. // back to a default texture.
bool usingDefaultTexture; bool usingDefaultTexture;
size_t textureMemorySize;
private: private:
Image(const Slices &data, const Settings &settings, bool validatedata); Image(const Slices &data, const Settings &settings, bool validatedata);
+12
View File
@@ -47,6 +47,7 @@ love::Type Texture::type("Texture", &Drawable::type);
Texture::Filter Texture::defaultFilter; Texture::Filter Texture::defaultFilter;
Texture::FilterMode Texture::defaultMipmapFilter = Texture::FILTER_LINEAR; Texture::FilterMode Texture::defaultMipmapFilter = Texture::FILTER_LINEAR;
float Texture::defaultMipmapSharpness = 0.0f; float Texture::defaultMipmapSharpness = 0.0f;
int64 Texture::totalGraphicsMemory = 0;
Texture::Texture(TextureType texType) Texture::Texture(TextureType texType)
: texType(texType) : texType(texType)
@@ -62,11 +63,13 @@ Texture::Texture(TextureType texType)
, filter(defaultFilter) , filter(defaultFilter)
, wrap() , wrap()
, mipmapSharpness(defaultMipmapSharpness) , mipmapSharpness(defaultMipmapSharpness)
, graphicsMemorySize(0)
{ {
} }
Texture::~Texture() Texture::~Texture()
{ {
setGraphicsMemorySize(0);
} }
void Texture::initQuad() void Texture::initQuad()
@@ -75,6 +78,15 @@ void Texture::initQuad()
quad.set(new Quad(v, width, height), Acquire::NORETAIN); 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 TextureType Texture::getTextureType() const
{ {
return texType; return texType;
+9 -3
View File
@@ -27,6 +27,7 @@
#include "common/pixelformat.h" #include "common/pixelformat.h"
#include "common/Exception.h" #include "common/Exception.h"
#include "common/Optional.h" #include "common/Optional.h"
#include "common/int.h"
#include "Drawable.h" #include "Drawable.h"
#include "Quad.h" #include "Quad.h"
#include "vertex.h" #include "vertex.h"
@@ -101,13 +102,15 @@ public:
WrapMode r = WRAP_CLAMP; WrapMode r = WRAP_CLAMP;
}; };
Texture(TextureType texType);
virtual ~Texture();
static Filter defaultFilter; static Filter defaultFilter;
static FilterMode defaultMipmapFilter; static FilterMode defaultMipmapFilter;
static float defaultMipmapSharpness; static float defaultMipmapSharpness;
static int64 totalGraphicsMemory;
Texture(TextureType texType);
virtual ~Texture();
// Drawable. // Drawable.
void draw(Graphics *gfx, const Matrix4 &m) override; void draw(Graphics *gfx, const Matrix4 &m) override;
@@ -169,6 +172,7 @@ public:
protected: protected:
void initQuad(); void initQuad();
void setGraphicsMemorySize(int64 size);
TextureType texType; TextureType texType;
@@ -194,6 +198,8 @@ protected:
StrongRef<Quad> quad; StrongRef<Quad> quad;
int64 graphicsMemorySize;
private: private:
static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[]; static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[];
+16 -21
View File
@@ -158,7 +158,6 @@ Canvas::Canvas(const Settings &settings)
, texture(0) , texture(0)
, renderbuffer(0) , renderbuffer(0)
, actualSamples(0) , actualSamples(0)
, textureMemory(0)
{ {
format = getSizedFormat(format); format = getSizedFormat(format);
@@ -245,6 +244,13 @@ bool Canvas::loadVolatile()
renderbuffer = 0; renderbuffer = 0;
status = GL_FRAMEBUFFER_COMPLETE; 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()) if (isReadable())
{ {
glGenTextures(1, &texture); 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) if (!isReadable() || actualSamples > 0)
createRenderbuffer(pixelWidth, pixelHeight, actualSamples, format, renderbuffer); createRenderbuffer(pixelWidth, pixelHeight, actualSamples, format, renderbuffer);
size_t prevmemsize = textureMemory; int64 memsize = getPixelFormatSize(format) * pixelWidth * pixelHeight;
textureMemory = getPixelFormatSize(format) * pixelWidth * pixelHeight;
if (actualSamples > 0 && isReadable())
textureMemory += (textureMemory * actualSamples);
else if (actualSamples > 0)
textureMemory *= actualSamples;
if (getMipmapCount() > 1) 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; return true;
} }
@@ -334,8 +330,7 @@ void Canvas::unloadVolatile()
renderbuffer = 0; renderbuffer = 0;
texture = 0; texture = 0;
gl.updateTextureMemorySize(textureMemory, 0); setGraphicsMemorySize(0);
textureMemory = 0;
} }
void Canvas::setFilter(const Texture::Filter &f) void Canvas::setFilter(const Texture::Filter &f)
-2
View File
@@ -112,8 +112,6 @@ private:
int actualSamples; int actualSamples;
size_t textureMemory;
static SupportedFormat supportedFormats[PIXELFORMAT_MAX_ENUM]; static SupportedFormat supportedFormats[PIXELFORMAT_MAX_ENUM];
static SupportedFormat checkedFormats[PIXELFORMAT_MAX_ENUM]; static SupportedFormat checkedFormats[PIXELFORMAT_MAX_ENUM];
+3 -18
View File
@@ -126,7 +126,6 @@ love::graphics::Canvas *Graphics::newCanvas(const Canvas::Settings &settings)
return new Canvas(settings); return new Canvas(settings);
} }
love::graphics::Shader *Graphics::newShader(const Shader::ShaderSource &source) love::graphics::Shader *Graphics::newShader(const Shader::ShaderSource &source)
{ {
return new Shader(source); return new Shader(source);
@@ -1446,24 +1445,10 @@ Graphics::RendererInfo Graphics::getRendererInfo() const
return info; return info;
} }
Graphics::Stats Graphics::getStats() const void Graphics::getAPIStats(int &drawcalls, int &shaderswitches) const
{ {
int drawcalls = gl.stats.drawCalls; drawcalls = gl.stats.drawCalls;
shaderswitches = gl.stats.shaderSwitches;
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;
} }
double Graphics::getSystemLimit(SystemLimit limittype) const double Graphics::getSystemLimit(SystemLimit limittype) const
+1 -1
View File
@@ -126,13 +126,13 @@ public:
bool isImageFormatSupported(PixelFormat format) const override; bool isImageFormatSupported(PixelFormat format) const override;
Renderer getRenderer() const override; Renderer getRenderer() const override;
RendererInfo getRendererInfo() const override; RendererInfo getRendererInfo() const override;
Stats getStats() const override;
Shader::Language getShaderLanguageTarget() const override; Shader::Language getShaderLanguageTarget() const override;
private: private:
love::graphics::StreamBuffer *newStreamBuffer(BufferType type, size_t size) override; love::graphics::StreamBuffer *newStreamBuffer(BufferType type, size_t size) override;
void getAPIStats(int &drawcalls, int &shaderswitches) const override;
void endPass(); void endPass();
void bindCachedFBO(const RenderTargets &targets); void bindCachedFBO(const RenderTargets &targets);
+5 -7
View File
@@ -268,16 +268,15 @@ bool Image::loadVolatile()
throw; throw;
} }
size_t prevmemsize = textureMemorySize; int64 memsize = 0;
textureMemorySize = 0;
for (int slice = 0; slice < data.getSliceCount(0); slice++) for (int slice = 0; slice < data.getSliceCount(0); slice++)
textureMemorySize += data.get(slice, 0)->getSize(); memsize += data.get(slice, 0)->getSize();
if (getMipmapCount() > 1) if (getMipmapCount() > 1)
textureMemorySize *= 1.33334; memsize *= 1.33334;
gl.updateTextureMemorySize(prevmemsize, textureMemorySize); setGraphicsMemorySize(memsize);
usingDefaultTexture = false; usingDefaultTexture = false;
return true; return true;
@@ -291,8 +290,7 @@ void Image::unloadVolatile()
gl.deleteTexture(texture); gl.deleteTexture(texture);
texture = 0; texture = 0;
gl.updateTextureMemorySize(textureMemorySize, 0); setGraphicsMemorySize(0);
textureMemorySize = 0;
} }
ptrdiff_t Image::getHandle() const ptrdiff_t Image::getHandle() const
-6
View File
@@ -1181,12 +1181,6 @@ float OpenGL::getMaxLODBias() const
return maxLODBias; 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 bool OpenGL::isCoreProfile() const
{ {
return coreProfile; return coreProfile;
+2 -6
View File
@@ -114,9 +114,8 @@ public:
struct Stats struct Stats
{ {
size_t textureMemory; int drawCalls;
int drawCalls; int shaderSwitches;
int shaderSwitches;
} stats; } stats;
struct Bugs struct Bugs
@@ -366,9 +365,6 @@ public:
float getMaxLODBias() const; float getMaxLODBias() const;
void updateTextureMemorySize(size_t oldsize, size_t newsize);
/** /**
* Gets whether the context is Core Profile OpenGL 3.2+. * Gets whether the context is Core Profile OpenGL 3.2+.
**/ **/