[SOLVED] Shader compile error with InstancedNode, Lighting material and LinearFog

Hi everyone,

I’ve been really enjoying experimenting with JMonkeyEngine, and I think I may have found a bug with 3.3.2-stable. I have an InstancedNode with a Box Geometry, with the Lighting material, and LinearFog. At render time, I get:
error C1503: undefined variable "Use_the_instancing_functions_for_this"

SSCE: (basically the JME3 TestInstanceNodeWithLight class, plus 3 lines to add the fog)

import com.jme3.app.SimpleApplication;
import com.jme3.light.PointLight;
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.instancing.InstancedNode;
import com.jme3.scene.shape.Box;

public class TestInstanceNodeWithLight extends SimpleApplication {
    // Try to test with different offset
    private static float offset = 12;

    public static void main(String[] args) {
        TestInstanceNodeWithLight app = new TestInstanceNodeWithLight();
        app.start();
    }

    Geometry box;
    PointLight pointLight;

    @Override
    public void simpleInitApp() {
        InstancedNode instancedNode = new InstancedNode("testInstancedNode");
        rootNode.attachChild(instancedNode);

        box = new Geometry("Box", new Box(0.5f, 0.5f, 0.5f));
        Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        material.setBoolean("UseInstancing", true);
        material.setColor("Diffuse", ColorRGBA.Red);
        material.setBoolean("UseMaterialColors", true);

        material.setBoolean("UseFog", true);
        material.setColor("FogColor", new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
        material.setVector2("LinearFog", new Vector2f(200f, 400f));


        box.setMaterial(material);

        instancedNode.attachChild(box);
        instancedNode.instance();

        pointLight = new PointLight();
        pointLight.setColor(ColorRGBA.White);
        pointLight.setRadius(10f);
        rootNode.addLight(pointLight);

        box.setLocalTranslation(new Vector3f(offset, 0, 0));
        pointLight.setPosition(new Vector3f(offset - 3f, 0, 0));

        cam.setLocation(new Vector3f(offset - 5f, 0, 0));
        cam.lookAtDirection(Vector3f.UNIT_X, Vector3f.UNIT_Y);
        flyCam.setMoveSpeed(30);
    }

}

Error message:
Uncaught exception thrown in Thread[jME3 Main,5,main]
RendererException: compile error in: ShaderSource[name=Common/MatDefs/Light/Lighting.vert, defines, type=Vertex, language=GLSL150]
0(762) : error C1503: undefined variable “Use_the_instancing_functions_for_this”

Full stack trace:

Jan 07, 2021 7:53:59 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.3.2-stable
 * Branch: HEAD
 * Git Hash: 1a05e3f
 * Build Date: 2020-04-27
Jan 07, 2021 7:54:00 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: LWJGL 2.9.3 context running on thread jME3 Main
 * Graphics Adapter: nvd3dumx,nvwgf2umx,nvwgf2umx
 * Driver Version: 27.21.14.5751
 * Scaling Factor: 1
Jan 07, 2021 7:54:00 PM com.jme3.renderer.opengl.GLRenderer loadCapabilitiesCommon
INFO: OpenGL Renderer Information
 * Vendor: NVIDIA Corporation
 * Renderer: GeForce GTX 1060 6GB/PCIe/SSE2
 * OpenGL Version: 4.6.0 NVIDIA 457.51
 * GLSL Version: 4.60 NVIDIA
 * Profile: Compatibility
Jan 07, 2021 7:54:00 PM com.jme3.asset.AssetConfig loadText
WARNING: Cannot find loader com.jme3.scene.plugins.blender.BlenderModelLoader
Jan 07, 2021 7:54:00 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio Renderer Information
 * Device: OpenAL Soft
 * Vendor: OpenAL Community
 * Renderer: OpenAL Soft
 * Version: 1.1 ALSOFT 1.15.1
 * Supported channels: 64
 * ALC extensions: ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context ALC_SOFT_loopback
 * AL extensions: AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points AL_SOFT_source_latency
Jan 07, 2021 7:54:00 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
WARNING: Pausing audio device not supported.
Jan 07, 2021 7:54:00 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio effect extension version: 1.0
Jan 07, 2021 7:54:00 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio max auxiliary sends: 4
Jan 07, 2021 7:54:00 PM com.jme3.renderer.opengl.GLRenderer updateShaderSourceData
WARNING: Bad compile of:

<Duncan: shader code removed to make this fit into a post>

Jan 07, 2021 7:54:00 PM com.jme3.app.LegacyApplication handleError
SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
com.jme3.renderer.RendererException: compile error in: ShaderSource[name=Common/MatDefs/Light/Lighting.vert, defines, type=Vertex, language=GLSL150]
0(762) : error C1503: undefined variable "Use_the_instancing_functions_for_this"

	at com.jme3.renderer.opengl.GLRenderer.updateShaderSourceData(GLRenderer.java:1485)
	at com.jme3.renderer.opengl.GLRenderer.updateShaderData(GLRenderer.java:1512)
	at com.jme3.renderer.opengl.GLRenderer.setShader(GLRenderer.java:1576)
	at com.jme3.material.logic.MultiPassLightingLogic.render(MultiPassLightingLogic.java:158)
	at com.jme3.material.Technique.render(Technique.java:166)
	at com.jme3.material.Material.render(Material.java:1026)
	at com.jme3.renderer.RenderManager.renderGeometry(RenderManager.java:614)
	at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:266)
	at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:305)
	at com.jme3.renderer.RenderManager.renderViewPortQueues(RenderManager.java:877)
	at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:779)
	at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1108)
	at com.jme3.renderer.RenderManager.render(RenderManager.java:1158)
	at com.jme3.app.SimpleApplication.update(SimpleApplication.java:272)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:153)
	at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:193)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:234)
	at java.lang.Thread.run(Thread.java:745)

As I say, this is happening on 3.3.2-stable, which I think is the latest at time of writing. Sadly, my vertex shader knowledge isn’t up to the task of understanding what’s going on enough to be able to propose a fix.

Please could you tell me whether we should expect LinearFog to work with an InstancedNode and Lighting, or whether I’m doing something obviously wrong?

Thanks,

Duncan.

1 Like

Looks like it’s something that got missed in an update ~6-7 years ago.

It appears that:

should instead be:

fog_distance = distance(g_CameraPosition, (TransformWorld(modelSpacePos)).xyz);
5 Likes

Yep, that fixes it for me. Thank you for such a quick response!

Is this something that could be added to the codebase for 3.3.3 ?

2 Likes

@duncanj can you please submit a PR with the proposed change, on the JME GitHub page?

1 Like

Done: Fix for lighting vertex shader when used with Fog and InstancedNode. by duncanj · Pull Request #1456 · jMonkeyEngine/jmonkeyengine · GitHub

3 Likes

Thanks

1 Like

To be precise this is from a 2019 commit by Jay

1 Like

This PR is now merged. Thanks again to sailsman63 / Lucas for the actual fix.

4 Likes

Seems the single-pass lighting is also affected by this. @duncanj can you please submit one for SPLighting.vert as well?

1 Like

Thanks - PR raised: Fix for lighting vertex shader when used with Fog and InstancedNode. by duncanj · Pull Request #1458 · jMonkeyEngine/jmonkeyengine · GitHub

1 Like