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

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

셰이더 프로그램.

## ShaderProgram extends Object

셰이더 프로그램.
- 버텍스/프래그먼트 셰이더의 컴파일과 링크, 로케이션 캐시를 담당.

예제: 셰이더를 바꿔 끼워 흑백으로 그리기

```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): 생성.
  - 컴파일이나 링크에 실패하면 그때까지 지은 셰이더와 프로그램을 지우고 오류를 던진다.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 렌더링 컨텍스트.
  - `vertexShaderSource` string: 버텍스 셰이더 GLSL 소스.
  - `fragmentShaderSource` string: 프래그먼트 셰이더 GLSL 소스.
  - `attributeLocationTable` object | null: 링크 전에 고정할 어트리뷰트 위치. ({ 이름: 위치 }, 다른 프로그램의 버텍스 어레이를 함께 쓸 때)
  - `isDeferred` boolean: true 면 컴파일과 링크를 맡기기만 하고 결과는 확인하지 않는다. (기본 false, finishBuild 를 불러야 쓸 수 있다)
- `static isParallelCompileSupported(webGL2RenderingContext)` (static, since 0.4.0): 병렬 셰이더 컴파일 확장이 있는지. (KHR_parallel_shader_compile, 있으면 컴파일과 링크를 맡기고 주 스레드를 막지 않고 끝을 물을 수 있다)
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 렌더링 컨텍스트.
  - returns boolean: 있으면 true.
- `isBuildComplete()` (method, since 0.4.0): 맡긴 짓기가 끝났는지. (맡기지 않았거나 이미 끝냈으면 true, 확장이 없으면 true, 물을 뿐 주 스레드를 막지 않는다)
  - returns boolean: 끝났으면 true. (true 여도 finishBuild 로 결과를 확인해야 쓴다)
- `finishBuild()` (method, since 0.4.0): 맡긴 짓기 끝내기. (컴파일과 링크 결과를 확인한다, 안 끝났으면 끝날 때까지 기다린다, 맡기지 않았거나 이미 끝냈으면 아무것도 하지 않는다)
  - 실패하면 셰이더와 프로그램을 지우고 오류를 던진다. (생성자와 같은 글)
- `createShader(shaderType, shaderSource)` (method, since 0.3.0): 셰이더 객체 생성.
  - `shaderType` GLenum: 셰이더 갈래. (VERTEX_SHADER 또는 FRAGMENT_SHADER)
  - `shaderSource` string: GLSL 소스.
  - returns WebGLShader: 컴파일한 셰이더. (컴파일에 실패하면 오류를 던진다)
- `useProgram()` (method, since 0.4.0): 프로그램 사용 시작. (컨텍스트의 상태 캐시가 이미 이 프로그램을 쓴다고 기억하면 다시 걸지 않는다)
- `getAttributeLocation(attributeName)` (method, since 0.3.0): 어트리뷰트 로케이션 반환. (캐시)
  - `attributeName` string: 어트리뷰트 이름.
  - returns GLint: 어트리뷰트 위치. (없으면 -1)
- `getUniformLocation(uniformName)` (method, since 0.3.0): 유니폼 로케이션 반환. (캐시)
  - `uniformName` string: 유니폼 이름.
  - returns WebGLUniformLocation: 유니폼 위치. (없으면 null)
- `getWebGL2RenderingContext()` (method, since 0.3.0): 렌더링 컨텍스트 반환.
  - returns WebGL2RenderingContext: 렌더링 컨텍스트.
- `getProgram()` (method, since 0.3.0): 프로그램 객체 반환.
  - returns WebGLProgram: 링크한 프로그램 객체.
