From 80fa30e54eaef932a97f9b798a22199420a8074b Mon Sep 17 00:00:00 2001 From: chyyran Date: Mon, 21 Nov 2022 16:13:37 -0500 Subject: [PATCH] preprocess: move ShaderSource to preprocess and rename common def libs --- Cargo.lock | 14 +++++---- Cargo.toml | 2 +- .../Cargo.toml | 2 +- {librashader => librashader-common}/src/gl.rs | 0 .../src/image.rs | 0 .../src/lib.rs | 19 ------------ librashader-preprocess/Cargo.toml | 2 +- librashader-preprocess/src/error.rs | 2 +- librashader-preprocess/src/lib.rs | 30 +++++++++++++++++-- librashader-preprocess/src/pragma.rs | 6 ++-- librashader-presets/Cargo.toml | 2 +- librashader-presets/src/parse/value.rs | 2 +- librashader-presets/src/preset.rs | 2 +- librashader-reflect/Cargo.toml | 6 +++- librashader-reflect/src/front/naga.rs | 2 +- librashader-reflect/src/front/shaderc.rs | 2 +- librashader-runtime-dx11/Cargo.toml | 2 +- librashader-runtime-dx11/src/lib.rs | 4 +-- librashader-runtime-gl/Cargo.toml | 2 +- librashader-runtime-gl/src/filter_chain.rs | 30 +++++++++---------- librashader-runtime-gl/src/filter_pass.rs | 3 +- librashader-runtime-gl/src/framebuffer.rs | 2 +- librashader-runtime-gl/src/hello_triangle.rs | 2 +- librashader-runtime-gl/src/util.rs | 2 +- 24 files changed, 77 insertions(+), 63 deletions(-) rename {librashader => librashader-common}/Cargo.toml (90%) rename {librashader => librashader-common}/src/gl.rs (100%) rename {librashader => librashader-common}/src/image.rs (100%) rename {librashader => librashader-common}/src/lib.rs (89%) diff --git a/Cargo.lock b/Cargo.lock index bb6432e..4e9413c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -611,7 +611,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" [[package]] -name = "librashader" +name = "librashader-common" version = "0.1.0" dependencies = [ "gl", @@ -622,7 +622,7 @@ dependencies = [ name = "librashader-preprocess" version = "0.1.0" dependencies = [ - "librashader", + "librashader-common", "nom", "thiserror", ] @@ -631,9 +631,10 @@ dependencies = [ name = "librashader-presets" version = "0.1.0" dependencies = [ - "librashader", + "librashader-common", "nom", "nom_locate", + "num-traits", "thiserror", ] @@ -642,7 +643,8 @@ name = "librashader-reflect" version = "0.1.0" dependencies = [ "bitflags", - "librashader", + "librashader-common", + "librashader-preprocess", "naga", "rspirv", "rspirv-reflect", @@ -656,7 +658,7 @@ dependencies = [ name = "librashader-runtime-dx11" version = "0.1.0" dependencies = [ - "librashader", + "librashader-common", "librashader-preprocess", "librashader-presets", "librashader-reflect", @@ -671,7 +673,7 @@ dependencies = [ "bytemuck", "gl", "glfw", - "librashader", + "librashader-common", "librashader-preprocess", "librashader-presets", "librashader-reflect", diff --git a/Cargo.toml b/Cargo.toml index 0eec267..0a97ec7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] members = [ - "librashader", + "librashader-common", "librashader-presets", "librashader-preprocess", "librashader-reflect", diff --git a/librashader/Cargo.toml b/librashader-common/Cargo.toml similarity index 90% rename from librashader/Cargo.toml rename to librashader-common/Cargo.toml index 6b99c62..cc03dc0 100644 --- a/librashader/Cargo.toml +++ b/librashader-common/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "librashader" +name = "librashader-common" version = "0.1.0" edition = "2021" diff --git a/librashader/src/gl.rs b/librashader-common/src/gl.rs similarity index 100% rename from librashader/src/gl.rs rename to librashader-common/src/gl.rs diff --git a/librashader/src/image.rs b/librashader-common/src/image.rs similarity index 100% rename from librashader/src/image.rs rename to librashader-common/src/image.rs diff --git a/librashader/src/lib.rs b/librashader-common/src/lib.rs similarity index 89% rename from librashader/src/lib.rs rename to librashader-common/src/lib.rs index 166b9d8..fbded0a 100644 --- a/librashader/src/lib.rs +++ b/librashader-common/src/lib.rs @@ -5,25 +5,6 @@ pub mod image; use std::convert::Infallible; use std::str::FromStr; -#[derive(Debug, Clone, PartialEq)] -pub struct ShaderSource { - pub vertex: String, - pub fragment: String, - pub name: Option, - pub parameters: Vec, - pub format: ShaderFormat, -} - -#[derive(Debug, Clone, PartialEq)] -pub struct ShaderParameter { - pub id: String, - pub description: String, - pub initial: f32, - pub minimum: f32, - pub maximum: f32, - pub step: f32, -} - #[repr(u32)] #[derive(Default, Copy, Clone, Debug, Eq, PartialEq)] pub enum ShaderFormat { diff --git a/librashader-preprocess/Cargo.toml b/librashader-preprocess/Cargo.toml index fab850c..19d20bb 100644 --- a/librashader-preprocess/Cargo.toml +++ b/librashader-preprocess/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] thiserror = "1.0.37" nom = "7.1.1" -librashader = { path = "../librashader" } +"librashader-common" = { path = "../librashader-common" } [features] default = [ "line_directives" ] diff --git a/librashader-preprocess/src/error.rs b/librashader-preprocess/src/error.rs index 7b5b194..5e634dc 100644 --- a/librashader-preprocess/src/error.rs +++ b/librashader-preprocess/src/error.rs @@ -1,4 +1,4 @@ -use librashader::ShaderParameter; +use crate::ShaderParameter; use std::convert::Infallible; use std::path::PathBuf; use thiserror::Error; diff --git a/librashader-preprocess/src/lib.rs b/librashader-preprocess/src/lib.rs index 5220e14..46a0c06 100644 --- a/librashader-preprocess/src/lib.rs +++ b/librashader-preprocess/src/lib.rs @@ -5,9 +5,34 @@ mod stage; use crate::include::read_source; pub use error::*; -use librashader::ShaderSource; +use librashader_common::ShaderFormat; use std::path::Path; +#[derive(Debug, Clone, PartialEq)] +pub struct ShaderSource { + pub vertex: String, + pub fragment: String, + pub name: Option, + pub parameters: Vec, + pub format: ShaderFormat, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct ShaderParameter { + pub id: String, + pub description: String, + pub initial: f32, + pub minimum: f32, + pub maximum: f32, + pub step: f32, +} + +impl ShaderSource { + pub fn load(path: impl AsRef) -> Result { + load_shader_source(path) + } +} + pub(crate) trait SourceOutput { fn push_line(&mut self, str: &str); fn mark_line(&mut self, line_no: usize, comment: &str) { @@ -23,7 +48,7 @@ impl SourceOutput for String { } } -pub fn load_shader_source(path: impl AsRef) -> Result { +pub(crate) fn load_shader_source(path: impl AsRef) -> Result { let source = read_source(path)?; let meta = pragma::parse_pragma_meta(&source)?; let text = stage::process_stages(&source)?; @@ -70,3 +95,4 @@ mod test { eprintln!("{params:?}") } } + diff --git a/librashader-preprocess/src/pragma.rs b/librashader-preprocess/src/pragma.rs index b56389d..0557380 100644 --- a/librashader-preprocess/src/pragma.rs +++ b/librashader-preprocess/src/pragma.rs @@ -1,5 +1,5 @@ -use crate::PreprocessError; -use librashader::{ShaderFormat, ShaderParameter}; +use crate::{PreprocessError, ShaderParameter}; +use librashader_common::ShaderFormat; use nom::bytes::complete::{is_not, tag, take_until, take_while}; use nom::combinator::map_res; use nom::number::complete::float; @@ -120,7 +120,7 @@ pub(crate) fn parse_pragma_meta(source: impl AsRef) -> Result) -> Result<(), Box>{ let mut reflections = Vec::new(); let mut compiled = Vec::new(); - for (index, (_, _, reflect)) in passes.iter_mut().enumerate() { + for (index, (_, _, mut reflect)) in passes.into_iter().enumerate() { let reflection = reflect.reflect(index, &semantics) .unwrap(); diff --git a/librashader-runtime-gl/Cargo.toml b/librashader-runtime-gl/Cargo.toml index 39e92ed..a57935e 100644 --- a/librashader-runtime-gl/Cargo.toml +++ b/librashader-runtime-gl/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -"librashader" = { path = "../librashader", features = ["opengl"] } +"librashader-common" = { path = "../librashader-common", features = ["opengl"] } "librashader-presets" = { path = "../librashader-presets" } "librashader-preprocess" = { path = "../librashader-preprocess" } "librashader-reflect" = { path = "../librashader-reflect" } diff --git a/librashader-runtime-gl/src/filter_chain.rs b/librashader-runtime-gl/src/filter_chain.rs index c67ffe4..15e0095 100644 --- a/librashader-runtime-gl/src/filter_chain.rs +++ b/librashader-runtime-gl/src/filter_chain.rs @@ -6,8 +6,8 @@ use crate::render_target::RenderTarget; use crate::util; use crate::util::{InlineRingBuffer, Texture}; use gl::types::{GLenum, GLint, GLsizei, GLsizeiptr, GLuint}; -use librashader::image::Image; -use librashader::{FilterMode, ShaderSource, Size, WrapMode}; +use librashader_common::image::Image; +use librashader_common::{FilterMode, Size, WrapMode}; use librashader_presets::{ScaleType, ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_reflect::back::cross::{GlslangGlslContext, GlVersion}; use librashader_reflect::back::targets::{CompilerBackend, FromCompilation, GLSL}; @@ -20,6 +20,7 @@ use rustc_hash::FxHashMap; use spirv_cross::spirv::Decoration; use std::error::Error; use std::path::Path; +use librashader_preprocess::ShaderSource; use crate::quad_render::DrawQuad; pub struct FilterChain { @@ -126,14 +127,13 @@ type ShaderPassMeta<'a> = ( CompilerBackend< impl CompileShader + ReflectShader - + Sized, >, ); impl FilterChain { /// Load a filter chain from a pre-parsed `ShaderPreset`. pub fn load_from_preset(preset: ShaderPreset) -> Result> { - let (passes, semantics) = FilterChain::load_preset(&preset); + let (passes, semantics) = FilterChain::load_preset(&preset)?; // initialize passes let filters = FilterChain::init_passes(passes, &semantics)?; @@ -193,21 +193,21 @@ impl FilterChain { Self::load_from_preset(preset) } - fn load_preset(preset: &ShaderPreset) -> (Vec, ReflectSemantics) { + fn load_preset(preset: &ShaderPreset) -> Result<(Vec, ReflectSemantics), Box> { let mut uniform_semantics: FxHashMap = Default::default(); let mut texture_semantics: FxHashMap> = Default::default(); - let passes: Vec<(&ShaderPassConfig, ShaderSource, _)> = preset + let passes = preset .shaders .iter() .map(|shader| { eprintln!("[gl] loading {}", &shader.name.display()); let source: ShaderSource = - librashader_preprocess::load_shader_source(&shader.name).unwrap(); + ShaderSource::load(&shader.name)?; - let spirv = librashader_reflect::front::shaderc::compile_spirv(&source).unwrap(); - let reflect = GLSL::from_compilation(spirv).unwrap(); + let spirv = librashader_reflect::front::shaderc::compile_spirv(&source)?; + let reflect = GLSL::from_compilation(spirv)?; for parameter in source.parameters.iter() { uniform_semantics.insert( @@ -218,12 +218,12 @@ impl FilterChain { }), ); } - - (shader, source, reflect) + Ok::<_, Box>((shader, source, reflect)) }) - .collect(); - - // todo: this can probably be extracted out. + .into_iter() + .collect::)>, _>>()?; for details in &passes { FilterChain::load_pass_semantics( @@ -257,7 +257,7 @@ impl FilterChain { non_uniform_semantics: texture_semantics, }; - (passes, semantics) + Ok((passes, semantics)) } fn load_luts(textures: &[TextureConfig]) -> Result, Box> { diff --git a/librashader-runtime-gl/src/filter_pass.rs b/librashader-runtime-gl/src/filter_pass.rs index 3445e4d..758d1b0 100644 --- a/librashader-runtime-gl/src/filter_pass.rs +++ b/librashader-runtime-gl/src/filter_pass.rs @@ -3,12 +3,13 @@ use librashader_reflect::back::cross::GlslangGlslContext; use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::reflect::ShaderReflection; -use librashader::{ShaderFormat, ShaderSource, Size}; +use librashader_common::{ShaderFormat, Size}; use librashader_presets::ShaderPassConfig; use librashader_reflect::reflect::semantics::{ MemberOffset, TextureImage, TextureSemantics, VariableSemantics, }; use rustc_hash::FxHashMap; +use librashader_preprocess::ShaderSource; use crate::binding::{UniformBinding, UniformLocation, VariableLocation}; use crate::filter_chain::FilterCommon; diff --git a/librashader-runtime-gl/src/framebuffer.rs b/librashader-runtime-gl/src/framebuffer.rs index 3b5cdf9..6b49afc 100644 --- a/librashader-runtime-gl/src/framebuffer.rs +++ b/librashader-runtime-gl/src/framebuffer.rs @@ -1,7 +1,7 @@ use crate::util; use crate::util::Texture; use gl::types::{GLenum, GLint, GLsizei, GLuint}; -use librashader::{FilterMode, ShaderFormat, Size, WrapMode}; +use librashader_common::{FilterMode, ShaderFormat, Size, WrapMode}; use librashader_presets::{Scale2D, ScaleType, Scaling}; #[derive(Debug)] diff --git a/librashader-runtime-gl/src/hello_triangle.rs b/librashader-runtime-gl/src/hello_triangle.rs index 1abaab0..9b2ed09 100644 --- a/librashader-runtime-gl/src/hello_triangle.rs +++ b/librashader-runtime-gl/src/hello_triangle.rs @@ -5,7 +5,7 @@ use std::sync::mpsc::Receiver; use glfw::{Context, Glfw, Window, WindowEvent}; use gl::types::{GLchar, GLenum, GLint, GLsizei, GLuint}; -use librashader::Size; +use librashader_common::Size; use crate::filter_chain::FilterChain; use crate::framebuffer::{Framebuffer, GlImage, Viewport}; diff --git a/librashader-runtime-gl/src/util.rs b/librashader-runtime-gl/src/util.rs index 8eeadad..bdba4ba 100644 --- a/librashader-runtime-gl/src/util.rs +++ b/librashader-runtime-gl/src/util.rs @@ -1,6 +1,6 @@ use crate::framebuffer::{Framebuffer, GlImage}; use gl::types::{GLenum, GLuint}; -use librashader::{FilterMode, WrapMode}; +use librashader_common::{FilterMode, WrapMode}; pub fn calc_miplevel(width: u32, height: u32) -> u32 { let mut size = std::cmp::max(width, height);