28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
31 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
35 .structure_type(StructureType::Dynamic)
36 .align_with_previous();
44 .structure_type(StructureType::Dynamic);
53 .
description(
"The number of samples used to approximate the motion blur");
57 .
description(
"Time between shutter opening and closing in frames");
62#define MOTION_BLUR_TILE_SIZE 32
63#define DEPTH_SCALE 100.0f
78 return (length_bits << 18u) | ((texel.x & 0x1FFu) << 9u) | (texel.y & 0x1FFu);
100 output.allocate_single_value();
115 for (
int j = 0; j < tile_size.y; j++) {
116 for (
int i = 0; i < tile_size.x; i++) {
117 int2 sub_texel = texel * tile_size +
int2(i, j);
119 max_previous_velocity =
max_velocity(velocity.xy(), max_previous_velocity);
120 max_next_velocity =
max_velocity(velocity.zw(), max_next_velocity);
146 max_point =
math::min(max_point, size - 1);
151 rect.
extent = 1 + max_point - min_point;
165 float2 dir = magnitude != 0.0f ? motion / magnitude : motion;
180 return math::abs(distance_to_line) < std::numbers::sqrt2_v<float>;
187 const Result &max_tile_velocity,
188 const float shutter_speed)
192 output.allocate_single_value();
209 const float2 max_previous_velocity = max_motion.xy() * shutter_speed;
210 const float2 max_next_velocity = max_motion.zw() * -shutter_speed;
217 for (
int j = 0; j < motion_rect.
extent.y; j++) {
218 for (
int i = 0; i < motion_rect.
extent.x; i++) {
223 current_max_velocity.xy(), max_previous_velocity,
tile, src_tile);
225 current_max_velocity.zw(), max_next_velocity,
tile, src_tile);
226 output.store_pixel(
tile,
float4(new_max_previous_velocity, new_max_next_velocity));
237 for (
int j = 0; j < motion_rect.
extent.y; j++) {
238 for (
int i = 0; i < motion_rect.
extent.x; i++) {
243 current_max_velocity.xy(), max_previous_velocity,
tile, src_tile);
245 current_max_velocity.zw(), max_next_velocity,
tile, src_tile);
246 output.store_pixel(
tile,
float4(new_max_previous_velocity, new_max_next_velocity));
265 const float sample_motion_length,
266 const float offset_length)
269 float2(center_motion_length, sample_motion_length) - offset_length + 1.0f, 0.0f, 1.0f);
275 return math::clamp(0.5f + depth_scale * (sample_depth - center_depth), 0.0f, 1.0f);
280 const float2 &sample_motion,
281 const float &sample_motion_length)
283 if (sample_motion_length < 0.5f) {
286 return (
math::dot(offset, sample_motion) > 0.0f) ? 1.0f : 0.0f;
291 const float sample_depth,
292 const float center_motion_length,
293 const float sample_motion_length,
294 const float offset_length)
299 float2 spread_weight =
spread_compare(center_motion_length, sample_motion_length, offset_length);
300 return depth_weight * spread_weight;
311 const Result &input_depth,
312 const Result &input_velocity,
315 const float center_depth,
316 const float center_motion_len,
318 const float offset_len,
320 const float shutter_speed,
326 float2 sample_motion = (next) ? sample_vectors.zw() : sample_vectors.xy();
332 center_depth, sample_depth, center_motion_len, sample_motion_len, offset_len);
335 weights.x = direct_weights.x;
336 weights.y = direct_weights.y;
337 weights.z =
dir_compare(offset, sample_motion, sample_motion_len);
338 weights.x *= weights.z;
339 weights.y *= weights.z;
341 accum.
fg += sample_color * weights.y;
342 accum.
bg += sample_color * weights.x;
347 const Result &input_depth,
348 const Result &input_velocity,
351 const float2 ¢er_motion,
352 const float center_depth,
356 const int samples_count,
357 const float shutter_speed,
365 float2 sanitized_max_motion = max_motion;
366 if (max_motion_len < center_motion_len) {
367 max_motion_len = center_motion_len;
368 sanitized_max_motion = center_motion;
371 if (max_motion_len < 0.5f) {
376 float t, inc = 1.0f /
float(samples_count);
377 for (i = 0, t = ofs * inc; i < samples_count; i++, t += inc) {
385 sanitized_max_motion * t,
392 if (center_motion_len < 0.5f) {
396 for (i = 0, t = ofs * inc; i < samples_count; i++, t += inc) {
408 center_motion_len * t,
416 const Result &input_depth,
417 const Result &input_velocity,
420 const int samples_count,
421 const float shutter_speed)
425 for (const int64_t y : sub_y_range) {
426 for (const int64_t x : IndexRange(size.x)) {
427 const int2 texel = int2(x, y);
428 float2 uv = (float2(texel) + 0.5f) / float2(size);
431 float center_depth = input_depth.load_pixel<float, true>(texel);
432 float4 center_motion = input_velocity.load_pixel<float4, true>(texel);
433 float2 center_previous_motion = center_motion.xy() * shutter_speed;
434 float2 center_next_motion = center_motion.zw() * -shutter_speed;
435 float4 center_color = float4(input_image.load_pixel<Color>(texel));
439 float rand = interleaved_gradient_noise(texel);
440 int2 tile = (texel + int2(rand * 2.0f - 1.0f * float(MOTION_BLUR_TILE_SIZE) * 0.25f)) /
441 MOTION_BLUR_TILE_SIZE;
445 float4 max_motion = max_velocity.load_pixel<float4, true>(tile);
448 accum.weight = float3(0.0f, 0.0f, 1.0f);
449 accum.bg = float4(0.0f);
450 accum.fg = float4(0.0f);
452 gather_blur(input_image,
457 center_previous_motion,
466 gather_blur(input_image,
482 float w = 1.0f / (50.0f * float(samples_count) * 4.0f);
483 accum.bg += center_color * w;
489 center_color = accum.bg / accum.weight.x;
492 accum.fg += accum.bg;
493 accum.weight.y += accum.weight.x;
496 float blend_fac = math::clamp(1.0f - accum.weight.y / accum.weight.z, 0.0f, 1.0f);
497 float4 out_color = (accum.fg / accum.weight.z) + center_color * blend_fac;
499 output.store_pixel(texel, Color(out_color));
518 if (this->
context().use_gpu()) {
540 if (velocity.is_single_value()) {
542 output.allocate_single_value();
543 output.set_single_value(velocity.get_single_value<
float4>());
552 velocity.bind_as_texture(
shader,
"input_tx");
562 velocity.unbind_as_texture();
587 const int size =
sizeof(
uint32_t) * 512 * 512 * 2;
600 return tile_indirection_buffer;
622 shader,
"max_velocity_tx");
630 output.allocate_texture(domain);
638 velocity.unbind_as_texture_or_single_value(velocity_texture);
654 output.allocate_texture(domain);
658 this->
context(), max_tile_velocity, shutter_speed);
679 return math::max(0.0f, this->
get_input(
"Shutter").get_single_value_default<float>()) / 2.0f;
694 ntype.
ui_description =
"Uses the vector speed render pass to blur the image pixels in 2D";
#define NODE_CLASS_OP_FILTER
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr IndexRange()=default
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Context & context() const
Result & get_input(StringRef identifier) const
virtual Domain compute_domain()
bool is_single_value() const
T sample_bilinear_extended(const float2 &coordinates) const
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
void unbind_as_texture() const
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
T load_pixel_extended(const int2 &texel) const
T load_pixel(const int2 &texel) const
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
const T & get_single_value() const
StructureType structure_type
Result compute_max_tile_velocity_gpu()
NodeOperation(Context &context, const bNode &node)
gpu::StorageBuf * dilate_max_velocity_gpu(Result &max_tile_velocity)
void compute_motion_blur(Result &max_tile_velocity, gpu::StorageBuf *tile_indirection_buffer)
#define MOTION_BLUR_TILE_SIZE
const ccl_global KernelWorkTile * tile
void node_register_type(bNodeType &ntype)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
ColorSceneLinear4f< eAlpha::Premultiplied > Color
void parallel_for(const int2 range, const Function &function)
T length_squared(const VecBase< T, Size > &a)
T clamp(const T &a, const T &min, const T &max)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static float2 max_velocity(const float2 &a, const float2 &b)
static uint32_t velocity_atomic_max_value(const float2 &value, const int2 &texel)
static void node_register()
static float2 spread_compare(const float center_motion_length, const float sample_motion_length, const float offset_length)
static float dir_compare(const float2 &offset, const float2 &sample_motion, const float &sample_motion_length)
static Result compute_max_tile_velocity_cpu(Context &context, const Result &velocity_image)
static float2 sample_weights(const float center_depth, const float sample_depth, const float center_motion_length, const float sample_motion_length, const float offset_length)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void gather_blur(const Result &input_image, const Result &input_depth, const Result &input_velocity, const int2 &size, const float2 &screen_uv, const float2 ¢er_motion, const float center_depth, const float2 &max_motion, const float ofs, const bool next, const int samples_count, const float shutter_speed, Accumulator &accum)
static bool is_inside_motion_line(const int2 &tile, const MotionLine &motion_line)
static MotionLine compute_motion_line(const int2 &tile, const float2 &motion)
static MotionRect compute_motion_rect(const int2 &tile, const float2 &motion, const int2 &size)
static Result dilate_max_velocity_cpu(Context &context, const Result &max_tile_velocity, const float shutter_speed)
static void motion_blur_cpu(const Result &input_image, const Result &input_depth, const Result &input_velocity, const Result &max_velocity, Result &output, const int samples_count, const float shutter_speed)
static float interleaved_gradient_noise(const int2 &p)
static void node_declare(NodeDeclarationBuilder &b)
static void gather_sample(const Result &input_image, const Result &input_depth, const Result &input_velocity, const int2 &size, const float2 &screen_uv, const float center_depth, const float center_motion_len, const float2 &offset, const float offset_len, const bool next, const float shutter_speed, Accumulator &accum)
static float2 depth_compare(const float center_depth, const float sample_depth)
static float2 max_velocity_approximate(const float2 &a, const float2 &b, const int2 &a_texel, const int2 &b_texel)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
void GPU_storagebuf_clear_to_zero(gpu::StorageBuf *ssbo)
gpu::StorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_memory_barrier(GPUBarrier barrier)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_SHADER_STORAGE
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void GPU_storagebuf_unbind(gpu::StorageBuf *ssbo)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
std::string ui_description
NodeGetCompositorOperationFunction get_compositor_operation
const char * enum_name_legacy
NodeDeclareFunction declare