reflect: get rid of redundant shader compiler argument

This commit is contained in:
chyyran 2024-02-13 17:37:48 -05:00 committed by Ronny Chan
parent 76aa5ce4c6
commit cc93e37701
13 changed files with 51 additions and 43 deletions

View file

@ -14,7 +14,7 @@ pub struct CachedCompilation<T> {
compilation: T,
}
impl<T: ShaderReflectObject> ShaderReflectObject for CachedCompilation<T> {}
impl<T: ShaderReflectObject> ShaderReflectObject for CachedCompilation<T> { type Compiler = T::Compiler; }
impl<T: ShaderReflectObject + for<'de> serde::Deserialize<'de> + serde::Serialize + Clone>
ShaderInputCompiler<CachedCompilation<T>> for Glslang

View file

@ -4,7 +4,10 @@ use serde::{Deserialize, Serialize};
mod glslang;
pub trait ShaderReflectObject: Sized {}
pub trait ShaderReflectObject: Sized {
/// The compiler that produces this reflect object.
type Compiler;
}
pub use crate::front::glslang::Glslang;
@ -15,7 +18,9 @@ pub trait ShaderInputCompiler<O: ShaderReflectObject>: Sized {
}
/// Marker trait for types that are the reflectable outputs of a shader compilation.
impl ShaderReflectObject for SpirvCompilation {}
impl ShaderReflectObject for SpirvCompilation {
type Compiler = Glslang;
}
/// A reflectable shader compilation via glslang.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]

View file

@ -25,15 +25,15 @@
//! source: &ShaderSource,
//! ) -> Result<Artifact, Box<dyn Error>>
//! {
//! let artifact = Glslang::compile(&source)?;
//! let spirv = SPIRV::from_compilation(artifact)?;
//! let compilation = SpirvCompilation::try_from(&source);
//! let spirv = SPIRV::from_compilation(compilation)?;
//! Ok(spirv)
//! }
//!
//! // Compile preset
//! pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>>
//! {
//! let (passes, semantics) = SPIRV::compile_preset_passes::<Glslang, SpirvCompilation, Box<dyn Error>>(
//! let (passes, semantics) = SPIRV::compile_preset_passes::<SpirvCompilation, SpirvCross, Box<dyn Error>>(
//! preset.shaders, &preset.textures)?;
//! Ok((passes, semantics))
//! }

View file

