Async loading of assets, still stuttering on attach

I’m loading almost all my resources (models and textures) in background thread, creating ready to use models and doing only sceneNode.attach(model) in enqueued action on main jme3 thread. Still, there seems to be considerable amount of stuttering when models are finally attached. From unscientific profiling, it seems that time is spent somewhere in uploading buffers, generating mipmaps, compiling shaders, etc.
Is it supposed to take so much time (I have 2-3 seconds during which just few frames are rendered, when attaching 10 or so models)? Is there any way to do some of these actions from other thread as well, to prepare the buffers, so final attach would be just about updating transforms and final binding of variables?

I’ve never tried it, but it is possible to create another OpenGL context on another thread, load all resources using that, and then our main render thread can use these shared resources without it being blocked

You can try material.preload() for each model. It won’t upload the buffers, but it uploads textures, compile shaders and so on.
I guess you can use it from another thread as it doesn’t modify the scene graph.

Out of curiosity, did either of these suggestions help?

I was unaware of Material.preload() … that’s a good thing to know :wink:

And the OpenGL context on a separate thread is an interesting idea as well. Just to be clear on this, if the resources are loaded to the GPU, both would have access to them? They are not tied to the context in some way? Does this mean that if I have a game running in the background that is using OpenGL, I have access to those resources… say, for instance by texture slot index, etc? Assuming that they are loaded into the slot index still…

Yes, be sure to make the first contesxt a shared one, then this should work. (And would be a great contribution)

http://www.opengl.org/wiki/OpenGL_Context

Using preload from different thread doesn’t work out of the box

[java]
Exception in thread “pool-3-thread-19” java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1383)
at com.jme3.renderer.lwjgl.LwjglRenderer.updateTexImageData(LwjglRenderer.java:1822)
at com.jme3.renderer.lwjgl.LwjglRenderer.setTexture(LwjglRenderer.java:1948)
at com.jme3.material.Material.preload(Material.java:976)
at net.virtualmat.locator.async.ModelLoader$4.visit(ModelLoader.java:148)
at com.jme3.scene.Spatial.breadthFirstTraversal(Spatial.java:1509)
at net.virtualmat.locator.async.ModelLoader.tryLoadingModel(ModelLoader.java:152)
at net.virtualmat.locator.async.ModelLoader.access$0(ModelLoader.java:141)
at net.virtualmat.locator.async.ModelLoader$3.run(ModelLoader.java:135)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
[/java]

I tried to created shared context with

[java]
val sd = new SharedDrawable(Display.getDrawable());
sd.makeCurrent();
[/java]
(after main windows is already rendering for some time) - but it fails with

