Evgenii Golubev 8 年前
当前提交
e4095dd0
共有 7 个文件被更改,包括 190 次插入48 次删除
  1. 6
      Assets/BasicRenderLoopTutorial/BasicRenderLoop.cs
  2. 40
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs
  3. 76
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  4. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePass.cs
  5. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.cs
  6. 33
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs
  7. 79
      Assets/ScriptableRenderLoop/HDRenderLoop/Utilities.cs

6
Assets/BasicRenderLoopTutorial/BasicRenderLoop.cs


// per-camera built-in shader variables).
loop.SetupCameraProperties (camera);
// clear depth buffer
var cmd = new CommandBuffer();
cmd.ClearRenderTarget(true, false, Color.black);
loop.ExecuteCommandBuffer(cmd);
cmd.Release();
// Setup global lighting shader variables
SetupLightShaderVariables (cull.visibleLights, loop);

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


public readonly GUIContent skyExposure = new GUIContent("Sky Exposure");
public readonly GUIContent skyRotation = new GUIContent("Sky Rotation");
public readonly GUIContent skyMultiplier = new GUIContent("Sky Multiplier");
public readonly GUIContent shadowSettings = new GUIContent("Shadow Settings");
public readonly GUIContent shadowsEnabled = new GUIContent("Enabled");
public readonly GUIContent shadowsAtlasWidth = new GUIContent("Atlas width");
public readonly GUIContent shadowsAtlasHeight = new GUIContent("Atlas height");
public readonly GUIContent shadowsMaxShadowDistance = new GUIContent("Maximum shadow distance");
public readonly GUIContent shadowsDirectionalLightCascadeCount = new GUIContent("Directional cascade count");
public readonly GUIContent[] shadowsCascadeCounts = new GUIContent[] { new GUIContent("1"), new GUIContent("2"), new GUIContent("3"), new GUIContent("4") };
public readonly int[] shadowsCascadeCountValues = new int[] { 1, 2, 3, 4 };
public readonly GUIContent shadowsCascades = new GUIContent("Cascade values");
}
private static Styles s_Styles = null;

}
EditorGUI.indentLevel--;
EditorGUILayout.Space();
var skyParameters = renderLoop.skyParameters;
EditorGUILayout.LabelField(styles.skyParameters);

skyParameters.multiplier = Mathf.Max(EditorGUILayout.FloatField(styles.skyMultiplier, skyParameters.multiplier), 0);
skyParameters.rotation = Mathf.Max(Mathf.Min(EditorGUILayout.FloatField(styles.skyRotation, skyParameters.rotation), 360), -360);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(renderLoop); // Repaint
}
EditorGUI.indentLevel--;
EditorGUILayout.Space();
var shadowParameters = renderLoop.shadowSettings;
EditorGUILayout.LabelField(styles.shadowSettings);
EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck();
shadowParameters.enabled = EditorGUILayout.Toggle(styles.shadowsEnabled, shadowParameters.enabled);
shadowParameters.shadowAtlasWidth = Mathf.Max(0, EditorGUILayout.IntField(styles.shadowsAtlasWidth, shadowParameters.shadowAtlasWidth));
shadowParameters.shadowAtlasHeight = Mathf.Max(0, EditorGUILayout.IntField(styles.shadowsAtlasHeight, shadowParameters.shadowAtlasHeight));
shadowParameters.maxShadowDistance = Mathf.Max(0, EditorGUILayout.FloatField(styles.shadowsMaxShadowDistance, shadowParameters.maxShadowDistance));
shadowParameters.directionalLightCascadeCount = EditorGUILayout.IntPopup(styles.shadowsDirectionalLightCascadeCount, shadowParameters.directionalLightCascadeCount, styles.shadowsCascadeCounts, styles.shadowsCascadeCountValues);
EditorGUI.indentLevel++;
for (int i = 0; i < shadowParameters.directionalLightCascadeCount-1; i++)
{
shadowParameters.directionalLightCascades[i] = Mathf.Max(0, EditorGUILayout.FloatField(shadowParameters.directionalLightCascades[i]));
}
EditorGUI.indentLevel--;
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(renderLoop); // Repaint

76
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


