[Bf-blender-cvs] [7f2cd2d969c] master: Realtime Compositor: Implement Tone Map node

Omar Emara noreply at git.blender.org
Thu Oct 20 15:03:41 CEST 2022


Commit: 7f2cd2d969cb227fcde406f68492492d97d1322e
Author: Omar Emara
Date:   Thu Oct 20 15:02:41 2022 +0200
Branches: master
https://developer.blender.org/rB7f2cd2d969cb227fcde406f68492492d97d1322e

Realtime Compositor: Implement Tone Map node

This patch implements the tone map node for the realtime compositor
based on the two papers:

Reinhard, Erik, et al. "Photographic tone reproduction for digital
images." Proceedings of the 29th annual conference on Computer graphics
and interactive techniques. 2002.

Reinhard, Erik, and Kate Devlin. "Dynamic range reduction inspired by
photoreceptor physiology." IEEE transactions on visualization and
computer graphics 11.1 (2005): 13-24.

The original implementation should be revisited later due to apparent
incompatibilities with the reference papers, which makes the operation
less useful.

Differential Revision: https://developer.blender.org/D16306

Reviewed By: Clement Foucault

===================================================================

M	source/blender/compositor/realtime_compositor/algorithms/COM_algorithm_parallel_reduction.hh
M	source/blender/compositor/realtime_compositor/algorithms/intern/algorithm_parallel_reduction.cc
M	source/blender/gpu/CMakeLists.txt
A	source/blender/gpu/shaders/compositor/compositor_tone_map_photoreceptor.glsl
A	source/blender/gpu/shaders/compositor/compositor_tone_map_simple.glsl
M	source/blender/gpu/shaders/compositor/infos/compositor_parallel_reduction_info.hh
A	source/blender/gpu/shaders/compositor/infos/compositor_tone_map_photoreceptor_info.hh
A	source/blender/gpu/shaders/compositor/infos/compositor_tone_map_simple_info.hh
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/nodes/composite/nodes/node_composite_tonemap.cc

===================================================================

diff --git a/source/blender/compositor/realtime_compositor/algorithms/COM_algorithm_parallel_reduction.hh b/source/blender/compositor/realtime_compositor/algorithms/COM_algorithm_parallel_reduction.hh
index 9d0851eff84..b094782c18e 100644
--- a/source/blender/compositor/realtime_compositor/algorithms/COM_algorithm_parallel_reduction.hh
+++ b/source/blender/compositor/realtime_compositor/algorithms/COM_algorithm_parallel_reduction.hh
@@ -27,6 +27,13 @@ float sum_blue(Context &context, GPUTexture *texture);
  * coefficients to compute the luminance. */
 float sum_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients);
 
+/* Computes the sum of the logarithm of the luminance of all pixels in the given texture, using the
+ * given luminance coefficients to compute the luminance. */
+float sum_log_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients);
+
+/* Computes the sum of the colors of all pixels in the given texture. */
+float4 sum_color(Context &context, GPUTexture *texture);
+
 /* --------------------------------------------------------------------
  * Sum Of Squared Difference Reductions.
  */
@@ -55,4 +62,20 @@ float sum_luminance_squared_difference(Context &context,
                                        float3 luminance_coefficients,
                                        float subtrahend);
 
+/* --------------------------------------------------------------------
+ * Maximum Reductions.
+ */
+
+/* Computes the maximum luminance of all pixels in the given texture, using the given luminance
+ * coefficients to compute the luminance. */
+float maximum_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients);
+
+/* --------------------------------------------------------------------
+ * Minimum Reductions.
+ */
+
+/* Computes the minimum luminance of all pixels in the given texture, using the given luminance
+ * coefficients to compute the luminance. */
+float minimum_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients);
+
 }  // namespace blender::realtime_compositor
diff --git a/source/blender/compositor/realtime_compositor/algorithms/intern/algorithm_parallel_reduction.cc b/source/blender/compositor/realtime_compositor/algorithms/intern/algorithm_parallel_reduction.cc
index 3266ccd14eb..139c58bc190 100644
--- a/source/blender/compositor/realtime_compositor/algorithms/intern/algorithm_parallel_reduction.cc
+++ b/source/blender/compositor/realtime_compositor/algorithms/intern/algorithm_parallel_reduction.cc
@@ -134,6 +134,34 @@ float sum_luminance(Context &context, GPUTexture *texture, float3 luminance_coef
   return sum;
 }
 
