diff options
Diffstat (limited to 'source/blender/nodes/composite/nodes/node_composite_blur.cc')
-rw-r--r-- | source/blender/nodes/composite/nodes/node_composite_blur.cc | 229 |
1 files changed, 26 insertions, 203 deletions
diff --git a/source/blender/nodes/composite/nodes/node_composite_blur.cc b/source/blender/nodes/composite/nodes/node_composite_blur.cc index a9372bdcfb7..6eacc685c7e 100644 --- a/source/blender/nodes/composite/nodes/node_composite_blur.cc +++ b/source/blender/nodes/composite/nodes/node_composite_blur.cc @@ -5,11 +5,7 @@ * \ingroup cmpnodes */ -#include <cstdint> - -#include "BLI_array.hh" #include "BLI_assert.h" -#include "BLI_index_range.hh" #include "BLI_math_base.hh" #include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh" @@ -19,12 +15,13 @@ #include "UI_interface.h" #include "UI_resources.h" -#include "RE_pipeline.h" - +#include "GPU_shader.h" #include "GPU_state.h" #include "GPU_texture.h" #include "COM_node_operation.hh" +#include "COM_symmetric_blur_weights.hh" +#include "COM_symmetric_separable_blur_weights.hh" #include "COM_utilities.hh" #include "node_composite_util.hh" @@ -92,192 +89,7 @@ static void node_composit_buts_blur(uiLayout *layout, bContext * /*C*/, PointerR using namespace blender::realtime_compositor; -/* A helper class that computes and caches a 1D GPU texture containing the weights of the separable - * filter of the given type and radius. The filter is assumed to be symmetric, because the filter - * functions are all even functions. Consequently, only the positive half of the filter is computed - * and the shader takes that into consideration. */ -class SymmetricSeparableBlurWeights { - private: - float radius_ = 1.0f; - int type_ = R_FILTER_GAUSS; - GPUTexture *texture_ = nullptr; - - public: - ~SymmetricSeparableBlurWeights() - { - if (texture_) { - GPU_texture_free(texture_); - } - } - - /* Check if a texture containing the weights was already computed for the given filter type and - * radius. If such texture exists, do nothing, otherwise, free the already computed texture and - * recompute it with the given filter type and radius. */ - void update(float radius, int type) - { - if (texture_ && type == type_ && radius == radius_) { - return; - } - - if (texture_) { - GPU_texture_free(texture_); - } - - /* The size of filter is double the radius plus 1, but since the filter is symmetric, we only - * compute half of it and no doubling happens. We add 1 to make sure the filter size is always - * odd and there is a center weight. */ - const int size = math::ceil(radius) + 1; - Array<float> weights(size); - - float sum = 0.0f; - - /* First, compute the center weight. */ - const float center_weight = RE_filter_value(type, 0.0f); - weights[0] = center_weight; - sum += center_weight; - - /* Second, compute the other weights in the positive direction, making sure to add double the - * weight to the sum of weights because the filter is symmetric and we only loop over half of - * it. Skip the center weight already computed by dropping the front index. */ - const float scale = radius > 0.0f ? 1.0f / radius : 0.0f; - for (const int i : weights.index_range().drop_front(1)) { - const float weight = RE_filter_value(type, i * scale); - weights[i] = weight; - sum += weight * 2.0f; - } - - /* Finally, normalize the weights. */ - for (const int i : weights.index_range()) { - weights[i] /= sum; - } - - texture_ = GPU_texture_create_1d("Weights", size, 1, GPU_R16F, weights.data()); - - type_ = type; - radius_ = radius; - } - - void bind_as_texture(GPUShader *shader, const char *texture_name) - { - const int texture_image_unit = GPU_shader_get_texture_binding(shader, texture_name); - GPU_texture_bind(texture_, texture_image_unit); - } - - void unbind_as_texture() - { - GPU_texture_unbind(texture_); - } -}; - -/* A helper class that computes and caches a 2D GPU texture containing the weights of the filter of - * the given type and radius. The filter is assumed to be symmetric, because the filter functions - * are evaluated on the normalized distance to the center. Consequently, only the upper right - * quadrant are computed and the shader takes that into consideration. */ -class SymmetricBlurWeights { - private: - int type_ = R_FILTER_GAUSS; - float2 radius_ = float2(1.0f); - GPUTexture *texture_ = nullptr; - - public: - ~SymmetricBlurWeights() - { - if (texture_) { - GPU_texture_free(texture_); - } - } - - /* Check if a texture containing the weights was already computed for the given filter type and - * radius. If such texture exists, do nothing, otherwise, free the already computed texture and - * recompute it with the given filter type and radius. */ - void update(float2 radius, int type) - { - if (texture_ && type == type_ && radius == radius_) { - return; - } - - if (texture_) { - GPU_texture_free(texture_); - } - - /* The full size of filter is double the radius plus 1, but since the filter is symmetric, we - * only compute a single quadrant of it and so no doubling happens. We add 1 to make sure the - * filter size is always odd and there is a center weight. */ - const float2 scale = math::safe_divide(float2(1.0f), radius); - const int2 size = int2(math::ceil(radius)) + int2(1); - Array<float> weights(size.x * size.y); - - float sum = 0.0f; - - /* First, compute the center weight. */ - const float center_weight = RE_filter_value(type, 0.0f); - weights[0] = center_weight; - sum += center_weight; - - /* Then, compute the weights along the positive x axis, making sure to add double the weight to - * the sum of weights because the filter is symmetric and we only loop over the positive half - * of the x axis. Skip the center weight already computed by dropping the front index. */ - for (const int x : IndexRange(size.x).drop_front(1)) { - const float weight = RE_filter_value(type, x * scale.x); - weights[x] = weight; - sum += weight * 2.0f; - } - - /* Then, compute the weights along the positive y axis, making sure to add double the weight to - * the sum of weights because the filter is symmetric and we only loop over the positive half - * of the y axis. Skip the center weight already computed by dropping the front index. */ - for (const int y : IndexRange(size.y).drop_front(1)) { - const float weight = RE_filter_value(type, y * scale.y); - weights[size.x * y] = weight; - sum += weight * 2.0f; - } - - /* Then, compute the other weights in the upper right quadrant, making sure to add quadruple - * the weight to the sum of weights because the filter is symmetric and we only loop over one - * quadrant of it. Skip the weights along the y and x axis already computed by dropping the - * front index. */ - for (const int y : IndexRange(size.y).drop_front(1)) { - for (const int x : IndexRange(size.x).drop_front(1)) { - const float weight = RE_filter_value(type, math::length(float2(x, y) * scale)); - weights[size.x * y + x] = weight; - sum += weight * 4.0f; - } - } - - /* Finally, normalize the weights. */ - for (const int y : IndexRange(size.y)) { - for (const int x : IndexRange(size.x)) { - weights[size.x * y + x] /= sum; - } - } - - texture_ = GPU_texture_create_2d("Weights", size.x, size.y, 1, GPU_R16F, weights.data()); - - type_ = type; - radius_ = radius; - } - - void bind_as_texture(GPUShader *shader, const char *texture_name) - { - const int texture_image_unit = GPU_shader_get_texture_binding(shader, texture_name); - GPU_texture_bind(texture_, texture_image_unit); - } - - void unbind_as_texture() - { - GPU_texture_unbind(texture_); - } -}; - class BlurOperation : public NodeOperation { - private: - /* Cached symmetric blur weights. */ - SymmetricBlurWeights blur_weights_; - /* Cached symmetric blur weights for the separable horizontal pass. */ - SymmetricSeparableBlurWeights blur_horizontal_weights_; - /* Cached symmetric blur weights for the separable vertical pass. */ - SymmetricSeparableBlurWeights blur_vertical_weights_; - public: using NodeOperation::NodeOperation; @@ -308,13 +120,16 @@ class BlurOperation : public NodeOperation { const Result &input_image = get_input("Image"); input_image.bind_as_texture(shader, "input_tx"); - blur_weights_.update(compute_blur_radius(), node_storage(bnode()).filtertype); - blur_weights_.bind_as_texture(shader, "weights_tx"); + const float2 blur_radius = compute_blur_radius(); + + const SymmetricBlurWeights &weights = context().cache_manager().get_symmetric_blur_weights( + node_storage(bnode()).filtertype, blur_radius); + weights.bind_as_texture(shader, "weights_tx"); Domain domain = compute_domain(); if (get_extend_bounds()) { /* Add a radius amount of pixels in both sides of the image, hence the multiply by 2. */ - domain.size += int2(math::ceil(compute_blur_radius())) * 2; + domain.size += int2(math::ceil(blur_radius)) * 2; } Result &output_image = get_result("Image"); @@ -326,7 +141,7 @@ class BlurOperation : public NodeOperation { GPU_shader_unbind(); output_image.unbind_as_image(); input_image.unbind_as_texture(); - blur_weights_.unbind_as_texture(); + weights.unbind_as_texture(); } GPUTexture *execute_separable_blur_horizontal_pass() @@ -341,12 +156,16 @@ class BlurOperation : public NodeOperation { const Result &input_image = get_input("Image"); input_image.bind_as_texture(shader, "input_tx"); - blur_horizontal_weights_.update(compute_blur_radius().x, node_storage(bnode()).filtertype); - blur_horizontal_weights_.bind_as_texture(shader, "weights_tx"); + const float2 blur_radius = compute_blur_radius(); + + const SymmetricSeparableBlurWeights &weights = + context().cache_manager().get_symmetric_separable_blur_weights( + node_storage(bnode()).filtertype, blur_radius.x); + weights.bind_as_texture(shader, "weights_tx"); Domain domain = compute_domain(); if (get_extend_bounds()) { - domain.size.x += int(math::ceil(compute_blur_radius().x)) * 2; + domain.size.x += int(math::ceil(blur_radius.x)) * 2; } /* We allocate an output image of a transposed size, that is, with a height equivalent to the @@ -367,7 +186,7 @@ class BlurOperation : public NodeOperation { GPU_shader_unbind(); input_image.unbind_as_texture(); - blur_horizontal_weights_.unbind_as_texture(); + weights.unbind_as_texture(); GPU_texture_image_unbind(horizontal_pass_result); return horizontal_pass_result; @@ -386,8 +205,12 @@ class BlurOperation : public NodeOperation { const int texture_image_unit = GPU_shader_get_texture_binding(shader, "input_tx"); GPU_texture_bind(horizontal_pass_result, texture_image_unit); - blur_vertical_weights_.update(compute_blur_radius().y, node_storage(bnode()).filtertype); - blur_vertical_weights_.bind_as_texture(shader, "weights_tx"); + const float2 blur_radius = compute_blur_radius(); + + const SymmetricSeparableBlurWeights &weights = + context().cache_manager().get_symmetric_separable_blur_weights( + node_storage(bnode()).filtertype, blur_radius.y); + weights.bind_as_texture(shader, "weights_tx"); Domain domain = compute_domain(); if (get_extend_bounds()) { @@ -405,7 +228,7 @@ class BlurOperation : public NodeOperation { GPU_shader_unbind(); output_image.unbind_as_image(); - blur_vertical_weights_.unbind_as_texture(); + weights.unbind_as_texture(); GPU_texture_unbind(horizontal_pass_result); } @@ -501,7 +324,7 @@ void register_node_type_cmp_blur() ntype.declare = file_ns::cmp_node_blur_declare; ntype.draw_buttons = file_ns::node_composit_buts_blur; ntype.flag |= NODE_PREVIEW; - node_type_init(&ntype, file_ns::node_composit_init_blur); + ntype.initfunc = file_ns::node_composit_init_blur; node_type_storage( &ntype, "NodeBlurData", node_free_standard_storage, node_copy_standard_storage); ntype.get_compositor_operation = file_ns::get_compositor_operation; |