d3d12: initial implementation of spirv-to-dxil

This commit is contained in:
chyyran 2023-02-05 00:55:46 -05:00
parent aa87e13201
commit f677666900
9 changed files with 289 additions and 57 deletions

202
Cargo.lock generated
View file

@ -58,6 +58,28 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 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]] [[package]]
name = "bit-set" name = "bit-set"
version = "0.5.3" version = "0.5.3"
@ -138,19 +160,39 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.78" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
dependencies = [ dependencies = [
"jobserver", "jobserver",
] ]
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 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]] [[package]]
name = "cmake" name = "cmake"
version = "0.1.49" version = "0.1.49"
@ -359,10 +401,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "encoding_rs" name = "either"
version = "0.8.31" version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"cfg-if", "cfg-if",
] ]
@ -463,6 +511,15 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "gfx-maths" name = "gfx-maths"
version = "0.2.8" version = "0.2.8"
@ -537,6 +594,12 @@ dependencies = [
"cmake", "cmake",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
@ -611,9 +674,9 @@ checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.60" version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730"
dependencies = [ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
@ -630,6 +693,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.139" version = "0.2.139"
@ -714,13 +783,16 @@ name = "librashader-reflect"
version = "0.1.0-beta.10" version = "0.1.0-beta.10"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"bytemuck",
"librashader-common", "librashader-common",
"librashader-preprocess", "librashader-preprocess",
"librashader-presets", "librashader-presets",
"librashader-spirv-cross", "librashader-spirv-cross",
"naga", "naga",
"rspirv",
"rustc-hash", "rustc-hash",
"shaderc", "shaderc",
"spirv-to-dxil",
"thiserror", "thiserror",
] ]
@ -1157,15 +1229,15 @@ dependencies = [
[[package]] [[package]]
name = "parking_lot_core" name = "parking_lot_core"
version = "0.9.6" version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"windows-sys 0.42.0", "windows-sys 0.45.0",
] ]
[[package]] [[package]]
@ -1174,6 +1246,12 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba"
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.2.0" version = "2.2.0"
@ -1229,9 +1307,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.50" version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -1284,6 +1362,21 @@ dependencies = [
"bitflags", "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]] [[package]]
name = "roxmltree" name = "roxmltree"
version = "0.14.1" version = "0.14.1"
@ -1293,6 +1386,17 @@ dependencies = [
"xmlparser", "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]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "1.1.0" version = "1.1.0"
@ -1391,6 +1495,12 @@ dependencies = [
"roxmltree", "roxmltree",
] ]
[[package]]
name = "shlex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]] [[package]]
name = "slotmap" name = "slotmap"
version = "1.0.6" version = "1.0.6"
@ -1435,6 +1545,26 @@ dependencies = [
"num-traits", "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]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@ -1516,9 +1646,9 @@ checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5"
[[package]] [[package]]
name = "toml_edit" name = "toml_edit"
version = "0.18.0" version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "729bfd096e40da9c001f778f5cdecbd2957929a24e10e5883d9392220a751581" checksum = "56c59d8dd7d0dcbc6428bf7aa2f0e823e26e43b3c9aca15bbc9475d23e5fa12b"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"nom8", "nom8",
@ -1557,9 +1687,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.83" version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"wasm-bindgen-macro", "wasm-bindgen-macro",
@ -1567,9 +1697,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.83" version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"log", "log",
@ -1582,9 +1712,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.83" version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@ -1592,9 +1722,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.83" version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1605,9 +1735,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.83" version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]] [[package]]
name = "wayland-client" name = "wayland-client"
@ -1684,9 +1814,9 @@ dependencies = [
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.60" version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
@ -1698,6 +1828,17 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" 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]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -1757,6 +1898,15 @@ dependencies = [
"windows_x86_64_msvc 0.42.1", "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]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.42.1" version = "0.42.1"

View file

@ -26,9 +26,12 @@ librashader-presets = { path = "../librashader-presets", version = "0.1.0-beta.1
spirv-to-dxil = { version = "0.2", optional = true } 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 } 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] [features]
default = ["dxil"] default = ["dxil", "unstable-naga"]
unstable-naga = [ "naga" ] unstable-naga = [ "naga", "rspirv" ]
standalone = ["shaderc/build-from-source"] standalone = ["shaderc/build-from-source"]
dxil = ["spirv-to-dxil"] dxil = ["spirv-to-dxil"]

View file

@ -48,6 +48,19 @@ impl CompileShader<DXIL> for WriteSpirV {
) -> Result<ShaderCompilerOutput<DxilObject, Self::Context>, ShaderCompileError> { ) -> Result<ShaderCompilerOutput<DxilObject, Self::Context>, ShaderCompileError> {
let sm = options.unwrap_or(ShaderModel::ShaderModel6_0); 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? // todo: do we want to allow other entry point names?
let vertex = let vertex =
spirv_to_dxil::spirv_to_dxil(&self.vertex, spirv_to_dxil::spirv_to_dxil(&self.vertex,

View file

@ -1,6 +1,6 @@
use crate::error::{SemanticsErrorKind, ShaderReflectError}; use crate::error::{SemanticsErrorKind, ShaderReflectError};
use crate::front::naga::NagaCompilation; use crate::front::NagaCompilation;
use crate::front::shaderc::GlslangCompilation; use crate::front::GlslangCompilation;
use crate::reflect::helper::SemanticErrorBlame; use crate::reflect::helper::SemanticErrorBlame;
use crate::reflect::semantics::MAX_BINDINGS_COUNT; use crate::reflect::semantics::MAX_BINDINGS_COUNT;
use naga::front::spv::Options; use naga::front::spv::Options;
@ -105,11 +105,23 @@ impl NagaReflect {
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use rspirv::dr::Instruction;
use rspirv::spirv::Op;
use librashader_preprocess::ShaderSource; use librashader_preprocess::ShaderSource;
#[test] #[test]
pub fn test_into() { pub fn test_into() {
let result = ShaderSource::load("../test/basic.slang").unwrap(); let result = ShaderSource::load("../test/slang-shaders/crt/shaders/crt-royale/src/crt-royale-scanlines-horizontal-apply-mask.slang").unwrap();
let _spirv = crate::front::shaderc::compile_spirv(&result).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);
} }
} }

View file

@ -33,6 +33,7 @@ features = [
"Win32_Graphics_Direct3D", "Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D12", "Win32_Graphics_Direct3D12",
"Win32_Graphics_Direct3D_Fxc", "Win32_Graphics_Direct3D_Fxc",
"Win32_Graphics_Direct3D_Dxc",
"Win32_Graphics_Gdi", "Win32_Graphics_Gdi",
"Win32_Security", "Win32_Security",
"Win32_System_LibraryLoader", "Win32_System_LibraryLoader",

View file

@ -4,7 +4,7 @@ use crate::descriptor_heap::{D3D12DescriptorHeap, CpuStagingHeap, ResourceWorkHe
use crate::samplers::SamplerSet; use crate::samplers::SamplerSet;
use crate::luts::LutTexture; use crate::luts::LutTexture;
use librashader_presets::{ShaderPreset, TextureConfig}; 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::front::GlslangCompilation;
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_runtime::image::{Image, UVDirection}; use librashader_runtime::image::{Image, UVDirection};
@ -15,6 +15,7 @@ use windows::core::Interface;
use windows::w; use windows::w;
use windows::Win32::Foundation::CloseHandle; 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::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::Threading::{CreateEventA, ResetEvent, WaitForSingleObject};
use windows::Win32::System::WindowsProgramming::INFINITE; use windows::Win32::System::WindowsProgramming::INFINITE;
use librashader_common::{ImageFormat, Size, Viewport}; use librashader_common::{ImageFormat, Size, Viewport};
@ -33,7 +34,7 @@ use crate::quad_render::DrawQuad;
use crate::render_target::RenderTarget; use crate::render_target::RenderTarget;
use crate::texture::{InputTexture, OutputDescriptor, OutputTexture}; use crate::texture::{InputTexture, OutputDescriptor, OutputTexture};
type ShaderPassMeta = ShaderPassArtifact<impl CompileReflectShader<HLSL, GlslangCompilation>>; type ShaderPassMeta = ShaderPassArtifact<impl CompileReflectShader<DXIL, GlslangCompilation>>;
pub struct FilterMutable { pub struct FilterMutable {
pub(crate) passes_enabled: usize, pub(crate) passes_enabled: usize,
@ -89,7 +90,7 @@ impl FilterChainD3D12 {
) -> error::Result<FilterChainD3D12> { ) -> error::Result<FilterChainD3D12> {
let shader_count = preset.shaders.len(); let shader_count = preset.shaders.len();
let lut_count = preset.textures.len(); let lut_count = preset.textures.len();
let (passes, semantics) = HLSL::compile_preset_passes::<GlslangCompilation, Box<dyn Error>>( let (passes, semantics) = DXIL::compile_preset_passes::<GlslangCompilation, Box<dyn Error>>(
preset.shaders, preset.shaders,
&preset.textures, &preset.textures,
).unwrap(); ).unwrap();
@ -344,6 +345,13 @@ impl FilterChainD3D12 {
passes: Vec<ShaderPassMeta>, passes: Vec<ShaderPassMeta>,
semantics: &ShaderSemantics,) semantics: &ShaderSemantics,)
-> error::Result<(ID3D12DescriptorHeap, ID3D12DescriptorHeap, Vec<FilterPass>)> { -> error::Result<(ID3D12DescriptorHeap, ID3D12DescriptorHeap, Vec<FilterPass>)> {
let validator: IDxcValidator = unsafe {
DxcCreateInstance(&CLSID_DxcValidator)?
};
let dxc: IDxcLibrary = unsafe {
DxcCreateInstance(&CLSID_DxcLibrary)?
};
let mut filters = Vec::new(); let mut filters = Vec::new();
let shader_count = passes.len(); let shader_count = passes.len();
@ -370,19 +378,25 @@ impl FilterChainD3D12 {
.zip(work_heaps) .zip(work_heaps)
.zip(sampler_work_heaps) .zip(sampler_work_heaps)
.enumerate() { .enumerate() {
let reflection = reflect.reflect(index, semantics)?; 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, let graphics_pipeline = D3D12GraphicsPipeline::new(device,
&hlsl, &dxc,
root_signature, &validator,
if let Some(format) = config.get_format_override() { &dxil,
root_signature,
if let Some(format) = config.get_format_override() {
format format
} else if source.format != ImageFormat::Unknown { } else if source.format != ImageFormat::Unknown {
source.format source.format
} else { } else {
ImageFormat::R8G8B8A8Unorm ImageFormat::R8G8B8A8Unorm
}.into() }.into(),
&source
)?; )?;
let uniform_storage = UniformStorage::new( let uniform_storage = UniformStorage::new(

View file

@ -113,30 +113,36 @@ impl D3D12RootSignature {
} }
impl D3D12GraphicsPipeline { impl D3D12GraphicsPipeline {
pub fn new(device: &ID3D12Device, pub fn new(device: &ID3D12Device,
shader_assembly: &ShaderCompilerOutput<String, CrossHlslContext>, library: &IDxcLibrary,
validator: &IDxcValidator,
shader_assembly: &ShaderCompilerOutput<DxilObject, (Vec<u32>, Vec<u32>)>,
root_signature: &D3D12RootSignature, root_signature: &D3D12RootSignature,
render_format: DXGI_FORMAT render_format: DXGI_FORMAT,
source: &ShaderSource,
) -> error::Result<D3D12GraphicsPipeline> { ) -> error::Result<D3D12GraphicsPipeline> {
let vertex_dxbc = if shader_assembly.vertex.requires_runtime_data() {
util::d3d_compile_shader(shader_assembly.vertex.as_bytes(), panic!("vertex needs rt data??")
b"main\0", b"vs_5_0\0")?; }
let fragment_dxbc = if shader_assembly.fragment.requires_runtime_data() {
util::d3d_compile_shader(shader_assembly.fragment.as_bytes(), b"main\0", panic!("fragment needs rt data??")
b"ps_5_0\0")?; }
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 input_element = DrawQuad::get_spirv_cross_vbo_desc();
let pipeline_state: ID3D12PipelineState = unsafe { let pipeline_state: ID3D12PipelineState = unsafe {
let pipeline_desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { let pipeline_desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC {
pRootSignature: windows::core::ManuallyDrop::new(&root_signature.handle), pRootSignature: windows::core::ManuallyDrop::new(&root_signature.handle),
VS: D3D12_SHADER_BYTECODE { VS: D3D12_SHADER_BYTECODE {
pShaderBytecode: vertex_dxbc.GetBufferPointer(), pShaderBytecode: vertex_dxil.GetBufferPointer(),
BytecodeLength: vertex_dxbc.GetBufferSize(), BytecodeLength: vertex_dxil.GetBufferSize(),
}, },
PS: D3D12_SHADER_BYTECODE { PS: D3D12_SHADER_BYTECODE {
pShaderBytecode: fragment_dxbc.GetBufferPointer(), pShaderBytecode: fragment_dxil.GetBufferPointer(),
BytecodeLength: fragment_dxbc.GetBufferSize(), BytecodeLength: fragment_dxil.GetBufferSize(),
}, },
StreamOutput: Default::default(), StreamOutput: Default::default(),
BlendState: D3D12_BLEND_DESC { BlendState: D3D12_BLEND_DESC {

View file

@ -6,7 +6,7 @@ use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
use librashader_common::Size; use librashader_common::Size;
use crate::{error, util}; use crate::{error, util};
use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, ResourceWorkHeap}; use crate::descriptor_heap::{D3D12DescriptorHeap, D3D12DescriptorHeapSlot, ResourceWorkHeap};
use crate::util::d3d_compile_shader; use crate::util::fxc_compile_shader;
use bytemuck::{Zeroable, Pod}; use bytemuck::{Zeroable, Pod};
use librashader_runtime::scaling::MipmapSize; use librashader_runtime::scaling::MipmapSize;
@ -102,7 +102,7 @@ impl <'a> MipmapGenContext<'a> {
impl D3D12MipmapGen { impl D3D12MipmapGen {
pub fn new(device: &ID3D12Device) -> error::Result<D3D12MipmapGen> { pub fn new(device: &ID3D12Device) -> error::Result<D3D12MipmapGen> {
unsafe { 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 blob = std::slice::from_raw_parts(blob.GetBufferPointer().cast(), blob.GetBufferSize());
let root_signature: ID3D12RootSignature = device.CreateRootSignature(0, blob).unwrap(); let root_signature: ID3D12RootSignature = device.CreateRootSignature(0, blob).unwrap();

View file

@ -1,10 +1,12 @@
use std::ffi::CStr;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::u64; use std::u64;
use crate::error; 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::Fxc::{D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_SKIP_OPTIMIZATION};
use windows::Win32::Graphics::Direct3D::ID3DBlob; 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::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 windows::Win32::Graphics::Dxgi::Common::*;
use crate::error::assume_d3d12_init; use crate::error::assume_d3d12_init;
@ -115,7 +117,7 @@ pub fn d3d12_get_closest_format(
DXGI_FORMAT_UNKNOWN DXGI_FORMAT_UNKNOWN
} }
pub fn d3d_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error::Result<ID3DBlob> { pub fn fxc_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error::Result<ID3DBlob> {
// todo: compile with dxc // todo: compile with dxc
unsafe { unsafe {
let mut blob = None; 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<IDxcBlob> {
// 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)] #[inline(always)]
pub fn d3d12_resource_transition(cmd: &ID3D12GraphicsCommandList, pub fn d3d12_resource_transition(cmd: &ID3D12GraphicsCommandList,
resource: &ID3D12Resource, resource: &ID3D12Resource,