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