WIP: Test Embree alternative to Blender BVH trees #108148
No reviewers
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
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#108148
Loading…
Reference in New Issue
No description provided.
Delete Branch "LukasTonne/blender:bvh-embree"
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?
Test branch to compare performance of Embree BVH trees to Blender's own implementation and determine feasibility for replacement.
We want to compare the Embree library to Blender's own BVH tree implementation. The goal of this task is to find where Blender's BVH features don't have clear Embree equivalents, or where Embree might provide additional features that Blender can take advantage of.
k-DOP nodes vs. plain bounding boxes
Blender BVH has ancient feature: k-DOP bounding volumes. This is barely documented.
TL;DR: It's probably outdated and virtually useless, would suggest abandoning it if we move to Embree. If you want to know how it works, i attempt an explanation below.
My understanding is that a discrete oriented polytope (DOP) is a bounding volume that can include more bounding planes than a standard axis-aligned bounding box (AABB). These planes are constructed by combinations of the cardinal axes X, Y, Z. A bounding box only has bounding planes in the X, Y, Z, -X, -Y, and -Z directions. Combining two axes creates a diagonal plane along the edges, while combining 3 axes adds diagonal corner planes.
Blender encodes the directions used to enclose BVH nodes in the
axis
variable, which is the k in "k-DOP" (not to be confused with thetree_type
which is the number of children per node).In total this yields 26 possible directions and their combinations. However, Blender BVH only supports certain combinations:
The advantage of DOP volumes over plain bounding boxes is that they can enclose geometry more tightly, which can lead to fewer false-positive broadphase checks. The overlap check of such polytopes is still cheap due to using only basic combinations of cardinal axes.
However, this feature seems to be very rarely used. The gain in performance seems marginal and the implementation becomes much more complex. The almost total lack of documentation means that hardly anyone uses
axis
andtree_type
values deliberately and most just copy from existing places.IMO this feature can be dropped from Blender to simplify BVH usage and be compatible with Embree.
High-level BVH build methods
bvhutils
provides wrappers in BLI around the simplerkdopbvh
API. If thekdopbvh
API can be made to use Embree BVH then not many changes should be needed inbvhutils
.Build from vertices (points):
bvhtree_from_editmesh_verts
bvhtree_from_editmesh_verts_ex
bvhtree_from_mesh_verts_ex
Build from edges:
bvhtree_from_editmesh_edges
bvhtree_from_editmesh_edges_ex
bvhtree_from_mesh_edges_ex
Build from triangles:
bvhtree_from_editmesh_looptri
bvhtree_from_editmesh_looptri_ex
bvhtree_from_mesh_looptri_ex
Higher level wrappers (may support different primitive types):
BKE_bvhtree_from_mesh_get
BKE_bvhtree_from_editmesh_get
BKE_bvhtree_from_pointcloud_get
Trees are freed again with
free_bvhtree_from_editmesh
free_bvhtree_from_mesh
free_bvhtree_from_pointcloud
Embree equivalents for Blender BVH API
Embree BVH trees are more structured than Blender BVH: Each tree can contain multiple scenes, which in turn can contain multiple geometries. The Blender BVH tree can be represented as a single geometry in a single scene. Embree scenes support features like compression flags and build quality settings for which Blender BVH does not have equivalents.
Geometries can be shared between multiple scenes. This kind of data sharing is implemented in Blender through the BVH cache and the general scene/object hierarchy, but there is no overall scene BVH. This means that in order to query the entire scene Blender has to search through object BVH trees linearly instead of taking advantage of bounding boxes. For the time being we'll keep it that way, supporting scene-wide BVH construction with Embree should be a future task.
Embree also requires a device object. This should be persistent during the lifetime of a Blender instance. We will only be using a standard CPU device, Blender BVH trees are not currently used with GPU code.
Build functions
BLI_bvhtree_new
rtcNewScene
+rtcNewGeometry
+rtcAttachGeometry
BLI_bvhtree_free
rtcReleaseGeometry
+rtcReleaseScene
BLI_bvhtree_insert
rtcSetNewGeometryBuffer
BLI_bvhtree_balance
rtcCommitGeometry
BLI_bvhtree_update_node
BLI_bvhtree_update_tree
rtcUpdateGeometryBuffer
Tree info
Embree does not seem to provide much info about a BVH tree after construction. For specifying details the
rtcBuildBVH
function can be used. This includes the branching factor (tree_type
in current BVH).Epsilon is a fudge factor to resolve tangent ray intersections. Embree does not have (or need?) an API for that, it may just be better at handling float precision corner cases.
BLI_bvhtree_get_len
BLI_bvhtree_get_tree_type
BLI_bvhtree_get_epsilon
BLI_bvhtree_get_bounding_box
rtcGetSceneBounds
,rtcGetSceneLinearBounds
rtcGetSceneLinearBounds
provides two bounds for start and end time which can be linearly interpolated.Query methods
Query methods with a
1
suffix usually have variants for handling 4, 8, or 16 elements in parallel (SIMD) which Blender's BVH does not support.BLI_bvhtree_overlap
rtcCollide
BLI_bvhtree_overlap_ex
rtcCollide
BLI_bvhtree_overlap_self
rtcCollide
BLI_bvhtree_overlap_ex
BLI_bvhtree_overlap_thread_num
rtcCollide
does not appear to be multi-threaded itself, but near-phase checks could be parallelized.BLI_bvhtree_intersect_plane
BLI_bvhtree_find_nearest
rtcPointQuery
rtcPointQuery
runs callback for alll intersections, just need to return the minBLI_bvhtree_find_nearest_ex
rtcPointQuery
BLI_bvhtree_find_nearest_first
rtcPointQuery
BLI_bvhtree_find_nearest_projected
rtcPointQuery
with additional filteringBLI_bvhtree_range_query
rtcPointQuery
rtcPointQuery
already uses a callback for all intersectionsBLI_bvhtree_ray_cast
rtcIntersect1
BLI_bvhtree_ray_cast_ex
rtcIntersect1
BLI_bvhtree_ray_cast_all
rtcOccluded1
BLI_bvhtree_ray_cast_all_ex
rtcOccluded1
BLI_bvhtree_bb_raycast
BLI_bvhtree_walk_dfs
Checkout
From your project repository, check out a new branch and test the changes.