浏览代码

Final bits to support FPTL in VR

Only thing left is skybox, but tiled light generated and deferred lighting work!
/vr_fptl_sandbox
robbiesri 8 年前
当前提交
f7a1cea9
共有 9 个文件被更改,包括 101 次插入35 次删除
  1. 73
      Assets/ScriptableRenderPipeline/fptl/FptlLighting.cs
  2. 7
      Assets/ScriptableRenderPipeline/fptl/Internal-DeferredComputeShading.compute
  3. 8
      Assets/ScriptableRenderPipeline/fptl/Internal-DeferredReflections.shader
  4. 9
      Assets/ScriptableRenderPipeline/fptl/Internal-DeferredShading.shader
  5. 8
      Assets/ScriptableRenderPipeline/fptl/LightingTemplate.hlsl
  6. 1
      Assets/ScriptableRenderPipeline/fptl/ReflectionTemplate.hlsl
  7. 4
      Assets/ScriptableRenderPipeline/fptl/TiledLightingTemplate.hlsl
  8. 21
      Assets/ScriptableRenderPipeline/fptl/TiledLightingUtils.hlsl
  9. 5
      Assets/ScriptableRenderPipeline/fptl/lightlistbuild.compute

73
Assets/ScriptableRenderPipeline/fptl/FptlLighting.cs


private static ComputeBuffer s_ConvexBoundsBuffer;
private static ComputeBuffer s_AABBBoundsBuffer;
private static ComputeBuffer s_LightList;
private static ComputeBuffer s_DirLightList;
private static ComputeBuffer s_DirLightList;
//private static int s_UnifiedDirLightListEyeOffset; // VR
//private static int s_UnifiedDirLightListEyeOffset; // VR
private static float[] s_UnifiedDirLightListBaseCount; //VR
private static ComputeBuffer s_BigTileLightList; // used for pre-pass coarse culling on 64x64 tiles

s_ConvexBoundsBuffer.Release();
s_LightDataBuffer.Release();
ReleaseResolutionDependentBuffers();
s_DirLightList.Release();
if (enableClustered)
s_DirLightList.Release();
s_UnifiedLightDataBuffer.Release();
s_UnifiedDirLightList.Release();
if (enableClustered)
{
if (s_GlobalLightListAtomic != null)
s_GlobalLightListAtomic.Release();

if (s_DirLightList != null)
s_DirLightList.Release();
if (enableClustered)
if (s_UnifiedLightDataBuffer != null)
s_UnifiedLightDataBuffer.Release();
if (s_UnifiedDirLightList != null)
s_UnifiedDirLightList.Release();
if (enableClustered)
{
if (s_GlobalLightListAtomic != null)
s_GlobalLightListAtomic.Release();

s_LightDataBuffer = new ComputeBuffer(MaxNumLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(SFiniteLightData)));
s_DirLightList = new ComputeBuffer(MaxNumDirLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLight)));
buildScreenAABBShader.SetBuffer(s_GenAABBKernel, "g_data", s_ConvexBoundsBuffer);
s_UnifiedLightDataBuffer = new ComputeBuffer(MaxNumLights*2, System.Runtime.InteropServices.Marshal.SizeOf(typeof(SFiniteLightData)));
s_UnifiedDirLightList = new ComputeBuffer(MaxNumDirLights*2, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLight)));
buildScreenAABBShader.SetBuffer(s_GenAABBKernel, "g_data", s_ConvexBoundsBuffer);
//m_BuildScreenAABBShader.SetBuffer(kGenAABBKernel, "g_vBoundsBuffer", m_aabbBoundsBuffer);
m_DeferredMaterial.SetBuffer("g_vLightData", s_LightDataBuffer);
m_DeferredMaterial.SetBuffer("g_dirLightData", s_DirLightList);

