Implement WebGL extensions.

This commit is contained in:
Imanol Fernandez 2017-05-16 11:14:23 +02:00
parent ac99a48aea
commit 32e23c4db4
40 changed files with 1346 additions and 86 deletions

View file

@ -0,0 +1,13 @@
/* 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::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use super::{ext_constants, WebGLExtension, WebGLExtensions};
pub mod oestexturefloat;
pub mod oestexturefloatlinear;
pub mod oestexturehalffloat;
pub mod oestexturehalffloatlinear;
pub mod oesvertexarrayobject;
pub mod webglvertexarrayobjectoes;

View file

@ -0,0 +1,56 @@
/* 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::OESTextureFloatBinding;
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::{constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensions};
#[dom_struct]
pub struct OESTextureFloat {
reflector_: Reflector,
}
impl OESTextureFloat {
fn new_inherited() -> OESTextureFloat {
Self {
reflector_: Reflector::new(),
}
}
}
impl WebGLExtension for OESTextureFloat {
type Extension = OESTextureFloat;
fn new(ctx: &WebGLRenderingContext) -> Root<OESTextureFloat> {
reflect_dom_object(box OESTextureFloat::new_inherited(),
&*ctx.global(),
OESTextureFloatBinding::Wrap)
}
fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_float",
"GL_ARB_texture_float"])
}
fn enable(ext: &WebGLExtensions) {
// Enable FLOAT text data type
ext.enable_tex_type(webgl::FLOAT);
let needs_replace = !ext.supports_gl_extension("GL_OES_texture_float");
if needs_replace {
// Special internal formats must be used to avoid clamped float values
ext.add_effective_tex_internal_format(webgl::RGBA, webgl::FLOAT, gl::RGBA32F);
ext.add_effective_tex_internal_format(webgl::RGB, webgl::FLOAT, gl::RGB32F);
ext.add_effective_tex_internal_format(webgl::LUMINANCE, webgl::FLOAT, gl::LUMINANCE32F_ARB);
ext.add_effective_tex_internal_format(webgl::ALPHA, webgl::FLOAT, gl::ALPHA32F_ARB);
ext.add_effective_tex_internal_format(webgl::LUMINANCE_ALPHA, webgl::FLOAT,
gl::LUMINANCE_ALPHA32F_ARB);
}
}
fn name() -> &'static str {
"OES_texture_float"
}
}

View file

@ -0,0 +1,45 @@
/* 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::OESTextureFloatLinearBinding;
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::{constants as webgl, WebGLExtension, WebGLExtensions};
#[dom_struct]
pub struct OESTextureFloatLinear {
reflector_: Reflector,
}
impl OESTextureFloatLinear {
fn new_inherited() -> OESTextureFloatLinear {
Self {
reflector_: Reflector::new(),
}
}
}
impl WebGLExtension for OESTextureFloatLinear {
type Extension = OESTextureFloatLinear;
fn new(ctx: &WebGLRenderingContext) -> Root<OESTextureFloatLinear> {
reflect_dom_object(box OESTextureFloatLinear::new_inherited(),
&*ctx.global(),
OESTextureFloatLinearBinding::Wrap)
}
fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_float_linear",
"GL_ARB_texture_float"])
}
fn enable(ext: &WebGLExtensions) {
ext.enable_filterable_tex_type(webgl::FLOAT);
}
fn name() -> &'static str {
"OES_texture_float_linear"
}
}

View file

@ -0,0 +1,57 @@
/* 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::OESTextureHalfFloatBinding::{self, OESTextureHalfFloatConstants};
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::{constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensions};
#[dom_struct]
pub struct OESTextureHalfFloat {
reflector_: Reflector,
}
impl OESTextureHalfFloat {
fn new_inherited() -> OESTextureHalfFloat {
Self {
reflector_: Reflector::new(),
}
}
}
impl WebGLExtension for OESTextureHalfFloat {
type Extension = OESTextureHalfFloat;
fn new(ctx: &WebGLRenderingContext) -> Root<OESTextureHalfFloat> {
reflect_dom_object(box OESTextureHalfFloat::new_inherited(),
&*ctx.global(),
OESTextureHalfFloatBinding::Wrap)
}
fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_half_float",
"GL_ARB_half_float_pixel",
"GL_NV_half_float"])
}
fn enable(ext: &WebGLExtensions) {
// Enable FLOAT text data type
let hf = OESTextureHalfFloatConstants::HALF_FLOAT_OES;
ext.enable_tex_type(hf);
let needs_replace = !ext.supports_gl_extension("GL_OES_texture_float");
if needs_replace {
// Special internal formats must be used to avoid clamped float values
ext.add_effective_tex_internal_format(webgl::RGBA, hf, gl::RGBA16F);
ext.add_effective_tex_internal_format(webgl::RGB, hf, gl::RGB16F);
ext.add_effective_tex_internal_format(webgl::LUMINANCE, hf, gl::LUMINANCE16F_ARB);
ext.add_effective_tex_internal_format(webgl::ALPHA, hf, gl::ALPHA16F_ARB);
ext.add_effective_tex_internal_format(webgl::LUMINANCE_ALPHA, hf, gl::LUMINANCE_ALPHA16F_ARB);
}
}
fn name() -> &'static str {
"OES_texture_half_float"
}
}

View file

@ -0,0 +1,47 @@
/* 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::OESTextureHalfFloatBinding::OESTextureHalfFloatConstants;
use dom::bindings::codegen::Bindings::OESTextureHalfFloatLinearBinding;
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 OESTextureHalfFloatLinear {
reflector_: Reflector,
}
impl OESTextureHalfFloatLinear {
fn new_inherited() -> OESTextureHalfFloatLinear {
Self {
reflector_: Reflector::new(),
}
}
}
impl WebGLExtension for OESTextureHalfFloatLinear {
type Extension = OESTextureHalfFloatLinear;
fn new(ctx: &WebGLRenderingContext) -> Root<OESTextureHalfFloatLinear> {
reflect_dom_object(box OESTextureHalfFloatLinear::new_inherited(),
&*ctx.global(),
OESTextureHalfFloatLinearBinding::Wrap)
}
fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_texture_float_linear",
"GL_ARB_half_float_pixel",
"GL_NV_half_float"])
}
fn enable(ext: &WebGLExtensions) {
ext.enable_filterable_tex_type(OESTextureHalfFloatConstants::HALF_FLOAT_OES);
}
fn name() -> &'static str {
"OES_texture_half_float_linear"
}
}

View file

@ -0,0 +1,166 @@
/* 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 canvas_traits::CanvasMsg;
use dom::bindings::codegen::Bindings::OESVertexArrayObjectBinding::{self, OESVertexArrayObjectMethods};
use dom::bindings::codegen::Bindings::OESVertexArrayObjectBinding::OESVertexArrayObjectConstants;
use dom::bindings::js::{JS, MutNullableJS, Root};
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::webglrenderingcontext::WebGLRenderingContext;
use dom::webglvertexarrayobjectoes::WebGLVertexArrayObjectOES;
use dom_struct::dom_struct;
use js::conversions::ToJSValConvertible;
use js::jsapi::JSContext;
use js::jsval::{JSVal, NullValue};
use std::iter;
use super::{WebGLExtension, WebGLExtensions};
use webrender_traits::{self, WebGLCommand, WebGLError};
#[dom_struct]
pub struct OESVertexArrayObject {
reflector_: Reflector,
ctx: JS<WebGLRenderingContext>,
bound_vao: MutNullableJS<WebGLVertexArrayObjectOES>,
}
impl OESVertexArrayObject {
fn new_inherited(ctx: &WebGLRenderingContext) -> OESVertexArrayObject {
Self {
reflector_: Reflector::new(),
ctx: JS::from_ref(ctx),
bound_vao: MutNullableJS::new(None)
}
}
#[allow(unsafe_code)]
fn get_current_binding(&self, cx:*mut JSContext) -> JSVal {
rooted!(in(cx) let mut rval = NullValue());
if let Some(bound_vao) = self.bound_vao.get() {
unsafe {
bound_vao.to_jsval(cx, rval.handle_mut());
}
}
rval.get()
}
}
impl OESVertexArrayObjectMethods for OESVertexArrayObject {
// https://www.khronos.org/registry/webgl/extensions/OES_vertex_array_object/
fn CreateVertexArrayOES(&self) -> Option<Root<WebGLVertexArrayObjectOES>> {
let (sender, receiver) = webrender_traits::channel::msg_channel().unwrap();
self.ctx.send_renderer_message(CanvasMsg::WebGL(WebGLCommand::CreateVertexArray(sender)));
let result = receiver.recv().unwrap();
result.map(|vao_id| WebGLVertexArrayObjectOES::new(&self.global(), vao_id))
}
// https://www.khronos.org/registry/webgl/extensions/OES_vertex_array_object/
fn DeleteVertexArrayOES(&self, vao: Option<&WebGLVertexArrayObjectOES>) {
if let Some(vao) = vao {
if vao.is_deleted() {
return;
}
// Unbind deleted VAO if currently bound
if let Some(bound_vao) = self.bound_vao.get() {
if bound_vao.id() == vao.id() {
self.bound_vao.set(None);
self.ctx.send_renderer_message(CanvasMsg::WebGL(WebGLCommand::BindVertexArray(None)));
}
}
// Remove VAO references from buffers
let buffers = vao.bound_attrib_buffers();
for buffer in buffers {
buffer.remove_vao_reference(vao.id());
}
if let Some(buffer) = vao.bound_buffer_element_array() {
buffer.remove_vao_reference(vao.id());
}
// Delete the vao
self.ctx.send_renderer_message(CanvasMsg::WebGL(WebGLCommand::DeleteVertexArray(vao.id())));
vao.set_deleted();
}
}
// https://www.khronos.org/registry/webgl/extensions/OES_vertex_array_object/
fn IsVertexArrayOES(&self, vao: Option<&WebGLVertexArrayObjectOES>) -> bool {
// Conformance tests expect false if vao never bound
vao.map_or(false, |vao| !vao.is_deleted() && vao.ever_bound())
}
// https://www.khronos.org/registry/webgl/extensions/OES_vertex_array_object/
fn BindVertexArrayOES(&self, vao: Option<&WebGLVertexArrayObjectOES>) {
if let Some(bound_vao) = self.bound_vao.get() {
// Store buffers attached to attrib pointers
let buffers = self.ctx.borrow_bound_attrib_buffers();
bound_vao.set_bound_attrib_buffers(buffers.iter().map(|(key, buffer)| {
(*buffer).add_vao_reference(bound_vao.id());
(*key, &**buffer)
}));
// Store element array buffer
let element_array = self.ctx.bound_buffer_element_array();
bound_vao.set_bound_buffer_element_array(element_array.as_ref().map(|buffer| {
buffer.add_vao_reference(bound_vao.id());
&**buffer
}));
}
if let Some(vao) = vao {
if vao.is_deleted() {
self.ctx.webgl_error(WebGLError::InvalidOperation);
return;
}
self.ctx.send_renderer_message(CanvasMsg::WebGL(WebGLCommand::BindVertexArray(Some(vao.id()))));
vao.set_ever_bound();
self.bound_vao.set(Some(&vao));
// Restore WebGLRenderingContext current bindings
let buffers = vao.borrow_bound_attrib_buffers();
self.ctx.set_bound_attrib_buffers(buffers.iter().map(|(k, v)| (*k, &**v)));
let element_array = vao.bound_buffer_element_array();
self.ctx.set_bound_buffer_element_array(element_array.as_ref().map(|buffer| &**buffer));
} else {
self.ctx.send_renderer_message(CanvasMsg::WebGL(WebGLCommand::BindVertexArray(None)));
self.bound_vao.set(None);
self.ctx.set_bound_attrib_buffers(iter::empty());
}
}
}
impl WebGLExtension for OESVertexArrayObject {
type Extension = OESVertexArrayObject;
fn new(ctx: &WebGLRenderingContext) -> Root<OESVertexArrayObject> {
reflect_dom_object(box OESVertexArrayObject::new_inherited(ctx),
&*ctx.global(),
OESVertexArrayObjectBinding::Wrap)
}
fn is_supported(ext: &WebGLExtensions) -> bool {
ext.supports_any_gl_extension(&["GL_OES_vertex_array_object",
"GL_ARB_vertex_array_object",
"GL_APPLE_vertex_array_object"])
}
fn enable(ext: &WebGLExtensions) {
let query = OESVertexArrayObjectConstants::VERTEX_ARRAY_BINDING_OES;
ext.add_query_parameter_handler(query, Box::new(|cx, webgl_ctx| {
match webgl_ctx.get_extension_manager().get_dom_object::<OESVertexArrayObject>() {
Some(dom_object) => {
Ok(dom_object.get_current_binding(cx))
},
None => {
// Extension instance not found!
Err(WebGLError::InvalidOperation)
}
}
}));
}
fn name() -> &'static str {
"OES_vertex_array_object"
}
}

View file

@ -0,0 +1,87 @@
/* 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 core::cell::Ref;
use core::iter::FromIterator;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::WebGLVertexArrayObjectOESBinding;
use dom::bindings::js::{JS, MutNullableJS};
use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::globalscope::GlobalScope;
use dom::webglbuffer::WebGLBuffer;
use dom::webglobject::WebGLObject;
use dom_struct::dom_struct;
use std::cell::Cell;
use std::collections::HashMap;
use webrender_traits::WebGLVertexArrayId;
#[dom_struct]
pub struct WebGLVertexArrayObjectOES {
webgl_object_: WebGLObject,
id: WebGLVertexArrayId,
ever_bound: Cell<bool>,
is_deleted: Cell<bool>,
bound_attrib_buffers: DOMRefCell<HashMap<u32, JS<WebGLBuffer>>>,
bound_buffer_element_array: MutNullableJS<WebGLBuffer>,
}
impl WebGLVertexArrayObjectOES {
fn new_inherited(id: WebGLVertexArrayId) -> WebGLVertexArrayObjectOES {
Self {
webgl_object_: WebGLObject::new_inherited(),
id: id,
ever_bound: Cell::new(false),
is_deleted: Cell::new(false),
bound_attrib_buffers: DOMRefCell::new(HashMap::new()),
bound_buffer_element_array: MutNullableJS::new(None),
}
}
pub fn new(global: &GlobalScope, id: WebGLVertexArrayId) -> Root<WebGLVertexArrayObjectOES> {
reflect_dom_object(box WebGLVertexArrayObjectOES::new_inherited(id),
global,
WebGLVertexArrayObjectOESBinding::Wrap)
}
pub fn id(&self) -> WebGLVertexArrayId {
self.id
}
pub fn is_deleted(&self) -> bool {
self.is_deleted.get()
}
pub fn set_deleted(&self) {
self.is_deleted.set(true)
}
pub fn ever_bound(&self) -> bool {
return self.ever_bound.get()
}
pub fn set_ever_bound(&self) {
self.ever_bound.set(true);
}
pub fn borrow_bound_attrib_buffers(&self) -> Ref<HashMap<u32, JS<WebGLBuffer>>> {
self.bound_attrib_buffers.borrow()
}
pub fn bound_attrib_buffers(&self) -> Vec<Root<WebGLBuffer>> {
self.bound_attrib_buffers.borrow().iter().map(|(_, b)| Root::from_ref(&**b)).collect()
}
pub fn set_bound_attrib_buffers<'a, T>(&self, iter: T) where T: Iterator<Item=(u32, &'a WebGLBuffer)> {
*self.bound_attrib_buffers.borrow_mut() = HashMap::from_iter(iter.map(|(k,v)| (k, JS::from_ref(v))));
}
pub fn bound_buffer_element_array(&self) -> Option<Root<WebGLBuffer>> {
self.bound_buffer_element_array.get()
}
pub fn set_bound_buffer_element_array(&self, buffer: Option<&WebGLBuffer>) {
self.bound_buffer_element_array.set(buffer);
}
}