diff options
author | Manuel Castilla <manzanillawork@gmail.com> | 2021-07-26 20:16:25 +0300 |
---|---|---|
committer | Manuel Castilla <manzanillawork@gmail.com> | 2021-07-26 21:13:55 +0300 |
commit | e33814ef6baece51cbf9249f4e450eaa4075e707 (patch) | |
tree | fc391e9fe063272bb54ffe3ce06e7e5c6d993dc1 /source/blender | |
parent | 883fb49d4f1101d5614049c53196546e308a6d33 (diff) |
Compositor: Full frame Levels node
Adds full frame implementation to this node operations.
No functional changes.
Reviewed By: jbakker
Differential Revision: https://developer.blender.org/D11749
Diffstat (limited to 'source/blender')
6 files changed, 202 insertions, 7 deletions
diff --git a/source/blender/compositor/intern/COM_ExecutionSystem.cc b/source/blender/compositor/intern/COM_ExecutionSystem.cc index cd2738fc2c7..a449d86deb9 100644 --- a/source/blender/compositor/intern/COM_ExecutionSystem.cc +++ b/source/blender/compositor/intern/COM_ExecutionSystem.cc @@ -82,6 +82,7 @@ ExecutionSystem::ExecutionSystem(RenderData *rd, BLI_assert_msg(0, "Non implemented execution model"); break; } + num_work_threads_ = WorkScheduler::get_num_cpu_threads(); } ExecutionSystem::~ExecutionSystem() @@ -130,7 +131,7 @@ void ExecutionSystem::execute_work(const rcti &work_rect, /* Split work vertically to maximize continuous memory. */ const int work_height = BLI_rcti_size_y(&work_rect); - const int num_sub_works = MIN2(WorkScheduler::get_num_cpu_threads(), work_height); + const int num_sub_works = MIN2(num_work_threads_, work_height); const int split_height = num_sub_works == 0 ? 0 : work_height / num_sub_works; int remaining_height = work_height - split_height * num_sub_works; diff --git a/source/blender/compositor/intern/COM_ExecutionSystem.h b/source/blender/compositor/intern/COM_ExecutionSystem.h index 38c3432a8ec..ad6f9fd4bdb 100644 --- a/source/blender/compositor/intern/COM_ExecutionSystem.h +++ b/source/blender/compositor/intern/COM_ExecutionSystem.h @@ -31,6 +31,7 @@ class ExecutionGroup; #include "DNA_node_types.h" #include "BLI_vector.hh" +#include "atomic_ops.h" namespace blender::compositor { @@ -150,6 +151,11 @@ class ExecutionSystem { */ ExecutionModel *execution_model_; + /** + * Number of cpu threads available for work execution. + */ + int num_work_threads_; + ThreadMutex work_mutex_; ThreadCondition work_finished_cond_; @@ -201,6 +207,27 @@ class ExecutionSystem { void execute_work(const rcti &work_rect, std::function<void(const rcti &split_rect)> work_func); + /** + * Multi-threadedly execute given work function passing work_rect splits as argument. On + * finished, caller thread will call reduce_func for each thread result. + */ + template<typename TResult> + void execute_work(const rcti &work_rect, + std::function<TResult(const rcti &split_rect)> work_func, + TResult &join, + std::function<void(TResult &join, const TResult &chunk)> reduce_func) + { + Array<TResult> chunks(num_work_threads_); + int num_started = 0; + execute_work(work_rect, [&](const rcti &split_rect) { + const int current = atomic_fetch_and_add_int32(&num_started, 1); + chunks[current] = work_func(split_rect); + }); + for (const int i : IndexRange(num_started)) { + reduce_func(join, chunks[i]); + } + } + bool is_breaked() const; private: diff --git a/source/blender/compositor/operations/COM_CalculateMeanOperation.cc b/source/blender/compositor/operations/COM_CalculateMeanOperation.cc index a7ea49aed8d..7457ac9e227 100644 --- a/source/blender/compositor/operations/COM_CalculateMeanOperation.cc +++ b/source/blender/compositor/operations/COM_CalculateMeanOperation.cc @@ -19,6 +19,7 @@ #include "COM_CalculateMeanOperation.h" #include "BLI_math.h" #include "BLI_utildefines.h" +#include "COM_ExecutionSystem.h" #include "IMB_colormanagement.h" @@ -128,4 +129,93 @@ void CalculateMeanOperation::calculateMean(MemoryBuffer *tile) this->m_result = sum / pixels; } +void CalculateMeanOperation::setSetting(int setting) +{ + this->m_setting = setting; + switch (setting) { + case 1: { + setting_func_ = IMB_colormanagement_get_luminance; + break; + } + case 2: { + setting_func_ = [](const float *elem) { return elem[0]; }; + break; + } + case 3: { + setting_func_ = [](const float *elem) { return elem[1]; }; + break; + } + case 4: { + setting_func_ = [](const float *elem) { return elem[2]; }; + break; + } + case 5: { + setting_func_ = [](const float *elem) { + float yuv[3]; + rgb_to_yuv(elem[0], elem[1], elem[2], &yuv[0], &yuv[1], &yuv[2], BLI_YUV_ITU_BT709); + return yuv[0]; + }; + break; + } + } +} + +void CalculateMeanOperation::get_area_of_interest(int input_idx, + const rcti &UNUSED(output_area), + rcti &r_input_area) +{ + BLI_assert(input_idx == 0); + NodeOperation *operation = getInputOperation(input_idx); + r_input_area.xmin = 0; + r_input_area.ymin = 0; + r_input_area.xmax = operation->getWidth(); + r_input_area.ymax = operation->getHeight(); +} + +void CalculateMeanOperation::update_memory_buffer_started(MemoryBuffer *UNUSED(output), + const rcti &UNUSED(area), + Span<MemoryBuffer *> inputs) +{ + if (!this->m_iscalculated) { + MemoryBuffer *input = inputs[0]; + m_result = calc_mean(input); + this->m_iscalculated = true; + } +} + +void CalculateMeanOperation::update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> UNUSED(inputs)) +{ + output->fill(area, &m_result); +} + +float CalculateMeanOperation::calc_mean(const MemoryBuffer *input) +{ + PixelsSum total = {0}; + exec_system_->execute_work<PixelsSum>( + input->get_rect(), + [=](const rcti &split) { return calc_area_sum(input, split); }, + total, + [](PixelsSum &join, const PixelsSum &chunk) { + join.sum += chunk.sum; + join.num_pixels += chunk.num_pixels; + }); + return total.num_pixels == 0 ? 0.0f : total.sum / total.num_pixels; +} + +using PixelsSum = CalculateMeanOperation::PixelsSum; +PixelsSum CalculateMeanOperation::calc_area_sum(const MemoryBuffer *input, const rcti &area) +{ + PixelsSum result = {0}; + for (const float *elem : input->get_buffer_area(area)) { + if (elem[3] <= 0.0f) { + continue; + } + result.sum += setting_func_(elem); + result.num_pixels++; + } + return result; +} + } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_CalculateMeanOperation.h b/source/blender/compositor/operations/COM_CalculateMeanOperation.h index 8b3bf281c93..779ca79b38a 100644 --- a/source/blender/compositor/operations/COM_CalculateMeanOperation.h +++ b/source/blender/compositor/operations/COM_CalculateMeanOperation.h @@ -18,8 +18,9 @@ #pragma once -#include "COM_NodeOperation.h" +#include "COM_MultiThreadedOperation.h" #include "DNA_node_types.h" +#include <functional> namespace blender::compositor { @@ -27,7 +28,13 @@ namespace blender::compositor { * \brief base class of CalculateMean, implementing the simple CalculateMean * \ingroup operation */ -class CalculateMeanOperation : public NodeOperation { +class CalculateMeanOperation : public MultiThreadedOperation { + public: + struct PixelsSum { + float sum; + int num_pixels; + }; + protected: /** * \brief Cached reference to the reader @@ -37,6 +44,7 @@ class CalculateMeanOperation : public NodeOperation { bool m_iscalculated; float m_result; int m_setting; + std::function<float(const float *elem)> setting_func_; public: CalculateMeanOperation(); @@ -61,13 +69,24 @@ class CalculateMeanOperation : public NodeOperation { bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override; - void setSetting(int setting) - { - this->m_setting = setting; - } + void setSetting(int setting); + + void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override; + + virtual void update_memory_buffer_started(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; + + virtual void update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; protected: void calculateMean(MemoryBuffer *tile); + float calc_mean(const MemoryBuffer *input); + + private: + PixelsSum calc_area_sum(const MemoryBuffer *input, const rcti &area); }; } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc index ed554b9ac06..494b66cb888 100644 --- a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc +++ b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc @@ -19,6 +19,7 @@ #include "COM_CalculateStandardDeviationOperation.h" #include "BLI_math.h" #include "BLI_utildefines.h" +#include "COM_ExecutionSystem.h" #include "IMB_colormanagement.h" @@ -96,4 +97,50 @@ void *CalculateStandardDeviationOperation::initializeTileData(rcti *rect) return nullptr; } +void CalculateStandardDeviationOperation::update_memory_buffer_started( + MemoryBuffer *UNUSED(output), const rcti &UNUSED(area), Span<MemoryBuffer *> inputs) +{ + if (!this->m_iscalculated) { + const MemoryBuffer *input = inputs[0]; + const float mean = CalculateMeanOperation::calc_mean(input); + + PixelsSum total = {0}; + exec_system_->execute_work<PixelsSum>( + input->get_rect(), + [=](const rcti &split) { return calc_area_sum(input, split, mean); }, + total, + [](PixelsSum &join, const PixelsSum &chunk) { + join.sum += chunk.sum; + join.num_pixels += chunk.num_pixels; + }); + this->m_standardDeviation = total.num_pixels <= 1 ? + 0.0f : + sqrt(total.sum / (float)(total.num_pixels - 1)); + this->m_iscalculated = true; + } +} + +void CalculateStandardDeviationOperation::update_memory_buffer_partial( + MemoryBuffer *output, const rcti &area, Span<MemoryBuffer *> UNUSED(inputs)) +{ + output->fill(area, &m_standardDeviation); +} + +using PixelsSum = CalculateMeanOperation::PixelsSum; +PixelsSum CalculateStandardDeviationOperation::calc_area_sum(const MemoryBuffer *input, + const rcti &area, + const float mean) +{ + PixelsSum result = {0}; + for (const float *elem : input->get_buffer_area(area)) { + if (elem[3] <= 0.0f) { + continue; + } + const float value = setting_func_(elem); + result.sum += (value - mean) * (value - mean); + result.num_pixels++; + } + return result; +} + } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h index bc4aca69546..20de4cf4701 100644 --- a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h +++ b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h @@ -40,6 +40,17 @@ class CalculateStandardDeviationOperation : public CalculateMeanOperation { void executePixel(float output[4], int x, int y, void *data) override; void *initializeTileData(rcti *rect) override; + + void update_memory_buffer_started(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; + + void update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; + + private: + PixelsSum calc_area_sum(const MemoryBuffer *input, const rcti &area, float mean); }; } // namespace blender::compositor |