cmd.SetGlobalFloat("g_isOpaquesOnlyEnabled", 0);
}
cmd.name = "DoTiledDeferredLighting";
// VR - There is a problem here with the deferred material overriding the light data list!
// we push the global param, but the material smooshes it!
if (stereoDoublewide)
{
m_DeferredMaterial.SetBuffer("g_vLightData", s_UnifiedLightDataBuffer);
m_DeferredMaterial.SetBuffer("g_dirLightData", s_UnifiedDirLightList);
m_DeferredReflectionMaterial.SetBuffer("g_vLightData", s_UnifiedLightDataBuffer);
}
else
{
m_DeferredMaterial.SetBuffer("g_vLightData", s_LightDataBuffer);
m_DeferredMaterial.SetBuffer("g_dirLightData", s_DirLightList);
m_DeferredReflectionMaterial.SetBuffer("g_vLightData", s_LightDataBuffer);
}
cmd.name = "DoTiledDeferredLighting";
//cmd.SetRenderTarget(new RenderTargetIdentifier(kGBufferEmission), new RenderTargetIdentifier(kGBufferZ));
//cmd.Blit (kGBufferNormal, (RenderTexture)null); // debug: display normals

}
else
{
cmd.Blit(BuiltinRenderTextureType.CameraTarget, s_CameraTarget, m_DeferredMaterial, 0);
cmd.Blit(BuiltinRenderTextureType.CameraTarget, s_CameraTarget, m_DeferredReflectionMaterial, 0);
}
cmd.Blit(BuiltinRenderTextureType.CameraTarget, s_CameraTarget, m_DeferredMaterial, 0);
cmd.Blit(BuiltinRenderTextureType.CameraTarget, s_CameraTarget, m_DeferredReflectionMaterial, 0);
}
// Set the intermediate target for compositing (skybox, etc)

}
// these are not allocated!
s_UnifiedLightDataBuffer.SetData(unifiedLightData.ToArray());
s_UnifiedDirLightList.SetData(unifiedDirLightData.ToArray());

// VR - no forward in the test scene
// render opaques using tiled forward
RenderForward(cullResults, camera, loop, true); // opaques only (requires a depth pre-pass)
//RenderForward(cullResults, camera, loop, true); // opaques only (requires a depth pre-pass)
m_SkyboxHelper.Draw(loop, camera);
//m_SkyboxHelper.Draw(loop, camera);
if(enableClustered) RenderForward(cullResults, camera, loop, false);
//if(enableClustered) RenderForward(cullResults, camera, loop, false);
// debug views.
if (enableDrawLightBoundsDebug) DrawLightBoundsDebug(loop, cullResults.visibleLights.Length);

