Browse Source

Cleaned up texture VRAM usage tracking.

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
b019a4a64c

+ 18 - 0
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)

+ 5 - 2
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);
 

+ 0 - 1
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;

+ 0 - 2
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);

+ 12 - 0
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;

+ 9 - 3
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> quad;
 
+	int64 graphicsMemorySize;
+
 private:
 
 	static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[];

+ 16 - 21
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;
+
+	if (actualSamples > 1 && isReadable())
+		memsize += getPixelFormatSize(format) * pixelWidth * pixelHeight * actualSamples;
+	else if (actualSamples > 1)
+		memsize *= actualSamples;
 
-	gl.updateTextureMemorySize(prevmemsize, textureMemory);
+	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)

+ 0 - 2
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];
 

+ 3 - 18
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

+ 1 - 1
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);

+ 5 - 7
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

+ 0 - 6
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;

+ 2 - 6
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+.
 	 **/