Panda3D
|
00001 // Filename: texture.h 00002 // Created by: mike (09Jan97) 00003 // Updated by: fperazzi, PandaSE(29Apr10) (added TT_2d_texture_array) 00004 // 00005 //////////////////////////////////////////////////////////////////// 00006 // 00007 // PANDA 3D SOFTWARE 00008 // Copyright (c) Carnegie Mellon University. All rights reserved. 00009 // 00010 // All use of this software is subject to the terms of the revised BSD 00011 // license. You should have received a copy of this license along 00012 // with this source code in a file named "LICENSE." 00013 // 00014 //////////////////////////////////////////////////////////////////// 00015 00016 #ifndef TEXTURE_H 00017 #define TEXTURE_H 00018 00019 #include "pandabase.h" 00020 00021 #include "filename.h" 00022 #include "typedWritableReferenceCount.h" 00023 #include "namable.h" 00024 #include "internalName.h" 00025 #include "graphicsStateGuardianBase.h" 00026 #include "updateSeq.h" 00027 #include "pmap.h" 00028 #include "config_gobj.h" 00029 #include "pStatCollector.h" 00030 #include "pmutex.h" 00031 #include "mutexHolder.h" 00032 #include "conditionVarFull.h" 00033 #include "loaderOptions.h" 00034 #include "string_utils.h" 00035 #include "cycleData.h" 00036 #include "cycleDataLockedReader.h" 00037 #include "cycleDataReader.h" 00038 #include "cycleDataWriter.h" 00039 #include "cycleDataStageReader.h" 00040 #include "cycleDataStageWriter.h" 00041 #include "pipelineCycler.h" 00042 00043 class PNMImage; 00044 class TextureContext; 00045 class FactoryParams; 00046 class PreparedGraphicsObjects; 00047 class CullTraverser; 00048 class CullTraverserData; 00049 class BamCacheRecord; 00050 class TexturePeeker; 00051 struct DDSHeader; 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Class : Texture 00055 // Description : Represents a texture object, which is typically a 00056 // single 2-d image but may also represent a 1-d or 3-d 00057 // texture image, or the six 2-d faces of a cube map 00058 // texture. 00059 // 00060 // A texture's image data might be stored in system RAM 00061 // (see get_ram_image()) or its image may be represented 00062 // in texture memory on one or more 00063 // GraphicsStateGuardians (see prepare()), or both. The 00064 // typical usage pattern is that a texture is loaded 00065 // from an image file on disk, which copies its image 00066 // data into system RAM; then the first time the texture 00067 // is rendered its image data is copied to texture 00068 // memory (actually, to the graphics API), and the 00069 // system RAM image is automatically freed. 00070 //////////////////////////////////////////////////////////////////// 00071 class EXPCL_PANDA_GOBJ Texture : public TypedWritableReferenceCount, public Namable { 00072 PUBLISHED: 00073 typedef PT(Texture) MakeTextureFunc(); 00074 00075 enum TextureType { 00076 TT_1d_texture, 00077 TT_2d_texture, 00078 TT_3d_texture, 00079 TT_2d_texture_array, 00080 TT_cube_map, 00081 }; 00082 00083 enum ComponentType { 00084 T_unsigned_byte, 00085 T_unsigned_short, 00086 T_float, 00087 T_unsigned_int_24_8, 00088 }; 00089 00090 enum Format { 00091 F_depth_stencil = 1, 00092 F_color_index, 00093 F_red, 00094 F_green, 00095 F_blue, 00096 F_alpha, 00097 F_rgb, // any suitable RGB mode, whatever the hardware prefers 00098 00099 // The following request a particular number of bits for the GSG's 00100 // internal_format (as stored in the framebuffer), but this 00101 // request is not related to the pixel storage within the Texture 00102 // object itself, which is always get_num_components() * 00103 // get_component_width(). 00104 F_rgb5, // 5 bits per R,G,B channel 00105 F_rgb8, // 8 bits per R,G,B channel 00106 F_rgb12, // 12 bits per R,G,B channel 00107 F_rgb332, // 3 bits per R & G, 2 bits for B 00108 00109 F_rgba, // any suitable RGBA mode, whatever the hardware prefers 00110 00111 // Again, the following bitdepth requests are only for the GSG; 00112 // within the Texture object itself, these are all equivalent. 00113 F_rgbm, // as above, but only requires 1 bit for alpha (i.e. mask) 00114 F_rgba4, // 4 bits per R,G,B,A channel 00115 F_rgba5, // 5 bits per R,G,B channel, 1 bit alpha 00116 F_rgba8, // 8 bits per R,G,B,A channel 00117 F_rgba12, // 12 bits per R,G,B,A channel 00118 00119 F_luminance, 00120 F_luminance_alpha, // 8 bits luminance, 8 bits alpha 00121 F_luminance_alphamask, // 8 bits luminance, only needs 1 bit of alpha 00122 00123 F_rgba16, // 16 bits per R,G,B,A channel 00124 F_rgba32, // 32 bits per R,G,B,A channel 00125 00126 F_depth_component, 00127 F_depth_component16, 00128 F_depth_component24, 00129 F_depth_component32, 00130 }; 00131 00132 enum FilterType { 00133 // Mag Filter and Min Filter 00134 00135 // Point sample the pixel 00136 FT_nearest, 00137 00138 // Bilinear filtering of four neighboring pixels 00139 FT_linear, 00140 00141 // Min Filter Only 00142 00143 // Point sample the pixel from the nearest mipmap level 00144 FT_nearest_mipmap_nearest, 00145 00146 // Bilinear filter the pixel from the nearest mipmap level 00147 FT_linear_mipmap_nearest, 00148 00149 // Point sample the pixel from two mipmap levels, and linearly blend 00150 FT_nearest_mipmap_linear, 00151 00152 // A.k.a. trilinear filtering: Bilinear filter the pixel from 00153 // two mipmap levels, and linearly blend the results. 00154 FT_linear_mipmap_linear, 00155 00156 // The OpenGL ARB_shadow extension can be thought of as a kind of filtering. 00157 FT_shadow, 00158 00159 // Default is usually linear, but it depends on format. 00160 // This was added at the end of the list to avoid bumping TXO version #. 00161 FT_default, 00162 00163 // Returned by string_filter_type() for an invalid match. 00164 FT_invalid 00165 }; 00166 00167 enum WrapMode { 00168 WM_clamp, // coords that would be outside [0-1] are clamped to 0 or 1 00169 WM_repeat, 00170 WM_mirror, 00171 WM_mirror_once, // mirror once, then clamp 00172 WM_border_color, // coords outside [0-1] use explict border color 00173 // Returned by string_wrap_mode() for an invalid match. 00174 WM_invalid 00175 }; 00176 00177 enum CompressionMode { 00178 // Generic compression modes. Usually, you should choose one of 00179 // these. 00180 CM_default, // on or off, according to compressed-textures 00181 CM_off, // uncompressed image 00182 CM_on, // whatever compression the driver supports 00183 00184 // Specific compression modes. Use only when you really want to 00185 // use a particular compression algorithm. Use with caution; not 00186 // all drivers support all compression modes. You can use 00187 // GSG::get_supports_compressed_texture_format() to query the 00188 // available compression modes for a particular GSG. 00189 CM_fxt1, 00190 CM_dxt1, 00191 CM_dxt2, 00192 CM_dxt3, 00193 CM_dxt4, 00194 CM_dxt5, 00195 CM_pvr1_2bpp, 00196 CM_pvr1_4bpp, 00197 }; 00198 00199 enum QualityLevel { 00200 QL_default, // according to texture-quality-level 00201 QL_fastest, 00202 QL_normal, 00203 QL_best, 00204 }; 00205 00206 PUBLISHED: 00207 Texture(const string &name = string()); 00208 protected: 00209 Texture(const Texture ©); 00210 void operator = (const Texture ©); 00211 PUBLISHED: 00212 virtual ~Texture(); 00213 00214 INLINE PT(Texture) make_copy() const; 00215 INLINE void clear(); 00216 00217 INLINE void setup_texture(TextureType texture_type, 00218 int x_size, int y_size, int z_size, 00219 ComponentType component_type, Format format); 00220 INLINE void setup_1d_texture(); 00221 INLINE void setup_1d_texture(int x_size, 00222 ComponentType component_type, Format format); 00223 INLINE void setup_2d_texture(); 00224 INLINE void setup_2d_texture(int x_size, int y_size, 00225 ComponentType component_type, Format format); 00226 INLINE void setup_3d_texture(int z_size = 1); 00227 INLINE void setup_3d_texture(int x_size, int y_size, int z_size, 00228 ComponentType component_type, Format format); 00229 INLINE void setup_cube_map(); 00230 INLINE void setup_cube_map(int size, 00231 ComponentType component_type, Format format); 00232 INLINE void setup_2d_texture_array(int z_size = 1); 00233 INLINE void setup_2d_texture_array(int x_size, int y_size, int z_size, 00234 ComponentType component_type, Format format); 00235 void generate_normalization_cube_map(int size); 00236 void generate_alpha_scale_map(); 00237 00238 BLOCKING bool read(const Filename &fullpath, const LoaderOptions &options = LoaderOptions()); 00239 BLOCKING bool read(const Filename &fullpath, const Filename &alpha_fullpath, 00240 int primary_file_num_channels, int alpha_file_channel, 00241 const LoaderOptions &options = LoaderOptions()); 00242 BLOCKING bool read(const Filename &fullpath, int z, int n, 00243 bool read_pages, bool read_mipmaps, 00244 const LoaderOptions &options = LoaderOptions()); 00245 BLOCKING bool read(const Filename &fullpath, const Filename &alpha_fullpath, 00246 int primary_file_num_channels, int alpha_file_channel, 00247 int z, int n, bool read_pages, bool read_mipmaps, 00248 BamCacheRecord *record = NULL, 00249 const LoaderOptions &options = LoaderOptions()); 00250 00251 BLOCKING INLINE bool write(const Filename &fullpath); 00252 BLOCKING INLINE bool write(const Filename &fullpath, int z, int n, 00253 bool write_pages, bool write_mipmaps); 00254 00255 BLOCKING bool read_txo(istream &in, const string &filename = ""); 00256 BLOCKING static PT(Texture) make_from_txo(istream &in, const string &filename = ""); 00257 BLOCKING bool write_txo(ostream &out, const string &filename = "") const; 00258 BLOCKING bool read_dds(istream &in, const string &filename = "", bool header_only = false); 00259 00260 BLOCKING INLINE bool load(const PNMImage &pnmimage, const LoaderOptions &options = LoaderOptions()); 00261 BLOCKING INLINE bool load(const PNMImage &pnmimage, int z, int n, const LoaderOptions &options = LoaderOptions()); 00262 BLOCKING INLINE bool store(PNMImage &pnmimage) const; 00263 BLOCKING INLINE bool store(PNMImage &pnmimage, int z, int n) const; 00264 00265 BLOCKING INLINE bool reload(); 00266 BLOCKING Texture *load_related(const InternalName *suffix) const; 00267 00268 INLINE bool has_filename() const; 00269 INLINE const Filename &get_filename() const; 00270 INLINE bool has_alpha_filename() const; 00271 INLINE const Filename &get_alpha_filename() const; 00272 00273 INLINE bool has_fullpath() const; 00274 INLINE const Filename &get_fullpath() const; 00275 INLINE bool has_alpha_fullpath() const; 00276 INLINE const Filename &get_alpha_fullpath() const; 00277 00278 INLINE int get_x_size() const; 00279 INLINE int get_y_size() const; 00280 INLINE int get_z_size() const; 00281 INLINE int get_num_views() const; 00282 INLINE int get_num_pages() const; 00283 INLINE int get_num_components() const; 00284 INLINE int get_component_width() const; 00285 INLINE TextureType get_texture_type() const; 00286 INLINE Format get_format() const; 00287 INLINE ComponentType get_component_type() const; 00288 00289 INLINE void set_wrap_u(WrapMode wrap); 00290 INLINE void set_wrap_v(WrapMode wrap); 00291 INLINE void set_wrap_w(WrapMode wrap); 00292 INLINE void set_minfilter(FilterType filter); 00293 INLINE void set_magfilter(FilterType filter); 00294 INLINE void set_anisotropic_degree(int anisotropic_degree); 00295 INLINE void set_border_color(const LColor &color); 00296 INLINE void set_compression(CompressionMode compression); 00297 INLINE void set_render_to_texture(bool render_to_texture); 00298 00299 INLINE WrapMode get_wrap_u() const; 00300 INLINE WrapMode get_wrap_v() const; 00301 INLINE WrapMode get_wrap_w() const; 00302 INLINE FilterType get_minfilter() const; 00303 INLINE FilterType get_magfilter() const; 00304 FilterType get_effective_minfilter() const; 00305 FilterType get_effective_magfilter() const; 00306 INLINE int get_anisotropic_degree() const; 00307 INLINE int get_effective_anisotropic_degree() const; 00308 INLINE LColor get_border_color() const; 00309 INLINE CompressionMode get_compression() const; 00310 INLINE bool has_compression() const; 00311 INLINE bool get_render_to_texture() const; 00312 INLINE bool uses_mipmaps() const; 00313 00314 INLINE void set_quality_level(QualityLevel quality_level); 00315 INLINE QualityLevel get_quality_level() const; 00316 INLINE QualityLevel get_effective_quality_level() const; 00317 00318 INLINE int get_expected_num_mipmap_levels() const; 00319 INLINE int get_expected_mipmap_x_size(int n) const; 00320 INLINE int get_expected_mipmap_y_size(int n) const; 00321 INLINE int get_expected_mipmap_z_size(int n) const; 00322 INLINE int get_expected_mipmap_num_pages(int n) const; 00323 00324 INLINE bool has_ram_image() const; 00325 INLINE bool has_uncompressed_ram_image() const; 00326 INLINE bool might_have_ram_image() const; 00327 INLINE size_t get_ram_image_size() const; 00328 INLINE size_t get_ram_view_size() const; 00329 INLINE size_t get_ram_page_size() const; 00330 INLINE size_t get_expected_ram_image_size() const; 00331 INLINE size_t get_expected_ram_page_size() const; 00332 INLINE CPTA_uchar get_ram_image(); 00333 INLINE CompressionMode get_ram_image_compression() const; 00334 INLINE CPTA_uchar get_uncompressed_ram_image(); 00335 CPTA_uchar get_ram_image_as(const string &requested_format); 00336 INLINE PTA_uchar modify_ram_image(); 00337 INLINE PTA_uchar make_ram_image(); 00338 INLINE void set_ram_image(CPTA_uchar image, CompressionMode compression = CM_off, 00339 size_t page_size = 0); 00340 void set_ram_image_as(CPTA_uchar image, const string &provided_format); 00341 INLINE void clear_ram_image(); 00342 INLINE void set_keep_ram_image(bool keep_ram_image); 00343 virtual bool get_keep_ram_image() const; 00344 virtual bool is_cacheable() const; 00345 00346 INLINE bool compress_ram_image(CompressionMode compression = CM_on, 00347 QualityLevel quality_level = QL_default, 00348 GraphicsStateGuardianBase *gsg = NULL); 00349 INLINE bool uncompress_ram_image(); 00350 00351 INLINE int get_num_ram_mipmap_images() const; 00352 INLINE bool has_ram_mipmap_image(int n) const; 00353 int get_num_loadable_ram_mipmap_images() const; 00354 INLINE bool has_all_ram_mipmap_images() const; 00355 INLINE size_t get_ram_mipmap_image_size(int n) const; 00356 INLINE size_t get_ram_mipmap_view_size(int n) const; 00357 INLINE size_t get_ram_mipmap_page_size(int n) const; 00358 INLINE size_t get_expected_ram_mipmap_image_size(int n) const; 00359 INLINE size_t get_expected_ram_mipmap_view_size(int n) const; 00360 INLINE size_t get_expected_ram_mipmap_page_size(int n) const; 00361 CPTA_uchar get_ram_mipmap_image(int n) const; 00362 void *get_ram_mipmap_pointer(int n) const; 00363 INLINE PTA_uchar modify_ram_mipmap_image(int n); 00364 INLINE PTA_uchar make_ram_mipmap_image(int n); 00365 void set_ram_mipmap_pointer(int n, void *image, size_t page_size = 0); 00366 void set_ram_mipmap_pointer_from_int(long long pointer, int n, int page_size); 00367 INLINE void set_ram_mipmap_image(int n, CPTA_uchar image, size_t page_size = 0); 00368 void clear_ram_mipmap_image(int n); 00369 INLINE void clear_ram_mipmap_images(); 00370 INLINE void generate_ram_mipmap_images(); 00371 00372 INLINE int get_simple_x_size() const; 00373 INLINE int get_simple_y_size() const; 00374 INLINE bool has_simple_ram_image() const; 00375 INLINE size_t get_simple_ram_image_size() const; 00376 INLINE CPTA_uchar get_simple_ram_image() const; 00377 INLINE void set_simple_ram_image(CPTA_uchar image, int x_size, int y_size); 00378 PTA_uchar modify_simple_ram_image(); 00379 PTA_uchar new_simple_ram_image(int x_size, int y_size); 00380 void generate_simple_ram_image(); 00381 INLINE void clear_simple_ram_image(); 00382 00383 PT(TexturePeeker) peek(); 00384 00385 INLINE UpdateSeq get_properties_modified() const; 00386 INLINE UpdateSeq get_image_modified() const; 00387 INLINE UpdateSeq get_simple_image_modified() const; 00388 00389 INLINE void set_auto_texture_scale(AutoTextureScale scale); 00390 INLINE AutoTextureScale get_auto_texture_scale() const; 00391 INLINE bool has_auto_texture_scale() const; 00392 00393 void prepare(PreparedGraphicsObjects *prepared_objects); 00394 bool is_prepared(PreparedGraphicsObjects *prepared_objects) const; 00395 bool was_image_modified(PreparedGraphicsObjects *prepared_objects) const; 00396 size_t get_data_size_bytes(PreparedGraphicsObjects *prepared_objects) const; 00397 bool get_active(PreparedGraphicsObjects *prepared_objects) const; 00398 bool get_resident(PreparedGraphicsObjects *prepared_objects) const; 00399 00400 bool release(PreparedGraphicsObjects *prepared_objects); 00401 int release_all(); 00402 00403 void write(ostream &out, int indent_level) const; 00404 00405 size_t estimate_texture_memory() const; 00406 00407 void set_aux_data(const string &key, TypedReferenceCount *aux_data); 00408 void clear_aux_data(const string &key); 00409 TypedReferenceCount *get_aux_data(const string &key) const; 00410 00411 INLINE static void set_textures_power_2(AutoTextureScale scale); 00412 INLINE static AutoTextureScale get_textures_power_2(); 00413 INLINE static bool has_textures_power_2(); 00414 00415 PUBLISHED: 00416 // These are published, but in general, you shouldn't be mucking 00417 // with these values; they are set automatically when a texture is 00418 // loaded. 00419 INLINE void set_filename(const Filename &filename); 00420 INLINE void clear_filename(); 00421 INLINE void set_alpha_filename(const Filename &alpha_filename); 00422 INLINE void clear_alpha_filename(); 00423 00424 INLINE void set_fullpath(const Filename &fullpath); 00425 INLINE void clear_fullpath(); 00426 INLINE void set_alpha_fullpath(const Filename &alpha_fullpath); 00427 INLINE void clear_alpha_fullpath(); 00428 00429 INLINE void set_x_size(int x_size); 00430 INLINE void set_y_size(int y_size); 00431 INLINE void set_z_size(int z_size); 00432 INLINE void set_num_views(int num_views); 00433 00434 INLINE int get_pad_x_size() const; 00435 INLINE int get_pad_y_size() const; 00436 INLINE int get_pad_z_size() const; 00437 INLINE LVecBase2 get_tex_scale() const; 00438 00439 INLINE void set_pad_size(int x=0, int y=0, int z=0); 00440 void set_size_padded(int x=1, int y=1, int z=1); 00441 00442 INLINE int get_orig_file_x_size() const; 00443 INLINE int get_orig_file_y_size() const; 00444 INLINE int get_orig_file_z_size() const; 00445 00446 void set_orig_file_size(int x, int y, int z = 1); 00447 00448 INLINE void set_format(Format format); 00449 INLINE void set_component_type(ComponentType component_type); 00450 INLINE void set_loaded_from_image(); 00451 INLINE bool get_loaded_from_image() const; 00452 00453 INLINE void set_loaded_from_txo(); 00454 INLINE bool get_loaded_from_txo() const; 00455 00456 static bool is_mipmap(FilterType type); 00457 00458 INLINE bool get_match_framebuffer_format() const; 00459 INLINE void set_match_framebuffer_format(bool flag); 00460 00461 INLINE bool get_post_load_store_cache() const; 00462 INLINE void set_post_load_store_cache(bool flag); 00463 00464 TextureContext *prepare_now(int view, 00465 PreparedGraphicsObjects *prepared_objects, 00466 GraphicsStateGuardianBase *gsg); 00467 00468 static int up_to_power_2(int value); 00469 static int down_to_power_2(int value); 00470 00471 void consider_rescale(PNMImage &pnmimage); 00472 static void consider_rescale(PNMImage &pnmimage, const string &name, AutoTextureScale auto_texture_scale = ATS_unspecified); 00473 INLINE bool rescale_texture(); 00474 00475 static string format_texture_type(TextureType tt); 00476 static TextureType string_texture_type(const string &str); 00477 00478 static string format_component_type(ComponentType ct); 00479 static ComponentType string_component_type(const string &str); 00480 00481 static string format_format(Format f); 00482 static Format string_format(const string &str); 00483 00484 static string format_filter_type(FilterType ft); 00485 static FilterType string_filter_type(const string &str); 00486 00487 static string format_wrap_mode(WrapMode wm); 00488 static WrapMode string_wrap_mode(const string &str); 00489 00490 static string format_compression_mode(CompressionMode cm); 00491 static CompressionMode string_compression_mode(const string &str); 00492 00493 static string format_quality_level(QualityLevel tql); 00494 static QualityLevel string_quality_level(const string &str); 00495 00496 public: 00497 void texture_uploaded(); 00498 00499 virtual bool has_cull_callback() const; 00500 virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const; 00501 00502 static PT(Texture) make_texture(); 00503 00504 public: 00505 static bool is_specific(CompressionMode compression); 00506 static bool has_alpha(Format format); 00507 static bool has_binary_alpha(Format format); 00508 00509 static bool adjust_size(int &x_size, int &y_size, const string &name, 00510 bool for_padding, AutoTextureScale auto_texture_scale = ATS_unspecified); 00511 INLINE bool adjust_this_size(int &x_size, int &y_size, const string &name, 00512 bool for_padding) const; 00513 00514 protected: 00515 class CData; 00516 00517 virtual void reconsider_dirty(); 00518 00519 // All of the functions in this class that begin "do_" are protected 00520 // methods. Many of them are implementations of public-facing 00521 // versions of the same methods. 00522 00523 // All of these assume the CData lock is already held (and receive a 00524 // CData pointer representing that lock); generally, they also avoid 00525 // adjusting the _properties_modified and _image_modified 00526 // semaphores. 00527 virtual bool do_adjust_this_size(const CData *cdata, 00528 int &x_size, int &y_size, const string &name, 00529 bool for_padding) const; 00530 00531 virtual bool do_read(CData *cdata, 00532 const Filename &fullpath, const Filename &alpha_fullpath, 00533 int primary_file_num_channels, int alpha_file_channel, 00534 int z, int n, bool read_pages, bool read_mipmaps, 00535 const LoaderOptions &options, BamCacheRecord *record); 00536 virtual bool do_read_one(CData *cdata, 00537 const Filename &fullpath, const Filename &alpha_fullpath, 00538 int z, int n, int primary_file_num_channels, int alpha_file_channel, 00539 const LoaderOptions &options, 00540 bool header_only, BamCacheRecord *record); 00541 virtual bool do_load_one(CData *cdata, 00542 const PNMImage &pnmimage, const string &name, 00543 int z, int n, const LoaderOptions &options); 00544 bool do_read_txo_file(CData *cdata, const Filename &fullpath); 00545 bool do_read_txo(CData *cdata, istream &in, const string &filename); 00546 bool do_read_dds_file(CData *cdata, const Filename &fullpath, bool header_only); 00547 bool do_read_dds(CData *cdata, istream &in, const string &filename, bool header_only); 00548 00549 bool do_write(CData *cdata, const Filename &fullpath, int z, int n, 00550 bool write_pages, bool write_mipmaps); 00551 bool do_write_one(CData *cdata, const Filename &fullpath, int z, int n); 00552 bool do_store_one(CData *cdata, PNMImage &pnmimage, int z, int n); 00553 bool do_write_txo_file(const CData *cdata, const Filename &fullpath) const; 00554 bool do_write_txo(const CData *cdata, ostream &out, const string &filename) const; 00555 00556 virtual CData *unlocked_ensure_ram_image(bool allow_compression); 00557 virtual void do_reload_ram_image(CData *cdata, bool allow_compression); 00558 00559 PTA_uchar do_modify_ram_image(CData *cdata); 00560 PTA_uchar do_make_ram_image(CData *cdata); 00561 void do_set_ram_image(CData *cdata, CPTA_uchar image, 00562 CompressionMode compression = CM_off, size_t page_size = 0); 00563 PTA_uchar do_modify_ram_mipmap_image(CData *cdata, int n); 00564 PTA_uchar do_make_ram_mipmap_image(CData *cdata, int n); 00565 void do_set_ram_mipmap_image(CData *cdata, int n, CPTA_uchar image, size_t page_size); 00566 00567 bool consider_auto_process_ram_image(bool generate_mipmaps, bool allow_compression); 00568 bool do_consider_auto_process_ram_image(CData *cdata, bool generate_mipmaps, 00569 bool allow_compression); 00570 bool do_compress_ram_image(CData *cdata, CompressionMode compression, 00571 QualityLevel quality_level, 00572 GraphicsStateGuardianBase *gsg); 00573 bool do_uncompress_ram_image(CData *cdata); 00574 bool do_has_all_ram_mipmap_images(const CData *cdata) const; 00575 00576 bool do_reconsider_z_size(CData *cdata, int z, const LoaderOptions &options); 00577 virtual void do_allocate_pages(CData *cdata); 00578 bool do_reconsider_image_properties(CData *cdata, 00579 int x_size, int y_size, int num_components, 00580 ComponentType component_type, int z, 00581 const LoaderOptions &options); 00582 bool do_rescale_texture(CData *cdata); 00583 00584 virtual PT(Texture) make_copy_impl() const; 00585 PT(Texture) do_make_copy(const CData *cdata) const; 00586 void do_assign(CData *cdata, const Texture *copy, const CData *cdata_copy); 00587 virtual void do_clear(CData *cdata); 00588 void do_setup_texture(CData *cdata, 00589 TextureType texture_type, int x_size, int y_size, 00590 int z_size, ComponentType component_type, 00591 Format format); 00592 void do_set_num_views(CData *cdata, int num_views); 00593 void do_set_format(CData *cdata, Format format); 00594 void do_set_component_type(CData *cdata, ComponentType component_type); 00595 void do_set_x_size(CData *cdata, int x_size); 00596 void do_set_y_size(CData *cdata, int y_size); 00597 void do_set_z_size(CData *cdata, int z_size); 00598 00599 void do_set_wrap_u(CData *cdata, WrapMode wrap); 00600 void do_set_wrap_v(CData *cdata, WrapMode wrap); 00601 void do_set_wrap_w(CData *cdata, WrapMode wrap); 00602 void do_set_minfilter(CData *cdata, FilterType filter); 00603 void do_set_magfilter(CData *cdata, FilterType filter); 00604 void do_set_anisotropic_degree(CData *cdata, int anisotropic_degree); 00605 void do_set_border_color(CData *cdata, const LColor &color); 00606 void do_set_compression(CData *cdata, CompressionMode compression); 00607 void do_set_quality_level(CData *cdata, QualityLevel quality_level); 00608 00609 bool do_has_compression(const CData *cdata) const; 00610 virtual bool do_has_ram_image(const CData *cdata) const; 00611 virtual bool do_has_uncompressed_ram_image(const CData *cdata) const; 00612 CPTA_uchar do_get_ram_image(CData *cdata); 00613 CPTA_uchar do_get_uncompressed_ram_image(CData *cdata); 00614 void do_set_simple_ram_image(CData *cdata, CPTA_uchar image, int x_size, int y_size); 00615 INLINE size_t do_get_ram_image_size(const CData *cdata) const; 00616 INLINE bool do_has_ram_mipmap_image(const CData *cdata, int n) const; 00617 int do_get_expected_num_mipmap_levels(const CData *cdata) const; 00618 INLINE size_t do_get_expected_ram_image_size(const CData *cdata) const; 00619 INLINE size_t do_get_expected_ram_view_size(const CData *cdata) const; 00620 INLINE size_t do_get_expected_ram_page_size(const CData *cdata) const; 00621 size_t do_get_ram_mipmap_page_size(const CData *cdata, int n) const; 00622 INLINE size_t do_get_expected_ram_mipmap_image_size(const CData *cdata, int n) const; 00623 INLINE size_t do_get_expected_ram_mipmap_view_size(const CData *cdata, int n) const; 00624 INLINE size_t do_get_expected_ram_mipmap_page_size(const CData *cdata, int n) const; 00625 int do_get_expected_mipmap_x_size(const CData *cdata, int n) const; 00626 int do_get_expected_mipmap_y_size(const CData *cdata, int n) const; 00627 int do_get_expected_mipmap_z_size(const CData *cdata, int n) const; 00628 INLINE int do_get_expected_mipmap_num_pages(const CData *cdata, int n) const; 00629 INLINE void do_clear_ram_image(CData *cdata); 00630 void do_clear_simple_ram_image(CData *cdata); 00631 void do_clear_ram_mipmap_images(CData *cdata); 00632 void do_generate_ram_mipmap_images(CData *cdata); 00633 void do_set_pad_size(CData *cdata, int x, int y, int z); 00634 virtual bool do_can_reload(const CData *cdata) const; 00635 bool do_reload(CData *cdata); 00636 00637 INLINE AutoTextureScale do_get_auto_texture_scale(const CData *cdata) const; 00638 00639 virtual bool do_has_bam_rawdata(const CData *cdata) const; 00640 virtual void do_get_bam_rawdata(CData *cdata); 00641 00642 // This nested class declaration is used below. 00643 class RamImage { 00644 public: 00645 INLINE RamImage(); 00646 00647 PTA_uchar _image; 00648 size_t _page_size; 00649 00650 // If _pointer_image is non-NULL, it represents an external block 00651 // of memory that is used instead of the above PTA_uchar. 00652 void *_pointer_image; 00653 }; 00654 00655 private: 00656 static void convert_from_pnmimage(PTA_uchar &image, size_t page_size, 00657 int z, const PNMImage &pnmimage, 00658 int num_components, int component_width); 00659 static bool convert_to_pnmimage(PNMImage &pnmimage, int x_size, int y_size, 00660 int num_components, int component_width, 00661 CPTA_uchar image, size_t page_size, 00662 int z); 00663 static PTA_uchar read_dds_level_bgr8(Texture *tex, CData *cdata, const DDSHeader &header, 00664 int n, istream &in); 00665 static PTA_uchar read_dds_level_rgb8(Texture *tex, CData *cdata, const DDSHeader &header, 00666 int n, istream &in); 00667 static PTA_uchar read_dds_level_abgr8(Texture *tex, CData *cdata, const DDSHeader &header, 00668 int n, istream &in); 00669 static PTA_uchar read_dds_level_rgba8(Texture *tex, CData *cdata, const DDSHeader &header, 00670 int n, istream &in); 00671 static PTA_uchar read_dds_level_generic_uncompressed(Texture *tex, CData *cdata, 00672 const DDSHeader &header, 00673 int n, istream &in); 00674 static PTA_uchar read_dds_level_luminance_uncompressed(Texture *tex, CData *cdata, 00675 const DDSHeader &header, 00676 int n, istream &in); 00677 static PTA_uchar read_dds_level_dxt1(Texture *tex, CData *cdata, 00678 const DDSHeader &header, 00679 int n, istream &in); 00680 static PTA_uchar read_dds_level_dxt23(Texture *tex, CData *cdata, 00681 const DDSHeader &header, 00682 int n, istream &in); 00683 static PTA_uchar read_dds_level_dxt45(Texture *tex, CData *cdata, 00684 const DDSHeader &header, 00685 int n, istream &in); 00686 00687 void clear_prepared(int view, PreparedGraphicsObjects *prepared_objects); 00688 00689 static void consider_downgrade(PNMImage &pnmimage, int num_channels, const string &name); 00690 00691 static bool compare_images(const PNMImage &a, const PNMImage &b); 00692 00693 INLINE static void store_unscaled_byte(unsigned char *&p, int value); 00694 INLINE static void store_unscaled_short(unsigned char *&p, int value); 00695 INLINE static void store_scaled_byte(unsigned char *&p, int value, double scale); 00696 INLINE static void store_scaled_short(unsigned char *&p, int value, double scale); 00697 INLINE static double get_unsigned_byte(const unsigned char *&p); 00698 INLINE static double get_unsigned_short(const unsigned char *&p); 00699 00700 INLINE static bool is_txo_filename(const Filename &fullpath); 00701 INLINE static bool is_dds_filename(const Filename &fullpath); 00702 00703 void do_filter_2d_mipmap_pages(const CData *cdata, 00704 RamImage &to, const RamImage &from, 00705 int x_size, int y_size) const; 00706 00707 void do_filter_3d_mipmap_level(const CData *cdata, 00708 RamImage &to, const RamImage &from, 00709 int x_size, int y_size, int z_size) const; 00710 00711 typedef void Filter2DComponent(unsigned char *&p, 00712 const unsigned char *&q, 00713 size_t pixel_size, size_t row_size); 00714 00715 typedef void Filter3DComponent(unsigned char *&p, 00716 const unsigned char *&q, 00717 size_t pixel_size, size_t row_size, 00718 size_t page_size); 00719 00720 static void filter_2d_unsigned_byte(unsigned char *&p, 00721 const unsigned char *&q, 00722 size_t pixel_size, size_t row_size); 00723 static void filter_2d_unsigned_short(unsigned char *&p, 00724 const unsigned char *&q, 00725 size_t pixel_size, size_t row_size); 00726 00727 static void filter_3d_unsigned_byte(unsigned char *&p, 00728 const unsigned char *&q, 00729 size_t pixel_size, size_t row_size, 00730 size_t page_size); 00731 static void filter_3d_unsigned_short(unsigned char *&p, 00732 const unsigned char *&q, 00733 size_t pixel_size, size_t row_size, 00734 size_t page_size); 00735 00736 bool do_squish(CData *cdata, CompressionMode compression, int squish_flags); 00737 bool do_unsquish(CData *cdata, int squish_flags); 00738 00739 protected: 00740 typedef pvector<RamImage> RamImages; 00741 00742 // This is the data that must be cycled between pipeline stages. 00743 class EXPCL_PANDA_GOBJ CData : public CycleData { 00744 public: 00745 CData(); 00746 CData(const CData ©); 00747 ALLOC_DELETED_CHAIN(CData); 00748 virtual CycleData *make_copy() const; 00749 virtual void write_datagram(BamWriter *manager, Datagram &dg) const; 00750 virtual int complete_pointers(TypedWritable **plist, BamReader *manager); 00751 virtual void fillin(DatagramIterator &scan, BamReader *manager); 00752 virtual TypeHandle get_parent_type() const { 00753 return Texture::get_class_type(); 00754 } 00755 00756 void do_assign(const CData *copy); 00757 00758 Filename _filename; 00759 Filename _alpha_filename; 00760 Filename _fullpath; 00761 Filename _alpha_fullpath; 00762 00763 // The number of channels of the primary file we use. 1, 2, 3, or 4. 00764 int _primary_file_num_channels; 00765 00766 // If we have a separate alpha file, this designates which channel 00767 // in the alpha file provides the alpha channel. 0 indicates the 00768 // combined grayscale value of rgb; otherwise, 1, 2, 3, or 4 are 00769 // valid. 00770 int _alpha_file_channel; 00771 00772 int _x_size; 00773 int _y_size; 00774 int _z_size; 00775 int _num_views; 00776 int _num_components; 00777 int _component_width; 00778 TextureType _texture_type; 00779 Format _format; 00780 ComponentType _component_type; 00781 00782 bool _loaded_from_image; 00783 bool _loaded_from_txo; 00784 bool _has_read_pages; 00785 bool _has_read_mipmaps; 00786 int _num_mipmap_levels_read; 00787 00788 WrapMode _wrap_u; 00789 WrapMode _wrap_v; 00790 WrapMode _wrap_w; 00791 FilterType _minfilter; 00792 FilterType _magfilter; 00793 int _anisotropic_degree; 00794 bool _keep_ram_image; 00795 LColor _border_color; 00796 CompressionMode _compression; 00797 bool _render_to_texture; 00798 bool _match_framebuffer_format; 00799 bool _post_load_store_cache; 00800 QualityLevel _quality_level; 00801 00802 int _pad_x_size; 00803 int _pad_y_size; 00804 int _pad_z_size; 00805 00806 int _orig_file_x_size; 00807 int _orig_file_y_size; 00808 00809 AutoTextureScale _auto_texture_scale; 00810 CompressionMode _ram_image_compression; 00811 00812 // There is usually one RamImage for the mipmap level 0 (the base 00813 // image). There may or may not also be additional images for the 00814 // additional mipmap levels. 00815 RamImages _ram_images; 00816 00817 // This is the simple image, which may be loaded before the texture 00818 // is loaded from disk. It exists only for 2-d textures. 00819 RamImage _simple_ram_image; 00820 int _simple_x_size; 00821 int _simple_y_size; 00822 PN_int32 _simple_image_date_generated; 00823 00824 UpdateSeq _properties_modified; 00825 UpdateSeq _image_modified; 00826 UpdateSeq _simple_image_modified; 00827 00828 public: 00829 static TypeHandle get_class_type() { 00830 return _type_handle; 00831 } 00832 static void init_type() { 00833 register_type(_type_handle, "Texture::CData"); 00834 } 00835 00836 private: 00837 static TypeHandle _type_handle; 00838 }; 00839 00840 PipelineCycler<CData> _cycler; 00841 typedef CycleDataLockedReader<CData> CDLockedReader; 00842 typedef CycleDataReader<CData> CDReader; 00843 typedef CycleDataWriter<CData> CDWriter; 00844 typedef CycleDataStageReader<CData> CDStageReader; 00845 typedef CycleDataStageWriter<CData> CDStageWriter; 00846 00847 // Protects the remaining members of this class. 00848 Mutex _lock; 00849 00850 // Used to implement unlocked_reload_ram_image(). 00851 ConditionVarFull _cvar; // condition: _reloading is true. 00852 bool _reloading; 00853 00854 // A Texture keeps a list (actually, a map) of all the 00855 // PreparedGraphicsObjects tables that it has been prepared into. 00856 // Each PGO conversely keeps a list (a set) of all the Textures that 00857 // have been prepared there. When either destructs, it removes 00858 // itself from the other's list. 00859 typedef pmap<int, TextureContext *> Contexts; 00860 typedef pmap<PreparedGraphicsObjects *, Contexts> PreparedViews; 00861 PreparedViews _prepared_views; 00862 00863 // It is common, when using normal maps, specular maps, gloss maps, 00864 // and such, to use a file naming convention where the filenames 00865 // of the special maps are derived by concatenating a suffix to 00866 // the name of the diffuse map. The following table enables 00867 // lookup of the special maps given the diffuse map and the suffix. 00868 typedef pmap<CPT(InternalName), PT(Texture)> RelatedTextures; 00869 RelatedTextures _related_textures; 00870 00871 // The TexturePool finds this useful. 00872 Filename _texture_pool_key; 00873 00874 private: 00875 // The auxiliary data is not recorded to a bam file. 00876 typedef pmap<string, PT(TypedReferenceCount) > AuxData; 00877 AuxData _aux_data; 00878 00879 static AutoTextureScale _textures_power_2; 00880 static PStatCollector _texture_read_pcollector; 00881 00882 // Datagram stuff 00883 public: 00884 static void register_with_read_factory(); 00885 virtual void write_datagram(BamWriter *manager, Datagram &me); 00886 00887 virtual void finalize(BamReader *manager); 00888 00889 protected: 00890 void do_write_datagram_header(CData *cdata, BamWriter *manager, Datagram &me, bool &has_rawdata); 00891 virtual void do_write_datagram_body(CData *cdata, BamWriter *manager, Datagram &me); 00892 virtual void do_write_datagram_rawdata(CData *cdata, BamWriter *manager, Datagram &me); 00893 static TypedWritable *make_from_bam(const FactoryParams ¶ms); 00894 virtual TypedWritable *make_this_from_bam(const FactoryParams ¶ms); 00895 virtual void do_fillin_body(CData *cdata, DatagramIterator &scan, BamReader *manager); 00896 virtual void do_fillin_rawdata(CData *cdata, DatagramIterator &scan, BamReader *manager); 00897 virtual void do_fillin_from(CData *cdata, const Texture *dummy); 00898 00899 public: 00900 static TypeHandle get_class_type() { 00901 return _type_handle; 00902 } 00903 static void init_type() { 00904 TypedWritableReferenceCount::init_type(); 00905 register_type(_type_handle, "Texture", 00906 TypedWritableReferenceCount::get_class_type()); 00907 CData::init_type(); 00908 } 00909 virtual TypeHandle get_type() const { 00910 return get_class_type(); 00911 } 00912 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00913 00914 private: 00915 00916 static TypeHandle _type_handle; 00917 00918 friend class TextureContext; 00919 friend class PreparedGraphicsObjects; 00920 friend class TexturePool; 00921 friend class TexturePeeker; 00922 }; 00923 00924 extern EXPCL_PANDA_GOBJ ConfigVariableEnum<Texture::QualityLevel> texture_quality_level; 00925 extern EXPCL_PANDA_GOBJ ConfigVariableEnum<Texture::FilterType> texture_minfilter; 00926 extern EXPCL_PANDA_GOBJ ConfigVariableEnum<Texture::FilterType> texture_magfilter; 00927 extern EXPCL_PANDA_GOBJ ConfigVariableInt texture_anisotropic_degree; 00928 00929 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::TextureType tt); 00930 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::ComponentType ct); 00931 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::Format f); 00932 00933 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::FilterType ft); 00934 EXPCL_PANDA_GOBJ istream &operator >> (istream &in, Texture::FilterType &ft); 00935 00936 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::WrapMode wm); 00937 EXPCL_PANDA_GOBJ istream &operator >> (istream &in, Texture::WrapMode &wm); 00938 00939 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::CompressionMode cm); 00940 EXPCL_PANDA_GOBJ ostream &operator << (ostream &out, Texture::QualityLevel tql); 00941 EXPCL_PANDA_GOBJ istream &operator >> (istream &in, Texture::QualityLevel &tql); 00942 00943 #include "texture.I" 00944 00945 #endif 00946