[SerializeField]
ShadowSettings m_ShadowSettings = ShadowSettings.Default;
public ShadowSettings shadowSettings
{
get { return m_ShadowSettings; }
}
ShadowRenderPass m_ShadowPass;

void InitAndClearBuffer(Camera camera, RenderLoop renderLoop)
{
using (new Utilities.ProfilingSample("InitAndClearBuffer", renderLoop))
{
using (new Utilities.ProfilingSample("InitGBuffers and clear Depth/Stencil", renderLoop))
cmd.name = "InitGBuffers and clear Depth/Stencil";
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).

Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, ClearFlag.ClearDepth);
}
using (new Utilities.ProfilingSample("Clear HDR target", renderLoop))
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, ClearFlag.ClearColor, Color.black, "Clear HDR target");
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, ClearFlag.ClearColor, Color.black);
using (new Utilities.ProfilingSample("Clear GBuffer", renderLoop))
Utilities.SetRenderTarget(renderLoop, m_gbufferManager.GetGBuffers(), m_CameraDepthBufferRT, ClearFlag.ClearColor, Color.black, "Clear GBuffer");
Utilities.SetRenderTarget(renderLoop, m_gbufferManager.GetGBuffers(), m_CameraDepthBufferRT, ClearFlag.ClearColor, Color.black);
}
}
void RenderOpaqueRenderList(CullResults cull, Camera camera, RenderLoop renderLoop, string passName, RendererConfiguration rendererConfiguration = 0)

if (!debugParameters.useDepthPrepass)
return;
using (new Utilities.ProfilingSample("Depth Prepass", renderLoop))
{
Utilities.SetRenderTarget(renderLoop, m_CameraDepthBufferRT, "Depth Prepass");
Utilities.SetRenderTarget(renderLoop, m_CameraDepthBufferRT);
}
}
void RenderGBuffer(CullResults cull, Camera camera, RenderLoop renderLoop)

return ;
}
using (new Utilities.ProfilingSample("GBuffer Pass", renderLoop))
{
Utilities.SetRenderTarget(renderLoop, m_gbufferManager.GetGBuffers(), m_CameraDepthBufferRT, "GBuffer Pass");
Utilities.SetRenderTarget(renderLoop, m_gbufferManager.GetGBuffers(), m_CameraDepthBufferRT);
}
}
// This pass is use in case of forward opaque and deferred rendering. We need to render forward objects before tile lighting pass

return;
// TODO: Use the render queue index to only send the forward opaque!
Utilities.SetRenderTarget(renderLoop, m_CameraDepthBufferRT, "Clear HDR target");
Utilities.SetRenderTarget(renderLoop, m_CameraDepthBufferRT, "Depth Prepass");
using (new Utilities.ProfilingSample("DebugView Material Mode Pass", renderLoop))
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, Utilities.kClearAll, Color.black, "DebugView Material Mode Pass");
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, Utilities.kClearAll, Color.black);
Shader.SetGlobalInt("_DebugViewMaterial", (int)debugParameters.debugViewMaterial);

return ;
}
using (new Utilities.ProfilingSample("Single Pass - Deferred Lighting Pass", renderLoop))
{
}
}
void RenderSky(Camera camera, RenderLoop renderLoop)

