From b7b7c5ec18eb93f6ffbe6bee449f646a3a54ec11 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 15 Aug 2017 12:16:15 -0500 Subject: [PATCH] textium: texture utils --- textium/src/texture.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 textium/src/texture.rs diff --git a/textium/src/texture.rs b/textium/src/texture.rs new file mode 100644 index 0000000..830e1e7 --- /dev/null +++ b/textium/src/texture.rs @@ -0,0 +1,42 @@ +use std::borrow::Cow; + +use glium::texture::{RawImage2d, Texture2dDataSource}; +pub use glium::texture::PixelValue; + + +pub struct Texture2dData where T: Clone { + pub data: Vec, + pub width: u32, + pub height: u32, +} + +impl<'a, P> Texture2dDataSource<'a> for &'a Texture2dData

+ where P: Clone + PixelValue +{ + type Data = P; + + fn into_raw(self) -> RawImage2d<'a, P> { + RawImage2d { + data: Cow::Borrowed(&self.data), + width: self.width, + height: self.height, + format:

::get_format(), + } + } +} + +pub fn get_nearest_pow2(mut x: u32) -> u32 { + // A nice, fast bit hack. + // Taken from https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + + assert!(x > 0); + + x -= 1; + x = x | (x >> 1); + x = x | (x >> 2); + x = x | (x >> 4); + x = x | (x >> 8); + x = x | (x >> 16); + + x + 1 +}