| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include <openxr/openxr.h> |
| 5 | |
| 6 | #include <cassert> |
| 7 | |
| 8 | #include "Swapchain.h" |
| 9 | |
| 10 | using namespace osgXR::OpenXR; |
| 11 | |
| 12 | Swapchain::Swapchain(osg::ref_ptr<Session> session, |
| 13 | const System::ViewConfiguration::View &view, |
| 14 | XrSwapchainUsageFlags usageFlags, |
| 15 | int64_t format) : |
| 16 | _session(session), |
| 17 | _swapchain(XR_NULL_HANDLE), |
| 18 | _width(view.getRecommendedWidth()), |
| 19 | _height(view.getRecommendedHeight()), |
| 20 | _samples(view.getRecommendedSamples()), |
| 21 | _format(format), |
| 22 | _readImageTextures(false), |
| 23 | _released(false) |
| 24 | { |
| 25 | XrSwapchainCreateInfo createInfo{ XR_TYPE_SWAPCHAIN_CREATE_INFO }; |
| 26 | createInfo.usageFlags = usageFlags; |
| 27 | createInfo.format = format; |
| 28 | createInfo.sampleCount = _samples; |
| 29 | createInfo.width = _width; |
| 30 | createInfo.height = _height; |
| 31 | createInfo.faceCount = 1; |
| 32 | createInfo.arraySize = 1; |
| 33 | createInfo.mipCount = 1; |
| 34 | |
| 35 | bool switchContext = _session->shouldSwitchContext(); |
| 36 | auto restoreAction = _session->getRestoreAction(); |
| 37 | if (switchContext) |
| 38 | _session->makeCurrent(); |
| 39 | |
| 40 | // GL context must not be bound in another thread |
| 41 | check(xrCreateSwapchain(getXrSession(), &createInfo, &_swapchain), |
| 42 | "create OpenXR swapchain"); |
| 43 | |
| 44 | if (restoreAction == Session::CONTEXT_RESTORE) |
| 45 | _session->makeCurrent(); |
| 46 | else if (switchContext || restoreAction == Session::CONTEXT_RELEASE) |
| 47 | _session->releaseContext(); |
| 48 | } |
| 49 | |
| 50 | Swapchain::~Swapchain() |
| 51 | { |
| 52 | if (_session->valid() && valid()) |
| 53 | { |
| 54 | // GL context must not be bound in another thread |
| 55 | check(xrDestroySwapchain(_swapchain), |
| 56 | "destroy OpenXR swapchain"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | const Swapchain::ImageTextures &Swapchain::getImageTextures() const |
| 61 | { |
| 62 | if (!_readImageTextures) |
| 63 | { |
| 64 | // Enumerate the images |
| 65 | uint32_t imageCount; |
| 66 | // GL context must not be bound in another thread |
| 67 | if (check(xrEnumerateSwapchainImages(_swapchain, 0, &imageCount, nullptr), |
| 68 | "count OpenXR swapchain images")) |
| 69 | { |
| 70 | if (imageCount) |
| 71 | { |
| 72 | std::vector<XrSwapchainImageOpenGLKHR> images(imageCount, { XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR }); |
| 73 | if (check(xrEnumerateSwapchainImages(_swapchain, images.size(), &imageCount, |
| 74 | (XrSwapchainImageBaseHeader *)images.data()), |
| 75 | "enumerate OpenXR swapchain images")) |
| 76 | { |
| 77 | for (auto image: images) |
| 78 | _imageTextures.push_back(image.image); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | _readImageTextures = true; |
| 84 | } |
| 85 | |
| 86 | return _imageTextures; |
| 87 | } |
| 88 | |
| 89 | osg::ref_ptr<osg::Texture2D> Swapchain::getImageOsgTexture(unsigned int index) const |
| 90 | { |
| 91 | if (_imageOsgTextures.empty()) |
| 92 | { |
| 93 | getImageTextures(); |
| 94 | _imageOsgTextures.resize(_imageTextures.size()); |
| 95 | } |
| 96 | |
| 97 | assert(index < _imageOsgTextures.size()); |
| 98 | if (!_imageOsgTextures[index].valid()) |
| 99 | { |
| 100 | // Create an OSG texture out of it |
| 101 | osg::Texture2D *texture = new osg::Texture2D; |
| 102 | texture->setTextureSize(getWidth(), |
| 103 | getHeight()); |
| 104 | texture->setInternalFormat(getFormat()); |
| 105 | unsigned int contextID = _session->getWindow()->getState()->getContextID(); |
| 106 | texture->setTextureObject(contextID, new osg::Texture::TextureObject(texture, _imageTextures[index], GL_TEXTURE_2D)); |
| 107 | // Disable mipmapping |
| 108 | texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); |
| 109 | |
| 110 | _imageOsgTextures[index] = texture; |
| 111 | } |
| 112 | |
| 113 | return _imageOsgTextures[index]; |
| 114 | } |
| 115 | |
| 116 | int Swapchain::acquireImage() const |
| 117 | { |
| 118 | // Acquire a swapchain image |
| 119 | uint32_t imageIndex; |
| 120 | |
| 121 | bool restoreContext = _session->shouldRestoreContext(); |
| 122 | // GL context must not be bound in another thread |
| 123 | if (check(xrAcquireSwapchainImage(_swapchain, nullptr, &imageIndex), |
| 124 | "acquire swapchain image")) |
| 125 | { |
| 126 | if (restoreContext) |
| 127 | _session->makeCurrent(); |
| 128 | |
| 129 | return imageIndex; |
| 130 | } |
| 131 | |
| 132 | if (restoreContext) |
| 133 | _session->makeCurrent(); |
| 134 | |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | bool Swapchain::waitImage(XrDuration timeoutNs) const |
| 139 | { |
| 140 | // Wait on the swapchain image |
| 141 | XrSwapchainImageWaitInfo waitInfo = { XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO }; |
| 142 | waitInfo.timeout = timeoutNs; // 100ms |
| 143 | |
| 144 | bool restoreContext = _session->shouldRestoreContext(); |
| 145 | // GL context must not be bound in another thread |
| 146 | bool ret = check(xrWaitSwapchainImage(_swapchain, &waitInfo), |
| 147 | "wait for swapchain image"); |
| 148 | |
| 149 | if (restoreContext) |
| 150 | _session->makeCurrent(); |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | void Swapchain::releaseImage() const |
| 156 | { |
| 157 | // Release the swapchain image |
| 158 | bool restoreContext = _session->shouldRestoreContext(); |
| 159 | // GL context must not be bound in another thread |
| 160 | if (check(xrReleaseSwapchainImage(_swapchain, nullptr), |
| 161 | "release OpenXR swapchain image")) |
| 162 | _released = true; |
| 163 | |
| 164 | if (restoreContext) |
| 165 | _session->makeCurrent(); |
| 166 | } |