rtcBuildBVH(3)
π rtcBuildBVH(3) π
okay bestie, welcome to Embree Ray Tracing Kernels 3, and today's main character is rtcBuildBVH β the girl who builds you a whole BVH. no cap. let's get into it π¦
β¨ NAME β¨
rtcBuildBVH β builds a BVH. that's it, that's the job, and she ate. π
πΈ SYNOPSIS πΈ
first of all, the lore:
#include <embree3/rtcore.h>
then the primitive struct, which is aligned, as she should be:
struct RTC_ALIGN(32) RTCBuildPrimitive
{
float lower_x, lower_y, lower_z;
unsigned int geomID;
float upper_x, upper_y, upper_z;
unsigned int primID;
};
now the callback girlies π :
typedef void* (*RTCCreateNodeFunction) (
RTCThreadLocalAllocator allocator,
unsigned int childCount,
void* userPtr
);
typedef void (*RTCSetNodeChildrenFunction) (
void* nodePtr,
void** children,
unsigned int childCount,
void* userPtr
);
typedef void (*RTCSetNodeBoundsFunction) (
void* nodePtr,
const struct RTCBounds** bounds,
unsigned int childCount,
void* userPtr
);
typedef void* (*RTCCreateLeafFunction) (
RTCThreadLocalAllocator allocator,
const struct RTCBuildPrimitive* primitives,
size_t primitiveCount,
void* userPtr
);
typedef void (*RTCSplitPrimitiveFunction) (
const struct RTCBuildPrimitive* primitive,
unsigned int dimension,
float position,
struct RTCBounds* leftBounds,
struct RTCBounds* rightBounds,
void* userPtr
);
typedef bool (*RTCProgressMonitorFunction)(
void* userPtr, double n
);
the flags, the vibes:
enum RTCBuildFlags
{
RTC_BUILD_FLAG_NONE,
RTC_BUILD_FLAG_DYNAMIC
};
and the big one, the build args struct β she's long, and every single member matters:
struct RTCBuildArguments
{
size_t byteSize;
enum RTCBuildQuality buildQuality;
enum RTCBuildFlags buildFlags;
unsigned int maxBranchingFactor;
unsigned int maxDepth;
unsigned int sahBlockSize;
unsigned int minLeafSize;
unsigned int maxLeafSize;
float traversalCost;
float intersectionCost;
RTCBVH bvh;
struct RTCBuildPrimitive* primitives;
size_t primitiveCount;
size_t primitiveArrayCapacity;
RTCCreateNodeFunction createNode;
RTCSetNodeChildrenFunction setNodeChildren;
RTCSetNodeBoundsFunction setNodeBounds;
RTCCreateLeafFunction createLeaf;
RTCSplitPrimitiveFunction splitPrimitive;
RTCProgressMonitorFunction buildProgress;
void* userPtr;
};
and the two functions you actually call:
struct RTCBuildArguments rtcDefaultBuildArguments();
void* rtcBuildBVH(
const struct RTCBuildArguments* args
);
π DESCRIPTION π
so. the rtcBuildBVH function can be used to build a BVH in a user-defined format over arbitrary primitives. like, whatever primitives you want. iconic. π«Ά
every single argument goes in through the RTCBuildArguments structure. and listen β the first member of that struct must be set to the size of the structure in bytes (that's the byteSize member). that's literally the whole reason it's there: it lets the structure get extended in the future without breaking everything. the recommended move is to initialize your build arguments with the rtcDefaultBuildArguments function. do it, bestie. don't be a hero. π
rtcBuildBVH gets handed:
- the BVH to build β
bvhmember - the array of primitives β
primitivesmember - the capacity of that array β
primitiveArrayCapacitymember - the number of primitives stored inside the array β
primitiveCountmember - callback function pointers, plus a user-defined pointer (
userPtrmember) that gets passed to all callback functions when they're invoked
the primitives array can be freed by the application after the BVH is built. okay? after. π
β οΈ all callback functions are typically called from multiple threads, so their implementation must be thread-safe. no exceptions, no "but it worked on my machine".
four callback functions must be registered, and they get invoked during the build:
createNodemember β creates BVH nodessetNodeChildrenmember β sets the pointers to all childrensetNodeBoundsmember β sets the bounding boxes of all childrencreateLeafmember β creates a leaf node
the function pointer to the primitive split function (splitPrimitive member) may be NULL β but then no spatial splitting in high quality mode is possible. so like, your call, but that's the tradeoff. the function pointer used to report the build progress (buildProgress member) is optional and may also be NULL.
π§ build settings, aka the tuning section π§
some build settings get passed in to configure the BVH build:
- the build quality settings (
buildQualitymember) let you pick between a faster, low quality build which is good for dynamic scenes, and a standard quality build for static scenes. maxBranchingFactormember β the desired maximum branching factor of the BVHmaxDepthmember β the maximum depth the BVH should havesahBlockSizemember β the block size for the SAH heuristicminLeafSizeandmaxLeafSizemember β the minimum and maximum leaf sizetraversalCostandintersectionCostmembers β the estimated costs of one traversal step and one primitive intersection
and when you enable the RTC_BUILD_FLAG_DYNAMIC build flags (buildFlags member), re-build performance for dynamic scenes is improved β at the cost of higher memory requirements. nothing's free, babe. π
π spatial splits & capacity π
to spatially split primitives in high quality mode, the builder needs extra space at the end of the build primitive array to store split primitives. the total capacity of the build primitive array is passed using the primitiveArrayCapacity member, and should be about twice the number of primitives when using spatial splits. β¨
π the allocator (do not skip this) π
the RTCCreateNodeFunc and RTCCreateLeafFunc callbacks are passed a thread local allocator object that should be used for fast allocation of nodes using the rtcThreadLocalAlloc function.
π₯ we strongly recommend using this allocation mechanism, because alternative approaches like standard malloc can be over 10Γ slower. over ten times!! bestie!!
the allocator object passed to the create callbacks may be used only inside the current thread.
memory allocated using rtcThreadLocalAlloc is automatically freed when the RTCBVH object is deleted. but if you use your own memory allocation scheme, you have to free the memory yourself when the RTCBVH object is no longer used. π
π what each callback actually gets π
- the
RTCCreateNodeFunccallback additionally gets the number of children for this node in the range from2tomaxBranchingFactor(that's thechildCountargument). - the
RTCSetNodeChildFunccallback function gets a pointer to the node as input (nodePtrargument), an array of pointers to the children (childPtrsargument), and the size of this array (childCountargument). - the
RTCSetNodeBoundsFunccallback function gets a pointer to the node as input (nodePtrargument), an array of pointers to the bounding boxes of the children (boundsargument), and the size of this array (childCountargument). - the
RTCCreateLeafFunccallback additionally gets an array of primitives as input (primitivesargument), and the size of this array (primitiveCountargument). the callback should read thegeomIDandprimIDmembers from the passed primitives to construct the leaf. π - the
RTCSplitPrimitiveFunccallback is invoked in high quality mode to split a primitive (primitiveargument) at the specified position (positionargument) and dimension (dimensionargument). the callback should return bounds of the clipped left and right parts of the primitive (leftBoundsandrightBoundsarguments). - the
RTCProgressMonitorFunctioncallback function is called with the estimated completion ratenin the range[0, 1]. returningtruefrom the callback lets the build continue; returningfalsecancels the build. so dramatic, love that for her. π
π EXIT STATUS π
on failure an error code is set that can be queried using rtcGetDeviceError. so like, check it. please. π₯Ί
π¦ SEE ALSO π¦
[rtcNewBVH]
and that's the whole page, bestie β no notes, she's perfect. this was rtcBuildBVH(3) from Embree Ray Tracing Kernels 3. go build something gorgeous with it. periodt. πβ¨πππ«Ά
π¦ where this page comes from
- epel 9: embree 3.13.5-4.el9 Β· Apache-2.0
- fedora 43: embree3 3.13.5-9.fc41 Β· Apache-2.0
- fedora 44: embree3 3.13.5-16.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms