Skip to content

Commit a7e8c2d

Browse files
committed
Make GPU rendering toggleable to prevent crashes due to bad initialisation
1 parent 6271507 commit a7e8c2d

File tree

11 files changed

+72
-27
lines changed

11 files changed

+72
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
- 1.26.4
2+
- Make GPU rendering toggleable to prevent crashes due to bad initialisation
3+
4+
15
- 1.26.3
26
- Fix potential bug with updating frequency spinner and slider from not-GUI thread
37

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>sh.ball</groupId>
88
<artifactId>osci-render</artifactId>
9-
<version>1.26.3</version>
9+
<version>1.26.4</version>
1010

1111
<name>osci-render</name>
1212

src/main/java/sh/ball/engine/Camera.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Camera {
2424

2525
private double focalLength;
2626
private Vector3 pos;
27+
private boolean hideEdges = false;
28+
private boolean usingGpu = false;
2729

2830
public Camera(double focalLength, Vector3 pos) {
2931
this.focalLength = focalLength;
@@ -105,4 +107,20 @@ public void setFocalLength(double focalLength) {
105107
public double getFocalLength() {
106108
return focalLength;
107109
}
110+
111+
public void hideEdges(boolean hideEdges) {
112+
this.hideEdges = hideEdges;
113+
}
114+
115+
public boolean edgesHidden() {
116+
return hideEdges;
117+
}
118+
119+
public void usingGpu(boolean usingGpu) {
120+
this.usingGpu = usingGpu;
121+
}
122+
123+
public boolean isUsingGpu() {
124+
return usingGpu;
125+
}
108126
}

src/main/java/sh/ball/engine/CameraDrawKernel.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public class CameraDrawKernel extends Kernel {
3636
private float focalLength;
3737
private int hideEdges = 0;
3838
private int usingObjectSet = 0;
39+
private boolean initialisedGpu = false;
3940
private boolean failedGpu = false;
41+
private int maxGroupSize = 256;
4042

4143
public CameraDrawKernel() {}
4244

@@ -131,9 +133,13 @@ public List<Shape> draw(Camera camera, WorldObject object) {
131133
this.cameraPosZ = (float) cameraPos.z;
132134
this.focalLength = (float) camera.getFocalLength();
133135

134-
this.hideEdges = object.edgesHidden() ? 1 : 0;
136+
this.hideEdges = camera.edgesHidden() ? 1 : 0;
135137

136-
return executeGpu();
138+
if (!camera.isUsingGpu() || failedGpu || vertices.length / 3 < MIN_GPU_VERTICES) {
139+
return executeCpu();
140+
} else {
141+
return executeGpu();
142+
}
137143
}
138144

139145
private List<Shape> postProcessVertices() {
@@ -164,16 +170,14 @@ private List<Shape> executeCpu() {
164170
}
165171

166172
private List<Shape> executeGpu() {
167-
if (failedGpu || vertices.length / 3 < MIN_GPU_VERTICES) {
168-
return executeCpu();
169-
}
170-
171173
try {
172-
int maxGroupSize = 256;
173-
try {
174-
maxGroupSize = getKernelMaxWorkGroupSize(getTargetDevice());
175-
} catch (QueryFailedException e) {
176-
logger.log(Level.WARNING, e.getMessage(), e);
174+
if (!initialisedGpu) {
175+
try {
176+
maxGroupSize = getKernelMaxWorkGroupSize(getTargetDevice());
177+
initialisedGpu = true;
178+
} catch (QueryFailedException e) {
179+
logger.log(Level.WARNING, e.getMessage(), e);
180+
}
177181
}
178182

179183
execute(Range.create(roundUp(vertices.length / 3, maxGroupSize), maxGroupSize));

src/main/java/sh/ball/engine/WorldObject.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class WorldObject {
2626
private List<List<Vector3>> vertexPath;
2727
private Vector3 position;
2828
private Vector3 rotation;
29-
private boolean hideEdges = false;
3029

3130
public WorldObject(Vector3[] vertices, int[] edgeIndices, int[][] faceIndices) {
3231
vertexPath = new ArrayList<>();
@@ -319,12 +318,4 @@ public WorldObject clone() {
319318
return new WorldObject(new ArrayList<>(objVertices), new ArrayList<>(vertexPath), position,
320319
rotation);
321320
}
322-
323-
public void hideEdges(boolean hideEdges) {
324-
this.hideEdges = hideEdges;
325-
}
326-
327-
public boolean edgesHidden() {
328-
return hideEdges;
329-
}
330321
}

src/main/java/sh/ball/gui/controller/MainController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
148148
@FXML
149149
private CheckMenuItem hideHiddenMeshesCheckMenuItem;
150150
@FXML
151+
private CheckMenuItem renderUsingGpuCheckMenuItem;
152+
@FXML
151153
private Spinner<Integer> midiChannelSpinner;
152154
@FXML
153155
private Spinner<Double> attackSpinner;
@@ -408,6 +410,8 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
408410

409411
hideHiddenMeshesCheckMenuItem.selectedProperty().addListener((e, old, hidden) -> objController.hideHiddenMeshes(hidden));
410412

413+
renderUsingGpuCheckMenuItem.selectedProperty().addListener((e, old, usingGpu) -> objController.renderUsingGpu(usingGpu));
414+
411415
NumberFormat format = NumberFormat.getIntegerInstance();
412416
UnaryOperator<TextFormatter.Change> filter = c -> {
413417
if (c.isContentChange()) {
@@ -691,6 +695,7 @@ private void changeFrameSource(int index) {
691695
}
692696
// FIXME: Is this safe since we are accessing GUI object in non-GUI thread?
693697
objController.hideHiddenMeshes(hideHiddenMeshesCheckMenuItem.isSelected());
698+
objController.renderUsingGpu(renderUsingGpuCheckMenuItem.isSelected());
694699
executor.submit(producer);
695700
} else if (samples != null) {
696701
samples.enable();
@@ -1141,6 +1146,10 @@ private void saveProject(String projectFileName) {
11411146
hiddenEdges.appendChild(document.createTextNode(String.valueOf(hideHiddenMeshesCheckMenuItem.isSelected())));
11421147
root.appendChild(hiddenEdges);
11431148

1149+
Element usingGpu = document.createElement("usingGpu");
1150+
usingGpu.appendChild(document.createTextNode(String.valueOf(renderUsingGpuCheckMenuItem.isSelected())));
1151+
root.appendChild(usingGpu);
1152+
11441153
Element translationIncrement = document.createElement("translationIncrement");
11451154
translationIncrement.appendChild(document.createTextNode(translationIncrementSpinner.getValue().toString()));
11461155
root.appendChild(translationIncrement);
@@ -1263,6 +1272,11 @@ private void openProject(String projectFileName) {
12631272
hideHiddenMeshesCheckMenuItem.setSelected(Boolean.parseBoolean(hiddenEdges.getTextContent()));
12641273
}
12651274

1275+
Element usingGpu = (Element) root.getElementsByTagName("usingGpu").item(0);
1276+
if (usingGpu != null) {
1277+
renderUsingGpuCheckMenuItem.setSelected(Boolean.parseBoolean(usingGpu.getTextContent()));
1278+
}
1279+
12661280
Element translationIncrement = (Element) root.getElementsByTagName("translationIncrement").item(0);
12671281
if (translationIncrement != null) {
12681282
translationIncrementSpinner.getValueFactory().setValue(Double.parseDouble(translationIncrement.getTextContent()));

src/main/java/sh/ball/gui/controller/ObjController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,8 @@ public List<Element> save(Document document) {
182182

183183
@Override
184184
public void load(Element root) {}
185+
186+
public void renderUsingGpu(boolean usingGpu) {
187+
producer.setFrameSettings(ObjSettingsFactory.renderUsingGpu(usingGpu));
188+
}
185189
}

src/main/java/sh/ball/parser/obj/ObjFrameSettings.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public class ObjFrameSettings {
1111
public Double rotateSpeed;
1212
public boolean resetRotation = false;
1313
public Boolean hideEdges = null;
14+
public Boolean usingGpu = null;
1415

15-
protected ObjFrameSettings(Double focalLength, Vector3 cameraPos, Vector3 baseRotation, Vector3 currentRotation, Double rotateSpeed, boolean resetRotation, Boolean hideEdges) {
16+
protected ObjFrameSettings(Double focalLength, Vector3 cameraPos, Vector3 baseRotation, Vector3 currentRotation, Double rotateSpeed, boolean resetRotation, Boolean hideEdges, Boolean usingGpu) {
1617
this.focalLength = focalLength;
1718
this.cameraPos = cameraPos;
1819
this.baseRotation = baseRotation;
1920
this.currentRotation = currentRotation;
2021
this.rotateSpeed = rotateSpeed;
2122
this.resetRotation = resetRotation;
2223
this.hideEdges = hideEdges;
24+
this.usingGpu = usingGpu;
2325
}
2426

2527
protected ObjFrameSettings(double focalLength) {

src/main/java/sh/ball/parser/obj/ObjFrameSource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ public void setFrameSettings(Object settings) {
6868
object.resetRotation();
6969
}
7070
if (obj.hideEdges != null) {
71-
object.hideEdges(obj.hideEdges);
71+
camera.hideEdges(obj.hideEdges);
72+
}
73+
if (obj.usingGpu != null) {
74+
camera.usingGpu(obj.usingGpu);
7275
}
7376
}
7477
}
7578

7679
// TODO: Refactor!
7780
@Override
7881
public Object getFrameSettings() {
79-
return new ObjFrameSettings(null, null, baseRotation, currentRotation, null, false, object.edgesHidden());
82+
return new ObjFrameSettings(null, null, baseRotation, currentRotation, null, false, camera.edgesHidden(), camera.isUsingGpu());
8083
}
8184
}

src/main/java/sh/ball/parser/obj/ObjSettingsFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static ObjFrameSettings baseRotation(Vector3 baseRotation) {
1717
}
1818

1919
public static ObjFrameSettings rotation(Vector3 baseRotation, Vector3 currentRotation) {
20-
return new ObjFrameSettings(null, null, baseRotation, currentRotation, null, false, null);
20+
return new ObjFrameSettings(null, null, baseRotation, currentRotation, null, false, null, null);
2121
}
2222

2323
public static ObjFrameSettings rotateSpeed(double rotateSpeed) {
@@ -29,6 +29,10 @@ public static ObjFrameSettings resetRotation() {
2929
}
3030

3131
public static ObjFrameSettings hideEdges(Boolean hideEdges) {
32-
return new ObjFrameSettings(null, null, null, null, null, false, hideEdges);
32+
return new ObjFrameSettings(null, null, null, null, null, false, hideEdges, null);
33+
}
34+
35+
public static ObjFrameSettings renderUsingGpu(Boolean usingGpu) {
36+
return new ObjFrameSettings(null, null, null, null, null, false, null, usingGpu);
3337
}
3438
}

0 commit comments

Comments
 (0)