portable_compressed_texture.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**************************************************************************/
  2. /* portable_compressed_texture.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "scene/resources/texture.h"
  32. class BitMap;
  33. class PortableCompressedTexture2D : public Texture2D {
  34. GDCLASS(PortableCompressedTexture2D, Texture2D);
  35. public:
  36. enum DataFormat {
  37. DATA_FORMAT_UNDEFINED,
  38. DATA_FORMAT_IMAGE,
  39. DATA_FORMAT_PNG,
  40. DATA_FORMAT_WEBP,
  41. DATA_FORMAT_BASIS_UNIVERSAL,
  42. };
  43. enum CompressionMode {
  44. COMPRESSION_MODE_LOSSLESS,
  45. COMPRESSION_MODE_LOSSY,
  46. COMPRESSION_MODE_BASIS_UNIVERSAL,
  47. COMPRESSION_MODE_S3TC,
  48. COMPRESSION_MODE_ETC2,
  49. COMPRESSION_MODE_BPTC,
  50. COMPRESSION_MODE_ASTC,
  51. };
  52. private:
  53. CompressionMode compression_mode = COMPRESSION_MODE_LOSSLESS;
  54. static bool keep_all_compressed_buffers;
  55. bool keep_compressed_buffer = false;
  56. Vector<uint8_t> compressed_buffer;
  57. Size2 size;
  58. Size2 size_override;
  59. bool mipmaps = false;
  60. Image::Format format = Image::FORMAT_L8;
  61. mutable RID texture;
  62. mutable Ref<BitMap> alpha_cache;
  63. bool image_stored = false;
  64. Image::BasisUniversalPackerParams basisu_params;
  65. protected:
  66. Vector<uint8_t> _get_data() const;
  67. void _set_data(const Vector<uint8_t> &p_data);
  68. static void _bind_methods();
  69. public:
  70. CompressionMode get_compression_mode() const;
  71. void create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map = false, float p_lossy_quality = 0.8);
  72. Image::Format get_format() const;
  73. void update(const Ref<Image> &p_image);
  74. Ref<Image> get_image() const override;
  75. int get_width() const override;
  76. int get_height() const override;
  77. virtual RID get_rid() const override;
  78. bool has_alpha() const override;
  79. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  80. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  81. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;
  82. bool is_pixel_opaque(int p_x, int p_y) const override;
  83. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  84. void set_size_override(const Size2 &p_size);
  85. Size2 get_size_override() const;
  86. void set_keep_compressed_buffer(bool p_keep);
  87. bool is_keeping_compressed_buffer() const;
  88. void set_basisu_compressor_params(int p_uastc_level, float p_rdo_quality_loss);
  89. static void set_keep_all_compressed_buffers(bool p_keep);
  90. static bool is_keeping_all_compressed_buffers();
  91. ~PortableCompressedTexture2D();
  92. };
  93. VARIANT_ENUM_CAST(PortableCompressedTexture2D::CompressionMode)