浏览代码

Merge pull request #9 from Unity-Technologies/VelocityBufferSupport

Velocity support
/main
GitHub 8 年前
当前提交
0d79a150
共有 23 个文件被更改,包括 445 次插入157 次删除
  1. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta
  2. 73
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  3. 40
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs
  4. 5
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs.hlsl
  5. 31
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.hlsl
  6. 39
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/LayeredLit/LayeredLitDefault.shader
  7. 18
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/Lit.cs
  8. 27
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitData.hlsl
  9. 39
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitDefault.shader
  10. 13
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitDepthPass.hlsl
  11. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitMetaPass.hlsl
  12. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitSharePass.hlsl
  13. 98
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl
  14. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderConfig.cs
  15. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassGBuffer.hlsl
  16. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl
  17. 5
      Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs.hlsl
  18. 124
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitVelocityPass.hlsl
  19. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitVelocityPass.hlsl.meta
  20. 18
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassVelocity.hlsl
  21. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassVelocity.hlsl.meta
  22. 13
      Assets/HDRenderLoop.asset
  23. 8
      Assets/HDRenderLoop.asset.meta

2
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta


fileFormatVersion: 2
guid: 2400b74f5ce370c4481e5dc417d03703
timeCreated: 1477406009
timeCreated: 1478744315
licenseType: Pro
NativeFormatImporter:
userData:

73
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


// Must be in sync with ShaderConfig.cs
//#define VELOCITY_IN_GBUFFER
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using System.Collections.Generic;

public bool displayOpaqueObjects = true;
public bool displayTransparentObjects = true;
public bool useForwardRenderingOnly = false;
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 enableTonemap = true;
public float exposure = 0;

// TODO: Find a way to automatically create/iterate through these kind of class
Lit.RenderLoop m_LitRenderLoop;
Builtin.RenderLoop m_BuiltinRenderLoop;
// Debug
Material m_DebugViewMaterialGBuffer;

static private int s_CameraColorBuffer;
static private int s_CameraDepthBuffer;
static private int s_VelocityBuffer;
static private ComputeBuffer s_punctualLightList;
static private ComputeBuffer s_envLightList;

// Init Lit material buffer - GBuffer and init
m_LitRenderLoop = new Lit.RenderLoop(); // Our object can be garbacge collected, so need to be allocate here
m_BuiltinRenderLoop = new Builtin.RenderLoop();
for (int gbufferIndex = 0; gbufferIndex < m_LitRenderLoop.GetGBufferCount(); ++gbufferIndex)
{
m_gbufferManager.SetBufferDescription(gbufferIndex, "_CameraGBufferTexture" + gbufferIndex, m_LitRenderLoop.RTFormat[gbufferIndex], m_LitRenderLoop.RTReadWrite[gbufferIndex]);
}
m_gbufferManager.gbufferCount = m_LitRenderLoop.GetGBufferCount();
m_gbufferManager.gbufferCount = m_LitRenderLoop.GetGBufferCount();
for (int gbufferIndex = 0; gbufferIndex < m_gbufferManager.gbufferCount; ++gbufferIndex)
// In forward we still name the buffer as if they were gbuffer, it doesn't matter
for (int gbufferIndex = 0; gbufferIndex < m_BuiltinRenderLoop.GetGBufferCount(); ++gbufferIndex)
m_gbufferManager.SetBufferDescription(gbufferIndex, "_CameraGBufferTexture" + gbufferIndex, m_LitRenderLoop.RTFormat[gbufferIndex], m_LitRenderLoop.RTReadWrite[gbufferIndex]);
int textureIndex = m_gbufferManager.gbufferCount + gbufferIndex;
m_gbufferManager.SetBufferDescription(textureIndex, "_CameraGBufferTexture" + textureIndex, m_BuiltinRenderLoop.RTFormat[gbufferIndex], m_BuiltinRenderLoop.RTReadWrite[gbufferIndex]);
m_gbufferManager.gbufferCount += m_BuiltinRenderLoop.GetGBufferCount();
// Caution velocity texture name must match with Macro in Material.hlsl
s_VelocityBuffer = Shader.PropertyToID("_VelocityTexture");
#if VELOCITY_IN_GBUFFER
m_gbufferManager.SetBufferDescription(m_gbufferManager.gbufferCount, "_VelocityTexture", m_BuiltinRenderLoop.GetVelocityBufferFormat(), m_BuiltinRenderLoop.GetVelocityBufferReadWrite());
m_gbufferManager.gbufferCount++;
#endif
m_BuiltinRenderLoop.Rebuild();
}
void OnDisable()

