compressed_texture.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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_valid()) {
  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);
  224. y = CLAMP(y, 0, ah);
  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. void CompressedTexture2D::_validate_property(PropertyInfo &p_property) const {
  242. }
  243. Ref<Image> CompressedTexture2D::load_image_from_file(Ref<FileAccess> f, int p_size_limit) {
  244. uint32_t data_format = f->get_32();
  245. uint32_t w = f->get_16();
  246. uint32_t h = f->get_16();
  247. uint32_t mipmaps = f->get_32();
  248. Image::Format format = Image::Format(f->get_32());
  249. if (data_format == DATA_FORMAT_PNG || data_format == DATA_FORMAT_WEBP) {
  250. //look for a PNG or WebP file inside
  251. int sw = w;
  252. int sh = h;
  253. //mipmaps need to be read independently, they will be later combined
  254. Vector<Ref<Image>> mipmap_images;
  255. uint64_t total_size = 0;
  256. bool first = true;
  257. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  258. uint32_t size = f->get_32();
  259. if (p_size_limit > 0 && i < (mipmaps - 1) && (sw > p_size_limit || sh > p_size_limit)) {
  260. //can't load this due to size limit
  261. sw = MAX(sw >> 1, 1);
  262. sh = MAX(sh >> 1, 1);
  263. f->seek(f->get_position() + size);
  264. continue;
  265. }
  266. Vector<uint8_t> pv;
  267. pv.resize(size);
  268. {
  269. uint8_t *wr = pv.ptrw();
  270. f->get_buffer(wr, size);
  271. }
  272. Ref<Image> img;
  273. if (data_format == DATA_FORMAT_PNG && Image::png_unpacker) {
  274. img = Image::png_unpacker(pv);
  275. } else if (data_format == DATA_FORMAT_WEBP && Image::webp_unpacker) {
  276. img = Image::webp_unpacker(pv);
  277. }
  278. if (img.is_null() || img->is_empty()) {
  279. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  280. }
  281. if (first) {
  282. //format will actually be the format of the first image,
  283. //as it may have changed on compression
  284. format = img->get_format();
  285. first = false;
  286. } else if (img->get_format() != format) {
  287. img->convert(format); //all needs to be the same format
  288. }
  289. total_size += img->get_data().size();
  290. mipmap_images.push_back(img);
  291. sw = MAX(sw >> 1, 1);
  292. sh = MAX(sh >> 1, 1);
  293. }
  294. //print_line("mipmap read total: " + itos(mipmap_images.size()));
  295. Ref<Image> image;
  296. image.instantiate();
  297. if (mipmap_images.size() == 1) {
  298. //only one image (which will most likely be the case anyway for this format)
  299. image = mipmap_images[0];
  300. return image;
  301. } else {
  302. //rarer use case, but needs to be supported
  303. Vector<uint8_t> img_data;
  304. img_data.resize(total_size);
  305. {
  306. uint8_t *wr = img_data.ptrw();
  307. int ofs = 0;
  308. for (int i = 0; i < mipmap_images.size(); i++) {
  309. Vector<uint8_t> id = mipmap_images[i]->get_data();
  310. int len = id.size();
  311. const uint8_t *r = id.ptr();
  312. memcpy(&wr[ofs], r, len);
  313. ofs += len;
  314. }
  315. }
  316. image->set_data(w, h, true, mipmap_images[0]->get_format(), img_data);
  317. return image;
  318. }
  319. } else if (data_format == DATA_FORMAT_BASIS_UNIVERSAL) {
  320. int sw = w;
  321. int sh = h;
  322. uint32_t size = f->get_32();
  323. if (p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  324. //can't load this due to size limit
  325. sw = MAX(sw >> 1, 1);
  326. sh = MAX(sh >> 1, 1);
  327. f->seek(f->get_position() + size);
  328. return Ref<Image>();
  329. }
  330. Vector<uint8_t> pv;
  331. pv.resize(size);
  332. {
  333. uint8_t *wr = pv.ptrw();
  334. f->get_buffer(wr, size);
  335. }
  336. Ref<Image> img;
  337. img = Image::basis_universal_unpacker(pv);
  338. if (img.is_null() || img->is_empty()) {
  339. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  340. }
  341. format = img->get_format();
  342. sw = MAX(sw >> 1, 1);
  343. sh = MAX(sh >> 1, 1);
  344. return img;
  345. } else if (data_format == DATA_FORMAT_IMAGE) {
  346. int size = Image::get_image_data_size(w, h, format, mipmaps ? true : false);
  347. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  348. int tw, th;
  349. int ofs = Image::get_image_mipmap_offset_and_dimensions(w, h, format, i, tw, th);
  350. if (p_size_limit > 0 && i < mipmaps && (p_size_limit > tw || p_size_limit > th)) {
  351. if (ofs) {
  352. f->seek(f->get_position() + ofs);
  353. }
  354. continue; //oops, size limit enforced, go to next
  355. }
  356. Vector<uint8_t> data;
  357. data.resize(size - ofs);
  358. {
  359. uint8_t *wr = data.ptrw();
  360. f->get_buffer(wr, data.size());
  361. }
  362. Ref<Image> image = Image::create_from_data(tw, th, mipmaps - i ? true : false, format, data);
  363. return image;
  364. }
  365. }
  366. return Ref<Image>();
  367. }
  368. void CompressedTexture2D::_bind_methods() {
  369. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture2D::load);
  370. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture2D::get_load_path);
  371. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  372. }
  373. CompressedTexture2D::CompressedTexture2D() {}
  374. CompressedTexture2D::~CompressedTexture2D() {
  375. if (texture.is_valid()) {
  376. ERR_FAIL_NULL(RenderingServer::get_singleton());
  377. RS::get_singleton()->free(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.get_extension().to_lower() == "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::_validate_property(PropertyInfo &p_property) const {
  515. }
  516. void CompressedTexture3D::_bind_methods() {
  517. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture3D::load);
  518. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture3D::get_load_path);
  519. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  520. }
  521. CompressedTexture3D::CompressedTexture3D() {}
  522. CompressedTexture3D::~CompressedTexture3D() {
  523. if (texture.is_valid()) {
  524. ERR_FAIL_NULL(RenderingServer::get_singleton());
  525. RS::get_singleton()->free(texture);
  526. }
  527. }
  528. 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) {
  529. Ref<CompressedTexture3D> st;
  530. st.instantiate();
  531. Error err = st->load(p_path);
  532. if (r_error) {
  533. *r_error = err;
  534. }
  535. if (err != OK) {
  536. return Ref<Resource>();
  537. }
  538. return st;
  539. }
  540. void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List<String> *p_extensions) const {
  541. p_extensions->push_back("ctex3d");
  542. }
  543. bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const {
  544. return p_type == "CompressedTexture3D";
  545. }
  546. String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const {
  547. if (p_path.get_extension().to_lower() == "ctex3d") {
  548. return "CompressedTexture3D";
  549. }
  550. return "";
  551. }
  552. void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) {
  553. if (texture.is_valid()) {
  554. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  555. }
  556. Resource::set_path(p_path, p_take_over);
  557. }
  558. Image::Format CompressedTextureLayered::get_format() const {
  559. return format;
  560. }
  561. Error CompressedTextureLayered::_load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit) {
  562. ERR_FAIL_COND_V(images.size() != 0, ERR_INVALID_PARAMETER);
  563. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  564. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  565. uint8_t header[4];
  566. f->get_buffer(header, 4);
  567. if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L') {
  568. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture layered file is corrupt (Bad header).");
  569. }
  570. uint32_t version = f->get_32();
  571. if (version > FORMAT_VERSION) {
  572. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  573. }
  574. uint32_t layer_count = f->get_32(); //layer count
  575. uint32_t type = f->get_32(); //layer count
  576. ERR_FAIL_COND_V((int)type != layered_type, ERR_INVALID_DATA);
  577. uint32_t df = f->get_32(); //data format
  578. mipmap_limit = int(f->get_32());
  579. //reserved
  580. f->get_32();
  581. f->get_32();
  582. f->get_32();
  583. if (!(df & FORMAT_BIT_STREAM)) {
  584. p_size_limit = 0;
  585. }
  586. images.resize(layer_count);
  587. for (uint32_t i = 0; i < layer_count; i++) {
  588. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, p_size_limit);
  589. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  590. images.write[i] = image;
  591. }
  592. return OK;
  593. }
  594. Error CompressedTextureLayered::load(const String &p_path) {
  595. Vector<Ref<Image>> images;
  596. int mipmap_limit;
  597. Error err = _load_data(p_path, images, mipmap_limit);
  598. if (err) {
  599. return err;
  600. }
  601. if (texture.is_valid()) {
  602. RID new_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  603. RS::get_singleton()->texture_replace(texture, new_texture);
  604. } else {
  605. texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  606. }
  607. w = images[0]->get_width();
  608. h = images[0]->get_height();
  609. mipmaps = images[0]->has_mipmaps();
  610. format = images[0]->get_format();
  611. layers = images.size();
  612. path_to_file = p_path;
  613. if (get_path().is_empty()) {
  614. //temporarily set path if no path set for resource, helps find errors
  615. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  616. }
  617. notify_property_list_changed();
  618. emit_changed();
  619. return OK;
  620. }
  621. String CompressedTextureLayered::get_load_path() const {
  622. return path_to_file;
  623. }
  624. int CompressedTextureLayered::get_width() const {
  625. return w;
  626. }
  627. int CompressedTextureLayered::get_height() const {
  628. return h;
  629. }
  630. int CompressedTextureLayered::get_layers() const {
  631. return layers;
  632. }
  633. bool CompressedTextureLayered::has_mipmaps() const {
  634. return mipmaps;
  635. }
  636. TextureLayered::LayeredType CompressedTextureLayered::get_layered_type() const {
  637. return layered_type;
  638. }
  639. RID CompressedTextureLayered::get_rid() const {
  640. if (!texture.is_valid()) {
  641. texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
  642. }
  643. return texture;
  644. }
  645. Ref<Image> CompressedTextureLayered::get_layer_data(int p_layer) const {
  646. if (texture.is_valid()) {
  647. ERR_FAIL_INDEX_V(p_layer, get_layers(), Ref<Image>());
  648. return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
  649. } else {
  650. return Ref<Image>();
  651. }
  652. }
  653. void CompressedTextureLayered::reload_from_file() {
  654. String path = get_path();
  655. if (!path.is_resource_file()) {
  656. return;
  657. }
  658. path = ResourceLoader::path_remap(path); //remap for translation
  659. path = ResourceLoader::import_remap(path); //remap for import
  660. if (!path.is_resource_file()) {
  661. return;
  662. }
  663. load(path);
  664. }
  665. void CompressedTextureLayered::_validate_property(PropertyInfo &p_property) const {
  666. }
  667. void CompressedTextureLayered::_bind_methods() {
  668. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTextureLayered::load);
  669. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTextureLayered::get_load_path);
  670. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  671. }
  672. CompressedTextureLayered::CompressedTextureLayered(LayeredType p_type) {
  673. layered_type = p_type;
  674. }
  675. CompressedTextureLayered::~CompressedTextureLayered() {
  676. if (texture.is_valid()) {
  677. ERR_FAIL_NULL(RenderingServer::get_singleton());
  678. RS::get_singleton()->free(texture);
  679. }
  680. }
  681. /////////////////////////////////////////////////
  682. 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) {
  683. Ref<CompressedTextureLayered> ct;
  684. if (p_path.get_extension().to_lower() == "ctexarray") {
  685. Ref<CompressedTexture2DArray> c;
  686. c.instantiate();
  687. ct = c;
  688. } else if (p_path.get_extension().to_lower() == "ccube") {
  689. Ref<CompressedCubemap> c;
  690. c.instantiate();
  691. ct = c;
  692. } else if (p_path.get_extension().to_lower() == "ccubearray") {
  693. Ref<CompressedCubemapArray> c;
  694. c.instantiate();
  695. ct = c;
  696. } else {
  697. if (r_error) {
  698. *r_error = ERR_FILE_UNRECOGNIZED;
  699. }
  700. return Ref<Resource>();
  701. }
  702. Error err = ct->load(p_path);
  703. if (r_error) {
  704. *r_error = err;
  705. }
  706. if (err != OK) {
  707. return Ref<Resource>();
  708. }
  709. return ct;
  710. }
  711. void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List<String> *p_extensions) const {
  712. p_extensions->push_back("ctexarray");
  713. p_extensions->push_back("ccube");
  714. p_extensions->push_back("ccubearray");
  715. }
  716. bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const {
  717. return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray";
  718. }
  719. String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const {
  720. if (p_path.get_extension().to_lower() == "ctexarray") {
  721. return "CompressedTexture2DArray";
  722. }
  723. if (p_path.get_extension().to_lower() == "ccube") {
  724. return "CompressedCubemap";
  725. }
  726. if (p_path.get_extension().to_lower() == "ccubearray") {
  727. return "CompressedCubemapArray";
  728. }
  729. return "";
  730. }