From c6f0b35ee937e61e83405dd1e291738ef0754680 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 16 Mar 2006 00:29:54 +0000 Subject: [PATCH] PStats tracks compressed texture sizes --- .../glstuff/glGraphicsStateGuardian_src.cxx | 81 ++++++++++++++++++- .../src/glstuff/glGraphicsStateGuardian_src.h | 2 + panda/src/glstuff/glTextureContext_src.I | 2 +- panda/src/glstuff/glTextureContext_src.cxx | 17 ++++ panda/src/glstuff/glTextureContext_src.h | 4 + 5 files changed, 104 insertions(+), 2 deletions(-) diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index 51f864d7ec..5a3891fee2 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -2193,7 +2193,7 @@ extract_texture_data(Texture *tex) { page_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X; } - GLint width = 0, height = 0, depth = 0; + GLint width = 1, height = 1, depth = 1; GLP(GetTexLevelParameteriv)(page_target, 0, GL_TEXTURE_WIDTH, &width); GLP(GetTexLevelParameteriv)(page_target, 0, GL_TEXTURE_HEIGHT, &height); if (_supports_3d_texture) { @@ -6430,6 +6430,10 @@ upload_texture(CLP(TextureContext) *gtc) { gtc->_height = height; gtc->_depth = depth; +#ifdef DO_PSTATS + gtc->_texture_memory_size = get_texture_memory_size(tex); +#endif + #ifndef NDEBUG if (uses_mipmaps && CLP(save_mipmaps)) { save_mipmap_images(tex); @@ -6630,6 +6634,81 @@ upload_texture_image(CLP(TextureContext) *gtc, return true; } +//////////////////////////////////////////////////////////////////// +// Function: GLGraphicsStateGuardian::get_texture_memory_size +// Access: Protected +// Description: Asks OpenGL how much texture memory is consumed by +// the indicated texture (which is also the +// currently-selected texture). +//////////////////////////////////////////////////////////////////// +size_t CLP(GraphicsStateGuardian):: +get_texture_memory_size(Texture *tex) const { + GLenum target = get_texture_target(tex->get_texture_type()); + + GLenum page_target = target; + GLint scale = 1; + if (target == GL_TEXTURE_CUBE_MAP) { + // We need a particular page to get the level parameter from. + page_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X; + scale = 6; + } + + GLint internal_format; + GLP(GetTexLevelParameteriv)(page_target, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format); + + if (is_compressed_format(internal_format)) { + // Try to get the compressed size. + GLint image_size; + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &image_size); + + GLenum error_code = GLP(GetError)(); + if (error_code != GL_NO_ERROR) { + const GLubyte *error_string = GLUP(ErrorString)(error_code); + GLCAT.debug() + << "Couldn't get compressed size for " << tex->get_name(); + if (error_string != (const GLubyte *)NULL) { + GLCAT.error(false) + << " : " << error_string; + } + GLCAT.debug(false) + << "\n"; + } else { + return image_size * scale; + } + } + + // OK, get the noncompressed size. + GLint red_size, green_size, blue_size, alpha_size, luminance_size, + depth_size, intensity_size; + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_RED_SIZE, &red_size); + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_GREEN_SIZE, &green_size); + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_BLUE_SIZE, &blue_size); + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_ALPHA_SIZE, &alpha_size); + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_LUMINANCE_SIZE, &luminance_size); + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_DEPTH_SIZE, &depth_size); + GLP(GetTexLevelParameteriv)(page_target, 0, + GL_TEXTURE_INTENSITY_SIZE, &intensity_size); + + GLint width = 1, height = 1, depth = 1; + GLP(GetTexLevelParameteriv)(page_target, 0, GL_TEXTURE_WIDTH, &width); + GLP(GetTexLevelParameteriv)(page_target, 0, GL_TEXTURE_HEIGHT, &height); + if (_supports_3d_texture) { + GLP(GetTexLevelParameteriv)(page_target, 0, GL_TEXTURE_DEPTH, &depth); + } + + size_t num_bits = (red_size + green_size + blue_size + alpha_size + luminance_size + depth_size + intensity_size); + size_t num_bytes = (num_bits + 7) / 8; + + return num_bytes * width * height * depth * scale; +} + //////////////////////////////////////////////////////////////////// // Function: GLGraphicsStateGuardian::do_point_size // Access: Protected diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.h b/panda/src/glstuff/glGraphicsStateGuardian_src.h index c1e7cd14bb..5a9450de6f 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.h +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.h @@ -276,6 +276,8 @@ protected: size_t image_size, Texture::CompressionMode image_compression); + size_t get_texture_memory_size(Texture *tex) const; + void do_point_size(); #ifndef NDEBUG diff --git a/panda/src/glstuff/glTextureContext_src.I b/panda/src/glstuff/glTextureContext_src.I index 73096c9259..c062ed4302 100644 --- a/panda/src/glstuff/glTextureContext_src.I +++ b/panda/src/glstuff/glTextureContext_src.I @@ -27,6 +27,6 @@ CLP(TextureContext)(Texture *tex) : TextureContext(tex) { _index = 0; - _already_applied = false; + _texture_memory_size = 0; } diff --git a/panda/src/glstuff/glTextureContext_src.cxx b/panda/src/glstuff/glTextureContext_src.cxx index 826bd6e372..aac0929197 100644 --- a/panda/src/glstuff/glTextureContext_src.cxx +++ b/panda/src/glstuff/glTextureContext_src.cxx @@ -16,4 +16,21 @@ // //////////////////////////////////////////////////////////////////// +#include "notify.h" + TypeHandle CLP(TextureContext)::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: GLTextureContext::estimate_texture_memory +// Access: Public, Virtual +// Description: Estimates the amount of texture memory that will be +// consumed by loading this texture. This is mainly +// useful for debugging and reporting purposes. +// +// Returns a value in bytes. +//////////////////////////////////////////////////////////////////// +size_t CLP(TextureContext):: +estimate_texture_memory() { + nassertr(_texture_memory_size != 0, TextureContext::estimate_texture_memory()); + return _texture_memory_size; +} diff --git a/panda/src/glstuff/glTextureContext_src.h b/panda/src/glstuff/glTextureContext_src.h index e4e948fff7..57a0f60bac 100644 --- a/panda/src/glstuff/glTextureContext_src.h +++ b/panda/src/glstuff/glTextureContext_src.h @@ -27,6 +27,8 @@ class EXPCL_GL CLP(TextureContext) : public TextureContext { public: INLINE CLP(TextureContext)(Texture *tex); + virtual size_t estimate_texture_memory(); + // This is the GL "name" of the texture object. GLuint _index; @@ -39,6 +41,8 @@ public: GLsizei _height; GLsizei _depth; + size_t _texture_memory_size; + public: static TypeHandle get_class_type() { return _type_handle;