{
private const string k_HDRenderLoopPath = "Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset" ;
// Must be in sync with DebugViewMaterial.hlsl
public enum DebugViewVaryingMode
public class SkyParameters
TexCoord0 = 1 ,
TexCoord1 = 2 ,
TexCoord2 = 3 ,
VertexTangentWS = 4 ,
VertexBitangentWS = 5 ,
VertexNormalWS = 6 ,
VertexColor = 7 ,
public Cubemap skyHDRI ;
public float rotation ;
public float exposure ;
public float multiplier ;
// Must be in sync with DebugViewMaterial.hlsl
public enum DebugViewGbufferMode
[SerializeField]
private SkyParameters m_SkyParameters = new SkyParameters ( ) ;
public SkyParameters skyParameters
Depth = 1 0 ,
BakeDiffuseLighting = 1 1 ,
get { return m_SkyParameters ; }
}
public class DebugParameters
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 enableTonemap = true ;
public float exposure = 0 ;
[SerializeField]
TextureSettings m_TextureSettings = TextureSettings . Default ;
Material m_SkyboxMaterial ;
Material m_SkyHDRIMaterial ;
Material m_DeferredMaterial ;
Material m_FinalPassMaterial ;
s_envLightList = new ComputeBuffer ( MaxLights , System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( EnvLightData ) ) ) ;
s_punctualShadowList = new ComputeBuffer ( MaxShadows , System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( PunctualShadowData ) ) ) ;
// TODO: We need to have an API to send our sky information to Enlighten. For now use a workaround through skybox/cubemap material...
m_SkyboxMaterial = CreateEngineMaterial ( "Skybox/Cubemap" ) ;
RenderSettings . skybox = m_SkyboxMaterial ; // Setup this material as the default to be use in RenderSettings
RenderSettings . ambientIntensity = 1.0f ; // fix this to 1, this parameter should not exist!
RenderSettings . ambientMode = UnityEngine . Rendering . AmbientMode . Skybox ; // Force skybox for our HDRI
RenderSettings . reflectionIntensity = 1.0f ;
m_SkyHDRIMaterial = CreateEngineMaterial ( "Hidden/HDRenderLoop/SkyHDRI" ) ;
m_DeferredMaterial = CreateEngineMaterial ( "Hidden/HDRenderLoop/Deferred" ) ;
m_FinalPassMaterial = CreateEngineMaterial ( "Hidden/HDRenderLoop/FinalPass" ) ;
s_areaLightList . Release ( ) ;
s_envLightList . Release ( ) ;
s_punctualShadowList . Release ( ) ;
if ( m_SkyboxMaterial ) DestroyImmediate ( m_SkyboxMaterial ) ;
if ( m_SkyHDRIMaterial ) DestroyImmediate ( m_SkyHDRIMaterial ) ;
if ( m_DeferredMaterial ) DestroyImmediate ( m_DeferredMaterial ) ;
if ( m_FinalPassMaterial ) DestroyImmediate ( m_FinalPassMaterial ) ;
// END TEMP
}
// TODO: Create an opaque and transparent list!
void RenderOpaqueNoLightingRenderList ( CullResults cull , Camera camera , RenderLoop renderLoop , string passName )
{
if ( ! debugParameters . displayOpaqueObjects )
return ;
var settings = new DrawRendererSettings ( cull , camera , new ShaderPassName ( passName ) )
{
rendererConfiguration = 0 ,
sorting = { sortOptions = SortOptions . SortByMaterialThenMesh }
} ;
settings . inputFilter . SetQueuesOpaque ( ) ;
renderLoop . DrawRenderers ( ref settings ) ;
}
DrawRendererSettings settings = new DrawRendererSettings ( cull , camera , new ShaderPassName ( passName ) ) ;
settings . sorting . sortOptions = SortOptions . SortByMaterialThenMesh ;
var settings = new DrawRendererSettings ( cull , camera , new ShaderPassName ( passName ) )
{
rendererConfiguration = RendererConfiguration . PerObjectLightProbe | RendererConfiguration . PerObjectReflectionProbes | RendererConfiguration . PerObjectLightmaps | RendererConfiguration . PerObjectLightProbeProxyVolume ,
sorting = { sortOptions = SortOptions . SortByMaterialThenMesh }
} ;
settings . rendererConfiguration = RendererConfiguration . PerObjectLightProbe | RendererConfiguration . PerObjectReflectionProbes | RendererConfiguration . PerObjectLightmaps | RendererConfiguration . PerObjectLightProbeProxyVolume ;
renderLoop . DrawRenderers ( ref settings ) ;
}
void RenderTransparentNoLightingRenderList ( CullResults cull , Camera camera , RenderLoop renderLoop , string passName )
{
if ( ! debugParameters . displayTransparentObjects )
return ;
var settings = new DrawRendererSettings ( cull , camera , new ShaderPassName ( passName ) )
{
rendererConfiguration = 0 ,
sorting = { sortOptions = SortOptions . BackToFront }
} ;
settings . inputFilter . SetQueuesTransparent ( ) ;
void RenderTransparentRenderList ( CullResults cull , Camera camera , RenderLoop renderLoop , string passName )
void RenderTransparentRenderList ( CullResults cull , Camera camera , RenderLoop renderLoop , string passName )
{
if ( ! debugParameters . displayTransparentObjects )
return ;
renderLoop . DrawRenderers ( ref settings ) ;
}
void RenderDepthPrepass ( CullResults cull , Camera camera , RenderLoop renderLoop )
{
// 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...
if ( ! debugParameters . useDepthPrepass )
return ;
// TODO: Must do opaque then alpha masked for performance!
// TODO: front to back for opaque and by materal for opaque tested when we split in two
var cmd = new CommandBuffer { name = "Depth Prepass" } ;
cmd . SetRenderTarget ( new RenderTargetIdentifier ( s_CameraDepthBuffer ) ) ;
renderLoop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
RenderOpaqueNoLightingRenderList ( cull , camera , renderLoop , "DepthOnly" ) ;
}
void RenderGBuffer ( CullResults cull , Camera camera , RenderLoop renderLoop )
{
if ( debugParameters . useForwardRenderingOnly )
// render opaque objects into GBuffer
RenderOpaqueRenderList ( cull , camera , renderLoop , "GBuffer" ) ;
}
// This pass is use in case of forward opaque and deferred rendering. We need to render forward objects before tile lighting pass
void RenderForwardOpaqueDepth ( CullResults cull , Camera camera , RenderLoop renderLoop )
{
// If we have render a depth prepass, no need for this pass
if ( debugParameters . useDepthPrepass )
return ;
// TODO: Use the render queue index to only send the forward opaque!
var cmd = new CommandBuffer { name = "Depth Prepass" } ;
cmd . SetRenderTarget ( new RenderTargetIdentifier ( s_CameraDepthBuffer ) ) ;
renderLoop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
RenderOpaqueNoLightingRenderList ( cull , camera , renderLoop , "DepthOnly" ) ;
}
void RenderDebugViewMaterial ( CullResults cull , Camera camera , RenderLoop renderLoop )
cmd . Dispose ( ) ;
}
void RenderSky ( Camera camera , RenderLoop renderLoop )
{
/ *
// Render sky into a cubemap - doesn't happen every frame, can be control
// TODO: do a render to texture here
// Downsample the cubemap and provide it to Enlighten
// TODO: currently workaround is to set the cubemap in a Skybox/cubemap material
//m_SkyboxMaterial.SetTexture(cubemap);
// Render the sky itself
Vector3 [ ] vertData = new Vector3 [ 4 ] ;
vertData [ 0 ] = new Vector3 ( - 1.0f , - 1.0f , 0.0f ) ;
vertData [ 1 ] = new Vector3 ( 1.0f , - 1.0f , 0.0f ) ;
vertData [ 2 ] = new Vector3 ( 1.0f , 1.0f , 0.0f ) ;
vertData [ 3 ] = new Vector3 ( - 1.0f , 1.0f , 0.0f ) ;
Vector3 [ ] eyeVectorData = new Vector3 [ 4 ] ;
// camera.worldToCameraMatrix, camera.projectionMatrix
// Get view vector vased on the frustrum, i.e (invert transform frustrum get position etc...)
eyeVectorData [ 0 ] =
eyeVectorData [ 1 ] =
eyeVectorData [ 2 ] =
eyeVectorData [ 3 ] =
// Write out the mesh
var triangles = new int [ 4 ] ;
for ( int i = 0 ; i < 4 ; i + + )
{
triangles [ i ] = i ;
}
Mesh mesh = new Mesh
{
vertices = vertData ,
normals = eyeVectorData ,
triangles = triangles
} ;
m_SkyHDRIMaterial . SetTexture ( "_Cubemap" , skyParameters . skyHDRI ) ;
m_SkyHDRIMaterial . SetVector ( "_SkyParam" , new Vector4 ( skyParameters . exposure , skyParameters . multiplier , skyParameters . rotation , 0.0f ) ) ;
var cmd = new CommandBuffer { name = "Skybox" } ;
cmd . DrawMesh ( mesh , Matrix4x4 . identity , m_SkyHDRIMaterial ) ;
renderloop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
* /
}
void RenderForward ( CullResults cullResults , Camera camera , RenderLoop renderLoop )
{
// Bind material data
renderLoop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
RenderOpaqueRenderList ( cullResults , camera , renderLoop , "ForwardUnlit" ) ;
RenderTransparentRenderList ( cullResults , camera , renderLoop , "ForwardUnlit" ) ;
RenderOpaqueNoLighting RenderList ( cullResults , camera , renderLoop , "ForwardUnlit" ) ;
RenderTransparentNoLighting RenderList ( cullResults , camera , renderLoop , "ForwardUnlit" ) ;
}
void RenderVelocity ( CullResults cullResults , Camera camera , RenderLoop renderLoop )
renderLoop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
RenderOpaqueRenderList ( cullResults , camera , renderLoop , "MotionVectors" ) ;
RenderOpaqueNoLighting RenderList ( cullResults , camera , renderLoop , "MotionVectors" ) ;
#pragma warning restore 162, 429
}
cmd . Dispose ( ) ;
// Only transparent object can render distortion vectors
RenderTransparentRenderList ( cullResults , camera , renderLoop , "DistortionVectors" ) ;
RenderTransparentNoLighting RenderList ( cullResults , camera , renderLoop , "DistortionVectors" ) ;
}
InitAndClearBuffer ( camera , renderLoop ) ;
RenderDepthPrepass ( cullResults , camera , renderLoop ) ;
// For tile lighting with forward opaque
//RenderForwardOpaqueDepth(cullResults, camera, renderLoop);
if ( debugParameters . debugViewMaterial ! = 0 )
{
RenderDeferredLighting ( camera , renderLoop ) ;
RenderForward ( cullResults , camera , renderLoop ) ;
RenderSky ( camera , renderLoop ) ;
RenderForward ( cullResults , camera , renderLoop ) ; // Note: We want to render forward opaque before RenderSky, then RenderTransparent - can only do that once we have material.SetPass feature...
RenderVelocity ( cullResults , camera , renderLoop ) ;
RenderVelocity ( cullResults , camera , renderLoop ) ; // 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.