2022-11-20 15:16:57 +11:00
|
|
|
use gl::types::{GLenum, GLuint};
|
2022-11-28 15:27:21 +11:00
|
|
|
use librashader_common::Size;
|
2022-11-25 12:00:55 +11:00
|
|
|
use librashader_reflect::back::cross::GlVersion;
|
2022-11-14 17:49:51 +11:00
|
|
|
|
2022-11-24 18:08:34 +11:00
|
|
|
pub fn calc_miplevel(size: Size<u32>) -> u32 {
|
|
|
|
let mut size = std::cmp::max(size.width, size.height);
|
2022-11-14 17:49:51 +11:00
|
|
|
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: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
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
2022-11-25 12:00:55 +11:00
|
|
|
|
|
|
|
pub fn gl_get_version() -> GlVersion {
|
|
|
|
let mut maj_ver = 0;
|
|
|
|
let mut min_ver = 0;
|
|
|
|
unsafe {
|
|
|
|
gl::GetIntegerv(gl::MAJOR_VERSION, &mut maj_ver);
|
|
|
|
gl::GetIntegerv(gl::MINOR_VERSION, &mut min_ver);
|
|
|
|
}
|
|
|
|
|
|
|
|
match maj_ver {
|
|
|
|
3 => match min_ver {
|
|
|
|
3 => GlVersion::V3_30,
|
|
|
|
2 => GlVersion::V1_50,
|
|
|
|
1 => GlVersion::V1_40,
|
|
|
|
0 => GlVersion::V1_30,
|
|
|
|
_ => GlVersion::V1_50,
|
|
|
|
}
|
|
|
|
4 => match min_ver {
|
|
|
|
6 => GlVersion::V4_60,
|
|
|
|
5 => GlVersion::V4_50,
|
|
|
|
4 => GlVersion::V4_40,
|
|
|
|
3 => GlVersion::V4_30,
|
|
|
|
2 => GlVersion::V4_20,
|
|
|
|
1 => GlVersion::V4_10,
|
|
|
|
0 => GlVersion::V4_00,
|
|
|
|
_ => GlVersion::V1_50
|
|
|
|
}
|
|
|
|
_ => GlVersion::V1_50
|
|
|
|
}
|
|
|
|
|
2022-11-28 15:27:21 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gl_u16_to_version(version: u16) -> GlVersion {
|
|
|
|
match version {
|
|
|
|
300 => GlVersion::V1_30,
|
|
|
|
310 => GlVersion::V1_40,
|
|
|
|
320 => GlVersion::V1_50,
|
|
|
|
330 => GlVersion::V3_30,
|
|
|
|
400 => GlVersion::V4_00,
|
|
|
|
410 => GlVersion::V4_10,
|
|
|
|
420 => GlVersion::V4_20,
|
|
|
|
430 => GlVersion::V4_30,
|
|
|
|
440 => GlVersion::V4_40,
|
|
|
|
450 => GlVersion::V4_50,
|
|
|
|
460 => GlVersion::V4_60,
|
|
|
|
_ => GlVersion::V1_50
|
|
|
|
}
|
2022-11-25 12:00:55 +11:00
|
|
|
}
|