reflect: remove ShaderOutputCompiler and just delegate to FromCompilation

This commit is contained in:
chyyran 2024-02-10 21:17:58 -05:00 committed by Ronny Chan
parent b98d86a940
commit e1f62fc984
15 changed files with 68 additions and 141 deletions

View file

@ -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<spirv_cross::glsl::Target>,
}
impl FromCompilation<SpirvCompilation, SpirvCross<GLSL>> for GLSL {
impl FromCompilation<SpirvCompilation, SpirvCross> for GLSL {
type Target = GLSL;
type Options = GlslVersion;
type Context = CrossGlslContext;
@ -29,8 +29,9 @@ impl FromCompilation<SpirvCompilation, SpirvCross<GLSL>> for GLSL {
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
let backend = SpirvCross::<GLSL>::create_reflection(compile)?;
Ok(CompilerBackend { backend })
Ok(CompilerBackend {
backend: GlslReflect::try_from(&compile)?,
})
}
}
@ -40,7 +41,7 @@ pub struct CrossHlslContext {
pub artifact: CompiledProgram<spirv_cross::hlsl::Target>,
}
impl FromCompilation<SpirvCompilation, SpirvCross<HLSL>> for HLSL {
impl FromCompilation<SpirvCompilation, SpirvCross> for HLSL {
type Target = HLSL;
type Options = Option<HlslShaderModel>;
type Context = CrossHlslContext;
@ -51,7 +52,7 @@ impl FromCompilation<SpirvCompilation, SpirvCross<HLSL>> for HLSL {
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: SpirvCross::<HLSL>::create_reflection(compile)?,
backend: HlslReflect::try_from(&compile)?,
})
}
}

View file

