2020-02-11 23:54:17 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
2021-07-02 00:23:26 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2021-07-02 00:23:26 +08:00
|
|
|
ASTC Texture Decompression.
|
2021-07-02 13:00:04 +08:00
|
|
|
|
2021-07-02 00:23:26 +08:00
|
|
|
astc compression tool: https://github.com/ARM-software/astc-encoder/releases
|
|
|
|
version required: v2.2+
|
|
|
|
command:
|
|
|
|
astcenc-sse4.2 -cl test1.png test1.astc 6x6 -medium -pp-premultiply
|
2021-07-02 13:00:04 +08:00
|
|
|
|
2020-02-11 23:54:17 +08:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/astc.h"
|
2021-07-02 13:25:00 +08:00
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
2021-07-02 14:20:54 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <deque>
|
2021-06-01 23:43:28 +08:00
|
|
|
#include "astc/astcenc.h"
|
|
|
|
#include "astc/astcenc_internal.h"
|
2021-07-02 00:18:02 +08:00
|
|
|
#include "yasio/detail/utils.hpp"
|
2020-02-11 23:54:17 +08:00
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
#define ASTCDEC_NO_CONTEXT 1
|
2021-07-02 15:10:31 +08:00
|
|
|
#define ASTCDEC_PRINT_BENCHMARK 0
|
2021-07-02 00:18:02 +08:00
|
|
|
|
|
|
|
typedef std::mutex astc_decompress_mutex_t;
|
|
|
|
|
|
|
|
struct astc_decompress_task {
|
2021-07-02 14:20:54 +08:00
|
|
|
astc_decompress_task() {}
|
|
|
|
~astc_decompress_task() {
|
2021-07-02 14:32:22 +08:00
|
|
|
#if ASTCDEC_NO_CONTEXT
|
|
|
|
term_block_size_descriptor(_bsd);
|
|
|
|
#else
|
2021-07-02 14:20:54 +08:00
|
|
|
if (_context)
|
|
|
|
astcenc_context_free(this->_context);
|
|
|
|
#endif
|
2021-07-02 00:18:02 +08:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:20:54 +08:00
|
|
|
void wait_done() {
|
|
|
|
#if ASTCDEC_NO_CONTEXT
|
|
|
|
_decompress_pm.wait();
|
|
|
|
#else
|
|
|
|
_context->manage_decompress.wait();
|
|
|
|
#endif
|
2021-07-02 00:18:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* _in_texels = nullptr;
|
|
|
|
void* _out_texels[1]{};
|
2021-07-02 14:20:54 +08:00
|
|
|
unsigned int _xblocks, _yblocks;
|
2021-07-02 13:00:04 +08:00
|
|
|
#if ASTCDEC_NO_CONTEXT
|
2021-07-02 14:20:54 +08:00
|
|
|
unsigned int _block_x, _block_y;
|
2021-07-02 13:00:04 +08:00
|
|
|
ParallelManager _decompress_pm{};
|
|
|
|
block_size_descriptor _bsd{};
|
|
|
|
#else
|
2021-07-02 14:20:54 +08:00
|
|
|
astcenc_config _config{};
|
2021-07-02 00:18:02 +08:00
|
|
|
astcenc_context* _context = nullptr;
|
2021-07-02 13:00:04 +08:00
|
|
|
#endif
|
|
|
|
astcenc_image _image_out{};
|
2021-07-02 00:18:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class astc_decompress_job_manager {
|
|
|
|
public:
|
|
|
|
static astc_decompress_job_manager* get_instance() {
|
|
|
|
static astc_decompress_job_manager s_instance;
|
|
|
|
return &s_instance;
|
|
|
|
}
|
|
|
|
astc_decompress_job_manager() {
|
2021-07-02 14:20:54 +08:00
|
|
|
int thread_count = std::thread::hardware_concurrency();
|
|
|
|
for (int i = 0; i < thread_count; ++i) {
|
2021-07-02 00:18:02 +08:00
|
|
|
_threads.push_back(std::thread{&astc_decompress_job_manager::run, this});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~astc_decompress_job_manager() {
|
|
|
|
_stopped = true;
|
|
|
|
|
2021-07-02 14:20:54 +08:00
|
|
|
_task_queue_mtx.lock();
|
|
|
|
_task_queue.clear();
|
|
|
|
_task_queue_cv.notify_all();
|
|
|
|
_task_queue_mtx.unlock();
|
2021-07-02 00:18:02 +08:00
|
|
|
|
|
|
|
for (auto& t : _threads) {
|
|
|
|
if (t.joinable())
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
_threads.clear();
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
int decompress_parallel_sync(const uint8_t* in, uint32_t inlen, uint8_t* out, unsigned int dim_x,
|
|
|
|
unsigned int dim_y, int block_x, int block_y) {
|
2021-07-02 14:20:54 +08:00
|
|
|
|
|
|
|
auto task = make_task(in, inlen, out, dim_x, dim_y, block_x, block_y);
|
|
|
|
if (!task)
|
|
|
|
return ASTCENC_ERR_OUT_OF_MEM;
|
|
|
|
|
|
|
|
_task_queue_mtx.lock();
|
|
|
|
_task_queue.push_back(task);
|
|
|
|
_task_queue_mtx.unlock();
|
|
|
|
_task_queue_cv.notify_all(); // notify all thread to process the single decompress task parallel
|
|
|
|
|
|
|
|
task->wait_done();
|
|
|
|
|
|
|
|
_task_queue_mtx.lock();
|
|
|
|
assert(!_task_queue.empty());
|
|
|
|
auto t = _task_queue.front();
|
|
|
|
assert(t.get() == task.get());
|
|
|
|
_task_queue.pop_front();
|
|
|
|
_task_queue_mtx.unlock();
|
|
|
|
|
|
|
|
return ASTCENC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-02 14:32:22 +08:00
|
|
|
std::shared_ptr<astc_decompress_task> make_task(const uint8_t* in, unsigned int inlen, uint8_t* out,
|
|
|
|
unsigned int dim_x, unsigned int dim_y, int block_x, int block_y) {
|
2021-07-02 13:00:04 +08:00
|
|
|
unsigned int xblocks = (dim_x + block_x - 1) / block_x;
|
|
|
|
unsigned int yblocks = (dim_y + block_y - 1) / block_y;
|
|
|
|
unsigned int zblocks = 1; // (dim_z + block_z - 1) / block_z;
|
|
|
|
|
|
|
|
// Check we have enough output space (16 bytes per block)
|
|
|
|
auto total_blocks = xblocks * yblocks * zblocks;
|
|
|
|
size_t size_needed = total_blocks * 16;
|
2021-07-02 14:20:54 +08:00
|
|
|
if (inlen < size_needed)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto task = std::make_shared<astc_decompress_task>();
|
|
|
|
task->_in_texels = in;
|
|
|
|
task->_out_texels[0] = out;
|
|
|
|
task->_image_out = {dim_x, dim_y, 1, ASTCENC_TYPE_U8, task->_out_texels};
|
|
|
|
|
|
|
|
task->_xblocks = xblocks;
|
|
|
|
task->_yblocks = yblocks;
|
2021-07-02 13:00:04 +08:00
|
|
|
#if ASTCDEC_NO_CONTEXT
|
2021-11-11 18:43:05 +08:00
|
|
|
//since astcenc-3.3, doesn't required
|
|
|
|
//static std::once_flag once_flag;
|
|
|
|
//std::call_once(once_flag, init_quant_mode_table);
|
2021-07-02 14:20:54 +08:00
|
|
|
|
|
|
|
task->_block_x = block_x;
|
|
|
|
task->_block_y = block_y;
|
|
|
|
init_block_size_descriptor(block_x, block_y, 1, false, 0 /*unused for decompress*/, task->_bsd);
|
2021-07-02 13:00:04 +08:00
|
|
|
task->_decompress_pm.init(total_blocks);
|
|
|
|
#else
|
2021-07-02 14:20:54 +08:00
|
|
|
(void) astcenc_config_init(
|
|
|
|
ASTCENC_PRF_LDR, block_x, block_y, 1, 0, ASTCENC_FLG_DECOMPRESS_ONLY, &task->_config);
|
2021-07-02 14:32:22 +08:00
|
|
|
(void) astcenc_context_alloc(&task->_config, (unsigned int) _threads.size(), &task->_context);
|
2021-07-02 13:00:04 +08:00
|
|
|
task->_context->manage_decompress.init(total_blocks);
|
|
|
|
#endif
|
2021-07-02 14:20:54 +08:00
|
|
|
return task;
|
2021-07-02 00:18:02 +08:00
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const astcenc_swizzle swz_decode{ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A};
|
|
|
|
|
|
|
|
bool no_task_count = false;
|
|
|
|
|
|
|
|
for (;;) {
|
2021-07-02 14:20:54 +08:00
|
|
|
std::unique_lock<astc_decompress_mutex_t> lck(_task_queue_mtx);
|
|
|
|
if (!_stopped && (_task_queue.empty() || no_task_count))
|
|
|
|
_task_queue_cv.wait(lck);
|
2021-07-02 00:18:02 +08:00
|
|
|
|
|
|
|
if (_stopped)
|
|
|
|
break;
|
|
|
|
|
2021-07-02 14:20:54 +08:00
|
|
|
if (_task_queue.empty())
|
2021-07-02 00:18:02 +08:00
|
|
|
continue;
|
2021-07-02 14:20:54 +08:00
|
|
|
auto task = _task_queue.front();
|
2021-07-02 00:18:02 +08:00
|
|
|
lck.unlock(); // unlock make sure other thread can work for the task
|
|
|
|
|
|
|
|
auto& image_out = task->_image_out;
|
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
#if ASTCDEC_NO_CONTEXT
|
2021-07-02 14:20:54 +08:00
|
|
|
unsigned int block_x = task->_block_x;
|
|
|
|
unsigned int block_y = task->_block_y;
|
|
|
|
unsigned int block_z = 1; // task->block_z;
|
|
|
|
auto& bsd = task->_bsd;
|
|
|
|
auto& decompress_pm = task->_decompress_pm;
|
2021-07-02 13:00:04 +08:00
|
|
|
#else
|
2021-07-02 14:20:54 +08:00
|
|
|
unsigned int block_x = task->_config.block_x;
|
|
|
|
unsigned int block_y = task->_config.block_y;
|
|
|
|
unsigned int block_z = 1; // task->_config.block_z;
|
|
|
|
auto& bsd = *task->_context->bsd;
|
2021-07-02 13:00:04 +08:00
|
|
|
auto& decompress_pm = task->_context->manage_decompress;
|
|
|
|
#endif
|
2021-07-02 14:20:54 +08:00
|
|
|
unsigned int xblocks = task->_xblocks;
|
|
|
|
unsigned int yblocks = task->_yblocks;
|
|
|
|
unsigned int zblocks = 1; // (image_out.dim_z + block_z - 1) / block_z;
|
|
|
|
|
2021-07-02 00:18:02 +08:00
|
|
|
int row_blocks = xblocks;
|
|
|
|
int plane_blocks = xblocks * yblocks;
|
|
|
|
|
|
|
|
image_block blk;
|
|
|
|
auto data = task->_in_texels;
|
|
|
|
for (;;) { // process the task
|
|
|
|
unsigned int count = 0;
|
2021-07-02 13:00:04 +08:00
|
|
|
unsigned int base = decompress_pm.get_task_assignment(128, count);
|
2021-07-02 00:18:02 +08:00
|
|
|
|
|
|
|
no_task_count = !count;
|
|
|
|
if (no_task_count) { // this thread will going to suspend until new task added
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = base; i < base + count; i++) {
|
|
|
|
// Decode i into x, y, z block indices
|
|
|
|
int z = i / plane_blocks;
|
|
|
|
unsigned int rem = i - (z * plane_blocks);
|
|
|
|
int y = rem / row_blocks;
|
|
|
|
int x = rem - (y * row_blocks);
|
|
|
|
|
|
|
|
unsigned int offset = (((z * yblocks + y) * xblocks) + x) * 16;
|
|
|
|
const uint8_t* bp = data + offset;
|
|
|
|
physical_compressed_block pcb = *(const physical_compressed_block*) bp;
|
|
|
|
symbolic_compressed_block scb;
|
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
physical_to_symbolic(bsd, pcb, scb);
|
2021-07-02 00:18:02 +08:00
|
|
|
|
2021-07-02 14:20:54 +08:00
|
|
|
decompress_symbolic_block(ASTCENC_PRF_LDR, bsd, x * block_x, y * block_y, z * block_z, scb, blk);
|
2021-07-02 00:18:02 +08:00
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
write_image_block(image_out, blk, bsd, x * block_x, y * block_y, z * block_z, swz_decode);
|
2021-07-02 00:18:02 +08:00
|
|
|
}
|
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
decompress_pm.complete_task_assignment(count);
|
2021-07-02 00:18:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::thread> _threads;
|
|
|
|
|
2021-07-02 14:20:54 +08:00
|
|
|
std::deque<std::shared_ptr<astc_decompress_task>> _task_queue;
|
|
|
|
astc_decompress_mutex_t _task_queue_mtx;
|
|
|
|
std::condition_variable_any _task_queue_cv;
|
2021-07-02 00:18:02 +08:00
|
|
|
|
2021-07-02 14:20:54 +08:00
|
|
|
bool _stopped = false;
|
|
|
|
};
|
2021-07-02 00:18:02 +08:00
|
|
|
|
2021-07-02 13:00:04 +08:00
|
|
|
int astc_decompress_image(const uint8_t* in, uint32_t inlen, uint8_t* out, uint32_t dim_x, uint32_t dim_y,
|
|
|
|
uint32_t block_x, uint32_t block_y) {
|
|
|
|
#if ASTCDEC_PRINT_BENCHMARK
|
|
|
|
struct benchmark_printer {
|
2021-07-02 15:10:31 +08:00
|
|
|
benchmark_printer(const char* fmt, int w, int h, float den)
|
|
|
|
: _fmt(fmt), _w(w), _h(h), _den(den), _start(yasio::highp_clock()) {}
|
2021-07-02 13:00:04 +08:00
|
|
|
~benchmark_printer() {
|
2021-07-02 15:10:31 +08:00
|
|
|
cocos2d::log(_fmt, _w, _h, (yasio::highp_clock() - _start) / _den);
|
2021-07-02 13:00:04 +08:00
|
|
|
}
|
|
|
|
const char* _fmt;
|
2021-07-02 15:10:31 +08:00
|
|
|
int _w, _h;
|
2021-07-02 13:00:04 +08:00
|
|
|
float _den;
|
|
|
|
yasio::highp_time_t _start;
|
|
|
|
};
|
2021-07-02 15:10:31 +08:00
|
|
|
benchmark_printer __printer("decompress astc image (%dx%d) cost: %.3lf(ms)", dim_x, dim_y, (float) std::milli::den);
|
2021-07-02 13:00:04 +08:00
|
|
|
#endif
|
|
|
|
return astc_decompress_job_manager::get_instance()->decompress_parallel_sync(
|
|
|
|
in, inlen, out, dim_x, dim_y, block_x, block_y);
|
2021-06-01 23:43:28 +08:00
|
|
|
}
|