浏览代码

Merge pull request #195 from Unity-Technologies/Branch_ComputeBufferSmallRefacto

Small refactor of ComputeBuffer handling for lighting
/fptl_cleanup
GitHub 8 年前
当前提交
ea39b5ac
共有 4 个文件被更改,包括 53 次插入58 次删除
  1. 3
      Assets/ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  2. 2
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/LightLoop.cs
  3. 96
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.cs
  4. 10
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.hlsl

3
Assets/ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


renderContext.ExecuteCommandBuffer(cmd);
cmd.Dispose();
if (m_LightLoop != null)
m_LightLoop.PushGlobalParams(hdCamera.camera, renderContext);
}
bool NeedDepthBufferCopy()

2
Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/LightLoop.cs


// TODO: this should not be part of the interface but for now make something working
public virtual void BuildGPULightLists(Camera camera, ScriptableRenderContext loop, RenderTargetIdentifier cameraDepthBufferRT) { }
public virtual void PushGlobalParams(Camera camera, ScriptableRenderContext loop) {}
public virtual void RenderDeferredLighting(HDCamera hdCamera, ScriptableRenderContext renderContext,
LightingDebugSettings lightDebugParameters,
RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthStencilBuffer, RenderTargetIdentifier depthStencilTexture,

96
Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.cs


s_LightVolumeDataBuffer = new ComputeBuffer(k_MaxLightsOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightVolumeData)));
s_DispatchIndirectBuffer = new ComputeBuffer(LightDefinitions.NUM_FEATURE_VARIANTS * 3, sizeof(uint), ComputeBufferType.IndirectArguments);
buildScreenAABBShader.SetBuffer(s_GenAABBKernel, "g_data", s_ConvexBoundsBuffer);
buildPerTileLightListShader.SetBuffer(s_GenListPerTileKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
buildPerTileLightListShader.SetBuffer(s_GenListPerTileKernel, "_LightVolumeData", s_LightVolumeDataBuffer);
buildPerTileLightListShader.SetBuffer(s_GenListPerTileKernel, "g_data", s_ConvexBoundsBuffer);
buildPerVoxelLightListShader.SetBuffer(s_GenListPerVoxelKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
buildPerVoxelLightListShader.SetBuffer(s_GenListPerVoxelKernel, "_LightVolumeData", s_LightVolumeDataBuffer);
buildPerVoxelLightListShader.SetBuffer(s_GenListPerVoxelKernel, "g_data", s_ConvexBoundsBuffer);
s_GlobalLightListAtomic = new ComputeBuffer(1, sizeof(uint));
}

buildPerBigTileLightListShader.SetBuffer(s_GenListPerBigTileKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
buildPerBigTileLightListShader.SetBuffer(s_GenListPerBigTileKernel, "_LightVolumeData", s_LightVolumeDataBuffer);
buildPerBigTileLightListShader.SetBuffer(s_GenListPerBigTileKernel, "g_data", s_ConvexBoundsBuffer);
}
s_ClearDispatchIndirectKernel = clearDispatchIndirectShader.FindKernel("ClearDispatchIndirect");

// Restore values after "special rendering"
m_PassSettings.specularGlobalDimmer = oldSpecularGlobalDimmer;
UpdateDataBuffers();
}
void VoxelLightListGeneration(CommandBuffer cmd, Camera camera, Matrix4x4 projscr, Matrix4x4 invProjscr, RenderTargetIdentifier cameraDepthBufferRT)

