浏览代码

HDRenderLoop: Add distortion pass control + depth offset support

/main
Sebastien Lagarde 8 年前
当前提交
d1bf008e
共有 17 个文件被更改,包括 362 次插入54 次删除
  1. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Debug/Resources/DebugViewTiles.shader
  2. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs
  3. 7
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  4. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Attributes.hlsl
  5. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Builtin/BuiltinData.cs
  6. 7
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Builtin/BuiltinData.hlsl
  7. 48
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/LayeredLit/LayeredLit.shader
  8. 76
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/BaseLitUI.cs
  9. 48
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.shader
  10. 26
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitData.hlsl
  11. 41
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitVelocityPass.hlsl
  12. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassGBuffer.hlsl
  13. 2
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  14. 94
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitDistortionPass.hlsl
  15. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitDistortionPass.hlsl.meta
  16. 23
      Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDistortion.hlsl
  17. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDistortion.hlsl.meta

4
Assets/ScriptableRenderLoop/HDRenderLoop/Debug/Resources/DebugViewTiles.shader


int2 coord = pixCoord - int2(1, 1);
float4 color = float4(pow(col.xyz, 2.2), 0.3*col.w);
float4 color = float4(PositivePow(col.xyz, 2.2), 0.3 * col.w);
if (numLights > 0)
{
if (SampleDebugFontNumber(coord, numLights)) // Shadow

UpdatePositionInput(depth, _InvViewProjMatrix, _ViewProjMatrix, posInput);
int2 pixelCoord = posInput.unPositionSS.xy;
int2 tileCoord = pixelCoord / TILE_SIZE;
int2 tileCoord = (float2)pixelCoord / TILE_SIZE;
int2 mouseTileCoord = _MousePixelCoord / TILE_SIZE;
int2 offsetInTile = pixelCoord - tileCoord * TILE_SIZE;

4
Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs


public readonly GUIContent useForwardRenderingOnly = new GUIContent("Use Forward Rendering Only");
public readonly GUIContent useDepthPrepass = new GUIContent("Use Depth Prepass");
public readonly GUIContent useDistortion = new GUIContent("Use Distortion");
public bool isDebugViewMaterialInit = false;
public GUIContent[] debugViewMaterialStrings = null;

debugParameters.displayTransparentObjects = EditorGUILayout.Toggle(styles.displayTransparentObjects, debugParameters.displayTransparentObjects);
debugParameters.useForwardRenderingOnly = EditorGUILayout.Toggle(styles.useForwardRenderingOnly, debugParameters.useForwardRenderingOnly);
debugParameters.useDepthPrepass = EditorGUILayout.Toggle(styles.useDepthPrepass, debugParameters.useDepthPrepass);
debugParameters.useDistortion = EditorGUILayout.Toggle(styles.useDistortion, debugParameters.useDistortion);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(renderLoop); // Repaint

7
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


public bool useForwardRenderingOnly = false; // TODO: Currently there is no way to strip the extra forward shaders generated by the shaders compiler, so we can switch dynamically.
public bool useDepthPrepass = false;
public bool useDistortion = true;
public bool enableTonemap = true;
public float exposure = 0;

void RenderDistortion(CullResults cullResults, Camera camera, RenderLoop renderLoop)
{
if (!debugParameters.useDistortion)
return ;
using (new Utilities.ProfilingSample("Distortion Pass", renderLoop))
{
int w = camera.pixelWidth;

cmd.GetTemporaryRT(m_DistortionBuffer, w, h, 0, FilterMode.Point, Builtin.RenderLoop.GetDistortionBufferFormat(), Builtin.RenderLoop.GetDistortionBufferReadWrite());
cmd.SetRenderTarget(m_DistortionBufferRT, m_CameraDepthBufferRT);
cmd.ClearRenderTarget(false, true, Color.black); // TODO: can we avoid this clear for performance ?
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();

// Rendering distortion here have off course lot of artifact.
// But resolving at each objects that write in distortion is not possible (need to sort transparent, render those that do not distort, then resolve, then etc...)
// Instead we chose to apply distortion at the end after we cumulate distortion vector and desired blurriness. This
// RenderDistortion(cullResults, camera, renderLoop);
RenderDistortion(cullResults, camera, renderLoop);
FinalPass(renderLoop);
}

6
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Attributes.hlsl


bool isFrontFace;
};
void ApplyDepthOffsetVS(float depthOffset, inout FragInputs fragInput)
void ApplyDepthOffsetAttribute(float depthOffsetVS, inout FragInputs fragInput)
fragInput.positionCS.w += depthOffset;
fragInput.previousPositionCS.w += depthOffset;
fragInput.positionCS.w += depthOffsetVS;
fragInput.previousPositionCS.w += depthOffsetVS;
}
void GetVaryingsDataDebug(uint paramId, FragInputs input, inout float3 result, inout bool needLinearToSRGB)

