texture_loader_ktx.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /**************************************************************************/
  2. /* texture_loader_ktx.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 "texture_loader_ktx.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/file_access_memory.h"
  33. #include "scene/resources/image_texture.h"
  34. #include <ktx.h>
  35. #include <vk_format.h>
  36. KTX_error_code ktx_read(ktxStream *stream, void *dst, const ktx_size_t count) {
  37. Ref<FileAccess> *f = reinterpret_cast<Ref<FileAccess> *>(stream->data.custom_ptr.address);
  38. (*f)->get_buffer(reinterpret_cast<uint8_t *>(dst), count);
  39. return KTX_SUCCESS;
  40. }
  41. KTX_error_code ktx_skip(ktxStream *stream, const ktx_size_t count) {
  42. Ref<FileAccess> *f = reinterpret_cast<Ref<FileAccess> *>(stream->data.custom_ptr.address);
  43. for (ktx_size_t i = 0; i < count; ++i) {
  44. (*f)->get_8();
  45. }
  46. return KTX_SUCCESS;
  47. }
  48. KTX_error_code ktx_write(ktxStream *stream, const void *src, const ktx_size_t size, const ktx_size_t count) {
  49. Ref<FileAccess> *f = reinterpret_cast<Ref<FileAccess> *>(stream->data.custom_ptr.address);
  50. (*f)->store_buffer(reinterpret_cast<const uint8_t *>(src), size * count);
  51. return KTX_SUCCESS;
  52. }
  53. KTX_error_code ktx_getpos(ktxStream *stream, ktx_off_t *const offset) {
  54. Ref<FileAccess> *f = reinterpret_cast<Ref<FileAccess> *>(stream->data.custom_ptr.address);
  55. *offset = (*f)->get_position();
  56. return KTX_SUCCESS;
  57. }
  58. KTX_error_code ktx_setpos(ktxStream *stream, const ktx_off_t offset) {
  59. Ref<FileAccess> *f = reinterpret_cast<Ref<FileAccess> *>(stream->data.custom_ptr.address);
  60. (*f)->seek(offset);
  61. return KTX_SUCCESS;
  62. }
  63. KTX_error_code ktx_getsize(ktxStream *stream, ktx_size_t *const size) {
  64. Ref<FileAccess> *f = reinterpret_cast<Ref<FileAccess> *>(stream->data.custom_ptr.address);
  65. *size = (*f)->get_length();
  66. return KTX_SUCCESS;
  67. }
  68. void ktx_destruct(ktxStream *stream) {
  69. (void)stream;
  70. }
  71. static Ref<Image> load_from_file_access(Ref<FileAccess> f, Error *r_error) {
  72. ktxStream ktx_stream;
  73. ktx_stream.read = ktx_read;
  74. ktx_stream.skip = ktx_skip;
  75. ktx_stream.write = ktx_write;
  76. ktx_stream.getpos = ktx_getpos;
  77. ktx_stream.setpos = ktx_setpos;
  78. ktx_stream.getsize = ktx_getsize;
  79. ktx_stream.destruct = ktx_destruct;
  80. ktx_stream.type = eStreamTypeCustom;
  81. ktx_stream.data.custom_ptr.address = &f;
  82. ktx_stream.data.custom_ptr.allocatorAddress = NULL;
  83. ktx_stream.data.custom_ptr.size = 0;
  84. ktx_stream.readpos = 0;
  85. ktx_stream.closeOnDestruct = false;
  86. ktxTexture *ktx_texture;
  87. KTX_error_code result = ktxTexture_CreateFromStream(&ktx_stream,
  88. KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
  89. &ktx_texture);
  90. if (result != KTX_SUCCESS) {
  91. ERR_FAIL_V_MSG(Ref<Resource>(), "Invalid or unsupported KTX texture file.");
  92. }
  93. if (ktx_texture->numDimensions != 2) {
  94. ktxTexture_Destroy(ktx_texture);
  95. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported non-2D KTX texture file.");
  96. }
  97. if (ktx_texture->isCubemap || ktx_texture->numFaces != 1) {
  98. ktxTexture_Destroy(ktx_texture);
  99. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported cube map KTX texture file.");
  100. }
  101. if (ktx_texture->isArray) {
  102. ktxTexture_Destroy(ktx_texture);
  103. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported array KTX texture file.");
  104. }
  105. uint32_t width = ktx_texture->baseWidth;
  106. uint32_t height = ktx_texture->baseHeight;
  107. uint32_t mipmaps = ktx_texture->numLevels;
  108. Image::Format format;
  109. bool srgb = false;
  110. switch (ktx_texture->classId) {
  111. case ktxTexture1_c:
  112. switch (((ktxTexture1 *)ktx_texture)->glInternalformat) {
  113. case GL_LUMINANCE:
  114. format = Image::FORMAT_L8;
  115. break;
  116. case GL_LUMINANCE_ALPHA:
  117. format = Image::FORMAT_LA8;
  118. break;
  119. case GL_SRGB8:
  120. format = Image::FORMAT_RGB8;
  121. srgb = true;
  122. break;
  123. case GL_SRGB8_ALPHA8:
  124. format = Image::FORMAT_RGBA8;
  125. srgb = true;
  126. break;
  127. case GL_R8:
  128. case GL_R8UI:
  129. format = Image::FORMAT_R8;
  130. break;
  131. case GL_RG8:
  132. format = Image::FORMAT_RG8;
  133. break;
  134. case GL_RGB8:
  135. format = Image::FORMAT_RGB8;
  136. break;
  137. case GL_RGBA8:
  138. format = Image::FORMAT_RGBA8;
  139. break;
  140. case GL_RGBA4:
  141. format = Image::FORMAT_RGBA4444;
  142. break;
  143. case GL_RGB565:
  144. format = Image::FORMAT_RGB565;
  145. break;
  146. case GL_R32F:
  147. format = Image::FORMAT_RF;
  148. break;
  149. case GL_RG32F:
  150. format = Image::FORMAT_RGF;
  151. break;
  152. case GL_RGB32F:
  153. format = Image::FORMAT_RGBF;
  154. break;
  155. case GL_RGBA32F:
  156. format = Image::FORMAT_RGBAF;
  157. break;
  158. case GL_R16F:
  159. format = Image::FORMAT_RH;
  160. break;
  161. case GL_RG16F:
  162. format = Image::FORMAT_RGH;
  163. break;
  164. case GL_RGB16F:
  165. format = Image::FORMAT_RGBH;
  166. break;
  167. case GL_RGBA16F:
  168. format = Image::FORMAT_RGBAH;
  169. break;
  170. case GL_RGB9_E5:
  171. format = Image::FORMAT_RGBE9995;
  172. break;
  173. case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  174. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  175. format = Image::FORMAT_DXT1;
  176. break;
  177. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  178. format = Image::FORMAT_DXT3;
  179. break;
  180. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  181. format = Image::FORMAT_DXT5;
  182. break;
  183. case GL_COMPRESSED_RED_RGTC1:
  184. format = Image::FORMAT_RGTC_R;
  185. break;
  186. case GL_COMPRESSED_RG_RGTC2:
  187. format = Image::FORMAT_RGTC_RG;
  188. break;
  189. case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
  190. format = Image::FORMAT_BPTC_RGBFU;
  191. break;
  192. case GL_COMPRESSED_RGBA_BPTC_UNORM:
  193. format = Image::FORMAT_BPTC_RGBA;
  194. break;
  195. #if 0 // TODO: ETC compression is bogus.
  196. case GL_ETC1_RGB8_OES:
  197. format = Image::FORMAT_ETC;
  198. break;
  199. case GL_COMPRESSED_R11_EAC:
  200. format = Image::FORMAT_ETC2_R11;
  201. break;
  202. case GL_COMPRESSED_SIGNED_R11_EAC:
  203. format = Image::FORMAT_ETC2_R11S;
  204. break;
  205. case GL_COMPRESSED_RG11_EAC:
  206. format = Image::FORMAT_ETC2_RG11;
  207. break;
  208. case GL_COMPRESSED_SIGNED_RG11_EAC:
  209. format = Image::FORMAT_ETC2_RG11S;
  210. break;
  211. case GL_COMPRESSED_RGB8_ETC2:
  212. format = Image::FORMAT_ETC2_RGB8;
  213. break;
  214. case GL_COMPRESSED_RGBA8_ETC2_EAC:
  215. format = Image::FORMAT_ETC2_RGBA8;
  216. break;
  217. case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  218. format = Image::FORMAT_ETC2_RGB8A1;
  219. break;
  220. #endif
  221. case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
  222. format = Image::FORMAT_ASTC_4x4;
  223. break;
  224. case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
  225. format = Image::FORMAT_ASTC_4x4_HDR;
  226. break;
  227. case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
  228. format = Image::FORMAT_ASTC_8x8;
  229. break;
  230. case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
  231. format = Image::FORMAT_ASTC_8x8_HDR;
  232. break;
  233. default:
  234. ktxTexture_Destroy(ktx_texture);
  235. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported format " + itos(((ktxTexture1 *)ktx_texture)->glInternalformat) + " of KTX1 texture file.");
  236. }
  237. break;
  238. case ktxTexture2_c: {
  239. ktxTexture2 *ktx_texture2 = reinterpret_cast<ktxTexture2 *>(ktx_texture);
  240. if (ktx_texture2->vkFormat == VK_FORMAT_UNDEFINED) {
  241. if (!ktxTexture2_NeedsTranscoding(ktx_texture2)) {
  242. ktxTexture_Destroy(ktx_texture);
  243. ERR_FAIL_V_MSG(Ref<Resource>(), "Invalid VK_FORMAT_UNDEFINED of KTX2 texture file.");
  244. }
  245. ktx_transcode_fmt_e ktxfmt;
  246. switch (ktxTexture2_GetNumComponents(ktx_texture2)) {
  247. case 1: {
  248. if (ktxTexture2_GetOETF_e(ktx_texture2) == KHR_DF_TRANSFER_SRGB) { // TODO srgb native support
  249. ktxfmt = KTX_TTF_RGBA32;
  250. } else if (RS::get_singleton()->has_os_feature("rgtc")) {
  251. ktxfmt = KTX_TTF_BC4_R;
  252. } else {
  253. ktxfmt = KTX_TTF_RGBA32;
  254. }
  255. break;
  256. }
  257. case 2: {
  258. if (ktxTexture2_GetOETF_e(ktx_texture2) == KHR_DF_TRANSFER_SRGB) { // TODO srgb native support
  259. ktxfmt = KTX_TTF_RGBA32;
  260. } else if (RS::get_singleton()->has_os_feature("rgtc")) {
  261. ktxfmt = KTX_TTF_BC5_RG;
  262. } else {
  263. ktxfmt = KTX_TTF_RGBA32;
  264. }
  265. break;
  266. }
  267. case 3: {
  268. if (ktxTexture2_GetOETF_e(ktx_texture2) == KHR_DF_TRANSFER_SRGB) { // TODO: srgb native support
  269. ktxfmt = KTX_TTF_RGBA32;
  270. } else if (RS::get_singleton()->has_os_feature("bptc")) {
  271. ktxfmt = KTX_TTF_BC7_RGBA;
  272. } else if (RS::get_singleton()->has_os_feature("s3tc")) {
  273. ktxfmt = KTX_TTF_BC1_RGB;
  274. } else if (RS::get_singleton()->has_os_feature("etc")) {
  275. ktxfmt = KTX_TTF_ETC1_RGB;
  276. } else {
  277. ktxfmt = KTX_TTF_RGBA32;
  278. }
  279. break;
  280. }
  281. case 4: {
  282. if (ktxTexture2_GetOETF_e(ktx_texture2) == KHR_DF_TRANSFER_SRGB) { // TODO srgb native support
  283. ktxfmt = KTX_TTF_RGBA32;
  284. } else if (RS::get_singleton()->has_os_feature("astc")) {
  285. ktxfmt = KTX_TTF_ASTC_4x4_RGBA;
  286. } else if (RS::get_singleton()->has_os_feature("bptc")) {
  287. ktxfmt = KTX_TTF_BC7_RGBA;
  288. } else if (RS::get_singleton()->has_os_feature("s3tc")) {
  289. ktxfmt = KTX_TTF_BC3_RGBA;
  290. } else if (RS::get_singleton()->has_os_feature("etc2")) {
  291. ktxfmt = KTX_TTF_ETC2_RGBA;
  292. } else {
  293. ktxfmt = KTX_TTF_RGBA32;
  294. }
  295. break;
  296. }
  297. default: {
  298. ktxTexture_Destroy(ktx_texture);
  299. ERR_FAIL_V_MSG(Ref<Resource>(), "Invalid components of KTX2 texture file.");
  300. }
  301. }
  302. result = ktxTexture2_TranscodeBasis(ktx_texture2, ktxfmt, 0);
  303. if (result != KTX_SUCCESS) {
  304. ktxTexture_Destroy(ktx_texture);
  305. ERR_FAIL_V_MSG(Ref<Resource>(), "Failed to transcode KTX2 texture file.");
  306. }
  307. }
  308. switch (ktx_texture2->vkFormat) {
  309. case VK_FORMAT_R8_UNORM:
  310. format = Image::FORMAT_L8;
  311. break;
  312. case VK_FORMAT_R8G8_UNORM:
  313. format = Image::FORMAT_LA8;
  314. break;
  315. case VK_FORMAT_R8G8B8_SRGB:
  316. format = Image::FORMAT_RGB8;
  317. srgb = true;
  318. break;
  319. case VK_FORMAT_R8G8B8A8_SRGB:
  320. format = Image::FORMAT_RGBA8;
  321. srgb = true;
  322. break;
  323. case VK_FORMAT_R8_UINT:
  324. format = Image::FORMAT_R8;
  325. break;
  326. case VK_FORMAT_R8G8_UINT:
  327. format = Image::FORMAT_RG8;
  328. break;
  329. case VK_FORMAT_R8G8B8_UINT:
  330. format = Image::FORMAT_RGB8;
  331. break;
  332. case VK_FORMAT_R8G8B8A8_UINT:
  333. format = Image::FORMAT_RGBA8;
  334. break;
  335. case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
  336. format = Image::FORMAT_RGBA4444;
  337. break;
  338. case VK_FORMAT_R5G6B5_UNORM_PACK16:
  339. format = Image::FORMAT_RGB565;
  340. break;
  341. case VK_FORMAT_R32_SFLOAT:
  342. format = Image::FORMAT_RF;
  343. break;
  344. case VK_FORMAT_R32G32_SFLOAT:
  345. format = Image::FORMAT_RGF;
  346. break;
  347. case VK_FORMAT_R32G32B32_SFLOAT:
  348. format = Image::FORMAT_RGBF;
  349. break;
  350. case VK_FORMAT_R32G32B32A32_SFLOAT:
  351. format = Image::FORMAT_RGBAF;
  352. break;
  353. case VK_FORMAT_R16_SFLOAT:
  354. format = Image::FORMAT_RH;
  355. break;
  356. case VK_FORMAT_R16G16_SFLOAT:
  357. format = Image::FORMAT_RGH;
  358. break;
  359. case VK_FORMAT_R16G16B16_SFLOAT:
  360. format = Image::FORMAT_RGBH;
  361. break;
  362. case VK_FORMAT_R16G16B16A16_SFLOAT:
  363. format = Image::FORMAT_RGBAH;
  364. break;
  365. case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32:
  366. format = Image::FORMAT_RGBE9995;
  367. break;
  368. case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
  369. case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
  370. format = Image::FORMAT_DXT1;
  371. break;
  372. case VK_FORMAT_BC2_UNORM_BLOCK:
  373. format = Image::FORMAT_DXT3;
  374. break;
  375. case VK_FORMAT_BC3_UNORM_BLOCK:
  376. format = Image::FORMAT_DXT5;
  377. break;
  378. case VK_FORMAT_BC4_UNORM_BLOCK:
  379. format = Image::FORMAT_RGTC_R;
  380. break;
  381. case VK_FORMAT_BC5_UNORM_BLOCK:
  382. format = Image::FORMAT_RGTC_RG;
  383. break;
  384. case VK_FORMAT_BC6H_UFLOAT_BLOCK:
  385. format = Image::FORMAT_BPTC_RGBFU;
  386. break;
  387. case VK_FORMAT_BC6H_SFLOAT_BLOCK:
  388. format = Image::FORMAT_BPTC_RGBF;
  389. break;
  390. case VK_FORMAT_BC7_UNORM_BLOCK:
  391. format = Image::FORMAT_BPTC_RGBA;
  392. break;
  393. #if 0 // TODO: ETC compression is bogus.
  394. case VK_FORMAT_EAC_R11_UNORM_BLOCK:
  395. format = Image::FORMAT_ETC2_R11;
  396. break;
  397. case VK_FORMAT_EAC_R11_SNORM_BLOCK:
  398. format = Image::FORMAT_ETC2_R11S;
  399. break;
  400. case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
  401. format = Image::FORMAT_ETC2_RG11;
  402. break;
  403. case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
  404. format = Image::FORMAT_ETC2_RG11S;
  405. break;
  406. case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
  407. format = Image::FORMAT_ETC2_RGB8;
  408. break;
  409. case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
  410. format = Image::FORMAT_ETC2_RGBA8;
  411. break;
  412. case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
  413. format = Image::FORMAT_ETC2_RGB8A1;
  414. break;
  415. #endif
  416. case VK_FORMAT_ASTC_4x4_SRGB_BLOCK:
  417. format = Image::FORMAT_ASTC_4x4;
  418. break;
  419. case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
  420. format = Image::FORMAT_ASTC_4x4_HDR;
  421. break;
  422. case VK_FORMAT_ASTC_8x8_SRGB_BLOCK:
  423. format = Image::FORMAT_ASTC_8x8;
  424. break;
  425. case VK_FORMAT_ASTC_8x8_UNORM_BLOCK:
  426. format = Image::FORMAT_ASTC_8x8_HDR;
  427. break;
  428. default:
  429. ktxTexture_Destroy(ktx_texture);
  430. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported format " + itos(((ktxTexture2 *)ktx_texture)->vkFormat) + " of KTX2 texture file.");
  431. break;
  432. }
  433. break;
  434. }
  435. default:
  436. ktxTexture_Destroy(ktx_texture);
  437. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported version KTX texture file.");
  438. break;
  439. }
  440. Vector<uint8_t> src_data;
  441. // KTX use 4-bytes padding, don't use mipmaps if padding is effective
  442. // TODO: unpad dynamically
  443. int pixel_size = Image::get_format_pixel_size(format);
  444. int pixel_rshift = Image::get_format_pixel_rshift(format);
  445. int block = Image::get_format_block_size(format);
  446. int minw, minh;
  447. Image::get_format_min_pixel_size(format, minw, minh);
  448. int w = width;
  449. int h = height;
  450. for (uint32_t i = 0; i < mipmaps; ++i) {
  451. ktx_size_t mip_size = ktxTexture_GetImageSize(ktx_texture, i);
  452. size_t bw = w % block != 0 ? w + (block - w % block) : w;
  453. size_t bh = h % block != 0 ? h + (block - h % block) : h;
  454. size_t s = bw * bh;
  455. s *= pixel_size;
  456. s >>= pixel_rshift;
  457. if (mip_size != static_cast<ktx_size_t>(s)) {
  458. if (!i) {
  459. ktxTexture_Destroy(ktx_texture);
  460. ERR_FAIL_V_MSG(Ref<Resource>(), "Unsupported padded KTX texture file.");
  461. }
  462. mipmaps = 1;
  463. break;
  464. }
  465. w = MAX(minw, w >> 1);
  466. h = MAX(minh, h >> 1);
  467. }
  468. for (uint32_t i = 0; i < mipmaps; ++i) {
  469. ktx_size_t mip_size = ktxTexture_GetImageSize(ktx_texture, i);
  470. ktx_size_t offset;
  471. if (ktxTexture_GetImageOffset(ktx_texture, i, 0, 0, &offset) != KTX_SUCCESS) {
  472. ktxTexture_Destroy(ktx_texture);
  473. ERR_FAIL_V_MSG(Ref<Resource>(), "Invalid KTX texture file.");
  474. }
  475. int prev_size = src_data.size();
  476. src_data.resize(prev_size + mip_size);
  477. memcpy(src_data.ptrw() + prev_size, ktxTexture_GetData(ktx_texture) + offset, mip_size);
  478. }
  479. Ref<Image> img = memnew(Image(width, height, mipmaps - 1, format, src_data));
  480. if (srgb) {
  481. img->srgb_to_linear();
  482. }
  483. if (r_error) {
  484. *r_error = OK;
  485. }
  486. ktxTexture_Destroy(ktx_texture);
  487. return img;
  488. }
  489. Ref<Resource> ResourceFormatKTX::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) {
  490. if (r_error) {
  491. *r_error = ERR_CANT_OPEN;
  492. }
  493. Error err;
  494. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  495. if (f.is_null()) {
  496. return Ref<Resource>();
  497. }
  498. Ref<FileAccess> fref(f);
  499. if (r_error) {
  500. *r_error = ERR_FILE_CORRUPT;
  501. }
  502. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Unable to open KTX texture file '" + p_path + "'.");
  503. Ref<Image> img = load_from_file_access(f, r_error);
  504. Ref<ImageTexture> texture = ImageTexture::create_from_image(img);
  505. return texture;
  506. }
  507. static Ref<Image> _ktx_mem_loader_func(const uint8_t *p_ktx, int p_size) {
  508. Ref<FileAccessMemory> f;
  509. f.instantiate();
  510. f->open_custom(p_ktx, p_size);
  511. Error err;
  512. Ref<Image> img = load_from_file_access(f, &err);
  513. ERR_FAIL_COND_V(err, Ref<Image>());
  514. return img;
  515. }
  516. void ResourceFormatKTX::get_recognized_extensions(List<String> *p_extensions) const {
  517. p_extensions->push_back("ktx");
  518. p_extensions->push_back("ktx2");
  519. }
  520. bool ResourceFormatKTX::handles_type(const String &p_type) const {
  521. return ClassDB::is_parent_class(p_type, "Texture2D");
  522. }
  523. String ResourceFormatKTX::get_resource_type(const String &p_path) const {
  524. if (p_path.get_extension().to_lower() == "ktx" || p_path.get_extension().to_lower() == "ktx2") {
  525. return "ImageTexture";
  526. }
  527. return "";
  528. }
  529. ResourceFormatKTX::ResourceFormatKTX() {
  530. Image::_ktx_mem_loader_func = _ktx_mem_loader_func;
  531. }