Sculpt Brush Refactor #118145

Open
opened 2024-02-12 17:27:23 +01:00 by Hans Goudey · 2 comments
Member

Background

Currently there are several design issues with sculpt code that make feature development and performance improvements too hard. Much of the code is written in a way that makes applying proven "modern" optimization concepts like data-oriented design too complicated or difficult.

On top of that, sculpt code often works, while things like semantic consistency and larger architectural challenges are still big issues.

The largest difficulty comes from the method of sharing code between sculpting implementations for the three different supported data structures: Mesh, BMesh, and multires grids. The current approach is to pass everything through the same code path using a large macro. At runtime, per vertex, the code switches based on the active data structure. Besides the obvious downsides of those constant checks embedded in hot loops, it poses a larger challenge by forcing code to be written for a single element (vertex) at a time. One of the simplest ways to improve performance is to process many elements together.

Design Changes

Here are the high level design goals of the brush refactor.

  • Use separate implementations of each brush per PBVH type. Code reuse should instead come from the use of common utilties, rather than systems that force different data through the same code path.
  • Remove most data stored/referenced in PBVH, so it is only used as an acceleration structure for raycasting that splits up geometry into multiple pieces.
  • Also remove much of the references in SculptSession. Instead access data as necessary during modal operations, and avoid redundant references in long term structs.
  • Remove most data from PBVHNode, separate node storage per PBVH type. Reduce per-node overhead to a minumum.
  • Reduce the size of PBVH nodes.
  • Use separate storage for leaf nodes and non-leaf nodes. Refer to leaf nodes by index.
  • Write brushes as a sequence of actions on arrays of data. Pull constant checks out of hot loops, avoiding unnecessary work without the runtime cost per-element.
  • Remove all permanent references to mesh data, including positions, color attributes, topology information, etc. Retrieve data as necessary before the start of each operation.
  • Move every brush action definition to its own file
  • Use a single GPU vertex buffer for all PBVH nodes to improve drawing performance, update ranges instead of entire buffers (see #97665 for other ideas)

Specific To Do

An incomplete list of to do items. Should be extended as necessary.

  • Automated performance testing
  • Brush types
    • Draw
    • Draw Sharp
    • Smooth
    • Scrape
    • Clay
    • Clay Strips
    • Clay Thumb
    • Snake Hook
    • Thumb
    • Rotate
    • Layer
    • Inflate
    • Nudge
    • Crease
    • Pinch
    • Grab
    • Elastic Deform
    • Topology Slide
    • Topology Relax
    • Displacement Eraser
    • Displacement Smear
    • Topology Rake
    • Mask
    • Fill
    • Flatten
    • Draw Face Set
    • Cloth
    • Pose
    • Multi-plane Scrape
    • Color painting
    • Boundary brush
  • Change automasking code to work on arrays
  • Change SculptBrushTestFn to works on arrays
  • Change brush curve strength factor calculation to work on arrays
    • Remove function pointer and type
  • Use IndexMask to address PBVH nodes by index
  • Move undo node push to sculpt brush implementations
  • Only access original and deformed positions in brushes (see node in code)
  • Remove eager inversion of deform_imats. Invert on demand instead
  • Completely remove BKE_pbvh_vertex_iter_begin
    • Remove macro for multires brush functions
    • Remove macro for BMesh brush functions'
    • Expand
    • Weight paint mode
    • Vertex paint mode
    • Gravity
    • Transform
  • Completely remove PBVHVertRef
  • Remove data from SculptSession, store in temporary context-like struct built on each call
    • Topology maps
    • Color attribute data
    • Mesh topology arrays
    • Depsgraph
    • Region, scene, 3D view
  • Remove more data stored in PBVH
    • Vertex and face normals
    • Vertex positions
    • Mesh topology arrays
    • Mesh pointer
    • Color attribute information
  • Data oriented storage of values in PBVH nodes
    • Use separate PBVH node types for mesh, multires, and BMesh
    • Separate array of non-leaf nodes with just bounds and child indices
    • Separate update tags to separate boolean arrays and/or index masks
    • Remove "color buffer"
    • Figure out what orig_vb is and potentially do something with it
  • Replace "fake neighbors" (SculptFakeNeighbors) API with code specific to the pose brush
  • Remove "proxies" system
## Background Currently there are several design issues with sculpt code that make feature development and performance improvements too hard. Much of the code is written in a way that makes applying proven "modern" optimization concepts like data-oriented design too complicated or difficult. On top of that, sculpt code often *works*, while things like semantic consistency and larger architectural challenges are still big issues. The largest difficulty comes from the method of sharing code between sculpting implementations for the three different supported data structures: Mesh, BMesh, and multires grids. The current approach is to pass everything through the same code path using a large macro. At runtime, per vertex, the code switches based on the active data structure. Besides the obvious downsides of those constant checks embedded in hot loops, it poses a larger challenge by forcing code to be written for a single element (vertex) at a time. One of the simplest ways to improve performance is to process many elements together. ## Design Changes Here are the high level design goals of the brush refactor. - Use separate implementations of each brush per PBVH type. Code reuse should instead come from the use of common utilties, rather than systems that force different data through the same code path. - Remove most data stored/referenced in `PBVH`, so it is _only_ used as an acceleration structure for raycasting that splits up geometry into multiple pieces. - Also remove much of the references in `SculptSession`. Instead access data as necessary during modal operations, and avoid redundant references in long term structs. - Remove most data from `PBVHNode`, separate node storage per PBVH type. Reduce per-node overhead to a minumum. - Reduce the size of PBVH nodes. - Use separate storage for leaf nodes and non-leaf nodes. Refer to leaf nodes by index. - Write brushes as a sequence of actions on _arrays_ of data. Pull constant checks out of hot loops, avoiding unnecessary work without the runtime cost per-element. - Remove all permanent references to mesh data, including positions, color attributes, topology information, etc. Retrieve data as necessary before the start of each operation. - Move every brush action definition to its own file - Use a single GPU vertex buffer for all PBVH nodes to improve drawing performance, update ranges instead of entire buffers (see #97665 for other ideas) ## Specific To Do An incomplete list of to do items. Should be extended as necessary. - [ ] Automated performance testing - [ ] Brush types - [ ] Draw - [ ] Draw Sharp - [ ] Smooth - [ ] Scrape - [ ] Clay - [ ] Clay Strips - [ ] Clay Thumb - [ ] Snake Hook - [ ] Thumb - [ ] Rotate - [ ] Layer - [ ] Inflate - [ ] Nudge - [ ] Crease - [ ] Pinch - [ ] Grab - [ ] Elastic Deform - [ ] Topology Slide - [ ] Topology Relax - [ ] Displacement Eraser - [ ] Displacement Smear - [ ] Topology Rake - [ ] Mask - [ ] Fill - [ ] Flatten - [ ] Draw Face Set - [ ] Cloth - [ ] Pose - [ ] Multi-plane Scrape - [ ] Color painting - [ ] Boundary brush - [ ] Change automasking code to work on arrays - [ ] Change `SculptBrushTestFn` to works on arrays - [ ] Change brush curve strength factor calculation to work on arrays - [ ] Remove function pointer and type - [ ] Use `IndexMask` to address PBVH nodes by index - [ ] Move undo node push to sculpt brush implementations - [ ] Only access original and deformed positions in brushes (see node in code) - [ ] Remove eager inversion of `deform_imats`. Invert on demand instead - [ ] Completely remove `BKE_pbvh_vertex_iter_begin` - [ ] Remove macro for multires brush functions - [ ] Remove macro for BMesh brush functions' - [ ] Expand - [ ] Weight paint mode - [ ] Vertex paint mode - [ ] Gravity - [ ] Transform - [ ] Completely remove `PBVHVertRef` - [ ] Remove data from `SculptSession`, store in temporary context-like struct built on each call - [ ] Topology maps - [ ] Color attribute data - [ ] Mesh topology arrays - [ ] Depsgraph - [ ] Region, scene, 3D view - [ ] Remove more data stored in `PBVH` - [ ] Vertex and face normals - [ ] Vertex positions - [ ] Mesh topology arrays - [ ] Mesh pointer - [ ] Color attribute information - [ ] Data oriented storage of values in PBVH nodes - [ ] Use separate PBVH node types for mesh, multires, and BMesh - [ ] Separate array of non-leaf nodes with just bounds and child indices - [ ] Separate update tags to separate boolean arrays and/or index masks - [ ] Remove "color buffer" - [ ] Figure out what `orig_vb` is and potentially do something with it - [ ] Replace "fake neighbors" (`SculptFakeNeighbors`) API with code specific to the pose brush - [ ] Remove "proxies" system
Hans Goudey added the
Type
Design
label 2024-02-12 17:27:23 +01:00
Hans Goudey added this to the Sculpt, Paint & Texture project 2024-02-12 17:27:31 +01:00
Julien Kaspar added the
Module
Sculpt, Paint & Texture
label 2024-02-13 11:55:49 +01:00

This redesign will bring a huge performance improvement by taking a more cache friendly data oriented design

This redesign will bring a huge performance improvement by taking a more cache friendly data oriented design
Blender Bot added the
Status
Archived
label 2024-02-13 15:32:53 +01:00
Blender Bot added
Status
Confirmed
and removed
Status
Archived
labels 2024-02-13 15:33:27 +01:00
Iliya Katushenock removed the
Status
Confirmed
label 2024-02-13 17:18:19 +01:00
Member

"Use separate implementations of each brush per PBVH type. Code reuse should instead come from the use of common utilties, rather than systems that force different data through the same code path."

There's no need to make individual brush implementations per PBVH type. The vast majority of brushes should use the common utilities you mentioned. The draw brush is the easiest case, but you should be able to build a few utilities for clay brushes (which are all based on projecting vertices to a plane).

"Use separate implementations of each brush per PBVH type. Code reuse should instead come from the use of common utilties, rather than systems that force different data through the same code path." There's no need to make _individual_ brush implementations per PBVH type. The vast majority of brushes should use the common utilities you mentioned. The draw brush is the easiest case, but you should be able to build a few utilities for clay brushes (which are all based on projecting vertices to a plane).
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser Project (Legacy)
Interest
Asset System
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 Assignees
3 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#118145
No description provided.