Skip to content

Conversation

ludovic-theobald
Copy link
Contributor

@ludovic-theobald ludovic-theobald commented Jul 13, 2021

[VFX] Additional Sorting Keys

Checklist for PR maker

  • Have you added a backport label (if needed)? For example, the need-backport-* label. After you backport the PR, the label changes to backported-*.
  • Have you updated the changelog? Each package has a CHANGELOG.md file.
  • Have you updated or added the documentation for your PR? When you add a new feature, change a property name, or change the behavior of a feature, it's best practice to include related documentation changes in the same PR. If you do add documentation, make sure to add the relevant Graphics Docs team member as a reviewer of the PR. If you are not sure which person to add, see the Docs team contacts sheet.
  • Have you added a graphic test for your PR (if needed)? When you add a new feature, or discover a bug that tests don't cover, please add a graphic test.

Purpose of this PR

This PR introduces new criteria to sort the particles of an output context. Prior to this PR, if sorting was enabled, particles were sorted by their distance to the camera. We now propose a total of 5 sorting criteria :

  • By Distance (previously, the only option)
  • Youngest in front
  • Oldest in front
  • By Depth
  • Custom
    • the Custom criterion creates a new slot in the Output context, where the user can plug a sorting key which will be used to sort the particles.
      SortKeySlot

The sorting criterion can be chosen in the inspector if the Sort is set to On or Auto.
SortCriterionMenu
FilteredOut_SortCriterionMenu

Notes:

  • We cannot sort particles from different outputs, meaning that if you have two outputs to a system, one will appear completely in front or completely behind, depending on their sort priority.
  • When possible (outputs sharing the same sort criterion), only one global sort is issued for all the outputs. If outputs have different sorting criteria, they will require their own sort pass, incurring an additional cost in performance.
  • This bug causing flickering when there are more than 4096 particles is fixed in this PR.

JIRA story

This PR required modification of the C++ code.

Link to C++ PR : https://ono.unity3d.com/unity/unity/pull-request/130921/_/graphics/vfx/feature/new-sortkeys


Testing status

