# core/graphic/shaderwarmer (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/graphic/shaderwarmer/

Shader pre-linking.

## ShaderWarmer

Shader pre-linking.
- ShaderProgram queries the result right after linking, so the main thread stalls until linking finishes. Building several shaders at once on first draw makes that frame stutter.
- At startup (during the logo, loading), linking the same sources in the background with KHR_parallel_shader_compile leaves them in the browser's program cache, so later when ShaderProgram builds the same sources it finishes quickly.
- Does nothing if the extension is absent. (Starting links without the extension can make later draw commands wait for the link and stall instead)
- Does not query link results, and deletes the programs and shaders once linked. (The cache remains, even if a source is wrong no error is raised here, the ShaderProgram at draw time reports it)
- Does not touch draw state (bound program, buffers, textures).
- Usage: new ShaderWarmer(webGL2RenderingContext, [{ vertexShader, fragmentShader, attributeLocationTable }]) then update() every frame.
  Attribute locations must be given the same as those passed to ShaderProgram to hit the cache.

Example: Prelink shaders at startup

```js
import { Engine, EngineConfiguration, Scene, Vector2, ShaderWarmer } from "./build/vanilla.js";

const VERTEX_SHADER_SOURCE = `#version 300 es
in vec2 vertexPosition;
void main() {
	gl_Position = vec4(vertexPosition, 0.0, 1.0);
}
`;
const FRAGMENT_SHADER_SOURCE = `#version 300 es
precision highp float;
out vec4 outputColor;
void main() {
	outputColor = vec4(1.0);
}
`;

class MainScene extends Scene {
	initialize(engine) {
		const graphic = engine.getGraphic();
		const webGL2RenderingContext = graphic.getWebGL2RenderingContext();
		const programSources = [{ vertexShader: VERTEX_SHADER_SOURCE, fragmentShader: FRAGMENT_SHADER_SOURCE, attributeLocationTable: { vertexPosition: 0 } }];
		this.shaderWarmer = new ShaderWarmer(webGL2RenderingContext, programSources);
		const isSupported = this.shaderWarmer.isSupported();
		console.log(isSupported);
	}

	tick(timeDelta) {
		super.tick(timeDelta);
		if (!this.shaderWarmer.isFinished()) {
			this.shaderWarmer.update();
		}
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor(webGL2RenderingContext, programSources)` (constructor, since 0.4.0): Constructor. (If the extension exists, compiles all sources and starts linking)
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
  - `programSources` Array<object>: An array of { vertexShader, fragmentShader, attributeLocationTable }. (Those used first come first)
- `linkProgram(programSource)` (method, since 0.4.0): Compiles one and starts linking. (Same order as ShaderProgram, attribute locations must also be bound the same to hit the cache)
  - `programSource` object: { vertexShader, fragmentShader, attributeLocationTable }
  - returns object: { program, vertexShader, fragmentShader }
- `update()` (method, since 0.4.0): Every frame. (Deletes the ones whose linking finished, does not wait when asking whether it finished)
- `isFinished()` (method, since 0.4.0): Returns whether everything has been linked. (true from the start if not done because the extension is absent)
  - returns boolean: True if all are linked.
- `isSupported()` (method, since 0.4.0): Returns whether the extension can be used. (If false, pre-linking is not done)
  - returns boolean: True if the extension is available.
- `getWebGL2RenderingContext()` (method, since 0.4.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getParallelExtension()` (method, since 0.4.0): Returns the parallel link extension. (null if none)
  - returns object | null: The KHR_parallel_shader_compile extension. (null if none)
