Implement WebGL OES_standard_derivatives extension.

This commit is contained in:
Imanol Fernandez 2017-08-21 22:37:19 +02:00
parent d4e43d9d76
commit 1dd3899c37
9 changed files with 127 additions and 20 deletions

View file

@ -5,6 +5,7 @@
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use super::{ext_constants, WebGLExtension, WebGLExtensions};
pub mod oesstandardderivatives;
pub mod oestexturefloat;
pub mod oestexturefloatlinear;
pub mod oestexturehalffloat;

View file

@ -0,0 +1,50 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 dom::bindings::codegen::Bindings::OESStandardDerivativesBinding;
use dom::bindings::codegen::Bindings::OESStandardDerivativesBinding::OESStandardDerivativesConstants;
use dom::bindings::js::Root;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom_struct::dom_struct;
use super::{WebGLExtension, WebGLExtensions};
#[dom_struct]
pub struct OESStandardDerivatives {
reflector_: Reflector,
}
impl OESStandardDerivatives {
fn new_inherited() -> OESStandardDerivatives {
Self {
reflector_: Reflector::new(),
}
}
}
impl WebGLExtension for OESStandardDerivatives {
type Extension = OESStandardDerivatives;
fn new(ctx: &WebGLRenderingContext) -> Root<OESStandardDerivatives> {
reflect_dom_object(box OESStandardDerivatives::new_inherited(),
&*ctx.global(),
OESStandardDerivativesBinding::Wrap)
}
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"]);
}
// The standard derivatives are always available in desktop OpenGL.
true
}
fn enable(ext: &WebGLExtensions) {
ext.enable_hint_target(OESStandardDerivativesConstants::FRAGMENT_SHADER_DERIVATIVE_HINT_OES);
ext.enable_get_parameter_name(OESStandardDerivativesConstants::FRAGMENT_SHADER_DERIVATIVE_HINT_OES);
}
fn name() -> &'static str {
"OES_standard_derivatives"
}
}

View file

@ -6,6 +6,7 @@ use canvas_traits::webgl::WebGLError;
use core::iter::FromIterator;
use core::nonzero::NonZero;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::OESStandardDerivativesBinding::OESStandardDerivativesConstants;
use dom::bindings::codegen::Bindings::OESTextureHalfFloatBinding::OESTextureHalfFloatConstants;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use dom::bindings::js::Root;
@ -35,6 +36,13 @@ const DEFAULT_NOT_FILTERABLE_TEX_TYPES: [GLenum; 2] = [
constants::FLOAT, OESTextureHalfFloatConstants::HALF_FLOAT_OES
];
// Param names that are implemented for getParameter WebGL function
// 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] = [
OESStandardDerivativesConstants::FRAGMENT_SHADER_DERIVATIVE_HINT_OES
];
/// WebGL features that are enabled/disabled by WebGL Extensions.
#[derive(JSTraceable, HeapSizeOf)]
struct WebGLExtensionFeatures {
@ -42,7 +50,11 @@ struct WebGLExtensionFeatures {
disabled_tex_types: HashSet<GLenum>,
not_filterable_tex_types: HashSet<GLenum>,
effective_tex_internal_formats: HashMap<TexFormatType, u32>,
query_parameter_handlers: HashMap<GLenum, WebGLQueryParameterHandler>
query_parameter_handlers: HashMap<GLenum, WebGLQueryParameterHandler>,
/// WebGL Hint() targets enabled by extensions.
hint_targets: HashSet<GLenum>,
/// WebGL GetParameter() names enabled by extensions.
disabled_get_parameter_names: HashSet<GLenum>,
}
impl Default for WebGLExtensionFeatures {
@ -52,7 +64,9 @@ impl Default for WebGLExtensionFeatures {
disabled_tex_types: DEFAULT_DISABLED_TEX_TYPES.iter().cloned().collect(),
not_filterable_tex_types: DEFAULT_NOT_FILTERABLE_TEX_TYPES.iter().cloned().collect(),
effective_tex_internal_formats: HashMap::new(),
query_parameter_handlers: HashMap::new()
query_parameter_handlers: HashMap::new(),
hint_targets: HashSet::new(),
disabled_get_parameter_names: DEFAULT_DISABLED_GET_PARAMETER_NAMES.iter().cloned().collect(),
}
}
}
@ -105,8 +119,18 @@ impl WebGLExtensions {
})
}
pub fn is_enabled<T>(&self) -> bool
where
T: 'static + WebGLExtension + JSTraceable + HeapSizeOf
{
let name = T::name().to_uppercase();
self.extensions.borrow().get(&name).map_or(false, |ext| { ext.is_enabled() })
}
pub fn get_dom_object<T>(&self) -> Option<Root<T::Extension>>
where T: 'static + WebGLExtension + JSTraceable + HeapSizeOf {
where
T: 'static + WebGLExtension + JSTraceable + HeapSizeOf
{
let name = T::name().to_uppercase();
self.extensions.borrow().get(&name).and_then(|extension| {
extension.as_any().downcast_ref::<TypedWebGLExtensionWrapper<T>>().and_then(|extension| {
@ -172,7 +196,24 @@ impl WebGLExtensions {
})
}
pub fn enable_hint_target(&self, name: GLenum) {
self.features.borrow_mut().hint_targets.insert(name);
}
pub fn is_hint_target_enabled(&self, name: GLenum) -> bool {
self.features.borrow().hint_targets.contains(&name)
}
pub fn enable_get_parameter_name(&self, name: GLenum) {
self.features.borrow_mut().disabled_get_parameter_names.remove(&name);
}
pub fn is_get_parameter_name_enabled(&self, name: GLenum) -> bool {
!self.features.borrow().disabled_get_parameter_names.contains(&name)
}
fn register_all_extensions(&self) {
self.register::<ext::oesstandardderivatives::OESStandardDerivatives>();
self.register::<ext::oestexturefloat::OESTextureFloat>();
self.register::<ext::oestexturefloatlinear::OESTextureFloatLinear>();
self.register::<ext::oestexturehalffloat::OESTextureHalfFloat>();

View file

@ -20,6 +20,7 @@ pub trait WebGLExtensionWrapper: JSTraceable + HeapSizeOf {
ext: &WebGLExtensions)
-> NonZero<*mut JSObject>;
fn is_supported(&self, &WebGLExtensions) -> bool;
fn is_enabled(&self) -> bool;
fn enable(&self, ext: &WebGLExtensions);
fn name(&self) -> &'static str;
fn as_any(&self) -> &Any;
@ -62,7 +63,11 @@ impl<T> WebGLExtensionWrapper for TypedWebGLExtensionWrapper<T>
}
fn is_supported(&self, ext: &WebGLExtensions) -> bool {
self.extension.get().is_some() || T::is_supported(ext)
self.is_enabled() || T::is_supported(ext)
}
fn is_enabled(&self) -> bool {
self.extension.get().is_some()
}
fn enable(&self, ext: &WebGLExtensions) {