Skip to content

Commit

Permalink
DOTS Mask Volume (#6)
Browse files Browse the repository at this point in the history
Refactored the access to Mask Volume instanced to be able to feed a DOTS version to the system.
  • Loading branch information
AndrewSaraevUnity authored and pastasfuture committed Sep 15, 2021
1 parent 1a29f12 commit b2b6502
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace UnityEngine.Rendering.HighDefinition
{
interface IMaskVolumeList
{
void ReleaseRemovedVolumesFromAtlas();
int GetVolumeCount();

bool IsDataAssigned(int i);
bool IsDataUpdated(int i);
Vector3Int GetResolution(int i);

Vector3 GetPosition(int i);
Quaternion GetRotation(int i);

ref MaskVolumeArtistParameters GetParameters(int i);

int GetAtlasID(int i);

int GetDataSHL0Length(int i);
void SetDataSHL0(int i, ComputeBuffer buffer);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ internal struct MaskVolumeArtistParameters
{
public bool drawGizmos;
public Color debugColor;
public int payloadIndex;
public Vector3 size;
[SerializeField]
private Vector3 m_PositiveFade;
Expand Down Expand Up @@ -437,9 +436,6 @@ internal struct MaskVolumeArtistParameters
public float weight;
public float normalBiasWS;

// public float backfaceTolerance;
// public int dilationIterations;

public LightLayerEnum lightLayers;

public Vector3 positiveFade
Expand Down Expand Up @@ -484,7 +480,6 @@ public MaskVolumeArtistParameters(Color debugColor)
{
this.debugColor = debugColor;
this.drawGizmos = false;
this.payloadIndex = -1;
this.size = Vector3.one;
this.m_PositiveFade = Vector3.zero;
this.m_NegativeFade = Vector3.zero;
Expand All @@ -504,8 +499,6 @@ public MaskVolumeArtistParameters(Color debugColor)
this.blendMode = MaskVolumeBlendMode.Normal;
this.weight = 1;
this.normalBiasWS = 0.0f;
// this.dilationIterations = 2;
// this.backfaceTolerance = 0.25f;
this.lightLayers = LightLayerEnum.LightLayerDefault;
}

Expand Down Expand Up @@ -555,41 +548,6 @@ internal void Constrain()
[AddComponentMenu("Rendering/Mask Volume")]
internal class MaskVolume : MonoBehaviour
{
static List<MaskVolume> s_Volumes = null;

internal static List<MaskVolume> GetVolumes()
{
if (s_Volumes == null)
s_Volumes = new List<MaskVolume>();
return s_Volumes;
}

static void RegisterVolume(MaskVolume volume)
{
var volumes = GetVolumes();
if (volumes.Contains(volume))
return;

volumes.Add(volume);
}

static void DeRegisterVolume(MaskVolume volume)
{
var volumes = GetVolumes();
var volumeIndex = volumes.IndexOf(volume);
if (volumeIndex == -1)
return;

volumes.RemoveAt(volumeIndex);
ReleaseFromAtlas(volume);
}

static void ReleaseFromAtlas(MaskVolume volume)
{
if (RenderPipelineManager.currentPipeline is HDRenderPipeline hdrp)
hdrp.ReleaseMaskVolumeFromAtlas(volume);
}

#if UNITY_EDITOR
// Debugging code
private Material m_DebugMaterial = null;
Expand All @@ -615,49 +573,19 @@ static void ReleaseFromAtlas(MaskVolume volume)
[SerializeField] internal MaskVolumeAsset maskVolumeAsset = null;
[SerializeField] internal MaskVolumeArtistParameters parameters = new MaskVolumeArtistParameters(Color.white);

internal int GetID()
int GetID()
{
return GetInstanceID();
}

internal MaskVolumeEngineData ConvertToEngineData()
internal int GetAtlasID()
{
MaskVolumeEngineData data = new MaskVolumeEngineData();

data.weight = parameters.weight;
data.normalBiasWS = parameters.normalBiasWS;

data.debugColor.x = parameters.debugColor.r;
data.debugColor.y = parameters.debugColor.g;
data.debugColor.z = parameters.debugColor.b;

// Clamp to avoid NaNs.
Vector3 positiveFade = Vector3.Max(parameters.positiveFade, new Vector3(1e-5f, 1e-5f, 1e-5f));
Vector3 negativeFade = Vector3.Max(parameters.negativeFade, new Vector3(1e-5f, 1e-5f, 1e-5f));

data.rcpPosFaceFade.x = Mathf.Min(1.0f / positiveFade.x, float.MaxValue);
data.rcpPosFaceFade.y = Mathf.Min(1.0f / positiveFade.y, float.MaxValue);
data.rcpPosFaceFade.z = Mathf.Min(1.0f / positiveFade.z, float.MaxValue);

data.rcpNegFaceFade.y = Mathf.Min(1.0f / negativeFade.y, float.MaxValue);
data.rcpNegFaceFade.x = Mathf.Min(1.0f / negativeFade.x, float.MaxValue);
data.rcpNegFaceFade.z = Mathf.Min(1.0f / negativeFade.z, float.MaxValue);

data.blendMode = (int)parameters.blendMode;

float distFadeLen = Mathf.Max(parameters.distanceFadeEnd - parameters.distanceFadeStart, 0.00001526f);
data.rcpDistFadeLen = 1.0f / distFadeLen;
data.endTimesRcpDistFadeLen = parameters.distanceFadeEnd * data.rcpDistFadeLen;

data.scale = parameters.scale;
data.bias = parameters.bias;

data.resolution = new Vector3(maskVolumeAsset.resolutionX, maskVolumeAsset.resolutionY, maskVolumeAsset.resolutionZ);
data.resolutionInverse = new Vector3(1.0f / maskVolumeAsset.resolutionX, 1.0f / maskVolumeAsset.resolutionY, 1.0f / maskVolumeAsset.resolutionZ);

data.lightLayers = (uint)parameters.lightLayers;
return maskVolumeAsset.instanceID;
}

return data;
internal Vector3Int GetResolution()
{
return new Vector3Int(maskVolumeAsset.resolutionX, maskVolumeAsset.resolutionY, maskVolumeAsset.resolutionZ);
}

internal MaskVolumePayload GetPayload()
Expand Down Expand Up @@ -697,7 +625,7 @@ protected void OnEnable()
{
// Migrate();

RegisterVolume(this);
MaskVolumeManager.manager.RegisterVolume(this);

// Signal update
if (maskVolumeAsset)
Expand All @@ -714,12 +642,12 @@ protected void OnEnable()

protected void OnDisable()
{
DeRegisterVolume(this);
MaskVolumeManager.manager.DeRegisterVolume(this);
}

internal bool IsAssetCompatible()
internal bool IsDataAssigned()
{
return maskVolumeAsset;
return maskVolumeAsset && maskVolumeAsset.IsDataAssigned();
}

internal bool IsAssetMatchingResolution()
Expand Down Expand Up @@ -830,7 +758,7 @@ internal void ResampleAsset()
}
}

ReleaseFromAtlas(this);
MaskVolumeManager.manager.ReleaseVolumeFromAtlas(this);
}

maskVolumeAsset.instanceID = GetID();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace UnityEngine.Rendering.HighDefinition
{
struct MaskVolumeHandle
{
IMaskVolumeList m_List;
int m_Index;

public MaskVolumeHandle(IMaskVolumeList list, int index)
{
m_List = list;
m_Index = index;
}

public bool IsDataAssigned() => m_List.IsDataAssigned(m_Index);
public bool IsDataUpdated() => m_List.IsDataUpdated(m_Index);
public Vector3Int GetResolution() => m_List.GetResolution(m_Index);

public Vector3 position => m_List.GetPosition(m_Index);
public Quaternion rotation => m_List.GetRotation(m_Index);

public ref MaskVolumeArtistParameters parameters => ref m_List.GetParameters(m_Index);

public int GetAtlasID() => m_List.GetAtlasID(m_Index);

public int DataSHL0Length => m_List.GetDataSHL0Length(m_Index);
public void SetDataSHL0(ComputeBuffer buffer) => m_List.SetDataSHL0(m_Index, buffer);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;

namespace UnityEngine.Rendering.HighDefinition
{
internal class MaskVolumeManager : IMaskVolumeList
{
static private MaskVolumeManager _instance = null;

internal static MaskVolumeManager manager
{
get
{
if (_instance == null)
{
_instance = new MaskVolumeManager();
}
return _instance;
}
}

List<MaskVolume> m_Volumes = new List<MaskVolume>();

List<IMaskVolumeList> m_AdditionalMaskLists = new List<IMaskVolumeList>();
List<MaskVolumeHandle> m_VolumeHandles = new List<MaskVolumeHandle>();

internal List<MaskVolumeHandle> CollectVolumesToRender()
{
m_VolumeHandles.Clear();
var count = m_Volumes.Count;
for (int i = 0; i < count; i++)
m_VolumeHandles.Add(new MaskVolumeHandle(this, i));
foreach (var list in m_AdditionalMaskLists)
{
list.ReleaseRemovedVolumesFromAtlas();
count = list.GetVolumeCount();
for (int i = 0; i < count; i++)
m_VolumeHandles.Add(new MaskVolumeHandle(list, i));
}
return m_VolumeHandles;
}

internal void RegisterVolume(MaskVolume volume)
{
if (m_Volumes.Contains(volume))
return;

m_Volumes.Add(volume);
}

internal void DeRegisterVolume(MaskVolume volume)
{
var index = m_Volumes.IndexOf(volume);
if (index == -1)
return;

ReleaseVolumeFromAtlas(new MaskVolumeHandle(this, index));
m_Volumes.RemoveAt(index);
}

public void ReleaseVolumeFromAtlas(MaskVolume volume)
{
var index = m_Volumes.IndexOf(volume);
if (index == -1)
return;

ReleaseVolumeFromAtlas(new MaskVolumeHandle(this, index));
}

public void ReleaseVolumeFromAtlas(MaskVolumeHandle volume)
{
if (RenderPipelineManager.currentPipeline is HDRenderPipeline hdrp)
hdrp.ReleaseMaskVolumeFromAtlas(volume);
}

public void AddMaskList(IMaskVolumeList list)
{
m_AdditionalMaskLists.Add(list);
}

public void RemoveMaskList(IMaskVolumeList list)
{
m_AdditionalMaskLists.Remove(list);
}

void IMaskVolumeList.ReleaseRemovedVolumesFromAtlas() { }
int IMaskVolumeList.GetVolumeCount() => m_Volumes.Count;
bool IMaskVolumeList.IsDataAssigned(int i) => m_Volumes[i].IsDataAssigned();
bool IMaskVolumeList.IsDataUpdated(int i) => m_Volumes[i].dataUpdated;
Vector3Int IMaskVolumeList.GetResolution(int i) => m_Volumes[i].GetResolution();

Vector3 IMaskVolumeList.GetPosition(int i) => m_Volumes[i].transform.position;
Quaternion IMaskVolumeList.GetRotation(int i) => m_Volumes[i].transform.rotation;
ref MaskVolumeArtistParameters IMaskVolumeList.GetParameters(int i) => ref m_Volumes[i].parameters;
int IMaskVolumeList.GetAtlasID(int i) => m_Volumes[i].GetAtlasID();

int IMaskVolumeList.GetDataSHL0Length(int i) => m_Volumes[i].GetPayload().dataSHL0.Length;
void IMaskVolumeList.SetDataSHL0(int i, ComputeBuffer buffer) => buffer.SetData(m_Volumes[i].GetPayload().dataSHL0);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2b6502

Please sign in to comment.