BabylonJS/Babylon.js · error

Shader program not found for block "${block.name}"!

Error message

Shader program not found for block "${block.name}"!

What it means

SmartFilterOptimizer._optimizeBlock needs the block's GLSL shader program to rewrite and inline it. It calls block.getShaderProgram(), and if that returns null/undefined it throws this error naming the block. A block that cannot produce its shader program cannot be optimized.

Source

Thrown at packages/dev/smartFilters/src/optimization/smartFilterOptimizer.ts:550

    }

    // Processes a block given one of its output connection point
    // Returns the name of the main function in the shader code
    private _optimizeBlock(optimizedBlock: OptimizedShaderBlock, outputConnectionPoint: ConnectionPoint, samplers: string[]): string {
        const block = outputConnectionPoint.ownerBlock;

        if (!(block instanceof ShaderBlock)) {
            throw `Unhandled block type! blockType=${block.blockType}`;
        }

        if (this._currentOutputTextureOptions === undefined) {
            this._currentOutputTextureOptions = block.outputTextureOptions;
        }

        // Sometimes getShaderProgram() does work, so only grab it once for efficiency
        const shaderProgram = block.getShaderProgram();
        if (!shaderProgram) {
            throw new Error(`Shader program not found for block "${block.name}"!`);
        }

        this._vertexShaderCode = this._vertexShaderCode ?? shaderProgram.vertex;

        // The operations we collect which we will apply to all functions of this block later
        const renameWork: RenameWork = {
            symbolRenames: [],
            samplerRenames: [],
            sampleToFunctionCallSwaps: [],
            samplersToApplyAutoTo: [],
        };

        // Generates a unique name for the fragment main function (if not already generated)
        const shaderFuncName = shaderProgram.fragment.mainFunctionName;

        let newShaderFuncName = this._blockToMainFunctionName.get(block);

        if (!newShaderFuncName) {

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Ensure the filter runtime is initialized (effects compiled) before running the optimizer.
  2. Check the block's getShaderProgram implementation/inputs — verify the shader code (vertex/fragment) is provided and valid.
  3. Confirm the block's runtime data exists in the runtime data map for the current runtime.
  4. Fix any earlier shader compile errors that leave the program unset.

Example fix

// before
await optimizer.run(); // effects not built yet -> null program
// after
await smartFilter.build(); // or attach the filter so effects compile first
await SmartFilterOptimizer.OptimizeAsync(engine, smartFilter, texture);
Defensive patterns

Strategy: validation

Validate before calling

for (const block of filter.allBlocks) {
  if (block.getShaderProgram && !block.getShaderProgram()) {
    throw new Error(`Block "${block.name}" has no shader program; build the filter first`);
  }
}

Type guard

function hasShaderProgram(b: BaseBlock): b is BaseBlock & { getShaderProgram(): IShaderProgram } {
  return !!(b as OptimizedBlock).getShaderProgram?.();
}

Try / catch

try {
  const rd = await SmartFilterOptimizer.OptimizeAsync(engine, filter, texture);
} catch (e) {
  if (e.message.startsWith("Shader program not found for block")) {
    await filter.build(); // ensure effects are compiled, then retry once
  } else throw e;
}

Prevention

When it happens

Trigger: getShaderProgram() returning null because the block's effect was never compiled (engine context missing, shader not yet built), the block is a custom block that failed to provide shader code, or the runtime data for the block is not initialized.

Common situations: Calling OptimizeAsync before the filter's effects were built; a custom block whose getShaderProgram depends on runtime state that isn't ready; shader compilation failing silently upstream so the program is null.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/f86903164355c7ec. Report an issue: GitHub.