metal_utils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #import <os/log.h>
  33. #pragma mark - Boolean flags
  34. namespace flags {
  35. /*! Sets the flags within the value parameter specified by the mask parameter. */
  36. template <typename Tv, typename Tm>
  37. void set(Tv &p_value, Tm p_mask) {
  38. using T = std::underlying_type_t<Tv>;
  39. p_value = static_cast<Tv>(static_cast<T>(p_value) | static_cast<T>(p_mask));
  40. }
  41. /*! Clears the flags within the value parameter specified by the mask parameter. */
  42. template <typename Tv, typename Tm>
  43. void clear(Tv &p_value, Tm p_mask) {
  44. using T = std::underlying_type_t<Tv>;
  45. p_value = static_cast<Tv>(static_cast<T>(p_value) & ~static_cast<T>(p_mask));
  46. }
  47. /*! Returns whether the specified value has any of the bits specified in mask set to 1. */
  48. template <typename Tv, typename Tm>
  49. static constexpr bool any(Tv p_value, const Tm p_mask) {
  50. return ((p_value & p_mask) != 0);
  51. }
  52. /*! Returns whether the specified value has all of the bits specified in mask set to 1. */
  53. template <typename Tv, typename Tm>
  54. static constexpr bool all(Tv p_value, const Tm p_mask) {
  55. return ((p_value & p_mask) == p_mask);
  56. }
  57. } //namespace flags
  58. #pragma mark - Alignment and Offsets
  59. static constexpr bool is_power_of_two(uint64_t p_value) {
  60. return p_value && ((p_value & (p_value - 1)) == 0);
  61. }
  62. static constexpr uint64_t round_up_to_alignment(uint64_t p_value, uint64_t p_alignment) {
  63. DEV_ASSERT(is_power_of_two(p_alignment));
  64. if (p_alignment == 0) {
  65. return p_value;
  66. }
  67. uint64_t mask = p_alignment - 1;
  68. uint64_t aligned_value = (p_value + mask) & ~mask;
  69. return aligned_value;
  70. }
  71. class Defer {
  72. public:
  73. Defer(std::function<void()> func) :
  74. func_(func) {}
  75. ~Defer() { func_(); }
  76. private:
  77. std::function<void()> func_;
  78. };
  79. #define CONCAT_INTERNAL(x, y) x##y
  80. #define CONCAT(x, y) CONCAT_INTERNAL(x, y)
  81. #define DEFER const Defer &CONCAT(defer__, __LINE__) = Defer
  82. extern os_log_t LOG_DRIVER;
  83. // Used for dynamic tracing.
  84. extern os_log_t LOG_INTERVALS;
  85. #endif // METAL_UTILS_H