webgl: Refactor texture validations to take advantage of rust type system

This commit introduces the `WebGLValidator` trait, and uses it for multiple
validations in the texture-related WebGL code, to move that logic out of the
already bloated `webglrenderingcontext.rs` file.

It also creates a type-safe wrapper for some WebGL types, removing all the
`unreachable!`s there, and introduces a macro for generating them conveniently.

This partially addresses #10693, pending refactor more code to use this
infrastructure, and (possibly?) introducing an `AsGLError` trait for the errors
to make the error handling happen in `WebGLContext`.
This commit is contained in:
Emilio Cobos Álvarez 2016-06-12 00:16:27 +02:00
parent 8d81ee77a8
commit 46c14aced2
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
7 changed files with 706 additions and 420 deletions

View file

@ -10,6 +10,7 @@ use dom::bindings::codegen::Bindings::WebGLTextureBinding;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::webgl_validations::types::{TexImageTarget, TexFormat, TexDataType};
use dom::webglobject::WebGLObject;
use ipc_channel::ipc::{self, IpcSender};
use std::cell::Cell;
@ -104,13 +105,13 @@ impl WebGLTexture {
}
pub fn initialize(&self,
target: u32,
target: TexImageTarget,
width: u32,
height: u32,
depth: u32,
internal_format: u32,
internal_format: TexFormat,
level: u32,
data_type: Option<u32>) -> WebGLResult<()> {
data_type: Option<TexDataType>) -> WebGLResult<()> {
let image_info = ImageInfo {
width: width,
height: height,
@ -120,17 +121,8 @@ impl WebGLTexture {
data_type: data_type,
};
let face = match target {
constants::TEXTURE_2D | constants::TEXTURE_CUBE_MAP_POSITIVE_X => 0,
constants::TEXTURE_CUBE_MAP_NEGATIVE_X => 1,
constants::TEXTURE_CUBE_MAP_POSITIVE_Y => 2,
constants::TEXTURE_CUBE_MAP_NEGATIVE_Y => 3,
constants::TEXTURE_CUBE_MAP_POSITIVE_Z => 4,
constants::TEXTURE_CUBE_MAP_NEGATIVE_Z => 5,
_ => unreachable!(),
};
self.set_image_infos_at_level_and_face(level, face, image_info);
let face_index = self.face_index_for_target(&target);
self.set_image_infos_at_level_and_face(level, face_index, image_info);
Ok(())
}
@ -312,7 +304,27 @@ impl WebGLTexture {
true
}
pub fn image_info_at_face(&self, face: u8, level: u32) -> ImageInfo {
fn face_index_for_target(&self,
target: &TexImageTarget) -> u8 {
match *target {
TexImageTarget::Texture2D => 0,
TexImageTarget::CubeMapPositiveX => 0,
TexImageTarget::CubeMapNegativeX => 1,
TexImageTarget::CubeMapPositiveY => 2,
TexImageTarget::CubeMapNegativeY => 3,
TexImageTarget::CubeMapPositiveZ => 4,
TexImageTarget::CubeMapNegativeZ => 5,
}
}
pub fn image_info_for_target(&self,
target: &TexImageTarget,
level: u32) -> ImageInfo {
let face_index = self.face_index_for_target(&target);
self.image_info_at_face(face_index, level)
}
fn image_info_at_face(&self, face: u8, level: u32) -> ImageInfo {
let pos = (level * self.face_count.get() as u32) + face as u32;
self.image_info_array.borrow()[pos as usize]
}
@ -347,9 +359,9 @@ pub struct ImageInfo {
width: u32,
height: u32,
depth: u32,
internal_format: Option<u32>,
internal_format: Option<TexFormat>,
is_initialized: bool,
data_type: Option<u32>,
data_type: Option<TexDataType>,
}
impl ImageInfo {
@ -372,16 +384,18 @@ impl ImageInfo {
self.height
}
pub fn internal_format(&self) -> Option<u32> {
pub fn internal_format(&self) -> Option<TexFormat> {
self.internal_format
}
pub fn data_type(&self) -> Option<u32> {
pub fn data_type(&self) -> Option<TexDataType> {
self.data_type
}
fn is_power_of_two(&self) -> bool {
self.width.is_power_of_two() && self.height.is_power_of_two() && self.depth.is_power_of_two()
self.width.is_power_of_two() &&
self.height.is_power_of_two() &&
self.depth.is_power_of_two()
}
fn is_initialized(&self) -> bool {