SortReverseAndStore.h 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. namespace JPH {
  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. } // JPH