using UnityEngine.Rendering ;
using System ;
using System.Linq ;
using UnityEngine.Experimental .PostProcessing ;
using UnityEngine.Rendering .PostProcessing ;
using UnityEngine.Experimental.Rendering.HDPipeline.TilePass ;
#if UNITY_EDITOR
{
public Matrix4x4 viewMatrix ;
public Matrix4x4 projMatrix ;
public Matrix4x4 nonJitteredProjMatrix ;
public Vector4 [ ] frustumPlaneEquations ;
public Camera camera ;
public Matrix4x4 viewProjMatrix
public Matrix4x4 nonJitteredViewProjMatrix
{
get { return nonJitteredProjMatrix * viewMatrix ; }
}
public Vector4 invProjParam
{
// Ref: An Efficient Depth Linearization Method for Oblique View Frustums, Eq. 6.
// View-projection matrix from the previous frame.
public Matrix4x4 prevViewProjMatrix ;
// We need to keep track of these when camera relative rendering is enabled so we can take
// camera translation into account when generating camera motion vectors
public Vector3 cameraPos ;
public Vector3 prevCameraPos ;
// The only way to reliably keep track of a frame change right now is to compare the frame
// count Unity gives us. We need this as a single camera could be rendered several times per
// frame and some matrices only have to be computed once. Realistically this shouldn't
// avoid one-frame jumps/hiccups with temporal effects (motion blur, TAA...)
bool m_FirstFrame ;
public HDCamera ( Camera camera )
public HDCamera ( Camera cam )
this . camera = camera ;
camera = cam ;
frustumPlaneEquations = new Vector4 [ 6 ] ;
public void Update ( )
public void Update ( PostProcessLayer postProcessLayer )
// If TAA is enabled projMatrix will hold a jittered projection matrix. The original,
// non-jittered projection matrix can be accessed via nonJitteredProjMatrix.
bool taaEnabled = camera . cameraType = = CameraType . Game
& & Utilities . IsTemporalAntialiasingActive ( postProcessLayer ) ;
Matrix4x4 nonJitteredCameraProj = camera . projectionMatrix ;
Matrix4x4 cameraProj = taaEnabled
? postProcessLayer . temporalAntialiasing . GetJitteredProjectionMatrix ( camera )
: nonJitteredCameraProj ;
var gpuProj = GL . GetGPUProjectionMatrix ( camera . projectionMatrix , true ) ; // Had to change this from 'false'
var gpuVP = gpuProj * camera . worldToCameraMatrix ;
Matrix4x4 gpuProj = GL . GetGPUProjectionMatrix ( cameraProj , true ) ; // Had to change this from 'false'
Matrix4x4 gpuView = camera . worldToCameraMatrix ;
Matrix4x4 gpuNonJitteredProj = GL . GetGPUProjectionMatrix ( nonJitteredCameraProj , true ) ;
// A camera could be rendered multiple time per frame, only updates the previous viewproj if needed
Vector3 pos = camera . transform . position ;
if ( ShaderConfig . s_CameraRelativeRendering ! = 0 )
{
// Zero out the translation component.
gpuView . SetColumn ( 3 , new Vector4 ( 0 , 0 , 0 , 1 ) ) ;
}
Matrix4x4 gpuVP = gpuNonJitteredProj * gpuView ;
// A camera could be rendered multiple times per frame, only updates the previous view proj & pos if needed
prevViewProjMatrix = ! m_FirstFrame
? viewProjMatrix
: gpuVP ;
if ( m_FirstFrame )
{
prevCameraPos = pos ;
prevViewProjMatrix = gpuVP ;
}
else
{
prevCameraPos = cameraPos ;
prevViewProjMatrix = nonJitteredViewProjMatrix ;
}
viewMatrix = camera . worldToCameraMatrix ;
viewMatrix = gpuView ;
nonJitteredProjMatrix = gpuNonJitteredProj ;
cameraPos = pos ;
Plane [ ] planes = GeometryUtility . CalculateFrustumPlanes ( viewProjMatrix ) ;
for ( int i = 0 ; i < 6 ; i + + )
{
frustumPlaneEquations [ i ] = new Vector4 ( planes [ i ] . normal . x , planes [ i ] . normal . y , planes [ i ] . normal . z , planes [ i ] . distance ) ;
}
m_LastFrameActive = Time . frameCount ;
}
static List < Camera > m_Cleanup = new List < Camera > ( ) ; // Recycled to reduce GC pressure
// Grab the HDCamera tied to a given Camera and update it.
public static HDCamera Get ( Camera camera )
public static HDCamera Get ( Camera camera , PostProcessLayer postProcessLayer )
{
HDCamera hdcam ;
m_Cameras . Add ( camera , hdcam ) ;
}
hdcam . Update ( ) ;
hdcam . Update ( postProcessLayer ) ;
return hdcam ;
}
public void SetupGlobalParams ( CommandBuffer cmd )
{
cmd . SetGlobalMatrix ( "_ViewMatrix" , viewMatrix ) ;
cmd . SetGlobalMatrix ( "_InvViewMatrix" , viewMatrix . inverse ) ;
cmd . SetGlobalMatrix ( "_ProjMatrix" , projMatrix ) ;
cmd . SetGlobalMatrix ( "_InvProjMatrix" , projMatrix . inverse ) ;
cmd . SetGlobalMatrix ( "_ViewProjMatrix" , viewProjMatrix ) ;
cmd . SetGlobalMatrix ( "_InvViewProjMatrix" , viewProjMatrix . inverse ) ;
cmd . SetGlobalVector ( "_InvProjParam" , invProjParam ) ;
cmd . SetGlobalVector ( "_ScreenSize" , screenSize ) ;
cmd . SetGlobalMatrix ( "_PrevViewProjMatrix" , prevViewProjMatrix ) ;
cmd . SetGlobalMatrix ( "_ViewMatrix" , viewMatrix ) ;
cmd . SetGlobalMatrix ( "_InvViewMatrix" , viewMatrix . inverse ) ;
cmd . SetGlobalMatrix ( "_ProjMatrix" , projMatrix ) ;
cmd . SetGlobalMatrix ( "_InvProjMatrix" , projMatrix . inverse ) ;
cmd . SetGlobalMatrix ( "_NonJitteredViewProjMatrix" , nonJitteredViewProjMatrix ) ;
cmd . SetGlobalMatrix ( "_ViewProjMatrix" , viewProjMatrix ) ;
cmd . SetGlobalMatrix ( "_InvViewProjMatrix" , viewProjMatrix . inverse ) ;
cmd . SetGlobalVector ( "_InvProjParam" , invProjParam ) ;
cmd . SetGlobalVector ( "_ScreenSize" , screenSize ) ;
cmd . SetGlobalMatrix ( "_PrevViewProjMatrix" , prevViewProjMatrix ) ;
cmd . SetGlobalVectorArray ( "_FrustumPlanes" , frustumPlaneEquations ) ;
material . SetMatrix ( "_ViewMatrix" , viewMatrix ) ;
material . SetMatrix ( "_InvViewMatrix" , viewMatrix . inverse ) ;
material . SetMatrix ( "_ProjMatrix" , projMatrix ) ;
material . SetMatrix ( "_InvProjMatrix" , projMatrix . inverse ) ;
material . SetMatrix ( "_ViewProjMatrix" , viewProjMatrix ) ;
material . SetMatrix ( "_InvViewProjMatrix" , viewProjMatrix . inverse ) ;
material . SetVector ( "_InvProjParam" , invProjParam ) ;
material . SetVector ( "_ScreenSize" , screenSize ) ;
material . SetMatrix ( "_PrevViewProjMatrix" , prevViewProjMatrix ) ;
material . SetMatrix ( "_ViewMatrix" , viewMatrix ) ;
material . SetMatrix ( "_InvViewMatrix" , viewMatrix . inverse ) ;
material . SetMatrix ( "_ProjMatrix" , projMatrix ) ;
material . SetMatrix ( "_InvProjMatrix" , projMatrix . inverse ) ;
material . SetMatrix ( "_NonJitteredViewProjMatrix" , nonJitteredViewProjMatrix ) ;
material . SetMatrix ( "_ViewProjMatrix" , viewProjMatrix ) ;
material . SetMatrix ( "_InvViewProjMatrix" , viewProjMatrix . inverse ) ;
material . SetVector ( "_InvProjParam" , invProjParam ) ;
material . SetVector ( "_ScreenSize" , screenSize ) ;
material . SetMatrix ( "_PrevViewProjMatrix" , prevViewProjMatrix ) ;
material . SetVectorArray ( "_FrustumPlanes" , frustumPlaneEquations ) ;
Utilities . SetMatrixCS ( cmd , cs , "_ViewMatrix" , viewMatrix ) ;
Utilities . SetMatrixCS ( cmd , cs , "_InvViewMatrix" , viewMatrix . inverse ) ;
Utilities . SetMatrixCS ( cmd , cs , "_ProjMatrix" , projMatrix ) ;
Utilities . SetMatrixCS ( cmd , cs , "_InvProjMatrix" , projMatrix . inverse ) ;
Utilities . SetMatrixCS ( cmd , cs , "_ViewProjMatrix" , viewProjMatrix ) ;
Utilities . SetMatrixCS ( cmd , cs , "_InvViewProjMatrix" , viewProjMatrix . inverse ) ;
cmd . SetComputeVectorParam ( cs , "_InvProjParam" , invProjParam ) ;
cmd . SetComputeVectorParam ( cs , "_ScreenSize" , screenSize ) ;
Utilities . SetMatrixCS ( cmd , cs , "_PrevViewProjMatrix" , prevViewProjMatrix ) ;
cmd . SetComputeMatrixParam ( cs , "_ViewMatrix" , viewMatrix ) ;
cmd . SetComputeMatrixParam ( cs , "_InvViewMatrix" , viewMatrix . inverse ) ;
cmd . SetComputeMatrixParam ( cs , "_ProjMatrix" , projMatrix ) ;
cmd . SetComputeMatrixParam ( cs , "_InvProjMatrix" , projMatrix . inverse ) ;
cmd . SetComputeMatrixParam ( cs , "_NonJitteredViewProjMatrix" , nonJitteredViewProjMatrix ) ;
cmd . SetComputeMatrixParam ( cs , "_ViewProjMatrix" , viewProjMatrix ) ;
cmd . SetComputeMatrixParam ( cs , "_InvViewProjMatrix" , viewProjMatrix . inverse ) ;
cmd . SetComputeVectorParam ( cs , "_InvProjParam" , invProjParam ) ;
cmd . SetComputeVectorParam ( cs , "_ScreenSize" , screenSize ) ;
cmd . SetComputeMatrixParam ( cs , "_PrevViewProjMatrix" , prevViewProjMatrix ) ;
cmd . SetComputeVectorArrayParam ( cs , "_FrustumPlanes" , frustumPlaneEquations ) ;
}
}
readonly GBufferManager m_gbufferManager = new GBufferManager ( ) ;
Material m_CopyStencilBuffer ;
// Various set of material use in render loop
Material m_FilterAndCombineSubsurfaceScattering ;
// Old SSS Model >>>
readonly RenderTargetIdentifier m_DistortionBufferRT ;
private RenderTexture m_CameraDepthStencilBuffer = null ;
private RenderTexture m_CameraDepthStencilBufferCopy = null ;
private RenderTexture m_CameraDepthBufferCopy = null ;
private RenderTexture m_CameraStencilBufferCopy = null ; // Currently, it's manually copied using a pixel shader, and optimized to only contain the SSS bit
private RenderTargetIdentifier m_CameraDepthStencilBufferCopyRT ;
private RenderTargetIdentifier m_CameraDepthBufferCopyRT ;
private RenderTargetIdentifier m_CameraStencilBufferCopyRT ;
// Post-processing context and screen-space effects (recycled on every frame to avoid GC alloc)
readonly PostProcessRenderContext m_PostProcessContext ;
// Currently we use only 2 bits to identify the kind of lighting that is expected from the render pipeline
// Usage is define in LightDefinitions.cs
[Flags]
public enum StencilBits
public enum StencilBitMask
Lighting = 3 , // 0
All = 2 5 5 // 0xFF
Clear = 0 , // 0x0
Lighting = 3 , // 0x3 - 2 bit
All = 2 5 5 // 0xFF - 8 bit
}
// Detect when windows size is changing
CreateSssMaterials ( sssSettings . useDisneySSS ) ;
// <<< Old SSS Model
m_CopyStencilBuffer = Utilities . CreateEngineMaterial ( "Hidden/HDRenderPipeline/CopyStencilBuffer" ) ;
m_CameraMotionVectorsMaterial = Utilities . CreateEngineMaterial ( "Hidden/HDRenderPipeline/CameraMotionVectors" ) ;
InitializeDebugMaterials ( ) ;
} ;
#endif
void CreateDepthBuffer ( Camera camera )
void CreateDepthStencil Buffer ( Camera camera )
{
if ( m_CameraDepthStencilBuffer ! = null )
{
if ( NeedDepthBufferCopy ( ) )
{
if ( m_CameraDepthStencilBufferCopy ! = null )
if ( m_CameraDepthBufferCopy ! = null )
m_CameraDepthStencilBufferCopy . Release ( ) ;
m_CameraDepthBufferCopy . Release ( ) ;
m_CameraDepthStencilBufferCopy = new RenderTexture ( camera . pixelWidth , camera . pixelHeight , 2 4 , RenderTextureFormat . Depth ) ;
m_CameraDepthStencilBufferCopy . filterMode = FilterMode . Point ;
m_CameraDepthStencilBufferCopy . Create ( ) ;
m_CameraDepthStencilBufferCopyRT = new RenderTargetIdentifier ( m_CameraDepthStencilBufferCopy ) ;
m_CameraDepthBufferCopy = new RenderTexture ( camera . pixelWidth , camera . pixelHeight , 2 4 , RenderTextureFormat . Depth ) ;
m_CameraDepthBufferCopy . filterMode = FilterMode . Point ;
m_CameraDepthBufferCopy . Create ( ) ;
m_CameraDepthBufferCopyRT = new RenderTargetIdentifier ( m_CameraDepthBufferCopy ) ;
}
if ( NeedStencilBufferCopy ( ) )
{
if ( m_CameraStencilBufferCopy ! = null )
{
m_CameraStencilBufferCopy . Release ( ) ;
}
m_CameraStencilBufferCopy = new RenderTexture ( camera . pixelWidth , camera . pixelHeight , 0 , RenderTextureFormat . R8 ) ;
m_CameraStencilBufferCopy . filterMode = FilterMode . Point ;
m_CameraStencilBufferCopy . Create ( ) ;
m_CameraStencilBufferCopyRT = new RenderTargetIdentifier ( m_CameraStencilBufferCopy ) ;
}
}
if ( resolutionChanged | | m_CameraDepthStencilBuffer = = null )
{
CreateDepthBuffer ( camera ) ;
CreateDepthStencil Buffer ( camera ) ;
}
if ( resolutionChanged | | m_LightLoop . NeedResize ( ) )
m_CurrentHeight = camera . pixelHeight ;
}
public void PushGlobalParams ( HDCamera hdCamera , ScriptableRenderContext renderContext , SubsurfaceScatteringSettings sssParameters )
public void PushGlobalParams ( HDCamera hdCamera , CommandBuffer cmd , SubsurfaceScatteringSettings sssParameters )
var cmd = CommandBufferPool . Get ( "Push Global Parameters" ) ;
using ( new Utilities . ProfilingSample ( "Push Global Parameters" , cmd ) )
{
hdCamera . SetupGlobalParams ( cmd ) ;
hdCamera . SetupGlobalParams ( cmd ) ;
// TODO: cmd.SetGlobalInt() does not exist, so we are forced to use Shader.SetGlobalInt() instead.
// TODO: cmd.SetGlobalInt() does not exist, so we are forced to use Shader.SetGlobalInt() instead.
if ( m_SkyManager . IsSkyValid ( ) )
{
m_SkyManager . SetGlobalSkyTexture ( ) ;
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 1 ) ;
}
else
{
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 0 ) ;
}
if ( m_SkyManager . IsSkyValid ( ) )
{
m_SkyManager . SetGlobalSkyTexture ( ) ;
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 1 ) ;
}
else
{
Shader . SetGlobalInt ( "_EnvLightSkyEnabled" , 0 ) ;
// Broadcast SSS parameters to all shaders.
Shader . SetGlobalInt ( "_EnableSSSAndTransmission" , m_DebugDisplaySettings . renderingDebugSettings . enableSSSAndTransmission ? 1 : 0 ) ;
Shader . SetGlobalInt ( "_TexturingModeFlags" , ( int ) sssParameters . texturingModeFlags ) ;
Shader . SetGlobalInt ( "_TransmissionFlags" , ( int ) sssParameters . transmissionFlags ) ;
cmd . SetGlobalVectorArray ( "_ThicknessRemaps" , sssParameters . thicknessRemaps ) ;
// We are currently supporting two different SSS mode: Jimenez (with 2-Gaussian profile) and Disney
// We have added the ability to switch between each other for subsurface scattering, but for transmittance this is more tricky as we need to add
// shader variant for forward, gbuffer and deferred shader. We want to avoid this.
// So for transmittance we use Disney profile formulation (that we know is more correct) in both case, and in the case of Jimenez we hack the parameters with 2-Gaussian parameters (Ideally we should fit but haven't find good fit) so it approximately match.
// Note: Jimenez SSS is in cm unit whereas Disney is in mm unit making an inconsistency here to compare model side by side
cmd . SetGlobalVectorArray ( "_ShapeParams" , sssParameters . useDisneySSS ? sssParameters . shapeParams : sssParameters . halfRcpWeightedVariances ) ;
cmd . SetGlobalVectorArray ( "_TransmissionTints" , sssParameters . transmissionTints ) ;
// Broadcast SSS parameters to all shaders.
Shader . SetGlobalInt ( "_EnableSSSAndTransmission" , m_DebugDisplaySettings . renderingDebugSettings . enableSSSAndTransmission ? 1 : 0 ) ;
Shader . SetGlobalInt ( "_TexturingModeFlags" , ( int ) sssParameters . texturingModeFlags ) ;
Shader . SetGlobalInt ( "_TransmissionFlags" , ( int ) sssParameters . transmissionFlags ) ;
cmd . SetGlobalFloatArray ( "_ThicknessRemaps" , sssParameters . thicknessRemaps ) ;
// We are currently supporting two different SSS mode: Jimenez (with 2-Gaussian profile) and Disney
// We have added the ability to switch between each other for subsurface scattering, but for transmittance this is more tricky as we need to add
// shader variant for forward, gbuffer and deferred shader. We want to avoid this.
// So for transmittance we use Disney profile formulation (that we know is more correct) in both case, and in the case of Jimenez we hack the parameters with 2-Gaussian parameters (Ideally we should fit but haven't find good fit) so it approximately match.
// Note: Jimenez SSS is in cm unit whereas Disney is in mm unit making an inconsistency here to compare model side by side
cmd . SetGlobalVectorArray ( "_ShapeParams" , sssParameters . useDisneySSS ? sssParameters . shapeParams : sssParameters . halfRcpWeightedVariances ) ;
cmd . SetGlobalVectorArray ( "_TransmissionTints" , sssParameters . transmissionTints ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
}
bool NeedDepthBufferCopy ( )
}
Texture GetDepthTexture ( )
bool NeedStencilBufferCopy ( )
if ( NeedDepthBufferCopy ( ) )
return m_CameraDepthStencilBufferCopy ;
else
return m_CameraDepthStencilBuffer ;
// Currently, Unity does not offer a way to bind the stencil buffer as a texture in a compute shader.
// Therefore, it's manually copied using a pixel shader, and optimized to only contain the SSS bit.
return m_DebugDisplaySettings . renderingDebugSettings . enableSSSAndTransmission ;
private void CopyDepthBufferIfNeeded ( ScriptableRenderContext renderContext )
RenderTargetIdentifier GetDepthTexture ( )
var cmd = CommandBufferPool . Get ( NeedDepthBufferCopy ( ) ? "Copy DepthBuffer" : "Set DepthBuffer" ) ;
return NeedDepthBufferCopy ( ) ? m_CameraDepthBufferCopy : m_CameraDepthStencilBuffer ;
}
if ( NeedDepthBufferCopy ( ) )
RenderTargetIdentifier GetStencilTexture ( )
{
return NeedStencilBufferCopy ( ) ? m_CameraStencilBufferCopyRT : m_CameraDepthStencilBufferRT ;
}
private void CopyDepthBufferIfNeeded ( CommandBuffer cmd )
{
using ( new Utilities . ProfilingSample ( NeedDepthBufferCopy ( ) ? "Copy DepthBuffer" : "Set DepthBuffer" , cmd ) )
{
if ( NeedDepthBufferCopy ( ) )
{
using ( new Utilities . ProfilingSample ( "Copy depth-stencil buffer" , cmd ) )
{
cmd . CopyTexture ( m_CameraDepthStencilBufferRT , m_CameraDepthBufferCopyRT ) ;
}
}
cmd . SetGlobalTexture ( "_MainDepthTexture" , GetDepthTexture ( ) ) ;
}
}
private void PrepareAndBindStencilTexture ( CommandBuffer cmd )
{
if ( NeedStencilBufferCopy ( ) )
using ( new Utilities . ProfilingSample ( "Copy depth-stencil buffer" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Copy StencilBuffer" , cmd ) )
cmd . CopyTexture ( m_CameraDepthStencilBufferRT , m_CameraDepthStencilBufferCopyRT ) ;
Utilities . DrawFullScreen ( cmd , m_CopyStencilBuffer , m_CameraStencilBufferCopyRT , m_CameraDepthStencilBufferRT ) ;
cmd . SetGlobalTexture ( "_MainDepthTexture" , GetDepthTexture ( ) ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
cmd . SetGlobalTexture ( "_StencilTexture" , GetStencilTexture ( ) ) ;
}
public void UpdateCommonSettings ( )
GraphicsSettings . lightsUseLinearIntensity = true ;
GraphicsSettings . lightsUseColorTemperature = true ;
m_MaterialList . ForEach ( material = > material . RenderInit ( renderContext ) ) ;
// This is the main command buffer used for the frame.
CommandBuffer cmd = CommandBufferPool . Get ( "" ) ;
m_MaterialList . ForEach ( material = > material . RenderInit ( cmd ) ) ;
// Do anything we need to do upon a new frame.
m_LightLoop . NewFrame ( ) ;
renderContext . SetupCameraProperties ( camera ) ;
var hdCamera = HDCamera . Get ( camera ) ;
var postProcessLayer = camera . GetComponent < PostProcessLayer > ( ) ;
HDCamera hdCamera = HDCamera . Get ( camera , postProcessLayer ) ;
PushGlobalParams ( hdCamera , cmd , m_Asset . sssSettings ) ;
// TODO: Find a correct place to bind these material textures
// We have to bind the material specific global parameters in this mode
// TODO: Add another path dedicated to planar reflection / real time cubemap that implement simpler lighting
string passName = "Forward" ; // It is up to the users to only send unlit object for this camera path
using ( new Utilities . ProfilingSample ( passName , renderContext ) )
using ( new Utilities . ProfilingSample ( passName , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor | ClearFlag . ClearDepth ) ;
RenderOpaqueRenderList ( m_CullResults , camera , renderContext , passName ) ;
RenderTransparentRenderList ( m_CullResults , camera , renderContext , passName ) ;
Utilities . SetRenderTarget ( cmd , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor | ClearFlag . ClearDepth ) ;
RenderOpaqueRenderList ( m_CullResults , camera , renderContext , cmd , passName ) ;
RenderTransparentRenderList ( m_CullResults , camera , renderContext , cmd , passName ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
InitAndClearBuffer ( camera , renderContext ) ;
PushGlobalParams ( hdCamera , renderContext , m_Asset . sssSettings ) ;
InitAndClearBuffer ( camera , cmd ) ;
RenderDepthPrepass ( m_CullResults , camera , renderContext ) ;
RenderDepthPrepass ( m_CullResults , camera , renderContext , cmd ) ;
RenderForwardOnlyOpaqueDepthPrepass ( m_CullResults , camera , renderContext ) ;
RenderGBuffer ( m_CullResults , camera , renderContext ) ;
RenderForwardOnlyOpaqueDepthPrepass ( m_CullResults , camera , renderContext , cmd ) ;
RenderGBuffer ( m_CullResults , camera , renderContext , cmd ) ;
CopyDepthBufferIfNeeded ( renderContext ) ;
CopyDepthBufferIfNeeded ( cmd ) ;
// Required for the SSS pass.
PrepareAndBindStencilTexture ( cmd ) ;
RenderDebugViewMaterial ( m_CullResults , hdCamera , renderContext ) ;
RenderDebugViewMaterial ( m_CullResults , hdCamera , renderContext , cmd ) ;
using ( new Utilities . ProfilingSample ( "Build Light list and render shadows" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Build Light list and render shadows" , cmd ) )
m_SsaoEffect . Render ( ssaoSettingsToUse , this , hdCamera , renderContext , m_Asset . renderingSettings . useForwardRenderingOnly ) ;
m_SsaoEffect . Render ( ssaoSettingsToUse , this , hdCamera , renderContext , cmd , m_Asset . renderingSettings . useForwardRenderingOnly ) ;
m_LightLoop . RenderShadows ( renderContext , m_CullResults ) ;
m_LightLoop . RenderShadows ( renderContext , cmd , m_CullResults ) ;
m_LightLoop . BuildGPULightLists ( camera , renderContext , m_CameraDepthStencilBufferRT ) ;
m_LightLoop . BuildGPULightLists ( camera , cmd , m_CameraDepthStencilBufferRT ) ;
UpdateSkyEnvironment ( hdCamera , renderContext ) ;
UpdateSkyEnvironment ( hdCamera , cmd ) ;
RenderDeferredLighting ( hdCamera , renderContext ) ;
RenderDeferredLighting ( hdCamera , cmd ) ;
CombineSubsurfaceScattering ( hdCamera , renderContext , m_Asset . sssSettings ) ;
CombineSubsurfaceScattering ( hdCamera , cmd , m_Asset . sssSettings ) ;
RenderForward ( m_CullResults , camera , renderContext , true ) ; // Render deferred or forward opaque
RenderForwardOnlyOpaque ( m_CullResults , camera , renderContext ) ;
RenderForward ( m_CullResults , camera , renderContext , cmd , true ) ; // Render deferred or forward opaque
RenderForwardOnlyOpaque ( m_CullResults , camera , renderContext , cmd ) ;
RenderLightingDebug ( hdCamera , renderContext , m_CameraColorBufferRT ) ;
RenderLightingDebug ( hdCamera , cmd , m_CameraColorBufferRT , m_DebugDisplaySettings ) ;
CopyDepthBufferIfNeeded ( renderContext ) ;
CopyDepthBufferIfNeeded ( cmd ) ;
RenderSky ( hdCamera , renderContext ) ;
RenderSky ( hdCamera , cmd ) ;
RenderForward ( m_CullResults , camera , renderContext , false ) ;
RenderForward ( m_CullResults , camera , renderContext , cmd , false ) ;
PushFullScreenDebugTexture ( cmd , m_CameraColorBuffer , camera , renderContext , FullScreenDebugMode . NanTracker ) ;
// Simple blit
var cmd = CommandBufferPool . Get ( "Blit to final RT" ) ;
cmd . Blit ( m_CameraColorBufferRT , BuiltinRenderTextureType . CameraTarget ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
using ( new Utilities . ProfilingSample ( "Blit to final RT" , cmd ) )
{
// Simple blit
cmd . Blit ( m_CameraColorBufferRT , BuiltinRenderTextureType . CameraTarget ) ;
}
RenderVelocity ( m_CullResults , hdCamera , 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 ?
RenderVelocity ( m_CullResults , hdCamera , renderContext , cmd ) ; // 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 ?
RenderDistortion ( m_CullResults , camera , renderContext ) ;
RenderDistortion ( m_CullResults , camera , renderContext , cmd ) ;
RenderPostProcesses ( camera , renderContext ) ;
RenderPostProcesses ( camera , cmd , postProcessLayer ) ;
RenderDebug ( hdCamera , renderContext ) ;
RenderDebug ( hdCamera , cmd ) ;
var cmd = CommandBufferPool . Get ( ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
void RenderOpaqueRenderList ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , string passName , RendererConfiguration rendererConfiguration = 0 )
void RenderOpaqueRenderList ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd , string passName , RendererConfiguration rendererConfiguration = 0 )
// This is done here because DrawRenderers API lives outside command buffers so we need to make call this before doing any DrawRenders
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Clear ( ) ;
var settings = new DrawRendererSettings ( cull , camera , new ShaderPassName ( passName ) )
{
renderContext . DrawRenderers ( ref settings ) ;
}
void RenderTransparentRenderList ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , string passName , RendererConfiguration rendererConfiguration = 0 )
void RenderTransparentRenderList ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd , string passName , RendererConfiguration rendererConfiguration = 0 )
// This is done here because DrawRenderers API lives outside command buffers so we need to make call this before doing any DrawRenders
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Clear ( ) ;
var settings = new DrawRendererSettings ( cull , camera , new ShaderPassName ( passName ) )
{
renderContext . DrawRenderers ( ref settings ) ;
}
void RenderDepthPrepass ( CullResults cull , Camera camera , ScriptableRenderContext renderContext )
void RenderDepthPrepass ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd )
{
// If we are forward only we will do a depth prepass
// TODO: Depth prepass should be enabled based on light loop settings. LightLoop define if they need a depth prepass + forward only...
using ( new Utilities . ProfilingSample ( "Depth Prepass" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Depth Prepass" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraDepthStencilBufferRT ) ;
RenderOpaqueRenderList ( cull , camera , renderContext , "DepthOnly" ) ;
Utilities . SetRenderTarget ( cmd , m_CameraDepthStencilBufferRT ) ;
RenderOpaqueRenderList ( cull , camera , renderContext , cmd , "DepthOnly" ) ;
void RenderGBuffer ( CullResults cull , Camera camera , ScriptableRenderContext renderContext )
void RenderGBuffer ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd )
{
if ( m_Asset . renderingSettings . ShouldUseForwardRenderingOnly ( ) )
{
string passName = m_DebugDisplaySettings . IsDebugDisplayEnabled ( ) ? "GBufferDebugDisplay" : "GBuffer" ;
using ( new Utilities . ProfilingSample ( passName , renderContext ) )
using ( new Utilities . ProfilingSample ( passName , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_gbufferManager . GetGBuffers ( ) , m_CameraDepthStencilBufferRT ) ;
Utilities . SetRenderTarget ( cmd , m_gbufferManager . GetGBuffers ( ) , m_CameraDepthStencilBufferRT ) ;
RenderOpaqueRenderList ( cull , camera , renderContext , passName , Utilities . kRendererConfigurationBakedLighting ) ;
RenderOpaqueRenderList ( cull , camera , renderContext , cmd , passName , Utilities . kRendererConfigurationBakedLighting ) ;
void RenderForwardOnlyOpaqueDepthPrepass ( CullResults cull , Camera camera , ScriptableRenderContext renderContext )
void RenderForwardOnlyOpaqueDepthPrepass ( CullResults cull , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd )
{
// If we are forward only we don't need to render ForwardOnlyOpaqueDepthOnly object
// But in case we request a prepass we render it
using ( new Utilities . ProfilingSample ( "Forward opaque depth" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Forward opaque depth" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraDepthStencilBufferRT ) ;
RenderOpaqueRenderList ( cull , camera , renderContext , "ForwardOnlyOpaqueDepthOnly" ) ;
Utilities . SetRenderTarget ( cmd , m_CameraDepthStencilBufferRT ) ;
RenderOpaqueRenderList ( cull , camera , renderContext , cmd , "ForwardOnlyOpaqueDepthOnly" ) ;
void RenderDebugViewMaterial ( CullResults cull , HDCamera hdCamera , ScriptableRenderContext renderContext )
void RenderDebugViewMaterial ( CullResults cull , HDCamera hdCamera , ScriptableRenderContext renderContext , CommandBuffer cmd )
using ( new Utilities . ProfilingSample ( "DisplayDebug ViewMaterial" , renderContext ) )
using ( new Utilities . ProfilingSample ( "DisplayDebug ViewMaterial" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , Utilities . kClearAll , Color . black ) ;
RenderOpaqueRenderList ( cull , hdCamera . camera , renderContext , "ForwardDisplayDebug" , Utilities . kRendererConfigurationBakedLighting ) ;
Utilities . SetRenderTarget ( cmd , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , Utilities . kClearAll , Color . black ) ;
RenderOpaqueRenderList ( cull , hdCamera . camera , renderContext , cmd , "ForwardDisplayDebug" , Utilities . kRendererConfigurationBakedLighting ) ;
// TODO: Bind depth textures
var cmd = CommandBufferPool . Get ( "DebugViewMaterialGBuffer" ) ;
cmd . Blit ( null , m_CameraColorBufferRT , m_DebugViewMaterialGBuffer , 0 ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
using ( new Utilities . ProfilingSample ( "DebugViewMaterialGBuffer" , cmd ) )
{
// TODO: Bind depth textures
cmd . Blit ( null , m_CameraColorBufferRT , m_DebugViewMaterialGBuffer , 0 ) ;
}
RenderTransparentRenderList ( cull , hdCamera . camera , renderContext , "ForwardDisplayDebug" , Utilities . kRendererConfigurationBakedLighting ) ;
RenderTransparentRenderList ( cull , hdCamera . camera , renderContext , cmd , "ForwardDisplayDebug" , Utilities . kRendererConfigurationBakedLighting ) ;
var cmd = CommandBufferPool . Get ( "Blit DebugView Material Debug" ) ;
cmd . Blit ( m_CameraColorBufferRT , BuiltinRenderTextureType . CameraTarget ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
using ( new Utilities . ProfilingSample ( "Blit DebugView Material Debug" , cmd ) )
{
cmd . Blit ( m_CameraColorBufferRT , BuiltinRenderTextureType . CameraTarget ) ;
}
void RenderDeferredLighting ( HDCamera hdCamera , ScriptableRenderContext renderContext )
void RenderDeferredLighting ( HDCamera hdCamera , CommandBuffer cmd )
{
if ( m_Asset . renderingSettings . ShouldUseForwardRenderingOnly ( ) )
{
RenderTargetIdentifier [ ] colorRTs = { m_CameraColorBufferRT , m_CameraSubsurfaceBufferRT } ;
RenderTargetIdentifier [ ] colorRTs = { m_CameraColorBufferRT , m_CameraSubsurfaceBufferRT } ;
RenderTargetIdentifier depthTexture = GetDepthTexture ( ) ;
m_LightLoop . RenderDeferredLighting ( hdCamera , renderContext , m_DebugDisplaySettings , colorRTs , m_CameraDepthStencilBufferRT , new RenderTargetIdentifier ( GetDepthTexture ( ) ) , true ) ;
m_LightLoop . RenderDeferredLighting ( hdCamera , cmd , m_DebugDisplaySettings , colorRTs , m_CameraDepthStencilBufferRT , depthTexture , true ) ;
m_LightLoop . RenderDeferredLighting ( hdCamera , renderContext , m_DebugDisplaySettings , colorRTs , m_CameraDepthStencilBufferRT , new RenderTargetIdentifier ( GetDepthTexture ( ) ) , false ) ;
m_LightLoop . RenderDeferredLighting ( hdCamera , cmd , m_DebugDisplaySettings , colorRTs , m_CameraDepthStencilBufferRT , depthTexture , false ) ;
void CombineSubsurfaceScattering ( HDCamera hdCamera , ScriptableRenderContext context , SubsurfaceScatteringSettings sssParameters )
void CombineSubsurfaceScattering ( HDCamera hdCamera , CommandBuffer cmd , SubsurfaceScatteringSettings sssParameters )
var cmd = CommandBufferPool . Get ( "Subsurface Scattering" ) ;
if ( sssSettings . useDisneySSS )
using ( new Utilities . ProfilingSample ( "Subsurface Scattering" , cmd ) )
cmd . SetGlobalTexture ( "_IrradianceSource" , m_CameraSubsurfaceBufferRT ) ; // Cannot set a RT on a material
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_WorldScales" , sssParameters . worldScales ) ;
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_FilterKernelsNearField" , sssParameters . filterKernelsNearField ) ;
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_FilterKernelsFarField" , sssParameters . filterKernelsFarField ) ;
if ( sssSettings . useDisneySSS )
{
cmd . SetGlobalTexture ( "_IrradianceSource" , m_CameraSubsurfaceBufferRT ) ; // Cannot set a RT on a material
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_WorldScales" , sssParameters . worldScales ) ;
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_FilterKernelsNearField" , sssParameters . filterKernelsNearField ) ;
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_FilterKernelsFarField" , sssParameters . filterKernelsFarField ) ;
Utilities . DrawFullScreen ( cmd , m_FilterAndCombineSubsurfaceScattering , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
}
else
{
// Perform the vertical SSS filtering pass.
cmd . SetGlobalTexture ( "_IrradianceSource" , m_CameraSubsurfaceBufferRT ) ; // Cannot set a RT on a material
m_FilterSubsurfaceScattering . SetFloatArray ( "_WorldScales" , sssParameters . worldScales ) ;
m_FilterSubsurfaceScattering . SetVectorArray ( "_FilterKernelsBasic" , sssParameters . filterKernelsBasic ) ;
m_FilterSubsurfaceScattering . SetVectorArray ( "_HalfRcpWeightedVariances" , sssParameters . halfRcpWeightedVariances ) ;
Utilities . DrawFullScreen ( cmd , m_FilterSubsurfaceScattering , m_CameraFilteringBufferRT , m_CameraDepthStencilBufferRT ) ;
Utilities . DrawFullScreen ( cmd , m_FilterAndCombineSubsurfaceScattering , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
}
else
{
// Perform the vertical SSS filtering pass.
cmd . SetGlobalTexture ( "_IrradianceSource" , m_CameraSubsurfaceBufferRT ) ; // Cannot set a RT on a material
m_FilterSubsurfaceScattering . SetFloatArray ( "_WorldScales" , sssParameters . worldScales ) ;
m_FilterSubsurfaceScattering . SetVectorArray ( "_FilterKernelsBasic" , sssParameters . filterKernelsBasic ) ;
m_FilterSubsurfaceScattering . SetVectorArray ( "_HalfRcpWeightedVariances" , sssParameters . halfRcpWeightedVariances ) ;
Utilities . DrawFullScreen ( cmd , m_FilterSubsurfaceScattering , m_CameraFilteringBufferRT , m_CameraDepthStencilBufferRT ) ;
// Perform the horizontal SSS filtering pass, and combine diffuse and specular lighting.
cmd . SetGlobalTexture ( "_IrradianceSource" , m_CameraFilteringBufferRT ) ; // Cannot set a RT on a material
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_WorldScales" , sssParameters . worldScales ) ;
m_FilterAndCombineSubsurfaceScattering . SetVectorArray ( "_FilterKernelsBasic" , sssParameters . filterKernelsBasic ) ;
m_FilterAndCombineSubsurfaceScattering . SetVectorArray ( "_HalfRcpWeightedVariances" , sssParameters . halfRcpWeightedVariances ) ;
Utilities . DrawFullScreen ( cmd , m_FilterAndCombineSubsurfaceScattering , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
// Perform the horizontal SSS filtering pass, and combine diffuse and specular lighting.
cmd . SetGlobalTexture ( "_IrradianceSource" , m_CameraFilteringBufferRT ) ; // Cannot set a RT on a material
m_FilterAndCombineSubsurfaceScattering . SetFloatArray ( "_WorldScales" , sssParameters . worldScales ) ;
m_FilterAndCombineSubsurfaceScattering . SetVectorArray ( "_FilterKernelsBasic" , sssParameters . filterKernelsBasic ) ;
m_FilterAndCombineSubsurfaceScattering . SetVectorArray ( "_HalfRcpWeightedVariances" , sssParameters . halfRcpWeightedVariances ) ;
Utilities . DrawFullScreen ( cmd , m_FilterAndCombineSubsurfaceScattering , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
}
context . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
void UpdateSkyEnvironment ( HDCamera hdCamera , ScriptableRenderContext renderContext )
void UpdateSkyEnvironment ( HDCamera hdCamera , CommandBuffer cmd )
m_SkyManager . UpdateEnvironment ( hdCamera , m_LightLoop . GetCurrentSunLight ( ) , renderContext ) ;
m_SkyManager . UpdateEnvironment ( hdCamera , m_LightLoop . GetCurrentSunLight ( ) , cmd ) ;
void RenderSky ( HDCamera hdCamera , ScriptableRenderContext renderContext )
void RenderSky ( HDCamera hdCamera , CommandBuffer cmd )
m_SkyManager . RenderSky ( hdCamera , m_LightLoop . GetCurrentSunLight ( ) , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , renderContext ) ;
m_SkyManager . RenderSky ( hdCamera , m_LightLoop . GetCurrentSunLight ( ) , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , cmd ) ;
}
public Texture2D ExportSkyToTexture ( )
void RenderLightingDebug ( HDCamera camera , ScriptableRenderContext renderContext , RenderTargetIdentifier colorBuffer )
void RenderLightingDebug ( HDCamera camera , CommandBuffer cmd , RenderTargetIdentifier colorBuffer , DebugDisplaySettings debugDisplaySettings )
m_LightLoop . RenderLightingDebug ( camera , renderContext , colorBuffer ) ;
m_LightLoop . RenderLightingDebug ( camera , cmd , colorBuffer , debugDisplaySettings ) ;
void RenderForward ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext , bool renderOpaque )
void RenderForward ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd , bool renderOpaque )
{
// TODO: Currently we can't render opaque object forward when deferred is enabled
// miss option
string passName = m_DebugDisplaySettings . IsDebugDisplayEnabled ( ) ? "ForwardDisplayDebug" : "Forward" ;
using ( new Utilities . ProfilingSample ( passName , renderContext ) )
using ( new Utilities . ProfilingSample ( passName , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
Utilities . SetRenderTarget ( cmd , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
m_LightLoop . RenderForward ( camera , renderContext , renderOpaque ) ;
m_LightLoop . RenderForward ( camera , cmd , renderOpaque ) ;
RenderOpaqueRenderList ( cullResults , camera , renderContext , passName , Utilities . kRendererConfigurationBakedLighting ) ;
RenderOpaqueRenderList ( cullResults , camera , renderContext , cmd , passName , Utilities . kRendererConfigurationBakedLighting ) ;
RenderTransparentRenderList ( cullResults , camera , renderContext , passName , Utilities . kRendererConfigurationBakedLighting ) ;
RenderTransparentRenderList ( cullResults , camera , renderContext , cmd , passName , Utilities . kRendererConfigurationBakedLighting ) ;
void RenderForwardOnlyOpaque ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext )
void RenderForwardOnlyOpaque ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd )
using ( new Utilities . ProfilingSample ( passName , renderContext ) )
using ( new Utilities . ProfilingSample ( passName , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
Utilities . SetRenderTarget ( cmd , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
m_LightLoop . RenderForward ( camera , renderContext , true ) ;
m_LightLoop . RenderForward ( camera , cmd , true ) ;
RenderOpaqueRenderList ( cullResults , camera , renderContext , passName , Utilities . kRendererConfigurationBakedLighting ) ;
RenderOpaqueRenderList ( cullResults , camera , renderContext , cmd , passName , Utilities . kRendererConfigurationBakedLighting ) ;
void RenderVelocity ( CullResults cullResults , HDCamera hdcam , ScriptableRenderContext renderContext )
void RenderVelocity ( CullResults cullResults , HDCamera hdcam , ScriptableRenderContext renderContext , CommandBuffer cmd )
using ( new Utilities . ProfilingSample ( "Velocity" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Velocity" , cmd ) )
{
// If opaque velocity have been render during GBuffer no need to render it here
if ( ( ShaderConfig . s_VelocityInGbuffer = = 1 ) | | m_Asset . renderingSettings . ShouldUseForwardRenderingOnly ( ) )
int w = ( int ) hdcam . screenSize . x ;
int h = ( int ) hdcam . screenSize . y ;
var cmd = CommandBufferPool . Get ( "" ) ;
m_CameraMotionVectorsMaterial . SetVector ( "_CameraPosDiff" , hdcam . prevCameraPos - hdcam . cameraPos ) ;
cmd . Blit ( BuiltinRenderTextureType . None , m_VelocityBufferRT , m_CameraMotionVectorsMaterial , 0 ) ;
Utilities . DrawFullScreen ( cmd , m_CameraMotionVectorsMaterial , m_VelocityBufferRT , null , 0 ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
RenderOpaqueRenderList ( cullResults , hdcam . camera , renderContext , cmd , "MotionVectors" , RendererConfiguration . PerObjectMotionVectors ) ;
RenderOpaqueRenderList ( cullResults , hdcam . camera , renderContext , "MotionVectors" , RendererConfiguration . PerObjectMotionVectors ) ;
PushFullScreenDebugTexture ( cmd , m_VelocityBuffer , hdcam . camera , renderContext , FullScreenDebugMode . MotionVectors ) ;
void RenderDistortion ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext )
void RenderDistortion ( CullResults cullResults , Camera camera , ScriptableRenderContext renderContext , CommandBuffer cmd )
using ( new Utilities . ProfilingSample ( "Distortion" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Distortion" , cmd ) )
var cmd = CommandBufferPool . Get ( "" ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
RenderTransparentRenderList ( cullResults , camera , renderContext , "DistortionVectors" ) ;
RenderTransparentRenderList ( cullResults , camera , renderContext , cmd , "DistortionVectors" ) ;
void RenderPostProcesses ( Camera camera , ScriptableRenderContext renderContext )
void RenderPostProcesses ( Camera camera , CommandBuffer cmd , PostProcessLayer layer )
using ( new Utilities . ProfilingSample ( "Post-processing" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Post-processing" , cmd ) )
var postProcessLayer = camera . GetComponent < PostProcessLayer > ( ) ;
var cmd = CommandBufferPool . Get ( "" ) ;
if ( postProcessLayer ! = null & & postProcessLayer . enabled )
if ( Utilities . IsPostProcessingActive ( layer ) )
{
cmd . SetGlobalTexture ( "_CameraDepthTexture" , GetDepthTexture ( ) ) ;
cmd . SetGlobalTexture ( "_CameraMotionVectorsTexture" , m_VelocityBufferRT ) ;
context . sourceFormat = RenderTextureFormat . ARGBHalf ;
context . flip = true ;
postProcessLayer . Render ( context ) ;
layer . Render ( context ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
}
}
}
MaterialPropertyBlock m_SharedPropertyBlock = new MaterialPropertyBlock ( ) ;
void RenderDebug ( HDCamera camera , ScriptableRenderContext renderContext )
void RenderDebug ( HDCamera camera , CommandBuffer cmd )
// We make sure the depth buffer is bound because we need it to write depth at near plane for overlays otherwise the editor grid end up visible in them.
Utilities . SetRenderTarget ( renderContext , BuiltinRenderTextureType . CameraTarget , m_CameraDepthStencilBufferRT ) ;
using ( new Utilities . ProfilingSample ( "Render Debug" , cmd ) )
{
// We make sure the depth buffer is bound because we need it to write depth at near plane for overlays otherwise the editor grid end up visible in them.
Utilities . SetRenderTarget ( cmd , BuiltinRenderTextureType . CameraTarget , m_CameraDepthStencilBufferRT ) ;
CommandBuffer debugCB = CommandBufferPool . Get ( ) ;
debugCB . name = "Render Debug" ;
// First render full screen debug texture
if ( m_DebugDisplaySettings . lightingDebugSettings . fullScreenDebugMode ! = FullScreenDebugMode . None & & m_FullScreenDebugPushed )
{
m_FullScreenDebugPushed = false ;
cmd . SetGlobalTexture ( "_DebugFullScreenTexture" , m_DebugFullScreenTempRT ) ;
m_DebugFullScreen . SetFloat ( "_FullScreenDebugMode" , ( float ) m_DebugDisplaySettings . lightingDebugSettings . fullScreenDebugMode ) ;
Utilities . DrawFullScreen ( cmd , m_DebugFullScreen , ( RenderTargetIdentifier ) BuiltinRenderTextureType . CameraTarget ) ;
}
// First render full screen debug texture
if ( m_DebugDisplaySettings . lightingDebugSettings . fullScreenDebugMode ! = FullScreenDebugMode . None & & m_FullScreenDebugPushed )
{
m_FullScreenDebugPushed = false ;
debugCB . SetGlobalTexture ( "_DebugFullScreenTexture" , m_DebugFullScreenTempRT ) ;
m_DebugFullScreen . SetFloat ( "_FullScreenDebugMode" , ( float ) m_DebugDisplaySettings . lightingDebugSettings . fullScreenDebugMode ) ;
Utilities . DrawFullScreen ( debugCB , m_DebugFullScreen , ( RenderTargetIdentifier ) BuiltinRenderTextureType . CameraTarget ) ;
}
// Then overlays
float x = 0 ;
float overlayRatio = m_DebugDisplaySettings . debugOverlayRatio ;
float overlaySize = Math . Min ( camera . camera . pixelHeight , camera . camera . pixelWidth ) * overlayRatio ;
float y = camera . camera . pixelHeight - overlaySize ;
// Then overlays
float x = 0 ;
float overlayRatio = m_DebugDisplaySettings . debugOverlayRatio ;
float overlaySize = Math . Min ( camera . camera . pixelHeight , camera . camera . pixelWidth ) * overlayRatio ;
float y = camera . camera . pixelHeight - overlaySize ;
LightingDebugSettings lightingDebug = m_DebugDisplaySettings . lightingDebugSettings ;
LightingDebugSettings lightingDebug = m_DebugDisplaySettings . lightingDebugSettings ;
if ( lightingDebug . displaySkyReflection )
{
Texture skyReflection = m_SkyManager . skyReflection ;
m_SharedPropertyBlock . SetTexture ( "_InputCubemap" , skyReflection ) ;
m_SharedPropertyBlock . SetFloat ( "_Mipmap" , lightingDebug . skyReflectionMipmap ) ;
cmd . SetViewport ( new Rect ( x , y , overlaySize , overlaySize ) ) ;
cmd . DrawProcedural ( Matrix4x4 . identity , m_DebugDisplayLatlong , 0 , MeshTopology . Triangles , 3 , 1 , m_SharedPropertyBlock ) ;
Utilities . NextOverlayCoord ( ref x , ref y , overlaySize , overlaySize , camera . camera . pixelWidth ) ;
}
if ( lightingDebug . displaySkyReflection )
{
Texture skyReflection = m_SkyManager . skyReflection ;
m_SharedPropertyBlock . SetTexture ( "_InputCubemap" , skyReflection ) ;
m_SharedPropertyBlock . SetFloat ( "_Mipmap" , lightingDebug . skyReflectionMipmap ) ;
debugCB . SetViewport ( new Rect ( x , y , overlaySize , overlaySize ) ) ;
debugCB . DrawProcedural ( Matrix4x4 . identity , m_DebugDisplayLatlong , 0 , MeshTopology . Triangles , 3 , 1 , m_SharedPropertyBlock ) ;
Utilities . NextOverlayCoord ( ref x , ref y , overlaySize , overlaySize , camera . camera . pixelWidth ) ;
m_LightLoop . RenderDebugOverlay ( camera . camera , cmd , m_DebugDisplaySettings , ref x , ref y , overlaySize , camera . camera . pixelWidth ) ;
renderContext . ExecuteCommandBuffer ( debugCB ) ;
CommandBufferPool . Release ( debugCB ) ;
m_LightLoop . RenderDebugOverlay ( camera . camera , renderContext , m_DebugDisplaySettings , ref x , ref y , overlaySize , camera . camera . pixelWidth ) ;
void InitAndClearBuffer ( Camera camera , ScriptableRenderContext renderContext )
void InitAndClearBuffer ( Camera camera , CommandBuffer cmd )
using ( new Utilities . ProfilingSample ( "InitAndClearBuffer" , renderContext ) )
using ( new Utilities . ProfilingSample ( "InitAndClearBuffer" , cmd ) )
using ( new Utilities . ProfilingSample ( "InitGBuffers and clear Depth/Stencil" , renderContext ) )
using ( new Utilities . ProfilingSample ( "InitGBuffers and clear Depth/Stencil" , cmd ) )
var cmd = CommandBufferPool . Get ( ) ;
cmd . name = "" ;
// 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.
m_gbufferManager . InitGBuffers ( w , h , cmd ) ;
}
renderContext . ExecuteCommandBuffer ( cmd ) ;
CommandBufferPool . Release ( cmd ) ;
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearDepth ) ;
Utilities . SetRenderTarget ( cmd , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearDepth ) ;
using ( new Utilities . ProfilingSample ( "Clear SSS diffuse target" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Clear SSS diffuse target" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraSubsurfaceBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
Utilities . SetRenderTarget ( cmd , m_CameraSubsurfaceBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
using ( new Utilities . ProfilingSample ( "Clear SSS filtering target" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Clear SSS filtering target" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraFilteringBuffer , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
Utilities . SetRenderTarget ( cmd , m_CameraFilteringBuffer , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
}
// <<< Old SSS Model
using ( new Utilities . ProfilingSample ( "Clear HDR target" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Clear HDR target" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
Utilities . SetRenderTarget ( cmd , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
using ( new Utilities . ProfilingSample ( "Clear GBuffer" , renderContext ) )
using ( new Utilities . ProfilingSample ( "Clear GBuffer" , cmd ) )
Utilities . SetRenderTarget ( renderContext , m_gbufferManager . GetGBuffers ( ) , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
Utilities . SetRenderTarget ( cmd , m_gbufferManager . GetGBuffers ( ) , m_CameraDepthStencilBufferRT , ClearFlag . ClearColor , Color . black ) ;
}
}
// END TEMP