SortReverseAndStore.h 1.9 KB

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