Browse Source

PStats tracks compressed texture sizes

David Rose 20 years ago
parent
commit
c6f0b35ee9

+ 80 - 1
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

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

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

+ 17 - 0
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;
+}

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