Skip to content
Draft
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 @@ -154,6 +154,26 @@ public ref ResourceReaderData ResourceReader(in ResourceHandle h, int i)
// Data per native renderpas
public NativeList<NativePassData> nativePassData;
public NativeList<SubPassDescriptor> nativeSubPassData; //Tighty packed list of per nrp subpasses

public bool passMerged(int passId1, int passId2)
{
return passData[passId1].nativePassIndex == passData[passId2].nativePassIndex;
}

public bool nativepassFragmentsContain(int passId, int fragIndex)
{
int nativePassId = passData[passId].nativePassIndex;
if (nativePassId <= nativePassData.Length)
{
NativePassData data = nativePassData[nativePassId];
for (int i = 0; i < data.fragments.size; i++)
{
if (data.fragments[i].resource.index == fragIndex)
return true;
}
}
return false;
}

// resources can be added as fragment both as input and output so make sure not to add them twice (return true upon new addition)
public bool TryAddToFragmentList(in TextureAccess access, int listFirstIndex, int numItems, out string errorMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,13 @@ public void ExecuteGraph(InternalRenderGraphContext rgContext, RenderGraphResour
ExecuteBeginRenderPass(rgContext, resources, ref nativePass);
nrpBegan = true;
inRenderPass = true;

for (int subpassIndex = nativePass.firstGraphPass; subpassIndex <= nativePass.lastGraphPass; subpassIndex++)
{
ref var subpassData = ref contextData.passData.ElementAt(subpassIndex);
rgContext.executingPass = passes[subpassData.passId];
passes[subpassData.passId].PreExecute(rgContext);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ void SetRenderAttachmentDepth(TextureHandle tex, AccessFlags flags = AccessFlags
/// <param name="flags">How this pass will access the buffer. Default value is set to AccessFlag.Read.</param>
/// <returns>The value passed to 'input'. You should not use the returned value it will be removed in the future.</returns>
BufferHandle UseBufferRandomAccess(BufferHandle tex, int index, bool preserveCounterValue, AccessFlags flags = AccessFlags.Read);

/// <summary>
/// Specify the pre-render function to use for this pass.
/// The call happens when a native render pass starts
/// </summary>
/// <typeparam name="PassData">The Type of the class that provides data to the Render Pass.</typeparam>
/// <param name="renderFunc">Render function for the pass.</param>
public void SetPreRenderFunc<PassData>(BaseRenderFunc<PassData, RasterGraphContext> preRenderFunc)
where PassData : class, new();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ void ExecuteNativeRenderGraph()
nativeCompiler.ExecuteGraph(m_RenderGraphContext, m_Resources, m_RenderPasses);
}
}

public bool IsPassUsingRenderTarget(int passId, int resourceId)
{
return nativeCompiler.contextData.nativepassFragmentsContain(passId, resourceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public void FromInternalContext(InternalRenderGraphContext context)
rastercmd.m_ExecutingPass = context.executingPass;
cmd = rastercmd;
}

public int CurrentRGPassId() {
return cmd.m_ExecutingPass.index;
}

/// <inheritdoc />
public readonly TextureUVOrigin GetTextureUVOrigin(in TextureHandle textureHandle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public void SetGlobalTextureAfterPass(in TextureHandle input, int propertyId)

// Shared validation between SetRenderAttachment/SetRenderAttachmentDepth
[Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
private void CheckUseFragment(in TextureHandle tex, bool isDepth)
private void CheckUseFragment(in TextureHandle tex, bool isDepth, bool ignoreReadTextureCheck = false)
{
if (RenderGraph.enableValidityChecks)
{
Expand All @@ -362,12 +362,15 @@ private void CheckUseFragment(in TextureHandle tex, bool isDepth)
// SetRenderAttachment()
// UseTexture(grab)
// will work but not the other way around
for (int i = 0; i < m_RenderPass.resourceReadLists[tex.handle.iType].Count; i++)
if (!ignoreReadTextureCheck)
{
if (m_RenderPass.resourceReadLists[tex.handle.iType][i].index == tex.handle.index)
for (int i = 0; i < m_RenderPass.resourceReadLists[tex.handle.iType].Count; i++)
{
alreadyUsed = true;
break;
if (m_RenderPass.resourceReadLists[tex.handle.iType][i].index == tex.handle.index)
{
alreadyUsed = true;
break;
}
}
}

Expand Down Expand Up @@ -478,7 +481,12 @@ public void SetInputAttachment(TextureHandle tex, int index, AccessFlags flags,
{
CheckFrameBufferFetchEmulationIsSupported(tex);

CheckUseFragment(tex, false);
// Depth texture can be bind as input attachment, bypass the depth format check inside CheckUseFragment
m_Resources.GetRenderTargetInfo(tex.handle, out var info);
bool isDepth = GraphicsFormatUtility.IsDepthFormat(info.format);
// Bypass the already used check for texture read, so that an attachment can be used as depth and input in one subpass
bool ignoreReadTextureCheck = true;
CheckUseFragment(tex, isDepth, ignoreReadTextureCheck);
var versionedTextureHandle = new TextureHandle(UseResource(tex.handle, flags));
m_RenderPass.SetFragmentInputRaw(versionedTextureHandle, index, flags, mipLevel, depthSlice);
}
Expand Down Expand Up @@ -518,6 +526,11 @@ public BufferHandle UseBufferRandomAccess(BufferHandle input, int index, bool pr
m_RenderPass.SetRandomWriteResourceRaw(h.handle, index, preserveCounterValue, flags);
return input;
}

public void SetPreRenderFunc<PassData>(BaseRenderFunc<PassData, RasterGraphContext> preRenderFunc) where PassData : class, new()
{
((RasterRenderGraphPass<PassData>)m_RenderPass).preRenderFunc = preRenderFunc;
}

public void SetRenderFunc<PassData>(BaseRenderFunc<PassData, ComputeGraphContext> renderFunc) where PassData : class, new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace UnityEngine.Rendering.RenderGraphModule
[DebuggerDisplay("RenderPass: {name} (Index:{index} Async:{enableAsyncCompute})")]
abstract class RenderGraphPass
{
public virtual void PreExecute(InternalRenderGraphContext renderGraphContext) { return; }
public abstract void Execute(InternalRenderGraphContext renderGraphContext);
public abstract void Release(RenderGraphObjectPool pool);
public abstract bool HasRenderFunc();
Expand Down Expand Up @@ -701,6 +702,18 @@ internal sealed class RasterRenderGraphPass<PassData> : BaseRenderGraphPass<Pass
where PassData : class, new()
{
internal static RasterGraphContext c = new RasterGraphContext();
internal BaseRenderFunc<PassData, RasterGraphContext> preRenderFunc;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void PreExecute(InternalRenderGraphContext renderGraphContext)
{
if (preRenderFunc != null)
{
RasterGraphContext temp_c = new RasterGraphContext();
temp_c.FromInternalContext(renderGraphContext);
preRenderFunc(data, temp_c);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Execute(InternalRenderGraphContext renderGraphContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ internal TextureHandle(int handle, bool shared = false, bool builtin = false)
/// <param name="renderGraph">The rendergraph instance that was used to create the texture on. Texture handles are a lightweight object, all information is stored on the RenderGraph itself.</param>
/// <returns>The texture descriptor for the given texture handle.</returns>
public TextureDesc GetDescriptor(RenderGraph renderGraph) { return renderGraph.GetTextureDesc(this); }

public int GetResourceId() { return handle.index; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ private static RenderTextureDescriptor XrRenderTextureDescToUnityRenderTextureDe
rtDesc.volumeDepth = xrDesc.volumeDepth;
rtDesc.vrUsage = xrDesc.vrUsage;
rtDesc.sRGB = xrDesc.sRGB;
rtDesc.msaaSamples = xrDesc.msaaSamples;
rtDesc.shadowSamplingMode = xrDesc.shadowSamplingMode;
return rtDesc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal class Styles
public static GUIContent overrideDepth = new GUIContent("Depth", "Select this option to specify how this Renderer Feature affects or uses the values in the Depth buffer.");
public static GUIContent writeDepth = new GUIContent("Write Depth", "Choose to write depth to the screen.");
public static GUIContent depthState = new GUIContent("Depth Test", "Choose a new depth test function.");
public static GUIContent depthInput = new GUIContent("Depth Input", "Choose to bind depth as an input attachment.");

//Camera Settings
public static GUIContent overrideCamera = new GUIContent("Camera", "Override camera matrices. Toggling this setting will make camera use perspective projection.");
Expand Down Expand Up @@ -86,6 +87,7 @@ internal class Styles
private SerializedProperty m_OverrideDepth;
private SerializedProperty m_WriteDepth;
private SerializedProperty m_DepthState;
private SerializedProperty m_DepthInput;
//Stencil props
private SerializedProperty m_StencilSettings;
//Caemra props
Expand Down Expand Up @@ -140,6 +142,7 @@ private void Init(SerializedProperty property)
m_OverrideDepth = property.FindPropertyRelative("overrideDepthState");
m_WriteDepth = property.FindPropertyRelative("enableWrite");
m_DepthState = property.FindPropertyRelative("depthCompareFunction");
m_DepthInput = property.FindPropertyRelative("depthInput");

//Stencil
m_StencilSettings = property.FindPropertyRelative("stencilSettings");
Expand Down Expand Up @@ -278,6 +281,13 @@ void DoDepthOverride(ref Rect rect)
EditorGUI.indentLevel++;
//Write depth
EditorGUI.PropertyField(rect, m_WriteDepth, Styles.writeDepth);
rect.y += Styles.defaultLineSpace;
EditorGUI.PropertyField(rect, m_DepthInput, Styles.depthInput);
if (m_DepthInput.boolValue && m_WriteDepth.boolValue) {
Debug.LogWarning("Depth Input and Write Depth can't be used at the same time. Write Depth has been disabled.");
m_WriteDepth.boolValue = false;
}

rect.y += Styles.defaultLineSpace;
//Depth testing options
EditorGUI.PropertyField(rect, m_DepthState, Styles.depthState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
#endif

CoreUtils.SetRenderTarget(renderingData.commandBuffer, cameraTargetHandle.nameID, loadAction, RenderBufferStoreAction.Store, ClearFlag.None, Color.clear);
// The final blit can't be easily avoided for the logo screen when using HDR, manually correct the scale bias when using nrp with render graph
Vector4 scaleBias = RenderingUtils.GetFinalBlitScaleBias(m_Source, cameraTargetHandle, cameraData);
ExecutePass(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer), m_PassData, m_Source, cameraTargetHandle, cameraData, scaleBias);
cameraData.renderer.ConfigureCameraTarget(cameraTargetHandle, cameraTargetHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,11 @@ private class UberPostPassData
internal bool isBackbuffer;
internal bool enableAlphaOutput;
internal bool hasFinalPass;
internal bool isPassMerged;

// For handling yFlip
internal int backBufferResourceId;
internal Vector2Int cameraTargetSizeCopy;
}

TextureHandle TryGetCachedUserLutTextureHandle(RenderGraph renderGraph)
Expand Down Expand Up @@ -2526,9 +2531,25 @@ public void RenderUberPost(RenderGraph renderGraph, ContextContainer frameData,

builder.AllowGlobalStateModification(true);
passData.destinationTexture = destTexture;
builder.SetRenderAttachment(destTexture, 0, AccessFlags.Write);
passData.sourceTexture = sourceTexture;
builder.UseTexture(sourceTexture, AccessFlags.Read);
builder.SetRenderAttachment(destTexture, 0, AccessFlags.WriteAll);
bool tileCompatible = !(
m_Bloom.IsActive() ||
m_ChromaticAberration.IsActive() ||
m_DepthOfField.IsActive() ||
m_LensDistortion.IsActive() ||
m_MotionBlur.IsActive() ||
m_PaniniProjection.IsActive());
bool passMerged = SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan && tileCompatible;

if (passMerged)
{
builder.SetInputAttachment(sourceTexture, index: 0, AccessFlags.Read);
}
else
{
passData.sourceTexture = sourceTexture;
builder.UseTexture(sourceTexture, AccessFlags.Read);
}
passData.lutTexture = lutTexture;
builder.UseTexture(lutTexture, AccessFlags.Read);
passData.lutParams = lutParams;
Expand All @@ -2551,6 +2572,17 @@ public void RenderUberPost(RenderGraph renderGraph, ContextContainer frameData,
passData.isHdrGrading = hdrGrading;
passData.enableAlphaOutput = enableAlphaOutput;
passData.hasFinalPass = hasFinalPass;
passData.isPassMerged = passMerged;

// Reset the camera data in the pre-render function where we know if the native pass need y flip or not
passData.cameraTargetSizeCopy = new Vector2Int(passData.cameraData.cameraTargetDescriptor.width, passData.cameraData.cameraTargetDescriptor.height);
passData.backBufferResourceId = frameData.Get<UniversalResourceData>().backBufferColor.GetResourceId();

builder.SetPreRenderFunc((UberPostPassData data, RasterGraphContext context) =>
{
bool yFlip = !SystemInfo.graphicsUVStartsAtTop || (renderGraph.nativeRenderPassesEnabled && renderGraph.IsPassUsingRenderTarget(context.CurrentRGPassId(), data.backBufferResourceId));
data.cameraData.renderer.SetPerCameraShaderVariables(context.cmd, data.cameraData, data.cameraTargetSizeCopy, !yFlip);
});

builder.SetRenderFunc(static (UberPostPassData data, RasterGraphContext context) =>
{
Expand Down Expand Up @@ -2583,13 +2615,45 @@ public void RenderUberPost(RenderGraph renderGraph, ContextContainer frameData,
}

CoreUtils.SetKeyword(material, ShaderKeywordStrings._ENABLE_ALPHA_OUTPUT, data.enableAlphaOutput);
CoreUtils.SetKeyword(material, "SUBPASS_INPUT_ATTACHMENT", data.isPassMerged);

// Done with Uber, blit it
#if ENABLE_VR && ENABLE_XR_MODULE
if (data.cameraData.xr.enabled && data.cameraData.xr.hasValidVisibleMesh)
ScaleViewportAndDrawVisibilityMesh(in context, in data.sourceTexture, in data.destinationTexture, data.cameraData, material, data.hasFinalPass);
else
#endif
if (data.isPassMerged)
{
switch (data.cameraData.cameraTargetDescriptor.msaaSamples)
{
case 8:
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa2, false);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa4, false);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa8, true);
break;
case 4:
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa2, false);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa4, true);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa8, false);
break;
case 2:
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa2, true);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa4, false);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa8, false);
break;
default:
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa2, false);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa4, false);
CoreUtils.SetKeyword(material, ShaderKeywordStrings.Msaa8, false);
break;
}


Vector4 scaleBias = new Vector4(1, 1, 0, 0);
Blitter.BlitTexture(cmd, scaleBias, material, 0);
}
else
ScaleViewportAndBlit(in context, in data.sourceTexture, in data.destinationTexture, data.cameraData, material, data.hasFinalPass);

});
Expand Down
Loading