Skip to content

Commit

Permalink
mark GlContext::create, make_current, and make_not_current unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Jan 24, 2022
1 parent bc2f84b commit b68a05b
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 286 deletions.
15 changes: 11 additions & 4 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();

let context = GlContext::create(&window, GlConfig::default()).unwrap();
let context = unsafe { GlContext::create(&window, GlConfig::default()).unwrap() };

context.make_current();
unsafe {
context.make_current();
}

gl::load_with(|symbol| context.get_proc_address(symbol) as *const _);

Expand All @@ -24,15 +26,20 @@ fn main() {
*control_flow = ControlFlow::Exit;
}
winit::event::Event::RedrawRequested(_) => {
context.make_current();
unsafe {
context.make_current();
}

unsafe {
gl::ClearColor(1.0, 0.0, 1.0, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}

context.swap_buffers();
context.make_not_current();

unsafe {
context.make_not_current();
}
}
_ => {}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct GlContext {
}

impl GlContext {
pub fn create(
pub unsafe fn create(
parent: &impl HasRawWindowHandle,
config: GlConfig,
) -> Result<GlContext, GlError> {
Expand All @@ -82,11 +82,11 @@ impl GlContext {
})
}

pub fn make_current(&self) {
pub unsafe fn make_current(&self) {
self.context.make_current();
}

pub fn make_not_current(&self) {
pub unsafe fn make_not_current(&self) {
self.context.make_not_current();
}

Expand Down
118 changes: 56 additions & 62 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct GlContext {
}

impl GlContext {
pub fn create(
pub unsafe fn create(
parent: &impl HasRawWindowHandle,
config: GlConfig,
) -> Result<GlContext, GlError> {
Expand All @@ -42,85 +42,79 @@ impl GlContext {

let parent_view = handle.ns_view as id;

unsafe {
let version = if config.version < (3, 2) && config.profile == Profile::Compatibility {
NSOpenGLProfileVersionLegacy
} else if config.version == (3, 2) && config.profile == Profile::Core {
NSOpenGLProfileVersion3_2Core
} else if config.version > (3, 2) && config.profile == Profile::Core {
NSOpenGLProfileVersion4_1Core
} else {
return Err(GlError::VersionNotSupported);
};
let version = if config.version < (3, 2) && config.profile == Profile::Compatibility {
NSOpenGLProfileVersionLegacy
} else if config.version == (3, 2) && config.profile == Profile::Core {
NSOpenGLProfileVersion3_2Core
} else if config.version > (3, 2) && config.profile == Profile::Core {
NSOpenGLProfileVersion4_1Core
} else {
return Err(GlError::VersionNotSupported);
};

#[rustfmt::skip]
let mut attrs = vec![
NSOpenGLPFAOpenGLProfile as u32, version as u32,
NSOpenGLPFAColorSize as u32, (config.red_bits + config.blue_bits + config.green_bits) as u32,
NSOpenGLPFAAlphaSize as u32, config.alpha_bits as u32,
NSOpenGLPFADepthSize as u32, config.depth_bits as u32,
NSOpenGLPFAStencilSize as u32, config.stencil_bits as u32,
NSOpenGLPFAAccelerated as u32,
];

if config.samples.is_some() {
#[rustfmt::skip]
let mut attrs = vec![
NSOpenGLPFAOpenGLProfile as u32, version as u32,
NSOpenGLPFAColorSize as u32, (config.red_bits + config.blue_bits + config.green_bits) as u32,
NSOpenGLPFAAlphaSize as u32, config.alpha_bits as u32,
NSOpenGLPFADepthSize as u32, config.depth_bits as u32,
NSOpenGLPFAStencilSize as u32, config.stencil_bits as u32,
NSOpenGLPFAAccelerated as u32,
];

if config.samples.is_some() {
#[rustfmt::skip]
attrs.extend_from_slice(&[
NSOpenGLPFAMultisample as u32,
NSOpenGLPFASampleBuffers as u32, 1,
NSOpenGLPFASamples as u32, config.samples.unwrap() as u32,
]);
}
attrs.extend_from_slice(&[
NSOpenGLPFAMultisample as u32,
NSOpenGLPFASampleBuffers as u32, 1,
NSOpenGLPFASamples as u32, config.samples.unwrap() as u32,
]);
}

if config.double_buffer {
attrs.push(NSOpenGLPFADoubleBuffer as u32);
}
if config.double_buffer {
attrs.push(NSOpenGLPFADoubleBuffer as u32);
}

attrs.push(0);
attrs.push(0);

let pixel_format = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attrs);
let pixel_format = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attrs);

if pixel_format == nil {
return Err(GlError::CreationFailed);
}
if pixel_format == nil {
return Err(GlError::CreationFailed);
}

let view = NSOpenGLView::alloc(nil)
.initWithFrame_pixelFormat_(parent_view.frame(), pixel_format);
let view = NSOpenGLView::alloc(nil)
.initWithFrame_pixelFormat_(parent_view.frame(), pixel_format);

if view == nil {
return Err(GlError::CreationFailed);
}
if view == nil {
return Err(GlError::CreationFailed);
}

view.setWantsBestResolutionOpenGLSurface_(YES);
view.setWantsBestResolutionOpenGLSurface_(YES);

let () = msg_send![view, retain];
NSOpenGLView::display_(view);
parent_view.addSubview_(view);
let () = msg_send![view, retain];
NSOpenGLView::display_(view);
parent_view.addSubview_(view);

let context: id = msg_send![view, openGLContext];
let () = msg_send![context, retain];
let context: id = msg_send![view, openGLContext];
let () = msg_send![context, retain];

context.setValues_forParameter_(
&(config.vsync as i32),
NSOpenGLContextParameter::NSOpenGLCPSwapInterval,
);
context.setValues_forParameter_(
&(config.vsync as i32),
NSOpenGLContextParameter::NSOpenGLCPSwapInterval,
);

let () = msg_send![pixel_format, release];
let () = msg_send![pixel_format, release];

Ok(GlContext { view, context })
}
Ok(GlContext { view, context })
}

pub fn make_current(&self) {
unsafe {
self.context.makeCurrentContext();
}
pub unsafe fn make_current(&self) {
self.context.makeCurrentContext();
}

pub fn make_not_current(&self) {
unsafe {
NSOpenGLContext::clearCurrentContext(self.context);
}
pub unsafe fn make_not_current(&self) {
NSOpenGLContext::clearCurrentContext(self.context);
}

pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
Expand Down
Loading

0 comments on commit b68a05b

Please sign in to comment.