From 0b336ca8c5454d5cc598ea7329200e5a29f0fd73 Mon Sep 17 00:00:00 2001 From: chyyran Date: Sun, 20 Nov 2022 02:23:10 -0500 Subject: [PATCH] gl: fix vec4 buffer length --- librashader-runtime-gl/src/filter_chain.rs | 4 +- librashader-runtime-gl/src/filter_pass.rs | 16 +++--- librashader-runtime-gl/src/lib.rs | 2 +- librashader-runtime-gl/src/util.rs | 67 ++++++++++++++++++++-- 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/librashader-runtime-gl/src/filter_chain.rs b/librashader-runtime-gl/src/filter_chain.rs index 2fa2730..65641d6 100644 --- a/librashader-runtime-gl/src/filter_chain.rs +++ b/librashader-runtime-gl/src/filter_chain.rs @@ -3,7 +3,7 @@ use crate::filter_pass::FilterPass; use crate::framebuffer::Framebuffer; use crate::render_target::RenderTarget; 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 librashader::image::Image; use librashader::{FilterMode, ShaderSource}; @@ -373,7 +373,7 @@ impl FilterChain { let ubo_ring = if let Some(ubo) = &reflection.ubo { let size = ubo.size; - let mut ring: RingBuffer = RingBuffer::new(); + let mut ring: InlineRingBuffer = InlineRingBuffer::new(); unsafe { gl::GenBuffers(16, ring.items_mut().as_mut_ptr()); for buffer in ring.items() { diff --git a/librashader-runtime-gl/src/filter_pass.rs b/librashader-runtime-gl/src/filter_pass.rs index f440249..25b0bcd 100644 --- a/librashader-runtime-gl/src/filter_pass.rs +++ b/librashader-runtime-gl/src/filter_pass.rs @@ -13,14 +13,14 @@ use rustc_hash::FxHashMap; use crate::binding::{UniformBinding, UniformLocation, VariableLocation}; use crate::filter_chain::FilterCommon; use crate::render_target::RenderTarget; -use crate::util::{RingBuffer, Size, Texture, Viewport}; +use crate::util::{InlineRingBuffer, RingBuffer, Size, Texture, Viewport}; pub struct FilterPass { pub reflection: ShaderReflection, pub compiled: ShaderCompilerOutput, pub program: GLuint, pub ubo_location: UniformLocation, - pub ubo_ring: Option>, + pub ubo_ring: Option>, pub uniform_buffer: Box<[u8]>, pub push_buffer: Box<[u8]>, pub variable_bindings: FxHashMap, @@ -290,7 +290,7 @@ impl FilterPass { 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 @@ -304,7 +304,7 @@ impl FilterPass { }; FilterPass::build_vec4( location.location(), - &mut buffer[offset..][..4], + &mut buffer[offset..][..16], viewport.output.size, ) } @@ -359,7 +359,7 @@ impl FilterPass { }; FilterPass::build_vec4( location.location(), - &mut buffer[offset..][..4], + &mut buffer[offset..][..16], original.image.size, ); } @@ -386,7 +386,7 @@ impl FilterPass { }; FilterPass::build_vec4( location.location(), - &mut buffer[offset..][..4], + &mut buffer[offset..][..16], source.image.size, ); } @@ -411,7 +411,7 @@ impl FilterPass { }; FilterPass::build_vec4( location.location(), - &mut buffer[offset..][..4], + &mut buffer[offset..][..16], output.image.size, ); } @@ -488,7 +488,7 @@ impl FilterPass { }; FilterPass::build_vec4( location.location(), - &mut buffer[offset..][..4], + &mut buffer[offset..][..16], lut.image.size, ); } diff --git a/librashader-runtime-gl/src/lib.rs b/librashader-runtime-gl/src/lib.rs index f5be735..12092d3 100644 --- a/librashader-runtime-gl/src/lib.rs +++ b/librashader-runtime-gl/src/lib.rs @@ -17,7 +17,7 @@ mod tests { #[test] fn triangle() { 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(); diff --git a/librashader-runtime-gl/src/util.rs b/librashader-runtime-gl/src/util.rs index 75400fb..2f79b6e 100644 --- a/librashader-runtime-gl/src/util.rs +++ b/librashader-runtime-gl/src/util.rs @@ -43,12 +43,22 @@ pub struct GlImage { pub padded_size: Size, } -impl RingBuffer { - pub fn current(&self) -> &T { +pub trait RingBuffer { + fn current(&self) -> &T; + fn current_mut(&mut self) -> &mut T; + fn next(&mut self); +} + +impl RingBuffer for InlineRingBuffer { + fn current(&self) -> &T { &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; if self.index >= SIZE { self.index = 0 @@ -56,12 +66,12 @@ impl RingBuffer { } } -pub struct RingBuffer { +pub struct InlineRingBuffer { items: [T; SIZE], index: usize, } -impl RingBuffer +impl InlineRingBuffer where T: Copy, T: Default, @@ -82,6 +92,53 @@ where } } + +pub struct AllocRingBuffer { + items: Box<[T]>, + index: usize +} + +impl AllocRingBuffer + 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 RingBuffer for AllocRingBuffer { + 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 { let shader = gl::CreateShader(stage); gl::ShaderSource(