Skip to content

Commit

Permalink
reflect: allow compilation of boxed trait objects
Browse files Browse the repository at this point in the history
Add a hidden `compile_boxed` function to CompileShader to support this. This is to allow Box<dyn CompileReflectShader> to work.
  • Loading branch information
chyyran committed Sep 15, 2024
1 parent dfabed2 commit 24a2830
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
50 changes: 50 additions & 0 deletions librashader-reflect/src/back/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ pub trait CompileShader<T: OutputTarget> {
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;

/// Consume the object and return the compiled output of the shader.
///
/// This is an internal implementation detail for stable building without TAIT,
/// to allow delegation when Self is unsized (i.e. dyn CompileReflectShader).
#[doc(hidden)]
fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;
}

/// Marker trait for combinations of targets and compilations that can be reflected and compiled
Expand Down Expand Up @@ -81,6 +91,13 @@ where
) -> Result<ShaderCompilerOutput<E::Output, Self::Context>, ShaderCompileError> {
self.backend.compile(options)
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<E::Output, Self::Context>, ShaderCompileError> {
self.backend.compile(options)
}
}

/// A trait for reflectable compilations that can be transformed
Expand Down Expand Up @@ -124,6 +141,39 @@ where
}
}

impl<T: ReflectShader + ?Sized> ReflectShader for Box<T> {
fn reflect(
&mut self,
pass_number: usize,
semantics: &ShaderSemantics,
) -> Result<ShaderReflection, ShaderReflectError> {
(**self).reflect(pass_number, semantics)
}
}

impl<O, T> CompileShader<T> for Box<O>
where
O: CompileShader<T> + ?Sized,
T: OutputTarget,
{
type Options = O::Options;
type Context = O::Context;

fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError> {
O::compile_boxed(self, options)
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError> {
self.compile(options)
}
}

#[cfg(test)]
mod test {
use crate::front::{Glslang, ShaderInputCompiler};
Expand Down
7 changes: 7 additions & 0 deletions librashader-reflect/src/reflect/cross/glsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,11 @@ impl CompileShader<GLSL> for CrossReflect<targets::Glsl> {
},
})
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
<CrossReflect<targets::Glsl> as CompileShader<GLSL>>::compile(*self, options)
}
}
7 changes: 7 additions & 0 deletions librashader-reflect/src/reflect/cross/hlsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,11 @@ impl CompileShader<HLSL> for CrossReflect<targets::Hlsl> {
},
})
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
<CrossReflect<targets::Hlsl> as CompileShader<HLSL>>::compile(*self, options)
}
}
7 changes: 7 additions & 0 deletions librashader-reflect/src/reflect/naga/msl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ impl CompileShader<MSL> for NagaReflect {
},
})
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
<NagaReflect as CompileShader<MSL>>::compile(*self, options)
}
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions librashader-reflect/src/reflect/naga/spirv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ impl CompileShader<SPIRV> for NagaReflect {
},
})
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<Vec<u32>, Self::Context>, ShaderCompileError> {
<NagaReflect as CompileShader<SPIRV>>::compile(*self, options)
}
}
7 changes: 7 additions & 0 deletions librashader-reflect/src/reflect/naga/wgsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ impl CompileShader<WGSL> for NagaReflect {
},
})
}

fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
<NagaReflect as CompileShader<WGSL>>::compile(*self, options)
}
}

0 comments on commit 24a2830

Please sign in to comment.