Switch from servo/angle to the mozangle crate

https://github.com/servo/mozangle
This commit is contained in:
Simon Sapin 2018-03-05 16:48:01 +01:00
parent 324e22db03
commit 67d983cb12
5 changed files with 81 additions and 28 deletions

View file

@ -31,6 +31,46 @@ pub struct WebGLProgram {
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 ANGLEs 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
}
impl WebGLProgram {
fn new_inherited(renderer: WebGLMsgSender,
id: WebGLProgramId)
@ -213,8 +253,10 @@ impl WebGLProgram {
return Err(WebGLError::InvalidOperation);
}
let name = to_name_in_compiled_shader(&name);
self.renderer
.send(WebGLCommand::BindAttribLocation(self.id, index, String::from(name)))
.send(WebGLCommand::BindAttribLocation(self.id, index, name))
.unwrap();
Ok(())
}
@ -228,8 +270,10 @@ impl WebGLProgram {
.send(WebGLCommand::GetActiveUniform(self.id, index, sender))
.unwrap();
receiver.recv().unwrap().map(|(size, ty, name)|
WebGLActiveInfo::new(self.global().as_window(), size, ty, DOMString::from(name)))
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)
})
}
/// glGetActiveAttrib
@ -242,8 +286,10 @@ impl WebGLProgram {
.send(WebGLCommand::GetActiveAttrib(self.id, index, sender))
.unwrap();
receiver.recv().unwrap().map(|(size, ty, name)|
WebGLActiveInfo::new(self.global().as_window(), size, ty, DOMString::from(name)))
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)
})
}
/// glGetAttribLocation
@ -264,9 +310,11 @@ impl WebGLProgram {
return Ok(None);
}
let name = to_name_in_compiled_shader(&name);
let (sender, receiver) = webgl_channel().unwrap();
self.renderer
.send(WebGLCommand::GetAttribLocation(self.id, String::from(name), sender))
.send(WebGLCommand::GetAttribLocation(self.id, name, sender))
.unwrap();
Ok(receiver.recv().unwrap())
}
@ -285,9 +333,11 @@ impl WebGLProgram {
return Ok(None);
}
let name = to_name_in_compiled_shader(&name);
let (sender, receiver) = webgl_channel().unwrap();
self.renderer
.send(WebGLCommand::GetUniformLocation(self.id, String::from(name), sender))
.send(WebGLCommand::GetUniformLocation(self.id, name, sender))
.unwrap();
Ok(receiver.recv().unwrap())
}