BMesh Refactor #91329
Labels
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
18 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#91329
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
BMesh has served Blender well for many years, but it as a few problems: the memory overhead is high, and the performance isn't always great compared to pure Mesh arrays.
The Problem
BMesh is a BREP (a variant of the classic half-edge data structure). The problem with BREPs is they do not always work well with CPU caches, especially on 64-bit platforms where all of the helper pointers can overwhelm the cache. When Geoffry Bantle designed BMesh he tried to improve cache coherency by writing the mempool library, but this did nothing to solve the problem of huge numbers of 8-byte helper pointers overwhelming the cache altogether (at the time Blender was still 32-bits).
I recently had a bunch of users on Blenderartists run a series of benchmarks for a simple vertex smooth operation. To summarize the results (which will be discussed in more depth later) we can get a pretty decent performance boost (~42%) if we replace all helper pointers in BMesh with 4-byte integer indices.
This makes sense. BMesh is memory bound (profiling consistently shows this), so replacing 8-byte pointers with 4-byte ints will reduce the load on the CPU cache and DRAM by quite a bit.
Short Intro To BMesh
BMesh has the following structure:
The Benchmark
To investigate where we should take BMesh in the future I wrote a series of benchmarks and
had users on BlenderArtists run them . There are four benchmarks in total, each of which run a simple vertex smoothing operation:
The data-oriented structure is inspired by recent developments in the games industry, and looks like this:
Results
As expected, the biggest performance gains came from the pure data-oriented approach. However a pretty hefty performance gain also came from simply replacing pointers with integer indices.
Here are the raw results:
Long term plans
Given these results, I think we should move BMesh to use integer offsets instead of pointers. This would be a far less disruptive change then the data-oriented approach. Note that I don't think we should rebuild the functionality of BMesh on top of Mesh as custom attribute layers, as that would be too disruptive to the existing codebase.
Needed Modifications
The structural modifications would be fairly simple (the actual code work might be another matter); basically, instead of using mempools for storing elements we would use simple arrays with freed element lists.
A hypothetical element array structure might look like this:
Of course this would make accessing the structure more complicated:
. . .but I suppose it's not too bad.
C++
It might be a good idea to use C++ for this. We could write ElementList and the topological
iterators in C++ and wrap them in C APIs. The core element structures will have to remain
in C though.
Added subscribers: @JosephEagar, @PabloDobarro
BREP Designto BMesh RefactorAdded subscribers: @ideasman42, @howardt, @mont29, @Sergey
Added subscriber: @kursadk
Added subscriber: @scurest
Added subscriber: @TheRedWaxPolice
Added subscriber: @GeorgiaPacific
Added subscriber: @bent
Added subscriber: @cdog
This is surely an interesting topic, but I'm missing some aspects of a design: what is the exact data structure you propose to use and how you see the current codebase to converge to it?
What are the downsides? Will operations like bevel/subdivide suffer from worse performance (due to i.e. need to maintain ordering in arrays)? Is there a memory usage footprint penalty?
Is the data-oriented type is another name for structure-of-arrays type of layout?
I'm also not sure how to read the table. Maybe you can explain better what columns and rows mean, and how did you do the performance test.
Added subscriber: @Roggii-4
Added subscriber: @SteffenD
[replied to the wrong post.]
There's no need to maintain orderings. The idea is to have an array where you allocate and free elements;
basically a flat version of mempool. We could in fact use mempool with a lookup table for the chunks,
though that would add a layer of indirection.
I'm not proposing we do this anytime soon. For the actual refactoring I'm thinking of writing a clang-tidy module to automate as much of it as possible.
Added subscriber: @Harley
The part that confuses me is that you summarize the tests by saying that "As expected, the biggest performance gains came from the pure data-oriented approach. However a pretty hefty performance gain also came from simply replacing pointers with integer indices."
But your raw data seems to indicate that "struct of arrays" gave you an average increase of 65.5% where "bm with indices" gives you 74.62%. Unless I am misunderstanding something or transposed your numbers wrong. That table could really use a summary at the bottom
Added subscriber: @mattli911
What is the proposal then? What's the purpose of this design task?
Added subscriber: @JulienKaspar
I've updated the table and summarized it, here's the python script I originally used to wrangle the data.
stats.py
To make long-term plans and goals?
Added subscriber: @HooglyBoogly
It seems to me that maintaining topology maps on top of the existing
Mesh
structure is the best way to achieve the data-oriented approach.Mesh
MVert
can be relatively simply split up to store normals separately, I started on that here: #91186So while the index based refactor of BMesh also seems to make sense, something else to pursue might be using
Mesh
in more places, like many modifiers already do.The basic intuition you are expressing has been obvious since the very beginning. Geoffry Bantle actually tried to make BMesh as Mesh-like as he possibly could, and I suspect he would have used integer indices if 64-bit processors had been more widespread back then. To be honest if Mesh with topology maps was good enough then BMesh would not exist.
Added subscriber: @fanny
Added subscriber: @SamGreen
Added subscriber: @AlexeyAdamitsky
Added subscriber: @hzuika