Skip to content

Remove automatic setup of global texture from Render Graph #1192

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

Merged
merged 10 commits into from
Jul 10, 2020
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
24 changes: 6 additions & 18 deletions com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class RenderGraphDebugParams
public bool tagResourceNamesWithRG;
public bool clearRenderTargetsAtCreation;
public bool clearRenderTargetsAtRelease;
public bool unbindGlobalTextures;
public bool logFrameInformation;
public bool logResources;

Expand All @@ -64,7 +63,6 @@ public void RegisterDebug()
list.Add(new DebugUI.BoolField { displayName = "Tag Resources with RG", getter = () => tagResourceNamesWithRG, setter = value => tagResourceNamesWithRG = value });
list.Add(new DebugUI.BoolField { displayName = "Clear Render Targets at creation", getter = () => clearRenderTargetsAtCreation, setter = value => clearRenderTargetsAtCreation = value });
list.Add(new DebugUI.BoolField { displayName = "Clear Render Targets at release", getter = () => clearRenderTargetsAtRelease, setter = value => clearRenderTargetsAtRelease = value });
list.Add(new DebugUI.BoolField { displayName = "Unbind Global Textures", getter = () => unbindGlobalTextures, setter = value => unbindGlobalTextures = value });
list.Add(new DebugUI.Button { displayName = "Log Frame Information", action = () => logFrameInformation = true });
list.Add(new DebugUI.Button { displayName = "Log Resources", action = () => logResources = true });

Expand Down Expand Up @@ -267,11 +265,10 @@ public void PurgeUnusedResources()
/// Any pass writing to an imported texture will be considered having side effects and can't be automatically pruned.
/// </summary>
/// <param name="rt">External RTHandle that needs to be imported.</param>
/// <param name="shaderProperty">Optional property that allows you to specify a Shader property name to use for automatic resource binding.</param>
/// <returns>A new TextureHandle.</returns>
public TextureHandle ImportTexture(RTHandle rt, int shaderProperty = 0)
public TextureHandle ImportTexture(RTHandle rt)
{
return m_Resources.ImportTexture(rt, shaderProperty);
return m_Resources.ImportTexture(rt);
}

/// <summary>
Expand All @@ -288,22 +285,20 @@ public TextureHandle ImportBackbuffer(RenderTargetIdentifier rt)
/// Create a new Render Graph Texture resource.
/// </summary>
/// <param name="desc">Texture descriptor.</param>
/// <param name="shaderProperty">Optional property that allows you to specify a Shader property name to use for automatic resource binding.</param>
/// <returns>A new TextureHandle.</returns>
public TextureHandle CreateTexture(in TextureDesc desc, int shaderProperty = 0)
public TextureHandle CreateTexture(in TextureDesc desc)
{
return m_Resources.CreateTexture(desc, shaderProperty);
return m_Resources.CreateTexture(desc);
}

/// <summary>
/// Create a new Render Graph Texture resource using the descriptor from another texture.
/// </summary>
/// <param name="texture">Texture from which the descriptor should be used.</param>
/// <param name="shaderProperty">Optional property that allows you to specify a Shader property name to use for automatic resource binding.</param>
/// <returns>A new TextureHandle.</returns>
public TextureHandle CreateTexture(TextureHandle texture, int shaderProperty = 0)
public TextureHandle CreateTexture(TextureHandle texture)
{
return m_Resources.CreateTexture(m_Resources.GetTextureResourceDesc(texture.handle), shaderProperty);
return m_Resources.CreateTexture(m_Resources.GetTextureResourceDesc(texture.handle));
}