{
cmd.SetComputeBufferParam(buildPerVoxelLightListShader, s_GenListPerVoxelKernel, "g_logBaseBuffer", s_PerTileLogBaseTweak);
}
cmd.SetComputeBufferParam(buildPerVoxelLightListShader, s_GenListPerVoxelKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
cmd.SetComputeBufferParam(buildPerVoxelLightListShader, s_GenListPerVoxelKernel, "_LightVolumeData", s_LightVolumeDataBuffer);
cmd.SetComputeBufferParam(buildPerVoxelLightListShader, s_GenListPerVoxelKernel, "g_data", s_ConvexBoundsBuffer);
var numTilesX = GetNumTileClusteredX(camera);
var numTilesY = GetNumTileClusteredY(camera);

var invProjh = projh.inverse;
cmd.SetComputeIntParam(buildScreenAABBShader, "g_iNrVisibLights", m_lightCount);
cmd.SetComputeBufferParam(buildScreenAABBShader, s_GenAABBKernel, "g_data", s_ConvexBoundsBuffer);
Utilities.SetMatrixCS(cmd, buildScreenAABBShader, "g_mProjection", projh);
Utilities.SetMatrixCS(cmd, buildScreenAABBShader, "g_mInvProjection", invProjh);
cmd.SetComputeBufferParam(buildScreenAABBShader, s_GenAABBKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);

cmd.SetComputeFloatParam(buildPerBigTileLightListShader, "g_fNearPlane", camera.nearClipPlane);
cmd.SetComputeFloatParam(buildPerBigTileLightListShader, "g_fFarPlane", camera.farClipPlane);
cmd.SetComputeBufferParam(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, "g_vLightList", s_BigTileLightList);
cmd.SetComputeBufferParam(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
cmd.SetComputeBufferParam(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, "_LightVolumeData", s_LightVolumeDataBuffer);
cmd.SetComputeBufferParam(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, "g_data", s_ConvexBoundsBuffer);
cmd.DispatchCompute(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, numBigTilesX, numBigTilesY, 1);
}

cmd.SetComputeIntParams(buildPerTileLightListShader, "g_viDimensions", new int[2] { w, h });
cmd.SetComputeIntParam(buildPerTileLightListShader, "_EnvLightIndexShift", m_lightList.lights.Count);
cmd.SetComputeIntParam(buildPerTileLightListShader, "g_iNrVisibLights", m_lightCount);
cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "_LightVolumeData", s_LightVolumeDataBuffer);
cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_data", s_ConvexBoundsBuffer);
Utilities.SetMatrixCS(cmd, buildPerTileLightListShader, "g_mScrProjection", projscr);
Utilities.SetMatrixCS(cmd, buildPerTileLightListShader, "g_mInvScrProjection", invProjscr);
cmd.SetComputeTextureParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_depth_tex", cameraDepthBufferRT);

if (activeComputeShader)
activeCommandBuffer.SetComputeTextureParam(activeComputeShader, activeComputeKernel, name, value);
else
Shader.SetGlobalTexture(name, value);
activeCommandBuffer.SetGlobalTexture(name, value);
}

activeCommandBuffer.SetComputeBufferParam(activeComputeShader, activeComputeKernel, name, buffer);
else
Shader.SetGlobalBuffer(name, buffer);
activeCommandBuffer.SetGlobalBuffer(name, buffer);
}
private void SetGlobalInt(string name, int value)

if (activeComputeShader)
activeCommandBuffer.SetComputeFloatParam(activeComputeShader, name, value);
else
Shader.SetGlobalFloat(name, value);
activeCommandBuffer.SetGlobalFloat(name, value);
}
private void SetGlobalVector(string name, Vector4 value)

else
Shader.SetGlobalVector(name, value);
activeCommandBuffer.SetGlobalVector(name, value);
}
private void SetGlobalVectorArray(string name, Vector4[] values)

}
else
{
Shader.SetGlobalVectorArray(name, values);
activeCommandBuffer.SetGlobalVectorArray(name, values);
private void UpdateDataBuffers()
{
s_DirectionalLightDatas.SetData(m_lightList.directionalLights.ToArray());
s_LightDatas.SetData(m_lightList.lights.ToArray());
s_EnvLightDatas.SetData(m_lightList.envLights.ToArray());
s_shadowDatas.SetData(m_lightList.shadows.ToArray());
// These two buffers have been set in Rebuild()
s_ConvexBoundsBuffer.SetData(m_lightList.bounds.ToArray());
s_LightVolumeDataBuffer.SetData(m_lightList.lightVolumes.ToArray());
}
SetGlobalPropertyRedirect(computeShader, kernelIndex, cmd);
SetGlobalBuffer("g_vLightListGlobal", !usingFptl ? s_PerVoxelLightLists : s_LightList); // opaques list (unless MSAA possibly)
SetGlobalTexture("_CookieTextures", m_CookieTexArray.GetTexCache());
SetGlobalTexture("_CookieCubeTextures", m_CubeCookieTexArray.GetTexCache());

}
}
public override void PushGlobalParams(Camera camera, ScriptableRenderContext loop)
private void PushGlobalParams(Camera camera, ScriptableRenderContext loop, ComputeShader computeShader, int kernelIndex)
s_DirectionalLightDatas.SetData(m_lightList.directionalLights.ToArray());
s_LightDatas.SetData(m_lightList.lights.ToArray());
s_EnvLightDatas.SetData(m_lightList.envLights.ToArray());
s_shadowDatas.SetData(m_lightList.shadows.ToArray());
// These two buffers have been set in Rebuild()
s_ConvexBoundsBuffer.SetData(m_lightList.bounds.ToArray());
s_LightVolumeDataBuffer.SetData(m_lightList.lightVolumes.ToArray());
BindGlobalParams(cmd, null, 0, camera, loop);
SetGlobalPropertyRedirect(computeShader, kernelIndex, cmd);
BindGlobalParams(cmd, computeShader, kernelIndex, camera, loop);
SetGlobalPropertyRedirect(null, 0, null);
loop.ExecuteCommandBuffer(cmd);

