Skip to content

Commit

Permalink
Add support for filtering WebGL extensions based on WebGL version
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Oct 31, 2017
1 parent d21657a commit f2f5817
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 25 deletions.
2 changes: 1 addition & 1 deletion components/canvas_traits/webgl.rs
Expand Up @@ -74,7 +74,7 @@ pub enum WebGLContextShareMode {
}

/// Defines the WebGL version
#[derive(Clone, Copy, Deserialize, MallocSizeOf, Serialize)]
#[derive(Clone, Copy, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
pub enum WebGLVersion {
/// https://www.khronos.org/registry/webgl/specs/1.0.2/
/// Conforms closely to the OpenGL ES 2.0 API
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webgl_extensions/ext/mod.rs
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use super::{ext_constants, WebGLExtension, WebGLExtensions};
use super::{ext_constants, WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

pub mod oesstandardderivatives;
pub mod oestexturefloat;
Expand Down
Expand Up @@ -2,13 +2,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use canvas_traits::webgl::WebGLVersion;
use dom::bindings::codegen::Bindings::OESStandardDerivativesBinding;
use dom::bindings::codegen::Bindings::OESStandardDerivativesBinding::OESStandardDerivativesConstants;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::root::DomRoot;
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom_struct::dom_struct;
use super::{WebGLExtension, WebGLExtensions};
use super::{WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

#[dom_struct]
pub struct OESStandardDerivatives {
Expand All @@ -31,6 +32,10 @@ impl WebGLExtension for OESStandardDerivatives {
OESStandardDerivativesBinding::Wrap)
}

fn spec() -> WebGLExtensionSpec {
WebGLExtensionSpec::Specific(WebGLVersion::WebGL1)
}

fn is_supported(ext: &WebGLExtensions) -> bool {
if cfg!(any(target_os = "android", target_os = "ios")) {
return ext.supports_any_gl_extension(&["GL_OES_standard_derivatives"]);
Expand Down
Expand Up @@ -2,12 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use canvas_traits::webgl::WebGLVersion;
use dom::bindings::codegen::Bindings::OESTextureFloatBinding;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::root::DomRoot;
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom_struct::dom_struct;
use super::{constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensions};
use super::{constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

#[dom_struct]
pub struct OESTextureFloat {
Expand All @@ -30,6 +31,10 @@ impl WebGLExtension for OESTextureFloat {
OESTextureFloatBinding::Wrap)
}

fn spec() -> WebGLExtensionSpec {
WebGLExtensionSpec::Specific(WebGLVersion::WebGL1)
}

fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_float",
"GL_ARB_texture_float",
Expand Down
Expand Up @@ -7,7 +7,7 @@ use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::root::DomRoot;
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom_struct::dom_struct;
use super::{constants as webgl, WebGLExtension, WebGLExtensions};
use super::{constants as webgl, WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

#[dom_struct]
pub struct OESTextureFloatLinear {
Expand All @@ -30,6 +30,10 @@ impl WebGLExtension for OESTextureFloatLinear {
OESTextureFloatLinearBinding::Wrap)
}

fn spec() -> WebGLExtensionSpec {
WebGLExtensionSpec::All
}

fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_float_linear",
"GL_ARB_texture_float"])
Expand Down
Expand Up @@ -2,12 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use canvas_traits::webgl::WebGLVersion;
use dom::bindings::codegen::Bindings::OESTextureHalfFloatBinding::{self, OESTextureHalfFloatConstants};
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::root::DomRoot;
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom_struct::dom_struct;
use super::{constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensions};
use super::{constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

#[dom_struct]
pub struct OESTextureHalfFloat {
Expand All @@ -30,6 +31,10 @@ impl WebGLExtension for OESTextureHalfFloat {
OESTextureHalfFloatBinding::Wrap)
}

fn spec() -> WebGLExtensionSpec {
WebGLExtensionSpec::Specific(WebGLVersion::WebGL1)
}

fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_half_float",
"GL_ARB_half_float_pixel",
Expand Down
Expand Up @@ -8,7 +8,7 @@ use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::root::DomRoot;
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom_struct::dom_struct;
use super::{WebGLExtension, WebGLExtensions};
use super::{WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

#[dom_struct]
pub struct OESTextureHalfFloatLinear {
Expand All @@ -31,6 +31,10 @@ impl WebGLExtension for OESTextureHalfFloatLinear {
OESTextureHalfFloatLinearBinding::Wrap)
}

fn spec() -> WebGLExtensionSpec {
WebGLExtensionSpec::All
}

fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_float_linear",
"GL_ARB_half_float_pixel",
Expand Down
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use canvas_traits::webgl::{webgl_channel, WebGLCommand, WebGLError};
use canvas_traits::webgl::{webgl_channel, WebGLCommand, WebGLError, WebGLVersion};
use dom::bindings::codegen::Bindings::OESVertexArrayObjectBinding::{self, OESVertexArrayObjectMethods};
use dom::bindings::codegen::Bindings::OESVertexArrayObjectBinding::OESVertexArrayObjectConstants;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
Expand All @@ -14,7 +14,7 @@ use js::conversions::ToJSValConvertible;
use js::jsapi::JSContext;
use js::jsval::{JSVal, NullValue};
use std::iter;
use super::{WebGLExtension, WebGLExtensions};
use super::{WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

#[dom_struct]
pub struct OESVertexArrayObject {
Expand Down Expand Up @@ -138,6 +138,10 @@ impl WebGLExtension for OESVertexArrayObject {
OESVertexArrayObjectBinding::Wrap)
}

fn spec() -> WebGLExtensionSpec {
WebGLExtensionSpec::Specific(WebGLVersion::WebGL1)
}

fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_vertex_array_object",
"GL_ARB_vertex_array_object",
Expand Down
11 changes: 11 additions & 0 deletions components/script/dom/webgl_extensions/extension.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use canvas_traits::webgl::WebGLVersion;
use dom::bindings::reflector::DomObject;
use dom::bindings::root::DomRoot;
use dom::bindings::trace::JSTraceable;
Expand All @@ -15,6 +16,9 @@ pub trait WebGLExtension: Sized where Self::Extension: DomObject + JSTraceable {
/// Creates the DOM object of the WebGL extension.
fn new(ctx: &WebGLRenderingContext) -> DomRoot<Self::Extension>;

/// Returns which WebGL spec is this extension written against.
fn spec() -> WebGLExtensionSpec;

/// Checks if the extension is supported.
fn is_supported(ext: &WebGLExtensions) -> bool;

Expand All @@ -24,3 +28,10 @@ pub trait WebGLExtension: Sized where Self::Extension: DomObject + JSTraceable {
/// Name of the WebGL Extension.
fn name() -> &'static str;
}

pub enum WebGLExtensionSpec {
/// Extensions written against both WebGL and WebGL2 specs.
All,
/// Extensions writen against a specific WebGL version spec.
Specific(WebGLVersion)
}
46 changes: 32 additions & 14 deletions components/script/dom/webgl_extensions/extensions.rs
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use canvas_traits::webgl::WebGLError;
use canvas_traits::webgl::{WebGLError, WebGLVersion};
use dom::bindings::cell::DomRefCell;
use dom::bindings::codegen::Bindings::OESStandardDerivativesBinding::OESStandardDerivativesConstants;
use dom::bindings::codegen::Bindings::OESTextureHalfFloatBinding::OESTextureHalfFloatConstants;
Expand All @@ -20,13 +20,13 @@ use ref_filter_map::ref_filter_map;
use std::cell::Ref;
use std::collections::HashMap;
use std::iter::FromIterator;
use super::{ext, WebGLExtension};
use super::{ext, WebGLExtension, WebGLExtensionSpec};
use super::wrapper::{WebGLExtensionWrapper, TypedWebGLExtensionWrapper};

// Data types that are implemented for texImage2D and texSubImage2D in WebGLRenderingContext
// Data types that are implemented for texImage2D and texSubImage2D in a WebGL 1.0 context
// but must trigger a InvalidValue error until the related WebGL Extensions are enabled.
// Example: https://www.khronos.org/registry/webgl/extensions/OES_texture_float/
const DEFAULT_DISABLED_TEX_TYPES: [GLenum; 2] = [
const DEFAULT_DISABLED_TEX_TYPES_WEBGL1: [GLenum; 2] = [
constants::FLOAT, OESTextureHalfFloatConstants::HALF_FLOAT_OES
];

Expand All @@ -37,10 +37,10 @@ const DEFAULT_NOT_FILTERABLE_TEX_TYPES: [GLenum; 2] = [
constants::FLOAT, OESTextureHalfFloatConstants::HALF_FLOAT_OES
];

// Param names that are implemented for getParameter WebGL function
// Param names that are implemented for glGetParameter in a WebGL 1.0 context
// but must trigger a InvalidEnum error until the related WebGL Extensions are enabled.
// Example: https://www.khronos.org/registry/webgl/extensions/OES_standard_derivatives/
const DEFAULT_DISABLED_GET_PARAMETER_NAMES: [GLenum; 1] = [
const DEFAULT_DISABLED_GET_PARAMETER_NAMES_WEBGL1: [GLenum; 1] = [
OESStandardDerivativesConstants::FRAGMENT_SHADER_DERIVATIVE_HINT_OES
];

Expand All @@ -58,16 +58,25 @@ struct WebGLExtensionFeatures {
disabled_get_parameter_names: FnvHashSet<GLenum>,
}

impl Default for WebGLExtensionFeatures {
fn default() -> WebGLExtensionFeatures {
WebGLExtensionFeatures {
impl WebGLExtensionFeatures {
fn new(webgl_version: WebGLVersion) -> Self {
let (disabled_tex_types, disabled_get_parameter_names) = match webgl_version {
WebGLVersion::WebGL1 => {
(DEFAULT_DISABLED_TEX_TYPES_WEBGL1.iter().cloned().collect(),
DEFAULT_DISABLED_GET_PARAMETER_NAMES_WEBGL1.iter().cloned().collect())
},
WebGLVersion::WebGL2 => {
(Default::default(), Default::default())
}
};
Self {
gl_extensions: Default::default(),
disabled_tex_types: DEFAULT_DISABLED_TEX_TYPES.iter().cloned().collect(),
disabled_tex_types,
not_filterable_tex_types: DEFAULT_NOT_FILTERABLE_TEX_TYPES.iter().cloned().collect(),
effective_tex_internal_formats: Default::default(),
query_parameter_handlers: Default::default(),
hint_targets: Default::default(),
disabled_get_parameter_names: DEFAULT_DISABLED_GET_PARAMETER_NAMES.iter().cloned().collect(),
disabled_get_parameter_names,
}
}
}
Expand All @@ -78,13 +87,15 @@ impl Default for WebGLExtensionFeatures {
pub struct WebGLExtensions {
extensions: DomRefCell<HashMap<String, Box<WebGLExtensionWrapper>>>,
features: DomRefCell<WebGLExtensionFeatures>,
webgl_version: WebGLVersion,
}

impl WebGLExtensions {
pub fn new() -> WebGLExtensions {
pub fn new(webgl_version: WebGLVersion) -> WebGLExtensions {
Self {
extensions: DomRefCell::new(HashMap::new()),
features: DomRefCell::new(Default::default())
features: DomRefCell::new(WebGLExtensionFeatures::new(webgl_version)),
webgl_version
}
}

Expand All @@ -104,7 +115,14 @@ impl WebGLExtensions {

pub fn get_suported_extensions(&self) -> Vec<&'static str> {
self.extensions.borrow().iter()
.filter(|ref v| v.1.is_supported(&self))
.filter(|ref v| {
if let WebGLExtensionSpec::Specific(version) = v.1.spec() {
if self.webgl_version != version {
return false;
}
}
v.1.is_supported(&self)
})
.map(|ref v| v.1.name())
.collect()
}
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/webgl_extensions/mod.rs
Expand Up @@ -22,4 +22,5 @@ pub mod ext_constants {
}

pub use self::extension::WebGLExtension;
pub use self::extension::WebGLExtensionSpec;
pub use self::extensions::WebGLExtensions;
7 changes: 6 additions & 1 deletion components/script/dom/webgl_extensions/wrapper.rs
Expand Up @@ -9,7 +9,7 @@ use dom::bindings::trace::JSTraceable;
use dom::webglrenderingcontext::WebGLRenderingContext;
use malloc_size_of::MallocSizeOf;
use std::any::Any;
use super::{WebGLExtension, WebGLExtensions};
use super::{WebGLExtension, WebGLExtensions, WebGLExtensionSpec};

/// Trait used internally by WebGLExtensions to store and
/// handle the different WebGL extensions in a common list.
Expand All @@ -18,6 +18,7 @@ pub trait WebGLExtensionWrapper: JSTraceable + MallocSizeOf {
ctx: &WebGLRenderingContext,
ext: &WebGLExtensions)
-> NonNullJSObjectPtr;
fn spec(&self) -> WebGLExtensionSpec;
fn is_supported(&self, &WebGLExtensions) -> bool;
fn is_enabled(&self) -> bool;
fn enable(&self, ext: &WebGLExtensions);
Expand Down Expand Up @@ -61,6 +62,10 @@ impl<T> WebGLExtensionWrapper for TypedWebGLExtensionWrapper<T>
}
}

fn spec(&self) -> WebGLExtensionSpec {
T::spec()
}

fn is_supported(&self, ext: &WebGLExtensions) -> bool {
self.is_enabled() || T::is_supported(ext)
}
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webglrenderingcontext.rs
Expand Up @@ -252,7 +252,7 @@ impl WebGLRenderingContext {
current_vertex_attrib_0: Cell::new((0f32, 0f32, 0f32, 1f32)),
current_scissor: Cell::new((0, 0, size.width, size.height)),
current_clear_color: Cell::new((0.0, 0.0, 0.0, 0.0)),
extension_manager: WebGLExtensions::new()
extension_manager: WebGLExtensions::new(webgl_version)
}
})
}
Expand Down

0 comments on commit f2f5817

Please sign in to comment.