2022-10-20 13:47:43 +11:00
|
|
|
use crate::error::ParsePresetError;
|
2023-01-12 09:28:09 +11:00
|
|
|
use librashader_common::{FilterMode, ImageFormat, WrapMode};
|
2022-11-17 17:21:29 +11:00
|
|
|
use std::ops::Mul;
|
2022-10-18 16:07:38 +11:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
/// The configuration for a single shader pass.
|
2022-11-17 17:21:29 +11:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ShaderPassConfig {
|
2022-12-01 14:50:57 +11:00
|
|
|
/// The index of the shader pass relative to its parent preset.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub id: i32,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The fully qualified path to the shader pass source file.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub name: PathBuf,
|
2022-12-01 14:50:57 +11:00
|
|
|
/// The alias of the shader pass if available.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub alias: Option<String>,
|
2022-12-01 14:50:57 +11:00
|
|
|
/// The filtering mode that this shader pass should expect.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub filter: FilterMode,
|
2022-12-01 14:50:57 +11:00
|
|
|
/// The texture addressing (wrap) mode that this shader pass expects.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub wrap_mode: WrapMode,
|
2022-12-01 14:50:57 +11:00
|
|
|
/// The number to which to wrap the frame count before passing it to the uniforms.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub frame_count_mod: u32,
|
2022-12-01 14:50:57 +11:00
|
|
|
/// Whether or not this shader pass expects an SRGB framebuffer output.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub srgb_framebuffer: bool,
|
2022-12-01 14:50:57 +11:00
|
|
|
/// Whether or not this shader pass expects an float framebuffer output.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub float_framebuffer: bool,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Whether or not to generate mipmaps for the input texture before passing to the shader.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub mipmap_input: bool,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Specifies the scaling of the output framebuffer for this shader pass.
|
2022-11-17 17:21:29 +11:00
|
|
|
pub scaling: Scale2D,
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:28:09 +11:00
|
|
|
impl ShaderPassConfig {
|
|
|
|
/// If the framebuffer expects a different format than what was defined in the
|
|
|
|
/// shader source, returns such format.
|
|
|
|
pub fn get_format_override(&self) -> Option<ImageFormat> {
|
|
|
|
if self.srgb_framebuffer {
|
2023-01-13 13:29:42 +11:00
|
|
|
return Some(ImageFormat::R8G8B8A8Srgb);
|
2023-01-12 09:28:09 +11:00
|
|
|
} else if self.float_framebuffer {
|
2023-01-13 13:29:42 +11:00
|
|
|
return Some(ImageFormat::R16G16B16A16Sfloat);
|
2023-01-12 09:28:09 +11:00
|
|
|
}
|
2023-01-13 13:29:42 +11:00
|
|
|
return None;
|
2023-01-12 09:28:09 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:47:43 +11:00
|
|
|
#[repr(i32)]
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Default, Copy, Clone, Debug)]
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The scaling type for the shader pass.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub enum ScaleType {
|
2022-10-20 16:59:15 +11:00
|
|
|
#[default]
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scale by the size of the input quad.
|
2022-10-20 13:47:43 +11:00
|
|
|
Input = 0,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scale the framebuffer in absolute units.
|
2022-10-18 16:07:38 +11:00
|
|
|
Absolute,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scale by the size of the viewport.
|
2022-10-20 13:47:43 +11:00
|
|
|
Viewport,
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The scaling factor for framebuffer scaling.
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-10-20 13:47:43 +11:00
|
|
|
pub enum ScaleFactor {
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scale by a fractional float factor.
|
2022-10-20 13:47:43 +11:00
|
|
|
Float(f32),
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scale by an absolute factor.
|
2022-10-20 13:47:43 +11:00
|
|
|
Absolute(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ScaleFactor {
|
|
|
|
fn default() -> Self {
|
|
|
|
ScaleFactor::Float(1.0f32)
|
|
|
|
}
|
2022-10-18 16:07:38 +11:00
|
|
|
}
|
|
|
|
|
2022-11-17 17:21:29 +11:00
|
|
|
impl From<ScaleFactor> for f32 {
|
|
|
|
fn from(value: ScaleFactor) -> Self {
|
|
|
|
match value {
|
|
|
|
ScaleFactor::Float(f) => f,
|
|
|
|
ScaleFactor::Absolute(f) => f as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<ScaleFactor> for f32 {
|
|
|
|
type Output = f32;
|
|
|
|
|
|
|
|
fn mul(self, rhs: ScaleFactor) -> Self::Output {
|
|
|
|
match rhs {
|
|
|
|
ScaleFactor::Float(f) => f * self,
|
2022-11-22 08:21:50 +11:00
|
|
|
ScaleFactor::Absolute(f) => f as f32 * self,
|
2022-11-17 17:21:29 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<ScaleFactor> for u32 {
|
|
|
|
type Output = f32;
|
|
|
|
|
|
|
|
fn mul(self, rhs: ScaleFactor) -> Self::Output {
|
|
|
|
match rhs {
|
|
|
|
ScaleFactor::Float(f) => f * self as f32,
|
2022-11-22 08:21:50 +11:00
|
|
|
ScaleFactor::Absolute(f) => (f as u32 * self) as f32,
|
2022-11-17 17:21:29 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 16:07:38 +11:00
|
|
|
impl FromStr for ScaleType {
|
2022-10-20 13:47:43 +11:00
|
|
|
type Err = ParsePresetError;
|
2022-10-18 16:07:38 +11:00
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2022-10-20 13:47:43 +11:00
|
|
|
match s {
|
|
|
|
"source" => Ok(ScaleType::Input),
|
|
|
|
"viewport" => Ok(ScaleType::Viewport),
|
|
|
|
"absolute" => Ok(ScaleType::Absolute),
|
|
|
|
_ => Err(ParsePresetError::InvalidScaleType(s.to_string())),
|
|
|
|
}
|
2022-10-18 16:07:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Framebuffer scaling parameters.
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Debug, Clone)]
|
2022-10-18 16:07:38 +11:00
|
|
|
pub struct Scaling {
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The method to scale the framebuffer with.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub scale_type: ScaleType,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The factor to scale by.
|
2022-10-20 13:47:43 +11:00
|
|
|
pub factor: ScaleFactor,
|
2022-10-18 16:07:38 +11:00
|
|
|
}
|
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
/// 2D quad scaling parameters.
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Debug, Clone)]
|
2022-10-18 16:07:38 +11:00
|
|
|
pub struct Scale2D {
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Whether or not this combination of scaling factors is valid.
|
2022-10-20 13:47:43 +11:00
|
|
|
pub valid: bool,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scaling parameters for the X axis.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub x: Scaling,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Scaling parameters for the Y axis.
|
2022-10-20 13:47:43 +11:00
|
|
|
pub y: Scaling,
|
2022-10-18 16:07:38 +11:00
|
|
|
}
|
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Configuration options for a lookup texture used in the shader.
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Debug, Clone)]
|
2022-10-18 16:07:38 +11:00
|
|
|
pub struct TextureConfig {
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The name of the texture.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub name: String,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The fully qualified path to the texture.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub path: PathBuf,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The wrap (addressing) mode to use when sampling the texture.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub wrap_mode: WrapMode,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The filter mode to use when sampling the texture.
|
2022-10-21 13:52:34 +11:00
|
|
|
pub filter_mode: FilterMode,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Whether or not to generate mipmaps for this texture.
|
2022-10-20 13:47:43 +11:00
|
|
|
pub mipmap: bool,
|
2022-10-18 16:07:38 +11:00
|
|
|
}
|
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
/// Configuration options for a shader parameter.
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Debug, Clone)]
|
2022-10-22 12:04:00 +11:00
|
|
|
pub struct ParameterConfig {
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The name of the parameter.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub name: String,
|
2022-12-01 16:24:24 +11:00
|
|
|
/// The value it is set to in the preset.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub value: f32,
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
/// A shader preset including all specified parameters, textures, and paths to specified shaders.
|
|
|
|
///
|
|
|
|
/// A shader preset can be used to create a filter chain runtime instance, or reflected to get
|
|
|
|
/// parameter metadata.
|
2022-10-21 13:52:34 +11:00
|
|
|
#[derive(Debug, Clone)]
|
2022-10-22 12:04:00 +11:00
|
|
|
pub struct ShaderPreset {
|
2022-11-28 17:16:09 +11:00
|
|
|
/// Used in legacy GLSL shader semantics. If < 0, no feedback pass is used.
|
|
|
|
/// Otherwise, the FBO after pass #N is passed a texture to next frame
|
|
|
|
#[cfg(feature = "parse_legacy_glsl")]
|
2022-10-21 13:52:34 +11:00
|
|
|
pub feedback_pass: i32,
|
2022-11-28 17:16:09 +11:00
|
|
|
|
|
|
|
/// The number of shaders enabled in the filter chain.
|
|
|
|
pub shader_count: i32,
|
2022-10-18 16:07:38 +11:00
|
|
|
// Everything is in Vecs because the expect number of values is well below 64.
|
2022-11-28 17:16:09 +11:00
|
|
|
/// Preset information for each shader.
|
2022-10-22 12:04:00 +11:00
|
|
|
pub shaders: Vec<ShaderPassConfig>,
|
2022-11-28 17:16:09 +11:00
|
|
|
|
|
|
|
/// Preset information for each texture.
|
2022-10-18 16:07:38 +11:00
|
|
|
pub textures: Vec<TextureConfig>,
|
2022-11-28 17:16:09 +11:00
|
|
|
|
|
|
|
/// Preset information for each user parameter.
|
2022-10-22 12:04:00 +11:00
|
|
|
pub parameters: Vec<ParameterConfig>,
|
2022-10-20 13:47:43 +11:00
|
|
|
}
|