mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
@@ -7,16 +7,18 @@ pub(crate) unsafe fn compile_shader(
|
||||
shader_type: u32,
|
||||
source: &str,
|
||||
) -> Result<glow::Shader, String> {
|
||||
let shader = gl.create_shader(shader_type)?;
|
||||
unsafe {
|
||||
let shader = gl.create_shader(shader_type)?;
|
||||
|
||||
gl.shader_source(shader, source);
|
||||
gl.shader_source(shader, source);
|
||||
|
||||
gl.compile_shader(shader);
|
||||
gl.compile_shader(shader);
|
||||
|
||||
if gl.get_shader_compile_status(shader) {
|
||||
Ok(shader)
|
||||
} else {
|
||||
Err(gl.get_shader_info_log(shader))
|
||||
if gl.get_shader_compile_status(shader) {
|
||||
Ok(shader)
|
||||
} else {
|
||||
Err(gl.get_shader_info_log(shader))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,17 +26,19 @@ pub(crate) unsafe fn link_program<'a, T: IntoIterator<Item = &'a glow::Shader>>(
|
||||
gl: &glow::Context,
|
||||
shaders: T,
|
||||
) -> Result<glow::Program, String> {
|
||||
let program = gl.create_program()?;
|
||||
unsafe {
|
||||
let program = gl.create_program()?;
|
||||
|
||||
for shader in shaders {
|
||||
gl.attach_shader(program, *shader);
|
||||
}
|
||||
for shader in shaders {
|
||||
gl.attach_shader(program, *shader);
|
||||
}
|
||||
|
||||
gl.link_program(program);
|
||||
gl.link_program(program);
|
||||
|
||||
if gl.get_program_link_status(program) {
|
||||
Ok(program)
|
||||
} else {
|
||||
Err(gl.get_program_info_log(program))
|
||||
if gl.get_program_link_status(program) {
|
||||
Ok(program)
|
||||
} else {
|
||||
Err(gl.get_program_info_log(program))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,51 +276,51 @@ impl Painter {
|
||||
&mut self,
|
||||
[width_in_pixels, height_in_pixels]: [u32; 2],
|
||||
pixels_per_point: f32,
|
||||
) -> (u32, u32) {
|
||||
self.gl.enable(glow::SCISSOR_TEST);
|
||||
// egui outputs mesh in both winding orders
|
||||
self.gl.disable(glow::CULL_FACE);
|
||||
self.gl.disable(glow::DEPTH_TEST);
|
||||
) {
|
||||
unsafe {
|
||||
self.gl.enable(glow::SCISSOR_TEST);
|
||||
// egui outputs mesh in both winding orders
|
||||
self.gl.disable(glow::CULL_FACE);
|
||||
self.gl.disable(glow::DEPTH_TEST);
|
||||
|
||||
self.gl.color_mask(true, true, true, true);
|
||||
self.gl.color_mask(true, true, true, true);
|
||||
|
||||
self.gl.enable(glow::BLEND);
|
||||
self.gl
|
||||
.blend_equation_separate(glow::FUNC_ADD, glow::FUNC_ADD);
|
||||
self.gl.blend_func_separate(
|
||||
// egui outputs colors with premultiplied alpha:
|
||||
glow::ONE,
|
||||
glow::ONE_MINUS_SRC_ALPHA,
|
||||
// Less important, but this is technically the correct alpha blend function
|
||||
// when you want to make use of the framebuffer alpha (for screenshots, compositing, etc).
|
||||
glow::ONE_MINUS_DST_ALPHA,
|
||||
glow::ONE,
|
||||
);
|
||||
self.gl.enable(glow::BLEND);
|
||||
self.gl
|
||||
.blend_equation_separate(glow::FUNC_ADD, glow::FUNC_ADD);
|
||||
self.gl.blend_func_separate(
|
||||
// egui outputs colors with premultiplied alpha:
|
||||
glow::ONE,
|
||||
glow::ONE_MINUS_SRC_ALPHA,
|
||||
// Less important, but this is technically the correct alpha blend function
|
||||
// when you want to make use of the framebuffer alpha (for screenshots, compositing, etc).
|
||||
glow::ONE_MINUS_DST_ALPHA,
|
||||
glow::ONE,
|
||||
);
|
||||
|
||||
if !cfg!(target_arch = "wasm32") {
|
||||
self.gl.disable(glow::FRAMEBUFFER_SRGB);
|
||||
check_for_gl_error!(&self.gl, "FRAMEBUFFER_SRGB");
|
||||
if !cfg!(target_arch = "wasm32") {
|
||||
self.gl.disable(glow::FRAMEBUFFER_SRGB);
|
||||
check_for_gl_error!(&self.gl, "FRAMEBUFFER_SRGB");
|
||||
}
|
||||
|
||||
let width_in_points = width_in_pixels as f32 / pixels_per_point;
|
||||
let height_in_points = height_in_pixels as f32 / pixels_per_point;
|
||||
|
||||
self.gl
|
||||
.viewport(0, 0, width_in_pixels as i32, height_in_pixels as i32);
|
||||
self.gl.use_program(Some(self.program));
|
||||
|
||||
self.gl
|
||||
.uniform_2_f32(Some(&self.u_screen_size), width_in_points, height_in_points);
|
||||
self.gl.uniform_1_i32(Some(&self.u_sampler), 0);
|
||||
self.gl.active_texture(glow::TEXTURE0);
|
||||
|
||||
self.vao.bind(&self.gl);
|
||||
self.gl
|
||||
.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer));
|
||||
}
|
||||
|
||||
let width_in_points = width_in_pixels as f32 / pixels_per_point;
|
||||
let height_in_points = height_in_pixels as f32 / pixels_per_point;
|
||||
|
||||
self.gl
|
||||
.viewport(0, 0, width_in_pixels as i32, height_in_pixels as i32);
|
||||
self.gl.use_program(Some(self.program));
|
||||
|
||||
self.gl
|
||||
.uniform_2_f32(Some(&self.u_screen_size), width_in_points, height_in_points);
|
||||
self.gl.uniform_1_i32(Some(&self.u_sampler), 0);
|
||||
self.gl.active_texture(glow::TEXTURE0);
|
||||
|
||||
self.vao.bind(&self.gl);
|
||||
self.gl
|
||||
.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer));
|
||||
|
||||
check_for_gl_error!(&self.gl, "prepare_painting");
|
||||
|
||||
(width_in_pixels, height_in_pixels)
|
||||
}
|
||||
|
||||
pub fn clear(&self, screen_size_in_pixels: [u32; 2], clear_color: [f32; 4]) {
|
||||
@@ -377,14 +377,14 @@ impl Painter {
|
||||
crate::profile_function!();
|
||||
self.assert_not_destroyed();
|
||||
|
||||
let size_in_pixels = unsafe { self.prepare_painting(screen_size_px, pixels_per_point) };
|
||||
unsafe { self.prepare_painting(screen_size_px, pixels_per_point) };
|
||||
|
||||
for egui::ClippedPrimitive {
|
||||
clip_rect,
|
||||
primitive,
|
||||
} in clipped_primitives
|
||||
{
|
||||
set_clip_rect(&self.gl, size_in_pixels, pixels_per_point, *clip_rect);
|
||||
set_clip_rect(&self.gl, screen_size_px, pixels_per_point, *clip_rect);
|
||||
|
||||
match primitive {
|
||||
Primitive::Mesh(mesh) => {
|
||||
@@ -687,14 +687,16 @@ impl Painter {
|
||||
}
|
||||
|
||||
unsafe fn destroy_gl(&self) {
|
||||
self.gl.delete_program(self.program);
|
||||
for tex in self.textures.values() {
|
||||
self.gl.delete_texture(*tex);
|
||||
}
|
||||
self.gl.delete_buffer(self.vbo);
|
||||
self.gl.delete_buffer(self.element_array_buffer);
|
||||
for t in &self.textures_to_destroy {
|
||||
self.gl.delete_texture(*t);
|
||||
unsafe {
|
||||
self.gl.delete_program(self.program);
|
||||
for tex in self.textures.values() {
|
||||
self.gl.delete_texture(*tex);
|
||||
}
|
||||
self.gl.delete_buffer(self.vbo);
|
||||
self.gl.delete_buffer(self.element_array_buffer);
|
||||
for t in &self.textures_to_destroy {
|
||||
self.gl.delete_texture(*t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -747,7 +749,7 @@ impl Drop for Painter {
|
||||
|
||||
fn set_clip_rect(
|
||||
gl: &glow::Context,
|
||||
size_in_pixels: (u32, u32),
|
||||
[width_px, height_px]: [u32; 2],
|
||||
pixels_per_point: f32,
|
||||
clip_rect: Rect,
|
||||
) {
|
||||
@@ -764,15 +766,15 @@ fn set_clip_rect(
|
||||
let clip_max_y = clip_max_y.round() as i32;
|
||||
|
||||
// Clamp:
|
||||
let clip_min_x = clip_min_x.clamp(0, size_in_pixels.0 as i32);
|
||||
let clip_min_y = clip_min_y.clamp(0, size_in_pixels.1 as i32);
|
||||
let clip_max_x = clip_max_x.clamp(clip_min_x, size_in_pixels.0 as i32);
|
||||
let clip_max_y = clip_max_y.clamp(clip_min_y, size_in_pixels.1 as i32);
|
||||
let clip_min_x = clip_min_x.clamp(0, width_px as i32);
|
||||
let clip_min_y = clip_min_y.clamp(0, height_px as i32);
|
||||
let clip_max_x = clip_max_x.clamp(clip_min_x, width_px as i32);
|
||||
let clip_max_y = clip_max_y.clamp(clip_min_y, height_px as i32);
|
||||
|
||||
unsafe {
|
||||
gl.scissor(
|
||||
clip_min_x,
|
||||
size_in_pixels.1 as i32 - clip_max_y,
|
||||
height_px as i32 - clip_max_y,
|
||||
clip_max_x - clip_min_x,
|
||||
clip_max_y - clip_min_y,
|
||||
);
|
||||
|
||||
@@ -34,30 +34,32 @@ impl VertexArrayObject {
|
||||
buffer_infos: Vec<BufferInfo>,
|
||||
) -> Self {
|
||||
let vao = if supports_vao(gl) {
|
||||
let vao = gl.create_vertex_array().unwrap();
|
||||
check_for_gl_error!(gl, "create_vertex_array");
|
||||
unsafe {
|
||||
let vao = gl.create_vertex_array().unwrap();
|
||||
check_for_gl_error!(gl, "create_vertex_array");
|
||||
|
||||
// Store state in the VAO:
|
||||
gl.bind_vertex_array(Some(vao));
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(vbo));
|
||||
// Store state in the VAO:
|
||||
gl.bind_vertex_array(Some(vao));
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(vbo));
|
||||
|
||||
for attribute in &buffer_infos {
|
||||
gl.vertex_attrib_pointer_f32(
|
||||
attribute.location,
|
||||
attribute.vector_size,
|
||||
attribute.data_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
attribute.offset,
|
||||
);
|
||||
check_for_gl_error!(gl, "vertex_attrib_pointer_f32");
|
||||
gl.enable_vertex_attrib_array(attribute.location);
|
||||
check_for_gl_error!(gl, "enable_vertex_attrib_array");
|
||||
for attribute in &buffer_infos {
|
||||
gl.vertex_attrib_pointer_f32(
|
||||
attribute.location,
|
||||
attribute.vector_size,
|
||||
attribute.data_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
attribute.offset,
|
||||
);
|
||||
check_for_gl_error!(gl, "vertex_attrib_pointer_f32");
|
||||
gl.enable_vertex_attrib_array(attribute.location);
|
||||
check_for_gl_error!(gl, "enable_vertex_attrib_array");
|
||||
}
|
||||
|
||||
gl.bind_vertex_array(None);
|
||||
|
||||
Some(vao)
|
||||
}
|
||||
|
||||
gl.bind_vertex_array(None);
|
||||
|
||||
Some(vao)
|
||||
} else {
|
||||
log::debug!("VAO not supported");
|
||||
None
|
||||
@@ -71,36 +73,40 @@ impl VertexArrayObject {
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn bind(&self, gl: &glow::Context) {
|
||||
if let Some(vao) = self.vao {
|
||||
gl.bind_vertex_array(Some(vao));
|
||||
check_for_gl_error!(gl, "bind_vertex_array");
|
||||
} else {
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vbo));
|
||||
check_for_gl_error!(gl, "bind_buffer");
|
||||
unsafe {
|
||||
if let Some(vao) = self.vao {
|
||||
gl.bind_vertex_array(Some(vao));
|
||||
check_for_gl_error!(gl, "bind_vertex_array");
|
||||
} else {
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vbo));
|
||||
check_for_gl_error!(gl, "bind_buffer");
|
||||
|
||||
for attribute in &self.buffer_infos {
|
||||
gl.vertex_attrib_pointer_f32(
|
||||
attribute.location,
|
||||
attribute.vector_size,
|
||||
attribute.data_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
attribute.offset,
|
||||
);
|
||||
check_for_gl_error!(gl, "vertex_attrib_pointer_f32");
|
||||
gl.enable_vertex_attrib_array(attribute.location);
|
||||
check_for_gl_error!(gl, "enable_vertex_attrib_array");
|
||||
for attribute in &self.buffer_infos {
|
||||
gl.vertex_attrib_pointer_f32(
|
||||
attribute.location,
|
||||
attribute.vector_size,
|
||||
attribute.data_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
attribute.offset,
|
||||
);
|
||||
check_for_gl_error!(gl, "vertex_attrib_pointer_f32");
|
||||
gl.enable_vertex_attrib_array(attribute.location);
|
||||
check_for_gl_error!(gl, "enable_vertex_attrib_array");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn unbind(&self, gl: &glow::Context) {
|
||||
if self.vao.is_some() {
|
||||
gl.bind_vertex_array(None);
|
||||
} else {
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, None);
|
||||
for attribute in &self.buffer_infos {
|
||||
gl.disable_vertex_attrib_array(attribute.location);
|
||||
unsafe {
|
||||
if self.vao.is_some() {
|
||||
gl.bind_vertex_array(None);
|
||||
} else {
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, None);
|
||||
for attribute in &self.buffer_infos {
|
||||
gl.disable_vertex_attrib_array(attribute.location);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user