wgpu backend: selection visualisation + global object_id rebase

Closes the loop on stage 4 (pick): clicking an object now highlights it
on screen. Plus the prerequisite plumbing for selection to behave
correctly across multi-sidecar loads.

Pieces:

  1. WgpuSelectionState (new header)
     CPU-side multi-set + active-id, mirroring the GL Selection.h shape
     but pure stdlib (no Qt deps) so it can move into ifcviewer-core
     later without dragging Qt across. clear/replace/add/remove/toggle
     APIs + a fillFlagsArray helper that packs (selected, active) into
     a u32 bitmap indexed by object_id.

  2. selection_flags storage buffer + frame_bgl bump to 2 entries
     Indexed by object_id, bit 0 = selected, bit 1 = active. Lives in
     the frame bind group (group=0 binding=1) because object_ids are
     globally unique — making it model-scoped would be the wrong cut.
     ensureSelectionFlagsBuffer grows geometrically (64 → 128 → … u32)
     as new models push next_object_id_ up, rebuilds the frame bind
     group when it does.

  3. Global object_id rebase in applyCachedModel
     Each sidecar's local ids start from 1 and collide across files;
     pick was previously ambiguous on multi-model loads. We now add
     next_object_id_ as a base offset, rewrite InstanceCpu.object_id
     (CPU mirror stays consistent) + InstanceGpu.object_id (what pick
     reads back), and bump next_object_id_ by the model's max + 1.

  4. WGSL main fragment reads sel_flags
     Vertex shader passes inst.object_id through to fragment as
     @interpolate(flat). Fragment reads sel_flags[object_id], mixes
     (0.2, 0.6, 1.0) at 0.45 for in-selection and (0.4, 0.8, 1.0) at
     0.40 on top for active. Same constants as the GL main shader.

  5. Mouse → selection
     LMB-click-without-drag pick result feeds the selection:
       no modifier → replace
       Shift      → add
       Ctrl       → remove (active migrates to another id in the set)
       miss + no modifier → clear
     uploadSelectionFlagsIfDirty repacks + writes the GPU bitmap at
     the top of the next render(); no upload on still frames.

Pick pipeline is unchanged — it already outputs the per-instance
object_id, and that's what the selection storage indexes.

Visibility + clip planes are pending follow-ups in stage 5 (mostly
small, share the same buffer-lifecycle pattern). Edge silhouette
(stage 9 partial), --screenshot diff harness (stage 10 partial),
ifcviewer-core extract (stage 12), and web chunking (stage 13) all
still pending.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-05-27 20:25:05 +10:00
parent 1bbcd10bc0
commit 54fa7d8379
3 changed files with 257 additions and 20 deletions
+103
View File
@@ -0,0 +1,103 @@
/********************************************************************************
* *
* 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 WGPUSELECTIONSTATE_H
#define WGPUSELECTIONSTATE_H
#include <cstdint>
#include <unordered_set>
#include <vector>
// CPU-side selection tracking. Mirrors src/ifcviewer/Selection.h shape but
// without Qt deps (kept pure stdlib so it can move into ifcviewer-core
// later without dragging Qt along).
//
// Two flavours of "selected":
// - the multi-set (ids()): every object the user has Shift-added.
// - active (activeId()): the *last* single-clicked object. UIs typically
// use this to drive the properties panel; the renderer tints it
// slightly more strongly than the rest of the multi-set.
//
// The GPU consumes a flat u32 array indexed by object_id: bit 0 = selected,
// bit 1 = active. Sized to (max_object_id + 1) by the caller.
class WgpuSelectionState {
public:
void clear() {
if (ids_.empty() && active_ == 0) return;
ids_.clear();
active_ = 0;
dirty_ = true;
}
// Replace the selection with a single object. id == 0 clears.
void replace(uint32_t id) {
ids_.clear();
if (id != 0) ids_.insert(id);
active_ = id;
dirty_ = true;
}
void add(uint32_t id) {
if (id == 0) return;
ids_.insert(id);
active_ = id;
dirty_ = true;
}
void remove(uint32_t id) {
if (id == 0) return;
if (ids_.erase(id) == 0) return;
if (active_ == id) {
active_ = ids_.empty() ? 0 : *ids_.begin();
}
dirty_ = true;
}
void toggle(uint32_t id) {
if (id == 0) return;
if (ids_.count(id)) remove(id);
else add(id);
}
bool contains(uint32_t id) const { return ids_.count(id) > 0; }
uint32_t activeId() const { return active_; }
const std::unordered_set<uint32_t>& ids() const { return ids_; }
size_t count() const { return ids_.size(); }
bool dirty() const { return dirty_; }
void markClean() { dirty_ = false; }
// Fill `out` (sized to entries u32s) with bit-packed flags:
// bit 0 = selected (in ids_), bit 1 = active. out[0] is always 0
// because object_id 0 is the "miss" sentinel.
void fillFlagsArray(std::vector<uint32_t>& out, uint32_t entries) const {
out.assign(entries, 0);
for (uint32_t id : ids_) {
if (id < entries) out[id] |= 1u;
}
if (active_ != 0 && active_ < entries) out[active_] |= 2u;
}
private:
std::unordered_set<uint32_t> ids_;
uint32_t active_ = 0;
bool dirty_ = false;
};
#endif // WGPUSELECTIONSTATE_H