From 8fca2954b006859507e76ed790d4b5578d7232a4 Mon Sep 17 00:00:00 2001 From: chyyran Date: Wed, 21 Dec 2022 21:39:31 -0500 Subject: [PATCH] fmt: run cargo fmt --- .../src/runtime/d3d11/filter_chain.rs | 5 +- .../src/runtime/gl/filter_chain.rs | 5 +- librashader-common/src/dxgi.rs | 4 +- librashader-common/src/vk.rs | 4 +- librashader-reflect/src/back/mod.rs | 2 +- librashader-reflect/src/back/spirv.rs | 25 ++- librashader-reflect/src/reflect/cross.rs | 13 +- librashader-reflect/src/reflect/semantics.rs | 1 - librashader-runtime-d3d11/src/error.rs | 2 +- librashader-runtime-d3d11/src/filter_chain.rs | 32 ++-- librashader-runtime-d3d11/src/framebuffer.rs | 17 +- librashader-runtime-d3d11/src/lib.rs | 4 +- librashader-runtime-d3d11/src/parameters.rs | 19 +- librashader-runtime-d3d11/src/texture.rs | 10 +- librashader-runtime-gl/src/error.rs | 4 +- .../src/filter_chain/filter_impl.rs | 58 +++--- .../src/filter_chain/mod.rs | 33 ++-- .../src/filter_chain/parameters.rs | 23 ++- librashader-runtime-gl/src/filter_pass.rs | 6 +- .../src/gl/gl3/framebuffer.rs | 5 +- librashader-runtime-gl/src/gl/gl3/lut_load.rs | 4 +- librashader-runtime-gl/src/gl/gl3/ubo_ring.rs | 2 +- .../src/gl/gl46/draw_quad.rs | 2 +- .../src/gl/gl46/framebuffer.rs | 5 +- .../src/gl/gl46/lut_load.rs | 4 +- .../src/gl/gl46/texture_bind.rs | 4 +- .../src/gl/gl46/ubo_ring.rs | 2 +- librashader-runtime-gl/src/lib.rs | 2 +- librashader-runtime-vk/src/draw_quad.rs | 10 +- librashader-runtime-vk/src/error.rs | 2 +- librashader-runtime-vk/src/filter_chain.rs | 145 ++++++++------- librashader-runtime-vk/src/filter_pass.rs | 4 +- librashader-runtime-vk/src/framebuffer.rs | 70 ++++--- librashader-runtime-vk/src/hello_triangle.rs | 10 +- .../src/hello_triangle/base.rs | 42 ++--- librashader-runtime-vk/src/lib.rs | 27 +-- librashader-runtime-vk/src/luts.rs | 175 +++++++++++------- librashader-runtime-vk/src/renderpass.rs | 17 +- librashader-runtime-vk/src/util.rs | 52 ++++-- .../src/vulkan_primitives.rs | 61 +++--- librashader-runtime-vk/src/vulkan_state.rs | 122 +++++++----- librashader-runtime/src/filter_chain.rs | 2 +- librashader-runtime/src/image.rs | 10 +- librashader-runtime/src/parameters.rs | 2 +- librashader-runtime/src/ringbuffer.rs | 11 +- librashader-runtime/src/scaling.rs | 17 +- librashader-runtime/src/uniforms.rs | 1 - librashader/src/lib.rs | 18 +- 48 files changed, 631 insertions(+), 464 deletions(-) diff --git a/librashader-capi/src/runtime/d3d11/filter_chain.rs b/librashader-capi/src/runtime/d3d11/filter_chain.rs index 0561da7..4b928ad 100644 --- a/librashader-capi/src/runtime/d3d11/filter_chain.rs +++ b/librashader-capi/src/runtime/d3d11/filter_chain.rs @@ -149,9 +149,8 @@ pub unsafe extern "C" fn libra_d3d11_filter_chain_frame( }) } -pub type PFN_lbr_d3d11_filter_chain_free = unsafe extern "C" fn( - chain: *mut libra_d3d11_filter_chain_t, -) -> libra_error_t; +pub type PFN_lbr_d3d11_filter_chain_free = + unsafe extern "C" fn(chain: *mut libra_d3d11_filter_chain_t) -> libra_error_t; /// Free a D3D11 filter chain. /// /// The resulting value in `chain` then becomes null. diff --git a/librashader-capi/src/runtime/gl/filter_chain.rs b/librashader-capi/src/runtime/gl/filter_chain.rs index 3f0f575..b76f0bc 100644 --- a/librashader-capi/src/runtime/gl/filter_chain.rs +++ b/librashader-capi/src/runtime/gl/filter_chain.rs @@ -169,9 +169,8 @@ pub unsafe extern "C" fn libra_gl_filter_chain_frame( }) } -pub type PFN_lbr_gl_filter_chain_free = unsafe extern "C" fn( - chain: *mut libra_gl_filter_chain_t, -)-> libra_error_t; +pub type PFN_lbr_gl_filter_chain_free = + unsafe extern "C" fn(chain: *mut libra_gl_filter_chain_t) -> libra_error_t; /// Free a GL filter chain. /// diff --git a/librashader-common/src/dxgi.rs b/librashader-common/src/dxgi.rs index 1d5b096..1dabdd3 100644 --- a/librashader-common/src/dxgi.rs +++ b/librashader-common/src/dxgi.rs @@ -1,6 +1,6 @@ +use crate::ImageFormat; use windows::Win32::Graphics::Dxgi::Common as dxgi; use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT; -use crate::ImageFormat; impl From for dxgi::DXGI_FORMAT { fn from(format: ImageFormat) -> Self { @@ -77,4 +77,4 @@ impl From for ImageFormat { _ => ImageFormat::Unknown, } } -} \ No newline at end of file +} diff --git a/librashader-common/src/vk.rs b/librashader-common/src/vk.rs index 74a94cb..1383bf3 100644 --- a/librashader-common/src/vk.rs +++ b/librashader-common/src/vk.rs @@ -1,5 +1,5 @@ -use ash::vk; use crate::{ImageFormat, Size}; +use ash::vk; impl From for vk::Format { fn from(format: ImageFormat) -> Self { @@ -86,4 +86,4 @@ impl From> for vk::Extent3D { depth: 1, } } -} \ No newline at end of file +} diff --git a/librashader-reflect/src/back/mod.rs b/librashader-reflect/src/back/mod.rs index 3faeb13..3110836 100644 --- a/librashader-reflect/src/back/mod.rs +++ b/librashader-reflect/src/back/mod.rs @@ -1,6 +1,6 @@ pub mod cross; -pub mod targets; mod spirv; +pub mod targets; use crate::back::targets::OutputTarget; use crate::error::{ShaderCompileError, ShaderReflectError}; diff --git a/librashader-reflect/src/back/spirv.rs b/librashader-reflect/src/back/spirv.rs index 8aadcda..2cd1496 100644 --- a/librashader-reflect/src/back/spirv.rs +++ b/librashader-reflect/src/back/spirv.rs @@ -1,24 +1,24 @@ -use crate::back::{CompilerBackend, CompileShader, FromCompilation, ShaderCompilerOutput}; use crate::back::targets::SpirV; +use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompilerOutput}; use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::front::shaderc::GlslangCompilation; -use crate::reflect::{ReflectShader, ShaderReflection}; -use crate::reflect::cross::{GlslReflect}; +use crate::reflect::cross::GlslReflect; use crate::reflect::semantics::ShaderSemantics; +use crate::reflect::{ReflectShader, ShaderReflection}; struct WriteSpirV { // rely on GLSL to provide out reflection but we don't actually need the AST. reflect: GlslReflect, vertex: Vec, - fragment: Vec + fragment: Vec, } impl FromCompilation for SpirV { type Target = SpirV; type Options = Option<()>; type Context = (); - type Output = impl CompileShader, Context = ()> - + ReflectShader; + type Output = + impl CompileShader, Context = ()> + ReflectShader; fn from_compilation( compile: GlslangCompilation, @@ -37,7 +37,11 @@ impl FromCompilation for SpirV { } impl ReflectShader for WriteSpirV { - fn reflect(&mut self, pass_number: usize, semantics: &ShaderSemantics) -> Result { + fn reflect( + &mut self, + pass_number: usize, + semantics: &ShaderSemantics, + ) -> Result { self.reflect.reflect(pass_number, semantics) } } @@ -46,11 +50,14 @@ impl CompileShader for WriteSpirV { type Options = Option<()>; type Context = (); - fn compile(self, _options: Self::Options) -> Result, Self::Context>, ShaderCompileError> { + fn compile( + self, + _options: Self::Options, + ) -> Result, Self::Context>, ShaderCompileError> { Ok(ShaderCompilerOutput { vertex: self.vertex, fragment: self.fragment, context: (), }) } -} \ No newline at end of file +} diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 0f0e88d..8924231 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -1,12 +1,17 @@ use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError}; use crate::front::shaderc::GlslangCompilation; -use crate::reflect::semantics::{BindingStage, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, MemberOffset, PushReflection, BindingMeta, ShaderSemantics, ShaderReflection, TextureBinding, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo, UboReflection, UniqueSemantics, ValidateTypeSemantics, VariableMeta, UniqueSemanticMap}; +use crate::reflect::semantics::{ + BindingMeta, BindingStage, MemberOffset, PushReflection, ShaderReflection, ShaderSemantics, + TextureBinding, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo, UboReflection, + UniqueSemanticMap, UniqueSemantics, ValidateTypeSemantics, VariableMeta, MAX_BINDINGS_COUNT, + MAX_PUSH_BUFFER_SIZE, +}; use crate::reflect::{align_uniform_size, ReflectShader}; use std::ops::Deref; use spirv_cross::hlsl::ShaderModel; use spirv_cross::spirv::{Ast, Decoration, Module, Resource, ShaderResources, Type}; -use spirv_cross::{ErrorCode, glsl, hlsl}; +use spirv_cross::{glsl, hlsl, ErrorCode}; use crate::back::cross::{CrossGlslContext, CrossHlslContext}; use crate::back::targets::{GLSL, HLSL}; @@ -862,9 +867,7 @@ mod test { use crate::back::CompileShader; use crate::front::shaderc::GlslangCompilation; - use crate::reflect::semantics::{ - ShaderSemantics, Semantic, UniformSemantic, UniqueSemantics, - }; + use crate::reflect::semantics::{Semantic, ShaderSemantics, UniformSemantic, UniqueSemantics}; use librashader_preprocess::ShaderSource; use spirv_cross::glsl; use spirv_cross::glsl::{CompilerOptions, Version}; diff --git a/librashader-reflect/src/reflect/semantics.rs b/librashader-reflect/src/reflect/semantics.rs index 6342006..064f829 100644 --- a/librashader-reflect/src/reflect/semantics.rs +++ b/librashader-reflect/src/reflect/semantics.rs @@ -201,7 +201,6 @@ pub enum MemberOffset { PushConstant(usize), } - /// Reflection information about a non-texture related uniform variable. #[derive(Debug)] pub struct VariableMeta { diff --git a/librashader-runtime-d3d11/src/error.rs b/librashader-runtime-d3d11/src/error.rs index 5ab48b3..86f6cf7 100644 --- a/librashader-runtime-d3d11/src/error.rs +++ b/librashader-runtime-d3d11/src/error.rs @@ -1,7 +1,7 @@ -use librashader_runtime::image::ImageError; use librashader_preprocess::PreprocessError; use librashader_presets::ParsePresetError; use librashader_reflect::error::{ShaderCompileError, ShaderReflectError}; +use librashader_runtime::image::ImageError; use thiserror::Error; #[derive(Error, Debug)] diff --git a/librashader-runtime-d3d11/src/filter_chain.rs b/librashader-runtime-d3d11/src/filter_chain.rs index db182f1..4979943 100644 --- a/librashader-runtime-d3d11/src/filter_chain.rs +++ b/librashader-runtime-d3d11/src/filter_chain.rs @@ -1,5 +1,4 @@ use crate::texture::{DxImageView, LutTexture, Texture}; -use librashader_runtime::image::{Image, UVDirection}; use librashader_common::{ImageFormat, Size}; use librashader_preprocess::ShaderSource; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; @@ -8,10 +7,10 @@ use librashader_reflect::back::targets::HLSL; use librashader_reflect::back::{CompileShader, CompilerBackend, FromCompilation}; use librashader_reflect::front::shaderc::GlslangCompilation; use librashader_reflect::reflect::semantics::{ - ShaderSemantics, Semantic, TextureSemantics, UniformBinding, UniformSemantic, - UniqueSemantics, + Semantic, ShaderSemantics, TextureSemantics, UniformBinding, UniformSemantic, UniqueSemantics, }; use librashader_reflect::reflect::ReflectShader; +use librashader_runtime::image::{Image, UVDirection}; use rustc_hash::FxHashMap; use std::collections::VecDeque; @@ -44,7 +43,7 @@ type ShaderPassMeta = ( ShaderPassConfig, ShaderSource, CompilerBackend< - impl CompileShader, Context =CrossHlslContext> + ReflectShader, + impl CompileShader, Context = CrossHlslContext> + ReflectShader, >, ); @@ -157,9 +156,7 @@ impl FilterChainD3D11 { let luts = FilterChainD3D11::load_luts(device, ¤t_context, &preset.textures)?; let (history_framebuffers, history_textures) = - FilterChainD3D11::init_history(device, - ¤t_context, - &filters)?; + FilterChainD3D11::init_history(device, ¤t_context, &filters)?; let draw_quad = DrawQuad::new(device, ¤t_context)?; @@ -497,7 +494,11 @@ impl FilterChainD3D11 { .zip(self.feedback_framebuffers.iter()) .zip(passes.iter()) { - *texture = Some(Texture::from_framebuffer(fbo, pass.config.wrap_mode, pass.config.filter)?); + *texture = Some(Texture::from_framebuffer( + fbo, + pass.config.wrap_mode, + pass.config.filter, + )?); } for (texture, fbo) in self @@ -589,7 +590,10 @@ impl FilterChainD3D11 { )?; } - std::mem::swap(&mut self.output_framebuffers, &mut self.feedback_framebuffers); + std::mem::swap( + &mut self.output_framebuffers, + &mut self.feedback_framebuffers, + ); self.push_history(&input)?; @@ -613,7 +617,13 @@ impl librashader_runtime::filter_chain::FilterChain for FilterChainD3D11 { type Viewport<'a> = Viewport<'a>; type FrameOptions = FrameOptionsD3D11; - fn frame<'a>(&mut self, input: Self::Input<'a>, viewport: &Self::Viewport<'a>, frame_count: usize, options: Option<&Self::FrameOptions>) -> Result<(), Self::Error> { + fn frame<'a>( + &mut self, + input: Self::Input<'a>, + viewport: &Self::Viewport<'a>, + frame_count: usize, + options: Option<&Self::FrameOptions>, + ) -> Result<(), Self::Error> { self.frame(input, viewport, frame_count, options) } -} \ No newline at end of file +} diff --git a/librashader-runtime-d3d11/src/framebuffer.rs b/librashader-runtime-d3d11/src/framebuffer.rs index dfb547e..929fd0b 100644 --- a/librashader-runtime-d3d11/src/framebuffer.rs +++ b/librashader-runtime-d3d11/src/framebuffer.rs @@ -3,11 +3,19 @@ use crate::texture::{DxImageView, Texture}; use crate::util::d3d11_get_closest_format; use librashader_common::{ImageFormat, Size}; use librashader_presets::Scale2D; +use librashader_runtime::scaling::ViewportSize; use windows::core::Interface; use windows::Win32::Graphics::Direct3D::D3D_SRV_DIMENSION_TEXTURE2D; -use windows::Win32::Graphics::Direct3D11::{ID3D11Device, ID3D11RenderTargetView, ID3D11ShaderResourceView, ID3D11Texture2D, D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE, D3D11_FORMAT_SUPPORT_RENDER_TARGET, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE, D3D11_FORMAT_SUPPORT_TEXTURE2D, D3D11_RENDER_TARGET_VIEW_DESC, D3D11_RENDER_TARGET_VIEW_DESC_0, D3D11_RTV_DIMENSION_TEXTURE2D, D3D11_SHADER_RESOURCE_VIEW_DESC, D3D11_SHADER_RESOURCE_VIEW_DESC_0, D3D11_TEX2D_RTV, D3D11_TEX2D_SRV, D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_VIEWPORT, D3D11_RESOURCE_MISC_GENERATE_MIPS, ID3D11DeviceContext, D3D11_BOX}; +use windows::Win32::Graphics::Direct3D11::{ + ID3D11Device, ID3D11DeviceContext, ID3D11RenderTargetView, ID3D11ShaderResourceView, + ID3D11Texture2D, D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, D3D11_BOX, + D3D11_CPU_ACCESS_WRITE, D3D11_FORMAT_SUPPORT_RENDER_TARGET, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE, + D3D11_FORMAT_SUPPORT_TEXTURE2D, D3D11_RENDER_TARGET_VIEW_DESC, D3D11_RENDER_TARGET_VIEW_DESC_0, + D3D11_RESOURCE_MISC_GENERATE_MIPS, D3D11_RTV_DIMENSION_TEXTURE2D, + D3D11_SHADER_RESOURCE_VIEW_DESC, D3D11_SHADER_RESOURCE_VIEW_DESC_0, D3D11_TEX2D_RTV, + D3D11_TEX2D_SRV, D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_VIEWPORT, +}; use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT, DXGI_SAMPLE_DESC}; -use librashader_runtime::scaling::ViewportSize; #[derive(Debug, Clone)] pub(crate) struct OwnedFramebuffer { @@ -48,14 +56,13 @@ impl OwnedFramebuffer { } } - pub(crate) fn scale( &mut self, scaling: Scale2D, format: ImageFormat, viewport_size: &Size, _original: &Texture, - source: &Texture + source: &Texture, ) -> error::Result> { if self.is_raw { return Ok(self.size); @@ -215,7 +222,7 @@ fn default_desc(size: Size, format: DXGI_FORMAT, mip_levels: u32) -> D3D11_ Usage: D3D11_USAGE_DEFAULT, BindFlags: D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, CPUAccessFlags: D3D11_CPU_ACCESS_WRITE, - MiscFlags: D3D11_RESOURCE_MISC_GENERATE_MIPS + MiscFlags: D3D11_RESOURCE_MISC_GENERATE_MIPS, } } pub const fn default_viewport(size: Size) -> D3D11_VIEWPORT { diff --git a/librashader-runtime-d3d11/src/lib.rs b/librashader-runtime-d3d11/src/lib.rs index b5ba8fa..d877fed 100644 --- a/librashader-runtime-d3d11/src/lib.rs +++ b/librashader-runtime-d3d11/src/lib.rs @@ -9,17 +9,17 @@ mod filter_chain; mod filter_pass; mod framebuffer; pub mod options; +mod parameters; mod quad_render; mod render_target; mod samplers; mod texture; mod util; mod viewport; -mod parameters; pub use filter_chain::FilterChainD3D11; -pub use viewport::Viewport; pub use texture::DxImageView; +pub use viewport::Viewport; #[cfg(test)] mod tests { diff --git a/librashader-runtime-d3d11/src/parameters.rs b/librashader-runtime-d3d11/src/parameters.rs index 64ac7cf..f2e1b92 100644 --- a/librashader-runtime-d3d11/src/parameters.rs +++ b/librashader-runtime-d3d11/src/parameters.rs @@ -1,6 +1,6 @@ -use std::collections::hash_map::Iter; -use librashader_runtime::parameters::FilterChainParameters; use crate::FilterChainD3D11; +use librashader_runtime::parameters::FilterChainParameters; +use std::collections::hash_map::Iter; impl FilterChainParameters for FilterChainD3D11 { fn get_enabled_pass_count(&self) -> usize { @@ -16,11 +16,20 @@ impl FilterChainParameters for FilterChainD3D11 { } fn get_parameter(&self, parameter: &str) -> Option { - self.common.config.parameters.get::(parameter.as_ref()).copied() + self.common + .config + .parameters + .get::(parameter.as_ref()) + .copied() } fn set_parameter(&mut self, parameter: &str, new_value: f32) -> Option { - if let Some(value) = self.common.config.parameters.get_mut::(parameter.as_ref()) { + if let Some(value) = self + .common + .config + .parameters + .get_mut::(parameter.as_ref()) + { let old = *value; *value = new_value; Some(old) @@ -28,4 +37,4 @@ impl FilterChainParameters for FilterChainD3D11 { None } } -} \ No newline at end of file +} diff --git a/librashader-runtime-d3d11/src/texture.rs b/librashader-runtime-d3d11/src/texture.rs index d515a69..95553b3 100644 --- a/librashader-runtime-d3d11/src/texture.rs +++ b/librashader-runtime-d3d11/src/texture.rs @@ -1,5 +1,5 @@ -use librashader_runtime::image::Image; use librashader_common::{FilterMode, Size, WrapMode}; +use librashader_runtime::image::Image; use windows::Win32::Graphics::Direct3D::D3D_SRV_DIMENSION_TEXTURE2D; use windows::Win32::Graphics::Direct3D11::{ ID3D11Device, ID3D11DeviceContext, ID3D11ShaderResourceView, ID3D11Texture2D, D3D11_BIND_FLAG, @@ -26,14 +26,18 @@ pub(crate) struct Texture { } impl Texture { - pub fn from_framebuffer(fbo: &OwnedFramebuffer, wrap_mode: WrapMode, filter: FilterMode) -> Result { + pub fn from_framebuffer( + fbo: &OwnedFramebuffer, + wrap_mode: WrapMode, + filter: FilterMode, + ) -> Result { Ok(Texture { view: DxImageView { handle: fbo.create_shader_resource_view()?, size: fbo.size, }, filter, - wrap_mode + wrap_mode, }) } } diff --git a/librashader-runtime-gl/src/error.rs b/librashader-runtime-gl/src/error.rs index aed3e56..2ff7a44 100644 --- a/librashader-runtime-gl/src/error.rs +++ b/librashader-runtime-gl/src/error.rs @@ -1,8 +1,8 @@ use gl::types::GLenum; -use librashader_runtime::image::ImageError; use librashader_preprocess::PreprocessError; use librashader_presets::ParsePresetError; use librashader_reflect::error::{ShaderCompileError, ShaderReflectError}; +use librashader_runtime::image::ImageError; use thiserror::Error; #[derive(Error, Debug)] @@ -22,7 +22,7 @@ pub enum FilterChainError { #[error("lut loading error")] LutLoadError(#[from] ImageError), #[error("opengl was not initialized")] - GLLoadError + GLLoadError, } pub type Result = std::result::Result; diff --git a/librashader-runtime-gl/src/filter_chain/filter_impl.rs b/librashader-runtime-gl/src/filter_chain/filter_impl.rs index f1a4a30..c5a478a 100644 --- a/librashader-runtime-gl/src/filter_chain/filter_impl.rs +++ b/librashader-runtime-gl/src/filter_chain/filter_impl.rs @@ -1,17 +1,3 @@ -use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; -use librashader_reflect::reflect::semantics::{MemberOffset, ShaderSemantics, Semantic, TextureSemantics, UniformBinding, UniformMeta, UniformSemantic, UniqueSemantics}; -use rustc_hash::FxHashMap; -use librashader_preprocess::ShaderSource; -use librashader_reflect::back::{CompilerBackend, CompileShader, FromCompilation}; -use librashader_reflect::back::cross::{CrossGlslContext, GlslVersion}; -use librashader_reflect::back::targets::GLSL; -use librashader_reflect::front::shaderc::GlslangCompilation; -use spirv_cross::spirv::Decoration; -use gl::types::{GLenum, GLint, GLuint}; -use librashader_common::{FilterMode, Size, WrapMode}; -use std::collections::VecDeque; -use librashader_reflect::reflect::ReflectShader; -use crate::{error, GLImage, util, Viewport}; use crate::binding::{GlUniformStorage, UniformLocation, VariableLocation}; use crate::error::FilterChainError; use crate::filter_pass::FilterPass; @@ -21,6 +7,23 @@ use crate::render_target::RenderTarget; use crate::samplers::SamplerSet; use crate::texture::Texture; use crate::util::{gl_get_version, gl_u16_to_version}; +use crate::{error, util, GLImage, Viewport}; +use gl::types::{GLenum, GLint, GLuint}; +use librashader_common::{FilterMode, Size, WrapMode}; +use librashader_preprocess::ShaderSource; +use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; +use librashader_reflect::back::cross::{CrossGlslContext, GlslVersion}; +use librashader_reflect::back::targets::GLSL; +use librashader_reflect::back::{CompileShader, CompilerBackend, FromCompilation}; +use librashader_reflect::front::shaderc::GlslangCompilation; +use librashader_reflect::reflect::semantics::{ + MemberOffset, Semantic, ShaderSemantics, TextureSemantics, UniformBinding, UniformMeta, + UniformSemantic, UniqueSemantics, +}; +use librashader_reflect::reflect::ReflectShader; +use rustc_hash::FxHashMap; +use spirv_cross::spirv::Decoration; +use std::collections::VecDeque; pub(crate) struct FilterChainImpl { pub(crate) common: FilterCommon, @@ -79,16 +82,19 @@ type ShaderPassMeta = ( ShaderPassConfig, ShaderSource, CompilerBackend< - impl CompileShader + ReflectShader, + impl CompileShader + ReflectShader, >, ); impl FilterChainImpl { - pub(crate) fn create_framebuffer_raw(&self, texture: GLuint, - handle: GLuint, - format: GLenum, - size: Size, - miplevels: u32,) -> Framebuffer { + pub(crate) fn create_framebuffer_raw( + &self, + texture: GLuint, + handle: GLuint, + format: GLenum, + size: Size, + miplevels: u32, + ) -> Framebuffer { T::FramebufferInterface::new_from_raw(texture, handle, format, size, miplevels) } /// Load a filter chain from a pre-parsed `ShaderPreset`. @@ -187,7 +193,8 @@ impl FilterChainImpl { Ok::<_, FilterChainError>((shader, source, reflect)) }) .into_iter() - .collect::)>>>()?; + .collect::)>>>( + )?; for details in &passes { librashader_runtime::semantics::insert_pass_semantics( @@ -542,11 +549,16 @@ impl FilterChainImpl { &source, viewport.into(), ); - self.common.output_textures[passes_len - 1] = viewport.output.as_texture(pass.config.filter, pass.config.wrap_mode); + self.common.output_textures[passes_len - 1] = viewport + .output + .as_texture(pass.config.filter, pass.config.wrap_mode); } // swap feedback framebuffers with output - std::mem::swap(&mut self.output_framebuffers, &mut self.feedback_framebuffers); + std::mem::swap( + &mut self.output_framebuffers, + &mut self.feedback_framebuffers, + ); self.push_history(input)?; diff --git a/librashader-runtime-gl/src/filter_chain/mod.rs b/librashader-runtime-gl/src/filter_chain/mod.rs index acb6576..2eec666 100644 --- a/librashader-runtime-gl/src/filter_chain/mod.rs +++ b/librashader-runtime-gl/src/filter_chain/mod.rs @@ -1,13 +1,13 @@ +use gl::types::{GLenum, GLuint}; use std::panic::catch_unwind; use std::path::Path; -use gl::types::{GLenum, GLuint}; -use librashader_presets::ShaderPreset; +use crate::error::{FilterChainError, Result}; use crate::filter_chain::filter_impl::FilterChainImpl; use crate::filter_chain::inner::FilterChainDispatch; -use crate::{Framebuffer, GLImage, Viewport}; -use crate::error::{Result, FilterChainError}; use crate::options::{FilterChainOptionsGL, FrameOptionsGL}; +use crate::{Framebuffer, GLImage, Viewport}; +use librashader_presets::ShaderPreset; mod filter_impl; mod inner; @@ -21,17 +21,21 @@ pub struct FilterChainGL { } impl FilterChainGL { - pub fn create_framebuffer_raw(&self, texture: GLuint, - handle: GLuint, - format: GLenum, - size: Size, - miplevels: u32,) -> Framebuffer { - + pub fn create_framebuffer_raw( + &self, + texture: GLuint, + handle: GLuint, + format: GLenum, + size: Size, + miplevels: u32, + ) -> Framebuffer { match &self.filter { FilterChainDispatch::DirectStateAccess(p) => { p.create_framebuffer_raw(texture, handle, format, size, miplevels) } - FilterChainDispatch::Compatibility(p) => p.create_framebuffer_raw(texture, handle, format, size, miplevels), + FilterChainDispatch::Compatibility(p) => { + p.create_framebuffer_raw(texture, handle, format, size, miplevels) + } } } pub fn load_from_preset( @@ -51,10 +55,8 @@ impl FilterChainGL { }) }); match result { - Err(_) => { - return Err(FilterChainError::GLLoadError) - } - Ok(res) => res + Err(_) => return Err(FilterChainError::GLLoadError), + Ok(res) => res, } } @@ -103,4 +105,3 @@ impl librashader_runtime::filter_chain::FilterChain for FilterChainGL { self.frame(input, viewport, frame_count, options) } } - diff --git a/librashader-runtime-gl/src/filter_chain/parameters.rs b/librashader-runtime-gl/src/filter_chain/parameters.rs index 68f79f4..670aee6 100644 --- a/librashader-runtime-gl/src/filter_chain/parameters.rs +++ b/librashader-runtime-gl/src/filter_chain/parameters.rs @@ -1,9 +1,9 @@ -use std::collections::hash_map::Iter; -use librashader_runtime::parameters::FilterChainParameters; use crate::filter_chain::filter_impl::FilterChainImpl; use crate::filter_chain::inner::FilterChainDispatch; -use crate::FilterChainGL; use crate::gl::GLInterface; +use crate::FilterChainGL; +use librashader_runtime::parameters::FilterChainParameters; +use std::collections::hash_map::Iter; impl AsRef for FilterChainDispatch { fn as_ref<'a>(&'a self) -> &'a (dyn FilterChainParameters + 'static) { @@ -45,7 +45,7 @@ impl FilterChainParameters for FilterChainGL { } } -impl FilterChainParameters for FilterChainImpl { +impl FilterChainParameters for FilterChainImpl { fn get_enabled_pass_count(&self) -> usize { self.common.config.passes_enabled } @@ -59,11 +59,20 @@ impl FilterChainParameters for FilterChainImpl { } fn get_parameter(&self, parameter: &str) -> Option { - self.common.config.parameters.get::(parameter.as_ref()).copied() + self.common + .config + .parameters + .get::(parameter.as_ref()) + .copied() } fn set_parameter(&mut self, parameter: &str, new_value: f32) -> Option { - if let Some(value) = self.common.config.parameters.get_mut::(parameter.as_ref()) { + if let Some(value) = self + .common + .config + .parameters + .get_mut::(parameter.as_ref()) + { let old = *value; *value = new_value; Some(old) @@ -71,4 +80,4 @@ impl FilterChainParameters for FilterChainImpl { None } } -} \ No newline at end of file +} diff --git a/librashader-runtime-gl/src/filter_pass.rs b/librashader-runtime-gl/src/filter_pass.rs index 5379df2..95540e8 100644 --- a/librashader-runtime-gl/src/filter_pass.rs +++ b/librashader-runtime-gl/src/filter_pass.rs @@ -127,15 +127,13 @@ impl FilterPass { source: &Texture, ) { // Bind MVP - if let Some((location, offset)) = self.uniform_bindings.get(&UniqueSemantics::MVP.into()) - { + if let Some((location, offset)) = self.uniform_bindings.get(&UniqueSemantics::MVP.into()) { self.uniform_storage .bind_mat4(*offset, mvp, location.location()); } // bind OutputSize - if let Some((location, offset)) = - self.uniform_bindings.get(&UniqueSemantics::Output.into()) + if let Some((location, offset)) = self.uniform_bindings.get(&UniqueSemantics::Output.into()) { self.uniform_storage .bind_vec4(*offset, fb_size, location.location()); diff --git a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs index 4cc5435..0ef00e6 100644 --- a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs @@ -64,7 +64,10 @@ impl FramebufferInterface for Gl3Framebuffer { return Ok(fb.size); } - let size = source.image.size.scale_viewport(scaling, viewport.output.size); + let size = source + .image + .size + .scale_viewport(scaling, viewport.output.size); if fb.size != size { fb.size = size; diff --git a/librashader-runtime-gl/src/gl/gl3/lut_load.rs b/librashader-runtime-gl/src/gl/gl3/lut_load.rs index 8efd430..c3a4523 100644 --- a/librashader-runtime-gl/src/gl/gl3/lut_load.rs +++ b/librashader-runtime-gl/src/gl/gl3/lut_load.rs @@ -3,11 +3,11 @@ use crate::framebuffer::GLImage; use crate::gl::LoadLut; use crate::texture::Texture; use gl::types::{GLsizei, GLuint}; -use librashader_runtime::image::{Image, UVDirection}; use librashader_common::Size; use librashader_presets::TextureConfig; -use rustc_hash::FxHashMap; +use librashader_runtime::image::{Image, UVDirection}; use librashader_runtime::scaling::MipmapSize; +use rustc_hash::FxHashMap; pub struct Gl3LutLoad; impl LoadLut for Gl3LutLoad { diff --git a/librashader-runtime-gl/src/gl/gl3/ubo_ring.rs b/librashader-runtime-gl/src/gl/gl3/ubo_ring.rs index 750e7e0..89491e8 100644 --- a/librashader-runtime-gl/src/gl/gl3/ubo_ring.rs +++ b/librashader-runtime-gl/src/gl/gl3/ubo_ring.rs @@ -1,8 +1,8 @@ use crate::binding::UniformLocation; use crate::gl::UboRing; -use librashader_runtime::ringbuffer::InlineRingBuffer; use gl::types::{GLsizei, GLsizeiptr, GLuint}; use librashader_reflect::reflect::semantics::UboReflection; +use librashader_runtime::ringbuffer::InlineRingBuffer; use librashader_runtime::ringbuffer::RingBuffer; use librashader_runtime::uniforms::UniformStorageAccess; diff --git a/librashader-runtime-gl/src/gl/gl46/draw_quad.rs b/librashader-runtime-gl/src/gl/gl46/draw_quad.rs index 8f6f1b2..a82df42 100644 --- a/librashader-runtime-gl/src/gl/gl46/draw_quad.rs +++ b/librashader-runtime-gl/src/gl/gl46/draw_quad.rs @@ -76,4 +76,4 @@ impl Drop for Gl46DrawQuad { } } } -} \ No newline at end of file +} diff --git a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs index 71122d1..527521c 100644 --- a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs @@ -62,7 +62,10 @@ impl FramebufferInterface for Gl46Framebuffer { return Ok(fb.size); } - let size = source.image.size.scale_viewport(scaling, viewport.output.size); + let size = source + .image + .size + .scale_viewport(scaling, viewport.output.size); if fb.size != size { fb.size = size; diff --git a/librashader-runtime-gl/src/gl/gl46/lut_load.rs b/librashader-runtime-gl/src/gl/gl46/lut_load.rs index 6cc3023..f4c8eba 100644 --- a/librashader-runtime-gl/src/gl/gl46/lut_load.rs +++ b/librashader-runtime-gl/src/gl/gl46/lut_load.rs @@ -3,11 +3,11 @@ use crate::framebuffer::GLImage; use crate::gl::LoadLut; use crate::texture::Texture; use gl::types::{GLsizei, GLuint}; -use librashader_runtime::image::{Image, UVDirection}; use librashader_common::Size; use librashader_presets::TextureConfig; -use rustc_hash::FxHashMap; +use librashader_runtime::image::{Image, UVDirection}; use librashader_runtime::scaling::MipmapSize; +use rustc_hash::FxHashMap; pub struct Gl46LutLoad; impl LoadLut for Gl46LutLoad { diff --git a/librashader-runtime-gl/src/gl/gl46/texture_bind.rs b/librashader-runtime-gl/src/gl/gl46/texture_bind.rs index 5445048..50ce9cc 100644 --- a/librashader-runtime-gl/src/gl/gl46/texture_bind.rs +++ b/librashader-runtime-gl/src/gl/gl46/texture_bind.rs @@ -18,8 +18,6 @@ impl BindTexture for Gl46BindTexture { } fn gen_mipmaps(texture: &Texture) { - unsafe { - gl::GenerateTextureMipmap(texture.image.handle) - } + unsafe { gl::GenerateTextureMipmap(texture.image.handle) } } } diff --git a/librashader-runtime-gl/src/gl/gl46/ubo_ring.rs b/librashader-runtime-gl/src/gl/gl46/ubo_ring.rs index 9ef7df3..6b9cf46 100644 --- a/librashader-runtime-gl/src/gl/gl46/ubo_ring.rs +++ b/librashader-runtime-gl/src/gl/gl46/ubo_ring.rs @@ -1,8 +1,8 @@ use crate::binding::UniformLocation; use crate::gl::UboRing; -use librashader_runtime::ringbuffer::InlineRingBuffer; use gl::types::{GLsizei, GLsizeiptr, GLuint}; use librashader_reflect::reflect::semantics::UboReflection; +use librashader_runtime::ringbuffer::InlineRingBuffer; use librashader_runtime::ringbuffer::RingBuffer; use librashader_runtime::uniforms::UniformStorageAccess; diff --git a/librashader-runtime-gl/src/lib.rs b/librashader-runtime-gl/src/lib.rs index 9678426..aba95a5 100644 --- a/librashader-runtime-gl/src/lib.rs +++ b/librashader-runtime-gl/src/lib.rs @@ -17,10 +17,10 @@ pub mod error; pub mod options; mod viewport; +pub use crate::gl::Framebuffer; pub use filter_chain::FilterChainGL; pub use framebuffer::GLImage; pub use viewport::Viewport; -pub use crate::gl::Framebuffer; #[cfg(test)] mod tests { diff --git a/librashader-runtime-vk/src/draw_quad.rs b/librashader-runtime-vk/src/draw_quad.rs index 5613481..53bbb21 100644 --- a/librashader-runtime-vk/src/draw_quad.rs +++ b/librashader-runtime-vk/src/draw_quad.rs @@ -1,15 +1,9 @@ static VBO_OFFSCREEN_MVP: &[f32; 16] = &[ // Offscreen - -1.0, -1.0, 0.0, 0.0, - -1.0, 1.0, 0.0, 1.0, - 1.0, -1.0, 1.0, 0.0, - 1.0, 1.0, 1.0, 1.0, + -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, ]; static VBO_DEFAULT_FINAL_MVP: &[f32; 16] = &[ // Final - 0.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 1.0, - 1.0, 0.0, 1.0, 0.0, - 1.0, 1.0, 1.0, 1.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, ]; diff --git a/librashader-runtime-vk/src/error.rs b/librashader-runtime-vk/src/error.rs index 9eb56e5..fa138cb 100644 --- a/librashader-runtime-vk/src/error.rs +++ b/librashader-runtime-vk/src/error.rs @@ -1,3 +1,3 @@ use std::error::Error; -pub type Result = std::result::Result>; \ No newline at end of file +pub type Result = std::result::Result>; diff --git a/librashader-runtime-vk/src/filter_chain.rs b/librashader-runtime-vk/src/filter_chain.rs index 9daa6ac..04f146a 100644 --- a/librashader-runtime-vk/src/filter_chain.rs +++ b/librashader-runtime-vk/src/filter_chain.rs @@ -1,22 +1,24 @@ -use std::error::Error; -use std::path::Path; -use ash::{Device, vk}; -use ash::vk::{CommandPoolCreateFlags, PFN_vkGetInstanceProcAddr, Queue, StaticFn}; -use rustc_hash::FxHashMap; -use librashader_common::ImageFormat; -use librashader_preprocess::ShaderSource; -use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; -use librashader_reflect::back::{CompilerBackend, CompileShader, FromCompilation}; -use librashader_reflect::back::targets::SpirV; -use librashader_reflect::front::shaderc::GlslangCompilation; -use librashader_reflect::reflect::ReflectShader; -use librashader_reflect::reflect::semantics::{Semantic, ShaderSemantics, TextureSemantics, UniformBinding, UniformSemantic, UniqueSemantics}; -use librashader_runtime::image::{Image, UVDirection}; -use librashader_runtime::uniforms::UniformStorage; use crate::error; use crate::filter_pass::FilterPass; use crate::luts::LutTexture; use crate::vulkan_state::VulkanGraphicsPipeline; +use ash::vk::{CommandPoolCreateFlags, PFN_vkGetInstanceProcAddr, Queue, StaticFn}; +use ash::{vk, Device}; +use librashader_common::ImageFormat; +use librashader_preprocess::ShaderSource; +use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; +use librashader_reflect::back::targets::SpirV; +use librashader_reflect::back::{CompileShader, CompilerBackend, FromCompilation}; +use librashader_reflect::front::shaderc::GlslangCompilation; +use librashader_reflect::reflect::semantics::{ + Semantic, ShaderSemantics, TextureSemantics, UniformBinding, UniformSemantic, UniqueSemantics, +}; +use librashader_reflect::reflect::ReflectShader; +use librashader_runtime::image::{Image, UVDirection}; +use librashader_runtime::uniforms::UniformStorage; +use rustc_hash::FxHashMap; +use std::error::Error; +use std::path::Path; pub struct Vulkan { // physical_device: vk::PhysicalDevice, @@ -31,12 +33,9 @@ pub struct Vulkan { type ShaderPassMeta = ( ShaderPassConfig, ShaderSource, - CompilerBackend< - impl CompileShader, Context =()> + ReflectShader, - >, + CompilerBackend, Context = ()> + ReflectShader>, ); - #[derive(Clone)] pub struct VulkanInfo<'a> { // physical_device: &'a vk::PhysicalDevice, @@ -44,7 +43,7 @@ pub struct VulkanInfo<'a> { instance: &'a vk::Instance, queue: &'a vk::Queue, memory_properties: &'a vk::PhysicalDeviceMemoryProperties, - get_instance_proc_addr: PFN_vkGetInstanceProcAddr + get_instance_proc_addr: PFN_vkGetInstanceProcAddr, } impl TryFrom> for Vulkan { @@ -52,21 +51,26 @@ impl TryFrom> for Vulkan { fn try_from(vulkan: VulkanInfo) -> Result> { unsafe { - let instance = ash::Instance::load(&StaticFn { - get_instance_proc_addr: vulkan.get_instance_proc_addr, - }, vulkan.instance.clone()); + let instance = ash::Instance::load( + &StaticFn { + get_instance_proc_addr: vulkan.get_instance_proc_addr, + }, + vulkan.instance.clone(), + ); let device = ash::Device::load(instance.fp_v1_0(), vulkan.device.clone()); let pipeline_cache = unsafe { - device.create_pipeline_cache(&vk::PipelineCacheCreateInfo::default(), - None)? + device.create_pipeline_cache(&vk::PipelineCacheCreateInfo::default(), None)? }; let command_pool = unsafe { - device.create_command_pool(&vk::CommandPoolCreateInfo::builder() - .flags(CommandPoolCreateFlags::RESET_COMMAND_BUFFER) - .build(), None)? + device.create_command_pool( + &vk::CommandPoolCreateInfo::builder() + .flags(CommandPoolCreateFlags::RESET_COMMAND_BUFFER) + .build(), + None, + )? }; Ok(Vulkan { @@ -75,7 +79,7 @@ impl TryFrom> for Vulkan { queue: vulkan.queue.clone(), command_pool, pipeline_cache, - memory_properties: vulkan.memory_properties.clone() + memory_properties: vulkan.memory_properties.clone(), }) } } @@ -89,14 +93,16 @@ impl TryFrom<(ash::Device, vk::Queue, vk::PhysicalDeviceMemoryProperties)> for V let device = value.0; let pipeline_cache = unsafe { - device.create_pipeline_cache(&vk::PipelineCacheCreateInfo::default(), - None)? + device.create_pipeline_cache(&vk::PipelineCacheCreateInfo::default(), None)? }; let command_pool = unsafe { - device.create_command_pool(&vk::CommandPoolCreateInfo::builder() - .flags(CommandPoolCreateFlags::RESET_COMMAND_BUFFER) - .build(), None)? + device.create_command_pool( + &vk::CommandPoolCreateInfo::builder() + .flags(CommandPoolCreateFlags::RESET_COMMAND_BUFFER) + .build(), + None, + )? }; Ok(Vulkan { @@ -104,7 +110,7 @@ impl TryFrom<(ash::Device, vk::Queue, vk::PhysicalDeviceMemoryProperties)> for V queue: value.1, command_pool, pipeline_cache, - memory_properties: value.2 + memory_properties: value.2, }) } } @@ -134,7 +140,7 @@ pub type FilterChainOptionsVulkan = (); impl FilterChainVulkan { /// Load the shader preset at the given path into a filter chain. pub fn load_from_path( - vulkan: impl TryInto>, + vulkan: impl TryInto>, path: impl AsRef, options: Option<&FilterChainOptionsVulkan>, ) -> error::Result { @@ -144,7 +150,7 @@ impl FilterChainVulkan { } pub fn load_from_preset( - vulkan: impl TryInto>, + vulkan: impl TryInto>, preset: ShaderPreset, options: Option<&FilterChainOptionsVulkan>, ) -> error::Result { @@ -158,9 +164,7 @@ impl FilterChainVulkan { eprintln!("filters initialized ok."); Ok(FilterChainVulkan { - common: FilterCommon { - luts - }, + common: FilterCommon { luts }, passes: filters, }) } @@ -194,7 +198,8 @@ impl FilterChainVulkan { Ok::<_, Box>((shader, source, reflect)) }) .into_iter() - .collect::)>>>()?; + .collect::)>>>( + )?; for details in &passes { librashader_runtime::semantics::insert_pass_semantics( @@ -261,10 +266,15 @@ impl FilterChainVulkan { if source.format == ImageFormat::Unknown { source.format = ImageFormat::R8G8B8A8Unorm } - - let graphics_pipeline = VulkanGraphicsPipeline::new(&vulkan.device, - &vulkan.pipeline_cache, - &spirv_words, &reflection, source.format, images)?; + + let graphics_pipeline = VulkanGraphicsPipeline::new( + &vulkan.device, + &vulkan.pipeline_cache, + &spirv_words, + &reflection, + source.format, + images, + )?; // shader_vulkan: 2026 filters.push(FilterPass { @@ -273,11 +283,10 @@ impl FilterChainVulkan { uniform_bindings, source, config, - graphics_pipeline + graphics_pipeline, }); } - Ok(filters.into_boxed_slice()) } @@ -288,28 +297,28 @@ impl FilterChainVulkan { let mut luts = FxHashMap::default(); let command_buffer = unsafe { // panic safety: command buffer count = 1 - vulkan.device.allocate_command_buffers(&vk::CommandBufferAllocateInfo::builder() - .command_pool(vulkan.command_pool) - .level(vk::CommandBufferLevel::PRIMARY) - .command_buffer_count(1) - .build())?[0] + vulkan.device.allocate_command_buffers( + &vk::CommandBufferAllocateInfo::builder() + .command_pool(vulkan.command_pool) + .level(vk::CommandBufferLevel::PRIMARY) + .command_buffer_count(1) + .build(), + )?[0] }; unsafe { - vulkan.device.begin_command_buffer(command_buffer, &vk::CommandBufferBeginInfo::builder() - .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT) - .build())? + vulkan.device.begin_command_buffer( + command_buffer, + &vk::CommandBufferBeginInfo::builder() + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT) + .build(), + )? } for (index, texture) in textures.iter().enumerate() { let image = Image::load(&texture.path, UVDirection::TopLeft)?; - let texture = LutTexture::new( - vulkan, - &command_buffer, - image, - texture, - )?; + let texture = LutTexture::new(vulkan, &command_buffer, image, texture)?; luts.insert(index, texture); } @@ -317,14 +326,16 @@ impl FilterChainVulkan { vulkan.device.end_command_buffer(command_buffer)?; let buffers = [command_buffer]; - let submits = [vk::SubmitInfo::builder() - .command_buffers(&buffers).build()]; + let submits = [vk::SubmitInfo::builder().command_buffers(&buffers).build()]; - vulkan.device.queue_submit(vulkan.queue, &submits, vk::Fence::null())?; + vulkan + .device + .queue_submit(vulkan.queue, &submits, vk::Fence::null())?; vulkan.device.queue_wait_idle(vulkan.queue)?; - vulkan.device.free_command_buffers(vulkan.command_pool, &buffers); + vulkan + .device + .free_command_buffers(vulkan.command_pool, &buffers); } Ok(luts) } - -} \ No newline at end of file +} diff --git a/librashader-runtime-vk/src/filter_pass.rs b/librashader-runtime-vk/src/filter_pass.rs index df95269..82842b9 100644 --- a/librashader-runtime-vk/src/filter_pass.rs +++ b/librashader-runtime-vk/src/filter_pass.rs @@ -1,10 +1,10 @@ -use rustc_hash::FxHashMap; +use crate::vulkan_state::VulkanGraphicsPipeline; use librashader_preprocess::ShaderSource; use librashader_presets::ShaderPassConfig; use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::reflect::semantics::{MemberOffset, UniformBinding}; use librashader_runtime::uniforms::UniformStorage; -use crate::vulkan_state::VulkanGraphicsPipeline; +use rustc_hash::FxHashMap; pub struct FilterPass { pub(crate) compiled: ShaderCompilerOutput>, diff --git a/librashader-runtime-vk/src/framebuffer.rs b/librashader-runtime-vk/src/framebuffer.rs index 8dc7000..8e4177a 100644 --- a/librashader-runtime-vk/src/framebuffer.rs +++ b/librashader-runtime-vk/src/framebuffer.rs @@ -1,11 +1,14 @@ -use ash::vk; -use ash::vk::{Extent3D, ImageAspectFlags, ImageLayout, ImageTiling, ImageType, ImageUsageFlags, ImageViewType, SampleCountFlags, SharingMode}; -use librashader_common::Size; -use librashader_runtime::scaling::MipmapSize; use crate::error; use crate::renderpass::VulkanRenderPass; use crate::util::find_vulkan_memory_type; use crate::vulkan_primitives::VulkanImageMemory; +use ash::vk; +use ash::vk::{ + Extent3D, ImageAspectFlags, ImageLayout, ImageTiling, ImageType, ImageUsageFlags, + ImageViewType, SampleCountFlags, SharingMode, +}; +use librashader_common::Size; +use librashader_runtime::scaling::MipmapSize; pub struct Framebuffer { device: ash::Device, @@ -13,7 +16,7 @@ pub struct Framebuffer { max_levels: u32, mem_props: vk::PhysicalDeviceMemoryProperties, render_pass: VulkanRenderPass, - framebuffer: Option + framebuffer: Option, } pub struct VulkanFramebuffer { @@ -45,14 +48,20 @@ impl Drop for VulkanFramebuffer { } impl Framebuffer { - pub fn new(device: &ash::Device, size: Size, render_pass: VulkanRenderPass, mip_levels: u32, mem_props: vk::PhysicalDeviceMemoryProperties) -> error::Result { + pub fn new( + device: &ash::Device, + size: Size, + render_pass: VulkanRenderPass, + mip_levels: u32, + mem_props: vk::PhysicalDeviceMemoryProperties, + ) -> error::Result { let mut framebuffer = Framebuffer { device: device.clone(), size, max_levels: mip_levels, mem_props, render_pass, - framebuffer: None + framebuffer: None, }; let vulkan_image = framebuffer.create_vulkan_image()?; @@ -66,11 +75,19 @@ impl Framebuffer { .image_type(ImageType::TYPE_2D) .format(self.render_pass.format.into()) .extent(self.size.into()) - .mip_levels(std::cmp::min(self.max_levels, self.size.calculate_miplevels())) + .mip_levels(std::cmp::min( + self.max_levels, + self.size.calculate_miplevels(), + )) .array_layers(1) .samples(SampleCountFlags::TYPE_1) .tiling(ImageTiling::OPTIMAL) - .usage(ImageUsageFlags::SAMPLED | ImageUsageFlags::COLOR_ATTACHMENT | ImageUsageFlags::TRANSFER_DST | ImageUsageFlags::TRANSFER_SRC) + .usage( + ImageUsageFlags::SAMPLED + | ImageUsageFlags::COLOR_ATTACHMENT + | ImageUsageFlags::TRANSFER_DST + | ImageUsageFlags::TRANSFER_SRC, + ) .sharing_mode(SharingMode::EXCLUSIVE) .initial_layout(ImageLayout::UNDEFINED) .build(); @@ -80,7 +97,11 @@ impl Framebuffer { let alloc_info = vk::MemoryAllocateInfo::builder() .allocation_size(mem_reqs.size) - .memory_type_index(find_vulkan_memory_type(&self.mem_props, mem_reqs.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)) + .memory_type_index(find_vulkan_memory_type( + &self.mem_props, + mem_reqs.memory_type_bits, + vk::MemoryPropertyFlags::DEVICE_LOCAL, + )) .build(); // todo: optimize by reusing existing memory. @@ -109,22 +130,22 @@ impl Framebuffer { .components(swizzle_components) .build(); - let image_view = unsafe { - self.device.create_image_view(&view_info, None)? - }; + let image_view = unsafe { self.device.create_image_view(&view_info, None)? }; view_info.subresource_range.level_count = 1; - let fb_view = unsafe { - self.device.create_image_view(&view_info, None)? - }; + let fb_view = unsafe { self.device.create_image_view(&view_info, None)? }; let framebuffer = unsafe { - self.device.create_framebuffer(&vk::FramebufferCreateInfo::builder() - .render_pass(self.render_pass.render_pass) - .attachments(&[image_view]) - .width(self.size.width) - .height(self.size.height) - .layers(1).build(), None)? + self.device.create_framebuffer( + &vk::FramebufferCreateInfo::builder() + .render_pass(self.render_pass.render_pass) + .attachments(&[image_view]) + .width(self.size.width) + .height(self.size.height) + .layers(1) + .build(), + None, + )? }; Ok(VulkanFramebuffer { @@ -133,8 +154,7 @@ impl Framebuffer { memory, image_view, fb_view, - image + image, }) - } -} \ No newline at end of file +} diff --git a/librashader-runtime-vk/src/hello_triangle.rs b/librashader-runtime-vk/src/hello_triangle.rs index d5ce6c2..74c9d22 100644 --- a/librashader-runtime-vk/src/hello_triangle.rs +++ b/librashader-runtime-vk/src/hello_triangle.rs @@ -1,3 +1,4 @@ +use crate::offset_of; use ash::util::*; use ash::vk; use std::default::Default; @@ -5,7 +6,6 @@ use std::ffi::CStr; use std::io::Cursor; use std::mem; use std::mem::align_of; -use crate::offset_of; mod base; pub use base::*; @@ -99,7 +99,7 @@ pub(crate) fn main(base: ExampleBase) { &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, ) - .expect("Unable to find suitable memorytype for the index buffer."); + .expect("Unable to find suitable memorytype for the index buffer."); let index_allocate_info = vk::MemoryAllocateInfo { allocation_size: index_buffer_memory_req.size, @@ -151,7 +151,7 @@ pub(crate) fn main(base: ExampleBase) { &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, ) - .expect("Unable to find suitable memorytype for the vertex buffer."); + .expect("Unable to find suitable memorytype for the vertex buffer."); let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: vertex_input_buffer_memory_req.size, @@ -164,7 +164,6 @@ pub(crate) fn main(base: ExampleBase) { .allocate_memory(&vertex_buffer_allocate_info, None) .unwrap(); - let vertices = [ // green Vertex { @@ -202,8 +201,7 @@ pub(crate) fn main(base: ExampleBase) { base.device .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) .unwrap(); - let mut vertex_spv_file = - Cursor::new(&include_bytes!("../shader/triangle/vert.spv")[..]); + let mut vertex_spv_file = Cursor::new(&include_bytes!("../shader/triangle/vert.spv")[..]); let mut frag_spv_file = Cursor::new(&include_bytes!("../shader/triangle/frag.spv")[..]); let vertex_code = diff --git a/librashader-runtime-vk/src/hello_triangle/base.rs b/librashader-runtime-vk/src/hello_triangle/base.rs index 71eb31f..6450890 100644 --- a/librashader-runtime-vk/src/hello_triangle/base.rs +++ b/librashader-runtime-vk/src/hello_triangle/base.rs @@ -21,13 +21,13 @@ use ash::vk::{ KhrGetPhysicalDeviceProperties2Fn, KhrPortabilityEnumerationFn, KhrPortabilitySubsetFn, }; +use winit::event_loop::EventLoopBuilder; use winit::{ event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event_loop::{ControlFlow, EventLoop}, platform::run_return::EventLoopExtRunReturn, window::WindowBuilder, }; -use winit::event_loop::EventLoopBuilder; // Simple offset_of macro akin to C++ offsetof #[macro_export] @@ -195,16 +195,16 @@ impl ExampleBase { match event { Event::WindowEvent { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - input: - KeyboardInput { - state: ElementState::Pressed, - virtual_keycode: Some(VirtualKeyCode::Escape), + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { + input: + KeyboardInput { + state: ElementState::Pressed, + virtual_keycode: Some(VirtualKeyCode::Escape), + .. + }, .. }, - .. - }, .. } => *control_flow = ControlFlow::Exit, Event::MainEventsCleared => f(), @@ -215,9 +215,7 @@ impl ExampleBase { pub fn new(window_width: u32, window_height: u32) -> Self { unsafe { - let mut event_loop = EventLoopBuilder::new() - .with_any_thread(true) - .build(); + let mut event_loop = EventLoopBuilder::new().with_any_thread(true).build(); let window = WindowBuilder::new() .with_title("Ash - Example") .with_inner_size(winit::dpi::LogicalSize::new( @@ -298,7 +296,7 @@ impl ExampleBase { window.raw_window_handle(), None, ) - .unwrap(); + .unwrap(); let pdevices = instance .enumerate_physical_devices() .expect("Physical device error"); @@ -314,12 +312,12 @@ impl ExampleBase { let supports_graphic_and_surface = info.queue_flags.contains(vk::QueueFlags::GRAPHICS) && surface_loader - .get_physical_device_surface_support( - *pdevice, - index as u32, - surface, - ) - .unwrap(); + .get_physical_device_surface_support( + *pdevice, + index as u32, + surface, + ) + .unwrap(); if supports_graphic_and_surface { Some((*pdevice, index)) } else { @@ -332,7 +330,7 @@ impl ExampleBase { let device_extension_names_raw = [ Swapchain::name().as_ptr(), #[cfg(any(target_os = "macos", target_os = "ios"))] - KhrPortabilitySubsetFn::name().as_ptr(), + KhrPortabilitySubsetFn::name().as_ptr(), ]; let features = vk::PhysicalDeviceFeatures { shader_clip_distance: 1, @@ -471,7 +469,7 @@ impl ExampleBase { &device_memory_properties, vk::MemoryPropertyFlags::DEVICE_LOCAL, ) - .expect("Unable to find suitable memory index for depth image."); + .expect("Unable to find suitable memory index for depth image."); let depth_image_allocate_info = vk::MemoryAllocateInfo::builder() .allocation_size(depth_image_memory_req.size) @@ -620,4 +618,4 @@ impl Drop for ExampleBase { self.instance.destroy_instance(None); } } -} \ No newline at end of file +} diff --git a/librashader-runtime-vk/src/lib.rs b/librashader-runtime-vk/src/lib.rs index ce7c21c..4df7671 100644 --- a/librashader-runtime-vk/src/lib.rs +++ b/librashader-runtime-vk/src/lib.rs @@ -1,22 +1,22 @@ #![feature(type_alias_impl_trait)] #![feature(let_chains)] -mod hello_triangle; +mod draw_quad; +mod error; mod filter_chain; mod filter_pass; -mod error; -mod util; mod framebuffer; -mod vulkan_state; -mod draw_quad; -mod renderpass; -mod vulkan_primitives; +mod hello_triangle; mod luts; +mod renderpass; +mod util; +mod vulkan_primitives; +mod vulkan_state; #[cfg(test)] mod tests { - use crate::filter_chain::FilterChainVulkan; use super::*; + use crate::filter_chain::FilterChainVulkan; #[test] fn triangle_vk() { let base = hello_triangle::ExampleBase::new(900, 600); @@ -27,13 +27,16 @@ mod tests { // ) let mut filter = FilterChainVulkan::load_from_path( - (base.device.clone(), base.present_queue.clone(), base.device_memory_properties.clone()), + ( + base.device.clone(), + base.present_queue.clone(), + base.device_memory_properties.clone(), + ), "../test/slang-shaders/border/gameboy-player/gameboy-player-crt-royale.slangp", - None + None, ) - // FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None) - .unwrap(); + .unwrap(); hello_triangle::main(base); } } diff --git a/librashader-runtime-vk/src/luts.rs b/librashader-runtime-vk/src/luts.rs index 4947e17..31b7f3c 100644 --- a/librashader-runtime-vk/src/luts.rs +++ b/librashader-runtime-vk/src/luts.rs @@ -1,13 +1,13 @@ +use crate::filter_chain::Vulkan; +use crate::vulkan_primitives::{VulkanBuffer, VulkanImageMemory}; +use crate::{error, util}; use ash::vk; use ash::vk::ImageSubresourceLayers; use glfw::Key::P; use librashader_common::{FilterMode, WrapMode}; use librashader_presets::TextureConfig; -use librashader_runtime::image::{BGRA8, Image}; +use librashader_runtime::image::{Image, BGRA8}; use librashader_runtime::scaling::MipmapSize; -use crate::filter_chain::Vulkan; -use crate::{error, util}; -use crate::vulkan_primitives::{VulkanBuffer, VulkanImageMemory}; pub struct LutTexture { pub texture: vk::Image, @@ -20,8 +20,12 @@ pub struct LutTexture { } impl LutTexture { - pub fn new(vulkan: &Vulkan, cmd: &vk::CommandBuffer, image: Image, config: &TextureConfig) -> error::Result { - + pub fn new( + vulkan: &Vulkan, + cmd: &vk::CommandBuffer, + image: Image, + config: &TextureConfig, + ) -> error::Result { // todo: might need to use bgra8 let image_info = vk::ImageCreateInfo::builder() .image_type(vk::ImageType::TYPE_2D) @@ -35,20 +39,29 @@ impl LutTexture { .array_layers(1) .samples(vk::SampleCountFlags::TYPE_1) .tiling(vk::ImageTiling::OPTIMAL) - .usage(vk::ImageUsageFlags::SAMPLED | vk::ImageUsageFlags::TRANSFER_SRC | vk::ImageUsageFlags::TRANSFER_DST) + .usage( + vk::ImageUsageFlags::SAMPLED + | vk::ImageUsageFlags::TRANSFER_SRC + | vk::ImageUsageFlags::TRANSFER_DST, + ) .initial_layout(vk::ImageLayout::UNDEFINED) .build(); - let texture = unsafe { - vulkan.device.create_image(&image_info, None)? - }; + let texture = unsafe { vulkan.device.create_image(&image_info, None)? }; let memory = unsafe { let mem_reqs = vulkan.device.get_image_memory_requirements(texture.clone()); - let mem_type = util::find_vulkan_memory_type(&vulkan.memory_properties, mem_reqs.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL); - crate::vulkan_primitives::VulkanImageMemory::new(&vulkan.device, &vk::MemoryAllocateInfo::builder() - .memory_type_index(mem_type) - .allocation_size(mem_reqs.size))? + let mem_type = util::find_vulkan_memory_type( + &vulkan.memory_properties, + mem_reqs.memory_type_bits, + vk::MemoryPropertyFlags::DEVICE_LOCAL, + ); + crate::vulkan_primitives::VulkanImageMemory::new( + &vulkan.device, + &vk::MemoryAllocateInfo::builder() + .memory_type_index(mem_type) + .allocation_size(mem_reqs.size), + )? }; memory.bind(&texture)?; @@ -74,20 +87,26 @@ impl LutTexture { .components(swizzle_components) .build(); - let texture_view = unsafe { - vulkan.device.create_image_view(&view_info, None)? - }; + let texture_view = unsafe { vulkan.device.create_image_view(&view_info, None)? }; - let mut staging = VulkanBuffer::new(&vulkan.device, &vulkan.memory_properties, vk::BufferUsageFlags::TRANSFER_SRC, image.bytes.len())?; + let mut staging = VulkanBuffer::new( + &vulkan.device, + &vulkan.memory_properties, + vk::BufferUsageFlags::TRANSFER_SRC, + image.bytes.len(), + )?; unsafe { let mut handle = staging.map()?; handle.copy_from(&image.bytes) } unsafe { - util::vulkan_image_layout_transition_levels(&vulkan.device, *cmd, texture, - vk::REMAINING_MIP_LEVELS, - vk::ImageLayout::UNDEFINED, + util::vulkan_image_layout_transition_levels( + &vulkan.device, + *cmd, + texture, + vk::REMAINING_MIP_LEVELS, + vk::ImageLayout::UNDEFINED, if config.mipmap { vk::ImageLayout::GENERAL } else { @@ -97,23 +116,31 @@ impl LutTexture { vk::AccessFlags::TRANSFER_WRITE, vk::PipelineStageFlags::TOP_OF_PIPE, vk::PipelineStageFlags::TRANSFER, - vk::QUEUE_FAMILY_IGNORED, - vk::QUEUE_FAMILY_IGNORED + vk::QUEUE_FAMILY_IGNORED, + vk::QUEUE_FAMILY_IGNORED, ); - vulkan.device.cmd_copy_buffer_to_image(*cmd, - staging.handle, - texture, - if config.mipmap { vk::ImageLayout::GENERAL } else { vk::ImageLayout::TRANSFER_DST_OPTIMAL }, - &[vk::BufferImageCopy::builder() - .image_subresource(vk::ImageSubresourceLayers::builder() - .aspect_mask(vk::ImageAspectFlags::COLOR) - .mip_level(0) - .base_array_layer(0) - .layer_count(1) - .build()) - .image_extent(image.size.into()).build()]) - + vulkan.device.cmd_copy_buffer_to_image( + *cmd, + staging.handle, + texture, + if config.mipmap { + vk::ImageLayout::GENERAL + } else { + vk::ImageLayout::TRANSFER_DST_OPTIMAL + }, + &[vk::BufferImageCopy::builder() + .image_subresource( + vk::ImageSubresourceLayers::builder() + .aspect_mask(vk::ImageAspectFlags::COLOR) + .mip_level(0) + .base_array_layer(0) + .layer_count(1) + .build(), + ) + .image_extent(image.size.into()) + .build()], + ) } // generate mipmaps @@ -122,11 +149,7 @@ impl LutTexture { let target_size = image.size.scale_mipmap(level); let src_offsets = [ - vk::Offset3D { - x: 0, - y: 0, - z: 0, - }, + vk::Offset3D { x: 0, y: 0, z: 0 }, vk::Offset3D { x: source_size.width as i32, y: source_size.height as i32, @@ -135,11 +158,7 @@ impl LutTexture { ]; let dst_offsets = [ - vk::Offset3D { - x: 0, - y: 0, - z: 0, - }, + vk::Offset3D { x: 0, y: 0, z: 0 }, vk::Offset3D { x: target_size.width as i32, y: target_size.height as i32, @@ -168,34 +187,52 @@ impl LutTexture { .build()]; unsafe { - util::vulkan_image_layout_transition_levels(&vulkan.device, *cmd, texture, - vk::REMAINING_MIP_LEVELS, - vk::ImageLayout::GENERAL, - vk::ImageLayout::GENERAL, - vk::AccessFlags::TRANSFER_WRITE, - vk::AccessFlags::TRANSFER_READ, - vk::PipelineStageFlags::TRANSFER, - vk::PipelineStageFlags::TRANSFER, - vk::QUEUE_FAMILY_IGNORED, - vk::QUEUE_FAMILY_IGNORED + util::vulkan_image_layout_transition_levels( + &vulkan.device, + *cmd, + texture, + vk::REMAINING_MIP_LEVELS, + vk::ImageLayout::GENERAL, + vk::ImageLayout::GENERAL, + vk::AccessFlags::TRANSFER_WRITE, + vk::AccessFlags::TRANSFER_READ, + vk::PipelineStageFlags::TRANSFER, + vk::PipelineStageFlags::TRANSFER, + vk::QUEUE_FAMILY_IGNORED, + vk::QUEUE_FAMILY_IGNORED, ); // todo: respect mipmap filter? - vulkan.device.cmd_blit_image(*cmd, texture, vk::ImageLayout::GENERAL, texture, vk::ImageLayout::GENERAL, &image_blit, vk::Filter::LINEAR); + vulkan.device.cmd_blit_image( + *cmd, + texture, + vk::ImageLayout::GENERAL, + texture, + vk::ImageLayout::GENERAL, + &image_blit, + vk::Filter::LINEAR, + ); } } unsafe { - util::vulkan_image_layout_transition_levels(&vulkan.device, *cmd, texture, - vk::REMAINING_MIP_LEVELS, - if config.mipmap { vk::ImageLayout::GENERAL } else { vk::ImageLayout::TRANSFER_DST_OPTIMAL }, - vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, - vk::AccessFlags::TRANSFER_WRITE, - vk::AccessFlags::TRANSFER_READ, - vk::PipelineStageFlags::TRANSFER, - vk::PipelineStageFlags::FRAGMENT_SHADER, - vk::QUEUE_FAMILY_IGNORED, - vk::QUEUE_FAMILY_IGNORED + util::vulkan_image_layout_transition_levels( + &vulkan.device, + *cmd, + texture, + vk::REMAINING_MIP_LEVELS, + if config.mipmap { + vk::ImageLayout::GENERAL + } else { + vk::ImageLayout::TRANSFER_DST_OPTIMAL + }, + vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, + vk::AccessFlags::TRANSFER_WRITE, + vk::AccessFlags::TRANSFER_READ, + vk::PipelineStageFlags::TRANSFER, + vk::PipelineStageFlags::FRAGMENT_SHADER, + vk::QUEUE_FAMILY_IGNORED, + vk::QUEUE_FAMILY_IGNORED, ); } @@ -206,7 +243,7 @@ impl LutTexture { staging, filter_mode: config.filter_mode, wrap_mode: config.wrap_mode, - mipmap: config.mipmap + mipmap: config.mipmap, }) } -} \ No newline at end of file +} diff --git a/librashader-runtime-vk/src/renderpass.rs b/librashader-runtime-vk/src/renderpass.rs index b0c30b2..e5b451d 100644 --- a/librashader-runtime-vk/src/renderpass.rs +++ b/librashader-runtime-vk/src/renderpass.rs @@ -1,15 +1,20 @@ -use ash::vk; -use ash::vk::{AttachmentLoadOp, AttachmentStoreOp, ImageLayout, PipelineBindPoint, SampleCountFlags}; -use librashader_common::ImageFormat; use crate::error; +use ash::vk; +use ash::vk::{ + AttachmentLoadOp, AttachmentStoreOp, ImageLayout, PipelineBindPoint, SampleCountFlags, +}; +use librashader_common::ImageFormat; pub struct VulkanRenderPass { pub render_pass: vk::RenderPass, - pub format: ImageFormat + pub format: ImageFormat, } impl VulkanRenderPass { - pub fn create_render_pass(device: &ash::Device, mut format: ImageFormat) -> error::Result { + pub fn create_render_pass( + device: &ash::Device, + mut format: ImageFormat, + ) -> error::Result { // default to reasonable choice if unknown if format == ImageFormat::Unknown { format = ImageFormat::R8G8B8A8Unorm; @@ -47,7 +52,7 @@ impl VulkanRenderPass { let rp = device.create_render_pass(&renderpass_info, None)?; Ok(Self { render_pass: rp, - format + format, }) } } diff --git a/librashader-runtime-vk/src/util.rs b/librashader-runtime-vk/src/util.rs index 31e053b..5b6a9d6 100644 --- a/librashader-runtime-vk/src/util.rs +++ b/librashader-runtime-vk/src/util.rs @@ -1,8 +1,8 @@ +use crate::error; use ash::vk; use ash::vk::{AccessFlags, Extent3D, ImageAspectFlags}; use librashader_common::Size; use librashader_reflect::reflect::semantics::BindingStage; -use crate::error; pub fn binding_stage_to_vulkan_stage(stage_mask: BindingStage) -> vk::ShaderStageFlags { let mut mask = vk::ShaderStageFlags::default(); @@ -17,11 +17,16 @@ pub fn binding_stage_to_vulkan_stage(stage_mask: BindingStage) -> vk::ShaderStag mask } -pub fn find_vulkan_memory_type(props: &vk::PhysicalDeviceMemoryProperties, device_reqs: u32, host_reqs: vk::MemoryPropertyFlags) -> u32 { +pub fn find_vulkan_memory_type( + props: &vk::PhysicalDeviceMemoryProperties, + device_reqs: u32, + host_reqs: vk::MemoryPropertyFlags, +) -> u32 { for i in 0..vk::MAX_MEMORY_TYPES { if device_reqs & (1 << i) != 0 - && props.memory_types[i].property_flags & host_reqs == host_reqs { - return i as u32 + && props.memory_types[i].property_flags & host_reqs == host_reqs + { + return i as u32; } } @@ -33,21 +38,21 @@ pub fn find_vulkan_memory_type(props: &vk::PhysicalDeviceMemoryProperties, devic } #[inline(always)] -pub unsafe fn vulkan_image_layout_transition_levels(device: &ash::Device, - cmd: vk::CommandBuffer, - image: vk::Image, - levels: u32, old_layout: vk::ImageLayout, - new_layout: vk::ImageLayout, - src_access: vk::AccessFlags, - dst_access: vk::AccessFlags, - src_stage: vk::PipelineStageFlags, - dst_stage: vk::PipelineStageFlags, +pub unsafe fn vulkan_image_layout_transition_levels( + device: &ash::Device, + cmd: vk::CommandBuffer, + image: vk::Image, + levels: u32, + old_layout: vk::ImageLayout, + new_layout: vk::ImageLayout, + src_access: vk::AccessFlags, + dst_access: vk::AccessFlags, + src_stage: vk::PipelineStageFlags, + dst_stage: vk::PipelineStageFlags, - - src_queue_family_index: u32, - dst_queue_family_index: u32, - - ) { + src_queue_family_index: u32, + dst_queue_family_index: u32, +) { let mut barrier = vk::ImageMemoryBarrier::default(); barrier.s_type = vk::StructureType::IMAGE_MEMORY_BARRIER; barrier.p_next = std::ptr::null(); @@ -62,6 +67,13 @@ pub unsafe fn vulkan_image_layout_transition_levels(device: &ash::Device, barrier.subresource_range.base_array_layer = 0; barrier.subresource_range.level_count = levels; barrier.subresource_range.layer_count = vk::REMAINING_ARRAY_LAYERS; - device.cmd_pipeline_barrier(cmd, src_stage, dst_stage, vk::DependencyFlags::empty(), - &[], &[], &[barrier]) + device.cmd_pipeline_barrier( + cmd, + src_stage, + dst_stage, + vk::DependencyFlags::empty(), + &[], + &[], + &[barrier], + ) } diff --git a/librashader-runtime-vk/src/vulkan_primitives.rs b/librashader-runtime-vk/src/vulkan_primitives.rs index e3d1ec4..63022dd 100644 --- a/librashader-runtime-vk/src/vulkan_primitives.rs +++ b/librashader-runtime-vk/src/vulkan_primitives.rs @@ -1,25 +1,30 @@ -use std::ffi::c_void; -use ash::vk; use crate::{error, util}; +use ash::vk; +use std::ffi::c_void; pub struct VulkanImageMemory { pub handle: vk::DeviceMemory, - device: ash::Device + device: ash::Device, } impl VulkanImageMemory { - pub fn new(device: &ash::Device, alloc: &vk::MemoryAllocateInfo) -> error::Result { + pub fn new( + device: &ash::Device, + alloc: &vk::MemoryAllocateInfo, + ) -> error::Result { unsafe { Ok(VulkanImageMemory { handle: device.allocate_memory(alloc, None)?, - device: device.clone() + device: device.clone(), }) } } - pub fn bind(&self, image: &vk::Image) -> error::Result<()>{ + pub fn bind(&self, image: &vk::Image) -> error::Result<()> { unsafe { - Ok(self.device.bind_image_memory(image.clone(), self.handle.clone(), 0)?) + Ok(self + .device + .bind_image_memory(image.clone(), self.handle.clone(), 0)?) } } } @@ -32,7 +37,6 @@ impl Drop for VulkanImageMemory { } } - pub struct VulkanBuffer { pub handle: vk::Buffer, device: ash::Device, @@ -42,12 +46,16 @@ pub struct VulkanBuffer { pub struct VulkanBufferMapHandle<'a> { buffer: &'a mut VulkanBuffer, - ptr: *mut c_void + ptr: *mut c_void, } impl VulkanBuffer { - pub fn new(device: &ash::Device, mem_props: &vk::PhysicalDeviceMemoryProperties, usage: vk::BufferUsageFlags, - size: usize) -> error::Result { + pub fn new( + device: &ash::Device, + mem_props: &vk::PhysicalDeviceMemoryProperties, + usage: vk::BufferUsageFlags, + size: usize, + ) -> error::Result { unsafe { let buffer_info = vk::BufferCreateInfo::builder() .size(size as vk::DeviceSize) @@ -59,8 +67,11 @@ impl VulkanBuffer { let memory_reqs = device.get_buffer_memory_requirements(buffer); let alloc_info = vk::MemoryAllocateInfo::builder() .allocation_size(memory_reqs.size) - .memory_type_index(util::find_vulkan_memory_type(mem_props, memory_reqs.memory_type_bits, - vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT)) + .memory_type_index(util::find_vulkan_memory_type( + mem_props, + memory_reqs.memory_type_bits, + vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, + )) .build(); let alloc = device.allocate_memory(&alloc_info, None)?; @@ -70,23 +81,25 @@ impl VulkanBuffer { handle: buffer, memory: alloc, size: size as vk::DeviceSize, - device: device.clone() + device: device.clone(), }) } } pub unsafe fn copy_from(&mut self, buffer: &[u8]) -> error::Result<()> { - let dst = self.device.map_memory(self.memory, 0, self.size, vk::MemoryMapFlags::empty())?; + let dst = self + .device + .map_memory(self.memory, 0, self.size, vk::MemoryMapFlags::empty())?; std::ptr::copy_nonoverlapping(buffer.as_ptr(), dst.cast(), buffer.len()); self.device.unmap_memory(self.memory); Ok(()) } pub fn map(&mut self) -> error::Result { - let dst = - unsafe { - self.device.map_memory(self.memory, 0, self.size, vk::MemoryMapFlags::empty())? - }; + let dst = unsafe { + self.device + .map_memory(self.memory, 0, self.size, vk::MemoryMapFlags::empty())? + }; Ok(VulkanBufferMapHandle { buffer: self, @@ -109,16 +122,14 @@ impl Drop for VulkanBuffer { } } -impl <'a> VulkanBufferMapHandle<'a> { +impl<'a> VulkanBufferMapHandle<'a> { pub unsafe fn copy_from(&mut self, src: &[u8]) { std::ptr::copy_nonoverlapping(src.as_ptr(), self.ptr.cast(), src.len()); } } -impl <'a> Drop for VulkanBufferMapHandle<'a> { +impl<'a> Drop for VulkanBufferMapHandle<'a> { fn drop(&mut self) { - unsafe { - self.buffer.device.unmap_memory(self.buffer.memory) - } + unsafe { self.buffer.device.unmap_memory(self.buffer.memory) } } -} \ No newline at end of file +} diff --git a/librashader-runtime-vk/src/vulkan_state.rs b/librashader-runtime-vk/src/vulkan_state.rs index b3b6a9d..ff92dd3 100644 --- a/librashader-runtime-vk/src/vulkan_state.rs +++ b/librashader-runtime-vk/src/vulkan_state.rs @@ -1,16 +1,16 @@ -use std::ffi::CStr; +use crate::renderpass::VulkanRenderPass; +use crate::{error, util}; use ash::vk; use librashader_common::ImageFormat; use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::reflect::semantics::{TextureBinding, UboReflection}; use librashader_reflect::reflect::ShaderReflection; -use crate::{error, util}; -use crate::renderpass::VulkanRenderPass; +use std::ffi::CStr; pub struct PipelineDescriptors { pub replicas: u32, pub layout_bindings: Vec, - pub pool_sizes: Vec + pub pool_sizes: Vec, } impl PipelineDescriptors { @@ -67,13 +67,17 @@ impl PipelineDescriptors { self.layout_bindings.as_ref() } - pub fn create_descriptor_set_layout(&self, device: &ash::Device) -> error::Result { + pub fn create_descriptor_set_layout( + &self, + device: &ash::Device, + ) -> error::Result { unsafe { let layout = device.create_descriptor_set_layout( &vk::DescriptorSetLayoutCreateInfo::builder() .bindings(self.bindings()) .build(), - None)?; + None, + )?; Ok(layout) } } @@ -87,38 +91,43 @@ pub struct PipelineLayoutObjects { } impl PipelineLayoutObjects { - pub fn new(reflection: &ShaderReflection, replicas: u32, device: &ash::Device) -> error::Result { + pub fn new( + reflection: &ShaderReflection, + replicas: u32, + device: &ash::Device, + ) -> error::Result { let mut descriptors = PipelineDescriptors::new(replicas); descriptors.add_ubo_binding(reflection.ubo.as_ref()); descriptors.add_texture_bindings(reflection.meta.texture_meta.values()); let mut descriptor_set_layout = [descriptors.create_descriptor_set_layout(device)?]; - let mut pipeline_create_info = vk::PipelineLayoutCreateInfo::builder() - .set_layouts(&descriptor_set_layout); + let mut pipeline_create_info = + vk::PipelineLayoutCreateInfo::builder().set_layouts(&descriptor_set_layout); let pipeline_create_info = if let Some(push_constant) = &reflection.push_constant { let mut stage_mask = util::binding_stage_to_vulkan_stage(push_constant.stage_mask); - let push_constant_range = [ - vk::PushConstantRange::builder() - .stage_flags(stage_mask) - .size(push_constant.size) - .build() - ]; - pipeline_create_info.push_constant_ranges(&push_constant_range).build() + let push_constant_range = [vk::PushConstantRange::builder() + .stage_flags(stage_mask) + .size(push_constant.size) + .build()]; + pipeline_create_info + .push_constant_ranges(&push_constant_range) + .build() } else { pipeline_create_info.build() }; - let layout = unsafe { - device.create_pipeline_layout(&pipeline_create_info, None)? - }; + let layout = unsafe { device.create_pipeline_layout(&pipeline_create_info, None)? }; let pool = unsafe { - device.create_descriptor_pool(&vk::DescriptorPoolCreateInfo::builder() - .max_sets(replicas) - .pool_sizes(&descriptors.pool_sizes) - .build(), None)? + device.create_descriptor_pool( + &vk::DescriptorPoolCreateInfo::builder() + .max_sets(replicas) + .pool_sizes(&descriptors.pool_sizes) + .build(), + None, + )? }; let mut descriptor_sets = Vec::new(); @@ -128,19 +137,18 @@ impl PipelineLayoutObjects { .build(); for _ in 0..replicas { - unsafe { - descriptor_sets.push(device.allocate_descriptor_sets(&alloc_info)?) - } + unsafe { descriptor_sets.push(device.allocate_descriptor_sets(&alloc_info)?) } } - let descriptor_sets: Vec = descriptor_sets.into_iter().flatten().collect(); + let descriptor_sets: Vec = + descriptor_sets.into_iter().flatten().collect(); return Ok(PipelineLayoutObjects { layout, descriptor_set_layout, descriptor_sets, pool, - }) + }); } } @@ -150,30 +158,38 @@ pub struct VulkanShaderModule { } impl VulkanShaderModule { - pub fn new(device: &ash::Device, info: &vk::ShaderModuleCreateInfo) -> error::Result { + pub fn new( + device: &ash::Device, + info: &vk::ShaderModuleCreateInfo, + ) -> error::Result { Ok(VulkanShaderModule { shader: unsafe { device.create_shader_module(info, None)? }, - device: device.clone() + device: device.clone(), }) } } impl Drop for VulkanShaderModule { fn drop(&mut self) { - unsafe { - self.device.destroy_shader_module(self.shader, None) - } + unsafe { self.device.destroy_shader_module(self.shader, None) } } } pub struct VulkanGraphicsPipeline { layout: PipelineLayoutObjects, render_pass: VulkanRenderPass, - pipeline: vk::Pipeline + pipeline: vk::Pipeline, } impl VulkanGraphicsPipeline { - pub fn new(device: &ash::Device, cache: &vk::PipelineCache, shader_assembly: &ShaderCompilerOutput>, reflection: &ShaderReflection, mut format: ImageFormat, replicas: u32) -> error::Result { + pub fn new( + device: &ash::Device, + cache: &vk::PipelineCache, + shader_assembly: &ShaderCompilerOutput>, + reflection: &ShaderReflection, + mut format: ImageFormat, + replicas: u32, + ) -> error::Result { // default to something sane if format == ImageFormat::Unknown { format = ImageFormat::R8G8B8A8Unorm @@ -186,17 +202,20 @@ impl VulkanGraphicsPipeline { .topology(vk::PrimitiveTopology::TRIANGLE_STRIP) .build(); - let vao_state = [vk::VertexInputAttributeDescription { - location: 0, - binding: 0, - format: vk::Format::R32G32_SFLOAT, - offset: 0, - }, vk::VertexInputAttributeDescription { - location: 1, - binding: 0, - format: vk::Format::R32G32_SFLOAT, - offset: (2 * std::mem::size_of::()) as u32, - }]; + let vao_state = [ + vk::VertexInputAttributeDescription { + location: 0, + binding: 0, + format: vk::Format::R32G32_SFLOAT, + offset: 0, + }, + vk::VertexInputAttributeDescription { + location: 1, + binding: 0, + format: vk::Format::R32G32_SFLOAT, + offset: (2 * std::mem::size_of::()) as u32, + }, + ]; let input_binding = vk::VertexInputBindingDescription::builder() .binding(0) @@ -289,15 +308,16 @@ impl VulkanGraphicsPipeline { let pipeline = unsafe { // panic_safety: if this is successful this should return 1 pipelines. - device.create_graphics_pipelines(cache.clone(), &[pipeline_info], None) - .map_err(|e| e.1).unwrap()[0] - + device + .create_graphics_pipelines(cache.clone(), &[pipeline_info], None) + .map_err(|e| e.1) + .unwrap()[0] }; Ok(VulkanGraphicsPipeline { layout: pipeline_layout, render_pass, - pipeline + pipeline, }) } -} \ No newline at end of file +} diff --git a/librashader-runtime/src/filter_chain.rs b/librashader-runtime/src/filter_chain.rs index b9dd2fe..97cd35d 100644 --- a/librashader-runtime/src/filter_chain.rs +++ b/librashader-runtime/src/filter_chain.rs @@ -27,4 +27,4 @@ pub trait FilterChain { frame_count: usize, options: Option<&Self::FrameOptions>, ) -> Result<(), Self::Error>; -} \ No newline at end of file +} diff --git a/librashader-runtime/src/image.rs b/librashader-runtime/src/image.rs index 6882d18..72169e5 100644 --- a/librashader-runtime/src/image.rs +++ b/librashader-runtime/src/image.rs @@ -1,6 +1,6 @@ -use std::marker::PhantomData; -use librashader_common::Size; pub use image::ImageError; +use librashader_common::Size; +use std::marker::PhantomData; use std::path::Path; @@ -8,7 +8,7 @@ pub struct Image { pub bytes: Vec, pub size: Size, pub pitch: usize, - _pd: PhantomData

