浏览代码

Merge remote-tracking branch 'refs/remotes/origin/master' into AreaLight

/main
sebastienlagarde 8 年前
当前提交
94bea850
共有 10 个文件被更改,包括 197 次插入26 次删除
  1. 73
      Assets/ScriptableRenderLoop/fptl/FptlLighting.cs
  2. 2
      Assets/ScriptableRenderLoop/fptl/Internal-DeferredReflections.shader
  3. 2
      Assets/ScriptableRenderLoop/fptl/Internal-DeferredShading.shader
  4. 42
      Assets/ScriptableRenderLoop/fptl/StandardTest.shader
  5. 2
      Assets/ScriptableRenderLoop/fptl/TiledLightingTemplate.hlsl
  6. 2
      Assets/ScriptableRenderLoop/fptl/TiledReflectionTemplate.hlsl
  7. 38
      Assets/ScriptableRenderLoop/fptl/UnityStandardForwardNew.cginc
  8. 19
      Assets/ScriptableRenderLoop/fptl/RegularForwardLightingTemplate.hlsl
  9. 25
      Assets/ScriptableRenderLoop/fptl/RegularForwardLightingUtils.hlsl
  10. 18
      Assets/ScriptableRenderLoop/fptl/RegularForwardReflectionTemplate.hlsl

73
Assets/ScriptableRenderLoop/fptl/FptlLighting.cs


// clustered light list specific buffers and data begin
public bool enableClustered = false;
const bool k_UseDepthBuffer = true;// // only has an impact when EnableClustered is true (requires a depth-prepass)
const bool disableFptlWhenClustered = false; // still useful on opaques
const int k_Log2NumClusters = 6; // accepted range is from 0 to 6. NumClusters is 1<<g_iLog2NumClusters
const float k_ClustLogBase = 1.02f; // each slice 2% bigger than the previous
float m_ClustScale;

//@TODO: need to get light probes + LPPV too?
settings.inputCullingOptions.SetQueuesOpaque();
settings.rendererConfiguration = RendererConfiguration.PerObjectLightmaps | RendererConfiguration.PerObjectLightProbe;
static void RenderForward(CullResults cull, Camera camera, RenderLoop loop, bool opaquesOnly)
void RenderForward(CullResults cull, Camera camera, RenderLoop loop, bool opaquesOnly)
// using these two lines will require a depth pre-pass for forward opaques which we don't have currently at least
//cmd.SetGlobalFloat("g_isOpaquesOnlyEnabled", opaquesOnly ? 1 : 0);
//cmd.SetGlobalBuffer("g_vLightListGlobal", opaquesOnly ? lightList : m_perVoxelLightLists);
bool useFptl = opaquesOnly && UsingFptl(); // requires depth pre-pass for forward opaques!
cmd.SetGlobalFloat("g_isOpaquesOnlyEnabled", 0);
cmd.SetGlobalBuffer("g_vLightListGlobal", s_PerVoxelLightLists);
bool haveTiledSolution = opaquesOnly || enableClustered;
cmd.EnableShaderKeyword(haveTiledSolution ? "TILED_FORWARD" : "REGULAR_FORWARD" );
cmd.SetGlobalFloat("g_isOpaquesOnlyEnabled", useFptl ? 1 : 0); // leaving this as a dynamic toggle for now for forward opaques to keep shader variants down.
cmd.SetGlobalBuffer("g_vLightListGlobal", useFptl ? s_LightList : s_PerVoxelLightLists);
loop.ExecuteCommandBuffer(cmd);
cmd.Dispose();

