Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render graph subpass changes #41

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 @@ -314,7 +314,7 @@ public void SetGlobalTextureAfterPass(in TextureHandle input, int propertyId)

// Shared validation between SetRenderAttachment/SetRenderAttachmentDepth
[Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
private void CheckUseFragment(TextureHandle tex, bool isDepth)
private void CheckUseFragment(TextureHandle tex, bool isDepth, bool ignoreReadTextureCheck=false)
Theundeadwarrior marked this conversation as resolved.
Show resolved Hide resolved
{
if(RenderGraph.enableValidityChecks)
{
Expand All @@ -326,15 +326,18 @@ private void CheckUseFragment(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;
}
}
}

}
for (int i = 0; i < m_RenderPass.resourceWriteLists[tex.handle.iType].Count; i++)
{
if (m_RenderPass.resourceWriteLists[tex.handle.iType][i].index == tex.handle.index)
Expand Down Expand Up @@ -397,7 +400,12 @@ public void SetRenderAttachment(TextureHandle tex, int index, AccessFlags flags,

public void SetInputAttachment(TextureHandle tex, int index, AccessFlags flags, int mipLevel, int depthSlice)
{
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);
ResourceHandle result = UseResource(tex.handle, flags);
// Note the version for the attachments is a bit arbitrary so we just use the latest for now
// it doesn't really matter as it's really the Read/Write lists that determine that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,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 @@ -133,7 +133,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
#endif

CoreUtils.SetRenderTarget(renderingData.commandBuffer, m_InternalLut, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.None, Color.clear);
ExecutePass(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer), m_PassData, m_InternalLut);
ExecutePass(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer), m_PassData);
}

private class PassData
Expand All @@ -147,7 +147,7 @@ private class PassData
internal TextureHandle internalLut;
}

private static void ExecutePass(RasterCommandBuffer cmd, PassData passData, RTHandle internalLutTarget)
private static void ExecutePass(RasterCommandBuffer cmd, PassData passData)
{
var lutBuilderLdr = passData.lutBuilderLdr;
var lutBuilderHdr = passData.lutBuilderHdr;
Expand Down Expand Up @@ -269,7 +269,7 @@ private static void ExecutePass(RasterCommandBuffer cmd, PassData passData, RTHa
passData.cameraData.xr.StopSinglePass(cmd);

// Render the lut.
Blitter.BlitTexture(cmd, internalLutTarget, Vector2.one, material, 0);
Blitter.BlitTexture(cmd, Vector2.one, material, 0);

passData.cameraData.xr.StartSinglePass(cmd);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ internal void Render(RenderGraph renderGraph, ContextContainer frameData, out Te

builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
{
ExecutePass(context.cmd, data, data.internalLut);
ExecutePass(context.cmd, data);
});

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,7 @@ private class UberPostPassData
internal bool isBackbuffer;
internal bool enableAlphaOutput;
internal bool hasFinalPass;
internal bool isPassMerged;
}

TextureHandle TryGetCachedUserLutTextureHandle(RenderGraph renderGraph)
Expand Down Expand Up @@ -1870,8 +1871,26 @@ 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);

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 @@ -1893,6 +1912,7 @@ public void RenderUberPost(RenderGraph renderGraph, ContextContainer frameData,
passData.isHdrGrading = hdrGrading;
passData.enableAlphaOutput = enableAlphaOutput;
passData.hasFinalPass = hasFinalPass;
passData.isPassMerged = passMerged;

builder.SetRenderFunc(static (UberPostPassData data, RasterGraphContext context) =>
{
Expand Down Expand Up @@ -1922,8 +1942,42 @@ 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
ScaleViewportAndBlit(cmd, sourceTextureHdl, data.destinationTexture, data.cameraData, material, data.hasFinalPass);
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(cmd, sourceTextureHdl, data.destinationTexture, data.cameraData, material, data.hasFinalPass);
}
});

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class RenderObjectsPass : ScriptableRenderPass
FilteringSettings m_FilteringSettings;
RenderObjects.CustomCameraSettings m_CameraSettings;

private const string DEPTH_INPUT_ATTACHMENT = "_DEPTH_INPUT_ATTACHMENT";
private static GlobalKeyword m_depthInputKeyword;

/// <summary>
/// The override material to use.
Expand All @@ -37,6 +39,8 @@ public class RenderObjectsPass : ScriptableRenderPass
/// </summary>
public int overrideShaderPassIndex { get; set; }

public bool useDepthInputAttachment { get; set; } = false;

List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
private PassData m_PassData;

