OpenXRVkUtils.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <OpenXRVk/OpenXRVkUtils.h>
  9. #include <AzCore/Debug/Trace.h>
  10. namespace OpenXRVk
  11. {
  12. AZ::RHI::ResultCode ConvertResult(XrResult xrResult)
  13. {
  14. switch (xrResult)
  15. {
  16. case XR_SUCCESS:
  17. return AZ::RHI::ResultCode::Success;
  18. case XR_ERROR_OUT_OF_MEMORY:
  19. return AZ::RHI::ResultCode::OutOfMemory;
  20. default:
  21. return AZ::RHI::ResultCode::Fail;
  22. }
  23. }
  24. bool IsSuccess(XrResult result)
  25. {
  26. if (result != XR_SUCCESS)
  27. {
  28. AZ_Error("XR", false, "ERROR: XR API method failed: %s", GetResultString(result));
  29. return false;
  30. }
  31. return true;
  32. }
  33. bool IsError(XrResult result)
  34. {
  35. return IsSuccess(result) == false;
  36. }
  37. const char* GetResultString(const XrResult result)
  38. {
  39. return to_string(result);
  40. }
  41. void PrintXrError([[maybe_unused]]const char* windowName, [[maybe_unused]]const XrResult error, const char* fmt, ...)
  42. {
  43. va_list argList;
  44. va_start(argList, fmt);
  45. const auto subMsg = AZStd::string::format_arg(fmt, argList);
  46. AZ_Error(windowName, false, "%s. Got OpenXR Error: %s\n", subMsg.c_str(), GetResultString(error));
  47. va_end(argList);
  48. }
  49. XR::RawStringList FilterList(const XR::RawStringList& source, const XR::StringList& filter)
  50. {
  51. XR::RawStringList filteredList;
  52. for (auto& item : source)
  53. {
  54. if (AZStd::find(filter.begin(), filter.end(), item) != filter.end())
  55. {
  56. filteredList.push_back(item);
  57. }
  58. }
  59. return filteredList;
  60. }
  61. AZStd::vector<const char*> ParseExtensionString(char* names)
  62. {
  63. AZStd::vector<const char*> list;
  64. while (*names != 0)
  65. {
  66. list.push_back(names);
  67. while (*(++names) != 0)
  68. {
  69. if (*names == ' ')
  70. {
  71. *names++ = '\0';
  72. break;
  73. }
  74. }
  75. }
  76. return list;
  77. }
  78. AZ::Quaternion AzQuaternionFromXrPose(const XrPosef& pose, bool convertCoordinates)
  79. {
  80. if (convertCoordinates)
  81. {
  82. return AZ::Quaternion(pose.orientation.x, -pose.orientation.z, pose.orientation.y, pose.orientation.w);
  83. }
  84. return AZ::Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w);
  85. }
  86. AZ::Vector3 AzVector3FromXrVector3(const XrVector3f& xrVec3, bool convertCoordinates)
  87. {
  88. return AZ::Vector3(xrVec3.x,
  89. convertCoordinates ? -xrVec3.z : xrVec3.y,
  90. convertCoordinates ? xrVec3.y : xrVec3.z);
  91. }
  92. AZ::Vector3 AzPositionFromXrPose(const XrPosef& pose, bool convertCoordinates)
  93. {
  94. return AzVector3FromXrVector3(pose.position, convertCoordinates);
  95. }
  96. AZ::Transform AzTransformFromXrPose(const XrPosef& pose, bool convertCoordinates)
  97. {
  98. const auto azQuat = AzQuaternionFromXrPose(pose, convertCoordinates);
  99. const auto azVec = AzPositionFromXrPose(pose, convertCoordinates);
  100. AZ::Transform tm = AZ::Transform::CreateFromQuaternionAndTranslation(azQuat, azVec);
  101. return tm;
  102. }
  103. XrPosef XrPoseFromAzTransform(const AZ::Transform& tm, bool convertCoordinates)
  104. {
  105. const auto &azQuat = tm.GetRotation();
  106. const auto &azPos = tm.GetTranslation();
  107. XrPosef pose;
  108. pose.orientation.x = azQuat.GetX();
  109. pose.orientation.y = convertCoordinates ? -azQuat.GetZ() : azQuat.GetY();
  110. pose.orientation.z = convertCoordinates ? azQuat.GetY() : azQuat.GetZ();
  111. pose.orientation.w = azQuat.GetW();
  112. pose.position.x = azPos.GetX();
  113. pose.position.y = convertCoordinates ? -azPos.GetZ() : azPos.GetY();
  114. pose.position.z = convertCoordinates ? azPos.GetY() : azPos.GetZ();
  115. return pose;
  116. }
  117. AZStd::string ConvertXrPathToString(XrInstance xrInstance, XrPath xrPath)
  118. {
  119. if ((xrInstance == XR_NULL_HANDLE) || (xrPath == XR_NULL_PATH))
  120. {
  121. return AZStd::string("");
  122. }
  123. constexpr uint32_t MaxBytes = 256;
  124. char pathAsChars[MaxBytes];
  125. uint32_t usedBytes = 0;
  126. XrResult result = xrPathToString(xrInstance, xrPath, MaxBytes, &usedBytes, pathAsChars);
  127. if (IsError(result))
  128. {
  129. PrintXrError("OpenXRVkUtils", result, "Failed to convert xrPath to a string with %u bytes.", MaxBytes);
  130. return AZStd::string("");
  131. }
  132. return AZStd::string(pathAsChars);
  133. }
  134. }