Skip to content

URP Performance Improvements #7653

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -42,7 +42,7 @@ internal static void Place(GameObject go, GameObject parentTransform)
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
go.layer = parentTransform.gameObject.layer;
go.layer = parentTransform.layer;

if (parentTransform.GetComponent<RectTransform>())
ObjectFactory.AddComponent<RectTransform>(go);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static internal void GUITextureCap(int controlID, Texture texture, Vector3 posit
{
case (EventType.Layout):
{
Vector2 size2 = Vector2.one * size * 0.5f;
Vector2 size2 = 0.5f * size * Vector2.one;
if (isAngleHandle)
size2.x = 0.0f;

Expand All @@ -32,7 +32,7 @@ static internal void GUITextureCap(int controlID, Texture texture, Vector3 posit
float w = texture.width;
float h = texture.height;
float max = Mathf.Max(w, h);
Vector3 scale = new Vector2(w / max, h / max) * size * 0.5f;
Vector3 scale = 0.5f * size * new Vector2(w / max, h / max);

if (Camera.current == null)
scale.y *= -1f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private VisualElement GetInfoView()

private void ViewBatch(int index)
{
if (index >= batchList.Count())
if (index >= batchList.Count)
return;

var infoView = GetInfoView();
Expand All @@ -150,7 +150,7 @@ private void ViewBatch(int index)
var lightLabel1 = infoView.Query<Label>("LightLabel1").First();
lightLabel1.text = $"Lights in <b>Batch {batch1.batchId}:</b>";

if (batch1.Lights.Count() == 0)
if (batch1.Lights.Count == 0)
lightLabel1.text += "\n\nNo lights found.";

var lightBubble1 = infoView.Query<VisualElement>("LightBubble1").First();
Expand All @@ -172,7 +172,7 @@ private void ViewBatch(int index)
var shadowLabel1 = infoView.Query<Label>("ShadowLabel1").First();
shadowLabel1.text = $"Shadow Casters in <b>Batch {batch1.batchId}:</b>";

if (batch1.Shadows.Count() == 0)
if (batch1.Shadows.Count == 0)
shadowLabel1.text += "\n\nNo shadow casters found.";

var shadowBubble1 = infoView.Query<VisualElement>("ShadowBubble1").First();
Expand Down Expand Up @@ -305,7 +305,7 @@ private void CreateGUI()

Action<VisualElement, int> bindItem = (e, i) =>
{
if (i >= batchList.Count())
if (i >= batchList.Count)
return;

// This is required to make the child of the ListView vary in heights
Expand Down Expand Up @@ -456,14 +456,14 @@ private bool IsDirty()
bool isDirty = false;

// Refresh if layers are added or removed
isDirty |= Light2DManager.GetCachedSortingLayer().Count() != batchList.Sum(x => x.LayerNames.Count());
isDirty |= Light2DManager.GetCachedSortingLayer().Length != batchList.Sum(x => x.LayerNames.Count);
isDirty |= cachedSceneHandle != SceneManager.GetActiveScene().handle;
isDirty |= cachedCamPos != Camera.main?.transform.position;

if (lightCullResult != null)
{
isDirty |= totalLightCount != lightCullResult.visibleLights.Count();
isDirty |= totalShadowCount != lightCullResult.visibleShadows.Count();
isDirty |= totalLightCount != lightCullResult.visibleLights.Count;
isDirty |= totalShadowCount != lightCullResult.visibleShadows.Count;
}

return isDirty;
Expand All @@ -478,8 +478,8 @@ private void ResetDirty()

if (lightCullResult != null)
{
totalLightCount = lightCullResult.visibleLights.Count();
totalShadowCount = lightCullResult.visibleShadows.Count();
totalLightCount = lightCullResult.visibleLights.Count;
totalShadowCount = lightCullResult.visibleShadows.Count;
}

doRefresh = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ internal class ShadowCasterPath : ScriptablePath
internal Bounds GetBounds()
{
ShadowCaster2D shadowCaster = (ShadowCaster2D)owner;
Renderer m_Renderer = shadowCaster.GetComponent<Renderer>();
if (m_Renderer != null)
if (shadowCaster.TryGetComponent<Renderer>(out var m_Renderer))
{
return m_Renderer.bounds;
}
else
{
Collider2D collider = shadowCaster.GetComponent<Collider2D>();
if (collider != null)
if (shadowCaster.TryGetComponent<Collider2D>(out var collider))
return collider.bounds;
}

Expand Down Expand Up @@ -110,8 +108,7 @@ public bool HasRenderer()
for (int i = 0; i < targets.Length; i++)
{
ShadowCaster2D shadowCaster = (ShadowCaster2D)targets[i];
Renderer renderer = shadowCaster.GetComponent<Renderer>();
if (renderer != null)
if (shadowCaster.TryGetComponent<Renderer>(out var renderer))
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class BezierUtility
public static Vector3 BezierPoint(Vector3 startPosition, Vector3 startTangent, Vector3 endTangent, Vector3 endPosition, float t)
{
float s = 1.0f - t;
return startPosition * s * s * s + startTangent * s * s * t * 3.0f + endTangent * s * t * t * 3.0f + endPosition * t * t * t;
return s * s * s * startPosition + 3.0f * s * s * t * startTangent + 3.0f * s * t * t * endTangent + t * t * t * endPosition;
}

public static Vector3 ClosestPointOnCurve(Vector3 point, Vector3 startPosition, Vector3 endPosition, Vector3 startTangent, Vector3 endTangent, out float t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ SerializedShaderPropertyUsage filterFlags
}

// if any scenes should be considered, do the same for clips used by scenes
if (scenePaths.Any())
if (scenePaths.Length > 0)
{
GetClipDependencyMappings(clipPaths, scenePaths, out var clipSceneDependents, out var sceneDependencies);
GatherClipsUsageInDependentScenes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MaterialModificationProcessor : AssetModificationProcessor
{
static void OnWillCreateAsset(string asset)
{
if (!asset.ToLowerInvariant().EndsWith(".mat"))
if (!asset.EndsWith(".mat", StringComparison.InvariantCultureIgnoreCase))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static void RunInBatchMode(ConverterContainerId containerName, List<Conve
}
}

if (containerType != null && converterTypes.Any())
if (containerType != null && converterTypes.Count > 0)
{
RunInBatchMode(containerType, converterTypes, converterFilter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static IEnumerable<PropertyInfo> GetMaterialPropertiesWithoutLeaking(this

// if there is a sharedMaterial property or sharedMaterials property, remove the property that will leak materials
var sharedMaterialProps =
materialProps.Where(prop => prop.Name.ToLowerInvariant().Contains("shared")).ToList();
materialProps.Where(prop => prop.Name.Contains("shared", StringComparison.InvariantCultureIgnoreCase)).ToList();

var propsToRemove = sharedMaterialProps
.Select(prop => prop.Name.ToLowerInvariant().Replace("shared", string.Empty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void OnEnable()

void InitIfNeeded()
{
if (m_CoreConvertersList.Any())
if (m_CoreConvertersList.Count > 0)
return;
m_CoreConvertersList = new List<RenderPipelineConverter>();

Expand All @@ -174,7 +174,7 @@ void InitIfNeeded()
m_ContainerChoices.Add(container.name);
}

if (m_ConverterContainers.Any())
if (m_ConverterContainers.Count > 0)
{
GetConverters();
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public void CreateGUI()
string theme = EditorGUIUtility.isProSkin ? "dark" : "light";
InitIfNeeded();

if (m_ConverterContainers.Any())
if (m_ConverterContainers.Count > 0)
{
m_SerializedObject = new SerializedObject(this);
converterEditorAsset.CloneTree(rootVisualElement);
Expand Down Expand Up @@ -285,7 +285,7 @@ void InitOrConvert()
// If not then Init Button should be active
// Get all active converters

if (m_ConverterStates.Any())
if (m_ConverterStates.Count > 0)
{
foreach (ConverterState state in m_ConverterStates)
{
Expand Down Expand Up @@ -385,7 +385,7 @@ void ToggleAllNone(ClickEvent evt, int index, bool value, VisualElement item)

void ConverterStatusInfo(int index, VisualElement item)
{
Tuple<string, Texture2D> info = converterStateInfoDisabled;;
Tuple<string, Texture2D> info = converterStateInfoDisabled;
// Check if it is active
if (m_ConverterStates[index].isActive)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused)

EditorGUI.PropertyField(rect, element, EditorGUIUtility.TrTextContent($"Layer {index}"), true);

if (element.stringValue == "")
if (element.stringValue?.Length == 0)
{
element.stringValue = GetDefaultLayerName(index);
serializedObject.ApplyModifiedProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,8 @@ public bool StripMultiCompile(in LocalKeyword kw, T feature, in LocalKeyword kw2
bool containsKeywords = ContainsKeyword(kw) && ContainsKeyword(kw2) && ContainsKeyword(kw3);
bool keywordsDisabled = !m_KeywordSet.IsEnabled(kw) && !m_KeywordSet.IsEnabled(kw2) && !m_KeywordSet.IsEnabled(kw3);
bool hasAnyFeatureEnabled = m_Features.HasFlag(feature) || m_Features.HasFlag(feature2) || m_Features.HasFlag(feature3);
if (m_stripUnusedVariants && containsKeywords && keywordsDisabled && hasAnyFeatureEnabled)
return true;

return false;
return m_stripUnusedVariants && containsKeywords && keywordsDisabled && hasAnyFeatureEnabled;
}

public bool StripMultiCompileKeepOffVariant(in LocalKeyword kw, T feature, in LocalKeyword kw2, T feature2)
Expand All @@ -331,10 +329,8 @@ public bool StripMultiCompile(in LocalKeyword kw, T feature, in LocalKeyword kw2
bool containsKeywords = ContainsKeyword(kw) && ContainsKeyword(kw2);
bool keywordsDisabled = !m_KeywordSet.IsEnabled(kw) && !m_KeywordSet.IsEnabled(kw2);
bool hasAnyFeatureEnabled = m_Features.HasFlag(feature) || m_Features.HasFlag(feature2);
if (m_stripUnusedVariants && containsKeywords && keywordsDisabled && hasAnyFeatureEnabled)
return true;

return false;
return m_stripUnusedVariants && containsKeywords && keywordsDisabled && hasAnyFeatureEnabled;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -690,10 +686,7 @@ bool StripUnsupportedVariants(ShaderCompilerData compilerData)
}

// Editor visualization is only used in scene view debug modes.
if (compilerData.shaderKeywordSet.IsEnabled(m_EditorVisualization))
return true;

return false;
return compilerData.shaderKeywordSet.IsEnabled(m_EditorVisualization);
}

bool StripInvalidVariants(ShaderCompilerData compilerData)
Expand All @@ -708,10 +701,8 @@ bool StripInvalidVariants(ShaderCompilerData compilerData)
return true;

bool isShadowVariant = isMainShadow || isAdditionalShadow;
if (!isShadowVariant && compilerData.shaderKeywordSet.IsEnabled(m_SoftShadows))
return true;

return false;
return !isShadowVariant && compilerData.shaderKeywordSet.IsEnabled(m_SoftShadows);
}

bool StripUnusedShaders(ShaderFeatures features, Shader shader)
Expand Down Expand Up @@ -748,12 +739,9 @@ bool StripUnused(ShaderFeatures features, Shader shader, ShaderSnippetData snipp
// Strip terrain holes
// TODO: checking for the string name here is expensive
// maybe we can rename alpha clip keyword name to be specific to terrain?
if (compilerData.shaderKeywordSet.IsEnabled(m_AlphaTestOn) &&
return compilerData.shaderKeywordSet.IsEnabled(m_AlphaTestOn) &&
!IsFeatureEnabled(features, ShaderFeatures.TerrainHoles) &&
shader.name.Contains(kTerrainShaderName))
return true;

return false;
shader.name.Contains(kTerrainShaderName);
}

public bool active => UniversalRenderPipeline.asset != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ out string newPropertyName
).ToList();

// if there are any, assume the material has been upgraded
if (matchingUpgraders.Any())
if (matchingUpgraders.Count > 0)
{
result |= SerializedShaderPropertyUsage.UsedByUpgraded;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ private void AdjustUVRect(ref Vector4 uvScaleOffset, Texture cookie, ref Vector2
// Payload texture is inset
var potAtlas = (m_AdditionalLightsCookieAtlas as PowerOfTwoTextureAtlas);
var mipPadding = potAtlas == null ? 1 : potAtlas.mipPadding;
var paddingSize = Vector2.one * (int)Mathf.Pow(2, mipPadding) * 2;
var paddingSize = (int)Mathf.Pow(2, mipPadding) * 2 * Vector2.one;
uvScaleOffset = PowerOfTwoTextureAtlas.GetPayloadScaleOffset(cookieSize, paddingSize, uvScaleOffset);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ internal void SetNativeRenderPassMRTAttachmentList(ScriptableRenderPass renderPa

bool IsDepthOnlyRenderTexture(RenderTexture t)
{
if (t.graphicsFormat == GraphicsFormat.None)
return true;
return false;
return t.graphicsFormat == GraphicsFormat.None;
}

internal void SetNativeRenderPassAttachmentList(ScriptableRenderPass renderPass, ref CameraData cameraData, RTHandle passColorAttachment, RTHandle passDepthAttachment, ClearFlag finalClearFlag, Color finalClearColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public DrawObjectsPass(string profilerTag, ShaderTagId[] shaderTagIds, bool opaq
m_PassData = new PassData();
m_ProfilerTag = profilerTag;
m_ProfilingSampler = new ProfilingSampler(profilerTag);
foreach (ShaderTagId sid in shaderTagIds)
m_ShaderTagIdList.Add(sid);
m_ShaderTagIdList.AddRange(shaderTagIds);
renderPassEvent = evt;
m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public PostProcessPass(RenderPassEvent evt, PostProcessData data, ref PostProces
m_Materials = new MaterialLibrary(data);

// Only two components are needed for edge render texture, but on some vendors four components may be faster.
if (SystemInfo.IsFormatSupported(GraphicsFormat.R8G8_UNorm, FormatUsage.Render) && SystemInfo.graphicsDeviceVendor.ToLowerInvariant().Contains("arm"))
if (SystemInfo.IsFormatSupported(GraphicsFormat.R8G8_UNorm, FormatUsage.Render) && SystemInfo.graphicsDeviceVendor.Contains("arm", System.StringComparison.InvariantCultureIgnoreCase))
m_SMAAEdgeFormat = GraphicsFormat.R8G8_UNorm;
else
m_SMAAEdgeFormat = GraphicsFormat.R8G8B8A8_UNorm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ internal static class SceneViewDrawMode

static bool RejectDrawMode(SceneView.CameraMode cameraMode)
{
if (cameraMode.drawMode == DrawCameraMode.ShadowCascades ||
cameraMode.drawMode == DrawCameraMode.RenderPaths ||
cameraMode.drawMode == DrawCameraMode.AlphaChannel ||
cameraMode.drawMode == DrawCameraMode.Overdraw ||
cameraMode.drawMode == DrawCameraMode.Mipmaps ||
cameraMode.drawMode == DrawCameraMode.SpriteMask ||
cameraMode.drawMode == DrawCameraMode.DeferredDiffuse ||
cameraMode.drawMode == DrawCameraMode.DeferredSpecular ||
cameraMode.drawMode == DrawCameraMode.DeferredSmoothness ||
cameraMode.drawMode == DrawCameraMode.DeferredNormal ||
cameraMode.drawMode == DrawCameraMode.ValidateAlbedo ||
cameraMode.drawMode == DrawCameraMode.ValidateMetalSpecular ||
cameraMode.drawMode == DrawCameraMode.TextureStreaming
)
return false;

return true;
return cameraMode.drawMode != DrawCameraMode.ShadowCascades &&
cameraMode.drawMode != DrawCameraMode.RenderPaths &&
cameraMode.drawMode != DrawCameraMode.AlphaChannel &&
cameraMode.drawMode != DrawCameraMode.Overdraw &&
cameraMode.drawMode != DrawCameraMode.Mipmaps &&
cameraMode.drawMode != DrawCameraMode.SpriteMask &&
cameraMode.drawMode != DrawCameraMode.DeferredDiffuse &&
cameraMode.drawMode != DrawCameraMode.DeferredSpecular &&
cameraMode.drawMode != DrawCameraMode.DeferredSmoothness &&
cameraMode.drawMode != DrawCameraMode.DeferredNormal &&
cameraMode.drawMode != DrawCameraMode.ValidateAlbedo &&
cameraMode.drawMode != DrawCameraMode.ValidateMetalSpecular &&
cameraMode.drawMode != DrawCameraMode.TextureStreaming;
}

static void UpdateSceneViewStates()
Expand Down