Files
IfcOpenShell/src/ifcviewer-wgpu-minimal/main.cpp
T
Dion Moult 9c067d1d0e wgpu: bonsai-ready API surface + direct-IFC ingestion + streaming-always
Make the wgpu viewport ready for bonsai's verb actions, federation
refresh, and tool routing — i.e. callable from an outside host, not
just from the minimal viewer's own hotkeys.

Surface additions on WgpuViewportWindow:
- Qt signals: objectPicked, frameStatsUpdated, surfacePickedInTool,
  toolModeChanged, toolBackspacePressed.
- FrameStats struct + rolling 60-sample frame-time window for the
  fps field; emit at end of render() so external listeners see fresh
  numbers in the same tick.
- InstanceLookup struct + findInstance(object_id, ...) const for the
  measurement tools' O(1) object → (model, mesh, placement) resolve.
- Federation hooks (setFederatedFalseOrigin / setModelCoordinateOperation
  / setModelTransformation) + per-model coordinate_operation_meters /
  model_transformation_meters fields on WgpuModelGpuData. Implement
  composeInstanceFromPlacement + recomposeAndUploadModel so each setter
  actually applies — model recompose runs in double, casts to float for
  the GPU upload, and refreshes per-chunk world AABBs. meshLocalToGlobal
  now composes coordinate_operation · placement properly.
- showModel / hideModel for per-model visibility, plus element-level
  verbs (hideSelectedElements / isolateSelectedElements / showAllElements
  / invertElementVisibility) and setSelectedObjectId / cameraState() /
  projectionOrtho() / toggle{Area,Length,Volume}Tool wrappers.
- Section-cutting methods (toggleSectionTool / clearSectionPlanes /
  sectionToolActive) moved to public so bonsai's Commands.cpp can call.
- QVector3D overload of computeObjectAabb to match the GL signature.
- ToolMode::None → ToolMode::NoTool (X11 macro collision avoidance).

Direct-IFC ingestion (A-path), mirrors the GL streaming push API:
- uploadMeshChunk / uploadInstanceChunk stage into pending_direct_loads_
  using the same vertex quantisation as SidecarBuilder so direct-load
  and sidecar-load produce byte-identical buffers.
- finalizeModel wraps the staged data in a file-less StreamingSidecar,
  routes through the existing applyCachedModel chunk planner, then
  gathers per-chunk vertex+index bytes from memory and feeds
  applyStreamedChunk synchronously. Every chunk lands is_resident=true
  immediately (no disk I/O to defer).

Streaming collapse:
- Delete the applyCachedModel(SidecarData) full-load path entirely.
- Rename applyCachedModelStreaming → applyCachedModel; loadSidecar
  always uses the metadata-only reader. Drop the --streaming CLI flag
  from IfcViewerWgpuMinimal and the streaming_enabled_ field.

WgpuSelectionState::ids() → selectionIds() so bonsai's
`viewport_->selection().selectionIds()` compiles unchanged.

Eigen3 added as a public dep of IfcViewerWgpu for the federation
matrices.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 17:36:02 +10:00

106 lines
4.7 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/>. *
* *
********************************************************************************/
#include <QApplication>
#include <QCommandLineParser>
#include <QMainWindow>
#include <QWidget>
#include <QVBoxLayout>
#include "WgpuViewportWindow.h"
// Stage-1 driver: opens a single window with the wgpu viewport embedded,
// clears to background colour, and exits on close. The shape mirrors
// ifcviewer-minimal so subsequent stages can grow this into a full
// benchmark-comparable binary.
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
app.setApplicationName("IfcViewerWgpuMinimal");
app.setOrganizationName("IfcOpenShell");
QCommandLineParser parser;
parser.setApplicationDescription(
"IfcOpenShell minimal wgpu IFC viewer");
parser.addHelpOption();
parser.addPositionalArgument("files",
"Sidecar (.ifcview) files to load. Stem-based: foo.ifc resolves to foo.ifcview.",
"[files...]");
parser.addOption({{"s", "screenshot"},
"Render one frame, save to PATH as PNG, exit.", "path"});
parser.addOption({{"b", "benchmark"},
"Render N frames (yaw-sweeping the camera), print stats, exit.", "frames"});
parser.addOption({{"c", "camera"},
"Set camera as tx,ty,tz,dist,yaw,pitch (same format as IfcViewerMinimal).",
"params"});
parser.addOption({"no-hiz",
"Disable HiZ occlusion culling for perf diagnostics."});
parser.addOption({"web-limits",
"Request the WebGPU mandatory floor limits (128MB max storage binding) "
"instead of the adapter's actual max. Use to verify scenes fit through "
"browser constraints."});
parser.process(app);
auto* viewport = new WgpuViewportWindow;
viewport->resize(1280, 800);
if (parser.isSet("no-hiz")) viewport->hiz_enabled_ = false;
if (parser.isSet("web-limits")) viewport->web_limits_ = true;
QWidget* container = QWidget::createWindowContainer(viewport);
container->setMinimumSize(320, 240);
QMainWindow main_window;
main_window.setWindowTitle("IfcViewer (wgpu) — stage 2");
main_window.setCentralWidget(container);
main_window.resize(1280, 800);
main_window.show();
// Queue sidecars; they're loaded after wgpu init completes in
// exposeEvent. Ordering matches the command line.
for (const QString& path : parser.positionalArguments()) {
viewport->queueLoadSidecar(path);
}
if (parser.isSet("camera")) {
const QStringList parts = parser.value("camera").split(',');
if (parts.size() == 6) {
bool ok = true;
float v[6];
for (int i = 0; i < 6 && ok; ++i) v[i] = parts[i].toFloat(&ok);
if (ok) {
viewport->setCamera(v[0], v[1], v[2], v[3], v[4], v[5]);
} else {
qWarning() << "--camera: failed to parse" << parser.value("camera");
}
} else {
qWarning() << "--camera: expected 6 comma-separated floats, got"
<< parts.size();
}
}
if (parser.isSet("screenshot")) {
viewport->captureNextFrameToPng(parser.value("screenshot"),
/*quit_after=*/true);
}
if (parser.isSet("benchmark")) {
viewport->setBenchmarkFrames(parser.value("benchmark").toInt());
}
return app.exec();
}