compressed_texture.h 9.9 KB

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