compressed_texture.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /**************************************************************************/
  2. /* 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 "compressed_texture.h"
  31. #include "core/io/file_access.h"
  32. #include "scene/resources/bit_map.h"
  33. Error CompressedTexture2D::_load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit) {
  34. alpha_cache.unref();
  35. ERR_FAIL_COND_V(image.is_null(), ERR_INVALID_PARAMETER);
  36. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  37. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  38. uint8_t header[4];
  39. f->get_buffer(header, 4);
  40. if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != '2') {
  41. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is corrupt (Bad header).");
  42. }
  43. uint32_t version = f->get_32();
  44. if (version > FORMAT_VERSION) {
  45. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  46. }
  47. r_width = f->get_32();
  48. r_height = f->get_32();
  49. uint32_t df = f->get_32(); //data format
  50. //skip reserved
  51. mipmap_limit = int(f->get_32());
  52. //reserved
  53. f->get_32();
  54. f->get_32();
  55. f->get_32();
  56. #ifdef TOOLS_ENABLED
  57. r_request_3d = request_3d_callback && df & FORMAT_BIT_DETECT_3D;
  58. r_request_roughness = request_roughness_callback && df & FORMAT_BIT_DETECT_ROUGNESS;
  59. r_request_normal = request_normal_callback && df & FORMAT_BIT_DETECT_NORMAL;
  60. #else
  61. r_request_3d = false;
  62. r_request_roughness = false;
  63. r_request_normal = false;
  64. #endif
  65. if (!(df & FORMAT_BIT_STREAM)) {
  66. p_size_limit = 0;
  67. }
  68. image = load_image_from_file(f, p_size_limit);
  69. if (image.is_null() || image->is_empty()) {
  70. return ERR_CANT_OPEN;
  71. }
  72. return OK;
  73. }
  74. void CompressedTexture2D::set_path(const String &p_path, bool p_take_over) {
  75. if (texture.is_valid()) {
  76. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  77. }
  78. Resource::set_path(p_path, p_take_over);
  79. }
  80. void CompressedTexture2D::_requested_3d(void *p_ud) {
  81. CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
  82. Ref<CompressedTexture2D> ctex(ct);
  83. ERR_FAIL_NULL(request_3d_callback);
  84. request_3d_callback(ctex);
  85. }
  86. void CompressedTexture2D::_requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel) {
  87. CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
  88. Ref<CompressedTexture2D> ctex(ct);
  89. ERR_FAIL_NULL(request_roughness_callback);
  90. request_roughness_callback(ctex, p_normal_path, p_roughness_channel);
  91. }
  92. void CompressedTexture2D::_requested_normal(void *p_ud) {
  93. CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
  94. Ref<CompressedTexture2D> ctex(ct);
  95. ERR_FAIL_NULL(request_normal_callback);
  96. request_normal_callback(ctex);
  97. }
  98. CompressedTexture2D::TextureFormatRequestCallback CompressedTexture2D::request_3d_callback = nullptr;
  99. CompressedTexture2D::TextureFormatRoughnessRequestCallback CompressedTexture2D::request_roughness_callback = nullptr;
  100. CompressedTexture2D::TextureFormatRequestCallback CompressedTexture2D::request_normal_callback = nullptr;
  101. Image::Format CompressedTexture2D::get_format() const {
  102. return format;
  103. }
  104. Error CompressedTexture2D::load(const String &p_path) {
  105. int lw, lh;
  106. Ref<Image> image;
  107. image.instantiate();
  108. bool request_3d;
  109. bool request_normal;
  110. bool request_roughness;
  111. int mipmap_limit;
  112. Error err = _load_data(p_path, lw, lh, image, request_3d, request_normal, request_roughness, mipmap_limit);
  113. if (err) {
  114. return err;
  115. }
  116. if (texture.is_valid()) {
  117. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  118. RS::get_singleton()->texture_replace(texture, new_texture);
  119. } else {
  120. texture = RS::get_singleton()->texture_2d_create(image);
  121. }
  122. if (lw || lh) {
  123. RS::get_singleton()->texture_set_size_override(texture, lw, lh);
  124. }
  125. w = lw;
  126. h = lh;
  127. path_to_file = p_path;
  128. format = image->get_format();
  129. if (get_path().is_empty()) {
  130. //temporarily set path if no path set for resource, helps find errors
  131. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  132. }
  133. #ifdef TOOLS_ENABLED
  134. if (request_3d) {
  135. //print_line("request detect 3D at " + p_path);
  136. RS::get_singleton()->texture_set_detect_3d_callback(texture, _requested_3d, this);
  137. } else {
  138. //print_line("not requesting detect 3D at " + p_path);
  139. RS::get_singleton()->texture_set_detect_3d_callback(texture, nullptr, nullptr);
  140. }
  141. if (request_roughness) {
  142. //print_line("request detect srgb at " + p_path);
  143. RS::get_singleton()->texture_set_detect_roughness_callback(texture, _requested_roughness, this);
  144. } else {
  145. //print_line("not requesting detect srgb at " + p_path);
  146. RS::get_singleton()->texture_set_detect_roughness_callback(texture, nullptr, nullptr);
  147. }
  148. if (request_normal) {
  149. //print_line("request detect srgb at " + p_path);
  150. RS::get_singleton()->texture_set_detect_normal_callback(texture, _requested_normal, this);
  151. } else {
  152. //print_line("not requesting detect normal at " + p_path);
  153. RS::get_singleton()->texture_set_detect_normal_callback(texture, nullptr, nullptr);
  154. }
  155. #endif
  156. notify_property_list_changed();
  157. emit_changed();
  158. return OK;
  159. }
  160. String CompressedTexture2D::get_load_path() const {
  161. return path_to_file;
  162. }
  163. int CompressedTexture2D::get_width() const {
  164. return w;
  165. }
  166. int CompressedTexture2D::get_height() const {
  167. return h;
  168. }
  169. RID CompressedTexture2D::get_rid() const {
  170. if (!texture.is_valid()) {
  171. texture = RS::get_singleton()->texture_2d_placeholder_create();
  172. }
  173. return texture;
  174. }
  175. void CompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  176. if ((w | h) == 0) {
  177. return;
  178. }
  179. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose);
  180. }
  181. void CompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  182. if ((w | h) == 0) {
  183. return;
  184. }
  185. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
  186. }
  187. void CompressedTexture2D::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 {
  188. if ((w | h) == 0) {
  189. return;
  190. }
  191. 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);
  192. }
  193. bool CompressedTexture2D::has_alpha() const {
  194. return false;
  195. }
  196. Ref<Image> CompressedTexture2D::get_image() const {
  197. if (texture.is_valid()) {
  198. return RS::get_singleton()->texture_2d_get(texture);
  199. } else {
  200. return Ref<Image>();
  201. }
  202. }
  203. bool CompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const {
  204. if (alpha_cache.is_null()) {
  205. Ref<Image> img = get_image();
  206. if (img.is_valid()) {
  207. if (img->is_compressed()) { //must decompress, if compressed
  208. Ref<Image> decom = img->duplicate();
  209. decom->decompress();
  210. img = decom;
  211. }
  212. alpha_cache.instantiate();
  213. alpha_cache->create_from_image_alpha(img);
  214. }
  215. }
  216. if (alpha_cache.is_valid()) {
  217. int aw = int(alpha_cache->get_size().width);
  218. int ah = int(alpha_cache->get_size().height);
  219. if (aw == 0 || ah == 0) {
  220. return true;
  221. }
  222. int x = p_x * aw / w;
  223. int y = p_y * ah / h;
  224. x = CLAMP(x, 0, aw - 1);
  225. y = CLAMP(y, 0, ah - 1);
  226. return alpha_cache->get_bit(x, y);
  227. }
  228. return true;
  229. }
  230. void CompressedTexture2D::reload_from_file() {
  231. String path = get_path();
  232. if (!path.is_resource_file()) {
  233. return;
  234. }
  235. path = ResourceLoader::path_remap(path); //remap for translation
  236. path = ResourceLoader::import_remap(path); //remap for import
  237. if (!path.is_resource_file()) {
  238. return;
  239. }
  240. load(path);
  241. }
  242. Ref<Image> CompressedTexture2D::load_image_from_file(Ref<FileAccess> f, int p_size_limit) {
  243. uint32_t data_format = f->get_32();
  244. uint32_t w = f->get_16();
  245. uint32_t h = f->get_16();
  246. uint32_t mipmaps = f->get_32();
  247. Image::Format format = Image::Format(f->get_32());
  248. if (data_format == DATA_FORMAT_PNG || data_format == DATA_FORMAT_WEBP) {
  249. //look for a PNG or WebP file inside
  250. int sw = w;
  251. int sh = h;
  252. //mipmaps need to be read independently, they will be later combined
  253. Vector<Ref<Image>> mipmap_images;
  254. uint64_t total_size = 0;
  255. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  256. uint32_t size = f->get_32();
  257. if (p_size_limit > 0 && i < (mipmaps - 1) && (sw > p_size_limit || sh > p_size_limit)) {
  258. //can't load this due to size limit
  259. sw = MAX(sw >> 1, 1);
  260. sh = MAX(sh >> 1, 1);
  261. f->seek(f->get_position() + size);
  262. continue;
  263. }
  264. Vector<uint8_t> pv;
  265. pv.resize(size);
  266. {
  267. uint8_t *wr = pv.ptrw();
  268. f->get_buffer(wr, size);
  269. }
  270. Ref<Image> img;
  271. if (data_format == DATA_FORMAT_PNG && Image::png_unpacker) {
  272. img = Image::png_unpacker(pv);
  273. } else if (data_format == DATA_FORMAT_WEBP && Image::webp_unpacker) {
  274. img = Image::webp_unpacker(pv);
  275. }
  276. if (img.is_null() || img->is_empty()) {
  277. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  278. }
  279. // If the image is compressed and its format doesn't match the desired format, return an empty reference.
  280. // This is done to avoid recompressing the image on load.
  281. ERR_FAIL_COND_V(img->is_compressed() && format != img->get_format(), Ref<Image>());
  282. // The format will actually be the format of the header,
  283. // as it may have changed on compression.
  284. if (format != img->get_format()) {
  285. // Convert the image to the desired format.
  286. // Note: We are not decompressing the image here, just changing its format.
  287. // It's important that all images in the texture array share the same format for correct rendering.
  288. img->convert(format);
  289. }
  290. total_size += img->get_data().size();
  291. mipmap_images.push_back(img);
  292. sw = MAX(sw >> 1, 1);
  293. sh = MAX(sh >> 1, 1);
  294. }
  295. //print_line("mipmap read total: " + itos(mipmap_images.size()));
  296. Ref<Image> image;
  297. image.instantiate();
  298. if (mipmap_images.size() == 1) {
  299. //only one image (which will most likely be the case anyway for this format)
  300. image = mipmap_images[0];
  301. return image;
  302. } else {
  303. //rarer use case, but needs to be supported
  304. Vector<uint8_t> img_data;
  305. img_data.resize(total_size);
  306. {
  307. uint8_t *wr = img_data.ptrw();
  308. int ofs = 0;
  309. for (int i = 0; i < mipmap_images.size(); i++) {
  310. Vector<uint8_t> id = mipmap_images[i]->get_data();
  311. int len = id.size();
  312. const uint8_t *r = id.ptr();
  313. memcpy(&wr[ofs], r, len);
  314. ofs += len;
  315. }
  316. }
  317. image->set_data(w, h, true, mipmap_images[0]->get_format(), img_data);
  318. return image;
  319. }
  320. } else if (data_format == DATA_FORMAT_BASIS_UNIVERSAL) {
  321. int sw = w;
  322. int sh = h;
  323. uint32_t size = f->get_32();
  324. if (p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  325. //can't load this due to size limit
  326. sw = MAX(sw >> 1, 1);
  327. sh = MAX(sh >> 1, 1);
  328. f->seek(f->get_position() + size);
  329. return Ref<Image>();
  330. }
  331. Vector<uint8_t> pv;
  332. pv.resize(size);
  333. {
  334. uint8_t *wr = pv.ptrw();
  335. f->get_buffer(wr, size);
  336. }
  337. Ref<Image> img;
  338. img = Image::basis_universal_unpacker(pv);
  339. if (img.is_null() || img->is_empty()) {
  340. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  341. }
  342. format = img->get_format();
  343. sw = MAX(sw >> 1, 1);
  344. sh = MAX(sh >> 1, 1);
  345. return img;
  346. } else if (data_format == DATA_FORMAT_IMAGE) {
  347. int size = Image::get_image_data_size(w, h, format, mipmaps ? true : false);
  348. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  349. int tw, th;
  350. int ofs = Image::get_image_mipmap_offset_and_dimensions(w, h, format, i, tw, th);
  351. if (p_size_limit > 0 && i < mipmaps && (p_size_limit > tw || p_size_limit > th)) {
  352. if (ofs) {
  353. f->seek(f->get_position() + ofs);
  354. }
  355. continue; //oops, size limit enforced, go to next
  356. }
  357. Vector<uint8_t> data;
  358. data.resize(size - ofs);
  359. {
  360. uint8_t *wr = data.ptrw();
  361. f->get_buffer(wr, data.size());
  362. }
  363. Ref<Image> image = Image::create_from_data(tw, th, mipmaps - i ? true : false, format, data);
  364. return image;
  365. }
  366. }
  367. return Ref<Image>();
  368. }
  369. void CompressedTexture2D::_bind_methods() {
  370. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture2D::load);
  371. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture2D::get_load_path);
  372. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  373. }
  374. CompressedTexture2D::~CompressedTexture2D() {
  375. if (texture.is_valid()) {
  376. ERR_FAIL_NULL(RenderingServer::get_singleton());
  377. RS::get_singleton()->free_rid(texture);
  378. }
  379. }
  380. Ref<Resource> ResourceFormatLoaderCompressedTexture2D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  381. Ref<CompressedTexture2D> st;
  382. st.instantiate();
  383. Error err = st->load(p_path);
  384. if (r_error) {
  385. *r_error = err;
  386. }
  387. if (err != OK) {
  388. return Ref<Resource>();
  389. }
  390. return st;
  391. }
  392. void ResourceFormatLoaderCompressedTexture2D::get_recognized_extensions(List<String> *p_extensions) const {
  393. p_extensions->push_back("ctex");
  394. }
  395. bool ResourceFormatLoaderCompressedTexture2D::handles_type(const String &p_type) const {
  396. return p_type == "CompressedTexture2D";
  397. }
  398. String ResourceFormatLoaderCompressedTexture2D::get_resource_type(const String &p_path) const {
  399. if (p_path.has_extension("ctex")) {
  400. return "CompressedTexture2D";
  401. }
  402. return "";
  403. }
  404. void CompressedTexture3D::set_path(const String &p_path, bool p_take_over) {
  405. if (texture.is_valid()) {
  406. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  407. }
  408. Resource::set_path(p_path, p_take_over);
  409. }
  410. Image::Format CompressedTexture3D::get_format() const {
  411. return format;
  412. }
  413. Error CompressedTexture3D::_load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps) {
  414. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  415. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  416. uint8_t header[4];
  417. f->get_buffer(header, 4);
  418. ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L', ERR_FILE_UNRECOGNIZED);
  419. //stored as compressed textures (used for lossless and lossy compression)
  420. uint32_t version = f->get_32();
  421. if (version > FORMAT_VERSION) {
  422. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  423. }
  424. r_depth = f->get_32(); //depth
  425. f->get_32(); //ignored (mode)
  426. f->get_32(); // ignored (data format)
  427. f->get_32(); //ignored
  428. int mipmap_count = f->get_32();
  429. f->get_32(); //ignored
  430. f->get_32(); //ignored
  431. r_mipmaps = mipmap_count != 0;
  432. r_data.clear();
  433. for (int i = 0; i < (r_depth + mipmap_count); i++) {
  434. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, 0);
  435. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  436. if (i == 0) {
  437. r_format = image->get_format();
  438. r_width = image->get_width();
  439. r_height = image->get_height();
  440. }
  441. r_data.push_back(image);
  442. }
  443. return OK;
  444. }
  445. Error CompressedTexture3D::load(const String &p_path) {
  446. Vector<Ref<Image>> data;
  447. int tw, th, td;
  448. Image::Format tfmt;
  449. bool tmm;
  450. Error err = _load_data(p_path, data, tfmt, tw, th, td, tmm);
  451. if (err) {
  452. return err;
  453. }
  454. if (texture.is_valid()) {
  455. RID new_texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
  456. RS::get_singleton()->texture_replace(texture, new_texture);
  457. } else {
  458. texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
  459. }
  460. w = tw;
  461. h = th;
  462. d = td;
  463. mipmaps = tmm;
  464. format = tfmt;
  465. path_to_file = p_path;
  466. if (get_path().is_empty()) {
  467. //temporarily set path if no path set for resource, helps find errors
  468. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  469. }
  470. notify_property_list_changed();
  471. emit_changed();
  472. return OK;
  473. }
  474. String CompressedTexture3D::get_load_path() const {
  475. return path_to_file;
  476. }
  477. int CompressedTexture3D::get_width() const {
  478. return w;
  479. }
  480. int CompressedTexture3D::get_height() const {
  481. return h;
  482. }
  483. int CompressedTexture3D::get_depth() const {
  484. return d;
  485. }
  486. bool CompressedTexture3D::has_mipmaps() const {
  487. return mipmaps;
  488. }
  489. RID CompressedTexture3D::get_rid() const {
  490. if (!texture.is_valid()) {
  491. texture = RS::get_singleton()->texture_3d_placeholder_create();
  492. }
  493. return texture;
  494. }
  495. Vector<Ref<Image>> CompressedTexture3D::get_data() const {
  496. if (texture.is_valid()) {
  497. return RS::get_singleton()->texture_3d_get(texture);
  498. } else {
  499. return Vector<Ref<Image>>();
  500. }
  501. }
  502. void CompressedTexture3D::reload_from_file() {
  503. String path = get_path();
  504. if (!path.is_resource_file()) {
  505. return;
  506. }
  507. path = ResourceLoader::path_remap(path); //remap for translation
  508. path = ResourceLoader::import_remap(path); //remap for import
  509. if (!path.is_resource_file()) {
  510. return;
  511. }
  512. load(path);
  513. }
  514. void CompressedTexture3D::_bind_methods() {
  515. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture3D::load);
  516. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture3D::get_load_path);
  517. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  518. }
  519. CompressedTexture3D::~CompressedTexture3D() {
  520. if (texture.is_valid()) {
  521. ERR_FAIL_NULL(RenderingServer::get_singleton());
  522. RS::get_singleton()->free_rid(texture);
  523. }
  524. }
  525. Ref<Resource> ResourceFormatLoaderCompressedTexture3D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  526. Ref<CompressedTexture3D> st;
  527. st.instantiate();
  528. Error err = st->load(p_path);
  529. if (r_error) {
  530. *r_error = err;
  531. }
  532. if (err != OK) {
  533. return Ref<Resource>();
  534. }
  535. return st;
  536. }
  537. void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List<String> *p_extensions) const {
  538. p_extensions->push_back("ctex3d");
  539. }
  540. bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const {
  541. return p_type == "CompressedTexture3D";
  542. }
  543. String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const {
  544. if (p_path.has_extension("ctex3d")) {
  545. return "CompressedTexture3D";
  546. }
  547. return "";
  548. }
  549. void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) {
  550. if (texture.is_valid()) {
  551. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  552. }
  553. Resource::set_path(p_path, p_take_over);
  554. }
  555. Image::Format CompressedTextureLayered::get_format() const {
  556. return format;
  557. }
  558. Error CompressedTextureLayered::_load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit) {
  559. ERR_FAIL_COND_V(images.size() != 0, ERR_INVALID_PARAMETER);
  560. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  561. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  562. uint8_t header[4];
  563. f->get_buffer(header, 4);
  564. if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L') {
  565. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture layered file is corrupt (Bad header).");
  566. }
  567. uint32_t version = f->get_32();
  568. if (version > FORMAT_VERSION) {
  569. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  570. }
  571. uint32_t layer_count = f->get_32(); //layer count
  572. uint32_t type = f->get_32(); //layer count
  573. ERR_FAIL_COND_V((int)type != layered_type, ERR_INVALID_DATA);
  574. uint32_t df = f->get_32(); //data format
  575. mipmap_limit = int(f->get_32());
  576. //reserved
  577. f->get_32();
  578. f->get_32();
  579. f->get_32();
  580. if (!(df & FORMAT_BIT_STREAM)) {
  581. p_size_limit = 0;
  582. }
  583. images.resize(layer_count);
  584. for (uint32_t i = 0; i < layer_count; i++) {
  585. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, p_size_limit);
  586. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  587. images.write[i] = image;
  588. }
  589. return OK;
  590. }
  591. Error CompressedTextureLayered::load(const String &p_path) {
  592. Vector<Ref<Image>> images;
  593. int mipmap_limit;
  594. Error err = _load_data(p_path, images, mipmap_limit);
  595. if (err) {
  596. return err;
  597. }
  598. if (texture.is_valid()) {
  599. RID new_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  600. RS::get_singleton()->texture_replace(texture, new_texture);
  601. } else {
  602. texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  603. }
  604. w = images[0]->get_width();
  605. h = images[0]->get_height();
  606. mipmaps = images[0]->has_mipmaps();
  607. format = images[0]->get_format();
  608. layers = images.size();
  609. path_to_file = p_path;
  610. if (get_path().is_empty()) {
  611. //temporarily set path if no path set for resource, helps find errors
  612. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  613. }
  614. notify_property_list_changed();
  615. emit_changed();
  616. return OK;
  617. }
  618. String CompressedTextureLayered::get_load_path() const {
  619. return path_to_file;
  620. }
  621. int CompressedTextureLayered::get_width() const {
  622. return w;
  623. }
  624. int CompressedTextureLayered::get_height() const {
  625. return h;
  626. }
  627. int CompressedTextureLayered::get_layers() const {
  628. return layers;
  629. }
  630. bool CompressedTextureLayered::has_mipmaps() const {
  631. return mipmaps;
  632. }
  633. TextureLayered::LayeredType CompressedTextureLayered::get_layered_type() const {
  634. return layered_type;
  635. }
  636. RID CompressedTextureLayered::get_rid() const {
  637. if (!texture.is_valid()) {
  638. texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
  639. }
  640. return texture;
  641. }
  642. Ref<Image> CompressedTextureLayered::get_layer_data(int p_layer) const {
  643. if (texture.is_valid()) {
  644. ERR_FAIL_INDEX_V(p_layer, get_layers(), Ref<Image>());
  645. return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
  646. } else {
  647. return Ref<Image>();
  648. }
  649. }
  650. void CompressedTextureLayered::reload_from_file() {
  651. String path = get_path();
  652. if (!path.is_resource_file()) {
  653. return;
  654. }
  655. path = ResourceLoader::path_remap(path); //remap for translation
  656. path = ResourceLoader::import_remap(path); //remap for import
  657. if (!path.is_resource_file()) {
  658. return;
  659. }
  660. load(path);
  661. }
  662. void CompressedTextureLayered::_bind_methods() {
  663. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTextureLayered::load);
  664. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTextureLayered::get_load_path);
  665. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  666. }
  667. CompressedTextureLayered::CompressedTextureLayered(LayeredType p_type) {
  668. layered_type = p_type;
  669. }
  670. CompressedTextureLayered::~CompressedTextureLayered() {
  671. if (texture.is_valid()) {
  672. ERR_FAIL_NULL(RenderingServer::get_singleton());
  673. RS::get_singleton()->free_rid(texture);
  674. }
  675. }
  676. /////////////////////////////////////////////////
  677. Ref<Resource> ResourceFormatLoaderCompressedTextureLayered::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  678. Ref<CompressedTextureLayered> ct;
  679. if (p_path.has_extension("ctexarray")) {
  680. Ref<CompressedTexture2DArray> c;
  681. c.instantiate();
  682. ct = c;
  683. } else if (p_path.has_extension("ccube")) {
  684. Ref<CompressedCubemap> c;
  685. c.instantiate();
  686. ct = c;
  687. } else if (p_path.has_extension("ccubearray")) {
  688. Ref<CompressedCubemapArray> c;
  689. c.instantiate();
  690. ct = c;
  691. } else {
  692. if (r_error) {
  693. *r_error = ERR_FILE_UNRECOGNIZED;
  694. }
  695. return Ref<Resource>();
  696. }
  697. Error err = ct->load(p_path);
  698. if (r_error) {
  699. *r_error = err;
  700. }
  701. if (err != OK) {
  702. return Ref<Resource>();
  703. }
  704. return ct;
  705. }
  706. void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List<String> *p_extensions) const {
  707. p_extensions->push_back("ctexarray");
  708. p_extensions->push_back("ccube");
  709. p_extensions->push_back("ccubearray");
  710. }
  711. bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const {
  712. return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray";
  713. }
  714. String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const {
  715. if (p_path.has_extension("ctexarray")) {
  716. return "CompressedTexture2DArray";
  717. }
  718. if (p_path.has_extension("ccube")) {
  719. return "CompressedCubemap";
  720. }
  721. if (p_path.has_extension("ccubearray")) {
  722. return "CompressedCubemapArray";
  723. }
  724. return "";
  725. }