void RenderForward(CullResults cullResults, Camera camera, RenderLoop renderLoop)
{
using (new Utilities.ProfilingSample("Forward Pass", renderLoop))
{
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, "Forward Pass");
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT);
if (debugParameters.useForwardRenderingOnly)
{

RenderTransparentRenderList(cullResults, camera, renderLoop, "Forward", Utilities.kRendererConfigurationBakedLighting);
}
}
using (new Utilities.ProfilingSample("Forward Unlit Pass", renderLoop))
{
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT, "Forward Unlit Pass");
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT);
}
using (new Utilities.ProfilingSample("Velocity Pass", renderLoop))
{
// warning CS0162: Unreachable code detected // warning CS0429: Unreachable expression code detected
#pragma warning disable 162, 429
// If opaque velocity have been render during GBuffer no need to render it here

int w = camera.pixelWidth;
int h = camera.pixelHeight;
var cmd = new CommandBuffer { name = "Velocity Pass" };
var cmd = new CommandBuffer { name = "" };
cmd.GetTemporaryRT(m_VelocityBuffer, w, h, 0, FilterMode.Point, Builtin.RenderLoop.GetVelocityBufferFormat(), Builtin.RenderLoop.GetVelocityBufferReadWrite());
cmd.SetRenderTarget(m_VelocityBufferRT, m_CameraDepthBufferRT);
renderLoop.ExecuteCommandBuffer(cmd);

#pragma warning restore 162, 429
}
}
using (new Utilities.ProfilingSample("Distortion Pass", renderLoop))
{
var cmd = new CommandBuffer { name = "Distortion Pass" };
var cmd = new CommandBuffer { name = "" };
cmd.GetTemporaryRT(m_DistortionBuffer, w, h, 0, FilterMode.Point, Builtin.RenderLoop.GetDistortionBufferFormat(), Builtin.RenderLoop.GetDistortionBufferReadWrite());
cmd.SetRenderTarget(m_DistortionBufferRT, m_CameraDepthBufferRT);
renderLoop.ExecuteCommandBuffer(cmd);

RenderTransparentRenderList(cullResults, camera, renderLoop, "DistortionVectors");
}
}
using (new Utilities.ProfilingSample("Final Pass", renderLoop))
{
// Those could be tweakable for the neutral tonemapper, but in the case of the LookDev we don't need that
const float blackIn = 0.02f;
const float whiteIn = 10.0f;

m_FinalPassMaterial.SetFloat("_EnableToneMap", debugParameters.enableTonemap ? 1.0f : 0.0f);
m_FinalPassMaterial.SetFloat("_Exposure", debugParameters.exposure);
var cmd = new CommandBuffer { name = "FinalPass" };
var cmd = new CommandBuffer { name = "" };
}
}
// Function to prepare light structure for GPU lighting

