From 03546153ff677859981a7eebe23586121c7300b5 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 23 Oct 2023 12:46:55 +1100 Subject: [PATCH] shaders can be either a path or a string --- librashader-common/src/lib.rs | 6 ++++++ librashader-preprocess/src/lib.rs | 15 ++++++++++----- librashader-presets/src/parse/preset.rs | 2 +- librashader-presets/src/preset.rs | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/librashader-common/src/lib.rs b/librashader-common/src/lib.rs index 1f78b7f..ab35844 100644 --- a/librashader-common/src/lib.rs +++ b/librashader-common/src/lib.rs @@ -27,6 +27,12 @@ use num_traits::AsPrimitive; use std::convert::Infallible; use std::str::FromStr; +#[derive(Debug, Clone)] +pub enum ShaderStorage { + Path(std::path::PathBuf), + String(String), +} + #[repr(u32)] #[derive(Default, Copy, Clone, Debug, Eq, PartialEq, Hash)] /// Supported image formats for textures. diff --git a/librashader-preprocess/src/lib.rs b/librashader-preprocess/src/lib.rs index da41330..f5e022c 100644 --- a/librashader-preprocess/src/lib.rs +++ b/librashader-preprocess/src/lib.rs @@ -17,7 +17,6 @@ use crate::include::read_source; pub use error::*; use librashader_common::ImageFormat; use rustc_hash::FxHashMap; -use std::path::Path; /// The source file for a single shader pass. #[derive(Debug, Clone, PartialEq)] @@ -58,8 +57,8 @@ pub struct ShaderParameter { impl ShaderSource { /// Load the source file at the given path, resolving includes relative to the location of the /// source file. - pub fn load(path: impl AsRef) -> Result { - load_shader_source(path) + pub fn load(file: &librashader_common::ShaderStorage) -> Result { + load_shader_source(file) } } @@ -78,8 +77,14 @@ impl SourceOutput for String { } } -pub(crate) fn load_shader_source(path: impl AsRef) -> Result { - let source = read_source(path)?; +pub(crate) fn load_shader_source( + file: &librashader_common::ShaderStorage, +) -> Result { + let source = match file { + librashader_common::ShaderStorage::Path(path) => read_source(path)?, + librashader_common::ShaderStorage::String(s) => s.to_string(), + }; + let meta = pragma::parse_pragma_meta(&source)?; let text = stage::process_stages(&source)?; let parameters = FxHashMap::from_iter(meta.parameters.into_iter().map(|p| (p.id.clone(), p))); diff --git a/librashader-presets/src/parse/preset.rs b/librashader-presets/src/parse/preset.rs index dbeab4b..20fa2f9 100644 --- a/librashader-presets/src/parse/preset.rs +++ b/librashader-presets/src/parse/preset.rs @@ -114,7 +114,7 @@ pub fn resolve_values(mut values: Vec) -> ShaderPreset { let shader = ShaderPassConfig { id, - name, + name: librashader_common::ShaderStorage::Path(name), alias: shader_values.iter().find_map(|f| match f { Value::Alias(_, value) => Some(value.to_string()), _ => None, diff --git a/librashader-presets/src/preset.rs b/librashader-presets/src/preset.rs index 2cdcc68..5af2d07 100644 --- a/librashader-presets/src/preset.rs +++ b/librashader-presets/src/preset.rs @@ -10,7 +10,7 @@ pub struct ShaderPassConfig { /// The index of the shader pass relative to its parent preset. pub id: i32, /// The fully qualified path to the shader pass source file. - pub name: PathBuf, + pub name: librashader_common::ShaderStorage, /// The alias of the shader pass if available. pub alias: Option, /// The filtering mode that this shader pass should expect.