/// <summary>
Expand Down Expand Up @@ -899,10 +894,6 @@ void PreRenderPassExecute(in CompiledPassInfo passInfo, RenderGraphContext rgCon
// TODO RENDERGRAPH merge clear and setup here if possible
RenderGraphPass pass = passInfo.pass;

// TODO RENDERGRAPH remove this when we do away with auto global texture setup
// (can't put it in the profiling scope otherwise it might be executed on compute queue which is not possible for global sets)
m_Resources.PreRenderPassSetGlobalTextures(rgContext, pass.resourceReadLists[(int)RenderGraphResourceType.Texture]);

foreach (var texture in passInfo.resourceCreateList[(int)RenderGraphResourceType.Texture])
m_Resources.CreateAndClearTexture(rgContext, texture);

Expand Down Expand Up @@ -944,9 +935,6 @@ void PostRenderPassExecute(CommandBuffer mainCmd, ref CompiledPassInfo passInfo,
rgContext.cmd = mainCmd; // Restore the main command buffer.
}

if (m_DebugParameters.unbindGlobalTextures)
m_Resources.PostRenderPassUnbindGlobalTextures(rgContext, pass.resourceReadLists[(int)RenderGraphResourceType.Texture]);

m_RenderGraphPool.ReleaseAllTempAlloc();

foreach (var texture in passInfo.resourceReleaseList[(int)RenderGraphResourceType.Texture])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public TextureHandle WriteTexture(in TextureHandle input)
/// <returns>A new transient TextureHandle.</returns>
public TextureHandle CreateTransientTexture(in TextureDesc desc)
{
var result = m_Resources.CreateTexture(desc, 0, m_RenderPass.index);
var result = m_Resources.CreateTexture(desc, m_RenderPass.index);
m_RenderPass.AddTransientResource(result.handle);
return result;
}
Expand All @@ -87,7 +87,7 @@ public TextureHandle CreateTransientTexture(in TextureDesc desc)
public TextureHandle CreateTransientTexture(in TextureHandle texture)
{
var desc = m_Resources.GetTextureResourceDesc(texture.handle);
var result = m_Resources.CreateTexture(desc, 0, m_RenderPass.index);
var result = m_Resources.CreateTexture(desc, m_RenderPass.index);
m_RenderPass.AddTransientResource(result.handle);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ class IRenderGraphResource
{
public bool imported;
public int cachedHash;
public int shaderProperty;
public int transientPassIndex;
public bool wasReleased;

public virtual void Reset()
{
imported = false;
cachedHash = -1;
shaderProperty = 0;
transientPassIndex = -1;
wasReleased = false;
}
Expand Down Expand Up @@ -209,12 +207,11 @@ internal int GetResourceTransientIndex(in ResourceHandle res)
}

// Texture Creation/Import APIs are internal because creation should only go through RenderGraph
internal TextureHandle ImportTexture(RTHandle rt, int shaderProperty = 0)
internal TextureHandle ImportTexture(RTHandle rt)
{
int newHandle = AddNewResource(m_Resources[(int)RenderGraphResourceType.Texture], out TextureResource texResource);
texResource.resource = rt;
texResource.imported = true;
texResource.shaderProperty = shaderProperty;

return new TextureHandle(newHandle);
}
Expand Down Expand Up @@ -246,13 +243,12 @@ internal TextureHandle ImportBackbuffer(RenderTargetIdentifier rt)
return result;
}

internal TextureHandle CreateTexture(in TextureDesc desc, int shaderProperty = 0, int transientPassIndex = -1)
internal TextureHandle CreateTexture(in TextureDesc desc, int transientPassIndex = -1)
{
ValidateTextureDesc(desc);

int newHandle = AddNewResource(m_Resources[(int)RenderGraphResourceType.Texture], out TextureResource texResource);
texResource.desc = desc;
texResource.shaderProperty = shaderProperty;
texResource.transientPassIndex = transientPassIndex;
return new TextureHandle(newHandle);
}
Expand Down Expand Up @@ -408,32 +404,6 @@ internal void CreateComputeBuffer(RenderGraphContext rgContext, int index)
}
}

void SetGlobalTextures(RenderGraphContext rgContext, List<ResourceHandle> textures, bool bindDummyTexture)
{
foreach (var resource in textures)
{
var resourceDesc = GetTextureResource(resource);
if (resourceDesc.shaderProperty != 0)
{
if (resourceDesc.resource != null)
{
rgContext.cmd.SetGlobalTexture(resourceDesc.shaderProperty, bindDummyTexture ? TextureXR.GetMagentaTexture() : resourceDesc.resource);
}
}
}
}


internal void PreRenderPassSetGlobalTextures(RenderGraphContext rgContext, List<ResourceHandle> textures)
{
SetGlobalTextures(rgContext, textures, false);
}

internal void PostRenderPassUnbindGlobalTextures(RenderGraphContext rgContext, List<ResourceHandle> textures)
{
SetGlobalTextures(rgContext, textures, true);
}

