metal_utils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**************************************************************************/
  2. /* metal_utils.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef METAL_UTILS_H
  31. #define METAL_UTILS_H
  32. #pragma mark - Boolean flags
  33. namespace flags {
  34. /*! Sets the flags within the value parameter specified by the mask parameter. */
  35. template <typename Tv, typename Tm>
  36. void set(Tv &p_value, Tm p_mask) {
  37. using T = std::underlying_type_t<Tv>;
  38. p_value = static_cast<Tv>(static_cast<T>(p_value) | static_cast<T>(p_mask));
  39. }
  40. /*! Clears the flags within the value parameter specified by the mask parameter. */
  41. template <typename Tv, typename Tm>
  42. void clear(Tv &p_value, Tm p_mask) {
  43. using T = std::underlying_type_t<Tv>;
  44. p_value = static_cast<Tv>(static_cast<T>(p_value) & ~static_cast<T>(p_mask));
  45. }
  46. /*! Returns whether the specified value has any of the bits specified in mask set to 1. */
  47. template <typename Tv, typename Tm>
  48. static constexpr bool any(Tv p_value, const Tm p_mask) { return ((p_value & p_mask) != 0); }
  49. /*! Returns whether the specified value has all of the bits specified in mask set to 1. */
  50. template <typename Tv, typename Tm>
  51. static constexpr bool all(Tv p_value, const Tm p_mask) { return ((p_value & p_mask) == p_mask); }
  52. } //namespace flags
  53. #pragma mark - Alignment and Offsets
  54. static constexpr bool is_power_of_two(uint64_t p_value) {
  55. return p_value && ((p_value & (p_value - 1)) == 0);
  56. }
  57. static constexpr uint64_t round_up_to_alignment(uint64_t p_value, uint64_t p_alignment) {
  58. DEV_ASSERT(is_power_of_two(p_alignment));
  59. if (p_alignment == 0) {
  60. return p_value;
  61. }
  62. uint64_t mask = p_alignment - 1;
  63. uint64_t aligned_value = (p_value + mask) & ~mask;
  64. return aligned_value;
  65. }
  66. #endif // METAL_UTILS_H