3
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Builtin/BuiltinData.cs


public static RenderTextureFormat GetDistortionBufferFormat()
{
return RenderTextureFormat.ARGBHalf; // This format need to be blendable and include distortionBlur
// TODO: // This format need to be additive blendable and include distortionBlur, blend mode different for alpha value
return RenderTextureFormat.ARGBHalf;
}
public static RenderTextureReadWrite GetDistortionBufferReadWrite()

7
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Builtin/BuiltinData.hlsl


void EncodeDistortion(float2 distortion, float distortionBlur, out float4 outBuffer)
{
// RT - 16:16 float
outBuffer = float4(distortion, distortionBlur, 0.0);
// RT - 16:16:16:16 float
// distortionBlur in alpha for a different blend mode
outBuffer = float4(distortion, 0.0, distortionBlur);
distortionBlur = inBuffer.z;
distortionBlur = inBuffer.a;
}
void GetBuiltinDataDebug(uint paramId, BuiltinData builtinData, inout float3 result, inout bool needLinearToSRGB)

48
Assets/ScriptableRenderLoop/HDRenderLoop/Material/LayeredLit/LayeredLit.shader


_LayerMaskMap("LayerMaskMap", 2D) = "white" {}
[ToggleOff] _LayerMaskVertexColor("Use Vertex Color Mask", Float) = 0.0
_DistortionVectorMap("DistortionVectorMap", 2D) = "black" {}
[ToggleOff] _DistortionOnly("Distortion Only", Float) = 0.0
[ToggleOff] _DistortionDepthTest("Distortion Only", Float) = 0.0
[ToggleOff] _DistortionEnable("Enable Distortion", Float) = 0.0
[ToggleOff] _DistortionOnly("Distortion Only", Float) = 0.0
[ToggleOff] _DistortionDepthTest("Distortion Depth Test Enable", Float) = 0.0
[ToggleOff] _DepthOffsetEnable("Depth Offset View space", Float) = 0.0
[ToggleOff] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0
[ToggleOff] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0
_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
[HideInInspector] _CullMode("__cullmode", Float) = 2.0
[HideInInspector] _ZTestMode("_ZTestMode", Int) = 8
[Enum(None, 0, DoubleSided, 1, DoubleSidedLigthingFlip, 2, DoubleSidedLigthingMirror, 3)] _DoubleSidedMode("Double sided mode", Float) = 0

#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _DISTORTION_ON
#pragma shader_feature _DEPTHOFFSET_ON
#pragma shader_feature _ _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

TEXTURE2D(_DiffuseLightingMap);
SAMPLER2D(sampler_DiffuseLightingMap);
TEXTURE2D(_DistortionVectorMap);
SAMPLER2D(sampler_DistortionVectorMap);
TEXTURE2D(_LayerMaskMap);
SAMPLER2D(sampler_LayerMaskMap);

Cull[_CullMode]
ZTest LEqual
ZWrite Off // TODO: Test Z equal here.
HLSLPROGRAM

Cull[_CullMode]
ZWrite On ZTest LEqual
ZWrite On
ZTest LEqual
HLSLPROGRAM

Cull[_CullMode]
ZWrite On ZTest LEqual
ZWrite On
HLSLPROGRAM

