浏览代码

do a depth pre-pass for forward opaques

do a depth pre-pass for forward opaques.
Enable light map and probe per mesh.
Separate forward into opaques and transparencies.
/main
mmikk 8 年前
当前提交
ede892d1
共有 3 个文件被更改,包括 119 次插入20 次删除
  1. 72
      Assets/ScriptableRenderLoop/fptl/FptlLighting.cs
  2. 41
      Assets/ScriptableRenderLoop/fptl/StandardTest.shader
  3. 26
      Assets/ScriptableRenderLoop/fptl/UnityStandardForwardNew.cginc

72
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);
cmd.SetGlobalFloat("g_isOpaquesOnlyEnabled", useFptl ? 1 : 0);
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(null, new RenderTargetIdentifier(s_GBufferZ));
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)

41
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 vertex vertForward
#pragma fragment fragNoLight
#include "UnityStandardForwardNew.cginc"
ENDCG
}
// ------------------------------------------------------------------
// Shadow rendering pass
Pass {

26
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 OutputForward (float4(res,1.0), gdata.alpha);
}
#endif // UNITY_STANDARD_SHADOW_INCLUDED
#endif
正在加载...
取消
保存