api: do some renaming of structs

This commit is contained in:
chyyran 2022-10-21 21:04:00 -04:00
parent 2f85ea9f24
commit de161373a9
12 changed files with 179 additions and 37 deletions

View file

@ -4,6 +4,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/librashader-preprocess/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader-presets/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

7
Cargo.lock generated
View file

@ -8,11 +8,16 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c"
[[package]]
name = "librashader"
version = "0.1.0"
[[package]]
name = "librashader-preprocess"
version = "0.1.0"
dependencies = [
"librashader-presets",
"librashader",
"nom",
"thiserror",
]

View file

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

View file

@ -7,4 +7,5 @@ edition = "2021"
[dependencies]
thiserror = "1.0.37"
librashader-presets = { path = "../librashader-presets" }
nom = "7.1.1"
librashader = { path = "../librashader"}

View file

@ -1,16 +1,20 @@
use crate::PreprocessError;
use std::fs::File;
use std::io::Read;
use std::str::Lines;
use std::path::Path;
use crate::PreprocessError;
use std::str::Lines;
const GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE: &'static str = "#extension GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE : require";
const GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE: &'static str =
"#extension GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE : require";
trait PushLine {
trait SourceOutput {
fn push_line(&mut self, str: &str);
fn mark_line(&mut self, line_no: usize, comment: &str) {
self.push_line(&format!("#line {} \"{}\"", line_no, comment))
}
}
impl PushLine for String {
impl SourceOutput for String {
fn push_line(&mut self, str: &str) {
self.push_str(str);
self.push('\n');
@ -20,7 +24,8 @@ impl PushLine for String {
fn read_file(path: impl AsRef<Path>) -> Result<String, PreprocessError> {
let path = path.as_ref();
let mut source = String::new();
File::open(path).and_then(|mut f| f.read_to_string(&mut source))
File::open(path)
.and_then(|mut f| f.read_to_string(&mut source))
.map_err(|e| PreprocessError::IOError(path.to_path_buf(), e))?;
Ok(source)
}
@ -35,15 +40,15 @@ pub fn read_source(path: impl AsRef<Path>) -> Result<String, PreprocessError> {
if let Some(header) = lines.next() {
if !header.starts_with("#version ") {
return Err(PreprocessError::MissingVersionHeader)
return Err(PreprocessError::MissingVersionHeader);
}
output.push_line(header);
} else {
return Err(PreprocessError::UnexpectedEof)
return Err(PreprocessError::UnexpectedEof);
}
output.push_line(GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE);
mark_line(2, path.file_name().and_then(|f| f.to_str()).unwrap_or(""), &mut output);
output.mark_line(2, path.file_name().and_then(|f| f.to_str()).unwrap_or(""));
preprocess(lines, path, &mut output)?;
Ok(output)
@ -53,7 +58,11 @@ fn mark_line(line_no: usize, comment: &str, output: &mut String) {
output.push_line(&format!("#line {} \"{}\"", line_no, comment))
}
fn preprocess(lines: Lines, file_name: impl AsRef<Path>, output: &mut String) -> Result<(), PreprocessError> {
fn preprocess(
lines: Lines,
file_name: impl AsRef<Path>,
output: &mut String,
) -> Result<(), PreprocessError> {
let file_name = file_name.as_ref();
let include_path = file_name.parent().unwrap();
let file_name = file_name.file_name().and_then(|f| f.to_str()).unwrap_or("");
@ -62,7 +71,7 @@ fn preprocess(lines: Lines, file_name: impl AsRef<Path>, output: &mut String) ->
if line.starts_with("#include ") {
let include_file = line["#include ".len()..].trim().trim_matches('"');
if include_file.is_empty() {
return Err(PreprocessError::UnexpectedEol(line_no))
return Err(PreprocessError::UnexpectedEol(line_no));
}
let mut include_path = include_path.to_path_buf();
@ -72,19 +81,22 @@ fn preprocess(lines: Lines, file_name: impl AsRef<Path>, output: &mut String) ->
let source = source.trim();
let lines = source.lines();
let include_file = include_path.file_name().and_then(|f| f.to_str()).unwrap_or("");
mark_line(1, include_file, output);
let include_file = include_path
.file_name()
.and_then(|f| f.to_str())
.unwrap_or("");
output.mark_line(1, include_file);
preprocess(lines, include_path, output)?;
mark_line(line_no + 1, file_name, output);
output.mark_line(line_no + 1, file_name);
continue;
}
if line.starts_with("#endif") || line.starts_with("#pragma") {
output.push_line(line);
mark_line(line_no + 2, file_name, output);
output.mark_line(line_no + 2, file_name);
continue;
}
output.push_line(line)
}
Ok(())
}
}

View file

@ -1,15 +1,17 @@
mod error;
mod include;
mod pragma;
pub use error::*;
#[cfg(test)]
mod test {
use crate::include::read_source;
#[test]
pub fn preprocess_file() {
let result = read_source("../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang").unwrap();
let result =
read_source("../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang")
.unwrap();
eprintln!("{result}")
}
}
}

View file

@ -0,0 +1,5 @@
use librashader::ShaderParameter;
pub fn parse_pragma_parameter(source: impl AsRef<str>) -> Vec<ShaderParameter> {
}

View file

@ -13,14 +13,14 @@ pub(crate) use token::Token;
use crate::error::ParsePresetError;
use crate::parse::preset::resolve_values;
use crate::parse::value::parse_preset;
use crate::Preset;
use crate::ShaderPreset;
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> {
impl ShaderPreset {
pub fn try_parse(path: impl AsRef<Path>) -> Result<ShaderPreset, ParsePresetError> {
let values = parse_preset(path)?;
Ok(resolve_values(values))
}
@ -28,14 +28,14 @@ impl Preset {
#[cfg(test)]
mod test {
use crate::Preset;
use crate::ShaderPreset;
use std::path::PathBuf;
#[test]
pub fn parse_preset() {
let root =
PathBuf::from("../test/slang-shaders/ntsc/ntsc-256px-svideo.slangp");
let basic = Preset::try_parse(root);
let basic = ShaderPreset::try_parse(root);
eprintln!("{:#?}", basic);
assert!(basic.is_ok());
}

View file

@ -1,8 +1,8 @@
use crate::parse::remove_if;
use crate::parse::value::Value;
use crate::{Parameter, Preset, Scale2D, Scaling, ShaderConfig, TextureConfig};
use crate::{ParameterConfig, ShaderPreset, Scale2D, Scaling, ShaderPassConfig, TextureConfig};
pub fn resolve_values(mut values: Vec<Value>) -> Preset {
pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset {
let textures: Vec<TextureConfig> = values
.drain_filter(|f| matches!(*f, Value::Texture { .. }))
.map(|value| {
@ -26,11 +26,11 @@ pub fn resolve_values(mut values: Vec<Value>) -> Preset {
}
})
.collect();
let parameters: Vec<Parameter> = values
let parameters: Vec<ParameterConfig> = values
.drain_filter(|f| matches!(*f, Value::Parameter { .. }))
.map(|value| {
if let Value::Parameter(name, value) = value {
Parameter { name, value }
ParameterConfig { name, value }
} else {
unreachable!("values should be all of type parameters")
}
@ -112,7 +112,7 @@ pub fn resolve_values(mut values: Vec<Value>) -> Preset {
scale_y = scale;
}
let shader = ShaderConfig {
let shader = ShaderPassConfig {
id,
name,
alias: shader_values.iter().find_map(|f| match f {
@ -178,7 +178,7 @@ pub fn resolve_values(mut values: Vec<Value>) -> Preset {
}
}
Preset {
ShaderPreset {
shader_count,
feedback_pass,
shaders,

View file

@ -84,7 +84,7 @@ pub struct Scale2D {
}
#[derive(Debug, Clone)]
pub struct ShaderConfig {
pub struct ShaderPassConfig {
pub id: i32,
pub name: PathBuf,
pub alias: Option<String>,
@ -107,17 +107,17 @@ pub struct TextureConfig {
}
#[derive(Debug, Clone)]
pub struct Parameter {
pub struct ParameterConfig {
pub name: String,
pub value: f32,
}
#[derive(Debug, Clone)]
pub struct Preset {
pub struct ShaderPreset {
pub shader_count: i32,
pub feedback_pass: i32,
// Everything is in Vecs because the expect number of values is well below 64.
pub shaders: Vec<ShaderConfig>,
pub shaders: Vec<ShaderPassConfig>,
pub textures: Vec<TextureConfig>,
pub parameters: Vec<Parameter>,
pub parameters: Vec<ParameterConfig>,
}

8
librashader/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "librashader"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

107
librashader/src/lib.rs Normal file
View file

@ -0,0 +1,107 @@
use std::convert::Infallible;
use std::str::FromStr;
pub struct ShaderSource {
pub vertex: String,
pub fragment: String,
pub name: Option<String>,
pub parameters: Vec<ShaderParameter>,
pub format: ShaderFormat,
}
pub struct ShaderParameter {
pub id: String,
pub description: String,
pub initial: f32,
pub minimum: f32,
pub maximum: f32,
pub step: f32,
}
#[repr(u32)]
pub enum ShaderFormat {
Unknown = 0,
/* 8-bit */
R8Unorm,
R8Uint,
R8Sint,
R8G8Unorm,
R8G8Uint,
R8G8Sint,
R8G8B8A8Unorm,
R8G8B8A8Uint,
R8G8B8A8Sint,
R8G8B8A8Srgb,
/* 10-bit */
A2B10G10R10UnormPack32,
A2B10G10R10UintPack32,
/* 16-bit */
R16Uint,
R16Sint,
R16Sfloat,
R16G16Uint,
R16G16Sint,
R16G16Sfloat,
R16G16B16A16Uint,
R16G16B16A16Sint,
R16G16B16A16Sfloat,
/* 32-bit */
R32Uint,
R32Sint,
R32Sfloat,
R32G32Uint,
R32G32Sint,
R32G32Sfloat,
R32G32B32A32Uint,
R32G32B32A32Sint,
R32G32B32A32Sfloat,
}
impl FromStr for ShaderFormat {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"UNKNOWN" => Self::Unknown,
"R8_UNORM" => Self::R8Unorm,
"R8_UINT" => Self::R8Uint,
"R8_SINT" => Self::R8Sint,
"R8G8_UNORM" => Self::R8G8Unorm,
"R8G8_UINT" => Self::R8Uint,
"R8G8_SINT" => Self::R8G8Sint,
"R8G8B8A8_UNORM" => Self::R8G8B8A8Unorm,
"R8G8B8A8_UINT" => Self::R8G8B8A8Uint,
"R8G8B8A8_SINT" => Self::R8G8B8A8Sint,
"R8G8B8A8_SRGB" => Self::R8G8B8A8Srgb,
"A2B10G10R10_UNORM_PACK32" => Self::A2B10G10R10UnormPack32,
"A2B10G10R10_UINT_PACK32" => Self::A2B10G10R10UintPack32,
"R16_UINT" => Self::R16Uint,
"R16_SINT" => Self::R16Sint,
"R16_SFLOAT" => Self::R16Sfloat,
"R16G16_UINT" => Self::R16G16Uint,
"R16G16_SINT" => Self::R16G16Sint,
"R16G16_SFLOAT" => Self::R16G16Sfloat,
"R16G16B16A16_UINT" => Self::R16G16B16A16Uint,
"R16G16B16A16_SINT" => Self::R16G16B16A16Sint,
"R16G16B16A16_SFLOAT" => Self::R16G16B16A16Sfloat,
"R32_UINT" => Self::R32Uint,
"R32_SINT" => Self::R32Sint,
"R32_SFLOAT" => Self::R32Sfloat,
"R32G32_UINT" => Self::R32G32Uint,
"R32G32_SINT" => Self::R32G32Sint,
"R32G32_SFLOAT" => Self::R32G32Sfloat,
"R32G32B32A32_UINT" => Self::R32G32A32Uint,
"R32G32B32A32_SINT" => Self::R32G32A32Sint,
"R32G32B32A32_SFLOAT" => Self::R32G32SA32float,
_ => Self::Unknown,
})
}
}