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

Update gleam, and use a dynamic GL function table. #81

Merged
merged 1 commit into from
Feb 26, 2017
Merged
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "offscreen_gl_context"
license = "MIT / Apache-2.0"
version = "0.6.1"
version = "0.7.0"
authors = ["Emilio Cobos Álvarez <emilio@crisal.io>", "The Servo Project Developers"]
description = "Creation and manipulation of HW accelerated offscreen rendering contexts in multiple platforms. Originally intended for the Servo project's WebGL implementation."
repository = "https://github.com/emilio/rust-offscreen-rendering-context"
Expand All @@ -19,7 +19,7 @@ test_osmesa = []

[dependencies]
log = "0.3"
gleam = "0.2.31"
gleam = "0.3"
euclid = "0.11"
serde = { version = "0.9", optional = true }
osmesa-sys = { version = "0.1", optional = true }
Expand Down
160 changes: 83 additions & 77 deletions src/draw_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use euclid::Size2D;
use gleam::gl;
use gleam::gl::types::{GLuint, GLenum, GLint};
use std::rc::Rc;

use GLContext;
use NativeGLContextMethods;
Expand All @@ -18,10 +19,11 @@ impl Default for ColorAttachmentType {
}


/// We either have a color renderbuffer
/// Or a surface bound to a texture
/// bound to a framebuffer as a color
/// attachment
/// We either have a color renderbuffer, or a surface bound to a texture bound
/// to a framebuffer as a color attachment.
///
/// NB: The draw buffer manages it, and calls its destroy method on drop, this
/// is just to avoid propagating the GL functions pointer further down.
#[derive(Debug)]
pub enum ColorAttachment {
Renderbuffer(GLuint),
Expand All @@ -35,13 +37,11 @@ impl ColorAttachment {
ColorAttachment::Texture(_) => ColorAttachmentType::Texture,
}
}
}

