compressed_texture.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**************************************************************************/
  2. /* 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. #ifndef COMPRESSED_TEXTURE_H
  31. #define COMPRESSED_TEXTURE_H
  32. #include "scene/resources/texture.h"
  33. class BitMap;
  34. class CompressedTexture2D : public Texture2D {
  35. GDCLASS(CompressedTexture2D, Texture2D);
  36. public:
  37. enum DataFormat {
  38. DATA_FORMAT_IMAGE,
  39. DATA_FORMAT_PNG,
  40. DATA_FORMAT_WEBP,
  41. DATA_FORMAT_BASIS_UNIVERSAL,
  42. };
  43. enum {
  44. FORMAT_VERSION = 1
  45. };
  46. enum FormatBits {
  47. FORMAT_BIT_STREAM = 1 << 22,
  48. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  49. FORMAT_BIT_DETECT_3D = 1 << 24,
  50. //FORMAT_BIT_DETECT_SRGB = 1 << 25,
  51. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  52. FORMAT_BIT_DETECT_ROUGNESS = 1 << 27,
  53. };
  54. private:
  55. String path_to_file;
  56. mutable RID texture;
  57. Image::Format format = Image::FORMAT_L8;
  58. int w = 0;
  59. int h = 0;
  60. mutable Ref<BitMap> alpha_cache;
  61. Error _load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit = 0);
  62. virtual void reload_from_file() override;
  63. static void _requested_3d(void *p_ud);
  64. static void _requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  65. static void _requested_normal(void *p_ud);
  66. protected:
  67. static void _bind_methods();
  68. void _validate_property(PropertyInfo &p_property) const;
  69. public:
  70. static Ref<Image> load_image_from_file(Ref<FileAccess> p_file, int p_size_limit);
  71. typedef void (*TextureFormatRequestCallback)(const Ref<CompressedTexture2D> &);
  72. typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<CompressedTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  73. static TextureFormatRequestCallback request_3d_callback;
  74. static TextureFormatRoughnessRequestCallback request_roughness_callback;
  75. static TextureFormatRequestCallback request_normal_callback;
  76. Image::Format get_format() const;
  77. Error load(const String &p_path);
  78. String get_load_path() const;
  79. int get_width() const override;
  80. int get_height() const override;
  81. virtual RID get_rid() const override;
  82. virtual void set_path(const String &p_path, bool p_take_over) override;
  83. 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;
  84. 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;
  85. 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;
  86. virtual bool has_alpha() const override;
  87. bool is_pixel_opaque(int p_x, int p_y) const override;
  88. virtual Ref<Image> get_image() const override;
  89. CompressedTexture2D();
  90. ~CompressedTexture2D();
  91. };
  92. class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {
  93. public:
  94. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  95. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  96. virtual bool handles_type(const String &p_type) const override;
  97. virtual String get_resource_type(const String &p_path) const override;
  98. };
  99. class CompressedTextureLayered : public TextureLayered {
  100. GDCLASS(CompressedTextureLayered, TextureLayered);
  101. public:
  102. enum DataFormat {
  103. DATA_FORMAT_IMAGE,
  104. DATA_FORMAT_PNG,
  105. DATA_FORMAT_WEBP,
  106. DATA_FORMAT_BASIS_UNIVERSAL,
  107. };
  108. enum {
  109. FORMAT_VERSION = 1
  110. };
  111. enum FormatBits {
  112. FORMAT_BIT_STREAM = 1 << 22,
  113. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  114. };
  115. private:
  116. Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
  117. String path_to_file;
  118. mutable RID texture;
  119. Image::Format format = Image::FORMAT_L8;
  120. int w = 0;
  121. int h = 0;
  122. int layers = 0;
  123. bool mipmaps = false;
  124. LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
  125. virtual void reload_from_file() override;
  126. protected:
  127. static void _bind_methods();
  128. void _validate_property(PropertyInfo &p_property) const;
  129. public:
  130. Image::Format get_format() const override;
  131. Error load(const String &p_path);
  132. String get_load_path() const;
  133. virtual LayeredType get_layered_type() const override;
  134. int get_width() const override;
  135. int get_height() const override;
  136. int get_layers() const override;
  137. virtual bool has_mipmaps() const override;
  138. virtual RID get_rid() const override;
  139. virtual void set_path(const String &p_path, bool p_take_over) override;
  140. virtual Ref<Image> get_layer_data(int p_layer) const override;
  141. CompressedTextureLayered(LayeredType p_layered_type);
  142. ~CompressedTextureLayered();
  143. };
  144. class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {
  145. public:
  146. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  147. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  148. virtual bool handles_type(const String &p_type) const override;
  149. virtual String get_resource_type(const String &p_path) const override;
  150. };
  151. class CompressedTexture2DArray : public CompressedTextureLayered {
  152. GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)
  153. public:
  154. CompressedTexture2DArray() :
  155. CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  156. };
  157. class CompressedCubemap : public CompressedTextureLayered {
  158. GDCLASS(CompressedCubemap, CompressedTextureLayered);
  159. public:
  160. CompressedCubemap() :
  161. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  162. };
  163. class CompressedCubemapArray : public CompressedTextureLayered {
  164. GDCLASS(CompressedCubemapArray, CompressedTextureLayered);
  165. public:
  166. CompressedCubemapArray() :
  167. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  168. };
  169. class CompressedTexture3D : public Texture3D {
  170. GDCLASS(CompressedTexture3D, Texture3D);
  171. public:
  172. enum DataFormat {
  173. DATA_FORMAT_IMAGE,
  174. DATA_FORMAT_PNG,
  175. DATA_FORMAT_WEBP,
  176. DATA_FORMAT_BASIS_UNIVERSAL,
  177. };
  178. enum {
  179. FORMAT_VERSION = 1
  180. };
  181. enum FormatBits {
  182. FORMAT_BIT_STREAM = 1 << 22,
  183. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  184. };
  185. private:
  186. Error _load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps);
  187. String path_to_file;
  188. mutable RID texture;
  189. Image::Format format = Image::FORMAT_L8;
  190. int w = 0;
  191. int h = 0;
  192. int d = 0;
  193. bool mipmaps = false;
  194. virtual void reload_from_file() override;
  195. protected:
  196. static void _bind_methods();
  197. void _validate_property(PropertyInfo &p_property) const;
  198. public:
  199. Image::Format get_format() const override;
  200. Error load(const String &p_path);
  201. String get_load_path() const;
  202. int get_width() const override;
  203. int get_height() const override;
  204. int get_depth() const override;
  205. virtual bool has_mipmaps() const override;
  206. virtual RID get_rid() const override;
  207. virtual void set_path(const String &p_path, bool p_take_over) override;
  208. virtual Vector<Ref<Image>> get_data() const override;
  209. CompressedTexture3D();
  210. ~CompressedTexture3D();
  211. };
  212. class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {
  213. public:
  214. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  215. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  216. virtual bool handles_type(const String &p_type) const override;
  217. virtual String get_resource_type(const String &p_path) const override;
  218. };
  219. #endif // COMPRESSED_TEXTURE_H