preset: parse to config structs

This commit is contained in:
chyyran 2022-10-20 22:52:34 -04:00
parent 806e1ae2ee
commit 6b8449b4da
7 changed files with 229 additions and 37 deletions

View file

@ -3,5 +3,6 @@
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/librashader-presets/test/slang-shaders" vcs="Git" />
</component> </component>
</project> </project>

View file

@ -1,12 +1,45 @@
use std::path::Path;
use nom::{ExtendInto, Offset}; use nom::{ExtendInto, Offset};
use nom_locate::LocatedSpan; use nom_locate::LocatedSpan;
use std::str; use std::str;
mod token; mod token;
mod value; mod value;
mod preset;
pub(crate) type Span<'a> = LocatedSpan<&'a str>;
pub(crate) use token::Token;
pub(crate) use value::Value;
pub type Span<'a> = LocatedSpan<&'a str>;
use crate::error::ParsePresetError; use crate::error::ParsePresetError;
pub use token::do_lex; use crate::parse::preset::resolve_values;
pub use token::Token; use crate::parse::value::parse_preset;
pub use value::parse_values; use crate::Preset;
pub(crate) fn remove_if<T>(values: &mut Vec<T>, f: impl FnMut(&T) -> bool) -> Option<T> {
values.iter()
.position(f)
.map(|idx| values.remove(idx))
}
impl Preset {
pub fn try_parse(path: impl AsRef<Path>) -> Result<Preset, ParsePresetError> {
let values = parse_preset(path)?;
Ok(resolve_values(values))
}
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::Preset;
#[test]
pub fn parse_preset() {
let root =
PathBuf::from("test/slang-shaders/bezel/Mega_Bezel/Presets/Base_CRT_Presets/MBZ__3__STD__MEGATRON-NTSC.slangp");
let basic = Preset::try_parse(root);
eprintln!("{:#?}", basic);
assert!(basic.is_ok());
}
}

View file