int w = camera.pixelWidth;
int h = camera.pixelHeight;
cmd.GetTemporaryRT(s_CameraColorBuffer, w, h, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Default);
cmd.GetTemporaryRT(s_CameraColorBuffer, w, h, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
m_gbufferManager.InitGBuffers(w, h, cmd);
if (!debugParameters.useForwardRenderingOnly)
{
m_gbufferManager.InitGBuffers(w, h, cmd);
}
cmd.SetRenderTarget(new RenderTargetIdentifier(s_CameraColorBuffer), new RenderTargetIdentifier(s_CameraDepthBuffer));
cmd.ClearRenderTarget(true, false, new Color(0, 0, 0, 0));

// END TEMP
}
// TODO: Create an opaque and transparent list!
void RenderOpaqueRenderList(CullResults cull, Camera camera, RenderLoop renderLoop, string passName)
{
if (!debugParameters.displayOpaqueObjects)

var settings = new DrawRendererSettings(cull, camera, new ShaderPassName(passName))
{
rendererConfiguration = RendererConfiguration.PerObjectLightProbe | RendererConfiguration.PerObjectReflectionProbes,
sorting = { sortOptions = SortOptions.SortByMaterialThenMesh }
rendererConfiguration = RendererConfiguration.PerObjectLightProbe | RendererConfiguration.PerObjectReflectionProbes | RendererConfiguration.PerObjectLightmaps | RendererConfiguration.PerObjectLightProbeProxyVolume,
sorting = { sortOptions = SortOptions.BackToFront }
};
settings.inputFilter.SetQueuesTransparent();
renderLoop.DrawRenderers(ref settings);

{
if (debugParameters.useForwardRenderingOnly)
{
return;
return ;
}
// Bind material data

RenderOpaqueRenderList(cullResults, camera, renderLoop, "ForwardUnlit");
RenderTransparentRenderList(cullResults, camera, renderLoop, "ForwardUnlit");
}
void RenderVelocity(CullResults cullResults, Camera camera, RenderLoop renderLoop)
{
int w = camera.pixelWidth;
int h = camera.pixelHeight;
var cmd = new CommandBuffer { name = "Velocity Pass" };
cmd.GetTemporaryRT(s_VelocityBuffer, w, h, 0, FilterMode.Point, m_BuiltinRenderLoop.GetVelocityBufferFormat(), m_BuiltinRenderLoop.GetVelocityBufferReadWrite());
cmd.SetRenderTarget(new RenderTargetIdentifier(s_VelocityBuffer), new RenderTargetIdentifier(s_CameraDepthBuffer));
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
RenderOpaqueRenderList(cullResults, camera, renderLoop, "MotionVectors");
}
void FinalPass(RenderLoop renderLoop)

RenderForward(cullResults, camera, renderLoop);
RenderForwardUnlit(cullResults, camera, renderLoop);
// If opaque velocity have been render during GBuffer no need to render them here
// TODO: What about forward opaque object that will come later, (with stencil bit to avoid double lighting), we need to
// also render their velocity inside the buffer. So It mean that when they are deferred opaque they should render during GBuffer pass
// but when they are forward they should render a second time here in velocity pass... Maybe we should enable MRT during forward pass ?
// TODO: implement MRT velocity buffer as an option in case of forward
// Render as late as possible to benefit from an up to date depth buffer (TODO: could use equal depth test ?)
#if VELOCITY_IN_GBUFFER
if (debugParameters.useForwardRenderingOnly)
#endif
RenderVelocity(cullResults, camera, renderLoop);
FinalPass(renderLoop);
}

// Post effects
}
#if UNITY_EDITOR
#if UNITY_EDITOR
public override UnityEditor.SupportedRenderingFeatures GetSupportedRenderingFeatures()
{
var features = new UnityEditor.SupportedRenderingFeatures

return features;
}
#endif
#endif
}
}

40
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs


