Browse Source

textium: texture utils

master
Erin 8 years ago
parent
commit
b7b7c5ec18
1 changed files with 42 additions and 0 deletions
  1. +42
    -0
      textium/src/texture.rs

+ 42
- 0
textium/src/texture.rs View File

@ -0,0 +1,42 @@
use std::borrow::Cow;
use glium::texture::{RawImage2d, Texture2dDataSource};
pub use glium::texture::PixelValue;
pub struct Texture2dData<T> where T: Clone {
pub data: Vec<T>,
pub width: u32,
pub height: u32,
}
impl<'a, P> Texture2dDataSource<'a> for &'a Texture2dData<P>
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: <P as PixelValue>::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
}

Loading…
Cancel
Save