Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

tinybvh

> DevOps
Open source

Single-header dependency-free BVH construction and traversal library.

1.2K stars0 likes0 views
WebsiteGitHub

About

Single-header dependency-free BVH construction and traversal library.

TinyBVH

Single-header BVH construction and traversal library written in C++14 / "Sane C++" (or "C with classes"). C++17 is used for threading. This library has no dependencies.

TinyBVH is fast. Here is, in a nutshell, how it compares to Intel's Embree and Madmann91's BVH library.

TinyBVH currently builds a SAH BVH faster than Embree and Maddmann91. It also traces individual primary rays faster than those libraries. Embree outperforms TinyBVH for any hit rays. Note: These results are based on the new tiny_bvh_benchmark.cpp application and are cautiously presented as 'preliminary'. Note that single-ray traversal is only a small part of Embree. If you suspect an inperfection in the experiment setup, please let me know.

When tracing rays on the GPU, (multicore) CPU performance is dwarfed. On an RTX 5080 laptop GPU, TinyBVH traces up to 4 billion rays per second in Crytek's Sponza scene using the basic binary BVH format, regardless of graphics API and without using specialized ray tracing hardware. That is 4 rays per pixel at 4k@120Hz, enough for most purposes.

TinyOCL

TinyBVH GPU ray traversal examples are available for OpenCL and OpenGL with compute shaders. For OpenCL support, TinyOCL is provided: a single-header OpenCL library, which helps you select and initialize a compute device. It also loads, compiles and runs kernels, with several convenient features:

  • Include-file expansion for AMD devices
  • Multi-argument passing
  • Host/device buffer management
  • Vendor and architecture detection and propagation to #defines in OpenCL code
  • ..and many other things.

To use TinyOCL, just include tiny_ocl.h; this will automatically cause linking with OpenCL.lib in the 'external' folder, which in turn passes on work to vendor-specific driver code.

Note that the tiny_bvh.h library will work without tiny_ocl.h and remains dependency-free. The new tiny_ocl.h is only needed in projects that wish to trace rays on the GPU using BVHs created by tiny_bvh.h.

BVH?

A Bounding Volume Hierarchy is a data structure used to quickly find intersections in a virtual scene; most commonly between a ray and a group of triangles. You can read more about this in a series of articles on the subject: https://jacco.ompf2.com/2022/04/13/how-to-build-a-bvh-part-1-basics .

To build a BVH using TinyBVH, simply call BVH::Build on an instantiated BVH. See tiny_bvh_minimal.cpp for a (really) small example. Internally, the call is forwarded to a specialized builder. There are fast builders, builders for 'high quality' BVHs and experimental builders for research and development.

A selection:

  • BVH::Build : Efficient plain-C/C+ binned SAH BVH builder which should run on any platform.
  • BVH::BuildAVX : A highly optimized version of BVH::Build for Intel and AMD CPUs.
  • BVH::BuildHQ : A 'spatial splits' BVH builder, for optimal BVH quality.

A constructed BVH can be used to intersect a ray with geometry, using BVH::Intersect or BVH::IsOccluded, for shadow (aka any hit) rays.

Apart from the default BVH layout (simply named BVH), several other layouts are available, which all serve one or more specific purposes. You can create a BVH in the desired layout by instantiating the appropriate class. The available layouts are:

  • BVH : A compact format that stores the AABB for a node, along with child pointers and leaf information in a cross-platform-friendly way. The 32-byte size allows for cache-line alignment.
  • BVH_Double : Double-precision version of BVH.
  • MBVH : In this (templated) format, each node stores M child pointers, reducing the depth of the tree. This improves performance for divergent rays. Based on the 2008 paper by Ingo Wald et al.
  • BVH4_CPU : SSE-optimized wide BVH traversal ("WiVe"). The fastest option for CPUs that do not support AVX.
  • BVH8_CPU : AVX2-optimized wide BVH traversal ("WiVe"). This is the fastest option on CPU.
  • BVH_GPU : This format uses 64 bytes per node and stores the AABBs of the two child nodes. This is the format presented in the 2009 Aila & Laine paper. It can be traversed with a simple GPU kernel.
  • BVH4_GPU : A compact version of the BVH4 format, which may be faster for GPU ray tracing.
  • BVH8_CWBVH : An advanced 80-byte representation of the 8-wide BVH, for state-of-the-art GPU rendering, based on the 2017 paper by Ylitie et al. and code by AlanWBFT.

A BVH in any format can be rebuilt at any time by calling the Build method on the changed triangle data. A BVH may also be refitted, in case the triangles moved, using BVH::Refit. Refitting is substantially faster than rebuilding and works well if the animation is subtle. Refitting does not work if polygon counts change.

Most layouts may be serialized and de-serialized via ::Save and ::Load.

TinyBVH also supports construction of a Top-Level Acceleration Structure (TLAS). The TLAS is a BVH over Bottom-Level Acceleration Structures (BLASses), where each BLAS is a BVH, with a 4x4 matrix transform. Different layouts can be combined under a single TLAS. The TLAS can be used for cheap rigid animation (by changing the transforms) as well as instancing.

