From 86d753b4f9164330fad9127d9fc96e3109f8b8d1 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 21 Aug 2008 20:04:10 +0000 Subject: [PATCH] better mipmap queries --- panda/src/gobj/texture.I | 7 ++++-- panda/src/gobj/texture.cxx | 50 +++++++++++++++++++++++++++++++++++++- panda/src/gobj/texture.h | 1 + 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/panda/src/gobj/texture.I b/panda/src/gobj/texture.I index e7cee19abb..3c3754ee61 100644 --- a/panda/src/gobj/texture.I +++ b/panda/src/gobj/texture.I @@ -858,8 +858,11 @@ set_keep_ram_image(bool keep_ram_image) { // Access: Published // Description: Returns the maximum number of mipmap level images // available in system memory. The actual number may be -// less than this; use has_ram_mipmap_image() to verify -// each level. +// less than this (that is, there might be gaps in the +// sequence); use has_ram_mipmap_image() to verify each +// level. +// +// Also see get_num_loadable_ram_mipmap_images(). //////////////////////////////////////////////////////////////////// INLINE int Texture:: get_num_ram_mipmap_images() const { diff --git a/panda/src/gobj/texture.cxx b/panda/src/gobj/texture.cxx index aab5d4d174..a03d521bc6 100644 --- a/panda/src/gobj/texture.cxx +++ b/panda/src/gobj/texture.cxx @@ -1482,6 +1482,54 @@ get_keep_ram_image() const { return _keep_ram_image; } +//////////////////////////////////////////////////////////////////// +// Function: Texture::get_num_loadable_ram_mipmap_images +// Access: Published +// Description: Returns the number of contiguous mipmap levels that +// exist in RAM, up until the first gap in the sequence. +// It is guaranteed that at least mipmap levels [0, +// get_num_ram_mipmap_images()) exist. +// +// The number returned will never exceed the number of +// required mipmap images based on the size of the +// texture and its filter mode. +// +// This method is different from +// get_num_ram_mipmap_images() in that it returns only +// the number of mipmap levels that can actually be +// usefully loaded, regardless of the actual number that +// may be stored. +//////////////////////////////////////////////////////////////////// +int Texture:: +get_num_loadable_ram_mipmap_images() const { + ReMutexHolder holder(_lock); + if (_ram_images.empty() || _ram_images[0]._image.empty()) { + // If we don't even have a base image, the answer is none. + return 0; + } + if (!uses_mipmaps()) { + // If we have a base image and don't require mipmapping, the + // answer is 1. + return 1; + } + + // Check that we have enough mipmap levels to meet the size + // requirements. + int size = max(_x_size, max(_y_size, _z_size)); + int n = 0; + int x = 1; + while (x < size) { + x = (x << 1); + ++n; + if (n >= (int)_ram_images.size() || _ram_images[n]._image.empty()) { + return n; + } + } + + ++n; + return n; +} + //////////////////////////////////////////////////////////////////// // Function: Texture::has_all_ram_mipmap_images // Access: Published @@ -1528,7 +1576,7 @@ has_all_ram_mipmap_images() const { CPTA_uchar Texture:: get_ram_mipmap_image(int n) { ReMutexHolder holder(_lock); - if (n < (int)_ram_images.size()) { + if (n < (int)_ram_images.size() || !_ram_images[n]._image.empty()) { return _ram_images[n]._image; } return CPTA_uchar(get_class_type()); diff --git a/panda/src/gobj/texture.h b/panda/src/gobj/texture.h index 59e1a53b6d..ed2a31f109 100644 --- a/panda/src/gobj/texture.h +++ b/panda/src/gobj/texture.h @@ -310,6 +310,7 @@ PUBLISHED: INLINE int get_num_ram_mipmap_images() const; INLINE bool has_ram_mipmap_image(int n) const; + int get_num_loadable_ram_mipmap_images() const; bool has_all_ram_mipmap_images() const; INLINE size_t get_ram_mipmap_image_size(int n) const; INLINE size_t get_ram_mipmap_page_size(int n) const;