gl-generator

This commit is contained in:
Michael Zhang 2020-06-30 02:19:48 -05:00
parent 1b17246d8d
commit 1a9624a80e
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
5 changed files with 57 additions and 1 deletions

View file

@ -14,7 +14,7 @@ all-features = true
[features] [features]
default = ["xlib"] default = ["xlib"]
glx = ["x11/glx", "xlib"] glx = ["x11/glx", "xlib", "gl_generator"]
xlib = ["x11/xlib"] xlib = ["x11/xlib"]
xrender = ["x11/xrender"] xrender = ["x11/xrender"]
xinerama = ["x11/xinerama", "xlib"] xinerama = ["x11/xinerama", "xlib"]
@ -26,3 +26,6 @@ x11 = { version = "2.18" }
thiserror = "1.0.20" thiserror = "1.0.20"
log = "0.4.8" log = "0.4.8"
bitflags = "1.2.1" bitflags = "1.2.1"
[build-dependencies]
gl_generator = { version = "0.14.0", optional = true }

14
build.rs Normal file
View file

@ -0,0 +1,14 @@
use std::env;
use std::fs::File;
use std::path::Path;
use gl_generator::{Api, Fallbacks, GlobalGenerator, Profile, Registry};
fn main() {
let dest = env::var("OUT_DIR").unwrap();
let mut file = File::create(&Path::new(&dest).join("bindings.rs")).unwrap();
Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, [])
.write_bindings(GlobalGenerator, &mut file)
.unwrap();
}

20
src/glx/context.rs Normal file
View file

@ -0,0 +1,20 @@
use x11::glx;
use crate::xlib::Display;
pub struct GlxContext<'a> {
pub(super) display: &'a Display,
pub(super) ctx: glx::GLXContext,
}
impl<'a> GlxContext<'a> {
pub fn as_raw(&self) -> glx::GLXContext {
self.ctx
}
}
impl<'a> Drop for GlxContext<'a> {
fn drop(&mut self) {
unsafe { glx::glXDestroyContext(self.display.as_raw(), self.ctx) };
}
}

View file

@ -1,10 +1,16 @@
mod context;
use x11::glx; use x11::glx;
use crate::errors::Result; use crate::errors::Result;
use crate::xlib::Display; use crate::xlib::Display;
pub use self::context::GlxContext;
pub trait GlxExtension { pub trait GlxExtension {
fn query_glx_extension(&self) -> Result<bool>; fn query_glx_extension(&self) -> Result<bool>;
fn create_context(&self, share_list: Option<&GlxContext>, direct: bool) -> Result<GlxContext>;
} }
impl GlxExtension for Display { impl GlxExtension for Display {
@ -12,4 +18,13 @@ impl GlxExtension for Display {
let result = unsafe { glx::glXQueryExtension(self.inner, 0 as *mut _, 0 as *mut _) }; let result = unsafe { glx::glXQueryExtension(self.inner, 0 as *mut _, 0 as *mut _) };
Ok(result == 1) Ok(result == 1)
} }
fn create_context(&self, share_list: Option<&GlxContext>, direct: bool) -> Result<GlxContext> {
let share_list = share_list
.map(|ctx| ctx.as_raw())
.unwrap_or_else(|| 0 as *mut _);
let ctx =
unsafe { glx::glXCreateContext(self.as_raw(), 0 as *mut _, share_list, direct.into()) };
Ok(GlxContext { display: self, ctx })
}
} }

View file

@ -9,6 +9,10 @@ extern crate log;
pub extern crate x11; pub extern crate x11;
#[cfg(feature = "glx")]
mod gl {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[cfg(feature = "glx")] #[cfg(feature = "glx")]
pub mod glx; pub mod glx;