2019-01-04 13:14:32 +00:00
|
|
|
use {
|
|
|
|
js_sys::WebAssembly,
|
|
|
|
wasm_bindgen::{prelude::*, JsCast},
|
2021-08-26 16:40:35 +00:00
|
|
|
web_sys::{
|
|
|
|
ExtSRgb, WebGlBuffer, WebGlFramebuffer, WebGlProgram, WebGlRenderingContext, WebGlShader,
|
|
|
|
WebGlTexture,
|
|
|
|
},
|
2019-01-04 13:14:32 +00:00
|
|
|
};
|
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
use egui::{
|
2021-03-21 16:47:03 +00:00
|
|
|
emath::vec2,
|
2021-02-14 09:53:39 +00:00
|
|
|
epaint::{Color32, Texture},
|
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;
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
pub struct WebGlPainter {
|
2020-07-18 16:00:05 +00:00
|
|
|
canvas_id: String,
|
2019-01-04 13:14:32 +00:00
|
|
|
canvas: web_sys::HtmlCanvasElement,
|
|
|
|
gl: WebGlRenderingContext,
|
|
|
|
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,
|
2021-08-26 16:40:35 +00:00
|
|
|
texture_format: u32,
|
|
|
|
post_process: Option<PostProcess>,
|
2020-09-11 06:56:47 +00:00
|
|
|
|
|
|
|
egui_texture: WebGlTexture,
|
|
|
|
egui_texture_version: Option<u64>,
|
|
|
|
|
2020-11-18 20:38:29 +00:00
|
|
|
/// `None` means unallocated (freed) slot.
|
|
|
|
user_textures: Vec<Option<UserTexture>>,
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct UserTexture {
|
|
|
|
size: (usize, usize),
|
|
|
|
|
|
|
|
/// Pending upload (will be emptied later).
|
|
|
|
pixels: Vec<u8>,
|
|
|
|
|
|
|
|
/// Lazily uploaded
|
2020-11-18 20:38:29 +00:00
|
|
|
gl_texture: Option<WebGlTexture>,
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
impl WebGlPainter {
|
|
|
|
pub fn new(canvas_id: &str) -> Result<WebGlPainter, JsValue> {
|
2020-07-18 16:00:05 +00:00
|
|
|
let canvas = crate::canvas_element_or_die(canvas_id);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
let gl = canvas
|
|
|
|
.get_context("webgl")?
|
2021-01-16 00:30:00 +00:00
|
|
|
.ok_or_else(|| JsValue::from("Failed to get WebGl context"))?
|
2019-01-04 13:14:32 +00:00
|
|
|
.dyn_into::<WebGlRenderingContext>()?;
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
let egui_texture = gl.create_texture().unwrap();
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&egui_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
|
|
|
|
2021-08-26 16:40:35 +00:00
|
|
|
let srgb_supported = matches!(gl.get_extension("EXT_sRGB"), Ok(Some(_)));
|
|
|
|
|
2021-09-08 22:16:06 +00:00
|
|
|
let vert_shader = compile_shader(
|
|
|
|
&gl,
|
|
|
|
Gl::VERTEX_SHADER,
|
|
|
|
include_str!("shader/main_vertex_100es.glsl"),
|
|
|
|
)?;
|
2021-08-26 16:40:35 +00:00
|
|
|
let (texture_format, program, post_process) = if srgb_supported {
|
|
|
|
let frag_shader = compile_shader(
|
|
|
|
&gl,
|
|
|
|
Gl::FRAGMENT_SHADER,
|
|
|
|
include_str!("shader/main_fragment_100es.glsl"),
|
|
|
|
)?;
|
|
|
|
let program = link_program(&gl, [vert_shader, frag_shader].iter())?;
|
|
|
|
|
|
|
|
let post_process =
|
|
|
|
PostProcess::new(gl.clone(), canvas.width() as i32, canvas.height() as i32)?;
|
|
|
|
|
|
|
|
(ExtSRgb::SRGB_ALPHA_EXT, program, Some(post_process))
|
|
|
|
} else {
|
|
|
|
let frag_shader = compile_shader(
|
|
|
|
&gl,
|
|
|
|
Gl::FRAGMENT_SHADER,
|
|
|
|
include_str!("shader/fragment_100es.glsl"),
|
|
|
|
)?;
|
|
|
|
let program = link_program(&gl, [vert_shader, frag_shader].iter())?;
|
|
|
|
|
|
|
|
(Gl::RGBA, program, None)
|
|
|
|
};
|
2020-08-29 09:10:11 +00:00
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
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")?;
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
Ok(WebGlPainter {
|
2020-07-18 16:00:05 +00:00
|
|
|
canvas_id: canvas_id.to_owned(),
|
2019-01-04 13:14:32 +00:00
|
|
|
canvas,
|
|
|
|
gl,
|
|
|
|
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,
|
2021-08-26 16:40:35 +00:00
|
|
|
texture_format,
|
|
|
|
post_process,
|
2020-09-11 06:56:47 +00:00
|
|
|
egui_texture,
|
|
|
|
egui_texture_version: None,
|
|
|
|
user_textures: Default::default(),
|
2019-01-04 13:14:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
fn alloc_user_texture_index(&mut self) -> usize {
|
|
|
|
for (index, tex) in self.user_textures.iter_mut().enumerate() {
|
2020-11-18 20:38:29 +00:00
|
|
|
if tex.is_none() {
|
|
|
|
*tex = Some(Default::default());
|
2021-01-16 00:30:00 +00:00
|
|
|
return index;
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-16 00:30:00 +00:00
|
|
|
let index = self.user_textures.len();
|
2020-11-18 20:38:29 +00:00
|
|
|
self.user_textures.push(Some(Default::default()));
|
2021-01-16 00:30:00 +00:00
|
|
|
index
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
fn alloc_user_texture(
|
2020-09-11 06:56:47 +00:00
|
|
|
&mut self,
|
|
|
|
size: (usize, usize),
|
2021-01-02 16:02:18 +00:00
|
|
|
srgba_pixels: &[Color32],
|
2021-01-16 00:30:00 +00:00
|
|
|
) -> egui::TextureId {
|
|
|
|
let index = self.alloc_user_texture_index();
|
2021-07-29 20:20:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
size.0 * size.1,
|
|
|
|
srgba_pixels.len(),
|
|
|
|
"Mismatch between texture size and texel count"
|
|
|
|
);
|
2020-09-11 06:56:47 +00:00
|
|
|
|
2021-02-12 16:40:53 +00:00
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(index) {
|
|
|
|
let mut pixels: Vec<u8> = Vec::with_capacity(srgba_pixels.len() * 4);
|
|
|
|
for srgba in srgba_pixels {
|
|
|
|
pixels.push(srgba.r());
|
|
|
|
pixels.push(srgba.g());
|
|
|
|
pixels.push(srgba.b());
|
|
|
|
pixels.push(srgba.a());
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
2021-02-12 16:40:53 +00:00
|
|
|
|
|
|
|
*user_texture = UserTexture {
|
|
|
|
size,
|
|
|
|
pixels,
|
|
|
|
gl_texture: None,
|
|
|
|
};
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
2021-01-16 00:30:00 +00:00
|
|
|
|
|
|
|
egui::TextureId::User(index as u64)
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
2020-09-11 06:56:47 +00:00
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
fn free_user_texture(&mut self, id: egui::TextureId) {
|
2020-11-18 20:38:29 +00:00
|
|
|
if let egui::TextureId::User(id) = id {
|
|
|
|
let index = id as usize;
|
|
|
|
if index < self.user_textures.len() {
|
|
|
|
self.user_textures[index] = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 20:20:22 +00:00
|
|
|
|
2021-03-13 11:41:51 +00:00
|
|
|
pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<&WebGlTexture> {
|
2020-11-18 20:38:29 +00:00
|
|
|
match texture_id {
|
|
|
|
egui::TextureId::Egui => Some(&self.egui_texture),
|
|
|
|
egui::TextureId::User(id) => self
|
|
|
|
.user_textures
|
|
|
|
.get(id as usize)?
|
|
|
|
.as_ref()?
|
|
|
|
.gl_texture
|
|
|
|
.as_ref(),
|
|
|
|
}
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn upload_user_textures(&mut self) {
|
|
|
|
let gl = &self.gl;
|
|
|
|
|
2021-05-06 18:49:22 +00:00
|
|
|
for user_texture in self.user_textures.iter_mut().flatten() {
|
|
|
|
if user_texture.gl_texture.is_none() {
|
|
|
|
let pixels = std::mem::take(&mut user_texture.pixels);
|
|
|
|
|
|
|
|
let gl_texture = gl.create_texture().unwrap();
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&gl_texture));
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&gl_texture));
|
|
|
|
|
|
|
|
let level = 0;
|
2021-08-26 16:40:35 +00:00
|
|
|
let internal_format = self.texture_format;
|
2021-05-06 18:49:22 +00:00
|
|
|
let border = 0;
|
2021-08-26 16:40:35 +00:00
|
|
|
let src_format = self.texture_format;
|
2021-05-06 18:49:22 +00:00
|
|
|
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,
|
|
|
|
user_texture.size.0 as i32,
|
|
|
|
user_texture.size.1 as i32,
|
|
|
|
border,
|
|
|
|
src_format,
|
|
|
|
src_type,
|
|
|
|
Some(&pixels),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
user_texture.gl_texture = Some(gl_texture);
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-17 17:03:39 +00:00
|
|
|
}
|
2021-04-01 20:11:29 +00:00
|
|
|
|
2021-09-05 09:00:45 +00:00
|
|
|
#[deprecated = "Use: `NativeTexture::register_native_texture` instead"]
|
2021-03-13 11:41:51 +00:00
|
|
|
pub fn register_webgl_texture(&mut self, texture: WebGlTexture) -> egui::TextureId {
|
|
|
|
let id = self.alloc_user_texture_index();
|
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(id) {
|
|
|
|
*user_texture = UserTexture {
|
|
|
|
size: (0, 0),
|
|
|
|
pixels: vec![],
|
|
|
|
gl_texture: Some(texture),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
egui::TextureId::User(id as u64)
|
|
|
|
}
|
2021-04-01 20:11:29 +00:00
|
|
|
|
2021-02-14 09:53:39 +00:00
|
|
|
fn paint_mesh(&self, mesh: &egui::epaint::Mesh16) -> Result<(), JsValue> {
|
2021-01-25 20:23:24 +00:00
|
|
|
debug_assert!(mesh.is_valid());
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2021-01-25 20:23:24 +00:00
|
|
|
let mut positions: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
|
|
|
|
let mut tex_coords: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
|
2021-04-01 20:11:29 +00:00
|
|
|
let mut colors: Vec<u8> = Vec::with_capacity(4 * mesh.vertices.len());
|
2021-01-25 20:23:24 +00:00
|
|
|
for v in &mesh.vertices {
|
2019-01-05 15:23:40 +00:00
|
|
|
positions.push(v.pos.x);
|
|
|
|
positions.push(v.pos.y);
|
2020-09-09 15:14:42 +00:00
|
|
|
tex_coords.push(v.uv.x);
|
|
|
|
tex_coords.push(v.uv.y);
|
2020-09-02 20:04:10 +00:00
|
|
|
colors.push(v.color[0]);
|
|
|
|
colors.push(v.color[1]);
|
|
|
|
colors.push(v.color[2]);
|
|
|
|
colors.push(v.color[3]);
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
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();
|
2021-01-25 21:06:06 +00:00
|
|
|
let indices_ptr = mesh.indices.as_ptr() as u32 / 2;
|
2019-01-04 13:14:32 +00:00
|
|
|
let indices_array = js_sys::Int16Array::new(&indices_memory_buffer)
|
2021-01-25 21:06:06 +00:00
|
|
|
.subarray(indices_ptr, indices_ptr + mesh.indices.len() as u32);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
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();
|
2020-09-09 15:14:42 +00:00
|
|
|
let tc_ptr = tex_coords.as_ptr() as u32 / 4;
|
|
|
|
let tc_array = js_sys::Float32Array::new(&tc_memory_buffer)
|
2019-01-05 14:28:07 +00:00
|
|
|
.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;
|
2020-09-09 15:14:42 +00:00
|
|
|
gl.vertex_attrib_pointer_with_i32(a_tc_loc, 2, Gl::FLOAT, 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
|
|
|
|
2020-08-29 09:10:11 +00:00
|
|
|
let a_srgba_loc = gl.get_attrib_location(&self.program, "a_srgba");
|
|
|
|
assert!(a_srgba_loc >= 0);
|
|
|
|
let a_srgba_loc = a_srgba_loc as u32;
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2020-08-29 09:10:11 +00:00
|
|
|
let normalize = false;
|
2019-01-04 13:14:32 +00:00
|
|
|
let stride = 0;
|
|
|
|
let offset = 0;
|
|
|
|
gl.vertex_attrib_pointer_with_i32(
|
2020-08-29 09:10:11 +00:00
|
|
|
a_srgba_loc,
|
2019-01-04 13:14:32 +00:00
|
|
|
4,
|
2019-01-05 14:28:07 +00:00
|
|
|
Gl::UNSIGNED_BYTE,
|
2019-01-04 13:14:32 +00:00
|
|
|
normalize,
|
|
|
|
stride,
|
|
|
|
offset,
|
|
|
|
);
|
2020-08-29 09:10:11 +00:00
|
|
|
gl.enable_vertex_attrib_array(a_srgba_loc);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2021-01-25 21:06:06 +00:00
|
|
|
gl.draw_elements_with_i32(
|
|
|
|
Gl::TRIANGLES,
|
|
|
|
mesh.indices.len() as i32,
|
|
|
|
Gl::UNSIGNED_SHORT,
|
|
|
|
0,
|
|
|
|
);
|
2019-01-04 13:14:32 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
impl epi::TextureAllocator for WebGlPainter {
|
|
|
|
fn alloc_srgba_premultiplied(
|
|
|
|
&mut self,
|
|
|
|
size: (usize, usize),
|
|
|
|
srgba_pixels: &[egui::Color32],
|
|
|
|
) -> egui::TextureId {
|
|
|
|
self.alloc_user_texture(size, srgba_pixels)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn free(&mut self, id: egui::TextureId) {
|
|
|
|
self.free_user_texture(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 09:00:45 +00:00
|
|
|
impl epi::NativeTexture for WebGlPainter {
|
|
|
|
type Texture = WebGlTexture;
|
|
|
|
|
|
|
|
fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId {
|
|
|
|
let id = self.alloc_user_texture_index();
|
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(id) {
|
|
|
|
*user_texture = UserTexture {
|
|
|
|
size: (0, 0),
|
|
|
|
pixels: vec![],
|
|
|
|
gl_texture: Some(native),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
egui::TextureId::User(id as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_native_texture(&mut self, id: egui::TextureId, replacing: Self::Texture) {
|
|
|
|
if let egui::TextureId::User(id) = id {
|
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) {
|
|
|
|
*user_texture = UserTexture {
|
|
|
|
size: (0, 0),
|
|
|
|
pixels: vec![],
|
|
|
|
gl_texture: Some(replacing),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
impl crate::Painter for WebGlPainter {
|
|
|
|
fn as_tex_allocator(&mut self) -> &mut dyn epi::TextureAllocator {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// id of the canvas html element containing the rendering
|
|
|
|
fn canvas_id(&self) -> &str {
|
|
|
|
&self.canvas_id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn upload_egui_texture(&mut self, texture: &Texture) {
|
|
|
|
if self.egui_texture_version == Some(texture.version) {
|
|
|
|
return; // No change
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:40:35 +00:00
|
|
|
let gamma = if self.post_process.is_none() {
|
|
|
|
1.0 / 2.2 // HACK due to non-linear framebuffer blending.
|
|
|
|
} else {
|
|
|
|
1.0 // post process enables linear blending
|
|
|
|
};
|
2021-01-16 00:30:00 +00:00
|
|
|
let mut pixels: Vec<u8> = Vec::with_capacity(texture.pixels.len() * 4);
|
2021-08-26 16:40:35 +00:00
|
|
|
for srgba in texture.srgba_pixels(gamma) {
|
2021-01-16 00:30:00 +00:00
|
|
|
pixels.push(srgba.r());
|
|
|
|
pixels.push(srgba.g());
|
|
|
|
pixels.push(srgba.b());
|
|
|
|
pixels.push(srgba.a());
|
|
|
|
}
|
|
|
|
|
|
|
|
let gl = &self.gl;
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.egui_texture));
|
|
|
|
|
|
|
|
let level = 0;
|
2021-08-26 16:40:35 +00:00
|
|
|
let internal_format = self.texture_format;
|
2021-01-16 00:30:00 +00:00
|
|
|
let border = 0;
|
2021-08-26 16:40:35 +00:00
|
|
|
let src_format = self.texture_format;
|
2021-01-16 00:30:00 +00:00
|
|
|
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,
|
|
|
|
Some(&pixels),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
self.egui_texture_version = Some(texture.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear(&mut self, clear_color: egui::Rgba) {
|
|
|
|
let gl = &self.gl;
|
|
|
|
|
|
|
|
gl.disable(Gl::SCISSOR_TEST);
|
2021-01-17 01:06:26 +00:00
|
|
|
|
|
|
|
let width = self.canvas.width() as i32;
|
|
|
|
let height = self.canvas.height() as i32;
|
|
|
|
gl.viewport(0, 0, width, height);
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
let clear_color: Color32 = clear_color.into();
|
|
|
|
gl.clear_color(
|
|
|
|
clear_color[0] as f32 / 255.0,
|
|
|
|
clear_color[1] as f32 / 255.0,
|
|
|
|
clear_color[2] as f32 / 255.0,
|
|
|
|
clear_color[3] as f32 / 255.0,
|
|
|
|
);
|
|
|
|
gl.clear(Gl::COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:43:17 +00:00
|
|
|
fn paint_meshes(
|
|
|
|
&mut self,
|
|
|
|
clipped_meshes: Vec<egui::ClippedMesh>,
|
|
|
|
pixels_per_point: f32,
|
|
|
|
) -> Result<(), JsValue> {
|
2021-01-16 00:30:00 +00:00
|
|
|
self.upload_user_textures();
|
|
|
|
|
|
|
|
let gl = &self.gl;
|
|
|
|
|
2021-08-26 16:40:35 +00:00
|
|
|
if let Some(ref mut post_process) = self.post_process {
|
|
|
|
post_process.begin(self.canvas.width() as i32, self.canvas.height() as i32)?;
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
gl.enable(Gl::SCISSOR_TEST);
|
2021-01-17 13:48:59 +00:00
|
|
|
gl.disable(Gl::CULL_FACE); // egui is not strict about winding order.
|
2021-01-16 00:30:00 +00:00
|
|
|
gl.enable(Gl::BLEND);
|
|
|
|
gl.blend_func(Gl::ONE, Gl::ONE_MINUS_SRC_ALPHA); // premultiplied alpha
|
|
|
|
gl.use_program(Some(&self.program));
|
|
|
|
gl.active_texture(Gl::TEXTURE0);
|
|
|
|
|
|
|
|
let u_screen_size_loc = gl
|
|
|
|
.get_uniform_location(&self.program, "u_screen_size")
|
|
|
|
.unwrap();
|
|
|
|
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;
|
|
|
|
gl.uniform2f(
|
|
|
|
Some(&u_screen_size_loc),
|
|
|
|
screen_size_points.x,
|
|
|
|
screen_size_points.y,
|
|
|
|
);
|
|
|
|
|
|
|
|
let u_sampler_loc = gl.get_uniform_location(&self.program, "u_sampler").unwrap();
|
|
|
|
gl.uniform1i(Some(&u_sampler_loc), 0);
|
|
|
|
|
2021-01-25 20:43:17 +00:00
|
|
|
for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes {
|
2021-01-25 20:23:24 +00:00
|
|
|
if let Some(gl_texture) = self.get_texture(mesh.texture_id) {
|
2021-01-16 00:30:00 +00:00
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(gl_texture));
|
|
|
|
|
|
|
|
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;
|
2021-03-21 16:47:03 +00:00
|
|
|
let clip_min_x = clip_min_x.clamp(0.0, screen_size_pixels.x);
|
|
|
|
let clip_min_y = clip_min_y.clamp(0.0, screen_size_pixels.y);
|
|
|
|
let clip_max_x = clip_max_x.clamp(clip_min_x, screen_size_pixels.x);
|
|
|
|
let clip_max_y = clip_max_y.clamp(clip_min_y, screen_size_pixels.y);
|
2021-01-16 00:30:00 +00:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
|
2021-01-25 20:23:24 +00:00
|
|
|
for mesh in mesh.split_to_u16() {
|
|
|
|
self.paint_mesh(&mesh)?;
|
2021-01-16 00:30:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
crate::console_warn(format!(
|
|
|
|
"WebGL: Failed to find texture {:?}",
|
2021-01-25 20:23:24 +00:00
|
|
|
mesh.texture_id
|
2021-01-16 00:30:00 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 16:40:35 +00:00
|
|
|
|
|
|
|
if let Some(ref post_process) = self.post_process {
|
|
|
|
post_process.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PostProcess {
|
|
|
|
gl: Gl,
|
|
|
|
pos_buffer: WebGlBuffer,
|
|
|
|
a_pos_loc: u32,
|
|
|
|
index_buffer: WebGlBuffer,
|
|
|
|
texture: WebGlTexture,
|
|
|
|
texture_size: (i32, i32),
|
|
|
|
fbo: WebGlFramebuffer,
|
|
|
|
program: WebGlProgram,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PostProcess {
|
|
|
|
fn new(gl: Gl, width: i32, height: i32) -> Result<PostProcess, JsValue> {
|
|
|
|
let fbo = gl
|
|
|
|
.create_framebuffer()
|
|
|
|
.ok_or("failed to create framebuffer")?;
|
|
|
|
gl.bind_framebuffer(Gl::FRAMEBUFFER, Some(&fbo));
|
|
|
|
|
|
|
|
let texture = gl.create_texture().unwrap();
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&texture));
|
|
|
|
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);
|
|
|
|
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MIN_FILTER, Gl::NEAREST as i32);
|
|
|
|
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MAG_FILTER, Gl::NEAREST as i32);
|
|
|
|
gl.pixel_storei(Gl::UNPACK_ALIGNMENT, 1);
|
|
|
|
gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
|
|
|
|
Gl::TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
ExtSRgb::SRGB_ALPHA_EXT as i32,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
0,
|
|
|
|
ExtSRgb::SRGB_ALPHA_EXT,
|
|
|
|
Gl::UNSIGNED_BYTE,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
gl.framebuffer_texture_2d(
|
|
|
|
Gl::FRAMEBUFFER,
|
|
|
|
Gl::COLOR_ATTACHMENT0,
|
|
|
|
Gl::TEXTURE_2D,
|
|
|
|
Some(&texture),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, None);
|
|
|
|
gl.bind_framebuffer(Gl::FRAMEBUFFER, None);
|
|
|
|
|
|
|
|
let vert_shader = compile_shader(
|
|
|
|
&gl,
|
|
|
|
Gl::VERTEX_SHADER,
|
|
|
|
include_str!("shader/post_vertex_100es.glsl"),
|
|
|
|
)?;
|
|
|
|
let frag_shader = compile_shader(
|
|
|
|
&gl,
|
|
|
|
Gl::FRAGMENT_SHADER,
|
|
|
|
include_str!("shader/post_fragment_100es.glsl"),
|
|
|
|
)?;
|
|
|
|
let program = link_program(&gl, [vert_shader, frag_shader].iter())?;
|
|
|
|
|
|
|
|
let positions = vec![0u8, 0, 1, 0, 0, 1, 1, 1];
|
|
|
|
|
|
|
|
let indices = vec![0u8, 1, 2, 1, 2, 3];
|
|
|
|
|
|
|
|
let pos_buffer = gl.create_buffer().ok_or("failed to create pos_buffer")?;
|
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&pos_buffer));
|
|
|
|
gl.buffer_data_with_u8_array(Gl::ARRAY_BUFFER, &positions, Gl::STATIC_DRAW);
|
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, None);
|
|
|
|
|
|
|
|
let a_pos_loc = gl.get_attrib_location(&program, "a_pos");
|
|
|
|
assert!(a_pos_loc >= 0);
|
|
|
|
let a_pos_loc = a_pos_loc as u32;
|
|
|
|
|
|
|
|
let index_buffer = gl.create_buffer().ok_or("failed to create index_buffer")?;
|
|
|
|
gl.bind_buffer(Gl::ELEMENT_ARRAY_BUFFER, Some(&index_buffer));
|
|
|
|
gl.buffer_data_with_u8_array(Gl::ELEMENT_ARRAY_BUFFER, &indices, Gl::STATIC_DRAW);
|
|
|
|
gl.bind_buffer(Gl::ELEMENT_ARRAY_BUFFER, None);
|
|
|
|
|
|
|
|
Ok(PostProcess {
|
|
|
|
gl,
|
|
|
|
pos_buffer,
|
|
|
|
a_pos_loc,
|
|
|
|
index_buffer,
|
|
|
|
texture,
|
|
|
|
texture_size: (width, height),
|
|
|
|
fbo,
|
|
|
|
program,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn begin(&mut self, width: i32, height: i32) -> Result<(), JsValue> {
|
|
|
|
let gl = &self.gl;
|
|
|
|
|
|
|
|
if (width, height) != self.texture_size {
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture));
|
|
|
|
gl.pixel_storei(Gl::UNPACK_ALIGNMENT, 1);
|
|
|
|
gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
|
|
|
|
Gl::TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
ExtSRgb::SRGB_ALPHA_EXT as i32,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
0,
|
|
|
|
ExtSRgb::SRGB_ALPHA_EXT,
|
|
|
|
Gl::UNSIGNED_BYTE,
|
|
|
|
None,
|
|
|
|
)?;
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, None);
|
|
|
|
|
|
|
|
self.texture_size = (width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.bind_framebuffer(Gl::FRAMEBUFFER, Some(&self.fbo));
|
|
|
|
|
2021-01-16 00:30:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-26 16:40:35 +00:00
|
|
|
|
|
|
|
fn end(&self) {
|
|
|
|
let gl = &self.gl;
|
|
|
|
|
|
|
|
gl.bind_framebuffer(Gl::FRAMEBUFFER, None);
|
|
|
|
gl.disable(Gl::SCISSOR_TEST);
|
|
|
|
|
|
|
|
gl.use_program(Some(&self.program));
|
|
|
|
|
|
|
|
gl.active_texture(Gl::TEXTURE0);
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture));
|
|
|
|
let u_sampler_loc = gl.get_uniform_location(&self.program, "u_sampler").unwrap();
|
|
|
|
gl.uniform1i(Some(&u_sampler_loc), 0);
|
|
|
|
|
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&self.pos_buffer));
|
|
|
|
gl.vertex_attrib_pointer_with_i32(self.a_pos_loc, 2, Gl::UNSIGNED_BYTE, false, 0, 0);
|
|
|
|
gl.enable_vertex_attrib_array(self.a_pos_loc);
|
|
|
|
|
|
|
|
gl.bind_buffer(Gl::ELEMENT_ARRAY_BUFFER, Some(&self.index_buffer));
|
|
|
|
|
|
|
|
gl.draw_elements_with_i32(Gl::TRIANGLES, 6, Gl::UNSIGNED_BYTE, 0);
|
|
|
|
|
|
|
|
gl.bind_buffer(Gl::ELEMENT_ARRAY_BUFFER, None);
|
|
|
|
gl.bind_buffer(Gl::ARRAY_BUFFER, None);
|
|
|
|
gl.bind_texture(Gl::TEXTURE_2D, None);
|
|
|
|
gl.use_program(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for PostProcess {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let gl = &self.gl;
|
|
|
|
gl.delete_buffer(Some(&self.pos_buffer));
|
|
|
|
gl.delete_buffer(Some(&self.index_buffer));
|
|
|
|
gl.delete_program(Some(&self.program));
|
|
|
|
gl.delete_framebuffer(Some(&self.fbo));
|
|
|
|
gl.delete_texture(Some(&self.texture));
|
|
|
|
}
|
2021-01-16 00:30:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
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()))
|
|
|
|
}
|
|
|
|
}
|