{"record":{"id":"e995e70ed392f25d","repo":"BabylonJS/Babylon.js","slug":"could-not-optimize-blocktype-block-blocktype-be","errorCode":null,"errorMessage":"Could not optimize blockType ${block.blockType} because a texture2D() sampled something other than a uniform, which is unsupported","messagePattern":"Could not optimize blockType (.+?) because a texture2D\\(\\) sampled something other than a uniform, which is unsupported","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/smartFilters/src/optimization/smartFilterOptimizer.ts","lineNumber":777,"sourceCode":"        for (const sampler of renameWork.samplersToApplyAutoTo) {\r\n            code = this._applyAutoSampleStrategy(code, sampler);\r\n        }\r\n\r\n        for (const rename of renameWork.symbolRenames) {\r\n            code = code.replace(new RegExp(`(?<!\\\\w)${rename.from}(?!\\\\w)`, \"g\"), rename.to);\r\n        }\r\n\r\n        for (const swap of renameWork.sampleToFunctionCallSwaps) {\r\n            code = this._replaceSampleTextureWithFunctionCall(code, swap.from, swap.to);\r\n        }\r\n\r\n        for (const rename of renameWork.samplerRenames) {\r\n            code = this._replaceSampleTextureWithTexture2DCall(code, rename.from, rename.to);\r\n        }\r\n\r\n        // Ensure all __sampleTexture( instances were replaced, and error out if not\r\n        if (code.indexOf(\"__sampleTexture(\") > -1) {\r\n            throw new Error(`Could not optimize blockType ${block.blockType} because a texture2D() sampled something other than a uniform, which is unsupported`);\r\n        }\r\n\r\n        return code;\r\n    }\r\n\r\n    private _saveBlockStackState(): void {\r\n        this._savedBlockStack = this._blockStack.slice();\r\n        this._savedBlockToStackItem = new Map();\r\n\r\n        for (const [key, value] of this._blockToStackItem) {\r\n            value.inputsToConnectTo = value.inputsToConnectTo.slice();\r\n            this._savedBlockToStackItem.set(key, value);\r\n        }\r\n    }\r\n\r\n    private _restoreBlockStackState(): void {\r\n        this._blockStack.length = 0;\r\n        this._blockStack.push(...this._savedBlockStack);\r","sourceCodeStart":759,"sourceCodeEnd":795,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/smartFilters/src/optimization/smartFilterOptimizer.ts#L759-L795","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure every sampled texture in the block's GLSL comes from a Texture-typed input declared as a uniform.","Refactor code that samples from function parameters or dynamic sampler variables to sample the block's uniform texture inputs directly.","If multiple textures must be sampled, declare each as its own Texture input rather than indexing a sampler array.","Check that sampler rename wiring (inputs to uniform names) is intact for custom blocks."],"exampleFix":"// before (GLSL)\nvec4 sampleHelper(sampler2D tex, vec2 uv) { return texture2D(tex, uv); }\nvec4 c = sampleHelper(uTexture, uv); // becomes stuck __sampleTexture\n// after\nvec4 c = texture2D(uTexture, uv); // sample the uniform directly","handlingStrategy":"validation","validationCode":"// Pre-check optimized GLSL for unreplaced sampling helpers\nif (glslCode.includes(\"__sampleTexture(\")) {\n  throw new Error(\"Block GLSL samples a non-uniform texture; refactor before optimizing\");\n}","typeGuard":"null","tryCatchPattern":"try {\n  await SmartFilterOptimizer.OptimizeAsync(engine, filter, texture);\n} catch (e) {\n  if (e.message.includes(\"sampled something other than a uniform\")) {\n    // locate the sampling helper call in the block's GLSL and rewrite it to sample a uniform directly\n  } else throw e;\n}","preventionTips":["Only sample Texture-typed input uniforms in block GLSL; never sample function parameters or derived samplers.","Avoid sampler arrays and conditional sampler selection in optimizable shader code.","Pre-process custom GLSL to hoist texture2D calls on non-uniforms into the main body using uniform inputs.","Grep block shader code for __sampleTexture( after writing custom sampling helpers."],"tags":["optimization","glsl","texture-sampling","unsupported-pattern"],"backgroundTag":"unsupported-texture-sampling","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}