+float sum_log_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients)
+{
+  GPUShader *shader = context.shader_manager().get("compositor_sum_log_luminance");
+  GPU_shader_bind(shader);
+
+  GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
+
+  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
+  const float sum = *reduced_value;
+  MEM_freeN(reduced_value);
+  GPU_shader_unbind();
+
+  return sum;
+}
+
+float4 sum_color(Context &context, GPUTexture *texture)
+{
+  GPUShader *shader = context.shader_manager().get("compositor_sum_color");
+  GPU_shader_bind(shader);
+
+  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_RGBA32F);
+  const float4 sum = float4(reduced_value);
+  MEM_freeN(reduced_value);
+  GPU_shader_unbind();
+
+  return sum;
+}
+
 /* --------------------------------------------------------------------
  * Sum Of Squared Difference Reductions.
  */
@@ -202,4 +230,42 @@ float sum_luminance_squared_difference(Context &context,
   return sum;
 }
 
+/* --------------------------------------------------------------------
+ * Maximum Reductions.
+ */
+
+float maximum_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients)
+{
+  GPUShader *shader = context.shader_manager().get("compositor_maximum_luminance");
+  GPU_shader_bind(shader);
+
+  GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
+
+  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
+  const float maximum = *reduced_value;
+  MEM_freeN(reduced_value);
+  GPU_shader_unbind();
+
+  return maximum;
+}
+
+/* --------------------------------------------------------------------
+ * Minimum Reductions.
+ */
+
+float minimum_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients)
+{
+  GPUShader *shader = context.shader_manager().get("compositor_minimum_luminance");
+  GPU_shader_bind(shader);
+
+  GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
+
+  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
+  const float minimum = *reduced_value;
+  MEM_freeN(reduced_value);
+  GPU_shader_unbind();
+
+  return minimum;
+}
+
 }  // namespace blender::realtime_compositor
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 5a1e0cde1d8..56031b2722b 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -357,6 +357,8 @@ set(GLSL_SRC
   shaders/compositor/compositor_split_viewer.glsl
   shaders/compositor/compositor_symmetric_blur.glsl
   shaders/compositor/compositor_symmetric_separable_blur.glsl
+  shaders/compositor/compositor_tone_map_photoreceptor.glsl
+  shaders/compositor/compositor_tone_map_simple.glsl
 
   shaders/compositor/library/gpu_shader_compositor_alpha_over.glsl
   shaders/compositor/library/gpu_shader_compositor_blur_common.glsl
@@ -639,6 +641,8 @@ set(SRC_SHADER_CREATE_INFOS
   shaders/compositor/infos/compositor_split_viewer_info.hh
   shaders/compositor/infos/compositor_symmetric_blur_info.hh
   shaders/compositor/infos/compositor_symmetric_separable_blur_info.hh
+  shaders/compositor/infos/compositor_tone_map_photoreceptor_info.hh
+  shaders/compositor/infos/compositor_tone_map_simple_info.hh
 )
 
 set(SRC_SHADER_CREATE_INFOS_MTL
diff --git a/source/blender/gpu/shaders/compositor/compositor_tone_map_photoreceptor.glsl b/source/blender/gpu/shaders/compositor/compositor_tone_map_photoreceptor.glsl
new file mode 100644
index 00000000000..167006585ca
--- /dev/null
+++ b/source/blender/gpu/shaders/compositor/compositor_tone_map_photoreceptor.glsl
@@ -0,0 +1,22 @@
+#pragma BLENDER_REQUIRE(gpu_shader_compositor_texture_utilities.glsl)
+
+/* Tone mapping based on equation (1) and the trilinear interpolation between equations (6) and (7)
+ * from Reinhard, Erik, and Kate Devlin. "Dynamic range reduction inspired by photoreceptor
+ * physiology." IEEE transactions on visualization and computer graphics 11.1 (2005): 13-24. */
+void main()
+{
+  ivec2 texel = ivec2(gl_GlobalInvocationID.xy);
+
+  vec4 input_color = texture_load(input_tx, texel);
+  float input_luminance = dot(input_color.rgb, luminance_coefficients);
+
+  /* Trilinear interpolation between equations (6) and (7) from Reinhard's 2005 paper. */
+  vec4 local_adaptation_level = mix(vec4(input_luminance), input_color, chromatic_adaptation);
+  vec4 adaptation_level = mix(global_adaptation_level, local_adaptation_level, light_adaptation);
+
+  /* Equation (1) from Reinhard's 2005 paper, assuming Vmax is 1. */
+  vec4 semi_saturation = pow(intensity * adaptation_level, vec4(contrast));
+  vec4 tone_mapped_color = input_color / (input_color + semi_saturation);
+
+  imageStore(output_img, texel, vec4(tone_mapped_color.rgb, input_color.a));
+}
diff --git a/source/blender/gpu/shaders/compositor/compositor_tone_map_simple.glsl b/source/blender/gpu/shaders/compositor/compositor_tone_map_simple.glsl
new file mode 100644
index 00000000000..ce42d021dd1
--- /dev/null
+++ b/source/blender/gpu/shaders/compositor/compositor_tone_map_simple.glsl
@@ -0,0 +1,26 @@
+#pragma BLENDER_REQUIRE(gpu_shader_compositor_texture_utilities.glsl)
+#pragma BLENDER_REQUIRE(gpu_shader_common_math_utils.glsl)
+
+/* Tone mapping based on equation (3) from Reinhard, Erik, et al. "Photographic tone reproduction
+ * for digital images." Proceedings of the 29th annual conference on Computer graphics and
+ * interactive techniques. 2002. */
+void main()
+{
+  ivec2 texel = ivec2(gl_GlobalInvocationID.xy);
+
+  vec4 input_color = texture_load(input_tx, texel);
+
+  /* Equation (2) from Reinhard's 2002 paper. */
+  vec4 scaled_color = input_color * luminance_scale;
+
+  /* Equation (3) from Reinhard's 2002 paper, but with the 1 replaced with the blend factor for
+   * more flexibility. See ToneMapOperation::compute_luminance_scale_blend_factor. */
+  vec4 denominator = luminance_scale_blend_factor + scaled_color;
+  vec4 tone_mapped_color = safe_divide(scaled_color, denominator);
+
+  if (inverse_gamma != 0.0) {
+    tone_mapped_color = pow(max(tone_mapped_color, vec4(0.0)), vec4(inverse_gamma));
+  }
+
+  imageStore(output_img, texel, vec4(tone_mapped_color.rgb, input_color.a));
+}
diff --git a/source/blender/gpu/shaders/compositor/infos/compositor_parallel_reduction_info.hh b/source/blender/gpu/shaders/compositor/infos/compositor_parallel_reduction_info.hh
index 2e661f280af..4a71bdc80e2 100644
--- a/source/blender/gpu/shaders/compositor/infos/compositor_parallel_reduction_info.hh
+++ b/source/blender/gpu/shaders/compositor/infos/compositor_parallel_reduction_info.hh
@@ -12,14 +12,17 @@ GPU_SHADER_CREATE_INFO(compositor_parallel_reduction_shared)
  * Sum Reductions.
  */
 
-GPU_SHADER_CREATE_INFO(compositor_sum_float_shared)
+GPU_SHADER_CREATE_INFO(compositor_sum_shared)
     .additional_info("compositor_parallel_reduction_shared")
-    .image(0, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D, "output_img")
-    .define("TYPE", "float")
     .define("IDENTITY", "vec4(0.0)")
-    .define("LOAD(value)", "value.x")
     .define("REDUCE(lhs, rhs)", "lhs + rhs");
 
+GPU_SHADER_CREATE_INFO(compositor_sum_float_shared)
+    .additional_info("compositor_sum_shared")
+    .image(0, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D, "output_img")
+    .define("TYPE", "float")
+    .define("LOAD(value)", "value.x");
+
 GPU_SHADER_CREATE_INFO(compositor_sum_red)
    

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list