Skip to content

Commit

Permalink
Merge pull request #1398 from bitshifter/glinfo
Browse files Browse the repository at this point in the history
Add additional GL context information to capabilities struct
  • Loading branch information
tomaka committed Jan 8, 2016
2 parents 2e00809 + bdde4a7 commit 8a7a1f9
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
55 changes: 55 additions & 0 deletions examples/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[macro_use]
extern crate glium;

fn main() {
use glium::{Api, CapabilitiesSource, DisplayBuild, Profile, Version};
use glium::backend::Facade;
use glium::glutin;

// building the display, ie. the main object
let display = glutin::WindowBuilder::new()
.with_visibility(false)
.build_glium()
.unwrap();

let version = *display.get_opengl_version();
let api = match version {
Version(Api::Gl, _, _) => "OpenGL",
Version(Api::GlEs, _, _) => "OpenGL ES"
};

let caps = display.get_context().get_capabilities();

println!("{} context verson: {}", api, caps.version);

print!("{} context flags:", api);
if caps.forward_compatible {
print!(" forward-compatible");
}
if caps.debug {
print!(" debug");
}
if caps.robustness {
print!(" robustness");
}
print!("\n");

if version >= Version(Api::Gl, 3, 2) {
println!("{} profile mask: {}", api,
match caps.profile {
Some(Profile::Core) => "core",
Some(Profile::Compatibility) => "compatibility",
None => "unknown"
});
}

println!("{} robustness strategy: {}", api,
if caps.can_lose_context {
"lose"
} else {
"none"
});

println!("{} context renderer: {}", api, caps.renderer);
println!("{} context vendor: {}", api, caps.vendor);
}
81 changes: 81 additions & 0 deletions src/context/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ use ToGlEnum;
use CapabilitiesSource;
use image_format::TextureFormat;

/// Describes the OpenGL context profile.
#[derive(Debug)]
pub enum Profile {
/// The context uses only future-compatible functions and definitions.
Core,
/// The context includes all immediate mode functions and definitions.
Compatibility
}

/// Represents the capabilities of the context.
///
/// Contrary to the state, these values never change.
Expand All @@ -23,6 +32,30 @@ pub struct Capabilities {
/// An empty list means that the backend doesn't have a compiler.
pub supported_glsl_versions: Vec<Version>,

/// Returns a version or release number. Vendor-specific information may follow the version
/// number.
pub version: String,

/// The company responsible for this GL implementation.
pub vendor: String,

/// The name of the renderer. This name is typically specific to a particular
/// configuration of a hardware platform.
pub renderer: String,

/// The OpenGL context profile if available.
///
/// The context profile is available from OpenGL 3.2 onwards. `None` if not supported.
pub profile: Option<Profile>,

/// The context is in debug mode, which may have additional error and performance issue
/// reporting functionality.
pub debug: bool,

/// The context is in "forward-compatible" mode, which means that no deprecated functionality
/// will be supported.
pub forward_compatible: bool,

/// True if out-of-bound access on the GPU side can't result in crashes.
pub robustness: bool,

Expand Down Expand Up @@ -132,6 +165,17 @@ pub enum ReleaseBehavior {
pub unsafe fn get_capabilities(gl: &gl::Gl, version: &Version, extensions: &ExtensionsList)
-> Capabilities
{
// GL_CONTEXT_FLAGS are only avaialble from GL 3.0 onwards
let (debug, forward_compatible) = if version >= &Version(Api::Gl, 3, 0) {
let mut val = mem::uninitialized();
gl.GetIntegerv(gl::CONTEXT_FLAGS, &mut val);
let val = val as gl::types::GLenum;
((val & gl::CONTEXT_FLAG_DEBUG_BIT) != 0,
(val & gl::CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT) != 0)
} else {
(false, false)
};

// getting the value of `GL_RENDERER`
let renderer = {
let s = gl.GetString(gl::RENDERER);
Expand All @@ -145,6 +189,41 @@ pub unsafe fn get_capabilities(gl: &gl::Gl, version: &Version, extensions: &Exte
get_supported_glsl(gl, version, extensions)
},

version: {
let s = gl.GetString(gl::VERSION);
assert!(!s.is_null());
String::from_utf8(CStr::from_ptr(s as *const _).to_bytes().to_vec()).ok()
.expect("glGetString(GL_VERSION) returned a non-UTF8 string")
},

vendor: {
let s = gl.GetString(gl::VENDOR);
assert!(!s.is_null());
String::from_utf8(CStr::from_ptr(s as *const _).to_bytes().to_vec()).ok()
.expect("glGetString(GL_VENDOR) returned a non-UTF8 string")
},

profile: {
if version >= &Version(Api::Gl, 3, 2) {
let mut val = mem::uninitialized();
gl.GetIntegerv(gl::CONTEXT_PROFILE_MASK, &mut val);
let val = val as gl::types::GLenum;
if (val & gl::CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0 {
Some(Profile::Compatibility)
} else if (val & gl::CONTEXT_CORE_PROFILE_BIT) != 0 {
Some(Profile::Core)
} else {
None
}
} else {
None
}
},

debug: debug,

forward_compatible: forward_compatible,

robustness: if version >= &Version(Api::Gl, 4, 5) || version >= &Version(Api::GlEs, 3, 2) ||
(version >= &Version(Api::Gl, 3, 0) && extensions.gl_arb_robustness)
{
Expand Down Expand Up @@ -496,6 +575,8 @@ pub unsafe fn get_capabilities(gl: &gl::Gl, version: &Version, extensions: &Exte
None
}
},

renderer: renderer,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use texture;
use uniforms;
use vertex_array_object;

pub use self::capabilities::{ReleaseBehavior, Capabilities};
pub use self::capabilities::{ReleaseBehavior, Capabilities, Profile};
pub use self::extensions::ExtensionsList;
pub use self::state::GlState;

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ extern crate smallvec;

#[cfg(feature = "glutin")]
pub use backend::glutin_backend::glutin;
pub use context::Profile;
pub use draw_parameters::{Blend, BlendingFunction, LinearBlendingFactor, BackfaceCullingMode};
pub use draw_parameters::{Depth, DepthTest, PolygonMode, DrawParameters, StencilTest, StencilOperation};
pub use draw_parameters::{Smooth};
Expand Down

0 comments on commit 8a7a1f9

Please sign in to comment.