Skip to content

Commit 2acfac3

Browse files
committed
Show 3D frames only once panels over them are painted
- A frame drawn into a 3D view's window waits until Swing has painted what lies behind the floating panels, so the ground behind the height slider no longer trails the view while it moves Model used: Claude Opus 5.5
1 parent 54213d4 commit 2acfac3

3 files changed

Lines changed: 58 additions & 25 deletions

File tree

‎core/src/main/java/art/arcane/iris/studio/map/GpuFrames.java‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,39 @@ static Optional<OrbitPicture> drawOnScreen(ScreenTarget screen, Drawing drawing)
7777
if (!windows.get().drawInto(window)) {
7878
throw new IllegalStateException("GLX can't draw into the view's window");
7979
}
80-
return drawing.drawInto(new GpuWindowFrame(windows.get(), window, screen.seeThrough()));
80+
return drawing.drawInto(new GpuWindowFrame(screen.seeThrough()));
8181
} catch (RuntimeException failure) {
8282
gpu.stopDrawingIntoWindows(failure);
8383
return Optional.empty();
8484
}
8585
});
8686
}
8787

88+
/**
89+
* Puts the last frame drawn into the view's window on screen. Another view may have drawn into its own window
90+
* since, so the view's window is made current again first.
91+
*
92+
* @return false when the window didn't take it, and the caller should draw a picture instead
93+
*/
94+
static boolean showOnScreen(ScreenTarget screen) {
95+
return onCard(gpu -> {
96+
Optional<GlxDisplay> windows = gpu.windows();
97+
long window = screen.canvas().window();
98+
if (windows.isEmpty() || window == 0L) {
99+
return Optional.<Boolean>empty();
100+
}
101+
try {
102+
if (!windows.get().drawInto(window) || !windows.get().show(window)) {
103+
throw new IllegalStateException("The window didn't take the frame");
104+
}
105+
return Optional.of(true);
106+
} catch (RuntimeException failure) {
107+
gpu.stopDrawingIntoWindows(failure);
108+
return Optional.<Boolean>empty();
109+
}
110+
}).isPresent();
111+
}
112+
88113
static void failOnError() {
89114
int error = glGetError();
90115
if (error != GL_NO_ERROR) {
@@ -119,7 +144,7 @@ private static GpuFramebuffer offScreenFramebuffer() {
119144
return offScreen;
120145
}
121146

122-
private static Optional<OrbitPicture> onCard(CardWork work) {
147+
private static <T> Optional<T> onCard(CardWork<T> work) {
123148
if (failed) {
124149
return Optional.empty();
125150
}
@@ -145,7 +170,7 @@ private static void stopUsingCard(RuntimeException failure) {
145170
}
146171

147172
@FunctionalInterface
148-
private interface CardWork {
149-
Optional<OrbitPicture> drawOn(GpuContext gpu);
173+
private interface CardWork<T> {
174+
Optional<T> drawOn(GpuContext gpu);
150175
}
151176
}

‎core/src/main/java/art/arcane/iris/studio/map/GpuWindowFrame.java‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,20 @@
2828

2929
/**
3030
* A frame drawn straight into a view's window. Of what was drawn, only the patches Swing paints where panels float
31-
* over the window are read back. Only for the GPU thread.
31+
* over the window are read back. The frame waits in the window's back buffer until {@link GpuFrames#showOnScreen},
32+
* so it can go on screen together with its patches. Only for the GPU thread.
3233
*/
3334
final class GpuWindowFrame implements GpuFrameTarget {
3435
private static final int WINDOW_FRAMEBUFFER = 0;
3536

36-
private final GlxDisplay windows;
37-
private final long window;
3837
private final List<Rectangle> seeThrough;
3938
private int width;
4039
private int height;
4140

4241
/**
4342
* @param seeThrough the parts of the view panels float over, which Swing paints instead of the window
4443
*/
45-
GpuWindowFrame(GlxDisplay windows, long window, List<Rectangle> seeThrough) {
46-
this.windows = windows;
47-
this.window = window;
44+
GpuWindowFrame(List<Rectangle> seeThrough) {
4845
this.seeThrough = seeThrough;
4946
}
5047

@@ -62,12 +59,7 @@ public void bindSized(int nextWidth, int nextHeight) {
6259

6360
@Override
6461
public OrbitPicture finish(OrbitProjection projection, double depthRange) {
65-
OrbitPicture picture = OrbitPicture.onScreen(width, height, readPatches(),
66-
GpuFrameTarget.depthInMiddle(projection, depthRange, width, height));
67-
if (!windows.show(window)) {
68-
throw new IllegalStateException("The window didn't take the frame");
69-
}
70-
return picture;
62+
return OrbitPicture.onScreen(width, height, readPatches(), GpuFrameTarget.depthInMiddle(projection, depthRange, width, height));
7163
}
7264

7365
private List<PicturePatch> readPatches() {

‎core/src/main/java/art/arcane/iris/studio/map/OrbitViewport.java‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.awt.GraphicsConfiguration;
3232
import java.awt.Point;
3333
import java.awt.RenderingHints;
34+
import java.awt.Toolkit;
3435
import java.awt.event.ActionEvent;
3536
import java.awt.event.ActionListener;
3637
import java.awt.event.ComponentAdapter;
@@ -338,7 +339,7 @@ private void requestFrame() {
338339
renderThread.execute(() -> {
339340
OrbitPicture drawn = draw(frameScene, screen, frameCamera, width, height, covered);
340341
boolean missedScreen = screen != null && !drawn.onScreen();
341-
EventQueue.invokeLater(() -> finishFrame(frameScene, frameCamera, drawn, missedScreen, requestedAtNanos));
342+
EventQueue.invokeLater(() -> finishFrame(frameScene, frameCamera, screen, drawn, missedScreen, requestedAtNanos));
342343
});
343344
}
344345

@@ -380,25 +381,40 @@ private boolean hasUnscaledPixels() {
380381

381382
/**
382383
* Until the card is opened, nothing knows whether frames can go into the window; the first picture opens it. A
383-
* frame that should have gone into the window but couldn't isn't tried again right away. The next frame is asked
384-
* for before this one's patches are painted, so the card draws while Swing paints.
384+
* frame that should have gone into the window but couldn't isn't tried again right away. A frame for a scene
385+
* replaced meanwhile is never shown.
385386
*/
386-
private void finishFrame(OrbitScene frameScene, OrbitCamera frameCamera, OrbitPicture drawn, boolean missedScreen,
387-
long requestedAtNanos) {
387+
private void finishFrame(OrbitScene frameScene, OrbitCamera frameCamera, ScreenTarget screen, OrbitPicture drawn,
388+
boolean missedScreen, long requestedAtNanos) {
388389
rendering = false;
389390
measureFrameTime(System.nanoTime() - requestedAtNanos);
390-
boolean shown = frameScene == scene;
391-
if (shown) {
391+
if (frameScene == scene) {
392392
takePicture(drawn, frameCamera);
393+
paintPicture(drawn);
394+
showInWindow(screen, drawn);
393395
}
394396
boolean canGoOnScreen = !missedScreen && canvas != null && !canvas.isVisible() && drawsOnScreen();
395397
if (renderAgain || drawn.catchingUp() || canGoOnScreen) {
396398
renderAgain = false;
397399
requestFrame();
398400
}
399-
if (shown) {
400-
paintPicture(drawn);
401+
}
402+
403+
/**
404+
* A frame in the window only goes on screen once Swing has painted its patches, or the panels floating over the
405+
* window would show the frame before. Swing and the card reach the screen through separate connections, so
406+
* Swing's is flushed first. The render thread shows the frame before it draws the next one into the window.
407+
*/
408+
private void showInWindow(ScreenTarget screen, OrbitPicture drawn) {
409+
if (!drawn.onScreen()) {
410+
return;
401411
}
412+
Toolkit.getDefaultToolkit().sync();
413+
renderThread.execute(() -> {
414+
if (!GpuFrames.showOnScreen(screen)) {
415+
EventQueue.invokeLater(this::requestFrame);
416+
}
417+
});
402418
}
403419

404420
// A whole picture replaces the window until frames go into it again.

0 commit comments

Comments
 (0)