Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add WebGLSampler support
  • Loading branch information
mmatyas committed Oct 8, 2019
1 parent 7843811 commit 26df196
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 87 deletions.
24 changes: 24 additions & 0 deletions components/canvas/webgl_thread.rs
Expand Up @@ -1637,6 +1637,30 @@ impl WebGLImpl {
let value = ctx.gl().get_query_object_uiv(query_id.get(), pname);
sender.send(value).unwrap()
},
WebGLCommand::GenerateSampler(ref sender) => {
let id = ctx.gl().gen_samplers(1)[0];
sender.send(unsafe { WebGLSamplerId::new(id) }).unwrap()
},
WebGLCommand::DeleteSampler(sampler_id) => {
ctx.gl().delete_samplers(&[sampler_id.get()]);
},
WebGLCommand::BindSampler(unit, sampler_id) => {
ctx.gl().bind_sampler(unit, sampler_id.get());
},
WebGLCommand::SetSamplerParameterInt(sampler_id, pname, value) => {
ctx.gl().sampler_parameter_i(sampler_id.get(), pname, value);
},
WebGLCommand::SetSamplerParameterFloat(sampler_id, pname, value) => {
ctx.gl().sampler_parameter_f(sampler_id.get(), pname, value);
},
WebGLCommand::GetSamplerParameterInt(sampler_id, pname, ref sender) => {
let value = ctx.gl().get_sampler_parameter_iv(sampler_id.get(), pname)[0];
sender.send(value).unwrap();
},
WebGLCommand::GetSamplerParameterFloat(sampler_id, pname, ref sender) => {
let value = ctx.gl().get_sampler_parameter_fv(sampler_id.get(), pname)[0];
sender.send(value).unwrap();
},
}

// TODO: update test expectations in order to enable debug assertions
Expand Down
8 changes: 8 additions & 0 deletions components/canvas_traits/webgl.rs
Expand Up @@ -438,6 +438,13 @@ pub enum WebGLCommand {
EndQuery(u32),
GenerateQuery(WebGLSender<WebGLQueryId>),
GetQueryState(WebGLSender<u32>, WebGLQueryId, u32),
GenerateSampler(WebGLSender<WebGLSamplerId>),
DeleteSampler(WebGLSamplerId),
BindSampler(u32, WebGLSamplerId),
SetSamplerParameterFloat(WebGLSamplerId, u32, f32),
SetSamplerParameterInt(WebGLSamplerId, u32, i32),
GetSamplerParameterFloat(WebGLSamplerId, u32, WebGLSender<f32>),
GetSamplerParameterInt(WebGLSamplerId, u32, WebGLSender<i32>),
}

macro_rules! nonzero_type {
Expand Down Expand Up @@ -519,6 +526,7 @@ define_resource_id!(WebGLRenderbufferId, u32);
define_resource_id!(WebGLTextureId, u32);
define_resource_id!(WebGLProgramId, u32);
define_resource_id!(WebGLQueryId, u32);
define_resource_id!(WebGLSamplerId, u32);
define_resource_id!(WebGLShaderId, u32);
define_resource_id!(WebGLSyncId, u64);
define_resource_id!(WebGLVertexArrayId, u32);
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/bindings/trace.rs
Expand Up @@ -47,7 +47,7 @@ use canvas_traits::canvas::{
use canvas_traits::canvas::{CompositionOrBlending, LineCapStyle, LineJoinStyle, RepetitionStyle};
use canvas_traits::webgl::WebGLVertexArrayId;
use canvas_traits::webgl::{ActiveAttribInfo, ActiveUniformInfo, GlType, TexDataType, TexFormat};
use canvas_traits::webgl::{GLFormats, GLLimits, WebGLQueryId};
use canvas_traits::webgl::{GLFormats, GLLimits, WebGLQueryId, WebGLSamplerId};
use canvas_traits::webgl::{WebGLBufferId, WebGLChan, WebGLContextShareMode, WebGLError};
use canvas_traits::webgl::{WebGLFramebufferId, WebGLMsgSender, WebGLPipeline, WebGLProgramId};
use canvas_traits::webgl::{WebGLReceiver, WebGLRenderbufferId, WebGLSLVersion, WebGLSender};
Expand Down Expand Up @@ -480,6 +480,7 @@ unsafe_no_jsmanaged_fields!(WebGLPipeline);
unsafe_no_jsmanaged_fields!(WebGLProgramId);
unsafe_no_jsmanaged_fields!(WebGLQueryId);
unsafe_no_jsmanaged_fields!(WebGLRenderbufferId);
unsafe_no_jsmanaged_fields!(WebGLSamplerId);
unsafe_no_jsmanaged_fields!(WebGLShaderId);
unsafe_no_jsmanaged_fields!(WebGLSyncId);
unsafe_no_jsmanaged_fields!(WebGLTextureId);
Expand Down
11 changes: 11 additions & 0 deletions components/script/dom/macros.rs
Expand Up @@ -660,3 +660,14 @@ macro_rules! impl_rare_data (
}
);
);

