This commit is contained in:
nora 2023-04-18 15:38:14 +02:00
parent 12163d1338
commit 550b1644cb
363 changed files with 84081 additions and 16 deletions

View file

@ -0,0 +1,122 @@
# Changelog for egui_glium
All notable changes to the `egui_glium` integration will be noted in this file.
## Unreleased
* Remove the `screen_reader` feature ([#2669](https://github.com/emilk/egui/pull/2669)).
## 0.20.1 - 2022-12-11
* Fix [docs.rs](https://docs.rs/egui_glium) build ([#2420](https://github.com/emilk/egui/pull/2420)).
## 0.20.0 - 2022-12-08
* MSRV (Minimum Supported Rust Version) is now `1.65.0` ([#2314](https://github.com/emilk/egui/pull/2314)).
## 0.19.0 - 2022-08-20
* MSRV (Minimum Supported Rust Version) is now `1.61.0` ([#1846](https://github.com/emilk/egui/pull/1846)).
## 0.18.0 - 2022-04-30
* Remove "epi" feature ([#1361](https://github.com/emilk/egui/pull/1361)).
* Remove need for `trait epi::NativeTexture` to use the `fn register_native_texture/replace_native_texture` ([#1361](https://github.com/emilk/egui/pull/1361)).
* MSRV (Minimum Supported Rust Version) is now `1.60.0` ([#1467](https://github.com/emilk/egui/pull/1467)).
## 0.17.0 - 2022-02-22
* `EguiGlium::run` no longer returns the shapes to paint, but stores them internally until you call `EguiGlium::paint` ([#1110](https://github.com/emilk/egui/pull/1110)).
* Optimize the painter and texture uploading ([#1110](https://github.com/emilk/egui/pull/1110)).
* Automatically detect and apply dark or light mode from system ([#1045](https://github.com/emilk/egui/pull/1045)).
## 0.16.0 - 2021-12-29
* Simplified `EguiGlium` interface ([#871](https://github.com/emilk/egui/pull/871)).
* Removed `EguiGlium::is_quit_event` ([#881](https://github.com/emilk/egui/pull/881)).
* Updated `glium` to 0.31 ([#930](https://github.com/emilk/egui/pull/930)).
* Changed the `Painter` interface slightly ([#999](https://github.com/emilk/egui/pull/999)).
## 0.15.0 - 2021-10-24
* Remove "http" feature (use https://github.com/emilk/ehttp instead!).
* Implement `epi::NativeTexture` trait for the glium painter.
* Deprecate 'Painter::register_glium_texture'.
* Increase scroll speed.
* Restore window position on startup without flickering.
* A lot of the code has been moved to the new library [`egui-winit`](https://github.com/emilk/egui/tree/master/crates/egui-winit).
* Fixed reactive mode on windows.
## 0.14.0 - 2021-08-24
* Fixed native file dialogs hanging (eg. when using [`rfd`](https://github.com/PolyMeilex/rfd)).
* Implement drag-and-dropping files into the application.
* [Fix minimize on Windows](https://github.com/emilk/egui/issues/518).
* Change `drag_and_drop_support` to `false` by default (Windows only). See <https://github.com/emilk/egui/issues/598>.
* Don't restore window position on Windows, because the position would sometimes be invalid.
## 0.13.1 - 2021-06-24
* Fixed `http` feature flag and docs
## 0.13.0 - 2021-06-24
* Added `EguiGlium::is_quit_event` to replace `control_flow` argument to `EguiGlium::on_event`.
* [Fix modifier key for zoom with mouse wheel on Mac](https://github.com/emilk/egui/issues/401)
* [Fix stuck modifier keys](https://github.com/emilk/egui/pull/479)
## 0.12.0 - 2021-05-10
* Simplify usage with a new `EguiGlium` wrapper type.
## 0.11.0 - 2021-04-05
* [Position IME candidate window next to text cursor](https://github.com/emilk/egui/pull/258).
* [Register your own glium textures](https://github.com/emilk/egui/pull/226).
* [Fix cursor icon flickering on Windows(https://github.com/emilk/egui/pull/218).
## 0.10.0 - 2021-02-28
* [Add shaders for GLSL 1.2, GLSL ES 1.0 and 3.0](https://github.com/emilk/egui/pull/187)
- now `egui` works well on old hardware which supports OpenGL 2.1 only like Raspberry Pi 1 and Zero.
## 0.9.0 - 2021-02-07
* Nothing new
## 0.8.0 - 2021-01-17
* Fixed a bug where key releases weren't sent to egui
* Fixed `set_window_size` for non-native `pixels_per_point`.
## 0.7.0 - 2021-01-04
* `http` `persistence` and `time` are now optional (and opt-in) features.
## 0.6.0 - 2020-12-26
### Added ⭐
* `egui_glium` will auto-save your app state every 30 seconds.
* `egui_glium` can now set windows as fixed size (e.g. the user can't resize the window). See `egui::App::is_resizable()`.
### Changed 🔧
* `egui_glium` will now save you app state to [a better directory](https://docs.rs/directories-next/2.0.0/directories_next/struct.ProjectDirs.html#method.data_dir).
* `egui_glium::run`: the parameter `app` now has signature `Box<dyn App>` (you need to add `Box::new(app)` to your code).
* Window title is now passed via the `trait` function `egui::App::name()`.
### Fixed 🐛
* Serialize window size in logical points instead of physical pixels.
* Window position is now restored on restart.
## 0.5.0 - 2020-12-13
* FileStorage::from_path now takes `Into<Path>` instead of `String`
## 0.4.0 - 2020-11-28
Started changelog. Features:
* Input
* Painting
* Clipboard handling
* Open URL:s
* Simple JSON-backed storage

View file

@ -0,0 +1,58 @@
[package]
name = "egui_glium"
version = "0.21.0"
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
description = "Bindings for using egui natively using the glium library"
edition = "2021"
rust-version = "1.65"
homepage = "https://github.com/emilk/egui/tree/master/crates/egui_glium"
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/emilk/egui/tree/master/crates/egui_glium"
categories = ["gui", "game-development"]
keywords = ["glium", "egui", "gui", "gamedev"]
include = [
"../LICENSE-APACHE",
"../LICENSE-MIT",
"**/*.rs",
"Cargo.toml",
"src/shader/*.glsl",
]
[package.metadata.docs.rs]
all-features = true
[features]
default = ["clipboard", "links"]
## Enable cut/copy/paste to OS clipboard.
##
## If disabled a clipboard will be simulated so you can still copy/paste within the egui app.
clipboard = ["egui-winit/clipboard"]
## Enable opening links in a browser when an egui hyperlink is clicked.
links = ["egui-winit/links"]
[dependencies]
egui = { version = "0.21.0", path = "../egui", default-features = false, features = [
"bytemuck",
] }
egui-winit = { version = "0.21.1", path = "../egui-winit", default-features = false }
ahash = { version = "0.8.1", default-features = false, features = [
"no-rng", # we don't need DOS-protection, so we let users opt-in to it instead
"std",
] }
bytemuck = "1.7"
glium = "0.32"
#! ### Optional dependencies
## Enable this when generating docs.
document-features = { version = "0.2", optional = true }
[dev-dependencies]
egui_demo_lib = { version = "0.21.0", path = "../egui_demo_lib", default-features = false }
image = { version = "0.24", default-features = false, features = ["png"] }

View file

@ -0,0 +1,21 @@
# egui_glium
[![Latest version](https://img.shields.io/crates/v/egui_glium.svg)](https://crates.io/crates/egui_glium)
[![Documentation](https://docs.rs/egui_glium/badge.svg)](https://docs.rs/egui_glium)
[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
![MIT](https://img.shields.io/badge/license-MIT-blue.svg)
![Apache](https://img.shields.io/badge/license-Apache-blue.svg)
This crates provides bindings between [`egui`](https://github.com/emilk/egui) and [glium](https://crates.io/crates/glium) which allows you to write GUI code using egui and compile it and run it natively, cross platform.
To use on Linux, first run:
```
sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
```
This crate depends on [`egui-winit`](https://github.com/emilk/egui/tree/master/crates/egui-winit).
## DEPRECATED - Looking for new maintainer
This crate is no longer being updated. If you are interested in keeping `egui_glium` updated, then fork it to its own repository, make a PR to the egui repo removing it, and then I will give you access to it on crates.io so you can publish new `egui_glium` crates.

View file

@ -0,0 +1,139 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use glium::glutin;
fn main() {
let event_loop = glutin::event_loop::EventLoopBuilder::with_user_event().build();
let display = create_display(&event_loop);
let mut egui_glium = egui_glium::EguiGlium::new(&display, &event_loop);
let png_data = include_bytes!("../../../examples/retained_image/src/rust-logo-256x256.png");
let image = load_glium_image(png_data);
let image_size = egui::vec2(image.width as f32, image.height as f32);
// Load to gpu memory
let glium_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
// Allow us to share the texture with egui:
let glium_texture = std::rc::Rc::new(glium_texture);
// Allocate egui's texture id for GL texture
let texture_id = egui_glium
.painter
.register_native_texture(glium_texture, Default::default());
// Setup button image size for reasonable image size for button container.
let button_image_size = egui::vec2(32_f32, 32_f32);
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
let mut quit = false;
let repaint_after = egui_glium.run(&display, |egui_ctx| {
egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| {
if ui
.add(egui::Button::image_and_text(
texture_id,
button_image_size,
"Quit",
))
.clicked()
{
quit = true;
}
});
egui::Window::new("NativeTextureDisplay").show(egui_ctx, |ui| {
ui.image(texture_id, image_size);
});
});
*control_flow = if quit {
glutin::event_loop::ControlFlow::Exit
} else if repaint_after.is_zero() {
display.gl_window().window().request_redraw();
glutin::event_loop::ControlFlow::Poll
} else if let Some(repaint_after_instant) =
std::time::Instant::now().checked_add(repaint_after)
{
glutin::event_loop::ControlFlow::WaitUntil(repaint_after_instant)
} else {
glutin::event_loop::ControlFlow::Wait
};
{
use glium::Surface as _;
let mut target = display.draw();
let color = egui::Rgba::from_rgb(0.1, 0.3, 0.2);
target.clear_color(color[0], color[1], color[2], color[3]);
// draw things behind egui here
egui_glium.paint(&display, &mut target);
// draw things on top of egui here
target.finish().unwrap();
}
};
match event {
// Platform-dependent event handlers to workaround a winit bug
// See: https://github.com/rust-windowing/winit/issues/987
// See: https://github.com/rust-windowing/winit/issues/1619
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => {
use glutin::event::WindowEvent;
if matches!(event, WindowEvent::CloseRequested | WindowEvent::Destroyed) {
*control_flow = glutin::event_loop::ControlFlow::Exit;
}
let event_response = egui_glium.on_event(&event);
if event_response.repaint {
display.gl_window().window().request_redraw();
}
}
glutin::event::Event::NewEvents(glutin::event::StartCause::ResumeTimeReached {
..
}) => {
display.gl_window().window().request_redraw();
}
_ => (),
}
});
}
fn create_display(event_loop: &glutin::event_loop::EventLoop<()>) -> glium::Display {
let window_builder = glutin::window::WindowBuilder::new()
.with_resizable(true)
.with_inner_size(glutin::dpi::LogicalSize {
width: 800.0,
height: 600.0,
})
.with_title("egui_glium example");
let context_builder = glutin::ContextBuilder::new()
.with_depth_buffer(0)
.with_stencil_buffer(0)
.with_vsync(true);
glium::Display::new(window_builder, context_builder, event_loop).unwrap()
}
fn load_glium_image(png_data: &[u8]) -> glium::texture::RawImage2d<'_, u8> {
// Load image using the image crate:
let image = image::load_from_memory(png_data).unwrap().to_rgba8();
let image_dimensions = image.dimensions();
// Premultiply alpha:
let pixels: Vec<_> = image
.into_vec()
.chunks_exact(4)
.map(|p| egui::Color32::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
.flat_map(|color| color.to_array())
.collect();
// Convert to glium image:
glium::texture::RawImage2d::from_raw_rgba(pixels, image_dimensions)
}

View file

@ -0,0 +1,108 @@
//! Example how to use `egui_glium`.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use glium::glutin;
fn main() {
let event_loop = glutin::event_loop::EventLoopBuilder::with_user_event().build();
let display = create_display(&event_loop);
let mut egui_glium = egui_glium::EguiGlium::new(&display, &event_loop);
let mut color_test = egui_demo_lib::ColorTest::default();
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
let mut quit = false;
let repaint_after = egui_glium.run(&display, |egui_ctx| {
egui::SidePanel::left("my_side_panel").show(egui_ctx, |ui| {
ui.heading("Hello World!");
if ui.button("Quit").clicked() {
quit = true;
}
});
egui::CentralPanel::default().show(egui_ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
color_test.ui(ui);
});
});
});
*control_flow = if quit {
glutin::event_loop::ControlFlow::Exit
} else if repaint_after.is_zero() {
display.gl_window().window().request_redraw();
glutin::event_loop::ControlFlow::Poll
} else if let Some(repaint_after_instant) =
std::time::Instant::now().checked_add(repaint_after)
{
glutin::event_loop::ControlFlow::WaitUntil(repaint_after_instant)
} else {
glutin::event_loop::ControlFlow::Wait
};
{
use glium::Surface as _;
let mut target = display.draw();
let color = egui::Rgba::from_rgb(0.1, 0.3, 0.2);
target.clear_color(color[0], color[1], color[2], color[3]);
// draw things behind egui here
egui_glium.paint(&display, &mut target);
// draw things on top of egui here
target.finish().unwrap();
}
};
match event {
// Platform-dependent event handlers to workaround a winit bug
// See: https://github.com/rust-windowing/winit/issues/987
// See: https://github.com/rust-windowing/winit/issues/1619
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => {
use glutin::event::WindowEvent;
if matches!(event, WindowEvent::CloseRequested | WindowEvent::Destroyed) {
*control_flow = glutin::event_loop::ControlFlow::Exit;
}
let event_response = egui_glium.on_event(&event);
if event_response.repaint {
display.gl_window().window().request_redraw();
}
}
glutin::event::Event::NewEvents(glutin::event::StartCause::ResumeTimeReached {
..
}) => {
display.gl_window().window().request_redraw();
}
_ => (),
}
});
}
fn create_display(event_loop: &glutin::event_loop::EventLoop<()>) -> glium::Display {
let window_builder = glutin::window::WindowBuilder::new()
.with_resizable(true)
.with_inner_size(glutin::dpi::LogicalSize {
width: 800.0,
height: 600.0,
})
.with_title("egui_glium example");
let context_builder = glutin::ContextBuilder::new()
.with_depth_buffer(0)
.with_stencil_buffer(0)
.with_vsync(true);
glium::Display::new(window_builder, context_builder, event_loop).unwrap()
}

View file

@ -0,0 +1,100 @@
//! [`egui`] bindings for [`glium`](https://github.com/glium/glium).
//!
//! The main type you want to use is [`EguiGlium`].
//!
//! If you are writing an app, you may want to look at [`eframe`](https://docs.rs/eframe) instead.
//!
//! ## Feature flags
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
//!
#![allow(clippy::float_cmp)]
#![allow(clippy::manual_range_contains)]
#![forbid(unsafe_code)]
mod painter;
pub use painter::Painter;
pub use egui_winit;
use egui_winit::winit::event_loop::EventLoopWindowTarget;
pub use egui_winit::EventResponse;
// ----------------------------------------------------------------------------
/// Convenience wrapper for using [`egui`] from a [`glium`] app.
pub struct EguiGlium {
pub egui_ctx: egui::Context,
pub egui_winit: egui_winit::State,
pub painter: crate::Painter,
shapes: Vec<egui::epaint::ClippedShape>,
textures_delta: egui::TexturesDelta,
}
impl EguiGlium {
pub fn new<E>(display: &glium::Display, event_loop: &EventLoopWindowTarget<E>) -> Self {
let painter = crate::Painter::new(display);
let mut egui_winit = egui_winit::State::new(event_loop);
egui_winit.set_max_texture_side(painter.max_texture_side());
let pixels_per_point = display.gl_window().window().scale_factor() as f32;
egui_winit.set_pixels_per_point(pixels_per_point);
Self {
egui_ctx: Default::default(),
egui_winit,
painter,
shapes: Default::default(),
textures_delta: Default::default(),
}
}
pub fn on_event(&mut self, event: &glium::glutin::event::WindowEvent<'_>) -> EventResponse {
self.egui_winit.on_event(&self.egui_ctx, event)
}
/// Returns `true` if egui requests a repaint.
///
/// Call [`Self::paint`] later to paint.
pub fn run(
&mut self,
display: &glium::Display,
run_ui: impl FnMut(&egui::Context),
) -> std::time::Duration {
let raw_input = self
.egui_winit
.take_egui_input(display.gl_window().window());
let egui::FullOutput {
platform_output,
repaint_after,
textures_delta,
shapes,
} = self.egui_ctx.run(raw_input, run_ui);
self.egui_winit.handle_platform_output(
display.gl_window().window(),
&self.egui_ctx,
platform_output,
);
self.shapes = shapes;
self.textures_delta.append(textures_delta);
repaint_after
}
/// Paint the results of the last call to [`Self::run`].
pub fn paint<T: glium::Surface>(&mut self, display: &glium::Display, target: &mut T) {
let shapes = std::mem::take(&mut self.shapes);
let textures_delta = std::mem::take(&mut self.textures_delta);
let clipped_primitives = self.egui_ctx.tessellate(shapes);
self.painter.paint_and_update_textures(
display,
target,
self.egui_ctx.pixels_per_point(),
&clipped_primitives,
&textures_delta,
);
}
}

View file

@ -0,0 +1,381 @@
#![allow(deprecated)] // legacy implement_vertex macro
#![allow(semicolon_in_expressions_from_macros)] // glium::program! macro
use egui::{
epaint::{textures::TextureFilter, Primitive},
TextureOptions,
};
use {
egui::{emath::Rect, epaint::Mesh},
glium::{
implement_vertex,
index::PrimitiveType,
texture::{self, srgb_texture2d::SrgbTexture2d},
uniform,
uniforms::{MagnifySamplerFilter, MinifySamplerFilter, SamplerWrapFunction},
},
std::rc::Rc,
};
pub struct Painter {
max_texture_side: usize,
program: glium::Program,
textures: ahash::HashMap<egui::TextureId, EguiTexture>,
/// [`egui::TextureId::User`] index
next_native_tex_id: u64,
}
fn create_program(
facade: &dyn glium::backend::Facade,
vertex_shader: &str,
fragment_shader: &str,
) -> glium::program::Program {
let input = glium::program::ProgramCreationInput::SourceCode {
vertex_shader,
tessellation_control_shader: None,
tessellation_evaluation_shader: None,
geometry_shader: None,
fragment_shader,
transform_feedback_varyings: None,
outputs_srgb: true,
uses_point_size: false,
};
glium::program::Program::new(facade, input)
.unwrap_or_else(|err| panic!("Failed to compile shader: {}", err))
}
impl Painter {
pub fn new(facade: &dyn glium::backend::Facade) -> Painter {
use glium::CapabilitiesSource as _;
let max_texture_side = facade.get_capabilities().max_texture_size as _;
let program = if facade
.get_context()
.is_glsl_version_supported(&glium::Version(glium::Api::Gl, 1, 4))
{
eprintln!("Using GL 1.4");
create_program(
facade,
include_str!("shader/vertex_140.glsl"),
include_str!("shader/fragment_140.glsl"),
)
} else if facade
.get_context()
.is_glsl_version_supported(&glium::Version(glium::Api::Gl, 1, 2))
{
eprintln!("Using GL 1.2");
create_program(
facade,
include_str!("shader/vertex_120.glsl"),
include_str!("shader/fragment_120.glsl"),
)
} else if facade
.get_context()
.is_glsl_version_supported(&glium::Version(glium::Api::GlEs, 3, 0))
{
eprintln!("Using GL ES 3.0");
create_program(
facade,
include_str!("shader/vertex_300es.glsl"),
include_str!("shader/fragment_300es.glsl"),
)
} else if facade
.get_context()
.is_glsl_version_supported(&glium::Version(glium::Api::GlEs, 1, 0))
{
eprintln!("Using GL ES 1.0");
create_program(
facade,
include_str!("shader/vertex_100es.glsl"),
include_str!("shader/fragment_100es.glsl"),
)
} else {
panic!(
"Failed to find a compatible shader for OpenGL version {:?}",
facade.get_version()
)
};
Painter {
max_texture_side,
program,
textures: Default::default(),
next_native_tex_id: 0,
}
}
pub fn max_texture_side(&self) -> usize {
self.max_texture_side
}
pub fn paint_and_update_textures<T: glium::Surface>(
&mut self,
display: &glium::Display,
target: &mut T,
pixels_per_point: f32,
clipped_primitives: &[egui::ClippedPrimitive],
textures_delta: &egui::TexturesDelta,
) {
for (id, image_delta) in &textures_delta.set {
self.set_texture(display, *id, image_delta);
}
self.paint_primitives(display, target, pixels_per_point, clipped_primitives);
for &id in &textures_delta.free {
self.free_texture(id);
}
}
/// Main entry-point for painting a frame.
/// You should call `target.clear_color(..)` before
/// and `target.finish()` after this.
pub fn paint_primitives<T: glium::Surface>(
&mut self,
display: &glium::Display,
target: &mut T,
pixels_per_point: f32,
clipped_primitives: &[egui::ClippedPrimitive],
) {
for egui::ClippedPrimitive {
clip_rect,
primitive,
} in clipped_primitives
{
match primitive {
Primitive::Mesh(mesh) => {
self.paint_mesh(target, display, pixels_per_point, clip_rect, mesh);
}
Primitive::Callback(_) => {
panic!("Custom rendering callbacks are not implemented in egui_glium");
}
}
}
}
#[inline(never)] // Easier profiling
fn paint_mesh<T: glium::Surface>(
&mut self,
target: &mut T,
display: &glium::Display,
pixels_per_point: f32,
clip_rect: &Rect,
mesh: &Mesh,
) {
debug_assert!(mesh.is_valid());
let vertex_buffer = {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
a_pos: [f32; 2],
a_tc: [f32; 2],
a_srgba: [u8; 4],
}
implement_vertex!(Vertex, a_pos, a_tc, a_srgba);
let vertices: &[Vertex] = bytemuck::cast_slice(&mesh.vertices);
// TODO(emilk): we should probably reuse the [`VertexBuffer`] instead of allocating a new one each frame.
glium::VertexBuffer::new(display, vertices).unwrap()
};
// TODO(emilk): we should probably reuse the [`IndexBuffer`] instead of allocating a new one each frame.
let index_buffer =
glium::IndexBuffer::new(display, PrimitiveType::TrianglesList, &mesh.indices).unwrap();
let (width_in_pixels, height_in_pixels) = display.get_framebuffer_dimensions();
let width_in_points = width_in_pixels as f32 / pixels_per_point;
let height_in_points = height_in_pixels as f32 / pixels_per_point;
if let Some(texture) = self.texture(mesh.texture_id) {
// The texture coordinates for text are so that both nearest and linear should work with the egui font texture.
let mag_filter = match texture.options.magnification {
TextureFilter::Nearest => MagnifySamplerFilter::Nearest,
TextureFilter::Linear => MagnifySamplerFilter::Linear,
};
let min_filter = match texture.options.minification {
TextureFilter::Nearest => MinifySamplerFilter::Nearest,
TextureFilter::Linear => MinifySamplerFilter::Linear,
};
let sampler = texture
.glium_texture
.sampled()
.magnify_filter(mag_filter)
.minify_filter(min_filter)
.wrap_function(SamplerWrapFunction::Clamp);
let uniforms = uniform! {
u_screen_size: [width_in_points, height_in_points],
u_sampler: sampler,
};
// egui outputs colors with premultiplied alpha:
let color_blend_func = glium::BlendingFunction::Addition {
source: glium::LinearBlendingFactor::One,
destination: glium::LinearBlendingFactor::OneMinusSourceAlpha,
};
// 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).
let alpha_blend_func = glium::BlendingFunction::Addition {
source: glium::LinearBlendingFactor::OneMinusDestinationAlpha,
destination: glium::LinearBlendingFactor::One,
};
let blend = glium::Blend {
color: color_blend_func,
alpha: alpha_blend_func,
..Default::default()
};
// egui outputs mesh in both winding orders:
let backface_culling = glium::BackfaceCullingMode::CullingDisabled;
// Transform clip rect to physical pixels:
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;
// Make sure clip rect can fit within a `u32`:
let clip_min_x = clip_min_x.clamp(0.0, width_in_pixels as f32);
let clip_min_y = clip_min_y.clamp(0.0, height_in_pixels as f32);
let clip_max_x = clip_max_x.clamp(clip_min_x, width_in_pixels as f32);
let clip_max_y = clip_max_y.clamp(clip_min_y, height_in_pixels as f32);
let clip_min_x = clip_min_x.round() as u32;
let clip_min_y = clip_min_y.round() as u32;
let clip_max_x = clip_max_x.round() as u32;
let clip_max_y = clip_max_y.round() as u32;
let params = glium::DrawParameters {
blend,
backface_culling,
scissor: Some(glium::Rect {
left: clip_min_x,
bottom: height_in_pixels - clip_max_y,
width: clip_max_x - clip_min_x,
height: clip_max_y - clip_min_y,
}),
..Default::default()
};
target
.draw(
&vertex_buffer,
&index_buffer,
&self.program,
&uniforms,
&params,
)
.unwrap();
}
}
// ------------------------------------------------------------------------
pub fn set_texture(
&mut self,
facade: &dyn glium::backend::Facade,
tex_id: egui::TextureId,
delta: &egui::epaint::ImageDelta,
) {
let pixels: Vec<(u8, u8, u8, u8)> = match &delta.image {
egui::ImageData::Color(image) => {
assert_eq!(
image.width() * image.height(),
image.pixels.len(),
"Mismatch between texture size and texel count"
);
image.pixels.iter().map(|color| color.to_tuple()).collect()
}
egui::ImageData::Font(image) => image
.srgba_pixels(None)
.map(|color| color.to_tuple())
.collect(),
};
let glium_image = glium::texture::RawImage2d {
data: std::borrow::Cow::Owned(pixels),
width: delta.image.width() as _,
height: delta.image.height() as _,
format: glium::texture::ClientFormat::U8U8U8U8,
};
let format = texture::SrgbFormat::U8U8U8U8;
let mipmaps = texture::MipmapsOption::NoMipmap;
if let Some(pos) = delta.pos {
// update a sub-region
if let Some(user_texture) = self.textures.get_mut(&tex_id) {
let rect = glium::Rect {
left: pos[0] as _,
bottom: pos[1] as _,
width: glium_image.width,
height: glium_image.height,
};
user_texture
.glium_texture
.main_level()
.write(rect, glium_image);
user_texture.options = delta.options;
}
} else {
let gl_texture =
SrgbTexture2d::with_format(facade, glium_image, format, mipmaps).unwrap();
let user_texture = EguiTexture::new(gl_texture.into(), delta.options);
self.textures.insert(tex_id, user_texture);
}
}
pub fn free_texture(&mut self, tex_id: egui::TextureId) {
self.textures.remove(&tex_id);
}
fn texture(&self, texture_id: egui::TextureId) -> Option<&EguiTexture> {
self.textures.get(&texture_id)
}
pub fn register_native_texture(
&mut self,
native: Rc<SrgbTexture2d>,
options: TextureOptions,
) -> egui::TextureId {
let id = egui::TextureId::User(self.next_native_tex_id);
self.next_native_tex_id += 1;
let texture = EguiTexture::new(native, options);
self.textures.insert(id, texture);
id
}
pub fn replace_native_texture(
&mut self,
id: egui::TextureId,
replacing: Rc<SrgbTexture2d>,
options: TextureOptions,
) {
let texture = EguiTexture::new(replacing, options);
self.textures.insert(id, texture);
}
}
struct EguiTexture {
glium_texture: Rc<SrgbTexture2d>,
options: TextureOptions,
}
impl EguiTexture {
fn new(glium_texture: Rc<SrgbTexture2d>, options: TextureOptions) -> Self {
Self {
glium_texture,
options,
}
}
}

View file

@ -0,0 +1,38 @@
#version 100
precision mediump float;
uniform sampler2D u_sampler;
varying vec4 v_rgba_gamma; // 0-1 gamma sRGBA
varying vec2 v_tc;
// 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(3294.6);
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
return mix(higher, lower, vec3(cutoff));
}
vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
}
// 0-1 linear from 0-255 sRGB
vec3 linear_from_srgb(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
vec3 lower = srgb / vec3(3294.6);
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
return mix(higher, lower, vec3(cutoff));
}
vec4 linear_from_srgba(vec4 srgba) {
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
}
void main() {
// WebGL doesn't come with sRGBA textures:
vec4 texture_in_gamma = texture2D(u_sampler, v_tc);
// Multiply vertex color with texture color (in gamma space).
gl_FragColor = v_rgba_gamma * texture_in_gamma;
}

View file

@ -0,0 +1,31 @@
#version 120
uniform sampler2D u_sampler;
varying vec4 v_rgba_gamma; // 0-1 gamma sRGBA
varying vec2 v_tc;
// 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(3294.6);
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
return mix(higher, lower, vec3(cutoff));
}
// 0-255 sRGBA from 0-1 linear
vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
}
// 0-1 gamma from 0-1 linear
vec4 gamma_from_linear_rgba(vec4 linear_rgba) {
return vec4(srgb_from_linear(linear_rgba.rgb) / 255.0, linear_rgba.a);
}
void main() {
// The texture is set up with `SRGB8_ALPHA8`
vec4 texture_in_gamma = gamma_from_linear_rgba(texture2D(u_sampler, v_tc));
// Multiply vertex color with texture color (in gamma space).
gl_FragColor = v_rgba_gamma * texture_in_gamma;
}

View file

@ -0,0 +1,32 @@
#version 140
uniform sampler2D u_sampler;
in vec4 v_rgba_gamma;
in vec2 v_tc;
out vec4 f_color;
// 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(3294.6);
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
return mix(higher, lower, vec3(cutoff));
}
// 0-255 sRGBA from 0-1 linear
vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
}
// 0-1 gamma from 0-1 linear
vec4 gamma_from_linear_rgba(vec4 linear_rgba) {
return vec4(srgb_from_linear(linear_rgba.rgb) / 255.0, linear_rgba.a);
}
void main() {
// The texture is set up with `SRGB8_ALPHA8`
vec4 texture_in_gamma = gamma_from_linear_rgba(texture(u_sampler, v_tc));
// Multiply vertex color with texture color (in gamma space).
f_color = v_rgba_gamma * texture_in_gamma;
}

View file

@ -0,0 +1,32 @@
#version 300 es
precision mediump float;
uniform sampler2D u_sampler;
varying vec4 v_rgba_gamma; // 0-1 gamma sRGBA
varying vec2 v_tc;
// 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(3294.6);
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
return mix(higher, lower, vec3(cutoff));
}
// 0-255 sRGBA from 0-1 linear
vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
}
// 0-1 gamma from 0-1 linear
vec4 gamma_from_linear_rgba(vec4 linear_rgba) {
return vec4(srgb_from_linear(linear_rgba.rgb) / 255.0, linear_rgba.a);
}
void main() {
// The texture is set up with `SRGB8_ALPHA8`
vec4 texture_in_gamma = gamma_from_linear_rgba(texture2D(u_sampler, v_tc));
// Multiply vertex color with texture color (in gamma space).
gl_FragColor = v_rgba_gamma * texture_in_gamma;
}

View file

@ -0,0 +1,19 @@
#version 100
precision mediump float;
uniform vec2 u_screen_size;
attribute vec2 a_pos;
attribute vec2 a_tc;
attribute vec4 a_srgba;
varying vec4 v_rgba_gamma; // 0-1 gamma sRGBA
varying vec2 v_tc;
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_rgba_gamma = a_srgba / 255.0;
v_tc = a_tc;
}

View file

@ -0,0 +1,18 @@
#version 120
uniform vec2 u_screen_size;
attribute vec2 a_pos;
attribute vec4 a_srgba; // 0-255 sRGB
attribute vec2 a_tc;
varying vec4 v_rgba_gamma; // 0-1 gamma sRGBA
varying vec2 v_tc;
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_rgba_gamma = a_srgba / 255.0;
v_tc = a_tc;
}

View file

@ -0,0 +1,18 @@
#version 140
uniform vec2 u_screen_size;
in vec2 a_pos;
in vec4 a_srgba; // 0-255 sRGB
in vec2 a_tc;
out vec4 v_rgba_gamma;
out vec2 v_tc;
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_rgba_gamma = a_srgba / 255.0;
v_tc = a_tc;
}

View file

@ -0,0 +1,19 @@
#version 300 es
precision mediump float;
uniform vec2 u_screen_size;
attribute vec2 a_pos;
attribute vec2 a_tc;
attribute vec4 a_srgba;
varying vec4 v_rgba_gamma; // 0-1 gamma sRGBA
varying vec2 v_tc;
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_rgba_gamma = a_srgba / 255.0;
v_tc = a_tc;
}