1 | /**************************************************************************/ |
2 | /* portable_compressed_texture.cpp */ |
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 | #include "portable_compressed_texture.h" |
32 | |
33 | #include "core/io/marshalls.h" |
34 | #include "scene/resources/bit_map.h" |
35 | |
36 | void PortableCompressedTexture2D::_set_data(const Vector<uint8_t> &p_data) { |
37 | if (p_data.size() == 0) { |
38 | return; //nothing to do |
39 | } |
40 | |
41 | const uint8_t *data = p_data.ptr(); |
42 | uint32_t data_size = p_data.size(); |
43 | ERR_FAIL_COND(data_size < 20); |
44 | compression_mode = CompressionMode(decode_uint32(data + 0)); |
45 | format = Image::Format(decode_uint32(data + 4)); |
46 | uint32_t mipmap_count = decode_uint32(data + 8); |
47 | size.width = decode_uint32(data + 12); |
48 | size.height = decode_uint32(data + 16); |
49 | mipmaps = mipmap_count > 1; |
50 | |
51 | data += 20; |
52 | data_size -= 20; |
53 | |
54 | Ref<Image> image; |
55 | |
56 | switch (compression_mode) { |
57 | case COMPRESSION_MODE_LOSSLESS: |
58 | case COMPRESSION_MODE_LOSSY: { |
59 | Vector<uint8_t> image_data; |
60 | |
61 | ERR_FAIL_COND(data_size < 4); |
62 | for (uint32_t i = 0; i < mipmap_count; i++) { |
63 | uint32_t mipsize = decode_uint32(data); |
64 | data += 4; |
65 | data_size -= 4; |
66 | ERR_FAIL_COND(mipsize < data_size); |
67 | Ref<Image> img = memnew(Image(data, data_size)); |
68 | ERR_FAIL_COND(img->is_empty()); |
69 | if (img->get_format() != format) { // May happen due to webp/png in the tiny mipmaps. |
70 | img->convert(format); |
71 | } |
72 | image_data.append_array(img->get_data()); |
73 | |
74 | data += mipsize; |
75 | data_size -= mipsize; |
76 | } |
77 | |
78 | image = Ref<Image>(memnew(Image(size.width, size.height, mipmap_count > 1, format, image_data))); |
79 | |
80 | } break; |
81 | case COMPRESSION_MODE_BASIS_UNIVERSAL: { |
82 | ERR_FAIL_NULL(Image::basis_universal_unpacker_ptr); |
83 | image = Image::basis_universal_unpacker_ptr(data, data_size); |
84 | |
85 | } break; |
86 | case COMPRESSION_MODE_S3TC: |
87 | case COMPRESSION_MODE_ETC2: |
88 | case COMPRESSION_MODE_BPTC: { |
89 | image = Ref<Image>(memnew(Image(size.width, size.height, mipmap_count > 1, format, p_data.slice(20)))); |
90 | } break; |
91 | } |
92 | ERR_FAIL_COND(image.is_null()); |
93 | |
94 | if (texture.is_null()) { |
95 | texture = RenderingServer::get_singleton()->texture_2d_create(image); |
96 | } else { |
97 | RID new_texture = RenderingServer::get_singleton()->texture_2d_create(image); |
98 | RenderingServer::get_singleton()->texture_replace(texture, new_texture); |
99 | } |
100 | |
101 | image_stored = true; |
102 | RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height); |
103 | alpha_cache.unref(); |
104 | |
105 | if (keep_all_compressed_buffers || keep_compressed_buffer) { |
106 | compressed_buffer = p_data; |
107 | } else { |
108 | compressed_buffer.clear(); |
109 | } |
110 | } |
111 | |
112 | PortableCompressedTexture2D::CompressionMode PortableCompressedTexture2D::get_compression_mode() const { |
113 | return compression_mode; |
114 | } |
115 | Vector<uint8_t> PortableCompressedTexture2D::_get_data() const { |
116 | return compressed_buffer; |
117 | } |
118 | |
119 | void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map, float p_lossy_quality) { |
120 | ERR_FAIL_COND(p_image.is_null() || p_image->is_empty()); |
121 | |
122 | Vector<uint8_t> buffer; |
123 | |
124 | buffer.resize(20); |
125 | encode_uint32(p_compression_mode, buffer.ptrw()); |
126 | encode_uint32(p_image->get_format(), buffer.ptrw() + 4); |
127 | encode_uint32(p_image->get_mipmap_count() + 1, buffer.ptrw() + 8); |
128 | encode_uint32(p_image->get_width(), buffer.ptrw() + 12); |
129 | encode_uint32(p_image->get_height(), buffer.ptrw() + 16); |
130 | |
131 | switch (p_compression_mode) { |
132 | case COMPRESSION_MODE_LOSSLESS: |
133 | case COMPRESSION_MODE_LOSSY: { |
134 | for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) { |
135 | Vector<uint8_t> data; |
136 | if (p_compression_mode == COMPRESSION_MODE_LOSSY) { |
137 | data = Image::webp_lossy_packer(p_image->get_image_from_mipmap(i), p_lossy_quality); |
138 | } else { |
139 | data = Image::webp_lossless_packer(p_image->get_image_from_mipmap(i)); |
140 | } |
141 | int data_len = data.size(); |
142 | buffer.resize(buffer.size() + 4); |
143 | encode_uint32(data_len, buffer.ptrw() + buffer.size() - 4); |
144 | buffer.append_array(data); |
145 | } |
146 | } break; |
147 | case COMPRESSION_MODE_BASIS_UNIVERSAL: { |
148 | Image::UsedChannels uc = p_image->detect_used_channels(p_normal_map ? Image::COMPRESS_SOURCE_NORMAL : Image::COMPRESS_SOURCE_GENERIC); |
149 | Vector<uint8_t> budata = Image::basis_universal_packer(p_image, uc); |
150 | buffer.append_array(budata); |
151 | |
152 | } break; |
153 | case COMPRESSION_MODE_S3TC: |
154 | case COMPRESSION_MODE_ETC2: |
155 | case COMPRESSION_MODE_BPTC: { |
156 | Ref<Image> copy = p_image->duplicate(); |
157 | switch (p_compression_mode) { |
158 | case COMPRESSION_MODE_S3TC: |
159 | copy->compress(Image::COMPRESS_S3TC); |
160 | break; |
161 | case COMPRESSION_MODE_ETC2: |
162 | copy->compress(Image::COMPRESS_ETC2); |
163 | break; |
164 | case COMPRESSION_MODE_BPTC: |
165 | copy->compress(Image::COMPRESS_BPTC); |
166 | break; |
167 | default: { |
168 | }; |
169 | } |
170 | |
171 | buffer.append_array(copy->get_data()); |
172 | |
173 | } break; |
174 | } |
175 | |
176 | _set_data(buffer); |
177 | } |
178 | |
179 | Image::Format PortableCompressedTexture2D::get_format() const { |
180 | return format; |
181 | } |
182 | |
183 | Ref<Image> PortableCompressedTexture2D::get_image() const { |
184 | if (image_stored) { |
185 | return RenderingServer::get_singleton()->texture_2d_get(texture); |
186 | } else { |
187 | return Ref<Image>(); |
188 | } |
189 | } |
190 | |
191 | int PortableCompressedTexture2D::get_width() const { |
192 | return size.width; |
193 | } |
194 | |
195 | int PortableCompressedTexture2D::get_height() const { |
196 | return size.height; |
197 | } |
198 | |
199 | RID PortableCompressedTexture2D::get_rid() const { |
200 | if (texture.is_null()) { |
201 | // We are in trouble, create something temporary. |
202 | texture = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
203 | } |
204 | return texture; |
205 | } |
206 | |
207 | bool PortableCompressedTexture2D::has_alpha() const { |
208 | return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8); |
209 | } |
210 | |
211 | void PortableCompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const { |
212 | if (size.width == 0 || size.height == 0) { |
213 | return; |
214 | } |
215 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), texture, false, p_modulate, p_transpose); |
216 | } |
217 | |
218 | void PortableCompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const { |
219 | if (size.width == 0 || size.height == 0) { |
220 | return; |
221 | } |
222 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose); |
223 | } |
224 | |
225 | void PortableCompressedTexture2D::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const { |
226 | if (size.width == 0 || size.height == 0) { |
227 | return; |
228 | } |
229 | RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv); |
230 | } |
231 | |
232 | bool PortableCompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const { |
233 | if (!alpha_cache.is_valid()) { |
234 | Ref<Image> img = get_image(); |
235 | if (img.is_valid()) { |
236 | if (img->is_compressed()) { //must decompress, if compressed |
237 | Ref<Image> decom = img->duplicate(); |
238 | decom->decompress(); |
239 | img = decom; |
240 | } |
241 | alpha_cache.instantiate(); |
242 | alpha_cache->create_from_image_alpha(img); |
243 | } |
244 | } |
245 | |
246 | if (alpha_cache.is_valid()) { |
247 | int aw = int(alpha_cache->get_size().width); |
248 | int ah = int(alpha_cache->get_size().height); |
249 | if (aw == 0 || ah == 0) { |
250 | return true; |
251 | } |
252 | |
253 | int x = p_x * aw / size.width; |
254 | int y = p_y * ah / size.height; |
255 | |
256 | x = CLAMP(x, 0, aw); |
257 | y = CLAMP(y, 0, ah); |
258 | |
259 | return alpha_cache->get_bit(x, y); |
260 | } |
261 | |
262 | return true; |
263 | } |
264 | |
265 | void PortableCompressedTexture2D::set_size_override(const Size2 &p_size) { |
266 | size_override = p_size; |
267 | RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height); |
268 | } |
269 | |
270 | Size2 PortableCompressedTexture2D::get_size_override() const { |
271 | return size_override; |
272 | } |
273 | |
274 | void PortableCompressedTexture2D::set_path(const String &p_path, bool p_take_over) { |
275 | if (texture.is_valid()) { |
276 | RenderingServer::get_singleton()->texture_set_path(texture, p_path); |
277 | } |
278 | |
279 | Resource::set_path(p_path, p_take_over); |
280 | } |
281 | |
282 | bool PortableCompressedTexture2D::keep_all_compressed_buffers = false; |
283 | |
284 | void PortableCompressedTexture2D::set_keep_all_compressed_buffers(bool p_keep) { |
285 | keep_all_compressed_buffers = p_keep; |
286 | } |
287 | |
288 | bool PortableCompressedTexture2D::is_keeping_all_compressed_buffers() { |
289 | return keep_all_compressed_buffers; |
290 | } |
291 | |
292 | void PortableCompressedTexture2D::set_keep_compressed_buffer(bool p_keep) { |
293 | keep_compressed_buffer = p_keep; |
294 | if (!p_keep) { |
295 | compressed_buffer.clear(); |
296 | } |
297 | } |
298 | |
299 | bool PortableCompressedTexture2D::is_keeping_compressed_buffer() const { |
300 | return keep_compressed_buffer; |
301 | } |
302 | |
303 | void PortableCompressedTexture2D::_bind_methods() { |
304 | ClassDB::bind_method(D_METHOD("create_from_image" , "image" , "compression_mode" , "normal_map" , "lossy_quality" ), &PortableCompressedTexture2D::create_from_image, DEFVAL(false), DEFVAL(0.8)); |
305 | ClassDB::bind_method(D_METHOD("get_format" ), &PortableCompressedTexture2D::get_format); |
306 | ClassDB::bind_method(D_METHOD("get_compression_mode" ), &PortableCompressedTexture2D::get_compression_mode); |
307 | |
308 | ClassDB::bind_method(D_METHOD("set_size_override" , "size" ), &PortableCompressedTexture2D::set_size_override); |
309 | ClassDB::bind_method(D_METHOD("get_size_override" ), &PortableCompressedTexture2D::get_size_override); |
310 | |
311 | ClassDB::bind_method(D_METHOD("set_keep_compressed_buffer" , "keep" ), &PortableCompressedTexture2D::set_keep_compressed_buffer); |
312 | ClassDB::bind_method(D_METHOD("is_keeping_compressed_buffer" ), &PortableCompressedTexture2D::is_keeping_compressed_buffer); |
313 | |
314 | ClassDB::bind_method(D_METHOD("_set_data" , "data" ), &PortableCompressedTexture2D::_set_data); |
315 | ClassDB::bind_method(D_METHOD("_get_data" ), &PortableCompressedTexture2D::_get_data); |
316 | |
317 | ClassDB::bind_static_method("PortableCompressedTexture2D" , D_METHOD("set_keep_all_compressed_buffers" , "keep" ), &PortableCompressedTexture2D::set_keep_all_compressed_buffers); |
318 | ClassDB::bind_static_method("PortableCompressedTexture2D" , D_METHOD("is_keeping_all_compressed_buffers" ), &PortableCompressedTexture2D::is_keeping_all_compressed_buffers); |
319 | |
320 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "_data" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "_set_data" , "_get_data" ); |
321 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size_override" , PROPERTY_HINT_NONE, "suffix:px" ), "set_size_override" , "get_size_override" ); |
322 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_compressed_buffer" ), "set_keep_compressed_buffer" , "is_keeping_compressed_buffer" ); |
323 | |
324 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSLESS); |
325 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSY); |
326 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_BASIS_UNIVERSAL); |
327 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_S3TC); |
328 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_ETC2); |
329 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_BPTC); |
330 | } |
331 | |
332 | PortableCompressedTexture2D::PortableCompressedTexture2D() {} |
333 | |
334 | PortableCompressedTexture2D::~PortableCompressedTexture2D() { |
335 | if (texture.is_valid()) { |
336 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
337 | RenderingServer::get_singleton()->free(texture); |
338 | } |
339 | } |
340 | |