Panda3D
 All Classes Functions Variables Enumerations
texture.h
1 // Filename: texture.h
2 // Created by: mike (09Jan97)
3 // Updated by: fperazzi, PandaSE(29Apr10) (added TT_2d_texture_array)
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #ifndef TEXTURE_H
17 #define TEXTURE_H
18 
19 #include "pandabase.h"
20 
21 #include "filename.h"
22 #include "typedWritableReferenceCount.h"
23 #include "namable.h"
24 #include "internalName.h"
25 #include "graphicsStateGuardianBase.h"
26 #include "updateSeq.h"
27 #include "pmap.h"
28 #include "config_gobj.h"
29 #include "pStatCollector.h"
30 #include "pmutex.h"
31 #include "mutexHolder.h"
32 #include "conditionVarFull.h"
33 #include "loaderOptions.h"
34 #include "string_utils.h"
35 #include "cycleData.h"
36 #include "cycleDataLockedReader.h"
37 #include "cycleDataReader.h"
38 #include "cycleDataWriter.h"
39 #include "cycleDataStageReader.h"
40 #include "cycleDataStageWriter.h"
41 #include "pipelineCycler.h"
42 #include "samplerState.h"
43 #include "pnmImage.h"
44 #include "colorSpace.h"
45 
46 class PNMImage;
47 class PfmFile;
48 class TextureContext;
49 class FactoryParams;
51 class CullTraverser;
52 class CullTraverserData;
53 class BamCacheRecord;
54 class TexturePeeker;
55 struct DDSHeader;
56 
57 ////////////////////////////////////////////////////////////////////
58 // Class : Texture
59 // Description : Represents a texture object, which is typically a
60 // single 2-d image but may also represent a 1-d or 3-d
61 // texture image, or the six 2-d faces of a cube map
62 // texture.
63 //
64 // A texture's image data might be stored in system RAM
65 // (see get_ram_image()) or its image may be represented
66 // in texture memory on one or more
67 // GraphicsStateGuardians (see prepare()), or both. The
68 // typical usage pattern is that a texture is loaded
69 // from an image file on disk, which copies its image
70 // data into system RAM; then the first time the texture
71 // is rendered its image data is copied to texture
72 // memory (actually, to the graphics API), and the
73 // system RAM image is automatically freed.
74 ////////////////////////////////////////////////////////////////////
75 class EXPCL_PANDA_GOBJ Texture : public TypedWritableReferenceCount, public Namable {
76 PUBLISHED:
77  typedef PT(Texture) MakeTextureFunc();
78 
79  enum TextureType {
80  TT_1d_texture,
81  TT_2d_texture,
82  TT_3d_texture,
83  TT_2d_texture_array,
84  TT_cube_map,
85  };
86 
87  enum ComponentType {
88  T_unsigned_byte,
89  T_unsigned_short,
90  T_float,
91  T_unsigned_int_24_8, // Packed
92  T_int,
93  };
94 
95  enum Format {
96  F_depth_stencil = 1,
97  F_color_index,
98  F_red,
99  F_green,
100  F_blue,
101  F_alpha,
102  F_rgb, // any suitable RGB mode, whatever the hardware prefers
103 
104  // The following request a particular number of bits for the GSG's
105  // internal_format (as stored in the framebuffer), but this
106  // request is not related to the pixel storage within the Texture
107  // object itself, which is always get_num_components() *
108  // get_component_width().
109  F_rgb5, // 5 bits per R,G,B channel
110  F_rgb8, // 8 bits per R,G,B channel
111  F_rgb12, // 12 bits per R,G,B channel
112  F_rgb332, // 3 bits per R & G, 2 bits for B
113 
114  F_rgba, // any suitable RGBA mode, whatever the hardware prefers
115 
116  // Again, the following bitdepth requests are only for the GSG;
117  // within the Texture object itself, these are all equivalent.
118  F_rgbm, // as above, but only requires 1 bit for alpha (i.e. mask)
119  F_rgba4, // 4 bits per R,G,B,A channel
120  F_rgba5, // 5 bits per R,G,B channel, 1 bit alpha
121  F_rgba8, // 8 bits per R,G,B,A channel
122  F_rgba12, // 12 bits per R,G,B,A channel
123 
124  F_luminance,
125  F_luminance_alpha, // 8 bits luminance, 8 bits alpha
126  F_luminance_alphamask, // 8 bits luminance, only needs 1 bit of alpha
127 
128  F_rgba16, // 16 bits per R,G,B,A channel
129  F_rgba32, // 32 bits per R,G,B,A channel
130 
131  F_depth_component,
132  F_depth_component16,
133  F_depth_component24,
134  F_depth_component32,
135 
136  F_r16,
137  F_rg16,
138  F_rgb16,
139 
140  // These formats are in the sRGB color space.
141  // RGB is 2.2 gamma corrected, alpha is always linear.
142  F_srgb,
143  F_srgb_alpha,
144  F_sluminance,
145  F_sluminance_alpha,
146 
147  F_r32i, // 32-bit integer, used for atomic access
148  F_r32,
149  F_rg32,
150  F_rgb32,
151 
152  F_r8i, // 8 integer bits per R channel
153  F_rg8i, // 8 integer bits per R,G channel
154  F_rgb8i, // 8 integer bits per R,G,B channel
155  F_rgba8i, // 8 integer bits per R,G,B,A channel
156  };
157 
158  // Deprecated. See SamplerState.FilterType.
159  enum DeprecatedFilterType {
160  FT_nearest = SamplerState::FT_nearest,
161  FT_linear = SamplerState::FT_linear,
162  FT_nearest_mipmap_nearest = SamplerState::FT_nearest_mipmap_nearest,
163  FT_linear_mipmap_nearest = SamplerState::FT_linear_mipmap_nearest,
164  FT_nearest_mipmap_linear = SamplerState::FT_nearest_mipmap_linear,
165  FT_linear_mipmap_linear = SamplerState::FT_linear_mipmap_linear,
166  FT_shadow = SamplerState::FT_shadow,
167  FT_default = SamplerState::FT_default,
168  FT_invalid = SamplerState::FT_invalid
169  };
170  typedef SamplerState::FilterType FilterType;
171 
172  // Deprecated. See SamplerState.WrapMode.
173  enum DeprecatedWrapMode {
174  WM_clamp = SamplerState::WM_clamp,
175  WM_repeat = SamplerState::WM_repeat,
176  WM_mirror = SamplerState::WM_mirror,
177  WM_mirror_once = SamplerState::WM_mirror_once,
178  WM_border_color = SamplerState::WM_border_color,
179  WM_invalid = SamplerState::WM_invalid
180  };
181  typedef SamplerState::WrapMode WrapMode;
182 
183  enum CompressionMode {
184  // Generic compression modes. Usually, you should choose one of
185  // these.
186  CM_default, // on or off, according to compressed-textures
187  CM_off, // uncompressed image
188  CM_on, // whatever compression the driver supports
189 
190  // Specific compression modes. Use only when you really want to
191  // use a particular compression algorithm. Use with caution; not
192  // all drivers support all compression modes. You can use
193  // GSG::get_supports_compressed_texture_format() to query the
194  // available compression modes for a particular GSG.
195  CM_fxt1,
196  CM_dxt1,
197  CM_dxt2,
198  CM_dxt3,
199  CM_dxt4,
200  CM_dxt5,
201  CM_pvr1_2bpp,
202  CM_pvr1_4bpp,
203  };
204 
205  enum QualityLevel {
206  QL_default, // according to texture-quality-level
207  QL_fastest,
208  QL_normal,
209  QL_best,
210  };
211 
212 PUBLISHED:
213  explicit Texture(const string &name = string());
214 
215 protected:
216  Texture(const Texture &copy);
217  void operator = (const Texture &copy);
218 
219 PUBLISHED:
220  virtual ~Texture();
221 
222  INLINE PT(Texture) make_copy() const;
223  INLINE void clear();
224 
225  INLINE void setup_texture(TextureType texture_type,
226  int x_size, int y_size, int z_size,
227  ComponentType component_type, Format format);
228  INLINE void setup_1d_texture();
229  INLINE void setup_1d_texture(int x_size,
230  ComponentType component_type, Format format);
231  INLINE void setup_2d_texture();
232  INLINE void setup_2d_texture(int x_size, int y_size,
233  ComponentType component_type, Format format);
234  INLINE void setup_3d_texture(int z_size = 1);
235  INLINE void setup_3d_texture(int x_size, int y_size, int z_size,
236  ComponentType component_type, Format format);
237  INLINE void setup_cube_map();
238  INLINE void setup_cube_map(int size,
239  ComponentType component_type, Format format);
240  INLINE void setup_2d_texture_array(int z_size = 1);
241  INLINE void setup_2d_texture_array(int x_size, int y_size, int z_size,
242  ComponentType component_type, Format format);
243  void generate_normalization_cube_map(int size);
244  void generate_alpha_scale_map();
245 
246  INLINE void clear_image();
247  INLINE bool has_clear_color() const;
248  INLINE LColor get_clear_color() const;
249  INLINE void set_clear_color(const LColor &color);
250  INLINE string get_clear_data() const;
251 
252  BLOCKING bool read(const Filename &fullpath, const LoaderOptions &options = LoaderOptions());
253  BLOCKING bool read(const Filename &fullpath, const Filename &alpha_fullpath,
254  int primary_file_num_channels, int alpha_file_channel,
255  const LoaderOptions &options = LoaderOptions());
256  BLOCKING bool read(const Filename &fullpath, int z, int n,
257  bool read_pages, bool read_mipmaps,
258  const LoaderOptions &options = LoaderOptions());
259  BLOCKING bool read(const Filename &fullpath, const Filename &alpha_fullpath,
260  int primary_file_num_channels, int alpha_file_channel,
261  int z, int n, bool read_pages, bool read_mipmaps,
262  BamCacheRecord *record = NULL,
263  const LoaderOptions &options = LoaderOptions());
264 
265  BLOCKING INLINE bool write(const Filename &fullpath);
266  BLOCKING INLINE bool write(const Filename &fullpath, int z, int n,
267  bool write_pages, bool write_mipmaps);
268 
269  BLOCKING bool read_txo(istream &in, const string &filename = "");
270  BLOCKING static PT(Texture) make_from_txo(istream &in, const string &filename = "");
271  BLOCKING bool write_txo(ostream &out, const string &filename = "") const;
272  BLOCKING bool read_dds(istream &in, const string &filename = "", bool header_only = false);
273 
274  BLOCKING INLINE bool load(const PNMImage &pnmimage, const LoaderOptions &options = LoaderOptions());
275  BLOCKING INLINE bool load(const PNMImage &pnmimage, int z, int n, const LoaderOptions &options = LoaderOptions());
276  BLOCKING INLINE bool load(const PfmFile &pfm, const LoaderOptions &options = LoaderOptions());
277  BLOCKING INLINE bool load(const PfmFile &pfm, int z, int n, const LoaderOptions &options = LoaderOptions());
278  BLOCKING INLINE bool load_sub_image(const PNMImage &pnmimage, int x, int y, int z=0, int n=0);
279  BLOCKING INLINE bool store(PNMImage &pnmimage) const;
280  BLOCKING INLINE bool store(PNMImage &pnmimage, int z, int n) const;
281  BLOCKING INLINE bool store(PfmFile &pfm) const;
282  BLOCKING INLINE bool store(PfmFile &pfm, int z, int n) const;
283 
284  BLOCKING INLINE bool reload();
285  BLOCKING Texture *load_related(const InternalName *suffix) const;
286 
287  INLINE bool has_filename() const;
288  INLINE const Filename &get_filename() const;
289  INLINE bool has_alpha_filename() const;
290  INLINE const Filename &get_alpha_filename() const;
291 
292  INLINE bool has_fullpath() const;
293  INLINE const Filename &get_fullpath() const;
294  INLINE bool has_alpha_fullpath() const;
295  INLINE const Filename &get_alpha_fullpath() const;
296 
297  INLINE int get_x_size() const;
298  INLINE int get_y_size() const;
299  INLINE int get_z_size() const;
300  INLINE int get_num_views() const;
301  INLINE int get_num_pages() const;
302  INLINE int get_num_components() const;
303  INLINE int get_component_width() const;
304  INLINE TextureType get_texture_type() const;
305  INLINE Format get_format() const;
306  INLINE ComponentType get_component_type() const;
307 
308  INLINE void set_wrap_u(WrapMode wrap);
309  INLINE void set_wrap_v(WrapMode wrap);
310  INLINE void set_wrap_w(WrapMode wrap);
311  INLINE void set_minfilter(FilterType filter);
312  INLINE void set_magfilter(FilterType filter);
313  INLINE void set_anisotropic_degree(int anisotropic_degree);
314  INLINE void set_border_color(const LColor &color);
315  INLINE void set_compression(CompressionMode compression);
316  INLINE void set_render_to_texture(bool render_to_texture);
317 
318  INLINE const SamplerState &get_default_sampler() const;
319  INLINE SamplerState::WrapMode get_wrap_u() const;
320  INLINE SamplerState::WrapMode get_wrap_v() const;
321  INLINE SamplerState::WrapMode get_wrap_w() const;
322  INLINE SamplerState::FilterType get_minfilter() const;
323  INLINE SamplerState::FilterType get_magfilter() const;
324  INLINE SamplerState::FilterType get_effective_minfilter() const;
325  INLINE SamplerState::FilterType get_effective_magfilter() const;
326  INLINE int get_anisotropic_degree() const;
327  INLINE int get_effective_anisotropic_degree() const;
328  INLINE LColor get_border_color() const;
329  INLINE CompressionMode get_compression() const;
330  INLINE bool has_compression() const;
331  INLINE bool get_render_to_texture() const;
332  INLINE bool uses_mipmaps() const;
333 
334  INLINE void set_quality_level(QualityLevel quality_level);
335  INLINE QualityLevel get_quality_level() const;
336  INLINE QualityLevel get_effective_quality_level() const;
337 
338  INLINE int get_expected_num_mipmap_levels() const;
339  INLINE int get_expected_mipmap_x_size(int n) const;
340  INLINE int get_expected_mipmap_y_size(int n) const;
341  INLINE int get_expected_mipmap_z_size(int n) const;
342  INLINE int get_expected_mipmap_num_pages(int n) const;
343 
344  INLINE bool has_ram_image() const;
345  INLINE bool has_uncompressed_ram_image() const;
346  INLINE bool might_have_ram_image() const;
347  INLINE size_t get_ram_image_size() const;
348  INLINE size_t get_ram_view_size() const;
349  INLINE size_t get_ram_page_size() const;
350  INLINE size_t get_expected_ram_image_size() const;
351  INLINE size_t get_expected_ram_page_size() const;
352  INLINE CPTA_uchar get_ram_image();
353  INLINE CompressionMode get_ram_image_compression() const;
354  INLINE CPTA_uchar get_uncompressed_ram_image();
355  CPTA_uchar get_ram_image_as(const string &requested_format);
356  INLINE PTA_uchar modify_ram_image();
357  INLINE PTA_uchar make_ram_image();
358  INLINE void set_ram_image(CPTA_uchar image, CompressionMode compression = CM_off,
359  size_t page_size = 0);
360  void set_ram_image_as(CPTA_uchar image, const string &provided_format);
361  INLINE void clear_ram_image();
362  INLINE void set_keep_ram_image(bool keep_ram_image);
363  virtual bool get_keep_ram_image() const;
364  virtual bool is_cacheable() const;
365 
366  INLINE bool compress_ram_image(CompressionMode compression = CM_on,
367  QualityLevel quality_level = QL_default,
368  GraphicsStateGuardianBase *gsg = NULL);
369  INLINE bool uncompress_ram_image();
370 
371  INLINE int get_num_ram_mipmap_images() const;
372  INLINE bool has_ram_mipmap_image(int n) const;
373  int get_num_loadable_ram_mipmap_images() const;
374  INLINE bool has_all_ram_mipmap_images() const;
375  INLINE size_t get_ram_mipmap_image_size(int n) const;
376  INLINE size_t get_ram_mipmap_view_size(int n) const;
377  INLINE size_t get_ram_mipmap_page_size(int n) const;
378  INLINE size_t get_expected_ram_mipmap_image_size(int n) const;
379  INLINE size_t get_expected_ram_mipmap_view_size(int n) const;
380  INLINE size_t get_expected_ram_mipmap_page_size(int n) const;
381  CPTA_uchar get_ram_mipmap_image(int n) const;
382  void *get_ram_mipmap_pointer(int n) const;
383  INLINE PTA_uchar modify_ram_mipmap_image(int n);
384  INLINE PTA_uchar make_ram_mipmap_image(int n);
385  void set_ram_mipmap_pointer(int n, void *image, size_t page_size = 0);
386  void set_ram_mipmap_pointer_from_int(long long pointer, int n, int page_size);
387  INLINE void set_ram_mipmap_image(int n, CPTA_uchar image, size_t page_size = 0);
388  void clear_ram_mipmap_image(int n);
389  INLINE void clear_ram_mipmap_images();
390  INLINE void generate_ram_mipmap_images();
391 
392  INLINE int get_simple_x_size() const;
393  INLINE int get_simple_y_size() const;
394  INLINE bool has_simple_ram_image() const;
395  INLINE size_t get_simple_ram_image_size() const;
396  INLINE CPTA_uchar get_simple_ram_image() const;
397  INLINE void set_simple_ram_image(CPTA_uchar image, int x_size, int y_size);
398  PTA_uchar modify_simple_ram_image();
399  PTA_uchar new_simple_ram_image(int x_size, int y_size);
400  void generate_simple_ram_image();
401  INLINE void clear_simple_ram_image();
402 
403  PT(TexturePeeker) peek();
404 
405  INLINE UpdateSeq get_properties_modified() const;
406  INLINE UpdateSeq get_image_modified() const;
407  INLINE UpdateSeq get_simple_image_modified() const;
408 
409  INLINE void set_auto_texture_scale(AutoTextureScale scale);
410  INLINE AutoTextureScale get_auto_texture_scale() const;
411  INLINE bool has_auto_texture_scale() const;
412 
413  void prepare(PreparedGraphicsObjects *prepared_objects);
414  bool is_prepared(PreparedGraphicsObjects *prepared_objects) const;
415  bool was_image_modified(PreparedGraphicsObjects *prepared_objects) const;
416  size_t get_data_size_bytes(PreparedGraphicsObjects *prepared_objects) const;
417  bool get_active(PreparedGraphicsObjects *prepared_objects) const;
418  bool get_resident(PreparedGraphicsObjects *prepared_objects) const;
419 
420  bool release(PreparedGraphicsObjects *prepared_objects);
421  int release_all();
422 
423  void write(ostream &out, int indent_level) const;
424 
425  size_t estimate_texture_memory() const;
426 
427  void set_aux_data(const string &key, TypedReferenceCount *aux_data);
428  void clear_aux_data(const string &key);
429  TypedReferenceCount *get_aux_data(const string &key) const;
430 
431  INLINE static void set_textures_power_2(AutoTextureScale scale);
432  INLINE static AutoTextureScale get_textures_power_2();
433  INLINE static bool has_textures_power_2();
434 
435 PUBLISHED:
436  // These are published, but in general, you shouldn't be mucking
437  // with these values; they are set automatically when a texture is
438  // loaded.
439  INLINE void set_filename(const Filename &filename);
440  INLINE void clear_filename();
441  INLINE void set_alpha_filename(const Filename &alpha_filename);
442  INLINE void clear_alpha_filename();
443 
444  INLINE void set_fullpath(const Filename &fullpath);
445  INLINE void clear_fullpath();
446  INLINE void set_alpha_fullpath(const Filename &alpha_fullpath);
447  INLINE void clear_alpha_fullpath();
448 
449  INLINE void set_x_size(int x_size);
450  INLINE void set_y_size(int y_size);
451  INLINE void set_z_size(int z_size);
452  INLINE void set_num_views(int num_views);
453 
454  INLINE int get_pad_x_size() const;
455  INLINE int get_pad_y_size() const;
456  INLINE int get_pad_z_size() const;
457  INLINE LVecBase2 get_tex_scale() const;
458 
459  INLINE void set_pad_size(int x=0, int y=0, int z=0);
460  void set_size_padded(int x=1, int y=1, int z=1);
461 
462  INLINE int get_orig_file_x_size() const;
463  INLINE int get_orig_file_y_size() const;
464  INLINE int get_orig_file_z_size() const;
465 
466  void set_orig_file_size(int x, int y, int z = 1);
467 
468  INLINE void set_format(Format format);
469  INLINE void set_component_type(ComponentType component_type);
470  INLINE void set_loaded_from_image();
471  INLINE bool get_loaded_from_image() const;
472 
473  INLINE void set_loaded_from_txo();
474  INLINE bool get_loaded_from_txo() const;
475 
476  INLINE bool get_match_framebuffer_format() const;
477  INLINE void set_match_framebuffer_format(bool flag);
478 
479  INLINE bool get_post_load_store_cache() const;
480  INLINE void set_post_load_store_cache(bool flag);
481 
482  TextureContext *prepare_now(int view,
483  PreparedGraphicsObjects *prepared_objects,
485 
486  static int up_to_power_2(int value);
487  static int down_to_power_2(int value);
488 
489  void consider_rescale(PNMImage &pnmimage);
490  static void consider_rescale(PNMImage &pnmimage, const string &name, AutoTextureScale auto_texture_scale = ATS_unspecified);
491  INLINE bool rescale_texture();
492 
493  static string format_texture_type(TextureType tt);
494  static TextureType string_texture_type(const string &str);
495 
496  static string format_component_type(ComponentType ct);
497  static ComponentType string_component_type(const string &str);
498 
499  static string format_format(Format f);
500  static Format string_format(const string &str);
501 
502  static string format_compression_mode(CompressionMode cm);
503  static CompressionMode string_compression_mode(const string &str);
504 
505  static string format_quality_level(QualityLevel tql);
506  static QualityLevel string_quality_level(const string &str);
507 
508 public:
509  void texture_uploaded();
510 
511  virtual bool has_cull_callback() const;
512  virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
513 
514  static PT(Texture) make_texture();
515 
516 public:
517  static bool is_specific(CompressionMode compression);
518  static bool has_alpha(Format format);
519  static bool has_binary_alpha(Format format);
520  static bool is_srgb(Format format);
521 
522  static bool adjust_size(int &x_size, int &y_size, const string &name,
523  bool for_padding, AutoTextureScale auto_texture_scale = ATS_unspecified);
524  INLINE bool adjust_this_size(int &x_size, int &y_size, const string &name,
525  bool for_padding) const;
526 
527  virtual void ensure_loader_type(const Filename &filename);
528 
529 protected:
530  class CData;
531 
532  virtual void reconsider_dirty();
533 
534  // All of the functions in this class that begin "do_" are protected
535  // methods. Many of them are implementations of public-facing
536  // versions of the same methods.
537 
538  // All of these assume the CData lock is already held (and receive a
539  // CData pointer representing that lock); generally, they also avoid
540  // adjusting the _properties_modified and _image_modified
541  // semaphores.
542  virtual bool do_adjust_this_size(const CData *cdata,
543  int &x_size, int &y_size, const string &name,
544  bool for_padding) const;
545 
546  virtual bool do_read(CData *cdata,
547  const Filename &fullpath, const Filename &alpha_fullpath,
548  int primary_file_num_channels, int alpha_file_channel,
549  int z, int n, bool read_pages, bool read_mipmaps,
550  const LoaderOptions &options, BamCacheRecord *record);
551  virtual bool do_read_one(CData *cdata,
552  const Filename &fullpath, const Filename &alpha_fullpath,
553  int z, int n, int primary_file_num_channels, int alpha_file_channel,
554  const LoaderOptions &options,
555  bool header_only, BamCacheRecord *record);
556  virtual bool do_load_one(CData *cdata,
557  const PNMImage &pnmimage, const string &name,
558  int z, int n, const LoaderOptions &options);
559  virtual bool do_load_one(CData *cdata,
560  const PfmFile &pfm, const string &name,
561  int z, int n, const LoaderOptions &options);
562  virtual bool do_load_sub_image(CData *cdata, const PNMImage &image,
563  int x, int y, int z, int n);
564  bool do_read_txo_file(CData *cdata, const Filename &fullpath);
565  bool do_read_txo(CData *cdata, istream &in, const string &filename);
566  bool do_read_dds_file(CData *cdata, const Filename &fullpath, bool header_only);
567  bool do_read_dds(CData *cdata, istream &in, const string &filename, bool header_only);
568 
569  bool do_write(CData *cdata, const Filename &fullpath, int z, int n,
570  bool write_pages, bool write_mipmaps);
571  bool do_write_one(CData *cdata, const Filename &fullpath, int z, int n);
572  bool do_store_one(CData *cdata, PNMImage &pnmimage, int z, int n);
573  bool do_store_one(CData *cdata, PfmFile &pfm, int z, int n);
574  bool do_write_txo_file(const CData *cdata, const Filename &fullpath) const;
575  bool do_write_txo(const CData *cdata, ostream &out, const string &filename) const;
576 
577  virtual CData *unlocked_ensure_ram_image(bool allow_compression);
578  virtual void do_reload_ram_image(CData *cdata, bool allow_compression);
579 
580  PTA_uchar do_modify_ram_image(CData *cdata);
581  PTA_uchar do_make_ram_image(CData *cdata);
582  void do_set_ram_image(CData *cdata, CPTA_uchar image,
583  CompressionMode compression = CM_off, size_t page_size = 0);
584  PTA_uchar do_modify_ram_mipmap_image(CData *cdata, int n);
585  PTA_uchar do_make_ram_mipmap_image(CData *cdata, int n);
586  void do_set_ram_mipmap_image(CData *cdata, int n, CPTA_uchar image, size_t page_size);
587  int do_get_clear_data(const CData *cdata, unsigned char *into) const;
588 
589  bool consider_auto_process_ram_image(bool generate_mipmaps, bool allow_compression);
590  bool do_consider_auto_process_ram_image(CData *cdata, bool generate_mipmaps,
591  bool allow_compression);
592  bool do_compress_ram_image(CData *cdata, CompressionMode compression,
593  QualityLevel quality_level,
595  bool do_uncompress_ram_image(CData *cdata);
596  bool do_has_all_ram_mipmap_images(const CData *cdata) const;
597 
598  bool do_reconsider_z_size(CData *cdata, int z, const LoaderOptions &options);
599  virtual void do_allocate_pages(CData *cdata);
600  bool do_reconsider_image_properties(CData *cdata,
601  int x_size, int y_size, int num_components,
602  ComponentType component_type, int z,
603  const LoaderOptions &options);
604  bool do_rescale_texture(CData *cdata);
605 
606  virtual PT(Texture) make_copy_impl() const;
607  PT(Texture) do_make_copy(const CData *cdata) const;
608  void do_assign(CData *cdata, const Texture *copy, const CData *cdata_copy);
609  virtual void do_clear(CData *cdata);
610  void do_setup_texture(CData *cdata,
611  TextureType texture_type, int x_size, int y_size,
612  int z_size, ComponentType component_type,
613  Format format);
614  void do_set_num_views(CData *cdata, int num_views);
615  void do_set_format(CData *cdata, Format format);
616  void do_set_component_type(CData *cdata, ComponentType component_type);
617  void do_set_x_size(CData *cdata, int x_size);
618  void do_set_y_size(CData *cdata, int y_size);
619  void do_set_z_size(CData *cdata, int z_size);
620 
621  void do_set_wrap_u(CData *cdata, WrapMode wrap);
622  void do_set_wrap_v(CData *cdata, WrapMode wrap);
623  void do_set_wrap_w(CData *cdata, WrapMode wrap);
624  void do_set_minfilter(CData *cdata, FilterType filter);
625  void do_set_magfilter(CData *cdata, FilterType filter);
626  void do_set_anisotropic_degree(CData *cdata, int anisotropic_degree);
627  void do_set_border_color(CData *cdata, const LColor &color);
628  void do_set_compression(CData *cdata, CompressionMode compression);
629  void do_set_quality_level(CData *cdata, QualityLevel quality_level);
630 
631  bool do_has_compression(const CData *cdata) const;
632  virtual bool do_has_ram_image(const CData *cdata) const;
633  virtual bool do_has_uncompressed_ram_image(const CData *cdata) const;
634  CPTA_uchar do_get_ram_image(CData *cdata);
635  CPTA_uchar do_get_uncompressed_ram_image(CData *cdata);
636  void do_set_simple_ram_image(CData *cdata, CPTA_uchar image, int x_size, int y_size);
637  INLINE size_t do_get_ram_image_size(const CData *cdata) const;
638  INLINE bool do_has_ram_mipmap_image(const CData *cdata, int n) const;
639  int do_get_expected_num_mipmap_levels(const CData *cdata) const;
640  INLINE size_t do_get_expected_ram_image_size(const CData *cdata) const;
641  INLINE size_t do_get_expected_ram_view_size(const CData *cdata) const;
642  INLINE size_t do_get_expected_ram_page_size(const CData *cdata) const;
643  size_t do_get_ram_mipmap_page_size(const CData *cdata, int n) const;
644  INLINE size_t do_get_expected_ram_mipmap_image_size(const CData *cdata, int n) const;
645  INLINE size_t do_get_expected_ram_mipmap_view_size(const CData *cdata, int n) const;
646  INLINE size_t do_get_expected_ram_mipmap_page_size(const CData *cdata, int n) const;
647  int do_get_expected_mipmap_x_size(const CData *cdata, int n) const;
648  int do_get_expected_mipmap_y_size(const CData *cdata, int n) const;
649  int do_get_expected_mipmap_z_size(const CData *cdata, int n) const;
650  INLINE int do_get_expected_mipmap_num_pages(const CData *cdata, int n) const;
651  INLINE void do_clear_ram_image(CData *cdata);
652  void do_clear_simple_ram_image(CData *cdata);
653  void do_clear_ram_mipmap_images(CData *cdata);
654  void do_generate_ram_mipmap_images(CData *cdata);
655  void do_set_pad_size(CData *cdata, int x, int y, int z);
656  virtual bool do_can_reload(const CData *cdata) const;
657  bool do_reload(CData *cdata);
658 
659  INLINE AutoTextureScale do_get_auto_texture_scale(const CData *cdata) const;
660 
661  virtual bool do_has_bam_rawdata(const CData *cdata) const;
662  virtual void do_get_bam_rawdata(CData *cdata);
663 
664  // This nested class declaration is used below.
665  class RamImage {
666  public:
667  INLINE RamImage();
668 
669  PTA_uchar _image;
670  size_t _page_size;
671 
672  // If _pointer_image is non-NULL, it represents an external block
673  // of memory that is used instead of the above PTA_uchar.
674  void *_pointer_image;
675  };
676 
677 private:
678  static void convert_from_pnmimage(PTA_uchar &image, size_t page_size,
679  int row_stride, int x, int y, int z,
680  const PNMImage &pnmimage,
681  int num_components, int component_width);
682  static void convert_from_pfm(PTA_uchar &image, size_t page_size,
683  int z, const PfmFile &pfm,
684  int num_components, int component_width);
685  static bool convert_to_pnmimage(PNMImage &pnmimage, int x_size, int y_size,
686  int num_components, int component_width,
687  CPTA_uchar image, size_t page_size,
688  int z);
689  static bool convert_to_pfm(PfmFile &pfm, int x_size, int y_size,
690  int num_components, int component_width,
691  CPTA_uchar image, size_t page_size,
692  int z);
693  static PTA_uchar read_dds_level_bgr8(Texture *tex, CData *cdata, const DDSHeader &header,
694  int n, istream &in);
695  static PTA_uchar read_dds_level_rgb8(Texture *tex, CData *cdata, const DDSHeader &header,
696  int n, istream &in);
697  static PTA_uchar read_dds_level_abgr8(Texture *tex, CData *cdata, const DDSHeader &header,
698  int n, istream &in);
699  static PTA_uchar read_dds_level_rgba8(Texture *tex, CData *cdata, const DDSHeader &header,
700  int n, istream &in);
701  static PTA_uchar read_dds_level_generic_uncompressed(Texture *tex, CData *cdata,
702  const DDSHeader &header,
703  int n, istream &in);
704  static PTA_uchar read_dds_level_luminance_uncompressed(Texture *tex, CData *cdata,
705  const DDSHeader &header,
706  int n, istream &in);
707  static PTA_uchar read_dds_level_dxt1(Texture *tex, CData *cdata,
708  const DDSHeader &header,
709  int n, istream &in);
710  static PTA_uchar read_dds_level_dxt23(Texture *tex, CData *cdata,
711  const DDSHeader &header,
712  int n, istream &in);
713  static PTA_uchar read_dds_level_dxt45(Texture *tex, CData *cdata,
714  const DDSHeader &header,
715  int n, istream &in);
716 
717  void clear_prepared(int view, PreparedGraphicsObjects *prepared_objects);
718 
719  static void consider_downgrade(PNMImage &pnmimage, int num_channels, const string &name);
720 
721  static bool compare_images(const PNMImage &a, const PNMImage &b);
722 
723  INLINE static void store_unscaled_byte(unsigned char *&p, int value);
724  INLINE static void store_unscaled_short(unsigned char *&p, int value);
725  INLINE static void store_scaled_byte(unsigned char *&p, int value, double scale);
726  INLINE static void store_scaled_short(unsigned char *&p, int value, double scale);
727  INLINE static double get_unsigned_byte(const unsigned char *&p);
728  INLINE static double get_unsigned_short(const unsigned char *&p);
729 
730  INLINE static bool is_txo_filename(const Filename &fullpath);
731  INLINE static bool is_dds_filename(const Filename &fullpath);
732 
733  void do_filter_2d_mipmap_pages(const CData *cdata,
734  RamImage &to, const RamImage &from,
735  int x_size, int y_size) const;
736 
737  void do_filter_3d_mipmap_level(const CData *cdata,
738  RamImage &to, const RamImage &from,
739  int x_size, int y_size, int z_size) const;
740 
741  typedef void Filter2DComponent(unsigned char *&p,
742  const unsigned char *&q,
743  size_t pixel_size, size_t row_size);
744 
745  typedef void Filter3DComponent(unsigned char *&p,
746  const unsigned char *&q,
747  size_t pixel_size, size_t row_size,
748  size_t page_size);
749 
750  static void filter_2d_unsigned_byte(unsigned char *&p,
751  const unsigned char *&q,
752  size_t pixel_size, size_t row_size);
753  static void filter_2d_unsigned_byte_srgb(unsigned char *&p,
754  const unsigned char *&q,
755  size_t pixel_size, size_t row_size);
756  static void filter_2d_unsigned_byte_srgb_sse2(unsigned char *&p,
757  const unsigned char *&q,
758  size_t pixel_size, size_t row_size);
759  static void filter_2d_unsigned_short(unsigned char *&p,
760  const unsigned char *&q,
761  size_t pixel_size, size_t row_size);
762  static void filter_2d_float(unsigned char *&p, const unsigned char *&q,
763  size_t pixel_size, size_t row_size);
764 
765  static void filter_3d_unsigned_byte(unsigned char *&p,
766  const unsigned char *&q,
767  size_t pixel_size, size_t row_size,
768  size_t page_size);
769  static void filter_3d_unsigned_byte_srgb(unsigned char *&p,
770  const unsigned char *&q,
771  size_t pixel_size, size_t row_size,
772  size_t page_size);
773  static void filter_3d_unsigned_byte_srgb_sse2(unsigned char *&p,
774  const unsigned char *&q,
775  size_t pixel_size, size_t row_size,
776  size_t page_size);
777  static void filter_3d_unsigned_short(unsigned char *&p,
778  const unsigned char *&q,
779  size_t pixel_size, size_t row_size,
780  size_t page_size);
781  static void filter_3d_float(unsigned char *&p, const unsigned char *&q,
782  size_t pixel_size, size_t row_size, size_t page_size);
783 
784  bool do_squish(CData *cdata, CompressionMode compression, int squish_flags);
785  bool do_unsquish(CData *cdata, int squish_flags);
786 
787 protected:
789 
790  // This is the data that must be cycled between pipeline stages.
791  class EXPCL_PANDA_GOBJ CData : public CycleData {
792  public:
793  CData();
794  CData(const CData &copy);
795  ALLOC_DELETED_CHAIN(CData);
796  virtual CycleData *make_copy() const;
797  virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
798  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
799  virtual void fillin(DatagramIterator &scan, BamReader *manager);
800  virtual TypeHandle get_parent_type() const {
801  return Texture::get_class_type();
802  }
803 
804  void do_assign(const CData *copy);
805  INLINE void inc_properties_modified();
806  INLINE void inc_image_modified();
807  INLINE void inc_simple_image_modified();
808 
809  Filename _filename;
810  Filename _alpha_filename;
811  Filename _fullpath;
812  Filename _alpha_fullpath;
813 
814  // The number of channels of the primary file we use. 1, 2, 3, or 4.
815  int _primary_file_num_channels;
816 
817  // If we have a separate alpha file, this designates which channel
818  // in the alpha file provides the alpha channel. 0 indicates the
819  // combined grayscale value of rgb; otherwise, 1, 2, 3, or 4 are
820  // valid.
821  int _alpha_file_channel;
822 
823  int _x_size;
824  int _y_size;
825  int _z_size;
826  int _num_views;
827  int _num_components;
828  int _component_width;
829  TextureType _texture_type;
830  Format _format;
831  ComponentType _component_type;
832 
833  bool _loaded_from_image;
834  bool _loaded_from_txo;
835  bool _has_read_pages;
836  bool _has_read_mipmaps;
837  int _num_mipmap_levels_read;
838 
839  SamplerState _default_sampler;
840  bool _keep_ram_image;
841  LColor _border_color;
842  CompressionMode _compression;
843  bool _render_to_texture;
844  bool _match_framebuffer_format;
845  bool _post_load_store_cache;
846  QualityLevel _quality_level;
847 
848  int _pad_x_size;
849  int _pad_y_size;
850  int _pad_z_size;
851 
852  int _orig_file_x_size;
853  int _orig_file_y_size;
854 
855  AutoTextureScale _auto_texture_scale;
856  CompressionMode _ram_image_compression;
857 
858  // There is usually one RamImage for the mipmap level 0 (the base
859  // image). There may or may not also be additional images for the
860  // additional mipmap levels.
861  RamImages _ram_images;
862 
863  // This is the simple image, which may be loaded before the texture
864  // is loaded from disk. It exists only for 2-d textures.
865  RamImage _simple_ram_image;
866  int _simple_x_size;
867  int _simple_y_size;
868  PN_int32 _simple_image_date_generated;
869 
870  // This is the color that should be used when no image was given.
871  bool _has_clear_color;
872  LColor _clear_color;
873 
874  UpdateSeq _properties_modified;
875  UpdateSeq _image_modified;
876  UpdateSeq _simple_image_modified;
877 
878  public:
879  static TypeHandle get_class_type() {
880  return _type_handle;
881  }
882  static void init_type() {
883  register_type(_type_handle, "Texture::CData");
884  }
885 
886  private:
887  static TypeHandle _type_handle;
888  };
889 
890  PipelineCycler<CData> _cycler;
896 
897  // Protects the remaining members of this class.
898  Mutex _lock;
899 
900  // Used to implement unlocked_reload_ram_image().
901  ConditionVarFull _cvar; // condition: _reloading is true.
902  bool _reloading;
903 
904  // A Texture keeps a list (actually, a map) of all the
905  // PreparedGraphicsObjects tables that it has been prepared into.
906  // Each PGO conversely keeps a list (a set) of all the Textures that
907  // have been prepared there. When either destructs, it removes
908  // itself from the other's list.
911  PreparedViews _prepared_views;
912 
913  // It is common, when using normal maps, specular maps, gloss maps,
914  // and such, to use a file naming convention where the filenames
915  // of the special maps are derived by concatenating a suffix to
916  // the name of the diffuse map. The following table enables
917  // lookup of the special maps given the diffuse map and the suffix.
919  RelatedTextures _related_textures;
920 
921  // The TexturePool finds this useful.
922  Filename _texture_pool_key;
923 
924 private:
925  // The auxiliary data is not recorded to a bam file.
927  AuxData _aux_data;
928 
929  static AutoTextureScale _textures_power_2;
930  static PStatCollector _texture_read_pcollector;
931 
932  // Datagram stuff
933 public:
934  static void register_with_read_factory();
935  virtual void write_datagram(BamWriter *manager, Datagram &me);
936 
937  virtual void finalize(BamReader *manager);
938 
939 protected:
940  void do_write_datagram_header(CData *cdata, BamWriter *manager, Datagram &me, bool &has_rawdata);
941  virtual void do_write_datagram_body(CData *cdata, BamWriter *manager, Datagram &me);
942  virtual void do_write_datagram_rawdata(CData *cdata, BamWriter *manager, Datagram &me);
943  static TypedWritable *make_from_bam(const FactoryParams &params);
944  virtual TypedWritable *make_this_from_bam(const FactoryParams &params);
945  virtual void do_fillin_body(CData *cdata, DatagramIterator &scan, BamReader *manager);
946  virtual void do_fillin_rawdata(CData *cdata, DatagramIterator &scan, BamReader *manager);
947  virtual void do_fillin_from(CData *cdata, const Texture *dummy);
948 
949 public:
950  static TypeHandle get_class_type() {
951  return _type_handle;
952  }
953  static void init_type() {
954  TypedWritableReferenceCount::init_type();
955  register_type(_type_handle, "Texture",
956  TypedWritableReferenceCount::get_class_type());
957  CData::init_type();
958  }
959  virtual TypeHandle get_type() const {
960  return get_class_type();
961  }
962  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
963 
964 private:
965 
966  static TypeHandle _type_handle;
967 
968  friend class TextureContext;
969  friend class PreparedGraphicsObjects;
970  friend class TexturePool;
971  friend class TexturePeeker;
972 };
973 
974 extern EXPCL_PANDA_GOBJ ConfigVariableEnum<Texture::QualityLevel> texture_quality_level;
975 
976 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::TextureType tt);
977 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::ComponentType ct);
978 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::Format f);
979 
980 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::CompressionMode cm);
981 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::QualityLevel tql);
982 EXPCL_PANDA_GOBJ istream &operator >> (istream &in, Texture::QualityLevel &tql);
983 
984 #include "texture.I"
985 
986 #endif
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:26
This class is similar to CycleDataWriter, except it allows writing to a particular stage of the pipel...
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is a special class object that holds all the information returned by a particular GSG to indicat...
This collects together the pieces of data that are accumulated for each node while walking the scene ...
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
This is our own Panda specialization on the default STL list.
Definition: plist.h:38
A table of objects that are saved within the graphics context for reference by handle later...
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
A lightweight class that represents a single element that may be timed and/or counted via stats...
A base class for all things which can have a name.
Definition: namable.h:29
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
Defines a pfm file, a 2-d table of floating-point numbers, either 3-component or 1-component, or with a special extension, 2- or 4-component.
Definition: pfmFile.h:34
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
This class specializes ConfigVariable as an enumerated type.
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
An instance of this object is returned by Texture::peek().
Definition: texturePeeker.h:30
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:39
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This class is similar to CycleDataReader, except it allows reading from a particular stage of the pip...
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
This class implements a condition variable; see ConditionVar for a brief introduction to this class...
A class to retrieve the individual data elements previously stored in a Datagram. ...
virtual void write_datagram(BamWriter *, Datagram &) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: cycleData.cxx:34
This is the preferred interface for loading textures from image files.
Definition: texturePool.h:40
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is a sequence number that increments monotonically.
Definition: updateSeq.h:43
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48