Merge pull request #133 from tomaka/multisampling

Implement multisampling for x11
This commit is contained in:
tomaka 2014-11-27 15:35:39 +01:00
commit 33455a4c3e
4 changed files with 28 additions and 6 deletions

View file

@ -94,6 +94,7 @@ pub struct WindowBuilder<'a> {
gl_debug: bool, gl_debug: bool,
vsync: bool, vsync: bool,
visible: bool, visible: bool,
multisampling: Option<u16>,
} }
#[cfg(feature = "window")] #[cfg(feature = "window")]
@ -109,6 +110,7 @@ impl<'a> WindowBuilder<'a> {
gl_debug: cfg!(ndebug), gl_debug: cfg!(ndebug),
vsync: false, vsync: false,
visible: true, visible: true,
multisampling: None,
} }
} }
@ -173,6 +175,18 @@ impl<'a> WindowBuilder<'a> {
self self
} }
/// Sets the multisampling level to request.
///
/// # Panic
///
/// Will panic if `samples` is not a power of two.
pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> {
use std::num::UnsignedInt;
assert!(samples.is_power_of_two());
self.multisampling = Some(samples);
self
}
/// Builds the window. /// Builds the window.
/// ///
/// Error should be very rare and only occur in case of permission denied, incompatible system, /// Error should be very rare and only occur in case of permission denied, incompatible system,

View file

@ -16,7 +16,8 @@ pub fn new_window(builder_dimensions: Option<(uint, uint)>, builder_title: Strin
builder_monitor: Option<super::MonitorID>, builder_monitor: Option<super::MonitorID>,
builder_gl_version: Option<(uint, uint)>, builder_debug: bool, builder_gl_version: Option<(uint, uint)>, builder_debug: bool,
builder_vsync: bool, builder_hidden: bool, builder_vsync: bool, builder_hidden: bool,
builder_sharelists: Option<ffi::HGLRC>) -> Result<Window, CreationError> builder_sharelists: Option<ffi::HGLRC>, builder_multisampling: Option<u16>)
-> Result<Window, CreationError>
{ {
use std::mem; use std::mem;
use std::os; use std::os;

View file

@ -26,7 +26,7 @@ impl HeadlessContext {
pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, CreationError> { pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, CreationError> {
let HeadlessRendererBuilder { dimensions, gl_version, gl_debug } = builder; let HeadlessRendererBuilder { dimensions, gl_version, gl_debug } = builder;
init::new_window(Some(dimensions), "".to_string(), None, gl_version, gl_debug, false, true, init::new_window(Some(dimensions), "".to_string(), None, gl_version, gl_debug, false, true,
None) None, None)
.map(|w| HeadlessContext(w)) .map(|w| HeadlessContext(w))
} }
@ -70,9 +70,9 @@ impl Window {
/// See the docs in the crate root file. /// See the docs in the crate root file.
pub fn new(builder: WindowBuilder) -> Result<Window, CreationError> { pub fn new(builder: WindowBuilder) -> Result<Window, CreationError> {
let WindowBuilder { dimensions, title, monitor, gl_version, let WindowBuilder { dimensions, title, monitor, gl_version,
gl_debug, vsync, visible, sharing } = builder; gl_debug, vsync, visible, sharing, multisampling } = builder;
init::new_window(dimensions, title, monitor, gl_version, gl_debug, vsync, init::new_window(dimensions, title, monitor, gl_version, gl_debug, vsync,
!visible, sharing.map(|w| w.window.context)) !visible, sharing.map(|w| w.window.context), multisampling)
} }
} }

View file

@ -58,7 +58,7 @@ impl Window {
// getting the FBConfig // getting the FBConfig
let fb_config = unsafe { let fb_config = unsafe {
const VISUAL_ATTRIBUTES: [libc::c_int, ..23] = [ let mut visual_attributes = vec![
ffi::GLX_X_RENDERABLE, 1, ffi::GLX_X_RENDERABLE, 1,
ffi::GLX_DRAWABLE_TYPE, ffi::GLX_WINDOW_BIT, ffi::GLX_DRAWABLE_TYPE, ffi::GLX_WINDOW_BIT,
ffi::GLX_RENDER_TYPE, ffi::GLX_RGBA_BIT, ffi::GLX_RENDER_TYPE, ffi::GLX_RGBA_BIT,
@ -73,10 +73,17 @@ impl Window {
0 0
]; ];
if let Some(val) = builder.multisampling {
visual_attributes.push(ffi::glx::SAMPLE_BUFFERS as libc::c_int);
visual_attributes.push(1);
visual_attributes.push(ffi::glx::SAMPLES as libc::c_int);
visual_attributes.push(val as libc::c_int);
}
let mut num_fb: libc::c_int = mem::uninitialized(); let mut num_fb: libc::c_int = mem::uninitialized();
let fb = ffi::glx::ChooseFBConfig(display, ffi::XDefaultScreen(display), let fb = ffi::glx::ChooseFBConfig(display, ffi::XDefaultScreen(display),
VISUAL_ATTRIBUTES.as_ptr(), &mut num_fb); visual_attributes.as_ptr(), &mut num_fb);
if fb.is_null() { if fb.is_null() {
return Err(OsError(format!("glx::ChooseFBConfig failed"))); return Err(OsError(format!("glx::ChooseFBConfig failed")));
} }