Rename BoundAttribBuffers to VertexAttribs and make it store a slice

This commit is contained in:
Anthony Ramine 2018-07-02 14:50:40 +02:00
parent c71c55e542
commit 5d43f1c9bd
4 changed files with 69 additions and 53 deletions

View file

@ -50,7 +50,13 @@ impl OESVertexArrayObjectMethods for OESVertexArrayObject {
self.ctx.send_command(WebGLCommand::CreateVertexArray(sender)); self.ctx.send_command(WebGLCommand::CreateVertexArray(sender));
let result = receiver.recv().unwrap(); let result = receiver.recv().unwrap();
result.map(|vao_id| WebGLVertexArrayObjectOES::new(&self.global(), vao_id)) result.map(|vao_id| {
WebGLVertexArrayObjectOES::new(
&self.global(),
vao_id,
self.ctx.limits().max_vertex_attribs,
)
})
} }
// https://www.khronos.org/registry/webgl/extensions/OES_vertex_array_object/ // https://www.khronos.org/registry/webgl/extensions/OES_vertex_array_object/
@ -69,7 +75,7 @@ impl OESVertexArrayObjectMethods for OESVertexArrayObject {
} }
// Remove VAO references from buffers // Remove VAO references from buffers
for (_, &(_, ref buffer)) in vao.bound_attrib_buffers().borrow().iter() { for (_, ref buffer) in vao.vertex_attribs().borrow().iter() {
if let Some(ref buffer) = *buffer { if let Some(ref buffer) = *buffer {
buffer.remove_vao_reference(vao.id()); buffer.remove_vao_reference(vao.id());
} }
@ -94,8 +100,8 @@ impl OESVertexArrayObjectMethods for OESVertexArrayObject {
fn BindVertexArrayOES(&self, vao: Option<&WebGLVertexArrayObjectOES>) { fn BindVertexArrayOES(&self, vao: Option<&WebGLVertexArrayObjectOES>) {
if let Some(bound_vao) = self.bound_vao.get() { if let Some(bound_vao) = self.bound_vao.get() {
// Store buffers attached to attrib pointers // Store buffers attached to attrib pointers
bound_vao.bound_attrib_buffers().set_from(&self.ctx.bound_attrib_buffers()); bound_vao.vertex_attribs().set_from(&self.ctx.vertex_attribs());
for (_, (_, ref buffer)) in bound_vao.bound_attrib_buffers().borrow().iter() { for (_, ref buffer) in bound_vao.vertex_attribs().borrow().iter() {
if let Some(ref buffer) = *buffer { if let Some(ref buffer) = *buffer {
buffer.add_vao_reference(bound_vao.id()); buffer.add_vao_reference(bound_vao.id());
} }
@ -119,13 +125,13 @@ impl OESVertexArrayObjectMethods for OESVertexArrayObject {
self.bound_vao.set(Some(&vao)); self.bound_vao.set(Some(&vao));
// Restore WebGLRenderingContext current bindings // Restore WebGLRenderingContext current bindings
self.ctx.bound_attrib_buffers().set_from(&vao.bound_attrib_buffers()); self.ctx.vertex_attribs().set_from(&vao.vertex_attribs());
let element_array = vao.bound_buffer_element_array(); let element_array = vao.bound_buffer_element_array();
self.ctx.set_bound_buffer_element_array(element_array.as_ref().map(|buffer| &**buffer)); self.ctx.set_bound_buffer_element_array(element_array.as_ref().map(|buffer| &**buffer));
} else { } else {
self.ctx.send_command(WebGLCommand::BindVertexArray(None)); self.ctx.send_command(WebGLCommand::BindVertexArray(None));
self.bound_vao.set(None); self.bound_vao.set(None);
self.ctx.bound_attrib_buffers().clear(); self.ctx.vertex_attribs().clear();
} }
} }
} }

View file

@ -193,7 +193,7 @@ pub struct WebGLRenderingContext {
bound_texture_unit: Cell<u32>, bound_texture_unit: Cell<u32>,
bound_buffer_array: MutNullableDom<WebGLBuffer>, bound_buffer_array: MutNullableDom<WebGLBuffer>,
bound_buffer_element_array: MutNullableDom<WebGLBuffer>, bound_buffer_element_array: MutNullableDom<WebGLBuffer>,
bound_attrib_buffers: BoundAttribBuffers, vertex_attribs: VertexAttribs,
current_program: MutNullableDom<WebGLProgram>, current_program: MutNullableDom<WebGLProgram>,
#[ignore_malloc_size_of = "Because it's small"] #[ignore_malloc_size_of = "Because it's small"]
current_vertex_attrib_0: Cell<(f32, f32, f32, f32)>, current_vertex_attrib_0: Cell<(f32, f32, f32, f32)>,
@ -234,6 +234,7 @@ impl WebGLRenderingContext {
share_mode: ctx_data.share_mode, share_mode: ctx_data.share_mode,
webgl_version, webgl_version,
glsl_version: ctx_data.glsl_version, glsl_version: ctx_data.glsl_version,
vertex_attribs: VertexAttribs::new(ctx_data.limits.max_vertex_attribs),
limits: ctx_data.limits, limits: ctx_data.limits,
canvas: Dom::from_ref(canvas), canvas: Dom::from_ref(canvas),
last_error: Cell::new(None), last_error: Cell::new(None),
@ -244,7 +245,6 @@ impl WebGLRenderingContext {
bound_texture_unit: Cell::new(constants::TEXTURE0), bound_texture_unit: Cell::new(constants::TEXTURE0),
bound_buffer_array: MutNullableDom::new(None), bound_buffer_array: MutNullableDom::new(None),
bound_buffer_element_array: MutNullableDom::new(None), bound_buffer_element_array: MutNullableDom::new(None),
bound_attrib_buffers: Default::default(),
bound_renderbuffer: MutNullableDom::new(None), bound_renderbuffer: MutNullableDom::new(None),
current_program: MutNullableDom::new(None), current_program: MutNullableDom::new(None),
current_vertex_attrib_0: Cell::new((0f32, 0f32, 0f32, 1f32)), current_vertex_attrib_0: Cell::new((0f32, 0f32, 0f32, 1f32)),
@ -312,8 +312,8 @@ impl WebGLRenderingContext {
}) })
} }
pub fn bound_attrib_buffers(&self) -> &BoundAttribBuffers { pub fn vertex_attribs(&self) -> &VertexAttribs {
&self.bound_attrib_buffers &self.vertex_attribs
} }
pub fn bound_buffer_element_array(&self) -> Option<DomRoot<WebGLBuffer>> { pub fn bound_buffer_element_array(&self) -> Option<DomRoot<WebGLBuffer>> {
@ -2075,7 +2075,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
} }
// Remove deleted buffer from bound attrib buffers. // Remove deleted buffer from bound attrib buffers.
self.bound_attrib_buffers.remove_buffer(buffer); self.vertex_attribs.delete_buffer(buffer);
// Delete buffer. // Delete buffer.
handle_object_deletion!(self, self.bound_buffer_array, buffer, handle_object_deletion!(self, self.bound_buffer_array, buffer,
@ -2204,8 +2204,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
{ {
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.2 // https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.2
let buffers = self.bound_attrib_buffers.borrow(); let buffers = self.vertex_attribs.borrow();
if buffers.iter().any(|(_, &(enabled, ref buffer))| enabled && buffer.is_none()) { if buffers.iter().any(|&(enabled, ref buffer)| enabled && buffer.is_none()) {
return self.webgl_error(InvalidOperation); return self.webgl_error(InvalidOperation);
} }
} }
@ -2279,8 +2279,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
{ {
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.2 // https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.2
let buffers = self.bound_attrib_buffers.borrow(); let buffers = self.vertex_attribs.borrow();
if buffers.iter().any(|(_, &(enabled, ref buffer))| enabled && buffer.is_none()) { if buffers.iter().any(|&(enabled, ref buffer)| enabled && buffer.is_none()) {
return self.webgl_error(InvalidOperation); return self.webgl_error(InvalidOperation);
} }
} }
@ -2299,7 +2299,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
return self.webgl_error(InvalidValue); return self.webgl_error(InvalidValue);
} }
self.bound_attrib_buffers.enabled(attrib_id, true); self.vertex_attribs.enabled_as_array(attrib_id, true);
self.send_command(WebGLCommand::EnableVertexAttribArray(attrib_id)); self.send_command(WebGLCommand::EnableVertexAttribArray(attrib_id));
} }
@ -2309,7 +2309,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
return self.webgl_error(InvalidValue); return self.webgl_error(InvalidValue);
} }
self.bound_attrib_buffers.enabled(attrib_id, false); self.vertex_attribs.enabled_as_array(attrib_id, false);
self.send_command(WebGLCommand::DisableVertexAttribArray(attrib_id)); self.send_command(WebGLCommand::DisableVertexAttribArray(attrib_id));
} }
@ -2601,7 +2601,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
if param == constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING { if param == constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING {
rooted!(in(cx) let mut jsval = NullValue()); rooted!(in(cx) let mut jsval = NullValue());
if let Some(buffer) = self.bound_attrib_buffers.get(index) { if let Some(buffer) = self.vertex_attribs.get(index) {
buffer.to_jsval(cx, jsval.handle_mut()); buffer.to_jsval(cx, jsval.handle_mut());
} }
return jsval.get(); return jsval.get();
@ -3367,7 +3367,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
} }
self.bound_attrib_buffers.bind_buffer(attrib_id, &buffer_array); self.vertex_attribs.bind_buffer(attrib_id, &buffer_array);
let msg = WebGLCommand::VertexAttribPointer(attrib_id, size, data_type, normalized, stride, offset as u32); let msg = WebGLCommand::VertexAttribPointer(attrib_id, size, data_type, normalized, stride, offset as u32);
self.send_command(msg); self.send_command(msg);
@ -3805,48 +3805,51 @@ impl UniformSetterType {
} }
} }
#[derive(Default, JSTraceable, MallocSizeOf)] #[derive(JSTraceable, MallocSizeOf)]
#[must_root] #[must_root]
pub struct BoundAttribBuffers { pub struct VertexAttribs {
elements: DomRefCell<FnvHashMap<u32, (bool, Option<Dom<WebGLBuffer>>)>>, attribs: DomRefCell<Box<[(bool, Option<Dom<WebGLBuffer>>)]>>,
} }
impl BoundAttribBuffers { impl VertexAttribs {
pub fn new(max: u32) -> Self {
// High-end GPUs have 16 of those, let's just use a boxed slice.
Self { attribs: DomRefCell::new(vec![Default::default(); max as usize].into()) }
}
pub fn clear(&self) { pub fn clear(&self) {
self.elements.borrow_mut().clear() for attrib in &mut **self.attribs.borrow_mut() {
*attrib = Default::default();
}
} }
pub fn set_from(&self, other: &BoundAttribBuffers) { pub fn set_from(&self, other: &Self) {
*self.elements.borrow_mut() = other.elements.borrow().clone(); self.attribs.borrow_mut().clone_from_slice(&other.attribs.borrow());
} }
pub fn borrow(&self) -> Ref<FnvHashMap<u32, (bool, Option<Dom<WebGLBuffer>>)>> { pub fn borrow(&self) -> Ref<[(bool, Option<Dom<WebGLBuffer>>)]> {
self.elements.borrow() Ref::map(self.attribs.borrow(), |attribs| &**attribs)
} }
fn remove_buffer(&self, buffer: &WebGLBuffer) { fn delete_buffer(&self, buffer: &WebGLBuffer) {
self.elements.borrow_mut().retain(|_, v| { for attrib in &mut **self.attribs.borrow_mut() {
v.1.as_ref().map_or(true, |b| b.id() != buffer.id()) if attrib.1.as_ref().map_or(false, |b| b.id() == buffer.id()) {
}) attrib.1 = None;
}
}
} }
fn get(&self, index: u32) -> Option<Ref<WebGLBuffer>> { fn get(&self, index: u32) -> Option<Ref<WebGLBuffer>> {
ref_filter_map(self.elements.borrow(), |elements| { ref_filter_map(self.attribs.borrow(), |attribs| {
elements.get(&index).and_then(|&(_, ref buffer)| { attribs[index as usize].1.as_ref().map(|buffer| &**buffer)
buffer.as_ref().map(|b| &**b)
})
}) })
} }
fn enabled(&self, index: u32, value: bool) { fn enabled_as_array(&self, index: u32, value: bool) {
let mut elements = self.elements.borrow_mut(); self.attribs.borrow_mut()[index as usize].0 = value;
let pair = elements.entry(index).or_insert((false, None));
pair.0 = value;
} }
fn bind_buffer(&self, index: u32, buffer: &WebGLBuffer) { fn bind_buffer(&self, index: u32, buffer: &WebGLBuffer) {
let mut elements = self.elements.borrow_mut(); self.attribs.borrow_mut()[index as usize].1 = Some(Dom::from_ref(buffer));
let pair = elements.entry(index).or_insert((false, None));
pair.1 = Some(Dom::from_ref(buffer));
} }
} }

