librashader/librashader-runtime-gl/src/util.rs

90 lines
1.9 KiB
Rust
Raw Normal View History

2022-11-22 08:21:50 +11:00
use crate::framebuffer::GlImage;
2022-11-20 15:16:57 +11:00
use gl::types::{GLenum, GLuint};
use librashader_common::{FilterMode, WrapMode};
2022-11-14 17:49:51 +11:00
pub fn calc_miplevel(width: u32, height: u32) -> u32 {
let mut size = std::cmp::max(width, height);
let mut levels = 0;
while size != 0 {
levels += 1;
size >>= 1;
}
2022-11-20 15:16:57 +11:00
levels
2022-11-14 17:49:51 +11:00
}
2022-11-20 18:09:05 +11:00
#[derive(Default, Debug, Copy, Clone)]
2022-11-17 17:21:29 +11:00
pub struct Texture {
pub image: GlImage,
2022-11-17 16:08:11 +11:00
pub filter: FilterMode,
pub mip_filter: FilterMode,
2022-11-20 15:16:57 +11:00
pub wrap_mode: WrapMode,
2022-11-14 17:49:51 +11:00
}
2022-11-20 18:23:10 +11:00
pub trait RingBuffer<T> {
fn current(&self) -> &T;
fn current_mut(&mut self) -> &mut T;
fn next(&mut self);
}
impl<T, const SIZE: usize> RingBuffer<T> for InlineRingBuffer<T, SIZE> {
fn current(&self) -> &T {
2022-11-14 17:49:51 +11:00
&self.items[self.index]
}
2022-11-20 18:23:10 +11:00
fn current_mut(&mut self) -> &mut T {
&mut self.items[self.index]
}
fn next(&mut self) {
2022-11-14 17:49:51 +11:00
self.index += 1;
if self.index >= SIZE {
self.index = 0
}
}
}
2022-11-20 18:23:10 +11:00
pub struct InlineRingBuffer<T, const SIZE: usize> {
2022-11-14 17:49:51 +11:00
items: [T; SIZE],
2022-11-20 15:16:57 +11:00
index: usize,
2022-11-14 17:49:51 +11:00
}
2022-11-20 18:23:10 +11:00
impl<T, const SIZE: usize> InlineRingBuffer<T, SIZE>
2022-11-20 15:16:57 +11:00
where
T: Copy,
T: Default,
2022-11-14 17:49:51 +11:00
{
pub fn new() -> Self {
Self {
items: [T::default(); SIZE],
2022-11-20 15:16:57 +11:00
index: 0,
2022-11-14 17:49:51 +11:00
}
}
pub fn items(&self) -> &[T; SIZE] {
&self.items
}
pub fn items_mut(&mut self) -> &mut [T; SIZE] {
&mut self.items
}
}
pub unsafe fn gl_compile_shader(stage: GLenum, source: &str) -> GLuint {
let shader = gl::CreateShader(stage);
2022-11-20 15:16:57 +11:00
gl::ShaderSource(
shader,
1,
&source.as_bytes().as_ptr().cast(),
std::ptr::null(),
);
gl::CompileShader(shader);
let mut compile_status = 0;
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut compile_status);
if compile_status == 0 {
panic!("failed to compile")
}
shader
}