Skip to content

Commit

Permalink
Bind VAO before use and unbind after draw call
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Mar 22, 2022
1 parent ac14dea commit 07bbf3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use crate::context::HasContext;
#[derive(Clone)]
pub struct Context {
context: Rc<crate::context::Context>,
pub(super) vao: crate::context::VertexArray,
programs: Rc<RefCell<HashMap<String, Program>>>,
effects: Rc<RefCell<HashMap<String, ImageEffect>>>,
camera2d: Rc<RefCell<Option<Camera>>>,
Expand All @@ -29,21 +30,23 @@ impl Context {
pub fn from_gl_context(context: Rc<crate::context::Context>) -> ThreeDResult<Self> {
#[cfg(not(target_arch = "wasm32"))]
unsafe {
// Create one Vertex Array Object which is then reused all the time.
let vao = context
.create_vertex_array()
.map_err(|e| CoreError::ContextCreation(e))?;
context.bind_vertex_array(Some(vao));
// Enable seamless cube map textures
context.enable(crate::context::TEXTURE_CUBE_MAP_SEAMLESS);
context.pixel_store_i32(crate::context::UNPACK_ALIGNMENT, 1);
context.pixel_store_i32(crate::context::PACK_ALIGNMENT, 1);
}
let c = Self {
context,
programs: Rc::new(RefCell::new(HashMap::new())),
effects: Rc::new(RefCell::new(HashMap::new())),
camera2d: Rc::new(RefCell::new(None)),
};
let c = unsafe {
// Create one Vertex Array Object which is then reused all the time.
let vao = context
.create_vertex_array()
.map_err(|e| CoreError::ContextCreation(e))?;
Self {
context,
vao,
programs: Rc::new(RefCell::new(HashMap::new())),
effects: Rc::new(RefCell::new(HashMap::new())),
camera2d: Rc::new(RefCell::new(None)),
}
};
c.error_check()?;
Ok(c)
Expand Down
14 changes: 14 additions & 0 deletions src/core/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context.vertex_attrib_pointer_f32(
loc,
Expand Down Expand Up @@ -410,6 +411,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context.vertex_attrib_pointer_f32(
loc,
Expand Down Expand Up @@ -441,6 +443,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 1, buffer.data_type(), false, 0, 0);
Expand All @@ -466,6 +469,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 1, buffer.data_type(), false, 0, 0);
Expand All @@ -491,6 +495,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 2, buffer.data_type(), false, 0, 0);
Expand Down Expand Up @@ -520,6 +525,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 2, buffer.data_type(), false, 0, 0);
Expand All @@ -545,6 +551,7 @@ impl Program {
buffer.bind();
let loc = self.location(&name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 3, buffer.data_type(), false, 0, 0);
Expand Down Expand Up @@ -574,6 +581,7 @@ impl Program {
buffer.bind();
let loc = self.location(&name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 3, buffer.data_type(), false, 0, 0);
Expand All @@ -599,6 +607,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 4, buffer.data_type(), false, 0, 0);
Expand All @@ -624,6 +633,7 @@ impl Program {
buffer.bind();
let loc = self.location(name)?;
unsafe {
self.context.bind_vertex_array(Some(self.context.vao));
self.context.enable_vertex_attrib_array(loc);
self.context
.vertex_attrib_pointer_f32(loc, 4, buffer.data_type(), false, 0, 0);
Expand Down Expand Up @@ -656,6 +666,7 @@ impl Program {
for location in self.attributes.values() {
self.context.disable_vertex_attrib_array(*location);
}
self.context.bind_vertex_array(None);
}
self.unuse_program();
self.context.error_check()
Expand Down Expand Up @@ -687,6 +698,7 @@ impl Program {
for location in self.attributes.values() {
self.context.disable_vertex_attrib_array(*location);
}
self.context.bind_vertex_array(None);
}
self.unuse_program();
self.context.error_check()
Expand Down Expand Up @@ -742,6 +754,7 @@ impl Program {
for location in self.attributes.values() {
self.context.disable_vertex_attrib_array(*location);
}
self.context.bind_vertex_array(None);
}
self.unuse_program();
self.context.error_check()
Expand Down Expand Up @@ -798,6 +811,7 @@ impl Program {
for location in self.attributes.values() {
self.context.disable_vertex_attrib_array(*location);
}
self.context.bind_vertex_array(None);
}
self.unuse_program();
self.context.error_check()
Expand Down

0 comments on commit 07bbf3b

Please sign in to comment.