mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-13 02:47:48 +00:00
ifcviewer-web: wire WebViewportHost + ViewportCore (#87)
Replaces the standalone wgpu-only clear-color spike in main_web.cpp with a real WebViewportHost implementation: surface creation via the emdawnwebgpu canvas-selector source, framebufferSize through emscripten_get_element_css_size + dpr, requestFrame as a deferred flag the RAF main_loop consumes, quit through emscripten_force_exit. main_web.cpp now does the same lifecycle the desktop initWgpu shell does: core_.initWgpu(web_limits=true) → buildPipelines → buildHiz/ Edge/Pick. The render loop runs core_.render() once per RAF tick when the host has flagged a frame pending, with a surface reconfigure on size changes. Builds clean under emcc 6.0 + emdawnwebgpu (1.4 MB wasm, 278 KB JS glue). Renders an empty scene with the configured background — the plumbing is end-to-end through the same ViewportCore code path the desktop build uses. No sidecar load yet: that lands with the emscripten_fetch streaming backend (#88).
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/********************************************************************************
|
||||
* *
|
||||
* 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 "WebViewportHost.h"
|
||||
|
||||
#include <emscripten/emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
WebViewportHost::WebViewportHost(std::string canvas_selector)
|
||||
: canvas_selector_(std::move(canvas_selector)) {}
|
||||
|
||||
WGPUSurface WebViewportHost::createSurface(WGPUInstance instance) {
|
||||
// Emdawnwebgpu canvas-selector surface source. Same shape as
|
||||
// WGPUSurfaceSourceCanvasHTMLSelector_Emscripten in Dawn's headers.
|
||||
WGPUEmscriptenSurfaceSourceCanvasHTMLSelector canvas_desc = {};
|
||||
canvas_desc.chain.sType =
|
||||
WGPUSType_EmscriptenSurfaceSourceCanvasHTMLSelector;
|
||||
canvas_desc.selector.data = canvas_selector_.c_str();
|
||||
canvas_desc.selector.length = canvas_selector_.size();
|
||||
|
||||
WGPUSurfaceDescriptor surface_desc = {};
|
||||
surface_desc.nextInChain = &canvas_desc.chain;
|
||||
return wgpuInstanceCreateSurface(instance, &surface_desc);
|
||||
}
|
||||
|
||||
void WebViewportHost::framebufferSize(int& width_px, int& height_px) const {
|
||||
double w_css = 0.0, h_css = 0.0;
|
||||
// Pull the element's logical (CSS-pixel) size, then multiply by DPR
|
||||
// — matches the QWindow desktop host's framebufferSize semantics.
|
||||
emscripten_get_element_css_size(canvas_selector_.c_str(), &w_css, &h_css);
|
||||
const float ratio = dpr();
|
||||
width_px = int(w_css * double(ratio));
|
||||
height_px = int(h_css * double(ratio));
|
||||
if (width_px < 1) width_px = 1;
|
||||
if (height_px < 1) height_px = 1;
|
||||
}
|
||||
|
||||
float WebViewportHost::dpr() const {
|
||||
return float(emscripten_get_device_pixel_ratio());
|
||||
}
|
||||
|
||||
void WebViewportHost::requestFrame() {
|
||||
request_frame_pending_ = true;
|
||||
}
|
||||
|
||||
void WebViewportHost::quit() {
|
||||
// emscripten_force_exit honours -sEXIT_RUNTIME=1; without that flag
|
||||
// the runtime swallows the call and keeps the page interactive.
|
||||
emscripten_force_exit(0);
|
||||
}
|
||||
|
||||
bool WebViewportHost::consumeFrameRequest() {
|
||||
const bool pending = request_frame_pending_;
|
||||
request_frame_pending_ = false;
|
||||
return pending;
|
||||
}
|
||||
Reference in New Issue
Block a user