sorting = { sortOptions = SortOptions.SortByMaterialThenMesh }
};
//settings.rendererConfiguration = RendererConfiguration.PerObjectLightProbe | RendererConfiguration.PerObjectReflectionProbes;
settings.rendererConfiguration = RendererConfiguration.PerObjectLightmaps | RendererConfiguration.PerObjectLightProbe;
else settings.inputCullingOptions.SetQueuesTransparent();
static void DepthOnlyForForwardOpaques(CullResults cull, Camera camera, RenderLoop loop)
{
var cmd = new CommandBuffer { name = "Forward Opaques - Depth Only" };
cmd.SetRenderTarget(new RenderTargetIdentifier(s_GBufferZ));
loop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
// render opaque objects using Deferred pass
var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("DepthOnly"))
{
sorting = { sortOptions = SortOptions.SortByMaterialThenMesh }
};
settings.inputCullingOptions.SetQueuesOpaque();
loop.DrawRenderers(ref settings);
}
bool UsingFptl()
{
bool isEnabledMSAA = false;
Debug.Assert((!isEnabledMSAA) || enableClustered);
bool disableFptl = (disableFptlWhenClustered && enableClustered) || isEnabledMSAA;
return !disableFptl;
}
static void CopyDepthAfterGBuffer(RenderLoop loop)
{
var cmd = new CommandBuffer { name = "Copy depth" };

void DoTiledDeferredLighting(Camera camera, RenderLoop loop)
{
var bUseClusteredForDeferred = false && enableClustered; // doesn't work on reflections yet but will soon
var bUseClusteredForDeferred = !UsingFptl(); // doesn't work on reflections yet but will soon
var cmd = new CommandBuffer();
m_DeferredMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");

RenderGBuffer(cullResults, camera, loop);
DepthOnlyForForwardOpaques(cullResults, camera, loop);
//@TODO: render forward-only objects into depth buffer
CopyDepthAfterGBuffer(loop);
//@TODO: render reflection probes

var cmd = new CommandBuffer() { name = "Build light list" };
// generate screen-space AABBs (used for both fptl and clustered).
cmd.SetComputeIntParam(buildScreenAABBShader, "g_iNrVisibLights", numLights);
SetMatrixCS(cmd, buildScreenAABBShader, "g_mProjection", projh);
SetMatrixCS(cmd, buildScreenAABBShader, "g_mInvProjection", invProjh);

cmd.SetComputeIntParams(buildPerTileLightListShader, "g_viDimensions", new int[2] { w, h });
cmd.SetComputeIntParam(buildPerTileLightListShader, "g_iNrVisibLights", numLights);
SetMatrixCS(cmd, buildPerTileLightListShader, "g_mScrProjection", projscr);
SetMatrixCS(cmd, buildPerTileLightListShader, "g_mInvScrProjection", invProjscr);
cmd.SetComputeTextureParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_depth_tex", new RenderTargetIdentifier(s_CameraDepthTexture));
cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_vLightList", s_LightList);
cmd.DispatchCompute(buildPerTileLightListShader, s_GenListPerTileKernel, numTilesX, numTilesY, 1);
if( UsingFptl() )
{
cmd.SetComputeIntParams(buildPerTileLightListShader, "g_viDimensions", new int[2] { w, h });
cmd.SetComputeIntParam(buildPerTileLightListShader, "g_iNrVisibLights", numLights);
SetMatrixCS(cmd, buildPerTileLightListShader, "g_mScrProjection", projscr);
SetMatrixCS(cmd, buildPerTileLightListShader, "g_mInvScrProjection", invProjscr);
cmd.SetComputeTextureParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_depth_tex", new RenderTargetIdentifier(s_CameraDepthTexture));
cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_vLightList", s_LightList);
cmd.DispatchCompute(buildPerTileLightListShader, s_GenListPerTileKernel, numTilesX, numTilesY, 1);
}
if (enableClustered)
{

// do deferred lighting
DoTiledDeferredLighting(camera, loop);
// don't have a depth pre-pass for forward lit meshes so have to require clustered for now
if (enableClustered) RenderForward(cullResults, camera, loop, false);
RenderForward(cullResults, camera, loop, true); // opaques only (requires a depth pre-pass)
if(enableClustered) RenderForward(cullResults, camera, loop, false); // transparencies atm. requires clustered until we get traditional forward
FinalPass(loop);
}

int NumLightIndicesPerClusteredTile()
{
return 4 * (1 << k_Log2NumClusters); // total footprint for all layers of the tile (measured in light index entries)
return 8 * (1 << k_Log2NumClusters); // total footprint for all layers of the tile (measured in light index entries)
}
void AllocResolutionDependentBuffers(int width, int height)

2
Assets/ScriptableRenderLoop/fptl/Internal-DeferredReflections.shader


g_localParams.Vworld = Vworld;
uint numReflectionsProcessed = 0;
float3 c = ExecuteReflectionListTiled(numReflectionsProcessed, pixCoord, vP, data.normalWorld, Vworld, data.smoothness);
float3 c = ExecuteReflectionList(numReflectionsProcessed, pixCoord, vP, data.normalWorld, Vworld, data.smoothness);
//c = OverlayHeatMap(numLightsProcessed, c);
return float4(c,1.0);

2
Assets/ScriptableRenderLoop/fptl/Internal-DeferredShading.shader


g_localParams.Vworld = Vworld;
uint numLightsProcessed = 0;
float3 c = data.emission + ExecuteLightListTiled(numLightsProcessed, pixCoord, vP, vPw, Vworld);
float3 c = data.emission + ExecuteLightList(numLightsProcessed, pixCoord, vP, vPw, Vworld);
//c = OverlayHeatMap(numLightsProcessed, c);
return float4(c,1.0);

42
Assets/ScriptableRenderLoop/fptl/StandardTest.shader


ENDCG
}
// ------------------------------------------------------------------
// Depth Only
Pass
{
Name "DEPTHONLY"
Tags { "LightMode" = "DepthOnly" }
Blend [_SrcBlend] [_DstBlend]
//ZWrite [_ZWrite]
ZWrite On ZTest LEqual
CGPROGRAM
#pragma target 5.0
// TODO: figure out what's needed here wrt. alpha test etc.
#pragma shader_feature _NORMALMAP
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma shader_feature _EMISSION
#pragma shader_feature _METALLICGLOSSMAP
#pragma shader_feature ___ _DETAIL_MULX2
#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
#pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
#pragma shader_feature _PARALLAXMAP
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
#pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
#pragma multi_compile_fog
#pragma multi_compile TILED_FORWARD REGULAR_FORWARD
#pragma vertex vertForward
#pragma fragment fragNoLight
#include "UnityStandardForwardNew.cginc"
ENDCG
}
// ------------------------------------------------------------------
// Shadow rendering pass
Pass {

2
Assets/ScriptableRenderLoop/fptl/TiledLightingTemplate.hlsl


float3 ExecuteLightListTiled(out uint numLightsProcessed, uint2 pixCoord, float3 vP, float3 vPw, float3 Vworld)
float3 ExecuteLightList(out uint numLightsProcessed, uint2 pixCoord, float3 vP, float3 vPw, float3 Vworld)
{
uint nrTilesX = (g_widthRT+15)/16; uint nrTilesY = (g_heightRT+15)/16;
uint2 tileIDX = pixCoord / 16;

2
Assets/ScriptableRenderLoop/fptl/TiledReflectionTemplate.hlsl


float3 ExecuteReflectionListTiled(out uint numReflectionProbesProcessed, uint2 pixCoord, float3 vP, float3 vNw, float3 Vworld, float smoothness)
float3 ExecuteReflectionList(out uint numReflectionProbesProcessed, uint2 pixCoord, float3 vP, float3 vNw, float3 Vworld, float smoothness)
{
uint nrTilesX = (g_widthRT+15)/16; uint nrTilesY = (g_heightRT+15)/16;
uint2 tileIDX = pixCoord / 16;

38
Assets/ScriptableRenderLoop/fptl/UnityStandardForwardNew.cginc


return o;
}
#include "LightingUtils.hlsl"
half4 fragNoLight(VertexOutputForwardNew i) : SV_Target
{
float linZ = GetLinearZFromSVPosW(i.pos.w); // matching script side where camera space is right handed.
float3 vP = GetViewPosFromLinDepth(i.pos.xy, linZ);
float3 vPw = mul(g_mViewToWorld, float4(vP,1.0)).xyz;
float3 Vworld = normalize(mul((float3x3) g_mViewToWorld, -vP).xyz); // not same as unity_CameraToWorld
#ifdef _PARALLAXMAP
half3 tangent = i.tangentToWorldAndParallax[0].xyz;
half3 bitangent = i.tangentToWorldAndParallax[1].xyz;
half3 normal = i.tangentToWorldAndParallax[2].xyz;
float3 vDirForParallax = float3( dot(tangent, Vworld), dot(bitangent, Vworld), dot(normal, Vworld));
#else
float3 vDirForParallax = Vworld;
#endif
gdata = FragmentSetup(i.tex, -Vworld, vDirForParallax, i.tangentToWorldAndParallax, vPw); // eyeVec = -Vworld
return OutputForward (float4(0.0,0.0,0.0,1.0), gdata.alpha); // figure out some alpha test stuff
}
float3 EvalMaterial(UnityLight light, UnityIndirect ind)
{
return UNITY_BRDF_PBS(gdata.diffColor, gdata.specColor, gdata.oneMinusReflectivity, gdata.smoothness, gdata.normalWorld, -gdata.eyeVec, light, ind);

}
#ifdef REGULAR_FORWARD
#include "RegularForwardLightingTemplate.hlsl"
#include "RegularForwardReflectionTemplate.hlsl"
#else
#endif
half4 fragForward(VertexOutputForwardNew i) : SV_Target

uint numLightsProcessed = 0, numReflectionsProcessed = 0;
float3 res = 0;
res += ExecuteLightListTiled(numLightsProcessed, pixCoord, vP, vPw, Vworld);
res += ExecuteReflectionListTiled(numReflectionsProcessed, pixCoord, vP, gdata.normalWorld, Vworld, gdata.smoothness);
res += ExecuteLightList(numLightsProcessed, pixCoord, vP, vPw, Vworld);
res += ExecuteReflectionList(numReflectionsProcessed, pixCoord, vP, gdata.normalWorld, Vworld, gdata.smoothness);
// don't really have a handle on this yet
//UnityLight mainLight = MainLight ();

return OutputForward (float4(res,1.0), gdata.alpha);
}
#endif // UNITY_STANDARD_SHADOW_INCLUDED
#endif

19
Assets/ScriptableRenderLoop/fptl/RegularForwardLightingTemplate.hlsl


#ifndef __REGULARFORWARDLIGHTINGTEMPLATE_H__
#define __REGULARFORWARDLIGHTINGTEMPLATE_H__
#include "RegularForwardLightingUtils.hlsl"
#include "LightingTemplate.hlsl"
float3 ExecuteLightList(out uint numLightsProcessed, uint2 pixCoord, float3 vP, float3 vPw, float3 Vworld)
{
uint start = 0, numLights = 0;
GetCountAndStart(start, numLights, DIRECT_LIGHT);
numLightsProcessed = numLights; // mainly for debugging/heat maps
return ExecuteLightList(start, numLights, vP, vPw, Vworld);
}
#endif

25
Assets/ScriptableRenderLoop/fptl/RegularForwardLightingUtils.hlsl


#ifndef __REGULARFORWARDLIGHTINGUTILS_H__
#define __REGULARFORWARDLIGHTINGUTILS_H__
#include "LightingUtils.hlsl"
StructuredBuffer<SFiniteLightData> g_vLightData;
StructuredBuffer<uint> g_vLightListMeshInst; // build on CPU if in use. direct lights first, then reflection probes.
uniform int g_numLights;
uniform int g_numReflectionProbes;
void GetCountAndStart(out uint start, out uint nrLights, uint model)
{
start = model==REFLECTION_LIGHT ? g_numLights : 0; // offset by numLights entries
nrLights = model==REFLECTION_LIGHT ? g_numReflectionProbes : g_numLights;
}
uint FetchIndex(const uint start, const uint l)
{
return g_vLightListMeshInst[start+l];
}
#endif

18
Assets/ScriptableRenderLoop/fptl/RegularForwardReflectionTemplate.hlsl


#ifndef __REGULARFORWARDREFLECTIONTEMPLATE_H__
#define __REGULARFORWARDREFLECTIONTEMPLATE_H__
#include "RegularForwardLightingUtils.hlsl"
#include "ReflectionTemplate.hlsl"
float3 ExecuteReflectionList(out uint numReflectionProbesProcessed, uint2 pixCoord, float3 vP, float3 vNw, float3 Vworld, float smoothness)
{
uint start = 0, numReflectionProbes = 0;
GetCountAndStart(start, numReflectionProbes, REFLECTION_LIGHT);
numReflectionProbesProcessed = numReflectionProbes; // mainly for debugging/heat maps
return ExecuteReflectionList(start, numReflectionProbes, vP, vNw, Vworld, smoothness);
}
#endif
正在加载...
取消
保存