public Vector3 diffuseColor;
public Vector3 emissiveColor;
};
[GenerateHLSL(PackingRules.Exact)]
public enum GBufferBuiltin
{
Count = 1
};
public class RenderLoop : Object
{
// Note: Velocity buffer must be the last buffer of a GBuffer pass if applicable
public RenderTextureFormat GetVelocityBufferFormat()
{
return RenderTextureFormat.RGHalf;
}
public RenderTextureReadWrite GetVelocityBufferReadWrite()
{
return RenderTextureReadWrite.Linear;
}
//-----------------------------------------------------------------------------
// GBuffer management
//-----------------------------------------------------------------------------
public int GetGBufferCount() { return (int)GBufferBuiltin.Count; }
public RenderTextureFormat[] RTFormat =
{
RenderTextureFormat.RGB111110Float
};
public RenderTextureReadWrite[] RTReadWrite =
{
RenderTextureReadWrite.Linear
};
public void Rebuild()
{
}
}
}
}

5
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs.hlsl


#define DEBUGVIEW_BUILTIN_LIGHTRANSPORTDATA_DIFFUSE_COLOR (120)
#define DEBUGVIEW_BUILTIN_LIGHTRANSPORTDATA_EMISSIVE_COLOR (121)
//
// UnityEngine.Experimental.ScriptableRenderLoop.Builtin.GBufferBuiltin: static fields
//
#define GBUFFERBUILTIN_COUNT (1)
// Generated from UnityEngine.Experimental.ScriptableRenderLoop.Builtin.BuiltinData
// PackingRules = Exact
struct BuiltinData

31
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.hlsl


#include "BuiltinData.cs.hlsl"
//-----------------------------------------------------------------------------
// common Encode/Decode functions
//-----------------------------------------------------------------------------
// Encode/Decode velocity in a buffer (either forward of deferred)
// Design note: We assume that VelocityVector fit into a single buffer (i.e not spread on several buffer)
void EncodeVelocity(float2 velocity, out float4 outBuffer)
{
// RT - 16:16 float
outBuffer = float4(velocity.xy, 0.0, 0.0);
}
float2 DecodeVelocity(float4 inBuffer)
{
return float2(inBuffer.xy);
}
// Encode/Decode into GBuffer - This is share so others material can use it.
// Design note: We assume that BakeDiffuseLighting and emissive fit into a single buffer (i.e not spread on several buffer)
void EncodeBakedDiffuseLigthingIntoGBuffer(float3 bakeDiffuseLighting, out float4 outBuffer)
{
// RT - 11:11:10f
outBuffer = float4(bakeDiffuseLighting.xyz, 0.0);
}
float3 DecodeBakedDiffuseLigthingFromGBuffer(float4 inBuffer)
{
return float3(inBuffer.xyz);
}
void GetBuiltinDataDebug(uint paramId, BuiltinData builtinData, inout float3 result, inout bool needLinearToSRGB)
{
switch (paramId)

39
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/LayeredLit/LayeredLitDefault.shader


Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
LOD 300
// ------------------------------------------------------------------
// Deferred pass
Pass
Pass
{
Name "GBuffer" // Name is not used
Tags { "LightMode" = "GBuffer" } // This will be only for opaque object based on the RenderQueue index

ENDHLSL
}
// ------------------------------------------------------------------
// Debug pass
Pass
{
Name "Debug"

ENDHLSL
}
// ------------------------------------------------------------------
// ------------------------------------------------------------------
Pass
{
Name "META"

ENDHLSL
}
// ------------------------------------------------------------------
// Depth only
// ------------------------------------------------------------------
Pass
{
Name "Motion Vectors"
Tags{ "LightMode" = "MotionVectors" } // Caution, this need to be call like this to setup the correct parameters by C++ (legacy Unity)
Cull[_CullMode]
ZTest LEqual
ZWrite Off // TODO: Test Z equal here.
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#define SHADERPASS SHADERPASS_VELOCITY
#define LAYERED_LIT_SHADER
#include "../../Material/Material.hlsl"
#include "../Lit/LitData.hlsl"
#include "../Lit/LitVelocityPass.hlsl"
#include "../../ShaderPass/ShaderPassVelocity.hlsl"
ENDHLSL
}
Pass
{
Name "ShadowCaster"

ENDHLSL
}
// ------------------------------------------------------------------
// forward pass
Pass
{
Name "Forward" // Name is not used

18
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/Lit.cs


// GBuffer management
//-----------------------------------------------------------------------------
#if (VELOCITY_IN_GBUFFER)
public const int s_GBufferCount = (int)GBufferMaterial.Count + 2; // +1 for emissive buffer
#else
public const int s_GBufferCount = (int)GBufferMaterial.Count + 1;
#endif
public int GetGBufferCount() { return s_GBufferCount; }
public int GetGBufferCount() { return (int)GBufferMaterial.Count; }
RenderTextureFormat.ARGB32,
#if (VELOCITY_IN_GBUFFER)
RenderTextureFormat.RGHalf,
#endif
RenderTextureFormat.RGB111110Float
RenderTextureFormat.ARGB32
};
public RenderTextureReadWrite[] RTReadWrite =

RenderTextureReadWrite.Linear,
#if (VELOCITY_IN_GBUFFER)
RenderTextureReadWrite.Linear,
#endif
RenderTextureReadWrite.Linear
};