@ -23,7 +23,7 @@ use rustc_hash::FxHashMap;
/// use librashader_reflect::reflect::cross::SpirvCross;
/// use librashader_reflect::reflect::presets::ShaderPassArtifact;
///
/// type VulkanPassMeta = ShaderPassArtifact<impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross<GLSL>>>;
/// type VulkanPassMeta = ShaderPassArtifact<impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross>>;
/// ```
///
/// This allows a runtime to not name the backing type of the compiled artifact if not necessary.
@ -36,7 +36,7 @@ impl<T: OutputTarget> CompilePresetTarget for T {}
pub trait CompilePresetTarget: OutputTarget {
/// Compile passes of a shader preset given the applicable
/// shader output target, compilation type, and resulting error.
fn compile_preset_passes<C, I, R, E>(
fn compile_preset_passes<I, R, E>(
passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
) -> Result<
@ -50,18 +50,18 @@ pub trait CompilePresetTarget: OutputTarget {
I: ShaderReflectObject,
Self: Sized,
Self: FromCompilation<I, R>,
C: ShaderInputCompiler<I>,
I::Compiler: ShaderInputCompiler<I>,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
{
compile_preset_passes::<Self, C, I, R, E>(passes, textures)
compile_preset_passes::<Self, I, R, E>(passes, textures)
}
}
/// Compile passes of a shader preset given the applicable
/// shader output target, compilation type, and resulting error.
fn compile_preset_passes<T, C, I, R, E>(
fn compile_preset_passes<T, I, R, E>(
passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
) -> Result<
@ -75,7 +75,7 @@ where
I: ShaderReflectObject,
T: OutputTarget,
T: FromCompilation<I, R>,
C: ShaderInputCompiler<I>,
I::Compiler: ShaderInputCompiler<I>,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
@ -88,7 +88,7 @@ where
.map(|shader| {
let source: ShaderSource = ShaderSource::load(&shader.name)?;
let compiled = C::compile(&source)?;
let compiled = I::Compiler::compile(&source)?;
let reflect = T::from_compilation(compiled)?;
for parameter in source.parameters.values() {

View file

@ -4,7 +4,7 @@ use librashader_common::{ImageFormat, Size, Viewport};
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::HLSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::front::{SpirvCompilation};
use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::image::{Image, ImageError, UVDirection};
@ -84,13 +84,12 @@ fn compile_passes(
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache {
HLSL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
HLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
HLSL::compile_preset_passes::<SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -18,7 +18,7 @@ use librashader_common::{ImageFormat, Size, Viewport};
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::{DXIL, HLSL};
use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::front::{SpirvCompilation};
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::{ShaderSemantics, MAX_BINDINGS_COUNT};
use librashader_reflect::reflect::ReflectShader;
@ -155,13 +155,12 @@ fn compile_passes_dxil(
) -> Result<(Vec<DxilShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache {
DXIL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
DXIL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
DXIL::compile_preset_passes::<SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};
@ -177,13 +176,12 @@ fn compile_passes_hlsl(
) -> Result<(Vec<HlslShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache {
HLSL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
HLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
HLSL::compile_preset_passes::<SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -16,7 +16,7 @@ use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::glsl::GlslVersion;
use librashader_reflect::back::targets::GLSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::front::{SpirvCompilation};
use librashader_reflect::reflect::semantics::{ShaderSemantics, UniformMeta};
use librashader_cache::CachedCompilation;
@ -108,13 +108,12 @@ fn compile_passes(
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache {
GLSL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
GLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
GLSL::compile_preset_passes::<SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -20,7 +20,7 @@ use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::msl::MslVersion;
use librashader_reflect::back::targets::MSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::front::SpirvCompilation;
use librashader_reflect::reflect::cross::SpirvCross;
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::ShaderSemantics;
@ -47,7 +47,7 @@ fn compile_passes(
textures: &[TextureConfig],
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) =
MSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
MSL::compile_preset_passes::<SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?;
Ok((passes, semantics))
@ -396,7 +396,7 @@ impl FilterChainMetal {
source
.texture
.setLabel(Some(&*NSString::from_str("librashader_sourcetex")));
// swap output and feedback **before** recording command buffers
std::mem::swap(
&mut self.output_framebuffers,

View file

@ -1,6 +1,10 @@
use crate::error::{FilterChainError, Result};
use crate::texture::InputTexture;
use icrate::Metal::{MTLBlitCommandEncoder, MTLDevice, MTLOrigin, MTLPixelFormatBGRA8Unorm, MTLRegion, MTLResourceStorageModeManaged, MTLResourceStorageModeShared, MTLSize, MTLTexture, MTLTextureDescriptor, MTLTextureUsageShaderRead};
use icrate::Metal::{
MTLBlitCommandEncoder, MTLDevice, MTLOrigin, MTLPixelFormatBGRA8Unorm, MTLRegion,
MTLResourceStorageModeManaged, MTLResourceStorageModeShared, MTLSize, MTLTexture,
MTLTextureDescriptor, MTLTextureUsageShaderRead,
};
use librashader_presets::TextureConfig;
use librashader_runtime::image::{Image, BGRA8};
use librashader_runtime::scaling::MipmapSize;
@ -39,12 +43,13 @@ impl LutTexture {
1
});
descriptor
.setStorageMode(if cfg!(all(target_arch = "aarch64", target_vendor = "apple")) {
descriptor.setStorageMode(
if cfg!(all(target_arch = "aarch64", target_vendor = "apple")) {
MTLResourceStorageModeShared
} else {
MTLResourceStorageModeManaged
});
},
);
descriptor.setUsage(MTLTextureUsageShaderRead);

View file

@ -1,6 +1,10 @@
use crate::error::{FilterChainError, Result};
use crate::select_optimal_pixel_format;
use icrate::Metal::{MTLBlitCommandEncoder, MTLCommandBuffer, MTLCommandEncoder, MTLDevice, MTLPixelFormat, MTLStorageModePrivate, MTLTexture, MTLTextureDescriptor, MTLTextureUsageRenderTarget, MTLTextureUsageShaderRead, MTLTextureUsageShaderWrite};
use icrate::Metal::{
MTLBlitCommandEncoder, MTLCommandBuffer, MTLCommandEncoder, MTLDevice, MTLPixelFormat,
MTLStorageModePrivate, MTLTexture, MTLTextureDescriptor, MTLTextureUsageRenderTarget,
MTLTextureUsageShaderRead, MTLTextureUsageShaderWrite,
};
use librashader_common::{FilterMode, ImageFormat, Size, WrapMode};
use librashader_presets::Scale2D;
use librashader_runtime::scaling::{MipmapSize, ScaleFramebuffer, ViewportSize};

View file

@ -17,7 +17,7 @@ use gpu_allocator::vulkan::Allocator;
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::SPIRV;
use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::front::{SpirvCompilation};
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader;
@ -218,13 +218,12 @@ fn compile_passes(
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache {
SPIRV::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
SPIRV::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
SPIRV::compile_preset_passes::<SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -1,7 +1,7 @@
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::WGSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::front::{SpirvCompilation};
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader;
@ -44,7 +44,7 @@ fn compile_passes(
textures: &[TextureConfig],
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) =
WGSL::compile_preset_passes::<Glslang, SpirvCompilation, Naga, FilterChainError>(
WGSL::compile_preset_passes::<SpirvCompilation, Naga, FilterChainError>(
shaders, &textures,
)?;
Ok((passes, semantics))

View file

@ -100,11 +100,10 @@ pub mod preprocess {
/// use librashader::presets::ShaderPreset;
/// use librashader::reflect::{CompileReflectShader, FromCompilation, CompilePresetTarget, ShaderPassArtifact};
/// use librashader::reflect::targets::SPIRV;
/// use librashader::reflect::Glslang;
/// use librashader::reflect::semantics::ShaderSemantics;
/// use librashader_reflect::front::{ShaderInputCompiler, SpirvCompilation};
/// use librashader_reflect::reflect::cross::SpirvCross;
/// type Artifact = impl CompileReflectShader<SPIRV, Glslang, SpirvCross>;
/// type Artifact = impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross>;
/// type ShaderPassMeta = ShaderPassArtifact<Artifact>;
///
/// // Compile single shader
@ -112,7 +111,7 @@ pub mod preprocess {
/// source: &ShaderSource,
/// ) -> Result<Artifact, Box<dyn Error>>
/// {
/// let compilation = Glslang::compile(&source)?;
/// let compilation = SpirvCompilation::compile(&source)?;
/// let spirv = SPIRV::from_compilation(compilation)?;
/// Ok(spirv)
/// }
@ -120,7 +119,7 @@ pub mod preprocess {
/// // Compile preset
/// pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>>
/// {
/// let (passes, semantics) = SPIRV::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, Box<dyn Error>>(
/// let (passes, semantics) = SPIRV::compile_preset_passes::<SpirvCompilation, SpirvCross, Box<dyn Error>>(
/// preset.shaders, &preset.textures)?;
/// Ok((passes, semantics))
/// }
@ -131,7 +130,7 @@ pub mod preprocess {
/// [naga](https://docs.rs/naga/latest/naga/index.html), a pure-Rust shader compiler, when it has
/// matured enough to support [the features librashader needs](https://github.com/gfx-rs/naga/issues/1012).
///
/// In the meanwhile, the only supported input compiler is [Glslang](crate::reflect::Glslang),
/// In the meanwhile, the only supported input compiler is [SpirvCompilation](crate::reflect::SpirvCompilation),
/// which does compilation of GLSL to SPIR-V via [glslang](https://github.com/KhronosGroup/glslang/).
pub mod reflect {
/// Supported shader compiler targets.
@ -152,7 +151,7 @@ pub mod reflect {
FromCompilation, ShaderCompilerOutput,
};
pub use librashader_reflect::front::{Glslang, ShaderReflectObject, SpirvCompilation};
pub use librashader_reflect::front::{ShaderReflectObject, SpirvCompilation};
/// Reflection via SPIRV-Cross.
#[cfg(feature = "reflect-cross")]