From 2cf5927920ceff756364c193d641f48f4116d9e8 Mon Sep 17 00:00:00 2001 From: clayjohn Date: Tue, 21 Feb 2023 13:46:45 -0800 Subject: [PATCH] Use multiple threads to import HDR images --- modules/cvtt/image_compress_cvtt.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/cvtt/image_compress_cvtt.cpp b/modules/cvtt/image_compress_cvtt.cpp index f19228cb18c..ad707722703 100644 --- a/modules/cvtt/image_compress_cvtt.cpp +++ b/modules/cvtt/image_compress_cvtt.cpp @@ -30,8 +30,8 @@ #include "image_compress_cvtt.h" +#include "core/object/worker_thread_pool.h" #include "core/os/os.h" -#include "core/os/thread.h" #include "core/string/print_string.h" #include "core/templates/safe_refcount.h" @@ -129,6 +129,18 @@ static void _digest_row_task(const CVTTCompressionJobParams &p_job_params, const } } +static void _digest_job_queue(void *p_job_queue, uint32_t p_index) { + CVTTCompressionJobQueue *job_queue = static_cast(p_job_queue); + uint32_t num_tasks = job_queue->num_tasks; + uint32_t total_threads = WorkerThreadPool::get_singleton()->get_thread_count(); + uint32_t start = p_index * num_tasks / total_threads; + uint32_t end = (p_index + 1 == total_threads) ? num_tasks : ((p_index + 1) * num_tasks / total_threads); + + for (uint32_t i = start; i < end; i++) { + _digest_row_task(job_queue->job_params, job_queue->job_tasks[i]); + } +} + void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) { if (p_image->get_format() >= Image::FORMAT_BPTC_RGBA) { return; //do not compress, already compressed @@ -220,7 +232,7 @@ void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) { row_task.in_mm_bytes = in_bytes; row_task.out_mm_bytes = out_bytes; - _digest_row_task(job_queue.job_params, row_task); + tasks.push_back(row_task); out_bytes += 16 * (bw / 4); } @@ -230,6 +242,13 @@ void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) { h = MAX(h / 2, 1); } + const CVTTCompressionRowTask *tasks_rb = tasks.ptr(); + + job_queue.job_tasks = &tasks_rb[0]; + job_queue.num_tasks = static_cast(tasks.size()); + WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&_digest_job_queue, &job_queue, WorkerThreadPool::get_singleton()->get_thread_count(), -1, true, SNAME("CVTT Compress")); + WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task); + p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data); }