IMplementing a memory counter

This commit is contained in:
samcake 2016-03-23 09:45:38 -07:00
parent 24b26e3097
commit b29ef7b037
4 changed files with 81 additions and 47 deletions

View file

@ -427,8 +427,8 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) {
if (needUpdate) { if (needUpdate) {
if (texture.isStoredMipFaceAvailable(0)) { if (texture.isStoredMipFaceAvailable(0)) {
Texture::PixelsPointer mip = texture.accessStoredMipFace(0); Texture::PixelsPointer mip = texture.accessStoredMipFace(0);
const GLvoid* bytes = mip->_sysmem.read<Byte>(); const GLvoid* bytes = mip->readData();
Element srcFormat = mip->_format; Element srcFormat = mip->getFormat();
GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat);
@ -458,8 +458,8 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) {
if (texture.isStoredMipFaceAvailable(0)) { if (texture.isStoredMipFaceAvailable(0)) {
Texture::PixelsPointer mip = texture.accessStoredMipFace(0); Texture::PixelsPointer mip = texture.accessStoredMipFace(0);
bytes = mip->_sysmem.read<Byte>(); bytes = mip->readData();
srcFormat = mip->_format; srcFormat = mip->getFormat();
object->_contentStamp = texture.getDataStamp(); object->_contentStamp = texture.getDataStamp();
} }
@ -507,11 +507,11 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) {
for (int f = 0; f < NUM_FACES; f++) { for (int f = 0; f < NUM_FACES; f++) {
if (texture.isStoredMipFaceAvailable(0, f)) { if (texture.isStoredMipFaceAvailable(0, f)) {
Texture::PixelsPointer mipFace = texture.accessStoredMipFace(0, f); Texture::PixelsPointer mipFace = texture.accessStoredMipFace(0, f);
Element srcFormat = mipFace->_format; Element srcFormat = mipFace->getFormat();
GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat);
glTexSubImage2D(FACE_LAYOUT[f], 0, texelFormat.internalFormat, texture.getWidth(), texture.getWidth(), 0, glTexSubImage2D(FACE_LAYOUT[f], 0, texelFormat.internalFormat, texture.getWidth(), texture.getWidth(), 0,
texelFormat.format, texelFormat.type, (GLvoid*) (mipFace->_sysmem.read<Byte>())); texelFormat.format, texelFormat.type, (GLvoid*) (mipFace->readData()));
// At this point the mip pixels have been loaded, we can notify // At this point the mip pixels have been loaded, we can notify
texture.notifyMipFaceGPULoaded(0, f); texture.notifyMipFaceGPULoaded(0, f);
@ -536,11 +536,11 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) {
for (int f = 0; f < NUM_FACES; f++) { for (int f = 0; f < NUM_FACES; f++) {
if (texture.isStoredMipFaceAvailable(0, f)) { if (texture.isStoredMipFaceAvailable(0, f)) {
Texture::PixelsPointer mipFace = texture.accessStoredMipFace(0, f); Texture::PixelsPointer mipFace = texture.accessStoredMipFace(0, f);
Element srcFormat = mipFace->_format; Element srcFormat = mipFace->getFormat();
GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat);
glTexImage2D(FACE_LAYOUT[f], 0, texelFormat.internalFormat, texture.getWidth(), texture.getWidth(), 0, glTexImage2D(FACE_LAYOUT[f], 0, texelFormat.internalFormat, texture.getWidth(), texture.getWidth(), 0,
texelFormat.format, texelFormat.type, (GLvoid*) (mipFace->_sysmem.read<Byte>())); texelFormat.format, texelFormat.type, (GLvoid*) (mipFace->readData()));
// At this point the mip pixels have been loaded, we can notify // At this point the mip pixels have been loaded, we can notify
texture.notifyMipFaceGPULoaded(0, f); texture.notifyMipFaceGPULoaded(0, f);

View file

@ -12,53 +12,82 @@
#include "Texture.h" #include "Texture.h"
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
#include "GPULogging.h"
#include <QDebug> //#include <QDebug>
using namespace gpu; using namespace gpu;
std::atomic<uint32_t> Texture::_textureSystemMemoryUsage; std::atomic<Texture::Size> Texture::_textureSystemMemoryUsage{ 0 };
std::atomic<uint32_t> Texture::_textureVideoMemoryUsage; std::atomic<Texture::Size> Texture::_textureVideoMemoryUsage{ 0 };
uint32_t Texture::getCurrentSystemMemoryUsage() { Texture::Size Texture::getCurrentSystemMemoryUsage() {
return _textureSystemMemoryUsage.load(); return _textureSystemMemoryUsage.load();
} }
uint32_t Texture::getCurrentVideoMemoryUsage() { Texture::Size Texture::getCurrentVideoMemoryUsage() {
return _textureVideoMemoryUsage.load(); return _textureVideoMemoryUsage.load();
} }
void Texture::addSystemMemoryUsage(uint32_t memorySize) { void Texture::addSystemMemoryUsage(Size memorySize) {
_textureSystemMemoryUsage.fetch_add(memorySize); _textureSystemMemoryUsage.fetch_add(memorySize);
} }
void Texture::subSystemMemoryUsage(uint32_t memorySize) { void Texture::subSystemMemoryUsage(Size memorySize) {
_textureSystemMemoryUsage.fetch_sub(memorySize); _textureSystemMemoryUsage.fetch_sub(memorySize);
} }
void Texture::addVideoMemoryUsage(uint32_t memorySize) { void Texture::updateSystemMemoryUsage(Size prevObjectSize, Size newObjectSize) {
if (prevObjectSize == newObjectSize) {
return;
}
if (prevObjectSize > newObjectSize) {
subSystemMemoryUsage(prevObjectSize - newObjectSize);
} else {
addSystemMemoryUsage(newObjectSize - prevObjectSize);
}
qCDebug(gpulogging) << "Texture::SysMem = " << getCurrentSystemMemoryUsage();
}
void Texture::addVideoMemoryUsage(Size memorySize) {
_textureVideoMemoryUsage.fetch_add(memorySize); _textureVideoMemoryUsage.fetch_add(memorySize);
} }
void Texture::subVideoMemoryUsage(uint32_t memorySize) { void Texture::subVideoMemoryUsage(Size memorySize) {
_textureVideoMemoryUsage.fetch_sub(memorySize); _textureVideoMemoryUsage.fetch_sub(memorySize);
} }
uint8 Texture::NUM_FACES_PER_TYPE[NUM_TYPES] = {1, 1, 1, 6}; uint8 Texture::NUM_FACES_PER_TYPE[NUM_TYPES] = {1, 1, 1, 6};
Texture::Pixels::Pixels(const Element& format, Size size, const Byte* bytes) : Texture::Pixels::Pixels(const Element& format, Size size, const Byte* bytes) :
_sysmem(size, bytes),
_format(format), _format(format),
_sysmem(size, bytes),
_isGPULoaded(false) { _isGPULoaded(false) {
Texture::updateSystemMemoryUsage(0, _sysmem.getSize());
} }
Texture::Pixels::~Pixels() { Texture::Pixels::~Pixels() {
Texture::updateSystemMemoryUsage(_sysmem.getSize(), 0);
} }
Texture::Size Texture::Pixels::resize(Size pSize) { Texture::Size Texture::Pixels::resize(Size pSize) {
return _sysmem.resize(pSize); auto prevSize = _sysmem.getSize();
auto newSize = _sysmem.resize(pSize);
Texture::updateSystemMemoryUsage(prevSize, newSize);
return newSize;
}
Texture::Size Texture::Pixels::setData(const Element& format, Size size, const Byte* bytes ) {
_format = format;
auto prevSize = _sysmem.getSize();
auto newSize = _sysmem.setData(size, bytes);
Texture::updateSystemMemoryUsage(prevSize, newSize);
_isGPULoaded = false;
return newSize;
} }
void Texture::Pixels::notifyGPULoaded() { void Texture::Pixels::notifyGPULoaded() {
_isGPULoaded = true; _isGPULoaded = true;
_sysmem.resize(0); auto prevSize = _sysmem.getSize();
auto newSize = _sysmem.resize(0);
Texture::updateSystemMemoryUsage(prevSize, newSize);
} }
void Texture::Storage::assignTexture(Texture* texture) { void Texture::Storage::assignTexture(Texture* texture) {
@ -92,14 +121,15 @@ const Texture::PixelsPointer Texture::Storage::getMipFace(uint16 level, uint8 fa
void Texture::Storage::notifyMipFaceGPULoaded(uint16 level, uint8 face) const { void Texture::Storage::notifyMipFaceGPULoaded(uint16 level, uint8 face) const {
PixelsPointer mipFace = getMipFace(level, face); PixelsPointer mipFace = getMipFace(level, face);
if (mipFace && (_type != TEX_CUBE)) { // if (mipFace && (_type != TEX_CUBE)) {
if (mipFaced) {
mipFace->notifyGPULoaded(); mipFace->notifyGPULoaded();
} }
} }
bool Texture::Storage::isMipAvailable(uint16 level, uint8 face) const { bool Texture::Storage::isMipAvailable(uint16 level, uint8 face) const {
PixelsPointer mipFace = getMipFace(level, face); PixelsPointer mipFace = getMipFace(level, face);
return (mipFace && mipFace->_sysmem.getSize()); return (mipFace && mipFace->getSize());
} }
bool Texture::Storage::allocateMip(uint16 level) { bool Texture::Storage::allocateMip(uint16 level) {
@ -135,9 +165,7 @@ bool Texture::Storage::assignMipData(uint16 level, const Element& format, Size s
auto faceBytes = bytes; auto faceBytes = bytes;
Size allocated = 0; Size allocated = 0;
for (auto& face : mip) { for (auto& face : mip) {
face->_format = format; allocated += face->setData(format, sizePerFace, faceBytes);
allocated += face->_sysmem.setData(sizePerFace, faceBytes);
face->_isGPULoaded = false;
faceBytes += sizePerFace; faceBytes += sizePerFace;
} }
@ -154,9 +182,7 @@ bool Texture::Storage::assignMipFaceData(uint16 level, const Element& format, Si
Size allocated = 0; Size allocated = 0;
if (face < mip.size()) { if (face < mip.size()) {
auto mipFace = mip[face]; auto mipFace = mip[face];
mipFace->_format = format; allocated += mipFace->setData(format, size, bytes);
allocated += mipFace->_sysmem.setData(size, bytes);
mipFace->_isGPULoaded = false;
bumpStamp(); bumpStamp();
} }
@ -396,7 +422,7 @@ uint16 Texture::autoGenerateMips(uint16 maxMip) {
uint16 Texture::getStoredMipWidth(uint16 level) const { uint16 Texture::getStoredMipWidth(uint16 level) const {
PixelsPointer mipFace = accessStoredMipFace(level); PixelsPointer mipFace = accessStoredMipFace(level);
if (mipFace && mipFace->_sysmem.getSize()) { if (mipFace && mipFace->getSize()) {
return evalMipWidth(level); return evalMipWidth(level);
} }
return 0; return 0;
@ -404,7 +430,7 @@ uint16 Texture::getStoredMipWidth(uint16 level) const {
uint16 Texture::getStoredMipHeight(uint16 level) const { uint16 Texture::getStoredMipHeight(uint16 level) const {
PixelsPointer mip = accessStoredMipFace(level); PixelsPointer mip = accessStoredMipFace(level);
if (mip && mip->_sysmem.getSize()) { if (mip && mip->getSize()) {
return evalMipHeight(level); return evalMipHeight(level);
} }
return 0; return 0;
@ -412,7 +438,7 @@ uint16 Texture::getStoredMipHeight(uint16 level) const {
uint16 Texture::getStoredMipDepth(uint16 level) const { uint16 Texture::getStoredMipDepth(uint16 level) const {
PixelsPointer mipFace = accessStoredMipFace(level); PixelsPointer mipFace = accessStoredMipFace(level);
if (mipFace && mipFace->_sysmem.getSize()) { if (mipFace && mipFace->getSize()) {
return evalMipDepth(level); return evalMipDepth(level);
} }
return 0; return 0;
@ -420,7 +446,7 @@ uint16 Texture::getStoredMipDepth(uint16 level) const {
uint32 Texture::getStoredMipNumTexels(uint16 level) const { uint32 Texture::getStoredMipNumTexels(uint16 level) const {
PixelsPointer mipFace = accessStoredMipFace(level); PixelsPointer mipFace = accessStoredMipFace(level);
if (mipFace && mipFace->_sysmem.getSize()) { if (mipFace && mipFace->getSize()) {
return evalMipWidth(level) * evalMipHeight(level) * evalMipDepth(level); return evalMipWidth(level) * evalMipHeight(level) * evalMipDepth(level);
} }
return 0; return 0;
@ -428,7 +454,7 @@ uint32 Texture::getStoredMipNumTexels(uint16 level) const {
uint32 Texture::getStoredMipSize(uint16 level) const { uint32 Texture::getStoredMipSize(uint16 level) const {
PixelsPointer mipFace = accessStoredMipFace(level); PixelsPointer mipFace = accessStoredMipFace(level);
if (mipFace && mipFace->_sysmem.getSize()) { if (mipFace && mipFace->getSize()) {
return evalMipWidth(level) * evalMipHeight(level) * evalMipDepth(level) * getTexelFormat().getSize(); return evalMipWidth(level) * evalMipHeight(level) * evalMipDepth(level) * getTexelFormat().getSize();
} }
return 0; return 0;
@ -674,8 +700,8 @@ bool sphericalHarmonicsFromTexture(const gpu::Texture& cubeTexture, std::vector<
// for each face of cube texture // for each face of cube texture
for(int face=0; face < gpu::Texture::NUM_CUBE_FACES; face++) { for(int face=0; face < gpu::Texture::NUM_CUBE_FACES; face++) {
auto numComponents = cubeTexture.accessStoredMipFace(0,face)->_format.getScalarCount(); auto numComponents = cubeTexture.accessStoredMipFace(0,face)->getFormat().getScalarCount();
auto data = cubeTexture.accessStoredMipFace(0,face)->_sysmem.readData(); auto data = cubeTexture.accessStoredMipFace(0,face)->readData();
if (data == nullptr) { if (data == nullptr) {
continue; continue;
} }

View file

@ -139,19 +139,20 @@ protected:
}; };
class Texture : public Resource { class Texture : public Resource {
static std::atomic<uint32_t> _textureSystemMemoryUsage; static std::atomic<Size> _textureSystemMemoryUsage;
static std::atomic<uint32_t> _textureVideoMemoryUsage; static std::atomic<Size> _textureVideoMemoryUsage;
void addSystemMemoryUsage(uint32_t memorySize); static void addSystemMemoryUsage(Size memorySize);
void subSystemMemoryUsage(uint32_t memorySize); static void subSystemMemoryUsage(Size memorySize);
static void updateSystemMemoryUsage(Size prevObjectSize, Size newObjectSize);
void addVideoMemoryUsage(uint32_t memorySize); static void addVideoMemoryUsage(Size memorySize);
void subVideoMemoryUsage(uint32_t memorySize); static void subVideoMemoryUsage(Size memorySize);
public: public:
uint32_t getCurrentSystemMemoryUsage(); static Size getCurrentSystemMemoryUsage();
uint32_t getCurrentVideoMemoryUsage(); static Size getCurrentVideoMemoryUsage();
class Usage { class Usage {
public: public:
@ -207,14 +208,21 @@ public:
Pixels(const Element& format, Size size, const Byte* bytes); Pixels(const Element& format, Size size, const Byte* bytes);
~Pixels(); ~Pixels();
const Byte* readData() const { return _sysmem.readData(); }
Size getSize() const { return _sysmem.getSize(); } Size getSize() const { return _sysmem.getSize(); }
Size resize(Size pSize); Size resize(Size pSize);
Size setData(const Element& format, Size size, const Byte* bytes );
const Element& getFormat() const { return _format; }
void notifyGPULoaded(); void notifyGPULoaded();
protected: protected:
Element _format; Element _format;
Sysmem _sysmem; Sysmem _sysmem;
bool _isGPULoaded; bool _isGPULoaded;
friend class Texture;
}; };
typedef std::shared_ptr< Pixels > PixelsPointer; typedef std::shared_ptr< Pixels > PixelsPointer;

View file

@ -1,4 +1,4 @@
// d//
// ScriptEngine.cpp // ScriptEngine.cpp
// libraries/script-engine/src // libraries/script-engine/src
// //