#include "../Lit/LitDepthPass.hlsl"
#include "../../ShaderPass/ShaderPassDepthOnly.hlsl"
ENDHLSL
}
Pass
{
Name "Distortion" // Name is not used
Tags { "LightMode" = "DistortionVectors" } // This will be only for transparent object based on the RenderQueue index
Blend One One
ZTest [_ZTestMode]
ZWrite off
Cull [_CullMode]
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#define SHADERPASS SHADERPASS_DISTORTION
#define LAYERED_LIT_SHADER
#include "../../Material/Material.hlsl"
#include "LitData.hlsl"
#include "LitDistortionPass.hlsl"
#include "../../ShaderPass/ShaderPassDistortion.hlsl"
ENDHLSL
}

76
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/BaseLitUI.cs


public static GUIContent alphaCutoffEnableText = new GUIContent("Alpha Cutoff Enable", "Threshold for alpha cutoff");
public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
public static GUIContent doubleSidedModeText = new GUIContent("Double Sided", "This will render the two face of the objects (disable backface culling)");
public static GUIContent distortionEnableText = new GUIContent("Distortion", "Enable distortion on this shader");
public static GUIContent distortionOnlyText = new GUIContent("Distortion Only", "This shader will only be use to render distortion");
public static GUIContent distortionDepthTestText = new GUIContent("Distortion Depth Test", "Enable the depth test for distortion");
public static GUIContent depthOffsetEnableText = new GUIContent("DepthOffset", "EnableDepthOffset on this shader (Use with heightmap)");
public static readonly string[] surfaceTypeNames = Enum.GetNames(typeof(SurfaceType));
public static readonly string[] blendModeNames = Enum.GetNames(typeof(BlendMode));

if ((SurfaceType)surfaceType.floatValue == SurfaceType.Transparent)
{
BlendModePopup();
m_MaterialEditor.ShaderProperty(distortionEnable, Styles.distortionEnableText.text);
if (distortionEnable.floatValue == 1.0)
{
m_MaterialEditor.ShaderProperty(distortionOnly, Styles.distortionOnlyText.text);
m_MaterialEditor.ShaderProperty(distortionDepthTest, Styles.distortionDepthTestText.text);
}
}
m_MaterialEditor.ShaderProperty(alphaCutoffEnable, Styles.alphaCutoffEnableText.text);
if (alphaCutoffEnable.floatValue == 1.0)

m_MaterialEditor.ShaderProperty(doubleSidedMode, Styles.doubleSidedModeText.text);
m_MaterialEditor.ShaderProperty(depthOffsetEnable, Styles.depthOffsetEnableText.text);
EditorGUI.indentLevel--;
}

alphaCutoff = FindProperty(kAlphaCutoff, props);
alphaCutoffEnable = FindProperty(kAlphaCutoffEnabled, props);
doubleSidedMode = FindProperty(kDoubleSidedMode, props);
distortionEnable = FindProperty(kDistortionEnable, props);
distortionOnly = FindProperty(kDistortionOnly, props);
distortionDepthTest = FindProperty(kDistortionDepthTest, props);
depthOffsetEnable = FindProperty(kDepthOffsetEnable, props);
}
protected void SetupCommonOptionsKeywords(Material material)

BlendMode blendMode = (BlendMode)material.GetFloat(kBlendMode);
DoubleSidedMode doubleSidedMode = (DoubleSidedMode)material.GetFloat(kDoubleSidedMode);
if (surfaceType == SurfaceType.Opaque)
{
material.SetOverrideTag("RenderType", alphaTestEnable ? "TransparentCutout" : "");

SetKeyword(material, "_ALPHATEST_ON", alphaTestEnable);
bool distortionEnable = material.GetFloat(kDistortionEnable) == 1.0;
bool distortionOnly = material.GetFloat(kDistortionOnly) == 1.0;
bool distortionDepthTest = material.GetFloat(kDistortionDepthTest) == 1.0;
bool depthOffsetEnable = material.GetFloat(kDepthOffsetEnable) == 1.0;
if (distortionEnable)
{
material.SetShaderPassEnabled("DistortionVectors", true);
}
else
{
material.SetShaderPassEnabled("DistortionVectors", false);
}
if (distortionEnable && distortionOnly)
{
// Disable all passes except dbug material
material.SetShaderPassEnabled("GBuffer", false);
material.SetShaderPassEnabled("DebugViewMaterial", true);
material.SetShaderPassEnabled("Meta", false);
material.SetShaderPassEnabled("ShadowCaster", false);
material.SetShaderPassEnabled("DepthOnly", false);
material.SetShaderPassEnabled("MotionVectors", false);
material.SetShaderPassEnabled("Forward", false);
}
else
{
material.SetShaderPassEnabled("GBuffer", true);
material.SetShaderPassEnabled("DebugViewMaterial", true);
material.SetShaderPassEnabled("Meta", true);
material.SetShaderPassEnabled("ShadowCaster", true);
material.SetShaderPassEnabled("DepthOnly", true);
material.SetShaderPassEnabled("MotionVectors", true);
material.SetShaderPassEnabled("Forward", true);
}
if (distortionDepthTest)
{
material.SetInt("_ZTestMode", (int)UnityEngine.Rendering.CompareFunction.LessEqual);
}
else
{
material.SetInt("_ZTestMode", (int)UnityEngine.Rendering.CompareFunction.Always);
}
SetKeyword(material, "_DISTORTION_ON", distortionEnable);
SetKeyword(material, "_DEPTHOFFSET_ON", depthOffsetEnable);
SetupEmissionGIFlags(material);
}

