doc: document reflect module

This commit is contained in:
chyyran 2023-01-19 18:59:39 -05:00
parent ef8b72b220
commit 08ca2963c5
2 changed files with 90 additions and 1 deletions

View file

@ -1,4 +1,50 @@
//! Shader reflection and cross-compilation for librashader. //! Shader reflection and cross-compilation for librashader.
//!
//! ## Usage
//!
//! librashader-reflect requires the `type_alias_impl_trait` nightly feature. You should choose your
//! target shading language, and a compilation type.
//!
//! ```rust
//! #![feature(type_alias_impl_trait)]
//!
//! use std::error::Error;
//! use librashader_preprocess::ShaderSource;
//! use librashader_presets::ShaderPreset;
//! use librashader_reflect::back::{CompileReflectShader, FromCompilation};
//! use librashader_reflect::back::targets::SPIRV;
//! use librashader_reflect::front::GlslangCompilation;
//! use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
//! use librashader_reflect::reflect::semantics::ShaderSemantics;
//! type Artifact = impl CompileReflectShader<SPIRV, GlslangCompilation>;
//! type ShaderPassMeta = ShaderPassArtifact<Artifact>;
//!
//! // Compile single shader
//! pub fn compile_spirv(
//! source: &ShaderSource,
//! ) -> Result<Artifact, Box<dyn Error>>
//! {
//! let compilation = GlslangCompilation::compile(&source)?;
//! let spirv = SPIRV::from_compilation(artifact)?;
//! Ok(spirv)
//! }
//!
//! // Compile preset
//! pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>>
//! {
//! let (passes, semantics) = SPIRV::compile_preset_passes::<GlslangCompilation, Box<dyn Error>>(
//! preset.shaders, &preset.textures)?;
//! Ok((passes, semantics))
//! }
//! ```
//!
//! ## What's with all the traits?
//! librashader-reflect is designed to be compiler-agnostic. In the future, we will allow usage of
//! [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 compilation type is [GlslangCompilation](crate::front::GlslangCompilation),
//! which does transpilation via [shaderc](https://github.com/google/shaderc) and [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross).
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
/// Shader codegen backends. /// Shader codegen backends.

View file

@ -75,7 +75,50 @@ pub mod preprocess {
#[cfg(feature = "reflect")] #[cfg(feature = "reflect")]
#[doc(cfg(reflect))] #[doc(cfg(reflect))]
/// Shader compilation and reflection. /// Shader reflection and cross-compilation.
///
/// The `type_alias_impl_trait` nightly feature is required. You should choose your
/// target shading language, and a compilation type.
///
/// ```rust
/// #![feature(type_alias_impl_trait)]
///
/// use std::error::Error;
/// use librashader::preprocess::ShaderSource;
/// use librashader::presets::ShaderPreset;
/// use librashader::reflect::{CompileReflectShader, FromCompilation, CompilePresetTarget, ShaderPassArtifact};
/// use librashader::reflect::targets::SPIRV;
/// use librashader::reflect::cross::GlslangCompilation;
/// use librashader::reflect::semantics::ShaderSemantics;
/// type Artifact = impl CompileReflectShader<SPIRV, GlslangCompilation>;
/// type ShaderPassMeta = ShaderPassArtifact<Artifact>;
///
/// // Compile single shader
/// pub fn compile_spirv(
/// source: &ShaderSource,
/// ) -> Result<Artifact, Box<dyn Error>>
/// {
/// let compilation = GlslangCompilation::compile(&source)?;
/// let spirv = SPIRV::from_compilation(artifact)?;
/// Ok(spirv)
/// }
///
/// // Compile preset
/// pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>>
/// {
/// let (passes, semantics) = SPIRV::compile_preset_passes::<GlslangCompilation, Box<dyn Error>>(
/// preset.shaders, &preset.textures)?;
/// Ok((passes, semantics))
/// }
/// ```
///
/// ## What's with all the traits?
/// librashader-reflect is designed to be compiler-agnostic. In the future, we will allow usage of
/// [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 compilation type is [GlslangCompilation](crate::front::GlslangCompilation),
/// which does transpilation via [shaderc](https://github.com/google/shaderc) and [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross).
pub mod reflect { pub mod reflect {
/// Supported shader compiler targets. /// Supported shader compiler targets.
pub mod targets { pub mod targets {