Blender V5.0
node_composite_moviedistortion.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
8
10#include "BLI_string_utf8.h"
11
12#include "DNA_movieclip_types.h"
13
14#include "BKE_context.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_tracking.h"
17
18#include "UI_interface.hh"
20#include "UI_resources.hh"
21
22#include "GPU_shader.hh"
23#include "GPU_texture.hh"
24
26#include "COM_node_operation.hh"
27#include "COM_utilities.hh"
28
30
32
33static const EnumPropertyItem type_items[] = {
34 {int(compositor::DistortionType::Distort), "UNDISTORT", 0, N_("Undistort"), ""},
35 {int(compositor::DistortionType::Undistort), "DISTORT", 0, N_("Distort"), ""},
36 {0, nullptr, 0, nullptr, nullptr},
37};
38
40{
41 b.add_input<decl::Color>("Image")
42 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
43 .structure_type(StructureType::Dynamic);
44 b.add_input<decl::Menu>("Type")
46 .static_items(type_items)
48
49 b.add_output<decl::Color>("Image").structure_type(StructureType::Dynamic);
50}
51
52static void init(const bContext *C, PointerRNA *ptr)
53{
54 bNode *node = (bNode *)ptr->data;
55 Scene *scene = CTX_data_scene(C);
56
57 node->id = (ID *)scene->clip;
58 id_us_plus(node->id);
59}
60
61static void storage_free(bNode *node)
62{
63 if (node->storage) {
65 }
66
67 node->storage = nullptr;
68}
69
70static void storage_copy(bNodeTree * /*dst_ntree*/, bNode *dest_node, const bNode *src_node)
71{
72 if (src_node->storage) {
74 }
75}
76
78{
79 uiTemplateID(layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
80}
81
82using namespace blender::compositor;
83
85 public:
87
88 void execute() override
89 {
90 const Result &input_image = this->get_input("Image");
91 if (input_image.is_single_value() || !this->get_movie_clip()) {
92 Result &output_image = this->get_result("Image");
93 output_image.share_data(input_image);
94 return;
95 }
96
97 const Domain domain = compute_domain();
98 const Result &distortion_grid = context().cache_manager().distortion_grids.get(
99 context(),
101 domain.size,
103 context().get_frame_number());
104
105 if (this->context().use_gpu()) {
106 this->execute_gpu(distortion_grid);
107 }
108 else {
109 this->execute_cpu(distortion_grid);
110 }
111 }
112
113 void execute_gpu(const Result &distortion_grid)
114 {
115 gpu::Shader *shader = context().get_shader("compositor_movie_distortion");
116 GPU_shader_bind(shader);
117
118 Result &input_image = get_input("Image");
120 GPU_texture_filter_mode(input_image, true);
121 input_image.bind_as_texture(shader, "input_tx");
122
123 distortion_grid.bind_as_texture(shader, "distortion_grid_tx");
124
125 Result &output_image = get_result("Image");
126 output_image.allocate_texture(distortion_grid.domain());
127 output_image.bind_as_image(shader, "output_img");
128
129 compute_dispatch_threads_at_least(shader, distortion_grid.domain().size);
130
131 input_image.unbind_as_texture();
132 distortion_grid.unbind_as_texture();
133 output_image.unbind_as_image();
135 }
136
137 void execute_cpu(const Result &distortion_grid)
138 {
139 Result &input = get_input("Image");
140
141 Result &output = get_result("Image");
142 output.allocate_texture(distortion_grid.domain());
143
144 parallel_for(distortion_grid.domain().size, [&](const int2 texel) {
145 output.store_pixel(texel,
146 input.sample_bilinear_zero(distortion_grid.load_pixel<float2>(texel)));
147 });
148 }
149
151 {
152 const Result &input = this->get_input("Type");
153 const MenuValue default_menu_value = MenuValue(DistortionType::Distort);
154 const MenuValue menu_value = input.get_single_value_default(default_menu_value);
155 return static_cast<DistortionType>(menu_value.value);
156 }
157
159 {
160 return reinterpret_cast<MovieClip *>(bnode().id);
161 }
162};
163
165{
166 return new MovieDistortionOperation(context, node);
167}
168
169} // namespace blender::nodes::node_composite_moviedistortion_cc
170
172{
174
175 static blender::bke::bNodeType ntype;
176
177 cmp_node_type_base(&ntype, "CompositorNodeMovieDistortion", CMP_NODE_MOVIEDISTORTION);
178 ntype.ui_name = "Movie Distortion";
179 ntype.ui_description =
180 "Remove lens distortion from footage, using motion tracking camera lens settings";
181 ntype.enum_name_legacy = "MOVIEDISTORTION";
183 ntype.declare = file_ns::cmp_node_moviedistortion_declare;
184 ntype.draw_buttons = file_ns::node_composit_buts_moviedistortion;
185 ntype.initfunc_api = file_ns::init;
187 ntype, std::nullopt, file_ns::storage_free, file_ns::storage_copy);
188 ntype.get_compositor_operation = file_ns::get_compositor_operation;
189
191}
Scene * CTX_data_scene(const bContext *C)
void id_us_plus(ID *id)
Definition lib_id.cc:358
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:455
#define CMP_NODE_MOVIEDISTORTION
struct MovieDistortion * BKE_tracking_distortion_copy(struct MovieDistortion *distortion)
Definition tracking.cc:2307
void BKE_tracking_distortion_free(struct MovieDistortion *distortion)
Definition tracking.cc:2408
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
void GPU_texture_extend_mode(blender::gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
void GPU_texture_filter_mode(blender::gpu::Texture *texture, bool use_filter)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
void uiTemplateID(uiLayout *layout, const bContext *C, PointerRNA *ptr, blender::StringRefNull propname, const char *newop, const char *openop, const char *unlinkop, int filter=UI_TEMPLATE_ID_FILTER_ALL, bool live_icon=false, std::optional< blender::StringRef > text=std::nullopt)
void init()
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
Result & get(Context &context, MovieClip *movie_clip, int2 size, DistortionType type, int frame_number)
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Definition operation.cc:39
Result & get_input(StringRef identifier) const
Definition operation.cc:138
virtual Domain compute_domain()
Definition operation.cc:56
void share_data(const Result &source)
Definition result.cc:523
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:389
void unbind_as_texture() const
Definition result.cc:511
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:487
const Domain & domain() const
void unbind_as_image() const
Definition result.cc:517
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:498
bool is_single_value() const
Definition result.cc:758
#define input
#define output
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:5414
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:196
void parallel_for(const int2 range, const Function &function)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
static void storage_copy(bNodeTree *, bNode *dest_node, const bNode *src_node)
static void cmp_node_moviedistortion_declare(NodeDeclarationBuilder &b)
VecBase< int32_t, 2 > int2
static void register_node_type_cmp_moviedistortion()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Definition DNA_ID.h:414
struct MovieClip * clip
struct ID * id
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:348
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* initfunc_api)(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:302
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4238