[java]
org.lwjgl.LWJGLException: Could not create context (WGL_ARB_create_context)
at org.lwjgl.opengl.WindowsContextImplementation.nCreate(Native Method)
at org.lwjgl.opengl.WindowsContextImplementation.create(WindowsContextImplementation.java:50)
at org.lwjgl.opengl.ContextGL.<init>(ContextGL.java:132)
at org.lwjgl.opengl.DrawableGL.createSharedContext(DrawableGL.java:77)
at org.lwjgl.opengl.DrawableGL.createSharedContext(DrawableGL.java:41)
at org.lwjgl.opengl.SharedDrawable.<init>(SharedDrawable.java:50)
at net.virtualmat.locator.async.ModelLoader.tryLoadingModel(ModelLoader.java:151)
at net.virtualmat.locator.async.ModelLoader.access$0(ModelLoader.java:146)
at net.virtualmat.locator.async.ModelLoader$3.run(ModelLoader.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
… 2 more
[/java]

Seems to be a common problem:

http://lwjgl.org/forum/index.php?topic=4069.0;wap2

I’ll try that on my ATI machine in few days to see if it is nvidia specific, as some people suggest in the threads.

One of the suggestions was saying something about specifying at least 3.2 context. Unfortunately, shared drawable has no way to pass attributes…

Well I can at lest confirm that that code part works perfectly fine under linux. (I just dumped it into the simpleInit)

deleted.

@Empire Phoenix said: Well I can at lest confirm that that code part works perfectly fine under linux. (I just dumped it into the simpleInit)

That is interesting. It also works on my PC. Of course, for it to make sense, you need to do it in different thread - and then it fails. But you gave me an idea - I have created SharedContext in jme3 render thread and only called makeCurrent in worker thread. This seems to not crash immediately, but rather with more interesting exception like
[java]
Exception in thread “pool-3-thread-1” java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:540)
at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:253)
at org.lwjgl.BufferChecks.checkNullTerminated(BufferChecks.java:66)
at org.lwjgl.opengl.GL20.glGetUniformLocation(GL20.java:608)
at com.jme3.renderer.lwjgl.LwjglRenderer.updateUniformLocation(LwjglRenderer.java:792)
at com.jme3.renderer.lwjgl.LwjglRenderer.updateUniform(LwjglRenderer.java:829)
at com.jme3.renderer.lwjgl.LwjglRenderer.updateShaderUniforms(LwjglRenderer.java:924)
at com.jme3.renderer.lwjgl.LwjglRenderer.setShader(LwjglRenderer.java:1131)
at com.jme3.material.Material.preload(Material.java:988)
at net.virtualmat.locator.async.ModelLoader$4.visit(ModelLoader.java:167)
at com.jme3.scene.Spatial.breadthFirstTraversal(Spatial.java:1509)
at net.virtualmat.locator.async.ModelLoader.tryLoadingModel(ModelLoader.java:171)
at net.virtualmat.locator.async.ModelLoader.access$0(ModelLoader.java:155)
at net.virtualmat.locator.async.ModelLoader$3.run(ModelLoader.java:145)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
[/java]

I’ll investigate more, but it seems that way to go is to call new SharedDrawable(Display.getDrawable()); in main render thread and only sd.makeCurrent(); in worked thread.

Update:
Sometimes it works, sometimes fails with random exceptions like
[java]
java.lang.IllegalStateException: Framebuffer has erronous attachment.
at com.jme3.renderer.lwjgl.LwjglRenderer.checkFrameBufferError(LwjglRenderer.java:1339) ~[bin/:na]
at com.jme3.renderer.lwjgl.LwjglRenderer.setFrameBuffer(LwjglRenderer.java:1618) ~[bin/:na]
at com.jme3.shadow.AbstractShadowRenderer.renderShadowMap(AbstractShadowRenderer.java:396) ~[bin/:na]
at com.jme3.shadow.AbstractShadowRenderer.postQueue(AbstractShadowRenderer.java:372) ~[bin/:na]
at com.jme3.shadow.AbstractShadowFilter.postQueue(AbstractShadowFilter.java:105) ~[bin/:na]
at com.jme3.post.FilterPostProcessor.postQueue(FilterPostProcessor.java:217) ~[bin/:na]
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:979) ~[bin/:na]
at com.jme3.renderer.RenderManager.render(RenderManager.java:1029) ~[bin/:na]
at net.virtualmat.gui.VmatGui.update(VmatGui.java:355) ~[bin/:na]
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151) ~[bin/:na]
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:194) ~[bin/:na]
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228) ~[bin/:na]
at java.lang.Thread.run(Thread.java:744) ~[na:1.8.0-ea]
[/java]

There might be a lot of issues around that - probably more than I’m able to solve atm with trial and error approach :wink:

Well but it is supposed to work at least, many games that use opengl use shared contexts.

What might be a reason, is the main context of jme shared?

Here maybeet his helps, For a test I would suggest to remove jme from the tests (because "You should call wglShareLists as soon as possible, meaning before you create any resources. "), and just try to start two empty contexts that are shared. And only after that works implement it baack to jme.

http://www.opengl.org/wiki/OpenGL_and_multithreading

@abies said: I'll investigate more, but it seems that way to go is to call new SharedDrawable(Display.getDrawable()); in main render thread and only sd.makeCurrent(); in worked thread.

Yes that looks like it is the correct way to do it.

There is a LWJGL test here, that loads Textures on a background context.

Hi.

