diff --git a/CHANGELOG.md b/CHANGELOG.md index 5616ff55a..855ebdb8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -153,6 +153,10 @@ By @sagudev in [#10307](https://github.com/gfx-rs/wgpu/pull/10307). - Add `wgpu_hal::vulkan::AccelerationStructure::raw_handle`, which returns the underlying `VkAccelerationStructureKHR`. Use it with `Adapter::open_with_callback` to record acceleration structure commands from extensions wgpu doesn't support, such as [VK_NV_cluster_acceleration_structure](https://registry.khronos.org/vulkan/specs/latest/man/html/VK_NV_cluster_acceleration_structure.html). Mark the result with `CommandEncoder::mark_acceleration_structures_built`. By @stuartparmenter in [#10187](https://github.com/gfx-rs/wgpu/pull/10187). - Add `wgpu_hal::vulkan::Surface::set_next_swapchain_create_chain` and `wgpu_hal::vulkan::Queue::set_next_submit_chain`. The first attaches a caller-provided `pNext` chain to the `VkSwapchainCreateInfoKHR` of the surface's next configuration. The second attaches one to the `VkSubmitInfo` of the queue's next submission. Together with `set_next_present_chain` and the existing raw-handle accessors, this makes [VK_NV_low_latency2](https://registry.khronos.org/vulkan/specs/latest/man/html/VK_NV_low_latency2.html) usable on a wgpu swapchain. That extension is the Vulkan interface for NVIDIA Reflex. By @stuartparmenter in [#10095](https://github.com/gfx-rs/wgpu/pull/10095). +#### DX12 + +- Support `Features::SHADER_EARLY_DEPTH_TEST` on DX12. Naga's HLSL backend now emits `[earlydepthstencil]` for `@early_depth_test(force)`, `SV_DepthGreaterEqual`/`SV_DepthLessEqual` for `@early_depth_test(greater_equal/less_equal)` on pipelines that are not multisampled, and discards the depth output for `@early_depth_test(unchanged)`. Callers of Naga's HLSL backend can set the new `naga::back::hlsl::PipelineOptions::multisampled`. By @DouglasDwyer in [#10624](https://github.com/gfx-rs/wgpu/pull/10624). + #### Metal - Support `Features::SHADER_EARLY_DEPTH_TEST` on Metal. Naga's MSL backend now emits `[[early_fragment_tests]]` for `@early_depth_test(force)`, `depth(greater)`/`depth(less)` for `@early_depth_test(greater_equal/less_equal)`, and discards the depth output for `@early_depth_test(unchanged)` and `@early_depth_test(force)`. By @DouglasDwyer in [#10625](https://github.com/gfx-rs/wgpu/pull/10625). diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index ad8a5611e..b6dd4ce85 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -654,6 +654,9 @@ pub struct PipelineOptions { /// If `None`, all entry points will be written. If `Some` and the entry /// point is not found, an error will be thrown while writing. pub entry_point: Option<(ir::ShaderStage, String)>, + + /// Whether the pipeline renders to multisampled attachments. + pub multisampled: bool, } #[derive(Error, Debug)] @@ -753,6 +756,8 @@ pub struct Writer<'a, W> { pipeline_options: &'a PipelineOptions, /// Information about entry point arguments and result types. entry_point_io: crate::FastHashMap, + /// The early depth test of the fragment entry point whose interface is being written. + early_depth_test: Option, /// Set of expressions that have associated temporary variables named_expressions: crate::NamedExpressions, wrapped: Wrapped, @@ -798,7 +803,7 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { // No CULL_DISTANCE | Caps::STORAGE_TEXTURE_16BIT_NORM_FORMATS | Caps::MULTIVIEW - // No EARLY_DEPTH_TEST + | Caps::EARLY_DEPTH_TEST | Caps::MULTISAMPLED_SHADING | Caps::RAY_QUERY | Caps::DUAL_SOURCE_BLENDING diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index af97f538e..8d04750f5 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -160,6 +160,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { options, pipeline_options, entry_point_io: crate::FastHashMap::default(), + early_depth_test: None, named_expressions: crate::NamedExpressions::default(), wrapped: super::Wrapped::default(), written_committed_intersection: false, @@ -373,6 +374,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { writeln!(self.out)?; } + let ep_range = get_entry_points(module, self.pipeline_options.entry_point.as_ref()) + .map_err(|(stage, name)| Error::EntryPointNotFound(stage, name))?; + // Save all entry point output types let ep_results = module .entry_points @@ -402,13 +406,16 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { } }); - self.write_struct( - module, - handle, - members, - span, - ep_result.map(|r| (r.0, Io::Output)), - )?; + let early_depth_test = shared_early_depth_test(module, ep_range.clone(), handle); + self.with_early_depth_test(early_depth_test, |this| { + this.write_struct( + module, + handle, + members, + span, + ep_result.map(|r| (r.0, Io::Output)), + ) + })?; writeln!(self.out)?; } } @@ -442,9 +449,6 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { writeln!(self.out)?; } - let ep_range = get_entry_points(module, self.pipeline_options.entry_point.as_ref()) - .map_err(|(stage, name)| Error::EntryPointNotFound(stage, name))?; - // Write all entry points wrapped structs for index in ep_range.clone() { let ep = &module.entry_points[index]; @@ -552,6 +556,11 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { num_threads[0], num_threads[1], num_threads[2] )?; } + if ep.stage == ShaderStage::Fragment + && ep.early_depth_test == Some(crate::EarlyDepthTest::Force) + { + writeln!(attribute_string, "[earlydepthstencil]")?; + } if let Some(ref info) = ep.mesh_info { let topology_str = match info.topology { crate::MeshOutputTopology::Points => unreachable!(), @@ -578,8 +587,15 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { fn write_modifier(&mut self, binding: &crate::Binding) -> BackendResult { match *binding { - crate::Binding::BuiltIn(crate::BuiltIn::Position { invariant: true }) => { - write!(self.out, "precise ")?; + crate::Binding::BuiltIn(crate::BuiltIn::Position { invariant }) => { + if invariant { + write!(self.out, "precise ")?; + } + if self.conservative_depth().is_some() { + // This is only valid for shaders that are not multisampled, which + // `conservative_depth` ensures. + write!(self.out, "noperspective centroid ")?; + } } crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { perspective: false }) => { write!(self.out, "noperspective ")?; @@ -607,6 +623,37 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { Ok(()) } + /// Gets the conservative depth that the fragment shader being written promises, if it is used. + /// + /// Fragment shaders expect the position input to be at the center of the pixel. DXC only + /// accepts `SV_DepthGreaterEqual` and `SV_DepthLessEqual` when the `SV_Position` input is + /// declared `noperspective centroid`. That is the pixel center when there is one sample per + /// pixel, but under MSAA it can differ, which would change the position that the shader sees. + /// Conservative depth is therefore only used for pipelines that are not multisampled. + /// Multisampled pipelines use an unrestricted depth output and leave the position input alone. + const fn conservative_depth(&self) -> Option { + match self.early_depth_test { + Some(crate::EarlyDepthTest::Allow { + conservative: + conservative @ (crate::ConservativeDepth::GreaterEqual + | crate::ConservativeDepth::LessEqual), + }) if !self.pipeline_options.multisampled => Some(conservative), + _ => None, + } + } + + /// Runs `write` with [`Self::early_depth_test`] set to `early_depth_test`. + fn with_early_depth_test( + &mut self, + early_depth_test: Option, + write: impl FnOnce(&mut Self) -> T, + ) -> T { + self.early_depth_test = early_depth_test; + let result = write(self); + self.early_depth_test = None; + result + } + //TODO: we could force fragment outputs to always go through `entry_point_io.output` path // if they are struct, so that the `stage` argument here could be omitted. pub(super) fn write_semantic( @@ -625,6 +672,16 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { )); } if let Some(builtin_str) = builtin.to_hlsl_str()? { + let builtin_str = match (builtin, self.conservative_depth()) { + ( + crate::BuiltIn::FragDepth, + Some(crate::ConservativeDepth::GreaterEqual), + ) => "SV_DepthGreaterEqual", + (crate::BuiltIn::FragDepth, Some(crate::ConservativeDepth::LessEqual)) => { + "SV_DepthLessEqual" + } + _ => builtin_str, + }; write!(self.out, " : {builtin_str}")?; } false @@ -785,11 +842,12 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { fn write_ep_output_struct( &mut self, module: &Module, + entry_point: &crate::EntryPoint, result: &crate::FunctionResult, - stage: ShaderStage, entry_point_name: &str, frag_ep: Option<&FragmentEntryPoint<'_>>, ) -> Result { + let stage = entry_point.stage; let struct_name = format!("{stage:?}Output_{entry_point_name}"); let empty = []; @@ -843,6 +901,10 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { } } + if discards_frag_depth(entry_point) && is_frag_depth(member.binding.as_ref()) { + continue; + } + let member_name = self.namer.call_or(&member.name, "member"); fake_members.push(EpStructMember { name: member_name, @@ -875,13 +937,20 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { .iter() .any(|arg| is_subgroup_builtin_binding(&arg.binding))) { - Some(self.write_ep_input_struct(module, func, stage, ep_name)?) + Some(self.with_early_depth_test(ep.early_depth_test, |this| { + this.write_ep_input_struct(module, func, stage, ep_name) + })?) } else { None }, output: match func.result { - Some(ref fr) if fr.binding.is_none() && stage == ShaderStage::Vertex => { - Some(self.write_ep_output_struct(module, fr, stage, ep_name, frag_ep)?) + Some(ref fr) + if fr.binding.is_none() + && (stage == ShaderStage::Vertex + || (discards_frag_depth(ep) + && !only_writes_discarded_depth(module, ep))) => + { + Some(self.write_ep_output_struct(module, ep, fr, ep_name, frag_ep)?) } _ => None, }, @@ -1647,7 +1716,12 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { write!(self.out, "{header}")?; } - if let Some(ref result) = func.result { + let result = func + .result + .as_ref() + .filter(|_| !ep.is_some_and(|ep| only_writes_discarded_depth(module, ep))); + + if let Some(result) = result { // Write typedef if return type is an array let array_return_type = match module.types[result.ty].inner { TypeInner::Array { base, size, .. } => { @@ -1805,9 +1879,11 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { // Write semantic if it present if let back::FunctionType::EntryPoint(index) = func_ctx.ty { - let stage = module.entry_points[index as usize].stage; - if let Some(crate::FunctionResult { ref binding, .. }) = func.result { - self.write_semantic(binding, Some((stage, Io::Output)))?; + let entry_point = &module.entry_points[index as usize]; + if let Some(result) = result { + self.with_early_depth_test(entry_point.early_depth_test, |this| { + this.write_semantic(&result.binding, Some((entry_point.stage, Io::Output))) + })?; } } @@ -2265,6 +2341,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { Statement::Return { value: None } => { writeln!(self.out, "{level}return;")?; } + Statement::Return { value: Some(_) } if returns_nothing(module, func_ctx) => { + writeln!(self.out, "{level}return;")?; + } Statement::Return { value: Some(expr) } => { let base_ty_res = &func_ctx.info[expr].ty; let mut resolved = base_ty_res.inner_with(&module.types); @@ -4965,6 +5044,74 @@ pub(super) fn get_inner_matrix_data( } } +/// Returns `true` if `binding` is the depth that a fragment shader writes. +const fn is_frag_depth(binding: Option<&crate::Binding>) -> bool { + matches!( + binding, + Some(crate::Binding::BuiltIn(crate::BuiltIn::FragDepth)) + ) +} + +/// Returns `true` if the depth that `entry_point` writes must be discarded. +/// +/// HLSL has no way to specify that depth is unchanged. Discarding the depth has the same effect, +/// and keeps early depth tests enabled. +fn discards_frag_depth(entry_point: &crate::EntryPoint) -> bool { + entry_point.stage == ShaderStage::Fragment + && entry_point.early_depth_test + == Some(crate::EarlyDepthTest::Allow { + conservative: crate::ConservativeDepth::Unchanged, + }) +} + +/// Returns `true` if `entry_point` has nothing to return once its depth is discarded. +fn only_writes_discarded_depth(module: &Module, entry_point: &crate::EntryPoint) -> bool { + discards_frag_depth(entry_point) + && entry_point.function.result.as_ref().is_some_and(|result| { + match module.types[result.ty].inner { + TypeInner::Struct { ref members, .. } if result.binding.is_none() => members + .iter() + .all(|member| is_frag_depth(member.binding.as_ref())), + _ => is_frag_depth(result.binding.as_ref()), + } + }) +} + +/// Returns `true` if the function described by `func_ctx` is an entry point that returns nothing, +/// because the only thing it would return is a depth that is discarded. +fn returns_nothing(module: &Module, func_ctx: &back::FunctionCtx<'_>) -> bool { + match func_ctx.ty { + back::FunctionType::EntryPoint(index) => { + only_writes_discarded_depth(module, &module.entry_points[index as usize]) + } + back::FunctionType::Function(_) => false, + } +} + +/// Gets the early depth test of the fragment entry points in `entry_points` that return `ty`. +/// +/// Returns `None` if there are no such entry points or if they disagree. The semantic of a +/// depth output is part of the definition of the struct that contains it, so a struct that is +/// shared by entry points with different settings cannot use a conservative semantic. +fn shared_early_depth_test( + module: &Module, + entry_points: core::ops::Range, + ty: Handle, +) -> Option { + let mut users = entry_points + .map(|index| &module.entry_points[index]) + .filter(|ep| { + ep.stage == ShaderStage::Fragment + && ep.function.result.as_ref().is_some_and(|r| r.ty == ty) + }); + let first = users.next()?.early_depth_test; + if users.all(|ep| ep.early_depth_test == first) { + first + } else { + None + } +} + /// If `base` is an access chain of the form `mat`, `mat[col]`, or `mat[col][row]`, /// returns a tuple of the matrix, the column (vector) index (if present), and /// the row (scalar) index (if present). diff --git a/naga/tests/in/wgsl/early-depth-test-conservative-greater.toml b/naga/tests/in/wgsl/early-depth-test-conservative-greater.toml index 15b30d27a..fc766ab8e 100644 --- a/naga/tests/in/wgsl/early-depth-test-conservative-greater.toml +++ b/naga/tests/in/wgsl/early-depth-test-conservative-greater.toml @@ -1,2 +1,2 @@ capabilities = "EARLY_DEPTH_TEST" -targets = "METAL" +targets = "METAL | HLSL" diff --git a/naga/tests/in/wgsl/early-depth-test-conservative-struct.toml b/naga/tests/in/wgsl/early-depth-test-conservative-struct.toml index 15b30d27a..fc766ab8e 100644 --- a/naga/tests/in/wgsl/early-depth-test-conservative-struct.toml +++ b/naga/tests/in/wgsl/early-depth-test-conservative-struct.toml @@ -1,2 +1,2 @@ capabilities = "EARLY_DEPTH_TEST" -targets = "METAL" +targets = "METAL | HLSL" diff --git a/naga/tests/in/wgsl/early-depth-test-conservative-unchanged.toml b/naga/tests/in/wgsl/early-depth-test-conservative-unchanged.toml index 15b30d27a..fc766ab8e 100644 --- a/naga/tests/in/wgsl/early-depth-test-conservative-unchanged.toml +++ b/naga/tests/in/wgsl/early-depth-test-conservative-unchanged.toml @@ -1,2 +1,2 @@ capabilities = "EARLY_DEPTH_TEST" -targets = "METAL" +targets = "METAL | HLSL" diff --git a/naga/tests/in/wgsl/early-depth-test-conservative.toml b/naga/tests/in/wgsl/early-depth-test-conservative.toml index e57237ae4..dfb1ea964 100644 --- a/naga/tests/in/wgsl/early-depth-test-conservative.toml +++ b/naga/tests/in/wgsl/early-depth-test-conservative.toml @@ -1,5 +1,5 @@ capabilities = "EARLY_DEPTH_TEST" -targets = "SPIRV | GLSL | METAL" +targets = "SPIRV | GLSL | METAL | HLSL" [glsl] version.Desktop = 420 diff --git a/naga/tests/in/wgsl/early-depth-test-force.toml b/naga/tests/in/wgsl/early-depth-test-force.toml index 8b4b4f2e4..2db59b13f 100644 --- a/naga/tests/in/wgsl/early-depth-test-force.toml +++ b/naga/tests/in/wgsl/early-depth-test-force.toml @@ -1,5 +1,5 @@ capabilities = "EARLY_DEPTH_TEST" -targets = "SPIRV | GLSL | METAL" +targets = "SPIRV | GLSL | METAL | HLSL" [msl] lang_version = [1, 2] diff --git a/naga/tests/in/wgsl/early-depth-test-unchanged-struct.toml b/naga/tests/in/wgsl/early-depth-test-unchanged-struct.toml index 15b30d27a..fc766ab8e 100644 --- a/naga/tests/in/wgsl/early-depth-test-unchanged-struct.toml +++ b/naga/tests/in/wgsl/early-depth-test-unchanged-struct.toml @@ -1,2 +1,2 @@ capabilities = "EARLY_DEPTH_TEST" -targets = "METAL" +targets = "METAL | HLSL" diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-greater.hlsl b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-greater.hlsl new file mode 100644 index 000000000..4ca2d1e67 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-greater.hlsl @@ -0,0 +1,9 @@ +struct FragmentInput_main { + noperspective centroid float4 pos_1 : SV_Position; +}; + +float main(FragmentInput_main fragmentinput_main) : SV_DepthGreaterEqual +{ + float4 pos = fragmentinput_main.pos_1; + return (pos.z + 0.1); +} diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-greater.ron b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-greater.ron new file mode 100644 index 000000000..403f6c3d2 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-greater.ron @@ -0,0 +1,16 @@ +( + vertex:[ + ], + fragment:[ + ( + entry_point:"main", + target_profile:"ps_5_1", + ), + ], + compute:[ + ], + task:[ + ], + mesh:[ + ], +) diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-struct.hlsl b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-struct.hlsl new file mode 100644 index 000000000..6affd83ca --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-struct.hlsl @@ -0,0 +1,67 @@ +struct Output { + float4 color : SV_Target0; + float depth : SV_DepthLessEqual; +}; + +struct DisagreeingOutput { + float4 color : SV_Target0; + float depth : SV_Depth; +}; + +struct FragmentInput_agree_first { + noperspective centroid float4 pos_4 : SV_Position; +}; + +struct FragmentInput_agree_second { + noperspective centroid float4 pos_5 : SV_Position; +}; + +struct FragmentInput_disagree_conservative { + noperspective centroid float4 pos_6 : SV_Position; +}; + +struct FragmentInput_disagree_unrestricted { + float4 pos_7 : SV_Position; +}; + +Output ConstructOutput(float4 arg0, float arg1) { + Output ret = (Output)0; + ret.color = arg0; + ret.depth = arg1; + return ret; +} + +Output agree_first(FragmentInput_agree_first fragmentinput_agree_first) +{ + float4 pos = fragmentinput_agree_first.pos_4; + const Output output = ConstructOutput(float4(0.4, 0.3, 0.2, 0.1), (pos.z - 0.1)); + return output; +} + +Output agree_second(FragmentInput_agree_second fragmentinput_agree_second) +{ + float4 pos_1 = fragmentinput_agree_second.pos_5; + const Output output_1 = ConstructOutput(float4(0.1, 0.2, 0.3, 0.4), (pos_1.z - 0.2)); + return output_1; +} + +DisagreeingOutput ConstructDisagreeingOutput(float4 arg0, float arg1) { + DisagreeingOutput ret = (DisagreeingOutput)0; + ret.color = arg0; + ret.depth = arg1; + return ret; +} + +DisagreeingOutput disagree_conservative(FragmentInput_disagree_conservative fragmentinput_disagree_conservative) +{ + float4 pos_2 = fragmentinput_disagree_conservative.pos_6; + const DisagreeingOutput disagreeingoutput = ConstructDisagreeingOutput(float4(0.4, 0.3, 0.2, 0.1), (pos_2.z + 0.1)); + return disagreeingoutput; +} + +DisagreeingOutput disagree_unrestricted(FragmentInput_disagree_unrestricted fragmentinput_disagree_unrestricted) +{ + float4 pos_3 = fragmentinput_disagree_unrestricted.pos_7; + const DisagreeingOutput disagreeingoutput_1 = ConstructDisagreeingOutput(float4(0.1, 0.2, 0.3, 0.4), (pos_3.z * 2.0)); + return disagreeingoutput_1; +} diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-struct.ron b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-struct.ron new file mode 100644 index 000000000..138c7a543 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-struct.ron @@ -0,0 +1,28 @@ +( + vertex:[ + ], + fragment:[ + ( + entry_point:"agree_first", + target_profile:"ps_5_1", + ), + ( + entry_point:"agree_second", + target_profile:"ps_5_1", + ), + ( + entry_point:"disagree_conservative", + target_profile:"ps_5_1", + ), + ( + entry_point:"disagree_unrestricted", + target_profile:"ps_5_1", + ), + ], + compute:[ + ], + task:[ + ], + mesh:[ + ], +) diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-unchanged.hlsl b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-unchanged.hlsl new file mode 100644 index 000000000..41a8baab7 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-unchanged.hlsl @@ -0,0 +1,9 @@ +struct FragmentInput_main { + float4 pos_1 : SV_Position; +}; + +void main(FragmentInput_main fragmentinput_main) +{ + float4 pos = fragmentinput_main.pos_1; + return; +} diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-unchanged.ron b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-unchanged.ron new file mode 100644 index 000000000..403f6c3d2 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative-unchanged.ron @@ -0,0 +1,16 @@ +( + vertex:[ + ], + fragment:[ + ( + entry_point:"main", + target_profile:"ps_5_1", + ), + ], + compute:[ + ], + task:[ + ], + mesh:[ + ], +) diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative.hlsl b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative.hlsl new file mode 100644 index 000000000..12f4dc245 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative.hlsl @@ -0,0 +1,9 @@ +struct FragmentInput_main { + noperspective centroid float4 pos_1 : SV_Position; +}; + +float main(FragmentInput_main fragmentinput_main) : SV_DepthLessEqual +{ + float4 pos = fragmentinput_main.pos_1; + return (pos.z - 0.1); +} diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-conservative.ron b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative.ron new file mode 100644 index 000000000..403f6c3d2 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-conservative.ron @@ -0,0 +1,16 @@ +( + vertex:[ + ], + fragment:[ + ( + entry_point:"main", + target_profile:"ps_5_1", + ), + ], + compute:[ + ], + task:[ + ], + mesh:[ + ], +) diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-force.hlsl b/naga/tests/out/hlsl/wgsl-early-depth-test-force.hlsl new file mode 100644 index 000000000..68bbf7c19 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-force.hlsl @@ -0,0 +1,5 @@ +[earlydepthstencil] +float4 main() : SV_Target0 +{ + return float4(0.4, 0.3, 0.2, 0.1); +} diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-force.ron b/naga/tests/out/hlsl/wgsl-early-depth-test-force.ron new file mode 100644 index 000000000..403f6c3d2 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-force.ron @@ -0,0 +1,16 @@ +( + vertex:[ + ], + fragment:[ + ( + entry_point:"main", + target_profile:"ps_5_1", + ), + ], + compute:[ + ], + task:[ + ], + mesh:[ + ], +) diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-unchanged-struct.hlsl b/naga/tests/out/hlsl/wgsl-early-depth-test-unchanged-struct.hlsl new file mode 100644 index 000000000..92f59c714 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-unchanged-struct.hlsl @@ -0,0 +1,58 @@ +struct ColorAndDepth { + float4 color : SV_Target0; + float depth : SV_Depth; +}; + +struct OnlyDepth { + float depth : SV_Depth; +}; + +struct FragmentInput_color_and_depth { + float4 pos_3 : SV_Position; +}; + +struct FragmentOutput_color_and_depth { + float4 color : SV_Target0; +}; + +struct FragmentInput_only_depth { + float4 pos_4 : SV_Position; +}; + +struct FragmentInput_unrestricted { + float4 pos_5 : SV_Position; +}; + +ColorAndDepth ConstructColorAndDepth(float4 arg0, float arg1) { + ColorAndDepth ret = (ColorAndDepth)0; + ret.color = arg0; + ret.depth = arg1; + return ret; +} + +FragmentOutput_color_and_depth color_and_depth(FragmentInput_color_and_depth fragmentinput_color_and_depth) +{ + float4 pos = fragmentinput_color_and_depth.pos_3; + const ColorAndDepth coloranddepth = ConstructColorAndDepth(float4(0.4, 0.3, 0.2, 0.1), pos.z); + const FragmentOutput_color_and_depth coloranddepth_1 = { coloranddepth.color }; + return coloranddepth_1; +} + +OnlyDepth ConstructOnlyDepth(float arg0) { + OnlyDepth ret = (OnlyDepth)0; + ret.depth = arg0; + return ret; +} + +void only_depth(FragmentInput_only_depth fragmentinput_only_depth) +{ + float4 pos_1 = fragmentinput_only_depth.pos_4; + return; +} + +ColorAndDepth unrestricted(FragmentInput_unrestricted fragmentinput_unrestricted) +{ + float4 pos_2 = fragmentinput_unrestricted.pos_5; + const ColorAndDepth coloranddepth_2 = ConstructColorAndDepth(float4(0.1, 0.2, 0.3, 0.4), (pos_2.z * 2.0)); + return coloranddepth_2; +} diff --git a/naga/tests/out/hlsl/wgsl-early-depth-test-unchanged-struct.ron b/naga/tests/out/hlsl/wgsl-early-depth-test-unchanged-struct.ron new file mode 100644 index 000000000..7fce9e1fd --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-early-depth-test-unchanged-struct.ron @@ -0,0 +1,24 @@ +( + vertex:[ + ], + fragment:[ + ( + entry_point:"color_and_depth", + target_profile:"ps_5_1", + ), + ( + entry_point:"only_depth", + target_profile:"ps_5_1", + ), + ( + entry_point:"unrestricted", + target_profile:"ps_5_1", + ), + ], + compute:[ + ], + task:[ + ], + mesh:[ + ], +) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 7a1a58526..8044dda91 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -476,6 +476,7 @@ impl super::Adapter { | wgt::Features::POLYGON_MODE_LINE | wgt::Features::PRIMITIVE_INDEX | wgt::Features::RG11B10UFLOAT_RENDERABLE + | wgt::Features::SHADER_EARLY_DEPTH_TEST | wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES | wgt::Features::TEXTURE_ATOMIC | wgt::Features::TEXTURE_COMPONENT_SWIZZLE diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 34428c595..9ea155b9f 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -285,6 +285,7 @@ impl super::Device { layout: &super::PipelineLayout, naga_stage: naga::ShaderStage, fragment_stage: Option<&crate::ProgrammableStage>, + multisampled: bool, ) -> Result { let stage_bit = auxil::map_naga_stage(naga_stage); @@ -358,6 +359,7 @@ impl super::Device { let pipeline_options = hlsl::PipelineOptions { entry_point: Some((naga_stage, stage.entry_point.to_string())), + multisampled, }; //TODO: reuse the writer @@ -2046,7 +2048,13 @@ impl crate::Device for super::Device { let blob_fs = match desc.fragment_stage { Some(ref stage) => { shader_stages |= wgt::ShaderStages::FRAGMENT; - Some(self.load_shader(stage, desc.layout, naga::ShaderStage::Fragment, None)?) + Some(self.load_shader( + stage, + desc.layout, + naga::ShaderStage::Fragment, + None, + 1 < desc.multisample.count, + )?) } None => None, }; @@ -2159,6 +2167,7 @@ impl crate::Device for super::Device { desc.layout, naga::ShaderStage::Vertex, desc.fragment_stage.as_ref(), + 1 < desc.multisample.count, )?); for (i, (stride, vbuf)) in vertex_strides.iter_mut().zip(vertex_buffers).enumerate() @@ -2232,6 +2241,7 @@ impl crate::Device for super::Device { desc.layout, naga::ShaderStage::Task, desc.fragment_stage.as_ref(), + 1 < desc.multisample.count, )?) } else { None @@ -2247,6 +2257,7 @@ impl crate::Device for super::Device { desc.layout, naga::ShaderStage::Mesh, desc.fragment_stage.as_ref(), + 1 < desc.multisample.count, )?); stream_desc.task_shader = task_shader; stream_desc.mesh_shader = blob_ms.as_ref().unwrap().create_native_shader(); @@ -2299,8 +2310,13 @@ impl crate::Device for super::Device { super::PipelineCache, >, ) -> Result { - let blob_cs = - self.load_shader(&desc.stage, desc.layout, naga::ShaderStage::Compute, None)?; + let blob_cs = self.load_shader( + &desc.stage, + desc.layout, + naga::ShaderStage::Compute, + None, + false, + )?; let pair = { profiling::scope!("ID3D12Device::CreateComputePipelineState"); diff --git a/wgpu-types/src/features.rs b/wgpu-types/src/features.rs index 61ac26917..122aa6c73 100644 --- a/wgpu-types/src/features.rs +++ b/wgpu-types/src/features.rs @@ -809,6 +809,8 @@ features! { /// /// - `[[early_fragment_tests]]` (MSL 1.2+) /// + /// - `[earlydepthstencil]` (HLSL) + /// /// 2. Provide a conservative depth specifier that allows an additional early /// depth test under certain conditions: /// @@ -818,11 +820,15 @@ features! { /// /// - `[[depth(greater/less)]]` (MSL, any version) /// + /// - `SV_Depth` (HLSL), for pipelines that are not + /// multisampled. Multisampled pipelines use an unrestricted `SV_Depth` instead. + /// /// See [`EarlyDepthTest`] for more details. /// /// Supported platforms: /// - Vulkan /// - Metal (with MSL 1.2+) + /// - DX12 /// - GLES 3.1+ /// /// This is a native only feature.