From eb0277dd66703f4336a275617433616427ab9c9c Mon Sep 17 00:00:00 2001 From: Eideren Date: Fri, 16 Aug 2024 15:52:45 +0200 Subject: [PATCH 1/2] [Graphics] Custom Depth Test Function for Materials --- .../Rendering/MaterialPass.cs | 5 ++++ .../Rendering/Materials/MaterialAttributes.cs | 25 ++++++++++++++++--- .../Materials/MaterialRenderFeature.cs | 8 ++++++ .../Rendering/MeshPipelineProcessor.cs | 3 +++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/sources/engine/Stride.Rendering/Rendering/MaterialPass.cs b/sources/engine/Stride.Rendering/Rendering/MaterialPass.cs index ffa390c8c5..c9a03c87b6 100644 --- a/sources/engine/Stride.Rendering/Rendering/MaterialPass.cs +++ b/sources/engine/Stride.Rendering/Rendering/MaterialPass.cs @@ -43,6 +43,11 @@ public MaterialPass(ParameterCollection parameters) /// public CullMode? CullMode; + /// + /// Overrides depth clip for this material. + /// + public CompareFunction? DepthFunction; + /// /// Overrides the blend state for this material. /// diff --git a/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs b/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs index 669cc98d18..ea23aaa84b 100644 --- a/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs +++ b/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs @@ -172,6 +172,15 @@ public MaterialAttributes() [DefaultValue(CullMode.Back)] public CullMode CullMode { get; set; } + /// + /// The test used to figure out whether the material should be drawn when beind other models + /// + /// The test used to figure out whether the material should be drawn when beind other models + [Display("Depth Function", "Misc")] + [DataMember(135)] + [DefaultValue(CompareFunction.Less)] + public CompareFunction DepthFunction { get; set; } = CompareFunction.Less; + /// /// Gets or sets the clear coat shading features for the material. /// @@ -198,8 +207,7 @@ public void Visit(MaterialGeneratorContext context) // TODO: Should we apply it to any Diffuse Model? var isEnergyConservative = (Specular as MaterialSpecularMapFeature)?.IsEnergyConservative ?? false; - var lambert = DiffuseModel as IEnergyConservativeDiffuseModelFeature; - if (lambert != null) + if (DiffuseModel is IEnergyConservativeDiffuseModelFeature lambert) { lambert.IsEnergyConservative = isEnergyConservative; } @@ -227,8 +235,8 @@ public void Visit(MaterialGeneratorContext context) // If hair shading is enabled, ignore the transparency feature to avoid errors during shader compilation. // Allowing the transparency feature while hair shading is on makes no sense anyway. - if (!(SpecularModel is MaterialSpecularHairModelFeature) && - !(DiffuseModel is MaterialDiffuseHairModelFeature)) + if (SpecularModel is not MaterialSpecularHairModelFeature && + DiffuseModel is not MaterialDiffuseHairModelFeature) { context.Visit(Transparency); } @@ -246,6 +254,15 @@ public void Visit(MaterialGeneratorContext context) context.MaterialPass.CullMode = CullMode; } } + + // Only set the DepthFunction when the pass is not overriden (Only based on assumptions from how CullMode operates, not exactly sure how/when this would be used) + if (context.Step == MaterialGeneratorStep.GenerateShader && DepthFunction != CompareFunction.Less) + { + if (context.MaterialPass.DepthFunction == null) + { + context.MaterialPass.DepthFunction = DepthFunction; + } + } } } } diff --git a/sources/engine/Stride.Rendering/Rendering/Materials/MaterialRenderFeature.cs b/sources/engine/Stride.Rendering/Rendering/Materials/MaterialRenderFeature.cs index 533b2c1c39..59f571b19b 100644 --- a/sources/engine/Stride.Rendering/Rendering/Materials/MaterialRenderFeature.cs +++ b/sources/engine/Stride.Rendering/Rendering/Materials/MaterialRenderFeature.cs @@ -66,6 +66,7 @@ internal class MaterialInfo : MaterialInfoBase public int PermutationCounter; // Dirty counter against material.Parameters.PermutationCounter public ParameterCollection MaterialParameters; // Protect against changes of Material.Parameters instance (happens with editor fast reload) public CullMode? CullMode; + public CompareFunction? DepthFunction; public ShaderSource VertexStageSurfaceShaders; public ShaderSource VertexStageStreamInitializer; @@ -92,6 +93,7 @@ public MaterialInfo(MaterialPass materialPass) { MaterialPass = materialPass; CullMode = materialPass.CullMode; + DepthFunction = materialPass.DepthFunction; } } @@ -182,6 +184,12 @@ public override void PrepareEffectPermutations(RenderDrawContext context) resetPipelineState = true; } + if (materialInfo != null && materialInfo.DepthFunction != material.DepthFunction) + { + materialInfo.DepthFunction = material.DepthFunction; + resetPipelineState = true; + } + for (int i = 0; i < effectSlotCount; ++i) { var staticEffectObjectNode = staticObjectNode * effectSlotCount + i; diff --git a/sources/engine/Stride.Rendering/Rendering/MeshPipelineProcessor.cs b/sources/engine/Stride.Rendering/Rendering/MeshPipelineProcessor.cs index 2730ce996c..fdde06b9a0 100644 --- a/sources/engine/Stride.Rendering/Rendering/MeshPipelineProcessor.cs +++ b/sources/engine/Stride.Rendering/Rendering/MeshPipelineProcessor.cs @@ -65,6 +65,9 @@ public override void Process(RenderNodeReference renderNodeReference, ref Render pipelineState.RasterizerState.CullMode = cullMode; + if (renderMesh.MaterialInfo.DepthFunction is {} newDepthFunction) + pipelineState.DepthStencilState.DepthBufferFunction = newDepthFunction; + if (isMultisample) { pipelineState.RasterizerState.MultisampleCount = output.MultisampleCount; From a69590d921cd4b5965523043b1d07fe9227ecb03 Mon Sep 17 00:00:00 2001 From: Eideren Date: Sun, 18 Aug 2024 22:59:17 +0200 Subject: [PATCH 2/2] Fix typo --- .../Rendering/Materials/MaterialAttributes.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs b/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs index ea23aaa84b..a4e6b2a3a9 100644 --- a/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs +++ b/sources/engine/Stride.Rendering/Rendering/Materials/MaterialAttributes.cs @@ -173,9 +173,9 @@ public MaterialAttributes() public CullMode CullMode { get; set; } /// - /// The test used to figure out whether the material should be drawn when beind other models + /// The test used to figure out whether the material should be drawn when behind other models /// - /// The test used to figure out whether the material should be drawn when beind other models + /// The test used to figure out whether the material should be drawn when behind other models [Display("Depth Function", "Misc")] [DataMember(135)] [DefaultValue(CompareFunction.Less)]