using (new Utilities.ProfilingSample(m_PassSettings.enableTileAndCluster ? "TilePass - Deferred Lighting Pass" : "SinglePass - Deferred Lighting Pass", renderContext))
{
var cmd = new CommandBuffer();
SetGlobalBuffer("g_vLightListGlobal", bUseClusteredForDeferred ? s_PerVoxelLightLists : s_LightList); // opaques list (unless MSAA possibly)
if (lightDebugSettings.lightingDebugMode == LightingDebugMode.None)
SetGlobalPropertyRedirect(shadeOpaqueShader, usingFptl ? s_shadeOpaqueFptlKernel : s_shadeOpaqueClusteredKernel, cmd);
else
SetGlobalPropertyRedirect(shadeOpaqueShader, usingFptl ? s_shadeOpaqueFptlDebugLightingKernel : s_shadeOpaqueClusteredDebugLightingKernel, cmd);
// In case of bUseClusteredForDeferred disable toggle option since we're using m_perVoxelLightLists as opposed to lightList
if (bUseClusteredForDeferred)
{
SetGlobalFloat("_UseTileLightList", 0);
}
var camera = hdCamera.camera;
// Must be done after setting up the compute shader above.
SetupRenderingForDebug(lightDebugSettings);

PushGlobalParams(camera, renderContext, null, 0);
// This is a debug brute force renderer to debug tile/cluster which render all the lights
if (outputSplitLightingForSSS)
{

{
if (m_PassSettings.enableComputeLightEvaluation)
{
// Compute shader evaluation
int kernel = bUseClusteredForDeferred ? s_shadeOpaqueClusteredKernel : s_shadeOpaqueFptlKernel;
int kernel = 0;
if (lightDebugSettings.lightingDebugMode == LightingDebugMode.None)
kernel = usingFptl ? s_shadeOpaqueFptlKernel : s_shadeOpaqueClusteredKernel;
else
kernel = usingFptl ? s_shadeOpaqueFptlDebugLightingKernel : s_shadeOpaqueClusteredDebugLightingKernel;
var camera = hdCamera.camera;
BindGlobalParams(cmd, shadeOpaqueShader, kernel, camera, renderContext);
PushGlobalParams(camera, renderContext, shadeOpaqueShader, kernel);
cmd.SetComputeBufferParam(shadeOpaqueShader, kernel, "g_vLightListGlobal", bUseClusteredForDeferred ? s_PerVoxelLightLists : s_LightList);

else
{
// Pixel shader evaluation
PushGlobalParams(camera, renderContext, null, 0);
if (m_PassSettings.enableSplitLightEvaluation)
{
if (outputSplitLightingForSSS)

10
Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.hlsl


#if defined (LIGHTLOOP_TILE_DIRECT) || defined(LIGHTLOOP_TILE_ALL)
#if defined (LIGHTLOOP_TILE_DIRECT) || defined(LIGHTLOOP_TILE_ALL)
#define PROCESS_DIRECTIONAL_LIGHT
#define PROCESS_PUNCTUAL_LIGHT
#define PROCESS_AREA_LIGHT

StructuredBuffer<float> g_logBaseBuffer; // don't support Buffer yet in unity
//#endif
StructuredBuffer<DirectionalLightData> _DirectionalLightDatas;
StructuredBuffer<LightData> _LightDatas;
StructuredBuffer<EnvLightData> _EnvLightDatas;
StructuredBuffer<ShadowData> _ShadowDatas;
StructuredBuffer<DirectionalLightData> _DirectionalLightDatas;
StructuredBuffer<LightData> _LightDatas;
StructuredBuffer<EnvLightData> _EnvLightDatas;
StructuredBuffer<ShadowData> _ShadowDatas;
// Use texture atlas for shadow map
//TEXTURE2D(_ShadowAtlas);

正在加载...
取消
保存