lib: sort out base library structure

This commit is contained in:
chyyran 2022-11-21 16:53:36 -05:00
parent 38ce621664
commit 86ad32ff0a
12 changed files with 292 additions and 200 deletions

View file

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

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = [ "opengl" ]
default = []
opengl = ["gl"]
[dependencies]

View file

@ -1,5 +1,5 @@
use crate::back::targets::{CompilerBackend, FromCompilation, GLSL, HLSL};
use crate::back::CompileShader;
use crate::back::targets::{GLSL, HLSL};
use crate::back::{CompilerBackend, CompileShader, FromCompilation};
use crate::error::ShaderReflectError;
use crate::front::shaderc::GlslangCompilation;
use crate::reflect::cross::{CompiledAst, GlslReflect, HlslReflect};

View file

@ -2,8 +2,10 @@ pub mod cross;
pub mod targets;
use std::fmt::Debug;
pub use targets::CompileShader;
use crate::back::targets::OutputTarget;
use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::reflect::{ReflectShader, ShaderReflection};
use crate::reflect::semantics::ReflectSemantics;
#[derive(Debug)]
pub struct ShaderCompilerOutput<T, Context = ()> {
@ -11,3 +13,59 @@ pub struct ShaderCompilerOutput<T, Context = ()> {
pub fragment: T,
pub context: Context,
}
pub trait CompileShader<T: OutputTarget> {
type Options;
type Context;
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;
}
impl<T, E> CompileShader<E> for CompilerBackend<T>
where
T: CompileShader<E>,
E: OutputTarget,
{
type Options = T::Options;
type Context = T::Context;
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<E::Output, Self::Context>, ShaderCompileError> {
self.backend.compile(options)
}
}
pub trait FromCompilation<T> {
type Target: OutputTarget;
type Options;
type Context;
fn from_compilation(
compile: T,
) -> Result<
CompilerBackend<impl CompileShader<Self::Target, Context = Self::Context> + ReflectShader>,
ShaderReflectError,
>;
}
pub struct CompilerBackend<T> {
pub(crate) backend: T,
}
impl<T> ReflectShader for CompilerBackend<T>
where
T: ReflectShader,
{
fn reflect(
&mut self,
pass_number: usize,
semantics: &ReflectSemantics,
) -> Result<ShaderReflection, ShaderReflectError> {
self.backend.reflect(pass_number, semantics)
}
}

View file

@ -1,6 +1,7 @@
use crate::back::ShaderCompilerOutput;
use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::reflect::{ReflectSemantics, ReflectShader, ShaderReflection};
use crate::reflect::{ReflectShader, ShaderReflection};
use crate::reflect::semantics::ReflectSemantics;
pub trait OutputTarget {
type Output;
@ -8,7 +9,7 @@ pub trait OutputTarget {
pub struct GLSL;
pub struct HLSL;
pub struct SpirV;
pub struct SPIRV;
pub struct MSL;
impl OutputTarget for GLSL {
@ -17,68 +18,13 @@ impl OutputTarget for GLSL {
impl OutputTarget for HLSL {
type Output = String;
}
impl OutputTarget for SpirV {
impl OutputTarget for SPIRV {
type Output = Vec<u32>;
}
pub struct CompilerBackend<T> {
pub(crate) backend: T,
}
pub trait FromCompilation<T> {
type Target: OutputTarget;
type Options;
type Context;
fn from_compilation(
compile: T,
) -> Result<
CompilerBackend<impl CompileShader<Self::Target, Context = Self::Context> + ReflectShader>,
ShaderReflectError,
>;
}
pub trait CompileShader<T: OutputTarget> {
type Options;
type Context;
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;
}
impl<T> ReflectShader for CompilerBackend<T>
where
T: ReflectShader,
{
fn reflect(
&mut self,
pass_number: usize,
semantics: &ReflectSemantics,
) -> Result<ShaderReflection, ShaderReflectError> {
self.backend.reflect(pass_number, semantics)
}
}
impl<T, E> CompileShader<E> for CompilerBackend<T>
where
T: CompileShader<E>,
E: OutputTarget,
{
type Options = T::Options;
type Context = T::Context;
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<E::Output, Self::Context>, ShaderCompileError> {
self.backend.compile(options)
}
}
mod test {
use crate::back::targets::{FromCompilation, GLSL};
use crate::back::FromCompilation;
use crate::back::targets::GLSL;
use crate::front::shaderc::GlslangCompilation;
pub fn huh(value: GlslangCompilation) {
let _x = GLSL::from_compilation(value).unwrap();

View file

@ -1,17 +1,13 @@
use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError};
use crate::front::shaderc::GlslangCompilation;
use crate::reflect::semantics::{
BindingStage, MemberOffset, PushReflection, ShaderReflection, TextureImage, TextureSemantics,
TextureSizeMeta, TypeInfo, UboReflection, ValidateTypeSemantics, VariableMeta,
VariableSemantics, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE,
};
use crate::reflect::semantics::{BindingStage, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, MemberOffset, PushReflection, ReflectSemantics, ShaderReflection, TextureImage, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo, UboReflection, ValidateTypeSemantics, VariableMeta, VariableSemanticMap, VariableSemantics};
use crate::reflect::{
ReflectMeta, ReflectSemantics, ReflectShader, TextureSemanticMap, VariableSemanticMap,
ReflectMeta, ReflectShader,
};
use spirv_cross::hlsl::ShaderModel;
use spirv_cross::spirv::{Ast, Decoration, Module, Resource, ShaderResources, Type};
use spirv_cross::{glsl, hlsl, ErrorCode};
use spirv_cross::{ErrorCode, glsl, hlsl};
use crate::back::cross::GlslangGlslContext;
use crate::back::targets::{GLSL, HLSL};
@ -846,11 +842,11 @@ impl CompileShader<HLSL> for CrossReflect<hlsl::Target> {
#[cfg(test)]
mod test {
use crate::reflect::cross::CrossReflect;
use crate::reflect::{ReflectSemantics, ReflectShader, UniformSemantic};
use crate::reflect::ReflectShader;
use rustc_hash::FxHashMap;
use crate::back::CompileShader;
use crate::reflect::semantics::{SemanticMap, VariableSemantics};
use crate::reflect::semantics::{ReflectSemantics, SemanticMap, UniformSemantic, VariableSemantics};
use librashader_preprocess::ShaderSource;
use spirv_cross::glsl;
use spirv_cross::glsl::{CompilerOptions, Version};

View file

@ -4,6 +4,7 @@ use crate::reflect::semantics::{
};
use rustc_hash::FxHashMap;
use std::str::FromStr;
use semantics::ReflectSemantics;
pub mod cross;
@ -22,120 +23,6 @@ pub trait ReflectShader {
) -> Result<ShaderReflection, ShaderReflectError>;
}
pub trait TextureSemanticMap<T> {
fn get_texture_semantic(&self, name: &str) -> Option<SemanticMap<TextureSemantics>>;
}
pub trait VariableSemanticMap<T> {
fn get_variable_semantic(&self, name: &str) -> Option<SemanticMap<VariableSemantics, ()>>;
}
impl VariableSemanticMap<UniformSemantic> for FxHashMap<String, UniformSemantic> {
fn get_variable_semantic(&self, name: &str) -> Option<SemanticMap<VariableSemantics, ()>> {
match self.get(name) {
// existing uniforms in the semantic map have priority
None => match name {
"MVP" => Some(SemanticMap {
semantics: VariableSemantics::MVP,
index: (),
}),
"OutputSize" => Some(SemanticMap {
semantics: VariableSemantics::Output,
index: (),
}),
"FinalViewportSize" => Some(SemanticMap {
semantics: VariableSemantics::FinalViewport,
index: (),
}),
"FrameCount" => Some(SemanticMap {
semantics: VariableSemantics::FrameCount,
index: (),
}),
"FrameDirection" => Some(SemanticMap {
semantics: VariableSemantics::FrameDirection,
index: (),
}),
_ => None,
},
Some(UniformSemantic::Variable(variable)) => Some(*variable),
Some(UniformSemantic::Texture(_)) => None,
}
}
}
impl TextureSemanticMap<UniformSemantic> for FxHashMap<String, UniformSemantic> {
fn get_texture_semantic(&self, name: &str) -> Option<SemanticMap<TextureSemantics>> {
match self.get(name) {
None => {
if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS
.iter()
.find(|f| name.starts_with(f.size_uniform_name()))
{
if semantics.is_array() {
let index = &name[semantics.size_uniform_name().len()..];
let Ok(index) = usize::from_str(index) else {
return None;
};
return Some(SemanticMap {
semantics: *semantics,
index,
});
} else if name == semantics.size_uniform_name() {
return Some(SemanticMap {
semantics: *semantics,
index: 0,
});
}
}
None
}
Some(UniformSemantic::Variable(_)) => None,
Some(UniformSemantic::Texture(texture)) => Some(*texture),
}
}
}
impl TextureSemanticMap<UniformSemantic> for FxHashMap<String, SemanticMap<TextureSemantics>> {
fn get_texture_semantic(&self, name: &str) -> Option<SemanticMap<TextureSemantics>> {
match self.get(name) {
None => {
if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS
.iter()
.find(|f| name.starts_with(f.texture_name()))
{
if semantics.is_array() {
let index = &name[semantics.texture_name().len()..];
let Ok(index) = usize::from_str(index) else {return None};
return Some(SemanticMap {
semantics: *semantics,
index,
});
} else if name == semantics.texture_name() {
return Some(SemanticMap {
semantics: *semantics,
index: 0,
});
}
}
None
}
Some(texture) => Some(*texture),
}
}
}
#[derive(Debug, Clone)]
pub enum UniformSemantic {
Variable(SemanticMap<VariableSemantics, ()>),
Texture(SemanticMap<TextureSemantics>),
}
#[derive(Debug, Clone)]
pub struct ReflectSemantics {
pub uniform_semantics: FxHashMap<String, UniformSemantic>,
pub non_uniform_semantics: FxHashMap<String, SemanticMap<TextureSemantics>>,
}
#[derive(Debug, Default)]
pub struct ReflectMeta {
pub parameter_meta: FxHashMap<String, VariableMeta>,

View file

@ -1,5 +1,7 @@
use crate::reflect::ReflectMeta;
use bitflags::bitflags;
use rustc_hash::FxHashMap;
use std::str::FromStr;
pub const BASE_SEMANTICS_COUNT: usize = 5;
pub const MAX_BINDINGS_COUNT: u32 = 16;
@ -208,3 +210,117 @@ impl UniformMeta for TextureSizeMeta {
&self.id
}
}
pub trait TextureSemanticMap<T> {
fn get_texture_semantic(&self, name: &str) -> Option<SemanticMap<TextureSemantics>>;
}
impl TextureSemanticMap<UniformSemantic> for FxHashMap<String, UniformSemantic> {
fn get_texture_semantic(&self, name: &str) -> Option<SemanticMap<TextureSemantics>> {
match self.get(name) {
None => {
if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS
.iter()
.find(|f| name.starts_with(f.size_uniform_name()))
{
if semantics.is_array() {
let index = &name[semantics.size_uniform_name().len()..];
let Ok(index) = usize::from_str(index) else {
return None;
};
return Some(SemanticMap {
semantics: *semantics,
index,
});
} else if name == semantics.size_uniform_name() {
return Some(SemanticMap {
semantics: *semantics,
index: 0,
});
}
}
None
}
Some(UniformSemantic::Variable(_)) => None,
Some(UniformSemantic::Texture(texture)) => Some(*texture),
}
}
}
impl TextureSemanticMap<UniformSemantic> for FxHashMap<String, SemanticMap<TextureSemantics>> {
fn get_texture_semantic(&self, name: &str) -> Option<SemanticMap<TextureSemantics>> {
match self.get(name) {
None => {
if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS
.iter()
.find(|f| name.starts_with(f.texture_name()))
{
if semantics.is_array() {
let index = &name[semantics.texture_name().len()..];
let Ok(index) = usize::from_str(index) else {return None};
return Some(SemanticMap {
semantics: *semantics,
index,
});
} else if name == semantics.texture_name() {
return Some(SemanticMap {
semantics: *semantics,
index: 0,
});
}
}
None
}
Some(texture) => Some(*texture),
}
}
}
pub trait VariableSemanticMap<T> {
fn get_variable_semantic(&self, name: &str) -> Option<SemanticMap<VariableSemantics, ()>>;
}
impl VariableSemanticMap<UniformSemantic> for FxHashMap<String, UniformSemantic> {
fn get_variable_semantic(&self, name: &str) -> Option<SemanticMap<VariableSemantics, ()>> {
match self.get(name) {
// existing uniforms in the semantic map have priority
None => match name {
"MVP" => Some(SemanticMap {
semantics: VariableSemantics::MVP,
index: (),
}),
"OutputSize" => Some(SemanticMap {
semantics: VariableSemantics::Output,
index: (),
}),
"FinalViewportSize" => Some(SemanticMap {
semantics: VariableSemantics::FinalViewport,
index: (),
}),
"FrameCount" => Some(SemanticMap {
semantics: VariableSemantics::FrameCount,
index: (),
}),
"FrameDirection" => Some(SemanticMap {
semantics: VariableSemantics::FrameDirection,
index: (),
}),
_ => None,
},
Some(UniformSemantic::Variable(variable)) => Some(*variable),
Some(UniformSemantic::Texture(_)) => None,
}
}
}
#[derive(Debug, Clone)]
pub enum UniformSemantic {
Variable(SemanticMap<VariableSemantics, ()>),
Texture(SemanticMap<TextureSemantics>),
}
#[derive(Debug, Clone)]
pub struct ReflectSemantics {
pub uniform_semantics: FxHashMap<String, UniformSemantic>,
pub non_uniform_semantics: FxHashMap<String, SemanticMap<TextureSemantics>>,
}

View file

@ -1,14 +1,14 @@
use librashader_preprocess::ShaderSource;
use librashader_presets::ShaderPassConfig;
use librashader_reflect::back::targets::{FromCompilation, HLSL};
use librashader_reflect::back::CompileShader;
use librashader_reflect::back::targets::HLSL;
use librashader_reflect::back::{CompileShader, FromCompilation};
use rustc_hash::FxHashMap;
use std::error::Error;
use std::path::Path;
use librashader_reflect::front::shaderc::GlslangCompilation;
use librashader_reflect::reflect::semantics::{SemanticMap, TextureSemantics, VariableSemantics};
use librashader_reflect::reflect::{ReflectSemantics, ReflectShader, UniformSemantic};
use librashader_reflect::reflect::semantics::{ReflectSemantics, SemanticMap, TextureSemantics, UniformSemantic, VariableSemantics};
use librashader_reflect::reflect::ReflectShader;
pub fn load_pass_semantics(
uniform_semantics: &mut FxHashMap<String, UniformSemantic>,

View file

@ -10,18 +10,16 @@ use librashader_common::image::Image;
use librashader_common::{FilterMode, Size, WrapMode};
use librashader_preprocess::ShaderSource;
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::cross::{GlVersion, GlslangGlslContext};
use librashader_reflect::back::targets::{CompilerBackend, FromCompilation, GLSL};
use librashader_reflect::back::CompileShader;
use librashader_reflect::reflect::semantics::{
MemberOffset, SemanticMap, TextureSemantics, UniformMeta, VariableSemantics,
};
use librashader_reflect::reflect::{ReflectSemantics, ReflectShader, UniformSemantic};
use librashader_reflect::back::cross::{GlslangGlslContext, GlVersion};
use librashader_reflect::back::targets::GLSL;
use librashader_reflect::reflect::semantics::{MemberOffset, ReflectSemantics, SemanticMap, TextureSemantics, UniformMeta, UniformSemantic, VariableSemantics};
use librashader_reflect::reflect::ReflectShader;
use rustc_hash::FxHashMap;
use spirv_cross::spirv::Decoration;
use std::collections::VecDeque;
use std::error::Error;
use std::path::Path;
use librashader_reflect::back::{CompilerBackend, CompileShader, FromCompilation};
use librashader_reflect::front::shaderc::GlslangCompilation;
pub struct FilterChain {

18
librashader/Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[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]
librashader-common = { path = "../librashader-common" }
librashader-presets = { path = "../librashader-presets" }
librashader-preprocess = { path = "../librashader-preprocess" }
librashader-reflect = { path = "../librashader-reflect" }
librashader-runtime-dx11 = { path = "../librashader-runtime-dx11" }
librashader-runtime-gl = { path = "../librashader-runtime-gl" }
[features]
gl = [ "librashader-common/opengl" ]

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

@ -0,0 +1,72 @@
pub mod presets {
pub use librashader_presets::*;
}
pub mod preprocess {
pub use librashader_preprocess::*;
}
pub mod reflect {
pub use librashader_reflect::error::*;
pub use librashader_reflect::reflect::{
ReflectMeta, ReflectShader, semantics, ShaderReflection
};
pub use librashader_reflect::front::shaderc::GlslangCompilation;
pub use librashader_reflect::back::{
CompileShader,
FromCompilation,
ShaderCompilerOutput,
CompilerBackend,
targets::OutputTarget,
};
}
pub mod targets {
/// Shader compiler targets and runtime for OpenGL.
pub mod gl {
/// Shader compiler target for GLSL.
pub use librashader_reflect::back::targets::GLSL;
/// Shader runtime for OpenGL.
pub mod runtime {
pub use librashader_runtime_gl::*;
}
}
/// Shader compiler targets and runtime for DirectX.
pub mod dx {
/// Shader compiler target for HLSL.
pub use librashader_reflect::back::targets::HLSL;
/// Shader runtime for DirectX.
pub mod runtime {
/// Shader runtime for DirectX 11.
pub mod dx11 {
pub use librashader_runtime_dx11::*;
}
/// Shader runtime for DirectX 12.
pub mod dx12 {
pub use librashader_runtime_dx11::*;
}
}
}
/// Shader compiler targets and runtime for Vulkan.
pub mod vk {
/// Shader compiler target for SPIR-V.
pub use librashader_reflect::back::targets::SPIRV;
}
}
pub use librashader_common::{
FilterMode,
ShaderFormat,
Size,
WrapMode
};