Skip to content

Commit

Permalink
Merge pull request #8065 from Unity-Technologies/internal/master
Browse files Browse the repository at this point in the history
Internal/master
  • Loading branch information
UnityAljosha authored Apr 29, 2024
2 parents 83af90b + cbb944d commit c8df1d8
Show file tree
Hide file tree
Showing 444 changed files with 68,959 additions and 2,183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ internal static class Styles
internal static readonly GUIContent virtualOffsetThreshold = new GUIContent("Validity Threshold", "Override the Virtual Offset Validity Threshold for probes covered by this Probe Adjustment Volume. Higher values increase the chance of probes being considered invalid.");
internal static readonly GUIContent s_VODirection = new GUIContent("Direction", "Rotate the axis along which probes will be pushed when applying Virtual Offset.");
internal static readonly GUIContent s_VODistance = new GUIContent("Distance", "Determines how far probes are pushed in the direction of the Virtual Offset.");
internal static readonly GUIContent renderingLayerMaskOperation = new GUIContent("Operation", "The operation to combine the Rendering Layer Mask set by this adjustment volume with the Rendering Layer Mask of the probes covered by this volume.");
internal static readonly GUIContent renderingLayerMask = new GUIContent("Rendering Layer Mask", "Sets the Rendering Layer Mask to be combined with the Rendering Layer Mask of the probes covered by this volume.");
internal static readonly GUIContent s_PreviewLighting = new GUIContent("Preview Probe Adjustments", "Quickly preview the effect of adjustments on probes covered by this volume.");

internal static readonly GUIContent skyOcclusionSampleCount = new GUIContent("Sample Count", "Controls the number of samples per probe for sky occlusion baking.");
Expand Down Expand Up @@ -248,6 +250,39 @@ public static void DrawAdjustmentContent(SerializedProbeAdjustmentVolume seriali
});
}
}
else if (serialized.mode.intValue == (int)ProbeAdjustmentVolume.Mode.OverrideRenderingLayerMask)
{
if (bakingSet != null && !bakingSet.useRenderingLayers)
{
CoreEditorUtils.DrawFixMeBox("Override Rendering Layer can be used only if Rendering Layers are enabled for the Baking Set.", MessageType.Warning, "Open", () =>
{
ProbeVolumeLightingTab.OpenBakingSet(bakingSet);
});
}
else
{
EditorGUILayout.PropertyField(serialized.renderingLayerMaskOperation, Styles.renderingLayerMaskOperation);

string[] options;
if (bakingSet != null)
{
options = new string[bakingSet.renderingLayerMasks.Length];
for (int i = 0; i < bakingSet.renderingLayerMasks.Length; i++)
options[i] = bakingSet.renderingLayerMasks[i].name;
}
else
{
options = new string[APVDefinitions.probeMaxRegionCount];
for (int i = 0; i < APVDefinitions.probeMaxRegionCount; i++)
options[i] = "Mask " + (i + 1);
}

EditorGUI.BeginChangeCheck();
int newMask = EditorGUILayout.MaskField(Styles.renderingLayerMask, serialized.renderingLayerMask.intValue, options);
if (EditorGUI.EndChangeCheck())
serialized.renderingLayerMask.uintValue = (uint)newMask;
}
}
}

