aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/webglrenderingcontext.rs333
-rw-r--r--components/script/dom/webidls/WebGLRenderingContext.webidl11
2 files changed, 222 insertions, 122 deletions
diff --git a/components/script/dom/webglrenderingcontext.rs b/components/script/dom/webglrenderingcontext.rs
index 4c6996cbb90..197dcce55f5 100644
--- a/components/script/dom/webglrenderingcontext.rs
+++ b/components/script/dom/webglrenderingcontext.rs
@@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use canvas_traits::{CanvasCommonMsg, CanvasMsg};
+use dom::bindings::codegen::Bindings::WebGLActiveInfoBinding::WebGLActiveInfoMethods;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{WebGLRenderingContextMethods};
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{self, WebGLContextAttributes};
@@ -66,6 +67,45 @@ bitflags! {
}
}
+pub enum UniformType {
+ Int,
+ IntVec2,
+ IntVec3,
+ IntVec4,
+ Float,
+ FloatVec2,
+ FloatVec3,
+ FloatVec4,
+}
+
+impl UniformType {
+ fn element_count(&self) -> usize {
+ match *self {
+ UniformType::Int => 1,
+ UniformType::IntVec2 => 2,
+ UniformType::IntVec3 => 3,
+ UniformType::IntVec4 => 4,
+ UniformType::Float => 1,
+ UniformType::FloatVec2 => 2,
+ UniformType::FloatVec3 => 3,
+ UniformType::FloatVec4 => 4,
+ }
+ }
+
+ fn as_gl_constant(&self) -> u32 {
+ match *self {
+ UniformType::Int => constants::INT,
+ UniformType::IntVec2 => constants::INT_VEC2,
+ UniformType::IntVec3 => constants::INT_VEC3,
+ UniformType::IntVec4 => constants::INT_VEC4,
+ UniformType::Float => constants::FLOAT,
+ UniformType::FloatVec2 => constants::FLOAT_VEC2,
+ UniformType::FloatVec3 => constants::FLOAT_VEC3,
+ UniformType::FloatVec4 => constants::FLOAT_VEC4,
+ }
+ }
+}
+
#[dom_struct]
pub struct WebGLRenderingContext {
reflector_: Reflector,
@@ -178,6 +218,59 @@ impl WebGLRenderingContext {
.send(CanvasMsg::WebGL(WebGLCommand::VertexAttrib(indx, x, y, z, w)))
.unwrap();
}
+
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml
+ // https://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.25.pdf#nameddest=section-2.10.4
+ fn validate_uniform_parameters<T>(&self,
+ uniform: Option<&WebGLUniformLocation>,
+ type_: UniformType,
+ data: Option<&[T]>) -> bool {
+ let uniform = match uniform {
+ Some(uniform) => uniform,
+ None => return false,
+ };
+
+ let program = self.current_program.get();
+ let program = match program {
+ Some(ref program) if program.id() == uniform.program_id() => program,
+ _ => {
+ self.webgl_error(InvalidOperation);
+ return false;
+ },
+ };
+
+ let data = match data {
+ Some(data) => data,
+ None => {
+ self.webgl_error(InvalidOperation);
+ return false;
+ },
+ };
+
+ // TODO(autrilla): Don't request this every time, cache it
+ let active_uniform = match program.get_active_uniform(
+ uniform.id() as u32) {
+ Ok(active_uniform) => active_uniform,
+ Err(_) => {
+ self.webgl_error(InvalidOperation);
+ return false;
+ },
+ };
+
+ if data.len() % type_.element_count() != 0 ||
+ (data.len() / type_.element_count() > active_uniform.Size() as usize) {
+ self.webgl_error(InvalidOperation);
+ return false;
+ }
+
+ if type_.as_gl_constant() != active_uniform.Type() {
+ self.webgl_error(InvalidOperation);
+ return false;
+ }
+
+ return true;
+ }
}
impl Drop for WebGLRenderingContext {
@@ -1018,38 +1111,22 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
fn Uniform1f(&self,
uniform: Option<&WebGLUniformLocation>,
val: f32) {
- let uniform = match uniform {
- Some(uniform) => uniform,
- None => return,
- };
-
- match self.current_program.get() {
- Some(ref program) if program.id() == uniform.program_id() => {},
- _ => return self.webgl_error(InvalidOperation),
- };
-
- self.ipc_renderer
- .send(CanvasMsg::WebGL(WebGLCommand::Uniform1f(uniform.id(), val)))
- .unwrap()
+ if self.validate_uniform_parameters(uniform, UniformType::Float, Some(&[val])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform1f(uniform.unwrap().id(), val)))
+ .unwrap()
+ }
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
fn Uniform1i(&self,
uniform: Option<&WebGLUniformLocation>,
val: i32) {
- let uniform = match uniform {
- Some(uniform) => uniform,
- None => return,
- };
-
- match self.current_program.get() {
- Some(ref program) if program.id() == uniform.program_id() => {},
- _ => return self.webgl_error(InvalidOperation),
- };
-
- self.ipc_renderer
- .send(CanvasMsg::WebGL(WebGLCommand::Uniform1i(uniform.id(), val)))
- .unwrap()
+ if self.validate_uniform_parameters(uniform, UniformType::Int, Some(&[val])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform1i(uniform.unwrap().id(), val)))
+ .unwrap()
+ }
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
@@ -1057,90 +1134,132 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
_cx: *mut JSContext,
uniform: Option<&WebGLUniformLocation>,
data: Option<*mut JSObject>) {
- let data = match data {
- Some(data) => data,
- None => return self.webgl_error(InvalidValue),
- };
-
- if let Some(data) = array_buffer_view_to_vec_checked::<i32>(data) {
- if data.len() < 1 {
- return self.webgl_error(InvalidOperation);
- }
-
- self.Uniform1i(uniform, data[0]);
- } else {
- self.webgl_error(InvalidValue);
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<i32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::Int, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform1iv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
}
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
fn Uniform1fv(&self,
+ _cx: *mut JSContext,
uniform: Option<&WebGLUniformLocation>,
- data: Vec<f32>) {
- if data.is_empty() {
- return self.webgl_error(InvalidValue);
+ data: Option<*mut JSObject>) {
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<f32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::Float, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform1fv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
}
-
- self.Uniform1f(uniform, data[0]);
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
fn Uniform2f(&self,
uniform: Option<&WebGLUniformLocation>,
x: f32, y: f32) {
- let uniform = match uniform {
- Some(uniform) => uniform,
- None => return,
- };
+ if self.validate_uniform_parameters(uniform, UniformType::FloatVec2, Some(&[x, y])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform2f(uniform.unwrap().id(), x, y)))
+ .unwrap()
+ }
+ }
- match self.current_program.get() {
- Some(ref program) if program.id() == uniform.program_id() => {},
- _ => return self.webgl_error(InvalidOperation),
- };
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ fn Uniform2fv(&self,
+ _cx: *mut JSContext,
+ uniform: Option<&WebGLUniformLocation>,
+ data: Option<*mut JSObject>) {
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<f32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::FloatVec2, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform2fv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
+ }
+ }
- self.ipc_renderer
- .send(CanvasMsg::WebGL(WebGLCommand::Uniform2f(uniform.id(), x, y)))
- .unwrap()
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ fn Uniform2i(&self,
+ uniform: Option<&WebGLUniformLocation>,
+ x: i32, y: i32) {
+ if self.validate_uniform_parameters(uniform, UniformType::IntVec2, Some(&[x, y])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform2i(uniform.unwrap().id(), x, y)))
+ .unwrap()
+ }
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
- fn Uniform2fv(&self,
+ fn Uniform2iv(&self,
_cx: *mut JSContext,
uniform: Option<&WebGLUniformLocation>,
data: Option<*mut JSObject>) {
- let data = match data {
- Some(data) => data,
- None => return self.webgl_error(InvalidValue),
- };
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<i32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::IntVec2, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform2iv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
+ }
+ }
- if let Some(data) = array_buffer_view_to_vec_checked::<f32>(data) {
- if data.len() < 2 {
- return self.webgl_error(InvalidOperation);
- }
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ fn Uniform3f(&self,
+ uniform: Option<&WebGLUniformLocation>,
+ x: f32, y: f32, z: f32) {
+ if self.validate_uniform_parameters(uniform, UniformType::FloatVec3, Some(&[x, y, z])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform3f(uniform.unwrap().id(), x, y, z)))
+ .unwrap()
+ }
+ }
- self.Uniform2f(uniform, data[0], data[1]);
- } else {
- self.webgl_error(InvalidValue);
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ fn Uniform3fv(&self,
+ _cx: *mut JSContext,
+ uniform: Option<&WebGLUniformLocation>,
+ data: Option<*mut JSObject>) {
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<f32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::FloatVec3, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform3fv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
}
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
- fn Uniform4i(&self,
+ fn Uniform3i(&self,
uniform: Option<&WebGLUniformLocation>,
- x: i32, y: i32, z: i32, w: i32) {
- let uniform = match uniform {
- Some(uniform) => uniform,
- None => return,
- };
+ x: i32, y: i32, z: i32) {
+ if self.validate_uniform_parameters(uniform, UniformType::IntVec3, Some(&[x, y, z])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform3i(uniform.unwrap().id(), x, y, z)))
+ .unwrap()
+ }
+ }
- match self.current_program.get() {
- Some(ref program) if program.id() == uniform.program_id() => {},
- _ => return self.webgl_error(InvalidOperation),
- };
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ fn Uniform3iv(&self,
+ _cx: *mut JSContext,
+ uniform: Option<&WebGLUniformLocation>,
+ data: Option<*mut JSObject>) {
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<i32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::IntVec3, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform3iv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
+ }
+ }
- self.ipc_renderer
- .send(CanvasMsg::WebGL(WebGLCommand::Uniform4i(uniform.id(), x, y, z, w)))
- .unwrap()
+ // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
+ fn Uniform4i(&self,
+ uniform: Option<&WebGLUniformLocation>,
+ x: i32, y: i32, z: i32, w: i32) {
+ if self.validate_uniform_parameters(uniform, UniformType::IntVec4, Some(&[x, y, z, w])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform4i(uniform.unwrap().id(), x, y, z, w)))
+ .unwrap()
+ }
}
@@ -1149,19 +1268,11 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
_cx: *mut JSContext,
uniform: Option<&WebGLUniformLocation>,
data: Option<*mut JSObject>) {
- let data = match data {
- Some(data) => data,
- None => return self.webgl_error(InvalidValue),
- };
-
- if let Some(data) = array_buffer_view_to_vec_checked::<i32>(data) {
- if data.len() < 4 {
- return self.webgl_error(InvalidOperation);
- }
-
- self.Uniform4i(uniform, data[0], data[1], data[2], data[3]);
- } else {
- self.webgl_error(InvalidValue);
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<i32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::IntVec4, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform4iv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
}
}
@@ -1169,19 +1280,11 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
fn Uniform4f(&self,
uniform: Option<&WebGLUniformLocation>,
x: f32, y: f32, z: f32, w: f32) {
- let uniform = match uniform {
- Some(uniform) => uniform,
- None => return,
- };
-
- match self.current_program.get() {
- Some(ref program) if program.id() == uniform.program_id() => {},
- _ => return self.webgl_error(InvalidOperation),
- };
-
- self.ipc_renderer
- .send(CanvasMsg::WebGL(WebGLCommand::Uniform4f(uniform.id(), x, y, z, w)))
- .unwrap()
+ if self.validate_uniform_parameters(uniform, UniformType::FloatVec4, Some(&[x, y, z, w])) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform4f(uniform.unwrap().id(), x, y, z, w)))
+ .unwrap()
+ }
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
@@ -1189,19 +1292,11 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
_cx: *mut JSContext,
uniform: Option<&WebGLUniformLocation>,
data: Option<*mut JSObject>) {
- let data = match data {
- Some(data) => data,
- None => return self.webgl_error(InvalidValue),
- };
-
- if let Some(data) = array_buffer_view_to_vec_checked::<f32>(data) {
- if data.len() < 4 {
- return self.webgl_error(InvalidOperation);
- }
-
- self.Uniform4f(uniform, data[0], data[1], data[2], data[3]);
- } else {
- self.webgl_error(InvalidValue);
+ let data_vec = data.and_then(|d| array_buffer_view_to_vec::<f32>(d));
+ if self.validate_uniform_parameters(uniform, UniformType::FloatVec4, data_vec.as_ref().map(Vec::as_slice)) {
+ self.ipc_renderer
+ .send(CanvasMsg::WebGL(WebGLCommand::Uniform4fv(uniform.unwrap().id(), data_vec.unwrap())))
+ .unwrap()
}
}
diff --git a/components/script/dom/webidls/WebGLRenderingContext.webidl b/components/script/dom/webidls/WebGLRenderingContext.webidl
index d2c93d9a3c5..6c36844f313 100644
--- a/components/script/dom/webidls/WebGLRenderingContext.webidl
+++ b/components/script/dom/webidls/WebGLRenderingContext.webidl
@@ -646,7 +646,8 @@ interface WebGLRenderingContextBase
void uniform1f(WebGLUniformLocation? location, GLfloat x);
//void uniform1fv(WebGLUniformLocation? location, Float32Array v);
- void uniform1fv(WebGLUniformLocation? location, sequence<GLfloat> v);
+ //void uniform1fv(WebGLUniformLocation? location, sequence<GLfloat> v);
+ void uniform1fv(WebGLUniformLocation? location, optional object v);
void uniform1i(WebGLUniformLocation? location, GLint x);
//void uniform1iv(WebGLUniformLocation? location, Int32Array v);
//void uniform1iv(WebGLUniformLocation? location, sequence<long> v);
@@ -656,14 +657,18 @@ interface WebGLRenderingContextBase
//void uniform2fv(WebGLUniformLocation? location, sequence<GLfloat> v);
void uniform2fv(WebGLUniformLocation? location, optional object v);
//void uniform2i(WebGLUniformLocation? location, GLint x, GLint y);
+ void uniform2i(WebGLUniformLocation? location, GLint x, GLint y);
//void uniform2iv(WebGLUniformLocation? location, Int32Array v);
//void uniform2iv(WebGLUniformLocation? location, sequence<long> v);
- //void uniform3f(WebGLUniformLocation? location, GLfloat x, GLfloat y, GLfloat z);
+ void uniform2iv(WebGLUniformLocation? location, optional object v);
+ void uniform3f(WebGLUniformLocation? location, GLfloat x, GLfloat y, GLfloat z);
+ void uniform3fv(WebGLUniformLocation? location, optional object v);
//void uniform3fv(WebGLUniformLocation? location, Float32Array v);
//void uniform3fv(WebGLUniformLocation? location, sequence<GLfloat> v);
- //void uniform3i(WebGLUniformLocation? location, GLint x, GLint y, GLint z);
+ void uniform3i(WebGLUniformLocation? location, GLint x, GLint y, GLint z);
//void uniform3iv(WebGLUniformLocation? location, Int32Array v);
//void uniform3iv(WebGLUniformLocation? location, sequence<long> v);
+ void uniform3iv(WebGLUniformLocation? location, optional object v);
void uniform4f(WebGLUniformLocation? location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
// FIXME(dmarcos) The function below is the original function in the webIdl:
//void uniform4fv(WebGLUniformLocation? location, Float32Array v);