27
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitData.hlsl


struct FragInput
{
float4 unPositionSS; // This is the position return by VPOS, only xy is use
float4 unPositionSS; // This is the position return by VPOS (That is name positionCS in PackedVarying), only xy is use
// For velocity
// Note: Z component is not use
float4 positionCS; // This is the clip spae position. Warning, do not confuse with the value of positionCS in PackedVarying which is VPOS and store in unPositionSS
float4 previousPositionCS;
// For two sided lighting
bool isFrontFace;
};

#endif
}
float2 CalculateVelocity(float4 positionCS, float4 previousPositionCS)
{
// This test on define is required to remove warning of divide by 0 when initializing empty struct
#if SHADERPASS == SHADERPASS_VELOCITY || (SHADERPASS == SHADERPASS_GBUFFER && defined(VELOCITY_IN_GBUFFER))
// Encode velocity
positionCS.xy = positionCS.xy / positionCS.w;
previousPositionCS.xy = previousPositionCS.xy / previousPositionCS.w;
return (positionCS.xy - previousPositionCS.xy) * _ForceNoMotion;
#else
return float2(0.0, 0.0);
#endif
}
#if !defined(LAYERED_LIT_SHADER)
void GetSurfaceAndBuiltinData(FragInput input, out SurfaceData surfaceData, out BuiltinData builtinData)

builtinData.emissiveIntensity = _EmissiveIntensity;
builtinData.velocity = float2(0.0, 0.0);
builtinData.velocity = CalculateVelocity(input.positionCS, input.previousPositionCS);
builtinData.distortion = float2(0.0, 0.0);
builtinData.distortionBlur = 0.0;

PROP_BLEND_SCALAR(emissiveIntensity, weights);
builtinData.emissiveIntensity = emissiveIntensity;
builtinData.velocity = float2(0.0, 0.0);
builtinData.velocity = CalculateVelocity(input.positionCS, input.previousPositionCS);
builtinData.distortion = float2(0.0, 0.0);
builtinData.distortionBlur = 0.0;

39
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitDefault.shader


Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
LOD 300
// ------------------------------------------------------------------
// Deferred pass
// ------------------------------------------------------------------
Pass
{
Name "GBuffer" // Name is not used

ENDHLSL
}
// ------------------------------------------------------------------
// Debug pass
// ------------------------------------------------------------------
Pass
{
Name "Debug"

ENDHLSL
}
// ------------------------------------------------------------------
// ------------------------------------------------------------------
Pass
{
Name "META"

ENDHLSL
}
// ------------------------------------------------------------------
// Depth only
// ------------------------------------------------------------------
Pass
{
Name "ShadowCaster"

ENDHLSL
}
// ------------------------------------------------------------------
// forward pass
// ------------------------------------------------------------------
Pass
{
Name "Motion Vectors"
Tags{ "LightMode" = "MotionVectors" } // Caution, this need to be call like this to setup the correct parameters by C++ (legacy Unity)
Cull[_CullMode]
ZTest LEqual
ZWrite Off // TODO: Test Z equal here.
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#define SHADERPASS SHADERPASS_VELOCITY
#include "../../Material/Material.hlsl"
#include "LitData.hlsl"
#include "LitVelocityPass.hlsl"
#include "../../ShaderPass/ShaderPassVelocity.hlsl"
ENDHLSL
}
Pass
{
Name "Forward" // Name is not used

13
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitDepthPass.hlsl


#error Undefine_SHADERPASS
#endif
#define NEED_TANGENT_TO_WORLD (defined(_HEIGHTMAP) && !defined (_HEIGHTMAP_AS_DISPLACEMENT))
#define NEED_TEXCOORD0 defined(_ALPHATEST_ON) || NEED_TANGENT_TO_WORLD
// Check if Alpha test is enabled. If it is, check if parallax is enabled on this material
#define NEED_TEXCOORD0 defined(_ALPHATEST_ON)
#define NEED_TANGENT_TO_WORLD NEED_TEXCOORD0 && (defined(_HEIGHTMAP) && !defined(_HEIGHTMAP_AS_DISPLACEMENT))
struct Attributes
{

FragInput output;
ZERO_INITIALIZE(FragInput, output);
output.unPositionSS = input.positionCS;
output.unPositionSS = input.positionCS; // as input we have the vpos
#if NEED_TANGENT_TO_WORLD
output.positionWS.xyz = input.interpolators[0].xyz;

output.positionWS = positionWS;
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0] = tangentToWorld[0];
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0] = tangentToWorld[0];
output.tangentToWorld[1] = tangentToWorld[1];
output.tangentToWorld[2] = tangentToWorld[2];
#endif

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitMetaPass.hlsl