Expand Down Expand Up @@ -136,6 +140,8 @@ internal void Init(RenderPassEvent renderPassEvent, string[] shaderTags, RenderQ

m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
m_CameraSettings = cameraSettings;

m_depthInputKeyword = GlobalKeyword.Create(DEPTH_INPUT_ATTACHMENT);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -279,8 +285,17 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer

passData.color = resourceData.activeColorTexture;
builder.SetRenderAttachment(resourceData.activeColorTexture, 0, AccessFlags.Write);
builder.SetRenderAttachmentDepth(resourceData.activeDepthTexture, AccessFlags.Write);

if (useDepthInputAttachment && SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan)
{
builder.SetRenderAttachmentDepth(resourceData.activeDepthTexture, AccessFlags.Read);
builder.SetInputAttachment(resourceData.activeDepthTexture, index: 0, AccessFlags.Read);
}
else
{
builder.SetRenderAttachmentDepth(resourceData.activeDepthTexture, AccessFlags.Write);
}

TextureHandle mainShadowsTexture = resourceData.mainShadowsTexture;
TextureHandle additionalShadowsTexture = resourceData.additionalShadowsTexture;

Expand Down Expand Up @@ -320,6 +335,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer

builder.SetRenderFunc((PassData data, RasterGraphContext rgContext) =>
{
rgContext.cmd.SetKeyword(m_depthInputKeyword, useDepthInputAttachment && SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan);
var isYFlipped = data.cameraData.IsRenderTargetProjectionMatrixFlipped(data.color);
ExecutePass(data, rgContext.cmd, data.rendererListHdl, isYFlipped);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public enum OverrideMaterialMode
/// The camera settings to use.
/// </summary>
public CustomCameraSettings cameraSettings = new CustomCameraSettings();

/// <summary>
/// Sets whether it should has depth as input attachment or not.
/// </summary>
public bool depthInput = false;
}

/// <summary>
Expand Down Expand Up @@ -223,7 +228,10 @@ public override void Create()
}

if (settings.overrideDepthState)
{
renderObjectsPass.useDepthInputAttachment = settings.depthInput;
renderObjectsPass.SetDepthState(settings.enableWrite, settings.depthCompareFunction);
}

if (settings.stencilSettings.overrideStencilState)
renderObjectsPass.SetStencilState(settings.stencilSettings.stencilReference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ internal void SetupRenderGraphCameraProperties(RenderGraph renderGraph, bool isT

builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
{
bool yFlip = !SystemInfo.graphicsUVStartsAtTop || data.isTargetBackbuffer;
bool yFlip = !SystemInfo.graphicsUVStartsAtTop || data.isTargetBackbuffer || (renderGraph.nativeRenderPassesEnabled && SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan);

// This is still required because of the following reasons:
// - Camera billboard properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,15 @@ public static class ShaderKeywordStrings
/// <summary> Keyword used for Multi Sampling Anti-Aliasing (MSAA) with 8 per pixel sample count. </summary>
public const string DepthMsaa8 = "_DEPTH_MSAA_8";

/// <summary> Keyword used for Multi Sampling Anti-Aliasing (MSAA) with 2 per pixel sample count. </summary>
public const string Msaa2 = "_MSAA_2";

/// <summary> Keyword used for Multi Sampling Anti-Aliasing (MSAA) with 4 per pixel sample count. </summary>
public const string Msaa4 = "_MSAA_4";

/// <summary> Keyword used for Multi Sampling Anti-Aliasing (MSAA) with 8 per pixel sample count. </summary>
public const string Msaa8 = "_MSAA_8";

/// <summary> Keyword used for Linear to SRGB conversions. </summary>
public const string LinearToSRGBConversion = "_LINEAR_TO_SRGB_CONVERSION";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,9 @@ bool RequiresIntermediateColorTexture(UniversalCameraData cameraData, ref Render
int msaaSamples = cameraTargetDescriptor.msaaSamples;
bool isScaledRender = cameraData.imageScalingMode != ImageScalingMode.None;
bool isCompatibleBackbufferTextureDimension = cameraTargetDescriptor.dimension == TextureDimension.Tex2D;
bool requiresExplicitMsaaResolve = msaaSamples > 1 && PlatformRequiresExplicitMsaaResolve();
// In XR when the eye buffer have msaa auto resolve (the render target has msaa samples > 1), we don't need an explicit resolve.
// This will avoid the finalBlit pass when using MSAA.
bool requiresExplicitMsaaResolve = msaaSamples > 1 && !(cameraData.xr.enabled && cameraData.xr.renderTargetDesc.msaaSamples > 1) && PlatformRequiresExplicitMsaaResolve();
bool isOffscreenRender = cameraData.targetTexture != null && !isSceneViewCamera;
bool isCapturing = cameraData.captureActions != null;

Expand Down
Loading