gl: fix vec4 buffer length

This commit is contained in:
chyyran 2022-11-20 02:23:10 -05:00
parent 1e9d180bf0
commit 0b336ca8c5
4 changed files with 73 additions and 16 deletions

View file

@ -3,7 +3,7 @@ use crate::filter_pass::FilterPass;
use crate::framebuffer::Framebuffer; use crate::framebuffer::Framebuffer;
use crate::render_target::RenderTarget; use crate::render_target::RenderTarget;
use crate::util; use crate::util;
use crate::util::{GlImage, RingBuffer, Size, Texture, Viewport}; use crate::util::{GlImage, InlineRingBuffer, Size, Texture, Viewport};
use gl::types::{GLenum, GLint, GLsizei, GLsizeiptr, GLuint}; use gl::types::{GLenum, GLint, GLsizei, GLsizeiptr, GLuint};
use librashader::image::Image; use librashader::image::Image;
use librashader::{FilterMode, ShaderSource}; use librashader::{FilterMode, ShaderSource};
@ -373,7 +373,7 @@ impl FilterChain {
let ubo_ring = if let Some(ubo) = &reflection.ubo { let ubo_ring = if let Some(ubo) = &reflection.ubo {
let size = ubo.size; let size = ubo.size;
let mut ring: RingBuffer<GLuint, 16> = RingBuffer::new(); let mut ring: InlineRingBuffer<GLuint, 16> = InlineRingBuffer::new();
unsafe { unsafe {
gl::GenBuffers(16, ring.items_mut().as_mut_ptr()); gl::GenBuffers(16, ring.items_mut().as_mut_ptr());
for buffer in ring.items() { for buffer in ring.items() {

View file

@ -13,14 +13,14 @@ use rustc_hash::FxHashMap;
use crate::binding::{UniformBinding, UniformLocation, VariableLocation}; use crate::binding::{UniformBinding, UniformLocation, VariableLocation};
use crate::filter_chain::FilterCommon; use crate::filter_chain::FilterCommon;
use crate::render_target::RenderTarget; use crate::render_target::RenderTarget;
use crate::util::{RingBuffer, Size, Texture, Viewport}; use crate::util::{InlineRingBuffer, RingBuffer, Size, Texture, Viewport};
pub struct FilterPass { pub struct FilterPass {
pub reflection: ShaderReflection, pub reflection: ShaderReflection,
pub compiled: ShaderCompilerOutput<String, GlslangGlslContext>, pub compiled: ShaderCompilerOutput<String, GlslangGlslContext>,
pub program: GLuint, pub program: GLuint,
pub ubo_location: UniformLocation<GLuint>, pub ubo_location: UniformLocation<GLuint>,
pub ubo_ring: Option<RingBuffer<GLuint, 16>>, pub ubo_ring: Option<InlineRingBuffer<GLuint, 16>>,
pub uniform_buffer: Box<[u8]>, pub uniform_buffer: Box<[u8]>,
pub push_buffer: Box<[u8]>, pub push_buffer: Box<[u8]>,
pub variable_bindings: FxHashMap<UniformBinding, (VariableLocation, MemberOffset)>, pub variable_bindings: FxHashMap<UniformBinding, (VariableLocation, MemberOffset)>,
@ -290,7 +290,7 @@ impl FilterPass {
MemberOffset::PushConstant(offset) => (&mut self.push_buffer, *offset), MemberOffset::PushConstant(offset) => (&mut self.push_buffer, *offset),
}; };
FilterPass::build_vec4(location.location(), &mut buffer[offset..][..4], fb_size) FilterPass::build_vec4(location.location(), &mut buffer[offset..][..16], fb_size)
} }
// bind FinalViewportSize // bind FinalViewportSize
@ -304,7 +304,7 @@ impl FilterPass {
}; };
FilterPass::build_vec4( FilterPass::build_vec4(
location.location(), location.location(),
&mut buffer[offset..][..4], &mut buffer[offset..][..16],
viewport.output.size, viewport.output.size,
) )
} }
@ -359,7 +359,7 @@ impl FilterPass {
}; };
FilterPass::build_vec4( FilterPass::build_vec4(
location.location(), location.location(),
&mut buffer[offset..][..4], &mut buffer[offset..][..16],
original.image.size, original.image.size,
); );
} }
@ -386,7 +386,7 @@ impl FilterPass {
}; };
FilterPass::build_vec4( FilterPass::build_vec4(
location.location(), location.location(),
&mut buffer[offset..][..4], &mut buffer[offset..][..16],
source.image.size, source.image.size,
); );
} }
@ -411,7 +411,7 @@ impl FilterPass {
}; };
FilterPass::build_vec4( FilterPass::build_vec4(
location.location(), location.location(),
&mut buffer[offset..][..4], &mut buffer[offset..][..16],
output.image.size, output.image.size,
); );
} }
@ -488,7 +488,7 @@ impl FilterPass {
}; };
FilterPass::build_vec4( FilterPass::build_vec4(
location.location(), location.location(),
&mut buffer[offset..][..4], &mut buffer[offset..][..16],
lut.image.size, lut.image.size,
); );
} }

