| 151 | } |
| 152 | |
| 153 | void Astc::decode(BlockDim blockdim, VkExtent3D extent, const uint8_t *compressed_data, uint32_t compressed_size) |
| 154 | { |
| 155 | PROFILE_SCOPE("Decode ASTC Image"); |
| 156 | |
| 157 | // Actual decoding |
| 158 | astcenc_swizzle swizzle = {ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A}; |
| 159 | // Configure the compressor run |
| 160 | astcenc_config astc_config; |
| 161 | auto atscresult = astcenc_config_init( |
| 162 | ASTCENC_PRF_LDR_SRGB, |
| 163 | blockdim.x, |
| 164 | blockdim.y, |
| 165 | blockdim.z, |
| 166 | ASTCENC_PRE_FAST, |
| 167 | ASTCENC_FLG_DECOMPRESS_ONLY, |
| 168 | &astc_config); |
| 169 | |
| 170 | if (atscresult != ASTCENC_SUCCESS) |
| 171 | { |
| 172 | throw std::runtime_error{"Error initializing astc"}; |
| 173 | } |
| 174 | |
| 175 | if (extent.width == 0 || extent.height == 0 || extent.depth == 0) |
| 176 | { |
| 177 | throw std::runtime_error{"Error reading astc: invalid size"}; |
| 178 | } |
| 179 | |
| 180 | // Allocate working state given config and thread_count |
| 181 | astcenc_context *astc_context; |
| 182 | astcenc_context_alloc(&astc_config, 1, &astc_context); |
| 183 | |
| 184 | astcenc_image decoded{}; |
| 185 | decoded.dim_x = extent.width; |
| 186 | decoded.dim_y = extent.height; |
| 187 | decoded.dim_z = extent.depth; |
| 188 | decoded.data_type = ASTCENC_TYPE_U8; |
| 189 | |
| 190 | // allocate storage for the decoded image |
| 191 | // The astcenc_decompress_image function will write directly to the image data vector |
| 192 | auto uncompressed_size = decoded.dim_x * decoded.dim_y * decoded.dim_z * 4; |
| 193 | auto &decoded_data = get_mut_data(); |
| 194 | decoded_data.resize(uncompressed_size); |
| 195 | void *data_ptr = static_cast<void *>(decoded_data.data()); |
| 196 | decoded.data = &data_ptr; |
| 197 | |
| 198 | atscresult = astcenc_decompress_image(astc_context, compressed_data, compressed_size, &decoded, &swizzle, 0); |
| 199 | if (atscresult != ASTCENC_SUCCESS) |
| 200 | { |
| 201 | throw std::runtime_error("Error decoding astc"); |
| 202 | } |
| 203 | astcenc_context_free(astc_context); |
| 204 | |
| 205 | set_format(VK_FORMAT_R8G8B8A8_SRGB); |
| 206 | set_width(decoded.dim_x); |
| 207 | set_height(decoded.dim_y); |
| 208 | set_depth(decoded.dim_z); |
| 209 | } |
| 210 |
no test coverage detected