Skip to content

Commit

Permalink
Switch from servo/angle to the mozangle crate
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Mar 12, 2018
1 parent 324e22d commit 67d983c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 28 deletions.
37 changes: 20 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/script/Cargo.toml
Expand Up @@ -99,4 +99,4 @@ webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}
webvr_traits = {path = "../webvr_traits"}

[target.'cfg(not(target_os = "ios"))'.dependencies]
angle = {git = "https://github.com/servo/angle", branch = "servo"}
mozangle = "0.1"
64 changes: 57 additions & 7 deletions components/script/dom/webglprogram.rs
Expand Up @@ -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 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
}

impl WebGLProgram {
fn new_inherited(renderer: WebGLMsgSender,
id: WebGLProgramId)
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/webglshader.rs
Expand Up @@ -3,7 +3,6 @@
* 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 angle::hl::{BuiltInResources, Output, ShaderValidator};
use canvas_traits::webgl::{WebGLSLVersion, WebGLVersion};
use canvas_traits::webgl::{webgl_channel, WebGLCommand, WebGLMsgSender, WebGLParameter, WebGLResult, WebGLShaderId};
use dom::bindings::cell::DomRefCell;
Expand All @@ -16,6 +15,7 @@ use dom::webgl_extensions::ext::oesstandardderivatives::OESStandardDerivatives;
use dom::webglobject::WebGLObject;
use dom::window::Window;
use dom_struct::dom_struct;
use mozangle::shaders::{BuiltInResources, Output, ShaderValidator};
use std::cell::Cell;
use std::sync::{ONCE_INIT, Once};

Expand Down Expand Up @@ -47,7 +47,7 @@ impl WebGLShader {
id: WebGLShaderId,
shader_type: u32)
-> WebGLShader {
GLSLANG_INITIALIZATION.call_once(|| ::angle::hl::initialize().unwrap());
GLSLANG_INITIALIZATION.call_once(|| ::mozangle::shaders::initialize().unwrap());
WebGLShader {
webgl_object: WebGLObject::new_inherited(),
id: id,
Expand Down
2 changes: 1 addition & 1 deletion components/script/lib.rs
Expand Up @@ -21,7 +21,6 @@
#![plugin(script_plugins)]
#![cfg_attr(not(feature = "unrooted_must_root_lint"), allow(unknown_lints))]

extern crate angle;
extern crate app_units;
extern crate audio_video_metadata;
extern crate base64;
Expand Down Expand Up @@ -64,6 +63,7 @@ extern crate metrics;
extern crate mime;
extern crate mime_guess;
extern crate mitochondria;
extern crate mozangle;
#[macro_use]
extern crate mozjs as js;
extern crate msg;
Expand Down

0 comments on commit 67d983c

Please sign in to comment.