FragInput output;
ZERO_INITIALIZE(FragInput, output);
output.unPositionSS = input.positionCS;
output.unPositionSS = input.positionCS; // as input we have the vpos
output.texCoord0 = input.interpolators[0].xy;
output.texCoord1 = input.interpolators[0].zw;

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitSharePass.hlsl


FragInput output;
ZERO_INITIALIZE(FragInput, output);
output.unPositionSS = input.positionCS;
output.unPositionSS = input.positionCS; // as input we have the vpos
output.positionWS.xyz = input.interpolators[0].xyz;
output.tangentToWorld[0] = input.interpolators[1].xyz;
output.tangentToWorld[1] = input.interpolators[2].xyz;

98
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl


#include "EntityLighting.hlsl"
//-----------------------------------------------------------------------------
// common Encode/Decode functions
//-----------------------------------------------------------------------------
// Encode/Decode velocity in a buffer (either forward of deferred)
// Design note: We assume that VelocityVector fit into a single buffer (i.e not spread on several buffer)
void EncodeVelocity(float2 velocity, out float4 outBuffer)
{
// RT - 16:16 float
outBuffer = float4(velocity.xy, 0.0, 0.0);
}
float2 DecodeVelocity(float4 inBuffer)
{
return float2(inBuffer.xy);
}
// Encode/Decode into GBuffer - This is share so others material can use it.
// Design note: We assume that BakeDiffuseLighting and emissive fit into a single buffer (i.e not spread on several buffer)
void EncodeBakedDiffuseLigthingIntoGBuffer(float3 bakeDiffuseLighting, out float4 outBuffer)
{
// RT - 11:11:10f
outBuffer = float4(bakeDiffuseLighting.xyz, 0.0);
}
float3 DecodeBakedDiffuseLigthingFromGBuffer(float4 inBuffer)
{
return float3(inBuffer.xyz);
}
//-----------------------------------------------------------------------------
// BuiltinData
//-----------------------------------------------------------------------------

#ifdef GBUFFERMATERIAL_COUNT
#if GBUFFERMATERIAL_COUNT == 3
#if GBUFFERMATERIAL_COUNT == 2
#define OUTPUT_GBUFFER(NAME) \
out float4 MERGE_NAME(NAME, 0) : SV_Target0, \
out float4 MERGE_NAME(NAME, 1) : SV_Target1
#define DECLARE_GBUFFER_TEXTURE(NAME) \
TEXTURE2D(MERGE_NAME(NAME, 0)); \
TEXTURE2D(MERGE_NAME(NAME, 1));
#define FETCH_GBUFFER(NAME, TEX, UV) \
float4 MERGE_NAME(NAME, 0) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 0), uint3(UV, 0)); \
float4 MERGE_NAME(NAME, 1) = LOAD_TEXTURE2D(MERGE_NAME(TEX, 1), uint3(UV, 0));
#define ENCODE_INTO_GBUFFER(SURFACE_DATA, NAME) EncodeIntoGBuffer(SURFACE_DATA, MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2))
#define DECODE_FROM_GBUFFER(NAME) DecodeFromGBuffer(MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2))
#define GBUFFER_BAKE_LIGHTING_NAME(NAME) MERGE_NAME(NAME, 2)
#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 2)
#ifdef VELOCITY_IN_GBUFFER
#define GBUFFER_VELOCITY_NAME(NAME) MERGE_NAME(NAME, 3)
#define GBUFFER_VELOCITY_TARGET(TARGET) MERGE_NAME(TARGET, 3)
#endif
#elif GBUFFERMATERIAL_COUNT == 3
#define OUTPUT_GBUFFER(NAME) \
out float4 MERGE_NAME(NAME, 0) : SV_Target0, \