else
{
ShadowOutput shadows;
using (new Utilities.ProfilingSample("Shadow Pass", renderLoop))
{
}
PrepareLightsForGPU(cullResults, camera, ref shadows, ref m_lightList);
//m_TilePassLightLoop.BuildGPULightLists(camera, renderLoop, m_lightList, m_CameraDepthBuffer);
using (new Utilities.ProfilingSample("Build Light list", renderLoop))
{
PrepareLightsForGPU(cullResults, camera, ref shadows, ref m_lightList);
//m_TilePassLightLoop.BuildGPULightLists(camera, renderLoop, m_lightList, m_CameraDepthBuffer);
PushGlobalParams(camera, renderLoop, m_lightList);
PushGlobalParams(camera, renderLoop, m_lightList);
}
RenderDeferredLighting(camera, renderLoop);
RenderSky(camera, renderLoop);

2
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePass.cs


// m_gbufferManager.BindBuffers(m_DeferredMaterial);
// TODO: Bind depth textures
var cmd = new CommandBuffer { name = "Single Pass - Deferred Ligthing Pass" };
var cmd = new CommandBuffer { name = "" };
cmd.Blit(null, colorBuffer, m_DeferredMaterial, 0);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();

2
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.cs


var projscr = temp * proj;
var invProjscr = projscr.inverse;
var cmd = new CommandBuffer() { name = "Build light list" };
var cmd = new CommandBuffer() { name = "" };
// generate screen-space AABBs (used for both fptl and clustered).
{

33
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs


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" };
var cmd = new CommandBuffer { name = "" };
cmd.DrawMesh(skyMesh, Matrix4x4.identity, m_SkyHDRIMaterial);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();

{
using (new Utilities.ProfilingSample("Sky Pass", renderLoop))
//using (new EditorGUI.DisabledScope(m_LookDevEnvLibrary.hdriList.Count <= 1))
// Render sky into a cubemap - doesn't happen every frame, can be control
for (int i = 0; i < 6; ++i)
using (new Utilities.ProfilingSample("Sky Pass: Render Cubemap", renderLoop))
Utilities.SetRenderTarget(renderLoop, m_SkyboxCubemapRT, "", 0, (CubemapFace)i);
Camera faceCamera = m_CubemapFaceCamera[i].GetComponent<Camera>();
RenderSky(faceCamera, skyParameters, true, renderLoop);
// Render sky into a cubemap - doesn't happen every frame, can be control
for (int i = 0; i < 6; ++i)
{
Utilities.SetRenderTarget(renderLoop, m_SkyboxCubemapRT, 0, (CubemapFace)i);
Camera faceCamera = m_CubemapFaceCamera[i].GetComponent<Camera>();
RenderSky(faceCamera, skyParameters, true, renderLoop);
}
m_StandardSkyboxMaterial.SetTexture("_Tex", m_SkyboxCubemapRT);
RenderSettings.skybox = m_StandardSkyboxMaterial; // 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;
//DynamicGI.UpdateEnvironment();
m_StandardSkyboxMaterial.SetTexture("_Tex", m_SkyboxCubemapRT);
RenderSettings.skybox = m_StandardSkyboxMaterial; // 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;
//DynamicGI.UpdateEnvironment();
// TODO: do a render to texture here

//m_SkyboxMaterial.SetTexture(cubemap);
// Render the sky itself
Utilities.SetRenderTarget(renderLoop, colorBuffer, depthBuffer, "Sky Pass");
Utilities.SetRenderTarget(renderLoop, colorBuffer, depthBuffer);
RenderSky(camera, skyParameters, false, renderLoop);
}

79
Assets/ScriptableRenderLoop/HDRenderLoop/Utilities.cs


// Render Target Management.
public const ClearFlag kClearAll = ClearFlag.ClearDepth | ClearFlag.ClearColor;
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier buffer, string name = "", int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier buffer, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
cmd.name = name;
cmd.name = "";
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, string name = "", int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
SetRenderTarget(renderLoop, colorBuffer, depthBuffer, ClearFlag.ClearNone, new Color(0.0f, 0.0f, 0.0f, 0.0f), name, miplevel, cubemapFace);
SetRenderTarget(renderLoop, colorBuffer, depthBuffer, ClearFlag.ClearNone, new Color(0.0f, 0.0f, 0.0f, 0.0f), miplevel, cubemapFace);
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, string name = "", int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
SetRenderTarget(renderLoop, colorBuffer, depthBuffer, clearFlag, new Color(0.0f, 0.0f, 0.0f, 0.0f), name, miplevel, cubemapFace);
SetRenderTarget(renderLoop, colorBuffer, depthBuffer, clearFlag, new Color(0.0f, 0.0f, 0.0f, 0.0f), miplevel, cubemapFace);
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor, string name = "", int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)
cmd.name = name;
cmd.name = "";
cmd.SetRenderTarget(colorBuffer, depthBuffer, miplevel, cubemapFace);
if (clearFlag != ClearFlag.ClearNone)
cmd.ClearRenderTarget((clearFlag & ClearFlag.ClearDepth) != 0, (clearFlag & ClearFlag.ClearColor) != 0, clearColor);

public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, string name = "")
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer)
SetRenderTarget(renderLoop, colorBuffers, depthBuffer, ClearFlag.ClearNone, Color.black, name);
SetRenderTarget(renderLoop, colorBuffers, depthBuffer, ClearFlag.ClearNone, Color.black);
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag = ClearFlag.ClearNone, string name = "")
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag = ClearFlag.ClearNone)
SetRenderTarget(renderLoop, colorBuffers, depthBuffer, clearFlag, Color.black, name);
SetRenderTarget(renderLoop, colorBuffers, depthBuffer, clearFlag, Color.black);
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor, string name = "")
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor)
cmd.name = name;
cmd.name = "";
cmd.SetRenderTarget(colorBuffers, depthBuffer);
if (clearFlag != ClearFlag.ClearNone)
cmd.ClearRenderTarget((clearFlag & ClearFlag.ClearDepth) != 0, (clearFlag & ClearFlag.ClearColor) != 0, clearColor);

#endif
}
}
public class ProfilingSample
: IDisposable
{
bool disposed = false;
RenderLoop renderLoop;
string name;
public ProfilingSample(string _name, RenderLoop _renderloop)
{
renderLoop = _renderloop;
name = _name;
CommandBuffer cmd = new CommandBuffer();
cmd.name = "";
cmd.BeginSample(name);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}
~ProfilingSample()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
CommandBuffer cmd = new CommandBuffer();
cmd.name = "";
cmd.EndSample(name);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}
disposed = true;
}
}
public static Matrix4x4 GetViewProjectionMatrix(Camera camera)
{
// The actual projection matrix used in shaders is actually massaged a bit to work across all platforms

正在加载...
取消
保存