diff options
Diffstat (limited to 'source/blender/compositor')
21 files changed, 863 insertions, 45 deletions
diff --git a/source/blender/compositor/intern/COM_Debug.cc b/source/blender/compositor/intern/COM_Debug.cc index d184e5540ea..a670af5eaca 100644 --- a/source/blender/compositor/intern/COM_Debug.cc +++ b/source/blender/compositor/intern/COM_Debug.cc @@ -305,7 +305,7 @@ bool DebugInfo::graphviz_system(const ExecutionSystem *system, char *str, int ma for (NodeOperation *operation : group->operations_) { - sprintf(strbuf, "_%p", group); + BLI_snprintf(strbuf, sizeof(strbuf), "_%p", group); op_groups[operation].push_back(std::string(strbuf)); len += graphviz_operation( diff --git a/source/blender/compositor/nodes/COM_OutputFileNode.cc b/source/blender/compositor/nodes/COM_OutputFileNode.cc index fc4270cc222..50989f73986 100644 --- a/source/blender/compositor/nodes/COM_OutputFileNode.cc +++ b/source/blender/compositor/nodes/COM_OutputFileNode.cc @@ -65,7 +65,7 @@ void OutputFileNode::convert_to_operations(NodeConverter &converter, if (storage->format.imtype == R_IMF_IMTYPE_MULTILAYER) { const bool use_half_float = (storage->format.depth == R_IMF_CHAN_DEPTH_16); - /* single output operation for the multilayer file */ + /* Single output operation for the multi-layer file. */ OutputOpenExrMultiLayerOperation *output_operation; if (is_multiview && storage->format.views_format == R_IMF_VIEWS_MULTIVIEW) { diff --git a/source/blender/compositor/operations/COM_MovieDistortionOperation.cc b/source/blender/compositor/operations/COM_MovieDistortionOperation.cc index b89a48f2a39..353f3da14d7 100644 --- a/source/blender/compositor/operations/COM_MovieDistortionOperation.cc +++ b/source/blender/compositor/operations/COM_MovieDistortionOperation.cc @@ -78,34 +78,41 @@ void MovieDistortionOperation::execute_pixel_sampled(float output[4], float y, PixelSampler /*sampler*/) { - if (distortion_ != nullptr) { - /* float overscan = 0.0f; */ - const float pixel_aspect = pixel_aspect_; - const float w = float(this->get_width()) /* / (1 + overscan) */; - const float h = float(this->get_height()) /* / (1 + overscan) */; - const float aspx = w / float(calibration_width_); - const float aspy = h / float(calibration_height_); - float in[2]; - float out[2]; - - in[0] = (x /* - 0.5 * overscan * w */) / aspx; - in[1] = (y /* - 0.5 * overscan * h */) / aspy / pixel_aspect; + const int width = this->get_width(); + const int height = this->get_height(); + if (distortion_ == nullptr || width == 0 || height == 0) { + /* When there is no precomputed distortion pass-through the coordinate as-is to the input + * samples. + * If the frame size is zero do the same and bypass any math. In theory it is probably more + * correct to zero the output but it is easier and safe to let the input to do so than to deal + * with possible different number of channels here. */ + input_operation_->read_sampled(output, x, y, PixelSampler::Bilinear); + return; + } - if (apply_) { - BKE_tracking_distortion_undistort_v2(distortion_, in, out); - } - else { - BKE_tracking_distortion_distort_v2(distortion_, in, out); - } + /* float overscan = 0.0f; */ + const float w = float(width) /* / (1 + overscan) */; + const float h = float(height) /* / (1 + overscan) */; + const float pixel_aspect = pixel_aspect_; + const float aspx = w / float(calibration_width_); + const float aspy = h / float(calibration_height_); + float in[2]; + float out[2]; - float u = out[0] * aspx /* + 0.5 * overscan * w */, - v = (out[1] * aspy /* + 0.5 * overscan * h */) * pixel_aspect; + in[0] = (x /* - 0.5 * overscan * w */) / aspx; + in[1] = (y /* - 0.5 * overscan * h */) / aspy / pixel_aspect; - input_operation_->read_sampled(output, u, v, PixelSampler::Bilinear); + if (apply_) { + BKE_tracking_distortion_undistort_v2(distortion_, in, out); } else { - input_operation_->read_sampled(output, x, y, PixelSampler::Bilinear); + BKE_tracking_distortion_distort_v2(distortion_, in, out); } + + float u = out[0] * aspx /* + 0.5 * overscan * w */, + v = (out[1] * aspy /* + 0.5 * overscan * h */) * pixel_aspect; + + input_operation_->read_sampled(output, u, v, PixelSampler::Bilinear); } bool MovieDistortionOperation::determine_depending_area_of_interest( diff --git a/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.h b/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.h index e36999e5cf1..70773c1a559 100644 --- a/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.h +++ b/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.h @@ -31,7 +31,7 @@ class OutputOpenExrSingleLayerMultiViewOperation : public OutputSingleLayerOpera void deinit_execution() override; }; -/* Writes inputs into OpenEXR multilayer channels. */ +/** Writes inputs into OpenEXR multi-layer channels. */ class OutputOpenExrMultiLayerMultiViewOperation : public OutputOpenExrMultiLayerOperation { private: public: diff --git a/source/blender/compositor/operations/COM_OutputFileOperation.h b/source/blender/compositor/operations/COM_OutputFileOperation.h index df1d68838d9..716bede8035 100644 --- a/source/blender/compositor/operations/COM_OutputFileOperation.h +++ b/source/blender/compositor/operations/COM_OutputFileOperation.h @@ -71,7 +71,7 @@ struct OutputOpenExrLayer { SocketReader *image_input; }; -/* Writes inputs into OpenEXR multilayer channels. */ +/* Writes inputs into OpenEXR multi-layer channels. */ class OutputOpenExrMultiLayerOperation : public MultiThreadedOperation { protected: const Scene *scene_; diff --git a/source/blender/compositor/realtime_compositor/CMakeLists.txt b/source/blender/compositor/realtime_compositor/CMakeLists.txt index 2402adcadaf..b4352248b5b 100644 --- a/source/blender/compositor/realtime_compositor/CMakeLists.txt +++ b/source/blender/compositor/realtime_compositor/CMakeLists.txt @@ -3,6 +3,7 @@ set(INC . algorithms + cached_resources ../../blenkernel ../../blenlib ../../gpu @@ -10,6 +11,7 @@ set(INC ../../makesdna ../../makesrna ../../nodes + ../../render ../../gpu/intern ../../../../intern/guardedalloc ) @@ -31,6 +33,7 @@ set(SRC intern/shader_node.cc intern/shader_operation.cc intern/simple_operation.cc + intern/static_cache_manager.cc intern/static_shader_manager.cc intern/texture_pool.cc intern/utilities.cc @@ -51,6 +54,7 @@ set(SRC COM_shader_node.hh COM_shader_operation.hh COM_simple_operation.hh + COM_static_cache_manager.hh COM_static_shader_manager.hh COM_texture_pool.hh COM_utilities.hh @@ -58,12 +62,22 @@ set(SRC algorithms/intern/algorithm_parallel_reduction.cc algorithms/COM_algorithm_parallel_reduction.hh + + cached_resources/intern/morphological_distance_feather_weights.cc + cached_resources/intern/symmetric_blur_weights.cc + cached_resources/intern/symmetric_separable_blur_weights.cc + + cached_resources/COM_cached_resource.hh + cached_resources/COM_morphological_distance_feather_weights.hh + cached_resources/COM_symmetric_blur_weights.hh + cached_resources/COM_symmetric_separable_blur_weights.hh ) set(LIB bf_gpu bf_nodes bf_imbuf + bf_render bf_blenlib bf_blenkernel ) diff --git a/source/blender/compositor/realtime_compositor/COM_context.hh b/source/blender/compositor/realtime_compositor/COM_context.hh index b5c8cea641f..80fb4f70ca4 100644 --- a/source/blender/compositor/realtime_compositor/COM_context.hh +++ b/source/blender/compositor/realtime_compositor/COM_context.hh @@ -9,6 +9,7 @@ #include "GPU_texture.h" +#include "COM_static_cache_manager.hh" #include "COM_static_shader_manager.hh" #include "COM_texture_pool.hh" @@ -22,14 +23,17 @@ namespace blender::realtime_compositor { * providing input data like render passes and the active scene, as well as references to the data * where the output of the evaluator will be written. The class also provides a reference to the * texture pool which should be implemented by the caller and provided during construction. - * Finally, the class have an instance of a static shader manager for convenient shader - * acquisition. */ + * Finally, the class have an instance of a static shader manager and a static resource manager + * for acquiring cached shaders and resources efficiently. */ class Context { private: /* A texture pool that can be used to allocate textures for the compositor efficiently. */ TexturePool &texture_pool_; /* A static shader manager that can be used to acquire shaders for the compositor efficiently. */ StaticShaderManager shader_manager_; + /* A static cache manager that can be used to acquire cached resources for the compositor + * efficiently. */ + StaticCacheManager cache_manager_; public: Context(TexturePool &texture_pool); @@ -67,6 +71,9 @@ class Context { /* Get a reference to the static shader manager of this context. */ StaticShaderManager &shader_manager(); + + /* Get a reference to the static cache manager of this context. */ + StaticCacheManager &cache_manager(); }; } // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/COM_scheduler.hh b/source/blender/compositor/realtime_compositor/COM_scheduler.hh index 4f778b32145..9f3bc14ae17 100644 --- a/source/blender/compositor/realtime_compositor/COM_scheduler.hh +++ b/source/blender/compositor/realtime_compositor/COM_scheduler.hh @@ -16,6 +16,6 @@ using Schedule = VectorSet<DNode>; /* Computes the execution schedule of the node tree. This is essentially a post-order depth first * traversal of the node tree from the output node to the leaf input nodes, with informed order of * traversal of dependencies based on a heuristic estimation of the number of needed buffers. */ -Schedule compute_schedule(DerivedNodeTree &tree); +Schedule compute_schedule(const DerivedNodeTree &tree); } // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh b/source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh new file mode 100644 index 00000000000..20fbb156879 --- /dev/null +++ b/source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +#include <memory> + +#include "BLI_map.hh" +#include "BLI_math_vec_types.hh" + +#include "COM_morphological_distance_feather_weights.hh" +#include "COM_symmetric_blur_weights.hh" +#include "COM_symmetric_separable_blur_weights.hh" + +namespace blender::realtime_compositor { + +/* ------------------------------------------------------------------------------------------------- + * Static Cache Manager + * + * A static cache manager is a collection of cached resources that can be retrieved when needed and + * created if not already available. In particular, each cached resource type has its own Map in + * the class, where all instances of that cached resource type are stored and tracked. See the + * CachedResource class for more information. + * + * The manager deletes the cached resources that are no longer needed. A cached resource is said to + * be not needed when it was not used in the previous evaluation. This is done through the + * following mechanism: + * + * - Before every evaluation, do the following: + * 1. All resources whose CachedResource::needed flag is false are deleted. + * 2. The CachedResource::needed flag of all remaining resources is set to false. + * - During evaluation, when retrieving any cached resource, set its CachedResource::needed flag to + * true. + * + * In effect, any resource that was used in the previous evaluation but was not used in the current + * evaluation will be deleted before the next evaluation. This mechanism is implemented in the + * reset() method of the class, which should be called before every evaluation. */ +class StaticCacheManager { + private: + /* A map that stores all SymmetricBlurWeights cached resources. */ + Map<SymmetricBlurWeightsKey, std::unique_ptr<SymmetricBlurWeights>> symmetric_blur_weights_; + + /* A map that stores all SymmetricSeparableBlurWeights cached resources. */ + Map<SymmetricSeparableBlurWeightsKey, std::unique_ptr<SymmetricSeparableBlurWeights>> + symmetric_separable_blur_weights_; + + /* A map that stores all MorphologicalDistanceFeatherWeights cached resources. */ + Map<MorphologicalDistanceFeatherWeightsKey, std::unique_ptr<MorphologicalDistanceFeatherWeights>> + morphological_distance_feather_weights_; + + public: + /* Reset the cache manager by deleting the cached resources that are no longer needed because + * they weren't used in the last evaluation and prepare the remaining cached resources to track + * their needed status in the next evaluation. See the class description for more information. + * This should be called before every evaluation. */ + void reset(); + + /* Check if there is an available SymmetricBlurWeights cached resource with the given parameters + * in the manager, if one exists, return it, otherwise, return a newly created one and add it to + * the manager. In both cases, tag the cached resource as needed to keep it cached for the next + * evaluation. */ + SymmetricBlurWeights &get_symmetric_blur_weights(int type, float2 radius); + + /* Check if there is an available SymmetricSeparableBlurWeights cached resource with the given + * parameters in the manager, if one exists, return it, otherwise, return a newly created one and + * add it to the manager. In both cases, tag the cached resource as needed to keep it cached for + * the next evaluation. */ + SymmetricSeparableBlurWeights &get_symmetric_separable_blur_weights(int type, float radius); + + /* Check if there is an available MorphologicalDistanceFeatherWeights cached resource with the + * given parameters in the manager, if one exists, return it, otherwise, return a newly created + * one and add it to the manager. In both cases, tag the cached resource as needed to keep it + * cached for the next evaluation. */ + MorphologicalDistanceFeatherWeights &get_morphological_distance_feather_weights(int type, + int radius); +}; + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh b/source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh new file mode 100644 index 00000000000..fe3158ef52d --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +namespace blender::realtime_compositor { + +/* ------------------------------------------------------------------------------------------------- + * Cached Resource. + * + * A cached resource is any resource that can be cached across compositor evaluations and across + * multiple operations. Cached resources are managed by an instance of a StaticCacheManager and are + * freed when they are no longer needed, a state which is represented by the `needed` member in the + * class. For more information on the caching mechanism, see the StaticCacheManager class. + * + * To add a new cached resource: + * + * - Create a derived class from CachedResource to represent the resource. + * - Create a key class that can be used in a Map to identify the resource. + * - Add a new Map to StaticCacheManager mapping the key to the resource. + * - Reset the contents of the added map in StaticCacheManager::reset. + * - Add an appropriate getter method in StaticCacheManager. + * + * See the existing cached resources for reference. */ +class CachedResource { + public: + /* A flag that represents the needed status of the cached resource. See the StaticCacheManager + * class for more information on how this member is utilized in the caching mechanism. */ + bool needed = true; +}; + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh b/source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh new file mode 100644 index 00000000000..cd6827bdd6b --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +#include <cstdint> + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "COM_cached_resource.hh" + +namespace blender::realtime_compositor { + +/* ------------------------------------------------------------------------------------------------ + * Morphological Distance Feather Key. + */ +class MorphologicalDistanceFeatherWeightsKey { + public: + int type; + float radius; + + MorphologicalDistanceFeatherWeightsKey(int type, float radius); + + uint64_t hash() const; +}; + +bool operator==(const MorphologicalDistanceFeatherWeightsKey &a, + const MorphologicalDistanceFeatherWeightsKey &b); + +/* ------------------------------------------------------------------------------------------------- + * Morphological Distance Feather Weights. + * + * A cached resource that computes and caches 1D GPU textures containing the weights of the + * separable Gaussian filter of the given radius as well as an inverse distance falloff of the + * given type and radius. The weights and falloffs are symmetric, because the Gaussian and falloff + * functions are all even functions. Consequently, only the positive half of the filter is computed + * and the shader takes that into consideration. */ +class MorphologicalDistanceFeatherWeights : public CachedResource { + private: + GPUTexture *weights_texture_ = nullptr; + GPUTexture *distance_falloffs_texture_ = nullptr; + + public: + MorphologicalDistanceFeatherWeights(int type, int radius); + + ~MorphologicalDistanceFeatherWeights(); + + void compute_weights(int radius); + + void compute_distance_falloffs(int type, int radius); + + void bind_weights_as_texture(GPUShader *shader, const char *texture_name) const; + + void unbind_weights_as_texture() const; + + void bind_distance_falloffs_as_texture(GPUShader *shader, const char *texture_name) const; + + void unbind_distance_falloffs_as_texture() const; +}; + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_blur_weights.hh b/source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_blur_weights.hh new file mode 100644 index 00000000000..05d3c7c6f3e --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_blur_weights.hh @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +#include <cstdint> + +#include "BLI_math_vec_types.hh" + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "COM_cached_resource.hh" + +namespace blender::realtime_compositor { + +/* ------------------------------------------------------------------------------------------------ + * Symmetric Blur Weights Key. + */ +class SymmetricBlurWeightsKey { + public: + int type; + float2 radius; + + SymmetricBlurWeightsKey(int type, float2 radius); + + uint64_t hash() const; +}; + +bool operator==(const SymmetricBlurWeightsKey &a, const SymmetricBlurWeightsKey &b); + +/* ------------------------------------------------------------------------------------------------- + * Symmetric Blur Weights. + * + * A cached resource 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 : public CachedResource { + private: + GPUTexture *texture_ = nullptr; + + public: + SymmetricBlurWeights(int type, float2 radius); + + ~SymmetricBlurWeights(); + + void bind_as_texture(GPUShader *shader, const char *texture_name) const; + + void unbind_as_texture() const; +}; + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_separable_blur_weights.hh b/source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_separable_blur_weights.hh new file mode 100644 index 00000000000..85e75e4535d --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_separable_blur_weights.hh @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +#include <cstdint> + +#include "BLI_math_vec_types.hh" + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "COM_cached_resource.hh" + +namespace blender::realtime_compositor { + +/* ------------------------------------------------------------------------------------------------ + * Symmetric Separable Blur Weights Key. + */ +class SymmetricSeparableBlurWeightsKey { + public: + int type; + float radius; + + SymmetricSeparableBlurWeightsKey(int type, float radius); + + uint64_t hash() const; +}; + +bool operator==(const SymmetricSeparableBlurWeightsKey &a, + const SymmetricSeparableBlurWeightsKey &b); + +/* ------------------------------------------------------------------------------------------------- + * Symmetric Separable Blur Weights. + * + * A cached resource 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 : public CachedResource { + private: + GPUTexture *texture_ = nullptr; + + public: + SymmetricSeparableBlurWeights(int type, float radius); + + ~SymmetricSeparableBlurWeights(); + + void bind_as_texture(GPUShader *shader, const char *texture_name) const; + + void unbind_as_texture() const; +}; + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/intern/morphological_distance_feather_weights.cc b/source/blender/compositor/realtime_compositor/cached_resources/intern/morphological_distance_feather_weights.cc new file mode 100644 index 00000000000..eac88b907b8 --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/intern/morphological_distance_feather_weights.cc @@ -0,0 +1,159 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <cmath> +#include <cstdint> + +#include "BLI_array.hh" +#include "BLI_hash.hh" +#include "BLI_index_range.hh" + +#include "RE_pipeline.h" + +#include "DNA_scene_types.h" + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "COM_morphological_distance_feather_weights.hh" + +namespace blender::realtime_compositor { + +/* -------------------------------------------------------------------- + * Morphological Distance Feather Weights Key. + */ + +MorphologicalDistanceFeatherWeightsKey::MorphologicalDistanceFeatherWeightsKey(int type, + float radius) + : type(type), radius(radius) +{ +} + +uint64_t MorphologicalDistanceFeatherWeightsKey::hash() const +{ + return get_default_hash_2(type, radius); +} + +bool operator==(const MorphologicalDistanceFeatherWeightsKey &a, + const MorphologicalDistanceFeatherWeightsKey &b) +{ + return a.type == b.type && a.radius == b.radius; +} + +/* -------------------------------------------------------------------- + * Morphological Distance Feather Weights. + */ + +MorphologicalDistanceFeatherWeights::MorphologicalDistanceFeatherWeights(int type, int radius) +{ + compute_weights(radius); + compute_distance_falloffs(type, radius); +} + +MorphologicalDistanceFeatherWeights::~MorphologicalDistanceFeatherWeights() +{ + GPU_texture_free(weights_texture_); + GPU_texture_free(distance_falloffs_texture_); +} + +void MorphologicalDistanceFeatherWeights::compute_weights(int radius) +{ + /* 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 = radius + 1; + Array<float> weights(size); + + float sum = 0.0f; + + /* First, compute the center weight. */ + const float center_weight = RE_filter_value(R_FILTER_GAUSS, 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(R_FILTER_GAUSS, i * scale); + weights[i] = weight; + sum += weight * 2.0f; + } + + /* Finally, normalize the weights. */ + for (const int i : weights.index_range()) { + weights[i] /= sum; + } + + weights_texture_ = GPU_texture_create_1d("Weights", size, 1, GPU_R16F, weights.data()); +} + +/* Computes a falloff that is equal to 1 at an input of zero and decrease to zero at an input of 1, + * with the rate of decrease depending on the falloff type. */ +static float compute_distance_falloff(int type, float x) +{ + x = 1.0f - x; + + switch (type) { + case PROP_SMOOTH: + return 3.0f * x * x - 2.0f * x * x * x; + case PROP_SPHERE: + return std::sqrt(2.0f * x - x * x); + case PROP_ROOT: + return std::sqrt(x); + case PROP_SHARP: + return x * x; + case PROP_INVSQUARE: + return x * (2.0f - x); + case PROP_LIN: + return x; + default: + BLI_assert_unreachable(); + return x; + } +} + +void MorphologicalDistanceFeatherWeights::compute_distance_falloffs(int type, int radius) +{ + /* The size of the distance falloffs is double the radius plus 1, but since the falloffs are + * symmetric, we only compute half of them and no doubling happens. We add 1 to make sure the + * falloffs size is always odd and there is a center falloff. */ + const int size = radius + 1; + Array<float> falloffs(size); + + /* Compute the distance falloffs in the positive direction only, because the falloffs are + * symmetric. */ + const float scale = radius > 0.0f ? 1.0f / radius : 0.0f; + for (const int i : falloffs.index_range()) { + falloffs[i] = compute_distance_falloff(type, i * scale); + } + + distance_falloffs_texture_ = GPU_texture_create_1d( + "Distance Factors", size, 1, GPU_R16F, falloffs.data()); +} + +void MorphologicalDistanceFeatherWeights::bind_weights_as_texture(GPUShader *shader, + const char *texture_name) const +{ + const int texture_image_unit = GPU_shader_get_texture_binding(shader, texture_name); + GPU_texture_bind(weights_texture_, texture_image_unit); +} + +void MorphologicalDistanceFeatherWeights::unbind_weights_as_texture() const +{ + GPU_texture_unbind(weights_texture_); +} + +void MorphologicalDistanceFeatherWeights::bind_distance_falloffs_as_texture( + GPUShader *shader, const char *texture_name) const +{ + const int texture_image_unit = GPU_shader_get_texture_binding(shader, texture_name); + GPU_texture_bind(distance_falloffs_texture_, texture_image_unit); +} + +void MorphologicalDistanceFeatherWeights::unbind_distance_falloffs_as_texture() const +{ + GPU_texture_unbind(distance_falloffs_texture_); +} + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_blur_weights.cc b/source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_blur_weights.cc new file mode 100644 index 00000000000..a22d32a8e18 --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_blur_weights.cc @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <cstdint> + +#include "BLI_array.hh" +#include "BLI_hash.hh" +#include "BLI_index_range.hh" +#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" + +#include "RE_pipeline.h" + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "COM_symmetric_blur_weights.hh" + +namespace blender::realtime_compositor { + +/* -------------------------------------------------------------------- + * Symmetric Blur Weights Key. + */ + +SymmetricBlurWeightsKey::SymmetricBlurWeightsKey(int type, float2 radius) + : type(type), radius(radius) +{ +} + +uint64_t SymmetricBlurWeightsKey::hash() const +{ + return get_default_hash_3(type, radius.x, radius.y); +} + +bool operator==(const SymmetricBlurWeightsKey &a, const SymmetricBlurWeightsKey &b) +{ + return a.type == b.type && a.radius == b.radius; +} + +/* -------------------------------------------------------------------- + * Symmetric Blur Weights. + */ + +SymmetricBlurWeights::SymmetricBlurWeights(int type, float2 radius) +{ + /* 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()); +} + +SymmetricBlurWeights::~SymmetricBlurWeights() +{ + GPU_texture_free(texture_); +} + +void SymmetricBlurWeights::bind_as_texture(GPUShader *shader, const char *texture_name) const +{ + const int texture_image_unit = GPU_shader_get_texture_binding(shader, texture_name); + GPU_texture_bind(texture_, texture_image_unit); +} + +void SymmetricBlurWeights::unbind_as_texture() const +{ + GPU_texture_unbind(texture_); +} + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_separable_blur_weights.cc b/source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_separable_blur_weights.cc new file mode 100644 index 00000000000..b8c47d5a5d0 --- /dev/null +++ b/source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_separable_blur_weights.cc @@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <cstdint> + +#include "BLI_array.hh" +#include "BLI_hash.hh" +#include "BLI_index_range.hh" +#include "BLI_math_base.hh" + +#include "RE_pipeline.h" + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "COM_symmetric_separable_blur_weights.hh" + +namespace blender::realtime_compositor { + +/* -------------------------------------------------------------------- + * Symmetric Separable Blur Weights Key. + */ + +SymmetricSeparableBlurWeightsKey::SymmetricSeparableBlurWeightsKey(int type, float radius) + : type(type), radius(radius) +{ +} + +uint64_t SymmetricSeparableBlurWeightsKey::hash() const +{ + return get_default_hash_2(type, radius); +} + +bool operator==(const SymmetricSeparableBlurWeightsKey &a, + const SymmetricSeparableBlurWeightsKey &b) +{ + return a.type == b.type && a.radius == b.radius; +} + +/* -------------------------------------------------------------------- + * Symmetric Separable Blur Weights. + */ + +SymmetricSeparableBlurWeights::SymmetricSeparableBlurWeights(int type, float radius) +{ + /* 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()); +} + +SymmetricSeparableBlurWeights::~SymmetricSeparableBlurWeights() +{ + GPU_texture_free(texture_); +} + +void SymmetricSeparableBlurWeights::bind_as_texture(GPUShader *shader, + const char *texture_name) const +{ + const int texture_image_unit = GPU_shader_get_texture_binding(shader, texture_name); + GPU_texture_bind(texture_, texture_image_unit); +} + +void SymmetricSeparableBlurWeights::unbind_as_texture() const +{ + GPU_texture_unbind(texture_); +} + +} // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/intern/context.cc b/source/blender/compositor/realtime_compositor/intern/context.cc index 924398f6747..0b123a2c271 100644 --- a/source/blender/compositor/realtime_compositor/intern/context.cc +++ b/source/blender/compositor/realtime_compositor/intern/context.cc @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "COM_context.hh" +#include "COM_static_cache_manager.hh" #include "COM_static_shader_manager.hh" #include "COM_texture_pool.hh" @@ -32,4 +33,9 @@ StaticShaderManager &Context::shader_manager() return shader_manager_; } +StaticCacheManager &Context::cache_manager() +{ + return cache_manager_; +} + } // namespace blender::realtime_compositor diff --git a/source/blender/compositor/realtime_compositor/intern/evaluator.cc b/source/blender/compositor/realtime_compositor/intern/evaluator.cc index 48457bec199..1cd7d4f8951 100644 --- a/source/blender/compositor/realtime_compositor/intern/evaluator.cc +++ b/source/blender/compositor/realtime_compositor/intern/evaluator.cc @@ -28,6 +28,7 @@ Evaluator::Evaluator(Context &context, bNodeTree &node_tree) void Evaluator::evaluate() { + context_.cache_manager().reset(); context_.texture_pool().reset(); if (!is_compiled_) { diff --git a/source/blender/compositor/realtime_compositor/intern/realize_on_domain_operation.cc b/source/blender/compositor/realtime_compositor/intern/realize_on_domain_operation.cc index 817293c0fa6..e5c448d0e33 100644 --- a/source/blender/compositor/realtime_compositor/intern/realize_on_domain_operation.cc +++ b/source/blender/compositor/realtime_compositor/intern/realize_on_domain_operation.cc @@ -38,8 +38,8 @@ void RealizeOnDomainOperation::execute() GPU_shader_bind(shader); /* Transform the input space into the domain space. */ - const float3x3 local_transformation = input.domain().transformation * - domain_.transformation.inverted(); + const float3x3 local_transformation = domain_.transformation.inverted() * + input.domain().transformation; /* Set the origin of the transformation to be the center of the domain. */ const float3x3 transformation = float3x3::from_origin_transformation( diff --git a/source/blender/compositor/realtime_compositor/intern/scheduler.cc b/source/blender/compositor/realtime_compositor/intern/scheduler.cc index ac5cc55a73f..0d3cce7af39 100644 --- a/source/blender/compositor/realtime_compositor/intern/scheduler.cc +++ b/source/blender/compositor/realtime_compositor/intern/scheduler.cc @@ -8,6 +8,7 @@ #include "NOD_derived_node_tree.hh" +#include "BKE_node.h" #include "BKE_node_runtime.hh" #include "COM_scheduler.hh" @@ -17,36 +18,103 @@ namespace blender::realtime_compositor { using namespace nodes::derived_node_tree_types; -/* Compute the output node whose result should be computed. The output node is the node marked as - * NODE_DO_OUTPUT. If multiple types of output nodes are marked, then the preference will be - * CMP_NODE_COMPOSITE > CMP_NODE_VIEWER > CMP_NODE_SPLITVIEWER. If no output node exists, a null - * node will be returned. */ -static DNode compute_output_node(DerivedNodeTree &tree) +/* Find the active context from the given context and its descendants contexts. The active context + * is the one whose node instance key matches the active_viewer_key stored in the root node tree. + * The instance key of each context is computed by calling BKE_node_instance_key given the key of + * the parent as well as the group node making the context. */ +static const DTreeContext *find_active_context_recursive(const DTreeContext *context, + bNodeInstanceKey key) { - const bNodeTree &root_tree = tree.root_context().btree(); + /* The instance key of the given context matches the active viewer instance key, so this is the + * active context, return it. */ + if (key.value == context->derived_tree().root_context().btree().active_viewer_key.value) { + return context; + } + + /* For each of the group nodes, compute their instance key and contexts and call this function + * recursively. */ + for (const bNode *group_node : context->btree().group_nodes()) { + const bNodeInstanceKey child_key = BKE_node_instance_key(key, &context->btree(), group_node); + const DTreeContext *child_context = context->child_context(*group_node); + const DTreeContext *found_context = find_active_context_recursive(child_context, child_key); + + /* If the found context is null, that means neither the child context nor one of its descendant + * contexts is active. */ + if (!found_context) { + continue; + } + + /* Otherwise, we have found our active context, return it. */ + return found_context; + } + + /* Neither the given context nor one of its descendant contexts is active, so return null. */ + return nullptr; +} + +/* Find the active context for the given node tree. The active context represents the node tree + * currently being edited. In most cases, that would be the top level node tree itself, but in the + * case where the user is editing the node tree of a node group, the active context would be a + * representation of the node tree of that node group. Note that the context also stores the group + * node that the user selected to edit the node tree, so the context fully represents a particular + * instance of the node group. */ +static const DTreeContext *find_active_context(const DerivedNodeTree &tree) +{ + /* The root context has an instance key of NODE_INSTANCE_KEY_BASE by definition. */ + return find_active_context_recursive(&tree.root_context(), NODE_INSTANCE_KEY_BASE); +} + +/* Return the output node which is marked as NODE_DO_OUTPUT. If multiple types of output nodes are + * marked, then the preference will be CMP_NODE_COMPOSITE > CMP_NODE_VIEWER > CMP_NODE_SPLITVIEWER. + * If no output node exists, a null node will be returned. */ +static DNode find_output_in_context(const DTreeContext *context) +{ + const bNodeTree &tree = context->btree(); - for (const bNode *node : root_tree.nodes_by_type("CompositorNodeComposite")) { + for (const bNode *node : tree.nodes_by_type("CompositorNodeComposite")) { if (node->flag & NODE_DO_OUTPUT) { - return DNode(&tree.root_context(), node); + return DNode(context, node); } } - for (const bNode *node : root_tree.nodes_by_type("CompositorNodeViewer")) { + for (const bNode *node : tree.nodes_by_type("CompositorNodeViewer")) { if (node->flag & NODE_DO_OUTPUT) { - return DNode(&tree.root_context(), node); + return DNode(context, node); } } - for (const bNode *node : root_tree.nodes_by_type("CompositorNodeSplitViewer")) { + for (const bNode *node : tree.nodes_by_type("CompositorNodeSplitViewer")) { if (node->flag & NODE_DO_OUTPUT) { - return DNode(&tree.root_context(), node); + return DNode(context, node); } } - /* No output node found, return a null node. */ return DNode(); } +/* Compute the output node whose result should be computed. This node is the output node that + * satisfies the requirements in the find_output_in_context function. First, the active context is + * searched for an output node, if non was found, the root context is search. For more information + * on what contexts mean here, see the find_active_context function. */ +static DNode compute_output_node(const DerivedNodeTree &tree) +{ + const DTreeContext *active_context = find_active_context(tree); + + const DNode node = find_output_in_context(active_context); + if (node) { + return node; + } + + /* If the active context is the root one and no output node was found, we consider this node tree + * to have no output node, even if one of the non-active descendants have an output node. */ + if (active_context->is_root()) { + return DNode(); + } + + /* The active context doesn't have an output node, search in the root context as a fallback. */ + return find_output_in_context(&tree.root_context()); +} + /* A type representing a mapping that associates each node with a heuristic estimation of the * number of intermediate buffers needed to compute it and all of its dependencies. See the * compute_number_of_needed_buffers function for more information. */ @@ -225,7 +293,7 @@ static NeededBuffers compute_number_of_needed_buffers(DNode output_node) * doesn't always guarantee an optimal evaluation order, as the optimal evaluation order is very * difficult to compute, however, this method works well in most cases. Moreover it assumes that * all buffers will have roughly the same size, which may not always be the case. */ -Schedule compute_schedule(DerivedNodeTree &tree) +Schedule compute_schedule(const DerivedNodeTree &tree) { Schedule schedule; diff --git a/source/blender/compositor/realtime_compositor/intern/static_cache_manager.cc b/source/blender/compositor/realtime_compositor/intern/static_cache_manager.cc new file mode 100644 index 00000000000..da78412a815 --- /dev/null +++ b/source/blender/compositor/realtime_compositor/intern/static_cache_manager.cc @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include <memory> + +#include "BLI_math_vec_types.hh" + +#include "COM_morphological_distance_feather_weights.hh" +#include "COM_symmetric_blur_weights.hh" +#include "COM_symmetric_separable_blur_weights.hh" + +#include "COM_static_cache_manager.hh" + +namespace blender::realtime_compositor { + +/* -------------------------------------------------------------------- + * Static Cache Manager. + */ + +void StaticCacheManager::reset() +{ + /* First, delete all resources that are no longer needed. */ + symmetric_blur_weights_.remove_if([](auto item) { return !item.value->needed; }); + symmetric_separable_blur_weights_.remove_if([](auto item) { return !item.value->needed; }); + morphological_distance_feather_weights_.remove_if([](auto item) { return !item.value->needed; }); + + /* Second, reset the needed status of the remaining resources to false to ready them to track + * their needed status for the next evaluation. */ + for (auto &value : symmetric_blur_weights_.values()) { + value->needed = false; + } + for (auto &value : symmetric_separable_blur_weights_.values()) { + value->needed = false; + } + for (auto &value : morphological_distance_feather_weights_.values()) { + value->needed = false; + } +} + +SymmetricBlurWeights &StaticCacheManager::get_symmetric_blur_weights(int type, float2 radius) +{ + const SymmetricBlurWeightsKey key(type, radius); + + auto &weights = *symmetric_blur_weights_.lookup_or_add_cb( + key, [&]() { return std::make_unique<SymmetricBlurWeights>(type, radius); }); + + weights.needed = true; + return weights; +} + +SymmetricSeparableBlurWeights &StaticCacheManager::get_symmetric_separable_blur_weights( + int type, float radius) +{ + const SymmetricSeparableBlurWeightsKey key(type, radius); + + auto &weights = *symmetric_separable_blur_weights_.lookup_or_add_cb( + key, [&]() { return std::make_unique<SymmetricSeparableBlurWeights>(type, radius); }); + + weights.needed = true; + return weights; +} + +MorphologicalDistanceFeatherWeights &StaticCacheManager:: + get_morphological_distance_feather_weights(int type, int radius) +{ + const MorphologicalDistanceFeatherWeightsKey key(type, radius); + + auto &weights = *morphological_distance_feather_weights_.lookup_or_add_cb( + key, [&]() { return std::make_unique<MorphologicalDistanceFeatherWeights>(type, radius); }); + + weights.needed = true; + return weights; +} + +} // namespace blender::realtime_compositor |