Below is a modified TerrainGridAlphaMapTest.java. You need this file “http://jmonkeyengine.googlecode.com/files/TerrainGridTestData.zip” to run the program. What the modification does:

[java]
this.terrain = new TerrainGrid(“terrain”, 33, 257, new ImageTileLoader(assetManager, new Namer() {

        public String getName(int x, int y) {
            return "TerrainAlphaTest/terrain_" + x + "_" + y + ".png";
        }
    }));

[/java]

Is to load .png as opposed to using a fractal. When the required .png is not found then it uses a plain height map with no elevations.

What I’m trying to say is that you should look at all the classes involved in this sample. Because it runs smoothly, which means that the stuttering you experience from dynamically loading models in not a JMonkey limitation as it has been solved before, like to make this sample.

[java]
package co.pixelapp.terrain;

import com.jme3.app.SimpleApplication;
import com.jme3.app.state.ScreenshotAppState;
import com.jme3.asset.plugins.HttpZipLocator;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.Arrow;
import com.jme3.terrain.geomipmap.TerrainGrid;
import com.jme3.terrain.geomipmap.TerrainGridListener;
import com.jme3.terrain.geomipmap.TerrainGridLodControl;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.geomipmap.grid.FractalTileLoader;
import com.jme3.terrain.geomipmap.grid.ImageTileLoader;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
import com.jme3.terrain.heightmap.Namer;
import com.jme3.terrain.noise.ShaderUtils;
import com.jme3.terrain.noise.basis.FilteredBasis;
import com.jme3.terrain.noise.filter.IterativeFilter;
import com.jme3.terrain.noise.filter.OptimizedErode;
import com.jme3.terrain.noise.filter.PerturbFilter;
import com.jme3.terrain.noise.filter.SmoothFilter;
import com.jme3.terrain.noise.fractal.FractalSum;
import com.jme3.terrain.noise.modulator.NoiseModulator;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import java.io.File;

public class TerrainGridAlphaMapTest extends SimpleApplication {

private TerrainGrid terrain;
private float grassScale = 64;
private float dirtScale = 16;
private float rockScale = 128;
private boolean usePhysics = true;

public static void main(final String[] args) {
    TerrainGridAlphaMapTest app = new TerrainGridAlphaMapTest();
    app.start();
}
private CharacterControl player3;
private FractalSum base;
private PerturbFilter perturb;
private OptimizedErode therm;
private SmoothFilter smooth;
private IterativeFilter iterate;
private Material material;
private Material matWire;

@Override
public void simpleInitApp() {
    DirectionalLight sun = new DirectionalLight();
    sun.setColor(ColorRGBA.White);
    sun.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
    rootNode.addLight(sun);

    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(1.3f));
    rootNode.addLight(al);

    File file = new File("TerrainGridTestData.zip");
    if (!file.exists()) {
        assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/TerrainGridTestData.zip", HttpZipLocator.class);
    } else {
        assetManager.registerLocator("TerrainGridTestData.zip", ZipLocator.class);
    }

    this.flyCam.setMoveSpeed(100f);
    ScreenshotAppState state = new ScreenshotAppState();
    this.stateManager.attach(state);

    // TERRAIN TEXTURE material
    material = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
    material.setBoolean("useTriPlanarMapping", false);
    //material.setBoolean("isTerrainGrid", true);
    material.setFloat("Shininess", 0.0f);

    // GRASS texture
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    material.setTexture("DiffuseMap", grass);
    material.setFloat("DiffuseMap_0_scale", grassScale);

    // DIRT texture
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    material.setTexture("DiffuseMap_1", dirt);
    material.setFloat("DiffuseMap_1_scale", dirtScale);

    // ROCK texture
    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    material.setTexture("DiffuseMap_2", rock);
    material.setFloat("DiffuseMap_2_scale", rockScale);

    // WIREFRAME material
    matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWire.getAdditionalRenderState().setWireframe(true);
    matWire.setColor("Color", ColorRGBA.Green);

