#12213·pixijs

WebGL: `GlProgram._key` is derived from post-preprocessed source, so the program-data cache misses on identical shaders — and nothing ever calls `gl.deleteProgram`

Author: lsgmasa33Created Sep 17, 2026Updated Sep 17, 2026

Evidence tier: defect 1 has an executed reproduction (below); defect 2 is settled by one grep. Neither has a measured byte figure attached, and neither needs one. Both verified against the installed pixi.js 8.20.1 on 2026-09-18.

These are two defects, reported together because they compound: (1) makes the GL program cache miss on byte-identical shader source, and (2) means each miss leaks the WebGLProgram it compiles.


Defect 1 — _key is computed after an incrementing #define SHADER_NAME is injected

setProgramName (rendering/renderers/gl/shader/program/preprocessors/setProgramName.mjs) keeps module-level counters and appends an increasing suffix to the name it injects:

javascript
const fragmentNameCache = {};
const VertexNameCache = {};
function setProgramName(src, { name = `pixi-program` }, isFragment = true) {
  name = name.replace(/\s+/g, "-");
  name += isFragment ? "-fragment" : "-vertex";
  const nameCache = isFragment ? fragmentNameCache : VertexNameCache;
  if (nameCache[name]) {
    nameCache[name]++;
    name += `-${nameCache[name]}`;      // <-- pixi-program-fragment-2, -3, -4 …
  } else {
    nameCache[name] = 1;
  }
  if (src.indexOf("#define SHADER_NAME") !== -1) return src;
  const shaderName = `#define SHADER_NAME ${name}`;
  return `${shaderName}\n${src}`;
}

GlProgram's constructor runs every preprocessor and then derives the key (rendering/renderers/gl/shader/GlProgram.mjs, lines 47-55):

javascript
Object.keys(processes).forEach((processKey) => {
  const processOptions = preprocessorOptions[processKey];
  fragment = processes[processKey](fragment, processOptions, true);
  vertex   = processes[processKey](vertex,   processOptions, false);
});
this.fragment = fragment;
this.vertex   = vertex;
this._key = createIdFromString(`${this.vertex}:${this.fragment}`, "gl-program");   // line 55

So two GlPrograms built from byte-identical source get different _keys, because the source they hash differs by the injected name.

GlShaderSystem caches compiled programs on exactly that key (rendering/renderers/gl/shader/GlShaderSystem.mjs):

javascript
_getProgramData(program) {
  return this._programDataHash[program._key] || this._createProgramData(program);   // line 91
}
_createProgramData(program) {
  const key = program._key;
  this._programDataHash[key] = generateProgram(this._gl, program);                   // line 95
  return this._programDataHash[key];
}

A miss compiles and links a fresh WebGLProgram.

Minimal reproduction

No GL context needed — the key alone shows it. In a browser console the DOMAdapter line is unnecessary; it is here only so the snippet runs under plain Node.

javascript
import { GlProgram, DOMAdapter } from 'pixi.js';

// Node only: the constructor probes a GL context for the max fragment precision.
// Returning null makes it fall back to 'mediump'. Unrelated to the defect.
DOMAdapter.set({ createCanvas: () => ({ getContext: () => null }) });

const vertex   = 'void main(){ gl_Position = vec4(0.0); }';
const fragment = 'void main(){ gl_FragColor = vec4(1.0); }';

const a = new GlProgram({ vertex, fragment });
const b = new GlProgram({ vertex, fragment });
console.log(a._key, b._key, a._key === b._key);
console.log(a.fragment.split('\n')[0]);
console.log(b.fragment.split('\n')[0]);

Output on 8.20.1:

1 2 false
#define SHADER_NAME pixi-program-fragment
#define SHADER_NAME pixi-program-fragment-2

GlProgram.from({ vertex, fragment }) called twice returns the same key, as expected — see the scope note below.

⚠️ Precise scope — GlProgram.from() is NOT affected

This is worth stating because it bounds the report honestly. GlProgram.from() caches on the raw source before construction, so repeated from() calls return the same instance and the same _key:

