HTML and CSS Reference
In-Depth Information
// Determine what type of compressed data the file contains.
var format = Module._crn_get_dxt_format(src, srcSize);
if (!DXT_FORMAT_MAP[format]) {
errorCallback("Unsupported image format");
// Gather basic metrics about the DXT data.
var levels = Module._crn_get_levels(src, srcSize);
var width = Module._crn_get_width(src, srcSize);
var height = Module._crn_get_height(src, srcSize);
var bytesPerBlock = Module._crn_get_bytes_per_block(src, srcSize);
// Determine the size of the decoded DXT data.
var dstSize = 0;
var i;
for (i = 0; i < levels; ++i) {
dstSize += dxtLevelSize(width >> i, height >> i, bytesPerBlock);
// Allocate enough space on the emscripten heap to hold the decoded DXT data
// or reuse the existing allocation if a previous call to this function has
// already acquired a large enough buffer.
if(cachedDstSize < dstSize) {
if(dst) { Module._free(dst); }
dst = Module._malloc(dstSize);
dxtData = new Uint8Array(Module.HEAPU8.buffer, dst, dstSize);
cachedDstSize = dstSize;
// Decompress the DXT data from the Crunch file into the allocated space.
Module._crn_decompress(src, srcSize, dst, dstSize, 0, levels);
// Release the crunch file data from the emscripten heap.
// Pass the DXT information to the callback for uploading.
callback(dxtData, width, height, levels, DXT_FORMAT_MAP[format], bytesPerBlock);
Here's what's happening. First off, if this is the first time you're calling this function, you load the Emscripten
module by calling LoadCrunchDecoder . That way none of that potentially expensive code is executed at all unless you
actually need it. (See, I told you that putting it in a function would come in handy!)
Next, you copy the bytes of the Crunch data you've been given into Emscripten's virtual “heap.” In Emscripten
compiled code, the C++ heap is represented by a large, pre-allocated TypedArray , which is accessed with Module.HEAPU8
in the code in Listing 21-12. (As the name suggests, this is a Uint8Array view of the heap buffer.) Under this setup,
memory allocations are simply reserved ranges of the array and pointers are integer offsets into the array (which
is pretty close to how native memory management works anyway). This means that anytime you want Emscripten
to operate on any data, you have to copy it into that heap manually. You're using a simple helper function here
( arrayBufferCopy ) to handle the copy.
Search WWH ::

Custom Search