2025-06-18 19:13:30 +08:00
|
|
|
|
using System;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
using Unity.Collections;
|
|
|
|
|
|
using Unity.Mathematics;
|
2025-06-18 19:13:30 +08:00
|
|
|
|
using UnityEngine;
|
2025-06-20 18:14:38 +08:00
|
|
|
|
using UnityEngine.Experimental.Rendering;
|
2025-06-18 19:13:30 +08:00
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
|
|
|
|
|
|
|
|
namespace X.Rendering.Feature
|
|
|
|
|
|
{
|
2025-06-23 19:18:41 +08:00
|
|
|
|
internal class CullResult : IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
public RenderTexture ResultTex;
|
|
|
|
|
|
public NativeArray<half> ResultArray;
|
|
|
|
|
|
public bool ReadDone = false;
|
|
|
|
|
|
public bool DataReady = false;
|
|
|
|
|
|
public Action<AsyncGPUReadbackRequest> ReadBackAction;
|
|
|
|
|
|
|
|
|
|
|
|
private void ReadBack(AsyncGPUReadbackRequest readback)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (readback.done && !readback.hasError)
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = readback.GetData<half>();
|
|
|
|
|
|
if (ResultArray.IsCreated)
|
|
|
|
|
|
{
|
|
|
|
|
|
ResultArray.CopyFrom(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
ReadDone = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ReadDone = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DataReady = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public CullResult CreateResources(int texSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
ResultArray.Dispose();
|
|
|
|
|
|
ResultArray = new(texSize * texSize, Allocator.Persistent);
|
|
|
|
|
|
if (ResultTex != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ResultTex.Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ResultTex = new RenderTexture(texSize, texSize, 0, RenderTextureFormat.RHalf);
|
|
|
|
|
|
ResultTex.filterMode = FilterMode.Point;
|
|
|
|
|
|
ResultTex.enableRandomWrite = true;
|
|
|
|
|
|
ResultTex.Create();
|
|
|
|
|
|
ReadBackAction = ReadBack;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ResultTex != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ResultTex.Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
ResultTex = null;
|
|
|
|
|
|
ResultArray.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class HierarchicalZOcclusionCullFeature : ScriptableRendererFeature
|
2025-06-18 19:13:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
public enum EDepthPyramidFunc
|
|
|
|
|
|
{
|
|
|
|
|
|
CopyDepth,
|
|
|
|
|
|
Compute,
|
|
|
|
|
|
SPD
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
|
public class Settings
|
|
|
|
|
|
{
|
|
|
|
|
|
public EDepthPyramidFunc PyramidFunc = EDepthPyramidFunc.CopyDepth;
|
|
|
|
|
|
public bool SkipThreeMip;
|
2025-06-20 18:14:38 +08:00
|
|
|
|
// 留历史 depth,做 tow pass cull
|
2025-06-23 19:18:41 +08:00
|
|
|
|
public bool UseThreeFrameReadback = true;
|
2025-06-18 19:13:30 +08:00
|
|
|
|
public ComputeShader ComputeShader;
|
|
|
|
|
|
public ComputeShader Spd;
|
|
|
|
|
|
public Material CopyDepth;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
//TODO: 通过使用上一帧的摄像机位置(包括矩阵)和上一帧的深度图做剔除,储存已经被剔除的物体和未被剔除的物体,然后绘制未被剔除的物体到GBuffer(包含深度图),
|
|
|
|
|
|
//再二次生成HiZ DepthTexture,并对已经被剔除的物体使用一遍新的深度和当前摄像机的位置做一次剔除判断
|
|
|
|
|
|
public bool UseTowCullPass;
|
|
|
|
|
|
public bool UseCompute;
|
|
|
|
|
|
public Material CullMat;
|
|
|
|
|
|
public ComputeShader CullShader;
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
private Settings settings;
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
private HizOcclusionCullPass hizPass;
|
|
|
|
|
|
private static HierarchicalZOcclusionCullFeature instance;
|
2025-06-18 19:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
|
|
|
|
|
{
|
2025-06-23 19:18:41 +08:00
|
|
|
|
if ((renderingData.cameraData.cameraType == CameraType.Game))
|
2025-06-18 19:13:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
renderer.EnqueuePass(hizPass);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Create()
|
|
|
|
|
|
{
|
|
|
|
|
|
hizPass = new(settings);
|
2025-06-23 19:18:41 +08:00
|
|
|
|
instance = this;
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
if(hizPass != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
hizPass.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
internal static CullResult GetCullResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
return instance.hizPass.GetCullResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class HizOcclusionCullPass : ScriptableRenderPass, IDisposable
|
2025-06-18 19:13:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly Settings settings;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
private ProfilingSampler profiler;
|
|
|
|
|
|
|
|
|
|
|
|
private Vector2Int cachedHardwareTextureSize;
|
|
|
|
|
|
private bool cachedSkipThreeMip;
|
|
|
|
|
|
private Vector2Int mip0SizeNOP;
|
|
|
|
|
|
private Vector2Int depthPyramidTextureSize;
|
|
|
|
|
|
private int mipLevelCount;
|
|
|
|
|
|
private Vector2Int[] mipLevelOffsets;
|
|
|
|
|
|
private Vector2Int[] mipLevelSizes;
|
2025-06-20 18:14:38 +08:00
|
|
|
|
private Vector4[] mipOffsetAndSizes = new Vector4[16];
|
2025-06-18 19:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
private RTHandle[] depthPyramidTexs = new RTHandle[3];
|
|
|
|
|
|
private static string[] depthPyramidNames = new string[3] { "_DepthPyramidTextureA", "_DepthPyramidTextureB", "_DepthPyramidTextureC" };
|
|
|
|
|
|
|
2025-06-20 18:14:38 +08:00
|
|
|
|
RenderTexture SpdAtomicCounter;
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
CullResult[] cullResults;
|
|
|
|
|
|
HizOcclusionCullPass instance;
|
|
|
|
|
|
|
|
|
|
|
|
public HizOcclusionCullPass(Settings settings)
|
2025-06-18 19:13:30 +08:00
|
|
|
|
{
|
2025-06-23 19:18:41 +08:00
|
|
|
|
profiler = new ProfilingSampler(nameof(HizOcclusionCullPass));
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
|
|
|
|
|
|
this.settings = settings;
|
2025-06-20 18:14:38 +08:00
|
|
|
|
mipLevelOffsets = new Vector2Int[16];
|
|
|
|
|
|
mipLevelSizes = new Vector2Int[16];
|
|
|
|
|
|
SpdAtomicCounter = new RenderTexture(1, 1, 0, GraphicsFormat.R32_UInt) { name = "FSR2_SpdAtomicCounter", enableRandomWrite = true };
|
|
|
|
|
|
SpdAtomicCounter.Create();
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
int texSize = 64;
|
|
|
|
|
|
|
|
|
|
|
|
cullResults = new CullResult[3]
|
|
|
|
|
|
{
|
|
|
|
|
|
new CullResult().CreateResources(texSize),
|
|
|
|
|
|
new CullResult().CreateResources(texSize),
|
|
|
|
|
|
new CullResult().CreateResources(texSize),
|
|
|
|
|
|
};
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int HizIndex { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
private int GetHizIndex()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(settings.UseThreeFrameReadback)
|
|
|
|
|
|
{
|
|
|
|
|
|
HizIndex = Time.frameCount % 3;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
HizIndex = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return HizIndex;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
public CullResult GetCullResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
var hizIndex = GetHizIndex();
|
|
|
|
|
|
return cullResults[GetLastCullIndex(hizIndex)];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
public void ComputePackedMipChainInfo(Vector2Int viewportSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (cachedHardwareTextureSize == viewportSize && cachedSkipThreeMip == settings.SkipThreeMip)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cachedHardwareTextureSize = viewportSize;
|
|
|
|
|
|
cachedSkipThreeMip = settings.SkipThreeMip;
|
|
|
|
|
|
|
|
|
|
|
|
mip0SizeNOP = viewportSize;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
int resizeX = viewportSize.x;
|
|
|
|
|
|
int resizeY = viewportSize.y;
|
|
|
|
|
|
|
|
|
|
|
|
resizeX = Mathf.IsPowerOfTwo(viewportSize.x) ? viewportSize.x : Mathf.NextPowerOfTwo(viewportSize.x);
|
|
|
|
|
|
resizeY = Mathf.IsPowerOfTwo(viewportSize.y) ? viewportSize.y : Mathf.NextPowerOfTwo(viewportSize.y);
|
2025-06-25 11:11:33 +08:00
|
|
|
|
//if (resizeX > viewportSize.x)
|
|
|
|
|
|
// resizeX /= 2;
|
|
|
|
|
|
//if (resizeY > viewportSize.y)
|
|
|
|
|
|
// resizeY /= 2;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
Vector2Int hardwareTextureSize = new Vector2Int(resizeX, resizeY);
|
|
|
|
|
|
mipLevelOffsets[0] = Vector2Int.zero;
|
|
|
|
|
|
mipLevelSizes[0] = Vector2Int.zero;
|
|
|
|
|
|
Vector2Int mipSize = hardwareTextureSize;
|
|
|
|
|
|
Vector2Int texSize = Vector2Int.zero;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
mipLevelCount = 1 + (int)math.floor(math.log2(math.max(resizeX, resizeY)));
|
|
|
|
|
|
|
|
|
|
|
|
for (int mipLevel = 1; mipLevel < mipLevelCount; mipLevel++)
|
2025-06-18 19:13:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
mipSize.x = System.Math.Max(1, (mipSize.x + 1) >> 1);
|
|
|
|
|
|
mipSize.y = System.Math.Max(1, (mipSize.y + 1) >> 1);
|
|
|
|
|
|
|
|
|
|
|
|
mipLevelSizes[mipLevel] = mipSize;
|
|
|
|
|
|
|
|
|
|
|
|
Vector2Int prevMipBegin = mipLevelOffsets[mipLevel - 1];
|
|
|
|
|
|
Vector2Int prevMipEnd = prevMipBegin + mipLevelSizes[mipLevel - 1];
|
|
|
|
|
|
|
|
|
|
|
|
Vector2Int mipBegin = new Vector2Int();
|
|
|
|
|
|
|
|
|
|
|
|
if ((mipLevel & 1) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
mipBegin.x = prevMipBegin.x;
|
|
|
|
|
|
mipBegin.y = prevMipEnd.y;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
mipBegin.x = prevMipEnd.x;
|
|
|
|
|
|
mipBegin.y = prevMipBegin.y;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (settings.SkipThreeMip && mipLevel == 4) //跳过前面3级的Mip,从第四级开始算
|
|
|
|
|
|
{
|
|
|
|
|
|
mipBegin = Vector2Int.zero;
|
|
|
|
|
|
texSize = Vector2Int.zero;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mipLevelOffsets[mipLevel] = mipBegin;
|
|
|
|
|
|
|
|
|
|
|
|
texSize.x = System.Math.Max(texSize.x, mipBegin.x + mipSize.x);
|
|
|
|
|
|
texSize.y = System.Math.Max(texSize.y, mipBegin.y + mipSize.y);
|
|
|
|
|
|
}
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
mipLevelSizes[0] = hardwareTextureSize;
|
|
|
|
|
|
//RT实际大小
|
|
|
|
|
|
depthPyramidTextureSize = new Vector2Int((int)Mathf.Ceil((float)texSize.x), (int)Mathf.Ceil((float)texSize.y));
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0;i < mipLevelSizes.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
mipOffsetAndSizes[i] = new Vector4(mipLevelOffsets[i].x, mipLevelOffsets[i].y, mipLevelSizes[i].x,
|
|
|
|
|
|
mipLevelSizes[i].y);
|
|
|
|
|
|
}
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int[] depthMipId = null;
|
|
|
|
|
|
|
|
|
|
|
|
private void DoCopyDepth(CommandBuffer cmd, Texture depthTex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (depthMipId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
depthMipId = new int[32];
|
|
|
|
|
|
for (int i = 0; i < depthMipId.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
depthMipId[i] = Shader.PropertyToID("_DepthMip" + i);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var sampleName = "DepthDownSample";
|
|
|
|
|
|
cmd.BeginSample(sampleName);
|
|
|
|
|
|
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalTexture(HizShaderIds.DepthInputId, depthTex);
|
2025-06-23 19:18:41 +08:00
|
|
|
|
for (int i = 0; i < mipLevelCount; i++)
|
2025-06-18 19:13:30 +08:00
|
|
|
|
{
|
2025-06-20 18:14:38 +08:00
|
|
|
|
var index = i;
|
2025-06-23 19:18:41 +08:00
|
|
|
|
if (settings.SkipThreeMip && i < 3)
|
2025-06-20 18:14:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-06-23 19:18:41 +08:00
|
|
|
|
else if (settings.SkipThreeMip && i == 3)
|
2025-06-20 18:14:38 +08:00
|
|
|
|
{
|
2025-06-23 19:18:41 +08:00
|
|
|
|
index = 0;
|
2025-06-20 18:14:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
var mipSize = mipLevelSizes[i];
|
2025-06-23 19:18:41 +08:00
|
|
|
|
var inputMipSize = index == 0 ? mip0SizeNOP : mipLevelSizes[i - 1];
|
2025-06-18 19:13:30 +08:00
|
|
|
|
var texId = depthMipId[i];
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalVector(HizShaderIds.InputScaleAndMaxIndexId, new Vector4(inputMipSize.x / (float)mipSize.x, inputMipSize.y / (float)mipSize.y, inputMipSize.x - 1, inputMipSize.y - 1));
|
2025-06-18 19:13:30 +08:00
|
|
|
|
cmd.GetTemporaryRT(texId, mipSize.x, mipSize.y, 0, FilterMode.Point, RenderTextureFormat.RFloat);
|
|
|
|
|
|
cmd.SetRenderTarget(texId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
|
|
|
|
|
|
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, settings.CopyDepth, 0, 0);
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalTexture(HizShaderIds.DepthInputId, texId);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cmd.EndSample(sampleName);
|
|
|
|
|
|
|
|
|
|
|
|
sampleName = "DepthCombine";
|
|
|
|
|
|
cmd.BeginSample(sampleName);
|
|
|
|
|
|
var hizIndex = GetHizIndex();
|
|
|
|
|
|
RTHandle hizRt = depthPyramidTexs[hizIndex];
|
|
|
|
|
|
|
|
|
|
|
|
RenderingUtils.ReAllocateIfNeeded(ref hizRt, new RenderTextureDescriptor()
|
|
|
|
|
|
{
|
|
|
|
|
|
width = depthPyramidTextureSize.x,
|
|
|
|
|
|
height = depthPyramidTextureSize.y,
|
|
|
|
|
|
dimension = TextureDimension.Tex2D,
|
|
|
|
|
|
colorFormat = RenderTextureFormat.RFloat,
|
|
|
|
|
|
msaaSamples = 1,
|
|
|
|
|
|
}, filterMode : FilterMode.Point, name: depthPyramidNames[hizIndex]);
|
|
|
|
|
|
depthPyramidTexs[hizIndex] = hizRt;
|
|
|
|
|
|
|
|
|
|
|
|
cmd.SetRenderTarget(hizRt, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
|
|
|
|
|
|
for (int i = 1; i < mipLevelCount; i++)
|
|
|
|
|
|
{
|
2025-06-20 18:14:38 +08:00
|
|
|
|
if (settings.SkipThreeMip && i < 3 + 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
var texId = depthMipId[i];
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalTexture(HizShaderIds.DepthInputId, texId);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
var mipSize = mipLevelSizes[i];
|
|
|
|
|
|
var mipOffset = mipLevelOffsets[i];
|
|
|
|
|
|
cmd.SetViewport(new Rect(mipOffset.x, mipOffset.y, mipSize.x, mipSize.y));
|
|
|
|
|
|
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, settings.CopyDepth, 0, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalTexture(HizShaderIds.DepthPyramidTexId, hizRt);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
cmd.EndSample(sampleName);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < depthMipId.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var texId = depthMipId[i];
|
|
|
|
|
|
cmd.ReleaseTemporaryRT(texId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DoComputeDepth(CommandBuffer cmd, Texture depthTex)
|
|
|
|
|
|
{
|
|
|
|
|
|
var hizIndex = GetHizIndex();
|
|
|
|
|
|
RTHandle hizBuffer = depthPyramidTexs[hizIndex];
|
2025-06-24 21:26:55 +08:00
|
|
|
|
cmd.BeginSample("Depth-Downsample");
|
2025-06-18 19:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
RenderingUtils.ReAllocateIfNeeded(ref hizBuffer, new RenderTextureDescriptor()
|
|
|
|
|
|
{
|
|
|
|
|
|
width = depthPyramidTextureSize.x,
|
|
|
|
|
|
height = depthPyramidTextureSize.y,
|
|
|
|
|
|
dimension = TextureDimension.Tex2D,
|
|
|
|
|
|
colorFormat = RenderTextureFormat.RFloat,
|
|
|
|
|
|
msaaSamples = 1,
|
|
|
|
|
|
enableRandomWrite = true,
|
2025-06-24 21:26:55 +08:00
|
|
|
|
sRGB = false,
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}, filterMode: FilterMode.Point, name: depthPyramidNames[hizIndex]);
|
|
|
|
|
|
depthPyramidTexs[hizIndex] = hizBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
int outputMipCnt = mipLevelCount - 1;
|
|
|
|
|
|
int dispatchCnt = Mathf.CeilToInt(outputMipCnt / 4f);
|
|
|
|
|
|
int mipCnt = outputMipCnt;
|
|
|
|
|
|
Matrix4x4 matrix4X4 = new Matrix4x4();
|
|
|
|
|
|
for (int i = 0; i <= dispatchCnt; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
int startMip = i * 4;
|
|
|
|
|
|
int mip1 = startMip + 1;
|
|
|
|
|
|
int mip2 = startMip + 2;
|
|
|
|
|
|
int mip3 = startMip + 3;
|
|
|
|
|
|
int mip4 = startMip + 4;
|
|
|
|
|
|
var outputMipSize = mipLevelSizes[mip1];
|
|
|
|
|
|
if(outputMipSize.x == 0 || outputMipSize.y == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
int kernelId = 0;
|
|
|
|
|
|
if (i == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
kernelId = 1;
|
|
|
|
|
|
if (settings.SkipThreeMip)
|
|
|
|
|
|
{
|
|
|
|
|
|
kernelId = 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetComputeTextureParam(settings.ComputeShader, kernelId, HizShaderIds.DepthInputId, i == 0 ? depthTex : hizBuffer);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
cmd.SetComputeTextureParam(settings.ComputeShader, kernelId, "_DepthMipChain", hizBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
int inputMipIndex = startMip;
|
|
|
|
|
|
var inputMipOffset = mipLevelOffsets[inputMipIndex];
|
|
|
|
|
|
var inputMipSize = inputMipIndex == 0 ? mip0SizeNOP : mipLevelSizes[inputMipIndex];
|
|
|
|
|
|
|
|
|
|
|
|
cmd.SetComputeVectorParam(settings.ComputeShader, "_InputMipOffsetAndSize", new Vector4(inputMipOffset.x, inputMipOffset.y, inputMipSize.x, inputMipSize.y));
|
|
|
|
|
|
cmd.SetComputeIntParam(settings.ComputeShader, "_MipCount", Mathf.Min(mipCnt, 4));
|
|
|
|
|
|
|
|
|
|
|
|
ref var mipOffset = ref mipLevelOffsets[mip1];
|
|
|
|
|
|
ref var mipSize = ref mipLevelSizes[mip1];
|
|
|
|
|
|
matrix4X4.SetRow(0, new Vector4(mipOffset.x, mipOffset.y, mipSize.x, mipSize.y));
|
|
|
|
|
|
mipOffset = ref mipLevelOffsets[mip2];
|
|
|
|
|
|
mipSize = ref mipLevelSizes[mip2];
|
|
|
|
|
|
matrix4X4.SetRow(1, new Vector4(mipOffset.x, mipOffset.y, mipSize.x, mipSize.y));
|
|
|
|
|
|
mipOffset = ref mipLevelOffsets[mip3];
|
|
|
|
|
|
mipSize = ref mipLevelSizes[mip3];
|
|
|
|
|
|
matrix4X4.SetRow(2, new Vector4(mipOffset.x, mipOffset.y, mipSize.x, mipSize.y));
|
|
|
|
|
|
mipOffset = ref mipLevelOffsets[mip4];
|
|
|
|
|
|
mipSize = ref mipLevelSizes[mip4];
|
|
|
|
|
|
matrix4X4.SetRow(3, new Vector4(mipOffset.x, mipOffset.y, mipSize.x, mipSize.y));
|
|
|
|
|
|
|
|
|
|
|
|
cmd.SetComputeMatrixParam(settings.ComputeShader, "_MipOffsetAndSizeArray", matrix4X4);
|
|
|
|
|
|
|
|
|
|
|
|
// XXX: 可以测试是否有正面作用
|
|
|
|
|
|
// cmd.SetExecutionFlags(CommandBufferExecutionFlags.AsyncCompute);
|
|
|
|
|
|
cmd.DispatchCompute(settings.ComputeShader, kernelId, Mathf.CeilToInt(outputMipSize.x / 8f), Mathf.CeilToInt(outputMipSize.y / 8f), 1);
|
|
|
|
|
|
mipCnt = mipCnt - 4;
|
|
|
|
|
|
}
|
2025-06-24 21:26:55 +08:00
|
|
|
|
cmd.EndSample("Depth-Downsample");
|
|
|
|
|
|
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalTexture(HizShaderIds.DepthPyramidTexId, hizBuffer);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DoSpdDepth(CommandBuffer cmd, Texture depthTex)
|
|
|
|
|
|
{
|
2025-06-20 18:14:38 +08:00
|
|
|
|
var hizIndex = GetHizIndex();
|
|
|
|
|
|
RTHandle hizBuffer = depthPyramidTexs[hizIndex];
|
2025-06-18 19:13:30 +08:00
|
|
|
|
|
2025-06-20 18:14:38 +08:00
|
|
|
|
RenderingUtils.ReAllocateIfNeeded(ref hizBuffer, new RenderTextureDescriptor()
|
|
|
|
|
|
{
|
|
|
|
|
|
width = depthPyramidTextureSize.x,
|
|
|
|
|
|
height = depthPyramidTextureSize.y,
|
|
|
|
|
|
dimension = TextureDimension.Tex2D,
|
|
|
|
|
|
colorFormat = RenderTextureFormat.RFloat,
|
|
|
|
|
|
msaaSamples = 1,
|
|
|
|
|
|
enableRandomWrite = true,
|
|
|
|
|
|
}, filterMode: FilterMode.Point, name: depthPyramidNames[hizIndex]);
|
|
|
|
|
|
depthPyramidTexs[hizIndex] = hizBuffer;
|
|
|
|
|
|
|
2025-06-25 11:11:33 +08:00
|
|
|
|
var dispatchX = Mathf.CeilToInt(mipOffsetAndSizes[0].z / 64f);
|
|
|
|
|
|
var dispatchY = Mathf.CeilToInt(mipOffsetAndSizes[0].w / 64f);
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetComputeIntParam(settings.Spd, "mips", mipLevelCount);
|
2025-06-24 21:26:55 +08:00
|
|
|
|
cmd.SetComputeIntParam(settings.Spd, "numWorkGroups", dispatchX * dispatchY);
|
2025-06-25 11:11:33 +08:00
|
|
|
|
cmd.SetComputeVectorParam(settings.Spd, "inputTextureSize", new Vector4(mip0SizeNOP.x, mip0SizeNOP.y, 0, 0));
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetComputeVectorArrayParam(settings.Spd, "_MipOffsetAndSizeArray", mipOffsetAndSizes);
|
|
|
|
|
|
|
|
|
|
|
|
cmd.SetComputeTextureParam(settings.Spd, 0, "_InputDepth", depthTex);
|
|
|
|
|
|
cmd.SetComputeTextureParam(settings.Spd, 0, "_OutDepth", hizBuffer);
|
|
|
|
|
|
cmd.SetComputeTextureParam(settings.Spd, 0, "rw_internal_global_atomic", SpdAtomicCounter);
|
|
|
|
|
|
|
|
|
|
|
|
cmd.DispatchCompute(settings.Spd, 0, dispatchX, dispatchY, 1);
|
|
|
|
|
|
cmd.SetGlobalTexture(HizShaderIds.DepthPyramidTexId, hizBuffer);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
private void DoCulling(ScriptableRenderContext context, ref RenderingData renderingData)
|
|
|
|
|
|
{
|
|
|
|
|
|
var cmd = renderingData.commandBuffer;;
|
|
|
|
|
|
cmd.BeginSample("Hiz-Culling");
|
|
|
|
|
|
var hizIndex = GetHizIndex();
|
|
|
|
|
|
var cullResult = cullResults[hizIndex];
|
|
|
|
|
|
var world2Project = renderingData.cameraData.GetGPUProjectionMatrix() *
|
|
|
|
|
|
renderingData.cameraData.GetViewMatrix();
|
|
|
|
|
|
if (settings.UseCompute)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.SetGlobalMatrix(HizShaderIds.GPUCullingVPId, world2Project);
|
2025-06-24 21:26:55 +08:00
|
|
|
|
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
cmd.SetRenderTarget(cullResult.ResultTex, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
|
|
|
|
|
|
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, settings.CullMat, 0, 0);
|
|
|
|
|
|
cmd.RequestAsyncReadback(cullResult.ResultTex, 0, cullResult.ReadBackAction);
|
|
|
|
|
|
//cmd.RequestAsyncReadbackIntoNativeArray(ref cullResult.ResultArray, cullResult.ResultTex, 0, cullResult.ReadBackAction);
|
|
|
|
|
|
cullResult.DataReady = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
cmd.EndSample("Hiz-Culling");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
|
|
|
|
|
{
|
|
|
|
|
|
var cmd = renderingData.commandBuffer;
|
2025-06-20 18:14:38 +08:00
|
|
|
|
Texture depthTex = Shader.GetGlobalTexture(HizShaderIds.CameraDepthTextureId);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
if(depthTex == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-06-23 19:18:41 +08:00
|
|
|
|
using var scp = new ProfilingScope(cmd, profiler);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
ComputePackedMipChainInfo(new Vector2Int(depthTex.width, depthTex.height));
|
|
|
|
|
|
|
|
|
|
|
|
switch (settings.PyramidFunc)
|
|
|
|
|
|
{
|
|
|
|
|
|
case EDepthPyramidFunc.CopyDepth:
|
|
|
|
|
|
DoCopyDepth(cmd, depthTex);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case EDepthPyramidFunc.Compute:
|
|
|
|
|
|
DoComputeDepth(cmd, depthTex);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case EDepthPyramidFunc.SPD:
|
|
|
|
|
|
DoSpdDepth(cmd, depthTex);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-06-20 18:14:38 +08:00
|
|
|
|
|
|
|
|
|
|
cmd.SetGlobalVector(HizShaderIds.Mip0SizeId, new Vector4(mip0SizeNOP.x, mip0SizeNOP.y, 0, 0));
|
2025-06-23 19:18:41 +08:00
|
|
|
|
if (settings.SkipThreeMip)
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.SetGlobalVector(HizShaderIds.MipmapLevelMinMaxIndexId, new Vector4(4, mipLevelCount - 1, 0, 0));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.SetGlobalVector(HizShaderIds.MipmapLevelMinMaxIndexId, new Vector4(1, mipLevelCount - 1, 0, 0));
|
|
|
|
|
|
}
|
2025-06-20 18:14:38 +08:00
|
|
|
|
cmd.SetGlobalVectorArray(HizShaderIds.MipOffsetAndSizeArrayId, mipOffsetAndSizes);
|
2025-06-23 19:18:41 +08:00
|
|
|
|
DoCulling(context, ref renderingData);
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int GetLastCullIndex(int hizIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!settings.UseThreeFrameReadback)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (hizIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
return 2;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new ArgumentException("参数错误 hizIndex:" + hizIndex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2025-06-20 18:14:38 +08:00
|
|
|
|
if (SpdAtomicCounter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SpdAtomicCounter.Release();
|
|
|
|
|
|
SpdAtomicCounter = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 19:13:30 +08:00
|
|
|
|
for (int i = 0; i < depthPyramidTexs.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rt = depthPyramidTexs[i];
|
|
|
|
|
|
rt?.Release();
|
|
|
|
|
|
}
|
2025-06-23 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < cullResults.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
cullResults[i]?.Dispose();
|
|
|
|
|
|
}
|
2025-06-18 19:13:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|