2019-01-04 13:14:32 +00:00
|
|
|
use {
|
|
|
|
js_sys::WebAssembly,
|
|
|
|
wasm_bindgen::{prelude::*, JsCast},
|
2019-01-05 14:28:07 +00:00
|
|
|
web_sys::{WebGlBuffer, WebGlProgram, WebGlRenderingContext, WebGlShader, WebGlTexture},
|
2019-01-04 13:14:32 +00:00
|
|
|
};
|
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
use egui::{
|
2020-07-17 08:29:21 +00:00
|
|
|
math::clamp,
|
2020-05-19 20:28:57 +00:00
|
|
|
paint::{Color, PaintBatches, Texture, Triangles},
|
2020-07-17 08:29:21 +00:00
|
|
|
vec2,
|
2020-05-19 20:28:57 +00:00
|
|
|
};
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
type Gl = WebGlRenderingContext;
|
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
pub struct Painter {
|
|
|
|
canvas: web_sys::HtmlCanvasElement,
|
|
|
|
gl: WebGlRenderingContext,
|
2019-01-05 14:28:07 +00:00
|
|
|
texture: WebGlTexture,
|
2019-01-04 13:14:32 +00:00
|
|
|
program: WebGlProgram,
|
|
|
|
index_buffer: WebGlBuffer,
|
|
|
|
pos_buffer: WebGlBuffer,
|
2019-01-05 14:28:07 +00:00
|
|
|
tc_buffer: WebGlBuffer,
|
2019-01-04 13:14:32 +00:00
|
|
|
color_buffer: WebGlBuffer,
|
2019-01-05 14:28:07 +00:00
|
|
|
tex_size: (u16, u16),
|
2019-01-17 17:03:39 +00:00
|
|
|
current_texture_id: Option<u64>,
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Painter {
|
2019-01-06 23:03:29 +00:00
|
|
|
pub fn debug_info(&self) -> String {
|
|
|
|
format!(
|
|
|
|
"Stored canvas size: {} x {}\n\
|
|
|
|
gl context size: {} x {}",
|
|
|
|
self.canvas.width(),
|
|
|
|
self.canvas.height(),
|
|
|
|
self.gl.drawing_buffer_width(),
|
|
|
|
self.gl.drawing_buffer_height(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-17 17:03:39 +00:00
|
|
|
pub fn new(canvas_id: &str) -> Result<Painter, JsValue> {
|
2019-01-04 13:14:32 +00:00
|
|
|
let document = web_sys::window().unwrap().document().unwrap();
|
|
|
|
let canvas = document.get_element_by_id(canvas_id).unwrap();
|
|
|
|
let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::<web_sys::HtmlCanvasElement>()?;
|
|
|
|
|
|
|
|
let gl = canvas
|
|
|
|
.get_context("webgl")?
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<WebGlRenderingContext>()?;
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2019-01-16 23:09:12 +00:00
|
|
|
let gl_texture = gl.create_texture().unwrap();
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&gl_texture));
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_WRAP_S, Gl::CLAMP_TO_EDGE as i32);
|
|
|
|
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_WRAP_T, Gl::CLAMP_TO_EDGE as i32);
|
2019-01-21 07:48:32 +00:00
|
|
|
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MIN_FILTER, Gl::LINEAR as i32);
|
|
|
|
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MAG_FILTER, Gl::LINEAR as i32);
|
2019-01-05 14:28:07 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
let vert_shader = compile_shader(
|
|
|
|
&gl,
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::VERTEX_SHADER,
|
2019-01-04 13:14:32 +00:00
|
|
|
r#"
|
|
|
|
uniform vec2 u_screen_size;
|
2019-01-05 14:28:07 +00:00
|
|
|
uniform vec2 u_tex_size;
|
2019-01-04 13:14:32 +00:00
|
|
|
attribute vec2 a_pos;
|
2019-01-05 14:28:07 +00:00
|
|
|
attribute vec2 a_tc;
|
2019-01-04 13:14:32 +00:00
|
|
|
attribute vec4 a_color;
|
|
|
|
varying vec4 v_color;
|
2020-04-21 05:51:48 +00:00
|
|
|
varying vec2 v_tc;
|
2019-01-04 13:14:32 +00:00
|
|
|
void main() {
|
|
|
|
gl_Position = vec4(
|
|
|
|
2.0 * a_pos.x / u_screen_size.x - 1.0,
|
|
|
|
1.0 - 2.0 * a_pos.y / u_screen_size.y,
|
|
|
|
0.0,
|
|
|
|
1.0);
|
|
|
|
v_color = a_color;
|
2020-04-21 05:51:48 +00:00
|
|
|
v_tc = a_tc / u_tex_size;
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
let frag_shader = compile_shader(
|
|
|
|
&gl,
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::FRAGMENT_SHADER,
|
2019-01-04 13:14:32 +00:00
|
|
|
r#"
|
2019-01-05 14:28:07 +00:00
|
|
|
uniform sampler2D u_sampler;
|
2019-01-04 13:14:32 +00:00
|
|
|
precision highp float;
|
|
|
|
varying vec4 v_color;
|
2020-04-21 05:51:48 +00:00
|
|
|
varying vec2 v_tc;
|
2019-01-04 13:14:32 +00:00
|
|
|
void main() {
|
|
|
|
gl_FragColor = v_color;
|
2020-05-11 15:57:11 +00:00
|
|
|
gl_FragColor *= texture2D(u_sampler, v_tc).a;
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
let program = link_program(&gl, [vert_shader, frag_shader].iter())?;
|
|
|
|
let index_buffer = gl.create_buffer().ok_or("failed to create index_buffer")?;
|
|
|
|
let pos_buffer = gl.create_buffer().ok_or("failed to create pos_buffer")?;
|
2019-01-05 14:28:07 +00:00
|
|
|
let tc_buffer = gl.create_buffer().ok_or("failed to create tc_buffer")?;
|
2019-01-04 13:14:32 +00:00
|
|
|
let color_buffer = gl.create_buffer().ok_or("failed to create color_buffer")?;
|
|
|
|
|
|
|
|
Ok(Painter {
|
|
|
|
canvas,
|
|
|
|
gl,
|
2019-01-16 23:09:12 +00:00
|
|
|
texture: gl_texture,
|
2019-01-04 13:14:32 +00:00
|
|
|
program,
|
|
|
|
index_buffer,
|
|
|
|
pos_buffer,
|
2019-01-05 14:28:07 +00:00
|
|
|
tc_buffer,
|
2019-01-04 13:14:32 +00:00
|
|
|
color_buffer,
|
2019-01-17 17:03:39 +00:00
|
|
|
tex_size: (0, 0),
|
|
|
|
current_texture_id: None,
|
2019-01-04 13:14:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-17 17:03:39 +00:00
|
|
|
fn upload_texture(&mut self, texture: &Texture) {
|
|
|
|
if self.current_texture_id == Some(texture.id) {
|
|
|
|
return; // No change
|
|
|
|
}
|
|
|
|
|
|
|
|
let gl = &self.gl;
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture));
|
|
|
|
|
|
|
|
let level = 0;
|
|
|
|
let internal_format = Gl::ALPHA;
|
|
|
|
let border = 0;
|
|
|
|
let src_format = Gl::ALPHA;
|
|
|
|
let src_type = Gl::UNSIGNED_BYTE;
|
|
|
|
gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
|
|
|
|
Gl::TEXTURE_2D,
|
|
|
|
level,
|
|
|
|
internal_format as i32,
|
|
|
|
texture.width as i32,
|
|
|
|
texture.height as i32,
|
|
|
|
border,
|
|
|
|
src_format,
|
|
|
|
src_type,
|
2020-04-27 14:56:16 +00:00
|
|
|
Some(&texture.pixels),
|
2019-01-17 17:03:39 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
self.tex_size = (texture.width as u16, texture.height as u16);
|
|
|
|
self.current_texture_id = Some(texture.id);
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
pub fn paint_batches(
|
2019-01-19 16:10:28 +00:00
|
|
|
&mut self,
|
2019-03-16 12:37:29 +00:00
|
|
|
bg_color: Color,
|
2020-04-20 21:33:16 +00:00
|
|
|
batches: PaintBatches,
|
2019-01-19 16:10:28 +00:00
|
|
|
texture: &Texture,
|
|
|
|
pixels_per_point: f32,
|
|
|
|
) -> Result<(), JsValue> {
|
2019-01-17 17:03:39 +00:00
|
|
|
self.upload_texture(texture);
|
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
let gl = &self.gl;
|
|
|
|
|
2020-07-17 08:29:21 +00:00
|
|
|
gl.enable(Gl::SCISSOR_TEST);
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.enable(Gl::BLEND);
|
2020-05-11 15:57:11 +00:00
|
|
|
gl.blend_func(Gl::ONE, Gl::ONE_MINUS_SRC_ALPHA); // premultiplied alpha
|
2019-01-04 13:14:32 +00:00
|
|
|
gl.use_program(Some(&self.program));
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.active_texture(Gl::TEXTURE0);
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture));
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2019-03-12 13:43:50 +00:00
|
|
|
let u_screen_size_loc = gl
|
|
|
|
.get_uniform_location(&self.program, "u_screen_size")
|
|
|
|
.unwrap();
|
2020-04-23 20:07:08 +00:00
|
|
|
let screen_size_pixels = vec2(self.canvas.width() as f32, self.canvas.height() as f32);
|
|
|
|
let screen_size_points = screen_size_pixels / pixels_per_point;
|
2019-03-12 13:43:50 +00:00
|
|
|
gl.uniform2f(
|
|
|
|
Some(&u_screen_size_loc),
|
2020-04-23 20:07:08 +00:00
|
|
|
screen_size_points.x,
|
|
|
|
screen_size_points.y,
|
2019-03-12 13:43:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let u_tex_size_loc = gl
|
|
|
|
.get_uniform_location(&self.program, "u_tex_size")
|
|
|
|
.unwrap();
|
|
|
|
gl.uniform2f(
|
|
|
|
Some(&u_tex_size_loc),
|
|
|
|
f32::from(self.tex_size.0),
|
|
|
|
f32::from(self.tex_size.1),
|
|
|
|
);
|
|
|
|
|
|
|
|
let u_sampler_loc = gl.get_uniform_location(&self.program, "u_sampler").unwrap();
|
|
|
|
gl.uniform1i(Some(&u_sampler_loc), 0);
|
|
|
|
|
|
|
|
gl.viewport(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
self.canvas.width() as i32,
|
|
|
|
self.canvas.height() as i32,
|
|
|
|
);
|
2020-07-17 08:29:21 +00:00
|
|
|
// TODO: sRGBA
|
2019-03-16 12:37:29 +00:00
|
|
|
gl.clear_color(
|
|
|
|
bg_color.r as f32 / 255.0,
|
|
|
|
bg_color.g as f32 / 255.0,
|
|
|
|
bg_color.b as f32 / 255.0,
|
|
|
|
bg_color.a as f32 / 255.0,
|
|
|
|
);
|
2019-03-12 13:43:50 +00:00
|
|
|
gl.clear(Gl::COLOR_BUFFER_BIT);
|
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
for (clip_rect, triangles) in batches {
|
2020-07-17 08:29:21 +00:00
|
|
|
let clip_min_x = pixels_per_point * clip_rect.min.x;
|
|
|
|
let clip_min_y = pixels_per_point * clip_rect.min.y;
|
|
|
|
let clip_max_x = pixels_per_point * clip_rect.max.x;
|
|
|
|
let clip_max_y = pixels_per_point * clip_rect.max.y;
|
|
|
|
let clip_min_x = clamp(clip_min_x, 0.0..=screen_size_pixels.x);
|
|
|
|
let clip_min_y = clamp(clip_min_y, 0.0..=screen_size_pixels.y);
|
|
|
|
let clip_max_x = clamp(clip_max_x, clip_min_x..=screen_size_pixels.x);
|
|
|
|
let clip_max_y = clamp(clip_max_y, clip_min_y..=screen_size_pixels.y);
|
|
|
|
let clip_min_x = clip_min_x.round() as i32;
|
|
|
|
let clip_min_y = clip_min_y.round() as i32;
|
|
|
|
let clip_max_x = clip_max_x.round() as i32;
|
|
|
|
let clip_max_y = clip_max_y.round() as i32;
|
|
|
|
|
|
|
|
// scissor Y coordinate is from the bottom
|
|
|
|
gl.scissor(
|
|
|
|
clip_min_x,
|
|
|
|
self.canvas.height() as i32 - clip_max_y,
|
|
|
|
clip_max_x - clip_min_x,
|
|
|
|
clip_max_y - clip_min_y,
|
2020-04-21 05:51:48 +00:00
|
|
|
);
|
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
for triangles in triangles.split_to_u16() {
|
|
|
|
self.paint_triangles(&triangles)?;
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
2019-03-12 13:43:50 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
fn paint_triangles(&self, triangles: &Triangles) -> Result<(), JsValue> {
|
|
|
|
let indices: Vec<u16> = triangles.indices.iter().map(|idx| *idx as u16).collect();
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
let mut positions: Vec<f32> = Vec::with_capacity(2 * triangles.vertices.len());
|
|
|
|
let mut tex_coords: Vec<u16> = Vec::with_capacity(2 * triangles.vertices.len());
|
|
|
|
for v in &triangles.vertices {
|
2019-01-05 15:23:40 +00:00
|
|
|
positions.push(v.pos.x);
|
|
|
|
positions.push(v.pos.y);
|
|
|
|
tex_coords.push(v.uv.0);
|
|
|
|
tex_coords.push(v.uv.1);
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
let mut colors: Vec<u8> = Vec::with_capacity(4 * triangles.vertices.len());
|
|
|
|
for v in &triangles.vertices {
|
2019-01-04 13:14:32 +00:00
|
|
|
colors.push(v.color.r);
|
|
|
|
colors.push(v.color.g);
|
|
|
|
colors.push(v.color.b);
|
|
|
|
colors.push(v.color.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2019-03-12 13:43:50 +00:00
|
|
|
let gl = &self.gl;
|
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
let indices_memory_buffer = wasm_bindgen::memory()
|
|
|
|
.dyn_into::<WebAssembly::Memory>()?
|
|
|
|
.buffer();
|
|
|
|
let indices_ptr = indices.as_ptr() as u32 / 2;
|
|
|
|
let indices_array = js_sys::Int16Array::new(&indices_memory_buffer)
|
|
|
|
.subarray(indices_ptr, indices_ptr + indices.len() as u32);
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.bind_buffer(Gl::ELEMENT_ARRAY_BUFFER, Some(&self.index_buffer));
|
2019-01-04 13:14:32 +00:00
|
|
|
gl.buffer_data_with_array_buffer_view(
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::ELEMENT_ARRAY_BUFFER,
|
2019-01-04 13:14:32 +00:00
|
|
|
&indices_array,
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::STREAM_DRAW,
|
2019-01-04 13:14:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
let pos_memory_buffer = wasm_bindgen::memory()
|
|
|
|
.dyn_into::<WebAssembly::Memory>()?
|
|
|
|
.buffer();
|
|
|
|
let pos_ptr = positions.as_ptr() as u32 / 4;
|
|
|
|
let pos_array = js_sys::Float32Array::new(&pos_memory_buffer)
|
|
|
|
.subarray(pos_ptr, pos_ptr + positions.len() as u32);
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&self.pos_buffer));
|
|
|
|
gl.buffer_data_with_array_buffer_view(Gl::ARRAY_BUFFER, &pos_array, Gl::STREAM_DRAW);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
let a_pos_loc = gl.get_attrib_location(&self.program, "a_pos");
|
|
|
|
assert!(a_pos_loc >= 0);
|
|
|
|
let a_pos_loc = a_pos_loc as u32;
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
let normalize = false;
|
|
|
|
let stride = 0;
|
|
|
|
let offset = 0;
|
|
|
|
gl.vertex_attrib_pointer_with_i32(a_pos_loc, 2, Gl::FLOAT, normalize, stride, offset);
|
|
|
|
gl.enable_vertex_attrib_array(a_pos_loc);
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
let tc_memory_buffer = wasm_bindgen::memory()
|
|
|
|
.dyn_into::<WebAssembly::Memory>()?
|
|
|
|
.buffer();
|
|
|
|
let tc_ptr = tex_coords.as_ptr() as u32 / 2;
|
|
|
|
let tc_array = js_sys::Uint16Array::new(&tc_memory_buffer)
|
|
|
|
.subarray(tc_ptr, tc_ptr + tex_coords.len() as u32);
|
|
|
|
|
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&self.tc_buffer));
|
|
|
|
gl.buffer_data_with_array_buffer_view(Gl::ARRAY_BUFFER, &tc_array, Gl::STREAM_DRAW);
|
|
|
|
|
|
|
|
let a_tc_loc = gl.get_attrib_location(&self.program, "a_tc");
|
|
|
|
assert!(a_tc_loc >= 0);
|
|
|
|
let a_tc_loc = a_tc_loc as u32;
|
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
let normalize = false;
|
|
|
|
let stride = 0;
|
|
|
|
let offset = 0;
|
|
|
|
gl.vertex_attrib_pointer_with_i32(
|
2019-01-05 14:28:07 +00:00
|
|
|
a_tc_loc,
|
2019-01-04 13:14:32 +00:00
|
|
|
2,
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::UNSIGNED_SHORT,
|
2019-01-04 13:14:32 +00:00
|
|
|
normalize,
|
|
|
|
stride,
|
|
|
|
offset,
|
|
|
|
);
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.enable_vertex_attrib_array(a_tc_loc);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
let colors_memory_buffer = wasm_bindgen::memory()
|
|
|
|
.dyn_into::<WebAssembly::Memory>()?
|
|
|
|
.buffer();
|
|
|
|
let colors_ptr = colors.as_ptr() as u32;
|
|
|
|
let colors_array = js_sys::Uint8Array::new(&colors_memory_buffer)
|
|
|
|
.subarray(colors_ptr, colors_ptr + colors.len() as u32);
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&self.color_buffer));
|
|
|
|
gl.buffer_data_with_array_buffer_view(Gl::ARRAY_BUFFER, &colors_array, Gl::STREAM_DRAW);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
let a_color_loc = gl.get_attrib_location(&self.program, "a_color");
|
|
|
|
assert!(a_color_loc >= 0);
|
|
|
|
let a_color_loc = a_color_loc as u32;
|
|
|
|
|
|
|
|
let normalize = true;
|
|
|
|
let stride = 0;
|
|
|
|
let offset = 0;
|
|
|
|
gl.vertex_attrib_pointer_with_i32(
|
|
|
|
a_color_loc,
|
|
|
|
4,
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::UNSIGNED_BYTE,
|
2019-01-04 13:14:32 +00:00
|
|
|
normalize,
|
|
|
|
stride,
|
|
|
|
offset,
|
|
|
|
);
|
|
|
|
gl.enable_vertex_attrib_array(a_color_loc);
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
gl.draw_elements_with_i32(Gl::TRIANGLES, indices.len() as i32, Gl::UNSIGNED_SHORT, 0);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_shader(
|
|
|
|
gl: &WebGlRenderingContext,
|
|
|
|
shader_type: u32,
|
|
|
|
source: &str,
|
|
|
|
) -> Result<WebGlShader, String> {
|
|
|
|
let shader = gl
|
|
|
|
.create_shader(shader_type)
|
|
|
|
.ok_or_else(|| String::from("Unable to create shader object"))?;
|
|
|
|
gl.shader_source(&shader, source);
|
|
|
|
gl.compile_shader(&shader);
|
|
|
|
|
|
|
|
if gl
|
2019-01-05 14:28:07 +00:00
|
|
|
.get_shader_parameter(&shader, Gl::COMPILE_STATUS)
|
2019-01-04 13:14:32 +00:00
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
Ok(shader)
|
|
|
|
} else {
|
|
|
|
Err(gl
|
|
|
|
.get_shader_info_log(&shader)
|
|
|
|
.unwrap_or_else(|| "Unknown error creating shader".into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link_program<'a, T: IntoIterator<Item = &'a WebGlShader>>(
|
|
|
|
gl: &WebGlRenderingContext,
|
|
|
|
shaders: T,
|
|
|
|
) -> Result<WebGlProgram, String> {
|
|
|
|
let program = gl
|
|
|
|
.create_program()
|
|
|
|
.ok_or_else(|| String::from("Unable to create shader object"))?;
|
|
|
|
for shader in shaders {
|
|
|
|
gl.attach_shader(&program, shader)
|
|
|
|
}
|
|
|
|
gl.link_program(&program);
|
|
|
|
|
|
|
|
if gl
|
2019-01-05 14:28:07 +00:00
|
|
|
.get_program_parameter(&program, Gl::LINK_STATUS)
|
2019-01-04 13:14:32 +00:00
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
Ok(program)
|
|
|
|
} else {
|
|
|
|
Err(gl
|
|
|
|
.get_program_info_log(&program)
|
|
|
|
.unwrap_or_else(|| "Unknown error creating program object".into()))
|
|
|
|
}
|
|
|
|
}
|