    this.base = new FractalSum();
    this.base.setRoughness(0.7f);
    this.base.setFrequency(1.0f);
    this.base.setAmplitude(1.0f);
    this.base.setLacunarity(2.12f);
    this.base.setOctaves(8);
    this.base.setScale(0.02125f);
    this.base.addModulator(new NoiseModulator() {

        @Override
        public float value(float... in) {
            return ShaderUtils.clamp(in[0] * 0.5f + 0.5f, 0, 1);
        }
    });

    FilteredBasis ground = new FilteredBasis(this.base);

    this.perturb = new PerturbFilter();
    this.perturb.setMagnitude(0.119f);

    this.therm = new OptimizedErode();
    this.therm.setRadius(5);
    this.therm.setTalus(0.011f);

    this.smooth = new SmoothFilter();
    this.smooth.setRadius(1);
    this.smooth.setEffect(0.7f);

    this.iterate = new IterativeFilter();
    this.iterate.addPreFilter(this.perturb);
    this.iterate.addPostFilter(this.smooth);
    this.iterate.setFilter(this.therm);
    this.iterate.setIterations(1);

    ground.addPreFilter(this.iterate);

    this.terrain = new TerrainGrid("terrain", 33, 257, new ImageTileLoader(assetManager, new Namer() {

        public String getName(int x, int y) {
            return "TerrainAlphaTest/terrain_" + x + "_" + y + ".png";
        }
    }));
    
    this.terrain.setMaterial(this.material);

    this.terrain.setLocalTranslation(0, 0, 0);
    this.terrain.setLocalScale(2f, 1f, 2f);
    this.rootNode.attachChild(this.terrain);

    TerrainLodControl control = new TerrainGridLodControl(this.terrain, this.getCamera());
    control.setLodCalculator( new DistanceLodCalculator(33, 2.7f) ); // patch size, and a multiplier
    this.terrain.addControl(control);

    final BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);


    this.getCamera().setLocation(new Vector3f(0, 256, 0));

    this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));

    if (usePhysics) {
        CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(0.5f, 1.8f, 1);
        player3 = new CharacterControl(capsuleShape, 0.5f);
        player3.setJumpSpeed(20);
        player3.setFallSpeed(10);
        player3.setGravity(10);

        player3.setPhysicsLocation(new Vector3f(cam.getLocation().x, 256, cam.getLocation().z));

        bulletAppState.getPhysicsSpace().add(player3);

    }
    terrain.addListener(new TerrainGridListener() {

        public void gridMoved(Vector3f newCenter) {
        }

        public void tileAttached(Vector3f cell, TerrainQuad quad) {
            Texture alpha = null;
            try {
                alpha = assetManager.loadTexture("TerrainAlphaTest/alpha_" + (int)cell.x+ "_" + (int)cell.z + ".png");
            } catch (Exception e) {
                alpha = assetManager.loadTexture("TerrainAlphaTest/alpha_default.png");
            }
            quad.getMaterial().setTexture("AlphaMap", alpha);
            if (usePhysics) {
                quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));
                bulletAppState.getPhysicsSpace().add(quad);
            }
            updateMarkerElevations();
        }

        public void tileDetached(Vector3f cell, TerrainQuad quad) {
            if (usePhysics) {
                if (quad.getControl(RigidBodyControl.class) != null) {
                    bulletAppState.getPhysicsSpace().remove(quad);
                    quad.removeControl(RigidBodyControl.class);
                }
            }
            updateMarkerElevations();
        }
    });
    
    this.initKeys();

    markers = new Node();
    rootNode.attachChild(markers);
    createMarkerPoints(1);
}

Node markers;


private void createMarkerPoints(float count) {
    Node center = createAxisMarker(10);
    markers.attachChild(center);
    
    float xS = (count-1)*terrain.getTerrainSize() - (terrain.getTerrainSize()/2);
    float zS = (count-1)*terrain.getTerrainSize() - (terrain.getTerrainSize()/2);
    float xSi = xS;
    float zSi = zS;
    for (int x=0; x&lt;count*2; x++) {
        for (int z=0; z&lt;count*2; z++) {
            Node m = createAxisMarker(5);
            m.setLocalTranslation(xSi, 0, zSi);
            markers.attachChild(m);
            zSi += terrain.getTerrainSize();
        }
        zSi = zS;
        xSi += terrain.getTerrainSize();
    }
}

