Skip to content

Commit

Permalink
Merge pull request #7975 from Unity-Technologies/internal/master
Browse files Browse the repository at this point in the history
Internal/master
  • Loading branch information
UnityAljosha authored Oct 16, 2023
2 parents b2905e2 + 7553a37 commit 482d2f8
Show file tree
Hide file tree
Showing 193 changed files with 6,566 additions and 2,046 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1075,18 +1075,18 @@ static void BakeProbeVolumeOnly()
var positionsBufferID = ctx.CreateBuffer(positionsBytes);
using var positionsNative = new NativeArray<Vector3>(probePositions, Allocator.TempJob);
ctx.WriteBuffer(positionsBufferID, positionsNative.Reinterpret<byte>(sizeOfFloat * 3));
var positionsSlice = new BufferSlice(positionsBufferID, 0);
var positionsSlice = new BufferSlice<Vector3>(positionsBufferID, 0);

LightmapParameters parameters = LightmapParameters.GetLightmapParametersForLightingSettings(lightingSettings);
integrator.Prepare(world, positionsSlice, parameters.pushoff, bounceCount);
integrator.Prepare(ctx, world, positionsSlice, parameters.pushoff, bounceCount);
integrator.SetProgressReporter(progress);

var positionOffset = 0; // can be used to bake portions/tiles of the full set of positions.
var shBytes = (ulong)(sizeSHL2RGB * positionsLength);
var directRadianceBufferId = ctx.CreateBuffer(shBytes);
var indirectRadianceBufferId = ctx.CreateBuffer(shBytes);
var directRadianceSlice = new BufferSlice(directRadianceBufferId, 0);
var indirectRadianceSlice = new BufferSlice(indirectRadianceBufferId, 0);
var directRadianceSlice = new BufferSlice<SphericalHarmonicsL2>(directRadianceBufferId, 0);
var indirectRadianceSlice = new BufferSlice<SphericalHarmonicsL2>(indirectRadianceBufferId, 0);

