Skip to content

Commit

Permalink
[Universal] Single CommandBuffer for URP (Unity-Technologies#6877)
Browse files Browse the repository at this point in the history
* making URP use a single cmd buffer for a single camera
  • Loading branch information
jonuuukas committed Feb 21, 2022
1 parent 6341538 commit da87443
Show file tree
Hide file tree
Showing 38 changed files with 128 additions and 193 deletions.
1 change: 1 addition & 0 deletions com.unity.render-pipelines.universal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added Downscale and Max Iterations options for Bloom
- Added default DOTS compatible loading shader (FallbackLoading.shader)
- Add #pragma editor_sync_compilation directive to FallbackError.shader
- Added commandBuffer variable to RenderingData struct and switched all of the renderer to use that buffer instead of creating local command buffers.

### Changed
- Re-added the menu button to be able to convert selected materials.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Setup(bool savedIsOrthographic, float savedOrthographicSize)

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;

using (new ProfilingScope(cmd, m_ProfilingScope))
{
Expand All @@ -35,10 +35,6 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
Color.black);
}


context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);

renderingData.cameraData.camera.orthographic = m_SavedIsOrthographic;
renderingData.cameraData.camera.orthographicSize = m_SavedOrthographicSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private void GetTransparencySortingMode(Camera camera, ref SortingSettings sorti

private void CopyCameraSortingLayerRenderTexture(ScriptableRenderContext context, RenderingData renderingData, RenderBufferStoreAction mainTargetStoreAction)
{
var cmd = CommandBufferPool.Get();
cmd.Clear();
var cmd = renderingData.commandBuffer;

this.CreateCameraSortingLayerRenderTexture(renderingData, cmd, m_Renderer2DData.cameraSortingLayerDownsamplingMethod);

Material copyMaterial = m_Renderer2DData.cameraSortingLayerDownsamplingMethod == Downsampling._4xBox ? m_SamplingMaterial : m_BlitMaterial;
Expand All @@ -88,7 +88,7 @@ private void CopyCameraSortingLayerRenderTexture(ScriptableRenderContext context
ClearFlag.None, Color.clear);
cmd.SetGlobalTexture(m_Renderer2DData.cameraSortingLayerRenderTargetId, m_Renderer2DData.cameraSortingLayerRenderTarget.nameID);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
cmd.Clear();
}

private short GetCameraSortingLayerBoundsIndex()
Expand Down Expand Up @@ -213,7 +213,7 @@ private int DrawLayerBatches(
{
filterSettings.sortingLayerRange = layerBatch.layerRange;
var depthTarget = m_NeedsDepth ? depthAttachmentHandle.nameID : BuiltinRenderTextureType.None;
this.RenderNormals(context, renderingData, normalsDrawSettings, filterSettings, depthTarget, cmd, layerBatch.lightStats);
this.RenderNormals(context, renderingData, normalsDrawSettings, filterSettings, depthTarget, layerBatch.lightStats);
}

using (new ProfilingScope(cmd, m_ProfilingDrawLightTextures))
Expand Down Expand Up @@ -368,7 +368,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
combinedDrawSettings.sortingSettings = sortSettings;
normalsDrawSettings.sortingSettings = sortSettings;

var cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
cmd.SetGlobalFloat(k_HDREmulationScaleID, m_Renderer2DData.hdrEmulationScale);
cmd.SetGlobalFloat(k_InverseHDREmulationScaleID, 1.0f / m_Renderer2DData.hdrEmulationScale);
cmd.SetGlobalFloat(k_UseSceneLightingID, isLitView ? 1.0f : 0.0f);
Expand All @@ -386,7 +386,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
this.DisableAllKeywords(cmd);
this.ReleaseRenderTextures(cmd);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
cmd.Clear();
}
else
{
Expand All @@ -398,7 +398,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
GetTransparencySortingMode(camera, ref sortSettings);
unlitDrawSettings.sortingSettings = sortSettings;

var cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSamplerUnlit))
{
CoreUtils.SetRenderTarget(cmd,
Expand All @@ -420,6 +420,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData

this.DisableAllKeywords(cmd);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();

Profiler.BeginSample("Render Sprites Unlit");
if (m_Renderer2DData.useCameraSortingLayerTexture)
Expand All @@ -437,8 +438,6 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
Render(context, cmd, ref renderingData, ref filterSettings, unlitDrawSettings);
}
Profiler.EndSample();

CommandBufferPool.Release(cmd);
}

filterSettings.sortingLayerRange = SortingLayerRange.all;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ public void Dispose()

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingScope))
{
CoreUtils.SetRenderTarget(cmd, m_UpscaleHandle,
RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
ClearFlag.None, Color.clear);
Blit(cmd, m_Source, m_UpscaleHandle);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,9 @@ public static void ClearDirtyLighting(this IRenderPass2D pass, CommandBuffer cmd
}
}