internal void ReleaseTexture(RenderGraphContext rgContext, int index)
{
var resource = m_Resources[(int)RenderGraphResourceType.Texture][index] as TextureResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ Shader "Hidden/HDRP/OpaqueAtmosphericScattering"
float2 positionSS = input.positionCS.xy;
float3 V = GetSkyViewDirWS(positionSS);
float depth = LoadCameraDepth(positionSS);
float3 surfColor = LOAD_TEXTURE2D_X(_ColorTexture, (int2)positionSS).rgb;

float3 volColor, volOpacity;
AtmosphericScatteringCompute(input, V, depth, volColor, volOpacity);
Expand All @@ -77,7 +76,6 @@ Shader "Hidden/HDRP/OpaqueAtmosphericScattering"
float2 positionSS = input.positionCS.xy;
float3 V = GetSkyViewDirWS(positionSS);
float depth = LOAD_TEXTURE2D_X_MSAA(_DepthTextureMS, (int2)positionSS, sampleIndex).x;
float3 surfColor = LOAD_TEXTURE2D_X_MSAA(_ColorTextureMS, (int2)positionSS, sampleIndex).rgb;

float3 volColor, volOpacity;
AtmosphericScatteringCompute(input, V, depth, volColor, volOpacity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ partial class AmbientOcclusionSystem
{
TextureHandle CreateAmbientOcclusionTexture(RenderGraph renderGraph)
{
return renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { enableRandomWrite = true, colorFormat = GraphicsFormat.R8_UNorm, name = "Ambient Occlusion" }, HDShaderIDs._AmbientOcclusionTexture);
return renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { enableRandomWrite = true, colorFormat = GraphicsFormat.R8_UNorm, name = "Ambient Occlusion" });
}

public TextureHandle Render(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle depthPyramid, TextureHandle normalBuffer, TextureHandle motionVectors, int frameCount, in HDUtils.PackedMipChainInfo depthMipInfo)
Expand All @@ -33,12 +33,12 @@ public TextureHandle Render(RenderGraph renderGraph, HDCamera hdCamera, TextureH
var aoParameters = PrepareRenderAOParameters(hdCamera, historySize * rtScaleForHistory, frameCount, depthMipInfo);

var packedData = RenderAO(renderGraph, aoParameters, depthPyramid, normalBuffer);
result = DenoiseAO(renderGraph, aoParameters, motionVectors, packedData, currentHistory, outputHistory);
result = DenoiseAO(renderGraph, aoParameters, depthPyramid, motionVectors, packedData, currentHistory, outputHistory);
}
}
else
{
result = renderGraph.ImportTexture(TextureXR.GetBlackTexture(), HDShaderIDs._AmbientOcclusionTexture);
result = renderGraph.ImportTexture(TextureXR.GetBlackTexture());
}
return result;
}
Expand Down Expand Up @@ -88,6 +88,7 @@ class DenoiseAOPassData

TextureHandle DenoiseAO( RenderGraph renderGraph,
in RenderAOParameters parameters,
TextureHandle depthTexture,
TextureHandle motionVectors,
TextureHandle aoPackedData,
TextureHandle currentHistory,
Expand Down Expand Up @@ -129,6 +130,7 @@ TextureHandle DenoiseAO( RenderGraph renderGraph,
data.packedDataBlurred,
data.currentHistory,
data.outputHistory,
data.motionVectors,
data.denoiseOutput,
ctx.cmd);
});
Expand All @@ -137,30 +139,32 @@ TextureHandle DenoiseAO( RenderGraph renderGraph,
return passData.denoiseOutput;
}

return UpsampleAO(renderGraph, parameters, denoiseOutput);
return UpsampleAO(renderGraph, parameters, denoiseOutput, depthTexture);
}

class UpsampleAOPassData
{
public RenderAOParameters parameters;
public TextureHandle depthTexture;
public TextureHandle input;
public TextureHandle output;
}

TextureHandle UpsampleAO(RenderGraph renderGraph, in RenderAOParameters parameters, TextureHandle input)
TextureHandle UpsampleAO(RenderGraph renderGraph, in RenderAOParameters parameters, TextureHandle input, TextureHandle depthTexture)
{
using (var builder = renderGraph.AddRenderPass<UpsampleAOPassData>("Upsample GTAO", out var passData, ProfilingSampler.Get(HDProfileId.UpSampleSSAO)))
{
builder.EnableAsyncCompute(parameters.runAsync);

passData.parameters = parameters;
passData.input = builder.ReadTexture(input);
passData.depthTexture = builder.ReadTexture(depthTexture);
passData.output = builder.WriteTexture(CreateAmbientOcclusionTexture(renderGraph));

builder.SetRenderFunc(
(UpsampleAOPassData data, RenderGraphContext ctx) =>
{
UpsampleAO(data.parameters, data.input, data.output, ctx.cmd);
UpsampleAO(data.parameters, data.depthTexture, data.input, data.output, ctx.cmd);
});

return passData.output;
Expand Down
Loading