Skip to content

Commit

Permalink
Implement gl.getUniform()
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Jul 18, 2018
1 parent 20714bd commit 3e8c2d6
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 200 deletions.
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
[WebGL test #19: getError expected: INVALID_OPERATION. Was NO_ERROR : after evaluating: contextA.getShaderSource(shaderB)]
expected: FAIL

[WebGL test #20: contextA.getUniform(programB, locationA) threw exception TypeError: contextA.getUniform is not a function]
expected: FAIL

[WebGL test #21: getError expected: INVALID_OPERATION. Was NO_ERROR : after evaluating: contextA.getUniformLocation(programB, 'u_modelViewProjMatrix')]
expected: FAIL

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
[methods.html]
[WebGL test #0: Property either does not exist or is not a function: getUniform]
expected: FAIL

[WebGL test #1: Property either does not exist or is not a function: isContextLost]
[WebGL test #0: Property either does not exist or is not a function: isContextLost]
expected: FAIL

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 3e8c2d6

Please sign in to comment.