CLeaning up the location of the global variables, moved them to the gpu::COntext class as static

This commit is contained in:
samcake 2016-03-28 11:45:07 -07:00
parent 7402dafc91
commit b5028acde1
8 changed files with 146 additions and 73 deletions

View file

@ -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<uint32_t> Context::_bufferGPUCount{ 0 };
std::atomic<Buffer::Size> Context::_bufferGPUMemoryUsage{ 0 };
std::atomic<uint32_t> Context::_textureGPUCount{ 0 };
std::atomic<Texture::Size> 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); }

View file

@ -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<uint32_t> _bufferGPUCount;
static std::atomic<Size> _bufferGPUMemoryUsage;
static std::atomic<uint32_t> _textureGPUCount;
static std::atomic<Size> _textureGPUMemoryUsage;
friend class Backend;
};
typedef std::shared_ptr<Context> ContextPointer;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -16,6 +16,8 @@
#include <NumericalConstants.h>
#include <QDebug>
#include "Context.h"
using namespace gpu;
class AllocationDebugger {
@ -233,25 +235,7 @@ Resource::Size Resource::Sysmem::append(Size size, const Byte* bytes) {
}
std::atomic<uint32_t> Buffer::_bufferCPUCount{ 0 };
std::atomic<uint32_t> Buffer::_bufferGPUCount{ 0 };
std::atomic<Buffer::Size> Buffer::_bufferCPUMemoryUsage{ 0 };
std::atomic<Buffer::Size> 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(),

View file

@ -114,10 +114,6 @@ class Buffer : public Resource {
static std::atomic<Size> _bufferCPUMemoryUsage;
static void updateBufferCPUMemoryUsage(Size prevObjectSize, Size newObjectSize);
public:
static std::atomic<uint32_t> _bufferGPUCount;
static std::atomic<Size> _bufferGPUMemoryUsage;
public:
static uint32_t getBufferCPUCount();
static Size getBufferCPUMemoryUsage();

View file

@ -13,31 +13,13 @@
#include <glm/gtc/constants.hpp>
#include "GPULogging.h"
//#include <QDebug>
#include "Context.h"
using namespace gpu;
std::atomic<uint32_t> Texture::_textureCPUCount{ 0 };
std::atomic<uint32_t> Texture::_textureGPUCount{ 0 };
std::atomic<Texture::Size> Texture::_textureCPUMemoryUsage{ 0 };
std::atomic<Texture::Size> 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();
}
}

View file

@ -141,11 +141,6 @@ class Texture : public Resource {
static std::atomic<uint32_t> _textureCPUCount;
static std::atomic<Size> _textureCPUMemoryUsage;
static void updateTextureCPUMemoryUsage(Size prevObjectSize, Size newObjectSize);
public:
static std::atomic<uint32_t> _textureGPUCount;
static std::atomic<Size> _textureGPUMemoryUsage;
public:
static uint32_t getTextureCPUCount();
static Size getTextureCPUMemoryUsage();