From e0a5c90103c10eb8c899d3ac7494d8f1716a67c6 Mon Sep 17 00:00:00 2001 From: chyyran Date: Sun, 15 Sep 2024 01:36:14 -0400 Subject: [PATCH] reflect: implement stable FromCompilation --- librashader-reflect/Cargo.toml | 1 + librashader-reflect/src/back/dxil.rs | 23 +++++++++++++++ librashader-reflect/src/back/glsl.rs | 17 +++++++++++ librashader-reflect/src/back/hlsl.rs | 21 ++++++++++++-- librashader-reflect/src/back/msl.rs | 35 ++++++++++++++++++++++- librashader-reflect/src/back/spirv.rs | 41 +++++++++++++++++++++++++++ librashader-reflect/src/back/wgsl.rs | 17 +++++++++++ librashader-reflect/src/lib.rs | 4 +-- librashader-runtime-d3d11/src/lib.rs | 2 +- 9 files changed, 155 insertions(+), 6 deletions(-) diff --git a/librashader-reflect/Cargo.toml b/librashader-reflect/Cargo.toml index 324e8b0..4fe4681 100644 --- a/librashader-reflect/Cargo.toml +++ b/librashader-reflect/Cargo.toml @@ -46,5 +46,6 @@ cross = [ "dep:spirv-cross2", "spirv-cross2/glsl", "spirv-cross2/hlsl", "spirv-c naga = [ "dep:rspirv", "dep:spirv", "naga/spv-in", "naga/spv-out", "naga/wgsl-out", "naga/msl-out" ] serialize = [ "dep:serde" ] msl = [ "spirv-cross2/msl", "naga/msl-out" ] +stable = [] unstable-naga-in = ["naga/glsl-in"] diff --git a/librashader-reflect/src/back/dxil.rs b/librashader-reflect/src/back/dxil.rs index b35ea46..e93b089 100644 --- a/librashader-reflect/src/back/dxil.rs +++ b/librashader-reflect/src/back/dxil.rs @@ -17,6 +17,7 @@ impl OutputTarget for DXIL { type Output = DxilObject; } +#[cfg(not(feature = "stable"))] impl FromCompilation for DXIL { type Target = DXIL; type Options = Option; @@ -38,6 +39,28 @@ impl FromCompilation for DXIL { } } +#[cfg(feature = "stable")] +impl FromCompilation for DXIL { + type Target = DXIL; + type Options = Option; + type Context = (); + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + let reflect = GlslReflect::try_from(&compile)?; + Ok(CompilerBackend { + // we can just reuse WriteSpirV as the backend. + backend: Box::new(WriteSpirV { + reflect, + vertex: compile.vertex, + fragment: compile.fragment, + }), + }) + } +} + impl CompileShader for WriteSpirV { type Options = Option; type Context = (); diff --git a/librashader-reflect/src/back/glsl.rs b/librashader-reflect/src/back/glsl.rs index 6351c31..6148551 100644 --- a/librashader-reflect/src/back/glsl.rs +++ b/librashader-reflect/src/back/glsl.rs @@ -17,6 +17,7 @@ pub struct CrossGlslContext { pub artifact: CompiledProgram, } +#[cfg(not(feature = "stable"))] impl FromCompilation for GLSL { type Target = GLSL; type Options = GlslVersion; @@ -31,3 +32,19 @@ impl FromCompilation for GLSL { }) } } + +#[cfg(feature = "stable")] +impl FromCompilation for GLSL { + type Target = GLSL; + type Options = GlslVersion; + type Context = CrossGlslContext; + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + Ok(CompilerBackend { + backend: Box::new(GlslReflect::try_from(&compile)?), + }) + } +} diff --git a/librashader-reflect/src/back/hlsl.rs b/librashader-reflect/src/back/hlsl.rs index 6417626..f6026cc 100644 --- a/librashader-reflect/src/back/hlsl.rs +++ b/librashader-reflect/src/back/hlsl.rs @@ -46,7 +46,7 @@ impl HlslBufferAssignments { { return true; } - return false; + false } // Check if the mangled name matches. @@ -91,7 +91,7 @@ impl HlslBufferAssignments { return true; } - return false; + false } } @@ -103,6 +103,7 @@ pub struct CrossHlslContext { pub fragment_buffers: HlslBufferAssignments, } +#[cfg(not(feature = "stable"))] impl FromCompilation for HLSL { type Target = HLSL; type Options = Option; @@ -118,6 +119,22 @@ impl FromCompilation for HLSL { } } +#[cfg(feature = "stable")] +impl FromCompilation for HLSL { + type Target = HLSL; + type Options = Option; + type Context = CrossHlslContext; + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + Ok(CompilerBackend { + backend: Box::new(HlslReflect::try_from(&compile)?), + }) + } +} + #[cfg(test)] mod test { use crate::back::hlsl::HlslBufferAssignments; diff --git a/librashader-reflect/src/back/msl.rs b/librashader-reflect/src/back/msl.rs index fb5f983..a74b2d4 100644 --- a/librashader-reflect/src/back/msl.rs +++ b/librashader-reflect/src/back/msl.rs @@ -5,7 +5,6 @@ use crate::front::SpirvCompilation; use crate::reflect::cross::msl::MslReflect; use crate::reflect::cross::{CompiledProgram, SpirvCross}; use crate::reflect::naga::{Naga, NagaReflect}; -use crate::reflect::ReflectShader; use naga::back::msl::TranslationInfo; use naga::Module; @@ -25,6 +24,7 @@ pub struct CrossMslContext { pub artifact: CompiledProgram, } +#[cfg(not(feature = "stable"))] impl FromCompilation for MSL { type Target = MSL; type Options = Option; @@ -40,6 +40,22 @@ impl FromCompilation for MSL { } } +#[cfg(feature = "stable")] +impl FromCompilation for MSL { + type Target = MSL; + type Options = Option; + type Context = CrossMslContext; + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + Ok(CompilerBackend { + backend: Box::new(MslReflect::try_from(&compile)?), + }) + } +} + /// The naga module for a shader after compilation pub struct NagaMslModule { pub translation_info: TranslationInfo, @@ -52,6 +68,7 @@ pub struct NagaMslContext { pub next_free_binding: u32, } +#[cfg(not(feature = "stable"))] impl FromCompilation for MSL { type Target = MSL; type Options = Option; @@ -66,3 +83,19 @@ impl FromCompilation for MSL { }) } } + +#[cfg(feature = "stable")] +impl FromCompilation for MSL { + type Target = MSL; + type Options = Option; + type Context = NagaMslContext; + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + Ok(CompilerBackend { + backend: Box::new(NagaReflect::try_from(&compile)?), + }) + } +} diff --git a/librashader-reflect/src/back/spirv.rs b/librashader-reflect/src/back/spirv.rs index 3628886..d5f0f7c 100644 --- a/librashader-reflect/src/back/spirv.rs +++ b/librashader-reflect/src/back/spirv.rs @@ -18,6 +18,7 @@ pub(crate) struct WriteSpirV { pub(crate) fragment: Vec, } +#[cfg(not(feature = "stable"))] impl FromCompilation for SPIRV { type Target = SPIRV; type Options = Option<()>; @@ -40,6 +41,29 @@ impl FromCompilation for SPIRV { } } +#[cfg(feature = "stable")] +impl FromCompilation for SPIRV { + type Target = SPIRV; + type Options = Option<()>; + type Context = (); + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + let reflect = GlslReflect::try_from(&compile)?; + let vertex = compile.vertex; + let fragment = compile.fragment; + Ok(CompilerBackend { + backend: Box::new(WriteSpirV { + reflect, + vertex, + fragment, + }), + }) + } +} + impl ReflectShader for WriteSpirV { fn reflect( &mut self, @@ -83,6 +107,7 @@ pub struct NagaSpirvContext { pub vertex: Module, } +#[cfg(not(feature = "stable"))] impl FromCompilation for SPIRV { type Target = SPIRV; type Options = NagaSpirvOptions; @@ -98,6 +123,22 @@ impl FromCompilation for SPIRV { } } +#[cfg(feature = "stable")] +impl FromCompilation for SPIRV { + type Target = SPIRV; + type Options = NagaSpirvOptions; + type Context = NagaSpirvContext; + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + Ok(CompilerBackend { + backend: Box::new(NagaReflect::try_from(&compile)?), + }) + } +} + pub struct NagaSpirvOptions { pub lowering: NagaLoweringOptions, pub version: (u8, u8), diff --git a/librashader-reflect/src/back/wgsl.rs b/librashader-reflect/src/back/wgsl.rs index 286f13f..92bdda7 100644 --- a/librashader-reflect/src/back/wgsl.rs +++ b/librashader-reflect/src/back/wgsl.rs @@ -11,6 +11,7 @@ pub struct NagaWgslContext { pub vertex: Module, } +#[cfg(not(feature = "stable"))] impl FromCompilation for WGSL { type Target = WGSL; type Options = NagaLoweringOptions; @@ -26,6 +27,22 @@ impl FromCompilation for WGSL { } } +#[cfg(feature = "stable")] +impl FromCompilation for WGSL { + type Target = WGSL; + type Options = NagaLoweringOptions; + type Context = NagaWgslContext; + type Output = Box + Send>; + + fn from_compilation( + compile: SpirvCompilation, + ) -> Result, ShaderReflectError> { + Ok(CompilerBackend { + backend: Box::new(NagaReflect::try_from(&compile)?), + }) + } +} + #[cfg(test)] mod test { use crate::back::targets::WGSL; diff --git a/librashader-reflect/src/lib.rs b/librashader-reflect/src/lib.rs index 65a5224..5697fe6 100644 --- a/librashader-reflect/src/lib.rs +++ b/librashader-reflect/src/lib.rs @@ -43,9 +43,9 @@ //! librashader-reflect is designed to be compiler-agnostic. [naga](https://docs.rs/naga/latest/naga/index.html), //! a pure-Rust shader compiler, as well as SPIRV-Cross via [SpirvCompilation](crate::front::SpirvCompilation) //! is supported. -#![feature(impl_trait_in_assoc_type)] +#![cfg_attr(not(feature="stable"), feature(impl_trait_in_assoc_type))] #![allow(stable_features)] -#![feature(c_str_literals)] +#![cfg_attr(not(feature="stable"), feature(c_str_literals))] /// Shader codegen backends. pub mod back; /// Error types. diff --git a/librashader-runtime-d3d11/src/lib.rs b/librashader-runtime-d3d11/src/lib.rs index 0452333..0845d42 100644 --- a/librashader-runtime-d3d11/src/lib.rs +++ b/librashader-runtime-d3d11/src/lib.rs @@ -5,7 +5,7 @@ //! This crate should not be used directly. //! See [`librashader::runtime::d3d11`](https://docs.rs/librashader/latest/librashader/runtime/d3d11/index.html) instead. -#![feature(type_alias_impl_trait)] +#![cfg_attr(not(feature = "stable"), feature(type_alias_impl_trait))] mod draw_quad; mod filter_chain;