// bind depth surface for editor grid/gizmo/selection rendering
if (camera.cameraType == CameraType.SceneView)
{
var cmd = new CommandBuffer();
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, new RenderTargetIdentifier(s_CameraDepthTexture));
loop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}
//// bind depth surface for editor grid/gizmo/selection rendering
//if (camera.cameraType == CameraType.SceneView)
//{
// var cmd = new CommandBuffer();
// cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, new RenderTargetIdentifier(s_CameraDepthTexture));
// loop.ExecuteCommandBuffer(cmd);
// cmd.Dispose();
//}
if (stereoActive)
{

7
Assets/ScriptableRenderPipeline/fptl/Internal-DeferredComputeShading.compute


float linDepth = GetLinearDepth(zbufDpth);
float3 vP = GetViewPosFromLinDepth(pixCoord, linDepth);
float3 vPw = mul(g_mViewToWorld, float4(vP, 1)).xyz;
float3 Vworld = normalize(mul((float3x3) g_mViewToWorld, -vP).xyz); //unity_CameraToWorld
//float3 vPw = mul(g_mViewToWorld, float4(vP, 1)).xyz;
//float3 Vworld = normalize(mul((float3x3) g_mViewToWorld, -vP).xyz); //unity_CameraToWorld
// VR - hacks to get this to compile since it is now an array
float3 vPw = mul(g_mViewToWorldArr[0], float4(vP, 1)).xyz;
float3 Vworld = normalize(mul((float3x3) g_mViewToWorldArr[0], -vP).xyz); //unity_CameraToWorld
float4 gbuffer0 = _CameraGBufferTexture0.Load(uint3(pixCoord.xy, 0));
float4 gbuffer1 = _CameraGBufferTexture1.Load(uint3(pixCoord.xy, 0));

8
Assets/ScriptableRenderPipeline/fptl/Internal-DeferredReflections.shader


#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
#pragma multi_compile __ ENABLE_DEBUG
#pragma enable_d3d11_debug_symbols
#include "UnityLightingCommon.cginc"
float3 EvalIndirectSpecular(UnityLight light, UnityIndirect ind);

float zbufDpth = FetchDepth(_CameraDepthTexture, pixCoord.xy).x;
float linDepth = GetLinearDepth(zbufDpth);
float3 vP = GetViewPosFromLinDepth(i.vertex.xy, linDepth);
float2 eyeScrPos = i.vertex.xy;
eyeScrPos.x -= unity_StereoEyeIndex * g_widthRT;
//float3 vP = GetViewPosFromLinDepth(i.vertex.xy, linDepth);
float3 vP = GetViewPosFromLinDepth(eyeScrPos, linDepth);
//float3 vPw = mul(g_mViewToWorld, float4(vP, 1)).xyz;
//float3 Vworld = normalize(mul((float3x3) g_mViewToWorld, -vP).xyz); //unity_CameraToWorld
float3 vPw = mul(g_mViewToWorldArr[unity_StereoEyeIndex], float4(vP, 1)).xyz;

9
Assets/ScriptableRenderPipeline/fptl/Internal-DeferredShading.shader


#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
#pragma multi_compile __ ENABLE_DEBUG
#pragma enable_d3d11_debug_symbols
#include "UnityLightingCommon.cginc"
float3 EvalMaterial(UnityLight light, UnityIndirect ind);

float zbufDpth = FetchDepth(_CameraDepthTexture, pixCoord.xy).x;
float linDepth = GetLinearDepth(zbufDpth);
float3 vP = GetViewPosFromLinDepth(i.vertex.xy, linDepth);
float2 eyeScrPos = i.vertex.xy;
eyeScrPos.x -= unity_StereoEyeIndex * g_widthRT;
//float3 vP = GetViewPosFromLinDepth(i.vertex.xy, linDepth);
float3 vP = GetViewPosFromLinDepth(eyeScrPos, linDepth);
//float3 vPw = mul(g_mViewToWorld, float4(vP, 1)).xyz;
//float3 Vworld = normalize(mul((float3x3) g_mViewToWorld, -vP).xyz); //unity_CameraToWorld
float3 vPw = mul(g_mViewToWorldArr[unity_StereoEyeIndex], float4(vP, 1)).xyz;

uint numLightsProcessed = 0;
float3 c = data.emission + ExecuteLightList(numLightsProcessed, pixCoord, vP, vPw, Vworld);
//c = (1-unity_StereoEyeIndex)*c + (unity_StereoEyeIndex * data.specularColor);
#if ENABLE_DEBUG
c = OverlayHeatMap(pixCoord & 15, numLightsProcessed, c);

8
Assets/ScriptableRenderPipeline/fptl/LightingTemplate.hlsl


#include "UnityStandardBRDF.cginc"
#include "UnityStandardUtils.cginc"
#include "UnityPBSLighting.cginc"
//#include "..\common\ShaderBase.h"
uniform uint g_nNumDirLights;

float3 ints = 0;
// VR adjustment for two dir light lists
//for (int i = 0; i < g_nNumDirLights; i++)
for (int i = dirBase; i < dirCount; i++)
for (int i = 0; i < g_nNumDirLights; i++)
//for (int i = dirBase; i < dirBase + dirCount; i++)
{
DirectionalLight lightData = g_dirLightData[i];
float atten = 1;

ints += EvalMaterial(light, ind);
}
//return ints;
uint l=0;
// don't need the outer loop since the lights are sorted by volume type

1
Assets/ScriptableRenderPipeline/fptl/ReflectionTemplate.hlsl


#include "UnityStandardBRDF.cginc"
#include "UnityStandardUtils.cginc"
#include "UnityPBSLighting.cginc"
//#include "..\common\ShaderBase.h"
UNITY_DECLARE_ABSTRACT_CUBE_ARRAY(_reflCubeTextures);

4
Assets/ScriptableRenderPipeline/fptl/TiledLightingTemplate.hlsl


#ifndef __TILEDLIGHTINGTEMPLATE_H__
#define __TILEDLIGHTINGTEMPLATE_H__
#include "LightingTemplate.hlsl" // change order to get access to unity_StereoEyeIndex
#include "UnityCG.cginc"
#include "LightingTemplate.hlsl" // change order to get access to unity_StereoEyeIndex
//#include "TiledLightingUtils.hlsl"

21
Assets/ScriptableRenderPipeline/fptl/TiledLightingUtils.hlsl


uint nrTilesX = ((uint) (g_widthRT+(tileSize-1)))/tileSize;
uint nrTilesY = ((uint) (g_heightRT+(tileSize-1)))/tileSize;
uint2 tileIDX = pixCoord / tileSize;
const int tileOffs = (tileIDX.y+model*nrTilesY)*nrTilesX+tileIDX.x;
uint2 modPixCoord = pixCoord;
// g_widthRT should be the 'eye' width, so we need to generate the tile index based
// on the eye texture info, then offset into the appropriate half of the tile list
modPixCoord.x = modPixCoord.x - (unity_StereoEyeIndex * g_widthRT);
//uint2 tileIDX = pixCoord / tileSize;
uint2 tileIDX = modPixCoord / tileSize;
const int tileOffs = (tileIDX.y+model*nrTilesY)*nrTilesX+tileIDX.x;
const int modTileOffs = tileOffs + (unity_StereoEyeIndex * nrTilesY * nrTilesX * NR_LIGHT_MODELS);
uNrLights = g_vLightListGlobal[ 16*tileOffs + 0]&0xffff;
uStart = tileOffs;
// uNrLights = g_vLightListGlobal[ 16*tileOffs + 0]&0xffff;
//uStart = tileOffs;
uNrLights = g_vLightListGlobal[16 * modTileOffs + 0] & 0xffff;
uStart = modTileOffs;
return (g_vLightListGlobal[ 16*tileOffs + (l1>>1)]>>((l1&1)*16))&0xffff;
//return (g_vLightListGlobal[ 16*tileOffs + (l1>>1)]>>((l1&1)*16))&0xffff;
// VR FIX - revert this once we have the light list stuff sorted
return ((g_vLightListGlobal[16 * tileOffs + (l1 >> 1)] >> ((l1 & 1) * 16)) & 0xffff) + (unity_StereoEyeIndex * g_lightDataEyeOffset);
}
#ifdef OPAQUES_ONLY

5
Assets/ScriptableRenderPipeline/fptl/lightlistbuild.compute


//int offs = tileIDX.y*nrTilesX + tileIDX.x;
int offs = tileIDX.y*nrTilesX + tileIDX.x + (g_iEye * nrTilesX * nrTilesY * NR_LIGHT_MODELS);
// VR TODO: It could be interesting to just generate the list list indices to point
// directly into the shared light data. For eye=1, the entirety of g_vLightData
// will be copied into the second part of the shared list.
// so to generate new indices, we'd simply just add that offset...
for(int category=0; category<NR_LIGHT_MODELS; category++)
{
int nrLightsFinal = ldsModelListCount[category];

正在加载...
取消
保存