compressed_texture.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  255. uint32_t size = f->get_32();
  256. if (p_size_limit > 0 && i < (mipmaps - 1) && (sw > p_size_limit || sh > p_size_limit)) {
  257. //can't load this due to size limit
  258. sw = MAX(sw >> 1, 1);
  259. sh = MAX(sh >> 1, 1);
  260. f->seek(f->get_position() + size);
  261. continue;
  262. }
  263. Vector<uint8_t> pv;
  264. pv.resize(size);
  265. {
  266. uint8_t *wr = pv.ptrw();
  267. f->get_buffer(wr, size);
  268. }
  269. Ref<Image> img;
  270. if (data_format == DATA_FORMAT_PNG && Image::png_unpacker) {
  271. img = Image::png_unpacker(pv);
  272. } else if (data_format == DATA_FORMAT_WEBP && Image::webp_unpacker) {
  273. img = Image::webp_unpacker(pv);
  274. }
  275. if (img.is_null() || img->is_empty()) {
  276. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  277. }
  278. // If the image is compressed and its format doesn't match the desired format, return an empty reference.
  279. // This is done to avoid recompressing the image on load.
  280. ERR_FAIL_COND_V(img->is_compressed() && format != img->get_format(), Ref<Image>());
  281. // The format will actually be the format of the header,
  282. // as it may have changed on compression.
  283. if (format != img->get_format()) {
  284. // Convert the image to the desired format.
  285. // Note: We are not decompressing the image here, just changing its format.
  286. // It's important that all images in the texture array share the same format for correct rendering.
  287. img->convert(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. if (texture.is_valid()) {
  375. ERR_FAIL_NULL(RenderingServer::get_singleton());
  376. RS::get_singleton()->free(texture);
  377. }
  378. }
  379. 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) {
  380. Ref<CompressedTexture2D> st;
  381. st.instantiate();
  382. Error err = st->load(p_path);
  383. if (r_error) {
  384. *r_error = err;
  385. }
  386. if (err != OK) {
  387. return Ref<Resource>();
  388. }
  389. return st;
  390. }
  391. void ResourceFormatLoaderCompressedTexture2D::get_recognized_extensions(List<String> *p_extensions) const {
  392. p_extensions->push_back("ctex");
  393. }
  394. bool ResourceFormatLoaderCompressedTexture2D::handles_type(const String &p_type) const {
  395. return p_type == "CompressedTexture2D";
  396. }
  397. String ResourceFormatLoaderCompressedTexture2D::get_resource_type(const String &p_path) const {
  398. if (p_path.get_extension().to_lower() == "ctex") {
  399. return "CompressedTexture2D";
  400. }
  401. return "";
  402. }
  403. void CompressedTexture3D::set_path(const String &p_path, bool p_take_over) {
  404. if (texture.is_valid()) {
  405. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  406. }
  407. Resource::set_path(p_path, p_take_over);
  408. }
  409. Image::Format CompressedTexture3D::get_format() const {
  410. return format;
  411. }
  412. 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) {
  413. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  414. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  415. uint8_t header[4];
  416. f->get_buffer(header, 4);
  417. ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L', ERR_FILE_UNRECOGNIZED);
  418. //stored as compressed textures (used for lossless and lossy compression)
  419. uint32_t version = f->get_32();
  420. if (version > FORMAT_VERSION) {
  421. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  422. }
  423. r_depth = f->get_32(); //depth
  424. f->get_32(); //ignored (mode)
  425. f->get_32(); // ignored (data format)
  426. f->get_32(); //ignored
  427. int mipmap_count = f->get_32();
  428. f->get_32(); //ignored
  429. f->get_32(); //ignored
  430. r_mipmaps = mipmap_count != 0;
  431. r_data.clear();
  432. for (int i = 0; i < (r_depth + mipmap_count); i++) {
  433. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, 0);
  434. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  435. if (i == 0) {
  436. r_format = image->get_format();
  437. r_width = image->get_width();
  438. r_height = image->get_height();
  439. }
  440. r_data.push_back(image);
  441. }
  442. return OK;
  443. }
  444. Error CompressedTexture3D::load(const String &p_path) {
  445. Vector<Ref<Image>> data;
  446. int tw, th, td;
  447. Image::Format tfmt;
  448. bool tmm;
  449. Error err = _load_data(p_path, data, tfmt, tw, th, td, tmm);
  450. if (err) {
  451. return err;
  452. }
  453. if (texture.is_valid()) {
  454. RID new_texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
  455. RS::get_singleton()->texture_replace(texture, new_texture);
  456. } else {
  457. texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
  458. }
  459. w = tw;
  460. h = th;
  461. d = td;
  462. mipmaps = tmm;
  463. format = tfmt;
  464. path_to_file = p_path;
  465. if (get_path().is_empty()) {
  466. //temporarily set path if no path set for resource, helps find errors
  467. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  468. }
  469. notify_property_list_changed();
  470. emit_changed();
  471. return OK;
  472. }
  473. String CompressedTexture3D::get_load_path() const {
  474. return path_to_file;
  475. }
  476. int CompressedTexture3D::get_width() const {
  477. return w;
  478. }
  479. int CompressedTexture3D::get_height() const {
  480. return h;
  481. }
  482. int CompressedTexture3D::get_depth() const {
  483. return d;
  484. }
  485. bool CompressedTexture3D::has_mipmaps() const {
  486. return mipmaps;
  487. }
  488. RID CompressedTexture3D::get_rid() const {
  489. if (!texture.is_valid()) {
  490. texture = RS::get_singleton()->texture_3d_placeholder_create();
  491. }
  492. return texture;
  493. }
  494. Vector<Ref<Image>> CompressedTexture3D::get_data() const {
  495. if (texture.is_valid()) {
  496. return RS::get_singleton()->texture_3d_get(texture);
  497. } else {
  498. return Vector<Ref<Image>>();
  499. }
  500. }
  501. void CompressedTexture3D::reload_from_file() {
  502. String path = get_path();
  503. if (!path.is_resource_file()) {
  504. return;
  505. }
  506. path = ResourceLoader::path_remap(path); //remap for translation
  507. path = ResourceLoader::import_remap(path); //remap for import
  508. if (!path.is_resource_file()) {
  509. return;
  510. }
  511. load(path);
  512. }
  513. void CompressedTexture3D::_bind_methods() {
  514. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture3D::load);
  515. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture3D::get_load_path);
  516. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  517. }
  518. CompressedTexture3D::~CompressedTexture3D() {
  519. if (texture.is_valid()) {
  520. ERR_FAIL_NULL(RenderingServer::get_singleton());
  521. RS::get_singleton()->free(texture);
  522. }
  523. }
  524. 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) {
  525. Ref<CompressedTexture3D> st;
  526. st.instantiate();
  527. Error err = st->load(p_path);
  528. if (r_error) {
  529. *r_error = err;
  530. }
  531. if (err != OK) {
  532. return Ref<Resource>();
  533. }
  534. return st;
  535. }
  536. void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List<String> *p_extensions) const {
  537. p_extensions->push_back("ctex3d");
  538. }
  539. bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const {
  540. return p_type == "CompressedTexture3D";
  541. }
  542. String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const {
  543. if (p_path.get_extension().to_lower() == "ctex3d") {
  544. return "CompressedTexture3D";
  545. }
  546. return "";
  547. }
  548. void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) {
  549. if (texture.is_valid()) {
  550. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  551. }
  552. Resource::set_path(p_path, p_take_over);
  553. }
  554. Image::Format CompressedTextureLayered::get_format() const {
  555. return format;
  556. }
  557. Error CompressedTextureLayered::_load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit) {
  558. ERR_FAIL_COND_V(images.size() != 0, ERR_INVALID_PARAMETER);
  559. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  560. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  561. uint8_t header[4];
  562. f->get_buffer(header, 4);
  563. if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L') {
  564. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture layered file is corrupt (Bad header).");
  565. }
  566. uint32_t version = f->get_32();
  567. if (version > FORMAT_VERSION) {
  568. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  569. }
  570. uint32_t layer_count = f->get_32(); //layer count
  571. uint32_t type = f->get_32(); //layer count
  572. ERR_FAIL_COND_V((int)type != layered_type, ERR_INVALID_DATA);
  573. uint32_t df = f->get_32(); //data format
  574. mipmap_limit = int(f->get_32());
  575. //reserved
  576. f->get_32();
  577. f->get_32();
  578. f->get_32();
  579. if (!(df & FORMAT_BIT_STREAM)) {
  580. p_size_limit = 0;
  581. }
  582. images.resize(layer_count);
  583. for (uint32_t i = 0; i < layer_count; i++) {
  584. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, p_size_limit);
  585. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  586. images.write[i] = image;
  587. }
  588. return OK;
  589. }
  590. Error CompressedTextureLayered::load(const String &p_path) {
  591. Vector<Ref<Image>> images;
  592. int mipmap_limit;
  593. Error err = _load_data(p_path, images, mipmap_limit);
  594. if (err) {
  595. return err;
  596. }
  597. if (texture.is_valid()) {
  598. RID new_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  599. RS::get_singleton()->texture_replace(texture, new_texture);
  600. } else {
  601. texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  602. }
  603. w = images[0]->get_width();
  604. h = images[0]->get_height();
  605. mipmaps = images[0]->has_mipmaps();
  606. format = images[0]->get_format();
  607. layers = images.size();
  608. path_to_file = p_path;
  609. if (get_path().is_empty()) {
  610. //temporarily set path if no path set for resource, helps find errors
  611. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  612. }
  613. notify_property_list_changed();
  614. emit_changed();
  615. return OK;
  616. }
  617. String CompressedTextureLayered::get_load_path() const {
  618. return path_to_file;
  619. }
  620. int CompressedTextureLayered::get_width() const {
  621. return w;
  622. }
  623. int CompressedTextureLayered::get_height() const {
  624. return h;
  625. }
  626. int CompressedTextureLayered::get_layers() const {
  627. return layers;
  628. }
  629. bool CompressedTextureLayered::has_mipmaps() const {
  630. return mipmaps;
  631. }
  632. TextureLayered::LayeredType CompressedTextureLayered::get_layered_type() const {
  633. return layered_type;
  634. }
  635. RID CompressedTextureLayered::get_rid() const {
  636. if (!texture.is_valid()) {
  637. texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
  638. }
  639. return texture;
  640. }
  641. Ref<Image> CompressedTextureLayered::get_layer_data(int p_layer) const {
  642. if (texture.is_valid()) {
  643. ERR_FAIL_INDEX_V(p_layer, get_layers(), Ref<Image>());
  644. return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
  645. } else {
  646. return Ref<Image>();
  647. }
  648. }
  649. void CompressedTextureLayered::reload_from_file() {
  650. String path = get_path();
  651. if (!path.is_resource_file()) {
  652. return;
  653. }
  654. path = ResourceLoader::path_remap(path); //remap for translation
  655. path = ResourceLoader::import_remap(path); //remap for import
  656. if (!path.is_resource_file()) {
  657. return;
  658. }
  659. load(path);
  660. }
  661. void CompressedTextureLayered::_bind_methods() {
  662. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTextureLayered::load);
  663. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTextureLayered::get_load_path);
  664. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  665. }
  666. CompressedTextureLayered::CompressedTextureLayered(LayeredType p_type) {
  667. layered_type = p_type;
  668. }
  669. CompressedTextureLayered::~CompressedTextureLayered() {
  670. if (texture.is_valid()) {
  671. ERR_FAIL_NULL(RenderingServer::get_singleton());
  672. RS::get_singleton()->free(texture);
  673. }
  674. }
  675. /////////////////////////////////////////////////
  676. 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) {
  677. Ref<CompressedTextureLayered> ct;
  678. if (p_path.get_extension().to_lower() == "ctexarray") {
  679. Ref<CompressedTexture2DArray> c;
  680. c.instantiate();
  681. ct = c;
  682. } else if (p_path.get_extension().to_lower() == "ccube") {
  683. Ref<CompressedCubemap> c;
  684. c.instantiate();
  685. ct = c;
  686. } else if (p_path.get_extension().to_lower() == "ccubearray") {
  687. Ref<CompressedCubemapArray> c;
  688. c.instantiate();
  689. ct = c;
  690. } else {
  691. if (r_error) {
  692. *r_error = ERR_FILE_UNRECOGNIZED;
  693. }
  694. return Ref<Resource>();
  695. }
  696. Error err = ct->load(p_path);
  697. if (r_error) {
  698. *r_error = err;
  699. }
  700. if (err != OK) {
  701. return Ref<Resource>();
  702. }
  703. return ct;
  704. }
  705. void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List<String> *p_extensions) const {
  706. p_extensions->push_back("ctexarray");
  707. p_extensions->push_back("ccube");
  708. p_extensions->push_back("ccubearray");
  709. }
  710. bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const {
  711. return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray";
  712. }
  713. String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const {
  714. if (p_path.get_extension().to_lower() == "ctexarray") {
  715. return "CompressedTexture2DArray";
  716. }
  717. if (p_path.get_extension().to_lower() == "ccube") {
  718. return "CompressedCubemap";
  719. }
  720. if (p_path.get_extension().to_lower() == "ccubearray") {
  721. return "CompressedCubemapArray";
  722. }
  723. return "";
  724. }