Initial Upload;

This commit is contained in:
2025-02-18 19:38:13 +09:00
commit de8eafb713
27 changed files with 554 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
using Misaki.AoVolume;
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
internal class AoVolumePass : CustomPass
{
private bool _initialized;
private NativeArray<VolumeData> _aoVolumeDatas;
private ComputeBuffer _aoVolumeDataBuffer;
private RTHandle _aoVolumeBuffer;
[ResourcePath("Packages/com.misaki.ao-volume/Runtime/Shader/AoVolume.compute")]
public ComputeShader shader;
// It can be used to configure render targets and their clear state. Also to create temporary render target textures.
// When empty this render pass will render to the active camera render target.
// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
// The render pipeline will ensure target setup and clearing happens in an performance manner.
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
_aoVolumeDatas = new NativeArray<VolumeData>(64, Allocator.Persistent);
_aoVolumeDataBuffer = new ComputeBuffer(64, Marshal.SizeOf<VolumeData>());
_aoVolumeBuffer = RTHandles.Alloc(Vector2.one, useDynamicScale: true, dimension: TextureXR.dimension, enableRandomWrite: true, format: GraphicsFormat.R8_UNorm, name: "AO Volume Buffer");
_initialized = true;
}
protected override void Execute(CustomPassContext ctx)
{
if (!_initialized)
{
return;
}
if (shader == null)
{
return;
}
var volumeCount = VolumeDatabase.Instance.volumeObjects.Count;
if (volumeCount <= 0)
{
return;
}
for (var i = 0; i < volumeCount; i++)
{
_aoVolumeDatas[i] = VolumeDatabase.Instance.volumeObjects[i].data;
}
_aoVolumeDataBuffer.SetData(_aoVolumeDatas);
// Worth it to allocate a new buffer?
//if (Shader.GetGlobalTexture("_AmbientOcclusionTexture") is not RenderTexture gtaoBuffer)
//{
// return;
//}
var kernelIndex = shader.FindKernel("CSMain");
ctx.cmd.SetComputeBufferParam(shader, kernelIndex, "_VolumeDatas", _aoVolumeDataBuffer);
ctx.cmd.SetComputeIntParam(shader, "_VolumeCount", volumeCount);
ctx.cmd.SetComputeTextureParam(shader, kernelIndex, "_AOVolumeBuffer", _aoVolumeBuffer);
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(shader, kernelIndex, threadGroupX, threadGroupY, _aoVolumeBuffer.rt.volumeDepth);
ctx.cmd.SetGlobalTexture("_AmbientOcclusionTexture", _aoVolumeBuffer);
}
protected override void Cleanup()
{
_aoVolumeBuffer?.Release();
_aoVolumeDatas.Dispose();
_aoVolumeDataBuffer?.Release();
_initialized = false;
}
}