impl Drop for ColorAttachment {
fn drop(&mut self) {
match *self {
ColorAttachment::Renderbuffer(id) => gl::delete_renderbuffers(&[id]),
ColorAttachment::Texture(tex_id) => gl::delete_textures(&[tex_id]),
fn destroy(self, gl: &gl::Gl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to destroy now instead of drop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to grow them by an extra pointer given we only create one per draw buffer, but I can definitely do it I guess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you are passing gl in, I see. And the function is not public, so there is no concern about calling it twice.
Let's leave it with destroy then :)

match self {
ColorAttachment::Renderbuffer(id) => gl.delete_renderbuffers(&[id]),
ColorAttachment::Texture(tex_id) => gl.delete_textures(&[tex_id]),
}
}
}
Expand All @@ -52,6 +52,7 @@ impl Drop for ColorAttachment {
/// a depth or stencil buffer, depending on context
/// requirements.
pub struct DrawBuffer {
gl_: Rc<gl::Gl>,
size: Size2D<i32>,
framebuffer: GLuint,
stencil_renderbuffer: GLuint,
Expand All @@ -61,14 +62,15 @@ pub struct DrawBuffer {
}

/// Helper function to create a render buffer
/// TODO(ecoal95): We'll need to switch between `glRenderbufferStorage` and
/// `glRenderbufferStorageMultisample` when we support antialising
fn create_renderbuffer(format: GLenum,
/// TODO(emilio): We'll need to switch between `glRenderbufferStorage` and
/// `glRenderbufferStorageMultisample` when we support antialising
fn create_renderbuffer(gl_: &gl::Gl,
format: GLenum,
size: &Size2D<i32>) -> GLuint {
let ret = gl::gen_renderbuffers(1)[0];
gl::bind_renderbuffer(gl::RENDERBUFFER, ret);
gl::renderbuffer_storage(gl::RENDERBUFFER, format, size.width, size.height);
gl::bind_renderbuffer(gl::RENDERBUFFER, 0);
let ret = gl_.gen_renderbuffers(1)[0];
gl_.bind_renderbuffer(gl::RENDERBUFFER, ret);
gl_.renderbuffer_storage(gl::RENDERBUFFER, format, size.width, size.height);
gl_.bind_renderbuffer(gl::RENDERBUFFER, 0);

ret
}
Expand All @@ -77,7 +79,7 @@ impl DrawBuffer {
pub fn new<T: NativeGLContextMethods>(context: &GLContext<T>,
mut size: Size2D<i32>,
color_attachment_type: ColorAttachmentType)
-> Result<DrawBuffer, &'static str>
-> Result<Self, &'static str>
{
const MIN_DRAWING_BUFFER_SIZE: i32 = 16;
use std::cmp;
Expand All @@ -101,6 +103,7 @@ impl DrawBuffer {
size.height = cmp::max(MIN_DRAWING_BUFFER_SIZE, size.height);

let mut draw_buffer = DrawBuffer {
gl_: context.clone_gl(),
size: size,
framebuffer: 0,
color_attachment: None,
Expand All @@ -113,8 +116,10 @@ impl DrawBuffer {

try!(draw_buffer.init(context, color_attachment_type));

debug_assert!(gl::check_frame_buffer_status(gl::FRAMEBUFFER) == gl::FRAMEBUFFER_COMPLETE);
debug_assert!(gl::get_error() == gl::NO_ERROR);
debug_assert_eq!(draw_buffer.gl().check_frame_buffer_status(gl::FRAMEBUFFER),
gl::FRAMEBUFFER_COMPLETE);
debug_assert_eq!(draw_buffer.gl().get_error(),
gl::NO_ERROR);

Ok(draw_buffer)
}
Expand Down Expand Up @@ -149,127 +154,128 @@ impl DrawBuffer {
&ColorAttachment::Texture(id) => Some(id),
}
}
}

// NOTE: The initially associated GLContext MUST be the current gl context
// when drop is called. I know this is an important constraint.
// Right now there are no problems, if not, consider using a pointer to a
// parent with Rc<GLContext> and call make_current()
impl Drop for DrawBuffer {
fn drop(&mut self) {
gl::delete_framebuffers(&[self.framebuffer]);

// NOTE: Color renderbuffer is destroyed on drop of
// ColorAttachment
gl::delete_renderbuffers(&[self.stencil_renderbuffer, self.depth_renderbuffer]);
fn gl(&self) -> &gl::Gl {
&*self.gl_
}
}

trait DrawBufferHelpers {
fn init<T: NativeGLContextMethods>(&mut self,
&GLContext<T>,
color_attachment_type: ColorAttachmentType)
-> Result<(), &'static str>;
fn attach_to_framebuffer(&mut self)
-> Result<(), &'static str>;
}

impl DrawBufferHelpers for DrawBuffer {
fn init<T: NativeGLContextMethods>(&mut self,
context: &GLContext<T>,
color_attachment_type: ColorAttachmentType)
-> Result<(), &'static str> {
let attrs = context.borrow_attributes();
let formats = context.borrow_formats();

assert!(self.color_attachment.is_none(),
"Would leak color attachment!");
self.color_attachment = match color_attachment_type {
ColorAttachmentType::Renderbuffer => {
let color_renderbuffer = create_renderbuffer(formats.color_renderbuffer, &self.size);
let color_renderbuffer =
create_renderbuffer(self.gl(), formats.color_renderbuffer, &self.size);
debug_assert!(color_renderbuffer != 0);

Some(ColorAttachment::Renderbuffer(color_renderbuffer))
},

// TODO(ecoal95): Allow more customization of textures
ColorAttachmentType::Texture => {
let texture = gl::gen_textures(1)[0];
let texture = self.gl().gen_textures(1)[0];
debug_assert!(texture != 0);

gl::bind_texture(gl::TEXTURE_2D, texture);
gl::tex_image_2d(gl::TEXTURE_2D, 0,
self.gl().bind_texture(gl::TEXTURE_2D, texture);
self.gl().tex_image_2d(gl::TEXTURE_2D, 0,
formats.texture_internal as GLint, self.size.width, self.size.height, 0, formats.texture, formats.texture_type, None);

// Low filtering to allow rendering
gl::tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as GLint);
gl::tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as GLint);
self.gl().tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as GLint);
self.gl().tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as GLint);

// TODO(ecoal95): Check if these two are neccessary, probably not
gl::tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as GLint);
gl::tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as GLint);
// TODO(emilio): Check if these two are neccessary, probably not
self.gl().tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as GLint);
self.gl().tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as GLint);

gl::bind_texture(gl::TEXTURE_2D, 0);
self.gl().bind_texture(gl::TEXTURE_2D, 0);

debug_assert!(gl::get_error() == gl::NO_ERROR);
debug_assert_eq!(self.gl().get_error(), gl::NO_ERROR);

Some(ColorAttachment::Texture(texture))
},
};

// After this we check if we need stencil and depth buffers
if attrs.depth {
self.depth_renderbuffer = create_renderbuffer(formats.depth, &self.size);
self.depth_renderbuffer = create_renderbuffer(self.gl(), formats.depth, &self.size);
debug_assert!(self.depth_renderbuffer != 0);
}

if attrs.stencil {
self.stencil_renderbuffer = create_renderbuffer(formats.stencil, &self.size);
self.stencil_renderbuffer = create_renderbuffer(self.gl(), formats.stencil, &self.size);
debug_assert!(self.stencil_renderbuffer != 0);
}

self.framebuffer = gl::gen_framebuffers(1)[0];
self.framebuffer = self.gl().gen_framebuffers(1)[0];
debug_assert!(self.framebuffer != 0);

// Finally we attach them to the framebuffer
self.attach_to_framebuffer()
}

fn attach_to_framebuffer(&mut self) -> Result<(), &'static str> {
gl::bind_framebuffer(gl::FRAMEBUFFER, self.framebuffer);
self.gl().bind_framebuffer(gl::FRAMEBUFFER, self.framebuffer);
// NOTE: The assertion fails if the framebuffer is not bound
debug_assert!(gl::is_framebuffer(self.framebuffer) == gl::TRUE);
debug_assert_eq!(self.gl().is_framebuffer(self.framebuffer), gl::TRUE);

match *self.color_attachment.as_ref().unwrap() {
ColorAttachment::Renderbuffer(color_renderbuffer) => {
gl::framebuffer_renderbuffer(gl::FRAMEBUFFER,
gl::COLOR_ATTACHMENT0,
gl::RENDERBUFFER,
color_renderbuffer);
debug_assert!(gl::is_renderbuffer(color_renderbuffer) == gl::TRUE);
self.gl().framebuffer_renderbuffer(gl::FRAMEBUFFER,
gl::COLOR_ATTACHMENT0,
gl::RENDERBUFFER,
color_renderbuffer);
debug_assert_eq!(self.gl().is_renderbuffer(color_renderbuffer), gl::TRUE);
},
ColorAttachment::Texture(texture_id) => {
gl::framebuffer_texture_2d(gl::FRAMEBUFFER,
gl::COLOR_ATTACHMENT0,
gl::TEXTURE_2D,
texture_id, 0);
self.gl().framebuffer_texture_2d(gl::FRAMEBUFFER,
gl::COLOR_ATTACHMENT0,
gl::TEXTURE_2D,
texture_id, 0);
},
}

if self.depth_renderbuffer != 0 {
gl::framebuffer_renderbuffer(gl::FRAMEBUFFER,
gl::DEPTH_ATTACHMENT,
gl::RENDERBUFFER,
self.depth_renderbuffer);
debug_assert!(gl::is_renderbuffer(self.depth_renderbuffer) == gl::TRUE);
self.gl().framebuffer_renderbuffer(gl::FRAMEBUFFER,
gl::DEPTH_ATTACHMENT,
gl::RENDERBUFFER,
self.depth_renderbuffer);
debug_assert_eq!(self.gl().is_renderbuffer(self.depth_renderbuffer), gl::TRUE);
}

if self.stencil_renderbuffer != 0 {
gl::framebuffer_renderbuffer(gl::FRAMEBUFFER,
gl::STENCIL_ATTACHMENT,
gl::RENDERBUFFER,
self.stencil_renderbuffer);
debug_assert!(gl::is_renderbuffer(self.stencil_renderbuffer) == gl::TRUE);
self.gl().framebuffer_renderbuffer(gl::FRAMEBUFFER,
gl::STENCIL_ATTACHMENT,
gl::RENDERBUFFER,
self.stencil_renderbuffer);
debug_assert_eq!(self.gl().is_renderbuffer(self.stencil_renderbuffer), gl::TRUE);
}

Ok(())
}
}

// NOTE: The initially associated GLContext MUST be the current gl context
// when drop is called. I know this is an important constraint.
// Right now there are no problems, if not, consider using a pointer to a
// parent with Rc<GLContext> and call make_current()
impl Drop for DrawBuffer {
fn drop(&mut self) {
if let Some(att) = self.color_attachment.take() {
att.destroy(self.gl());
}

self.gl().delete_framebuffers(&[self.framebuffer]);

// NOTE: Color renderbuffer is destroyed on drop of
// ColorAttachment
self.gl().delete_renderbuffers(&[self.stencil_renderbuffer, self.depth_renderbuffer]);
}
}
Loading