2022-11-20 14:03:58 +11:00
|
|
|
use crate::framebuffer::Framebuffer;
|
2022-11-20 15:16:57 +11:00
|
|
|
use gl::types::{GLenum, GLuint};
|
|
|
|
use librashader::{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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2022-11-20 14:03:58 +11:00
|
|
|
pub struct Viewport<'a> {
|
2022-11-14 17:49:51 +11:00
|
|
|
pub x: i32,
|
|
|
|
pub y: i32,
|
2022-11-20 14:03:58 +11:00
|
|
|
pub output: &'a Framebuffer,
|
2022-11-20 15:16:57 +11:00
|
|
|
pub mvp: Option<&'a [f32]>,
|
2022-11-14 17:49:51 +11:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:55:49 +11:00
|
|
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
2022-11-14 17:49:51 +11:00
|
|
|
pub struct Size {
|
|
|
|
pub width: u32,
|
|
|
|
pub height: u32,
|
|
|
|
}
|
|
|
|
|
2022-11-20 18:09:05 +11:00
|
|
|
#[derive(Default, Debug, Copy, Clone)]
|
2022-11-17 17:21:29 +11:00
|
|
|
pub struct GlImage {
|
2022-11-14 17:49:51 +11:00
|
|
|
pub handle: GLuint,
|
|
|
|
pub format: GLenum,
|
|
|
|
pub size: Size,
|
2022-11-20 15:16:57 +11:00
|
|
|
pub padded_size: Size,
|
2022-11-14 17:49:51 +11:00
|
|
|
}
|
|
|
|
|
2022-11-20 15:16:57 +11:00
|
|
|
impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
|
2022-11-14 17:49:51 +11:00
|
|
|
pub fn current(&self) -> &T {
|
|
|
|
&self.items[self.index]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next(&mut self) {
|
|
|
|
self.index += 1;
|
|
|
|
if self.index >= SIZE {
|
|
|
|
self.index = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RingBuffer<T, const SIZE: usize> {
|
|
|
|
items: [T; SIZE],
|
2022-11-20 15:16:57 +11:00
|
|
|
index: usize,
|
2022-11-14 17:49:51 +11:00
|
|
|
}
|
|
|
|
|
2022-11-20 15:16:57 +11:00
|
|
|
impl<T, const SIZE: usize> RingBuffer<T, SIZE>
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 14:09:01 +11:00
|
|
|
|
|
|
|
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(),
|
|
|
|
);
|
2022-11-20 14:09:01 +11:00
|
|
|
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
|
|
|
|
}
|