#define ENCODE_INTO_GBUFFER(SURFACE_DATA, NAME) EncodeIntoGBuffer(SURFACE_DATA, MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2))
#define DECODE_FROM_GBUFFER(NAME) DecodeFromGBuffer(MERGE_NAME(NAME,0), MERGE_NAME(NAME,1), MERGE_NAME(NAME,2))
#ifdef VELOCITY_IN_GBUFFER
#define GBUFFER_VELOCITY_NAME(NAME) MERGE_NAME(NAME, 3)
#define GBUFFER_VELOCITY_TARGET(TARGET) MERGE_NAME(TARGET, 3)
#define GBUFFER_BAKE_LIGHTING_NAME(NAME) MERGE_NAME(NAME, 4)
#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 4)
#else
#ifdef VELOCITY_IN_GBUFFER
#define GBUFFER_VELOCITY_NAME(NAME) MERGE_NAME(NAME, 4)
#define GBUFFER_VELOCITY_TARGET(TARGET) MERGE_NAME(TARGET, 4)
#endif
#elif GBUFFERMATERIAL_COUNT == 4

#define ENCODE_INTO_GBUFFER(SURFACE_DATA, NAME) EncodeIntoGBuffer(SURFACE_DATA, MERGE_NAME(NAME, 0), MERGE_NAME(NAME, 1), MERGE_NAME(NAME, 2), MERGE_NAME(NAME, 3))
#define DECODE_FROM_GBUFFER(NAME) DecodeFromGBuffer(MERGE_NAME(NAME, 0), MERGE_NAME(NAME, 1), MERGE_NAME(NAME, 2), MERGE_NAME(NAME, 3))
#define GBUFFER_BAKE_LIGHTING_NAME(NAME) MERGE_NAME(NAME, 4)
#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 4)
#define GBUFFER_VELOCITY_NAME(NAME) MERGE_NAME(NAME, 4)
#define GBUFFER_VELOCITY_TARGET(TARGET) MERGE_NAME(TARGET, 4)
#else
#define GBUFFER_BAKE_LIGHTING_NAME(NAME) MERGE_NAME(NAME, 4)
#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 4)
#endif
#elif GBUFFERMATERIAL_COUNT == 5

#define ENCODE_INTO_GBUFFER(SURFACE_DATA, NAME) EncodeIntoGBuffer(SURFACE_DATA, MERGE_NAME(NAME, 0), MERGE_NAME(NAME, 1), MERGE_NAME(NAME, 2), MERGE_NAME(NAME, 3), MERGE_NAME(NAME, 4))
#define DECODE_FROM_GBUFFER(NAME) DecodeFromGBuffer(MERGE_NAME(NAME, 0), MERGE_NAME(NAME, 1), MERGE_NAME(NAME, 2), MERGE_NAME(NAME, 3), MERGE_NAME(NAME, 4))
#define GBUFFER_BAKE_LIGHTING_NAME(NAME) MERGE_NAME(NAME, 5)
#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 5)
#define GBUFFER_VELOCITY_NAME(NAME) MERGE_NAME(NAME, 5)
#define GBUFFER_VELOCITY_TARGET(TARGET) MERGE_NAME(TARGET, 5)
#else
#define GBUFFER_BAKE_LIGHTING_NAME(NAME) MERGE_NAME(NAME, 5)
#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 5)
#ifdef VELOCITY_IN_GBUFFER
#define OUTPUT_GBUFFER_VELOCITY(NAME) out float4 GBUFFER_VELOCITY_NAME(NAME) : GBUFFER_VELOCITY_TARGET(SV_Target)
#define DECLARE_GBUFFER_VELOCITY_TEXTURE(NAME) TEXTURE2D(GBUFFER_VELOCITY_NAME(NAME));
#define ENCODE_VELOCITY_INTO_GBUFFER(VELOCITY, NAME) EncodeVelocity(VELOCITY, GBUFFER_VELOCITY_NAME(NAME))
#endif
#define FETCH_BAKE_LIGHTING_GBUFFER(NAME, TEX, UV) float4 GBUFFER_BAKE_LIGHTING_NAME(NAME) = GBUFFER_BAKE_LIGHTING_NAME(TEX).Load(uint3(UV, 0));
#define FETCH_BAKE_LIGHTING_GBUFFER(NAME, TEX, UV) float4 GBUFFER_BAKE_LIGHTING_NAME(NAME) = LOAD_TEXTURE2D(GBUFFER_BAKE_LIGHTING_NAME(TEX), uint3(UV, 0));
#endif // #ifdef GBUFFERMATERIAL_COUNT
// Decode velocity need to be accessible in both forward and deferred
#define DECODE_VELOCITY_BUFFER(NAME) DecodeVelocity(GBUFFER_VELOCITY_NAME(NAME))
#else
#define DECODE_VELOCITY_BUFFER(NAME) DecodeVelocity(GBUFFER_VELOCITY_NAME(NAME))
#define OUTPUT_GBUFFER_VELOCITY(NAME) out float4 GBUFFER_VELOCITY_NAME(NAME) : GBUFFER_VELOCITY_TARGET(SV_Target)
#define DECLARE_GBUFFER_VELOCITY_TEXTURE(NAME) TEXTURE2D(GBUFFER_VELOCITY_NAME(NAME));
#define ENCODE_VELOCITY_INTO_GBUFFER(VELOCITY, NAME) EncodeVelocity(VELOCITY, GBUFFER_VELOCITY_NAME(NAME))
#endif // #ifdef GBUFFERMATERIAL_COUNT
#endif // UNITY_MATERIAL_INCLUDED