MaterialProperty blendMode = null;
MaterialProperty alphaCutoff = null;
MaterialProperty doubleSidedMode = null;
MaterialProperty distortionEnable = null;
MaterialProperty distortionOnly = null;
MaterialProperty distortionDepthTest = null;
MaterialProperty depthOffsetEnable = null;
const string kDistortionEnable = "_DistortionEnable";
const string kDistortionOnly = "_DistortionOnly";
const string kDistortionDepthTest = "_DistortionDepthTest";
const string kDepthOffsetEnable = "_DepthOffsetEnable";
protected static string[] reservedProperties = new string[] { kSurfaceType, kBlendMode, kAlphaCutoff, kAlphaCutoffEnabled, kDoubleSidedMode };
protected abstract void FindMaterialProperties(MaterialProperty[] props);

48
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.shader


//_CoatRoughness("CoatRoughness", Range(0.0, 1.0)) = 0
//_CoatRoughnessMap("CoatRoughnessMap", 2D) = "white" {}
// _DistortionVectorMap("DistortionVectorMap", 2D) = "white" {}
// _DistortionBlur("DistortionBlur", Range(0.0, 1.0)) = 0
_DistortionVectorMap("DistortionVectorMap", 2D) = "black" {}
// Following options are for the GUI inspector and different from the input parameters above
// These option below will cause different compilation flag.

_EmissiveIntensity("EmissiveIntensity", Float) = 0
[ToggleOff] _DistortionOnly("Distortion Only", Float) = 0.0
[ToggleOff] _DistortionDepthTest("Distortion Only", Float) = 0.0
[ToggleOff] _DistortionEnable("Enable Distortion", Float) = 0.0
[ToggleOff] _DistortionOnly("Distortion Only", Float) = 0.0
[ToggleOff] _DistortionDepthTest("Distortion Depth Test Enable", Float) = 0.0
[ToggleOff] _DepthOffsetEnable("Depth Offset View space", Float) = 0.0
[ToggleOff] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0
_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
[HideInInspector] _CullMode("__cullmode", Float) = 2.0
[HideInInspector] _ZTestMode("_ZTestMode", Int) = 8
// Material Id
[HideInInspector] _MaterialId("_MaterialId", FLoat) = 0

//-------------------------------------------------------------------------------------
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _DISTORTION_ON
#pragma shader_feature _DEPTHOFFSET_ON
#pragma shader_feature _ _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

TEXTURE2D(_DiffuseLightingMap);
SAMPLER2D(sampler_DiffuseLightingMap);
TEXTURE2D(_DistortionVectorMap);
SAMPLER2D(sampler_DistortionVectorMap);
float3 _EmissiveColor;
TEXTURE2D(_EmissiveColorMap);
SAMPLER2D(sampler_EmissiveColorMap);

Cull[_CullMode]
ZWrite On ZTest LEqual
ZWrite On
ZTest LEqual
HLSLPROGRAM