A more complete overview of TinyBVH functionality can be found in the Basic Use Manual and the Advanced Topics Manual.

How To Use

The library tiny_bvh.h is designed to be easy to use. Please have a look at tiny_bvh_minimal.cpp for an example. A Visual Studio 'solution' (.sln/.vcxproj) is included, as well as a CMake file. That being said: Most examples consists of only a single source file, which can be compiled with clang or g++, e.g.:

g++ examples/tiny_bvh_minimal.cpp

The cross-platform fenster-based single-source bitmap renderer can be compiled with

g++ -mwindows -O3 examples/tiny_bvh_fenster.cpp -o tiny_bvh_fenster (on Linux and Windows)

c++ --std=c++17 -framework Cocoa -O3 examples/tiny_bvh_fenster.cpp -o tiny_bvh_fenster (on macOS)

The multi-threaded path tracing demo can be compiled with

g++ -mwindows -O3 examples/tiny_bvh_pt.cpp -o tiny_bvh_pt (on Linux and Windows)

c++ --std=c++17 -framework Cocoa -O3 examples/tiny_bvh_pt.cpp -o tiny_bvh_pt (on macOS)

The performance measurement tool can be compiled with:

g++ -mavx2 -mfma -Ofast tiny_bvh_speedtest.cpp -o tiny_bvh_speedtest (on Linux and Windows)

c++ --std=c++17 -framework OpenCL -Ofast tiny_bvh_speedtest.cpp -o tiny_bvh_speedtest (on macOS)

Note: A new, more advanced benchmark tool is now available. See build.bat and build_debug.bat for an example of a commandline to compile it using gcc.

Many additional demos are provided, demonstrating features of the library in small source files.

Version 1.8.0

Basic use:

BVH bvh;
bvh.Build( (bvhvec4*)myTriData, triangleCount ); // or: BuildHQ( .. )
bvh.Intersect( ray );

To build a BVH for indexed vertices, use the new indexed interface:

BVH bvh;
bvh.Build( (bvhvec4*)vertices, (uint32_t*)indices, triangleCount );

If you wish to use a specific builder (such as the spatial splits builder) or if you need to do custom operations on the BVH, such as post-build optimizing, you can still do the conversions manually. Example:

BVH bvh;
bvh.Build( verts, indices, triCount );
BVH_Verbose tmp;
tmp.ConvertFrom( bvh );
tmp.Optimize( 100 );
bvh.ConvertFrom( tmp );
printf( "Optimized BVH SAH cost: %f\n", bvh.SAHCost() );

Note that in this case, data ownership and lifetime must be managed carefully. Specifically, layouts converted from other layouts use data from the original, so both must be kept alive.

New in 1.7.3: BVH build settings are now in BVHBase::settings:

…

`
This supersedes the calls to ::BuildHQ and ::BuildAVX, which are now invoked from ::Build based on the hints in BVHBuildSettings. The old interface also remains available for now.

This version of the library includes the following functionality:

  • Reference 'full-sweep' SAH BVH builder
  • Fast cross-platform binned SAH BVH builder
  • Fast binned SAH BVH builder using AVX intrinsics
  • Fast binned SAH BVH builder using NEON intrinsices, by wuyakuma
  • Spatial Splits (SBVH, Stich et al., 2009) builder, including "unsplitting"
  • Wide BVHs (any width) using collapsing
  • TLAS builder with instancing and fast TLAS/BLAS traversal, even for 'mixed trees'
  • TLAS masking (similar to OptiX), by Romain Augier.
  • Support for custom geometry and mixed scenes
  • Double-precision binned SAH BVH builder
  • Customizable SAH parameters for all builders
  • "End-Point Overlap" BVH cost metric ("On Quality Metrics of Bounding Volume Hierarchies", Aila et al., 2013)
  • GPU-friendly layouts, including 'Compressed Wide BVH' (CWBVH) for state-of-the-art GPU performance
  • Support for custom primitives via callbacks, also in double-precision BVHs
  • BVH (de)serialization for most layouts

Besides basic examples demonstrating usage of the library, these more advanced examples are provided:

  • Example OpenCL code for GPU TLAS/BLAS traversal (dragon invasion demo, tiny_bvh_gpu2.cpp)
  • Example OpenGL / compute shader code for GPU BLAS traversal (tiny_bvh_gl_compute)
  • Example TLAS/BLAS application using OpenGL interop (windows only)

Advanced / exotic features of the library include:

  • BVH optimizer: reduces SAH cost and improves ray tracing performance (Bittner et al., 2013)
  • BVH pre-splitting (implementing ideas from a paper by Karras and Aila and explanation by BoybayKiller)
  • Full-Sweep SAH BVH (with support from BoyBaykiller)
  • Opacity Micro Map support (as proposed by Gruen et al. in 2020)
  • Sphere/BVH collision detection via BVH::IntersectSphere(..)
  • Fast AVX2 ray tracing: Implements the 2017 paper by Fuetterling et al.
  • Fast SSE4.2 ray tracing: A modified version of the AVX2 implementation using just SSE4.2 achieves 80% of AVX2 performance.
  • Fast triangle intersection: Implements the 2016 paper by [Baldwin & We

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++bvhraytracingrendering

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理