aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorbors-servo <metajack+bors@gmail.com>2015-08-30 09:29:42 -0600
committerbors-servo <metajack+bors@gmail.com>2015-08-30 09:29:42 -0600
commit79328c11e320ad10a19d64d280df2e292eaf2e03 (patch)
treebff9e3737d0a851165c301a418a3386a7b7df0b1 /components
parent940bcadc134d1cf123518bebd84635b0053d6858 (diff)
parent167885707dfc7b050ac1fd1b6aee670a067515a4 (diff)
downloadservo-79328c11e320ad10a19d64d280df2e292eaf2e03.tar.gz
servo-79328c11e320ad10a19d64d280df2e292eaf2e03.zip
Auto merge of #7442 - ecoal95:shader-validation, r=jdm
Add WebGL shader validation and translation r? @jdm <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7442) <!-- Reviewable:end -->
Diffstat (limited to 'components')
-rw-r--r--components/canvas/webgl_paint_task.rs21
-rw-r--r--components/canvas_traits/lib.rs4
-rw-r--r--components/script/Cargo.toml4
-rw-r--r--components/script/dom/webglshader.rs57
-rw-r--r--components/script/lib.rs1
-rw-r--r--components/servo/Cargo.lock9
6 files changed, 68 insertions, 28 deletions
diff --git a/components/canvas/webgl_paint_task.rs b/components/canvas/webgl_paint_task.rs
index b434f468711..aad45c916aa 100644
--- a/components/canvas/webgl_paint_task.rs
+++ b/components/canvas/webgl_paint_task.rs
@@ -112,14 +112,12 @@ impl WebGLPaintTask {
gl::enable_vertex_attrib_array(attrib_id),
CanvasWebGLMsg::GetAttribLocation(program_id, name, chan) =>
self.get_attrib_location(program_id, name, chan),
- CanvasWebGLMsg::GetShaderInfoLog(shader_id, chan) =>
- self.get_shader_info_log(shader_id, chan),
CanvasWebGLMsg::GetShaderParameter(shader_id, param_id, chan) =>
self.get_shader_parameter(shader_id, param_id, chan),
CanvasWebGLMsg::GetUniformLocation(program_id, name, chan) =>
self.get_uniform_location(program_id, name, chan),
- CanvasWebGLMsg::CompileShader(shader_id) =>
- self.compile_shader(shader_id),
+ CanvasWebGLMsg::CompileShader(shader_id, source) =>
+ self.compile_shader(shader_id, source),
CanvasWebGLMsg::CreateBuffer(chan) =>
self.create_buffer(chan),
CanvasWebGLMsg::CreateFramebuffer(chan) =>
@@ -154,8 +152,6 @@ impl WebGLPaintTask {
gl::bind_texture(target, id),
CanvasWebGLMsg::LinkProgram(program_id) =>
gl::link_program(program_id),
- CanvasWebGLMsg::ShaderSource(shader_id, source) =>
- gl::shader_source(shader_id, &[source.as_bytes()]),
CanvasWebGLMsg::Uniform4fv(uniform_id, data) =>
gl::uniform_4f(uniform_id, data[0], data[1], data[2], data[3]),
CanvasWebGLMsg::UseProgram(program_id) =>
@@ -303,11 +299,9 @@ impl WebGLPaintTask {
gl::bind_framebuffer(target, id);
}
- // TODO(ecoal95): This is not spec-compliant, we must check
- // the version of GLSL used. This functionality should probably
- // be in the WebGLShader object
#[inline]
- fn compile_shader(&self, shader_id: u32) {
+ fn compile_shader(&self, shader_id: u32, source: String) {
+ gl::shader_source(shader_id, &[source.as_bytes()]);
gl::compile_shader(shader_id);
}
@@ -323,13 +317,6 @@ impl WebGLPaintTask {
chan.send(attrib_location).unwrap();
}
- fn get_shader_info_log(&self, shader_id: u32, chan: IpcSender<Option<String>>) {
- // TODO(ecoal95): Right now we always return a value, we should
- // check for gl errors and return None there
- let info = gl::get_shader_info_log(shader_id);
- chan.send(Some(info)).unwrap();
- }
-
fn get_shader_parameter(&self,
shader_id: u32,
param_id: u32,
diff --git a/components/canvas_traits/lib.rs b/components/canvas_traits/lib.rs
index 068eada3d9d..6f2924d2171 100644
--- a/components/canvas_traits/lib.rs
+++ b/components/canvas_traits/lib.rs
@@ -138,7 +138,7 @@ pub enum CanvasWebGLMsg {
DepthRange(f64, f64),
Enable(u32),
Disable(u32),
- CompileShader(u32),
+ CompileShader(u32, String),
CreateBuffer(IpcSender<Option<NonZero<u32>>>),
CreateFramebuffer(IpcSender<Option<NonZero<u32>>>),
CreateRenderbuffer(IpcSender<Option<NonZero<u32>>>),
@@ -157,7 +157,6 @@ pub enum CanvasWebGLMsg {
BindTexture(u32, u32),
DrawArrays(u32, i32, i32),
EnableVertexAttribArray(u32),
- GetShaderInfoLog(u32, IpcSender<Option<String>>),
GetShaderParameter(u32, u32, IpcSender<WebGLShaderParameter>),
GetAttribLocation(u32, String, IpcSender<Option<i32>>),
GetUniformLocation(u32, String, IpcSender<Option<i32>>),
@@ -166,7 +165,6 @@ pub enum CanvasWebGLMsg {
LineWidth(f32),
PixelStorei(u32, i32),
LinkProgram(u32),
- ShaderSource(u32, String),
Uniform4fv(i32, Vec<f32>),
UseProgram(u32),
VertexAttribPointer2f(u32, i32, bool, i32, u32),
diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml
index e52594d8c15..aa8967a62ae 100644
--- a/components/script/Cargo.toml
+++ b/components/script/Cargo.toml
@@ -55,6 +55,10 @@ features = ["query_encoding", "serde_serialization"]
[dependencies.offscreen_gl_context]
git = "https://github.com/ecoal95/rust-offscreen-rendering-context"
+[dependencies.angle]
+git = "https://github.com/ecoal95/angle"
+branch = "servo"
+
[dependencies.cssparser]
version = "0.3"
features = [ "serde-serialization" ]
diff --git a/components/script/dom/webglshader.rs b/components/script/dom/webglshader.rs
index 3920bcb7b7c..bcad061853f 100644
--- a/components/script/dom/webglshader.rs
+++ b/components/script/dom/webglshader.rs
@@ -11,10 +11,19 @@ use dom::webglobject::WebGLObject;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
+use angle::hl::{BuiltInResources, Output, ShaderValidator};
use canvas_traits::{CanvasMsg, CanvasWebGLMsg, WebGLResult, WebGLError, WebGLShaderParameter};
use ipc_channel::ipc::{self, IpcSender};
use std::cell::Cell;
use std::cell::RefCell;
+use std::sync::{Once, ONCE_INIT};
+
+#[derive(Clone, Copy, PartialEq, Debug, JSTraceable, HeapSizeOf)]
+pub enum ShaderCompilationStatus {
+ NotCompiled,
+ Succeeded,
+ Failed,
+}
#[dom_struct]
pub struct WebGLShader {
@@ -22,20 +31,32 @@ pub struct WebGLShader {
id: u32,
gl_type: u32,
source: RefCell<Option<String>>,
+ info_log: RefCell<Option<String>>,
is_deleted: Cell<bool>,
- // TODO(ecoal95): Evaluate moving this to `WebGLObject`
+ compilation_status: Cell<ShaderCompilationStatus>,
#[ignore_heap_size_of = "Defined in ipc-channel"]
renderer: IpcSender<CanvasMsg>,
}
+#[cfg(not(target_os = "android"))]
+const SHADER_OUTPUT_FORMAT: Output = Output::Glsl;
+
+#[cfg(target_os = "android")]
+const SHADER_OUTPUT_FORMAT: Output = Output::Essl;
+
+static GLSLANG_INITIALIZATION: Once = ONCE_INIT;
+
impl WebGLShader {
fn new_inherited(renderer: IpcSender<CanvasMsg>, id: u32, shader_type: u32) -> WebGLShader {
+ GLSLANG_INITIALIZATION.call_once(|| ::angle::hl::initialize().unwrap());
WebGLShader {
webgl_object: WebGLObject::new_inherited(),
id: id,
gl_type: shader_type,
source: RefCell::new(None),
+ info_log: RefCell::new(None),
is_deleted: Cell::new(false),
+ compilation_status: Cell::new(ShaderCompilationStatus::NotCompiled),
renderer: renderer,
}
}
@@ -69,10 +90,33 @@ impl WebGLShader {
self.gl_type
}
- // TODO(ecoal95): Validate shaders to be conforming to the WebGL spec
/// glCompileShader
pub fn compile(&self) {
- self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::CompileShader(self.id))).unwrap()
+ if self.compilation_status.get() != ShaderCompilationStatus::NotCompiled {
+ debug!("Compiling already compiled shader {}", self.id);
+ }
+
+ if let Some(ref source) = *self.source.borrow() {
+ let validator = ShaderValidator::for_webgl(self.gl_type,
+ SHADER_OUTPUT_FORMAT,
+ &BuiltInResources::default()).unwrap();
+ match validator.compile_and_translate(&[source.as_bytes()]) {
+ Ok(translated_source) => {
+ // NOTE: At this point we should be pretty sure that the compilation in the paint task
+ // will succeed.
+ // It could be interesting to retrieve the info log from the paint task though
+ let msg = CanvasWebGLMsg::CompileShader(self.id, translated_source);
+ self.renderer.send(CanvasMsg::WebGL(msg)).unwrap();
+ self.compilation_status.set(ShaderCompilationStatus::Succeeded);
+ },
+ Err(error) => {
+ self.compilation_status.set(ShaderCompilationStatus::Failed);
+ debug!("Shader {} compilation failed: {}", self.id, error);
+ },
+ }
+
+ *self.info_log.borrow_mut() = Some(validator.info_log());
+ }
}
/// Mark this shader as deleted (if it wasn't previously)
@@ -86,9 +130,7 @@ impl WebGLShader {
/// glGetShaderInfoLog
pub fn info_log(&self) -> Option<String> {
- let (sender, receiver) = ipc::channel().unwrap();
- self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::GetShaderInfoLog(self.id, sender))).unwrap();
- receiver.recv().unwrap()
+ self.info_log.borrow().clone()
}
/// glGetShaderParameter
@@ -110,7 +152,6 @@ impl WebGLShader {
/// glShaderSource
pub fn set_source(&self, source: String) {
- *self.source.borrow_mut() = Some(source.clone());
- self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::ShaderSource(self.id, source))).unwrap()
+ *self.source.borrow_mut() = Some(source);
}
}
diff --git a/components/script/lib.rs b/components/script/lib.rs
index bfe02d56cdc..651af1a7f60 100644
--- a/components/script/lib.rs
+++ b/components/script/lib.rs
@@ -78,6 +78,7 @@ extern crate url;
extern crate uuid;
extern crate string_cache;
extern crate offscreen_gl_context;
+extern crate angle;
extern crate tendril;
pub mod cors;
diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock
index 3e880ad7a2e..81063198cce 100644
--- a/components/servo/Cargo.lock
+++ b/components/servo/Cargo.lock
@@ -57,6 +57,14 @@ version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "angle"
+version = "0.1.0"
+source = "git+https://github.com/ecoal95/angle?branch=servo#77288884bd7a89bf3019ca94dcda6c94cb178aa4"
+dependencies = [
+ "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "aster"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1349,6 +1357,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "script"
version = "0.0.1"
dependencies = [
+ "angle 0.1.0 (git+https://github.com/ecoal95/angle?branch=servo)",
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"canvas 0.0.1",
"canvas_traits 0.0.1",