Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1413,11 +1413,14 @@ static void ApplyPostBakeOperations()
if (m_BakingSet.hasDilation)
{
// This subsequent block needs to happen AFTER we call WriteBakingCells.
// Otherwise in cases where we change the spacing between probes, we end up loading cells with a certain layout in ForceSHBand
// Otherwise, in cases where we change the spacing between probes, we end up loading cells with a certain layout in ForceSHBand
// And then we unload cells using the wrong layout in PerformDilation (after WriteBakingCells updates the baking set object) which leads to a broken internal state.

// Don't use Disk streaming to avoid having to wait for it when doing dilation.
probeRefVolume.ForceNoDiskStreaming(true);
// Increase the memory budget to make sure we can fit the current cell and all its neighbors when doing dilation.
var prevMemoryBudget = probeRefVolume.memoryBudget;
probeRefVolume.ForceMemoryBudget(ProbeVolumeTextureMemoryBudget.MemoryBudgetHigh);
// Force maximum sh bands to perform baking, we need to store what sh bands was selected from the settings as we need to restore it after.
var prevSHBands = probeRefVolume.shBands;
probeRefVolume.ForceSHBand(ProbeVolumeSHBands.SphericalHarmonicsL2);
Expand All @@ -1428,8 +1431,9 @@ static void ApplyPostBakeOperations()
using (new BakingCompleteProfiling(BakingCompleteProfiling.Stages.PerformDilation))
PerformDilation();

// Need to restore the original state
// Restore the original state.
probeRefVolume.ForceNoDiskStreaming(false);
probeRefVolume.ForceMemoryBudget(prevMemoryBudget);
probeRefVolume.ForceSHBand(prevSHBands);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
area: Post-processing and UI Features
area: Post-processing and Compositing
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,7 @@ public static class DynamicArrayExtensions
}
}

