Merge pull request #134 from tomaka/share-lists

Implement lists sharing
This commit is contained in:
tomaka 2014-11-24 20:51:59 +01:00
commit d7a7765638
6 changed files with 55 additions and 21 deletions

View file

@ -39,9 +39,13 @@ impl MonitorID {
} }
impl Window { impl Window {
pub fn new(_builder: WindowBuilder) -> Result<Window, CreationError> { pub fn new(builder: WindowBuilder) -> Result<Window, CreationError> {
use std::{mem, ptr}; use std::{mem, ptr};
if builder.sharing.is_some() {
unimplemented!()
}
let native_window = unsafe { android_glue::get_native_window() }; let native_window = unsafe { android_glue::get_native_window() };
if native_window.is_null() { if native_window.is_null() {
return Err(OsError(format!("Android's native window is null"))); return Err(OsError(format!("Android's native window is null")));

View file

@ -2,6 +2,7 @@
#![feature(unsafe_destructor)] #![feature(unsafe_destructor)]
#![feature(globs)] #![feature(globs)]
#![feature(phase)] #![feature(phase)]
#![feature(if_let)]
#![unstable] #![unstable]
//! The purpose of this library is to provide an OpenGL context on as many //! The purpose of this library is to provide an OpenGL context on as many
@ -86,7 +87,8 @@ impl std::error::Error for CreationError {
/// Object that allows you to build windows. /// Object that allows you to build windows.
#[cfg(feature = "window")] #[cfg(feature = "window")]
pub struct WindowBuilder { pub struct WindowBuilder<'a> {
sharing: Option<&'a Window>,
dimensions: Option<(uint, uint)>, dimensions: Option<(uint, uint)>,
title: String, title: String,
monitor: Option<winimpl::MonitorID>, monitor: Option<winimpl::MonitorID>,
@ -97,10 +99,11 @@ pub struct WindowBuilder {
} }
#[cfg(feature = "window")] #[cfg(feature = "window")]
impl WindowBuilder { impl<'a> WindowBuilder<'a> {
/// Initializes a new `WindowBuilder` with default values. /// Initializes a new `WindowBuilder` with default values.
pub fn new() -> WindowBuilder { pub fn new() -> WindowBuilder<'a> {
WindowBuilder { WindowBuilder {
sharing: None,
dimensions: None, dimensions: None,
title: "glutin window".to_string(), title: "glutin window".to_string(),
monitor: None, monitor: None,
@ -114,13 +117,13 @@ impl WindowBuilder {
/// Requests the window to be of specific dimensions. /// Requests the window to be of specific dimensions.
/// ///
/// Width and height are in pixels. /// Width and height are in pixels.
pub fn with_dimensions(mut self, width: uint, height: uint) -> WindowBuilder { pub fn with_dimensions(mut self, width: uint, height: uint) -> WindowBuilder<'a> {
self.dimensions = Some((width, height)); self.dimensions = Some((width, height));
self self
} }
/// Requests a specific title for the window. /// Requests a specific title for the window.
pub fn with_title(mut self, title: String) -> WindowBuilder { pub fn with_title(mut self, title: String) -> WindowBuilder<'a> {
self.title = title; self.title = title;
self self
} }
@ -128,17 +131,25 @@ impl WindowBuilder {
/// Requests fullscreen mode. /// Requests fullscreen mode.
/// ///
/// If you don't specify dimensions for the window, it will match the monitor's. /// If you don't specify dimensions for the window, it will match the monitor's.
pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder { pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> {
let MonitorID(monitor) = monitor; let MonitorID(monitor) = monitor;
self.monitor = Some(monitor); self.monitor = Some(monitor);
self self
} }
/// The created window will share all its OpenGL objects with the window in the parameter.
///
/// There are some exceptions, like FBOs or VAOs. See the OpenGL documentation.
pub fn with_shared_lists(mut self, other: &'a Window) -> WindowBuilder<'a> {
self.sharing = Some(other);
self
}
/// Requests to use a specific OpenGL version. /// Requests to use a specific OpenGL version.
/// ///
/// Version is a (major, minor) pair. For example to request OpenGL 3.3 /// Version is a (major, minor) pair. For example to request OpenGL 3.3
/// you would pass `(3, 3)`. /// you would pass `(3, 3)`.
pub fn with_gl_version(mut self, version: (uint, uint)) -> WindowBuilder { pub fn with_gl_version(mut self, version: (uint, uint)) -> WindowBuilder<'a> {
self.gl_version = Some(version); self.gl_version = Some(version);
self self
} }
@ -147,19 +158,19 @@ impl WindowBuilder {
/// ///
/// The default value for this flag is `cfg!(ndebug)`, which means that it's enabled /// The default value for this flag is `cfg!(ndebug)`, which means that it's enabled
/// when you run `cargo build` and disabled when you run `cargo build --release`. /// when you run `cargo build` and disabled when you run `cargo build --release`.
pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder { pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> {
self.gl_debug = flag; self.gl_debug = flag;
self self
} }
/// Requests that the window has vsync enabled. /// Requests that the window has vsync enabled.
pub fn with_vsync(mut self) -> WindowBuilder { pub fn with_vsync(mut self) -> WindowBuilder<'a> {
self.vsync = true; self.vsync = true;
self self
} }
/// Sets whether the window will be initially hidden or visible. /// Sets whether the window will be initially hidden or visible.
pub fn with_visibility(mut self, visible: bool) -> WindowBuilder { pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> {
self.visible = visible; self.visible = visible;
self self
} }

View file

@ -71,6 +71,10 @@ impl Deref<Window> for HeadlessContext {
#[cfg(feature = "window")] #[cfg(feature = "window")]
impl Window { impl Window {
pub fn new(builder: WindowBuilder) -> Result<Window, CreationError> { pub fn new(builder: WindowBuilder) -> Result<Window, CreationError> {
if builder.sharing.is_some() {
unimplemented!()
}
Window::new_impl(builder.dimensions, builder.title.as_slice(), builder.monitor, true) Window::new_impl(builder.dimensions, builder.title.as_slice(), builder.monitor, true)
} }
} }

View file

@ -15,7 +15,8 @@ local_data_key!(WINDOW: (ffi::HWND, Sender<Event>))
pub fn new_window(builder_dimensions: Option<(uint, uint)>, builder_title: String, pub fn new_window(builder_dimensions: Option<(uint, uint)>, builder_title: String,
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) -> Result<Window, CreationError> builder_vsync: bool, builder_hidden: bool,
builder_sharelists: Option<ffi::HGLRC>) -> Result<Window, CreationError>
{ {
use std::mem; use std::mem;
use std::os; use std::os;
@ -305,10 +306,16 @@ pub fn new_window(builder_dimensions: Option<(uint, uint)>, builder_title: Strin
let ctxt = unsafe { let ctxt = unsafe {
if extra_functions.CreateContextAttribsARB.is_loaded() { if extra_functions.CreateContextAttribsARB.is_loaded() {
extra_functions.CreateContextAttribsARB(hdc, ptr::null(), let share = if let Some(c) = builder_sharelists { c } else { ptr::null() };
extra_functions.CreateContextAttribsARB(hdc, share,
attributes.as_slice().as_ptr()) attributes.as_slice().as_ptr())
} else { } else {
ffi::wgl::CreateContext(hdc) let ctxt = ffi::wgl::CreateContext(hdc);
if let Some(c) = builder_sharelists {
ffi::wgl::ShareLists(c, ctxt);
};
ctxt
} }
}; };

View file

@ -25,7 +25,8 @@ impl HeadlessContext {
/// See the docs in the crate root file. /// See the docs in the crate root file.
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)
.map(|w| HeadlessContext(w)) .map(|w| HeadlessContext(w))
} }
@ -69,8 +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 } = builder; gl_debug, vsync, visible, sharing } = builder;
init::new_window(dimensions, title, monitor, gl_version, gl_debug, vsync, !visible) init::new_window(dimensions, title, monitor, gl_version, gl_debug, vsync,
!visible, sharing.map(|w| w.window.context))
} }
} }

View file

@ -248,11 +248,17 @@ impl Window {
}) })
}); });
let share = if let Some(win) = builder.sharing {
win.window.context
} else {
ptr::null()
};
let context = if extra_functions.CreateContextAttribsARB.is_loaded() { let context = if extra_functions.CreateContextAttribsARB.is_loaded() {
extra_functions.CreateContextAttribsARB(display as *mut ffi::glx_extra::types::Display, extra_functions.CreateContextAttribsARB(display as *mut ffi::glx_extra::types::Display,
fb_config, ptr::null(), 1, attributes.as_ptr()) fb_config, share, 1, attributes.as_ptr())
} else { } else {
ffi::glx::CreateContext(display, &mut visual_infos, ptr::null(), 1) ffi::glx::CreateContext(display, &mut visual_infos, share, 1)
}; };
if context.is_null() { if context.is_null() {