reflect: simplify shaderpassmeta type declarations with a macro

This commit is contained in:
chyyran 2023-01-19 00:37:37 -05:00
parent fc54c7f65c
commit b3dd378b5b
5 changed files with 52 additions and 46 deletions

View file

@ -1,12 +1,12 @@
use librashader_preprocess::{PreprocessError, ShaderSource};
use librashader_presets::{ShaderPassConfig, TextureConfig};
use crate::back::targets::OutputTarget;
use crate::back::{CompilerBackend, FromCompilation};
use crate::back::{CompileShader, CompilerBackend, FromCompilation};
use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::front::ShaderCompilation;
use crate::reflect::semantics::{
Semantic, ShaderSemantics, TextureSemantics, UniformSemantic, UniqueSemantics,
};
use librashader_preprocess::{PreprocessError, ShaderSource};
use librashader_presets::{ShaderPassConfig, TextureConfig};
use rustc_hash::FxHashMap;
use std::error::Error;
@ -23,26 +23,26 @@ pub trait CompilePreset: OutputTarget {
fn compile_preset_passes<C, E>(
passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
)-> Result<
) -> Result<
(
Vec<ShaderPassMeta<<Self as FromCompilation<C>>::Output>>,
ShaderSemantics,
),
E,
>
where
Self: Sized,
Self: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError> {
where
Self: Sized,
Self: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
{
compile_preset_passes::<Self, C, E>(passes, textures)
}
}
/// Compile passes of a shader preset given the applicable
/// shader output target, compilation type, and resulting error.
pub(crate) fn compile_preset_passes<T, C, E>(
@ -55,14 +55,14 @@ pub(crate) fn compile_preset_passes<T, C, E>(
),
E,
>
where
T: OutputTarget,
T: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
where
T: OutputTarget,
T: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
{
let mut uniform_semantics: FxHashMap<String, UniformSemantic> = Default::default();
let mut texture_semantics: FxHashMap<String, Semantic<TextureSemantics>> = Default::default();
@ -89,17 +89,9 @@ pub(crate) fn compile_preset_passes<T, C, E>(
.collect::<Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>, E>>()?;
for details in &passes {
insert_pass_semantics(
&mut uniform_semantics,
&mut texture_semantics,
&details.0,
)
insert_pass_semantics(&mut uniform_semantics, &mut texture_semantics, &details.0)
}
insert_lut_semantics(
textures,
&mut uniform_semantics,
&mut texture_semantics,
);
insert_lut_semantics(textures, &mut uniform_semantics, &mut texture_semantics);
let semantics = ShaderSemantics {
uniform_semantics,
@ -109,7 +101,6 @@ pub(crate) fn compile_preset_passes<T, C, E>(
Ok((passes, semantics))
}
/// Insert the available semantics for the input pass config into the provided semantic maps.
fn insert_pass_semantics(
uniform_semantics: &mut FxHashMap<String, UniformSemantic>,

View file

@ -2,7 +2,6 @@ use crate::texture::{D3D11InputView, InputTexture, LutTexture};
use librashader_common::{ImageFormat, Size, Viewport};
use librashader_presets::{ShaderPreset, TextureConfig};
use librashader_reflect::back::cross::CrossHlslContext;
use librashader_reflect::back::targets::HLSL;
use librashader_reflect::back::CompileShader;
use librashader_reflect::front::shaderc::GlslangCompilation;
@ -23,6 +22,8 @@ use crate::render_target::RenderTarget;
use crate::samplers::SamplerSet;
use crate::util::d3d11_compile_bound_shader;
use crate::{error, util, D3D11OutputView};
use librashader_reflect::reflect::presets::CompilePreset;
use librashader_runtime::decl_shader_pass_meta;
use librashader_runtime::uniforms::UniformStorage;
use windows::Win32::Graphics::Direct3D11::{
ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, D3D11_BIND_CONSTANT_BUFFER, D3D11_BUFFER_DESC,
@ -30,16 +31,13 @@ use windows::Win32::Graphics::Direct3D11::{
D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_USAGE_DYNAMIC,
};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM;
use librashader_reflect::reflect::presets::CompilePreset;
pub struct FilterMutable {
pub(crate) passes_enabled: usize,
pub(crate) parameters: FxHashMap<String, f32>,
}
type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta<
impl CompileShader<HLSL, Options = Option<()>, Context = CrossHlslContext> + ReflectShader,
>;
decl_shader_pass_meta!(type ShaderPassMeta = <HLSL, GlslangCompilation>);
/// A Direct3D 11 filter chain.
pub struct FilterChainD3D11 {

View file

@ -12,7 +12,7 @@ use gl::types::{GLint, GLuint};
use librashader_common::{FilterMode, Viewport, WrapMode};
use librashader_presets::ShaderPreset;
use librashader_reflect::back::cross::{CrossGlslContext, GlslVersion};
use librashader_reflect::back::cross::GlslVersion;
use librashader_reflect::back::targets::GLSL;
use librashader_reflect::back::CompileShader;
use librashader_reflect::front::shaderc::GlslangCompilation;
@ -20,11 +20,12 @@ use librashader_reflect::reflect::semantics::{
MemberOffset, ShaderSemantics, TextureSemantics, UniformBinding, UniformMeta,
};
use librashader_reflect::reflect::presets::CompilePreset;
use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::decl_shader_pass_meta;
use rustc_hash::FxHashMap;
use spirv_cross::spirv::Decoration;
use std::collections::VecDeque;
use librashader_reflect::reflect::presets::CompilePreset;
pub(crate) struct FilterChainImpl<T: GLInterface> {
pub(crate) common: FilterCommon,
@ -80,9 +81,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
}
}
type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta<
impl CompileShader<GLSL, Options = GlslVersion, Context = CrossGlslContext> + ReflectShader,
>;
decl_shader_pass_meta!(type ShaderPassMeta = <GLSL, GlslangCompilation>);
impl<T: GLInterface> FilterChainImpl<T> {
/// Load a filter chain from a pre-parsed `ShaderPreset`.

View file

@ -19,15 +19,16 @@ use librashader_presets::{ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::SPIRV;
use librashader_reflect::back::CompileShader;
use librashader_reflect::front::shaderc::GlslangCompilation;
use librashader_reflect::reflect::presets::CompilePreset;
use librashader_reflect::reflect::semantics::{ShaderSemantics, TextureSemantics, UniformBinding};
use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::decl_shader_pass_meta;
use librashader_runtime::image::{Image, UVDirection};
use librashader_runtime::uniforms::UniformStorage;
use rustc_hash::FxHashMap;
use std::collections::VecDeque;
use std::path::Path;
use std::sync::Arc;
use librashader_reflect::reflect::presets::CompilePreset;
/// A Vulkan device and metadata that is required by the shader runtime.
pub struct VulkanObjects {
@ -37,9 +38,7 @@ pub struct VulkanObjects {
pipeline_cache: vk::PipelineCache,
}
type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta<
impl CompileShader<SPIRV, Options = Option<()>, Context = ()> + ReflectShader,
>;
decl_shader_pass_meta!(type ShaderPassMeta = <SPIRV, GlslangCompilation>);
/// A collection of handles needed to access the Vulkan instance.
#[derive(Clone)]

View file

@ -23,3 +23,22 @@ pub mod ringbuffer;
/// Generic implementation of semantics binding.
pub mod binding;
/// Used to declare a `ShaderPassMeta` type for the given target shader language and compilation type.
#[macro_export]
macro_rules! decl_shader_pass_meta {
(type $ty_name:ident = <$target:ty, $compilation:ty>) => {
type $ty_name =
librashader_reflect::reflect::presets::ShaderPassMeta<
impl librashader_reflect::back::CompileShader<
$target,
Options = <$target as librashader_reflect::back::FromCompilation<
$compilation,
>>::Options,
Context = <$target as librashader_reflect::back::FromCompilation<
$compilation,
>>::Context,
> + librashader_reflect::reflect::ReflectShader,
>;
};
}