Panda3D
texturePool.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file texturePool.h
10  * @author drose
11  * @date 2000-04-26
12  * @author fperazzi, PandaSE
13  * @date 2010-04-29
14  */
15 
16 #ifndef TEXTUREPOOL_H
17 #define TEXTUREPOOL_H
18 
19 #include "pandabase.h"
20 #include "texture.h"
21 #include "filename.h"
22 #include "config_gobj.h"
23 #include "loaderOptions.h"
24 #include "pmutex.h"
25 #include "pmap.h"
26 #include "textureCollection.h"
27 
28 class TexturePoolFilter;
29 class BamCache;
30 class BamCacheRecord;
31 
32 /**
33  * This is the preferred interface for loading textures from image files. It
34  * unifies all references to the same filename, so that multiple models that
35  * reference the same textures don't waste texture memory unnecessarily.
36  */
37 class EXPCL_PANDA_GOBJ TexturePool {
38 PUBLISHED:
39  INLINE static bool has_texture(const Filename &filename);
40  INLINE static bool verify_texture(const Filename &filename);
41  BLOCKING INLINE static Texture *load_texture(const Filename &filename,
42  int primary_file_num_channels = 0,
43  bool read_mipmaps = false,
44  const LoaderOptions &options = LoaderOptions());
45  BLOCKING INLINE static Texture *load_texture(const Filename &filename,
46  const Filename &alpha_filename,
47  int primary_file_num_channels = 0,
48  int alpha_file_channel = 0,
49  bool read_mipmaps = false,
50  const LoaderOptions &options = LoaderOptions());
51  BLOCKING INLINE static Texture *load_3d_texture(const Filename &filename_pattern,
52  bool read_mipmaps = false,
53  const LoaderOptions &options = LoaderOptions());
54  BLOCKING INLINE static Texture *load_2d_texture_array(const Filename &filename_pattern,
55  bool read_mipmaps = false,
56  const LoaderOptions &options = LoaderOptions());
57  BLOCKING INLINE static Texture *load_cube_map(const Filename &filename_pattern,
58  bool read_mipmaps = false,
59  const LoaderOptions &options = LoaderOptions());
60 
61  INLINE static Texture *get_normalization_cube_map(int size);
62  INLINE static Texture *get_alpha_scale_map();
63 
64  INLINE static void add_texture(Texture *texture);
65  INLINE static void release_texture(Texture *texture);
66  INLINE static void release_all_textures();
67  INLINE static void rehash();
68 
69  INLINE static int garbage_collect();
70 
71  INLINE static void list_contents(std::ostream &out);
72  INLINE static void list_contents();
73 
74  INLINE static Texture *find_texture(const std::string &name);
75  INLINE static TextureCollection find_all_textures(const std::string &name = "*");
76 
77  INLINE static void set_fake_texture_image(const Filename &filename);
78  INLINE static void clear_fake_texture_image();
79  INLINE static bool has_fake_texture_image();
80  INLINE static const Filename &get_fake_texture_image();
81  INLINE static PT(Texture) make_texture(const std::string &extension);
82 
83  static void write(std::ostream &out);
84 
85 public:
86  typedef Texture::MakeTextureFunc MakeTextureFunc;
87  void register_texture_type(MakeTextureFunc *func, const std::string &extensions);
88  void register_filter(TexturePoolFilter *filter);
89 
90  MakeTextureFunc *get_texture_type(const std::string &extension) const;
91  void write_texture_types(std::ostream &out, int indent_level) const;
92 
93  static TexturePool *get_global_ptr();
94 
95 private:
96  TexturePool();
97 
98  bool ns_has_texture(const Filename &orig_filename);
99  Texture *ns_load_texture(const Filename &orig_filename,
100  int primary_file_num_channels,
101  bool read_mipmaps,
102  const LoaderOptions &options);
103  Texture *ns_load_texture(const Filename &orig_filename,
104  const Filename &orig_alpha_filename,
105  int primary_file_num_channels,
106  int alpha_file_channel,
107  bool read_mipmaps,
108  const LoaderOptions &options);
109  Texture *ns_load_3d_texture(const Filename &filename_pattern,
110  bool read_mipmaps,
111  const LoaderOptions &options);
112  Texture *ns_load_2d_texture_array(const Filename &filename_pattern,
113  bool read_mipmaps,
114  const LoaderOptions &options);
115  Texture *ns_load_cube_map(const Filename &filename_pattern,
116  bool read_mipmaps,
117  const LoaderOptions &options);
118  Texture *ns_get_normalization_cube_map(int size);
119  Texture *ns_get_alpha_scale_map();
120 
121  void ns_add_texture(Texture *texture);
122  void ns_release_texture(Texture *texture);
123  void ns_release_all_textures();
124  int ns_garbage_collect();
125  void ns_list_contents(std::ostream &out) const;
126  Texture *ns_find_texture(const std::string &name) const;
127  TextureCollection ns_find_all_textures(const std::string &name) const;
128  PT(Texture) ns_make_texture(const std::string &extension) const;
129 
130  void resolve_filename(Filename &new_filename, const Filename &orig_filename,
131  bool read_mipmaps, const LoaderOptions &options);
132 
133  void try_load_cache(PT(Texture) &tex, BamCache *cache,
134  const Filename &filename, PT(BamCacheRecord) &record,
135  bool &compressed_cache_record,
136  const LoaderOptions &options);
137  void report_texture_unreadable(const Filename &filename) const;
138 
139  // Methods to invoke a TexturePoolFilter.
140  PT(Texture) pre_load(const Filename &orig_filename,
141  const Filename &orig_alpha_filename,
142  int primary_file_num_channels,
143  int alpha_file_channel,
144  bool read_mipmaps, const LoaderOptions &options);
145  PT(Texture) post_load(Texture *tex);
146 
147  void load_filters();
148 
149  static TexturePool *_global_ptr;
150 
151  Mutex _lock;
152  struct LookupKey {
153  Filename _fullpath;
154  Filename _alpha_fullpath;
155  int _primary_file_num_channels = 0;
156  int _alpha_file_channel = 0;
157  Texture::TextureType _texture_type = Texture::TT_2d_texture;
158 
159  INLINE bool operator < (const LookupKey &other) const;
160  };
161  typedef pmap<LookupKey, PT(Texture)> Textures;
162  Textures _textures;
164  RelpathLookup _relpath_lookup;
165 
166  Filename _fake_texture_image;
167 
168  PT(Texture) _normalization_cube_map;
169  PT(Texture) _alpha_scale_map;
170 
172  TypeRegistry _type_registry;
173 
175  FilterRegistry _filter_registry;
176 };
177 
178 #include "texturePool.I"
179 
180 #endif
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
This class maintains a cache of Bam and/or Txo objects generated from model files and texture images ...
Definition: bamCache.h:42
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:23
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:40
Manages a list of Texture objects, as returned by TexturePool::find_all_textures().
This is an abstract base class, a placeholder for any number of different classes that may wish to im...
This is the preferred interface for loading textures from image files.
Definition: texturePool.h:37
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:71
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.