portable_compressed_texture.cpp 16 KB

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