Skip to content

Commit f15f3ed

Browse files
Markil3stephengold
authored andcommitted
Refactors the context restart code. (#1526)
* Refactors the context restart code. Now most of the code that goes into initializing the renderer * Fixed a misleading comment in lwjgl3's LwjglWindow * Adds documentation to the LwjglContext methods. * Cleans up the code and comments. * Moves the renderer initialization for jme3-lwjgl to just the first run. Apparently, this seems to fix the issue of LWJGL not rendering to the new display. However, trying to apply this fix to jme3-lwjgl3 actually creates the problem there.
1 parent b70be12 commit f15f3ed

File tree

5 files changed

+274
-89
lines changed

5 files changed

+274
-89
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2009-2021 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package jme3test.renderer;
33+
34+
import com.jme3.app.SimpleApplication;
35+
import com.jme3.input.KeyInput;
36+
import com.jme3.input.controls.ActionListener;
37+
import com.jme3.input.controls.KeyTrigger;
38+
import com.jme3.material.Material;
39+
import com.jme3.math.ColorRGBA;
40+
import com.jme3.math.FastMath;
41+
import com.jme3.math.Quaternion;
42+
import com.jme3.renderer.RenderManager;
43+
import com.jme3.renderer.ViewPort;
44+
import com.jme3.scene.Geometry;
45+
import com.jme3.scene.control.AbstractControl;
46+
import com.jme3.scene.shape.Box;
47+
import com.jme3.system.AppSettings;
48+
49+
/**
50+
* Tests whether gamma correction works after a context restart. This test
51+
* generates a series of boxes, each one with a slightly different shade from
52+
* the other. If the boxes look the same before and after the restart, that
53+
* means that gamma correction is working properly.
54+
* <p>
55+
* Note that for testing, it may be helpful to bypass the test chooser and run
56+
* this class directly, since it can be easier to define your own settings
57+
* beforehand. Of course, it should still workif all you need to test is the
58+
* gamma correction, as long as you enable it in the settings dialog.
59+
* </p>
60+
*
61+
* @author Markil 3
62+
*/
63+
public class TestContextRestart extends SimpleApplication
64+
{
65+
public static final String INPUT_RESTART_CONTEXT = "SIMPLEAPP_Restart";
66+
67+
public static void main(String[] args)
68+
{
69+
TestContextRestart app = new TestContextRestart();
70+
AppSettings settings = new AppSettings(true);
71+
settings.setGammaCorrection(true);
72+
// settings.setRenderer(AppSettings.LWJGL_OPENGL32);
73+
app.setSettings(settings);
74+
app.start();
75+
}
76+
77+
@Override
78+
public void simpleInitApp()
79+
{
80+
for (int i = 0, l = 256; i < l; i += 8)
81+
{
82+
Geometry box = new Geometry("Box" + i, new Box(10, 200, 10));
83+
Material mat = new Material(this.assetManager,
84+
"Common/MatDefs/Misc/Unshaded.j3md");
85+
mat.setColor("Color", new ColorRGBA((float) i / 255F, 0, 0, 1));
86+
box.setMaterial(mat);
87+
box.setLocalTranslation(-2.5F * (l / 2 - i), 0, -700);
88+
box.addControl(new AbstractControl()
89+
{
90+
@Override
91+
protected void controlUpdate(float tpf)
92+
{
93+
float[] angles = this.getSpatial()
94+
.getLocalRotation()
95+
.toAngles(new float[3]);
96+
angles[0] = angles[0] + (FastMath.PI / 500F);
97+
this.getSpatial()
98+
.setLocalRotation(new Quaternion().fromAngles(angles));
99+
}
100+
101+
@Override
102+
protected void controlRender(RenderManager rm, ViewPort vp)
103+
{
104+
105+
}
106+
});
107+
this.rootNode.attachChild(box);
108+
}
109+
110+
this.viewPort.setBackgroundColor(ColorRGBA.Yellow);
111+
112+
this.flyCam.setEnabled(false);
113+
this.inputManager.setCursorVisible(true);
114+
115+
inputManager.addMapping(INPUT_RESTART_CONTEXT, new KeyTrigger(
116+
KeyInput.KEY_TAB));
117+
this.inputManager.addListener(new ActionListener()
118+
{
119+
@Override
120+
public void onAction(String name, boolean isPressed, float tpf)
121+
{
122+
if (name.equals(INPUT_RESTART_CONTEXT))
123+
{
124+
restart();
125+
}
126+
}
127+
}, INPUT_RESTART_CONTEXT);
128+
}
129+
}

jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglContext.java

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -251,42 +251,65 @@ protected int getNumSamplesToUse() {
251251
return samples;
252252
}
253253

254+
/**
255+
* Reinitializes the relevent details of the context. For internal use only.
256+
*/
257+
protected void reinitContext() {
258+
initContext(false);
259+
}
260+
261+
/**
262+
* Initializes the LWJGL renderer and input for the first time. For internal
263+
* use only.
264+
*/
254265
protected void initContextFirstTime() {
266+
initContext(true);
267+
}
268+
269+
/**
270+
* Initializes the LWJGL renderer and input.
271+
* @param first - Whether this is the first time we are initializing and we
272+
* need to create the renderer or not. Otherwise, we'll just reset the
273+
* renderer as needed.
274+
*/
275+
private void initContext(boolean first) {
255276
if (!GLContext.getCapabilities().OpenGL20) {
256277
throw new RendererException("OpenGL 2.0 or higher is "
257278
+ "required for jMonkeyEngine");
258279
}
259-
280+
260281
int vers[] = getGLVersion(settings.getRenderer());
261282
if (vers != null) {
262-
GL gl = new LwjglGL();
263-
GLExt glext = new LwjglGLExt();
264-
GLFbo glfbo;
265-
266-
if (GLContext.getCapabilities().OpenGL30) {
267-
glfbo = new LwjglGLFboGL3();
268-
} else {
269-
glfbo = new LwjglGLFboEXT();
270-
}
271-
272-
if (settings.getBoolean("GraphicsDebug")) {
273-
gl = (GL) GLDebug.createProxy(gl, gl, GL.class, GL2.class, GL3.class, GL4.class);
274-
glext = (GLExt) GLDebug.createProxy(gl, glext, GLExt.class);
275-
glfbo = (GLFbo) GLDebug.createProxy(gl, glfbo, GLFbo.class);
276-
}
277-
if (settings.getBoolean("GraphicsTiming")) {
278-
GLTimingState timingState = new GLTimingState();
279-
gl = (GL) GLTiming.createGLTiming(gl, timingState, GL.class, GL2.class, GL3.class, GL4.class);
280-
glext = (GLExt) GLTiming.createGLTiming(glext, timingState, GLExt.class);
281-
glfbo = (GLFbo) GLTiming.createGLTiming(glfbo, timingState, GLFbo.class);
282-
}
283-
if (settings.getBoolean("GraphicsTrace")) {
284-
gl = (GL) GLTracer.createDesktopGlTracer(gl, GL.class, GL2.class, GL3.class, GL4.class);
285-
glext = (GLExt) GLTracer.createDesktopGlTracer(glext, GLExt.class);
286-
glfbo = (GLFbo) GLTracer.createDesktopGlTracer(glfbo, GLFbo.class);
283+
if (first) {
284+
GL gl = new LwjglGL();
285+
GLExt glext = new LwjglGLExt();
286+
GLFbo glfbo;
287+
288+
if (GLContext.getCapabilities().OpenGL30) {
289+
glfbo = new LwjglGLFboGL3();
290+
} else {
291+
glfbo = new LwjglGLFboEXT();
292+
}
293+
294+
if (settings.getBoolean("GraphicsDebug")) {
295+
gl = (GL) GLDebug.createProxy(gl, gl, GL.class, GL2.class, GL3.class, GL4.class);
296+
glext = (GLExt) GLDebug.createProxy(gl, glext, GLExt.class);
297+
glfbo = (GLFbo) GLDebug.createProxy(gl, glfbo, GLFbo.class);
298+
}
299+
if (settings.getBoolean("GraphicsTiming")) {
300+
GLTimingState timingState = new GLTimingState();
301+
gl = (GL) GLTiming.createGLTiming(gl, timingState, GL.class, GL2.class, GL3.class, GL4.class);
302+
glext = (GLExt) GLTiming.createGLTiming(glext, timingState, GLExt.class);
303+
glfbo = (GLFbo) GLTiming.createGLTiming(glfbo, timingState, GLFbo.class);
304+
}
305+
if (settings.getBoolean("GraphicsTrace")) {
306+
gl = (GL) GLTracer.createDesktopGlTracer(gl, GL.class, GL2.class, GL3.class, GL4.class);
307+
glext = (GLExt) GLTracer.createDesktopGlTracer(glext, GLExt.class);
308+
glfbo = (GLFbo) GLTracer.createDesktopGlTracer(glfbo, GLFbo.class);
309+
}
310+
renderer = new GLRenderer(gl, glext, glfbo);
311+
renderer.initialize();
287312
}
288-
renderer = new GLRenderer(gl, glext, glfbo);
289-
renderer.initialize();
290313
} else {
291314
throw new UnsupportedOperationException("Unsupported renderer: " + settings.getRenderer());
292315
}
@@ -296,19 +319,20 @@ protected void initContextFirstTime() {
296319
renderer.setMainFrameBufferSrgb(settings.isGammaCorrection());
297320
renderer.setLinearizeSrgbImages(settings.isGammaCorrection());
298321

299-
// Init input
300-
if (keyInput != null) {
301-
keyInput.initialize();
302-
}
322+
if (first) {
323+
// Init input
324+
if (keyInput != null) {
325+
keyInput.initialize();
326+
}
303327

304-
if (mouseInput != null) {
305-
mouseInput.initialize();
306-
}
328+
if (mouseInput != null) {
329+
mouseInput.initialize();
330+
}
307331

308-
if (joyInput != null) {
309-
joyInput.initialize();
332+
if (joyInput != null) {
333+
joyInput.initialize();
334+
}
310335
}
311-
312336
}
313337

314338
@SuppressWarnings("unchecked")

jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglDisplay.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ public void runLoop(){
185185
logger.log(Level.SEVERE, "Failed to set display settings!", ex);
186186
}
187187
listener.reshape(settings.getWidth(), settings.getHeight());
188+
if (renderable.get()) {
189+
reinitContext();
190+
} else {
191+
assert getType() == Type.Canvas;
192+
}
188193
logger.fine("Display restarted.");
189194
} else if (Display.wasResized()) {
190195
int newWidth = Display.getWidth();

0 commit comments

Comments
 (0)