# core/shaderwarmer (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/shaderwarmer/

Shader prelinking. (ShaderWarmer of vanilla.js core/graphic/shaderwarmer.js)
- ShaderProgram queries the result right after linking, so it stalls until linking finishes. Building several shaders at once on the first draw makes that frame hitch.
- Linking the same sources in the background with KHR_parallel_shader_compile at startup (during the logo, loading) leaves them in the driver's (on the web, the browser's) program cache,
  so when ShaderProgram later builds the same sources it finishes quickly.
- Does nothing without the extension. (starting links without the extension can make later draw commands wait on the link and stall instead)
- Does not query link results, and deletes the programs and shaders once all are linked. (the cache remains, and no error is raised here even if a source is wrong)
- Usage: ShaderWarmer::new(gl, &[(vertex source, fragment source, attribute location list)]), then update(gl) every frame.
  Attribute locations must be the same as those passed to ShaderProgram::create_with_attribute_locations to hit the cache.

## ShaderWarmerSource (type)

One source to prelink. (vertex shader, fragment shader, attribute location list)

```rust
pub type ShaderWarmerSource<'a> = (&'a str, &'a str, &'a [(&'a str, u32)])
```

## ShaderWarmer (struct)

Shader prelinker. (links in the background with KHR_parallel_shader_compile and leaves the result in the driver's program cache)
- Create it at startup (during the logo, loading) and call update every frame. Does nothing without the extension.

Example: Prelink during loading and check whether it is done

```rust
use vanilla::{Graphic, ShaderWarmer, ShaderWarmerSource};

const VERTEX_SHADER_SOURCE: &str = "#version 300 es
in vec2 vertexPosition;
void main() {
	gl_Position = vec4(vertexPosition, 0.0, 1.0);
}
";

const FRAGMENT_SHADER_SOURCE: &str = "#version 300 es
precision highp float;
out vec4 outputColor;
void main() {
	outputColor = vec4(1.0);
}
";

const ATTRIBUTE_LOCATION_LIST: [(&str, u32); 1] = [("vertexPosition", 0)];

fn start_warming(graphic: &Graphic) -> ShaderWarmer {
	let program_source_list: [ShaderWarmerSource; 1] = [(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE, &ATTRIBUTE_LOCATION_LIST)];
	ShaderWarmer::new(graphic.get_gl(), &program_source_list)
}

fn update_loading(graphic: &Graphic, shader_warmer: &mut ShaderWarmer) -> bool {
	shader_warmer.update(graphic.get_gl());
	shader_warmer.is_finished()
}
```

- `pub fn new(gl: &GlContext, program_source_list: &[ShaderWarmerSource]) -> ShaderWarmer` (constructor, since 0.4.0): Constructor. (If the extension exists, compiles all sources and starts linking)
  - `gl` &GlContext: GL context. (get_gl of Graphic)
  - `program_source_list` &[ShaderWarmerSource]: List of (vertex source, fragment source, attribute location list) to prelink. (same as what will later be given to ShaderProgram)
  - returns ShaderWarmer: The created warmer. Without the extension nothing is started and is_finished is true right away. (no error even if compiling or linking is wrong)
- `pub fn update(&mut self, gl: &GlContext)` (method, since 0.4.0): Every frame. (Deletes the ones whose linking finished, does not wait when asking whether it finished)
  - `gl` &GlContext: The same GL context given to new.
- `pub fn is_finished(&self) -> bool` (method, since 0.4.0): Whether all are linked. (true from the start if not done for lack of the extension)
  - returns bool: True if no program is waiting for linking.
- `pub fn is_supported(&self) -> bool` (method, since 0.4.0): Whether the extension is available. (if false, prelinking is not done)
  - returns bool: True if there is an extension whose name contains parallel_shader_compile.
