WIP: Sculpt: new C++ brush vertex API #109549

Draft
Joseph Eagar wants to merge 14 commits from JosephEagar/blender:temp-sculpt-brush-iter into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This PR implements a new C++ API for sculpt brushes, blender::bke::pbvh::foreach_brush_verts. It is meant to replace the current brush code pattern. Brushes are created in single functions using a lambda callback API. There are four callbacks:

Note: Review the API first.

  • A vertex filter callback. This tests if a vertex is inside the brush, and also returns the squared distance. Brushes thus still handle radius testing and falloff on their own.
  • A pre-execution node visit callback. It returns any custom per-node data this brush needs as a struct.
  • The main execution callback. We use a generic lambda to allow for greater template specialization of the underlying iterators.
  • A post-execution node visit callback.

The execution callback is provided a range, whose iteration values provide the following members, similar to PBVHVertIter:

{
  PBVHVertRef vertex;
  float *co;
  const float *no;
  float *mask;
  bool is_mesh;
  BrushsCustomNodeData node_data;
  PBVHNode *node;
  int vertex_node_index; /* PBVHVertIter.i */
}

Internally foreach_brush_verts works by building a flat list of vertices which is allocated to tasks via parallel_for. It still uses BKE_pbvh_vertex_iter_begin (which will need its own C++ implementation).

This is faster in some situations but not others. SCULPT_smooth has been ported to the new API (the new and old versions are executed and runtimes are printed in the console). The new API is faster on multi-core CPUs on mid-resolution meshes, where the old system fails to allocate work to enough threads. The overhead of building the flat vertex list is not worth it for single-threaded CPUs or meshes with a high resolution to achieve multicore task saturation.

To solve this I plan to write a second node-based iterator back-end, that can be used on single-core systems or if some heuristic of the ratio of nodes to vertices is met.

Example:


void ExampleBrush(
    Sculpt *sd, Object *ob, Span<PBVHNode *> nodes, float bstrength)
{
  SCOPED_TIMER(__func__);

  SculptSession *ss = ob->sculpt;
  Brush *brush = BKE_paint_brush(&sd->paint);

  /* Per-node custom data. */
  struct NodeData {
    AutomaskingNodeData automask_data;
  };

  SculptBrushTest test;
  SculptBrushTestFn sculpt_brush_test_sq_fn = SCULPT_brush_test_init_with_falloff_shape(
      ss, &test, brush->falloff_shape);

  blender::bke::pbvh::foreach_brush_verts<NodeData>(
      ss->pbvh,
      nodes,
      true,
      /* Filter brush verts. */
      [&](PBVHVertRef vertex, const float *co, const float *no, float mask, float *r_dist) {
        SculptBrushTest test2 = test;
        bool ret = sculpt_brush_test_sq_fn(&test2, co);
        if (ret) {
          *r_dist = test2.dist;
        }
        return ret;
      },

      /* Visit nodes before run. */
      [&](PBVHNode *node) {
        NodeData data = {};
        SCULPT_automasking_node_begin(ob, ss, ss->cache->automasking, &data.automask_data, node);
        return data;
      },
      
      /*  Main execution block.  We use a generic lambda to allow
        *  for iterator specialization.
        */
      [&](auto range) {
        const int thread_id = BLI_task_parallel_thread_id(nullptr);
        for (auto &vd : range) {
           SCULPT_automasking_node_update<NodeData>(ss, &vd.node_data->automask_data, vd);
           //...brush code, use vd.co, vd.no, vd.vertex, etc.
          }
        }
      },
      [&](PBVHNode *node, NodeData *node_data) {});
  }