6
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderConfig.cs


// #define DIFFUSE_LAMBERT_BRDF
// #define USE_BSDF_PRE_LAMBDAV
// #define VELOCITY_IN_GBUFFER
// Note: C# define can't be reuse in another C# file and ideally we would like that these define are present both on C# and HLSL side... How to do that ?
// For now sync by hand within HDRenderLoop.cs file and this one
// TODO: Currently it is not yet possible to use this feature, we need to provide previousPositionCS to the vertex shader as part of Attribute for GBuffer pass
//#define VELOCITY_IN_GBUFFER

12
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassGBuffer.hlsl


#endif
void Frag( PackedVaryings packedInput,
OUTPUT_GBUFFER(outGBuffer)
#ifdef VELOCITY_IN_GBUFFER
, OUTPUT_GBUFFER_VELOCITY(outGBuffer)
#endif
, OUTPUT_GBUFFER_BAKE_LIGHTING(outGBuffer)
OUTPUT_GBUFFER(outGBuffer),
OUTPUT_GBUFFER_BAKE_LIGHTING(outGBuffer)
#ifdef VELOCITY_IN_GBUFFER
, OUTPUT_GBUFFER_VELOCITY(outGBuffer)
#endif
)
{
FragInput input = UnpackVaryings(packedInput);

PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
ENCODE_INTO_GBUFFER(surfaceData, outGBuffer);
ENCODE_BAKE_LIGHTING_INTO_GBUFFER(GetBakedDiffuseLigthing(preLightData, surfaceData, builtinData, bsdfData), outGBuffer);
ENCODE_BAKE_LIGHTING_INTO_GBUFFER(GetBakedDiffuseLigthing(preLightData, surfaceData, builtinData, bsdfData), outGBuffer);
}

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl


float3 unity_ProbeVolumeMin;
CBUFFER_END
CBUFFER_START(UnityVelocityPass)
float4x4 _NonJitteredVP;
float4x4 _PreviousVP;
float4x4 _PreviousM;
bool _HasLastPositionData;
bool _ForceNoMotion;
float _MotionVectorDepthBias;
CBUFFER_END
// ----------------------------------------------------------------------------
// TODO: move this to constant buffer by Pass

5
Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs.hlsl


//
// LightDefinitions: static fields
//
#define USE_LEFTHAND_CAMERASPACE (0)
#define USE_LEFTHAND_CAMERASPACE (0)
#define IS_CIRCULAR_SPOT_SHAPE (1)
#define HAS_COOKIE_TEXTURE (2)
#define IS_BOX_PROJECTED (4)

124
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitVelocityPass.hlsl


