From 4b95454e8a2bc76db9754463fefeca849bea4c26 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 16 Nov 2026 14:02:27 +0100 Subject: [PATCH] Open in half the time Before the window had anything to draw, the GUI thread sculpted the set one figure after another and computed 98 textures, each twice. A set's figures are now cast side by side on every core, a geometry declared in QML is built once rather than once per property (which also sculpted an Orbital pawn nobody asked for), each surface is computed once, and the scene shares two textures between every square and piece. Co-Authored-By: Claude Fable 5.2 --- src/BoardView.qml | 6 +++++- src/ChessPiece.qml | 6 ++++-- src/sculptgeometry.cpp | 37 +++++++++++++++++++++++++++++++------ src/sculptgeometry.h | 4 ++++ src/surfacetexture.cpp | 9 +++++++-- src/woodland.cpp | 10 +++++++++- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/BoardView.qml b/src/BoardView.qml index 1cc56bd..47a28f5 100644 --- a/src/BoardView.qml +++ b/src/BoardView.qml @@ -124,7 +124,9 @@ Item { specularAAEnabled: true tonemapMode: SceneEnvironment.TonemapModeAces } + // Two surfaces dress the whole scene, shared by every square and piece: the set's own, and the pale wood of Atelier's light side. Texture { id: surfaceMap; textureData: SurfaceTexture { style: board.design } tilingModeHorizontal: Texture.Repeat; tilingModeVertical: Texture.Repeat } + Texture { id: paleMap; textureData: SurfaceTexture { style: 5 } tilingModeHorizontal: Texture.Repeat; tilingModeVertical: Texture.Repeat } Node { eulerRotation: Qt.vector3d(board.elevation,board.azimuth,0) PerspectiveCamera { id: camera; z: board.distance; clipNear: .1; clipFar: 150; fieldOfView: board.lens } @@ -176,7 +178,7 @@ Item { y: .04 scale: Qt.vector3d(.00996,.001,.00996) materials: PrincipledMaterial { - baseColorMap: Texture { textureData: SurfaceTexture { style: board.design === 1 && (Math.floor(tile.index / 8) + tile.index % 8) % 2 === 1 ? 5 : board.design } } + baseColorMap: board.design === 1 && (Math.floor(tile.index / 8) + tile.index % 8) % 2 === 1 ? paleMap : surfaceMap baseColor: tile.marked ? board.accents[board.design] : (Math.floor(index / 8) + index % 8) % 2 === 1 ? board.lightSquares[board.design] : board.darkSquares[board.design] roughness: board.design === 3 ? .3 : .55 metalness: board.design === 0 ? .15 : 0 @@ -202,6 +204,8 @@ Item { design: board.design seat: board.seat motion: board.motion + grain: surfaceMap + paleGrain: paleMap clock: board.clock attention: backend.selected selected: backend.selected === record.square && record.alive diff --git a/src/ChessPiece.qml b/src/ChessPiece.qml index c15700a..3268519 100644 --- a/src/ChessPiece.qml +++ b/src/ChessPiece.qml @@ -11,6 +11,9 @@ Node { required property bool motion required property bool selected required property real seat + // The scene's shared surfaces: the set's own, and Atelier's pale wood. + required property Texture grain + required property Texture paleGrain property real lift: 0 // Followed by hand, like `place`, so a capture can be timed to its captor's arrival. property bool alive: record.alive @@ -266,10 +269,9 @@ Node { property int boardSquare: piece.record.square pickable: piece.record.alive geometry: SculptGeometry { kind: piece.record.kind; design: piece.design; tone: piece.record.white ? 0 : 1; part: 0 } - Texture { id: surface; textureData: SurfaceTexture { style: piece.design === 1 ? (piece.record.white ? 5 : 1) : piece.design } tilingModeVertical: Texture.Repeat } // Ceramic or gunmetal, polished wood, painted carving, boiled sugar. materials: PrincipledMaterial { - baseColorMap: piece.woodland ? null : surface + baseColorMap: piece.woodland ? null : piece.design === 1 && piece.record.white ? piece.paleGrain : piece.grain baseColor: piece.bodyColor vertexColorsEnabled: true metalness: piece.design === 0 ? (piece.record.white ? .2 : .5) : 0 diff --git a/src/sculptgeometry.cpp b/src/sculptgeometry.cpp index 74f4af5..d71c7b5 100644 --- a/src/sculptgeometry.cpp +++ b/src/sculptgeometry.cpp @@ -4,15 +4,25 @@ #include #include #include +#include #include #include -SculptGeometry::SculptGeometry(QQuick3DObject *parent) : QQuick3DGeometry(parent) { rebuild(); } -void SculptGeometry::setKind(int value) { if (m_kind != value) { m_kind=value; rebuild(); emit changed(); } } -void SculptGeometry::setDesign(int value) { if (m_design != value) { m_design=value; rebuild(); emit changed(); } } -void SculptGeometry::setPart(int value) { if (m_part != value) { m_part=value; rebuild(); emit changed(); } } -void SculptGeometry::setTone(int value) { if (m_tone != value) { m_tone=value; rebuild(); emit changed(); } } +// Nothing is sculpted for the defaults a geometry starts with: one made in code builds on its first setter, +// and one declared in QML waits for the last of them, so neither casts a figure nobody asked for. +SculptGeometry::SculptGeometry(QQuick3DObject *parent) : QQuick3DGeometry(parent) {} +void SculptGeometry::setKind(int value) { const bool moved=m_kind != value; m_kind=value; if (moved || !m_built) { rebuild(); if (moved) emit changed(); } } +void SculptGeometry::setDesign(int value) { const bool moved=m_design != value; m_design=value; if (moved || !m_built) { rebuild(); if (moved) emit changed(); } } +void SculptGeometry::setPart(int value) { const bool moved=m_part != value; m_part=value; if (moved || !m_built) { rebuild(); if (moved) emit changed(); } } +void SculptGeometry::setTone(int value) { const bool moved=m_tone != value; m_tone=value; if (moved || !m_built) { rebuild(); if (moved) emit changed(); } } +void SculptGeometry::componentComplete() { + QQuick3DGeometry::componentComplete(); + m_declared=false; + rebuild(); +} void SculptGeometry::rebuild() { + if(m_declared) return; + m_built=true; // Sculpting takes a moment, so every part is made once and shared by all the pieces that wear it. struct Sculpture { QByteArray vertices, indices; QVector3D minimum, maximum; }; static std::map sculptures; @@ -39,8 +49,23 @@ void SculptGeometry::rebuild() { const auto found=sculptures.find(key); if(found!=sculptures.end()) { upload(found->second); return; } // Moss & Myth figures are whole painted carvings; the other sets are cast part by part. + // A cast set is wanted whole the moment one part of it is, so its parts are cast side by side on every core + // rather than one after another while the window waits. Moss & Myth carves its six figures the same way. + static std::map cast; + static bool poured[4]={}; + if(!woodland && m_part<4 && !poured[m_design]) { + poured[m_design]=true; + struct Job { int kind,part; SculptFigure figure; }; + QList jobs; + for(int kind=1;kind<=6;++kind) for(int part=0;part<4;++part) jobs.append({kind,part,{}}); + const int design=m_design; + QtConcurrent::blockingMap(jobs,[design](Job &job) { job.figure=Figures::part(design,job.kind,job.part); }); + for(Job &job:jobs) cast.emplace((design*6+job.kind-1)*6+job.part,std::move(job.figure)); + } SculptFigure figure; - if(!woodland) figure=Figures::part(m_design,m_kind,m_part); + const auto ready=cast.find((m_design*6+m_kind-1)*6+m_part); + if(!woodland && ready!=cast.end()) { figure=std::move(ready->second); cast.erase(ready); } + else if(!woodland) figure=Figures::part(m_design,m_kind,m_part); else if(m_part==0) figure=Woodland::figure(m_kind,m_tone); // A part a piece does not have is a single point, which draws nothing. if(figure.vertices.empty()) { diff --git a/src/sculptgeometry.h b/src/sculptgeometry.h index 00d2776..0552568 100644 --- a/src/sculptgeometry.h +++ b/src/sculptgeometry.h @@ -18,9 +18,13 @@ class SculptGeometry : public QQuick3DGeometry { void setDesign(int value); void setPart(int value); void setTone(int value); + // A geometry declared in QML is built once, when all four of its properties are in. + void classBegin() override { m_declared = true; } + void componentComplete() override; signals: void changed(); private: void rebuild(); int m_kind = 1, m_design = 0, m_part = 0, m_tone = 0; + bool m_declared = false, m_built = false; }; diff --git a/src/surfacetexture.cpp b/src/surfacetexture.cpp index e7b2b65..047e411 100644 --- a/src/surfacetexture.cpp +++ b/src/surfacetexture.cpp @@ -1,4 +1,5 @@ #include "surfacetexture.h" +#include #include #include #include @@ -12,9 +13,13 @@ void SurfaceTexture::rebuild() { const int width = 256, height = m_style == 4 ? 128 : 256; setSize({width,height}); setHasTransparency(false); + setFormat(m_style == 4 ? QQuick3DTextureData::RGBA32F : QQuick3DTextureData::RGBA8); + // Each surface is computed once and shared by every texture that shows it. + static QHash surfaces; + const auto made = surfaces.constFind(m_style); + if (made != surfaces.constEnd()) { setTextureData(*made); return; } QByteArray data; if (m_style == 4) { - setFormat(QQuick3DTextureData::RGBA32F); data.resize(width*height*4*sizeof(float)); for (int y=0;y #include "clay.h" #include #include @@ -604,8 +605,15 @@ const Woodland::Figure &Woodland::figure(int kind,int tone) { const int key=kind*2+(tone ? 1 : 0); const auto found=figures.find(key); if(found!=figures.end()) return found->second; + // The first figure asked for has all six carved, each on a thread of its own: a set is never wanted one figure at a time. + if(carvings.empty()) { + Carving carved[6]; + std::vector carvers; + for(int each=0;each<6;++each) carvers.emplace_back([&carved,each] { carved[each]=sculpture(each+1); }); + for(auto &carver:carvers) carver.join(); + for(int each=0;each<6;++each) carvings.emplace(each+1,std::move(carved[each])); + } auto carving=carvings.find(kind); - if(carving==carvings.end()) carving=carvings.emplace(kind,sculpture(kind)).first; const Carving &carved=carving->second; Figure figure; figure.indices=carved.indices;