Let the viewport composite over what is behind it

Background colour gains a meaningful alpha. Below 1 the viewport composites
over whatever the host has stacked behind it rather than painting a colour
of its own; at 0 it clears to nothing, so the model draws over another 3D
view, a map, or ordinary page content. There is no depth interaction -- the
layer behind is strictly behind and cannot occlude the model.

Honouring the alpha needs a premultiplied surface. configureSurface asks for
one only when the platform advertises it, the same way it picks a present
mode, and falls back to Auto otherwise -- this machine's Vulkan surface
offers opaque compositing only, so hardcoding premultiplied would have
requested a mode it does not support. The clear follows that choice: scaled
by alpha when premultiplied, held at 1 when not, since scaling would
otherwise just darken the colour for an alpha nobody reads.

ViewportWindow::setBackgroundColor now forwards to core rather than writing
through a reference proxy, dropping one more member from the friend
declaration it is meant to shrink. The screenshot QImage is tagged
premultiplied to match what the main pass leaves in the buffer.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-08-04 10:46:02 +10:00
parent c9f269a0e5
commit 0b8875669e
8 changed files with 227 additions and 12 deletions
+42 -5
View File
@@ -489,6 +489,13 @@ void ViewportCore::setBackfaceCulling(bool enabled) {
host_->requestFrame();
}
void ViewportCore::setBackgroundColor(float r, float g, float b, float a) {
Eigen::Vector4f next{r, g, b, a};
if (background_color_ == next) return;
background_color_ = next;
host_->requestFrame();
}
bool ViewportCore::frameSelection() {
if (selection_.count() == 0) return false;
float lo[3] = { std::numeric_limits<float>::infinity(),
@@ -6869,6 +6876,22 @@ void ViewportCore::configureSurface(int width_px, int height_px) {
case WGPUPresentMode_Fifo: pm_name = "fifo"; break;
default: break;
}
// Premultiplied is what lets setBackgroundColor's alpha reach the
// compositor, so a clear below alpha 1 shows through to whatever the
// viewport is stacked over. Not every surface advertises it, so pick it
// only when offered and fall back to Auto — which composites opaquely and
// discards the alpha channel — otherwise. The two are indistinguishable
// at alpha 1, the default and the only value a caller that never touches
// the background will see, so the fallback costs nothing there.
auto supportsAlphaMode = [&](WGPUCompositeAlphaMode mode) {
for (std::size_t i = 0; i < caps.alphaModeCount; ++i) {
if (caps.alphaModes[i] == mode) return true;
}
return false;
};
const bool premultiplied =
supportsAlphaMode(WGPUCompositeAlphaMode_Premultiplied);
wgpuSurfaceCapabilitiesFreeMembers(caps);
cfg.presentMode = pm;
if (!surface_configured_) {
@@ -6886,7 +6909,14 @@ void ViewportCore::configureSurface(int width_px, int height_px) {
}
Log::info() << "[wgpu] present mode = " << pm_name << note;
}
cfg.alphaMode = WGPUCompositeAlphaMode_Auto;
cfg.alphaMode = premultiplied ? WGPUCompositeAlphaMode_Premultiplied
: WGPUCompositeAlphaMode_Auto;
surface_premultiplied_ = premultiplied;
if (!surface_configured_) {
Log::info() << "[wgpu] composite alpha = "
<< (premultiplied ? "premultiplied (background alpha honoured)"
: "auto (opaque -- background alpha ignored)");
}
wgpuSurfaceConfigure(surface_, &cfg);
configured_w_ = width_px;
@@ -7292,11 +7322,18 @@ void ViewportCore::render() {
color.resolveTarget = view;
color.loadOp = WGPULoadOp_Clear;
color.storeOp = WGPUStoreOp_Store;
// A premultiplied surface expects colour already scaled by alpha, so the
// clear fades toward transparent instead of tinting what shows through:
// leaving it unscaled would have the compositor add the background colour
// on top of the layer behind. When the surface could only be configured
// opaque the alpha is discarded anyway, and scaling would darken the
// colour for nothing — so hold alpha at 1 and write it straight.
const float bg_a = surface_premultiplied_ ? background_color_[3] : 1.0f;
color.clearValue = {
srgbToLinear(background_color_[0]),
srgbToLinear(background_color_[1]),
srgbToLinear(background_color_[2]),
1.0,
srgbToLinear(background_color_[0]) * bg_a,
srgbToLinear(background_color_[1]) * bg_a,
srgbToLinear(background_color_[2]) * bg_a,
bg_a,
};
color.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
+19
View File
@@ -248,6 +248,20 @@ public:
void setBackfaceCulling(bool enabled);
bool backfaceCulling() const { return backface_culling_; }
// Background colour, RGBA in [0..1]. An alpha below 1 makes the viewport
// composite over whatever is behind it rather than painting a colour of
// its own: at alpha 0 it clears to nothing, so the model appears to sit
// on top of the layer behind — another canvas, a second 3D view, plain
// page content, anything the host stacks there. There is no depth
// interaction; the layer behind is strictly behind.
//
// Honouring the alpha needs a premultiplied surface, which configureSurface
// requests when the platform advertises it. Where it does not, the alpha is
// ignored and the clear stays opaque. At alpha 1 — the default, and every
// caller that leaves the background alone — the two are identical.
void setBackgroundColor(float r, float g, float b, float a);
const Eigen::Vector4f& backgroundColor() const { return background_color_; }
// Frame the current selection: union the selected objects' world AABBs and
// fit the camera to them (same 1.30 padding as the desktop "F" hotkey).
// No-op with an empty selection or no resolvable AABBs; returns whether it
@@ -1504,6 +1518,11 @@ private:
// an sRGB-to-linear conversion on top so the on-screen colour
// matches the hex value passed via setBackgroundColor.
Eigen::Vector4f background_color_ = {0.125f, 0.137f, 0.161f, 1.0f};
// Whether the surface was configured with a premultiplied alpha mode, and
// so whether background_color_'s alpha reaches the compositor at all.
// Decided per configureSurface against the surface's advertised modes.
bool surface_premultiplied_ = false;
};
#endif // VIEWPORTCORE_H
+8 -4
View File
@@ -233,7 +233,6 @@ ViewportWindow::ViewportWindow(QWindow* parent)
camera_fov_y_deg_(core_.camera_fov_y_deg_),
camera_near_ (core_.camera_near_),
camera_far_ (core_.camera_far_),
background_color_(core_.background_color_),
frame_uniform_buffer_(core_.frame_uniform_buffer_),
frame_bind_group_ (core_.frame_bind_group_),
selection_flags_buffer_ (core_.selection_flags_buffer_),
@@ -465,7 +464,13 @@ void ViewportWindow::saveScreenshotRgba8(const std::string& path,
// QImage takes a stride argument so it doesn't try to read past the
// last row — wgpu's staging buffer was BGRA8 padded; core already
// packed the rows tightly into rgba.
QImage img(rgba, w, h, w * 4, QImage::Format_RGBA8888);
//
// Premultiplied, matching what the render pass leaves in the buffer: the
// main blend accumulates alpha as One/OneMinusSrcAlpha, so colour there is
// already scaled by coverage. Tagging it straight would wash out a
// screenshot taken over a translucent background; at the default opaque
// alpha the two formats describe the same bytes.
QImage img(rgba, w, h, w * 4, QImage::Format_RGBA8888_Premultiplied);
const QString qpath = QString::fromStdString(path);
if (img.save(qpath, "PNG")) {
Log::info() << "[wgpu] saved screenshot: "
@@ -476,8 +481,7 @@ void ViewportWindow::saveScreenshotRgba8(const std::string& path,
}
void ViewportWindow::setBackgroundColor(float r, float g, float b, float a) {
background_color_ = {r, g, b, a};
if (isExposed()) requestUpdate();
core_.setBackgroundColor(r, g, b, a);
}
// -----------------------------------------------------------------------------
-2
View File
@@ -760,8 +760,6 @@ private:
uint32_t& hiz_reject_count_;
std::atomic<int>& hiz_trace_budget_;
Eigen::Vector4f& background_color_;
// Camera state aliases (storage in core_).
float (&camera_target_)[3];
float& camera_distance_;