core/graphic/shaderwarmer
셰이더 미리 링크.
Shader pre-linking.
ShaderWarmer
셰이더 미리 링크.
- ShaderProgram 은 링크하자마자 결과를 물어 주 스레드가 링크가 끝날 때까지 멈춘다. 셰이더 여럿을 처음 그릴 때 한꺼번에 지으면 그 프레임이 멈칫한다.
- 켤 때(로고, 로딩 동안) 같은 소스를 KHR_parallel_shader_compile 로 뒤에서 링크해 두면 브라우저의 프로그램 캐시에 남아, 나중에 ShaderProgram 이 같은 소스를 지을 때 금방 끝난다.
- 확장이 없으면 아무것도 하지 않는다. (확장 없이 링크를 걸어 두면 그 뒤의 그리기 명령이 링크를 기다려 오히려 멈출 수 있다)
- 링크 결과는 묻지 않고, 다 링크되면 프로그램과 셰이더를 지운다. (캐시는 남는다, 소스가 틀려도 여기서는 오류를 내지 않고 그릴 때의 ShaderProgram 이 알린다)
- 그리기 상태(묶인 프로그램, 버퍼, 텍스처)는 건드리지 않는다.
- 사용: new ShaderWarmer(webGL2RenderingContext, [{ vertexShader, fragmentShader, attributeLocationTable }]) 뒤 매 프레임 update(). 어트리뷰트 위치는 ShaderProgram 에 넘길 것과 같게 줘야 캐시에 걸린다.
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.
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. (If the extension exists, compiles all sources and starts linking)
- webGL2RenderingContextWebGL2RenderingContext
- WebGL2 렌더링 컨텍스트.WebGL2 rendering context.
- programSourcesArray<object>
- { vertexShader, fragmentShader, attributeLocationTable } 의 배열. (먼저 쓸 것부터)An array of { vertexShader, fragmentShader, attributeLocationTable }. (Those used first come first)
짝Counterpart: vanilla-engine ShaderWarmer.new
하나를 컴파일하고 링크 걸기. (ShaderProgram 과 같은 차례, 어트리뷰트 위치도 같게 묶어야 캐시에 걸린다)
Compiles one and starts linking. (Same order as ShaderProgram, attribute locations must also be bound the same to hit the cache)
- programSourceobject
- { vertexShader, fragmentShader, attributeLocationTable }
반환Returnsobject { program, vertexShader, fragmentShader }
매 프레임. (링크가 끝난 것을 지운다, 끝났는지 묻는 것은 기다리지 않는다)
Every frame. (Deletes the ones whose linking finished, does not wait when asking whether it finished)
짝Counterpart: vanilla-engine ShaderWarmer.update
다 링크했는지 반환. (확장이 없어 하지 않았으면 처음부터 true)
Returns whether everything has been linked. (true from the start if not done because the extension is absent)
반환Returnsboolean 다 링크했으면 참.True if all are linked.
짝Counterpart: vanilla-engine ShaderWarmer.is_finished
확장을 쓸 수 있는지 반환. (false 면 미리 링크를 하지 않는다)
Returns whether the extension can be used. (If false, pre-linking is not done)
반환Returnsboolean 확장을 쓸 수 있으면 참.True if the extension is available.
짝Counterpart: vanilla-engine ShaderWarmer.is_supported
렌더링 컨텍스트 반환.
Returns the rendering context.
반환ReturnsWebGL2RenderingContext 렌더링 컨텍스트.Rendering context.
병렬 링크 확장 반환. (없으면 null)
Returns the parallel link extension. (null if none)
반환Returnsobject | null KHR_parallel_shader_compile 확장. (없으면 null)The KHR_parallel_shader_compile extension. (null if none)