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

Implement gl.getUniform() #21202

Merged
merged 3 commits into from
Jul 18, 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
72 changes: 36 additions & 36 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions components/canvas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ compositing = {path = "../compositing"}
cssparser = "0.24"
euclid = "0.18"
fnv = "1.0"
gleam = "0.5"
gleam = "0.6"
ipc-channel = "0.10"
log = "0.4"
num-traits = "0.1.32"
offscreen_gl_context = {version = "0.19", features = ["serde", "osmesa"]}
offscreen_gl_context = {version = "0.20", features = ["serde", "osmesa"]}
serde_bytes = "0.10"
servo_config = {path = "../config"}
webrender = {git = "https://github.com/servo/webrender"}
Expand Down
113 changes: 113 additions & 0 deletions components/canvas/webgl_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,119 @@ impl WebGLImpl {
WebGLCommand::VertexAttribDivisor { index, divisor } => {
ctx.gl().vertex_attrib_divisor(index, divisor)
}
WebGLCommand::GetUniformBool(program_id, loc, ref sender) => {
let mut value = [0];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
sender.send(value[0] != 0).unwrap();
}
WebGLCommand::GetUniformBool2(program_id, loc, ref sender) => {
let mut value = [0; 2];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
let value = [
value[0] != 0,
value[1] != 0,
];
sender.send(value).unwrap();
}
WebGLCommand::GetUniformBool3(program_id, loc, ref sender) => {
let mut value = [0; 3];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
let value = [
value[0] != 0,
value[1] != 0,
value[2] != 0,
];
sender.send(value).unwrap();
}
WebGLCommand::GetUniformBool4(program_id, loc, ref sender) => {
let mut value = [0; 4];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
let value = [
value[0] != 0,
value[1] != 0,
value[2] != 0,
value[3] != 0,
];
sender.send(value).unwrap();
}
WebGLCommand::GetUniformInt(program_id, loc, ref sender) => {
let mut value = [0];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
sender.send(value[0]).unwrap();
}
WebGLCommand::GetUniformInt2(program_id, loc, ref sender) => {
let mut value = [0; 2];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformInt3(program_id, loc, ref sender) => {
let mut value = [0; 3];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformInt4(program_id, loc, ref sender) => {
let mut value = [0; 4];
unsafe {
ctx.gl().get_uniform_iv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformFloat(program_id, loc, ref sender) => {
let mut value = [0.];
unsafe {
ctx.gl().get_uniform_fv(program_id.get(), loc, &mut value);
}
sender.send(value[0]).unwrap();
}
WebGLCommand::GetUniformFloat2(program_id, loc, ref sender) => {
let mut value = [0.; 2];
unsafe {
ctx.gl().get_uniform_fv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformFloat3(program_id, loc, ref sender) => {
let mut value = [0.; 3];
unsafe {
ctx.gl().get_uniform_fv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformFloat4(program_id, loc, ref sender) => {
let mut value = [0.; 4];
unsafe {
ctx.gl().get_uniform_fv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformFloat9(program_id, loc, ref sender) => {
let mut value = [0.; 9];
unsafe {
ctx.gl().get_uniform_fv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
WebGLCommand::GetUniformFloat16(program_id, loc, ref sender) => {
let mut value = [0.; 16];
unsafe {
ctx.gl().get_uniform_fv(program_id.get(), loc, &mut value);
}
sender.send(value).unwrap();
}
}

// TODO: update test expectations in order to enable debug assertions
Expand Down
4 changes: 2 additions & 2 deletions components/canvas_traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ path = "lib.rs"
cssparser = "0.24.0"
euclid = "0.18"
ipc-channel = "0.10"
gleam = "0.5.1"
gleam = "0.6"
lazy_static = "1"
malloc_size_of = { path = "../malloc_size_of" }
malloc_size_of_derive = { path = "../malloc_size_of_derive" }
offscreen_gl_context = {version = "0.19", features = ["serde"]}
offscreen_gl_context = {version = "0.20", features = ["serde"]}
serde = "1.0"
serde_bytes = "0.10"
servo_config = {path = "../config"}
Expand Down
14 changes: 14 additions & 0 deletions components/canvas_traits/webgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ pub enum WebGLCommand {
DrawArraysInstanced { mode: u32, first: i32, count: i32, primcount: i32 },
DrawElementsInstanced { mode: u32, count: i32, type_: u32, offset: u32, primcount: i32 },
VertexAttribDivisor { index: u32, divisor: u32 },
GetUniformBool(WebGLProgramId, i32, WebGLSender<bool>),
GetUniformBool2(WebGLProgramId, i32, WebGLSender<[bool; 2]>),
GetUniformBool3(WebGLProgramId, i32, WebGLSender<[bool; 3]>),
GetUniformBool4(WebGLProgramId, i32, WebGLSender<[bool; 4]>),
GetUniformInt(WebGLProgramId, i32, WebGLSender<i32>),
GetUniformInt2(WebGLProgramId, i32, WebGLSender<[i32; 2]>),
GetUniformInt3(WebGLProgramId, i32, WebGLSender<[i32; 3]>),
GetUniformInt4(WebGLProgramId, i32, WebGLSender<[i32; 4]>),
GetUniformFloat(WebGLProgramId, i32, WebGLSender<f32>),
GetUniformFloat2(WebGLProgramId, i32, WebGLSender<[f32; 2]>),
GetUniformFloat3(WebGLProgramId, i32, WebGLSender<[f32; 3]>),
GetUniformFloat4(WebGLProgramId, i32, WebGLSender<[f32; 4]>),
GetUniformFloat9(WebGLProgramId, i32, WebGLSender<[f32; 9]>),
GetUniformFloat16(WebGLProgramId, i32, WebGLSender<[f32; 16]>),
}

macro_rules! define_resource_id_struct {
Expand Down
2 changes: 1 addition & 1 deletion components/compositing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default = []
embedder_traits = {path = "../embedder_traits"}
euclid = "0.18"
gfx_traits = {path = "../gfx_traits"}
gleam = {version = "0.5", optional = true}
gleam = {version = "0.6", optional = true}
image = "0.19"
ipc-channel = "0.10"
libc = "0.2"
Expand Down
4 changes: 2 additions & 2 deletions components/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ encoding_rs = "0.7"
enum-iterator = "0.2.0"
euclid = "0.18"
fnv = "1.0"
gleam = "0.5"
gleam = "0.6"
half = "1.0"
html5ever = "0.22"
hyper = "0.10"
Expand All @@ -69,7 +69,7 @@ mozjs = { version = "0.7.1", features = ["promises"]}
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
num-traits = "0.1.32"
offscreen_gl_context = {version = "0.19", features = ["serde"]}
offscreen_gl_context = {version = "0.20", features = ["serde"]}
parking_lot = "0.5"
phf = "0.7.18"
profile_traits = {path = "../profile_traits"}
Expand Down
11 changes: 11 additions & 0 deletions components/script/dom/webgl2renderingcontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,17 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
self.base.UniformMatrix4fv(location, transpose, v)
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
#[allow(unsafe_code)]
unsafe fn GetUniform(
&self,
cx: *mut JSContext,
program: &WebGLProgram,
location: &WebGLUniformLocation,
) -> JSVal {
self.base.GetUniform(cx, program, location)
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
fn UseProgram(&self, program: Option<&WebGLProgram>) {
self.base.UseProgram(program)
Expand Down
82 changes: 79 additions & 3 deletions components/script/dom/webglrenderingcontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use canvas_traits::webgl::{ActiveAttribInfo, DOMToTextureCommand, Parameter};
use canvas_traits::webgl::{ShaderParameter, TexParameter, WebGLCommand};
use canvas_traits::webgl::{WebGLContextShareMode, WebGLError};
use canvas_traits::webgl::{WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender};
use canvas_traits::webgl::{WebGLResult, WebGLSLVersion, WebGLVersion};
use canvas_traits::webgl::{WebVRCommand, webgl_channel};
use canvas_traits::webgl::{WebGLProgramId, WebGLResult, WebGLSLVersion, WebGLSender};
use canvas_traits::webgl::{WebGLVersion, WebVRCommand, webgl_channel};
use canvas_traits::webgl::WebGLError::*;
use dom::bindings::cell::DomRefCell;
use dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::ANGLEInstancedArraysConstants;
Expand Down Expand Up @@ -56,12 +56,14 @@ use js::jsapi::{JSContext, JSObject, Type};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, UInt32Value, JSVal};
use js::jsval::{ObjectValue, NullValue, UndefinedValue};
use js::rust::CustomAutoRooterGuard;
use js::typedarray::{ArrayBufferView, CreateWith, Float32Array, Int32Array, Uint32Array};
use js::typedarray::{ArrayBufferView, CreateWith, Float32, Float32Array, Int32, Int32Array, Uint32Array};
use js::typedarray::{TypedArray, TypedArrayElementCreator};
use net_traits::image::base::PixelFormat;
use net_traits::image_cache::ImageResponse;
use offscreen_gl_context::{GLContextAttributes, GLLimits};
use ref_filter_map::ref_filter_map;
use script_layout_interface::HTMLCanvasDataSource;
use serde::{Deserialize, Serialize};
use servo_config::prefs::PREFS;
use std::cell::{Cell, Ref};
use std::cmp;
Expand Down Expand Up @@ -3600,6 +3602,80 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
});
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
#[allow(unsafe_code)]
unsafe fn GetUniform(
&self,
cx: *mut JSContext,
program: &WebGLProgram,
location: &WebGLUniformLocation,
) -> JSVal {
// FIXME(nox): https://github.com/servo/servo/issues/21133

if program.is_deleted() || !program.is_linked() || program.id() != location.program_id() {
self.webgl_error(InvalidOperation);
return NullValue();
}

fn get<T, F>(
triple: (&WebGLRenderingContext, WebGLProgramId, i32),
f: F,
) -> T
where
F: FnOnce(WebGLProgramId, i32, WebGLSender<T>) -> WebGLCommand,
T: for<'de> Deserialize<'de> + Serialize,
{
let (sender, receiver) = webgl_channel().unwrap();
triple.0.send_command(f(triple.1, triple.2, sender));
receiver.recv().unwrap()
}

let triple = (self, program.id(), location.id());

unsafe fn typed<T>(cx: *mut JSContext, value: &[T::Element]) -> JSVal
where
T: TypedArrayElementCreator,
{
rooted!(in(cx) let mut rval = ptr::null_mut::<JSObject>());
<TypedArray<T, *mut JSObject>>::create(cx, CreateWith::Slice(&value), rval.handle_mut()).unwrap();
ObjectValue(rval.get())
}

match location.type_() {
constants::BOOL => BooleanValue(get(triple, WebGLCommand::GetUniformBool)),
constants::BOOL_VEC2 => {
rooted!(in(cx) let mut rval = NullValue());
get(triple, WebGLCommand::GetUniformBool2).to_jsval(cx, rval.handle_mut());
rval.get()
}
constants::BOOL_VEC3 => {
rooted!(in(cx) let mut rval = NullValue());
get(triple, WebGLCommand::GetUniformBool3).to_jsval(cx, rval.handle_mut());
rval.get()
}
constants::BOOL_VEC4 => {
rooted!(in(cx) let mut rval = NullValue());
get(triple, WebGLCommand::GetUniformBool4).to_jsval(cx, rval.handle_mut());
rval.get()
}
constants::INT | constants::SAMPLER_2D | constants::SAMPLER_CUBE => {
Int32Value(get(triple, WebGLCommand::GetUniformInt))
}
constants::INT_VEC2 => typed::<Int32>(cx, &get(triple, WebGLCommand::GetUniformInt2)),
constants::INT_VEC3 => typed::<Int32>(cx, &get(triple, WebGLCommand::GetUniformInt3)),
constants::INT_VEC4 => typed::<Int32>(cx, &get(triple, WebGLCommand::GetUniformInt4)),
constants::FLOAT => DoubleValue(get(triple, WebGLCommand::GetUniformFloat) as f64),
constants::FLOAT_VEC2 => typed::<Float32>(cx, &get(triple, WebGLCommand::GetUniformFloat2)),
constants::FLOAT_VEC3 => typed::<Float32>(cx, &get(triple, WebGLCommand::GetUniformFloat3)),
constants::FLOAT_VEC4 | constants::FLOAT_MAT2 => {
typed::<Float32>(cx, &get(triple, WebGLCommand::GetUniformFloat4))
}
constants::FLOAT_MAT3 => typed::<Float32>(cx, &get(triple, WebGLCommand::GetUniformFloat9)),
constants::FLOAT_MAT4 => typed::<Float32>(cx, &get(triple, WebGLCommand::GetUniformFloat16)),
_ => panic!("wrong uniform type"),
}
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
fn UseProgram(&self, program: Option<&WebGLProgram>) {
if let Some(program) = program {
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/WebGLRenderingContext.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ interface WebGLRenderingContextBase

any getTexParameter(GLenum target, GLenum pname);

// any getUniform(WebGLProgram program, WebGLUniformLocation location);
any getUniform(WebGLProgram program, WebGLUniformLocation location);

WebGLUniformLocation? getUniformLocation(WebGLProgram program, DOMString name);

Expand Down
2 changes: 1 addition & 1 deletion components/servo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ embedder_traits = {path = "../embedder_traits"}
env_logger = "0.5"
euclid = "0.18"
gfx = {path = "../gfx"}
gleam = "0.5"
gleam = "0.6"
ipc-channel = "0.10"
layout_thread = {path = "../layout_thread"}
log = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion ports/servo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unstable = ["libservo/unstable"]
backtrace = "0.3"
bitflags = "1.0"
euclid = "0.18"
gleam = "0.5"
gleam = "0.6"
glutin = "0.17"
lazy_static = "1"
libservo = {path = "../../components/servo"}
Expand Down