Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The big 2018 cleanup #507

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27,208 changes: 27,208 additions & 0 deletions NIL

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions gl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gl"
version = "0.14.0"
version = "0.15.0"
authors = [
"Brendan Zabarauskas <bjzaba@yahoo.com.au>",
"Corey Richardson",
Expand All @@ -15,9 +15,10 @@ repository = "https://github.com/brendanzab/gl-rs/"
readme = "README.md"
categories = ["api-bindings", "rendering::graphics-api"]
keywords = ["gl", "egl", "opengl", "khronos"]
edition = "2018"

[build-dependencies]
gl_generator = { version = "0.14.0", path = "../gl_generator" }
gl_generator = { version = "0.15.0", path = "../gl_generator" }

[dev-dependencies]
glutin = "0.21.0"
4 changes: 2 additions & 2 deletions gl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let mut file = File::create(&Path::new(&out_dir).join("bindings.rs")).unwrap();

Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, [])
.write_bindings(GlobalGenerator, &mut file)
Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::Yes, [])
.write_bindings(GlobalGenerator::default(), &mut file)
.unwrap();
}
7 changes: 5 additions & 2 deletions gl/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.


extern crate gl;
extern crate glutin;

Expand All @@ -26,8 +27,10 @@ fn main() {
let gl_window = unsafe { gl_window.make_current().unwrap() };

// Load the OpenGL function pointers
// TODO: `as *const _` will not be needed once glutin is updated to the latest gl version
gl::load_with(|symbol| gl_window.get_proc_address(symbol) as *const _);
unsafe { gl::load_with(|symbol| {
let c_str = std::ffi::CStr::from_ptr(symbol);
gl_window.get_proc_address(c_str.to_str().unwrap()) as *const gl::c_void
}) };

events_loop.run_forever(|event| {
use glutin::{ControlFlow, Event, WindowEvent};
Expand Down
6 changes: 4 additions & 2 deletions gl/examples/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ fn main() {
let gl_window = unsafe { gl_window.make_current() }.unwrap();

// Load the OpenGL function pointers
// TODO: `as *const _` will not be needed once glutin is updated to the latest gl version
gl::load_with(|symbol| gl_window.get_proc_address(symbol) as *const _);
unsafe { gl::load_with(|symbol| {
let c_str = std::ffi::CStr::from_ptr(symbol);
gl_window.get_proc_address(c_str.to_str().unwrap()) as *const gl::c_void
}) };

// Create GLSL shaders
let vs = compile_shader(VS_SRC, gl::VERTEX_SHADER);
Expand Down
7 changes: 7 additions & 0 deletions gl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@
#![crate_name = "gl"]
#![crate_type = "lib"]

#![allow(bad_style)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::let_unit_value)]
#![allow(clippy::let_and_return)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
4 changes: 3 additions & 1 deletion gl_generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "gl_generator"
version = "0.14.0"
version = "0.15.0"
authors = [
"Brendan Zabarauskas <bjzaba@yahoo.com.au>",
"Corey Richardson",
"Arseny Kapoulkine",
"Lokathor <zefria@gmail.com>",
]
description = "Code generators for creating bindings to the Khronos OpenGL APIs."
license = "Apache-2.0"
Expand All @@ -14,6 +15,7 @@ repository = "https://github.com/brendanzab/gl-rs/"
readme = "README.md"
categories = ["api-bindings", "rendering::graphics-api"]
keywords = ["gl", "egl", "opengl", "khronos"]
edition = "2018"

[lib]
name = "gl_generator"
Expand Down
4 changes: 2 additions & 2 deletions gl_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ 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)
Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::Yes, [])
.write_bindings(GlobalGenerator::default(), &mut file)
.unwrap();
}
```
Expand Down
99 changes: 71 additions & 28 deletions gl_generator/generators/mod.rs → gl_generator/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use registry::{Cmd, Enum, Registry};
// some stuff in here is dead if you don't turn on the unstable feature.
#![allow(dead_code)]

use crate::registry::{Api, Cmd, Enum, Registry};
use std::io;
use Api;

pub mod debug_struct_gen;
pub mod global_gen;
pub mod static_gen;
pub mod static_struct_gen;
Expand All @@ -27,9 +28,7 @@ pub mod struct_gen;
/// See https://github.com/brendanzab/gl-rs/tree/master/gl_generator#generator-types
pub trait Generator {
/// Builds the GL bindings.
fn write<W>(&self, registry: &Registry, dest: &mut W) -> io::Result<()>
where
W: io::Write;
fn write(&self, registry: &Registry, dest: &mut dyn io::Write) -> io::Result<()>;
}

pub fn gen_struct_name(api: Api) -> &'static str {
Expand All @@ -46,41 +45,55 @@ pub fn gen_struct_name(api: Api) -> &'static str {
}

/// This function generates a `const name: type = value;` item.
pub fn gen_enum_item<W>(enm: &Enum, types_prefix: &str, dest: &mut W) -> io::Result<()>
where
W: io::Write,
{
writeln!(dest,
"#[allow(dead_code, non_upper_case_globals)] pub const {ident}: {types_prefix}{ty} = {value}{cast_suffix};",
pub fn gen_enum_item(enm: &Enum, types_prefix: &str, dest: &mut dyn io::Write) -> io::Result<()> {
// TODO: remove the types_prefix thing? Seems silly, doesn't seem user
// configurable.
writeln!(
dest,
"pub const {ident}: {types_prefix}{ty} = {value}{cast_suffix};",
ident = enm.ident,
types_prefix = if enm.ty == "&'static str" { "" } else { types_prefix },
types_prefix = if enm.ty == "&'static str" {
""
} else {
types_prefix
},
ty = enm.ty,
value = enm.value,
cast_suffix = match enm.cast {
true => format!(" as {}{}", types_prefix, enm.ty),
false => String::new(),
cast_suffix = if enm.cast {
format!(" as {}{}", types_prefix, enm.ty)
} else {
String::new()
},
)
}

/// Generates all the type aliases for a namespace.
///
/// Aliases are either `pub type = ...` or `#[repr(C)] pub struct ... { ... }` and contain all the
/// things that we can't obtain from the XML files.
pub fn gen_types<W>(api: Api, dest: &mut W) -> io::Result<()>
where
W: io::Write,
{
/// Aliases are either `pub type = ...` or `#[repr(C)] pub struct ... { ... }`
/// and contain all the things that we can't obtain from the XML files.
pub fn gen_types(api: Api, dest: &mut dyn io::Write) -> io::Result<()> {
if let Api::Egl = api {
try!(writeln!(dest, "{}", include_str!("templates/types/egl.rs")));
writeln!(
dest,
"{}",
include_str!("generators/templates/types/egl.rs")
)?;
return Ok(());
}

try!(writeln!(dest, "{}", include_str!("templates/types/gl.rs")));
writeln!(dest, "{}", include_str!("generators/templates/types/gl.rs"))?;

match api {
Api::Glx => try!(writeln!(dest, "{}", include_str!("templates/types/glx.rs"))),
Api::Wgl => try!(writeln!(dest, "{}", include_str!("templates/types/wgl.rs"))),
Api::Glx => writeln!(
dest,
"{}",
include_str!("generators/templates/types/glx.rs")
)?,
Api::Wgl => writeln!(
dest,
"{}",
include_str!("generators/templates/types/wgl.rs")
)?,
_ => {},
}

Expand All @@ -96,9 +109,9 @@ pub fn gen_parameters(cmd: &Cmd, with_idents: bool, with_types: bool) -> Vec<Str
if with_idents && with_types {
format!("{}: {}", binding.ident, binding.ty)
} else if with_types {
format!("{}", binding.ty)
binding.ty.to_string()
} else if with_idents {
format!("{}", binding.ident)
binding.ident.to_string()
} else {
panic!()
}
Expand All @@ -117,3 +130,33 @@ pub fn gen_symbol_name(api: Api, cmd: &str) -> String {
Api::Egl => format!("egl{}", cmd),
}
}

/// Writes all types into their own sub-module.
pub fn write_type_aliases(registry: &Registry, dest: &mut dyn io::Write) -> io::Result<()> {
writeln!(
dest,
r#"pub use types::*;
pub mod types {{
use super::*;"#
)?;

gen_types(registry.api(), dest)?;

writeln!(dest, "}}")
}

/// Writes all consts into their own sub-module.
fn write_enums(registry: &Registry, dest: &mut dyn io::Write) -> io::Result<()> {
writeln!(
dest,
r#"pub use consts::*;
pub mod consts {{
use super::*;"#
)?;

for enm in registry.enums() {
gen_enum_item(enm, "", dest)?;
}

writeln!(dest, "}}")
}
Loading