javascript
static from(options) {
  const key = `${options.vertex}:${options.fragment}`;
  if (!programCache[key]) { programCache[key] = new _GlProgram(options); … }
  return programCache[key];
}

The defect bites wherever a GlProgram is constructed directly, which includes pixi's own high-shader path — compileHighShaderGlProgram (rendering/high-shader/compileHighShaderToProgram.mjs) ends in new GlProgram(...). That is how we hit it.

Suggested fix

Derive _key from the source before preprocessing, or exclude the injected SHADER_NAME line from the hash. The name is a debug aid; it should not participate in identity.

Workaround for users

Declare your own #define SHADER_NAME in both the vertex and the fragment source. The indexOf check above then returns src unchanged, so the key is stable.

⚠️ Both stages, not one — the preprocessor runs per stage and the key hashes vertex:fragment, so a SHADER_NAME in the fragment alone still gets an incrementing name injected into the vertex and the key still moves. Measured on 8.20.1:

SHADER_NAME in BOTH stages   -> keys 1, 1   (stable)
SHADER_NAME in FRAGMENT only -> keys 2, 3   (still moves)

Note also that the counter is bumped before the early return either way, so the workaround stabilises the key without stopping the counting.


Defect 2 — gl.deleteProgram has zero call sites in the library

A WebGLProgram created by generateProgram is never deleted. Not on program-data destruction, not on renderer teardown, not anywhere:

$ grep -rn "deleteProgram" node_modules/pixi.js/lib
(no matches)

Every sibling GL delete is called, which is what makes this look like an omission rather than a policy: deleteBuffer, deleteTexture, deleteVertexArray, deleteFramebuffer, deleteRenderbuffer and deleteShader are all present. deleteProgram alone is absent.

Deliberately stated as a contrast, not a count. An occurrence table does not survive scrutiny here: the published package ships a dual build (719 .mjs + 719 .js, plus source maps), so the same call is counted once, twice, or more depending on the glob — and src/ is not published at all, so a maintainer grepping this repo gets a different number again from anyone grepping node_modules. The zero is invariant under every one of those; the other rows are not.

The sharpest illustration is inside the very function that creates the program (rendering/renderers/gl/shader/program/generateProgram.mjs):

javascript
const webGLProgram = gl.createProgram();          // line 14
…
gl.deleteShader(glVertShader);                    // line 40
gl.deleteShader(glFragShader);                    // line 41

The two shaders are cleaned up correctly. The program they were linked into is not, ever.

For contrast, the WebGPU backend does have an explicit program-destruction path — it was being tuned as recently as #12201, where a shared cached GpuProgram was destroyed too eagerly. The WebGL backend has no such path at all, in either direction.

And GlProgramData.destroy() only clears JS fields — the WebGLProgram handle is dropped, which makes it unreachable from JS and undeletable:

javascript
destroy() {
  this.uniformData = null;
  this.uniformGroups = null;
  this.uniformDirtyGroups = null;
  this.uniformBlockBindings = null;
  this.program = null;        // the WebGLProgram, released without gl.deleteProgram()
}

Impact

On its own, bounded: an app with a fixed set of shaders compiles each once and leaks a fixed amount. Combined with defect 1 it is unbounded — every cache miss compiles a program that can never be freed, and defect 1 makes identical source miss.

Suggested fix

Call gl.deleteProgram(this.program) in GlProgramData.destroy(), and ensure GlShaderSystem.destroy()'s existing loop over _programDataHash reaches it (it already calls .destroy() on each entry).

No workaround exists

Unlike defect 1, application code has no hook here — GlProgramData is internal and the handle is nulled out of reach. Caching harder bounds the count; it frees nothing.

Environment

  • pixi.js 8.20.1 (re-verified 2026-09-18); also present in 8.19.0
  • WebGL renderer only — neither defect reaches the WebGPU backend, which has its own program cache and its own destruction path (setProgramName is not in that code path at all)
  • Found while diagnosing an iOS WebKit GPU-process jetsam kill (com.apple.WebKit.GPU, ~300 MB)