Skip to content

Overlay through a BVH: region queries over many regions (assemblies) #337

Open

Description

@rmrsk

An Overlay of many regions has no Accelerate::BVH: a tape evaluates it as a plain union, and every region query loops over all regions. An assembly read as one region per part (the language's meshes() with a list in a region line, #259) is the case that needs it: the studio renders a RegionView of it.

Measured (2026-11-15)

NASA's gantry, 730 parts (rmrsk/meshes, Clean/STL/Binary/Gantry), through ebgstudio render at 800×500 on the host:

Model Frame
bvh_union of the 730 parts 0.6 s
overlay with a region per part 2.4 s

F18 (67 parts) is still fine as a plain overlay: 1.1 s on the device and 3.0 s on the host at 1200×750, against 0.8 s and 0.7 s for its bvh_union.

What loops over every region

All in EBGeometry_TapeImplem.hpp:

  • region(p): the last region whose solid holds the point, by evaluating every region from the last down.
  • signedDistance(p, region), the region's own function max(S_k, -min_{j>k} S_j): every later region.
  • closestSurface(p, region): the same loop, tagged; for the background, every region.
  • regionDistances(p, visit): every region's function. It is used by Domain::signedDistances() (a subdomain's function, what a solver reads) and by RegionView (its signedDistance(), section fills, contours and domain samples).
  • firstRegionChange() and firstSurface(): at each crossing, every region from the current one on is traced.

The whole object's value, signedDistance(p) and the rays of the whole object, is a plain union of the operands too.

API

Approved at the checkpoint on 2026-11-15:

/**
 * @brief Regions by priority, of any number of operands (see Overlay()), optionally through a BVH.
 * @details The regions, their order and every value are those of Accelerate::None. With Accelerate::BVH, a Tape
 * evaluates the whole object as a BVH union of the operands, and answers the region queries (region(),
 * signedDistance(p, region), closestSurface(), firstRegionChange(), firstSurface(), ...) through a BVH over the
 * operands' boxes, visiting only the regions near the point or the ray instead of every region.
 * @param[in] a_operands   The regions, in priority order (must not be empty).
 * @param[in] a_accelerate Whether the tape answers through a BVH.
 */
template <class A>
[[nodiscard]] EBGEOMETRY_HOST
inline Expr<ExprPrecision<A>>
Overlay(const std::vector<A>& a_operands, Accelerate a_accelerate = Accelerate::None);
  • Operands: a BVH overlay's operands are plain solids only (maps allowed). A nested overlay, or a region clipped by a mask, aborts with a message; either can follow later.
  • Language: object overlay(bvh = true) { ... }, explicit like bvh_union. A large plain overlay prints a warning suggesting it, as a large plain union does. This is a small commit on the language stack once this lands on main.

Design

  • The BVH: a PackedBVH of region numbers in the tape's pool, built when the tape is compiled, like a BVH union's (TapeDetail::UnionBVH). Each region's box is that of its operand under its maps. Regions with unbounded boxes go in a separate list that every query checks, as a BVH union does.
  • region(p): a walk over the boxes holding the point, keeping the highest region whose solid holds it.
  • signedDistance(p, k) and closestSurface(p, k): only later regions near enough to decide matter. That means regions whose box holds the point when the point is outside region k, and otherwise those within |S_k|, with the bound shrinking as later regions are found.
  • The background's closestSurface: a nearest-first search for the region whose solid comes nearest.
  • The rays: a walk over the region boxes along the ray, nearest first, bounded by the first crossing found.
  • Values: exact wherever the regions' functions are distances. With operands that are only bounds, a value can be larger but keeps its sign and stays a bound, the same contract as Union(..., Accelerate::BVH).

Open decision: regionDistances(). It hands every region's function to a caller that takes minima over sets of regions (subdomains, visible regions). Visiting every region exactly keeps today's cost for those functions. The proposal:

  • visit the point's own region, and every region whose box lies within twice the distance to that region's own boundary;
  • return that radius, and a set with no visited region counts as at least that far.

That makes a subdomain's function exact near every interface (where an EB code cuts cells, and where sections and contours are drawn), right in sign everywhere, and a distance bound far from interfaces. The point and ray queries above are not affected by this choice.

Testing and performance

  • Unit tests: every query compared with the same overlay without the BVH, at random points and rays, for regions that overlap, touch and nest. Plus [gpu] cases.
  • Probes: hostdevice pairs for region(), a subdomain's function and a RegionView frame of a many-part overlay.
  • Device checks: the register and stack checks for the tape's ray and region kernels.

Status

Paused before any code, to work on the gantry and on seeing a union's parts first. A branch overlay-bvh off main (569fff8) is created, with no commits.

Related:


Generated by Claude Code

Activity

  1. added
    csg
    performance
    ray tracing
    rendering
    gpu
    on Nov 15, 2026
  2. rmrsk commented on Nov 29, 2026

    @rmrsk
    OwnerAuthor

    Status (2026-11-29): the language is frozen as ebg 1 (#384), with this issue's setting reserved: overlay(bvh = true|false) is accepted, validated (a Bool, set once) and held to #349's rules, then evaluated as a plain overlay until this lands:

    • the overlay can be the object or be moved (translate, rotate, scale, reflect), but not clipped, drilled, labeled or placed in another overlay;
    • an overlay inside it may not be clipped or drilled, nor hold one that is, at any depth;
    • it counts toward the sub-program call depth.

    So landing #349 adds no syntax and breaks no ebg 1 file. What remains is the evaluation (a Language::Model accessor and the RegionBVH path), still parked on the ptxas crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    csggpuperformanceray tracingrendering

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions