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