+ _pd: PhantomData

, } pub struct RGBA8; @@ -20,9 +20,7 @@ pub trait PixelFormat { } impl PixelFormat for RGBA8 { - fn convert(_pixels: &mut Vec) { - - } + fn convert(_pixels: &mut Vec) {} } impl PixelFormat for BGRA8 { diff --git a/librashader-runtime/src/parameters.rs b/librashader-runtime/src/parameters.rs index 661c699..949b04b 100644 --- a/librashader-runtime/src/parameters.rs +++ b/librashader-runtime/src/parameters.rs @@ -16,4 +16,4 @@ pub trait FilterChainParameters { /// /// Returns `None` if the parameter did not exist, or the old value if successful. fn set_parameter(&mut self, parameter: &str, new_value: f32) -> Option; -} \ No newline at end of file +} diff --git a/librashader-runtime/src/ringbuffer.rs b/librashader-runtime/src/ringbuffer.rs index e45a71c..7fd5635 100644 --- a/librashader-runtime/src/ringbuffer.rs +++ b/librashader-runtime/src/ringbuffer.rs @@ -69,9 +69,9 @@ pub struct BoxRingBuffer { } impl BoxRingBuffer - where - T: Copy, - T: Default, +where + T: Copy, + T: Default, { pub fn new(size: usize) -> Self { Self { @@ -81,12 +81,11 @@ impl BoxRingBuffer } } - impl BoxRingBuffer { pub fn from_vec(items: Vec) -> Self { Self { items: items.into_boxed_slice(), - index: 0 + index: 0, } } @@ -126,4 +125,4 @@ impl RingBuffer for BoxRingBuffer { fn current_index(&self) -> usize { self.index } -} \ No newline at end of file +} diff --git a/librashader-runtime/src/scaling.rs b/librashader-runtime/src/scaling.rs index 5715e81..8924348 100644 --- a/librashader-runtime/src/scaling.rs +++ b/librashader-runtime/src/scaling.rs @@ -1,23 +1,28 @@ +use crate::scaling; use librashader_common::Size; use librashader_presets::{Scale2D, ScaleFactor, ScaleType, Scaling}; use num_traits::AsPrimitive; use std::ops::Mul; -use crate::scaling; pub trait ViewportSize - where - T: Mul + Copy + 'static, - f32: AsPrimitive +where + T: Mul + Copy + 'static, + f32: AsPrimitive, { /// Produce a `Size` scaled with the input scaling options. fn scale_viewport(self, scaling: Scale2D, viewport: Size) -> Size; } impl ViewportSize for Size +where + T: Mul + Copy + 'static, + f32: AsPrimitive, +{ + fn scale_viewport(self, scaling: Scale2D, viewport: Size) -> Size where T: Mul + Copy + 'static, - f32: AsPrimitive{ - fn scale_viewport(self, scaling: Scale2D, viewport: Size) -> Size where T: Mul + Copy + 'static, f32: AsPrimitive { + f32: AsPrimitive, + { scaling::scale(scaling, self, viewport) } } diff --git a/librashader-runtime/src/uniforms.rs b/librashader-runtime/src/uniforms.rs index e6fe458..51bc80e 100644 --- a/librashader-runtime/src/uniforms.rs +++ b/librashader-runtime/src/uniforms.rs @@ -63,7 +63,6 @@ where H: for<'a> BindUniform, H: for<'a> BindUniform, { - /// Create a new `UniformStorage` with the given size for UBO and Push Constant Buffer sizes. pub fn new(ubo_size: usize, push_size: usize) -> Self { UniformStorage { diff --git a/librashader/src/lib.rs b/librashader/src/lib.rs index 86e7cf6..474f8c8 100644 --- a/librashader/src/lib.rs +++ b/librashader/src/lib.rs @@ -35,8 +35,8 @@ /// Shader presets contain shader and texture parameters, and the order in which to apply a set of shaders /// in a filter chain. pub mod presets { - pub use librashader_presets::*; use librashader_preprocess::{PreprocessError, ShaderParameter, ShaderSource}; + pub use librashader_presets::*; /// Get full parameter metadata from a shader preset. pub fn get_parameter_meta( preset: &ShaderPreset, @@ -66,20 +66,18 @@ pub mod preprocess { pub mod reflect { /// Supported shader compiler targets. pub mod targets { + pub use librashader_reflect::back::targets::SpirV; pub use librashader_reflect::back::targets::GLSL; pub use librashader_reflect::back::targets::HLSL; - pub use librashader_reflect::back::targets::SpirV; } pub use librashader_reflect::error::*; - pub use librashader_reflect::reflect::{ - ReflectShader, semantics, ShaderReflection, - }; + pub use librashader_reflect::reflect::{semantics, ReflectShader, ShaderReflection}; pub use librashader_reflect::back::{ - CompilerBackend, CompileShader, FromCompilation, ShaderCompilerOutput, - targets::OutputTarget, + targets::OutputTarget, CompileShader, CompilerBackend, FromCompilation, + ShaderCompilerOutput, }; pub use librashader_reflect::front::shaderc::GlslangCompilation; pub use librashader_reflect::reflect::semantics::BindingMeta; @@ -88,8 +86,8 @@ pub mod reflect { /// Shader runtimes to execute a filter chain on a GPU surface. #[cfg(feature = "runtime")] pub mod runtime { - pub use librashader_runtime::parameters::FilterChainParameters; pub use librashader_runtime::filter_chain::FilterChain; + pub use librashader_runtime::parameters::FilterChainParameters; #[cfg(feature = "gl")] /// Shader runtime for OpenGL 3.3+. @@ -108,9 +106,7 @@ pub mod runtime { #[cfg(feature = "vk")] /// Shader compiler targets and runtime for Vulkan. - pub mod vk { - - } + pub mod vk {} } pub use librashader_common::{FilterMode, ImageFormat, Size, WrapMode};