using UnityEngine.Rendering ;
using System.Collections.Generic ;
using System.Linq ;
using UnityEngine.Experimental.Rendering.HDPipeline.TilePass ;
public class HDRenderPipelineInstance : RenderPipeline
{
private readonly HDRenderPipeline m_Owner ;
public HDRenderPipelineInstance ( HDRenderPipeline owner )
{
m_Owner = owner ;
if ( m_Owner ! = null )
m_Owner . Build ( ) ;
}
public override void Dispose ( )
{
base . Dispose ( ) ;
if ( m_Owner ! = null )
m_Owner . Cleanup ( ) ;
}
public override void Render ( ScriptableRenderContext renderContext , Camera [ ] cameras )
{
base . Render ( renderContext , cameras ) ;
m_Owner . Render ( renderContext , cameras ) ;
}
}
[ExecuteInEditMode]
// This HDRenderPipeline assume linear lighting. Don't work with gamma.
public class HDRenderPipeline : RenderPipelineAsset
[UnityEditor.MenuItem("RenderPipeline/CreateHDRenderPipeline")]
static void CreateHDRenderPipeline ( )
{
var instance = ScriptableObject . CreateInstance < HDRenderPipeline > ( ) ;
UnityEditor . AssetDatabase . CreateAsset ( instance , k_HDRenderPipelinePath ) ;
var instance = CreateInstance < HDRenderPipeline > ( ) ;
AssetDatabase . CreateAsset ( instance , k_HDRenderPipelinePath ) ;
instance . m_setup = UnityEditor . AssetDatabase . LoadAssetAtPath < HDRenderPipelineSetup > ( "Assets/HDRenderPipelineSetup.asset" ) ;
}
}
#endif
HDRenderPipeline ( )
{
m_lightLoop = new TilePass . LightLoop ( this ) ;
}
private HDRenderPipeline ( )
{ }
[SerializeField]
private HDRenderPipelineSetup m_setup ;
get { return m_setup ; }
}
SkyManager m_SkyManager = new SkyManager ( ) ;
public SkyManager skyManager
readonly DebugParameters m_DebugParameters = new DebugParameters ( ) ;
public DebugParameters debugParameters
get { return m_SkyManager ; }
get { return m_DebugParameters ; }
public void InstantiateSkyRenderer ( Type skyRendererType )
[SerializeField]
ShadowSettings m_ShadowSettings = ShadowSettings . Default ;
public ShadowSettings shadowSettings
m_SkyManager . InstantiateSkyRenderer ( skyRendererType ) ;
get { return m_ShadowSettings ; }
public class DebugParameters
[SerializeField]
TextureSettings m_TextureSettings = TextureSettings . Default ;
public TextureSettings textureSettings
// Material Debugging
public int debugViewMaterial = 0 ;
get { return m_TextureSettings ; }
set { m_TextureSettings = value ; }
}
// Rendering debugging
public bool displayOpaqueObjects = true ;
public bool displayTransparentObjects = true ;
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 ;
[SerializeField]
TileSettings m_TileSettings = new TileSettings ( ) ;
// we have to fallback to forward-only rendering when scene view is using wireframe rendering mode --
// as rendering everything in wireframe + deferred do not play well together
public bool ShouldUseForwardRenderingOnly ( ) { return useForwardRenderingOnly | | GL . wireframe ; }
public TileSettings tileSettings
{
get { return m_TileSettings ; }
DebugParameters m_DebugParameters = new DebugParameters ( ) ;
public DebugParameters debugParameters
CommonSettings m_CommonSettings ;
public CommonSettings commonSettings
get { return m_DebugParameters ; }
set { m_CommonSettings = value ; }
get { return m_CommonSettings ; }
public class GBufferManager
public void UpdateCommonSettings ( )
public const int MaxGbuffer = 8 ;
public void SetBufferDescription ( int index , string stringId , RenderTextureFormat inFormat , RenderTextureReadWrite inSRGBWrite )
if ( commonSettings = = null )
IDs [ index ] = Shader . PropertyToID ( stringId ) ;
RTIDs [ index ] = new RenderTargetIdentifier ( IDs [ index ] ) ;
formats [ index ] = inFormat ;
sRGBWrites [ index ] = inSRGBWrite ;
m_ShadowSettings . maxShadowDistance = ShadowSettings . Default . maxShadowDistance ;
m_ShadowSettings . directionalLightCascadeCount = ShadowSettings . Default . directionalLightCascadeCount ;
m_ShadowSettings . directionalLightCascades = ShadowSettings . Default . directionalLightCascades ;
public void InitGBuffers ( int width , int height , CommandBuffer cmd )
else
for ( int index = 0 ; index < gbufferCount ; index + + )
{
/* RTs[index] = */ cmd . GetTemporaryRT ( IDs [ index ] , width , height , 0 , FilterMode . Point , formats [ index ] , sRGBWrites [ index ] ) ;
}
m_ShadowSettings . directionalLightCascadeCount = commonSettings . shadowCascadeCount ;
m_ShadowSettings . directionalLightCascades = new Vector3 ( commonSettings . shadowCascadeSplit0 , commonSettings . shadowCascadeSplit1 , commonSettings . shadowCascadeSplit2 ) ;
m_ShadowSettings . maxShadowDistance = commonSettings . shadowMaxDistance ;
}
public void InstantiateSkyRenderer ( Type skyRendererType )
{
var instances = CreatedInstances ( ) . OfType < HDRenderPipeline > ( ) ;
foreach ( var instance in instances )
instance . InstantiateSkyRenderer ( skyRendererType ) ;
}
public RenderTargetIdentifier [ ] GetGBuffers ( )
{
var colorMRTs = new RenderTargetIdentifier [ gbufferCount ] ;
for ( int index = 0 ; index < gbufferCount ; index + + )
{
colorMRTs [ index ] = RTIDs [ index ] ;
}
public Type GetSkyParameterType ( )
{
var instance = CreatedInstances ( ) . OfType < HDRenderPipeline > ( ) . FirstOrDefault ( ) ;
if ( instance = = null )
return null ;
return instance . GetType ( ) ;
}
}
return colorMRTs ;
}
[Serializable]
public class TileSettings
{
public bool enableDrawLightBoundsDebug = false ;
public bool disableTileAndCluster = true ; // For debug / test
public bool disableDeferredShadingInCompute = true ;
public bool enableSplitLightEvaluation = true ;
public bool enableComputeLightEvaluation = false ;
/ *
public void BindBuffers ( Material mat )
{
for ( int index = 0 ; index < gbufferCount ; index + + )
{
mat . SetTexture ( IDs [ index ] , RTs [ index ] ) ;
}
}
* /
// clustered light list specific buffers and data begin
public int debugViewTilesFlags = 0 ;
public bool enableClustered = false ;
public bool disableFptlWhenClustered = true ; // still useful on opaques. Should be false by default to force tile on opaque.
public bool enableBigTilePrepass = false ;
}
public int gbufferCount { get ; set ; }
int [ ] IDs = new int [ MaxGbuffer ] ;
RenderTargetIdentifier [ ] RTIDs = new RenderTargetIdentifier [ MaxGbuffer ] ;
RenderTextureFormat [ ] formats = new RenderTextureFormat [ MaxGbuffer ] ;
RenderTextureReadWrite [ ] sRGBWrites = new RenderTextureReadWrite [ MaxGbuffer ] ;
}
public struct HDCamera
{
public Camera camera ;
public Vector4 screenSize ;
public Matrix4x4 viewProjectionMatrix ;
public Matrix4x4 invViewProjectionMatrix ;
}
public class DebugParameters
{
// Material Debugging
public int debugViewMaterial = 0 ;
GBufferManager m_gbufferManager = new GBufferManager ( ) ;
// Rendering debugging
public bool displayOpaqueObjects = true ;
public bool displayTransparentObjects = true ;
[SerializeField]
ShadowSettings m_ShadowSettings = ShadowSettings . Default ;
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 ShadowSettings shadowSettings
// we have to fallback to forward-only rendering when scene view is using wireframe rendering mode --
// as rendering everything in wireframe + deferred do not play well together
public bool ShouldUseForwardRenderingOnly ( ) { return useForwardRenderingOnly | | GL . wireframe ; }
}
public class GBufferManager
{
public const int MaxGbuffer = 8 ;
public void SetBufferDescription ( int index , string stringId , RenderTextureFormat inFormat , RenderTextureReadWrite inSRGBWrite )
get { return m_ShadowSettings ; }
IDs [ index ] = Shader . PropertyToID ( stringId ) ;
RTIDs [ index ] = new RenderTargetIdentifier ( IDs [ index ] ) ;
formats [ index ] = inFormat ;
sRGBWrites [ index ] = inSRGBWrite ;
ShadowRenderPass m_ShadowPass ;
public void InitGBuffers ( int width , int height , CommandBuffer cmd )
{
for ( int index = 0 ; index < gbufferCount ; index + + )
{
/* RTs[index] = */
cmd . GetTemporaryRT ( IDs [ index ] , width , height , 0 , FilterMode . Point , formats [ index ] , sRGBWrites [ index ] ) ;
}
}
[SerializeField]
TextureSettings m_TextureSettings = TextureSettings . Default ;
public RenderTargetIdentifier [ ] GetGBuffers ( )
{
var colorMRTs = new RenderTargetIdentifier [ gbufferCount ] ;
for ( int index = 0 ; index < gbufferCount ; index + + )
{
colorMRTs [ index ] = RTIDs [ index ] ;
}
return colorMRTs ;
}
public TextureSettings textureSettings
/ *
public void BindBuffers ( Material mat )
get { return m_TextureSettings ; }
set { m_TextureSettings = value ; }
for ( int index = 0 ; index < gbufferCount ; index + + )
{
mat . SetTexture ( IDs [ index ] , RTs [ index ] ) ;
}
* /
public int gbufferCount { get ; set ; }
int [ ] IDs = new int [ MaxGbuffer ] ;
RenderTargetIdentifier [ ] RTIDs = new RenderTargetIdentifier [ MaxGbuffer ] ;
RenderTextureFormat [ ] formats = new RenderTextureFormat [ MaxGbuffer ] ;
RenderTextureReadWrite [ ] sRGBWrites = new RenderTextureReadWrite [ MaxGbuffer ] ;
}
public class HDRenderPipelineInstance : RenderPipeline
{
private readonly HDRenderPipeline m_Owner ;
// TODO: Find a way to automatically create/iterate through deferred material
// TODO TO CHECK: SebL I move allocation from Build() to here, but there was a comment "// Our object can be garbage collected, so need to be allocate here", it is still true ?
private readonly Lit . RenderLoop m_LitRenderLoop = new Lit . RenderLoop ( ) ;
readonly GBufferManager m_gbufferManager = new GBufferManager ( ) ;
Material m_DebugViewMaterialGBuffer ;
readonly Material m_DebugViewMaterialGBuffer ;
int m_CameraColorBuffer ;
int m_CameraDepthBuffer ;
int m_VelocityBuffer ;
int m_DistortionBuffer ;
readonly int m_CameraColorBuffer ;
readonly int m_CameraDepthBuffer ;
readonly int m_VelocityBuffer ;
readonly int m_DistortionBuffer ;
RenderTargetIdentifier m_CameraColorBufferRT ;
RenderTargetIdentifier m_CameraDepthBufferRT ;
RenderTargetIdentifier m_VelocityBufferRT ;
RenderTargetIdentifier m_DistortionBufferRT ;
readonly RenderTargetIdentifier m_CameraColorBufferRT ;
readonly RenderTargetIdentifier m_CameraDepthBufferRT ;
readonly RenderTargetIdentifier m_VelocityBufferRT ;
readonly RenderTargetIdentifier m_DistortionBufferRT ;
int m_currentWidth ;
int m_currentHeight ;
int m_CurrentWidth ;
int m_CurrentHeight ;
// Keep these settings safe to recover when leaving the render pipeline
bool previousLightsUseLinearIntensity ;
bool previousLightsUseCCT ;
ShadowRenderPass m_ShadowPass ;
BaseLightLoop m_lightLoop ;
//readonly BaseLightLoop m_lightLoop;
public BaseLightLoop lightLoop
SkyManager m_SkyManager ;
public SkyManager skyManager
get { return m_lightLoop ; }
get
{
if ( m_SkyManager = = null )
{
m_SkyManager = new SkyManager ( ) ;
m_SkyManager . skyParameters = Object . FindObjectOfType < SkyParameters > ( ) ;
}
return m_SkyManager ;
}
// TODO: Find a way to automatically create/iterate through deferred material
// TODO TO CHECK: SebL I move allocation from Build() to here, but there was a comment "// Our object can be garbage collected, so need to be allocate here", it is still true ?
Lit . RenderLoop m_LitRenderLoop = new Lit . RenderLoop ( ) ;
private DebugParameters debugParameters
{
get { return m_Owner . debugParameters ; }
}
public struct HDCamera
private CommonSettings commonSettings
public Camera camera ;
public Vector4 screenSize ;
public Matrix4x4 viewProjectionMatrix ;
public Matrix4x4 invViewProjectionMatrix ;
get { return m_Owner . commonSettings ; }
CommonSettings m_CommonSettings = null ;
public CommonSettings commonSettings
public void InstantiateSkyRenderer ( Type skyRendererType )
set { m_CommonSettings = value ; }
get { return m_CommonSettings ; }
m_SkyManager . InstantiateSkyRenderer ( skyRendererType ) ;
public void Build ( )
public HDRenderPipelineInstance ( HDRenderPipeline owner )
#if UNITY_EDITOR
UnityEditor . SupportedRenderingFeatures . active = new UnityEditor . SupportedRenderingFeatures
{
reflectionProbe = UnityEditor . SupportedRenderingFeatures . ReflectionProbe . Rotation
} ;
#endif
previousLightsUseLinearIntensity = UnityEngine . Rendering . GraphicsSettings . lightsUseLinearIntensity ;
previousLightsUseCCT = UnityEngine . Rendering . GraphicsSettings . lightsUseCCT ;
UnityEngine . Rendering . GraphicsSettings . lightsUseLinearIntensity = true ;
UnityEngine . Rendering . GraphicsSettings . lightsUseCCT = true ;
m_Owner = owner ;
m_CameraDepthBuffer = Shader . PropertyToID ( "_CameraDepthTexture" ) ;
m_CameraDepthBuffer = Shader . PropertyToID ( "_CameraDepthTexture" ) ;
m_SkyManager . Build ( ) ;
m_ShadowPass = new ShadowRenderPass ( m_ShadowSettings ) ;
m_ShadowPass = new ShadowRenderPass ( owner . shadowSettings ) ;
RenderTextureFormat [ ] RTFormat ; RenderTextureReadWrite [ ] RTReadWrite ;
RenderTextureFormat [ ] RTFormat ;
RenderTextureReadWrite [ ] RTReadWrite ;
m_LitRenderLoop . GetMaterialGBufferDescription ( out RTFormat , out RTReadWrite ) ;
for ( int gbufferIndex = 0 ; gbufferIndex < m_gbufferManager . gbufferCount ; + + gbufferIndex )
m_DistortionBufferRT = new RenderTargetIdentifier ( m_DistortionBuffer ) ;
m_LitRenderLoop . Build ( ) ;
m_lightLoop . Build ( m_TextureSettings ) ;
m_lightLoop = new LightLoop ( owner ) ;
m_lightLoop . Build ( owner . textureSettings ) ;
public void Cleanup ( )
public override void Dispose ( )
base . Dispose ( ) ;
UnityEditor . SupportedRenderingFeatures . active = UnityEditor . SupportedRenderingFeatures . Default ;
SupportedRenderingFeatures . active = SupportedRenderingFeatures . Default ;
UnityEngine . Rendering . GraphicsSettings . lightsUseLinearIntensity = previousLightsUseLinearIntensity ;
UnityEngine . Rendering . GraphicsSettings . lightsUseCCT = previousLightsUseCCT ;
void InitAndClearBuffer ( Camera camera , ScriptableRenderContext renderContext )
#if UNITY_EDITOR
private static readonly SupportedRenderingFeatures s_NeededFeatures = new SupportedRenderingFeatures ( )
using ( new Utilities . ProfilingSample ( "InitAndClearBuffer" , renderContext ) )
reflectionProbe = SupportedRenderingFeatures . ReflectionProbe . Rotation
} ;
private LightLoop m_lightLoop ;
#endif
void Resize ( Camera camera )
{
// TODO: Detect if renderdoc just load and force a resize in this case, as often renderdoc require to realloc resource.
// TODO: This is the wrong way to handle resize/allocation. We can have several different camera here, mean that the loop on camera will allocate and deallocate
// the below buffer which is bad. Best is to have a set of buffer for each camera that is persistent and reallocate resource if need
// For now consider we have only one camera that go to this code, the main one.
m_SkyManager . Resize ( ) ; // TODO: Also a bad naming, here we just want to realloc texture if skyparameters change (usefull for lookdev)
if ( camera . pixelWidth ! = m_CurrentWidth | | camera . pixelHeight ! = m_CurrentHeight | | m_lightLoop . NeedResize ( ) )
// We clear only the depth buffer, no need to clear the various color buffer as we overwrite them.
// Clear depth/stencil and init buffers
using ( new Utilities . ProfilingSample ( "InitGBuffers and clear Depth/Stencil" , renderContext ) )
if ( m_CurrentWidth > 0 & & m_CurrentHeight > 0 )
var cmd = new CommandBuffer ( ) ;
cmd . name = "" ;
m_lightLoop . ReleaseResolutionDependentBuffers ( ) ;
}
// Init buffer
// With scriptable render loop we must allocate ourself depth and color buffer (We must be independent of backbuffer for now, hope to fix that later).
// Also we manage ourself the HDR format, here allocating fp16 directly.
// With scriptable render loop we can allocate temporary RT in a command buffer, they will not be release with ExecuteCommandBuffer
// These temporary surface are release automatically at the end of the scriptable render pipeline if not release explicitly
int w = camera . pixelWidth ;
int h = camera . pixelHeight ;
m_lightLoop . AllocResolutionDependentBuffers ( camera . pixelWidth , camera . pixelHeight ) ;
// update recorded window resolution
m_CurrentWidth = camera . pixelWidth ;
m_CurrentHeight = camera . pixelHeight ;
}
}
public void PushGlobalParams ( HDCamera hdCamera , ScriptableRenderContext renderContext )
{
if ( m_SkyManager . IsSkyValid ( ) )
{
m_SkyManager . SetGlobalSkyTexture ( ) ;
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 1 ) ;
}
else
{
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 0 ) ;
}
var cmd = new CommandBuffer { name = "Push Global Parameters" } ;
cmd . SetGlobalVector ( "_ScreenSize" , hdCamera . screenSize ) ;
cmd . SetGlobalMatrix ( "_ViewProjMatrix" , hdCamera . viewProjectionMatrix ) ;
cmd . SetGlobalMatrix ( "_InvViewProjMatrix" , hdCamera . invViewProjectionMatrix ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
m_lightLoop . PushGlobalParams ( hdCamera . camera , renderContext ) ;
}
public override void Render ( ScriptableRenderContext renderContext , Camera [ ] cameras )
{
base . Render ( renderContext , cameras ) ;
#if UNITY_EDITOR
SupportedRenderingFeatures . active = s_NeededFeatures ;
#endif
GraphicsSettings . lightsUseLinearIntensity = true ;
GraphicsSettings . lightsUseCCT = true ;
skyManager . Build ( ) ;
if ( ! m_LitRenderLoop . isInit )
m_LitRenderLoop . RenderInit ( renderContext ) ;
cmd . GetTemporaryRT ( m_CameraColorBuffer , w , h , 0 , FilterMode . Point , RenderTextureFormat . ARGBHalf , RenderTextureReadWrite . Linear , 1 , true ) ; // Enable UAV
cmd . GetTemporaryRT ( m_CameraDepthBuffer , w , h , 2 4 , FilterMode . Point , RenderTextureFormat . Depth ) ;
if ( ! debugParameters . ShouldUseForwardRenderingOnly ( ) )
{
m_gbufferManager . InitGBuffers ( w , h , cmd ) ;
}
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
// Do anything we need to do upon a new frame.
m_lightLoop . NewFrame ( ) ;
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthBufferRT , ClearFlag . ClearDepth ) ;
}
m_Owner . UpdateCommonSettings ( ) ;
// TEMP: As we are in development and have not all the setup pass we still clear the color in emissive buffer and gbuffer, but this will be removed later.
// Set Frame constant buffer
// TODO...
// Clear HDR target
using ( new Utilities . ProfilingSample ( "Clear HDR target" , renderContext ) )
{
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthBufferRT , ClearFlag . ClearColor , Color . black ) ;
}
// we only want to render one camera for now
// select the most main camera!
Camera camera = cameras . OrderByDescending ( x = > x . tag = = "MainCamera" ) . FirstOrDefault ( ) ;
if ( camera = = null )
return ;
// Clear GBuffers
using ( new Utilities . ProfilingSample ( "Clear GBuffer" , renderContext ) )
{
Utilities . SetRenderTarget ( renderContext , m_gbufferManager . GetGBuffers ( ) , m_CameraDepthBufferRT , ClearFlag . ClearColor , Color . black ) ;
}
// Set camera constant buffer
// TODO...
CullingParameters cullingParams ;
if ( ! CullResults . GetCullingParameters ( camera , out cullingParams ) )
return ;
// END TEMP
m_ShadowPass . UpdateCullingParameters ( ref cullingParams ) ;
var cullResults = CullResults . Cull ( ref cullingParams , renderContext ) ;
Resize ( camera ) ;
renderContext . SetupCameraProperties ( camera ) ;
HDCamera hdCamera = Utilities . GetHDCamera ( camera ) ;
InitAndClearBuffer ( camera , renderContext ) ;
RenderDepthPrepass ( cullResults , camera , renderContext ) ;
// Forward opaque with deferred/cluster tile require that we fill the depth buffer
// correctly to build the light list.
// TODO: avoid double lighting by tagging stencil or gbuffer that we must not lit.
RenderForwardOnlyOpaqueDepthPrepass ( cullResults , camera , renderContext ) ;
RenderGBuffer ( cullResults , camera , renderContext ) ;
if ( debugParameters . debugViewMaterial ! = 0 )
{
RenderDebugViewMaterial ( cullResults , hdCamera , renderContext ) ;
return ;
ShadowOutput shadows ;
using ( new Utilities . ProfilingSample ( "Shadow Pass" , renderContext ) )
{
m_ShadowPass . Render ( renderContext , cullResults , out shadows ) ;
}
renderContext . SetupCameraProperties ( camera ) ; // Need to recall SetupCameraProperties after m_ShadowPass.Render
using ( new Utilities . ProfilingSample ( "Build Light list" , renderContext ) )
{
m_lightLoop . PrepareLightsForGPU ( m_Owner . shadowSettings , cullResults , camera , ref shadows ) ;
m_lightLoop . BuildGPULightLists ( camera , renderContext , m_CameraDepthBufferRT ) ; // TODO: Use async compute here to run light culling during shadow
PushGlobalParams ( hdCamera , renderContext ) ;
}
// Caution: We require sun light here as some sky use the sun light to render, mean UpdateSkyEnvironment
// must be call after BuildGPULightLists.
// TODO: Try to arrange code so we can trigger this call earlier and use async compute here to run sky convolution during other passes (once we move convolution shader to compute).
UpdateSkyEnvironment ( hdCamera , renderContext ) ;
RenderDeferredLighting ( hdCamera , renderContext ) ;
// For opaque forward we have split rendering in two categories
// Material that are always forward and material that can be deferred or forward depends on render pipeline options (like switch to rendering forward only mode)
// Material that are always forward are unlit and complex (Like Hair) and don't require sorting, so it is ok to split them.
RenderForward ( cullResults , camera , renderContext , true ) ; // Render deferred or forward opaque
RenderForwardOnlyOpaque ( cullResults , camera , renderContext ) ;
RenderSky ( hdCamera , renderContext ) ;
// Render all type of transparent forward (unlit, lit, complex (hair...)) to keep the sorting between transparent objects.
RenderForward ( cullResults , camera , renderContext , false ) ;
RenderVelocity ( cullResults , camera , renderContext ) ; // Note we may have to render velocity earlier if we do temporalAO, temporal volumetric etc... Mean we will not take into account forward opaque in case of deferred rendering ?
// TODO: Check with VFX team.
// 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 , renderContext ) ;
FinalPass ( camera , renderContext ) ;
// bind depth surface for editor grid/gizmo/selection rendering
if ( camera . cameraType = = CameraType . SceneView )
{
var cmd = new CommandBuffer ( ) ;
cmd . SetRenderTarget ( BuiltinRenderTextureType . CameraTarget , m_CameraDepthBufferRT ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
}
renderContext . Submit ( ) ;
}
void RenderOpaqueRenderList ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , string passName , RendererConfiguration rendererConfiguration = 0 )
{
if ( debugParameters . ShouldUseForwardRenderingOnly ( ) )
{
return ;
return ;
}
using ( new Utilities . ProfilingSample ( "GBuffer Pass" , renderContext ) )
{
if ( debugParameters . ShouldUseForwardRenderingOnly ( ) )
{
return ;
return ;
}
// Bind material data
void UpdateSkyEnvironment ( HDCamera hdCamera , ScriptableRenderContext renderContext )
{
m_S kyManager. UpdateEnvironment ( hdCamera , m_lightLoop . GetCurrentSunLight ( ) , renderContext ) ;
s kyManager. UpdateEnvironment ( hdCamera , m_lightLoop . GetCurrentSunLight ( ) , renderContext ) ;
m_S kyManager. RenderSky ( hdCamera , m_lightLoop . GetCurrentSunLight ( ) , m_CameraColorBufferRT , m_CameraDepthBufferRT , renderContext ) ;
s kyManager. RenderSky ( hdCamera , m_lightLoop . GetCurrentSunLight ( ) , m_CameraColorBufferRT , m_CameraDepthBufferRT , renderContext ) ;
void RenderForward ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext , bool renderOpaque )
{
// TODO: Currently we can't render opaque object forward when deferred is enabled
{
// If opaque velocity have been render during GBuffer no need to render it here
if ( ( ShaderConfig . s_VelocityInGbuffer = = 0 ) | | debugParameters . ShouldUseForwardRenderingOnly ( ) )
return ;
return ;
int w = camera . pixelWidth ;
int h = camera . pixelHeight ;
void RenderDistortion ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext )
{
if ( ! debugParameters . useDistortion )
return ;
return ;
using ( new Utilities . ProfilingSample ( "Distortion Pass" , renderContext ) )
{
m_lightLoop . PrepareLightsForGPU ( shadowSettings , cullResults , camera , ref shadowOutput ) ;
}
void Resize ( Camera camera )
{
// TODO: Detect if renderdoc just load and force a resize in this case, as often renderdoc require to realloc resource.
// TODO: This is the wrong way to handle resize/allocation. We can have several different camera here, mean that the loop on camera will allocate and deallocate
// the below buffer which is bad. Best is to have a set of buffer for each camera that is persistent and reallocate resource if need
// For now consider we have only one camera that go to this code, the main one.
m_SkyManager . Resize ( ) ; // TODO: Also a bad naming, here we just want to realloc texture if skyparameters change (usefull for lookdev)
if ( camera . pixelWidth ! = m_currentWidth | | camera . pixelHeight ! = m_currentHeight | | m_lightLoop . NeedResize ( ) )
{
if ( m_currentWidth > 0 & & m_currentHeight > 0 )
{
m_lightLoop . ReleaseResolutionDependentBuffers ( ) ;
}
m_lightLoop . AllocResolutionDependentBuffers ( camera . pixelWidth , camera . pixelHeight ) ;
// update recorded window resolution
m_currentWidth = camera . pixelWidth ;
m_currentHeight = camera . pixelHeight ;
}
}
public void PushGlobalParams ( HDCamera hdCamera , ScriptableRenderContext renderContext )
void InitAndClearBuffer ( Camera camera , ScriptableRenderContext renderContext )
if ( m_SkyManager . IsSkyValid ( ) )
{
m_SkyManager . SetGlobalSkyTexture ( ) ;
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 1 ) ;
}
else
using ( new Utilities . ProfilingSample ( "InitAndClearBuffer" , renderContext ) )
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 0 ) ;
}
var cmd = new CommandBuffer { name = "Push Global Parameters" } ;
cmd . SetGlobalVector ( "_ScreenSize" , hdCamera . screenSize ) ;
cmd . SetGlobalMatrix ( "_ViewProjMatrix" , hdCamera . viewProjectionMatrix ) ;
cmd . SetGlobalMatrix ( "_InvViewProjMatrix" , hdCamera . invViewProjectionMatrix ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
m_lightLoop . PushGlobalParams ( hdCamera . camera , renderContext ) ;
}
void UpdateCommonSettings ( )
{
if ( m_CommonSettings = = null )
{
m_ShadowSettings . maxShadowDistance = ShadowSettings . Default . maxShadowDistance ;
m_ShadowSettings . directionalLightCascadeCount = ShadowSettings . Default . directionalLightCascadeCount ;
m_ShadowSettings . directionalLightCascades = ShadowSettings . Default . directionalLightCascades ;
}
else
{
m_ShadowSettings . directionalLightCascadeCount = m_CommonSettings . shadowCascadeCount ;
m_ShadowSettings . directionalLightCascades = new Vector3 ( m_CommonSettings . shadowCascadeSplit0 , m_CommonSettings . shadowCascadeSplit1 , m_CommonSettings . shadowCascadeSplit2 ) ;
m_ShadowSettings . maxShadowDistance = m_CommonSettings . shadowMaxDistance ;
}
}
public void Render ( ScriptableRenderContext renderContext , IEnumerable < Camera > cameras )
{
if ( ! m_LitRenderLoop . isInit )
{
m_LitRenderLoop . RenderInit ( renderContext ) ;
}
// Do anything we need to do upon a new frame.
m_lightLoop . NewFrame ( ) ;
UpdateCommonSettings ( ) ;
// Set Frame constant buffer
// TODO...
foreach ( var camera in cameras )
{
// Set camera constant buffer
// TODO...
CullingParameters cullingParams ;
if ( ! CullResults . GetCullingParameters ( camera , out cullingParams ) )
continue ;
m_ShadowPass . UpdateCullingParameters ( ref cullingParams ) ;
var cullResults = CullResults . Cull ( ref cullingParams , renderContext ) ;
Resize ( camera ) ;
renderContext . SetupCameraProperties ( camera ) ;
HDCamera hdCamera = Utilities . GetHDCamera ( camera ) ;
InitAndClearBuffer ( camera , renderContext ) ;
RenderDepthPrepass ( cullResults , camera , renderContext ) ;
// Forward opaque with deferred/cluster tile require that we fill the depth buffer
// correctly to build the light list.
// TODO: avoid double lighting by tagging stencil or gbuffer that we must not lit.
RenderForwardOnlyOpaqueDepthPrepass ( cullResults , camera , renderContext ) ;
RenderGBuffer ( cullResults , camera , renderContext ) ;
if ( debugParameters . debugViewMaterial ! = 0 )
// We clear only the depth buffer, no need to clear the various color buffer as we overwrite them.
// Clear depth/stencil and init buffers
using ( new Utilities . ProfilingSample ( "InitGBuffers and clear Depth/Stencil" , renderContext ) )
RenderDebugViewMaterial ( cullResults , hdCamera , renderContext ) ;
}
else
{
ShadowOutput shadows ;
using ( new Utilities . ProfilingSample ( "Shadow Pass" , renderContext ) )
{
m_ShadowPass . Render ( renderContext , cullResults , out shadows ) ;
}
var cmd = new CommandBuffer ( ) ;
cmd . name = "" ;
renderContext . SetupCameraProperties ( camera ) ; // Need to recall SetupCameraProperties after m_ShadowPass.Render
// Init buffer
// With scriptable render loop we must allocate ourself depth and color buffer (We must be independent of backbuffer for now, hope to fix that later).
// Also we manage ourself the HDR format, here allocating fp16 directly.
// With scriptable render loop we can allocate temporary RT in a command buffer, they will not be release with ExecuteCommandBuffer
// These temporary surface are release automatically at the end of the scriptable render pipeline if not release explicitly
int w = camera . pixelWidth ;
int h = camera . pixelHeight ;
using ( new Utilities . ProfilingSample ( "Build Light list" , renderContext ) )
cmd . GetTemporaryRT ( m_CameraColorBuffer , w , h , 0 , FilterMode . Point , RenderTextureFormat . ARGBHalf , RenderTextureReadWrite . Linear , 1 , true ) ; // Enable UAV
cmd . GetTemporaryRT ( m_CameraDepthBuffer , w , h , 2 4 , FilterMode . Point , RenderTextureFormat . Depth ) ;
if ( ! m_Owner . debugParameters . ShouldUseForwardRenderingOnly ( ) )
m_lightLoop . PrepareLightsForGPU ( m_ShadowSettings , cullResults , camera , ref shadows ) ;
m_lightLoop . BuildGPULightLists ( camera , renderContext , m_CameraDepthBufferRT ) ; // TODO: Use async compute here to run light culling during shadow
PushGlobalParams ( hdCamera , renderContext ) ;
m_gbufferManager . InitGBuffers ( w , h , cmd ) ;
// Caution: We require sun light here as some sky use the sun light to render, mean UpdateSkyEnvironment
// must be call after BuildGPULightLists.
// TODO: Try to arrange code so we can trigger this call earlier and use async compute here to run sky convolution during other passes (once we move convolution shader to compute).
UpdateSkyEnvironment ( hdCamera , renderContext ) ;
RenderDeferredLighting ( hdCamera , renderContext ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
// For opaque forward we have split rendering in two categories
// Material that are always forward and material that can be deferred or forward depends on render pipeline options (like switch to rendering forward only mode)
// Material that are always forward are unlit and complex (Like Hair) and don't require sorting, so it is ok to split them.
RenderForward ( cullResults , camera , renderContext , true ) ; // Render deferred or forward opaque
RenderForwardOnlyOpaque ( cullResults , camera , renderContext ) ;
RenderSky ( hdCamera , renderContext ) ;
// Render all type of transparent forward (unlit, lit, complex (hair...)) to keep the sorting between transparent objects.
RenderForward ( cullResults , camera , renderContext , false ) ;
RenderVelocity ( cullResults , camera , renderContext ) ; // Note we may have to render velocity earlier if we do temporalAO, temporal volumetric etc... Mean we will not take into account forward opaque in case of deferred rendering ?
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthBufferRT , ClearFlag . ClearDepth ) ;
}
// TODO: Check with VFX team.
// 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 , renderContext ) ;
// TEMP: As we are in development and have not all the setup pass we still clear the color in emissive buffer and gbuffer, but this will be removed later.
FinalPass ( camera , renderContext ) ;
// Clear HDR target
using ( new Utilities . ProfilingSample ( "Clear HDR target" , renderContext ) )
{
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthBufferRT , ClearFlag . ClearColor , Color . black ) ;
// bind depth surface for editor grid/gizmo/selection rendering
if ( camera . cameraType = = CameraType . SceneView )
// Clear GBuffers
using ( new Utilities . ProfilingSample ( "Clear GBuffer" , renderContext ) )
var cmd = new CommandBuffer ( ) ;
cmd . SetRenderTarget ( BuiltinRenderTextureType . CameraTarget , m_CameraDepthBufferRT ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
Utilities . SetRenderTarget ( renderContext , m_gbufferManager . GetGBuffers ( ) , m_CameraDepthBufferRT , ClearFlag . ClearColor , Color . black ) ;
renderContext . Submit ( ) ;
// END TEMP
}
}
}