#[macro_export]
macro_rules! optional_root_object_to_js_or_null {
($cx: expr, $binding:expr) => {{
rooted!(in($cx) let mut rval = NullValue());
if let Some(object) = $binding {
object.to_jsval($cx, rval.handle_mut());
}
rval.get()
}};
}
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -525,6 +525,7 @@ pub mod webglprogram;
pub mod webglquery;
pub mod webglrenderbuffer;
pub mod webglrenderingcontext;
pub mod webglsampler;
pub mod webglshader;
pub mod webglshaderprecisionformat;
pub mod webglsync;
Expand Down
101 changes: 100 additions & 1 deletion components/script/dom/webgl2renderingcontext.rs
Expand Up @@ -11,6 +11,7 @@ use crate::dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer;
use crate::dom::bindings::codegen::UnionTypes::Float32ArrayOrUnrestrictedFloatSequence;
use crate::dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement;
use crate::dom::bindings::codegen::UnionTypes::Int32ArrayOrLongSequence;
use crate::dom::bindings::conversions::ToJSValConvertible;
use crate::dom::bindings::error::{ErrorResult, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom};
Expand All @@ -26,6 +27,7 @@ use crate::dom::webglrenderbuffer::WebGLRenderbuffer;
use crate::dom::webglrenderingcontext::{
LayoutCanvasWebGLRenderingContextHelpers, WebGLRenderingContext,
};
use crate::dom::webglsampler::{WebGLSampler, WebGLSamplerValue};
use crate::dom::webglshader::WebGLShader;
use crate::dom::webglshaderprecisionformat::WebGLShaderPrecisionFormat;
use crate::dom::webglsync::WebGLSync;
Expand All @@ -39,7 +41,7 @@ use canvas_traits::webgl::{webgl_channel, GLContextAttributes, WebGLCommand, Web
use dom_struct::dom_struct;
use euclid::default::Size2D;
use js::jsapi::JSObject;
use js::jsval::{BooleanValue, Int32Value, JSVal, NullValue, UInt32Value};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value};
use js::rust::CustomAutoRooterGuard;
use js::typedarray::ArrayBufferView;
use script_layout_interface::HTMLCanvasDataSource;
Expand All @@ -51,6 +53,7 @@ pub struct WebGL2RenderingContext {
base: Dom<WebGLRenderingContext>,
occlusion_query: MutNullableDom<WebGLQuery>,
primitives_query: MutNullableDom<WebGLQuery>,
samplers: Box<[MutNullableDom<WebGLSampler>]>,
}