private void updateMarkerElevations() {
    for (Spatial s : markers.getChildren()) {
        float h = terrain.getHeight(new Vector2f(s.getLocalTranslation().x, s.getLocalTranslation().z));
        s.setLocalTranslation(s.getLocalTranslation().x, h+1, s.getLocalTranslation().z);
    }
}

private void initKeys() {
    // You can map one or several inputs to one named action
    this.inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
    this.inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
    this.inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
    this.inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
    this.inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
    this.inputManager.addListener(this.actionListener, "Lefts");
    this.inputManager.addListener(this.actionListener, "Rights");
    this.inputManager.addListener(this.actionListener, "Ups");
    this.inputManager.addListener(this.actionListener, "Downs");
    this.inputManager.addListener(this.actionListener, "Jumps");
}
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private final ActionListener actionListener = new ActionListener() {

    @Override
    public void onAction(final String name, final boolean keyPressed, final float tpf) {
        if (name.equals("Lefts")) {
            if (keyPressed) {
                TerrainGridAlphaMapTest.this.left = true;
            } else {
                TerrainGridAlphaMapTest.this.left = false;
            }
        } else if (name.equals("Rights")) {
            if (keyPressed) {
                TerrainGridAlphaMapTest.this.right = true;
            } else {
                TerrainGridAlphaMapTest.this.right = false;
            }
        } else if (name.equals("Ups")) {
            if (keyPressed) {
                TerrainGridAlphaMapTest.this.up = true;
            } else {
                TerrainGridAlphaMapTest.this.up = false;
            }
        } else if (name.equals("Downs")) {
            if (keyPressed) {
                TerrainGridAlphaMapTest.this.down = true;
            } else {
                TerrainGridAlphaMapTest.this.down = false;
            }
        } else if (name.equals("Jumps")) {
            TerrainGridAlphaMapTest.this.player3.jump();
        }
    }
};
private final Vector3f walkDirection = new Vector3f();

@Override
public void simpleUpdate(final float tpf) {
    Vector3f camDir = this.cam.getDirection().clone().multLocal(0.6f);
    Vector3f camLeft = this.cam.getLeft().clone().multLocal(0.4f);
    this.walkDirection.set(0, 0, 0);
    if (this.left) {
        this.walkDirection.addLocal(camLeft);
    }
    if (this.right) {
        this.walkDirection.addLocal(camLeft.negate());
    }
    if (this.up) {
        this.walkDirection.addLocal(camDir);
    }
    if (this.down) {
        this.walkDirection.addLocal(camDir.negate());
    }

    if (usePhysics) {
        this.player3.setWalkDirection(this.walkDirection);
        this.cam.setLocation(this.player3.getPhysicsLocation());
    }
}

protected Node createAxisMarker(float arrowSize) {

    Material redMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    redMat.getAdditionalRenderState().setWireframe(true);
    redMat.setColor("Color", ColorRGBA.Red);
    
    Material greenMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    greenMat.getAdditionalRenderState().setWireframe(true);
    greenMat.setColor("Color", ColorRGBA.Green);
    
    Material blueMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    blueMat.getAdditionalRenderState().setWireframe(true);
    blueMat.setColor("Color", ColorRGBA.Blue);

    Node axis = new Node();

    // create arrows
    Geometry arrowX = new Geometry("arrowX", new Arrow(new Vector3f(arrowSize, 0, 0)));
    arrowX.setMaterial(redMat);
    Geometry arrowY = new Geometry("arrowY", new Arrow(new Vector3f(0, arrowSize, 0)));
    arrowY.setMaterial(greenMat);
    Geometry arrowZ = new Geometry("arrowZ", new Arrow(new Vector3f(0, 0, arrowSize)));
    arrowZ.setMaterial(blueMat);
    axis.attachChild(arrowX);
    axis.attachChild(arrowY);
    axis.attachChild(arrowZ);

    //axis.setModelBound(new BoundingBox());
    return axis;
}

}

[/java]