Cull[_CullMode]
ZWrite On ZTest LEqual
ZWrite On
HLSLPROGRAM
#pragma vertex Vert

Cull[_CullMode]
ZTest LEqual
ZWrite Off // TODO: Test Z equal here.
HLSLPROGRAM

#include "LitVelocityPass.hlsl"
#include "../../ShaderPass/ShaderPassVelocity.hlsl"
ENDHLSL
}
Pass
{
Name "Distortion" // Name is not used
Tags { "LightMode" = "DistortionVectors" } // This will be only for transparent object based on the RenderQueue index
Blend One One
ZTest [_ZTestMode]
ZWrite off
Cull [_CullMode]
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#define SHADERPASS SHADERPASS_DISTORTION
#include "../../Material/Material.hlsl"
#include "LitData.hlsl"
#include "LitDistortionPass.hlsl"
#include "../../ShaderPass/ShaderPassDistortion.hlsl"
ENDHLSL
}

26
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitData.hlsl


//-------------------------------------------------------------------------------------
#include "../MaterialUtilities.hlsl"
void GetBuiltinData(FragInputs input, SurfaceData surfaceData, float alpha, out BuiltinData builtinData)
void GetBuiltinData(FragInputs input, SurfaceData surfaceData, float alpha, float depthOffset, out BuiltinData builtinData)
{
// Builtin Data
builtinData.opacity = alpha;

builtinData.velocity = CalculateVelocity(input.positionCS, input.previousPositionCS);
#ifdef _DISTORTION_ON
float3 distortion = SAMPLE_TEXTURE2D(_DistortionVectorMap, sampler_DistortionVectorMap, input.texCoord0).rgb;
builtinData.distortion = distortion.rg;
builtinData.distortionBlur = distortion.b;
#else
#endif
builtinData.depthOffset = 0.0;
builtinData.depthOffset = depthOffset;
}
// Gather all kind of mapping in one struct, allow to improve code readability

// Transform view vector in tangent space
float3 viewDirTS = TransformWorldToTangent(V, input.tangentToWorld);
ApplyDisplacement(input, viewDirTS, layerTexCoord);
float depthOffset = 0.0;
#ifdef _DEPTHOFFSET_ON
ApplyDepthOffsetPositionInput(V, builtinData.depthOffset, posInput);
ApplyDepthOffsetAttribute(depthOffset, input);
#endif
GetBuiltinData(input, surfaceData, alpha, builtinData);
GetBuiltinData(input, surfaceData, alpha, depthOffset, builtinData);
}
#else

ApplyDisplacement1(input, viewDirTS, layerTexCoord);
ApplyDisplacement2(input, viewDirTS, layerTexCoord);
ApplyDisplacement3(input, viewDirTS, layerTexCoord);
float depthOffset = 0.0;
#ifdef _DEPTHOFFSET_ON
ApplyDepthOffsetPositionInput(V, builtinData.depthOffset, posInput);
ApplyDepthOffsetAttribute(depthOffset, input);
#endif
SurfaceData surfaceData0;
SurfaceData surfaceData1;

surfaceData.specularColor = float3(0.0, 0.0, 0.0);
float alpha = PROP_BLEND_SCALAR(alpha, weights);
GetBuiltinData(input, surfaceData, alpha, builtinData);
GetBuiltinData(input, surfaceData, alpha, depthOffset, builtinData);
}
#endif // #ifndef LAYERED_LIT_SHADER

41
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitVelocityPass.hlsl


struct Varyings
{
float4 positionCS;
float3 positionWS;
// Note: Z component is not use
float4 transferPositionCS;
float4 transferPreviousPositionCS;

#if NEED_TANGENT_TO_WORLD
float3 positionWS;
float3 tangentToWorld[3];
#endif
};

#if NEED_TANGENT_TO_WORLD
float4 interpolators[5] : TEXCOORD0;
#elif NEED_TEXCOORD0
float4 interpolators[2] : TEXCOORD0;
float4 interpolators[3] : TEXCOORD0;
float4 interpolators[2] : TEXCOORD0;
float4 interpolators[3] : TEXCOORD0;
#endif
};