// C# SUCKS
// Had to copy paste because it's apparently impossible to pass a sort delegate where T is Comparable<T>, otherwise some boxing happens and allocates...
// A copy/paste because it's apparently impossible to pass a sort delegate where T is Comparable<T>, otherwise some boxing happens and allocates...
// So two identical versions of the function, one with delegate but no Comparable and the other with just the comparable.
static int Partition<T>(Span<T> data, int left, int right, DynamicArray<T>.SortComparer comparer) where T : new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,7 @@ private void UpdateRendererInstancesAndBatches(in GPUDrivenRendererGroupData ren

Profiler.BeginSample("InstanceCullingBatcher.BuildBatch");
{
m_InstanceCullingBatcher.BuildBatch(
instances,
rendererData.materialID,
rendererData.meshID,
rendererData, true);

m_InstanceCullingBatcher.BuildBatch(instances, rendererData, true);
}
Profiler.EndSample();

Expand Down Expand Up @@ -234,15 +229,11 @@ private void UpdateRendererBatches(in GPUDrivenRendererGroupData rendererData, I

Profiler.BeginSample("InstanceCullingBatcher.BuildBatch");
{
m_InstanceCullingBatcher.BuildBatch(
instances.AsArray(),
rendererData.materialID,
rendererData.meshID,
rendererData, false);
instances.Dispose();
m_InstanceCullingBatcher.BuildBatch(instances.AsArray(), rendererData, false);
}
Profiler.EndSample();

instances.Dispose();
}
Profiler.EndSample();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
using Unity.Burst;
using UnityEngine.Profiling;

[assembly: RegisterGenericJobType(typeof(UnityEngine.Rendering.RegisterNewInstancesJob<UnityEngine.Rendering.BatchMeshID>))]
[assembly: RegisterGenericJobType(typeof(UnityEngine.Rendering.RegisterNewInstancesJob<UnityEngine.Rendering.BatchMaterialID>))]
[assembly: RegisterGenericJobType(typeof(UnityEngine.Rendering.FindNonRegisteredInstancesJob<UnityEngine.Rendering.BatchMeshID>))]
[assembly: RegisterGenericJobType(typeof(UnityEngine.Rendering.FindNonRegisteredInstancesJob<UnityEngine.Rendering.BatchMaterialID>))]

namespace UnityEngine.Rendering
{
internal delegate void OnCullingCompleteCallback(JobHandle jobHandle, in BatchCullingContext cullingContext, in BatchCullingOutput cullingOutput);
Expand Down Expand Up @@ -191,48 +186,109 @@ public void Execute(int startIndex, int count)
}

[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
internal struct FindNonRegisteredInstancesJob<T> : IJobParallelForBatch where T : unmanaged
internal struct FindNonRegisteredMeshesJob : IJobParallelForBatch
{
public const int k_BatchSize = 128;

[ReadOnly] public NativeArray<int> instanceIDs;
[ReadOnly] public NativeParallelHashMap<int, T> hashMap;
[ReadOnly] public NativeParallelHashMap<int, BatchMeshID> hashMap;

[WriteOnly] public NativeList<int>.ParallelWriter outInstancesWriter;

public unsafe void Execute(int startIndex, int count)
{
int* notFoundinstanceIDs = stackalloc int[k_BatchSize];
int length = 0;
int* notFoundinstanceIDsPtr = stackalloc int[k_BatchSize];
var notFoundinstanceIDs = new UnsafeList<int>(notFoundinstanceIDsPtr, k_BatchSize);

notFoundinstanceIDs.Length = 0;

for (int i = startIndex; i < startIndex + count; ++i)
{
int instanceID = instanceIDs[i];

if (!hashMap.ContainsKey(instanceID))
notFoundinstanceIDs[length++] = instanceID;
notFoundinstanceIDs.AddNoResize(instanceID);
}

outInstancesWriter.AddRangeNoResize(notFoundinstanceIDs, length);
outInstancesWriter.AddRangeNoResize(notFoundinstanceIDsPtr, notFoundinstanceIDs.Length);
}
}

[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
internal struct RegisterNewInstancesJob<T> : IJobParallelFor where T : unmanaged
internal struct FindNonRegisteredMaterialsJob : IJobParallelForBatch
{
public const int k_BatchSize = 128;

[ReadOnly] public NativeArray<int> instanceIDs;
[ReadOnly] public NativeArray<T> batchIDs;
[ReadOnly] public NativeArray<GPUDrivenPackedMaterialData> packedMaterialDatas;
[ReadOnly] public NativeParallelHashMap<int, BatchMaterialID> hashMap;

[WriteOnly] public NativeParallelHashMap<int, T>.ParallelWriter hashMap;
[WriteOnly] public NativeList<int>.ParallelWriter outInstancesWriter;
[WriteOnly] public NativeList<GPUDrivenPackedMaterialData>.ParallelWriter outPackedMaterialDatasWriter;

public unsafe void Execute(int index)
public unsafe void Execute(int startIndex, int count)
{
int* notFoundinstanceIDsPtr = stackalloc int[k_BatchSize];
var notFoundinstanceIDs = new UnsafeList<int>(notFoundinstanceIDsPtr, k_BatchSize);

GPUDrivenPackedMaterialData* notFoundPackedMaterialDatasPtr = stackalloc GPUDrivenPackedMaterialData[k_BatchSize];
var notFoundPackedMaterialDatas = new UnsafeList<GPUDrivenPackedMaterialData>(notFoundPackedMaterialDatasPtr, k_BatchSize);

notFoundinstanceIDs.Length = 0;
notFoundPackedMaterialDatas.Length = 0;

for (int i = startIndex; i < startIndex + count; ++i)
{
int instanceID = instanceIDs[i];

if (!hashMap.ContainsKey(instanceID))
{
notFoundinstanceIDs.AddNoResize(instanceID);
notFoundPackedMaterialDatas.AddNoResize(packedMaterialDatas[i]);
}
}

outInstancesWriter.AddRangeNoResize(notFoundinstanceIDsPtr, notFoundinstanceIDs.Length);
outPackedMaterialDatasWriter.AddRangeNoResize(notFoundPackedMaterialDatasPtr, notFoundPackedMaterialDatas.Length);
}
}

[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
internal struct RegisterNewMeshesJob : IJobParallelFor
{
public const int k_BatchSize = 128;

[ReadOnly] public NativeArray<int> instanceIDs;
[ReadOnly] public NativeArray<BatchMeshID> batchIDs;

[WriteOnly] public NativeParallelHashMap<int, BatchMeshID>.ParallelWriter hashMap;

public void Execute(int index)
{
hashMap.TryAdd(instanceIDs[index], batchIDs[index]);
}
}

[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
internal struct RegisterNewMaterialsJob : IJobParallelFor
{
public const int k_BatchSize = 128;

[ReadOnly] public NativeArray<int> instanceIDs;
[ReadOnly] public NativeArray<GPUDrivenPackedMaterialData> packedMaterialDatas;
[ReadOnly] public NativeArray<BatchMaterialID> batchIDs;

[WriteOnly] public NativeParallelHashMap<int, BatchMaterialID>.ParallelWriter batchMaterialHashMap;
[WriteOnly] public NativeParallelHashMap<int, GPUDrivenPackedMaterialData>.ParallelWriter packedMaterialHashMap;

public void Execute(int index)
{
var instanceID = instanceIDs[index];
batchMaterialHashMap.TryAdd(instanceID, batchIDs[index]);
packedMaterialHashMap.TryAdd(instanceID, packedMaterialDatas[index]);
}
}

[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
internal struct RemoveDrawInstanceIndicesJob : IJob
{
Expand Down Expand Up @@ -468,7 +524,7 @@ public void ProcessRenderer(int i)
{
var materialID = rendererData.materialID[materialIndex];
bool isFound = packedMaterialDataHash.TryGetValue(materialID, out packedMaterialData);
Assert.IsTrue(isFound);
Assert.IsTrue(isFound, "Packed material data not found.");
}
supportsIndirect &= packedMaterialData.isIndirectSupported;

Expand Down Expand Up @@ -1011,41 +1067,45 @@ public void PostCullBeginCameraRendering(RenderRequestBatcherContext context)
private void RegisterBatchMeshes(NativeArray<int> meshIDs)
{
var newMeshIDs = new NativeList<int>(meshIDs.Length, Allocator.TempJob);
new FindNonRegisteredInstancesJob<BatchMeshID>
new FindNonRegisteredMeshesJob
{
instanceIDs = meshIDs,
hashMap = m_BatchMeshHash,
outInstancesWriter = newMeshIDs.AsParallelWriter()
}
.ScheduleBatch(meshIDs.Length, FindNonRegisteredInstancesJob<BatchMeshID>.k_BatchSize).Complete();
.ScheduleBatch(meshIDs.Length, FindNonRegisteredMeshesJob.k_BatchSize).Complete();
var newBatchMeshIDs = new NativeArray<BatchMeshID>(newMeshIDs.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
m_BRG.RegisterMeshes(newMeshIDs.AsArray(), newBatchMeshIDs);

int totalMeshesNum = m_BatchMeshHash.Count() + newBatchMeshIDs.Length;
m_BatchMeshHash.Capacity = Math.Max(m_BatchMeshHash.Capacity, Mathf.CeilToInt(totalMeshesNum / 1023.0f) * 1024);

new RegisterNewInstancesJob<BatchMeshID>
new RegisterNewMeshesJob
{
instanceIDs = newMeshIDs.AsArray(),
batchIDs = newBatchMeshIDs,
hashMap = m_BatchMeshHash.AsParallelWriter()
}
.Schedule(newMeshIDs.Length, RegisterNewInstancesJob<BatchMeshID>.k_BatchSize).Complete();
.Schedule(newMeshIDs.Length, RegisterNewMeshesJob.k_BatchSize).Complete();

newMeshIDs.Dispose();
newBatchMeshIDs.Dispose();
}

private void RegisterBatchMaterials(in NativeArray<int> usedMaterialIDs)
private void RegisterBatchMaterials(in NativeArray<int> usedMaterialIDs, in NativeArray<GPUDrivenPackedMaterialData> usedPackedMaterialDatas)
{
Debug.Assert(usedMaterialIDs.Length == usedPackedMaterialDatas.Length, "Each material ID should correspond to one packed material data.");
var newMaterialIDs = new NativeList<int>(usedMaterialIDs.Length, Allocator.TempJob);
new FindNonRegisteredInstancesJob<BatchMaterialID>
var newPackedMaterialDatas = new NativeList<GPUDrivenPackedMaterialData>(usedMaterialIDs.Length, Allocator.TempJob);
new FindNonRegisteredMaterialsJob
{
instanceIDs = usedMaterialIDs,
packedMaterialDatas = usedPackedMaterialDatas,
hashMap = m_BatchMaterialHash,
outInstancesWriter = newMaterialIDs.AsParallelWriter()
outInstancesWriter = newMaterialIDs.AsParallelWriter(),
outPackedMaterialDatasWriter = newPackedMaterialDatas.AsParallelWriter()
}
.ScheduleBatch(usedMaterialIDs.Length, FindNonRegisteredInstancesJob<BatchMaterialID>.k_BatchSize).Complete();
.ScheduleBatch(usedMaterialIDs.Length, FindNonRegisteredMaterialsJob.k_BatchSize).Complete();

var newBatchMaterialIDs = new NativeArray<BatchMaterialID>(newMaterialIDs.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
m_BRG.RegisterMaterials(newMaterialIDs.AsArray(), newBatchMaterialIDs);
Expand All @@ -1054,15 +1114,18 @@ private void RegisterBatchMaterials(in NativeArray<int> usedMaterialIDs)
m_BatchMaterialHash.Capacity = Math.Max(m_BatchMaterialHash.Capacity, Mathf.CeilToInt(totalMaterialsNum / 1023.0f) * 1024);
m_PackedMaterialHash.Capacity = m_BatchMaterialHash.Capacity;

new RegisterNewInstancesJob<BatchMaterialID>
new RegisterNewMaterialsJob
{
instanceIDs = newMaterialIDs.AsArray(),
packedMaterialDatas = newPackedMaterialDatas.AsArray(),
batchIDs = newBatchMaterialIDs,
hashMap = m_BatchMaterialHash.AsParallelWriter()
batchMaterialHashMap = m_BatchMaterialHash.AsParallelWriter(),
packedMaterialHashMap = m_PackedMaterialHash.AsParallelWriter()
}
.Schedule(newMaterialIDs.Length, RegisterNewInstancesJob<BatchMaterialID>.k_BatchSize).Complete();
.Schedule(newMaterialIDs.Length, RegisterNewMaterialsJob.k_BatchSize).Complete();

newMaterialIDs.Dispose();
newPackedMaterialDatas.Dispose();
newBatchMaterialIDs.Dispose();
}

Expand All @@ -1078,15 +1141,13 @@ public JobHandle SchedulePackedMaterialCacheUpdate(NativeArray<int> materialIDs,

public void BuildBatch(
NativeArray<InstanceHandle> instances,
NativeArray<int> usedMaterialIDs,
NativeArray<int> usedMeshIDs,
in GPUDrivenRendererGroupData rendererData,
bool registerMaterialsAndMeshes)
{
if (registerMaterialsAndMeshes)
{
RegisterBatchMaterials(usedMaterialIDs);
RegisterBatchMeshes(usedMeshIDs);
RegisterBatchMaterials(rendererData.materialID, rendererData.packedMaterialData);
RegisterBatchMeshes(rendererData.meshID);
}

new CreateDrawBatchesJob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ internal bool Allocate(int numberOfBrickChunks, List<BrickChunkAlloc> outAllocat
if (!ignoreErrorLog)
Debug.LogError("Cannot allocate more brick chunks, probe volume brick pool is full.");

Deallocate(outAllocations);
outAllocations.Clear();
return false; // failure case, pool is full
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,11 @@ public void SetVertexSamplingEnabled(bool value)
m_VertexSampling = value;
}

internal void ForceMemoryBudget(ProbeVolumeTextureMemoryBudget budget)
{
m_MemoryBudget = budget;
}

// This is used for steps such as dilation that require the maximum order allowed to be loaded at all times. Should really never be used as a general purpose function.
internal void ForceSHBand(ProbeVolumeSHBands shBands)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
area: Post-processing and UI Features
area: Post-processing and Compositing
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ Use HDRP's water system to create and control realistic water surfaces. HDRP's w
- Multiple presets.
- Simulation-based caustics.
- Underwater rendering.
- Deformer.
- Deformation.
- Foam.
- Water Excluder.
- A mirrored simulation on the CPU for high-fidelity game interactions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ This sample includes examples on how to create a [Fullscreen Shader](create-a-fu
The Water samples contain the following scenes you can use to learn about HDRP's [Water](water.md) features:

- Pool: Demonstrates ripples and buoyancy.
- Glacier: Demonstrates current, water deformers, floating objects, and a simulation mask.
- Glacier: Demonstrates current, deformation, water decals, floating objects, and a simulation mask.
- Island: Demonstrates waves, foam, and the water excluder.
- Rain: Demonstrates how to add pertubations to the normals using shader graph.
- Waterline: Demonstrates how to override rendering of the waterline using a [Custom Pass](Custom-Pass.md).
Expand Down
Loading