webgl: Fix support for float/half-float texture formats.

This commit is contained in:
Josh Matthews 2020-04-09 17:18:35 -04:00
parent 8789a6a8d8
commit 29ab55d076
10 changed files with 88 additions and 56 deletions

View file

@ -2773,7 +2773,8 @@ fn image_to_tex_image_data(
}
match (format, data_type) {
(TexFormat::RGBA, TexDataType::UnsignedByte) => pixels,
(TexFormat::RGBA, TexDataType::UnsignedByte) |
(TexFormat::RGBA8, TexDataType::UnsignedByte) => pixels,
(TexFormat::RGB, TexDataType::UnsignedByte) => {
for i in 0..pixel_count {
let rgb = {
@ -2854,7 +2855,8 @@ fn image_to_tex_image_data(
pixels.truncate(pixel_count * 2);
pixels
},
(TexFormat::RGBA, TexDataType::Float) => {
(TexFormat::RGBA, TexDataType::Float) |
(TexFormat::RGBA32f, TexDataType::Float) => {
let mut rgbaf32 = Vec::<u8>::with_capacity(pixel_count * 16);
for rgba8 in pixels.chunks(4) {
rgbaf32.write_f32::<NativeEndian>(rgba8[0] as f32).unwrap();
@ -2865,7 +2867,8 @@ fn image_to_tex_image_data(
rgbaf32
},
(TexFormat::RGB, TexDataType::Float) => {
(TexFormat::RGB, TexDataType::Float) |
(TexFormat::RGB32f, TexDataType::Float) => {
let mut rgbf32 = Vec::<u8>::with_capacity(pixel_count * 12);
for rgba8 in pixels.chunks(4) {
rgbf32.write_f32::<NativeEndian>(rgba8[0] as f32).unwrap();
@ -2875,7 +2878,8 @@ fn image_to_tex_image_data(
rgbf32
},
(TexFormat::Alpha, TexDataType::Float) => {
(TexFormat::Alpha, TexDataType::Float) |
(TexFormat::Alpha32f, TexDataType::Float) => {
for rgba8 in pixels.chunks_mut(4) {
let p = rgba8[3] as f32;
NativeEndian::write_f32(rgba8, p);
@ -2883,7 +2887,8 @@ fn image_to_tex_image_data(
pixels
},
(TexFormat::Luminance, TexDataType::Float) => {
(TexFormat::Luminance, TexDataType::Float) |
(TexFormat::Luminance32f, TexDataType::Float) => {
for rgba8 in pixels.chunks_mut(4) {
let p = rgba8[0] as f32;
NativeEndian::write_f32(rgba8, p);
@ -2891,7 +2896,8 @@ fn image_to_tex_image_data(
pixels
},
(TexFormat::LuminanceAlpha, TexDataType::Float) => {
(TexFormat::LuminanceAlpha, TexDataType::Float) |
(TexFormat::LuminanceAlpha32f, TexDataType::Float) => {
let mut data = Vec::<u8>::with_capacity(pixel_count * 8);
for rgba8 in pixels.chunks(4) {
data.write_f32::<NativeEndian>(rgba8[0] as f32).unwrap();
@ -2900,7 +2906,8 @@ fn image_to_tex_image_data(
data
},
(TexFormat::RGBA, TexDataType::HalfFloat) => {
(TexFormat::RGBA, TexDataType::HalfFloat) |
(TexFormat::RGBA16f, TexDataType::HalfFloat) => {
let mut rgbaf16 = Vec::<u8>::with_capacity(pixel_count * 8);
for rgba8 in pixels.chunks(4) {
rgbaf16
@ -2919,7 +2926,8 @@ fn image_to_tex_image_data(
rgbaf16
},
(TexFormat::RGB, TexDataType::HalfFloat) => {
(TexFormat::RGB, TexDataType::HalfFloat) |
(TexFormat::RGB16f, TexDataType::HalfFloat) => {
let mut rgbf16 = Vec::<u8>::with_capacity(pixel_count * 6);
for rgba8 in pixels.chunks(4) {
rgbf16
@ -2934,7 +2942,8 @@ fn image_to_tex_image_data(
}
rgbf16
},
(TexFormat::Alpha, TexDataType::HalfFloat) => {
(TexFormat::Alpha, TexDataType::HalfFloat) |
(TexFormat::Alpha16f, TexDataType::HalfFloat) => {
for i in 0..pixel_count {
let p = f16::from_f32(pixels[i * 4 + 3] as f32).as_bits();
NativeEndian::write_u16(&mut pixels[i * 2..i * 2 + 2], p);
@ -2942,7 +2951,8 @@ fn image_to_tex_image_data(
pixels.truncate(pixel_count * 2);
pixels
},
(TexFormat::Luminance, TexDataType::HalfFloat) => {
(TexFormat::Luminance, TexDataType::HalfFloat) |
(TexFormat::Luminance16f, TexDataType::HalfFloat) => {
for i in 0..pixel_count {
let p = f16::from_f32(pixels[i * 4] as f32).as_bits();
NativeEndian::write_u16(&mut pixels[i * 2..i * 2 + 2], p);
@ -2950,7 +2960,8 @@ fn image_to_tex_image_data(
pixels.truncate(pixel_count * 2);
pixels
},
(TexFormat::LuminanceAlpha, TexDataType::HalfFloat) => {
(TexFormat::LuminanceAlpha, TexDataType::HalfFloat) |
(TexFormat::LuminanceAlpha16f, TexDataType::HalfFloat) => {
for rgba8 in pixels.chunks_mut(4) {
let lum = f16::from_f32(rgba8[0] as f32).as_bits();
let a = f16::from_f32(rgba8[3] as f32).as_bits();

View file

@ -954,6 +954,13 @@ mod gl_ext_constants {
COMPRESSED_RGBA_S3TC_DXT5_EXT,
COMPRESSED_RGB_ETC1_WEBGL,
];
pub const ALPHA16F_ARB: u32 = 0x881C;
pub const ALPHA32F_ARB: u32 = 0x8816;
pub const LUMINANCE16F_ARB: u32 = 0x881E;
pub const LUMINANCE32F_ARB: u32 = 0x8818;
pub const LUMINANCE_ALPHA16F_ARB: u32 = 0x881F;
pub const LUMINANCE_ALPHA32F_ARB: u32 = 0x8819;
}
gl_enums! {
@ -961,6 +968,8 @@ gl_enums! {
DepthComponent = gl::DEPTH_COMPONENT,
DepthStencil = gl::DEPTH_STENCIL,
Alpha = gl::ALPHA,
Alpha32f = gl_ext_constants::ALPHA32F_ARB,
Alpha16f = gl_ext_constants::ALPHA16F_ARB,
Red = gl::RED,
RedInteger = gl::RED_INTEGER,
RG = gl::RG,
@ -971,6 +980,10 @@ gl_enums! {
RGBAInteger = gl::RGBA_INTEGER,
Luminance = gl::LUMINANCE,
LuminanceAlpha = gl::LUMINANCE_ALPHA,
Luminance32f = gl_ext_constants::LUMINANCE32F_ARB,
Luminance16f = gl_ext_constants::LUMINANCE16F_ARB,
LuminanceAlpha32f = gl_ext_constants::LUMINANCE_ALPHA32F_ARB,
LuminanceAlpha16f = gl_ext_constants::LUMINANCE_ALPHA16F_ARB,
CompressedRgbS3tcDxt1 = gl_ext_constants::COMPRESSED_RGB_S3TC_DXT1_EXT,
CompressedRgbaS3tcDxt1 = gl_ext_constants::COMPRESSED_RGBA_S3TC_DXT1_EXT,
CompressedRgbaS3tcDxt3 = gl_ext_constants::COMPRESSED_RGBA_S3TC_DXT3_EXT,
@ -1143,21 +1156,40 @@ impl TexFormat {
TexFormat::DepthComponent32f => TexFormat::DepthComponent,
TexFormat::Depth24Stencil8 => TexFormat::DepthStencil,
TexFormat::Depth32fStencil8 => TexFormat::DepthStencil,
TexFormat::Alpha32f => TexFormat::Alpha,
TexFormat::Alpha16f => TexFormat::Alpha,
TexFormat::Luminance32f => TexFormat::Luminance,
TexFormat::Luminance16f => TexFormat::Luminance,
TexFormat::LuminanceAlpha32f => TexFormat::LuminanceAlpha,
TexFormat::LuminanceAlpha16f => TexFormat::LuminanceAlpha,
_ => self,
}
}
pub fn compatible_data_types(self) -> &'static [TexDataType] {
match self {
TexFormat::RGB => &[TexDataType::UnsignedByte, TexDataType::UnsignedShort565][..],
TexFormat::RGB => &[
TexDataType::UnsignedByte,
TexDataType::UnsignedShort565,
TexDataType::Float,
TexDataType::HalfFloat,
][..],
TexFormat::RGBA => &[
TexDataType::UnsignedByte,
TexDataType::UnsignedShort4444,
TexDataType::UnsignedShort5551,
TexDataType::Float,
TexDataType::HalfFloat,
][..],
TexFormat::LuminanceAlpha => &[TexDataType::UnsignedByte][..],
TexFormat::Luminance => &[TexDataType::UnsignedByte][..],
TexFormat::Alpha => &[TexDataType::UnsignedByte][..],
TexFormat::LuminanceAlpha => &[TexDataType::UnsignedByte, TexDataType::Float, TexDataType::HalfFloat][..],
TexFormat::Luminance => &[TexDataType::UnsignedByte, TexDataType::Float, TexDataType::HalfFloat][..],
TexFormat::Alpha => &[TexDataType::UnsignedByte, TexDataType::Float, TexDataType::HalfFloat][..],
TexFormat::LuminanceAlpha32f => &[TexDataType::Float][..],
TexFormat::LuminanceAlpha16f => &[TexDataType::HalfFloat][..],
TexFormat::Luminance32f => &[TexDataType::Float][..],
TexFormat::Luminance16f => &[TexDataType::HalfFloat][..],
TexFormat::Alpha32f => &[TexDataType::Float][..],
TexFormat::Alpha16f => &[TexDataType::HalfFloat][..],
TexFormat::R8 => &[TexDataType::UnsignedByte][..],
TexFormat::R8SNorm => &[TexDataType::Byte][..],
TexFormat::R16f => &[TexDataType::HalfFloat, TexDataType::Float][..],

View file

@ -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 https://mozilla.org/MPL/2.0/. */
use super::{ext_constants, WebGLExtension, WebGLExtensionSpec, WebGLExtensions};
use super::{WebGLExtension, WebGLExtensionSpec, WebGLExtensions};
use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
pub mod angleinstancedarrays;

View file

@ -3,12 +3,12 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use super::{
constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensionSpec, WebGLExtensions,
constants as webgl, WebGLExtension, WebGLExtensionSpec, WebGLExtensions,
};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::webglrenderingcontext::WebGLRenderingContext;
use canvas_traits::webgl::WebGLVersion;
use canvas_traits::webgl::{TexFormat, WebGLVersion};
use dom_struct::dom_struct;
#[dom_struct]
@ -44,14 +44,14 @@ impl WebGLExtension for OESTextureFloat {
fn enable(ext: &WebGLExtensions) {
ext.enable_tex_type(webgl::FLOAT);
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(TexFormat::RGBA, webgl::FLOAT, TexFormat::RGBA32f);
ext.add_effective_tex_internal_format(TexFormat::RGB, webgl::FLOAT, TexFormat::RGB32f);
ext.add_effective_tex_internal_format(TexFormat::Luminance, webgl::FLOAT, TexFormat::Luminance32f);
ext.add_effective_tex_internal_format(TexFormat::Alpha, webgl::FLOAT, TexFormat::Alpha32f);
ext.add_effective_tex_internal_format(
webgl::LUMINANCE_ALPHA,
TexFormat::LuminanceAlpha,
webgl::FLOAT,
gl::LUMINANCE_ALPHA32F_ARB,
TexFormat::LuminanceAlpha32f,
);
}

View file

@ -2,14 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use super::{
constants as webgl, ext_constants as gl, WebGLExtension, WebGLExtensionSpec, WebGLExtensions,
};
use super::{WebGLExtension, WebGLExtensionSpec, WebGLExtensions};
use crate::dom::bindings::codegen::Bindings::OESTextureHalfFloatBinding::OESTextureHalfFloatConstants;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::webglrenderingcontext::WebGLRenderingContext;
use canvas_traits::webgl::WebGLVersion;
use canvas_traits::webgl::{TexFormat, WebGLVersion};
use dom_struct::dom_struct;
#[dom_struct]
@ -50,14 +48,14 @@ impl WebGLExtension for OESTextureHalfFloat {
fn enable(ext: &WebGLExtensions) {
let hf = OESTextureHalfFloatConstants::HALF_FLOAT_OES;
ext.enable_tex_type(hf);
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(TexFormat::RGBA, hf, TexFormat::RGBA16f);
ext.add_effective_tex_internal_format(TexFormat::RGB, hf, TexFormat::RGB16f);
ext.add_effective_tex_internal_format(TexFormat::Luminance, hf, TexFormat::Luminance16f);
ext.add_effective_tex_internal_format(TexFormat::Alpha, hf, TexFormat::Alpha16f);
ext.add_effective_tex_internal_format(
webgl::LUMINANCE_ALPHA,
TexFormat::LuminanceAlpha,
hf,
gl::LUMINANCE_ALPHA16F_ARB,
TexFormat::LuminanceAlpha16f,
);
}

View file

@ -18,7 +18,7 @@ use crate::dom::oestexturehalffloat::OESTextureHalfFloat;
use crate::dom::webglcolorbufferfloat::WEBGLColorBufferFloat;
use crate::dom::webglrenderingcontext::WebGLRenderingContext;
use crate::dom::webgltexture::TexCompression;
use canvas_traits::webgl::{GlType, WebGLVersion};
use canvas_traits::webgl::{GlType, TexFormat, WebGLVersion};
use fnv::{FnvHashMap, FnvHashSet};
use js::jsapi::JSObject;
use malloc_size_of::MallocSizeOf;
@ -82,7 +82,7 @@ struct WebGLExtensionFeatures {
gl_extensions: FnvHashSet<String>,
disabled_tex_types: FnvHashSet<GLenum>,
not_filterable_tex_types: FnvHashSet<GLenum>,
effective_tex_internal_formats: FnvHashMap<TexFormatType, u32>,
effective_tex_internal_formats: FnvHashMap<TexFormatType, TexFormat>,
/// WebGL Hint() targets enabled by extensions.
hint_targets: FnvHashSet<GLenum>,
/// WebGL GetParameter() names enabled by extensions.
@ -273,9 +273,9 @@ impl WebGLExtensions {
pub fn add_effective_tex_internal_format(
&self,
source_internal_format: u32,
source_internal_format: TexFormat,
source_data_type: u32,
effective_internal_format: u32,
effective_internal_format: TexFormat,
) {
let format = TexFormatType(source_internal_format, source_data_type);
self.features
@ -286,9 +286,9 @@ impl WebGLExtensions {
pub fn get_effective_tex_internal_format(
&self,
source_internal_format: u32,
source_internal_format: TexFormat,
source_data_type: u32,
) -> u32 {
) -> TexFormat {
let format = TexFormatType(source_internal_format, source_data_type);
*(self
.features
@ -453,4 +453,4 @@ impl WebGLExtensions {
// Helper structs
#[derive(Eq, Hash, JSTraceable, MallocSizeOf, PartialEq)]
struct TexFormatType(u32, u32);
struct TexFormatType(TexFormat, u32);

View file

@ -7,20 +7,6 @@ mod extension;
mod extensions;
mod wrapper;
// Some extra constants not exposed in WebGLRenderingContext constants
pub mod ext_constants {
pub const ALPHA16F_ARB: u32 = 0x881C;
pub const ALPHA32F_ARB: u32 = 0x8816;
pub const LUMINANCE16F_ARB: u32 = 0x881E;
pub const LUMINANCE32F_ARB: u32 = 0x8818;
pub const LUMINANCE_ALPHA16F_ARB: u32 = 0x881F;
pub const LUMINANCE_ALPHA32F_ARB: u32 = 0x8819;
pub const RGBA16F: u32 = 0x881A;
pub const RGB16F: u32 = 0x881B;
pub const RGBA32F: u32 = 0x8814;
pub const RGB32F: u32 = 0x8815;
}
pub use self::extension::WebGLExtension;
pub use self::extension::WebGLExtensionSpec;
pub use self::extensions::WebGLExtensions;

View file

@ -790,6 +790,10 @@ impl WebGLRenderingContext {
YAxisTreatment::AsIs
};
let internal_format = self
.extension_manager
.get_effective_tex_internal_format(internal_format, data_type.as_gl_constant());
let effective_data_type = self
.extension_manager
.effective_type(data_type.as_gl_constant());

View file

@ -8,7 +8,7 @@
[WebGL test #2: getError expected: NO_ERROR. Was INVALID_OPERATION : texParameter(TEXTURE_MIN_FILTER) should succeed]
expected: FAIL
[WebGL test #4: getError expected: NO_ERROR. Was INVALID_OPERATION : texStorage3D should succeed]
[WebGL test #4: getError expected: NO_ERROR. Was INVALID_ENUM : texStorage3D should succeed]
expected: FAIL
[WebGL test #1: getError expected: NO_ERROR. Was INVALID_OPERATION : texParameter(TEXTURE_MAG_FILTER) should succeed]

View file

@ -1,4 +1,5 @@
[tex-storage-2d.html]
disable: until texture initialization is implemented
[WebGL test #32: texture should sample as uninitialized texture after texStorage2D\nat (0, 0) expected: 0,0,0,255 was 255,84,64,255]
expected: FAIL