diff --git a/Cargo.lock b/Cargo.lock index de7ce69..0d54e9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,28 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bindgen" +version = "0.63.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36d860121800b2a9a94f9b5604b332d5cffb234ce17609ea479d723dbc9d3885" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -138,19 +160,39 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.78" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" dependencies = [ "jobserver", ] +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clang-sys" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa2e27ae6ab525c3d369ded447057bca5438d86dc3a68f6faafb8269ba82ebf3" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "cmake" version = "0.1.49" @@ -359,10 +401,16 @@ dependencies = [ ] [[package]] -name = "encoding_rs" -version = "0.8.31" +name = "either" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "encoding_rs" +version = "0.8.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" dependencies = [ "cfg-if", ] @@ -463,6 +511,15 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "gfx-maths" version = "0.2.8" @@ -537,6 +594,12 @@ dependencies = [ "cmake", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "hashbrown" version = "0.12.3" @@ -611,9 +674,9 @@ checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e" [[package]] name = "js-sys" -version = "0.3.60" +version = "0.3.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" dependencies = [ "wasm-bindgen", ] @@ -630,6 +693,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.139" @@ -714,13 +783,16 @@ name = "librashader-reflect" version = "0.1.0-beta.10" dependencies = [ "bitflags", + "bytemuck", "librashader-common", "librashader-preprocess", "librashader-presets", "librashader-spirv-cross", "naga", + "rspirv", "rustc-hash", "shaderc", + "spirv-to-dxil", "thiserror", ] @@ -1157,15 +1229,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.6" +version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" dependencies = [ "cfg-if", "libc", "redox_syscall", "smallvec", - "windows-sys 0.42.0", + "windows-sys 0.45.0", ] [[package]] @@ -1174,6 +1246,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -1229,9 +1307,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.50" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" dependencies = [ "unicode-ident", ] @@ -1284,6 +1362,21 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "roxmltree" version = "0.14.1" @@ -1293,6 +1386,17 @@ dependencies = [ "xmlparser", ] +[[package]] +name = "rspirv" +version = "0.11.0+1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1503993b59ca9ae4127365c3293517576d7ce56be9f3d8abb1625c85ddc583ba" +dependencies = [ + "fxhash", + "num-traits", + "spirv", +] + [[package]] name = "rustc-hash" version = "1.1.0" @@ -1391,6 +1495,12 @@ dependencies = [ "roxmltree", ] +[[package]] +name = "shlex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" + [[package]] name = "slotmap" version = "1.0.6" @@ -1435,6 +1545,26 @@ dependencies = [ "num-traits", ] +[[package]] +name = "spirv-to-dxil" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eb2294ffb1db58f59cd21ba6f75bce09c6a3efbce8f66e7e133d79753deccd3" +dependencies = [ + "bitflags", + "spirv-to-dxil-sys", +] + +[[package]] +name = "spirv-to-dxil-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59c84aff6e149d1e6b6dfe178360a9ee5250c92abc691210e5db093a98d8f357" +dependencies = [ + "bindgen", + "cmake", +] + [[package]] name = "strsim" version = "0.10.0" @@ -1516,9 +1646,9 @@ checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5" [[package]] name = "toml_edit" -version = "0.18.0" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "729bfd096e40da9c001f778f5cdecbd2957929a24e10e5883d9392220a751581" +checksum = "56c59d8dd7d0dcbc6428bf7aa2f0e823e26e43b3c9aca15bbc9475d23e5fa12b" dependencies = [ "indexmap", "nom8", @@ -1557,9 +1687,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.83" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1567,9 +1697,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.83" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" dependencies = [ "bumpalo", "log", @@ -1582,9 +1712,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.83" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1592,9 +1722,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.83" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ "proc-macro2", "quote", @@ -1605,9 +1735,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.83" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" [[package]] name = "wayland-client" @@ -1684,9 +1814,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.60" +version = "0.3.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" +checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97" dependencies = [ "js-sys", "wasm-bindgen", @@ -1698,6 +1828,17 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" +[[package]] +name = "which" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +dependencies = [ + "either", + "libc", + "once_cell", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1757,6 +1898,15 @@ dependencies = [ "windows_x86_64_msvc 0.42.1", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.42.1" diff --git a/librashader-reflect/Cargo.toml b/librashader-reflect/Cargo.toml index caa365b..2e57f15 100644 --- a/librashader-reflect/Cargo.toml +++ b/librashader-reflect/Cargo.toml @@ -26,9 +26,12 @@ librashader-presets = { path = "../librashader-presets", version = "0.1.0-beta.1 spirv-to-dxil = { version = "0.2", optional = true } naga = { version = "0.10.0", features = ["glsl-in", "spv-in", "spv-out", "glsl-out", "wgsl-out"], optional = true } +bytemuck = "1.13.0" +rspirv = { version = "0.11.0+1.5.4", optional = true } + [features] -default = ["dxil"] -unstable-naga = [ "naga" ] +default = ["dxil", "unstable-naga"] +unstable-naga = [ "naga", "rspirv" ] standalone = ["shaderc/build-from-source"] dxil = ["spirv-to-dxil"] diff --git a/librashader-reflect/src/back/dxil.rs b/librashader-reflect/src/back/dxil.rs index ff108f1..a0b6a28 100644 --- a/librashader-reflect/src/back/dxil.rs +++ b/librashader-reflect/src/back/dxil.rs @@ -48,6 +48,19 @@ impl CompileShader for WriteSpirV { ) -> Result, ShaderCompileError> { let sm = options.unwrap_or(ShaderModel::ShaderModel6_0); + let config = RuntimeConfig { + runtime_data_cbv: ConstantBufferConfig { + register_space: 0, + base_shader_register: u32::MAX, + }, + push_constant_cbv: ConstantBufferConfig { + register_space: 0, + base_shader_register: 1, + }, + ..RuntimeConfig::default() + }; + + // todo: do we want to allow other entry point names? let vertex = spirv_to_dxil::spirv_to_dxil(&self.vertex, diff --git a/librashader-reflect/src/reflect/naga.rs b/librashader-reflect/src/reflect/naga.rs index 3d463f1..cafbb24 100644 --- a/librashader-reflect/src/reflect/naga.rs +++ b/librashader-reflect/src/reflect/naga.rs @@ -1,6 +1,6 @@ use crate::error::{SemanticsErrorKind, ShaderReflectError}; -use crate::front::naga::NagaCompilation; -use crate::front::shaderc::GlslangCompilation; +use crate::front::NagaCompilation; +use crate::front::GlslangCompilation; use crate::reflect::helper::SemanticErrorBlame; use crate::reflect::semantics::MAX_BINDINGS_COUNT; use naga::front::spv::Options; @@ -105,11 +105,23 @@ impl NagaReflect { } #[cfg(test)] mod test { + use rspirv::dr::Instruction; + use rspirv::spirv::Op; use librashader_preprocess::ShaderSource; #[test] pub fn test_into() { - let result = ShaderSource::load("../test/basic.slang").unwrap(); - let _spirv = crate::front::shaderc::compile_spirv(&result).unwrap(); + let result = ShaderSource::load("../test/slang-shaders/crt/shaders/crt-royale/src/crt-royale-scanlines-horizontal-apply-mask.slang").unwrap(); + let compilation = crate::front::GlslangCompilation::try_from(&result).unwrap(); + + let mut loader = rspirv::dr::Loader::new(); + rspirv::binary::parse_words(&compilation.vertex.as_binary(), &mut loader).unwrap(); + let module = loader.module(); + + let outputs: Vec<&Instruction> = module.types_global_values.iter() + .filter(|i| i.class.opcode == Op::Variable) + .collect(); + + println!("{:#?}", outputs); } } diff --git a/librashader-runtime-d3d12/Cargo.toml b/librashader-runtime-d3d12/Cargo.toml index 2083ebf..fdfaf37 100644 --- a/librashader-runtime-d3d12/Cargo.toml +++ b/librashader-runtime-d3d12/Cargo.toml @@ -33,6 +33,7 @@ features = [ "Win32_Graphics_Direct3D", "Win32_Graphics_Direct3D12", "Win32_Graphics_Direct3D_Fxc", + "Win32_Graphics_Direct3D_Dxc", "Win32_Graphics_Gdi", "Win32_Security", "Win32_System_LibraryLoader", diff --git a/librashader-runtime-d3d12/src/filter_chain.rs b/librashader-runtime-d3d12/src/filter_chain.rs index b605ccf..70df262 100644 --- a/librashader-runtime-d3d12/src/filter_chain.rs +++ b/librashader-runtime-d3d12/src/filter_chain.rs @@ -4,7 +4,7 @@ use crate::descriptor_heap::{D3D12DescriptorHeap, CpuStagingHeap, ResourceWorkHe use crate::samplers::SamplerSet; use crate::luts::LutTexture; use librashader_presets::{ShaderPreset, TextureConfig}; -use librashader_reflect::back::targets::HLSL; +use librashader_reflect::back::targets::DXIL; use librashader_reflect::front::GlslangCompilation; use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; use librashader_runtime::image::{Image, UVDirection}; @@ -15,6 +15,7 @@ use windows::core::Interface; use windows::w; use windows::Win32::Foundation::CloseHandle; use windows::Win32::Graphics::Direct3D12::{ID3D12CommandAllocator, ID3D12CommandQueue, ID3D12Device, ID3D12Fence, ID3D12GraphicsCommandList, D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_DESC, D3D12_COMMAND_QUEUE_FLAG_NONE, D3D12_FENCE_FLAG_NONE, ID3D12DescriptorHeap, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET}; +use windows::Win32::Graphics::Direct3D::Dxc::{CLSID_DxcLibrary, CLSID_DxcValidator, DxcCreateInstance, IDxcLibrary, IDxcValidator}; use windows::Win32::System::Threading::{CreateEventA, ResetEvent, WaitForSingleObject}; use windows::Win32::System::WindowsProgramming::INFINITE; use librashader_common::{ImageFormat, Size, Viewport}; @@ -33,7 +34,7 @@ use crate::quad_render::DrawQuad; use crate::render_target::RenderTarget; use crate::texture::{InputTexture, OutputDescriptor, OutputTexture}; -type ShaderPassMeta = ShaderPassArtifact>; +type ShaderPassMeta = ShaderPassArtifact>; pub struct FilterMutable { pub(crate) passes_enabled: usize, @@ -89,7 +90,7 @@ impl FilterChainD3D12 { ) -> error::Result { let shader_count = preset.shaders.len(); let lut_count = preset.textures.len(); - let (passes, semantics) = HLSL::compile_preset_passes::>( + let (passes, semantics) = DXIL::compile_preset_passes::>( preset.shaders, &preset.textures, ).unwrap(); @@ -344,6 +345,13 @@ impl FilterChainD3D12 { passes: Vec, semantics: &ShaderSemantics,) -> error::Result<(ID3D12DescriptorHeap, ID3D12DescriptorHeap, Vec)> { + let validator: IDxcValidator = unsafe { + DxcCreateInstance(&CLSID_DxcValidator)? + }; + + let dxc: IDxcLibrary = unsafe { + DxcCreateInstance(&CLSID_DxcLibrary)? + }; let mut filters = Vec::new(); let shader_count = passes.len(); @@ -370,19 +378,25 @@ impl FilterChainD3D12 { .zip(work_heaps) .zip(sampler_work_heaps) .enumerate() { + let reflection = reflect.reflect(index, semantics)?; - let hlsl = reflect.compile(None)?; + let dxil = reflect.compile(None)?; + + eprintln!("building pipeline for pass {:?}", index); let graphics_pipeline = D3D12GraphicsPipeline::new(device, - &hlsl, - root_signature, - if let Some(format) = config.get_format_override() { + &dxc, + &validator, + &dxil, + root_signature, + if let Some(format) = config.get_format_override() { format } else if source.format != ImageFormat::Unknown { source.format } else { ImageFormat::R8G8B8A8Unorm - }.into() + }.into(), + &source )?; let uniform_storage = UniformStorage::new( diff --git a/librashader-runtime-d3d12/src/graphics_pipeline.rs b/librashader-runtime-d3d12/src/graphics_pipeline.rs index 64b98a8..ce0f9a8 100644 --- a/librashader-runtime-d3d12/src/graphics_pipeline.rs +++ b/librashader-runtime-d3d12/src/graphics_pipeline.rs @@ -113,30 +113,36 @@ impl D3D12RootSignature { } impl D3D12GraphicsPipeline { pub fn new(device: &ID3D12Device, - shader_assembly: &ShaderCompilerOutput, + library: &IDxcLibrary, + validator: &IDxcValidator, + shader_assembly: &ShaderCompilerOutput, Vec)>, root_signature: &D3D12RootSignature, - render_format: DXGI_FORMAT + render_format: DXGI_FORMAT, + source: &ShaderSource, ) -> error::Result { - let vertex_dxbc = - util::d3d_compile_shader(shader_assembly.vertex.as_bytes(), - b"main\0", b"vs_5_0\0")?; - let fragment_dxbc = - util::d3d_compile_shader(shader_assembly.fragment.as_bytes(), b"main\0", - b"ps_5_0\0")?; + if shader_assembly.vertex.requires_runtime_data() { + panic!("vertex needs rt data??") + } + if shader_assembly.fragment.requires_runtime_data() { + panic!("fragment needs rt data??") + } + let vertex_dxil = + util::dxc_validate_shader(library, validator, &shader_assembly.vertex)?; + let fragment_dxil = + util::dxc_validate_shader(library, validator, &shader_assembly.fragment)?; let input_element = DrawQuad::get_spirv_cross_vbo_desc(); - let pipeline_state: ID3D12PipelineState = unsafe { let pipeline_desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { pRootSignature: windows::core::ManuallyDrop::new(&root_signature.handle), VS: D3D12_SHADER_BYTECODE { - pShaderBytecode: vertex_dxbc.GetBufferPointer(), - BytecodeLength: vertex_dxbc.GetBufferSize(), + pShaderBytecode: vertex_dxil.GetBufferPointer(), + BytecodeLength: vertex_dxil.GetBufferSize(), }, PS: D3D12_SHADER_BYTECODE { - pShaderBytecode: fragment_dxbc.GetBufferPointer(), - BytecodeLength: fragment_dxbc.GetBufferSize(), + pShaderBytecode: fragment_dxil.GetBufferPointer(), + BytecodeLength: fragment_dxil.GetBufferSize(), }, StreamOutput: Default::default(), BlendState: D3D12_BLEND_DESC { diff --git a/librashader-runtime-d3d12/src/mipmap.rs b/librashader-runtime-d3d12/src/mipmap.rs index ca2d6e3..7a1f976 100644 --- a/librashader-runtime-d3d12/src/mipmap.rs +++ b/librashader-runtime-d3d12/src/mipmap.rs @@ -6,7 +6,7 @@ use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT; use librashader_common::Size; use crate::{error, util}; use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, ResourceWorkHeap}; -use crate::util::d3d_compile_shader; +use crate::util::fxc_compile_shader; use bytemuck::{Zeroable, Pod}; use librashader_runtime::scaling::MipmapSize; @@ -102,7 +102,7 @@ impl <'a> MipmapGenContext<'a> { impl D3D12MipmapGen { pub fn new(device: &ID3D12Device) -> error::Result { unsafe { - let blob = d3d_compile_shader(GENERATE_MIPS_SRC, b"main\0", b"cs_5_1\0").unwrap(); + let blob = fxc_compile_shader(GENERATE_MIPS_SRC, b"main\0", b"cs_5_1\0").unwrap(); let blob = std::slice::from_raw_parts(blob.GetBufferPointer().cast(), blob.GetBufferSize()); let root_signature: ID3D12RootSignature = device.CreateRootSignature(0, blob).unwrap(); diff --git a/librashader-runtime-d3d12/src/util.rs b/librashader-runtime-d3d12/src/util.rs index 21059d3..9bf4d27 100644 --- a/librashader-runtime-d3d12/src/util.rs +++ b/librashader-runtime-d3d12/src/util.rs @@ -1,10 +1,12 @@ +use std::ffi::CStr; use std::mem::ManuallyDrop; use std::u64; use crate::error; -use windows::core::PCSTR; +use windows::core::{Interface, PCSTR}; use windows::Win32::Graphics::Direct3D::Fxc::{D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_SKIP_OPTIMIZATION}; use windows::Win32::Graphics::Direct3D::ID3DBlob; use windows::Win32::Graphics::Direct3D12::{ID3D12Device, D3D12_FEATURE_DATA_FORMAT_SUPPORT, D3D12_FEATURE_FORMAT_SUPPORT, ID3D12GraphicsCommandList, ID3D12Resource, D3D12_RESOURCE_BARRIER, D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, D3D12_RESOURCE_BARRIER_FLAG_NONE, D3D12_RESOURCE_BARRIER_0, D3D12_RESOURCE_TRANSITION_BARRIER, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, D3D12_RESOURCE_STATES, D3D12_PLACED_SUBRESOURCE_FOOTPRINT, D3D12_MEMCPY_DEST, D3D12_SUBRESOURCE_DATA, D3D12_RESOURCE_DIMENSION_BUFFER, D3D12_TEXTURE_COPY_LOCATION, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, D3D12_TEXTURE_COPY_LOCATION_0, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; +use windows::Win32::Graphics::Direct3D::Dxc::{DXC_CP, DxcValidatorFlags_Default, DxcValidatorFlags_InPlaceEdit, DxcValidatorFlags_ModuleOnly, DxcValidatorFlags_ValidMask, IDxcBlob, IDxcBlobUtf8, IDxcLibrary, IDxcValidator}; use windows::Win32::Graphics::Dxgi::Common::*; use crate::error::assume_d3d12_init; @@ -115,7 +117,7 @@ pub fn d3d12_get_closest_format( DXGI_FORMAT_UNKNOWN } -pub fn d3d_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error::Result { +pub fn fxc_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error::Result { // todo: compile with dxc unsafe { let mut blob = None; @@ -143,6 +145,37 @@ pub fn d3d_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error: } } +pub fn dxc_validate_shader(library: &IDxcLibrary, validator: &IDxcValidator, source: &[u8]) -> error::Result { + // todo: compile with dxc + // let mut source = source.to_vec(); + + let blob = unsafe { + library.CreateBlobWithEncodingOnHeapCopy( + source.as_ptr().cast(), + source.len() as u32, + DXC_CP(0) + )? + }; + + unsafe { + let result = validator + .Validate(&blob, DxcValidatorFlags_InPlaceEdit).unwrap(); + + if let Ok(buf) = result.GetErrorBuffer() { + unsafe { + let buf: IDxcBlobUtf8 = buf.cast().unwrap(); + let buf = std::slice::from_raw_parts(buf.GetBufferPointer() + .cast(), buf.GetBufferSize()); + let str = std::str::from_utf8_unchecked(buf); + if str.len() != 0 { + eprintln!("{}", str); + } + } + } + Ok(IDxcBlob::from(blob)) + } +} + #[inline(always)] pub fn d3d12_resource_transition(cmd: &ID3D12GraphicsCommandList, resource: &ID3D12Resource,