webgl: Add shader validation and translation

This commit adds angle-based validation and translation to WebGL
shaders.

The changes to the tex_image_2d test is neccessary (it was not valid
GLES 2.0 shader language).
This commit is contained in:
ecoal95 2015-08-29 18:42:42 +02:00
parent 67cbda4be3
commit 167885707d
9 changed files with 88 additions and 28 deletions

View file

@ -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,

View file

@ -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),

View file

@ -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" ]

View file

@ -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);
}
}

View file

@ -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;

View file

@ -56,6 +56,14 @@ name = "android_glue"
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"
@ -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",

9
ports/cef/Cargo.lock generated
View file

@ -55,6 +55,14 @@ name = "android_glue"
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"
@ -1327,6 +1335,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",

9
ports/gonk/Cargo.lock generated
View file

@ -41,6 +41,14 @@ dependencies = [
"memchr 0.1.3 (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"
@ -1193,6 +1201,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",

View file

@ -10,6 +10,7 @@
</style>
<canvas id="c" width="256" height="256"></canvas>
<script id="vertex_shader" type="x-shader/x-vertex">
precision mediump float;
attribute vec2 a_texCoord;
attribute vec2 a_position;
varying vec2 v_texCoord;
@ -21,6 +22,7 @@
</script>
<script id="fragment_shader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_image;
varying vec2 v_texCoord;
void main() {