What has been tested :

  • Sorting 1 output (simple quads) with all the sorting criterion
  • Sorting up to 4 outputs, with heterogeneous sorting criteria
  • Sorting two multimesh outputs
  • Only in HDRP (but shouldn't be dependant on the SRP)

What hasn't been tested :

  • Different platforms
  • Extensive test on the custom sort key slot (e.g. will it break with some inputs?)
  • Upgrading from an older version

I plan to add a graphics test for this feature too.


Comments to reviewers

@github-actions
Copy link

github-actions bot commented Jul 13, 2021

Hi! This comment will help you figure out which jobs to run before merging your PR. The suggestions are dynamic based on what files you have changed.
Link to Yamato: https://yamato.cds.internal.unity3d.com/jobs/902-Graphics
Search for your PR branch using the sidebar on the left, then add the following segment(s) to the end of the URL (you may need multiple tabs depending on how many packages you change)

HDRP
/.yamato%252Fall-hdrp.yml%2523PR_HDRP_2021.2

VFX
/.yamato%252Fall-vfx.yml%2523PR_VFX_2021.2

Depending on the scope of your PR, you may need to run more jobs than what has been suggested. Please speak to your lead or a Graphics SDET (#devs-graphics-automation) if you are unsure.

@ludovic-theobald ludovic-theobald requested review from a team and julienf-unity July 13, 2021 09:21
@VladNeykov VladNeykov requested review from VladNeykov and removed request for a team July 13, 2021 09:31
@ludovic-theobald ludovic-theobald requested a review from peeweek July 13, 2021 11:41
@ludovic-theobald ludovic-theobald marked this pull request as ready for review July 26, 2021 07:25
@VladNeykov
Copy link
Contributor

Hi, dropping the WIP Test Doc with a few items already that can be looked at.

Copy link
Contributor

@julienf-unity julienf-unity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In progress

// Select left or right: Must handle special case for equality
bool select0 = (key0 == key1 && (id & 1)) || (CompareKeys(key0,key1) != reverse);
uint value = inputSequence[(select0 ? index0 : index1)].value;
bool select0 = (kvp0.key == kvp1.key && kvp0.value == kvp1.value && (id & 1)) || (CompareKVP(kvp0, kvp1) != reverse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first condition does not seem relevant anymore. CompareKVP test seems enough

@ludovic-theobald ludovic-theobald requested a review from a team as a code owner August 18, 2021 09:04
@ludovic-theobald
Copy link
Contributor Author

any update on this PR?

C++ side of the PR is currently being reviewed

I've made some changes to the new sections. Please double-check for accuracy.
Copy link
Contributor

@Vic-Cooper Vic-Cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve my own changes :D

return;


#if VFX_FEATURE_SORT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldnt all this be factorized in some way with what's in GlobalSortKey.template?

var sortKeyExp = m_Output.inputSlots.First(s => s.name == "sortKey").GetExpression();
expressionMapper.AddExpression(sortKeyExp, "sortKey", -1);
}
catch (Exception e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt this try catch just a temp debug things?

var implicitContext = new List<VFXContext>();
if (NeedsGlobalSort())
bool needsGlobalSort = NeedsGlobalSort();
//Issues a global sort, when it affects at least one output.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt the global sort be used when it affects at least 2 outputs, for 1 output using UpdateOutput pass seems better if any as it potentially removes 1 dispatch (+ takes into account attribute modification in output if any)

@PaulDemeulenaere PaulDemeulenaere self-requested a review November 2, 2021 10:51
Copy link
Contributor

@PaulDemeulenaere PaulDemeulenaere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving but only because I'm following the landing of this PR (see #6172)

@ludovic-theobald ludovic-theobald merged commit f4079dc into master Nov 4, 2021
@ludovic-theobald ludovic-theobald deleted the vfx/feature/new-sorting-keys branch November 4, 2021 07:24
PaulDemeulenaere added a commit that referenced this pull request Nov 4, 2021
Needed after merge of #5125
alelievr pushed a commit that referenced this pull request Nov 4, 2021
* renaming/shader code for built-in sort keys

* New task types + sorting logic + sort with value

* Vote to choose the global sorting (wip)

* Also compare the custom slot if needs be

* Fix binding buffer issues

* Fix needsOwnSort Condition

* use needsOwnSort for MeshOutputs

* Reformatting

* Sort order consistent with other sortings

* Warnings for changes in output

* New graphics test

* Always add isPerCameraSort mapping

* sort mode instead of criterion + tooltips

* small renaming

* remove useless condition + cleaner ternary op

* restore ternary operator (error on user defined types)

* local reformatting

* fix bad merge

* Fix naming in filteredSettings + strips case

* change feedback text to workaround layout issue

* Wrongly public method

* Error feedback only when block is enabled

* Fix Output ParticleStrip having sort available when created

* Update VFX to correct criterion

* Criteria renaming (explicit "ToCamera")

* Cleanup voting mechanism

* more cleanup

* fix missing comparer

* expose the option (does nothing)

* little cleaning

* moved helper function

* EqualityComparer instead of the interface

* Unify sorting criteria

* Simplify logic

* keep SortKeySlotComparer internal

* Reuse SortingCriteriaComparer for needOwnSort

* Activate inversion in shader

* Activate inversion in shader (bis)

* reformat file

* Remove OldestInFront

* Forces Per Output update when there is no Update but Sorting is on

* Shut down intermediate return (buggy) warning

* Warning when age is not set

* Add reference images

* Add reference image + update affected tests

* Attributes -> VFXAttributes

* Correct cast

* Update docs

* update changelog

* Docs review.

I've made some changes to the new sections. Please double-check for accuracy.

* Fixed *Sort* definition

* Same amount of mappings for global/peroutput

* Fix merge

* Remove try/catch

* Two similar criterion for global sort

* Return global sorting criterion only when needed

* FeedSortingKeys factorized

* Fix needsOwnSort condition

* add the graphics test

* Fix test failure

* Fix metal refs

* exclude 16_MeshParticles in XR

Co-authored-by: Vic-Cooper <vic.cooper@unity3d.com>
unity-emilk pushed a commit that referenced this pull request Oct 14, 2022
This PR backports some changes from the [Additional sorting keys PR](#5125) that fixes the flickering that happens for systems with capacity >4096.
It also includes some optimizations introduced in this [fix](https://ono.unity3d.com/unity/unity/pull-request/139471/_/graphics/vfx/1300251-bitonic-sort-low-end).

Please note that it is **not** a full backport of the Additional Sorting keys feature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants