compressed_texture.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #pragma once
  31. #include "core/io/resource_loader.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. };
  91. class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {
  92. public:
  93. 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;
  94. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  95. virtual bool handles_type(const String &p_type) const override;
  96. virtual String get_resource_type(const String &p_path) const override;
  97. };
  98. class CompressedTextureLayered : public TextureLayered {
  99. GDCLASS(CompressedTextureLayered, TextureLayered);
  100. public:
  101. enum DataFormat {
  102. DATA_FORMAT_IMAGE,
  103. DATA_FORMAT_PNG,
  104. DATA_FORMAT_WEBP,
  105. DATA_FORMAT_BASIS_UNIVERSAL,
  106. };
  107. enum {
  108. FORMAT_VERSION = 1
  109. };
  110. enum FormatBits {
  111. FORMAT_BIT_STREAM = 1 << 22,
  112. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  113. };
  114. private:
  115. Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
  116. String path_to_file;
  117. mutable RID texture;
  118. Image::Format format = Image::FORMAT_L8;
  119. int w = 0;
  120. int h = 0;
  121. int layers = 0;
  122. bool mipmaps = false;
  123. LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
  124. virtual void reload_from_file() override;
  125. protected:
  126. static void _bind_methods();
  127. void _validate_property(PropertyInfo &p_property) const;
  128. public:
  129. Image::Format get_format() const override;
  130. Error load(const String &p_path);
  131. String get_load_path() const;
  132. virtual LayeredType get_layered_type() const override;
  133. int get_width() const override;
  134. int get_height() const override;
  135. int get_layers() const override;
  136. virtual bool has_mipmaps() const override;
  137. virtual RID get_rid() const override;
  138. virtual void set_path(const String &p_path, bool p_take_over) override;
  139. virtual Ref<Image> get_layer_data(int p_layer) const override;
  140. CompressedTextureLayered(LayeredType p_layered_type);
  141. ~CompressedTextureLayered();
  142. };
  143. class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {
  144. public:
  145. 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;
  146. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  147. virtual bool handles_type(const String &p_type) const override;
  148. virtual String get_resource_type(const String &p_path) const override;
  149. };
  150. class CompressedTexture2DArray : public CompressedTextureLayered {
  151. GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)
  152. public:
  153. CompressedTexture2DArray() :
  154. CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  155. };
  156. class CompressedCubemap : public CompressedTextureLayered {
  157. GDCLASS(CompressedCubemap, CompressedTextureLayered);
  158. public:
  159. CompressedCubemap() :
  160. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  161. };
  162. class CompressedCubemapArray : public CompressedTextureLayered {
  163. GDCLASS(CompressedCubemapArray, CompressedTextureLayered);
  164. public:
  165. CompressedCubemapArray() :
  166. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  167. };
  168. class CompressedTexture3D : public Texture3D {
  169. GDCLASS(CompressedTexture3D, Texture3D);
  170. public:
  171. enum DataFormat {
  172. DATA_FORMAT_IMAGE,
  173. DATA_FORMAT_PNG,
  174. DATA_FORMAT_WEBP,
  175. DATA_FORMAT_BASIS_UNIVERSAL,
  176. };
  177. enum {
  178. FORMAT_VERSION = 1
  179. };
  180. enum FormatBits {
  181. FORMAT_BIT_STREAM = 1 << 22,
  182. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  183. };
  184. private:
  185. 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);
  186. String path_to_file;
  187. mutable RID texture;
  188. Image::Format format = Image::FORMAT_L8;
  189. int w = 0;
  190. int h = 0;
  191. int d = 0;
  192. bool mipmaps = false;
  193. virtual void reload_from_file() override;
  194. protected:
  195. static void _bind_methods();
  196. void _validate_property(PropertyInfo &p_property) const;
  197. public:
  198. Image::Format get_format() const override;
  199. Error load(const String &p_path);
  200. String get_load_path() const;
  201. int get_width() const override;
  202. int get_height() const override;
  203. int get_depth() const override;
  204. virtual bool has_mipmaps() const override;
  205. virtual RID get_rid() const override;
  206. virtual void set_path(const String &p_path, bool p_take_over) override;
  207. virtual Vector<Ref<Image>> get_data() const override;
  208. ~CompressedTexture3D();
  209. };
  210. class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {
  211. public:
  212. 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;
  213. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  214. virtual bool handles_type(const String &p_type) const override;
  215. virtual String get_resource_type(const String &p_path) const override;
  216. };