vanilladocs
vanillapunch.com
API / core/shaderwarmer

core/shaderwarmer

셰이더 미리 링크. (vanilla.js core/graphic/shaderwarmer.js 의 ShaderWarmer)

  • ShaderProgram 은 링크하자마자 결과를 물어 링크가 끝날 때까지 멈춘다. 셰이더 여럿을 처음 그릴 때 한꺼번에 지으면 그 프레임이 멈칫한다.
  • 켤 때(로고, 로딩 동안) 같은 소스를 KHR_parallel_shader_compile 로 뒤에서 링크해 두면 드라이버(웹은 브라우저)의 프로그램 캐시에 남아, 나중에 ShaderProgram 이 같은 소스를 지을 때 금방 끝난다.
  • 확장이 없으면 아무것도 하지 않는다. (확장 없이 링크를 걸어 두면 그 뒤의 그리기 명령이 링크를 기다려 오히려 멈출 수 있다)
  • 링크 결과는 묻지 않고, 다 링크되면 프로그램과 셰이더를 지운다. (캐시는 남는다, 소스가 틀려도 여기서는 오류를 내지 않는다)
  • 사용: ShaderWarmer::new(gl, &[(정점 소스, 조각 소스, 어트리뷰트 위치 목록)]) 뒤 매 프레임 update(gl). 어트리뷰트 위치는 ShaderProgram::create_with_attribute_locations 에 넘길 것과 같게 줘야 캐시에 걸린다.

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.

ShaderWarmerSourcetype

소스source

미리 링크할 소스 하나. (정점 셰이더, 조각 셰이더, 어트리뷰트 위치 목록)

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

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

ShaderWarmerstruct

짝Counterpart: vanilla.js ShaderWarmer소스source

셰이더 미리 링크기. (KHR_parallel_shader_compile 로 뒤에서 링크해 드라이버의 프로그램 캐시에 남긴다)

  • 켤 때(로고, 로딩 동안) 만들고 매 프레임 update 를 부른다. 확장이 없으면 아무것도 하지 않는다.

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
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()
}
생성자constructorpub fn new(gl: &GlContext, program_source_list: &[ShaderWarmerSource]) -> ShaderWarmer#0.4.0 부터since 0.4.0소스source

생성. (확장이 있으면 소스를 모두 컴파일하고 링크를 걸어 둔다)

Constructor. (If the extension exists, compiles all sources and starts linking)

gl&GlContext
GL 컨텍스트. (Graphic 의 get_gl)GL context. (get_gl of Graphic)
program_source_list&[ShaderWarmerSource]
미리 링크할 (버텍스 소스, 프래그먼트 소스, 어트리뷰트 위치 목록) 목록. (나중에 ShaderProgram 에 줄 것과 같게)List of (vertex source, fragment source, attribute location list) to prelink. (same as what will later be given to ShaderProgram)

반환ReturnsShaderWarmer 만든 워머. 확장이 없으면 아무것도 걸지 않아 is_finished 가 바로 true 다. (컴파일이나 링크가 틀려도 오류를 내지 않는다)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)

짝Counterpart: vanilla.js ShaderWarmer.constructor

메서드methodpub fn update(&mut self, gl: &GlContext)#0.4.0 부터since 0.4.0소스source

매 프레임. (링크가 끝난 것을 지운다, 끝났는지 묻는 것은 기다리지 않는다)

Every frame. (Deletes the ones whose linking finished, does not wait when asking whether it finished)

gl&GlContext
new 에 준 것과 같은 GL 컨텍스트.The same GL context given to new.

짝Counterpart: vanilla.js ShaderWarmer.update

메서드methodpub fn is_finished(&self) -> bool#0.4.0 부터since 0.4.0소스source

다 링크했는지. (확장이 없어 하지 않았으면 처음부터 true)

Whether all are linked. (true from the start if not done for lack of the extension)

반환Returnsbool 링크를 기다리는 프로그램이 없으면 true.True if no program is waiting for linking.

짝Counterpart: vanilla.js ShaderWarmer.isFinished

메서드methodpub fn is_supported(&self) -> bool#0.4.0 부터since 0.4.0소스source

확장을 쓸 수 있는지. (false 면 미리 링크를 하지 않는다)

Whether the extension is available. (if false, prelinking is not done)

반환Returnsbool 확장 이름에 parallel_shader_compile 이 든 확장이 있으면 true.True if there is an extension whose name contains parallel_shader_compile.

짝Counterpart: vanilla.js ShaderWarmer.isSupported