@ -16,7 +16,7 @@ impl OutputTarget for DXIL {
type Output = DxilObject;
}
impl FromCompilation<SpirvCompilation, SpirvCross<DXIL>> for DXIL {
impl FromCompilation<SpirvCompilation, SpirvCross> for DXIL {
type Target = DXIL;
type Options = Option<ShaderModel>;
type Context = ();

View file

@ -42,26 +42,26 @@ pub trait CompileShader<T: OutputTarget> {
///
/// 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<T: OutputTarget, C, O>:
pub trait CompileReflectShader<T: OutputTarget, C, S>:
CompileShader<
T,
Options = <T as FromCompilation<C, O>>::Options,
Context = <T as FromCompilation<C, O>>::Context,
Options = <T as FromCompilation<C, S>>::Options,
Context = <T as FromCompilation<C, S>>::Context,
> + ReflectShader
where
T: FromCompilation<C, O>,
T: FromCompilation<C, S>,
{
}
impl<T, C, O, R> CompileReflectShader<T, C, R> for O
impl<T, C, O, S> CompileReflectShader<T, C, S> for O
where
T: OutputTarget,
T: FromCompilation<C, R>,
T: FromCompilation<C, S>,
O: ReflectShader,
O: CompileShader<
T,
Options = <T as FromCompilation<C, R>>::Options,
Context = <T as FromCompilation<C, R>>::Context,
Options = <T as FromCompilation<C, S>>::Options,
Context = <T as FromCompilation<C, S>>::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<T, R> {
/// 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<T, S> {
/// The target that the transformed object is expected to compile for.
type Target: OutputTarget;
/// Options provided to the compiler.

View file

@ -13,7 +13,7 @@ pub(crate) struct WriteSpirV {
pub(crate) fragment: Vec<u32>,
}
impl FromCompilation<SpirvCompilation, SpirvCross<SPIRV>> for SPIRV {
impl FromCompilation<SpirvCompilation, SpirvCross> for SPIRV {
type Target = SPIRV;
type Options = Option<()>;
type Context = ();

View file

@ -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<SpirvCompilation, Naga> for WGSL {
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Naga::create_reflection(compile)?,
backend: NagaReflect::create_reflection(&compile)?,
})
}
}

View file

@ -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),
}

View file

@ -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<O: ShaderReflectObject>: Sized {

View file

@ -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<SPIRV, SpirvCompilation>;
//! type Artifact = impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross>;
//! type ShaderPassMeta = ShaderPassArtifact<Artifact>;
//!
//! // Compile single shader

View file

@ -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<T: OutputTarget + SpirvCrossTarget>(PhantomData<T>);
/// Reflect shaders under SPIRV-Cross semantics.
pub struct SpirvCross;
pub(crate) type HlslReflect = CrossReflect<hlsl::Target>;
pub(crate) type GlslReflect = CrossReflect<glsl::Target>;
impl<T: OutputTarget + SpirvCrossTarget, Opt, Ctx>
ShaderOutputCompiler<SpirvCompilation, T, Opt, Ctx> for SpirvCross<T>
// where Spv: spirv_cross::spirv::Target,
// Ast<Spv>: spirv_cross::spirv::Compile<Spv>,
// Ast<Spv>: spirv_cross::spirv::Parse<Spv>,
where
CrossReflect<<T as SpirvCrossTarget>::CrossTarget>:
CompileShader<T, Options = Opt, Context = Ctx>,
<T as SpirvCrossTarget>::CrossTarget: spirv_cross::spirv::Target,
Ast<<T as SpirvCrossTarget>::CrossTarget>:
spirv_cross::spirv::Compile<<T as SpirvCrossTarget>::CrossTarget>,
Ast<<T as SpirvCrossTarget>::CrossTarget>:
spirv_cross::spirv::Parse<<T as SpirvCrossTarget>::CrossTarget>,
{
fn create_reflection(
compiled: SpirvCompilation,
) -> Result<
impl ReflectShader + CompileShader<T, Options = Opt, Context = Ctx>,
ShaderReflectError,
> {
let compiled = compiled.borrow();
let vertex_module = Module::from_words(&compiled.vertex);
let fragment_module = Module::from_words(&compiled.fragment);
let vertex = Ast::<T::CrossTarget>::parse(&vertex_module)?;
let fragment = Ast::<T::CrossTarget>::parse(&fragment_module)?;
Ok(CrossReflect { vertex, fragment })
}
}
// This is "probably" OK.
unsafe impl<T: Send + spirv_cross::spirv::Target> Send for CrossReflect<T>
where

View file

@ -15,16 +15,6 @@ mod helper;
#[cfg(feature = "naga")]
pub mod naga;
pub trait ShaderOutputCompiler<O: ShaderReflectObject, T: OutputTarget, Opt, Ctx> {
/// Create the reflection object
fn create_reflection(
compiled: O,
) -> Result<
impl ReflectShader + CompileShader<T, Options = Opt, Context = Ctx>,
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<ShaderReflection, ShaderReflectError>;
}
use crate::back::targets::OutputTarget;
use crate::back::CompileShader;
use crate::front::ShaderReflectObject;
pub use semantics::ShaderReflection;
#[inline(always)]

View file

@ -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<T: OutputTarget, Opt, Ctx> ShaderOutputCompiler<SpirvCompilation, T, Opt, Ctx> for Naga
where
NagaReflect: CompileShader<T, Options = Opt, Context = Ctx>,
{
fn create_reflection(
compile: SpirvCompilation,
) -> Result<
impl ReflectShader + CompileShader<T, Options = Opt, Context = Ctx>,
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<Self, error::ShaderReflectError> {
fn lower_fragment_shader(words: &[u32]) -> Vec<u32> {
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<TypeInfo> {
let (TypeInner::Vector { .. } | TypeInner::Scalar { .. } | TypeInner::Matrix { .. }) = *ty

View file

@ -75,7 +75,7 @@ pub(crate) struct FilterCommon {
}
type ShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<HLSL, SpirvCompilation, SpirvCross<HLSL>> + Send>;
ShaderPassArtifact<impl CompileReflectShader<HLSL, SpirvCompilation, SpirvCross> + Send>;
fn compile_passes(
shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
@ -85,11 +85,11 @@ fn compile_passes(
HLSL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross<HLSL>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
HLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross<HLSL>, FilterChainError>(
HLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -146,7 +146,7 @@ impl Drop for FrameResiduals {
}
type DxilShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<DXIL, SpirvCompilation, SpirvCross<DXIL>> + Send>;
ShaderPassArtifact<impl CompileReflectShader<DXIL, SpirvCompilation, SpirvCross> + Send>;
fn compile_passes_dxil(
shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
@ -156,11 +156,11 @@ fn compile_passes_dxil(
DXIL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross<DXIL>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
DXIL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross<DXIL>, FilterChainError>(
DXIL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};
@ -168,7 +168,7 @@ fn compile_passes_dxil(
Ok((passes, semantics))
}
type HlslShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<HLSL, SpirvCompilation, SpirvCross<HLSL>> + Send>;
ShaderPassArtifact<impl CompileReflectShader<HLSL, SpirvCompilation, SpirvCross> + Send>;
fn compile_passes_hlsl(
shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
@ -178,11 +178,11 @@ fn compile_passes_hlsl(
HLSL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross<HLSL>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
HLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross<HLSL>, FilterChainError>(
HLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -99,7 +99,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
}
type ShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<GLSL, SpirvCompilation, SpirvCross<GLSL>>>;
ShaderPassArtifact<impl CompileReflectShader<GLSL, SpirvCompilation, SpirvCross>>;
fn compile_passes(
shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
@ -109,11 +109,11 @@ fn compile_passes(
GLSL::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross<GLSL>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
GLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross<GLSL>, FilterChainError>(
GLSL::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};

View file

@ -208,9 +208,8 @@ impl Drop for FrameResiduals {
}
}
type ShaderPassMeta = ShaderPassArtifact<
impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross<SPIRV>> + Send,
>;
type ShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross> + Send>;
fn compile_passes(
shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
@ -220,16 +219,13 @@ fn compile_passes(
SPIRV::compile_preset_passes::<
Glslang,
CachedCompilation<SpirvCompilation>,
SpirvCross<SPIRV>,
SpirvCross,
FilterChainError,
>(shaders, &textures)?
} else {
SPIRV::compile_preset_passes::<
Glslang,
SpirvCompilation,
SpirvCross<SPIRV>,
FilterChainError,
>(shaders, &textures)?
SPIRV::compile_preset_passes::<Glslang, SpirvCompilation, SpirvCross, FilterChainError>(
shaders, &textures,
)?
};
Ok((passes, semantics))