static void DrawBakingHelpers(SerializedProbeAdjustmentVolume p, Editor owner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ static void PerformDilation(ProbeReferenceVolume.Cell cell, ProbeVolumeBakingSet
parameters.reflNormalizationUpperClamp = 1.0f;
parameters.skyOcclusionIntensity = 0.0f;
parameters.skyOcclusionShadingDirection = false;
parameters.regionCount = 1;
parameters.regionLayerMasks = 1;
ProbeReferenceVolume.instance.UpdateConstantBuffer(cmd, parameters);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace UnityEngine.Rendering
partial class AdaptiveProbeVolumes
{
// We use this scratch memory as a way of spoofing the texture.
static DynamicArray<float> s_Validity_locData = new DynamicArray<float>();
static DynamicArray<(float, byte)> s_ValidityLayer_locData = new DynamicArray<(float, byte)>();
static DynamicArray<int> s_ProbeIndices = new DynamicArray<int>();

internal static Vector3Int GetSampleOffset(int i)
Expand All @@ -17,28 +17,39 @@ internal static Vector3Int GetSampleOffset(int i)

const float k_MinValidityForLeaking = 0.05f;

internal static int PackValidity(float[] validity)
internal static uint PackValidity(float[] validity)
{
int outputByte = 0;
uint outputByte = 0;
for (int i = 0; i < 8; ++i)
{
int val = (validity[i] > k_MinValidityForLeaking) ? 0 : 1;
uint val = (validity[i] > k_MinValidityForLeaking) ? 0u : 1u;
outputByte |= (val << i);
}
return outputByte;
}

static void StoreScratchData(int x, int y, int z, int dataWidth, int dataHeight, float value, int probeIndex)
internal static uint PackLayer(byte[] layers, int layer)
{
uint outputLayer = 0;
for (int i = 0; i < 8; ++i)
{
if ((layers[i] & (byte)(1 << layer)) != 0)
outputLayer |= (1u << i);
}
return outputLayer;
}

static void StoreScratchData(int x, int y, int z, int dataWidth, int dataHeight, float value, byte layer, int probeIndex)
{
int index = x + dataWidth * (y + dataHeight * z);
s_Validity_locData[index] = value;
s_ValidityLayer_locData[index] = (value, layer);
s_ProbeIndices[index] = probeIndex;
}

static float ReadValidity(int x, int y, int z, int dataWidth, int dataHeight)
static (float, byte) ReadValidity(int x, int y, int z, int dataWidth, int dataHeight)
{
int index = x + dataWidth * (y + dataHeight * z);
return s_Validity_locData[index];
return s_ValidityLayer_locData[index];
}

static int ReadProbeIndex(int x, int y, int z, int dataWidth, int dataHeight)
Expand Down Expand Up @@ -74,6 +85,7 @@ static void ComputeValidityMasks(in BakingCell cell)
var bricks = cell.bricks;
int chunkSize = ProbeBrickPool.GetChunkSizeInBrickCount();
int brickChunksCount = (bricks.Length + chunkSize - 1) / chunkSize;
int validityLayerCount = cell.layerValidity != null ? cell.validityNeighbourMask.GetLength(0) : 1;

var probeHasEmptySpaceInGrid = new NativeArray<bool>(cell.probePositions.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

Expand All @@ -85,7 +97,7 @@ static void ComputeValidityMasks(in BakingCell cell)
int count = ProbeBrickPool.GetChunkSizeInProbeCount();
int bx = 0, by = 0, bz = 0;

s_Validity_locData.Resize(size);
s_ValidityLayer_locData.Resize(size);
s_ProbeIndices.Resize(size);

HashSet<Vector3Int> probesToRestore = new HashSet<Vector3Int>();
Expand All @@ -104,11 +116,12 @@ static void ComputeValidityMasks(in BakingCell cell)

if (shidx >= cell.validity.Length)
{
StoreScratchData(ix, iy, iz, locSize.x, locSize.y, 1.0f, shidx);
StoreScratchData(ix, iy, iz, locSize.x, locSize.y, 1.0f, 0, shidx);
}
else
{
StoreScratchData(ix, iy, iz, locSize.x, locSize.y, cell.validity[shidx], shidx);
byte layer = validityLayerCount > 1 ? cell.layerValidity[shidx] : (byte)0xFF;
StoreScratchData(ix, iy, iz, locSize.x, locSize.y, cell.validity[shidx], layer, shidx);

// Check if we need to do some extra check on this probe.
bool hasFreeNeighbourhood = false;
Expand Down Expand Up @@ -150,11 +163,11 @@ static void ComputeValidityMasks(in BakingCell cell)
for (int z = 0; z < locSize.z; ++z)
{
int outIdx = ReadProbeIndex(x, y, z, locSize.x, locSize.y);
float probeValidity = ReadValidity(x, y, z, locSize.x, locSize.y);

if (outIdx < cell.validity.Length)
{
float[] validities = new float[8];
byte[] layers = new byte[8];
bool forceAllValid = false;
for (int o = 0; o < 8; ++o)
{
Expand All @@ -171,14 +184,20 @@ static void ComputeValidityMasks(in BakingCell cell)
}
}

validities[o] = ReadValidity(samplePos.x, samplePos.y, samplePos.z, locSize.x, locSize.y);
(validities[o], layers[o]) = ReadValidity(samplePos.x, samplePos.y, samplePos.z, locSize.x, locSize.y);
}

byte mask = forceAllValid ? (byte)255 : Convert.ToByte(PackValidity(validities));
float validity = probeValidity;
// Keeping for safety but i think this is useless
(float probeValidity, uint _) = ReadValidity(x, y, z, locSize.x, locSize.y);
cell.validity[outIdx] = probeValidity;

cell.validity[outIdx] = validity;
cell.validityNeighbourMask[outIdx] = mask;
// Pack validity with layer mask
uint mask = forceAllValid ? 255 : PackValidity(validities);
for (int l = 0; l < validityLayerCount; l++)
{
uint layer = validityLayerCount == 1 ? 0xFF : PackLayer(layers, l);
cell.validityNeighbourMask[l, outIdx] = Convert.ToByte(mask & layer);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ struct APVRTContext

static IRayTracingShader m_ShaderVO = null;
static IRayTracingShader m_ShaderSO = null;
static IRayTracingShader m_ShaderRL = null;

internal IRayTracingAccelStruct CreateAccelerationStructure()
{
Expand Down Expand Up @@ -550,6 +551,25 @@ public IRayTracingShader shaderSO
}
}

public IRayTracingShader shaderRL
{
get
{
if (m_ShaderRL == null)
{
var bakingResources = GraphicsSettings.GetRenderPipelineSettings<ProbeVolumeBakingResources>();
m_ShaderRL = m_Context.CreateRayTracingShader(m_Backend switch
{
RayTracingBackend.Hardware => bakingResources.renderingLayerRT,
RayTracingBackend.Compute => bakingResources.renderingLayerCS,
_ => null
});
}

return m_ShaderRL;
}
}

public void BindSamplingTextures(CommandBuffer cmd)
{
if (m_SamplingResources == null)
Expand All @@ -561,6 +581,26 @@ public void BindSamplingTextures(CommandBuffer cmd)
SamplingResources.BindSamplingTextures(cmd, m_SamplingResources);
}

public bool TryGetMeshForAccelerationStructure(Renderer renderer, out Mesh mesh)
{
mesh = null;
if (renderer.isPartOfStaticBatch)
{
Debug.LogError("Static batching is not supported when baking APV.");
return false;
}

mesh = renderer.GetComponent<MeshFilter>().sharedMesh;
if (mesh == null)
return false;

// This would error out later in LoadIndexBuffer in LightTransport package
if ((mesh.indexBufferTarget & GraphicsBuffer.Target.Raw) == 0 && (mesh.GetIndices(0) == null || mesh.GetIndices(0).Length == 0))
return false;

return true;
}

public void Dispose()
{
if (m_Context != null)
Expand Down Expand Up @@ -626,13 +666,15 @@ internal static void BakeAdjustmentVolume(ProbeVolumeBakingSet bakingSet, ProbeA
bool savedSkyOcclusion = bakingSet.skyOcclusion;
bool savedSkyDirection = bakingSet.skyOcclusionShadingDirection;
bool savedVirtualOffset = bakingSet.settings.virtualOffsetSettings.useVirtualOffset;
bool savedRenderingLayers = bakingSet.useRenderingLayers;
{
// Patch baking set as we are not gonna use a mix of baked values and new values
bakingSet.simplificationLevels = bakingSet.bakedSimplificationLevels;
bakingSet.minDistanceBetweenProbes = bakingSet.bakedMinDistanceBetweenProbes;
bakingSet.skyOcclusion = bakingSet.bakedSkyOcclusion;
bakingSet.skyOcclusionShadingDirection = bakingSet.bakedSkyShadingDirection;
bakingSet.settings.virtualOffsetSettings.useVirtualOffset = bakingSet.supportOffsetsChunkSize != 0;
bakingSet.useRenderingLayers = bakingSet.bakedMaskCount == 1 ? false : true;

m_BakingSet = bakingSet;
m_BakingBatch = new BakingBatch(cellCount);
Expand Down Expand Up @@ -746,19 +788,27 @@ internal static void BakeAdjustmentVolume(ProbeVolumeBakingSet bakingSet, ProbeA
while (!failed && lightingJob.currentStep < lightingJob.stepCount)
failed |= !lightingJob.Step();

// Bake rendering layers
var layerMaskJob = renderingLayerOverride ?? new DefaultRenderingLayer();
layerMaskJob.Initialize(bakingSet, uniquePositions.AsArray());
while (!failed && layerMaskJob.currentStep < layerMaskJob.stepCount)
failed |= !layerMaskJob.Step();

// Upload new data in cells
foreach ((int uniqueProbeIndex, int cellIndex, int i) in bakedProbes)
{
ref var cell = ref bakingCells[cellIndex];
cell.SetBakedData(m_BakingSet, m_BakingBatch, cellVolumes[cellIndex], i, uniqueProbeIndex,
lightingJob.irradiance[uniqueProbeIndex], lightingJob.validity[uniqueProbeIndex],
virtualOffsetJob.offsets, skyOcclusionJob.occlusion, skyOcclusionJob.encodedDirections);
layerMaskJob.renderingLayerMasks, virtualOffsetJob.offsets,
skyOcclusionJob.occlusion, skyOcclusionJob.encodedDirections);
}

skyOcclusionJob.encodedDirections.Dispose();
virtualOffsetJob.Dispose();
skyOcclusionJob.Dispose();
lightingJob.Dispose();
layerMaskJob.Dispose();

if (!failed)
{
Expand Down Expand Up @@ -800,6 +850,7 @@ internal static void BakeAdjustmentVolume(ProbeVolumeBakingSet bakingSet, ProbeA
bakingSet.skyOcclusion = savedSkyOcclusion;
bakingSet.skyOcclusionShadingDirection = savedSkyDirection;
bakingSet.settings.virtualOffsetSettings.useVirtualOffset = savedVirtualOffset;
bakingSet.useRenderingLayers = savedRenderingLayers;

m_BakingBatch = null;
m_BakingSet = null;
Expand Down
Loading

0 comments on commit c8df1d8

Please sign in to comment.