#ifndef SHADERPASS
#error Undefine_SHADERPASS
#endif
#define NEED_TEXCOORD0 defined(_ALPHATEST_ON)
#define NEED_TANGENT_TO_WORLD NEED_TEXCOORD0 && (defined(_HEIGHTMAP) && !defined(_HEIGHTMAP_AS_DISPLACEMENT))
struct Attributes
{
float3 positionOS : POSITION;
float3 previousPositionOS : NORMAL; // Contain previous transform position (in case of skinning for example)
#if NEED_TEXCOORD0
float2 uv0 : TEXCOORD0;
#endif
#if NEED_TANGENT_TO_WORLD
float4 tangentOS : TANGENT;
#endif
};
struct Varyings
{
float4 positionCS;
// Note: Z component is not use
float4 transferPositionCS;
float4 transferPreviousPositionCS;
#if NEED_TEXCOORD0
float2 texCoord0;
#endif
#if NEED_TANGENT_TO_WORLD
float3 positionWS;
float3 tangentToWorld[3];
#endif
};
struct PackedVaryings
{
float4 positionCS : SV_Position;
#if NEED_TANGENT_TO_WORLD
float4 interpolators[5] : TEXCOORD0;
#elif NEED_TEXCOORD0
float4 interpolators[2] : 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.transferPositionCS.xyw, 0.0);
output.interpolators[1] = float4(input.transferPreviousPositionCS.xyw, 0.0);
#if NEED_TANGENT_TO_WORLD
output.interpolators[0].w = input.texCoord0.x;
output.interpolators[1].w = input.texCoord0.y;
output.interpolators[2].xyz = input.tangentToWorld[0];
output.interpolators[3].xyz = input.tangentToWorld[1];
output.interpolators[4].xyz = input.tangentToWorld[2];
output.interpolators[2].w = input.positionWS.x;
output.interpolators[3].w = input.positionWS.y;
output.interpolators[4].w = input.positionWS.z;
#elif NEED_TEXCOORD0
output.interpolators[0].w = input.texCoord0.x;
output.interpolators[1].w = input.texCoord0.y;
#endif
return output;
}
FragInput UnpackVaryings(PackedVaryings input)
{
FragInput output;
ZERO_INITIALIZE(FragInput, output);
output.unPositionSS = input.positionCS; // as input we have the vpos
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);
#if NEED_TANGENT_TO_WORLD
output.texCoord0.xy = float2(input.interpolators[0].w, input.interpolators[1].w);
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;
#elif NEED_TEXCOORD0
output.texCoord0.xy = float2(input.interpolators[0].w, input.interpolators[1].w);
#endif
return output;
}
PackedVaryings Vert(Attributes input)
{
Varyings output;
float3 positionWS = TransformObjectToWorld(input.positionOS);
output.positionCS = TransformWorldToHClip(positionWS);
// TODO: Clean this code, put in function ?
output.transferPositionCS = mul(_NonJitteredVP, mul(unity_ObjectToWorld, float4(input.positionOS, 1.0)));
output.transferPreviousPositionCS = mul(_PreviousVP, mul(_PreviousM, _HasLastPositionData ? float4(input.previousPositionOS, 1.0) : float4(input.positionOS, 1.0)));
#if NEED_TEXCOORD0
output.texCoord0 = input.uv0;
#endif
#if NEED_TANGENT_TO_WORLD
output.positionWS = positionWS;
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/Shaders/Material/Lit/LitVelocityPass.hlsl.meta


fileFormatVersion: 2
guid: e20c8a8a58e51844aad6d4df1dc7d3cd
timeCreated: 1478738809
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

18
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassVelocity.hlsl


#if SHADERPASS != SHADERPASS_VELOCITY
#error SHADERPASS_is_not_correctly_define
#endif
float4 Frag(PackedVaryings packedInput) : SV_Target
{
FragInput input = UnpackVaryings(packedInput);
// Perform alpha testing + get velocity
SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, surfaceData, builtinData);
float4 outBuffer;
EncodeVelocity(builtinData.velocity, outBuffer);
return outBuffer;
}

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassVelocity.hlsl.meta


fileFormatVersion: 2
guid: 5f5d2672c7761b44fb3d549f7cea8877
timeCreated: 1478738808
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

13
Assets/HDRenderLoop.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 558064ecdbf6b6742892d699acb39aed, type: 3}
m_Name: HDRenderLoop
m_EditorClassIdentifier:

8
Assets/HDRenderLoop.asset.meta


fileFormatVersion: 2
guid: a760b5cf201f0f5478077e4ee5df8236
timeCreated: 1474961622
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存