Blender V4.3
node_composite_movieclip.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
10
11#include "BKE_context.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_movieclip.h"
14#include "BKE_tracking.h"
15
16#include "DNA_defaults.h"
17
18#include "RNA_access.hh"
19
20#include "UI_interface.hh"
21#include "UI_resources.hh"
22
23#include "GPU_shader.hh"
24#include "GPU_texture.hh"
25
26#include "COM_node_operation.hh"
27#include "COM_utilities.hh"
28
30
32
34{
35 b.add_output<decl::Color>("Image");
36 b.add_output<decl::Float>("Alpha");
37 b.add_output<decl::Float>("Offset X");
38 b.add_output<decl::Float>("Offset Y");
39 b.add_output<decl::Float>("Scale");
40 b.add_output<decl::Float>("Angle");
41}
42
43static void init(const bContext *C, PointerRNA *ptr)
44{
45 bNode *node = (bNode *)ptr->data;
46 Scene *scene = CTX_data_scene(C);
48
49 node->id = (ID *)scene->clip;
50 id_us_plus(node->id);
51 node->storage = user;
52 user->framenr = 1;
53}
54
56{
57 uiTemplateID(layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
58}
59
61{
62 bNode *node = (bNode *)ptr->data;
63 PointerRNA clipptr;
64
65 uiTemplateID(layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
66
67 if (!node->id) {
68 return;
69 }
70
71 clipptr = RNA_pointer_get(ptr, "clip");
72
73 uiTemplateColorspaceSettings(layout, &clipptr, "colorspace_settings");
74}
75
76using namespace blender::realtime_compositor;
77
79 public:
81
82 void execute() override
83 {
84 GPUTexture *movie_clip_texture = get_movie_clip_texture();
85
86 compute_image(movie_clip_texture);
87 compute_alpha(movie_clip_texture);
88 compute_stabilization_data(movie_clip_texture);
89
91 }
92
93 void compute_image(GPUTexture *movie_clip_texture)
94 {
95 if (!should_compute_output("Image")) {
96 return;
97 }
98
99 Result &result = get_result("Image");
100
101 /* The movie clip texture is invalid or missing, set an appropriate fallback value. */
102 if (!movie_clip_texture) {
103 result.allocate_invalid();
104 return;
105 }
106
107 const int2 size = int2(GPU_texture_width(movie_clip_texture),
108 GPU_texture_height(movie_clip_texture));
109 result.allocate_texture(Domain(size));
110
111 GPUShader *shader = context().get_shader("compositor_read_input_color");
112 GPU_shader_bind(shader);
113
114 const int2 lower_bound = int2(0);
115 GPU_shader_uniform_2iv(shader, "lower_bound", lower_bound);
116
117 const int input_unit = GPU_shader_get_sampler_binding(shader, "input_tx");
118 GPU_texture_bind(movie_clip_texture, input_unit);
119
120 result.bind_as_image(shader, "output_img");
121
123
125 GPU_texture_unbind(movie_clip_texture);
126 result.unbind_as_image();
127 }
128
129 void compute_alpha(GPUTexture *movie_clip_texture)
130 {
131 if (!should_compute_output("Alpha")) {
132 return;
133 }
134
135 Result &result = get_result("Alpha");
136
137 /* The movie clip texture is invalid or missing, set an appropriate fallback value. */
138 if (!movie_clip_texture) {
139 result.allocate_single_value();
140 result.set_float_value(1.0f);
141 return;
142 }
143
144 const int2 size = int2(GPU_texture_width(movie_clip_texture),
145 GPU_texture_height(movie_clip_texture));
146 result.allocate_texture(Domain(size));
147
148 GPUShader *shader = context().get_shader("compositor_read_input_alpha");
149 GPU_shader_bind(shader);
150
151 const int2 lower_bound = int2(0);
152 GPU_shader_uniform_2iv(shader, "lower_bound", lower_bound);
153
154 const int input_unit = GPU_shader_get_sampler_binding(shader, "input_tx");
155 GPU_texture_bind(movie_clip_texture, input_unit);
156
157 result.bind_as_image(shader, "output_img");
158
160
162 GPU_texture_unbind(movie_clip_texture);
163 result.unbind_as_image();
164 }
165
166 void compute_stabilization_data(GPUTexture *movie_clip_texture)
167 {
168 /* The movie clip texture is invalid or missing, set appropriate fallback values. */
169 if (!movie_clip_texture) {
170 if (should_compute_output("Offset X")) {
171 Result &result = get_result("Offset X");
172 result.allocate_single_value();
173 result.set_float_value(0.0f);
174 }
175 if (should_compute_output("Offset Y")) {
176 Result &result = get_result("Offset Y");
177 result.allocate_single_value();
178 result.set_float_value(0.0f);
179 }
180 if (should_compute_output("Scale")) {
181 Result &result = get_result("Scale");
182 result.allocate_single_value();
183 result.set_float_value(1.0f);
184 }
185 if (should_compute_output("Angle")) {
186 Result &result = get_result("Angle");
187 result.allocate_single_value();
188 result.set_float_value(0.0f);
189 }
190 return;
191 }
192
193 MovieClip *movie_clip = get_movie_clip();
194 const int frame_number = BKE_movieclip_remap_scene_to_clip_frame(movie_clip,
195 context().get_frame_number());
196 const int width = GPU_texture_width(movie_clip_texture);
197 const int height = GPU_texture_height(movie_clip_texture);
198
199 /* If the movie clip has no stabilization data, it will initialize the given values with
200 * fallback values regardless, so no need to handle that case. */
201 float2 offset;
202 float scale, angle;
204 movie_clip, frame_number, width, height, offset, &scale, &angle);
205
206 if (should_compute_output("Offset X")) {
207 Result &result = get_result("Offset X");
208 result.allocate_single_value();
209 result.set_float_value(offset.x);
210 }
211 if (should_compute_output("Offset Y")) {
212 Result &result = get_result("Offset Y");
213 result.allocate_single_value();
214 result.set_float_value(offset.y);
215 }
216 if (should_compute_output("Scale")) {
217 Result &result = get_result("Scale");
218 result.allocate_single_value();
219 result.set_float_value(scale);
220 }
221 if (should_compute_output("Angle")) {
222 Result &result = get_result("Angle");
223 result.allocate_single_value();
224 result.set_float_value(angle);
225 }
226 }
227
229 {
230 MovieClip *movie_clip = get_movie_clip();
231 MovieClipUser *movie_clip_user = get_movie_clip_user();
232 BKE_movieclip_user_set_frame(movie_clip_user, context().get_frame_number());
233 return BKE_movieclip_get_gpu_texture(movie_clip, movie_clip_user);
234 }
235
237 {
238 MovieClip *movie_clip = get_movie_clip();
239 if (movie_clip) {
241 }
242 }
243
245 {
246 return (MovieClip *)bnode().id;
247 }
248
250 {
251 return static_cast<MovieClipUser *>(bnode().storage);
252 }
253};
254
256{
257 return new MovieClipOperation(context, node);
258}
259
260} // namespace blender::nodes::node_composite_movieclip_cc
261
263{
265
266 static blender::bke::bNodeType ntype;
267
268 cmp_node_type_base(&ntype, CMP_NODE_MOVIECLIP, "Movie Clip", NODE_CLASS_INPUT);
269 ntype.declare = file_ns::cmp_node_movieclip_declare;
270 ntype.draw_buttons = file_ns::node_composit_buts_movieclip;
271 ntype.draw_buttons_ex = file_ns::node_composit_buts_movieclip_ex;
272 ntype.get_compositor_operation = file_ns::get_compositor_operation;
273 ntype.initfunc_api = file_ns::init;
274 ntype.flag |= NODE_PREVIEW;
277
279}
Scene * CTX_data_scene(const bContext *C)
void id_us_plus(ID *id)
Definition lib_id.cc:351
float BKE_movieclip_remap_scene_to_clip_frame(const struct MovieClip *clip, float framenr)
void BKE_movieclip_user_set_frame(struct MovieClipUser *user, int framenr)
struct GPUTexture * BKE_movieclip_get_gpu_texture(struct MovieClip *clip, struct MovieClipUser *cuser)
void BKE_movieclip_free_gputexture(struct MovieClip *clip)
#define NODE_CLASS_INPUT
Definition BKE_node.hh:404
void BKE_tracking_stabilization_data_get(struct MovieClip *clip, int framenr, int width, int height, float translation[2], float *scale, float *angle)
#define DNA_struct_default_alloc(struct_name)
@ NODE_PREVIEW
int GPU_shader_get_sampler_binding(GPUShader *shader, const char *name)
void GPU_shader_uniform_2iv(GPUShader *sh, const char *name, const int data[2])
void GPU_shader_bind(GPUShader *shader)
void GPU_shader_unbind()
int GPU_texture_height(const GPUTexture *texture)
void GPU_texture_bind(GPUTexture *texture, int unit)
int GPU_texture_width(const GPUTexture *texture)
void GPU_texture_unbind(GPUTexture *texture)
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:125
void uiTemplateColorspaceSettings(uiLayout *layout, PointerRNA *ptr, const char *propname)
void uiTemplateID(uiLayout *layout, const bContext *C, PointerRNA *ptr, const char *propname, const char *newop, const char *openop, const char *unlinkop, int filter=UI_TEMPLATE_ID_FILTER_ALL, bool live_icon=false, const char *text=nullptr)
struct GPUShader GPUShader
void init()
GPUShader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
bool should_compute_output(StringRef identifier)
Result & get_result(StringRef identifier)
Definition operation.cc:46
local_group_size(16, 16) .push_constant(Type b
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void cmp_node_movieclip_declare(NodeDeclarationBuilder &b)
static void node_composit_buts_movieclip_ex(uiLayout *layout, bContext *C, PointerRNA *ptr)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:131
VecBase< int32_t, 2 > int2
void register_node_type_cmp_movieclip()
void cmp_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition DNA_ID.h:413
void * data
Definition RNA_types.hh:42
struct ID * id
void * storage
Defines a node type.
Definition BKE_node.hh:218
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:324
void(* draw_buttons_ex)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:240
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
void(* initfunc_api)(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:280
PointerRNA * ptr
Definition wm_files.cc:4126