core/shaderprogram
셰이더 프로그램.
- 원본은 GLSL ES 3.00 (#version 300 es) 이다. 데스크탑 GL 3.3 코어에서 돌도록 머리만 #version 330 core 로 바꿔 컴파일한다. (precision 한정자는 GLSL 1.30 부터 의미 없이 받아 주므로 본문은 그대로 둔다)
- 유니폼 / 어트리뷰트 위치는 처음 물을 때 읽어 담아 둔다.
Shader program.
- The original is GLSL ES 3.00 (#version 300 es). To run on desktop GL 3.3 core, only the header is changed to #version 330 core before compiling. (precision qualifiers are accepted without meaning since GLSL 1.30, so the body is left as is)
- Uniform / attribute locations are read and cached on the first query.
ShaderProgramstruct
셰이더 프로그램. (버텍스, 프래그먼트 셰이더를 링크한 GL 프로그램과 유니폼, 어트리뷰트 위치 캐시)
- 소스는 GLSL ES 3.00 으로 쓴다. 데스크탑은 머리만 #version 330 core 로 바꿔 컴파일한다.
- GL 컨텍스트를 들지 않고 함수마다 받는다. (Graphic 의 get_gl)
Shader program. (a GL program linking vertex and fragment shaders, with a cache of uniform and attribute locations)
- Sources are written in GLSL ES 3.00. On desktop only the header is changed to #version 330 core before compiling.
- Does not hold the GL context and takes it per function. (Graphic's get_gl)
use vanilla::{Graphic, ShaderProgram};
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;
uniform vec4 mainColor;
out vec4 outputColor;
void main() {
outputColor = mainColor;
}
";
fn create_program(graphic: &Graphic) -> Result<ShaderProgram, String> {
let gl = graphic.get_gl();
let shader_program = ShaderProgram::create_with_attribute_locations(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE, &[("vertexPosition", 0)])?;
shader_program.use_program(gl);
let main_color_location = shader_program.get_uniform_location(gl, "mainColor");
unsafe {
gl.uniform_4_f32(main_color_location.as_ref(), 1.0, 0.5, 0.0, 1.0);
}
assert_eq!(shader_program.get_attribute_location(gl, "vertexPosition"), Some(0));
Ok(shader_program)
}pub fn new(gl: &GlContext, vertex_shader_source: &str, fragment_shader_source: &str) -> Result<ShaderProgram, String>#생성. (컴파일 / 링크 실패면 로그를 담은 오류, 그때까지 지은 셰이더와 프로그램은 지운다)
Creates. (an error holding the log on compile / link failure, the shaders and programs built until then are deleted)
- gl&GlContext
- GL 컨텍스트. (Graphic 의 get_gl)GL context. (get_gl of Graphic)
- vertex_shader_source&str
- 버텍스 셰이더 소스. (GLSL ES 3.00)Vertex shader source. (GLSL ES 3.00)
- fragment_shader_source&str
- 프래그먼트 셰이더 소스. (GLSL ES 3.00)Fragment shader source. (GLSL ES 3.00)
반환ReturnsResult<ShaderProgram, String> 링크한 프로그램. 셰이더 컴파일이나 프로그램 링크에 실패하면 정보 로그를 담은 Err, GL 객체를 만들지 못하면 그 오류 글을 담은 Err.The linked program. Err with the info log if shader compilation or program linking fails, Err with the error text if a GL object cannot be created.
짝Counterpart: vanilla.js ShaderProgram.constructor
pub fn create_with_attribute_locations(gl: &GlContext, vertex_shader_source: &str, fragment_shader_source: &str, attribute_location_list: &[(&str, u32)]) -> Result<ShaderProgram, String>#어트리뷰트 위치를 링크 전에 고정해 생성. (원본 생성자의 attributeLocationTable 인자, 다른 프로그램의 버텍스 어레이를 함께 쓸 때)
Creates with attribute locations fixed before linking. (the original constructor's attributeLocationTable argument, for sharing another program's vertex array)
- gl&GlContext
- GL 컨텍스트.GL context.
- vertex_shader_source&str
- 버텍스 셰이더 소스. (GLSL ES 3.00)Vertex shader source. (GLSL ES 3.00)
- fragment_shader_source&str
- 프래그먼트 셰이더 소스. (GLSL ES 3.00)Fragment shader source. (GLSL ES 3.00)
- attribute_location_list&[(&str, u32)]
- (어트리뷰트 이름, 위치) 목록. (링크 전에 bind_attrib_location 으로 건다, 비면 new 와 같다)List of (attribute name, location). (bound with bind_attrib_location before linking, same as new if empty)
반환ReturnsResult<ShaderProgram, String> 링크한 프로그램. 셰이더 컴파일이나 프로그램 링크에 실패하면 정보 로그를 담은 Err, GL 객체를 만들지 못하면 그 오류 글을 담은 Err.The linked program. Err with the info log if shader compilation or program linking fails, Err with the error text if a GL object cannot be created.
pub fn create_deferred(gl: &GlContext, vertex_shader_source: &str, fragment_shader_source: &str, attribute_location_list: &[(&str, u32)]) -> Result<ShaderProgram, String>#컴파일과 링크를 맡기기만 하고 결과는 확인하지 않는 생성. (원본 생성자의 isDeferred, finish_build 를 불러야 쓸 수 있다)
- KHR_parallel_shader_compile 이 있으면 드라이버가 뒤에서 짓고 is_build_complete 로 주 스레드를 막지 않고 끝을 물을 수 있다.
Creation that only hands off compiling and linking without checking the result. (The original constructor's isDeferred; finish_build must be called before use)
- With KHR_parallel_shader_compile the driver builds in the background and is_build_complete can poll completion without blocking the main thread.
- gl&GlContext
- GL 컨텍스트.GL context.
- vertex_shader_source&str
- 버텍스 셰이더 소스. (GLSL ES 3.00)Vertex shader source. (GLSL ES 3.00)
- fragment_shader_source&str
- 프래그먼트 셰이더 소스. (GLSL ES 3.00)Fragment shader source. (GLSL ES 3.00)
- attribute_location_list&[(&str, u32)]
- (어트리뷰트 이름, 위치) 목록. (링크 전에 건다)List of (attribute name, location). (Bound before linking)
반환ReturnsResult<ShaderProgram, String> 맡긴 프로그램. 컴파일이나 링크 오류는 finish_build 가 알린다. GL 객체를 만들지 못하면 그 오류 글을 담은 Err.The handed-off program. A compile or link error is reported by finish_build. Err with the error text if a GL object cannot be created.
use vanilla::{Graphic, ShaderProgram};
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);
}
";
fn build(graphic: &Graphic) -> Result<ShaderProgram, String> {
let gl = graphic.get_gl();
let shader_program = ShaderProgram::create_deferred(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE, &[("vertexPosition", 0)])?;
assert!(shader_program.is_deferred());
// Finish it once it is done. (finish_build waits until done if it is not)
if shader_program.is_build_complete(gl) {
shader_program.finish_build(gl)?;
}
Ok(shader_program)
}병렬 셰이더 컴파일 확장이 있는지. (원본 isParallelCompileSupported, KHR_parallel_shader_compile, 웹은 같은 확장)
Whether the parallel shader compile extension exists. (The original isParallelCompileSupported, KHR_parallel_shader_compile; the web uses the same extension)
- gl&GlContext
- GL 컨텍스트.GL context.
반환Returnsbool 있으면 true. (있으면 컴파일과 링크를 맡기고 주 스레드를 막지 않고 끝을 물을 수 있다)True if present. (If present, compiling and linking can be handed off and completion polled without blocking the main thread)
짝Counterpart: vanilla.js ShaderProgram.isParallelCompileSupported
짓기를 맡긴 채 아직 끝내지 않았는지. (create_deferred 로 지었고 finish_build 를 아직 안 불렀으면 true)
Whether the build is handed off and not yet finished. (True if built with create_deferred and finish_build has not been called yet)
반환Returnsbool 맡긴 짓기가 아직 안 끝났으면 true.True if the building handed off is not finished yet.
맡긴 짓기가 끝났는지. (원본 isBuildComplete, 맡기지 않았거나 이미 끝냈으면 true, 확장이 없으면 true, 물을 뿐 멈추지 않는다)
Whether the handed-off build is done. (The original isBuildComplete; true if nothing was handed off or it is already finished, or if the extension is missing; it only asks and never blocks)
- gl&GlContext
- 프로그램을 만든 GL 컨텍스트.GL context that created the program.
반환Returnsbool 끝났으면 true. (true 여도 finish_build 로 결과를 확인해야 쓴다)True if done. (Even when true, call finish_build to check the result before use)
짝Counterpart: vanilla.js ShaderProgram.isBuildComplete
맡긴 짓기 끝내기. (원본 finishBuild, 컴파일과 링크 결과를 확인한다, 안 끝났으면 끝날 때까지 기다린다, 맡기지 않았거나 이미 끝냈으면 아무것도 하지 않는다)
- 실패하면 셰이더와 프로그램을 지우고 정보 로그를 담은 Err 를 돌려준다. (생성자와 같은 글)
Finishes the handed-off build. (The original finishBuild; 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 returns an Err with the info log. (Same message as the constructor)
- gl&GlContext
- 프로그램을 만든 GL 컨텍스트.GL context that created the program.
반환ReturnsResult<(), String> 성공하면 Ok. 컴파일이나 링크에 실패하면 Err.Ok on success. Err if compiling or linking failed.
짝Counterpart: vanilla.js ShaderProgram.finishBuild
사용. (원본 useProgram, 상태 캐시가 이미 이 프로그램을 쓴다고 기억하면 GL 에 다시 걸지 않는다)
Use. (original useProgram, not set on GL again when the state cache remembers this program is already in use)
- gl&GlContext
- 프로그램을 만든 GL 컨텍스트.GL context that created the program.
짝Counterpart: vanilla.js ShaderProgram.useProgram
pub fn get_uniform_location(&self, gl: &GlContext, name: &str) -> Option<crate::core::graphic::GlUniformLocation>#유니폼 위치 반환. 웹 판 때문에 clone 을 쓴다. (데스크탑에서는 Copy 라 clippy 가 말린다)
Returns the uniform location. Uses clone because of the web version. (on desktop it is Copy, so clippy objects)
- gl&GlContext
- 프로그램을 만든 GL 컨텍스트.GL context that created the program.
- name&str
- 유니폼 이름. (셰이더 소스의 이름 그대로)Uniform name. (exactly as named in the shader source)
반환ReturnsOption<crate::core::graphic::GlUniformLocation> 유니폼 위치. 그 이름의 활성 유니폼이 없으면 None. (처음 물을 때 읽고, 없다는 것까지 담아 둔다)Uniform location. None if there is no active uniform with that name. (read on the first query, and even its absence is cached)
짝Counterpart: vanilla.js ShaderProgram.getUniformLocation
어트리뷰트 위치 반환.
Returns the attribute location.
- gl&GlContext
- 프로그램을 만든 GL 컨텍스트.GL context that created the program.
- name&str
- 어트리뷰트 이름. (셰이더 소스의 이름 그대로)Attribute name. (as named in the shader source)
반환ReturnsOption<u32> 어트리뷰트 위치. 그 이름의 활성 어트리뷰트가 없으면 None. (처음 물을 때 읽고 담아 둔다)Attribute location. None if there is no active attribute with that name. (read and cached on the first query)
짝Counterpart: vanilla.js ShaderProgram.getAttributeLocation
GL 프로그램 반환.
Returns the GL program.
반환Returnscrate::core::graphic::GlProgram 링크한 GL 프로그램 객체.The linked GL program object.
짝Counterpart: vanilla.js ShaderProgram.getProgram
상수Constants
엔진이 이름으로 정한 뼈 행렬 유니폼 블록의 이름. (링크한 프로그램에 이 이름의 블록이 있으면 JOINT_BLOCK_BINDING 자리에 묶는다, 원본 JOINT_BLOCK_NAME)
- 뼈 행렬 같은 큰 배열은 기본 블록 유니폼 한도(WebGL2 가 보장하는 최솟값 256 칸)에 들지 않아 유니폼 블록으로 넘긴다. (툰 재질 블록 0, 바깥선 블록 1, 프레임 블록 2 는 툰 렌더러가 따로 묶는다)
Name of the joint matrix uniform block fixed by the engine. (if a linked program has a block with this name, it is bound to the JOINT_BLOCK_BINDING point; original JOINT_BLOCK_NAME)
- Large arrays such as joint matrices do not fit the default-block uniform limit (the minimum of 256 slots that WebGL2 guarantees), so they are passed through a uniform block. (toon material block 0, outline block 1 and frame block 2 are bound separately by the toon renderer)
반환Returns&str
뼈 행렬 유니폼 블록의 바인딩 자리. (원본 JOINT_BLOCK_BINDING)
Binding point of the joint matrix uniform block. (original JOINT_BLOCK_BINDING)
반환Returnsu32