diff --git a/Cargo.lock b/Cargo.lock index aa840eb..774dd7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -327,17 +327,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15b55663a85f33501257357e6421bb33e769d5c9ffb5ba0921c975a123e35e68" dependencies = [ "block-sys", - "objc2 0.4.1", -] - -[[package]] -name = "block2" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e58aa60e59d8dbfcc36138f5f18be5f24394d33b38b24f7fd0b1caa33095f22f" -dependencies = [ - "block-sys", - "objc2 0.5.0", + "objc2", ] [[package]] @@ -1295,19 +1285,9 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99d3aaff8a54577104bafdf686ff18565c3b6903ca5782a2026ef06e2c7aa319" dependencies = [ - "block2 0.3.0", + "block2", "dispatch", - "objc2 0.4.1", -] - -[[package]] -name = "icrate" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e286f4b975ac6c054971a0600a9b76438b332edace54bff79c71c9d3adfc9772" -dependencies = [ - "block2 0.4.0", - "objc2 0.5.0", + "objc2", ] [[package]] @@ -1554,7 +1534,6 @@ version = "0.2.0-beta.9" dependencies = [ "ash", "gl", - "icrate 0.1.0", "num-traits", "wgpu-types", "windows 0.48.0", @@ -1704,23 +1683,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "librashader-runtime-metal" -version = "0.2.0-beta.7" -dependencies = [ - "array-concat", - "bytemuck", - "icrate 0.1.0", - "librashader-common 0.2.0-beta.9", - "librashader-preprocess", - "librashader-presets 0.2.0-beta.9", - "librashader-reflect", - "librashader-runtime", - "objc2 0.5.0", - "rustc-hash", - "thiserror", -] - [[package]] name = "librashader-runtime-vk" version = "0.2.0-beta.9" @@ -2095,17 +2057,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "559c5a40fdd30eb5e344fbceacf7595a81e242529fb4e21cf5f43fb4f11ff98d" dependencies = [ "objc-sys", - "objc2-encode 3.0.0", -] - -[[package]] -name = "objc2" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a9c7f0d511a4ce26b078183179dca908171cfc69f88986fe36c5138e1834476" -dependencies = [ - "objc-sys", - "objc2-encode 4.0.0", + "objc2-encode", ] [[package]] @@ -2114,12 +2066,6 @@ version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d079845b37af429bfe5dfa76e6d087d788031045b25cfc6fd898486fd9847666" -[[package]] -name = "objc2-encode" -version = "4.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ff06a6505cde0766484f38d8479ac8e6d31c66fbc2d5492f65ca8c091456379" - [[package]] name = "objc_exception" version = "0.1.2" @@ -3642,14 +3588,14 @@ dependencies = [ "core-foundation", "core-graphics", "cursor-icon", - "icrate 0.0.4", + "icrate", "js-sys", "libc", "log", "memmap2", "ndk", "ndk-sys", - "objc2 0.4.1", + "objc2", "once_cell", "orbclient", "percent-encoding", diff --git a/librashader-cache/src/compilation.rs b/librashader-cache/src/compilation.rs index b4b1200..06148cd 100644 --- a/librashader-cache/src/compilation.rs +++ b/librashader-cache/src/compilation.rs @@ -6,18 +6,20 @@ use librashader_reflect::back::targets::{GLSL, HLSL, SPIRV}; use librashader_reflect::back::{CompilerBackend, FromCompilation}; use librashader_reflect::error::{ShaderCompileError, ShaderReflectError}; -use librashader_reflect::front::{Glslang, ShaderInputCompiler, ShaderReflectObject, SpirvCompilation}; +use librashader_reflect::front::{ + Glslang, ShaderInputCompiler, ShaderReflectObject, SpirvCompilation, +}; pub struct CachedCompilation { compilation: T, } -impl ShaderReflectObject for CachedCompilation { - -} +impl ShaderReflectObject for CachedCompilation {} impl serde::Deserialize<'de> + serde::Serialize + Clone> -ShaderInputCompiler> for Glslang where Glslang: ShaderInputCompiler + ShaderInputCompiler> for Glslang +where + Glslang: ShaderInputCompiler, { fn compile(source: &ShaderSource) -> Result, ShaderCompileError> { let cache = crate::cache::internal::get_cache(); diff --git a/librashader-capi/src/ctypes.rs b/librashader-capi/src/ctypes.rs index 47bd45d..0813331 100644 --- a/librashader-capi/src/ctypes.rs +++ b/librashader-capi/src/ctypes.rs @@ -70,24 +70,21 @@ use librashader::runtime::d3d11::FilterChain as FilterChainD3D11; /// A handle to a Direct3D 11 filter chain. #[cfg(all(target_os = "windows", feature = "runtime-d3d11"))] #[doc(cfg(all(target_os = "windows", feature = "runtime-d3d11")))] -pub type libra_d3d11_filter_chain_t = - Option>; +pub type libra_d3d11_filter_chain_t = Option>; #[cfg(all(target_os = "windows", feature = "runtime-d3d12"))] use librashader::runtime::d3d12::FilterChain as FilterChainD3D12; /// A handle to a Direct3D 12 filter chain. #[cfg(all(target_os = "windows", feature = "runtime-d3d12"))] #[doc(cfg(all(target_os = "windows", feature = "runtime-d3d12")))] -pub type libra_d3d12_filter_chain_t = - Option>; +pub type libra_d3d12_filter_chain_t = Option>; #[cfg(feature = "runtime-vulkan")] use librashader::runtime::vk::FilterChain as FilterChainVulkan; /// A handle to a Vulkan filter chain. #[cfg(feature = "runtime-vulkan")] #[doc(cfg(feature = "runtime-vulkan"))] -pub type libra_vk_filter_chain_t = - Option>; +pub type libra_vk_filter_chain_t = Option>; /// Defines the output viewport for a rendered frame. #[repr(C)] @@ -174,4 +171,4 @@ mod __cbindgen_opaque_forward_declarations { /// Opaque struct for a Vulkan filter chain. FilterChainVulkan; } -} \ No newline at end of file +} diff --git a/librashader-capi/src/runtime/d3d11/filter_chain.rs b/librashader-capi/src/runtime/d3d11/filter_chain.rs index 128608f..2ec3dcd 100644 --- a/librashader-capi/src/runtime/d3d11/filter_chain.rs +++ b/librashader-capi/src/runtime/d3d11/filter_chain.rs @@ -3,7 +3,9 @@ use crate::ctypes::{ }; use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError}; use crate::ffi::extern_fn; -use librashader::runtime::d3d11::{FilterChain, FilterChainOptions, FrameOptions, D3D11InputView, D3D11OutputView}; +use librashader::runtime::d3d11::{ + D3D11InputView, D3D11OutputView, FilterChain, FilterChainOptions, FrameOptions, +}; use std::ffi::c_char; use std::ffi::CStr; use std::mem::{ManuallyDrop, MaybeUninit}; diff --git a/librashader-capi/src/runtime/d3d12/filter_chain.rs b/librashader-capi/src/runtime/d3d12/filter_chain.rs index 3b8249a..a0360af 100644 --- a/librashader-capi/src/runtime/d3d12/filter_chain.rs +++ b/librashader-capi/src/runtime/d3d12/filter_chain.rs @@ -14,7 +14,9 @@ use windows::Win32::Graphics::Direct3D12::{ use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT; use crate::LIBRASHADER_API_VERSION; -use librashader::runtime::d3d12::{FilterChain, FilterChainOptions, FrameOptions, D3D12InputImage, D3D12OutputView}; +use librashader::runtime::d3d12::{ + D3D12InputImage, D3D12OutputView, FilterChain, FilterChainOptions, FrameOptions, +}; use librashader::runtime::{FilterChainParameters, Size, Viewport}; /// Direct3D 12 parameters for the source image. diff --git a/librashader-capi/src/runtime/gl/filter_chain.rs b/librashader-capi/src/runtime/gl/filter_chain.rs index 0d5c336..407645c 100644 --- a/librashader-capi/src/runtime/gl/filter_chain.rs +++ b/librashader-capi/src/runtime/gl/filter_chain.rs @@ -3,7 +3,9 @@ use crate::ctypes::{ }; use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError}; use crate::ffi::extern_fn; -use librashader::runtime::gl::{FilterChain, FilterChainOptions, FrameOptions, GLFramebuffer, GLImage}; +use librashader::runtime::gl::{ + FilterChain, FilterChainOptions, FrameOptions, GLFramebuffer, GLImage, +}; use std::ffi::CStr; use std::ffi::{c_char, c_void, CString}; use std::mem::MaybeUninit; diff --git a/librashader-capi/src/runtime/vk/filter_chain.rs b/librashader-capi/src/runtime/vk/filter_chain.rs index a84a55d..f3af7d8 100644 --- a/librashader-capi/src/runtime/vk/filter_chain.rs +++ b/librashader-capi/src/runtime/vk/filter_chain.rs @@ -3,7 +3,9 @@ use crate::ctypes::{ }; use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError}; use crate::ffi::extern_fn; -use librashader::runtime::vk::{FrameOptions, FilterChainOptions, FilterChain, VulkanImage, VulkanInstance}; +use librashader::runtime::vk::{ + FilterChain, FilterChainOptions, FrameOptions, VulkanImage, VulkanInstance, +}; use std::ffi::CStr; use std::ffi::{c_char, c_void}; use std::mem::MaybeUninit; diff --git a/librashader-presets/src/context.rs b/librashader-presets/src/context.rs index b9a977e..5affaed 100644 --- a/librashader-presets/src/context.rs +++ b/librashader-presets/src/context.rs @@ -379,7 +379,6 @@ pub(crate) fn apply_context(path: &mut PathBuf, context: &FxHashMap { let haystack = path.as_encoded_bytes(); - let replaced = WILDCARD_REGEX.replace_all(haystack, |caps: ®ex::bytes::Captures| { let Some(name) = caps.get(1) else { diff --git a/librashader-presets/src/lib.rs b/librashader-presets/src/lib.rs index 4cb0de1..22548ac 100644 --- a/librashader-presets/src/lib.rs +++ b/librashader-presets/src/lib.rs @@ -11,11 +11,11 @@ #![allow(stable_features)] #![allow(unstable_name_collisions)] +pub mod context; mod error; mod extract_if; mod parse; mod preset; -pub mod context; pub use context::WildcardContext; pub use error::*; diff --git a/librashader-presets/src/parse/mod.rs b/librashader-presets/src/parse/mod.rs index 7246ef4..e78ad23 100644 --- a/librashader-presets/src/parse/mod.rs +++ b/librashader-presets/src/parse/mod.rs @@ -10,8 +10,8 @@ mod value; pub(crate) type Span<'a> = LocatedSpan<&'a str>; pub(crate) use token::Token; -use crate::error::ParsePresetError; use crate::context::{VideoDriver, WildcardContext}; +use crate::error::ParsePresetError; use crate::parse::preset::resolve_values; use crate::parse::value::parse_preset; use crate::ShaderPreset; diff --git a/librashader-presets/src/parse/token.rs b/librashader-presets/src/parse/token.rs index a237710..d0bdfe3 100644 --- a/librashader-presets/src/parse/token.rs +++ b/librashader-presets/src/parse/token.rs @@ -171,7 +171,7 @@ pub fn do_lex(input: &str) -> Result, ParsePresetError> { #[cfg(test)] mod test { - use crate::parse::token::{single_comment}; + use crate::parse::token::single_comment; #[test] fn parses_single_line_comment() { diff --git a/librashader-presets/src/parse/value.rs b/librashader-presets/src/parse/value.rs index dd97307..0a0b4ff 100644 --- a/librashader-presets/src/parse/value.rs +++ b/librashader-presets/src/parse/value.rs @@ -17,8 +17,8 @@ use std::io::Read; use std::path::{Path, PathBuf}; use std::str::FromStr; -use crate::extract_if::MakeExtractIf; use crate::context::{apply_context, WildcardContext}; +use crate::extract_if::MakeExtractIf; #[derive(Debug)] pub enum Value { @@ -615,8 +615,8 @@ pub fn parse_values( #[cfg(test)] mod test { use crate::parse::value::parse_preset; - use std::path::PathBuf; use crate::WildcardContext; + use std::path::PathBuf; #[test] pub fn parse_basic() { diff --git a/librashader-reflect/Cargo.toml b/librashader-reflect/Cargo.toml index 5810eec..bb13e23 100644 --- a/librashader-reflect/Cargo.toml +++ b/librashader-reflect/Cargo.toml @@ -44,3 +44,4 @@ dxil = ["cross", "spirv-to-dxil"] wgsl = ["cross", "naga", "spirv", "rspirv"] cross = [ "spirv_cross", "spirv_cross/glsl", "spirv_cross/hlsl" ] serialize = [ "serde" ] +msl = [ "cross", "naga" ] \ No newline at end of file diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index a69ae9d..54cdcd3 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -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, GlslReflect, HlslReflect}; -use crate::reflect::ReflectShader; +use crate::reflect::cross::{CompiledProgram, GlslReflect, HlslReflect, SpirvCross}; +use crate::reflect::{ReflectShader, ShaderOutputCompiler}; /// The GLSL version to target. pub use spirv_cross::glsl::Version as GlslVersion; @@ -29,8 +29,9 @@ impl FromCompilation for GLSL { fn from_compilation( compile: SpirvCompilation, ) -> Result, ShaderReflectError> { + let backend = SpirvCross::::create_reflection(compile)?; Ok(CompilerBackend { - backend: GlslReflect::try_from(&compile)?, + backend, }) } } @@ -52,7 +53,7 @@ impl FromCompilation for HLSL { compile: SpirvCompilation, ) -> Result, ShaderReflectError> { Ok(CompilerBackend { - backend: HlslReflect::try_from(&compile)?, + backend: SpirvCross::::create_reflection(compile)?, }) } } diff --git a/librashader-reflect/src/back/dxil.rs b/librashader-reflect/src/back/dxil.rs index ea988f5..274cd3b 100644 --- a/librashader-reflect/src/back/dxil.rs +++ b/librashader-reflect/src/back/dxil.rs @@ -10,7 +10,7 @@ use crate::back::targets::{OutputTarget, DXIL}; use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::front::SpirvCompilation; use crate::reflect::cross::GlslReflect; -use crate::reflect::ReflectShader; +use crate::reflect::{ReflectShader, ShaderOutputCompiler}; impl OutputTarget for DXIL { type Output = DxilObject; @@ -26,7 +26,7 @@ impl FromCompilation for DXIL { fn from_compilation( compile: SpirvCompilation, ) -> Result, ShaderReflectError> { - let reflect = GlslReflect::try_from(&compile)?; + let reflect = GlslReflect::new().create_reflection(compile)?; let vertex = compile.vertex; let fragment = compile.fragment; Ok(CompilerBackend { diff --git a/librashader-reflect/src/back/mod.rs b/librashader-reflect/src/back/mod.rs index 1d361aa..d89664f 100644 --- a/librashader-reflect/src/back/mod.rs +++ b/librashader-reflect/src/back/mod.rs @@ -1,17 +1,16 @@ pub mod cross; #[cfg(all(target_os = "windows", feature = "dxil"))] pub mod dxil; +mod msl; mod spirv; pub mod targets; pub mod wgsl; -mod msl; use crate::back::targets::OutputTarget; use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::reflect::semantics::ShaderSemantics; use crate::reflect::{ReflectShader, ShaderReflection}; use std::fmt::Debug; -use crate::front::ShaderReflectObject; /// The output of the shader compiler. #[derive(Debug)] diff --git a/librashader-reflect/src/back/msl.rs b/librashader-reflect/src/back/msl.rs index e69de29..8b13789 100644 --- a/librashader-reflect/src/back/msl.rs +++ b/librashader-reflect/src/back/msl.rs @@ -0,0 +1 @@ + diff --git a/librashader-reflect/src/back/wgsl/mod.rs b/librashader-reflect/src/back/wgsl/mod.rs index b45c003..27b0f91 100644 --- a/librashader-reflect/src/back/wgsl/mod.rs +++ b/librashader-reflect/src/back/wgsl/mod.rs @@ -1,17 +1,13 @@ -mod lower_samplers; - use crate::back::targets::WGSL; -use crate::back::wgsl::lower_samplers::LowerCombinedImageSamplerPass; use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompilerOutput}; use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::front::SpirvCompilation; -use crate::reflect::naga::NagaReflect; -use crate::reflect::ReflectShader; +use crate::reflect::naga::{Naga, NagaReflect}; +use crate::reflect::{ReflectShader, ShaderOutputCompiler}; use naga::back::wgsl::WriterFlags; use naga::valid::{Capabilities, ValidationFlags}; use naga::{AddressSpace, Module}; use rspirv::binary::Assemble; -use rspirv::dr::Builder; /// The context for a WGSL compilation via Naga pub struct NagaWgslContext { @@ -36,36 +32,8 @@ impl FromCompilation for WGSL { fn from_compilation( compile: SpirvCompilation, ) -> Result, ShaderReflectError> { - fn lower_fragment_shader(words: &[u32]) -> Vec { - let mut loader = rspirv::dr::Loader::new(); - rspirv::binary::parse_words(words, &mut loader).unwrap(); - let module = loader.module(); - let mut builder = Builder::new_from_module(module); - - let mut pass = LowerCombinedImageSamplerPass::new(&mut builder); - - pass.ensure_op_type_sampler(); - pass.do_pass(); - - let module = builder.module(); - - module.assemble() - } - - let options = naga::front::spv::Options { - adjust_coordinate_space: true, - strict_capabilities: false, - block_ctx_dump_prefix: None, - }; - - let vertex = - naga::front::spv::parse_u8_slice(bytemuck::cast_slice(&compile.vertex), &options)?; - - let fragment = lower_fragment_shader(&compile.fragment); - let fragment = naga::front::spv::parse_u8_slice(bytemuck::cast_slice(&fragment), &options)?; - Ok(CompilerBackend { - backend: NagaReflect { vertex, fragment }, + backend: Naga::create_reflection(compile)?, }) } } diff --git a/librashader-reflect/src/front/glslang.rs b/librashader-reflect/src/front/glslang.rs index eb1e290..86f3e40 100644 --- a/librashader-reflect/src/front/glslang.rs +++ b/librashader-reflect/src/front/glslang.rs @@ -2,9 +2,9 @@ use crate::error::ShaderCompileError; use glslang::{CompilerOptions, ShaderInput}; use librashader_preprocess::ShaderSource; +use crate::front::{ShaderInputCompiler, SpirvCompilation}; #[cfg(feature = "serialize")] use serde::{Deserialize, Serialize}; -use crate::front::{ShaderInputCompiler, SpirvCompilation}; /// glslang compiler pub struct Glslang; @@ -15,9 +15,7 @@ impl ShaderInputCompiler for Glslang { } } -pub(crate) fn compile_spirv( - source: &ShaderSource, -) -> Result { +pub(crate) fn compile_spirv(source: &ShaderSource) -> Result { let compiler = glslang::Compiler::acquire().ok_or(ShaderCompileError::CompilerInitError)?; let options = CompilerOptions { source_language: glslang::SourceLanguage::GLSL, diff --git a/librashader-reflect/src/front/mod.rs b/librashader-reflect/src/front/mod.rs index cfb6259..b95090f 100644 --- a/librashader-reflect/src/front/mod.rs +++ b/librashader-reflect/src/front/mod.rs @@ -4,10 +4,7 @@ use serde::{Deserialize, Serialize}; mod glslang; - -pub trait ShaderReflectObject : Sized { - -} +pub trait ShaderReflectObject: Sized {} pub use glslang::Glslang; @@ -17,7 +14,7 @@ pub trait ShaderInputCompiler: Sized { fn compile(source: &ShaderSource) -> Result; } -impl ShaderReflectObject for SpirvCompilation { } +impl ShaderReflectObject for SpirvCompilation {} /// A reflectable shader compilation via glslang. #[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] @@ -41,4 +38,3 @@ impl TryFrom<&ShaderSource> for SpirvCompilation { Glslang::compile(source) } } - diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 93aa8e4..4fd463f 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -1,3 +1,5 @@ +use std::borrow::Borrow; +use std::marker::PhantomData; use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError}; use crate::front::SpirvCompilation; use crate::reflect::semantics::{ @@ -6,23 +8,72 @@ use crate::reflect::semantics::{ UniformMemberBlock, UniqueSemanticMap, UniqueSemantics, ValidateTypeSemantics, VariableMeta, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, }; -use crate::reflect::{align_uniform_size, ReflectShader}; +use crate::reflect::{align_uniform_size, ReflectShader, ShaderOutputCompiler}; use std::ops::Deref; -use spirv_cross::spirv::{Ast, Decoration, Module, Resource, ShaderResources, Type}; +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::{GLSL, HLSL}; +use crate::back::targets::{GLSL, HLSL, OutputTarget}; use crate::back::{CompileShader, ShaderCompilerOutput}; use crate::reflect::helper::{SemanticErrorBlame, TextureData, UboData}; + +pub trait SpirvCrossTarget { + type CrossTarget: spirv_cross::spirv::Target; +} + +impl SpirvCrossTarget for HLSL { + type CrossTarget = spirv_cross::hlsl::Target; +} + + +impl SpirvCrossTarget for GLSL { + type CrossTarget = spirv_cross::glsl::Target; +} + +pub struct SpirvCross(PhantomData); + +pub(crate) type HlslReflect = CrossReflect; +pub(crate) type GlslReflect = CrossReflect; + +impl +ShaderOutputCompiler for SpirvCross +// where Spv: spirv_cross::spirv::Target, +// Ast: spirv_cross::spirv::Compile, +// Ast: spirv_cross::spirv::Parse, +where CrossReflect<::CrossTarget> + : CompileShader, + ::CrossTarget: spirv_cross::spirv::Target, + Ast<::CrossTarget>: spirv_cross::spirv::Compile<::CrossTarget>, + Ast<::CrossTarget>: spirv_cross::spirv::Parse<::CrossTarget>, +{ + fn create_reflection(compiled: SpirvCompilation) + -> Result, + ShaderReflectError> { + let compiled = compiled.borrow(); + let vertex_module = Module::from_words(&compiled.vertex); + let fragment_module = Module::from_words(&compiled.fragment); + + let vertex = Ast::::parse(&vertex_module)?; + let fragment = Ast::::parse(&fragment_module)?; + + Ok(CrossReflect { vertex, fragment }) + } +} + // This is "probably" OK. -unsafe impl Send for CrossReflect {} +unsafe impl Send for CrossReflect +where Ast: spirv_cross::spirv::Compile, + Ast: spirv_cross::spirv::Parse, +{} pub(crate) struct CrossReflect where T: spirv_cross::spirv::Target, + Ast: spirv_cross::spirv::Compile, + Ast: spirv_cross::spirv::Parse, { vertex: Ast, fragment: Ast, @@ -49,8 +100,7 @@ where pub fragment: CompiledAst, } -pub(crate) type HlslReflect = CrossReflect; -pub(crate) type GlslReflect = CrossReflect; + impl ValidateTypeSemantics for UniqueSemantics { fn validate_type(&self, ty: &Type) -> Option { diff --git a/librashader-reflect/src/reflect/mod.rs b/librashader-reflect/src/reflect/mod.rs index bd3b6c7..a3a425d 100644 --- a/librashader-reflect/src/reflect/mod.rs +++ b/librashader-reflect/src/reflect/mod.rs @@ -1,3 +1,4 @@ +use std::borrow::Borrow; use crate::error::ShaderReflectError; use semantics::ShaderSemantics; @@ -12,9 +13,19 @@ pub mod presets; mod helper; -#[cfg(feature = "wgsl")] +#[cfg(feature = "naga")] pub mod naga; +pub trait ShaderOutputCompiler { + /// Create the reflection object + fn create_reflection( + compiled: O, + ) -> Result< + impl ReflectShader + CompileShader, + 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 @@ -26,6 +37,9 @@ pub trait ReflectShader { ) -> Result; } +use crate::back::targets::OutputTarget; +use crate::back::CompileShader; +use crate::front::ShaderReflectObject; pub use semantics::ShaderReflection; #[inline(always)] diff --git a/librashader-reflect/src/back/wgsl/lower_samplers.rs b/librashader-reflect/src/reflect/naga/lower_samplers.rs similarity index 100% rename from librashader-reflect/src/back/wgsl/lower_samplers.rs rename to librashader-reflect/src/reflect/naga/lower_samplers.rs diff --git a/librashader-reflect/src/reflect/naga.rs b/librashader-reflect/src/reflect/naga/mod.rs similarity index 94% rename from librashader-reflect/src/reflect/naga.rs rename to librashader-reflect/src/reflect/naga/mod.rs index 9dfca2c..f719705 100644 --- a/librashader-reflect/src/reflect/naga.rs +++ b/librashader-reflect/src/reflect/naga/mod.rs @@ -1,9 +1,17 @@ +mod lower_samplers; + +use std::borrow::Borrow; use crate::error::{SemanticsErrorKind, ShaderReflectError}; +use crate::back::targets::OutputTarget; +use crate::back::CompileShader; +use crate::front::SpirvCompilation; use naga::{ AddressSpace, Binding, GlobalVariable, Handle, ImageClass, Module, ResourceBinding, Scalar, ScalarKind, TypeInner, VectorSize, }; +use rspirv::binary::Assemble; +use rspirv::dr::Builder; use crate::reflect::helper::{SemanticErrorBlame, TextureData, UboData}; use crate::reflect::semantics::{ @@ -12,7 +20,57 @@ use crate::reflect::semantics::{ UniqueSemanticMap, UniqueSemantics, ValidateTypeSemantics, VariableMeta, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, }; -use crate::reflect::{align_uniform_size, ReflectShader, ShaderReflection}; +use crate::reflect::{align_uniform_size, ReflectShader, ShaderOutputCompiler, ShaderReflection}; + +/// Represents the naga output compiler. +/// +/// The Naga reflector will lower combined image samplers to split, +/// with the same bind point on descriptor group 1. +pub struct Naga; + +impl ShaderOutputCompiler for Naga +where + NagaReflect: CompileShader, +{ + fn create_reflection( + compile: SpirvCompilation, + ) -> Result< + impl ReflectShader + CompileShader, + ShaderReflectError + > { + fn lower_fragment_shader(words: &[u32]) -> Vec { + let mut loader = rspirv::dr::Loader::new(); + rspirv::binary::parse_words(words, &mut loader).unwrap(); + let module = loader.module(); + let mut builder = Builder::new_from_module(module); + + let mut pass = lower_samplers::LowerCombinedImageSamplerPass::new(&mut builder); + + pass.ensure_op_type_sampler(); + pass.do_pass(); + + let module = builder.module(); + + module.assemble() + } + + let compile = compile.borrow(); + + let options = naga::front::spv::Options { + adjust_coordinate_space: true, + strict_capabilities: false, + block_ctx_dump_prefix: None, + }; + + let vertex = + naga::front::spv::parse_u8_slice(bytemuck::cast_slice(&compile.vertex), &options)?; + + let fragment = lower_fragment_shader(&compile.fragment); + let fragment = naga::front::spv::parse_u8_slice(bytemuck::cast_slice(&fragment), &options)?; + + Ok(NagaReflect { vertex, fragment }) + } +} #[derive(Debug)] pub(crate) struct NagaReflect { diff --git a/librashader-reflect/src/reflect/presets.rs b/librashader-reflect/src/reflect/presets.rs index b0c9a53..91a278f 100644 --- a/librashader-reflect/src/reflect/presets.rs +++ b/librashader-reflect/src/reflect/presets.rs @@ -1,7 +1,7 @@ use crate::back::targets::OutputTarget; use crate::back::{CompilerBackend, FromCompilation}; use crate::error::{ShaderCompileError, ShaderReflectError}; -use crate::front::{ShaderInputCompiler, ShaderReflectObject, SpirvCompilation}; +use crate::front::{ShaderInputCompiler, ShaderReflectObject}; use crate::reflect::semantics::{ Semantic, ShaderSemantics, TextureSemantics, UniformSemantic, UniqueSemantics, }; diff --git a/librashader-runtime-d3d11/src/filter_chain.rs b/librashader-runtime-d3d11/src/filter_chain.rs index 49e7487..9f24ab8 100644 --- a/librashader-runtime-d3d11/src/filter_chain.rs +++ b/librashader-runtime-d3d11/src/filter_chain.rs @@ -73,8 +73,7 @@ pub(crate) struct FilterCommon { pub(crate) draw_quad: DrawQuad, } -type ShaderPassMeta = - ShaderPassArtifact + Send>; +type ShaderPassMeta = ShaderPassArtifact + Send>; fn compile_passes( shaders: Vec, textures: &[TextureConfig], @@ -85,7 +84,9 @@ fn compile_passes( shaders, &textures, )? } else { - HLSL::compile_preset_passes::(shaders, &textures)? + HLSL::compile_preset_passes::( + shaders, &textures, + )? }; Ok((passes, semantics)) diff --git a/librashader-runtime-d3d12/src/filter_chain.rs b/librashader-runtime-d3d12/src/filter_chain.rs index 06726ca..0c4665f 100644 --- a/librashader-runtime-d3d12/src/filter_chain.rs +++ b/librashader-runtime-d3d12/src/filter_chain.rs @@ -3,6 +3,7 @@ use crate::descriptor_heap::{ CpuStagingHeap, D3D12DescriptorHeap, D3D12DescriptorHeapSlot, RenderTargetHeap, ResourceWorkHeap, }; +use crate::draw_quad::DrawQuad; use crate::error::FilterChainError; use crate::filter_pass::FilterPass; use crate::framebuffer::OwnedImage; @@ -10,7 +11,6 @@ use crate::graphics_pipeline::{D3D12GraphicsPipeline, D3D12RootSignature}; use crate::luts::LutTexture; use crate::mipmap::D3D12MipmapGen; use crate::options::{FilterChainOptionsD3D12, FrameOptionsD3D12}; -use crate::draw_quad::DrawQuad; use crate::samplers::SamplerSet; use crate::texture::{D3D12InputImage, D3D12OutputView, InputTexture, OutputDescriptor}; use crate::{error, util}; @@ -156,7 +156,9 @@ fn compile_passes_dxil( shaders, &textures, )? } else { - DXIL::compile_preset_passes::(shaders, &textures)? + DXIL::compile_preset_passes::( + shaders, &textures, + )? }; Ok((passes, semantics)) @@ -173,7 +175,9 @@ fn compile_passes_hlsl( shaders, &textures, )? } else { - HLSL::compile_preset_passes::(shaders, &textures)? + HLSL::compile_preset_passes::( + shaders, &textures, + )? }; Ok((passes, semantics)) diff --git a/librashader-runtime-d3d12/src/graphics_pipeline.rs b/librashader-runtime-d3d12/src/graphics_pipeline.rs index d1f6833..41267ed 100644 --- a/librashader-runtime-d3d12/src/graphics_pipeline.rs +++ b/librashader-runtime-d3d12/src/graphics_pipeline.rs @@ -1,6 +1,6 @@ +use crate::draw_quad::DrawQuad; use crate::error::assume_d3d12_init; use crate::error::FilterChainError::Direct3DOperationError; -use crate::draw_quad::DrawQuad; use crate::{error, util}; use librashader_cache::{cache_pipeline, cache_shader_object}; use librashader_reflect::back::cross::CrossHlslContext; diff --git a/librashader-runtime-d3d12/src/lib.rs b/librashader-runtime-d3d12/src/lib.rs index 53ecc40..d3b710d 100644 --- a/librashader-runtime-d3d12/src/lib.rs +++ b/librashader-runtime-d3d12/src/lib.rs @@ -5,6 +5,7 @@ mod buffer; mod descriptor_heap; +mod draw_quad; mod filter_chain; mod filter_pass; mod framebuffer; @@ -12,7 +13,6 @@ mod graphics_pipeline; mod luts; mod mipmap; mod parameters; -mod draw_quad; mod samplers; mod texture; mod util; diff --git a/librashader-runtime-gl/src/filter_chain/filter_impl.rs b/librashader-runtime-gl/src/filter_chain/filter_impl.rs index f6c9801..12254b4 100644 --- a/librashader-runtime-gl/src/filter_chain/filter_impl.rs +++ b/librashader-runtime-gl/src/filter_chain/filter_impl.rs @@ -108,7 +108,9 @@ fn compile_passes( shaders, &textures, )? } else { - GLSL::compile_preset_passes::(shaders, &textures)? + GLSL::compile_preset_passes::( + shaders, &textures, + )? }; Ok((passes, semantics)) diff --git a/librashader-runtime-vk/src/filter_chain.rs b/librashader-runtime-vk/src/filter_chain.rs index 492cd95..6f6a7c0 100644 --- a/librashader-runtime-vk/src/filter_chain.rs +++ b/librashader-runtime-vk/src/filter_chain.rs @@ -207,19 +207,22 @@ impl Drop for FrameResiduals { } } -type ShaderPassMeta = - ShaderPassArtifact + Send>; +type ShaderPassMeta = ShaderPassArtifact + Send>; fn compile_passes( shaders: Vec, textures: &[TextureConfig], disable_cache: bool, ) -> Result<(Vec, ShaderSemantics), FilterChainError> { let (passes, semantics) = if !disable_cache { - SPIRV::compile_preset_passes::, FilterChainError>( + SPIRV::compile_preset_passes::< + Glslang, + CachedCompilation, + FilterChainError, + >(shaders, &textures)? + } else { + SPIRV::compile_preset_passes::( shaders, &textures, )? - } else { - SPIRV::compile_preset_passes::(shaders, &textures)? }; Ok((passes, semantics)) diff --git a/librashader-runtime-wgpu/src/filter_chain.rs b/librashader-runtime-wgpu/src/filter_chain.rs index 45f4b7c..f8ce13e 100644 --- a/librashader-runtime-wgpu/src/filter_chain.rs +++ b/librashader-runtime-wgpu/src/filter_chain.rs @@ -37,14 +37,15 @@ use crate::options::{FilterChainOptionsWgpu, FrameOptionsWgpu}; use crate::samplers::SamplerSet; use crate::texture::{InputImage, OwnedImage}; -type ShaderPassMeta = - ShaderPassArtifact + Send>; +type ShaderPassMeta = ShaderPassArtifact + Send>; fn compile_passes( shaders: Vec, textures: &[TextureConfig], ) -> Result<(Vec, ShaderSemantics), FilterChainError> { let (passes, semantics) = - WGSL::compile_preset_passes::(shaders, &textures)?; + WGSL::compile_preset_passes::( + shaders, &textures, + )?; Ok((passes, semantics)) }