View file

@ -17,7 +17,7 @@ mod tests {
#[test] #[test]
fn triangle() { fn triangle() {
let (glfw, window, events, shader, vao) = hello_triangle::setup(); let (glfw, window, events, shader, vao) = hello_triangle::setup();
let mut filter = FilterChain::load("../test/basic.slangp").unwrap(); let mut filter = FilterChain::load("../test/slang-shaders/crt/crt-geom.slangp").unwrap();
// FilterChain::load("../test/slang-shaders/crt/crt-royale.slangp").unwrap(); // FilterChain::load("../test/slang-shaders/crt/crt-royale.slangp").unwrap();

View file

@ -43,12 +43,22 @@ pub struct GlImage {
pub padded_size: Size, pub padded_size: Size,
} }
impl<T, const SIZE: usize> RingBuffer<T, SIZE> { pub trait RingBuffer<T> {
pub fn current(&self) -> &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 {
&self.items[self.index] &self.items[self.index]
} }
pub fn next(&mut self) { fn current_mut(&mut self) -> &mut T {
&mut self.items[self.index]
}
fn next(&mut self) {
self.index += 1; self.index += 1;
if self.index >= SIZE { if self.index >= SIZE {
self.index = 0 self.index = 0
@ -56,12 +66,12 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
} }
} }
pub struct RingBuffer<T, const SIZE: usize> { pub struct InlineRingBuffer<T, const SIZE: usize> {
items: [T; SIZE], items: [T; SIZE],
index: usize, index: usize,
} }
impl<T, const SIZE: usize> RingBuffer<T, SIZE> impl<T, const SIZE: usize> InlineRingBuffer<T, SIZE>
where where
T: Copy, T: Copy,
T: Default, T: Default,
@ -82,6 +92,53 @@ where
} }
} }
pub struct AllocRingBuffer<T> {
items: Box<[T]>,
index: usize
}
impl<T> AllocRingBuffer<T>
where
T: Default,
{
pub fn new(len: usize) -> Self {
let mut items = Vec::new();
items.resize_with(len, T::default);
Self {
items: items.into_boxed_slice(),
index: 0,
}
}
pub fn items(&self) -> &[T] {
&self.items
}
pub fn items_mut(&mut self) -> &mut [T] {
&mut self.items
}
}
impl <T> RingBuffer<T> for AllocRingBuffer<T> {
fn current(&self) -> &T {
&self.items[self.index]
}
fn current_mut(&mut self) -> &mut T {
&mut self.items[self.index]
}
fn next(&mut self) {
self.index += 1;
if self.index >= self.items.len() {
self.index = 0
}
}
}
pub unsafe fn gl_compile_shader(stage: GLenum, source: &str) -> GLuint { pub unsafe fn gl_compile_shader(stage: GLenum, source: &str) -> GLuint {
let shader = gl::CreateShader(stage); let shader = gl::CreateShader(stage);
gl::ShaderSource( gl::ShaderSource(