BabylonJS/Babylon.js · error

Could not optimize blockType ${block.blockType} because a te

Error message

Could not optimize blockType ${block.blockType} because a texture2D() sampled something other than a uniform, which is unsupported

What it means

When optimizing, the optimizer replaces each block's __sampleTexture(...) helper (backed by uniform texture samplers) with direct texture2D() calls on remapped uniforms. _processFunction rewrites the code and then verifies no __sampleTexture( call remains; leftover calls mean a texture was sampled from something that is not a supported uniform sampler, which the optimizer cannot inline.

Source

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

        for (const sampler of renameWork.samplersToApplyAutoTo) {
            code = this._applyAutoSampleStrategy(code, sampler);
        }

        for (const rename of renameWork.symbolRenames) {
            code = code.replace(new RegExp(`(?<!\\w)${rename.from}(?!\\w)`, "g"), rename.to);
        }

        for (const swap of renameWork.sampleToFunctionCallSwaps) {
            code = this._replaceSampleTextureWithFunctionCall(code, swap.from, swap.to);
        }

        for (const rename of renameWork.samplerRenames) {
            code = this._replaceSampleTextureWithTexture2DCall(code, rename.from, rename.to);
        }

        // Ensure all __sampleTexture( instances were replaced, and error out if not
        if (code.indexOf("__sampleTexture(") > -1) {
            throw new Error(`Could not optimize blockType ${block.blockType} because a texture2D() sampled something other than a uniform, which is unsupported`);
        }

        return code;
    }

    private _saveBlockStackState(): void {
        this._savedBlockStack = this._blockStack.slice();
        this._savedBlockToStackItem = new Map();

        for (const [key, value] of this._blockToStackItem) {
            value.inputsToConnectTo = value.inputsToConnectTo.slice();
            this._savedBlockToStackItem.set(key, value);
        }
    }

    private _restoreBlockStackState(): void {
        this._blockStack.length = 0;
        this._blockStack.push(...this._savedBlockStack);

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Ensure every sampled texture in the block's GLSL comes from a Texture-typed input declared as a uniform.
  2. Refactor code that samples from function parameters or dynamic sampler variables to sample the block's uniform texture inputs directly.
  3. If multiple textures must be sampled, declare each as its own Texture input rather than indexing a sampler array.
  4. Check that sampler rename wiring (inputs to uniform names) is intact for custom blocks.

Example fix

// before (GLSL)
vec4 sampleHelper(sampler2D tex, vec2 uv) { return texture2D(tex, uv); }
vec4 c = sampleHelper(uTexture, uv); // becomes stuck __sampleTexture
// after
vec4 c = texture2D(uTexture, uv); // sample the uniform directly
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check optimized GLSL for unreplaced sampling helpers
if (glslCode.includes("__sampleTexture(")) {
  throw new Error("Block GLSL samples a non-uniform texture; refactor before optimizing");
}

Type guard

null

Try / catch

try {
  await SmartFilterOptimizer.OptimizeAsync(engine, filter, texture);
} catch (e) {
  if (e.message.includes("sampled something other than a uniform")) {
    // locate the sampling helper call in the block's GLSL and rewrite it to sample a uniform directly
  } else throw e;
}

Prevention

When it happens

Trigger: A block's GLSL samples a texture passed as a function argument, a dynamically chosen sampler, a sampler built from a non-uniform variable, or texture2D applied to a variable produced at runtime — leaving an unrewritten __sampleTexture( call.

Common situations: Custom shader blocks sampling arrays of textures or switching textures by condition; passing textures into helper functions; sampling from a render target texture obtained programmatically rather than declared as a block input uniform.

Related errors


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