PackedVaryings output;
output.positionCS = input.positionCS;
output.interpolators[0] = float4(input.transferPositionCS.xyw, 0.0);
output.interpolators[1] = float4(input.transferPreviousPositionCS.xyw, 0.0);
output.interpolators[0] = float4(input.positionWS, 0.0);
output.interpolators[1] = float4(input.transferPositionCS.xyw, 0.0);
output.interpolators[2] = float4(input.transferPreviousPositionCS.xyw, 0.0);
output.interpolators[2].xyz = input.tangentToWorld[0];
output.interpolators[3].xyz = input.tangentToWorld[1];
output.interpolators[4].xyz = input.tangentToWorld[2];
output.interpolators[3].xyz = input.tangentToWorld[0];
output.interpolators[4].xyz = input.tangentToWorld[1];
output.interpolators[2].w = input.positionWS.x;
output.interpolators[3].w = input.positionWS.y;
output.interpolators[4].w = input.positionWS.z;
output.interpolators[2].w = input.tangentToWorld[2].x;
output.interpolators[3].w = input.tangentToWorld[2].y;
output.interpolators[4].w = input.tangentToWorld[2].z;
#elif NEED_TEXCOORD0
output.interpolators[0].w = input.texCoord0.x;
output.interpolators[1].w = input.texCoord0.y;

ZERO_INITIALIZE(FragInputs, output);
output.unPositionSS = input.positionCS; // input.positionCS is SV_Position
output.positionCS = float4(input.interpolators[0].xy, 0.0, input.interpolators[0].z);
output.previousPositionCS = float4(input.interpolators[1].xy, 0.0, input.interpolators[1].z);
output.positionWS = input.interpolators[0].xyz;
output.positionCS = float4(input.interpolators[1].xy, 0.0, input.interpolators[1].z);
output.previousPositionCS = float4(input.interpolators[1].xy, 0.0, input.interpolators[2].z);
output.positionWS.xyz = float2(input.interpolators[2].w, input.interpolators[3].w, input.interpolators[4].w);
output.tangentToWorld[0] = input.interpolators[2].xyz;
output.tangentToWorld[1] = input.interpolators[3].xyz;
output.tangentToWorld[2] = input.interpolators[4].xyz;
output.tangentToWorld[0] = input.interpolators[3].xyz;
output.tangentToWorld[1] = input.interpolators[4].xyz;
output.tangentToWorld[2] = float3(input.interpolators[2].w, input.interpolators[3].w, input.interpolators[4].w);
#elif NEED_TEXCOORD0
output.texCoord0.xy = float2(input.interpolators[0].w, input.interpolators[1].w);
#endif

{
Varyings output;
float3 positionWS = TransformObjectToWorld(input.positionOS);
output.positionCS = TransformWorldToHClip(positionWS);
output.positionWS = TransformObjectToWorld(input.positionOS);
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow steps transform to clip space + instancing matrix)
output.positionCS = TransformWorldToHClip(output.positionWS);
// TODO: Clean this code, put in function ?
output.transferPositionCS = mul(_NonJitteredVP, mul(unity_ObjectToWorld, float4(input.positionOS, 1.0)));

#endif
#if NEED_TANGENT_TO_WORLD
output.positionWS = positionWS;
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);

9
Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassGBuffer.hlsl


void Frag( PackedVaryings packedInput,
OUTPUT_GBUFFER(outGBuffer)
OUTPUT_GBUFFER_VELOCITY(outVelocityBuffer)
#ifdef _DEPTHOFFSET_ON
float outputDepth : SV_Depth
#endif
)
{
FragInputs input = UnpackVaryings(packedInput);

PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);
float3 bakeDiffuseLighting = GetBakedDiffuseLigthing(surfaceData, builtinData, bsdfData, preLightData);
ENCODE_INTO_GBUFFER(surfaceData, bakeDiffuseLighting, outGBuffer);
ENCODE_INTO_GBUFFER(surfaceData, bakeDiffuseLighting, outGBuffer);
#ifdef _DEPTHOFFSET_ON
outputDepth = posInput.rawDepth;
#endif
}

