Blender V4.5
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"
19#include "UI_resources.hh"
20
21#include "GPU_shader.hh"
22#include "GPU_texture.hh"
23
25#include "COM_node_operation.hh"
26#include "COM_utilities.hh"
27
29
30/* **************** Translate ******************** */
31
33
35{
36 b.add_input<decl::Color>("Image")
37 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
38 .compositor_domain_priority(0);
39 b.add_output<decl::Color>("Image");
40}
41
42static void init(const bContext *C, PointerRNA *ptr)
43{
44 bNode *node = (bNode *)ptr->data;
45 Scene *scene = CTX_data_scene(C);
46
47 node->id = (ID *)scene->clip;
48 id_us_plus(node->id);
49}
50
51static void storage_free(bNode *node)
52{
53 if (node->storage) {
55 }
56
57 node->storage = nullptr;
58}
59
60static void storage_copy(bNodeTree * /*dst_ntree*/, bNode *dest_node, const bNode *src_node)
61{
62 if (src_node->storage) {
64 }
65}
66
68{
69 bNode *node = (bNode *)ptr->data;
70
71 uiTemplateID(layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
72
73 if (!node->id) {
74 return;
75 }
76
77 layout->prop(ptr, "distortion_type", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
78}
79
80using namespace blender::compositor;
81
83 public:
85
86 void execute() override
87 {
88 const Result &input_image = this->get_input("Image");
89 if (input_image.is_single_value() || !this->get_movie_clip()) {
90 Result &output_image = this->get_result("Image");
91 output_image.share_data(input_image);
92 return;
93 }
94
95 const Domain domain = compute_domain();
96 const Result &distortion_grid = context().cache_manager().distortion_grids.get(
97 context(),
99 domain.size,
101 context().get_frame_number());
102
103 if (this->context().use_gpu()) {
104 this->execute_gpu(distortion_grid);
105 }
106 else {
107 this->execute_cpu(distortion_grid);
108 }
109 }
110
111 void execute_gpu(const Result &distortion_grid)
112 {
113 GPUShader *shader = context().get_shader("compositor_movie_distortion");
114 GPU_shader_bind(shader);
115
116 Result &input_image = get_input("Image");
118 GPU_texture_filter_mode(input_image, true);
119 input_image.bind_as_texture(shader, "input_tx");
120
121 distortion_grid.bind_as_texture(shader, "distortion_grid_tx");
122
123 Result &output_image = get_result("Image");
124 output_image.allocate_texture(distortion_grid.domain());
125 output_image.bind_as_image(shader, "output_img");
126
127 compute_dispatch_threads_at_least(shader, distortion_grid.domain().size);
128
129 input_image.unbind_as_texture();
130 distortion_grid.unbind_as_texture();
131 output_image.unbind_as_image();
133 }
134
135 void execute_cpu(const Result &distortion_grid)
136 {
137 Result &input = get_input("Image");
138
139 Result &output = get_result("Image");
140 output.allocate_texture(distortion_grid.domain());
141
142 parallel_for(distortion_grid.domain().size, [&](const int2 texel) {
143 output.store_pixel(texel,
144 input.sample_bilinear_zero(distortion_grid.load_pixel<float2>(texel)));
145 });
146 }
147
152
154 {
155 return reinterpret_cast<MovieClip *>(bnode().id);
156 }
157};
158
160{
161 return new MovieDistortionOperation(context, node);
162}
163
164} // namespace blender::nodes::node_composite_moviedistortion_cc
165
167{
169
170 static blender::bke::bNodeType ntype;
171
172 cmp_node_type_base(&ntype, "CompositorNodeMovieDistortion", CMP_NODE_MOVIEDISTORTION);
173 ntype.ui_name = "Movie Distortion";
174 ntype.ui_description =
175 "Remove lens distortion from footage, using motion tracking camera lens settings";
176 ntype.enum_name_legacy = "MOVIEDISTORTION";
178 ntype.declare = file_ns::cmp_node_moviedistortion_declare;
179 ntype.draw_buttons = file_ns::node_composit_buts_moviedistortion;
180 ntype.initfunc_api = file_ns::init;
182 ntype, std::nullopt, file_ns::storage_free, file_ns::storage_copy);
183 ntype.get_compositor_operation = file_ns::get_compositor_operation;
184
186}
Scene * CTX_data_scene(const bContext *C)
void id_us_plus(ID *id)
Definition lib_id.cc:353
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:441
#define CMP_NODE_MOVIEDISTORTION
struct MovieDistortion * BKE_tracking_distortion_copy(struct MovieDistortion *distortion)
Definition tracking.cc:2306
void BKE_tracking_distortion_free(struct MovieDistortion *distortion)
Definition tracking.cc:2407
void GPU_shader_bind(GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
void GPU_texture_extend_mode(GPUTexture *texture, GPUSamplerExtendMode extend_mode)
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
void GPU_texture_filter_mode(GPUTexture *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)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void init()
GPUShader * 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:401
void allocate_texture(Domain domain, bool from_pool=true)
Definition result.cc:309
void unbind_as_texture() const
Definition result.cc:389
void bind_as_texture(GPUShader *shader, const char *texture_name) const
Definition result.cc:365
const Domain & domain() const
void bind_as_image(GPUShader *shader, const char *image_name, bool read=false) const
Definition result.cc:376
void unbind_as_image() const
Definition result.cc:395
bool is_single_value() const
Definition result.cc:625
#define input
#define this
#define output
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
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:5603
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:170
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:404
struct MovieClip * clip
int16_t custom1
struct ID * id
void * storage
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:336
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
void(* initfunc_api)(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4227