public static void RenderNormals(this IRenderPass2D pass, ScriptableRenderContext context, RenderingData renderingData, DrawingSettings drawSettings, FilteringSettings filterSettings, RenderTargetIdentifier depthTarget, CommandBuffer cmd, LightStats lightStats)
public static void RenderNormals(this IRenderPass2D pass, ScriptableRenderContext context, RenderingData renderingData, DrawingSettings drawSettings, FilteringSettings filterSettings, RenderTargetIdentifier depthTarget, LightStats lightStats)
{
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
// figure out the scale
Expand Down
6 changes: 3 additions & 3 deletions com.unity.render-pipelines.universal/Runtime/2D/Renderer2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public override void Setup(ScriptableRenderContext context, ref RenderingData re
{
stackHasPostProcess = stackHasPostProcess && DebugHandler.IsPostProcessingAllowed;
}
DebugHandler.Setup(context, ref cameraData);
DebugHandler.Setup(context, ref renderingData);
}

#if UNITY_EDITOR
Expand Down Expand Up @@ -206,14 +206,14 @@ public override void Setup(ScriptableRenderContext context, ref RenderingData re
RTHandle colorTargetHandle;
RTHandle depthTargetHandle;

CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
CreateRenderTextures(ref cameraData, ppcUsesOffscreenRT, colorTextureFilterMode, cmd,
out colorTargetHandle, out depthTargetHandle);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
cmd.Clear();

ConfigureCameraTarget(colorTargetHandle, depthTargetHandle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ internal void UpdateShaderGlobalPropertiesForFinalValidationPass(CommandBuffer c
}

[Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
internal void Setup(ScriptableRenderContext context, ref CameraData cameraData)
internal void Setup(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get("");
var cmd = renderingData.commandBuffer;
var cameraData = renderingData.cameraData;

if (IsActiveForCamera(ref cameraData))
{
Expand Down Expand Up @@ -288,7 +289,7 @@ internal void Setup(ScriptableRenderContext context, ref CameraData cameraData)
}

context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
cmd.Clear();
}

#region DebugRenderPasses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);

CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
context.ExecuteCommandBuffer(cmd);
Expand Down Expand Up @@ -152,8 +152,6 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData

context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

private void ClearDBuffers(CommandBuffer cmd, in CameraData cameraData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);

CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
context.ExecuteCommandBuffer(cmd);
cmd.Clear();

m_DrawSystem.Execute(cmd);

context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);

CommandBuffer cmd = CommandBufferPool.Get();
CommandBuffer cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
context.ExecuteCommandBuffer(cmd);
cmd.Clear();

context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);

CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
context.ExecuteCommandBuffer(cmd);
Expand All @@ -81,8 +81,6 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData

context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

public override void OnCameraCleanup(CommandBuffer cmd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
SortingCriteria sortingCriteria = SortingCriteria.CommonTransparent;
DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);

CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
context.ExecuteCommandBuffer(cmd);
Expand All @@ -59,8 +59,6 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData

context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

public override void OnCameraCleanup(CommandBuffer cmd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ internal void SetupLights(ScriptableRenderContext context, ref RenderingData ren
);

{
CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingSetupLightConstants))
{
// Shared uniform constants for all lights.
Expand All @@ -304,7 +304,7 @@ internal void SetupLights(ScriptableRenderContext context, ref RenderingData ren
}

context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
cmd.Clear();
}

Profiler.EndSample();
Expand Down Expand Up @@ -515,7 +515,7 @@ internal void ExecuteDeferredPass(ScriptableRenderContext context, ref Rendering
if (m_StencilDeferredPasses[0] < 0)
InitStencilDeferredMaterial();

CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(cmd, m_ProfilingDeferredPass))
{
// This does 2 things:
Expand All @@ -542,9 +542,6 @@ internal void ExecuteDeferredPass(ScriptableRenderContext context, ref Rendering
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.AdditionalLightShadows, renderingData.shadowData.isKeywordAdditionalLightShadowsEnabled);
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.SoftShadows, renderingData.shadowData.isKeywordSoftShadowsEnabled);
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.LightCookies, m_LightCookieManager.IsKeywordLightCookieEnabled);

context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

// adapted from ForwardLights.SetupShaderLightConstants
Expand Down
4 changes: 2 additions & 2 deletions com.unity.render-pipelines.universal/Runtime/ForwardLights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public void Setup(ScriptableRenderContext context, ref RenderingData renderingDa
{
int additionalLightsCount = renderingData.lightData.additionalLightsCount;
bool additionalLightsPerVertex = renderingData.lightData.shadeAdditionalLightsPerVertex;
CommandBuffer cmd = CommandBufferPool.Get();
var cmd = renderingData.commandBuffer;
using (new ProfilingScope(null, m_ProfilingSampler))
{
var useClusteredRendering = m_UseClusteredRendering;
Expand Down Expand Up @@ -385,7 +385,7 @@ public void Setup(ScriptableRenderContext context, ref RenderingData renderingDa
m_LightCookieManager.Setup(context, cmd, ref renderingData.lightData);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
cmd.Clear();
}

internal void Cleanup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ internal void ExecuteNativeRenderPass(ScriptableRenderContext context, Scriptabl

renderPass.Execute(context, ref renderingData);

// Need to execute it immediately to avoid sync issues between context and cmd buffer
context.ExecuteCommandBuffer(renderingData.commandBuffer);
renderingData.commandBuffer.Clear();

if (validPassCount == 1 || currentMergeablePasses[validPassCount - 1] == currentPassIndex) // Check if it's the last pass
{
context.EndSubPass();
Expand Down
Loading

0 comments on commit da87443

Please sign in to comment.