Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace `object` function arguments in WebGL with typed arrays #20396

Merged
merged 9 commits into from Mar 23, 2018
Next

Change WebGL function signatures accepting typed arrays

  • Loading branch information
Xanewok committed Mar 23, 2018
commit 36f39ce27afc83abc21295ee61d2a0485a2071ab
@@ -9,6 +9,7 @@ use dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2Rende
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods;
use dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement;
use dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferView;
use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::reflector::{reflect_dom_object, Reflector};
use dom::bindings::root::{Dom, DomRoot, LayoutDom};
@@ -30,6 +31,8 @@ use dom_struct::dom_struct;
use euclid::Size2D;
use js::jsapi::{JSContext, JSObject};
use js::jsval::JSVal;
use js::rust::CustomAutoRooterGuard;
use js::typedarray::ArrayBufferView;
use offscreen_gl_context::GLContextAttributes;
use script_layout_interface::HTMLCanvasDataSource;
use std::ptr::NonNull;
@@ -235,25 +238,23 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
self.base.BufferData_(target, size, usage)
}

#[allow(unsafe_code)]
/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.5
unsafe fn BufferSubData(&self, cx: *mut JSContext, target: u32, offset: i64, data: *mut JSObject) -> Fallible<()> {
self.base.BufferSubData(cx, target, offset, data)
fn BufferSubData(&self, target: u32, offset: i64, data: Option<ArrayBufferOrArrayBufferView>) {
self.base.BufferSubData(target, offset, data)
}

#[allow(unsafe_code)]
/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
unsafe fn CompressedTexImage2D(&self, cx: *mut JSContext, target: u32, level: i32, internal_format: u32,
width: i32, height: i32, border: i32, pixels: *mut JSObject) -> Fallible<()> {
self.base.CompressedTexImage2D(cx, target, level, internal_format, width, height, border, pixels)
fn CompressedTexImage2D(&self, target: u32, level: i32, internal_format: u32,
width: i32, height: i32, border: i32,
pixels: CustomAutoRooterGuard<ArrayBufferView>) {
self.base.CompressedTexImage2D(target, level, internal_format, width, height, border, pixels)
}

#[allow(unsafe_code)]
/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
unsafe fn CompressedTexSubImage2D(&self, cx: *mut JSContext, target: u32, level: i32,
xoffset: i32, yoffset: i32, width: i32, height: i32,
format: u32, pixels: *mut JSObject) -> Fallible<()> {
self.base.CompressedTexSubImage2D(cx, target, level, xoffset, yoffset, width, height, format, pixels)
fn CompressedTexSubImage2D(&self, target: u32, level: i32, xoffset: i32,
yoffset: i32, width: i32, height: i32, format: u32,
pixels: CustomAutoRooterGuard<ArrayBufferView>) {
self.base.CompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, pixels)
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
@@ -533,11 +534,10 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
self.base.PolygonOffset(factor, units)
}

#[allow(unsafe_code)]
/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12
unsafe fn ReadPixels(&self, cx: *mut JSContext, x: i32, y: i32, width: i32, height: i32,
format: u32, pixel_type: u32, pixels: *mut JSObject) -> Fallible<()> {
self.base.ReadPixels(cx, x, y, width, height, format, pixel_type, pixels)
fn ReadPixels(&self, x: i32, y: i32, width: i32, height: i32, format: u32, pixel_type: u32,
pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>) {
self.base.ReadPixels(x, y, width, height, format, pixel_type, pixels)
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.3
@@ -820,19 +820,17 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexImage2D(&self,
cx: *mut JSContext,
target: u32,
level: i32,
internal_format: u32,
width: i32,
height: i32,
border: i32,
format: u32,
data_type: u32,
data_ptr: *mut JSObject) -> Fallible<()> {
self.base.TexImage2D(cx, target, level, internal_format, width, height, border, format, data_type, data_ptr)
fn TexImage2D(&self,
target: u32,
level: i32,
internal_format: u32,
width: i32,
height: i32,
border: i32,
format: u32,
data_type: u32,
pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>) -> Fallible<()> {
self.base.TexImage2D(target, level, internal_format, width, height, border, format, data_type, pixels)
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
@@ -862,19 +860,17 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingContext {
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexSubImage2D(&self,
cx: *mut JSContext,
target: u32,
level: i32,
xoffset: i32,
yoffset: i32,
width: i32,
height: i32,
format: u32,
data_type: u32,
data_ptr: *mut JSObject) -> Fallible<()> {
self.base.TexSubImage2D(cx, target, level, xoffset, yoffset, width, height, format, data_type, data_ptr)
fn TexSubImage2D(&self,
target: u32,
level: i32,
xoffset: i32,
yoffset: i32,
width: i32,
height: i32,
format: u32,
data_type: u32,
pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>) -> Fallible<()> {
self.base.TexSubImage2D(target, level, xoffset, yoffset, width, height, format, data_type, pixels)
}

/// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
@@ -15,6 +15,7 @@ use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{self, WebGL
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods;
use dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement;
use dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferView;
use dom::bindings::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible};
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::inheritance::Castable;
@@ -49,7 +50,8 @@ use half::f16;
use js::conversions::ConversionBehavior;
use js::jsapi::{JSContext, JSObject, Type};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UndefinedValue};
use js::typedarray::{TypedArray, TypedArrayElement, Float32, Int32};
use js::typedarray::{ArrayBufferView, TypedArray, TypedArrayElement, Float32, Int32};
use js::rust::CustomAutoRooterGuard;
use net_traits::image::base::PixelFormat;
use net_traits::image_cache::ImageResponse;
use offscreen_gl_context::{GLContextAttributes, GLLimits};
@@ -1706,9 +1708,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
Ok(())
}

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.5
unsafe fn BufferSubData(&self, cx: *mut JSContext, target: u32, offset: i64, data: *mut JSObject) -> Fallible<()> {
fn BufferSubData(&self, target: u32, offset: i64, data: Option<ArrayBufferOrArrayBufferView>) {
if data.is_null() {
return Ok(self.webgl_error(InvalidValue));
}
@@ -1742,23 +1743,20 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
Ok(())
}

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
unsafe fn CompressedTexImage2D(&self, cx: *mut JSContext, _target: u32, _level: i32, _internal_format: u32,
_width: i32, _height: i32, _border: i32, pixels: *mut JSObject) -> Fallible<()> {
let _data = fallible_array_buffer_view_to_vec(cx, pixels)?;
fn CompressedTexImage2D(&self, _target: u32, _level: i32, _internal_format: u32,
_width: i32, _height: i32, _border: i32,
_data: CustomAutoRooterGuard<ArrayBufferView>) {
// FIXME: No compressed texture format is currently supported, so error out as per
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#COMPRESSED_TEXTURE_SUPPORT
self.webgl_error(InvalidEnum);
Ok(())
}

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
unsafe fn CompressedTexSubImage2D(&self, cx: *mut JSContext, _target: u32, _level: i32,
_xoffset: i32, _yoffset: i32, _width: i32, _height: i32,
_format: u32, pixels: *mut JSObject) -> Fallible<()> {
let _data = fallible_array_buffer_view_to_vec(cx, pixels)?;
fn CompressedTexSubImage2D(&self, _target: u32, _level: i32, _xoffset: i32,
_yoffset: i32, _width: i32, _height: i32, _format: u32,
_data: CustomAutoRooterGuard<ArrayBufferView>) {
// FIXME: No compressed texture format is currently supported, so error out as per
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#COMPRESSED_TEXTURE_SUPPORT
self.webgl_error(InvalidEnum);
@@ -2609,10 +2607,9 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
self.send_command(WebGLCommand::PolygonOffset(factor, units))
}

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12
unsafe fn ReadPixels(&self, cx: *mut JSContext, x: i32, y: i32, width: i32, height: i32,
format: u32, pixel_type: u32, pixels: *mut JSObject) -> Fallible<()> {
fn ReadPixels(&self, x: i32, y: i32, width: i32, height: i32, format: u32, pixel_type: u32,
pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>) {
if pixels.is_null() {
return Ok(self.webgl_error(InvalidValue));
}
@@ -3238,9 +3235,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexImage2D(&self,
cx: *mut JSContext,
fn TexImage2D(&self,
target: u32,
level: i32,
internal_format: u32,
@@ -3249,7 +3244,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
border: i32,
format: u32,
data_type: u32,
data_ptr: *mut JSObject) -> Fallible<()> {
pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>) -> Fallible<()> {
if !self.extension_manager.is_tex_type_enabled(data_type) {
return Ok(self.webgl_error(InvalidEnum));
}
@@ -3410,9 +3405,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexSubImage2D(&self,
cx: *mut JSContext,
fn TexSubImage2D(&self,
target: u32,
level: i32,
xoffset: i32,
@@ -3421,7 +3414,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
height: i32,
format: u32,
data_type: u32,
data_ptr: *mut JSObject) -> Fallible<()> {
pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>) -> Fallible<()> {
let data = if data_ptr.is_null() {
None
} else {
@@ -28,6 +28,7 @@ typedef (ImageData or
HTMLImageElement or
HTMLCanvasElement or
HTMLVideoElement) TexImageSource;
typedef (ArrayBuffer or ArrayBufferView) BufferDataSource;


dictionary WebGLContextAttributes {
@@ -488,21 +489,15 @@ interface WebGLRenderingContextBase
void blendFuncSeparate(GLenum srcRGB, GLenum dstRGB,
GLenum srcAlpha, GLenum dstAlpha);

// typedef (ArrayBuffer or ArrayBufferView) BufferDataSource;
// FIXME(dmarcos) The function below is the original function in the webIdl:
// FIXME(xanewok) from CodegenRust.py:
// 'No support for unions as distinguishing arguments yet' for below
// original WebIDL function definition
// void bufferData(GLenum target, BufferDataSource? data, GLenum usage);
// The Code generator doesn't handle BufferDataSource so we're using 'object?'
// in the meantime, and marking the function as [Throws], so we can handle
// the type error from inside.
[Throws]
void bufferData(GLenum target, object? data, GLenum usage);
// FIXME: Codegen requires that this have [Throws] to match the other one.
[Throws]
void bufferData(GLenum target, GLsizeiptr size, GLenum usage);

//void bufferSubData(GLenum target, GLintptr offset, BufferDataSource? data);
[Throws]
void bufferSubData(GLenum target, GLintptr offset, object? data);
void bufferSubData(GLenum target, GLintptr offset, BufferDataSource? data);

[WebGLHandlesContextLoss] GLenum checkFramebufferStatus(GLenum target);
void clear(GLbitfield mask);
@@ -512,25 +507,13 @@ interface WebGLRenderingContextBase
void colorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
void compileShader(WebGLShader shader);

// FIXME(simartin) The Code generator doesn't handle ArrayBufferView so we're
// using 'object' in the meantime, and marking the function as Throws to
// handle the type error from inside.
// void compressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
// GLsizei width, GLsizei height, GLint border,
// ArrayBufferView data);
[Throws]
void compressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
GLsizei width, GLsizei height, GLint border,
object data);
// void compressedTexSubImage2D(GLenum target, GLint level,
// GLint xoffset, GLint yoffset,
// GLsizei width, GLsizei height, GLenum format,
// ArrayBufferView data);
[Throws]
ArrayBufferView data);
void compressedTexSubImage2D(GLenum target, GLint level,
GLint xoffset, GLint yoffset,
GLsizei width, GLsizei height, GLenum format,
object data);
ArrayBufferView data);

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
void copyTexImage2D(GLenum target, GLint level, GLenum internalformat,
@@ -623,11 +606,8 @@ interface WebGLRenderingContextBase
void pixelStorei(GLenum pname, GLint param);
void polygonOffset(GLfloat factor, GLfloat units);

//void readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
// GLenum format, GLenum type, ArrayBufferView? pixels);
[Throws]
void readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type, object? pixels);
GLenum format, GLenum type, ArrayBufferView? pixels);

void renderbufferStorage(GLenum target, GLenum internalformat,
GLsizei width, GLsizei height);
@@ -643,14 +623,11 @@ interface WebGLRenderingContextBase
void stencilOp(GLenum fail, GLenum zfail, GLenum zpass);
void stencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);

//void texImage2D(GLenum target, GLint level, GLenum internalformat,
// GLsizei width, GLsizei height, GLint border, GLenum format,
// GLenum type, ArrayBufferView? pixels);
// FIXME: SM interface arguments
// FIXME: Codegen requires that this have [Throws] to match the other one.
[Throws]
void texImage2D(GLenum target, GLint level, GLenum internalformat,
GLsizei width, GLsizei height, GLint border, GLenum format,
GLenum type, object? data);
GLenum type, ArrayBufferView? pixels);
[Throws]
void texImage2D(GLenum target, GLint level, GLenum internalformat,
GLenum format, GLenum type, TexImageSource source); // May throw DOMException
@@ -661,10 +638,11 @@ interface WebGLRenderingContextBase
void texParameterf(GLenum target, GLenum pname, GLfloat param);
void texParameteri(GLenum target, GLenum pname, GLint param);

// FIXME: Codegen requires that this have [Throws] to match the other one.
[Throws]
void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
GLsizei width, GLsizei height,
GLenum format, GLenum type, object? data);
GLenum format, GLenum type, ArrayBufferView? pixels);
[Throws]
void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
GLenum format, GLenum type, TexImageSource source); // May throw DOMException
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.