diff options
author | Omar Emara <mail@OmarEmara.dev> | 2022-08-18 13:00:14 +0300 |
---|---|---|
committer | Omar Emara <mail@OmarEmara.dev> | 2022-08-18 13:00:14 +0300 |
commit | b828d453e93c30e81d3cb0d5aa3edc553ea9a333 (patch) | |
tree | fd0604a2b2563d8a8f0f88ab6a131e2f92734eb3 /source/blender/nodes/composite | |
parent | 6cb0a122dfcd7d3e75e8bc0d12e09b75b97cee99 (diff) |
Realtime Compositor: Implement filter node
This patch implements the filter node for the realtime compositor.
Differential Revision: https://developer.blender.org/D15661
Reviewed By: Clement Foucault
Diffstat (limited to 'source/blender/nodes/composite')
-rw-r--r-- | source/blender/nodes/composite/nodes/node_composite_filter.cc | 100 |
1 files changed, 99 insertions, 1 deletions
diff --git a/source/blender/nodes/composite/nodes/node_composite_filter.cc b/source/blender/nodes/composite/nodes/node_composite_filter.cc index 854cf684806..c071becac54 100644 --- a/source/blender/nodes/composite/nodes/node_composite_filter.cc +++ b/source/blender/nodes/composite/nodes/node_composite_filter.cc @@ -5,6 +5,8 @@ * \ingroup cmpnodes */ +#include "BLI_float3x3.hh" + #include "UI_interface.h" #include "UI_resources.h" @@ -36,7 +38,103 @@ class FilterOperation : public NodeOperation { void execute() override { - get_input("Image").pass_through(get_result("Image")); + GPUShader *shader = shader_manager().get(get_shader_name()); + GPU_shader_bind(shader); + + GPU_shader_uniform_mat3_as_mat4(shader, "kernel", get_filter_kernel().ptr()); + + const Result &input_image = get_input("Image"); + input_image.bind_as_texture(shader, "input_tx"); + + const Result &factor = get_input("Fac"); + factor.bind_as_texture(shader, "factor_tx"); + + const Domain domain = compute_domain(); + + Result &output_image = get_result("Image"); + output_image.allocate_texture(domain); + output_image.bind_as_image(shader, "output_img"); + + compute_dispatch_threads_at_least(shader, domain.size); + + input_image.unbind_as_texture(); + factor.unbind_as_texture(); + output_image.unbind_as_image(); + GPU_shader_unbind(); + } + + int get_filter_method() + { + return bnode().custom1; + } + + float3x3 get_filter_kernel() + { + /* Initialize the kernels as arrays of rows with the top row first. Edge detection kernels + * return the kernel in the X direction, while the kernel in the Y direction will be computed + * inside the shader by transposing the kernel in the X direction. */ + switch (get_filter_method()) { + case CMP_FILT_SOFT: { + const float kernel[3][3] = {{1.0f / 16.0f, 2.0f / 16.0f, 1.0f / 16.0f}, + {2.0f / 16.0f, 4.0f / 16.0f, 2.0f / 16.0f}, + {1.0f / 16.0f, 2.0f / 16.0f, 1.0f / 16.0f}}; + return float3x3(kernel); + } + case CMP_FILT_SHARP_BOX: { + const float kernel[3][3] = { + {-1.0f, -1.0f, -1.0f}, {-1.0f, 9.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}}; + return float3x3(kernel); + } + case CMP_FILT_LAPLACE: { + const float kernel[3][3] = {{-1.0f / 8.0f, -1.0f / 8.0f, -1.0f / 8.0f}, + {-1.0f / 8.0f, 1.0f, -1.0f / 8.0f}, + {-1.0f / 8.0f, -1.0f / 8.0f, -1.0f / 8.0f}}; + return float3x3(kernel); + } + case CMP_FILT_SOBEL: { + const float kernel[3][3] = {{1.0f, 0.0f, -1.0f}, {2.0f, 0.0f, -2.0f}, {1.0f, 0.0f, -1.0f}}; + return float3x3(kernel); + } + case CMP_FILT_PREWITT: { + const float kernel[3][3] = {{1.0f, 0.0f, -1.0f}, {1.0f, 0.0f, -1.0f}, {1.0f, 0.0f, -1.0f}}; + return float3x3(kernel); + } + case CMP_FILT_KIRSCH: { + const float kernel[3][3] = { + {5.0f, -3.0f, -2.0f}, {5.0f, -3.0f, -2.0f}, {5.0f, -3.0f, -2.0f}}; + return float3x3(kernel); + } + case CMP_FILT_SHADOW: { + const float kernel[3][3] = {{1.0f, 2.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, {-1.0f, -2.0f, -1.0f}}; + return float3x3(kernel); + } + case CMP_FILT_SHARP_DIAMOND: { + const float kernel[3][3] = { + {0.0f, -1.0f, 0.0f}, {-1.0f, 5.0f, -1.0f}, {0.0f, -1.0f, 0.0f}}; + return float3x3(kernel); + } + default: { + const float kernel[3][3] = {{0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}; + return float3x3(kernel); + } + } + } + + const char *get_shader_name() + { + switch (get_filter_method()) { + case CMP_FILT_LAPLACE: + case CMP_FILT_SOBEL: + case CMP_FILT_PREWITT: + case CMP_FILT_KIRSCH: + return "compositor_edge_filter"; + case CMP_FILT_SOFT: + case CMP_FILT_SHARP_BOX: + case CMP_FILT_SHADOW: + case CMP_FILT_SHARP_DIAMOND: + default: + return "compositor_filter"; + } } }; |