impl WebGL2RenderingContext {
Expand All @@ -61,11 +64,18 @@ impl WebGL2RenderingContext {
attrs: GLContextAttributes,
) -> Option<WebGL2RenderingContext> {
let base = WebGLRenderingContext::new(window, canvas, WebGLVersion::WebGL2, size, attrs)?;

let samplers = (0..base.limits().max_combined_texture_image_units)
.map(|_| Default::default())
.collect::<Vec<_>>()
.into();

Some(WebGL2RenderingContext {
reflector_: Reflector::new(),
base: Dom::from_ref(&*base),
occlusion_query: MutNullableDom::new(None),
primitives_query: MutNullableDom::new(None),
samplers: samplers,
})
}

Expand Down Expand Up @@ -130,6 +140,12 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
constants::MAX_CLIENT_WAIT_TIMEOUT_WEBGL => {
Int32Value(self.base.limits().max_client_wait_timeout_webgl.as_nanos() as i32)
},
constants::SAMPLER_BINDING => unsafe {
let idx = (self.base.textures().active_unit_enum() - constants::TEXTURE0) as usize;
assert!(idx < self.samplers.len());
let sampler = self.samplers[idx].get();
optional_root_object_to_js_or_null!(*cx, sampler)
},
_ => self.base.GetParameter(cx, parameter),
}
}
Expand Down Expand Up @@ -1099,6 +1115,32 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn CreateSampler(&self) -> Option<DomRoot<WebGLSampler>> {
Some(WebGLSampler::new(&self.base))
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn DeleteSampler(&self, sampler: Option<&WebGLSampler>) {
if let Some(sampler) = sampler {
handle_potential_webgl_error!(self.base, self.base.validate_ownership(sampler), return);
for slot in self.samplers.iter() {
if slot.get().map_or(false, |s| sampler == &*s) {
slot.set(None);
}
}
sampler.delete(false);
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn IsSampler(&self, sampler: Option<&WebGLSampler>) -> bool {
match sampler {
Some(sampler) => self.base.validate_ownership(sampler).is_ok() && sampler.is_valid(),
None => false,
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.12
#[cfg_attr(rustfmt, rustfmt_skip)]
fn BeginQuery(&self, target: u32, query: &WebGLQuery) {
Expand Down Expand Up @@ -1321,6 +1363,63 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
sync.delete(false);
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn BindSampler(&self, unit: u32, sampler: Option<&WebGLSampler>) {
if let Some(sampler) = sampler {
handle_potential_webgl_error!(self.base, self.base.validate_ownership(sampler), return);

if unit as usize >= self.samplers.len() {
self.base.webgl_error(InvalidValue);
return;
}

let result = sampler.bind(&self.base, unit);
match result {
Ok(_) => self.samplers[unit as usize].set(Some(sampler)),
Err(error) => self.base.webgl_error(error),
}
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn SamplerParameteri(&self, sampler: &WebGLSampler, pname: u32, param: i32) {
handle_potential_webgl_error!(self.base, self.base.validate_ownership(sampler), return);
let param = WebGLSamplerValue::GLenum(param as u32);
let result = sampler.set_parameter(&self.base, pname, param);
if let Err(error) = result {
self.base.webgl_error(error);
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn SamplerParameterf(&self, sampler: &WebGLSampler, pname: u32, param: f32) {
handle_potential_webgl_error!(self.base, self.base.validate_ownership(sampler), return);
let param = WebGLSamplerValue::Float(param);
let result = sampler.set_parameter(&self.base, pname, param);
if let Err(error) = result {
self.base.webgl_error(error);
}
}

/// https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7.13
fn GetSamplerParameter(&self, _cx: JSContext, sampler: &WebGLSampler, pname: u32) -> JSVal {
handle_potential_webgl_error!(
self.base,
self.base.validate_ownership(sampler),
return NullValue()
);
match sampler.get_parameter(&self.base, pname) {
Ok(value) => match value {
WebGLSamplerValue::GLenum(value) => UInt32Value(value),
WebGLSamplerValue::Float(value) => DoubleValue(value as f64),
},
Err(error) => {
self.base.webgl_error(error);
NullValue()
},
}
}
}

impl LayoutCanvasWebGLRenderingContextHelpers for LayoutDom<WebGL2RenderingContext> {
Expand Down
12 changes: 1 addition & 11 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -103,16 +103,6 @@ macro_rules! handle_object_deletion {
};
}

macro_rules! optional_root_object_to_js_or_null {
($cx: expr, $binding:expr) => {{
rooted!(in($cx) let mut rval = NullValue());
if let Some(object) = $binding {
object.to_jsval($cx, rval.handle_mut());
}
rval.get()
}};
}

fn has_invalid_blend_constants(arg1: u32, arg2: u32) -> bool {
match (arg1, arg2) {
(constants::CONSTANT_COLOR, constants::CONSTANT_ALPHA) => true,
Expand Down Expand Up @@ -4259,7 +4249,7 @@ impl Textures {
}
}

fn active_unit_enum(&self) -> u32 {
pub fn active_unit_enum(&self) -> u32 {
self.active_unit.get() + constants::TEXTURE0
}

Expand Down

0 comments on commit 26df196

Please sign in to comment.