[Bf-blender-cvs] [d518dc411ea] tmp-workbench-rewrite2: TAA

Miguel Pozo noreply at git.blender.org
Fri Oct 28 15:11:17 CEST 2022


Commit: d518dc411eaa5243a3717a0146f93faaae29ffcc
Author: Miguel Pozo
Date:   Wed Oct 26 20:37:09 2022 +0200
Branches: tmp-workbench-rewrite2
https://developer.blender.org/rBd518dc411eaa5243a3717a0146f93faaae29ffcc

TAA

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

M	source/blender/draw/engines/workbench/shaders/workbench_composite_comp.glsl
M	source/blender/draw/engines/workbench/shaders/workbench_effect_smaa_frag.glsl
M	source/blender/draw/engines/workbench/shaders/workbench_transparent_resolve_frag.glsl
M	source/blender/draw/engines/workbench/workbench_effect_antialiasing.cc
M	source/blender/draw/engines/workbench/workbench_engine.cc
M	source/blender/draw/engines/workbench/workbench_private.hh

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

diff --git a/source/blender/draw/engines/workbench/shaders/workbench_composite_comp.glsl b/source/blender/draw/engines/workbench/shaders/workbench_composite_comp.glsl
index 9a652d39542..c18d4ba843d 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_composite_comp.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_composite_comp.glsl
@@ -75,10 +75,6 @@ void main()
 #endif
 
   if (color != world_data.background_color) {
-    /* TODO(fclem): Port the TAA shader that does this tranformation. */
-    /* Use log2 space to avoid highlights creating too much aliasing. */
-    /* TODO(Miguel Pozo): Re-enable */
-    // color.rgb = log2(color.rgb + 0.5);
     imageStore(out_color_img, texel, color);
   }
 }
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_effect_smaa_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_effect_smaa_frag.glsl
index ad55eab9e44..8b9e3f968ea 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_effect_smaa_frag.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_effect_smaa_frag.glsl
@@ -25,8 +25,7 @@ void main()
   }
   out_color /= taaAccumulatedWeight;
   /* Exit log2 space used for Antialiasing. */
-  /* TODO(Miguel Pozo): Re-enable */
-  // out_color = exp2(out_color) - 0.5;
+  out_color = exp2(out_color) - 0.5;
 
   /* Avoid float precision issue. */
   if (out_color.a > 0.999) {
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_transparent_resolve_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_transparent_resolve_frag.glsl
index b314d949308..35bea830bac 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_transparent_resolve_frag.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_transparent_resolve_frag.glsl
@@ -16,9 +16,4 @@ void main()
   /* Listing 4 */
   fragColor.rgb = trans_accum.rgb / clamp(trans_weight, 1e-4, 5e4);
   fragColor.a = 1.0 - trans_reveal;
-
-  /* TODO(fclem): Port the TAA shader that does this tranformation. */
-  /* Use log2 space to avoid highlights creating too much aliasing. */
-  /* TODO(Miguel Pozo): Re-enable */
-  // fragColor.rgb = log2(fragColor.rgb + 0.5);
 }
