| 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/config/project_settings.h" |
| 34 | #include "core/io/marshalls.h" |
| 35 | #include "scene/resources/bit_map.h" |
| 36 | #include "servers/rendering/rendering_server.h" |
| 37 | |
| 38 | static const char *compression_mode_names[7] = { |
| 39 | "Lossless", "Lossy", "Basis Universal", "S3TC", "ETC2", "BPTC", "ASTC" |
| 40 | }; |
| 41 | |
| 42 | static PortableCompressedTexture2D::CompressionMode get_expected_compression_mode(Image::Format format) { |
| 43 | if ((format >= Image::FORMAT_DXT1 && format <= Image::FORMAT_RGTC_RG) || format == Image::FORMAT_DXT5_RA_AS_RG) { |
| 44 | return PortableCompressedTexture2D::COMPRESSION_MODE_S3TC; |
| 45 | } else if (format >= Image::FORMAT_ETC && format <= Image::FORMAT_ETC2_RA_AS_RG) { |
| 46 | return PortableCompressedTexture2D::COMPRESSION_MODE_ETC2; |
| 47 | } else if (format >= Image::FORMAT_BPTC_RGBA && format <= Image::FORMAT_BPTC_RGBFU) { |
| 48 | return PortableCompressedTexture2D::COMPRESSION_MODE_BPTC; |
| 49 | } else if (format >= Image::FORMAT_ASTC_4x4 && format <= Image::FORMAT_ASTC_8x8_HDR) { |
| 50 | return PortableCompressedTexture2D::COMPRESSION_MODE_ASTC; |
| 51 | } |
| 52 | ERR_FAIL_V(PortableCompressedTexture2D::COMPRESSION_MODE_LOSSLESS); |
| 53 | } |
| 54 | |
| 55 | void PortableCompressedTexture2D::_set_data(const Vector<uint8_t> &p_data) { |
| 56 | if (p_data.is_empty()) { |
| 57 | return; //nothing to do |
| 58 | } |
| 59 | |
| 60 | const uint8_t *data = p_data.ptr(); |
| 61 | uint32_t data_size = p_data.size(); |
| 62 | ERR_FAIL_COND(data_size < 20); |
| 63 | compression_mode = CompressionMode(decode_uint16(data)); |
| 64 | DataFormat data_format = DataFormat(decode_uint16(data + 2)); |
| 65 | format = Image::Format(decode_uint32(data + 4)); |
| 66 | uint32_t mipmap_count = decode_uint32(data + 8); |
| 67 | size.width = decode_uint32(data + 12); |
| 68 | size.height = decode_uint32(data + 16); |
| 69 | mipmaps = mipmap_count > 1; |
| 70 | |
| 71 | data += 20; |
| 72 | data_size -= 20; |
| 73 | |
| 74 | Ref<Image> image; |
| 75 | |
| 76 | switch (compression_mode) { |
| 77 | case COMPRESSION_MODE_LOSSLESS: |
| 78 | case COMPRESSION_MODE_LOSSY: { |
| 79 | ImageMemLoadFunc loader_func; |
| 80 | if (data_format == DATA_FORMAT_UNDEFINED) { |
| 81 | loader_func = nullptr; |
| 82 | } else if (data_format == DATA_FORMAT_PNG) { |
| 83 | loader_func = Image::_png_mem_unpacker_func; |
| 84 | } else if (data_format == DATA_FORMAT_WEBP) { |
| 85 | loader_func = Image::_webp_mem_loader_func; |
| 86 | } else { |
| 87 | ERR_FAIL(); |
| 88 | } |
| 89 | Vector<uint8_t> image_data; |
| 90 | |
| 91 | ERR_FAIL_COND(data_size < 4); |
| 92 | for (uint32_t i = 0; i < mipmap_count; i++) { |
| 93 | uint32_t mipsize = decode_uint32(data); |
| 94 | data += 4; |
| 95 | data_size -= 4; |
| 96 | ERR_FAIL_COND(mipsize > data_size); |
| 97 | Ref<Image> img = loader_func == nullptr |
| 98 | ? memnew(Image(data, data_size)) |
| 99 | : Ref<Image>(loader_func(data, data_size)); |
| 100 | ERR_FAIL_COND(img->is_empty()); |
| 101 | if (img->get_format() != format) { // May happen due to webp/png in the tiny mipmaps. |
| 102 | img->convert(format); |
| 103 | } |
| 104 | image_data.append_array(img->get_data()); |
| 105 | |
| 106 | data += mipsize; |
| 107 | data_size -= mipsize; |
| 108 | } |
| 109 | |
| 110 | image.instantiate(size.width, size.height, mipmaps, format, image_data); |
| 111 | |
| 112 | } break; |
| 113 | case COMPRESSION_MODE_BASIS_UNIVERSAL: { |
| 114 | ERR_FAIL_NULL(Image::basis_universal_unpacker_ptr); |
| 115 | image = Image::basis_universal_unpacker_ptr(data, data_size); |
| 116 | format = image->get_format(); |
| 117 | } break; |
| 118 | case COMPRESSION_MODE_S3TC: |
| 119 | case COMPRESSION_MODE_ETC2: |
| 120 | case COMPRESSION_MODE_BPTC: |
| 121 | case COMPRESSION_MODE_ASTC: { |
| 122 | image.instantiate(size.width, size.height, mipmaps, format, p_data.slice(20)); |
| 123 | } break; |
| 124 | } |
| 125 | ERR_FAIL_COND(image.is_null()); |
| 126 | |
| 127 | if (texture.is_null()) { |
| 128 | texture = RenderingServer::get_singleton()->texture_2d_create(image); |
| 129 | } else { |
| 130 | RID new_texture = RenderingServer::get_singleton()->texture_2d_create(image); |
| 131 | RenderingServer::get_singleton()->texture_replace(texture, new_texture); |
| 132 | } |
| 133 | |
| 134 | image_stored = true; |
| 135 | size_override = size; |
| 136 | RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height); |
| 137 | alpha_cache.unref(); |
| 138 | |
| 139 | if (keep_all_compressed_buffers || keep_compressed_buffer) { |
| 140 | compressed_buffer = p_data; |
| 141 | } else { |
| 142 | compressed_buffer.clear(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | PortableCompressedTexture2D::CompressionMode PortableCompressedTexture2D::get_compression_mode() const { |
| 147 | return compression_mode; |
| 148 | } |
| 149 | Vector<uint8_t> PortableCompressedTexture2D::_get_data() const { |
| 150 | return compressed_buffer; |
| 151 | } |
| 152 | |
| 153 | void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map, float p_lossy_quality) { |
| 154 | ERR_FAIL_COND(p_image.is_null() || p_image->is_empty()); |
| 155 | |
| 156 | Vector<uint8_t> buffer; |
| 157 | |
| 158 | buffer.resize(20); |
| 159 | encode_uint16(p_compression_mode, buffer.ptrw()); |
| 160 | encode_uint16(DATA_FORMAT_UNDEFINED, buffer.ptrw() + 2); |
| 161 | encode_uint32(p_image->get_format(), buffer.ptrw() + 4); |
| 162 | encode_uint32(p_image->get_mipmap_count() + 1, buffer.ptrw() + 8); |
| 163 | encode_uint32(p_image->get_width(), buffer.ptrw() + 12); |
| 164 | encode_uint32(p_image->get_height(), buffer.ptrw() + 16); |
| 165 | |
| 166 | switch (p_compression_mode) { |
| 167 | case COMPRESSION_MODE_LOSSLESS: |
| 168 | case COMPRESSION_MODE_LOSSY: { |
| 169 | bool lossless_force_png = GLOBAL_GET_CACHED(bool, "rendering/textures/lossless_compression/force_png") || |
| 170 | !Image::_webp_mem_loader_func; // WebP module disabled. |
| 171 | bool use_webp = !lossless_force_png && p_image->get_width() <= 16383 && p_image->get_height() <= 16383; // WebP has a size limit. |
| 172 | for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) { |
| 173 | Vector<uint8_t> data; |
| 174 | if (p_compression_mode == COMPRESSION_MODE_LOSSY) { |
| 175 | data = Image::webp_lossy_packer(i ? p_image->get_image_from_mipmap(i) : p_image, p_lossy_quality); |
| 176 | encode_uint16(DATA_FORMAT_WEBP, buffer.ptrw() + 2); |
| 177 | } else { |
| 178 | if (use_webp) { |
| 179 | data = Image::webp_lossless_packer(i ? p_image->get_image_from_mipmap(i) : p_image); |
| 180 | encode_uint16(DATA_FORMAT_WEBP, buffer.ptrw() + 2); |
| 181 | } else { |
| 182 | data = Image::png_packer(i ? p_image->get_image_from_mipmap(i) : p_image); |
| 183 | encode_uint16(DATA_FORMAT_PNG, buffer.ptrw() + 2); |
| 184 | } |
| 185 | } |
| 186 | int data_len = data.size(); |
| 187 | buffer.resize(buffer.size() + 4); |
| 188 | encode_uint32(data_len, buffer.ptrw() + buffer.size() - 4); |
| 189 | buffer.append_array(data); |
| 190 | } |
| 191 | } break; |
| 192 | case COMPRESSION_MODE_BASIS_UNIVERSAL: { |
| 193 | #ifdef TOOLS_ENABLED |
| 194 | ERR_FAIL_COND(p_image->is_compressed()); |
| 195 | encode_uint16(DATA_FORMAT_BASIS_UNIVERSAL, buffer.ptrw() + 2); |
| 196 | Image::UsedChannels uc = p_image->detect_used_channels(p_normal_map ? Image::COMPRESS_SOURCE_NORMAL : Image::COMPRESS_SOURCE_GENERIC); |
| 197 | Vector<uint8_t> budata = Image::basis_universal_packer(p_image, uc, basisu_params); |
| 198 | buffer.append_array(budata); |
| 199 | #else |
| 200 | ERR_FAIL_MSG("Basis Universal compression can only run in editor build."); |
| 201 | #endif |
| 202 | } break; |
| 203 | case COMPRESSION_MODE_S3TC: |
| 204 | case COMPRESSION_MODE_ETC2: |
| 205 | case COMPRESSION_MODE_BPTC: |
| 206 | case COMPRESSION_MODE_ASTC: { |
| 207 | encode_uint16(DATA_FORMAT_IMAGE, buffer.ptrw() + 2); |
| 208 | Ref<Image> copy = p_image; |
| 209 | if (p_image->is_compressed()) { |
| 210 | CompressionMode expected_compression_mode = get_expected_compression_mode(p_image->get_format()); |
| 211 | ERR_FAIL_COND_MSG(expected_compression_mode != p_compression_mode, vformat("Mismatched compression mode for image format %s, expected %s, got %s.", Image::get_format_name(p_image->get_format()), compression_mode_names[expected_compression_mode], compression_mode_names[p_compression_mode])); |
| 212 | } else { |
| 213 | copy = p_image->duplicate(); |
| 214 | switch (p_compression_mode) { |
| 215 | case COMPRESSION_MODE_S3TC: |
| 216 | copy->compress(Image::COMPRESS_S3TC); |
| 217 | break; |
| 218 | case COMPRESSION_MODE_ETC2: |
| 219 | copy->compress(Image::COMPRESS_ETC2); |
| 220 | break; |
| 221 | case COMPRESSION_MODE_BPTC: |
| 222 | copy->compress(Image::COMPRESS_BPTC); |
| 223 | break; |
| 224 | case COMPRESSION_MODE_ASTC: |
| 225 | copy->compress(Image::COMPRESS_ASTC); |
| 226 | break; |
| 227 | default: { |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | encode_uint32(copy->get_format(), buffer.ptrw() + 4); |
| 232 | buffer.append_array(copy->get_data()); |
| 233 | |
| 234 | } break; |
| 235 | } |
| 236 | |
| 237 | _set_data(buffer); |
| 238 | } |
| 239 | |
| 240 | Image::Format PortableCompressedTexture2D::get_format() const { |
| 241 | return format; |
| 242 | } |
| 243 | |
| 244 | Ref<Image> PortableCompressedTexture2D::get_image() const { |
| 245 | if (image_stored) { |
| 246 | return RenderingServer::get_singleton()->texture_2d_get(texture); |
| 247 | } else { |
| 248 | return Ref<Image>(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | int PortableCompressedTexture2D::get_width() const { |
| 253 | return size.width; |
| 254 | } |
| 255 | |
| 256 | int PortableCompressedTexture2D::get_height() const { |
| 257 | return size.height; |
| 258 | } |
| 259 | |
| 260 | RID PortableCompressedTexture2D::get_rid() const { |
| 261 | if (texture.is_null()) { |
| 262 | // We are in trouble, create something temporary. |
| 263 | texture = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 264 | } |
| 265 | return texture; |
| 266 | } |
| 267 | |
| 268 | bool PortableCompressedTexture2D::has_alpha() const { |
| 269 | return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8); |
| 270 | } |
| 271 | |
| 272 | void PortableCompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const { |
| 273 | if (size.width == 0 || size.height == 0) { |
| 274 | return; |
| 275 | } |
| 276 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), texture, false, p_modulate, p_transpose); |
| 277 | } |
| 278 | |
| 279 | void PortableCompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const { |
| 280 | if (size.width == 0 || size.height == 0) { |
| 281 | return; |
| 282 | } |
| 283 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose); |
| 284 | } |
| 285 | |
| 286 | 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 { |
| 287 | if (size.width == 0 || size.height == 0) { |
| 288 | return; |
| 289 | } |
| 290 | 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); |
| 291 | } |
| 292 | |
| 293 | bool PortableCompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const { |
| 294 | if (alpha_cache.is_null()) { |
| 295 | Ref<Image> img = get_image(); |
| 296 | if (img.is_valid()) { |
| 297 | if (img->is_compressed()) { //must decompress, if compressed |
| 298 | Ref<Image> decom = img->duplicate(); |
| 299 | decom->decompress(); |
| 300 | img = decom; |
| 301 | } |
| 302 | alpha_cache.instantiate(); |
| 303 | alpha_cache->create_from_image_alpha(img); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (alpha_cache.is_valid()) { |
| 308 | int aw = int(alpha_cache->get_size().width); |
| 309 | int ah = int(alpha_cache->get_size().height); |
| 310 | if (aw == 0 || ah == 0) { |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | int x = p_x * aw / size.width; |
| 315 | int y = p_y * ah / size.height; |
| 316 | |
| 317 | x = CLAMP(x, 0, aw - 1); |
| 318 | y = CLAMP(y, 0, ah - 1); |
| 319 | |
| 320 | return alpha_cache->get_bit(x, y); |
| 321 | } |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | void PortableCompressedTexture2D::set_size_override(const Size2 &p_size) { |
| 327 | size_override = p_size; |
| 328 | RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height); |
| 329 | } |
| 330 | |
| 331 | Size2 PortableCompressedTexture2D::get_size_override() const { |
| 332 | return size_override; |
| 333 | } |
| 334 | |
| 335 | void PortableCompressedTexture2D::set_path(const String &p_path, bool p_take_over) { |
| 336 | if (texture.is_valid()) { |
| 337 | RenderingServer::get_singleton()->texture_set_path(texture, p_path); |
| 338 | } |
| 339 | |
| 340 | Resource::set_path(p_path, p_take_over); |
| 341 | } |
| 342 | |
| 343 | bool PortableCompressedTexture2D::keep_all_compressed_buffers = false; |
| 344 | |
| 345 | void PortableCompressedTexture2D::set_keep_all_compressed_buffers(bool p_keep) { |
| 346 | keep_all_compressed_buffers = p_keep; |
| 347 | } |
| 348 | |
| 349 | bool PortableCompressedTexture2D::is_keeping_all_compressed_buffers() { |
| 350 | return keep_all_compressed_buffers; |
| 351 | } |
| 352 | |
| 353 | void PortableCompressedTexture2D::set_keep_compressed_buffer(bool p_keep) { |
| 354 | keep_compressed_buffer = p_keep; |
| 355 | if (!p_keep) { |
| 356 | compressed_buffer.clear(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | bool PortableCompressedTexture2D::is_keeping_compressed_buffer() const { |
| 361 | return keep_compressed_buffer; |
| 362 | } |
| 363 | |
| 364 | void PortableCompressedTexture2D::set_basisu_compressor_params(int p_uastc_level, float p_rdo_quality_loss) { |
| 365 | basisu_params.uastc_level = p_uastc_level; |
| 366 | basisu_params.rdo_quality_loss = p_rdo_quality_loss; |
| 367 | } |
| 368 | |
| 369 | void PortableCompressedTexture2D::_bind_methods() { |
| 370 | ClassDB::bind_method(D_METHOD("create_from_image", "image", "compression_mode", "normal_map", "lossy_quality"), &PortableCompressedTexture2D::create_from_image, DEFVAL(false), DEFVAL(0.8)); |
| 371 | ClassDB::bind_method(D_METHOD("get_format"), &PortableCompressedTexture2D::get_format); |
| 372 | ClassDB::bind_method(D_METHOD("get_compression_mode"), &PortableCompressedTexture2D::get_compression_mode); |
| 373 | |
| 374 | ClassDB::bind_method(D_METHOD("set_size_override", "size"), &PortableCompressedTexture2D::set_size_override); |
| 375 | ClassDB::bind_method(D_METHOD("get_size_override"), &PortableCompressedTexture2D::get_size_override); |
| 376 | |
| 377 | ClassDB::bind_method(D_METHOD("set_keep_compressed_buffer", "keep"), &PortableCompressedTexture2D::set_keep_compressed_buffer); |
| 378 | ClassDB::bind_method(D_METHOD("is_keeping_compressed_buffer"), &PortableCompressedTexture2D::is_keeping_compressed_buffer); |
| 379 | |
| 380 | ClassDB::bind_method(D_METHOD("set_basisu_compressor_params", "uastc_level", "rdo_quality_loss"), &PortableCompressedTexture2D::set_basisu_compressor_params); |
| 381 | |
| 382 | ClassDB::bind_method(D_METHOD("_set_data", "data"), &PortableCompressedTexture2D::_set_data); |
| 383 | ClassDB::bind_method(D_METHOD("_get_data"), &PortableCompressedTexture2D::_get_data); |
| 384 | |
| 385 | ClassDB::bind_static_method("PortableCompressedTexture2D", D_METHOD("set_keep_all_compressed_buffers", "keep"), &PortableCompressedTexture2D::set_keep_all_compressed_buffers); |
| 386 | ClassDB::bind_static_method("PortableCompressedTexture2D", D_METHOD("is_keeping_all_compressed_buffers"), &PortableCompressedTexture2D::is_keeping_all_compressed_buffers); |
| 387 | |
| 388 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data"); |
| 389 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size_override", PROPERTY_HINT_NONE, "suffix:px"), "set_size_override", "get_size_override"); |
| 390 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_compressed_buffer"), "set_keep_compressed_buffer", "is_keeping_compressed_buffer"); |
| 391 | |
| 392 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSLESS); |
| 393 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSY); |
| 394 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_BASIS_UNIVERSAL); |
| 395 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_S3TC); |
| 396 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_ETC2); |
| 397 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_BPTC); |
| 398 | BIND_ENUM_CONSTANT(COMPRESSION_MODE_ASTC); |
| 399 | } |
| 400 | |
| 401 | PortableCompressedTexture2D::~PortableCompressedTexture2D() { |
| 402 | if (texture.is_valid()) { |
| 403 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 404 | RenderingServer::get_singleton()->free_rid(texture); |
| 405 | } |
| 406 | } |