Panda3D
Loading...
Searching...
No Matches
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
29class BamCache;
30class 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 */
37class EXPCL_PANDA_GOBJ TexturePool {
38PUBLISHED:
39 INLINE static bool has_texture(const Filename &filename);
40 INLINE static bool verify_texture(const Filename &filename);
41 INLINE static Texture *get_texture(const Filename &filename,
42 int primary_file_num_channels = 0,
43 bool read_mipmaps = false);
44 INLINE static Texture *get_texture(const Filename &filename,
45 const Filename &alpha_filename,
46 int primary_file_num_channels = 0,
47 int alpha_file_channel = 0,
48 bool read_mipmaps = false);
49 BLOCKING INLINE static Texture *load_texture(const Filename &filename,
50 int primary_file_num_channels = 0,
51 bool read_mipmaps = false,
52 const LoaderOptions &options = LoaderOptions());
53 BLOCKING INLINE static Texture *load_texture(const Filename &filename,
54 const Filename &alpha_filename,
55 int primary_file_num_channels = 0,
56 int alpha_file_channel = 0,
57 bool read_mipmaps = false,
58 const LoaderOptions &options = LoaderOptions());
59 BLOCKING INLINE static Texture *load_3d_texture(const Filename &filename_pattern,
60 bool read_mipmaps = false,
61 const LoaderOptions &options = LoaderOptions());
62 BLOCKING INLINE static Texture *load_2d_texture_array(const Filename &filename_pattern,
63 bool read_mipmaps = false,
64 const LoaderOptions &options = LoaderOptions());
65 BLOCKING INLINE static Texture *load_cube_map(const Filename &filename_pattern,
66 bool read_mipmaps = false,
67 const LoaderOptions &options = LoaderOptions());
68
69 INLINE static Texture *get_normalization_cube_map(int size);
70 INLINE static Texture *get_alpha_scale_map();
71
72 INLINE static void add_texture(Texture *texture);
73 INLINE static void release_texture(Texture *texture);
74 INLINE static void release_all_textures();
75 INLINE static void rehash();
76
77 INLINE static int garbage_collect();
78
79 INLINE static void list_contents(std::ostream &out);
80 INLINE static void list_contents();
81
82 INLINE static Texture *find_texture(const std::string &name);
83 INLINE static TextureCollection find_all_textures(const std::string &name = "*");
84
85 INLINE static void set_fake_texture_image(const Filename &filename);
86 INLINE static void clear_fake_texture_image();
87 INLINE static bool has_fake_texture_image();
88 INLINE static const Filename &get_fake_texture_image();
89 INLINE static PT(Texture) make_texture(const std::string &extension);
90
91 static void write(std::ostream &out);
92
93public:
94 typedef Texture::MakeTextureFunc MakeTextureFunc;
95 void register_texture_type(MakeTextureFunc *func, const std::string &extensions);
97
98 MakeTextureFunc *get_texture_type(const std::string &extension) const;
99 void write_texture_types(std::ostream &out, int indent_level) const;
100
101 static TexturePool *get_global_ptr();
102
103private:
104 TexturePool();
105
106 bool ns_has_texture(const Filename &orig_filename);
107 Texture *ns_get_texture(const Filename &filename,
108 int primary_file_num_channels = 0,
109 bool read_mipmaps = false);
110 Texture *ns_get_texture(const Filename &filename,
111 const Filename &alpha_filename,
112 int primary_file_num_channels = 0,
113 int alpha_file_channel = 0,
114 bool read_mipmaps = false);
115 Texture *ns_load_texture(const Filename &orig_filename,
116 int primary_file_num_channels,
117 bool read_mipmaps,
118 const LoaderOptions &options);
119 Texture *ns_load_texture(const Filename &orig_filename,
120 const Filename &orig_alpha_filename,
121 int primary_file_num_channels,
122 int alpha_file_channel,
123 bool read_mipmaps,
124 const LoaderOptions &options);
125 Texture *ns_load_3d_texture(const Filename &filename_pattern,
126 bool read_mipmaps,
127 const LoaderOptions &options);
128 Texture *ns_load_2d_texture_array(const Filename &filename_pattern,
129 bool read_mipmaps,
130 const LoaderOptions &options);
131 Texture *ns_load_cube_map(const Filename &filename_pattern,
132 bool read_mipmaps,
133 const LoaderOptions &options);
134 Texture *ns_get_normalization_cube_map(int size);
135 Texture *ns_get_alpha_scale_map();
136
137 void ns_add_texture(Texture *texture);
138 void ns_release_texture(Texture *texture);
139 void ns_release_all_textures();
140 int ns_garbage_collect();
141 void ns_list_contents(std::ostream &out) const;
142 Texture *ns_find_texture(const std::string &name) const;
143 TextureCollection ns_find_all_textures(const std::string &name) const;
144 PT(Texture) ns_make_texture(const std::string &extension) const;
145
146 void resolve_filename(Filename &new_filename, const Filename &orig_filename,
147 bool read_mipmaps, const LoaderOptions &options);
148
149 void try_load_cache(PT(Texture) &tex, BamCache *cache,
150 const Filename &filename, PT(BamCacheRecord) &record,
151 bool &compressed_cache_record,
152 const LoaderOptions &options);
153 void report_texture_unreadable(const Filename &filename) const;
154
155 // Methods to invoke a TexturePoolFilter.
156 PT(Texture) pre_load(const Filename &orig_filename,
157 const Filename &orig_alpha_filename,
158 int primary_file_num_channels,
159 int alpha_file_channel,
160 bool read_mipmaps, const LoaderOptions &options);
161 PT(Texture) post_load(Texture *tex);
162
163 void load_filters();
164
165 static TexturePool *_global_ptr;
166
167 Mutex _lock;
168 struct LookupKey {
169 Filename _fullpath;
170 Filename _alpha_fullpath;
171 int _primary_file_num_channels = 0;
172 int _alpha_file_channel = 0;
173 Texture::TextureType _texture_type = Texture::TT_2d_texture;
174
175 INLINE bool operator < (const LookupKey &other) const;
176 };
177 typedef pmap<LookupKey, PT(Texture)> Textures;
178 Textures _textures;
179 typedef pmap<Filename, Filename> RelpathLookup;
180 RelpathLookup _relpath_lookup;
181
182 Filename _fake_texture_image;
183
184 PT(Texture) _normalization_cube_map;
185 PT(Texture) _alpha_scale_map;
186
187 typedef pmap<std::string, MakeTextureFunc *> TypeRegistry;
188 TypeRegistry _type_registry;
189
190 typedef pvector<TexturePoolFilter *> FilterRegistry;
191 FilterRegistry _filter_registry;
192};
193
194#include "texturePool.I"
195
196#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:44
Specifies parameters that may be passed to the loader.
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...
static Texture * load_texture(const Filename &filename, int primary_file_num_channels=0, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads the given filename up into a texture, if it has not already been loaded, and returns the new te...
Definition texturePool.I:70
static int garbage_collect()
Releases only those textures in the pool that have a reference count of exactly 1; i....
static void list_contents(std::ostream &out)
Lists the contents of the texture pool to the indicated output stream.
static void release_all_textures()
Releases all textures in the pool and restores the pool to the empty state.
static bool has_fake_texture_image()
Returns true if fake_texture_image mode has been enabled, false if we are in the normal mode.
static TexturePool * get_global_ptr()
Initializes and/or returns the global pointer to the one TexturePool object in the system.
static Texture * get_texture(const Filename &filename, int primary_file_num_channels=0, bool read_mipmaps=false)
Returns the texture that has already been previously loaded, or NULL otherwise.
Definition texturePool.I:41
static Texture * load_2d_texture_array(const Filename &filename_pattern, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads a 2-D texture array that is specified with a series of n pages, all numbered in sequence,...
static bool has_texture(const Filename &filename)
Returns true if the texture has ever been loaded, false otherwise.
Definition texturePool.I:20
static void set_fake_texture_image(const Filename &filename)
Sets a bogus filename that will be loaded in lieu of any textures requested from this point on.
void register_texture_type(MakeTextureFunc *func, const std::string &extensions)
Records a factory function that makes a Texture object of the appropriate type for one or more partic...
static bool verify_texture(const Filename &filename)
Loads the given filename up into a texture, if it has not already been loaded, and returns true to in...
Definition texturePool.I:32
static void add_texture(Texture *texture)
Adds the indicated already-loaded texture to the pool.
void register_filter(TexturePoolFilter *filter)
Records a TexturePoolFilter object that may operate on texture images as they are loaded from disk.
static void clear_fake_texture_image()
Restores normal behavior of loading the textures actually requested.
static Texture * load_cube_map(const Filename &filename_pattern, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads a cube map texture that is specified with a series of 6 pages, numbered 0 through 5.
static Texture * get_normalization_cube_map(int size)
Returns a standard Texture object that has been created with Texture::generate_normalization_cube_map...
static const Filename & get_fake_texture_image()
Returns the filename that was specified with a previous call to set_fake_texture_image().
void write_texture_types(std::ostream &out, int indent_level) const
Outputs a list of the available texture types to the indicated output stream.
static Texture * get_alpha_scale_map()
Returns a standard Texture object that has been created with Texture::generate_alpha_scale_map().
static void rehash()
Should be called when the model-path changes, to blow away the cache of texture pathnames found along...
static Texture * find_texture(const std::string &name)
Returns the first texture found in the pool that matches the indicated name (which may contain wildca...
static static PT(Texture) make_texture(const std void write(std::ostream &out)
Lists the contents of the texture pool to the indicated output stream.
static TextureCollection find_all_textures(const std::string &name="*")
Returns the set of all textures found in the pool that match the indicated name (which may contain wi...
static void release_texture(Texture *texture)
Removes the indicated texture from the pool, indicating it will never be loaded again; the texture ma...
static Texture * load_3d_texture(const Filename &filename_pattern, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads a 3-D texture that is specified with a series of n pages, all numbered in sequence,...
MakeTextureFunc * get_texture_type(const std::string &extension) const
Returns the factory function to construct a new texture of the type appropriate for the indicated fil...
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition texture.h:72
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.