[Bf-blender-cvs] [dcdf29d936f] tmp-wbench-next-shadow-backup: Workbench Next: Shadows: Compute based culling

Miguel Pozo noreply at git.blender.org
Mon Dec 19 13:11:59 CET 2022


Commit: dcdf29d936fd8667ad1ead8c385f6b485911ab0b
Author: Miguel Pozo
Date:   Thu Dec 15 17:51:20 2022 +0100
Branches: tmp-wbench-next-shadow-backup
https://developer.blender.org/rBdcdf29d936fd8667ad1ead8c385f6b485911ab0b

Workbench Next: Shadows: Compute based culling

fix 1

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/engines/workbench/shaders/infos/workbench_shadow_info.hh
M	source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
M	source/blender/draw/engines/workbench/shaders/workbench_shadow_visibility_comp.glsl
M	source/blender/draw/engines/workbench/workbench_engine.cc
M	source/blender/draw/engines/workbench/workbench_private.hh
M	source/blender/draw/engines/workbench/workbench_shader_shared.h
M	source/blender/draw/engines/workbench/workbench_shadow.cc
M	source/blender/draw/intern/draw_manager.cc
M	source/blender/draw/intern/draw_view.cc
M	source/blender/draw/intern/draw_view.hh

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index c747e191e3e..7e51319e681 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -299,8 +299,11 @@ set(SRC
   engines/image/image_space_node.hh
   engines/image/image_texture_info.hh
   engines/image/image_usage.hh
+  engines/workbench/workbench_defines.hh
   engines/workbench/workbench_engine.h
+  engines/workbench/workbench_enums.hh
   engines/workbench/workbench_private.h
+  engines/workbench/workbench_private.hh
   engines/workbench/workbench_shader_shared.h
   engines/select/select_engine.h
   engines/select/select_private.h
@@ -490,6 +493,7 @@ set(GLSL_SRC
   engines/workbench/shaders/workbench_shadow_debug_frag.glsl
   engines/workbench/shaders/workbench_shadow_geom.glsl
   engines/workbench/shaders/workbench_shadow_vert.glsl
+  engines/workbench/shaders/workbench_shadow_visibility_comp.glsl
   engines/workbench/shaders/workbench_transparent_accum_frag.glsl
   engines/workbench/shaders/workbench_transparent_resolve_frag.glsl
   engines/workbench/shaders/workbench_volume_frag.glsl
diff --git a/source/blender/draw/engines/workbench/shaders/infos/workbench_shadow_info.hh b/source/blender/draw/engines/workbench/shaders/infos/workbench_shadow_info.hh
index ad6b3560117..ecccb2083b1 100644
--- a/source/blender/draw/engines/workbench/shaders/infos/workbench_shadow_info.hh
+++ b/source/blender/draw/engines/workbench/shaders/infos/workbench_shadow_info.hh
@@ -24,27 +24,41 @@ GPU_SHADER_CREATE_INFO(workbench_shadow_common)
 GPU_SHADER_CREATE_INFO(workbench_next_shadow_common)
     .vertex_in(0, Type::VEC3, "pos")
     .vertex_out(workbench_shadow_iface)
-    .push_constant(Type::FLOAT, "lightDistance")
+    .define("WORKBENCH_NEXT")
+    .uniform_buf(1, "ShadowPassData", "pass_data")
     .push_constant(Type::VEC3, "lightDirection")
+    .typedef_source("workbench_shader_shared.h")
     .vertex_source("workbench_shadow_vert.glsl")
     .additional_info("draw_view")
     .additional_info("draw_modelmat_new")
     .additional_info("draw_resource_handle_new");
 
-GPU_SHADER_CREATE_INFO(workbench_next_shadow_visibility_compute)
-    .do_static_compilation(true)
+GPU_SHADER_CREATE_INFO(workbench_next_shadow_visibility_compute_common)
     .local_group_size(DRW_VISIBILITY_GROUP_SIZE)
     .define("DRW_VIEW_LEN", "64")
     .storage_buf(0, Qualifier::READ, "ObjectBounds", "bounds_buf[]")
-    .storage_buf(1, Qualifier::READ_WRITE, "uint", "visibility_buf[]")
-    .storage_buf(2, Qualifier::READ, "uint", "pass_technique_buf[]")
+    .uniform_buf(2, "ExtrudedFrustum", "extruded_frustum")
+    .push_constant(Type::BOOL, "forced_fail_pass")
     .push_constant(Type::INT, "resource_len")
     .push_constant(Type::INT, "view_len")
     .push_constant(Type::INT, "visibility_word_per_draw")
     .push_constant(Type::VEC3, "shadow_direction")
-    .compute_source("draw_visibility_comp.glsl")
+    .typedef_source("workbench_shader_shared.h")
+    .compute_source("workbench_shadow_visibility_comp.glsl")
     .additional_info("draw_view", "draw_view_culling");
 
+GPU_SHADER_CREATE_INFO(workbench_next_shadow_visibility_compute_dynamic_pass_type)
+    .additional_info("workbench_next_shadow_visibility_compute_common")
+    .define("DYNAMIC_PASS_SELECTION")
+    .storage_buf(1, Qualifier::READ_WRITE, "uint", "pass_visibility_buf[]")
+    .storage_buf(2, Qualifier::READ_WRITE, "uint", "fail_visibility_buf[]")
+    .do_static_compilation(true);
+
+GPU_SHADER_CREATE_INFO(workbench_next_shadow_visibility_compute_static_pass_type)
+    .additional_info("workbench_next_shadow_visibility_compute_common")
+    .storage_buf(1, Qualifier::READ_WRITE, "uint", "visibility_buf[]")
+    .do_static_compilation(true);
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl b/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
index a220434ec45..c3664da66ad 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
@@ -5,5 +5,17 @@ void main()
 {
   vData.pos = pos;
   vData.frontPosition = point_object_to_ndc(pos);
+#ifdef WORKBENCH_NEXT
+  vec3 pos_ws = point_object_to_world(pos);
+  float extrude_distance = 1e5f;
+  float LDoFP = dot(pass_data.light_direction_ws, pass_data.far_plane.xyz);
+  if (LDoFP > 0) {
+    float signed_distance = dot(pass_data.far_plane.xyz, pos_ws) - pass_data.far_plane.w;
+    extrude_distance = -signed_distance / LDoFP;
+  }
+  vData.backPosition = point_world_to_ndc(pos_ws +
+                                          pass_data.light_direction_ws * extrude_distance);
+#else
   vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance);
+#endif
 }
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_shadow_visibility_comp.glsl b/source/blender/draw/engines/workbench/shaders/workbench_shadow_visibility_comp.glsl
index 7e89120a79f..ddf655ec006 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_shadow_visibility_comp.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_shadow_visibility_comp.glsl
@@ -9,45 +9,90 @@
 
 shared uint shared_result;
 
-void mask_visibility_bit(uint view_id)
+#ifdef DYNAMIC_PASS_SELECTION
+void set_visibility(bool pass, bool fail)
 {
-  if (view_len > 1) {
-    uint index = gl_GlobalInvocationID.x * uint(visibility_word_per_draw) + (view_id / 32u);
-    visibility_buf[index] &= ~(1u << view_id);
+  if (!pass) {
+    atomicAnd(pass_visibility_buf[gl_WorkGroupID.x], ~(1u << gl_LocalInvocationID.x));
   }
-  else {
+  if (!fail) {
+    atomicAnd(fail_visibility_buf[gl_WorkGroupID.x], ~(1u << gl_LocalInvocationID.x));
+  }
+}
+#else
+void set_visibility(bool visibility)
+{
+  if (!visibility) {
     atomicAnd(visibility_buf[gl_WorkGroupID.x], ~(1u << gl_LocalInvocationID.x));
   }
 }
+#endif
 
-void main()
+bool is_visible(IsectBox box)
 {
-  if (gl_GlobalInvocationID.x >= resource_len) {
-    return;
+  for (int i_plane = 0; i_plane < extruded_frustum.planes_count; i_plane++) {
+    vec4 plane = extruded_frustum.planes[i_plane];
+    bool separating_axis = true;
+    for (int i_corner = 0; i_corner < 8; i_corner++) {
+      float signed_distance = dot(box.corners[i_corner], plane.xyz) - plane.w;
+      if (signed_distance <= 0) {
+        separating_axis = false;
+        break;
+      }
+    }
+    if (separating_axis) {
+      return false;
+    }
   }
+  return true;
+}
 
-  ObjectBounds bounds = bounds_buf[gl_GlobalInvocationID.x];
+bool intersects_near_plane(IsectBox box)
+{
+  vec4 near_plane = drw_view_culling.planes[4];
+  bool on_positive_side = false;
+  bool on_negative_side = false;
 
-  if (bounds.bounding_sphere.w != -1.0) {
-    IsectBox box = isect_data_setup(bounds.bounding_corners[0].xyz,
-                                    bounds.bounding_corners[1].xyz,
-                                    bounds.bounding_corners[2].xyz,
-                                    bounds.bounding_corners[3].xyz);
-    Sphere bounding_sphere = Sphere(bounds.bounding_sphere.xyz, bounds.bounding_sphere.w);
-    Sphere inscribed_sphere = Sphere(bounds.bounding_sphere.xyz, bounds._inner_sphere_radius);
-
-    for (drw_view_id = 0; drw_view_id < view_len; drw_view_id++) {
-      if (intersect_view(inscribed_sphere) == true) {
-        /* Visible. */
+  for (int i_corner = 0; i_corner < 8; i_corner++) {
+    for (int i_displace = 0; i_displace < 2; i_displace++) {
+      vec3 corner = box.corners[i_corner] + (shadow_direction * 1e5f * i_displace);
+      float signed_distance = dot(corner, -near_plane.xyz) - near_plane.w;
+      if (signed_distance <= 0) {
+        on_negative_side = true;
       }
-      else if (intersect_view(bounding_sphere) == false) {
-        /* Not visible. */
-        mask_visibility_bit(drw_view_id);
+      else {
+        on_positive_side = true;
       }
-      else if (intersect_view(box) == false) {
-        /* Not visible. */
-        mask_visibility_bit(drw_view_id);
+      if (on_negative_side && on_positive_side) {
+        return true;
       }
     }
   }
+
+  return false;
+}
+
+void main()
+{
+  if (gl_GlobalInvocationID.x >= resource_len) {
+    return;
+  }
+
+  ObjectBounds bounds = bounds_buf[gl_GlobalInvocationID.x];
+  IsectBox box = isect_data_setup(bounds.bounding_corners[0].xyz,
+                                  bounds.bounding_corners[1].xyz,
+                                  bounds.bounding_corners[2].xyz,
+                                  bounds.bounding_corners[3].xyz);
+
+#ifdef DYNAMIC_PASS_SELECTION
+  if (is_visible(box)) {
+    bool use_fail_pass = intersects_near_plane(box);
+    set_visibility(!use_fail_pass, use_fail_pass);
+  }
+  else {
+    set_visibility(false, false);
+  }
+#else
+  set_visibility(is_visible(box));
+#endif
 }
diff --git a/source/blender/draw/engines/workbench/workbench_engine.cc b/source/blender/draw/engines/workbench/workbench_engine.cc
index bb71816b07c..8805657acfd 100644
--- a/source/blender/draw/engines/workbench/workbench_engine.cc
+++ b/source/blender/draw/engines/workbench/workbench_engine.cc
@@ -337,7 +337,6 @@ class Instance {
     }
 
     opaque_ps.draw(manager, view, resources, resolution, &shadow_ps);
-    // shadow_ps.draw(manager, view, resources, resolution);
     transparent_ps.draw(manager, view, resources, resolution);
     transparent_depth_ps.draw(manager, view, resources, resolution);
 
@@ -363,12 +362,6 @@ class Instance {
   }
 };
 
-void ObjectData::init(DrawData *dd)
-{
-  ObjectData *data = (ObjectData *)dd;
-  data->shadow_data.init();
-}
-
 /* This returns an array of nullptr GPUMaterial pointers so we can call
  * DRW_cache_object_surface_material_get. They never get actually used.
  */
diff --git a/source/blender/draw/engines/workbench/workbench_private.hh b/source/blender/draw/engines/workbench/workbench_private.hh
index ac92cb19eb1..231be03ae3e 100644
--- a/source/blender/draw/engines/workbench/workbench_private.hh
+++ b/source/blender/draw/engines/workbench/workbench_private.hh
@@ -197,7 +197,7 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list