diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilShowShader.shader b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilShowShader.shader index 4fddac06f1a..ea14c9eaf94 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilShowShader.shader +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilShowShader.shader @@ -88,7 +88,6 @@ Shader "Custom/StencilShowShader" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT //enable GPU instancing support diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilWriteShader.shader b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilWriteShader.shader index d6eb76b74a3..3cc31cfb464 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilWriteShader.shader +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/2x_Lighting/2208_And_2209_Reflection_Stencil/StencilWriteShader.shader @@ -88,7 +88,6 @@ Shader "Custom/StencilWriteShader" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT //enable GPU instancing support diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 1d5f2229539..d8240ace50c 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -169,6 +169,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Improve performance of GPU light AABB generation - Removed the max clamp value for the RTR, RTAO and RTGI's ray length (case 1279849). - Meshes assigned with a decal material are not visible anymore in ray-tracing or path-tracing. +- Removed BLEND shader keywords. ## [10.0.0] - 2019-06-10 diff --git a/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md b/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md index 75b31489028..bb0f4665d4c 100644 --- a/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md +++ b/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md @@ -166,11 +166,23 @@ to: For example, the call in the Lit shader has been updated to: `float4 preLD = SampleEnv(lightLoopContext, lightData.envIndex, R, PerceptualRoughnessToMipmapLevel(preLightData.iblPerceptualRoughness), lightData.rangeCompressionFactorCompensation, posInput.positionNDC);` +From 10.x, the shader keywords _BLENDMODE_ALPHA _BLENDMODE_ADD and _BLENDMODE_PRE_MULTIPLY have been removed. They are no longer used and the property _Blendmode is used instead. +For example in Material.hlsl, the following lines: +``` + #if defined(_BLENDMODE_ADD) || defined(_BLENDMODE_ALPHA) + return float4(diffuseLighting * opacity + specularLighting, opacity); +``` +is replace by +``` + if (_BlendMode == BLENDMODE_ALPHA || _BlendMode == BLENDMODE_ADDITIVE) + return float4(diffuseLighting * opacity + specularLighting, opacity); +``` +This reduced the number of shader variant. In case of custom shader it can be required to move the include of Material.hlsl after the declaration of the property _Blendmode. + From 10.x, HDRP includes a new optimization for [Planar Reflection Probes](Planar-Reflection-Probe.md). Now, when a shader samples a probe's environment map, it samples from mip level 0 if the LightData.roughReflections parameter is enabled (has a value of 1.0). You must update your custom shaders to take this behavior into account. For example, the call in the Lit shader has been updated to: `float4 preLD = SampleEnv(lightLoopContext, lightData.envIndex, R, PerceptualRoughnessToMipmapLevel(preLightData.iblPerceptualRoughness) * lightData.roughReflections, lightData.rangeCompressionFactorCompensation, posInput.positionNDC);` - ## Raytracing From Unity 2020.2, the Raytracing Node in shader graph now apply the raytraced path (previously low path) to all raytraced effects but path tracing. diff --git a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXArnoldSurfaceMaterialDescriptionPreprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXArnoldSurfaceMaterialDescriptionPreprocessor.cs index b3eca64b7eb..1ad50274841 100644 --- a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXArnoldSurfaceMaterialDescriptionPreprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXArnoldSurfaceMaterialDescriptionPreprocessor.cs @@ -104,11 +104,11 @@ void CreateFromMayaArnoldStandardSurfaceMaterial(MaterialDescription description material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 10); + material.SetFloat("_BlendMode", (float)BlendMode.Alpha); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); material.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); material.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); - material.EnableKeyword("_BLENDMODE_ALPHA"); material.renderQueue = 3000; } else @@ -209,11 +209,11 @@ void CreateFrom3DsMaxArnoldStandardSurfaceMaterial(MaterialDescription descripti material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 10); + material.SetFloat("_BlendMode", (float)BlendMode.Alpha); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); material.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); material.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); - material.EnableKeyword("_BLENDMODE_ALPHA"); material.renderQueue = 3000; } else diff --git a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXMaterialDescriptionPostprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXMaterialDescriptionPostprocessor.cs index ed17c905afb..dc861dc5587 100644 --- a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXMaterialDescriptionPostprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/FBXMaterialDescriptionPostprocessor.cs @@ -81,11 +81,11 @@ public void OnPreprocessMaterialDescription(MaterialDescription description, Mat material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); + material.SetFloat("_BlendMode", (float)BlendMode.Alpha); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); material.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); material.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); - material.EnableKeyword("_BLENDMODE_ALPHA"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else diff --git a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/PhysicalMaterial3DsMaxPreprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/PhysicalMaterial3DsMaxPreprocessor.cs index 9865413ae39..72b23a0d24d 100644 --- a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/PhysicalMaterial3DsMaxPreprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/PhysicalMaterial3DsMaxPreprocessor.cs @@ -210,11 +210,11 @@ void CreateFrom3DsPhysicalMaterial(MaterialDescription description, Material mat material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 10); + material.SetFloat("_BlendMode", (float)BlendMode.Alpha); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); material.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); material.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); - material.EnableKeyword("_BLENDMODE_ALPHA"); material.renderQueue = 3000; } else diff --git a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/SketchupMaterialDescriptionPostprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/SketchupMaterialDescriptionPostprocessor.cs index a3c6f361731..403b8c856b5 100644 --- a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/SketchupMaterialDescriptionPostprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/SketchupMaterialDescriptionPostprocessor.cs @@ -66,11 +66,11 @@ public void OnPreprocessMaterialDescription(MaterialDescription description, Mat material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); + material.SetFloat("_BlendMode", (float)BlendMode.Alpha); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); material.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); material.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); - material.EnableKeyword("_BLENDMODE_ALPHA"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else diff --git a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/ThreeDSMaterialDescriptionPostprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/ThreeDSMaterialDescriptionPostprocessor.cs index 55f4669596e..28c46e23884 100644 --- a/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/ThreeDSMaterialDescriptionPostprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/AssetProcessors/ThreeDSMaterialDescriptionPostprocessor.cs @@ -59,11 +59,11 @@ public void OnPreprocessMaterialDescription(MaterialDescription description, Mat material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); + material.SetFloat("_BlendMode", (float)BlendMode.Alpha); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); material.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); material.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); - material.EnableKeyword("_BLENDMODE_ALPHA"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs index 156f6cf827e..267e7ebdbc0 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/HDTarget.cs @@ -855,7 +855,6 @@ static class CoreDefines static class CoreIncludes { // CorePregraph - public const string kTextureStack = "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl"; public const string kShaderVariables = "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"; public const string kFragInputs = "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl"; public const string kMaterial = "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"; @@ -933,9 +932,6 @@ static class CoreIncludes public static IncludeCollection CorePregraph = new IncludeCollection { - { kTextureStack, IncludeLocation.Pregraph }, // TODO: put this on a conditional - { kShaderVariables, IncludeLocation.Pregraph }, - { kFragInputs, IncludeLocation.Pregraph }, { kDebugDisplay, IncludeLocation.Pregraph }, { kMaterial, IncludeLocation.Pregraph }, }; @@ -943,12 +939,7 @@ static class CoreIncludes public static IncludeCollection RaytracingCorePregraph = new IncludeCollection { // Pregraph includes - { CoreIncludes.kTextureStack, IncludeLocation.Pregraph }, - { CoreIncludes.kFragInputs, IncludeLocation.Pregraph }, - - // Ray Tracing macros should be included before shader variables to guarantee that the macros are overriden { CoreIncludes.kRaytracingMacros, IncludeLocation.Pregraph }, - { CoreIncludes.kShaderVariables, IncludeLocation.Pregraph }, { CoreIncludes.kMaterial, IncludeLocation.Pregraph }, { CoreIncludes.kShaderVariablesRaytracing, IncludeLocation.Pregraph }, { CoreIncludes.kShaderVariablesRaytracingLightLoop, IncludeLocation.Pregraph }, diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/Templates/ShaderPass.template b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/Templates/ShaderPass.template index 8e875b9492e..34231b87c85 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/Templates/ShaderPass.template +++ b/com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/Templates/ShaderPass.template @@ -25,7 +25,10 @@ Pass $splice(GraphKeywords) #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl" + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl" // Required to be include before we include properties as it define DECLARE_STACK_CB // -------------------------------------------------- // Defines @@ -108,17 +111,22 @@ Pass // Dots Instancing $splice(DotsInstancingOptions) + // Various properties + $splice(HybridV1InjectedBuiltinProperties) - // Includes - $splice(PreGraphIncludes) + // -- Graph Properties + $splice(GraphProperties) - // Properties used by SceneSelectionPass + // -- Properties used by SceneSelectionPass #ifdef SCENESELECTIONPASS int _ObjectId; int _PassValue; #endif + // Includes + $splice(PreGraphIncludes) + // -------------------------------------------------- // Structs and Packing @@ -129,8 +137,6 @@ Pass // -------------------------------------------------- // Graph - // Graph Properties - $splice(GraphProperties) // Graph Functions $splice(GraphFunctions) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/BaseUnlitGUI.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/BaseUnlitGUI.cs index e326dd4ece8..867543932f7 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/BaseUnlitGUI.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Unlit/BaseUnlitGUI.cs @@ -38,11 +38,6 @@ public static void SetupBaseUnlitKeywords(this Material material) bool transparentWritesMotionVec = (surfaceType == SurfaceType.Transparent) && material.HasProperty(kTransparentWritingMotionVec) && material.GetInt(kTransparentWritingMotionVec) > 0; CoreUtils.SetKeyword(material, "_TRANSPARENT_WRITES_MOTION_VEC", transparentWritesMotionVec); - // These need to always been set either with opaque or transparent! So a users can switch to opaque and remove the keyword correctly - CoreUtils.SetKeyword(material, "_BLENDMODE_ALPHA", false); - CoreUtils.SetKeyword(material, "_BLENDMODE_ADD", false); - CoreUtils.SetKeyword(material, "_BLENDMODE_PRE_MULTIPLY", false); - HDRenderQueue.RenderQueueType renderQueueType = HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue); bool needOffScreenBlendFactor = renderQueueType == HDRenderQueue.RenderQueueType.AfterPostprocessTransparent || renderQueueType == HDRenderQueue.RenderQueueType.LowTransparent; @@ -93,10 +88,6 @@ public static void SetupBaseUnlitKeywords(this Material material) { BlendMode blendMode = material.GetBlendMode(); - CoreUtils.SetKeyword(material, "_BLENDMODE_ALPHA", BlendMode.Alpha == blendMode); - CoreUtils.SetKeyword(material, "_BLENDMODE_ADD", BlendMode.Additive == blendMode); - CoreUtils.SetKeyword(material, "_BLENDMODE_PRE_MULTIPLY", BlendMode.Premultiply == blendMode); - // When doing off-screen transparency accumulation, we change blend factors as described here: https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html switch (blendMode) { diff --git a/com.unity.render-pipelines.high-definition/Editor/VFXGraph/Outputs/VFXAbstractParticleHDRPLitOutput.cs b/com.unity.render-pipelines.high-definition/Editor/VFXGraph/Outputs/VFXAbstractParticleHDRPLitOutput.cs index bd047060442..467a80a226d 100644 --- a/com.unity.render-pipelines.high-definition/Editor/VFXGraph/Outputs/VFXAbstractParticleHDRPLitOutput.cs +++ b/com.unity.render-pipelines.high-definition/Editor/VFXGraph/Outputs/VFXAbstractParticleHDRPLitOutput.cs @@ -428,18 +428,7 @@ public override IEnumerable> additionalRep var forwardDefines = new VFXShaderWriter(); forwardDefines.WriteLine("#define _ENABLE_FOG_ON_TRANSPARENT"); forwardDefines.WriteLine("#define _DISABLE_DECALS"); - switch (blendMode) - { - case BlendMode.Alpha: - forwardDefines.WriteLine("#define _BLENDMODE_ALPHA"); - break; - case BlendMode.Additive: - forwardDefines.WriteLine("#define _BLENDMODE_ADD"); - break; - case BlendMode.AlphaPremultiplied: - forwardDefines.WriteLine("#define _BLENDMODE_PRE_MULTIPLY"); - break; - } + if (!isBlendModeOpaque) { if (preserveSpecularLighting) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader index a13ee805576..e22d60871c9 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader @@ -120,7 +120,7 @@ Shader "HDRP/AxF" // [ToggleUI] _EnableFogOnTransparent("Enable Fog", Float) = 1.0 -// [ToggleUI] _EnableBlendModePreserveSpecularLighting("Enable Blend Mode Preserve Specular Lighting", Float) = 1.0 +// [HideInInspector][ToggleUI] _EnableBlendModePreserveSpecularLighting("Enable Blend Mode Preserve Specular Lighting", Float) = 1.0 [ToggleUI] _DoubleSidedEnable("Double sided enable", Float) = 0.0 [Enum(Flip, 0, Mirror, 1, None, 2)] _DoubleSidedNormalMode("Double sided normal mode", Float) = 1 // This is for the editor only, see BaseLitUI.cs: _DoubleSidedConstants will be set based on the mode. @@ -180,7 +180,6 @@ Shader "HDRP/AxF" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _BLENDMODE_PRESERVE_SPECULAR_LIGHTING // easily handled in material.hlsl, so adding this already. #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFProperties.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFProperties.hlsl index 59d7ab2b016..7cced1b697f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFProperties.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFProperties.hlsl @@ -120,6 +120,7 @@ float _AlphaCutoff; float _UseShadowThreshold; float _AlphaCutoffShadow; float4 _DoubleSidedConstants; +float _BlendMode; // Specular AA float _EnableGeometricSpecularAA; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader index 4edf9209fa1..96062fd3663 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader @@ -458,7 +458,6 @@ Shader "HDRP/LayeredLit" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _BLENDMODE_PRESERVE_SPECULAR_LIGHTING #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader index d778a5b9352..2e032c70e79 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitTessellation.shader @@ -466,7 +466,6 @@ Shader "HDRP/LayeredLitTessellation" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _BLENDMODE_PRESERVE_SPECULAR_LIGHTING #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index ddd32fe71c9..dca1e9f2cd8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -289,7 +289,6 @@ Shader "HDRP/Lit" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _BLENDMODE_PRESERVE_SPECULAR_LIGHTING #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT #pragma shader_feature_local _TRANSPARENT_WRITES_MOTION_VEC diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitProperties.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitProperties.hlsl index 2a9a82e5ba3..e0640512335 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitProperties.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitProperties.hlsl @@ -112,6 +112,7 @@ float _DistortionVectorBias; float _DistortionBlurScale; float _DistortionBlurRemapMin; float _DistortionBlurRemapMax; +float _BlendMode; float _PPDMaxSamples; float _PPDMinSamples; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader index de06161e0f1..ae72203e1ca 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitTessellation.shader @@ -297,7 +297,6 @@ Shader "HDRP/LitTessellation" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _BLENDMODE_PRESERVE_SPECULAR_LIGHTING #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT @@ -357,6 +356,8 @@ Shader "HDRP/LitTessellation" // LitShading.hlsl implements the light loop API. // LitData.hlsl is included here, LitShading.hlsl is included below for shading passes only. + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" + ENDHLSL SubShader diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl index 8e776624000..ab3b09342cc 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl @@ -10,12 +10,12 @@ #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ImageBasedLighting.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/AtmosphericScattering/AtmosphericScattering.hlsl" +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl" // Guidelines for Material Keyword. // There is a set of Material Keyword that a HD shaders must define (or not define). We call them system KeyWord. // .Shader need to define: // - _SURFACE_TYPE_TRANSPARENT if they use a transparent material -// - _BLENDMODE_ALPHA, _BLENDMODE_ADD, _BLENDMODE_PRE_MULTIPLY for blend mode // - _BLENDMODE_PRESERVE_SPECULAR_LIGHTING for correct lighting when blend mode are use with a Lit material // - _ENABLE_FOG_ON_TRANSPARENT if fog is enable on transparent surface // - _DISABLE_DECALS if the material don't support decals @@ -33,7 +33,9 @@ float4 ApplyBlendMode(float3 diffuseLighting, float3 specularLighting, float opa // When doing off screen rendering (low-res transparency or after post process pass) and rendering opaque objects, opacity needs to be set to zero for proper compositing. if (_OffScreenRendering != 0) opacity = 0; -#endif + + return float4(diffuseLighting + specularLighting, opacity); +#else // ref: http://advances.realtimerendering.com/other/2016/naughty_dog/NaughtyDog_TechArt_Final.pdf // Lit transparent object should have reflection and transmission. @@ -41,19 +43,21 @@ float4 ApplyBlendMode(float3 diffuseLighting, float3 specularLighting, float opa // However reflection should not be affected by blend mode. For example a glass should still display reflection and not lose the highlight when blend // This is the purpose of following function, "Cancel" the blend mode effect on the specular lighting but not on the diffuse lighting #ifdef _BLENDMODE_PRESERVE_SPECULAR_LIGHTING - // In the case of _BLENDMODE_ALPHA the code should be float4(diffuseLighting + (specularLighting / max(opacity, 0.01)), opacity) + // In the case of alpha blend mode the code should be float4(diffuseLighting + (specularLighting / max(opacity, 0.01)), opacity) // However this have precision issue when reaching 0, so we change the blend mode and apply src * src_a inside the shader instead - #if defined(_BLENDMODE_ADD) || defined(_BLENDMODE_ALPHA) - return float4(diffuseLighting * opacity + specularLighting, opacity); - #else // defined(_BLENDMODE_PRE_MULTIPLY) - return float4(diffuseLighting + specularLighting, opacity); - #endif + if (_BlendMode == BLENDMODE_ALPHA || _BlendMode == BLENDMODE_ADDITIVE) + return float4(diffuseLighting * opacity + specularLighting, opacity); + else + return float4(diffuseLighting + specularLighting, opacity); #else - #if defined(_BLENDMODE_ADD) || defined(_BLENDMODE_ALPHA) - return float4((diffuseLighting + specularLighting) * opacity, opacity); - #else // defined(_BLENDMODE_PRE_MULTIPLY) - return float4(diffuseLighting + specularLighting, opacity); - #endif + + if (_BlendMode == BLENDMODE_ALPHA || _BlendMode == BLENDMODE_ADDITIVE) + return float4((diffuseLighting + specularLighting) * opacity, opacity); + else + return float4(diffuseLighting + specularLighting, opacity); + +#endif + #endif } @@ -68,6 +72,7 @@ float4 ApplyBlendMode(float3 color, float opacity) // Used for transparent object. input color is color + alpha of the original transparent pixel. // This must be call after ApplyBlendMode to work correctly +// Caution: Must stay in sync with VFXApplyFog in VFXCommon.hlsl float4 EvaluateAtmosphericScattering(PositionInputs posInput, float3 V, float4 inputColor) { float4 result = inputColor; @@ -76,23 +81,29 @@ float4 EvaluateAtmosphericScattering(PositionInputs posInput, float3 V, float4 i float3 volColor, volOpacity; EvaluateAtmosphericScattering(posInput, V, volColor, volOpacity); // Premultiplied alpha - #if defined(_BLENDMODE_ALPHA) + if (_BlendMode == BLENDMODE_ALPHA) + { // Regular alpha blend need to multiply fog color by opacity (as we do src * src_a inside the shader) // result.rgb = lerp(result.rgb, unpremul_volColor * result.a, volOpacity); // result.rgb = result.rgb + volOpacity * (unpremul_volColor * result.a - result.rgb); // result.rgb = result.rgb + volColor * result.a - result.rgb * volOpacity; result.rgb = result.rgb * (1 - volOpacity) + volColor * result.a; - #elif defined(_BLENDMODE_ADD) + } + else if (_BlendMode == BLENDMODE_ADDITIVE) + { // For additive, we just need to fade to black with fog density (black + background == background color == fog color) result.rgb = result.rgb * (1.0 - volOpacity); - #elif defined(_BLENDMODE_PRE_MULTIPLY) + } + else if (_BlendMode == BLENDMODE_PREMULTIPLY) + { // For Pre-Multiplied Alpha Blend, we need to multiply fog color by src alpha to match regular alpha blending formula. // result.rgb = lerp(result.rgb, unpremul_volColor * result.a, volOpacity); result.rgb = result.rgb * (1 - volOpacity) + volColor * result.a; // Note: this formula for color is correct, assuming we apply the Over operator afterwards // (see the appendix in the Deep Compositing paper). But do we? // Additionally, we do not modify the alpha here, which is most certainly WRONG. - #endif + } + #else // Evaluation of fog for opaque objects is currently done in a full screen pass independent from any material parameters. // but this funtction is called in generic forward shader code so we need it to be neutral in this case. diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs new file mode 100644 index 00000000000..490a973dbf1 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs @@ -0,0 +1,17 @@ +// This enum definition really belongs to MaterialExtension.cs, however for limitation of the parser that generates HLSL definitions, +// hlsl code is not generated from files that use C# 7+ features, so this needs to be in its own file for now. Will need to move back +// MaterialExtension.cs once the parser is updated to support C# 7. +using UnityEngine.Rendering; + +namespace UnityEditor.Rendering.HighDefinition +{ + // Enum values are hardcoded for retro-compatibility. Don't change them. + [GenerateHLSL] + enum BlendMode + { + // Note: value is due to code change, don't change the value + Alpha = 0, + Premultiply = 4, + Additive = 1 + } +} diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl new file mode 100644 index 00000000000..1651cc12044 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl @@ -0,0 +1,15 @@ +// +// This file was automatically generated. Please don't edit by hand. +// + +#ifndef MATERIALBLENDMODEENUM_CS_HLSL +#define MATERIALBLENDMODEENUM_CS_HLSL +// +// UnityEditor.Rendering.HighDefinition.BlendMode: static fields +// +#define BLENDMODE_ALPHA (0) +#define BLENDMODE_PREMULTIPLY (4) +#define BLENDMODE_ADDITIVE (1) + + +#endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl.meta b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl.meta new file mode 100644 index 00000000000..a46d46508d7 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: ea277db9c382b5348b7a8993b2c547e3 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.meta b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.meta new file mode 100644 index 00000000000..35a87afc1c9 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c91a0012882f0914c971134f6ea58d5d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialExtension.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialExtension.cs index d9641a7a16a..e5422c068e4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialExtension.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialExtension.cs @@ -17,15 +17,6 @@ enum SurfaceType Transparent } - // Enum values are hardcoded for retro-compatibility. Don't change them. - enum BlendMode - { - // Note: value is due to code change, don't change the value - Alpha = 0, - Premultiply = 4, - Additive = 1 - } - enum DisplacementMode { None, diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader index 21bc5893d5d..23b591aa62f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader @@ -113,7 +113,6 @@ Shader "HDRP/Unlit" // Keyword for transparent #pragma shader_feature _SURFACE_TYPE_TRANSPARENT - #pragma shader_feature_local _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY #pragma shader_feature_local _ENABLE_FOG_ON_TRANSPARENT #pragma shader_feature_local _ADD_PRECOMPUTED_VELOCITY diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitProperties.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitProperties.hlsl index ea9aa29b447..d259d3ae6df 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitProperties.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitProperties.hlsl @@ -25,6 +25,7 @@ float _DistortionVectorBias; float _DistortionBlurScale; float _DistortionBlurRemapMin; float _DistortionBlurRemapMax; +float _BlendMode; // Caution: C# code in BaseLitUI.cs call LightmapEmissionFlagsProperty() which assume that there is an existing "_EmissionColor" // value that exist to identify if the GI emission need to be enabled. diff --git a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl index e055fc79581..78684f3182c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl @@ -141,7 +141,7 @@ float4 VFXApplyFog(float4 color,float4 posCS,float3 posWS) float3 volColor, volOpacity; EvaluateAtmosphericScattering(posInput, V, volColor, volOpacity); // Premultiplied alpha - + #if VFX_BLENDMODE_ALPHA color.rgb = color.rgb * (1 - volOpacity) + volColor * color.a; #elif VFX_BLENDMODE_ADD diff --git a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXDefines.hlsl b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXDefines.hlsl index f18665dbebd..e75eeb8556a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXDefines.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXDefines.hlsl @@ -1,5 +1,17 @@ #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialBlendModeEnum.cs.hlsl" + +#if VFX_BLENDMODE_ALPHA + #define _BlendMode BLENDMODE_ALPHA +#elif VFX_BLENDMODE_ADD + #define _BlendMode BLENDMODE_ADDITIVE +#elif VFX_BLENDMODE_PREMULTIPLY + #define _BlendMode BLENDMODE_PREMULTIPLY +#else + //Opaque, doesn't really matter what we specify, but a definition is needed to avoid compilation errors. + #define _BlendMode BLENDMODE_ALPHA +#endif #if IS_TRANSPARENT_PARTICLE && !HDRP_LIT // Fog for opaque is handled in a dedicated pass #define USE_FOG 1 diff --git a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXLit.hlsl b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXLit.hlsl index 52c493d8436..03fb93a5a65 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXLit.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXLit.hlsl @@ -159,4 +159,4 @@ SurfaceData VFXGetSurfaceData(const VFX_VARYING_PS_INPUTS i, float3 normalWS,con } -#endif \ No newline at end of file +#endif