@ -0,0 +1,142 @@
use crate::parse::value::Value;
use crate::{WrapMode, FilterMode, Parameter, Preset, Scale2D, Scaling, ShaderConfig, TextureConfig};
use crate::parse::remove_if;
pub fn resolve_values(mut values: Vec<Value>) -> Preset {
let textures: Vec<TextureConfig> = values.drain_filter(|f| matches!(*f, Value::Texture { .. }))
.map(|value| {
if let Value::Texture { name, filter_mode, wrap_mode, mipmap, path } = value {
TextureConfig {
name,
path,
wrap_mode,
filter_mode,
mipmap
}
} else {
unreachable!("values should all be of type Texture")
}
}).collect();
let parameters: Vec<Parameter> = values.drain_filter(|f| matches!(*f, Value::Parameter { .. })).map(|value| {
if let Value::Parameter(name, value) = value {
Parameter {
name,
value
}
} else {
unreachable!("values should be all of type parameters")
}
}).collect();
let mut shaders = Vec::new();
let shader_count = remove_if(&mut values, |v| {
matches!(*v, Value::ShaderCount(_))
})
.map(|value| if let Value::ShaderCount(count) = value { count } else { unreachable!("value should be of type shader_count") })
.unwrap_or(0);
let feedback_pass = remove_if(&mut values, |v| {
matches!(*v, Value::FeedbackPass(_))
})
.map(|value| if let Value::FeedbackPass(pass) = value { pass } else { unreachable!("value should be of type feedback_pass") })
.unwrap_or(0);
for shader in 0..shader_count {
if let Some(Value::Shader(id, name)) = remove_if(&mut values, |v| matches!(*v, Value::Shader(shader_index, _) if shader_index == shader)) {
let shader_values: Vec<Value> = values.drain_filter(|v| v.shader_index() == Some(shader)).collect();
let scale_type = shader_values.iter().find_map(|f| match f {
Value::ScaleType(_, value) => Some(*value),
_ => None
});
let mut scale_type_x = shader_values.iter().find_map(|f| match f {
Value::ScaleType(_, value) => Some(*value),
_ => None
});
let mut scale_type_y = shader_values.iter().find_map(|f| match f {
Value::ScaleType(_, value) => Some(*value),
_ => None
});
if scale_type.is_some() {
// scale takes priority
// https://github.com/libretro/RetroArch/blob/fcbd72dbf3579eb31721fbbf0d89a139834bcce9/gfx/video_shader_parse.c#L310
scale_type_x = scale_type;
scale_type_y = scale_type;
}
let scale_valid = scale_type_x.is_some() || scale_type_y.is_some();
let scale = shader_values.iter().find_map(|f| match f {
Value::Scale(_, value) => Some(*value),
_ => None
});
let mut scale_x = shader_values.iter().find_map(|f| match f {
Value::ScaleX(_, value) => Some(*value),
_ => None
});
let mut scale_y = shader_values.iter().find_map(|f| match f {
Value::ScaleY(_, value) => Some(*value),
_ => None
});
if scale.is_some() {
// scale takes priority
// https://github.com/libretro/RetroArch/blob/fcbd72dbf3579eb31721fbbf0d89a139834bcce9/gfx/video_shader_parse.c#L310
scale_x = scale;
scale_y = scale;
}
let mut shader = ShaderConfig {
id,
name,
alias: shader_values.iter().find_map(|f| match f {
Value::Alias(_, value) => Some(value.to_string()),
_ => None
}),
filter: shader_values.iter().find_map(|f| match f {
Value::FilterMode(_, value) => Some(*value),
_ => None
}).unwrap_or(FilterMode::default()),
wrap_mode: shader_values.iter().find_map(|f| match f {
Value::WrapMode(_, value) => Some(*value),
_ => None
}).unwrap_or(WrapMode::default()),
frame_count_mod: shader_values.iter().find_map(|f| match f {
Value::FrameCountMod(_, value) => Some(*value),
_ => None
}).unwrap_or(0),
srgb_framebuffer: shader_values.iter().find_map(|f| match f {
Value::SrgbFramebuffer(_, value) => Some(*value),
_ => None
}).unwrap_or(false),
float_framebuffer: shader_values.iter().find_map(|f| match f {
Value::FloatFramebuffer(_, value) => Some(*value),
_ => None
}).unwrap_or(false),
mipmap_input: shader_values.iter().find_map(|f| match f {
Value::MipmapInput(_, value) => Some(*value),
_ => None
}).unwrap_or(false),
scaling: Scale2D {
valid: scale_valid,
x: Scaling { scale_type: scale_type_x.unwrap_or_default(), factor: scale_x.unwrap_or_default() },
y: Scaling { scale_type: scale_type_y.unwrap_or_default(), factor: scale_y.unwrap_or_default() }
}
};
shaders.push(shader)
}
}
Preset {
shader_count,
feedback_pass,
shaders,
textures,
parameters
}
}

View file

