SortReverseAndStore.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// This function will sort values from high to low and only keep the ones that are less than inMaxValue
  6. /// @param inValues Values to be sorted
  7. /// @param inMaxValue Values need to be less than this to keep them
  8. /// @param ioIdentifiers 4 identifiers that will be sorted in the same way as the values
  9. /// @param outValues The values are stored here from high to low
  10. /// @return The number of values that were kept
  11. JPH_INLINE int SortReverseAndStore(Vec4Arg inValues, float inMaxValue, UVec4 &ioIdentifiers, float *outValues)
  12. {
  13. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  14. Vec4::sSort4Reverse(inValues, ioIdentifiers);
  15. // Count how many results are less than the max value
  16. UVec4 closer = Vec4::sLess(inValues, Vec4::sReplicate(inMaxValue));
  17. int num_results = closer.CountTrues();
  18. // Shift the values so that only the ones that are less than max are kept
  19. inValues = inValues.ReinterpretAsInt().ShiftComponents4Minus(num_results).ReinterpretAsFloat();
  20. ioIdentifiers = ioIdentifiers.ShiftComponents4Minus(num_results);
  21. // Store the values
  22. inValues.StoreFloat4((Float4 *)outValues);
  23. return num_results;
  24. }
  25. /// Shift the elements so that the identifiers that correspond with the trues in inValue come first
  26. /// @param inValue Values to test for true or false
  27. /// @param ioIdentifiers the identifiers that are shifted, on return they are shifted
  28. /// @return The number of trues
  29. JPH_INLINE int CountAndSortTrues(UVec4Arg inValue, UVec4 &ioIdentifiers)
  30. {
  31. // Sort the hits
  32. ioIdentifiers = UVec4::sSort4True(inValue, ioIdentifiers);
  33. // Return the amount of hits
  34. return inValue.CountTrues();
  35. }
  36. JPH_NAMESPACE_END