compressed_texture.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. public:
  69. static Ref<Image> load_image_from_file(Ref<FileAccess> p_file, int p_size_limit);
  70. typedef void (*TextureFormatRequestCallback)(const Ref<CompressedTexture2D> &);
  71. typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<CompressedTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  72. static TextureFormatRequestCallback request_3d_callback;
  73. static TextureFormatRoughnessRequestCallback request_roughness_callback;
  74. static TextureFormatRequestCallback request_normal_callback;
  75. Image::Format get_format() const;
  76. Error load(const String &p_path);
  77. String get_load_path() const;
  78. int get_width() const override;
  79. int get_height() const override;
  80. virtual RID get_rid() const override;
  81. virtual void set_path(const String &p_path, bool p_take_over) override;
  82. 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;
  83. 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;
  84. 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;
  85. virtual bool has_alpha() const override;
  86. bool is_pixel_opaque(int p_x, int p_y) const override;
  87. virtual Ref<Image> get_image() const override;
  88. ~CompressedTexture2D();
  89. };
  90. class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {
  91. public:
  92. 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;
  93. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  94. virtual bool handles_type(const String &p_type) const override;
  95. virtual String get_resource_type(const String &p_path) const override;
  96. };
  97. class CompressedTextureLayered : public TextureLayered {
  98. GDCLASS(CompressedTextureLayered, TextureLayered);
  99. public:
  100. enum DataFormat {
  101. DATA_FORMAT_IMAGE,
  102. DATA_FORMAT_PNG,
  103. DATA_FORMAT_WEBP,
  104. DATA_FORMAT_BASIS_UNIVERSAL,
  105. };
  106. enum {
  107. FORMAT_VERSION = 1
  108. };
  109. enum FormatBits {
  110. FORMAT_BIT_STREAM = 1 << 22,
  111. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  112. };
  113. private:
  114. Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
  115. String path_to_file;
  116. mutable RID texture;
  117. Image::Format format = Image::FORMAT_L8;
  118. int w = 0;
  119. int h = 0;
  120. int layers = 0;
  121. bool mipmaps = false;
  122. LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
  123. virtual void reload_from_file() override;
  124. protected:
  125. static void _bind_methods();
  126. public:
  127. Image::Format get_format() const override;
  128. Error load(const String &p_path);
  129. String get_load_path() const;
  130. virtual LayeredType get_layered_type() const override;
  131. int get_width() const override;
  132. int get_height() const override;
  133. int get_layers() const override;
  134. virtual bool has_mipmaps() const override;
  135. virtual RID get_rid() const override;
  136. virtual void set_path(const String &p_path, bool p_take_over) override;
  137. virtual Ref<Image> get_layer_data(int p_layer) const override;
  138. CompressedTextureLayered(LayeredType p_layered_type);
  139. ~CompressedTextureLayered();
  140. };
  141. class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {
  142. public:
  143. 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;
  144. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  145. virtual bool handles_type(const String &p_type) const override;
  146. virtual String get_resource_type(const String &p_path) const override;
  147. };
  148. class CompressedTexture2DArray : public CompressedTextureLayered {
  149. GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)
  150. public:
  151. CompressedTexture2DArray() :
  152. CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  153. };
  154. class CompressedCubemap : public CompressedTextureLayered {
  155. GDCLASS(CompressedCubemap, CompressedTextureLayered);
  156. public:
  157. CompressedCubemap() :
  158. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  159. };
  160. class CompressedCubemapArray : public CompressedTextureLayered {
  161. GDCLASS(CompressedCubemapArray, CompressedTextureLayered);
  162. public:
  163. CompressedCubemapArray() :
  164. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  165. };
  166. class CompressedTexture3D : public Texture3D {
  167. GDCLASS(CompressedTexture3D, Texture3D);
  168. public:
  169. enum DataFormat {
  170. DATA_FORMAT_IMAGE,
  171. DATA_FORMAT_PNG,
  172. DATA_FORMAT_WEBP,
  173. DATA_FORMAT_BASIS_UNIVERSAL,
  174. };
  175. enum {
  176. FORMAT_VERSION = 1
  177. };
  178. enum FormatBits {
  179. FORMAT_BIT_STREAM = 1 << 22,
  180. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  181. };
  182. private:
  183. 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);
  184. String path_to_file;
  185. mutable RID texture;
  186. Image::Format format = Image::FORMAT_L8;
  187. int w = 0;
  188. int h = 0;
  189. int d = 0;
  190. bool mipmaps = false;
  191. virtual void reload_from_file() override;
  192. protected:
  193. static void _bind_methods();
  194. public:
  195. Image::Format get_format() const override;
  196. Error load(const String &p_path);
  197. String get_load_path() const;
  198. int get_width() const override;
  199. int get_height() const override;
  200. int get_depth() const override;
  201. virtual bool has_mipmaps() const override;
  202. virtual RID get_rid() const override;
  203. virtual void set_path(const String &p_path, bool p_take_over) override;
  204. virtual Vector<Ref<Image>> get_data() const override;
  205. ~CompressedTexture3D();
  206. };
  207. class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {
  208. public:
  209. 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;
  210. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  211. virtual bool handles_type(const String &p_type) const override;
  212. virtual String get_resource_type(const String &p_path) const override;
  213. };