Add RGBA32F support

This commit is contained in:
Patrick Walton 2019-12-14 13:57:55 -08:00
parent 521ab3b5ba
commit 8dabd0a7ea
3 changed files with 35 additions and 5 deletions

View File

@ -192,6 +192,16 @@ impl RectI {
Vector2I(self.0.zw()) Vector2I(self.0.zw())
} }
#[inline]
pub fn scale(self, factor: i32) -> RectI {
RectI(self.0 * I32x4::splat(factor))
}
#[inline]
pub fn scale_xy(self, factors: Vector2I) -> RectI {
RectI(self.0 * factors.0.concat_xy_xy(factors.0))
}
#[inline] #[inline]
pub fn min_x(self) -> i32 { pub fn min_x(self) -> i32 {
self.0[0] self.0[0]

View File

@ -470,17 +470,18 @@ impl Device for GLDevice {
} }
fn upload_to_texture(&self, texture: &Self::Texture, size: Vector2I, data: &[u8]) { fn upload_to_texture(&self, texture: &Self::Texture, size: Vector2I, data: &[u8]) {
// FIXME(pcwalton): Fix size issues!!
assert!(data.len() >= size.x() as usize * size.y() as usize * 4); assert!(data.len() >= size.x() as usize * size.y() as usize * 4);
unsafe { unsafe {
self.bind_texture(texture, 0); self.bind_texture(texture, 0);
gl::TexImage2D(gl::TEXTURE_2D, gl::TexImage2D(gl::TEXTURE_2D,
0, 0,
gl::RGBA as GLint, texture.format.gl_internal_format(),
size.x() as GLsizei, size.x() as GLsizei,
size.y() as GLsizei, size.y() as GLsizei,
0, 0,
gl::RGBA, texture.format.gl_format(),
gl::UNSIGNED_BYTE, texture.format.gl_type(),
data.as_ptr() as *const GLvoid); ck(); data.as_ptr() as *const GLvoid); ck();
} }
@ -522,6 +523,21 @@ impl Device for GLDevice {
flip_y(&mut pixels, size, 1); flip_y(&mut pixels, size, 1);
TextureData::U16(pixels) TextureData::U16(pixels)
} }
TextureFormat::RGBA32F => {
let channels = format.channels();
let mut pixels = vec![0.0; size.x() as usize * size.y() as usize * channels];
unsafe {
gl::ReadPixels(origin.x(),
origin.y(),
size.x() as GLsizei,
size.y() as GLsizei,
format.gl_format(),
format.gl_type(),
pixels.as_mut_ptr() as *mut GLvoid); ck();
}
flip_y(&mut pixels, size, channels);
TextureData::F32(pixels)
}
} }
} }
@ -955,13 +971,14 @@ impl TextureFormatExt for TextureFormat {
TextureFormat::R8 => gl::R8 as GLint, TextureFormat::R8 => gl::R8 as GLint,
TextureFormat::R16F => gl::R16F as GLint, TextureFormat::R16F => gl::R16F as GLint,
TextureFormat::RGBA8 => gl::RGBA as GLint, TextureFormat::RGBA8 => gl::RGBA as GLint,
TextureFormat::RGBA32F => gl::RGBA32F as GLint,
} }
} }
fn gl_format(self) -> GLuint { fn gl_format(self) -> GLuint {
match self { match self {
TextureFormat::R8 | TextureFormat::R16F => gl::RED, TextureFormat::R8 | TextureFormat::R16F => gl::RED,
TextureFormat::RGBA8 => gl::RGBA, TextureFormat::RGBA8 | TextureFormat::RGBA32F => gl::RGBA,
} }
} }
@ -969,6 +986,7 @@ impl TextureFormatExt for TextureFormat {
match self { match self {
TextureFormat::R8 | TextureFormat::RGBA8 => gl::UNSIGNED_BYTE, TextureFormat::R8 | TextureFormat::RGBA8 => gl::UNSIGNED_BYTE,
TextureFormat::R16F => gl::HALF_FLOAT, TextureFormat::R16F => gl::HALF_FLOAT,
TextureFormat::RGBA32F => gl::FLOAT,
} }
} }
} }

View File

@ -114,6 +114,7 @@ pub enum TextureFormat {
R8, R8,
R16F, R16F,
RGBA8, RGBA8,
RGBA32F,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -284,6 +285,7 @@ impl Default for StencilFunc {
pub enum TextureData { pub enum TextureData {
U8(Vec<u8>), U8(Vec<u8>),
U16(Vec<u16>), U16(Vec<u16>),
F32(Vec<f32>),
} }
impl UniformData { impl UniformData {
@ -316,7 +318,7 @@ impl TextureFormat {
pub fn channels(self) -> usize { pub fn channels(self) -> usize {
match self { match self {
TextureFormat::R8 | TextureFormat::R16F => 1, TextureFormat::R8 | TextureFormat::R16F => 1,
TextureFormat::RGBA8 => 4, TextureFormat::RGBA8 | TextureFormat::RGBA32F => 4,
} }
} }
} }