// Bake direct radiance.
var integrationResult = integrator.IntegrateDirectRadiance(ctx, positionOffset, positionsLength,
Expand All @@ -1101,7 +1101,7 @@ static void BakeProbeVolumeOnly()
// Bake validity.
var validityBytes = (ulong)(sizeOfFloat * positionsLength);
var validityBufferId = ctx.CreateBuffer(validityBytes);
var validitySlice = new BufferSlice(validityBufferId, 0);
var validitySlice = new BufferSlice<float>(validityBufferId, 0);
var validityResult = integrator.IntegrateValidity(ctx, positionOffset, positionsLength,
sampleCountValidity, validitySlice);
Assert.AreEqual(IProbeIntegrator.ResultType.Success, validityResult.type, "IntegrateLightProbeValidity failed.");
Expand All @@ -1114,7 +1114,7 @@ static void BakeProbeVolumeOnly()
// Note that windowing can be controlled separately from de-ringing.
// Windowing and de-ringing are done together to stay as close to legacy light probe baking behaviour as possible.
var windowedDirectSHBufferId = ctx.CreateBuffer(shBytes);
var windowedDirectRadianceSlice = new BufferSlice(windowedDirectSHBufferId, 0);
var windowedDirectRadianceSlice = new BufferSlice<SphericalHarmonicsL2>(windowedDirectSHBufferId, 0);
bool dering = true; // TODO: get this from LightProbeGroup.dering or APV UI?
if (dering)
{
Expand All @@ -1128,7 +1128,7 @@ static void BakeProbeVolumeOnly()

// Apply indirect intensity multiplier to indirect radiance.
var boostedIndirectSHBufferId = ctx.CreateBuffer(shBytes);
var boostedIndirectRadianceSlice = new BufferSlice(boostedIndirectSHBufferId, 0);
var boostedIndirectRadianceSlice = new BufferSlice<SphericalHarmonicsL2>(boostedIndirectSHBufferId, 0);
if (lightingSettings.indirectScale.Equals(1.0f) == false)
{
bool multiplyOk = postProcessor.ScaleSphericalHarmonicsL2(ctx, indirectRadianceSlice, boostedIndirectRadianceSlice, positionsLength, lightingSettings.indirectScale);
Expand All @@ -1141,13 +1141,13 @@ static void BakeProbeVolumeOnly()

// Combine direct and indirect radiance.
var combinedSHBufferId = ctx.CreateBuffer(shBytes);
var combinedSHBufferSlice = new BufferSlice(combinedSHBufferId, 0);
var combinedSHBufferSlice = new BufferSlice<SphericalHarmonicsL2>(combinedSHBufferId, 0);
bool addOk = postProcessor.AddSphericalHarmonicsL2(ctx, windowedDirectRadianceSlice, boostedIndirectRadianceSlice, combinedSHBufferSlice, positionsLength);
Assert.IsTrue(addOk);

// Convert radiance to irradiance and transform to the format expected by the Unity renderer.
var irradianceBufferId = ctx.CreateBuffer(shBytes);
var irradianceSlice = new BufferSlice(irradianceBufferId, 0);
var irradianceSlice = new BufferSlice<SphericalHarmonicsL2>(irradianceBufferId, 0);
bool convolvedOk = postProcessor.ConvolveRadianceToIrradiance(ctx, combinedSHBufferSlice, irradianceSlice, positionsLength);
Assert.IsTrue(convolvedOk);

Expand All @@ -1170,9 +1170,9 @@ static void BakeProbeVolumeOnly()
Assert.IsTrue(flushOk);

// Wait for read backs to complete.
bool waitResult = ctx.WaitForAsyncOperation(irradianceReadEvent);
bool waitResult = ctx.Wait(irradianceReadEvent);
Debug.Assert(waitResult, "Failed to read irradiance from context.");
waitResult = ctx.WaitForAsyncOperation(validityReadEvent);
waitResult = ctx.Wait(validityReadEvent);
Debug.Assert(waitResult, "Failed to read validity from context.");

// Output data in result buffers is now ready, in CPU side memory, release all buffers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine.Experimental.Rendering.RenderGraphModule;
using UnityEngine.Experimental.Rendering.RenderGraphModule.NativeRenderPassCompiler;
using UnityEngine.Rendering;

internal class RenderGraphDebugger : IRenderGraphDebugger
{
Expand All @@ -19,7 +20,7 @@ public void OutputGraph(NativePassCompiler graph)
if (captureNextGraph)
{
var view = RenderGraphWindow.OpenGraphVisualizer();
CollectGraphDebugData(graph.contextData, graph.graph.m_ResourcesForDebugOnly, view);
CollectGraphDebugData(graph.contextData, graph.graph, view);
view.UpdateNodeVisualisation();
captureNextGraph = false;
}
Expand All @@ -32,10 +33,92 @@ public enum InputUsageType
Fetch //Used with fetch ops
}

// Actual values of global variables when a certain pass executes
class PassGlobalState
{
public PassGlobalState() { }
public PassGlobalState(PassGlobalState copyFrom)
{
values = new DynamicArray<ValueTuple<int, ResourceHandle>>(copyFrom.values);
}

public void SetGlobalValue(int shaderPropertyId, ResourceHandle value)
{
if (TryFind(shaderPropertyId, out var index))
{
values[index].Item2 = value;
}
else
{
values.Add(ValueTuple.Create(shaderPropertyId, value));
}
}

public ResourceHandle GetGlobalValue(int shaderPropertyId)
{
if (TryFind(shaderPropertyId, out var index))
{
return values[index].Item2;
}
return new ResourceHandle();
}

public bool IsUsedAsGlobal(ResourceHandle h)
{
for (int i = 0; i < values.size; i++)
{
if (h.index == values[i].Item2.index)
{
return true;
}
}
return false;
}

private bool TryFind(int shaderPropertyId, out int idx)
{
for (int i = 0; i < values.size; i++)
{
if (shaderPropertyId == values[i].Item1)
{
idx = i;
return true;
}
}
idx = -1;
return false;
}

DynamicArray<ValueTuple<int, ResourceHandle>> values = new DynamicArray<ValueTuple<int, ResourceHandle>>();
}


private static RenderGraphWindow debugWindow;
static void CollectGraphDebugData(CompilerContextData ctx,
RenderGraphResourceRegistry resources, RenderGraphView view)
NativePassCompiler.RenderGraphInputInfo inputInfo, RenderGraphView view)
{
var resources = inputInfo.m_ResourcesForDebugOnly;

// Derive the state of the global variables at each pass
List<PassGlobalState> passGlobals = new List<PassGlobalState>(ctx.passData.Length);
for (var i = 0; i < ctx.passData.Length; i++)
passGlobals.Add(null);
passGlobals[0] = new PassGlobalState();// first pass has empty globals set

for (var i = 0; i < ctx.passData.Length; i++)
{
ref readonly var pass = ref ctx.passData.ElementAt(i);
if (pass.passId == ctx.passData.Length-1) continue; // last pass doesn't matter
var nextGlobalState = new PassGlobalState(passGlobals[pass.passId]);
var globals = inputInfo.m_RenderPasses[pass.passId].setGlobalsList;
foreach (var g in globals)
{
nextGlobalState.SetGlobalValue(g.Item2, g.Item1.handle);
}

passGlobals[pass.passId + 1] = nextGlobalState;
}

//loop over all passes to add them and their resources to the graph
for (int passIndex = 0; passIndex < ctx.passData.Length; passIndex++)
{
Expand Down Expand Up @@ -297,7 +380,8 @@ static void CollectGraphDebugData(CompilerContextData ctx,

if (pointToVer.written)
{
view.AddConnection(prevPass.identifier, pass.identifier, resourceVersionedName, resourceName, mergeMessage, use);
bool isGlobal = passGlobals[pass.passId].IsUsedAsGlobal(inputResource);
view.AddConnection(prevPass.identifier, pass.identifier, resourceVersionedName, resourceName, mergeMessage, use, isGlobal);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,23 @@ internal void AddNRP(string nrpString)

foreach (var pass in nrpPasses)
{
nrp.title += pass.title + " ";
nrp.title += pass.title + ", ";
m_Passes.Remove(pass);
MoveConnections(pass, nrp, nrp.m_MergedPasses);
}

for (int i = 0; i < nrp.m_InputPort.Count; ++i)
for (int i = nrp.m_InputPort.Count-1; i >= 0; i--)
{
if (LinqButNotLinq.Any(nrp.m_InputPort[i].connections) == false)
bool isGlobal = (nrp.m_InputPort[i].portType == typeof(bool)); //HACK: Find a better way to keep track of globals
if (LinqButNotLinq.Any(nrp.m_InputPort[i].connections) == false && !isGlobal)
{

nrp.inputContainer.Remove(nrp.m_InputPort[i]);
nrp.m_InputPort.RemoveAt(i);
}
}

for (int i = 0; i < nrp.m_OutputPort.Count; ++i)
for (int i = nrp.m_OutputPort.Count-1; i >= 0; i--)
{
if (LinqButNotLinq.Any(nrp.m_OutputPort[i].connections) == false)
{
Expand Down Expand Up @@ -311,7 +313,8 @@ void MoveConnections(RenderGraphNode origin, RenderGraphNode destination, List<R
Port destPort = destination.GetPort(Direction.Input, port.name);
if (destPort == null)
{
destPort = destination.AddPort(Direction.Input, port.name);
bool isGlobal = (port.portType == typeof(bool)); //HACK: Find a better way to keep track of globals
destPort = destination.AddPort(Direction.Input, port.name, isGlobal);
}

foreach (var edge in port.connections)
Expand Down Expand Up @@ -341,7 +344,7 @@ void MoveConnections(RenderGraphNode origin, RenderGraphNode destination, List<R
var destPort = destination.GetPort(Direction.Output, port.name);
if (destPort == null)
{
destPort = destination.AddPort(Direction.Output, port.name);
destPort = destination.AddPort(Direction.Output, port.name, false);
}

foreach (var edge in port.connections)
Expand Down Expand Up @@ -426,7 +429,7 @@ internal void AddPass(string passID, string passName, RenderGraphWindow.PassDebu
}

internal void AddConnection(string from, string to, string resourceName,
string resourceNameNoVersion, string mergeMessage, RenderGraphDebugger.InputUsageType use)
string resourceNameNoVersion, string mergeMessage, RenderGraphDebugger.InputUsageType use, bool isGlobal)
{
var fromNode = m_Passes.Find(node => node.m_ID == from);
var toNode = m_Passes.Find(node => node.m_ID == to);
Expand Down Expand Up @@ -457,23 +460,28 @@ internal void AddConnection(string from, string to, string resourceName,
Port fromPort = fromNode.GetPort(Direction.Output, resourceNameNoVersion);
if (fromPort == null)
{
fromPort = fromNode.AddPort(Direction.Output, resourceNameNoVersion);
fromPort = fromNode.AddPort(Direction.Output, resourceNameNoVersion, isGlobal);
}

Port toPort = toNode.GetPort(Direction.Input, resourceNameNoVersion);
if (toPort == null)
{
toPort = toNode.AddPort(Direction.Input, resourceNameNoVersion);
toPort = toNode.AddPort(Direction.Input, resourceNameNoVersion, isGlobal);
}

//connect ports
var edge = new RenderGraphEdge();
edge.view = this;
edge.tooltip = mergeMessage;
edge.input = toPort;
edge.output = fromPort;
toPort.Connect(edge);
fromPort.Connect(edge);
if (!isGlobal)
{
var edge = new RenderGraphEdge();
edge.view = this;
edge.tooltip = mergeMessage;
edge.input = toPort;
edge.output = fromPort;
toPort.Connect(edge);
fromPort.Connect(edge);

AddElement(edge);
}

//save input/output connections to make sorting the view easier
if (!fromNode.m_Outputs.Contains(toNode))
Expand All @@ -485,8 +493,6 @@ internal void AddConnection(string from, string to, string resourceName,
{
toNode.m_Inputs.Add(fromNode);
}

AddElement(edge);
}

internal ResourceNode AddResource(string resourceID, string label, RenderGraphWindow.ResourceDebugData data)
Expand Down Expand Up @@ -910,10 +916,9 @@ public Port GetPort(Direction direction, string connectionName)
return null;
}

public Port AddPort(Direction portDirection, string connectionName,
Port.Capacity capacity = Port.Capacity.Multi)
public Port AddPort(Direction portDirection, string connectionName, bool isGlobal)
{
var n = InstantiatePort(Orientation.Horizontal, portDirection, capacity, typeof(string));
var n = InstantiatePort(Orientation.Horizontal, portDirection, Port.Capacity.Multi, (isGlobal) ? typeof(bool) : typeof(string));
n.name = connectionName;
n.portName = connectionName;
if (portDirection == Direction.Output)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace UnityEngine.Rendering
Expand Down Expand Up @@ -62,7 +63,7 @@ public DynamicArray(int size)
/// Constructor. This overload allows you to only allocate memory without setting the size.
/// </summary>
/// <param name="capacity">The nubmer of elements to allocate.</param>
/// <param name="resize">If true, also set the size of the array to the passed in capacity. If false, only allocate data but keep the size at 0.</param>///
/// <param name="resize">If true, also set the size of the array to the passed in capacity. If false, only allocate data but keep the size at 0.</param>///
public DynamicArray(int capacity, bool resize)
{
m_Array = new T[capacity];
Expand All @@ -72,6 +73,20 @@ public DynamicArray(int capacity, bool resize)
#endif
}

/// <summary>
/// Constructor. This constructor allocates memory and does a deep copy of the provided array.
/// </summary>
/// <param name="deepCopy">Array to be copied</param>
public DynamicArray(DynamicArray<T> deepCopy)
{
m_Array = new T[deepCopy.size];
size = deepCopy.size;
Array.Copy(deepCopy.m_Array, m_Array, size);
#if DEVELOPMENT_BUILD || UNITY_EDITOR
version = 0;
#endif
}

/// <summary>
/// Clear the array of all elements.
/// </summary>
Expand Down Expand Up @@ -102,7 +117,7 @@ public int Add(in T value)
// Grow array if needed;
if (index >= m_Array.Length)
{
var newArray = new T[m_Array.Length * 2];
var newArray = new T[Math.Max(m_Array.Length * 2,1)];
Array.Copy(m_Array, newArray, m_Array.Length);
m_Array = newArray;
}
Expand Down Expand Up @@ -193,7 +208,7 @@ public void RemoveRange(int index, int count)
{
if (count == 0)
return;

#if DEVELOPMENT_BUILD || UNITY_EDITOR
if (index < 0 || index >= size || count < 0 || index + count > size)
throw new ArgumentOutOfRangeException();
Expand Down
Loading

0 comments on commit 482d2f8

Please sign in to comment.