compressed_texture.cpp 26 KB

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