diff --git a/libraries/gpu/src/gpu/Context.cpp b/libraries/gpu/src/gpu/Context.cpp index 6148994a1b..b14c461bc5 100644 --- a/libraries/gpu/src/gpu/Context.cpp +++ b/libraries/gpu/src/gpu/Context.cpp @@ -107,3 +107,68 @@ Backend::TransformCamera Backend::TransformCamera::getEyeCamera(int eye, const S return result; } + +// Counters for Buffer and Texture usage in GPU/Context +std::atomic Context::_bufferGPUCount{ 0 }; +std::atomic Context::_bufferGPUMemoryUsage{ 0 }; + +std::atomic Context::_textureGPUCount{ 0 }; +std::atomic Context::_textureGPUMemoryUsage{ 0 }; + +void Context::incrementBufferGPUCount() { + _bufferGPUCount++; +} +void Context::decrementBufferGPUCount() { + _bufferGPUCount--; +} +void Context::updateBufferGPUMemoryUsage(Size prevObjectSize, Size newObjectSize) { + if (prevObjectSize == newObjectSize) { + return; + } + if (newObjectSize > prevObjectSize) { + _bufferGPUMemoryUsage.fetch_add(newObjectSize - prevObjectSize); + } else { + _bufferGPUMemoryUsage.fetch_sub(prevObjectSize - newObjectSize); + } +} + +void Context::incrementTextureGPUCount() { + _textureGPUCount++; +} +void Context::decrementTextureGPUCount() { + _textureGPUCount--; +} +void Context::updateTextureGPUMemoryUsage(Size prevObjectSize, Size newObjectSize) { + if (prevObjectSize == newObjectSize) { + return; + } + if (newObjectSize > prevObjectSize) { + _textureGPUMemoryUsage.fetch_add(newObjectSize - prevObjectSize); + } else { + _textureGPUMemoryUsage.fetch_sub(prevObjectSize - newObjectSize); + } +} + +uint32_t Context::getBufferGPUCount() { + return _bufferGPUCount.load(); +} + +Context::Size Context::getBufferGPUMemoryUsage() { + return _bufferGPUMemoryUsage.load(); +} + +uint32_t Context::getTextureGPUCount() { + return _textureGPUCount.load(); +} + +Context::Size Context::getTextureGPUMemoryUsage() { + return _textureGPUMemoryUsage.load(); +} + +void Backend::incrementBufferGPUCount() { Context::incrementBufferGPUCount(); } +void Backend::decrementBufferGPUCount() { Context::decrementBufferGPUCount(); } +void Backend::updateBufferGPUMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize) { Context::updateBufferGPUMemoryUsage(prevObjectSize, newObjectSize); } +void Backend::incrementTextureGPUCount() { Context::incrementTextureGPUCount(); } +void Backend::decrementTextureGPUCount() { Context::decrementTextureGPUCount(); } +void Backend::updateTextureGPUMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize) { Context::updateTextureGPUMemoryUsage(prevObjectSize, newObjectSize); } + diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index c2cd1f239e..b898bddef9 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -117,6 +117,17 @@ public: void getStats(ContextStats& stats) const { stats = _stats; } + + + // These should only be accessed by Backend implementation to repport the buffer and texture allocations, + // they are NOT public calls + static void incrementBufferGPUCount(); + static void decrementBufferGPUCount(); + static void updateBufferGPUMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize); + static void incrementTextureGPUCount(); + static void decrementTextureGPUCount(); + static void updateTextureGPUMemoryUsage(Resource::Size prevObjectSize, Resource::Size newObjectSize); + protected: StereoState _stereo; ContextStats _stats; @@ -124,6 +135,7 @@ protected: class Context { public: + using Size = Resource::Size; typedef Backend* (*CreateBackend)(); typedef bool (*MakeProgram)(Shader& shader, const Shader::BindingSet& bindings); @@ -158,6 +170,13 @@ public: // Repporting stats of the context void getStats(ContextStats& stats) const; + + static uint32_t getBufferGPUCount(); + static Size getBufferGPUMemoryUsage(); + + static uint32_t getTextureGPUCount(); + static Size getTextureGPUMemoryUsage(); + protected: Context(const Context& context); @@ -174,6 +193,23 @@ protected: static std::once_flag _initialized; friend class Shader; + + // These should only be accessed by the Backend, they are NOT public calls + static void incrementBufferGPUCount(); + static void decrementBufferGPUCount(); + static void updateBufferGPUMemoryUsage(Size prevObjectSize, Size newObjectSize); + static void incrementTextureGPUCount(); + static void decrementTextureGPUCount(); + static void updateTextureGPUMemoryUsage(Size prevObjectSize, Size newObjectSize); + + // Buffer and Texture Counters + static std::atomic _bufferGPUCount; + static std::atomic _bufferGPUMemoryUsage; + + static std::atomic _textureGPUCount; + static std::atomic _textureGPUMemoryUsage; + + friend class Backend; }; typedef std::shared_ptr ContextPointer; diff --git a/libraries/gpu/src/gpu/GLBackendBuffer.cpp b/libraries/gpu/src/gpu/GLBackendBuffer.cpp index 8b8af386bb..080d743104 100755 --- a/libraries/gpu/src/gpu/GLBackendBuffer.cpp +++ b/libraries/gpu/src/gpu/GLBackendBuffer.cpp @@ -17,27 +17,19 @@ GLBackend::GLBuffer::GLBuffer() : _buffer(0), _size(0) { - Buffer::_bufferGPUCount++; + Backend::incrementBufferGPUCount(); } GLBackend::GLBuffer::~GLBuffer() { if (_buffer != 0) { glDeleteBuffers(1, &_buffer); } - Buffer::_bufferGPUMemoryUsage.fetch_sub(_size); - Buffer::_bufferGPUCount--; + Backend::updateBufferGPUMemoryUsage(_size, 0); + Backend::decrementBufferGPUCount(); } void GLBackend::GLBuffer::setSize(GLuint size) { - if (_size == size) { - return; - } - if (size > _size) { - Buffer::_bufferGPUMemoryUsage.fetch_add(size - _size); - } else { - Buffer::_bufferGPUMemoryUsage.fetch_sub(_size - size); - } - + Backend::updateBufferGPUMemoryUsage(_size, size); _size = size; } diff --git a/libraries/gpu/src/gpu/GLBackendTexture.cpp b/libraries/gpu/src/gpu/GLBackendTexture.cpp index bc25048a3f..09714b5542 100755 --- a/libraries/gpu/src/gpu/GLBackendTexture.cpp +++ b/libraries/gpu/src/gpu/GLBackendTexture.cpp @@ -20,27 +20,19 @@ GLBackend::GLTexture::GLTexture() : _target(GL_TEXTURE_2D), _size(0) { - Texture::_textureGPUCount++; + Backend::incrementTextureGPUCount(); } GLBackend::GLTexture::~GLTexture() { if (_texture != 0) { glDeleteTextures(1, &_texture); } - Texture::_textureGPUMemoryUsage.fetch_sub(_size); - Texture::_textureGPUCount--; + Backend::updateTextureGPUMemoryUsage(_size, 0); + Backend::decrementTextureGPUCount(); } void GLBackend::GLTexture::setSize(GLuint size) { - if (_size == size) { - return; - } - if (size > _size) { - Texture::_textureGPUMemoryUsage.fetch_add(size - _size); - } else { - Texture::_textureGPUMemoryUsage.fetch_sub(_size - size); - } - + Backend::updateTextureGPUMemoryUsage(_size, size); _size = size; } diff --git a/libraries/gpu/src/gpu/Resource.cpp b/libraries/gpu/src/gpu/Resource.cpp index f10d95c03b..c793a92b72 100644 --- a/libraries/gpu/src/gpu/Resource.cpp +++ b/libraries/gpu/src/gpu/Resource.cpp @@ -16,6 +16,8 @@ #include #include +#include "Context.h" + using namespace gpu; class AllocationDebugger { @@ -233,25 +235,7 @@ Resource::Size Resource::Sysmem::append(Size size, const Byte* bytes) { } std::atomic Buffer::_bufferCPUCount{ 0 }; -std::atomic Buffer::_bufferGPUCount{ 0 }; std::atomic Buffer::_bufferCPUMemoryUsage{ 0 }; -std::atomic Buffer::_bufferGPUMemoryUsage{ 0 }; - -uint32_t Buffer::getBufferCPUCount() { - return _bufferCPUCount.load(); -} - -Buffer::Size Buffer::getBufferCPUMemoryUsage() { - return _bufferCPUMemoryUsage.load(); -} - -uint32_t Buffer::getBufferGPUCount() { - return _bufferGPUCount.load(); -} - -Buffer::Size Buffer::getBufferGPUMemoryUsage() { - return _bufferGPUMemoryUsage.load(); -} void Buffer::updateBufferCPUMemoryUsage(Size prevObjectSize, Size newObjectSize) { if (prevObjectSize == newObjectSize) { @@ -264,6 +248,21 @@ void Buffer::updateBufferCPUMemoryUsage(Size prevObjectSize, Size newObjectSize) } } +uint32_t Buffer::getBufferCPUCount() { + return _bufferCPUCount.load(); +} + +Buffer::Size Buffer::getBufferCPUMemoryUsage() { + return _bufferCPUMemoryUsage.load(); +} + +uint32_t Buffer::getBufferGPUCount() { + return Context::getBufferGPUCount(); +} + +Buffer::Size Buffer::getBufferGPUMemoryUsage() { + return Context::getBufferGPUMemoryUsage(); +} Buffer::Buffer() : Resource(), diff --git a/libraries/gpu/src/gpu/Resource.h b/libraries/gpu/src/gpu/Resource.h index 176e33a428..98ad0a2d28 100644 --- a/libraries/gpu/src/gpu/Resource.h +++ b/libraries/gpu/src/gpu/Resource.h @@ -114,10 +114,6 @@ class Buffer : public Resource { static std::atomic _bufferCPUMemoryUsage; static void updateBufferCPUMemoryUsage(Size prevObjectSize, Size newObjectSize); -public: - static std::atomic _bufferGPUCount; - static std::atomic _bufferGPUMemoryUsage; - public: static uint32_t getBufferCPUCount(); static Size getBufferCPUMemoryUsage(); diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 15ad4eea97..df93cd76a5 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -13,31 +13,13 @@ #include #include "GPULogging.h" -//#include +#include "Context.h" using namespace gpu; std::atomic Texture::_textureCPUCount{ 0 }; -std::atomic Texture::_textureGPUCount{ 0 }; std::atomic Texture::_textureCPUMemoryUsage{ 0 }; -std::atomic Texture::_textureGPUMemoryUsage{ 0 }; - -uint32_t Texture::getTextureCPUCount() { - return _textureCPUCount.load(); -} - -Texture::Size Texture::getTextureCPUMemoryUsage() { - return _textureCPUMemoryUsage.load(); -} - -uint32_t Texture::getTextureGPUCount() { - return _textureGPUCount.load(); -} - -Texture::Size Texture::getTextureGPUMemoryUsage() { - return _textureGPUMemoryUsage.load(); -} void Texture::updateTextureCPUMemoryUsage(Size prevObjectSize, Size newObjectSize) { if (prevObjectSize == newObjectSize) { @@ -50,6 +32,22 @@ void Texture::updateTextureCPUMemoryUsage(Size prevObjectSize, Size newObjectSiz } } +uint32_t Texture::getTextureCPUCount() { + return _textureCPUCount.load(); +} + +Texture::Size Texture::getTextureCPUMemoryUsage() { + return _textureCPUMemoryUsage.load(); +} + +uint32_t Texture::getTextureGPUCount() { + return Context::getTextureGPUCount(); +} + +Texture::Size Texture::getTextureGPUMemoryUsage() { + return Context::getTextureGPUMemoryUsage(); + +} uint8 Texture::NUM_FACES_PER_TYPE[NUM_TYPES] = {1, 1, 1, 6}; @@ -118,9 +116,9 @@ const Texture::PixelsPointer Texture::Storage::getMipFace(uint16 level, uint8 fa void Texture::Storage::notifyMipFaceGPULoaded(uint16 level, uint8 face) const { PixelsPointer mipFace = getMipFace(level, face); - // if (mipFace && (_type != TEX_CUBE)) { - if (mipFace) { - mipFace->notifyGPULoaded(); + // Free the mips + if (mipFace) { + mipFace->notifyGPULoaded(); } } diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index c498b5cc22..80fbc867e3 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -141,11 +141,6 @@ class Texture : public Resource { static std::atomic _textureCPUCount; static std::atomic _textureCPUMemoryUsage; static void updateTextureCPUMemoryUsage(Size prevObjectSize, Size newObjectSize); - -public: - static std::atomic _textureGPUCount; - static std::atomic _textureGPUMemoryUsage; - public: static uint32_t getTextureCPUCount(); static Size getTextureCPUMemoryUsage();