mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Store active attribs in DOM and optimise active attributes APIs
This commit is contained in:
parent
0e2e834d18
commit
fc593c68c5
6 changed files with 166 additions and 125 deletions
|
@ -32,7 +32,7 @@
|
|||
use app_units::Au;
|
||||
use canvas_traits::canvas::{CanvasGradientStop, CanvasId, LinearGradientStyle, RadialGradientStyle};
|
||||
use canvas_traits::canvas::{CompositionOrBlending, LineCapStyle, LineJoinStyle, RepetitionStyle};
|
||||
use canvas_traits::webgl::{WebGLBufferId, WebGLFramebufferId, WebGLProgramId, WebGLRenderbufferId};
|
||||
use canvas_traits::webgl::{ActiveAttribInfo, WebGLBufferId, WebGLFramebufferId, WebGLProgramId, WebGLRenderbufferId};
|
||||
use canvas_traits::webgl::{WebGLChan, WebGLContextShareMode, WebGLError, WebGLPipeline, WebGLMsgSender};
|
||||
use canvas_traits::webgl::{WebGLReceiver, WebGLSender, WebGLShaderId, WebGLTextureId, WebGLVertexArrayId};
|
||||
use canvas_traits::webgl::{WebGLSLVersion, WebGLVersion};
|
||||
|
@ -342,6 +342,7 @@ unsafe impl<A: JSTraceable, B: JSTraceable, C: JSTraceable> JSTraceable for (A,
|
|||
}
|
||||
}
|
||||
|
||||
unsafe_no_jsmanaged_fields!(ActiveAttribInfo);
|
||||
unsafe_no_jsmanaged_fields!(bool, f32, f64, String, AtomicBool, AtomicUsize, Uuid, char);
|
||||
unsafe_no_jsmanaged_fields!(usize, u8, u16, u32, u64);
|
||||
unsafe_no_jsmanaged_fields!(isize, i8, i16, i32, i64);
|
||||
|
|
|
@ -3,8 +3,10 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
|
||||
use canvas_traits::webgl::{WebGLCommand, WebGLError, WebGLMsgSender, WebGLProgramId, WebGLResult};
|
||||
use canvas_traits::webgl::webgl_channel;
|
||||
use canvas_traits::webgl::{ActiveAttribInfo, WebGLCommand, WebGLError, WebGLMsgSender};
|
||||
use canvas_traits::webgl::{WebGLProgramId, WebGLResult, from_name_in_compiled_shader};
|
||||
use canvas_traits::webgl::{to_name_in_compiled_shader, webgl_channel};
|
||||
use dom::bindings::cell::DomRefCell;
|
||||
use dom::bindings::codegen::Bindings::WebGLProgramBinding;
|
||||
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
|
||||
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||
|
@ -16,7 +18,7 @@ use dom::webglrenderingcontext::MAX_UNIFORM_AND_ATTRIBUTE_LEN;
|
|||
use dom::webglshader::WebGLShader;
|
||||
use dom::window::Window;
|
||||
use dom_struct::dom_struct;
|
||||
use std::cell::Cell;
|
||||
use std::cell::{Cell, Ref};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct WebGLProgram {
|
||||
|
@ -29,46 +31,7 @@ pub struct WebGLProgram {
|
|||
vertex_shader: MutNullableDom<WebGLShader>,
|
||||
#[ignore_malloc_size_of = "Defined in ipc-channel"]
|
||||
renderer: WebGLMsgSender,
|
||||
}
|
||||
|
||||
/// ANGLE adds a `_u` prefix to variable names:
|
||||
///
|
||||
/// https://chromium.googlesource.com/angle/angle/+/855d964bd0d05f6b2cb303f625506cf53d37e94f
|
||||
///
|
||||
/// To avoid hard-coding this we would need to use the `sh::GetAttributes` and `sh::GetUniforms`
|
||||
/// API to look up the `x.name` and `x.mappedName` members,
|
||||
/// then build a data structure for bi-directional lookup (so either linear scan or two hashmaps).
|
||||
/// Even then, this would probably only support plain variable names like "foo".
|
||||
/// Strings passed to e.g. `GetUniformLocation` can be expressions like "foo[0].bar",
|
||||
/// with the mapping for that "bar" name in yet another part of ANGLE’s API.
|
||||
const ANGLE_NAME_PREFIX: &'static str = "_u";
|
||||
|
||||
fn to_name_in_compiled_shader(s: &str) -> String {
|
||||
map_dot_separated(s, |s, mapped| {
|
||||
mapped.push_str(ANGLE_NAME_PREFIX);
|
||||
mapped.push_str(s);
|
||||
})
|
||||
}
|
||||
|
||||
fn from_name_in_compiled_shader(s: &str) -> String {
|
||||
map_dot_separated(s, |s, mapped| {
|
||||
mapped.push_str(if s.starts_with(ANGLE_NAME_PREFIX) {
|
||||
&s[ANGLE_NAME_PREFIX.len()..]
|
||||
} else {
|
||||
s
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn map_dot_separated<F: Fn(&str, &mut String)>(s: &str, f: F) -> String {
|
||||
let mut iter = s.split('.');
|
||||
let mut mapped = String::new();
|
||||
f(iter.next().unwrap(), &mut mapped);
|
||||
for s in iter {
|
||||
mapped.push('.');
|
||||
f(s, &mut mapped);
|
||||
}
|
||||
mapped
|
||||
active_attribs: DomRefCell<Box<[ActiveAttribInfo]>>,
|
||||
}
|
||||
|
||||
impl WebGLProgram {
|
||||
|
@ -84,6 +47,7 @@ impl WebGLProgram {
|
|||
fragment_shader: Default::default(),
|
||||
vertex_shader: Default::default(),
|
||||
renderer: renderer,
|
||||
active_attribs: DomRefCell::new(vec![].into()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,7 +106,7 @@ impl WebGLProgram {
|
|||
return Err(WebGLError::InvalidOperation);
|
||||
}
|
||||
self.linked.set(false);
|
||||
self.link_called.set(true);
|
||||
*self.active_attribs.borrow_mut() = vec![].into();
|
||||
|
||||
match self.fragment_shader.get() {
|
||||
Some(ref shader) if shader.successfully_compiled() => {},
|
||||
|
@ -154,11 +118,18 @@ impl WebGLProgram {
|
|||
_ => return Ok(()), // callers use gl.LINK_STATUS to check link errors
|
||||
}
|
||||
|
||||
self.linked.set(true);
|
||||
self.renderer.send(WebGLCommand::LinkProgram(self.id)).unwrap();
|
||||
let (sender, receiver) = webgl_channel().unwrap();
|
||||
self.renderer.send(WebGLCommand::LinkProgram(self.id, sender)).unwrap();
|
||||
let link_info = receiver.recv().unwrap();
|
||||
self.linked.set(link_info.linked);
|
||||
*self.active_attribs.borrow_mut() = link_info.active_attribs;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn active_attribs(&self) -> Ref<[ActiveAttribInfo]> {
|
||||
Ref::map(self.active_attribs.borrow(), |attribs| &**attribs)
|
||||
}
|
||||
|
||||
/// glUseProgram
|
||||
pub fn use_program(&self) -> WebGLResult<()> {
|
||||
if self.is_deleted() {
|
||||
|
@ -281,19 +252,18 @@ impl WebGLProgram {
|
|||
if self.is_deleted() {
|
||||
return Err(WebGLError::InvalidValue);
|
||||
}
|
||||
let (sender, receiver) = webgl_channel().unwrap();
|
||||
self.renderer
|
||||
.send(WebGLCommand::GetActiveAttrib(self.id, index, sender))
|
||||
.unwrap();
|
||||
|
||||
receiver.recv().unwrap().map(|(size, ty, name)| {
|
||||
let name = DOMString::from(from_name_in_compiled_shader(&name));
|
||||
WebGLActiveInfo::new(self.global().as_window(), size, ty, name)
|
||||
})
|
||||
let attribs = self.active_attribs.borrow();
|
||||
let data = attribs.get(index as usize).ok_or(WebGLError::InvalidValue)?;
|
||||
Ok(WebGLActiveInfo::new(
|
||||
self.global().as_window(),
|
||||
data.size,
|
||||
data.type_,
|
||||
data.name.clone().into(),
|
||||
))
|
||||
}
|
||||
|
||||
/// glGetAttribLocation
|
||||
pub fn get_attrib_location(&self, name: DOMString) -> WebGLResult<Option<i32>> {
|
||||
pub fn get_attrib_location(&self, name: DOMString) -> WebGLResult<i32> {
|
||||
if !self.is_linked() || self.is_deleted() {
|
||||
return Err(WebGLError::InvalidOperation);
|
||||
}
|
||||
|
@ -303,21 +273,20 @@ impl WebGLProgram {
|
|||
|
||||
// Check if the name is reserved
|
||||
if name.starts_with("gl_") {
|
||||
return Ok(None);
|
||||
return Ok(-1);
|
||||
}
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#GLSL_CONSTRUCTS
|
||||
if name.starts_with("webgl_") || name.starts_with("_webgl_") {
|
||||
return Ok(None);
|
||||
return Ok(-1);
|
||||
}
|
||||
|
||||
let name = to_name_in_compiled_shader(&name);
|
||||
|
||||
let (sender, receiver) = webgl_channel().unwrap();
|
||||
self.renderer
|
||||
.send(WebGLCommand::GetAttribLocation(self.id, name, sender))
|
||||
.unwrap();
|
||||
Ok(receiver.recv().unwrap())
|
||||
let location = self.active_attribs
|
||||
.borrow()
|
||||
.iter()
|
||||
.find(|attrib| attrib.name == &*name)
|
||||
.map_or(-1, |attrib| attrib.location);
|
||||
Ok(location)
|
||||
}
|
||||
|
||||
/// glGetUniformLocation
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
|
||||
use canvas_traits::canvas::{byte_swap, multiply_u8_pixel};
|
||||
use canvas_traits::webgl::{DOMToTextureCommand, Parameter, ProgramParameter};
|
||||
use canvas_traits::webgl::{DOMToTextureCommand, Parameter};
|
||||
use canvas_traits::webgl::{ShaderParameter, TexParameter, WebGLCommand};
|
||||
use canvas_traits::webgl::{WebGLContextShareMode, WebGLError};
|
||||
use canvas_traits::webgl::{WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender};
|
||||
|
@ -2314,18 +2314,12 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
|
|||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
|
||||
fn GetActiveAttrib(&self, program: &WebGLProgram, index: u32) -> Option<DomRoot<WebGLActiveInfo>> {
|
||||
match program.get_active_attrib(index) {
|
||||
Ok(ret) => Some(ret),
|
||||
Err(e) => {
|
||||
self.webgl_error(e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
handle_potential_webgl_error!(self, program.get_active_attrib(index), None)
|
||||
}
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
|
||||
fn GetAttribLocation(&self, program: &WebGLProgram, name: DOMString) -> i32 {
|
||||
handle_potential_webgl_error!(self, program.get_attrib_location(name), None).unwrap_or(-1)
|
||||
handle_potential_webgl_error!(self, program.get_attrib_location(name), -1)
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
|
@ -2497,17 +2491,32 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
|
|||
#[allow(unsafe_code)]
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
|
||||
unsafe fn GetProgramParameter(&self, _: *mut JSContext, program: &WebGLProgram, param: u32) -> JSVal {
|
||||
match handle_potential_webgl_error!(self, ProgramParameter::from_u32(param), return NullValue()) {
|
||||
ProgramParameter::Bool(param) => {
|
||||
// FIXME(nox): INVALID_OPERATION if program comes from a different context.
|
||||
match param {
|
||||
constants::DELETE_STATUS => BooleanValue(program.is_deleted()),
|
||||
constants::LINK_STATUS => BooleanValue(program.is_linked()),
|
||||
constants::VALIDATE_STATUS => {
|
||||
// FIXME(nox): This could be cached on the DOM side when we call validateProgram
|
||||
// but I'm not sure when the value should be reset.
|
||||
let (sender, receiver) = webgl_channel().unwrap();
|
||||
self.send_command(WebGLCommand::GetProgramParameterBool(program.id(), param, sender));
|
||||
self.send_command(WebGLCommand::GetProgramValidateStatus(program.id(), sender));
|
||||
BooleanValue(receiver.recv().unwrap())
|
||||
}
|
||||
ProgramParameter::Int(param) => {
|
||||
constants::ATTACHED_SHADERS => {
|
||||
// FIXME(nox): This allocates a vector and roots a couple of shaders for nothing.
|
||||
Int32Value(program.attached_shaders().map(|shaders| shaders.len() as i32).unwrap_or(0))
|
||||
}
|
||||
constants::ACTIVE_ATTRIBUTES => Int32Value(program.active_attribs().len() as i32),
|
||||
constants::ACTIVE_UNIFORMS => {
|
||||
// FIXME(nox): We'll need to cache that on the DOM side at some point.
|
||||
let (sender, receiver) = webgl_channel().unwrap();
|
||||
self.send_command(WebGLCommand::GetProgramParameterInt(program.id(), param, sender));
|
||||
self.send_command(WebGLCommand::GetProgramActiveUniforms(program.id(), sender));
|
||||
Int32Value(receiver.recv().unwrap())
|
||||
}
|
||||
_ => {
|
||||
self.webgl_error(InvalidEnum);
|
||||
NullValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue