Files
IfcOpenShell/src/ifcviewer/BvhAccel.h
T
Dion Moult 1f17d73f3e BVH frustum culling over instances
Re-wires the BVH acceleration structure on top of the new instanced
renderer.  Per model, build a BVH over per-instance world AABBs at
finalize (and on sidecar apply).  Each frame, traverse the BVH against
the camera frustum to produce a visible-instance index list, bucket by
mesh_id, and upload to a per-model SSBO at binding=1.  The main and
pick vertex shaders do a double-indirection
`instances[visible[u_offset + gl_InstanceID]]` so draws only touch
instances that passed the frustum test.

Models with fewer than BVH_MIN_OBJECTS instances skip the BVH build
and fall back to a linear per-instance frustum test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-23 21:32:24 +10:00

71 lines
3.2 KiB
C++

/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef BVHACCEL_H
#define BVHACCEL_H
#include <cstdint>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <memory>
// Generic BVH item — anything with a world AABB and a model_id.
// For the instanced renderer each item represents one InstanceCpu.
struct BvhItem {
float aabb_min[3];
float aabb_max[3];
uint32_t model_id;
};
static constexpr uint32_t BVH_MAX_LEAF_SIZE = 8;
static constexpr uint32_t BVH_MIN_OBJECTS = 32;
struct BvhNode {
float aabb_min[3];
float aabb_max[3];
uint32_t right_or_first; // interior: right child index (left is always this_index+1); leaf: first item index
uint16_t count; // 0 = interior; >0 = leaf with this many items
uint16_t axis; // split axis (0/1/2) for interior; unused for leaf
};
static_assert(sizeof(BvhNode) == 32, "BvhNode must be 32 bytes for cache alignment and sidecar format");
struct ModelBvh {
uint32_t model_id = 0;
std::vector<BvhNode> nodes;
std::vector<uint32_t> item_indices; // indices into the model's InstanceCpu array
};
struct BvhSet {
std::unordered_map<uint32_t, ModelBvh> models;
std::unordered_set<uint32_t> bvh_model_ids;
};
// Build BVH trees for all models in the given item snapshot.
// Items are expected to already be grouped/filtered by caller if needed.
// item_indices in the result reference positions within the full `items`
// vector — callers providing a single model's items will see 0..N-1.
std::shared_ptr<BvhSet> buildBvhSet(const std::vector<BvhItem>& items);
// Build a single-model BVH over `items`. model_id is stored on the result
// for identification; item_indices will be 0..items.size()-1.
ModelBvh buildModelBvhOne(const std::vector<BvhItem>& items, uint32_t model_id);
#endif // BVHACCEL_H