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
+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);
}
// -----------------------------------------------------------------------------