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

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

Shader program.

## ShaderProgram extends Object

Shader program.
- Handles compiling and linking vertex/fragment shaders and the location cache.

Example: Swap in a shader to draw in grayscale

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

const VERTEX_SHADER_SOURCE = `#version 300 es
in vec2 vertexPosition;
in vec2 vertexTextureCoordinate;
uniform mat3 projectionMatrix;
uniform mat3 modelMatrix;
out vec2 fragmentTextureCoordinate;
void main() {
	vec3 transformedPosition = projectionMatrix * (modelMatrix * vec3(vertexPosition, 1.0));
	gl_Position = vec4(transformedPosition.xy, 0.0, 1.0);
	fragmentTextureCoordinate = vertexTextureCoordinate;
}
`;
const GRAY_FRAGMENT_SHADER_SOURCE = `#version 300 es
precision highp float;
in vec2 fragmentTextureCoordinate;
uniform sampler2D mainTexture;
uniform vec4 mainColor;
uniform float globalAlpha;
out vec4 outputColor;
void main() {
	vec4 textureColor = texture(mainTexture, fragmentTextureCoordinate);
	vec3 color = textureColor.rgb * mainColor.rgb;
	float gray = dot(color, vec3(0.299, 0.587, 0.114));
	float alpha = textureColor.a * mainColor.a * globalAlpha;
	outputColor = vec4(vec3(gray) * mainColor.a * globalAlpha, alpha);
}
`;

class MainScene extends Scene {
	initialize(engine) {
		const graphic = engine.getGraphic();
		const webGL2RenderingContext = graphic.getWebGL2RenderingContext();
		const defaultShaderProgram = graphic.getShaderProgram();
		const attributeLocationTable = {
			vertexPosition: defaultShaderProgram.getAttributeLocation("vertexPosition"),
			vertexTextureCoordinate: defaultShaderProgram.getAttributeLocation("vertexTextureCoordinate"),
		};
		this.grayShaderProgram = new ShaderProgram(webGL2RenderingContext, VERTEX_SHADER_SOURCE, GRAY_FRAGMENT_SHADER_SOURCE, attributeLocationTable);
	}

	draw(graphic) {
		super.draw(graphic);
		graphic.setFillColor("#ff6600");
		graphic.drawRect(Rect.create(60, 120, 200, 120));
		graphic.setShaderProgramOverride(this.grayShaderProgram);
		graphic.drawRect(Rect.create(380, 120, 200, 120));
		graphic.setShaderProgramOverride(null);
	}
}

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

- `constructor(webGL2RenderingContext, vertexShaderSource, fragmentShaderSource, attributeLocationTable = null, isDeferred = false)` (constructor, since 0.3.0): Constructor.
  - If compiling or linking fails, it deletes the shaders and program created so far and throws an error.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
  - `vertexShaderSource` string: Vertex shader GLSL source.
  - `fragmentShaderSource` string: Fragment shader GLSL source.
  - `attributeLocationTable` object | null: Attribute locations fixed before linking. ({ name: location }, when sharing vertex arrays with another program)
  - `isDeferred` boolean: If true, only hands off compiling and linking without checking the result. (Default false; call finishBuild before use)
- `static isParallelCompileSupported(webGL2RenderingContext)` (static, since 0.4.0): Whether the parallel shader compile extension exists. (KHR_parallel_shader_compile; if present, compiling and linking can be handed off and completion polled without blocking the main thread)
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
  - returns boolean: True if present.
- `isBuildComplete()` (method, since 0.4.0): Whether the handed-off build is done. (True if nothing was handed off or it is already finished, or if the extension is missing; it only asks and never blocks the main thread)
  - returns boolean: True if done. (Even when true, call finishBuild to check the result before use)
- `finishBuild()` (method, since 0.4.0): Finishes the handed-off build. (Checks the compile and link results and waits until done if not; does nothing if nothing was handed off or it is already finished)
  - On failure, deletes the shaders and program and throws. (Same message as the constructor)
- `createShader(shaderType, shaderSource)` (method, since 0.3.0): Creates a shader object.
  - `shaderType` GLenum: Shader type. (VERTEX_SHADER or FRAGMENT_SHADER)
  - `shaderSource` string: GLSL source.
  - returns WebGLShader: The compiled shader. (throws an error if compiling fails)
- `useProgram()` (method, since 0.4.0): Starts using the program. (Does not set it again when the context's state cache remembers that this program is already in use)
- `getAttributeLocation(attributeName)` (method, since 0.3.0): Returns the attribute location. (cached)
  - `attributeName` string: Attribute name.
  - returns GLint: Attribute location. (-1 if none)
- `getUniformLocation(uniformName)` (method, since 0.3.0): Returns the uniform location. (cached)
  - `uniformName` string: Uniform name.
  - returns WebGLUniformLocation: Uniform location. (null if none)
- `getWebGL2RenderingContext()` (method, since 0.3.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getProgram()` (method, since 0.3.0): Returns the program object.
  - returns WebGLProgram: The linked program object.
