From 8dabd0a7eadc54b7dd4e0652429b0b4b6450a038 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sat, 14 Dec 2019 13:57:55 -0800 Subject: [PATCH] Add RGBA32F support --- geometry/src/rect.rs | 10 ++++++++++ gl/src/lib.rs | 26 ++++++++++++++++++++++---- gpu/src/lib.rs | 4 +++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/geometry/src/rect.rs b/geometry/src/rect.rs index 58c59cd0..993ef3fc 100644 --- a/geometry/src/rect.rs +++ b/geometry/src/rect.rs @@ -192,6 +192,16 @@ impl RectI { 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] pub fn min_x(self) -> i32 { self.0[0] diff --git a/gl/src/lib.rs b/gl/src/lib.rs index 2bb8de93..6bb4a7c2 100644 --- a/gl/src/lib.rs +++ b/gl/src/lib.rs @@ -470,17 +470,18 @@ impl Device for GLDevice { } 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); unsafe { self.bind_texture(texture, 0); gl::TexImage2D(gl::TEXTURE_2D, 0, - gl::RGBA as GLint, + texture.format.gl_internal_format(), size.x() as GLsizei, size.y() as GLsizei, 0, - gl::RGBA, - gl::UNSIGNED_BYTE, + texture.format.gl_format(), + texture.format.gl_type(), data.as_ptr() as *const GLvoid); ck(); } @@ -522,6 +523,21 @@ impl Device for GLDevice { flip_y(&mut pixels, size, 1); 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::R16F => gl::R16F as GLint, TextureFormat::RGBA8 => gl::RGBA as GLint, + TextureFormat::RGBA32F => gl::RGBA32F as GLint, } } fn gl_format(self) -> GLuint { match self { 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 { TextureFormat::R8 | TextureFormat::RGBA8 => gl::UNSIGNED_BYTE, TextureFormat::R16F => gl::HALF_FLOAT, + TextureFormat::RGBA32F => gl::FLOAT, } } } diff --git a/gpu/src/lib.rs b/gpu/src/lib.rs index eaaa0dc7..1fbe389d 100644 --- a/gpu/src/lib.rs +++ b/gpu/src/lib.rs @@ -114,6 +114,7 @@ pub enum TextureFormat { R8, R16F, RGBA8, + RGBA32F, } #[derive(Clone, Copy, Debug)] @@ -284,6 +285,7 @@ impl Default for StencilFunc { pub enum TextureData { U8(Vec), U16(Vec), + F32(Vec), } impl UniformData { @@ -316,7 +318,7 @@ impl TextureFormat { pub fn channels(self) -> usize { match self { TextureFormat::R8 | TextureFormat::R16F => 1, - TextureFormat::RGBA8 => 4, + TextureFormat::RGBA8 | TextureFormat::RGBA32F => 4, } } }