Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use active uniforms data to implement gl.uniform* checks #21184

Merged
merged 4 commits into from Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
140 changes: 81 additions & 59 deletions components/canvas/webgl_thread.rs
Expand Up @@ -646,8 +646,9 @@ impl WebGLImpl {
ctx.gl().attach_shader(program_id.get(), shader_id.get()),
WebGLCommand::DetachShader(program_id, shader_id) =>
ctx.gl().detach_shader(program_id.get(), shader_id.get()),
WebGLCommand::BindAttribLocation(program_id, index, ref name) =>
ctx.gl().bind_attrib_location(program_id.get(), index, name),
WebGLCommand::BindAttribLocation(program_id, index, ref name) => {
ctx.gl().bind_attrib_location(program_id.get(), index, &to_name_in_compiled_shader(name))
}
WebGLCommand::BlendColor(r, g, b, a) =>
ctx.gl().blend_color(r, g, b, a),
WebGLCommand::BlendEquation(mode) =>
Expand Down Expand Up @@ -736,10 +737,6 @@ impl WebGLImpl {
ctx.gl().stencil_op(fail, zfail, zpass),
WebGLCommand::StencilOpSeparate(face, fail, zfail, zpass) =>
ctx.gl().stencil_op_separate(face, fail, zfail, zpass),
WebGLCommand::GetActiveAttrib(program_id, index, ref chan) =>
Self::active_attrib(ctx.gl(), program_id, index, chan),
WebGLCommand::GetActiveUniform(program_id, index, ref chan) =>
Self::active_uniform(ctx.gl(), program_id, index, chan),
WebGLCommand::GetRenderbufferParameter(target, pname, ref chan) =>
Self::get_renderbuffer_parameter(ctx.gl(), target, pname, chan),
WebGLCommand::GetFramebufferAttachmentParameter(target, attachment, pname, ref chan) =>
Expand Down Expand Up @@ -820,12 +817,15 @@ impl WebGLImpl {
ctx.gl().uniform_4i(uniform_id, x, y, z, w),
WebGLCommand::Uniform4iv(uniform_id, ref v) =>
ctx.gl().uniform_4iv(uniform_id, v),
WebGLCommand::UniformMatrix2fv(uniform_id, transpose, ref v) =>
ctx.gl().uniform_matrix_2fv(uniform_id, transpose, v),
WebGLCommand::UniformMatrix3fv(uniform_id, transpose, ref v) =>
ctx.gl().uniform_matrix_3fv(uniform_id, transpose, v),
WebGLCommand::UniformMatrix4fv(uniform_id, transpose, ref v) =>
ctx.gl().uniform_matrix_4fv(uniform_id, transpose, v),
WebGLCommand::UniformMatrix2fv(uniform_id, ref v) => {
ctx.gl().uniform_matrix_2fv(uniform_id, false, v)
}
WebGLCommand::UniformMatrix3fv(uniform_id, ref v) => {
ctx.gl().uniform_matrix_3fv(uniform_id, false, v)
}
WebGLCommand::UniformMatrix4fv(uniform_id, ref v) => {
ctx.gl().uniform_matrix_4fv(uniform_id, false, v)
}
WebGLCommand::ValidateProgram(program_id) =>
ctx.gl().validate_program(program_id.get()),
WebGLCommand::VertexAttrib(attrib_id, x, y, z, w) =>
Expand Down Expand Up @@ -1003,6 +1003,7 @@ impl WebGLImpl {
return ProgramLinkInfo {
linked: false,
active_attribs: vec![].into(),
active_uniforms: vec![].into(),
}
}

Expand All @@ -1011,6 +1012,8 @@ impl WebGLImpl {
gl.get_program_iv(program.get(), gl::ACTIVE_ATTRIBUTES, &mut num_active_attribs);
}
let active_attribs = (0..num_active_attribs[0] as u32).map(|i| {
// FIXME(nox): This allocates strings sometimes for nothing
// and the gleam method keeps getting ACTIVE_ATTRIBUTE_MAX_LENGTH.
let (size, type_, name) = gl.get_active_attrib(program.get(), i);
let location = if name.starts_with("gl_") {
-1
Expand All @@ -1025,9 +1028,31 @@ impl WebGLImpl {
}
}).collect::<Vec<_>>().into();

let mut num_active_uniforms = [0];
unsafe {
gl.get_program_iv(program.get(), gl::ACTIVE_UNIFORMS, &mut num_active_uniforms);
}
let active_uniforms = (0..num_active_uniforms[0] as u32).map(|i| {
// FIXME(nox): This allocates strings sometimes for nothing
// and the gleam method keeps getting ACTIVE_UNIFORM_MAX_LENGTH.
let (size, type_, mut name) = gl.get_active_uniform(program.get(), i);
let is_array = name.ends_with("[0]");
if is_array {
// FIXME(nox): NLL
let len = name.len();
name.truncate(len - 3);
}
ActiveUniformInfo {
base_name: from_name_in_compiled_shader(&name).into(),
size: if is_array { Some(size) } else { None },
type_,
}
}).collect::<Vec<_>>().into();

ProgramLinkInfo {
linked: true,
active_attribs,
active_uniforms,
}
}

Expand All @@ -1045,42 +1070,6 @@ impl WebGLImpl {
chan.send(result.into()).unwrap()
}

#[allow(unsafe_code)]
fn active_attrib(
gl: &gl::Gl,
program_id: WebGLProgramId,
index: u32,
chan: &WebGLSender<WebGLResult<(i32, u32, String)>>,
) {
let mut max = [0];
unsafe {
gl.get_program_iv(program_id.get(), gl::ACTIVE_ATTRIBUTES, &mut max);
}
let result = if index >= max[0] as u32 {
Err(WebGLError::InvalidValue)
} else {
Ok(gl.get_active_attrib(program_id.get(), index))
};
chan.send(result).unwrap();
}

#[allow(unsafe_code)]
fn active_uniform(gl: &gl::Gl,
program_id: WebGLProgramId,
index: u32,
chan: &WebGLSender<WebGLResult<(i32, u32, String)>>) {
let mut max = [0];
unsafe {
gl.get_program_iv(program_id.get(), gl::ACTIVE_UNIFORMS, &mut max);
}
let result = if index >= max[0] as u32 {
Err(WebGLError::InvalidValue)
} else {
Ok(gl.get_active_uniform(program_id.get(), index))
};
chan.send(result).unwrap();
}

fn finish(gl: &gl::Gl, chan: &WebGLSender<()>) {
gl.finish();
chan.send(()).unwrap();
Expand Down Expand Up @@ -1121,17 +1110,14 @@ impl WebGLImpl {
chan.send(parameter).unwrap();
}

fn uniform_location(gl: &gl::Gl,
program_id: WebGLProgramId,
name: &str,
chan: &WebGLSender<Option<i32>>) {
let location = gl.get_uniform_location(program_id.get(), name);
let location = if location == -1 {
None
} else {
Some(location)
};

fn uniform_location(
gl: &gl::Gl,
program_id: WebGLProgramId,
name: &str,
chan: &WebGLSender<i32>,
) {
let location = gl.get_uniform_location(program_id.get(), &to_name_in_compiled_shader(name));
assert!(location >= 0);
chan.send(location).unwrap();
}

Expand Down Expand Up @@ -1244,3 +1230,39 @@ impl WebGLImpl {
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
}
72 changes: 30 additions & 42 deletions components/canvas_traits/webgl.rs
Expand Up @@ -6,6 +6,7 @@ use euclid::Size2D;
use gleam::gl;
use offscreen_gl_context::{GLContextAttributes, GLLimits};
use serde_bytes::ByteBuf;
use std::borrow::Cow;
use std::num::NonZeroU32;
use webrender_api::{DocumentId, ImageKey, PipelineId};

Expand Down Expand Up @@ -207,9 +208,7 @@ pub enum WebGLCommand {
FramebufferTexture2D(u32, u32, u32, Option<WebGLTextureId>, i32),
GetExtensions(WebGLSender<String>),
GetShaderPrecisionFormat(u32, u32, WebGLSender<(i32, i32, i32)>),
GetActiveAttrib(WebGLProgramId, u32, WebGLSender<WebGLResult<(i32, u32, String)>>),
GetActiveUniform(WebGLProgramId, u32, WebGLSender<WebGLResult<(i32, u32, String)>>),
GetUniformLocation(WebGLProgramId, String, WebGLSender<Option<i32>>),
GetUniformLocation(WebGLProgramId, String, WebGLSender<i32>),
GetShaderInfoLog(WebGLShaderId, WebGLSender<String>),
GetProgramInfoLog(WebGLProgramId, WebGLSender<String>),
GetFramebufferAttachmentParameter(u32, u32, u32, WebGLSender<i32>),
Expand Down Expand Up @@ -246,9 +245,9 @@ pub enum WebGLCommand {
Uniform4fv(i32, Vec<f32>),
Uniform4i(i32, i32, i32, i32, i32),
Uniform4iv(i32, Vec<i32>),
UniformMatrix2fv(i32, bool, Vec<f32>),
UniformMatrix3fv(i32, bool, Vec<f32>),
UniformMatrix4fv(i32, bool, Vec<f32>),
UniformMatrix2fv(i32, Vec<f32>),
UniformMatrix3fv(i32, Vec<f32>),
UniformMatrix4fv(i32, Vec<f32>),
UseProgram(Option<WebGLProgramId>),
ValidateProgram(WebGLProgramId),
VertexAttrib(u32, f32, f32, f32, f32),
Expand Down Expand Up @@ -425,6 +424,8 @@ pub struct ProgramLinkInfo {
pub linked: bool,
/// The list of active attributes.
pub active_attribs: Box<[ActiveAttribInfo]>,
/// The list of active uniforms.
pub active_uniforms: Box<[ActiveUniformInfo]>,
}

/// Description of a single active attribute.
Expand All @@ -440,6 +441,29 @@ pub struct ActiveAttribInfo {
pub location: i32,
}

/// Description of a single active uniform.
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct ActiveUniformInfo {
/// The base name of the uniform.
pub base_name: Box<str>,
/// The size of the uniform, if it is an array.
pub size: Option<i32>,
/// The type of the uniform.
pub type_: u32,
}

impl ActiveUniformInfo {
pub fn name(&self) -> Cow<str> {
if self.size.is_some() {
let mut name = String::from(&*self.base_name);
name.push_str("[0]");
Cow::Owned(name)
} else {
Cow::Borrowed(&self.base_name)
}
}
}

macro_rules! parameters {
($name:ident { $(
$variant:ident($kind:ident { $(
Expand Down Expand Up @@ -578,39 +602,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
}
10 changes: 6 additions & 4 deletions components/script/dom/bindings/trace.rs
Expand Up @@ -32,10 +32,11 @@
use app_units::Au;
use canvas_traits::canvas::{CanvasGradientStop, CanvasId, LinearGradientStyle, RadialGradientStyle};
use canvas_traits::canvas::{CompositionOrBlending, LineCapStyle, LineJoinStyle, RepetitionStyle};
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};
use canvas_traits::webgl::{ActiveAttribInfo, ActiveUniformInfo, WebGLBufferId, WebGLChan};
use canvas_traits::webgl::{WebGLContextShareMode, WebGLError, WebGLFramebufferId, WebGLMsgSender};
use canvas_traits::webgl::{WebGLPipeline, WebGLProgramId, WebGLReceiver, WebGLRenderbufferId};
use canvas_traits::webgl::{WebGLSLVersion, WebGLSender, WebGLShaderId, WebGLTextureId};
use canvas_traits::webgl::{WebGLVersion, WebGLVertexArrayId};
use cssparser::RGBA;
use devtools_traits::{CSSError, TimelineMarkerType, WorkerId};
use dom::abstractworker::SharedRt;
Expand Down Expand Up @@ -343,6 +344,7 @@ unsafe impl<A: JSTraceable, B: JSTraceable, C: JSTraceable> JSTraceable for (A,
}

unsafe_no_jsmanaged_fields!(ActiveAttribInfo);
unsafe_no_jsmanaged_fields!(ActiveUniformInfo);
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);
Expand Down