diff --git a/source/blender/draw/engines/workbench/workbench_effect_antialiasing.cc b/source/blender/draw/engines/workbench/workbench_effect_antialiasing.cc
index 6a471765241..b7275eefb3b 100644
--- a/source/blender/draw/engines/workbench/workbench_effect_antialiasing.cc
+++ b/source/blender/draw/engines/workbench/workbench_effect_antialiasing.cc
@@ -19,7 +19,6 @@ class TaaSamples {
 
     for (int i : samples.index_range()) {
       float2 sample = samples[i];
-      printf("%f : %f", sample.x, sample.y);
       const float squared_dist = len_squared_v2(sample);
       if (squared_dist < closest_squared_distance) {
         closest_squared_distance = squared_dist;
@@ -77,8 +76,37 @@ class TaaSamples {
 
 static TaaSamples TAA_SAMPLES = TaaSamples();
 
+static float filter_blackman_harris(float x, const float width)
+{
+  if (x > width * 0.5f) {
+    return 0.0f;
+  }
+  x = 2.0f * M_PI * clamp_f((x / width + 0.5f), 0.0f, 1.0f);
+  return 0.35875f - 0.48829f * cosf(x) + 0.14128f * cosf(2.0f * x) - 0.01168f * cosf(3.0f * x);
+}
+
+/* Compute weights for the 3x3 neighborhood using a 1.5px filter. */
+static void setup_taa_weights(const float2 offset, float r_weights[9], float &r_weight_sum)
+{
+  /* NOTE: If filter width is bigger than 2.0f, then we need to sample more neighborhood. */
+  const float filter_width = 2.0f;
+  r_weight_sum = 0.0f;
+  int i = 0;
+  for (int x = -1; x <= 1; x++) {
+    for (int y = -1; y <= 1; y++, i++) {
+      float2 sample_co = float2(x, y) - offset;
+      float r = len_v2(sample_co);
+      /* fclem: is radial distance ok here? */
+      float weight = filter_blackman_harris(r, filter_width);
+      r_weight_sum += weight;
+      r_weights[i] = weight;
+    }
+  }
+}
+
 AntiAliasingPass::AntiAliasingPass()
 {
+  taa_accumulation_sh = GPU_shader_create_from_info_name("workbench_taa");
   smaa_edge_detect_sh = GPU_shader_create_from_info_name("workbench_smaa_stage_0");
   smaa_aa_weight_sh = GPU_shader_create_from_info_name("workbench_smaa_stage_1");
   smaa_resolve_sh = GPU_shader_create_from_info_name("workbench_smaa_stage_2");
@@ -94,90 +122,254 @@ AntiAliasingPass::AntiAliasingPass()
 
 AntiAliasingPass::~AntiAliasingPass()
 {
-  if (smaa_edge_detect_sh) {
-    GPU_shader_free(smaa_edge_detect_sh);
-  }
-  if (smaa_aa_weight_sh) {
-    GPU_shader_free(smaa_aa_weight_sh);
-  }
-  if (smaa_resolve_sh) {
-    GPU_shader_free(smaa_resolve_sh);
-  }
+  DRW_SHADER_FREE_SAFE(taa_accumulation_sh);
+  DRW_SHADER_FREE_SAFE(smaa_edge_detect_sh);
+  DRW_SHADER_FREE_SAFE(smaa_aa_weight_sh);
+  DRW_SHADER_FREE_SAFE(smaa_resolve_sh);
 }
 
-void AntiAliasingPass::init(bool reset_taa)
+void AntiAliasingPass::reset_taa()
+{
+  reset_next_sample = true;
+}
+
+void AntiAliasingPass::init()
 {
   is_playback = DRW_state_is_playback();
   is_navigating = DRW_state_is_navigating();
 
-  if (reset_taa || is_playback || is_navigating) {
-    taa_sample = 0;
+  {
+    /*TODO(Miguel Pozo): Move to Draw Settings */
+    const DRWContextState *draw_ctx = DRW_context_state_get();
+    const View3D *v3d = draw_ctx->v3d;
+    const SceneDisplay &display = draw_ctx->scene->display;
+
+    sample_len = U.viewport_aa;
+    if (is_navigating || is_playback) {
+      /* Only draw using SMAA or no AA when navigating. */
+      sample_len = min_ii(sample_len, 1);
+    }
+    else if (v3d && ELEM(v3d->shading.type, OB_RENDER, OB_MATERIAL)) {
+      sample_len = display.viewport_aa;
+    }
+    else if (DRW_state_is_image_render()) {
+      sample_len = display.render_aa;
+    }
+  }
+
+  /* Reset complete drawing when navigating or during viewport playback or when
+   * leaving one of those states. In case of multires modifier the navigation
+   * mesh differs from the viewport mesh, so we need to be sure to restart. */
+  if (reset_next_sample || is_playback || is_navigating) {
+    sample = 0;
+  }
+  reset_next_sample = is_playback || is_navigating;
+
+  /* Reset the TAA when we have already draw a sample, but the sample count differs from previous
+   * time. This removes render artifacts when the viewport anti-aliasing in the user preferences
+   * is set to a lower value. */
+  if (sample_len != sample_len_previous) {
+    sample = 0;
+    sample_len_previous = sample_len;
+  }
+
+  /*TODO(Miguel Pozo): This can probably removed.*/
+  /*
+  if (sample_len > 0 && valid_history == false) {
+    sample = 0;
+  }
+  */
+
+  /*TODO(Miguel Pozo): Move all the reset taa logic to the same place (DrawSettings?)*/
+  {
+    float4x4 matrix;
+    /*TODO(Miguel Pozo): New API ?*/
+    DRW_view_persmat_get(nullptr, matrix.ptr(), false);
+    if (matrix != last_matrix) {
+      last_matrix = matrix;
+      sample = 0;
+    }
   }
 }
 
-void AntiAliasingPass::sync(SceneResources &resources)
+void AntiAliasingPass::sync(SceneResources &resources, int2 resolution)
 {
-  {
-    smaa_edge_detect_ps_.init();
-    smaa_edge_detect_ps_.state_set(DRW_STATE_WRITE_COLOR);
-    smaa_edge_detect_ps_.shader_set(smaa_edge_detect_sh);
-    smaa_edge_detect_ps_.bind_texture("colorTex", &resources.color_tx);
-    smaa_edge_detect_ps_.push_constant("viewportMetrics", &smaa_viewport_metrics, 1);
-    smaa_edge_detect_ps_.clear_color(float4(0.0f));
-    smaa_edge_detect_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
+  if (sample_len > 0) {
+    taa_accumulation_tx.ensure_2d(GPU_RGBA16F, resolution);
+    sample0_depth_tx.ensure_2d(GPU_DEPTH24_STENCIL8, resolution);
   }
-  {
-    smaa_aa_weight_ps_.init();
-    smaa_aa_weight_ps_.state_set(DRW_STATE_WRITE_COLOR);
-    smaa_aa_weight_ps_.shader_set(smaa_aa_weight_sh);
-    smaa_aa_weight_ps_.bind_texture("edgesTex", &smaa_edge_tx);
-    smaa_aa_weight_ps_.bind_texture("areaTex", smaa_area_tx);
-    smaa_aa_weight_ps_.bind_texture("searchTex", smaa_search_tx);
-    smaa_aa_weight_ps_.push_constant("viewportMetrics", &smaa_viewport_metrics, 1);
-    smaa_aa_weight_ps_.clear_color(float4(0.0f));
-    smaa_aa_weight_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
+  else {
+    taa_accumulation_tx.free();
+    sample0_depth_tx.free();
   }
-  {
-    smaa_resolve_ps_.init();
-    smaa_resolve_ps_.state_set(DRW_STATE_WRITE_COLOR);
-    smaa_resolve_ps_.shader_set(smaa_resolve_sh);
-    smaa_resolve_ps_.bind_texture("blendTex", &smaa_weight_tx);
-    smaa_resolve_ps_.bind_texture("colorTex", &resources.color_tx);
-    smaa_resolve_ps_.push_constant("viewportMetrics", &smaa_viewport_metrics, 1);
-    smaa_resolve_ps_.push_constant("mixFactor", &smaa_mix_factor, 1);
-    smaa_resolve_ps_.push_constant("taaAccumulatedWeight", &taa_weight_accum, 1);
-    smaa_resolve_ps_.clear_color(float4(0.0f));
-    smaa_resolve_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
+
+  taa_accumulation_ps_.init();
+  taa_accumulation_ps_.state_set(sample == 0 ? DRW_STATE_WRITE_COLOR :
+                                               DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ADD_FULL);
+  taa_accumulation_ps_.shader_set(taa_accumulation_sh);
+  taa_accumulation_ps_.bind_texture("colorBuffer", &resources.color_tx);
+  taa_accumulation_ps_.push_constant("samplesWeights", weights, 9);
+  taa_accumulation_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
+
+  smaa_edge_detect_ps_.init();
+  smaa_edge_detect_ps_.state_set(DRW_STATE_WRITE_COLOR);
+  smaa_edge_detect_ps_.shader_set(smaa_edge_detect_sh);
+  smaa_edge_detect_ps_.bind_texture("colorTex", &taa_accumulation_tx);
+  smaa_edge_detect_ps_.push_constant("viewportMetrics", &smaa_viewport_metrics, 1);
+  smaa_edge_detect_ps_.clear_color(float4(0.0f));
+  smaa_edge_detect_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
+
+  smaa_aa_weight_ps_.init();
+  smaa_aa_weight_ps_.state_set(DRW_STATE_WRITE_COLOR);
+  smaa_aa_weight_ps_.shader_set(smaa_aa_weight_sh);
+  smaa_aa_weight_ps_.bind_texture("edgesTex", &smaa_edge_tx);
+  smaa_aa_weight_ps_.bind_texture("areaTex", smaa_area_tx);
+  smaa_aa_weight_ps_.bind_texture("searchTex", smaa_search_tx);
+  smaa_aa_weight_ps_.push_constant("viewportMetrics", &smaa_viewport_metrics, 1);
+  smaa_aa_weight_ps_.clear_color(flo

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list