mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Rename BoundAttribBuffers to VertexAttribs and make it store a slice
This commit is contained in:
parent
c71c55e542
commit
5d43f1c9bd
4 changed files with 69 additions and 53 deletions
|
@ -193,7 +193,7 @@ pub struct WebGLRenderingContext {
|
|||
bound_texture_unit: Cell<u32>,
|
||||
bound_buffer_array: MutNullableDom<WebGLBuffer>,
|
||||
bound_buffer_element_array: MutNullableDom<WebGLBuffer>,
|
||||
bound_attrib_buffers: BoundAttribBuffers,
|
||||
vertex_attribs: VertexAttribs,
|
||||
current_program: MutNullableDom<WebGLProgram>,
|
||||
#[ignore_malloc_size_of = "Because it's small"]
|
||||
current_vertex_attrib_0: Cell<(f32, f32, f32, f32)>,
|
||||
|
@ -234,6 +234,7 @@ impl WebGLRenderingContext {
|
|||
share_mode: ctx_data.share_mode,
|
||||
webgl_version,
|
||||
glsl_version: ctx_data.glsl_version,
|
||||
vertex_attribs: VertexAttribs::new(ctx_data.limits.max_vertex_attribs),
|
||||
limits: ctx_data.limits,
|
||||
canvas: Dom::from_ref(canvas),
|
||||
last_error: Cell::new(None),
|
||||
|
@ -244,7 +245,6 @@ impl WebGLRenderingContext {
|
|||
bound_texture_unit: Cell::new(constants::TEXTURE0),
|
||||
bound_buffer_array: MutNullableDom::new(None),
|
||||
bound_buffer_element_array: MutNullableDom::new(None),
|
||||
bound_attrib_buffers: Default::default(),
|
||||
bound_renderbuffer: MutNullableDom::new(None),
|
||||
current_program: MutNullableDom::new(None),
|
||||
current_vertex_attrib_0: Cell::new((0f32, 0f32, 0f32, 1f32)),
|
||||
|
@ -312,8 +312,8 @@ impl WebGLRenderingContext {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn bound_attrib_buffers(&self) -> &BoundAttribBuffers {
|
||||
&self.bound_attrib_buffers
|
||||
pub fn vertex_attribs(&self) -> &VertexAttribs {
|
||||
&self.vertex_attribs
|
||||
}
|
||||
|
||||
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.
|
||||
self.bound_attrib_buffers.remove_buffer(buffer);
|
||||
self.vertex_attribs.delete_buffer(buffer);
|
||||
|
||||
// Delete 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
|
||||
let buffers = self.bound_attrib_buffers.borrow();
|
||||
if buffers.iter().any(|(_, &(enabled, ref buffer))| enabled && buffer.is_none()) {
|
||||
let buffers = self.vertex_attribs.borrow();
|
||||
if buffers.iter().any(|&(enabled, ref buffer)| enabled && buffer.is_none()) {
|
||||
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
|
||||
let buffers = self.bound_attrib_buffers.borrow();
|
||||
if buffers.iter().any(|(_, &(enabled, ref buffer))| enabled && buffer.is_none()) {
|
||||
let buffers = self.vertex_attribs.borrow();
|
||||
if buffers.iter().any(|&(enabled, ref buffer)| enabled && buffer.is_none()) {
|
||||
return self.webgl_error(InvalidOperation);
|
||||
}
|
||||
}
|
||||
|
@ -2299,7 +2299,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
|
|||
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));
|
||||
}
|
||||
|
||||
|
@ -2309,7 +2309,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
|
|||
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));
|
||||
}
|
||||
|
||||
|
@ -2601,7 +2601,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
|
|||
|
||||
if param == constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING {
|
||||
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());
|
||||
}
|
||||
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);
|
||||
self.send_command(msg);
|
||||
|
@ -3805,48 +3805,51 @@ impl UniformSetterType {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, JSTraceable, MallocSizeOf)]
|
||||
#[derive(JSTraceable, MallocSizeOf)]
|
||||
#[must_root]
|
||||
pub struct BoundAttribBuffers {
|
||||
elements: DomRefCell<FnvHashMap<u32, (bool, Option<Dom<WebGLBuffer>>)>>,
|
||||
pub struct VertexAttribs {
|
||||
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) {
|
||||
self.elements.borrow_mut().clear()
|
||||
for attrib in &mut **self.attribs.borrow_mut() {
|
||||
*attrib = Default::default();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_from(&self, other: &BoundAttribBuffers) {
|
||||
*self.elements.borrow_mut() = other.elements.borrow().clone();
|
||||
pub fn set_from(&self, other: &Self) {
|
||||
self.attribs.borrow_mut().clone_from_slice(&other.attribs.borrow());
|
||||
}
|
||||
|
||||
pub fn borrow(&self) -> Ref<FnvHashMap<u32, (bool, Option<Dom<WebGLBuffer>>)>> {
|
||||
self.elements.borrow()
|
||||
pub fn borrow(&self) -> Ref<[(bool, Option<Dom<WebGLBuffer>>)]> {
|
||||
Ref::map(self.attribs.borrow(), |attribs| &**attribs)
|
||||
}
|
||||
|
||||
fn remove_buffer(&self, buffer: &WebGLBuffer) {
|
||||
self.elements.borrow_mut().retain(|_, v| {
|
||||
v.1.as_ref().map_or(true, |b| b.id() != buffer.id())
|
||||
})
|
||||
fn delete_buffer(&self, buffer: &WebGLBuffer) {
|
||||
for attrib in &mut **self.attribs.borrow_mut() {
|
||||
if attrib.1.as_ref().map_or(false, |b| b.id() == buffer.id()) {
|
||||
attrib.1 = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&self, index: u32) -> Option<Ref<WebGLBuffer>> {
|
||||
ref_filter_map(self.elements.borrow(), |elements| {
|
||||
elements.get(&index).and_then(|&(_, ref buffer)| {
|
||||
buffer.as_ref().map(|b| &**b)
|
||||
})
|
||||
ref_filter_map(self.attribs.borrow(), |attribs| {
|
||||
attribs[index as usize].1.as_ref().map(|buffer| &**buffer)
|
||||
})
|
||||
}
|
||||
|
||||
fn enabled(&self, index: u32, value: bool) {
|
||||
let mut elements = self.elements.borrow_mut();
|
||||
let pair = elements.entry(index).or_insert((false, None));
|
||||
pair.0 = value;
|
||||
fn enabled_as_array(&self, index: u32, value: bool) {
|
||||
self.attribs.borrow_mut()[index as usize].0 = value;
|
||||
}
|
||||
|
||||
fn bind_buffer(&self, index: u32, buffer: &WebGLBuffer) {
|
||||
let mut elements = self.elements.borrow_mut();
|
||||
let pair = elements.entry(index).or_insert((false, None));
|
||||
pair.1 = Some(Dom::from_ref(buffer));
|
||||
self.attribs.borrow_mut()[index as usize].1 = Some(Dom::from_ref(buffer));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue