mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-11 18:16:40 +00:00
ifcviewer: move scene mutators + releaseWgpuModelGpuData into ViewportCore (#84-f)
Move the eight scene-mutation methods that drive bonsai's load/unload
and georeference setters, plus the per-model GPU teardown helper.
All are mechanical transplants — no logic change — so behaviour stays
identical; only the owner has changed.
Methods moved (ViewportWindow public-API methods stay as forwarders
to keep the bonsai-side callers compiling):
removeModel / resetScene / hideModel / showModel
setFederatedFalseOrigin
setModelCoordinateOperation
setModelTransformation
recomposeAndUploadModel
State moved:
bool wgpu_initialized_ (storage → core_, alias kept in VW for
the initWgpu call site that still flips
it; goes when initWgpu moves)
Free function moved:
releaseWgpuModelGpuData(ModelGpuData&, BufferPool&) → ViewportCore.cpp
(must live in IfcViewerCore now that ViewportCore.cpp's
removeModel / resetScene call it; ViewportWindow.cpp's remaining
two call sites continue to resolve through ModelGpuData.h's
declaration — same linker view, different definition TU)
The `if (isExposed()) requestUpdate()` Qt pattern inside the moved
bodies became `host_->requestFrame()` since ViewportCore can't see
QWindow; the desktop ViewportHost override at the bottom of
ViewportWindow.cpp continues to translate that into requestUpdate().
Builds: desktop / bonsai / web all green. Tests 100/100.
This commit is contained in:
@@ -19,11 +19,154 @@
|
||||
|
||||
#include "ViewportCore.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
#include "InstanceCompose.h"
|
||||
|
||||
ViewportCore::ViewportCore(ViewportHost* host) : host_(host) {}
|
||||
ViewportCore::~ViewportCore() = default;
|
||||
|
||||
// Tear down a model's per-chunk GPU resources, free its pool slices,
|
||||
// and reset all the bookkeeping vectors so the slot can be reused.
|
||||
// Static because callers from outside this TU still live in
|
||||
// ViewportWindow.cpp; ModelGpuData.h's declaration keeps the
|
||||
// inter-TU contract.
|
||||
void releaseWgpuModelGpuData(ModelGpuData& m, BufferPool& pool) {
|
||||
for (auto& c : m.chunks) {
|
||||
if (c.bind_group) { wgpuBindGroupRelease(c.bind_group); c.bind_group = nullptr; }
|
||||
if (c.vertex_slice.valid()) {
|
||||
pool.free(c.vertex_slice);
|
||||
c.vertex_slice = {};
|
||||
}
|
||||
if (c.index_slice.valid()) {
|
||||
pool.free(c.index_slice);
|
||||
c.index_slice = {};
|
||||
}
|
||||
if (c.visible_draws_buffer) { wgpuBufferRelease(c.visible_draws_buffer); c.visible_draws_buffer = nullptr; }
|
||||
if (c.prefix_sums_buffer) { wgpuBufferRelease(c.prefix_sums_buffer); c.prefix_sums_buffer = nullptr; }
|
||||
if (c.per_chunk_uniform) { wgpuBufferRelease(c.per_chunk_uniform); c.per_chunk_uniform = nullptr; }
|
||||
}
|
||||
m.chunks.clear();
|
||||
m.mesh_chunk_idx.clear();
|
||||
m.mesh_chunk_local_base_vertex.clear();
|
||||
m.mesh_chunk_local_ebo_first_u32.clear();
|
||||
m.mesh_chunk_local_lod1_first_u32.clear();
|
||||
m.instance_chunk_idx.clear();
|
||||
m.instance_base_vertex.clear();
|
||||
m.instance_ebo_first_u32.clear();
|
||||
m.instance_lod1_first_u32.clear();
|
||||
if (m.mesh_storage) { wgpuBufferRelease(m.mesh_storage); m.mesh_storage = nullptr; }
|
||||
if (m.instance_storage) { wgpuBufferRelease(m.instance_storage); m.instance_storage = nullptr; }
|
||||
m.vertex_bytes = 0;
|
||||
m.index_count = 0;
|
||||
m.mesh_count = 0;
|
||||
m.instance_count = 0;
|
||||
m.meshes.clear();
|
||||
m.instances.clear();
|
||||
}
|
||||
|
||||
// ---- Scene mutators -------------------------------------------------------
|
||||
|
||||
void ViewportCore::removeModel(uint32_t model_id) {
|
||||
auto it = models_gpu_.find(model_id);
|
||||
if (it == models_gpu_.end()) return;
|
||||
releaseWgpuModelGpuData(it->second, pool_);
|
||||
models_gpu_.erase(it);
|
||||
host_->requestFrame();
|
||||
}
|
||||
|
||||
void ViewportCore::resetScene() {
|
||||
for (auto& [mid, m] : models_gpu_) releaseWgpuModelGpuData(m, pool_);
|
||||
models_gpu_.clear();
|
||||
host_->requestFrame();
|
||||
}
|
||||
|
||||
void ViewportCore::hideModel(uint32_t model_id) {
|
||||
auto it = models_gpu_.find(model_id);
|
||||
if (it == models_gpu_.end() || it->second.hidden) return;
|
||||
it->second.hidden = true;
|
||||
host_->requestFrame();
|
||||
}
|
||||
|
||||
void ViewportCore::showModel(uint32_t model_id) {
|
||||
auto it = models_gpu_.find(model_id);
|
||||
if (it == models_gpu_.end() || !it->second.hidden) return;
|
||||
it->second.hidden = false;
|
||||
host_->requestFrame();
|
||||
}
|
||||
|
||||
void ViewportCore::setFederatedFalseOrigin(const Eigen::Matrix4d& matrix_meters) {
|
||||
if (federated_false_origin_meters_ == matrix_meters) return;
|
||||
federated_false_origin_meters_ = matrix_meters;
|
||||
for (auto& kv : models_gpu_) recomposeAndUploadModel(kv.first);
|
||||
}
|
||||
|
||||
void ViewportCore::setModelCoordinateOperation(uint32_t model_id,
|
||||
const Eigen::Matrix4d& matrix_meters) {
|
||||
auto it = models_gpu_.find(model_id);
|
||||
if (it == models_gpu_.end()) return;
|
||||
if (it->second.coordinate_operation_meters == matrix_meters) return;
|
||||
it->second.coordinate_operation_meters = matrix_meters;
|
||||
recomposeAndUploadModel(model_id);
|
||||
}
|
||||
|
||||
void ViewportCore::setModelTransformation(uint32_t model_id,
|
||||
const Eigen::Matrix4d& matrix_meters) {
|
||||
auto it = models_gpu_.find(model_id);
|
||||
if (it == models_gpu_.end()) return;
|
||||
if (it->second.model_transformation_meters == matrix_meters) return;
|
||||
it->second.model_transformation_meters = matrix_meters;
|
||||
recomposeAndUploadModel(model_id);
|
||||
}
|
||||
|
||||
void ViewportCore::recomposeAndUploadModel(uint32_t model_id) {
|
||||
if (!wgpu_initialized_) return;
|
||||
auto it = models_gpu_.find(model_id);
|
||||
if (it == models_gpu_.end()) return;
|
||||
ModelGpuData& m = it->second;
|
||||
if (m.instances.empty() || m.instance_storage == nullptr) return;
|
||||
|
||||
std::vector<InstanceGpu> gpu(m.instances.size());
|
||||
for (size_t i = 0; i < m.instances.size(); ++i) {
|
||||
InstanceCpu& inst = m.instances[i];
|
||||
composeInstanceFromPlacement(inst, m);
|
||||
|
||||
InstanceGpu& dst = gpu[i];
|
||||
std::memcpy(dst.transform, inst.transform, sizeof(dst.transform));
|
||||
dst.object_id = inst.object_id;
|
||||
dst.color_override_rgba8 = inst.color_override_rgba8;
|
||||
dst.mesh_id = inst.mesh_id;
|
||||
dst._pad1 = 0;
|
||||
}
|
||||
wgpuQueueWriteBuffer(queue_, m.instance_storage, 0,
|
||||
gpu.data(), gpu.size() * sizeof(InstanceGpu));
|
||||
|
||||
// Per-chunk world AABBs are derived from instance world AABBs; they
|
||||
// drive chunk-level frustum cull and the streaming priority, so they
|
||||
// must follow the recompose. Reset to ±inf and re-fold every chunk's
|
||||
// instances. Streaming chunks that haven't yet been assigned
|
||||
// instance_ids (extremely rare path) just stay at ±inf and naturally
|
||||
// fall out of frustum tests until the next load completes.
|
||||
for (auto& c : m.chunks) {
|
||||
c.aabb_min[0] = c.aabb_min[1] = c.aabb_min[2] =
|
||||
std::numeric_limits<float>::infinity();
|
||||
c.aabb_max[0] = c.aabb_max[1] = c.aabb_max[2] =
|
||||
-std::numeric_limits<float>::infinity();
|
||||
for (uint32_t inst_idx : c.instance_ids) {
|
||||
if (inst_idx >= m.instances.size()) continue;
|
||||
const InstanceCpu& inst = m.instances[inst_idx];
|
||||
for (int a = 0; a < 3; ++a) {
|
||||
c.aabb_min[a] = std::min(c.aabb_min[a], inst.world_aabb_min[a]);
|
||||
c.aabb_max[a] = std::max(c.aabb_max[a], inst.world_aabb_max[a]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
host_->requestFrame();
|
||||
}
|
||||
|
||||
bool ViewportCore::findInstance(uint32_t object_id,
|
||||
InstanceCompose::InstanceLookup& out) const {
|
||||
return InstanceCompose::findInstanceInModels(object_id, models_gpu_, out);
|
||||
|
||||
Reference in New Issue
Block a user