From 35145615bf4a62eca2f5f7eb9a157135ca84a4ad Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sun, 18 Jul 2021 11:36:57 -0400 Subject: [PATCH] Update naga, use binding map in DX12 --- Cargo.lock | 2 +- wgpu-core/Cargo.toml | 2 +- wgpu-hal/Cargo.toml | 4 +-- wgpu-hal/src/dx12/device.rs | 64 ++++++++++++++++++++++++++++++------- wgpu/Cargo.toml | 4 +-- 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 460f49db0..3b550a0f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1051,7 +1051,7 @@ dependencies = [ [[package]] name = "naga" version = "0.5.0" -source = "git+https://github.com/gfx-rs/naga?rev=8f71a36#8f71a368eff3a18fd348ab6193cb183df0f49f95" +source = "git+https://github.com/gfx-rs/naga?rev=feee1a2#feee1a2edbcae2a9ad7d9cb39a71ae4e0a5578e9" dependencies = [ "bit-set", "bitflags", diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index c9f408588..04d1158b4 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -36,7 +36,7 @@ thiserror = "1" [dependencies.naga] git = "https://github.com/gfx-rs/naga" -rev = "8f71a36" +rev = "feee1a2" features = ["wgsl-in"] [dependencies.wgt] diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index d30363600..f555e65fd 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -65,11 +65,11 @@ core-graphics-types = "0.1" [dependencies.naga] git = "https://github.com/gfx-rs/naga" -rev = "8f71a36" +rev = "feee1a2" [dev-dependencies.naga] git = "https://github.com/gfx-rs/naga" -rev = "8f71a36" +rev = "feee1a2" features = ["wgsl-in"] [dev-dependencies] diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 731c8a3de..5c5567091 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -529,7 +529,7 @@ impl super::Device { //TODO: reuse the writer let mut source = String::new(); let mut writer = hlsl::Writer::new(&mut source, &layout.naga_options); - let _reflection_info = writer + let reflection_info = writer .write(module, &stage.module.naga.info) .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("HLSL: {:?}", e)))?; @@ -538,7 +538,15 @@ impl super::Device { naga_stage.to_hlsl_str(), layout.naga_options.shader_model.to_str() ); - let raw_ep = ffi::CString::new(stage.entry_point).unwrap(); + let ep_index = module + .entry_points + .iter() + .position(|ep| ep.name == stage.entry_point) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?; + let raw_ep = reflection_info.entry_point_names[ep_index] + .as_ref() + .map(|name| ffi::CString::new(name.as_str()).unwrap()) + .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{}", e)))?; let mut shader_data = native::Blob::null(); let mut error = native::Blob::null(); @@ -985,6 +993,7 @@ impl crate::Device for super::Device { &self, desc: &crate::PipelineLayoutDescriptor, ) -> Result { + use naga::back::hlsl; // Pipeline layouts are implemented as RootSignature for D3D12. // // Push Constants are implemented as root constants. @@ -1012,6 +1021,7 @@ impl crate::Device for super::Device { //Note: lower bind group indices are put futher down the root signature. See: // https://microsoft.github.io/DirectX-Specs/d3d/ResourceBinding.html#binding-model + let mut binding_map = hlsl::BindingMap::default(); let root_constants: &[()] = &[]; // Number of elements in the root signature. @@ -1073,16 +1083,25 @@ impl crate::Device for super::Device { | wgt::BindingType::Sampler { .. } => continue, ref other => conv::map_binding_type(other), }; + let register = (ranges.len() - range_base) as u32; + binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + hlsl::BindTarget { + space: space as u8, + register, + }, + ); ranges.push(native::DescriptorRange::new( range_ty, entry.count.map_or(1, |count| count.get()), - native::Binding { - register: entry.binding, - space, - }, + native::Binding { register, space }, d3d12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, )); } + let base_view_register = ranges.len() - range_base; if ranges.len() > range_base { parameters.push(native::RootParameter::descriptor_table( conv::map_visibility(visibility_view_static), @@ -1098,13 +1117,21 @@ impl crate::Device for super::Device { wgt::BindingType::Sampler { .. } => native::DescriptorRangeType::Sampler, _ => continue, }; + let register = (ranges.len() - range_base) as u32; + binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + hlsl::BindTarget { + space: space as u8, + register, + }, + ); ranges.push(native::DescriptorRange::new( range_ty, entry.count.map_or(1, |count| count.get()), - native::Binding { - register: entry.binding, - space, - }, + native::Binding { register, space }, d3d12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, )); } @@ -1117,6 +1144,7 @@ impl crate::Device for super::Device { } // Root (dynamic) descriptor tables + let parameter_base = parameters.len(); let dynamic_buffers_visibility = conv::map_visibility(visibility_view_dynamic); for entry in bgl.entries.iter() { let buffer_ty = match entry.ty { @@ -1127,6 +1155,16 @@ impl crate::Device for super::Device { } => ty, _ => continue, }; + binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + hlsl::BindTarget { + space: space as u8, + register: (base_view_register + parameters.len() - parameter_base) as u32, + }, + ); let binding = native::Binding { register: entry.binding, space, @@ -1195,8 +1233,10 @@ impl crate::Device for super::Device { raw, bind_group_infos, total_root_elements: total_parameters as super::RootIndex, - naga_options: naga::back::hlsl::Options { - shader_model: naga::back::hlsl::ShaderModel::V5_1, + naga_options: hlsl::Options { + shader_model: hlsl::ShaderModel::V5_1, + binding_map, + fake_missing_bindings: false, }, }) } diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 56d751d91..3721443dd 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -73,13 +73,13 @@ env_logger = "0.8" [dependencies.naga] git = "https://github.com/gfx-rs/naga" -rev = "8f71a36" +rev = "feee1a2" optional = true # used to test all the example shaders [dev-dependencies.naga] git = "https://github.com/gfx-rs/naga" -rev = "8f71a36" +rev = "feee1a2" features = ["wgsl-in"] [[example]]