Skip to content

Commit

Permalink
Merge pull request #8098 from Unity-Technologies/internal/2021.3/staging
Browse files Browse the repository at this point in the history
Internal/2021.3/staging
  • Loading branch information
UnityAljosha committed Sep 19, 2024
2 parents 77948e4 + b08c5f3 commit 358299b
Show file tree
Hide file tree
Showing 98 changed files with 3,643 additions and 389 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This document describes the main principles of a render graph and an overview of

Before you can write render passes with the [RenderGraph](../api/UnityEngine.Experimental.Rendering.RenderGraphModule.RenderGraph.html) API, you need to know the following foundational principles:

- You no longer handle resources directly and instead use render graph system-specific handles. All RenderGraph APIs use these handles to manipulate resources. The resource types a render graph manages are [RTHandles](rthandle-system.md), [ComputeBuffers](https://docs.unity3d.com/ScriptReference/ComputeBuffer.html), and [RendererLists](../api/UnityEngine.Experimental.Rendering.RendererList.html).
- You no longer handle resources directly and instead use render graph system-specific handles. All RenderGraph APIs use these handles to manipulate resources. The resource types a render graph manages are [RTHandles](rthandle-system.md), [ComputeBuffers](xref:UnityEngine.ComputeBuffer), and [RendererLists](xref:UnityEngine.Rendering.RendererList).
- Actual resource references are only accessible within the execution code of a render pass.
- The framework requires an explicit declaration of render passes. Each render pass must state which resources it reads from and/or writes to.
- There is no persistence between each execution of a render graph. This means that the resources you create inside one execution of the render graph cannot carry over to the next execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This document describes the main principles of the RTHandle (RTHandle) system.
The RTHandle system is an abstraction on top of Unity's [RenderTexture](https://docs.unity3d.com/ScriptReference/RenderTexture.html) API. It makes it trivial to reuse render textures across Cameras that use various resolutions. The following principles are the foundation of how the RTHandle system works:

- You no longer allocate render textures yourself with a fixed resolution. Instead, you declare a render texture using a scale related to the full screen at a given resolution. The RTHandle system allocates the texture only once for the whole render pipeline so that it can reuse it for different Cameras.
- There is now the concept of reference size. This is the resolution the application uses for rendering. It is your responsibility to declare it before the render pipeline renders every Camera at a particular resolution. For information on how to do this, see the [Updating the RTHandle system](#updating-the-rthandle-system) section.
- There is now the concept of reference size. This is the resolution the application uses for rendering. It is your responsibility to declare it before the render pipeline renders every Camera at a particular resolution. For information on how to do this, see the [Updating the RTHandle system](rthandle-system-using.md#updating-the-rthandle-system) section.
- Internally, the RTHandle system tracks the largest reference size you declare. It uses this as the actual size of render textures. The largest reference size is the maximum size.
- Every time you declare a new reference size for rendering, the RTHandle system checks if it is larger than the current recorded largest reference size. If it is, the RTHandle system reallocates all render textures internally to fit the new size and replaces the largest reference size with the new size.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public HierarchicalBox(Color baseColor, Color[] polychromeHandleColors = null, H
public void DrawHull(bool filled)
{
Color previousColor = Handles.color;
if (filled)
if (filled && Event.current.type == EventType.Repaint)
{
// Draw the hull
var xSize = new Vector3(size.z, size.y, 1f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ internal void OnPassAdded(RenderGraphPass pass)
{
if (m_DebugParameters.immediateMode)
{
ExecutePassImmediatly(pass);
ExecutePassImmediately(pass);
}
}

Expand Down Expand Up @@ -1323,7 +1323,7 @@ ref CompiledPassInfo CompilePassImmediatly(RenderGraphPass pass)
return ref passInfo;
}

void ExecutePassImmediatly(RenderGraphPass pass)
void ExecutePassImmediately(RenderGraphPass pass)
{
ExecuteCompiledPass(ref CompilePassImmediatly(pass), m_CurrentImmediatePassIndex - 1);
}
Expand Down Expand Up @@ -1422,25 +1422,37 @@ void PreRenderPassExecute(in CompiledPassInfo passInfo, RenderGraphContext rgCon
// Need to save the command buffer to restore it later as the one in the context can changed if running a pass async.
m_PreviousCommandBuffer = rgContext.cmd;

bool executedWorkDuringResourceCreation = false;
for (int type = 0; type < (int)RenderGraphResourceType.Count; ++type)
{
foreach (int resource in passInfo.resourceCreateList[type])
{
m_Resources.CreatePooledResource(rgContext, type, resource);
executedWorkDuringResourceCreation |= m_Resources.CreatePooledResource(rgContext, type, resource);
}
}

PreRenderPassSetRenderTargets(passInfo, rgContext);

if (passInfo.enableAsyncCompute)
{
GraphicsFence previousFence = new GraphicsFence();
if (executedWorkDuringResourceCreation)
{
previousFence = rgContext.cmd.CreateGraphicsFence(GraphicsFenceType.AsyncQueueSynchronisation, SynchronisationStageFlags.AllGPUOperations);
}

// Flush current command buffer on the render context before enqueuing async commands.
rgContext.renderContext.ExecuteCommandBuffer(rgContext.cmd);
rgContext.cmd.Clear();

CommandBuffer asyncCmd = CommandBufferPool.Get(pass.name);
asyncCmd.SetExecutionFlags(CommandBufferExecutionFlags.AsyncCompute);
rgContext.cmd = asyncCmd;

if (executedWorkDuringResourceCreation)
{
rgContext.cmd.WaitOnAsyncGraphicsFence(previousFence);
}
}

// Synchronize with graphics or compute pipe if needed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using UnityEngine.Rendering;

namespace UnityEngine.Experimental.Rendering.RenderGraphModule
{
Expand Down Expand Up @@ -82,13 +83,11 @@ public ComputeBufferDesc(int count, int stride, ComputeBufferType type)
/// <returns>The texture descriptor hash.</returns>
public override int GetHashCode()
{
int hashCode = 17;

hashCode = hashCode * 23 + count;
hashCode = hashCode * 23 + stride;
hashCode = hashCode * 23 + (int)type;

return hashCode;
var hashCode = HashFNV1A32.Create();
hashCode.Append(count);
hashCode.Append(stride);
hashCode.Append((int)type);
return hashCode.value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ internal static RenderGraphResourceRegistry current
m_CurrentRegistry = value;
}
}


delegate bool ResourceCreateCallback(RenderGraphContext rgContext, IRenderGraphResource res);
delegate void ResourceCallback(RenderGraphContext rgContext, IRenderGraphResource res);

class RenderGraphResourcesData
{
public DynamicArray<IRenderGraphResource> resourceArray = new DynamicArray<IRenderGraphResource>();
public int sharedResourcesCount;
public IRenderGraphResourcePool pool;
public ResourceCallback createResourceCallback;
public ResourceCreateCallback createResourceCallback;
public ResourceCallback releaseResourceCallback;

public void Clear(bool onException, int frameIndex)
Expand Down Expand Up @@ -435,8 +436,9 @@ void ManageSharedRenderGraphResources()
}
}

internal void CreatePooledResource(RenderGraphContext rgContext, int type, int index)
internal bool CreatePooledResource(RenderGraphContext rgContext, int type, int index)
{
bool? executedWork = false;
var resource = m_RenderGraphResources[type].resourceArray[index];
if (!resource.imported)
{
Expand All @@ -445,11 +447,13 @@ internal void CreatePooledResource(RenderGraphContext rgContext, int type, int i
if (m_RenderGraphDebug.enableLogging)
resource.LogCreation(m_FrameInformationLogger);

m_RenderGraphResources[type].createResourceCallback?.Invoke(rgContext, resource);
executedWork = m_RenderGraphResources[type].createResourceCallback?.Invoke(rgContext, resource);
}

return executedWork ?? false;
}

void CreateTextureCallback(RenderGraphContext rgContext, IRenderGraphResource res)
bool CreateTextureCallback(RenderGraphContext rgContext, IRenderGraphResource res)
{
var resource = res as TextureResource;

Expand All @@ -461,6 +465,7 @@ void CreateTextureCallback(RenderGraphContext rgContext, IRenderGraphResource re
}
#endif

bool executedWork = false;
if (resource.desc.clearBuffer || m_RenderGraphDebug.clearRenderTargetsAtCreation)
{
bool debugClear = m_RenderGraphDebug.clearRenderTargetsAtCreation && !resource.desc.clearBuffer;
Expand All @@ -470,7 +475,9 @@ void CreateTextureCallback(RenderGraphContext rgContext, IRenderGraphResource re
var clearColor = debugClear ? Color.magenta : resource.desc.clearColor;
CoreUtils.SetRenderTarget(rgContext.cmd, resource.graphicsResource, clearFlag, clearColor);
}
executedWork = true;
}
return executedWork;
}

internal void ReleasePooledResource(RenderGraphContext rgContext, int type, int index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,48 +241,42 @@ public TextureDesc(TextureDesc input)
/// <returns>The texture descriptor hash.</returns>
public override int GetHashCode()
{
int hashCode = 17;

unchecked
var hashCode = HashFNV1A32.Create();
switch (sizeMode)
{
switch (sizeMode)
{
case TextureSizeMode.Explicit:
hashCode = hashCode * 23 + width;
hashCode = hashCode * 23 + height;
break;
case TextureSizeMode.Functor:
if (func != null)
hashCode = hashCode * 23 + func.GetHashCode();
break;
case TextureSizeMode.Scale:
hashCode = hashCode * 23 + scale.x.GetHashCode();
hashCode = hashCode * 23 + scale.y.GetHashCode();
break;
}
case TextureSizeMode.Explicit:
hashCode.Append(width);
hashCode.Append(height);
break;
case TextureSizeMode.Functor:
if (func != null)
hashCode.Append(func);
break;
case TextureSizeMode.Scale:
hashCode.Append(scale);
break;
}

hashCode = hashCode * 23 + mipMapBias.GetHashCode();
hashCode = hashCode * 23 + slices;
hashCode = hashCode * 23 + (int)depthBufferBits;
hashCode = hashCode * 23 + (int)colorFormat;
hashCode = hashCode * 23 + (int)filterMode;
hashCode = hashCode * 23 + (int)wrapMode;
hashCode = hashCode * 23 + (int)dimension;
hashCode = hashCode * 23 + (int)memoryless;
hashCode = hashCode * 23 + anisoLevel;
hashCode = hashCode * 23 + (enableRandomWrite ? 1 : 0);
hashCode = hashCode * 23 + (useMipMap ? 1 : 0);
hashCode = hashCode * 23 + (autoGenerateMips ? 1 : 0);
hashCode = hashCode * 23 + (isShadowMap ? 1 : 0);
hashCode = hashCode * 23 + (bindTextureMS ? 1 : 0);
hashCode = hashCode * 23 + (useDynamicScale ? 1 : 0);
hashCode = hashCode * 23 + (int)msaaSamples;
hashCode.Append(mipMapBias);
hashCode.Append(slices);
hashCode.Append((int) depthBufferBits);
hashCode.Append((int) colorFormat);
hashCode.Append((int) filterMode);
hashCode.Append((int) wrapMode);
hashCode.Append((int) dimension);
hashCode.Append((int) memoryless);
hashCode.Append(anisoLevel);
hashCode.Append(enableRandomWrite);
hashCode.Append(useMipMap);
hashCode.Append(autoGenerateMips);
hashCode.Append(isShadowMap);
hashCode.Append(bindTextureMS);
hashCode.Append(useDynamicScale);
hashCode.Append((int) msaaSamples);
#if UNITY_2020_2_OR_NEWER
hashCode = hashCode * 23 + (fastMemoryDesc.inFastMemory ? 1 : 0);
hashCode.Append(fastMemoryDesc.inFastMemory);
#endif
}

return hashCode;
return hashCode.value;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Runtime.CompilerServices;

namespace UnityEngine.Rendering
{
internal ref struct HashFNV1A32
{
/// <summary>
/// FNV prime.
/// </summary>
const uint k_Prime = 16777619;

/// <summary>
/// FNV offset basis.
/// </summary>
const uint k_OffsetBasis = 2166136261;

uint m_Hash;

public static HashFNV1A32 Create()
{
return new HashFNV1A32 { m_Hash = k_OffsetBasis };
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in int input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in uint input)
{
unchecked
{
m_Hash = (m_Hash ^ input) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in bool input)
{
m_Hash = (m_Hash ^ (input ? 1u : 0u)) * k_Prime;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in float input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in double input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in Vector2 input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in Vector3 input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in Vector4 input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append<T>(T input) where T : struct
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(Delegate del)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)GetFuncHashCode(del)) * k_Prime;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int GetFuncHashCode(Delegate del)
{
return del.Method.GetHashCode() ^ RuntimeHelpers.GetHashCode(del.Target);
}

public int value => (int)m_Hash;

public override int GetHashCode()
{
return value;
}
}
}

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

Loading

0 comments on commit 358299b

Please sign in to comment.