MCPcopy Create free account
hub / github.com/BinomialLLC/basis_universal / transcode_hdr

Function transcode_hdr

example/example.cpp:235–323  ·  view source on GitHub ↗

This example function loads a .KTX2 file and then transcodes it to various compressed/uncompressed texture formats. It writes .DDS and .ASTC files. ARM's astcenc tool can be used to unpack the .ASTC file: astcenc-avx2.exe -dh test_uastc_hdr_astc.astc out.exr

Source from the content-addressed store, hash-verified

233// ARM's astcenc tool can be used to unpack the .ASTC file:
234// astcenc-avx2.exe -dh test_uastc_hdr_astc.astc out.exr
235static bool transcode_hdr()
236{
237 // Note: The encoder already initializes the transcoder, but if you haven't initialized the encoder you MUST call this function to initialize the transcoder.
238 basist::basisu_transcoder_init();
239
240 // Read the .KTX2 file's data into memory.
241 uint8_vec ktx2_file_data;
242 if (!read_file_to_vec("test_uastc_hdr.ktx2", ktx2_file_data))
243 return false;
244
245 // Create the KTX2 transcoder object.
246 basist::ktx2_transcoder transcoder;
247
248 // Initialize the transcoder.
249 if (!transcoder.init(ktx2_file_data.data(), ktx2_file_data.size_u32()))
250 return false;
251
252 const uint32_t width = transcoder.get_width();
253 const uint32_t height = transcoder.get_height();
254
255 printf("Texture dimensions: %ux%u, levels: %u\n", width, height, transcoder.get_levels());
256
257 // This example only transcodes UASTC HDR textures.
258 if (!transcoder.is_hdr())
259 return false;
260
261 // Begin transcoding (this will be a no-op with UASTC HDR textures, but you still need to do it. For ETC1S it'll unpack the global codebooks.)
262 transcoder.start_transcoding();
263
264 // Transcode to BC6H and write a BC6H .DDS file.
265 {
266 gpu_image tex(texture_format::cBC6HUnsigned, width, height);
267
268 bool status = transcoder.transcode_image_level(0, 0, 0,
269 tex.get_ptr(), tex.get_total_blocks(),
270 basist::transcoder_texture_format::cTFBC6H, 0);
271 if (!status)
272 return false;
273
274 gpu_image_vec tex_vec;
275 tex_vec.push_back(tex);
276 if (!write_compressed_texture_file("test_uastc_hdr_bc6h.dds", tex_vec, false))
277 return false;
278 }
279
280 // Transcode to ASTC HDR 4x4 and write a ASTC 4x4 HDR .astc file.
281 {
282 gpu_image tex(texture_format::cASTC_HDR_4x4, width, height);
283
284 bool status = transcoder.transcode_image_level(0, 0, 0,
285 tex.get_ptr(), tex.get_total_blocks(),
286 basist::transcoder_texture_format::cTFASTC_HDR_4x4_RGBA, 0);
287 if (!status)
288 return false;
289
290 if (!write_astc_file("test_uastc_hdr_astc.astc", tex.get_ptr(), 4, 4, tex.get_pixel_width(), tex.get_pixel_height()))
291 return false;
292 }

Callers 1

mainFunction · 0.85

Calls 15

read_file_to_vecFunction · 0.85
write_exrFunction · 0.85
start_transcodingMethod · 0.80
get_pixel_widthMethod · 0.80
get_pixel_heightMethod · 0.80
write_astc_fileFunction · 0.50
half_to_floatFunction · 0.50
initMethod · 0.45
dataMethod · 0.45
size_u32Method · 0.45
get_widthMethod · 0.45

Tested by

no test coverage detected