This PR implements a new C++ API for sculpt brushes, `blender::bke::pbvh::foreach_brush_verts`. It is meant to replace the current brush code pattern. Brushes are created in single functions using a lambda callback API. There are four callbacks: **Note: Review the API first.** * A vertex filter callback. This tests if a vertex is inside the brush, and also returns the squared distance. Brushes thus still handle radius testing and falloff on their own. * A pre-execution node visit callback. It returns any custom per-node data this brush needs as a struct. * The main execution callback. We use a generic lambda to allow for greater template specialization of the underlying iterators. * A post-execution node visit callback. The execution callback is provided a range, whose iteration values provide the following members, similar to PBVHVertIter: ```Cpp { PBVHVertRef vertex; float *co; const float *no; float *mask; bool is_mesh; BrushsCustomNodeData node_data; PBVHNode *node; int vertex_node_index; /* PBVHVertIter.i */ } ``` Internally `foreach_brush_verts` works by building a flat list of vertices which is allocated to tasks via `parallel_for`. It still uses `BKE_pbvh_vertex_iter_begin` (which will need its own C++ implementation). This is faster in some situations but not others. `SCULPT_smooth` has been ported to the new API (the new *and old* versions are executed and runtimes are printed in the console). The new API is faster on multi-core CPUs on mid-resolution meshes, where the old system fails to allocate work to enough threads. The overhead of building the flat vertex list is not worth it for single-threaded CPUs or meshes with a high resolution to achieve multicore task saturation. To solve this I plan to write a second node-based iterator back-end, that can be used on single-core systems or if some heuristic of the ratio of nodes to vertices is met. Example: ```Cpp void ExampleBrush( Sculpt *sd, Object *ob, Span<PBVHNode *> nodes, float bstrength) { SCOPED_TIMER(__func__); SculptSession *ss = ob->sculpt; Brush *brush = BKE_paint_brush(&sd->paint); /* Per-node custom data. */ struct NodeData { AutomaskingNodeData automask_data; }; SculptBrushTest test; SculptBrushTestFn sculpt_brush_test_sq_fn = SCULPT_brush_test_init_with_falloff_shape( ss, &test, brush->falloff_shape); blender::bke::pbvh::foreach_brush_verts<NodeData>( ss->pbvh, nodes, true, /* Filter brush verts. */ [&](PBVHVertRef vertex, const float *co, const float *no, float mask, float *r_dist) { SculptBrushTest test2 = test; bool ret = sculpt_brush_test_sq_fn(&test2, co); if (ret) { *r_dist = test2.dist; } return ret; }, /* Visit nodes before run. */ [&](PBVHNode *node) { NodeData data = {}; SCULPT_automasking_node_begin(ob, ss, ss->cache->automasking, &data.automask_data, node); return data; }, /* Main execution block. We use a generic lambda to allow * for iterator specialization. */ [&](auto range) { const int thread_id = BLI_task_parallel_thread_id(nullptr); for (auto &vd : range) { SCULPT_automasking_node_update<NodeData>(ss, &vd.node_data->automask_data, vd); //...brush code, use vd.co, vd.no, vd.vertex, etc. } } }, [&](PBVHNode *node, NodeData *node_data) {}); } ```
Joseph Eagar added 1 commit 2023-06-30 09:15:54 +02:00
7e3ad1fbd0 Sculpt: C++ new brush vertex API
This PR implements a new C++ API for brush code,
blender::bke::pbvh::foreach_brush_verts.
It gathers verts visible to a brush
into a single flat array that is fed to
parallel_for.  This is much more efficient
than breaking brush tasks down by PBVHNodes.

Brush code is also much more clearer, as brushes can
be written in one function with lambdas.

Note: this does not itself replace
BKE_pbvh_vertex_iter_begin although it will
replace most uses of it.
Joseph Eagar added 4 commits 2023-07-03 06:42:14 +02:00
Joseph Eagar added 1 commit 2023-07-03 09:15:05 +02:00
Joseph Eagar added 1 commit 2023-07-03 14:19:39 +02:00
Joseph Eagar added 1 commit 2023-07-04 03:22:53 +02:00
Joseph Eagar added 1 commit 2023-07-10 15:15:32 +02:00
Joseph Eagar added 2 commits 2023-07-10 20:12:22 +02:00
Joseph Eagar added 1 commit 2023-07-10 20:33:30 +02:00
Joseph Eagar added 1 commit 2023-07-11 17:14:15 +02:00
Joseph Eagar added 1 commit 2023-07-11 17:15:07 +02:00
This pull request has changes conflicting with the target branch.
  • source/blender/blenkernel/CMakeLists.txt
  • source/blender/blenkernel/intern/pbvh.cc
  • source/blender/editors/sculpt_paint/sculpt_intern.hh
  • source/blender/editors/sculpt_paint/sculpt_smooth.cc
  • source/blender/editors/space_file/fsmenu.c

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u temp-sculpt-brush-iter:JosephEagar-temp-sculpt-brush-iter
git checkout JosephEagar-temp-sculpt-brush-iter
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#109549
No description provided.