Move ANGLE name shenanigans to WebGL thread

This commit is contained in:
Anthony Ramine 2018-07-11 12:18:51 +02:00
parent cbac5d05be
commit e7631cea61
3 changed files with 50 additions and 51 deletions

View file

@ -646,8 +646,9 @@ impl WebGLImpl {
ctx.gl().attach_shader(program_id.get(), shader_id.get()), ctx.gl().attach_shader(program_id.get(), shader_id.get()),
WebGLCommand::DetachShader(program_id, shader_id) => WebGLCommand::DetachShader(program_id, shader_id) =>
ctx.gl().detach_shader(program_id.get(), shader_id.get()), ctx.gl().detach_shader(program_id.get(), shader_id.get()),
WebGLCommand::BindAttribLocation(program_id, index, ref name) => WebGLCommand::BindAttribLocation(program_id, index, ref name) => {
ctx.gl().bind_attrib_location(program_id.get(), index, name), ctx.gl().bind_attrib_location(program_id.get(), index, &to_name_in_compiled_shader(name))
}
WebGLCommand::BlendColor(r, g, b, a) => WebGLCommand::BlendColor(r, g, b, a) =>
ctx.gl().blend_color(r, g, b, a), ctx.gl().blend_color(r, g, b, a),
WebGLCommand::BlendEquation(mode) => WebGLCommand::BlendEquation(mode) =>
@ -1100,11 +1101,13 @@ impl WebGLImpl {
chan.send(parameter).unwrap(); chan.send(parameter).unwrap();
} }
fn uniform_location(gl: &gl::Gl, fn uniform_location(
program_id: WebGLProgramId, gl: &gl::Gl,
name: &str, program_id: WebGLProgramId,
chan: &WebGLSender<Option<i32>>) { name: &str,
let location = gl.get_uniform_location(program_id.get(), name); chan: &WebGLSender<Option<i32>>,
) {
let location = gl.get_uniform_location(program_id.get(), &to_name_in_compiled_shader(name));
let location = if location == -1 { let location = if location == -1 {
None None
} else { } else {
@ -1223,3 +1226,39 @@ impl WebGLImpl {
gl.compile_shader(shader_id.get()); gl.compile_shader(shader_id.get());
} }
} }
/// 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.
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
}

View file

@ -589,39 +589,3 @@ parameters! {
}), }),
} }
} }
/// 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.
const ANGLE_NAME_PREFIX: &'static str = "_u";
pub 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);
})
}
pub 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
}

View file

@ -3,8 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl // https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
use canvas_traits::webgl::{ActiveAttribInfo, ActiveUniformInfo, WebGLCommand, WebGLError, WebGLMsgSender}; use canvas_traits::webgl::{ActiveAttribInfo, ActiveUniformInfo, WebGLCommand, WebGLError};
use canvas_traits::webgl::{WebGLProgramId, WebGLResult, to_name_in_compiled_shader, webgl_channel}; use canvas_traits::webgl::{WebGLMsgSender, WebGLProgramId, WebGLResult, webgl_channel};
use dom::bindings::cell::DomRefCell; use dom::bindings::cell::DomRefCell;
use dom::bindings::codegen::Bindings::WebGLProgramBinding; use dom::bindings::codegen::Bindings::WebGLProgramBinding;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants; use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
@ -248,10 +248,8 @@ impl WebGLProgram {
return Err(WebGLError::InvalidOperation); return Err(WebGLError::InvalidOperation);
} }
let name = to_name_in_compiled_shader(&name);
self.renderer self.renderer
.send(WebGLCommand::BindAttribLocation(self.id, index, name)) .send(WebGLCommand::BindAttribLocation(self.id, index, name.into()))
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
@ -326,11 +324,9 @@ impl WebGLProgram {
return Ok(None); return Ok(None);
} }
let name = to_name_in_compiled_shader(&name);
let (sender, receiver) = webgl_channel().unwrap(); let (sender, receiver) = webgl_channel().unwrap();
self.renderer self.renderer
.send(WebGLCommand::GetUniformLocation(self.id, name, sender)) .send(WebGLCommand::GetUniformLocation(self.id, name.into(), sender))
.unwrap(); .unwrap();
Ok(receiver.recv().unwrap()) Ok(receiver.recv().unwrap())
} }