浏览代码

HDRenderPipeline: Remove multiply blend mode

- Multiply make it easy to do mistake (inf, negative number, NaN) and
prevent optimization like half res rendering or offscreen compositing
/main
Sebastien Lagarde 7 年前
当前提交
8e92ddef
共有 8 个文件被更改,包括 13 次插入21 次删除
  1. 4
      ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLit.shader
  2. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLitTessellation.shader
  3. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.shader
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitTessellation.shader
  5. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl
  6. 7
      ScriptableRenderPipeline/HDRenderPipeline/Material/Unlit/Editor/BaseUnlitUI.cs
  7. 4
      ScriptableRenderPipeline/HDRenderPipeline/Material/Unlit/Unlit.shader
  8. 4
      Tests/GraphicsTests/RenderPipeline/LightweightPipeline/Scenes/037_Particles/Scripts.meta

4
ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLit.shader


// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_MULTIPLY _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _BLENDMODE_PRESERVE_SPECULAR_LIGHTING
#pragma shader_feature _ENABLE_FOG_ON_TRANSPARENT

#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
#define SHADERPASS SHADERPASS_FORWARD
#include "../../ShaderVariables.hlsl"
#include "../../ShaderVariables.hlsl"
#include "../../Lighting/Lighting.hlsl"
#include "../Lit/ShaderPass/LitSharePass.hlsl"
#include "LayeredLitData.hlsl"

2
ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLitTessellation.shader


// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_MULTIPLY _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _BLENDMODE_PRESERVE_SPECULAR_LIGHTING
#pragma shader_feature _ENABLE_FOG_ON_TRANSPARENT

2
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.shader


// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_MULTIPLY _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _BLENDMODE_PRESERVE_SPECULAR_LIGHTING
#pragma shader_feature _ENABLE_FOG_ON_TRANSPARENT

2
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitTessellation.shader


// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_MULTIPLY _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _BLENDMODE_PRESERVE_SPECULAR_LIGHTING
#pragma shader_feature _ENABLE_FOG_ON_TRANSPARENT

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl


// 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_MULTIPLY, _BLENDMODE_PRE_MULTIPLY for blend mode
// - _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

// 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_MULTIPLY) || defined(_BLENDMODE_PRE_MULTIPLY)
#else // defined(_BLENDMODE_PRE_MULTIPLY)
#else // defined(_BLENDMODE_MULTIPLY) || defined(_BLENDMODE_PRE_MULTIPLY)
#else // defined(_BLENDMODE_PRE_MULTIPLY)
return float4(diffuseLighting + specularLighting, opacity);
#endif
#endif

#elif defined(_BLENDMODE_ADD)
// For additive, we just need to fade to black with fog density (black + background == background color == fog color)
result.rgb = result.rgb * (1.0 - fog.a);
#elif defined(_BLENDMODE_MULTIPLY)
// For multiplicative, we just need to fade to white with fog density (white * background == background color == fog color)
result.rgb = lerp(result.rgb, float3(1.0, 1.0, 1.0), fog.a);
#elif defined(_BLENDMODE_PRE_MULTIPLY)
// 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, fog.rgb * result.a, fog.a);

7
ScriptableRenderPipeline/HDRenderPipeline/Material/Unlit/Editor/BaseUnlitUI.cs


{
Alpha = 0,
Additive = 1,
Multiplicative = 3,
PremultipliedAlpha = 4
}

protected const string kEnableFogOnTransparent = "_EnableFogOnTransparent";
protected MaterialProperty enableBlendModePreserveSpecularLighting = null;
protected const string kEnableBlendModePreserveSpecularLighting = "_EnableBlendModePreserveSpecularLighting";
// See comment in LitProperties.hlsl
const string kEmissionColor = "_EmissionColor";

preRefractionPass = FindProperty(kPreRefractionPass, props, false);
enableFogOnTransparent = FindProperty(kEnableFogOnTransparent, props, false);
enableBlendModePreserveSpecularLighting = FindProperty(kEnableBlendModePreserveSpecularLighting, props, false);
enableBlendModePreserveSpecularLighting = FindProperty(kEnableBlendModePreserveSpecularLighting, props, false);
}
void SurfaceTypePopup()

// These need to always been set either with opaque or transparent! So a users can switch to opaque and remove the keyword correctly
SetKeyword(material, "_BLENDMODE_ALPHA", false);
SetKeyword(material, "_BLENDMODE_ADD", false);
SetKeyword(material, "_BLENDMODE_MULTIPLY", false);
SetKeyword(material, "_BLENDMODE_PRE_MULTIPLY", false);
if (surfaceType == SurfaceType.Opaque)

SetKeyword(material, "_BLENDMODE_ALPHA", BlendMode.Alpha == blendMode);
SetKeyword(material, "_BLENDMODE_ADD", BlendMode.Additive == blendMode);
SetKeyword(material, "_BLENDMODE_MULTIPLY", BlendMode.Multiplicative == blendMode);
SetKeyword(material, "_BLENDMODE_PRE_MULTIPLY", BlendMode.PremultipliedAlpha == blendMode);
switch (blendMode)

4
ScriptableRenderPipeline/HDRenderPipeline/Material/Unlit/Unlit.shader


// In our case we don't use such a mechanism but need to keep the code quiet. We declare the value and always enable it.
// TODO: Fix the code in legacy unity so we can customize the beahvior for GI
_EmissionColor("Color", Color) = (1, 1, 1)
// HACK: GI Baking system relies on some properties existing in the shader ("_MainTex", "_Cutoff" and "_Color") for opacity handling, so we need to store our version of those parameters in the hard-coded name the GI baking system recognizes.
_MainTex("Albedo", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)

// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_MULTIPLY _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
#pragma shader_feature _ENABLE_FOG_ON_TRANSPARENT
//-------------------------------------------------------------------------------------

4
Tests/GraphicsTests/RenderPipeline/LightweightPipeline/Scenes/037_Particles/Scripts.meta


fileFormatVersion: 2
guid: e475c42352524461ca0371d90bbca97f
guid: c6fe8859c4c681d428c024202d9f27b4
timeCreated: 1509363645
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
正在加载...
取消
保存