Skip to content

Shader Graph CPU performance improvements #7655

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 @@ -844,7 +844,7 @@ void AddBlockNoValidate(BlockNode blockNode, ContextData contextData, int index)
blockNode.contextData = contextData;

// Add to ContextData
if (index == -1 || index >= contextData.blocks.Count())
if (index == -1 || index >= contextData.blocks.Count)
{
contextData.blocks.Add(blockNode);
}
Expand Down Expand Up @@ -2801,7 +2801,7 @@ public void OnDisable()

internal void ValidateCustomBlockLimit()
{
if (m_ActiveTargets.Count() == 0)
if (m_ActiveTargets.Count == 0)
return;

int nonCustomUsage = 0;
Expand All @@ -2814,7 +2814,7 @@ internal void ValidateCustomBlockLimit()
nonCustomUsage += 4;
else nonCustomUsage += bnode.descriptor.vectorCount;
}
int maxTargetUsage = m_ActiveTargets.Select(jt => jt.value.padCustomInterpolatorLimit).Max() * 4;
int maxTargetUsage = m_ActiveTargets.Max(jt => jt.value.padCustomInterpolatorLimit) * 4;

int padding = nonCustomUsage + maxTargetUsage;

Expand Down Expand Up @@ -2873,7 +2873,7 @@ void FindAndReportSlotErrors(MaterialSlot initialSlot, ShaderStage expectedShade
slotStack.Push(initialSlot);

// Trace back and find any edges that introduce an error
while (slotStack.Any())
while (slotStack.Count > 0)
{
var slot = slotStack.Pop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void DepthFirstCollectNodesFromNode(List<AbstractMaterialNode> nod
// The only valid port id is the port that corresponds to that keywords value in the active permutation
if (node is KeywordNode keywordNode && keywordPermutation != null)
{
var valueInPermutation = keywordPermutation.Where(x => x.Key == keywordNode.keyword).FirstOrDefault();
var valueInPermutation = keywordPermutation.FirstOrDefault(x => x.Key == keywordNode.keyword);
ids = new int[] { keywordNode.GetSlotIdForPermutation(valueInPermutation) };
}
else
Expand Down Expand Up @@ -439,7 +439,7 @@ public static ShaderStage GetEffectiveShaderStage(MaterialSlot initialSlot, bool
var graph = initialSlot.owner.owner;
s_SlotStack.Clear();
s_SlotStack.Push(initialSlot);
while (s_SlotStack.Any())
while (s_SlotStack.Count > 0)
{
var slot = s_SlotStack.Pop();
ShaderStage stage;
Expand Down Expand Up @@ -483,7 +483,7 @@ public static ShaderStageCapability GetEffectiveShaderStageCapability(MaterialSl
s_SlotStack.Clear();
s_SlotStack.Push(initialSlot);
ShaderStageCapability capabilities = ShaderStageCapability.All;
while (s_SlotStack.Any())
while (s_SlotStack.Count > 0)
{
var slot = s_SlotStack.Pop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ public AbstractShaderProperty GetShaderProperty(int id)
if (index >= 0)
{
var guid = m_PropertyGuids[index];
return asset?.inputs.Where(x => x.guid.ToString().Equals(guid)).FirstOrDefault();
return asset?.inputs.FirstOrDefault(x => x.guid.ToString().Equals(guid));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ internal static string DeduplicateName(IEnumerable<string> existingNames, string

var duplicateNumber = 1;
existingDuplicateNumbers.Sort();
if (existingDuplicateNumbers.Any() && existingDuplicateNumbers.First() == 1)
if (existingDuplicateNumbers.Count > 0 && existingDuplicateNumbers.First() == 1)
{
duplicateNumber = existingDuplicateNumbers.Last() + 1;
for (var i = 1; i < existingDuplicateNumbers.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override string title
set
{
m_TitleLabel.text = value;
if (m_TitleLabel.text == String.Empty)
if (m_TitleLabel.text?.Length == 0)
{
AddToClassList("unnamed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ internal SGBlackboardRow InsertBlackboardRow(BlackboardItem shaderInput, int ins
{
m_BlackboardItemControllers.Add(shaderInput.objectId, blackboardItemController);
// If no index specified, or if trying to insert at last index, add to end of category
if (insertionIndex == -1 || insertionIndex == m_BlackboardItemControllers.Count() - 1)
if (insertionIndex == -1 || insertionIndex == m_BlackboardItemControllers.Count - 1)
blackboardCategoryView.Add(blackboardItemController.BlackboardItemView);
else
blackboardCategoryView.Insert(insertionIndex, blackboardItemController.BlackboardItemView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void AddShaderInput(GraphData graphData)
graphData.AddGraphInput(shaderInputReference);

// If no categoryToAddItemToGuid is provided, add the input to the default category
if (categoryToAddItemToGuid == String.Empty)
if (categoryToAddItemToGuid?.Length == 0)
{
var defaultCategory = graphData.categories.FirstOrDefault();
AssertHelpers.IsNotNull(defaultCategory, "Default category reference is null.");
Expand Down Expand Up @@ -442,7 +442,7 @@ public string GetFirstSelectedCategoryGuid()
selectedCategories.Add(selectable as SGBlackboardCategory);
}
}
if (selectedCategories.Any())
if (selectedCategories.Count > 0)
{
selectedCategoryGuid = selectedCategories[0].viewModel.associatedCategoryGuid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,7 @@ public bool IsGraphAssetCheckedOut()
{
var path = AssetDatabase.GUIDToAssetPath(selectedGuid);
var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
if (!AssetDatabase.IsOpenForEdit(asset, StatusQueryOptions.UseCachedIfPossible))
return false;

return true;
return AssetDatabase.IsOpenForEdit(asset, StatusQueryOptions.UseCachedIfPossible);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,12 @@ public void DoRenderPreview(RenderTexture renderTexture, Material mat, Mesh mesh
m_Camera.targetTexture = renderTexture;
if (mode == PreviewMode.Preview3D)
{
m_Camera.transform.position = -Vector3.forward * 5;
m_Camera.transform.rotation = Quaternion.identity;
m_Camera.transform.SetPositionAndRotation(-Vector3.forward * 5, Quaternion.identity);
m_Camera.orthographic = false;
}
else
{
m_Camera.transform.position = -Vector3.forward * 2;
m_Camera.transform.rotation = Quaternion.identity;
m_Camera.transform.SetPositionAndRotation(-Vector3.forward * 2, Quaternion.identity);
m_Camera.orthographicSize = 1;
m_Camera.orthographic = true;
}
Expand Down
10 changes: 4 additions & 6 deletions Packages/com.unity.shadergraph/Editor/Drawing/PreviewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,17 +631,15 @@ public void RenderPreviews(EditorWindow editorWindow, bool requestShaders = true
m_SceneResources.camera.clearFlags = CameraClearFlags.Color;

// Render 2D previews
m_SceneResources.camera.transform.position = -Vector3.forward * 2;
m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.transform.SetPositionAndRotation(-Vector3.forward * 2, Quaternion.identity);
m_SceneResources.camera.orthographicSize = 0.5f;
m_SceneResources.camera.orthographic = true;

foreach (var renderData in renderList2D)
RenderPreview(renderData, m_SceneResources.quad, Matrix4x4.identity, perMaterialPreviewProperties);

// Render 3D previews
m_SceneResources.camera.transform.position = -Vector3.forward * 5;
m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.transform.SetPositionAndRotation(-Vector3.forward * 5, Quaternion.identity);
m_SceneResources.camera.orthographic = false;

foreach (var renderData in renderList3D)
Expand Down Expand Up @@ -670,7 +668,7 @@ public void RenderPreviews(EditorWindow editorWindow, bool requestShaders = true

var previewTransform = preventRotation ? Matrix4x4.identity : Matrix4x4.Rotate(m_Graph.previewData.rotation);
var scale = m_Graph.previewData.scale;
previewTransform *= Matrix4x4.Scale(scale * Vector3.one * (Vector3.one).magnitude / mesh.bounds.size.magnitude);
previewTransform *= Matrix4x4.Scale((Vector3.one).magnitude * scale * Vector3.one / mesh.bounds.size.magnitude);
previewTransform *= Matrix4x4.Translate(-mesh.bounds.center);

RenderPreview(masterRenderData, mesh, previewTransform, perMaterialPreviewProperties);
Expand Down Expand Up @@ -784,7 +782,7 @@ void KickOffShaderCompilations()

// add each node to compile list if it needs a preview, is not already compiling, and we have room
// (we don't want to double kick compiles, so wait for the first one to get back before kicking another)
for (int i = 0; i < m_PreviewsNeedsRecompile.Count(); i++)
for (int i = 0; i < m_PreviewsNeedsRecompile.Count; i++)
{
if (m_PreviewsCompiling.Count + previewsToCompile.Count >= m_MaxPreviewsCompiling)
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public bool OnSearcherSelectEntry(SearcherItem entry, Vector2 screenMousePositio
}
// Test against all current BlockNodes in the Context
// Never allow duplicate BlockNodes
else if (contextView.contextData.blocks.Where(x => x.value.name == blockNode.name).FirstOrDefault().value != null)
else if (contextView.contextData.blocks.FirstOrDefault(x => x.value.name == blockNode.name).value != null)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,12 @@ GraphViewChange GraphViewChanged(GraphViewChange graphViewChange)
if (groupNode == null)
continue;

foreach (GraphElement graphElement in groupNode.containedElements)
{
nodesInsideGroup.Add(graphElement);
}
nodesInsideGroup.AddRange(groupNode.containedElements);

SetGroupPosition(groupNode);
}

if (nodesInsideGroup.Any())
if (nodesInsideGroup.Count > 0)
graphViewChange.movedElements.AddRange(nodesInsideGroup);

foreach (var element in graphViewChange.movedElements)
Expand Down Expand Up @@ -1237,7 +1234,7 @@ void UpdateEdgeColors(HashSet<IShaderNodeView> nodeViews)
foreach (var nodeView in nodeViews)
nodeStack.Push((Node)nodeView);
PooledList<Edge> edgesToUpdate = PooledList<Edge>.Get();
while (nodeStack.Any())
while (nodeStack.Count > 0)
{
var nodeView = nodeStack.Pop();
if (nodeView is IShaderNodeView shaderNodeView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void Draw(CustomFunctionNode node)
});
m_FunctionName.RegisterCallback<FocusOutEvent>(s =>
{
if (m_FunctionName.value == "")
if (m_FunctionName.value?.Length == 0)
m_FunctionName.value = CustomFunctionNode.defaultFunctionName;
else
m_FunctionName.value = NodeUtils.ConvertToValidHLSLIdentifier(m_FunctionName.value);
Expand Down Expand Up @@ -92,7 +92,7 @@ private void Draw(CustomFunctionNode node)
});
m_FunctionBody.RegisterCallback<FocusOutEvent>(s =>
{
if (m_FunctionBody.value == "")
if (m_FunctionBody.value?.Length == 0)
m_FunctionBody.value = CustomFunctionNode.defaultFunctionBody;

if (m_FunctionBody.value != node.functionBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private void ReorderEntries(ReorderableList list)
// For each list entry get the slot with that ID
for (int i = 0; i < list.list.Count; i++)
{
var currentSlot = slots.Where(s => s.id == (int)list.list[i]).FirstOrDefault();
var currentSlot = slots.FirstOrDefault(s => s.id == (int)list.list[i]);
m_Node.AddSlot(currentSlot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public AdditionalCommandCollection()

public AdditionalCommandCollection Add(AdditionalCommandCollection fields)
{
foreach (AdditionalCommandCollection.Item item in fields)
{
m_Items.Add(item);
}
m_Items.AddRange(fields);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ public DefineCollection(DefineCollection defines)
{
m_Items = new List<Item>();

foreach (DefineCollection.Item item in defines)
{
m_Items.Add(item);
}
m_Items.AddRange(defines);
}

public DefineCollection Add(DefineCollection defines)
{
foreach (DefineCollection.Item item in defines)
{
m_Items.Add(item);
}
m_Items.AddRange(defines);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public DependencyCollection()

public DependencyCollection Add(DependencyCollection dependencies)
{
foreach (DependencyCollection.Item item in dependencies)
{
m_Items.Add(item);
}
m_Items.AddRange(dependencies);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public FieldCollection()

public FieldCollection Add(FieldCollection fields)
{
foreach (FieldCollection.Item item in fields)
{
m_Items.Add(item);
}
m_Items.AddRange(fields);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public KernelCollection()

public KernelCollection Add(KernelCollection passes)
{
foreach (KernelCollection.Item item in passes)
{
m_Items.Add(item);
}
m_Items.AddRange(passes);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public KeywordCollection()

public KeywordCollection Add(KeywordCollection keywords)
{
foreach (KeywordCollection.Item item in keywords)
{
m_Items.Add(item);
}
m_Items.AddRange(keywords);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public PassCollection()

public PassCollection Add(PassCollection passes)
{
foreach (PassCollection.Item item in passes)
{
m_Items.Add(item);
}
m_Items.AddRange(passes);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public PragmaCollection()

public PragmaCollection Add(PragmaCollection pragmas)
{
foreach (PragmaCollection.Item item in pragmas)
{
m_Items.Add(item);
}
m_Items.AddRange(pragmas);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public RenderStateCollection()

public RenderStateCollection Add(RenderStateCollection renderStates)
{
foreach (RenderStateCollection.Item item in renderStates)
{
m_Items.Add(item);
}
m_Items.AddRange(renderStates);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public StructCollection()

public StructCollection Add(StructCollection structs)
{
foreach (StructCollection.Item item in structs)
{
m_Items.Add(item);
}
m_Items.AddRange(structs);

return this;
}
Expand Down
Loading