From e1f62fc9849e0a0f5ab6333ec594a1e9a0c90c4d Mon Sep 17 00:00:00 2001 From: chyyran Date: Sat, 10 Feb 2024 21:17:58 -0500 Subject: [PATCH] reflect: remove ShaderOutputCompiler and just delegate to FromCompilation --- librashader-reflect/src/back/cross.rs | 15 ++--- librashader-reflect/src/back/dxil.rs | 2 +- librashader-reflect/src/back/mod.rs | 26 +++++--- librashader-reflect/src/back/spirv.rs | 2 +- .../src/back/{wgsl/mod.rs => wgsl.rs} | 5 +- librashader-reflect/src/error.rs | 2 +- librashader-reflect/src/front/mod.rs | 2 +- librashader-reflect/src/lib.rs | 3 +- librashader-reflect/src/reflect/cross.rs | 62 ++----------------- librashader-reflect/src/reflect/mod.rs | 13 ---- librashader-reflect/src/reflect/naga/mod.rs | 37 ++++------- librashader-runtime-d3d11/src/filter_chain.rs | 6 +- librashader-runtime-d3d12/src/filter_chain.rs | 12 ++-- .../src/filter_chain/filter_impl.rs | 6 +- librashader-runtime-vk/src/filter_chain.rs | 16 ++--- 15 files changed, 68 insertions(+), 141 deletions(-) rename librashader-reflect/src/back/{wgsl/mod.rs => wgsl.rs} (97%) diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index cac4567..94cb82a 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -2,8 +2,8 @@ use crate::back::targets::{GLSL, HLSL}; use crate::back::{CompileShader, CompilerBackend, FromCompilation}; use crate::error::ShaderReflectError; use crate::front::SpirvCompilation; -use crate::reflect::cross::{CompiledProgram, SpirvCross}; -use crate::reflect::{ReflectShader, ShaderOutputCompiler}; +use crate::reflect::cross::{CompiledProgram, GlslReflect, HlslReflect, SpirvCross}; +use crate::reflect::ReflectShader; /// The GLSL version to target. pub use spirv_cross::glsl::Version as GlslVersion; @@ -19,7 +19,7 @@ pub struct CrossGlslContext { pub artifact: CompiledProgram, } -impl FromCompilation> for GLSL { +impl FromCompilation for GLSL { type Target = GLSL; type Options = GlslVersion; type Context = CrossGlslContext; @@ -29,8 +29,9 @@ impl FromCompilation> for GLSL { fn from_compilation( compile: SpirvCompilation, ) -> Result, ShaderReflectError> { - let backend = SpirvCross::::create_reflection(compile)?; - Ok(CompilerBackend { backend }) + Ok(CompilerBackend { + backend: GlslReflect::try_from(&compile)?, + }) } } @@ -40,7 +41,7 @@ pub struct CrossHlslContext { pub artifact: CompiledProgram, } -impl FromCompilation> for HLSL { +impl FromCompilation for HLSL { type Target = HLSL; type Options = Option; type Context = CrossHlslContext; @@ -51,7 +52,7 @@ impl FromCompilation> for HLSL { compile: SpirvCompilation, ) -> Result, ShaderReflectError> { Ok(CompilerBackend { - backend: SpirvCross::::create_reflection(compile)?, + backend: HlslReflect::try_from(&compile)?, }) } } diff --git a/librashader-reflect/src/back/dxil.rs b/librashader-reflect/src/back/dxil.rs index ffb16ce..cf00a78 100644 --- a/librashader-reflect/src/back/dxil.rs +++ b/librashader-reflect/src/back/dxil.rs @@ -16,7 +16,7 @@ impl OutputTarget for DXIL { type Output = DxilObject; } -impl FromCompilation> for DXIL { +impl FromCompilation for DXIL { type Target = DXIL; type Options = Option; type Context = (); diff --git a/librashader-reflect/src/back/mod.rs b/librashader-reflect/src/back/mod.rs index 4dde377..bb685b6 100644 --- a/librashader-reflect/src/back/mod.rs +++ b/librashader-reflect/src/back/mod.rs @@ -42,26 +42,26 @@ pub trait CompileShader { /// /// This trait is automatically implemented for reflected outputs that have [`FromCompilation`](crate::back::FromCompilation) implement /// for a given target that also implement [`CompileShader`](crate::back::CompileShader) for that target. -pub trait CompileReflectShader: +pub trait CompileReflectShader: CompileShader< T, - Options = >::Options, - Context = >::Context, + Options = >::Options, + Context = >::Context, > + ReflectShader where - T: FromCompilation, + T: FromCompilation, { } -impl CompileReflectShader for O +impl CompileReflectShader for O where T: OutputTarget, - T: FromCompilation, + T: FromCompilation, O: ReflectShader, O: CompileShader< T, - Options = >::Options, - Context = >::Context, + Options = >::Options, + Context = >::Context, >, { } @@ -82,8 +82,14 @@ where } } -/// A trait for reflectable compilations that can be transformed into an object ready for reflection or compilation. -pub trait FromCompilation { +/// A trait for reflectable compilations that can be transformed +/// into an object ready for reflection or compilation. +/// +/// `T` is the compiled reflectable form of the shader. +/// `S` is the semantics under which the shader is reflected. +/// +/// librashader currently supports two semantics, [`SpirvCross`](crate::reflect::cross::SpirvCross) +pub trait FromCompilation { /// The target that the transformed object is expected to compile for. type Target: OutputTarget; /// Options provided to the compiler. diff --git a/librashader-reflect/src/back/spirv.rs b/librashader-reflect/src/back/spirv.rs index d720267..ef86de1 100644 --- a/librashader-reflect/src/back/spirv.rs +++ b/librashader-reflect/src/back/spirv.rs @@ -13,7 +13,7 @@ pub(crate) struct WriteSpirV { pub(crate) fragment: Vec, } -impl FromCompilation> for SPIRV { +impl FromCompilation for SPIRV { type Target = SPIRV; type Options = Option<()>; type Context = (); diff --git a/librashader-reflect/src/back/wgsl/mod.rs b/librashader-reflect/src/back/wgsl.rs similarity index 97% rename from librashader-reflect/src/back/wgsl/mod.rs rename to librashader-reflect/src/back/wgsl.rs index 6c9db9b..bb401ac 100644 --- a/librashader-reflect/src/back/wgsl/mod.rs +++ b/librashader-reflect/src/back/wgsl.rs @@ -3,11 +3,10 @@ use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompile use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::front::SpirvCompilation; use crate::reflect::naga::{Naga, NagaReflect}; -use crate::reflect::{ReflectShader, ShaderOutputCompiler}; +use crate::reflect::ReflectShader; use naga::back::wgsl::WriterFlags; use naga::valid::{Capabilities, ValidationFlags}; use naga::{AddressSpace, Module}; -use rspirv::binary::Assemble; /// The context for a WGSL compilation via Naga pub struct NagaWgslContext { @@ -33,7 +32,7 @@ impl FromCompilation for WGSL { compile: SpirvCompilation, ) -> Result, ShaderReflectError> { Ok(CompilerBackend { - backend: Naga::create_reflection(compile)?, + backend: NagaReflect::create_reflection(&compile)?, }) } } diff --git a/librashader-reflect/src/error.rs b/librashader-reflect/src/error.rs index bc854cb..bb79cd0 100644 --- a/librashader-reflect/src/error.rs +++ b/librashader-reflect/src/error.rs @@ -111,7 +111,7 @@ pub enum ShaderReflectError { #[error("the binding is already in use")] BindingInUse(u32), /// Error when transpiling from naga - #[cfg(feature = "wgsl")] + #[cfg(feature = "naga")] #[error("naga-spv")] NagaInputError(#[from] naga::front::spv::Error), } diff --git a/librashader-reflect/src/front/mod.rs b/librashader-reflect/src/front/mod.rs index b95090f..f816187 100644 --- a/librashader-reflect/src/front/mod.rs +++ b/librashader-reflect/src/front/mod.rs @@ -6,7 +6,7 @@ mod glslang; pub trait ShaderReflectObject: Sized {} -pub use glslang::Glslang; +pub use crate::front::glslang::Glslang; /// Trait for types that can compile shader sources into a compilation unit. pub trait ShaderInputCompiler: Sized { diff --git a/librashader-reflect/src/lib.rs b/librashader-reflect/src/lib.rs index 5d48e73..578d448 100644 --- a/librashader-reflect/src/lib.rs +++ b/librashader-reflect/src/lib.rs @@ -14,9 +14,10 @@ //! use librashader_reflect::back::{CompileReflectShader, FromCompilation}; //! use librashader_reflect::back::targets::SPIRV; //! use librashader_reflect::front::{Glslang, ShaderInputCompiler, SpirvCompilation}; +//! use librashader_reflect::reflect::cross::SpirvCross; //! use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; //! use librashader_reflect::reflect::semantics::ShaderSemantics; -//! type Artifact = impl CompileReflectShader; +//! type Artifact = impl CompileReflectShader; //! type ShaderPassMeta = ShaderPassArtifact; //! //! // Compile single shader diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 2d78d2f..85637de 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -6,75 +6,23 @@ use crate::reflect::semantics::{ UniformMemberBlock, UniqueSemanticMap, UniqueSemantics, ValidateTypeSemantics, VariableMeta, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, }; -use crate::reflect::{align_uniform_size, ReflectShader, ShaderOutputCompiler}; -use std::borrow::Borrow; -use std::marker::PhantomData; +use crate::reflect::{align_uniform_size, ReflectShader}; use std::ops::Deref; use spirv_cross::spirv::{Ast, Decoration, Module, Resource, ShaderResources, Type}; use spirv_cross::{glsl, hlsl, ErrorCode}; use crate::back::cross::{CrossGlslContext, CrossHlslContext, HlslShaderModel}; -use crate::back::targets::{OutputTarget, DXIL, GLSL, HLSL, SPIRV}; +use crate::back::targets::{GLSL, HLSL}; use crate::back::{CompileShader, ShaderCompilerOutput}; use crate::reflect::helper::{SemanticErrorBlame, TextureData, UboData}; -trait SpirvCrossTarget { - type CrossTarget: spirv_cross::spirv::Target; -} - -impl SpirvCrossTarget for HLSL { - type CrossTarget = spirv_cross::hlsl::Target; -} - -impl SpirvCrossTarget for SPIRV { - type CrossTarget = spirv_cross::glsl::Target; -} - -impl SpirvCrossTarget for GLSL { - type CrossTarget = spirv_cross::glsl::Target; -} - -impl SpirvCrossTarget for DXIL { - type CrossTarget = spirv_cross::glsl::Target; -} - -#[allow(private_bounds)] -pub struct SpirvCross(PhantomData); +/// Reflect shaders under SPIRV-Cross semantics. +pub struct SpirvCross; +pub(crate) type HlslReflect = CrossReflect; pub(crate) type GlslReflect = CrossReflect; -impl - ShaderOutputCompiler for SpirvCross -// where Spv: spirv_cross::spirv::Target, -// Ast: spirv_cross::spirv::Compile, -// Ast: spirv_cross::spirv::Parse, -where - CrossReflect<::CrossTarget>: - CompileShader, - ::CrossTarget: spirv_cross::spirv::Target, - Ast<::CrossTarget>: - spirv_cross::spirv::Compile<::CrossTarget>, - Ast<::CrossTarget>: - spirv_cross::spirv::Parse<::CrossTarget>, -{ - fn create_reflection( - compiled: SpirvCompilation, - ) -> Result< - impl ReflectShader + CompileShader, - ShaderReflectError, - > { - let compiled = compiled.borrow(); - let vertex_module = Module::from_words(&compiled.vertex); - let fragment_module = Module::from_words(&compiled.fragment); - - let vertex = Ast::::parse(&vertex_module)?; - let fragment = Ast::::parse(&fragment_module)?; - - Ok(CrossReflect { vertex, fragment }) - } -} - // This is "probably" OK. unsafe impl Send for CrossReflect where diff --git a/librashader-reflect/src/reflect/mod.rs b/librashader-reflect/src/reflect/mod.rs index fed049a..eef039f 100644 --- a/librashader-reflect/src/reflect/mod.rs +++ b/librashader-reflect/src/reflect/mod.rs @@ -15,16 +15,6 @@ mod helper; #[cfg(feature = "naga")] pub mod naga; -pub trait ShaderOutputCompiler { - /// Create the reflection object - fn create_reflection( - compiled: O, - ) -> Result< - impl ReflectShader + CompileShader, - ShaderReflectError, - >; -} - /// A trait for compilation outputs that can provide reflection information. pub trait ReflectShader { /// Reflect the shader as the given pass within the shader preset, against the provided @@ -36,9 +26,6 @@ pub trait ReflectShader { ) -> Result; } -use crate::back::targets::OutputTarget; -use crate::back::CompileShader; -use crate::front::ShaderReflectObject; pub use semantics::ShaderReflection; #[inline(always)] diff --git a/librashader-reflect/src/reflect/naga/mod.rs b/librashader-reflect/src/reflect/naga/mod.rs index a35d983..928837d 100644 --- a/librashader-reflect/src/reflect/naga/mod.rs +++ b/librashader-reflect/src/reflect/naga/mod.rs @@ -1,10 +1,8 @@ mod lower_samplers; use crate::error::{SemanticsErrorKind, ShaderReflectError}; -use std::borrow::Borrow; -use crate::back::targets::OutputTarget; -use crate::back::CompileShader; +use crate::error; use crate::front::SpirvCompilation; use naga::{ AddressSpace, Binding, GlobalVariable, Handle, ImageClass, Module, ResourceBinding, Scalar, @@ -20,24 +18,24 @@ use crate::reflect::semantics::{ UniqueSemanticMap, UniqueSemantics, ValidateTypeSemantics, VariableMeta, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, }; -use crate::reflect::{align_uniform_size, ReflectShader, ShaderOutputCompiler, ShaderReflection}; +use crate::reflect::{align_uniform_size, ReflectShader, ShaderReflection}; -/// Represents the naga output compiler. +/// Reflect under Naga semantics /// /// The Naga reflector will lower combined image samplers to split, /// with the same bind point on descriptor group 1. pub struct Naga; -impl ShaderOutputCompiler for Naga -where - NagaReflect: CompileShader, -{ - fn create_reflection( - compile: SpirvCompilation, - ) -> Result< - impl ReflectShader + CompileShader, - ShaderReflectError, - > { +#[derive(Debug)] +pub(crate) struct NagaReflect { + pub(crate) vertex: Module, + pub(crate) fragment: Module, +} + +impl NagaReflect { + pub(crate) fn create_reflection( + compile: &SpirvCompilation, + ) -> Result { fn lower_fragment_shader(words: &[u32]) -> Vec { let mut loader = rspirv::dr::Loader::new(); rspirv::binary::parse_words(words, &mut loader).unwrap(); @@ -54,8 +52,6 @@ where module.assemble() } - let compile = compile.borrow(); - let options = naga::front::spv::Options { adjust_coordinate_space: true, strict_capabilities: false, @@ -71,13 +67,6 @@ where Ok(NagaReflect { vertex, fragment }) } } - -#[derive(Debug)] -pub(crate) struct NagaReflect { - pub(crate) vertex: Module, - pub(crate) fragment: Module, -} - impl ValidateTypeSemantics<&TypeInner> for UniqueSemantics { fn validate_type(&self, ty: &&TypeInner) -> Option { let (TypeInner::Vector { .. } | TypeInner::Scalar { .. } | TypeInner::Matrix { .. }) = *ty diff --git a/librashader-runtime-d3d11/src/filter_chain.rs b/librashader-runtime-d3d11/src/filter_chain.rs index 6a59a6f..794bd8e 100644 --- a/librashader-runtime-d3d11/src/filter_chain.rs +++ b/librashader-runtime-d3d11/src/filter_chain.rs @@ -75,7 +75,7 @@ pub(crate) struct FilterCommon { } type ShaderPassMeta = - ShaderPassArtifact> + Send>; + ShaderPassArtifact + Send>; fn compile_passes( shaders: Vec, textures: &[TextureConfig], @@ -85,11 +85,11 @@ fn compile_passes( HLSL::compile_preset_passes::< Glslang, CachedCompilation, - SpirvCross, + SpirvCross, FilterChainError, >(shaders, &textures)? } else { - HLSL::compile_preset_passes::, FilterChainError>( + HLSL::compile_preset_passes::( shaders, &textures, )? }; diff --git a/librashader-runtime-d3d12/src/filter_chain.rs b/librashader-runtime-d3d12/src/filter_chain.rs index 4e1dd6c..6b11602 100644 --- a/librashader-runtime-d3d12/src/filter_chain.rs +++ b/librashader-runtime-d3d12/src/filter_chain.rs @@ -146,7 +146,7 @@ impl Drop for FrameResiduals { } type DxilShaderPassMeta = - ShaderPassArtifact> + Send>; + ShaderPassArtifact + Send>; fn compile_passes_dxil( shaders: Vec, textures: &[TextureConfig], @@ -156,11 +156,11 @@ fn compile_passes_dxil( DXIL::compile_preset_passes::< Glslang, CachedCompilation, - SpirvCross, + SpirvCross, FilterChainError, >(shaders, &textures)? } else { - DXIL::compile_preset_passes::, FilterChainError>( + DXIL::compile_preset_passes::( shaders, &textures, )? }; @@ -168,7 +168,7 @@ fn compile_passes_dxil( Ok((passes, semantics)) } type HlslShaderPassMeta = - ShaderPassArtifact> + Send>; + ShaderPassArtifact + Send>; fn compile_passes_hlsl( shaders: Vec, textures: &[TextureConfig], @@ -178,11 +178,11 @@ fn compile_passes_hlsl( HLSL::compile_preset_passes::< Glslang, CachedCompilation, - SpirvCross, + SpirvCross, FilterChainError, >(shaders, &textures)? } else { - HLSL::compile_preset_passes::, FilterChainError>( + HLSL::compile_preset_passes::( shaders, &textures, )? }; diff --git a/librashader-runtime-gl/src/filter_chain/filter_impl.rs b/librashader-runtime-gl/src/filter_chain/filter_impl.rs index 4e45912..72a1c42 100644 --- a/librashader-runtime-gl/src/filter_chain/filter_impl.rs +++ b/librashader-runtime-gl/src/filter_chain/filter_impl.rs @@ -99,7 +99,7 @@ impl FilterChainImpl { } type ShaderPassMeta = - ShaderPassArtifact>>; + ShaderPassArtifact>; fn compile_passes( shaders: Vec, textures: &[TextureConfig], @@ -109,11 +109,11 @@ fn compile_passes( GLSL::compile_preset_passes::< Glslang, CachedCompilation, - SpirvCross, + SpirvCross, FilterChainError, >(shaders, &textures)? } else { - GLSL::compile_preset_passes::, FilterChainError>( + GLSL::compile_preset_passes::( shaders, &textures, )? }; diff --git a/librashader-runtime-vk/src/filter_chain.rs b/librashader-runtime-vk/src/filter_chain.rs index 12a029a..94a46bd 100644 --- a/librashader-runtime-vk/src/filter_chain.rs +++ b/librashader-runtime-vk/src/filter_chain.rs @@ -208,9 +208,8 @@ impl Drop for FrameResiduals { } } -type ShaderPassMeta = ShaderPassArtifact< - impl CompileReflectShader> + Send, ->; +type ShaderPassMeta = + ShaderPassArtifact + Send>; fn compile_passes( shaders: Vec, textures: &[TextureConfig], @@ -220,16 +219,13 @@ fn compile_passes( SPIRV::compile_preset_passes::< Glslang, CachedCompilation, - SpirvCross, + SpirvCross, FilterChainError, >(shaders, &textures)? } else { - SPIRV::compile_preset_passes::< - Glslang, - SpirvCompilation, - SpirvCross, - FilterChainError, - >(shaders, &textures)? + SPIRV::compile_preset_passes::( + shaders, &textures, + )? }; Ok((passes, semantics))