From cbfe76928eb922f4d8066a26d3d2ad79c9faece6 Mon Sep 17 00:00:00 2001 From: chyyran Date: Wed, 30 Nov 2022 19:05:24 -0500 Subject: [PATCH] gl: overall cleanup --- librashader-runtime-gl/src/filter_chain.rs | 31 ++++++++++--------- librashader-runtime-gl/src/filter_pass.rs | 3 +- librashader-runtime-gl/src/framebuffer.rs | 10 ------ librashader-runtime-gl/src/gl/framebuffer.rs | 4 +-- .../src/gl/gl3/framebuffer.rs | 8 ++--- .../src/gl/gl3/hello_triangle.rs | 9 +++--- .../src/gl/gl46/framebuffer.rs | 11 +++---- .../src/gl/gl46/hello_triangle.rs | 9 +++--- librashader-runtime-gl/src/gl/mod.rs | 5 +-- librashader-runtime-gl/src/lib.rs | 30 +++++++++++------- librashader-runtime-gl/src/options.rs | 2 +- librashader-runtime-gl/src/render_target.rs | 4 +-- librashader-runtime-gl/src/viewport.rs | 9 ++++++ 13 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 librashader-runtime-gl/src/viewport.rs diff --git a/librashader-runtime-gl/src/filter_chain.rs b/librashader-runtime-gl/src/filter_chain.rs index a9d75c8..1eb60bd 100644 --- a/librashader-runtime-gl/src/filter_chain.rs +++ b/librashader-runtime-gl/src/filter_chain.rs @@ -1,7 +1,7 @@ use crate::binding::{UniformLocation, VariableLocation}; use crate::error::{FilterChainError, Result}; use crate::filter_pass::FilterPass; -use crate::framebuffer::{GLImage, Viewport}; +use crate::framebuffer::GLImage; use crate::render_target::RenderTarget; use crate::util; use crate::util::{gl_get_version, gl_u16_to_version}; @@ -13,6 +13,7 @@ use crate::gl::{DrawQuad, Framebuffer, FramebufferInterface, GLInterface, LoadLu use crate::options::{FilterChainOptions, FrameOptions}; use crate::samplers::SamplerSet; use crate::texture::Texture; +use crate::viewport::Viewport; use librashader_common::{FilterMode, WrapMode}; use librashader_preprocess::ShaderSource; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; @@ -31,7 +32,7 @@ use std::collections::VecDeque; use std::path::Path; pub struct FilterChain { - filter: FilterChainInner + filter: FilterChainInner, } impl FilterChain { @@ -41,11 +42,13 @@ impl FilterChain { ) -> Result { if let Some(options) = options && options.use_dsa { return Ok(Self { - filter: FilterChainInner::DSA(FilterChainImpl::load_from_preset(preset, Some(options))?) + filter: FilterChainInner::DirectStateAccess(FilterChainImpl::load_from_preset(preset, Some(options))?) }) } - return Ok(Self { - filter: FilterChainInner::Compatibility(FilterChainImpl::load_from_preset(preset, options)?) + Ok(Self { + filter: FilterChainInner::Compatibility(FilterChainImpl::load_from_preset( + preset, options, + )?), }) } @@ -64,25 +67,23 @@ impl FilterChain { /// When this frame returns, GL_FRAMEBUFFER is bound to 0. pub fn frame( &mut self, - count: usize, - viewport: &Viewport, input: &GLImage, + viewport: &Viewport, + frame_count: usize, options: Option<&FrameOptions>, ) -> Result<()> { match &mut self.filter { - FilterChainInner::DSA(p) => { - p.frame(count, viewport, input, options) - } - FilterChainInner::Compatibility(p) => { - p.frame(count, viewport, input, options) + FilterChainInner::DirectStateAccess(p) => { + p.frame(frame_count, viewport, input, options) } + FilterChainInner::Compatibility(p) => p.frame(frame_count, viewport, input, options), } } } enum FilterChainInner { - DSA(FilterChainImpl), - Compatibility(FilterChainImpl) + DirectStateAccess(FilterChainImpl), + Compatibility(FilterChainImpl), } struct FilterChainImpl { @@ -595,7 +596,7 @@ impl FilterChainImpl { viewport, &original, &source, - RenderTarget::new(viewport.output, viewport.mvp, viewport.x, viewport.y), + viewport.into(), ); } diff --git a/librashader-runtime-gl/src/filter_pass.rs b/librashader-runtime-gl/src/filter_pass.rs index 89802c7..a981b90 100644 --- a/librashader-runtime-gl/src/filter_pass.rs +++ b/librashader-runtime-gl/src/filter_pass.rs @@ -13,9 +13,9 @@ use rustc_hash::FxHashMap; use crate::binding::{BufferStorage, UniformLocation, VariableLocation}; use crate::filter_chain::FilterCommon; -use crate::framebuffer::Viewport; use crate::gl::{BindTexture, FramebufferInterface, GLInterface, UboRing}; use crate::render_target::RenderTarget; +use crate::viewport::Viewport; use crate::texture::Texture; @@ -71,7 +71,6 @@ impl FilterPass { } unsafe { - // can't use framebuffer.clear because it will unbind. framebuffer.clear::(); let framebuffer_size = framebuffer.size; diff --git a/librashader-runtime-gl/src/framebuffer.rs b/librashader-runtime-gl/src/framebuffer.rs index 19f294e..eba007c 100644 --- a/librashader-runtime-gl/src/framebuffer.rs +++ b/librashader-runtime-gl/src/framebuffer.rs @@ -1,16 +1,6 @@ use gl::types::{GLenum, GLuint}; use librashader_common::Size; -use crate::gl::{Framebuffer, FramebufferInterface}; - -#[derive(Debug, Copy, Clone)] -pub struct Viewport<'a> { - pub x: i32, - pub y: i32, - pub output: &'a Framebuffer, - pub mvp: Option<&'a [f32; 16]>, -} - #[derive(Default, Debug, Copy, Clone)] pub struct GLImage { pub handle: GLuint, diff --git a/librashader-runtime-gl/src/gl/framebuffer.rs b/librashader-runtime-gl/src/gl/framebuffer.rs index eaadff0..42b2aa6 100644 --- a/librashader-runtime-gl/src/gl/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/framebuffer.rs @@ -2,7 +2,7 @@ use crate::error::Result; use crate::framebuffer::GLImage; use crate::gl::FramebufferInterface; use crate::texture::Texture; -use crate::Viewport; +use crate::viewport::Viewport; use gl::types::{GLenum, GLuint}; use librashader_common::{FilterMode, ImageFormat, Size, WrapMode}; use librashader_presets::Scale2D; @@ -34,7 +34,7 @@ impl Framebuffer { } pub fn clear(&self) { - T::clear::(&self) + T::clear::(self) } pub fn scale( diff --git a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs index 47a732a..9cd0194 100644 --- a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs @@ -1,10 +1,11 @@ use crate::error::{FilterChainError, Result}; -use crate::framebuffer::{GLImage, Viewport}; +use crate::framebuffer::GLImage; use crate::gl::framebuffer::Framebuffer; use crate::gl::FramebufferInterface; use crate::texture::Texture; +use crate::viewport::Viewport; use gl::types::{GLenum, GLint, GLsizei, GLuint}; -use librashader_common::{FilterMode, ImageFormat, Size, WrapMode}; +use librashader_common::{ImageFormat, Size}; use librashader_presets::Scale2D; #[derive(Debug)] @@ -96,8 +97,7 @@ impl FramebufferInterface for Gl3Framebuffer { fn copy_from(fb: &mut Framebuffer, image: &GLImage) -> Result<()> { // todo: may want to use a shader and draw a quad to be faster. if image.size != fb.size || image.format != fb.format { - Self::init( - fb,image.size, image.format)?; + Self::init(fb, image.size, image.format)?; } unsafe { diff --git a/librashader-runtime-gl/src/gl/gl3/hello_triangle.rs b/librashader-runtime-gl/src/gl/gl3/hello_triangle.rs index 50a0262..ae695ca 100644 --- a/librashader-runtime-gl/src/gl/gl3/hello_triangle.rs +++ b/librashader-runtime-gl/src/gl/gl3/hello_triangle.rs @@ -8,9 +8,10 @@ use gl::types::{GLchar, GLenum, GLint, GLsizei, GLuint}; use librashader_common::Size; use crate::filter_chain::FilterChain; -use crate::framebuffer::{GLImage, Viewport}; +use crate::framebuffer::GLImage; use crate::gl::gl3::CompatibilityGL; use crate::gl::{FramebufferInterface, GLInterface}; +use crate::viewport::Viewport; const WIDTH: u32 = 900; const HEIGHT: u32 = 700; @@ -503,8 +504,8 @@ void main() } let viewport = Viewport { - x: 0, - y: 0, + x: 0f32, + y: 0f32, output: &output, mvp: None, }; @@ -520,7 +521,7 @@ void main() }; filter - .frame(framecount, &viewport, &rendered, None) + .frame(&rendered, &viewport, framecount, None) .unwrap(); unsafe { diff --git a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs index 9e54e8c..443b618 100644 --- a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs @@ -1,10 +1,11 @@ use crate::error::{FilterChainError, Result}; -use crate::framebuffer::{GLImage, Viewport}; +use crate::framebuffer::GLImage; use crate::gl::framebuffer::Framebuffer; use crate::gl::FramebufferInterface; use crate::texture::Texture; +use crate::viewport::Viewport; use gl::types::{GLenum, GLint, GLsizei, GLuint}; -use librashader_common::{FilterMode, ImageFormat, Size, WrapMode}; +use librashader_common::{ImageFormat, Size}; use librashader_presets::Scale2D; #[derive(Debug)] @@ -91,14 +92,12 @@ impl FramebufferInterface for Gl46Framebuffer { fn copy_from(fb: &mut Framebuffer, image: &GLImage) -> Result<()> { // todo: confirm this behaviour for unbound image. if image.handle == 0 { - return Ok(()) + return Ok(()); } // todo: may want to use a shader and draw a quad to be faster. if image.size != fb.size || image.format != fb.format { - Self::init( - fb, - image.size, image.format)?; + Self::init(fb, image.size, image.format)?; } unsafe { diff --git a/librashader-runtime-gl/src/gl/gl46/hello_triangle.rs b/librashader-runtime-gl/src/gl/gl46/hello_triangle.rs index e9ca0c4..48c2037 100644 --- a/librashader-runtime-gl/src/gl/gl46/hello_triangle.rs +++ b/librashader-runtime-gl/src/gl/gl46/hello_triangle.rs @@ -8,9 +8,10 @@ use gl::types::{GLchar, GLenum, GLint, GLsizei, GLuint}; use librashader_common::Size; use crate::filter_chain::FilterChain; -use crate::framebuffer::{GLImage, Viewport}; +use crate::framebuffer::GLImage; use crate::gl::gl46::DirectStateAccessGL; use crate::gl::{FramebufferInterface, GLInterface}; +use crate::viewport::Viewport; const WIDTH: u32 = 900; const HEIGHT: u32 = 700; @@ -497,8 +498,8 @@ void main() } let viewport = Viewport { - x: 0, - y: 0, + x: 0f32, + y: 0f32, output: &output, mvp: None, }; @@ -514,7 +515,7 @@ void main() }; filter - .frame(framecount, &viewport, &rendered, None) + .frame(&rendered, &viewport, framecount, None) .unwrap(); unsafe { diff --git a/librashader-runtime-gl/src/gl/mod.rs b/librashader-runtime-gl/src/gl/mod.rs index 30b19f9..20396f5 100644 --- a/librashader-runtime-gl/src/gl/mod.rs +++ b/librashader-runtime-gl/src/gl/mod.rs @@ -4,12 +4,13 @@ pub(crate) mod gl46; use crate::binding::UniformLocation; use crate::error::Result; -use crate::framebuffer::{GLImage, Viewport}; +use crate::framebuffer::GLImage; use crate::samplers::SamplerSet; use crate::texture::Texture; +use crate::viewport::Viewport; pub use framebuffer::Framebuffer; use gl::types::{GLenum, GLuint}; -use librashader_common::{FilterMode, ImageFormat, Size, WrapMode}; +use librashader_common::{ImageFormat, Size}; use librashader_presets::{Scale2D, TextureConfig}; use librashader_reflect::reflect::semantics::{TextureBinding, UboReflection}; use librashader_runtime::uniforms::UniformStorageAccess; diff --git a/librashader-runtime-gl/src/lib.rs b/librashader-runtime-gl/src/lib.rs index d19b09d..8d16698 100644 --- a/librashader-runtime-gl/src/lib.rs +++ b/librashader-runtime-gl/src/lib.rs @@ -13,11 +13,13 @@ mod gl; mod samplers; mod texture; -pub mod options; pub mod error; +pub mod options; +mod viewport; + pub use filter_chain::FilterChain; -pub use framebuffer::Viewport; pub use framebuffer::GLImage; +pub use viewport::Viewport; #[cfg(test)] mod tests { @@ -28,26 +30,30 @@ mod tests { #[test] fn triangle_gl() { let (glfw, window, events, shader, vao) = gl::gl3::hello_triangle::setup(); - let mut filter = - FilterChain::load_from_path("../test/slang-shaders/vhs/VHSPro.slangp", Some(&FilterChainOptions { + let mut filter = FilterChain::load_from_path( + "../test/slang-shaders/vhs/VHSPro.slangp", + Some(&FilterChainOptions { gl_version: 0, use_dsa: false, - })) - // FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None) - .unwrap(); + }), + ) + // FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None) + .unwrap(); gl::gl3::hello_triangle::do_loop(glfw, window, events, shader, vao, &mut filter); } #[test] fn triangle_gl46() { let (glfw, window, events, shader, vao) = gl::gl46::hello_triangle::setup(); - let mut filter = - FilterChain::load_from_path("../test/slang-shaders/vhs/VHSPro.slangp", Some(&FilterChainOptions { + let mut filter = FilterChain::load_from_path( + "../test/slang-shaders/vhs/VHSPro.slangp", + Some(&FilterChainOptions { gl_version: 0, use_dsa: true, - })) - // FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None) - .unwrap(); + }), + ) + // FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None) + .unwrap(); gl::gl46::hello_triangle::do_loop(glfw, window, events, shader, vao, &mut filter); } } diff --git a/librashader-runtime-gl/src/options.rs b/librashader-runtime-gl/src/options.rs index 0853b77..ed633a8 100644 --- a/librashader-runtime-gl/src/options.rs +++ b/librashader-runtime-gl/src/options.rs @@ -8,5 +8,5 @@ pub struct FrameOptions { #[derive(Debug, Clone)] pub struct FilterChainOptions { pub gl_version: u16, - pub use_dsa: bool + pub use_dsa: bool, } diff --git a/librashader-runtime-gl/src/render_target.rs b/librashader-runtime-gl/src/render_target.rs index 176e053..6bcba2f 100644 --- a/librashader-runtime-gl/src/render_target.rs +++ b/librashader-runtime-gl/src/render_target.rs @@ -1,5 +1,5 @@ -use crate::framebuffer::Viewport; use crate::gl::{Framebuffer, FramebufferInterface}; +use crate::viewport::Viewport; #[rustfmt::skip] static DEFAULT_MVP: &[f32; 16] = &[ @@ -39,6 +39,6 @@ impl<'a> RenderTarget<'a> { impl<'a> From<&Viewport<'a>> for RenderTarget<'a> { fn from(value: &Viewport<'a>) -> Self { - RenderTarget::new(value.output, value.mvp, value.x, value.y) + RenderTarget::new(value.output, value.mvp, value.x as i32, value.y as i32) } } diff --git a/librashader-runtime-gl/src/viewport.rs b/librashader-runtime-gl/src/viewport.rs new file mode 100644 index 0000000..3d5c9ca --- /dev/null +++ b/librashader-runtime-gl/src/viewport.rs @@ -0,0 +1,9 @@ +use crate::gl::Framebuffer; + +#[derive(Debug, Copy, Clone)] +pub struct Viewport<'a> { + pub x: f32, + pub y: f32, + pub output: &'a Framebuffer, + pub mvp: Option<&'a [f32; 16]>, +}