2
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


}
// depthOffsetVS is always in the direction of the view vector (V)
void ApplyDepthOffsetVS(float V, float depthOffsetVS, inout PositionInputs posInput)
void ApplyDepthOffsetPositionInput(float V, float depthOffsetVS, inout PositionInputs posInput)
{
posInput.depthVS += depthOffsetVS;
// TODO: it is an approx, need a correct value where we use projection matrix to reproject the depth from VS

94
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitDistortionPass.hlsl


#ifndef SHADERPASS
#error Undefine_SHADERPASS
#endif
#define NEED_TANGENT_TO_WORLD NEED_TEXCOORD0 && (defined(_HEIGHTMAP) && !defined(_HEIGHTMAP_AS_DISPLACEMENT))
struct Attributes
{
float3 positionOS : POSITION;
float2 uv0 : TEXCOORD0;
#if NEED_TANGENT_TO_WORLD
float4 tangentOS : TANGENT;
#endif
};
struct Varyings
{
float4 positionCS;
float3 positionWS;
float2 texCoord0;
#if NEED_TANGENT_TO_WORLD
float3 tangentToWorld[3];
#endif
};
struct PackedVaryings
{
float4 positionCS : SV_Position;
#if NEED_TANGENT_TO_WORLD
float4 interpolators[4] : TEXCOORD0;
#else
float4 interpolators[2] : TEXCOORD0;
#endif
};
// Function to pack data to use as few interpolator as possible, the ShaderGraph should generate these functions
PackedVaryings PackVaryings(Varyings input)
{
PackedVaryings output;
output.positionCS = input.positionCS;
output.interpolators[0] = float4(input.positionWS, 0.0);
output.interpolators[0].w = input.texCoord0.x;
output.interpolators[1] = float4(0.0, 0.0, 0.0, input.texCoord0.y);
#if NEED_TANGENT_TO_WORLD
output.interpolators[1].xyz = input.tangentToWorld[0];
output.interpolators[2].xyz = input.tangentToWorld[1];
output.interpolators[3].xyz = input.tangentToWorld[2];
#endif
return output;
}
FragInputs UnpackVaryings(PackedVaryings input)
{
FragInputs output;
ZERO_INITIALIZE(FragInputs, output);
output.unPositionSS = input.positionCS; // input.positionCS is SV_Position
output.positionWS = input.interpolators[0].xyz;
#if NEED_TANGENT_TO_WORLD
output.texCoord0.xy = float2(input.interpolators[0].w, input.interpolators[1].w);
output.tangentToWorld[0] = input.interpolators[1].xyz;
output.tangentToWorld[1] = input.interpolators[2].xyz;
output.tangentToWorld[2] = input.interpolators[3].xyz;
#endif
return output;
}
PackedVaryings Vert(Attributes input)
{
Varyings output;
output.positionWS = TransformObjectToWorld(input.positionOS);
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow steps transform to clip space + instancing matrix)
output.positionCS = TransformWorldToHClip(output.positionWS);
output.texCoord0 = input.uv0;
#if NEED_TANGENT_TO_WORLD
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0] = tangentToWorld[0];
output.tangentToWorld[1] = tangentToWorld[1];
output.tangentToWorld[2] = tangentToWorld[2];
#endif
return PackVaryings(output);
}

9
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitDistortionPass.hlsl.meta


fileFormatVersion: 2
guid: a89db0bb66712eb4287e14bbafacfa15
timeCreated: 1481762668
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

23
Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDistortion.hlsl


#if SHADERPASS != SHADERPASS_DISTORTION
#error SHADERPASS_is_not_correctly_define
#endif
float4 Frag(PackedVaryings packedInput) : SV_Target
{
FragInputs input = UnpackVaryings(packedInput);
// input.unPositionSS is SV_Position
PositionInputs posInput = GetPositionInput(input.unPositionSS.xy, _ScreenSize.zw);
UpdatePositionInput(input.unPositionSS.z, input.unPositionSS.w, input.positionWS, posInput);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
// Perform alpha testing + get distortion
SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
float4 outBuffer;
EncodeDistortion(builtinData.distortion, builtinData.distortionBlur, outBuffer);
return outBuffer;
}

9
Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDistortion.hlsl.meta


fileFormatVersion: 2
guid: b6e11447d75c89a4e99bee65db7c78f5
timeCreated: 1481762668
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存