View file

@ -9,7 +9,7 @@ use dom::bindings::root::{DomRoot, MutNullableDom};
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
use dom::webglbuffer::WebGLBuffer; use dom::webglbuffer::WebGLBuffer;
use dom::webglobject::WebGLObject; use dom::webglobject::WebGLObject;
use dom::webglrenderingcontext::BoundAttribBuffers; use dom::webglrenderingcontext::VertexAttribs;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use std::cell::Cell; use std::cell::Cell;
@ -19,30 +19,36 @@ pub struct WebGLVertexArrayObjectOES {
id: WebGLVertexArrayId, id: WebGLVertexArrayId,
ever_bound: Cell<bool>, ever_bound: Cell<bool>,
is_deleted: Cell<bool>, is_deleted: Cell<bool>,
bound_attrib_buffers: BoundAttribBuffers, vertex_attribs: VertexAttribs,
bound_buffer_element_array: MutNullableDom<WebGLBuffer>, bound_buffer_element_array: MutNullableDom<WebGLBuffer>,
} }
impl WebGLVertexArrayObjectOES { impl WebGLVertexArrayObjectOES {
fn new_inherited(id: WebGLVertexArrayId) -> WebGLVertexArrayObjectOES { fn new_inherited(id: WebGLVertexArrayId, max_vertex_attribs: u32) -> Self {
Self { Self {
webgl_object_: WebGLObject::new_inherited(), webgl_object_: WebGLObject::new_inherited(),
id: id, id: id,
ever_bound: Cell::new(false), ever_bound: Cell::new(false),
is_deleted: Cell::new(false), is_deleted: Cell::new(false),
bound_attrib_buffers: Default::default(), vertex_attribs: VertexAttribs::new(max_vertex_attribs),
bound_buffer_element_array: MutNullableDom::new(None), bound_buffer_element_array: MutNullableDom::new(None),
} }
} }
pub fn new(global: &GlobalScope, id: WebGLVertexArrayId) -> DomRoot<WebGLVertexArrayObjectOES> { pub fn new(
reflect_dom_object(Box::new(WebGLVertexArrayObjectOES::new_inherited(id)), global: &GlobalScope,
global, id: WebGLVertexArrayId,
WebGLVertexArrayObjectOESBinding::Wrap) max_vertex_attribs: u32,
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(WebGLVertexArrayObjectOES::new_inherited(id, max_vertex_attribs)),
global,
WebGLVertexArrayObjectOESBinding::Wrap,
)
} }
pub fn bound_attrib_buffers(&self) -> &BoundAttribBuffers { pub fn vertex_attribs(&self) -> &VertexAttribs {
&self.bound_attrib_buffers &self.vertex_attribs
} }
pub fn id(&self) -> WebGLVertexArrayId { pub fn id(&self) -> WebGLVertexArrayId {

View file

@ -52,6 +52,7 @@ fn is_unrooted_ty(cx: &LateContext, ty: &ty::TyS, in_new_function: bool) -> bool
} else if match_def_path(cx, did.did, &["core", "cell", "Ref"]) } else if match_def_path(cx, did.did, &["core", "cell", "Ref"])
|| match_def_path(cx, did.did, &["core", "cell", "RefMut"]) || match_def_path(cx, did.did, &["core", "cell", "RefMut"])
|| match_def_path(cx, did.did, &["core", "slice", "Iter"]) || match_def_path(cx, did.did, &["core", "slice", "Iter"])
|| match_def_path(cx, did.did, &["core", "slice", "IterMut"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Entry"]) || match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Entry"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "OccupiedEntry"]) || match_def_path(cx, did.did, &["std", "collections", "hash", "map", "OccupiedEntry"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "VacantEntry"]) || match_def_path(cx, did.did, &["std", "collections", "hash", "map", "VacantEntry"])