@ -155,8 +155,7 @@ pub fn do_lex(input: &str) -> Result<Vec<Token>, ParsePresetError> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::parse::do_lex; use crate::parse::token::{do_lex, parse_key_value, parse_tokens, single_comment, Span};
use crate::parse::token::{parse_key_value, parse_tokens, single_comment, Span};
#[test] #[test]
fn parses_single_line_comment() { fn parses_single_line_comment() {

View file

@ -1,5 +1,5 @@
use crate::error::{ParseErrorKind, ParsePresetError}; use crate::error::{ParseErrorKind, ParsePresetError};
use crate::parse::{Span, Token}; use crate::parse::{remove_if, Span, Token};
use crate::{FilterMode, ScaleFactor, ScaleType, Scaling, WrapMode}; use crate::{FilterMode, ScaleFactor, ScaleType, Scaling, WrapMode};
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::character::complete::digit1; use nom::character::complete::digit1;
@ -11,6 +11,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 crate::parse::token::do_lex;
#[derive(Debug)] #[derive(Debug)]
pub enum Value { pub enum Value {
@ -38,6 +39,26 @@ pub enum Value {
}, },
} }
impl Value {
pub(crate) fn shader_index(&self) -> Option<i32> {
match self {
Value::Shader(i, _) => Some(*i),
Value::ScaleX(i, _) => Some(*i),
Value::ScaleY(i, _) => Some(*i),
Value::Scale(i, _) => Some(*i),
Value::ScaleType(i, _) => Some(*i),
Value::FilterMode(i, _) => Some(*i),
Value::WrapMode(i, _) => Some(*i),
Value::FrameCountMod(i, _) => Some(*i),
Value::FloatFramebuffer(i, _) => Some(*i),
Value::SrgbFramebuffer(i, _) => Some(*i),
Value::MipmapInput(i, _) => Some(*i),
Value::Alias(i, _) => Some(*i),
_ => None
}
}
}
fn from_int(input: Span) -> Result<i32, ParsePresetError> { fn from_int(input: Span) -> Result<i32, ParsePresetError> {
i32::from_str(input.trim()).map_err(|_| { i32::from_str(input.trim()).map_err(|_| {
eprintln!("{input}"); eprintln!("{input}");
@ -135,7 +156,7 @@ fn load_child_reference_strings(
.read_to_string(&mut reference_contents) .read_to_string(&mut reference_contents)
.map_err(|e| ParsePresetError::IOError(reference_root.clone(), e))?; .map_err(|e| ParsePresetError::IOError(reference_root.clone(), e))?;
let mut new_tokens = super::do_lex(&reference_contents)?; let mut new_tokens = do_lex(&reference_contents)?;
let mut new_references: Vec<PathBuf> = new_tokens let mut new_references: Vec<PathBuf> = new_tokens
.drain_filter(|token| *token.key.fragment() == "#reference") .drain_filter(|token| *token.key.fragment() == "#reference")
.map(|value| PathBuf::from(*value.value.fragment())) .map(|value| PathBuf::from(*value.value.fragment()))
@ -195,7 +216,7 @@ pub fn parse_values(
all_tokens.push((root_path.as_path(), tokens)); all_tokens.push((root_path.as_path(), tokens));
for (path, string) in child_strings.iter() { for (path, string) in child_strings.iter() {
let mut tokens = crate::parse::do_lex(string.as_ref())?; let mut tokens = do_lex(string.as_ref())?;
tokens.retain(|token| *token.key.fragment() != "#reference"); tokens.retain(|token| *token.key.fragment() != "#reference");
all_tokens.push((path.as_path(), tokens)) all_tokens.push((path.as_path(), tokens))
} }
@ -272,34 +293,24 @@ pub fn parse_values(
.collect(); .collect();
for (texture, path) in textures { for (texture, path) in textures {
let mipmap = tokens let mipmap = remove_if(&mut tokens, |t| {
.iter() t.key.starts_with(*texture)
.position(|t| { && t.key.ends_with("_mipmap")
t.key.starts_with(*texture) && t.key.len() == texture.len() + "_mipmap".len()
&& t.key.ends_with("_mipmap") }).map_or_else(|| Ok(false), |v| from_bool(v.value))?;
&& t.key.len() == texture.len() + "_mipmap".len()
})
.map(|idx| tokens.remove(idx))
.map_or_else(|| Ok(false), |v| from_bool(v.value))?;
let linear = tokens let linear = remove_if(&mut tokens, |t| {
.iter()
.position(|t| {
t.key.starts_with(*texture) t.key.starts_with(*texture)
&& t.key.ends_with("_linear") && t.key.ends_with("_linear")
&& t.key.len() == texture.len() + "_linear".len() && t.key.len() == texture.len() + "_linear".len()
}) })
.map(|idx| tokens.remove(idx))
.map_or_else(|| Ok(false), |v| from_bool(v.value))?; .map_or_else(|| Ok(false), |v| from_bool(v.value))?;
let wrap_mode = tokens let wrap_mode = remove_if(&mut tokens, |t| {
.iter()
.position(|t| {
t.key.starts_with(*texture) t.key.starts_with(*texture)
&& t.key.ends_with("_wrap_mode") && t.key.ends_with("_wrap_mode")
&& t.key.len() == texture.len() + "_wrap_mode".len() && t.key.len() == texture.len() + "_wrap_mode".len()
}) })
.map(|idx| tokens.remove(idx))
// NOPANIC: infallible // NOPANIC: infallible
.map_or_else( .map_or_else(
|| WrapMode::default(), || WrapMode::default(),
@ -439,7 +450,6 @@ pub fn parse_values(
continue; continue;
} }
eprintln!("{}", token.key);
// handle undeclared parameters after parsing everything else as a last resort. // handle undeclared parameters after parsing everything else as a last resort.
let param_val = from_float(token.value)?; let param_val = from_float(token.value)?;
values.push(Value::Parameter( values.push(Value::Parameter(

View file

@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
#[repr(i32)] #[repr(i32)]
#[derive(Default, Debug)] #[derive(Copy, Clone, Default, Debug)]
pub enum FilterMode { pub enum FilterMode {
#[default] #[default]
Linear = 0, Linear = 0,
@ -13,7 +13,7 @@ pub enum FilterMode {
} }
#[repr(i32)] #[repr(i32)]
#[derive(Default, Debug)] #[derive(Copy, Clone, Default, Debug)]
pub enum WrapMode { pub enum WrapMode {
#[default] #[default]
ClampToBorder = 0, ClampToBorder = 0,
@ -23,7 +23,7 @@ pub enum WrapMode {
} }
#[repr(i32)] #[repr(i32)]
#[derive(Default, Debug)] #[derive(Default, Copy, Clone, Debug)]
pub enum ScaleType { pub enum ScaleType {
#[default] #[default]
Input = 0, Input = 0,
@ -31,7 +31,7 @@ pub enum ScaleType {
Viewport, Viewport,
} }
#[derive(Debug)] #[derive(Copy, Clone, Debug)]
pub enum ScaleFactor { pub enum ScaleFactor {
Float(f32), Float(f32),
Absolute(i32), Absolute(i32),
@ -70,45 +70,52 @@ impl FromStr for ScaleType {
} }
} }
#[derive(Debug, Clone)]
pub struct Scaling { pub struct Scaling {
pub scale_type: ScaleType, pub scale_type: ScaleType,
pub factor: ScaleFactor, pub factor: ScaleFactor,
} }
#[derive(Debug, Clone)]
pub struct Scale2D { pub struct Scale2D {
pub valid: bool, pub valid: bool,
pub x: Scaling, pub x: Scaling,
pub y: Scaling, pub y: Scaling,
} }
#[derive(Debug, Clone)]
pub struct ShaderConfig { pub struct ShaderConfig {
pub id: usize, pub id: i32,
pub name: String, pub name: PathBuf,
pub alias: String, pub alias: Option<String>,
pub filter: FilterMode, pub filter: FilterMode,
pub wrap_mode: WrapMode, pub wrap_mode: WrapMode,
pub frame_count_mod: u32, pub frame_count_mod: u32,
pub srgb_framebuffer: bool, pub srgb_framebuffer: bool,
pub float_framebuffer: bool, pub float_framebuffer: bool,
pub feedback_pass: i32,
pub mipmap_input: bool, pub mipmap_input: bool,
pub scaling: Scale2D, pub scaling: Scale2D,
} }
#[derive(Debug, Clone)]
pub struct TextureConfig { pub struct TextureConfig {
pub name: String, pub name: String,
pub path: PathBuf, pub path: PathBuf,
pub wrap_mode: WrapMode, pub wrap_mode: WrapMode,
pub filter: FilterMode, pub filter_mode: FilterMode,
pub mipmap: bool, pub mipmap: bool,
} }
#[derive(Debug, Clone)]
pub struct Parameter { pub struct Parameter {
pub name: String, pub name: String,
pub value: f32, pub value: f32,
} }
#[derive(Debug, Clone)]
pub struct Preset { pub struct Preset {
pub shader_count: i32,
pub feedback_pass: i32,
// Everything is in Vecs because the expect number of values is well below 64. // Everything is in Vecs because the expect number of values is well below 64.
pub shaders: Vec<ShaderConfig>, pub shaders: Vec<ShaderConfig>,
pub textures: Vec<TextureConfig>, pub textures: Vec<TextureConfig>,

View file

@ -1 +1 @@
{"rustc_fingerprint":1842804115855352081,"outputs":{"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\ronny\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"llvm14-builtins-abi\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.65.0-nightly (750bd1a7f 2022-09-14)\nbinary: rustc\ncommit-hash: 750bd1a7ff3e010611b97ee75d30b7cbf5f3a03c\ncommit-date: 2022-09-14\nhost: x86_64-pc-windows-msvc\nrelease: 1.65.0-nightly\nLLVM version: 15.0.0\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\n","stderr":""}},"successes":{}} {"rustc_fingerprint":1842804115855352081,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.65.0-nightly (750bd1a7f 2022-09-14)\nbinary: rustc\ncommit-hash: 750bd1a7ff3e010611b97ee75d30b7cbf5f3a03c\ncommit-date: 2022-09-14\nhost: x86_64-pc-windows-msvc\nrelease: 1.65.0-nightly\nLLVM version: 15.0.0\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\ronny\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"llvm14-builtins-abi\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}