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