Blender V5.3
node_composite_movie_distortion.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
9#include "BKE_context.hh"
10#include "BKE_lib_id.hh"
11#include "BKE_tracking.hh"
12
13#include "UI_interface.hh"
14
15#include "GPU_shader.hh"
16#include "GPU_texture.hh"
17
19#include "COM_node_operation.hh"
20#include "COM_utilities.hh"
21
23
25
26static const EnumPropertyItem type_items[] = {
27 {int(compositor::DistortionType::Undistort), "UNDISTORT", 0, N_("Undistort"), ""},
28 {int(compositor::DistortionType::Distort), "DISTORT", 0, N_("Distort"), ""},
29 {0, nullptr, 0, nullptr, nullptr},
30};
31
33{
34 b.add_input<decl::Color>("Image"_ustr)
35 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
36 .structure_type(StructureType::Dynamic);
37 b.add_input<decl::Menu>("Type"_ustr)
39 .static_items(type_items)
41
42 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
43}
44
45static void node_init_api(const bContext *C, PointerRNA *node_ptr)
46{
47 bNode *node = node_ptr->data_as<bNode>();
48 Scene *scene = CTX_data_scene(C);
49 node->id = id_cast<ID *>(scene->clip);
50 id_us_plus(node->id);
51}
52
53static void node_draw_buttons(ui::Layout &layout, bContext *C, PointerRNA *ptr)
54{
55 template_id(&layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
56}
57
58using namespace blender::compositor;
59
61 public:
63
64 void execute() override
65 {
66 const Result &input_image = this->get_input("Image");
67 if (input_image.is_single_value() || !this->get_movie_clip()) {
68 Result &output_image = this->get_result("Image");
69 output_image.share_data(input_image);
70 return;
71 }
72
73 const Result &distortion_grid = context().cache_manager().distortion_grids.get(
74 context(),
76 input_image.domain(),
78 context().get_frame_number());
79
80 if (this->context().use_gpu()) {
81 this->execute_gpu(distortion_grid);
82 }
83 else {
84 this->execute_cpu(distortion_grid);
85 }
86 }
87
88 void execute_gpu(const Result &distortion_grid)
89 {
90 gpu::Shader *shader = context().get_shader("compositor_movie_distortion");
92
93 Result &input_image = get_input("Image");
95 GPU_texture_filter_mode(input_image, true);
96 input_image.bind_as_texture(shader, "input_tx");
97
98 distortion_grid.bind_as_texture(shader, "distortion_grid_tx");
99
100 Result &output_image = get_result("Image");
101 output_image.allocate_texture(distortion_grid.domain());
102 output_image.bind_as_image(shader, "output_img");
103
105
106 input_image.unbind_as_texture();
107 distortion_grid.unbind_as_texture();
108 output_image.unbind_as_image();
110 }
111
112 void execute_cpu(const Result &distortion_grid)
113 {
114 Result &input = get_input("Image");
115
116 Result &output = get_result("Image");
117 output.allocate_texture(distortion_grid.domain());
118
119 parallel_for(distortion_grid.domain().data_size, [&](const int2 texel) {
120 output.store_pixel(
121 texel, input.sample_bilinear_zero<Color>(distortion_grid.load_pixel<float2>(texel)));
122 });
123 }
124
126 {
127 return DistortionType(this->get_input("Type").get_single_value_default<MenuValue>().value);
128 }
129
131 {
132 return reinterpret_cast<MovieClip *>(node().id);
133 }
134};
135
137{
138 return new MovieDistortionOperation(context, node);
139}
140
141static void node_register()
142{
143 static bke::bNodeType ntype;
144
145 cmp_node_type_base(&ntype, "CompositorNodeMovieDistortion"_ustr, CMP_NODE_MOVIEDISTORTION);
146 ntype.ui_name = "Movie Distortion";
147 ntype.ui_description =
148 "Remove lens distortion from footage, using motion tracking camera lens settings";
149 ntype.enum_name_legacy = "MOVIEDISTORTION";
151 ntype.declare = node_declare;
155
157}
159
160} // namespace blender::nodes::node_composite_movie_distortion_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_MOVIEDISTORTION
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
Result & get(Context &context, MovieClip *movie_clip, Domain domain, DistortionType type, int frame_number)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void share_data(const Result &source)
Definition result.cc:996
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
void parallel_for(const int2 range, const Function &function)
static void node_init_api(const bContext *C, PointerRNA *node_ptr)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_draw_buttons(ui::Layout &layout, bContext *C, PointerRNA *ptr)
void id_us_plus(ID *id)
Definition lib_id.cc:433
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
void GPU_shader_unbind()
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
Scene * CTX_data_scene(const bContext *C)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
T * data_as() const
Definition RNA_types.hh:139
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) initfunc_api(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:333
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)