preprocess: move ShaderSource to preprocess and rename common def libs

This commit is contained in:
chyyran 2022-11-21 16:13:37 -05:00
parent e0b66c2c60
commit 80fa30e54e
24 changed files with 77 additions and 63 deletions

14
Cargo.lock generated
View file

@ -611,7 +611,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c"
[[package]] [[package]]
name = "librashader" name = "librashader-common"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"gl", "gl",
@ -622,7 +622,7 @@ dependencies = [
name = "librashader-preprocess" name = "librashader-preprocess"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"librashader", "librashader-common",
"nom", "nom",
"thiserror", "thiserror",
] ]
@ -631,9 +631,10 @@ dependencies = [
name = "librashader-presets" name = "librashader-presets"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"librashader", "librashader-common",
"nom", "nom",
"nom_locate", "nom_locate",
"num-traits",
"thiserror", "thiserror",
] ]
@ -642,7 +643,8 @@ name = "librashader-reflect"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"librashader", "librashader-common",
"librashader-preprocess",
"naga", "naga",
"rspirv", "rspirv",
"rspirv-reflect", "rspirv-reflect",
@ -656,7 +658,7 @@ dependencies = [
name = "librashader-runtime-dx11" name = "librashader-runtime-dx11"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"librashader", "librashader-common",
"librashader-preprocess", "librashader-preprocess",
"librashader-presets", "librashader-presets",
"librashader-reflect", "librashader-reflect",
@ -671,7 +673,7 @@ dependencies = [
"bytemuck", "bytemuck",
"gl", "gl",
"glfw", "glfw",
"librashader", "librashader-common",
"librashader-preprocess", "librashader-preprocess",
"librashader-presets", "librashader-presets",
"librashader-reflect", "librashader-reflect",

View file

@ -1,6 +1,6 @@
[workspace] [workspace]
members = [ members = [
"librashader", "librashader-common",
"librashader-presets", "librashader-presets",
"librashader-preprocess", "librashader-preprocess",
"librashader-reflect", "librashader-reflect",

View file

@ -1,5 +1,5 @@
[package] [package]
name = "librashader" name = "librashader-common"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View file

@ -5,25 +5,6 @@ pub mod image;
use std::convert::Infallible; use std::convert::Infallible;
use std::str::FromStr; use std::str::FromStr;
#[derive(Debug, Clone, PartialEq)]
pub struct ShaderSource {
pub vertex: String,
pub fragment: String,
pub name: Option<String>,
pub parameters: Vec<ShaderParameter>,
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)] #[repr(u32)]
#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)] #[derive(Default, Copy, Clone, Debug, Eq, PartialEq)]
pub enum ShaderFormat { pub enum ShaderFormat {

View file

@ -8,7 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
thiserror = "1.0.37" thiserror = "1.0.37"
nom = "7.1.1" nom = "7.1.1"
librashader = { path = "../librashader" } "librashader-common" = { path = "../librashader-common" }
[features] [features]
default = [ "line_directives" ] default = [ "line_directives" ]

View file

@ -1,4 +1,4 @@
use librashader::ShaderParameter; use crate::ShaderParameter;
use std::convert::Infallible; use std::convert::Infallible;
use std::path::PathBuf; use std::path::PathBuf;
use thiserror::Error; use thiserror::Error;

View file

@ -5,9 +5,34 @@ mod stage;
use crate::include::read_source; use crate::include::read_source;
pub use error::*; pub use error::*;
use librashader::ShaderSource; use librashader_common::ShaderFormat;
use std::path::Path; use std::path::Path;
#[derive(Debug, Clone, PartialEq)]
pub struct ShaderSource {
pub vertex: String,
pub fragment: String,
pub name: Option<String>,
pub parameters: Vec<ShaderParameter>,
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<Path>) -> Result<ShaderSource, PreprocessError> {
load_shader_source(path)
}
}
pub(crate) trait SourceOutput { pub(crate) trait SourceOutput {
fn push_line(&mut self, str: &str); fn push_line(&mut self, str: &str);
fn mark_line(&mut self, line_no: usize, comment: &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<Path>) -> Result<ShaderSource, PreprocessError> { pub(crate) fn load_shader_source(path: impl AsRef<Path>) -> Result<ShaderSource, PreprocessError> {
let source = read_source(path)?; let source = read_source(path)?;
let meta = pragma::parse_pragma_meta(&source)?; let meta = pragma::parse_pragma_meta(&source)?;
let text = stage::process_stages(&source)?; let text = stage::process_stages(&source)?;
@ -70,3 +95,4 @@ mod test {
eprintln!("{params:?}") eprintln!("{params:?}")
} }
} }

View file

@ -1,5 +1,5 @@
use crate::PreprocessError; use crate::{PreprocessError, ShaderParameter};
use librashader::{ShaderFormat, ShaderParameter}; use librashader_common::ShaderFormat;
use nom::bytes::complete::{is_not, tag, take_until, take_while}; use nom::bytes::complete::{is_not, tag, take_until, take_while};
use nom::combinator::map_res; use nom::combinator::map_res;
use nom::number::complete::float; use nom::number::complete::float;
@ -120,7 +120,7 @@ pub(crate) fn parse_pragma_meta(source: impl AsRef<str>) -> Result<ShaderMeta, P
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::pragma::parse_parameter_string; use crate::pragma::parse_parameter_string;
use librashader::ShaderParameter; use crate::ShaderParameter;
#[test] #[test]
fn parses_parameter_pragma() { fn parses_parameter_pragma() {

View file

@ -9,5 +9,5 @@ edition = "2021"
thiserror = "1.0.37" thiserror = "1.0.37"
nom = "7.1.1" nom = "7.1.1"
nom_locate = "4.0.0" nom_locate = "4.0.0"
librashader = { path = "../librashader" } "librashader-common" = { path = "../librashader-common" }
num-traits = "0.2" num-traits = "0.2"

View file

@ -14,7 +14,7 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use librashader::{FilterMode, WrapMode}; use librashader_common::{FilterMode, WrapMode};
#[derive(Debug)] #[derive(Debug)]
pub enum Value { pub enum Value {

View file

@ -2,7 +2,7 @@ use crate::error::ParsePresetError;
use std::ops::Mul; use std::ops::Mul;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use librashader::{FilterMode, WrapMode}; use librashader_common::{FilterMode, WrapMode};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ShaderPassConfig { pub struct ShaderPassConfig {

View file

@ -9,7 +9,8 @@ edition = "2021"
naga = { version = "0.10.0", path = "../naga", features = ["glsl-in", "spv-in", "spv-out", "glsl-out", "wgsl-out"] } naga = { version = "0.10.0", path = "../naga", features = ["glsl-in", "spv-in", "spv-out", "glsl-out", "wgsl-out"] }
shaderc = { version = "0.8.0" } shaderc = { version = "0.8.0" }
spirv_cross = { version = "0.23.1", features = [ "glsl", "hlsl" ] } spirv_cross = { version = "0.23.1", features = [ "glsl", "hlsl" ] }
librashader = { path = "../librashader" }
thiserror = "1.0.37" thiserror = "1.0.37"
bitflags = "1.3.2" bitflags = "1.3.2"
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
@ -17,4 +18,7 @@ rspirv = "0.11.0+1.5.4"
rspirv-reflect = { git = "https://github.com/Traverse-Research/rspirv-reflect" } rspirv-reflect = { git = "https://github.com/Traverse-Research/rspirv-reflect" }
librashader-common = { path = "../librashader-common" }
librashader-preprocess = { path = "../librashader-preprocess" }
[dev-dependencies] [dev-dependencies]

View file

@ -1,5 +1,5 @@
use crate::error::ShaderCompileError; use crate::error::ShaderCompileError;
use librashader::ShaderSource; use librashader_preprocess::ShaderSource;
use naga::front::glsl::{Options, Parser}; use naga::front::glsl::{Options, Parser};
use naga::{Module, ShaderStage}; use naga::{Module, ShaderStage};

View file

@ -1,5 +1,5 @@
use crate::error::ShaderCompileError; use crate::error::ShaderCompileError;
use librashader::ShaderSource; use librashader_preprocess::ShaderSource;
use shaderc::{CompilationArtifact, CompileOptions, Limit, ShaderKind}; use shaderc::{CompilationArtifact, CompileOptions, Limit, ShaderKind};
pub struct GlslangCompilation { pub struct GlslangCompilation {

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
"librashader" = { path = "../librashader" } "librashader-common" = { path = "../librashader-common" }
"librashader-presets" = { path = "../librashader-presets" } "librashader-presets" = { path = "../librashader-presets" }
"librashader-preprocess" = { path = "../librashader-preprocess" } "librashader-preprocess" = { path = "../librashader-preprocess" }
"librashader-reflect" = { path = "../librashader-reflect" } "librashader-reflect" = { path = "../librashader-reflect" }

View file

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::path::Path; use std::path::Path;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use librashader::ShaderSource; use librashader_preprocess::ShaderSource;
use librashader_presets::ShaderPassConfig; use librashader_presets::ShaderPassConfig;
use librashader_reflect::back::{CompileShader}; use librashader_reflect::back::{CompileShader};
use librashader_reflect::back::targets::{FromCompilation, HLSL}; use librashader_reflect::back::targets::{FromCompilation, HLSL};
@ -91,7 +91,7 @@ pub fn load(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>>{
let mut reflections = Vec::new(); let mut reflections = Vec::new();
let mut compiled = 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) let reflection = reflect.reflect(index, &semantics)
.unwrap(); .unwrap();

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
"librashader" = { path = "../librashader", features = ["opengl"] } "librashader-common" = { path = "../librashader-common", features = ["opengl"] }
"librashader-presets" = { path = "../librashader-presets" } "librashader-presets" = { path = "../librashader-presets" }
"librashader-preprocess" = { path = "../librashader-preprocess" } "librashader-preprocess" = { path = "../librashader-preprocess" }
"librashader-reflect" = { path = "../librashader-reflect" } "librashader-reflect" = { path = "../librashader-reflect" }

View file

@ -6,8 +6,8 @@ use crate::render_target::RenderTarget;
use crate::util; use crate::util;
use crate::util::{InlineRingBuffer, Texture}; use crate::util::{InlineRingBuffer, Texture};
use gl::types::{GLenum, GLint, GLsizei, GLsizeiptr, GLuint}; use gl::types::{GLenum, GLint, GLsizei, GLsizeiptr, GLuint};
use librashader::image::Image; use librashader_common::image::Image;
use librashader::{FilterMode, ShaderSource, Size, WrapMode}; use librashader_common::{FilterMode, Size, WrapMode};
use librashader_presets::{ScaleType, ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_presets::{ScaleType, ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::cross::{GlslangGlslContext, GlVersion}; use librashader_reflect::back::cross::{GlslangGlslContext, GlVersion};
use librashader_reflect::back::targets::{CompilerBackend, FromCompilation, GLSL}; use librashader_reflect::back::targets::{CompilerBackend, FromCompilation, GLSL};
@ -20,6 +20,7 @@ use rustc_hash::FxHashMap;
use spirv_cross::spirv::Decoration; use spirv_cross::spirv::Decoration;
use std::error::Error; use std::error::Error;
use std::path::Path; use std::path::Path;
use librashader_preprocess::ShaderSource;
use crate::quad_render::DrawQuad; use crate::quad_render::DrawQuad;
pub struct FilterChain { pub struct FilterChain {
@ -126,14 +127,13 @@ type ShaderPassMeta<'a> = (
CompilerBackend< CompilerBackend<
impl CompileShader<GLSL, Options = GlVersion, Context = GlslangGlslContext> impl CompileShader<GLSL, Options = GlVersion, Context = GlslangGlslContext>
+ ReflectShader + ReflectShader
+ Sized,
>, >,
); );
impl FilterChain { impl FilterChain {
/// Load a filter chain from a pre-parsed `ShaderPreset`. /// Load a filter chain from a pre-parsed `ShaderPreset`.
pub fn load_from_preset(preset: ShaderPreset) -> Result<FilterChain, Box<dyn Error>> { pub fn load_from_preset(preset: ShaderPreset) -> Result<FilterChain, Box<dyn Error>> {
let (passes, semantics) = FilterChain::load_preset(&preset); let (passes, semantics) = FilterChain::load_preset(&preset)?;
// initialize passes // initialize passes
let filters = FilterChain::init_passes(passes, &semantics)?; let filters = FilterChain::init_passes(passes, &semantics)?;
@ -193,21 +193,21 @@ impl FilterChain {
Self::load_from_preset(preset) Self::load_from_preset(preset)
} }
fn load_preset(preset: &ShaderPreset) -> (Vec<ShaderPassMeta>, ReflectSemantics) { fn load_preset(preset: &ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ReflectSemantics), Box<dyn Error>> {
let mut uniform_semantics: FxHashMap<String, UniformSemantic> = Default::default(); let mut uniform_semantics: FxHashMap<String, UniformSemantic> = Default::default();
let mut texture_semantics: FxHashMap<String, SemanticMap<TextureSemantics>> = let mut texture_semantics: FxHashMap<String, SemanticMap<TextureSemantics>> =
Default::default(); Default::default();
let passes: Vec<(&ShaderPassConfig, ShaderSource, _)> = preset let passes = preset
.shaders .shaders
.iter() .iter()
.map(|shader| { .map(|shader| {
eprintln!("[gl] loading {}", &shader.name.display()); eprintln!("[gl] loading {}", &shader.name.display());
let source: ShaderSource = 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 spirv = librashader_reflect::front::shaderc::compile_spirv(&source)?;
let reflect = GLSL::from_compilation(spirv).unwrap(); let reflect = GLSL::from_compilation(spirv)?;
for parameter in source.parameters.iter() { for parameter in source.parameters.iter() {
uniform_semantics.insert( uniform_semantics.insert(
@ -218,12 +218,12 @@ impl FilterChain {
}), }),
); );
} }
Ok::<_, Box<dyn Error>>((shader, source, reflect))
(shader, source, reflect)
}) })
.collect(); .into_iter()
.collect::<Result<Vec<(&ShaderPassConfig,
// todo: this can probably be extracted out. ShaderSource,
CompilerBackend<_>)>, _>>()?;
for details in &passes { for details in &passes {
FilterChain::load_pass_semantics( FilterChain::load_pass_semantics(
@ -257,7 +257,7 @@ impl FilterChain {
non_uniform_semantics: texture_semantics, non_uniform_semantics: texture_semantics,
}; };
(passes, semantics) Ok((passes, semantics))
} }
fn load_luts(textures: &[TextureConfig]) -> Result<FxHashMap<usize, Texture>, Box<dyn Error>> { fn load_luts(textures: &[TextureConfig]) -> Result<FxHashMap<usize, Texture>, Box<dyn Error>> {

View file

@ -3,12 +3,13 @@ use librashader_reflect::back::cross::GlslangGlslContext;
use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::back::ShaderCompilerOutput;
use librashader_reflect::reflect::ShaderReflection; use librashader_reflect::reflect::ShaderReflection;
use librashader::{ShaderFormat, ShaderSource, Size}; use librashader_common::{ShaderFormat, Size};
use librashader_presets::ShaderPassConfig; use librashader_presets::ShaderPassConfig;
use librashader_reflect::reflect::semantics::{ use librashader_reflect::reflect::semantics::{
MemberOffset, TextureImage, TextureSemantics, VariableSemantics, MemberOffset, TextureImage, TextureSemantics, VariableSemantics,
}; };
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use librashader_preprocess::ShaderSource;
use crate::binding::{UniformBinding, UniformLocation, VariableLocation}; use crate::binding::{UniformBinding, UniformLocation, VariableLocation};
use crate::filter_chain::FilterCommon; use crate::filter_chain::FilterCommon;

View file

@ -1,7 +1,7 @@
use crate::util; use crate::util;
use crate::util::Texture; use crate::util::Texture;
use gl::types::{GLenum, GLint, GLsizei, GLuint}; 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}; use librashader_presets::{Scale2D, ScaleType, Scaling};
#[derive(Debug)] #[derive(Debug)]

View file

@ -5,7 +5,7 @@ use std::sync::mpsc::Receiver;
use glfw::{Context, Glfw, Window, WindowEvent}; use glfw::{Context, Glfw, Window, WindowEvent};
use gl::types::{GLchar, GLenum, GLint, GLsizei, GLuint}; use gl::types::{GLchar, GLenum, GLint, GLsizei, GLuint};
use librashader::Size; use librashader_common::Size;
use crate::filter_chain::FilterChain; use crate::filter_chain::FilterChain;
use crate::framebuffer::{Framebuffer, GlImage, Viewport}; use crate::framebuffer::{Framebuffer, GlImage, Viewport};

View file

@ -1,6 +1,6 @@
use crate::framebuffer::{Framebuffer, GlImage}; use crate::framebuffer::{Framebuffer, GlImage};
use gl::types::{GLenum, GLuint}; use gl::types::{GLenum, GLuint};
use librashader::{FilterMode, WrapMode}; use librashader_common::{FilterMode, WrapMode};
pub fn calc_miplevel(width: u32, height: u32) -> u32 { pub fn calc_miplevel(width: u32, height: u32) -> u32 {
let mut size = std::cmp::max(width, height); let mut size = std::cmp::max(width, height);