| 1528 | } |
| 1529 | |
| 1530 | uint32_t encode(const emscripten::val& dst_basis_file_js_val) |
| 1531 | { |
| 1532 | if (!g_basis_initialized_flag) |
| 1533 | { |
| 1534 | #if BASISU_DEBUG_PRINTF |
| 1535 | printf("basis_encoder::encode: Must call basis_init() first!\n"); |
| 1536 | #endif |
| 1537 | assert(0); |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | // We don't use threading for now, but the compressor needs a job pool. |
| 1542 | uint32_t num_new_threads = 0; |
| 1543 | bool enable_threading = false; |
| 1544 | |
| 1545 | #if WASM_THREADS_ENABLED |
| 1546 | if ((emscripten_has_threading_support()) && (m_threading_enabled) && (m_num_extra_worker_threads)) |
| 1547 | { |
| 1548 | enable_threading = true; |
| 1549 | num_new_threads = m_num_extra_worker_threads; |
| 1550 | } |
| 1551 | #endif |
| 1552 | |
| 1553 | // We always need a job pool, but making a job pool with just 1 thread doesn't actually create any additional helper threads (i.e. it always utilizes the main thread). |
| 1554 | job_pool jpool(1 + num_new_threads); |
| 1555 | |
| 1556 | // Initialize the compression parameters structure. This is the same structure that the command line tool fills in. |
| 1557 | basis_compressor_params ¶ms = m_params; |
| 1558 | |
| 1559 | // Check to see if we would risk running out of memory in 32-bit WASM. There's not much we can do about this limit until memory64 is available. |
| 1560 | uint64_t total_src_texels = 0; |
| 1561 | |
| 1562 | for (uint32_t i = 0; i < m_params.m_source_images.size(); i++) |
| 1563 | total_src_texels += m_params.m_source_images[i].get_total_pixels(); |
| 1564 | |
| 1565 | for (uint32_t i = 0; i < m_params.m_source_images_hdr.size(); i++) |
| 1566 | total_src_texels += m_params.m_source_images_hdr[i].get_total_pixels(); |
| 1567 | |
| 1568 | // Try to prevent running out of memory inside WASM. |
| 1569 | uint32_t max_pixels_thresh = BASISU_ENCODER_MAX_SOURCE_IMAGE_PIXELS; |
| 1570 | |
| 1571 | // The simpler compressors need less temporary memory, so their threshold can be higher. |
| 1572 | if (m_params.is_etc1s() || m_params.is_uastc_ldr_4x4() || m_params.is_uastc_hdr_4x4()) |
| 1573 | { |
| 1574 | max_pixels_thresh = BASISU_ENCODER_MAX_SOURCE_IMAGE_PIXELS_HIGHER_LIMIT; |
| 1575 | } |
| 1576 | |
| 1577 | if (total_src_texels > max_pixels_thresh) |
| 1578 | { |
| 1579 | printf("ERROR: basis_encoder::encode(): The total number of source texels to compress %llu is greater than %u, which is likely too large for WASM (above BASISU_ENCODER_MAX_SOURCE_IMAGE_PIXELS in basis_wrappers.cpp).", |
| 1580 | total_src_texels, max_pixels_thresh); |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
| 1584 | params.m_pJob_pool = &jpool; |
| 1585 | |
| 1586 | params.m_multithreading = enable_threading; |
| 1587 |
no test coverage detected