portable_compressed_texture.cpp 14 KB

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