← Back to Blog Tech

libGDX Shader Caching: Why Overriding canRender Crashes Your Game

If you're customizing libGDX's 3D pipeline, you'll eventually outgrow the built-in DefaultShader. You take the default .vert and .frag, subclass DefaultShader, pass in your GLSL, and expect it to work.

Then you get a NullPointerException deep in the framework:

java.lang.NullPointerException: Attempt to read from field 'com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute.textureDescription' on a null object reference
    at com.badlogic.gdx.graphics.g3d.shaders.DefaultShader$Setters$14.set(DefaultShader.java:237)
    at com.badlogic.gdx.graphics.g3d.shaders.BaseShader.render(BaseShader.java:262)


The strange part: disable your custom shader and use stock libGDX rendering, and the crash vanishes and textures draw fine.

It's not your GLSL. It's how libGDX caches and sorts shaders, and the trap of overriding canRender.


How ModelBatch and the shader contract work

To see the crash you have to understand how libGDX avoids costly GPU state changes.

When you hand a set of Renderable objects to ModelBatch, it doesn't draw them one at a time. It passes them to a ShaderProvider. The provider inspects a model's attributes (bones? diffuse texture? vertex colors?) and returns a shader that matches them.

Under the hood, DefaultShader keeps a list of fixed Setters that map Java data to GLSL uniforms. For instance Setter #14 is the one that binds TextureAttribute.Diffuse.

When a shader is built, it records a bitmask of required attributes based on the first model it sees.


The trap: forcing canRender

When writing a wrapper or custom shader, it's tempting to short-circuit setup like this:

// THE TRAP CODE
class GameShader(renderable: Renderable, config: Config) : DefaultShader(renderable, config) {
    
    override fun canRender(instance: Renderable): Boolean = true // DO NOT DO THIS
    
    override fun compareTo(other: Shader?): Int = 0 // DO NOT DO THIS
}


Returning true unconditionally from canRender hijacks the ModelBatch pipeline. You're telling the framework: "whatever this model is, my shader can handle it."

How the crash happens:

  1. First draw. You render something with no texture (a debug box, an untextured UI part). Your GameShader compiles without the texture-related flags set.

  2. Caching. Because canRender always returns true, ModelBatch caches that instance as the answer for everything.

  3. The mismatch. Next, ModelBatch tries to draw a textured asset, like a row of trees. It asks the cached shader if it can render it. Your override says true.

  4. Crash. The textured model goes through the pipeline. The engine sees a texture and runs Setter #14. But this instance was initialized for a plain color block, so the internal texture descriptor is null. Setter #14 reads a null reference and the engine dies.


The correct way

To avoid broken state, keep the framework's matching and sorting logic. If you subclass DefaultShader, don't override canRender or compareTo unless you're filtering bitmasks yourself.

Here's a clean custom shader that uses its own GLSL:

package io.zerodigest.crycroc.screens

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.g3d.Attributes
import com.badlogic.gdx.graphics.g3d.Renderable
import com.badlogic.gdx.graphics.g3d.Shader
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader
import com.badlogic.gdx.graphics.g3d.utils.RenderContext

class GameShader(
    renderable: Renderable,
    config: Config
) : DefaultShader(renderable, config) {

    companion object {
        fun create(renderable: Renderable): GameShader {
            // Load your custom GLSL strings directly from your assets folder
            val vert = Gdx.files.internal("shaders/game.vert").readString()
            val frag = Gdx.files.internal("shaders/game.frag").readString()

            // Pass your source code cleanly into the standard Config constructor
            val config = Config(vert, frag)

            return GameShader(renderable, config)
        }
    }

    // Leave these methods to the superclass! 
    // DefaultShader already contains complex bitmask matching to safely validate rendering capabilities.
    override fun canRender(renderable: Renderable): Boolean = super.canRender(renderable)

    override fun compareTo(other: Shader?): Int {
        if (other == null) return -1
        if (other === this) return 0
        return super.compareTo(other)
    }
}


In short

Shortcuts aren't free. In something as stateful as a 3D renderer, stubbing out validation like canRender() breaks the engine's internal contract. Let the base class check the renderable's attributes, and let your ShaderProvider build and cache separate instances for textured versus untextured geometry.