| 66 | } |
| 67 | |
| 68 | Ktx::Ktx(const std::string &name, const std::vector<uint8_t> &data, ContentType content_type) : |
| 69 | Image{name} |
| 70 | { |
| 71 | auto data_buffer = reinterpret_cast<const ktx_uint8_t *>(data.data()); |
| 72 | auto data_size = static_cast<ktx_size_t>(data.size()); |
| 73 | |
| 74 | ktxTexture *texture; |
| 75 | auto load_ktx_result = ktxTexture_CreateFromMemory(data_buffer, |
| 76 | data_size, |
| 77 | KTX_TEXTURE_CREATE_NO_FLAGS, |
| 78 | &texture); |
| 79 | if (load_ktx_result != KTX_SUCCESS) |
| 80 | { |
| 81 | throw std::runtime_error{"Error loading KTX texture: " + name}; |
| 82 | } |
| 83 | |
| 84 | if (texture->pData) |
| 85 | { |
| 86 | // Already loaded |
| 87 | set_data(texture->pData, texture->dataSize); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // Load |
| 92 | auto &mut_data = get_mut_data(); |
| 93 | auto size = texture->dataSize; |
| 94 | mut_data.resize(size); |
| 95 | auto load_data_result = ktxTexture_LoadImageData(texture, mut_data.data(), size); |
| 96 | if (load_data_result != KTX_SUCCESS) |
| 97 | { |
| 98 | throw std::runtime_error{"Error loading KTX image data: " + name}; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | set_width(texture->baseWidth); |
| 103 | set_height(texture->baseHeight); |
| 104 | set_depth(texture->baseDepth); |
| 105 | set_layers(texture->numLayers); |
| 106 | update_hash(); |
| 107 | |
| 108 | bool cubemap = false; |
| 109 | |
| 110 | // Use the faces if there are 6 (for cubemap) |
| 111 | if (texture->numLayers == 1 && texture->numFaces == 6) |
| 112 | { |
| 113 | cubemap = true; |
| 114 | set_layers(texture->numFaces); |
| 115 | } |
| 116 | |
| 117 | auto updated_format = ktxTexture_GetVkFormat(texture); |
| 118 | |
| 119 | set_format(updated_format); |
| 120 | |
| 121 | if (texture->classId == ktxTexture1_c && content_type == Color) |
| 122 | { |
| 123 | // KTX-1 files don't contain color space information. Color data is normally |
| 124 | // in sRGB, but the format we get back won't report that, so this will adjust it |
| 125 | // if necessary. |