Finish up the HizCulling

This commit is contained in:
2025-02-22 23:01:42 +09:00
parent f0dbf8e581
commit 833502f87c
12 changed files with 136 additions and 129 deletions

View File

@@ -9,8 +9,6 @@ using UnityEngine.Rendering.HighDefinition;
internal class AoVolumePass : CustomPass
{
private bool _initialized;
private NativeArray<OrientedBoundingBox> _volumeBounds;
private ComputeBuffer _volumeBoundsBuffer;
private ComputeBuffer _visibleIndicesBuffer;
@@ -39,11 +37,6 @@ internal class AoVolumePass : CustomPass
// The render pipeline will ensure target setup and clearing happens in an performance manner.
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
if (_initialized)
{
return;
}
_volumeBounds = new NativeArray<OrientedBoundingBox>(64, Allocator.Persistent);
_volumeBoundsBuffer = new ComputeBuffer(64, Marshal.SizeOf<OrientedBoundingBox>());
_visibleIndicesBuffer = new ComputeBuffer(64, Marshal.SizeOf<uint>(), ComputeBufferType.Raw);
@@ -54,17 +47,15 @@ internal class AoVolumePass : CustomPass
_volumeBuffer = RTHandles.Alloc(Vector2.one, useDynamicScale: true, dimension: TextureXR.dimension, enableRandomWrite: true, format: GraphicsFormat.R8_UNorm, name: "AO Volume Buffer");
ClearVisibleVolumeCounter();
}
_initialized = true;
private static int GetDepthPyramidMaxMipLevel(HDCamera hDCamera)
{
return Mathf.CeilToInt(Mathf.Log(Mathf.Min(hDCamera.actualWidth, hDCamera.actualHeight), 2)) - 1;
}
protected override void Execute(CustomPassContext ctx)
{
if (!_initialized)
{
return;
}
if (cullingShader == null || renderingShader == null)
{
return;
@@ -82,11 +73,14 @@ internal class AoVolumePass : CustomPass
return;
}
const int groupSizeX = 8;
const int groupSizeY = 8;
var threadGroupX = (ctx.hdCamera.actualWidth + (groupSizeX - 1)) / groupSizeX;
var threadGroupY = (ctx.hdCamera.actualHeight + (groupSizeY - 1)) / groupSizeY;
HierarchicalZCulling(ctx, volumeCount);
//DebugVisibleVolumes();
RenderVisibleVolumes(ctx);
ClearVisibleVolumeCounter();
}
private void HierarchicalZCulling(CustomPassContext ctx, int volumeCount)
{
for (var i = 0; i < volumeCount; i++)
{
_volumeBounds[i] = new OrientedBoundingBox(VolumeDatabase.Instance.VolumeDatas[i].worldMatrix);
@@ -95,16 +89,37 @@ internal class AoVolumePass : CustomPass
ctx.cmd.SetComputeBufferParam(cullingShader, 0, "_VolumeBounds", _volumeBoundsBuffer);
ctx.cmd.SetComputeIntParam(cullingShader, "_FullVolumeCount", volumeCount);
ctx.cmd.SetComputeIntParam(cullingShader, "_DepthPyramidMaxMip", ctx.cameraDepthBuffer.rt.mipmapCount - 1);
ctx.cmd.SetComputeIntParam(cullingShader, "_DepthPyramidMaxMip", GetDepthPyramidMaxMipLevel(ctx.hdCamera));
ctx.cmd.SetComputeBufferParam(cullingShader, 0, "_VisibleVolumeIndices", _visibleIndicesBuffer);
ctx.cmd.SetComputeBufferParam(cullingShader, 0, "_VisibleVolumeCount", _visibleVolumeCountBuffer);
ctx.cmd.SetComputeBufferParam(cullingShader, 0, "_VisibleVolumeIndices", _visibleIndicesBuffer);
const int groupSize = 64;
var threadGroup = (volumeCount + (groupSize - 1)) / groupSize;
ctx.cmd.DispatchCompute(cullingShader, 0, threadGroup, 1, 1);
}
// Debug
private void RenderVisibleVolumes(CustomPassContext ctx)
{
_volumeDataBuffer.SetData(VolumeDatabase.Instance.VolumeDatas);
ctx.cmd.SetComputeBufferParam(renderingShader, 0, "_VolumeDatas", _volumeDataBuffer);
ctx.cmd.SetComputeBufferParam(renderingShader, 0, "_VisibleVolumeCount", _visibleVolumeCountBuffer);
ctx.cmd.SetComputeBufferParam(renderingShader, 0, "_VisibleVolumeIndices", _visibleIndicesBuffer);
ctx.cmd.SetComputeTextureParam(renderingShader, 0, "_AOVolumeBuffer", _volumeBuffer);
const int groupSizeX = 8;
const int groupSizeY = 8;
var threadGroupX = (ctx.hdCamera.actualWidth + (groupSizeX - 1)) / groupSizeX;
var threadGroupY = (ctx.hdCamera.actualHeight + (groupSizeY - 1)) / groupSizeY;
ctx.cmd.DispatchCompute(renderingShader, 0, threadGroupX, threadGroupY, _volumeBuffer.rt.volumeDepth);
ctx.cmd.SetGlobalTexture("_AmbientOcclusionTexture", _volumeBuffer);
}
private void DebugVisibleVolumes()
{
var visibleVolumeCount = new int[1];
_visibleVolumeCountBuffer.GetData(visibleVolumeCount);
Debug.Log($"Visible Volume Count: {visibleVolumeCount[0]}");
@@ -118,23 +133,12 @@ internal class AoVolumePass : CustomPass
break;
}
Debug.Log($"Visible Volume Index: {visibleVolumeIndices[i]}");
var volumeIndex = visibleVolumeIndices[i];
Debug.Log($"Visible Volume Index: {volumeIndex}");
Debug.Log($"Volume Data: {VolumeDatabase.Instance.VolumeDatas[(int)volumeIndex].worldMatrix.GetColumn(3)}");
}
Debug.Log("End");
ArrayPool<uint>.Shared.Return(visibleVolumeIndices);
ClearVisibleVolumeCounter();
//return;
//_volumeDataBuffer.SetData(VolumeDatabase.Instance.VolumeDatas);
//ctx.cmd.SetComputeBufferParam(renderingShader, 0, "_VolumeDatas", _volumeDataBuffer);
//ctx.cmd.SetComputeIntParam(renderingShader, "_VolumeCount", volumeCount);
//ctx.cmd.SetComputeTextureParam(renderingShader, 0, "_AOVolumeBuffer", _volumeBuffer);
//ctx.cmd.DispatchCompute(renderingShader, 0, threadGroupX, threadGroupY, _volumeBuffer.rt.volumeDepth);
//ctx.cmd.SetGlobalTexture("_AmbientOcclusionTexture", _volumeBuffer);
}
protected override void Cleanup()
@@ -147,7 +151,5 @@ internal class AoVolumePass : CustomPass
_volumeDataBuffer?.Dispose();
_volumeBuffer?.Release();
_initialized = false;
}
}