|
| 1 | +/* |
| 2 | + * Copyright 2024 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#if GL_SUPPORTED |
| 30 | + |
| 31 | +# define XR_USE_GRAPHICS_API_OPENGL |
| 32 | +# include <openxr/openxr_platform.h> |
| 33 | + |
| 34 | +using XrGraphicsRequirementsGL = XrGraphicsRequirementsOpenGLKHR; |
| 35 | +using PFN_xrGetGLGraphicsRequirements = PFN_xrGetOpenGLGraphicsRequirementsKHR; |
| 36 | +static constexpr char xrGetGLGraphicsRequirementsFunctionName[] = "xrGetOpenGLGraphicsRequirementsKHR"; |
| 37 | +static constexpr XrStructureType XR_TYPE_GRAPHICS_REQUIREMENTS_GL = XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR; |
| 38 | + |
| 39 | + |
| 40 | +#else GLES_SUPPORTED |
| 41 | + |
| 42 | +# define XR_USE_GRAPHICS_API_OPENGL_ES |
| 43 | +# include <openxr/openxr_platform.h> |
| 44 | + |
| 45 | +using XrGraphicsRequirementsGL = XrGraphicsRequirementsOpenGLESKHR; |
| 46 | +using PFN_xrGetGLGraphicsRequirements = PFN_xrGetOpenGLESGraphicsRequirementsKHR; |
| 47 | +static constexpr char xrGetGLGraphicsRequirementsFunctionName[] = "xrGetOpenGLESGraphicsRequirementsKHR"; |
| 48 | +static constexpr XrStructureType XR_TYPE_GRAPHICS_REQUIREMENTS_GL = XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_ES_KHR; |
| 49 | + |
| 50 | +#endif |
| 51 | + |
| 52 | +namespace Diligent |
| 53 | +{ |
| 54 | + |
| 55 | +static Version GetOpenXRRequiredGLVersion(const OpenXRAttribs* pXR) noexcept(false) |
| 56 | +{ |
| 57 | + if (pXR == nullptr || pXR->Instance == 0) |
| 58 | + return {}; |
| 59 | + |
| 60 | + if (pXR->GetInstanceProcAddr == nullptr) |
| 61 | + LOG_ERROR_AND_THROW("xrGetInstanceProcAddr must not be null"); |
| 62 | + |
| 63 | + XrInstance xrInstance = XR_NULL_HANDLE; |
| 64 | + static_assert(sizeof(xrInstance) == sizeof(pXR->Instance), "XrInstance size mismatch"); |
| 65 | + memcpy(&xrInstance, &pXR->Instance, sizeof(xrInstance)); |
| 66 | + |
| 67 | + XrSystemId xrSystemId = XR_NULL_SYSTEM_ID; |
| 68 | + static_assert(sizeof(xrSystemId) == sizeof(pXR->SystemId), "XrSystemId size mismatch"); |
| 69 | + memcpy(&xrSystemId, &pXR->SystemId, sizeof(XrSystemId)); |
| 70 | + |
| 71 | + PFN_xrGetInstanceProcAddr xrGetInstanceProcAddr = reinterpret_cast<PFN_xrGetInstanceProcAddr>(pXR->GetInstanceProcAddr); |
| 72 | + PFN_xrGetGLGraphicsRequirements xrGetOpenGLGraphicsRequirements = nullptr; |
| 73 | + if (XR_FAILED(xrGetInstanceProcAddr(xrInstance, xrGetGLGraphicsRequirementsFunctionName, reinterpret_cast<PFN_xrVoidFunction*>(&xrGetOpenGLGraphicsRequirements)))) |
| 74 | + { |
| 75 | + LOG_ERROR_AND_THROW("Failed to get ", xrGetGLGraphicsRequirementsFunctionName, ". Make sure that XR_KHR_opengl_enable/XR_KHR_opengl_es_enable extension is enabled."); |
| 76 | + } |
| 77 | + |
| 78 | + XrGraphicsRequirementsGL xrGraphicsRequirements{XR_TYPE_GRAPHICS_REQUIREMENTS_GL}; |
| 79 | + if (XR_FAILED(xrGetOpenGLGraphicsRequirements(xrInstance, xrSystemId, &xrGraphicsRequirements))) |
| 80 | + { |
| 81 | + LOG_ERROR_AND_THROW("Failed to get OpenGL graphics requirements"); |
| 82 | + } |
| 83 | + |
| 84 | + return Version{ |
| 85 | + XR_VERSION_MAJOR(xrGraphicsRequirements.minApiVersionSupported), |
| 86 | + XR_VERSION_MINOR(xrGraphicsRequirements.minApiVersionSupported), |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace Diligent |
0 commit comments