implement EGL based render for termwindow/opengl mode on linux
This commit is contained in:
1 parent
0e95f00a52
commit
7c2afce851
5 files changed
+227
-33
No files matched your search
@@ -1,4 +1,3 @@
|
||||
#version 330
|
||||
precision mediump float;
|
||||
|
||||
in vec2 o_tex;
|
||||
|
||||
@@ -395,17 +395,32 @@ impl OpenGLRenderState {
|
||||
let glyph_cache = RefCell::new(GlyphCache::new_gl(&context, fonts, size)?);
|
||||
let util_sprites = UtilSprites::new(&mut *glyph_cache.borrow_mut(), metrics)?;
|
||||
|
||||
let source = glium::program::ProgramCreationInput::SourceCode {
|
||||
vertex_shader: Self::vertex_shader(),
|
||||
fragment_shader: Self::fragment_shader(),
|
||||
outputs_srgb: true,
|
||||
tessellation_control_shader: None,
|
||||
tessellation_evaluation_shader: None,
|
||||
transform_feedback_varyings: None,
|
||||
uses_point_size: false,
|
||||
geometry_shader: None,
|
||||
};
|
||||
let program = glium::Program::new(&context, source)?;
|
||||
let mut errors = vec![];
|
||||
let mut program = None;
|
||||
for version in &["330", "300 es"] {
|
||||
let source = glium::program::ProgramCreationInput::SourceCode {
|
||||
vertex_shader: &Self::vertex_shader(version),
|
||||
fragment_shader: &Self::fragment_shader(version),
|
||||
outputs_srgb: true,
|
||||
tessellation_control_shader: None,
|
||||
tessellation_evaluation_shader: None,
|
||||
transform_feedback_varyings: None,
|
||||
uses_point_size: false,
|
||||
geometry_shader: None,
|
||||
};
|
||||
log::error!("compiling a prog with version {}", version);
|
||||
match glium::Program::new(&context, source) {
|
||||
Ok(prog) => {
|
||||
program = Some(prog);
|
||||
break;
|
||||
}
|
||||
Err(err) => errors.push(err.to_string()),
|
||||
};
|
||||
}
|
||||
|
||||
let program = program.ok_or_else(|| {
|
||||
failure::format_err!("Failed to compile shaders: {}", errors.join("\n"))
|
||||
})?;
|
||||
|
||||
let (glyph_vertex_buffer, glyph_index_buffer) =
|
||||
Self::compute_vertices(&context, metrics, pixel_width as f32, pixel_height as f32)?;
|
||||
@@ -438,12 +453,12 @@ impl OpenGLRenderState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn vertex_shader() -> &'static str {
|
||||
include_str!("vertex.glsl")
|
||||
fn vertex_shader(version: &str) -> String {
|
||||
format!("#version {}\n{}", version, include_str!("vertex.glsl"))
|
||||
}
|
||||
|
||||
fn fragment_shader() -> &'static str {
|
||||
include_str!("fragment.glsl")
|
||||
fn fragment_shader(version: &str) -> String {
|
||||
format!("#version {}\n{}", version, include_str!("fragment.glsl"))
|
||||
}
|
||||
|
||||
/// Compute a vertex buffer to hold the quads that comprise the visible
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#version 330
|
||||
|
||||
precision mediump float;
|
||||
in vec2 position;
|
||||
in vec2 adjust;
|
||||
in vec2 tex;
|
||||
|
||||
+155
-6
@@ -45,6 +45,9 @@ struct EglWrapper {
|
||||
|
||||
pub struct GlState {
|
||||
egl: EglWrapper,
|
||||
display: ffi::types::EGLDisplay,
|
||||
surface: ffi::types::EGLSurface,
|
||||
context: ffi::types::EGLContext,
|
||||
}
|
||||
|
||||
type GetProcAddressFunc =
|
||||
@@ -111,10 +114,95 @@ impl EglWrapper {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn choose_config(
|
||||
&self,
|
||||
display: ffi::types::EGLDisplay,
|
||||
attributes: &[u32],
|
||||
) -> Fallible<Vec<ffi::types::EGLConfig>> {
|
||||
failure::ensure!(
|
||||
attributes.len() > 0 && attributes[attributes.len() - 1] == ffi::NONE,
|
||||
"attributes list must be terminated with ffi::NONE"
|
||||
);
|
||||
|
||||
let mut num_configs = 0;
|
||||
if unsafe {
|
||||
self.egl
|
||||
.GetConfigs(display, std::ptr::null_mut(), 0, &mut num_configs)
|
||||
} != 1
|
||||
{
|
||||
return Err(self.error("egl GetConfigs to count possible number of configurations"));
|
||||
}
|
||||
|
||||
let mut configs = vec![std::ptr::null(); num_configs as usize];
|
||||
|
||||
if unsafe {
|
||||
self.egl.ChooseConfig(
|
||||
display,
|
||||
attributes.as_ptr() as *const ffi::EGLint,
|
||||
configs.as_mut_ptr(),
|
||||
configs.len() as ffi::EGLint,
|
||||
&mut num_configs,
|
||||
)
|
||||
} != 1
|
||||
{
|
||||
return Err(self.error("egl ChooseConfig to select configurations"));
|
||||
}
|
||||
|
||||
configs.resize(num_configs as usize, std::ptr::null());
|
||||
|
||||
Ok(configs)
|
||||
}
|
||||
|
||||
pub fn create_window_surface(
|
||||
&self,
|
||||
display: ffi::types::EGLDisplay,
|
||||
config: ffi::types::EGLConfig,
|
||||
window: ffi::EGLNativeWindowType,
|
||||
) -> Fallible<ffi::types::EGLSurface> {
|
||||
let surface = unsafe {
|
||||
self.egl
|
||||
.CreateWindowSurface(display, config, window, std::ptr::null())
|
||||
};
|
||||
if surface.is_null() {
|
||||
Err(self.error("EGL CreateWindowSurface"))
|
||||
} else {
|
||||
Ok(surface)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_context(
|
||||
&self,
|
||||
display: ffi::types::EGLDisplay,
|
||||
config: ffi::types::EGLConfig,
|
||||
share_context: ffi::types::EGLContext,
|
||||
attributes: &[u32],
|
||||
) -> Fallible<ffi::types::EGLConfig> {
|
||||
failure::ensure!(
|
||||
attributes.len() > 0 && attributes[attributes.len() - 1] == ffi::NONE,
|
||||
"attributes list must be terminated with ffi::NONE"
|
||||
);
|
||||
let context = unsafe {
|
||||
self.egl.CreateContext(
|
||||
display,
|
||||
config,
|
||||
share_context,
|
||||
attributes.as_ptr() as *const i32,
|
||||
)
|
||||
};
|
||||
if context.is_null() {
|
||||
Err(self.error("EGL CreateContext"))
|
||||
} else {
|
||||
Ok(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GlState {
|
||||
pub fn create(display: Option<ffi::EGLNativeDisplayType>) -> Fallible<Self> {
|
||||
pub fn create(
|
||||
display: Option<ffi::EGLNativeDisplayType>,
|
||||
window: ffi::EGLNativeWindowType,
|
||||
) -> Fallible<Self> {
|
||||
let paths = [
|
||||
// While EGL is cross platform, it isn't available on macOS nor is it
|
||||
// available on my nvidia based system
|
||||
@@ -136,7 +224,48 @@ impl GlState {
|
||||
let (major, minor) = egl.initialize_and_get_version(egl_display)?;
|
||||
eprintln!("initialized EGL version {}.{}", major, minor);
|
||||
|
||||
return Ok(Self { egl });
|
||||
let configs = egl.choose_config(
|
||||
egl_display,
|
||||
&[
|
||||
ffi::ALPHA_SIZE,
|
||||
8,
|
||||
ffi::RED_SIZE,
|
||||
8,
|
||||
ffi::GREEN_SIZE,
|
||||
8,
|
||||
ffi::BLUE_SIZE,
|
||||
8,
|
||||
ffi::DEPTH_SIZE,
|
||||
24,
|
||||
ffi::CONFORMANT,
|
||||
ffi::OPENGL_ES3_BIT,
|
||||
ffi::RENDERABLE_TYPE,
|
||||
ffi::OPENGL_ES3_BIT,
|
||||
ffi::SURFACE_TYPE,
|
||||
ffi::WINDOW_BIT | ffi::PBUFFER_BIT | ffi::PIXMAP_BIT,
|
||||
ffi::NONE,
|
||||
],
|
||||
)?;
|
||||
|
||||
let first_config = *configs.first().ok_or_else(|| {
|
||||
failure::err_msg("no compatible EGL configuration was found")
|
||||
})?;
|
||||
|
||||
let surface = egl.create_window_surface(egl_display, first_config, window)?;
|
||||
|
||||
let context = egl.create_context(
|
||||
egl_display,
|
||||
first_config,
|
||||
std::ptr::null(),
|
||||
&[ffi::CONTEXT_MAJOR_VERSION, 3, ffi::NONE],
|
||||
)?;
|
||||
|
||||
return Ok(Self {
|
||||
egl,
|
||||
display: egl_display,
|
||||
context,
|
||||
surface,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,7 +275,12 @@ impl GlState {
|
||||
|
||||
unsafe impl glium::backend::Backend for GlState {
|
||||
fn swap_buffers(&self) -> Result<(), glium::SwapBuffersError> {
|
||||
unimplemented!();
|
||||
let res = unsafe { self.egl.egl.SwapBuffers(self.display, self.surface) };
|
||||
if res != 1 {
|
||||
Err(glium::SwapBuffersError::AlreadySwapped)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
|
||||
@@ -155,14 +289,29 @@ unsafe impl glium::backend::Backend for GlState {
|
||||
}
|
||||
|
||||
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
|
||||
unimplemented!();
|
||||
let mut width = 0;
|
||||
let mut height = 0;
|
||||
|
||||
unsafe {
|
||||
self.egl
|
||||
.egl
|
||||
.QuerySurface(self.display, self.surface, ffi::WIDTH as i32, &mut width);
|
||||
}
|
||||
unsafe {
|
||||
self.egl
|
||||
.egl
|
||||
.QuerySurface(self.display, self.surface, ffi::HEIGHT as i32, &mut height);
|
||||
}
|
||||
(width as u32, height as u32)
|
||||
}
|
||||
|
||||
fn is_current(&self) -> bool {
|
||||
unimplemented!();
|
||||
unsafe { self.egl.egl.GetCurrentContext() == self.context }
|
||||
}
|
||||
|
||||
unsafe fn make_current(&self) {
|
||||
unimplemented!();
|
||||
self.egl
|
||||
.egl
|
||||
.MakeCurrent(self.display, self.surface, self.surface, self.context);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,8 @@ pub(crate) struct WindowInner {
|
||||
paint_all: bool,
|
||||
buffer_image: BufferImage,
|
||||
cursor: Option<MouseCursor>,
|
||||
#[cfg(feature = "opengl")]
|
||||
gl_state: Option<Rc<glium::backend::Context>>,
|
||||
}
|
||||
|
||||
fn enclosing_boundary_with(a: &Rect, b: &Rect) -> Rect {
|
||||
@@ -92,6 +94,22 @@ impl WindowInner {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
#[cfg(feature = "opengl")]
|
||||
{
|
||||
if let Some(gl_context) = self.gl_state.as_ref() {
|
||||
self.expose.clear();
|
||||
|
||||
let mut frame = glium::Frame::new(
|
||||
Rc::clone(&gl_context),
|
||||
(self.width as u32, self.height as u32),
|
||||
);
|
||||
|
||||
self.callbacks.paint_opengl(&mut frame);
|
||||
frame.finish()?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let (buf_width, buf_height) = self.buffer_image.image_dimensions();
|
||||
if buf_width != self.width.into() || buf_height != self.height.into() {
|
||||
// Window was resized, so we need to update our buffer
|
||||
@@ -401,6 +419,8 @@ impl Window {
|
||||
paint_all: true,
|
||||
buffer_image,
|
||||
cursor: None,
|
||||
#[cfg(feature = "opengl")]
|
||||
gl_state: None,
|
||||
}))
|
||||
};
|
||||
|
||||
@@ -499,15 +519,27 @@ impl WindowOps for Window {
|
||||
{
|
||||
Connection::with_window_inner(self.0, move |inner| {
|
||||
let window = Window(inner.window_id);
|
||||
match crate::egl::GlState::create(Some(inner.conn.display as *const _)) {
|
||||
Ok(_) => eprintln!("EGL initialized!?"),
|
||||
Err(err) => eprintln!("EGL: {}", err),
|
||||
};
|
||||
func(
|
||||
inner.callbacks.as_any(),
|
||||
&window,
|
||||
Err(failure::err_msg("opengl not supported in this build")),
|
||||
);
|
||||
|
||||
let gl_state = crate::egl::GlState::create(
|
||||
Some(inner.conn.display as *const _),
|
||||
inner.window_id as *mut _,
|
||||
)
|
||||
.map(Rc::new)
|
||||
.and_then(|state| unsafe {
|
||||
Ok(glium::backend::Context::new(
|
||||
Rc::clone(&state),
|
||||
true,
|
||||
if cfg!(debug_assertions) {
|
||||
glium::debug::DebugCallbackBehavior::DebugMessageOnError
|
||||
} else {
|
||||
glium::debug::DebugCallbackBehavior::Ignore
|
||||
},
|
||||
)?)
|
||||
});
|
||||
|
||||
inner.gl_state = gl_state.as_ref().map(Rc::clone).ok();
|
||||
|
||||
func(inner.callbacks.as_any(), &window, gl_state);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user